dasd_eckd.c 160.3 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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

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

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

59 60 61 62 63 64 65 66 67
/*
 * 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 已提交
68 69 70 71 72 73 74
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 已提交
75 76
	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
77
	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
H
Heiko Carstens 已提交
78 79 80 81 82 83 84
	{ 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 已提交
85 86 87 88 89 90 91
	{ /* end of list */ },
};

MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);

static struct ccw_driver dasd_eckd_driver; /* see below */

92 93
static void *rawpadpage;

94 95 96 97
#define INIT_CQR_OK 0
#define INIT_CQR_UNFORMATTED 1
#define INIT_CQR_ERROR 2

98 99 100 101 102 103 104 105
/* 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);

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

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

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 147
	}
	ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
	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 160 161 162
/* head and record addresses of count_area read in analysis ccw */
static const int count_area_head[] = { 0, 0, 0, 0, 2 };
static const int count_area_rec[] = { 1, 2, 3, 4, 1 };

L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175
static inline unsigned int
round_up_multiple(unsigned int no, unsigned int mult)
{
	int rem = no % mult;
	return (rem ? no - rem + mult : no);
}

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

176
static unsigned int
L
Linus Torvalds 已提交
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 202 203 204 205 206 207 208
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;
}

209 210 211 212 213 214 215 216
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;
}

217
static int set_timestamp(struct ccw1 *ccw, struct DE_eckd_data *data,
218
		     struct dasd_device *device)
L
Linus Torvalds 已提交
219
{
220
	struct dasd_eckd_private *private = device->private;
M
Martin Schwidefsky 已提交
221
	int rc;
L
Linus Torvalds 已提交
222

223 224 225 226 227 228 229
	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 已提交
230
		return 0;
L
Linus Torvalds 已提交
231

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

236 237 238 239 240
	if (ccw) {
		ccw->count = sizeof(struct DE_eckd_data);
		ccw->flags |= CCW_FLAG_SLI;
	}

M
Martin Schwidefsky 已提交
241 242
	return rc;
}
L
Linus Torvalds 已提交
243

244
static int
245
define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
246 247
	      unsigned int totrk, int cmd, struct dasd_device *device,
	      int blksize)
L
Linus Torvalds 已提交
248
{
249
	struct dasd_eckd_private *private = device->private;
250
	u16 heads, beghead, endhead;
251
	u32 begcyl, endcyl;
M
Martin Schwidefsky 已提交
252
	int rc = 0;
L
Linus Torvalds 已提交
253

254 255 256 257 258 259
	if (ccw) {
		ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
		ccw->flags = 0;
		ccw->count = 16;
		ccw->cda = (__u32)__pa(data);
	}
L
Linus Torvalds 已提交
260

261
	memset(data, 0, sizeof(struct DE_eckd_data));
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273
	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;
274 275 276 277
	case DASD_ECKD_CCW_READ_COUNT:
		data->mask.perm = 0x1;
		data->attributes.operation = DASD_BYPASS_CACHE;
		break;
278 279 280 281 282 283
	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 已提交
284 285 286 287 288 289
	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;
290
		rc = set_timestamp(ccw, data, device);
L
Linus Torvalds 已提交
291 292 293 294
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->attributes.operation = DASD_BYPASS_CACHE;
295
		rc = set_timestamp(ccw, data, device);
L
Linus Torvalds 已提交
296 297 298 299 300 301 302
		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;
303
		rc = set_timestamp(ccw, data, device);
304 305 306 307 308 309 310 311 312 313
		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;
314
		rc = set_timestamp(ccw, data, device);
L
Linus Torvalds 已提交
315 316
		break;
	default:
S
Stefan Haberland 已提交
317 318
		dev_err(&device->cdev->dev,
			"0x%x is not a known command\n", cmd);
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329
		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 */

330 331 332 333 334
	heads = private->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;
L
Linus Torvalds 已提交
335 336 337 338

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

340 341
		if (endcyl + private->attrib.nr_cyl < private->real_cyl)
			endcyl += private->attrib.nr_cyl;
L
Linus Torvalds 已提交
342
		else
343
			endcyl = (private->real_cyl - 1);
L
Linus Torvalds 已提交
344 345
	}

346 347
	set_ch_t(&data->beg_ext, begcyl, beghead);
	set_ch_t(&data->end_ext, endcyl, endhead);
M
Martin Schwidefsky 已提交
348
	return rc;
L
Linus Torvalds 已提交
349 350
}

351

352 353 354 355
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)
356
{
357
	struct dasd_eckd_private *private = device->private;
358 359 360
	int sector;
	int dn, d;

361 362 363 364 365 366 367 368 369 370
	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);
	}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	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;
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	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;
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	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;
465 466 467 468 469 470
	case DASD_ECKD_CCW_READ_TRACK:
		data->operation.orientation = 0x1;
		data->operation.operation = 0x0C;
		data->extended_parameter_length = 0;
		data->sector = 0xFF;
		break;
471 472 473 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_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,
497
		      unsigned int format, unsigned int rec_on_trk, int count,
498
		      unsigned int blksize, unsigned int tlf)
499 500
{
	struct dasd_eckd_private *basepriv, *startpriv;
501
	struct LRE_eckd_data *lredata;
502
	struct DE_eckd_data *dedata;
503 504
	int rc = 0;

505 506
	basepriv = basedev->private;
	startpriv = startdev->private;
507 508
	dedata = &pfxdata->define_extent;
	lredata = &pfxdata->locate_record;
509 510 511

	ccw->cmd_code = DASD_ECKD_CCW_PFX;
	ccw->flags = 0;
512 513 514 515 516 517 518 519 520
	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));
	}
521 522

	/* prefix data */
523 524 525 526 527 528 529
	if (format > 1) {
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "PFX LRE unknown format 0x%x", format);
		BUG();
		return -EINVAL;
	}
	pfxdata->format = format;
530 531
	pfxdata->base_address = basepriv->ned->unit_addr;
	pfxdata->base_lss = basepriv->ned->ID;
532
	pfxdata->validity.define_extent = 1;
533 534

	/* private uid is kept up to date, conf_data may be outdated */
535
	if (startpriv->uid.type == UA_BASE_PAV_ALIAS)
536
		pfxdata->validity.verify_base = 1;
537 538 539 540

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

543
	rc = define_extent(NULL, dedata, trk, totrk, cmd, basedev, blksize);
544

545 546 547 548 549 550 551
	/*
	 * 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'   */
552 553

	if (format == 1) {
554 555
		locate_record_ext(NULL, lredata, trk, rec_on_trk, count, cmd,
				  basedev, blksize, tlf);
556 557
	}

558 559 560
	return rc;
}

561 562 563 564 565 566 567 568
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);
}

569
static void
570 571
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 已提交
572 573
	      struct dasd_device * device, int reclen)
{
574
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
575 576
	int sector;
	int dn, d;
577

L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586
	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);

587
	memset(data, 0, sizeof(struct LO_eckd_data));
L
Linus Torvalds 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 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
	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 已提交
660 661
		DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
			      "opcode 0x%x", cmd);
L
Linus Torvalds 已提交
662
	}
663 664 665 666 667
	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 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	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;
}
705 706
/* create unique id from private structure. */
static void create_uid(struct dasd_eckd_private *private)
707
{
708
	int count;
709
	struct dasd_uid *uid;
710

711
	uid = &private->uid;
712
	memset(uid, 0, sizeof(struct dasd_uid));
713
	memcpy(uid->vendor, private->ned->HDA_manufacturer,
714
	       sizeof(uid->vendor) - 1);
715
	EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
716
	memcpy(uid->serial, private->ned->HDA_location,
717
	       sizeof(uid->serial) - 1);
718
	EBCASC(uid->serial, sizeof(uid->serial) - 1);
719
	uid->ssid = private->gneq->subsystemID;
720
	uid->real_unit_addr = private->ned->unit_addr;
721 722
	if (private->sneq) {
		uid->type = private->sneq->sua_flags;
723
		if (uid->type == UA_BASE_PAV_ALIAS)
724
			uid->base_unit_addr = private->sneq->base_unit_addr;
725 726 727
	} else {
		uid->type = UA_BASE_DEVICE;
	}
728 729 730 731 732 733
	if (private->vdsneq) {
		for (count = 0; count < 16; count++) {
			sprintf(uid->vduit+2*count, "%02x",
				private->vdsneq->uit[count]);
		}
	}
734 735 736 737 738 739 740
}

/*
 * Generate device unique id that specifies the physical device.
 */
static int dasd_eckd_generate_uid(struct dasd_device *device)
{
741
	struct dasd_eckd_private *private = device->private;
742 743 744 745 746 747 748 749
	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);
750
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
751 752 753
	return 0;
}

754 755
static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
{
756
	struct dasd_eckd_private *private = device->private;
757 758
	unsigned long flags;

759
	if (private) {
760 761 762 763 764 765 766 767
		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;
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
/*
 * 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));
}

783 784 785 786
static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
				   struct dasd_ccw_req *cqr,
				   __u8 *rcd_buffer,
				   __u8 lpm)
787 788
{
	struct ccw1 *ccw;
789 790 791 792 793 794 795 796
	/*
	 * 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;
797 798

	ccw = cqr->cpaddr;
799 800
	ccw->cmd_code = DASD_ECKD_CCW_RCD;
	ccw->flags = 0;
801
	ccw->cda = (__u32)(addr_t)rcd_buffer;
802 803
	ccw->count = DASD_ECKD_RCD_DATA_SIZE;
	cqr->magic = DASD_ECKD_MAGIC;
804

805 806 807
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
808 809
	cqr->expires = 10*HZ;
	cqr->lpm = lpm;
810
	cqr->retries = 256;
811
	cqr->buildclk = get_tod_clock();
812
	cqr->status = DASD_CQR_FILLED;
813 814 815
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
}

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/*
 * 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);
}

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
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);
857
	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
858
	cqr->retries = 5;
859
	cqr->callback = read_conf_cb;
860 861
	rc = dasd_sleep_on_immediatly(cqr);
	return rc;
862 863 864 865 866 867 868 869 870 871 872 873
}

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;

	/*
874 875
	 * sanity check: scan for RCD command in extended SenseID data
	 * some devices do not support RCD
876 877
	 */
	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
878
	if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
879 880 881
		ret = -EOPNOTSUPP;
		goto out_error;
	}
882
	rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
883 884 885 886
	if (!rcd_buf) {
		ret = -ENOMEM;
		goto out_error;
	}
887 888 889
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
				   0, /* use rcd_buf as data ara */
				   device);
890
	if (IS_ERR(cqr)) {
891 892 893
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "Could not allocate RCD request");
		ret = -ENOMEM;
894 895
		goto out_error;
	}
896
	dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
897
	cqr->callback = read_conf_cb;
898 899 900 901
	ret = dasd_sleep_on(cqr);
	/*
	 * on success we update the user input parms
	 */
902
	dasd_sfree_request(cqr, cqr->memdev);
903 904 905
	if (ret)
		goto out_error;

906
	*rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
907 908 909 910 911 912 913 914 915
	*rcd_buffer = rcd_buf;
	return 0;
out_error:
	kfree(rcd_buf);
	*rcd_buffer = NULL;
	*rcd_buffer_size = 0;
	return ret;
}

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
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;
}

971 972
static void dasd_eckd_clear_conf_data(struct dasd_device *device)
{
973
	struct dasd_eckd_private *private = device->private;
974 975 976 977 978
	int i;

	private->conf_data = NULL;
	private->conf_len = 0;
	for (i = 0; i < 8; i++) {
979 980
		kfree(device->path[i].conf_data);
		device->path[i].conf_data = NULL;
981 982 983
		device->path[i].cssid = 0;
		device->path[i].ssid = 0;
		device->path[i].chpid = 0;
984 985 986 987
	}
}


