dasd_eckd.c 142.4 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2
 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3
 *		    Horst Hummel <Horst.Hummel@de.ibm.com>
L
Linus Torvalds 已提交
4 5 6
 *		    Carsten Otte <Cotte@de.ibm.com>
 *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
 * Bugreports.to..: <Linux390@de.ibm.com>
7
 * Copyright IBM Corp. 1999, 2009
8 9
 * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
 * Author.........: Nigel Hislop <hislop_nigel@emc.com>
L
Linus Torvalds 已提交
10 11
 */

12
#define KMSG_COMPONENT "dasd-eckd"
S
Stefan Haberland 已提交
13

L
Linus Torvalds 已提交
14 15 16 17 18 19
#include <linux/stddef.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/hdreg.h>	/* HDIO_GETGEO			    */
#include <linux/bio.h>
#include <linux/module.h>
20
#include <linux/compat.h>
L
Linus Torvalds 已提交
21 22
#include <linux/init.h>

S
Sebastian Ott 已提交
23
#include <asm/css_chars.h>
L
Linus Torvalds 已提交
24 25 26 27 28
#include <asm/debug.h>
#include <asm/idals.h>
#include <asm/ebcdic.h>
#include <asm/io.h>
#include <asm/uaccess.h>
29
#include <asm/cio.h>
L
Linus Torvalds 已提交
30
#include <asm/ccwdev.h>
31
#include <asm/itcw.h>
32 33
#include <asm/schid.h>
#include <asm/chpid.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

#include "dasd_int.h"
#include "dasd_eckd.h"

#ifdef PRINTK_HEADER
#undef PRINTK_HEADER
#endif				/* PRINTK_HEADER */
#define PRINTK_HEADER "dasd(eckd):"

#define ECKD_C0(i) (i->home_bytes)
#define ECKD_F(i) (i->formula)
#define ECKD_F1(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f1):\
		    (i->factors.f_0x02.f1))
#define ECKD_F2(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f2):\
		    (i->factors.f_0x02.f2))
#define ECKD_F3(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f3):\
		    (i->factors.f_0x02.f3))
#define ECKD_F4(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f4):0)
#define ECKD_F5(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f5):0)
#define ECKD_F6(i) (i->factor6)
#define ECKD_F7(i) (i->factor7)
#define ECKD_F8(i) (i->factor8)

57 58 59 60 61 62 63 64 65
/*
 * raw track access always map to 64k in memory
 * so it maps to 16 blocks of 4k per track
 */
#define DASD_RAW_BLOCK_PER_TRACK 16
#define DASD_RAW_BLOCKSIZE 4096
/* 64k are 128 x 512 byte sectors  */
#define DASD_RAW_SECTORS_PER_TRACK 128

L
Linus Torvalds 已提交
66 67 68 69 70 71 72
MODULE_LICENSE("GPL");

static struct dasd_discipline dasd_eckd_discipline;

/* The ccw bus type uses this table to find devices that it sends to
 * dasd_eckd_probe */
static struct ccw_device_id dasd_eckd_ids[] = {
H
Heiko Carstens 已提交
73 74
	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
75
	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
H
Heiko Carstens 已提交
76 77 78 79 80 81 82
	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), .driver_info = 0x4},
	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), .driver_info = 0x5},
	{ CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), .driver_info = 0x6},
	{ CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), .driver_info = 0x7},
	{ CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), .driver_info = 0x8},
	{ CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), .driver_info = 0x9},
	{ CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), .driver_info = 0xa},
L
Linus Torvalds 已提交
83 84 85 86 87 88 89
	{ /* end of list */ },
};

MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);

static struct ccw_driver dasd_eckd_driver; /* see below */

90 91
static void *rawpadpage;

92 93 94 95
#define INIT_CQR_OK 0
#define INIT_CQR_UNFORMATTED 1
#define INIT_CQR_ERROR 2

96 97 98 99 100 101 102 103
/* emergency request for reserve/release */
static struct {
	struct dasd_ccw_req cqr;
	struct ccw1 ccw;
	char data[32];
} *dasd_reserve_req;
static DEFINE_MUTEX(dasd_reserve_mutex);

104 105 106 107 108 109 110 111 112 113 114 115
/* definitions for the path verification worker */
struct path_verification_work_data {
	struct work_struct worker;
	struct dasd_device *device;
	struct dasd_ccw_req cqr;
	struct ccw1 ccw;
	__u8 rcd_buffer[DASD_ECKD_RCD_DATA_SIZE];
	int isglobal;
	__u8 tbvpm;
};
static struct path_verification_work_data *path_verification_worker;
static DEFINE_MUTEX(dasd_path_verification_mutex);
116

117 118 119 120 121 122
struct check_attention_work_data {
	struct work_struct worker;
	struct dasd_device *device;
	__u8 lpum;
};

L
Linus Torvalds 已提交
123 124 125 126 127 128 129
/* initial attempt at a probe function. this can be simplified once
 * the other detection code is gone */
static int
dasd_eckd_probe (struct ccw_device *cdev)
{
	int ret;

130
	/* set ECKD specific ccw-device options */
131 132
	ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
				     CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
133
	if (ret) {
134 135 136
		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
				"dasd_eckd_probe: could not set "
				"ccw-device options");
L
Linus Torvalds 已提交
137
		return ret;
138 139 140
	}
	ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
	return ret;
L
Linus Torvalds 已提交
141 142 143 144 145
}

static int
dasd_eckd_set_online(struct ccw_device *cdev)
{
146
	return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
L
Linus Torvalds 已提交
147 148 149 150 151
}

static const int sizes_trk0[] = { 28, 148, 84 };
#define LABEL_SIZE 140

152 153 154 155
/* head and record addresses of count_area read in analysis ccw */
static const int count_area_head[] = { 0, 0, 0, 0, 2 };
static const int count_area_rec[] = { 1, 2, 3, 4, 1 };

L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
static inline unsigned int
round_up_multiple(unsigned int no, unsigned int mult)
{
	int rem = no % mult;
	return (rem ? no - rem + mult : no);
}

static inline unsigned int
ceil_quot(unsigned int d1, unsigned int d2)
{
	return (d1 + (d2 - 1)) / d2;
}

169
static unsigned int
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
recs_per_track(struct dasd_eckd_characteristics * rdc,
	       unsigned int kl, unsigned int dl)
{
	int dn, kn;

	switch (rdc->dev_type) {
	case 0x3380:
		if (kl)
			return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
				       ceil_quot(dl + 12, 32));
		else
			return 1499 / (15 + ceil_quot(dl + 12, 32));
	case 0x3390:
		dn = ceil_quot(dl + 6, 232) + 1;
		if (kl) {
			kn = ceil_quot(kl + 6, 232) + 1;
			return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
				       9 + ceil_quot(dl + 6 * dn, 34));
		} else
			return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
	case 0x9345:
		dn = ceil_quot(dl + 6, 232) + 1;
		if (kl) {
			kn = ceil_quot(kl + 6, 232) + 1;
			return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
				       ceil_quot(dl + 6 * dn, 34));
		} else
			return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
	}
	return 0;
}

202 203 204 205 206 207 208 209
static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
{
	geo->cyl = (__u16) cyl;
	geo->head = cyl >> 16;
	geo->head <<= 4;
	geo->head |= head;
}

210
static int
L
Linus Torvalds 已提交
211 212 213 214 215
check_XRC (struct ccw1         *de_ccw,
           struct DE_eckd_data *data,
           struct dasd_device  *device)
{
        struct dasd_eckd_private *private;
M
Martin Schwidefsky 已提交
216
	int rc;
L
Linus Torvalds 已提交
217 218

        private = (struct dasd_eckd_private *) device->private;
M
Martin Schwidefsky 已提交
219 220
	if (!private->rdc_data.facilities.XRC_supported)
		return 0;
L
Linus Torvalds 已提交
221 222

        /* switch on System Time Stamp - needed for XRC Support */
M
Martin Schwidefsky 已提交
223 224
	data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid'   */
	data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
L
Linus Torvalds 已提交
225

M
Martin Schwidefsky 已提交
226 227
	rc = get_sync_clock(&data->ep_sys_time);
	/* Ignore return code if sync clock is switched off. */
228
	if (rc == -EOPNOTSUPP || rc == -EACCES)
M
Martin Schwidefsky 已提交
229
		rc = 0;
L
Linus Torvalds 已提交
230

231
	de_ccw->count = sizeof(struct DE_eckd_data);
M
Martin Schwidefsky 已提交
232 233 234
	de_ccw->flags |= CCW_FLAG_SLI;
	return rc;
}
L
Linus Torvalds 已提交
235

236
static int
237 238
define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
	      unsigned int totrk, int cmd, struct dasd_device *device)
L
Linus Torvalds 已提交
239 240
{
	struct dasd_eckd_private *private;
241 242
	u32 begcyl, endcyl;
	u16 heads, beghead, endhead;
M
Martin Schwidefsky 已提交
243
	int rc = 0;
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251

	private = (struct dasd_eckd_private *) device->private;

	ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
	ccw->flags = 0;
	ccw->count = 16;
	ccw->cda = (__u32) __pa(data);

252
	memset(data, 0, sizeof(struct DE_eckd_data));
L
Linus Torvalds 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	switch (cmd) {
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
	case DASD_ECKD_CCW_READ:
	case DASD_ECKD_CCW_READ_MT:
	case DASD_ECKD_CCW_READ_CKD:
	case DASD_ECKD_CCW_READ_CKD_MT:
	case DASD_ECKD_CCW_READ_KD:
	case DASD_ECKD_CCW_READ_KD_MT:
	case DASD_ECKD_CCW_READ_COUNT:
		data->mask.perm = 0x1;
		data->attributes.operation = private->attrib.operation;
		break;
	case DASD_ECKD_CCW_WRITE:
	case DASD_ECKD_CCW_WRITE_MT:
	case DASD_ECKD_CCW_WRITE_KD:
	case DASD_ECKD_CCW_WRITE_KD_MT:
		data->mask.perm = 0x02;
		data->attributes.operation = private->attrib.operation;
M
Martin Schwidefsky 已提交
272
		rc = check_XRC (ccw, data, device);
L
Linus Torvalds 已提交
273 274 275 276
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->attributes.operation = DASD_BYPASS_CACHE;
M
Martin Schwidefsky 已提交
277
		rc = check_XRC (ccw, data, device);
L
Linus Torvalds 已提交
278 279 280 281 282 283 284
		break;
	case DASD_ECKD_CCW_ERASE:
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
		data->mask.perm = 0x3;
		data->mask.auth = 0x1;
		data->attributes.operation = DASD_BYPASS_CACHE;
M
Martin Schwidefsky 已提交
285
		rc = check_XRC (ccw, data, device);
L
Linus Torvalds 已提交
286 287
		break;
	default:
S
Stefan Haberland 已提交
288 289
		dev_err(&device->cdev->dev,
			"0x%x is not a known command\n", cmd);
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299 300
		break;
	}

	data->attributes.mode = 0x3;	/* ECKD */

	if ((private->rdc_data.cu_type == 0x2105 ||
	     private->rdc_data.cu_type == 0x2107 ||
	     private->rdc_data.cu_type == 0x1750)
	    && !(private->uses_cdl && trk < 2))
		data->ga_extended |= 0x40; /* Regular Data Format Mode */

301 302 303 304 305
	heads = private->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;
L
Linus Torvalds 已提交
306 307 308 309

	/* check for sequential prestage - enhance cylinder range */
	if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
	    data->attributes.operation == DASD_SEQ_ACCESS) {
310

311 312
		if (endcyl + private->attrib.nr_cyl < private->real_cyl)
			endcyl += private->attrib.nr_cyl;
L
Linus Torvalds 已提交
313
		else
314
			endcyl = (private->real_cyl - 1);
L
Linus Torvalds 已提交
315 316
	}

317 318
	set_ch_t(&data->beg_ext, begcyl, beghead);
	set_ch_t(&data->end_ext, endcyl, endhead);
M
Martin Schwidefsky 已提交
319
	return rc;
L
Linus Torvalds 已提交
320 321
}

322 323 324 325 326 327 328 329 330 331 332
static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
			       struct dasd_device  *device)
{
	struct dasd_eckd_private *private;
	int rc;

	private = (struct dasd_eckd_private *) device->private;
	if (!private->rdc_data.facilities.XRC_supported)
		return 0;

	/* switch on System Time Stamp - needed for XRC Support */
333 334
	pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid'   */
	pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
335 336
	pfxdata->validity.time_stamp = 1;	    /* 'Time Stamp Valid'   */

337
	rc = get_sync_clock(&pfxdata->define_extent.ep_sys_time);
338
	/* Ignore return code if sync clock is switched off. */
339
	if (rc == -EOPNOTSUPP || rc == -EACCES)
340 341 342 343
		rc = 0;
	return rc;
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
			  unsigned int rec_on_trk, int count, int cmd,
			  struct dasd_device *device, unsigned int reclen,
			  unsigned int tlf)
{
	struct dasd_eckd_private *private;
	int sector;
	int dn, d;

	private = (struct dasd_eckd_private *) device->private;

	memset(data, 0, sizeof(*data));
	sector = 0;
	if (rec_on_trk) {
		switch (private->rdc_data.dev_type) {
		case 0x3390:
			dn = ceil_quot(reclen + 6, 232);
			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
			break;
		case 0x3380:
			d = 7 + ceil_quot(reclen + 12, 32);
			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
			break;
		}
	}
	data->sector = sector;
	/* note: meaning of count depends on the operation
	 *	 for record based I/O it's the number of records, but for
	 *	 track based I/O it's the number of tracks
	 */
	data->count = count;
	switch (cmd) {
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x03;
		break;
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x16;
		break;
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
		data->operation.orientation = 0x1;
		data->operation.operation = 0x03;
		data->count++;
		break;
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x16;
		data->count++;
		break;
	case DASD_ECKD_CCW_WRITE:
	case DASD_ECKD_CCW_WRITE_MT:
	case DASD_ECKD_CCW_WRITE_KD:
	case DASD_ECKD_CCW_WRITE_KD_MT:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;
		data->operation.operation = 0x01;
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;
		data->operation.operation = 0x03;
		break;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
		data->operation.orientation = 0x0;
		data->operation.operation = 0x3F;
		data->extended_operation = 0x11;
		data->length = 0;
		data->extended_parameter_length = 0x02;
		if (data->count > 8) {
			data->extended_parameter[0] = 0xFF;
			data->extended_parameter[1] = 0xFF;
			data->extended_parameter[1] <<= (16 - count);
		} else {
			data->extended_parameter[0] = 0xFF;
			data->extended_parameter[0] <<= (8 - count);
			data->extended_parameter[1] = 0x00;
		}
		data->sector = 0xFF;
		break;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;	/* not tlf, as one might think */
		data->operation.operation = 0x3F;
		data->extended_operation = 0x23;
		break;
	case DASD_ECKD_CCW_READ:
	case DASD_ECKD_CCW_READ_MT:
	case DASD_ECKD_CCW_READ_KD:
	case DASD_ECKD_CCW_READ_KD_MT:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;
		data->operation.operation = 0x06;
		break;
	case DASD_ECKD_CCW_READ_CKD:
	case DASD_ECKD_CCW_READ_CKD_MT:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;
		data->operation.operation = 0x16;
		break;
	case DASD_ECKD_CCW_READ_COUNT:
		data->operation.operation = 0x06;
		break;
449 450 451 452 453 454
	case DASD_ECKD_CCW_READ_TRACK:
		data->operation.orientation = 0x1;
		data->operation.operation = 0x0C;
		data->extended_parameter_length = 0;
		data->sector = 0xFF;
		break;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	case DASD_ECKD_CCW_READ_TRACK_DATA:
		data->auxiliary.length_valid = 0x1;
		data->length = tlf;
		data->operation.operation = 0x0C;
		break;
	case DASD_ECKD_CCW_ERASE:
		data->length = reclen;
		data->auxiliary.length_valid = 0x1;
		data->operation.operation = 0x0b;
		break;
	default:
		DBF_DEV_EVENT(DBF_ERR, device,
			    "fill LRE unknown opcode 0x%x", cmd);
		BUG();
	}
	set_ch_t(&data->seek_addr,
		 trk / private->rdc_data.trk_per_cyl,
		 trk % private->rdc_data.trk_per_cyl);
	data->search_arg.cyl = data->seek_addr.cyl;
	data->search_arg.head = data->seek_addr.head;
	data->search_arg.record = rec_on_trk;
}

static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
		      unsigned int trk, unsigned int totrk, int cmd,
		      struct dasd_device *basedev, struct dasd_device *startdev,
		      unsigned char format, unsigned int rec_on_trk, int count,
		      unsigned int blksize, unsigned int tlf)
483 484
{
	struct dasd_eckd_private *basepriv, *startpriv;
485 486
	struct DE_eckd_data *dedata;
	struct LRE_eckd_data *lredata;
487 488
	u32 begcyl, endcyl;
	u16 heads, beghead, endhead;
489 490 491 492
	int rc = 0;

	basepriv = (struct dasd_eckd_private *) basedev->private;
	startpriv = (struct dasd_eckd_private *) startdev->private;
493 494
	dedata = &pfxdata->define_extent;
	lredata = &pfxdata->locate_record;
495 496 497

	ccw->cmd_code = DASD_ECKD_CCW_PFX;
	ccw->flags = 0;
498 499 500 501 502 503 504 505 506
	if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK) {
		ccw->count = sizeof(*pfxdata) + 2;
		ccw->cda = (__u32) __pa(pfxdata);
		memset(pfxdata, 0, sizeof(*pfxdata) + 2);
	} else {
		ccw->count = sizeof(*pfxdata);
		ccw->cda = (__u32) __pa(pfxdata);
		memset(pfxdata, 0, sizeof(*pfxdata));
	}
507 508

	/* prefix data */
509 510 511 512 513 514 515
	if (format > 1) {
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "PFX LRE unknown format 0x%x", format);
		BUG();
		return -EINVAL;
	}
	pfxdata->format = format;
516 517
	pfxdata->base_address = basepriv->ned->unit_addr;
	pfxdata->base_lss = basepriv->ned->ID;
518
	pfxdata->validity.define_extent = 1;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

	/* private uid is kept up to date, conf_data may be outdated */
	if (startpriv->uid.type != UA_BASE_DEVICE) {
		pfxdata->validity.verify_base = 1;
		if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
			pfxdata->validity.hyper_pav = 1;
	}

	/* define extend data (mostly)*/
	switch (cmd) {
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
	case DASD_ECKD_CCW_READ:
	case DASD_ECKD_CCW_READ_MT:
	case DASD_ECKD_CCW_READ_CKD:
	case DASD_ECKD_CCW_READ_CKD_MT:
	case DASD_ECKD_CCW_READ_KD:
	case DASD_ECKD_CCW_READ_KD_MT:
	case DASD_ECKD_CCW_READ_COUNT:
538 539 540
		dedata->mask.perm = 0x1;
		dedata->attributes.operation = basepriv->attrib.operation;
		break;
541
	case DASD_ECKD_CCW_READ_TRACK:
542 543 544 545
	case DASD_ECKD_CCW_READ_TRACK_DATA:
		dedata->mask.perm = 0x1;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = 0;
546 547 548 549 550
		break;
	case DASD_ECKD_CCW_WRITE:
	case DASD_ECKD_CCW_WRITE_MT:
	case DASD_ECKD_CCW_WRITE_KD:
	case DASD_ECKD_CCW_WRITE_KD_MT:
551 552
		dedata->mask.perm = 0x02;
		dedata->attributes.operation = basepriv->attrib.operation;
553 554 555 556
		rc = check_XRC_on_prefix(pfxdata, basedev);
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
557
		dedata->attributes.operation = DASD_BYPASS_CACHE;
558 559 560 561 562
		rc = check_XRC_on_prefix(pfxdata, basedev);
		break;
	case DASD_ECKD_CCW_ERASE:
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
563 564 565
		dedata->mask.perm = 0x3;
		dedata->mask.auth = 0x1;
		dedata->attributes.operation = DASD_BYPASS_CACHE;
566 567
		rc = check_XRC_on_prefix(pfxdata, basedev);
		break;
568 569 570 571 572
	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
		dedata->mask.perm = 0x03;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = 0;
		break;
573 574 575 576 577
	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
		dedata->mask.perm = 0x02;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = blksize;
		rc = check_XRC_on_prefix(pfxdata, basedev);
578
		break;
579 580 581 582 583
	default:
		DBF_DEV_EVENT(DBF_ERR, basedev,
			    "PFX LRE unknown opcode 0x%x", cmd);
		BUG();
		return -EINVAL;
584 585
	}

586
	dedata->attributes.mode = 0x3;	/* ECKD */
587 588 589 590 591

	if ((basepriv->rdc_data.cu_type == 0x2105 ||
	     basepriv->rdc_data.cu_type == 0x2107 ||
	     basepriv->rdc_data.cu_type == 0x1750)
	    && !(basepriv->uses_cdl && trk < 2))
592
		dedata->ga_extended |= 0x40; /* Regular Data Format Mode */
593

594 595 596 597 598
	heads = basepriv->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;
599 600

	/* check for sequential prestage - enhance cylinder range */
601 602
	if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
	    dedata->attributes.operation == DASD_SEQ_ACCESS) {
603

604 605
		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
			endcyl += basepriv->attrib.nr_cyl;
606
		else
607
			endcyl = (basepriv->real_cyl - 1);
608 609
	}

610 611 612 613 614 615 616 617
	set_ch_t(&dedata->beg_ext, begcyl, beghead);
	set_ch_t(&dedata->end_ext, endcyl, endhead);

	if (format == 1) {
		fill_LRE_data(lredata, trk, rec_on_trk, count, cmd,
			      basedev, blksize, tlf);
	}

618 619 620
	return rc;
}

621 622 623 624 625 626 627 628
static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
		  unsigned int trk, unsigned int totrk, int cmd,
		  struct dasd_device *basedev, struct dasd_device *startdev)
{
	return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
			  0, 0, 0, 0, 0);
}

629
static void
630 631
locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
	      unsigned int rec_on_trk, int no_rec, int cmd,
