[S390] Use diagnose 308 for system reset
[linux-block.git] / drivers / s390 / block / dasd_eckd.c
CommitLineData
138c014d 1/*
1da177e4
LT
2 * File...........: linux/drivers/s390/block/dasd_eckd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
138c014d 4 * Horst Hummel <Horst.Hummel@de.ibm.com>
1da177e4
LT
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
d41dd122 8 * Copyright IBM Corp. 1999, 2009
ab1d848f
NH
9 * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
10 * Author.........: Nigel Hislop <hislop_nigel@emc.com>
1da177e4
LT
11 */
12
ca99dab0 13#define KMSG_COMPONENT "dasd-eckd"
fc19f381 14
1da177e4
LT
15#include <linux/stddef.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/hdreg.h> /* HDIO_GETGEO */
19#include <linux/bio.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
23#include <asm/debug.h>
24#include <asm/idals.h>
25#include <asm/ebcdic.h>
f8b06859 26#include <asm/compat.h>
1da177e4 27#include <asm/io.h>
1da177e4 28#include <asm/uaccess.h>
40545573 29#include <asm/cio.h>
1da177e4 30#include <asm/ccwdev.h>
f3eb5384 31#include <asm/itcw.h>
1da177e4
LT
32
33#include "dasd_int.h"
34#include "dasd_eckd.h"
f3eb5384
SW
35#include "../cio/chsc.h"
36
1da177e4
LT
37
38#ifdef PRINTK_HEADER
39#undef PRINTK_HEADER
40#endif /* PRINTK_HEADER */
41#define PRINTK_HEADER "dasd(eckd):"
42
43#define ECKD_C0(i) (i->home_bytes)
44#define ECKD_F(i) (i->formula)
45#define ECKD_F1(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f1):\
46 (i->factors.f_0x02.f1))
47#define ECKD_F2(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f2):\
48 (i->factors.f_0x02.f2))
49#define ECKD_F3(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f3):\
50 (i->factors.f_0x02.f3))
51#define ECKD_F4(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f4):0)
52#define ECKD_F5(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f5):0)
53#define ECKD_F6(i) (i->factor6)
54#define ECKD_F7(i) (i->factor7)
55#define ECKD_F8(i) (i->factor8)
56
e4dbb0f2
SH
57/*
58 * raw track access always map to 64k in memory
59 * so it maps to 16 blocks of 4k per track
60 */
61#define DASD_RAW_BLOCK_PER_TRACK 16
62#define DASD_RAW_BLOCKSIZE 4096
63/* 64k are 128 x 512 byte sectors */
64#define DASD_RAW_SECTORS_PER_TRACK 128
65
1da177e4
LT
66MODULE_LICENSE("GPL");
67
68static struct dasd_discipline dasd_eckd_discipline;
69
1da177e4
LT
70/* The ccw bus type uses this table to find devices that it sends to
71 * dasd_eckd_probe */
72static struct ccw_device_id dasd_eckd_ids[] = {
d2c993d8
HC
73 { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
74 { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
5da24b76 75 { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
d2c993d8
HC
76 { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), .driver_info = 0x4},
77 { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), .driver_info = 0x5},
78 { CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), .driver_info = 0x6},
79 { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), .driver_info = 0x7},
80 { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), .driver_info = 0x8},
81 { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), .driver_info = 0x9},
82 { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), .driver_info = 0xa},
1da177e4
LT
83 { /* end of list */ },
84};
85
86MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);
87
88static struct ccw_driver dasd_eckd_driver; /* see below */
89
eb6e199b
SW
90#define INIT_CQR_OK 0
91#define INIT_CQR_UNFORMATTED 1
92#define INIT_CQR_ERROR 2
93
f932bcea
SW
94/* emergency request for reserve/release */
95static struct {
96 struct dasd_ccw_req cqr;
97 struct ccw1 ccw;
98 char data[32];
99} *dasd_reserve_req;
100static DEFINE_MUTEX(dasd_reserve_mutex);
101
a4d26c6a
SW
102/* definitions for the path verification worker */
103struct path_verification_work_data {
104 struct work_struct worker;
105 struct dasd_device *device;
106 struct dasd_ccw_req cqr;
107 struct ccw1 ccw;
108 __u8 rcd_buffer[DASD_ECKD_RCD_DATA_SIZE];
109 int isglobal;
110 __u8 tbvpm;
111};
112static struct path_verification_work_data *path_verification_worker;
113static DEFINE_MUTEX(dasd_path_verification_mutex);
eb6e199b 114
1da177e4
LT
115/* initial attempt at a probe function. this can be simplified once
116 * the other detection code is gone */
117static int
118dasd_eckd_probe (struct ccw_device *cdev)
119{
120 int ret;
121
40545573 122 /* set ECKD specific ccw-device options */
454e1fa1
PO
123 ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
124 CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
40545573 125 if (ret) {
b8ed5dd5
SH
126 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
127 "dasd_eckd_probe: could not set "
128 "ccw-device options");
1da177e4 129 return ret;
40545573
HH
130 }
131 ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
132 return ret;
1da177e4
LT
133}
134
135static int
136dasd_eckd_set_online(struct ccw_device *cdev)
137{
40545573 138 return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
1da177e4
LT
139}
140
1da177e4
LT
141static const int sizes_trk0[] = { 28, 148, 84 };
142#define LABEL_SIZE 140
143
144static inline unsigned int
145round_up_multiple(unsigned int no, unsigned int mult)
146{
147 int rem = no % mult;
148 return (rem ? no - rem + mult : no);
149}
150
151static inline unsigned int
152ceil_quot(unsigned int d1, unsigned int d2)
153{
154 return (d1 + (d2 - 1)) / d2;
155}
156
4d284cac 157static unsigned int
1da177e4
LT
158recs_per_track(struct dasd_eckd_characteristics * rdc,
159 unsigned int kl, unsigned int dl)
160{
161 int dn, kn;
162
163 switch (rdc->dev_type) {
164 case 0x3380:
165 if (kl)
166 return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
167 ceil_quot(dl + 12, 32));
168 else
169 return 1499 / (15 + ceil_quot(dl + 12, 32));
170 case 0x3390:
171 dn = ceil_quot(dl + 6, 232) + 1;
172 if (kl) {
173 kn = ceil_quot(kl + 6, 232) + 1;
174 return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
175 9 + ceil_quot(dl + 6 * dn, 34));
176 } else
177 return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
178 case 0x9345:
179 dn = ceil_quot(dl + 6, 232) + 1;
180 if (kl) {
181 kn = ceil_quot(kl + 6, 232) + 1;
182 return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
183 ceil_quot(dl + 6 * dn, 34));
184 } else
185 return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
186 }
187 return 0;
188}
189
b44b0ab3
SW
190static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
191{
192 geo->cyl = (__u16) cyl;
193 geo->head = cyl >> 16;
194 geo->head <<= 4;
195 geo->head |= head;
196}
197
4d284cac 198static int
1da177e4
LT
199check_XRC (struct ccw1 *de_ccw,
200 struct DE_eckd_data *data,
201 struct dasd_device *device)
202{
203 struct dasd_eckd_private *private;
d54853ef 204 int rc;
1da177e4
LT
205
206 private = (struct dasd_eckd_private *) device->private;
d54853ef
MS
207 if (!private->rdc_data.facilities.XRC_supported)
208 return 0;
1da177e4
LT
209
210 /* switch on System Time Stamp - needed for XRC Support */
d54853ef
MS
211 data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid' */
212 data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
1da177e4 213
d54853ef
MS
214 rc = get_sync_clock(&data->ep_sys_time);
215 /* Ignore return code if sync clock is switched off. */
216 if (rc == -ENOSYS || rc == -EACCES)
217 rc = 0;
1da177e4 218
8e09f215 219 de_ccw->count = sizeof(struct DE_eckd_data);
d54853ef
MS
220 de_ccw->flags |= CCW_FLAG_SLI;
221 return rc;
222}
1da177e4 223
4d284cac 224static int
b44b0ab3
SW
225define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
226 unsigned int totrk, int cmd, struct dasd_device *device)
1da177e4
LT
227{
228 struct dasd_eckd_private *private;
b44b0ab3
SW
229 u32 begcyl, endcyl;
230 u16 heads, beghead, endhead;
d54853ef 231 int rc = 0;
1da177e4
LT
232
233 private = (struct dasd_eckd_private *) device->private;
234
235 ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
236 ccw->flags = 0;
237 ccw->count = 16;
238 ccw->cda = (__u32) __pa(data);
239
8e09f215 240 memset(data, 0, sizeof(struct DE_eckd_data));
1da177e4
LT
241 switch (cmd) {
242 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
243 case DASD_ECKD_CCW_READ_RECORD_ZERO:
244 case DASD_ECKD_CCW_READ:
245 case DASD_ECKD_CCW_READ_MT:
246 case DASD_ECKD_CCW_READ_CKD:
247 case DASD_ECKD_CCW_READ_CKD_MT:
248 case DASD_ECKD_CCW_READ_KD:
249 case DASD_ECKD_CCW_READ_KD_MT:
250 case DASD_ECKD_CCW_READ_COUNT:
251 data->mask.perm = 0x1;
252 data->attributes.operation = private->attrib.operation;
253 break;
254 case DASD_ECKD_CCW_WRITE:
255 case DASD_ECKD_CCW_WRITE_MT:
256 case DASD_ECKD_CCW_WRITE_KD:
257 case DASD_ECKD_CCW_WRITE_KD_MT:
258 data->mask.perm = 0x02;
259 data->attributes.operation = private->attrib.operation;
d54853ef 260 rc = check_XRC (ccw, data, device);
1da177e4
LT
261 break;
262 case DASD_ECKD_CCW_WRITE_CKD:
263 case DASD_ECKD_CCW_WRITE_CKD_MT:
264 data->attributes.operation = DASD_BYPASS_CACHE;
d54853ef 265 rc = check_XRC (ccw, data, device);
1da177e4
LT
266 break;
267 case DASD_ECKD_CCW_ERASE:
268 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
269 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
270 data->mask.perm = 0x3;
271 data->mask.auth = 0x1;
272 data->attributes.operation = DASD_BYPASS_CACHE;
d54853ef 273 rc = check_XRC (ccw, data, device);
1da177e4
LT
274 break;
275 default:
fc19f381
SH
276 dev_err(&device->cdev->dev,
277 "0x%x is not a known command\n", cmd);
1da177e4
LT
278 break;
279 }
280
281 data->attributes.mode = 0x3; /* ECKD */
282
283 if ((private->rdc_data.cu_type == 0x2105 ||
284 private->rdc_data.cu_type == 0x2107 ||
285 private->rdc_data.cu_type == 0x1750)
286 && !(private->uses_cdl && trk < 2))
287 data->ga_extended |= 0x40; /* Regular Data Format Mode */
288
b44b0ab3
SW
289 heads = private->rdc_data.trk_per_cyl;
290 begcyl = trk / heads;
291 beghead = trk % heads;
292 endcyl = totrk / heads;
293 endhead = totrk % heads;
1da177e4
LT
294
295 /* check for sequential prestage - enhance cylinder range */
296 if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
297 data->attributes.operation == DASD_SEQ_ACCESS) {
138c014d 298
b44b0ab3
SW
299 if (endcyl + private->attrib.nr_cyl < private->real_cyl)
300 endcyl += private->attrib.nr_cyl;
1da177e4 301 else
b44b0ab3 302 endcyl = (private->real_cyl - 1);
1da177e4
LT
303 }
304
b44b0ab3
SW
305 set_ch_t(&data->beg_ext, begcyl, beghead);
306 set_ch_t(&data->end_ext, endcyl, endhead);
d54853ef 307 return rc;
1da177e4
LT
308}
309
8e09f215
SW
310static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
311 struct dasd_device *device)
312{
313 struct dasd_eckd_private *private;
314 int rc;
315
316 private = (struct dasd_eckd_private *) device->private;
317 if (!private->rdc_data.facilities.XRC_supported)
318 return 0;
319
320 /* switch on System Time Stamp - needed for XRC Support */
f3eb5384
SW
321 pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid' */
322 pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
8e09f215
SW
323 pfxdata->validity.time_stamp = 1; /* 'Time Stamp Valid' */
324
f3eb5384 325 rc = get_sync_clock(&pfxdata->define_extent.ep_sys_time);
8e09f215
SW
326 /* Ignore return code if sync clock is switched off. */
327 if (rc == -ENOSYS || rc == -EACCES)
328 rc = 0;
329 return rc;
330}
331
f3eb5384
SW
332static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
333 unsigned int rec_on_trk, int count, int cmd,
334 struct dasd_device *device, unsigned int reclen,
335 unsigned int tlf)
336{
337 struct dasd_eckd_private *private;
338 int sector;
339 int dn, d;
340
341 private = (struct dasd_eckd_private *) device->private;
342
343 memset(data, 0, sizeof(*data));
344 sector = 0;
345 if (rec_on_trk) {
346 switch (private->rdc_data.dev_type) {
347 case 0x3390:
348 dn = ceil_quot(reclen + 6, 232);
349 d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
350 sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
351 break;
352 case 0x3380:
353 d = 7 + ceil_quot(reclen + 12, 32);
354 sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
355 break;
356 }
357 }
358 data->sector = sector;
359 /* note: meaning of count depends on the operation
360 * for record based I/O it's the number of records, but for
361 * track based I/O it's the number of tracks
362 */
363 data->count = count;
364 switch (cmd) {
365 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
366 data->operation.orientation = 0x3;
367 data->operation.operation = 0x03;
368 break;
369 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
370 data->operation.orientation = 0x3;
371 data->operation.operation = 0x16;
372 break;
373 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
374 data->operation.orientation = 0x1;
375 data->operation.operation = 0x03;
376 data->count++;
377 break;
378 case DASD_ECKD_CCW_READ_RECORD_ZERO:
379 data->operation.orientation = 0x3;
380 data->operation.operation = 0x16;
381 data->count++;
382 break;
383 case DASD_ECKD_CCW_WRITE:
384 case DASD_ECKD_CCW_WRITE_MT:
385 case DASD_ECKD_CCW_WRITE_KD:
386 case DASD_ECKD_CCW_WRITE_KD_MT:
387 data->auxiliary.length_valid = 0x1;
388 data->length = reclen;
389 data->operation.operation = 0x01;
390 break;
391 case DASD_ECKD_CCW_WRITE_CKD:
392 case DASD_ECKD_CCW_WRITE_CKD_MT:
393 data->auxiliary.length_valid = 0x1;
394 data->length = reclen;
395 data->operation.operation = 0x03;
396 break;
e4dbb0f2
SH
397 case DASD_ECKD_CCW_WRITE_FULL_TRACK:
398 data->operation.orientation = 0x0;
399 data->operation.operation = 0x3F;
400 data->extended_operation = 0x11;
401 data->length = 0;
402 data->extended_parameter_length = 0x02;
403 if (data->count > 8) {
404 data->extended_parameter[0] = 0xFF;
405 data->extended_parameter[1] = 0xFF;
406 data->extended_parameter[1] <<= (16 - count);
407 } else {
408 data->extended_parameter[0] = 0xFF;
409 data->extended_parameter[0] <<= (8 - count);
410 data->extended_parameter[1] = 0x00;
411 }
412 data->sector = 0xFF;
413 break;
f3eb5384
SW
414 case DASD_ECKD_CCW_WRITE_TRACK_DATA:
415 data->auxiliary.length_valid = 0x1;
416 data->length = reclen; /* not tlf, as one might think */
417 data->operation.operation = 0x3F;
418 data->extended_operation = 0x23;
419 break;
420 case DASD_ECKD_CCW_READ:
421 case DASD_ECKD_CCW_READ_MT:
422 case DASD_ECKD_CCW_READ_KD:
423 case DASD_ECKD_CCW_READ_KD_MT:
424 data->auxiliary.length_valid = 0x1;
425 data->length = reclen;
426 data->operation.operation = 0x06;
427 break;
428 case DASD_ECKD_CCW_READ_CKD:
429 case DASD_ECKD_CCW_READ_CKD_MT:
430 data->auxiliary.length_valid = 0x1;
431 data->length = reclen;
432 data->operation.operation = 0x16;
433 break;
434 case DASD_ECKD_CCW_READ_COUNT:
435 data->operation.operation = 0x06;
436 break;
e4dbb0f2
SH
437 case DASD_ECKD_CCW_READ_TRACK:
438 data->operation.orientation = 0x1;
439 data->operation.operation = 0x0C;
440 data->extended_parameter_length = 0;
441 data->sector = 0xFF;
442 break;
f3eb5384
SW
443 case DASD_ECKD_CCW_READ_TRACK_DATA:
444 data->auxiliary.length_valid = 0x1;
445 data->length = tlf;
446 data->operation.operation = 0x0C;
447 break;
448 case DASD_ECKD_CCW_ERASE:
449 data->length = reclen;
450 data->auxiliary.length_valid = 0x1;
451 data->operation.operation = 0x0b;
452 break;
453 default:
454 DBF_DEV_EVENT(DBF_ERR, device,
455 "fill LRE unknown opcode 0x%x", cmd);
456 BUG();
457 }
458 set_ch_t(&data->seek_addr,
459 trk / private->rdc_data.trk_per_cyl,
460 trk % private->rdc_data.trk_per_cyl);
461 data->search_arg.cyl = data->seek_addr.cyl;
462 data->search_arg.head = data->seek_addr.head;
463 data->search_arg.record = rec_on_trk;
464}
465
466static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
467 unsigned int trk, unsigned int totrk, int cmd,
468 struct dasd_device *basedev, struct dasd_device *startdev,
469 unsigned char format, unsigned int rec_on_trk, int count,
470 unsigned int blksize, unsigned int tlf)
8e09f215
SW
471{
472 struct dasd_eckd_private *basepriv, *startpriv;
f3eb5384
SW
473 struct DE_eckd_data *dedata;
474 struct LRE_eckd_data *lredata;
b44b0ab3
SW
475 u32 begcyl, endcyl;
476 u16 heads, beghead, endhead;
8e09f215
SW
477 int rc = 0;
478
479 basepriv = (struct dasd_eckd_private *) basedev->private;
480 startpriv = (struct dasd_eckd_private *) startdev->private;
f3eb5384
SW
481 dedata = &pfxdata->define_extent;
482 lredata = &pfxdata->locate_record;
8e09f215
SW
483
484 ccw->cmd_code = DASD_ECKD_CCW_PFX;
485 ccw->flags = 0;
e4dbb0f2
SH
486 if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK) {
487 ccw->count = sizeof(*pfxdata) + 2;
488 ccw->cda = (__u32) __pa(pfxdata);
489 memset(pfxdata, 0, sizeof(*pfxdata) + 2);
490 } else {
491 ccw->count = sizeof(*pfxdata);
492 ccw->cda = (__u32) __pa(pfxdata);
493 memset(pfxdata, 0, sizeof(*pfxdata));
494 }
8e09f215 495
8e09f215 496 /* prefix data */
f3eb5384
SW
497 if (format > 1) {
498 DBF_DEV_EVENT(DBF_ERR, basedev,
499 "PFX LRE unknown format 0x%x", format);
500 BUG();
501 return -EINVAL;
502 }
503 pfxdata->format = format;
4abb08c2
SW
504 pfxdata->base_address = basepriv->ned->unit_addr;
505 pfxdata->base_lss = basepriv->ned->ID;
f3eb5384 506 pfxdata->validity.define_extent = 1;
8e09f215
SW
507
508 /* private uid is kept up to date, conf_data may be outdated */
509 if (startpriv->uid.type != UA_BASE_DEVICE) {
510 pfxdata->validity.verify_base = 1;
511 if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
512 pfxdata->validity.hyper_pav = 1;
513 }
514
515 /* define extend data (mostly)*/
516 switch (cmd) {
517 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
518 case DASD_ECKD_CCW_READ_RECORD_ZERO:
519 case DASD_ECKD_CCW_READ:
520 case DASD_ECKD_CCW_READ_MT:
521 case DASD_ECKD_CCW_READ_CKD:
522 case DASD_ECKD_CCW_READ_CKD_MT:
523 case DASD_ECKD_CCW_READ_KD:
524 case DASD_ECKD_CCW_READ_KD_MT:
525 case DASD_ECKD_CCW_READ_COUNT:
f3eb5384
SW
526 dedata->mask.perm = 0x1;
527 dedata->attributes.operation = basepriv->attrib.operation;
528 break;
e4dbb0f2 529 case DASD_ECKD_CCW_READ_TRACK:
f3eb5384
SW
530 case DASD_ECKD_CCW_READ_TRACK_DATA:
531 dedata->mask.perm = 0x1;
532 dedata->attributes.operation = basepriv->attrib.operation;
533 dedata->blk_size = 0;
8e09f215
SW
534 break;
535 case DASD_ECKD_CCW_WRITE:
536 case DASD_ECKD_CCW_WRITE_MT:
537 case DASD_ECKD_CCW_WRITE_KD:
538 case DASD_ECKD_CCW_WRITE_KD_MT:
f3eb5384
SW
539 dedata->mask.perm = 0x02;
540 dedata->attributes.operation = basepriv->attrib.operation;
8e09f215
SW
541 rc = check_XRC_on_prefix(pfxdata, basedev);
542 break;
543 case DASD_ECKD_CCW_WRITE_CKD:
544 case DASD_ECKD_CCW_WRITE_CKD_MT:
f3eb5384 545 dedata->attributes.operation = DASD_BYPASS_CACHE;
8e09f215
SW
546 rc = check_XRC_on_prefix(pfxdata, basedev);
547 break;
548 case DASD_ECKD_CCW_ERASE:
549 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
550 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
f3eb5384
SW
551 dedata->mask.perm = 0x3;
552 dedata->mask.auth = 0x1;
553 dedata->attributes.operation = DASD_BYPASS_CACHE;
8e09f215
SW
554 rc = check_XRC_on_prefix(pfxdata, basedev);
555 break;
e4dbb0f2
SH
556 case DASD_ECKD_CCW_WRITE_FULL_TRACK:
557 dedata->mask.perm = 0x03;
558 dedata->attributes.operation = basepriv->attrib.operation;
559 dedata->blk_size = 0;
560 break;
f3eb5384
SW
561 case DASD_ECKD_CCW_WRITE_TRACK_DATA:
562 dedata->mask.perm = 0x02;
563 dedata->attributes.operation = basepriv->attrib.operation;
564 dedata->blk_size = blksize;
565 rc = check_XRC_on_prefix(pfxdata, basedev);
8e09f215 566 break;
f3eb5384
SW
567 default:
568 DBF_DEV_EVENT(DBF_ERR, basedev,
569 "PFX LRE unknown opcode 0x%x", cmd);
570 BUG();
571 return -EINVAL;
8e09f215
SW
572 }
573
f3eb5384 574 dedata->attributes.mode = 0x3; /* ECKD */
8e09f215
SW
575
576 if ((basepriv->rdc_data.cu_type == 0x2105 ||
577 basepriv->rdc_data.cu_type == 0x2107 ||
578 basepriv->rdc_data.cu_type == 0x1750)
579 && !(basepriv->uses_cdl && trk < 2))
f3eb5384 580 dedata->ga_extended |= 0x40; /* Regular Data Format Mode */
8e09f215 581
b44b0ab3
SW
582 heads = basepriv->rdc_data.trk_per_cyl;
583 begcyl = trk / heads;
584 beghead = trk % heads;
585 endcyl = totrk / heads;
586 endhead = totrk % heads;
8e09f215
SW
587
588 /* check for sequential prestage - enhance cylinder range */
f3eb5384
SW
589 if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
590 dedata->attributes.operation == DASD_SEQ_ACCESS) {
8e09f215 591
b44b0ab3
SW
592 if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
593 endcyl += basepriv->attrib.nr_cyl;
8e09f215 594 else
b44b0ab3 595 endcyl = (basepriv->real_cyl - 1);
8e09f215
SW
596 }
597
f3eb5384
SW
598 set_ch_t(&dedata->beg_ext, begcyl, beghead);
599 set_ch_t(&dedata->end_ext, endcyl, endhead);
600
601 if (format == 1) {
602 fill_LRE_data(lredata, trk, rec_on_trk, count, cmd,
603 basedev, blksize, tlf);
604 }
605
8e09f215
SW
606 return rc;
607}
608
f3eb5384
SW
609static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
610 unsigned int trk, unsigned int totrk, int cmd,
611 struct dasd_device *basedev, struct dasd_device *startdev)
612{
613 return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
614 0, 0, 0, 0, 0);
615}
616
4d284cac 617static void
b44b0ab3
SW
618locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
619 unsigned int rec_on_trk, int no_rec, int cmd,
1da177e4
LT
620 struct dasd_device * device, int reclen)
621{
622 struct dasd_eckd_private *private;
623 int sector;
624 int dn, d;
138c014d 625
1da177e4
LT
626 private = (struct dasd_eckd_private *) device->private;
627
628 DBF_DEV_EVENT(DBF_INFO, device,
629 "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
630 trk, rec_on_trk, no_rec, cmd, reclen);
631
632 ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
633 ccw->flags = 0;
634 ccw->count = 16;
635 ccw->cda = (__u32) __pa(data);
636
8e09f215 637 memset(data, 0, sizeof(struct LO_eckd_data));
1da177e4
LT
638 sector = 0;
639 if (rec_on_trk) {
640 switch (private->rdc_data.dev_type) {
641 case 0x3390:
642 dn = ceil_quot(reclen + 6, 232);
643 d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
644 sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
645 break;
646 case 0x3380:
647 d = 7 + ceil_quot(reclen + 12, 32);
648 sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
649 break;
650 }
651 }
652 data->sector = sector;
653 data->count = no_rec;
654 switch (cmd) {
655 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
656 data->operation.orientation = 0x3;
657 data->operation.operation = 0x03;
658 break;
659 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
660 data->operation.orientation = 0x3;
661 data->operation.operation = 0x16;
662 break;
663 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
664 data->operation.orientation = 0x1;
665 data->operation.operation = 0x03;
666 data->count++;
667 break;
668 case DASD_ECKD_CCW_READ_RECORD_ZERO:
669 data->operation.orientation = 0x3;
670 data->operation.operation = 0x16;
671 data->count++;
672 break;
673 case DASD_ECKD_CCW_WRITE:
674 case DASD_ECKD_CCW_WRITE_MT:
675 case DASD_ECKD_CCW_WRITE_KD:
676 case DASD_ECKD_CCW_WRITE_KD_MT:
677 data->auxiliary.last_bytes_used = 0x1;
678 data->length = reclen;
679 data->operation.operation = 0x01;
680 break;
681 case DASD_ECKD_CCW_WRITE_CKD:
682 case DASD_ECKD_CCW_WRITE_CKD_MT:
683 data->auxiliary.last_bytes_used = 0x1;
684 data->length = reclen;
685 data->operation.operation = 0x03;
686 break;
687 case DASD_ECKD_CCW_READ:
688 case DASD_ECKD_CCW_READ_MT:
689 case DASD_ECKD_CCW_READ_KD:
690 case DASD_ECKD_CCW_READ_KD_MT:
691 data->auxiliary.last_bytes_used = 0x1;
692 data->length = reclen;
693 data->operation.operation = 0x06;
694 break;
695 case DASD_ECKD_CCW_READ_CKD:
696 case DASD_ECKD_CCW_READ_CKD_MT:
697 data->auxiliary.last_bytes_used = 0x1;
698 data->length = reclen;
699 data->operation.operation = 0x16;
700 break;
701 case DASD_ECKD_CCW_READ_COUNT:
702 data->operation.operation = 0x06;
703 break;
704 case DASD_ECKD_CCW_ERASE:
705 data->length = reclen;
706 data->auxiliary.last_bytes_used = 0x1;
707 data->operation.operation = 0x0b;
708 break;
709 default:
fc19f381
SH
710 DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
711 "opcode 0x%x", cmd);
1da177e4 712 }
b44b0ab3
SW
713 set_ch_t(&data->seek_addr,
714 trk / private->rdc_data.trk_per_cyl,
715 trk % private->rdc_data.trk_per_cyl);
716 data->search_arg.cyl = data->seek_addr.cyl;
717 data->search_arg.head = data->seek_addr.head;
1da177e4
LT
718 data->search_arg.record = rec_on_trk;
719}
720
721/*
722 * Returns 1 if the block is one of the special blocks that needs
723 * to get read/written with the KD variant of the command.
724 * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
725 * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
726 * Luckily the KD variants differ only by one bit (0x08) from the
727 * normal variant. So don't wonder about code like:
728 * if (dasd_eckd_cdl_special(blk_per_trk, recid))
729 * ccw->cmd_code |= 0x8;
730 */
731static inline int
732dasd_eckd_cdl_special(int blk_per_trk, int recid)
733{
734 if (recid < 3)
735 return 1;
736 if (recid < blk_per_trk)
737 return 0;
738 if (recid < 2 * blk_per_trk)
739 return 1;
740 return 0;
741}
742
743/*
744 * Returns the record size for the special blocks of the cdl format.
745 * Only returns something useful if dasd_eckd_cdl_special is true
746 * for the recid.
747 */
748static inline int
749dasd_eckd_cdl_reclen(int recid)
750{
751 if (recid < 3)
752 return sizes_trk0[recid];
753 return LABEL_SIZE;
754}
755
3d052595
HH
756/*
757 * Generate device unique id that specifies the physical device.
758 */
2dedf0d9 759static int dasd_eckd_generate_uid(struct dasd_device *device)
3d052595
HH
760{
761 struct dasd_eckd_private *private;
2dedf0d9 762 struct dasd_uid *uid;
4abb08c2 763 int count;
2dedf0d9 764 unsigned long flags;
3d052595
HH
765
766 private = (struct dasd_eckd_private *) device->private;
767 if (!private)
768 return -ENODEV;
4abb08c2 769 if (!private->ned || !private->gneq)
3d052595 770 return -ENODEV;
2dedf0d9
SH
771 uid = &private->uid;
772 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
3d052595 773 memset(uid, 0, sizeof(struct dasd_uid));
4abb08c2 774 memcpy(uid->vendor, private->ned->HDA_manufacturer,
d0710c7c 775 sizeof(uid->vendor) - 1);
3d052595 776 EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
4abb08c2 777 memcpy(uid->serial, private->ned->HDA_location,
d0710c7c 778 sizeof(uid->serial) - 1);
3d052595 779 EBCASC(uid->serial, sizeof(uid->serial) - 1);
4abb08c2 780 uid->ssid = private->gneq->subsystemID;
a419aef8 781 uid->real_unit_addr = private->ned->unit_addr;
4abb08c2
SW
782 if (private->sneq) {
783 uid->type = private->sneq->sua_flags;
8e09f215 784 if (uid->type == UA_BASE_PAV_ALIAS)
4abb08c2 785 uid->base_unit_addr = private->sneq->base_unit_addr;
8e09f215
SW
786 } else {
787 uid->type = UA_BASE_DEVICE;
788 }
4abb08c2
SW
789 if (private->vdsneq) {
790 for (count = 0; count < 16; count++) {
791 sprintf(uid->vduit+2*count, "%02x",
792 private->vdsneq->uit[count]);
793 }
794 }
2dedf0d9 795 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
3d052595
HH
796 return 0;
797}
798
2dedf0d9
SH
799static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
800{
801 struct dasd_eckd_private *private;
802 unsigned long flags;
803
804 if (device->private) {
805 private = (struct dasd_eckd_private *)device->private;
806 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
807 *uid = private->uid;
808 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
809 return 0;
810 }
811 return -EINVAL;
812}
813
a4d26c6a
SW
814static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
815 struct dasd_ccw_req *cqr,
816 __u8 *rcd_buffer,
817 __u8 lpm)
17283b56 818{
17283b56 819 struct ccw1 *ccw;
a4d26c6a
SW
820 /*
821 * buffer has to start with EBCDIC "V1.0" to show
822 * support for virtual device SNEQ
823 */
824 rcd_buffer[0] = 0xE5;
825 rcd_buffer[1] = 0xF1;
826 rcd_buffer[2] = 0x4B;
827 rcd_buffer[3] = 0xF0;
17283b56
CH
828
829 ccw = cqr->cpaddr;
a4d26c6a
SW
830 ccw->cmd_code = DASD_ECKD_CCW_RCD;
831 ccw->flags = 0;
17283b56 832 ccw->cda = (__u32)(addr_t)rcd_buffer;
a4d26c6a
SW
833 ccw->count = DASD_ECKD_RCD_DATA_SIZE;
834 cqr->magic = DASD_ECKD_MAGIC;
17283b56 835
8e09f215
SW
836 cqr->startdev = device;
837 cqr->memdev = device;
838 cqr->block = NULL;
17283b56
CH
839 cqr->expires = 10*HZ;
840 cqr->lpm = lpm;
eb6e199b 841 cqr->retries = 256;
17283b56
CH
842 cqr->buildclk = get_clock();
843 cqr->status = DASD_CQR_FILLED;
a4d26c6a
SW
844 set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
845}
846
847static int dasd_eckd_read_conf_immediately(struct dasd_device *device,
848 struct dasd_ccw_req *cqr,
849 __u8 *rcd_buffer,
850 __u8 lpm)
851{
852 struct ciw *ciw;
853 int rc;
854 /*
855 * sanity check: scan for RCD command in extended SenseID data
856 * some devices do not support RCD
857 */
858 ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
859 if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD)
860 return -EOPNOTSUPP;
861
862 dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buffer, lpm);
863 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5a27e60d 864 set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
a4d26c6a
SW
865 cqr->retries = 5;
866 rc = dasd_sleep_on_immediatly(cqr);
867 return rc;
17283b56
CH
868}
869
870static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
871 void **rcd_buffer,
872 int *rcd_buffer_size, __u8 lpm)
873{
874 struct ciw *ciw;
875 char *rcd_buf = NULL;
876 int ret;
877 struct dasd_ccw_req *cqr;
878
879 /*
a4d26c6a
SW
880 * sanity check: scan for RCD command in extended SenseID data
881 * some devices do not support RCD
17283b56
CH
882 */
883 ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
a4d26c6a 884 if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
17283b56
CH
885 ret = -EOPNOTSUPP;
886 goto out_error;
887 }
a4d26c6a 888 rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
17283b56
CH
889 if (!rcd_buf) {
890 ret = -ENOMEM;
891 goto out_error;
892 }
a4d26c6a
SW
893 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
894 0, /* use rcd_buf as data ara */
895 device);
17283b56 896 if (IS_ERR(cqr)) {
a4d26c6a
SW
897 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
898 "Could not allocate RCD request");
899 ret = -ENOMEM;
17283b56
CH
900 goto out_error;
901 }
a4d26c6a 902 dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
17283b56
CH
903 ret = dasd_sleep_on(cqr);
904 /*
905 * on success we update the user input parms
906 */
8e09f215 907 dasd_sfree_request(cqr, cqr->memdev);
17283b56
CH
908 if (ret)
909 goto out_error;
910
a4d26c6a 911 *rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
17283b56
CH
912 *rcd_buffer = rcd_buf;
913 return 0;
914out_error:
915 kfree(rcd_buf);
916 *rcd_buffer = NULL;
917 *rcd_buffer_size = 0;
918 return ret;
919}
920
4abb08c2
SW
921static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
922{
923
924 struct dasd_sneq *sneq;
925 int i, count;
926
927 private->ned = NULL;
928 private->sneq = NULL;
929 private->vdsneq = NULL;
930 private->gneq = NULL;
931 count = private->conf_len / sizeof(struct dasd_sneq);
932 sneq = (struct dasd_sneq *)private->conf_data;
933 for (i = 0; i < count; ++i) {
934 if (sneq->flags.identifier == 1 && sneq->format == 1)
935 private->sneq = sneq;
936 else if (sneq->flags.identifier == 1 && sneq->format == 4)
937 private->vdsneq = (struct vd_sneq *)sneq;
938 else if (sneq->flags.identifier == 2)
939 private->gneq = (struct dasd_gneq *)sneq;
940 else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
941 private->ned = (struct dasd_ned *)sneq;
942 sneq++;
943 }
944 if (!private->ned || !private->gneq) {
945 private->ned = NULL;
946 private->sneq = NULL;
947 private->vdsneq = NULL;
948 private->gneq = NULL;
949 return -EINVAL;
950 }
951 return 0;
952
953};
954
955static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
956{
957 struct dasd_gneq *gneq;
958 int i, count, found;
959
960 count = conf_len / sizeof(*gneq);
961 gneq = (struct dasd_gneq *)conf_data;
962 found = 0;
963 for (i = 0; i < count; ++i) {
964 if (gneq->flags.identifier == 2) {
965 found = 1;
966 break;
967 }
968 gneq++;
969 }
970 if (found)
971 return ((char *)gneq)[18] & 0x07;
972 else
973 return 0;
974}
975
976static int dasd_eckd_read_conf(struct dasd_device *device)
1da177e4
LT
977{
978 void *conf_data;
979 int conf_len, conf_data_saved;
980 int rc;
a4d26c6a 981 __u8 lpm, opm;
1da177e4 982 struct dasd_eckd_private *private;
a4d26c6a 983 struct dasd_path *path_data;
1da177e4
LT
984
985 private = (struct dasd_eckd_private *) device->private;
a4d26c6a
SW
986 path_data = &device->path_data;
987 opm = ccw_device_get_path_mask(device->cdev);
1da177e4
LT
988 lpm = 0x80;
989 conf_data_saved = 0;
1da177e4
LT
990 /* get configuration data per operational path */
991 for (lpm = 0x80; lpm; lpm>>= 1) {
a4d26c6a 992 if (lpm & opm) {
17283b56
CH
993 rc = dasd_eckd_read_conf_lpm(device, &conf_data,
994 &conf_len, lpm);
1da177e4 995 if (rc && rc != -EOPNOTSUPP) { /* -EOPNOTSUPP is ok */
b8ed5dd5 996 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
fc19f381 997 "Read configuration data returned "
b8ed5dd5 998 "error %d", rc);
1da177e4
LT
999 return rc;
1000 }
1001 if (conf_data == NULL) {
b8ed5dd5
SH
1002 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1003 "No configuration data "
1004 "retrieved");
a4d26c6a
SW
1005 /* no further analysis possible */
1006 path_data->opm |= lpm;
d133a960 1007 continue; /* no error */
1da177e4 1008 }
1da177e4 1009 /* save first valid configuration data */
4abb08c2
SW
1010 if (!conf_data_saved) {
1011 kfree(private->conf_data);
1012 private->conf_data = conf_data;
1013 private->conf_len = conf_len;
1014 if (dasd_eckd_identify_conf_parts(private)) {
1015 private->conf_data = NULL;
1016 private->conf_len = 0;
1017 kfree(conf_data);
1018 continue;
1019 }
1da177e4
LT
1020 conf_data_saved++;
1021 }
4abb08c2 1022 switch (dasd_eckd_path_access(conf_data, conf_len)) {
1da177e4
LT
1023 case 0x02:
1024 path_data->npm |= lpm;
1025 break;
1026 case 0x03:
1027 path_data->ppm |= lpm;
1028 break;
1029 }
a4d26c6a 1030 path_data->opm |= lpm;
4abb08c2
SW
1031 if (conf_data != private->conf_data)
1032 kfree(conf_data);
1da177e4
LT
1033 }
1034 }
1035 return 0;
1036}
1037
a4d26c6a
SW
1038static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
1039{
1040 struct dasd_eckd_private *private;
1041 int mdc;
1042 u32 fcx_max_data;
1043
1044 private = (struct dasd_eckd_private *) device->private;
1045 if (private->fcx_max_data) {
1046 mdc = ccw_device_get_mdc(device->cdev, lpm);
1047 if ((mdc < 0)) {
1048 dev_warn(&device->cdev->dev,
1049 "Detecting the maximum data size for zHPF "
1050 "requests failed (rc=%d) for a new path %x\n",
1051 mdc, lpm);
1052 return mdc;
1053 }
1054 fcx_max_data = mdc * FCX_MAX_DATA_FACTOR;
1055 if (fcx_max_data < private->fcx_max_data) {
1056 dev_warn(&device->cdev->dev,
1057 "The maximum data size for zHPF requests %u "
1058 "on a new path %x is below the active maximum "
1059 "%u\n", fcx_max_data, lpm,
1060 private->fcx_max_data);
1061 return -EACCES;
1062 }
1063 }
1064 return 0;
1065}
1066
1067static void do_path_verification_work(struct work_struct *work)
1068{
1069 struct path_verification_work_data *data;
1070 struct dasd_device *device;
1071 __u8 lpm, opm, npm, ppm, epm;
1072 unsigned long flags;
1073 int rc;
1074
1075 data = container_of(work, struct path_verification_work_data, worker);
1076 device = data->device;
1077
1078 opm = 0;
1079 npm = 0;
1080 ppm = 0;
1081 epm = 0;
1082 for (lpm = 0x80; lpm; lpm >>= 1) {
1083 if (lpm & data->tbvpm) {
1084 memset(data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1085 memset(&data->cqr, 0, sizeof(data->cqr));
1086 data->cqr.cpaddr = &data->ccw;
1087 rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1088 data->rcd_buffer,
1089 lpm);
1090 if (!rc) {
1091 switch (dasd_eckd_path_access(data->rcd_buffer,
1092 DASD_ECKD_RCD_DATA_SIZE)) {
1093 case 0x02:
1094 npm |= lpm;
1095 break;
1096 case 0x03:
1097 ppm |= lpm;
1098 break;
1099 }
1100 opm |= lpm;
1101 } else if (rc == -EOPNOTSUPP) {
1102 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1103 "path verification: No configuration "
1104 "data retrieved");
1105 opm |= lpm;
1106 } else if (rc == -EAGAIN) {
1107 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1108 "path verification: device is stopped,"
1109 " try again later");
1110 epm |= lpm;
1111 } else {
1112 dev_warn(&device->cdev->dev,
1113 "Reading device feature codes failed "
1114 "(rc=%d) for new path %x\n", rc, lpm);
1115 continue;
1116 }
1117 if (verify_fcx_max_data(device, lpm)) {
1118 opm &= ~lpm;
1119 npm &= ~lpm;
1120 ppm &= ~lpm;
1121 }
1122 }
1123 }
1124 /*
1125 * There is a small chance that a path is lost again between
1126 * above path verification and the following modification of
1127 * the device opm mask. We could avoid that race here by using
1128 * yet another path mask, but we rather deal with this unlikely
1129 * situation in dasd_start_IO.
1130 */
1131 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1132 if (!device->path_data.opm && opm) {
1133 device->path_data.opm = opm;
1134 dasd_generic_path_operational(device);
1135 } else
1136 device->path_data.opm |= opm;
1137 device->path_data.npm |= npm;
1138 device->path_data.ppm |= ppm;
1139 device->path_data.tbvpm |= epm;
1140 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1141
1142 dasd_put_device(device);
1143 if (data->isglobal)
1144 mutex_unlock(&dasd_path_verification_mutex);
1145 else
1146 kfree(data);
1147}
1148
1149static int dasd_eckd_verify_path(struct dasd_device *device, __u8 lpm)
1150{
1151 struct path_verification_work_data *data;
1152
1153 data = kmalloc(sizeof(*data), GFP_ATOMIC | GFP_DMA);
1154 if (!data) {
1155 if (mutex_trylock(&dasd_path_verification_mutex)) {
1156 data = path_verification_worker;
1157 data->isglobal = 1;
1158 } else
1159 return -ENOMEM;
1160 } else {
1161 memset(data, 0, sizeof(*data));
1162 data->isglobal = 0;
1163 }
1164 INIT_WORK(&data->worker, do_path_verification_work);
1165 dasd_get_device(device);
1166 data->device = device;
1167 data->tbvpm = lpm;
1168 schedule_work(&data->worker);
1169 return 0;
1170}
1171
8e09f215
SW
1172static int dasd_eckd_read_features(struct dasd_device *device)
1173{
1174 struct dasd_psf_prssd_data *prssdp;
1175 struct dasd_rssd_features *features;
1176 struct dasd_ccw_req *cqr;
1177 struct ccw1 *ccw;
1178 int rc;
1179 struct dasd_eckd_private *private;
1180
1181 private = (struct dasd_eckd_private *) device->private;
68d1e5f0 1182 memset(&private->features, 0, sizeof(struct dasd_rssd_features));
68b781fe 1183 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
8e09f215
SW
1184 (sizeof(struct dasd_psf_prssd_data) +
1185 sizeof(struct dasd_rssd_features)),
1186 device);
1187 if (IS_ERR(cqr)) {
b8ed5dd5
SH
1188 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
1189 "allocate initialization request");
8e09f215
SW
1190 return PTR_ERR(cqr);
1191 }
1192 cqr->startdev = device;
1193 cqr->memdev = device;
1194 cqr->block = NULL;
eb6e199b 1195 cqr->retries = 256;
8e09f215
SW
1196 cqr->expires = 10 * HZ;
1197
1198 /* Prepare for Read Subsystem Data */
1199 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1200 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1201 prssdp->order = PSF_ORDER_PRSSD;
1202 prssdp->suborder = 0x41; /* Read Feature Codes */
1203 /* all other bytes of prssdp must be zero */
1204
1205 ccw = cqr->cpaddr;
1206 ccw->cmd_code = DASD_ECKD_CCW_PSF;
1207 ccw->count = sizeof(struct dasd_psf_prssd_data);
1208 ccw->flags |= CCW_FLAG_CC;
1209 ccw->cda = (__u32)(addr_t) prssdp;
1210
1211 /* Read Subsystem Data - feature codes */
1212 features = (struct dasd_rssd_features *) (prssdp + 1);
1213 memset(features, 0, sizeof(struct dasd_rssd_features));
1214
1215 ccw++;
1216 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1217 ccw->count = sizeof(struct dasd_rssd_features);
1218 ccw->cda = (__u32)(addr_t) features;
1219
1220 cqr->buildclk = get_clock();
1221 cqr->status = DASD_CQR_FILLED;
1222 rc = dasd_sleep_on(cqr);
1223 if (rc == 0) {
1224 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1225 features = (struct dasd_rssd_features *) (prssdp + 1);
1226 memcpy(&private->features, features,
1227 sizeof(struct dasd_rssd_features));
68d1e5f0
SW
1228 } else
1229 dev_warn(&device->cdev->dev, "Reading device feature codes"
1230 " failed with rc=%d\n", rc);
8e09f215
SW
1231 dasd_sfree_request(cqr, cqr->memdev);
1232 return rc;
1233}
1234
1235
40545573
HH
1236/*
1237 * Build CP for Perform Subsystem Function - SSC.
1238 */
f3eb5384
SW
1239static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
1240 int enable_pav)
40545573 1241{
8e09f215
SW
1242 struct dasd_ccw_req *cqr;
1243 struct dasd_psf_ssc_data *psf_ssc_data;
1244 struct ccw1 *ccw;
40545573 1245
68b781fe 1246 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
40545573
HH
1247 sizeof(struct dasd_psf_ssc_data),
1248 device);
1249
8e09f215 1250 if (IS_ERR(cqr)) {
fc19f381 1251 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
40545573 1252 "Could not allocate PSF-SSC request");
8e09f215
SW
1253 return cqr;
1254 }
1255 psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
1256 psf_ssc_data->order = PSF_ORDER_SSC;
626350b6 1257 psf_ssc_data->suborder = 0xc0;
f3eb5384 1258 if (enable_pav) {
626350b6 1259 psf_ssc_data->suborder |= 0x08;
f3eb5384
SW
1260 psf_ssc_data->reserved[0] = 0x88;
1261 }
8e09f215
SW
1262 ccw = cqr->cpaddr;
1263 ccw->cmd_code = DASD_ECKD_CCW_PSF;
1264 ccw->cda = (__u32)(addr_t)psf_ssc_data;
1265 ccw->count = 66;
1266
1267 cqr->startdev = device;
1268 cqr->memdev = device;
1269 cqr->block = NULL;
eb6e199b 1270 cqr->retries = 256;
8e09f215
SW
1271 cqr->expires = 10*HZ;
1272 cqr->buildclk = get_clock();
1273 cqr->status = DASD_CQR_FILLED;
1274 return cqr;
40545573
HH
1275}
1276
1277/*
1278 * Perform Subsystem Function.
1279 * It is necessary to trigger CIO for channel revalidation since this
1280 * call might change behaviour of DASD devices.
1281 */
1282static int
f3eb5384 1283dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav)
40545573 1284{
8e09f215
SW
1285 struct dasd_ccw_req *cqr;
1286 int rc;
1287
f3eb5384 1288 cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
8e09f215
SW
1289 if (IS_ERR(cqr))
1290 return PTR_ERR(cqr);
1291
1292 rc = dasd_sleep_on(cqr);
1293 if (!rc)
1294 /* trigger CIO to reprobe devices */
1295 css_schedule_reprobe();
1296 dasd_sfree_request(cqr, cqr->memdev);
1297 return rc;
40545573
HH
1298}
1299
1300/*
1301 * Valide storage server of current device.
1302 */
f4ac1d02 1303static void dasd_eckd_validate_server(struct dasd_device *device)
40545573
HH
1304{
1305 int rc;
8e09f215 1306 struct dasd_eckd_private *private;
f3eb5384 1307 int enable_pav;
40545573 1308
40545573 1309 if (dasd_nopav || MACHINE_IS_VM)
f3eb5384
SW
1310 enable_pav = 0;
1311 else
1312 enable_pav = 1;
1313 rc = dasd_eckd_psf_ssc(device, enable_pav);
eb6e199b 1314
8e79a441
HH
1315 /* may be requested feature is not available on server,
1316 * therefore just report error and go ahead */
8e09f215 1317 private = (struct dasd_eckd_private *) device->private;
b8ed5dd5
SH
1318 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
1319 "returned rc=%d", private->uid.ssid, rc);
40545573
HH
1320}
1321
ef19298b
SW
1322static u32 get_fcx_max_data(struct dasd_device *device)
1323{
1324#if defined(CONFIG_64BIT)
1325 int tpm, mdc;
1326 int fcx_in_css, fcx_in_gneq, fcx_in_features;
1327 struct dasd_eckd_private *private;
1328
1329 if (dasd_nofcx)
1330 return 0;
1331 /* is transport mode supported? */
1332 private = (struct dasd_eckd_private *) device->private;
1333 fcx_in_css = css_general_characteristics.fcx;
1334 fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
1335 fcx_in_features = private->features.feature[40] & 0x80;
1336 tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;
1337
1338 if (!tpm)
1339 return 0;
1340
1341 mdc = ccw_device_get_mdc(device->cdev, 0);
1342 if (mdc < 0) {
1343 dev_warn(&device->cdev->dev, "Detecting the maximum supported"
1344 " data size for zHPF requests failed\n");
1345 return 0;
1346 } else
1347 return mdc * FCX_MAX_DATA_FACTOR;
1348#else
1349 return 0;
1350#endif
1351}
1352
3d052595
HH
1353/*
1354 * Check device characteristics.
1355 * If the device is accessible using ECKD discipline, the device is enabled.
1356 */
1da177e4
LT
1357static int
1358dasd_eckd_check_characteristics(struct dasd_device *device)
1359{
1360 struct dasd_eckd_private *private;
8e09f215 1361 struct dasd_block *block;
2dedf0d9 1362 struct dasd_uid temp_uid;
7c8faa86 1363 int is_known, rc, i;
33b62a30 1364 int readonly;
7c8faa86 1365 unsigned long value;
1da177e4 1366
454e1fa1
PO
1367 if (!ccw_device_is_pathgroup(device->cdev)) {
1368 dev_warn(&device->cdev->dev,
1369 "A channel path group could not be established\n");
1370 return -EIO;
1371 }
1372 if (!ccw_device_is_multipath(device->cdev)) {
1373 dev_info(&device->cdev->dev,
1374 "The DASD is not operating in multipath mode\n");
1375 }
1da177e4 1376 private = (struct dasd_eckd_private *) device->private;
92636b15
SO
1377 if (!private) {
1378 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1379 if (!private) {
fc19f381
SH
1380 dev_warn(&device->cdev->dev,
1381 "Allocating memory for private DASD data "
1382 "failed\n");
1da177e4
LT
1383 return -ENOMEM;
1384 }
1da177e4 1385 device->private = (void *) private;
92636b15
SO
1386 } else {
1387 memset(private, 0, sizeof(*private));
1da177e4
LT
1388 }
1389 /* Invalidate status of initial analysis. */
1390 private->init_cqr_status = -1;
1391 /* Set default cache operations. */
1392 private->attrib.operation = DASD_NORMAL_CACHE;
1393 private->attrib.nr_cyl = 0;
1394
40545573
HH
1395 /* Read Configuration Data */
1396 rc = dasd_eckd_read_conf(device);
1397 if (rc)
8e09f215 1398 goto out_err1;
40545573 1399
7c8faa86
SH
1400 /* set default timeout */
1401 device->default_expires = DASD_EXPIRES;
1402 if (private->gneq) {
1403 value = 1;
1404 for (i = 0; i < private->gneq->timeout.value; i++)
1405 value = 10 * value;
1406 value = value * private->gneq->timeout.number;
1407 /* do not accept useless values */
1408 if (value != 0 && value <= DASD_EXPIRES_MAX)
1409 device->default_expires = value;
1410 }
1411
2dedf0d9
SH
1412 /* Generate device unique id */
1413 rc = dasd_eckd_generate_uid(device);
40545573 1414 if (rc)
8e09f215 1415 goto out_err1;
8e09f215 1416
2dedf0d9
SH
1417 dasd_eckd_get_uid(device, &temp_uid);
1418 if (temp_uid.type == UA_BASE_DEVICE) {
8e09f215
SW
1419 block = dasd_alloc_block();
1420 if (IS_ERR(block)) {
b8ed5dd5
SH
1421 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1422 "could not allocate dasd "
1423 "block structure");
8e09f215
SW
1424 rc = PTR_ERR(block);
1425 goto out_err1;
1426 }
1427 device->block = block;
1428 block->base = device;
1429 }
1430
1431 /* register lcu with alias handling, enable PAV if this is a new lcu */
1432 is_known = dasd_alias_make_device_known_to_lcu(device);
1433 if (is_known < 0) {
1434 rc = is_known;
1435 goto out_err2;
1436 }
f4ac1d02 1437 /*
817f2c84 1438 * dasd_eckd_validate_server is done on the first device that
f4ac1d02
SW
1439 * is found for an LCU. All later other devices have to wait
1440 * for it, so they will read the correct feature codes.
1441 */
8e09f215 1442 if (!is_known) {
f4ac1d02
SW
1443 dasd_eckd_validate_server(device);
1444 dasd_alias_lcu_setup_complete(device);
1445 } else
1446 dasd_alias_wait_for_lcu_setup(device);
1447
1448 /* device may report different configuration data after LCU setup */
1449 rc = dasd_eckd_read_conf(device);
1450 if (rc)
1451 goto out_err3;
8e09f215
SW
1452
1453 /* Read Feature Codes */
68d1e5f0 1454 dasd_eckd_read_features(device);
40545573 1455
1da177e4 1456 /* Read Device Characteristics */
68b781fe
SH
1457 rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
1458 &private->rdc_data, 64);
8e09f215 1459 if (rc) {
b8ed5dd5
SH
1460 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1461 "Read device characteristic failed, rc=%d", rc);
8e09f215
SW
1462 goto out_err3;
1463 }
817f2c84 1464 /* find the valid cylinder size */
b44b0ab3
SW
1465 if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
1466 private->rdc_data.long_no_cyl)
1467 private->real_cyl = private->rdc_data.long_no_cyl;
1468 else
1469 private->real_cyl = private->rdc_data.no_cyl;
1470
ef19298b
SW
1471 private->fcx_max_data = get_fcx_max_data(device);
1472
33b62a30
SW
1473 readonly = dasd_device_is_ro(device);
1474 if (readonly)
1475 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1476
fc19f381 1477 dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
33b62a30 1478 "with %d cylinders, %d heads, %d sectors%s\n",
fc19f381
SH
1479 private->rdc_data.dev_type,
1480 private->rdc_data.dev_model,
1481 private->rdc_data.cu_type,
1482 private->rdc_data.cu_model.model,
92636b15 1483 private->real_cyl,
fc19f381 1484 private->rdc_data.trk_per_cyl,
33b62a30
SW
1485 private->rdc_data.sec_per_trk,
1486 readonly ? ", read-only device" : "");
8e09f215
SW
1487 return 0;
1488
1489out_err3:
1490 dasd_alias_disconnect_device_from_lcu(device);
1491out_err2:
1492 dasd_free_block(device->block);
1493 device->block = NULL;
1494out_err1:
4abb08c2 1495 kfree(private->conf_data);
8e09f215
SW
1496 kfree(device->private);
1497 device->private = NULL;
3d052595 1498 return rc;
1da177e4
LT
1499}
1500
8e09f215
SW
1501static void dasd_eckd_uncheck_device(struct dasd_device *device)
1502{
4abb08c2
SW
1503 struct dasd_eckd_private *private;
1504
1505 private = (struct dasd_eckd_private *) device->private;
8e09f215 1506 dasd_alias_disconnect_device_from_lcu(device);
4abb08c2
SW
1507 private->ned = NULL;
1508 private->sneq = NULL;
1509 private->vdsneq = NULL;
1510 private->gneq = NULL;
1511 private->conf_len = 0;
1512 kfree(private->conf_data);
1513 private->conf_data = NULL;
8e09f215
SW
1514}
1515
1da177e4
LT
1516static struct dasd_ccw_req *
1517dasd_eckd_analysis_ccw(struct dasd_device *device)
1518{
1519 struct dasd_eckd_private *private;
1520 struct eckd_count *count_data;
1521 struct LO_eckd_data *LO_data;
1522 struct dasd_ccw_req *cqr;
1523 struct ccw1 *ccw;
1524 int cplength, datasize;
1525 int i;
1526
1527 private = (struct dasd_eckd_private *) device->private;
1528
1529 cplength = 8;
1530 datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
68b781fe 1531 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1da177e4
LT
1532 if (IS_ERR(cqr))
1533 return cqr;
1534 ccw = cqr->cpaddr;
1535 /* Define extent for the first 3 tracks. */
1536 define_extent(ccw++, cqr->data, 0, 2,
1537 DASD_ECKD_CCW_READ_COUNT, device);
8e09f215 1538 LO_data = cqr->data + sizeof(struct DE_eckd_data);
1da177e4
LT
1539 /* Locate record for the first 4 records on track 0. */
1540 ccw[-1].flags |= CCW_FLAG_CC;
1541 locate_record(ccw++, LO_data++, 0, 0, 4,
1542 DASD_ECKD_CCW_READ_COUNT, device, 0);
1543
1544 count_data = private->count_area;
1545 for (i = 0; i < 4; i++) {
1546 ccw[-1].flags |= CCW_FLAG_CC;
1547 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1548 ccw->flags = 0;
1549 ccw->count = 8;
1550 ccw->cda = (__u32)(addr_t) count_data;
1551 ccw++;
1552 count_data++;
1553 }
1554
1555 /* Locate record for the first record on track 2. */
1556 ccw[-1].flags |= CCW_FLAG_CC;
1557 locate_record(ccw++, LO_data++, 2, 0, 1,
1558 DASD_ECKD_CCW_READ_COUNT, device, 0);
1559 /* Read count ccw. */
1560 ccw[-1].flags |= CCW_FLAG_CC;
1561 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1562 ccw->flags = 0;
1563 ccw->count = 8;
1564 ccw->cda = (__u32)(addr_t) count_data;
1565
8e09f215
SW
1566 cqr->block = NULL;
1567 cqr->startdev = device;
1568 cqr->memdev = device;
eb6e199b 1569 cqr->retries = 255;
1da177e4
LT
1570 cqr->buildclk = get_clock();
1571 cqr->status = DASD_CQR_FILLED;
1572 return cqr;
1573}
1574
eb6e199b
SW
1575/* differentiate between 'no record found' and any other error */
1576static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
1577{
1578 char *sense;
1579 if (init_cqr->status == DASD_CQR_DONE)
1580 return INIT_CQR_OK;
1581 else if (init_cqr->status == DASD_CQR_NEED_ERP ||
1582 init_cqr->status == DASD_CQR_FAILED) {
1583 sense = dasd_get_sense(&init_cqr->irb);
1584 if (sense && (sense[1] & SNS1_NO_REC_FOUND))
1585 return INIT_CQR_UNFORMATTED;
1586 else
1587 return INIT_CQR_ERROR;
1588 } else
1589 return INIT_CQR_ERROR;
1590}
1591
1da177e4
LT
1592/*
1593 * This is the callback function for the init_analysis cqr. It saves
1594 * the status of the initial analysis ccw before it frees it and kicks
1595 * the device to continue the startup sequence. This will call
1596 * dasd_eckd_do_analysis again (if the devices has not been marked
1597 * for deletion in the meantime).
1598 */
eb6e199b
SW
1599static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
1600 void *data)
1da177e4
LT
1601{
1602 struct dasd_eckd_private *private;
1603 struct dasd_device *device;
1604
8e09f215 1605 device = init_cqr->startdev;
1da177e4 1606 private = (struct dasd_eckd_private *) device->private;
eb6e199b 1607 private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
1da177e4
LT
1608 dasd_sfree_request(init_cqr, device);
1609 dasd_kick_device(device);
1610}
1611
eb6e199b 1612static int dasd_eckd_start_analysis(struct dasd_block *block)
1da177e4 1613{
1da177e4
LT
1614 struct dasd_ccw_req *init_cqr;
1615
8e09f215 1616 init_cqr = dasd_eckd_analysis_ccw(block->base);
1da177e4
LT
1617 if (IS_ERR(init_cqr))
1618 return PTR_ERR(init_cqr);
1619 init_cqr->callback = dasd_eckd_analysis_callback;
1620 init_cqr->callback_data = NULL;
1621 init_cqr->expires = 5*HZ;
eb6e199b
SW
1622 /* first try without ERP, so we can later handle unformatted
1623 * devices as special case
1624 */
1625 clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
1626 init_cqr->retries = 0;
1da177e4
LT
1627 dasd_add_request_head(init_cqr);
1628 return -EAGAIN;
1629}
1630
eb6e199b 1631static int dasd_eckd_end_analysis(struct dasd_block *block)
1da177e4 1632{
8e09f215 1633 struct dasd_device *device;
1da177e4
LT
1634 struct dasd_eckd_private *private;
1635 struct eckd_count *count_area;
1636 unsigned int sb, blk_per_trk;
1637 int status, i;
eb6e199b 1638 struct dasd_ccw_req *init_cqr;
1da177e4 1639
8e09f215 1640 device = block->base;
1da177e4
LT
1641 private = (struct dasd_eckd_private *) device->private;
1642 status = private->init_cqr_status;
1643 private->init_cqr_status = -1;
eb6e199b
SW
1644 if (status == INIT_CQR_ERROR) {
1645 /* try again, this time with full ERP */
1646 init_cqr = dasd_eckd_analysis_ccw(device);
1647 dasd_sleep_on(init_cqr);
1648 status = dasd_eckd_analysis_evaluation(init_cqr);
1649 dasd_sfree_request(init_cqr, device);
1650 }
1651
e4dbb0f2
SH
1652 if (device->features & DASD_FEATURE_USERAW) {
1653 block->bp_block = DASD_RAW_BLOCKSIZE;
1654 blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
1655 block->s2b_shift = 3;
1656 goto raw;
1657 }
1658
eb6e199b
SW
1659 if (status == INIT_CQR_UNFORMATTED) {
1660 dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
1da177e4 1661 return -EMEDIUMTYPE;
eb6e199b
SW
1662 } else if (status == INIT_CQR_ERROR) {
1663 dev_err(&device->cdev->dev,
1664 "Detecting the DASD disk layout failed because "
1665 "of an I/O error\n");
1666 return -EIO;
1da177e4
LT
1667 }
1668
1669 private->uses_cdl = 1;
1da177e4
LT
1670 /* Check Track 0 for Compatible Disk Layout */
1671 count_area = NULL;
1672 for (i = 0; i < 3; i++) {
1673 if (private->count_area[i].kl != 4 ||
1674 private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4) {
1675 private->uses_cdl = 0;
1676 break;
1677 }
1678 }
1679 if (i == 3)
1680 count_area = &private->count_area[4];
1681
1682 if (private->uses_cdl == 0) {
1683 for (i = 0; i < 5; i++) {
1684 if ((private->count_area[i].kl != 0) ||
1685 (private->count_area[i].dl !=
1686 private->count_area[0].dl))
1687 break;
1688 }
1689 if (i == 5)
1690 count_area = &private->count_area[0];
1691 } else {
1692 if (private->count_area[3].record == 1)
fc19f381
SH
1693 dev_warn(&device->cdev->dev,
1694 "Track 0 has no records following the VTOC\n");
1da177e4 1695 }
e4dbb0f2 1696
1da177e4
LT
1697 if (count_area != NULL && count_area->kl == 0) {
1698 /* we found notthing violating our disk layout */
1699 if (dasd_check_blocksize(count_area->dl) == 0)
8e09f215 1700 block->bp_block = count_area->dl;
1da177e4 1701 }
8e09f215 1702 if (block->bp_block == 0) {
fc19f381
SH
1703 dev_warn(&device->cdev->dev,
1704 "The disk layout of the DASD is not supported\n");
1da177e4
LT
1705 return -EMEDIUMTYPE;
1706 }
8e09f215
SW
1707 block->s2b_shift = 0; /* bits to shift 512 to get a block */
1708 for (sb = 512; sb < block->bp_block; sb = sb << 1)
1709 block->s2b_shift++;
1da177e4 1710
8e09f215 1711 blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
e4dbb0f2
SH
1712
1713raw:
b44b0ab3 1714 block->blocks = (private->real_cyl *
1da177e4
LT
1715 private->rdc_data.trk_per_cyl *
1716 blk_per_trk);
1717
fc19f381
SH
1718 dev_info(&device->cdev->dev,
1719 "DASD with %d KB/block, %d KB total size, %d KB/track, "
1720 "%s\n", (block->bp_block >> 10),
1721 ((private->real_cyl *
1722 private->rdc_data.trk_per_cyl *
1723 blk_per_trk * (block->bp_block >> 9)) >> 1),
1724 ((blk_per_trk * block->bp_block) >> 10),
1725 private->uses_cdl ?
1726 "compatible disk layout" : "linux disk layout");
1da177e4
LT
1727
1728 return 0;
1729}
1730
8e09f215 1731static int dasd_eckd_do_analysis(struct dasd_block *block)
1da177e4
LT
1732{
1733 struct dasd_eckd_private *private;
1734
8e09f215 1735 private = (struct dasd_eckd_private *) block->base->private;
1da177e4 1736 if (private->init_cqr_status < 0)
8e09f215 1737 return dasd_eckd_start_analysis(block);
1da177e4 1738 else
8e09f215 1739 return dasd_eckd_end_analysis(block);
1da177e4
LT
1740}
1741
8e09f215
SW
1742static int dasd_eckd_ready_to_online(struct dasd_device *device)
1743{
1744 return dasd_alias_add_device(device);
1745};
1746
1747static int dasd_eckd_online_to_ready(struct dasd_device *device)
1748{
501183f2 1749 cancel_work_sync(&device->reload_device);
8e09f215
SW
1750 return dasd_alias_remove_device(device);
1751};
1752
1da177e4 1753static int
8e09f215 1754dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
1da177e4
LT
1755{
1756 struct dasd_eckd_private *private;
1757
8e09f215
SW
1758 private = (struct dasd_eckd_private *) block->base->private;
1759 if (dasd_check_blocksize(block->bp_block) == 0) {
1da177e4 1760 geo->sectors = recs_per_track(&private->rdc_data,
8e09f215 1761 0, block->bp_block);
1da177e4
LT
1762 }
1763 geo->cylinders = private->rdc_data.no_cyl;
1764 geo->heads = private->rdc_data.trk_per_cyl;
1765 return 0;
1766}
1767
1768static struct dasd_ccw_req *
1769dasd_eckd_format_device(struct dasd_device * device,
1770 struct format_data_t * fdata)
1771{
1772 struct dasd_eckd_private *private;
1773 struct dasd_ccw_req *fcp;
1774 struct eckd_count *ect;
1775 struct ccw1 *ccw;
1776 void *data;
b44b0ab3
SW
1777 int rpt;
1778 struct ch_t address;
1da177e4
LT
1779 int cplength, datasize;
1780 int i;
f9a28f7b
JBJ
1781 int intensity = 0;
1782 int r0_perm;
1da177e4
LT
1783
1784 private = (struct dasd_eckd_private *) device->private;
1785 rpt = recs_per_track(&private->rdc_data, 0, fdata->blksize);
b44b0ab3
SW
1786 set_ch_t(&address,
1787 fdata->start_unit / private->rdc_data.trk_per_cyl,
1788 fdata->start_unit % private->rdc_data.trk_per_cyl);
1da177e4
LT
1789
1790 /* Sanity checks. */
1791 if (fdata->start_unit >=
b44b0ab3 1792 (private->real_cyl * private->rdc_data.trk_per_cyl)) {
fc19f381
SH
1793 dev_warn(&device->cdev->dev, "Start track number %d used in "
1794 "formatting is too big\n", fdata->start_unit);
1da177e4
LT
1795 return ERR_PTR(-EINVAL);
1796 }
1797 if (fdata->start_unit > fdata->stop_unit) {
fc19f381
SH
1798 dev_warn(&device->cdev->dev, "Start track %d used in "
1799 "formatting exceeds end track\n", fdata->start_unit);
1da177e4
LT
1800 return ERR_PTR(-EINVAL);
1801 }
1802 if (dasd_check_blocksize(fdata->blksize) != 0) {
fc19f381
SH
1803 dev_warn(&device->cdev->dev,
1804 "The DASD cannot be formatted with block size %d\n",
1805 fdata->blksize);
1da177e4
LT
1806 return ERR_PTR(-EINVAL);
1807 }
1808
1809 /*
1810 * fdata->intensity is a bit string that tells us what to do:
1811 * Bit 0: write record zero
1812 * Bit 1: write home address, currently not supported
1813 * Bit 2: invalidate tracks
1814 * Bit 3: use OS/390 compatible disk layout (cdl)
f9a28f7b 1815 * Bit 4: do not allow storage subsystem to modify record zero
1da177e4
LT
1816 * Only some bit combinations do make sense.
1817 */
f9a28f7b
JBJ
1818 if (fdata->intensity & 0x10) {
1819 r0_perm = 0;
1820 intensity = fdata->intensity & ~0x10;
1821 } else {
1822 r0_perm = 1;
1823 intensity = fdata->intensity;
1824 }
1825 switch (intensity) {
1da177e4
LT
1826 case 0x00: /* Normal format */
1827 case 0x08: /* Normal format, use cdl. */
1828 cplength = 2 + rpt;
1829 datasize = sizeof(struct DE_eckd_data) +
1830 sizeof(struct LO_eckd_data) +
1831 rpt * sizeof(struct eckd_count);
1832 break;
1833 case 0x01: /* Write record zero and format track. */
1834 case 0x09: /* Write record zero and format track, use cdl. */
1835 cplength = 3 + rpt;
1836 datasize = sizeof(struct DE_eckd_data) +
1837 sizeof(struct LO_eckd_data) +
1838 sizeof(struct eckd_count) +
1839 rpt * sizeof(struct eckd_count);
1840 break;
1841 case 0x04: /* Invalidate track. */
1842 case 0x0c: /* Invalidate track, use cdl. */
1843 cplength = 3;
1844 datasize = sizeof(struct DE_eckd_data) +
1845 sizeof(struct LO_eckd_data) +
1846 sizeof(struct eckd_count);
1847 break;
1848 default:
fc19f381
SH
1849 dev_warn(&device->cdev->dev, "An I/O control call used "
1850 "incorrect flags 0x%x\n", fdata->intensity);
1da177e4
LT
1851 return ERR_PTR(-EINVAL);
1852 }
1853 /* Allocate the format ccw request. */
68b781fe 1854 fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1da177e4
LT
1855 if (IS_ERR(fcp))
1856 return fcp;
1857
1858 data = fcp->data;
1859 ccw = fcp->cpaddr;
1860
f9a28f7b 1861 switch (intensity & ~0x08) {
1da177e4
LT
1862 case 0x00: /* Normal format. */
1863 define_extent(ccw++, (struct DE_eckd_data *) data,
1864 fdata->start_unit, fdata->start_unit,
1865 DASD_ECKD_CCW_WRITE_CKD, device);
f9a28f7b
JBJ
1866 /* grant subsystem permission to format R0 */
1867 if (r0_perm)
1868 ((struct DE_eckd_data *)data)->ga_extended |= 0x04;
1da177e4
LT
1869 data += sizeof(struct DE_eckd_data);
1870 ccw[-1].flags |= CCW_FLAG_CC;
1871 locate_record(ccw++, (struct LO_eckd_data *) data,
1872 fdata->start_unit, 0, rpt,
1873 DASD_ECKD_CCW_WRITE_CKD, device,
1874 fdata->blksize);
1875 data += sizeof(struct LO_eckd_data);
1876 break;
1877 case 0x01: /* Write record zero + format track. */
1878 define_extent(ccw++, (struct DE_eckd_data *) data,
1879 fdata->start_unit, fdata->start_unit,
1880 DASD_ECKD_CCW_WRITE_RECORD_ZERO,
1881 device);
1882 data += sizeof(struct DE_eckd_data);
1883 ccw[-1].flags |= CCW_FLAG_CC;
1884 locate_record(ccw++, (struct LO_eckd_data *) data,
1885 fdata->start_unit, 0, rpt + 1,
1886 DASD_ECKD_CCW_WRITE_RECORD_ZERO, device,
8e09f215 1887 device->block->bp_block);
1da177e4
LT
1888 data += sizeof(struct LO_eckd_data);
1889 break;
1890 case 0x04: /* Invalidate track. */
1891 define_extent(ccw++, (struct DE_eckd_data *) data,
1892 fdata->start_unit, fdata->start_unit,
1893 DASD_ECKD_CCW_WRITE_CKD, device);
1894 data += sizeof(struct DE_eckd_data);
1895 ccw[-1].flags |= CCW_FLAG_CC;
1896 locate_record(ccw++, (struct LO_eckd_data *) data,
1897 fdata->start_unit, 0, 1,
1898 DASD_ECKD_CCW_WRITE_CKD, device, 8);
1899 data += sizeof(struct LO_eckd_data);
1900 break;
1901 }
f9a28f7b 1902 if (intensity & 0x01) { /* write record zero */
1da177e4
LT
1903 ect = (struct eckd_count *) data;
1904 data += sizeof(struct eckd_count);
b44b0ab3
SW
1905 ect->cyl = address.cyl;
1906 ect->head = address.head;
1da177e4
LT
1907 ect->record = 0;
1908 ect->kl = 0;
1909 ect->dl = 8;
1910 ccw[-1].flags |= CCW_FLAG_CC;
1911 ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
1912 ccw->flags = CCW_FLAG_SLI;
1913 ccw->count = 8;
1914 ccw->cda = (__u32)(addr_t) ect;
1915 ccw++;
1916 }
f9a28f7b 1917 if ((intensity & ~0x08) & 0x04) { /* erase track */
1da177e4
LT
1918 ect = (struct eckd_count *) data;
1919 data += sizeof(struct eckd_count);
b44b0ab3
SW
1920 ect->cyl = address.cyl;
1921 ect->head = address.head;
1da177e4
LT
1922 ect->record = 1;
1923 ect->kl = 0;
1924 ect->dl = 0;
1925 ccw[-1].flags |= CCW_FLAG_CC;
1926 ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
1927 ccw->flags = CCW_FLAG_SLI;
1928 ccw->count = 8;
1929 ccw->cda = (__u32)(addr_t) ect;
1930 } else { /* write remaining records */
1931 for (i = 0; i < rpt; i++) {
1932 ect = (struct eckd_count *) data;
1933 data += sizeof(struct eckd_count);
b44b0ab3
SW
1934 ect->cyl = address.cyl;
1935 ect->head = address.head;
1da177e4
LT
1936 ect->record = i + 1;
1937 ect->kl = 0;
1938 ect->dl = fdata->blksize;
1939 /* Check for special tracks 0-1 when formatting CDL */
f9a28f7b 1940 if ((intensity & 0x08) &&
1da177e4
LT
1941 fdata->start_unit == 0) {
1942 if (i < 3) {
1943 ect->kl = 4;
1944 ect->dl = sizes_trk0[i] - 4;
138c014d 1945 }
1da177e4 1946 }
f9a28f7b 1947 if ((intensity & 0x08) &&
1da177e4
LT
1948 fdata->start_unit == 1) {
1949 ect->kl = 44;
1950 ect->dl = LABEL_SIZE - 44;
1951 }
1952 ccw[-1].flags |= CCW_FLAG_CC;
1953 ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
1954 ccw->flags = CCW_FLAG_SLI;
1955 ccw->count = 8;
1956 ccw->cda = (__u32)(addr_t) ect;
1957 ccw++;
1958 }
1959 }
8e09f215
SW
1960 fcp->startdev = device;
1961 fcp->memdev = device;
eb6e199b 1962 fcp->retries = 256;
1da177e4
LT
1963 fcp->buildclk = get_clock();
1964 fcp->status = DASD_CQR_FILLED;
1965 return fcp;
1966}
1967
8e09f215 1968static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
1da177e4 1969{
8e09f215
SW
1970 cqr->status = DASD_CQR_FILLED;
1971 if (cqr->block && (cqr->startdev != cqr->block->base)) {
1972 dasd_eckd_reset_ccw_to_base_io(cqr);
1973 cqr->startdev = cqr->block->base;
a4d26c6a 1974 cqr->lpm = cqr->block->base->path_data.opm;
1da177e4 1975 }
8e09f215 1976};
1da177e4
LT
1977
1978static dasd_erp_fn_t
1979dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
1980{
8e09f215 1981 struct dasd_device *device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
1982 struct ccw_device *cdev = device->cdev;
1983
1984 switch (cdev->id.cu_type) {
1985 case 0x3990:
1986 case 0x2105:
1987 case 0x2107:
1988 case 0x1750:
1989 return dasd_3990_erp_action;
1990 case 0x9343:
1991 case 0x3880:
1992 default:
1993 return dasd_default_erp_action;
1994 }
1995}
1996
1997static dasd_erp_fn_t
1998dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
1999{
2000 return dasd_default_erp_postaction;
2001}
2002
5a27e60d
SW
2003static void dasd_eckd_check_for_device_change(struct dasd_device *device,
2004 struct dasd_ccw_req *cqr,
2005 struct irb *irb)
8e09f215
SW
2006{
2007 char mask;
f3eb5384 2008 char *sense = NULL;
501183f2 2009 struct dasd_eckd_private *private;
8e09f215 2010
501183f2 2011 private = (struct dasd_eckd_private *) device->private;
8e09f215
SW
2012 /* first of all check for state change pending interrupt */
2013 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
f3eb5384 2014 if ((scsw_dstat(&irb->scsw) & mask) == mask) {
501183f2
SH
2015 /* for alias only and not in offline processing*/
2016 if (!device->block && private->lcu &&
2017 !test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2018 /*
2019 * the state change could be caused by an alias
2020 * reassignment remove device from alias handling
2021 * to prevent new requests from being scheduled on
2022 * the wrong alias device
2023 */
2024 dasd_alias_remove_device(device);
2025
2026 /* schedule worker to reload device */
2027 dasd_reload_device(device);
2028 }
8e09f215
SW
2029 dasd_generic_handle_state_change(device);
2030 return;
2031 }
2032
a5a0061f 2033 sense = dasd_get_sense(irb);
5a27e60d
SW
2034 if (!sense)
2035 return;
2036
2037 /* summary unit check */
c7a29e56 2038 if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
a5a0061f 2039 (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
8e09f215
SW
2040 dasd_alias_handle_summary_unit_check(device, irb);
2041 return;
2042 }
2043
f60c768c 2044 /* service information message SIM */
5a27e60d 2045 if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
f3eb5384
SW
2046 ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
2047 dasd_3990_erp_handle_sim(device, sense);
f60c768c
SH
2048 return;
2049 }
2050
5a27e60d
SW
2051 /* loss of device reservation is handled via base devices only
2052 * as alias devices may be used with several bases
2053 */
c7a29e56
SW
2054 if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
2055 (sense[7] == 0x3F) &&
5a27e60d
SW
2056 (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
2057 test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
2058 if (device->features & DASD_FEATURE_FAILONSLCK)
2059 set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
2060 clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
2061 dev_err(&device->cdev->dev,
2062 "The device reservation was lost\n");
ada3df91 2063 }
5a27e60d 2064}
f3eb5384
SW
2065
2066static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
2067 struct dasd_device *startdev,
8e09f215 2068 struct dasd_block *block,
f3eb5384
SW
2069 struct request *req,
2070 sector_t first_rec,
2071 sector_t last_rec,
2072 sector_t first_trk,
2073 sector_t last_trk,
2074 unsigned int first_offs,
2075 unsigned int last_offs,
2076 unsigned int blk_per_trk,
2077 unsigned int blksize)
1da177e4
LT
2078{
2079 struct dasd_eckd_private *private;
2080 unsigned long *idaws;
2081 struct LO_eckd_data *LO_data;
2082 struct dasd_ccw_req *cqr;
2083 struct ccw1 *ccw;
5705f702 2084 struct req_iterator iter;
1da177e4
LT
2085 struct bio_vec *bv;
2086 char *dst;
f3eb5384 2087 unsigned int off;
1da177e4 2088 int count, cidaw, cplength, datasize;
f3eb5384 2089 sector_t recid;
1da177e4 2090 unsigned char cmd, rcmd;
8e09f215
SW
2091 int use_prefix;
2092 struct dasd_device *basedev;
1da177e4 2093
8e09f215
SW
2094 basedev = block->base;
2095 private = (struct dasd_eckd_private *) basedev->private;
1da177e4
LT
2096 if (rq_data_dir(req) == READ)
2097 cmd = DASD_ECKD_CCW_READ_MT;
2098 else if (rq_data_dir(req) == WRITE)
2099 cmd = DASD_ECKD_CCW_WRITE_MT;
2100 else
2101 return ERR_PTR(-EINVAL);
f3eb5384 2102
1da177e4
LT
2103 /* Check struct bio and count the number of blocks for the request. */
2104 count = 0;
2105 cidaw = 0;
5705f702 2106 rq_for_each_segment(bv, req, iter) {
6c92e699
JA
2107 if (bv->bv_len & (blksize - 1))
2108 /* Eckd can only do full blocks. */
2109 return ERR_PTR(-EINVAL);
8e09f215 2110 count += bv->bv_len >> (block->s2b_shift + 9);
347a8dc3 2111#if defined(CONFIG_64BIT)
6c92e699 2112 if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
8e09f215 2113 cidaw += bv->bv_len >> (block->s2b_shift + 9);
1da177e4 2114#endif
1da177e4
LT
2115 }
2116 /* Paranoia. */
2117 if (count != last_rec - first_rec + 1)
2118 return ERR_PTR(-EINVAL);
8e09f215
SW
2119
2120 /* use the prefix command if available */
2121 use_prefix = private->features.feature[8] & 0x01;
2122 if (use_prefix) {
2123 /* 1x prefix + number of blocks */
2124 cplength = 2 + count;
2125 /* 1x prefix + cidaws*sizeof(long) */
2126 datasize = sizeof(struct PFX_eckd_data) +
2127 sizeof(struct LO_eckd_data) +
2128 cidaw * sizeof(unsigned long);
2129 } else {
2130 /* 1x define extent + 1x locate record + number of blocks */
2131 cplength = 2 + count;
2132 /* 1x define extent + 1x locate record + cidaws*sizeof(long) */
2133 datasize = sizeof(struct DE_eckd_data) +
2134 sizeof(struct LO_eckd_data) +
2135 cidaw * sizeof(unsigned long);
2136 }
1da177e4
LT
2137 /* Find out the number of additional locate record ccws for cdl. */
2138 if (private->uses_cdl && first_rec < 2*blk_per_trk) {
2139 if (last_rec >= 2*blk_per_trk)
2140 count = 2*blk_per_trk - first_rec;
2141 cplength += count;
2142 datasize += count*sizeof(struct LO_eckd_data);
2143 }
2144 /* Allocate the ccw request. */
68b781fe
SH
2145 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2146 startdev);
1da177e4
LT
2147 if (IS_ERR(cqr))
2148 return cqr;
2149 ccw = cqr->cpaddr;
8e09f215
SW
2150 /* First ccw is define extent or prefix. */
2151 if (use_prefix) {
2152 if (prefix(ccw++, cqr->data, first_trk,
2153 last_trk, cmd, basedev, startdev) == -EAGAIN) {
2154 /* Clock not in sync and XRC is enabled.
2155 * Try again later.
2156 */
2157 dasd_sfree_request(cqr, startdev);
2158 return ERR_PTR(-EAGAIN);
2159 }
2160 idaws = (unsigned long *) (cqr->data +
2161 sizeof(struct PFX_eckd_data));
2162 } else {
2163 if (define_extent(ccw++, cqr->data, first_trk,
2164 last_trk, cmd, startdev) == -EAGAIN) {
2165 /* Clock not in sync and XRC is enabled.
2166 * Try again later.
2167 */
2168 dasd_sfree_request(cqr, startdev);
2169 return ERR_PTR(-EAGAIN);
2170 }
2171 idaws = (unsigned long *) (cqr->data +
2172 sizeof(struct DE_eckd_data));
d54853ef 2173 }
1da177e4 2174 /* Build locate_record+read/write/ccws. */
1da177e4
LT
2175 LO_data = (struct LO_eckd_data *) (idaws + cidaw);
2176 recid = first_rec;
2177 if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
2178 /* Only standard blocks so there is just one locate record. */
2179 ccw[-1].flags |= CCW_FLAG_CC;
2180 locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
8e09f215 2181 last_rec - recid + 1, cmd, basedev, blksize);
1da177e4 2182 }
5705f702 2183 rq_for_each_segment(bv, req, iter) {
1da177e4
LT
2184 dst = page_address(bv->bv_page) + bv->bv_offset;
2185 if (dasd_page_cache) {
2186 char *copy = kmem_cache_alloc(dasd_page_cache,
441e143e 2187 GFP_DMA | __GFP_NOWARN);
1da177e4
LT
2188 if (copy && rq_data_dir(req) == WRITE)
2189 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
2190 if (copy)
2191 dst = copy + bv->bv_offset;
2192 }
2193 for (off = 0; off < bv->bv_len; off += blksize) {
2194 sector_t trkid = recid;
2195 unsigned int recoffs = sector_div(trkid, blk_per_trk);
2196 rcmd = cmd;
2197 count = blksize;
2198 /* Locate record for cdl special block ? */
2199 if (private->uses_cdl && recid < 2*blk_per_trk) {
2200 if (dasd_eckd_cdl_special(blk_per_trk, recid)){
2201 rcmd |= 0x8;
2202 count = dasd_eckd_cdl_reclen(recid);
ec5883ab
HH
2203 if (count < blksize &&
2204 rq_data_dir(req) == READ)
1da177e4
LT
2205 memset(dst + count, 0xe5,
2206 blksize - count);
2207 }
2208 ccw[-1].flags |= CCW_FLAG_CC;
2209 locate_record(ccw++, LO_data++,
2210 trkid, recoffs + 1,
8e09f215 2211 1, rcmd, basedev, count);
1da177e4
LT
2212 }
2213 /* Locate record for standard blocks ? */
2214 if (private->uses_cdl && recid == 2*blk_per_trk) {
2215 ccw[-1].flags |= CCW_FLAG_CC;
2216 locate_record(ccw++, LO_data++,
2217 trkid, recoffs + 1,
2218 last_rec - recid + 1,
8e09f215 2219 cmd, basedev, count);
1da177e4
LT
2220 }
2221 /* Read/write ccw. */
2222 ccw[-1].flags |= CCW_FLAG_CC;
2223 ccw->cmd_code = rcmd;
2224 ccw->count = count;
2225 if (idal_is_needed(dst, blksize)) {
2226 ccw->cda = (__u32)(addr_t) idaws;
2227 ccw->flags = CCW_FLAG_IDA;
2228 idaws = idal_create_words(idaws, dst, blksize);
2229 } else {
2230 ccw->cda = (__u32)(addr_t) dst;
2231 ccw->flags = 0;
2232 }
2233 ccw++;
2234 dst += blksize;
2235 recid++;
2236 }
2237 }
13de227b
HS
2238 if (blk_noretry_request(req) ||
2239 block->base->features & DASD_FEATURE_FAILFAST)
1c01b8a5 2240 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
8e09f215
SW
2241 cqr->startdev = startdev;
2242 cqr->memdev = startdev;
2243 cqr->block = block;
7c8faa86 2244 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
a4d26c6a 2245 cqr->lpm = startdev->path_data.ppm;
1da177e4
LT
2246 cqr->retries = 256;
2247 cqr->buildclk = get_clock();
2248 cqr->status = DASD_CQR_FILLED;
2249 return cqr;
2250}
2251
f3eb5384
SW
2252static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
2253 struct dasd_device *startdev,
2254 struct dasd_block *block,
2255 struct request *req,
2256 sector_t first_rec,
2257 sector_t last_rec,
2258 sector_t first_trk,
2259 sector_t last_trk,
2260 unsigned int first_offs,
2261 unsigned int last_offs,
2262 unsigned int blk_per_trk,
2263 unsigned int blksize)
2264{
f3eb5384
SW
2265 unsigned long *idaws;
2266 struct dasd_ccw_req *cqr;
2267 struct ccw1 *ccw;
2268 struct req_iterator iter;
2269 struct bio_vec *bv;
2270 char *dst, *idaw_dst;
2271 unsigned int cidaw, cplength, datasize;
2272 unsigned int tlf;
2273 sector_t recid;
2274 unsigned char cmd;
2275 struct dasd_device *basedev;
2276 unsigned int trkcount, count, count_to_trk_end;
2277 unsigned int idaw_len, seg_len, part_len, len_to_track_end;
2278 unsigned char new_track, end_idaw;
2279 sector_t trkid;
2280 unsigned int recoffs;
2281
2282 basedev = block->base;
f3eb5384
SW
2283 if (rq_data_dir(req) == READ)
2284 cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2285 else if (rq_data_dir(req) == WRITE)
2286 cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2287 else
2288 return ERR_PTR(-EINVAL);
2289
2290 /* Track based I/O needs IDAWs for each page, and not just for
2291 * 64 bit addresses. We need additional idals for pages
2292 * that get filled from two tracks, so we use the number
2293 * of records as upper limit.
2294 */
2295 cidaw = last_rec - first_rec + 1;
2296 trkcount = last_trk - first_trk + 1;
2297
2298 /* 1x prefix + one read/write ccw per track */
2299 cplength = 1 + trkcount;
2300
2301 /* on 31-bit we need space for two 32 bit addresses per page
2302 * on 64-bit one 64 bit address
2303 */
2304 datasize = sizeof(struct PFX_eckd_data) +
2305 cidaw * sizeof(unsigned long long);
2306
2307 /* Allocate the ccw request. */
68b781fe
SH
2308 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2309 startdev);
f3eb5384
SW
2310 if (IS_ERR(cqr))
2311 return cqr;
2312 ccw = cqr->cpaddr;
2313 /* transfer length factor: how many bytes to read from the last track */
2314 if (first_trk == last_trk)
2315 tlf = last_offs - first_offs + 1;
2316 else
2317 tlf = last_offs + 1;
2318 tlf *= blksize;
2319
2320 if (prefix_LRE(ccw++, cqr->data, first_trk,
2321 last_trk, cmd, basedev, startdev,
2322 1 /* format */, first_offs + 1,
2323 trkcount, blksize,
2324 tlf) == -EAGAIN) {
2325 /* Clock not in sync and XRC is enabled.
2326 * Try again later.
2327 */
2328 dasd_sfree_request(cqr, startdev);
2329 return ERR_PTR(-EAGAIN);
2330 }
2331
2332 /*
2333 * The translation of request into ccw programs must meet the
2334 * following conditions:
2335 * - all idaws but the first and the last must address full pages
2336 * (or 2K blocks on 31-bit)
2337 * - the scope of a ccw and it's idal ends with the track boundaries
2338 */
2339 idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
2340 recid = first_rec;
2341 new_track = 1;
2342 end_idaw = 0;
2343 len_to_track_end = 0;
2344 idaw_dst = 0;
2345 idaw_len = 0;
2346 rq_for_each_segment(bv, req, iter) {
2347 dst = page_address(bv->bv_page) + bv->bv_offset;
2348 seg_len = bv->bv_len;
2349 while (seg_len) {
2350 if (new_track) {
2351 trkid = recid;
2352 recoffs = sector_div(trkid, blk_per_trk);
2353 count_to_trk_end = blk_per_trk - recoffs;
2354 count = min((last_rec - recid + 1),
2355 (sector_t)count_to_trk_end);
2356 len_to_track_end = count * blksize;
2357 ccw[-1].flags |= CCW_FLAG_CC;
2358 ccw->cmd_code = cmd;
2359 ccw->count = len_to_track_end;
2360 ccw->cda = (__u32)(addr_t)idaws;
2361 ccw->flags = CCW_FLAG_IDA;
2362 ccw++;
2363 recid += count;
2364 new_track = 0;
52db45c3
SW
2365 /* first idaw for a ccw may start anywhere */
2366 if (!idaw_dst)
2367 idaw_dst = dst;
f3eb5384 2368 }
52db45c3
SW
2369 /* If we start a new idaw, we must make sure that it
2370 * starts on an IDA_BLOCK_SIZE boundary.
f3eb5384
SW
2371 * If we continue an idaw, we must make sure that the
2372 * current segment begins where the so far accumulated
2373 * idaw ends
2374 */
52db45c3
SW
2375 if (!idaw_dst) {
2376 if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
2377 dasd_sfree_request(cqr, startdev);
2378 return ERR_PTR(-ERANGE);
2379 } else
2380 idaw_dst = dst;
2381 }
f3eb5384
SW
2382 if ((idaw_dst + idaw_len) != dst) {
2383 dasd_sfree_request(cqr, startdev);
2384 return ERR_PTR(-ERANGE);
2385 }
2386 part_len = min(seg_len, len_to_track_end);
2387 seg_len -= part_len;
2388 dst += part_len;
2389 idaw_len += part_len;
2390 len_to_track_end -= part_len;
2391 /* collected memory area ends on an IDA_BLOCK border,
2392 * -> create an idaw
2393 * idal_create_words will handle cases where idaw_len
2394 * is larger then IDA_BLOCK_SIZE
2395 */
2396 if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
2397 end_idaw = 1;
2398 /* We also need to end the idaw at track end */
2399 if (!len_to_track_end) {
2400 new_track = 1;
2401 end_idaw = 1;
2402 }
2403 if (end_idaw) {
2404 idaws = idal_create_words(idaws, idaw_dst,
2405 idaw_len);
2406 idaw_dst = 0;
2407 idaw_len = 0;
2408 end_idaw = 0;
2409 }
2410 }
2411 }
2412
2413 if (blk_noretry_request(req) ||
2414 block->base->features & DASD_FEATURE_FAILFAST)
2415 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2416 cqr->startdev = startdev;
2417 cqr->memdev = startdev;
2418 cqr->block = block;
7c8faa86 2419 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
a4d26c6a 2420 cqr->lpm = startdev->path_data.ppm;
f3eb5384
SW
2421 cqr->retries = 256;
2422 cqr->buildclk = get_clock();
2423 cqr->status = DASD_CQR_FILLED;
2424 return cqr;
2425}
2426
2427static int prepare_itcw(struct itcw *itcw,
2428 unsigned int trk, unsigned int totrk, int cmd,
2429 struct dasd_device *basedev,
2430 struct dasd_device *startdev,
2431 unsigned int rec_on_trk, int count,
2432 unsigned int blksize,
2433 unsigned int total_data_size,
2434 unsigned int tlf,
2435 unsigned int blk_per_trk)
2436{
2437 struct PFX_eckd_data pfxdata;
2438 struct dasd_eckd_private *basepriv, *startpriv;
2439 struct DE_eckd_data *dedata;
2440 struct LRE_eckd_data *lredata;
2441 struct dcw *dcw;
2442
2443 u32 begcyl, endcyl;
2444 u16 heads, beghead, endhead;
2445 u8 pfx_cmd;
2446
2447 int rc = 0;
2448 int sector = 0;
2449 int dn, d;
2450
2451
2452 /* setup prefix data */
2453 basepriv = (struct dasd_eckd_private *) basedev->private;
2454 startpriv = (struct dasd_eckd_private *) startdev->private;
2455 dedata = &pfxdata.define_extent;
2456 lredata = &pfxdata.locate_record;
2457
2458 memset(&pfxdata, 0, sizeof(pfxdata));
2459 pfxdata.format = 1; /* PFX with LRE */
2460 pfxdata.base_address = basepriv->ned->unit_addr;
2461 pfxdata.base_lss = basepriv->ned->ID;
2462 pfxdata.validity.define_extent = 1;
2463
2464 /* private uid is kept up to date, conf_data may be outdated */
2465 if (startpriv->uid.type != UA_BASE_DEVICE) {
2466 pfxdata.validity.verify_base = 1;
2467 if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
2468 pfxdata.validity.hyper_pav = 1;
2469 }
2470
2471 switch (cmd) {
2472 case DASD_ECKD_CCW_READ_TRACK_DATA:
2473 dedata->mask.perm = 0x1;
2474 dedata->attributes.operation = basepriv->attrib.operation;
2475 dedata->blk_size = blksize;
2476 dedata->ga_extended |= 0x42;
2477 lredata->operation.orientation = 0x0;
2478 lredata->operation.operation = 0x0C;
2479 lredata->auxiliary.check_bytes = 0x01;
2480 pfx_cmd = DASD_ECKD_CCW_PFX_READ;
2481 break;
2482 case DASD_ECKD_CCW_WRITE_TRACK_DATA:
2483 dedata->mask.perm = 0x02;
2484 dedata->attributes.operation = basepriv->attrib.operation;
2485 dedata->blk_size = blksize;
2486 rc = check_XRC_on_prefix(&pfxdata, basedev);
2487 dedata->ga_extended |= 0x42;
2488 lredata->operation.orientation = 0x0;
2489 lredata->operation.operation = 0x3F;
2490 lredata->extended_operation = 0x23;
2491 lredata->auxiliary.check_bytes = 0x2;
2492 pfx_cmd = DASD_ECKD_CCW_PFX;
2493 break;
2494 default:
2495 DBF_DEV_EVENT(DBF_ERR, basedev,
2496 "prepare itcw, unknown opcode 0x%x", cmd);
2497 BUG();
2498 break;
2499 }
2500 if (rc)
2501 return rc;
2502
2503 dedata->attributes.mode = 0x3; /* ECKD */
2504
2505 heads = basepriv->rdc_data.trk_per_cyl;
2506 begcyl = trk / heads;
2507 beghead = trk % heads;
2508 endcyl = totrk / heads;
2509 endhead = totrk % heads;
2510
2511 /* check for sequential prestage - enhance cylinder range */
2512 if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
2513 dedata->attributes.operation == DASD_SEQ_ACCESS) {
2514
2515 if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
2516 endcyl += basepriv->attrib.nr_cyl;
2517 else
2518 endcyl = (basepriv->real_cyl - 1);
2519 }
2520
2521 set_ch_t(&dedata->beg_ext, begcyl, beghead);
2522 set_ch_t(&dedata->end_ext, endcyl, endhead);
2523
2524 dedata->ep_format = 0x20; /* records per track is valid */
2525 dedata->ep_rec_per_track = blk_per_trk;
2526
2527 if (rec_on_trk) {
2528 switch (basepriv->rdc_data.dev_type) {
2529 case 0x3390:
2530 dn = ceil_quot(blksize + 6, 232);
2531 d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
2532 sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
2533 break;
2534 case 0x3380:
2535 d = 7 + ceil_quot(blksize + 12, 32);
2536 sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
2537 break;
2538 }
2539 }
2540
2541 lredata->auxiliary.length_valid = 1;
2542 lredata->auxiliary.length_scope = 1;
2543 lredata->auxiliary.imbedded_ccw_valid = 1;
2544 lredata->length = tlf;
2545 lredata->imbedded_ccw = cmd;
2546 lredata->count = count;
2547 lredata->sector = sector;
2548 set_ch_t(&lredata->seek_addr, begcyl, beghead);
2549 lredata->search_arg.cyl = lredata->seek_addr.cyl;
2550 lredata->search_arg.head = lredata->seek_addr.head;
2551 lredata->search_arg.record = rec_on_trk;
2552
2553 dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
2554 &pfxdata, sizeof(pfxdata), total_data_size);
364c18d8 2555 return IS_ERR(dcw) ? PTR_ERR(dcw) : 0;
f3eb5384
SW
2556}
2557
2558static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
2559 struct dasd_device *startdev,
2560 struct dasd_block *block,
2561 struct request *req,
2562 sector_t first_rec,
2563 sector_t last_rec,
2564 sector_t first_trk,
2565 sector_t last_trk,
2566 unsigned int first_offs,
2567 unsigned int last_offs,
2568 unsigned int blk_per_trk,
2569 unsigned int blksize)
2570{
f3eb5384
SW
2571 struct dasd_ccw_req *cqr;
2572 struct req_iterator iter;
2573 struct bio_vec *bv;
2574 char *dst;
2575 unsigned int trkcount, ctidaw;
2576 unsigned char cmd;
2577 struct dasd_device *basedev;
2578 unsigned int tlf;
2579 struct itcw *itcw;
2580 struct tidaw *last_tidaw = NULL;
2581 int itcw_op;
2582 size_t itcw_size;
ef19298b
SW
2583 u8 tidaw_flags;
2584 unsigned int seg_len, part_len, len_to_track_end;
2585 unsigned char new_track;
2586 sector_t recid, trkid;
2587 unsigned int offs;
2588 unsigned int count, count_to_trk_end;
f3eb5384
SW
2589
2590 basedev = block->base;
f3eb5384
SW
2591 if (rq_data_dir(req) == READ) {
2592 cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2593 itcw_op = ITCW_OP_READ;
2594 } else if (rq_data_dir(req) == WRITE) {
2595 cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2596 itcw_op = ITCW_OP_WRITE;
2597 } else
2598 return ERR_PTR(-EINVAL);
2599
2600 /* trackbased I/O needs address all memory via TIDAWs,
2601 * not just for 64 bit addresses. This allows us to map
2602 * each segment directly to one tidaw.
ef19298b
SW
2603 * In the case of write requests, additional tidaws may
2604 * be needed when a segment crosses a track boundary.
f3eb5384
SW
2605 */
2606 trkcount = last_trk - first_trk + 1;
2607 ctidaw = 0;
2608 rq_for_each_segment(bv, req, iter) {
2609 ++ctidaw;
2610 }
ef19298b
SW
2611 if (rq_data_dir(req) == WRITE)
2612 ctidaw += (last_trk - first_trk);
f3eb5384
SW
2613
2614 /* Allocate the ccw request. */
2615 itcw_size = itcw_calc_size(0, ctidaw, 0);
68b781fe 2616 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
f3eb5384
SW
2617 if (IS_ERR(cqr))
2618 return cqr;
2619
f3eb5384
SW
2620 /* transfer length factor: how many bytes to read from the last track */
2621 if (first_trk == last_trk)
2622 tlf = last_offs - first_offs + 1;
2623 else
2624 tlf = last_offs + 1;
2625 tlf *= blksize;
2626
2627 itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
ef19298b
SW
2628 if (IS_ERR(itcw)) {
2629 dasd_sfree_request(cqr, startdev);
2630 return ERR_PTR(-EINVAL);
2631 }
f3eb5384 2632 cqr->cpaddr = itcw_get_tcw(itcw);
f3eb5384
SW
2633 if (prepare_itcw(itcw, first_trk, last_trk,
2634 cmd, basedev, startdev,
2635 first_offs + 1,
2636 trkcount, blksize,
2637 (last_rec - first_rec + 1) * blksize,
2638 tlf, blk_per_trk) == -EAGAIN) {
2639 /* Clock not in sync and XRC is enabled.
2640 * Try again later.
2641 */
2642 dasd_sfree_request(cqr, startdev);
2643 return ERR_PTR(-EAGAIN);
2644 }
d54cddb6 2645 len_to_track_end = 0;
f3eb5384
SW
2646 /*
2647 * A tidaw can address 4k of memory, but must not cross page boundaries
2648 * We can let the block layer handle this by setting
2649 * blk_queue_segment_boundary to page boundaries and
2650 * blk_max_segment_size to page size when setting up the request queue.
ef19298b
SW
2651 * For write requests, a TIDAW must not cross track boundaries, because
2652 * we have to set the CBC flag on the last tidaw for each track.
f3eb5384 2653 */
ef19298b
SW
2654 if (rq_data_dir(req) == WRITE) {
2655 new_track = 1;
2656 recid = first_rec;
2657 rq_for_each_segment(bv, req, iter) {
2658 dst = page_address(bv->bv_page) + bv->bv_offset;
2659 seg_len = bv->bv_len;
2660 while (seg_len) {
2661 if (new_track) {
2662 trkid = recid;
2663 offs = sector_div(trkid, blk_per_trk);
2664 count_to_trk_end = blk_per_trk - offs;
2665 count = min((last_rec - recid + 1),
2666 (sector_t)count_to_trk_end);
2667 len_to_track_end = count * blksize;
2668 recid += count;
2669 new_track = 0;
2670 }
2671 part_len = min(seg_len, len_to_track_end);
2672 seg_len -= part_len;
2673 len_to_track_end -= part_len;
2674 /* We need to end the tidaw at track end */
2675 if (!len_to_track_end) {
2676 new_track = 1;
2677 tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
2678 } else
2679 tidaw_flags = 0;
2680 last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
2681 dst, part_len);
2682 if (IS_ERR(last_tidaw))
2683 return ERR_PTR(-EINVAL);
2684 dst += part_len;
2685 }
2686 }
2687 } else {
2688 rq_for_each_segment(bv, req, iter) {
2689 dst = page_address(bv->bv_page) + bv->bv_offset;
2690 last_tidaw = itcw_add_tidaw(itcw, 0x00,
2691 dst, bv->bv_len);
2692 if (IS_ERR(last_tidaw))
2693 return ERR_PTR(-EINVAL);
2694 }
f3eb5384 2695 }
ef19298b
SW
2696 last_tidaw->flags |= TIDAW_FLAGS_LAST;
2697 last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
f3eb5384
SW
2698 itcw_finalize(itcw);
2699
2700 if (blk_noretry_request(req) ||
2701 block->base->features & DASD_FEATURE_FAILFAST)
2702 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
ef19298b 2703 cqr->cpmode = 1;
f3eb5384
SW
2704 cqr->startdev = startdev;
2705 cqr->memdev = startdev;
2706 cqr->block = block;
7c8faa86 2707 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
a4d26c6a 2708 cqr->lpm = startdev->path_data.ppm;
f3eb5384
SW
2709 cqr->retries = 256;
2710 cqr->buildclk = get_clock();
2711 cqr->status = DASD_CQR_FILLED;
2712 return cqr;
2713}
2714
2715static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
2716 struct dasd_block *block,
2717 struct request *req)
2718{
ef19298b 2719 int cmdrtd, cmdwtd;
f3eb5384 2720 int use_prefix;
ef19298b 2721 int fcx_multitrack;
45b44d76 2722 struct dasd_eckd_private *private;
f3eb5384
SW
2723 struct dasd_device *basedev;
2724 sector_t first_rec, last_rec;
2725 sector_t first_trk, last_trk;
2726 unsigned int first_offs, last_offs;
2727 unsigned int blk_per_trk, blksize;
2728 int cdlspecial;
ef19298b 2729 unsigned int data_size;
f3eb5384
SW
2730 struct dasd_ccw_req *cqr;
2731
2732 basedev = block->base;
2733 private = (struct dasd_eckd_private *) basedev->private;
2734
2735 /* Calculate number of blocks/records per track. */
2736 blksize = block->bp_block;
2737 blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
6fca97a9
SH
2738 if (blk_per_trk == 0)
2739 return ERR_PTR(-EINVAL);
f3eb5384 2740 /* Calculate record id of first and last block. */
83096ebf 2741 first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
f3eb5384
SW
2742 first_offs = sector_div(first_trk, blk_per_trk);
2743 last_rec = last_trk =
83096ebf 2744 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
f3eb5384
SW
2745 last_offs = sector_div(last_trk, blk_per_trk);
2746 cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
2747
ef19298b
SW
2748 fcx_multitrack = private->features.feature[40] & 0x20;
2749 data_size = blk_rq_bytes(req);
2750 /* tpm write request add CBC data on each track boundary */
2751 if (rq_data_dir(req) == WRITE)
2752 data_size += (last_trk - first_trk) * 4;
f3eb5384
SW
2753
2754 /* is read track data and write track data in command mode supported? */
2755 cmdrtd = private->features.feature[9] & 0x20;
2756 cmdwtd = private->features.feature[12] & 0x40;
2757 use_prefix = private->features.feature[8] & 0x01;
2758
2759 cqr = NULL;
2760 if (cdlspecial || dasd_page_cache) {
2761 /* do nothing, just fall through to the cmd mode single case */
ef19298b
SW
2762 } else if ((data_size <= private->fcx_max_data)
2763 && (fcx_multitrack || (first_trk == last_trk))) {
f3eb5384
SW
2764 cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
2765 first_rec, last_rec,
2766 first_trk, last_trk,
2767 first_offs, last_offs,
2768 blk_per_trk, blksize);
ef19298b
SW
2769 if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
2770 (PTR_ERR(cqr) != -ENOMEM))
f3eb5384
SW
2771 cqr = NULL;
2772 } else if (use_prefix &&
2773 (((rq_data_dir(req) == READ) && cmdrtd) ||
2774 ((rq_data_dir(req) == WRITE) && cmdwtd))) {
2775 cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
2776 first_rec, last_rec,
2777 first_trk, last_trk,
2778 first_offs, last_offs,
2779 blk_per_trk, blksize);
ef19298b
SW
2780 if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
2781 (PTR_ERR(cqr) != -ENOMEM))
f3eb5384
SW
2782 cqr = NULL;
2783 }
2784 if (!cqr)
2785 cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
2786 first_rec, last_rec,
2787 first_trk, last_trk,
2788 first_offs, last_offs,
2789 blk_per_trk, blksize);
2790 return cqr;
2791}
2792
e4dbb0f2
SH
2793static struct dasd_ccw_req *dasd_raw_build_cp(struct dasd_device *startdev,
2794 struct dasd_block *block,
2795 struct request *req)
2796{
e4dbb0f2
SH
2797 unsigned long *idaws;
2798 struct dasd_device *basedev;
2799 struct dasd_ccw_req *cqr;
2800 struct ccw1 *ccw;
2801 struct req_iterator iter;
2802 struct bio_vec *bv;
2803 char *dst;
2804 unsigned char cmd;
2805 unsigned int trkcount;
2806 unsigned int seg_len, len_to_track_end;
2807 unsigned int first_offs;
2808 unsigned int cidaw, cplength, datasize;
2809 sector_t first_trk, last_trk;
2810 unsigned int pfx_datasize;
2811
2812 /*
2813 * raw track access needs to be mutiple of 64k and on 64k boundary
2814 */
2815 if ((blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK) != 0) {
2816 cqr = ERR_PTR(-EINVAL);
2817 goto out;
2818 }
2819 if (((blk_rq_pos(req) + blk_rq_sectors(req)) %
2820 DASD_RAW_SECTORS_PER_TRACK) != 0) {
2821 cqr = ERR_PTR(-EINVAL);
2822 goto out;
2823 }
2824
2825 first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
2826 last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
2827 DASD_RAW_SECTORS_PER_TRACK;
2828 trkcount = last_trk - first_trk + 1;
2829 first_offs = 0;
2830 basedev = block->base;
e4dbb0f2
SH
2831
2832 if (rq_data_dir(req) == READ)
2833 cmd = DASD_ECKD_CCW_READ_TRACK;
2834 else if (rq_data_dir(req) == WRITE)
2835 cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
2836 else {
2837 cqr = ERR_PTR(-EINVAL);
2838 goto out;
2839 }
2840
2841 /*
2842 * Raw track based I/O needs IDAWs for each page,
2843 * and not just for 64 bit addresses.
2844 */
2845 cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;
2846
2847 /* 1x prefix + one read/write ccw per track */
2848 cplength = 1 + trkcount;
2849
2850 /*
2851 * struct PFX_eckd_data has up to 2 byte as extended parameter
2852 * this is needed for write full track and has to be mentioned
25985edc 2853 * separately
e4dbb0f2
SH
2854 * add 8 instead of 2 to keep 8 byte boundary
2855 */
2856 pfx_datasize = sizeof(struct PFX_eckd_data) + 8;
2857
2858 datasize = pfx_datasize + cidaw * sizeof(unsigned long long);
2859
2860 /* Allocate the ccw request. */
2861 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
2862 datasize, startdev);
2863 if (IS_ERR(cqr))
2864 goto out;
2865 ccw = cqr->cpaddr;
2866
2867 if (prefix_LRE(ccw++, cqr->data, first_trk, last_trk, cmd,
2868 basedev, startdev, 1 /* format */, first_offs + 1,
2869 trkcount, 0, 0) == -EAGAIN) {
2870 /* Clock not in sync and XRC is enabled.
2871 * Try again later.
2872 */
2873 dasd_sfree_request(cqr, startdev);
2874 cqr = ERR_PTR(-EAGAIN);
2875 goto out;
2876 }
2877
2878 idaws = (unsigned long *)(cqr->data + pfx_datasize);
2879
2880 len_to_track_end = 0;
2881
2882 rq_for_each_segment(bv, req, iter) {
2883 dst = page_address(bv->bv_page) + bv->bv_offset;
2884 seg_len = bv->bv_len;
2885 if (!len_to_track_end) {
2886 ccw[-1].flags |= CCW_FLAG_CC;
2887 ccw->cmd_code = cmd;
2888 /* maximum 3390 track size */
2889 ccw->count = 57326;
2890 /* 64k map to one track */
2891 len_to_track_end = 65536;
2892 ccw->cda = (__u32)(addr_t)idaws;
2893 ccw->flags |= CCW_FLAG_IDA;
2894 ccw->flags |= CCW_FLAG_SLI;
2895 ccw++;
2896 }
2897 len_to_track_end -= seg_len;
2898 idaws = idal_create_words(idaws, dst, seg_len);
2899 }
2900
2901 if (blk_noretry_request(req) ||
2902 block->base->features & DASD_FEATURE_FAILFAST)
2903 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2904 cqr->startdev = startdev;
2905 cqr->memdev = startdev;
2906 cqr->block = block;
2907 cqr->expires = startdev->default_expires * HZ;
2908 cqr->lpm = startdev->path_data.ppm;
2909 cqr->retries = 256;
2910 cqr->buildclk = get_clock();
2911 cqr->status = DASD_CQR_FILLED;
2912
2913 if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
2914 cqr = NULL;
2915out:
2916 return cqr;
2917}
2918
2919
1da177e4
LT
2920static int
2921dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
2922{
2923 struct dasd_eckd_private *private;
2924 struct ccw1 *ccw;
5705f702 2925 struct req_iterator iter;
1da177e4
LT
2926 struct bio_vec *bv;
2927 char *dst, *cda;
2928 unsigned int blksize, blk_per_trk, off;
2929 sector_t recid;
5705f702 2930 int status;
1da177e4
LT
2931
2932 if (!dasd_page_cache)
2933 goto out;
8e09f215
SW
2934 private = (struct dasd_eckd_private *) cqr->block->base->private;
2935 blksize = cqr->block->bp_block;
1da177e4 2936 blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
83096ebf 2937 recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
1da177e4
LT
2938 ccw = cqr->cpaddr;
2939 /* Skip over define extent & locate record. */
2940 ccw++;
2941 if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
2942 ccw++;
5705f702 2943 rq_for_each_segment(bv, req, iter) {
1da177e4
LT
2944 dst = page_address(bv->bv_page) + bv->bv_offset;
2945 for (off = 0; off < bv->bv_len; off += blksize) {
2946 /* Skip locate record. */
2947 if (private->uses_cdl && recid <= 2*blk_per_trk)
2948 ccw++;
2949 if (dst) {
2950 if (ccw->flags & CCW_FLAG_IDA)
2951 cda = *((char **)((addr_t) ccw->cda));
2952 else
2953 cda = (char *)((addr_t) ccw->cda);
2954 if (dst != cda) {
2955 if (rq_data_dir(req) == READ)
2956 memcpy(dst, cda, bv->bv_len);
2957 kmem_cache_free(dasd_page_cache,
2958 (void *)((addr_t)cda & PAGE_MASK));
2959 }
2960 dst = NULL;
2961 }
2962 ccw++;
2963 recid++;
2964 }
2965 }
2966out:
2967 status = cqr->status == DASD_CQR_DONE;
8e09f215 2968 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
2969 return status;
2970}
2971
8e09f215 2972/*
f3eb5384 2973 * Modify ccw/tcw in cqr so it can be started on a base device.
8e09f215
SW
2974 *
2975 * Note that this is not enough to restart the cqr!
2976 * Either reset cqr->startdev as well (summary unit check handling)
2977 * or restart via separate cqr (as in ERP handling).
2978 */
2979void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
2980{
2981 struct ccw1 *ccw;
2982 struct PFX_eckd_data *pfxdata;
f3eb5384
SW
2983 struct tcw *tcw;
2984 struct tccb *tccb;
2985 struct dcw *dcw;
2986
2987 if (cqr->cpmode == 1) {
2988 tcw = cqr->cpaddr;
2989 tccb = tcw_get_tccb(tcw);
2990 dcw = (struct dcw *)&tccb->tca[0];
2991 pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
8e09f215
SW
2992 pfxdata->validity.verify_base = 0;
2993 pfxdata->validity.hyper_pav = 0;
f3eb5384
SW
2994 } else {
2995 ccw = cqr->cpaddr;
2996 pfxdata = cqr->data;
2997 if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
2998 pfxdata->validity.verify_base = 0;
2999 pfxdata->validity.hyper_pav = 0;
3000 }
8e09f215
SW
3001 }
3002}
3003
3004#define DASD_ECKD_CHANQ_MAX_SIZE 4
3005
3006static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
3007 struct dasd_block *block,
3008 struct request *req)
3009{
3010 struct dasd_eckd_private *private;
3011 struct dasd_device *startdev;
3012 unsigned long flags;
3013 struct dasd_ccw_req *cqr;
3014
3015 startdev = dasd_alias_get_start_dev(base);
3016 if (!startdev)
3017 startdev = base;
3018 private = (struct dasd_eckd_private *) startdev->private;
3019 if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
3020 return ERR_PTR(-EBUSY);
3021
3022 spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
3023 private->count++;
e4dbb0f2
SH
3024 if ((base->features & DASD_FEATURE_USERAW))
3025 cqr = dasd_raw_build_cp(startdev, block, req);
3026 else
3027 cqr = dasd_eckd_build_cp(startdev, block, req);
8e09f215
SW
3028 if (IS_ERR(cqr))
3029 private->count--;
3030 spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
3031 return cqr;
3032}
3033
3034static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
3035 struct request *req)
3036{
3037 struct dasd_eckd_private *private;
3038 unsigned long flags;
3039
3040 spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
3041 private = (struct dasd_eckd_private *) cqr->memdev->private;
3042 private->count--;
3043 spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
3044 return dasd_eckd_free_cp(cqr, req);
3045}
3046
1da177e4
LT
3047static int
3048dasd_eckd_fill_info(struct dasd_device * device,
3049 struct dasd_information2_t * info)
3050{
3051 struct dasd_eckd_private *private;
3052
3053 private = (struct dasd_eckd_private *) device->private;
3054 info->label_block = 2;
3055 info->FBA_layout = private->uses_cdl ? 0 : 1;
3056 info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
3057 info->characteristics_size = sizeof(struct dasd_eckd_characteristics);
3058 memcpy(info->characteristics, &private->rdc_data,
3059 sizeof(struct dasd_eckd_characteristics));
4abb08c2
SW
3060 info->confdata_size = min((unsigned long)private->conf_len,
3061 sizeof(info->configuration_data));
3062 memcpy(info->configuration_data, private->conf_data,
3063 info->confdata_size);
1da177e4
LT
3064 return 0;
3065}
3066
3067/*
3068 * SECTION: ioctl functions for eckd devices.
3069 */
3070
3071/*
3072 * Release device ioctl.
138c014d 3073 * Buils a channel programm to releases a prior reserved
1da177e4
LT
3074 * (see dasd_eckd_reserve) device.
3075 */
3076static int
1107ccfb 3077dasd_eckd_release(struct dasd_device *device)
1da177e4 3078{
1da177e4
LT
3079 struct dasd_ccw_req *cqr;
3080 int rc;
f3eb5384 3081 struct ccw1 *ccw;
f932bcea 3082 int useglobal;
1da177e4
LT
3083
3084 if (!capable(CAP_SYS_ADMIN))
3085 return -EACCES;
3086
f932bcea 3087 useglobal = 0;
68b781fe 3088 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
1da177e4 3089 if (IS_ERR(cqr)) {
f932bcea
SW
3090 mutex_lock(&dasd_reserve_mutex);
3091 useglobal = 1;
3092 cqr = &dasd_reserve_req->cqr;
3093 memset(cqr, 0, sizeof(*cqr));
3094 memset(&dasd_reserve_req->ccw, 0,
3095 sizeof(dasd_reserve_req->ccw));
3096 cqr->cpaddr = &dasd_reserve_req->ccw;
3097 cqr->data = &dasd_reserve_req->data;
3098 cqr->magic = DASD_ECKD_MAGIC;
1da177e4 3099 }
f3eb5384
SW
3100 ccw = cqr->cpaddr;
3101 ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
3102 ccw->flags |= CCW_FLAG_SLI;
3103 ccw->count = 32;
3104 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
3105 cqr->startdev = device;
3106 cqr->memdev = device;
1da177e4 3107 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1c01b8a5 3108 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
336c340b 3109 cqr->retries = 2; /* set retry counter to enable basic ERP */
1da177e4
LT
3110 cqr->expires = 2 * HZ;
3111 cqr->buildclk = get_clock();
3112 cqr->status = DASD_CQR_FILLED;
3113
3114 rc = dasd_sleep_on_immediatly(cqr);
5a27e60d
SW
3115 if (!rc)
3116 clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
1da177e4 3117
f932bcea
SW
3118 if (useglobal)
3119 mutex_unlock(&dasd_reserve_mutex);
3120 else
3121 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3122 return rc;
3123}
3124
3125/*
3126 * Reserve device ioctl.
3127 * Options are set to 'synchronous wait for interrupt' and
138c014d
HH
3128 * 'timeout the request'. This leads to a terminate IO if
3129 * the interrupt is outstanding for a certain time.
1da177e4
LT
3130 */
3131static int
1107ccfb 3132dasd_eckd_reserve(struct dasd_device *device)
1da177e4 3133{
1da177e4
LT
3134 struct dasd_ccw_req *cqr;
3135 int rc;
f3eb5384 3136 struct ccw1 *ccw;
f932bcea 3137 int useglobal;
1da177e4
LT
3138
3139 if (!capable(CAP_SYS_ADMIN))
3140 return -EACCES;
3141
f932bcea 3142 useglobal = 0;
68b781fe 3143 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
1da177e4 3144 if (IS_ERR(cqr)) {
f932bcea
SW
3145 mutex_lock(&dasd_reserve_mutex);
3146 useglobal = 1;
3147 cqr = &dasd_reserve_req->cqr;
3148 memset(cqr, 0, sizeof(*cqr));
3149 memset(&dasd_reserve_req->ccw, 0,
3150 sizeof(dasd_reserve_req->ccw));
3151 cqr->cpaddr = &dasd_reserve_req->ccw;
3152 cqr->data = &dasd_reserve_req->data;
3153 cqr->magic = DASD_ECKD_MAGIC;
1da177e4 3154 }
f3eb5384
SW
3155 ccw = cqr->cpaddr;
3156 ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
3157 ccw->flags |= CCW_FLAG_SLI;
3158 ccw->count = 32;
3159 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
3160 cqr->startdev = device;
3161 cqr->memdev = device;
1da177e4 3162 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1c01b8a5 3163 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
336c340b 3164 cqr->retries = 2; /* set retry counter to enable basic ERP */
1da177e4
LT
3165 cqr->expires = 2 * HZ;
3166 cqr->buildclk = get_clock();
3167 cqr->status = DASD_CQR_FILLED;
3168
3169 rc = dasd_sleep_on_immediatly(cqr);
5a27e60d
SW
3170 if (!rc)
3171 set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
1da177e4 3172
f932bcea
SW
3173 if (useglobal)
3174 mutex_unlock(&dasd_reserve_mutex);
3175 else
3176 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3177 return rc;
3178}
3179
3180/*
3181 * Steal lock ioctl - unconditional reserve device.
138c014d 3182 * Buils a channel programm to break a device's reservation.
1da177e4
LT
3183 * (unconditional reserve)
3184 */
3185static int
1107ccfb 3186dasd_eckd_steal_lock(struct dasd_device *device)
1da177e4 3187{
1da177e4
LT
3188 struct dasd_ccw_req *cqr;
3189 int rc;
f3eb5384 3190 struct ccw1 *ccw;
f932bcea 3191 int useglobal;
1da177e4
LT
3192
3193 if (!capable(CAP_SYS_ADMIN))
3194 return -EACCES;
3195
f932bcea 3196 useglobal = 0;
68b781fe 3197 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
1da177e4 3198 if (IS_ERR(cqr)) {
f932bcea
SW
3199 mutex_lock(&dasd_reserve_mutex);
3200 useglobal = 1;
3201 cqr = &dasd_reserve_req->cqr;
3202 memset(cqr, 0, sizeof(*cqr));
3203 memset(&dasd_reserve_req->ccw, 0,
3204 sizeof(dasd_reserve_req->ccw));
3205 cqr->cpaddr = &dasd_reserve_req->ccw;
3206 cqr->data = &dasd_reserve_req->data;
3207 cqr->magic = DASD_ECKD_MAGIC;
1da177e4 3208 }
f3eb5384
SW
3209 ccw = cqr->cpaddr;
3210 ccw->cmd_code = DASD_ECKD_CCW_SLCK;
3211 ccw->flags |= CCW_FLAG_SLI;
3212 ccw->count = 32;
3213 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
3214 cqr->startdev = device;
3215 cqr->memdev = device;
1da177e4 3216 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1c01b8a5 3217 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
336c340b 3218 cqr->retries = 2; /* set retry counter to enable basic ERP */
1da177e4
LT
3219 cqr->expires = 2 * HZ;
3220 cqr->buildclk = get_clock();
3221 cqr->status = DASD_CQR_FILLED;
3222
3223 rc = dasd_sleep_on_immediatly(cqr);
5a27e60d
SW
3224 if (!rc)
3225 set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
1da177e4 3226
f932bcea
SW
3227 if (useglobal)
3228 mutex_unlock(&dasd_reserve_mutex);
3229 else
3230 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3231 return rc;
3232}
3233
196339f1
SW
3234/*
3235 * SNID - Sense Path Group ID
3236 * This ioctl may be used in situations where I/O is stalled due to
3237 * a reserve, so if the normal dasd_smalloc_request fails, we use the
3238 * preallocated dasd_reserve_req.
3239 */
3240static int dasd_eckd_snid(struct dasd_device *device,
3241 void __user *argp)
3242{
3243 struct dasd_ccw_req *cqr;
3244 int rc;
3245 struct ccw1 *ccw;
3246 int useglobal;
3247 struct dasd_snid_ioctl_data usrparm;
3248
3249 if (!capable(CAP_SYS_ADMIN))
3250 return -EACCES;
3251
3252 if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3253 return -EFAULT;
3254
3255 useglobal = 0;
3256 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
3257 sizeof(struct dasd_snid_data), device);
3258 if (IS_ERR(cqr)) {
3259 mutex_lock(&dasd_reserve_mutex);
3260 useglobal = 1;
3261 cqr = &dasd_reserve_req->cqr;
3262 memset(cqr, 0, sizeof(*cqr));
3263 memset(&dasd_reserve_req->ccw, 0,
3264 sizeof(dasd_reserve_req->ccw));
3265 cqr->cpaddr = &dasd_reserve_req->ccw;
3266 cqr->data = &dasd_reserve_req->data;
3267 cqr->magic = DASD_ECKD_MAGIC;
3268 }
3269 ccw = cqr->cpaddr;
3270 ccw->cmd_code = DASD_ECKD_CCW_SNID;
3271 ccw->flags |= CCW_FLAG_SLI;
3272 ccw->count = 12;
3273 ccw->cda = (__u32)(addr_t) cqr->data;
3274 cqr->startdev = device;
3275 cqr->memdev = device;
3276 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3277 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5a27e60d 3278 set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
196339f1
SW
3279 cqr->retries = 5;
3280 cqr->expires = 10 * HZ;
3281 cqr->buildclk = get_clock();
3282 cqr->status = DASD_CQR_FILLED;
3283 cqr->lpm = usrparm.path_mask;
3284
3285 rc = dasd_sleep_on_immediatly(cqr);
3286 /* verify that I/O processing didn't modify the path mask */
3287 if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
3288 rc = -EIO;
3289 if (!rc) {
3290 usrparm.data = *((struct dasd_snid_data *)cqr->data);
3291 if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
3292 rc = -EFAULT;
3293 }
3294
3295 if (useglobal)
3296 mutex_unlock(&dasd_reserve_mutex);
3297 else
3298 dasd_sfree_request(cqr, cqr->memdev);
3299 return rc;
3300}
3301
1da177e4
LT
3302/*
3303 * Read performance statistics
3304 */
3305static int
1107ccfb 3306dasd_eckd_performance(struct dasd_device *device, void __user *argp)
1da177e4 3307{
1da177e4
LT
3308 struct dasd_psf_prssd_data *prssdp;
3309 struct dasd_rssd_perf_stats_t *stats;
3310 struct dasd_ccw_req *cqr;
3311 struct ccw1 *ccw;
3312 int rc;
3313
68b781fe 3314 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
8e09f215
SW
3315 (sizeof(struct dasd_psf_prssd_data) +
3316 sizeof(struct dasd_rssd_perf_stats_t)),
1da177e4
LT
3317 device);
3318 if (IS_ERR(cqr)) {
fc19f381 3319 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
3320 "Could not allocate initialization request");
3321 return PTR_ERR(cqr);
3322 }
8e09f215
SW
3323 cqr->startdev = device;
3324 cqr->memdev = device;
1da177e4 3325 cqr->retries = 0;
eb6e199b 3326 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1da177e4
LT
3327 cqr->expires = 10 * HZ;
3328
3329 /* Prepare for Read Subsystem Data */
3330 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
8e09f215 3331 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1da177e4 3332 prssdp->order = PSF_ORDER_PRSSD;
5d67d164 3333 prssdp->suborder = 0x01; /* Performance Statistics */
1da177e4
LT
3334 prssdp->varies[1] = 0x01; /* Perf Statistics for the Subsystem */
3335
3336 ccw = cqr->cpaddr;
3337 ccw->cmd_code = DASD_ECKD_CCW_PSF;
8e09f215 3338 ccw->count = sizeof(struct dasd_psf_prssd_data);
1da177e4
LT
3339 ccw->flags |= CCW_FLAG_CC;
3340 ccw->cda = (__u32)(addr_t) prssdp;
3341
3342 /* Read Subsystem Data - Performance Statistics */
3343 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
8e09f215 3344 memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
1da177e4
LT
3345
3346 ccw++;
3347 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
8e09f215 3348 ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
1da177e4
LT
3349 ccw->cda = (__u32)(addr_t) stats;
3350
3351 cqr->buildclk = get_clock();
3352 cqr->status = DASD_CQR_FILLED;
3353 rc = dasd_sleep_on(cqr);
3354 if (rc == 0) {
1da177e4
LT
3355 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3356 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
1107ccfb
CH
3357 if (copy_to_user(argp, stats,
3358 sizeof(struct dasd_rssd_perf_stats_t)))
3359 rc = -EFAULT;
1da177e4 3360 }
8e09f215 3361 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3362 return rc;
3363}
3364
3365/*
3366 * Get attributes (cache operations)
3367 * Returnes the cache attributes used in Define Extend (DE).
3368 */
3369static int
1107ccfb 3370dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
1da177e4 3371{
1107ccfb
CH
3372 struct dasd_eckd_private *private =
3373 (struct dasd_eckd_private *)device->private;
3374 struct attrib_data_t attrib = private->attrib;
1da177e4
LT
3375 int rc;
3376
3377 if (!capable(CAP_SYS_ADMIN))
3378 return -EACCES;
1107ccfb 3379 if (!argp)
1da177e4
LT
3380 return -EINVAL;
3381
1107ccfb
CH
3382 rc = 0;
3383 if (copy_to_user(argp, (long *) &attrib,
8e09f215 3384 sizeof(struct attrib_data_t)))
1107ccfb 3385 rc = -EFAULT;
1da177e4
LT
3386
3387 return rc;
3388}
3389
3390/*
3391 * Set attributes (cache operations)
3392 * Stores the attributes for cache operation to be used in Define Extend (DE).
3393 */
3394static int
1107ccfb 3395dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
1da177e4 3396{
1107ccfb
CH
3397 struct dasd_eckd_private *private =
3398 (struct dasd_eckd_private *)device->private;
1da177e4
LT
3399 struct attrib_data_t attrib;
3400
3401 if (!capable(CAP_SYS_ADMIN))
3402 return -EACCES;
1107ccfb 3403 if (!argp)
1da177e4
LT
3404 return -EINVAL;
3405
1107ccfb 3406 if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
1da177e4 3407 return -EFAULT;
1da177e4
LT
3408 private->attrib = attrib;
3409
fc19f381
SH
3410 dev_info(&device->cdev->dev,
3411 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
3412 private->attrib.operation, private->attrib.nr_cyl);
1da177e4
LT
3413 return 0;
3414}
3415
ab1d848f
NH
3416/*
3417 * Issue syscall I/O to EMC Symmetrix array.
3418 * CCWs are PSF and RSSD
3419 */
3420static int dasd_symm_io(struct dasd_device *device, void __user *argp)
3421{
3422 struct dasd_symmio_parms usrparm;
3423 char *psf_data, *rssd_result;
3424 struct dasd_ccw_req *cqr;
3425 struct ccw1 *ccw;
52898025 3426 char psf0, psf1;
ab1d848f
NH
3427 int rc;
3428
52898025
NH
3429 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
3430 return -EACCES;
3431 psf0 = psf1 = 0;
3432
ab1d848f
NH
3433 /* Copy parms from caller */
3434 rc = -EFAULT;
3435 if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3436 goto out;
f8b06859
HC
3437 if (is_compat_task() || sizeof(long) == 4) {
3438 /* Make sure pointers are sane even on 31 bit. */
ab1d848f 3439 rc = -EINVAL;
f8b06859
HC
3440 if ((usrparm.psf_data >> 32) != 0)
3441 goto out;
3442 if ((usrparm.rssd_result >> 32) != 0)
3443 goto out;
3444 usrparm.psf_data &= 0x7fffffffULL;
3445 usrparm.rssd_result &= 0x7fffffffULL;
ab1d848f 3446 }
ab1d848f
NH
3447 /* alloc I/O data area */
3448 psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
3449 rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
3450 if (!psf_data || !rssd_result) {
3451 rc = -ENOMEM;
3452 goto out_free;
3453 }
3454
3455 /* get syscall header from user space */
3456 rc = -EFAULT;
3457 if (copy_from_user(psf_data,
3458 (void __user *)(unsigned long) usrparm.psf_data,
3459 usrparm.psf_data_len))
3460 goto out_free;
52898025
NH
3461 psf0 = psf_data[0];
3462 psf1 = psf_data[1];
ab1d848f
NH
3463
3464 /* setup CCWs for PSF + RSSD */
68b781fe 3465 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
ab1d848f 3466 if (IS_ERR(cqr)) {
fc19f381 3467 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
ab1d848f
NH
3468 "Could not allocate initialization request");
3469 rc = PTR_ERR(cqr);
3470 goto out_free;
3471 }
3472
3473 cqr->startdev = device;
3474 cqr->memdev = device;
3475 cqr->retries = 3;
3476 cqr->expires = 10 * HZ;
3477 cqr->buildclk = get_clock();
3478 cqr->status = DASD_CQR_FILLED;
3479
3480 /* Build the ccws */
3481 ccw = cqr->cpaddr;
3482
3483 /* PSF ccw */
3484 ccw->cmd_code = DASD_ECKD_CCW_PSF;
3485 ccw->count = usrparm.psf_data_len;
3486 ccw->flags |= CCW_FLAG_CC;
3487 ccw->cda = (__u32)(addr_t) psf_data;
3488
3489 ccw++;
3490
3491 /* RSSD ccw */
3492 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
3493 ccw->count = usrparm.rssd_result_len;
3494 ccw->flags = CCW_FLAG_SLI ;
3495 ccw->cda = (__u32)(addr_t) rssd_result;
3496
3497 rc = dasd_sleep_on(cqr);
3498 if (rc)
3499 goto out_sfree;
3500
3501 rc = -EFAULT;
3502 if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
3503 rssd_result, usrparm.rssd_result_len))
3504 goto out_sfree;
3505 rc = 0;
3506
3507out_sfree:
3508 dasd_sfree_request(cqr, cqr->memdev);
3509out_free:
3510 kfree(rssd_result);
3511 kfree(psf_data);
3512out:
52898025
NH
3513 DBF_DEV_EVENT(DBF_WARNING, device,
3514 "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
3515 (int) psf0, (int) psf1, rc);
ab1d848f
NH
3516 return rc;
3517}
3518
1107ccfb 3519static int
8e09f215 3520dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
1107ccfb 3521{
8e09f215
SW
3522 struct dasd_device *device = block->base;
3523
1107ccfb
CH
3524 switch (cmd) {
3525 case BIODASDGATTR:
3526 return dasd_eckd_get_attrib(device, argp);
3527 case BIODASDSATTR:
3528 return dasd_eckd_set_attrib(device, argp);
3529 case BIODASDPSRD:
3530 return dasd_eckd_performance(device, argp);
3531 case BIODASDRLSE:
3532 return dasd_eckd_release(device);
3533 case BIODASDRSRV:
3534 return dasd_eckd_reserve(device);
3535 case BIODASDSLCK:
3536 return dasd_eckd_steal_lock(device);
196339f1
SW
3537 case BIODASDSNID:
3538 return dasd_eckd_snid(device, argp);
ab1d848f
NH
3539 case BIODASDSYMMIO:
3540 return dasd_symm_io(device, argp);
1107ccfb
CH
3541 default:
3542 return -ENOIOCTLCMD;
3543 }
3544}
3545
445b5b49
HH
3546/*
3547 * Dump the range of CCWs into 'page' buffer
3548 * and return number of printed chars.
3549 */
4d284cac 3550static int
445b5b49
HH
3551dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
3552{
3553 int len, count;
3554 char *datap;
3555
3556 len = 0;
3557 while (from <= to) {
3558 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3559 " CCW %p: %08X %08X DAT:",
3560 from, ((int *) from)[0], ((int *) from)[1]);
3561
3562 /* get pointer to data (consider IDALs) */
3563 if (from->flags & CCW_FLAG_IDA)
3564 datap = (char *) *((addr_t *) (addr_t) from->cda);
3565 else
3566 datap = (char *) ((addr_t) from->cda);
3567
3568 /* dump data (max 32 bytes) */
3569 for (count = 0; count < from->count && count < 32; count++) {
3570 if (count % 8 == 0) len += sprintf(page + len, " ");
3571 if (count % 4 == 0) len += sprintf(page + len, " ");
3572 len += sprintf(page + len, "%02x", datap[count]);
3573 }
3574 len += sprintf(page + len, "\n");
3575 from++;
3576 }
3577 return len;
3578}
3579
fc19f381 3580static void
aeec92ca
SH
3581dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
3582 char *reason)
fc19f381
SH
3583{
3584 u64 *sense;
a5a0061f 3585 u64 *stat;
aeec92ca
SH
3586
3587 sense = (u64 *) dasd_get_sense(irb);
a5a0061f 3588 stat = (u64 *) &irb->scsw;
fc19f381 3589 if (sense) {
a5a0061f
SW
3590 DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
3591 "%016llx %016llx %016llx %016llx",
3592 reason, *stat, *((u32 *) (stat + 1)),
ed3640b2 3593 sense[0], sense[1], sense[2], sense[3]);
fc19f381 3594 } else {
a5a0061f
SW
3595 DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
3596 reason, *stat, *((u32 *) (stat + 1)),
3597 "NO VALID SENSE");
fc19f381
SH
3598 }
3599}
3600
1da177e4
LT
3601/*
3602 * Print sense data and related channel program.
3603 * Parts are printed because printk buffer is only 1024 bytes.
3604 */
f3eb5384 3605static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
8e09f215 3606 struct dasd_ccw_req *req, struct irb *irb)
1da177e4
LT
3607{
3608 char *page;
445b5b49
HH
3609 struct ccw1 *first, *last, *fail, *from, *to;
3610 int len, sl, sct;
1da177e4
LT
3611
3612 page = (char *) get_zeroed_page(GFP_ATOMIC);
3613 if (page == NULL) {
fc19f381
SH
3614 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3615 "No memory to dump sense data\n");
1da177e4
LT
3616 return;
3617 }
445b5b49
HH
3618 /* dump the sense data */
3619 len = sprintf(page, KERN_ERR PRINTK_HEADER
1da177e4 3620 " I/O status report for device %s:\n",
2a0217d5 3621 dev_name(&device->cdev->dev));
1da177e4 3622 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
a5a0061f
SW
3623 " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
3624 "CS:%02X RC:%d\n",
3625 req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
3626 scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
3627 scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
3628 req ? req->intrc : 0);
1da177e4
LT
3629 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3630 " device %s: Failing CCW: %p\n",
2a0217d5 3631 dev_name(&device->cdev->dev),
23d805b6 3632 (void *) (addr_t) irb->scsw.cmd.cpa);
1da177e4
LT
3633 if (irb->esw.esw0.erw.cons) {
3634 for (sl = 0; sl < 4; sl++) {
3635 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3636 " Sense(hex) %2d-%2d:",
3637 (8 * sl), ((8 * sl) + 7));
3638
3639 for (sct = 0; sct < 8; sct++) {
3640 len += sprintf(page + len, " %02x",
3641 irb->ecw[8 * sl + sct]);
3642 }
3643 len += sprintf(page + len, "\n");
3644 }
3645
3646 if (irb->ecw[27] & DASD_SENSE_BIT_0) {
3647 /* 24 Byte Sense Data */
445b5b49
HH
3648 sprintf(page + len, KERN_ERR PRINTK_HEADER
3649 " 24 Byte: %x MSG %x, "
3650 "%s MSGb to SYSOP\n",
3651 irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
3652 irb->ecw[1] & 0x10 ? "" : "no");
1da177e4
LT
3653 } else {
3654 /* 32 Byte Sense Data */
445b5b49
HH
3655 sprintf(page + len, KERN_ERR PRINTK_HEADER
3656 " 32 Byte: Format: %x "
3657 "Exception class %x\n",
3658 irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
1da177e4
LT
3659 }
3660 } else {
445b5b49
HH
3661 sprintf(page + len, KERN_ERR PRINTK_HEADER
3662 " SORRY - NO VALID SENSE AVAILABLE\n");
1da177e4 3663 }
445b5b49
HH
3664 printk("%s", page);
3665
8e09f215
SW
3666 if (req) {
3667 /* req == NULL for unsolicited interrupts */
3668 /* dump the Channel Program (max 140 Bytes per line) */
3669 /* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
3670 first = req->cpaddr;
3671 for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
3672 to = min(first + 6, last);
3673 len = sprintf(page, KERN_ERR PRINTK_HEADER
3674 " Related CP in req: %p\n", req);
3675 dasd_eckd_dump_ccw_range(first, to, page + len);
3676 printk("%s", page);
1da177e4 3677
8e09f215
SW
3678 /* print failing CCW area (maximum 4) */
3679 /* scsw->cda is either valid or zero */
3680 len = 0;
3681 from = ++to;
23d805b6
PO
3682 fail = (struct ccw1 *)(addr_t)
3683 irb->scsw.cmd.cpa; /* failing CCW */
8e09f215
SW
3684 if (from < fail - 2) {
3685 from = fail - 2; /* there is a gap - print header */
3686 len += sprintf(page, KERN_ERR PRINTK_HEADER "......\n");
3687 }
3688 to = min(fail + 1, last);
3689 len += dasd_eckd_dump_ccw_range(from, to, page + len);
3690
3691 /* print last CCWs (maximum 2) */
3692 from = max(from, ++to);
3693 if (from < last - 1) {
3694 from = last - 1; /* there is a gap - print header */
3695 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
3696 }
3697 len += dasd_eckd_dump_ccw_range(from, last, page + len);
3698 if (len > 0)
3699 printk("%s", page);
1da177e4 3700 }
1da177e4
LT
3701 free_page((unsigned long) page);
3702}
3703
f3eb5384
SW
3704
3705/*
3706 * Print sense data from a tcw.
3707 */
3708static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
3709 struct dasd_ccw_req *req, struct irb *irb)
3710{
3711 char *page;
3712 int len, sl, sct, residual;
f3eb5384 3713 struct tsb *tsb;
ef19298b 3714 u8 *sense, *rcq;
f3eb5384
SW
3715
3716 page = (char *) get_zeroed_page(GFP_ATOMIC);
3717 if (page == NULL) {
fc19f381 3718 DBF_DEV_EVENT(DBF_WARNING, device, " %s",
f3eb5384
SW
3719 "No memory to dump sense data");
3720 return;
3721 }
3722 /* dump the sense data */
3723 len = sprintf(page, KERN_ERR PRINTK_HEADER
3724 " I/O status report for device %s:\n",
3725 dev_name(&device->cdev->dev));
3726 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
a5a0061f
SW
3727 " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
3728 "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
3729 req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
3730 scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
3731 scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
3732 irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
3733 req ? req->intrc : 0);
f3eb5384
SW
3734 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3735 " device %s: Failing TCW: %p\n",
3736 dev_name(&device->cdev->dev),
3737 (void *) (addr_t) irb->scsw.tm.tcw);
3738
3739 tsb = NULL;
3740 sense = NULL;
a5a0061f 3741 if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
f3eb5384
SW
3742 tsb = tcw_get_tsb(
3743 (struct tcw *)(unsigned long)irb->scsw.tm.tcw);
3744
b8fde722 3745 if (tsb) {
f3eb5384
SW
3746 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3747 " tsb->length %d\n", tsb->length);
3748 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3749 " tsb->flags %x\n", tsb->flags);
3750 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3751 " tsb->dcw_offset %d\n", tsb->dcw_offset);
3752 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3753 " tsb->count %d\n", tsb->count);
3754 residual = tsb->count - 28;
3755 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3756 " residual %d\n", residual);
3757
3758 switch (tsb->flags & 0x07) {
3759 case 1: /* tsa_iostat */
3760 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3761 " tsb->tsa.iostat.dev_time %d\n",
3762 tsb->tsa.iostat.dev_time);
3763 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3764 " tsb->tsa.iostat.def_time %d\n",
3765 tsb->tsa.iostat.def_time);
3766 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3767 " tsb->tsa.iostat.queue_time %d\n",
3768 tsb->tsa.iostat.queue_time);
3769 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3770 " tsb->tsa.iostat.dev_busy_time %d\n",
3771 tsb->tsa.iostat.dev_busy_time);
3772 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3773 " tsb->tsa.iostat.dev_act_time %d\n",
3774 tsb->tsa.iostat.dev_act_time);
3775 sense = tsb->tsa.iostat.sense;
3776 break;
3777 case 2: /* ts_ddpc */
3778 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3779 " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
ef19298b
SW
3780 for (sl = 0; sl < 2; sl++) {
3781 len += sprintf(page + len,
3782 KERN_ERR PRINTK_HEADER
3783 " tsb->tsa.ddpc.rcq %2d-%2d: ",
3784 (8 * sl), ((8 * sl) + 7));
3785 rcq = tsb->tsa.ddpc.rcq;
f3eb5384
SW
3786 for (sct = 0; sct < 8; sct++) {
3787 len += sprintf(page + len, " %02x",
ef19298b 3788 rcq[8 * sl + sct]);
f3eb5384
SW
3789 }
3790 len += sprintf(page + len, "\n");
3791 }
3792 sense = tsb->tsa.ddpc.sense;
3793 break;
3794 case 3: /* tsa_intrg */
3795 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
3796 " tsb->tsa.intrg.: not supportet yet \n");
3797 break;
3798 }
3799
3800 if (sense) {
3801 for (sl = 0; sl < 4; sl++) {
3802 len += sprintf(page + len,
3803 KERN_ERR PRINTK_HEADER
3804 " Sense(hex) %2d-%2d:",
3805 (8 * sl), ((8 * sl) + 7));
3806 for (sct = 0; sct < 8; sct++) {
3807 len += sprintf(page + len, " %02x",
3808 sense[8 * sl + sct]);
3809 }
3810 len += sprintf(page + len, "\n");
3811 }
3812
3813 if (sense[27] & DASD_SENSE_BIT_0) {
3814 /* 24 Byte Sense Data */
3815 sprintf(page + len, KERN_ERR PRINTK_HEADER
3816 " 24 Byte: %x MSG %x, "
3817 "%s MSGb to SYSOP\n",
3818 sense[7] >> 4, sense[7] & 0x0f,
3819 sense[1] & 0x10 ? "" : "no");
3820 } else {
3821 /* 32 Byte Sense Data */
3822 sprintf(page + len, KERN_ERR PRINTK_HEADER
3823 " 32 Byte: Format: %x "
3824 "Exception class %x\n",
3825 sense[6] & 0x0f, sense[22] >> 4);
3826 }
3827 } else {
3828 sprintf(page + len, KERN_ERR PRINTK_HEADER
3829 " SORRY - NO VALID SENSE AVAILABLE\n");
3830 }
3831 } else {
3832 sprintf(page + len, KERN_ERR PRINTK_HEADER
3833 " SORRY - NO TSB DATA AVAILABLE\n");
3834 }
3835 printk("%s", page);
3836 free_page((unsigned long) page);
3837}
3838
3839static void dasd_eckd_dump_sense(struct dasd_device *device,
3840 struct dasd_ccw_req *req, struct irb *irb)
3841{
a5a0061f 3842 if (scsw_is_tm(&irb->scsw))
f3eb5384
SW
3843 dasd_eckd_dump_sense_tcw(device, req, irb);
3844 else
3845 dasd_eckd_dump_sense_ccw(device, req, irb);
3846}
3847
501183f2 3848static int dasd_eckd_pm_freeze(struct dasd_device *device)
d41dd122
SH
3849{
3850 /*
3851 * the device should be disconnected from our LCU structure
3852 * on restore we will reconnect it and reread LCU specific
3853 * information like PAV support that might have changed
3854 */
3855 dasd_alias_remove_device(device);
3856 dasd_alias_disconnect_device_from_lcu(device);
3857
3858 return 0;
3859}
3860
501183f2 3861static int dasd_eckd_restore_device(struct dasd_device *device)
d41dd122
SH
3862{
3863 struct dasd_eckd_private *private;
6fca97a9 3864 struct dasd_eckd_characteristics temp_rdc_data;
d41dd122
SH
3865 int is_known, rc;
3866 struct dasd_uid temp_uid;
a7602f6c 3867 unsigned long flags;
d41dd122 3868
d41dd122
SH
3869 private = (struct dasd_eckd_private *) device->private;
3870
3871 /* Read Configuration Data */
3872 rc = dasd_eckd_read_conf(device);
3873 if (rc)
3874 goto out_err;
3875
2dedf0d9
SH
3876 dasd_eckd_get_uid(device, &temp_uid);
3877 /* Generate device unique id */
3878 rc = dasd_eckd_generate_uid(device);
3879 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
d41dd122 3880 if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
a7602f6c
SH
3881 dev_err(&device->cdev->dev, "The UID of the DASD has "
3882 "changed\n");
2dedf0d9 3883 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
d41dd122
SH
3884 if (rc)
3885 goto out_err;
d41dd122
SH
3886
3887 /* register lcu with alias handling, enable PAV if this is a new lcu */
3888 is_known = dasd_alias_make_device_known_to_lcu(device);
3889 if (is_known < 0)
3890 return is_known;
3891 if (!is_known) {
f4ac1d02
SW
3892 dasd_eckd_validate_server(device);
3893 dasd_alias_lcu_setup_complete(device);
3894 } else
3895 dasd_alias_wait_for_lcu_setup(device);
3896
3897 /* RE-Read Configuration Data */
3898 rc = dasd_eckd_read_conf(device);
3899 if (rc)
3900 goto out_err;
d41dd122
SH
3901
3902 /* Read Feature Codes */
68d1e5f0 3903 dasd_eckd_read_features(device);
d41dd122
SH
3904
3905 /* Read Device Characteristics */
68b781fe 3906 rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
6fca97a9 3907 &temp_rdc_data, 64);
d41dd122 3908 if (rc) {
b8ed5dd5
SH
3909 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
3910 "Read device characteristic failed, rc=%d", rc);
d41dd122
SH
3911 goto out_err;
3912 }
a7602f6c 3913 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
6fca97a9 3914 memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
a7602f6c 3915 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
d41dd122
SH
3916
3917 /* add device to alias management */
3918 dasd_alias_add_device(device);
3919
3920 return 0;
3921
3922out_err:
e6125fba 3923 return -1;
d41dd122
SH
3924}
3925
501183f2
SH
3926static int dasd_eckd_reload_device(struct dasd_device *device)
3927{
3928 struct dasd_eckd_private *private;
3929 int rc, old_base;
2dedf0d9
SH
3930 char print_uid[60];
3931 struct dasd_uid uid;
3932 unsigned long flags;
501183f2
SH
3933
3934 private = (struct dasd_eckd_private *) device->private;
2dedf0d9
SH
3935
3936 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
501183f2 3937 old_base = private->uid.base_unit_addr;
2dedf0d9
SH
3938 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
3939
501183f2
SH
3940 /* Read Configuration Data */
3941 rc = dasd_eckd_read_conf(device);
3942 if (rc)
3943 goto out_err;
3944
2dedf0d9 3945 rc = dasd_eckd_generate_uid(device);
501183f2
SH
3946 if (rc)
3947 goto out_err;
501183f2
SH
3948 /*
3949 * update unit address configuration and
3950 * add device to alias management
3951 */
3952 dasd_alias_update_add_device(device);
3953
2dedf0d9
SH
3954 dasd_eckd_get_uid(device, &uid);
3955
3956 if (old_base != uid.base_unit_addr) {
3957 if (strlen(uid.vduit) > 0)
3958 snprintf(print_uid, sizeof(print_uid),
3959 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
3960 uid.ssid, uid.base_unit_addr, uid.vduit);
501183f2 3961 else
2dedf0d9
SH
3962 snprintf(print_uid, sizeof(print_uid),
3963 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
3964 uid.ssid, uid.base_unit_addr);
501183f2
SH
3965
3966 dev_info(&device->cdev->dev,
3967 "An Alias device was reassigned to a new base device "
2dedf0d9 3968 "with UID: %s\n", print_uid);
501183f2
SH
3969 }
3970 return 0;
3971
3972out_err:
3973 return -1;
3974}
3975
d41dd122 3976static struct ccw_driver dasd_eckd_driver = {
3bda058b
SO
3977 .driver = {
3978 .name = "dasd-eckd",
3979 .owner = THIS_MODULE,
3980 },
d41dd122
SH
3981 .ids = dasd_eckd_ids,
3982 .probe = dasd_eckd_probe,
3983 .remove = dasd_generic_remove,
3984 .set_offline = dasd_generic_set_offline,
3985 .set_online = dasd_eckd_set_online,
3986 .notify = dasd_generic_notify,
a4d26c6a 3987 .path_event = dasd_generic_path_event,
d41dd122
SH
3988 .freeze = dasd_generic_pm_freeze,
3989 .thaw = dasd_generic_restore_device,
3990 .restore = dasd_generic_restore_device,
a23ed009 3991 .uc_handler = dasd_generic_uc_handler,
d41dd122 3992};
f3eb5384 3993
1da177e4
LT
3994/*
3995 * max_blocks is dependent on the amount of storage that is available
3996 * in the static io buffer for each device. Currently each device has
3997 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
3998 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
3999 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
4000 * addition we have one define extent ccw + 16 bytes of data and one
4001 * locate record ccw + 16 bytes of data. That makes:
4002 * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
4003 * We want to fit two into the available memory so that we can immediately
4004 * start the next request if one finishes off. That makes 249.5 blocks
4005 * for one request. Give a little safety and the result is 240.
4006 */
4007static struct dasd_discipline dasd_eckd_discipline = {
4008 .owner = THIS_MODULE,
4009 .name = "ECKD",
4010 .ebcname = "ECKD",
ef19298b 4011 .max_blocks = 190,
1da177e4 4012 .check_device = dasd_eckd_check_characteristics,
8e09f215 4013 .uncheck_device = dasd_eckd_uncheck_device,
1da177e4 4014 .do_analysis = dasd_eckd_do_analysis,
a4d26c6a 4015 .verify_path = dasd_eckd_verify_path,
8e09f215
SW
4016 .ready_to_online = dasd_eckd_ready_to_online,
4017 .online_to_ready = dasd_eckd_online_to_ready,
1da177e4
LT
4018 .fill_geometry = dasd_eckd_fill_geometry,
4019 .start_IO = dasd_start_IO,
4020 .term_IO = dasd_term_IO,
8e09f215 4021 .handle_terminated_request = dasd_eckd_handle_terminated_request,
1da177e4 4022 .format_device = dasd_eckd_format_device,
1da177e4
LT
4023 .erp_action = dasd_eckd_erp_action,
4024 .erp_postaction = dasd_eckd_erp_postaction,
5a27e60d 4025 .check_for_device_change = dasd_eckd_check_for_device_change,
8e09f215
SW
4026 .build_cp = dasd_eckd_build_alias_cp,
4027 .free_cp = dasd_eckd_free_alias_cp,
1da177e4 4028 .dump_sense = dasd_eckd_dump_sense,
fc19f381 4029 .dump_sense_dbf = dasd_eckd_dump_sense_dbf,
1da177e4 4030 .fill_info = dasd_eckd_fill_info,
1107ccfb 4031 .ioctl = dasd_eckd_ioctl,
d41dd122
SH
4032 .freeze = dasd_eckd_pm_freeze,
4033 .restore = dasd_eckd_restore_device,
501183f2 4034 .reload = dasd_eckd_reload_device,
2dedf0d9 4035 .get_uid = dasd_eckd_get_uid,
1da177e4
LT
4036};
4037
4038static int __init
4039dasd_eckd_init(void)
4040{
736e6ea0
SO
4041 int ret;
4042
1da177e4 4043 ASCEBC(dasd_eckd_discipline.ebcname, 4);
f932bcea
SW
4044 dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
4045 GFP_KERNEL | GFP_DMA);
4046 if (!dasd_reserve_req)
4047 return -ENOMEM;
a4d26c6a
SW
4048 path_verification_worker = kmalloc(sizeof(*path_verification_worker),
4049 GFP_KERNEL | GFP_DMA);
4050 if (!path_verification_worker) {
4051 kfree(dasd_reserve_req);
4052 return -ENOMEM;
4053 }
736e6ea0
SO
4054 ret = ccw_driver_register(&dasd_eckd_driver);
4055 if (!ret)
4056 wait_for_device_probe();
a4d26c6a
SW
4057 else {
4058 kfree(path_verification_worker);
f932bcea 4059 kfree(dasd_reserve_req);
a4d26c6a 4060 }
736e6ea0 4061 return ret;
1da177e4
LT
4062}
4063
4064static void __exit
4065dasd_eckd_cleanup(void)
4066{
4067 ccw_driver_unregister(&dasd_eckd_driver);
a4d26c6a 4068 kfree(path_verification_worker);
f932bcea 4069 kfree(dasd_reserve_req);
1da177e4
LT
4070}
4071
4072module_init(dasd_eckd_init);
4073module_exit(dasd_eckd_cleanup);