988
static int dasd_eckd_read_conf(struct dasd_device *device)
L
Linus Torvalds 已提交
989 990 991
{
	void *conf_data;
	int conf_len, conf_data_saved;
992
	int rc, path_err, pos;
993
	__u8 lpm, opm;
994 995 996
	struct dasd_eckd_private *private, path_private;
	struct dasd_uid *uid;
	char print_path_uid[60], print_device_uid[60];
997
	struct channel_path_desc_fmt0 *chp_desc;
998
	struct subchannel_id sch_id;
L
Linus Torvalds 已提交
999

1000
	private = device->private;
1001
	opm = ccw_device_get_path_mask(device->cdev);
1002
	ccw_device_get_schid(device->cdev, &sch_id);
L
Linus Torvalds 已提交
1003
	conf_data_saved = 0;
1004
	path_err = 0;
L
Linus Torvalds 已提交
1005 1006
	/* get configuration data per operational path */
	for (lpm = 0x80; lpm; lpm>>= 1) {
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		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 */
1022
			dasd_path_add_opm(device, opm);
1023 1024 1025 1026
			continue;	/* no error */
		}
		/* save first valid configuration data */
		if (!conf_data_saved) {
1027 1028
			/* initially clear previously stored conf_data */
			dasd_eckd_clear_conf_data(device);
1029 1030 1031 1032 1033 1034 1035
			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 已提交
1036
			}
1037 1038
			pos = pathmask_to_pos(lpm);
			/* store per path conf_data */
1039
			device->path[pos].conf_data = conf_data;
1040 1041 1042 1043 1044 1045
			device->path[pos].cssid = sch_id.cssid;
			device->path[pos].ssid = sch_id.ssid;
			chp_desc = ccw_device_get_chp_desc(device->cdev, pos);
			if (chp_desc)
				device->path[pos].chpid = chp_desc->chpid;
			kfree(chp_desc);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
			/*
			 * 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 已提交
1061
			}
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
			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);
1099
				path_err = -EINVAL;
1100
				dasd_path_add_cablepm(device, lpm);
1101
				continue;
L
Linus Torvalds 已提交
1102
			}
1103 1104
			pos = pathmask_to_pos(lpm);
			/* store per path conf_data */
1105
			device->path[pos].conf_data = conf_data;
1106 1107 1108 1109 1110 1111
			device->path[pos].cssid = sch_id.cssid;
			device->path[pos].ssid = sch_id.ssid;
			chp_desc = ccw_device_get_chp_desc(device->cdev, pos);
			if (chp_desc)
				device->path[pos].chpid = chp_desc->chpid;
			kfree(chp_desc);
1112 1113 1114 1115 1116
			path_private.conf_data = NULL;
			path_private.conf_len = 0;
		}
		switch (dasd_eckd_path_access(conf_data, conf_len)) {
		case 0x02:
1117
			dasd_path_add_nppm(device, lpm);
1118 1119
			break;
		case 0x03:
1120
			dasd_path_add_ppm(device, lpm);
1121
			break;
L
Linus Torvalds 已提交
1122
		}
1123 1124
		if (!dasd_path_get_opm(device)) {
			dasd_path_set_opm(device, lpm);
1125 1126
			dasd_generic_path_operational(device);
		} else {
1127
			dasd_path_add_opm(device, lpm);
1128
		}
L
Linus Torvalds 已提交
1129
	}
1130

1131
	return path_err;
L
Linus Torvalds 已提交
1132 1133
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
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;
	int tpm, mdc;

	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);
	if (mdc < 0) {
		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;
	}
}

1160 1161
static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
{
1162
	struct dasd_eckd_private *private = device->private;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	int mdc;
	u32 fcx_max_data;

	if (private->fcx_max_data) {
		mdc = ccw_device_get_mdc(device->cdev, lpm);
		if ((mdc < 0)) {
			dev_warn(&device->cdev->dev,
				 "Detecting the maximum data size for zHPF "
				 "requests failed (rc=%d) for a new path %x\n",
				 mdc, lpm);
			return mdc;
		}
1175
		fcx_max_data = (u32)mdc * FCX_MAX_DATA_FACTOR;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		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;
}

1188 1189 1190
static int rebuild_device_uid(struct dasd_device *device,
			      struct path_verification_work_data *data)
{
1191
	struct dasd_eckd_private *private = device->private;
1192
	__u8 lpm, opm = dasd_path_get_opm(device);
1193
	int rc = -ENODEV;
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

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

1227 1228 1229 1230
static void do_path_verification_work(struct work_struct *work)
{
	struct path_verification_work_data *data;
	struct dasd_device *device;
1231 1232 1233
	struct dasd_eckd_private path_private;
	struct dasd_uid *uid;
	__u8 path_rcd_buf[DASD_ECKD_RCD_DATA_SIZE];
1234
	__u8 lpm, opm, npm, ppm, epm, hpfpm, cablepm;
1235
	unsigned long flags;
1236
	char print_uid[60];
1237 1238 1239 1240 1241
	int rc;

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

1242 1243 1244 1245 1246
	/* delay path verification until device was resumed */
	if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
		schedule_work(work);
		return;
	}
1247 1248 1249 1250 1251
	/* 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;
	}
1252 1253 1254 1255
	opm = 0;
	npm = 0;
	ppm = 0;
	epm = 0;
1256 1257 1258
	hpfpm = 0;
	cablepm = 0;

1259
	for (lpm = 0x80; lpm; lpm >>= 1) {
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
		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",
1287 1288
					"path verification: device is stopped,"
					" try again later");
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
			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;
1300
			hpfpm |= lpm;
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
			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
		 */
1325
		if (dasd_path_get_opm(device) &&
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		    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);
1359 1360 1361
				opm &= ~lpm;
				npm &= ~lpm;
				ppm &= ~lpm;
1362
				cablepm |= lpm;
1363
				continue;
1364 1365
			}
		}
1366 1367 1368 1369 1370 1371 1372 1373 1374

		/*
		 * 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);
1375 1376
		if (!dasd_path_get_opm(device) && opm) {
			dasd_path_set_opm(device, opm);
1377
			dasd_generic_path_operational(device);
1378
		} else {
1379
			dasd_path_add_opm(device, opm);
1380
		}
1381 1382 1383 1384 1385
		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);
1386
		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1387
	}
1388
	clear_bit(DASD_FLAG_PATH_VERIFY, &device->flags);
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	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;
}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
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);
}

1432 1433
static int dasd_eckd_read_features(struct dasd_device *device)
{
1434
	struct dasd_eckd_private *private = device->private;
1435 1436 1437 1438 1439 1440
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_features *features;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

1441
	memset(&private->features, 0, sizeof(struct dasd_rssd_features));
1442
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
1443 1444 1445 1446
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_features)),
				   device);
	if (IS_ERR(cqr)) {
1447 1448
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
				"allocate initialization request");
1449 1450 1451 1452 1453
		return PTR_ERR(cqr);
	}
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
1454
	cqr->retries = 256;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	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;

1479
	cqr->buildclk = get_tod_clock();
1480 1481 1482 1483 1484 1485 1486
	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));
1487 1488 1489
	} else
		dev_warn(&device->cdev->dev, "Reading device feature codes"
			 " failed with rc=%d\n", rc);
1490 1491 1492 1493 1494
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}


1495 1496 1497
/*
 * Build CP for Perform Subsystem Function - SSC.
 */
1498 1499
static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
						    int enable_pav)
1500
{
1501 1502 1503
	struct dasd_ccw_req *cqr;
	struct dasd_psf_ssc_data *psf_ssc_data;
	struct ccw1 *ccw;
1504

1505
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1506 1507 1508
				  sizeof(struct dasd_psf_ssc_data),
				  device);

1509
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
1510
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1511
			   "Could not allocate PSF-SSC request");
1512 1513 1514 1515
		return cqr;
	}
	psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
	psf_ssc_data->order = PSF_ORDER_SSC;
1516
	psf_ssc_data->suborder = 0xc0;
1517
	if (enable_pav) {
1518
		psf_ssc_data->suborder |= 0x08;
1519 1520
		psf_ssc_data->reserved[0] = 0x88;
	}
1521 1522 1523 1524 1525 1526 1527 1528
	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;
1529
	cqr->retries = 256;
1530
	cqr->expires = 10*HZ;
1531
	cqr->buildclk = get_tod_clock();
1532 1533
	cqr->status = DASD_CQR_FILLED;
	return cqr;
1534 1535 1536 1537 1538 1539 1540 1541
}

/*
 * 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 已提交
1542 1543
dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
		  unsigned long flags)
1544
{
1545 1546 1547
	struct dasd_ccw_req *cqr;
	int rc;

1548
	cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1549 1550 1551
	if (IS_ERR(cqr))
		return PTR_ERR(cqr);

S
Stefan Haberland 已提交
1552 1553 1554 1555 1556 1557
	/*
	 * set flags e.g. turn on failfast, to prevent blocking
	 * the calling function should handle failed requests
	 */
	cqr->flags |= flags;

1558 1559 1560 1561
	rc = dasd_sleep_on(cqr);
	if (!rc)
		/* trigger CIO to reprobe devices */
		css_schedule_reprobe();
S
Stefan Haberland 已提交
1562 1563 1564
	else if (cqr->intrc == -EAGAIN)
		rc = -EAGAIN;

1565 1566
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
1567 1568 1569 1570 1571
}

/*
 * Valide storage server of current device.
 */
S
Stefan Haberland 已提交
1572 1573
static int dasd_eckd_validate_server(struct dasd_device *device,
				     unsigned long flags)
1574
{
1575 1576
	struct dasd_eckd_private *private = device->private;
	int enable_pav, rc;
1577

1578 1579
	if (private->uid.type == UA_BASE_PAV_ALIAS ||
	    private->uid.type == UA_HYPER_PAV_ALIAS)
S
Stefan Haberland 已提交
1580
		return 0;
1581
	if (dasd_nopav || MACHINE_IS_VM)
1582 1583 1584
		enable_pav = 0;
	else
		enable_pav = 1;
S
Stefan Haberland 已提交
1585
	rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
1586

H
Horst Hummel 已提交
1587 1588
	/* may be requested feature is not available on server,
	 * therefore just report error and go ahead */
1589 1590
	DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
			"returned rc=%d", private->uid.ssid, rc);
S
Stefan Haberland 已提交
1591
	return rc;
1592 1593
}

1594 1595 1596 1597 1598 1599 1600
/*
 * 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);
1601 1602 1603 1604
	unsigned long flags = 0;

	set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
	if (dasd_eckd_validate_server(device, flags)
S
Stefan Haberland 已提交
1605 1606 1607 1608 1609 1610
	    == -EAGAIN) {
		/* schedule worker again if failed */
		schedule_work(&device->kick_validate);
		return;
	}

1611 1612 1613 1614 1615 1616
	dasd_put_device(device);
}

static void dasd_eckd_kick_validate_server(struct dasd_device *device)
{
	dasd_get_device(device);
1617 1618 1619 1620 1621 1622
	/* 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;
	}
1623
	/* queue call to do_validate_server to the kernel event daemon. */
1624 1625
	if (!schedule_work(&device->kick_validate))
		dasd_put_device(device);
1626 1627
}

1628 1629 1630 1631
/*
 * Check device characteristics.
 * If the device is accessible using ECKD discipline, the device is enabled.
 */
L
Linus Torvalds 已提交
1632 1633 1634
static int
dasd_eckd_check_characteristics(struct dasd_device *device)
{
1635
	struct dasd_eckd_private *private = device->private;
1636
	struct dasd_block *block;
1637
	struct dasd_uid temp_uid;
1638
	int rc, i;
1639
	int readonly;
1640
	unsigned long value;
L
Linus Torvalds 已提交
1641

1642 1643
	/* setup work queue for validate server*/
	INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1644 1645
	/* setup work queue for summary unit check */
	INIT_WORK(&device->suc_work, dasd_alias_handle_summary_unit_check);
1646

1647 1648 1649 1650 1651 1652 1653 1654 1655
	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");
	}
1656 1657 1658
	if (!private) {
		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
		if (!private) {
S
Stefan Haberland 已提交
1659 1660 1661
			dev_warn(&device->cdev->dev,
				 "Allocating memory for private DASD data "
				 "failed\n");
L
Linus Torvalds 已提交
1662 1663
			return -ENOMEM;
		}
1664
		device->private = private;
1665 1666
	} else {
		memset(private, 0, sizeof(*private));
L
Linus Torvalds 已提交
1667 1668 1669 1670 1671 1672 1673
	}
	/* 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;

1674 1675 1676
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
1677
		goto out_err1;
1678

1679
	/* set some default values */
1680
	device->default_expires = DASD_EXPIRES;
1681
	device->default_retries = DASD_RETRIES;
1682 1683
	device->path_thrhld = DASD_ECKD_PATH_THRHLD;
	device->path_interval = DASD_ECKD_PATH_INTERVAL;
1684

1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
	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;
	}

1695 1696
	dasd_eckd_get_uid(device, &temp_uid);
	if (temp_uid.type == UA_BASE_DEVICE) {
1697 1698
		block = dasd_alloc_block();
		if (IS_ERR(block)) {
1699 1700 1701
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
					"could not allocate dasd "
					"block structure");
1702 1703 1704 1705 1706 1707 1708
			rc = PTR_ERR(block);
			goto out_err1;
		}
		device->block = block;
		block->base = device;
	}

1709 1710 1711
	/* register lcu with alias handling, enable PAV */
	rc = dasd_alias_make_device_known_to_lcu(device);
	if (rc)
1712
		goto out_err2;
1713

S
Stefan Haberland 已提交
1714
	dasd_eckd_validate_server(device, 0);
