1
2
3
4
5
6
7#define KMSG_COMPONENT "dasd-fba"
8
9#include <linux/stddef.h>
10#include <linux/kernel.h>
11#include <asm/debug.h>
12
13#include <linux/slab.h>
14#include <linux/hdreg.h>
15#include <linux/bio.h>
16#include <linux/module.h>
17#include <linux/init.h>
18
19#include <asm/idals.h>
20#include <asm/ebcdic.h>
21#include <asm/io.h>
22#include <asm/ccwdev.h>
23
24#include "dasd_int.h"
25#include "dasd_fba.h"
26
27#ifdef PRINTK_HEADER
28#undef PRINTK_HEADER
29#endif
30#define PRINTK_HEADER "dasd(fba):"
31
32#define FBA_DEFAULT_RETRIES 32
33
34#define DASD_FBA_CCW_WRITE 0x41
35#define DASD_FBA_CCW_READ 0x42
36#define DASD_FBA_CCW_LOCATE 0x43
37#define DASD_FBA_CCW_DEFINE_EXTENT 0x63
38
39MODULE_LICENSE("GPL");
40
41static struct dasd_discipline dasd_fba_discipline;
42
43struct dasd_fba_private {
44 struct dasd_fba_characteristics rdc_data;
45};
46
47static struct ccw_device_id dasd_fba_ids[] = {
48 { CCW_DEVICE_DEVTYPE (0x6310, 0, 0x9336, 0), .driver_info = 0x1},
49 { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3370, 0), .driver_info = 0x2},
50 { },
51};
52
53MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
54
55static struct ccw_driver dasd_fba_driver;
56static int
57dasd_fba_probe(struct ccw_device *cdev)
58{
59 return dasd_generic_probe(cdev, &dasd_fba_discipline);
60}
61
62static int
63dasd_fba_set_online(struct ccw_device *cdev)
64{
65 return dasd_generic_set_online(cdev, &dasd_fba_discipline);
66}
67
68static struct ccw_driver dasd_fba_driver = {
69 .driver = {
70 .name = "dasd-fba",
71 .owner = THIS_MODULE,
72 },
73 .ids = dasd_fba_ids,
74 .probe = dasd_fba_probe,
75 .remove = dasd_generic_remove,
76 .set_offline = dasd_generic_set_offline,
77 .set_online = dasd_fba_set_online,
78 .notify = dasd_generic_notify,
79 .path_event = dasd_generic_path_event,
80 .freeze = dasd_generic_pm_freeze,
81 .thaw = dasd_generic_restore_device,
82 .restore = dasd_generic_restore_device,
83 .int_class = IRQIO_DAS,
84};
85
86static void
87define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
88 int blksize, int beg, int nr)
89{
90 ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
91 ccw->flags = 0;
92 ccw->count = 16;
93 ccw->cda = (__u32) __pa(data);
94 memset(data, 0, sizeof (struct DE_fba_data));
95 if (rw == WRITE)
96 (data->mask).perm = 0x0;
97 else if (rw == READ)
98 (data->mask).perm = 0x1;
99 else
100 data->mask.perm = 0x2;
101 data->blk_size = blksize;
102 data->ext_loc = beg;
103 data->ext_end = nr - 1;
104}
105
106static void
107locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
108 int block_nr, int block_ct)
109{
110 ccw->cmd_code = DASD_FBA_CCW_LOCATE;
111 ccw->flags = 0;
112 ccw->count = 8;
113 ccw->cda = (__u32) __pa(data);
114 memset(data, 0, sizeof (struct LO_fba_data));
115 if (rw == WRITE)
116 data->operation.cmd = 0x5;
117 else if (rw == READ)
118 data->operation.cmd = 0x6;
119 else
120 data->operation.cmd = 0x8;
121 data->blk_nr = block_nr;
122 data->blk_ct = block_ct;
123}
124
125static int
126dasd_fba_check_characteristics(struct dasd_device *device)
127{
128 struct dasd_block *block;
129 struct dasd_fba_private *private;
130 struct ccw_device *cdev = device->cdev;
131 int rc;
132 int readonly;
133
134 private = (struct dasd_fba_private *) device->private;
135 if (!private) {
136 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
137 if (!private) {
138 dev_warn(&device->cdev->dev,
139 "Allocating memory for private DASD "
140 "data failed\n");
141 return -ENOMEM;
142 }
143 device->private = (void *) private;
144 } else {
145 memset(private, 0, sizeof(*private));
146 }
147 block = dasd_alloc_block();
148 if (IS_ERR(block)) {
149 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s", "could not allocate "
150 "dasd block structure");
151 device->private = NULL;
152 kfree(private);
153 return PTR_ERR(block);
154 }
155 device->block = block;
156 block->base = device;
157
158
159 rc = dasd_generic_read_dev_chars(device, DASD_FBA_MAGIC,
160 &private->rdc_data, 32);
161 if (rc) {
162 DBF_EVENT_DEVID(DBF_WARNING, cdev, "Read device "
163 "characteristics returned error %d", rc);
164 device->block = NULL;
165 dasd_free_block(block);
166 device->private = NULL;
167 kfree(private);
168 return rc;
169 }
170
171 device->default_expires = DASD_EXPIRES;
172 device->default_retries = FBA_DEFAULT_RETRIES;
173 device->path_data.opm = LPM_ANYPATH;
174
175 readonly = dasd_device_is_ro(device);
176 if (readonly)
177 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
178
179 dev_info(&device->cdev->dev,
180 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
181 "and %d B/blk%s\n",
182 cdev->id.dev_type,
183 cdev->id.dev_model,
184 cdev->id.cu_type,
185 cdev->id.cu_model,
186 ((private->rdc_data.blk_bdsa *
187 (private->rdc_data.blk_size >> 9)) >> 11),
188 private->rdc_data.blk_size,
189 readonly ? ", read-only device" : "");
190 return 0;
191}
192
193static int dasd_fba_do_analysis(struct dasd_block *block)
194{
195 struct dasd_fba_private *private;
196 int sb, rc;
197
198 private = (struct dasd_fba_private *) block->base->private;
199 rc = dasd_check_blocksize(private->rdc_data.blk_size);
200 if (rc) {
201 DBF_DEV_EVENT(DBF_WARNING, block->base, "unknown blocksize %d",
202 private->rdc_data.blk_size);
203 return rc;
204 }
205 block->blocks = private->rdc_data.blk_bdsa;
206 block->bp_block = private->rdc_data.blk_size;
207 block->s2b_shift = 0;
208 for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
209 block->s2b_shift++;
210 return 0;
211}
212
213static int dasd_fba_fill_geometry(struct dasd_block *block,
214 struct hd_geometry *geo)
215{
216 if (dasd_check_blocksize(block->bp_block) != 0)
217 return -EINVAL;
218 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
219 geo->heads = 16;
220 geo->sectors = 128 >> block->s2b_shift;
221 return 0;
222}
223
224static dasd_erp_fn_t
225dasd_fba_erp_action(struct dasd_ccw_req * cqr)
226{
227 return dasd_default_erp_action;
228}
229
230static dasd_erp_fn_t
231dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
232{
233 if (cqr->function == dasd_default_erp_action)
234 return dasd_default_erp_postaction;
235
236 DBF_DEV_EVENT(DBF_WARNING, cqr->startdev, "unknown ERP action %p",
237 cqr->function);
238 return NULL;
239}
240
241static void dasd_fba_check_for_device_change(struct dasd_device *device,
242 struct dasd_ccw_req *cqr,
243 struct irb *irb)
244{
245 char mask;
246
247
248 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
249 if ((irb->scsw.cmd.dstat & mask) == mask)
250 dasd_generic_handle_state_change(device);
251};
252
253static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device * memdev,
254 struct dasd_block *block,
255 struct request *req)
256{
257 struct dasd_fba_private *private;
258 unsigned long *idaws;
259 struct LO_fba_data *LO_data;
260 struct dasd_ccw_req *cqr;
261 struct ccw1 *ccw;
262 struct req_iterator iter;
263 struct bio_vec bv;
264 char *dst;
265 int count, cidaw, cplength, datasize;
266 sector_t recid, first_rec, last_rec;
267 unsigned int blksize, off;
268 unsigned char cmd;
269
270 private = (struct dasd_fba_private *) block->base->private;
271 if (rq_data_dir(req) == READ) {
272 cmd = DASD_FBA_CCW_READ;
273 } else if (rq_data_dir(req) == WRITE) {
274 cmd = DASD_FBA_CCW_WRITE;
275 } else
276 return ERR_PTR(-EINVAL);
277 blksize = block->bp_block;
278
279 first_rec = blk_rq_pos(req) >> block->s2b_shift;
280 last_rec =
281 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
282
283 count = 0;
284 cidaw = 0;
285 rq_for_each_segment(bv, req, iter) {
286 if (bv.bv_len & (blksize - 1))
287
288 return ERR_PTR(-EINVAL);
289 count += bv.bv_len >> (block->s2b_shift + 9);
290 if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
291 cidaw += bv.bv_len / blksize;
292 }
293
294 if (count != last_rec - first_rec + 1)
295 return ERR_PTR(-EINVAL);
296
297 cplength = 2 + count;
298
299 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
300 cidaw * sizeof(unsigned long);
301
302
303
304
305 if (private->rdc_data.mode.bits.data_chain == 0) {
306 cplength += count - 1;
307 datasize += (count - 1)*sizeof(struct LO_fba_data);
308 }
309
310 cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev);
311 if (IS_ERR(cqr))
312 return cqr;
313 ccw = cqr->cpaddr;
314
315 define_extent(ccw++, cqr->data, rq_data_dir(req),
316 block->bp_block, blk_rq_pos(req), blk_rq_sectors(req));
317
318 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
319 LO_data = (struct LO_fba_data *) (idaws + cidaw);
320
321 if (private->rdc_data.mode.bits.data_chain != 0) {
322 ccw[-1].flags |= CCW_FLAG_CC;
323 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
324 }
325 recid = first_rec;
326 rq_for_each_segment(bv, req, iter) {
327 dst = page_address(bv.bv_page) + bv.bv_offset;
328 if (dasd_page_cache) {
329 char *copy = kmem_cache_alloc(dasd_page_cache,
330 GFP_DMA | __GFP_NOWARN);
331 if (copy && rq_data_dir(req) == WRITE)
332 memcpy(copy + bv.bv_offset, dst, bv.bv_len);
333 if (copy)
334 dst = copy + bv.bv_offset;
335 }
336 for (off = 0; off < bv.bv_len; off += blksize) {
337
338 if (private->rdc_data.mode.bits.data_chain == 0) {
339 ccw[-1].flags |= CCW_FLAG_CC;
340 locate_record(ccw, LO_data++,
341 rq_data_dir(req),
342 recid - first_rec, 1);
343 ccw->flags = CCW_FLAG_CC;
344 ccw++;
345 } else {
346 if (recid > first_rec)
347 ccw[-1].flags |= CCW_FLAG_DC;
348 else
349 ccw[-1].flags |= CCW_FLAG_CC;
350 }
351 ccw->cmd_code = cmd;
352 ccw->count = block->bp_block;
353 if (idal_is_needed(dst, blksize)) {
354 ccw->cda = (__u32)(addr_t) idaws;
355 ccw->flags = CCW_FLAG_IDA;
356 idaws = idal_create_words(idaws, dst, blksize);
357 } else {
358 ccw->cda = (__u32)(addr_t) dst;
359 ccw->flags = 0;
360 }
361 ccw++;
362 dst += blksize;
363 recid++;
364 }
365 }
366 if (blk_noretry_request(req) ||
367 block->base->features & DASD_FEATURE_FAILFAST)
368 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
369 cqr->startdev = memdev;
370 cqr->memdev = memdev;
371 cqr->block = block;
372 cqr->expires = memdev->default_expires * HZ;
373 cqr->retries = memdev->default_retries;
374 cqr->buildclk = get_tod_clock();
375 cqr->status = DASD_CQR_FILLED;
376 return cqr;
377}
378
379static int
380dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
381{
382 struct dasd_fba_private *private;
383 struct ccw1 *ccw;
384 struct req_iterator iter;
385 struct bio_vec bv;
386 char *dst, *cda;
387 unsigned int blksize, off;
388 int status;
389
390 if (!dasd_page_cache)
391 goto out;
392 private = (struct dasd_fba_private *) cqr->block->base->private;
393 blksize = cqr->block->bp_block;
394 ccw = cqr->cpaddr;
395
396 ccw++;
397 if (private->rdc_data.mode.bits.data_chain != 0)
398 ccw++;
399 rq_for_each_segment(bv, req, iter) {
400 dst = page_address(bv.bv_page) + bv.bv_offset;
401 for (off = 0; off < bv.bv_len; off += blksize) {
402
403 if (private->rdc_data.mode.bits.data_chain == 0)
404 ccw++;
405 if (dst) {
406 if (ccw->flags & CCW_FLAG_IDA)
407 cda = *((char **)((addr_t) ccw->cda));
408 else
409 cda = (char *)((addr_t) ccw->cda);
410 if (dst != cda) {
411 if (rq_data_dir(req) == READ)
412 memcpy(dst, cda, bv.bv_len);
413 kmem_cache_free(dasd_page_cache,
414 (void *)((addr_t)cda & PAGE_MASK));
415 }
416 dst = NULL;
417 }
418 ccw++;
419 }
420 }
421out:
422 status = cqr->status == DASD_CQR_DONE;
423 dasd_sfree_request(cqr, cqr->memdev);
424 return status;
425}
426
427static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
428{
429 if (cqr->retries < 0)
430 cqr->status = DASD_CQR_FAILED;
431 else
432 cqr->status = DASD_CQR_FILLED;
433};
434
435static int
436dasd_fba_fill_info(struct dasd_device * device,
437 struct dasd_information2_t * info)
438{
439 info->label_block = 1;
440 info->FBA_layout = 1;
441 info->format = DASD_FORMAT_LDL;
442 info->characteristics_size = sizeof(struct dasd_fba_characteristics);
443 memcpy(info->characteristics,
444 &((struct dasd_fba_private *) device->private)->rdc_data,
445 sizeof (struct dasd_fba_characteristics));
446 info->confdata_size = 0;
447 return 0;
448}
449
450static void
451dasd_fba_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
452 char *reason)
453{
454 u64 *sense;
455
456 sense = (u64 *) dasd_get_sense(irb);
457 if (sense) {
458 DBF_DEV_EVENT(DBF_EMERG, device,
459 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
460 "%016llx", reason,
461 scsw_is_tm(&irb->scsw) ? "t" : "c",
462 scsw_cc(&irb->scsw), scsw_cstat(&irb->scsw),
463 scsw_dstat(&irb->scsw), sense[0], sense[1],
464 sense[2], sense[3]);
465 } else {
466 DBF_DEV_EVENT(DBF_EMERG, device, "%s",
467 "SORRY - NO VALID SENSE AVAILABLE\n");
468 }
469}
470
471
472static void
473dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
474 struct irb *irb)
475{
476 char *page;
477 struct ccw1 *act, *end, *last;
478 int len, sl, sct, count;
479
480 page = (char *) get_zeroed_page(GFP_ATOMIC);
481 if (page == NULL) {
482 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
483 "No memory to dump sense data");
484 return;
485 }
486 len = sprintf(page, PRINTK_HEADER
487 " I/O status report for device %s:\n",
488 dev_name(&device->cdev->dev));
489 len += sprintf(page + len, PRINTK_HEADER
490 " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
491 irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
492 len += sprintf(page + len, PRINTK_HEADER
493 " device %s: Failing CCW: %p\n",
494 dev_name(&device->cdev->dev),
495 (void *) (addr_t) irb->scsw.cmd.cpa);
496 if (irb->esw.esw0.erw.cons) {
497 for (sl = 0; sl < 4; sl++) {
498 len += sprintf(page + len, PRINTK_HEADER
499 " Sense(hex) %2d-%2d:",
500 (8 * sl), ((8 * sl) + 7));
501
502 for (sct = 0; sct < 8; sct++) {
503 len += sprintf(page + len, " %02x",
504 irb->ecw[8 * sl + sct]);
505 }
506 len += sprintf(page + len, "\n");
507 }
508 } else {
509 len += sprintf(page + len, PRINTK_HEADER
510 " SORRY - NO VALID SENSE AVAILABLE\n");
511 }
512 printk(KERN_ERR "%s", page);
513
514
515
516 act = req->cpaddr;
517 for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
518 end = min(act + 8, last);
519 len = sprintf(page, PRINTK_HEADER " Related CP in req: %p\n", req);
520 while (act <= end) {
521 len += sprintf(page + len, PRINTK_HEADER
522 " CCW %p: %08X %08X DAT:",
523 act, ((int *) act)[0], ((int *) act)[1]);
524 for (count = 0; count < 32 && count < act->count;
525 count += sizeof(int))
526 len += sprintf(page + len, " %08X",
527 ((int *) (addr_t) act->cda)
528 [(count>>2)]);
529 len += sprintf(page + len, "\n");
530 act++;
531 }
532 printk(KERN_ERR "%s", page);
533
534
535
536 len = 0;
537 if (act < ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) {
538 act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2;
539 len += sprintf(page + len, PRINTK_HEADER "......\n");
540 }
541 end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last);
542 while (act <= end) {
543 len += sprintf(page + len, PRINTK_HEADER
544 " CCW %p: %08X %08X DAT:",
545 act, ((int *) act)[0], ((int *) act)[1]);
546 for (count = 0; count < 32 && count < act->count;
547 count += sizeof(int))
548 len += sprintf(page + len, " %08X",
549 ((int *) (addr_t) act->cda)
550 [(count>>2)]);
551 len += sprintf(page + len, "\n");
552 act++;
553 }
554
555
556 if (act < last - 2) {
557 act = last - 2;
558 len += sprintf(page + len, PRINTK_HEADER "......\n");
559 }
560 while (act <= last) {
561 len += sprintf(page + len, PRINTK_HEADER
562 " CCW %p: %08X %08X DAT:",
563 act, ((int *) act)[0], ((int *) act)[1]);
564 for (count = 0; count < 32 && count < act->count;
565 count += sizeof(int))
566 len += sprintf(page + len, " %08X",
567 ((int *) (addr_t) act->cda)
568 [(count>>2)]);
569 len += sprintf(page + len, "\n");
570 act++;
571 }
572 if (len > 0)
573 printk(KERN_ERR "%s", page);
574 free_page((unsigned long) page);
575}
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591static struct dasd_discipline dasd_fba_discipline = {
592 .owner = THIS_MODULE,
593 .name = "FBA ",
594 .ebcname = "FBA ",
595 .max_blocks = 96,
596 .check_device = dasd_fba_check_characteristics,
597 .do_analysis = dasd_fba_do_analysis,
598 .verify_path = dasd_generic_verify_path,
599 .fill_geometry = dasd_fba_fill_geometry,
600 .start_IO = dasd_start_IO,
601 .term_IO = dasd_term_IO,
602 .handle_terminated_request = dasd_fba_handle_terminated_request,
603 .erp_action = dasd_fba_erp_action,
604 .erp_postaction = dasd_fba_erp_postaction,
605 .check_for_device_change = dasd_fba_check_for_device_change,
606 .build_cp = dasd_fba_build_cp,
607 .free_cp = dasd_fba_free_cp,
608 .dump_sense = dasd_fba_dump_sense,
609 .dump_sense_dbf = dasd_fba_dump_sense_dbf,
610 .fill_info = dasd_fba_fill_info,
611};
612
613static int __init
614dasd_fba_init(void)
615{
616 int ret;
617
618 ASCEBC(dasd_fba_discipline.ebcname, 4);
619 ret = ccw_driver_register(&dasd_fba_driver);
620 if (!ret)
621 wait_for_device_probe();
622
623 return ret;
624}
625
626static void __exit
627dasd_fba_cleanup(void)
628{
629 ccw_driver_unregister(&dasd_fba_driver);
630}
631
632module_init(dasd_fba_init);
633module_exit(dasd_fba_cleanup);
634