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;
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. */
66 static inline int dia250(void *iob, int cmd)
68 register unsigned long reg0 asm ("0") = (unsigned long) iob;
70 struct dasd_diag_init_io init_io;
71 struct dasd_diag_rw_io rw_io;
83 : "+d" (rc), "=m" (*(addr_type *) iob)
84 : "d" (cmd), "d" (reg0), "m" (*(addr_type *) iob)
89 /* Initialize block I/O to DIAG device using the specified blocksize and
90 * block offset. On success, return zero and set end_block to contain the
91 * number of blocks on the device minus the specified offset. Return non-zero
94 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
95 blocknum_t offset, blocknum_t *end_block)
97 struct dasd_diag_private *private;
98 struct dasd_diag_init_io *iib;
101 private = (struct dasd_diag_private *) device->private;
103 memset(iib, 0, sizeof (struct dasd_diag_init_io));
105 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
106 iib->block_size = blocksize;
107 iib->offset = offset;
108 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
110 rc = dia250(iib, INIT_BIO);
112 if ((rc & 3) == 0 && end_block)
113 *end_block = iib->end_block;
118 /* Remove block I/O environment for device. Return zero on success, non-zero
121 mdsk_term_io(struct dasd_device * device)
123 struct dasd_diag_private *private;
124 struct dasd_diag_init_io *iib;
127 private = (struct dasd_diag_private *) device->private;
129 memset(iib, 0, sizeof (struct dasd_diag_init_io));
130 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
131 rc = dia250(iib, TERM_BIO);
135 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
138 dasd_diag_erp(struct dasd_device *device)
142 mdsk_term_io(device);
143 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
145 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
149 /* Start a given request at the device. Return zero on success, non-zero
152 dasd_start_diag(struct dasd_ccw_req * cqr)
154 struct dasd_device *device;
155 struct dasd_diag_private *private;
156 struct dasd_diag_req *dreq;
159 device = cqr->device;
160 if (cqr->retries < 0) {
161 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
162 "- no retry left)", cqr);
163 cqr->status = DASD_CQR_FAILED;
166 private = (struct dasd_diag_private *) device->private;
167 dreq = (struct dasd_diag_req *) cqr->data;
169 private->iob.dev_nr = _ccw_device_get_device_number(device->cdev);
170 private->iob.key = 0;
171 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
172 private->iob.block_count = dreq->block_count;
173 private->iob.interrupt_params = (addr_t) cqr;
174 private->iob.bio_list = dreq->bio;
175 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
177 cqr->startclk = get_clock();
178 cqr->starttime = jiffies;
181 rc = dia250(&private->iob, RW_BIO);
183 case 0: /* Synchronous I/O finished successfully */
184 cqr->stopclk = get_clock();
185 cqr->status = DASD_CQR_DONE;
186 /* Indicate to calling function that only a dasd_schedule_bh()
187 and no timer is needed */
190 case 8: /* Asynchronous I/O was started */
191 cqr->status = DASD_CQR_IN_IO;
194 default: /* Error condition */
195 cqr->status = DASD_CQR_QUEUED;
196 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
197 dasd_diag_erp(device);
204 /* Terminate given request at the device. */
206 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
208 struct dasd_device *device;
210 device = cqr->device;
211 mdsk_term_io(device);
212 mdsk_init_io(device, device->bp_block, 0, NULL);
213 cqr->status = DASD_CQR_CLEAR;
214 cqr->stopclk = get_clock();
215 dasd_schedule_bh(device);
219 /* Handle external interruption. */
221 dasd_ext_handler(__u16 code)
223 struct dasd_ccw_req *cqr, *next;
224 struct dasd_device *device;
225 unsigned long long expires;
231 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
232 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
234 case DASD_DIAG_CODE_31BIT:
235 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
237 case DASD_DIAG_CODE_64BIT:
238 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
243 if (!ip) { /* no intparm: unsolicited interrupt */
244 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
247 cqr = (struct dasd_ccw_req *) ip;
248 device = (struct dasd_device *) cqr->device;
249 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
250 DEV_MESSAGE(KERN_WARNING, device,
251 " magic number of dasd_ccw_req 0x%08X doesn't"
252 " match discipline 0x%08X",
253 cqr->magic, *(int *) (&device->discipline->name));
257 /* get irq lock to modify request queue */
258 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
260 /* Check for a pending clear operation */
261 if (cqr->status == DASD_CQR_CLEAR) {
262 cqr->status = DASD_CQR_QUEUED;
263 dasd_clear_timer(device);
264 dasd_schedule_bh(device);
265 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
269 cqr->stopclk = get_clock();
273 cqr->status = DASD_CQR_DONE;
274 /* Start first request on queue if possible -> fast_io. */
275 if (!list_empty(&device->ccw_queue)) {
276 next = list_entry(device->ccw_queue.next,
277 struct dasd_ccw_req, list);
278 if (next->status == DASD_CQR_QUEUED) {
279 rc = dasd_start_diag(next);
281 expires = next->expires;
282 else if (rc != -EACCES)
283 DEV_MESSAGE(KERN_WARNING, device, "%s",
284 "Interrupt fastpath "
289 cqr->status = DASD_CQR_QUEUED;
290 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
291 "request %p was %d (%d retries left)", cqr, status,
293 dasd_diag_erp(device);
297 dasd_set_timer(device, expires);
299 dasd_clear_timer(device);
300 dasd_schedule_bh(device);
302 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
305 /* Check whether device can be controlled by DIAG discipline. Return zero on
306 * success, non-zero otherwise. */
308 dasd_diag_check_device(struct dasd_device *device)
310 struct dasd_diag_private *private;
311 struct dasd_diag_characteristics *rdc_data;
312 struct dasd_diag_bio bio;
313 struct vtoc_cms_label *label;
314 blocknum_t end_block;
315 unsigned int sb, bsize;
318 private = (struct dasd_diag_private *) device->private;
319 if (private == NULL) {
320 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
321 if (private == NULL) {
322 DEV_MESSAGE(KERN_WARNING, device, "%s",
323 "memory allocation failed for private data");
326 device->private = (void *) private;
328 /* Read Device Characteristics */
329 rdc_data = (void *) &(private->rdc_data);
330 rdc_data->dev_nr = _ccw_device_get_device_number(device->cdev);
331 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
333 rc = diag210((struct diag210 *) rdc_data);
335 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
336 "information (rc=%d)", rc);
340 /* Figure out position of label block */
341 switch (private->rdc_data.vdev_class) {
343 private->pt_block = 1;
346 private->pt_block = 2;
349 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
350 "(class=%d)", private->rdc_data.vdev_class);
354 DBF_DEV_EVENT(DBF_INFO, device,
355 "%04X: %04X on real %04X/%02X",
358 rdc_data->rdev_type, rdc_data->rdev_model);
360 /* terminate all outstanding operations */
361 mdsk_term_io(device);
363 /* figure out blocksize of device */
364 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
366 DEV_MESSAGE(KERN_WARNING, device, "%s",
367 "No memory to allocate initialization request");
372 /* try all sizes - needed for ECKD devices */
373 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
374 mdsk_init_io(device, bsize, 0, &end_block);
375 memset(&bio, 0, sizeof (struct dasd_diag_bio));
376 bio.type = MDSK_READ_REQ;
377 bio.block_number = private->pt_block + 1;
379 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
380 private->iob.dev_nr = rdc_data->dev_nr;
381 private->iob.key = 0;
382 private->iob.flags = 0; /* do synchronous io */
383 private->iob.block_count = 1;
384 private->iob.interrupt_params = 0;
385 private->iob.bio_list = &bio;
386 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
387 rc = dia250(&private->iob, RW_BIO);
389 DEV_MESSAGE(KERN_WARNING, device, "%s",
394 mdsk_term_io(device);
398 if (bsize > PAGE_SIZE) {
399 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
404 /* check for label block */
405 if (memcmp(label->label_id, DASD_DIAG_CMS1,
406 sizeof(DASD_DIAG_CMS1)) == 0) {
407 /* get formatted blocksize from label block */
408 bsize = (unsigned int) label->block_size;
409 device->blocks = (unsigned long) label->block_count;
411 device->blocks = end_block;
412 device->bp_block = bsize;
413 device->s2b_shift = 0; /* bits to shift 512 to get a block */
414 for (sb = 512; sb < bsize; sb = sb << 1)
416 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
418 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
419 "failed (rc=%d)", rc);
422 DEV_MESSAGE(KERN_INFO, device,
423 "(%ld B/blk): %ldkB",
424 (unsigned long) device->bp_block,
425 (unsigned long) (device->blocks <<
426 device->s2b_shift) >> 1);
429 free_page((long) label);
433 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
436 dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
438 if (dasd_check_blocksize(device->bp_block) != 0)
440 geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
442 geo->sectors = 128 >> device->s2b_shift;
447 dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
449 return dasd_era_fatal;
453 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
455 return dasd_default_erp_action;
459 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
461 return dasd_default_erp_postaction;
464 /* Create DASD request from block device request. Return pointer to new
465 * request on success, ERR_PTR otherwise. */
466 static struct dasd_ccw_req *
467 dasd_diag_build_cp(struct dasd_device * device, struct request *req)
469 struct dasd_ccw_req *cqr;
470 struct dasd_diag_req *dreq;
471 struct dasd_diag_bio *dbio;
475 unsigned int count, datasize;
476 sector_t recid, first_rec, last_rec;
477 unsigned int blksize, off;
478 unsigned char rw_cmd;
481 if (rq_data_dir(req) == READ)
482 rw_cmd = MDSK_READ_REQ;
483 else if (rq_data_dir(req) == WRITE)
484 rw_cmd = MDSK_WRITE_REQ;
486 return ERR_PTR(-EINVAL);
487 blksize = device->bp_block;
488 /* Calculate record id of first and last block. */
489 first_rec = req->sector >> device->s2b_shift;
490 last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
491 /* Check struct bio and count the number of blocks for the request. */
493 rq_for_each_bio(bio, req) {
494 bio_for_each_segment(bv, bio, i) {
495 if (bv->bv_len & (blksize - 1))
496 /* Fba can only do full blocks. */
497 return ERR_PTR(-EINVAL);
498 count += bv->bv_len >> (device->s2b_shift + 9);
502 if (count != last_rec - first_rec + 1)
503 return ERR_PTR(-EINVAL);
504 /* Build the request */
505 datasize = sizeof(struct dasd_diag_req) +
506 count*sizeof(struct dasd_diag_bio);
507 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
512 dreq = (struct dasd_diag_req *) cqr->data;
513 dreq->block_count = count;
516 rq_for_each_bio(bio, req) {
517 bio_for_each_segment(bv, bio, i) {
518 dst = page_address(bv->bv_page) + bv->bv_offset;
519 for (off = 0; off < bv->bv_len; off += blksize) {
520 memset(dbio, 0, sizeof (struct dasd_diag_bio));
522 dbio->block_number = recid + 1;
530 cqr->retries = DIAG_MAX_RETRIES;
531 cqr->buildclk = get_clock();
532 if (req->cmd_flags & REQ_FAILFAST)
533 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
534 cqr->device = device;
535 cqr->expires = DIAG_TIMEOUT;
536 cqr->status = DASD_CQR_FILLED;
540 /* Release DASD request. Return non-zero if request was successful, zero
543 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
547 status = cqr->status == DASD_CQR_DONE;
548 dasd_sfree_request(cqr, cqr->device);
552 /* Fill in IOCTL data for device. */
554 dasd_diag_fill_info(struct dasd_device * device,
555 struct dasd_information2_t * info)
557 struct dasd_diag_private *private;
559 private = (struct dasd_diag_private *) device->private;
560 info->label_block = (unsigned int) private->pt_block;
561 info->FBA_layout = 1;
562 info->format = DASD_FORMAT_LDL;
563 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
564 memcpy(info->characteristics,
565 &((struct dasd_diag_private *) device->private)->rdc_data,
566 sizeof (struct dasd_diag_characteristics));
567 info->confdata_size = 0;
572 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
575 DEV_MESSAGE(KERN_ERR, device, "%s",
576 "dump sense not available for DIAG data");
579 static struct dasd_discipline dasd_diag_discipline = {
580 .owner = THIS_MODULE,
583 .max_blocks = DIAG_MAX_BLOCKS,
584 .check_device = dasd_diag_check_device,
585 .fill_geometry = dasd_diag_fill_geometry,
586 .start_IO = dasd_start_diag,
587 .term_IO = dasd_diag_term_IO,
588 .examine_error = dasd_diag_examine_error,
589 .erp_action = dasd_diag_erp_action,
590 .erp_postaction = dasd_diag_erp_postaction,
591 .build_cp = dasd_diag_build_cp,
592 .free_cp = dasd_diag_free_cp,
593 .dump_sense = dasd_diag_dump_sense,
594 .fill_info = dasd_diag_fill_info,
600 if (!MACHINE_IS_VM) {
601 MESSAGE_LOG(KERN_INFO,
602 "Machine is not VM: %s "
603 "discipline not initializing",
604 dasd_diag_discipline.name);
607 ASCEBC(dasd_diag_discipline.ebcname, 4);
610 register_external_interrupt(0x2603, dasd_ext_handler);
611 dasd_diag_discipline_pointer = &dasd_diag_discipline;
616 dasd_diag_cleanup(void)
618 unregister_external_interrupt(0x2603, dasd_ext_handler);
620 dasd_diag_discipline_pointer = NULL;
623 module_init(dasd_diag_init);
624 module_exit(dasd_diag_cleanup);