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

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

L
Linus Torvalds 已提交
15 16 17 18 19 20
#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>
21
#include <linux/compat.h>
L
Linus Torvalds 已提交
22
#include <linux/init.h>
23
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
24

S
Sebastian Ott 已提交
25
#include <asm/css_chars.h>
L
Linus Torvalds 已提交
26 27 28 29
#include <asm/debug.h>
#include <asm/idals.h>
#include <asm/ebcdic.h>
#include <asm/io.h>
30
#include <linux/uaccess.h>
31
#include <asm/cio.h>
L
Linus Torvalds 已提交
32
#include <asm/ccwdev.h>
33
#include <asm/itcw.h>
34 35
#include <asm/schid.h>
#include <asm/chpid.h>
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44

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

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

45 46 47 48 49 50 51 52 53
/*
 * 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 已提交
54 55 56 57 58 59 60
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 已提交
61 62
	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
63
	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
H
Heiko Carstens 已提交
64 65 66 67 68 69 70
	{ 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 已提交
71 72 73 74 75 76 77
	{ /* end of list */ },
};

MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);

static struct ccw_driver dasd_eckd_driver; /* see below */

78 79
static void *rawpadpage;

80 81 82 83
#define INIT_CQR_OK 0
#define INIT_CQR_UNFORMATTED 1
#define INIT_CQR_ERROR 2

84 85 86 87 88 89 90 91
/* 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);

92 93 94 95 96 97 98 99 100 101 102 103 104
static struct {
	struct dasd_ccw_req cqr;
	struct ccw1 ccw[2];
	char data[40];
} *dasd_vol_info_req;
static DEFINE_MUTEX(dasd_vol_info_mutex);

struct ext_pool_exhaust_work_data {
	struct work_struct worker;
	struct dasd_device *device;
	struct dasd_device *base;
};

105 106 107 108 109 110 111 112 113 114 115 116
/* 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);
117

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

124
static int dasd_eckd_ext_pool_id(struct dasd_device *);
125 126 127 128 129
static int prepare_itcw(struct itcw *, unsigned int, unsigned int, int,
			struct dasd_device *, struct dasd_device *,
			unsigned int, int, unsigned int, unsigned int,
			unsigned int, unsigned int);

L
Linus Torvalds 已提交
130 131 132 133 134 135 136
/* 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;

137
	/* set ECKD specific ccw-device options */
138 139
	ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
				     CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
140
	if (ret) {
141 142 143
		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
				"dasd_eckd_probe: could not set "
				"ccw-device options");
L
Linus Torvalds 已提交
144
		return ret;
145
	}
146
	ret = dasd_generic_probe(cdev);
147
	return ret;
L
Linus Torvalds 已提交
148 149 150 151 152
}

static int
dasd_eckd_set_online(struct ccw_device *cdev)
{
153
	return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
L
Linus Torvalds 已提交
154 155 156 157 158
}

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

159
/* head and record addresses of count_area read in analysis ccw */
160
static const int count_area_head[] = { 0, 0, 0, 0, 1 };
161 162
static const int count_area_rec[] = { 1, 2, 3, 4, 1 };

L
Linus Torvalds 已提交
163 164 165 166 167 168
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/*
 * calculate failing track from sense data depending if
 * it is an EAV device or not
 */
static int dasd_eckd_track_from_irb(struct irb *irb, struct dasd_device *device,
				    sector_t *track)
{
	struct dasd_eckd_private *private = device->private;
	u8 *sense = NULL;
	u32 cyl;
	u8 head;

	sense = dasd_get_sense(irb);
	if (!sense) {
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "ESE error no sense data\n");
		return -EINVAL;
	}
	if (!(sense[27] & DASD_SENSE_BIT_2)) {
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "ESE error no valid track data\n");
		return -EINVAL;
	}

	if (sense[27] & DASD_SENSE_BIT_3) {
		/* enhanced addressing */
		cyl = sense[30] << 20;
		cyl |= (sense[31] & 0xF0) << 12;
		cyl |= sense[28] << 8;
		cyl |= sense[29];
	} else {
		cyl = sense[29] << 8;
		cyl |= sense[30];
	}
	head = sense[31] & 0x0F;
	*track = cyl * private->rdc_data.trk_per_cyl + head;
	return 0;
}

249
static int set_timestamp(struct ccw1 *ccw, struct DE_eckd_data *data,
250
		     struct dasd_device *device)
L
Linus Torvalds 已提交
251
{
252
	struct dasd_eckd_private *private = device->private;
M
Martin Schwidefsky 已提交
253
	int rc;
L
Linus Torvalds 已提交
254

255 256 257 258 259 260 261
	rc = get_phys_clock(&data->ep_sys_time);
	/*
	 * Ignore return code if XRC is not supported or
	 * sync clock is switched off
	 */
	if ((rc && !private->rdc_data.facilities.XRC_supported) ||
	    rc == -EOPNOTSUPP || rc == -EACCES)
M
Martin Schwidefsky 已提交
262
		return 0;
L
Linus Torvalds 已提交
263

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

268 269 270 271 272
	if (ccw) {
		ccw->count = sizeof(struct DE_eckd_data);
		ccw->flags |= CCW_FLAG_SLI;
	}

M
Martin Schwidefsky 已提交
273 274
	return rc;
}
L
Linus Torvalds 已提交
275

276
static int
277
define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
278 279
	      unsigned int totrk, int cmd, struct dasd_device *device,
	      int blksize)
L
Linus Torvalds 已提交
280
{
281
	struct dasd_eckd_private *private = device->private;
282
	u16 heads, beghead, endhead;
283
	u32 begcyl, endcyl;
M
Martin Schwidefsky 已提交
284
	int rc = 0;
L
Linus Torvalds 已提交
285

286 287 288 289 290 291
	if (ccw) {
		ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
		ccw->flags = 0;
		ccw->count = 16;
		ccw->cda = (__u32)__pa(data);
	}
L
Linus Torvalds 已提交
292

293
	memset(data, 0, sizeof(struct DE_eckd_data));
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305
	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:
		data->mask.perm = 0x1;
		data->attributes.operation = private->attrib.operation;
		break;
306 307 308 309
	case DASD_ECKD_CCW_READ_COUNT:
		data->mask.perm = 0x1;
		data->attributes.operation = DASD_BYPASS_CACHE;
		break;
310 311 312 313 314 315
	case DASD_ECKD_CCW_READ_TRACK:
	case DASD_ECKD_CCW_READ_TRACK_DATA:
		data->mask.perm = 0x1;
		data->attributes.operation = private->attrib.operation;
		data->blk_size = 0;
		break;
L
Linus Torvalds 已提交
316 317 318 319 320 321
	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;
322
		rc = set_timestamp(ccw, data, device);
L
Linus Torvalds 已提交
323 324 325 326
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->attributes.operation = DASD_BYPASS_CACHE;
327
		rc = set_timestamp(ccw, data, device);
L
Linus Torvalds 已提交
328 329 330 331 332 333 334
		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;
335
		rc = set_timestamp(ccw, data, device);
336 337 338 339 340 341 342 343 344 345
		break;
	case DASD_ECKD_CCW_WRITE_FULL_TRACK:
		data->mask.perm = 0x03;
		data->attributes.operation = private->attrib.operation;
		data->blk_size = 0;
		break;
	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
		data->mask.perm = 0x02;
		data->attributes.operation = private->attrib.operation;
		data->blk_size = blksize;
346
		rc = set_timestamp(ccw, data, device);
L
Linus Torvalds 已提交
347 348
		break;
	default:
S
Stefan Haberland 已提交
349 350
		dev_err(&device->cdev->dev,
			"0x%x is not a known command\n", cmd);
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361
		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 */

362 363 364 365 366
	heads = private->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;
L
Linus Torvalds 已提交
367 368 369 370

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

372 373
		if (endcyl + private->attrib.nr_cyl < private->real_cyl)
			endcyl += private->attrib.nr_cyl;
L
Linus Torvalds 已提交
374
		else
375
			endcyl = (private->real_cyl - 1);
L
Linus Torvalds 已提交
376 377
	}

378 379
	set_ch_t(&data->beg_ext, begcyl, beghead);
	set_ch_t(&data->end_ext, endcyl, endhead);
M
Martin Schwidefsky 已提交
380
	return rc;
L
Linus Torvalds 已提交
381 382
}

383

384 385 386 387
static void locate_record_ext(struct ccw1 *ccw, 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)
388
{
389
	struct dasd_eckd_private *private = device->private;
390 391 392
	int sector;
	int dn, d;

393 394 395 396 397 398 399 400 401 402
	if (ccw) {
		ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD_EXT;
		ccw->flags = 0;
		if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK)
			ccw->count = 22;
		else
			ccw->count = 20;
		ccw->cda = (__u32)__pa(data);
	}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	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;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	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;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	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;
497 498 499 500 501 502
	case DASD_ECKD_CCW_READ_TRACK:
		data->operation.orientation = 0x1;
		data->operation.operation = 0x0C;
		data->extended_parameter_length = 0;
		data->sector = 0xFF;
		break;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	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,
529
		      unsigned int format, unsigned int rec_on_trk, int count,
530
		      unsigned int blksize, unsigned int tlf)
531 532
{
	struct dasd_eckd_private *basepriv, *startpriv;
533
	struct LRE_eckd_data *lredata;
534
	struct DE_eckd_data *dedata;
535 536
	int rc = 0;

537 538
	basepriv = basedev->private;
	startpriv = startdev->private;
539 540
	dedata = &pfxdata->define_extent;
	lredata = &pfxdata->locate_record;
541 542 543

	ccw->cmd_code = DASD_ECKD_CCW_PFX;
	ccw->flags = 0;
544 545 546 547 548 549 550 551 552
	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));
	}
553 554

	/* prefix data */
555 556 557 558 559 560 561
	if (format > 1) {
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "PFX LRE unknown format 0x%x", format);
		BUG();
		return -EINVAL;
	}
	pfxdata->format = format;
562 563
	pfxdata->base_address = basepriv->ned->unit_addr;
	pfxdata->base_lss = basepriv->ned->ID;
564
	pfxdata->validity.define_extent = 1;
565 566

	/* private uid is kept up to date, conf_data may be outdated */
567
	if (startpriv->uid.type == UA_BASE_PAV_ALIAS)
568
		pfxdata->validity.verify_base = 1;
569 570 571 572

	if (startpriv->uid.type == UA_HYPER_PAV_ALIAS) {
		pfxdata->validity.verify_base = 1;
		pfxdata->validity.hyper_pav = 1;
573 574
	}

575
	rc = define_extent(NULL, dedata, trk, totrk, cmd, basedev, blksize);
576

577 578 579 580 581 582 583
	/*
	 * For some commands the System Time Stamp is set in the define extent
	 * data when XRC is supported. The validity of the time stamp must be
	 * reflected in the prefix data as well.
	 */
	if (dedata->ga_extended & 0x08 && dedata->ga_extended & 0x02)
		pfxdata->validity.time_stamp = 1; /* 'Time Stamp Valid'   */
584 585

	if (format == 1) {
586 587
		locate_record_ext(NULL, lredata, trk, rec_on_trk, count, cmd,
				  basedev, blksize, tlf);
588 589
	}

590 591 592
	return rc;
}

593 594 595 596 597 598 599 600
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);
}

601
static void
602 603
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 已提交
604 605
	      struct dasd_device * device, int reclen)
{
606
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
607 608
	int sector;
	int dn, d;
609

L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618
	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);

619
	memset(data, 0, sizeof(struct LO_eckd_data));
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 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
	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 已提交
692 693
		DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
			      "opcode 0x%x", cmd);
L
Linus Torvalds 已提交
694
	}
695 696 697 698 699
	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 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	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;
}
737 738
/* create unique id from private structure. */
static void create_uid(struct dasd_eckd_private *private)
739
{
740
	int count;
741
	struct dasd_uid *uid;
742

743
	uid = &private->uid;
744
	memset(uid, 0, sizeof(struct dasd_uid));
745
	memcpy(uid->vendor, private->ned->HDA_manufacturer,
746
	       sizeof(uid->vendor) - 1);
747
	EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
748
	memcpy(uid->serial, private->ned->HDA_location,
749
	       sizeof(uid->serial) - 1);
750
	EBCASC(uid->serial, sizeof(uid->serial) - 1);
751
	uid->ssid = private->gneq->subsystemID;
752
	uid->real_unit_addr = private->ned->unit_addr;
753 754
	if (private->sneq) {
		uid->type = private->sneq->sua_flags;
755
		if (uid->type == UA_BASE_PAV_ALIAS)
756
			uid->base_unit_addr = private->sneq->base_unit_addr;
757 758 759
	} else {
		uid->type = UA_BASE_DEVICE;
	}
760 761 762 763 764 765
	if (private->vdsneq) {
		for (count = 0; count < 16; count++) {
			sprintf(uid->vduit+2*count, "%02x",
				private->vdsneq->uit[count]);
		}
	}
766 767 768 769 770 771 772
}

/*
 * Generate device unique id that specifies the physical device.
 */
static int dasd_eckd_generate_uid(struct dasd_device *device)
{
773
	struct dasd_eckd_private *private = device->private;
774 775 776 777 778 779 780 781
	unsigned long flags;

	if (!private)
		return -ENODEV;
	if (!private->ned || !private->gneq)
		return -ENODEV;
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
	create_uid(private);
782
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
783 784 785
	return 0;
}

786 787
static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
{
788
	struct dasd_eckd_private *private = device->private;
789 790
	unsigned long flags;

791
	if (private) {
792 793 794 795 796 797 798 799
		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;
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
/*
 * 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));
}

815 816 817 818
static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
				   struct dasd_ccw_req *cqr,
				   __u8 *rcd_buffer,
				   __u8 lpm)
819 820
{
	struct ccw1 *ccw;
821 822 823 824 825 826 827 828
	/*
	 * 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;
829 830

	ccw = cqr->cpaddr;
831 832
	ccw->cmd_code = DASD_ECKD_CCW_RCD;
	ccw->flags = 0;
833
	ccw->cda = (__u32)(addr_t)rcd_buffer;
834 835
	ccw->count = DASD_ECKD_RCD_DATA_SIZE;
	cqr->magic = DASD_ECKD_MAGIC;
836

837 838 839
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
840 841
	cqr->expires = 10*HZ;
	cqr->lpm = lpm;
842
	cqr->retries = 256;
843
	cqr->buildclk = get_tod_clock();
844
	cqr->status = DASD_CQR_FILLED;
845 846 847
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/*
 * 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);
}

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
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);
889
	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
890
	cqr->retries = 5;
891
	cqr->callback = read_conf_cb;
892 893
	rc = dasd_sleep_on_immediatly(cqr);
	return rc;
894 895 896 897 898 899 900 901 902 903 904 905
}

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;

	/*
906 907
	 * sanity check: scan for RCD command in extended SenseID data
	 * some devices do not support RCD
908 909
	 */
	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
910
	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
911 912 913
		ret = -EOPNOTSUPP;
		goto out_error;
	}
914
	rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
915 916 917 918
	if (!rcd_buf) {
		ret = -ENOMEM;
		goto out_error;
	}
919 920
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
				   0, /* use rcd_buf as data ara */
921
				   device, NULL);
922
	if (IS_ERR(cqr)) {
923 924 925
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "Could not allocate RCD request");
		ret = -ENOMEM;
926 927
		goto out_error;
	}
928
	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
929
	cqr->callback = read_conf_cb;
930 931 932 933
	ret = dasd_sleep_on(cqr);
	/*
	 * on success we update the user input parms
	 */
934
	dasd_sfree_request(cqr, cqr->memdev);
935 936 937
	if (ret)
		goto out_error;

938
	*rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
939 940 941 942 943 944 945 946 947
	*rcd_buffer = rcd_buf;
	return 0;
out_error:
	kfree(rcd_buf);
	*rcd_buffer = NULL;
	*rcd_buffer_size = 0;
	return ret;
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
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;
}

1003 1004 1005 1006 1007 1008 1009
static void dasd_eckd_store_conf_data(struct dasd_device *device,
				      struct dasd_conf_data *conf_data, int chp)
{
	struct channel_path_desc_fmt0 *chp_desc;
	struct subchannel_id sch_id;

	ccw_device_get_schid(device->cdev, &sch_id);
1010 1011 1012 1013 1014
	/*
	 * path handling and read_conf allocate data
	 * free it before replacing the pointer
	 */
	kfree(device->path[chp].conf_data);
1015 1016 1017 1018 1019 1020 1021 1022 1023
	device->path[chp].conf_data = conf_data;
	device->path[chp].cssid = sch_id.cssid;
	device->path[chp].ssid = sch_id.ssid;
	chp_desc = ccw_device_get_chp_desc(device->cdev, chp);
	if (chp_desc)
		device->path[chp].chpid = chp_desc->chpid;
	kfree(chp_desc);
}

1024 1025
static void dasd_eckd_clear_conf_data(struct dasd_device *device)
{
1026
	struct dasd_eckd_private *private = device->private;
1027 1028 1029 1030 1031
	int i;

	private->conf_data = NULL;
	private->conf_len = 0;
	for (i = 0; i < 8; i++) {
1032 1033
		kfree(device->path[i].conf_data);
		device->path[i].conf_data = NULL;
1034 1035 1036
		device->path[i].cssid = 0;
		device->path[i].ssid = 0;
		device->path[i].chpid = 0;
1037 1038 1039
	}
}

1040
static int dasd_eckd_read_conf(struct dasd_device *device)
L
Linus Torvalds 已提交
1041 1042 1043
{
	void *conf_data;
	int conf_len, conf_data_saved;
1044
	int rc, path_err, pos;
1045
	__u8 lpm, opm;
1046 1047 1048
	struct dasd_eckd_private *private, path_private;
	struct dasd_uid *uid;
	char print_path_uid[60], print_device_uid[60];
L
Linus Torvalds 已提交
1049

1050
	private = device->private;
1051
	opm = ccw_device_get_path_mask(device->cdev);
L
Linus Torvalds 已提交
1052
	conf_data_saved = 0;
1053
	path_err = 0;
L
Linus Torvalds 已提交
1054 1055
	/* get configuration data per operational path */
	for (lpm = 0x80; lpm; lpm>>= 1) {
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		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 */
1071
			dasd_path_add_opm(device, opm);
1072 1073 1074 1075
			continue;	/* no error */
		}
		/* save first valid configuration data */
		if (!conf_data_saved) {
1076 1077
			/* initially clear previously stored conf_data */
			dasd_eckd_clear_conf_data(device);
1078 1079 1080 1081 1082 1083 1084
			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 已提交
1085
			}
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
			/*
			 * 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 已提交
1101
			}
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
			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);
1139
				path_err = -EINVAL;
1140
				dasd_path_add_cablepm(device, lpm);
1141
				continue;
L
Linus Torvalds 已提交
1142
			}
1143 1144 1145
			path_private.conf_data = NULL;
			path_private.conf_len = 0;
		}
1146 1147 1148 1149

		pos = pathmask_to_pos(lpm);
		dasd_eckd_store_conf_data(device, conf_data, pos);

1150 1151
		switch (dasd_eckd_path_access(conf_data, conf_len)) {
		case 0x02:
1152
			dasd_path_add_nppm(device, lpm);
1153 1154
			break;
		case 0x03:
1155
			dasd_path_add_ppm(device, lpm);
1156
			break;
L
Linus Torvalds 已提交
1157
		}
1158 1159
		if (!dasd_path_get_opm(device)) {
			dasd_path_set_opm(device, lpm);
1160 1161
			dasd_generic_path_operational(device);
		} else {
1162
			dasd_path_add_opm(device, lpm);
1163
		}
L
Linus Torvalds 已提交
1164
	}
1165

1166
	return path_err;
L
Linus Torvalds 已提交
1167 1168
}

1169 1170 1171 1172
static u32 get_fcx_max_data(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	int fcx_in_css, fcx_in_gneq, fcx_in_features;
1173 1174
	unsigned int mdc;
	int tpm;
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

	if (dasd_nofcx)
		return 0;
	/* is transport mode supported? */
	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);
