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/ccwdev.h>
30 #endif /* PRINTK_HEADER */
31 #define PRINTK_HEADER "dasd(fba):"
33 #define DASD_FBA_CCW_WRITE 0x41
34 #define DASD_FBA_CCW_READ 0x42
35 #define DASD_FBA_CCW_LOCATE 0x43
36 #define DASD_FBA_CCW_DEFINE_EXTENT 0x63
38 MODULE_LICENSE("GPL");
40 static struct dasd_discipline dasd_fba_discipline
;
42 struct dasd_fba_private
{
43 struct dasd_fba_characteristics rdc_data
;
46 static struct ccw_device_id dasd_fba_ids
[] = {
47 { CCW_DEVICE_DEVTYPE (0x6310, 0, 0x9336, 0), .driver_info
= 0x1},
48 { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3370, 0), .driver_info
= 0x2},
49 { /* end of list */ },
52 MODULE_DEVICE_TABLE(ccw
, dasd_fba_ids
);
54 static struct ccw_driver dasd_fba_driver
; /* see below */
56 dasd_fba_probe(struct ccw_device
*cdev
)
58 return dasd_generic_probe(cdev
, &dasd_fba_discipline
);
62 dasd_fba_set_online(struct ccw_device
*cdev
)
64 return dasd_generic_set_online(cdev
, &dasd_fba_discipline
);
67 static struct ccw_driver dasd_fba_driver
= {
73 .probe
= dasd_fba_probe
,
74 .remove
= dasd_generic_remove
,
75 .set_offline
= dasd_generic_set_offline
,
76 .set_online
= dasd_fba_set_online
,
77 .notify
= dasd_generic_notify
,
78 .path_event
= dasd_generic_path_event
,
79 .freeze
= dasd_generic_pm_freeze
,
80 .thaw
= dasd_generic_restore_device
,
81 .restore
= dasd_generic_restore_device
,
85 define_extent(struct ccw1
* ccw
, struct DE_fba_data
*data
, int rw
,
86 int blksize
, int beg
, int nr
)
88 ccw
->cmd_code
= DASD_FBA_CCW_DEFINE_EXTENT
;
91 ccw
->cda
= (__u32
) __pa(data
);
92 memset(data
, 0, sizeof (struct DE_fba_data
));
94 (data
->mask
).perm
= 0x0;
96 (data
->mask
).perm
= 0x1;
98 data
->mask
.perm
= 0x2;
99 data
->blk_size
= blksize
;
101 data
->ext_end
= nr
- 1;
105 locate_record(struct ccw1
* ccw
, struct LO_fba_data
*data
, int rw
,
106 int block_nr
, int block_ct
)
108 ccw
->cmd_code
= DASD_FBA_CCW_LOCATE
;
111 ccw
->cda
= (__u32
) __pa(data
);
112 memset(data
, 0, sizeof (struct LO_fba_data
));
114 data
->operation
.cmd
= 0x5;
116 data
->operation
.cmd
= 0x6;
118 data
->operation
.cmd
= 0x8;
119 data
->blk_nr
= block_nr
;
120 data
->blk_ct
= block_ct
;
124 dasd_fba_check_characteristics(struct dasd_device
*device
)
126 struct dasd_block
*block
;
127 struct dasd_fba_private
*private;
128 struct ccw_device
*cdev
= device
->cdev
;
132 private = (struct dasd_fba_private
*) device
->private;
134 private = kzalloc(sizeof(*private), GFP_KERNEL
| GFP_DMA
);
136 dev_warn(&device
->cdev
->dev
,
137 "Allocating memory for private DASD "
141 device
->private = (void *) private;
143 memset(private, 0, sizeof(*private));
145 block
= dasd_alloc_block();
147 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "%s", "could not allocate "
148 "dasd block structure");
149 device
->private = NULL
;
151 return PTR_ERR(block
);
153 device
->block
= block
;
154 block
->base
= device
;
156 /* Read Device Characteristics */
157 rc
= dasd_generic_read_dev_chars(device
, DASD_FBA_MAGIC
,
158 &private->rdc_data
, 32);
160 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "Read device "
161 "characteristics returned error %d", rc
);
162 device
->block
= NULL
;
163 dasd_free_block(block
);
164 device
->private = NULL
;
169 device
->default_expires
= DASD_EXPIRES
;
170 device
->path_data
.opm
= LPM_ANYPATH
;
172 readonly
= dasd_device_is_ro(device
);
174 set_bit(DASD_FLAG_DEVICE_RO
, &device
->flags
);
176 dev_info(&device
->cdev
->dev
,
177 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
183 ((private->rdc_data
.blk_bdsa
*
184 (private->rdc_data
.blk_size
>> 9)) >> 11),
185 private->rdc_data
.blk_size
,
186 readonly
? ", read-only device" : "");
190 static int dasd_fba_do_analysis(struct dasd_block
*block
)
192 struct dasd_fba_private
*private;
195 private = (struct dasd_fba_private
*) block
->base
->private;
196 rc
= dasd_check_blocksize(private->rdc_data
.blk_size
);
198 DBF_DEV_EVENT(DBF_WARNING
, block
->base
, "unknown blocksize %d",
199 private->rdc_data
.blk_size
);
202 block
->blocks
= private->rdc_data
.blk_bdsa
;
203 block
->bp_block
= private->rdc_data
.blk_size
;
204 block
->s2b_shift
= 0; /* bits to shift 512 to get a block */
205 for (sb
= 512; sb
< private->rdc_data
.blk_size
; sb
= sb
<< 1)
210 static int dasd_fba_fill_geometry(struct dasd_block
*block
,
211 struct hd_geometry
*geo
)
213 if (dasd_check_blocksize(block
->bp_block
) != 0)
215 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
217 geo
->sectors
= 128 >> block
->s2b_shift
;
222 dasd_fba_erp_action(struct dasd_ccw_req
* cqr
)
224 return dasd_default_erp_action
;
228 dasd_fba_erp_postaction(struct dasd_ccw_req
* cqr
)
230 if (cqr
->function
== dasd_default_erp_action
)
231 return dasd_default_erp_postaction
;
233 DBF_DEV_EVENT(DBF_WARNING
, cqr
->startdev
, "unknown ERP action %p",
238 static void dasd_fba_check_for_device_change(struct dasd_device
*device
,
239 struct dasd_ccw_req
*cqr
,
244 /* first of all check for state change pending interrupt */
245 mask
= DEV_STAT_ATTENTION
| DEV_STAT_DEV_END
| DEV_STAT_UNIT_EXCEP
;
246 if ((irb
->scsw
.cmd
.dstat
& mask
) == mask
)
247 dasd_generic_handle_state_change(device
);
250 static struct dasd_ccw_req
*dasd_fba_build_cp(struct dasd_device
* memdev
,
251 struct dasd_block
*block
,
254 struct dasd_fba_private
*private;
255 unsigned long *idaws
;
256 struct LO_fba_data
*LO_data
;
257 struct dasd_ccw_req
*cqr
;
259 struct req_iterator iter
;
262 int count
, cidaw
, cplength
, datasize
;
263 sector_t recid
, first_rec
, last_rec
;
264 unsigned int blksize
, off
;
267 private = (struct dasd_fba_private
*) block
->base
->private;
268 if (rq_data_dir(req
) == READ
) {
269 cmd
= DASD_FBA_CCW_READ
;
270 } else if (rq_data_dir(req
) == WRITE
) {
271 cmd
= DASD_FBA_CCW_WRITE
;
273 return ERR_PTR(-EINVAL
);
274 blksize
= block
->bp_block
;
275 /* Calculate record id of first and last block. */
276 first_rec
= blk_rq_pos(req
) >> block
->s2b_shift
;
278 (blk_rq_pos(req
) + blk_rq_sectors(req
) - 1) >> block
->s2b_shift
;
279 /* Check struct bio and count the number of blocks for the request. */
282 rq_for_each_segment(bv
, req
, iter
) {
283 if (bv
->bv_len
& (blksize
- 1))
284 /* Fba can only do full blocks. */
285 return ERR_PTR(-EINVAL
);
286 count
+= bv
->bv_len
>> (block
->s2b_shift
+ 9);
287 #if defined(CONFIG_64BIT)
288 if (idal_is_needed (page_address(bv
->bv_page
), bv
->bv_len
))
289 cidaw
+= bv
->bv_len
/ blksize
;
293 if (count
!= last_rec
- first_rec
+ 1)
294 return ERR_PTR(-EINVAL
);
295 /* 1x define extent + 1x locate record + number of blocks */
296 cplength
= 2 + count
;
297 /* 1x define extent + 1x locate record */
298 datasize
= sizeof(struct DE_fba_data
) + sizeof(struct LO_fba_data
) +
299 cidaw
* sizeof(unsigned long);
301 * Find out number of additional locate record ccws if the device
302 * can't do data chaining.
304 if (private->rdc_data
.mode
.bits
.data_chain
== 0) {
305 cplength
+= count
- 1;
306 datasize
+= (count
- 1)*sizeof(struct LO_fba_data
);
308 /* Allocate the ccw request. */
309 cqr
= dasd_smalloc_request(DASD_FBA_MAGIC
, cplength
, datasize
, memdev
);
313 /* First ccw is define extent. */
314 define_extent(ccw
++, cqr
->data
, rq_data_dir(req
),
315 block
->bp_block
, blk_rq_pos(req
), blk_rq_sectors(req
));
316 /* Build locate_record + read/write ccws. */
317 idaws
= (unsigned long *) (cqr
->data
+ sizeof(struct DE_fba_data
));
318 LO_data
= (struct LO_fba_data
*) (idaws
+ cidaw
);
319 /* Locate record for all blocks for smart devices. */
320 if (private->rdc_data
.mode
.bits
.data_chain
!= 0) {
321 ccw
[-1].flags
|= CCW_FLAG_CC
;
322 locate_record(ccw
++, LO_data
++, rq_data_dir(req
), 0, count
);
325 rq_for_each_segment(bv
, req
, iter
) {
326 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
327 if (dasd_page_cache
) {
328 char *copy
= kmem_cache_alloc(dasd_page_cache
,
329 GFP_DMA
| __GFP_NOWARN
);
330 if (copy
&& rq_data_dir(req
) == WRITE
)
331 memcpy(copy
+ bv
->bv_offset
, dst
, bv
->bv_len
);
333 dst
= copy
+ bv
->bv_offset
;
335 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
336 /* Locate record for stupid devices. */
337 if (private->rdc_data
.mode
.bits
.data_chain
== 0) {
338 ccw
[-1].flags
|= CCW_FLAG_CC
;
339 locate_record(ccw
, LO_data
++,
341 recid
- first_rec
, 1);
342 ccw
->flags
= CCW_FLAG_CC
;
345 if (recid
> first_rec
)
346 ccw
[-1].flags
|= CCW_FLAG_DC
;
348 ccw
[-1].flags
|= CCW_FLAG_CC
;
351 ccw
->count
= block
->bp_block
;
352 if (idal_is_needed(dst
, blksize
)) {
353 ccw
->cda
= (__u32
)(addr_t
) idaws
;
354 ccw
->flags
= CCW_FLAG_IDA
;
355 idaws
= idal_create_words(idaws
, dst
, blksize
);
357 ccw
->cda
= (__u32
)(addr_t
) dst
;
365 if (blk_noretry_request(req
) ||
366 block
->base
->features
& DASD_FEATURE_FAILFAST
)
367 set_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
);
368 cqr
->startdev
= memdev
;
369 cqr
->memdev
= memdev
;
371 cqr
->expires
= memdev
->default_expires
* HZ
; /* default 5 minutes */
373 cqr
->buildclk
= get_clock();
374 cqr
->status
= DASD_CQR_FILLED
;
379 dasd_fba_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
381 struct dasd_fba_private
*private;
383 struct req_iterator iter
;
386 unsigned int blksize
, off
;
389 if (!dasd_page_cache
)
391 private = (struct dasd_fba_private
*) cqr
->block
->base
->private;
392 blksize
= cqr
->block
->bp_block
;
394 /* Skip over define extent & locate record. */
396 if (private->rdc_data
.mode
.bits
.data_chain
!= 0)
398 rq_for_each_segment(bv
, req
, iter
) {
399 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
400 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
401 /* Skip locate record. */
402 if (private->rdc_data
.mode
.bits
.data_chain
== 0)
405 if (ccw
->flags
& CCW_FLAG_IDA
)
406 cda
= *((char **)((addr_t
) ccw
->cda
));
408 cda
= (char *)((addr_t
) ccw
->cda
);
410 if (rq_data_dir(req
) == READ
)
411 memcpy(dst
, cda
, bv
->bv_len
);
412 kmem_cache_free(dasd_page_cache
,
413 (void *)((addr_t
)cda
& PAGE_MASK
));
421 status
= cqr
->status
== DASD_CQR_DONE
;
422 dasd_sfree_request(cqr
, cqr
->memdev
);
426 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req
*cqr
)
428 cqr
->status
= DASD_CQR_FILLED
;
432 dasd_fba_fill_info(struct dasd_device
* device
,
433 struct dasd_information2_t
* info
)
435 info
->label_block
= 1;
436 info
->FBA_layout
= 1;
437 info
->format
= DASD_FORMAT_LDL
;
438 info
->characteristics_size
= sizeof(struct dasd_fba_characteristics
);
439 memcpy(info
->characteristics
,
440 &((struct dasd_fba_private
*) device
->private)->rdc_data
,
441 sizeof (struct dasd_fba_characteristics
));
442 info
->confdata_size
= 0;
447 dasd_fba_dump_sense_dbf(struct dasd_device
*device
, struct irb
*irb
,
452 sense
= (u64
*) dasd_get_sense(irb
);
454 DBF_DEV_EVENT(DBF_EMERG
, device
,
455 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
457 scsw_is_tm(&irb
->scsw
) ? "t" : "c",
458 scsw_cc(&irb
->scsw
), scsw_cstat(&irb
->scsw
),
459 scsw_dstat(&irb
->scsw
), sense
[0], sense
[1],
462 DBF_DEV_EVENT(DBF_EMERG
, device
, "%s",
463 "SORRY - NO VALID SENSE AVAILABLE\n");
469 dasd_fba_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
473 struct ccw1
*act
, *end
, *last
;
474 int len
, sl
, sct
, count
;
476 page
= (char *) get_zeroed_page(GFP_ATOMIC
);
478 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
479 "No memory to dump sense data");
482 len
= sprintf(page
, KERN_ERR PRINTK_HEADER
483 " I/O status report for device %s:\n",
484 dev_name(&device
->cdev
->dev
));
485 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
486 " in req: %p CS: 0x%02X DS: 0x%02X\n", req
,
487 irb
->scsw
.cmd
.cstat
, irb
->scsw
.cmd
.dstat
);
488 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
489 " device %s: Failing CCW: %p\n",
490 dev_name(&device
->cdev
->dev
),
491 (void *) (addr_t
) irb
->scsw
.cmd
.cpa
);
492 if (irb
->esw
.esw0
.erw
.cons
) {
493 for (sl
= 0; sl
< 4; sl
++) {
494 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
495 " Sense(hex) %2d-%2d:",
496 (8 * sl
), ((8 * sl
) + 7));
498 for (sct
= 0; sct
< 8; sct
++) {
499 len
+= sprintf(page
+ len
, " %02x",
500 irb
->ecw
[8 * sl
+ sct
]);
502 len
+= sprintf(page
+ len
, "\n");
505 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
506 " SORRY - NO VALID SENSE AVAILABLE\n");
508 printk(KERN_ERR
"%s", page
);
510 /* dump the Channel Program */
511 /* print first CCWs (maximum 8) */
513 for (last
= act
; last
->flags
& (CCW_FLAG_CC
| CCW_FLAG_DC
); last
++);
514 end
= min(act
+ 8, last
);
515 len
= sprintf(page
, KERN_ERR PRINTK_HEADER
516 " Related CP in req: %p\n", req
);
518 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
519 " CCW %p: %08X %08X DAT:",
520 act
, ((int *) act
)[0], ((int *) act
)[1]);
521 for (count
= 0; count
< 32 && count
< act
->count
;
522 count
+= sizeof(int))
523 len
+= sprintf(page
+ len
, " %08X",
524 ((int *) (addr_t
) act
->cda
)
526 len
+= sprintf(page
+ len
, "\n");
529 printk(KERN_ERR
"%s", page
);
532 /* print failing CCW area */
534 if (act
< ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2) {
535 act
= ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2;
536 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
"......\n");
538 end
= min((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
+ 2, last
);
540 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
541 " CCW %p: %08X %08X DAT:",
542 act
, ((int *) act
)[0], ((int *) act
)[1]);
543 for (count
= 0; count
< 32 && count
< act
->count
;
544 count
+= sizeof(int))
545 len
+= sprintf(page
+ len
, " %08X",
546 ((int *) (addr_t
) act
->cda
)
548 len
+= sprintf(page
+ len
, "\n");
552 /* print last CCWs */
553 if (act
< last
- 2) {
555 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
"......\n");
557 while (act
<= last
) {
558 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
559 " CCW %p: %08X %08X DAT:",
560 act
, ((int *) act
)[0], ((int *) act
)[1]);
561 for (count
= 0; count
< 32 && count
< act
->count
;
562 count
+= sizeof(int))
563 len
+= sprintf(page
+ len
, " %08X",
564 ((int *) (addr_t
) act
->cda
)
566 len
+= sprintf(page
+ len
, "\n");
570 printk(KERN_ERR
"%s", page
);
571 free_page((unsigned long) page
);
575 * max_blocks is dependent on the amount of storage that is available
576 * in the static io buffer for each device. Currently each device has
577 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
578 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
579 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
580 * addition we have one define extent ccw + 16 bytes of data and a
581 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
583 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
584 * We want to fit two into the available memory so that we can immediately
585 * start the next request if one finishes off. That makes 100.1 blocks
586 * for one request. Give a little safety and the result is 96.
588 static struct dasd_discipline dasd_fba_discipline
= {
589 .owner
= THIS_MODULE
,
593 .check_device
= dasd_fba_check_characteristics
,
594 .do_analysis
= dasd_fba_do_analysis
,
595 .verify_path
= dasd_generic_verify_path
,
596 .fill_geometry
= dasd_fba_fill_geometry
,
597 .start_IO
= dasd_start_IO
,
598 .term_IO
= dasd_term_IO
,
599 .handle_terminated_request
= dasd_fba_handle_terminated_request
,
600 .erp_action
= dasd_fba_erp_action
,
601 .erp_postaction
= dasd_fba_erp_postaction
,
602 .check_for_device_change
= dasd_fba_check_for_device_change
,
603 .build_cp
= dasd_fba_build_cp
,
604 .free_cp
= dasd_fba_free_cp
,
605 .dump_sense
= dasd_fba_dump_sense
,
606 .dump_sense_dbf
= dasd_fba_dump_sense_dbf
,
607 .fill_info
= dasd_fba_fill_info
,
615 ASCEBC(dasd_fba_discipline
.ebcname
, 4);
616 ret
= ccw_driver_register(&dasd_fba_driver
);
618 wait_for_device_probe();
624 dasd_fba_cleanup(void)
626 ccw_driver_unregister(&dasd_fba_driver
);
629 module_init(dasd_fba_init
);
630 module_exit(dasd_fba_cleanup
);