dasd_eckd.c 100.6 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3
 * File...........: linux/drivers/s390/block/dasd_eckd.c
 * 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 21 22 23 24 25
#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>
#include <linux/init.h>

#include <asm/debug.h>
#include <asm/idals.h>
#include <asm/ebcdic.h>
26
#include <asm/compat.h>
L
Linus Torvalds 已提交
27 28
#include <asm/io.h>
#include <asm/uaccess.h>
29
#include <asm/cio.h>
L
Linus Torvalds 已提交
30
#include <asm/ccwdev.h>
31
#include <asm/itcw.h>
L
Linus Torvalds 已提交
32 33 34

#include "dasd_int.h"
#include "dasd_eckd.h"
35 36
#include "../cio/chsc.h"

L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#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)

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 已提交
64 65 66 67 68 69 70 71 72 73
	{ CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
	{ CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3390, 0), .driver_info = 0x3},
	{ 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 已提交
74 75 76 77 78 79 80
	{ /* end of list */ },
};

MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);

static struct ccw_driver dasd_eckd_driver; /* see below */

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

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

93

L
Linus Torvalds 已提交
94 95 96 97 98 99 100
/* 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;

101
	/* set ECKD specific ccw-device options */
102 103
	ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
				     CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
104
	if (ret) {
105 106 107
		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
				"dasd_eckd_probe: could not set "
				"ccw-device options");
L
Linus Torvalds 已提交
108
		return ret;
109 110 111
	}
	ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
	return ret;
L
Linus Torvalds 已提交
112 113 114 115 116
}

static int
dasd_eckd_set_online(struct ccw_device *cdev)
{
117
	return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

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

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

136
static unsigned int
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
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;
}

169 170 171 172 173 174 175 176
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;
}

177
static int
L
Linus Torvalds 已提交
178 179 180 181 182
check_XRC (struct ccw1         *de_ccw,
           struct DE_eckd_data *data,
           struct dasd_device  *device)
{
        struct dasd_eckd_private *private;
M
Martin Schwidefsky 已提交
183
	int rc;
L
Linus Torvalds 已提交
184 185

        private = (struct dasd_eckd_private *) device->private;
M
Martin Schwidefsky 已提交
186 187
	if (!private->rdc_data.facilities.XRC_supported)
		return 0;
L
Linus Torvalds 已提交
188 189

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

M
Martin Schwidefsky 已提交
193 194 195 196
	rc = get_sync_clock(&data->ep_sys_time);
	/* Ignore return code if sync clock is switched off. */
	if (rc == -ENOSYS || rc == -EACCES)
		rc = 0;
L
Linus Torvalds 已提交
197

198
	de_ccw->count = sizeof(struct DE_eckd_data);
M
Martin Schwidefsky 已提交
199 200 201
	de_ccw->flags |= CCW_FLAG_SLI;
	return rc;
}
L
Linus Torvalds 已提交
202

203
static int
204 205
define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
	      unsigned int totrk, int cmd, struct dasd_device *device)
L
Linus Torvalds 已提交
206 207
{
	struct dasd_eckd_private *private;
208 209
	u32 begcyl, endcyl;
	u16 heads, beghead, endhead;
M
Martin Schwidefsky 已提交
210
	int rc = 0;
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218

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

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

219
	memset(data, 0, sizeof(struct DE_eckd_data));
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	switch (cmd) {
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
	case DASD_ECKD_CCW_READ:
	case DASD_ECKD_CCW_READ_MT:
	case DASD_ECKD_CCW_READ_CKD:
	case DASD_ECKD_CCW_READ_CKD_MT:
	case DASD_ECKD_CCW_READ_KD:
	case DASD_ECKD_CCW_READ_KD_MT:
	case DASD_ECKD_CCW_READ_COUNT:
		data->mask.perm = 0x1;
		data->attributes.operation = private->attrib.operation;
		break;
	case DASD_ECKD_CCW_WRITE:
	case DASD_ECKD_CCW_WRITE_MT:
	case DASD_ECKD_CCW_WRITE_KD:
	case DASD_ECKD_CCW_WRITE_KD_MT:
		data->mask.perm = 0x02;
		data->attributes.operation = private->attrib.operation;
M
Martin Schwidefsky 已提交
239
		rc = check_XRC (ccw, data, device);
L
Linus Torvalds 已提交
240 241 242 243
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->attributes.operation = DASD_BYPASS_CACHE;
M
Martin Schwidefsky 已提交
244
		rc = check_XRC (ccw, data, device);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251
		break;
	case DASD_ECKD_CCW_ERASE:
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
		data->mask.perm = 0x3;
		data->mask.auth = 0x1;
		data->attributes.operation = DASD_BYPASS_CACHE;
M
Martin Schwidefsky 已提交
252
		rc = check_XRC (ccw, data, device);
L
Linus Torvalds 已提交
253 254
		break;
	default:
S
Stefan Haberland 已提交
255 256
		dev_err(&device->cdev->dev,
			"0x%x is not a known command\n", cmd);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267
		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 */

268 269 270 271 272
	heads = private->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;
L
Linus Torvalds 已提交
273 274 275 276

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

278 279
		if (endcyl + private->attrib.nr_cyl < private->real_cyl)
			endcyl += private->attrib.nr_cyl;
L
Linus Torvalds 已提交
280
		else
281
			endcyl = (private->real_cyl - 1);
L
Linus Torvalds 已提交
282 283
	}

284 285
	set_ch_t(&data->beg_ext, begcyl, beghead);
	set_ch_t(&data->end_ext, endcyl, endhead);
M
Martin Schwidefsky 已提交
286
	return rc;
L
Linus Torvalds 已提交
287 288
}

289 290 291 292 293 294 295 296 297 298 299
static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
			       struct dasd_device  *device)
{
	struct dasd_eckd_private *private;
	int rc;

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

	/* switch on System Time Stamp - needed for XRC Support */
300 301
	pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid'   */
	pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
302 303
	pfxdata->validity.time_stamp = 1;	    /* 'Time Stamp Valid'   */

304
	rc = get_sync_clock(&pfxdata->define_extent.ep_sys_time);
305 306 307 308 309 310
	/* Ignore return code if sync clock is switched off. */
	if (rc == -ENOSYS || rc == -EACCES)
		rc = 0;
	return rc;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
			  unsigned int rec_on_trk, int count, int cmd,
			  struct dasd_device *device, unsigned int reclen,
			  unsigned int tlf)
{
	struct dasd_eckd_private *private;
	int sector;
	int dn, d;

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

	memset(data, 0, sizeof(*data));
	sector = 0;
	if (rec_on_trk) {
		switch (private->rdc_data.dev_type) {
		case 0x3390:
			dn = ceil_quot(reclen + 6, 232);
			d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
			sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
			break;
		case 0x3380:
			d = 7 + ceil_quot(reclen + 12, 32);
			sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
			break;
		}
	}
	data->sector = sector;
	/* note: meaning of count depends on the operation
	 *	 for record based I/O it's the number of records, but for
	 *	 track based I/O it's the number of tracks
	 */
	data->count = count;
	switch (cmd) {
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x03;
		break;
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x16;
		break;
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
		data->operation.orientation = 0x1;
		data->operation.operation = 0x03;
		data->count++;
		break;
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
		data->operation.orientation = 0x3;
		data->operation.operation = 0x16;
		data->count++;
		break;
	case DASD_ECKD_CCW_WRITE:
	case DASD_ECKD_CCW_WRITE_MT:
	case DASD_ECKD_CCW_WRITE_KD:
	case DASD_ECKD_CCW_WRITE_KD_MT:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;
		data->operation.operation = 0x01;
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
		data->auxiliary.length_valid = 0x1;
		data->length = reclen;
		data->operation.operation = 0x03;
		break;
	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;
	case DASD_ECKD_CCW_READ_TRACK_DATA:
		data->auxiliary.length_valid = 0x1;
		data->length = tlf;
		data->operation.operation = 0x0C;
		break;
	case DASD_ECKD_CCW_ERASE:
		data->length = reclen;
		data->auxiliary.length_valid = 0x1;
		data->operation.operation = 0x0b;
		break;
	default:
		DBF_DEV_EVENT(DBF_ERR, device,
			    "fill LRE unknown opcode 0x%x", cmd);
		BUG();
	}
	set_ch_t(&data->seek_addr,
		 trk / private->rdc_data.trk_per_cyl,
		 trk % private->rdc_data.trk_per_cyl);
	data->search_arg.cyl = data->seek_addr.cyl;
	data->search_arg.head = data->seek_addr.head;
	data->search_arg.record = rec_on_trk;
}

static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
		      unsigned int trk, unsigned int totrk, int cmd,
		      struct dasd_device *basedev, struct dasd_device *startdev,
		      unsigned char format, unsigned int rec_on_trk, int count,
		      unsigned int blksize, unsigned int tlf)