L
Linus Torvalds 已提交
632 633 634 635 636
	      struct dasd_device * device, int reclen)
{
	struct dasd_eckd_private *private;
	int sector;
	int dn, d;
637

L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648
	private = (struct dasd_eckd_private *) device->private;

	DBF_DEV_EVENT(DBF_INFO, device,
		  "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
		  trk, rec_on_trk, no_rec, cmd, reclen);

	ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
	ccw->flags = 0;
	ccw->count = 16;
	ccw->cda = (__u32) __pa(data);

649
	memset(data, 0, sizeof(struct LO_eckd_data));
L
Linus Torvalds 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	sector = 0;
	if (rec_on_trk) {
		switch (private->rdc_data.dev_type) {
		case 0x3390:
			dn = ceil_quot(reclen + 6, 232);
			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
			break;
		case 0x3380:
			d = 7 + ceil_quot(reclen + 12, 32);
			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
			break;
		}
	}
	data->sector = sector;
	data->count = no_rec;
	switch (cmd) {
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x03;
		break;
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x16;
		break;
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
		data->operation.orientation = 0x1;
		data->operation.operation = 0x03;
		data->count++;
		break;
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x16;
		data->count++;
		break;
	case DASD_ECKD_CCW_WRITE:
	case DASD_ECKD_CCW_WRITE_MT:
	case DASD_ECKD_CCW_WRITE_KD:
	case DASD_ECKD_CCW_WRITE_KD_MT:
		data->auxiliary.last_bytes_used = 0x1;
		data->length = reclen;
		data->operation.operation = 0x01;
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->auxiliary.last_bytes_used = 0x1;
		data->length = reclen;
		data->operation.operation = 0x03;
		break;
	case DASD_ECKD_CCW_READ:
	case DASD_ECKD_CCW_READ_MT:
	case DASD_ECKD_CCW_READ_KD:
	case DASD_ECKD_CCW_READ_KD_MT:
		data->auxiliary.last_bytes_used = 0x1;
		data->length = reclen;
		data->operation.operation = 0x06;
		break;
	case DASD_ECKD_CCW_READ_CKD:
	case DASD_ECKD_CCW_READ_CKD_MT:
		data->auxiliary.last_bytes_used = 0x1;
		data->length = reclen;
		data->operation.operation = 0x16;
		break;
	case DASD_ECKD_CCW_READ_COUNT:
		data->operation.operation = 0x06;
		break;
	case DASD_ECKD_CCW_ERASE:
		data->length = reclen;
		data->auxiliary.last_bytes_used = 0x1;
		data->operation.operation = 0x0b;
		break;
	default:
S
Stefan Haberland 已提交
722 723
		DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
			      "opcode 0x%x", cmd);
L
Linus Torvalds 已提交
724
	}
725 726 727 728 729
	set_ch_t(&data->seek_addr,
		 trk / private->rdc_data.trk_per_cyl,
		 trk % private->rdc_data.trk_per_cyl);
	data->search_arg.cyl = data->seek_addr.cyl;
	data->search_arg.head = data->seek_addr.head;
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	data->search_arg.record = rec_on_trk;
}

/*
 * Returns 1 if the block is one of the special blocks that needs
 * to get read/written with the KD variant of the command.
 * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
 * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
 * Luckily the KD variants differ only by one bit (0x08) from the
 * normal variant. So don't wonder about code like:
 * if (dasd_eckd_cdl_special(blk_per_trk, recid))
 *         ccw->cmd_code |= 0x8;
 */
static inline int
dasd_eckd_cdl_special(int blk_per_trk, int recid)
{
	if (recid < 3)
		return 1;
	if (recid < blk_per_trk)
		return 0;
	if (recid < 2 * blk_per_trk)
		return 1;
	return 0;
}

/*
 * Returns the record size for the special blocks of the cdl format.
 * Only returns something useful if dasd_eckd_cdl_special is true
 * for the recid.
 */
static inline int
dasd_eckd_cdl_reclen(int recid)
{
	if (recid < 3)
		return sizes_trk0[recid];
	return LABEL_SIZE;
}
767 768
/* create unique id from private structure. */
static void create_uid(struct dasd_eckd_private *private)
769
{
770
	int count;
771
	struct dasd_uid *uid;
772

773
	uid = &private->uid;
774
	memset(uid, 0, sizeof(struct dasd_uid));
775
	memcpy(uid->vendor, private->ned->HDA_manufacturer,
776
	       sizeof(uid->vendor) - 1);
777
	EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
778
	memcpy(uid->serial, private->ned->HDA_location,
779
	       sizeof(uid->serial) - 1);
780
	EBCASC(uid->serial, sizeof(uid->serial) - 1);
781
	uid->ssid = private->gneq->subsystemID;
782
	uid->real_unit_addr = private->ned->unit_addr;
783 784
	if (private->sneq) {
		uid->type = private->sneq->sua_flags;
785
		if (uid->type == UA_BASE_PAV_ALIAS)
786
			uid->base_unit_addr = private->sneq->base_unit_addr;
787 788 789
	} else {
		uid->type = UA_BASE_DEVICE;
	}
790 791 792 793 794 795
	if (private->vdsneq) {
		for (count = 0; count < 16; count++) {
			sprintf(uid->vduit+2*count, "%02x",
				private->vdsneq->uit[count]);
		}
	}
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
}

/*
 * Generate device unique id that specifies the physical device.
 */
static int dasd_eckd_generate_uid(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
	unsigned long flags;

	private = (struct dasd_eckd_private *) device->private;
	if (!private)
		return -ENODEV;
	if (!private->ned || !private->gneq)
		return -ENODEV;
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
	create_uid(private);
813
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
814 815 816
	return 0;
}

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
{
	struct dasd_eckd_private *private;
	unsigned long flags;

	if (device->private) {
		private = (struct dasd_eckd_private *)device->private;
		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
		*uid = private->uid;
		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
		return 0;
	}
	return -EINVAL;
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
/*
 * compare device UID with data of a given dasd_eckd_private structure
 * return 0 for match
 */
static int dasd_eckd_compare_path_uid(struct dasd_device *device,
				      struct dasd_eckd_private *private)
{
	struct dasd_uid device_uid;

	create_uid(private);
	dasd_eckd_get_uid(device, &device_uid);

	return memcmp(&device_uid, &private->uid, sizeof(struct dasd_uid));
}

847 848 849 850
static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
				   struct dasd_ccw_req *cqr,
				   __u8 *rcd_buffer,
				   __u8 lpm)
851 852
{
	struct ccw1 *ccw;
853 854 855 856 857 858 859 860
	/*
	 * buffer has to start with EBCDIC "V1.0" to show
	 * support for virtual device SNEQ
	 */
	rcd_buffer[0] = 0xE5;
	rcd_buffer[1] = 0xF1;
	rcd_buffer[2] = 0x4B;
	rcd_buffer[3] = 0xF0;
861 862

	ccw = cqr->cpaddr;
863 864
	ccw->cmd_code = DASD_ECKD_CCW_RCD;
	ccw->flags = 0;
865
	ccw->cda = (__u32)(addr_t)rcd_buffer;
866 867
	ccw->count = DASD_ECKD_RCD_DATA_SIZE;
	cqr->magic = DASD_ECKD_MAGIC;
868

869 870 871
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
872 873
	cqr->expires = 10*HZ;
	cqr->lpm = lpm;
874
	cqr->retries = 256;
875
	cqr->buildclk = get_tod_clock();
876
	cqr->status = DASD_CQR_FILLED;
877 878 879
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
/*
 * Wakeup helper for read_conf
 * if the cqr is not done and needs some error recovery
 * the buffer has to be re-initialized with the EBCDIC "V1.0"
 * to show support for virtual device SNEQ
 */
static void read_conf_cb(struct dasd_ccw_req *cqr, void *data)
{
	struct ccw1 *ccw;
	__u8 *rcd_buffer;

	if (cqr->status !=  DASD_CQR_DONE) {
		ccw = cqr->cpaddr;
		rcd_buffer = (__u8 *)((addr_t) ccw->cda);
		memset(rcd_buffer, 0, sizeof(*rcd_buffer));

		rcd_buffer[0] = 0xE5;
		rcd_buffer[1] = 0xF1;
		rcd_buffer[2] = 0x4B;
		rcd_buffer[3] = 0xF0;
	}
	dasd_wakeup_cb(cqr, data);
}

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
static int dasd_eckd_read_conf_immediately(struct dasd_device *device,
					   struct dasd_ccw_req *cqr,
					   __u8 *rcd_buffer,
					   __u8 lpm)
{
	struct ciw *ciw;
	int rc;
	/*
	 * sanity check: scan for RCD command in extended SenseID data
	 * some devices do not support RCD
	 */
	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD)
		return -EOPNOTSUPP;

	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buffer, lpm);
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
921
	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
922
	cqr->retries = 5;
923
	cqr->callback = read_conf_cb;
924 925
	rc = dasd_sleep_on_immediatly(cqr);
	return rc;
926 927 928 929 930 931 932 933 934 935 936 937
}

static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
				   void **rcd_buffer,
				   int *rcd_buffer_size, __u8 lpm)
{
	struct ciw *ciw;
	char *rcd_buf = NULL;
	int ret;
	struct dasd_ccw_req *cqr;

	/*
938 939
	 * sanity check: scan for RCD command in extended SenseID data
	 * some devices do not support RCD
940 941
	 */
	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
942
	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
943 944 945
		ret = -EOPNOTSUPP;
		goto out_error;
	}
946
	rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
947 948 949 950
	if (!rcd_buf) {
		ret = -ENOMEM;
		goto out_error;
	}
951 952 953
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
				   0, /* use rcd_buf as data ara */
				   device);
954
	if (IS_ERR(cqr)) {
955 956 957
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "Could not allocate RCD request");
		ret = -ENOMEM;
958 959
		goto out_error;
	}
960
	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
961
	cqr->callback = read_conf_cb;
962 963 964 965
	ret = dasd_sleep_on(cqr);
	/*
	 * on success we update the user input parms
	 */
966
	dasd_sfree_request(cqr, cqr->memdev);
967 968 969
	if (ret)
		goto out_error;

970
	*rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
971 972 973 974 975 976 977 978 979
	*rcd_buffer = rcd_buf;
	return 0;
out_error:
	kfree(rcd_buf);
	*rcd_buffer = NULL;
	*rcd_buffer_size = 0;
	return ret;
}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
{

	struct dasd_sneq *sneq;
	int i, count;

	private->ned = NULL;
	private->sneq = NULL;
	private->vdsneq = NULL;
	private->gneq = NULL;
	count = private->conf_len / sizeof(struct dasd_sneq);
	sneq = (struct dasd_sneq *)private->conf_data;
	for (i = 0; i < count; ++i) {
		if (sneq->flags.identifier == 1 && sneq->format == 1)
			private->sneq = sneq;
		else if (sneq->flags.identifier == 1 && sneq->format == 4)
			private->vdsneq = (struct vd_sneq *)sneq;
		else if (sneq->flags.identifier == 2)
			private->gneq = (struct dasd_gneq *)sneq;
		else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
			private->ned = (struct dasd_ned *)sneq;
		sneq++;
	}
	if (!private->ned || !private->gneq) {
		private->ned = NULL;
		private->sneq = NULL;
		private->vdsneq = NULL;
		private->gneq = NULL;
		return -EINVAL;
	}
	return 0;

};

static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
{
	struct dasd_gneq *gneq;
	int i, count, found;

	count = conf_len / sizeof(*gneq);
	gneq = (struct dasd_gneq *)conf_data;
	found = 0;
	for (i = 0; i < count; ++i) {
		if (gneq->flags.identifier == 2) {
			found = 1;
			break;
		}
		gneq++;
	}
	if (found)
		return ((char *)gneq)[18] & 0x07;
	else
		return 0;
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
static void dasd_eckd_clear_conf_data(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
	int i;

	private = (struct dasd_eckd_private *) device->private;
	private->conf_data = NULL;
	private->conf_len = 0;
	for (i = 0; i < 8; i++) {
		kfree(private->path_conf_data[i]);
		private->path_conf_data[i] = NULL;
	}
}


1050
static int dasd_eckd_read_conf(struct dasd_device *device)
L
Linus Torvalds 已提交
1051 1052 1053
{
	void *conf_data;
	int conf_len, conf_data_saved;
1054
	int rc, path_err, pos;
1055
	__u8 lpm, opm;
1056
	struct dasd_eckd_private *private, path_private;
1057
	struct dasd_path *path_data;
1058 1059
	struct dasd_uid *uid;
	char print_path_uid[60], print_device_uid[60];
L
Linus Torvalds 已提交
1060 1061

	private = (struct dasd_eckd_private *) device->private;
1062 1063
	path_data = &device->path_data;
	opm = ccw_device_get_path_mask(device->cdev);
L
Linus Torvalds 已提交
1064
	conf_data_saved = 0;
1065
	path_err = 0;
L
Linus Torvalds 已提交
1066 1067
	/* get configuration data per operational path */
	for (lpm = 0x80; lpm; lpm>>= 1) {
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		if (!(lpm & opm))
			continue;
		rc = dasd_eckd_read_conf_lpm(device, &conf_data,
					     &conf_len, lpm);
		if (rc && rc != -EOPNOTSUPP) {	/* -EOPNOTSUPP is ok */
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
					"Read configuration data returned "
					"error %d", rc);
			return rc;
		}
		if (conf_data == NULL) {
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
					"No configuration data "
					"retrieved");
			/* no further analysis possible */
			path_data->opm |= lpm;
			continue;	/* no error */
		}
		/* save first valid configuration data */
		if (!conf_data_saved) {
1088 1089
			/* initially clear previously stored conf_data */
			dasd_eckd_clear_conf_data(device);
1090 1091 1092 1093 1094 1095 1096
			private->conf_data = conf_data;
			private->conf_len = conf_len;
			if (dasd_eckd_identify_conf_parts(private)) {
				private->conf_data = NULL;
				private->conf_len = 0;
				kfree(conf_data);
				continue;
L
Linus Torvalds 已提交
1097
			}
1098 1099 1100 1101
			pos = pathmask_to_pos(lpm);
			/* store per path conf_data */
			private->path_conf_data[pos] =
				(struct dasd_conf_data *) conf_data;
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
			/*
			 * build device UID that other path data
			 * can be compared to it
			 */
			dasd_eckd_generate_uid(device);
			conf_data_saved++;
		} else {
			path_private.conf_data = conf_data;
			path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
			if (dasd_eckd_identify_conf_parts(
				    &path_private)) {
				path_private.conf_data = NULL;
				path_private.conf_len = 0;
				kfree(conf_data);
				continue;
L
Linus Torvalds 已提交
1117
			}
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
			if (dasd_eckd_compare_path_uid(
				    device, &path_private)) {
				uid = &path_private.uid;
				if (strlen(uid->vduit) > 0)
					snprintf(print_path_uid,
						 sizeof(print_path_uid),
						 "%s.%s.%04x.%02x.%s",
						 uid->vendor, uid->serial,
						 uid->ssid, uid->real_unit_addr,
						 uid->vduit);
				else
					snprintf(print_path_uid,
						 sizeof(print_path_uid),
						 "%s.%s.%04x.%02x",
						 uid->vendor, uid->serial,
						 uid->ssid,
						 uid->real_unit_addr);
				uid = &private->uid;
				if (strlen(uid->vduit) > 0)
					snprintf(print_device_uid,
						 sizeof(print_device_uid),
						 "%s.%s.%04x.%02x.%s",
						 uid->vendor, uid->serial,
						 uid->ssid, uid->real_unit_addr,
						 uid->vduit);
				else
					snprintf(print_device_uid,
						 sizeof(print_device_uid),
						 "%s.%s.%04x.%02x",
						 uid->vendor, uid->serial,
						 uid->ssid,
						 uid->real_unit_addr);
				dev_err(&device->cdev->dev,
					"Not all channel paths lead to "
					"the same device, path %02X leads to "
					"device %s instead of %s\n", lpm,
					print_path_uid, print_device_uid);
1155
				path_err = -EINVAL;
1156
				path_data->cablepm |= lpm;
1157
				continue;
L
Linus Torvalds 已提交
1158
			}
1159 1160 1161 1162
			pos = pathmask_to_pos(lpm);
			/* store per path conf_data */
			private->path_conf_data[pos] =
				(struct dasd_conf_data *) conf_data;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
			path_private.conf_data = NULL;
			path_private.conf_len = 0;
		}
		switch (dasd_eckd_path_access(conf_data, conf_len)) {
		case 0x02:
			path_data->npm |= lpm;
			break;
		case 0x03:
			path_data->ppm |= lpm;
			break;
L
Linus Torvalds 已提交
1173
		}
1174
		path_data->opm |= lpm;
1175 1176 1177 1178 1179 1180 1181
		/*
		 * if the path is used
		 * it should not be in one of the negative lists
		 */
		path_data->cablepm &= ~lpm;
		path_data->hpfpm &= ~lpm;
		path_data->cuirpm &= ~lpm;
L
Linus Torvalds 已提交
1182
	}
1183

1184
	return path_err;
L
Linus Torvalds 已提交
1185 1186
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
{
	struct dasd_eckd_private *private;
	int mdc;
	u32 fcx_max_data;

	private = (struct dasd_eckd_private *) device->private;
	if (private->fcx_max_data) {
		mdc = ccw_device_get_mdc(device->cdev, lpm);
		if ((mdc < 0)) {
			dev_warn(&device->cdev->dev,
				 "Detecting the maximum data size for zHPF "
				 "requests failed (rc=%d) for a new path %x\n",
				 mdc, lpm);
			return mdc;
		}
		fcx_max_data = mdc * FCX_MAX_DATA_FACTOR;
		if (fcx_max_data < private->fcx_max_data) {
			dev_warn(&device->cdev->dev,
				 "The maximum data size for zHPF requests %u "
				 "on a new path %x is below the active maximum "
				 "%u\n", fcx_max_data, lpm,
				 private->fcx_max_data);
			return -EACCES;
		}
	}
	return 0;
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
static int rebuild_device_uid(struct dasd_device *device,
			      struct path_verification_work_data *data)
{
	struct dasd_eckd_private *private;
	struct dasd_path *path_data;
	__u8 lpm, opm;
	int rc;

	rc = -ENODEV;
	private = (struct dasd_eckd_private *) device->private;
	path_data = &device->path_data;
	opm = device->path_data.opm;

	for (lpm = 0x80; lpm; lpm >>= 1) {
		if (!(lpm & opm))
			continue;
		memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
		memset(&data->cqr, 0, sizeof(data->cqr));
		data->cqr.cpaddr = &data->ccw;
		rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
						     data->rcd_buffer,
						     lpm);

		if (rc) {
			if (rc == -EOPNOTSUPP) /* -EOPNOTSUPP is ok */
				continue;
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
					"Read configuration data "
					"returned error %d", rc);
			break;
		}
		memcpy(private->conf_data, data->rcd_buffer,
		       DASD_ECKD_RCD_DATA_SIZE);
		if (dasd_eckd_identify_conf_parts(private)) {
			rc = -ENODEV;
		} else /* first valid path is enough */
			break;
	}

	if (!rc)
		rc = dasd_eckd_generate_uid(device);

	return rc;
}

1261 1262 1263 1264
static void do_path_verification_work(struct work_struct *work)
{
	struct path_verification_work_data *data;
	struct dasd_device *device;
1265 1266 1267
	struct dasd_eckd_private path_private;
	struct dasd_uid *uid;
	__u8 path_rcd_buf[DASD_ECKD_RCD_DATA_SIZE];
1268
	__u8 lpm, opm, npm, ppm, epm, hpfpm, cablepm;
1269
	unsigned long flags;
1270
	char print_uid[60];
1271 1272 1273 1274 1275
	int rc;

	data = container_of(work, struct path_verification_work_data, worker);
	device = data->device;

1276 1277 1278 1279 1280
	/* delay path verification until device was resumed */
	if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
		schedule_work(work);
		return;
	}
1281 1282 1283 1284 1285
	/* check if path verification already running and delay if so */
	if (test_and_set_bit(DASD_FLAG_PATH_VERIFY, &device->flags)) {
		schedule_work(work);
		return;
	}
1286 1287 1288 1289
	opm = 0;
	npm = 0;
	ppm = 0;
	epm = 0;
1290 1291 1292
	hpfpm = 0;
	cablepm = 0;