1188
	if (mdc == 0) {
1189 1190 1191 1192 1193 1194 1195
		dev_warn(&device->cdev->dev, "Detecting the maximum supported data size for zHPF requests failed\n");
		return 0;
	} else {
		return (u32)mdc * FCX_MAX_DATA_FACTOR;
	}
}

1196 1197
static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
{
1198
	struct dasd_eckd_private *private = device->private;
1199
	unsigned int mdc;
1200 1201 1202 1203
	u32 fcx_max_data;

	if (private->fcx_max_data) {
		mdc = ccw_device_get_mdc(device->cdev, lpm);
1204
		if (mdc == 0) {
1205 1206 1207 1208 1209 1210
			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;
		}
1211
		fcx_max_data = (u32)mdc * FCX_MAX_DATA_FACTOR;
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
		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;
}

1224 1225 1226
static int rebuild_device_uid(struct dasd_device *device,
			      struct path_verification_work_data *data)
{
1227
	struct dasd_eckd_private *private = device->private;
1228
	__u8 lpm, opm = dasd_path_get_opm(device);
1229
	int rc = -ENODEV;
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 1261 1262

	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;
}

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

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

1279 1280 1281 1282 1283
	/* delay path verification until device was resumed */
	if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
		schedule_work(work);
		return;
	}
1284 1285 1286 1287 1288
	/* 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;
	}
1289 1290 1291 1292
	opm = 0;
	npm = 0;
	ppm = 0;
	epm = 0;
1293 1294 1295
	hpfpm = 0;
	cablepm = 0;

1296
	for (lpm = 0x80; lpm; lpm >>= 1) {
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
		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",
1324 1325
					"path verification: device is stopped,"
					" try again later");
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
			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;
1337
			hpfpm |= lpm;
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
			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
		 */
1362
		if (dasd_path_get_opm(device) &&
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 1393 1394 1395
		    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);
1396 1397 1398
				opm &= ~lpm;
				npm &= ~lpm;
				ppm &= ~lpm;
1399
				cablepm |= lpm;
1400
				continue;
1401 1402
			}
		}
1403

1404 1405 1406 1407 1408 1409 1410 1411
		conf_data = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL);
		if (conf_data) {
			memcpy(conf_data, data->rcd_buffer,
			       DASD_ECKD_RCD_DATA_SIZE);
		}
		pos = pathmask_to_pos(lpm);
		dasd_eckd_store_conf_data(device, conf_data, pos);

1412 1413 1414 1415 1416 1417 1418 1419
		/*
		 * 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);
1420 1421
		if (!dasd_path_get_opm(device) && opm) {
			dasd_path_set_opm(device, opm);
1422
			dasd_generic_path_operational(device);
1423
		} else {
1424
			dasd_path_add_opm(device, opm);
1425
		}
1426 1427 1428 1429 1430
		dasd_path_add_nppm(device, npm);
		dasd_path_add_ppm(device, ppm);
		dasd_path_add_tbvpm(device, epm);
		dasd_path_add_cablepm(device, cablepm);
		dasd_path_add_nohpfpm(device, hpfpm);
1431
		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1432
	}
1433
	clear_bit(DASD_FLAG_PATH_VERIFY, &device->flags);
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 1459 1460 1461 1462 1463
	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;
}

1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
static void dasd_eckd_reset_path(struct dasd_device *device, __u8 pm)
{
	struct dasd_eckd_private *private = device->private;
	unsigned long flags;

	if (!private->fcx_max_data)
		private->fcx_max_data = get_fcx_max_data(device);
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
	dasd_path_set_tbvpm(device, pm ? : dasd_path_get_notoperpm(device));
	dasd_schedule_device_bh(device);
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
}

1477 1478
static int dasd_eckd_read_features(struct dasd_device *device)
{
1479
	struct dasd_eckd_private *private = device->private;
1480 1481 1482 1483 1484 1485
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_features *features;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

1486
	memset(&private->features, 0, sizeof(struct dasd_rssd_features));
1487
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
1488 1489
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_features)),
1490
				   device, NULL);
1491
	if (IS_ERR(cqr)) {
1492 1493
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
				"allocate initialization request");
1494 1495 1496 1497 1498
		return PTR_ERR(cqr);
	}
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
1499
	cqr->retries = 256;
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	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;

1524
	cqr->buildclk = get_tod_clock();
1525 1526 1527 1528 1529 1530 1531
	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));
1532 1533 1534
	} else
		dev_warn(&device->cdev->dev, "Reading device feature codes"
			 " failed with rc=%d\n", rc);
1535 1536 1537 1538
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}

1539 1540 1541 1542 1543 1544 1545 1546
/* Read Volume Information - Volume Storage Query */
static int dasd_eckd_read_vol_info(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_vsq *vsq;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
1547
	int useglobal;
1548 1549 1550 1551 1552 1553 1554
	int rc;

	/* This command cannot be executed on an alias device */
	if (private->uid.type == UA_BASE_PAV_ALIAS ||
	    private->uid.type == UA_HYPER_PAV_ALIAS)
		return 0;

1555
	useglobal = 0;
1556 1557 1558 1559 1560
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 /* PSF + RSSD */,
				   sizeof(*prssdp) + sizeof(*vsq), device, NULL);
	if (IS_ERR(cqr)) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate initialization request");
1561 1562 1563 1564 1565 1566 1567 1568
		mutex_lock(&dasd_vol_info_mutex);
		useglobal = 1;
		cqr = &dasd_vol_info_req->cqr;
		memset(cqr, 0, sizeof(*cqr));
		memset(dasd_vol_info_req, 0, sizeof(*dasd_vol_info_req));
		cqr->cpaddr = &dasd_vol_info_req->ccw;
		cqr->data = &dasd_vol_info_req->data;
		cqr->magic = DASD_ECKD_MAGIC;
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	}

	/* Prepare for Read Subsystem Data */
	prssdp = cqr->data;
	prssdp->order = PSF_ORDER_PRSSD;
	prssdp->suborder = PSF_SUBORDER_VSQ;	/* Volume Storage Query */
	prssdp->lss = private->ned->ID;
	prssdp->volume = private->ned->unit_addr;

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

	/* Read Subsystem Data - Volume Storage Query */
	vsq = (struct dasd_rssd_vsq *)(prssdp + 1);
	memset(vsq, 0, sizeof(*vsq));

	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
	ccw->count = sizeof(*vsq);
	ccw->flags |= CCW_FLAG_SLI;
	ccw->cda = (__u32)(addr_t)vsq;

	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->retries = 256;
	cqr->expires = device->default_expires * HZ;
	/* The command might not be supported. Suppress the error output */
	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);

	rc = dasd_sleep_on_interruptible(cqr);
	if (rc == 0) {
		memcpy(&private->vsq, vsq, sizeof(*vsq));
	} else {
1608 1609
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Reading the volume storage information failed with rc=%d", rc);
1610 1611
	}

1612 1613 1614 1615
	if (useglobal)
		mutex_unlock(&dasd_vol_info_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669

	return rc;
}

static int dasd_eckd_is_ese(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->vsq.vol_info.ese;
}

static int dasd_eckd_ext_pool_id(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->vsq.extent_pool_id;
}

/*
 * This value represents the total amount of available space. As more space is
 * allocated by ESE volumes, this value will decrease.
 * The data for this value is therefore updated on any call.
 */
static int dasd_eckd_space_configured(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	int rc;

	rc = dasd_eckd_read_vol_info(device);

	return rc ? : private->vsq.space_configured;
}

/*
 * The value of space allocated by an ESE volume may have changed and is
 * therefore updated on any call.
 */
static int dasd_eckd_space_allocated(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	int rc;

	rc = dasd_eckd_read_vol_info(device);

	return rc ? : private->vsq.space_allocated;
}

static int dasd_eckd_logical_capacity(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->vsq.logical_capacity;
}

1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
static void dasd_eckd_ext_pool_exhaust_work(struct work_struct *work)
{
	struct ext_pool_exhaust_work_data *data;
	struct dasd_device *device;
	struct dasd_device *base;

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

	if (!base)
		base = device;
	if (dasd_eckd_space_configured(base) != 0) {
		dasd_generic_space_avail(device);
	} else {
		dev_warn(&device->cdev->dev, "No space left in the extent pool\n");
		DBF_DEV_EVENT(DBF_WARNING, device, "%s", "out of space");
	}

	dasd_put_device(device);
	kfree(data);
}

static int dasd_eckd_ext_pool_exhaust(struct dasd_device *device,
				      struct dasd_ccw_req *cqr)
{
	struct ext_pool_exhaust_work_data *data;

	data = kzalloc(sizeof(*data), GFP_ATOMIC);
	if (!data)
		return -ENOMEM;
	INIT_WORK(&data->worker, dasd_eckd_ext_pool_exhaust_work);
	dasd_get_device(device);
	data->device = device;

	if (cqr->block)
		data->base = cqr->block->base;
	else if (cqr->basedev)
		data->base = cqr->basedev;
	else
		data->base = NULL;

	schedule_work(&data->worker);

	return 0;
}

1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
static void dasd_eckd_cpy_ext_pool_data(struct dasd_device *device,
					struct dasd_rssd_lcq *lcq)
{
	struct dasd_eckd_private *private = device->private;
	int pool_id = dasd_eckd_ext_pool_id(device);
	struct dasd_ext_pool_sum eps;
	int i;

	for (i = 0; i < lcq->pool_count; i++) {
		eps = lcq->ext_pool_sum[i];
		if (eps.pool_id == pool_id) {
			memcpy(&private->eps, &eps,
			       sizeof(struct dasd_ext_pool_sum));
		}
	}
}

/* Read Extent Pool Information - Logical Configuration Query */
static int dasd_eckd_read_ext_pool_info(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_lcq *lcq;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

	/* This command cannot be executed on an alias device */
	if (private->uid.type == UA_BASE_PAV_ALIAS ||
	    private->uid.type == UA_HYPER_PAV_ALIAS)
		return 0;

	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 /* PSF + RSSD */,
				   sizeof(*prssdp) + sizeof(*lcq), device, NULL);
	if (IS_ERR(cqr)) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate initialization request");
		return PTR_ERR(cqr);
	}

	/* Prepare for Read Subsystem Data */
	prssdp = cqr->data;
	memset(prssdp, 0, sizeof(*prssdp));
	prssdp->order = PSF_ORDER_PRSSD;
	prssdp->suborder = PSF_SUBORDER_LCQ;	/* Logical Configuration Query */

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

	lcq = (struct dasd_rssd_lcq *)(prssdp + 1);
	memset(lcq, 0, sizeof(*lcq));

	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
	ccw->count = sizeof(*lcq);
	ccw->flags |= CCW_FLAG_SLI;
	ccw->cda = (__u32)(addr_t)lcq;

	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->retries = 256;
	cqr->expires = device->default_expires * HZ;
	/* The command might not be supported. Suppress the error output */
	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);

	rc = dasd_sleep_on_interruptible(cqr);
	if (rc == 0) {
		dasd_eckd_cpy_ext_pool_data(device, lcq);
	} else {
1792 1793
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Reading the logical configuration failed with rc=%d", rc);
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
	}

	dasd_sfree_request(cqr, cqr->memdev);

	return rc;
}

/*
 * Depending on the device type, the extent size is specified either as
 * cylinders per extent (CKD) or size per extent (FBA)
 * A 1GB size corresponds to 1113cyl, and 16MB to 21cyl.
 */
static int dasd_eckd_ext_size(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	struct dasd_ext_pool_sum eps = private->eps;

	if (!eps.flags.extent_size_valid)
		return 0;
	if (eps.extent_size.size_1G)
		return 1113;
	if (eps.extent_size.size_16M)
		return 21;

	return 0;
}

static int dasd_eckd_ext_pool_warn_thrshld(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->eps.warn_thrshld;
}

static int dasd_eckd_ext_pool_cap_at_warnlevel(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->eps.flags.capacity_at_warnlevel;
}

/*
 * Extent Pool out of space
 */
static int dasd_eckd_ext_pool_oos(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->eps.flags.pool_oos;
}
1844

1845 1846 1847
/*
 * Build CP for Perform Subsystem Function - SSC.
 */
1848 1849
static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
						    int enable_pav)
1850
{
1851 1852 1853
	struct dasd_ccw_req *cqr;
	struct dasd_psf_ssc_data *psf_ssc_data;
	struct ccw1 *ccw;
1854

1855
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1856
				  sizeof(struct dasd_psf_ssc_data),
1857
				   device, NULL);
1858

1859
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
1860
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1861
			   "Could not allocate PSF-SSC request");
1862 1863 1864 1865
		return cqr;
	}
	psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
	psf_ssc_data->order = PSF_ORDER_SSC;
1866
	psf_ssc_data->suborder = 0xc0;
1867
	if (enable_pav) {
1868
		psf_ssc_data->suborder |= 0x08;
1869 1870
		psf_ssc_data->reserved[0] = 0x88;
	}
1871 1872 1873 1874 1875 1876 1877 1878
	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;
1879
	cqr->retries = 256;
1880
	cqr->expires = 10*HZ;
1881
	cqr->buildclk = get_tod_clock();
1882 1883
	cqr->status = DASD_CQR_FILLED;
	return cqr;
1884 1885 1886 1887 1888 1889 1890 1891
}

/*
 * 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 已提交
1892 1893
dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
		  unsigned long flags)
1894
{
1895 1896 1897
	struct dasd_ccw_req *cqr;
	int rc;

1898
	cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1899 1900 1901
	if (IS_ERR(cqr))
		return PTR_ERR(cqr);

S
Stefan Haberland 已提交
1902 1903 1904 1905 1906 1907
	/*
	 * set flags e.g. turn on failfast, to prevent blocking
	 * the calling function should handle failed requests
	 */
	cqr->flags |= flags;

1908 1909 1910 1911
	rc = dasd_sleep_on(cqr);
	if (!rc)
		/* trigger CIO to reprobe devices */
		css_schedule_reprobe();
S
Stefan Haberland 已提交
1912 1913 1914
	else if (cqr->intrc == -EAGAIN)
		rc = -EAGAIN;

1915 1916
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
1917 1918 1919 1920 1921
}

/*
 * Valide storage server of current device.
 */
S
Stefan Haberland 已提交
1922 1923
static int dasd_eckd_validate_server(struct dasd_device *device,
				     unsigned long flags)
1924
{
1925 1926
	struct dasd_eckd_private *private = device->private;
	int enable_pav, rc;
1927

1928 1929
	if (private->uid.type == UA_BASE_PAV_ALIAS ||
	    private->uid.type == UA_HYPER_PAV_ALIAS)
S
Stefan Haberland 已提交
1930
		return 0;
1931
	if (dasd_nopav || MACHINE_IS_VM)
1932 1933 1934
		enable_pav = 0;
	else
		enable_pav = 1;
S
Stefan Haberland 已提交
1935
	rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
1936

H
Horst Hummel 已提交
1937 1938
	/* may be requested feature is not available on server,
	 * therefore just report error and go ahead */
1939 1940
	DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
			"returned rc=%d", private->uid.ssid, rc);
S
Stefan Haberland 已提交
1941
	return rc;
1942 1943
}

1944 1945 1946 1947 1948 1949 1950
/*
 * 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);
1951 1952 1953 1954
	unsigned long flags = 0;

	set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
	if (dasd_eckd_validate_server(device, flags)
S
Stefan Haberland 已提交
1955 1956 1957 1958 1959 1960
	    == -EAGAIN) {
		/* schedule worker again if failed */
		schedule_work(&device->kick_validate);
		return;
	}

1961 1962 1963 1964 1965 1966
	dasd_put_device(device);
}

static void dasd_eckd_kick_validate_server(struct dasd_device *device)
{
	dasd_get_device(device);
1967 1968 1969 1970 1971 1972
	/* 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;
	}
1973
	/* queue call to do_validate_server to the kernel event daemon. */
1974 1975
	if (!schedule_work(&device->kick_validate))
		dasd_put_device(device);
1976 1977
}

1978 1979 1980 1981
/*
 * Check device characteristics.
 * If the device is accessible using ECKD discipline, the device is enabled.
 */
L
Linus Torvalds 已提交
1982 1983 1984
static int
dasd_eckd_check_characteristics(struct dasd_device *device)
{
1985
	struct dasd_eckd_private *private = device->private;
1986
	struct dasd_block *block;
1987
	struct dasd_uid temp_uid;
1988
	int rc, i;
1989
	int readonly;
1990
	unsigned long value;
L
Linus Torvalds 已提交
1991

1992 1993
	/* setup work queue for validate server*/
	INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1994 1995
	/* setup work queue for summary unit check */
	INIT_WORK(&device->suc_work, dasd_alias_handle_summary_unit_check);
1996

1997 1998 1999 2000 2001 2002 2003 2004 2005
	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");
	}
2006 2007 2008
	if (!private) {
		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
		if (!private) {
S
Stefan Haberland 已提交
2009 2010 2011
			dev_warn(&device->cdev->dev,
				 "Allocating memory for private DASD data "
				 "failed\n");
L
Linus Torvalds 已提交
2012 2013
			return -ENOMEM;
		}
2014
		device->private = private;
2015 2016
	} else {
		memset(private, 0, sizeof(*private));
L
Linus Torvalds 已提交
2017 2018 2019 2020 2021 2022 2023
	}
	/* 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;

2024 2025 2026
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
2027
		goto out_err1;
2028

2029
	/* set some default values */
2030
	device->default_expires = DASD_EXPIRES;
2031
	device->default_retries = DASD_RETRIES;
2032 2033
	device->path_thrhld = DASD_ECKD_PATH_THRHLD;
	device->path_interval = DASD_ECKD_PATH_INTERVAL;
2034

2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
	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;
	}

2045 2046
	dasd_eckd_get_uid(device, &temp_uid);
	if (temp_uid.type == UA_BASE_DEVICE) {
2047 2048
		block = dasd_alloc_block();
		if (IS_ERR(block)) {
2049 2050 2051
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
					"could not allocate dasd "
					"block structure");
2052 2053 2054 2055 2056 2057 2058
			rc = PTR_ERR(block);
			goto out_err1;
		}
		device->block = block;
		block->base = device;
	}

2059 2060 2061
	/* register lcu with alias handling, enable PAV */
	rc = dasd_alias_make_device_known_to_lcu(device);
	if (rc)
2062
		goto out_err2;
2063

S
Stefan Haberland 已提交
2064
	dasd_eckd_validate_server(device, 0);
2065 2066 2067 2068 2069

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

	/* Read Feature Codes */
2072
	dasd_eckd_read_features(device);
2073

2074
	/* Read Volume Information */
2075
	dasd_eckd_read_vol_info(device);
2076 2077

	/* Read Extent Pool Information */
2078
	dasd_eckd_read_ext_pool_info(device);
2079

L
Linus Torvalds 已提交
2080
	/* Read Device Characteristics */
2081 2082
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
					 &private->rdc_data, 64);
2083
	if (rc) {
2084 2085
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
2086 2087
		goto out_err3;
	}
2088 2089 2090 2091 2092 2093 2094 2095 2096

	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;
	}

2097
	/* find the valid cylinder size */
2098 2099 2100 2101 2102 2103
	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;

2104 2105
	private->fcx_max_data = get_fcx_max_data(device);

2106 2107 2108 2109
	readonly = dasd_device_is_ro(device);
	if (readonly)
		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);

S
Stefan Haberland 已提交
2110
	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
2111
		 "with %d cylinders, %d heads, %d sectors%s\n",