427 428
{
	struct dasd_eckd_private *basepriv, *startpriv;
429 430
	struct DE_eckd_data *dedata;
	struct LRE_eckd_data *lredata;
431 432
	u32 begcyl, endcyl;
	u16 heads, beghead, endhead;
433 434 435 436
	int rc = 0;

	basepriv = (struct dasd_eckd_private *) basedev->private;
	startpriv = (struct dasd_eckd_private *) startdev->private;
437 438
	dedata = &pfxdata->define_extent;
	lredata = &pfxdata->locate_record;
439 440 441 442 443 444 445 446

	ccw->cmd_code = DASD_ECKD_CCW_PFX;
	ccw->flags = 0;
	ccw->count = sizeof(*pfxdata);
	ccw->cda = (__u32) __pa(pfxdata);

	memset(pfxdata, 0, sizeof(*pfxdata));
	/* prefix data */
447 448 449 450 451 452 453
	if (format > 1) {
		DBF_DEV_EVENT(DBF_ERR, basedev,
			      "PFX LRE unknown format 0x%x", format);
		BUG();
		return -EINVAL;
	}
	pfxdata->format = format;
454 455
	pfxdata->base_address = basepriv->ned->unit_addr;
	pfxdata->base_lss = basepriv->ned->ID;
456
	pfxdata->validity.define_extent = 1;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

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

	/* define extend data (mostly)*/
	switch (cmd) {
	case DASD_ECKD_CCW_READ_HOME_ADDRESS:
	case DASD_ECKD_CCW_READ_RECORD_ZERO:
	case DASD_ECKD_CCW_READ:
	case DASD_ECKD_CCW_READ_MT:
	case DASD_ECKD_CCW_READ_CKD:
	case DASD_ECKD_CCW_READ_CKD_MT:
	case DASD_ECKD_CCW_READ_KD:
	case DASD_ECKD_CCW_READ_KD_MT:
	case DASD_ECKD_CCW_READ_COUNT:
476 477 478 479 480 481 482
		dedata->mask.perm = 0x1;
		dedata->attributes.operation = basepriv->attrib.operation;
		break;
	case DASD_ECKD_CCW_READ_TRACK_DATA:
		dedata->mask.perm = 0x1;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = 0;
483 484 485 486 487
		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:
488 489
		dedata->mask.perm = 0x02;
		dedata->attributes.operation = basepriv->attrib.operation;
490 491 492 493
		rc = check_XRC_on_prefix(pfxdata, basedev);
		break;
	case DASD_ECKD_CCW_WRITE_CKD:
	case DASD_ECKD_CCW_WRITE_CKD_MT:
494
		dedata->attributes.operation = DASD_BYPASS_CACHE;
495 496 497 498 499
		rc = check_XRC_on_prefix(pfxdata, basedev);
		break;
	case DASD_ECKD_CCW_ERASE:
	case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
	case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
500 501 502
		dedata->mask.perm = 0x3;
		dedata->mask.auth = 0x1;
		dedata->attributes.operation = DASD_BYPASS_CACHE;
503 504
		rc = check_XRC_on_prefix(pfxdata, basedev);
		break;
505 506 507 508 509
	case DASD_ECKD_CCW_WRITE_TRACK_DATA:
		dedata->mask.perm = 0x02;
		dedata->attributes.operation = basepriv->attrib.operation;
		dedata->blk_size = blksize;
		rc = check_XRC_on_prefix(pfxdata, basedev);
510
		break;
511 512 513 514 515
	default:
		DBF_DEV_EVENT(DBF_ERR, basedev,
			    "PFX LRE unknown opcode 0x%x", cmd);
		BUG();
		return -EINVAL;
516 517
	}

518
	dedata->attributes.mode = 0x3;	/* ECKD */
519 520 521 522 523

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

526 527 528 529 530
	heads = basepriv->rdc_data.trk_per_cyl;
	begcyl = trk / heads;
	beghead = trk % heads;
	endcyl = totrk / heads;
	endhead = totrk % heads;
531 532

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

536 537
		if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
			endcyl += basepriv->attrib.nr_cyl;
538
		else
539
			endcyl = (basepriv->real_cyl - 1);
540 541
	}

542 543 544 545 546 547 548 549
	set_ch_t(&dedata->beg_ext, begcyl, beghead);
	set_ch_t(&dedata->end_ext, endcyl, endhead);

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

550 551 552
	return rc;
}

553 554 555 556 557 558 559 560
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);
}

561
static void
562 563
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 已提交
564 565 566 567 568
	      struct dasd_device * device, int reclen)
{
	struct dasd_eckd_private *private;
	int sector;
	int dn, d;
569

L
Linus Torvalds 已提交
570 571 572 573 574 575 576 577 578 579 580
	private = (struct dasd_eckd_private *) device->private;

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

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

581
	memset(data, 0, sizeof(struct LO_eckd_data));
L
Linus Torvalds 已提交
582 583 584 585 586 587 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
	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 已提交
654 655
		DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
			      "opcode 0x%x", cmd);
L
Linus Torvalds 已提交
656
	}
657 658 659 660 661
	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 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	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;
}

700 701 702
/*
 * Generate device unique id that specifies the physical device.
 */
703
static int dasd_eckd_generate_uid(struct dasd_device *device)
704 705
{
	struct dasd_eckd_private *private;
706
	struct dasd_uid *uid;
707
	int count;
708
	unsigned long flags;
709 710 711 712

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

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
{
	struct dasd_eckd_private *private;
	unsigned long flags;

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

H
Heiko Carstens 已提交
758 759 760
static struct dasd_ccw_req *dasd_eckd_build_rcd_lpm(struct dasd_device *device,
						    void *rcd_buffer,
						    struct ciw *ciw, __u8 lpm)
761 762 763 764
{
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;

765 766
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */, ciw->count,
				   device);
767 768

	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
769 770
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "Could not allocate RCD request");
771 772 773 774 775 776 777 778
		return cqr;
	}

	ccw = cqr->cpaddr;
	ccw->cmd_code = ciw->cmd;
	ccw->cda = (__u32)(addr_t)rcd_buffer;
	ccw->count = ciw->count;

779 780 781
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
782 783
	cqr->expires = 10*HZ;
	cqr->lpm = lpm;
784
	cqr->retries = 256;
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

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;

	/*
	 * scan for RCD command in extended SenseID data
	 */
	ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
	if (!ciw || ciw->cmd == 0) {
		ret = -EOPNOTSUPP;
		goto out_error;
	}
	rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
	if (!rcd_buf) {
		ret = -ENOMEM;
		goto out_error;
	}
812 813 814 815 816 817 818 819 820

	/*
	 * buffer has to start with EBCDIC "V1.0" to show
	 * support for virtual device SNEQ
	 */
	rcd_buf[0] = 0xE5;
	rcd_buf[1] = 0xF1;
	rcd_buf[2] = 0x4B;
	rcd_buf[3] = 0xF0;
821 822 823 824 825 826 827 828 829
	cqr = dasd_eckd_build_rcd_lpm(device, rcd_buf, ciw, lpm);
	if (IS_ERR(cqr)) {
		ret =  PTR_ERR(cqr);
		goto out_error;
	}
	ret = dasd_sleep_on(cqr);
	/*
	 * on success we update the user input parms
	 */
830
	dasd_sfree_request(cqr, cqr->memdev);
831 832 833 834 835 836 837 838 839 840 841 842 843
	if (ret)
		goto out_error;

	*rcd_buffer_size = ciw->count;
	*rcd_buffer = rcd_buf;
	return 0;
out_error:
	kfree(rcd_buf);
	*rcd_buffer = NULL;
	*rcd_buffer_size = 0;
	return ret;
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
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;
}

static int dasd_eckd_read_conf(struct dasd_device *device)
L
Linus Torvalds 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
{
	void *conf_data;
	int conf_len, conf_data_saved;
	int rc;
	__u8 lpm;
	struct dasd_eckd_private *private;
	struct dasd_eckd_path *path_data;

	private = (struct dasd_eckd_private *) device->private;
	path_data = (struct dasd_eckd_path *) &private->path_data;
	path_data->opm = ccw_device_get_path_mask(device->cdev);
	lpm = 0x80;
	conf_data_saved = 0;
	/* get configuration data per operational path */
	for (lpm = 0x80; lpm; lpm>>= 1) {
		if (lpm & path_data->opm){
916 917
			rc = dasd_eckd_read_conf_lpm(device, &conf_data,
						     &conf_len, lpm);
L
Linus Torvalds 已提交
918
			if (rc && rc != -EOPNOTSUPP) {	/* -EOPNOTSUPP is ok */
919
				DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
S
Stefan Haberland 已提交
920
					  "Read configuration data returned "
921
					  "error %d", rc);
L
Linus Torvalds 已提交
922 923 924
				return rc;
			}
			if (conf_data == NULL) {
925 926 927
				DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
						"No configuration data "
						"retrieved");
G
Gabriel Craciunescu 已提交
928
				continue;	/* no error */
L
Linus Torvalds 已提交
929 930
			}
			/* save first valid configuration data */
931 932 933 934 935 936 937 938 939 940
			if (!conf_data_saved) {
				kfree(private->conf_data);
				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 已提交
941 942
				conf_data_saved++;
			}
943
			switch (dasd_eckd_path_access(conf_data, conf_len)) {
L
Linus Torvalds 已提交
944 945 946 947 948 949 950
			case 0x02:
				path_data->npm |= lpm;
				break;
			case 0x03:
				path_data->ppm |= lpm;
				break;
			}
951 952
			if (conf_data != private->conf_data)
				kfree(conf_data);
L
Linus Torvalds 已提交
953 954 955 956 957
		}
	}
	return 0;
}

958 959 960 961 962 963 964 965 966 967
static int dasd_eckd_read_features(struct dasd_device *device)
{
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_features *features;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;
	struct dasd_eckd_private *private;

	private = (struct dasd_eckd_private *) device->private;
968
	memset(&private->features, 0, sizeof(struct dasd_rssd_features));
969
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */	+ 1 /* RSSD */,
970 971 972 973
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_features)),
				   device);
	if (IS_ERR(cqr)) {
974 975
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
				"allocate initialization request");
976 977 978 979 980
		return PTR_ERR(cqr);
	}
	cqr->startdev = device;
	cqr->memdev = device;
	cqr->block = NULL;
981
	cqr->retries = 256;
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	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;

	cqr->buildclk = get_clock();
	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));
1014 1015 1016
	} else
		dev_warn(&device->cdev->dev, "Reading device feature codes"
			 " failed with rc=%d\n", rc);
1017 1018 1019 1020 1021
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
}


1022 1023 1024
/*
 * Build CP for Perform Subsystem Function - SSC.
 */
1025 1026
static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
						    int enable_pav)
1027
{
1028 1029 1030
	struct dasd_ccw_req *cqr;
	struct dasd_psf_ssc_data *psf_ssc_data;
	struct ccw1 *ccw;
1031

1032
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1033 1034 1035
				  sizeof(struct dasd_psf_ssc_data),
				  device);