1293
	for (lpm = 0x80; lpm; lpm >>= 1) {
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
		if (!(lpm & data->tbvpm))
			continue;
		memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
		memset(&data->cqr, 0, sizeof(data->cqr));
		data->cqr.cpaddr = &data->ccw;
		rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
						     data->rcd_buffer,
						     lpm);
		if (!rc) {
			switch (dasd_eckd_path_access(data->rcd_buffer,
						      DASD_ECKD_RCD_DATA_SIZE)
				) {
			case 0x02:
				npm |= lpm;
				break;
			case 0x03:
				ppm |= lpm;
				break;
			}
			opm |= lpm;
		} else if (rc == -EOPNOTSUPP) {
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
					"path verification: No configuration "
					"data retrieved");
			opm |= lpm;
		} else if (rc == -EAGAIN) {
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1321 1322
					"path verification: device is stopped,"
					" try again later");
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
			epm |= lpm;
		} else {
			dev_warn(&device->cdev->dev,
				 "Reading device feature codes failed "
				 "(rc=%d) for new path %x\n", rc, lpm);
			continue;
		}
		if (verify_fcx_max_data(device, lpm)) {
			opm &= ~lpm;
			npm &= ~lpm;
			ppm &= ~lpm;
1334
			hpfpm |= lpm;
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
			continue;
		}

		/*
		 * save conf_data for comparison after
		 * rebuild_device_uid may have changed
		 * the original data
		 */
		memcpy(&path_rcd_buf, data->rcd_buffer,
		       DASD_ECKD_RCD_DATA_SIZE);
		path_private.conf_data = (void *) &path_rcd_buf;
		path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
		if (dasd_eckd_identify_conf_parts(&path_private)) {
			path_private.conf_data = NULL;
			path_private.conf_len = 0;
			continue;
		}

		/*
		 * compare path UID with device UID only if at least
		 * one valid path is left
		 * in other case the device UID may have changed and
		 * the first working path UID will be used as device UID
		 */
		if (device->path_data.opm &&
		    dasd_eckd_compare_path_uid(device, &path_private)) {
			/*
			 * the comparison was not successful
			 * rebuild the device UID with at least one
			 * known path in case a z/VM hyperswap command
			 * has changed the device
			 *
			 * after this compare again
			 *
			 * if either the rebuild or the recompare fails
			 * the path can not be used
			 */
			if (rebuild_device_uid(device, data) ||
			    dasd_eckd_compare_path_uid(
				    device, &path_private)) {
				uid = &path_private.uid;
				if (strlen(uid->vduit) > 0)
					snprintf(print_uid, sizeof(print_uid),
						 "%s.%s.%04x.%02x.%s",
						 uid->vendor, uid->serial,
						 uid->ssid, uid->real_unit_addr,
						 uid->vduit);
				else
					snprintf(print_uid, sizeof(print_uid),
						 "%s.%s.%04x.%02x",
						 uid->vendor, uid->serial,
						 uid->ssid,
						 uid->real_unit_addr);
				dev_err(&device->cdev->dev,
					"The newly added channel path %02X "
					"will not be used because it leads "
					"to a different device %s\n",
					lpm, print_uid);
1393 1394 1395
				opm &= ~lpm;
				npm &= ~lpm;
				ppm &= ~lpm;
1396
				cablepm |= lpm;
1397
				continue;
1398 1399
			}
		}
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

		/*
		 * There is a small chance that a path is lost again between
		 * above path verification and the following modification of
		 * the device opm mask. We could avoid that race here by using
		 * yet another path mask, but we rather deal with this unlikely
		 * situation in dasd_start_IO.
		 */
		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
		if (!device->path_data.opm && opm) {
			device->path_data.opm = opm;
1411 1412 1413
			device->path_data.cablepm &= ~opm;
			device->path_data.cuirpm &= ~opm;
			device->path_data.hpfpm &= ~opm;
1414
			dasd_generic_path_operational(device);
1415
		} else {
1416
			device->path_data.opm |= opm;
1417 1418 1419 1420
			device->path_data.cablepm &= ~opm;
			device->path_data.cuirpm &= ~opm;
			device->path_data.hpfpm &= ~opm;
		}
1421 1422 1423
		device->path_data.npm |= npm;
		device->path_data.ppm |= ppm;
		device->path_data.tbvpm |= epm;
1424 1425
		device->path_data.cablepm |= cablepm;
		device->path_data.hpfpm |= hpfpm;
1426
		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1427
	}
1428
	clear_bit(DASD_FLAG_PATH_VERIFY, &device->flags);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	dasd_put_device(device);
	if (data->isglobal)
		mutex_unlock(&dasd_path_verification_mutex);
	else
		kfree(data);
}

static int dasd_eckd_verify_path(struct dasd_device *device, __u8 lpm)
{
	struct path_verification_work_data *data;

	data = kmalloc(sizeof(*data), GFP_ATOMIC | GFP_DMA);
	if (!data) {
		if (mutex_trylock(&dasd_path_verification_mutex)) {
			data = path_verification_worker;
			data->isglobal = 1;
		} else
			return -ENOMEM;
	} else {
		memset(data, 0, sizeof(*data));
		data->isglobal = 0;
	}
	INIT_WORK(&data->worker, do_path_verification_work);
	dasd_get_device(device);
	data->device = device;
	data->tbvpm = lpm;
	schedule_work(&data->worker);
	return 0;
}

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
static int dasd_eckd_read_features(struct dasd_device *device)
{
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_features *features;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;
	struct dasd_eckd_private *private;

	private = (struct dasd_eckd_private *) device->private;
1469
	memset(&private->features, 0, sizeof(struct dasd_rssd_features));
1470
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
1471 1472 1473 1474
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_features)),
				   device);
	if (IS_ERR(cqr)) {
1475 1476
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
				"allocate initialization request");
1477 1478 1479 1480 1481
		return PTR_ERR(cqr);
	}
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
1482
	cqr->retries = 256;
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	cqr->expires = 10 * HZ;

	/* Prepare for Read Subsystem Data */
	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
	prssdp->order = PSF_ORDER_PRSSD;
	prssdp->suborder = 0x41;	/* Read Feature Codes */
	/* all other bytes of prssdp must be zero */

	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->count = sizeof(struct dasd_psf_prssd_data);
	ccw->flags |= CCW_FLAG_CC;
	ccw->cda = (__u32)(addr_t) prssdp;

	/* Read Subsystem Data - feature codes */
	features = (struct dasd_rssd_features *) (prssdp + 1);
	memset(features, 0, sizeof(struct dasd_rssd_features));

	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
	ccw->count = sizeof(struct dasd_rssd_features);
	ccw->cda = (__u32)(addr_t) features;

1507
	cqr->buildclk = get_tod_clock();
1508 1509 1510 1511 1512 1513 1514
	cqr->status = DASD_CQR_FILLED;
	rc = dasd_sleep_on(cqr);
	if (rc == 0) {
		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
		features = (struct dasd_rssd_features *) (prssdp + 1);
		memcpy(&private->features, features,
		       sizeof(struct dasd_rssd_features));
1515 1516 1517
	} else
		dev_warn(&device->cdev->dev, "Reading device feature codes"
			 " failed with rc=%d\n", rc);
1518 1519 1520 1521 1522
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}


1523 1524 1525
/*
 * Build CP for Perform Subsystem Function - SSC.
 */
1526 1527
static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
						    int enable_pav)
1528
{
1529 1530 1531
	struct dasd_ccw_req *cqr;
	struct dasd_psf_ssc_data *psf_ssc_data;
	struct ccw1 *ccw;
1532

1533
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1534 1535 1536
				  sizeof(struct dasd_psf_ssc_data),
				  device);

1537
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
1538
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1539
			   "Could not allocate PSF-SSC request");
1540 1541 1542 1543
		return cqr;
	}
	psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
	psf_ssc_data->order = PSF_ORDER_SSC;
1544
	psf_ssc_data->suborder = 0xc0;
1545
	if (enable_pav) {
1546
		psf_ssc_data->suborder |= 0x08;
1547 1548
		psf_ssc_data->reserved[0] = 0x88;
	}
1549 1550 1551 1552 1553 1554 1555 1556
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->cda = (__u32)(addr_t)psf_ssc_data;
	ccw->count = 66;

	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
1557
	cqr->retries = 256;
1558
	cqr->expires = 10*HZ;
1559
	cqr->buildclk = get_tod_clock();
1560 1561
	cqr->status = DASD_CQR_FILLED;
	return cqr;
1562 1563 1564 1565 1566 1567 1568 1569
}

/*
 * Perform Subsystem Function.
 * It is necessary to trigger CIO for channel revalidation since this
 * call might change behaviour of DASD devices.
 */
static int
S
Stefan Haberland 已提交
1570 1571
dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
		  unsigned long flags)
1572
{
1573 1574 1575
	struct dasd_ccw_req *cqr;
	int rc;

1576
	cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1577 1578 1579
	if (IS_ERR(cqr))
		return PTR_ERR(cqr);

S
Stefan Haberland 已提交
1580 1581 1582 1583 1584 1585
	/*
	 * set flags e.g. turn on failfast, to prevent blocking
	 * the calling function should handle failed requests
	 */
	cqr->flags |= flags;

1586 1587 1588 1589
	rc = dasd_sleep_on(cqr);
	if (!rc)
		/* trigger CIO to reprobe devices */
		css_schedule_reprobe();
S
Stefan Haberland 已提交
1590 1591 1592
	else if (cqr->intrc == -EAGAIN)
		rc = -EAGAIN;

1593 1594
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
1595 1596 1597 1598 1599
}

/*
 * Valide storage server of current device.
 */
S
Stefan Haberland 已提交
1600 1601
static int dasd_eckd_validate_server(struct dasd_device *device,
				     unsigned long flags)
1602 1603
{
	int rc;
1604
	struct dasd_eckd_private *private;
1605
	int enable_pav;
1606

1607 1608 1609
	private = (struct dasd_eckd_private *) device->private;
	if (private->uid.type == UA_BASE_PAV_ALIAS ||
	    private->uid.type == UA_HYPER_PAV_ALIAS)
S
Stefan Haberland 已提交
1610
		return 0;
1611
	if (dasd_nopav || MACHINE_IS_VM)
1612 1613 1614
		enable_pav = 0;
	else
		enable_pav = 1;
S
Stefan Haberland 已提交
1615
	rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
1616

H
Horst Hummel 已提交
1617 1618
	/* may be requested feature is not available on server,
	 * therefore just report error and go ahead */
1619 1620
	DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
			"returned rc=%d", private->uid.ssid, rc);
S
Stefan Haberland 已提交
1621
	return rc;
1622 1623
}

1624 1625 1626 1627 1628 1629 1630
/*
 * worker to do a validate server in case of a lost pathgroup
 */
static void dasd_eckd_do_validate_server(struct work_struct *work)
{
	struct dasd_device *device = container_of(work, struct dasd_device,
						  kick_validate);
1631 1632 1633 1634
	unsigned long flags = 0;

	set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
	if (dasd_eckd_validate_server(device, flags)
S
Stefan Haberland 已提交
1635 1636 1637 1638 1639 1640
	    == -EAGAIN) {
		/* schedule worker again if failed */
		schedule_work(&device->kick_validate);
		return;
	}

1641 1642 1643 1644 1645 1646
	dasd_put_device(device);
}

static void dasd_eckd_kick_validate_server(struct dasd_device *device)
{
	dasd_get_device(device);
1647 1648 1649 1650 1651 1652
	/* exit if device not online or in offline processing */
	if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
	   device->state < DASD_STATE_ONLINE) {
		dasd_put_device(device);
		return;
	}
1653
	/* queue call to do_validate_server to the kernel event daemon. */
1654 1655
	if (!schedule_work(&device->kick_validate))
		dasd_put_device(device);
1656 1657
}

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
static u32 get_fcx_max_data(struct dasd_device *device)
{
	int tpm, mdc;
	int fcx_in_css, fcx_in_gneq, fcx_in_features;
	struct dasd_eckd_private *private;

	if (dasd_nofcx)
		return 0;
	/* is transport mode supported? */
	private = (struct dasd_eckd_private *) device->private;
	fcx_in_css = css_general_characteristics.fcx;
	fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
	fcx_in_features = private->features.feature[40] & 0x80;
	tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;

	if (!tpm)
		return 0;

	mdc = ccw_device_get_mdc(device->cdev, 0);
	if (mdc < 0) {
		dev_warn(&device->cdev->dev, "Detecting the maximum supported"
			 " data size for zHPF requests failed\n");
		return 0;
	} else
		return mdc * FCX_MAX_DATA_FACTOR;
}

1685 1686 1687 1688
/*
 * Check device characteristics.
 * If the device is accessible using ECKD discipline, the device is enabled.
 */
L
Linus Torvalds 已提交
1689 1690 1691 1692
static int
dasd_eckd_check_characteristics(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
1693
	struct dasd_block *block;
1694
	struct dasd_uid temp_uid;
1695
	int rc, i;
1696
	int readonly;
1697
	unsigned long value;
L
Linus Torvalds 已提交
1698

1699 1700 1701
	/* setup work queue for validate server*/
	INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);

1702 1703 1704 1705 1706 1707 1708 1709 1710
	if (!ccw_device_is_pathgroup(device->cdev)) {
		dev_warn(&device->cdev->dev,
			 "A channel path group could not be established\n");
		return -EIO;
	}
	if (!ccw_device_is_multipath(device->cdev)) {
		dev_info(&device->cdev->dev,
			 "The DASD is not operating in multipath mode\n");
	}
L
Linus Torvalds 已提交
1711
	private = (struct dasd_eckd_private *) device->private;
1712 1713 1714
	if (!private) {
		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
		if (!private) {
S
Stefan Haberland 已提交
1715 1716 1717
			dev_warn(&device->cdev->dev,
				 "Allocating memory for private DASD data "
				 "failed\n");
L
Linus Torvalds 已提交
1718 1719 1720
			return -ENOMEM;
		}
		device->private = (void *) private;
1721 1722
	} else {
		memset(private, 0, sizeof(*private));
L
Linus Torvalds 已提交
1723 1724 1725 1726 1727 1728 1729
	}
	/* Invalidate status of initial analysis. */
	private->init_cqr_status = -1;
	/* Set default cache operations. */
	private->attrib.operation = DASD_NORMAL_CACHE;
	private->attrib.nr_cyl = 0;

1730 1731 1732
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
1733
		goto out_err1;
1734

1735 1736
	/* set default timeout */
	device->default_expires = DASD_EXPIRES;
1737 1738 1739
	/* set default retry count */
	device->default_retries = DASD_RETRIES;

1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
	if (private->gneq) {
		value = 1;
		for (i = 0; i < private->gneq->timeout.value; i++)
			value = 10 * value;
		value = value * private->gneq->timeout.number;
		/* do not accept useless values */
		if (value != 0 && value <= DASD_EXPIRES_MAX)
			device->default_expires = value;
	}

1750 1751
	dasd_eckd_get_uid(device, &temp_uid);
	if (temp_uid.type == UA_BASE_DEVICE) {
1752 1753
		block = dasd_alloc_block();
		if (IS_ERR(block)) {
1754 1755 1756
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
					"could not allocate dasd "
					"block structure");
1757 1758 1759 1760 1761 1762 1763
			rc = PTR_ERR(block);
			goto out_err1;
		}
		device->block = block;
		block->base = device;
	}

1764 1765 1766
	/* register lcu with alias handling, enable PAV */
	rc = dasd_alias_make_device_known_to_lcu(device);
	if (rc)
1767
		goto out_err2;
1768

S
Stefan Haberland 已提交
1769
	dasd_eckd_validate_server(device, 0);
1770 1771 1772 1773 1774

	/* device may report different configuration data after LCU setup */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err3;
1775 1776

	/* Read Feature Codes */
1777
	dasd_eckd_read_features(device);
1778

L
Linus Torvalds 已提交
1779
	/* Read Device Characteristics */
1780 1781
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
					 &private->rdc_data, 64);
1782
	if (rc) {
1783 1784
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
1785 1786
		goto out_err3;
	}
1787 1788 1789 1790 1791 1792 1793 1794 1795

	if ((device->features & DASD_FEATURE_USERAW) &&
	    !(private->rdc_data.facilities.RT_in_LR)) {
		dev_err(&device->cdev->dev, "The storage server does not "
			"support raw-track access\n");
		rc = -EINVAL;
		goto out_err3;
	}

1796
	/* find the valid cylinder size */
1797 1798 1799 1800 1801 1802
	if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
	    private->rdc_data.long_no_cyl)
		private->real_cyl = private->rdc_data.long_no_cyl;
	else
		private->real_cyl = private->rdc_data.no_cyl;

1803 1804
	private->fcx_max_data = get_fcx_max_data(device);

1805 1806 1807 1808
	readonly = dasd_device_is_ro(device);
	if (readonly)
		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);

S
Stefan Haberland 已提交
1809
	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1810
		 "with %d cylinders, %d heads, %d sectors%s\n",
S
Stefan Haberland 已提交
1811 1812 1813 1814
		 private->rdc_data.dev_type,
		 private->rdc_data.dev_model,
		 private->rdc_data.cu_type,
		 private->rdc_data.cu_model.model,
1815
		 private->real_cyl,
S
Stefan Haberland 已提交
1816
		 private->rdc_data.trk_per_cyl,
1817 1818
		 private->rdc_data.sec_per_trk,
		 readonly ? ", read-only device" : "");
1819 1820 1821 1822 1823 1824 1825 1826
	return 0;

out_err3:
	dasd_alias_disconnect_device_from_lcu(device);
out_err2:
	dasd_free_block(device->block);
	device->block = NULL;
out_err1:
1827
	kfree(private->conf_data);
1828 1829
	kfree(device->private);
	device->private = NULL;
1830
	return rc;
L
Linus Torvalds 已提交
1831 1832
}

1833 1834
static void dasd_eckd_uncheck_device(struct dasd_device *device)
{
1835
	struct dasd_eckd_private *private;
1836
	int i;
1837 1838

	private = (struct dasd_eckd_private *) device->private;
1839
	dasd_alias_disconnect_device_from_lcu(device);
1840 1841 1842 1843 1844
	private->ned = NULL;
	private->sneq = NULL;
	private->vdsneq = NULL;
	private->gneq = NULL;
	private->conf_len = 0;
1845 1846 1847 1848 1849 1850 1851 1852 1853
	for (i = 0; i < 8; i++) {
		kfree(private->path_conf_data[i]);
		if ((__u8 *)private->path_conf_data[i] ==
		    private->conf_data) {
			private->conf_data = NULL;
			private->conf_len = 0;
		}
		private->path_conf_data[i] = NULL;
	}
1854 1855
	kfree(private->conf_data);
	private->conf_data = NULL;
1856 1857
}

L
Linus Torvalds 已提交
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
static struct dasd_ccw_req *
dasd_eckd_analysis_ccw(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
	struct eckd_count *count_data;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int cplength, datasize;
	int i;

	private = (struct dasd_eckd_private *) device->private;

	cplength = 8;
	datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
1873
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
L
Linus Torvalds 已提交
1874 1875 1876 1877 1878 1879
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
	/* Define extent for the first 3 tracks. */
	define_extent(ccw++, cqr->data, 0, 2,
		      DASD_ECKD_CCW_READ_COUNT, device);
1880
	LO_data = cqr->data + sizeof(struct DE_eckd_data);
L
Linus Torvalds 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
	/* Locate record for the first 4 records on track 0. */
	ccw[-1].flags |= CCW_FLAG_CC;
	locate_record(ccw++, LO_data++, 0, 0, 4,
		      DASD_ECKD_CCW_READ_COUNT, device, 0);

	count_data = private->count_area;
	for (i = 0; i < 4; i++) {
		ccw[-1].flags |= CCW_FLAG_CC;
		ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
		ccw->flags = 0;
		ccw->count = 8;
		ccw->cda = (__u32)(addr_t) count_data;
		ccw++;
		count_data++;
	}

	/* Locate record for the first record on track 2. */
	ccw[-1].flags |= CCW_FLAG_CC;
	locate_record(ccw++, LO_data++, 2, 0, 1,
		      DASD_ECKD_CCW_READ_COUNT, device, 0);
	/* Read count ccw. */
	ccw[-1].flags |= CCW_FLAG_CC;
	ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
	ccw->flags = 0;
	ccw->count = 8;
	ccw->cda = (__u32)(addr_t) count_data;

1908 1909 1910
	cqr->block = NULL;
	cqr->startdev = device;
	cqr->memdev = device;
1911
	cqr->retries = 255;
1912
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
1913 1914 1915 1916
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
/* differentiate between 'no record found' and any other error */
static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
{
	char *sense;
	if (init_cqr->status == DASD_CQR_DONE)
		return INIT_CQR_OK;
	else if (init_cqr->status == DASD_CQR_NEED_ERP ||
		 init_cqr->status == DASD_CQR_FAILED) {
		sense = dasd_get_sense(&init_cqr->irb);
		if (sense && (sense[1] & SNS1_NO_REC_FOUND))
			return INIT_CQR_UNFORMATTED;
		else
			return INIT_CQR_ERROR;
	} else
		return INIT_CQR_ERROR;
}

L
Linus Torvalds 已提交
1934 1935 1936 1937 1938 1939 1940
/*
 * This is the callback function for the init_analysis cqr. It saves
 * the status of the initial analysis ccw before it frees it and kicks
 * the device to continue the startup sequence. This will call
 * dasd_eckd_do_analysis again (if the devices has not been marked
 * for deletion in the meantime).
 */
1941 1942
static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
					void *data)
L
Linus Torvalds 已提交
1943 1944 1945 1946
{
	struct dasd_eckd_private *private;
	struct dasd_device *device;

1947
	device = init_cqr->startdev;
L
Linus Torvalds 已提交
1948
	private = (struct dasd_eckd_private *) device->private;
1949
	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
L
Linus Torvalds 已提交
1950 1951 1952 1953
	dasd_sfree_request(init_cqr, device);
	dasd_kick_device(device);
}

1954
static int dasd_eckd_start_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1955 1956 1957
{
	struct dasd_ccw_req *init_cqr;

1958
	init_cqr = dasd_eckd_analysis_ccw(block->base);
L
Linus Torvalds 已提交
1959 1960 1961 1962 1963
	if (IS_ERR(init_cqr))
		return PTR_ERR(init_cqr);
	init_cqr->callback = dasd_eckd_analysis_callback;
	init_cqr->callback_data = NULL;
	init_cqr->expires = 5*HZ;
1964 1965 1966 1967 1968
	/* first try without ERP, so we can later handle unformatted
	 * devices as special case
	 */
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
	init_cqr->retries = 0;
L
Linus Torvalds 已提交
1969 1970 1971 1972
	dasd_add_request_head(init_cqr);
	return -EAGAIN;
}

