2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Based on.......: linux/drivers/s390/block/mdisk.c
4 * ...............: by Hartmunt Penner <hpenner@de.ibm.com>
5 * Bugreports.to..: <Linux390@de.ibm.com>
6 * Copyright IBM Corp. 1999, 2000
10 #define KMSG_COMPONENT "dasd"
12 #include <linux/kernel_stat.h>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/hdreg.h>
17 #include <linux/bio.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/jiffies.h>
23 #include <asm/debug.h>
24 #include <asm/ebcdic.h>
31 #include "dasd_diag.h"
33 #define PRINTK_HEADER "dasd(diag):"
35 MODULE_LICENSE("GPL");
37 /* The maximum number of blocks per request (max_blocks) is dependent on the
38 * amount of storage that is available in the static I/O buffer for each
39 * device. Currently each device gets 2 pages. We want to fit two requests
40 * into the available memory so that we can immediately start the next if one
42 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
43 sizeof(struct dasd_diag_req)) / \
44 sizeof(struct dasd_diag_bio)) / 2)
45 #define DIAG_MAX_RETRIES 32
46 #define DIAG_TIMEOUT 50
48 static struct dasd_discipline dasd_diag_discipline
;
50 struct dasd_diag_private
{
51 struct dasd_diag_characteristics rdc_data
;
52 struct dasd_diag_rw_io iob
;
53 struct dasd_diag_init_io iib
;
55 struct ccw_dev_id dev_id
;
58 struct dasd_diag_req
{
59 unsigned int block_count
;
60 struct dasd_diag_bio bio
[0];
63 static const u8 DASD_DIAG_CMS1
[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
65 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
66 * and function code cmd.
67 * In case of an exception return 3. Otherwise return result of bitwise OR of
68 * resulting condition code and DIAG return code. */
69 static inline int dia250(void *iob
, int cmd
)
71 register unsigned long reg2
asm ("2") = (unsigned long) iob
;
73 struct dasd_diag_init_io init_io
;
74 struct dasd_diag_rw_io rw_io
;
86 : "+d" (rc
), "=m" (*(addr_type
*) iob
)
87 : "d" (cmd
), "d" (reg2
), "m" (*(addr_type
*) iob
)
92 /* Initialize block I/O to DIAG device using the specified blocksize and
93 * block offset. On success, return zero and set end_block to contain the
94 * number of blocks on the device minus the specified offset. Return non-zero
97 mdsk_init_io(struct dasd_device
*device
, unsigned int blocksize
,
98 blocknum_t offset
, blocknum_t
*end_block
)
100 struct dasd_diag_private
*private;
101 struct dasd_diag_init_io
*iib
;
104 private = (struct dasd_diag_private
*) device
->private;
106 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
108 iib
->dev_nr
= private->dev_id
.devno
;
109 iib
->block_size
= blocksize
;
110 iib
->offset
= offset
;
111 iib
->flaga
= DASD_DIAG_FLAGA_DEFAULT
;
113 rc
= dia250(iib
, INIT_BIO
);
115 if ((rc
& 3) == 0 && end_block
)
116 *end_block
= iib
->end_block
;
121 /* Remove block I/O environment for device. Return zero on success, non-zero
124 mdsk_term_io(struct dasd_device
* device
)
126 struct dasd_diag_private
*private;
127 struct dasd_diag_init_io
*iib
;
130 private = (struct dasd_diag_private
*) device
->private;
132 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
133 iib
->dev_nr
= private->dev_id
.devno
;
134 rc
= dia250(iib
, TERM_BIO
);
138 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
141 dasd_diag_erp(struct dasd_device
*device
)
145 mdsk_term_io(device
);
146 rc
= mdsk_init_io(device
, device
->block
->bp_block
, 0, NULL
);
148 if (!(test_and_set_bit(DASD_FLAG_DEVICE_RO
, &device
->flags
)))
149 pr_warning("%s: The access mode of a DIAG device "
150 "changed to read-only\n",
151 dev_name(&device
->cdev
->dev
));
155 pr_warning("%s: DIAG ERP failed with "
156 "rc=%d\n", dev_name(&device
->cdev
->dev
), rc
);
159 /* Start a given request at the device. Return zero on success, non-zero
162 dasd_start_diag(struct dasd_ccw_req
* cqr
)
164 struct dasd_device
*device
;
165 struct dasd_diag_private
*private;
166 struct dasd_diag_req
*dreq
;
169 device
= cqr
->startdev
;
170 if (cqr
->retries
< 0) {
171 DBF_DEV_EVENT(DBF_ERR
, device
, "DIAG start_IO: request %p "
172 "- no retry left)", cqr
);
173 cqr
->status
= DASD_CQR_ERROR
;
176 private = (struct dasd_diag_private
*) device
->private;
177 dreq
= (struct dasd_diag_req
*) cqr
->data
;
179 private->iob
.dev_nr
= private->dev_id
.devno
;
180 private->iob
.key
= 0;
181 private->iob
.flags
= DASD_DIAG_RWFLAG_ASYNC
;
182 private->iob
.block_count
= dreq
->block_count
;
183 private->iob
.interrupt_params
= (addr_t
) cqr
;
184 private->iob
.bio_list
= dreq
->bio
;
185 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
187 cqr
->startclk
= get_tod_clock();
188 cqr
->starttime
= jiffies
;
191 rc
= dia250(&private->iob
, RW_BIO
);
193 case 0: /* Synchronous I/O finished successfully */
194 cqr
->stopclk
= get_tod_clock();
195 cqr
->status
= DASD_CQR_SUCCESS
;
196 /* Indicate to calling function that only a dasd_schedule_bh()
197 and no timer is needed */
200 case 8: /* Asynchronous I/O was started */
201 cqr
->status
= DASD_CQR_IN_IO
;
204 default: /* Error condition */
205 cqr
->status
= DASD_CQR_QUEUED
;
206 DBF_DEV_EVENT(DBF_WARNING
, device
, "dia250 returned rc=%d", rc
);
207 dasd_diag_erp(device
);
215 /* Terminate given request at the device. */
217 dasd_diag_term_IO(struct dasd_ccw_req
* cqr
)
219 struct dasd_device
*device
;
221 device
= cqr
->startdev
;
222 mdsk_term_io(device
);
223 mdsk_init_io(device
, device
->block
->bp_block
, 0, NULL
);
224 cqr
->status
= DASD_CQR_CLEAR_PENDING
;
225 cqr
->stopclk
= get_tod_clock();
226 dasd_schedule_device_bh(device
);
230 /* Handle external interruption. */
231 static void dasd_ext_handler(struct ext_code ext_code
,
232 unsigned int param32
, unsigned long param64
)
234 struct dasd_ccw_req
*cqr
, *next
;
235 struct dasd_device
*device
;
236 unsigned long long expires
;
241 switch (ext_code
.subcode
>> 8) {
242 case DASD_DIAG_CODE_31BIT
:
243 ip
= (addr_t
) param32
;
245 case DASD_DIAG_CODE_64BIT
:
246 ip
= (addr_t
) param64
;
251 inc_irq_stat(IRQEXT_DSD
);
252 if (!ip
) { /* no intparm: unsolicited interrupt */
253 DBF_EVENT(DBF_NOTICE
, "%s", "caught unsolicited "
257 cqr
= (struct dasd_ccw_req
*) ip
;
258 device
= (struct dasd_device
*) cqr
->startdev
;
259 if (strncmp(device
->discipline
->ebcname
, (char *) &cqr
->magic
, 4)) {
260 DBF_DEV_EVENT(DBF_WARNING
, device
,
261 " magic number of dasd_ccw_req 0x%08X doesn't"
262 " match discipline 0x%08X",
263 cqr
->magic
, *(int *) (&device
->discipline
->name
));
267 /* get irq lock to modify request queue */
268 spin_lock_irqsave(get_ccwdev_lock(device
->cdev
), flags
);
270 /* Check for a pending clear operation */
271 if (cqr
->status
== DASD_CQR_CLEAR_PENDING
) {
272 cqr
->status
= DASD_CQR_CLEARED
;
273 dasd_device_clear_timer(device
);
274 dasd_schedule_device_bh(device
);
275 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
279 cqr
->stopclk
= get_tod_clock();
282 if ((ext_code
.subcode
& 0xff) == 0) {
283 cqr
->status
= DASD_CQR_SUCCESS
;
284 /* Start first request on queue if possible -> fast_io. */
285 if (!list_empty(&device
->ccw_queue
)) {
286 next
= list_entry(device
->ccw_queue
.next
,
287 struct dasd_ccw_req
, devlist
);
288 if (next
->status
== DASD_CQR_QUEUED
) {
289 rc
= dasd_start_diag(next
);
291 expires
= next
->expires
;
295 cqr
->status
= DASD_CQR_QUEUED
;
296 DBF_DEV_EVENT(DBF_DEBUG
, device
, "interrupt status for "
297 "request %p was %d (%d retries left)", cqr
,
298 ext_code
.subcode
& 0xff, cqr
->retries
);
299 dasd_diag_erp(device
);
303 dasd_device_set_timer(device
, expires
);
305 dasd_device_clear_timer(device
);
306 dasd_schedule_device_bh(device
);
308 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
311 /* Check whether device can be controlled by DIAG discipline. Return zero on
312 * success, non-zero otherwise. */
314 dasd_diag_check_device(struct dasd_device
*device
)
316 struct dasd_block
*block
;
317 struct dasd_diag_private
*private;
318 struct dasd_diag_characteristics
*rdc_data
;
319 struct dasd_diag_bio bio
;
320 struct vtoc_cms_label
*label
;
321 blocknum_t end_block
;
322 unsigned int sb
, bsize
;
325 private = (struct dasd_diag_private
*) device
->private;
326 if (private == NULL
) {
327 private = kzalloc(sizeof(struct dasd_diag_private
),GFP_KERNEL
);
328 if (private == NULL
) {
329 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
330 "Allocating memory for private DASD data "
334 ccw_device_get_id(device
->cdev
, &private->dev_id
);
335 device
->private = (void *) private;
337 block
= dasd_alloc_block();
339 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
340 "could not allocate dasd block structure");
341 device
->private = NULL
;
343 return PTR_ERR(block
);
345 device
->block
= block
;
346 block
->base
= device
;
348 /* Read Device Characteristics */
349 rdc_data
= (void *) &(private->rdc_data
);
350 rdc_data
->dev_nr
= private->dev_id
.devno
;
351 rdc_data
->rdc_len
= sizeof (struct dasd_diag_characteristics
);
353 rc
= diag210((struct diag210
*) rdc_data
);
355 DBF_DEV_EVENT(DBF_WARNING
, device
, "failed to retrieve device "
356 "information (rc=%d)", rc
);
361 device
->default_expires
= DIAG_TIMEOUT
;
362 device
->default_retries
= DIAG_MAX_RETRIES
;
364 /* Figure out position of label block */
365 switch (private->rdc_data
.vdev_class
) {
367 private->pt_block
= 1;
370 private->pt_block
= 2;
373 pr_warning("%s: Device type %d is not supported "
374 "in DIAG mode\n", dev_name(&device
->cdev
->dev
),
375 private->rdc_data
.vdev_class
);
380 DBF_DEV_EVENT(DBF_INFO
, device
,
381 "%04X: %04X on real %04X/%02X",
384 rdc_data
->rdev_type
, rdc_data
->rdev_model
);
386 /* terminate all outstanding operations */
387 mdsk_term_io(device
);
389 /* figure out blocksize of device */
390 label
= (struct vtoc_cms_label
*) get_zeroed_page(GFP_KERNEL
);
392 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
393 "No memory to allocate initialization request");
399 /* try all sizes - needed for ECKD devices */
400 for (bsize
= 512; bsize
<= PAGE_SIZE
; bsize
<<= 1) {
401 mdsk_init_io(device
, bsize
, 0, &end_block
);
402 memset(&bio
, 0, sizeof (struct dasd_diag_bio
));
403 bio
.type
= MDSK_READ_REQ
;
404 bio
.block_number
= private->pt_block
+ 1;
406 memset(&private->iob
, 0, sizeof (struct dasd_diag_rw_io
));
407 private->iob
.dev_nr
= rdc_data
->dev_nr
;
408 private->iob
.key
= 0;
409 private->iob
.flags
= 0; /* do synchronous io */
410 private->iob
.block_count
= 1;
411 private->iob
.interrupt_params
= 0;
412 private->iob
.bio_list
= &bio
;
413 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
414 rc
= dia250(&private->iob
, RW_BIO
);
416 pr_warning("%s: A 64-bit DIAG call failed\n",
417 dev_name(&device
->cdev
->dev
));
421 mdsk_term_io(device
);
425 if (bsize
> PAGE_SIZE
) {
426 pr_warning("%s: Accessing the DASD failed because of an "
427 "incorrect format (rc=%d)\n",
428 dev_name(&device
->cdev
->dev
), rc
);
432 /* check for label block */
433 if (memcmp(label
->label_id
, DASD_DIAG_CMS1
,
434 sizeof(DASD_DIAG_CMS1
)) == 0) {
435 /* get formatted blocksize from label block */
436 bsize
= (unsigned int) label
->block_size
;
437 block
->blocks
= (unsigned long) label
->block_count
;
439 block
->blocks
= end_block
;
440 block
->bp_block
= bsize
;
441 block
->s2b_shift
= 0; /* bits to shift 512 to get a block */
442 for (sb
= 512; sb
< bsize
; sb
= sb
<< 1)
444 rc
= mdsk_init_io(device
, block
->bp_block
, 0, NULL
);
445 if (rc
&& (rc
!= 4)) {
446 pr_warning("%s: DIAG initialization failed with rc=%d\n",
447 dev_name(&device
->cdev
->dev
), rc
);
451 set_bit(DASD_FLAG_DEVICE_RO
, &device
->flags
);
452 pr_info("%s: New DASD with %ld byte/block, total size %ld "
453 "KB%s\n", dev_name(&device
->cdev
->dev
),
454 (unsigned long) block
->bp_block
,
455 (unsigned long) (block
->blocks
<<
456 block
->s2b_shift
) >> 1,
457 (rc
== 4) ? ", read-only device" : "");
461 free_page((long) label
);
464 device
->block
= NULL
;
465 dasd_free_block(block
);
466 device
->private = NULL
;
472 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
475 dasd_diag_fill_geometry(struct dasd_block
*block
, struct hd_geometry
*geo
)
477 if (dasd_check_blocksize(block
->bp_block
) != 0)
479 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
481 geo
->sectors
= 128 >> block
->s2b_shift
;
486 dasd_diag_erp_action(struct dasd_ccw_req
* cqr
)
488 return dasd_default_erp_action
;
492 dasd_diag_erp_postaction(struct dasd_ccw_req
* cqr
)
494 return dasd_default_erp_postaction
;
497 /* Create DASD request from block device request. Return pointer to new
498 * request on success, ERR_PTR otherwise. */
499 static struct dasd_ccw_req
*dasd_diag_build_cp(struct dasd_device
*memdev
,
500 struct dasd_block
*block
,
503 struct dasd_ccw_req
*cqr
;
504 struct dasd_diag_req
*dreq
;
505 struct dasd_diag_bio
*dbio
;
506 struct req_iterator iter
;
509 unsigned int count
, datasize
;
510 sector_t recid
, first_rec
, last_rec
;
511 unsigned int blksize
, off
;
512 unsigned char rw_cmd
;
514 if (rq_data_dir(req
) == READ
)
515 rw_cmd
= MDSK_READ_REQ
;
516 else if (rq_data_dir(req
) == WRITE
)
517 rw_cmd
= MDSK_WRITE_REQ
;
519 return ERR_PTR(-EINVAL
);
520 blksize
= block
->bp_block
;
521 /* Calculate record id of first and last block. */
522 first_rec
= blk_rq_pos(req
) >> block
->s2b_shift
;
524 (blk_rq_pos(req
) + blk_rq_sectors(req
) - 1) >> block
->s2b_shift
;
525 /* Check struct bio and count the number of blocks for the request. */
527 rq_for_each_segment(bv
, req
, iter
) {
528 if (bv
.bv_len
& (blksize
- 1))
529 /* Fba can only do full blocks. */
530 return ERR_PTR(-EINVAL
);
531 count
+= bv
.bv_len
>> (block
->s2b_shift
+ 9);
534 if (count
!= last_rec
- first_rec
+ 1)
535 return ERR_PTR(-EINVAL
);
536 /* Build the request */
537 datasize
= sizeof(struct dasd_diag_req
) +
538 count
*sizeof(struct dasd_diag_bio
);
539 cqr
= dasd_smalloc_request(DASD_DIAG_MAGIC
, 0, datasize
, memdev
);
543 dreq
= (struct dasd_diag_req
*) cqr
->data
;
544 dreq
->block_count
= count
;
547 rq_for_each_segment(bv
, req
, iter
) {
548 dst
= page_address(bv
.bv_page
) + bv
.bv_offset
;
549 for (off
= 0; off
< bv
.bv_len
; off
+= blksize
) {
550 memset(dbio
, 0, sizeof (struct dasd_diag_bio
));
552 dbio
->block_number
= recid
+ 1;
559 cqr
->retries
= memdev
->default_retries
;
560 cqr
->buildclk
= get_tod_clock();
561 if (blk_noretry_request(req
) ||
562 block
->base
->features
& DASD_FEATURE_FAILFAST
)
563 set_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
);
564 cqr
->startdev
= memdev
;
565 cqr
->memdev
= memdev
;
567 cqr
->expires
= memdev
->default_expires
* HZ
;
568 cqr
->status
= DASD_CQR_FILLED
;
572 /* Release DASD request. Return non-zero if request was successful, zero
575 dasd_diag_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
579 status
= cqr
->status
== DASD_CQR_DONE
;
580 dasd_sfree_request(cqr
, cqr
->memdev
);
584 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req
*cqr
)
586 if (cqr
->retries
< 0)
587 cqr
->status
= DASD_CQR_FAILED
;
589 cqr
->status
= DASD_CQR_FILLED
;
592 /* Fill in IOCTL data for device. */
594 dasd_diag_fill_info(struct dasd_device
* device
,
595 struct dasd_information2_t
* info
)
597 struct dasd_diag_private
*private;
599 private = (struct dasd_diag_private
*) device
->private;
600 info
->label_block
= (unsigned int) private->pt_block
;
601 info
->FBA_layout
= 1;
602 info
->format
= DASD_FORMAT_LDL
;
603 info
->characteristics_size
= sizeof (struct dasd_diag_characteristics
);
604 memcpy(info
->characteristics
,
605 &((struct dasd_diag_private
*) device
->private)->rdc_data
,
606 sizeof (struct dasd_diag_characteristics
));
607 info
->confdata_size
= 0;
612 dasd_diag_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
615 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
616 "dump sense not available for DIAG data");
619 static struct dasd_discipline dasd_diag_discipline
= {
620 .owner
= THIS_MODULE
,
623 .max_blocks
= DIAG_MAX_BLOCKS
,
624 .check_device
= dasd_diag_check_device
,
625 .verify_path
= dasd_generic_verify_path
,
626 .fill_geometry
= dasd_diag_fill_geometry
,
627 .start_IO
= dasd_start_diag
,
628 .term_IO
= dasd_diag_term_IO
,
629 .handle_terminated_request
= dasd_diag_handle_terminated_request
,
630 .erp_action
= dasd_diag_erp_action
,
631 .erp_postaction
= dasd_diag_erp_postaction
,
632 .build_cp
= dasd_diag_build_cp
,
633 .free_cp
= dasd_diag_free_cp
,
634 .dump_sense
= dasd_diag_dump_sense
,
635 .fill_info
= dasd_diag_fill_info
,
641 if (!MACHINE_IS_VM
) {
642 pr_info("Discipline %s cannot be used without z/VM\n",
643 dasd_diag_discipline
.name
);
646 ASCEBC(dasd_diag_discipline
.ebcname
, 4);
648 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL
);
649 register_external_irq(EXT_IRQ_CP_SERVICE
, dasd_ext_handler
);
650 dasd_diag_discipline_pointer
= &dasd_diag_discipline
;
655 dasd_diag_cleanup(void)
657 unregister_external_irq(EXT_IRQ_CP_SERVICE
, dasd_ext_handler
);
658 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL
);
659 dasd_diag_discipline_pointer
= NULL
;
662 module_init(dasd_diag_init
);
663 module_exit(dasd_diag_cleanup
);