1036
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
1037
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1038
			   "Could not allocate PSF-SSC request");
1039 1040 1041 1042
		return cqr;
	}
	psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
	psf_ssc_data->order = PSF_ORDER_SSC;
1043
	psf_ssc_data->suborder = 0xc0;
1044
	if (enable_pav) {
1045
		psf_ssc_data->suborder |= 0x08;
1046 1047
		psf_ssc_data->reserved[0] = 0x88;
	}
1048 1049 1050 1051 1052 1053 1054 1055
	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;
1056
	cqr->retries = 256;
1057 1058 1059 1060
	cqr->expires = 10*HZ;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	return cqr;
1061 1062 1063 1064 1065 1066 1067 1068
}

/*
 * Perform Subsystem Function.
 * It is necessary to trigger CIO for channel revalidation since this
 * call might change behaviour of DASD devices.
 */
static int
1069
dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav)
1070
{
1071 1072 1073
	struct dasd_ccw_req *cqr;
	int rc;

1074
	cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1075 1076 1077 1078 1079 1080 1081 1082 1083
	if (IS_ERR(cqr))
		return PTR_ERR(cqr);

	rc = dasd_sleep_on(cqr);
	if (!rc)
		/* trigger CIO to reprobe devices */
		css_schedule_reprobe();
	dasd_sfree_request(cqr, cqr->memdev);
	return rc;
1084 1085 1086 1087 1088
}

/*
 * Valide storage server of current device.
 */
1089
static void dasd_eckd_validate_server(struct dasd_device *device)
1090 1091
{
	int rc;
1092
	struct dasd_eckd_private *private;
1093
	int enable_pav;
1094 1095

	if (dasd_nopav || MACHINE_IS_VM)
1096 1097 1098 1099
		enable_pav = 0;
	else
		enable_pav = 1;
	rc = dasd_eckd_psf_ssc(device, enable_pav);
1100

H
Horst Hummel 已提交
1101 1102
	/* may be requested feature is not available on server,
	 * therefore just report error and go ahead */
1103
	private = (struct dasd_eckd_private *) device->private;
1104 1105
	DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
			"returned rc=%d", private->uid.ssid, rc);
1106 1107
}

1108 1109 1110 1111
/*
 * Check device characteristics.
 * If the device is accessible using ECKD discipline, the device is enabled.
 */
L
Linus Torvalds 已提交
1112 1113 1114 1115
static int
dasd_eckd_check_characteristics(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
1116
	struct dasd_block *block;
1117
	struct dasd_uid temp_uid;
1118
	int is_known, rc, i;
1119
	int readonly;
1120
	unsigned long value;
L
Linus Torvalds 已提交
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130
	if (!ccw_device_is_pathgroup(device->cdev)) {
		dev_warn(&device->cdev->dev,
			 "A channel path group could not be established\n");
		return -EIO;
	}
	if (!ccw_device_is_multipath(device->cdev)) {
		dev_info(&device->cdev->dev,
			 "The DASD is not operating in multipath mode\n");
	}
L
Linus Torvalds 已提交
1131
	private = (struct dasd_eckd_private *) device->private;
1132 1133 1134
	if (!private) {
		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
		if (!private) {
S
Stefan Haberland 已提交
1135 1136 1137
			dev_warn(&device->cdev->dev,
				 "Allocating memory for private DASD data "
				 "failed\n");
L
Linus Torvalds 已提交
1138 1139 1140
			return -ENOMEM;
		}
		device->private = (void *) private;
1141 1142
	} else {
		memset(private, 0, sizeof(*private));
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148 1149
	}
	/* 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;

1150 1151 1152
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
1153
		goto out_err1;
1154

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	/* set default timeout */
	device->default_expires = DASD_EXPIRES;
	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;
	}

1167 1168
	/* Generate device unique id */
	rc = dasd_eckd_generate_uid(device);
1169
	if (rc)
1170 1171
		goto out_err1;

1172 1173
	dasd_eckd_get_uid(device, &temp_uid);
	if (temp_uid.type == UA_BASE_DEVICE) {
1174 1175
		block = dasd_alloc_block();
		if (IS_ERR(block)) {
1176 1177 1178
			DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
					"could not allocate dasd "
					"block structure");
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
			rc = PTR_ERR(block);
			goto out_err1;
		}
		device->block = block;
		block->base = device;
	}

	/* register lcu with alias handling, enable PAV if this is a new lcu */
	is_known = dasd_alias_make_device_known_to_lcu(device);
	if (is_known < 0) {
		rc = is_known;
		goto out_err2;
	}
1192
	/*
1193
	 * dasd_eckd_validate_server is done on the first device that
1194 1195 1196
	 * is found for an LCU. All later other devices have to wait
	 * for it, so they will read the correct feature codes.
	 */
1197
	if (!is_known) {
1198 1199 1200 1201 1202 1203 1204 1205 1206
		dasd_eckd_validate_server(device);
		dasd_alias_lcu_setup_complete(device);
	} else
		dasd_alias_wait_for_lcu_setup(device);

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

	/* Read Feature Codes */
1209
	dasd_eckd_read_features(device);
1210

L
Linus Torvalds 已提交
1211
	/* Read Device Characteristics */
1212 1213
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
					 &private->rdc_data, 64);
1214
	if (rc) {
1215 1216
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
1217 1218
		goto out_err3;
	}
1219
	/* find the valid cylinder size */
1220 1221 1222 1223 1224 1225
	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;

1226 1227 1228 1229
	readonly = dasd_device_is_ro(device);
	if (readonly)
		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);

S
Stefan Haberland 已提交
1230
	dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1231
		 "with %d cylinders, %d heads, %d sectors%s\n",
S
Stefan Haberland 已提交
1232 1233 1234 1235
		 private->rdc_data.dev_type,
		 private->rdc_data.dev_model,
		 private->rdc_data.cu_type,
		 private->rdc_data.cu_model.model,
1236
		 private->real_cyl,
S
Stefan Haberland 已提交
1237
		 private->rdc_data.trk_per_cyl,
1238 1239
		 private->rdc_data.sec_per_trk,
		 readonly ? ", read-only device" : "");
1240 1241 1242 1243 1244 1245 1246 1247
	return 0;

out_err3:
	dasd_alias_disconnect_device_from_lcu(device);
out_err2:
	dasd_free_block(device->block);
	device->block = NULL;
out_err1:
1248
	kfree(private->conf_data);
1249 1250
	kfree(device->private);
	device->private = NULL;
1251
	return rc;
L
Linus Torvalds 已提交
1252 1253
}

1254 1255
static void dasd_eckd_uncheck_device(struct dasd_device *device)
{
1256 1257 1258
	struct dasd_eckd_private *private;

	private = (struct dasd_eckd_private *) device->private;
1259
	dasd_alias_disconnect_device_from_lcu(device);
1260 1261 1262 1263 1264 1265 1266
	private->ned = NULL;
	private->sneq = NULL;
	private->vdsneq = NULL;
	private->gneq = NULL;
	private->conf_len = 0;
	kfree(private->conf_data);
	private->conf_data = NULL;
1267 1268
}

L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
static struct dasd_ccw_req *
dasd_eckd_analysis_ccw(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
	struct eckd_count *count_data;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int cplength, datasize;
	int i;

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

	cplength = 8;
	datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
1284
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
L
Linus Torvalds 已提交
1285 1286 1287 1288 1289 1290
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
	/* Define extent for the first 3 tracks. */
	define_extent(ccw++, cqr->data, 0, 2,
		      DASD_ECKD_CCW_READ_COUNT, device);
1291
	LO_data = cqr->data + sizeof(struct DE_eckd_data);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	/* 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;

1319 1320 1321
	cqr->block = NULL;
	cqr->startdev = device;
	cqr->memdev = device;
1322
	cqr->retries = 255;
L
Linus Torvalds 已提交
1323 1324 1325 1326 1327
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
/* 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 已提交
1345 1346 1347 1348 1349 1350 1351
/*
 * 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).
 */
1352 1353
static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
					void *data)
L
Linus Torvalds 已提交
1354 1355 1356 1357
{
	struct dasd_eckd_private *private;
	struct dasd_device *device;

1358
	device = init_cqr->startdev;
L
Linus Torvalds 已提交
1359
	private = (struct dasd_eckd_private *) device->private;
1360
	private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
L
Linus Torvalds 已提交
1361 1362 1363 1364
	dasd_sfree_request(init_cqr, device);
	dasd_kick_device(device);
}

1365
static int dasd_eckd_start_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1366 1367 1368 1369
{
	struct dasd_eckd_private *private;
	struct dasd_ccw_req *init_cqr;

1370 1371
	private = (struct dasd_eckd_private *) block->base->private;
	init_cqr = dasd_eckd_analysis_ccw(block->base);
L
Linus Torvalds 已提交
1372 1373 1374 1375 1376
	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;
1377 1378 1379 1380 1381
	/* 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 已提交
1382 1383 1384 1385
	dasd_add_request_head(init_cqr);
	return -EAGAIN;
}

1386
static int dasd_eckd_end_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1387
{
1388
	struct dasd_device *device;
L
Linus Torvalds 已提交
1389 1390 1391 1392
	struct dasd_eckd_private *private;
	struct eckd_count *count_area;
	unsigned int sb, blk_per_trk;
	int status, i;
1393
	struct dasd_ccw_req *init_cqr;
L
Linus Torvalds 已提交
1394

1395
	device = block->base;
L
Linus Torvalds 已提交
1396 1397 1398
	private = (struct dasd_eckd_private *) device->private;
	status = private->init_cqr_status;
	private->init_cqr_status = -1;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	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);
	}

	if (status == INIT_CQR_UNFORMATTED) {
		dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
L
Linus Torvalds 已提交
1409
		return -EMEDIUMTYPE;
1410 1411 1412 1413 1414
	} 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 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
	}

	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 ||
		    private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4) {
			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 !=
			     private->count_area[0].dl))
				break;
		}
		if (i == 5)
			count_area = &private->count_area[0];
	} else {
		if (private->count_area[3].record == 1)
S
Stefan Haberland 已提交
1441 1442
			dev_warn(&device->cdev->dev,
				 "Track 0 has no records following the VTOC\n");
L
Linus Torvalds 已提交
1443 1444 1445 1446
	}
	if (count_area != NULL && count_area->kl == 0) {
		/* we found notthing violating our disk layout */
		if (dasd_check_blocksize(count_area->dl) == 0)
1447
			block->bp_block = count_area->dl;
L
Linus Torvalds 已提交
1448
	}