S
Stefan Haberland 已提交
2112 2113 2114 2115
		 private->rdc_data.dev_type,
		 private->rdc_data.dev_model,
		 private->rdc_data.cu_type,
		 private->rdc_data.cu_model.model,
2116
		 private->real_cyl,
S
Stefan Haberland 已提交
2117
		 private->rdc_data.trk_per_cyl,
2118 2119
		 private->rdc_data.sec_per_trk,
		 readonly ? ", read-only device" : "");
2120 2121 2122 2123 2124 2125 2126 2127
	return 0;

out_err3:
	dasd_alias_disconnect_device_from_lcu(device);
out_err2:
	dasd_free_block(device->block);
	device->block = NULL;
out_err1:
2128
	dasd_eckd_clear_conf_data(device);
2129 2130
	kfree(device->private);
	device->private = NULL;
2131
	return rc;
L
Linus Torvalds 已提交
2132 2133
}

2134 2135
static void dasd_eckd_uncheck_device(struct dasd_device *device)
{
2136
	struct dasd_eckd_private *private = device->private;
2137

2138 2139 2140
	if (!private)
		return;

2141
	dasd_alias_disconnect_device_from_lcu(device);
2142 2143 2144 2145
	private->ned = NULL;
	private->sneq = NULL;
	private->vdsneq = NULL;
	private->gneq = NULL;
2146
	dasd_eckd_clear_conf_data(device);
2147 2148
}

L
Linus Torvalds 已提交
2149 2150 2151
static struct dasd_ccw_req *
dasd_eckd_analysis_ccw(struct dasd_device *device)
{
2152
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
2153 2154 2155 2156 2157 2158 2159 2160 2161
	struct eckd_count *count_data;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int cplength, datasize;
	int i;

	cplength = 8;
	datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
2162 2163
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device,
				   NULL);
L
Linus Torvalds 已提交
2164 2165 2166
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
2167 2168
	/* Define extent for the first 2 tracks. */
	define_extent(ccw++, cqr->data, 0, 1,
2169
		      DASD_ECKD_CCW_READ_COUNT, device, 0);
2170
	LO_data = cqr->data + sizeof(struct DE_eckd_data);
L
Linus Torvalds 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
	/* 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++;
	}

2187
	/* Locate record for the first record on track 1. */
L
Linus Torvalds 已提交
2188
	ccw[-1].flags |= CCW_FLAG_CC;
2189
	locate_record(ccw++, LO_data++, 1, 0, 1,
L
Linus Torvalds 已提交
2190 2191 2192 2193 2194 2195 2196 2197
		      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;

2198 2199 2200
	cqr->block = NULL;
	cqr->startdev = device;
	cqr->memdev = device;
2201
	cqr->retries = 255;
2202
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
2203
	cqr->status = DASD_CQR_FILLED;
2204 2205 2206
	/* Set flags to suppress output for expected errors */
	set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);

L
Linus Torvalds 已提交
2207 2208 2209
	return cqr;
}

2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
/* 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 已提交
2227 2228 2229 2230 2231 2232 2233
/*
 * 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).
 */
2234 2235
static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
					void *data)
L
Linus Torvalds 已提交
2236
{
2237 2238
	struct dasd_device *device = init_cqr->startdev;
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
2239

2240
	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
L
Linus Torvalds 已提交
2241 2242 2243 2244
	dasd_sfree_request(init_cqr, device);
	dasd_kick_device(device);
}

2245
static int dasd_eckd_start_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
2246 2247 2248
{
	struct dasd_ccw_req *init_cqr;

2249
	init_cqr = dasd_eckd_analysis_ccw(block->base);
L
Linus Torvalds 已提交
2250 2251 2252 2253 2254
	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;
2255 2256 2257 2258 2259
	/* 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 已提交
2260 2261 2262 2263
	dasd_add_request_head(init_cqr);
	return -EAGAIN;
}

2264
static int dasd_eckd_end_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
2265
{
2266 2267
	struct dasd_device *device = block->base;
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
2268 2269 2270
	struct eckd_count *count_area;
	unsigned int sb, blk_per_trk;
	int status, i;
2271
	struct dasd_ccw_req *init_cqr;
L
Linus Torvalds 已提交
2272 2273 2274

	status = private->init_cqr_status;
	private->init_cqr_status = -1;
2275 2276 2277 2278 2279 2280 2281 2282
	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);
	}

2283 2284 2285 2286 2287 2288 2289
	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;
	}

2290 2291
	if (status == INIT_CQR_UNFORMATTED) {
		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
L
Linus Torvalds 已提交
2292
		return -EMEDIUMTYPE;
2293 2294 2295 2296 2297
	} 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 已提交
2298 2299 2300 2301 2302 2303 2304
	}

	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 ||
2305 2306 2307 2308
		    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 已提交
2309 2310 2311 2312 2313
			private->uses_cdl = 0;
			break;
		}
	}
	if (i == 3)
2314
		count_area = &private->count_area[3];
L
Linus Torvalds 已提交
2315 2316 2317 2318 2319

	if (private->uses_cdl == 0) {
		for (i = 0; i < 5; i++) {
			if ((private->count_area[i].kl != 0) ||
			    (private->count_area[i].dl !=
2320 2321 2322 2323
			     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 已提交
2324 2325 2326 2327 2328 2329
				break;
		}
		if (i == 5)
			count_area = &private->count_area[0];
	} else {
		if (private->count_area[3].record == 1)
S
Stefan Haberland 已提交
2330 2331
			dev_warn(&device->cdev->dev,
				 "Track 0 has no records following the VTOC\n");
L
Linus Torvalds 已提交
2332
	}
2333

L
Linus Torvalds 已提交
2334 2335 2336
	if (count_area != NULL && count_area->kl == 0) {
		/* we found notthing violating our disk layout */
		if (dasd_check_blocksize(count_area->dl) == 0)
2337
			block->bp_block = count_area->dl;
L
Linus Torvalds 已提交
2338
	}
2339
	if (block->bp_block == 0) {
S
Stefan Haberland 已提交
2340 2341
		dev_warn(&device->cdev->dev,
			 "The disk layout of the DASD is not supported\n");
L
Linus Torvalds 已提交
2342 2343
		return -EMEDIUMTYPE;
	}
2344 2345 2346
	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 已提交
2347

2348
	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
2349 2350

raw:
2351
	block->blocks = ((unsigned long) private->real_cyl *
L
Linus Torvalds 已提交
2352 2353 2354
			  private->rdc_data.trk_per_cyl *
			  blk_per_trk);

S
Stefan Haberland 已提交
2355
	dev_info(&device->cdev->dev,
2356
		 "DASD with %u KB/block, %lu KB total size, %u KB/track, "
S
Stefan Haberland 已提交
2357
		 "%s\n", (block->bp_block >> 10),
2358
		 (((unsigned long) private->real_cyl *
S
Stefan Haberland 已提交
2359 2360 2361 2362 2363
		   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 已提交
2364 2365 2366 2367

	return 0;
}

2368
static int dasd_eckd_do_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
2369
{
2370
	struct dasd_eckd_private *private = block->base->private;
L
Linus Torvalds 已提交
2371 2372

	if (private->init_cqr_status < 0)
2373
		return dasd_eckd_start_analysis(block);
L
Linus Torvalds 已提交
2374
	else
2375
		return dasd_eckd_end_analysis(block);
L
Linus Torvalds 已提交
2376 2377
}

2378
static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2379 2380 2381 2382 2383 2384
{
	return dasd_alias_add_device(device);
};

static int dasd_eckd_online_to_ready(struct dasd_device *device)
{
2385 2386 2387 2388 2389
	if (cancel_work_sync(&device->reload_device))
		dasd_put_device(device);
	if (cancel_work_sync(&device->kick_validate))
		dasd_put_device(device);

2390 2391 2392
	return 0;
};

2393
static int dasd_eckd_basic_to_known(struct dasd_device *device)
2394
{
2395 2396 2397
	return dasd_alias_remove_device(device);
};

L
Linus Torvalds 已提交
2398
static int
2399
dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
L
Linus Torvalds 已提交
2400
{
2401
	struct dasd_eckd_private *private = block->base->private;
L
Linus Torvalds 已提交
2402

2403
	if (dasd_check_blocksize(block->bp_block) == 0) {
L
Linus Torvalds 已提交
2404
		geo->sectors = recs_per_track(&private->rdc_data,
2405
					      0, block->bp_block);
L
Linus Torvalds 已提交
2406 2407 2408 2409 2410 2411
	}
	geo->cylinders = private->rdc_data.no_cyl;
	geo->heads = private->rdc_data.trk_per_cyl;
	return 0;
}

2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
/*
 * Build the TCW request for the format check
 */
static struct dasd_ccw_req *
dasd_eckd_build_check_tcw(struct dasd_device *base, struct format_data_t *fdata,
			  int enable_pav, struct eckd_count *fmt_buffer,
			  int rpt)
{
	struct dasd_eckd_private *start_priv;
	struct dasd_device *startdev = NULL;
	struct tidaw *last_tidaw = NULL;
	struct dasd_ccw_req *cqr;
	struct itcw *itcw;
	int itcw_size;
	int count;
	int rc;
	int i;

	if (enable_pav)
		startdev = dasd_alias_get_start_dev(base);

	if (!startdev)
		startdev = base;

	start_priv = startdev->private;

	count = rpt * (fdata->stop_unit - fdata->start_unit + 1);

	/*
	 * we're adding 'count' amount of tidaw to the itcw.
	 * calculate the corresponding itcw_size
	 */
	itcw_size = itcw_calc_size(0, count, 0);

2446
	cqr = dasd_fmalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
	if (IS_ERR(cqr))
		return cqr;

	start_priv->count++;

	itcw = itcw_init(cqr->data, itcw_size, ITCW_OP_READ, 0, count, 0);
	if (IS_ERR(itcw)) {
		rc = -EINVAL;
		goto out_err;
	}

	cqr->cpaddr = itcw_get_tcw(itcw);
	rc = prepare_itcw(itcw, fdata->start_unit, fdata->stop_unit,
			  DASD_ECKD_CCW_READ_COUNT_MT, base, startdev, 0, count,
			  sizeof(struct eckd_count),
			  count * sizeof(struct eckd_count), 0, rpt);
	if (rc)
		goto out_err;

	for (i = 0; i < count; i++) {
		last_tidaw = itcw_add_tidaw(itcw, 0, fmt_buffer++,
					    sizeof(struct eckd_count));
		if (IS_ERR(last_tidaw)) {
			rc = -EINVAL;
			goto out_err;
		}
	}

	last_tidaw->flags |= TIDAW_FLAGS_LAST;
	itcw_finalize(itcw);

	cqr->cpmode = 1;
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->basedev = base;
	cqr->retries = startdev->default_retries;
	cqr->expires = startdev->default_expires * HZ;
	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
	/* Set flags to suppress output for expected errors */
	set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
	set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);

	return cqr;

out_err:
	dasd_sfree_request(cqr, startdev);

	return ERR_PTR(rc);
}

/*
 * Build the CCW request for the format check
 */
static struct dasd_ccw_req *
dasd_eckd_build_check(struct dasd_device *base, struct format_data_t *fdata,
		      int enable_pav, struct eckd_count *fmt_buffer, int rpt)
{
	struct dasd_eckd_private *start_priv;
	struct dasd_eckd_private *base_priv;
	struct dasd_device *startdev = NULL;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	void *data;
	int cplength, datasize;
	int use_prefix;
	int count;
	int i;

	if (enable_pav)
		startdev = dasd_alias_get_start_dev(base);

	if (!startdev)
		startdev = base;

	start_priv = startdev->private;
	base_priv = base->private;

	count = rpt * (fdata->stop_unit - fdata->start_unit + 1);

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

	if (use_prefix) {
		cplength = 1;
		datasize = sizeof(struct PFX_eckd_data);
	} else {
		cplength = 2;
		datasize = sizeof(struct DE_eckd_data) +
			sizeof(struct LO_eckd_data);
	}
	cplength += count;

2539
	cqr = dasd_fmalloc_request(DASD_ECKD_MAGIC, cplength, datasize, startdev);
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
	if (IS_ERR(cqr))
		return cqr;

	start_priv->count++;
	data = cqr->data;
	ccw = cqr->cpaddr;

	if (use_prefix) {
		prefix_LRE(ccw++, data, fdata->start_unit, fdata->stop_unit,
			   DASD_ECKD_CCW_READ_COUNT, base, startdev, 1, 0,
			   count, 0, 0);
	} else {
		define_extent(ccw++, data, fdata->start_unit, fdata->stop_unit,
2553
			      DASD_ECKD_CCW_READ_COUNT, startdev, 0);
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584

		data += sizeof(struct DE_eckd_data);
		ccw[-1].flags |= CCW_FLAG_CC;

		locate_record(ccw++, data, fdata->start_unit, 0, count,
			      DASD_ECKD_CCW_READ_COUNT, base, 0);
	}

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

	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->basedev = base;
	cqr->retries = DASD_RETRIES;
	cqr->expires = startdev->default_expires * HZ;
	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
	/* Set flags to suppress output for expected errors */
	set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);

	return cqr;
}

L
Linus Torvalds 已提交
2585
static struct dasd_ccw_req *
2586 2587
dasd_eckd_build_format(struct dasd_device *base, struct dasd_device *startdev,
		       struct format_data_t *fdata, int enable_pav)
L
Linus Torvalds 已提交
2588
{
2589 2590
	struct dasd_eckd_private *base_priv;
	struct dasd_eckd_private *start_priv;
L
Linus Torvalds 已提交
2591 2592
	struct dasd_ccw_req *fcp;
	struct eckd_count *ect;
2593
	struct ch_t address;
L
Linus Torvalds 已提交
2594 2595
	struct ccw1 *ccw;
	void *data;
2596
	int rpt;
L
Linus Torvalds 已提交
2597
	int cplength, datasize;
2598
	int i, j;
2599 2600
	int intensity = 0;
	int r0_perm;
2601
	int nr_tracks;
2602
	int use_prefix;
L
Linus Torvalds 已提交
2603

S
Stefan Haberland 已提交
2604
	if (enable_pav)
2605 2606
		startdev = dasd_alias_get_start_dev(base);

2607 2608
	if (!startdev)
		startdev = base;
L
Linus Torvalds 已提交
2609

2610 2611
	start_priv = startdev->private;
	base_priv = base->private;
2612 2613 2614 2615

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

	nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
L
Linus Torvalds 已提交
2616 2617 2618 2619 2620 2621 2622

	/*
	 * 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)
2623
	 *   Bit 4: do not allow storage subsystem to modify record zero
L
Linus Torvalds 已提交
2624 2625
	 * Only some bit combinations do make sense.
	 */
2626 2627 2628 2629 2630 2631 2632
	if (fdata->intensity & 0x10) {
		r0_perm = 0;
		intensity = fdata->intensity & ~0x10;
	} else {
		r0_perm = 1;
		intensity = fdata->intensity;
	}
2633

2634 2635
	use_prefix = base_priv->features.feature[8] & 0x01;

2636
	switch (intensity) {
L
Linus Torvalds 已提交
2637 2638
	case 0x00:	/* Normal format */
	case 0x08:	/* Normal format, use cdl. */
2639
		cplength = 2 + (rpt*nr_tracks);
2640 2641 2642 2643 2644 2645 2646 2647
		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 已提交
2648 2649 2650
		break;
	case 0x01:	/* Write record zero and format track. */
	case 0x09:	/* Write record zero and format track, use cdl. */
2651
		cplength = 2 + rpt * nr_tracks;
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
		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 已提交
2662 2663 2664 2665
		break;
	case 0x04:	/* Invalidate track. */
	case 0x0c:	/* Invalidate track, use cdl. */
		cplength = 3;
2666 2667 2668 2669 2670 2671 2672 2673
		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 已提交
2674 2675
		break;
	default:
2676 2677 2678
		dev_warn(&startdev->cdev->dev,
			 "An I/O control call used incorrect flags 0x%x\n",
			 fdata->intensity);
L
Linus Torvalds 已提交
2679 2680
		return ERR_PTR(-EINVAL);
	}
2681 2682

	fcp = dasd_fmalloc_request(DASD_ECKD_MAGIC, cplength, datasize, startdev);
L
Linus Torvalds 已提交
2683 2684 2685
	if (IS_ERR(fcp))
		return fcp;

2686
	start_priv->count++;
L
Linus Torvalds 已提交
2687 2688 2689
	data = fcp->data;
	ccw = fcp->cpaddr;

2690
	switch (intensity & ~0x08) {
L
Linus Torvalds 已提交
2691
	case 0x00: /* Normal format. */
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
		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,
2704
				      DASD_ECKD_CCW_WRITE_CKD, startdev, 0);
2705 2706 2707 2708 2709 2710
			/* 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 已提交
2711 2712
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
2713 2714
			      fdata->start_unit, 0, rpt*nr_tracks,
			      DASD_ECKD_CCW_WRITE_CKD, base,
L
Linus Torvalds 已提交
2715 2716 2717 2718
			      fdata->blksize);
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x01: /* Write record zero + format track. */
2719 2720 2721 2722 2723 2724 2725 2726 2727
		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,
2728
			       DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev, 0);
2729 2730
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2731 2732
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
2733 2734 2735
			      fdata->start_unit, 0, rpt * nr_tracks + 1,
			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
			      base->block->bp_block);
L
Linus Torvalds 已提交
2736 2737 2738
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x04: /* Invalidate track. */
2739 2740 2741 2742 2743 2744 2745 2746
		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,
2747
			       DASD_ECKD_CCW_WRITE_CKD, startdev, 0);
2748 2749
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2750 2751 2752
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
			      fdata->start_unit, 0, 1,
2753
			      DASD_ECKD_CCW_WRITE_CKD, base, 8);
L
Linus Torvalds 已提交
2754 2755 2756
		data += sizeof(struct LO_eckd_data);
		break;
	}
2757 2758 2759 2760 2761 2762 2763 2764 2765

	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 已提交
2766 2767
			ect = (struct eckd_count *) data;
			data += sizeof(struct eckd_count);
2768 2769
			ect->cyl = address.cyl;
			ect->head = address.head;
2770
			ect->record = 0;
L
Linus Torvalds 已提交
2771
			ect->kl = 0;
2772
			ect->dl = 8;
L
Linus Torvalds 已提交
2773
			ccw[-1].flags |= CCW_FLAG_CC;
2774
			ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
L
Linus Torvalds 已提交
2775 2776 2777 2778 2779
			ccw->flags = CCW_FLAG_SLI;
			ccw->count = 8;
			ccw->cda = (__u32)(addr_t) ect;
			ccw++;
		}
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
		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) &&
2807
				    address.cyl == 0 && address.head == 0) {
2808 2809 2810 2811 2812 2813
					if (i < 3) {
						ect->kl = 4;
						ect->dl = sizes_trk0[i] - 4;
					}
				}
				if ((intensity & 0x08) &&
2814
				    address.cyl == 0 && address.head == 1) {
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
					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;
2827 2828
				ccw->cda = (__u32)(addr_t) ect;
				ccw++;
2829 2830
			}
		}
L
Linus Torvalds 已提交
2831
	}
2832 2833 2834

	fcp->startdev = startdev;
	fcp->memdev = startdev;
2835
	fcp->basedev = base;
2836
	fcp->retries = 256;
2837
	fcp->expires = startdev->default_expires * HZ;
2838
	fcp->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
2839
	fcp->status = DASD_CQR_FILLED;
2840

L
Linus Torvalds 已提交
2841 2842 2843
	return fcp;
}

2844 2845 2846 2847 2848
/*
 * Wrapper function to build a CCW request depending on input data
 */
static struct dasd_ccw_req *
dasd_eckd_format_build_ccw_req(struct dasd_device *base,
2849 2850
			       struct format_data_t *fdata, int enable_pav,
			       int tpm, struct eckd_count *fmt_buffer, int rpt)