1973
static int dasd_eckd_end_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1974
{
1975
	struct dasd_device *device;
L
Linus Torvalds 已提交
1976 1977 1978 1979
	struct dasd_eckd_private *private;
	struct eckd_count *count_area;
	unsigned int sb, blk_per_trk;
	int status, i;
1980
	struct dasd_ccw_req *init_cqr;
L
Linus Torvalds 已提交
1981

1982
	device = block->base;
L
Linus Torvalds 已提交
1983 1984 1985
	private = (struct dasd_eckd_private *) device->private;
	status = private->init_cqr_status;
	private->init_cqr_status = -1;
1986 1987 1988 1989 1990 1991 1992 1993
	if (status == INIT_CQR_ERROR) {
		/* try again, this time with full ERP */
		init_cqr = dasd_eckd_analysis_ccw(device);
		dasd_sleep_on(init_cqr);
		status = dasd_eckd_analysis_evaluation(init_cqr);
		dasd_sfree_request(init_cqr, device);
	}

1994 1995 1996 1997 1998 1999 2000
	if (device->features & DASD_FEATURE_USERAW) {
		block->bp_block = DASD_RAW_BLOCKSIZE;
		blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
		block->s2b_shift = 3;
		goto raw;
	}

2001 2002
	if (status == INIT_CQR_UNFORMATTED) {
		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
L
Linus Torvalds 已提交
2003
		return -EMEDIUMTYPE;
2004 2005 2006 2007 2008
	} else if (status == INIT_CQR_ERROR) {
		dev_err(&device->cdev->dev,
			"Detecting the DASD disk layout failed because "
			"of an I/O error\n");
		return -EIO;
L
Linus Torvalds 已提交
2009 2010 2011 2012 2013 2014 2015
	}

	private->uses_cdl = 1;
	/* Check Track 0 for Compatible Disk Layout */
	count_area = NULL;
	for (i = 0; i < 3; i++) {
		if (private->count_area[i].kl != 4 ||
2016 2017 2018 2019
		    private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4 ||
		    private->count_area[i].cyl != 0 ||
		    private->count_area[i].head != count_area_head[i] ||
		    private->count_area[i].record != count_area_rec[i]) {
L
Linus Torvalds 已提交
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
			private->uses_cdl = 0;
			break;
		}
	}
	if (i == 3)
		count_area = &private->count_area[4];

	if (private->uses_cdl == 0) {
		for (i = 0; i < 5; i++) {
			if ((private->count_area[i].kl != 0) ||
			    (private->count_area[i].dl !=
2031 2032 2033 2034
			     private->count_area[0].dl) ||
			    private->count_area[i].cyl !=  0 ||
			    private->count_area[i].head != count_area_head[i] ||
			    private->count_area[i].record != count_area_rec[i])
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039 2040
				break;
		}
		if (i == 5)
			count_area = &private->count_area[0];
	} else {
		if (private->count_area[3].record == 1)
S
Stefan Haberland 已提交
2041 2042
			dev_warn(&device->cdev->dev,
				 "Track 0 has no records following the VTOC\n");
L
Linus Torvalds 已提交
2043
	}
2044

L
Linus Torvalds 已提交
2045 2046 2047
	if (count_area != NULL && count_area->kl == 0) {
		/* we found notthing violating our disk layout */
		if (dasd_check_blocksize(count_area->dl) == 0)
2048
			block->bp_block = count_area->dl;
L
Linus Torvalds 已提交
2049
	}
2050
	if (block->bp_block == 0) {
S
Stefan Haberland 已提交
2051 2052
		dev_warn(&device->cdev->dev,
			 "The disk layout of the DASD is not supported\n");
L
Linus Torvalds 已提交
2053 2054
		return -EMEDIUMTYPE;
	}
2055 2056 2057
	block->s2b_shift = 0;	/* bits to shift 512 to get a block */
	for (sb = 512; sb < block->bp_block; sb = sb << 1)
		block->s2b_shift++;
L
Linus Torvalds 已提交
2058

2059
	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
2060 2061

raw:
2062
	block->blocks = (private->real_cyl *
L
Linus Torvalds 已提交
2063 2064 2065
			  private->rdc_data.trk_per_cyl *
			  blk_per_trk);

S
Stefan Haberland 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074
	dev_info(&device->cdev->dev,
		 "DASD with %d KB/block, %d KB total size, %d KB/track, "
		 "%s\n", (block->bp_block >> 10),
		 ((private->real_cyl *
		   private->rdc_data.trk_per_cyl *
		   blk_per_trk * (block->bp_block >> 9)) >> 1),
		 ((blk_per_trk * block->bp_block) >> 10),
		 private->uses_cdl ?
		 "compatible disk layout" : "linux disk layout");
L
Linus Torvalds 已提交
2075 2076 2077 2078

	return 0;
}

2079
static int dasd_eckd_do_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
2080 2081 2082
{
	struct dasd_eckd_private *private;

2083
	private = (struct dasd_eckd_private *) block->base->private;
L
Linus Torvalds 已提交
2084
	if (private->init_cqr_status < 0)
2085
		return dasd_eckd_start_analysis(block);
L
Linus Torvalds 已提交
2086
	else
2087
		return dasd_eckd_end_analysis(block);
L
Linus Torvalds 已提交
2088 2089
}

2090
static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2091 2092 2093 2094 2095 2096
{
	return dasd_alias_add_device(device);
};

static int dasd_eckd_online_to_ready(struct dasd_device *device)
{
2097
	cancel_work_sync(&device->reload_device);
2098
	cancel_work_sync(&device->kick_validate);
2099 2100 2101
	return 0;
};

2102
static int dasd_eckd_basic_to_known(struct dasd_device *device)
2103
{
2104 2105 2106
	return dasd_alias_remove_device(device);
};

L
Linus Torvalds 已提交
2107
static int
2108
dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
L
Linus Torvalds 已提交
2109 2110 2111
{
	struct dasd_eckd_private *private;

2112 2113
	private = (struct dasd_eckd_private *) block->base->private;
	if (dasd_check_blocksize(block->bp_block) == 0) {
L
Linus Torvalds 已提交
2114
		geo->sectors = recs_per_track(&private->rdc_data,
2115
					      0, block->bp_block);
L
Linus Torvalds 已提交
2116 2117 2118 2119 2120 2121 2122
	}
	geo->cylinders = private->rdc_data.no_cyl;
	geo->heads = private->rdc_data.trk_per_cyl;
	return 0;
}

static struct dasd_ccw_req *
2123
dasd_eckd_build_format(struct dasd_device *base,
2124
		       struct format_data_t *fdata,
S
Stefan Haberland 已提交
2125
		       int enable_pav)
L
Linus Torvalds 已提交
2126
{
2127 2128
	struct dasd_eckd_private *base_priv;
	struct dasd_eckd_private *start_priv;
2129
	struct dasd_device *startdev = NULL;
L
Linus Torvalds 已提交
2130 2131
	struct dasd_ccw_req *fcp;
	struct eckd_count *ect;
2132
	struct ch_t address;
L
Linus Torvalds 已提交
2133 2134
	struct ccw1 *ccw;
	void *data;
2135
	int rpt;
L
Linus Torvalds 已提交
2136
	int cplength, datasize;
2137
	int i, j;
2138 2139
	int intensity = 0;
	int r0_perm;
2140
	int nr_tracks;
2141
	int use_prefix;
L
Linus Torvalds 已提交
2142

S
Stefan Haberland 已提交
2143
	if (enable_pav)
2144 2145
		startdev = dasd_alias_get_start_dev(base);

2146 2147
	if (!startdev)
		startdev = base;
L
Linus Torvalds 已提交
2148

2149 2150 2151 2152 2153 2154
	start_priv = (struct dasd_eckd_private *) startdev->private;
	base_priv = (struct dasd_eckd_private *) base->private;

	rpt = recs_per_track(&base_priv->rdc_data, 0, fdata->blksize);

	nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
L
Linus Torvalds 已提交
2155 2156 2157 2158 2159 2160 2161

	/*
	 * fdata->intensity is a bit string that tells us what to do:
	 *   Bit 0: write record zero
	 *   Bit 1: write home address, currently not supported
	 *   Bit 2: invalidate tracks
	 *   Bit 3: use OS/390 compatible disk layout (cdl)
2162
	 *   Bit 4: do not allow storage subsystem to modify record zero
L
Linus Torvalds 已提交
2163 2164
	 * Only some bit combinations do make sense.
	 */
2165 2166 2167 2168 2169 2170 2171
	if (fdata->intensity & 0x10) {
		r0_perm = 0;
		intensity = fdata->intensity & ~0x10;
	} else {
		r0_perm = 1;
		intensity = fdata->intensity;
	}
2172

2173 2174
	use_prefix = base_priv->features.feature[8] & 0x01;

2175
	switch (intensity) {
L
Linus Torvalds 已提交
2176 2177
	case 0x00:	/* Normal format */
	case 0x08:	/* Normal format, use cdl. */
2178
		cplength = 2 + (rpt*nr_tracks);
2179 2180 2181 2182 2183 2184 2185 2186
		if (use_prefix)
			datasize = sizeof(struct PFX_eckd_data) +
				sizeof(struct LO_eckd_data) +
				rpt * nr_tracks * sizeof(struct eckd_count);
		else
			datasize = sizeof(struct DE_eckd_data) +
				sizeof(struct LO_eckd_data) +
				rpt * nr_tracks * sizeof(struct eckd_count);
L
Linus Torvalds 已提交
2187 2188 2189
		break;
	case 0x01:	/* Write record zero and format track. */
	case 0x09:	/* Write record zero and format track, use cdl. */
2190
		cplength = 2 + rpt * nr_tracks;
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
		if (use_prefix)
			datasize = sizeof(struct PFX_eckd_data) +
				sizeof(struct LO_eckd_data) +
				sizeof(struct eckd_count) +
				rpt * nr_tracks * sizeof(struct eckd_count);
		else
			datasize = sizeof(struct DE_eckd_data) +
				sizeof(struct LO_eckd_data) +
				sizeof(struct eckd_count) +
				rpt * nr_tracks * sizeof(struct eckd_count);
L
Linus Torvalds 已提交
2201 2202 2203 2204
		break;
	case 0x04:	/* Invalidate track. */
	case 0x0c:	/* Invalidate track, use cdl. */
		cplength = 3;
2205 2206 2207 2208 2209 2210 2211 2212
		if (use_prefix)
			datasize = sizeof(struct PFX_eckd_data) +
				sizeof(struct LO_eckd_data) +
				sizeof(struct eckd_count);
		else
			datasize = sizeof(struct DE_eckd_data) +
				sizeof(struct LO_eckd_data) +
				sizeof(struct eckd_count);
L
Linus Torvalds 已提交
2213 2214
		break;
	default:
2215 2216 2217
		dev_warn(&startdev->cdev->dev,
			 "An I/O control call used incorrect flags 0x%x\n",
			 fdata->intensity);
L
Linus Torvalds 已提交
2218 2219 2220
		return ERR_PTR(-EINVAL);
	}
	/* Allocate the format ccw request. */
2221 2222
	fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
				   datasize, startdev);
L
Linus Torvalds 已提交
2223 2224 2225
	if (IS_ERR(fcp))
		return fcp;

2226
	start_priv->count++;
L
Linus Torvalds 已提交
2227 2228 2229
	data = fcp->data;
	ccw = fcp->cpaddr;

2230
	switch (intensity & ~0x08) {
L
Linus Torvalds 已提交
2231
	case 0x00: /* Normal format. */
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
		if (use_prefix) {
			prefix(ccw++, (struct PFX_eckd_data *) data,
			       fdata->start_unit, fdata->stop_unit,
			       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
			/* grant subsystem permission to format R0 */
			if (r0_perm)
				((struct PFX_eckd_data *)data)
					->define_extent.ga_extended |= 0x04;
			data += sizeof(struct PFX_eckd_data);
		} else {
			define_extent(ccw++, (struct DE_eckd_data *) data,
				      fdata->start_unit, fdata->stop_unit,
				      DASD_ECKD_CCW_WRITE_CKD, startdev);
			/* grant subsystem permission to format R0 */
			if (r0_perm)
				((struct DE_eckd_data *) data)
					->ga_extended |= 0x04;
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2251 2252
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
2253 2254
			      fdata->start_unit, 0, rpt*nr_tracks,
			      DASD_ECKD_CCW_WRITE_CKD, base,
L
Linus Torvalds 已提交
2255 2256 2257 2258
			      fdata->blksize);
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x01: /* Write record zero + format track. */
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
		if (use_prefix) {
			prefix(ccw++, (struct PFX_eckd_data *) data,
			       fdata->start_unit, fdata->stop_unit,
			       DASD_ECKD_CCW_WRITE_RECORD_ZERO,
			       base, startdev);
			data += sizeof(struct PFX_eckd_data);
		} else {
			define_extent(ccw++, (struct DE_eckd_data *) data,
			       fdata->start_unit, fdata->stop_unit,
			       DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev);
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2271 2272
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
2273 2274 2275
			      fdata->start_unit, 0, rpt * nr_tracks + 1,
			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
			      base->block->bp_block);
L
Linus Torvalds 已提交
2276 2277 2278
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x04: /* Invalidate track. */
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
		if (use_prefix) {
			prefix(ccw++, (struct PFX_eckd_data *) data,
			       fdata->start_unit, fdata->stop_unit,
			       DASD_ECKD_CCW_WRITE_CKD, base, startdev);
			data += sizeof(struct PFX_eckd_data);
		} else {
			define_extent(ccw++, (struct DE_eckd_data *) data,
			       fdata->start_unit, fdata->stop_unit,
			       DASD_ECKD_CCW_WRITE_CKD, startdev);
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2290 2291 2292
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
			      fdata->start_unit, 0, 1,
2293
			      DASD_ECKD_CCW_WRITE_CKD, base, 8);
L
Linus Torvalds 已提交
2294 2295 2296
		data += sizeof(struct LO_eckd_data);
		break;
	}
2297 2298 2299 2300 2301 2302 2303 2304 2305

	for (j = 0; j < nr_tracks; j++) {
		/* calculate cylinder and head for the current track */
		set_ch_t(&address,
			 (fdata->start_unit + j) /
			 base_priv->rdc_data.trk_per_cyl,
			 (fdata->start_unit + j) %
			 base_priv->rdc_data.trk_per_cyl);
		if (intensity & 0x01) {	/* write record zero */
L
Linus Torvalds 已提交
2306 2307
			ect = (struct eckd_count *) data;
			data += sizeof(struct eckd_count);
2308 2309
			ect->cyl = address.cyl;
			ect->head = address.head;
2310
			ect->record = 0;
L
Linus Torvalds 已提交
2311
			ect->kl = 0;
2312
			ect->dl = 8;
L
Linus Torvalds 已提交
2313
			ccw[-1].flags |= CCW_FLAG_CC;
2314
			ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
L
Linus Torvalds 已提交
2315 2316 2317 2318 2319
			ccw->flags = CCW_FLAG_SLI;
			ccw->count = 8;
			ccw->cda = (__u32)(addr_t) ect;
			ccw++;
		}
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
		if ((intensity & ~0x08) & 0x04) {	/* erase track */
			ect = (struct eckd_count *) data;
			data += sizeof(struct eckd_count);
			ect->cyl = address.cyl;
			ect->head = address.head;
			ect->record = 1;
			ect->kl = 0;
			ect->dl = 0;
			ccw[-1].flags |= CCW_FLAG_CC;
			ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
			ccw->flags = CCW_FLAG_SLI;
			ccw->count = 8;
			ccw->cda = (__u32)(addr_t) ect;
		} else {		/* write remaining records */
			for (i = 0; i < rpt; i++) {
				ect = (struct eckd_count *) data;
				data += sizeof(struct eckd_count);
				ect->cyl = address.cyl;
				ect->head = address.head;
				ect->record = i + 1;
				ect->kl = 0;
				ect->dl = fdata->blksize;
				/*
				 * Check for special tracks 0-1
				 * when formatting CDL
				 */
				if ((intensity & 0x08) &&
				    fdata->start_unit == 0) {
					if (i < 3) {
						ect->kl = 4;
						ect->dl = sizes_trk0[i] - 4;
					}
				}
				if ((intensity & 0x08) &&
				    fdata->start_unit == 1) {
					ect->kl = 44;
					ect->dl = LABEL_SIZE - 44;
				}
				ccw[-1].flags |= CCW_FLAG_CC;
				if (i != 0 || j == 0)
					ccw->cmd_code =
						DASD_ECKD_CCW_WRITE_CKD;
				else
					ccw->cmd_code =
						DASD_ECKD_CCW_WRITE_CKD_MT;
				ccw->flags = CCW_FLAG_SLI;
				ccw->count = 8;
					ccw->cda = (__u32)(addr_t) ect;
					ccw++;
			}
		}
L
Linus Torvalds 已提交
2371
	}
2372 2373 2374

	fcp->startdev = startdev;
	fcp->memdev = startdev;
2375
	fcp->basedev = base;
2376
	fcp->retries = 256;
2377
	fcp->expires = startdev->default_expires * HZ;
2378
	fcp->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
2379
	fcp->status = DASD_CQR_FILLED;
2380

L
Linus Torvalds 已提交
2381 2382 2383
	return fcp;
}

2384 2385
static int
dasd_eckd_format_device(struct dasd_device *base,
2386
			struct format_data_t *fdata,
S
Stefan Haberland 已提交
2387
			int enable_pav)
2388 2389 2390 2391 2392 2393 2394
{
	struct dasd_ccw_req *cqr, *n;
	struct dasd_block *block;
	struct dasd_eckd_private *private;
	struct list_head format_queue;
	struct dasd_device *device;
	int old_stop, format_step;
2395
	int step, rc = 0, sleep_rc;
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429

	block = base->block;
	private = (struct dasd_eckd_private *) base->private;

	/* Sanity checks. */
	if (fdata->start_unit >=
	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
		dev_warn(&base->cdev->dev,
			 "Start track number %u used in formatting is too big\n",
			 fdata->start_unit);
		return -EINVAL;
	}
	if (fdata->stop_unit >=
	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
		dev_warn(&base->cdev->dev,
			 "Stop track number %u used in formatting is too big\n",
			 fdata->stop_unit);
		return -EINVAL;
	}
	if (fdata->start_unit > fdata->stop_unit) {
		dev_warn(&base->cdev->dev,
			 "Start track %u used in formatting exceeds end track\n",
			 fdata->start_unit);
		return -EINVAL;
	}
	if (dasd_check_blocksize(fdata->blksize) != 0) {
		dev_warn(&base->cdev->dev,
			 "The DASD cannot be formatted with block size %u\n",
			 fdata->blksize);
		return -EINVAL;
	}

	INIT_LIST_HEAD(&format_queue);

2430
	old_stop = fdata->stop_unit;
2431 2432
	while (fdata->start_unit <= 1) {
		fdata->stop_unit = fdata->start_unit;
S
Stefan Haberland 已提交
2433
		cqr = dasd_eckd_build_format(base, fdata, enable_pav);
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
		list_add(&cqr->blocklist, &format_queue);

		fdata->stop_unit = old_stop;
		fdata->start_unit++;

		if (fdata->start_unit > fdata->stop_unit)
			goto sleep;
	}

retry:
	format_step = 255 / recs_per_track(&private->rdc_data, 0,
					   fdata->blksize);
	while (fdata->start_unit <= old_stop) {
		step = fdata->stop_unit - fdata->start_unit + 1;
		if (step > format_step)
			fdata->stop_unit = fdata->start_unit + format_step - 1;

S
Stefan Haberland 已提交
2451
		cqr = dasd_eckd_build_format(base, fdata, enable_pav);
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
		if (IS_ERR(cqr)) {
			if (PTR_ERR(cqr) == -ENOMEM) {
				/*
				 * not enough memory available
				 * go to out and start requests
				 * retry after first requests were finished
				 */
				fdata->stop_unit = old_stop;
				goto sleep;
			} else
				return PTR_ERR(cqr);
		}
		list_add(&cqr->blocklist, &format_queue);

		fdata->start_unit = fdata->stop_unit + 1;
		fdata->stop_unit = old_stop;
	}

sleep:
2471
	sleep_rc = dasd_sleep_on_queue(&format_queue);
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482

	list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
		device = cqr->startdev;
		private = (struct dasd_eckd_private *) device->private;
		if (cqr->status == DASD_CQR_FAILED)
			rc = -EIO;
		list_del_init(&cqr->blocklist);
		dasd_sfree_request(cqr, device);
		private->count--;
	}

2483 2484 2485
	if (sleep_rc)
		return sleep_rc;

2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
	/*
	 * in case of ENOMEM we need to retry after
	 * first requests are finished
	 */
	if (fdata->start_unit <= fdata->stop_unit)
		goto retry;

	return rc;
}

2496
static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
L
Linus Torvalds 已提交
2497
{
2498 2499 2500 2501
	if (cqr->retries < 0) {
		cqr->status = DASD_CQR_FAILED;
		return;
	}
2502 2503 2504 2505
	cqr->status = DASD_CQR_FILLED;
	if (cqr->block && (cqr->startdev != cqr->block->base)) {
		dasd_eckd_reset_ccw_to_base_io(cqr);
		cqr->startdev = cqr->block->base;
2506
		cqr->lpm = cqr->block->base->path_data.opm;
L
Linus Torvalds 已提交
2507
	}
2508
};
L
Linus Torvalds 已提交
2509 2510 2511 2512

static dasd_erp_fn_t
dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
{
2513
	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
L
Linus Torvalds 已提交
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
	struct ccw_device *cdev = device->cdev;

	switch (cdev->id.cu_type) {
	case 0x3990:
	case 0x2105:
	case 0x2107:
	case 0x1750:
		return dasd_3990_erp_action;
	case 0x9343:
	case 0x3880:
	default:
		return dasd_default_erp_action;
	}
}

static dasd_erp_fn_t
dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
{
	return dasd_default_erp_postaction;
}

2535 2536 2537
static void dasd_eckd_check_for_device_change(struct dasd_device *device,
					      struct dasd_ccw_req *cqr,
					      struct irb *irb)
2538 2539
{
	char mask;
2540
	char *sense = NULL;
2541
	struct dasd_eckd_private *private;
2542

2543
	private = (struct dasd_eckd_private *) device->private;
2544 2545
	/* first of all check for state change pending interrupt */
	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
2546
	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
2547 2548 2549 2550
		/*
		 * for alias only, not in offline processing
		 * and only if not suspended
		 */
2551
		if (!device->block && private->lcu &&
2552
		    device->state == DASD_STATE_ONLINE &&
2553 2554
		    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
		    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
			/*
			 * the state change could be caused by an alias
			 * reassignment remove device from alias handling
			 * to prevent new requests from being scheduled on
			 * the wrong alias device
			 */
			dasd_alias_remove_device(device);

			/* schedule worker to reload device */
			dasd_reload_device(device);
		}
2566 2567 2568 2569
		dasd_generic_handle_state_change(device);
		return;
	}

2570
	sense = dasd_get_sense(irb);
2571 2572 2573 2574
	if (!sense)
		return;

	/* summary unit check */
2575
	if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
2576
	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
2577 2578 2579 2580
		dasd_alias_handle_summary_unit_check(device, irb);
		return;
	}

2581
	/* service information message SIM */
2582
	if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
2583 2584
	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
		dasd_3990_erp_handle_sim(device, sense);
2585 2586 2587
		return;
	}

2588 2589 2590
	/* loss of device reservation is handled via base devices only
	 * as alias devices may be used with several bases
	 */
