OMAP3: PM: Added DVFS OPP locking interface for VDD1 and VDD2
[linux-ginger.git] / drivers / s390 / block / dasd_fba.c
blobf245377e8e27809876196d12fd459fade197505d
1 /*
2 * File...........: linux/drivers/s390/block/dasd_fba.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Bugreports.to..: <Linux390@de.ibm.com>
5 * Copyright IBM Corp. 1999, 2009
6 */
8 #define KMSG_COMPONENT "dasd-fba"
10 #include <linux/stddef.h>
11 #include <linux/kernel.h>
12 #include <asm/debug.h>
14 #include <linux/slab.h>
15 #include <linux/hdreg.h> /* HDIO_GETGEO */
16 #include <linux/bio.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
20 #include <asm/idals.h>
21 #include <asm/ebcdic.h>
22 #include <asm/io.h>
23 #include <asm/todclk.h>
24 #include <asm/ccwdev.h>
26 #include "dasd_int.h"
27 #include "dasd_fba.h"
29 #ifdef PRINTK_HEADER
30 #undef PRINTK_HEADER
31 #endif /* PRINTK_HEADER */
32 #define PRINTK_HEADER "dasd(fba):"
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 .name = "dasd-fba",
70 .owner = THIS_MODULE,
71 .ids = dasd_fba_ids,
72 .probe = dasd_fba_probe,
73 .remove = dasd_generic_remove,
74 .set_offline = dasd_generic_set_offline,
75 .set_online = dasd_fba_set_online,
76 .notify = dasd_generic_notify,
77 .freeze = dasd_generic_pm_freeze,
78 .thaw = dasd_generic_restore_device,
79 .restore = dasd_generic_restore_device,
82 static void
83 define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
84 int blksize, int beg, int nr)
86 ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
87 ccw->flags = 0;
88 ccw->count = 16;
89 ccw->cda = (__u32) __pa(data);
90 memset(data, 0, sizeof (struct DE_fba_data));
91 if (rw == WRITE)
92 (data->mask).perm = 0x0;
93 else if (rw == READ)
94 (data->mask).perm = 0x1;
95 else
96 data->mask.perm = 0x2;
97 data->blk_size = blksize;
98 data->ext_loc = beg;
99 data->ext_end = nr - 1;
102 static void
103 locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
104 int block_nr, int block_ct)
106 ccw->cmd_code = DASD_FBA_CCW_LOCATE;
107 ccw->flags = 0;
108 ccw->count = 8;
109 ccw->cda = (__u32) __pa(data);
110 memset(data, 0, sizeof (struct LO_fba_data));
111 if (rw == WRITE)
112 data->operation.cmd = 0x5;
113 else if (rw == READ)
114 data->operation.cmd = 0x6;
115 else
116 data->operation.cmd = 0x8;
117 data->blk_nr = block_nr;
118 data->blk_ct = block_ct;
121 static int
122 dasd_fba_check_characteristics(struct dasd_device *device)
124 struct dasd_block *block;
125 struct dasd_fba_private *private;
126 struct ccw_device *cdev = device->cdev;
127 int rc;
129 private = (struct dasd_fba_private *) device->private;
130 if (!private) {
131 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
132 if (!private) {
133 dev_warn(&device->cdev->dev,
134 "Allocating memory for private DASD "
135 "data failed\n");
136 return -ENOMEM;
138 device->private = (void *) private;
139 } else {
140 memset(private, 0, sizeof(*private));
142 block = dasd_alloc_block();
143 if (IS_ERR(block)) {
144 DBF_EVENT(DBF_WARNING, "could not allocate dasd block "
145 "structure for device: %s",
146 dev_name(&device->cdev->dev));
147 device->private = NULL;
148 kfree(private);
149 return PTR_ERR(block);
151 device->block = block;
152 block->base = device;
154 /* Read Device Characteristics */
155 rc = dasd_generic_read_dev_chars(device, DASD_FBA_MAGIC,
156 &private->rdc_data, 32);
157 if (rc) {
158 DBF_EVENT(DBF_WARNING, "Read device characteristics returned "
159 "error %d for device: %s",
160 rc, dev_name(&device->cdev->dev));
161 device->block = NULL;
162 dasd_free_block(block);
163 device->private = NULL;
164 kfree(private);
165 return rc;
168 dev_info(&device->cdev->dev,
169 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
170 "and %d B/blk\n",
171 cdev->id.dev_type,
172 cdev->id.dev_model,
173 cdev->id.cu_type,
174 cdev->id.cu_model,
175 ((private->rdc_data.blk_bdsa *
176 (private->rdc_data.blk_size >> 9)) >> 11),
177 private->rdc_data.blk_size);
178 return 0;
181 static int dasd_fba_do_analysis(struct dasd_block *block)
183 struct dasd_fba_private *private;
184 int sb, rc;
186 private = (struct dasd_fba_private *) block->base->private;
187 rc = dasd_check_blocksize(private->rdc_data.blk_size);
188 if (rc) {
189 DBF_DEV_EVENT(DBF_WARNING, block->base, "unknown blocksize %d",
190 private->rdc_data.blk_size);
191 return rc;
193 block->blocks = private->rdc_data.blk_bdsa;
194 block->bp_block = private->rdc_data.blk_size;
195 block->s2b_shift = 0; /* bits to shift 512 to get a block */
196 for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
197 block->s2b_shift++;
198 return 0;
201 static int dasd_fba_fill_geometry(struct dasd_block *block,
202 struct hd_geometry *geo)
204 if (dasd_check_blocksize(block->bp_block) != 0)
205 return -EINVAL;
206 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
207 geo->heads = 16;
208 geo->sectors = 128 >> block->s2b_shift;
209 return 0;
212 static dasd_erp_fn_t
213 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
215 return dasd_default_erp_action;
218 static dasd_erp_fn_t
219 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
221 if (cqr->function == dasd_default_erp_action)
222 return dasd_default_erp_postaction;
224 DBF_DEV_EVENT(DBF_WARNING, cqr->startdev, "unknown ERP action %p",
225 cqr->function);
226 return NULL;
229 static void dasd_fba_handle_unsolicited_interrupt(struct dasd_device *device,
230 struct irb *irb)
232 char mask;
234 /* first of all check for state change pending interrupt */
235 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
236 if ((irb->scsw.cmd.dstat & mask) == mask) {
237 dasd_generic_handle_state_change(device);
238 return;
241 /* check for unsolicited interrupts */
242 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
243 "unsolicited interrupt received");
244 device->discipline->dump_sense_dbf(device, irb, "unsolicited");
245 dasd_schedule_device_bh(device);
246 return;
249 static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device * memdev,
250 struct dasd_block *block,
251 struct request *req)
253 struct dasd_fba_private *private;
254 unsigned long *idaws;
255 struct LO_fba_data *LO_data;
256 struct dasd_ccw_req *cqr;
257 struct ccw1 *ccw;
258 struct req_iterator iter;
259 struct bio_vec *bv;
260 char *dst;
261 int count, cidaw, cplength, datasize;
262 sector_t recid, first_rec, last_rec;
263 unsigned int blksize, off;
264 unsigned char cmd;
266 private = (struct dasd_fba_private *) block->base->private;
267 if (rq_data_dir(req) == READ) {
268 cmd = DASD_FBA_CCW_READ;
269 } else if (rq_data_dir(req) == WRITE) {
270 cmd = DASD_FBA_CCW_WRITE;
271 } else
272 return ERR_PTR(-EINVAL);
273 blksize = block->bp_block;
274 /* Calculate record id of first and last block. */
275 first_rec = blk_rq_pos(req) >> block->s2b_shift;
276 last_rec =
277 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
278 /* Check struct bio and count the number of blocks for the request. */
279 count = 0;
280 cidaw = 0;
281 rq_for_each_segment(bv, req, iter) {
282 if (bv->bv_len & (blksize - 1))
283 /* Fba can only do full blocks. */
284 return ERR_PTR(-EINVAL);
285 count += bv->bv_len >> (block->s2b_shift + 9);
286 #if defined(CONFIG_64BIT)
287 if (idal_is_needed (page_address(bv->bv_page), bv->bv_len))
288 cidaw += bv->bv_len / blksize;
289 #endif
291 /* Paranoia. */
292 if (count != last_rec - first_rec + 1)
293 return ERR_PTR(-EINVAL);
294 /* 1x define extent + 1x locate record + number of blocks */
295 cplength = 2 + count;
296 /* 1x define extent + 1x locate record */
297 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
298 cidaw * sizeof(unsigned long);
300 * Find out number of additional locate record ccws if the device
301 * can't do data chaining.
303 if (private->rdc_data.mode.bits.data_chain == 0) {
304 cplength += count - 1;
305 datasize += (count - 1)*sizeof(struct LO_fba_data);
307 /* Allocate the ccw request. */
308 cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev);
309 if (IS_ERR(cqr))
310 return cqr;
311 ccw = cqr->cpaddr;
312 /* First ccw is define extent. */
313 define_extent(ccw++, cqr->data, rq_data_dir(req),
314 block->bp_block, blk_rq_pos(req), blk_rq_sectors(req));
315 /* Build locate_record + read/write ccws. */
316 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
317 LO_data = (struct LO_fba_data *) (idaws + cidaw);
318 /* Locate record for all blocks for smart devices. */
319 if (private->rdc_data.mode.bits.data_chain != 0) {
320 ccw[-1].flags |= CCW_FLAG_CC;
321 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
323 recid = first_rec;
324 rq_for_each_segment(bv, req, iter) {
325 dst = page_address(bv->bv_page) + bv->bv_offset;
326 if (dasd_page_cache) {
327 char *copy = kmem_cache_alloc(dasd_page_cache,
328 GFP_DMA | __GFP_NOWARN);
329 if (copy && rq_data_dir(req) == WRITE)
330 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
331 if (copy)
332 dst = copy + bv->bv_offset;
334 for (off = 0; off < bv->bv_len; off += blksize) {
335 /* Locate record for stupid devices. */
336 if (private->rdc_data.mode.bits.data_chain == 0) {
337 ccw[-1].flags |= CCW_FLAG_CC;
338 locate_record(ccw, LO_data++,
339 rq_data_dir(req),
340 recid - first_rec, 1);
341 ccw->flags = CCW_FLAG_CC;
342 ccw++;
343 } else {
344 if (recid > first_rec)
345 ccw[-1].flags |= CCW_FLAG_DC;
346 else
347 ccw[-1].flags |= CCW_FLAG_CC;
349 ccw->cmd_code = cmd;
350 ccw->count = block->bp_block;
351 if (idal_is_needed(dst, blksize)) {
352 ccw->cda = (__u32)(addr_t) idaws;
353 ccw->flags = CCW_FLAG_IDA;
354 idaws = idal_create_words(idaws, dst, blksize);
355 } else {
356 ccw->cda = (__u32)(addr_t) dst;
357 ccw->flags = 0;
359 ccw++;
360 dst += blksize;
361 recid++;
364 if (blk_noretry_request(req) ||
365 block->base->features & DASD_FEATURE_FAILFAST)
366 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
367 cqr->startdev = memdev;
368 cqr->memdev = memdev;
369 cqr->block = block;
370 cqr->expires = 5 * 60 * HZ; /* 5 minutes */
371 cqr->retries = 32;
372 cqr->buildclk = get_clock();
373 cqr->status = DASD_CQR_FILLED;
374 return cqr;
377 static int
378 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
380 struct dasd_fba_private *private;
381 struct ccw1 *ccw;
382 struct req_iterator iter;
383 struct bio_vec *bv;
384 char *dst, *cda;
385 unsigned int blksize, off;
386 int status;
388 if (!dasd_page_cache)
389 goto out;
390 private = (struct dasd_fba_private *) cqr->block->base->private;
391 blksize = cqr->block->bp_block;
392 ccw = cqr->cpaddr;
393 /* Skip over define extent & locate record. */
394 ccw++;
395 if (private->rdc_data.mode.bits.data_chain != 0)
396 ccw++;
397 rq_for_each_segment(bv, req, iter) {
398 dst = page_address(bv->bv_page) + bv->bv_offset;
399 for (off = 0; off < bv->bv_len; off += blksize) {
400 /* Skip locate record. */
401 if (private->rdc_data.mode.bits.data_chain == 0)
402 ccw++;
403 if (dst) {
404 if (ccw->flags & CCW_FLAG_IDA)
405 cda = *((char **)((addr_t) ccw->cda));
406 else
407 cda = (char *)((addr_t) ccw->cda);
408 if (dst != cda) {
409 if (rq_data_dir(req) == READ)
410 memcpy(dst, cda, bv->bv_len);
411 kmem_cache_free(dasd_page_cache,
412 (void *)((addr_t)cda & PAGE_MASK));
414 dst = NULL;
416 ccw++;
419 out:
420 status = cqr->status == DASD_CQR_DONE;
421 dasd_sfree_request(cqr, cqr->memdev);
422 return status;
425 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
427 cqr->status = DASD_CQR_FILLED;
430 static int
431 dasd_fba_fill_info(struct dasd_device * device,
432 struct dasd_information2_t * info)
434 info->label_block = 1;
435 info->FBA_layout = 1;
436 info->format = DASD_FORMAT_LDL;
437 info->characteristics_size = sizeof(struct dasd_fba_characteristics);
438 memcpy(info->characteristics,
439 &((struct dasd_fba_private *) device->private)->rdc_data,
440 sizeof (struct dasd_fba_characteristics));
441 info->confdata_size = 0;
442 return 0;
445 static void
446 dasd_fba_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
447 char *reason)
449 u64 *sense;
451 sense = (u64 *) dasd_get_sense(irb);
452 if (sense) {
453 DBF_DEV_EVENT(DBF_EMERG, device,
454 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
455 "%016llx", reason,
456 scsw_is_tm(&irb->scsw) ? "t" : "c",
457 scsw_cc(&irb->scsw), scsw_cstat(&irb->scsw),
458 scsw_dstat(&irb->scsw), sense[0], sense[1],
459 sense[2], sense[3]);
460 } else {
461 DBF_DEV_EVENT(DBF_EMERG, device, "%s",
462 "SORRY - NO VALID SENSE AVAILABLE\n");
467 static void
468 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
469 struct irb *irb)
471 char *page;
472 struct ccw1 *act, *end, *last;
473 int len, sl, sct, count;
475 page = (char *) get_zeroed_page(GFP_ATOMIC);
476 if (page == NULL) {
477 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
478 "No memory to dump sense data");
479 return;
481 len = sprintf(page, KERN_ERR PRINTK_HEADER
482 " I/O status report for device %s:\n",
483 dev_name(&device->cdev->dev));
484 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
485 " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
486 irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
487 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
488 " device %s: Failing CCW: %p\n",
489 dev_name(&device->cdev->dev),
490 (void *) (addr_t) irb->scsw.cmd.cpa);
491 if (irb->esw.esw0.erw.cons) {
492 for (sl = 0; sl < 4; sl++) {
493 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
494 " Sense(hex) %2d-%2d:",
495 (8 * sl), ((8 * sl) + 7));
497 for (sct = 0; sct < 8; sct++) {
498 len += sprintf(page + len, " %02x",
499 irb->ecw[8 * sl + sct]);
501 len += sprintf(page + len, "\n");
503 } else {
504 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
505 " SORRY - NO VALID SENSE AVAILABLE\n");
507 printk(KERN_ERR "%s", page);
509 /* dump the Channel Program */
510 /* print first CCWs (maximum 8) */
511 act = req->cpaddr;
512 for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
513 end = min(act + 8, last);
514 len = sprintf(page, KERN_ERR PRINTK_HEADER
515 " Related CP in req: %p\n", req);
516 while (act <= end) {
517 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
518 " CCW %p: %08X %08X DAT:",
519 act, ((int *) act)[0], ((int *) act)[1]);
520 for (count = 0; count < 32 && count < act->count;
521 count += sizeof(int))
522 len += sprintf(page + len, " %08X",
523 ((int *) (addr_t) act->cda)
524 [(count>>2)]);
525 len += sprintf(page + len, "\n");
526 act++;
528 printk(KERN_ERR "%s", page);
531 /* print failing CCW area */
532 len = 0;
533 if (act < ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) {
534 act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2;
535 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
537 end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last);
538 while (act <= end) {
539 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
540 " CCW %p: %08X %08X DAT:",
541 act, ((int *) act)[0], ((int *) act)[1]);
542 for (count = 0; count < 32 && count < act->count;
543 count += sizeof(int))
544 len += sprintf(page + len, " %08X",
545 ((int *) (addr_t) act->cda)
546 [(count>>2)]);
547 len += sprintf(page + len, "\n");
548 act++;
551 /* print last CCWs */
552 if (act < last - 2) {
553 act = last - 2;
554 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
556 while (act <= last) {
557 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
558 " CCW %p: %08X %08X DAT:",
559 act, ((int *) act)[0], ((int *) act)[1]);
560 for (count = 0; count < 32 && count < act->count;
561 count += sizeof(int))
562 len += sprintf(page + len, " %08X",
563 ((int *) (addr_t) act->cda)
564 [(count>>2)]);
565 len += sprintf(page + len, "\n");
566 act++;
568 if (len > 0)
569 printk(KERN_ERR "%s", page);
570 free_page((unsigned long) page);
574 * max_blocks is dependent on the amount of storage that is available
575 * in the static io buffer for each device. Currently each device has
576 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
577 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
578 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
579 * addition we have one define extent ccw + 16 bytes of data and a
580 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
581 * That makes:
582 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
583 * We want to fit two into the available memory so that we can immediately
584 * start the next request if one finishes off. That makes 100.1 blocks
585 * for one request. Give a little safety and the result is 96.
587 static struct dasd_discipline dasd_fba_discipline = {
588 .owner = THIS_MODULE,
589 .name = "FBA ",
590 .ebcname = "FBA ",
591 .max_blocks = 96,
592 .check_device = dasd_fba_check_characteristics,
593 .do_analysis = dasd_fba_do_analysis,
594 .fill_geometry = dasd_fba_fill_geometry,
595 .start_IO = dasd_start_IO,
596 .term_IO = dasd_term_IO,
597 .handle_terminated_request = dasd_fba_handle_terminated_request,
598 .erp_action = dasd_fba_erp_action,
599 .erp_postaction = dasd_fba_erp_postaction,
600 .handle_unsolicited_interrupt = dasd_fba_handle_unsolicited_interrupt,
601 .build_cp = dasd_fba_build_cp,
602 .free_cp = dasd_fba_free_cp,
603 .dump_sense = dasd_fba_dump_sense,
604 .dump_sense_dbf = dasd_fba_dump_sense_dbf,
605 .fill_info = dasd_fba_fill_info,
608 static int __init
609 dasd_fba_init(void)
611 int ret;
613 ASCEBC(dasd_fba_discipline.ebcname, 4);
614 ret = ccw_driver_register(&dasd_fba_driver);
615 if (!ret)
616 wait_for_device_probe();
618 return ret;
621 static void __exit
622 dasd_fba_cleanup(void)
624 ccw_driver_unregister(&dasd_fba_driver);
627 module_init(dasd_fba_init);
628 module_exit(dasd_fba_cleanup);