2851
{
2852 2853 2854
	struct dasd_ccw_req *ccw_req;

	if (!fmt_buffer) {
2855
		ccw_req = dasd_eckd_build_format(base, NULL, fdata, enable_pav);
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
	} else {
		if (tpm)
			ccw_req = dasd_eckd_build_check_tcw(base, fdata,
							    enable_pav,
							    fmt_buffer, rpt);
		else
			ccw_req = dasd_eckd_build_check(base, fdata, enable_pav,
							fmt_buffer, rpt);
	}

	return ccw_req;
2867 2868 2869 2870 2871 2872 2873
}

/*
 * Sanity checks on format_data
 */
static int dasd_eckd_format_sanity_checks(struct dasd_device *base,
					  struct format_data_t *fdata)
2874
{
2875
	struct dasd_eckd_private *private = base->private;
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902

	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;
	}
2903 2904 2905 2906 2907 2908 2909 2910
	return 0;
}

/*
 * This function will process format_data originally coming from an IOCTL
 */
static int dasd_eckd_format_process_data(struct dasd_device *base,
					 struct format_data_t *fdata,
2911 2912 2913
					 int enable_pav, int tpm,
					 struct eckd_count *fmt_buffer, int rpt,
					 struct irb *irb)
2914
{
2915
	struct dasd_eckd_private *private = base->private;
2916 2917 2918
	struct dasd_ccw_req *cqr, *n;
	struct list_head format_queue;
	struct dasd_device *device;
2919
	char *sense = NULL;
2920 2921 2922 2923 2924 2925 2926
	int old_start, old_stop, format_step;
	int step, retry;
	int rc;

	rc = dasd_eckd_format_sanity_checks(base, fdata);
	if (rc)
		return rc;
2927 2928 2929

	INIT_LIST_HEAD(&format_queue);

2930
	old_start = fdata->start_unit;
2931
	old_stop = fdata->stop_unit;
2932

2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
	if (!tpm && fmt_buffer != NULL) {
		/* Command Mode / Format Check */
		format_step = 1;
	} else if (tpm && fmt_buffer != NULL) {
		/* Transport Mode / Format Check */
		format_step = DASD_CQR_MAX_CCW / rpt;
	} else {
		/* Normal Formatting */
		format_step = DASD_CQR_MAX_CCW /
			recs_per_track(&private->rdc_data, 0, fdata->blksize);
	}

2945 2946 2947 2948 2949 2950 2951 2952
	do {
		retry = 0;
		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;
			}
2953

2954
			cqr = dasd_eckd_format_build_ccw_req(base, fdata,
2955 2956
							     enable_pav, tpm,
							     fmt_buffer, rpt);
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
			if (IS_ERR(cqr)) {
				rc = PTR_ERR(cqr);
				if (rc == -ENOMEM) {
					if (list_empty(&format_queue))
						goto out;
					/*
					 * not enough memory available, start
					 * requests retry after first requests
					 * were finished
					 */
					retry = 1;
					break;
				}
				goto out_err;
			}
			list_add_tail(&cqr->blocklist, &format_queue);
2973

2974 2975 2976 2977
			if (fmt_buffer) {
				step = fdata->stop_unit - fdata->start_unit + 1;
				fmt_buffer += rpt * step;
			}
2978 2979
			fdata->start_unit = fdata->stop_unit + 1;
			fdata->stop_unit = old_stop;
2980 2981
		}

2982 2983 2984 2985 2986
		rc = dasd_sleep_on_queue(&format_queue);

out_err:
		list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
			device = cqr->startdev;
2987
			private = device->private;
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997

			if (cqr->status == DASD_CQR_FAILED) {
				/*
				 * Only get sense data if called by format
				 * check
				 */
				if (fmt_buffer && irb) {
					sense = dasd_get_sense(&cqr->irb);
					memcpy(irb, &cqr->irb, sizeof(*irb));
				}
2998
				rc = -EIO;
2999
			}
3000
			list_del_init(&cqr->blocklist);
3001
			dasd_ffree_request(cqr, device);
3002 3003
			private->count--;
		}
3004

3005
		if (rc && rc != -EIO)
3006
			goto out;
3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
		if (rc == -EIO) {
			/*
			 * In case fewer than the expected records are on the
			 * track, we will most likely get a 'No Record Found'
			 * error (in command mode) or a 'File Protected' error
			 * (in transport mode). Those particular cases shouldn't
			 * pass the -EIO to the IOCTL, therefore reset the rc
			 * and continue.
			 */
			if (sense &&
			    (sense[1] & SNS1_NO_REC_FOUND ||
			     sense[1] & SNS1_FILE_PROTECTED))
				retry = 1;
			else
				goto out;
		}
3023

3024
	} while (retry);
3025

3026 3027 3028
out:
	fdata->start_unit = old_start;
	fdata->stop_unit = old_stop;
3029 3030 3031 3032

	return rc;
}

3033 3034 3035
static int dasd_eckd_format_device(struct dasd_device *base,
				   struct format_data_t *fdata, int enable_pav)
{
3036 3037 3038 3039
	return dasd_eckd_format_process_data(base, fdata, enable_pav, 0, NULL,
					     0, NULL);
}

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
static bool test_and_set_format_track(struct dasd_format_entry *to_format,
				      struct dasd_block *block)
{
	struct dasd_format_entry *format;
	unsigned long flags;
	bool rc = false;

	spin_lock_irqsave(&block->format_lock, flags);
	list_for_each_entry(format, &block->format_list, list) {
		if (format->track == to_format->track) {
			rc = true;
			goto out;
		}
	}
	list_add_tail(&to_format->list, &block->format_list);

out:
	spin_unlock_irqrestore(&block->format_lock, flags);
	return rc;
}

static void clear_format_track(struct dasd_format_entry *format,
			      struct dasd_block *block)
{
	unsigned long flags;

	spin_lock_irqsave(&block->format_lock, flags);
	list_del_init(&format->list);
	spin_unlock_irqrestore(&block->format_lock, flags);
}

3071 3072 3073 3074 3075 3076 3077
/*
 * Callback function to free ESE format requests.
 */
static void dasd_eckd_ese_format_cb(struct dasd_ccw_req *cqr, void *data)
{
	struct dasd_device *device = cqr->startdev;
	struct dasd_eckd_private *private = device->private;
3078
	struct dasd_format_entry *format = data;
3079

3080
	clear_format_track(format, cqr->basedev->block);
3081 3082 3083 3084 3085
	private->count--;
	dasd_ffree_request(cqr, device);
}

static struct dasd_ccw_req *
3086 3087
dasd_eckd_ese_format(struct dasd_device *startdev, struct dasd_ccw_req *cqr,
		     struct irb *irb)
3088 3089
{
	struct dasd_eckd_private *private;
3090
	struct dasd_format_entry *format;
3091 3092 3093 3094 3095 3096 3097 3098 3099
	struct format_data_t fdata;
	unsigned int recs_per_trk;
	struct dasd_ccw_req *fcqr;
	struct dasd_device *base;
	struct dasd_block *block;
	unsigned int blksize;
	struct request *req;
	sector_t first_trk;
	sector_t last_trk;
3100
	sector_t curr_trk;
3101 3102 3103
	int rc;

	req = cqr->callback_data;
3104 3105
	block = cqr->block;
	base = block->base;
3106 3107 3108
	private = base->private;
	blksize = block->bp_block;
	recs_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3109
	format = &startdev->format_entry;
3110 3111 3112 3113 3114 3115

	first_trk = blk_rq_pos(req) >> block->s2b_shift;
	sector_div(first_trk, recs_per_trk);
	last_trk =
		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
	sector_div(last_trk, recs_per_trk);
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
	rc = dasd_eckd_track_from_irb(irb, base, &curr_trk);
	if (rc)
		return ERR_PTR(rc);

	if (curr_trk < first_trk || curr_trk > last_trk) {
		DBF_DEV_EVENT(DBF_WARNING, startdev,
			      "ESE error track %llu not within range %llu - %llu\n",
			      curr_trk, first_trk, last_trk);
		return ERR_PTR(-EINVAL);
	}
	format->track = curr_trk;
	/* test if track is already in formatting by another thread */
	if (test_and_set_format_track(format, block))
		return ERR_PTR(-EEXIST);
3130

3131 3132
	fdata.start_unit = curr_trk;
	fdata.stop_unit = curr_trk;
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
	fdata.blksize = blksize;
	fdata.intensity = private->uses_cdl ? DASD_FMT_INT_COMPAT : 0;

	rc = dasd_eckd_format_sanity_checks(base, &fdata);
	if (rc)
		return ERR_PTR(-EINVAL);

	/*
	 * We're building the request with PAV disabled as we're reusing
	 * the former startdev.
	 */
	fcqr = dasd_eckd_build_format(base, startdev, &fdata, 0);
	if (IS_ERR(fcqr))
		return fcqr;

	fcqr->callback = dasd_eckd_ese_format_cb;
3149
	fcqr->callback_data = (void *) format;
3150 3151 3152 3153 3154 3155 3156

	return fcqr;
}

/*
 * When data is read from an unformatted area of an ESE volume, this function
 * returns zeroed data and thereby mimics a read of zero data.
3157 3158 3159 3160 3161 3162 3163 3164
 *
 * The first unformatted track is the one that got the NRF error, the address is
 * encoded in the sense data.
 *
 * All tracks before have returned valid data and should not be touched.
 * All tracks after the unformatted track might be formatted or not. This is
 * currently not known, remember the processed data and return the remainder of
 * the request to the blocklayer in __dasd_cleanup_cqr().
3165
 */
3166
static int dasd_eckd_ese_read(struct dasd_ccw_req *cqr, struct irb *irb)
3167
{
3168 3169 3170
	struct dasd_eckd_private *private;
	sector_t first_trk, last_trk;
	sector_t first_blk, last_blk;
3171
	unsigned int blksize, off;
3172
	unsigned int recs_per_trk;
3173 3174
	struct dasd_device *base;
	struct req_iterator iter;
3175 3176 3177
	struct dasd_block *block;
	unsigned int skip_block;
	unsigned int blk_count;
3178 3179
	struct request *req;
	struct bio_vec bv;
3180 3181
	sector_t curr_trk;
	sector_t end_blk;
3182
	char *dst;
3183
	int rc;
3184 3185 3186 3187

	req = (struct request *) cqr->callback_data;
	base = cqr->block->base;
	blksize = base->block->bp_block;
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
	block =  cqr->block;
	private = base->private;
	skip_block = 0;
	blk_count = 0;

	recs_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
	first_trk = first_blk = blk_rq_pos(req) >> block->s2b_shift;
	sector_div(first_trk, recs_per_trk);
	last_trk = last_blk =
		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
	sector_div(last_trk, recs_per_trk);
	rc = dasd_eckd_track_from_irb(irb, base, &curr_trk);
	if (rc)
		return rc;

	/* sanity check if the current track from sense data is valid */
	if (curr_trk < first_trk || curr_trk > last_trk) {
		DBF_DEV_EVENT(DBF_WARNING, base,
			      "ESE error track %llu not within range %llu - %llu\n",
			      curr_trk, first_trk, last_trk);
		return -EINVAL;
	}

	/*
	 * if not the first track got the NRF error we have to skip over valid
	 * blocks
	 */
	if (curr_trk != first_trk)
		skip_block = curr_trk * recs_per_trk - first_blk;

	/* we have no information beyond the current track */
	end_blk = (curr_trk + 1) * recs_per_trk;
3220 3221 3222 3223

	rq_for_each_segment(bv, req, iter) {
		dst = page_address(bv.bv_page) + bv.bv_offset;
		for (off = 0; off < bv.bv_len; off += blksize) {
3224 3225 3226 3227 3228
			if (first_blk + blk_count >= end_blk) {
				cqr->proc_bytes = blk_count * blksize;
				return 0;
			}
			if (dst && !skip_block) {
3229 3230
				dst += off;
				memset(dst, 0, blksize);
3231 3232
			} else {
				skip_block--;
3233
			}
3234
			blk_count++;
3235 3236
		}
	}
3237
	return 0;
3238 3239
}

3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 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 3405 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 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454
/*
 * Helper function to count consecutive records of a single track.
 */
static int dasd_eckd_count_records(struct eckd_count *fmt_buffer, int start,
				   int max)
{
	int head;
	int i;

	head = fmt_buffer[start].head;

	/*
	 * There are 3 conditions where we stop counting:
	 * - if data reoccurs (same head and record may reoccur), which may
	 *   happen due to the way DASD_ECKD_CCW_READ_COUNT works
	 * - when the head changes, because we're iterating over several tracks
	 *   then (DASD_ECKD_CCW_READ_COUNT_MT)
	 * - when we've reached the end of sensible data in the buffer (the
	 *   record will be 0 then)
	 */
	for (i = start; i < max; i++) {
		if (i > start) {
			if ((fmt_buffer[i].head == head &&
			    fmt_buffer[i].record == 1) ||
			    fmt_buffer[i].head != head ||
			    fmt_buffer[i].record == 0)
				break;
		}
	}

	return i - start;
}

/*
 * Evaluate a given range of tracks. Data like number of records, blocksize,
 * record ids, and key length are compared with expected data.
 *
 * If a mismatch occurs, the corresponding error bit is set, as well as
 * additional information, depending on the error.
 */
static void dasd_eckd_format_evaluate_tracks(struct eckd_count *fmt_buffer,
					     struct format_check_t *cdata,
					     int rpt_max, int rpt_exp,
					     int trk_per_cyl, int tpm)
{
	struct ch_t geo;
	int max_entries;
	int count = 0;
	int trkcount;
	int blksize;
	int pos = 0;
	int i, j;
	int kl;

	trkcount = cdata->expect.stop_unit - cdata->expect.start_unit + 1;
	max_entries = trkcount * rpt_max;

	for (i = cdata->expect.start_unit; i <= cdata->expect.stop_unit; i++) {
		/* Calculate the correct next starting position in the buffer */
		if (tpm) {
			while (fmt_buffer[pos].record == 0 &&
			       fmt_buffer[pos].dl == 0) {
				if (pos++ > max_entries)
					break;
			}
		} else {
			if (i != cdata->expect.start_unit)
				pos += rpt_max - count;
		}

		/* Calculate the expected geo values for the current track */
		set_ch_t(&geo, i / trk_per_cyl, i % trk_per_cyl);

		/* Count and check number of records */
		count = dasd_eckd_count_records(fmt_buffer, pos, pos + rpt_max);

		if (count < rpt_exp) {
			cdata->result = DASD_FMT_ERR_TOO_FEW_RECORDS;
			break;
		}
		if (count > rpt_exp) {
			cdata->result = DASD_FMT_ERR_TOO_MANY_RECORDS;
			break;
		}

		for (j = 0; j < count; j++, pos++) {
			blksize = cdata->expect.blksize;
			kl = 0;

			/*
			 * Set special values when checking CDL formatted
			 * devices.
			 */
			if ((cdata->expect.intensity & 0x08) &&
			    geo.cyl == 0 && geo.head == 0) {
				if (j < 3) {
					blksize = sizes_trk0[j] - 4;
					kl = 4;
				}
			}
			if ((cdata->expect.intensity & 0x08) &&
			    geo.cyl == 0 && geo.head == 1) {
				blksize = LABEL_SIZE - 44;
				kl = 44;
			}

			/* Check blocksize */
			if (fmt_buffer[pos].dl != blksize) {
				cdata->result = DASD_FMT_ERR_BLKSIZE;
				goto out;
			}
			/* Check if key length is 0 */
			if (fmt_buffer[pos].kl != kl) {
				cdata->result = DASD_FMT_ERR_KEY_LENGTH;
				goto out;
			}
			/* Check if record_id is correct */
			if (fmt_buffer[pos].cyl != geo.cyl ||
			    fmt_buffer[pos].head != geo.head ||
			    fmt_buffer[pos].record != (j + 1)) {
				cdata->result = DASD_FMT_ERR_RECORD_ID;
				goto out;
			}
		}
	}

out:
	/*
	 * In case of no errors, we need to decrease by one
	 * to get the correct positions.
	 */
	if (!cdata->result) {
		i--;
		pos--;
	}

	cdata->unit = i;
	cdata->num_records = count;
	cdata->rec = fmt_buffer[pos].record;
	cdata->blksize = fmt_buffer[pos].dl;
	cdata->key_length = fmt_buffer[pos].kl;
}

/*
 * Check the format of a range of tracks of a DASD.
 */
static int dasd_eckd_check_device_format(struct dasd_device *base,
					 struct format_check_t *cdata,
					 int enable_pav)
{
	struct dasd_eckd_private *private = base->private;
	struct eckd_count *fmt_buffer;
	struct irb irb;
	int rpt_max, rpt_exp;
	int fmt_buffer_size;
	int trk_per_cyl;
	int trkcount;
	int tpm = 0;
	int rc;

	trk_per_cyl = private->rdc_data.trk_per_cyl;

	/* Get maximum and expected amount of records per track */
	rpt_max = recs_per_track(&private->rdc_data, 0, 512) + 1;
	rpt_exp = recs_per_track(&private->rdc_data, 0, cdata->expect.blksize);

	trkcount = cdata->expect.stop_unit - cdata->expect.start_unit + 1;
	fmt_buffer_size = trkcount * rpt_max * sizeof(struct eckd_count);

	fmt_buffer = kzalloc(fmt_buffer_size, GFP_KERNEL | GFP_DMA);
	if (!fmt_buffer)
		return -ENOMEM;

	/*
	 * A certain FICON feature subset is needed to operate in transport
	 * mode. Additionally, the support for transport mode is implicitly
	 * checked by comparing the buffer size with fcx_max_data. As long as
	 * the buffer size is smaller we can operate in transport mode and
	 * process multiple tracks. If not, only one track at once is being
	 * processed using command mode.
	 */
	if ((private->features.feature[40] & 0x04) &&
	    fmt_buffer_size <= private->fcx_max_data)
		tpm = 1;

	rc = dasd_eckd_format_process_data(base, &cdata->expect, enable_pav,
					   tpm, fmt_buffer, rpt_max, &irb);
	if (rc && rc != -EIO)
		goto out;
	if (rc == -EIO) {
		/*
		 * If our first attempt with transport mode enabled comes back
		 * with an incorrect length error, we're going to retry the
		 * check with command mode.
		 */
		if (tpm && scsw_cstat(&irb.scsw) == 0x40) {
			tpm = 0;
			rc = dasd_eckd_format_process_data(base, &cdata->expect,
							   enable_pav, tpm,
							   fmt_buffer, rpt_max,
							   &irb);
			if (rc)
				goto out;
		} else {
			goto out;
		}
	}

	dasd_eckd_format_evaluate_tracks(fmt_buffer, cdata, rpt_max, rpt_exp,
					 trk_per_cyl, tpm);

out:
	kfree(fmt_buffer);

	return rc;
3455 3456
}

3457
static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
L
Linus Torvalds 已提交
3458
{
3459 3460 3461 3462
	if (cqr->retries < 0) {
		cqr->status = DASD_CQR_FAILED;
		return;
	}
3463 3464 3465 3466
	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;
3467
		cqr->lpm = dasd_path_get_opm(cqr->block->base);
L
Linus Torvalds 已提交
3468
	}
3469
};
L
Linus Torvalds 已提交
3470 3471 3472 3473

static dasd_erp_fn_t
dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
{
3474
	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
L
Linus Torvalds 已提交
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
	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;
}

3496 3497 3498
static void dasd_eckd_check_for_device_change(struct dasd_device *device,
					      struct dasd_ccw_req *cqr,
					      struct irb *irb)