2591 2592
	if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
	    (sense[7] == 0x3F) &&
2593 2594 2595 2596 2597 2598 2599
	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
	    test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
		if (device->features & DASD_FEATURE_FAILONSLCK)
			set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
		dev_err(&device->cdev->dev,
			"The device reservation was lost\n");
2600
	}
2601
}
2602 2603 2604

static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
					       struct dasd_device *startdev,
2605
					       struct dasd_block *block,
2606 2607 2608 2609 2610 2611 2612 2613 2614
					       struct request *req,
					       sector_t first_rec,
					       sector_t last_rec,
					       sector_t first_trk,
					       sector_t last_trk,
					       unsigned int first_offs,
					       unsigned int last_offs,
					       unsigned int blk_per_trk,
					       unsigned int blksize)
L
Linus Torvalds 已提交
2615 2616 2617 2618 2619 2620
{
	struct dasd_eckd_private *private;
	unsigned long *idaws;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
2621
	struct req_iterator iter;
2622
	struct bio_vec bv;
L
Linus Torvalds 已提交
2623
	char *dst;
2624
	unsigned int off;
L
Linus Torvalds 已提交
2625
	int count, cidaw, cplength, datasize;
2626
	sector_t recid;
L
Linus Torvalds 已提交
2627
	unsigned char cmd, rcmd;
2628 2629
	int use_prefix;
	struct dasd_device *basedev;
L
Linus Torvalds 已提交
2630

2631 2632
	basedev = block->base;
	private = (struct dasd_eckd_private *) basedev->private;
L
Linus Torvalds 已提交
2633 2634 2635 2636 2637 2638
	if (rq_data_dir(req) == READ)
		cmd = DASD_ECKD_CCW_READ_MT;
	else if (rq_data_dir(req) == WRITE)
		cmd = DASD_ECKD_CCW_WRITE_MT;
	else
		return ERR_PTR(-EINVAL);
2639

L
Linus Torvalds 已提交
2640 2641 2642
	/* Check struct bio and count the number of blocks for the request. */
	count = 0;
	cidaw = 0;
2643
	rq_for_each_segment(bv, req, iter) {
2644
		if (bv.bv_len & (blksize - 1))
2645 2646
			/* Eckd can only do full blocks. */
			return ERR_PTR(-EINVAL);
2647 2648 2649
		count += bv.bv_len >> (block->s2b_shift + 9);
		if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
			cidaw += bv.bv_len >> (block->s2b_shift + 9);
L
Linus Torvalds 已提交
2650 2651 2652 2653
	}
	/* Paranoia. */
	if (count != last_rec - first_rec + 1)
		return ERR_PTR(-EINVAL);
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671

	/* use the prefix command if available */
	use_prefix = private->features.feature[8] & 0x01;
	if (use_prefix) {
		/* 1x prefix + number of blocks */
		cplength = 2 + count;
		/* 1x prefix + cidaws*sizeof(long) */
		datasize = sizeof(struct PFX_eckd_data) +
			sizeof(struct LO_eckd_data) +
			cidaw * sizeof(unsigned long);
	} else {
		/* 1x define extent + 1x locate record + number of blocks */
		cplength = 2 + count;
		/* 1x define extent + 1x locate record + cidaws*sizeof(long) */
		datasize = sizeof(struct DE_eckd_data) +
			sizeof(struct LO_eckd_data) +
			cidaw * sizeof(unsigned long);
	}
L
Linus Torvalds 已提交
2672 2673 2674 2675 2676 2677 2678 2679
	/* Find out the number of additional locate record ccws for cdl. */
	if (private->uses_cdl && first_rec < 2*blk_per_trk) {
		if (last_rec >= 2*blk_per_trk)
			count = 2*blk_per_trk - first_rec;
		cplength += count;
		datasize += count*sizeof(struct LO_eckd_data);
	}
	/* Allocate the ccw request. */
2680 2681
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				   startdev);
L
Linus Torvalds 已提交
2682 2683 2684
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
	/* First ccw is define extent or prefix. */
	if (use_prefix) {
		if (prefix(ccw++, cqr->data, first_trk,
			   last_trk, cmd, basedev, startdev) == -EAGAIN) {
			/* Clock not in sync and XRC is enabled.
			 * Try again later.
			 */
			dasd_sfree_request(cqr, startdev);
			return ERR_PTR(-EAGAIN);
		}
		idaws = (unsigned long *) (cqr->data +
					   sizeof(struct PFX_eckd_data));
	} else {
		if (define_extent(ccw++, cqr->data, first_trk,
2699
				  last_trk, cmd, basedev) == -EAGAIN) {
2700 2701 2702 2703 2704 2705 2706 2707
			/* Clock not in sync and XRC is enabled.
			 * Try again later.
			 */
			dasd_sfree_request(cqr, startdev);
			return ERR_PTR(-EAGAIN);
		}
		idaws = (unsigned long *) (cqr->data +
					   sizeof(struct DE_eckd_data));
M
Martin Schwidefsky 已提交
2708
	}
L
Linus Torvalds 已提交
2709 2710 2711 2712 2713 2714 2715
	/* Build locate_record+read/write/ccws. */
	LO_data = (struct LO_eckd_data *) (idaws + cidaw);
	recid = first_rec;
	if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
		/* Only standard blocks so there is just one locate record. */
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
2716
			      last_rec - recid + 1, cmd, basedev, blksize);
L
Linus Torvalds 已提交
2717
	}
2718
	rq_for_each_segment(bv, req, iter) {
2719
		dst = page_address(bv.bv_page) + bv.bv_offset;
L
Linus Torvalds 已提交
2720 2721
		if (dasd_page_cache) {
			char *copy = kmem_cache_alloc(dasd_page_cache,
C
Christoph Lameter 已提交
2722
						      GFP_DMA | __GFP_NOWARN);
L
Linus Torvalds 已提交
2723
			if (copy && rq_data_dir(req) == WRITE)
2724
				memcpy(copy + bv.bv_offset, dst, bv.bv_len);
L
Linus Torvalds 已提交
2725
			if (copy)
2726
				dst = copy + bv.bv_offset;
L
Linus Torvalds 已提交
2727
		}
2728
		for (off = 0; off < bv.bv_len; off += blksize) {
L
Linus Torvalds 已提交
2729 2730 2731 2732 2733 2734 2735 2736 2737
			sector_t trkid = recid;
			unsigned int recoffs = sector_div(trkid, blk_per_trk);
			rcmd = cmd;
			count = blksize;
			/* Locate record for cdl special block ? */
			if (private->uses_cdl && recid < 2*blk_per_trk) {
				if (dasd_eckd_cdl_special(blk_per_trk, recid)){
					rcmd |= 0x8;
					count = dasd_eckd_cdl_reclen(recid);
2738 2739
					if (count < blksize &&
					    rq_data_dir(req) == READ)
L
Linus Torvalds 已提交
2740 2741 2742 2743 2744 2745
						memset(dst + count, 0xe5,
						       blksize - count);
				}
				ccw[-1].flags |= CCW_FLAG_CC;
				locate_record(ccw++, LO_data++,
					      trkid, recoffs + 1,
2746
					      1, rcmd, basedev, count);
L
Linus Torvalds 已提交
2747 2748 2749 2750 2751 2752 2753
			}
			/* Locate record for standard blocks ? */
			if (private->uses_cdl && recid == 2*blk_per_trk) {
				ccw[-1].flags |= CCW_FLAG_CC;
				locate_record(ccw++, LO_data++,
					      trkid, recoffs + 1,
					      last_rec - recid + 1,
2754
					      cmd, basedev, count);
L
Linus Torvalds 已提交
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
			}
			/* Read/write ccw. */
			ccw[-1].flags |= CCW_FLAG_CC;
			ccw->cmd_code = rcmd;
			ccw->count = count;
			if (idal_is_needed(dst, blksize)) {
				ccw->cda = (__u32)(addr_t) idaws;
				ccw->flags = CCW_FLAG_IDA;
				idaws = idal_create_words(idaws, dst, blksize);
			} else {
				ccw->cda = (__u32)(addr_t) dst;
				ccw->flags = 0;
			}
			ccw++;
			dst += blksize;
			recid++;
		}
	}
2773 2774
	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
2775
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2776 2777 2778
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
2779
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2780
	cqr->lpm = startdev->path_data.ppm;
2781
	cqr->retries = startdev->default_retries;
2782
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
2783 2784 2785 2786
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
					       struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req,
					       sector_t first_rec,
					       sector_t last_rec,
					       sector_t first_trk,
					       sector_t last_trk,
					       unsigned int first_offs,
					       unsigned int last_offs,
					       unsigned int blk_per_trk,
					       unsigned int blksize)
{
	unsigned long *idaws;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	struct req_iterator iter;
2804
	struct bio_vec bv;
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
	char *dst, *idaw_dst;
	unsigned int cidaw, cplength, datasize;
	unsigned int tlf;
	sector_t recid;
	unsigned char cmd;
	struct dasd_device *basedev;
	unsigned int trkcount, count, count_to_trk_end;
	unsigned int idaw_len, seg_len, part_len, len_to_track_end;
	unsigned char new_track, end_idaw;
	sector_t trkid;
	unsigned int recoffs;

	basedev = block->base;
	if (rq_data_dir(req) == READ)
		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
	else if (rq_data_dir(req) == WRITE)
		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
	else
		return ERR_PTR(-EINVAL);

	/* Track based I/O needs IDAWs for each page, and not just for
	 * 64 bit addresses. We need additional idals for pages
	 * that get filled from two tracks, so we use the number
	 * of records as upper limit.
	 */
	cidaw = last_rec - first_rec + 1;
	trkcount = last_trk - first_trk + 1;

	/* 1x prefix + one read/write ccw per track */
	cplength = 1 + trkcount;

	/* on 31-bit we need space for two 32 bit addresses per page
	 * on 64-bit one 64 bit address
	 */
	datasize = sizeof(struct PFX_eckd_data) +
		cidaw * sizeof(unsigned long long);

	/* Allocate the ccw request. */
2843 2844
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				   startdev);
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
	/* transfer length factor: how many bytes to read from the last track */
	if (first_trk == last_trk)
		tlf = last_offs - first_offs + 1;
	else
		tlf = last_offs + 1;
	tlf *= blksize;

	if (prefix_LRE(ccw++, cqr->data, first_trk,
		       last_trk, cmd, basedev, startdev,
		       1 /* format */, first_offs + 1,
		       trkcount, blksize,
		       tlf) == -EAGAIN) {
		/* Clock not in sync and XRC is enabled.
		 * Try again later.
		 */
		dasd_sfree_request(cqr, startdev);
		return ERR_PTR(-EAGAIN);
	}

	/*
	 * The translation of request into ccw programs must meet the
	 * following conditions:
	 * - all idaws but the first and the last must address full pages
	 *   (or 2K blocks on 31-bit)
	 * - the scope of a ccw and it's idal ends with the track boundaries
	 */
	idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
	recid = first_rec;
	new_track = 1;
	end_idaw = 0;
	len_to_track_end = 0;
2879
	idaw_dst = NULL;
2880 2881
	idaw_len = 0;
	rq_for_each_segment(bv, req, iter) {
2882 2883
		dst = page_address(bv.bv_page) + bv.bv_offset;
		seg_len = bv.bv_len;
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
		while (seg_len) {
			if (new_track) {
				trkid = recid;
				recoffs = sector_div(trkid, blk_per_trk);
				count_to_trk_end = blk_per_trk - recoffs;
				count = min((last_rec - recid + 1),
					    (sector_t)count_to_trk_end);
				len_to_track_end = count * blksize;
				ccw[-1].flags |= CCW_FLAG_CC;
				ccw->cmd_code = cmd;
				ccw->count = len_to_track_end;
				ccw->cda = (__u32)(addr_t)idaws;
				ccw->flags = CCW_FLAG_IDA;
				ccw++;
				recid += count;
				new_track = 0;
2900 2901 2902
				/* first idaw for a ccw may start anywhere */
				if (!idaw_dst)
					idaw_dst = dst;
2903
			}
2904 2905
			/* If we start a new idaw, we must make sure that it
			 * starts on an IDA_BLOCK_SIZE boundary.
2906 2907 2908 2909
			 * If we continue an idaw, we must make sure that the
			 * current segment begins where the so far accumulated
			 * idaw ends
			 */
2910 2911 2912 2913 2914 2915 2916
			if (!idaw_dst) {
				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
					dasd_sfree_request(cqr, startdev);
					return ERR_PTR(-ERANGE);
				} else
					idaw_dst = dst;
			}
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
			if ((idaw_dst + idaw_len) != dst) {
				dasd_sfree_request(cqr, startdev);
				return ERR_PTR(-ERANGE);
			}
			part_len = min(seg_len, len_to_track_end);
			seg_len -= part_len;
			dst += part_len;
			idaw_len += part_len;
			len_to_track_end -= part_len;
			/* collected memory area ends on an IDA_BLOCK border,
			 * -> create an idaw
			 * idal_create_words will handle cases where idaw_len
			 * is larger then IDA_BLOCK_SIZE
			 */
			if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
				end_idaw = 1;
			/* We also need to end the idaw at track end */
			if (!len_to_track_end) {
				new_track = 1;
				end_idaw = 1;
			}
			if (end_idaw) {
				idaws = idal_create_words(idaws, idaw_dst,
							  idaw_len);
2941
				idaw_dst = NULL;
2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953
				idaw_len = 0;
				end_idaw = 0;
			}
		}
	}

	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
2954
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2955
	cqr->lpm = startdev->path_data.ppm;
2956
	cqr->retries = startdev->default_retries;
2957
	cqr->buildclk = get_tod_clock();
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

static int prepare_itcw(struct itcw *itcw,
			unsigned int trk, unsigned int totrk, int cmd,
			struct dasd_device *basedev,
			struct dasd_device *startdev,
			unsigned int rec_on_trk, int count,
			unsigned int blksize,
			unsigned int total_data_size,
			unsigned int tlf,
			unsigned int blk_per_trk)
{
	struct PFX_eckd_data pfxdata;
	struct dasd_eckd_private *basepriv, *startpriv;
	struct DE_eckd_data *dedata;
	struct LRE_eckd_data *lredata;
	struct dcw *dcw;

	u32 begcyl, endcyl;
	u16 heads, beghead, endhead;
	u8 pfx_cmd;

	int rc = 0;
	int sector = 0;
	int dn, d;


	/* setup prefix data */
	basepriv = (struct dasd_eckd_private *) basedev->private;
	startpriv = (struct dasd_eckd_private *) startdev->private;
	dedata = &pfxdata.define_extent;
	lredata = &pfxdata.locate_record;

	memset(&pfxdata, 0, sizeof(pfxdata));
	pfxdata.format = 1; /* PFX with LRE */
	pfxdata.base_address = basepriv->ned->unit_addr;
	pfxdata.base_lss = basepriv->ned->ID;
	pfxdata.validity.define_extent = 1;

	/* private uid is kept up to date, conf_data may be outdated */
	if (startpriv->uid.type != UA_BASE_DEVICE) {
		pfxdata.validity.verify_base = 1;
		if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
			pfxdata.validity.hyper_pav = 1;
	}

	switch (cmd) {
	case DASD_ECKD_CCW_READ_TRACK_DATA:
		dedata->mask.perm = 0x1;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = blksize;
		dedata->ga_extended |= 0x42;
		lredata->operation.orientation = 0x0;
		lredata->operation.operation = 0x0C;
		lredata->auxiliary.check_bytes = 0x01;
		pfx_cmd = DASD_ECKD_CCW_PFX_READ;
		break;
	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
		dedata->mask.perm = 0x02;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = blksize;
		rc = check_XRC_on_prefix(&pfxdata, basedev);
		dedata->ga_extended |= 0x42;
		lredata->operation.orientation = 0x0;
		lredata->operation.operation = 0x3F;
		lredata->extended_operation = 0x23;
		lredata->auxiliary.check_bytes = 0x2;
		pfx_cmd = DASD_ECKD_CCW_PFX;
		break;
	default:
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "prepare itcw, unknown opcode 0x%x", cmd);
		BUG();
		break;
	}
	if (rc)
		return rc;

	dedata->attributes.mode = 0x3;	/* ECKD */

	heads = basepriv->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;

	/* check for sequential prestage - enhance cylinder range */
	if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
	    dedata->attributes.operation == DASD_SEQ_ACCESS) {

		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
			endcyl += basepriv->attrib.nr_cyl;
		else
			endcyl = (basepriv->real_cyl - 1);
	}

	set_ch_t(&dedata->beg_ext, begcyl, beghead);
	set_ch_t(&dedata->end_ext, endcyl, endhead);

	dedata->ep_format = 0x20; /* records per track is valid */
	dedata->ep_rec_per_track = blk_per_trk;

	if (rec_on_trk) {
		switch (basepriv->rdc_data.dev_type) {
		case 0x3390:
			dn = ceil_quot(blksize + 6, 232);
			d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
			break;
		case 0x3380:
			d = 7 + ceil_quot(blksize + 12, 32);
			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
			break;
		}
	}

	lredata->auxiliary.length_valid = 1;
	lredata->auxiliary.length_scope = 1;
	lredata->auxiliary.imbedded_ccw_valid = 1;
	lredata->length = tlf;
	lredata->imbedded_ccw = cmd;
	lredata->count = count;
	lredata->sector = sector;
	set_ch_t(&lredata->seek_addr, begcyl, beghead);
	lredata->search_arg.cyl = lredata->seek_addr.cyl;
	lredata->search_arg.head = lredata->seek_addr.head;
	lredata->search_arg.record = rec_on_trk;

	dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
		     &pfxdata, sizeof(pfxdata), total_data_size);
3090
	return PTR_RET(dcw);
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107
}

static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
					       struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req,
					       sector_t first_rec,
					       sector_t last_rec,
					       sector_t first_trk,
					       sector_t last_trk,
					       unsigned int first_offs,
					       unsigned int last_offs,
					       unsigned int blk_per_trk,
					       unsigned int blksize)
{
	struct dasd_ccw_req *cqr;
	struct req_iterator iter;
3108
	struct bio_vec bv;
3109 3110 3111 3112 3113 3114 3115 3116 3117
	char *dst;
	unsigned int trkcount, ctidaw;
	unsigned char cmd;
	struct dasd_device *basedev;
	unsigned int tlf;
	struct itcw *itcw;
	struct tidaw *last_tidaw = NULL;
	int itcw_op;
	size_t itcw_size;
3118 3119 3120 3121 3122 3123
	u8 tidaw_flags;
	unsigned int seg_len, part_len, len_to_track_end;
	unsigned char new_track;
	sector_t recid, trkid;
	unsigned int offs;
	unsigned int count, count_to_trk_end;
3124
	int ret;
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138

	basedev = block->base;
	if (rq_data_dir(req) == READ) {
		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
		itcw_op = ITCW_OP_READ;
	} else if (rq_data_dir(req) == WRITE) {
		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
		itcw_op = ITCW_OP_WRITE;
	} else
		return ERR_PTR(-EINVAL);

	/* trackbased I/O needs address all memory via TIDAWs,
	 * not just for 64 bit addresses. This allows us to map
	 * each segment directly to one tidaw.
3139 3140
	 * In the case of write requests, additional tidaws may
	 * be needed when a segment crosses a track boundary.
3141 3142 3143 3144 3145 3146
	 */
	trkcount = last_trk - first_trk + 1;
	ctidaw = 0;
	rq_for_each_segment(bv, req, iter) {
		++ctidaw;
	}
3147 3148
	if (rq_data_dir(req) == WRITE)
		ctidaw += (last_trk - first_trk);
3149 3150 3151

	/* Allocate the ccw request. */
	itcw_size = itcw_calc_size(0, ctidaw, 0);
3152
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
	if (IS_ERR(cqr))
		return cqr;

	/* transfer length factor: how many bytes to read from the last track */
	if (first_trk == last_trk)
		tlf = last_offs - first_offs + 1;
	else
		tlf = last_offs + 1;
	tlf *= blksize;

	itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
3164
	if (IS_ERR(itcw)) {
3165 3166
		ret = -EINVAL;
		goto out_error;
3167
	}
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
	cqr->cpaddr = itcw_get_tcw(itcw);
	if (prepare_itcw(itcw, first_trk, last_trk,
			 cmd, basedev, startdev,
			 first_offs + 1,
			 trkcount, blksize,
			 (last_rec - first_rec + 1) * blksize,
			 tlf, blk_per_trk) == -EAGAIN) {
		/* Clock not in sync and XRC is enabled.
		 * Try again later.
		 */
3178 3179
		ret = -EAGAIN;
		goto out_error;
3180
	}
3181
	len_to_track_end = 0;
3182 3183 3184 3185 3186
	/*
	 * A tidaw can address 4k of memory, but must not cross page boundaries
	 * We can let the block layer handle this by setting
	 * blk_queue_segment_boundary to page boundaries and
	 * blk_max_segment_size to page size when setting up the request queue.
3187 3188
	 * For write requests, a TIDAW must not cross track boundaries, because
	 * we have to set the CBC flag on the last tidaw for each track.
3189
	 */
3190 3191 3192 3193
	if (rq_data_dir(req) == WRITE) {
		new_track = 1;
		recid = first_rec;
		rq_for_each_segment(bv, req, iter) {
3194 3195
			dst = page_address(bv.bv_page) + bv.bv_offset;
			seg_len = bv.bv_len;
3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217
			while (seg_len) {
				if (new_track) {
					trkid = recid;
					offs = sector_div(trkid, blk_per_trk);
					count_to_trk_end = blk_per_trk - offs;
					count = min((last_rec - recid + 1),
						    (sector_t)count_to_trk_end);
					len_to_track_end = count * blksize;
					recid += count;
					new_track = 0;
				}
				part_len = min(seg_len, len_to_track_end);
				seg_len -= part_len;
				len_to_track_end -= part_len;
				/* We need to end the tidaw at track end */
				if (!len_to_track_end) {
					new_track = 1;
					tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
				} else
					tidaw_flags = 0;
				last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
							    dst, part_len);
3218 3219 3220 3221
				if (IS_ERR(last_tidaw)) {
					ret = -EINVAL;
					goto out_error;
				}
3222 3223 3224 3225 3226
				dst += part_len;
			}
		}
	} else {
		rq_for_each_segment(bv, req, iter) {
3227
			dst = page_address(bv.bv_page) + bv.bv_offset;
3228
			last_tidaw = itcw_add_tidaw(itcw, 0x00,
3229
						    dst, bv.bv_len);
3230 3231 3232 3233
			if (IS_ERR(last_tidaw)) {
				ret = -EINVAL;
				goto out_error;
			}
3234
		}
3235
	}
