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 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;
55 struct dasd_diag_req {
56 unsigned int block_count;
57 struct dasd_diag_bio bio[0];
60 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
62 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
63 * and function code cmd.
64 * In case of an exception return 3. Otherwise return result of bitwise OR of
65 * resulting condition code and DIAG return code. */
67 dia250(void *iob, int cmd)
70 struct dasd_diag_init_io init_io;
71 struct dasd_diag_rw_io rw_io;
84 ".section __ex_table,\"a\"\n"
96 ".section __ex_table,\"a\"\n"
101 : "=&d" (rc), "=m" (*(addr_type *) iob)
102 : "d" (cmd), "d" (iob), "m" (*(addr_type *) iob)
107 /* Initialize block I/O to DIAG device using the specified blocksize and
108 * block offset. On success, return zero and set end_block to contain the
109 * number of blocks on the device minus the specified offset. Return non-zero
111 static __inline__ int
112 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
113 blocknum_t offset, blocknum_t *end_block)
115 struct dasd_diag_private *private;
116 struct dasd_diag_init_io *iib;
119 private = (struct dasd_diag_private *) device->private;
121 memset(iib, 0, sizeof (struct dasd_diag_init_io));
123 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
124 iib->block_size = blocksize;
125 iib->offset = offset;
126 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
128 rc = dia250(iib, INIT_BIO);
130 if ((rc & 3) == 0 && end_block)
131 *end_block = iib->end_block;
136 /* Remove block I/O environment for device. Return zero on success, non-zero
138 static __inline__ int
139 mdsk_term_io(struct dasd_device * device)
141 struct dasd_diag_private *private;
142 struct dasd_diag_init_io *iib;
145 private = (struct dasd_diag_private *) device->private;
147 memset(iib, 0, sizeof (struct dasd_diag_init_io));
148 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
149 rc = dia250(iib, TERM_BIO);
153 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
156 dasd_diag_erp(struct dasd_device *device)
160 mdsk_term_io(device);
161 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
163 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
167 /* Start a given request at the device. Return zero on success, non-zero
170 dasd_start_diag(struct dasd_ccw_req * cqr)
172 struct dasd_device *device;
173 struct dasd_diag_private *private;
174 struct dasd_diag_req *dreq;
177 device = cqr->device;
178 if (cqr->retries < 0) {
179 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
180 "- no retry left)", cqr);
181 cqr->status = DASD_CQR_FAILED;
184 private = (struct dasd_diag_private *) device->private;
185 dreq = (struct dasd_diag_req *) cqr->data;
187 private->iob.dev_nr = _ccw_device_get_device_number(device->cdev);
188 private->iob.key = 0;
189 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
190 private->iob.block_count = dreq->block_count;
191 private->iob.interrupt_params = (addr_t) cqr;
192 private->iob.bio_list = dreq->bio;
193 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
195 cqr->startclk = get_clock();
196 cqr->starttime = jiffies;
199 rc = dia250(&private->iob, RW_BIO);
201 case 0: /* Synchronous I/O finished successfully */
202 cqr->stopclk = get_clock();
203 cqr->status = DASD_CQR_DONE;
204 /* Indicate to calling function that only a dasd_schedule_bh()
205 and no timer is needed */
208 case 8: /* Asynchronous I/O was started */
209 cqr->status = DASD_CQR_IN_IO;
212 default: /* Error condition */
213 cqr->status = DASD_CQR_QUEUED;
214 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
215 dasd_diag_erp(device);
222 /* Terminate given request at the device. */
224 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
226 struct dasd_device *device;
228 device = cqr->device;
229 mdsk_term_io(device);
230 mdsk_init_io(device, device->bp_block, 0, NULL);
231 cqr->status = DASD_CQR_CLEAR;
232 cqr->stopclk = get_clock();
233 dasd_schedule_bh(device);
237 /* Handle external interruption. */
239 dasd_ext_handler(struct pt_regs *regs, __u16 code)
241 struct dasd_ccw_req *cqr, *next;
242 struct dasd_device *device;
243 unsigned long long expires;
249 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
250 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
252 case DASD_DIAG_CODE_31BIT:
253 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
255 case DASD_DIAG_CODE_64BIT:
256 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
261 if (!ip) { /* no intparm: unsolicited interrupt */
262 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
265 cqr = (struct dasd_ccw_req *) ip;
266 device = (struct dasd_device *) cqr->device;
267 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
268 DEV_MESSAGE(KERN_WARNING, device,
269 " magic number of dasd_ccw_req 0x%08X doesn't"
270 " match discipline 0x%08X",
271 cqr->magic, *(int *) (&device->discipline->name));
275 /* get irq lock to modify request queue */
276 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
278 /* Check for a pending clear operation */
279 if (cqr->status == DASD_CQR_CLEAR) {
280 cqr->status = DASD_CQR_QUEUED;
281 dasd_clear_timer(device);
282 dasd_schedule_bh(device);
283 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
287 cqr->stopclk = get_clock();
291 cqr->status = DASD_CQR_DONE;
292 /* Start first request on queue if possible -> fast_io. */
293 if (!list_empty(&device->ccw_queue)) {
294 next = list_entry(device->ccw_queue.next,
295 struct dasd_ccw_req, list);
296 if (next->status == DASD_CQR_QUEUED) {
297 rc = dasd_start_diag(next);
299 expires = next->expires;
300 else if (rc != -EACCES)
301 DEV_MESSAGE(KERN_WARNING, device, "%s",
302 "Interrupt fastpath "
307 cqr->status = DASD_CQR_QUEUED;
308 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
309 "request %p was %d (%d retries left)", cqr, status,
311 dasd_diag_erp(device);
315 dasd_set_timer(device, expires);
317 dasd_clear_timer(device);
318 dasd_schedule_bh(device);
320 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
323 /* Check whether device can be controlled by DIAG discipline. Return zero on
324 * success, non-zero otherwise. */
326 dasd_diag_check_device(struct dasd_device *device)
328 struct dasd_diag_private *private;
329 struct dasd_diag_characteristics *rdc_data;
330 struct dasd_diag_bio bio;
331 struct vtoc_cms_label *label;
332 blocknum_t end_block;
333 unsigned int sb, bsize;
336 private = (struct dasd_diag_private *) device->private;
337 if (private == NULL) {
338 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
339 if (private == NULL) {
340 DEV_MESSAGE(KERN_WARNING, device, "%s",
341 "memory allocation failed for private data");
344 device->private = (void *) private;
346 /* Read Device Characteristics */
347 rdc_data = (void *) &(private->rdc_data);
348 rdc_data->dev_nr = _ccw_device_get_device_number(device->cdev);
349 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
351 rc = diag210((struct diag210 *) rdc_data);
353 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
354 "information (rc=%d)", rc);
358 /* Figure out position of label block */
359 switch (private->rdc_data.vdev_class) {
361 private->pt_block = 1;
364 private->pt_block = 2;
367 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
368 "(class=%d)", private->rdc_data.vdev_class);
372 DBF_DEV_EVENT(DBF_INFO, device,
373 "%04X: %04X on real %04X/%02X",
376 rdc_data->rdev_type, rdc_data->rdev_model);
378 /* terminate all outstanding operations */
379 mdsk_term_io(device);
381 /* figure out blocksize of device */
382 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
384 DEV_MESSAGE(KERN_WARNING, device, "%s",
385 "No memory to allocate initialization request");
390 /* try all sizes - needed for ECKD devices */
391 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
392 mdsk_init_io(device, bsize, 0, &end_block);
393 memset(&bio, 0, sizeof (struct dasd_diag_bio));
394 bio.type = MDSK_READ_REQ;
395 bio.block_number = private->pt_block + 1;
397 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
398 private->iob.dev_nr = rdc_data->dev_nr;
399 private->iob.key = 0;
400 private->iob.flags = 0; /* do synchronous io */
401 private->iob.block_count = 1;
402 private->iob.interrupt_params = 0;
403 private->iob.bio_list = &bio;
404 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
405 rc = dia250(&private->iob, RW_BIO);
407 DEV_MESSAGE(KERN_WARNING, device, "%s",
412 mdsk_term_io(device);
416 if (bsize > PAGE_SIZE) {
417 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
422 /* check for label block */
423 if (memcmp(label->label_id, DASD_DIAG_CMS1,
424 sizeof(DASD_DIAG_CMS1)) == 0) {
425 /* get formatted blocksize from label block */
426 bsize = (unsigned int) label->block_size;
427 device->blocks = (unsigned long) label->block_count;
429 device->blocks = end_block;
430 device->bp_block = bsize;
431 device->s2b_shift = 0; /* bits to shift 512 to get a block */
432 for (sb = 512; sb < bsize; sb = sb << 1)
434 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
436 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
437 "failed (rc=%d)", rc);
440 DEV_MESSAGE(KERN_INFO, device,
441 "(%ld B/blk): %ldkB",
442 (unsigned long) device->bp_block,
443 (unsigned long) (device->blocks <<
444 device->s2b_shift) >> 1);
447 free_page((long) label);
451 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
454 dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
456 if (dasd_check_blocksize(device->bp_block) != 0)
458 geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
460 geo->sectors = 128 >> device->s2b_shift;
465 dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
467 return dasd_era_fatal;
471 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
473 return dasd_default_erp_action;
477 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
479 return dasd_default_erp_postaction;
482 /* Create DASD request from block device request. Return pointer to new
483 * request on success, ERR_PTR otherwise. */
484 static struct dasd_ccw_req *
485 dasd_diag_build_cp(struct dasd_device * device, struct request *req)
487 struct dasd_ccw_req *cqr;
488 struct dasd_diag_req *dreq;
489 struct dasd_diag_bio *dbio;
493 unsigned int count, datasize;
494 sector_t recid, first_rec, last_rec;
495 unsigned int blksize, off;
496 unsigned char rw_cmd;
499 if (rq_data_dir(req) == READ)
500 rw_cmd = MDSK_READ_REQ;
501 else if (rq_data_dir(req) == WRITE)
502 rw_cmd = MDSK_WRITE_REQ;
504 return ERR_PTR(-EINVAL);
505 blksize = device->bp_block;
506 /* Calculate record id of first and last block. */
507 first_rec = req->sector >> device->s2b_shift;
508 last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
509 /* Check struct bio and count the number of blocks for the request. */
511 rq_for_each_bio(bio, req) {
512 bio_for_each_segment(bv, bio, i) {
513 if (bv->bv_len & (blksize - 1))
514 /* Fba can only do full blocks. */
515 return ERR_PTR(-EINVAL);
516 count += bv->bv_len >> (device->s2b_shift + 9);
520 if (count != last_rec - first_rec + 1)
521 return ERR_PTR(-EINVAL);
522 /* Build the request */
523 datasize = sizeof(struct dasd_diag_req) +
524 count*sizeof(struct dasd_diag_bio);
525 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
530 dreq = (struct dasd_diag_req *) cqr->data;
531 dreq->block_count = count;
534 rq_for_each_bio(bio, req) {
535 bio_for_each_segment(bv, bio, i) {
536 dst = page_address(bv->bv_page) + bv->bv_offset;
537 for (off = 0; off < bv->bv_len; off += blksize) {
538 memset(dbio, 0, sizeof (struct dasd_diag_bio));
540 dbio->block_number = recid + 1;
548 cqr->retries = DIAG_MAX_RETRIES;
549 cqr->buildclk = get_clock();
550 if (req->flags & REQ_FAILFAST)
551 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
552 cqr->device = device;
553 cqr->expires = DIAG_TIMEOUT;
554 cqr->status = DASD_CQR_FILLED;
558 /* Release DASD request. Return non-zero if request was successful, zero
561 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
565 status = cqr->status == DASD_CQR_DONE;
566 dasd_sfree_request(cqr, cqr->device);
570 /* Fill in IOCTL data for device. */
572 dasd_diag_fill_info(struct dasd_device * device,
573 struct dasd_information2_t * info)
575 struct dasd_diag_private *private;
577 private = (struct dasd_diag_private *) device->private;
578 info->label_block = (unsigned int) private->pt_block;
579 info->FBA_layout = 1;
580 info->format = DASD_FORMAT_LDL;
581 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
582 memcpy(info->characteristics,
583 &((struct dasd_diag_private *) device->private)->rdc_data,
584 sizeof (struct dasd_diag_characteristics));
585 info->confdata_size = 0;
590 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
593 DEV_MESSAGE(KERN_ERR, device, "%s",
594 "dump sense not available for DIAG data");
597 struct dasd_discipline dasd_diag_discipline = {
598 .owner = THIS_MODULE,
601 .max_blocks = DIAG_MAX_BLOCKS,
602 .check_device = dasd_diag_check_device,
603 .fill_geometry = dasd_diag_fill_geometry,
604 .start_IO = dasd_start_diag,
605 .term_IO = dasd_diag_term_IO,
606 .examine_error = dasd_diag_examine_error,
607 .erp_action = dasd_diag_erp_action,
608 .erp_postaction = dasd_diag_erp_postaction,
609 .build_cp = dasd_diag_build_cp,
610 .free_cp = dasd_diag_free_cp,
611 .dump_sense = dasd_diag_dump_sense,
612 .fill_info = dasd_diag_fill_info,
618 if (!MACHINE_IS_VM) {
619 MESSAGE_LOG(KERN_INFO,
620 "Machine is not VM: %s "
621 "discipline not initializing",
622 dasd_diag_discipline.name);
625 ASCEBC(dasd_diag_discipline.ebcname, 4);
628 register_external_interrupt(0x2603, dasd_ext_handler);
629 dasd_diag_discipline_pointer = &dasd_diag_discipline;
634 dasd_diag_cleanup(void)
636 unregister_external_interrupt(0x2603, dasd_ext_handler);
638 dasd_diag_discipline_pointer = NULL;
641 module_init(dasd_diag_init);
642 module_exit(dasd_diag_cleanup);