3499 3500
{
	char mask;
3501
	char *sense = NULL;
3502
	struct dasd_eckd_private *private = device->private;
3503 3504 3505

	/* first of all check for state change pending interrupt */
	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
3506
	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
3507 3508 3509 3510
		/*
		 * for alias only, not in offline processing
		 * and only if not suspended
		 */
3511
		if (!device->block && private->lcu &&
3512
		    device->state == DASD_STATE_ONLINE &&
3513 3514
		    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
		    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
3515 3516 3517
			/* schedule worker to reload device */
			dasd_reload_device(device);
		}
3518 3519 3520 3521
		dasd_generic_handle_state_change(device);
		return;
	}

3522
	sense = dasd_get_sense(irb);
3523 3524 3525 3526
	if (!sense)
		return;

	/* summary unit check */
3527
	if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
3528
	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
		if (test_and_set_bit(DASD_FLAG_SUC, &device->flags)) {
			DBF_DEV_EVENT(DBF_WARNING, device, "%s",
				      "eckd suc: device already notified");
			return;
		}
		sense = dasd_get_sense(irb);
		if (!sense) {
			DBF_DEV_EVENT(DBF_WARNING, device, "%s",
				      "eckd suc: no reason code available");
			clear_bit(DASD_FLAG_SUC, &device->flags);
			return;

		}
		private->suc_reason = sense[8];
		DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
			      "eckd handle summary unit check: reason",
			      private->suc_reason);
		dasd_get_device(device);
		if (!schedule_work(&device->suc_work))
			dasd_put_device(device);

3550 3551 3552
		return;
	}

3553
	/* service information message SIM */
3554
	if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
3555 3556
	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
		dasd_3990_erp_handle_sim(device, sense);
3557 3558 3559
		return;
	}

3560 3561 3562
	/* loss of device reservation is handled via base devices only
	 * as alias devices may be used with several bases
	 */
3563 3564
	if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
	    (sense[7] == 0x3F) &&
3565 3566 3567 3568 3569 3570 3571
	    (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");
3572
	}
3573
}
3574

3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 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
static int dasd_eckd_ras_sanity_checks(struct dasd_device *device,
				       unsigned int first_trk,
				       unsigned int last_trk)
{
	struct dasd_eckd_private *private = device->private;
	unsigned int trks_per_vol;
	int rc = 0;

	trks_per_vol = private->real_cyl * private->rdc_data.trk_per_cyl;

	if (first_trk >= trks_per_vol) {
		dev_warn(&device->cdev->dev,
			 "Start track number %u used in the space release command is too big\n",
			 first_trk);
		rc = -EINVAL;
	} else if (last_trk >= trks_per_vol) {
		dev_warn(&device->cdev->dev,
			 "Stop track number %u used in the space release command is too big\n",
			 last_trk);
		rc = -EINVAL;
	} else if (first_trk > last_trk) {
		dev_warn(&device->cdev->dev,
			 "Start track %u used in the space release command exceeds the end track\n",
			 first_trk);
		rc = -EINVAL;
	}
	return rc;
}

/*
 * Helper function to count the amount of involved extents within a given range
 * with extent alignment in mind.
 */
static int count_exts(unsigned int from, unsigned int to, int trks_per_ext)
{
	int cur_pos = 0;
	int count = 0;
	int tmp;

	if (from == to)
		return 1;

	/* Count first partial extent */
	if (from % trks_per_ext != 0) {
		tmp = from + trks_per_ext - (from % trks_per_ext) - 1;
		if (tmp > to)
			tmp = to;
		cur_pos = tmp - from + 1;
		count++;
	}
	/* Count full extents */
	if (to - (from + cur_pos) + 1 >= trks_per_ext) {
		tmp = to - ((to - trks_per_ext + 1) % trks_per_ext);
		count += (tmp - (from + cur_pos) + 1) / trks_per_ext;
		cur_pos = tmp;
	}
	/* Count last partial extent */
	if (cur_pos < to)
		count++;

	return count;
}

/*
 * Release allocated space for a given range or an entire volume.
 */
static struct dasd_ccw_req *
dasd_eckd_dso_ras(struct dasd_device *device, struct dasd_block *block,
		  struct request *req, unsigned int first_trk,
		  unsigned int last_trk, int by_extent)
{
	struct dasd_eckd_private *private = device->private;
	struct dasd_dso_ras_ext_range *ras_range;
	struct dasd_rssd_features *features;
	struct dasd_dso_ras_data *ras_data;
	u16 heads, beg_head, end_head;
	int cur_to_trk, cur_from_trk;
	struct dasd_ccw_req *cqr;
	u32 beg_cyl, end_cyl;
	struct ccw1 *ccw;
	int trks_per_ext;
	size_t ras_size;
	size_t size;
	int nr_exts;
	void *rq;
	int i;

	if (dasd_eckd_ras_sanity_checks(device, first_trk, last_trk))
		return ERR_PTR(-EINVAL);

	rq = req ? blk_mq_rq_to_pdu(req) : NULL;

	features = &private->features;

	trks_per_ext = dasd_eckd_ext_size(device) * private->rdc_data.trk_per_cyl;
	nr_exts = 0;
	if (by_extent)
		nr_exts = count_exts(first_trk, last_trk, trks_per_ext);
	ras_size = sizeof(*ras_data);
	size = ras_size + (nr_exts * sizeof(*ras_range));

	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, size, device, rq);
	if (IS_ERR(cqr)) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate RAS request");
		return cqr;
	}

	ras_data = cqr->data;
	memset(ras_data, 0, size);

	ras_data->order = DSO_ORDER_RAS;
	ras_data->flags.vol_type = 0; /* CKD volume */
	/* Release specified extents or entire volume */
	ras_data->op_flags.by_extent = by_extent;
	/*
	 * This bit guarantees initialisation of tracks within an extent that is
	 * not fully specified, but is only supported with a certain feature
	 * subset.
	 */
	ras_data->op_flags.guarantee_init = !!(features->feature[56] & 0x01);
	ras_data->lss = private->ned->ID;
	ras_data->dev_addr = private->ned->unit_addr;
	ras_data->nr_exts = nr_exts;

	if (by_extent) {
		heads = private->rdc_data.trk_per_cyl;
		cur_from_trk = first_trk;
		cur_to_trk = first_trk + trks_per_ext -
			(first_trk % trks_per_ext) - 1;
		if (cur_to_trk > last_trk)
			cur_to_trk = last_trk;
		ras_range = (struct dasd_dso_ras_ext_range *)(cqr->data + ras_size);

		for (i = 0; i < nr_exts; i++) {
			beg_cyl = cur_from_trk / heads;
			beg_head = cur_from_trk % heads;
			end_cyl = cur_to_trk / heads;
			end_head = cur_to_trk % heads;

			set_ch_t(&ras_range->beg_ext, beg_cyl, beg_head);
			set_ch_t(&ras_range->end_ext, end_cyl, end_head);

			cur_from_trk = cur_to_trk + 1;
			cur_to_trk = cur_from_trk + trks_per_ext - 1;
			if (cur_to_trk > last_trk)
				cur_to_trk = last_trk;
			ras_range++;
		}
	}

	ccw = cqr->cpaddr;
	ccw->cda = (__u32)(addr_t)cqr->data;
	ccw->cmd_code = DASD_ECKD_CCW_DSO;
	ccw->count = size;

	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = block;
	cqr->retries = 256;
	cqr->expires = device->default_expires * HZ;
	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;

	return cqr;
}

static int dasd_eckd_release_space_full(struct dasd_device *device)
{
	struct dasd_ccw_req *cqr;
	int rc;

	cqr = dasd_eckd_dso_ras(device, NULL, NULL, 0, 0, 0);
	if (IS_ERR(cqr))
		return PTR_ERR(cqr);

	rc = dasd_sleep_on_interruptible(cqr);

	dasd_sfree_request(cqr, cqr->memdev);

	return rc;
}

static int dasd_eckd_release_space_trks(struct dasd_device *device,
					unsigned int from, unsigned int to)
{
	struct dasd_eckd_private *private = device->private;
	struct dasd_block *block = device->block;
	struct dasd_ccw_req *cqr, *n;
	struct list_head ras_queue;
	unsigned int device_exts;
	int trks_per_ext;
	int stop, step;
	int cur_pos;
	int rc = 0;
	int retry;

	INIT_LIST_HEAD(&ras_queue);

	device_exts = private->real_cyl / dasd_eckd_ext_size(device);
	trks_per_ext = dasd_eckd_ext_size(device) * private->rdc_data.trk_per_cyl;

	/* Make sure device limits are not exceeded */
	step = trks_per_ext * min(device_exts, DASD_ECKD_RAS_EXTS_MAX);
	cur_pos = from;

	do {
		retry = 0;
		while (cur_pos < to) {
			stop = cur_pos + step -
				((cur_pos + step) % trks_per_ext) - 1;
			if (stop > to)
				stop = to;

			cqr = dasd_eckd_dso_ras(device, NULL, NULL, cur_pos, stop, 1);
			if (IS_ERR(cqr)) {
				rc = PTR_ERR(cqr);
				if (rc == -ENOMEM) {
					if (list_empty(&ras_queue))
						goto out;
					retry = 1;
					break;
				}
				goto err_out;
			}

			spin_lock_irq(&block->queue_lock);
			list_add_tail(&cqr->blocklist, &ras_queue);
			spin_unlock_irq(&block->queue_lock);
			cur_pos = stop + 1;
		}

		rc = dasd_sleep_on_queue_interruptible(&ras_queue);

err_out:
		list_for_each_entry_safe(cqr, n, &ras_queue, blocklist) {
			device = cqr->startdev;
			private = device->private;

			spin_lock_irq(&block->queue_lock);
			list_del_init(&cqr->blocklist);
			spin_unlock_irq(&block->queue_lock);
			dasd_sfree_request(cqr, device);
			private->count--;
		}
	} while (retry);

out:
	return rc;
}

static int dasd_eckd_release_space(struct dasd_device *device,
				   struct format_data_t *rdata)
{
	if (rdata->intensity & DASD_FMT_INT_ESE_FULL)
		return dasd_eckd_release_space_full(device);
	else if (rdata->intensity == 0)
		return dasd_eckd_release_space_trks(device, rdata->start_unit,
						    rdata->stop_unit);
	else
		return -EINVAL;
}

3838 3839
static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
					       struct dasd_device *startdev,
3840
					       struct dasd_block *block,
3841 3842 3843 3844 3845 3846 3847 3848 3849
					       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 已提交
3850 3851 3852 3853 3854 3855
{
	struct dasd_eckd_private *private;
	unsigned long *idaws;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
3856
	struct req_iterator iter;
3857
	struct bio_vec bv;
L
Linus Torvalds 已提交
3858
	char *dst;
3859
	unsigned int off;
L
Linus Torvalds 已提交
3860
	int count, cidaw, cplength, datasize;
3861
	sector_t recid;
L
Linus Torvalds 已提交
3862
	unsigned char cmd, rcmd;
3863 3864
	int use_prefix;
	struct dasd_device *basedev;
L
Linus Torvalds 已提交
3865

3866
	basedev = block->base;
3867
	private = basedev->private;
L
Linus Torvalds 已提交
3868 3869 3870 3871 3872 3873
	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);
3874

L
Linus Torvalds 已提交
3875 3876 3877
	/* Check struct bio and count the number of blocks for the request. */
	count = 0;
	cidaw = 0;
3878
	rq_for_each_segment(bv, req, iter) {
3879
		if (bv.bv_len & (blksize - 1))
3880 3881
			/* Eckd can only do full blocks. */
			return ERR_PTR(-EINVAL);
3882 3883 3884
		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 已提交
3885 3886 3887 3888
	}
	/* Paranoia. */
	if (count != last_rec - first_rec + 1)
		return ERR_PTR(-EINVAL);
3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906

	/* 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 已提交
3907 3908 3909 3910 3911 3912 3913 3914
	/* 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. */
3915
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
3916
				   startdev, blk_mq_rq_to_pdu(req));
L
Linus Torvalds 已提交
3917 3918 3919
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
	/* 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,
3934
				  last_trk, cmd, basedev, 0) == -EAGAIN) {
3935 3936 3937 3938 3939 3940 3941 3942
			/* 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 已提交
3943
	}
L
Linus Torvalds 已提交
3944 3945 3946 3947 3948 3949 3950
	/* 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,
3951
			      last_rec - recid + 1, cmd, basedev, blksize);
L
Linus Torvalds 已提交
3952
	}
3953
	rq_for_each_segment(bv, req, iter) {
3954
		dst = page_address(bv.bv_page) + bv.bv_offset;
L
Linus Torvalds 已提交
3955 3956
		if (dasd_page_cache) {
			char *copy = kmem_cache_alloc(dasd_page_cache,
C
Christoph Lameter 已提交
3957
						      GFP_DMA | __GFP_NOWARN);
L
Linus Torvalds 已提交
3958
			if (copy && rq_data_dir(req) == WRITE)
3959
				memcpy(copy + bv.bv_offset, dst, bv.bv_len);
L
Linus Torvalds 已提交
3960
			if (copy)
3961
				dst = copy + bv.bv_offset;
L
Linus Torvalds 已提交
3962
		}
3963
		for (off = 0; off < bv.bv_len; off += blksize) {
L
Linus Torvalds 已提交
3964 3965 3966 3967 3968 3969 3970 3971 3972
			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);
3973 3974
					if (count < blksize &&
					    rq_data_dir(req) == READ)
L
Linus Torvalds 已提交
3975 3976 3977 3978 3979 3980
						memset(dst + count, 0xe5,
						       blksize - count);
				}
				ccw[-1].flags |= CCW_FLAG_CC;
				locate_record(ccw++, LO_data++,
					      trkid, recoffs + 1,
3981
					      1, rcmd, basedev, count);
L
Linus Torvalds 已提交
3982 3983 3984 3985 3986 3987 3988
			}
			/* 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,
3989
					      cmd, basedev, count);
L
Linus Torvalds 已提交
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007
			}
			/* 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++;
		}
	}
4008 4009
	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
4010
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4011 4012 4013
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
4014
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
4015
	cqr->lpm = dasd_path_get_ppm(startdev);
4016
	cqr->retries = startdev->default_retries;
4017
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
4018
	cqr->status = DASD_CQR_FILLED;
4019 4020 4021 4022 4023 4024 4025 4026

	/* Set flags to suppress output for expected errors */
	if (dasd_eckd_is_ese(basedev)) {
		set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
		set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);
		set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
	}

L
Linus Torvalds 已提交
4027 4028 4029
	return cqr;
}

4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
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;
4047
	struct bio_vec bv;
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
	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;

4079
	datasize = sizeof(struct PFX_eckd_data) + cidaw * sizeof(unsigned long);
4080 4081

	/* Allocate the ccw request. */
4082
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
4083
				   startdev, blk_mq_rq_to_pdu(req));
4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
	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;
4118
	idaw_dst = NULL;
4119 4120
	idaw_len = 0;
	rq_for_each_segment(bv, req, iter) {
4121 4122
		dst = page_address(bv.bv_page) + bv.bv_offset;
		seg_len = bv.bv_len;
4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
		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;
4139 4140 4141
				/* first idaw for a ccw may start anywhere */
				if (!idaw_dst)
					idaw_dst = dst;
4142
			}
4143 4144
			/* If we start a new idaw, we must make sure that it
			 * starts on an IDA_BLOCK_SIZE boundary.
4145 4146 4147 4148
			 * If we continue an idaw, we must make sure that the
			 * current segment begins where the so far accumulated
			 * idaw ends
			 */
4149 4150 4151 4152 4153 4154 4155
			if (!idaw_dst) {
				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
					dasd_sfree_request(cqr, startdev);
					return ERR_PTR(-ERANGE);
				} else
					idaw_dst = dst;
			}
4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179
			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);
4180
				idaw_dst = NULL;
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
				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;
4193
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
4194
	cqr->lpm = dasd_path_get_ppm(startdev);
4195
	cqr->retries = startdev->default_retries;
4196
	cqr->buildclk = get_tod_clock();
4197
	cqr->status = DASD_CQR_FILLED;
4198 4199 4200 4201 4202

	/* Set flags to suppress output for expected errors */
	if (dasd_eckd_is_ese(basedev))
		set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);

4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231
	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 */
4232 4233
	basepriv = basedev->private;
	startpriv = startdev->private;
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
	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 */
4244 4245 4246 4247
	if (startpriv->uid.type == UA_BASE_PAV_ALIAS)
		pfxdata.validity.verify_base = 1;

	if (startpriv->uid.type == UA_HYPER_PAV_ALIAS) {
4248
		pfxdata.validity.verify_base = 1;
4249
		pfxdata.validity.hyper_pav = 1;
4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266
	}

	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;
4267
		rc = set_timestamp(NULL, dedata, basedev);
4268 4269 4270 4271 4272
		dedata->ga_extended |= 0x42;
		lredata->operation.orientation = 0x0;
		lredata->operation.operation = 0x3F;
		lredata->extended_operation = 0x23;
		lredata->auxiliary.check_bytes = 0x2;
4273 4274 4275 4276 4277 4278 4279
		/*
		 * If XRC is supported the System Time Stamp is set. The
		 * validity of the time stamp must be reflected in the prefix
		 * data as well.
		 */
		if (dedata->ga_extended & 0x08 && dedata->ga_extended & 0x02)
			pfxdata.validity.time_stamp = 1; /* 'Time Stamp Valid' */
4280 4281
		pfx_cmd = DASD_ECKD_CCW_PFX;
		break;
4282 4283 4284 4285 4286 4287 4288 4289 4290 4291
	case DASD_ECKD_CCW_READ_COUNT_MT:
		dedata->mask.perm = 0x1;
		dedata->attributes.operation = DASD_BYPASS_CACHE;
		dedata->ga_extended |= 0x42;
		dedata->blk_size = blksize;
		lredata->operation.orientation = 0x2;
		lredata->operation.operation = 0x16;
		lredata->auxiliary.check_bytes = 0x01;
		pfx_cmd = DASD_ECKD_CCW_PFX_READ;
		break;
4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338
	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;
		}
	}

4339 4340 4341 4342 4343 4344 4345 4346 4347
	if (cmd == DASD_ECKD_CCW_READ_COUNT_MT) {
		lredata->auxiliary.length_valid = 0;
		lredata->auxiliary.length_scope = 0;
		lredata->sector = 0xff;
	} else {
		lredata->auxiliary.length_valid = 1;
		lredata->auxiliary.length_scope = 1;
		lredata->sector = sector;
	}
4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358
	lredata->auxiliary.imbedded_ccw_valid = 1;
	lredata->length = tlf;
	lredata->imbedded_ccw = cmd;
	lredata->count = count;
	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);
4359
	return PTR_ERR_OR_ZERO(dcw);
4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376
}

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;
4377
	struct bio_vec bv;
4378 4379 4380 4381 4382 4383 4384 4385 4386
	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;
4387 4388 4389 4390 4391 4392
	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;
4393
	int ret;
4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407

	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.
4408 4409
	 * In the case of write requests, additional tidaws may
	 * be needed when a segment crosses a track boundary.
4410 4411 4412 4413 4414 4415
	 */
	trkcount = last_trk - first_trk + 1;
	ctidaw = 0;
	rq_for_each_segment(bv, req, iter) {
		++ctidaw;
	}
4416 4417
	if (rq_data_dir(req) == WRITE)
		ctidaw += (last_trk - first_trk);
4418 4419 4420

	/* Allocate the ccw request. */
	itcw_size = itcw_calc_size(0, ctidaw, 0);
4421 4422
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev,
				   blk_mq_rq_to_pdu(req));
4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
	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);
4434
	if (IS_ERR(itcw)) {
4435 4436
		ret = -EINVAL;
		goto out_error;
4437
	}
4438 4439 4440 4441 4442 4443 4444 4445 4446 4447
	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.
		 */
4448 4449
		ret = -EAGAIN;
		goto out_error;
4450
	}
4451
	len_to_track_end = 0;
