Linux 4.1.16
[linux/fpc-iii.git] / drivers / s390 / block / dasd_fba.c
blobc9262e78938bdf1154531394623dd4d8a3e14b19
1 /*
2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Bugreports.to..: <Linux390@de.ibm.com>
4 * Copyright IBM Corp. 1999, 2009
5 */
7 #define KMSG_COMPONENT "dasd-fba"
9 #include <linux/stddef.h>
10 #include <linux/kernel.h>
11 #include <asm/debug.h>
13 #include <linux/slab.h>
14 #include <linux/hdreg.h> /* HDIO_GETGEO */
15 #include <linux/bio.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
19 #include <asm/idals.h>
20 #include <asm/ebcdic.h>
21 #include <asm/io.h>
22 #include <asm/ccwdev.h>
24 #include "dasd_int.h"
25 #include "dasd_fba.h"
27 #ifdef PRINTK_HEADER
28 #undef PRINTK_HEADER
29 #endif /* PRINTK_HEADER */
30 #define PRINTK_HEADER "dasd(fba):"
32 #define FBA_DEFAULT_RETRIES 32
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
39 MODULE_LICENSE("GPL");
41 static struct dasd_discipline dasd_fba_discipline;
43 struct dasd_fba_private {
44 struct dasd_fba_characteristics rdc_data;
47 static 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 { /* end of list */ },
53 MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
55 static struct ccw_driver dasd_fba_driver; /* see below */
56 static int
57 dasd_fba_probe(struct ccw_device *cdev)
59 return dasd_generic_probe(cdev, &dasd_fba_discipline);
62 static int
63 dasd_fba_set_online(struct ccw_device *cdev)
65 return dasd_generic_set_online(cdev, &dasd_fba_discipline);
68 static struct ccw_driver dasd_fba_driver = {
69 .driver = {
70 .name = "dasd-fba",
71 .owner = THIS_MODULE,
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,
86 static void
87 define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
88 int blksize, int beg, int nr)
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;
106 static void
107 locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
108 int block_nr, int block_ct)
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;
125 static int
126 dasd_fba_check_characteristics(struct dasd_device *device)
128 struct dasd_block *block;
129 struct dasd_fba_private *private;
130 struct ccw_device *cdev = device->cdev;
131 int rc;
132 int readonly;
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;
143 device->private = (void *) private;
144 } else {
145 memset(private, 0, sizeof(*private));
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);
155 device->block = block;
156 block->base = device;
158 /* Read Device Characteristics */
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;
171 device->default_expires = DASD_EXPIRES;
172 device->default_retries = FBA_DEFAULT_RETRIES;
173 device->path_data.opm = LPM_ANYPATH;
175 readonly = dasd_device_is_ro(device);
176 if (readonly)
177 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
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;
193 static int dasd_fba_do_analysis(struct dasd_block *block)
195 struct dasd_fba_private *private;
196 int sb, rc;
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;
205 block->blocks = private->rdc_data.blk_bdsa;
206 block->bp_block = private->rdc_data.blk_size;
207 block->s2b_shift = 0; /* bits to shift 512 to get a block */
208 for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
209 block->s2b_shift++;
210 return 0;
213 static int dasd_fba_fill_geometry(struct dasd_block *block,
214 struct hd_geometry *geo)
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;
224 static dasd_erp_fn_t
225 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
227 return dasd_default_erp_action;
230 static dasd_erp_fn_t
231 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
233 if (cqr->function == dasd_default_erp_action)
234 return dasd_default_erp_postaction;
236 DBF_DEV_EVENT(DBF_WARNING, cqr->startdev, "unknown ERP action %p",
237 cqr->function);
238 return NULL;
241 static void dasd_fba_check_for_device_change(struct dasd_device *device,
242 struct dasd_ccw_req *cqr,
243 struct irb *irb)
245 char mask;
247 /* first of all check for state change pending interrupt */
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);
253 static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device * memdev,
254 struct dasd_block *block,
255 struct request *req)
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;
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 /* Calculate record id of first and last block. */
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 /* Check struct bio and count the number of blocks for the request. */
283 count = 0;
284 cidaw = 0;
285 rq_for_each_segment(bv, req, iter) {
286 if (bv.bv_len & (blksize - 1))
287 /* Fba can only do full blocks. */
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;
293 /* Paranoia. */
294 if (count != last_rec - first_rec + 1)
295 return ERR_PTR(-EINVAL);
296 /* 1x define extent + 1x locate record + number of blocks */
297 cplength = 2 + count;
298 /* 1x define extent + 1x locate record */
299 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
300 cidaw * sizeof(unsigned long);
302 * Find out number of additional locate record ccws if the device
303 * can't do data chaining.
305 if (private->rdc_data.mode.bits.data_chain == 0) {
306 cplength += count - 1;
307 datasize += (count - 1)*sizeof(struct LO_fba_data);
309 /* Allocate the ccw request. */
310 cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev);
311 if (IS_ERR(cqr))
312 return cqr;
313 ccw = cqr->cpaddr;
314 /* First ccw is define extent. */
315 define_extent(ccw++, cqr->data, rq_data_dir(req),
316 block->bp_block, blk_rq_pos(req), blk_rq_sectors(req));
317 /* Build locate_record + read/write ccws. */
318 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
319 LO_data = (struct LO_fba_data *) (idaws + cidaw);
320 /* Locate record for all blocks for smart devices. */
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);
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;
336 for (off = 0; off < bv.bv_len; off += blksize) {
337 /* Locate record for stupid devices. */
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;
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;
361 ccw++;
362 dst += blksize;
363 recid++;
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; /* default 5 minutes */
373 cqr->retries = memdev->default_retries;
374 cqr->buildclk = get_tod_clock();
375 cqr->status = DASD_CQR_FILLED;
376 return cqr;
379 static int
380 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
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;
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 /* Skip over define extent & locate record. */
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 /* Skip locate record. */
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));
416 dst = NULL;
418 ccw++;
421 out:
422 status = cqr->status == DASD_CQR_DONE;
423 dasd_sfree_request(cqr, cqr->memdev);
424 return status;
427 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
429 if (cqr->retries < 0)
430 cqr->status = DASD_CQR_FAILED;
431 else
432 cqr->status = DASD_CQR_FILLED;
435 static int
436 dasd_fba_fill_info(struct dasd_device * device,
437 struct dasd_information2_t * info)
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;
450 static void
451 dasd_fba_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
452 char *reason)
454 u64 *sense;
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");
472 static void
473 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
474 struct irb *irb)
476 char *page;
477 struct ccw1 *act, *end, *last;
478 int len, sl, sct, count;
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;
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));
502 for (sct = 0; sct < 8; sct++) {
503 len += sprintf(page + len, " %02x",
504 irb->ecw[8 * sl + sct]);
506 len += sprintf(page + len, "\n");
508 } else {
509 len += sprintf(page + len, PRINTK_HEADER
510 " SORRY - NO VALID SENSE AVAILABLE\n");
512 printk(KERN_ERR "%s", page);
514 /* dump the Channel Program */
515 /* print first CCWs (maximum 8) */
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++;
532 printk(KERN_ERR "%s", page);
535 /* print failing CCW area */
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");
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++;
555 /* print last CCWs */
556 if (act < last - 2) {
557 act = last - 2;
558 len += sprintf(page + len, PRINTK_HEADER "......\n");
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++;
572 if (len > 0)
573 printk(KERN_ERR "%s", page);
574 free_page((unsigned long) page);
578 * max_blocks is dependent on the amount of storage that is available
579 * in the static io buffer for each device. Currently each device has
580 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
581 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
582 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
583 * addition we have one define extent ccw + 16 bytes of data and a
584 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
585 * That makes:
586 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
587 * We want to fit two into the available memory so that we can immediately
588 * start the next request if one finishes off. That makes 100.1 blocks
589 * for one request. Give a little safety and the result is 96.
591 static 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,
613 static int __init
614 dasd_fba_init(void)
616 int ret;
618 ASCEBC(dasd_fba_discipline.ebcname, 4);
619 ret = ccw_driver_register(&dasd_fba_driver);
620 if (!ret)
621 wait_for_device_probe();
623 return ret;
626 static void __exit
627 dasd_fba_cleanup(void)
629 ccw_driver_unregister(&dasd_fba_driver);
632 module_init(dasd_fba_init);
633 module_exit(dasd_fba_cleanup);