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
12 #include <linux/config.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>
26 #include <asm/s390_ext.h>
27 #include <asm/todclk.h>
30 #include "dasd_diag.h"
32 #define PRINTK_HEADER "dasd(diag):"
34 MODULE_LICENSE("GPL");
36 /* The maximum number of blocks per request (max_blocks) is dependent on the
37 * amount of storage that is available in the static I/O buffer for each
38 * device. Currently each device gets 2 pages. We want to fit two requests
39 * into the available memory so that we can immediately start the next if one
41 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
42 sizeof(struct dasd_diag_req)) / \
43 sizeof(struct dasd_diag_bio)) / 2)
44 #define DIAG_MAX_RETRIES 32
45 #define DIAG_TIMEOUT 50 * HZ
47 struct dasd_discipline dasd_diag_discipline
;
49 struct dasd_diag_private
{
50 struct dasd_diag_characteristics rdc_data
;
51 struct dasd_diag_rw_io iob
;
52 struct dasd_diag_init_io iib
;
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. */
68 dia250(void *iob
, int cmd
)
71 char _
[max(sizeof (struct dasd_diag_init_io
),
72 sizeof (struct dasd_diag_rw_io
))];
77 #ifdef CONFIG_ARCH_S390X
85 ".section __ex_table,\"a\"\n"
97 ".section __ex_table,\"a\"\n"
102 : "=&d" (rc
), "=m" (*(addr_type
*) iob
)
103 : "d" (cmd
), "d" (iob
), "m" (*(addr_type
*) iob
)
108 /* Initialize block I/O to DIAG device using the specified blocksize and
109 * block offset. On success, return zero and set end_block to contain the
110 * number of blocks on the device minus the specified offset. Return non-zero
112 static __inline__
int
113 mdsk_init_io(struct dasd_device
*device
, unsigned int blocksize
,
114 blocknum_t offset
, blocknum_t
*end_block
)
116 struct dasd_diag_private
*private;
117 struct dasd_diag_init_io
*iib
;
120 private = (struct dasd_diag_private
*) device
->private;
122 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
124 iib
->dev_nr
= _ccw_device_get_device_number(device
->cdev
);
125 iib
->block_size
= blocksize
;
126 iib
->offset
= offset
;
127 iib
->flaga
= DASD_DIAG_FLAGA_DEFAULT
;
129 rc
= dia250(iib
, INIT_BIO
);
131 if ((rc
& 3) == 0 && end_block
)
132 *end_block
= iib
->end_block
;
137 /* Remove block I/O environment for device. Return zero on success, non-zero
139 static __inline__
int
140 mdsk_term_io(struct dasd_device
* device
)
142 struct dasd_diag_private
*private;
143 struct dasd_diag_init_io
*iib
;
146 private = (struct dasd_diag_private
*) device
->private;
148 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
149 iib
->dev_nr
= _ccw_device_get_device_number(device
->cdev
);
150 rc
= dia250(iib
, TERM_BIO
);
154 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
157 dasd_diag_erp(struct dasd_device
*device
)
161 mdsk_term_io(device
);
162 rc
= mdsk_init_io(device
, device
->bp_block
, 0, NULL
);
164 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG ERP unsuccessful, "
168 /* Start a given request at the device. Return zero on success, non-zero
171 dasd_start_diag(struct dasd_ccw_req
* cqr
)
173 struct dasd_device
*device
;
174 struct dasd_diag_private
*private;
175 struct dasd_diag_req
*dreq
;
178 device
= cqr
->device
;
179 if (cqr
->retries
< 0) {
180 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG start_IO: request %p "
181 "- no retry left)", cqr
);
182 cqr
->status
= DASD_CQR_FAILED
;
185 private = (struct dasd_diag_private
*) device
->private;
186 dreq
= (struct dasd_diag_req
*) cqr
->data
;
188 private->iob
.dev_nr
= _ccw_device_get_device_number(device
->cdev
);
189 private->iob
.key
= 0;
190 private->iob
.flags
= DASD_DIAG_RWFLAG_ASYNC
;
191 private->iob
.block_count
= dreq
->block_count
;
192 private->iob
.interrupt_params
= (addr_t
) cqr
;
193 private->iob
.bio_list
= __pa(dreq
->bio
);
194 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
196 cqr
->startclk
= get_clock();
197 cqr
->starttime
= jiffies
;
200 rc
= dia250(&private->iob
, RW_BIO
);
202 case 0: /* Synchronous I/O finished successfully */
203 cqr
->stopclk
= get_clock();
204 cqr
->status
= DASD_CQR_DONE
;
205 /* Indicate to calling function that only a dasd_schedule_bh()
206 and no timer is needed */
209 case 8: /* Asynchronous I/O was started */
210 cqr
->status
= DASD_CQR_IN_IO
;
213 default: /* Error condition */
214 cqr
->status
= DASD_CQR_QUEUED
;
215 DEV_MESSAGE(KERN_WARNING
, device
, "dia250 returned rc=%d", rc
);
216 dasd_diag_erp(device
);
223 /* Terminate given request at the device. */
225 dasd_diag_term_IO(struct dasd_ccw_req
* cqr
)
227 struct dasd_device
*device
;
229 device
= cqr
->device
;
230 mdsk_term_io(device
);
231 mdsk_init_io(device
, device
->bp_block
, 0, NULL
);
232 cqr
->status
= DASD_CQR_CLEAR
;
233 cqr
->stopclk
= get_clock();
234 dasd_schedule_bh(device
);
238 /* Handle external interruption. */
240 dasd_ext_handler(struct pt_regs
*regs
, __u16 code
)
242 struct dasd_ccw_req
*cqr
, *next
;
243 struct dasd_device
*device
;
244 unsigned long long expires
;
250 int_code
= *((u8
*) DASD_DIAG_LC_INT_CODE
);
251 status
= *((u8
*) DASD_DIAG_LC_INT_STATUS
);
253 case DASD_DIAG_CODE_31BIT
:
254 ip
= (addr_t
) *((u32
*) DASD_DIAG_LC_INT_PARM_31BIT
);
256 case DASD_DIAG_CODE_64BIT
:
257 ip
= (addr_t
) *((u64
*) DASD_DIAG_LC_INT_PARM_64BIT
);
262 if (!ip
) { /* no intparm: unsolicited interrupt */
263 MESSAGE(KERN_DEBUG
, "%s", "caught unsolicited interrupt");
266 cqr
= (struct dasd_ccw_req
*) ip
;
267 device
= (struct dasd_device
*) cqr
->device
;
268 if (strncmp(device
->discipline
->ebcname
, (char *) &cqr
->magic
, 4)) {
269 DEV_MESSAGE(KERN_WARNING
, device
,
270 " magic number of dasd_ccw_req 0x%08X doesn't"
271 " match discipline 0x%08X",
272 cqr
->magic
, *(int *) (&device
->discipline
->name
));
276 /* get irq lock to modify request queue */
277 spin_lock_irqsave(get_ccwdev_lock(device
->cdev
), flags
);
279 /* Check for a pending clear operation */
280 if (cqr
->status
== DASD_CQR_CLEAR
) {
281 cqr
->status
= DASD_CQR_QUEUED
;
282 dasd_clear_timer(device
);
283 dasd_schedule_bh(device
);
284 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
288 cqr
->stopclk
= get_clock();
292 cqr
->status
= DASD_CQR_DONE
;
293 /* Start first request on queue if possible -> fast_io. */
294 if (!list_empty(&device
->ccw_queue
)) {
295 next
= list_entry(device
->ccw_queue
.next
,
296 struct dasd_ccw_req
, list
);
297 if (next
->status
== DASD_CQR_QUEUED
) {
298 rc
= dasd_start_diag(next
);
300 expires
= next
->expires
;
301 else if (rc
!= -EACCES
)
302 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
303 "Interrupt fastpath "
308 cqr
->status
= DASD_CQR_QUEUED
;
309 DEV_MESSAGE(KERN_WARNING
, device
, "interrupt status for "
310 "request %p was %d (%d retries left)", cqr
, status
,
312 dasd_diag_erp(device
);
316 dasd_set_timer(device
, expires
);
318 dasd_clear_timer(device
);
319 dasd_schedule_bh(device
);
321 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
324 /* Check whether device can be controlled by DIAG discipline. Return zero on
325 * success, non-zero otherwise. */
327 dasd_diag_check_device(struct dasd_device
*device
)
329 struct dasd_diag_private
*private;
330 struct dasd_diag_characteristics
*rdc_data
;
331 struct dasd_diag_bio bio
;
332 struct dasd_diag_cms_label
*label
;
333 blocknum_t end_block
;
334 unsigned int sb
, bsize
;
337 private = (struct dasd_diag_private
*) device
->private;
338 if (private == NULL
) {
339 private = kmalloc(sizeof(struct dasd_diag_private
),GFP_KERNEL
);
340 if (private == NULL
) {
341 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
342 "memory allocation failed for private data");
345 device
->private = (void *) private;
347 /* Read Device Characteristics */
348 rdc_data
= (void *) &(private->rdc_data
);
349 rdc_data
->dev_nr
= _ccw_device_get_device_number(device
->cdev
);
350 rdc_data
->rdc_len
= sizeof (struct dasd_diag_characteristics
);
352 rc
= diag210((struct diag210
*) rdc_data
);
354 DEV_MESSAGE(KERN_WARNING
, device
, "failed to retrieve device "
355 "information (rc=%d)", rc
);
359 /* Figure out position of label block */
360 switch (private->rdc_data
.vdev_class
) {
362 private->pt_block
= 1;
365 private->pt_block
= 2;
368 DEV_MESSAGE(KERN_WARNING
, device
, "unsupported device class "
369 "(class=%d)", private->rdc_data
.vdev_class
);
373 DBF_DEV_EVENT(DBF_INFO
, device
,
374 "%04X: %04X on real %04X/%02X",
377 rdc_data
->rdev_type
, rdc_data
->rdev_model
);
379 /* terminate all outstanding operations */
380 mdsk_term_io(device
);
382 /* figure out blocksize of device */
383 label
= (struct dasd_diag_cms_label
*) get_zeroed_page(GFP_KERNEL
);
385 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
386 "No memory to allocate initialization request");
391 /* try all sizes - needed for ECKD devices */
392 for (bsize
= 512; bsize
<= PAGE_SIZE
; bsize
<<= 1) {
393 mdsk_init_io(device
, bsize
, 0, &end_block
);
394 memset(&bio
, 0, sizeof (struct dasd_diag_bio
));
395 bio
.type
= MDSK_READ_REQ
;
396 bio
.block_number
= private->pt_block
+ 1;
397 bio
.buffer
= __pa(label
);
398 memset(&private->iob
, 0, sizeof (struct dasd_diag_rw_io
));
399 private->iob
.dev_nr
= rdc_data
->dev_nr
;
400 private->iob
.key
= 0;
401 private->iob
.flags
= 0; /* do synchronous io */
402 private->iob
.block_count
= 1;
403 private->iob
.interrupt_params
= 0;
404 private->iob
.bio_list
= __pa(&bio
);
405 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
406 rc
= dia250(&private->iob
, RW_BIO
);
407 if (rc
== 0 || rc
== 3)
409 mdsk_term_io(device
);
412 DEV_MESSAGE(KERN_WARNING
, device
, "%s", "DIAG call failed");
414 } else if (rc
!= 0) {
415 DEV_MESSAGE(KERN_WARNING
, device
, "device access failed "
419 if (memcmp(label
->label_id
, DASD_DIAG_CMS1
,
420 sizeof(DASD_DIAG_CMS1
)) == 0) {
421 /* get formatted blocksize from label block */
422 bsize
= (unsigned int) label
->block_size
;
423 device
->blocks
= (unsigned long) label
->block_count
;
425 device
->blocks
= end_block
;
426 device
->bp_block
= bsize
;
427 device
->s2b_shift
= 0; /* bits to shift 512 to get a block */
428 for (sb
= 512; sb
< bsize
; sb
= sb
<< 1)
431 DEV_MESSAGE(KERN_INFO
, device
,
432 "(%ld B/blk): %ldkB",
433 (unsigned long) device
->bp_block
,
434 (unsigned long) (device
->blocks
<<
435 device
->s2b_shift
) >> 1);
438 free_page((long) label
);
442 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
445 dasd_diag_fill_geometry(struct dasd_device
*device
, struct hd_geometry
*geo
)
447 if (dasd_check_blocksize(device
->bp_block
) != 0)
449 geo
->cylinders
= (device
->blocks
<< device
->s2b_shift
) >> 10;
451 geo
->sectors
= 128 >> device
->s2b_shift
;
456 dasd_diag_examine_error(struct dasd_ccw_req
* cqr
, struct irb
* stat
)
458 return dasd_era_fatal
;
462 dasd_diag_erp_action(struct dasd_ccw_req
* cqr
)
464 return dasd_default_erp_action
;
468 dasd_diag_erp_postaction(struct dasd_ccw_req
* cqr
)
470 return dasd_default_erp_postaction
;
473 /* Create DASD request from block device request. Return pointer to new
474 * request on success, ERR_PTR otherwise. */
475 static struct dasd_ccw_req
*
476 dasd_diag_build_cp(struct dasd_device
* device
, struct request
*req
)
478 struct dasd_ccw_req
*cqr
;
479 struct dasd_diag_req
*dreq
;
480 struct dasd_diag_bio
*dbio
;
484 unsigned int count
, datasize
;
485 sector_t recid
, first_rec
, last_rec
;
486 unsigned int blksize
, off
;
487 unsigned char rw_cmd
;
490 if (rq_data_dir(req
) == READ
)
491 rw_cmd
= MDSK_READ_REQ
;
492 else if (rq_data_dir(req
) == WRITE
)
493 rw_cmd
= MDSK_WRITE_REQ
;
495 return ERR_PTR(-EINVAL
);
496 blksize
= device
->bp_block
;
497 /* Calculate record id of first and last block. */
498 first_rec
= req
->sector
>> device
->s2b_shift
;
499 last_rec
= (req
->sector
+ req
->nr_sectors
- 1) >> device
->s2b_shift
;
500 /* Check struct bio and count the number of blocks for the request. */
502 rq_for_each_bio(bio
, req
) {
503 bio_for_each_segment(bv
, bio
, i
) {
504 if (bv
->bv_len
& (blksize
- 1))
505 /* Fba can only do full blocks. */
506 return ERR_PTR(-EINVAL
);
507 count
+= bv
->bv_len
>> (device
->s2b_shift
+ 9);
511 if (count
!= last_rec
- first_rec
+ 1)
512 return ERR_PTR(-EINVAL
);
513 /* Build the request */
514 datasize
= sizeof(struct dasd_diag_req
) +
515 count
*sizeof(struct dasd_diag_bio
);
516 cqr
= dasd_smalloc_request(dasd_diag_discipline
.name
, 0,
521 dreq
= (struct dasd_diag_req
*) cqr
->data
;
522 dreq
->block_count
= count
;
525 rq_for_each_bio(bio
, req
) {
526 bio_for_each_segment(bv
, bio
, i
) {
527 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
528 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
529 memset(dbio
, 0, sizeof (struct dasd_diag_bio
));
531 dbio
->block_number
= recid
+ 1;
532 dbio
->buffer
= __pa(dst
);
539 cqr
->retries
= DIAG_MAX_RETRIES
;
540 cqr
->buildclk
= get_clock();
541 cqr
->device
= device
;
542 cqr
->expires
= DIAG_TIMEOUT
;
543 cqr
->status
= DASD_CQR_FILLED
;
547 /* Release DASD request. Return non-zero if request was successful, zero
550 dasd_diag_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
554 status
= cqr
->status
== DASD_CQR_DONE
;
555 dasd_sfree_request(cqr
, cqr
->device
);
559 /* Fill in IOCTL data for device. */
561 dasd_diag_fill_info(struct dasd_device
* device
,
562 struct dasd_information2_t
* info
)
564 struct dasd_diag_private
*private;
566 private = (struct dasd_diag_private
*) device
->private;
567 info
->label_block
= (unsigned int) private->pt_block
;
568 info
->FBA_layout
= 1;
569 info
->format
= DASD_FORMAT_LDL
;
570 info
->characteristics_size
= sizeof (struct dasd_diag_characteristics
);
571 memcpy(info
->characteristics
,
572 &((struct dasd_diag_private
*) device
->private)->rdc_data
,
573 sizeof (struct dasd_diag_characteristics
));
574 info
->confdata_size
= 0;
579 dasd_diag_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
582 DEV_MESSAGE(KERN_ERR
, device
, "%s",
583 "dump sense not available for DIAG data");
586 struct dasd_discipline dasd_diag_discipline
= {
587 .owner
= THIS_MODULE
,
590 .max_blocks
= DIAG_MAX_BLOCKS
,
591 .check_device
= dasd_diag_check_device
,
592 .fill_geometry
= dasd_diag_fill_geometry
,
593 .start_IO
= dasd_start_diag
,
594 .term_IO
= dasd_diag_term_IO
,
595 .examine_error
= dasd_diag_examine_error
,
596 .erp_action
= dasd_diag_erp_action
,
597 .erp_postaction
= dasd_diag_erp_postaction
,
598 .build_cp
= dasd_diag_build_cp
,
599 .free_cp
= dasd_diag_free_cp
,
600 .dump_sense
= dasd_diag_dump_sense
,
601 .fill_info
= dasd_diag_fill_info
,
607 if (!MACHINE_IS_VM
) {
608 MESSAGE_LOG(KERN_INFO
,
609 "Machine is not VM: %s "
610 "discipline not initializing",
611 dasd_diag_discipline
.name
);
614 ASCEBC(dasd_diag_discipline
.ebcname
, 4);
617 register_external_interrupt(0x2603, dasd_ext_handler
);
618 dasd_diag_discipline_pointer
= &dasd_diag_discipline
;
623 dasd_diag_cleanup(void)
625 unregister_external_interrupt(0x2603, dasd_ext_handler
);
627 dasd_diag_discipline_pointer
= NULL
;
630 module_init(dasd_diag_init
);
631 module_exit(dasd_diag_cleanup
);