Pull sbs into release branch
[linux-2.6] / drivers / s390 / block / dasd_diag.c
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
8  *
9  */
10
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>
19
20 #include <asm/dasd.h>
21 #include <asm/debug.h>
22 #include <asm/ebcdic.h>
23 #include <asm/io.h>
24 #include <asm/s390_ext.h>
25 #include <asm/todclk.h>
26 #include <asm/vtoc.h>
27
28 #include "dasd_int.h"
29 #include "dasd_diag.h"
30
31 #define PRINTK_HEADER "dasd(diag):"
32
33 MODULE_LICENSE("GPL");
34
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
39  * finishes. */
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
45
46 static struct dasd_discipline dasd_diag_discipline;
47
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;
52         blocknum_t pt_block;
53 };
54
55 struct dasd_diag_req {
56         unsigned int block_count;
57         struct dasd_diag_bio bio[0];
58 };
59
60 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
61
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)
67 {
68         register unsigned long reg2 asm ("2") = (unsigned long) iob;
69         typedef union {
70                 struct dasd_diag_init_io init_io;
71                 struct dasd_diag_rw_io rw_io;
72         } addr_type;
73         int rc;
74
75         rc = 3;
76         asm volatile(
77                 "       diag    2,%2,0x250\n"
78                 "0:     ipm     %0\n"
79                 "       srl     %0,28\n"
80                 "       or      %0,3\n"
81                 "1:\n"
82                 EX_TABLE(0b,1b)
83                 : "+d" (rc), "=m" (*(addr_type *) iob)
84                 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
85                 : "3", "cc");
86         return rc;
87 }
88
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
92  * otherwise. */
93 static inline int
94 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
95              blocknum_t offset, blocknum_t *end_block)
96 {
97         struct dasd_diag_private *private;
98         struct dasd_diag_init_io *iib;
99         int rc;
100
101         private = (struct dasd_diag_private *) device->private;
102         iib = &private->iib;
103         memset(iib, 0, sizeof (struct dasd_diag_init_io));
104
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;
109
110         rc = dia250(iib, INIT_BIO);
111
112         if ((rc & 3) == 0 && end_block)
113                 *end_block = iib->end_block;
114
115         return rc;
116 }
117
118 /* Remove block I/O environment for device. Return zero on success, non-zero
119  * otherwise. */
120 static inline int
121 mdsk_term_io(struct dasd_device * device)
122 {
123         struct dasd_diag_private *private;
124         struct dasd_diag_init_io *iib;
125         int rc;
126
127         private = (struct dasd_diag_private *) device->private;
128         iib = &private->iib;
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);
132         return rc;
133 }
134
135 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
136  * environment. */
137 static void
138 dasd_diag_erp(struct dasd_device *device)
139 {
140         int rc;
141
142         mdsk_term_io(device);
143         rc = mdsk_init_io(device, device->bp_block, 0, NULL);
144         if (rc)
145                 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
146                             "rc=%d", rc);
147 }
148
149 /* Start a given request at the device. Return zero on success, non-zero
150  * otherwise. */
151 static int
152 dasd_start_diag(struct dasd_ccw_req * cqr)
153 {
154         struct dasd_device *device;
155         struct dasd_diag_private *private;
156         struct dasd_diag_req *dreq;
157         int rc;
158
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;
164                 return -EIO;
165         }
166         private = (struct dasd_diag_private *) device->private;
167         dreq = (struct dasd_diag_req *) cqr->data;
168
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;
176
177         cqr->startclk = get_clock();
178         cqr->starttime = jiffies;
179         cqr->retries--;
180
181         rc = dia250(&private->iob, RW_BIO);
182         switch (rc) {
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 */
188                 rc = -EACCES;
189                 break;
190         case 8: /* Asynchronous I/O was started */
191                 cqr->status = DASD_CQR_IN_IO;
192                 rc = 0;
193                 break;
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);
198                 rc = -EIO;
199                 break;
200         }
201         return rc;
202 }
203
204 /* Terminate given request at the device. */
205 static int
206 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
207 {
208         struct dasd_device *device;
209
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);
216         return 0;
217 }
218
219 /* Handle external interruption. */
220 static void
221 dasd_ext_handler(__u16 code)
222 {
223         struct dasd_ccw_req *cqr, *next;
224         struct dasd_device *device;
225         unsigned long long expires;
226         unsigned long flags;
227         u8 int_code, status;
228         addr_t ip;
229         int rc;
230
231         int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
232         status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
233         switch (int_code) {
234         case DASD_DIAG_CODE_31BIT:
235                 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
236                 break;
237         case DASD_DIAG_CODE_64BIT:
238                 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
239                 break;
240         default:
241                 return;
242         }
243         if (!ip) {              /* no intparm: unsolicited interrupt */
244                 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
245                 return;
246         }
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));
254                 return;
255         }
256
257         /* get irq lock to modify request queue */
258         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
259
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);
266                 return;
267         }
268
269         cqr->stopclk = get_clock();
270
271         expires = 0;
272         if (status == 0) {
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);
280                                 if (rc == 0)
281                                         expires = next->expires;
282                                 else if (rc != -EACCES)
283                                         DEV_MESSAGE(KERN_WARNING, device, "%s",
284                                                     "Interrupt fastpath "
285                                                     "failed!");
286                         }
287                 }
288         } else {
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,
292                             cqr->retries);
293                 dasd_diag_erp(device);
294         }
295
296         if (expires != 0)
297                 dasd_set_timer(device, expires);
298         else
299                 dasd_clear_timer(device);
300         dasd_schedule_bh(device);
301
302         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
303 }
304
305 /* Check whether device can be controlled by DIAG discipline. Return zero on
306  * success, non-zero otherwise. */
307 static int
308 dasd_diag_check_device(struct dasd_device *device)
309 {
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;
316         int rc;
317
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");
324                         return -ENOMEM;
325                 }
326                 device->private = (void *) private;
327         }
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);
332
333         rc = diag210((struct diag210 *) rdc_data);
334         if (rc) {
335                 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
336                             "information (rc=%d)", rc);
337                 return -ENOTSUPP;
338         }
339
340         /* Figure out position of label block */
341         switch (private->rdc_data.vdev_class) {
342         case DEV_CLASS_FBA:
343                 private->pt_block = 1;
344                 break;
345         case DEV_CLASS_ECKD:
346                 private->pt_block = 2;
347                 break;
348         default:
349                 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
350                             "(class=%d)", private->rdc_data.vdev_class);
351                 return -ENOTSUPP;
352         }
353
354         DBF_DEV_EVENT(DBF_INFO, device,
355                       "%04X: %04X on real %04X/%02X",
356                       rdc_data->dev_nr,
357                       rdc_data->vdev_type,
358                       rdc_data->rdev_type, rdc_data->rdev_model);
359
360         /* terminate all outstanding operations */
361         mdsk_term_io(device);
362
363         /* figure out blocksize of device */
364         label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
365         if (label == NULL)  {
366                 DEV_MESSAGE(KERN_WARNING, device, "%s",
367                             "No memory to allocate initialization request");
368                 return -ENOMEM;
369         }
370         rc = 0;
371         end_block = 0;
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;
378                 bio.buffer = label;
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);
388                 if (rc == 3) {
389                         DEV_MESSAGE(KERN_WARNING, device, "%s",
390                                 "DIAG call failed");
391                         rc = -EOPNOTSUPP;
392                         goto out;
393                 }
394                 mdsk_term_io(device);
395                 if (rc == 0)
396                         break;
397         }
398         if (bsize > PAGE_SIZE) {
399                 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
400                             "(rc=%d)", rc);
401                 rc = -EIO;
402                 goto out;
403         }
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;
410         } else
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)
415                 device->s2b_shift++;
416         rc = mdsk_init_io(device, device->bp_block, 0, NULL);
417         if (rc) {
418                 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
419                         "failed (rc=%d)", rc);
420                 rc = -EIO;
421         } else {
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);
427         }
428 out:
429         free_page((long) label);
430         return rc;
431 }
432
433 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
434  * otherwise. */
435 static int
436 dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
437 {
438         if (dasd_check_blocksize(device->bp_block) != 0)
439                 return -EINVAL;
440         geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
441         geo->heads = 16;
442         geo->sectors = 128 >> device->s2b_shift;
443         return 0;
444 }
445
446 static dasd_era_t
447 dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
448 {
449         return dasd_era_fatal;
450 }
451
452 static dasd_erp_fn_t
453 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
454 {
455         return dasd_default_erp_action;
456 }
457
458 static dasd_erp_fn_t
459 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
460 {
461         return dasd_default_erp_postaction;
462 }
463
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)
468 {
469         struct dasd_ccw_req *cqr;
470         struct dasd_diag_req *dreq;
471         struct dasd_diag_bio *dbio;
472         struct bio *bio;
473         struct bio_vec *bv;
474         char *dst;
475         unsigned int count, datasize;
476         sector_t recid, first_rec, last_rec;
477         unsigned int blksize, off;
478         unsigned char rw_cmd;
479         int i;
480
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;
485         else
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. */
492         count = 0;
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);
499                 }
500         }
501         /* Paranoia. */
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,
508                                    datasize, device);
509         if (IS_ERR(cqr))
510                 return cqr;
511
512         dreq = (struct dasd_diag_req *) cqr->data;
513         dreq->block_count = count;
514         dbio = dreq->bio;
515         recid = first_rec;
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));
521                                 dbio->type = rw_cmd;
522                                 dbio->block_number = recid + 1;
523                                 dbio->buffer = dst;
524                                 dbio++;
525                                 dst += blksize;
526                                 recid++;
527                         }
528                 }
529         }
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;
537         return cqr;
538 }
539
540 /* Release DASD request. Return non-zero if request was successful, zero
541  * otherwise. */
542 static int
543 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
544 {
545         int status;
546
547         status = cqr->status == DASD_CQR_DONE;
548         dasd_sfree_request(cqr, cqr->device);
549         return status;
550 }
551
552 /* Fill in IOCTL data for device. */
553 static int
554 dasd_diag_fill_info(struct dasd_device * device,
555                     struct dasd_information2_t * info)
556 {
557         struct dasd_diag_private *private;
558
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;
568         return 0;
569 }
570
571 static void
572 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
573                      struct irb *stat)
574 {
575         DEV_MESSAGE(KERN_ERR, device, "%s",
576                     "dump sense not available for DIAG data");
577 }
578
579 static struct dasd_discipline dasd_diag_discipline = {
580         .owner = THIS_MODULE,
581         .name = "DIAG",
582         .ebcname = "DIAG",
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,
595 };
596
597 static int __init
598 dasd_diag_init(void)
599 {
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);
605                 return -ENODEV;
606         }
607         ASCEBC(dasd_diag_discipline.ebcname, 4);
608
609         ctl_set_bit(0, 9);
610         register_external_interrupt(0x2603, dasd_ext_handler);
611         dasd_diag_discipline_pointer = &dasd_diag_discipline;
612         return 0;
613 }
614
615 static void __exit
616 dasd_diag_cleanup(void)
617 {
618         unregister_external_interrupt(0x2603, dasd_ext_handler);
619         ctl_clear_bit(0, 9);
620         dasd_diag_discipline_pointer = NULL;
621 }
622
623 module_init(dasd_diag_init);
624 module_exit(dasd_diag_cleanup);