3236 3237
	last_tidaw->flags |= TIDAW_FLAGS_LAST;
	last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
3238 3239 3240 3241 3242
	itcw_finalize(itcw);

	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3243
	cqr->cpmode = 1;
3244 3245 3246
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
3247
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
3248
	cqr->lpm = startdev->path_data.ppm;
3249
	cqr->retries = startdev->default_retries;
3250
	cqr->buildclk = get_tod_clock();
3251 3252
	cqr->status = DASD_CQR_FILLED;
	return cqr;
3253 3254 3255
out_error:
	dasd_sfree_request(cqr, startdev);
	return ERR_PTR(ret);
3256 3257 3258 3259 3260 3261
}

static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req)
{
3262
	int cmdrtd, cmdwtd;
3263
	int use_prefix;
3264
	int fcx_multitrack;
3265
	struct dasd_eckd_private *private;
3266 3267 3268 3269 3270 3271
	struct dasd_device *basedev;
	sector_t first_rec, last_rec;
	sector_t first_trk, last_trk;
	unsigned int first_offs, last_offs;
	unsigned int blk_per_trk, blksize;
	int cdlspecial;
3272
	unsigned int data_size;
3273 3274 3275 3276 3277 3278 3279 3280
	struct dasd_ccw_req *cqr;

	basedev = block->base;
	private = (struct dasd_eckd_private *) basedev->private;

	/* Calculate number of blocks/records per track. */
	blksize = block->bp_block;
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3281 3282
	if (blk_per_trk == 0)
		return ERR_PTR(-EINVAL);
3283
	/* Calculate record id of first and last block. */
3284
	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
3285 3286
	first_offs = sector_div(first_trk, blk_per_trk);
	last_rec = last_trk =
3287
		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3288 3289 3290
	last_offs = sector_div(last_trk, blk_per_trk);
	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);

3291 3292
	fcx_multitrack = private->features.feature[40] & 0x20;
	data_size = blk_rq_bytes(req);
3293 3294
	if (data_size % blksize)
		return ERR_PTR(-EINVAL);
3295 3296 3297
	/* tpm write request add CBC data on each track boundary */
	if (rq_data_dir(req) == WRITE)
		data_size += (last_trk - first_trk) * 4;
3298 3299 3300 3301 3302 3303 3304 3305 3306

	/* is read track data and write track data in command mode supported? */
	cmdrtd = private->features.feature[9] & 0x20;
	cmdwtd = private->features.feature[12] & 0x40;
	use_prefix = private->features.feature[8] & 0x01;

	cqr = NULL;
	if (cdlspecial || dasd_page_cache) {
		/* do nothing, just fall through to the cmd mode single case */
3307 3308
	} else if ((data_size <= private->fcx_max_data)
		   && (fcx_multitrack || (first_trk == last_trk))) {
3309 3310 3311 3312 3313
		cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
						    first_rec, last_rec,
						    first_trk, last_trk,
						    first_offs, last_offs,
						    blk_per_trk, blksize);
3314 3315
		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
		    (PTR_ERR(cqr) != -ENOMEM))
3316 3317 3318 3319 3320 3321 3322 3323 3324
			cqr = NULL;
	} else if (use_prefix &&
		   (((rq_data_dir(req) == READ) && cmdrtd) ||
		    ((rq_data_dir(req) == WRITE) && cmdwtd))) {
		cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
						   first_rec, last_rec,
						   first_trk, last_trk,
						   first_offs, last_offs,
						   blk_per_trk, blksize);
3325 3326
		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
		    (PTR_ERR(cqr) != -ENOMEM))
3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337
			cqr = NULL;
	}
	if (!cqr)
		cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
						    first_rec, last_rec,
						    first_trk, last_trk,
						    first_offs, last_offs,
						    blk_per_trk, blksize);
	return cqr;
}

3338 3339 3340 3341 3342 3343 3344 3345 3346
static struct dasd_ccw_req *dasd_raw_build_cp(struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req)
{
	unsigned long *idaws;
	struct dasd_device *basedev;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	struct req_iterator iter;
3347
	struct bio_vec bv;
3348 3349 3350 3351 3352 3353
	char *dst;
	unsigned char cmd;
	unsigned int trkcount;
	unsigned int seg_len, len_to_track_end;
	unsigned int first_offs;
	unsigned int cidaw, cplength, datasize;
3354 3355
	sector_t first_trk, last_trk, sectors;
	sector_t start_padding_sectors, end_sector_offset, end_padding_sectors;
3356 3357 3358 3359
	unsigned int pfx_datasize;

	/*
	 * raw track access needs to be mutiple of 64k and on 64k boundary
3360 3361
	 * For read requests we can fix an incorrect alignment by padding
	 * the request with dummy pages.
3362
	 */
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
	start_padding_sectors = blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK;
	end_sector_offset = (blk_rq_pos(req) + blk_rq_sectors(req)) %
		DASD_RAW_SECTORS_PER_TRACK;
	end_padding_sectors = (DASD_RAW_SECTORS_PER_TRACK - end_sector_offset) %
		DASD_RAW_SECTORS_PER_TRACK;
	basedev = block->base;
	if ((start_padding_sectors || end_padding_sectors) &&
	    (rq_data_dir(req) == WRITE)) {
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "raw write not track aligned (%lu,%lu) req %p",
			      start_padding_sectors, end_padding_sectors, req);
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404
		cqr = ERR_PTR(-EINVAL);
		goto out;
	}

	first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
	last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
		DASD_RAW_SECTORS_PER_TRACK;
	trkcount = last_trk - first_trk + 1;
	first_offs = 0;

	if (rq_data_dir(req) == READ)
		cmd = DASD_ECKD_CCW_READ_TRACK;
	else if (rq_data_dir(req) == WRITE)
		cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
	else {
		cqr = ERR_PTR(-EINVAL);
		goto out;
	}

	/*
	 * Raw track based I/O needs IDAWs for each page,
	 * and not just for 64 bit addresses.
	 */
	cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;

	/* 1x prefix + one read/write ccw per track */
	cplength = 1 + trkcount;

	/*
	 * struct PFX_eckd_data has up to 2 byte as extended parameter
	 * this is needed for write full track and has to be mentioned
L
Lucas De Marchi 已提交
3405
	 * separately
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431
	 * add 8 instead of 2 to keep 8 byte boundary
	 */
	pfx_datasize = sizeof(struct PFX_eckd_data) + 8;

	datasize = pfx_datasize + cidaw * sizeof(unsigned long long);

	/* Allocate the ccw request. */
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
				   datasize, startdev);
	if (IS_ERR(cqr))
		goto out;
	ccw = cqr->cpaddr;

	if (prefix_LRE(ccw++, cqr->data, first_trk, last_trk, cmd,
		       basedev, startdev, 1 /* format */, first_offs + 1,
		       trkcount, 0, 0) == -EAGAIN) {
		/* Clock not in sync and XRC is enabled.
		 * Try again later.
		 */
		dasd_sfree_request(cqr, startdev);
		cqr = ERR_PTR(-EAGAIN);
		goto out;
	}

	idaws = (unsigned long *)(cqr->data + pfx_datasize);
	len_to_track_end = 0;
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
	if (start_padding_sectors) {
		ccw[-1].flags |= CCW_FLAG_CC;
		ccw->cmd_code = cmd;
		/* maximum 3390 track size */
		ccw->count = 57326;
		/* 64k map to one track */
		len_to_track_end = 65536 - start_padding_sectors * 512;
		ccw->cda = (__u32)(addr_t)idaws;
		ccw->flags |= CCW_FLAG_IDA;
		ccw->flags |= CCW_FLAG_SLI;
		ccw++;
		for (sectors = 0; sectors < start_padding_sectors; sectors += 8)
			idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
	}
3446
	rq_for_each_segment(bv, req, iter) {
3447 3448
		dst = page_address(bv.bv_page) + bv.bv_offset;
		seg_len = bv.bv_len;
3449 3450
		if (cmd == DASD_ECKD_CCW_READ_TRACK)
			memset(dst, 0, seg_len);
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465
		if (!len_to_track_end) {
			ccw[-1].flags |= CCW_FLAG_CC;
			ccw->cmd_code = cmd;
			/* maximum 3390 track size */
			ccw->count = 57326;
			/* 64k map to one track */
			len_to_track_end = 65536;
			ccw->cda = (__u32)(addr_t)idaws;
			ccw->flags |= CCW_FLAG_IDA;
			ccw->flags |= CCW_FLAG_SLI;
			ccw++;
		}
		len_to_track_end -= seg_len;
		idaws = idal_create_words(idaws, dst, seg_len);
	}
3466 3467
	for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
		idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3468 3469 3470 3471 3472 3473 3474 3475
	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
	cqr->expires = startdev->default_expires * HZ;
	cqr->lpm = startdev->path_data.ppm;
3476
	cqr->retries = startdev->default_retries;
3477
	cqr->buildclk = get_tod_clock();
3478 3479 3480 3481 3482 3483 3484 3485 3486
	cqr->status = DASD_CQR_FILLED;

	if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
		cqr = NULL;
out:
	return cqr;
}


L
Linus Torvalds 已提交
3487 3488 3489 3490 3491
static int
dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
{
	struct dasd_eckd_private *private;
	struct ccw1 *ccw;
3492
	struct req_iterator iter;
3493
	struct bio_vec bv;
L
Linus Torvalds 已提交
3494 3495 3496
	char *dst, *cda;
	unsigned int blksize, blk_per_trk, off;
	sector_t recid;
3497
	int status;
L
Linus Torvalds 已提交
3498 3499 3500

	if (!dasd_page_cache)
		goto out;
3501 3502
	private = (struct dasd_eckd_private *) cqr->block->base->private;
	blksize = cqr->block->bp_block;
L
Linus Torvalds 已提交
3503
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3504
	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
L
Linus Torvalds 已提交
3505 3506 3507 3508 3509
	ccw = cqr->cpaddr;
	/* Skip over define extent & locate record. */
	ccw++;
	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
		ccw++;
3510
	rq_for_each_segment(bv, req, iter) {
3511 3512
		dst = page_address(bv.bv_page) + bv.bv_offset;
		for (off = 0; off < bv.bv_len; off += blksize) {
L
Linus Torvalds 已提交
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
			/* Skip locate record. */
			if (private->uses_cdl && recid <= 2*blk_per_trk)
				ccw++;
			if (dst) {
				if (ccw->flags & CCW_FLAG_IDA)
					cda = *((char **)((addr_t) ccw->cda));
				else
					cda = (char *)((addr_t) ccw->cda);
				if (dst != cda) {
					if (rq_data_dir(req) == READ)
3523
						memcpy(dst, cda, bv.bv_len);
L
Linus Torvalds 已提交
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
					kmem_cache_free(dasd_page_cache,
					    (void *)((addr_t)cda & PAGE_MASK));
				}
				dst = NULL;
			}
			ccw++;
			recid++;
		}
	}
out:
	status = cqr->status == DASD_CQR_DONE;
3535
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
3536 3537 3538
	return status;
}

3539
/*
3540
 * Modify ccw/tcw in cqr so it can be started on a base device.
3541 3542 3543 3544 3545 3546 3547 3548 3549
 *
 * Note that this is not enough to restart the cqr!
 * Either reset cqr->startdev as well (summary unit check handling)
 * or restart via separate cqr (as in ERP handling).
 */
void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
{
	struct ccw1 *ccw;
	struct PFX_eckd_data *pfxdata;
3550 3551 3552 3553 3554 3555 3556 3557 3558
	struct tcw *tcw;
	struct tccb *tccb;
	struct dcw *dcw;

	if (cqr->cpmode == 1) {
		tcw = cqr->cpaddr;
		tccb = tcw_get_tccb(tcw);
		dcw = (struct dcw *)&tccb->tca[0];
		pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
3559 3560
		pfxdata->validity.verify_base = 0;
		pfxdata->validity.hyper_pav = 0;
3561 3562 3563 3564 3565 3566 3567
	} else {
		ccw = cqr->cpaddr;
		pfxdata = cqr->data;
		if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
			pfxdata->validity.verify_base = 0;
			pfxdata->validity.hyper_pav = 0;
		}
3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
	}
}

#define DASD_ECKD_CHANQ_MAX_SIZE 4

static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
						     struct dasd_block *block,
						     struct request *req)
{
	struct dasd_eckd_private *private;
	struct dasd_device *startdev;
	unsigned long flags;
	struct dasd_ccw_req *cqr;

	startdev = dasd_alias_get_start_dev(base);
	if (!startdev)
		startdev = base;
	private = (struct dasd_eckd_private *) startdev->private;
	if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
		return ERR_PTR(-EBUSY);

	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
	private->count++;
3591 3592 3593 3594
	if ((base->features & DASD_FEATURE_USERAW))
		cqr = dasd_raw_build_cp(startdev, block, req);
	else
		cqr = dasd_eckd_build_cp(startdev, block, req);
3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
	if (IS_ERR(cqr))
		private->count--;
	spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
	return cqr;
}

static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
				   struct request *req)
{
	struct dasd_eckd_private *private;
	unsigned long flags;

	spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
	private = (struct dasd_eckd_private *) cqr->memdev->private;
	private->count--;
	spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
	return dasd_eckd_free_cp(cqr, req);
}

L
Linus Torvalds 已提交
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626
static int
dasd_eckd_fill_info(struct dasd_device * device,
		    struct dasd_information2_t * info)
{
	struct dasd_eckd_private *private;

	private = (struct dasd_eckd_private *) device->private;
	info->label_block = 2;
	info->FBA_layout = private->uses_cdl ? 0 : 1;
	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
	info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
	memcpy(info->characteristics, &private->rdc_data,
	       sizeof(struct dasd_eckd_characteristics));
3627 3628 3629 3630
	info->confdata_size = min((unsigned long)private->conf_len,
				  sizeof(info->configuration_data));
	memcpy(info->configuration_data, private->conf_data,
	       info->confdata_size);
L
Linus Torvalds 已提交
3631 3632 3633 3634 3635 3636 3637 3638 3639
	return 0;
}

/*
 * SECTION: ioctl functions for eckd devices.
 */

/*
 * Release device ioctl.
3640
 * Buils a channel programm to releases a prior reserved
L
Linus Torvalds 已提交
3641 3642 3643
 * (see dasd_eckd_reserve) device.
 */
static int
3644
dasd_eckd_release(struct dasd_device *device)
L
Linus Torvalds 已提交
3645 3646 3647
{
	struct dasd_ccw_req *cqr;
	int rc;
3648
	struct ccw1 *ccw;
3649
	int useglobal;
L
Linus Torvalds 已提交
3650 3651 3652 3653

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

3654
	useglobal = 0;
3655
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
3656
	if (IS_ERR(cqr)) {
3657 3658 3659 3660 3661 3662 3663 3664 3665
		mutex_lock(&dasd_reserve_mutex);
		useglobal = 1;
		cqr = &dasd_reserve_req->cqr;
		memset(cqr, 0, sizeof(*cqr));
		memset(&dasd_reserve_req->ccw, 0,
		       sizeof(dasd_reserve_req->ccw));
		cqr->cpaddr = &dasd_reserve_req->ccw;
		cqr->data = &dasd_reserve_req->data;
		cqr->magic = DASD_ECKD_MAGIC;
L
Linus Torvalds 已提交
3666
	}
3667 3668 3669 3670 3671
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
	ccw->flags |= CCW_FLAG_SLI;
	ccw->count = 32;
	ccw->cda = (__u32)(addr_t) cqr->data;
3672 3673
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
3674
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3675
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3676
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
3677
	cqr->expires = 2 * HZ;
3678
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
3679 3680 3681
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
3682 3683
	if (!rc)
		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
3684

3685 3686 3687 3688
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
3689 3690 3691 3692 3693 3694
	return rc;
}

/*
 * Reserve device ioctl.
 * Options are set to 'synchronous wait for interrupt' and
3695 3696
 * 'timeout the request'. This leads to a terminate IO if
 * the interrupt is outstanding for a certain time.
L
Linus Torvalds 已提交
3697 3698
 */
static int
3699
dasd_eckd_reserve(struct dasd_device *device)
L
Linus Torvalds 已提交
3700 3701 3702
{
	struct dasd_ccw_req *cqr;
	int rc;
3703
	struct ccw1 *ccw;
3704
	int useglobal;
L
Linus Torvalds 已提交
3705 3706 3707 3708

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

3709
	useglobal = 0;
3710
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
3711
	if (IS_ERR(cqr)) {
3712 3713 3714 3715 3716 3717 3718 3719 3720
		mutex_lock(&dasd_reserve_mutex);
		useglobal = 1;
		cqr = &dasd_reserve_req->cqr;
		memset(cqr, 0, sizeof(*cqr));
		memset(&dasd_reserve_req->ccw, 0,
		       sizeof(dasd_reserve_req->ccw));
		cqr->cpaddr = &dasd_reserve_req->ccw;
		cqr->data = &dasd_reserve_req->data;
		cqr->magic = DASD_ECKD_MAGIC;
L
Linus Torvalds 已提交
3721
	}
3722 3723 3724 3725 3726
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
	ccw->flags |= CCW_FLAG_SLI;
	ccw->count = 32;
	ccw->cda = (__u32)(addr_t) cqr->data;
3727 3728
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
3729
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3730
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3731
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
3732
	cqr->expires = 2 * HZ;
3733
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
3734 3735 3736
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
3737 3738
	if (!rc)
		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
3739

3740 3741 3742 3743
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
3744 3745 3746 3747 3748
	return rc;
}

/*
 * Steal lock ioctl - unconditional reserve device.
3749
 * Buils a channel programm to break a device's reservation.
L
Linus Torvalds 已提交
3750 3751 3752
 * (unconditional reserve)
 */
static int
3753
dasd_eckd_steal_lock(struct dasd_device *device)
L
Linus Torvalds 已提交
3754 3755 3756
{
	struct dasd_ccw_req *cqr;
	int rc;
3757
	struct ccw1 *ccw;
3758
	int useglobal;
L
Linus Torvalds 已提交
3759 3760 3761 3762

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

3763
	useglobal = 0;
3764
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
3765
	if (IS_ERR(cqr)) {
3766 3767 3768 3769 3770 3771 3772 3773 3774
		mutex_lock(&dasd_reserve_mutex);
		useglobal = 1;
		cqr = &dasd_reserve_req->cqr;
		memset(cqr, 0, sizeof(*cqr));
		memset(&dasd_reserve_req->ccw, 0,
		       sizeof(dasd_reserve_req->ccw));
		cqr->cpaddr = &dasd_reserve_req->ccw;
		cqr->data = &dasd_reserve_req->data;
		cqr->magic = DASD_ECKD_MAGIC;
L
Linus Torvalds 已提交
3775
	}
3776 3777 3778 3779 3780
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_SLCK;
	ccw->flags |= CCW_FLAG_SLI;
	ccw->count = 32;
	ccw->cda = (__u32)(addr_t) cqr->data;
3781 3782
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
3783
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3784
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3785
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
3786
	cqr->expires = 2 * HZ;
3787
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
3788 3789 3790
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
3791 3792
	if (!rc)
		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
3793

3794 3795 3796 3797
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
3798 3799 3800
	return rc;
}

3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844
/*
 * SNID - Sense Path Group ID
 * This ioctl may be used in situations where I/O is stalled due to
 * a reserve, so if the normal dasd_smalloc_request fails, we use the
 * preallocated dasd_reserve_req.
 */
static int dasd_eckd_snid(struct dasd_device *device,
			  void __user *argp)
{
	struct dasd_ccw_req *cqr;
	int rc;
	struct ccw1 *ccw;
	int useglobal;
	struct dasd_snid_ioctl_data usrparm;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
		return -EFAULT;

	useglobal = 0;
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
				   sizeof(struct dasd_snid_data), device);
	if (IS_ERR(cqr)) {
		mutex_lock(&dasd_reserve_mutex);
		useglobal = 1;
		cqr = &dasd_reserve_req->cqr;
		memset(cqr, 0, sizeof(*cqr));
		memset(&dasd_reserve_req->ccw, 0,
		       sizeof(dasd_reserve_req->ccw));
		cqr->cpaddr = &dasd_reserve_req->ccw;
		cqr->data = &dasd_reserve_req->data;
		cqr->magic = DASD_ECKD_MAGIC;
	}
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_SNID;
	ccw->flags |= CCW_FLAG_SLI;
	ccw->count = 12;
	ccw->cda = (__u32)(addr_t) cqr->data;
	cqr->startdev = device;
	cqr->memdev = device;
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3845
	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
3846 3847
	cqr->retries = 5;
	cqr->expires = 10 * HZ;
3848
	cqr->buildclk = get_tod_clock();
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
	cqr->status = DASD_CQR_FILLED;
	cqr->lpm = usrparm.path_mask;

	rc = dasd_sleep_on_immediatly(cqr);
	/* verify that I/O processing didn't modify the path mask */
	if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
		rc = -EIO;
	if (!rc) {
		usrparm.data = *((struct dasd_snid_data *)cqr->data);
		if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
			rc = -EFAULT;
	}

	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}

L
Linus Torvalds 已提交
3869 3870 3871 3872
/*
 * Read performance statistics
 */
static int
3873
dasd_eckd_performance(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
3874 3875 3876 3877 3878 3879 3880
{
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_perf_stats_t *stats;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

3881
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
3882 3883
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_perf_stats_t)),
L
Linus Torvalds 已提交
3884 3885
				   device);
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
3886
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
L
Linus Torvalds 已提交
3887 3888 3889
			    "Could not allocate initialization request");
		return PTR_ERR(cqr);
	}
3890 3891
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
3892
	cqr->retries = 0;
