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
,
82 .int_class
= IOINT_DAS
,
86 define_extent(struct ccw1
* ccw
, struct DE_fba_data
*data
, int rw
,
87 int blksize
, int beg
, int nr
)
89 ccw
->cmd_code
= DASD_FBA_CCW_DEFINE_EXTENT
;
92 ccw
->cda
= (__u32
) __pa(data
);
93 memset(data
, 0, sizeof (struct DE_fba_data
));
95 (data
->mask
).perm
= 0x0;
97 (data
->mask
).perm
= 0x1;
99 data
->mask
.perm
= 0x2;
100 data
->blk_size
= blksize
;
102 data
->ext_end
= nr
- 1;
106 locate_record(struct ccw1
* ccw
, struct LO_fba_data
*data
, int rw
,
107 int block_nr
, int block_ct
)
109 ccw
->cmd_code
= DASD_FBA_CCW_LOCATE
;
112 ccw
->cda
= (__u32
) __pa(data
);
113 memset(data
, 0, sizeof (struct LO_fba_data
));
115 data
->operation
.cmd
= 0x5;
117 data
->operation
.cmd
= 0x6;
119 data
->operation
.cmd
= 0x8;
120 data
->blk_nr
= block_nr
;
121 data
->blk_ct
= block_ct
;
125 dasd_fba_check_characteristics(struct dasd_device
*device
)
127 struct dasd_block
*block
;
128 struct dasd_fba_private
*private;
129 struct ccw_device
*cdev
= device
->cdev
;
133 private = (struct dasd_fba_private
*) device
->private;
135 private = kzalloc(sizeof(*private), GFP_KERNEL
| GFP_DMA
);
137 dev_warn(&device
->cdev
->dev
,
138 "Allocating memory for private DASD "
142 device
->private = (void *) private;
144 memset(private, 0, sizeof(*private));
146 block
= dasd_alloc_block();
148 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "%s", "could not allocate "
149 "dasd block structure");
150 device
->private = NULL
;
152 return PTR_ERR(block
);
154 device
->block
= block
;
155 block
->base
= device
;
157 /* Read Device Characteristics */
158 rc
= dasd_generic_read_dev_chars(device
, DASD_FBA_MAGIC
,
159 &private->rdc_data
, 32);
161 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "Read device "
162 "characteristics returned error %d", rc
);
163 device
->block
= NULL
;
164 dasd_free_block(block
);
165 device
->private = NULL
;
170 device
->default_expires
= DASD_EXPIRES
;
171 device
->path_data
.opm
= LPM_ANYPATH
;
173 readonly
= dasd_device_is_ro(device
);
175 set_bit(DASD_FLAG_DEVICE_RO
, &device
->flags
);
177 dev_info(&device
->cdev
->dev
,
178 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
184 ((private->rdc_data
.blk_bdsa
*
185 (private->rdc_data
.blk_size
>> 9)) >> 11),
186 private->rdc_data
.blk_size
,
187 readonly
? ", read-only device" : "");
191 static int dasd_fba_do_analysis(struct dasd_block
*block
)
193 struct dasd_fba_private
*private;
196 private = (struct dasd_fba_private
*) block
->base
->private;
197 rc
= dasd_check_blocksize(private->rdc_data
.blk_size
);
199 DBF_DEV_EVENT(DBF_WARNING
, block
->base
, "unknown blocksize %d",
200 private->rdc_data
.blk_size
);
203 block
->blocks
= private->rdc_data
.blk_bdsa
;
204 block
->bp_block
= private->rdc_data
.blk_size
;
205 block
->s2b_shift
= 0; /* bits to shift 512 to get a block */
206 for (sb
= 512; sb
< private->rdc_data
.blk_size
; sb
= sb
<< 1)
211 static int dasd_fba_fill_geometry(struct dasd_block
*block
,
212 struct hd_geometry
*geo
)
214 if (dasd_check_blocksize(block
->bp_block
) != 0)
216 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
218 geo
->sectors
= 128 >> block
->s2b_shift
;
223 dasd_fba_erp_action(struct dasd_ccw_req
* cqr
)
225 return dasd_default_erp_action
;
229 dasd_fba_erp_postaction(struct dasd_ccw_req
* cqr
)
231 if (cqr
->function
== dasd_default_erp_action
)
232 return dasd_default_erp_postaction
;
234 DBF_DEV_EVENT(DBF_WARNING
, cqr
->startdev
, "unknown ERP action %p",
239 static void dasd_fba_check_for_device_change(struct dasd_device
*device
,
240 struct dasd_ccw_req
*cqr
,
245 /* first of all check for state change pending interrupt */
246 mask
= DEV_STAT_ATTENTION
| DEV_STAT_DEV_END
| DEV_STAT_UNIT_EXCEP
;
247 if ((irb
->scsw
.cmd
.dstat
& mask
) == mask
)
248 dasd_generic_handle_state_change(device
);
251 static struct dasd_ccw_req
*dasd_fba_build_cp(struct dasd_device
* memdev
,
252 struct dasd_block
*block
,
255 struct dasd_fba_private
*private;
256 unsigned long *idaws
;
257 struct LO_fba_data
*LO_data
;
258 struct dasd_ccw_req
*cqr
;
260 struct req_iterator iter
;
263 int count
, cidaw
, cplength
, datasize
;
264 sector_t recid
, first_rec
, last_rec
;
265 unsigned int blksize
, off
;
268 private = (struct dasd_fba_private
*) block
->base
->private;
269 if (rq_data_dir(req
) == READ
) {
270 cmd
= DASD_FBA_CCW_READ
;
271 } else if (rq_data_dir(req
) == WRITE
) {
272 cmd
= DASD_FBA_CCW_WRITE
;
274 return ERR_PTR(-EINVAL
);
275 blksize
= block
->bp_block
;
276 /* Calculate record id of first and last block. */
277 first_rec
= blk_rq_pos(req
) >> block
->s2b_shift
;
279 (blk_rq_pos(req
) + blk_rq_sectors(req
) - 1) >> block
->s2b_shift
;
280 /* Check struct bio and count the number of blocks for the request. */
283 rq_for_each_segment(bv
, req
, iter
) {
284 if (bv
->bv_len
& (blksize
- 1))
285 /* Fba can only do full blocks. */
286 return ERR_PTR(-EINVAL
);
287 count
+= bv
->bv_len
>> (block
->s2b_shift
+ 9);
288 #if defined(CONFIG_64BIT)
289 if (idal_is_needed (page_address(bv
->bv_page
), bv
->bv_len
))
290 cidaw
+= bv
->bv_len
/ blksize
;
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
);
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
);
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
);
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
++,
342 recid
- first_rec
, 1);
343 ccw
->flags
= CCW_FLAG_CC
;
346 if (recid
> first_rec
)
347 ccw
[-1].flags
|= CCW_FLAG_DC
;
349 ccw
[-1].flags
|= CCW_FLAG_CC
;
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
);
358 ccw
->cda
= (__u32
)(addr_t
) dst
;
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
;
372 cqr
->expires
= memdev
->default_expires
* HZ
; /* default 5 minutes */
374 cqr
->buildclk
= get_clock();
375 cqr
->status
= DASD_CQR_FILLED
;
380 dasd_fba_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
382 struct dasd_fba_private
*private;
384 struct req_iterator iter
;
387 unsigned int blksize
, off
;
390 if (!dasd_page_cache
)
392 private = (struct dasd_fba_private
*) cqr
->block
->base
->private;
393 blksize
= cqr
->block
->bp_block
;
395 /* Skip over define extent & locate record. */
397 if (private->rdc_data
.mode
.bits
.data_chain
!= 0)
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)
406 if (ccw
->flags
& CCW_FLAG_IDA
)
407 cda
= *((char **)((addr_t
) ccw
->cda
));
409 cda
= (char *)((addr_t
) ccw
->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
));
422 status
= cqr
->status
== DASD_CQR_DONE
;
423 dasd_sfree_request(cqr
, cqr
->memdev
);
427 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req
*cqr
)
429 cqr
->status
= DASD_CQR_FILLED
;
433 dasd_fba_fill_info(struct dasd_device
* device
,
434 struct dasd_information2_t
* info
)
436 info
->label_block
= 1;
437 info
->FBA_layout
= 1;
438 info
->format
= DASD_FORMAT_LDL
;
439 info
->characteristics_size
= sizeof(struct dasd_fba_characteristics
);
440 memcpy(info
->characteristics
,
441 &((struct dasd_fba_private
*) device
->private)->rdc_data
,
442 sizeof (struct dasd_fba_characteristics
));
443 info
->confdata_size
= 0;
448 dasd_fba_dump_sense_dbf(struct dasd_device
*device
, struct irb
*irb
,
453 sense
= (u64
*) dasd_get_sense(irb
);
455 DBF_DEV_EVENT(DBF_EMERG
, device
,
456 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
458 scsw_is_tm(&irb
->scsw
) ? "t" : "c",
459 scsw_cc(&irb
->scsw
), scsw_cstat(&irb
->scsw
),
460 scsw_dstat(&irb
->scsw
), sense
[0], sense
[1],
463 DBF_DEV_EVENT(DBF_EMERG
, device
, "%s",
464 "SORRY - NO VALID SENSE AVAILABLE\n");
470 dasd_fba_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
474 struct ccw1
*act
, *end
, *last
;
475 int len
, sl
, sct
, count
;
477 page
= (char *) get_zeroed_page(GFP_ATOMIC
);
479 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
480 "No memory to dump sense data");
483 len
= sprintf(page
, KERN_ERR PRINTK_HEADER
484 " I/O status report for device %s:\n",
485 dev_name(&device
->cdev
->dev
));
486 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
487 " in req: %p CS: 0x%02X DS: 0x%02X\n", req
,
488 irb
->scsw
.cmd
.cstat
, irb
->scsw
.cmd
.dstat
);
489 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
490 " device %s: Failing CCW: %p\n",
491 dev_name(&device
->cdev
->dev
),
492 (void *) (addr_t
) irb
->scsw
.cmd
.cpa
);
493 if (irb
->esw
.esw0
.erw
.cons
) {
494 for (sl
= 0; sl
< 4; sl
++) {
495 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
496 " Sense(hex) %2d-%2d:",
497 (8 * sl
), ((8 * sl
) + 7));
499 for (sct
= 0; sct
< 8; sct
++) {
500 len
+= sprintf(page
+ len
, " %02x",
501 irb
->ecw
[8 * sl
+ sct
]);
503 len
+= sprintf(page
+ len
, "\n");
506 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
507 " SORRY - NO VALID SENSE AVAILABLE\n");
509 printk(KERN_ERR
"%s", page
);
511 /* dump the Channel Program */
512 /* print first CCWs (maximum 8) */
514 for (last
= act
; last
->flags
& (CCW_FLAG_CC
| CCW_FLAG_DC
); last
++);
515 end
= min(act
+ 8, last
);
516 len
= sprintf(page
, KERN_ERR PRINTK_HEADER
517 " Related CP in req: %p\n", req
);
519 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
520 " CCW %p: %08X %08X DAT:",
521 act
, ((int *) act
)[0], ((int *) act
)[1]);
522 for (count
= 0; count
< 32 && count
< act
->count
;
523 count
+= sizeof(int))
524 len
+= sprintf(page
+ len
, " %08X",
525 ((int *) (addr_t
) act
->cda
)
527 len
+= sprintf(page
+ len
, "\n");
530 printk(KERN_ERR
"%s", page
);
533 /* print failing CCW area */
535 if (act
< ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2) {
536 act
= ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2;
537 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
"......\n");
539 end
= min((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
+ 2, last
);
541 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
542 " CCW %p: %08X %08X DAT:",
543 act
, ((int *) act
)[0], ((int *) act
)[1]);
544 for (count
= 0; count
< 32 && count
< act
->count
;
545 count
+= sizeof(int))
546 len
+= sprintf(page
+ len
, " %08X",
547 ((int *) (addr_t
) act
->cda
)
549 len
+= sprintf(page
+ len
, "\n");
553 /* print last CCWs */
554 if (act
< last
- 2) {
556 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
"......\n");
558 while (act
<= last
) {
559 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
560 " CCW %p: %08X %08X DAT:",
561 act
, ((int *) act
)[0], ((int *) act
)[1]);
562 for (count
= 0; count
< 32 && count
< act
->count
;
563 count
+= sizeof(int))
564 len
+= sprintf(page
+ len
, " %08X",
565 ((int *) (addr_t
) act
->cda
)
567 len
+= sprintf(page
+ len
, "\n");
571 printk(KERN_ERR
"%s", page
);
572 free_page((unsigned long) page
);
576 * max_blocks is dependent on the amount of storage that is available
577 * in the static io buffer for each device. Currently each device has
578 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
579 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
580 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
581 * addition we have one define extent ccw + 16 bytes of data and a
582 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
584 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
585 * We want to fit two into the available memory so that we can immediately
586 * start the next request if one finishes off. That makes 100.1 blocks
587 * for one request. Give a little safety and the result is 96.
589 static struct dasd_discipline dasd_fba_discipline
= {
590 .owner
= THIS_MODULE
,
594 .check_device
= dasd_fba_check_characteristics
,
595 .do_analysis
= dasd_fba_do_analysis
,
596 .verify_path
= dasd_generic_verify_path
,
597 .fill_geometry
= dasd_fba_fill_geometry
,
598 .start_IO
= dasd_start_IO
,
599 .term_IO
= dasd_term_IO
,
600 .handle_terminated_request
= dasd_fba_handle_terminated_request
,
601 .erp_action
= dasd_fba_erp_action
,
602 .erp_postaction
= dasd_fba_erp_postaction
,
603 .check_for_device_change
= dasd_fba_check_for_device_change
,
604 .build_cp
= dasd_fba_build_cp
,
605 .free_cp
= dasd_fba_free_cp
,
606 .dump_sense
= dasd_fba_dump_sense
,
607 .dump_sense_dbf
= dasd_fba_dump_sense_dbf
,
608 .fill_info
= dasd_fba_fill_info
,
616 ASCEBC(dasd_fba_discipline
.ebcname
, 4);
617 ret
= ccw_driver_register(&dasd_fba_driver
);
619 wait_for_device_probe();
625 dasd_fba_cleanup(void)
627 ccw_driver_unregister(&dasd_fba_driver
);
630 module_init(dasd_fba_init
);
631 module_exit(dasd_fba_cleanup
);