1715 1716 1717 1718 1719

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

	/* Read Feature Codes */
1722
	dasd_eckd_read_features(device);
1723

L
Linus Torvalds 已提交
1724
	/* Read Device Characteristics */
1725 1726
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
					 &private->rdc_data, 64);
1727
	if (rc) {
1728 1729
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
1730 1731
		goto out_err3;
	}
1732 1733 1734 1735 1736 1737 1738 1739 1740

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

1741
	/* find the valid cylinder size */
1742 1743 1744 1745 1746 1747
	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;

1748 1749
	private->fcx_max_data = get_fcx_max_data(device);

1750 1751 1752 1753
	readonly = dasd_device_is_ro(device);
	if (readonly)
		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);

S
Stefan Haberland 已提交
1754
	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1755
		 "with %d cylinders, %d heads, %d sectors%s\n",
S
Stefan Haberland 已提交
1756 1757 1758 1759
		 private->rdc_data.dev_type,
		 private->rdc_data.dev_model,
		 private->rdc_data.cu_type,
		 private->rdc_data.cu_model.model,
1760
		 private->real_cyl,
S
Stefan Haberland 已提交
1761
		 private->rdc_data.trk_per_cyl,
1762 1763
		 private->rdc_data.sec_per_trk,
		 readonly ? ", read-only device" : "");
1764 1765 1766 1767 1768 1769 1770 1771
	return 0;

out_err3:
	dasd_alias_disconnect_device_from_lcu(device);
out_err2:
	dasd_free_block(device->block);
	device->block = NULL;
out_err1:
1772
	kfree(private->conf_data);
1773 1774
	kfree(device->private);
	device->private = NULL;
1775
	return rc;
L
Linus Torvalds 已提交
1776 1777
}

1778 1779
static void dasd_eckd_uncheck_device(struct dasd_device *device)
{
1780
	struct dasd_eckd_private *private = device->private;
1781
	int i;
1782

1783
	dasd_alias_disconnect_device_from_lcu(device);
1784 1785 1786 1787 1788
	private->ned = NULL;
	private->sneq = NULL;
	private->vdsneq = NULL;
	private->gneq = NULL;
	private->conf_len = 0;
1789
	for (i = 0; i < 8; i++) {
1790 1791
		kfree(device->path[i].conf_data);
		if ((__u8 *)device->path[i].conf_data ==
1792 1793 1794 1795
		    private->conf_data) {
			private->conf_data = NULL;
			private->conf_len = 0;
		}
1796
		device->path[i].conf_data = NULL;
1797 1798 1799
		device->path[i].cssid = 0;
		device->path[i].ssid = 0;
		device->path[i].chpid = 0;
1800
	}
1801 1802
	kfree(private->conf_data);
	private->conf_data = NULL;
1803 1804
}

L
Linus Torvalds 已提交
1805 1806 1807
static struct dasd_ccw_req *
dasd_eckd_analysis_ccw(struct dasd_device *device)
{
1808
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817
	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);
1818
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
	/* Define extent for the first 3 tracks. */
	define_extent(ccw++, cqr->data, 0, 2,
1824
		      DASD_ECKD_CCW_READ_COUNT, device, 0);
1825
	LO_data = cqr->data + sizeof(struct DE_eckd_data);
L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
	/* Locate record for the first 4 records on track 0. */
	ccw[-1].flags |= CCW_FLAG_CC;
	locate_record(ccw++, LO_data++, 0, 0, 4,
		      DASD_ECKD_CCW_READ_COUNT, device, 0);

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

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

1853 1854 1855
	cqr->block = NULL;
	cqr->startdev = device;
	cqr->memdev = device;
1856
	cqr->retries = 255;
1857
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
1858 1859 1860 1861
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
/* 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 已提交
1879 1880 1881 1882 1883 1884 1885
/*
 * 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).
 */
1886 1887
static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
					void *data)
L
Linus Torvalds 已提交
1888
{
1889 1890
	struct dasd_device *device = init_cqr->startdev;
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
1891

1892
	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
L
Linus Torvalds 已提交
1893 1894 1895 1896
	dasd_sfree_request(init_cqr, device);
	dasd_kick_device(device);
}

1897
static int dasd_eckd_start_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1898 1899 1900
{
	struct dasd_ccw_req *init_cqr;

1901
	init_cqr = dasd_eckd_analysis_ccw(block->base);
L
Linus Torvalds 已提交
1902 1903 1904 1905 1906
	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;
1907 1908 1909 1910 1911
	/* 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 已提交
1912 1913 1914 1915
	dasd_add_request_head(init_cqr);
	return -EAGAIN;
}

1916
static int dasd_eckd_end_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1917
{
1918 1919
	struct dasd_device *device = block->base;
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
1920 1921 1922
	struct eckd_count *count_area;
	unsigned int sb, blk_per_trk;
	int status, i;
1923
	struct dasd_ccw_req *init_cqr;
L
Linus Torvalds 已提交
1924 1925 1926

	status = private->init_cqr_status;
	private->init_cqr_status = -1;
1927 1928 1929 1930 1931 1932 1933 1934
	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);
	}

1935 1936 1937 1938 1939 1940 1941
	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;
	}

1942 1943
	if (status == INIT_CQR_UNFORMATTED) {
		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
L
Linus Torvalds 已提交
1944
		return -EMEDIUMTYPE;
1945 1946 1947 1948 1949
	} 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 已提交
1950 1951 1952 1953 1954 1955 1956
	}

	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 ||
1957 1958 1959 1960
		    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 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
			private->uses_cdl = 0;
			break;
		}
	}
	if (i == 3)
		count_area = &private->count_area[4];

	if (private->uses_cdl == 0) {
		for (i = 0; i < 5; i++) {
			if ((private->count_area[i].kl != 0) ||
			    (private->count_area[i].dl !=
1972 1973 1974 1975
			     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 已提交
1976 1977 1978 1979 1980 1981
				break;
		}
		if (i == 5)
			count_area = &private->count_area[0];
	} else {
		if (private->count_area[3].record == 1)
S
Stefan Haberland 已提交
1982 1983
			dev_warn(&device->cdev->dev,
				 "Track 0 has no records following the VTOC\n");
L
Linus Torvalds 已提交
1984
	}
1985

L
Linus Torvalds 已提交
1986 1987 1988
	if (count_area != NULL && count_area->kl == 0) {
		/* we found notthing violating our disk layout */
		if (dasd_check_blocksize(count_area->dl) == 0)
1989
			block->bp_block = count_area->dl;
L
Linus Torvalds 已提交
1990
	}
1991
	if (block->bp_block == 0) {
S
Stefan Haberland 已提交
1992 1993
		dev_warn(&device->cdev->dev,
			 "The disk layout of the DASD is not supported\n");
L
Linus Torvalds 已提交
1994 1995
		return -EMEDIUMTYPE;
	}
1996 1997 1998
	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 已提交
1999

2000
	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
2001 2002

raw:
2003
	block->blocks = (private->real_cyl *
L
Linus Torvalds 已提交
2004 2005 2006
			  private->rdc_data.trk_per_cyl *
			  blk_per_trk);

S
Stefan Haberland 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015
	dev_info(&device->cdev->dev,
		 "DASD with %d KB/block, %d KB total size, %d KB/track, "
		 "%s\n", (block->bp_block >> 10),
		 ((private->real_cyl *
		   private->rdc_data.trk_per_cyl *
		   blk_per_trk * (block->bp_block >> 9)) >> 1),
		 ((blk_per_trk * block->bp_block) >> 10),
		 private->uses_cdl ?
		 "compatible disk layout" : "linux disk layout");
L
Linus Torvalds 已提交
2016 2017 2018 2019

	return 0;
}

2020
static int dasd_eckd_do_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
2021
{
2022
	struct dasd_eckd_private *private = block->base->private;
L
Linus Torvalds 已提交
2023 2024

	if (private->init_cqr_status < 0)
2025
		return dasd_eckd_start_analysis(block);
L
Linus Torvalds 已提交
2026
	else
2027
		return dasd_eckd_end_analysis(block);
L
Linus Torvalds 已提交
2028 2029
}

2030
static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2031 2032 2033 2034 2035 2036
{
	return dasd_alias_add_device(device);
};

static int dasd_eckd_online_to_ready(struct dasd_device *device)
{
2037
	cancel_work_sync(&device->reload_device);
2038
	cancel_work_sync(&device->kick_validate);
2039 2040 2041
	return 0;
};

2042
static int dasd_eckd_basic_to_known(struct dasd_device *device)
2043
{
2044 2045 2046
	return dasd_alias_remove_device(device);
};

L
Linus Torvalds 已提交
2047
static int
2048
dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
L
Linus Torvalds 已提交
2049
{
2050
	struct dasd_eckd_private *private = block->base->private;
L
Linus Torvalds 已提交
2051

2052
	if (dasd_check_blocksize(block->bp_block) == 0) {
L
Linus Torvalds 已提交
2053
		geo->sectors = recs_per_track(&private->rdc_data,
2054
					      0, block->bp_block);
L
Linus Torvalds 已提交
2055 2056 2057 2058 2059 2060
	}
	geo->cylinders = private->rdc_data.no_cyl;
	geo->heads = private->rdc_data.trk_per_cyl;
	return 0;
}

2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
/*
 * 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);

	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
	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;

	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				  startdev);
	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,
2203
			      DASD_ECKD_CCW_READ_COUNT, startdev, 0);
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

		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 已提交
2235
static struct dasd_ccw_req *
2236
dasd_eckd_build_format(struct dasd_device *base,
2237
		       struct format_data_t *fdata,
S
Stefan Haberland 已提交
2238
		       int enable_pav)
L
Linus Torvalds 已提交
2239
{
2240 2241
	struct dasd_eckd_private *base_priv;
	struct dasd_eckd_private *start_priv;
2242
	struct dasd_device *startdev = NULL;
L
Linus Torvalds 已提交
2243 2244
	struct dasd_ccw_req *fcp;
	struct eckd_count *ect;
2245
	struct ch_t address;
L
Linus Torvalds 已提交
2246 2247
	struct ccw1 *ccw;
	void *data;
2248
	int rpt;
L
Linus Torvalds 已提交
2249
	int cplength, datasize;
2250
	int i, j;
2251 2252
	int intensity = 0;
	int r0_perm;
2253
	int nr_tracks;
2254
	int use_prefix;
L
Linus Torvalds 已提交
2255

S
Stefan Haberland 已提交
2256
	if (enable_pav)
2257 2258
		startdev = dasd_alias_get_start_dev(base);

2259 2260
	if (!startdev)
		startdev = base;
L
Linus Torvalds 已提交
2261

2262 2263
	start_priv = startdev->private;
	base_priv = base->private;
2264 2265 2266 2267

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

	nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
L
Linus Torvalds 已提交
2268 2269 2270 2271 2272 2273 2274

	/*
	 * 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)
2275
	 *   Bit 4: do not allow storage subsystem to modify record zero
L
Linus Torvalds 已提交
2276 2277
	 * Only some bit combinations do make sense.
	 */
2278 2279 2280 2281 2282 2283 2284
	if (fdata->intensity & 0x10) {
		r0_perm = 0;
		intensity = fdata->intensity & ~0x10;
	} else {
		r0_perm = 1;
		intensity = fdata->intensity;
	}
2285

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

2288
	switch (intensity) {
L
Linus Torvalds 已提交
2289 2290
	case 0x00:	/* Normal format */
	case 0x08:	/* Normal format, use cdl. */
2291
		cplength = 2 + (rpt*nr_tracks);
2292 2293 2294 2295 2296 2297 2298 2299
		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 已提交
2300 2301 2302
		break;
	case 0x01:	/* Write record zero and format track. */
	case 0x09:	/* Write record zero and format track, use cdl. */
2303
		cplength = 2 + rpt * nr_tracks;
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
		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 已提交
2314 2315 2316 2317
		break;
	case 0x04:	/* Invalidate track. */
	case 0x0c:	/* Invalidate track, use cdl. */
		cplength = 3;
2318 2319 2320 2321 2322 2323 2324 2325
		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 已提交
2326 2327
		break;
	default:
2328 2329 2330
		dev_warn(&startdev->cdev->dev,
			 "An I/O control call used incorrect flags 0x%x\n",
			 fdata->intensity);
L
Linus Torvalds 已提交
2331 2332 2333
		return ERR_PTR(-EINVAL);
	}
	/* Allocate the format ccw request. */
2334 2335
	fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
				   datasize, startdev);
L
Linus Torvalds 已提交
2336 2337 2338
	if (IS_ERR(fcp))
		return fcp;

2339
	start_priv->count++;
L
Linus Torvalds 已提交
2340 2341 2342
	data = fcp->data;
	ccw = fcp->cpaddr;