3893
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
L
Linus Torvalds 已提交
3894 3895 3896 3897
	cqr->expires = 10 * HZ;

	/* Prepare for Read Subsystem Data */
	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3898
	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
L
Linus Torvalds 已提交
3899
	prssdp->order = PSF_ORDER_PRSSD;
3900
	prssdp->suborder = 0x01;	/* Performance Statistics */
L
Linus Torvalds 已提交
3901 3902 3903 3904
	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */

	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
3905
	ccw->count = sizeof(struct dasd_psf_prssd_data);
L
Linus Torvalds 已提交
3906 3907 3908 3909 3910
	ccw->flags |= CCW_FLAG_CC;
	ccw->cda = (__u32)(addr_t) prssdp;

	/* Read Subsystem Data - Performance Statistics */
	stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
3911
	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
L
Linus Torvalds 已提交
3912 3913 3914

	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
3915
	ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
L
Linus Torvalds 已提交
3916 3917
	ccw->cda = (__u32)(addr_t) stats;

3918
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
3919 3920 3921 3922 3923
	cqr->status = DASD_CQR_FILLED;
	rc = dasd_sleep_on(cqr);
	if (rc == 0) {
		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
		stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
3924 3925 3926
		if (copy_to_user(argp, stats,
				 sizeof(struct dasd_rssd_perf_stats_t)))
			rc = -EFAULT;
L
Linus Torvalds 已提交
3927
	}
3928
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
3929 3930 3931 3932 3933 3934 3935 3936
	return rc;
}

/*
 * Get attributes (cache operations)
 * Returnes the cache attributes used in Define Extend (DE).
 */
static int
3937
dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
3938
{
3939 3940 3941
	struct dasd_eckd_private *private =
		(struct dasd_eckd_private *)device->private;
	struct attrib_data_t attrib = private->attrib;
L
Linus Torvalds 已提交
3942 3943 3944 3945
	int rc;

        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
3946
	if (!argp)
L
Linus Torvalds 已提交
3947 3948
                return -EINVAL;

3949 3950
	rc = 0;
	if (copy_to_user(argp, (long *) &attrib,
3951
			 sizeof(struct attrib_data_t)))
3952
		rc = -EFAULT;
L
Linus Torvalds 已提交
3953 3954 3955 3956 3957 3958 3959 3960 3961

	return rc;
}

/*
 * Set attributes (cache operations)
 * Stores the attributes for cache operation to be used in Define Extend (DE).
 */
static int
3962
dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
3963
{
3964 3965
	struct dasd_eckd_private *private =
		(struct dasd_eckd_private *)device->private;
L
Linus Torvalds 已提交
3966 3967 3968 3969
	struct attrib_data_t attrib;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
3970
	if (!argp)
L
Linus Torvalds 已提交
3971 3972
		return -EINVAL;

3973
	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
L
Linus Torvalds 已提交
3974 3975 3976
		return -EFAULT;
	private->attrib = attrib;

S
Stefan Haberland 已提交
3977 3978 3979
	dev_info(&device->cdev->dev,
		 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
		 private->attrib.operation, private->attrib.nr_cyl);
L
Linus Torvalds 已提交
3980 3981 3982
	return 0;
}

3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
/*
 * Issue syscall I/O to EMC Symmetrix array.
 * CCWs are PSF and RSSD
 */
static int dasd_symm_io(struct dasd_device *device, void __user *argp)
{
	struct dasd_symmio_parms usrparm;
	char *psf_data, *rssd_result;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
3993
	char psf0, psf1;
3994 3995
	int rc;

3996 3997 3998 3999
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
		return -EACCES;
	psf0 = psf1 = 0;

4000 4001 4002 4003
	/* Copy parms from caller */
	rc = -EFAULT;
	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
		goto out;
4004
	if (is_compat_task()) {
4005
		/* Make sure pointers are sane even on 31 bit. */
4006
		rc = -EINVAL;
4007 4008 4009 4010 4011 4012
		if ((usrparm.psf_data >> 32) != 0)
			goto out;
		if ((usrparm.rssd_result >> 32) != 0)
			goto out;
		usrparm.psf_data &= 0x7fffffffULL;
		usrparm.rssd_result &= 0x7fffffffULL;
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027
	}
	/* alloc I/O data area */
	psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
	rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
	if (!psf_data || !rssd_result) {
		rc = -ENOMEM;
		goto out_free;
	}

	/* get syscall header from user space */
	rc = -EFAULT;
	if (copy_from_user(psf_data,
			   (void __user *)(unsigned long) usrparm.psf_data,
			   usrparm.psf_data_len))
		goto out_free;
4028 4029
	psf0 = psf_data[0];
	psf1 = psf_data[1];
4030 4031

	/* setup CCWs for PSF + RSSD */
4032
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
4033
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
4034
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4035 4036 4037 4038 4039 4040 4041 4042 4043
			"Could not allocate initialization request");
		rc = PTR_ERR(cqr);
		goto out_free;
	}

	cqr->startdev = device;
	cqr->memdev = device;
	cqr->retries = 3;
	cqr->expires = 10 * HZ;
4044
	cqr->buildclk = get_tod_clock();
4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
	cqr->status = DASD_CQR_FILLED;

	/* Build the ccws */
	ccw = cqr->cpaddr;

	/* PSF ccw */
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->count = usrparm.psf_data_len;
	ccw->flags |= CCW_FLAG_CC;
	ccw->cda = (__u32)(addr_t) psf_data;

	ccw++;

	/* RSSD ccw  */
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
	ccw->count = usrparm.rssd_result_len;
	ccw->flags = CCW_FLAG_SLI ;
	ccw->cda = (__u32)(addr_t) rssd_result;

	rc = dasd_sleep_on(cqr);
	if (rc)
		goto out_sfree;

	rc = -EFAULT;
	if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
			   rssd_result, usrparm.rssd_result_len))
		goto out_sfree;
	rc = 0;

out_sfree:
	dasd_sfree_request(cqr, cqr->memdev);
out_free:
	kfree(rssd_result);
	kfree(psf_data);
out:
4080 4081 4082
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
		      (int) psf0, (int) psf1, rc);
4083 4084 4085
	return rc;
}

4086
static int
4087
dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
4088
{
4089 4090
	struct dasd_device *device = block->base;

4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
	switch (cmd) {
	case BIODASDGATTR:
		return dasd_eckd_get_attrib(device, argp);
	case BIODASDSATTR:
		return dasd_eckd_set_attrib(device, argp);
	case BIODASDPSRD:
		return dasd_eckd_performance(device, argp);
	case BIODASDRLSE:
		return dasd_eckd_release(device);
	case BIODASDRSRV:
		return dasd_eckd_reserve(device);
	case BIODASDSLCK:
		return dasd_eckd_steal_lock(device);
4104 4105
	case BIODASDSNID:
		return dasd_eckd_snid(device, argp);
4106 4107
	case BIODASDSYMMIO:
		return dasd_symm_io(device, argp);
4108
	default:
4109
		return -ENOTTY;
4110 4111 4112
	}
}

H
Horst Hummel 已提交
4113 4114 4115 4116
/*
 * Dump the range of CCWs into 'page' buffer
 * and return number of printed chars.
 */
4117
static int
H
Horst Hummel 已提交
4118 4119 4120 4121 4122 4123 4124
dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
{
	int len, count;
	char *datap;

	len = 0;
	while (from <= to) {
4125
		len += sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146
			       " CCW %p: %08X %08X DAT:",
			       from, ((int *) from)[0], ((int *) from)[1]);

		/* get pointer to data (consider IDALs) */
		if (from->flags & CCW_FLAG_IDA)
			datap = (char *) *((addr_t *) (addr_t) from->cda);
		else
			datap = (char *) ((addr_t) from->cda);

		/* dump data (max 32 bytes) */
		for (count = 0; count < from->count && count < 32; count++) {
			if (count % 8 == 0) len += sprintf(page + len, " ");
			if (count % 4 == 0) len += sprintf(page + len, " ");
			len += sprintf(page + len, "%02x", datap[count]);
		}
		len += sprintf(page + len, "\n");
		from++;
	}
	return len;
}

S
Stefan Haberland 已提交
4147
static void
4148 4149
dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
			 char *reason)
S
Stefan Haberland 已提交
4150 4151
{
	u64 *sense;
4152
	u64 *stat;
4153 4154

	sense = (u64 *) dasd_get_sense(irb);
4155
	stat = (u64 *) &irb->scsw;
S
Stefan Haberland 已提交
4156
	if (sense) {
4157 4158 4159
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
			      "%016llx %016llx %016llx %016llx",
			      reason, *stat, *((u32 *) (stat + 1)),
S
Stefan Haberland 已提交
4160
			      sense[0], sense[1], sense[2], sense[3]);
S
Stefan Haberland 已提交
4161
	} else {
4162 4163 4164
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
			      reason, *stat, *((u32 *) (stat + 1)),
			      "NO VALID SENSE");
S
Stefan Haberland 已提交
4165 4166 4167
	}
}

L
Linus Torvalds 已提交
4168 4169 4170 4171
/*
 * Print sense data and related channel program.
 * Parts are printed because printk buffer is only 1024 bytes.
 */
4172
static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
4173
				 struct dasd_ccw_req *req, struct irb *irb)
L
Linus Torvalds 已提交
4174 4175
{
	char *page;
H
Horst Hummel 已提交
4176 4177
	struct ccw1 *first, *last, *fail, *from, *to;
	int len, sl, sct;
L
Linus Torvalds 已提交
4178 4179 4180

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
4181 4182
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "No memory to dump sense data\n");
L
Linus Torvalds 已提交
4183 4184
		return;
	}
H
Horst Hummel 已提交
4185
	/* dump the sense data */
4186
	len = sprintf(page, PRINTK_HEADER
L
Linus Torvalds 已提交
4187
		      " I/O status report for device %s:\n",
4188
		      dev_name(&device->cdev->dev));
4189
	len += sprintf(page + len, PRINTK_HEADER
4190 4191 4192 4193 4194 4195
		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
		       "CS:%02X RC:%d\n",
		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
		       req ? req->intrc : 0);
4196
	len += sprintf(page + len, PRINTK_HEADER
L
Linus Torvalds 已提交
4197
		       " device %s: Failing CCW: %p\n",
4198
		       dev_name(&device->cdev->dev),
4199
		       (void *) (addr_t) irb->scsw.cmd.cpa);
L
Linus Torvalds 已提交
4200 4201
	if (irb->esw.esw0.erw.cons) {
		for (sl = 0; sl < 4; sl++) {
4202
			len += sprintf(page + len, PRINTK_HEADER
L
Linus Torvalds 已提交
4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214
				       " Sense(hex) %2d-%2d:",
				       (8 * sl), ((8 * sl) + 7));

			for (sct = 0; sct < 8; sct++) {
				len += sprintf(page + len, " %02x",
					       irb->ecw[8 * sl + sct]);
			}
			len += sprintf(page + len, "\n");
		}

		if (irb->ecw[27] & DASD_SENSE_BIT_0) {
			/* 24 Byte Sense Data */
4215
			sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4216 4217 4218 4219
				" 24 Byte: %x MSG %x, "
				"%s MSGb to SYSOP\n",
				irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
				irb->ecw[1] & 0x10 ? "" : "no");
L
Linus Torvalds 已提交
4220 4221
		} else {
			/* 32 Byte Sense Data */
4222
			sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4223 4224 4225
				" 32 Byte: Format: %x "
				"Exception class %x\n",
				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
L
Linus Torvalds 已提交
4226 4227
		}
	} else {
4228
		sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4229
			" SORRY - NO VALID SENSE AVAILABLE\n");
L
Linus Torvalds 已提交
4230
	}
4231
	printk(KERN_ERR "%s", page);
H
Horst Hummel 已提交
4232

4233 4234 4235 4236 4237 4238 4239
	if (req) {
		/* req == NULL for unsolicited interrupts */
		/* dump the Channel Program (max 140 Bytes per line) */
		/* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
		first = req->cpaddr;
		for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
		to = min(first + 6, last);
4240
		len = sprintf(page, PRINTK_HEADER
4241 4242
			      " Related CP in req: %p\n", req);
		dasd_eckd_dump_ccw_range(first, to, page + len);
4243
		printk(KERN_ERR "%s", page);
L
Linus Torvalds 已提交
4244

4245 4246 4247 4248
		/* print failing CCW area (maximum 4) */
		/* scsw->cda is either valid or zero  */
		len = 0;
		from = ++to;
4249 4250
		fail = (struct ccw1 *)(addr_t)
				irb->scsw.cmd.cpa; /* failing CCW */
4251 4252
		if (from <  fail - 2) {
			from = fail - 2;     /* there is a gap - print header */
4253
			len += sprintf(page, PRINTK_HEADER "......\n");
4254 4255 4256 4257 4258 4259 4260 4261
		}
		to = min(fail + 1, last);
		len += dasd_eckd_dump_ccw_range(from, to, page + len);

		/* print last CCWs (maximum 2) */
		from = max(from, ++to);
		if (from < last - 1) {
			from = last - 1;     /* there is a gap - print header */
4262
			len += sprintf(page + len, PRINTK_HEADER "......\n");
4263 4264 4265
		}
		len += dasd_eckd_dump_ccw_range(from, last, page + len);
		if (len > 0)
4266
			printk(KERN_ERR "%s", page);
L
Linus Torvalds 已提交
4267 4268 4269 4270
	}
	free_page((unsigned long) page);
}

4271 4272 4273 4274 4275 4276 4277 4278 4279 4280

/*
 * Print sense data from a tcw.
 */
static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
				 struct dasd_ccw_req *req, struct irb *irb)
{
	char *page;
	int len, sl, sct, residual;
	struct tsb *tsb;
4281
	u8 *sense, *rcq;
4282 4283 4284

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
4285
		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
4286 4287 4288 4289
			    "No memory to dump sense data");
		return;
	}
	/* dump the sense data */
4290
	len = sprintf(page, PRINTK_HEADER
4291 4292
		      " I/O status report for device %s:\n",
		      dev_name(&device->cdev->dev));
4293
	len += sprintf(page + len, PRINTK_HEADER
4294 4295 4296 4297 4298 4299 4300
		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
		       "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
		       irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
		       req ? req->intrc : 0);
4301
	len += sprintf(page + len, PRINTK_HEADER
4302 4303 4304 4305 4306 4307
		       " device %s: Failing TCW: %p\n",
		       dev_name(&device->cdev->dev),
		       (void *) (addr_t) irb->scsw.tm.tcw);

	tsb = NULL;
	sense = NULL;
4308
	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
4309 4310 4311
		tsb = tcw_get_tsb(
			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);

4312
	if (tsb) {
4313
		len += sprintf(page + len, PRINTK_HEADER
4314
			       " tsb->length %d\n", tsb->length);
4315
		len += sprintf(page + len, PRINTK_HEADER
4316
			       " tsb->flags %x\n", tsb->flags);
4317
		len += sprintf(page + len, PRINTK_HEADER
4318
			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
4319
		len += sprintf(page + len, PRINTK_HEADER
4320 4321
			       " tsb->count %d\n", tsb->count);
		residual = tsb->count - 28;
4322
		len += sprintf(page + len, PRINTK_HEADER
4323 4324 4325 4326
			       " residual %d\n", residual);

		switch (tsb->flags & 0x07) {
		case 1:	/* tsa_iostat */
4327
			len += sprintf(page + len, PRINTK_HEADER
4328 4329
			       " tsb->tsa.iostat.dev_time %d\n",
				       tsb->tsa.iostat.dev_time);
4330
			len += sprintf(page + len, PRINTK_HEADER
4331 4332
			       " tsb->tsa.iostat.def_time %d\n",
				       tsb->tsa.iostat.def_time);
4333
			len += sprintf(page + len, PRINTK_HEADER
4334 4335
			       " tsb->tsa.iostat.queue_time %d\n",
				       tsb->tsa.iostat.queue_time);
4336
			len += sprintf(page + len, PRINTK_HEADER
4337 4338
			       " tsb->tsa.iostat.dev_busy_time %d\n",
				       tsb->tsa.iostat.dev_busy_time);
4339
			len += sprintf(page + len, PRINTK_HEADER
4340 4341 4342 4343 4344
			       " tsb->tsa.iostat.dev_act_time %d\n",
				       tsb->tsa.iostat.dev_act_time);
			sense = tsb->tsa.iostat.sense;
			break;
		case 2: /* ts_ddpc */
4345
			len += sprintf(page + len, PRINTK_HEADER
4346
			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
4347
			for (sl = 0; sl < 2; sl++) {
4348
				len += sprintf(page + len, PRINTK_HEADER
4349 4350 4351
					       " tsb->tsa.ddpc.rcq %2d-%2d: ",
					       (8 * sl), ((8 * sl) + 7));
				rcq = tsb->tsa.ddpc.rcq;
4352 4353
				for (sct = 0; sct < 8; sct++) {
					len += sprintf(page + len, " %02x",
4354
						       rcq[8 * sl + sct]);
4355 4356 4357 4358 4359 4360
				}
				len += sprintf(page + len, "\n");
			}
			sense = tsb->tsa.ddpc.sense;
			break;
		case 3: /* tsa_intrg */
4361 4362
			len += sprintf(page + len, PRINTK_HEADER
				      " tsb->tsa.intrg.: not supportet yet\n");
4363 4364 4365 4366 4367
			break;
		}

		if (sense) {
			for (sl = 0; sl < 4; sl++) {
4368
				len += sprintf(page + len, PRINTK_HEADER
4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379
					       " Sense(hex) %2d-%2d:",
					       (8 * sl), ((8 * sl) + 7));
				for (sct = 0; sct < 8; sct++) {
					len += sprintf(page + len, " %02x",
						       sense[8 * sl + sct]);
				}
				len += sprintf(page + len, "\n");
			}

			if (sense[27] & DASD_SENSE_BIT_0) {
				/* 24 Byte Sense Data */
4380
				sprintf(page + len, PRINTK_HEADER
4381 4382 4383 4384 4385 4386
					" 24 Byte: %x MSG %x, "
					"%s MSGb to SYSOP\n",
					sense[7] >> 4, sense[7] & 0x0f,
					sense[1] & 0x10 ? "" : "no");
			} else {
				/* 32 Byte Sense Data */
4387
				sprintf(page + len, PRINTK_HEADER
4388 4389 4390 4391 4392
					" 32 Byte: Format: %x "
					"Exception class %x\n",
					sense[6] & 0x0f, sense[22] >> 4);
			}
		} else {
4393
			sprintf(page + len, PRINTK_HEADER
4394 4395 4396
				" SORRY - NO VALID SENSE AVAILABLE\n");
		}
	} else {
4397
		sprintf(page + len, PRINTK_HEADER
4398 4399
			" SORRY - NO TSB DATA AVAILABLE\n");
	}
4400
	printk(KERN_ERR "%s", page);
4401 4402 4403 4404 4405 4406
	free_page((unsigned long) page);
}

static void dasd_eckd_dump_sense(struct dasd_device *device,
				 struct dasd_ccw_req *req, struct irb *irb)
{
4407
	if (scsw_is_tm(&irb->scsw))
4408 4409 4410 4411 4412
		dasd_eckd_dump_sense_tcw(device, req, irb);
	else
		dasd_eckd_dump_sense_ccw(device, req, irb);
}

4413
static int dasd_eckd_pm_freeze(struct dasd_device *device)
4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425
{
	/*
	 * the device should be disconnected from our LCU structure
	 * on restore we will reconnect it and reread LCU specific
	 * information like PAV support that might have changed
	 */
	dasd_alias_remove_device(device);
	dasd_alias_disconnect_device_from_lcu(device);

	return 0;
}

4426
static int dasd_eckd_restore_device(struct dasd_device *device)
4427 4428
{
	struct dasd_eckd_private *private;
4429
	struct dasd_eckd_characteristics temp_rdc_data;
4430
	int rc;
4431
	struct dasd_uid temp_uid;
S
Stefan Haberland 已提交
4432
	unsigned long flags;
4433
	unsigned long cqr_flags = 0;
4434 4435 4436 4437

	private = (struct dasd_eckd_private *) device->private;

	/* Read Configuration Data */
4438
	dasd_eckd_read_conf(device);
4439

4440 4441 4442 4443
	dasd_eckd_get_uid(device, &temp_uid);
	/* Generate device unique id */
	rc = dasd_eckd_generate_uid(device);
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4444
	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
S
Stefan Haberland 已提交
4445 4446
		dev_err(&device->cdev->dev, "The UID of the DASD has "
			"changed\n");
4447
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4448 4449 4450 4451
	if (rc)
		goto out_err;

	/* register lcu with alias handling, enable PAV if this is a new lcu */
4452 4453 4454
	rc = dasd_alias_make_device_known_to_lcu(device);
	if (rc)
		return rc;
4455 4456 4457

	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
	dasd_eckd_validate_server(device, cqr_flags);
4458 4459

	/* RE-Read Configuration Data */
4460
	dasd_eckd_read_conf(device);
4461 4462

	/* Read Feature Codes */
4463
	dasd_eckd_read_features(device);
4464 4465

	/* Read Device Characteristics */
4466
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
4467
					 &temp_rdc_data, 64);
4468
	if (rc) {
4469 4470
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
4471 4472
		goto out_err;
	}
S
Stefan Haberland 已提交
4473
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4474
	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
S
Stefan Haberland 已提交
4475
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4476 4477 4478 4479 4480 4481 4482

	/* add device to alias management */
	dasd_alias_add_device(device);

	return 0;

out_err:
4483
	return -1;
4484 4485
}

4486 4487 4488 4489
static int dasd_eckd_reload_device(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
	int rc, old_base;
4490 4491 4492
	char print_uid[60];
	struct dasd_uid uid;
	unsigned long flags;
4493 4494

	private = (struct dasd_eckd_private *) device->private;
4495 4496

	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4497
	old_base = private->uid.base_unit_addr;
4498 4499
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);

4500 4501 4502 4503 4504
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err;

4505
	rc = dasd_eckd_generate_uid(device);
4506 4507 4508 4509 4510 4511 4512 4513
	if (rc)
		goto out_err;
	/*
	 * update unit address configuration and
	 * add device to alias management
	 */
	dasd_alias_update_add_device(device);

