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