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>
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 static 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
;
54 struct ccw_dev_id dev_id
;
57 struct dasd_diag_req
{
58 unsigned int block_count
;
59 struct dasd_diag_bio bio
[0];
62 static const u8 DASD_DIAG_CMS1
[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
64 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
65 * and function code cmd.
66 * In case of an exception return 3. Otherwise return result of bitwise OR of
67 * resulting condition code and DIAG return code. */
68 static inline int dia250(void *iob
, int cmd
)
70 register unsigned long reg2
asm ("2") = (unsigned long) iob
;
72 struct dasd_diag_init_io init_io
;
73 struct dasd_diag_rw_io rw_io
;
85 : "+d" (rc
), "=m" (*(addr_type
*) iob
)
86 : "d" (cmd
), "d" (reg2
), "m" (*(addr_type
*) iob
)
91 /* Initialize block I/O to DIAG device using the specified blocksize and
92 * block offset. On success, return zero and set end_block to contain the
93 * number of blocks on the device minus the specified offset. Return non-zero
96 mdsk_init_io(struct dasd_device
*device
, unsigned int blocksize
,
97 blocknum_t offset
, blocknum_t
*end_block
)
99 struct dasd_diag_private
*private;
100 struct dasd_diag_init_io
*iib
;
103 private = (struct dasd_diag_private
*) device
->private;
105 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
107 iib
->dev_nr
= private->dev_id
.devno
;
108 iib
->block_size
= blocksize
;
109 iib
->offset
= offset
;
110 iib
->flaga
= DASD_DIAG_FLAGA_DEFAULT
;
112 rc
= dia250(iib
, INIT_BIO
);
114 if ((rc
& 3) == 0 && end_block
)
115 *end_block
= iib
->end_block
;
120 /* Remove block I/O environment for device. Return zero on success, non-zero
123 mdsk_term_io(struct dasd_device
* device
)
125 struct dasd_diag_private
*private;
126 struct dasd_diag_init_io
*iib
;
129 private = (struct dasd_diag_private
*) device
->private;
131 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
132 iib
->dev_nr
= private->dev_id
.devno
;
133 rc
= dia250(iib
, TERM_BIO
);
137 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
140 dasd_diag_erp(struct dasd_device
*device
)
144 mdsk_term_io(device
);
145 rc
= mdsk_init_io(device
, device
->block
->bp_block
, 0, NULL
);
147 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG ERP unsuccessful, "
151 /* Start a given request at the device. Return zero on success, non-zero
154 dasd_start_diag(struct dasd_ccw_req
* cqr
)
156 struct dasd_device
*device
;
157 struct dasd_diag_private
*private;
158 struct dasd_diag_req
*dreq
;
161 device
= cqr
->startdev
;
162 if (cqr
->retries
< 0) {
163 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG start_IO: request %p "
164 "- no retry left)", cqr
);
165 cqr
->status
= DASD_CQR_ERROR
;
168 private = (struct dasd_diag_private
*) device
->private;
169 dreq
= (struct dasd_diag_req
*) cqr
->data
;
171 private->iob
.dev_nr
= private->dev_id
.devno
;
172 private->iob
.key
= 0;
173 private->iob
.flags
= DASD_DIAG_RWFLAG_ASYNC
;
174 private->iob
.block_count
= dreq
->block_count
;
175 private->iob
.interrupt_params
= (addr_t
) cqr
;
176 private->iob
.bio_list
= dreq
->bio
;
177 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
179 cqr
->startclk
= get_clock();
180 cqr
->starttime
= jiffies
;
183 rc
= dia250(&private->iob
, RW_BIO
);
185 case 0: /* Synchronous I/O finished successfully */
186 cqr
->stopclk
= get_clock();
187 cqr
->status
= DASD_CQR_SUCCESS
;
188 /* Indicate to calling function that only a dasd_schedule_bh()
189 and no timer is needed */
192 case 8: /* Asynchronous I/O was started */
193 cqr
->status
= DASD_CQR_IN_IO
;
196 default: /* Error condition */
197 cqr
->status
= DASD_CQR_QUEUED
;
198 DEV_MESSAGE(KERN_WARNING
, device
, "dia250 returned rc=%d", rc
);
199 dasd_diag_erp(device
);
206 /* Terminate given request at the device. */
208 dasd_diag_term_IO(struct dasd_ccw_req
* cqr
)
210 struct dasd_device
*device
;
212 device
= cqr
->startdev
;
213 mdsk_term_io(device
);
214 mdsk_init_io(device
, device
->block
->bp_block
, 0, NULL
);
215 cqr
->status
= DASD_CQR_CLEAR_PENDING
;
216 cqr
->stopclk
= get_clock();
217 dasd_schedule_device_bh(device
);
221 /* Handle external interruption. */
223 dasd_ext_handler(__u16 code
)
225 struct dasd_ccw_req
*cqr
, *next
;
226 struct dasd_device
*device
;
227 unsigned long long expires
;
233 int_code
= *((u8
*) DASD_DIAG_LC_INT_CODE
);
234 status
= *((u8
*) DASD_DIAG_LC_INT_STATUS
);
236 case DASD_DIAG_CODE_31BIT
:
237 ip
= (addr_t
) *((u32
*) DASD_DIAG_LC_INT_PARM_31BIT
);
239 case DASD_DIAG_CODE_64BIT
:
240 ip
= (addr_t
) *((u64
*) DASD_DIAG_LC_INT_PARM_64BIT
);
245 if (!ip
) { /* no intparm: unsolicited interrupt */
246 MESSAGE(KERN_DEBUG
, "%s", "caught unsolicited interrupt");
249 cqr
= (struct dasd_ccw_req
*) ip
;
250 device
= (struct dasd_device
*) cqr
->startdev
;
251 if (strncmp(device
->discipline
->ebcname
, (char *) &cqr
->magic
, 4)) {
252 DEV_MESSAGE(KERN_WARNING
, device
,
253 " magic number of dasd_ccw_req 0x%08X doesn't"
254 " match discipline 0x%08X",
255 cqr
->magic
, *(int *) (&device
->discipline
->name
));
259 /* get irq lock to modify request queue */
260 spin_lock_irqsave(get_ccwdev_lock(device
->cdev
), flags
);
262 /* Check for a pending clear operation */
263 if (cqr
->status
== DASD_CQR_CLEAR_PENDING
) {
264 cqr
->status
= DASD_CQR_CLEARED
;
265 dasd_device_clear_timer(device
);
266 dasd_schedule_device_bh(device
);
267 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
271 cqr
->stopclk
= get_clock();
275 cqr
->status
= DASD_CQR_SUCCESS
;
276 /* Start first request on queue if possible -> fast_io. */
277 if (!list_empty(&device
->ccw_queue
)) {
278 next
= list_entry(device
->ccw_queue
.next
,
279 struct dasd_ccw_req
, devlist
);
280 if (next
->status
== DASD_CQR_QUEUED
) {
281 rc
= dasd_start_diag(next
);
283 expires
= next
->expires
;
284 else if (rc
!= -EACCES
)
285 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
286 "Interrupt fastpath "
291 cqr
->status
= DASD_CQR_QUEUED
;
292 DEV_MESSAGE(KERN_WARNING
, device
, "interrupt status for "
293 "request %p was %d (%d retries left)", cqr
, status
,
295 dasd_diag_erp(device
);
299 dasd_device_set_timer(device
, expires
);
301 dasd_device_clear_timer(device
);
302 dasd_schedule_device_bh(device
);
304 spin_unlock_irqrestore(get_ccwdev_lock(device
->cdev
), flags
);
307 /* Check whether device can be controlled by DIAG discipline. Return zero on
308 * success, non-zero otherwise. */
310 dasd_diag_check_device(struct dasd_device
*device
)
312 struct dasd_block
*block
;
313 struct dasd_diag_private
*private;
314 struct dasd_diag_characteristics
*rdc_data
;
315 struct dasd_diag_bio bio
;
316 struct vtoc_cms_label
*label
;
317 blocknum_t end_block
;
318 unsigned int sb
, bsize
;
321 private = (struct dasd_diag_private
*) device
->private;
322 if (private == NULL
) {
323 private = kzalloc(sizeof(struct dasd_diag_private
),GFP_KERNEL
);
324 if (private == NULL
) {
325 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
326 "memory allocation failed for private data");
329 ccw_device_get_id(device
->cdev
, &private->dev_id
);
330 device
->private = (void *) private;
332 block
= dasd_alloc_block();
334 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
335 "could not allocate dasd block structure");
336 kfree(device
->private);
337 return PTR_ERR(block
);
339 device
->block
= block
;
340 block
->base
= device
;
342 /* Read Device Characteristics */
343 rdc_data
= (void *) &(private->rdc_data
);
344 rdc_data
->dev_nr
= private->dev_id
.devno
;
345 rdc_data
->rdc_len
= sizeof (struct dasd_diag_characteristics
);
347 rc
= diag210((struct diag210
*) rdc_data
);
349 DEV_MESSAGE(KERN_WARNING
, device
, "failed to retrieve device "
350 "information (rc=%d)", rc
);
354 /* Figure out position of label block */
355 switch (private->rdc_data
.vdev_class
) {
357 private->pt_block
= 1;
360 private->pt_block
= 2;
363 DEV_MESSAGE(KERN_WARNING
, device
, "unsupported device class "
364 "(class=%d)", private->rdc_data
.vdev_class
);
368 DBF_DEV_EVENT(DBF_INFO
, device
,
369 "%04X: %04X on real %04X/%02X",
372 rdc_data
->rdev_type
, rdc_data
->rdev_model
);
374 /* terminate all outstanding operations */
375 mdsk_term_io(device
);
377 /* figure out blocksize of device */
378 label
= (struct vtoc_cms_label
*) get_zeroed_page(GFP_KERNEL
);
380 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
381 "No memory to allocate initialization request");
386 /* try all sizes - needed for ECKD devices */
387 for (bsize
= 512; bsize
<= PAGE_SIZE
; bsize
<<= 1) {
388 mdsk_init_io(device
, bsize
, 0, &end_block
);
389 memset(&bio
, 0, sizeof (struct dasd_diag_bio
));
390 bio
.type
= MDSK_READ_REQ
;
391 bio
.block_number
= private->pt_block
+ 1;
393 memset(&private->iob
, 0, sizeof (struct dasd_diag_rw_io
));
394 private->iob
.dev_nr
= rdc_data
->dev_nr
;
395 private->iob
.key
= 0;
396 private->iob
.flags
= 0; /* do synchronous io */
397 private->iob
.block_count
= 1;
398 private->iob
.interrupt_params
= 0;
399 private->iob
.bio_list
= &bio
;
400 private->iob
.flaga
= DASD_DIAG_FLAGA_DEFAULT
;
401 rc
= dia250(&private->iob
, RW_BIO
);
403 DEV_MESSAGE(KERN_WARNING
, device
, "%s",
408 mdsk_term_io(device
);
412 if (bsize
> PAGE_SIZE
) {
413 DEV_MESSAGE(KERN_WARNING
, device
, "device access failed "
418 /* check for label block */
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 block
->blocks
= (unsigned long) label
->block_count
;
425 block
->blocks
= end_block
;
426 block
->bp_block
= bsize
;
427 block
->s2b_shift
= 0; /* bits to shift 512 to get a block */
428 for (sb
= 512; sb
< bsize
; sb
= sb
<< 1)
430 rc
= mdsk_init_io(device
, block
->bp_block
, 0, NULL
);
432 DEV_MESSAGE(KERN_WARNING
, device
, "DIAG initialization "
433 "failed (rc=%d)", rc
);
436 DEV_MESSAGE(KERN_INFO
, device
,
437 "(%ld B/blk): %ldkB",
438 (unsigned long) block
->bp_block
,
439 (unsigned long) (block
->blocks
<<
440 block
->s2b_shift
) >> 1);
443 free_page((long) label
);
447 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
450 dasd_diag_fill_geometry(struct dasd_block
*block
, struct hd_geometry
*geo
)
452 if (dasd_check_blocksize(block
->bp_block
) != 0)
454 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
456 geo
->sectors
= 128 >> block
->s2b_shift
;
461 dasd_diag_erp_action(struct dasd_ccw_req
* cqr
)
463 return dasd_default_erp_action
;
467 dasd_diag_erp_postaction(struct dasd_ccw_req
* cqr
)
469 return dasd_default_erp_postaction
;
472 /* Create DASD request from block device request. Return pointer to new
473 * request on success, ERR_PTR otherwise. */
474 static struct dasd_ccw_req
*dasd_diag_build_cp(struct dasd_device
*memdev
,
475 struct dasd_block
*block
,
478 struct dasd_ccw_req
*cqr
;
479 struct dasd_diag_req
*dreq
;
480 struct dasd_diag_bio
*dbio
;
481 struct req_iterator iter
;
484 unsigned int count
, datasize
;
485 sector_t recid
, first_rec
, last_rec
;
486 unsigned int blksize
, off
;
487 unsigned char rw_cmd
;
489 if (rq_data_dir(req
) == READ
)
490 rw_cmd
= MDSK_READ_REQ
;
491 else if (rq_data_dir(req
) == WRITE
)
492 rw_cmd
= MDSK_WRITE_REQ
;
494 return ERR_PTR(-EINVAL
);
495 blksize
= block
->bp_block
;
496 /* Calculate record id of first and last block. */
497 first_rec
= req
->sector
>> block
->s2b_shift
;
498 last_rec
= (req
->sector
+ req
->nr_sectors
- 1) >> block
->s2b_shift
;
499 /* Check struct bio and count the number of blocks for the request. */
501 rq_for_each_segment(bv
, req
, iter
) {
502 if (bv
->bv_len
& (blksize
- 1))
503 /* Fba can only do full blocks. */
504 return ERR_PTR(-EINVAL
);
505 count
+= bv
->bv_len
>> (block
->s2b_shift
+ 9);
508 if (count
!= last_rec
- first_rec
+ 1)
509 return ERR_PTR(-EINVAL
);
510 /* Build the request */
511 datasize
= sizeof(struct dasd_diag_req
) +
512 count
*sizeof(struct dasd_diag_bio
);
513 cqr
= dasd_smalloc_request(dasd_diag_discipline
.name
, 0,
518 dreq
= (struct dasd_diag_req
*) cqr
->data
;
519 dreq
->block_count
= count
;
522 rq_for_each_segment(bv
, req
, iter
) {
523 dst
= page_address(bv
->bv_page
) + bv
->bv_offset
;
524 for (off
= 0; off
< bv
->bv_len
; off
+= blksize
) {
525 memset(dbio
, 0, sizeof (struct dasd_diag_bio
));
527 dbio
->block_number
= recid
+ 1;
534 cqr
->retries
= DIAG_MAX_RETRIES
;
535 cqr
->buildclk
= get_clock();
536 if (req
->cmd_flags
& REQ_FAILFAST
)
537 set_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
);
538 cqr
->startdev
= memdev
;
539 cqr
->memdev
= memdev
;
541 cqr
->expires
= DIAG_TIMEOUT
;
542 cqr
->status
= DASD_CQR_FILLED
;
546 /* Release DASD request. Return non-zero if request was successful, zero
549 dasd_diag_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
553 status
= cqr
->status
== DASD_CQR_DONE
;
554 dasd_sfree_request(cqr
, cqr
->memdev
);
558 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req
*cqr
)
560 cqr
->status
= DASD_CQR_FILLED
;
563 /* Fill in IOCTL data for device. */
565 dasd_diag_fill_info(struct dasd_device
* device
,
566 struct dasd_information2_t
* info
)
568 struct dasd_diag_private
*private;
570 private = (struct dasd_diag_private
*) device
->private;
571 info
->label_block
= (unsigned int) private->pt_block
;
572 info
->FBA_layout
= 1;
573 info
->format
= DASD_FORMAT_LDL
;
574 info
->characteristics_size
= sizeof (struct dasd_diag_characteristics
);
575 memcpy(info
->characteristics
,
576 &((struct dasd_diag_private
*) device
->private)->rdc_data
,
577 sizeof (struct dasd_diag_characteristics
));
578 info
->confdata_size
= 0;
583 dasd_diag_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
586 DEV_MESSAGE(KERN_ERR
, device
, "%s",
587 "dump sense not available for DIAG data");
590 static struct dasd_discipline dasd_diag_discipline
= {
591 .owner
= THIS_MODULE
,
594 .max_blocks
= DIAG_MAX_BLOCKS
,
595 .check_device
= dasd_diag_check_device
,
596 .fill_geometry
= dasd_diag_fill_geometry
,
597 .start_IO
= dasd_start_diag
,
598 .term_IO
= dasd_diag_term_IO
,
599 .handle_terminated_request
= dasd_diag_handle_terminated_request
,
600 .erp_action
= dasd_diag_erp_action
,
601 .erp_postaction
= dasd_diag_erp_postaction
,
602 .build_cp
= dasd_diag_build_cp
,
603 .free_cp
= dasd_diag_free_cp
,
604 .dump_sense
= dasd_diag_dump_sense
,
605 .fill_info
= dasd_diag_fill_info
,
611 if (!MACHINE_IS_VM
) {
612 MESSAGE_LOG(KERN_INFO
,
613 "Machine is not VM: %s "
614 "discipline not initializing",
615 dasd_diag_discipline
.name
);
618 ASCEBC(dasd_diag_discipline
.ebcname
, 4);
621 register_external_interrupt(0x2603, dasd_ext_handler
);
622 dasd_diag_discipline_pointer
= &dasd_diag_discipline
;
627 dasd_diag_cleanup(void)
629 unregister_external_interrupt(0x2603, dasd_ext_handler
);
631 dasd_diag_discipline_pointer
= NULL
;
634 module_init(dasd_diag_init
);
635 module_exit(dasd_diag_cleanup
);