Version:  2.0.40 2.2.26 2.4.37 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6

Linux/drivers/s390/block/dasd_eckd.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us