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 #define KMSG_COMPONENT "dasd"
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>
32 #include "dasd_diag.h"
34 #define PRINTK_HEADER "dasd(diag):"
36 MODULE_LICENSE("GPL");
38 /* The maximum number of blocks per request (max_blocks) is dependent on the
39 * amount of storage that is available in the static I/O buffer for each
40 * device. Currently each device gets 2 pages. We want to fit two requests
41 * into the available memory so that we can immediately start the next if one
43 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
44 sizeof(struct dasd_diag_req)) / \
45 sizeof(struct dasd_diag_bio)) / 2)
46 #define DIAG_MAX_RETRIES 32
47 #define DIAG_TIMEOUT 50 * HZ
49 static struct dasd_discipline dasd_diag_discipline;
51 struct dasd_diag_private {
52 struct dasd_diag_characteristics rdc_data;
53 struct dasd_diag_rw_io iob;
54 struct dasd_diag_init_io iib;
56 struct ccw_dev_id dev_id;
59 struct dasd_diag_req {
60 unsigned int block_count;
61 struct dasd_diag_bio bio[0];
64 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
66 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
67 * and function code cmd.
68 * In case of an exception return 3. Otherwise return result of bitwise OR of
69 * resulting condition code and DIAG return code. */
70 static inline int dia250(void *iob, int cmd)
72 register unsigned long reg2 asm ("2") = (unsigned long) iob;
74 struct dasd_diag_init_io init_io;
75 struct dasd_diag_rw_io rw_io;
87 : "+d" (rc), "=m" (*(addr_type *) iob)
88 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
93 /* Initialize block I/O to DIAG device using the specified blocksize and
94 * block offset. On success, return zero and set end_block to contain the
95 * number of blocks on the device minus the specified offset. Return non-zero
98 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
99 blocknum_t offset, blocknum_t *end_block)
101 struct dasd_diag_private *private;
102 struct dasd_diag_init_io *iib;
105 private = (struct dasd_diag_private *) device->private;
107 memset(iib, 0, sizeof (struct dasd_diag_init_io));
109 iib->dev_nr = private->dev_id.devno;
110 iib->block_size = blocksize;
111 iib->offset = offset;
112 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
114 rc = dia250(iib, INIT_BIO);
116 if ((rc & 3) == 0 && end_block)
117 *end_block = iib->end_block;
122 /* Remove block I/O environment for device. Return zero on success, non-zero
125 mdsk_term_io(struct dasd_device * device)
127 struct dasd_diag_private *private;
128 struct dasd_diag_init_io *iib;
131 private = (struct dasd_diag_private *) device->private;
133 memset(iib, 0, sizeof (struct dasd_diag_init_io));
134 iib->dev_nr = private->dev_id.devno;
135 rc = dia250(iib, TERM_BIO);
139 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
142 dasd_diag_erp(struct dasd_device *device)
146 mdsk_term_io(device);
147 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
149 dev_warn(&device->cdev->dev, "DIAG ERP failed with "
153 /* Start a given request at the device. Return zero on success, non-zero
156 dasd_start_diag(struct dasd_ccw_req * cqr)
158 struct dasd_device *device;
159 struct dasd_diag_private *private;
160 struct dasd_diag_req *dreq;
163 device = cqr->startdev;
164 if (cqr->retries < 0) {
165 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
166 "- no retry left)", cqr);
167 cqr->status = DASD_CQR_ERROR;
170 private = (struct dasd_diag_private *) device->private;
171 dreq = (struct dasd_diag_req *) cqr->data;
173 private->iob.dev_nr = private->dev_id.devno;
174 private->iob.key = 0;
175 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
176 private->iob.block_count = dreq->block_count;
177 private->iob.interrupt_params = (addr_t) cqr;
178 private->iob.bio_list = dreq->bio;
179 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
181 cqr->startclk = get_clock();
182 cqr->starttime = jiffies;
185 rc = dia250(&private->iob, RW_BIO);
187 case 0: /* Synchronous I/O finished successfully */
188 cqr->stopclk = get_clock();
189 cqr->status = DASD_CQR_SUCCESS;
190 /* Indicate to calling function that only a dasd_schedule_bh()
191 and no timer is needed */
194 case 8: /* Asynchronous I/O was started */
195 cqr->status = DASD_CQR_IN_IO;
198 default: /* Error condition */
199 cqr->status = DASD_CQR_QUEUED;
200 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
201 dasd_diag_erp(device);
209 /* Terminate given request at the device. */
211 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
213 struct dasd_device *device;
215 device = cqr->startdev;
216 mdsk_term_io(device);
217 mdsk_init_io(device, device->block->bp_block, 0, NULL);
218 cqr->status = DASD_CQR_CLEAR_PENDING;
219 cqr->stopclk = get_clock();
220 dasd_schedule_device_bh(device);
224 /* Handle external interruption. */
226 dasd_ext_handler(__u16 code)
228 struct dasd_ccw_req *cqr, *next;
229 struct dasd_device *device;
230 unsigned long long expires;
236 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
237 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
239 case DASD_DIAG_CODE_31BIT:
240 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
242 case DASD_DIAG_CODE_64BIT:
243 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
248 if (!ip) { /* no intparm: unsolicited interrupt */
249 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
253 cqr = (struct dasd_ccw_req *) ip;
254 device = (struct dasd_device *) cqr->startdev;
255 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
256 DBF_DEV_EVENT(DBF_WARNING, device,
257 " magic number of dasd_ccw_req 0x%08X doesn't"
258 " match discipline 0x%08X",
259 cqr->magic, *(int *) (&device->discipline->name));
263 /* get irq lock to modify request queue */
264 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
266 /* Check for a pending clear operation */
267 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
268 cqr->status = DASD_CQR_CLEARED;
269 dasd_device_clear_timer(device);
270 dasd_schedule_device_bh(device);
271 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
275 cqr->stopclk = get_clock();
279 cqr->status = DASD_CQR_SUCCESS;
280 /* Start first request on queue if possible -> fast_io. */
281 if (!list_empty(&device->ccw_queue)) {
282 next = list_entry(device->ccw_queue.next,
283 struct dasd_ccw_req, devlist);
284 if (next->status == DASD_CQR_QUEUED) {
285 rc = dasd_start_diag(next);
287 expires = next->expires;
291 cqr->status = DASD_CQR_QUEUED;
292 DBF_DEV_EVENT(DBF_DEBUG, 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 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
326 "Allocating memory for private DASD data "
330 ccw_device_get_id(device->cdev, &private->dev_id);
331 device->private = (void *) private;
333 block = dasd_alloc_block();
335 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
336 "could not allocate dasd block structure");
337 device->private = NULL;
339 return PTR_ERR(block);
341 device->block = block;
342 block->base = device;
344 /* Read Device Characteristics */
345 rdc_data = (void *) &(private->rdc_data);
346 rdc_data->dev_nr = private->dev_id.devno;
347 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
349 rc = diag210((struct diag210 *) rdc_data);
351 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
352 "information (rc=%d)", rc);
357 /* Figure out position of label block */
358 switch (private->rdc_data.vdev_class) {
360 private->pt_block = 1;
363 private->pt_block = 2;
366 dev_warn(&device->cdev->dev, "Device type %d is not supported "
367 "in DIAG mode\n", 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 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
385 "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;
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 = &bio;
405 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
406 rc = dia250(&private->iob, RW_BIO);
408 dev_warn(&device->cdev->dev,
409 "A 64-bit DIAG call failed\n");
413 mdsk_term_io(device);
417 if (bsize > PAGE_SIZE) {
418 dev_warn(&device->cdev->dev, "Accessing the DASD failed because"
419 " of an incorrect format (rc=%d)\n", rc);
423 /* check for label block */
424 if (memcmp(label->label_id, DASD_DIAG_CMS1,
425 sizeof(DASD_DIAG_CMS1)) == 0) {
426 /* get formatted blocksize from label block */
427 bsize = (unsigned int) label->block_size;
428 block->blocks = (unsigned long) label->block_count;
430 block->blocks = end_block;
431 block->bp_block = bsize;
432 block->s2b_shift = 0; /* bits to shift 512 to get a block */
433 for (sb = 512; sb < bsize; sb = sb << 1)
435 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
437 dev_warn(&device->cdev->dev, "DIAG initialization "
438 "failed with rc=%d\n", rc);
441 dev_info(&device->cdev->dev,
442 "New DASD with %ld byte/block, total size %ld KB\n",
443 (unsigned long) block->bp_block,
444 (unsigned long) (block->blocks <<
445 block->s2b_shift) >> 1);
448 free_page((long) label);
451 device->block = NULL;
452 dasd_free_block(block);
453 device->private = NULL;
459 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
462 dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
464 if (dasd_check_blocksize(block->bp_block) != 0)
466 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
468 geo->sectors = 128 >> block->s2b_shift;
473 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
475 return dasd_default_erp_action;
479 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
481 return dasd_default_erp_postaction;
484 /* Create DASD request from block device request. Return pointer to new
485 * request on success, ERR_PTR otherwise. */
486 static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
487 struct dasd_block *block,
490 struct dasd_ccw_req *cqr;
491 struct dasd_diag_req *dreq;
492 struct dasd_diag_bio *dbio;
493 struct req_iterator iter;
496 unsigned int count, datasize;
497 sector_t recid, first_rec, last_rec;
498 unsigned int blksize, off;
499 unsigned char rw_cmd;
501 if (rq_data_dir(req) == READ)
502 rw_cmd = MDSK_READ_REQ;
503 else if (rq_data_dir(req) == WRITE)
504 rw_cmd = MDSK_WRITE_REQ;
506 return ERR_PTR(-EINVAL);
507 blksize = block->bp_block;
508 /* Calculate record id of first and last block. */
509 first_rec = blk_rq_pos(req) >> block->s2b_shift;
511 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
512 /* Check struct bio and count the number of blocks for the request. */
514 rq_for_each_segment(bv, req, iter) {
515 if (bv->bv_len & (blksize - 1))
516 /* Fba can only do full blocks. */
517 return ERR_PTR(-EINVAL);
518 count += bv->bv_len >> (block->s2b_shift + 9);
521 if (count != last_rec - first_rec + 1)
522 return ERR_PTR(-EINVAL);
523 /* Build the request */
524 datasize = sizeof(struct dasd_diag_req) +
525 count*sizeof(struct dasd_diag_bio);
526 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
531 dreq = (struct dasd_diag_req *) cqr->data;
532 dreq->block_count = count;
535 rq_for_each_segment(bv, req, iter) {
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;
547 cqr->retries = DIAG_MAX_RETRIES;
548 cqr->buildclk = get_clock();
549 if (blk_noretry_request(req) ||
550 block->base->features & DASD_FEATURE_FAILFAST)
551 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
552 cqr->startdev = memdev;
553 cqr->memdev = memdev;
555 cqr->expires = DIAG_TIMEOUT;
556 cqr->status = DASD_CQR_FILLED;
560 /* Release DASD request. Return non-zero if request was successful, zero
563 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
567 status = cqr->status == DASD_CQR_DONE;
568 dasd_sfree_request(cqr, cqr->memdev);
572 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
574 cqr->status = DASD_CQR_FILLED;
577 /* Fill in IOCTL data for device. */
579 dasd_diag_fill_info(struct dasd_device * device,
580 struct dasd_information2_t * info)
582 struct dasd_diag_private *private;
584 private = (struct dasd_diag_private *) device->private;
585 info->label_block = (unsigned int) private->pt_block;
586 info->FBA_layout = 1;
587 info->format = DASD_FORMAT_LDL;
588 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
589 memcpy(info->characteristics,
590 &((struct dasd_diag_private *) device->private)->rdc_data,
591 sizeof (struct dasd_diag_characteristics));
592 info->confdata_size = 0;
597 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
600 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
601 "dump sense not available for DIAG data");
604 static struct dasd_discipline dasd_diag_discipline = {
605 .owner = THIS_MODULE,
608 .max_blocks = DIAG_MAX_BLOCKS,
609 .check_device = dasd_diag_check_device,
610 .fill_geometry = dasd_diag_fill_geometry,
611 .start_IO = dasd_start_diag,
612 .term_IO = dasd_diag_term_IO,
613 .handle_terminated_request = dasd_diag_handle_terminated_request,
614 .erp_action = dasd_diag_erp_action,
615 .erp_postaction = dasd_diag_erp_postaction,
616 .build_cp = dasd_diag_build_cp,
617 .free_cp = dasd_diag_free_cp,
618 .dump_sense = dasd_diag_dump_sense,
619 .fill_info = dasd_diag_fill_info,
625 if (!MACHINE_IS_VM) {
626 pr_info("Discipline %s cannot be used without z/VM\n",
627 dasd_diag_discipline.name);
630 ASCEBC(dasd_diag_discipline.ebcname, 4);
633 register_external_interrupt(0x2603, dasd_ext_handler);
634 dasd_diag_discipline_pointer = &dasd_diag_discipline;
639 dasd_diag_cleanup(void)
641 unregister_external_interrupt(0x2603, dasd_ext_handler);
643 dasd_diag_discipline_pointer = NULL;
646 module_init(dasd_diag_init);
647 module_exit(dasd_diag_cleanup);