2343
	switch (intensity & ~0x08) {
L
Linus Torvalds 已提交
2344
	case 0x00: /* Normal format. */
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
		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,
2357
				      DASD_ECKD_CCW_WRITE_CKD, startdev, 0);
2358 2359 2360 2361 2362 2363
			/* 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 已提交
2364 2365
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
2366 2367
			      fdata->start_unit, 0, rpt*nr_tracks,
			      DASD_ECKD_CCW_WRITE_CKD, base,
L
Linus Torvalds 已提交
2368 2369 2370 2371
			      fdata->blksize);
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x01: /* Write record zero + format track. */
2372 2373 2374 2375 2376 2377 2378 2379 2380
		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,
2381
			       DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev, 0);
2382 2383
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2384 2385
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
2386 2387 2388
			      fdata->start_unit, 0, rpt * nr_tracks + 1,
			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
			      base->block->bp_block);
L
Linus Torvalds 已提交
2389 2390 2391
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x04: /* Invalidate track. */
2392 2393 2394 2395 2396 2397 2398 2399
		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,
2400
			       DASD_ECKD_CCW_WRITE_CKD, startdev, 0);
2401 2402
			data += sizeof(struct DE_eckd_data);
		}
L
Linus Torvalds 已提交
2403 2404 2405
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
			      fdata->start_unit, 0, 1,
2406
			      DASD_ECKD_CCW_WRITE_CKD, base, 8);
L
Linus Torvalds 已提交
2407 2408 2409
		data += sizeof(struct LO_eckd_data);
		break;
	}
2410 2411 2412 2413 2414 2415 2416 2417 2418

	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 已提交
2419 2420
			ect = (struct eckd_count *) data;
			data += sizeof(struct eckd_count);
2421 2422
			ect->cyl = address.cyl;
			ect->head = address.head;
2423
			ect->record = 0;
L
Linus Torvalds 已提交
2424
			ect->kl = 0;
2425
			ect->dl = 8;
L
Linus Torvalds 已提交
2426
			ccw[-1].flags |= CCW_FLAG_CC;
2427
			ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
L
Linus Torvalds 已提交
2428 2429 2430 2431 2432
			ccw->flags = CCW_FLAG_SLI;
			ccw->count = 8;
			ccw->cda = (__u32)(addr_t) ect;
			ccw++;
		}
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
		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) &&
2460
				    address.cyl == 0 && address.head == 0) {
2461 2462 2463 2464 2465 2466
					if (i < 3) {
						ect->kl = 4;
						ect->dl = sizes_trk0[i] - 4;
					}
				}
				if ((intensity & 0x08) &&
2467
				    address.cyl == 0 && address.head == 1) {
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
					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;
2480 2481
				ccw->cda = (__u32)(addr_t) ect;
				ccw++;
2482 2483
			}
		}
L
Linus Torvalds 已提交
2484
	}
2485 2486 2487

	fcp->startdev = startdev;
	fcp->memdev = startdev;
2488
	fcp->basedev = base;
2489
	fcp->retries = 256;
2490
	fcp->expires = startdev->default_expires * HZ;
2491
	fcp->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
2492
	fcp->status = DASD_CQR_FILLED;
2493

L
Linus Torvalds 已提交
2494 2495 2496
	return fcp;
}

2497 2498 2499 2500 2501
/*
 * 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,
2502 2503
			       struct format_data_t *fdata, int enable_pav,
			       int tpm, struct eckd_count *fmt_buffer, int rpt)
2504
{
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
	struct dasd_ccw_req *ccw_req;

	if (!fmt_buffer) {
		ccw_req = dasd_eckd_build_format(base, fdata, enable_pav);
	} 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;
2520 2521 2522 2523 2524 2525 2526
}

/*
 * Sanity checks on format_data
 */
static int dasd_eckd_format_sanity_checks(struct dasd_device *base,
					  struct format_data_t *fdata)
2527
{
2528
	struct dasd_eckd_private *private = base->private;
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555

	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;
	}
2556 2557 2558 2559 2560 2561 2562 2563
	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,
2564 2565 2566
					 int enable_pav, int tpm,
					 struct eckd_count *fmt_buffer, int rpt,
					 struct irb *irb)
2567
{
2568
	struct dasd_eckd_private *private = base->private;
2569 2570 2571
	struct dasd_ccw_req *cqr, *n;
	struct list_head format_queue;
	struct dasd_device *device;
2572
	char *sense = NULL;
2573 2574 2575 2576 2577 2578 2579
	int old_start, old_stop, format_step;
	int step, retry;
	int rc;

	rc = dasd_eckd_format_sanity_checks(base, fdata);
	if (rc)
		return rc;
2580 2581 2582

	INIT_LIST_HEAD(&format_queue);

2583
	old_start = fdata->start_unit;
2584
	old_stop = fdata->stop_unit;
2585

2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
	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);
	}

2598 2599 2600 2601 2602 2603 2604 2605
	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;
			}
2606

2607
			cqr = dasd_eckd_format_build_ccw_req(base, fdata,
2608 2609
							     enable_pav, tpm,
							     fmt_buffer, rpt);
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
			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);
2626

2627 2628 2629 2630
			if (fmt_buffer) {
				step = fdata->stop_unit - fdata->start_unit + 1;
				fmt_buffer += rpt * step;
			}
2631 2632
			fdata->start_unit = fdata->stop_unit + 1;
			fdata->stop_unit = old_stop;
2633 2634
		}

2635 2636 2637 2638 2639
		rc = dasd_sleep_on_queue(&format_queue);

out_err:
		list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
			device = cqr->startdev;
2640
			private = device->private;
2641 2642 2643 2644 2645 2646 2647 2648 2649 2650

			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));
				}
2651
				rc = -EIO;
2652
			}
2653 2654 2655 2656
			list_del_init(&cqr->blocklist);
			dasd_sfree_request(cqr, device);
			private->count--;
		}
2657

2658
		if (rc && rc != -EIO)
2659
			goto out;
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
		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;
		}
2676

2677
	} while (retry);
2678

2679 2680 2681
out:
	fdata->start_unit = old_start;
	fdata->stop_unit = old_stop;
2682 2683 2684 2685

	return rc;
}

2686 2687 2688
static int dasd_eckd_format_device(struct dasd_device *base,
				   struct format_data_t *fdata, int enable_pav)
{
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 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 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
	return dasd_eckd_format_process_data(base, fdata, enable_pav, 0, NULL,
					     0, NULL);
}

/*
 * 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;
2908 2909
}

2910
static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
L
Linus Torvalds 已提交
2911
{
2912 2913 2914 2915
	if (cqr->retries < 0) {
		cqr->status = DASD_CQR_FAILED;
		return;
	}
2916 2917 2918 2919
	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;
2920
		cqr->lpm = dasd_path_get_opm(cqr->block->base);
L
Linus Torvalds 已提交
2921
	}
2922
};
L
Linus Torvalds 已提交
2923 2924 2925 2926

static dasd_erp_fn_t
dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
{
2927
	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
L
Linus Torvalds 已提交
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
	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;
}

2949 2950 2951
static void dasd_eckd_check_for_device_change(struct dasd_device *device,
					      struct dasd_ccw_req *cqr,
					      struct irb *irb)
2952 2953
{
	char mask;
2954
	char *sense = NULL;
2955
	struct dasd_eckd_private *private = device->private;
2956 2957 2958

	/* first of all check for state change pending interrupt */
	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
2959
	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
2960 2961 2962 2963
		/*
		 * for alias only, not in offline processing
		 * and only if not suspended
		 */
2964
		if (!device->block && private->lcu &&
2965
		    device->state == DASD_STATE_ONLINE &&
2966 2967
		    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
		    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
2968 2969 2970
			/* schedule worker to reload device */
			dasd_reload_device(device);
		}
2971 2972 2973 2974
		dasd_generic_handle_state_change(device);
		return;
	}

2975
	sense = dasd_get_sense(irb);
2976 2977 2978 2979
	if (!sense)
		return;

	/* summary unit check */
2980
	if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
2981
	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
		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);

3003 3004 3005
		return;
	}

3006
	/* service information message SIM */
3007
	if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
3008 3009
	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
		dasd_3990_erp_handle_sim(device, sense);
3010 3011 3012
		return;
	}

3013 3014 3015
	/* loss of device reservation is handled via base devices only
	 * as alias devices may be used with several bases
	 */
3016 3017
	if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
	    (sense[7] == 0x3F) &&
3018 3019 3020 3021 3022 3023 3024
	    (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");
3025
	}
3026
}
3027 3028 3029

static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
					       struct dasd_device *startdev,
3030
					       struct dasd_block *block,
3031 3032 3033 3034 3035 3036 3037 3038 3039
					       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 已提交
3040 3041 3042 3043 3044 3045
{
	struct dasd_eckd_private *private;
	unsigned long *idaws;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
3046
	struct req_iterator iter;
3047
	struct bio_vec bv;
L
Linus Torvalds 已提交
3048
	char *dst;
3049
	unsigned int off;
L
Linus Torvalds 已提交
3050
	int count, cidaw, cplength, datasize;
3051
	sector_t recid;
L
Linus Torvalds 已提交
3052
	unsigned char cmd, rcmd;
3053 3054
	int use_prefix;
	struct dasd_device *basedev;
L
Linus Torvalds 已提交
3055

3056
	basedev = block->base;
3057
	private = basedev->private;
L
Linus Torvalds 已提交
3058 3059 3060 3061 3062 3063
	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);
3064

L
Linus Torvalds 已提交
3065 3066 3067
	/* Check struct bio and count the number of blocks for the request. */
	count = 0;
	cidaw = 0;
3068
	rq_for_each_segment(bv, req, iter) {
3069
		if (bv.bv_len & (blksize - 1))
3070 3071
			/* Eckd can only do full blocks. */
			return ERR_PTR(-EINVAL);
3072 3073 3074
		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 已提交
3075 3076 3077 3078
	}
	/* Paranoia. */
	if (count != last_rec - first_rec + 1)
		return ERR_PTR(-EINVAL);
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096

	/* 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 已提交
3097 3098 3099 3100 3101 3102 3103 3104
	/* 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. */
3105 3106
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				   startdev);
L
Linus Torvalds 已提交
3107 3108 3109
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
	/* 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,
3124
				  last_trk, cmd, basedev, 0) == -EAGAIN) {
3125 3126 3127 3128 3129 3130 3131 3132
			/* 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 已提交
3133
	}
L
Linus Torvalds 已提交
3134 3135 3136 3137 3138 3139 3140
	/* 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,
3141
			      last_rec - recid + 1, cmd, basedev, blksize);
L
Linus Torvalds 已提交
3142
	}
3143
	rq_for_each_segment(bv, req, iter) {
3144
		dst = page_address(bv.bv_page) + bv.bv_offset;
L
Linus Torvalds 已提交
3145 3146
		if (dasd_page_cache) {
			char *copy = kmem_cache_alloc(dasd_page_cache,
C
Christoph Lameter 已提交
3147
						      GFP_DMA | __GFP_NOWARN);
L
Linus Torvalds 已提交
3148
			if (copy && rq_data_dir(req) == WRITE)
3149
				memcpy(copy + bv.bv_offset, dst, bv.bv_len);
L
Linus Torvalds 已提交
3150
			if (copy)
3151
				dst = copy + bv.bv_offset;
L
Linus Torvalds 已提交
3152
		}
3153
		for (off = 0; off < bv.bv_len; off += blksize) {
L
Linus Torvalds 已提交
3154 3155 3156 3157 3158 3159 3160 3161 3162
			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);
3163 3164
					if (count < blksize &&
					    rq_data_dir(req) == READ)
L
Linus Torvalds 已提交
3165 3166 3167 3168 3169 3170
						memset(dst + count, 0xe5,
						       blksize - count);
				}
				ccw[-1].flags |= CCW_FLAG_CC;
				locate_record(ccw++, LO_data++,
					      trkid, recoffs + 1,
3171
					      1, rcmd, basedev, count);
L
Linus Torvalds 已提交
3172 3173 3174 3175 3176 3177 3178
			}
			/* 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,
3179
					      cmd, basedev, count);
L
Linus Torvalds 已提交
3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197
			}
			/* 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++;
		}
	}
3198 3199
	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
3200
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3201 3202 3203
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
3204
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
3205
	cqr->lpm = dasd_path_get_ppm(startdev);
3206
	cqr->retries = startdev->default_retries;
3207
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
3208 3209 3210 3211
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228
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;
3229
	struct bio_vec bv;
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260
	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;

3261
	datasize = sizeof(struct PFX_eckd_data) + cidaw * sizeof(unsigned long);
3262 3263

	/* Allocate the ccw request. */
3264 3265
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				   startdev);
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
	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;
3300
	idaw_dst = NULL;
