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
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>
23 #include <asm/todclk.h>
24 #include <asm/ccwdev.h>
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 */
57 dasd_fba_probe(struct ccw_device
*cdev
)
59 return dasd_generic_probe(cdev
, &dasd_fba_discipline
);
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
= {
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
,
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
;
89 ccw
->cda
= (__u32
) __pa(data
);
90 memset(data
, 0, sizeof (struct DE_fba_data
));
92 (data
->mask
).perm
= 0x0;
94 (data
->mask
).perm
= 0x1;
96 data
->mask
.perm
= 0x2;
97 data
->blk_size
= blksize
;
99 data
->ext_end
= nr
- 1;
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
;
109 ccw
->cda
= (__u32
) __pa(data
);
110 memset(data
, 0, sizeof (struct LO_fba_data
));
112 data
->operation
.cmd
= 0x5;
114 data
->operation
.cmd
= 0x6;
116 data
->operation
.cmd
= 0x8;
117 data
->blk_nr
= block_nr
;
118 data
->blk_ct
= block_ct
;
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
;
129 private = (struct dasd_fba_private
*) device
->private;
131 private = kzalloc(sizeof(*private), GFP_KERNEL
| GFP_DMA
);
133 dev_warn(&device
->cdev
->dev
,
134 "Allocating memory for private DASD "
138 device
->private = (void *) private;
140 memset(private, 0, sizeof(*private));
142 block
= dasd_alloc_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
;
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);
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
;
168 dev_info(&device
->cdev
->dev
,
169 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
175 ((private->rdc_data
.blk_bdsa
*
176 (private->rdc_data
.blk_size
>> 9)) >> 11),
177 private->rdc_data
.blk_size
);
181 static int dasd_fba_do_analysis(struct dasd_block
*block
)
183 struct dasd_fba_private
*private;
186 private = (struct dasd_fba_private
*) block
->base
->private;
187 rc
= dasd_check_blocksize(private->rdc_data
.blk_size
);
189 DBF_DEV_EVENT(DBF_WARNING
, block
->base
, "unknown blocksize %d",
190 private->rdc_data
.blk_size
);
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)
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)
206 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
208 geo
->sectors
= 128 >> block
->s2b_shift
;
213 dasd_fba_erp_action(struct dasd_ccw_req
* cqr
)
215 return dasd_default_erp_action
;
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",
229 static void dasd_fba_handle_unsolicited_interrupt(struct dasd_device
*device
,
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
);
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
);
249 static struct dasd_ccw_req
*dasd_fba_build_cp(struct dasd_device
* memdev
,
250 struct dasd_block
*block
,
253 struct dasd_fba_private
*private;
254 unsigned long *idaws
;
255 struct LO_fba_data
*LO_data
;
256 struct dasd_ccw_req
*cqr
;
258 struct req_iterator iter
;
261 int count
, cidaw
, cplength
, datasize
;
262 sector_t recid
, first_rec
, last_rec
;
263 unsigned int blksize
, off
;
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
;
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
;
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. */
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
;
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
);
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
);
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
);
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
++,
340 recid
- first_rec
, 1);
341 ccw
->flags
= CCW_FLAG_CC
;
344 if (recid
> first_rec
)
345 ccw
[-1].flags
|= CCW_FLAG_DC
;
347 ccw
[-1].flags
|= CCW_FLAG_CC
;
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
);
356 ccw
->cda
= (__u32
)(addr_t
) dst
;
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
;
370 cqr
->expires
= 5 * 60 * HZ
; /* 5 minutes */
372 cqr
->buildclk
= get_clock();
373 cqr
->status
= DASD_CQR_FILLED
;
378 dasd_fba_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
380 struct dasd_fba_private
*private;
382 struct req_iterator iter
;
385 unsigned int blksize
, off
;
388 if (!dasd_page_cache
)
390 private = (struct dasd_fba_private
*) cqr
->block
->base
->private;
391 blksize
= cqr
->block
->bp_block
;
393 /* Skip over define extent & locate record. */
395 if (private->rdc_data
.mode
.bits
.data_chain
!= 0)
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)
404 if (ccw
->flags
& CCW_FLAG_IDA
)
405 cda
= *((char **)((addr_t
) ccw
->cda
));
407 cda
= (char *)((addr_t
) ccw
->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
));
420 status
= cqr
->status
== DASD_CQR_DONE
;
421 dasd_sfree_request(cqr
, cqr
->memdev
);
425 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req
*cqr
)
427 cqr
->status
= DASD_CQR_FILLED
;
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;
446 dasd_fba_dump_sense_dbf(struct dasd_device
*device
, struct irb
*irb
,
451 sense
= (u64
*) dasd_get_sense(irb
);
453 DBF_DEV_EVENT(DBF_EMERG
, device
,
454 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
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],
461 DBF_DEV_EVENT(DBF_EMERG
, device
, "%s",
462 "SORRY - NO VALID SENSE AVAILABLE\n");
468 dasd_fba_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
472 struct ccw1
*act
, *end
, *last
;
473 int len
, sl
, sct
, count
;
475 page
= (char *) get_zeroed_page(GFP_ATOMIC
);
477 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
478 "No memory to dump sense data");
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");
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) */
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
);
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
)
525 len
+= sprintf(page
+ len
, "\n");
528 printk(KERN_ERR
"%s", page
);
531 /* print failing CCW area */
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
);
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
)
547 len
+= sprintf(page
+ len
, "\n");
551 /* print last CCWs */
552 if (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
)
565 len
+= sprintf(page
+ len
, "\n");
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.
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
,
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
,
613 ASCEBC(dasd_fba_discipline
.ebcname
, 4);
614 ret
= ccw_driver_register(&dasd_fba_driver
);
616 wait_for_device_probe();
622 dasd_fba_cleanup(void)
624 ccw_driver_unregister(&dasd_fba_driver
);
627 module_init(dasd_fba_init
);
628 module_exit(dasd_fba_cleanup
);