4452 4453 4454 4455 4456
	/*
	 * 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.
4457 4458
	 * 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.
4459
	 */
4460 4461 4462 4463
	if (rq_data_dir(req) == WRITE) {
		new_track = 1;
		recid = first_rec;
		rq_for_each_segment(bv, req, iter) {
4464 4465
			dst = page_address(bv.bv_page) + bv.bv_offset;
			seg_len = bv.bv_len;
4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
			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);
4488 4489 4490 4491
				if (IS_ERR(last_tidaw)) {
					ret = -EINVAL;
					goto out_error;
				}
4492 4493 4494 4495 4496
				dst += part_len;
			}
		}
	} else {
		rq_for_each_segment(bv, req, iter) {
4497
			dst = page_address(bv.bv_page) + bv.bv_offset;
4498
			last_tidaw = itcw_add_tidaw(itcw, 0x00,
4499
						    dst, bv.bv_len);
4500 4501 4502 4503
			if (IS_ERR(last_tidaw)) {
				ret = -EINVAL;
				goto out_error;
			}
4504
		}
4505
	}
4506 4507
	last_tidaw->flags |= TIDAW_FLAGS_LAST;
	last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
4508 4509 4510 4511 4512
	itcw_finalize(itcw);

	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4513
	cqr->cpmode = 1;
4514 4515 4516
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
4517
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
4518
	cqr->lpm = dasd_path_get_ppm(startdev);
4519
	cqr->retries = startdev->default_retries;
4520
	cqr->buildclk = get_tod_clock();
4521
	cqr->status = DASD_CQR_FILLED;
4522 4523 4524 4525 4526 4527 4528 4529

	/* Set flags to suppress output for expected errors */
	if (dasd_eckd_is_ese(basedev)) {
		set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
		set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);
		set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
	}

4530
	return cqr;
4531 4532 4533
out_error:
	dasd_sfree_request(cqr, startdev);
	return ERR_PTR(ret);
4534 4535 4536 4537 4538 4539
}

static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req)
{
4540
	int cmdrtd, cmdwtd;
4541
	int use_prefix;
4542
	int fcx_multitrack;
4543
	struct dasd_eckd_private *private;
4544 4545 4546 4547 4548 4549
	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;
4550
	unsigned int data_size;
4551 4552 4553
	struct dasd_ccw_req *cqr;

	basedev = block->base;
4554
	private = basedev->private;
4555 4556 4557 4558

	/* Calculate number of blocks/records per track. */
	blksize = block->bp_block;
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
4559 4560
	if (blk_per_trk == 0)
		return ERR_PTR(-EINVAL);
4561
	/* Calculate record id of first and last block. */
4562
	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
4563 4564
	first_offs = sector_div(first_trk, blk_per_trk);
	last_rec = last_trk =
4565
		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
4566 4567 4568
	last_offs = sector_div(last_trk, blk_per_trk);
	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);

4569 4570
	fcx_multitrack = private->features.feature[40] & 0x20;
	data_size = blk_rq_bytes(req);
4571 4572
	if (data_size % blksize)
		return ERR_PTR(-EINVAL);
4573 4574 4575
	/* tpm write request add CBC data on each track boundary */
	if (rq_data_dir(req) == WRITE)
		data_size += (last_trk - first_trk) * 4;
4576 4577 4578 4579 4580 4581 4582 4583 4584

	/* 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 */
4585 4586
	} else if ((data_size <= private->fcx_max_data)
		   && (fcx_multitrack || (first_trk == last_trk))) {
4587 4588 4589 4590 4591
		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);
4592 4593
		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
		    (PTR_ERR(cqr) != -ENOMEM))
4594 4595 4596 4597 4598 4599 4600 4601 4602
			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);
4603 4604
		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
		    (PTR_ERR(cqr) != -ENOMEM))
4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
			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;
}

4616 4617 4618
static struct dasd_ccw_req *dasd_eckd_build_cp_raw(struct dasd_device *startdev,
						   struct dasd_block *block,
						   struct request *req)
4619
{
4620 4621 4622 4623 4624
	sector_t start_padding_sectors, end_sector_offset, end_padding_sectors;
	unsigned int seg_len, len_to_track_end;
	unsigned int cidaw, cplength, datasize;
	sector_t first_trk, last_trk, sectors;
	struct dasd_eckd_private *base_priv;
4625 4626
	struct dasd_device *basedev;
	struct req_iterator iter;
4627 4628 4629 4630 4631 4632
	struct dasd_ccw_req *cqr;
	unsigned int first_offs;
	unsigned int trkcount;
	unsigned long *idaws;
	unsigned int size;
	unsigned char cmd;
4633
	struct bio_vec bv;
4634 4635 4636
	struct ccw1 *ccw;
	int use_prefix;
	void *data;
4637 4638 4639 4640
	char *dst;

	/*
	 * raw track access needs to be mutiple of 64k and on 64k boundary
4641 4642
	 * For read requests we can fix an incorrect alignment by padding
	 * the request with dummy pages.
4643
	 */
4644 4645 4646 4647 4648 4649 4650 4651 4652
	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,
4653
			      "raw write not track aligned (%llu,%llu) req %p",
4654
			      start_padding_sectors, end_padding_sectors, req);
4655
		return ERR_PTR(-EINVAL);
4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667
	}

	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;
4668 4669
	else
		return ERR_PTR(-EINVAL);
4670 4671 4672 4673 4674 4675 4676 4677

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

	/*
4678 4679
	 * struct PFX_eckd_data and struct LRE_eckd_data can have up to 2 bytes
	 * of extended parameter. This is needed for write full track.
4680
	 */
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
	base_priv = basedev->private;
	use_prefix = base_priv->features.feature[8] & 0x01;
	if (use_prefix) {
		cplength = 1 + trkcount;
		size = sizeof(struct PFX_eckd_data) + 2;
	} else {
		cplength = 2 + trkcount;
		size = sizeof(struct DE_eckd_data) +
			sizeof(struct LRE_eckd_data) + 2;
	}
	size = ALIGN(size, 8);
4692

4693
	datasize = size + cidaw * sizeof(unsigned long);
4694 4695 4696

	/* Allocate the ccw request. */
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
4697
				   datasize, startdev, blk_mq_rq_to_pdu(req));
4698
	if (IS_ERR(cqr))
4699 4700
		return cqr;

4701
	ccw = cqr->cpaddr;
4702
	data = cqr->data;
4703

4704 4705 4706 4707 4708 4709 4710 4711 4712 4713
	if (use_prefix) {
		prefix_LRE(ccw++, data, first_trk, last_trk, cmd, basedev,
			   startdev, 1, first_offs + 1, trkcount, 0, 0);
	} else {
		define_extent(ccw++, data, first_trk, last_trk, cmd, basedev, 0);
		ccw[-1].flags |= CCW_FLAG_CC;

		data += sizeof(struct DE_eckd_data);
		locate_record_ext(ccw++, data, first_trk, first_offs + 1,
				  trkcount, cmd, basedev, 0, 0);
4714 4715
	}

4716
	idaws = (unsigned long *)(cqr->data + size);
4717
	len_to_track_end = 0;
4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731
	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);
	}
4732
	rq_for_each_segment(bv, req, iter) {
4733 4734
		dst = page_address(bv.bv_page) + bv.bv_offset;
		seg_len = bv.bv_len;
4735 4736
		if (cmd == DASD_ECKD_CCW_READ_TRACK)
			memset(dst, 0, seg_len);
4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751
		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);
	}
4752 4753
	for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
		idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
4754 4755 4756 4757 4758 4759 4760
	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;
4761
	cqr->lpm = dasd_path_get_ppm(startdev);
4762
	cqr->retries = startdev->default_retries;
4763
	cqr->buildclk = get_tod_clock();
4764 4765 4766 4767 4768 4769
	cqr->status = DASD_CQR_FILLED;

	return cqr;
}


L
Linus Torvalds 已提交
4770 4771 4772 4773 4774
static int
dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
{
	struct dasd_eckd_private *private;
	struct ccw1 *ccw;
4775
	struct req_iterator iter;
4776
	struct bio_vec bv;
L
Linus Torvalds 已提交
4777 4778 4779
	char *dst, *cda;
	unsigned int blksize, blk_per_trk, off;
	sector_t recid;
4780
	int status;
L
Linus Torvalds 已提交
4781 4782 4783

	if (!dasd_page_cache)
		goto out;
4784
	private = cqr->block->base->private;
4785
	blksize = cqr->block->bp_block;
L
Linus Torvalds 已提交
4786
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
4787
	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
L
Linus Torvalds 已提交
4788 4789 4790 4791 4792
	ccw = cqr->cpaddr;
	/* Skip over define extent & locate record. */
	ccw++;
	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
		ccw++;
4793
	rq_for_each_segment(bv, req, iter) {
4794 4795
		dst = page_address(bv.bv_page) + bv.bv_offset;
		for (off = 0; off < bv.bv_len; off += blksize) {
L
Linus Torvalds 已提交
4796 4797 4798 4799 4800 4801 4802 4803 4804 4805
			/* 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)
4806
						memcpy(dst, cda, bv.bv_len);
L
Linus Torvalds 已提交
4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817
					kmem_cache_free(dasd_page_cache,
					    (void *)((addr_t)cda & PAGE_MASK));
				}
				dst = NULL;
			}
			ccw++;
			recid++;
		}
	}
out:
	status = cqr->status == DASD_CQR_DONE;
4818
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
4819 4820 4821
	return status;
}

4822
/*
4823
 * Modify ccw/tcw in cqr so it can be started on a base device.
4824 4825 4826 4827 4828 4829 4830 4831 4832
 *
 * 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;
4833 4834 4835 4836 4837 4838 4839 4840 4841
	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];
4842 4843
		pfxdata->validity.verify_base = 0;
		pfxdata->validity.hyper_pav = 0;
4844 4845 4846 4847 4848 4849 4850
	} 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;
		}
4851 4852 4853 4854 4855 4856 4857 4858 4859 4860
	}
}

#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;
4861
	struct dasd_device *startdev;
4862
	unsigned long flags;
4863
	struct dasd_ccw_req *cqr;
4864

4865
	startdev = dasd_alias_get_start_dev(base);
4866 4867
	if (!startdev)
		startdev = base;
4868
	private = startdev->private;
4869 4870 4871 4872 4873
	if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
		return ERR_PTR(-EBUSY);

	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
	private->count++;
4874
	if ((base->features & DASD_FEATURE_USERAW))
4875
		cqr = dasd_eckd_build_cp_raw(startdev, block, req);
4876 4877
	else
		cqr = dasd_eckd_build_cp(startdev, block, req);
4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
	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);
4891
	private = cqr->memdev->private;
4892 4893 4894 4895 4896
	private->count--;
	spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
	return dasd_eckd_free_cp(cqr, req);
}

L
Linus Torvalds 已提交
4897 4898 4899 4900
static int
dasd_eckd_fill_info(struct dasd_device * device,
		    struct dasd_information2_t * info)
{
4901
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
4902 4903 4904 4905

	info->label_block = 2;
	info->FBA_layout = private->uses_cdl ? 0 : 1;
	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
4906
	info->characteristics_size = sizeof(private->rdc_data);
L
Linus Torvalds 已提交
4907
	memcpy(info->characteristics, &private->rdc_data,
4908
	       sizeof(private->rdc_data));
4909 4910 4911 4912
	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 已提交
4913 4914 4915 4916 4917 4918 4919 4920 4921
	return 0;
}

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

/*
 * Release device ioctl.
4922
 * Buils a channel programm to releases a prior reserved
L
Linus Torvalds 已提交
4923 4924 4925
 * (see dasd_eckd_reserve) device.
 */
static int
4926
dasd_eckd_release(struct dasd_device *device)
L
Linus Torvalds 已提交
4927 4928 4929
{
	struct dasd_ccw_req *cqr;
	int rc;
4930
	struct ccw1 *ccw;
4931
	int useglobal;
L
Linus Torvalds 已提交
4932 4933 4934 4935

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

4936
	useglobal = 0;
4937
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device, NULL);
L
Linus Torvalds 已提交
4938
	if (IS_ERR(cqr)) {
4939 4940 4941 4942 4943 4944 4945 4946 4947
		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 已提交
4948
	}
4949 4950 4951 4952 4953
	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;
4954 4955
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
4956
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4957
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4958
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
4959
	cqr->expires = 2 * HZ;
4960
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
4961 4962 4963
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
4964 4965
	if (!rc)
		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
4966

4967 4968 4969 4970
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
4971 4972 4973 4974 4975 4976
	return rc;
}

/*
 * Reserve device ioctl.
 * Options are set to 'synchronous wait for interrupt' and
4977 4978
 * 'timeout the request'. This leads to a terminate IO if
 * the interrupt is outstanding for a certain time.
L
Linus Torvalds 已提交
4979 4980
 */
static int
4981
dasd_eckd_reserve(struct dasd_device *device)
L
Linus Torvalds 已提交
4982 4983 4984
{
	struct dasd_ccw_req *cqr;
	int rc;
4985
	struct ccw1 *ccw;
4986
	int useglobal;
L
Linus Torvalds 已提交
4987 4988 4989 4990

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

4991
	useglobal = 0;
4992
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device, NULL);
L
Linus Torvalds 已提交
4993
	if (IS_ERR(cqr)) {
4994 4995 4996 4997 4998 4999 5000 5001 5002
		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 已提交
5003
	}
5004 5005 5006 5007 5008
	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;
5009 5010
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
5011
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5012
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5013
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
5014
	cqr->expires = 2 * HZ;
5015
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
5016 5017 5018
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
5019 5020
	if (!rc)
		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
5021

5022 5023 5024 5025
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
5026 5027 5028 5029 5030
	return rc;
}

/*
 * Steal lock ioctl - unconditional reserve device.
5031
 * Buils a channel programm to break a device's reservation.
L
Linus Torvalds 已提交
5032 5033 5034
 * (unconditional reserve)
 */
static int
5035
dasd_eckd_steal_lock(struct dasd_device *device)
L
Linus Torvalds 已提交
5036 5037 5038
{
	struct dasd_ccw_req *cqr;
	int rc;
5039
	struct ccw1 *ccw;
5040
	int useglobal;
L
Linus Torvalds 已提交
5041 5042 5043 5044

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

5045
	useglobal = 0;
5046
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device, NULL);
L
Linus Torvalds 已提交
5047
	if (IS_ERR(cqr)) {
5048 5049 5050 5051 5052 5053 5054 5055 5056
		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 已提交
5057
	}
5058 5059 5060 5061 5062
	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;
5063 5064
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
5065
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5066
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5067
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
5068
	cqr->expires = 2 * HZ;
5069
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
5070 5071 5072
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
5073 5074
	if (!rc)
		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
5075

5076 5077 5078 5079
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
5080 5081 5082
	return rc;
}

5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105
/*
 * 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,
5106 5107
				   sizeof(struct dasd_snid_data), device,
				   NULL);
5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
	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);
5128
	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
5129 5130
	cqr->retries = 5;
	cqr->expires = 10 * HZ;
5131
	cqr->buildclk = get_tod_clock();
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151
	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 已提交
5152 5153 5154 5155
/*
 * Read performance statistics
 */
static int
5156
dasd_eckd_performance(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
5157 5158 5159 5160 5161 5162 5163
{
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_perf_stats_t *stats;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

5164
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
5165 5166
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_perf_stats_t)),
5167
				   device, NULL);
L
Linus Torvalds 已提交
5168
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
5169
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
L
Linus Torvalds 已提交
5170 5171 5172
			    "Could not allocate initialization request");
		return PTR_ERR(cqr);
	}
5173 5174
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
5175
	cqr->retries = 0;
5176
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
L
Linus Torvalds 已提交
5177 5178 5179 5180
	cqr->expires = 10 * HZ;

	/* Prepare for Read Subsystem Data */
	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5181
	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
L
Linus Torvalds 已提交
5182
	prssdp->order = PSF_ORDER_PRSSD;
5183
	prssdp->suborder = 0x01;	/* Performance Statistics */
L
Linus Torvalds 已提交
5184 5185 5186 5187
	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */

	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
5188
	ccw->count = sizeof(struct dasd_psf_prssd_data);
L
Linus Torvalds 已提交
5189 5190 5191 5192 5193
	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);
5194
	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
L
Linus Torvalds 已提交
5195 5196 5197

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

5201
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
5202 5203 5204 5205 5206
	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);
5207 5208 5209
		if (copy_to_user(argp, stats,
				 sizeof(struct dasd_rssd_perf_stats_t)))
			rc = -EFAULT;
L
Linus Torvalds 已提交
5210
	}
5211
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
5212 5213 5214 5215 5216 5217 5218 5219
	return rc;
}

/*
 * Get attributes (cache operations)
 * Returnes the cache attributes used in Define Extend (DE).
 */
static int
5220
dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
5221
{
5222
	struct dasd_eckd_private *private = device->private;
5223
	struct attrib_data_t attrib = private->attrib;
L
Linus Torvalds 已提交
5224 5225 5226 5227
	int rc;

        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
5228
	if (!argp)
L
Linus Torvalds 已提交
5229 5230
                return -EINVAL;

5231 5232
	rc = 0;
	if (copy_to_user(argp, (long *) &attrib,
5233
			 sizeof(struct attrib_data_t)))
5234
		rc = -EFAULT;
L
Linus Torvalds 已提交
5235 5236 5237 5238 5239 5240 5241 5242 5243

	return rc;
}

/*
 * Set attributes (cache operations)
 * Stores the attributes for cache operation to be used in Define Extend (DE).
 */
static int
5244
dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
5245
{
5246
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
5247 5248 5249 5250
	struct attrib_data_t attrib;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
5251
	if (!argp)
L
Linus Torvalds 已提交
5252 5253
		return -EINVAL;

5254
	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
L
Linus Torvalds 已提交
5255 5256 5257
		return -EFAULT;
	private->attrib = attrib;

S
Stefan Haberland 已提交
5258 5259 5260
	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 已提交
5261 5262 5263
	return 0;
}

5264 5265 5266 5267 5268 5269 5270 5271 5272 5273
/*
 * 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;
5274
	char psf0, psf1;
5275 5276
	int rc;

5277 5278 5279 5280
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
		return -EACCES;
	psf0 = psf1 = 0;

5281 5282 5283 5284
	/* Copy parms from caller */
	rc = -EFAULT;
	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
		goto out;
5285
	if (is_compat_task()) {
5286
		/* Make sure pointers are sane even on 31 bit. */
5287
		rc = -EINVAL;
5288 5289 5290 5291 5292 5293
		if ((usrparm.psf_data >> 32) != 0)
			goto out;
		if ((usrparm.rssd_result >> 32) != 0)
			goto out;
		usrparm.psf_data &= 0x7fffffffULL;
		usrparm.rssd_result &= 0x7fffffffULL;
5294
	}
5295 5296 5297 5298 5299 5300 5301 5302
	/* at least 2 bytes are accessed and should be allocated */
	if (usrparm.psf_data_len < 2) {
		DBF_DEV_EVENT(DBF_WARNING, device,
			      "Symmetrix ioctl invalid data length %d",
			      usrparm.psf_data_len);
		rc = -EINVAL;
		goto out;
	}
5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316
	/* 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;
5317 5318
	psf0 = psf_data[0];
	psf1 = psf_data[1];
5319 5320

	/* setup CCWs for PSF + RSSD */
5321
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2, 0, device, NULL);
5322
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
5323
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
5324 5325 5326 5327 5328 5329 5330 5331 5332
			"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;
5333
	cqr->buildclk = get_tod_clock();
5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368
	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:
5369 5370 5371
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
		      (int) psf0, (int) psf1, rc);
5372 5373 5374
	return rc;
}

5375
static int
5376
dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
5377
{
5378 5379
	struct dasd_device *device = block->base;

5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392
	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);
5393 5394
	case BIODASDSNID:
		return dasd_eckd_snid(device, argp);