3301 3302
	idaw_len = 0;
	rq_for_each_segment(bv, req, iter) {
3303 3304
		dst = page_address(bv.bv_page) + bv.bv_offset;
		seg_len = bv.bv_len;
3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
		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;
3321 3322 3323
				/* first idaw for a ccw may start anywhere */
				if (!idaw_dst)
					idaw_dst = dst;
3324
			}
3325 3326
			/* If we start a new idaw, we must make sure that it
			 * starts on an IDA_BLOCK_SIZE boundary.
3327 3328 3329 3330
			 * If we continue an idaw, we must make sure that the
			 * current segment begins where the so far accumulated
			 * idaw ends
			 */
3331 3332 3333 3334 3335 3336 3337
			if (!idaw_dst) {
				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
					dasd_sfree_request(cqr, startdev);
					return ERR_PTR(-ERANGE);
				} else
					idaw_dst = dst;
			}
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
			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);
3362
				idaw_dst = NULL;
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
				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;
3375
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
3376
	cqr->lpm = dasd_path_get_ppm(startdev);
3377
	cqr->retries = startdev->default_retries;
3378
	cqr->buildclk = get_tod_clock();
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
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

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

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

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


	/* setup prefix data */
3409 3410
	basepriv = basedev->private;
	startpriv = startdev->private;
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
	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 */
3421 3422 3423 3424
	if (startpriv->uid.type == UA_BASE_PAV_ALIAS)
		pfxdata.validity.verify_base = 1;

	if (startpriv->uid.type == UA_HYPER_PAV_ALIAS) {
3425
		pfxdata.validity.verify_base = 1;
3426
		pfxdata.validity.hyper_pav = 1;
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443
	}

	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;
3444
		rc = set_timestamp(NULL, dedata, basedev);
3445 3446 3447 3448 3449
		dedata->ga_extended |= 0x42;
		lredata->operation.orientation = 0x0;
		lredata->operation.operation = 0x3F;
		lredata->extended_operation = 0x23;
		lredata->auxiliary.check_bytes = 0x2;
3450 3451 3452 3453 3454 3455 3456
		/*
		 * 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' */
3457 3458
		pfx_cmd = DASD_ECKD_CCW_PFX;
		break;
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
	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;
3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
	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;
		}
	}

3516 3517 3518 3519 3520 3521 3522 3523 3524
	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;
	}
3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
	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);
3536
	return PTR_RET(dcw);
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
}

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;
3554
	struct bio_vec bv;
3555 3556 3557 3558 3559 3560 3561 3562 3563
	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;
3564 3565 3566 3567 3568 3569
	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;
3570
	int ret;
3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584

	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.
3585 3586
	 * In the case of write requests, additional tidaws may
	 * be needed when a segment crosses a track boundary.
3587 3588 3589 3590 3591 3592
	 */
	trkcount = last_trk - first_trk + 1;
	ctidaw = 0;
	rq_for_each_segment(bv, req, iter) {
		++ctidaw;
	}
3593 3594
	if (rq_data_dir(req) == WRITE)
		ctidaw += (last_trk - first_trk);
3595 3596 3597

	/* Allocate the ccw request. */
	itcw_size = itcw_calc_size(0, ctidaw, 0);
3598
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
	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);
3610
	if (IS_ERR(itcw)) {
3611 3612
		ret = -EINVAL;
		goto out_error;
3613
	}
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
	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.
		 */
3624 3625
		ret = -EAGAIN;
		goto out_error;
3626
	}
3627
	len_to_track_end = 0;
3628 3629 3630 3631 3632
	/*
	 * 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.
3633 3634
	 * 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.
3635
	 */
3636 3637 3638 3639
	if (rq_data_dir(req) == WRITE) {
		new_track = 1;
		recid = first_rec;
		rq_for_each_segment(bv, req, iter) {
3640 3641
			dst = page_address(bv.bv_page) + bv.bv_offset;
			seg_len = bv.bv_len;
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663
			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);
3664 3665 3666 3667
				if (IS_ERR(last_tidaw)) {
					ret = -EINVAL;
					goto out_error;
				}
3668 3669 3670 3671 3672
				dst += part_len;
			}
		}
	} else {
		rq_for_each_segment(bv, req, iter) {
3673
			dst = page_address(bv.bv_page) + bv.bv_offset;
3674
			last_tidaw = itcw_add_tidaw(itcw, 0x00,
3675
						    dst, bv.bv_len);
3676 3677 3678 3679
			if (IS_ERR(last_tidaw)) {
				ret = -EINVAL;
				goto out_error;
			}
3680
		}
3681
	}
3682 3683
	last_tidaw->flags |= TIDAW_FLAGS_LAST;
	last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
3684 3685 3686 3687 3688
	itcw_finalize(itcw);

	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3689
	cqr->cpmode = 1;
3690 3691 3692
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
3693
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
3694
	cqr->lpm = dasd_path_get_ppm(startdev);
3695
	cqr->retries = startdev->default_retries;
3696
	cqr->buildclk = get_tod_clock();
3697 3698
	cqr->status = DASD_CQR_FILLED;
	return cqr;
3699 3700 3701
out_error:
	dasd_sfree_request(cqr, startdev);
	return ERR_PTR(ret);
3702 3703 3704 3705 3706 3707
}

static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req)
{
3708
	int cmdrtd, cmdwtd;
3709
	int use_prefix;
3710
	int fcx_multitrack;
3711
	struct dasd_eckd_private *private;
3712 3713 3714 3715 3716 3717
	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;
3718
	unsigned int data_size;
3719 3720 3721
	struct dasd_ccw_req *cqr;

	basedev = block->base;
3722
	private = basedev->private;
3723 3724 3725 3726

	/* Calculate number of blocks/records per track. */
	blksize = block->bp_block;
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3727 3728
	if (blk_per_trk == 0)
		return ERR_PTR(-EINVAL);
3729
	/* Calculate record id of first and last block. */
3730
	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
3731 3732
	first_offs = sector_div(first_trk, blk_per_trk);
	last_rec = last_trk =
3733
		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3734 3735 3736
	last_offs = sector_div(last_trk, blk_per_trk);
	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);

3737 3738
	fcx_multitrack = private->features.feature[40] & 0x20;
	data_size = blk_rq_bytes(req);
3739 3740
	if (data_size % blksize)
		return ERR_PTR(-EINVAL);
3741 3742 3743
	/* tpm write request add CBC data on each track boundary */
	if (rq_data_dir(req) == WRITE)
		data_size += (last_trk - first_trk) * 4;
3744 3745 3746 3747 3748 3749 3750 3751 3752

	/* 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 */
3753 3754
	} else if ((data_size <= private->fcx_max_data)
		   && (fcx_multitrack || (first_trk == last_trk))) {
3755 3756 3757 3758 3759
		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);
3760 3761
		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
		    (PTR_ERR(cqr) != -ENOMEM))
3762 3763 3764 3765 3766 3767 3768 3769 3770
			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);
3771 3772
		if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
		    (PTR_ERR(cqr) != -ENOMEM))
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783
			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;
}

3784 3785 3786
static struct dasd_ccw_req *dasd_eckd_build_cp_raw(struct dasd_device *startdev,
						   struct dasd_block *block,
						   struct request *req)
3787
{
3788 3789 3790 3791 3792
	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;
3793 3794
	struct dasd_device *basedev;
	struct req_iterator iter;
3795 3796 3797 3798 3799 3800
	struct dasd_ccw_req *cqr;
	unsigned int first_offs;
	unsigned int trkcount;
	unsigned long *idaws;
	unsigned int size;
	unsigned char cmd;
3801
	struct bio_vec bv;
3802 3803 3804
	struct ccw1 *ccw;
	int use_prefix;
	void *data;
3805 3806 3807 3808
	char *dst;

	/*
	 * raw track access needs to be mutiple of 64k and on 64k boundary
3809 3810
	 * For read requests we can fix an incorrect alignment by padding
	 * the request with dummy pages.
3811
	 */
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822
	start_padding_sectors = blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK;
	end_sector_offset = (blk_rq_pos(req) + blk_rq_sectors(req)) %
		DASD_RAW_SECTORS_PER_TRACK;
	end_padding_sectors = (DASD_RAW_SECTORS_PER_TRACK - end_sector_offset) %
		DASD_RAW_SECTORS_PER_TRACK;
	basedev = block->base;
	if ((start_padding_sectors || end_padding_sectors) &&
	    (rq_data_dir(req) == WRITE)) {
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "raw write not track aligned (%lu,%lu) req %p",
			      start_padding_sectors, end_padding_sectors, req);
3823
		return ERR_PTR(-EINVAL);
3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835
	}

	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;
3836 3837
	else
		return ERR_PTR(-EINVAL);
3838 3839 3840 3841 3842 3843 3844 3845

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

	/*
3846 3847
	 * 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.
3848
	 */
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859
	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);
3860

3861
	datasize = size + cidaw * sizeof(unsigned long);
3862 3863 3864 3865 3866

	/* Allocate the ccw request. */
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
				   datasize, startdev);
	if (IS_ERR(cqr))
3867 3868
		return cqr;

3869
	ccw = cqr->cpaddr;
3870
	data = cqr->data;
3871

3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
	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);
3882 3883
	}

3884
	idaws = (unsigned long *)(cqr->data + size);
3885
	len_to_track_end = 0;
3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899
	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);
	}
3900
	rq_for_each_segment(bv, req, iter) {
3901 3902
		dst = page_address(bv.bv_page) + bv.bv_offset;
		seg_len = bv.bv_len;
3903 3904
		if (cmd == DASD_ECKD_CCW_READ_TRACK)
			memset(dst, 0, seg_len);
3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919
		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);
	}
3920 3921
	for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
		idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3922 3923 3924 3925 3926 3927 3928
	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;
3929
	cqr->lpm = dasd_path_get_ppm(startdev);
3930
	cqr->retries = startdev->default_retries;
3931
	cqr->buildclk = get_tod_clock();
3932 3933 3934 3935 3936 3937
	cqr->status = DASD_CQR_FILLED;

	return cqr;
}


