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