ata: add AMD Seattle platform driver
[deliverable/linux.git] / drivers / s390 / block / dasd_eckd.c
CommitLineData
138c014d 1/*
1da177e4 2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
138c014d 3 * Horst Hummel <Horst.Hummel@de.ibm.com>
1da177e4
LT
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
d41dd122 7 * Copyright IBM Corp. 1999, 2009
ab1d848f
NH
8 * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
9 * Author.........: Nigel Hislop <hislop_nigel@emc.com>
1da177e4
LT
10 */
11
ca99dab0 12#define KMSG_COMPONENT "dasd-eckd"
fc19f381 13
1da177e4
LT
14#include <linux/stddef.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/hdreg.h> /* HDIO_GETGEO */
18#include <linux/bio.h>
19#include <linux/module.h>
048cd4e5 20#include <linux/compat.h>
1da177e4
LT
21#include <linux/init.h>
22
382b7366 23#include <asm/css_chars.h>
1da177e4
LT
24#include <asm/debug.h>
25#include <asm/idals.h>
26#include <asm/ebcdic.h>
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>
5db8440c
SH
32#include <asm/schid.h>
33#include <asm/chpid.h>
1da177e4
LT
34
35#include "dasd_int.h"
36#include "dasd_eckd.h"
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
558b9ef0
SW
90static void *rawpadpage;
91
eb6e199b
SW
92#define INIT_CQR_OK 0
93#define INIT_CQR_UNFORMATTED 1
94#define INIT_CQR_ERROR 2
95
f932bcea
SW
96/* emergency request for reserve/release */
97static struct {
98 struct dasd_ccw_req cqr;
99 struct ccw1 ccw;
100 char data[32];
101} *dasd_reserve_req;
102static DEFINE_MUTEX(dasd_reserve_mutex);
103
a4d26c6a
SW
104/* definitions for the path verification worker */
105struct path_verification_work_data {
106 struct work_struct worker;
107 struct dasd_device *device;
108 struct dasd_ccw_req cqr;
109 struct ccw1 ccw;
110 __u8 rcd_buffer[DASD_ECKD_RCD_DATA_SIZE];
111 int isglobal;
112 __u8 tbvpm;
113};
114static struct path_verification_work_data *path_verification_worker;
115static DEFINE_MUTEX(dasd_path_verification_mutex);
eb6e199b 116
5db8440c
SH
117struct check_attention_work_data {
118 struct work_struct worker;
119 struct dasd_device *device;
120 __u8 lpum;
121};
122
1da177e4
LT
123/* initial attempt at a probe function. this can be simplified once
124 * the other detection code is gone */
125static int
126dasd_eckd_probe (struct ccw_device *cdev)
127{
128 int ret;
129
40545573 130 /* set ECKD specific ccw-device options */
454e1fa1
PO
131 ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
132 CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
40545573 133 if (ret) {
b8ed5dd5
SH
134 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
135 "dasd_eckd_probe: could not set "
136 "ccw-device options");
1da177e4 137 return ret;
40545573
HH
138 }
139 ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
140 return ret;
1da177e4
LT
141}
142
143static int
144dasd_eckd_set_online(struct ccw_device *cdev)
145{
40545573 146 return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
1da177e4
LT
147}
148
1da177e4
LT
149static const int sizes_trk0[] = { 28, 148, 84 };
150#define LABEL_SIZE 140
151
3bc9fef9
SH
152/* head and record addresses of count_area read in analysis ccw */
153static const int count_area_head[] = { 0, 0, 0, 0, 2 };
154static const int count_area_rec[] = { 1, 2, 3, 4, 1 };
155
1da177e4
LT
156static inline unsigned int
157round_up_multiple(unsigned int no, unsigned int mult)
158{
159 int rem = no % mult;
160 return (rem ? no - rem + mult : no);
161}
162
163static inline unsigned int
164ceil_quot(unsigned int d1, unsigned int d2)
165{
166 return (d1 + (d2 - 1)) / d2;
167}
168
4d284cac 169static unsigned int
1da177e4
LT
170recs_per_track(struct dasd_eckd_characteristics * rdc,
171 unsigned int kl, unsigned int dl)
172{
173 int dn, kn;
174
175 switch (rdc->dev_type) {
176 case 0x3380:
177 if (kl)
178 return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
179 ceil_quot(dl + 12, 32));
180 else
181 return 1499 / (15 + ceil_quot(dl + 12, 32));
182 case 0x3390:
183 dn = ceil_quot(dl + 6, 232) + 1;
184 if (kl) {
185 kn = ceil_quot(kl + 6, 232) + 1;
186 return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
187 9 + ceil_quot(dl + 6 * dn, 34));
188 } else
189 return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
190 case 0x9345:
191 dn = ceil_quot(dl + 6, 232) + 1;
192 if (kl) {
193 kn = ceil_quot(kl + 6, 232) + 1;
194 return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
195 ceil_quot(dl + 6 * dn, 34));
196 } else
197 return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
198 }
199 return 0;
200}
201
b44b0ab3
SW
202static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
203{
204 geo->cyl = (__u16) cyl;
205 geo->head = cyl >> 16;
206 geo->head <<= 4;
207 geo->head |= head;
208}
209
4d284cac 210static int
1da177e4
LT
211check_XRC (struct ccw1 *de_ccw,
212 struct DE_eckd_data *data,
213 struct dasd_device *device)
214{
543691a4 215 struct dasd_eckd_private *private = device->private;
d54853ef 216 int rc;
1da177e4 217
d54853ef
MS
218 if (!private->rdc_data.facilities.XRC_supported)
219 return 0;
1da177e4
LT
220
221 /* switch on System Time Stamp - needed for XRC Support */
d54853ef
MS
222 data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid' */
223 data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
1da177e4 224
d54853ef
MS
225 rc = get_sync_clock(&data->ep_sys_time);
226 /* Ignore return code if sync clock is switched off. */
a8f6db4d 227 if (rc == -EOPNOTSUPP || rc == -EACCES)
d54853ef 228 rc = 0;
1da177e4 229
8e09f215 230 de_ccw->count = sizeof(struct DE_eckd_data);
d54853ef
MS
231 de_ccw->flags |= CCW_FLAG_SLI;
232 return rc;
233}
1da177e4 234
4d284cac 235static int
b44b0ab3
SW
236define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
237 unsigned int totrk, int cmd, struct dasd_device *device)
1da177e4 238{
543691a4 239 struct dasd_eckd_private *private = device->private;
b44b0ab3
SW
240 u32 begcyl, endcyl;
241 u16 heads, beghead, endhead;
d54853ef 242 int rc = 0;
1da177e4 243
1da177e4
LT
244 ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
245 ccw->flags = 0;
246 ccw->count = 16;
247 ccw->cda = (__u32) __pa(data);
248
8e09f215 249 memset(data, 0, sizeof(struct DE_eckd_data));
1da177e4
LT
250 switch (cmd) {
251 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
252 case DASD_ECKD_CCW_READ_RECORD_ZERO:
253 case DASD_ECKD_CCW_READ:
254 case DASD_ECKD_CCW_READ_MT:
255 case DASD_ECKD_CCW_READ_CKD:
256 case DASD_ECKD_CCW_READ_CKD_MT:
257 case DASD_ECKD_CCW_READ_KD:
258 case DASD_ECKD_CCW_READ_KD_MT:
259 case DASD_ECKD_CCW_READ_COUNT:
260 data->mask.perm = 0x1;
261 data->attributes.operation = private->attrib.operation;
262 break;
263 case DASD_ECKD_CCW_WRITE:
264 case DASD_ECKD_CCW_WRITE_MT:
265 case DASD_ECKD_CCW_WRITE_KD:
266 case DASD_ECKD_CCW_WRITE_KD_MT:
267 data->mask.perm = 0x02;
268 data->attributes.operation = private->attrib.operation;
d54853ef 269 rc = check_XRC (ccw, data, device);
1da177e4
LT
270 break;
271 case DASD_ECKD_CCW_WRITE_CKD:
272 case DASD_ECKD_CCW_WRITE_CKD_MT:
273 data->attributes.operation = DASD_BYPASS_CACHE;
d54853ef 274 rc = check_XRC (ccw, data, device);
1da177e4
LT
275 break;
276 case DASD_ECKD_CCW_ERASE:
277 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
278 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
279 data->mask.perm = 0x3;
280 data->mask.auth = 0x1;
281 data->attributes.operation = DASD_BYPASS_CACHE;
d54853ef 282 rc = check_XRC (ccw, data, device);
1da177e4
LT
283 break;
284 default:
fc19f381
SH
285 dev_err(&device->cdev->dev,
286 "0x%x is not a known command\n", cmd);
1da177e4
LT
287 break;
288 }
289
290 data->attributes.mode = 0x3; /* ECKD */
291
292 if ((private->rdc_data.cu_type == 0x2105 ||
293 private->rdc_data.cu_type == 0x2107 ||
294 private->rdc_data.cu_type == 0x1750)
295 && !(private->uses_cdl && trk < 2))
296 data->ga_extended |= 0x40; /* Regular Data Format Mode */
297
b44b0ab3
SW
298 heads = private->rdc_data.trk_per_cyl;
299 begcyl = trk / heads;
300 beghead = trk % heads;
301 endcyl = totrk / heads;
302 endhead = totrk % heads;
1da177e4
LT
303
304 /* check for sequential prestage - enhance cylinder range */
305 if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
306 data->attributes.operation == DASD_SEQ_ACCESS) {
138c014d 307
b44b0ab3
SW
308 if (endcyl + private->attrib.nr_cyl < private->real_cyl)
309 endcyl += private->attrib.nr_cyl;
1da177e4 310 else
b44b0ab3 311 endcyl = (private->real_cyl - 1);
1da177e4
LT
312 }
313
b44b0ab3
SW
314 set_ch_t(&data->beg_ext, begcyl, beghead);
315 set_ch_t(&data->end_ext, endcyl, endhead);
d54853ef 316 return rc;
1da177e4
LT
317}
318
8e09f215
SW
319static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
320 struct dasd_device *device)
321{
543691a4 322 struct dasd_eckd_private *private = device->private;
8e09f215
SW
323 int rc;
324
8e09f215
SW
325 if (!private->rdc_data.facilities.XRC_supported)
326 return 0;
327
328 /* switch on System Time Stamp - needed for XRC Support */
f3eb5384
SW
329 pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid' */
330 pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
8e09f215
SW
331 pfxdata->validity.time_stamp = 1; /* 'Time Stamp Valid' */
332
f3eb5384 333 rc = get_sync_clock(&pfxdata->define_extent.ep_sys_time);
8e09f215 334 /* Ignore return code if sync clock is switched off. */
a8f6db4d 335 if (rc == -EOPNOTSUPP || rc == -EACCES)
8e09f215
SW
336 rc = 0;
337 return rc;
338}
339
f3eb5384
SW
340static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
341 unsigned int rec_on_trk, int count, int cmd,
342 struct dasd_device *device, unsigned int reclen,
343 unsigned int tlf)
344{
543691a4 345 struct dasd_eckd_private *private = device->private;
f3eb5384
SW
346 int sector;
347 int dn, d;
348
f3eb5384
SW
349 memset(data, 0, sizeof(*data));
350 sector = 0;
351 if (rec_on_trk) {
352 switch (private->rdc_data.dev_type) {
353 case 0x3390:
354 dn = ceil_quot(reclen + 6, 232);
355 d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
356 sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
357 break;
358 case 0x3380:
359 d = 7 + ceil_quot(reclen + 12, 32);
360 sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
361 break;
362 }
363 }
364 data->sector = sector;
365 /* note: meaning of count depends on the operation
366 * for record based I/O it's the number of records, but for
367 * track based I/O it's the number of tracks
368 */
369 data->count = count;
370 switch (cmd) {
371 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
372 data->operation.orientation = 0x3;
373 data->operation.operation = 0x03;
374 break;
375 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
376 data->operation.orientation = 0x3;
377 data->operation.operation = 0x16;
378 break;
379 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
380 data->operation.orientation = 0x1;
381 data->operation.operation = 0x03;
382 data->count++;
383 break;
384 case DASD_ECKD_CCW_READ_RECORD_ZERO:
385 data->operation.orientation = 0x3;
386 data->operation.operation = 0x16;
387 data->count++;
388 break;
389 case DASD_ECKD_CCW_WRITE:
390 case DASD_ECKD_CCW_WRITE_MT:
391 case DASD_ECKD_CCW_WRITE_KD:
392 case DASD_ECKD_CCW_WRITE_KD_MT:
393 data->auxiliary.length_valid = 0x1;
394 data->length = reclen;
395 data->operation.operation = 0x01;
396 break;
397 case DASD_ECKD_CCW_WRITE_CKD:
398 case DASD_ECKD_CCW_WRITE_CKD_MT:
399 data->auxiliary.length_valid = 0x1;
400 data->length = reclen;
401 data->operation.operation = 0x03;
402 break;
e4dbb0f2
SH
403 case DASD_ECKD_CCW_WRITE_FULL_TRACK:
404 data->operation.orientation = 0x0;
405 data->operation.operation = 0x3F;
406 data->extended_operation = 0x11;
407 data->length = 0;
408 data->extended_parameter_length = 0x02;
409 if (data->count > 8) {
410 data->extended_parameter[0] = 0xFF;
411 data->extended_parameter[1] = 0xFF;
412 data->extended_parameter[1] <<= (16 - count);
413 } else {
414 data->extended_parameter[0] = 0xFF;
415 data->extended_parameter[0] <<= (8 - count);
416 data->extended_parameter[1] = 0x00;
417 }
418 data->sector = 0xFF;
419 break;
f3eb5384
SW
420 case DASD_ECKD_CCW_WRITE_TRACK_DATA:
421 data->auxiliary.length_valid = 0x1;
422 data->length = reclen; /* not tlf, as one might think */
423 data->operation.operation = 0x3F;
424 data->extended_operation = 0x23;
425 break;
426 case DASD_ECKD_CCW_READ:
427 case DASD_ECKD_CCW_READ_MT:
428 case DASD_ECKD_CCW_READ_KD:
429 case DASD_ECKD_CCW_READ_KD_MT:
430 data->auxiliary.length_valid = 0x1;
431 data->length = reclen;
432 data->operation.operation = 0x06;
433 break;
434 case DASD_ECKD_CCW_READ_CKD:
435 case DASD_ECKD_CCW_READ_CKD_MT:
436 data->auxiliary.length_valid = 0x1;
437 data->length = reclen;
438 data->operation.operation = 0x16;
439 break;
440 case DASD_ECKD_CCW_READ_COUNT:
441 data->operation.operation = 0x06;
442 break;
e4dbb0f2
SH
443 case DASD_ECKD_CCW_READ_TRACK:
444 data->operation.orientation = 0x1;
445 data->operation.operation = 0x0C;
446 data->extended_parameter_length = 0;
447 data->sector = 0xFF;
448 break;
f3eb5384
SW
449 case DASD_ECKD_CCW_READ_TRACK_DATA:
450 data->auxiliary.length_valid = 0x1;
451 data->length = tlf;
452 data->operation.operation = 0x0C;
453 break;
454 case DASD_ECKD_CCW_ERASE:
455 data->length = reclen;
456 data->auxiliary.length_valid = 0x1;
457 data->operation.operation = 0x0b;
458 break;
459 default:
460 DBF_DEV_EVENT(DBF_ERR, device,
461 "fill LRE unknown opcode 0x%x", cmd);
462 BUG();
463 }
464 set_ch_t(&data->seek_addr,
465 trk / private->rdc_data.trk_per_cyl,
466 trk % private->rdc_data.trk_per_cyl);
467 data->search_arg.cyl = data->seek_addr.cyl;
468 data->search_arg.head = data->seek_addr.head;
469 data->search_arg.record = rec_on_trk;
470}
471
472static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
473 unsigned int trk, unsigned int totrk, int cmd,
474 struct dasd_device *basedev, struct dasd_device *startdev,
475 unsigned char format, unsigned int rec_on_trk, int count,
476 unsigned int blksize, unsigned int tlf)
8e09f215
SW
477{
478 struct dasd_eckd_private *basepriv, *startpriv;
f3eb5384
SW
479 struct DE_eckd_data *dedata;
480 struct LRE_eckd_data *lredata;
b44b0ab3
SW
481 u32 begcyl, endcyl;
482 u16 heads, beghead, endhead;
8e09f215
SW
483 int rc = 0;
484
543691a4
SO
485 basepriv = basedev->private;
486 startpriv = startdev->private;
f3eb5384
SW
487 dedata = &pfxdata->define_extent;
488 lredata = &pfxdata->locate_record;
8e09f215
SW
489
490 ccw->cmd_code = DASD_ECKD_CCW_PFX;
491 ccw->flags = 0;
e4dbb0f2
SH
492 if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK) {
493 ccw->count = sizeof(*pfxdata) + 2;
494 ccw->cda = (__u32) __pa(pfxdata);
495 memset(pfxdata, 0, sizeof(*pfxdata) + 2);
496 } else {
497 ccw->count = sizeof(*pfxdata);
498 ccw->cda = (__u32) __pa(pfxdata);
499 memset(pfxdata, 0, sizeof(*pfxdata));
500 }
8e09f215 501
8e09f215 502 /* prefix data */
f3eb5384
SW
503 if (format > 1) {
504 DBF_DEV_EVENT(DBF_ERR, basedev,
505 "PFX LRE unknown format 0x%x", format);
506 BUG();
507 return -EINVAL;
508 }
509 pfxdata->format = format;
4abb08c2
SW
510 pfxdata->base_address = basepriv->ned->unit_addr;
511 pfxdata->base_lss = basepriv->ned->ID;
f3eb5384 512 pfxdata->validity.define_extent = 1;
8e09f215
SW
513
514 /* private uid is kept up to date, conf_data may be outdated */
515 if (startpriv->uid.type != UA_BASE_DEVICE) {
516 pfxdata->validity.verify_base = 1;
517 if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
518 pfxdata->validity.hyper_pav = 1;
519 }
520
521 /* define extend data (mostly)*/
522 switch (cmd) {
523 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
524 case DASD_ECKD_CCW_READ_RECORD_ZERO:
525 case DASD_ECKD_CCW_READ:
526 case DASD_ECKD_CCW_READ_MT:
527 case DASD_ECKD_CCW_READ_CKD:
528 case DASD_ECKD_CCW_READ_CKD_MT:
529 case DASD_ECKD_CCW_READ_KD:
530 case DASD_ECKD_CCW_READ_KD_MT:
531 case DASD_ECKD_CCW_READ_COUNT:
f3eb5384
SW
532 dedata->mask.perm = 0x1;
533 dedata->attributes.operation = basepriv->attrib.operation;
534 break;
e4dbb0f2 535 case DASD_ECKD_CCW_READ_TRACK:
f3eb5384
SW
536 case DASD_ECKD_CCW_READ_TRACK_DATA:
537 dedata->mask.perm = 0x1;
538 dedata->attributes.operation = basepriv->attrib.operation;
539 dedata->blk_size = 0;
8e09f215
SW
540 break;
541 case DASD_ECKD_CCW_WRITE:
542 case DASD_ECKD_CCW_WRITE_MT:
543 case DASD_ECKD_CCW_WRITE_KD:
544 case DASD_ECKD_CCW_WRITE_KD_MT:
f3eb5384
SW
545 dedata->mask.perm = 0x02;
546 dedata->attributes.operation = basepriv->attrib.operation;
8e09f215
SW
547 rc = check_XRC_on_prefix(pfxdata, basedev);
548 break;
549 case DASD_ECKD_CCW_WRITE_CKD:
550 case DASD_ECKD_CCW_WRITE_CKD_MT:
f3eb5384 551 dedata->attributes.operation = DASD_BYPASS_CACHE;
8e09f215
SW
552 rc = check_XRC_on_prefix(pfxdata, basedev);
553 break;
554 case DASD_ECKD_CCW_ERASE:
555 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
556 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
f3eb5384
SW
557 dedata->mask.perm = 0x3;
558 dedata->mask.auth = 0x1;
559 dedata->attributes.operation = DASD_BYPASS_CACHE;
8e09f215
SW
560 rc = check_XRC_on_prefix(pfxdata, basedev);
561 break;
e4dbb0f2
SH
562 case DASD_ECKD_CCW_WRITE_FULL_TRACK:
563 dedata->mask.perm = 0x03;
564 dedata->attributes.operation = basepriv->attrib.operation;
565 dedata->blk_size = 0;
566 break;
f3eb5384
SW
567 case DASD_ECKD_CCW_WRITE_TRACK_DATA:
568 dedata->mask.perm = 0x02;
569 dedata->attributes.operation = basepriv->attrib.operation;
570 dedata->blk_size = blksize;
571 rc = check_XRC_on_prefix(pfxdata, basedev);
8e09f215 572 break;
f3eb5384
SW
573 default:
574 DBF_DEV_EVENT(DBF_ERR, basedev,
575 "PFX LRE unknown opcode 0x%x", cmd);
576 BUG();
577 return -EINVAL;
8e09f215
SW
578 }
579
f3eb5384 580 dedata->attributes.mode = 0x3; /* ECKD */
8e09f215
SW
581
582 if ((basepriv->rdc_data.cu_type == 0x2105 ||
583 basepriv->rdc_data.cu_type == 0x2107 ||
584 basepriv->rdc_data.cu_type == 0x1750)
585 && !(basepriv->uses_cdl && trk < 2))
f3eb5384 586 dedata->ga_extended |= 0x40; /* Regular Data Format Mode */
8e09f215 587
b44b0ab3
SW
588 heads = basepriv->rdc_data.trk_per_cyl;
589 begcyl = trk / heads;
590 beghead = trk % heads;
591 endcyl = totrk / heads;
592 endhead = totrk % heads;
8e09f215
SW
593
594 /* check for sequential prestage - enhance cylinder range */
f3eb5384
SW
595 if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
596 dedata->attributes.operation == DASD_SEQ_ACCESS) {
8e09f215 597
b44b0ab3
SW
598 if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
599 endcyl += basepriv->attrib.nr_cyl;
8e09f215 600 else
b44b0ab3 601 endcyl = (basepriv->real_cyl - 1);
8e09f215
SW
602 }
603
f3eb5384
SW
604 set_ch_t(&dedata->beg_ext, begcyl, beghead);
605 set_ch_t(&dedata->end_ext, endcyl, endhead);
606
607 if (format == 1) {
608 fill_LRE_data(lredata, trk, rec_on_trk, count, cmd,
609 basedev, blksize, tlf);
610 }
611
8e09f215
SW
612 return rc;
613}
614
f3eb5384
SW
615static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
616 unsigned int trk, unsigned int totrk, int cmd,
617 struct dasd_device *basedev, struct dasd_device *startdev)
618{
619 return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
620 0, 0, 0, 0, 0);
621}
622
4d284cac 623static void
b44b0ab3
SW
624locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
625 unsigned int rec_on_trk, int no_rec, int cmd,
1da177e4
LT
626 struct dasd_device * device, int reclen)
627{
543691a4 628 struct dasd_eckd_private *private = device->private;
1da177e4
LT
629 int sector;
630 int dn, d;
138c014d 631
1da177e4
LT
632 DBF_DEV_EVENT(DBF_INFO, device,
633 "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
634 trk, rec_on_trk, no_rec, cmd, reclen);
635
636 ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
637 ccw->flags = 0;
638 ccw->count = 16;
639 ccw->cda = (__u32) __pa(data);
640
8e09f215 641 memset(data, 0, sizeof(struct LO_eckd_data));
1da177e4
LT
642 sector = 0;
643 if (rec_on_trk) {
644 switch (private->rdc_data.dev_type) {
645 case 0x3390:
646 dn = ceil_quot(reclen + 6, 232);
647 d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
648 sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
649 break;
650 case 0x3380:
651 d = 7 + ceil_quot(reclen + 12, 32);
652 sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
653 break;
654 }
655 }
656 data->sector = sector;
657 data->count = no_rec;
658 switch (cmd) {
659 case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
660 data->operation.orientation = 0x3;
661 data->operation.operation = 0x03;
662 break;
663 case DASD_ECKD_CCW_READ_HOME_ADDRESS:
664 data->operation.orientation = 0x3;
665 data->operation.operation = 0x16;
666 break;
667 case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
668 data->operation.orientation = 0x1;
669 data->operation.operation = 0x03;
670 data->count++;
671 break;
672 case DASD_ECKD_CCW_READ_RECORD_ZERO:
673 data->operation.orientation = 0x3;
674 data->operation.operation = 0x16;
675 data->count++;
676 break;
677 case DASD_ECKD_CCW_WRITE:
678 case DASD_ECKD_CCW_WRITE_MT:
679 case DASD_ECKD_CCW_WRITE_KD:
680 case DASD_ECKD_CCW_WRITE_KD_MT:
681 data->auxiliary.last_bytes_used = 0x1;
682 data->length = reclen;
683 data->operation.operation = 0x01;
684 break;
685 case DASD_ECKD_CCW_WRITE_CKD:
686 case DASD_ECKD_CCW_WRITE_CKD_MT:
687 data->auxiliary.last_bytes_used = 0x1;
688 data->length = reclen;
689 data->operation.operation = 0x03;
690 break;
691 case DASD_ECKD_CCW_READ:
692 case DASD_ECKD_CCW_READ_MT:
693 case DASD_ECKD_CCW_READ_KD:
694 case DASD_ECKD_CCW_READ_KD_MT:
695 data->auxiliary.last_bytes_used = 0x1;
696 data->length = reclen;
697 data->operation.operation = 0x06;
698 break;
699 case DASD_ECKD_CCW_READ_CKD:
700 case DASD_ECKD_CCW_READ_CKD_MT:
701 data->auxiliary.last_bytes_used = 0x1;
702 data->length = reclen;
703 data->operation.operation = 0x16;
704 break;
705 case DASD_ECKD_CCW_READ_COUNT:
706 data->operation.operation = 0x06;
707 break;
708 case DASD_ECKD_CCW_ERASE:
709 data->length = reclen;
710 data->auxiliary.last_bytes_used = 0x1;
711 data->operation.operation = 0x0b;
712 break;
713 default:
fc19f381
SH
714 DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
715 "opcode 0x%x", cmd);
1da177e4 716 }
b44b0ab3
SW
717 set_ch_t(&data->seek_addr,
718 trk / private->rdc_data.trk_per_cyl,
719 trk % private->rdc_data.trk_per_cyl);
720 data->search_arg.cyl = data->seek_addr.cyl;
721 data->search_arg.head = data->seek_addr.head;
1da177e4
LT
722 data->search_arg.record = rec_on_trk;
723}
724
725/*
726 * Returns 1 if the block is one of the special blocks that needs
727 * to get read/written with the KD variant of the command.
728 * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
729 * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
730 * Luckily the KD variants differ only by one bit (0x08) from the
731 * normal variant. So don't wonder about code like:
732 * if (dasd_eckd_cdl_special(blk_per_trk, recid))
733 * ccw->cmd_code |= 0x8;
734 */
735static inline int
736dasd_eckd_cdl_special(int blk_per_trk, int recid)
737{
738 if (recid < 3)
739 return 1;
740 if (recid < blk_per_trk)
741 return 0;
742 if (recid < 2 * blk_per_trk)
743 return 1;
744 return 0;
745}
746
747/*
748 * Returns the record size for the special blocks of the cdl format.
749 * Only returns something useful if dasd_eckd_cdl_special is true
750 * for the recid.
751 */
752static inline int
753dasd_eckd_cdl_reclen(int recid)
754{
755 if (recid < 3)
756 return sizes_trk0[recid];
757 return LABEL_SIZE;
758}
b206181d
SH
759/* create unique id from private structure. */
760static void create_uid(struct dasd_eckd_private *private)
3d052595 761{
4abb08c2 762 int count;
b206181d 763 struct dasd_uid *uid;
3d052595 764
2dedf0d9 765 uid = &private->uid;
3d052595 766 memset(uid, 0, sizeof(struct dasd_uid));
4abb08c2 767 memcpy(uid->vendor, private->ned->HDA_manufacturer,
d0710c7c 768 sizeof(uid->vendor) - 1);
3d052595 769 EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
4abb08c2 770 memcpy(uid->serial, private->ned->HDA_location,
d0710c7c 771 sizeof(uid->serial) - 1);
3d052595 772 EBCASC(uid->serial, sizeof(uid->serial) - 1);
4abb08c2 773 uid->ssid = private->gneq->subsystemID;
a419aef8 774 uid->real_unit_addr = private->ned->unit_addr;
4abb08c2
SW
775 if (private->sneq) {
776 uid->type = private->sneq->sua_flags;
8e09f215 777 if (uid->type == UA_BASE_PAV_ALIAS)
4abb08c2 778 uid->base_unit_addr = private->sneq->base_unit_addr;
8e09f215
SW
779 } else {
780 uid->type = UA_BASE_DEVICE;
781 }
4abb08c2
SW
782 if (private->vdsneq) {
783 for (count = 0; count < 16; count++) {
784 sprintf(uid->vduit+2*count, "%02x",
785 private->vdsneq->uit[count]);
786 }
787 }
b206181d
SH
788}
789
790/*
791 * Generate device unique id that specifies the physical device.
792 */
793static int dasd_eckd_generate_uid(struct dasd_device *device)
794{
543691a4 795 struct dasd_eckd_private *private = device->private;
b206181d
SH
796 unsigned long flags;
797
b206181d
SH
798 if (!private)
799 return -ENODEV;
800 if (!private->ned || !private->gneq)
801 return -ENODEV;
802 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
803 create_uid(private);
2dedf0d9 804 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
3d052595
HH
805 return 0;
806}
807
2dedf0d9
SH
808static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
809{
543691a4 810 struct dasd_eckd_private *private = device->private;
2dedf0d9
SH
811 unsigned long flags;
812
543691a4 813 if (private) {
2dedf0d9
SH
814 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
815 *uid = private->uid;
816 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
817 return 0;
818 }
819 return -EINVAL;
820}
821
b206181d
SH
822/*
823 * compare device UID with data of a given dasd_eckd_private structure
824 * return 0 for match
825 */
826static int dasd_eckd_compare_path_uid(struct dasd_device *device,
827 struct dasd_eckd_private *private)
828{
829 struct dasd_uid device_uid;
830
831 create_uid(private);
832 dasd_eckd_get_uid(device, &device_uid);
833
834 return memcmp(&device_uid, &private->uid, sizeof(struct dasd_uid));
835}
836
a4d26c6a
SW
837static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
838 struct dasd_ccw_req *cqr,
839 __u8 *rcd_buffer,
840 __u8 lpm)
17283b56 841{
17283b56 842 struct ccw1 *ccw;
a4d26c6a
SW
843 /*
844 * buffer has to start with EBCDIC "V1.0" to show
845 * support for virtual device SNEQ
846 */
847 rcd_buffer[0] = 0xE5;
848 rcd_buffer[1] = 0xF1;
849 rcd_buffer[2] = 0x4B;
850 rcd_buffer[3] = 0xF0;
17283b56
CH
851
852 ccw = cqr->cpaddr;
a4d26c6a
SW
853 ccw->cmd_code = DASD_ECKD_CCW_RCD;
854 ccw->flags = 0;
17283b56 855 ccw->cda = (__u32)(addr_t)rcd_buffer;
a4d26c6a
SW
856 ccw->count = DASD_ECKD_RCD_DATA_SIZE;
857 cqr->magic = DASD_ECKD_MAGIC;
17283b56 858
8e09f215
SW
859 cqr->startdev = device;
860 cqr->memdev = device;
861 cqr->block = NULL;
17283b56
CH
862 cqr->expires = 10*HZ;
863 cqr->lpm = lpm;
eb6e199b 864 cqr->retries = 256;
1aae0560 865 cqr->buildclk = get_tod_clock();
17283b56 866 cqr->status = DASD_CQR_FILLED;
a4d26c6a
SW
867 set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
868}
869
5915a873
SH
870/*
871 * Wakeup helper for read_conf
872 * if the cqr is not done and needs some error recovery
873 * the buffer has to be re-initialized with the EBCDIC "V1.0"
874 * to show support for virtual device SNEQ
875 */
876static void read_conf_cb(struct dasd_ccw_req *cqr, void *data)
877{
878 struct ccw1 *ccw;
879 __u8 *rcd_buffer;
880
881 if (cqr->status != DASD_CQR_DONE) {
882 ccw = cqr->cpaddr;
883 rcd_buffer = (__u8 *)((addr_t) ccw->cda);
884 memset(rcd_buffer, 0, sizeof(*rcd_buffer));
885
886 rcd_buffer[0] = 0xE5;
887 rcd_buffer[1] = 0xF1;
888 rcd_buffer[2] = 0x4B;
889 rcd_buffer[3] = 0xF0;
890 }
891 dasd_wakeup_cb(cqr, data);
892}
893
a4d26c6a
SW
894static int dasd_eckd_read_conf_immediately(struct dasd_device *device,
895 struct dasd_ccw_req *cqr,
896 __u8 *rcd_buffer,
897 __u8 lpm)
898{
899 struct ciw *ciw;
900 int rc;
901 /*
902 * sanity check: scan for RCD command in extended SenseID data
903 * some devices do not support RCD
904 */
905 ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
906 if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD)
907 return -EOPNOTSUPP;
908
909 dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buffer, lpm);
910 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5a27e60d 911 set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
a4d26c6a 912 cqr->retries = 5;
5915a873 913 cqr->callback = read_conf_cb;
a4d26c6a
SW
914 rc = dasd_sleep_on_immediatly(cqr);
915 return rc;
17283b56
CH
916}
917
918static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
919 void **rcd_buffer,
920 int *rcd_buffer_size, __u8 lpm)
921{
922 struct ciw *ciw;
923 char *rcd_buf = NULL;
924 int ret;
925 struct dasd_ccw_req *cqr;
926
927 /*
a4d26c6a
SW
928 * sanity check: scan for RCD command in extended SenseID data
929 * some devices do not support RCD
17283b56
CH
930 */
931 ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
a4d26c6a 932 if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
17283b56
CH
933 ret = -EOPNOTSUPP;
934 goto out_error;
935 }
a4d26c6a 936 rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
17283b56
CH
937 if (!rcd_buf) {
938 ret = -ENOMEM;
939 goto out_error;
940 }
a4d26c6a
SW
941 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
942 0, /* use rcd_buf as data ara */
943 device);
17283b56 944 if (IS_ERR(cqr)) {
a4d26c6a
SW
945 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
946 "Could not allocate RCD request");
947 ret = -ENOMEM;
17283b56
CH
948 goto out_error;
949 }
a4d26c6a 950 dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
5915a873 951 cqr->callback = read_conf_cb;
17283b56
CH
952 ret = dasd_sleep_on(cqr);
953 /*
954 * on success we update the user input parms
955 */
8e09f215 956 dasd_sfree_request(cqr, cqr->memdev);
17283b56
CH
957 if (ret)
958 goto out_error;
959
a4d26c6a 960 *rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
17283b56
CH
961 *rcd_buffer = rcd_buf;
962 return 0;
963out_error:
964 kfree(rcd_buf);
965 *rcd_buffer = NULL;
966 *rcd_buffer_size = 0;
967 return ret;
968}
969
4abb08c2
SW
970static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
971{
972
973 struct dasd_sneq *sneq;
974 int i, count;
975
976 private->ned = NULL;
977 private->sneq = NULL;
978 private->vdsneq = NULL;
979 private->gneq = NULL;
980 count = private->conf_len / sizeof(struct dasd_sneq);
981 sneq = (struct dasd_sneq *)private->conf_data;
982 for (i = 0; i < count; ++i) {
983 if (sneq->flags.identifier == 1 && sneq->format == 1)
984 private->sneq = sneq;
985 else if (sneq->flags.identifier == 1 && sneq->format == 4)
986 private->vdsneq = (struct vd_sneq *)sneq;
987 else if (sneq->flags.identifier == 2)
988 private->gneq = (struct dasd_gneq *)sneq;
989 else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
990 private->ned = (struct dasd_ned *)sneq;
991 sneq++;
992 }
993 if (!private->ned || !private->gneq) {
994 private->ned = NULL;
995 private->sneq = NULL;
996 private->vdsneq = NULL;
997 private->gneq = NULL;
998 return -EINVAL;
999 }
1000 return 0;
1001
1002};
1003
1004static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
1005{
1006 struct dasd_gneq *gneq;
1007 int i, count, found;
1008
1009 count = conf_len / sizeof(*gneq);
1010 gneq = (struct dasd_gneq *)conf_data;
1011 found = 0;
1012 for (i = 0; i < count; ++i) {
1013 if (gneq->flags.identifier == 2) {
1014 found = 1;
1015 break;
1016 }
1017 gneq++;
1018 }
1019 if (found)
1020 return ((char *)gneq)[18] & 0x07;
1021 else
1022 return 0;
1023}
1024
c7c0c9de
SH
1025static void dasd_eckd_clear_conf_data(struct dasd_device *device)
1026{
543691a4 1027 struct dasd_eckd_private *private = device->private;
c7c0c9de
SH
1028 int i;
1029
c7c0c9de
SH
1030 private->conf_data = NULL;
1031 private->conf_len = 0;
1032 for (i = 0; i < 8; i++) {
1033 kfree(private->path_conf_data[i]);
1034 private->path_conf_data[i] = NULL;
1035 }
1036}
1037
1038
4abb08c2 1039static int dasd_eckd_read_conf(struct dasd_device *device)
1da177e4
LT
1040{
1041 void *conf_data;
1042 int conf_len, conf_data_saved;
b179b037 1043 int rc, path_err, pos;
a4d26c6a 1044 __u8 lpm, opm;
b206181d 1045 struct dasd_eckd_private *private, path_private;
a4d26c6a 1046 struct dasd_path *path_data;
b206181d
SH
1047 struct dasd_uid *uid;
1048 char print_path_uid[60], print_device_uid[60];
1da177e4 1049
543691a4 1050 private = device->private;
a4d26c6a
SW
1051 path_data = &device->path_data;
1052 opm = ccw_device_get_path_mask(device->cdev);
1da177e4 1053 conf_data_saved = 0;
55d3a85c 1054 path_err = 0;
1da177e4
LT
1055 /* get configuration data per operational path */
1056 for (lpm = 0x80; lpm; lpm>>= 1) {
b206181d
SH
1057 if (!(lpm & opm))
1058 continue;
1059 rc = dasd_eckd_read_conf_lpm(device, &conf_data,
1060 &conf_len, lpm);
1061 if (rc && rc != -EOPNOTSUPP) { /* -EOPNOTSUPP is ok */
1062 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1063 "Read configuration data returned "
1064 "error %d", rc);
1065 return rc;
1066 }
1067 if (conf_data == NULL) {
1068 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1069 "No configuration data "
1070 "retrieved");
1071 /* no further analysis possible */
1072 path_data->opm |= lpm;
1073 continue; /* no error */
1074 }
1075 /* save first valid configuration data */
1076 if (!conf_data_saved) {
c7c0c9de
SH
1077 /* initially clear previously stored conf_data */
1078 dasd_eckd_clear_conf_data(device);
b206181d
SH
1079 private->conf_data = conf_data;
1080 private->conf_len = conf_len;
1081 if (dasd_eckd_identify_conf_parts(private)) {
1082 private->conf_data = NULL;
1083 private->conf_len = 0;
1084 kfree(conf_data);
1085 continue;
1da177e4 1086 }
c7c0c9de
SH
1087 pos = pathmask_to_pos(lpm);
1088 /* store per path conf_data */
1089 private->path_conf_data[pos] =
1090 (struct dasd_conf_data *) conf_data;
b206181d
SH
1091 /*
1092 * build device UID that other path data
1093 * can be compared to it
1094 */
1095 dasd_eckd_generate_uid(device);
1096 conf_data_saved++;
1097 } else {
1098 path_private.conf_data = conf_data;
1099 path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1100 if (dasd_eckd_identify_conf_parts(
1101 &path_private)) {
1102 path_private.conf_data = NULL;
1103 path_private.conf_len = 0;
1104 kfree(conf_data);
1105 continue;
1da177e4 1106 }
b206181d
SH
1107 if (dasd_eckd_compare_path_uid(
1108 device, &path_private)) {
1109 uid = &path_private.uid;
1110 if (strlen(uid->vduit) > 0)
1111 snprintf(print_path_uid,
1112 sizeof(print_path_uid),
1113 "%s.%s.%04x.%02x.%s",
1114 uid->vendor, uid->serial,
1115 uid->ssid, uid->real_unit_addr,
1116 uid->vduit);
1117 else
1118 snprintf(print_path_uid,
1119 sizeof(print_path_uid),
1120 "%s.%s.%04x.%02x",
1121 uid->vendor, uid->serial,
1122 uid->ssid,
1123 uid->real_unit_addr);
1124 uid = &private->uid;
1125 if (strlen(uid->vduit) > 0)
1126 snprintf(print_device_uid,
1127 sizeof(print_device_uid),
1128 "%s.%s.%04x.%02x.%s",
1129 uid->vendor, uid->serial,
1130 uid->ssid, uid->real_unit_addr,
1131 uid->vduit);
1132 else
1133 snprintf(print_device_uid,
1134 sizeof(print_device_uid),
1135 "%s.%s.%04x.%02x",
1136 uid->vendor, uid->serial,
1137 uid->ssid,
1138 uid->real_unit_addr);
1139 dev_err(&device->cdev->dev,
1140 "Not all channel paths lead to "
1141 "the same device, path %02X leads to "
1142 "device %s instead of %s\n", lpm,
1143 print_path_uid, print_device_uid);
55d3a85c 1144 path_err = -EINVAL;
5db8440c 1145 path_data->cablepm |= lpm;
55d3a85c 1146 continue;
1da177e4 1147 }
c7c0c9de
SH
1148 pos = pathmask_to_pos(lpm);
1149 /* store per path conf_data */
1150 private->path_conf_data[pos] =
1151 (struct dasd_conf_data *) conf_data;
b206181d
SH
1152 path_private.conf_data = NULL;
1153 path_private.conf_len = 0;
1154 }
1155 switch (dasd_eckd_path_access(conf_data, conf_len)) {
1156 case 0x02:
1157 path_data->npm |= lpm;
1158 break;
1159 case 0x03:
1160 path_data->ppm |= lpm;
1161 break;
1da177e4 1162 }
ccc0e7dc
SH
1163 if (!path_data->opm) {
1164 path_data->opm = lpm;
1165 dasd_generic_path_operational(device);
1166 } else {
1167 path_data->opm |= lpm;
1168 }
5db8440c
SH
1169 /*
1170 * if the path is used
1171 * it should not be in one of the negative lists
1172 */
1173 path_data->cablepm &= ~lpm;
1174 path_data->hpfpm &= ~lpm;
1175 path_data->cuirpm &= ~lpm;
1da177e4 1176 }
b206181d 1177
55d3a85c 1178 return path_err;
1da177e4
LT
1179}
1180
a4d26c6a
SW
1181static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
1182{
543691a4 1183 struct dasd_eckd_private *private = device->private;
a4d26c6a
SW
1184 int mdc;
1185 u32 fcx_max_data;
1186
a4d26c6a
SW
1187 if (private->fcx_max_data) {
1188 mdc = ccw_device_get_mdc(device->cdev, lpm);
1189 if ((mdc < 0)) {
1190 dev_warn(&device->cdev->dev,
1191 "Detecting the maximum data size for zHPF "
1192 "requests failed (rc=%d) for a new path %x\n",
1193 mdc, lpm);
1194 return mdc;
1195 }
1196 fcx_max_data = mdc * FCX_MAX_DATA_FACTOR;
1197 if (fcx_max_data < private->fcx_max_data) {
1198 dev_warn(&device->cdev->dev,
1199 "The maximum data size for zHPF requests %u "
1200 "on a new path %x is below the active maximum "
1201 "%u\n", fcx_max_data, lpm,
1202 private->fcx_max_data);
1203 return -EACCES;
1204 }
1205 }
1206 return 0;
1207}
1208
b206181d
SH
1209static int rebuild_device_uid(struct dasd_device *device,
1210 struct path_verification_work_data *data)
1211{
543691a4
SO
1212 struct dasd_eckd_private *private = device->private;
1213 struct dasd_path *path_data = &device->path_data;
1214 __u8 lpm, opm = path_data->opm;
1215 int rc = -ENODEV;
b206181d
SH
1216
1217 for (lpm = 0x80; lpm; lpm >>= 1) {
1218 if (!(lpm & opm))
1219 continue;
1220 memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1221 memset(&data->cqr, 0, sizeof(data->cqr));
1222 data->cqr.cpaddr = &data->ccw;
1223 rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1224 data->rcd_buffer,
1225 lpm);
1226
1227 if (rc) {
1228 if (rc == -EOPNOTSUPP) /* -EOPNOTSUPP is ok */
1229 continue;
1230 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1231 "Read configuration data "
1232 "returned error %d", rc);
1233 break;
1234 }
1235 memcpy(private->conf_data, data->rcd_buffer,
1236 DASD_ECKD_RCD_DATA_SIZE);
1237 if (dasd_eckd_identify_conf_parts(private)) {
1238 rc = -ENODEV;
1239 } else /* first valid path is enough */
1240 break;
1241 }
1242
1243 if (!rc)
1244 rc = dasd_eckd_generate_uid(device);
1245
1246 return rc;
1247}
1248
a4d26c6a
SW
1249static void do_path_verification_work(struct work_struct *work)
1250{
1251 struct path_verification_work_data *data;
1252 struct dasd_device *device;
b206181d
SH
1253 struct dasd_eckd_private path_private;
1254 struct dasd_uid *uid;
1255 __u8 path_rcd_buf[DASD_ECKD_RCD_DATA_SIZE];
5db8440c 1256 __u8 lpm, opm, npm, ppm, epm, hpfpm, cablepm;
a4d26c6a 1257 unsigned long flags;
b206181d 1258 char print_uid[60];
a4d26c6a
SW
1259 int rc;
1260
1261 data = container_of(work, struct path_verification_work_data, worker);
1262 device = data->device;
1263
c8d1c0ff
SH
1264 /* delay path verification until device was resumed */
1265 if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
1266 schedule_work(work);
1267 return;
1268 }
1eb38023
SH
1269 /* check if path verification already running and delay if so */
1270 if (test_and_set_bit(DASD_FLAG_PATH_VERIFY, &device->flags)) {
1271 schedule_work(work);
1272 return;
1273 }
a4d26c6a
SW
1274 opm = 0;
1275 npm = 0;
1276 ppm = 0;
1277 epm = 0;
5db8440c
SH
1278 hpfpm = 0;
1279 cablepm = 0;
1280
a4d26c6a 1281 for (lpm = 0x80; lpm; lpm >>= 1) {
b206181d
SH
1282 if (!(lpm & data->tbvpm))
1283 continue;
1284 memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1285 memset(&data->cqr, 0, sizeof(data->cqr));
1286 data->cqr.cpaddr = &data->ccw;
1287 rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1288 data->rcd_buffer,
1289 lpm);
1290 if (!rc) {
1291 switch (dasd_eckd_path_access(data->rcd_buffer,
1292 DASD_ECKD_RCD_DATA_SIZE)
1293 ) {
1294 case 0x02:
1295 npm |= lpm;
1296 break;
1297 case 0x03:
1298 ppm |= lpm;
1299 break;
1300 }
1301 opm |= lpm;
1302 } else if (rc == -EOPNOTSUPP) {
1303 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1304 "path verification: No configuration "
1305 "data retrieved");
1306 opm |= lpm;
1307 } else if (rc == -EAGAIN) {
1308 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
a4d26c6a
SW
1309 "path verification: device is stopped,"
1310 " try again later");
b206181d
SH
1311 epm |= lpm;
1312 } else {
1313 dev_warn(&device->cdev->dev,
1314 "Reading device feature codes failed "
1315 "(rc=%d) for new path %x\n", rc, lpm);
1316 continue;
1317 }
1318 if (verify_fcx_max_data(device, lpm)) {
1319 opm &= ~lpm;
1320 npm &= ~lpm;
1321 ppm &= ~lpm;
5db8440c 1322 hpfpm |= lpm;
b206181d
SH
1323 continue;
1324 }
1325
1326 /*
1327 * save conf_data for comparison after
1328 * rebuild_device_uid may have changed
1329 * the original data
1330 */
1331 memcpy(&path_rcd_buf, data->rcd_buffer,
1332 DASD_ECKD_RCD_DATA_SIZE);
1333 path_private.conf_data = (void *) &path_rcd_buf;
1334 path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1335 if (dasd_eckd_identify_conf_parts(&path_private)) {
1336 path_private.conf_data = NULL;
1337 path_private.conf_len = 0;
1338 continue;
1339 }
1340
1341 /*
1342 * compare path UID with device UID only if at least
1343 * one valid path is left
1344 * in other case the device UID may have changed and
1345 * the first working path UID will be used as device UID
1346 */
1347 if (device->path_data.opm &&
1348 dasd_eckd_compare_path_uid(device, &path_private)) {
1349 /*
1350 * the comparison was not successful
1351 * rebuild the device UID with at least one
1352 * known path in case a z/VM hyperswap command
1353 * has changed the device
1354 *
1355 * after this compare again
1356 *
1357 * if either the rebuild or the recompare fails
1358 * the path can not be used
1359 */
1360 if (rebuild_device_uid(device, data) ||
1361 dasd_eckd_compare_path_uid(
1362 device, &path_private)) {
1363 uid = &path_private.uid;
1364 if (strlen(uid->vduit) > 0)
1365 snprintf(print_uid, sizeof(print_uid),
1366 "%s.%s.%04x.%02x.%s",
1367 uid->vendor, uid->serial,
1368 uid->ssid, uid->real_unit_addr,
1369 uid->vduit);
1370 else
1371 snprintf(print_uid, sizeof(print_uid),
1372 "%s.%s.%04x.%02x",
1373 uid->vendor, uid->serial,
1374 uid->ssid,
1375 uid->real_unit_addr);
1376 dev_err(&device->cdev->dev,
1377 "The newly added channel path %02X "
1378 "will not be used because it leads "
1379 "to a different device %s\n",
1380 lpm, print_uid);
a4d26c6a
SW
1381 opm &= ~lpm;
1382 npm &= ~lpm;
1383 ppm &= ~lpm;
5db8440c 1384 cablepm |= lpm;
b206181d 1385 continue;
a4d26c6a
SW
1386 }
1387 }
b206181d
SH
1388
1389 /*
1390 * There is a small chance that a path is lost again between
1391 * above path verification and the following modification of
1392 * the device opm mask. We could avoid that race here by using
1393 * yet another path mask, but we rather deal with this unlikely
1394 * situation in dasd_start_IO.
1395 */
1396 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1397 if (!device->path_data.opm && opm) {
1398 device->path_data.opm = opm;
5db8440c
SH
1399 device->path_data.cablepm &= ~opm;
1400 device->path_data.cuirpm &= ~opm;
1401 device->path_data.hpfpm &= ~opm;
b206181d 1402 dasd_generic_path_operational(device);
5db8440c 1403 } else {
b206181d 1404 device->path_data.opm |= opm;
5db8440c
SH
1405 device->path_data.cablepm &= ~opm;
1406 device->path_data.cuirpm &= ~opm;
1407 device->path_data.hpfpm &= ~opm;
1408 }
b206181d
SH
1409 device->path_data.npm |= npm;
1410 device->path_data.ppm |= ppm;
1411 device->path_data.tbvpm |= epm;
5db8440c
SH
1412 device->path_data.cablepm |= cablepm;
1413 device->path_data.hpfpm |= hpfpm;
b206181d 1414 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
a4d26c6a 1415 }
1eb38023 1416 clear_bit(DASD_FLAG_PATH_VERIFY, &device->flags);
a4d26c6a
SW
1417 dasd_put_device(device);
1418 if (data->isglobal)
1419 mutex_unlock(&dasd_path_verification_mutex);
1420 else
1421 kfree(data);
1422}
1423
1424static int dasd_eckd_verify_path(struct dasd_device *device, __u8 lpm)
1425{
1426 struct path_verification_work_data *data;
1427
1428 data = kmalloc(sizeof(*data), GFP_ATOMIC | GFP_DMA);
1429 if (!data) {
1430 if (mutex_trylock(&dasd_path_verification_mutex)) {
1431 data = path_verification_worker;
1432 data->isglobal = 1;
1433 } else
1434 return -ENOMEM;
1435 } else {
1436 memset(data, 0, sizeof(*data));
1437 data->isglobal = 0;
1438 }
1439 INIT_WORK(&data->worker, do_path_verification_work);
1440 dasd_get_device(device);
1441 data->device = device;
1442 data->tbvpm = lpm;
1443 schedule_work(&data->worker);
1444 return 0;
1445}
1446
8e09f215
SW
1447static int dasd_eckd_read_features(struct dasd_device *device)
1448{
543691a4 1449 struct dasd_eckd_private *private = device->private;
8e09f215
SW
1450 struct dasd_psf_prssd_data *prssdp;
1451 struct dasd_rssd_features *features;
1452 struct dasd_ccw_req *cqr;
1453 struct ccw1 *ccw;
1454 int rc;
8e09f215 1455
68d1e5f0 1456 memset(&private->features, 0, sizeof(struct dasd_rssd_features));
68b781fe 1457 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
8e09f215
SW
1458 (sizeof(struct dasd_psf_prssd_data) +
1459 sizeof(struct dasd_rssd_features)),
1460 device);
1461 if (IS_ERR(cqr)) {
b8ed5dd5
SH
1462 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
1463 "allocate initialization request");
8e09f215
SW
1464 return PTR_ERR(cqr);
1465 }
1466 cqr->startdev = device;
1467 cqr->memdev = device;
1468 cqr->block = NULL;
eb6e199b 1469 cqr->retries = 256;
8e09f215
SW
1470 cqr->expires = 10 * HZ;
1471
1472 /* Prepare for Read Subsystem Data */
1473 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1474 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1475 prssdp->order = PSF_ORDER_PRSSD;
1476 prssdp->suborder = 0x41; /* Read Feature Codes */
1477 /* all other bytes of prssdp must be zero */
1478
1479 ccw = cqr->cpaddr;
1480 ccw->cmd_code = DASD_ECKD_CCW_PSF;
1481 ccw->count = sizeof(struct dasd_psf_prssd_data);
1482 ccw->flags |= CCW_FLAG_CC;
1483 ccw->cda = (__u32)(addr_t) prssdp;
1484
1485 /* Read Subsystem Data - feature codes */
1486 features = (struct dasd_rssd_features *) (prssdp + 1);
1487 memset(features, 0, sizeof(struct dasd_rssd_features));
1488
1489 ccw++;
1490 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1491 ccw->count = sizeof(struct dasd_rssd_features);
1492 ccw->cda = (__u32)(addr_t) features;
1493
1aae0560 1494 cqr->buildclk = get_tod_clock();
8e09f215
SW
1495 cqr->status = DASD_CQR_FILLED;
1496 rc = dasd_sleep_on(cqr);
1497 if (rc == 0) {
1498 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1499 features = (struct dasd_rssd_features *) (prssdp + 1);
1500 memcpy(&private->features, features,
1501 sizeof(struct dasd_rssd_features));
68d1e5f0
SW
1502 } else
1503 dev_warn(&device->cdev->dev, "Reading device feature codes"
1504 " failed with rc=%d\n", rc);
8e09f215
SW
1505 dasd_sfree_request(cqr, cqr->memdev);
1506 return rc;
1507}
1508
1509
40545573
HH
1510/*
1511 * Build CP for Perform Subsystem Function - SSC.
1512 */
f3eb5384
SW
1513static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
1514 int enable_pav)
40545573 1515{
8e09f215
SW
1516 struct dasd_ccw_req *cqr;
1517 struct dasd_psf_ssc_data *psf_ssc_data;
1518 struct ccw1 *ccw;
40545573 1519
68b781fe 1520 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
40545573
HH
1521 sizeof(struct dasd_psf_ssc_data),
1522 device);
1523
8e09f215 1524 if (IS_ERR(cqr)) {
fc19f381 1525 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
40545573 1526 "Could not allocate PSF-SSC request");
8e09f215
SW
1527 return cqr;
1528 }
1529 psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
1530 psf_ssc_data->order = PSF_ORDER_SSC;
626350b6 1531 psf_ssc_data->suborder = 0xc0;
f3eb5384 1532 if (enable_pav) {
626350b6 1533 psf_ssc_data->suborder |= 0x08;
f3eb5384
SW
1534 psf_ssc_data->reserved[0] = 0x88;
1535 }
8e09f215
SW
1536 ccw = cqr->cpaddr;
1537 ccw->cmd_code = DASD_ECKD_CCW_PSF;
1538 ccw->cda = (__u32)(addr_t)psf_ssc_data;
1539 ccw->count = 66;
1540
1541 cqr->startdev = device;
1542 cqr->memdev = device;
1543 cqr->block = NULL;
eb6e199b 1544 cqr->retries = 256;
8e09f215 1545 cqr->expires = 10*HZ;
1aae0560 1546 cqr->buildclk = get_tod_clock();
8e09f215
SW
1547 cqr->status = DASD_CQR_FILLED;
1548 return cqr;
40545573
HH
1549}
1550
1551/*
1552 * Perform Subsystem Function.
1553 * It is necessary to trigger CIO for channel revalidation since this
1554 * call might change behaviour of DASD devices.
1555 */
1556static int
12d7b107
SH
1557dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
1558 unsigned long flags)
40545573 1559{
8e09f215
SW
1560 struct dasd_ccw_req *cqr;
1561 int rc;
1562
f3eb5384 1563 cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
8e09f215
SW
1564 if (IS_ERR(cqr))
1565 return PTR_ERR(cqr);
1566
12d7b107
SH
1567 /*
1568 * set flags e.g. turn on failfast, to prevent blocking
1569 * the calling function should handle failed requests
1570 */
1571 cqr->flags |= flags;
1572
8e09f215
SW
1573 rc = dasd_sleep_on(cqr);
1574 if (!rc)
1575 /* trigger CIO to reprobe devices */
1576 css_schedule_reprobe();
12d7b107
SH
1577 else if (cqr->intrc == -EAGAIN)
1578 rc = -EAGAIN;
1579
8e09f215
SW
1580 dasd_sfree_request(cqr, cqr->memdev);
1581 return rc;
40545573
HH
1582}
1583
1584/*
1585 * Valide storage server of current device.
1586 */
12d7b107
SH
1587static int dasd_eckd_validate_server(struct dasd_device *device,
1588 unsigned long flags)
40545573 1589{
543691a4
SO
1590 struct dasd_eckd_private *private = device->private;
1591 int enable_pav, rc;
40545573 1592
f9f8d02f
SH
1593 if (private->uid.type == UA_BASE_PAV_ALIAS ||
1594 private->uid.type == UA_HYPER_PAV_ALIAS)
12d7b107 1595 return 0;
40545573 1596 if (dasd_nopav || MACHINE_IS_VM)
f3eb5384
SW
1597 enable_pav = 0;
1598 else
1599 enable_pav = 1;
12d7b107 1600 rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
eb6e199b 1601
8e79a441
HH
1602 /* may be requested feature is not available on server,
1603 * therefore just report error and go ahead */
b8ed5dd5
SH
1604 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
1605 "returned rc=%d", private->uid.ssid, rc);
12d7b107 1606 return rc;
40545573
HH
1607}
1608
f1633031
SH
1609/*
1610 * worker to do a validate server in case of a lost pathgroup
1611 */
1612static void dasd_eckd_do_validate_server(struct work_struct *work)
1613{
1614 struct dasd_device *device = container_of(work, struct dasd_device,
1615 kick_validate);
ea4da6ea
SH
1616 unsigned long flags = 0;
1617
1618 set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
1619 if (dasd_eckd_validate_server(device, flags)
12d7b107
SH
1620 == -EAGAIN) {
1621 /* schedule worker again if failed */
1622 schedule_work(&device->kick_validate);
1623 return;
1624 }
1625
f1633031
SH
1626 dasd_put_device(device);
1627}
1628
1629static void dasd_eckd_kick_validate_server(struct dasd_device *device)
1630{
1631 dasd_get_device(device);
25e2cf1c
SH
1632 /* exit if device not online or in offline processing */
1633 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1634 device->state < DASD_STATE_ONLINE) {
1635 dasd_put_device(device);
1636 return;
1637 }
f1633031 1638 /* queue call to do_validate_server to the kernel event daemon. */
f2608cd4
SH
1639 if (!schedule_work(&device->kick_validate))
1640 dasd_put_device(device);
f1633031
SH
1641}
1642
ef19298b
SW
1643static u32 get_fcx_max_data(struct dasd_device *device)
1644{
543691a4 1645 struct dasd_eckd_private *private = device->private;
ef19298b 1646 int fcx_in_css, fcx_in_gneq, fcx_in_features;
543691a4 1647 int tpm, mdc;
ef19298b
SW
1648
1649 if (dasd_nofcx)
1650 return 0;
1651 /* is transport mode supported? */
ef19298b
SW
1652 fcx_in_css = css_general_characteristics.fcx;
1653 fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
1654 fcx_in_features = private->features.feature[40] & 0x80;
1655 tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;
1656
1657 if (!tpm)
1658 return 0;
1659
1660 mdc = ccw_device_get_mdc(device->cdev, 0);
1661 if (mdc < 0) {
1662 dev_warn(&device->cdev->dev, "Detecting the maximum supported"
1663 " data size for zHPF requests failed\n");
1664 return 0;
1665 } else
1666 return mdc * FCX_MAX_DATA_FACTOR;
ef19298b
SW
1667}
1668
3d052595
HH
1669/*
1670 * Check device characteristics.
1671 * If the device is accessible using ECKD discipline, the device is enabled.
1672 */
1da177e4
LT
1673static int
1674dasd_eckd_check_characteristics(struct dasd_device *device)
1675{
543691a4 1676 struct dasd_eckd_private *private = device->private;
8e09f215 1677 struct dasd_block *block;
2dedf0d9 1678 struct dasd_uid temp_uid;
f9f8d02f 1679 int rc, i;
33b62a30 1680 int readonly;
7c8faa86 1681 unsigned long value;
1da177e4 1682
f1633031
SH
1683 /* setup work queue for validate server*/
1684 INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1685
454e1fa1
PO
1686 if (!ccw_device_is_pathgroup(device->cdev)) {
1687 dev_warn(&device->cdev->dev,
1688 "A channel path group could not be established\n");
1689 return -EIO;
1690 }
1691 if (!ccw_device_is_multipath(device->cdev)) {
1692 dev_info(&device->cdev->dev,
1693 "The DASD is not operating in multipath mode\n");
1694 }
92636b15
SO
1695 if (!private) {
1696 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1697 if (!private) {
fc19f381
SH
1698 dev_warn(&device->cdev->dev,
1699 "Allocating memory for private DASD data "
1700 "failed\n");
1da177e4
LT
1701 return -ENOMEM;
1702 }
543691a4 1703 device->private = private;
92636b15
SO
1704 } else {
1705 memset(private, 0, sizeof(*private));
1da177e4
LT
1706 }
1707 /* Invalidate status of initial analysis. */
1708 private->init_cqr_status = -1;
1709 /* Set default cache operations. */
1710 private->attrib.operation = DASD_NORMAL_CACHE;
1711 private->attrib.nr_cyl = 0;
1712
40545573
HH
1713 /* Read Configuration Data */
1714 rc = dasd_eckd_read_conf(device);
1715 if (rc)
8e09f215 1716 goto out_err1;
40545573 1717
7c8faa86
SH
1718 /* set default timeout */
1719 device->default_expires = DASD_EXPIRES;
1f1ee9ad
HR
1720 /* set default retry count */
1721 device->default_retries = DASD_RETRIES;
1722
7c8faa86
SH
1723 if (private->gneq) {
1724 value = 1;
1725 for (i = 0; i < private->gneq->timeout.value; i++)
1726 value = 10 * value;
1727 value = value * private->gneq->timeout.number;
1728 /* do not accept useless values */
1729 if (value != 0 && value <= DASD_EXPIRES_MAX)
1730 device->default_expires = value;
1731 }
1732
2dedf0d9
SH
1733 dasd_eckd_get_uid(device, &temp_uid);
1734 if (temp_uid.type == UA_BASE_DEVICE) {
8e09f215
SW
1735 block = dasd_alloc_block();
1736 if (IS_ERR(block)) {
b8ed5dd5
SH
1737 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1738 "could not allocate dasd "
1739 "block structure");
8e09f215
SW
1740 rc = PTR_ERR(block);
1741 goto out_err1;
1742 }
1743 device->block = block;
1744 block->base = device;
1745 }
1746
f9f8d02f
SH
1747 /* register lcu with alias handling, enable PAV */
1748 rc = dasd_alias_make_device_known_to_lcu(device);
1749 if (rc)
8e09f215 1750 goto out_err2;
f9f8d02f 1751
12d7b107 1752 dasd_eckd_validate_server(device, 0);
f4ac1d02
SW
1753
1754 /* device may report different configuration data after LCU setup */
1755 rc = dasd_eckd_read_conf(device);
1756 if (rc)
1757 goto out_err3;
8e09f215
SW
1758
1759 /* Read Feature Codes */
68d1e5f0 1760 dasd_eckd_read_features(device);
40545573 1761
1da177e4 1762 /* Read Device Characteristics */
68b781fe
SH
1763 rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
1764 &private->rdc_data, 64);
8e09f215 1765 if (rc) {
b8ed5dd5
SH
1766 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1767 "Read device characteristic failed, rc=%d", rc);
8e09f215
SW
1768 goto out_err3;
1769 }
34cd551a
SH
1770
1771 if ((device->features & DASD_FEATURE_USERAW) &&
1772 !(private->rdc_data.facilities.RT_in_LR)) {
1773 dev_err(&device->cdev->dev, "The storage server does not "
1774 "support raw-track access\n");
1775 rc = -EINVAL;
1776 goto out_err3;
1777 }
1778
817f2c84 1779 /* find the valid cylinder size */
b44b0ab3
SW
1780 if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
1781 private->rdc_data.long_no_cyl)
1782 private->real_cyl = private->rdc_data.long_no_cyl;
1783 else
1784 private->real_cyl = private->rdc_data.no_cyl;
1785
ef19298b
SW
1786 private->fcx_max_data = get_fcx_max_data(device);
1787
33b62a30
SW
1788 readonly = dasd_device_is_ro(device);
1789 if (readonly)
1790 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1791
fc19f381 1792 dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
33b62a30 1793 "with %d cylinders, %d heads, %d sectors%s\n",
fc19f381
SH
1794 private->rdc_data.dev_type,
1795 private->rdc_data.dev_model,
1796 private->rdc_data.cu_type,
1797 private->rdc_data.cu_model.model,
92636b15 1798 private->real_cyl,
fc19f381 1799 private->rdc_data.trk_per_cyl,
33b62a30
SW
1800 private->rdc_data.sec_per_trk,
1801 readonly ? ", read-only device" : "");
8e09f215
SW
1802 return 0;
1803
1804out_err3:
1805 dasd_alias_disconnect_device_from_lcu(device);
1806out_err2:
1807 dasd_free_block(device->block);
1808 device->block = NULL;
1809out_err1:
4abb08c2 1810 kfree(private->conf_data);
8e09f215
SW
1811 kfree(device->private);
1812 device->private = NULL;
3d052595 1813 return rc;
1da177e4
LT
1814}
1815
8e09f215
SW
1816static void dasd_eckd_uncheck_device(struct dasd_device *device)
1817{
543691a4 1818 struct dasd_eckd_private *private = device->private;
b179b037 1819 int i;
4abb08c2 1820
8e09f215 1821 dasd_alias_disconnect_device_from_lcu(device);
4abb08c2
SW
1822 private->ned = NULL;
1823 private->sneq = NULL;
1824 private->vdsneq = NULL;
1825 private->gneq = NULL;
1826 private->conf_len = 0;
b179b037
SH
1827 for (i = 0; i < 8; i++) {
1828 kfree(private->path_conf_data[i]);
1829 if ((__u8 *)private->path_conf_data[i] ==
1830 private->conf_data) {
1831 private->conf_data = NULL;
1832 private->conf_len = 0;
1833 }
1834 private->path_conf_data[i] = NULL;
1835 }
4abb08c2
SW
1836 kfree(private->conf_data);
1837 private->conf_data = NULL;
8e09f215
SW
1838}
1839
1da177e4
LT
1840static struct dasd_ccw_req *
1841dasd_eckd_analysis_ccw(struct dasd_device *device)
1842{
543691a4 1843 struct dasd_eckd_private *private = device->private;
1da177e4
LT
1844 struct eckd_count *count_data;
1845 struct LO_eckd_data *LO_data;
1846 struct dasd_ccw_req *cqr;
1847 struct ccw1 *ccw;
1848 int cplength, datasize;
1849 int i;
1850
1da177e4
LT
1851 cplength = 8;
1852 datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
68b781fe 1853 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1da177e4
LT
1854 if (IS_ERR(cqr))
1855 return cqr;
1856 ccw = cqr->cpaddr;
1857 /* Define extent for the first 3 tracks. */
1858 define_extent(ccw++, cqr->data, 0, 2,
1859 DASD_ECKD_CCW_READ_COUNT, device);
8e09f215 1860 LO_data = cqr->data + sizeof(struct DE_eckd_data);
1da177e4
LT
1861 /* Locate record for the first 4 records on track 0. */
1862 ccw[-1].flags |= CCW_FLAG_CC;
1863 locate_record(ccw++, LO_data++, 0, 0, 4,
1864 DASD_ECKD_CCW_READ_COUNT, device, 0);
1865
1866 count_data = private->count_area;
1867 for (i = 0; i < 4; i++) {
1868 ccw[-1].flags |= CCW_FLAG_CC;
1869 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1870 ccw->flags = 0;
1871 ccw->count = 8;
1872 ccw->cda = (__u32)(addr_t) count_data;
1873 ccw++;
1874 count_data++;
1875 }
1876
1877 /* Locate record for the first record on track 2. */
1878 ccw[-1].flags |= CCW_FLAG_CC;
1879 locate_record(ccw++, LO_data++, 2, 0, 1,
1880 DASD_ECKD_CCW_READ_COUNT, device, 0);
1881 /* Read count ccw. */
1882 ccw[-1].flags |= CCW_FLAG_CC;
1883 ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1884 ccw->flags = 0;
1885 ccw->count = 8;
1886 ccw->cda = (__u32)(addr_t) count_data;
1887
8e09f215
SW
1888 cqr->block = NULL;
1889 cqr->startdev = device;
1890 cqr->memdev = device;
eb6e199b 1891 cqr->retries = 255;
1aae0560 1892 cqr->buildclk = get_tod_clock();
1da177e4
LT
1893 cqr->status = DASD_CQR_FILLED;
1894 return cqr;
1895}
1896
eb6e199b
SW
1897/* differentiate between 'no record found' and any other error */
1898static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
1899{
1900 char *sense;
1901 if (init_cqr->status == DASD_CQR_DONE)
1902 return INIT_CQR_OK;
1903 else if (init_cqr->status == DASD_CQR_NEED_ERP ||
1904 init_cqr->status == DASD_CQR_FAILED) {
1905 sense = dasd_get_sense(&init_cqr->irb);
1906 if (sense && (sense[1] & SNS1_NO_REC_FOUND))
1907 return INIT_CQR_UNFORMATTED;
1908 else
1909 return INIT_CQR_ERROR;
1910 } else
1911 return INIT_CQR_ERROR;
1912}
1913
1da177e4
LT
1914/*
1915 * This is the callback function for the init_analysis cqr. It saves
1916 * the status of the initial analysis ccw before it frees it and kicks
1917 * the device to continue the startup sequence. This will call
1918 * dasd_eckd_do_analysis again (if the devices has not been marked
1919 * for deletion in the meantime).
1920 */
eb6e199b
SW
1921static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
1922 void *data)
1da177e4 1923{
543691a4
SO
1924 struct dasd_device *device = init_cqr->startdev;
1925 struct dasd_eckd_private *private = device->private;
1da177e4 1926
eb6e199b 1927 private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
1da177e4
LT
1928 dasd_sfree_request(init_cqr, device);
1929 dasd_kick_device(device);
1930}
1931
eb6e199b 1932static int dasd_eckd_start_analysis(struct dasd_block *block)
1da177e4 1933{
1da177e4
LT
1934 struct dasd_ccw_req *init_cqr;
1935
8e09f215 1936 init_cqr = dasd_eckd_analysis_ccw(block->base);
1da177e4
LT
1937 if (IS_ERR(init_cqr))
1938 return PTR_ERR(init_cqr);
1939 init_cqr->callback = dasd_eckd_analysis_callback;
1940 init_cqr->callback_data = NULL;
1941 init_cqr->expires = 5*HZ;
eb6e199b
SW
1942 /* first try without ERP, so we can later handle unformatted
1943 * devices as special case
1944 */
1945 clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
1946 init_cqr->retries = 0;
1da177e4
LT
1947 dasd_add_request_head(init_cqr);
1948 return -EAGAIN;
1949}
1950
eb6e199b 1951static int dasd_eckd_end_analysis(struct dasd_block *block)
1da177e4 1952{
543691a4
SO
1953 struct dasd_device *device = block->base;
1954 struct dasd_eckd_private *private = device->private;
1da177e4
LT
1955 struct eckd_count *count_area;
1956 unsigned int sb, blk_per_trk;
1957 int status, i;
eb6e199b 1958 struct dasd_ccw_req *init_cqr;
1da177e4 1959
1da177e4
LT
1960 status = private->init_cqr_status;
1961 private->init_cqr_status = -1;
eb6e199b
SW
1962 if (status == INIT_CQR_ERROR) {
1963 /* try again, this time with full ERP */
1964 init_cqr = dasd_eckd_analysis_ccw(device);
1965 dasd_sleep_on(init_cqr);
1966 status = dasd_eckd_analysis_evaluation(init_cqr);
1967 dasd_sfree_request(init_cqr, device);
1968 }
1969
e4dbb0f2
SH
1970 if (device->features & DASD_FEATURE_USERAW) {
1971 block->bp_block = DASD_RAW_BLOCKSIZE;
1972 blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
1973 block->s2b_shift = 3;
1974 goto raw;
1975 }
1976
eb6e199b
SW
1977 if (status == INIT_CQR_UNFORMATTED) {
1978 dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
1da177e4 1979 return -EMEDIUMTYPE;
eb6e199b
SW
1980 } else if (status == INIT_CQR_ERROR) {
1981 dev_err(&device->cdev->dev,
1982 "Detecting the DASD disk layout failed because "
1983 "of an I/O error\n");
1984 return -EIO;
1da177e4
LT
1985 }
1986
1987 private->uses_cdl = 1;
1da177e4
LT
1988 /* Check Track 0 for Compatible Disk Layout */
1989 count_area = NULL;
1990 for (i = 0; i < 3; i++) {
1991 if (private->count_area[i].kl != 4 ||
3bc9fef9
SH
1992 private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4 ||
1993 private->count_area[i].cyl != 0 ||
1994 private->count_area[i].head != count_area_head[i] ||
1995 private->count_area[i].record != count_area_rec[i]) {
1da177e4
LT
1996 private->uses_cdl = 0;
1997 break;
1998 }
1999 }
2000 if (i == 3)
2001 count_area = &private->count_area[4];
2002
2003 if (private->uses_cdl == 0) {
2004 for (i = 0; i < 5; i++) {
2005 if ((private->count_area[i].kl != 0) ||
2006 (private->count_area[i].dl !=
3bc9fef9
SH
2007 private->count_area[0].dl) ||
2008 private->count_area[i].cyl != 0 ||
2009 private->count_area[i].head != count_area_head[i] ||
2010 private->count_area[i].record != count_area_rec[i])
1da177e4
LT
2011 break;
2012 }
2013 if (i == 5)
2014 count_area = &private->count_area[0];
2015 } else {
2016 if (private->count_area[3].record == 1)
fc19f381
SH
2017 dev_warn(&device->cdev->dev,
2018 "Track 0 has no records following the VTOC\n");
1da177e4 2019 }
e4dbb0f2 2020
1da177e4
LT
2021 if (count_area != NULL && count_area->kl == 0) {
2022 /* we found notthing violating our disk layout */
2023 if (dasd_check_blocksize(count_area->dl) == 0)
8e09f215 2024 block->bp_block = count_area->dl;
1da177e4 2025 }
8e09f215 2026 if (block->bp_block == 0) {
fc19f381
SH
2027 dev_warn(&device->cdev->dev,
2028 "The disk layout of the DASD is not supported\n");
1da177e4
LT
2029 return -EMEDIUMTYPE;
2030 }
8e09f215
SW
2031 block->s2b_shift = 0; /* bits to shift 512 to get a block */
2032 for (sb = 512; sb < block->bp_block; sb = sb << 1)
2033 block->s2b_shift++;
1da177e4 2034
8e09f215 2035 blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
e4dbb0f2
SH
2036
2037raw:
b44b0ab3 2038 block->blocks = (private->real_cyl *
1da177e4
LT
2039 private->rdc_data.trk_per_cyl *
2040 blk_per_trk);
2041
fc19f381
SH
2042 dev_info(&device->cdev->dev,
2043 "DASD with %d KB/block, %d KB total size, %d KB/track, "
2044 "%s\n", (block->bp_block >> 10),
2045 ((private->real_cyl *
2046 private->rdc_data.trk_per_cyl *
2047 blk_per_trk * (block->bp_block >> 9)) >> 1),
2048 ((blk_per_trk * block->bp_block) >> 10),
2049 private->uses_cdl ?
2050 "compatible disk layout" : "linux disk layout");
1da177e4
LT
2051
2052 return 0;
2053}
2054
8e09f215 2055static int dasd_eckd_do_analysis(struct dasd_block *block)
1da177e4 2056{
543691a4 2057 struct dasd_eckd_private *private = block->base->private;
1da177e4 2058
1da177e4 2059 if (private->init_cqr_status < 0)
8e09f215 2060 return dasd_eckd_start_analysis(block);
1da177e4 2061 else
8e09f215 2062 return dasd_eckd_end_analysis(block);
1da177e4
LT
2063}
2064
d42e1712 2065static int dasd_eckd_basic_to_ready(struct dasd_device *device)
8e09f215
SW
2066{
2067 return dasd_alias_add_device(device);
2068};
2069
2070static int dasd_eckd_online_to_ready(struct dasd_device *device)
2071{
501183f2 2072 cancel_work_sync(&device->reload_device);
25e2cf1c 2073 cancel_work_sync(&device->kick_validate);
d42e1712
SH
2074 return 0;
2075};
2076
daa991bf 2077static int dasd_eckd_basic_to_known(struct dasd_device *device)
d42e1712 2078{
8e09f215
SW
2079 return dasd_alias_remove_device(device);
2080};
2081
1da177e4 2082static int
8e09f215 2083dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
1da177e4 2084{
543691a4 2085 struct dasd_eckd_private *private = block->base->private;
1da177e4 2086
8e09f215 2087 if (dasd_check_blocksize(block->bp_block) == 0) {
1da177e4 2088 geo->sectors = recs_per_track(&private->rdc_data,
8e09f215 2089 0, block->bp_block);
1da177e4
LT
2090 }
2091 geo->cylinders = private->rdc_data.no_cyl;
2092 geo->heads = private->rdc_data.trk_per_cyl;
2093 return 0;
2094}
2095
2096static struct dasd_ccw_req *
d42e1712 2097dasd_eckd_build_format(struct dasd_device *base,
29b8dd9d 2098 struct format_data_t *fdata,
a94fa154 2099 int enable_pav)
1da177e4 2100{
d42e1712
SH
2101 struct dasd_eckd_private *base_priv;
2102 struct dasd_eckd_private *start_priv;
29b8dd9d 2103 struct dasd_device *startdev = NULL;
1da177e4
LT
2104 struct dasd_ccw_req *fcp;
2105 struct eckd_count *ect;
d42e1712 2106 struct ch_t address;
1da177e4
LT
2107 struct ccw1 *ccw;
2108 void *data;
b44b0ab3 2109 int rpt;
1da177e4 2110 int cplength, datasize;
d42e1712 2111 int i, j;
f9a28f7b
JBJ
2112 int intensity = 0;
2113 int r0_perm;
d42e1712 2114 int nr_tracks;
18d6624e 2115 int use_prefix;
1da177e4 2116
a94fa154 2117 if (enable_pav)
29b8dd9d
SH
2118 startdev = dasd_alias_get_start_dev(base);
2119
d42e1712
SH
2120 if (!startdev)
2121 startdev = base;
1da177e4 2122
543691a4
SO
2123 start_priv = startdev->private;
2124 base_priv = base->private;
d42e1712
SH
2125
2126 rpt = recs_per_track(&base_priv->rdc_data, 0, fdata->blksize);
2127
2128 nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
1da177e4
LT
2129
2130 /*
2131 * fdata->intensity is a bit string that tells us what to do:
2132 * Bit 0: write record zero
2133 * Bit 1: write home address, currently not supported
2134 * Bit 2: invalidate tracks
2135 * Bit 3: use OS/390 compatible disk layout (cdl)
f9a28f7b 2136 * Bit 4: do not allow storage subsystem to modify record zero
1da177e4
LT
2137 * Only some bit combinations do make sense.
2138 */
f9a28f7b
JBJ
2139 if (fdata->intensity & 0x10) {
2140 r0_perm = 0;
2141 intensity = fdata->intensity & ~0x10;
2142 } else {
2143 r0_perm = 1;
2144 intensity = fdata->intensity;
2145 }
d42e1712 2146
18d6624e
SH
2147 use_prefix = base_priv->features.feature[8] & 0x01;
2148
f9a28f7b 2149 switch (intensity) {
1da177e4
LT
2150 case 0x00: /* Normal format */
2151 case 0x08: /* Normal format, use cdl. */
d42e1712 2152 cplength = 2 + (rpt*nr_tracks);
18d6624e
SH
2153 if (use_prefix)
2154 datasize = sizeof(struct PFX_eckd_data) +
2155 sizeof(struct LO_eckd_data) +
2156 rpt * nr_tracks * sizeof(struct eckd_count);
2157 else
2158 datasize = sizeof(struct DE_eckd_data) +
2159 sizeof(struct LO_eckd_data) +
2160 rpt * nr_tracks * sizeof(struct eckd_count);
1da177e4
LT
2161 break;
2162 case 0x01: /* Write record zero and format track. */
2163 case 0x09: /* Write record zero and format track, use cdl. */
d42e1712 2164 cplength = 2 + rpt * nr_tracks;
18d6624e
SH
2165 if (use_prefix)
2166 datasize = sizeof(struct PFX_eckd_data) +
2167 sizeof(struct LO_eckd_data) +
2168 sizeof(struct eckd_count) +
2169 rpt * nr_tracks * sizeof(struct eckd_count);
2170 else
2171 datasize = sizeof(struct DE_eckd_data) +
2172 sizeof(struct LO_eckd_data) +
2173 sizeof(struct eckd_count) +
2174 rpt * nr_tracks * sizeof(struct eckd_count);
1da177e4
LT
2175 break;
2176 case 0x04: /* Invalidate track. */
2177 case 0x0c: /* Invalidate track, use cdl. */
2178 cplength = 3;
18d6624e
SH
2179 if (use_prefix)
2180 datasize = sizeof(struct PFX_eckd_data) +
2181 sizeof(struct LO_eckd_data) +
2182 sizeof(struct eckd_count);
2183 else
2184 datasize = sizeof(struct DE_eckd_data) +
2185 sizeof(struct LO_eckd_data) +
2186 sizeof(struct eckd_count);
1da177e4
LT
2187 break;
2188 default:
d42e1712
SH
2189 dev_warn(&startdev->cdev->dev,
2190 "An I/O control call used incorrect flags 0x%x\n",
2191 fdata->intensity);
1da177e4
LT
2192 return ERR_PTR(-EINVAL);
2193 }
2194 /* Allocate the format ccw request. */
d42e1712
SH
2195 fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
2196 datasize, startdev);
1da177e4
LT
2197 if (IS_ERR(fcp))
2198 return fcp;
2199
d42e1712 2200 start_priv->count++;
1da177e4
LT
2201 data = fcp->data;
2202 ccw = fcp->cpaddr;
2203
f9a28f7b 2204 switch (intensity & ~0x08) {
1da177e4 2205 case 0x00: /* Normal format. */
18d6624e
SH
2206 if (use_prefix) {
2207 prefix(ccw++, (struct PFX_eckd_data *) data,
2208 fdata->start_unit, fdata->stop_unit,
2209 DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2210 /* grant subsystem permission to format R0 */
2211 if (r0_perm)
2212 ((struct PFX_eckd_data *)data)
2213 ->define_extent.ga_extended |= 0x04;
2214 data += sizeof(struct PFX_eckd_data);
2215 } else {
2216 define_extent(ccw++, (struct DE_eckd_data *) data,
2217 fdata->start_unit, fdata->stop_unit,
2218 DASD_ECKD_CCW_WRITE_CKD, startdev);
2219 /* grant subsystem permission to format R0 */
2220 if (r0_perm)
2221 ((struct DE_eckd_data *) data)
2222 ->ga_extended |= 0x04;
2223 data += sizeof(struct DE_eckd_data);
2224 }
1da177e4
LT
2225 ccw[-1].flags |= CCW_FLAG_CC;
2226 locate_record(ccw++, (struct LO_eckd_data *) data,
d42e1712
SH
2227 fdata->start_unit, 0, rpt*nr_tracks,
2228 DASD_ECKD_CCW_WRITE_CKD, base,
1da177e4
LT
2229 fdata->blksize);
2230 data += sizeof(struct LO_eckd_data);
2231 break;
2232 case 0x01: /* Write record zero + format track. */
18d6624e
SH
2233 if (use_prefix) {
2234 prefix(ccw++, (struct PFX_eckd_data *) data,
2235 fdata->start_unit, fdata->stop_unit,
2236 DASD_ECKD_CCW_WRITE_RECORD_ZERO,
2237 base, startdev);
2238 data += sizeof(struct PFX_eckd_data);
2239 } else {
2240 define_extent(ccw++, (struct DE_eckd_data *) data,
2241 fdata->start_unit, fdata->stop_unit,
2242 DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev);
2243 data += sizeof(struct DE_eckd_data);
2244 }
1da177e4
LT
2245 ccw[-1].flags |= CCW_FLAG_CC;
2246 locate_record(ccw++, (struct LO_eckd_data *) data,
d42e1712
SH
2247 fdata->start_unit, 0, rpt * nr_tracks + 1,
2248 DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
2249 base->block->bp_block);
1da177e4
LT
2250 data += sizeof(struct LO_eckd_data);
2251 break;
2252 case 0x04: /* Invalidate track. */
18d6624e
SH
2253 if (use_prefix) {
2254 prefix(ccw++, (struct PFX_eckd_data *) data,
2255 fdata->start_unit, fdata->stop_unit,
2256 DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2257 data += sizeof(struct PFX_eckd_data);
2258 } else {
2259 define_extent(ccw++, (struct DE_eckd_data *) data,
2260 fdata->start_unit, fdata->stop_unit,
2261 DASD_ECKD_CCW_WRITE_CKD, startdev);
2262 data += sizeof(struct DE_eckd_data);
2263 }
1da177e4
LT
2264 ccw[-1].flags |= CCW_FLAG_CC;
2265 locate_record(ccw++, (struct LO_eckd_data *) data,
2266 fdata->start_unit, 0, 1,
d42e1712 2267 DASD_ECKD_CCW_WRITE_CKD, base, 8);
1da177e4
LT
2268 data += sizeof(struct LO_eckd_data);
2269 break;
2270 }
d42e1712
SH
2271
2272 for (j = 0; j < nr_tracks; j++) {
2273 /* calculate cylinder and head for the current track */
2274 set_ch_t(&address,
2275 (fdata->start_unit + j) /
2276 base_priv->rdc_data.trk_per_cyl,
2277 (fdata->start_unit + j) %
2278 base_priv->rdc_data.trk_per_cyl);
2279 if (intensity & 0x01) { /* write record zero */
1da177e4
LT
2280 ect = (struct eckd_count *) data;
2281 data += sizeof(struct eckd_count);
b44b0ab3
SW
2282 ect->cyl = address.cyl;
2283 ect->head = address.head;
d42e1712 2284 ect->record = 0;
1da177e4 2285 ect->kl = 0;
d42e1712 2286 ect->dl = 8;
1da177e4 2287 ccw[-1].flags |= CCW_FLAG_CC;
d42e1712 2288 ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
1da177e4
LT
2289 ccw->flags = CCW_FLAG_SLI;
2290 ccw->count = 8;
2291 ccw->cda = (__u32)(addr_t) ect;
2292 ccw++;
2293 }
d42e1712
SH
2294 if ((intensity & ~0x08) & 0x04) { /* erase track */
2295 ect = (struct eckd_count *) data;
2296 data += sizeof(struct eckd_count);
2297 ect->cyl = address.cyl;
2298 ect->head = address.head;
2299 ect->record = 1;
2300 ect->kl = 0;
2301 ect->dl = 0;
2302 ccw[-1].flags |= CCW_FLAG_CC;
2303 ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
2304 ccw->flags = CCW_FLAG_SLI;
2305 ccw->count = 8;
2306 ccw->cda = (__u32)(addr_t) ect;
2307 } else { /* write remaining records */
2308 for (i = 0; i < rpt; i++) {
2309 ect = (struct eckd_count *) data;
2310 data += sizeof(struct eckd_count);
2311 ect->cyl = address.cyl;
2312 ect->head = address.head;
2313 ect->record = i + 1;
2314 ect->kl = 0;
2315 ect->dl = fdata->blksize;
2316 /*
2317 * Check for special tracks 0-1
2318 * when formatting CDL
2319 */
2320 if ((intensity & 0x08) &&
46d1c03c 2321 address.cyl == 0 && address.head == 0) {
d42e1712
SH
2322 if (i < 3) {
2323 ect->kl = 4;
2324 ect->dl = sizes_trk0[i] - 4;
2325 }
2326 }
2327 if ((intensity & 0x08) &&
46d1c03c 2328 address.cyl == 0 && address.head == 1) {
d42e1712
SH
2329 ect->kl = 44;
2330 ect->dl = LABEL_SIZE - 44;
2331 }
2332 ccw[-1].flags |= CCW_FLAG_CC;
2333 if (i != 0 || j == 0)
2334 ccw->cmd_code =
2335 DASD_ECKD_CCW_WRITE_CKD;
2336 else
2337 ccw->cmd_code =
2338 DASD_ECKD_CCW_WRITE_CKD_MT;
2339 ccw->flags = CCW_FLAG_SLI;
2340 ccw->count = 8;
2341 ccw->cda = (__u32)(addr_t) ect;
2342 ccw++;
2343 }
2344 }
1da177e4 2345 }
d42e1712
SH
2346
2347 fcp->startdev = startdev;
2348 fcp->memdev = startdev;
29b8dd9d 2349 fcp->basedev = base;
eb6e199b 2350 fcp->retries = 256;
d42e1712 2351 fcp->expires = startdev->default_expires * HZ;
1aae0560 2352 fcp->buildclk = get_tod_clock();
1da177e4 2353 fcp->status = DASD_CQR_FILLED;
d42e1712 2354
1da177e4
LT
2355 return fcp;
2356}
2357
570d237c
JH
2358/*
2359 * Wrapper function to build a CCW request depending on input data
2360 */
2361static struct dasd_ccw_req *
2362dasd_eckd_format_build_ccw_req(struct dasd_device *base,
2363 struct format_data_t *fdata, int enable_pav)
2364{
2365 return dasd_eckd_build_format(base, fdata, enable_pav);
2366}
2367
2368/*
2369 * Sanity checks on format_data
2370 */
2371static int dasd_eckd_format_sanity_checks(struct dasd_device *base,
2372 struct format_data_t *fdata)
d42e1712 2373{
543691a4 2374 struct dasd_eckd_private *private = base->private;
d42e1712 2375
d42e1712
SH
2376 if (fdata->start_unit >=
2377 (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2378 dev_warn(&base->cdev->dev,
2379 "Start track number %u used in formatting is too big\n",
2380 fdata->start_unit);
2381 return -EINVAL;
2382 }
2383 if (fdata->stop_unit >=
2384 (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2385 dev_warn(&base->cdev->dev,
2386 "Stop track number %u used in formatting is too big\n",
2387 fdata->stop_unit);
2388 return -EINVAL;
2389 }
2390 if (fdata->start_unit > fdata->stop_unit) {
2391 dev_warn(&base->cdev->dev,
2392 "Start track %u used in formatting exceeds end track\n",
2393 fdata->start_unit);
2394 return -EINVAL;
2395 }
2396 if (dasd_check_blocksize(fdata->blksize) != 0) {
2397 dev_warn(&base->cdev->dev,
2398 "The DASD cannot be formatted with block size %u\n",
2399 fdata->blksize);
2400 return -EINVAL;
2401 }
570d237c
JH
2402 return 0;
2403}
2404
2405/*
2406 * This function will process format_data originally coming from an IOCTL
2407 */
2408static int dasd_eckd_format_process_data(struct dasd_device *base,
2409 struct format_data_t *fdata,
2410 int enable_pav)
2411{
543691a4 2412 struct dasd_eckd_private *private = base->private;
570d237c 2413 struct dasd_ccw_req *cqr, *n;
570d237c
JH
2414 struct list_head format_queue;
2415 struct dasd_device *device;
2416 int old_start, old_stop, format_step;
2417 int step, retry;
2418 int rc;
2419
570d237c
JH
2420 rc = dasd_eckd_format_sanity_checks(base, fdata);
2421 if (rc)
2422 return rc;
d42e1712
SH
2423
2424 INIT_LIST_HEAD(&format_queue);
d42e1712 2425
46d1c03c 2426 old_start = fdata->start_unit;
29b8dd9d 2427 old_stop = fdata->stop_unit;
d42e1712 2428
46d1c03c
JH
2429 format_step = DASD_CQR_MAX_CCW / recs_per_track(&private->rdc_data, 0,
2430 fdata->blksize);
2431 do {
2432 retry = 0;
2433 while (fdata->start_unit <= old_stop) {
2434 step = fdata->stop_unit - fdata->start_unit + 1;
2435 if (step > format_step) {
2436 fdata->stop_unit =
2437 fdata->start_unit + format_step - 1;
2438 }
d42e1712 2439
570d237c
JH
2440 cqr = dasd_eckd_format_build_ccw_req(base, fdata,
2441 enable_pav);
46d1c03c
JH
2442 if (IS_ERR(cqr)) {
2443 rc = PTR_ERR(cqr);
2444 if (rc == -ENOMEM) {
2445 if (list_empty(&format_queue))
2446 goto out;
2447 /*
2448 * not enough memory available, start
2449 * requests retry after first requests
2450 * were finished
2451 */
2452 retry = 1;
2453 break;
2454 }
2455 goto out_err;
2456 }
2457 list_add_tail(&cqr->blocklist, &format_queue);
d42e1712 2458
46d1c03c
JH
2459 fdata->start_unit = fdata->stop_unit + 1;
2460 fdata->stop_unit = old_stop;
d42e1712 2461 }
d42e1712 2462
46d1c03c
JH
2463 rc = dasd_sleep_on_queue(&format_queue);
2464
2465out_err:
2466 list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
2467 device = cqr->startdev;
543691a4 2468 private = device->private;
46d1c03c
JH
2469 if (cqr->status == DASD_CQR_FAILED)
2470 rc = -EIO;
2471 list_del_init(&cqr->blocklist);
2472 dasd_sfree_request(cqr, device);
2473 private->count--;
2474 }
d42e1712 2475
46d1c03c
JH
2476 if (rc)
2477 goto out;
d42e1712 2478
46d1c03c 2479 } while (retry);
29b8dd9d 2480
46d1c03c
JH
2481out:
2482 fdata->start_unit = old_start;
2483 fdata->stop_unit = old_stop;
d42e1712
SH
2484
2485 return rc;
2486}
2487
570d237c
JH
2488static int dasd_eckd_format_device(struct dasd_device *base,
2489 struct format_data_t *fdata, int enable_pav)
2490{
2491 return dasd_eckd_format_process_data(base, fdata, enable_pav);
2492}
2493
8e09f215 2494static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
1da177e4 2495{
a2ace466
HR
2496 if (cqr->retries < 0) {
2497 cqr->status = DASD_CQR_FAILED;
2498 return;
2499 }
8e09f215
SW
2500 cqr->status = DASD_CQR_FILLED;
2501 if (cqr->block && (cqr->startdev != cqr->block->base)) {
2502 dasd_eckd_reset_ccw_to_base_io(cqr);
2503 cqr->startdev = cqr->block->base;
a4d26c6a 2504 cqr->lpm = cqr->block->base->path_data.opm;
1da177e4 2505 }
8e09f215 2506};
1da177e4
LT
2507
2508static dasd_erp_fn_t
2509dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
2510{
8e09f215 2511 struct dasd_device *device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
2512 struct ccw_device *cdev = device->cdev;
2513
2514 switch (cdev->id.cu_type) {
2515 case 0x3990:
2516 case 0x2105:
2517 case 0x2107:
2518 case 0x1750:
2519 return dasd_3990_erp_action;
2520 case 0x9343:
2521 case 0x3880:
2522 default:
2523 return dasd_default_erp_action;
2524 }
2525}
2526
2527static dasd_erp_fn_t
2528dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
2529{
2530 return dasd_default_erp_postaction;
2531}
2532
5a27e60d
SW
2533static void dasd_eckd_check_for_device_change(struct dasd_device *device,
2534 struct dasd_ccw_req *cqr,
2535 struct irb *irb)
8e09f215
SW
2536{
2537 char mask;
f3eb5384 2538 char *sense = NULL;
543691a4 2539 struct dasd_eckd_private *private = device->private;
8e09f215
SW
2540
2541 /* first of all check for state change pending interrupt */
2542 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
f3eb5384 2543 if ((scsw_dstat(&irb->scsw) & mask) == mask) {
c8d1c0ff
SH
2544 /*
2545 * for alias only, not in offline processing
2546 * and only if not suspended
2547 */
501183f2 2548 if (!device->block && private->lcu &&
25e2cf1c 2549 device->state == DASD_STATE_ONLINE &&
c8d1c0ff
SH
2550 !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2551 !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
501183f2
SH
2552 /*
2553 * the state change could be caused by an alias
2554 * reassignment remove device from alias handling
2555 * to prevent new requests from being scheduled on
2556 * the wrong alias device
2557 */
2558 dasd_alias_remove_device(device);
2559
2560 /* schedule worker to reload device */
2561 dasd_reload_device(device);
2562 }
8e09f215
SW
2563 dasd_generic_handle_state_change(device);
2564 return;
2565 }
2566
a5a0061f 2567 sense = dasd_get_sense(irb);
5a27e60d
SW
2568 if (!sense)
2569 return;
2570
2571 /* summary unit check */
c7a29e56 2572 if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
a5a0061f 2573 (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
8e09f215
SW
2574 dasd_alias_handle_summary_unit_check(device, irb);
2575 return;
2576 }
2577
f60c768c 2578 /* service information message SIM */
5a27e60d 2579 if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
f3eb5384
SW
2580 ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
2581 dasd_3990_erp_handle_sim(device, sense);
f60c768c
SH
2582 return;
2583 }
2584
5a27e60d
SW
2585 /* loss of device reservation is handled via base devices only
2586 * as alias devices may be used with several bases
2587 */
c7a29e56
SW
2588 if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
2589 (sense[7] == 0x3F) &&
5a27e60d
SW
2590 (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
2591 test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
2592 if (device->features & DASD_FEATURE_FAILONSLCK)
2593 set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
2594 clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
2595 dev_err(&device->cdev->dev,
2596 "The device reservation was lost\n");
ada3df91 2597 }
5a27e60d 2598}
f3eb5384
SW
2599
2600static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
2601 struct dasd_device *startdev,
8e09f215 2602 struct dasd_block *block,
f3eb5384
SW
2603 struct request *req,
2604 sector_t first_rec,
2605 sector_t last_rec,
2606 sector_t first_trk,
2607 sector_t last_trk,
2608 unsigned int first_offs,
2609 unsigned int last_offs,
2610 unsigned int blk_per_trk,
2611 unsigned int blksize)
1da177e4
LT
2612{
2613 struct dasd_eckd_private *private;
2614 unsigned long *idaws;
2615 struct LO_eckd_data *LO_data;
2616 struct dasd_ccw_req *cqr;
2617 struct ccw1 *ccw;
5705f702 2618 struct req_iterator iter;
7988613b 2619 struct bio_vec bv;
1da177e4 2620 char *dst;
f3eb5384 2621 unsigned int off;
1da177e4 2622 int count, cidaw, cplength, datasize;
f3eb5384 2623 sector_t recid;
1da177e4 2624 unsigned char cmd, rcmd;
8e09f215
SW
2625 int use_prefix;
2626 struct dasd_device *basedev;
1da177e4 2627
8e09f215 2628 basedev = block->base;
543691a4 2629 private = basedev->private;
1da177e4
LT
2630 if (rq_data_dir(req) == READ)
2631 cmd = DASD_ECKD_CCW_READ_MT;
2632 else if (rq_data_dir(req) == WRITE)
2633 cmd = DASD_ECKD_CCW_WRITE_MT;
2634 else
2635 return ERR_PTR(-EINVAL);
f3eb5384 2636
1da177e4
LT
2637 /* Check struct bio and count the number of blocks for the request. */
2638 count = 0;
2639 cidaw = 0;
5705f702 2640 rq_for_each_segment(bv, req, iter) {
7988613b 2641 if (bv.bv_len & (blksize - 1))
6c92e699
JA
2642 /* Eckd can only do full blocks. */
2643 return ERR_PTR(-EINVAL);
7988613b 2644 count += bv.bv_len >> (block->s2b_shift + 9);
7988613b
KO
2645 if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
2646 cidaw += bv.bv_len >> (block->s2b_shift + 9);
1da177e4
LT
2647 }
2648 /* Paranoia. */
2649 if (count != last_rec - first_rec + 1)
2650 return ERR_PTR(-EINVAL);
8e09f215
SW
2651
2652 /* use the prefix command if available */
2653 use_prefix = private->features.feature[8] & 0x01;
2654 if (use_prefix) {
2655 /* 1x prefix + number of blocks */
2656 cplength = 2 + count;
2657 /* 1x prefix + cidaws*sizeof(long) */
2658 datasize = sizeof(struct PFX_eckd_data) +
2659 sizeof(struct LO_eckd_data) +
2660 cidaw * sizeof(unsigned long);
2661 } else {
2662 /* 1x define extent + 1x locate record + number of blocks */
2663 cplength = 2 + count;
2664 /* 1x define extent + 1x locate record + cidaws*sizeof(long) */
2665 datasize = sizeof(struct DE_eckd_data) +
2666 sizeof(struct LO_eckd_data) +
2667 cidaw * sizeof(unsigned long);
2668 }
1da177e4
LT
2669 /* Find out the number of additional locate record ccws for cdl. */
2670 if (private->uses_cdl && first_rec < 2*blk_per_trk) {
2671 if (last_rec >= 2*blk_per_trk)
2672 count = 2*blk_per_trk - first_rec;
2673 cplength += count;
2674 datasize += count*sizeof(struct LO_eckd_data);
2675 }
2676 /* Allocate the ccw request. */
68b781fe
SH
2677 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2678 startdev);
1da177e4
LT
2679 if (IS_ERR(cqr))
2680 return cqr;
2681 ccw = cqr->cpaddr;
8e09f215
SW
2682 /* First ccw is define extent or prefix. */
2683 if (use_prefix) {
2684 if (prefix(ccw++, cqr->data, first_trk,
2685 last_trk, cmd, basedev, startdev) == -EAGAIN) {
2686 /* Clock not in sync and XRC is enabled.
2687 * Try again later.
2688 */
2689 dasd_sfree_request(cqr, startdev);
2690 return ERR_PTR(-EAGAIN);
2691 }
2692 idaws = (unsigned long *) (cqr->data +
2693 sizeof(struct PFX_eckd_data));
2694 } else {
2695 if (define_extent(ccw++, cqr->data, first_trk,
b38f27e8 2696 last_trk, cmd, basedev) == -EAGAIN) {
8e09f215
SW
2697 /* Clock not in sync and XRC is enabled.
2698 * Try again later.
2699 */
2700 dasd_sfree_request(cqr, startdev);
2701 return ERR_PTR(-EAGAIN);
2702 }
2703 idaws = (unsigned long *) (cqr->data +
2704 sizeof(struct DE_eckd_data));
d54853ef 2705 }
1da177e4 2706 /* Build locate_record+read/write/ccws. */
1da177e4
LT
2707 LO_data = (struct LO_eckd_data *) (idaws + cidaw);
2708 recid = first_rec;
2709 if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
2710 /* Only standard blocks so there is just one locate record. */
2711 ccw[-1].flags |= CCW_FLAG_CC;
2712 locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
8e09f215 2713 last_rec - recid + 1, cmd, basedev, blksize);
1da177e4 2714 }
5705f702 2715 rq_for_each_segment(bv, req, iter) {
7988613b 2716 dst = page_address(bv.bv_page) + bv.bv_offset;
1da177e4
LT
2717 if (dasd_page_cache) {
2718 char *copy = kmem_cache_alloc(dasd_page_cache,
441e143e 2719 GFP_DMA | __GFP_NOWARN);
1da177e4 2720 if (copy && rq_data_dir(req) == WRITE)
7988613b 2721 memcpy(copy + bv.bv_offset, dst, bv.bv_len);
1da177e4 2722 if (copy)
7988613b 2723 dst = copy + bv.bv_offset;
1da177e4 2724 }
7988613b 2725 for (off = 0; off < bv.bv_len; off += blksize) {
1da177e4
LT
2726 sector_t trkid = recid;
2727 unsigned int recoffs = sector_div(trkid, blk_per_trk);
2728 rcmd = cmd;
2729 count = blksize;
2730 /* Locate record for cdl special block ? */
2731 if (private->uses_cdl && recid < 2*blk_per_trk) {
2732 if (dasd_eckd_cdl_special(blk_per_trk, recid)){
2733 rcmd |= 0x8;
2734 count = dasd_eckd_cdl_reclen(recid);
ec5883ab
HH
2735 if (count < blksize &&
2736 rq_data_dir(req) == READ)
1da177e4
LT
2737 memset(dst + count, 0xe5,
2738 blksize - count);
2739 }
2740 ccw[-1].flags |= CCW_FLAG_CC;
2741 locate_record(ccw++, LO_data++,
2742 trkid, recoffs + 1,
8e09f215 2743 1, rcmd, basedev, count);
1da177e4
LT
2744 }
2745 /* Locate record for standard blocks ? */
2746 if (private->uses_cdl && recid == 2*blk_per_trk) {
2747 ccw[-1].flags |= CCW_FLAG_CC;
2748 locate_record(ccw++, LO_data++,
2749 trkid, recoffs + 1,
2750 last_rec - recid + 1,
8e09f215 2751 cmd, basedev, count);
1da177e4
LT
2752 }
2753 /* Read/write ccw. */
2754 ccw[-1].flags |= CCW_FLAG_CC;
2755 ccw->cmd_code = rcmd;
2756 ccw->count = count;
2757 if (idal_is_needed(dst, blksize)) {
2758 ccw->cda = (__u32)(addr_t) idaws;
2759 ccw->flags = CCW_FLAG_IDA;
2760 idaws = idal_create_words(idaws, dst, blksize);
2761 } else {
2762 ccw->cda = (__u32)(addr_t) dst;
2763 ccw->flags = 0;
2764 }
2765 ccw++;
2766 dst += blksize;
2767 recid++;
2768 }
2769 }
13de227b
HS
2770 if (blk_noretry_request(req) ||
2771 block->base->features & DASD_FEATURE_FAILFAST)
1c01b8a5 2772 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
8e09f215
SW
2773 cqr->startdev = startdev;
2774 cqr->memdev = startdev;
2775 cqr->block = block;
7c8faa86 2776 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
a4d26c6a 2777 cqr->lpm = startdev->path_data.ppm;
1f1ee9ad 2778 cqr->retries = startdev->default_retries;
1aae0560 2779 cqr->buildclk = get_tod_clock();
1da177e4
LT
2780 cqr->status = DASD_CQR_FILLED;
2781 return cqr;
2782}
2783
f3eb5384
SW
2784static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
2785 struct dasd_device *startdev,
2786 struct dasd_block *block,
2787 struct request *req,
2788 sector_t first_rec,
2789 sector_t last_rec,
2790 sector_t first_trk,
2791 sector_t last_trk,
2792 unsigned int first_offs,
2793 unsigned int last_offs,
2794 unsigned int blk_per_trk,
2795 unsigned int blksize)
2796{
f3eb5384
SW
2797 unsigned long *idaws;
2798 struct dasd_ccw_req *cqr;
2799 struct ccw1 *ccw;
2800 struct req_iterator iter;
7988613b 2801 struct bio_vec bv;
f3eb5384
SW
2802 char *dst, *idaw_dst;
2803 unsigned int cidaw, cplength, datasize;
2804 unsigned int tlf;
2805 sector_t recid;
2806 unsigned char cmd;
2807 struct dasd_device *basedev;
2808 unsigned int trkcount, count, count_to_trk_end;
2809 unsigned int idaw_len, seg_len, part_len, len_to_track_end;
2810 unsigned char new_track, end_idaw;
2811 sector_t trkid;
2812 unsigned int recoffs;
2813
2814 basedev = block->base;
f3eb5384
SW
2815 if (rq_data_dir(req) == READ)
2816 cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
2817 else if (rq_data_dir(req) == WRITE)
2818 cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
2819 else
2820 return ERR_PTR(-EINVAL);
2821
2822 /* Track based I/O needs IDAWs for each page, and not just for
2823 * 64 bit addresses. We need additional idals for pages
2824 * that get filled from two tracks, so we use the number
2825 * of records as upper limit.
2826 */
2827 cidaw = last_rec - first_rec + 1;
2828 trkcount = last_trk - first_trk + 1;
2829
2830 /* 1x prefix + one read/write ccw per track */
2831 cplength = 1 + trkcount;
2832
2833 /* on 31-bit we need space for two 32 bit addresses per page
2834 * on 64-bit one 64 bit address
2835 */
2836 datasize = sizeof(struct PFX_eckd_data) +
2837 cidaw * sizeof(unsigned long long);
2838
2839 /* Allocate the ccw request. */
68b781fe
SH
2840 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2841 startdev);
f3eb5384
SW
2842 if (IS_ERR(cqr))
2843 return cqr;
2844 ccw = cqr->cpaddr;
2845 /* transfer length factor: how many bytes to read from the last track */
2846 if (first_trk == last_trk)
2847 tlf = last_offs - first_offs + 1;
2848 else
2849 tlf = last_offs + 1;
2850 tlf *= blksize;
2851
2852 if (prefix_LRE(ccw++, cqr->data, first_trk,
2853 last_trk, cmd, basedev, startdev,
2854 1 /* format */, first_offs + 1,
2855 trkcount, blksize,
2856 tlf) == -EAGAIN) {
2857 /* Clock not in sync and XRC is enabled.
2858 * Try again later.
2859 */
2860 dasd_sfree_request(cqr, startdev);
2861 return ERR_PTR(-EAGAIN);
2862 }
2863
2864 /*
2865 * The translation of request into ccw programs must meet the
2866 * following conditions:
2867 * - all idaws but the first and the last must address full pages
2868 * (or 2K blocks on 31-bit)
2869 * - the scope of a ccw and it's idal ends with the track boundaries
2870 */
2871 idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
2872 recid = first_rec;
2873 new_track = 1;
2874 end_idaw = 0;
2875 len_to_track_end = 0;
246ccea1 2876 idaw_dst = NULL;
f3eb5384
SW
2877 idaw_len = 0;
2878 rq_for_each_segment(bv, req, iter) {
7988613b
KO
2879 dst = page_address(bv.bv_page) + bv.bv_offset;
2880 seg_len = bv.bv_len;
f3eb5384
SW
2881 while (seg_len) {
2882 if (new_track) {
2883 trkid = recid;
2884 recoffs = sector_div(trkid, blk_per_trk);
2885 count_to_trk_end = blk_per_trk - recoffs;
2886 count = min((last_rec - recid + 1),
2887 (sector_t)count_to_trk_end);
2888 len_to_track_end = count * blksize;
2889 ccw[-1].flags |= CCW_FLAG_CC;
2890 ccw->cmd_code = cmd;
2891 ccw->count = len_to_track_end;
2892 ccw->cda = (__u32)(addr_t)idaws;
2893 ccw->flags = CCW_FLAG_IDA;
2894 ccw++;
2895 recid += count;
2896 new_track = 0;
52db45c3
SW
2897 /* first idaw for a ccw may start anywhere */
2898 if (!idaw_dst)
2899 idaw_dst = dst;
f3eb5384 2900 }
52db45c3
SW
2901 /* If we start a new idaw, we must make sure that it
2902 * starts on an IDA_BLOCK_SIZE boundary.
f3eb5384
SW
2903 * If we continue an idaw, we must make sure that the
2904 * current segment begins where the so far accumulated
2905 * idaw ends
2906 */
52db45c3
SW
2907 if (!idaw_dst) {
2908 if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
2909 dasd_sfree_request(cqr, startdev);
2910 return ERR_PTR(-ERANGE);
2911 } else
2912 idaw_dst = dst;
2913 }
f3eb5384
SW
2914 if ((idaw_dst + idaw_len) != dst) {
2915 dasd_sfree_request(cqr, startdev);
2916 return ERR_PTR(-ERANGE);
2917 }
2918 part_len = min(seg_len, len_to_track_end);
2919 seg_len -= part_len;
2920 dst += part_len;
2921 idaw_len += part_len;
2922 len_to_track_end -= part_len;
2923 /* collected memory area ends on an IDA_BLOCK border,
2924 * -> create an idaw
2925 * idal_create_words will handle cases where idaw_len
2926 * is larger then IDA_BLOCK_SIZE
2927 */
2928 if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
2929 end_idaw = 1;
2930 /* We also need to end the idaw at track end */
2931 if (!len_to_track_end) {
2932 new_track = 1;
2933 end_idaw = 1;
2934 }
2935 if (end_idaw) {
2936 idaws = idal_create_words(idaws, idaw_dst,
2937 idaw_len);
246ccea1 2938 idaw_dst = NULL;
f3eb5384
SW
2939 idaw_len = 0;
2940 end_idaw = 0;
2941 }
2942 }
2943 }
2944
2945 if (blk_noretry_request(req) ||
2946 block->base->features & DASD_FEATURE_FAILFAST)
2947 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
2948 cqr->startdev = startdev;
2949 cqr->memdev = startdev;
2950 cqr->block = block;
7c8faa86 2951 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
a4d26c6a 2952 cqr->lpm = startdev->path_data.ppm;
1f1ee9ad 2953 cqr->retries = startdev->default_retries;
1aae0560 2954 cqr->buildclk = get_tod_clock();
f3eb5384
SW
2955 cqr->status = DASD_CQR_FILLED;
2956 return cqr;
2957}
2958
2959static int prepare_itcw(struct itcw *itcw,
2960 unsigned int trk, unsigned int totrk, int cmd,
2961 struct dasd_device *basedev,
2962 struct dasd_device *startdev,
2963 unsigned int rec_on_trk, int count,
2964 unsigned int blksize,
2965 unsigned int total_data_size,
2966 unsigned int tlf,
2967 unsigned int blk_per_trk)
2968{
2969 struct PFX_eckd_data pfxdata;
2970 struct dasd_eckd_private *basepriv, *startpriv;
2971 struct DE_eckd_data *dedata;
2972 struct LRE_eckd_data *lredata;
2973 struct dcw *dcw;
2974
2975 u32 begcyl, endcyl;
2976 u16 heads, beghead, endhead;
2977 u8 pfx_cmd;
2978
2979 int rc = 0;
2980 int sector = 0;
2981 int dn, d;
2982
2983
2984 /* setup prefix data */
543691a4
SO
2985 basepriv = basedev->private;
2986 startpriv = startdev->private;
f3eb5384
SW
2987 dedata = &pfxdata.define_extent;
2988 lredata = &pfxdata.locate_record;
2989
2990 memset(&pfxdata, 0, sizeof(pfxdata));
2991 pfxdata.format = 1; /* PFX with LRE */
2992 pfxdata.base_address = basepriv->ned->unit_addr;
2993 pfxdata.base_lss = basepriv->ned->ID;
2994 pfxdata.validity.define_extent = 1;
2995
2996 /* private uid is kept up to date, conf_data may be outdated */
2997 if (startpriv->uid.type != UA_BASE_DEVICE) {
2998 pfxdata.validity.verify_base = 1;
2999 if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
3000 pfxdata.validity.hyper_pav = 1;
3001 }
3002
3003 switch (cmd) {
3004 case DASD_ECKD_CCW_READ_TRACK_DATA:
3005 dedata->mask.perm = 0x1;
3006 dedata->attributes.operation = basepriv->attrib.operation;
3007 dedata->blk_size = blksize;
3008 dedata->ga_extended |= 0x42;
3009 lredata->operation.orientation = 0x0;
3010 lredata->operation.operation = 0x0C;
3011 lredata->auxiliary.check_bytes = 0x01;
3012 pfx_cmd = DASD_ECKD_CCW_PFX_READ;
3013 break;
3014 case DASD_ECKD_CCW_WRITE_TRACK_DATA:
3015 dedata->mask.perm = 0x02;
3016 dedata->attributes.operation = basepriv->attrib.operation;
3017 dedata->blk_size = blksize;
3018 rc = check_XRC_on_prefix(&pfxdata, basedev);
3019 dedata->ga_extended |= 0x42;
3020 lredata->operation.orientation = 0x0;
3021 lredata->operation.operation = 0x3F;
3022 lredata->extended_operation = 0x23;
3023 lredata->auxiliary.check_bytes = 0x2;
3024 pfx_cmd = DASD_ECKD_CCW_PFX;
3025 break;
3026 default:
3027 DBF_DEV_EVENT(DBF_ERR, basedev,
3028 "prepare itcw, unknown opcode 0x%x", cmd);
3029 BUG();
3030 break;
3031 }
3032 if (rc)
3033 return rc;
3034
3035 dedata->attributes.mode = 0x3; /* ECKD */
3036
3037 heads = basepriv->rdc_data.trk_per_cyl;
3038 begcyl = trk / heads;
3039 beghead = trk % heads;
3040 endcyl = totrk / heads;
3041 endhead = totrk % heads;
3042
3043 /* check for sequential prestage - enhance cylinder range */
3044 if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
3045 dedata->attributes.operation == DASD_SEQ_ACCESS) {
3046
3047 if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
3048 endcyl += basepriv->attrib.nr_cyl;
3049 else
3050 endcyl = (basepriv->real_cyl - 1);
3051 }
3052
3053 set_ch_t(&dedata->beg_ext, begcyl, beghead);
3054 set_ch_t(&dedata->end_ext, endcyl, endhead);
3055
3056 dedata->ep_format = 0x20; /* records per track is valid */
3057 dedata->ep_rec_per_track = blk_per_trk;
3058
3059 if (rec_on_trk) {
3060 switch (basepriv->rdc_data.dev_type) {
3061 case 0x3390:
3062 dn = ceil_quot(blksize + 6, 232);
3063 d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
3064 sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
3065 break;
3066 case 0x3380:
3067 d = 7 + ceil_quot(blksize + 12, 32);
3068 sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
3069 break;
3070 }
3071 }
3072
3073 lredata->auxiliary.length_valid = 1;
3074 lredata->auxiliary.length_scope = 1;
3075 lredata->auxiliary.imbedded_ccw_valid = 1;
3076 lredata->length = tlf;
3077 lredata->imbedded_ccw = cmd;
3078 lredata->count = count;
3079 lredata->sector = sector;
3080 set_ch_t(&lredata->seek_addr, begcyl, beghead);
3081 lredata->search_arg.cyl = lredata->seek_addr.cyl;
3082 lredata->search_arg.head = lredata->seek_addr.head;
3083 lredata->search_arg.record = rec_on_trk;
3084
3085 dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
3086 &pfxdata, sizeof(pfxdata), total_data_size);
82a7b955 3087 return PTR_RET(dcw);
f3eb5384
SW
3088}
3089
3090static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
3091 struct dasd_device *startdev,
3092 struct dasd_block *block,
3093 struct request *req,
3094 sector_t first_rec,
3095 sector_t last_rec,
3096 sector_t first_trk,
3097 sector_t last_trk,
3098 unsigned int first_offs,
3099 unsigned int last_offs,
3100 unsigned int blk_per_trk,
3101 unsigned int blksize)
3102{
f3eb5384
SW
3103 struct dasd_ccw_req *cqr;
3104 struct req_iterator iter;
7988613b 3105 struct bio_vec bv;
f3eb5384
SW
3106 char *dst;
3107 unsigned int trkcount, ctidaw;
3108 unsigned char cmd;
3109 struct dasd_device *basedev;
3110 unsigned int tlf;
3111 struct itcw *itcw;
3112 struct tidaw *last_tidaw = NULL;
3113 int itcw_op;
3114 size_t itcw_size;
ef19298b
SW
3115 u8 tidaw_flags;
3116 unsigned int seg_len, part_len, len_to_track_end;
3117 unsigned char new_track;
3118 sector_t recid, trkid;
3119 unsigned int offs;
3120 unsigned int count, count_to_trk_end;
cd10502b 3121 int ret;
f3eb5384
SW
3122
3123 basedev = block->base;
f3eb5384
SW
3124 if (rq_data_dir(req) == READ) {
3125 cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
3126 itcw_op = ITCW_OP_READ;
3127 } else if (rq_data_dir(req) == WRITE) {
3128 cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
3129 itcw_op = ITCW_OP_WRITE;
3130 } else
3131 return ERR_PTR(-EINVAL);
3132
3133 /* trackbased I/O needs address all memory via TIDAWs,
3134 * not just for 64 bit addresses. This allows us to map
3135 * each segment directly to one tidaw.
ef19298b
SW
3136 * In the case of write requests, additional tidaws may
3137 * be needed when a segment crosses a track boundary.
f3eb5384
SW
3138 */
3139 trkcount = last_trk - first_trk + 1;
3140 ctidaw = 0;
3141 rq_for_each_segment(bv, req, iter) {
3142 ++ctidaw;
3143 }
ef19298b
SW
3144 if (rq_data_dir(req) == WRITE)
3145 ctidaw += (last_trk - first_trk);
f3eb5384
SW
3146
3147 /* Allocate the ccw request. */
3148 itcw_size = itcw_calc_size(0, ctidaw, 0);
68b781fe 3149 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
f3eb5384
SW
3150 if (IS_ERR(cqr))
3151 return cqr;
3152
f3eb5384
SW
3153 /* transfer length factor: how many bytes to read from the last track */
3154 if (first_trk == last_trk)
3155 tlf = last_offs - first_offs + 1;
3156 else
3157 tlf = last_offs + 1;
3158 tlf *= blksize;
3159
3160 itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
ef19298b 3161 if (IS_ERR(itcw)) {
cd10502b
JL
3162 ret = -EINVAL;
3163 goto out_error;
ef19298b 3164 }
f3eb5384 3165 cqr->cpaddr = itcw_get_tcw(itcw);
f3eb5384
SW
3166 if (prepare_itcw(itcw, first_trk, last_trk,
3167 cmd, basedev, startdev,
3168 first_offs + 1,
3169 trkcount, blksize,
3170 (last_rec - first_rec + 1) * blksize,
3171 tlf, blk_per_trk) == -EAGAIN) {
3172 /* Clock not in sync and XRC is enabled.
3173 * Try again later.
3174 */
cd10502b
JL
3175 ret = -EAGAIN;
3176 goto out_error;
f3eb5384 3177 }
d54cddb6 3178 len_to_track_end = 0;
f3eb5384
SW
3179 /*
3180 * A tidaw can address 4k of memory, but must not cross page boundaries
3181 * We can let the block layer handle this by setting
3182 * blk_queue_segment_boundary to page boundaries and
3183 * blk_max_segment_size to page size when setting up the request queue.
ef19298b
SW
3184 * For write requests, a TIDAW must not cross track boundaries, because
3185 * we have to set the CBC flag on the last tidaw for each track.
f3eb5384 3186 */
ef19298b
SW
3187 if (rq_data_dir(req) == WRITE) {
3188 new_track = 1;
3189 recid = first_rec;
3190 rq_for_each_segment(bv, req, iter) {
7988613b
KO
3191 dst = page_address(bv.bv_page) + bv.bv_offset;
3192 seg_len = bv.bv_len;
ef19298b
SW
3193 while (seg_len) {
3194 if (new_track) {
3195 trkid = recid;
3196 offs = sector_div(trkid, blk_per_trk);
3197 count_to_trk_end = blk_per_trk - offs;
3198 count = min((last_rec - recid + 1),
3199 (sector_t)count_to_trk_end);
3200 len_to_track_end = count * blksize;
3201 recid += count;
3202 new_track = 0;
3203 }
3204 part_len = min(seg_len, len_to_track_end);
3205 seg_len -= part_len;
3206 len_to_track_end -= part_len;
3207 /* We need to end the tidaw at track end */
3208 if (!len_to_track_end) {
3209 new_track = 1;
3210 tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
3211 } else
3212 tidaw_flags = 0;
3213 last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
3214 dst, part_len);
cd10502b
JL
3215 if (IS_ERR(last_tidaw)) {
3216 ret = -EINVAL;
3217 goto out_error;
3218 }
ef19298b
SW
3219 dst += part_len;
3220 }
3221 }
3222 } else {
3223 rq_for_each_segment(bv, req, iter) {
7988613b 3224 dst = page_address(bv.bv_page) + bv.bv_offset;
ef19298b 3225 last_tidaw = itcw_add_tidaw(itcw, 0x00,
7988613b 3226 dst, bv.bv_len);
cd10502b
JL
3227 if (IS_ERR(last_tidaw)) {
3228 ret = -EINVAL;
3229 goto out_error;
3230 }
ef19298b 3231 }
f3eb5384 3232 }
ef19298b
SW
3233 last_tidaw->flags |= TIDAW_FLAGS_LAST;
3234 last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
f3eb5384
SW
3235 itcw_finalize(itcw);
3236
3237 if (blk_noretry_request(req) ||
3238 block->base->features & DASD_FEATURE_FAILFAST)
3239 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
ef19298b 3240 cqr->cpmode = 1;
f3eb5384
SW
3241 cqr->startdev = startdev;
3242 cqr->memdev = startdev;
3243 cqr->block = block;
7c8faa86 3244 cqr->expires = startdev->default_expires * HZ; /* default 5 minutes */
a4d26c6a 3245 cqr->lpm = startdev->path_data.ppm;
1f1ee9ad 3246 cqr->retries = startdev->default_retries;
1aae0560 3247 cqr->buildclk = get_tod_clock();
f3eb5384
SW
3248 cqr->status = DASD_CQR_FILLED;
3249 return cqr;
cd10502b
JL
3250out_error:
3251 dasd_sfree_request(cqr, startdev);
3252 return ERR_PTR(ret);
f3eb5384
SW
3253}
3254
3255static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
3256 struct dasd_block *block,
3257 struct request *req)
3258{
ef19298b 3259 int cmdrtd, cmdwtd;
f3eb5384 3260 int use_prefix;
ef19298b 3261 int fcx_multitrack;
45b44d76 3262 struct dasd_eckd_private *private;
f3eb5384
SW
3263 struct dasd_device *basedev;
3264 sector_t first_rec, last_rec;
3265 sector_t first_trk, last_trk;
3266 unsigned int first_offs, last_offs;
3267 unsigned int blk_per_trk, blksize;
3268 int cdlspecial;
ef19298b 3269 unsigned int data_size;
f3eb5384
SW
3270 struct dasd_ccw_req *cqr;
3271
3272 basedev = block->base;
543691a4 3273 private = basedev->private;
f3eb5384
SW
3274
3275 /* Calculate number of blocks/records per track. */
3276 blksize = block->bp_block;
3277 blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
6fca97a9
SH
3278 if (blk_per_trk == 0)
3279 return ERR_PTR(-EINVAL);
f3eb5384 3280 /* Calculate record id of first and last block. */
83096ebf 3281 first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
f3eb5384
SW
3282 first_offs = sector_div(first_trk, blk_per_trk);
3283 last_rec = last_trk =
83096ebf 3284 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
f3eb5384
SW
3285 last_offs = sector_div(last_trk, blk_per_trk);
3286 cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
3287
ef19298b
SW
3288 fcx_multitrack = private->features.feature[40] & 0x20;
3289 data_size = blk_rq_bytes(req);
26a35f37
SW
3290 if (data_size % blksize)
3291 return ERR_PTR(-EINVAL);
ef19298b
SW
3292 /* tpm write request add CBC data on each track boundary */
3293 if (rq_data_dir(req) == WRITE)
3294 data_size += (last_trk - first_trk) * 4;
f3eb5384
SW
3295
3296 /* is read track data and write track data in command mode supported? */
3297 cmdrtd = private->features.feature[9] & 0x20;
3298 cmdwtd = private->features.feature[12] & 0x40;
3299 use_prefix = private->features.feature[8] & 0x01;
3300
3301 cqr = NULL;
3302 if (cdlspecial || dasd_page_cache) {
3303 /* do nothing, just fall through to the cmd mode single case */
ef19298b
SW
3304 } else if ((data_size <= private->fcx_max_data)
3305 && (fcx_multitrack || (first_trk == last_trk))) {
f3eb5384
SW
3306 cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
3307 first_rec, last_rec,
3308 first_trk, last_trk,
3309 first_offs, last_offs,
3310 blk_per_trk, blksize);
ef19298b
SW
3311 if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3312 (PTR_ERR(cqr) != -ENOMEM))
f3eb5384
SW
3313 cqr = NULL;
3314 } else if (use_prefix &&
3315 (((rq_data_dir(req) == READ) && cmdrtd) ||
3316 ((rq_data_dir(req) == WRITE) && cmdwtd))) {
3317 cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
3318 first_rec, last_rec,
3319 first_trk, last_trk,
3320 first_offs, last_offs,
3321 blk_per_trk, blksize);
ef19298b
SW
3322 if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3323 (PTR_ERR(cqr) != -ENOMEM))
f3eb5384
SW
3324 cqr = NULL;
3325 }
3326 if (!cqr)
3327 cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
3328 first_rec, last_rec,
3329 first_trk, last_trk,
3330 first_offs, last_offs,
3331 blk_per_trk, blksize);
3332 return cqr;
3333}
3334
e4dbb0f2
SH
3335static struct dasd_ccw_req *dasd_raw_build_cp(struct dasd_device *startdev,
3336 struct dasd_block *block,
3337 struct request *req)
3338{
e4dbb0f2
SH
3339 unsigned long *idaws;
3340 struct dasd_device *basedev;
3341 struct dasd_ccw_req *cqr;
3342 struct ccw1 *ccw;
3343 struct req_iterator iter;
7988613b 3344 struct bio_vec bv;
e4dbb0f2
SH
3345 char *dst;
3346 unsigned char cmd;
3347 unsigned int trkcount;
3348 unsigned int seg_len, len_to_track_end;
3349 unsigned int first_offs;
3350 unsigned int cidaw, cplength, datasize;
558b9ef0
SW
3351 sector_t first_trk, last_trk, sectors;
3352 sector_t start_padding_sectors, end_sector_offset, end_padding_sectors;
e4dbb0f2
SH
3353 unsigned int pfx_datasize;
3354
3355 /*
3356 * raw track access needs to be mutiple of 64k and on 64k boundary
558b9ef0
SW
3357 * For read requests we can fix an incorrect alignment by padding
3358 * the request with dummy pages.
e4dbb0f2 3359 */
558b9ef0
SW
3360 start_padding_sectors = blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK;
3361 end_sector_offset = (blk_rq_pos(req) + blk_rq_sectors(req)) %
3362 DASD_RAW_SECTORS_PER_TRACK;
3363 end_padding_sectors = (DASD_RAW_SECTORS_PER_TRACK - end_sector_offset) %
3364 DASD_RAW_SECTORS_PER_TRACK;
3365 basedev = block->base;
3366 if ((start_padding_sectors || end_padding_sectors) &&
3367 (rq_data_dir(req) == WRITE)) {
3368 DBF_DEV_EVENT(DBF_ERR, basedev,
3369 "raw write not track aligned (%lu,%lu) req %p",
3370 start_padding_sectors, end_padding_sectors, req);
e4dbb0f2
SH
3371 cqr = ERR_PTR(-EINVAL);
3372 goto out;
3373 }
3374
3375 first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
3376 last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
3377 DASD_RAW_SECTORS_PER_TRACK;
3378 trkcount = last_trk - first_trk + 1;
3379 first_offs = 0;
e4dbb0f2
SH
3380
3381 if (rq_data_dir(req) == READ)
3382 cmd = DASD_ECKD_CCW_READ_TRACK;
3383 else if (rq_data_dir(req) == WRITE)
3384 cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
3385 else {
3386 cqr = ERR_PTR(-EINVAL);
3387 goto out;
3388 }
3389
3390 /*
3391 * Raw track based I/O needs IDAWs for each page,
3392 * and not just for 64 bit addresses.
3393 */
3394 cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;
3395
3396 /* 1x prefix + one read/write ccw per track */
3397 cplength = 1 + trkcount;
3398
3399 /*
3400 * struct PFX_eckd_data has up to 2 byte as extended parameter
3401 * this is needed for write full track and has to be mentioned
25985edc 3402 * separately
e4dbb0f2
SH
3403 * add 8 instead of 2 to keep 8 byte boundary
3404 */
3405 pfx_datasize = sizeof(struct PFX_eckd_data) + 8;
3406
3407 datasize = pfx_datasize + cidaw * sizeof(unsigned long long);
3408
3409 /* Allocate the ccw request. */
3410 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
3411 datasize, startdev);
3412 if (IS_ERR(cqr))
3413 goto out;
3414 ccw = cqr->cpaddr;
3415
3416 if (prefix_LRE(ccw++, cqr->data, first_trk, last_trk, cmd,
3417 basedev, startdev, 1 /* format */, first_offs + 1,
3418 trkcount, 0, 0) == -EAGAIN) {
3419 /* Clock not in sync and XRC is enabled.
3420 * Try again later.
3421 */
3422 dasd_sfree_request(cqr, startdev);
3423 cqr = ERR_PTR(-EAGAIN);
3424 goto out;
3425 }
3426
3427 idaws = (unsigned long *)(cqr->data + pfx_datasize);
e4dbb0f2 3428 len_to_track_end = 0;
558b9ef0
SW
3429 if (start_padding_sectors) {
3430 ccw[-1].flags |= CCW_FLAG_CC;
3431 ccw->cmd_code = cmd;
3432 /* maximum 3390 track size */
3433 ccw->count = 57326;
3434 /* 64k map to one track */
3435 len_to_track_end = 65536 - start_padding_sectors * 512;
3436 ccw->cda = (__u32)(addr_t)idaws;
3437 ccw->flags |= CCW_FLAG_IDA;
3438 ccw->flags |= CCW_FLAG_SLI;
3439 ccw++;
3440 for (sectors = 0; sectors < start_padding_sectors; sectors += 8)
3441 idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3442 }
e4dbb0f2 3443 rq_for_each_segment(bv, req, iter) {
7988613b
KO
3444 dst = page_address(bv.bv_page) + bv.bv_offset;
3445 seg_len = bv.bv_len;
558b9ef0
SW
3446 if (cmd == DASD_ECKD_CCW_READ_TRACK)
3447 memset(dst, 0, seg_len);
e4dbb0f2
SH
3448 if (!len_to_track_end) {
3449 ccw[-1].flags |= CCW_FLAG_CC;
3450 ccw->cmd_code = cmd;
3451 /* maximum 3390 track size */
3452 ccw->count = 57326;
3453 /* 64k map to one track */
3454 len_to_track_end = 65536;
3455 ccw->cda = (__u32)(addr_t)idaws;
3456 ccw->flags |= CCW_FLAG_IDA;
3457 ccw->flags |= CCW_FLAG_SLI;
3458 ccw++;
3459 }
3460 len_to_track_end -= seg_len;
3461 idaws = idal_create_words(idaws, dst, seg_len);
3462 }
558b9ef0
SW
3463 for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
3464 idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
e4dbb0f2
SH
3465 if (blk_noretry_request(req) ||
3466 block->base->features & DASD_FEATURE_FAILFAST)
3467 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3468 cqr->startdev = startdev;
3469 cqr->memdev = startdev;
3470 cqr->block = block;
3471 cqr->expires = startdev->default_expires * HZ;
3472 cqr->lpm = startdev->path_data.ppm;
1f1ee9ad 3473 cqr->retries = startdev->default_retries;
1aae0560 3474 cqr->buildclk = get_tod_clock();
e4dbb0f2
SH
3475 cqr->status = DASD_CQR_FILLED;
3476
3477 if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
3478 cqr = NULL;
3479out:
3480 return cqr;
3481}
3482
3483
1da177e4
LT
3484static int
3485dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
3486{
3487 struct dasd_eckd_private *private;
3488 struct ccw1 *ccw;
5705f702 3489 struct req_iterator iter;
7988613b 3490 struct bio_vec bv;
1da177e4
LT
3491 char *dst, *cda;
3492 unsigned int blksize, blk_per_trk, off;
3493 sector_t recid;
5705f702 3494 int status;
1da177e4
LT
3495
3496 if (!dasd_page_cache)
3497 goto out;
543691a4 3498 private = cqr->block->base->private;
8e09f215 3499 blksize = cqr->block->bp_block;
1da177e4 3500 blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
83096ebf 3501 recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
1da177e4
LT
3502 ccw = cqr->cpaddr;
3503 /* Skip over define extent & locate record. */
3504 ccw++;
3505 if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
3506 ccw++;
5705f702 3507 rq_for_each_segment(bv, req, iter) {
7988613b
KO
3508 dst = page_address(bv.bv_page) + bv.bv_offset;
3509 for (off = 0; off < bv.bv_len; off += blksize) {
1da177e4
LT
3510 /* Skip locate record. */
3511 if (private->uses_cdl && recid <= 2*blk_per_trk)
3512 ccw++;
3513 if (dst) {
3514 if (ccw->flags & CCW_FLAG_IDA)
3515 cda = *((char **)((addr_t) ccw->cda));
3516 else
3517 cda = (char *)((addr_t) ccw->cda);
3518 if (dst != cda) {
3519 if (rq_data_dir(req) == READ)
7988613b 3520 memcpy(dst, cda, bv.bv_len);
1da177e4
LT
3521 kmem_cache_free(dasd_page_cache,
3522 (void *)((addr_t)cda & PAGE_MASK));
3523 }
3524 dst = NULL;
3525 }
3526 ccw++;
3527 recid++;
3528 }
3529 }
3530out:
3531 status = cqr->status == DASD_CQR_DONE;
8e09f215 3532 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3533 return status;
3534}
3535
8e09f215 3536/*
f3eb5384 3537 * Modify ccw/tcw in cqr so it can be started on a base device.
8e09f215
SW
3538 *
3539 * Note that this is not enough to restart the cqr!
3540 * Either reset cqr->startdev as well (summary unit check handling)
3541 * or restart via separate cqr (as in ERP handling).
3542 */
3543void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
3544{
3545 struct ccw1 *ccw;
3546 struct PFX_eckd_data *pfxdata;
f3eb5384
SW
3547 struct tcw *tcw;
3548 struct tccb *tccb;
3549 struct dcw *dcw;
3550
3551 if (cqr->cpmode == 1) {
3552 tcw = cqr->cpaddr;
3553 tccb = tcw_get_tccb(tcw);
3554 dcw = (struct dcw *)&tccb->tca[0];
3555 pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
8e09f215
SW
3556 pfxdata->validity.verify_base = 0;
3557 pfxdata->validity.hyper_pav = 0;
f3eb5384
SW
3558 } else {
3559 ccw = cqr->cpaddr;
3560 pfxdata = cqr->data;
3561 if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
3562 pfxdata->validity.verify_base = 0;
3563 pfxdata->validity.hyper_pav = 0;
3564 }
8e09f215
SW
3565 }
3566}
3567
3568#define DASD_ECKD_CHANQ_MAX_SIZE 4
3569
3570static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
3571 struct dasd_block *block,
3572 struct request *req)
3573{
3574 struct dasd_eckd_private *private;
3575 struct dasd_device *startdev;
3576 unsigned long flags;
3577 struct dasd_ccw_req *cqr;
3578
3579 startdev = dasd_alias_get_start_dev(base);
3580 if (!startdev)
3581 startdev = base;
543691a4 3582 private = startdev->private;
8e09f215
SW
3583 if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
3584 return ERR_PTR(-EBUSY);
3585
3586 spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
3587 private->count++;
e4dbb0f2
SH
3588 if ((base->features & DASD_FEATURE_USERAW))
3589 cqr = dasd_raw_build_cp(startdev, block, req);
3590 else
3591 cqr = dasd_eckd_build_cp(startdev, block, req);
8e09f215
SW
3592 if (IS_ERR(cqr))
3593 private->count--;
3594 spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
3595 return cqr;
3596}
3597
3598static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
3599 struct request *req)
3600{
3601 struct dasd_eckd_private *private;
3602 unsigned long flags;
3603
3604 spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
543691a4 3605 private = cqr->memdev->private;
8e09f215
SW
3606 private->count--;
3607 spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
3608 return dasd_eckd_free_cp(cqr, req);
3609}
3610
1da177e4
LT
3611static int
3612dasd_eckd_fill_info(struct dasd_device * device,
3613 struct dasd_information2_t * info)
3614{
543691a4 3615 struct dasd_eckd_private *private = device->private;
1da177e4 3616
1da177e4
LT
3617 info->label_block = 2;
3618 info->FBA_layout = private->uses_cdl ? 0 : 1;
3619 info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
543691a4 3620 info->characteristics_size = sizeof(private->rdc_data);
1da177e4 3621 memcpy(info->characteristics, &private->rdc_data,
543691a4 3622 sizeof(private->rdc_data));
4abb08c2
SW
3623 info->confdata_size = min((unsigned long)private->conf_len,
3624 sizeof(info->configuration_data));
3625 memcpy(info->configuration_data, private->conf_data,
3626 info->confdata_size);
1da177e4
LT
3627 return 0;
3628}
3629
3630/*
3631 * SECTION: ioctl functions for eckd devices.
3632 */
3633
3634/*
3635 * Release device ioctl.
138c014d 3636 * Buils a channel programm to releases a prior reserved
1da177e4
LT
3637 * (see dasd_eckd_reserve) device.
3638 */
3639static int
1107ccfb 3640dasd_eckd_release(struct dasd_device *device)
1da177e4 3641{
1da177e4
LT
3642 struct dasd_ccw_req *cqr;
3643 int rc;
f3eb5384 3644 struct ccw1 *ccw;
f932bcea 3645 int useglobal;
1da177e4
LT
3646
3647 if (!capable(CAP_SYS_ADMIN))
3648 return -EACCES;
3649
f932bcea 3650 useglobal = 0;
68b781fe 3651 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
1da177e4 3652 if (IS_ERR(cqr)) {
f932bcea
SW
3653 mutex_lock(&dasd_reserve_mutex);
3654 useglobal = 1;
3655 cqr = &dasd_reserve_req->cqr;
3656 memset(cqr, 0, sizeof(*cqr));
3657 memset(&dasd_reserve_req->ccw, 0,
3658 sizeof(dasd_reserve_req->ccw));
3659 cqr->cpaddr = &dasd_reserve_req->ccw;
3660 cqr->data = &dasd_reserve_req->data;
3661 cqr->magic = DASD_ECKD_MAGIC;
1da177e4 3662 }
f3eb5384
SW
3663 ccw = cqr->cpaddr;
3664 ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
3665 ccw->flags |= CCW_FLAG_SLI;
3666 ccw->count = 32;
3667 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
3668 cqr->startdev = device;
3669 cqr->memdev = device;
1da177e4 3670 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1c01b8a5 3671 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
336c340b 3672 cqr->retries = 2; /* set retry counter to enable basic ERP */
1da177e4 3673 cqr->expires = 2 * HZ;
1aae0560 3674 cqr->buildclk = get_tod_clock();
1da177e4
LT
3675 cqr->status = DASD_CQR_FILLED;
3676
3677 rc = dasd_sleep_on_immediatly(cqr);
5a27e60d
SW
3678 if (!rc)
3679 clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
1da177e4 3680
f932bcea
SW
3681 if (useglobal)
3682 mutex_unlock(&dasd_reserve_mutex);
3683 else
3684 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3685 return rc;
3686}
3687
3688/*
3689 * Reserve device ioctl.
3690 * Options are set to 'synchronous wait for interrupt' and
138c014d
HH
3691 * 'timeout the request'. This leads to a terminate IO if
3692 * the interrupt is outstanding for a certain time.
1da177e4
LT
3693 */
3694static int
1107ccfb 3695dasd_eckd_reserve(struct dasd_device *device)
1da177e4 3696{
1da177e4
LT
3697 struct dasd_ccw_req *cqr;
3698 int rc;
f3eb5384 3699 struct ccw1 *ccw;
f932bcea 3700 int useglobal;
1da177e4
LT
3701
3702 if (!capable(CAP_SYS_ADMIN))
3703 return -EACCES;
3704
f932bcea 3705 useglobal = 0;
68b781fe 3706 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
1da177e4 3707 if (IS_ERR(cqr)) {
f932bcea
SW
3708 mutex_lock(&dasd_reserve_mutex);
3709 useglobal = 1;
3710 cqr = &dasd_reserve_req->cqr;
3711 memset(cqr, 0, sizeof(*cqr));
3712 memset(&dasd_reserve_req->ccw, 0,
3713 sizeof(dasd_reserve_req->ccw));
3714 cqr->cpaddr = &dasd_reserve_req->ccw;
3715 cqr->data = &dasd_reserve_req->data;
3716 cqr->magic = DASD_ECKD_MAGIC;
1da177e4 3717 }
f3eb5384
SW
3718 ccw = cqr->cpaddr;
3719 ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
3720 ccw->flags |= CCW_FLAG_SLI;
3721 ccw->count = 32;
3722 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
3723 cqr->startdev = device;
3724 cqr->memdev = device;
1da177e4 3725 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1c01b8a5 3726 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
336c340b 3727 cqr->retries = 2; /* set retry counter to enable basic ERP */
1da177e4 3728 cqr->expires = 2 * HZ;
1aae0560 3729 cqr->buildclk = get_tod_clock();
1da177e4
LT
3730 cqr->status = DASD_CQR_FILLED;
3731
3732 rc = dasd_sleep_on_immediatly(cqr);
5a27e60d
SW
3733 if (!rc)
3734 set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
1da177e4 3735
f932bcea
SW
3736 if (useglobal)
3737 mutex_unlock(&dasd_reserve_mutex);
3738 else
3739 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3740 return rc;
3741}
3742
3743/*
3744 * Steal lock ioctl - unconditional reserve device.
138c014d 3745 * Buils a channel programm to break a device's reservation.
1da177e4
LT
3746 * (unconditional reserve)
3747 */
3748static int
1107ccfb 3749dasd_eckd_steal_lock(struct dasd_device *device)
1da177e4 3750{
1da177e4
LT
3751 struct dasd_ccw_req *cqr;
3752 int rc;
f3eb5384 3753 struct ccw1 *ccw;
f932bcea 3754 int useglobal;
1da177e4
LT
3755
3756 if (!capable(CAP_SYS_ADMIN))
3757 return -EACCES;
3758
f932bcea 3759 useglobal = 0;
68b781fe 3760 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
1da177e4 3761 if (IS_ERR(cqr)) {
f932bcea
SW
3762 mutex_lock(&dasd_reserve_mutex);
3763 useglobal = 1;
3764 cqr = &dasd_reserve_req->cqr;
3765 memset(cqr, 0, sizeof(*cqr));
3766 memset(&dasd_reserve_req->ccw, 0,
3767 sizeof(dasd_reserve_req->ccw));
3768 cqr->cpaddr = &dasd_reserve_req->ccw;
3769 cqr->data = &dasd_reserve_req->data;
3770 cqr->magic = DASD_ECKD_MAGIC;
1da177e4 3771 }
f3eb5384
SW
3772 ccw = cqr->cpaddr;
3773 ccw->cmd_code = DASD_ECKD_CCW_SLCK;
3774 ccw->flags |= CCW_FLAG_SLI;
3775 ccw->count = 32;
3776 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
3777 cqr->startdev = device;
3778 cqr->memdev = device;
1da177e4 3779 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1c01b8a5 3780 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
336c340b 3781 cqr->retries = 2; /* set retry counter to enable basic ERP */
1da177e4 3782 cqr->expires = 2 * HZ;
1aae0560 3783 cqr->buildclk = get_tod_clock();
1da177e4
LT
3784 cqr->status = DASD_CQR_FILLED;
3785
3786 rc = dasd_sleep_on_immediatly(cqr);
5a27e60d
SW
3787 if (!rc)
3788 set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
1da177e4 3789
f932bcea
SW
3790 if (useglobal)
3791 mutex_unlock(&dasd_reserve_mutex);
3792 else
3793 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3794 return rc;
3795}
3796
196339f1
SW
3797/*
3798 * SNID - Sense Path Group ID
3799 * This ioctl may be used in situations where I/O is stalled due to
3800 * a reserve, so if the normal dasd_smalloc_request fails, we use the
3801 * preallocated dasd_reserve_req.
3802 */
3803static int dasd_eckd_snid(struct dasd_device *device,
3804 void __user *argp)
3805{
3806 struct dasd_ccw_req *cqr;
3807 int rc;
3808 struct ccw1 *ccw;
3809 int useglobal;
3810 struct dasd_snid_ioctl_data usrparm;
3811
3812 if (!capable(CAP_SYS_ADMIN))
3813 return -EACCES;
3814
3815 if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3816 return -EFAULT;
3817
3818 useglobal = 0;
3819 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
3820 sizeof(struct dasd_snid_data), device);
3821 if (IS_ERR(cqr)) {
3822 mutex_lock(&dasd_reserve_mutex);
3823 useglobal = 1;
3824 cqr = &dasd_reserve_req->cqr;
3825 memset(cqr, 0, sizeof(*cqr));
3826 memset(&dasd_reserve_req->ccw, 0,
3827 sizeof(dasd_reserve_req->ccw));
3828 cqr->cpaddr = &dasd_reserve_req->ccw;
3829 cqr->data = &dasd_reserve_req->data;
3830 cqr->magic = DASD_ECKD_MAGIC;
3831 }
3832 ccw = cqr->cpaddr;
3833 ccw->cmd_code = DASD_ECKD_CCW_SNID;
3834 ccw->flags |= CCW_FLAG_SLI;
3835 ccw->count = 12;
3836 ccw->cda = (__u32)(addr_t) cqr->data;
3837 cqr->startdev = device;
3838 cqr->memdev = device;
3839 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
3840 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
5a27e60d 3841 set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
196339f1
SW
3842 cqr->retries = 5;
3843 cqr->expires = 10 * HZ;
1aae0560 3844 cqr->buildclk = get_tod_clock();
196339f1
SW
3845 cqr->status = DASD_CQR_FILLED;
3846 cqr->lpm = usrparm.path_mask;
3847
3848 rc = dasd_sleep_on_immediatly(cqr);
3849 /* verify that I/O processing didn't modify the path mask */
3850 if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
3851 rc = -EIO;
3852 if (!rc) {
3853 usrparm.data = *((struct dasd_snid_data *)cqr->data);
3854 if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
3855 rc = -EFAULT;
3856 }
3857
3858 if (useglobal)
3859 mutex_unlock(&dasd_reserve_mutex);
3860 else
3861 dasd_sfree_request(cqr, cqr->memdev);
3862 return rc;
3863}
3864
1da177e4
LT
3865/*
3866 * Read performance statistics
3867 */
3868static int
1107ccfb 3869dasd_eckd_performance(struct dasd_device *device, void __user *argp)
1da177e4 3870{
1da177e4
LT
3871 struct dasd_psf_prssd_data *prssdp;
3872 struct dasd_rssd_perf_stats_t *stats;
3873 struct dasd_ccw_req *cqr;
3874 struct ccw1 *ccw;
3875 int rc;
3876
68b781fe 3877 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
8e09f215
SW
3878 (sizeof(struct dasd_psf_prssd_data) +
3879 sizeof(struct dasd_rssd_perf_stats_t)),
1da177e4
LT
3880 device);
3881 if (IS_ERR(cqr)) {
fc19f381 3882 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
3883 "Could not allocate initialization request");
3884 return PTR_ERR(cqr);
3885 }
8e09f215
SW
3886 cqr->startdev = device;
3887 cqr->memdev = device;
1da177e4 3888 cqr->retries = 0;
eb6e199b 3889 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1da177e4
LT
3890 cqr->expires = 10 * HZ;
3891
3892 /* Prepare for Read Subsystem Data */
3893 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
8e09f215 3894 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1da177e4 3895 prssdp->order = PSF_ORDER_PRSSD;
5d67d164 3896 prssdp->suborder = 0x01; /* Performance Statistics */
1da177e4
LT
3897 prssdp->varies[1] = 0x01; /* Perf Statistics for the Subsystem */
3898
3899 ccw = cqr->cpaddr;
3900 ccw->cmd_code = DASD_ECKD_CCW_PSF;
8e09f215 3901 ccw->count = sizeof(struct dasd_psf_prssd_data);
1da177e4
LT
3902 ccw->flags |= CCW_FLAG_CC;
3903 ccw->cda = (__u32)(addr_t) prssdp;
3904
3905 /* Read Subsystem Data - Performance Statistics */
3906 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
8e09f215 3907 memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
1da177e4
LT
3908
3909 ccw++;
3910 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
8e09f215 3911 ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
1da177e4
LT
3912 ccw->cda = (__u32)(addr_t) stats;
3913
1aae0560 3914 cqr->buildclk = get_tod_clock();
1da177e4
LT
3915 cqr->status = DASD_CQR_FILLED;
3916 rc = dasd_sleep_on(cqr);
3917 if (rc == 0) {
1da177e4
LT
3918 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
3919 stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
1107ccfb
CH
3920 if (copy_to_user(argp, stats,
3921 sizeof(struct dasd_rssd_perf_stats_t)))
3922 rc = -EFAULT;
1da177e4 3923 }
8e09f215 3924 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
3925 return rc;
3926}
3927
3928/*
3929 * Get attributes (cache operations)
3930 * Returnes the cache attributes used in Define Extend (DE).
3931 */
3932static int
1107ccfb 3933dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
1da177e4 3934{
543691a4 3935 struct dasd_eckd_private *private = device->private;
1107ccfb 3936 struct attrib_data_t attrib = private->attrib;
1da177e4
LT
3937 int rc;
3938
3939 if (!capable(CAP_SYS_ADMIN))
3940 return -EACCES;
1107ccfb 3941 if (!argp)
1da177e4
LT
3942 return -EINVAL;
3943
1107ccfb
CH
3944 rc = 0;
3945 if (copy_to_user(argp, (long *) &attrib,
8e09f215 3946 sizeof(struct attrib_data_t)))
1107ccfb 3947 rc = -EFAULT;
1da177e4
LT
3948
3949 return rc;
3950}
3951
3952/*
3953 * Set attributes (cache operations)
3954 * Stores the attributes for cache operation to be used in Define Extend (DE).
3955 */
3956static int
1107ccfb 3957dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
1da177e4 3958{
543691a4 3959 struct dasd_eckd_private *private = device->private;
1da177e4
LT
3960 struct attrib_data_t attrib;
3961
3962 if (!capable(CAP_SYS_ADMIN))
3963 return -EACCES;
1107ccfb 3964 if (!argp)
1da177e4
LT
3965 return -EINVAL;
3966
1107ccfb 3967 if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
1da177e4 3968 return -EFAULT;
1da177e4
LT
3969 private->attrib = attrib;
3970
fc19f381
SH
3971 dev_info(&device->cdev->dev,
3972 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
3973 private->attrib.operation, private->attrib.nr_cyl);
1da177e4
LT
3974 return 0;
3975}
3976
ab1d848f
NH
3977/*
3978 * Issue syscall I/O to EMC Symmetrix array.
3979 * CCWs are PSF and RSSD
3980 */
3981static int dasd_symm_io(struct dasd_device *device, void __user *argp)
3982{
3983 struct dasd_symmio_parms usrparm;
3984 char *psf_data, *rssd_result;
3985 struct dasd_ccw_req *cqr;
3986 struct ccw1 *ccw;
52898025 3987 char psf0, psf1;
ab1d848f
NH
3988 int rc;
3989
52898025
NH
3990 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
3991 return -EACCES;
3992 psf0 = psf1 = 0;
3993
ab1d848f
NH
3994 /* Copy parms from caller */
3995 rc = -EFAULT;
3996 if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
3997 goto out;
92d62891 3998 if (is_compat_task()) {
f8b06859 3999 /* Make sure pointers are sane even on 31 bit. */
ab1d848f 4000 rc = -EINVAL;
f8b06859
HC
4001 if ((usrparm.psf_data >> 32) != 0)
4002 goto out;
4003 if ((usrparm.rssd_result >> 32) != 0)
4004 goto out;
4005 usrparm.psf_data &= 0x7fffffffULL;
4006 usrparm.rssd_result &= 0x7fffffffULL;
ab1d848f 4007 }
ab1d848f
NH
4008 /* alloc I/O data area */
4009 psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
4010 rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
4011 if (!psf_data || !rssd_result) {
4012 rc = -ENOMEM;
4013 goto out_free;
4014 }
4015
4016 /* get syscall header from user space */
4017 rc = -EFAULT;
4018 if (copy_from_user(psf_data,
4019 (void __user *)(unsigned long) usrparm.psf_data,
4020 usrparm.psf_data_len))
4021 goto out_free;
52898025
NH
4022 psf0 = psf_data[0];
4023 psf1 = psf_data[1];
ab1d848f
NH
4024
4025 /* setup CCWs for PSF + RSSD */
68b781fe 4026 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
ab1d848f 4027 if (IS_ERR(cqr)) {
fc19f381 4028 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
ab1d848f
NH
4029 "Could not allocate initialization request");
4030 rc = PTR_ERR(cqr);
4031 goto out_free;
4032 }
4033
4034 cqr->startdev = device;
4035 cqr->memdev = device;
4036 cqr->retries = 3;
4037 cqr->expires = 10 * HZ;
1aae0560 4038 cqr->buildclk = get_tod_clock();
ab1d848f
NH
4039 cqr->status = DASD_CQR_FILLED;
4040
4041 /* Build the ccws */
4042 ccw = cqr->cpaddr;
4043
4044 /* PSF ccw */
4045 ccw->cmd_code = DASD_ECKD_CCW_PSF;
4046 ccw->count = usrparm.psf_data_len;
4047 ccw->flags |= CCW_FLAG_CC;
4048 ccw->cda = (__u32)(addr_t) psf_data;
4049
4050 ccw++;
4051
4052 /* RSSD ccw */
4053 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
4054 ccw->count = usrparm.rssd_result_len;
4055 ccw->flags = CCW_FLAG_SLI ;
4056 ccw->cda = (__u32)(addr_t) rssd_result;
4057
4058 rc = dasd_sleep_on(cqr);
4059 if (rc)
4060 goto out_sfree;
4061
4062 rc = -EFAULT;
4063 if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
4064 rssd_result, usrparm.rssd_result_len))
4065 goto out_sfree;
4066 rc = 0;
4067
4068out_sfree:
4069 dasd_sfree_request(cqr, cqr->memdev);
4070out_free:
4071 kfree(rssd_result);
4072 kfree(psf_data);
4073out:
52898025
NH
4074 DBF_DEV_EVENT(DBF_WARNING, device,
4075 "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
4076 (int) psf0, (int) psf1, rc);
ab1d848f
NH
4077 return rc;
4078}
4079
1107ccfb 4080static int
8e09f215 4081dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
1107ccfb 4082{
8e09f215
SW
4083 struct dasd_device *device = block->base;
4084
1107ccfb
CH
4085 switch (cmd) {
4086 case BIODASDGATTR:
4087 return dasd_eckd_get_attrib(device, argp);
4088 case BIODASDSATTR:
4089 return dasd_eckd_set_attrib(device, argp);
4090 case BIODASDPSRD:
4091 return dasd_eckd_performance(device, argp);
4092 case BIODASDRLSE:
4093 return dasd_eckd_release(device);
4094 case BIODASDRSRV:
4095 return dasd_eckd_reserve(device);
4096 case BIODASDSLCK:
4097 return dasd_eckd_steal_lock(device);
196339f1
SW
4098 case BIODASDSNID:
4099 return dasd_eckd_snid(device, argp);
ab1d848f
NH
4100 case BIODASDSYMMIO:
4101 return dasd_symm_io(device, argp);
1107ccfb 4102 default:
6b79d14e 4103 return -ENOTTY;
1107ccfb
CH
4104 }
4105}
4106
445b5b49
HH
4107/*
4108 * Dump the range of CCWs into 'page' buffer
4109 * and return number of printed chars.
4110 */
4d284cac 4111static int
445b5b49
HH
4112dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
4113{
4114 int len, count;
4115 char *datap;
4116
4117 len = 0;
4118 while (from <= to) {
773bab4a 4119 len += sprintf(page + len, PRINTK_HEADER
445b5b49
HH
4120 " CCW %p: %08X %08X DAT:",
4121 from, ((int *) from)[0], ((int *) from)[1]);
4122
4123 /* get pointer to data (consider IDALs) */
4124 if (from->flags & CCW_FLAG_IDA)
4125 datap = (char *) *((addr_t *) (addr_t) from->cda);
4126 else
4127 datap = (char *) ((addr_t) from->cda);
4128
4129 /* dump data (max 32 bytes) */
4130 for (count = 0; count < from->count && count < 32; count++) {
4131 if (count % 8 == 0) len += sprintf(page + len, " ");
4132 if (count % 4 == 0) len += sprintf(page + len, " ");
4133 len += sprintf(page + len, "%02x", datap[count]);
4134 }
4135 len += sprintf(page + len, "\n");
4136 from++;
4137 }
4138 return len;
4139}
4140
fc19f381 4141static void
aeec92ca
SH
4142dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
4143 char *reason)
fc19f381
SH
4144{
4145 u64 *sense;
a5a0061f 4146 u64 *stat;
aeec92ca
SH
4147
4148 sense = (u64 *) dasd_get_sense(irb);
a5a0061f 4149 stat = (u64 *) &irb->scsw;
fc19f381 4150 if (sense) {
a5a0061f
SW
4151 DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
4152 "%016llx %016llx %016llx %016llx",
4153 reason, *stat, *((u32 *) (stat + 1)),
ed3640b2 4154 sense[0], sense[1], sense[2], sense[3]);
fc19f381 4155 } else {
a5a0061f
SW
4156 DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
4157 reason, *stat, *((u32 *) (stat + 1)),
4158 "NO VALID SENSE");
fc19f381
SH
4159 }
4160}
4161
1da177e4
LT
4162/*
4163 * Print sense data and related channel program.
4164 * Parts are printed because printk buffer is only 1024 bytes.
4165 */
f3eb5384 4166static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
8e09f215 4167 struct dasd_ccw_req *req, struct irb *irb)
1da177e4
LT
4168{
4169 char *page;
445b5b49
HH
4170 struct ccw1 *first, *last, *fail, *from, *to;
4171 int len, sl, sct;
1da177e4
LT
4172
4173 page = (char *) get_zeroed_page(GFP_ATOMIC);
4174 if (page == NULL) {
fc19f381
SH
4175 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4176 "No memory to dump sense data\n");
1da177e4
LT
4177 return;
4178 }
445b5b49 4179 /* dump the sense data */
773bab4a 4180 len = sprintf(page, PRINTK_HEADER
1da177e4 4181 " I/O status report for device %s:\n",
2a0217d5 4182 dev_name(&device->cdev->dev));
773bab4a 4183 len += sprintf(page + len, PRINTK_HEADER
a5a0061f
SW
4184 " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4185 "CS:%02X RC:%d\n",
4186 req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4187 scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4188 scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4189 req ? req->intrc : 0);
773bab4a 4190 len += sprintf(page + len, PRINTK_HEADER
1da177e4 4191 " device %s: Failing CCW: %p\n",
2a0217d5 4192 dev_name(&device->cdev->dev),
23d805b6 4193 (void *) (addr_t) irb->scsw.cmd.cpa);
1da177e4
LT
4194 if (irb->esw.esw0.erw.cons) {
4195 for (sl = 0; sl < 4; sl++) {
773bab4a 4196 len += sprintf(page + len, PRINTK_HEADER
1da177e4
LT
4197 " Sense(hex) %2d-%2d:",
4198 (8 * sl), ((8 * sl) + 7));
4199
4200 for (sct = 0; sct < 8; sct++) {
4201 len += sprintf(page + len, " %02x",
4202 irb->ecw[8 * sl + sct]);
4203 }
4204 len += sprintf(page + len, "\n");
4205 }
4206
4207 if (irb->ecw[27] & DASD_SENSE_BIT_0) {
4208 /* 24 Byte Sense Data */
773bab4a 4209 sprintf(page + len, PRINTK_HEADER
445b5b49
HH
4210 " 24 Byte: %x MSG %x, "
4211 "%s MSGb to SYSOP\n",
4212 irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
4213 irb->ecw[1] & 0x10 ? "" : "no");
1da177e4
LT
4214 } else {
4215 /* 32 Byte Sense Data */
773bab4a 4216 sprintf(page + len, PRINTK_HEADER
445b5b49
HH
4217 " 32 Byte: Format: %x "
4218 "Exception class %x\n",
4219 irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
1da177e4
LT
4220 }
4221 } else {
773bab4a 4222 sprintf(page + len, PRINTK_HEADER
445b5b49 4223 " SORRY - NO VALID SENSE AVAILABLE\n");
1da177e4 4224 }
773bab4a 4225 printk(KERN_ERR "%s", page);
445b5b49 4226
8e09f215
SW
4227 if (req) {
4228 /* req == NULL for unsolicited interrupts */
4229 /* dump the Channel Program (max 140 Bytes per line) */
4230 /* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
4231 first = req->cpaddr;
4232 for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
4233 to = min(first + 6, last);
773bab4a 4234 len = sprintf(page, PRINTK_HEADER
8e09f215
SW
4235 " Related CP in req: %p\n", req);
4236 dasd_eckd_dump_ccw_range(first, to, page + len);
773bab4a 4237 printk(KERN_ERR "%s", page);
1da177e4 4238
8e09f215
SW
4239 /* print failing CCW area (maximum 4) */
4240 /* scsw->cda is either valid or zero */
4241 len = 0;
4242 from = ++to;
23d805b6
PO
4243 fail = (struct ccw1 *)(addr_t)
4244 irb->scsw.cmd.cpa; /* failing CCW */
8e09f215
SW
4245 if (from < fail - 2) {
4246 from = fail - 2; /* there is a gap - print header */
773bab4a 4247 len += sprintf(page, PRINTK_HEADER "......\n");
8e09f215
SW
4248 }
4249 to = min(fail + 1, last);
4250 len += dasd_eckd_dump_ccw_range(from, to, page + len);
4251
4252 /* print last CCWs (maximum 2) */
4253 from = max(from, ++to);
4254 if (from < last - 1) {
4255 from = last - 1; /* there is a gap - print header */
773bab4a 4256 len += sprintf(page + len, PRINTK_HEADER "......\n");
8e09f215
SW
4257 }
4258 len += dasd_eckd_dump_ccw_range(from, last, page + len);
4259 if (len > 0)
773bab4a 4260 printk(KERN_ERR "%s", page);
1da177e4 4261 }
1da177e4
LT
4262 free_page((unsigned long) page);
4263}
4264
f3eb5384
SW
4265
4266/*
4267 * Print sense data from a tcw.
4268 */
4269static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
4270 struct dasd_ccw_req *req, struct irb *irb)
4271{
4272 char *page;
4273 int len, sl, sct, residual;
f3eb5384 4274 struct tsb *tsb;
ef19298b 4275 u8 *sense, *rcq;
f3eb5384
SW
4276
4277 page = (char *) get_zeroed_page(GFP_ATOMIC);
4278 if (page == NULL) {
fc19f381 4279 DBF_DEV_EVENT(DBF_WARNING, device, " %s",
f3eb5384
SW
4280 "No memory to dump sense data");
4281 return;
4282 }
4283 /* dump the sense data */
773bab4a 4284 len = sprintf(page, PRINTK_HEADER
f3eb5384
SW
4285 " I/O status report for device %s:\n",
4286 dev_name(&device->cdev->dev));
773bab4a 4287 len += sprintf(page + len, PRINTK_HEADER
a5a0061f
SW
4288 " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4289 "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
4290 req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4291 scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4292 scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4293 irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
4294 req ? req->intrc : 0);
773bab4a 4295 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4296 " device %s: Failing TCW: %p\n",
4297 dev_name(&device->cdev->dev),
4298 (void *) (addr_t) irb->scsw.tm.tcw);
4299
4300 tsb = NULL;
4301 sense = NULL;
a5a0061f 4302 if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
f3eb5384
SW
4303 tsb = tcw_get_tsb(
4304 (struct tcw *)(unsigned long)irb->scsw.tm.tcw);
4305
b8fde722 4306 if (tsb) {
773bab4a 4307 len += sprintf(page + len, PRINTK_HEADER
f3eb5384 4308 " tsb->length %d\n", tsb->length);
773bab4a 4309 len += sprintf(page + len, PRINTK_HEADER
f3eb5384 4310 " tsb->flags %x\n", tsb->flags);
773bab4a 4311 len += sprintf(page + len, PRINTK_HEADER
f3eb5384 4312 " tsb->dcw_offset %d\n", tsb->dcw_offset);
773bab4a 4313 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4314 " tsb->count %d\n", tsb->count);
4315 residual = tsb->count - 28;
773bab4a 4316 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4317 " residual %d\n", residual);
4318
4319 switch (tsb->flags & 0x07) {
4320 case 1: /* tsa_iostat */
773bab4a 4321 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4322 " tsb->tsa.iostat.dev_time %d\n",
4323 tsb->tsa.iostat.dev_time);
773bab4a 4324 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4325 " tsb->tsa.iostat.def_time %d\n",
4326 tsb->tsa.iostat.def_time);
773bab4a 4327 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4328 " tsb->tsa.iostat.queue_time %d\n",
4329 tsb->tsa.iostat.queue_time);
773bab4a 4330 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4331 " tsb->tsa.iostat.dev_busy_time %d\n",
4332 tsb->tsa.iostat.dev_busy_time);
773bab4a 4333 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4334 " tsb->tsa.iostat.dev_act_time %d\n",
4335 tsb->tsa.iostat.dev_act_time);
4336 sense = tsb->tsa.iostat.sense;
4337 break;
4338 case 2: /* ts_ddpc */
773bab4a 4339 len += sprintf(page + len, PRINTK_HEADER
f3eb5384 4340 " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
ef19298b 4341 for (sl = 0; sl < 2; sl++) {
773bab4a 4342 len += sprintf(page + len, PRINTK_HEADER
ef19298b
SW
4343 " tsb->tsa.ddpc.rcq %2d-%2d: ",
4344 (8 * sl), ((8 * sl) + 7));
4345 rcq = tsb->tsa.ddpc.rcq;
f3eb5384
SW
4346 for (sct = 0; sct < 8; sct++) {
4347 len += sprintf(page + len, " %02x",
ef19298b 4348 rcq[8 * sl + sct]);
f3eb5384
SW
4349 }
4350 len += sprintf(page + len, "\n");
4351 }
4352 sense = tsb->tsa.ddpc.sense;
4353 break;
4354 case 3: /* tsa_intrg */
773bab4a
SO
4355 len += sprintf(page + len, PRINTK_HEADER
4356 " tsb->tsa.intrg.: not supportet yet\n");
f3eb5384
SW
4357 break;
4358 }
4359
4360 if (sense) {
4361 for (sl = 0; sl < 4; sl++) {
773bab4a 4362 len += sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4363 " Sense(hex) %2d-%2d:",
4364 (8 * sl), ((8 * sl) + 7));
4365 for (sct = 0; sct < 8; sct++) {
4366 len += sprintf(page + len, " %02x",
4367 sense[8 * sl + sct]);
4368 }
4369 len += sprintf(page + len, "\n");
4370 }
4371
4372 if (sense[27] & DASD_SENSE_BIT_0) {
4373 /* 24 Byte Sense Data */
773bab4a 4374 sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4375 " 24 Byte: %x MSG %x, "
4376 "%s MSGb to SYSOP\n",
4377 sense[7] >> 4, sense[7] & 0x0f,
4378 sense[1] & 0x10 ? "" : "no");
4379 } else {
4380 /* 32 Byte Sense Data */
773bab4a 4381 sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4382 " 32 Byte: Format: %x "
4383 "Exception class %x\n",
4384 sense[6] & 0x0f, sense[22] >> 4);
4385 }
4386 } else {
773bab4a 4387 sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4388 " SORRY - NO VALID SENSE AVAILABLE\n");
4389 }
4390 } else {
773bab4a 4391 sprintf(page + len, PRINTK_HEADER
f3eb5384
SW
4392 " SORRY - NO TSB DATA AVAILABLE\n");
4393 }
773bab4a 4394 printk(KERN_ERR "%s", page);
f3eb5384
SW
4395 free_page((unsigned long) page);
4396}
4397
4398static void dasd_eckd_dump_sense(struct dasd_device *device,
4399 struct dasd_ccw_req *req, struct irb *irb)
4400{
a5a0061f 4401 if (scsw_is_tm(&irb->scsw))
f3eb5384
SW
4402 dasd_eckd_dump_sense_tcw(device, req, irb);
4403 else
4404 dasd_eckd_dump_sense_ccw(device, req, irb);
4405}
4406
501183f2 4407static int dasd_eckd_pm_freeze(struct dasd_device *device)
d41dd122
SH
4408{
4409 /*
4410 * the device should be disconnected from our LCU structure
4411 * on restore we will reconnect it and reread LCU specific
4412 * information like PAV support that might have changed
4413 */
4414 dasd_alias_remove_device(device);
4415 dasd_alias_disconnect_device_from_lcu(device);
4416
4417 return 0;
4418}
4419
501183f2 4420static int dasd_eckd_restore_device(struct dasd_device *device)
d41dd122 4421{
543691a4 4422 struct dasd_eckd_private *private = device->private;
6fca97a9 4423 struct dasd_eckd_characteristics temp_rdc_data;
f9f8d02f 4424 int rc;
d41dd122 4425 struct dasd_uid temp_uid;
a7602f6c 4426 unsigned long flags;
ea4da6ea 4427 unsigned long cqr_flags = 0;
d41dd122 4428
d41dd122 4429 /* Read Configuration Data */
251afd69
SH
4430 rc = dasd_eckd_read_conf(device);
4431 if (rc) {
4432 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4433 "Read configuration data failed, rc=%d", rc);
4434 goto out_err;
4435 }
d41dd122 4436
2dedf0d9
SH
4437 dasd_eckd_get_uid(device, &temp_uid);
4438 /* Generate device unique id */
4439 rc = dasd_eckd_generate_uid(device);
4440 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
d41dd122 4441 if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
a7602f6c
SH
4442 dev_err(&device->cdev->dev, "The UID of the DASD has "
4443 "changed\n");
2dedf0d9 4444 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
d41dd122
SH
4445 if (rc)
4446 goto out_err;
d41dd122
SH
4447
4448 /* register lcu with alias handling, enable PAV if this is a new lcu */
f9f8d02f
SH
4449 rc = dasd_alias_make_device_known_to_lcu(device);
4450 if (rc)
251afd69 4451 goto out_err;
ea4da6ea
SH
4452
4453 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
4454 dasd_eckd_validate_server(device, cqr_flags);
f4ac1d02
SW
4455
4456 /* RE-Read Configuration Data */
251afd69
SH
4457 rc = dasd_eckd_read_conf(device);
4458 if (rc) {
4459 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4460 "Read configuration data failed, rc=%d", rc);
4461 goto out_err2;
4462 }
d41dd122
SH
4463
4464 /* Read Feature Codes */
68d1e5f0 4465 dasd_eckd_read_features(device);
d41dd122
SH
4466
4467 /* Read Device Characteristics */
68b781fe 4468 rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
6fca97a9 4469 &temp_rdc_data, 64);
d41dd122 4470 if (rc) {
b8ed5dd5
SH
4471 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4472 "Read device characteristic failed, rc=%d", rc);
251afd69 4473 goto out_err2;
d41dd122 4474 }
a7602f6c 4475 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
6fca97a9 4476 memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
a7602f6c 4477 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
d41dd122
SH
4478
4479 /* add device to alias management */
4480 dasd_alias_add_device(device);
4481
4482 return 0;
4483
251afd69
SH
4484out_err2:
4485 dasd_alias_disconnect_device_from_lcu(device);
d41dd122 4486out_err:
e6125fba 4487 return -1;
d41dd122
SH
4488}
4489
501183f2
SH
4490static int dasd_eckd_reload_device(struct dasd_device *device)
4491{
543691a4 4492 struct dasd_eckd_private *private = device->private;
501183f2 4493 int rc, old_base;
2dedf0d9
SH
4494 char print_uid[60];
4495 struct dasd_uid uid;
4496 unsigned long flags;
501183f2 4497
2dedf0d9 4498 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
501183f2 4499 old_base = private->uid.base_unit_addr;
2dedf0d9
SH
4500 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4501
501183f2
SH
4502 /* Read Configuration Data */
4503 rc = dasd_eckd_read_conf(device);
4504 if (rc)
4505 goto out_err;
4506
2dedf0d9 4507 rc = dasd_eckd_generate_uid(device);
501183f2
SH
4508 if (rc)
4509 goto out_err;
501183f2
SH
4510 /*
4511 * update unit address configuration and
4512 * add device to alias management
4513 */
4514 dasd_alias_update_add_device(device);
4515
2dedf0d9
SH
4516 dasd_eckd_get_uid(device, &uid);
4517
4518 if (old_base != uid.base_unit_addr) {
4519 if (strlen(uid.vduit) > 0)
4520 snprintf(print_uid, sizeof(print_uid),
4521 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
4522 uid.ssid, uid.base_unit_addr, uid.vduit);
501183f2 4523 else
2dedf0d9
SH
4524 snprintf(print_uid, sizeof(print_uid),
4525 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
4526 uid.ssid, uid.base_unit_addr);
501183f2
SH
4527
4528 dev_info(&device->cdev->dev,
4529 "An Alias device was reassigned to a new base device "
2dedf0d9 4530 "with UID: %s\n", print_uid);
501183f2
SH
4531 }
4532 return 0;
4533
4534out_err:
4535 return -1;
4536}
4537
5db8440c
SH
4538static int dasd_eckd_read_message_buffer(struct dasd_device *device,
4539 struct dasd_rssd_messages *messages,
4540 __u8 lpum)
4541{
4542 struct dasd_rssd_messages *message_buf;
4543 struct dasd_psf_prssd_data *prssdp;
5db8440c
SH
4544 struct dasd_ccw_req *cqr;
4545 struct ccw1 *ccw;
4546 int rc;
4547
5db8440c
SH
4548 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
4549 (sizeof(struct dasd_psf_prssd_data) +
4550 sizeof(struct dasd_rssd_messages)),
4551 device);
4552 if (IS_ERR(cqr)) {
4553 DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
4554 "Could not allocate read message buffer request");
4555 return PTR_ERR(cqr);
4556 }
4557
4558 cqr->startdev = device;
4559 cqr->memdev = device;
4560 cqr->block = NULL;
5db8440c 4561 cqr->expires = 10 * HZ;
5db8440c 4562 set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
b179b037
SH
4563 /* dasd_sleep_on_immediatly does not do complex error
4564 * recovery so clear erp flag and set retry counter to
4565 * do basic erp */
4566 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4567 cqr->retries = 256;
5db8440c
SH
4568
4569 /* Prepare for Read Subsystem Data */
4570 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4571 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
4572 prssdp->order = PSF_ORDER_PRSSD;
4573 prssdp->suborder = 0x03; /* Message Buffer */
4574 /* all other bytes of prssdp must be zero */
4575
4576 ccw = cqr->cpaddr;
4577 ccw->cmd_code = DASD_ECKD_CCW_PSF;
4578 ccw->count = sizeof(struct dasd_psf_prssd_data);
4579 ccw->flags |= CCW_FLAG_CC;
4580 ccw->flags |= CCW_FLAG_SLI;
4581 ccw->cda = (__u32)(addr_t) prssdp;
4582
4583 /* Read Subsystem Data - message buffer */
4584 message_buf = (struct dasd_rssd_messages *) (prssdp + 1);
4585 memset(message_buf, 0, sizeof(struct dasd_rssd_messages));
4586
4587 ccw++;
4588 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
4589 ccw->count = sizeof(struct dasd_rssd_messages);
4590 ccw->flags |= CCW_FLAG_SLI;
4591 ccw->cda = (__u32)(addr_t) message_buf;
4592
4593 cqr->buildclk = get_tod_clock();
4594 cqr->status = DASD_CQR_FILLED;
4595 rc = dasd_sleep_on_immediatly(cqr);
4596 if (rc == 0) {
4597 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4598 message_buf = (struct dasd_rssd_messages *)
4599 (prssdp + 1);
4600 memcpy(messages, message_buf,
4601 sizeof(struct dasd_rssd_messages));
4602 } else
4603 DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4604 "Reading messages failed with rc=%d\n"
4605 , rc);
4606 dasd_sfree_request(cqr, cqr->memdev);
4607 return rc;
4608}
4609
4610/*
4611 * Perform Subsystem Function - CUIR response
4612 */
4613static int
4614dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
4615 __u32 message_id,
4616 struct channel_path_desc *desc,
4617 struct subchannel_id sch_id)
4618{
4619 struct dasd_psf_cuir_response *psf_cuir;
4620 struct dasd_ccw_req *cqr;
4621 struct ccw1 *ccw;
4622 int rc;
4623
4624 cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
4625 sizeof(struct dasd_psf_cuir_response),
4626 device);
4627
4628 if (IS_ERR(cqr)) {
4629 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4630 "Could not allocate PSF-CUIR request");
4631 return PTR_ERR(cqr);
4632 }
4633
4634 psf_cuir = (struct dasd_psf_cuir_response *)cqr->data;
4635 psf_cuir->order = PSF_ORDER_CUIR_RESPONSE;
4636 psf_cuir->cc = response;
4637 if (desc)
4638 psf_cuir->chpid = desc->chpid;
4639 psf_cuir->message_id = message_id;
4640 psf_cuir->cssid = sch_id.cssid;
4641 psf_cuir->ssid = sch_id.ssid;
5db8440c
SH
4642 ccw = cqr->cpaddr;
4643 ccw->cmd_code = DASD_ECKD_CCW_PSF;
4644 ccw->cda = (__u32)(addr_t)psf_cuir;
b179b037 4645 ccw->flags = CCW_FLAG_SLI;
5db8440c
SH
4646 ccw->count = sizeof(struct dasd_psf_cuir_response);
4647
4648 cqr->startdev = device;
4649 cqr->memdev = device;
4650 cqr->block = NULL;
4651 cqr->retries = 256;
4652 cqr->expires = 10*HZ;
4653 cqr->buildclk = get_tod_clock();
4654 cqr->status = DASD_CQR_FILLED;
b179b037 4655 set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5db8440c
SH
4656
4657 rc = dasd_sleep_on(cqr);
4658
4659 dasd_sfree_request(cqr, cqr->memdev);
4660 return rc;
4661}
4662
b179b037
SH
4663/*
4664 * return configuration data that is referenced by record selector
4665 * if a record selector is specified or per default return the
4666 * conf_data pointer for the path specified by lpum
4667 */
4668static struct dasd_conf_data *dasd_eckd_get_ref_conf(struct dasd_device *device,
4669 __u8 lpum,
4670 struct dasd_cuir_message *cuir)
5db8440c 4671{
543691a4 4672 struct dasd_eckd_private *private = device->private;
b179b037
SH
4673 struct dasd_conf_data *conf_data;
4674 int path, pos;
5db8440c 4675
b179b037
SH
4676 if (cuir->record_selector == 0)
4677 goto out;
4678 for (path = 0x80, pos = 0; path; path >>= 1, pos++) {
4679 conf_data = private->path_conf_data[pos];
4680 if (conf_data->gneq.record_selector ==
4681 cuir->record_selector)
4682 return conf_data;
5db8440c 4683 }
b179b037 4684out:
9d49f86d 4685 return private->path_conf_data[pathmask_to_pos(lpum)];
5db8440c
SH
4686}
4687
4688/*
b179b037
SH
4689 * This function determines the scope of a reconfiguration request by
4690 * analysing the path and device selection data provided in the CUIR request.
4691 * Returns a path mask containing CUIR affected paths for the give device.
4692 *
4693 * If the CUIR request does not contain the required information return the
4694 * path mask of the path the attention message for the CUIR request was reveived
4695 * on.
4696 */
4697static int dasd_eckd_cuir_scope(struct dasd_device *device, __u8 lpum,
4698 struct dasd_cuir_message *cuir)
4699{
543691a4 4700 struct dasd_eckd_private *private = device->private;
b179b037
SH
4701 struct dasd_conf_data *ref_conf_data;
4702 unsigned long bitmask = 0, mask = 0;
b179b037
SH
4703 struct dasd_conf_data *conf_data;
4704 unsigned int pos, path;
4705 char *ref_gneq, *gneq;
4706 char *ref_ned, *ned;
4707 int tbcpm = 0;
4708
4709 /* if CUIR request does not specify the scope use the path
4710 the attention message was presented on */
4711 if (!cuir->ned_map ||
4712 !(cuir->neq_map[0] | cuir->neq_map[1] | cuir->neq_map[2]))
4713 return lpum;
4714
b179b037
SH
4715 /* get reference conf data */
4716 ref_conf_data = dasd_eckd_get_ref_conf(device, lpum, cuir);
4717 /* reference ned is determined by ned_map field */
4718 pos = 8 - ffs(cuir->ned_map);
4719 ref_ned = (char *)&ref_conf_data->neds[pos];
4720 ref_gneq = (char *)&ref_conf_data->gneq;
4721 /* transfer 24 bit neq_map to mask */
4722 mask = cuir->neq_map[2];
4723 mask |= cuir->neq_map[1] << 8;
4724 mask |= cuir->neq_map[0] << 16;
4725
4726 for (path = 0x80; path; path >>= 1) {
4727 /* initialise data per path */
4728 bitmask = mask;
9d49f86d 4729 pos = pathmask_to_pos(path);
b179b037
SH
4730 conf_data = private->path_conf_data[pos];
4731 pos = 8 - ffs(cuir->ned_map);
4732 ned = (char *) &conf_data->neds[pos];
4733 /* compare reference ned and per path ned */
4734 if (memcmp(ref_ned, ned, sizeof(*ned)) != 0)
4735 continue;
4736 gneq = (char *)&conf_data->gneq;
4737 /* compare reference gneq and per_path gneq under
4738 24 bit mask where mask bit 0 equals byte 7 of
4739 the gneq and mask bit 24 equals byte 31 */
4740 while (bitmask) {
4741 pos = ffs(bitmask) - 1;
4742 if (memcmp(&ref_gneq[31 - pos], &gneq[31 - pos], 1)
4743 != 0)
4744 break;
4745 clear_bit(pos, &bitmask);
4746 }
4747 if (bitmask)
4748 continue;
4749 /* device and path match the reference values
4750 add path to CUIR scope */
4751 tbcpm |= path;
4752 }
4753 return tbcpm;
4754}
4755
4756static void dasd_eckd_cuir_notify_user(struct dasd_device *device,
4757 unsigned long paths,
4758 struct subchannel_id sch_id, int action)
4759{
4760 struct channel_path_desc *desc;
4761 int pos;
4762
4763 while (paths) {
4764 /* get position of bit in mask */
4765 pos = ffs(paths) - 1;
4766 /* get channel path descriptor from this position */
4767 desc = ccw_device_get_chp_desc(device->cdev, 7 - pos);
4768 if (action == CUIR_QUIESCE)
4769 pr_warn("Service on the storage server caused path "
4770 "%x.%02x to go offline", sch_id.cssid,
4771 desc ? desc->chpid : 0);
4772 else if (action == CUIR_RESUME)
4773 pr_info("Path %x.%02x is back online after service "
4774 "on the storage server", sch_id.cssid,
4775 desc ? desc->chpid : 0);
4776 kfree(desc);
4777 clear_bit(pos, &paths);
4778 }
4779}
4780
4781static int dasd_eckd_cuir_remove_path(struct dasd_device *device, __u8 lpum,
4782 struct dasd_cuir_message *cuir)
4783{
4784 unsigned long tbcpm;
4785
4786 tbcpm = dasd_eckd_cuir_scope(device, lpum, cuir);
4787 /* nothing to do if path is not in use */
4788 if (!(device->path_data.opm & tbcpm))
4789 return 0;
4790 if (!(device->path_data.opm & ~tbcpm)) {
4791 /* no path would be left if the CUIR action is taken
4792 return error */
4793 return -EINVAL;
4794 }
4795 /* remove device from operational path mask */
4796 device->path_data.opm &= ~tbcpm;
4797 device->path_data.cuirpm |= tbcpm;
4798 return tbcpm;
4799}
4800
4801/*
4802 * walk through all devices and build a path mask to quiesce them
4803 * return an error if the last path to a device would be removed
5db8440c
SH
4804 *
4805 * if only part of the devices are quiesced and an error
4806 * occurs no onlining necessary, the storage server will
4807 * notify the already set offline devices again
4808 */
4809static int dasd_eckd_cuir_quiesce(struct dasd_device *device, __u8 lpum,
b179b037
SH
4810 struct subchannel_id sch_id,
4811 struct dasd_cuir_message *cuir)
5db8440c 4812{
543691a4 4813 struct dasd_eckd_private *private = device->private;
5db8440c 4814 struct alias_pav_group *pavgroup, *tempgroup;
5db8440c 4815 struct dasd_device *dev, *n;
b179b037
SH
4816 unsigned long paths = 0;
4817 unsigned long flags;
4818 int tbcpm;
5db8440c 4819
5db8440c 4820 /* active devices */
b179b037 4821 list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
5db8440c 4822 alias_list) {
b179b037
SH
4823 spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
4824 tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
4825 spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
4826 if (tbcpm < 0)
4827 goto out_err;
4828 paths |= tbcpm;
5db8440c 4829 }
5db8440c 4830 /* inactive devices */
b179b037 4831 list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
5db8440c 4832 alias_list) {
b179b037
SH
4833 spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
4834 tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
4835 spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
4836 if (tbcpm < 0)
4837 goto out_err;
4838 paths |= tbcpm;
5db8440c 4839 }
5db8440c
SH
4840 /* devices in PAV groups */
4841 list_for_each_entry_safe(pavgroup, tempgroup,
4842 &private->lcu->grouplist, group) {
4843 list_for_each_entry_safe(dev, n, &pavgroup->baselist,
4844 alias_list) {
b179b037
SH
4845 spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
4846 tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
4847 spin_unlock_irqrestore(
4848 get_ccwdev_lock(dev->cdev), flags);
4849 if (tbcpm < 0)
4850 goto out_err;
4851 paths |= tbcpm;
5db8440c
SH
4852 }
4853 list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
4854 alias_list) {
b179b037
SH
4855 spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
4856 tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
4857 spin_unlock_irqrestore(
4858 get_ccwdev_lock(dev->cdev), flags);
4859 if (tbcpm < 0)
4860 goto out_err;
4861 paths |= tbcpm;
5db8440c
SH
4862 }
4863 }
b179b037
SH
4864 /* notify user about all paths affected by CUIR action */
4865 dasd_eckd_cuir_notify_user(device, paths, sch_id, CUIR_QUIESCE);
4866 return 0;
4867out_err:
4868 return tbcpm;
5db8440c
SH
4869}
4870
4871static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
b179b037
SH
4872 struct subchannel_id sch_id,
4873 struct dasd_cuir_message *cuir)
5db8440c 4874{
543691a4 4875 struct dasd_eckd_private *private = device->private;
5db8440c 4876 struct alias_pav_group *pavgroup, *tempgroup;
5db8440c 4877 struct dasd_device *dev, *n;
b179b037
SH
4878 unsigned long paths = 0;
4879 int tbcpm;
5db8440c 4880
5db8440c
SH
4881 /*
4882 * the path may have been added through a generic path event before
4883 * only trigger path verification if the path is not already in use
4884 */
5db8440c
SH
4885 list_for_each_entry_safe(dev, n,
4886 &private->lcu->active_devices,
4887 alias_list) {
b179b037
SH
4888 tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
4889 paths |= tbcpm;
4890 if (!(dev->path_data.opm & tbcpm)) {
4891 dev->path_data.tbvpm |= tbcpm;
5db8440c
SH
4892 dasd_schedule_device_bh(dev);
4893 }
4894 }
5db8440c
SH
4895 list_for_each_entry_safe(dev, n,
4896 &private->lcu->inactive_devices,
4897 alias_list) {
b179b037
SH
4898 tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
4899 paths |= tbcpm;
4900 if (!(dev->path_data.opm & tbcpm)) {
4901 dev->path_data.tbvpm |= tbcpm;
5db8440c
SH
4902 dasd_schedule_device_bh(dev);
4903 }
4904 }
5db8440c
SH
4905 /* devices in PAV groups */
4906 list_for_each_entry_safe(pavgroup, tempgroup,
4907 &private->lcu->grouplist,
4908 group) {
4909 list_for_each_entry_safe(dev, n,
4910 &pavgroup->baselist,
4911 alias_list) {
b179b037
SH
4912 tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
4913 paths |= tbcpm;
4914 if (!(dev->path_data.opm & tbcpm)) {
4915 dev->path_data.tbvpm |= tbcpm;
5db8440c
SH
4916 dasd_schedule_device_bh(dev);
4917 }
4918 }
4919 list_for_each_entry_safe(dev, n,
4920 &pavgroup->aliaslist,
4921 alias_list) {
b179b037
SH
4922 tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
4923 paths |= tbcpm;
4924 if (!(dev->path_data.opm & tbcpm)) {
4925 dev->path_data.tbvpm |= tbcpm;
5db8440c
SH
4926 dasd_schedule_device_bh(dev);
4927 }
4928 }
4929 }
b179b037
SH
4930 /* notify user about all paths affected by CUIR action */
4931 dasd_eckd_cuir_notify_user(device, paths, sch_id, CUIR_RESUME);
4932 return 0;
5db8440c
SH
4933}
4934
4935static void dasd_eckd_handle_cuir(struct dasd_device *device, void *messages,
4936 __u8 lpum)
4937{
4938 struct dasd_cuir_message *cuir = messages;
4939 struct channel_path_desc *desc;
4940 struct subchannel_id sch_id;
4941 int pos, response;
5db8440c 4942
b179b037
SH
4943 DBF_DEV_EVENT(DBF_WARNING, device,
4944 "CUIR request: %016llx %016llx %016llx %08x",
4945 ((u64 *)cuir)[0], ((u64 *)cuir)[1], ((u64 *)cuir)[2],
4946 ((u32 *)cuir)[3]);
4947 ccw_device_get_schid(device->cdev, &sch_id);
9d49f86d 4948 pos = pathmask_to_pos(lpum);
5db8440c
SH
4949 desc = ccw_device_get_chp_desc(device->cdev, pos);
4950
4951 if (cuir->code == CUIR_QUIESCE) {
4952 /* quiesce */
b179b037
SH
4953 if (dasd_eckd_cuir_quiesce(device, lpum, sch_id, cuir))
4954 response = PSF_CUIR_LAST_PATH;
4955 else
4956 response = PSF_CUIR_COMPLETED;
5db8440c
SH
4957 } else if (cuir->code == CUIR_RESUME) {
4958 /* resume */
b179b037
SH
4959 dasd_eckd_cuir_resume(device, lpum, sch_id, cuir);
4960 response = PSF_CUIR_COMPLETED;
5db8440c
SH
4961 } else
4962 response = PSF_CUIR_NOT_SUPPORTED;
4963
b179b037
SH
4964 dasd_eckd_psf_cuir_response(device, response,
4965 cuir->message_id, desc, sch_id);
4966 DBF_DEV_EVENT(DBF_WARNING, device,
4967 "CUIR response: %d on message ID %08x", response,
4968 cuir->message_id);
5db8440c
SH
4969 /* free descriptor copy */
4970 kfree(desc);
b179b037
SH
4971 /* to make sure there is no attention left schedule work again */
4972 device->discipline->check_attention(device, lpum);
5db8440c
SH
4973}
4974
4975static void dasd_eckd_check_attention_work(struct work_struct *work)
4976{
4977 struct check_attention_work_data *data;
4978 struct dasd_rssd_messages *messages;
4979 struct dasd_device *device;
4980 int rc;
4981
4982 data = container_of(work, struct check_attention_work_data, worker);
4983 device = data->device;
5db8440c
SH
4984 messages = kzalloc(sizeof(*messages), GFP_KERNEL);
4985 if (!messages) {
4986 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4987 "Could not allocate attention message buffer");
4988 goto out;
4989 }
5db8440c
SH
4990 rc = dasd_eckd_read_message_buffer(device, messages, data->lpum);
4991 if (rc)
4992 goto out;
5db8440c
SH
4993 if (messages->length == ATTENTION_LENGTH_CUIR &&
4994 messages->format == ATTENTION_FORMAT_CUIR)
4995 dasd_eckd_handle_cuir(device, messages, data->lpum);
5db8440c
SH
4996out:
4997 dasd_put_device(device);
4998 kfree(messages);
4999 kfree(data);
5000}
5001
5002static int dasd_eckd_check_attention(struct dasd_device *device, __u8 lpum)
5003{
5004 struct check_attention_work_data *data;
5005
5006 data = kzalloc(sizeof(*data), GFP_ATOMIC);
5007 if (!data)
5008 return -ENOMEM;
5009 INIT_WORK(&data->worker, dasd_eckd_check_attention_work);
5010 dasd_get_device(device);
5011 data->device = device;
5012 data->lpum = lpum;
5013 schedule_work(&data->worker);
5014 return 0;
5015}
5016
d41dd122 5017static struct ccw_driver dasd_eckd_driver = {
3bda058b
SO
5018 .driver = {
5019 .name = "dasd-eckd",
5020 .owner = THIS_MODULE,
5021 },
d41dd122
SH
5022 .ids = dasd_eckd_ids,
5023 .probe = dasd_eckd_probe,
5024 .remove = dasd_generic_remove,
5025 .set_offline = dasd_generic_set_offline,
5026 .set_online = dasd_eckd_set_online,
5027 .notify = dasd_generic_notify,
a4d26c6a 5028 .path_event = dasd_generic_path_event,
4679e893 5029 .shutdown = dasd_generic_shutdown,
d41dd122
SH
5030 .freeze = dasd_generic_pm_freeze,
5031 .thaw = dasd_generic_restore_device,
5032 .restore = dasd_generic_restore_device,
a23ed009 5033 .uc_handler = dasd_generic_uc_handler,
420f42ec 5034 .int_class = IRQIO_DAS,
d41dd122 5035};
f3eb5384 5036
1da177e4
LT
5037/*
5038 * max_blocks is dependent on the amount of storage that is available
5039 * in the static io buffer for each device. Currently each device has
5040 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
5041 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
5042 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
5043 * addition we have one define extent ccw + 16 bytes of data and one
5044 * locate record ccw + 16 bytes of data. That makes:
5045 * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
5046 * We want to fit two into the available memory so that we can immediately
5047 * start the next request if one finishes off. That makes 249.5 blocks
5048 * for one request. Give a little safety and the result is 240.
5049 */
5050static struct dasd_discipline dasd_eckd_discipline = {
5051 .owner = THIS_MODULE,
5052 .name = "ECKD",
5053 .ebcname = "ECKD",
ef19298b 5054 .max_blocks = 190,
1da177e4 5055 .check_device = dasd_eckd_check_characteristics,
8e09f215 5056 .uncheck_device = dasd_eckd_uncheck_device,
1da177e4 5057 .do_analysis = dasd_eckd_do_analysis,
a4d26c6a 5058 .verify_path = dasd_eckd_verify_path,
d42e1712 5059 .basic_to_ready = dasd_eckd_basic_to_ready,
8e09f215 5060 .online_to_ready = dasd_eckd_online_to_ready,
daa991bf 5061 .basic_to_known = dasd_eckd_basic_to_known,
1da177e4
LT
5062 .fill_geometry = dasd_eckd_fill_geometry,
5063 .start_IO = dasd_start_IO,
5064 .term_IO = dasd_term_IO,
8e09f215 5065 .handle_terminated_request = dasd_eckd_handle_terminated_request,
1da177e4 5066 .format_device = dasd_eckd_format_device,
1da177e4
LT
5067 .erp_action = dasd_eckd_erp_action,
5068 .erp_postaction = dasd_eckd_erp_postaction,
5a27e60d 5069 .check_for_device_change = dasd_eckd_check_for_device_change,
8e09f215
SW
5070 .build_cp = dasd_eckd_build_alias_cp,
5071 .free_cp = dasd_eckd_free_alias_cp,
1da177e4 5072 .dump_sense = dasd_eckd_dump_sense,
fc19f381 5073 .dump_sense_dbf = dasd_eckd_dump_sense_dbf,
1da177e4 5074 .fill_info = dasd_eckd_fill_info,
1107ccfb 5075 .ioctl = dasd_eckd_ioctl,
d41dd122
SH
5076 .freeze = dasd_eckd_pm_freeze,
5077 .restore = dasd_eckd_restore_device,
501183f2 5078 .reload = dasd_eckd_reload_device,
2dedf0d9 5079 .get_uid = dasd_eckd_get_uid,
f1633031 5080 .kick_validate = dasd_eckd_kick_validate_server,
5db8440c 5081 .check_attention = dasd_eckd_check_attention,
1da177e4
LT
5082};
5083
5084static int __init
5085dasd_eckd_init(void)
5086{
736e6ea0
SO
5087 int ret;
5088
1da177e4 5089 ASCEBC(dasd_eckd_discipline.ebcname, 4);
f932bcea
SW
5090 dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
5091 GFP_KERNEL | GFP_DMA);
5092 if (!dasd_reserve_req)
5093 return -ENOMEM;
a4d26c6a
SW
5094 path_verification_worker = kmalloc(sizeof(*path_verification_worker),
5095 GFP_KERNEL | GFP_DMA);
5096 if (!path_verification_worker) {
5097 kfree(dasd_reserve_req);
5098 return -ENOMEM;
5099 }
558b9ef0
SW
5100 rawpadpage = (void *)__get_free_page(GFP_KERNEL);
5101 if (!rawpadpage) {
5102 kfree(path_verification_worker);
5103 kfree(dasd_reserve_req);
5104 return -ENOMEM;
5105 }
736e6ea0
SO
5106 ret = ccw_driver_register(&dasd_eckd_driver);
5107 if (!ret)
5108 wait_for_device_probe();
a4d26c6a
SW
5109 else {
5110 kfree(path_verification_worker);
f932bcea 5111 kfree(dasd_reserve_req);
558b9ef0 5112 free_page((unsigned long)rawpadpage);
a4d26c6a 5113 }
736e6ea0 5114 return ret;
1da177e4
LT
5115}
5116
5117static void __exit
5118dasd_eckd_cleanup(void)
5119{
5120 ccw_driver_unregister(&dasd_eckd_driver);
a4d26c6a 5121 kfree(path_verification_worker);
f932bcea 5122 kfree(dasd_reserve_req);
558b9ef0 5123 free_page((unsigned long)rawpadpage);
1da177e4
LT
5124}
5125
5126module_init(dasd_eckd_init);
5127module_exit(dasd_eckd_cleanup);
This page took 1.143837 seconds and 5 git commands to generate.