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>
31 #include "dasd_diag.h"
33 #define PRINTK_HEADER "dasd(diag):"
35 MODULE_LICENSE("GPL");
37 /* The maximum number of blocks per request (max_blocks) is dependent on the
38 * amount of storage that is available in the static I/O buffer for each
39 * device. Currently each device gets 2 pages. We want to fit two requests
40 * into the available memory so that we can immediately start the next if one
42 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
43 sizeof(struct dasd_diag_req)) / \
44 sizeof(struct dasd_diag_bio)) / 2)
45 #define DIAG_MAX_RETRIES 32
46 #define DIAG_TIMEOUT 50 * HZ
48 struct dasd_discipline dasd_diag_discipline;
50 struct dasd_diag_private {
51 struct dasd_diag_characteristics rdc_data;
52 struct dasd_diag_rw_io iob;
53 struct dasd_diag_init_io iib;
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. */
69 dia250(void *iob, int cmd)
72 struct dasd_diag_init_io init_io;
73 struct dasd_diag_rw_io rw_io;
86 ".section __ex_table,\"a\"\n"
98 ".section __ex_table,\"a\"\n"
103 : "=&d" (rc), "=m" (*(addr_type *) iob)
104 : "d" (cmd), "d" (iob), "m" (*(addr_type *) iob)
109 /* Initialize block I/O to DIAG device using the specified blocksize and
110 * block offset. On success, return zero and set end_block to contain the
111 * number of blocks on the device minus the specified offset. Return non-zero
113 static __inline__ int
114 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
115 blocknum_t offset, blocknum_t *end_block)
117 struct dasd_diag_private *private;
118 struct dasd_diag_init_io *iib;
121 private = (struct dasd_diag_private *) device->private;
123 memset(iib, 0, sizeof (struct dasd_diag_init_io));
125 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
126 iib->block_size = blocksize;
127 iib->offset = offset;
128 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
130 rc = dia250(iib, INIT_BIO);
132 if ((rc & 3) == 0 && end_block)
133 *end_block = iib->end_block;
138 /* Remove block I/O environment for device. Return zero on success, non-zero
140 static __inline__ int
141 mdsk_term_io(struct dasd_device * device)
143 struct dasd_diag_private *private;
144 struct dasd_diag_init_io *iib;
147 private = (struct dasd_diag_private *) device->private;
149 memset(iib, 0, sizeof (struct dasd_diag_init_io));
150 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
151 rc = dia250(iib, TERM_BIO);
155 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
158 dasd_diag_erp(struct dasd_device *device)
162 mdsk_term_io(device);
163 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
165 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
169 /* Start a given request at the device. Return zero on success, non-zero
172 dasd_start_diag(struct dasd_ccw_req * cqr)
174 struct dasd_device *device;
175 struct dasd_diag_private *private;
176 struct dasd_diag_req *dreq;
179 device = cqr->device;
180 if (cqr->retries < 0) {
181 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
182 "- no retry left)", cqr);
183 cqr->status = DASD_CQR_FAILED;
186 private = (struct dasd_diag_private *) device->private;
187 dreq = (struct dasd_diag_req *) cqr->data;
189 private->iob.dev_nr = _ccw_device_get_device_number(device->cdev);
190 private->iob.key = 0;
191 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
192 private->iob.block_count = dreq->block_count;
193 private->iob.interrupt_params = (addr_t) cqr;
194 private->iob.bio_list = dreq->bio;
195 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
197 cqr->startclk = get_clock();
198 cqr->starttime = jiffies;
201 rc = dia250(&private->iob, RW_BIO);
203 case 0: /* Synchronous I/O finished successfully */
204 cqr->stopclk = get_clock();
205 cqr->status = DASD_CQR_DONE;
206 /* Indicate to calling function that only a dasd_schedule_bh()
207 and no timer is needed */
210 case 8: /* Asynchronous I/O was started */
211 cqr->status = DASD_CQR_IN_IO;
214 default: /* Error condition */
215 cqr->status = DASD_CQR_QUEUED;
216 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
217 dasd_diag_erp(device);
224 /* Terminate given request at the device. */
226 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
228 struct dasd_device *device;
230 device = cqr->device;
231 mdsk_term_io(device);
232 mdsk_init_io(device, device->bp_block, 0, NULL);
233 cqr->status = DASD_CQR_CLEAR;
234 cqr->stopclk = get_clock();
235 dasd_schedule_bh(device);
239 /* Handle external interruption. */
241 dasd_ext_handler(struct pt_regs *regs, __u16 code)
243 struct dasd_ccw_req *cqr, *next;
244 struct dasd_device *device;
245 unsigned long long expires;
251 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
252 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
254 case DASD_DIAG_CODE_31BIT:
255 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
257 case DASD_DIAG_CODE_64BIT:
258 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
263 if (!ip) { /* no intparm: unsolicited interrupt */
264 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
267 cqr = (struct dasd_ccw_req *) ip;
268 device = (struct dasd_device *) cqr->device;
269 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
270 DEV_MESSAGE(KERN_WARNING, device,
271 " magic number of dasd_ccw_req 0x%08X doesn't"
272 " match discipline 0x%08X",
273 cqr->magic, *(int *) (&device->discipline->name));
277 /* get irq lock to modify request queue */
278 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
280 /* Check for a pending clear operation */
281 if (cqr->status == DASD_CQR_CLEAR) {
282 cqr->status = DASD_CQR_QUEUED;
283 dasd_clear_timer(device);
284 dasd_schedule_bh(device);
285 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
289 cqr->stopclk = get_clock();
293 cqr->status = DASD_CQR_DONE;
294 /* Start first request on queue if possible -> fast_io. */
295 if (!list_empty(&device->ccw_queue)) {
296 next = list_entry(device->ccw_queue.next,
297 struct dasd_ccw_req, list);
298 if (next->status == DASD_CQR_QUEUED) {
299 rc = dasd_start_diag(next);
301 expires = next->expires;
302 else if (rc != -EACCES)
303 DEV_MESSAGE(KERN_WARNING, device, "%s",
304 "Interrupt fastpath "
309 cqr->status = DASD_CQR_QUEUED;
310 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
311 "request %p was %d (%d retries left)", cqr, status,
313 dasd_diag_erp(device);
317 dasd_set_timer(device, expires);
319 dasd_clear_timer(device);
320 dasd_schedule_bh(device);
322 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
325 /* Check whether device can be controlled by DIAG discipline. Return zero on
326 * success, non-zero otherwise. */
328 dasd_diag_check_device(struct dasd_device *device)
330 struct dasd_diag_private *private;
331 struct dasd_diag_characteristics *rdc_data;
332 struct dasd_diag_bio bio;
333 struct vtoc_cms_label *label;
334 blocknum_t end_block;
335 unsigned int sb, bsize;
338 private = (struct dasd_diag_private *) device->private;
339 if (private == NULL) {
340 private = kmalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
341 if (private == NULL) {
342 DEV_MESSAGE(KERN_WARNING, device, "%s",
343 "memory allocation failed for private data");
346 device->private = (void *) private;
348 /* Read Device Characteristics */
349 rdc_data = (void *) &(private->rdc_data);
350 rdc_data->dev_nr = _ccw_device_get_device_number(device->cdev);
351 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
353 rc = diag210((struct diag210 *) rdc_data);
355 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
356 "information (rc=%d)", rc);
360 /* Figure out position of label block */
361 switch (private->rdc_data.vdev_class) {
363 private->pt_block = 1;
366 private->pt_block = 2;
369 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
370 "(class=%d)", private->rdc_data.vdev_class);
374 DBF_DEV_EVENT(DBF_INFO, device,
375 "%04X: %04X on real %04X/%02X",
378 rdc_data->rdev_type, rdc_data->rdev_model);
380 /* terminate all outstanding operations */
381 mdsk_term_io(device);
383 /* figure out blocksize of device */
384 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
386 DEV_MESSAGE(KERN_WARNING, device, "%s",
387 "No memory to allocate initialization request");
392 /* try all sizes - needed for ECKD devices */
393 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
394 mdsk_init_io(device, bsize, 0, &end_block);
395 memset(&bio, 0, sizeof (struct dasd_diag_bio));
396 bio.type = MDSK_READ_REQ;
397 bio.block_number = private->pt_block + 1;
399 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
400 private->iob.dev_nr = rdc_data->dev_nr;
401 private->iob.key = 0;
402 private->iob.flags = 0; /* do synchronous io */
403 private->iob.block_count = 1;
404 private->iob.interrupt_params = 0;
405 private->iob.bio_list = &bio;
406 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
407 rc = dia250(&private->iob, RW_BIO);
409 DEV_MESSAGE(KERN_WARNING, device, "%s",
414 mdsk_term_io(device);
418 if (bsize > PAGE_SIZE) {
419 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
424 /* check for label block */
425 if (memcmp(label->label_id, DASD_DIAG_CMS1,
426 sizeof(DASD_DIAG_CMS1)) == 0) {
427 /* get formatted blocksize from label block */
428 bsize = (unsigned int) label->block_size;
429 device->blocks = (unsigned long) label->block_count;
431 device->blocks = end_block;
432 device->bp_block = bsize;
433 device->s2b_shift = 0; /* bits to shift 512 to get a block */
434 for (sb = 512; sb < bsize; sb = sb << 1)
436 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
438 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
439 "failed (rc=%d)", rc);
442 DEV_MESSAGE(KERN_INFO, device,
443 "(%ld B/blk): %ldkB",
444 (unsigned long) device->bp_block,
445 (unsigned long) (device->blocks <<
446 device->s2b_shift) >> 1);
449 free_page((long) label);
453 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
456 dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
458 if (dasd_check_blocksize(device->bp_block) != 0)
460 geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
462 geo->sectors = 128 >> device->s2b_shift;
467 dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
469 return dasd_era_fatal;
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 *
487 dasd_diag_build_cp(struct dasd_device * device, struct request *req)
489 struct dasd_ccw_req *cqr;
490 struct dasd_diag_req *dreq;
491 struct dasd_diag_bio *dbio;
495 unsigned int count, datasize;
496 sector_t recid, first_rec, last_rec;
497 unsigned int blksize, off;
498 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 = device->bp_block;
508 /* Calculate record id of first and last block. */
509 first_rec = req->sector >> device->s2b_shift;
510 last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
511 /* Check struct bio and count the number of blocks for the request. */
513 rq_for_each_bio(bio, req) {
514 bio_for_each_segment(bv, bio, i) {
515 if (bv->bv_len & (blksize - 1))
516 /* Fba can only do full blocks. */
517 return ERR_PTR(-EINVAL);
518 count += bv->bv_len >> (device->s2b_shift + 9);
522 if (count != last_rec - first_rec + 1)
523 return ERR_PTR(-EINVAL);
524 /* Build the request */
525 datasize = sizeof(struct dasd_diag_req) +
526 count*sizeof(struct dasd_diag_bio);
527 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
532 dreq = (struct dasd_diag_req *) cqr->data;
533 dreq->block_count = count;
536 rq_for_each_bio(bio, req) {
537 bio_for_each_segment(bv, bio, i) {
538 dst = page_address(bv->bv_page) + bv->bv_offset;
539 for (off = 0; off < bv->bv_len; off += blksize) {
540 memset(dbio, 0, sizeof (struct dasd_diag_bio));
542 dbio->block_number = recid + 1;
550 cqr->retries = DIAG_MAX_RETRIES;
551 cqr->buildclk = get_clock();
552 if (req->flags & REQ_FAILFAST)
553 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
554 cqr->device = device;
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->device);
572 /* Fill in IOCTL data for device. */
574 dasd_diag_fill_info(struct dasd_device * device,
575 struct dasd_information2_t * info)
577 struct dasd_diag_private *private;
579 private = (struct dasd_diag_private *) device->private;
580 info->label_block = (unsigned int) private->pt_block;
581 info->FBA_layout = 1;
582 info->format = DASD_FORMAT_LDL;
583 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
584 memcpy(info->characteristics,
585 &((struct dasd_diag_private *) device->private)->rdc_data,
586 sizeof (struct dasd_diag_characteristics));
587 info->confdata_size = 0;
592 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
595 DEV_MESSAGE(KERN_ERR, device, "%s",
596 "dump sense not available for DIAG data");
599 struct dasd_discipline dasd_diag_discipline = {
600 .owner = THIS_MODULE,
603 .max_blocks = DIAG_MAX_BLOCKS,
604 .check_device = dasd_diag_check_device,
605 .fill_geometry = dasd_diag_fill_geometry,
606 .start_IO = dasd_start_diag,
607 .term_IO = dasd_diag_term_IO,
608 .examine_error = dasd_diag_examine_error,
609 .erp_action = dasd_diag_erp_action,
610 .erp_postaction = dasd_diag_erp_postaction,
611 .build_cp = dasd_diag_build_cp,
612 .free_cp = dasd_diag_free_cp,
613 .dump_sense = dasd_diag_dump_sense,
614 .fill_info = dasd_diag_fill_info,
620 if (!MACHINE_IS_VM) {
621 MESSAGE_LOG(KERN_INFO,
622 "Machine is not VM: %s "
623 "discipline not initializing",
624 dasd_diag_discipline.name);
627 ASCEBC(dasd_diag_discipline.ebcname, 4);
630 register_external_interrupt(0x2603, dasd_ext_handler);
631 dasd_diag_discipline_pointer = &dasd_diag_discipline;
636 dasd_diag_cleanup(void)
638 unregister_external_interrupt(0x2603, dasd_ext_handler);
640 dasd_diag_discipline_pointer = NULL;
643 module_init(dasd_diag_init);
644 module_exit(dasd_diag_cleanup);