L
Linus Torvalds 已提交
3938 3939 3940 3941 3942
static int
dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
{
	struct dasd_eckd_private *private;
	struct ccw1 *ccw;
3943
	struct req_iterator iter;
3944
	struct bio_vec bv;
L
Linus Torvalds 已提交
3945 3946 3947
	char *dst, *cda;
	unsigned int blksize, blk_per_trk, off;
	sector_t recid;
3948
	int status;
L
Linus Torvalds 已提交
3949 3950 3951

	if (!dasd_page_cache)
		goto out;
3952
	private = cqr->block->base->private;
3953
	blksize = cqr->block->bp_block;
L
Linus Torvalds 已提交
3954
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3955
	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
L
Linus Torvalds 已提交
3956 3957 3958 3959 3960
	ccw = cqr->cpaddr;
	/* Skip over define extent & locate record. */
	ccw++;
	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
		ccw++;
3961
	rq_for_each_segment(bv, req, iter) {
3962 3963
		dst = page_address(bv.bv_page) + bv.bv_offset;
		for (off = 0; off < bv.bv_len; off += blksize) {
L
Linus Torvalds 已提交
3964 3965 3966 3967 3968 3969 3970 3971 3972 3973
			/* 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)
3974
						memcpy(dst, cda, bv.bv_len);
L
Linus Torvalds 已提交
3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
					kmem_cache_free(dasd_page_cache,
					    (void *)((addr_t)cda & PAGE_MASK));
				}
				dst = NULL;
			}
			ccw++;
			recid++;
		}
	}
out:
	status = cqr->status == DASD_CQR_DONE;
3986
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
3987 3988 3989
	return status;
}

3990
/*
3991
 * Modify ccw/tcw in cqr so it can be started on a base device.
3992 3993 3994 3995 3996 3997 3998 3999 4000
 *
 * 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;
4001 4002 4003 4004 4005 4006 4007 4008 4009
	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];
4010 4011
		pfxdata->validity.verify_base = 0;
		pfxdata->validity.hyper_pav = 0;
4012 4013 4014 4015 4016 4017 4018
	} 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;
		}
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
	}
}

#define DASD_ECKD_CHANQ_MAX_SIZE 4

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

	startdev = dasd_alias_get_start_dev(base);
	if (!startdev)
		startdev = base;
4036
	private = startdev->private;
4037 4038 4039 4040 4041
	if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
		return ERR_PTR(-EBUSY);

	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
	private->count++;
4042
	if ((base->features & DASD_FEATURE_USERAW))
4043
		cqr = dasd_eckd_build_cp_raw(startdev, block, req);
4044 4045
	else
		cqr = dasd_eckd_build_cp(startdev, block, req);
4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058
	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);
4059
	private = cqr->memdev->private;
4060 4061 4062 4063 4064
	private->count--;
	spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
	return dasd_eckd_free_cp(cqr, req);
}

L
Linus Torvalds 已提交
4065 4066 4067 4068
static int
dasd_eckd_fill_info(struct dasd_device * device,
		    struct dasd_information2_t * info)
{
4069
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
4070 4071 4072 4073

	info->label_block = 2;
	info->FBA_layout = private->uses_cdl ? 0 : 1;
	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
4074
	info->characteristics_size = sizeof(private->rdc_data);
L
Linus Torvalds 已提交
4075
	memcpy(info->characteristics, &private->rdc_data,
4076
	       sizeof(private->rdc_data));
4077 4078 4079 4080
	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 已提交
4081 4082 4083 4084 4085 4086 4087 4088 4089
	return 0;
}

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

/*
 * Release device ioctl.
4090
 * Buils a channel programm to releases a prior reserved
L
Linus Torvalds 已提交
4091 4092 4093
 * (see dasd_eckd_reserve) device.
 */
static int
4094
dasd_eckd_release(struct dasd_device *device)
L
Linus Torvalds 已提交
4095 4096 4097
{
	struct dasd_ccw_req *cqr;
	int rc;
4098
	struct ccw1 *ccw;
4099
	int useglobal;
L
Linus Torvalds 已提交
4100 4101 4102 4103

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

4104
	useglobal = 0;
4105
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
4106
	if (IS_ERR(cqr)) {
4107 4108 4109 4110 4111 4112 4113 4114 4115
		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 已提交
4116
	}
4117 4118 4119 4120 4121
	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;
4122 4123
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
4124
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4125
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4126
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
4127
	cqr->expires = 2 * HZ;
4128
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
4129 4130 4131
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
4132 4133
	if (!rc)
		clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
4134

4135 4136 4137 4138
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
4139 4140 4141 4142 4143 4144
	return rc;
}

/*
 * Reserve device ioctl.
 * Options are set to 'synchronous wait for interrupt' and
4145 4146
 * 'timeout the request'. This leads to a terminate IO if
 * the interrupt is outstanding for a certain time.
L
Linus Torvalds 已提交
4147 4148
 */
static int
4149
dasd_eckd_reserve(struct dasd_device *device)
L
Linus Torvalds 已提交
4150 4151 4152
{
	struct dasd_ccw_req *cqr;
	int rc;
4153
	struct ccw1 *ccw;
4154
	int useglobal;
L
Linus Torvalds 已提交
4155 4156 4157 4158

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

4159
	useglobal = 0;
4160
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
4161
	if (IS_ERR(cqr)) {
4162 4163 4164 4165 4166 4167 4168 4169 4170
		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 已提交
4171
	}
4172 4173 4174 4175 4176
	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;
4177 4178
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
4179
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4180
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4181
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
4182
	cqr->expires = 2 * HZ;
4183
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
4184 4185 4186
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
4187 4188
	if (!rc)
		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
4189

4190 4191 4192 4193
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
4194 4195 4196 4197 4198
	return rc;
}

/*
 * Steal lock ioctl - unconditional reserve device.
4199
 * Buils a channel programm to break a device's reservation.
L
Linus Torvalds 已提交
4200 4201 4202
 * (unconditional reserve)
 */
static int
4203
dasd_eckd_steal_lock(struct dasd_device *device)
L
Linus Torvalds 已提交
4204 4205 4206
{
	struct dasd_ccw_req *cqr;
	int rc;
4207
	struct ccw1 *ccw;
4208
	int useglobal;
L
Linus Torvalds 已提交
4209 4210 4211 4212

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

4213
	useglobal = 0;
4214
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
4215
	if (IS_ERR(cqr)) {
4216 4217 4218 4219 4220 4221 4222 4223 4224
		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 已提交
4225
	}
4226 4227 4228 4229 4230
	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;
4231 4232
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
4233
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4234
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4235
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
4236
	cqr->expires = 2 * HZ;
4237
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
4238 4239 4240
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);
4241 4242
	if (!rc)
		set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
L
Linus Torvalds 已提交
4243

4244 4245 4246 4247
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
4248 4249 4250
	return rc;
}

4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
/*
 * SNID - Sense Path Group ID
 * This ioctl may be used in situations where I/O is stalled due to
 * a reserve, so if the normal dasd_smalloc_request fails, we use the
 * preallocated dasd_reserve_req.
 */
static int dasd_eckd_snid(struct dasd_device *device,
			  void __user *argp)
{
	struct dasd_ccw_req *cqr;
	int rc;
	struct ccw1 *ccw;
	int useglobal;
	struct dasd_snid_ioctl_data usrparm;

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

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

	useglobal = 0;
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
				   sizeof(struct dasd_snid_data), device);
	if (IS_ERR(cqr)) {
		mutex_lock(&dasd_reserve_mutex);
		useglobal = 1;
		cqr = &dasd_reserve_req->cqr;
		memset(cqr, 0, sizeof(*cqr));
		memset(&dasd_reserve_req->ccw, 0,
		       sizeof(dasd_reserve_req->ccw));
		cqr->cpaddr = &dasd_reserve_req->ccw;
		cqr->data = &dasd_reserve_req->data;
		cqr->magic = DASD_ECKD_MAGIC;
	}
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_SNID;
	ccw->flags |= CCW_FLAG_SLI;
	ccw->count = 12;
	ccw->cda = (__u32)(addr_t) cqr->data;
	cqr->startdev = device;
	cqr->memdev = device;
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4295
	set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
4296 4297
	cqr->retries = 5;
	cqr->expires = 10 * HZ;
4298
	cqr->buildclk = get_tod_clock();
4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318
	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 已提交
4319 4320 4321 4322
/*
 * Read performance statistics
 */
static int
4323
dasd_eckd_performance(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
4324 4325 4326 4327 4328 4329 4330
{
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_perf_stats_t *stats;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

4331
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
4332 4333
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_perf_stats_t)),
L
Linus Torvalds 已提交
4334 4335
				   device);
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
4336
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
L
Linus Torvalds 已提交
4337 4338 4339
			    "Could not allocate initialization request");
		return PTR_ERR(cqr);
	}
4340 4341
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
4342
	cqr->retries = 0;
4343
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
L
Linus Torvalds 已提交
4344 4345 4346 4347
	cqr->expires = 10 * HZ;

	/* Prepare for Read Subsystem Data */
	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4348
	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
L
Linus Torvalds 已提交
4349
	prssdp->order = PSF_ORDER_PRSSD;
4350
	prssdp->suborder = 0x01;	/* Performance Statistics */
L
Linus Torvalds 已提交
4351 4352 4353 4354
	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */

	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
4355
	ccw->count = sizeof(struct dasd_psf_prssd_data);
L
Linus Torvalds 已提交
4356 4357 4358 4359 4360
	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);
4361
	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
L
Linus Torvalds 已提交
4362 4363 4364

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

4368
	cqr->buildclk = get_tod_clock();
L
Linus Torvalds 已提交
4369 4370 4371 4372 4373
	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);
4374 4375 4376
		if (copy_to_user(argp, stats,
				 sizeof(struct dasd_rssd_perf_stats_t)))
			rc = -EFAULT;
L
Linus Torvalds 已提交
4377
	}
4378
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
4379 4380 4381 4382 4383 4384 4385 4386
	return rc;
}

/*
 * Get attributes (cache operations)
 * Returnes the cache attributes used in Define Extend (DE).
 */
static int
4387
dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
4388
{
4389
	struct dasd_eckd_private *private = device->private;
4390
	struct attrib_data_t attrib = private->attrib;
L
Linus Torvalds 已提交
4391 4392 4393 4394
	int rc;

        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
4395
	if (!argp)
L
Linus Torvalds 已提交
4396 4397
                return -EINVAL;

4398 4399
	rc = 0;
	if (copy_to_user(argp, (long *) &attrib,
4400
			 sizeof(struct attrib_data_t)))
4401
		rc = -EFAULT;
L
Linus Torvalds 已提交
4402 4403 4404 4405 4406 4407 4408 4409 4410

	return rc;
}

/*
 * Set attributes (cache operations)
 * Stores the attributes for cache operation to be used in Define Extend (DE).
 */
static int
4411
dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
4412
{
4413
	struct dasd_eckd_private *private = device->private;
L
Linus Torvalds 已提交
4414 4415 4416 4417
	struct attrib_data_t attrib;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
4418
	if (!argp)
L
Linus Torvalds 已提交
4419 4420
		return -EINVAL;

4421
	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
L
Linus Torvalds 已提交
4422 4423 4424
		return -EFAULT;
	private->attrib = attrib;

S
Stefan Haberland 已提交
4425 4426 4427
	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 已提交
4428 4429 4430
	return 0;
}

4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
/*
 * 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;
4441
	char psf0, psf1;
4442 4443
	int rc;

4444 4445 4446 4447
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
		return -EACCES;
	psf0 = psf1 = 0;

4448 4449 4450 4451
	/* Copy parms from caller */
	rc = -EFAULT;
	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
		goto out;
4452
	if (is_compat_task()) {
4453
		/* Make sure pointers are sane even on 31 bit. */
4454
		rc = -EINVAL;
4455 4456 4457 4458 4459 4460
		if ((usrparm.psf_data >> 32) != 0)
			goto out;
		if ((usrparm.rssd_result >> 32) != 0)
			goto out;
		usrparm.psf_data &= 0x7fffffffULL;
		usrparm.rssd_result &= 0x7fffffffULL;
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475
	}
	/* 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;
4476 4477
	psf0 = psf_data[0];
	psf1 = psf_data[1];
4478 4479

	/* setup CCWs for PSF + RSSD */
4480
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
4481
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
4482
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4483 4484 4485 4486 4487 4488 4489 4490 4491
			"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;
4492
	cqr->buildclk = get_tod_clock();
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527
	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:
4528 4529 4530
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
		      (int) psf0, (int) psf1, rc);
4531 4532 4533
	return rc;
}

4534
static int
4535
dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
4536
{
4537 4538
	struct dasd_device *device = block->base;

4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551
	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);
4552 4553
	case BIODASDSNID:
		return dasd_eckd_snid(device, argp);
4554 4555
	case BIODASDSYMMIO:
		return dasd_symm_io(device, argp);
4556
	default:
4557
		return -ENOTTY;
4558 4559 4560
	}
}

H
Horst Hummel 已提交
4561 4562 4563 4564
/*
 * Dump the range of CCWs into 'page' buffer
 * and return number of printed chars.
 */
4565
static int
H
Horst Hummel 已提交
4566 4567 4568 4569 4570 4571 4572
dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
{
	int len, count;
	char *datap;

	len = 0;
	while (from <= to) {
4573
		len += sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
			       " 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 已提交
4595
static void
4596 4597
dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
			 char *reason)
S
Stefan Haberland 已提交
4598 4599
{
	u64 *sense;
4600
	u64 *stat;
4601 4602

	sense = (u64 *) dasd_get_sense(irb);
4603
	stat = (u64 *) &irb->scsw;
S
Stefan Haberland 已提交
4604
	if (sense) {
4605 4606 4607
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
			      "%016llx %016llx %016llx %016llx",
			      reason, *stat, *((u32 *) (stat + 1)),
S
Stefan Haberland 已提交
4608
			      sense[0], sense[1], sense[2], sense[3]);
S
Stefan Haberland 已提交
4609
	} else {
4610 4611 4612
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
			      reason, *stat, *((u32 *) (stat + 1)),
			      "NO VALID SENSE");
S
Stefan Haberland 已提交
4613 4614 4615
	}
}

L
Linus Torvalds 已提交
4616 4617 4618 4619
/*
 * Print sense data and related channel program.
 * Parts are printed because printk buffer is only 1024 bytes.
 */
4620
static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
4621
				 struct dasd_ccw_req *req, struct irb *irb)
L
Linus Torvalds 已提交
4622 4623
{
	char *page;
H
Horst Hummel 已提交
4624 4625
	struct ccw1 *first, *last, *fail, *from, *to;
	int len, sl, sct;
L
Linus Torvalds 已提交
4626 4627 4628

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
4629 4630
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "No memory to dump sense data\n");
L
Linus Torvalds 已提交
4631 4632
		return;
	}
H
Horst Hummel 已提交
4633
	/* dump the sense data */
4634
	len = sprintf(page, PRINTK_HEADER
L
Linus Torvalds 已提交
4635
		      " I/O status report for device %s:\n",
4636
		      dev_name(&device->cdev->dev));
