ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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  * $Revision: 1.34 $
10  */
11
12 #include <linux/config.h>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/hdreg.h>        /* HDIO_GETGEO                      */
17 #include <linux/bio.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20
21 #include <asm/dasd.h>
22 #include <asm/debug.h>
23 #include <asm/ebcdic.h>
24 #include <asm/io.h>
25 #include <asm/s390_ext.h>
26 #include <asm/todclk.h>
27
28 #include "dasd_int.h"
29 #include "dasd_diag.h"
30
31 #ifdef PRINTK_HEADER
32 #undef PRINTK_HEADER
33 #endif                          /* PRINTK_HEADER */
34 #define PRINTK_HEADER "dasd(diag):"
35
36 MODULE_LICENSE("GPL");
37
38 struct dasd_diag_private {
39         struct dasd_diag_characteristics rdc_data;
40         struct dasd_diag_rw_io iob;
41         struct dasd_diag_init_io iib;
42         unsigned int pt_block;
43 };
44
45 struct dasd_diag_req {
46         int block_count;
47         struct dasd_diag_bio bio[0];
48 };
49
50 static __inline__ int
51 dia250(void *iob, int cmd)
52 {
53         int rc;
54
55         __asm__ __volatile__("    lhi   %0,3\n"
56                              "    lr    0,%2\n"
57                              "    diag  0,%1,0x250\n"
58                              "0:  ipm   %0\n"
59                              "    srl   %0,28\n"
60                              "    or    %0,1\n"
61                              "1:\n"
62 #ifndef CONFIG_ARCH_S390X
63                              ".section __ex_table,\"a\"\n"
64                              "    .align 4\n"
65                              "    .long 0b,1b\n"
66                              ".previous\n"
67 #else
68                              ".section __ex_table,\"a\"\n"
69                              "    .align 8\n"
70                              "    .quad  0b,1b\n"
71                              ".previous\n"
72 #endif
73                              : "=&d" (rc)
74                              : "d" (cmd), "d" ((void *) __pa(iob))
75                              : "0", "1", "cc");
76         return rc;
77 }
78
79 static __inline__ int
80 mdsk_init_io(struct dasd_device * device, int blocksize, int offset, int size)
81 {
82         struct dasd_diag_private *private;
83         struct dasd_diag_init_io *iib;
84         int rc;
85
86         private = (struct dasd_diag_private *) device->private;
87         iib = &private->iib;
88         memset(iib, 0, sizeof (struct dasd_diag_init_io));
89
90         iib->dev_nr = _ccw_device_get_device_number(device->cdev);
91         iib->block_size = blocksize;
92         iib->offset = offset;
93         iib->start_block = 0;
94         iib->end_block = size;
95
96         rc = dia250(iib, INIT_BIO);
97
98         return rc & 3;
99 }
100
101 static __inline__ int
102 mdsk_term_io(struct dasd_device * device)
103 {
104         struct dasd_diag_private *private;
105         struct dasd_diag_init_io *iib;
106         int rc;
107
108         private = (struct dasd_diag_private *) device->private;
109         iib = &private->iib;
110         memset(iib, 0, sizeof (struct dasd_diag_init_io));
111         iib->dev_nr = _ccw_device_get_device_number(device->cdev);
112         rc = dia250(iib, TERM_BIO);
113         return rc & 3;
114 }
115
116 static int
117 dasd_start_diag(struct dasd_ccw_req * cqr)
118 {
119         struct dasd_device *device;
120         struct dasd_diag_private *private;
121         struct dasd_diag_req *dreq;
122         int rc;
123
124         device = cqr->device;
125         private = (struct dasd_diag_private *) device->private;
126         dreq = (struct dasd_diag_req *) cqr->data;
127
128         private->iob.dev_nr = _ccw_device_get_device_number(device->cdev);
129         private->iob.key = 0;
130         private->iob.flags = 2; /* do asynchronous io */
131         private->iob.block_count = dreq->block_count;
132         private->iob.interrupt_params = (u32)(addr_t) cqr;
133         private->iob.bio_list = __pa(dreq->bio);
134
135         cqr->startclk = get_clock();
136
137         rc = dia250(&private->iob, RW_BIO);
138         if (rc > 8) {
139                 MESSAGE(KERN_WARNING, "dia250 returned CC %d", rc);
140                 cqr->status = DASD_CQR_ERROR;
141         } else if (rc == 0) {
142                 cqr->status = DASD_CQR_DONE;
143                 dasd_schedule_bh(device);
144         } else {
145                 cqr->status = DASD_CQR_IN_IO;
146                 rc = 0;
147         }
148         return rc;
149 }
150
151 static void
152 dasd_ext_handler(struct pt_regs *regs, __u16 code)
153 {
154         struct dasd_ccw_req *cqr, *next;
155         struct dasd_device *device;
156         unsigned long long expires;
157         unsigned long flags;
158         char status;
159         int ip, cpu;
160
161         /*
162          * Get the external interruption subcode. VM stores
163          * this in the 'cpu address' field associated with
164          * the external interrupt. For diag 250 the subcode
165          * needs to be 3.
166          */
167         if ((S390_lowcore.cpu_addr & 0xff00) != 0x0300)
168                 return;
169         status = *((char *) &S390_lowcore.ext_params + 5);
170         ip = S390_lowcore.ext_params;
171
172         cpu = smp_processor_id();
173
174         if (!ip) {              /* no intparm: unsolicited interrupt */
175                 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
176                 return;
177         }
178         cqr = (struct dasd_ccw_req *)(addr_t) ip;
179         device = (struct dasd_device *) cqr->device;
180         if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
181                 DEV_MESSAGE(KERN_WARNING, device,
182                             " magic number of dasd_ccw_req 0x%08X doesn't"
183                             " match discipline 0x%08X",
184                             cqr->magic, *(int *) (&device->discipline->name));
185                 return;
186         }
187
188         /* get irq lock to modify request queue */
189         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
190
191         cqr->stopclk = get_clock();
192
193         expires = 0;
194         if (status == 0) {
195                 cqr->status = DASD_CQR_DONE;
196                 /* Start first request on queue if possible -> fast_io. */
197                 if (!list_empty(&device->ccw_queue)) {
198                         next = list_entry(device->ccw_queue.next,
199                                           struct dasd_ccw_req, list);
200                         if (next->status == DASD_CQR_QUEUED) {
201                                 if (dasd_start_diag(next) == 0)
202                                         expires = next->expires;
203                                 else
204                                         MESSAGE(KERN_WARNING, "%s",
205                                                 "Interrupt fastpath failed!");
206                         }
207                 }
208         } else 
209                 cqr->status = DASD_CQR_FAILED;
210
211         if (expires != 0)
212                 dasd_set_timer(device, expires);
213         else
214                 dasd_clear_timer(device);
215         dasd_schedule_bh(device);
216
217         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
218 }
219
220 static int
221 dasd_diag_check_device(struct dasd_device *device)
222 {
223         struct dasd_diag_private *private;
224         struct dasd_diag_characteristics *rdc_data;
225         struct dasd_diag_bio bio;
226         long *label;
227         int sb, bsize;
228         int rc;
229
230         private = (struct dasd_diag_private *) device->private;
231         if (private == NULL) {
232                 private = kmalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
233                 if (private == NULL) {
234                         MESSAGE(KERN_WARNING, "%s",
235                                 "memory allocation failed for private data");
236                         return -ENOMEM;
237                 }
238                 device->private = (void *) private;
239         }
240         /* Read Device Characteristics */
241         rdc_data = (void *) &(private->rdc_data);
242         rdc_data->dev_nr = _ccw_device_get_device_number(device->cdev);
243         rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
244
245         rc = diag210((struct diag210 *) rdc_data);
246         if (rc)
247                 return -ENOTSUPP;
248
249         /* Figure out position of label block */
250         switch (private->rdc_data.vdev_class) {
251         case DEV_CLASS_FBA:
252                 private->pt_block = 1;
253                 break;
254         case DEV_CLASS_ECKD:
255                 private->pt_block = 2;
256                 break;
257         default:
258                 return -ENOTSUPP;
259         }
260
261         DBF_EVENT(DBF_INFO,
262                   "%04X: %04X on real %04X/%02X",
263                   rdc_data->dev_nr,
264                   rdc_data->vdev_type,
265                   rdc_data->rdev_type, rdc_data->rdev_model);
266
267         /* terminate all outstanding operations */
268         mdsk_term_io(device);
269
270         /* figure out blocksize of device */
271         label = (long *) get_zeroed_page(GFP_KERNEL);
272         if (label == NULL)  {
273                 MESSAGE(KERN_WARNING, "%s",
274                         "No memory to allocate initialization request");
275                 return -ENOMEM;
276         }
277         for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
278                 mdsk_init_io(device, bsize, 0, 64);
279                 memset(&bio, 0, sizeof (struct dasd_diag_bio));
280                 bio.type = MDSK_READ_REQ;
281                 bio.block_number = private->pt_block + 1;
282                 bio.buffer = __pa(label);
283                 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
284                 private->iob.dev_nr = rdc_data->dev_nr;
285                 private->iob.key = 0;
286                 private->iob.flags = 0; /* do synchronous io */
287                 private->iob.block_count = 1;
288                 private->iob.interrupt_params = 0;
289                 private->iob.bio_list = __pa(&bio);
290                 if (dia250(&private->iob, RW_BIO) == 0)
291                         break;
292                 mdsk_term_io(device);
293         }
294         if (bsize <= PAGE_SIZE && label[3] == bsize &&
295             label[0] == 0xc3d4e2f1 && label[13] != 0) {
296                 device->blocks = label[7];
297                 device->bp_block = bsize;
298                 device->s2b_shift = 0;  /* bits to shift 512 to get a block */
299                 for (sb = 512; sb < bsize; sb = sb << 1)
300                         device->s2b_shift++;
301                 
302                 DEV_MESSAGE(KERN_INFO, device,
303                             "capacity (%dkB blks): %ldkB",
304                             (device->bp_block >> 10),
305                             (device->blocks << device->s2b_shift) >> 1);
306                 rc = 0;
307         } else
308                 rc = -EMEDIUMTYPE;
309         free_page((long) label);
310         return rc;
311 }
312
313 static int
314 dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
315 {
316         if (dasd_check_blocksize(device->bp_block) != 0)
317                 return -EINVAL;
318         geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
319         geo->heads = 16;
320         geo->sectors = 128 >> device->s2b_shift;
321         return 0;
322 }
323
324 static dasd_era_t
325 dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
326 {
327         return dasd_era_fatal;
328 }
329
330 static dasd_erp_fn_t
331 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
332 {
333         return dasd_default_erp_action;
334 }
335
336 static dasd_erp_fn_t
337 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
338 {
339         return dasd_default_erp_postaction;
340 }
341
342 static struct dasd_ccw_req *
343 dasd_diag_build_cp(struct dasd_device * device, struct request *req)
344 {
345         struct dasd_ccw_req *cqr;
346         struct dasd_diag_req *dreq;
347         struct dasd_diag_bio *dbio;
348         struct bio *bio;
349         struct bio_vec *bv;
350         char *dst;
351         int count, datasize;
352         sector_t recid, first_rec, last_rec;
353         unsigned blksize, off;
354         unsigned char rw_cmd;
355         int i;
356
357         if (rq_data_dir(req) == READ)
358                 rw_cmd = MDSK_READ_REQ;
359         else if (rq_data_dir(req) == WRITE)
360                 rw_cmd = MDSK_WRITE_REQ;
361         else
362                 return ERR_PTR(-EINVAL);
363         blksize = device->bp_block;
364         /* Calculate record id of first and last block. */
365         first_rec = req->sector >> device->s2b_shift;
366         last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
367         /* Check struct bio and count the number of blocks for the request. */
368         count = 0;
369         rq_for_each_bio(bio, req) {
370                 bio_for_each_segment(bv, bio, i) {
371                         if (bv->bv_len & (blksize - 1))
372                                 /* Fba can only do full blocks. */
373                                 return ERR_PTR(-EINVAL);
374                         count += bv->bv_len >> (device->s2b_shift + 9);
375                 }
376         }
377         /* Paranoia. */
378         if (count != last_rec - first_rec + 1)
379                 return ERR_PTR(-EINVAL);
380         /* Build the request */
381         datasize = sizeof(struct dasd_diag_req) +
382                 count*sizeof(struct dasd_diag_bio);
383         cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
384                                    datasize, device);
385         if (IS_ERR(cqr))
386                 return cqr;
387         
388         dreq = (struct dasd_diag_req *) cqr->data;
389         dreq->block_count = count;
390         dbio = dreq->bio;
391         recid = first_rec;
392         rq_for_each_bio(bio, req) {
393                 bio_for_each_segment(bv, bio, i) {
394                         dst = page_address(bv->bv_page) + bv->bv_offset;
395                         for (off = 0; off < bv->bv_len; off += blksize) {
396                                 memset(dbio, 0, sizeof (struct dasd_diag_bio));
397                                 dbio->type = rw_cmd;
398                                 dbio->block_number = recid + 1;
399                                 dbio->buffer = __pa(dst);
400                                 dbio++;
401                                 dst += blksize;
402                                 recid++;
403                         }
404                 }
405         }
406         cqr->buildclk = get_clock();
407         cqr->device = device;
408         cqr->expires = 50 * HZ; /* 50 seconds */
409         cqr->status = DASD_CQR_FILLED;
410         return cqr;
411 }
412
413 static int
414 dasd_diag_fill_info(struct dasd_device * device,
415                     struct dasd_information2_t * info)
416 {
417         struct dasd_diag_private *private;
418
419         private = (struct dasd_diag_private *) device->private;
420         info->label_block = private->pt_block;
421         info->FBA_layout = 1;
422         info->format = DASD_FORMAT_LDL;
423         info->characteristics_size = sizeof (struct dasd_diag_characteristics);
424         memcpy(info->characteristics,
425                &((struct dasd_diag_private *) device->private)->rdc_data,
426                sizeof (struct dasd_diag_characteristics));
427         info->confdata_size = 0;
428         return 0;
429 }
430
431 static void
432 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
433                      struct irb *stat)
434 {
435         char *page;
436
437         page = (char *) get_zeroed_page(GFP_KERNEL);
438         if (page == NULL) {
439                 MESSAGE(KERN_ERR, "%s", "No memory to dump sense data");
440                 return;
441         }
442         sprintf(page, KERN_WARNING PRINTK_HEADER
443                 "device %s: I/O status report:\n",
444                 device->cdev->dev.bus_id);
445
446         MESSAGE(KERN_ERR, "Sense data:\n%s", page);
447
448         free_page((unsigned long) page);
449 }
450
451 /*
452  * max_blocks is dependent on the amount of storage that is available
453  * in the static io buffer for each device. Currently each device has
454  * 8192 bytes (=2 pages). dasd diag is only relevant for 31 bit.
455  * The struct dasd_ccw_req has 96 bytes, the struct dasd_diag_req has
456  * 8 bytes and the struct dasd_diag_bio for each block has 16 bytes. 
457  * That makes:
458  * (8192 - 96 - 8) / 16 = 505.5 blocks at maximum.
459  * We want to fit two into the available memory so that we can immediately
460  * start the next request if one finishes off. That makes 252.75 blocks
461  * for one request. Give a little safety and the result is 240.
462  */
463 struct dasd_discipline dasd_diag_discipline = {
464         .owner = THIS_MODULE,
465         .name = "DIAG",
466         .ebcname = "DIAG",
467         .max_blocks = 240,
468         .check_device = dasd_diag_check_device,
469         .fill_geometry = dasd_diag_fill_geometry,
470         .start_IO = dasd_start_diag,
471         .examine_error = dasd_diag_examine_error,
472         .erp_action = dasd_diag_erp_action,
473         .erp_postaction = dasd_diag_erp_postaction,
474         .build_cp = dasd_diag_build_cp,
475         .dump_sense = dasd_diag_dump_sense,
476         .fill_info = dasd_diag_fill_info,
477 };
478
479 static int __init
480 dasd_diag_init(void)
481 {
482         if (!MACHINE_IS_VM) {
483                 MESSAGE(KERN_INFO,
484                         "Machine is not VM: %s discipline not initializing",
485                         dasd_diag_discipline.name);
486                 return -EINVAL;
487         }
488         ASCEBC(dasd_diag_discipline.ebcname, 4);
489
490         ctl_set_bit(0, 9);
491         register_external_interrupt(0x2603, dasd_ext_handler);
492         return 0;
493 }
494
495 static void __exit
496 dasd_diag_cleanup(void)
497 {
498         if (!MACHINE_IS_VM) {
499                 MESSAGE(KERN_INFO,
500                         "Machine is not VM: %s discipline not initializing",
501                         dasd_diag_discipline.name);
502                 return;
503         }
504         unregister_external_interrupt(0x2603, dasd_ext_handler);
505         ctl_clear_bit(0, 9);
506 }
507
508 module_init(dasd_diag_init);
509 module_exit(dasd_diag_cleanup);
510
511 /*
512  * Overrides for Emacs so that we follow Linus's tabbing style.
513  * Emacs will notice this stuff at the end of the file and automatically
514  * adjust the settings for this buffer only.  This must remain at the end
515  * of the file.
516  * ---------------------------------------------------------------------------
517  * Local variables:
518  * c-indent-level: 4 
519  * c-brace-imaginary-offset: 0
520  * c-brace-offset: -4
521  * c-argdecl-indent: 4
522  * c-label-offset: -4
523  * c-continued-statement-offset: 4
524  * c-continued-brace-offset: 0
525  * indent-tabs-mode: 1
526  * tab-width: 8
527  * End:
528  */