Merge branch 'locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 #define KMSG_COMPONENT "dasd"
12
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/s390_ext.h>
27 #include <asm/todclk.h>
28 #include <asm/vtoc.h>
29 #include <asm/diag.h>
30
31 #include "dasd_int.h"
32 #include "dasd_diag.h"
33
34 #define PRINTK_HEADER "dasd(diag):"
35
36 MODULE_LICENSE("GPL");
37
38 /* The maximum number of blocks per request (max_blocks) is dependent on the
39  * amount of storage that is available in the static I/O buffer for each
40  * device. Currently each device gets 2 pages. We want to fit two requests
41  * into the available memory so that we can immediately start the next if one
42  * finishes. */
43 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
44                            sizeof(struct dasd_diag_req)) / \
45                            sizeof(struct dasd_diag_bio)) / 2)
46 #define DIAG_MAX_RETRIES        32
47 #define DIAG_TIMEOUT            50 * HZ
48
49 static struct dasd_discipline dasd_diag_discipline;
50
51 struct dasd_diag_private {
52         struct dasd_diag_characteristics rdc_data;
53         struct dasd_diag_rw_io iob;
54         struct dasd_diag_init_io iib;
55         blocknum_t pt_block;
56         struct ccw_dev_id dev_id;
57 };
58
59 struct dasd_diag_req {
60         unsigned int block_count;
61         struct dasd_diag_bio bio[0];
62 };
63
64 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
65
66 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
67  * and function code cmd.
68  * In case of an exception return 3. Otherwise return result of bitwise OR of
69  * resulting condition code and DIAG return code. */
70 static inline int dia250(void *iob, int cmd)
71 {
72         register unsigned long reg2 asm ("2") = (unsigned long) iob;
73         typedef union {
74                 struct dasd_diag_init_io init_io;
75                 struct dasd_diag_rw_io rw_io;
76         } addr_type;
77         int rc;
78
79         rc = 3;
80         asm volatile(
81                 "       diag    2,%2,0x250\n"
82                 "0:     ipm     %0\n"
83                 "       srl     %0,28\n"
84                 "       or      %0,3\n"
85                 "1:\n"
86                 EX_TABLE(0b,1b)
87                 : "+d" (rc), "=m" (*(addr_type *) iob)
88                 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
89                 : "3", "cc");
90         return rc;
91 }
92
93 /* Initialize block I/O to DIAG device using the specified blocksize and
94  * block offset. On success, return zero and set end_block to contain the
95  * number of blocks on the device minus the specified offset. Return non-zero
96  * otherwise. */
97 static inline int
98 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
99              blocknum_t offset, blocknum_t *end_block)
100 {
101         struct dasd_diag_private *private;
102         struct dasd_diag_init_io *iib;
103         int rc;
104
105         private = (struct dasd_diag_private *) device->private;
106         iib = &private->iib;
107         memset(iib, 0, sizeof (struct dasd_diag_init_io));
108
109         iib->dev_nr = private->dev_id.devno;
110         iib->block_size = blocksize;
111         iib->offset = offset;
112         iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
113
114         rc = dia250(iib, INIT_BIO);
115
116         if ((rc & 3) == 0 && end_block)
117                 *end_block = iib->end_block;
118
119         return rc;
120 }
121
122 /* Remove block I/O environment for device. Return zero on success, non-zero
123  * otherwise. */
124 static inline int
125 mdsk_term_io(struct dasd_device * device)
126 {
127         struct dasd_diag_private *private;
128         struct dasd_diag_init_io *iib;
129         int rc;
130
131         private = (struct dasd_diag_private *) device->private;
132         iib = &private->iib;
133         memset(iib, 0, sizeof (struct dasd_diag_init_io));
134         iib->dev_nr = private->dev_id.devno;
135         rc = dia250(iib, TERM_BIO);
136         return rc;
137 }
138
139 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
140  * environment. */
141 static void
142 dasd_diag_erp(struct dasd_device *device)
143 {
144         int rc;
145
146         mdsk_term_io(device);
147         rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
148         if (rc)
149                 dev_warn(&device->cdev->dev, "DIAG ERP failed with "
150                             "rc=%d\n", rc);
151 }
152
153 /* Start a given request at the device. Return zero on success, non-zero
154  * otherwise. */
155 static int
156 dasd_start_diag(struct dasd_ccw_req * cqr)
157 {
158         struct dasd_device *device;
159         struct dasd_diag_private *private;
160         struct dasd_diag_req *dreq;
161         int rc;
162
163         device = cqr->startdev;
164         if (cqr->retries < 0) {
165                 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
166                             "- no retry left)", cqr);
167                 cqr->status = DASD_CQR_ERROR;
168                 return -EIO;
169         }
170         private = (struct dasd_diag_private *) device->private;
171         dreq = (struct dasd_diag_req *) cqr->data;
172
173         private->iob.dev_nr = private->dev_id.devno;
174         private->iob.key = 0;
175         private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
176         private->iob.block_count = dreq->block_count;
177         private->iob.interrupt_params = (addr_t) cqr;
178         private->iob.bio_list = dreq->bio;
179         private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
180
181         cqr->startclk = get_clock();
182         cqr->starttime = jiffies;
183         cqr->retries--;
184
185         rc = dia250(&private->iob, RW_BIO);
186         switch (rc) {
187         case 0: /* Synchronous I/O finished successfully */
188                 cqr->stopclk = get_clock();
189                 cqr->status = DASD_CQR_SUCCESS;
190                 /* Indicate to calling function that only a dasd_schedule_bh()
191                    and no timer is needed */
192                 rc = -EACCES;
193                 break;
194         case 8: /* Asynchronous I/O was started */
195                 cqr->status = DASD_CQR_IN_IO;
196                 rc = 0;
197                 break;
198         default: /* Error condition */
199                 cqr->status = DASD_CQR_QUEUED;
200                 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
201                 dasd_diag_erp(device);
202                 rc = -EIO;
203                 break;
204         }
205         return rc;
206 }
207
208 /* Terminate given request at the device. */
209 static int
210 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
211 {
212         struct dasd_device *device;
213
214         device = cqr->startdev;
215         mdsk_term_io(device);
216         mdsk_init_io(device, device->block->bp_block, 0, NULL);
217         cqr->status = DASD_CQR_CLEAR_PENDING;
218         cqr->stopclk = get_clock();
219         dasd_schedule_device_bh(device);
220         return 0;
221 }
222
223 /* Handle external interruption. */
224 static void
225 dasd_ext_handler(__u16 code)
226 {
227         struct dasd_ccw_req *cqr, *next;
228         struct dasd_device *device;
229         unsigned long long expires;
230         unsigned long flags;
231         u8 int_code, status;
232         addr_t ip;
233         int rc;
234
235         int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
236         status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
237         switch (int_code) {
238         case DASD_DIAG_CODE_31BIT:
239                 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
240                 break;
241         case DASD_DIAG_CODE_64BIT:
242                 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
243                 break;
244         default:
245                 return;
246         }
247         if (!ip) {              /* no intparm: unsolicited interrupt */
248                 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
249                               "interrupt");
250                 return;
251         }
252         cqr = (struct dasd_ccw_req *) ip;
253         device = (struct dasd_device *) cqr->startdev;
254         if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
255                 DBF_DEV_EVENT(DBF_WARNING, device,
256                             " magic number of dasd_ccw_req 0x%08X doesn't"
257                             " match discipline 0x%08X",
258                             cqr->magic, *(int *) (&device->discipline->name));
259                 return;
260         }
261
262         /* get irq lock to modify request queue */
263         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
264
265         /* Check for a pending clear operation */
266         if (cqr->status == DASD_CQR_CLEAR_PENDING) {
267                 cqr->status = DASD_CQR_CLEARED;
268                 dasd_device_clear_timer(device);
269                 dasd_schedule_device_bh(device);
270                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
271                 return;
272         }
273
274         cqr->stopclk = get_clock();
275
276         expires = 0;
277         if (status == 0) {
278                 cqr->status = DASD_CQR_SUCCESS;
279                 /* Start first request on queue if possible -> fast_io. */
280                 if (!list_empty(&device->ccw_queue)) {
281                         next = list_entry(device->ccw_queue.next,
282                                           struct dasd_ccw_req, devlist);
283                         if (next->status == DASD_CQR_QUEUED) {
284                                 rc = dasd_start_diag(next);
285                                 if (rc == 0)
286                                         expires = next->expires;
287                         }
288                 }
289         } else {
290                 cqr->status = DASD_CQR_QUEUED;
291                 DBF_DEV_EVENT(DBF_DEBUG, 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_device_set_timer(device, expires);
299         else
300                 dasd_device_clear_timer(device);
301         dasd_schedule_device_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_block *block;
312         struct dasd_diag_private *private;
313         struct dasd_diag_characteristics *rdc_data;
314         struct dasd_diag_bio bio;
315         struct vtoc_cms_label *label;
316         blocknum_t end_block;
317         unsigned int sb, bsize;
318         int rc;
319
320         private = (struct dasd_diag_private *) device->private;
321         if (private == NULL) {
322                 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
323                 if (private == NULL) {
324                         DBF_DEV_EVENT(DBF_WARNING, device, "%s",
325                                 "Allocating memory for private DASD data "
326                                       "failed\n");
327                         return -ENOMEM;
328                 }
329                 ccw_device_get_id(device->cdev, &private->dev_id);
330                 device->private = (void *) private;
331         }
332         block = dasd_alloc_block();
333         if (IS_ERR(block)) {
334                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
335                             "could not allocate dasd block structure");
336                 device->private = NULL;
337                 kfree(private);
338                 return PTR_ERR(block);
339         }
340         device->block = block;
341         block->base = device;
342
343         /* Read Device Characteristics */
344         rdc_data = (void *) &(private->rdc_data);
345         rdc_data->dev_nr = private->dev_id.devno;
346         rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
347
348         rc = diag210((struct diag210 *) rdc_data);
349         if (rc) {
350                 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
351                             "information (rc=%d)", rc);
352                 rc = -EOPNOTSUPP;
353                 goto out;
354         }
355
356         /* Figure out position of label block */
357         switch (private->rdc_data.vdev_class) {
358         case DEV_CLASS_FBA:
359                 private->pt_block = 1;
360                 break;
361         case DEV_CLASS_ECKD:
362                 private->pt_block = 2;
363                 break;
364         default:
365                 dev_warn(&device->cdev->dev, "Device type %d is not supported "
366                             "in DIAG mode\n", private->rdc_data.vdev_class);
367                 rc = -EOPNOTSUPP;
368                 goto out;
369         }
370
371         DBF_DEV_EVENT(DBF_INFO, device,
372                       "%04X: %04X on real %04X/%02X",
373                       rdc_data->dev_nr,
374                       rdc_data->vdev_type,
375                       rdc_data->rdev_type, rdc_data->rdev_model);
376
377         /* terminate all outstanding operations */
378         mdsk_term_io(device);
379
380         /* figure out blocksize of device */
381         label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
382         if (label == NULL)  {
383                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
384                             "No memory to allocate initialization request");
385                 rc = -ENOMEM;
386                 goto out;
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_warn(&device->cdev->dev,
408                                 "A 64-bit DIAG call failed\n");
409                         rc = -EOPNOTSUPP;
410                         goto out_label;
411                 }
412                 mdsk_term_io(device);
413                 if (rc == 0)
414                         break;
415         }
416         if (bsize > PAGE_SIZE) {
417                 dev_warn(&device->cdev->dev, "Accessing the DASD failed because"
418                          " of an incorrect format (rc=%d)\n", rc);
419                 rc = -EIO;
420                 goto out_label;
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                 block->blocks = (unsigned long) label->block_count;
428         } else
429                 block->blocks = end_block;
430         block->bp_block = bsize;
431         block->s2b_shift = 0;   /* bits to shift 512 to get a block */
432         for (sb = 512; sb < bsize; sb = sb << 1)
433                 block->s2b_shift++;
434         rc = mdsk_init_io(device, block->bp_block, 0, NULL);
435         if (rc) {
436                 dev_warn(&device->cdev->dev, "DIAG initialization "
437                         "failed with rc=%d\n", rc);
438                 rc = -EIO;
439         } else {
440                 dev_info(&device->cdev->dev,
441                          "New DASD with %ld byte/block, total size %ld KB\n",
442                          (unsigned long) block->bp_block,
443                          (unsigned long) (block->blocks <<
444                                           block->s2b_shift) >> 1);
445         }
446 out_label:
447         free_page((long) label);
448 out:
449         if (rc) {
450                 device->block = NULL;
451                 dasd_free_block(block);
452                 device->private = NULL;
453                 kfree(private);
454         }
455         return rc;
456 }
457
458 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
459  * otherwise. */
460 static int
461 dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
462 {
463         if (dasd_check_blocksize(block->bp_block) != 0)
464                 return -EINVAL;
465         geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
466         geo->heads = 16;
467         geo->sectors = 128 >> block->s2b_shift;
468         return 0;
469 }
470
471 static dasd_erp_fn_t
472 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
473 {
474         return dasd_default_erp_action;
475 }
476
477 static dasd_erp_fn_t
478 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
479 {
480         return dasd_default_erp_postaction;
481 }
482
483 /* Create DASD request from block device request. Return pointer to new
484  * request on success, ERR_PTR otherwise. */
485 static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
486                                                struct dasd_block *block,
487                                                struct request *req)
488 {
489         struct dasd_ccw_req *cqr;
490         struct dasd_diag_req *dreq;
491         struct dasd_diag_bio *dbio;
492         struct req_iterator iter;
493         struct bio_vec *bv;
494         char *dst;
495         unsigned int count, datasize;
496         sector_t recid, first_rec, last_rec;
497         unsigned int blksize, off;
498         unsigned char rw_cmd;
499
500         if (rq_data_dir(req) == READ)
501                 rw_cmd = MDSK_READ_REQ;
502         else if (rq_data_dir(req) == WRITE)
503                 rw_cmd = MDSK_WRITE_REQ;
504         else
505                 return ERR_PTR(-EINVAL);
506         blksize = block->bp_block;
507         /* Calculate record id of first and last block. */
508         first_rec = req->sector >> block->s2b_shift;
509         last_rec = (req->sector + req->nr_sectors - 1) >> block->s2b_shift;
510         /* Check struct bio and count the number of blocks for the request. */
511         count = 0;
512         rq_for_each_segment(bv, req, iter) {
513                 if (bv->bv_len & (blksize - 1))
514                         /* Fba can only do full blocks. */
515                         return ERR_PTR(-EINVAL);
516                 count += bv->bv_len >> (block->s2b_shift + 9);
517         }
518         /* Paranoia. */
519         if (count != last_rec - first_rec + 1)
520                 return ERR_PTR(-EINVAL);
521         /* Build the request */
522         datasize = sizeof(struct dasd_diag_req) +
523                 count*sizeof(struct dasd_diag_bio);
524         cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
525                                    datasize, memdev);
526         if (IS_ERR(cqr))
527                 return cqr;
528
529         dreq = (struct dasd_diag_req *) cqr->data;
530         dreq->block_count = count;
531         dbio = dreq->bio;
532         recid = first_rec;
533         rq_for_each_segment(bv, req, iter) {
534                 dst = page_address(bv->bv_page) + bv->bv_offset;
535                 for (off = 0; off < bv->bv_len; off += blksize) {
536                         memset(dbio, 0, sizeof (struct dasd_diag_bio));
537                         dbio->type = rw_cmd;
538                         dbio->block_number = recid + 1;
539                         dbio->buffer = dst;
540                         dbio++;
541                         dst += blksize;
542                         recid++;
543                 }
544         }
545         cqr->retries = DIAG_MAX_RETRIES;
546         cqr->buildclk = get_clock();
547         if (blk_noretry_request(req) ||
548             block->base->features & DASD_FEATURE_FAILFAST)
549                 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
550         cqr->startdev = memdev;
551         cqr->memdev = memdev;
552         cqr->block = block;
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->memdev);
567         return status;
568 }
569
570 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
571 {
572         cqr->status = DASD_CQR_FILLED;
573 };
574
575 /* Fill in IOCTL data for device. */
576 static int
577 dasd_diag_fill_info(struct dasd_device * device,
578                     struct dasd_information2_t * info)
579 {
580         struct dasd_diag_private *private;
581
582         private = (struct dasd_diag_private *) device->private;
583         info->label_block = (unsigned int) private->pt_block;
584         info->FBA_layout = 1;
585         info->format = DASD_FORMAT_LDL;
586         info->characteristics_size = sizeof (struct dasd_diag_characteristics);
587         memcpy(info->characteristics,
588                &((struct dasd_diag_private *) device->private)->rdc_data,
589                sizeof (struct dasd_diag_characteristics));
590         info->confdata_size = 0;
591         return 0;
592 }
593
594 static void
595 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
596                      struct irb *stat)
597 {
598         DBF_DEV_EVENT(DBF_WARNING, device, "%s",
599                     "dump sense not available for DIAG data");
600 }
601
602 static struct dasd_discipline dasd_diag_discipline = {
603         .owner = THIS_MODULE,
604         .name = "DIAG",
605         .ebcname = "DIAG",
606         .max_blocks = DIAG_MAX_BLOCKS,
607         .check_device = dasd_diag_check_device,
608         .fill_geometry = dasd_diag_fill_geometry,
609         .start_IO = dasd_start_diag,
610         .term_IO = dasd_diag_term_IO,
611         .handle_terminated_request = dasd_diag_handle_terminated_request,
612         .erp_action = dasd_diag_erp_action,
613         .erp_postaction = dasd_diag_erp_postaction,
614         .build_cp = dasd_diag_build_cp,
615         .free_cp = dasd_diag_free_cp,
616         .dump_sense = dasd_diag_dump_sense,
617         .fill_info = dasd_diag_fill_info,
618 };
619
620 static int __init
621 dasd_diag_init(void)
622 {
623         if (!MACHINE_IS_VM) {
624                 pr_info("Discipline %s cannot be used without z/VM\n",
625                         dasd_diag_discipline.name);
626                 return -ENODEV;
627         }
628         ASCEBC(dasd_diag_discipline.ebcname, 4);
629
630         ctl_set_bit(0, 9);
631         register_external_interrupt(0x2603, dasd_ext_handler);
632         dasd_diag_discipline_pointer = &dasd_diag_discipline;
633         return 0;
634 }
635
636 static void __exit
637 dasd_diag_cleanup(void)
638 {
639         unregister_external_interrupt(0x2603, dasd_ext_handler);
640         ctl_clear_bit(0, 9);
641         dasd_diag_discipline_pointer = NULL;
642 }
643
644 module_init(dasd_diag_init);
645 module_exit(dasd_diag_cleanup);