initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / s390 / block / dasd_diag.c
blob8174ec92176954864696ead10b799b6a42238fd9
1 /*
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
9 */
11 #define KMSG_COMPONENT "dasd-diag"
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>
22 #include <asm/dasd.h>
23 #include <asm/debug.h>
24 #include <asm/ebcdic.h>
25 #include <asm/io.h>
26 #include <asm/s390_ext.h>
27 #include <asm/todclk.h>
28 #include <asm/vtoc.h>
29 #include <asm/diag.h>
31 #include "dasd_int.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
42 * finishes. */
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;
55 blocknum_t pt_block;
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;
73 typedef union {
74 struct dasd_diag_init_io init_io;
75 struct dasd_diag_rw_io rw_io;
76 } addr_type;
77 int rc;
79 rc = 3;
80 asm volatile(
81 " diag 2,%2,0x250\n"
82 "0: ipm %0\n"
83 " srl %0,28\n"
84 " or %0,3\n"
85 "1:\n"
86 EX_TABLE(0b,1b)
87 : "+d" (rc), "=m" (*(addr_type *) iob)
88 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
89 : "3", "cc");
90 return rc;
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
96 * otherwise. */
97 static inline int
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;
103 int rc;
105 private = (struct dasd_diag_private *) device->private;
106 iib = &private->iib;
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;
119 return rc;
122 /* Remove block I/O environment for device. Return zero on success, non-zero
123 * otherwise. */
124 static inline int
125 mdsk_term_io(struct dasd_device * device)
127 struct dasd_diag_private *private;
128 struct dasd_diag_init_io *iib;
129 int rc;
131 private = (struct dasd_diag_private *) device->private;
132 iib = &private->iib;
133 memset(iib, 0, sizeof (struct dasd_diag_init_io));
134 iib->dev_nr = private->dev_id.devno;
135 rc = dia250(iib, TERM_BIO);
136 return rc;
139 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
140 * environment. */
141 static void
142 dasd_diag_erp(struct dasd_device *device)
144 int rc;
146 mdsk_term_io(device);
147 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
148 if (rc == 4) {
149 if (!(device->features & DASD_FEATURE_READONLY)) {
150 dev_warn(&device->cdev->dev,
151 "The access mode of a DIAG device changed"
152 " to read-only");
153 device->features |= DASD_FEATURE_READONLY;
155 rc = 0;
157 if (rc)
158 dev_warn(&device->cdev->dev, "DIAG ERP failed with "
159 "rc=%d\n", rc);
162 /* Start a given request at the device. Return zero on success, non-zero
163 * otherwise. */
164 static int
165 dasd_start_diag(struct dasd_ccw_req * cqr)
167 struct dasd_device *device;
168 struct dasd_diag_private *private;
169 struct dasd_diag_req *dreq;
170 int rc;
172 device = cqr->startdev;
173 if (cqr->retries < 0) {
174 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
175 "- no retry left)", cqr);
176 cqr->status = DASD_CQR_ERROR;
177 return -EIO;
179 private = (struct dasd_diag_private *) device->private;
180 dreq = (struct dasd_diag_req *) cqr->data;
182 private->iob.dev_nr = private->dev_id.devno;
183 private->iob.key = 0;
184 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
185 private->iob.block_count = dreq->block_count;
186 private->iob.interrupt_params = (addr_t) cqr;
187 private->iob.bio_list = dreq->bio;
188 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
190 cqr->startclk = get_clock();
191 cqr->starttime = jiffies;
192 cqr->retries--;
194 rc = dia250(&private->iob, RW_BIO);
195 switch (rc) {
196 case 0: /* Synchronous I/O finished successfully */
197 cqr->stopclk = get_clock();
198 cqr->status = DASD_CQR_SUCCESS;
199 /* Indicate to calling function that only a dasd_schedule_bh()
200 and no timer is needed */
201 rc = -EACCES;
202 break;
203 case 8: /* Asynchronous I/O was started */
204 cqr->status = DASD_CQR_IN_IO;
205 rc = 0;
206 break;
207 default: /* Error condition */
208 cqr->status = DASD_CQR_QUEUED;
209 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
210 dasd_diag_erp(device);
211 rc = -EIO;
212 break;
214 cqr->intrc = rc;
215 return rc;
218 /* Terminate given request at the device. */
219 static int
220 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
222 struct dasd_device *device;
224 device = cqr->startdev;
225 mdsk_term_io(device);
226 mdsk_init_io(device, device->block->bp_block, 0, NULL);
227 cqr->status = DASD_CQR_CLEAR_PENDING;
228 cqr->stopclk = get_clock();
229 dasd_schedule_device_bh(device);
230 return 0;
233 /* Handle external interruption. */
234 static void
235 dasd_ext_handler(__u16 code)
237 struct dasd_ccw_req *cqr, *next;
238 struct dasd_device *device;
239 unsigned long long expires;
240 unsigned long flags;
241 u8 int_code, status;
242 addr_t ip;
243 int rc;
245 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
246 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
247 switch (int_code) {
248 case DASD_DIAG_CODE_31BIT:
249 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
250 break;
251 case DASD_DIAG_CODE_64BIT:
252 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
253 break;
254 default:
255 return;
257 if (!ip) { /* no intparm: unsolicited interrupt */
258 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
259 "interrupt");
260 return;
262 cqr = (struct dasd_ccw_req *) ip;
263 device = (struct dasd_device *) cqr->startdev;
264 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
265 DBF_DEV_EVENT(DBF_WARNING, device,
266 " magic number of dasd_ccw_req 0x%08X doesn't"
267 " match discipline 0x%08X",
268 cqr->magic, *(int *) (&device->discipline->name));
269 return;
272 /* get irq lock to modify request queue */
273 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
275 /* Check for a pending clear operation */
276 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
277 cqr->status = DASD_CQR_CLEARED;
278 dasd_device_clear_timer(device);
279 dasd_schedule_device_bh(device);
280 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
281 return;
284 cqr->stopclk = get_clock();
286 expires = 0;
287 if (status == 0) {
288 cqr->status = DASD_CQR_SUCCESS;
289 /* Start first request on queue if possible -> fast_io. */
290 if (!list_empty(&device->ccw_queue)) {
291 next = list_entry(device->ccw_queue.next,
292 struct dasd_ccw_req, devlist);
293 if (next->status == DASD_CQR_QUEUED) {
294 rc = dasd_start_diag(next);
295 if (rc == 0)
296 expires = next->expires;
299 } else {
300 cqr->status = DASD_CQR_QUEUED;
301 DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
302 "request %p was %d (%d retries left)", cqr, status,
303 cqr->retries);
304 dasd_diag_erp(device);
307 if (expires != 0)
308 dasd_device_set_timer(device, expires);
309 else
310 dasd_device_clear_timer(device);
311 dasd_schedule_device_bh(device);
313 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
316 /* Check whether device can be controlled by DIAG discipline. Return zero on
317 * success, non-zero otherwise. */
318 static int
319 dasd_diag_check_device(struct dasd_device *device)
321 struct dasd_block *block;
322 struct dasd_diag_private *private;
323 struct dasd_diag_characteristics *rdc_data;
324 struct dasd_diag_bio bio;
325 struct vtoc_cms_label *label;
326 blocknum_t end_block;
327 unsigned int sb, bsize;
328 int rc;
330 private = (struct dasd_diag_private *) device->private;
331 if (private == NULL) {
332 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
333 if (private == NULL) {
334 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
335 "Allocating memory for private DASD data "
336 "failed\n");
337 return -ENOMEM;
339 ccw_device_get_id(device->cdev, &private->dev_id);
340 device->private = (void *) private;
342 block = dasd_alloc_block();
343 if (IS_ERR(block)) {
344 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
345 "could not allocate dasd block structure");
346 device->private = NULL;
347 kfree(private);
348 return PTR_ERR(block);
350 device->block = block;
351 block->base = device;
353 /* Read Device Characteristics */
354 rdc_data = (void *) &(private->rdc_data);
355 rdc_data->dev_nr = private->dev_id.devno;
356 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
358 rc = diag210((struct diag210 *) rdc_data);
359 if (rc) {
360 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
361 "information (rc=%d)", rc);
362 rc = -EOPNOTSUPP;
363 goto out;
366 /* Figure out position of label block */
367 switch (private->rdc_data.vdev_class) {
368 case DEV_CLASS_FBA:
369 private->pt_block = 1;
370 break;
371 case DEV_CLASS_ECKD:
372 private->pt_block = 2;
373 break;
374 default:
375 dev_warn(&device->cdev->dev, "Device type %d is not supported "
376 "in DIAG mode\n", private->rdc_data.vdev_class);
377 rc = -EOPNOTSUPP;
378 goto out;
381 DBF_DEV_EVENT(DBF_INFO, device,
382 "%04X: %04X on real %04X/%02X",
383 rdc_data->dev_nr,
384 rdc_data->vdev_type,
385 rdc_data->rdev_type, rdc_data->rdev_model);
387 /* terminate all outstanding operations */
388 mdsk_term_io(device);
390 /* figure out blocksize of device */
391 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
392 if (label == NULL) {
393 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
394 "No memory to allocate initialization request");
395 rc = -ENOMEM;
396 goto out;
398 rc = 0;
399 end_block = 0;
400 /* try all sizes - needed for ECKD devices */
401 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
402 mdsk_init_io(device, bsize, 0, &end_block);
403 memset(&bio, 0, sizeof (struct dasd_diag_bio));
404 bio.type = MDSK_READ_REQ;
405 bio.block_number = private->pt_block + 1;
406 bio.buffer = label;
407 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
408 private->iob.dev_nr = rdc_data->dev_nr;
409 private->iob.key = 0;
410 private->iob.flags = 0; /* do synchronous io */
411 private->iob.block_count = 1;
412 private->iob.interrupt_params = 0;
413 private->iob.bio_list = &bio;
414 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
415 rc = dia250(&private->iob, RW_BIO);
416 if (rc == 3) {
417 dev_warn(&device->cdev->dev,
418 "A 64-bit DIAG call failed\n");
419 rc = -EOPNOTSUPP;
420 goto out_label;
422 mdsk_term_io(device);
423 if (rc == 0)
424 break;
426 if (bsize > PAGE_SIZE) {
427 dev_warn(&device->cdev->dev, "Accessing the DASD failed because"
428 " of an incorrect format (rc=%d)\n", rc);
429 rc = -EIO;
430 goto out_label;
432 /* check for label block */
433 if (memcmp(label->label_id, DASD_DIAG_CMS1,
434 sizeof(DASD_DIAG_CMS1)) == 0) {
435 /* get formatted blocksize from label block */
436 bsize = (unsigned int) label->block_size;
437 block->blocks = (unsigned long) label->block_count;
438 } else
439 block->blocks = end_block;
440 block->bp_block = bsize;
441 block->s2b_shift = 0; /* bits to shift 512 to get a block */
442 for (sb = 512; sb < bsize; sb = sb << 1)
443 block->s2b_shift++;
444 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
445 if (rc && (rc != 4)) {
446 dev_warn(&device->cdev->dev, "DIAG initialization "
447 "failed with rc=%d\n", rc);
448 rc = -EIO;
449 } else {
450 if (rc == 4)
451 device->features |= DASD_FEATURE_READONLY;
452 dev_info(&device->cdev->dev,
453 "New DASD with %ld byte/block, total size %ld KB%s\n",
454 (unsigned long) block->bp_block,
455 (unsigned long) (block->blocks <<
456 block->s2b_shift) >> 1,
457 (rc == 4) ? ", read-only device" : "");
458 rc = 0;
460 out_label:
461 free_page((long) label);
462 out:
463 if (rc) {
464 device->block = NULL;
465 dasd_free_block(block);
466 device->private = NULL;
467 kfree(private);
469 return rc;
472 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
473 * otherwise. */
474 static int
475 dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
477 if (dasd_check_blocksize(block->bp_block) != 0)
478 return -EINVAL;
479 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
480 geo->heads = 16;
481 geo->sectors = 128 >> block->s2b_shift;
482 return 0;
485 static dasd_erp_fn_t
486 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
488 return dasd_default_erp_action;
491 static dasd_erp_fn_t
492 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
494 return dasd_default_erp_postaction;
497 /* Create DASD request from block device request. Return pointer to new
498 * request on success, ERR_PTR otherwise. */
499 static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
500 struct dasd_block *block,
501 struct request *req)
503 struct dasd_ccw_req *cqr;
504 struct dasd_diag_req *dreq;
505 struct dasd_diag_bio *dbio;
506 struct req_iterator iter;
507 struct bio_vec *bv;
508 char *dst;
509 unsigned int count, datasize;
510 sector_t recid, first_rec, last_rec;
511 unsigned int blksize, off;
512 unsigned char rw_cmd;
514 if (rq_data_dir(req) == READ)
515 rw_cmd = MDSK_READ_REQ;
516 else if (rq_data_dir(req) == WRITE)
517 rw_cmd = MDSK_WRITE_REQ;
518 else
519 return ERR_PTR(-EINVAL);
520 blksize = block->bp_block;
521 /* Calculate record id of first and last block. */
522 first_rec = blk_rq_pos(req) >> block->s2b_shift;
523 last_rec =
524 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
525 /* Check struct bio and count the number of blocks for the request. */
526 count = 0;
527 rq_for_each_segment(bv, req, iter) {
528 if (bv->bv_len & (blksize - 1))
529 /* Fba can only do full blocks. */
530 return ERR_PTR(-EINVAL);
531 count += bv->bv_len >> (block->s2b_shift + 9);
533 /* Paranoia. */
534 if (count != last_rec - first_rec + 1)
535 return ERR_PTR(-EINVAL);
536 /* Build the request */
537 datasize = sizeof(struct dasd_diag_req) +
538 count*sizeof(struct dasd_diag_bio);
539 cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, datasize, memdev);
540 if (IS_ERR(cqr))
541 return cqr;
543 dreq = (struct dasd_diag_req *) cqr->data;
544 dreq->block_count = count;
545 dbio = dreq->bio;
546 recid = first_rec;
547 rq_for_each_segment(bv, req, iter) {
548 dst = page_address(bv->bv_page) + bv->bv_offset;
549 for (off = 0; off < bv->bv_len; off += blksize) {
550 memset(dbio, 0, sizeof (struct dasd_diag_bio));
551 dbio->type = rw_cmd;
552 dbio->block_number = recid + 1;
553 dbio->buffer = dst;
554 dbio++;
555 dst += blksize;
556 recid++;
559 cqr->retries = DIAG_MAX_RETRIES;
560 cqr->buildclk = get_clock();
561 if (blk_noretry_request(req) ||
562 block->base->features & DASD_FEATURE_FAILFAST)
563 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
564 cqr->startdev = memdev;
565 cqr->memdev = memdev;
566 cqr->block = block;
567 cqr->expires = DIAG_TIMEOUT;
568 cqr->status = DASD_CQR_FILLED;
569 return cqr;
572 /* Release DASD request. Return non-zero if request was successful, zero
573 * otherwise. */
574 static int
575 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
577 int status;
579 status = cqr->status == DASD_CQR_DONE;
580 dasd_sfree_request(cqr, cqr->memdev);
581 return status;
584 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
586 cqr->status = DASD_CQR_FILLED;
589 /* Fill in IOCTL data for device. */
590 static int
591 dasd_diag_fill_info(struct dasd_device * device,
592 struct dasd_information2_t * info)
594 struct dasd_diag_private *private;
596 private = (struct dasd_diag_private *) device->private;
597 info->label_block = (unsigned int) private->pt_block;
598 info->FBA_layout = 1;
599 info->format = DASD_FORMAT_LDL;
600 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
601 memcpy(info->characteristics,
602 &((struct dasd_diag_private *) device->private)->rdc_data,
603 sizeof (struct dasd_diag_characteristics));
604 info->confdata_size = 0;
605 return 0;
608 static void
609 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
610 struct irb *stat)
612 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
613 "dump sense not available for DIAG data");
616 static struct dasd_discipline dasd_diag_discipline = {
617 .owner = THIS_MODULE,
618 .name = "DIAG",
619 .ebcname = "DIAG",
620 .max_blocks = DIAG_MAX_BLOCKS,
621 .check_device = dasd_diag_check_device,
622 .fill_geometry = dasd_diag_fill_geometry,
623 .start_IO = dasd_start_diag,
624 .term_IO = dasd_diag_term_IO,
625 .handle_terminated_request = dasd_diag_handle_terminated_request,
626 .erp_action = dasd_diag_erp_action,
627 .erp_postaction = dasd_diag_erp_postaction,
628 .build_cp = dasd_diag_build_cp,
629 .free_cp = dasd_diag_free_cp,
630 .dump_sense = dasd_diag_dump_sense,
631 .fill_info = dasd_diag_fill_info,
634 static int __init
635 dasd_diag_init(void)
637 if (!MACHINE_IS_VM) {
638 pr_info("Discipline %s cannot be used without z/VM\n",
639 dasd_diag_discipline.name);
640 return -ENODEV;
642 ASCEBC(dasd_diag_discipline.ebcname, 4);
644 ctl_set_bit(0, 9);
645 register_external_interrupt(0x2603, dasd_ext_handler);
646 dasd_diag_discipline_pointer = &dasd_diag_discipline;
647 return 0;
650 static void __exit
651 dasd_diag_cleanup(void)
653 unregister_external_interrupt(0x2603, dasd_ext_handler);
654 ctl_clear_bit(0, 9);
655 dasd_diag_discipline_pointer = NULL;
658 module_init(dasd_diag_init);
659 module_exit(dasd_diag_cleanup);