1449
	if (block->bp_block == 0) {
S
Stefan Haberland 已提交
1450 1451
		dev_warn(&device->cdev->dev,
			 "The disk layout of the DASD is not supported\n");
L
Linus Torvalds 已提交
1452 1453
		return -EMEDIUMTYPE;
	}
1454 1455 1456
	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 已提交
1457

1458
	blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
1459
	block->blocks = (private->real_cyl *
L
Linus Torvalds 已提交
1460 1461 1462
			  private->rdc_data.trk_per_cyl *
			  blk_per_trk);

S
Stefan Haberland 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471
	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 已提交
1472 1473 1474 1475

	return 0;
}

1476
static int dasd_eckd_do_analysis(struct dasd_block *block)
L
Linus Torvalds 已提交
1477 1478 1479
{
	struct dasd_eckd_private *private;

1480
	private = (struct dasd_eckd_private *) block->base->private;
L
Linus Torvalds 已提交
1481
	if (private->init_cqr_status < 0)
1482
		return dasd_eckd_start_analysis(block);
L
Linus Torvalds 已提交
1483
	else
1484
		return dasd_eckd_end_analysis(block);
L
Linus Torvalds 已提交
1485 1486
}

1487 1488 1489 1490 1491 1492 1493
static int dasd_eckd_ready_to_online(struct dasd_device *device)
{
	return dasd_alias_add_device(device);
};

static int dasd_eckd_online_to_ready(struct dasd_device *device)
{
1494
	cancel_work_sync(&device->reload_device);
1495 1496 1497
	return dasd_alias_remove_device(device);
};

L
Linus Torvalds 已提交
1498
static int
1499
dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
L
Linus Torvalds 已提交
1500 1501 1502
{
	struct dasd_eckd_private *private;

1503 1504
	private = (struct dasd_eckd_private *) block->base->private;
	if (dasd_check_blocksize(block->bp_block) == 0) {
L
Linus Torvalds 已提交
1505
		geo->sectors = recs_per_track(&private->rdc_data,
1506
					      0, block->bp_block);
L
Linus Torvalds 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	}
	geo->cylinders = private->rdc_data.no_cyl;
	geo->heads = private->rdc_data.trk_per_cyl;
	return 0;
}

static struct dasd_ccw_req *
dasd_eckd_format_device(struct dasd_device * device,
			struct format_data_t * fdata)
{
	struct dasd_eckd_private *private;
	struct dasd_ccw_req *fcp;
	struct eckd_count *ect;
	struct ccw1 *ccw;
	void *data;
1522 1523
	int rpt;
	struct ch_t address;
L
Linus Torvalds 已提交
1524 1525
	int cplength, datasize;
	int i;
1526 1527
	int intensity = 0;
	int r0_perm;
L
Linus Torvalds 已提交
1528 1529 1530

	private = (struct dasd_eckd_private *) device->private;
	rpt = recs_per_track(&private->rdc_data, 0, fdata->blksize);
1531 1532 1533
	set_ch_t(&address,
		 fdata->start_unit / private->rdc_data.trk_per_cyl,
		 fdata->start_unit % private->rdc_data.trk_per_cyl);
L
Linus Torvalds 已提交
1534 1535 1536

	/* Sanity checks. */
	if (fdata->start_unit >=
1537
	    (private->real_cyl * private->rdc_data.trk_per_cyl)) {
S
Stefan Haberland 已提交
1538 1539
		dev_warn(&device->cdev->dev, "Start track number %d used in "
			 "formatting is too big\n", fdata->start_unit);
L
Linus Torvalds 已提交
1540 1541 1542
		return ERR_PTR(-EINVAL);
	}
	if (fdata->start_unit > fdata->stop_unit) {
S
Stefan Haberland 已提交
1543 1544
		dev_warn(&device->cdev->dev, "Start track %d used in "
			 "formatting exceeds end track\n", fdata->start_unit);
L
Linus Torvalds 已提交
1545 1546 1547
		return ERR_PTR(-EINVAL);
	}
	if (dasd_check_blocksize(fdata->blksize) != 0) {
S
Stefan Haberland 已提交
1548 1549 1550
		dev_warn(&device->cdev->dev,
			 "The DASD cannot be formatted with block size %d\n",
			 fdata->blksize);
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559
		return ERR_PTR(-EINVAL);
	}

	/*
	 * 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)
1560
	 *   Bit 4: do not allow storage subsystem to modify record zero
L
Linus Torvalds 已提交
1561 1562
	 * Only some bit combinations do make sense.
	 */
1563 1564 1565 1566 1567 1568 1569 1570
	if (fdata->intensity & 0x10) {
		r0_perm = 0;
		intensity = fdata->intensity & ~0x10;
	} else {
		r0_perm = 1;
		intensity = fdata->intensity;
	}
	switch (intensity) {
L
Linus Torvalds 已提交
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	case 0x00:	/* Normal format */
	case 0x08:	/* Normal format, use cdl. */
		cplength = 2 + rpt;
		datasize = sizeof(struct DE_eckd_data) +
			sizeof(struct LO_eckd_data) +
			rpt * sizeof(struct eckd_count);
		break;
	case 0x01:	/* Write record zero and format track. */
	case 0x09:	/* Write record zero and format track, use cdl. */
		cplength = 3 + rpt;
		datasize = sizeof(struct DE_eckd_data) +
			sizeof(struct LO_eckd_data) +
			sizeof(struct eckd_count) +
			rpt * sizeof(struct eckd_count);
		break;
	case 0x04:	/* Invalidate track. */
	case 0x0c:	/* Invalidate track, use cdl. */
		cplength = 3;
		datasize = sizeof(struct DE_eckd_data) +
			sizeof(struct LO_eckd_data) +
			sizeof(struct eckd_count);
		break;
	default:
S
Stefan Haberland 已提交
1594 1595
		dev_warn(&device->cdev->dev, "An I/O control call used "
			 "incorrect flags 0x%x\n", fdata->intensity);
L
Linus Torvalds 已提交
1596 1597 1598
		return ERR_PTR(-EINVAL);
	}
	/* Allocate the format ccw request. */
1599
	fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
L
Linus Torvalds 已提交
1600 1601 1602 1603 1604 1605
	if (IS_ERR(fcp))
		return fcp;

	data = fcp->data;
	ccw = fcp->cpaddr;

1606
	switch (intensity & ~0x08) {
L
Linus Torvalds 已提交
1607 1608 1609 1610
	case 0x00: /* Normal format. */
		define_extent(ccw++, (struct DE_eckd_data *) data,
			      fdata->start_unit, fdata->start_unit,
			      DASD_ECKD_CCW_WRITE_CKD, device);
1611 1612 1613
		/* grant subsystem permission to format R0 */
		if (r0_perm)
			((struct DE_eckd_data *)data)->ga_extended |= 0x04;
L
Linus Torvalds 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
		data += sizeof(struct DE_eckd_data);
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
			      fdata->start_unit, 0, rpt,
			      DASD_ECKD_CCW_WRITE_CKD, device,
			      fdata->blksize);
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x01: /* Write record zero + format track. */
		define_extent(ccw++, (struct DE_eckd_data *) data,
			      fdata->start_unit, fdata->start_unit,
			      DASD_ECKD_CCW_WRITE_RECORD_ZERO,
			      device);
		data += sizeof(struct DE_eckd_data);
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
			      fdata->start_unit, 0, rpt + 1,
			      DASD_ECKD_CCW_WRITE_RECORD_ZERO, device,
1632
			      device->block->bp_block);
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
		data += sizeof(struct LO_eckd_data);
		break;
	case 0x04: /* Invalidate track. */
		define_extent(ccw++, (struct DE_eckd_data *) data,
			      fdata->start_unit, fdata->start_unit,
			      DASD_ECKD_CCW_WRITE_CKD, device);
		data += sizeof(struct DE_eckd_data);
		ccw[-1].flags |= CCW_FLAG_CC;
		locate_record(ccw++, (struct LO_eckd_data *) data,
			      fdata->start_unit, 0, 1,
			      DASD_ECKD_CCW_WRITE_CKD, device, 8);
		data += sizeof(struct LO_eckd_data);
		break;
	}
1647
	if (intensity & 0x01) {	/* write record zero */
L
Linus Torvalds 已提交
1648 1649
		ect = (struct eckd_count *) data;
		data += sizeof(struct eckd_count);
1650 1651
		ect->cyl = address.cyl;
		ect->head = address.head;
L
Linus Torvalds 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
		ect->record = 0;
		ect->kl = 0;
		ect->dl = 8;
		ccw[-1].flags |= CCW_FLAG_CC;
		ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
		ccw->flags = CCW_FLAG_SLI;
		ccw->count = 8;
		ccw->cda = (__u32)(addr_t) ect;
		ccw++;
	}
1662
	if ((intensity & ~0x08) & 0x04) {	/* erase track */
L
Linus Torvalds 已提交
1663 1664
		ect = (struct eckd_count *) data;
		data += sizeof(struct eckd_count);
1665 1666
		ect->cyl = address.cyl;
		ect->head = address.head;
L
Linus Torvalds 已提交
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
		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);