5395 5396
	case BIODASDSYMMIO:
		return dasd_symm_io(device, argp);
5397
	default:
5398
		return -ENOTTY;
5399 5400 5401
	}
}

H
Horst Hummel 已提交
5402 5403 5404 5405
/*
 * Dump the range of CCWs into 'page' buffer
 * and return number of printed chars.
 */
5406
static int
H
Horst Hummel 已提交
5407 5408 5409 5410 5411 5412 5413
dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
{
	int len, count;
	char *datap;

	len = 0;
	while (from <= to) {
5414
		len += sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435
			       " 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 已提交
5436
static void
5437 5438
dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
			 char *reason)
S
Stefan Haberland 已提交
5439 5440
{
	u64 *sense;
5441
	u64 *stat;
5442 5443

	sense = (u64 *) dasd_get_sense(irb);
5444
	stat = (u64 *) &irb->scsw;
S
Stefan Haberland 已提交
5445
	if (sense) {
5446 5447 5448
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
			      "%016llx %016llx %016llx %016llx",
			      reason, *stat, *((u32 *) (stat + 1)),
S
Stefan Haberland 已提交
5449
			      sense[0], sense[1], sense[2], sense[3]);
S
Stefan Haberland 已提交
5450
	} else {
5451 5452 5453
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
			      reason, *stat, *((u32 *) (stat + 1)),
			      "NO VALID SENSE");
S
Stefan Haberland 已提交
5454 5455 5456
	}
}

L
Linus Torvalds 已提交
5457 5458 5459 5460
/*
 * Print sense data and related channel program.
 * Parts are printed because printk buffer is only 1024 bytes.
 */
5461
static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
5462
				 struct dasd_ccw_req *req, struct irb *irb)
L
Linus Torvalds 已提交
5463 5464
{
	char *page;
H
Horst Hummel 已提交
5465 5466
	struct ccw1 *first, *last, *fail, *from, *to;
	int len, sl, sct;
L
Linus Torvalds 已提交
5467 5468 5469

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
5470 5471
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "No memory to dump sense data\n");
L
Linus Torvalds 已提交
5472 5473
		return;
	}
H
Horst Hummel 已提交
5474
	/* dump the sense data */
5475
	len = sprintf(page, PRINTK_HEADER
L
Linus Torvalds 已提交
5476
		      " I/O status report for device %s:\n",
5477
		      dev_name(&device->cdev->dev));
5478
	len += sprintf(page + len, PRINTK_HEADER
5479 5480 5481 5482 5483 5484
		       " 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);
5485
	len += sprintf(page + len, PRINTK_HEADER
L
Linus Torvalds 已提交
5486
		       " device %s: Failing CCW: %p\n",
5487
		       dev_name(&device->cdev->dev),
5488
		       (void *) (addr_t) irb->scsw.cmd.cpa);
L
Linus Torvalds 已提交
5489 5490
	if (irb->esw.esw0.erw.cons) {
		for (sl = 0; sl < 4; sl++) {
5491
			len += sprintf(page + len, PRINTK_HEADER
L
Linus Torvalds 已提交
5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503
				       " 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 */
5504
			sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
5505 5506 5507 5508
				" 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 已提交
5509 5510
		} else {
			/* 32 Byte Sense Data */
5511
			sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
5512 5513 5514
				" 32 Byte: Format: %x "
				"Exception class %x\n",
				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
L
Linus Torvalds 已提交
5515 5516
		}
	} else {
5517
		sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
5518
			" SORRY - NO VALID SENSE AVAILABLE\n");
L
Linus Torvalds 已提交
5519
	}
5520
	printk(KERN_ERR "%s", page);
H
Horst Hummel 已提交
5521

5522 5523 5524 5525 5526 5527 5528
	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);
5529
		len = sprintf(page, PRINTK_HEADER
5530 5531
			      " Related CP in req: %p\n", req);
		dasd_eckd_dump_ccw_range(first, to, page + len);
5532
		printk(KERN_ERR "%s", page);
L
Linus Torvalds 已提交
5533

5534 5535 5536 5537
		/* print failing CCW area (maximum 4) */
		/* scsw->cda is either valid or zero  */
		len = 0;
		from = ++to;
5538 5539
		fail = (struct ccw1 *)(addr_t)
				irb->scsw.cmd.cpa; /* failing CCW */
5540 5541
		if (from <  fail - 2) {
			from = fail - 2;     /* there is a gap - print header */
5542
			len += sprintf(page, PRINTK_HEADER "......\n");
5543 5544 5545 5546 5547 5548 5549 5550
		}
		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 */
5551
			len += sprintf(page + len, PRINTK_HEADER "......\n");
5552 5553 5554
		}
		len += dasd_eckd_dump_ccw_range(from, last, page + len);
		if (len > 0)
5555
			printk(KERN_ERR "%s", page);
L
Linus Torvalds 已提交
5556 5557 5558 5559
	}
	free_page((unsigned long) page);
}

5560 5561 5562 5563 5564 5565 5566 5567 5568 5569

/*
 * 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;
5570
	u8 *sense, *rcq;
5571 5572 5573

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
5574
		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
5575 5576 5577 5578
			    "No memory to dump sense data");
		return;
	}
	/* dump the sense data */
5579
	len = sprintf(page, PRINTK_HEADER
5580 5581
		      " I/O status report for device %s:\n",
		      dev_name(&device->cdev->dev));
5582
	len += sprintf(page + len, PRINTK_HEADER
5583 5584 5585 5586 5587
		       " 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),
5588 5589
		       irb->scsw.tm.fcxs,
		       (irb->scsw.tm.ifob << 7) | irb->scsw.tm.sesq,
5590
		       req ? req->intrc : 0);
5591
	len += sprintf(page + len, PRINTK_HEADER
5592 5593 5594 5595 5596 5597
		       " device %s: Failing TCW: %p\n",
		       dev_name(&device->cdev->dev),
		       (void *) (addr_t) irb->scsw.tm.tcw);

	tsb = NULL;
	sense = NULL;
5598
	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
5599 5600 5601
		tsb = tcw_get_tsb(
			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);

5602
	if (tsb) {
5603
		len += sprintf(page + len, PRINTK_HEADER
5604
			       " tsb->length %d\n", tsb->length);
5605
		len += sprintf(page + len, PRINTK_HEADER
5606
			       " tsb->flags %x\n", tsb->flags);
5607
		len += sprintf(page + len, PRINTK_HEADER
5608
			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
5609
		len += sprintf(page + len, PRINTK_HEADER
5610 5611
			       " tsb->count %d\n", tsb->count);
		residual = tsb->count - 28;
5612
		len += sprintf(page + len, PRINTK_HEADER
5613 5614 5615 5616
			       " residual %d\n", residual);

		switch (tsb->flags & 0x07) {
		case 1:	/* tsa_iostat */
5617
			len += sprintf(page + len, PRINTK_HEADER
5618 5619
			       " tsb->tsa.iostat.dev_time %d\n",
				       tsb->tsa.iostat.dev_time);
5620
			len += sprintf(page + len, PRINTK_HEADER
5621 5622
			       " tsb->tsa.iostat.def_time %d\n",
				       tsb->tsa.iostat.def_time);
5623
			len += sprintf(page + len, PRINTK_HEADER
5624 5625
			       " tsb->tsa.iostat.queue_time %d\n",
				       tsb->tsa.iostat.queue_time);
5626
			len += sprintf(page + len, PRINTK_HEADER
5627 5628
			       " tsb->tsa.iostat.dev_busy_time %d\n",
				       tsb->tsa.iostat.dev_busy_time);
5629
			len += sprintf(page + len, PRINTK_HEADER
5630 5631 5632 5633 5634
			       " tsb->tsa.iostat.dev_act_time %d\n",
				       tsb->tsa.iostat.dev_act_time);
			sense = tsb->tsa.iostat.sense;
			break;
		case 2: /* ts_ddpc */
5635
			len += sprintf(page + len, PRINTK_HEADER
5636
			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
5637
			for (sl = 0; sl < 2; sl++) {
5638
				len += sprintf(page + len, PRINTK_HEADER
5639 5640 5641
					       " tsb->tsa.ddpc.rcq %2d-%2d: ",
					       (8 * sl), ((8 * sl) + 7));
				rcq = tsb->tsa.ddpc.rcq;
5642 5643
				for (sct = 0; sct < 8; sct++) {
					len += sprintf(page + len, " %02x",
5644
						       rcq[8 * sl + sct]);
5645 5646 5647 5648 5649 5650
				}
				len += sprintf(page + len, "\n");
			}
			sense = tsb->tsa.ddpc.sense;
			break;
		case 3: /* tsa_intrg */
5651
			len += sprintf(page + len, PRINTK_HEADER
5652
				      " tsb->tsa.intrg.: not supported yet\n");
5653 5654 5655 5656 5657
			break;
		}

		if (sense) {
			for (sl = 0; sl < 4; sl++) {
5658
				len += sprintf(page + len, PRINTK_HEADER
5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669
					       " 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 */
5670
				sprintf(page + len, PRINTK_HEADER
5671 5672 5673 5674 5675 5676
					" 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 */
5677
				sprintf(page + len, PRINTK_HEADER
5678 5679 5680 5681 5682
					" 32 Byte: Format: %x "
					"Exception class %x\n",
					sense[6] & 0x0f, sense[22] >> 4);
			}
		} else {
5683
			sprintf(page + len, PRINTK_HEADER
5684 5685 5686
				" SORRY - NO VALID SENSE AVAILABLE\n");
		}
	} else {
5687
		sprintf(page + len, PRINTK_HEADER
5688 5689
			" SORRY - NO TSB DATA AVAILABLE\n");
	}
5690
	printk(KERN_ERR "%s", page);
5691 5692 5693 5694 5695 5696
	free_page((unsigned long) page);
}

static void dasd_eckd_dump_sense(struct dasd_device *device,
				 struct dasd_ccw_req *req, struct irb *irb)
{
5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711
	u8 *sense = dasd_get_sense(irb);

	if (scsw_is_tm(&irb->scsw)) {
		/*
		 * In some cases the 'File Protected' or 'Incorrect Length'
		 * error might be expected and log messages shouldn't be written
		 * then. Check if the according suppress bit is set.
		 */
		if (sense && (sense[1] & SNS1_FILE_PROTECTED) &&
		    test_bit(DASD_CQR_SUPPRESS_FP, &req->flags))
			return;
		if (scsw_cstat(&irb->scsw) == 0x40 &&
		    test_bit(DASD_CQR_SUPPRESS_IL, &req->flags))
			return;

5712
		dasd_eckd_dump_sense_tcw(device, req, irb);
5713 5714
	} else {
		/*
5715 5716 5717
		 * In some cases the 'Command Reject' or 'No Record Found'
		 * error might be expected and log messages shouldn't be
		 * written then. Check if the according suppress bit is set.
5718
		 */
5719 5720 5721 5722
		if (sense && sense[0] & SNS0_CMD_REJECT &&
		    test_bit(DASD_CQR_SUPPRESS_CR, &req->flags))
			return;

5723 5724 5725 5726
		if (sense && sense[1] & SNS1_NO_REC_FOUND &&
		    test_bit(DASD_CQR_SUPPRESS_NRF, &req->flags))
			return;

5727
		dasd_eckd_dump_sense_ccw(device, req, irb);
5728
	}
5729 5730
}

5731
static int dasd_eckd_pm_freeze(struct dasd_device *device)
5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743
{
	/*
	 * 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;
}

5744
static int dasd_eckd_restore_device(struct dasd_device *device)
5745
{
5746
	struct dasd_eckd_private *private = device->private;
5747
	struct dasd_eckd_characteristics temp_rdc_data;
5748
	int rc;
5749
	struct dasd_uid temp_uid;
S
Stefan Haberland 已提交
5750
	unsigned long flags;
5751
	unsigned long cqr_flags = 0;
5752 5753

	/* Read Configuration Data */
5754 5755 5756 5757 5758 5759
	rc = dasd_eckd_read_conf(device);
	if (rc) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read configuration data failed, rc=%d", rc);
		goto out_err;
	}
5760

5761 5762 5763 5764
	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);
5765
	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
S
Stefan Haberland 已提交
5766 5767
		dev_err(&device->cdev->dev, "The UID of the DASD has "
			"changed\n");
5768
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
5769 5770 5771 5772
	if (rc)
		goto out_err;

	/* register lcu with alias handling, enable PAV if this is a new lcu */
5773 5774
	rc = dasd_alias_make_device_known_to_lcu(device);
	if (rc)
5775
		goto out_err;
5776 5777 5778

	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
	dasd_eckd_validate_server(device, cqr_flags);
5779 5780

	/* RE-Read Configuration Data */
5781 5782 5783 5784 5785 5786
	rc = dasd_eckd_read_conf(device);
	if (rc) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
			"Read configuration data failed, rc=%d", rc);
		goto out_err2;
	}
5787 5788

	/* Read Feature Codes */
5789
	dasd_eckd_read_features(device);
5790

5791
	/* Read Volume Information */
5792
	dasd_eckd_read_vol_info(device);
5793 5794

	/* Read Extent Pool Information */
5795
	dasd_eckd_read_ext_pool_info(device);
5796

5797
	/* Read Device Characteristics */
5798
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
5799
					 &temp_rdc_data, 64);
5800
	if (rc) {
5801 5802
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
5803
		goto out_err2;
5804
	}
S
Stefan Haberland 已提交
5805
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
5806
	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
S
Stefan Haberland 已提交
5807
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
5808 5809 5810 5811 5812 5813

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

	return 0;

5814 5815
out_err2:
	dasd_alias_disconnect_device_from_lcu(device);
5816
out_err:
5817
	return -1;
5818 5819
}

5820 5821
static int dasd_eckd_reload_device(struct dasd_device *device)
{
5822
	struct dasd_eckd_private *private = device->private;
5823
	int rc, old_base;
5824 5825 5826
	char print_uid[60];
	struct dasd_uid uid;
	unsigned long flags;
5827

5828 5829 5830 5831 5832 5833
	/*
	 * remove device from alias handling to prevent new requests
	 * from being scheduled on the wrong alias device
	 */
	dasd_alias_remove_device(device);

5834
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
5835
	old_base = private->uid.base_unit_addr;
5836 5837
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);

5838 5839 5840 5841 5842
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err;

5843
	rc = dasd_eckd_generate_uid(device);
5844 5845 5846 5847 5848 5849 5850 5851
	if (rc)
		goto out_err;
	/*
	 * update unit address configuration and
	 * add device to alias management
	 */
	dasd_alias_update_add_device(device);

5852 5853 5854 5855 5856 5857 5858
	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);
5859
		else
5860 5861 5862
			snprintf(print_uid, sizeof(print_uid),
				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
				 uid.ssid, uid.base_unit_addr);
5863 5864 5865

		dev_info(&device->cdev->dev,
			 "An Alias device was reassigned to a new base device "
5866
			 "with UID: %s\n", print_uid);
5867 5868 5869 5870 5871 5872 5873
	}
	return 0;

out_err:
	return -1;
}

5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886
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_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

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

5894 5895
	cqr->lpm = lpum;
retry:
5896 5897 5898 5899 5900
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->expires = 10 * HZ;
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5901 5902 5903 5904 5905
	/* 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;
5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939

	/* 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));
5940 5941 5942 5943 5944 5945 5946 5947
	} else if (cqr->lpm) {
		/*
		 * on z/VM we might not be able to do I/O on the requested path
		 * but instead we get the required information on any path
		 * so retry with open path mask
		 */
		cqr->lpm = 0;
		goto retry;
5948 5949 5950 5951 5952 5953 5954 5955
	} else
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Reading messages failed with rc=%d\n"
				, rc);
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}

5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969
static int dasd_eckd_query_host_access(struct dasd_device *device,
				       struct dasd_psf_query_host_access *data)
{
	struct dasd_eckd_private *private = device->private;
	struct dasd_psf_query_host_access *host_access;
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

	/* not available for HYPER PAV alias devices */
	if (!device->block && private->lcu->pav == HYPER_PAV)
		return -EOPNOTSUPP;

5970 5971 5972 5973
	/* may not be supported by the storage server */
	if (!(private->features.feature[14] & 0x80))
		return -EOPNOTSUPP;

5974 5975
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
				   sizeof(struct dasd_psf_prssd_data) + 1,
5976
				   device, NULL);
5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020
	if (IS_ERR(cqr)) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate read message buffer request");
		return PTR_ERR(cqr);
	}
	host_access = kzalloc(sizeof(*host_access), GFP_KERNEL | GFP_DMA);
	if (!host_access) {
		dasd_sfree_request(cqr, device);
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate host_access buffer");
		return -ENOMEM;
	}
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->retries = 256;
	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 = PSF_SUBORDER_QHA;	/* query host access */
	/* LSS and Volume that will be queried */
	prssdp->lss = private->ned->ID;
	prssdp->volume = private->ned->unit_addr;
	/* 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 - query host access */
	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
	ccw->count = sizeof(struct dasd_psf_query_host_access);
	ccw->flags |= CCW_FLAG_SLI;
	ccw->cda = (__u32)(addr_t) host_access;

	cqr->buildclk = get_tod_clock();
	cqr->status = DASD_CQR_FILLED;
6021 6022
	/* the command might not be supported, suppress error message */
	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);
6023
	rc = dasd_sleep_on_interruptible(cqr);
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081
	if (rc == 0) {
		*data = *host_access;
	} else {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Reading host access data failed with rc=%d\n",
				rc);
		rc = -EOPNOTSUPP;
	}

	dasd_sfree_request(cqr, cqr->memdev);
	kfree(host_access);
	return rc;
}
/*
 * return number of grouped devices
 */
static int dasd_eckd_host_access_count(struct dasd_device *device)
{
	struct dasd_psf_query_host_access *access;
	struct dasd_ckd_path_group_entry *entry;
	struct dasd_ckd_host_information *info;
	int count = 0;
	int rc, i;

	access = kzalloc(sizeof(*access), GFP_NOIO);
	if (!access) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate access buffer");
		return -ENOMEM;
	}
	rc = dasd_eckd_query_host_access(device, access);
	if (rc) {
		kfree(access);
		return rc;
	}

	info = (struct dasd_ckd_host_information *)
		access->host_access_information;
	for (i = 0; i < info->entry_count; i++) {
		entry = (struct dasd_ckd_path_group_entry *)
			(info->entry + i * info->entry_size);
		if (entry->status_flags & DASD_ECKD_PG_GROUPED)
			count++;
	}

	kfree(access);
	return count;
}

/*
 * write host access information to a sequential file
 */
static int dasd_hosts_print(struct dasd_device *device, struct seq_file *m)
{
	struct dasd_psf_query_host_access *access;
	struct dasd_ckd_path_group_entry *entry;
	struct dasd_ckd_host_information *info;
	char sysplex[9] = "";
C
Christophe JAILLET 已提交
6082
	int rc, i;
6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101

	access = kzalloc(sizeof(*access), GFP_NOIO);
	if (!access) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate access buffer");
		return -ENOMEM;
	}
	rc = dasd_eckd_query_host_access(device, access);
	if (rc) {
		kfree(access);
		return rc;
	}

	info = (struct dasd_ckd_host_information *)
		access->host_access_information;
	for (i = 0; i < info->entry_count; i++) {
		entry = (struct dasd_ckd_path_group_entry *)
			(info->entry + i * info->entry_size);
		/* PGID */
C
Christophe JAILLET 已提交
6102
		seq_printf(m, "pgid %*phN\n", 11, entry->pgid);
6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119
		/* FLAGS */
		seq_printf(m, "status_flags %02x\n", entry->status_flags);
		/* SYSPLEX NAME */
		memcpy(&sysplex, &entry->sysplex_name, sizeof(sysplex) - 1);
		EBCASC(sysplex, sizeof(sysplex));
		seq_printf(m, "sysplex_name %8s\n", sysplex);
		/* SUPPORTED CYLINDER */
		seq_printf(m, "supported_cylinder %d\n", entry->cylinder);
		/* TIMESTAMP */
		seq_printf(m, "timestamp %lu\n", (unsigned long)
			   entry->timestamp);
	}
	kfree(access);

	return 0;
}

