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