Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dasd_diag.c
Go to the documentation of this file.
1 /*
2  * Author(s)......: Holger Smolinski <[email protected]>
3  * Based on.......: linux/drivers/s390/block/mdisk.c
4  * ...............: by Hartmunt Penner <[email protected]>
5  * Bugreports.to..: <[email protected]>
6  * Copyright IBM Corp. 1999, 2000
7  *
8  */
9 
10 #define KMSG_COMPONENT "dasd"
11 
12 #include <linux/kernel_stat.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>
21 
22 #include <asm/dasd.h>
23 #include <asm/debug.h>
24 #include <asm/ebcdic.h>
25 #include <asm/io.h>
26 #include <asm/irq.h>
27 #include <asm/vtoc.h>
28 #include <asm/diag.h>
29 
30 #include "dasd_int.h"
31 #include "dasd_diag.h"
32 
33 #define PRINTK_HEADER "dasd(diag):"
34 
35 MODULE_LICENSE("GPL");
36 
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
41  * finishes. */
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
47 
48 static struct dasd_discipline dasd_diag_discipline;
49 
56 };
57 
58 struct dasd_diag_req {
59  unsigned int block_count;
60  struct dasd_diag_bio bio[0];
61 };
62 
63 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
64 
65 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
66  * and function code cmd.
67  * In case of an exception return 3. Otherwise return result of bitwise OR of
68  * resulting condition code and DIAG return code. */
69 static inline int dia250(void *iob, int cmd)
70 {
71  register unsigned long reg2 asm ("2") = (unsigned long) iob;
72  typedef union {
73  struct dasd_diag_init_io init_io;
74  struct dasd_diag_rw_io rw_io;
75  } addr_type;
76  int rc;
77 
78  rc = 3;
79  asm volatile(
80  " diag 2,%2,0x250\n"
81  "0: ipm %0\n"
82  " srl %0,28\n"
83  " or %0,3\n"
84  "1:\n"
85  EX_TABLE(0b,1b)
86  : "+d" (rc), "=m" (*(addr_type *) iob)
87  : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
88  : "3", "cc");
89  return rc;
90 }
91 
92 /* Initialize block I/O to DIAG device using the specified blocksize and
93  * block offset. On success, return zero and set end_block to contain the
94  * number of blocks on the device minus the specified offset. Return non-zero
95  * otherwise. */
96 static inline int
97 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
99 {
100  struct dasd_diag_private *private;
101  struct dasd_diag_init_io *iib;
102  int rc;
103 
104  private = (struct dasd_diag_private *) device->private;
105  iib = &private->iib;
106  memset(iib, 0, sizeof (struct dasd_diag_init_io));
107 
108  iib->dev_nr = private->dev_id.devno;
109  iib->block_size = blocksize;
110  iib->offset = offset;
112 
113  rc = dia250(iib, INIT_BIO);
114 
115  if ((rc & 3) == 0 && end_block)
116  *end_block = iib->end_block;
117 
118  return rc;
119 }
120 
121 /* Remove block I/O environment for device. Return zero on success, non-zero
122  * otherwise. */
123 static inline int
124 mdsk_term_io(struct dasd_device * device)
125 {
126  struct dasd_diag_private *private;
127  struct dasd_diag_init_io *iib;
128  int rc;
129 
130  private = (struct dasd_diag_private *) device->private;
131  iib = &private->iib;
132  memset(iib, 0, sizeof (struct dasd_diag_init_io));
133  iib->dev_nr = private->dev_id.devno;
134  rc = dia250(iib, TERM_BIO);
135  return rc;
136 }
137 
138 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
139  * environment. */
140 static void
141 dasd_diag_erp(struct dasd_device *device)
142 {
143  int rc;
144 
145  mdsk_term_io(device);
146  rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
147  if (rc == 4) {
148  if (!(test_and_set_bit(DASD_FLAG_DEVICE_RO, &device->flags)))
149  pr_warning("%s: The access mode of a DIAG device "
150  "changed to read-only\n",
151  dev_name(&device->cdev->dev));
152  rc = 0;
153  }
154  if (rc)
155  pr_warning("%s: DIAG ERP failed with "
156  "rc=%d\n", dev_name(&device->cdev->dev), rc);
157 }
158 
159 /* Start a given request at the device. Return zero on success, non-zero
160  * otherwise. */
161 static int
162 dasd_start_diag(struct dasd_ccw_req * cqr)
163 {
164  struct dasd_device *device;
165  struct dasd_diag_private *private;
166  struct dasd_diag_req *dreq;
167  int rc;
168 
169  device = cqr->startdev;
170  if (cqr->retries < 0) {
171  DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
172  "- no retry left)", cqr);
173  cqr->status = DASD_CQR_ERROR;
174  return -EIO;
175  }
176  private = (struct dasd_diag_private *) device->private;
177  dreq = (struct dasd_diag_req *) cqr->data;
178 
179  private->iob.dev_nr = private->dev_id.devno;
180  private->iob.key = 0;
181  private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
182  private->iob.block_count = dreq->block_count;
183  private->iob.interrupt_params = (addr_t) cqr;
184  private->iob.bio_list = dreq->bio;
185  private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
186 
187  cqr->startclk = get_clock();
188  cqr->starttime = jiffies;
189  cqr->retries--;
190 
191  rc = dia250(&private->iob, RW_BIO);
192  switch (rc) {
193  case 0: /* Synchronous I/O finished successfully */
194  cqr->stopclk = get_clock();
195  cqr->status = DASD_CQR_SUCCESS;
196  /* Indicate to calling function that only a dasd_schedule_bh()
197  and no timer is needed */
198  rc = -EACCES;
199  break;
200  case 8: /* Asynchronous I/O was started */
201  cqr->status = DASD_CQR_IN_IO;
202  rc = 0;
203  break;
204  default: /* Error condition */
205  cqr->status = DASD_CQR_QUEUED;
206  DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
207  dasd_diag_erp(device);
208  rc = -EIO;
209  break;
210  }
211  cqr->intrc = rc;
212  return rc;
213 }
214 
215 /* Terminate given request at the device. */
216 static int
217 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
218 {
219  struct dasd_device *device;
220 
221  device = cqr->startdev;
222  mdsk_term_io(device);
223  mdsk_init_io(device, device->block->bp_block, 0, NULL);
225  cqr->stopclk = get_clock();
226  dasd_schedule_device_bh(device);
227  return 0;
228 }
229 
230 /* Handle external interruption. */
231 static void dasd_ext_handler(struct ext_code ext_code,
232  unsigned int param32, unsigned long param64)
233 {
234  struct dasd_ccw_req *cqr, *next;
235  struct dasd_device *device;
236  unsigned long long expires;
237  unsigned long flags;
238  addr_t ip;
239  int rc;
240 
241  switch (ext_code.subcode >> 8) {
243  ip = (addr_t) param32;
244  break;
246  ip = (addr_t) param64;
247  break;
248  default:
249  return;
250  }
252  if (!ip) { /* no intparm: unsolicited interrupt */
253  DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
254  "interrupt");
255  return;
256  }
257  cqr = (struct dasd_ccw_req *) ip;
258  device = (struct dasd_device *) cqr->startdev;
259  if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
260  DBF_DEV_EVENT(DBF_WARNING, device,
261  " magic number of dasd_ccw_req 0x%08X doesn't"
262  " match discipline 0x%08X",
263  cqr->magic, *(int *) (&device->discipline->name));
264  return;
265  }
266 
267  /* get irq lock to modify request queue */
268  spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
269 
270  /* Check for a pending clear operation */
271  if (cqr->status == DASD_CQR_CLEAR_PENDING) {
272  cqr->status = DASD_CQR_CLEARED;
273  dasd_device_clear_timer(device);
274  dasd_schedule_device_bh(device);
275  spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
276  return;
277  }
278 
279  cqr->stopclk = get_clock();
280 
281  expires = 0;
282  if ((ext_code.subcode & 0xff) == 0) {
283  cqr->status = DASD_CQR_SUCCESS;
284  /* Start first request on queue if possible -> fast_io. */
285  if (!list_empty(&device->ccw_queue)) {
286  next = list_entry(device->ccw_queue.next,
287  struct dasd_ccw_req, devlist);
288  if (next->status == DASD_CQR_QUEUED) {
289  rc = dasd_start_diag(next);
290  if (rc == 0)
291  expires = next->expires;
292  }
293  }
294  } else {
295  cqr->status = DASD_CQR_QUEUED;
296  DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
297  "request %p was %d (%d retries left)", cqr,
298  ext_code.subcode & 0xff, cqr->retries);
299  dasd_diag_erp(device);
300  }
301 
302  if (expires != 0)
303  dasd_device_set_timer(device, expires);
304  else
305  dasd_device_clear_timer(device);
306  dasd_schedule_device_bh(device);
307 
308  spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
309 }
310 
311 /* Check whether device can be controlled by DIAG discipline. Return zero on
312  * success, non-zero otherwise. */
313 static int
314 dasd_diag_check_device(struct dasd_device *device)
315 {
316  struct dasd_block *block;
317  struct dasd_diag_private *private;
318  struct dasd_diag_characteristics *rdc_data;
319  struct dasd_diag_bio bio;
320  struct vtoc_cms_label *label;
322  unsigned int sb, bsize;
323  int rc;
324 
325  private = (struct dasd_diag_private *) device->private;
326  if (private == NULL) {
327  private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
328  if (private == NULL) {
329  DBF_DEV_EVENT(DBF_WARNING, device, "%s",
330  "Allocating memory for private DASD data "
331  "failed\n");
332  return -ENOMEM;
333  }
334  ccw_device_get_id(device->cdev, &private->dev_id);
335  device->private = (void *) private;
336  }
337  block = dasd_alloc_block();
338  if (IS_ERR(block)) {
339  DBF_DEV_EVENT(DBF_WARNING, device, "%s",
340  "could not allocate dasd block structure");
341  device->private = NULL;
342  kfree(private);
343  return PTR_ERR(block);
344  }
345  device->block = block;
346  block->base = device;
347 
348  /* Read Device Characteristics */
349  rdc_data = (void *) &(private->rdc_data);
350  rdc_data->dev_nr = private->dev_id.devno;
351  rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
352 
353  rc = diag210((struct diag210 *) rdc_data);
354  if (rc) {
355  DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
356  "information (rc=%d)", rc);
357  rc = -EOPNOTSUPP;
358  goto out;
359  }
360 
361  device->default_expires = DIAG_TIMEOUT;
362 
363  /* Figure out position of label block */
364  switch (private->rdc_data.vdev_class) {
365  case DEV_CLASS_FBA:
366  private->pt_block = 1;
367  break;
368  case DEV_CLASS_ECKD:
369  private->pt_block = 2;
370  break;
371  default:
372  pr_warning("%s: Device type %d is not supported "
373  "in DIAG mode\n", dev_name(&device->cdev->dev),
374  private->rdc_data.vdev_class);
375  rc = -EOPNOTSUPP;
376  goto out;
377  }
378 
379  DBF_DEV_EVENT(DBF_INFO, device,
380  "%04X: %04X on real %04X/%02X",
381  rdc_data->dev_nr,
382  rdc_data->vdev_type,
383  rdc_data->rdev_type, rdc_data->rdev_model);
384 
385  /* terminate all outstanding operations */
386  mdsk_term_io(device);
387 
388  /* figure out blocksize of device */
389  label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
390  if (label == NULL) {
391  DBF_DEV_EVENT(DBF_WARNING, device, "%s",
392  "No memory to allocate initialization request");
393  rc = -ENOMEM;
394  goto out;
395  }
396  rc = 0;
397  end_block = 0;
398  /* try all sizes - needed for ECKD devices */
399  for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
400  mdsk_init_io(device, bsize, 0, &end_block);
401  memset(&bio, 0, sizeof (struct dasd_diag_bio));
402  bio.type = MDSK_READ_REQ;
403  bio.block_number = private->pt_block + 1;
404  bio.buffer = label;
405  memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
406  private->iob.dev_nr = rdc_data->dev_nr;
407  private->iob.key = 0;
408  private->iob.flags = 0; /* do synchronous io */
409  private->iob.block_count = 1;
410  private->iob.interrupt_params = 0;
411  private->iob.bio_list = &bio;
412  private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
413  rc = dia250(&private->iob, RW_BIO);
414  if (rc == 3) {
415  pr_warning("%s: A 64-bit DIAG call failed\n",
416  dev_name(&device->cdev->dev));
417  rc = -EOPNOTSUPP;
418  goto out_label;
419  }
420  mdsk_term_io(device);
421  if (rc == 0)
422  break;
423  }
424  if (bsize > PAGE_SIZE) {
425  pr_warning("%s: Accessing the DASD failed because of an "
426  "incorrect format (rc=%d)\n",
427  dev_name(&device->cdev->dev), rc);
428  rc = -EIO;
429  goto out_label;
430  }
431  /* check for label block */
432  if (memcmp(label->label_id, DASD_DIAG_CMS1,
433  sizeof(DASD_DIAG_CMS1)) == 0) {
434  /* get formatted blocksize from label block */
435  bsize = (unsigned int) label->block_size;
436  block->blocks = (unsigned long) label->block_count;
437  } else
438  block->blocks = end_block;
439  block->bp_block = bsize;
440  block->s2b_shift = 0; /* bits to shift 512 to get a block */
441  for (sb = 512; sb < bsize; sb = sb << 1)
442  block->s2b_shift++;
443  rc = mdsk_init_io(device, block->bp_block, 0, NULL);
444  if (rc && (rc != 4)) {
445  pr_warning("%s: DIAG initialization failed with rc=%d\n",
446  dev_name(&device->cdev->dev), rc);
447  rc = -EIO;
448  } else {
449  if (rc == 4)
450  set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
451  pr_info("%s: New DASD with %ld byte/block, total size %ld "
452  "KB%s\n", dev_name(&device->cdev->dev),
453  (unsigned long) block->bp_block,
454  (unsigned long) (block->blocks <<
455  block->s2b_shift) >> 1,
456  (rc == 4) ? ", read-only device" : "");
457  rc = 0;
458  }
459 out_label:
460  free_page((long) label);
461 out:
462  if (rc) {
463  device->block = NULL;
464  dasd_free_block(block);
465  device->private = NULL;
466  kfree(private);
467  }
468  return rc;
469 }
470 
471 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
472  * otherwise. */
473 static int
474 dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
475 {
476  if (dasd_check_blocksize(block->bp_block) != 0)
477  return -EINVAL;
478  geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
479  geo->heads = 16;
480  geo->sectors = 128 >> block->s2b_shift;
481  return 0;
482 }
483 
484 static dasd_erp_fn_t
485 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
486 {
488 }
489 
490 static dasd_erp_fn_t
491 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
492 {
494 }
495 
496 /* Create DASD request from block device request. Return pointer to new
497  * request on success, ERR_PTR otherwise. */
498 static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
499  struct dasd_block *block,
500  struct request *req)
501 {
502  struct dasd_ccw_req *cqr;
503  struct dasd_diag_req *dreq;
504  struct dasd_diag_bio *dbio;
505  struct req_iterator iter;
506  struct bio_vec *bv;
507  char *dst;
508  unsigned int count, datasize;
509  sector_t recid, first_rec, last_rec;
510  unsigned int blksize, off;
511  unsigned char rw_cmd;
512 
513  if (rq_data_dir(req) == READ)
514  rw_cmd = MDSK_READ_REQ;
515  else if (rq_data_dir(req) == WRITE)
516  rw_cmd = MDSK_WRITE_REQ;
517  else
518  return ERR_PTR(-EINVAL);
519  blksize = block->bp_block;
520  /* Calculate record id of first and last block. */
521  first_rec = blk_rq_pos(req) >> block->s2b_shift;
522  last_rec =
523  (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
524  /* Check struct bio and count the number of blocks for the request. */
525  count = 0;
526  rq_for_each_segment(bv, req, iter) {
527  if (bv->bv_len & (blksize - 1))
528  /* Fba can only do full blocks. */
529  return ERR_PTR(-EINVAL);
530  count += bv->bv_len >> (block->s2b_shift + 9);
531  }
532  /* Paranoia. */
533  if (count != last_rec - first_rec + 1)
534  return ERR_PTR(-EINVAL);
535  /* Build the request */
536  datasize = sizeof(struct dasd_diag_req) +
537  count*sizeof(struct dasd_diag_bio);
538  cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, datasize, memdev);
539  if (IS_ERR(cqr))
540  return cqr;
541 
542  dreq = (struct dasd_diag_req *) cqr->data;
543  dreq->block_count = count;
544  dbio = dreq->bio;
545  recid = first_rec;
546  rq_for_each_segment(bv, req, iter) {
547  dst = page_address(bv->bv_page) + bv->bv_offset;
548  for (off = 0; off < bv->bv_len; off += blksize) {
549  memset(dbio, 0, sizeof (struct dasd_diag_bio));
550  dbio->type = rw_cmd;
551  dbio->block_number = recid + 1;
552  dbio->buffer = dst;
553  dbio++;
554  dst += blksize;
555  recid++;
556  }
557  }
558  cqr->retries = DIAG_MAX_RETRIES;
559  cqr->buildclk = get_clock();
560  if (blk_noretry_request(req) ||
561  block->base->features & DASD_FEATURE_FAILFAST)
562  set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
563  cqr->startdev = memdev;
564  cqr->memdev = memdev;
565  cqr->block = block;
566  cqr->expires = memdev->default_expires * HZ;
567  cqr->status = DASD_CQR_FILLED;
568  return cqr;
569 }
570 
571 /* Release DASD request. Return non-zero if request was successful, zero
572  * otherwise. */
573 static int
574 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
575 {
576  int status;
577 
578  status = cqr->status == DASD_CQR_DONE;
579  dasd_sfree_request(cqr, cqr->memdev);
580  return status;
581 }
582 
583 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
584 {
585  cqr->status = DASD_CQR_FILLED;
586 };
587 
588 /* Fill in IOCTL data for device. */
589 static int
590 dasd_diag_fill_info(struct dasd_device * device,
591  struct dasd_information2_t * info)
592 {
593  struct dasd_diag_private *private;
594 
595  private = (struct dasd_diag_private *) device->private;
596  info->label_block = (unsigned int) private->pt_block;
597  info->FBA_layout = 1;
598  info->format = DASD_FORMAT_LDL;
599  info->characteristics_size = sizeof (struct dasd_diag_characteristics);
600  memcpy(info->characteristics,
601  &((struct dasd_diag_private *) device->private)->rdc_data,
603  info->confdata_size = 0;
604  return 0;
605 }
606 
607 static void
608 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
609  struct irb *stat)
610 {
611  DBF_DEV_EVENT(DBF_WARNING, device, "%s",
612  "dump sense not available for DIAG data");
613 }
614 
615 static struct dasd_discipline dasd_diag_discipline = {
616  .owner = THIS_MODULE,
617  .name = "DIAG",
618  .ebcname = "DIAG",
619  .max_blocks = DIAG_MAX_BLOCKS,
620  .check_device = dasd_diag_check_device,
621  .verify_path = dasd_generic_verify_path,
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,
632 };
633 
634 static int __init
635 dasd_diag_init(void)
636 {
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;
641  }
642  ASCEBC(dasd_diag_discipline.ebcname, 4);
643 
645  register_external_interrupt(0x2603, dasd_ext_handler);
646  dasd_diag_discipline_pointer = &dasd_diag_discipline;
647  return 0;
648 }
649 
650 static void __exit
651 dasd_diag_cleanup(void)
652 {
653  unregister_external_interrupt(0x2603, dasd_ext_handler);
656 }
657 
658 module_init(dasd_diag_init);
659 module_exit(dasd_diag_cleanup);