4514 4515 4516 4517 4518 4519 4520
	dasd_eckd_get_uid(device, &uid);

	if (old_base != uid.base_unit_addr) {
		if (strlen(uid.vduit) > 0)
			snprintf(print_uid, sizeof(print_uid),
				 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
				 uid.ssid, uid.base_unit_addr, uid.vduit);
4521
		else
4522 4523 4524
			snprintf(print_uid, sizeof(print_uid),
				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
				 uid.ssid, uid.base_unit_addr);
4525 4526 4527

		dev_info(&device->cdev->dev,
			 "An Alias device was reassigned to a new base device "
4528
			 "with UID: %s\n", print_uid);
4529 4530 4531 4532 4533 4534 4535
	}
	return 0;

out_err:
	return -1;
}

4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562
static int dasd_eckd_read_message_buffer(struct dasd_device *device,
					 struct dasd_rssd_messages *messages,
					 __u8 lpum)
{
	struct dasd_rssd_messages *message_buf;
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_eckd_private *private;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

	private = (struct dasd_eckd_private *) device->private;
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_messages)),
				   device);
	if (IS_ERR(cqr)) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate read message buffer request");
		return PTR_ERR(cqr);
	}

	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->expires = 10 * HZ;
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
4563 4564 4565 4566 4567
	/* dasd_sleep_on_immediatly does not do complex error
	 * recovery so clear erp flag and set retry counter to
	 * do basic erp */
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
	cqr->retries = 256;
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644

	/* Prepare for Read Subsystem Data */
	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
	prssdp->order = PSF_ORDER_PRSSD;
	prssdp->suborder = 0x03;	/* Message Buffer */
	/* all other bytes of prssdp must be zero */

	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->count = sizeof(struct dasd_psf_prssd_data);
	ccw->flags |= CCW_FLAG_CC;
	ccw->flags |= CCW_FLAG_SLI;
	ccw->cda = (__u32)(addr_t) prssdp;

	/* Read Subsystem Data - message buffer */
	message_buf = (struct dasd_rssd_messages *) (prssdp + 1);
	memset(message_buf, 0, sizeof(struct dasd_rssd_messages));

	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
	ccw->count = sizeof(struct dasd_rssd_messages);
	ccw->flags |= CCW_FLAG_SLI;
	ccw->cda = (__u32)(addr_t) message_buf;

	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
	rc = dasd_sleep_on_immediatly(cqr);
	if (rc == 0) {
		prssdp = (struct dasd_psf_prssd_data *) cqr->data;
		message_buf = (struct dasd_rssd_messages *)
			(prssdp + 1);
		memcpy(messages, message_buf,
		       sizeof(struct dasd_rssd_messages));
	} else
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Reading messages failed with rc=%d\n"
				, rc);
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}

/*
 * Perform Subsystem Function - CUIR response
 */
static int
dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
			    __u32 message_id,
			    struct channel_path_desc *desc,
			    struct subchannel_id sch_id)
{
	struct dasd_psf_cuir_response *psf_cuir;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
				  sizeof(struct dasd_psf_cuir_response),
				  device);

	if (IS_ERR(cqr)) {
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			   "Could not allocate PSF-CUIR request");
		return PTR_ERR(cqr);
	}

	psf_cuir = (struct dasd_psf_cuir_response *)cqr->data;
	psf_cuir->order = PSF_ORDER_CUIR_RESPONSE;
	psf_cuir->cc = response;
	if (desc)
		psf_cuir->chpid = desc->chpid;
	psf_cuir->message_id = message_id;
	psf_cuir->cssid = sch_id.cssid;
	psf_cuir->ssid = sch_id.ssid;
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->cda = (__u32)(addr_t)psf_cuir;
4645
	ccw->flags = CCW_FLAG_SLI;
4646 4647 4648 4649 4650 4651 4652 4653 4654
	ccw->count = sizeof(struct dasd_psf_cuir_response);

	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->retries = 256;
	cqr->expires = 10*HZ;
	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
4655
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
4656 4657 4658 4659 4660 4661 4662

	rc = dasd_sleep_on(cqr);

	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}

4663 4664 4665 4666 4667 4668 4669 4670
/*
 * return configuration data that is referenced by record selector
 * if a record selector is specified or per default return the
 * conf_data pointer for the path specified by lpum
 */
static struct dasd_conf_data *dasd_eckd_get_ref_conf(struct dasd_device *device,
						     __u8 lpum,
						     struct dasd_cuir_message *cuir)
4671
{
4672 4673 4674
	struct dasd_eckd_private *private;
	struct dasd_conf_data *conf_data;
	int path, pos;
4675

4676 4677 4678 4679 4680 4681 4682 4683
	private = (struct dasd_eckd_private *) device->private;
	if (cuir->record_selector == 0)
		goto out;
	for (path = 0x80, pos = 0; path; path >>= 1, pos++) {
		conf_data = private->path_conf_data[pos];
		if (conf_data->gneq.record_selector ==
		    cuir->record_selector)
			return conf_data;
4684
	}
4685
out:
4686
	return private->path_conf_data[pathmask_to_pos(lpum)];
4687 4688 4689
}

/*
4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
 * This function determines the scope of a reconfiguration request by
 * analysing the path and device selection data provided in the CUIR request.
 * Returns a path mask containing CUIR affected paths for the give device.
 *
 * If the CUIR request does not contain the required information return the
 * path mask of the path the attention message for the CUIR request was reveived
 * on.
 */
static int dasd_eckd_cuir_scope(struct dasd_device *device, __u8 lpum,
				struct dasd_cuir_message *cuir)
{
	struct dasd_conf_data *ref_conf_data;
	unsigned long bitmask = 0, mask = 0;
	struct dasd_eckd_private *private;
	struct dasd_conf_data *conf_data;
	unsigned int pos, path;
	char *ref_gneq, *gneq;
	char *ref_ned, *ned;
	int tbcpm = 0;

	/* if CUIR request does not specify the scope use the path
	   the attention message was presented on */
	if (!cuir->ned_map ||
	    !(cuir->neq_map[0] | cuir->neq_map[1] | cuir->neq_map[2]))
		return lpum;

	private = (struct dasd_eckd_private *) device->private;
	/* get reference conf data */
	ref_conf_data = dasd_eckd_get_ref_conf(device, lpum, cuir);
	/* reference ned is determined by ned_map field */
	pos = 8 - ffs(cuir->ned_map);
	ref_ned = (char *)&ref_conf_data->neds[pos];
	ref_gneq = (char *)&ref_conf_data->gneq;
	/* transfer 24 bit neq_map to mask */
	mask = cuir->neq_map[2];
	mask |= cuir->neq_map[1] << 8;
	mask |= cuir->neq_map[0] << 16;

	for (path = 0x80; path; path >>= 1) {
		/* initialise data per path */
		bitmask = mask;
4731
		pos = pathmask_to_pos(path);
4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805
		conf_data = private->path_conf_data[pos];
		pos = 8 - ffs(cuir->ned_map);
		ned = (char *) &conf_data->neds[pos];
		/* compare reference ned and per path ned */
		if (memcmp(ref_ned, ned, sizeof(*ned)) != 0)
			continue;
		gneq = (char *)&conf_data->gneq;
		/* compare reference gneq and per_path gneq under
		   24 bit mask where mask bit 0 equals byte 7 of
		   the gneq and mask bit 24 equals byte 31 */
		while (bitmask) {
			pos = ffs(bitmask) - 1;
			if (memcmp(&ref_gneq[31 - pos], &gneq[31 - pos], 1)
			    != 0)
				break;
			clear_bit(pos, &bitmask);
		}
		if (bitmask)
			continue;
		/* device and path match the reference values
		   add path to CUIR scope */
		tbcpm |= path;
	}
	return tbcpm;
}

static void dasd_eckd_cuir_notify_user(struct dasd_device *device,
				       unsigned long paths,
				       struct subchannel_id sch_id, int action)
{
	struct channel_path_desc *desc;
	int pos;

	while (paths) {
		/* get position of bit in mask */
		pos = ffs(paths) - 1;
		/* get channel path descriptor from this position */
		desc = ccw_device_get_chp_desc(device->cdev, 7 - pos);
		if (action == CUIR_QUIESCE)
			pr_warn("Service on the storage server caused path "
				"%x.%02x to go offline", sch_id.cssid,
				desc ? desc->chpid : 0);
		else if (action == CUIR_RESUME)
			pr_info("Path %x.%02x is back online after service "
				"on the storage server", sch_id.cssid,
				desc ? desc->chpid : 0);
		kfree(desc);
		clear_bit(pos, &paths);
	}
}

static int dasd_eckd_cuir_remove_path(struct dasd_device *device, __u8 lpum,
				      struct dasd_cuir_message *cuir)
{
	unsigned long tbcpm;

	tbcpm = dasd_eckd_cuir_scope(device, lpum, cuir);
	/* nothing to do if path is not in use */
	if (!(device->path_data.opm & tbcpm))
		return 0;
	if (!(device->path_data.opm & ~tbcpm)) {
		/* no path would be left if the CUIR action is taken
		   return error */
		return -EINVAL;
	}
	/* remove device from operational path mask */
	device->path_data.opm &= ~tbcpm;
	device->path_data.cuirpm |= tbcpm;
	return tbcpm;
}

/*
 * walk through all devices and build a path mask to quiesce them
 * return an error if the last path to a device would be removed
4806 4807 4808 4809 4810 4811
 *
 * if only part of the devices are quiesced and an error
 * occurs no onlining necessary, the storage server will
 * notify the already set offline devices again
 */
static int dasd_eckd_cuir_quiesce(struct dasd_device *device, __u8 lpum,
4812 4813
				  struct subchannel_id sch_id,
				  struct dasd_cuir_message *cuir)
4814 4815 4816 4817
{
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_eckd_private *private;
	struct dasd_device *dev, *n;
4818 4819 4820
	unsigned long paths = 0;
	unsigned long flags;
	int tbcpm;
4821 4822 4823

	private = (struct dasd_eckd_private *) device->private;
	/* active devices */
4824
	list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
4825
				 alias_list) {
4826 4827 4828 4829 4830 4831
		spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
		tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
		spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
		if (tbcpm < 0)
			goto out_err;
		paths |= tbcpm;
4832 4833
	}
	/* inactive devices */
4834
	list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
4835
				 alias_list) {
4836 4837 4838 4839 4840 4841
		spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
		tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
		spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
		if (tbcpm < 0)
			goto out_err;
		paths |= tbcpm;
4842 4843 4844 4845 4846 4847
	}
	/* devices in PAV groups */
	list_for_each_entry_safe(pavgroup, tempgroup,
				 &private->lcu->grouplist, group) {
		list_for_each_entry_safe(dev, n, &pavgroup->baselist,
					 alias_list) {
4848 4849 4850 4851 4852 4853 4854
			spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
			tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
			spin_unlock_irqrestore(
				get_ccwdev_lock(dev->cdev), flags);
			if (tbcpm < 0)
				goto out_err;
			paths |= tbcpm;
4855 4856 4857
		}
		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
					 alias_list) {
4858 4859 4860 4861 4862 4863 4864
			spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
			tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
			spin_unlock_irqrestore(
				get_ccwdev_lock(dev->cdev), flags);
			if (tbcpm < 0)
				goto out_err;
			paths |= tbcpm;
4865 4866
		}
	}
4867 4868 4869 4870 4871
	/* notify user about all paths affected by CUIR action */
	dasd_eckd_cuir_notify_user(device, paths, sch_id, CUIR_QUIESCE);
	return 0;
out_err:
	return tbcpm;
4872 4873 4874
}

static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
4875 4876
				 struct subchannel_id sch_id,
				 struct dasd_cuir_message *cuir)
4877 4878 4879 4880
{
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_eckd_private *private;
	struct dasd_device *dev, *n;
4881 4882
	unsigned long paths = 0;
	int tbcpm;
4883 4884 4885 4886 4887 4888 4889 4890 4891

	private = (struct dasd_eckd_private *) device->private;
	/*
	 * the path may have been added through a generic path event before
	 * only trigger path verification if the path is not already in use
	 */
	list_for_each_entry_safe(dev, n,
				 &private->lcu->active_devices,
				 alias_list) {
4892 4893 4894 4895
		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
		paths |= tbcpm;
		if (!(dev->path_data.opm & tbcpm)) {
			dev->path_data.tbvpm |= tbcpm;
4896 4897 4898 4899 4900 4901
			dasd_schedule_device_bh(dev);
		}
	}
	list_for_each_entry_safe(dev, n,
				 &private->lcu->inactive_devices,
				 alias_list) {
4902 4903 4904 4905
		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
		paths |= tbcpm;
		if (!(dev->path_data.opm & tbcpm)) {
			dev->path_data.tbvpm |= tbcpm;
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915
			dasd_schedule_device_bh(dev);
		}
	}
	/* devices in PAV groups */
	list_for_each_entry_safe(pavgroup, tempgroup,
				 &private->lcu->grouplist,
				 group) {
		list_for_each_entry_safe(dev, n,
					 &pavgroup->baselist,
					 alias_list) {
4916 4917 4918 4919
			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
			paths |= tbcpm;
			if (!(dev->path_data.opm & tbcpm)) {
				dev->path_data.tbvpm |= tbcpm;
4920 4921 4922 4923 4924 4925
				dasd_schedule_device_bh(dev);
			}
		}
		list_for_each_entry_safe(dev, n,
					 &pavgroup->aliaslist,
					 alias_list) {
4926 4927 4928 4929
			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
			paths |= tbcpm;
			if (!(dev->path_data.opm & tbcpm)) {
				dev->path_data.tbvpm |= tbcpm;
4930 4931 4932 4933
				dasd_schedule_device_bh(dev);
			}
		}
	}
4934 4935 4936
	/* notify user about all paths affected by CUIR action */
	dasd_eckd_cuir_notify_user(device, paths, sch_id, CUIR_RESUME);
	return 0;
4937 4938 4939 4940 4941 4942 4943 4944 4945 4946
}

static void dasd_eckd_handle_cuir(struct dasd_device *device, void *messages,
				 __u8 lpum)
{
	struct dasd_cuir_message *cuir = messages;
	struct channel_path_desc *desc;
	struct subchannel_id sch_id;
	int pos, response;

4947 4948 4949 4950 4951
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "CUIR request: %016llx %016llx %016llx %08x",
		      ((u64 *)cuir)[0], ((u64 *)cuir)[1], ((u64 *)cuir)[2],
		      ((u32 *)cuir)[3]);
	ccw_device_get_schid(device->cdev, &sch_id);
4952
	pos = pathmask_to_pos(lpum);
4953 4954 4955 4956
	desc = ccw_device_get_chp_desc(device->cdev, pos);

	if (cuir->code == CUIR_QUIESCE) {
		/* quiesce */
4957 4958 4959 4960
		if (dasd_eckd_cuir_quiesce(device, lpum, sch_id, cuir))
			response = PSF_CUIR_LAST_PATH;
		else
			response = PSF_CUIR_COMPLETED;
4961 4962
	} else if (cuir->code == CUIR_RESUME) {
		/* resume */
4963 4964
		dasd_eckd_cuir_resume(device, lpum, sch_id, cuir);
		response = PSF_CUIR_COMPLETED;
4965 4966 4967
	} else
		response = PSF_CUIR_NOT_SUPPORTED;

4968 4969 4970 4971 4972
	dasd_eckd_psf_cuir_response(device, response,
				    cuir->message_id, desc, sch_id);
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "CUIR response: %d on message ID %08x", response,
		      cuir->message_id);
4973 4974
	/* free descriptor copy */
	kfree(desc);
4975 4976
	/* to make sure there is no attention left schedule work again */
	device->discipline->check_attention(device, lpum);
4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020
}

static void dasd_eckd_check_attention_work(struct work_struct *work)
{
	struct check_attention_work_data *data;
	struct dasd_rssd_messages *messages;
	struct dasd_device *device;
	int rc;

	data = container_of(work, struct check_attention_work_data, worker);
	device = data->device;
	messages = kzalloc(sizeof(*messages), GFP_KERNEL);
	if (!messages) {
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "Could not allocate attention message buffer");
		goto out;
	}
	rc = dasd_eckd_read_message_buffer(device, messages, data->lpum);
	if (rc)
		goto out;
	if (messages->length == ATTENTION_LENGTH_CUIR &&
	    messages->format == ATTENTION_FORMAT_CUIR)
		dasd_eckd_handle_cuir(device, messages, data->lpum);
out:
	dasd_put_device(device);
	kfree(messages);
	kfree(data);
}

static int dasd_eckd_check_attention(struct dasd_device *device, __u8 lpum)
{
	struct check_attention_work_data *data;

	data = kzalloc(sizeof(*data), GFP_ATOMIC);
	if (!data)
		return -ENOMEM;
	INIT_WORK(&data->worker, dasd_eckd_check_attention_work);
	dasd_get_device(device);
	data->device = device;
	data->lpum = lpum;
	schedule_work(&data->worker);
	return 0;
}

5021
static struct ccw_driver dasd_eckd_driver = {
5022 5023 5024 5025
	.driver = {
		.name	= "dasd-eckd",
		.owner	= THIS_MODULE,
	},
5026 5027 5028 5029 5030 5031
	.ids	     = dasd_eckd_ids,
	.probe	     = dasd_eckd_probe,
	.remove      = dasd_generic_remove,
	.set_offline = dasd_generic_set_offline,
	.set_online  = dasd_eckd_set_online,
	.notify      = dasd_generic_notify,
5032
	.path_event  = dasd_generic_path_event,
5033
	.shutdown    = dasd_generic_shutdown,
5034 5035 5036
	.freeze      = dasd_generic_pm_freeze,
	.thaw	     = dasd_generic_restore_device,
	.restore     = dasd_generic_restore_device,
5037
	.uc_handler  = dasd_generic_uc_handler,
5038
	.int_class   = IRQIO_DAS,
5039
};
5040

L
Linus Torvalds 已提交
5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057
/*
 * max_blocks is dependent on the amount of storage that is available
 * in the static io buffer for each device. Currently each device has
 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
 * addition we have one define extent ccw + 16 bytes of data and one
 * locate record ccw + 16 bytes of data. That makes:
 * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
 * We want to fit two into the available memory so that we can immediately
 * start the next request if one finishes off. That makes 249.5 blocks
 * for one request. Give a little safety and the result is 240.
 */
static struct dasd_discipline dasd_eckd_discipline = {
	.owner = THIS_MODULE,
	.name = "ECKD",
	.ebcname = "ECKD",
5058
	.max_blocks = 190,
L
Linus Torvalds 已提交
5059
	.check_device = dasd_eckd_check_characteristics,
5060
	.uncheck_device = dasd_eckd_uncheck_device,
L
Linus Torvalds 已提交
5061
	.do_analysis = dasd_eckd_do_analysis,
5062
	.verify_path = dasd_eckd_verify_path,
5063
	.basic_to_ready = dasd_eckd_basic_to_ready,
5064
	.online_to_ready = dasd_eckd_online_to_ready,
5065
	.basic_to_known = dasd_eckd_basic_to_known,
L
Linus Torvalds 已提交
5066 5067 5068
	.fill_geometry = dasd_eckd_fill_geometry,
	.start_IO = dasd_start_IO,
	.term_IO = dasd_term_IO,
5069
	.handle_terminated_request = dasd_eckd_handle_terminated_request,
L
Linus Torvalds 已提交
5070 5071 5072
	.format_device = dasd_eckd_format_device,
	.erp_action = dasd_eckd_erp_action,
	.erp_postaction = dasd_eckd_erp_postaction,
5073
	.check_for_device_change = dasd_eckd_check_for_device_change,
5074 5075
	.build_cp = dasd_eckd_build_alias_cp,
	.free_cp = dasd_eckd_free_alias_cp,
L
Linus Torvalds 已提交
5076
	.dump_sense = dasd_eckd_dump_sense,
S
Stefan Haberland 已提交
5077
	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
L
Linus Torvalds 已提交
5078
	.fill_info = dasd_eckd_fill_info,
5079
	.ioctl = dasd_eckd_ioctl,
5080 5081
	.freeze = dasd_eckd_pm_freeze,
	.restore = dasd_eckd_restore_device,
5082
	.reload = dasd_eckd_reload_device,
5083
	.get_uid = dasd_eckd_get_uid,
5084
	.kick_validate = dasd_eckd_kick_validate_server,
5085
	.check_attention = dasd_eckd_check_attention,
L
Linus Torvalds 已提交
5086 5087 5088 5089 5090
};

static int __init
dasd_eckd_init(void)
{
5091 5092
	int ret;

L
Linus Torvalds 已提交
5093
	ASCEBC(dasd_eckd_discipline.ebcname, 4);
5094 5095 5096 5097
	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
				   GFP_KERNEL | GFP_DMA);
	if (!dasd_reserve_req)
		return -ENOMEM;
5098 5099 5100 5101 5102 5103
	path_verification_worker = kmalloc(sizeof(*path_verification_worker),
				   GFP_KERNEL | GFP_DMA);
	if (!path_verification_worker) {
		kfree(dasd_reserve_req);
		return -ENOMEM;
	}
5104 5105 5106 5107 5108 5109
	rawpadpage = (void *)__get_free_page(GFP_KERNEL);
	if (!rawpadpage) {
		kfree(path_verification_worker);
		kfree(dasd_reserve_req);
		return -ENOMEM;
	}
5110 5111 5112
	ret = ccw_driver_register(&dasd_eckd_driver);
	if (!ret)
		wait_for_device_probe();
5113 5114
	else {
		kfree(path_verification_worker);
5115
		kfree(dasd_reserve_req);
5116
		free_page((unsigned long)rawpadpage);
5117
	}
5118
	return ret;
L
Linus Torvalds 已提交
5119 5120 5121 5122 5123 5124
}

static void __exit
dasd_eckd_cleanup(void)
{
	ccw_driver_unregister(&dasd_eckd_driver);
5125
	kfree(path_verification_worker);
5126
	kfree(dasd_reserve_req);
5127
	free_page((unsigned long)rawpadpage);
L
Linus Torvalds 已提交
5128 5129 5130 5131
}

module_init(dasd_eckd_init);
module_exit(dasd_eckd_cleanup);