1679 1680
			ect->cyl = address.cyl;
			ect->head = address.head;
L
Linus Torvalds 已提交
1681 1682 1683 1684
			ect->record = i + 1;
			ect->kl = 0;
			ect->dl = fdata->blksize;
			/* Check for special tracks 0-1 when formatting CDL */
1685
			if ((intensity & 0x08) &&
L
Linus Torvalds 已提交
1686 1687 1688 1689
			    fdata->start_unit == 0) {
				if (i < 3) {
					ect->kl = 4;
					ect->dl = sizes_trk0[i] - 4;
1690
				}
L
Linus Torvalds 已提交
1691
			}
1692
			if ((intensity & 0x08) &&
L
Linus Torvalds 已提交
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
			    fdata->start_unit == 1) {
				ect->kl = 44;
				ect->dl = LABEL_SIZE - 44;
			}
			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;
			ccw++;
		}
	}
1705 1706
	fcp->startdev = device;
	fcp->memdev = device;
1707
	fcp->retries = 256;
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712
	fcp->buildclk = get_clock();
	fcp->status = DASD_CQR_FILLED;
	return fcp;
}

1713
static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
L
Linus Torvalds 已提交
1714
{
1715 1716 1717 1718
	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;
L
Linus Torvalds 已提交
1719
	}
1720
};
L
Linus Torvalds 已提交
1721 1722 1723 1724

static dasd_erp_fn_t
dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
{
1725
	struct dasd_device *device = (struct dasd_device *) cqr->startdev;
L
Linus Torvalds 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
	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;
}

1747 1748 1749 1750 1751

static void dasd_eckd_handle_unsolicited_interrupt(struct dasd_device *device,
						   struct irb *irb)
{
	char mask;
1752
	char *sense = NULL;
1753
	struct dasd_eckd_private *private;
1754

1755
	private = (struct dasd_eckd_private *) device->private;
1756 1757
	/* first of all check for state change pending interrupt */
	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
1758
	if ((scsw_dstat(&irb->scsw) & mask) == mask) {
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
		/* for alias only and not in offline processing*/
		if (!device->block && private->lcu &&
		    !test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
			/*
			 * the state change could be caused by an alias
			 * reassignment remove device from alias handling
			 * to prevent new requests from being scheduled on
			 * the wrong alias device
			 */
			dasd_alias_remove_device(device);

			/* schedule worker to reload device */
			dasd_reload_device(device);
		}

1774 1775 1776 1777 1778
		dasd_generic_handle_state_change(device);
		return;
	}

	/* summary unit check */
1779 1780 1781
	sense = dasd_get_sense(irb);
	if (sense && (sense[7] == 0x0D) &&
	    (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
1782 1783 1784 1785
		dasd_alias_handle_summary_unit_check(device, irb);
		return;
	}

1786
	/* service information message SIM */
1787 1788 1789
	if (sense && !(sense[27] & DASD_SENSE_BIT_0) &&
	    ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
		dasd_3990_erp_handle_sim(device, sense);
1790
		dasd_schedule_device_bh(device);
1791 1792 1793
		return;
	}

1794 1795 1796 1797
	if ((scsw_cc(&irb->scsw) == 1) && !sense &&
	    (scsw_fctl(&irb->scsw) == SCSW_FCTL_START_FUNC) &&
	    (scsw_actl(&irb->scsw) == SCSW_ACTL_START_PEND) &&
	    (scsw_stctl(&irb->scsw) == SCSW_STCTL_STATUS_PEND)) {
1798 1799 1800 1801
		/* fake irb do nothing, they are handled elsewhere */
		dasd_schedule_device_bh(device);
		return;
	}
1802

1803
	dasd_schedule_device_bh(device);
1804 1805 1806
	return;
};

1807 1808 1809

static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
					       struct dasd_device *startdev,
1810
					       struct dasd_block *block,
1811 1812 1813 1814 1815 1816 1817 1818 1819
					       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 已提交
1820 1821 1822 1823 1824 1825
{
	struct dasd_eckd_private *private;
	unsigned long *idaws;
	struct LO_eckd_data *LO_data;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
1826
	struct req_iterator iter;
L
Linus Torvalds 已提交
1827 1828
	struct bio_vec *bv;
	char *dst;
1829
	unsigned int off;
L
Linus Torvalds 已提交
1830
	int count, cidaw, cplength, datasize;
1831
	sector_t recid;
L
Linus Torvalds 已提交
1832
	unsigned char cmd, rcmd;
1833 1834
	int use_prefix;
	struct dasd_device *basedev;
L
Linus Torvalds 已提交
1835

1836 1837
	basedev = block->base;
	private = (struct dasd_eckd_private *) basedev->private;
L
Linus Torvalds 已提交
1838 1839 1840 1841 1842 1843
	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);
1844

L
Linus Torvalds 已提交
1845 1846 1847
	/* Check struct bio and count the number of blocks for the request. */
	count = 0;
	cidaw = 0;
1848
	rq_for_each_segment(bv, req, iter) {
1849 1850 1851
		if (bv->bv_len & (blksize - 1))
			/* Eckd can only do full blocks. */
			return ERR_PTR(-EINVAL);
1852
		count += bv->bv_len >> (block->s2b_shift + 9);
1853
#if defined(CONFIG_64BIT)
1854
		if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
1855
			cidaw += bv->bv_len >> (block->s2b_shift + 9);
L
Linus Torvalds 已提交
1856 1857 1858 1859 1860
#endif
	}
	/* Paranoia. */
	if (count != last_rec - first_rec + 1)
		return ERR_PTR(-EINVAL);
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878

	/* 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 已提交
1879 1880 1881 1882 1883 1884 1885 1886
	/* 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. */
1887 1888
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				   startdev);
L
Linus Torvalds 已提交
1889 1890 1891
	if (IS_ERR(cqr))
		return cqr;
	ccw = cqr->cpaddr;
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
	/* 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,
				  last_trk, cmd, 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 DE_eckd_data));
M
Martin Schwidefsky 已提交
1915
	}
L
Linus Torvalds 已提交
1916 1917 1918 1919 1920 1921 1922
	/* 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,
1923
			      last_rec - recid + 1, cmd, basedev, blksize);
L
Linus Torvalds 已提交
1924
	}
1925
	rq_for_each_segment(bv, req, iter) {
L
Linus Torvalds 已提交
1926 1927 1928
		dst = page_address(bv->bv_page) + bv->bv_offset;
		if (dasd_page_cache) {
			char *copy = kmem_cache_alloc(dasd_page_cache,
C
Christoph Lameter 已提交
1929
						      GFP_DMA | __GFP_NOWARN);
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
			if (copy && rq_data_dir(req) == WRITE)
				memcpy(copy + bv->bv_offset, dst, bv->bv_len);
			if (copy)
				dst = copy + bv->bv_offset;
		}
		for (off = 0; off < bv->bv_len; off += blksize) {
			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);
1945 1946
					if (count < blksize &&
					    rq_data_dir(req) == READ)
L
Linus Torvalds 已提交
1947 1948 1949 1950 1951 1952
						memset(dst + count, 0xe5,
						       blksize - count);
				}
				ccw[-1].flags |= CCW_FLAG_CC;
				locate_record(ccw++, LO_data++,
					      trkid, recoffs + 1,
1953
					      1, rcmd, basedev, count);
L
Linus Torvalds 已提交
1954 1955 1956 1957 1958 1959 1960
			}
			/* 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,
1961
					      cmd, basedev, count);
L
Linus Torvalds 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
			}
			/* 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++;
		}
	}
1980 1981
	if (blk_noretry_request(req) ||
	    block->base->features & DASD_FEATURE_FAILFAST)
1982
		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
1983 1984 1985
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
1986
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
L
Linus Torvalds 已提交
1987 1988 1989 1990 1991 1992 1993
	cqr->lpm = private->path_data.ppm;
	cqr->retries = 256;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
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)
{
	struct dasd_eckd_private *private;
	unsigned long *idaws;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	struct req_iterator iter;
	struct bio_vec *bv;
	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;
	private = (struct dasd_eckd_private *) basedev->private;
	if (rq_data_dir(req) == READ)
		cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
	else if (rq_data_dir(req) == WRITE)
		cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
	else
		return ERR_PTR(-EINVAL);

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

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

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

	/* Allocate the ccw request. */
2052 2053
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
				   startdev);
2054 2055 2056 2057 2058 2059 2060 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
	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;
	idaw_dst = 0;
	idaw_len = 0;
	rq_for_each_segment(bv, req, iter) {
		dst = page_address(bv->bv_page) + bv->bv_offset;
		seg_len = bv->bv_len;
		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;
2109 2110 2111
				/* first idaw for a ccw may start anywhere */
				if (!idaw_dst)
					idaw_dst = dst;
2112
			}
2113 2114
			/* If we start a new idaw, we must make sure that it
			 * starts on an IDA_BLOCK_SIZE boundary.
2115 2116 2117 2118
			 * If we continue an idaw, we must make sure that the
			 * current segment begins where the so far accumulated
			 * idaw ends
			 */
2119 2120 2121 2122 2123 2124 2125
			if (!idaw_dst) {
				if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
					dasd_sfree_request(cqr, startdev);
					return ERR_PTR(-ERANGE);
				} else
					idaw_dst = dst;
			}
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
			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);
				idaw_dst = 0;
				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;
2163
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
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 2203 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 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
	cqr->lpm = private->path_data.ppm;
	cqr->retries = 256;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

	dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
		     &pfxdata, sizeof(pfxdata), total_data_size);

	return rc;
}

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_eckd_private *private;
	struct dasd_ccw_req *cqr;
	struct req_iterator iter;
	struct bio_vec *bv;
	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;

	basedev = block->base;
	private = (struct dasd_eckd_private *) basedev->private;
	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.
	 */
	trkcount = last_trk - first_trk + 1;
	ctidaw = 0;
	rq_for_each_segment(bv, req, iter) {
		++ctidaw;
	}

	/* Allocate the ccw request. */
	itcw_size = itcw_calc_size(0, ctidaw, 0);
