Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[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         struct ccw_dev_id dev_id;
54 };
55
56 struct dasd_diag_req {
57         unsigned int block_count;
58         struct dasd_diag_bio bio[0];
59 };
60
61 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
62
63 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
64  * and function code cmd.
65  * In case of an exception return 3. Otherwise return result of bitwise OR of
66  * resulting condition code and DIAG return code. */
67 static inline int dia250(void *iob, int cmd)
68 {
69         register unsigned long reg2 asm ("2") = (unsigned long) iob;
70         typedef union {
71                 struct dasd_diag_init_io init_io;
72                 struct dasd_diag_rw_io rw_io;
73         } addr_type;
74         int rc;
75
76         rc = 3;
77         asm volatile(
78                 "       diag    2,%2,0x250\n"
79                 "0:     ipm     %0\n"
80                 "       srl     %0,28\n"
81                 "       or      %0,3\n"
82                 "1:\n"
83                 EX_TABLE(0b,1b)
84                 : "+d" (rc), "=m" (*(addr_type *) iob)
85                 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
86                 : "3", "cc");
87         return rc;
88 }
89
90 /* Initialize block I/O to DIAG device using the specified blocksize and
91  * block offset. On success, return zero and set end_block to contain the
92  * number of blocks on the device minus the specified offset. Return non-zero
93  * otherwise. */
94 static inline int
95 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
96              blocknum_t offset, blocknum_t *end_block)
97 {
98         struct dasd_diag_private *private;
99         struct dasd_diag_init_io *iib;
100         int rc;
101
102         private = (struct dasd_diag_private *) device->private;
103         iib = &private->iib;
104         memset(iib, 0, sizeof (struct dasd_diag_init_io));
105
106         iib->dev_nr = private->dev_id.devno;
107         iib->block_size = blocksize;
108         iib->offset = offset;
109         iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
110
111         rc = dia250(iib, INIT_BIO);
112
113         if ((rc & 3) == 0 && end_block)
114                 *end_block = iib->end_block;
115
116         return rc;
117 }
118
119 /* Remove block I/O environment for device. Return zero on success, non-zero
120  * otherwise. */
121 static inline int
122 mdsk_term_io(struct dasd_device * device)
123 {
124         struct dasd_diag_private *private;
125         struct dasd_diag_init_io *iib;
126         int rc;
127
128         private = (struct dasd_diag_private *) device->private;
129         iib = &private->iib;
130         memset(iib, 0, sizeof (struct dasd_diag_init_io));
131         iib->dev_nr = private->dev_id.devno;
132         rc = dia250(iib, TERM_BIO);
133         return rc;
134 }
135
136 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
137  * environment. */
138 static void
139 dasd_diag_erp(struct dasd_device *device)
140 {
141         int rc;
142
143         mdsk_term_io(device);
144         rc = mdsk_init_io(device, device->bp_block, 0, NULL);
145         if (rc)
146                 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
147                             "rc=%d", rc);
148 }
149
150 /* Start a given request at the device. Return zero on success, non-zero
151  * otherwise. */
152 static int
153 dasd_start_diag(struct dasd_ccw_req * cqr)
154 {
155         struct dasd_device *device;
156         struct dasd_diag_private *private;
157         struct dasd_diag_req *dreq;
158         int rc;
159
160         device = cqr->device;
161         if (cqr->retries < 0) {
162                 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
163                             "- no retry left)", cqr);
164                 cqr->status = DASD_CQR_FAILED;
165                 return -EIO;
166         }
167         private = (struct dasd_diag_private *) device->private;
168         dreq = (struct dasd_diag_req *) cqr->data;
169
170         private->iob.dev_nr = private->dev_id.devno;
171         private->iob.key = 0;
172         private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
173         private->iob.block_count = dreq->block_count;
174         private->iob.interrupt_params = (addr_t) cqr;
175         private->iob.bio_list = dreq->bio;
176         private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
177
178         cqr->startclk = get_clock();
179         cqr->starttime = jiffies;
180         cqr->retries--;
181
182         rc = dia250(&private->iob, RW_BIO);
183         switch (rc) {
184         case 0: /* Synchronous I/O finished successfully */
185                 cqr->stopclk = get_clock();
186                 cqr->status = DASD_CQR_DONE;
187                 /* Indicate to calling function that only a dasd_schedule_bh()
188                    and no timer is needed */
189                 rc = -EACCES;
190                 break;
191         case 8: /* Asynchronous I/O was started */
192                 cqr->status = DASD_CQR_IN_IO;
193                 rc = 0;
194                 break;
195         default: /* Error condition */
196                 cqr->status = DASD_CQR_QUEUED;
197                 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
198                 dasd_diag_erp(device);
199                 rc = -EIO;
200                 break;
201         }
202         return rc;
203 }
204
205 /* Terminate given request at the device. */
206 static int
207 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
208 {
209         struct dasd_device *device;
210
211         device = cqr->device;
212         mdsk_term_io(device);
213         mdsk_init_io(device, device->bp_block, 0, NULL);
214         cqr->status = DASD_CQR_CLEAR;
215         cqr->stopclk = get_clock();
216         dasd_schedule_bh(device);
217         return 0;
218 }
219
220 /* Handle external interruption. */
221 static void
222 dasd_ext_handler(__u16 code)
223 {
224         struct dasd_ccw_req *cqr, *next;
225         struct dasd_device *device;
226         unsigned long long expires;
227         unsigned long flags;
228         u8 int_code, status;
229         addr_t ip;
230         int rc;
231
232         int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
233         status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
234         switch (int_code) {
235         case DASD_DIAG_CODE_31BIT:
236                 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
237                 break;
238         case DASD_DIAG_CODE_64BIT:
239                 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
240                 break;
241         default:
242                 return;
243         }
244         if (!ip) {              /* no intparm: unsolicited interrupt */
245                 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
246                 return;
247         }
248         cqr = (struct dasd_ccw_req *) ip;
249         device = (struct dasd_device *) cqr->device;
250         if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
251                 DEV_MESSAGE(KERN_WARNING, device,
252                             " magic number of dasd_ccw_req 0x%08X doesn't"
253                             " match discipline 0x%08X",
254                             cqr->magic, *(int *) (&device->discipline->name));
255                 return;
256         }
257
258         /* get irq lock to modify request queue */
259         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
260
261         /* Check for a pending clear operation */
262         if (cqr->status == DASD_CQR_CLEAR) {
263                 cqr->status = DASD_CQR_QUEUED;
264                 dasd_clear_timer(device);
265                 dasd_schedule_bh(device);
266                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
267                 return;
268         }
269
270         cqr->stopclk = get_clock();
271
272         expires = 0;
273         if (status == 0) {
274                 cqr->status = DASD_CQR_DONE;
275                 /* Start first request on queue if possible -> fast_io. */
276                 if (!list_empty(&device->ccw_queue)) {
277                         next = list_entry(device->ccw_queue.next,
278                                           struct dasd_ccw_req, list);
279                         if (next->status == DASD_CQR_QUEUED) {
280                                 rc = dasd_start_diag(next);
281                                 if (rc == 0)
282                                         expires = next->expires;
283                                 else if (rc != -EACCES)
284                                         DEV_MESSAGE(KERN_WARNING, device, "%s",
285                                                     "Interrupt fastpath "
286                                                     "failed!");
287                         }
288                 }
289         } else {
290                 cqr->status = DASD_CQR_QUEUED;
291                 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
292                             "request %p was %d (%d retries left)", cqr, status,
293                             cqr->retries);
294                 dasd_diag_erp(device);
295         }
296
297         if (expires != 0)
298                 dasd_set_timer(device, expires);
299         else
300                 dasd_clear_timer(device);
301         dasd_schedule_bh(device);
302
303         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
304 }
305
306 /* Check whether device can be controlled by DIAG discipline. Return zero on
307  * success, non-zero otherwise. */
308 static int
309 dasd_diag_check_device(struct dasd_device *device)
310 {
311         struct dasd_diag_private *private;
312         struct dasd_diag_characteristics *rdc_data;
313         struct dasd_diag_bio bio;
314         struct vtoc_cms_label *label;
315         blocknum_t end_block;
316         unsigned int sb, bsize;
317         int rc;
318
319         private = (struct dasd_diag_private *) device->private;
320         if (private == NULL) {
321                 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
322                 if (private == NULL) {
323                         DEV_MESSAGE(KERN_WARNING, device, "%s",
324                                 "memory allocation failed for private data");
325                         return -ENOMEM;
326                 }
327                 ccw_device_get_id(device->cdev, &private->dev_id);
328                 device->private = (void *) private;
329         }
330         /* Read Device Characteristics */
331         rdc_data = (void *) &(private->rdc_data);
332         rdc_data->dev_nr = private->dev_id.devno;
333         rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
334
335         rc = diag210((struct diag210 *) rdc_data);
336         if (rc) {
337                 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
338                             "information (rc=%d)", rc);
339                 return -ENOTSUPP;
340         }
341
342         /* Figure out position of label block */
343         switch (private->rdc_data.vdev_class) {
344         case DEV_CLASS_FBA:
345                 private->pt_block = 1;
346                 break;
347         case DEV_CLASS_ECKD:
348                 private->pt_block = 2;
349                 break;
350         default:
351                 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
352                             "(class=%d)", private->rdc_data.vdev_class);
353                 return -ENOTSUPP;
354         }
355
356         DBF_DEV_EVENT(DBF_INFO, device,
357                       "%04X: %04X on real %04X/%02X",
358                       rdc_data->dev_nr,
359                       rdc_data->vdev_type,
360                       rdc_data->rdev_type, rdc_data->rdev_model);
361
362         /* terminate all outstanding operations */
363         mdsk_term_io(device);
364
365         /* figure out blocksize of device */
366         label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
367         if (label == NULL)  {
368                 DEV_MESSAGE(KERN_WARNING, device, "%s",
369                             "No memory to allocate initialization request");
370                 return -ENOMEM;
371         }
372         rc = 0;
373         end_block = 0;
374         /* try all sizes - needed for ECKD devices */
375         for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
376                 mdsk_init_io(device, bsize, 0, &end_block);
377                 memset(&bio, 0, sizeof (struct dasd_diag_bio));
378                 bio.type = MDSK_READ_REQ;
379                 bio.block_number = private->pt_block + 1;
380                 bio.buffer = label;
381                 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
382                 private->iob.dev_nr = rdc_data->dev_nr;
383                 private->iob.key = 0;
384                 private->iob.flags = 0; /* do synchronous io */
385                 private->iob.block_count = 1;
386                 private->iob.interrupt_params = 0;
387                 private->iob.bio_list = &bio;
388                 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
389                 rc = dia250(&private->iob, RW_BIO);
390                 if (rc == 3) {
391                         DEV_MESSAGE(KERN_WARNING, device, "%s",
392                                 "DIAG call failed");
393                         rc = -EOPNOTSUPP;
394                         goto out;
395                 }
396                 mdsk_term_io(device);
397                 if (rc == 0)
398                         break;
399         }
400         if (bsize > PAGE_SIZE) {
401                 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
402                             "(rc=%d)", rc);
403                 rc = -EIO;
404                 goto out;
405         }
406         /* check for label block */
407         if (memcmp(label->label_id, DASD_DIAG_CMS1,
408                   sizeof(DASD_DIAG_CMS1)) == 0) {
409                 /* get formatted blocksize from label block */
410                 bsize = (unsigned int) label->block_size;
411                 device->blocks = (unsigned long) label->block_count;
412         } else
413                 device->blocks = end_block;
414         device->bp_block = bsize;
415         device->s2b_shift = 0;  /* bits to shift 512 to get a block */
416         for (sb = 512; sb < bsize; sb = sb << 1)
417                 device->s2b_shift++;
418         rc = mdsk_init_io(device, device->bp_block, 0, NULL);
419         if (rc) {
420                 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
421                         "failed (rc=%d)", rc);
422                 rc = -EIO;
423         } else {
424                 DEV_MESSAGE(KERN_INFO, device,
425                             "(%ld B/blk): %ldkB",
426                             (unsigned long) device->bp_block,
427                             (unsigned long) (device->blocks <<
428                                 device->s2b_shift) >> 1);
429         }
430 out:
431         free_page((long) label);
432         return rc;
433 }
434
435 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
436  * otherwise. */
437 static int
438 dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
439 {
440         if (dasd_check_blocksize(device->bp_block) != 0)
441                 return -EINVAL;
442         geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
443         geo->heads = 16;
444         geo->sectors = 128 >> device->s2b_shift;
445         return 0;
446 }
447
448 static dasd_era_t
449 dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
450 {
451         return dasd_era_fatal;
452 }
453
454 static dasd_erp_fn_t
455 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
456 {
457         return dasd_default_erp_action;
458 }
459
460 static dasd_erp_fn_t
461 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
462 {
463         return dasd_default_erp_postaction;
464 }
465
466 /* Create DASD request from block device request. Return pointer to new
467  * request on success, ERR_PTR otherwise. */
468 static struct dasd_ccw_req *
469 dasd_diag_build_cp(struct dasd_device * device, struct request *req)
470 {
471         struct dasd_ccw_req *cqr;
472         struct dasd_diag_req *dreq;
473         struct dasd_diag_bio *dbio;
474         struct bio *bio;
475         struct bio_vec *bv;
476         char *dst;
477         unsigned int count, datasize;
478         sector_t recid, first_rec, last_rec;
479         unsigned int blksize, off;
480         unsigned char rw_cmd;
481         int i;
482
483         if (rq_data_dir(req) == READ)
484                 rw_cmd = MDSK_READ_REQ;
485         else if (rq_data_dir(req) == WRITE)
486                 rw_cmd = MDSK_WRITE_REQ;
487         else
488                 return ERR_PTR(-EINVAL);
489         blksize = device->bp_block;
490         /* Calculate record id of first and last block. */
491         first_rec = req->sector >> device->s2b_shift;
492         last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
493         /* Check struct bio and count the number of blocks for the request. */
494         count = 0;
495         rq_for_each_bio(bio, req) {
496                 bio_for_each_segment(bv, bio, i) {
497                         if (bv->bv_len & (blksize - 1))
498                                 /* Fba can only do full blocks. */
499                                 return ERR_PTR(-EINVAL);
500                         count += bv->bv_len >> (device->s2b_shift + 9);
501                 }
502         }
503         /* Paranoia. */
504         if (count != last_rec - first_rec + 1)
505                 return ERR_PTR(-EINVAL);
506         /* Build the request */
507         datasize = sizeof(struct dasd_diag_req) +
508                 count*sizeof(struct dasd_diag_bio);
509         cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
510                                    datasize, device);
511         if (IS_ERR(cqr))
512                 return cqr;
513
514         dreq = (struct dasd_diag_req *) cqr->data;
515         dreq->block_count = count;
516         dbio = dreq->bio;
517         recid = first_rec;
518         rq_for_each_bio(bio, req) {
519                 bio_for_each_segment(bv, bio, i) {
520                         dst = page_address(bv->bv_page) + bv->bv_offset;
521                         for (off = 0; off < bv->bv_len; off += blksize) {
522                                 memset(dbio, 0, sizeof (struct dasd_diag_bio));
523                                 dbio->type = rw_cmd;
524                                 dbio->block_number = recid + 1;
525                                 dbio->buffer = dst;
526                                 dbio++;
527                                 dst += blksize;
528                                 recid++;
529                         }
530                 }
531         }
532         cqr->retries = DIAG_MAX_RETRIES;
533         cqr->buildclk = get_clock();
534         if (req->cmd_flags & REQ_FAILFAST)
535                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
536         cqr->device = device;
537         cqr->expires = DIAG_TIMEOUT;
538         cqr->status = DASD_CQR_FILLED;
539         return cqr;
540 }
541
542 /* Release DASD request. Return non-zero if request was successful, zero
543  * otherwise. */
544 static int
545 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
546 {
547         int status;
548
549         status = cqr->status == DASD_CQR_DONE;
550         dasd_sfree_request(cqr, cqr->device);
551         return status;
552 }
553
554 /* Fill in IOCTL data for device. */
555 static int
556 dasd_diag_fill_info(struct dasd_device * device,
557                     struct dasd_information2_t * info)
558 {
559         struct dasd_diag_private *private;
560
561         private = (struct dasd_diag_private *) device->private;
562         info->label_block = (unsigned int) private->pt_block;
563         info->FBA_layout = 1;
564         info->format = DASD_FORMAT_LDL;
565         info->characteristics_size = sizeof (struct dasd_diag_characteristics);
566         memcpy(info->characteristics,
567                &((struct dasd_diag_private *) device->private)->rdc_data,
568                sizeof (struct dasd_diag_characteristics));
569         info->confdata_size = 0;
570         return 0;
571 }
572
573 static void
574 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
575                      struct irb *stat)
576 {
577         DEV_MESSAGE(KERN_ERR, device, "%s",
578                     "dump sense not available for DIAG data");
579 }
580
581 static struct dasd_discipline dasd_diag_discipline = {
582         .owner = THIS_MODULE,
583         .name = "DIAG",
584         .ebcname = "DIAG",
585         .max_blocks = DIAG_MAX_BLOCKS,
586         .check_device = dasd_diag_check_device,
587         .fill_geometry = dasd_diag_fill_geometry,
588         .start_IO = dasd_start_diag,
589         .term_IO = dasd_diag_term_IO,
590         .examine_error = dasd_diag_examine_error,
591         .erp_action = dasd_diag_erp_action,
592         .erp_postaction = dasd_diag_erp_postaction,
593         .build_cp = dasd_diag_build_cp,
594         .free_cp = dasd_diag_free_cp,
595         .dump_sense = dasd_diag_dump_sense,
596         .fill_info = dasd_diag_fill_info,
597 };
598
599 static int __init
600 dasd_diag_init(void)
601 {
602         if (!MACHINE_IS_VM) {
603                 MESSAGE_LOG(KERN_INFO,
604                             "Machine is not VM: %s "
605                             "discipline not initializing",
606                             dasd_diag_discipline.name);
607                 return -ENODEV;
608         }
609         ASCEBC(dasd_diag_discipline.ebcname, 4);
610
611         ctl_set_bit(0, 9);
612         register_external_interrupt(0x2603, dasd_ext_handler);
613         dasd_diag_discipline_pointer = &dasd_diag_discipline;
614         return 0;
615 }
616
617 static void __exit
618 dasd_diag_cleanup(void)
619 {
620         unregister_external_interrupt(0x2603, dasd_ext_handler);
621         ctl_clear_bit(0, 9);
622         dasd_diag_discipline_pointer = NULL;
623 }
624
625 module_init(dasd_diag_init);
626 module_exit(dasd_diag_cleanup);