2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Bugreports.to..: <Linux390@de.ibm.com>
4 * Copyright IBM Corp. 1999, 2009
7 #define KMSG_COMPONENT "dasd-fba"
9 #include <linux/stddef.h>
10 #include <linux/kernel.h>
11 #include <asm/debug.h>
13 #include <linux/slab.h>
14 #include <linux/hdreg.h> /* HDIO_GETGEO */
15 #include <linux/bio.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
19 #include <asm/idals.h>
20 #include <asm/ebcdic.h>
22 #include <asm/ccwdev.h>
29 #endif /* PRINTK_HEADER */
30 #define PRINTK_HEADER "dasd(fba):"
32 #define FBA_DEFAULT_RETRIES 32
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
= {
74 .probe
= dasd_fba_probe
,
75 .remove
= dasd_generic_remove
,
76 .set_offline
= dasd_generic_set_offline
,
77 .set_online
= dasd_fba_set_online
,
78 .notify
= dasd_generic_notify
,
79 .path_event
= dasd_generic_path_event
,
80 .freeze
= dasd_generic_pm_freeze
,
81 .thaw
= dasd_generic_restore_device
,
82 .restore
= dasd_generic_restore_device
,
83 .int_class
= IRQIO_DAS
,
87 define_extent(struct ccw1
* ccw
, struct DE_fba_data
*data
, int rw
,
88 int blksize
, int beg
, int nr
)
90 ccw
->cmd_code
= DASD_FBA_CCW_DEFINE_EXTENT
;
93 ccw
->cda
= (__u32
) __pa(data
);
94 memset(data
, 0, sizeof (struct DE_fba_data
));
96 (data
->mask
).perm
= 0x0;
98 (data
->mask
).perm
= 0x1;
100 data
->mask
.perm
= 0x2;
101 data
->blk_size
= blksize
;
103 data
->ext_end
= nr
- 1;
107 locate_record(struct ccw1
* ccw
, struct LO_fba_data
*data
, int rw
,
108 int block_nr
, int block_ct
)
110 ccw
->cmd_code
= DASD_FBA_CCW_LOCATE
;
113 ccw
->cda
= (__u32
) __pa(data
);
114 memset(data
, 0, sizeof (struct LO_fba_data
));
116 data
->operation
.cmd
= 0x5;
118 data
->operation
.cmd
= 0x6;
120 data
->operation
.cmd
= 0x8;
121 data
->blk_nr
= block_nr
;
122 data
->blk_ct
= block_ct
;
126 dasd_fba_check_characteristics(struct dasd_device
*device
)
128 struct dasd_block
*block
;
129 struct dasd_fba_private
*private;
130 struct ccw_device
*cdev
= device
->cdev
;
134 private = (struct dasd_fba_private
*) device
->private;
136 private = kzalloc(sizeof(*private), GFP_KERNEL
| GFP_DMA
);
138 dev_warn(&device
->cdev
->dev
,
139 "Allocating memory for private DASD "
143 device
->private = (void *) private;
145 memset(private, 0, sizeof(*private));
147 block
= dasd_alloc_block();
149 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "%s", "could not allocate "
150 "dasd block structure");
151 device
->private = NULL
;
153 return PTR_ERR(block
);
155 device
->block
= block
;
156 block
->base
= device
;
158 /* Read Device Characteristics */
159 rc
= dasd_generic_read_dev_chars(device
, DASD_FBA_MAGIC
,
160 &private->rdc_data
, 32);
162 DBF_EVENT_DEVID(DBF_WARNING
, cdev
, "Read device "
163 "characteristics returned error %d", rc
);
164 device
->block
= NULL
;
165 dasd_free_block(block
);
166 device
->private = NULL
;
171 device
->default_expires
= DASD_EXPIRES
;
172 device
->default_retries
= FBA_DEFAULT_RETRIES
;
173 device
->path_data
.opm
= LPM_ANYPATH
;
175 readonly
= dasd_device_is_ro(device
);
177 set_bit(DASD_FLAG_DEVICE_RO
, &device
->flags
);
179 dev_info(&device
->cdev
->dev
,
180 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
186 ((private->rdc_data
.blk_bdsa
*
187 (private->rdc_data
.blk_size
>> 9)) >> 11),
188 private->rdc_data
.blk_size
,
189 readonly
? ", read-only device" : "");
193 static int dasd_fba_do_analysis(struct dasd_block
*block
)
195 struct dasd_fba_private
*private;
198 private = (struct dasd_fba_private
*) block
->base
->private;
199 rc
= dasd_check_blocksize(private->rdc_data
.blk_size
);
201 DBF_DEV_EVENT(DBF_WARNING
, block
->base
, "unknown blocksize %d",
202 private->rdc_data
.blk_size
);
205 block
->blocks
= private->rdc_data
.blk_bdsa
;
206 block
->bp_block
= private->rdc_data
.blk_size
;
207 block
->s2b_shift
= 0; /* bits to shift 512 to get a block */
208 for (sb
= 512; sb
< private->rdc_data
.blk_size
; sb
= sb
<< 1)
213 static int dasd_fba_fill_geometry(struct dasd_block
*block
,
214 struct hd_geometry
*geo
)
216 if (dasd_check_blocksize(block
->bp_block
) != 0)
218 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
220 geo
->sectors
= 128 >> block
->s2b_shift
;
225 dasd_fba_erp_action(struct dasd_ccw_req
* cqr
)
227 return dasd_default_erp_action
;
231 dasd_fba_erp_postaction(struct dasd_ccw_req
* cqr
)
233 if (cqr
->function
== dasd_default_erp_action
)
234 return dasd_default_erp_postaction
;
236 DBF_DEV_EVENT(DBF_WARNING
, cqr
->startdev
, "unknown ERP action %p",
241 static void dasd_fba_check_for_device_change(struct dasd_device
*device
,
242 struct dasd_ccw_req
*cqr
,
247 /* first of all check for state change pending interrupt */
248 mask
= DEV_STAT_ATTENTION
| DEV_STAT_DEV_END
| DEV_STAT_UNIT_EXCEP
;
249 if ((irb
->scsw
.cmd
.dstat
& mask
) == mask
)
250 dasd_generic_handle_state_change(device
);
253 static struct dasd_ccw_req
*dasd_fba_build_cp(struct dasd_device
* memdev
,
254 struct dasd_block
*block
,
257 struct dasd_fba_private
*private;
258 unsigned long *idaws
;
259 struct LO_fba_data
*LO_data
;
260 struct dasd_ccw_req
*cqr
;
262 struct req_iterator iter
;
265 int count
, cidaw
, cplength
, datasize
;
266 sector_t recid
, first_rec
, last_rec
;
267 unsigned int blksize
, off
;
270 private = (struct dasd_fba_private
*) block
->base
->private;
271 if (rq_data_dir(req
) == READ
) {
272 cmd
= DASD_FBA_CCW_READ
;
273 } else if (rq_data_dir(req
) == WRITE
) {
274 cmd
= DASD_FBA_CCW_WRITE
;
276 return ERR_PTR(-EINVAL
);
277 blksize
= block
->bp_block
;
278 /* Calculate record id of first and last block. */
279 first_rec
= blk_rq_pos(req
) >> block
->s2b_shift
;
281 (blk_rq_pos(req
) + blk_rq_sectors(req
) - 1) >> block
->s2b_shift
;
282 /* Check struct bio and count the number of blocks for the request. */
285 rq_for_each_segment(bv
, req
, iter
) {
286 if (bv
.bv_len
& (blksize
- 1))
287 /* Fba can only do full blocks. */
288 return ERR_PTR(-EINVAL
);
289 count
+= bv
.bv_len
>> (block
->s2b_shift
+ 9);
290 #if defined(CONFIG_64BIT)
291 if (idal_is_needed (page_address(bv
.bv_page
), bv
.bv_len
))
292 cidaw
+= bv
.bv_len
/ blksize
;
296 if (count
!= last_rec
- first_rec
+ 1)
297 return ERR_PTR(-EINVAL
);
298 /* 1x define extent + 1x locate record + number of blocks */
299 cplength
= 2 + count
;
300 /* 1x define extent + 1x locate record */
301 datasize
= sizeof(struct DE_fba_data
) + sizeof(struct LO_fba_data
) +
302 cidaw
* sizeof(unsigned long);
304 * Find out number of additional locate record ccws if the device
305 * can't do data chaining.
307 if (private->rdc_data
.mode
.bits
.data_chain
== 0) {
308 cplength
+= count
- 1;
309 datasize
+= (count
- 1)*sizeof(struct LO_fba_data
);
311 /* Allocate the ccw request. */
312 cqr
= dasd_smalloc_request(DASD_FBA_MAGIC
, cplength
, datasize
, memdev
);
316 /* First ccw is define extent. */
317 define_extent(ccw
++, cqr
->data
, rq_data_dir(req
),
318 block
->bp_block
, blk_rq_pos(req
), blk_rq_sectors(req
));
319 /* Build locate_record + read/write ccws. */
320 idaws
= (unsigned long *) (cqr
->data
+ sizeof(struct DE_fba_data
));
321 LO_data
= (struct LO_fba_data
*) (idaws
+ cidaw
);
322 /* Locate record for all blocks for smart devices. */
323 if (private->rdc_data
.mode
.bits
.data_chain
!= 0) {
324 ccw
[-1].flags
|= CCW_FLAG_CC
;
325 locate_record(ccw
++, LO_data
++, rq_data_dir(req
), 0, count
);
328 rq_for_each_segment(bv
, req
, iter
) {
329 dst
= page_address(bv
.bv_page
) + bv
.bv_offset
;
330 if (dasd_page_cache
) {
331 char *copy
= kmem_cache_alloc(dasd_page_cache
,
332 GFP_DMA
| __GFP_NOWARN
);
333 if (copy
&& rq_data_dir(req
) == WRITE
)
334 memcpy(copy
+ bv
.bv_offset
, dst
, bv
.bv_len
);
336 dst
= copy
+ bv
.bv_offset
;
338 for (off
= 0; off
< bv
.bv_len
; off
+= blksize
) {
339 /* Locate record for stupid devices. */
340 if (private->rdc_data
.mode
.bits
.data_chain
== 0) {
341 ccw
[-1].flags
|= CCW_FLAG_CC
;
342 locate_record(ccw
, LO_data
++,
344 recid
- first_rec
, 1);
345 ccw
->flags
= CCW_FLAG_CC
;
348 if (recid
> first_rec
)
349 ccw
[-1].flags
|= CCW_FLAG_DC
;
351 ccw
[-1].flags
|= CCW_FLAG_CC
;
354 ccw
->count
= block
->bp_block
;
355 if (idal_is_needed(dst
, blksize
)) {
356 ccw
->cda
= (__u32
)(addr_t
) idaws
;
357 ccw
->flags
= CCW_FLAG_IDA
;
358 idaws
= idal_create_words(idaws
, dst
, blksize
);
360 ccw
->cda
= (__u32
)(addr_t
) dst
;
368 if (blk_noretry_request(req
) ||
369 block
->base
->features
& DASD_FEATURE_FAILFAST
)
370 set_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
);
371 cqr
->startdev
= memdev
;
372 cqr
->memdev
= memdev
;
374 cqr
->expires
= memdev
->default_expires
* HZ
; /* default 5 minutes */
375 cqr
->retries
= memdev
->default_retries
;
376 cqr
->buildclk
= get_tod_clock();
377 cqr
->status
= DASD_CQR_FILLED
;
382 dasd_fba_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
384 struct dasd_fba_private
*private;
386 struct req_iterator iter
;
389 unsigned int blksize
, off
;
392 if (!dasd_page_cache
)
394 private = (struct dasd_fba_private
*) cqr
->block
->base
->private;
395 blksize
= cqr
->block
->bp_block
;
397 /* Skip over define extent & locate record. */
399 if (private->rdc_data
.mode
.bits
.data_chain
!= 0)
401 rq_for_each_segment(bv
, req
, iter
) {
402 dst
= page_address(bv
.bv_page
) + bv
.bv_offset
;
403 for (off
= 0; off
< bv
.bv_len
; off
+= blksize
) {
404 /* Skip locate record. */
405 if (private->rdc_data
.mode
.bits
.data_chain
== 0)
408 if (ccw
->flags
& CCW_FLAG_IDA
)
409 cda
= *((char **)((addr_t
) ccw
->cda
));
411 cda
= (char *)((addr_t
) ccw
->cda
);
413 if (rq_data_dir(req
) == READ
)
414 memcpy(dst
, cda
, bv
.bv_len
);
415 kmem_cache_free(dasd_page_cache
,
416 (void *)((addr_t
)cda
& PAGE_MASK
));
424 status
= cqr
->status
== DASD_CQR_DONE
;
425 dasd_sfree_request(cqr
, cqr
->memdev
);
429 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req
*cqr
)
431 if (cqr
->retries
< 0)
432 cqr
->status
= DASD_CQR_FAILED
;
434 cqr
->status
= DASD_CQR_FILLED
;
438 dasd_fba_fill_info(struct dasd_device
* device
,
439 struct dasd_information2_t
* info
)
441 info
->label_block
= 1;
442 info
->FBA_layout
= 1;
443 info
->format
= DASD_FORMAT_LDL
;
444 info
->characteristics_size
= sizeof(struct dasd_fba_characteristics
);
445 memcpy(info
->characteristics
,
446 &((struct dasd_fba_private
*) device
->private)->rdc_data
,
447 sizeof (struct dasd_fba_characteristics
));
448 info
->confdata_size
= 0;
453 dasd_fba_dump_sense_dbf(struct dasd_device
*device
, struct irb
*irb
,
458 sense
= (u64
*) dasd_get_sense(irb
);
460 DBF_DEV_EVENT(DBF_EMERG
, device
,
461 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
463 scsw_is_tm(&irb
->scsw
) ? "t" : "c",
464 scsw_cc(&irb
->scsw
), scsw_cstat(&irb
->scsw
),
465 scsw_dstat(&irb
->scsw
), sense
[0], sense
[1],
468 DBF_DEV_EVENT(DBF_EMERG
, device
, "%s",
469 "SORRY - NO VALID SENSE AVAILABLE\n");
475 dasd_fba_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
479 struct ccw1
*act
, *end
, *last
;
480 int len
, sl
, sct
, count
;
482 page
= (char *) get_zeroed_page(GFP_ATOMIC
);
484 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
485 "No memory to dump sense data");
488 len
= sprintf(page
, PRINTK_HEADER
489 " I/O status report for device %s:\n",
490 dev_name(&device
->cdev
->dev
));
491 len
+= sprintf(page
+ len
, PRINTK_HEADER
492 " in req: %p CS: 0x%02X DS: 0x%02X\n", req
,
493 irb
->scsw
.cmd
.cstat
, irb
->scsw
.cmd
.dstat
);
494 len
+= sprintf(page
+ len
, PRINTK_HEADER
495 " device %s: Failing CCW: %p\n",
496 dev_name(&device
->cdev
->dev
),
497 (void *) (addr_t
) irb
->scsw
.cmd
.cpa
);
498 if (irb
->esw
.esw0
.erw
.cons
) {
499 for (sl
= 0; sl
< 4; sl
++) {
500 len
+= sprintf(page
+ len
, PRINTK_HEADER
501 " Sense(hex) %2d-%2d:",
502 (8 * sl
), ((8 * sl
) + 7));
504 for (sct
= 0; sct
< 8; sct
++) {
505 len
+= sprintf(page
+ len
, " %02x",
506 irb
->ecw
[8 * sl
+ sct
]);
508 len
+= sprintf(page
+ len
, "\n");
511 len
+= sprintf(page
+ len
, PRINTK_HEADER
512 " SORRY - NO VALID SENSE AVAILABLE\n");
514 printk(KERN_ERR
"%s", page
);
516 /* dump the Channel Program */
517 /* print first CCWs (maximum 8) */
519 for (last
= act
; last
->flags
& (CCW_FLAG_CC
| CCW_FLAG_DC
); last
++);
520 end
= min(act
+ 8, last
);
521 len
= sprintf(page
, PRINTK_HEADER
" Related CP in req: %p\n", req
);
523 len
+= sprintf(page
+ len
, PRINTK_HEADER
524 " CCW %p: %08X %08X DAT:",
525 act
, ((int *) act
)[0], ((int *) act
)[1]);
526 for (count
= 0; count
< 32 && count
< act
->count
;
527 count
+= sizeof(int))
528 len
+= sprintf(page
+ len
, " %08X",
529 ((int *) (addr_t
) act
->cda
)
531 len
+= sprintf(page
+ len
, "\n");
534 printk(KERN_ERR
"%s", page
);
537 /* print failing CCW area */
539 if (act
< ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2) {
540 act
= ((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
) - 2;
541 len
+= sprintf(page
+ len
, PRINTK_HEADER
"......\n");
543 end
= min((struct ccw1
*)(addr_t
) irb
->scsw
.cmd
.cpa
+ 2, last
);
545 len
+= sprintf(page
+ len
, PRINTK_HEADER
546 " CCW %p: %08X %08X DAT:",
547 act
, ((int *) act
)[0], ((int *) act
)[1]);
548 for (count
= 0; count
< 32 && count
< act
->count
;
549 count
+= sizeof(int))
550 len
+= sprintf(page
+ len
, " %08X",
551 ((int *) (addr_t
) act
->cda
)
553 len
+= sprintf(page
+ len
, "\n");
557 /* print last CCWs */
558 if (act
< last
- 2) {
560 len
+= sprintf(page
+ len
, PRINTK_HEADER
"......\n");
562 while (act
<= last
) {
563 len
+= sprintf(page
+ len
, PRINTK_HEADER
564 " CCW %p: %08X %08X DAT:",
565 act
, ((int *) act
)[0], ((int *) act
)[1]);
566 for (count
= 0; count
< 32 && count
< act
->count
;
567 count
+= sizeof(int))
568 len
+= sprintf(page
+ len
, " %08X",
569 ((int *) (addr_t
) act
->cda
)
571 len
+= sprintf(page
+ len
, "\n");
575 printk(KERN_ERR
"%s", page
);
576 free_page((unsigned long) page
);
580 * max_blocks is dependent on the amount of storage that is available
581 * in the static io buffer for each device. Currently each device has
582 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
583 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
584 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
585 * addition we have one define extent ccw + 16 bytes of data and a
586 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
588 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
589 * We want to fit two into the available memory so that we can immediately
590 * start the next request if one finishes off. That makes 100.1 blocks
591 * for one request. Give a little safety and the result is 96.
593 static struct dasd_discipline dasd_fba_discipline
= {
594 .owner
= THIS_MODULE
,
598 .check_device
= dasd_fba_check_characteristics
,
599 .do_analysis
= dasd_fba_do_analysis
,
600 .verify_path
= dasd_generic_verify_path
,
601 .fill_geometry
= dasd_fba_fill_geometry
,
602 .start_IO
= dasd_start_IO
,
603 .term_IO
= dasd_term_IO
,
604 .handle_terminated_request
= dasd_fba_handle_terminated_request
,
605 .erp_action
= dasd_fba_erp_action
,
606 .erp_postaction
= dasd_fba_erp_postaction
,
607 .check_for_device_change
= dasd_fba_check_for_device_change
,
608 .build_cp
= dasd_fba_build_cp
,
609 .free_cp
= dasd_fba_free_cp
,
610 .dump_sense
= dasd_fba_dump_sense
,
611 .dump_sense_dbf
= dasd_fba_dump_sense_dbf
,
612 .fill_info
= dasd_fba_fill_info
,
620 ASCEBC(dasd_fba_discipline
.ebcname
, 4);
621 ret
= ccw_driver_register(&dasd_fba_driver
);
623 wait_for_device_probe();
629 dasd_fba_cleanup(void)
631 ccw_driver_unregister(&dasd_fba_driver
);
634 module_init(dasd_fba_init
);
635 module_exit(dasd_fba_cleanup
);