2353
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
	if (IS_ERR(cqr))
		return cqr;

	cqr->cpmode = 1;
	cqr->startdev = startdev;
	cqr->memdev = startdev;
	cqr->block = block;
	cqr->expires = 100*HZ;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	cqr->retries = 10;

	/* 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);
	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.
		 */
		dasd_sfree_request(cqr, startdev);
		return ERR_PTR(-EAGAIN);
	}

	/*
	 * 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.
	 */
	rq_for_each_segment(bv, req, iter) {
		dst = page_address(bv->bv_page) + bv->bv_offset;
		last_tidaw = itcw_add_tidaw(itcw, 0x00, dst, bv->bv_len);
		if (IS_ERR(last_tidaw))
			return (struct dasd_ccw_req *)last_tidaw;
	}

	last_tidaw->flags |= 0x80;
	itcw_finalize(itcw);

	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;
2411
	cqr->expires = startdev->default_expires * HZ;	/* default 5 minutes */
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
	cqr->lpm = private->path_data.ppm;
	cqr->retries = 256;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;
	return cqr;
}

static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
					       struct dasd_block *block,
					       struct request *req)
{
	int tpm, cmdrtd, cmdwtd;
	int use_prefix;
2425
#if defined(CONFIG_64BIT)
2426
	int fcx_in_css, fcx_in_gneq, fcx_in_features;
2427 2428
#endif
	struct dasd_eckd_private *private;
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
	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;
	struct dasd_ccw_req *cqr;

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

	/* Calculate number of blocks/records per track. */
	blksize = block->bp_block;
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
2443 2444
	if (blk_per_trk == 0)
		return ERR_PTR(-EINVAL);
2445
	/* Calculate record id of first and last block. */
2446
	first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
2447 2448
	first_offs = sector_div(first_trk, blk_per_trk);
	last_rec = last_trk =
2449
		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
2450 2451 2452
	last_offs = sector_div(last_trk, blk_per_trk);
	cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);

2453 2454
	/* is transport mode supported? */
#if defined(CONFIG_64BIT)
2455 2456 2457 2458
	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;
2459 2460 2461
#else
	tpm = 0;
#endif
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498

	/* 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 */
	} else if (!dasd_nofcx && tpm && (first_trk == last_trk)) {
		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);
		if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
			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);
		if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
			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;
}

L
Linus Torvalds 已提交
2499 2500 2501 2502 2503
static int
dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
{
	struct dasd_eckd_private *private;
	struct ccw1 *ccw;
2504
	struct req_iterator iter;
L
Linus Torvalds 已提交
2505 2506 2507 2508
	struct bio_vec *bv;
	char *dst, *cda;
	unsigned int blksize, blk_per_trk, off;
	sector_t recid;
2509
	int status;
L
Linus Torvalds 已提交
2510 2511 2512

	if (!dasd_page_cache)
		goto out;
2513 2514
	private = (struct dasd_eckd_private *) cqr->block->base->private;
	blksize = cqr->block->bp_block;
L
Linus Torvalds 已提交
2515
	blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
2516
	recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
L
Linus Torvalds 已提交
2517 2518 2519 2520 2521
	ccw = cqr->cpaddr;
	/* Skip over define extent & locate record. */
	ccw++;
	if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
		ccw++;
2522
	rq_for_each_segment(bv, req, iter) {
L
Linus Torvalds 已提交
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
		dst = page_address(bv->bv_page) + bv->bv_offset;
		for (off = 0; off < bv->bv_len; off += blksize) {
			/* 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)
						memcpy(dst, cda, bv->bv_len);
					kmem_cache_free(dasd_page_cache,
					    (void *)((addr_t)cda & PAGE_MASK));
				}
				dst = NULL;
			}
			ccw++;
			recid++;
		}
	}
out:
	status = cqr->status == DASD_CQR_DONE;
2547
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
2548 2549 2550
	return status;
}

2551
/*
2552
 * Modify ccw/tcw in cqr so it can be started on a base device.
2553 2554 2555 2556 2557 2558 2559 2560 2561
 *
 * 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;
2562 2563 2564 2565 2566 2567 2568 2569 2570
	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];
2571 2572
		pfxdata->validity.verify_base = 0;
		pfxdata->validity.hyper_pav = 0;
2573 2574 2575 2576 2577 2578 2579
	} 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;
		}
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
	}
}

#define DASD_ECKD_CHANQ_MAX_SIZE 4

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

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

	spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
	private->count++;
	cqr = dasd_eckd_build_cp(startdev, block, req);
	if (IS_ERR(cqr))
		private->count--;
	spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
	return cqr;
}

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

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

L
Linus Torvalds 已提交
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
static int
dasd_eckd_fill_info(struct dasd_device * device,
		    struct dasd_information2_t * info)
{
	struct dasd_eckd_private *private;

	private = (struct dasd_eckd_private *) device->private;
	info->label_block = 2;
	info->FBA_layout = private->uses_cdl ? 0 : 1;
	info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
	info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
	memcpy(info->characteristics, &private->rdc_data,
	       sizeof(struct dasd_eckd_characteristics));
2636 2637 2638 2639
	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 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648
	return 0;
}

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

/*
 * Release device ioctl.
2649
 * Buils a channel programm to releases a prior reserved
L
Linus Torvalds 已提交
2650 2651 2652
 * (see dasd_eckd_reserve) device.
 */
static int
2653
dasd_eckd_release(struct dasd_device *device)
L
Linus Torvalds 已提交
2654 2655 2656
{
	struct dasd_ccw_req *cqr;
	int rc;
2657
	struct ccw1 *ccw;
2658
	int useglobal;
L
Linus Torvalds 已提交
2659 2660 2661 2662

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

2663
	useglobal = 0;
2664
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
2665
	if (IS_ERR(cqr)) {
2666 2667 2668 2669 2670 2671 2672 2673 2674
		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 已提交
2675
	}
2676 2677 2678 2679 2680
	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;
2681 2682
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
2683
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2684
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2685
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
2686 2687 2688 2689 2690 2691
	cqr->expires = 2 * HZ;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);

2692 2693 2694 2695
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
2696 2697 2698 2699 2700 2701
	return rc;
}

/*
 * Reserve device ioctl.
 * Options are set to 'synchronous wait for interrupt' and
2702 2703
 * 'timeout the request'. This leads to a terminate IO if
 * the interrupt is outstanding for a certain time.
L
Linus Torvalds 已提交
2704 2705
 */
static int
2706
dasd_eckd_reserve(struct dasd_device *device)
L
Linus Torvalds 已提交
2707 2708 2709
{
	struct dasd_ccw_req *cqr;
	int rc;
2710
	struct ccw1 *ccw;
2711
	int useglobal;
L
Linus Torvalds 已提交
2712 2713 2714 2715

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

2716
	useglobal = 0;
2717
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
2718
	if (IS_ERR(cqr)) {
2719 2720 2721 2722 2723 2724 2725 2726 2727
		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 已提交
2728
	}
2729 2730 2731 2732 2733
	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;
2734 2735
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
2736
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2737
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2738
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
2739 2740 2741 2742 2743 2744
	cqr->expires = 2 * HZ;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);

2745 2746 2747 2748
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
2749 2750 2751 2752 2753
	return rc;
}

/*
 * Steal lock ioctl - unconditional reserve device.
2754
 * Buils a channel programm to break a device's reservation.
L
Linus Torvalds 已提交
2755 2756 2757
 * (unconditional reserve)
 */
static int
2758
dasd_eckd_steal_lock(struct dasd_device *device)
L
Linus Torvalds 已提交
2759 2760 2761
{
	struct dasd_ccw_req *cqr;
	int rc;
2762
	struct ccw1 *ccw;
2763
	int useglobal;
L
Linus Torvalds 已提交
2764 2765 2766 2767

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

2768
	useglobal = 0;
2769
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
L
Linus Torvalds 已提交
2770
	if (IS_ERR(cqr)) {
2771 2772 2773 2774 2775 2776 2777 2778 2779
		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 已提交
2780
	}
2781 2782 2783 2784 2785
	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;
2786 2787
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
2788
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2789
	set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2790
	cqr->retries = 2;	/* set retry counter to enable basic ERP */
L
Linus Torvalds 已提交
2791 2792 2793 2794 2795 2796
	cqr->expires = 2 * HZ;
	cqr->buildclk = get_clock();
	cqr->status = DASD_CQR_FILLED;

	rc = dasd_sleep_on_immediatly(cqr);

2797 2798 2799 2800
	if (useglobal)
		mutex_unlock(&dasd_reserve_mutex);
	else
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
2801 2802 2803
	return rc;
}

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
/*
 * 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);
	cqr->retries = 5;
	cqr->expires = 10 * HZ;
	cqr->buildclk = get_clock();
	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 已提交
2871 2872 2873 2874
/*
 * Read performance statistics
 */
static int
2875
dasd_eckd_performance(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
2876 2877 2878 2879 2880 2881 2882
{
	struct dasd_psf_prssd_data *prssdp;
	struct dasd_rssd_perf_stats_t *stats;
	struct dasd_ccw_req *cqr;
	struct ccw1 *ccw;
	int rc;

2883
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
2884 2885
				   (sizeof(struct dasd_psf_prssd_data) +
				    sizeof(struct dasd_rssd_perf_stats_t)),
L
Linus Torvalds 已提交
2886 2887
				   device);
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
2888
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
L
Linus Torvalds 已提交
2889 2890 2891
			    "Could not allocate initialization request");
		return PTR_ERR(cqr);
	}
2892 2893
	cqr->startdev = device;
	cqr->memdev = device;
L
Linus Torvalds 已提交
2894
	cqr->retries = 0;
2895
	clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