6120 6121 6122 6123 6124
/*
 * Perform Subsystem Function - CUIR response
 */
static int
dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
6125
			    __u32 message_id, __u8 lpum)
6126 6127
{
	struct dasd_psf_cuir_response *psf_cuir;
6128
	int pos = pathmask_to_pos(lpum);
6129 6130 6131 6132 6133
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
6134 6135
				   sizeof(struct dasd_psf_cuir_response),
				   device, NULL);
6136 6137 6138 6139 6140 6141 6142 6143 6144 6145

	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;
6146
	psf_cuir->chpid = device->path[pos].chpid;
6147
	psf_cuir->message_id = message_id;
6148 6149
	psf_cuir->cssid = device->path[pos].cssid;
	psf_cuir->ssid = device->path[pos].ssid;
6150 6151 6152
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->cda = (__u32)(addr_t)psf_cuir;
6153
	ccw->flags = CCW_FLAG_SLI;
6154 6155 6156 6157 6158 6159 6160 6161 6162
	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;
6163
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
6164 6165 6166 6167 6168 6169 6170

	rc = dasd_sleep_on(cqr);

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

6171 6172 6173 6174 6175 6176 6177 6178
/*
 * 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)
6179
{
6180 6181
	struct dasd_conf_data *conf_data;
	int path, pos;
6182

6183 6184 6185
	if (cuir->record_selector == 0)
		goto out;
	for (path = 0x80, pos = 0; path; path >>= 1, pos++) {
6186
		conf_data = device->path[pos].conf_data;
6187 6188 6189
		if (conf_data->gneq.record_selector ==
		    cuir->record_selector)
			return conf_data;
6190
	}
6191
out:
6192
	return device->path[pathmask_to_pos(lpum)].conf_data;
6193 6194 6195
}

/*
6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231
 * 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_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;

	/* 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;

6232
	for (path = 0; path < 8; path++) {
6233 6234
		/* initialise data per path */
		bitmask = mask;
6235
		conf_data = device->path[path].conf_data;
6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255
		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 */
6256
		tbcpm |= 0x80 >> path;
6257 6258 6259 6260 6261
	}
	return tbcpm;
}

static void dasd_eckd_cuir_notify_user(struct dasd_device *device,
6262
				       unsigned long paths, int action)
6263 6264 6265 6266 6267
{
	int pos;

	while (paths) {
		/* get position of bit in mask */
6268
		pos = 8 - ffs(paths);
6269 6270
		/* get channel path descriptor from this position */
		if (action == CUIR_QUIESCE)
6271 6272 6273
			pr_warn("Service on the storage server caused path %x.%02x to go offline",
				device->path[pos].cssid,
				device->path[pos].chpid);
6274
		else if (action == CUIR_RESUME)
6275 6276 6277 6278
			pr_info("Path %x.%02x is back online after service on the storage server",
				device->path[pos].cssid,
				device->path[pos].chpid);
		clear_bit(7 - pos, &paths);
6279 6280 6281 6282 6283 6284 6285 6286 6287 6288
	}
}

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 */
6289
	if (!(dasd_path_get_opm(device) & tbcpm))
6290
		return 0;
6291
	if (!(dasd_path_get_opm(device) & ~tbcpm)) {
6292 6293 6294 6295 6296
		/* no path would be left if the CUIR action is taken
		   return error */
		return -EINVAL;
	}
	/* remove device from operational path mask */
6297 6298
	dasd_path_remove_opm(device, tbcpm);
	dasd_path_add_cuirpm(device, tbcpm);
6299 6300 6301 6302 6303 6304
	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
6305 6306 6307 6308 6309 6310
 *
 * 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,
6311
				  struct dasd_cuir_message *cuir)
6312
{
6313
	struct dasd_eckd_private *private = device->private;
6314 6315
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_device *dev, *n;
6316 6317 6318
	unsigned long paths = 0;
	unsigned long flags;
	int tbcpm;
6319 6320

	/* active devices */
6321
	list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
6322
				 alias_list) {
6323 6324 6325 6326 6327 6328
		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;
6329 6330
	}
	/* inactive devices */
6331
	list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
6332
				 alias_list) {
6333 6334 6335 6336 6337 6338
		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;
6339 6340 6341 6342 6343 6344
	}
	/* 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) {
6345 6346 6347 6348 6349 6350 6351
			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;
6352 6353 6354
		}
		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
					 alias_list) {
6355 6356 6357 6358 6359 6360 6361
			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;
6362 6363
		}
	}
6364
	/* notify user about all paths affected by CUIR action */
6365
	dasd_eckd_cuir_notify_user(device, paths, CUIR_QUIESCE);
6366 6367 6368
	return 0;
out_err:
	return tbcpm;
6369 6370 6371
}

static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
6372
				 struct dasd_cuir_message *cuir)
6373
{
6374
	struct dasd_eckd_private *private = device->private;
6375 6376
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_device *dev, *n;
6377 6378
	unsigned long paths = 0;
	int tbcpm;
6379 6380 6381 6382 6383 6384 6385 6386

	/*
	 * 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) {
6387 6388
		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
		paths |= tbcpm;
6389 6390
		if (!(dasd_path_get_opm(dev) & tbcpm)) {
			dasd_path_add_tbvpm(dev, tbcpm);
6391 6392 6393 6394 6395 6396
			dasd_schedule_device_bh(dev);
		}
	}
	list_for_each_entry_safe(dev, n,
				 &private->lcu->inactive_devices,
				 alias_list) {
6397 6398
		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
		paths |= tbcpm;
6399 6400
		if (!(dasd_path_get_opm(dev) & tbcpm)) {
			dasd_path_add_tbvpm(dev, tbcpm);
6401 6402 6403 6404 6405 6406 6407 6408 6409 6410
			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) {
6411 6412
			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
			paths |= tbcpm;
6413 6414
			if (!(dasd_path_get_opm(dev) & tbcpm)) {
				dasd_path_add_tbvpm(dev, tbcpm);
6415 6416 6417 6418 6419 6420
				dasd_schedule_device_bh(dev);
			}
		}
		list_for_each_entry_safe(dev, n,
					 &pavgroup->aliaslist,
					 alias_list) {
6421 6422
			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
			paths |= tbcpm;
6423 6424
			if (!(dasd_path_get_opm(dev) & tbcpm)) {
				dasd_path_add_tbvpm(dev, tbcpm);
6425 6426 6427 6428
				dasd_schedule_device_bh(dev);
			}
		}
	}
6429
	/* notify user about all paths affected by CUIR action */
6430
	dasd_eckd_cuir_notify_user(device, paths, CUIR_RESUME);
6431
	return 0;
6432 6433 6434 6435 6436 6437
}

static void dasd_eckd_handle_cuir(struct dasd_device *device, void *messages,
				 __u8 lpum)
{
	struct dasd_cuir_message *cuir = messages;
6438
	int response;
6439

6440 6441 6442 6443
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "CUIR request: %016llx %016llx %016llx %08x",
		      ((u64 *)cuir)[0], ((u64 *)cuir)[1], ((u64 *)cuir)[2],
		      ((u32 *)cuir)[3]);
6444 6445 6446

	if (cuir->code == CUIR_QUIESCE) {
		/* quiesce */
6447
		if (dasd_eckd_cuir_quiesce(device, lpum, cuir))
6448 6449 6450
			response = PSF_CUIR_LAST_PATH;
		else
			response = PSF_CUIR_COMPLETED;
6451 6452
	} else if (cuir->code == CUIR_RESUME) {
		/* resume */
6453
		dasd_eckd_cuir_resume(device, lpum, cuir);
6454
		response = PSF_CUIR_COMPLETED;
6455 6456 6457
	} else
		response = PSF_CUIR_NOT_SUPPORTED;

6458
	dasd_eckd_psf_cuir_response(device, response,
6459
				    cuir->message_id, lpum);
6460 6461 6462 6463 6464
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "CUIR response: %d on message ID %08x", response,
		      cuir->message_id);
	/* to make sure there is no attention left schedule work again */
	device->discipline->check_attention(device, lpum);
6465 6466
}

6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533
static void dasd_eckd_oos_resume(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_device *dev, *n;
	unsigned long flags;

	spin_lock_irqsave(&private->lcu->lock, flags);
	list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
				 alias_list) {
		if (dev->stopped & DASD_STOPPED_NOSPC)
			dasd_generic_space_avail(dev);
	}
	list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
				 alias_list) {
		if (dev->stopped & DASD_STOPPED_NOSPC)
			dasd_generic_space_avail(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) {
			if (dev->stopped & DASD_STOPPED_NOSPC)
				dasd_generic_space_avail(dev);
		}
		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
					 alias_list) {
			if (dev->stopped & DASD_STOPPED_NOSPC)
				dasd_generic_space_avail(dev);
		}
	}
	spin_unlock_irqrestore(&private->lcu->lock, flags);
}

static void dasd_eckd_handle_oos(struct dasd_device *device, void *messages,
				 __u8 lpum)
{
	struct dasd_oos_message *oos = messages;

	switch (oos->code) {
	case REPO_WARN:
	case POOL_WARN:
		dev_warn(&device->cdev->dev,
			 "Extent pool usage has reached a critical value\n");
		dasd_eckd_oos_resume(device);
		break;
	case REPO_EXHAUST:
	case POOL_EXHAUST:
		dev_warn(&device->cdev->dev,
			 "Extent pool is exhausted\n");
		break;
	case REPO_RELIEVE:
	case POOL_RELIEVE:
		dev_info(&device->cdev->dev,
			 "Extent pool physical space constraint has been relieved\n");
		break;
	}

	/* In any case, update related data */
	dasd_eckd_read_ext_pool_info(device);

	/* to make sure there is no attention left schedule work again */
	device->discipline->check_attention(device, lpum);
}

6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551
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;
6552

6553 6554 6555
	if (messages->length == ATTENTION_LENGTH_CUIR &&
	    messages->format == ATTENTION_FORMAT_CUIR)
		dasd_eckd_handle_cuir(device, messages, data->lpum);
6556 6557 6558 6559
	if (messages->length == ATTENTION_LENGTH_OOS &&
	    messages->format == ATTENTION_FORMAT_OOS)
		dasd_eckd_handle_oos(device, messages, data->lpum);

6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580
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;
}

6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
static int dasd_eckd_disable_hpf_path(struct dasd_device *device, __u8 lpum)
{
	if (~lpum & dasd_path_get_opm(device)) {
		dasd_path_add_nohpfpm(device, lpum);
		dasd_path_remove_opm(device, lpum);
		dev_err(&device->cdev->dev,
			"Channel path %02X lost HPF functionality and is disabled\n",
			lpum);
		return 1;
	}
	return 0;
}

static void dasd_eckd_disable_hpf_device(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	dev_err(&device->cdev->dev,
		"High Performance FICON disabled\n");
	private->fcx_max_data = 0;
}

static int dasd_eckd_hpf_enabled(struct dasd_device *device)
{
	struct dasd_eckd_private *private = device->private;

	return private->fcx_max_data ? 1 : 0;
}

static void dasd_eckd_handle_hpf_error(struct dasd_device *device,
				       struct irb *irb)
{
	struct dasd_eckd_private *private = device->private;

	if (!private->fcx_max_data) {
		/* sanity check for no HPF, the error makes no sense */
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "Trying to disable HPF for a non HPF device");
		return;
	}
	if (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX) {
		dasd_eckd_disable_hpf_device(device);
	} else if (irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX) {
		if (dasd_eckd_disable_hpf_path(device, irb->esw.esw1.lpum))
			return;
		dasd_eckd_disable_hpf_device(device);
		dasd_path_set_tbvpm(device,
				  dasd_path_get_hpfpm(device));
	}
	/*
	 * prevent that any new I/O ist started on the device and schedule a
	 * requeue of existing requests
	 */
	dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
	dasd_schedule_requeue(device);
}

6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669
/*
 * Initialize block layer request queue.
 */
static void dasd_eckd_setup_blk_queue(struct dasd_block *block)
{
	unsigned int logical_block_size = block->bp_block;
	struct request_queue *q = block->request_queue;
	struct dasd_device *device = block->base;
	int max;

	if (device->features & DASD_FEATURE_USERAW) {
		/*
		 * the max_blocks value for raw_track access is 256
		 * it is higher than the native ECKD value because we
		 * only need one ccw per track
		 * so the max_hw_sectors are
		 * 2048 x 512B = 1024kB = 16 tracks
		 */
		max = DASD_ECKD_MAX_BLOCKS_RAW << block->s2b_shift;
	} else {
		max = DASD_ECKD_MAX_BLOCKS << block->s2b_shift;
	}
	blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
	q->limits.max_dev_sectors = max;
	blk_queue_logical_block_size(q, logical_block_size);
	blk_queue_max_hw_sectors(q, max);
	blk_queue_max_segments(q, USHRT_MAX);
	/* With page sized segments each segment can be translated into one idaw/tidaw */
	blk_queue_max_segment_size(q, PAGE_SIZE);
	blk_queue_segment_boundary(q, PAGE_SIZE - 1);
}

6670
static struct ccw_driver dasd_eckd_driver = {
6671 6672 6673 6674
	.driver = {
		.name	= "dasd-eckd",
		.owner	= THIS_MODULE,
	},
6675 6676 6677 6678 6679 6680
	.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,
6681
	.path_event  = dasd_generic_path_event,
6682
	.shutdown    = dasd_generic_shutdown,
6683 6684 6685
	.freeze      = dasd_generic_pm_freeze,
	.thaw	     = dasd_generic_restore_device,
	.restore     = dasd_generic_restore_device,
6686
	.uc_handler  = dasd_generic_uc_handler,
6687
	.int_class   = IRQIO_DAS,
6688
};
6689

L
Linus Torvalds 已提交
6690 6691 6692 6693 6694
static struct dasd_discipline dasd_eckd_discipline = {
	.owner = THIS_MODULE,
	.name = "ECKD",
	.ebcname = "ECKD",
	.check_device = dasd_eckd_check_characteristics,
6695
	.uncheck_device = dasd_eckd_uncheck_device,
L
Linus Torvalds 已提交
6696
	.do_analysis = dasd_eckd_do_analysis,
6697
	.verify_path = dasd_eckd_verify_path,
6698
	.basic_to_ready = dasd_eckd_basic_to_ready,
6699
	.online_to_ready = dasd_eckd_online_to_ready,
6700
	.basic_to_known = dasd_eckd_basic_to_known,
6701
	.setup_blk_queue = dasd_eckd_setup_blk_queue,
L
Linus Torvalds 已提交
6702 6703 6704
	.fill_geometry = dasd_eckd_fill_geometry,
	.start_IO = dasd_start_IO,
	.term_IO = dasd_term_IO,
6705
	.handle_terminated_request = dasd_eckd_handle_terminated_request,
L
Linus Torvalds 已提交
6706
	.format_device = dasd_eckd_format_device,
6707
	.check_device_format = dasd_eckd_check_device_format,
L
Linus Torvalds 已提交
6708 6709
	.erp_action = dasd_eckd_erp_action,
	.erp_postaction = dasd_eckd_erp_postaction,
6710
	.check_for_device_change = dasd_eckd_check_for_device_change,
6711 6712
	.build_cp = dasd_eckd_build_alias_cp,
	.free_cp = dasd_eckd_free_alias_cp,
L
Linus Torvalds 已提交
6713
	.dump_sense = dasd_eckd_dump_sense,
S
Stefan Haberland 已提交
6714
	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
L
Linus Torvalds 已提交
6715
	.fill_info = dasd_eckd_fill_info,
6716
	.ioctl = dasd_eckd_ioctl,
6717 6718
	.freeze = dasd_eckd_pm_freeze,
	.restore = dasd_eckd_restore_device,
6719
	.reload = dasd_eckd_reload_device,
6720
	.get_uid = dasd_eckd_get_uid,
6721
	.kick_validate = dasd_eckd_kick_validate_server,
6722
	.check_attention = dasd_eckd_check_attention,
6723 6724
	.host_access_count = dasd_eckd_host_access_count,
	.hosts_print = dasd_hosts_print,
6725 6726 6727 6728
	.handle_hpf_error = dasd_eckd_handle_hpf_error,
	.disable_hpf = dasd_eckd_disable_hpf_device,
	.hpf_enabled = dasd_eckd_hpf_enabled,
	.reset_path = dasd_eckd_reset_path,
6729 6730 6731 6732
	.is_ese = dasd_eckd_is_ese,
	.space_allocated = dasd_eckd_space_allocated,
	.space_configured = dasd_eckd_space_configured,
	.logical_capacity = dasd_eckd_logical_capacity,
6733
	.release_space = dasd_eckd_release_space,
6734 6735 6736 6737 6738
	.ext_pool_id = dasd_eckd_ext_pool_id,
	.ext_size = dasd_eckd_ext_size,
	.ext_pool_cap_at_warnlevel = dasd_eckd_ext_pool_cap_at_warnlevel,
	.ext_pool_warn_thrshld = dasd_eckd_ext_pool_warn_thrshld,
	.ext_pool_oos = dasd_eckd_ext_pool_oos,
6739
	.ext_pool_exhaust = dasd_eckd_ext_pool_exhaust,
6740 6741
	.ese_format = dasd_eckd_ese_format,
	.ese_read = dasd_eckd_ese_read,
L
Linus Torvalds 已提交
6742 6743 6744 6745 6746
};

static int __init
dasd_eckd_init(void)
{
6747 6748
	int ret;

L
Linus Torvalds 已提交
6749
	ASCEBC(dasd_eckd_discipline.ebcname, 4);
6750 6751 6752 6753
	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
				   GFP_KERNEL | GFP_DMA);
	if (!dasd_reserve_req)
		return -ENOMEM;
6754 6755 6756 6757
	dasd_vol_info_req = kmalloc(sizeof(*dasd_vol_info_req),
				    GFP_KERNEL | GFP_DMA);
	if (!dasd_vol_info_req)
		return -ENOMEM;
6758 6759 6760 6761
	path_verification_worker = kmalloc(sizeof(*path_verification_worker),
				   GFP_KERNEL | GFP_DMA);
	if (!path_verification_worker) {
		kfree(dasd_reserve_req);
6762
		kfree(dasd_vol_info_req);
6763 6764
		return -ENOMEM;
	}
6765 6766 6767 6768
	rawpadpage = (void *)__get_free_page(GFP_KERNEL);
	if (!rawpadpage) {
		kfree(path_verification_worker);
		kfree(dasd_reserve_req);
6769
		kfree(dasd_vol_info_req);
6770 6771
		return -ENOMEM;
	}
6772 6773 6774
	ret = ccw_driver_register(&dasd_eckd_driver);
	if (!ret)
		wait_for_device_probe();
6775 6776
	else {
		kfree(path_verification_worker);
6777
		kfree(dasd_reserve_req);
6778
		kfree(dasd_vol_info_req);
6779
		free_page((unsigned long)rawpadpage);
6780
	}
6781
	return ret;
L
Linus Torvalds 已提交
6782 6783 6784 6785 6786 6787
}

static void __exit
dasd_eckd_cleanup(void)
{
	ccw_driver_unregister(&dasd_eckd_driver);
6788
	kfree(path_verification_worker);
6789
	kfree(dasd_reserve_req);
6790
	free_page((unsigned long)rawpadpage);
L
Linus Torvalds 已提交
6791 6792 6793 6794
}

module_init(dasd_eckd_init);
module_exit(dasd_eckd_cleanup);