2 * File...........: linux/drivers/s390/block/dasd_diag.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Based on.......: linux/drivers/s390/block/mdisk.c
5 * ...............: by Hartmunt Penner <hpenner@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
7 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999,2000
11 #include <linux/stddef.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/hdreg.h>
15 #include <linux/bio.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/jiffies.h>
21 #include <asm/debug.h>
22 #include <asm/ebcdic.h>
24 #include <asm/s390_ext.h>
25 #include <asm/todclk.h>
29 #include "dasd_diag.h"
31 #define PRINTK_HEADER "dasd(diag):"
33 MODULE_LICENSE("GPL");
35 /* The maximum number of blocks per request (max_blocks) is dependent on the
36 * amount of storage that is available in the static I/O buffer for each
37 * device. Currently each device gets 2 pages. We want to fit two requests
38 * into the available memory so that we can immediately start the next if one
40 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
41 sizeof(struct dasd_diag_req)) / \
42 sizeof(struct dasd_diag_bio)) / 2)
43 #define DIAG_MAX_RETRIES 32
44 #define DIAG_TIMEOUT 50 * HZ
46 static struct dasd_discipline dasd_diag_discipline
;
48 struct dasd_diag_private
{
49 struct dasd_diag_characteristics rdc_data
;
50 struct dasd_diag_rw_io iob
;
51 struct dasd_diag_init_io iib
;
53 struct ccw_dev_id dev_id
;
56 struct dasd_diag_req
{
57 unsigned int block_count
;
58 struct dasd_diag_bio bio
[0];
61 static const u8 DASD_DIAG_CMS1
[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
63 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
64 * and function code cmd.
65 * In case of an exception return 3. Otherwise return result of bitwise OR of
66 * resulting condition code and DIAG return code. */
67 static inline int dia250(void *iob
, int cmd
)
69 register unsigned long reg2
asm ("2") = (unsigned long) iob
;
71 struct dasd_diag_init_io init_io
;
72 struct dasd_diag_rw_io rw_io
;
84 : "+d" (rc
), "=m" (*(addr_type
*) iob
)
85 : "d" (cmd
), "d" (reg2
), "m" (*(addr_type
*) iob
)
90 /* Initialize block I/O to DIAG device using the specified blocksize and
91 * block offset. On success, return zero and set end_block to contain the
92 * number of blocks on the device minus the specified offset. Return non-zero
95 mdsk_init_io(struct dasd_device
*device
, unsigned int blocksize
,
96 blocknum_t offset
, blocknum_t
*end_block
)
98 struct dasd_diag_private
*private;
99 struct dasd_diag_init_io
*iib
;
102 private = (struct dasd_diag_private
*) device
->private;
104 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
106 iib
->dev_nr
= private->dev_id
.devno
;
107 iib
->block_size
= blocksize
;
108 iib
->offset
= offset
;
109 iib
->flaga
= DASD_DIAG_FLAGA_DEFAULT
;
111 rc
= dia250(iib
, INIT_BIO
);
113 if ((rc
& 3) == 0 && end_block
)
114 *end_block
= iib
->end_block
;
119 /* Remove block I/O environment for device. Return zero on success, non-zero
122 mdsk_term_io(struct dasd_device
* device
)
124 struct dasd_diag_private
*private;
125 struct dasd_diag_init_io
*iib
;
128 private = (struct dasd_diag_private
*) device
->private;
130 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
131 iib
->dev_nr
= private->dev_id
.devno
;
132 rc
= dia250(iib
, TERM_BIO
);
136 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
139 dasd_diag_erp(struct dasd_device
*device
)
143 mdsk_term_io(device
);
144 rc
= mdsk_init_io(device
, device
->bp_block
, 0, NULL
);
146 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG ERP unsuccessful, "
150 /* Start a given request at the device. Return zero on success, non-zero
153 dasd_start_diag(struct dasd_ccw_req
* cqr
)
155 struct dasd_device
*device
;
156 struct dasd_diag_private
*private;
157 struct dasd_diag_req
*dreq
;
160 device
= cqr
->device
;
161 if (cqr
->retries
< 0) {
162 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG start_IO: request %p "
163 "- no retry left)", cqr
);
164 cqr
->status
= DASD_CQR_FAILED
;
167 private = (struct dasd_diag_private
*) device
->private;
168 dreq
= (struct dasd_diag_req
*) cqr
->data
;
170 private->iob
.dev_nr
= private->dev_id
.devno
;
171 private->iob
.key
= 0;
172 private->iob
.flags
= DASD_DIAG_RWFLAG_ASYNC
;
173 private->iob
.block_count
= dreq
->block_count
;
174 private->iob
.interrupt_params
= (addr_t
) cqr
;
175 private->iob
.bio_list
= dreq
->bio
;
176 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
178 cqr
->startclk
= get_clock();
179 cqr
->starttime
= jiffies
;
182 rc
= dia250(&private->iob
, RW_BIO
);
184 case 0: /* Synchronous I/O finished successfully */
185 cqr
->stopclk
= get_clock();
186 cqr
->status
= DASD_CQR_DONE
;
187 /* Indicate to calling function that only a dasd_schedule_bh()
188 and no timer is needed */
191 case 8: /* Asynchronous I/O was started */
192 cqr
->status
= DASD_CQR_IN_IO
;
195 default: /* Error condition */
196 cqr
->status
= DASD_CQR_QUEUED
;
197 DEV_MESSAGE(KERN_WARNING
, device
, "dia250 returned rc=%d", rc
);
198 dasd_diag_erp(device
);
205 /* Terminate given request at the device. */
207 dasd_diag_term_IO(struct dasd_ccw_req
* cqr
)
209 struct dasd_device
*device
;
211 device
= cqr
->device
;
212 mdsk_term_io(device
);
213 mdsk_init_io(device
, device
->bp_block
, 0, NULL
);
214 cqr
->status
= DASD_CQR_CLEAR
;
215 cqr
->stopclk
= get_clock();
216 dasd_schedule_bh(device
);
220 /* Handle external interruption. */
222 dasd_ext_handler(__u16 code
)
224 struct dasd_ccw_req
*cqr
, *next
;
225 struct dasd_device
*device
;
226 unsigned long long expires
;
232 int_code
= *((u8
*) DASD_DIAG_LC_INT_CODE
);
233 status
= *((u8
*) DASD_DIAG_LC_INT_STATUS
);
235 case DASD_DIAG_CODE_31BIT
:
236 ip
= (addr_t
) *((u32
*) DASD_DIAG_LC_INT_PARM_31BIT
);
238 case DASD_DIAG_CODE_64BIT
:
239 ip
= (addr_t
) *((u64
*) DASD_DIAG_LC_INT_PARM_64BIT
);
244 if (!ip
) { /* no intparm: unsolicited interrupt */
245 MESSAGE(KERN_DEBUG
, "%s", "caught unsolicited interrupt");
248 cqr
= (struct dasd_ccw_req
*) ip
;
249 device
= (struct dasd_device
*) cqr
->device
;
250 if (strncmp(device
->discipline
->ebcname
, (char *) &cqr
->magic
, 4)) {
251 DEV_MESSAGE(KERN_WARNING
, device
,
252 " magic number of dasd_ccw_req 0x%08X doesn't"
253 " match discipline 0x%08X",
254 cqr
->magic
, *(int *) (&device
->discipline
->name
));
258 /* get irq lock to modify request queue */
259 spin_lock_irqsave(get_ccwdev_lock(device
->cdev
), flags
);
261 /* Check for a pending clear operation */
262 if (cqr
->status
== DASD_CQR_CLEAR
) {
263 cqr
->status
= DASD_CQR_QUEUED
;
264 dasd_clear_timer(device
);
265 dasd_schedule_bh(device
);
266 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
270 cqr
->stopclk
= get_clock();
274 cqr
->status
= DASD_CQR_DONE
;
275 /* Start first request on queue if possible -> fast_io. */
276 if (!list_empty(&device
->ccw_queue
)) {
277 next
= list_entry(device
->ccw_queue
.next
,
278 struct dasd_ccw_req
, list
);
279 if (next
->status
== DASD_CQR_QUEUED
) {
280 rc
= dasd_start_diag(next
);
282 expires
= next
->expires
;
283 else if (rc
!= -EACCES
)
284 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
285 "Interrupt fastpath "
290 cqr
->status
= DASD_CQR_QUEUED
;
291 DEV_MESSAGE(KERN_WARNING
, device
, "interrupt status for "
292 "request %p was %d (%d retries left)", cqr
, status
,
294 dasd_diag_erp(device
);
298 dasd_set_timer(device
, expires
);
300 dasd_clear_timer(device
);
301 dasd_schedule_bh(device
);
303 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
306 /* Check whether device can be controlled by DIAG discipline. Return zero on
307 * success, non-zero otherwise. */
309 dasd_diag_check_device(struct dasd_device
*device
)
311 struct dasd_diag_private
*private;
312 struct dasd_diag_characteristics
*rdc_data
;
313 struct dasd_diag_bio bio
;
314 struct vtoc_cms_label
*label
;
315 blocknum_t end_block
;
316 unsigned int sb
, bsize
;
319 private = (struct dasd_diag_private
*) device
->private;
320 if (private == NULL
) {
321 private = kzalloc(sizeof(struct dasd_diag_private
),GFP_KERNEL
);
322 if (private == NULL
) {
323 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
324 "memory allocation failed for private data");
327 ccw_device_get_id(device
->cdev
, &private->dev_id
);
328 device
->private = (void *) private;
330 /* Read Device Characteristics */
331 rdc_data
= (void *) &(private->rdc_data
);
332 rdc_data
->dev_nr
= private->dev_id
.devno
;
333 rdc_data
->rdc_len
= sizeof (struct dasd_diag_characteristics
);
335 rc
= diag210((struct diag210
*) rdc_data
);
337 DEV_MESSAGE(KERN_WARNING
, device
, "failed to retrieve device "
338 "information (rc=%d)", rc
);
342 /* Figure out position of label block */
343 switch (private->rdc_data
.vdev_class
) {
345 private->pt_block
= 1;
348 private->pt_block
= 2;
351 DEV_MESSAGE(KERN_WARNING
, device
, "unsupported device class "
352 "(class=%d)", private->rdc_data
.vdev_class
);
356 DBF_DEV_EVENT(DBF_INFO
, device
,
357 "%04X: %04X on real %04X/%02X",
360 rdc_data
->rdev_type
, rdc_data
->rdev_model
);
362 /* terminate all outstanding operations */
363 mdsk_term_io(device
);
365 /* figure out blocksize of device */
366 label
= (struct vtoc_cms_label
*) get_zeroed_page(GFP_KERNEL
);
368 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
369 "No memory to allocate initialization request");
374 /* try all sizes - needed for ECKD devices */
375 for (bsize
= 512; bsize
<= PAGE_SIZE
; bsize
<<= 1) {
376 mdsk_init_io(device
, bsize
, 0, &end_block
);
377 memset(&bio
, 0, sizeof (struct dasd_diag_bio
));
378 bio
.type
= MDSK_READ_REQ
;
379 bio
.block_number
= private->pt_block
+ 1;
381 memset(&private->iob
, 0, sizeof (struct dasd_diag_rw_io
));
382 private->iob
.dev_nr
= rdc_data
->dev_nr
;
383 private->iob
.key
= 0;
384 private->iob
.flags
= 0; /* do synchronous io */
385 private->iob
.block_count
= 1;
386 private->iob
.interrupt_params
= 0;
387 private->iob
.bio_list
= &bio
;
388 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
389 rc
= dia250(&private->iob
, RW_BIO
);
391 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
396 mdsk_term_io(device
);
400 if (bsize
> PAGE_SIZE
) {
401 DEV_MESSAGE(KERN_WARNING
, device
, "device access failed "
406 /* check for label block */
407 if (memcmp(label
->label_id
, DASD_DIAG_CMS1
,
408 sizeof(DASD_DIAG_CMS1
)) == 0) {
409 /* get formatted blocksize from label block */
410 bsize
= (unsigned int) label
->block_size
;
411 device
->blocks
= (unsigned long) label
->block_count
;
413 device
->blocks
= end_block
;
414 device
->bp_block
= bsize
;
415 device
->s2b_shift
= 0; /* bits to shift 512 to get a block */
416 for (sb
= 512; sb
< bsize
; sb
= sb
<< 1)
418 rc
= mdsk_init_io(device
, device
->bp_block
, 0, NULL
);
420 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG initialization "
421 "failed (rc=%d)", rc
);
424 DEV_MESSAGE(KERN_INFO
, device
,
425 "(%ld B/blk): %ldkB",
426 (unsigned long) device
->bp_block
,
427 (unsigned long) (device
->blocks
<<
428 device
->s2b_shift
) >> 1);
431 free_page((long) label
);
435 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
438 dasd_diag_fill_geometry(struct dasd_device
*device
, struct hd_geometry
*geo
)
440 if (dasd_check_blocksize(device
->bp_block
) != 0)
442 geo
->cylinders
= (device
->blocks
<< device
->s2b_shift
) >> 10;
444 geo
->sectors
= 128 >> device
->s2b_shift
;
449 dasd_diag_examine_error(struct dasd_ccw_req
* cqr
, struct irb
* stat
)
451 return dasd_era_fatal
;
455 dasd_diag_erp_action(struct dasd_ccw_req
* cqr
)
457 return dasd_default_erp_action
;
461 dasd_diag_erp_postaction(struct dasd_ccw_req
* cqr
)
463 return dasd_default_erp_postaction
;
466 /* Create DASD request from block device request. Return pointer to new
467 * request on success, ERR_PTR otherwise. */
468 static struct dasd_ccw_req
*
469 dasd_diag_build_cp(struct dasd_device
* device
, struct request
*req
)
471 struct dasd_ccw_req
*cqr
;
472 struct dasd_diag_req
*dreq
;
473 struct dasd_diag_bio
*dbio
;
477 unsigned int count
, datasize
;
478 sector_t recid
, first_rec
, last_rec
;
479 unsigned int blksize
, off
;
480 unsigned char rw_cmd
;
483 if (rq_data_dir(req
) == READ
)
484 rw_cmd
= MDSK_READ_REQ
;
485 else if (rq_data_dir(req
) == WRITE
)
486 rw_cmd
= MDSK_WRITE_REQ
;
488 return ERR_PTR(-EINVAL
);
489 blksize
= device
->bp_block
;
490 /* Calculate record id of first and last block. */
491 first_rec
= req
->sector
>> device
->s2b_shift
;
492 last_rec
= (req
->sector
+ req
->nr_sectors
- 1) >> device
->s2b_shift
;
493 /* Check struct bio and count the number of blocks for the request. */
495 rq_for_each_bio(bio
, req
) {
496 bio_for_each_segment(bv
, bio
, i
) {
497 if (bv
->bv_len
& (blksize
- 1))
498 /* Fba can only do full blocks. */
499 return ERR_PTR(-EINVAL
);
500 count
+= bv
->bv_len
>> (device
->s2b_shift
+ 9);
504 if (count
!= last_rec
- first_rec
+ 1)
505 return ERR_PTR(-EINVAL
);
506 /* Build the request */
507 datasize
= sizeof(struct dasd_diag_req
) +
508 count
*sizeof(struct dasd_diag_bio
);
509 cqr
= dasd_smalloc_request(dasd_diag_discipline
.name
, 0,
514 dreq
= (struct dasd_diag_req
*) cqr
->data
;
515 dreq
->block_count
= count
;
518 rq_for_each_bio(bio
, req
) {
519 bio_for_each_segment(bv
, bio
, i
) {
520 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
521 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
522 memset(dbio
, 0, sizeof (struct dasd_diag_bio
));
524 dbio
->block_number
= recid
+ 1;
532 cqr
->retries
= DIAG_MAX_RETRIES
;
533 cqr
->buildclk
= get_clock();
534 if (req
->cmd_flags
& REQ_FAILFAST
)
535 set_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
);
536 cqr
->device
= device
;
537 cqr
->expires
= DIAG_TIMEOUT
;
538 cqr
->status
= DASD_CQR_FILLED
;
542 /* Release DASD request. Return non-zero if request was successful, zero
545 dasd_diag_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
549 status
= cqr
->status
== DASD_CQR_DONE
;
550 dasd_sfree_request(cqr
, cqr
->device
);
554 /* Fill in IOCTL data for device. */
556 dasd_diag_fill_info(struct dasd_device
* device
,
557 struct dasd_information2_t
* info
)
559 struct dasd_diag_private
*private;
561 private = (struct dasd_diag_private
*) device
->private;
562 info
->label_block
= (unsigned int) private->pt_block
;
563 info
->FBA_layout
= 1;
564 info
->format
= DASD_FORMAT_LDL
;
565 info
->characteristics_size
= sizeof (struct dasd_diag_characteristics
);
566 memcpy(info
->characteristics
,
567 &((struct dasd_diag_private
*) device
->private)->rdc_data
,
568 sizeof (struct dasd_diag_characteristics
));
569 info
->confdata_size
= 0;
574 dasd_diag_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
577 DEV_MESSAGE(KERN_ERR
, device
, "%s",
578 "dump sense not available for DIAG data");
581 static struct dasd_discipline dasd_diag_discipline
= {
582 .owner
= THIS_MODULE
,
585 .max_blocks
= DIAG_MAX_BLOCKS
,
586 .check_device
= dasd_diag_check_device
,
587 .fill_geometry
= dasd_diag_fill_geometry
,
588 .start_IO
= dasd_start_diag
,
589 .term_IO
= dasd_diag_term_IO
,
590 .examine_error
= dasd_diag_examine_error
,
591 .erp_action
= dasd_diag_erp_action
,
592 .erp_postaction
= dasd_diag_erp_postaction
,
593 .build_cp
= dasd_diag_build_cp
,
594 .free_cp
= dasd_diag_free_cp
,
595 .dump_sense
= dasd_diag_dump_sense
,
596 .fill_info
= dasd_diag_fill_info
,
602 if (!MACHINE_IS_VM
) {
603 MESSAGE_LOG(KERN_INFO
,
604 "Machine is not VM: %s "
605 "discipline not initializing",
606 dasd_diag_discipline
.name
);
609 ASCEBC(dasd_diag_discipline
.ebcname
, 4);
612 register_external_interrupt(0x2603, dasd_ext_handler
);
613 dasd_diag_discipline_pointer
= &dasd_diag_discipline
;
618 dasd_diag_cleanup(void)
620 unregister_external_interrupt(0x2603, dasd_ext_handler
);
622 dasd_diag_discipline_pointer
= NULL
;
625 module_init(dasd_diag_init
);
626 module_exit(dasd_diag_cleanup
);