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
= {
71 .probe
= dasd_fba_probe
,
72 .remove
= dasd_generic_remove
,
73 .set_offline
= dasd_generic_set_offline
,
74 .set_online
= dasd_fba_set_online
,
75 .notify
= dasd_generic_notify
,
76 .freeze
= dasd_generic_pm_freeze
,
77 .thaw
= dasd_generic_restore_device
,
78 .restore
= dasd_generic_restore_device
,
82 define_extent(struct ccw1
* ccw
, struct DE_fba_data
*data
, int rw
,
83 int blksize
, int beg
, int nr
)
85 ccw
->cmd_code
= DASD_FBA_CCW_DEFINE_EXTENT
;
88 ccw
->cda
= (__u32
) __pa(data
);
89 memset(data
, 0, sizeof (struct DE_fba_data
));
91 (data
->mask
).perm
= 0x0;
93 (data
->mask
).perm
= 0x1;
95 data
->mask
.perm
= 0x2;
96 data
->blk_size
= blksize
;
98 data
->ext_end
= nr
- 1;
102 locate_record(struct ccw1
* ccw
, struct LO_fba_data
*data
, int rw
,
103 int block_nr
, int block_ct
)
105 ccw
->cmd_code
= DASD_FBA_CCW_LOCATE
;
108 ccw
->cda
= (__u32
) __pa(data
);
109 memset(data
, 0, sizeof (struct LO_fba_data
));
111 data
->operation
.cmd
= 0x5;
113 data
->operation
.cmd
= 0x6;
115 data
->operation
.cmd
= 0x8;
116 data
->blk_nr
= block_nr
;
117 data
->blk_ct
= block_ct
;
121 dasd_fba_check_characteristics(struct dasd_device
*device
)
123 struct dasd_block
*block
;
124 struct dasd_fba_private
*private;
125 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_DEVID(DBF_WARNING
, cdev
, "%s", "could not allocate "
145 "dasd block structure");
146 device
->private = NULL
;
148 return PTR_ERR(block
);
150 device
->block
= block
;
151 block
->base
= device
;
153 /* Read Device Characteristics */
154 rc
= dasd_generic_read_dev_chars(device
, DASD_FBA_MAGIC
,
155 &private->rdc_data
, 32);
157 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "Read device "
158 "characteristics returned error %d", rc
);
159 device
->block
= NULL
;
160 dasd_free_block(block
);
161 device
->private = NULL
;
166 readonly
= dasd_device_is_ro(device
);
168 set_bit(DASD_FLAG_DEVICE_RO
, &device
->flags
);
170 dev_info(&device
->cdev
->dev
,
171 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
177 ((private->rdc_data
.blk_bdsa
*
178 (private->rdc_data
.blk_size
>> 9)) >> 11),
179 private->rdc_data
.blk_size
,
180 readonly
? ", read-only device" : "");
184 static int dasd_fba_do_analysis(struct dasd_block
*block
)
186 struct dasd_fba_private
*private;
189 private = (struct dasd_fba_private
*) block
->base
->private;
190 rc
= dasd_check_blocksize(private->rdc_data
.blk_size
);
192 DBF_DEV_EVENT(DBF_WARNING
, block
->base
, "unknown blocksize %d",
193 private->rdc_data
.blk_size
);
196 block
->blocks
= private->rdc_data
.blk_bdsa
;
197 block
->bp_block
= private->rdc_data
.blk_size
;
198 block
->s2b_shift
= 0; /* bits to shift 512 to get a block */
199 for (sb
= 512; sb
< private->rdc_data
.blk_size
; sb
= sb
<< 1)
204 static int dasd_fba_fill_geometry(struct dasd_block
*block
,
205 struct hd_geometry
*geo
)
207 if (dasd_check_blocksize(block
->bp_block
) != 0)
209 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
211 geo
->sectors
= 128 >> block
->s2b_shift
;
216 dasd_fba_erp_action(struct dasd_ccw_req
* cqr
)
218 return dasd_default_erp_action
;
222 dasd_fba_erp_postaction(struct dasd_ccw_req
* cqr
)
224 if (cqr
->function
== dasd_default_erp_action
)
225 return dasd_default_erp_postaction
;
227 DBF_DEV_EVENT(DBF_WARNING
, cqr
->startdev
, "unknown ERP action %p",
232 static void dasd_fba_handle_unsolicited_interrupt(struct dasd_device
*device
,
237 /* first of all check for state change pending interrupt */
238 mask
= DEV_STAT_ATTENTION
| DEV_STAT_DEV_END
| DEV_STAT_UNIT_EXCEP
;
239 if ((irb
->scsw
.cmd
.dstat
& mask
) == mask
) {
240 dasd_generic_handle_state_change(device
);
244 /* check for unsolicited interrupts */
245 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
246 "unsolicited interrupt received");
247 device
->discipline
->dump_sense_dbf(device
, irb
, "unsolicited");
248 dasd_schedule_device_bh(device
);
252 static struct dasd_ccw_req
*dasd_fba_build_cp(struct dasd_device
* memdev
,
253 struct dasd_block
*block
,
256 struct dasd_fba_private
*private;
257 unsigned long *idaws
;
258 struct LO_fba_data
*LO_data
;
259 struct dasd_ccw_req
*cqr
;
261 struct req_iterator iter
;
264 int count
, cidaw
, cplength
, datasize
;
265 sector_t recid
, first_rec
, last_rec
;
266 unsigned int blksize
, off
;
269 private = (struct dasd_fba_private
*) block
->base
->private;
270 if (rq_data_dir(req
) == READ
) {
271 cmd
= DASD_FBA_CCW_READ
;
272 } else if (rq_data_dir(req
) == WRITE
) {
273 cmd
= DASD_FBA_CCW_WRITE
;
275 return ERR_PTR(-EINVAL
);
276 blksize
= block
->bp_block
;
277 /* Calculate record id of first and last block. */
278 first_rec
= blk_rq_pos(req
) >> block
->s2b_shift
;
280 (blk_rq_pos(req
) + blk_rq_sectors(req
) - 1) >> block
->s2b_shift
;
281 /* Check struct bio and count the number of blocks for the request. */
284 rq_for_each_segment(bv
, req
, iter
) {
285 if (bv
->bv_len
& (blksize
- 1))
286 /* Fba can only do full blocks. */
287 return ERR_PTR(-EINVAL
);
288 count
+= bv
->bv_len
>> (block
->s2b_shift
+ 9);
289 #if defined(CONFIG_64BIT)
290 if (idal_is_needed (page_address(bv
->bv_page
), bv
->bv_len
))
291 cidaw
+= bv
->bv_len
/ blksize
;
295 if (count
!= last_rec
- first_rec
+ 1)
296 return ERR_PTR(-EINVAL
);
297 /* 1x define extent + 1x locate record + number of blocks */
298 cplength
= 2 + count
;
299 /* 1x define extent + 1x locate record */
300 datasize
= sizeof(struct DE_fba_data
) + sizeof(struct LO_fba_data
) +
301 cidaw
* sizeof(unsigned long);
303 * Find out number of additional locate record ccws if the device
304 * can't do data chaining.
306 if (private->rdc_data
.mode
.bits
.data_chain
== 0) {
307 cplength
+= count
- 1;
308 datasize
+= (count
- 1)*sizeof(struct LO_fba_data
);
310 /* Allocate the ccw request. */
311 cqr
= dasd_smalloc_request(DASD_FBA_MAGIC
, cplength
, datasize
, memdev
);
315 /* First ccw is define extent. */
316 define_extent(ccw
++, cqr
->data
, rq_data_dir(req
),
317 block
->bp_block
, blk_rq_pos(req
), blk_rq_sectors(req
));
318 /* Build locate_record + read/write ccws. */
319 idaws
= (unsigned long *) (cqr
->data
+ sizeof(struct DE_fba_data
));
320 LO_data
= (struct LO_fba_data
*) (idaws
+ cidaw
);
321 /* Locate record for all blocks for smart devices. */
322 if (private->rdc_data
.mode
.bits
.data_chain
!= 0) {
323 ccw
[-1].flags
|= CCW_FLAG_CC
;
324 locate_record(ccw
++, LO_data
++, rq_data_dir(req
), 0, count
);
327 rq_for_each_segment(bv
, req
, iter
) {
328 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
329 if (dasd_page_cache
) {
330 char *copy
= kmem_cache_alloc(dasd_page_cache
,
331 GFP_DMA
| __GFP_NOWARN
);
332 if (copy
&& rq_data_dir(req
) == WRITE
)
333 memcpy(copy
+ bv
->bv_offset
, dst
, bv
->bv_len
);
335 dst
= copy
+ bv
->bv_offset
;
337 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
338 /* Locate record for stupid devices. */
339 if (private->rdc_data
.mode
.bits
.data_chain
== 0) {
340 ccw
[-1].flags
|= CCW_FLAG_CC
;
341 locate_record(ccw
, LO_data
++,
343 recid
- first_rec
, 1);
344 ccw
->flags
= CCW_FLAG_CC
;
347 if (recid
> first_rec
)
348 ccw
[-1].flags
|= CCW_FLAG_DC
;
350 ccw
[-1].flags
|= CCW_FLAG_CC
;
353 ccw
->count
= block
->bp_block
;
354 if (idal_is_needed(dst
, blksize
)) {
355 ccw
->cda
= (__u32
)(addr_t
) idaws
;
356 ccw
->flags
= CCW_FLAG_IDA
;
357 idaws
= idal_create_words(idaws
, dst
, blksize
);
359 ccw
->cda
= (__u32
)(addr_t
) dst
;
367 if (blk_noretry_request(req
) ||
368 block
->base
->features
& DASD_FEATURE_FAILFAST
)
369 set_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
);
370 cqr
->startdev
= memdev
;
371 cqr
->memdev
= memdev
;
373 cqr
->expires
= 5 * 60 * HZ
; /* 5 minutes */
375 cqr
->buildclk
= get_clock();
376 cqr
->status
= DASD_CQR_FILLED
;
381 dasd_fba_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
383 struct dasd_fba_private
*private;
385 struct req_iterator iter
;
388 unsigned int blksize
, off
;
391 if (!dasd_page_cache
)
393 private = (struct dasd_fba_private
*) cqr
->block
->base
->private;
394 blksize
= cqr
->block
->bp_block
;
396 /* Skip over define extent & locate record. */
398 if (private->rdc_data
.mode
.bits
.data_chain
!= 0)
400 rq_for_each_segment(bv
, req
, iter
) {
401 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
402 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
403 /* Skip locate record. */
404 if (private->rdc_data
.mode
.bits
.data_chain
== 0)
407 if (ccw
->flags
& CCW_FLAG_IDA
)
408 cda
= *((char **)((addr_t
) ccw
->cda
));
410 cda
= (char *)((addr_t
) ccw
->cda
);
412 if (rq_data_dir(req
) == READ
)
413 memcpy(dst
, cda
, bv
->bv_len
);
414 kmem_cache_free(dasd_page_cache
,
415 (void *)((addr_t
)cda
& PAGE_MASK
));
423 status
= cqr
->status
== DASD_CQR_DONE
;
424 dasd_sfree_request(cqr
, cqr
->memdev
);
428 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req
*cqr
)
430 cqr
->status
= DASD_CQR_FILLED
;
434 dasd_fba_fill_info(struct dasd_device
* device
,
435 struct dasd_information2_t
* info
)
437 info
->label_block
= 1;
438 info
->FBA_layout
= 1;
439 info
->format
= DASD_FORMAT_LDL
;
440 info
->characteristics_size
= sizeof(struct dasd_fba_characteristics
);
441 memcpy(info
->characteristics
,
442 &((struct dasd_fba_private
*) device
->private)->rdc_data
,
443 sizeof (struct dasd_fba_characteristics
));
444 info
->confdata_size
= 0;
449 dasd_fba_dump_sense_dbf(struct dasd_device
*device
, struct irb
*irb
,
454 sense
= (u64
*) dasd_get_sense(irb
);
456 DBF_DEV_EVENT(DBF_EMERG
, device
,
457 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
459 scsw_is_tm(&irb
->scsw
) ? "t" : "c",
460 scsw_cc(&irb
->scsw
), scsw_cstat(&irb
->scsw
),
461 scsw_dstat(&irb
->scsw
), sense
[0], sense
[1],
464 DBF_DEV_EVENT(DBF_EMERG
, device
, "%s",
465 "SORRY - NO VALID SENSE AVAILABLE\n");
471 dasd_fba_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
475 struct ccw1
*act
, *end
, *last
;
476 int len
, sl
, sct
, count
;
478 page
= (char *) get_zeroed_page(GFP_ATOMIC
);
480 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
481 "No memory to dump sense data");
484 len
= sprintf(page
, KERN_ERR PRINTK_HEADER
485 " I/O status report for device %s:\n",
486 dev_name(&device
->cdev
->dev
));
487 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
488 " in req: %p CS: 0x%02X DS: 0x%02X\n", req
,
489 irb
->scsw
.cmd
.cstat
, irb
->scsw
.cmd
.dstat
);
490 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
491 " device %s: Failing CCW: %p\n",
492 dev_name(&device
->cdev
->dev
),
493 (void *) (addr_t
) irb
->scsw
.cmd
.cpa
);
494 if (irb
->esw
.esw0
.erw
.cons
) {
495 for (sl
= 0; sl
< 4; sl
++) {
496 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
497 " Sense(hex) %2d-%2d:",
498 (8 * sl
), ((8 * sl
) + 7));
500 for (sct
= 0; sct
< 8; sct
++) {
501 len
+= sprintf(page
+ len
, " %02x",
502 irb
->ecw
[8 * sl
+ sct
]);
504 len
+= sprintf(page
+ len
, "\n");
507 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
508 " SORRY - NO VALID SENSE AVAILABLE\n");
510 printk(KERN_ERR
"%s", page
);
512 /* dump the Channel Program */
513 /* print first CCWs (maximum 8) */
515 for (last
= act
; last
->flags
& (CCW_FLAG_CC
| CCW_FLAG_DC
); last
++);
516 end
= min(act
+ 8, last
);
517 len
= sprintf(page
, KERN_ERR PRINTK_HEADER
518 " Related CP in req: %p\n", req
);
520 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
521 " CCW %p: %08X %08X DAT:",
522 act
, ((int *) act
)[0], ((int *) act
)[1]);
523 for (count
= 0; count
< 32 && count
< act
->count
;
524 count
+= sizeof(int))
525 len
+= sprintf(page
+ len
, " %08X",
526 ((int *) (addr_t
) act
->cda
)
528 len
+= sprintf(page
+ len
, "\n");
531 printk(KERN_ERR
"%s", page
);
534 /* print failing CCW area */
536 if (act
< ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2) {
537 act
= ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2;
538 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
"......\n");
540 end
= min((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
+ 2, last
);
542 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
543 " CCW %p: %08X %08X DAT:",
544 act
, ((int *) act
)[0], ((int *) act
)[1]);
545 for (count
= 0; count
< 32 && count
< act
->count
;
546 count
+= sizeof(int))
547 len
+= sprintf(page
+ len
, " %08X",
548 ((int *) (addr_t
) act
->cda
)
550 len
+= sprintf(page
+ len
, "\n");
554 /* print last CCWs */
555 if (act
< last
- 2) {
557 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
"......\n");
559 while (act
<= last
) {
560 len
+= sprintf(page
+ len
, KERN_ERR PRINTK_HEADER
561 " CCW %p: %08X %08X DAT:",
562 act
, ((int *) act
)[0], ((int *) act
)[1]);
563 for (count
= 0; count
< 32 && count
< act
->count
;
564 count
+= sizeof(int))
565 len
+= sprintf(page
+ len
, " %08X",
566 ((int *) (addr_t
) act
->cda
)
568 len
+= sprintf(page
+ len
, "\n");
572 printk(KERN_ERR
"%s", page
);
573 free_page((unsigned long) page
);
577 * max_blocks is dependent on the amount of storage that is available
578 * in the static io buffer for each device. Currently each device has
579 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
580 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
581 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
582 * addition we have one define extent ccw + 16 bytes of data and a
583 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
585 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
586 * We want to fit two into the available memory so that we can immediately
587 * start the next request if one finishes off. That makes 100.1 blocks
588 * for one request. Give a little safety and the result is 96.
590 static struct dasd_discipline dasd_fba_discipline
= {
591 .owner
= THIS_MODULE
,
595 .check_device
= dasd_fba_check_characteristics
,
596 .do_analysis
= dasd_fba_do_analysis
,
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 .handle_unsolicited_interrupt
= dasd_fba_handle_unsolicited_interrupt
,
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
);