L
Linus Torvalds 已提交
2896 2897 2898 2899
	cqr->expires = 10 * HZ;

	/* Prepare for Read Subsystem Data */
	prssdp = (struct dasd_psf_prssd_data *) cqr->data;
2900
	memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
L
Linus Torvalds 已提交
2901
	prssdp->order = PSF_ORDER_PRSSD;
2902
	prssdp->suborder = 0x01;	/* Performance Statistics */
L
Linus Torvalds 已提交
2903 2904 2905 2906
	prssdp->varies[1] = 0x01;	/* Perf Statistics for the Subsystem */

	ccw = cqr->cpaddr;
	ccw->cmd_code = DASD_ECKD_CCW_PSF;
2907
	ccw->count = sizeof(struct dasd_psf_prssd_data);
L
Linus Torvalds 已提交
2908 2909 2910 2911 2912
	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);
2913
	memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
L
Linus Torvalds 已提交
2914 2915 2916

	ccw++;
	ccw->cmd_code = DASD_ECKD_CCW_RSSD;
2917
	ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
L
Linus Torvalds 已提交
2918 2919 2920 2921 2922 2923 2924 2925
	ccw->cda = (__u32)(addr_t) stats;

	cqr->buildclk = get_clock();
	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);
2926 2927 2928
		if (copy_to_user(argp, stats,
				 sizeof(struct dasd_rssd_perf_stats_t)))
			rc = -EFAULT;
L
Linus Torvalds 已提交
2929
	}
2930
	dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
2931 2932 2933 2934 2935 2936 2937 2938
	return rc;
}

/*
 * Get attributes (cache operations)
 * Returnes the cache attributes used in Define Extend (DE).
 */
static int
2939
dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
2940
{
2941 2942 2943
	struct dasd_eckd_private *private =
		(struct dasd_eckd_private *)device->private;
	struct attrib_data_t attrib = private->attrib;
L
Linus Torvalds 已提交
2944 2945 2946 2947
	int rc;

        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
2948
	if (!argp)
L
Linus Torvalds 已提交
2949 2950
                return -EINVAL;

2951 2952
	rc = 0;
	if (copy_to_user(argp, (long *) &attrib,
2953
			 sizeof(struct attrib_data_t)))
2954
		rc = -EFAULT;
L
Linus Torvalds 已提交
2955 2956 2957 2958 2959 2960 2961 2962 2963

	return rc;
}

/*
 * Set attributes (cache operations)
 * Stores the attributes for cache operation to be used in Define Extend (DE).
 */
static int
2964
dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
L
Linus Torvalds 已提交
2965
{
2966 2967
	struct dasd_eckd_private *private =
		(struct dasd_eckd_private *)device->private;
L
Linus Torvalds 已提交
2968 2969 2970 2971
	struct attrib_data_t attrib;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
2972
	if (!argp)
L
Linus Torvalds 已提交
2973 2974
		return -EINVAL;

2975
	if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
L
Linus Torvalds 已提交
2976 2977 2978
		return -EFAULT;
	private->attrib = attrib;

S
Stefan Haberland 已提交
2979 2980 2981
	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 已提交
2982 2983 2984
	return 0;
}

2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
/*
 * 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;
2995
	char psf0, psf1;
2996 2997
	int rc;

2998 2999 3000 3001
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
		return -EACCES;
	psf0 = psf1 = 0;

3002 3003 3004 3005
	/* Copy parms from caller */
	rc = -EFAULT;
	if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
		goto out;
3006 3007
	if (is_compat_task() || sizeof(long) == 4) {
		/* Make sure pointers are sane even on 31 bit. */
3008
		rc = -EINVAL;
3009 3010 3011 3012 3013 3014
		if ((usrparm.psf_data >> 32) != 0)
			goto out;
		if ((usrparm.rssd_result >> 32) != 0)
			goto out;
		usrparm.psf_data &= 0x7fffffffULL;
		usrparm.rssd_result &= 0x7fffffffULL;
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
	}
	/* 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;
3030 3031
	psf0 = psf_data[0];
	psf1 = psf_data[1];
3032 3033

	/* setup CCWs for PSF + RSSD */
3034
	cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
3035
	if (IS_ERR(cqr)) {
S
Stefan Haberland 已提交
3036
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
			"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;
	cqr->buildclk = get_clock();
	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:
3082 3083 3084
	DBF_DEV_EVENT(DBF_WARNING, device,
		      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
		      (int) psf0, (int) psf1, rc);
3085 3086 3087
	return rc;
}

3088
static int
3089
dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
3090
{
3091 3092
	struct dasd_device *device = block->base;

3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
	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);
3106 3107
	case BIODASDSNID:
		return dasd_eckd_snid(device, argp);
3108 3109
	case BIODASDSYMMIO:
		return dasd_symm_io(device, argp);
3110 3111 3112 3113 3114
	default:
		return -ENOIOCTLCMD;
	}
}

H
Horst Hummel 已提交
3115 3116 3117 3118
/*
 * Dump the range of CCWs into 'page' buffer
 * and return number of printed chars.
 */
3119
static int
H
Horst Hummel 已提交
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
{
	int len, count;
	char *datap;

	len = 0;
	while (from <= to) {
		len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " 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 已提交
3149
static void
3150 3151
dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
			 char *reason)
S
Stefan Haberland 已提交
3152 3153
{
	u64 *sense;
3154
	u64 *stat;
3155 3156

	sense = (u64 *) dasd_get_sense(irb);
3157
	stat = (u64 *) &irb->scsw;
S
Stefan Haberland 已提交
3158
	if (sense) {
3159 3160 3161
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
			      "%016llx %016llx %016llx %016llx",
			      reason, *stat, *((u32 *) (stat + 1)),
S
Stefan Haberland 已提交
3162
			      sense[0], sense[1], sense[2], sense[3]);
S
Stefan Haberland 已提交
3163
	} else {
3164 3165 3166
		DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
			      reason, *stat, *((u32 *) (stat + 1)),
			      "NO VALID SENSE");
S
Stefan Haberland 已提交
3167 3168 3169
	}
}

L
Linus Torvalds 已提交
3170 3171 3172 3173
/*
 * Print sense data and related channel program.
 * Parts are printed because printk buffer is only 1024 bytes.
 */
3174
static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
3175
				 struct dasd_ccw_req *req, struct irb *irb)
L
Linus Torvalds 已提交
3176 3177
{
	char *page;
H
Horst Hummel 已提交
3178 3179
	struct ccw1 *first, *last, *fail, *from, *to;
	int len, sl, sct;
L
Linus Torvalds 已提交
3180 3181 3182

	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
3183 3184
		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
			      "No memory to dump sense data\n");
L
Linus Torvalds 已提交
3185 3186
		return;
	}
H
Horst Hummel 已提交
3187 3188
	/* dump the sense data */
	len = sprintf(page,  KERN_ERR PRINTK_HEADER
L
Linus Torvalds 已提交
3189
		      " I/O status report for device %s:\n",
3190
		      dev_name(&device->cdev->dev));
L
Linus Torvalds 已提交
3191
	len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3192 3193 3194 3195 3196 3197
		       " 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);
L
Linus Torvalds 已提交
3198 3199
	len += sprintf(page + len, KERN_ERR PRINTK_HEADER
		       " device %s: Failing CCW: %p\n",
3200
		       dev_name(&device->cdev->dev),
3201
		       (void *) (addr_t) irb->scsw.cmd.cpa);
L
Linus Torvalds 已提交
3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
	if (irb->esw.esw0.erw.cons) {
		for (sl = 0; sl < 4; sl++) {
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
				       " 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 */
H
Horst Hummel 已提交
3217 3218 3219 3220 3221
			sprintf(page + len, KERN_ERR PRINTK_HEADER
				" 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 已提交
3222 3223
		} else {
			/* 32 Byte Sense Data */
H
Horst Hummel 已提交
3224 3225 3226 3227
			sprintf(page + len, KERN_ERR PRINTK_HEADER
				" 32 Byte: Format: %x "
				"Exception class %x\n",
				irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
L
Linus Torvalds 已提交
3228 3229
		}
	} else {
H
Horst Hummel 已提交
3230 3231
		sprintf(page + len, KERN_ERR PRINTK_HEADER
			" SORRY - NO VALID SENSE AVAILABLE\n");
L
Linus Torvalds 已提交
3232
	}
H
Horst Hummel 已提交
3233 3234
	printk("%s", page);

3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
	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);
		len = sprintf(page,  KERN_ERR PRINTK_HEADER
			      " Related CP in req: %p\n", req);
		dasd_eckd_dump_ccw_range(first, to, page + len);
		printk("%s", page);
L
Linus Torvalds 已提交
3246

3247 3248 3249 3250
		/* print failing CCW area (maximum 4) */
		/* scsw->cda is either valid or zero  */
		len = 0;
		from = ++to;
3251 3252
		fail = (struct ccw1 *)(addr_t)
				irb->scsw.cmd.cpa; /* failing CCW */
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
		if (from <  fail - 2) {
			from = fail - 2;     /* there is a gap - print header */
			len += sprintf(page, KERN_ERR PRINTK_HEADER "......\n");
		}
		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 */
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
		}
		len += dasd_eckd_dump_ccw_range(from, last, page + len);
		if (len > 0)
			printk("%s", page);
L
Linus Torvalds 已提交
3269 3270 3271 3272
	}
	free_page((unsigned long) page);
}

3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288

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


	page = (char *) get_zeroed_page(GFP_ATOMIC);
	if (page == NULL) {
S
Stefan Haberland 已提交
3289
		DBF_DEV_EVENT(DBF_WARNING, device, " %s",
3290 3291 3292 3293 3294 3295 3296 3297
			    "No memory to dump sense data");
		return;
	}
	/* dump the sense data */
	len = sprintf(page,  KERN_ERR PRINTK_HEADER
		      " I/O status report for device %s:\n",
		      dev_name(&device->cdev->dev));
	len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3298 3299 3300 3301 3302 3303 3304
		       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
		       "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
		       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
		       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
		       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
		       irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
		       req ? req->intrc : 0);