4637
	len += sprintf(page + len, PRINTK_HEADER
4638 4639 4640 4641 4642 4643
		       " 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);
4644
	len += sprintf(page + len, PRINTK_HEADER
L
Linus Torvalds 已提交
4645
		       " device %s: Failing CCW: %p\n",
4646
		       dev_name(&device->cdev->dev),
4647
		       (void *) (addr_t) irb->scsw.cmd.cpa);
L
Linus Torvalds 已提交
4648 4649
	if (irb->esw.esw0.erw.cons) {
		for (sl = 0; sl < 4; sl++) {
4650
			len += sprintf(page + len, PRINTK_HEADER
L
Linus Torvalds 已提交
4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662
				       " 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 */
4663
			sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4664 4665 4666 4667
				" 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 已提交
4668 4669
		} else {
			/* 32 Byte Sense Data */
4670
			sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4671 4672 4673
				" 32 Byte: Format: %x "
				"Exception class %x\n",
				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
L
Linus Torvalds 已提交
4674 4675
		}
	} else {
4676
		sprintf(page + len, PRINTK_HEADER
H
Horst Hummel 已提交
4677
			" SORRY - NO VALID SENSE AVAILABLE\n");
L
Linus Torvalds 已提交
4678
	}
4679
	printk(KERN_ERR "%s", page);
H
Horst Hummel 已提交
4680

4681 4682 4683 4684 4685 4686 4687
	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);
4688
		len = sprintf(page, PRINTK_HEADER
4689 4690
			      " Related CP in req: %p\n", req);
		dasd_eckd_dump_ccw_range(first, to, page + len);
4691
		printk(KERN_ERR "%s", page);
L
Linus Torvalds 已提交
4692

4693 4694 4695 4696
		/* print failing CCW area (maximum 4) */
		/* scsw->cda is either valid or zero  */
		len = 0;
		from = ++to;
4697 4698
		fail = (struct ccw1 *)(addr_t)
				irb->scsw.cmd.cpa; /* failing CCW */
4699 4700
		if (from <  fail - 2) {
			from = fail - 2;     /* there is a gap - print header */
4701
			len += sprintf(page, PRINTK_HEADER "......\n");
4702 4703 4704 4705 4706 4707 4708 4709
		}
		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 */
4710
			len += sprintf(page + len, PRINTK_HEADER "......\n");
4711 4712 4713
		}
		len += dasd_eckd_dump_ccw_range(from, last, page + len);
		if (len > 0)
4714
			printk(KERN_ERR "%s", page);
L
Linus Torvalds 已提交
4715 4716 4717 4718
	}
	free_page((unsigned long) page);
}

4719 4720 4721 4722 4723 4724 4725 4726 4727 4728

/*
 * 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;
4729
	u8 *sense, *rcq;
4730 4731 4732

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
4733
		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
4734 4735 4736 4737
			    "No memory to dump sense data");
		return;
	}
	/* dump the sense data */
4738
	len = sprintf(page, PRINTK_HEADER
4739 4740
		      " I/O status report for device %s:\n",
		      dev_name(&device->cdev->dev));
4741
	len += sprintf(page + len, PRINTK_HEADER
4742 4743 4744 4745 4746
		       " 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),
4747 4748
		       irb->scsw.tm.fcxs,
		       (irb->scsw.tm.ifob << 7) | irb->scsw.tm.sesq,
4749
		       req ? req->intrc : 0);
4750
	len += sprintf(page + len, PRINTK_HEADER
4751 4752 4753 4754 4755 4756
		       " device %s: Failing TCW: %p\n",
		       dev_name(&device->cdev->dev),
		       (void *) (addr_t) irb->scsw.tm.tcw);

	tsb = NULL;
	sense = NULL;
4757
	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
4758 4759 4760
		tsb = tcw_get_tsb(
			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);

4761
	if (tsb) {
4762
		len += sprintf(page + len, PRINTK_HEADER
4763
			       " tsb->length %d\n", tsb->length);
4764
		len += sprintf(page + len, PRINTK_HEADER
4765
			       " tsb->flags %x\n", tsb->flags);
4766
		len += sprintf(page + len, PRINTK_HEADER
4767
			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
4768
		len += sprintf(page + len, PRINTK_HEADER
4769 4770
			       " tsb->count %d\n", tsb->count);
		residual = tsb->count - 28;
4771
		len += sprintf(page + len, PRINTK_HEADER
4772 4773 4774 4775
			       " residual %d\n", residual);

		switch (tsb->flags & 0x07) {
		case 1:	/* tsa_iostat */
4776
			len += sprintf(page + len, PRINTK_HEADER
4777 4778
			       " tsb->tsa.iostat.dev_time %d\n",
				       tsb->tsa.iostat.dev_time);
4779
			len += sprintf(page + len, PRINTK_HEADER
4780 4781
			       " tsb->tsa.iostat.def_time %d\n",
				       tsb->tsa.iostat.def_time);
4782
			len += sprintf(page + len, PRINTK_HEADER
4783 4784
			       " tsb->tsa.iostat.queue_time %d\n",
				       tsb->tsa.iostat.queue_time);
4785
			len += sprintf(page + len, PRINTK_HEADER
4786 4787
			       " tsb->tsa.iostat.dev_busy_time %d\n",
				       tsb->tsa.iostat.dev_busy_time);
4788
			len += sprintf(page + len, PRINTK_HEADER
4789 4790 4791 4792 4793
			       " tsb->tsa.iostat.dev_act_time %d\n",
				       tsb->tsa.iostat.dev_act_time);
			sense = tsb->tsa.iostat.sense;
			break;
		case 2: /* ts_ddpc */
4794
			len += sprintf(page + len, PRINTK_HEADER
4795
			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
4796
			for (sl = 0; sl < 2; sl++) {
4797
				len += sprintf(page + len, PRINTK_HEADER
4798 4799 4800
					       " tsb->tsa.ddpc.rcq %2d-%2d: ",
					       (8 * sl), ((8 * sl) + 7));
				rcq = tsb->tsa.ddpc.rcq;
4801 4802
				for (sct = 0; sct < 8; sct++) {
					len += sprintf(page + len, " %02x",
4803
						       rcq[8 * sl + sct]);
4804 4805 4806 4807 4808 4809
				}
				len += sprintf(page + len, "\n");
			}
			sense = tsb->tsa.ddpc.sense;
			break;
		case 3: /* tsa_intrg */
4810
			len += sprintf(page + len, PRINTK_HEADER
4811
				      " tsb->tsa.intrg.: not supported yet\n");
4812 4813 4814 4815 4816
			break;
		}

		if (sense) {
			for (sl = 0; sl < 4; sl++) {
4817
				len += sprintf(page + len, PRINTK_HEADER
4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828
					       " 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 */
4829
				sprintf(page + len, PRINTK_HEADER
4830 4831 4832 4833 4834 4835
					" 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 */
4836
				sprintf(page + len, PRINTK_HEADER
4837 4838 4839 4840 4841
					" 32 Byte: Format: %x "
					"Exception class %x\n",
					sense[6] & 0x0f, sense[22] >> 4);
			}
		} else {
4842
			sprintf(page + len, PRINTK_HEADER
4843 4844 4845
				" SORRY - NO VALID SENSE AVAILABLE\n");
		}
	} else {
4846
		sprintf(page + len, PRINTK_HEADER
4847 4848
			" SORRY - NO TSB DATA AVAILABLE\n");
	}
4849
	printk(KERN_ERR "%s", page);
4850 4851 4852 4853 4854 4855
	free_page((unsigned long) page);
}

static void dasd_eckd_dump_sense(struct dasd_device *device,
				 struct dasd_ccw_req *req, struct irb *irb)
{
4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870
	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;

4871
		dasd_eckd_dump_sense_tcw(device, req, irb);
4872 4873
	} else {
		/*
4874 4875 4876
		 * 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.
4877
		 */
4878 4879 4880 4881
		if (sense && sense[0] & SNS0_CMD_REJECT &&
		    test_bit(DASD_CQR_SUPPRESS_CR, &req->flags))
			return;

4882 4883 4884 4885
		if (sense && sense[1] & SNS1_NO_REC_FOUND &&
		    test_bit(DASD_CQR_SUPPRESS_NRF, &req->flags))
			return;

4886
		dasd_eckd_dump_sense_ccw(device, req, irb);
4887
	}
4888 4889
}

4890
static int dasd_eckd_pm_freeze(struct dasd_device *device)
4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902
{
	/*
	 * 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;
}

4903
static int dasd_eckd_restore_device(struct dasd_device *device)
4904
{
4905
	struct dasd_eckd_private *private = device->private;
4906
	struct dasd_eckd_characteristics temp_rdc_data;
4907
	int rc;
4908
	struct dasd_uid temp_uid;
S
Stefan Haberland 已提交
4909
	unsigned long flags;
4910
	unsigned long cqr_flags = 0;
4911 4912

	/* Read Configuration Data */
4913 4914 4915 4916 4917 4918
	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;
	}
4919

4920 4921 4922 4923
	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);
4924
	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
S
Stefan Haberland 已提交
4925 4926
		dev_err(&device->cdev->dev, "The UID of the DASD has "
			"changed\n");
4927
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4928 4929 4930 4931
	if (rc)
		goto out_err;

	/* register lcu with alias handling, enable PAV if this is a new lcu */
4932 4933
	rc = dasd_alias_make_device_known_to_lcu(device);
	if (rc)
4934
		goto out_err;
4935 4936 4937

	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
	dasd_eckd_validate_server(device, cqr_flags);
4938 4939

	/* RE-Read Configuration Data */
4940 4941 4942 4943 4944 4945
	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;
	}
4946 4947

	/* Read Feature Codes */
4948
	dasd_eckd_read_features(device);
4949 4950

	/* Read Device Characteristics */
4951
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
4952
					 &temp_rdc_data, 64);
4953
	if (rc) {
4954 4955
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
4956
		goto out_err2;
4957
	}
S
Stefan Haberland 已提交
4958
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4959
	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
S
Stefan Haberland 已提交
4960
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4961 4962 4963 4964 4965 4966

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

	return 0;

4967 4968
out_err2:
	dasd_alias_disconnect_device_from_lcu(device);
4969
out_err:
4970
	return -1;
4971 4972
}

4973 4974
static int dasd_eckd_reload_device(struct dasd_device *device)
{
4975
	struct dasd_eckd_private *private = device->private;
4976
	int rc, old_base;
4977 4978 4979
	char print_uid[60];
	struct dasd_uid uid;
	unsigned long flags;
4980

4981 4982 4983 4984 4985 4986
	/*
	 * remove device from alias handling to prevent new requests
	 * from being scheduled on the wrong alias device
	 */
	dasd_alias_remove_device(device);

4987
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4988
	old_base = private->uid.base_unit_addr;
4989 4990
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);

4991 4992 4993 4994 4995
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err;

4996
	rc = dasd_eckd_generate_uid(device);
4997 4998 4999 5000 5001 5002 5003 5004
	if (rc)
		goto out_err;
	/*
	 * update unit address configuration and
	 * add device to alias management
	 */
	dasd_alias_update_add_device(device);

5005 5006 5007 5008 5009 5010 5011
	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);
5012
		else
5013 5014 5015
			snprintf(print_uid, sizeof(print_uid),
				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
				 uid.ssid, uid.base_unit_addr);
5016 5017 5018

		dev_info(&device->cdev->dev,
			 "An Alias device was reassigned to a new base device "
5019
			 "with UID: %s\n", print_uid);
5020 5021 5022 5023 5024 5025 5026
	}
	return 0;

out_err:
	return -1;
}

5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
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)),
				   device);
	if (IS_ERR(cqr)) {
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
				"Could not allocate read message buffer request");
		return PTR_ERR(cqr);
	}

5047 5048
	cqr->lpm = lpum;
retry:
5049 5050 5051 5052 5053
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
	cqr->expires = 10 * HZ;
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5054 5055 5056 5057 5058
	/* 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;
5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092

	/* 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));
5093 5094 5095 5096 5097 5098 5099 5100
	} 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;
5101 5102 5103 5104 5105 5106 5107 5108
	} else
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Reading messages failed with rc=%d\n"
				, rc);
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}

5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122
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;

5123 5124 5125 5126
	/* may not be supported by the storage server */
	if (!(private->features.feature[14] & 0x80))
		return -EOPNOTSUPP;

5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
				   sizeof(struct dasd_psf_prssd_data) + 1,
				   device);
	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;
5174 5175
	/* the command might not be supported, suppress error message */
	__set_bit(DASD_CQR_SUPPRESS_CR, &cqr->flags);
5176
	rc = dasd_sleep_on_interruptible(cqr);
5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234
	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 已提交
5235
	int rc, i;
5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254

	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 已提交
5255
		seq_printf(m, "pgid %*phN\n", 11, entry->pgid);
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272
		/* 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;
}

5273 5274 5275 5276 5277
/*
 * Perform Subsystem Function - CUIR response
 */
static int
dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
5278
			    __u32 message_id, __u8 lpum)