3305 3306 3307 3308 3309 3310 3311
	len += sprintf(page + len, KERN_ERR PRINTK_HEADER
		       " device %s: Failing TCW: %p\n",
		       dev_name(&device->cdev->dev),
		       (void *) (addr_t) irb->scsw.tm.tcw);

	tsb = NULL;
	sense = NULL;
3312
	if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
3313 3314 3315
		tsb = tcw_get_tsb(
			(struct tcw *)(unsigned long)irb->scsw.tm.tcw);

3316
	if (tsb) {
3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409
		len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->length %d\n", tsb->length);
		len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->flags %x\n", tsb->flags);
		len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->dcw_offset %d\n", tsb->dcw_offset);
		len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->count %d\n", tsb->count);
		residual = tsb->count - 28;
		len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " residual %d\n", residual);

		switch (tsb->flags & 0x07) {
		case 1:	/* tsa_iostat */
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.iostat.dev_time %d\n",
				       tsb->tsa.iostat.dev_time);
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.iostat.def_time %d\n",
				       tsb->tsa.iostat.def_time);
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.iostat.queue_time %d\n",
				       tsb->tsa.iostat.queue_time);
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.iostat.dev_busy_time %d\n",
				       tsb->tsa.iostat.dev_busy_time);
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.iostat.dev_act_time %d\n",
				       tsb->tsa.iostat.dev_act_time);
			sense = tsb->tsa.iostat.sense;
			break;
		case 2: /* ts_ddpc */
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
			       " tsb->tsa.ddpc.rcq:  ");
			for (sl = 0; sl < 16; sl++) {
				for (sct = 0; sct < 8; sct++) {
					len += sprintf(page + len, " %02x",
						       tsb->tsa.ddpc.rcq[sl]);
				}
				len += sprintf(page + len, "\n");
			}
			sense = tsb->tsa.ddpc.sense;
			break;
		case 3: /* tsa_intrg */
			len += sprintf(page + len, KERN_ERR PRINTK_HEADER
				      " tsb->tsa.intrg.: not supportet yet \n");
			break;
		}

		if (sense) {
			for (sl = 0; sl < 4; sl++) {
				len += sprintf(page + len,
					       KERN_ERR PRINTK_HEADER
					       " 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 */
				sprintf(page + len, KERN_ERR PRINTK_HEADER
					" 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 */
				sprintf(page + len, KERN_ERR PRINTK_HEADER
					" 32 Byte: Format: %x "
					"Exception class %x\n",
					sense[6] & 0x0f, sense[22] >> 4);
			}
		} else {
			sprintf(page + len, KERN_ERR PRINTK_HEADER
				" SORRY - NO VALID SENSE AVAILABLE\n");
		}
	} else {
		sprintf(page + len, KERN_ERR PRINTK_HEADER
			" SORRY - NO TSB DATA AVAILABLE\n");
	}
	printk("%s", page);
	free_page((unsigned long) page);
}

static void dasd_eckd_dump_sense(struct dasd_device *device,
				 struct dasd_ccw_req *req, struct irb *irb)
{
3410
	if (scsw_is_tm(&irb->scsw))
3411 3412 3413 3414 3415
		dasd_eckd_dump_sense_tcw(device, req, irb);
	else
		dasd_eckd_dump_sense_ccw(device, req, irb);
}

3416
static int dasd_eckd_pm_freeze(struct dasd_device *device)
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
{
	/*
	 * 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;
}

3429
static int dasd_eckd_restore_device(struct dasd_device *device)
3430 3431
{
	struct dasd_eckd_private *private;
3432
	struct dasd_eckd_characteristics temp_rdc_data;
3433 3434
	int is_known, rc;
	struct dasd_uid temp_uid;
S
Stefan Haberland 已提交
3435
	unsigned long flags;
3436 3437 3438 3439 3440 3441 3442 3443

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

	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err;

3444 3445 3446 3447
	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);
3448
	if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
S
Stefan Haberland 已提交
3449 3450
		dev_err(&device->cdev->dev, "The UID of the DASD has "
			"changed\n");
3451
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
3452 3453 3454 3455 3456 3457 3458 3459
	if (rc)
		goto out_err;

	/* register lcu with alias handling, enable PAV if this is a new lcu */
	is_known = dasd_alias_make_device_known_to_lcu(device);
	if (is_known < 0)
		return is_known;
	if (!is_known) {
3460 3461 3462 3463 3464 3465 3466 3467 3468
		dasd_eckd_validate_server(device);
		dasd_alias_lcu_setup_complete(device);
	} else
		dasd_alias_wait_for_lcu_setup(device);

	/* RE-Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err;
3469 3470

	/* Read Feature Codes */
3471
	dasd_eckd_read_features(device);
3472 3473

	/* Read Device Characteristics */
3474
	rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
3475
					 &temp_rdc_data, 64);
3476
	if (rc) {
3477 3478
		DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
				"Read device characteristic failed, rc=%d", rc);
3479 3480
		goto out_err;
	}
S
Stefan Haberland 已提交
3481
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
3482
	memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
S
Stefan Haberland 已提交
3483
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
3484 3485 3486 3487 3488 3489 3490

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

	return 0;

out_err:
3491
	return -1;
3492 3493
}

3494 3495 3496 3497
static int dasd_eckd_reload_device(struct dasd_device *device)
{
	struct dasd_eckd_private *private;
	int rc, old_base;
3498 3499 3500
	char print_uid[60];
	struct dasd_uid uid;
	unsigned long flags;
3501 3502

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

	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
3505
	old_base = private->uid.base_unit_addr;
3506 3507
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);

3508 3509 3510 3511 3512
	/* Read Configuration Data */
	rc = dasd_eckd_read_conf(device);
	if (rc)
		goto out_err;

3513
	rc = dasd_eckd_generate_uid(device);
3514 3515 3516 3517 3518 3519 3520 3521
	if (rc)
		goto out_err;
	/*
	 * update unit address configuration and
	 * add device to alias management
	 */
	dasd_alias_update_add_device(device);

3522 3523 3524 3525 3526 3527 3528
	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);
3529
		else
3530 3531 3532
			snprintf(print_uid, sizeof(print_uid),
				 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
				 uid.ssid, uid.base_unit_addr);
3533 3534 3535

		dev_info(&device->cdev->dev,
			 "An Alias device was reassigned to a new base device "
3536
			 "with UID: %s\n", print_uid);
3537 3538 3539 3540 3541 3542 3543
	}
	return 0;

out_err:
	return -1;
}

3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555
static struct ccw_driver dasd_eckd_driver = {
	.name	     = "dasd-eckd",
	.owner	     = THIS_MODULE,
	.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,
	.freeze      = dasd_generic_pm_freeze,
	.thaw	     = dasd_generic_restore_device,
	.restore     = dasd_generic_restore_device,
3556
	.uc_handler  = dasd_generic_uc_handler,
3557
};
3558

L
Linus Torvalds 已提交
3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577
/*
 * 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",
	.max_blocks = 240,
	.check_device = dasd_eckd_check_characteristics,
3578
	.uncheck_device = dasd_eckd_uncheck_device,
L
Linus Torvalds 已提交
3579
	.do_analysis = dasd_eckd_do_analysis,
3580 3581
	.ready_to_online = dasd_eckd_ready_to_online,
	.online_to_ready = dasd_eckd_online_to_ready,
L
Linus Torvalds 已提交
3582 3583 3584
	.fill_geometry = dasd_eckd_fill_geometry,
	.start_IO = dasd_start_IO,
	.term_IO = dasd_term_IO,
3585
	.handle_terminated_request = dasd_eckd_handle_terminated_request,
L
Linus Torvalds 已提交
3586 3587 3588
	.format_device = dasd_eckd_format_device,
	.erp_action = dasd_eckd_erp_action,
	.erp_postaction = dasd_eckd_erp_postaction,
3589 3590 3591
	.handle_unsolicited_interrupt = dasd_eckd_handle_unsolicited_interrupt,
	.build_cp = dasd_eckd_build_alias_cp,
	.free_cp = dasd_eckd_free_alias_cp,
L
Linus Torvalds 已提交
3592
	.dump_sense = dasd_eckd_dump_sense,
S
Stefan Haberland 已提交
3593
	.dump_sense_dbf = dasd_eckd_dump_sense_dbf,
L
Linus Torvalds 已提交
3594
	.fill_info = dasd_eckd_fill_info,
3595
	.ioctl = dasd_eckd_ioctl,
3596 3597
	.freeze = dasd_eckd_pm_freeze,
	.restore = dasd_eckd_restore_device,
3598
	.reload = dasd_eckd_reload_device,
3599
	.get_uid = dasd_eckd_get_uid,
L
Linus Torvalds 已提交
3600 3601 3602 3603 3604
};

static int __init
dasd_eckd_init(void)
{
3605 3606
	int ret;

L
Linus Torvalds 已提交
3607
	ASCEBC(dasd_eckd_discipline.ebcname, 4);
3608 3609 3610 3611
	dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
				   GFP_KERNEL | GFP_DMA);
	if (!dasd_reserve_req)
		return -ENOMEM;
3612 3613 3614
	ret = ccw_driver_register(&dasd_eckd_driver);
	if (!ret)
		wait_for_device_probe();
3615 3616
	else
		kfree(dasd_reserve_req);
3617
	return ret;
L
Linus Torvalds 已提交
3618 3619 3620 3621 3622 3623
}

static void __exit
dasd_eckd_cleanup(void)
{
	ccw_driver_unregister(&dasd_eckd_driver);
3624
	kfree(dasd_reserve_req);
L
Linus Torvalds 已提交
3625 3626 3627 3628
}

module_init(dasd_eckd_init);
module_exit(dasd_eckd_cleanup);