5279 5280
{
	struct dasd_psf_cuir_response *psf_cuir;
5281
	int pos = pathmask_to_pos(lpum);
5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

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

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

	psf_cuir = (struct dasd_psf_cuir_response *)cqr->data;
	psf_cuir->order = PSF_ORDER_CUIR_RESPONSE;
	psf_cuir->cc = response;
5299
	psf_cuir->chpid = device->path[pos].chpid;
5300
	psf_cuir->message_id = message_id;
5301 5302
	psf_cuir->cssid = device->path[pos].cssid;
	psf_cuir->ssid = device->path[pos].ssid;
5303 5304 5305
	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
	ccw->cda = (__u32)(addr_t)psf_cuir;
5306
	ccw->flags = CCW_FLAG_SLI;
5307 5308 5309 5310 5311 5312 5313 5314 5315
	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;
5316
	set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5317 5318 5319 5320 5321 5322 5323

	rc = dasd_sleep_on(cqr);

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

5324 5325 5326 5327 5328 5329 5330 5331
/*
 * 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)
5332
{
5333 5334
	struct dasd_conf_data *conf_data;
	int path, pos;
5335

5336 5337 5338
	if (cuir->record_selector == 0)
		goto out;
	for (path = 0x80, pos = 0; path; path >>= 1, pos++) {
5339
		conf_data = device->path[pos].conf_data;
5340 5341 5342
		if (conf_data->gneq.record_selector ==
		    cuir->record_selector)
			return conf_data;
5343
	}
5344
out:
5345
	return device->path[pathmask_to_pos(lpum)].conf_data;
5346 5347 5348
}

/*
5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384
 * 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;

5385
	for (path = 0; path < 8; path++) {
5386 5387
		/* initialise data per path */
		bitmask = mask;
5388
		conf_data = device->path[path].conf_data;
5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408
		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 */
5409
		tbcpm |= 0x80 >> path;
5410 5411 5412 5413 5414
	}
	return tbcpm;
}

static void dasd_eckd_cuir_notify_user(struct dasd_device *device,
5415
				       unsigned long paths, int action)
5416 5417 5418 5419 5420
{
	int pos;

	while (paths) {
		/* get position of bit in mask */
5421
		pos = 8 - ffs(paths);
5422 5423
		/* get channel path descriptor from this position */
		if (action == CUIR_QUIESCE)
5424 5425 5426
			pr_warn("Service on the storage server caused path %x.%02x to go offline",
				device->path[pos].cssid,
				device->path[pos].chpid);
5427
		else if (action == CUIR_RESUME)
5428 5429 5430 5431
			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);
5432 5433 5434 5435 5436 5437 5438 5439 5440 5441
	}
}

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 */
5442
	if (!(dasd_path_get_opm(device) & tbcpm))
5443
		return 0;
5444
	if (!(dasd_path_get_opm(device) & ~tbcpm)) {
5445 5446 5447 5448 5449
		/* no path would be left if the CUIR action is taken
		   return error */
		return -EINVAL;
	}
	/* remove device from operational path mask */
5450 5451
	dasd_path_remove_opm(device, tbcpm);
	dasd_path_add_cuirpm(device, tbcpm);
5452 5453 5454 5455 5456 5457
	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
5458 5459 5460 5461 5462 5463
 *
 * 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,
5464
				  struct dasd_cuir_message *cuir)
5465
{
5466
	struct dasd_eckd_private *private = device->private;
5467 5468
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_device *dev, *n;
5469 5470 5471
	unsigned long paths = 0;
	unsigned long flags;
	int tbcpm;
5472 5473

	/* active devices */
5474
	list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
5475
				 alias_list) {
5476 5477 5478 5479 5480 5481
		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;
5482 5483
	}
	/* inactive devices */
5484
	list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
5485
				 alias_list) {
5486 5487 5488 5489 5490 5491
		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;
5492 5493 5494 5495 5496 5497
	}
	/* 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) {
5498 5499 5500 5501 5502 5503 5504
			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;
5505 5506 5507
		}
		list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
					 alias_list) {
5508 5509 5510 5511 5512 5513 5514
			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;
5515 5516
		}
	}
5517
	/* notify user about all paths affected by CUIR action */
5518
	dasd_eckd_cuir_notify_user(device, paths, CUIR_QUIESCE);
5519 5520 5521
	return 0;
out_err:
	return tbcpm;
5522 5523 5524
}

static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
5525
				 struct dasd_cuir_message *cuir)
5526
{
5527
	struct dasd_eckd_private *private = device->private;
5528 5529
	struct alias_pav_group *pavgroup, *tempgroup;
	struct dasd_device *dev, *n;
5530 5531
	unsigned long paths = 0;
	int tbcpm;
5532 5533 5534 5535 5536 5537 5538 5539

	/*
	 * 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) {
5540 5541
		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
		paths |= tbcpm;
5542 5543
		if (!(dasd_path_get_opm(dev) & tbcpm)) {
			dasd_path_add_tbvpm(dev, tbcpm);
5544 5545 5546 5547 5548 5549
			dasd_schedule_device_bh(dev);
		}
	}
	list_for_each_entry_safe(dev, n,
				 &private->lcu->inactive_devices,
				 alias_list) {
5550 5551
		tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
		paths |= tbcpm;
5552 5553
		if (!(dasd_path_get_opm(dev) & tbcpm)) {
			dasd_path_add_tbvpm(dev, tbcpm);
5554 5555 5556 5557 5558 5559 5560 5561 5562 5563
			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) {
5564 5565
			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
			paths |= tbcpm;
5566 5567
			if (!(dasd_path_get_opm(dev) & tbcpm)) {
				dasd_path_add_tbvpm(dev, tbcpm);
5568 5569 5570 5571 5572 5573
				dasd_schedule_device_bh(dev);
			}
		}
		list_for_each_entry_safe(dev, n,
					 &pavgroup->aliaslist,
					 alias_list) {
5574 5575
			tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
			paths |= tbcpm;
5576 5577
			if (!(dasd_path_get_opm(dev) & tbcpm)) {
				dasd_path_add_tbvpm(dev, tbcpm);
5578 5579 5580 5581
				dasd_schedule_device_bh(dev);
			}
		}
	}
5582
	/* notify user about all paths affected by CUIR action */
5583
	dasd_eckd_cuir_notify_user(device, paths, CUIR_RESUME);
5584
	return 0;
5585 5586 5587 5588 5589 5590
}

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

5593 5594 5595 5596
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "CUIR request: %016llx %016llx %016llx %08x",
		      ((u64 *)cuir)[0], ((u64 *)cuir)[1], ((u64 *)cuir)[2],
		      ((u32 *)cuir)[3]);
5597 5598 5599

	if (cuir->code == CUIR_QUIESCE) {
		/* quiesce */
5600
		if (dasd_eckd_cuir_quiesce(device, lpum, cuir))
5601 5602 5603
			response = PSF_CUIR_LAST_PATH;
		else
			response = PSF_CUIR_COMPLETED;
5604 5605
	} else if (cuir->code == CUIR_RESUME) {
		/* resume */
5606
		dasd_eckd_cuir_resume(device, lpum, cuir);
5607
		response = PSF_CUIR_COMPLETED;
5608 5609 5610
	} else
		response = PSF_CUIR_NOT_SUPPORTED;

5611
	dasd_eckd_psf_cuir_response(device, response,
5612
				    cuir->message_id, lpum);
5613 5614 5615 5616 5617
	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);
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661
}

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

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

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

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

5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718
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);
}

5719
static struct ccw_driver dasd_eckd_driver = {
5720 5721 5722 5723
	.driver = {
		.name	= "dasd-eckd",
		.owner	= THIS_MODULE,
	},
5724 5725 5726 5727 5728 5729
	.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,
5730
	.path_event  = dasd_generic_path_event,
5731
	.shutdown    = dasd_generic_shutdown,
5732 5733 5734
	.freeze      = dasd_generic_pm_freeze,
	.thaw	     = dasd_generic_restore_device,
	.restore     = dasd_generic_restore_device,
5735
	.uc_handler  = dasd_generic_uc_handler,
5736
	.int_class   = IRQIO_DAS,
5737
};
5738

L
Linus Torvalds 已提交
5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755
/*
 * max_blocks is dependent on the amount of storage that is available
 * in the static io buffer for each device. Currently each device has
 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
 * addition we have one define extent ccw + 16 bytes of data and one
 * locate record ccw + 16 bytes of data. That makes:
 * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
 * We want to fit two into the available memory so that we can immediately
 * start the next request if one finishes off. That makes 249.5 blocks
 * for one request. Give a little safety and the result is 240.
 */
static struct dasd_discipline dasd_eckd_discipline = {
	.owner = THIS_MODULE,
	.name = "ECKD",
	.ebcname = "ECKD",
5756
	.max_blocks = 190,
L
Linus Torvalds 已提交
5757
	.check_device = dasd_eckd_check_characteristics,
5758
	.uncheck_device = dasd_eckd_uncheck_device,
L
Linus Torvalds 已提交
5759
	.do_analysis = dasd_eckd_do_analysis,
5760
	.verify_path = dasd_eckd_verify_path,
5761
	.basic_to_ready = dasd_eckd_basic_to_ready,
5762
	.online_to_ready = dasd_eckd_online_to_ready,
5763
	.basic_to_known = dasd_eckd_basic_to_known,
L
Linus Torvalds 已提交
5764 5765 5766
	.fill_geometry = dasd_eckd_fill_geometry,
	.start_IO = dasd_start_IO,
	.term_IO = dasd_term_IO,
5767
	.handle_terminated_request = dasd_eckd_handle_terminated_request,
L
Linus Torvalds 已提交
5768
	.format_device = dasd_eckd_format_device,
5769
	.check_device_format = dasd_eckd_check_device_format,
L
Linus Torvalds 已提交
5770 5771
	.erp_action = dasd_eckd_erp_action,
	.erp_postaction = dasd_eckd_erp_postaction,
5772
	.check_for_device_change = dasd_eckd_check_for_device_change,
5773 5774
	.build_cp = dasd_eckd_build_alias_cp,
	.free_cp = dasd_eckd_free_alias_cp,
L
Linus Torvalds 已提交
5775
	.dump_sense = dasd_eckd_dump_sense,
S
Stefan Haberland 已提交
5776
	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
L
Linus Torvalds 已提交
5777
	.fill_info = dasd_eckd_fill_info,
5778
	.ioctl = dasd_eckd_ioctl,
5779 5780
	.freeze = dasd_eckd_pm_freeze,
	.restore = dasd_eckd_restore_device,
5781
	.reload = dasd_eckd_reload_device,
5782
	.get_uid = dasd_eckd_get_uid,
5783
	.kick_validate = dasd_eckd_kick_validate_server,
5784
	.check_attention = dasd_eckd_check_attention,
5785 5786
	.host_access_count = dasd_eckd_host_access_count,
	.hosts_print = dasd_hosts_print,
5787 5788 5789 5790
	.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,
L
Linus Torvalds 已提交
5791 5792 5793 5794 5795
};

static int __init
dasd_eckd_init(void)
{
5796 5797
	int ret;

L
Linus Torvalds 已提交
5798
	ASCEBC(dasd_eckd_discipline.ebcname, 4);
5799 5800 5801 5802
	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
				   GFP_KERNEL | GFP_DMA);
	if (!dasd_reserve_req)
		return -ENOMEM;
5803 5804 5805 5806 5807 5808
	path_verification_worker = kmalloc(sizeof(*path_verification_worker),
				   GFP_KERNEL | GFP_DMA);
	if (!path_verification_worker) {
		kfree(dasd_reserve_req);
		return -ENOMEM;
	}
5809 5810 5811 5812 5813 5814
	rawpadpage = (void *)__get_free_page(GFP_KERNEL);
	if (!rawpadpage) {
		kfree(path_verification_worker);
		kfree(dasd_reserve_req);
		return -ENOMEM;
	}
5815 5816 5817
	ret = ccw_driver_register(&dasd_eckd_driver);
	if (!ret)
		wait_for_device_probe();
5818 5819
	else {
		kfree(path_verification_worker);
5820
		kfree(dasd_reserve_req);
5821
		free_page((unsigned long)rawpadpage);
5822
	}
5823
	return ret;
L
Linus Torvalds 已提交
5824 5825 5826 5827 5828 5829
}

static void __exit
dasd_eckd_cleanup(void)
{
	ccw_driver_unregister(&dasd_eckd_driver);
5830
	kfree(path_verification_worker);
5831
	kfree(dasd_reserve_req);
5832
	free_page((unsigned long)rawpadpage);
L
Linus Torvalds 已提交
5833 5834 5835 5836
}

module_init(dasd_eckd_init);
module_exit(dasd_eckd_cleanup);