1
2
3
4
5
6
7
8
9
10
11#define KMSG_COMPONENT "dasd"
12
13#include <linux/kernel_stat.h>
14#include <linux/stddef.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/hdreg.h>
18#include <linux/bio.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/jiffies.h>
22
23#include <asm/dasd.h>
24#include <asm/debug.h>
25#include <asm/diag.h>
26#include <asm/ebcdic.h>
27#include <asm/io.h>
28#include <asm/irq.h>
29#include <asm/vtoc.h>
30
31#include "dasd_int.h"
32#include "dasd_diag.h"
33
34#define PRINTK_HEADER "dasd(diag):"
35
36MODULE_LICENSE("GPL");
37
38
39
40
41
42
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
48
49static struct dasd_discipline dasd_diag_discipline;
50
51struct 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
59struct dasd_diag_req {
60 unsigned int block_count;
61 struct dasd_diag_bio bio[0];
62};
63
64static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };
65
66
67
68
69
70static 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
93static inline int dia250(void *iob, int cmd)
94{
95 diag_stat_inc(DIAG_STAT_X250);
96 return __dia250(iob, cmd);
97}
98
99
100
101
102
103static inline int
104mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
105 blocknum_t offset, blocknum_t *end_block)
106{
107 struct dasd_diag_private *private = device->private;
108 struct dasd_diag_init_io *iib = &private->iib;
109 int rc;
110
111 memset(iib, 0, sizeof (struct dasd_diag_init_io));
112
113 iib->dev_nr = private->dev_id.devno;
114 iib->block_size = blocksize;
115 iib->offset = offset;
116 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
117
118 rc = dia250(iib, INIT_BIO);
119
120 if ((rc & 3) == 0 && end_block)
121 *end_block = iib->end_block;
122
123 return rc;
124}
125
126
127
128static inline int
129mdsk_term_io(struct dasd_device * device)
130{
131 struct dasd_diag_private *private = device->private;
132 struct dasd_diag_init_io *iib = &private->iib;
133 int rc;
134
135 memset(iib, 0, sizeof (struct dasd_diag_init_io));
136 iib->dev_nr = private->dev_id.devno;
137 rc = dia250(iib, TERM_BIO);
138 return rc;
139}
140
141
142
143static void
144dasd_diag_erp(struct dasd_device *device)
145{
146 int rc;
147
148 mdsk_term_io(device);
149 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
150 if (rc == 4) {
151 if (!(test_and_set_bit(DASD_FLAG_DEVICE_RO, &device->flags)))
152 pr_warn("%s: The access mode of a DIAG device changed to read-only\n",
153 dev_name(&device->cdev->dev));
154 rc = 0;
155 }
156 if (rc)
157 pr_warn("%s: DIAG ERP failed with rc=%d\n",
158 dev_name(&device->cdev->dev), rc);
159}
160
161
162
163static int
164dasd_start_diag(struct dasd_ccw_req * cqr)
165{
166 struct dasd_device *device;
167 struct dasd_diag_private *private;
168 struct dasd_diag_req *dreq;
169 int rc;
170
171 device = cqr->startdev;
172 if (cqr->retries < 0) {
173 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
174 "- no retry left)", cqr);
175 cqr->status = DASD_CQR_ERROR;
176 return -EIO;
177 }
178 private = device->private;
179 dreq = cqr->data;
180
181 private->iob.dev_nr = private->dev_id.devno;
182 private->iob.key = 0;
183 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
184 private->iob.block_count = dreq->block_count;
185 private->iob.interrupt_params = (addr_t) cqr;
186 private->iob.bio_list = dreq->bio;
187 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
188
189 cqr->startclk = get_tod_clock();
190 cqr->starttime = jiffies;
191 cqr->retries--;
192
193 rc = dia250(&private->iob, RW_BIO);
194 switch (rc) {
195 case 0:
196 cqr->stopclk = get_tod_clock();
197 cqr->status = DASD_CQR_SUCCESS;
198
199
200 rc = -EACCES;
201 break;
202 case 8:
203 cqr->status = DASD_CQR_IN_IO;
204 rc = 0;
205 break;
206 default:
207 cqr->status = DASD_CQR_QUEUED;
208 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
209 dasd_diag_erp(device);
210 rc = -EIO;
211 break;
212 }
213 cqr->intrc = rc;
214 return rc;
215}
216
217
218static int
219dasd_diag_term_IO(struct dasd_ccw_req * cqr)
220{
221 struct dasd_device *device;
222
223 device = cqr->startdev;
224 mdsk_term_io(device);
225 mdsk_init_io(device, device->block->bp_block, 0, NULL);
226 cqr->status = DASD_CQR_CLEAR_PENDING;
227 cqr->stopclk = get_tod_clock();
228 dasd_schedule_device_bh(device);
229 return 0;
230}
231
232
233static void dasd_ext_handler(struct ext_code ext_code,
234 unsigned int param32, unsigned long param64)
235{
236 struct dasd_ccw_req *cqr, *next;
237 struct dasd_device *device;
238 unsigned long expires;
239 unsigned long flags;
240 addr_t ip;
241 int rc;
242
243 switch (ext_code.subcode >> 8) {
244 case DASD_DIAG_CODE_31BIT:
245 ip = (addr_t) param32;
246 break;
247 case DASD_DIAG_CODE_64BIT:
248 ip = (addr_t) param64;
249 break;
250 default:
251 return;
252 }
253 inc_irq_stat(IRQEXT_DSD);
254 if (!ip) {
255 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
256 "interrupt");
257 return;
258 }
259 cqr = (struct dasd_ccw_req *) ip;
260 device = (struct dasd_device *) cqr->startdev;
261 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
262 DBF_DEV_EVENT(DBF_WARNING, device,
263 " magic number of dasd_ccw_req 0x%08X doesn't"
264 " match discipline 0x%08X",
265 cqr->magic, *(int *) (&device->discipline->name));
266 return;
267 }
268
269
270 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
271
272
273 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
274 cqr->status = DASD_CQR_CLEARED;
275 dasd_device_clear_timer(device);
276 dasd_schedule_device_bh(device);
277 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
278 return;
279 }
280
281 cqr->stopclk = get_tod_clock();
282
283 expires = 0;
284 if ((ext_code.subcode & 0xff) == 0) {
285 cqr->status = DASD_CQR_SUCCESS;
286
287 if (!list_empty(&device->ccw_queue)) {
288 next = list_entry(device->ccw_queue.next,
289 struct dasd_ccw_req, devlist);
290 if (next->status == DASD_CQR_QUEUED) {
291 rc = dasd_start_diag(next);
292 if (rc == 0)
293 expires = next->expires;
294 }
295 }
296 } else {
297 cqr->status = DASD_CQR_QUEUED;
298 DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
299 "request %p was %d (%d retries left)", cqr,
300 ext_code.subcode & 0xff, cqr->retries);
301 dasd_diag_erp(device);
302 }
303
304 if (expires != 0)
305 dasd_device_set_timer(device, expires);
306 else
307 dasd_device_clear_timer(device);
308 dasd_schedule_device_bh(device);
309
310 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
311}
312
313
314
315static int
316dasd_diag_check_device(struct dasd_device *device)
317{
318 struct dasd_diag_private *private = device->private;
319 struct dasd_diag_characteristics *rdc_data;
320 struct vtoc_cms_label *label;
321 struct dasd_block *block;
322 struct dasd_diag_bio *bio;
323 unsigned int sb, bsize;
324 blocknum_t end_block;
325 int rc;
326
327 if (private == NULL) {
328 private = kzalloc(sizeof(*private), GFP_KERNEL);
329 if (private == NULL) {
330 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
331 "Allocating memory for private DASD data "
332 "failed\n");
333 return -ENOMEM;
334 }
335 ccw_device_get_id(device->cdev, &private->dev_id);
336 device->private = private;
337 }
338 block = dasd_alloc_block();
339 if (IS_ERR(block)) {
340 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
341 "could not allocate dasd block structure");
342 device->private = NULL;
343 kfree(private);
344 return PTR_ERR(block);
345 }
346 device->block = block;
347 block->base = device;
348
349
350 rdc_data = &private->rdc_data;
351 rdc_data->dev_nr = private->dev_id.devno;
352 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
353
354 rc = diag210((struct diag210 *) rdc_data);
355 if (rc) {
356 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
357 "information (rc=%d)", rc);
358 rc = -EOPNOTSUPP;
359 goto out;
360 }
361
362 device->default_expires = DIAG_TIMEOUT;
363 device->default_retries = DIAG_MAX_RETRIES;
364
365
366 switch (private->rdc_data.vdev_class) {
367 case DEV_CLASS_FBA:
368 private->pt_block = 1;
369 break;
370 case DEV_CLASS_ECKD:
371 private->pt_block = 2;
372 break;
373 default:
374 pr_warn("%s: Device type %d is not supported in DIAG mode\n",
375 dev_name(&device->cdev->dev),
376 private->rdc_data.vdev_class);
377 rc = -EOPNOTSUPP;
378 goto out;
379 }
380
381 DBF_DEV_EVENT(DBF_INFO, device,
382 "%04X: %04X on real %04X/%02X",
383 rdc_data->dev_nr,
384 rdc_data->vdev_type,
385 rdc_data->rdev_type, rdc_data->rdev_model);
386
387
388 mdsk_term_io(device);
389
390
391 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
392 if (label == NULL) {
393 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
394 "No memory to allocate initialization request");
395 rc = -ENOMEM;
396 goto out;
397 }
398 bio = kzalloc(sizeof(*bio), GFP_KERNEL);
399 if (bio == NULL) {
400 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
401 "No memory to allocate initialization bio");
402 rc = -ENOMEM;
403 goto out_label;
404 }
405 rc = 0;
406 end_block = 0;
407
408 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
409 mdsk_init_io(device, bsize, 0, &end_block);
410 memset(bio, 0, sizeof(*bio));
411 bio->type = MDSK_READ_REQ;
412 bio->block_number = private->pt_block + 1;
413 bio->buffer = label;
414 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
415 private->iob.dev_nr = rdc_data->dev_nr;
416 private->iob.key = 0;
417 private->iob.flags = 0;
418 private->iob.block_count = 1;
419 private->iob.interrupt_params = 0;
420 private->iob.bio_list = bio;
421 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
422 rc = dia250(&private->iob, RW_BIO);
423 if (rc == 3) {
424 pr_warn("%s: A 64-bit DIAG call failed\n",
425 dev_name(&device->cdev->dev));
426 rc = -EOPNOTSUPP;
427 goto out_bio;
428 }
429 mdsk_term_io(device);
430 if (rc == 0)
431 break;
432 }
433 if (bsize > PAGE_SIZE) {
434 pr_warn("%s: Accessing the DASD failed because of an incorrect format (rc=%d)\n",
435 dev_name(&device->cdev->dev), rc);
436 rc = -EIO;
437 goto out_bio;
438 }
439
440 if (memcmp(label->label_id, DASD_DIAG_CMS1,
441 sizeof(DASD_DIAG_CMS1)) == 0) {
442
443 bsize = (unsigned int) label->block_size;
444 block->blocks = (unsigned long) label->block_count;
445 } else
446 block->blocks = end_block;
447 block->bp_block = bsize;
448 block->s2b_shift = 0;
449 for (sb = 512; sb < bsize; sb = sb << 1)
450 block->s2b_shift++;
451 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
452 if (rc && (rc != 4)) {
453 pr_warn("%s: DIAG initialization failed with rc=%d\n",
454 dev_name(&device->cdev->dev), rc);
455 rc = -EIO;
456 } else {
457 if (rc == 4)
458 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
459 pr_info("%s: New DASD with %ld byte/block, total size %ld "
460 "KB%s\n", dev_name(&device->cdev->dev),
461 (unsigned long) block->bp_block,
462 (unsigned long) (block->blocks <<
463 block->s2b_shift) >> 1,
464 (rc == 4) ? ", read-only device" : "");
465 rc = 0;
466 }
467out_bio:
468 kfree(bio);
469out_label:
470 free_page((long) label);
471out:
472 if (rc) {
473 device->block = NULL;
474 dasd_free_block(block);
475 device->private = NULL;
476 kfree(private);
477 }
478 return rc;
479}
480
481
482
483static int
484dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
485{
486 if (dasd_check_blocksize(block->bp_block) != 0)
487 return -EINVAL;
488 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
489 geo->heads = 16;
490 geo->sectors = 128 >> block->s2b_shift;
491 return 0;
492}
493
494static dasd_erp_fn_t
495dasd_diag_erp_action(struct dasd_ccw_req * cqr)
496{
497 return dasd_default_erp_action;
498}
499
500static dasd_erp_fn_t
501dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
502{
503 return dasd_default_erp_postaction;
504}
505
506
507
508static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
509 struct dasd_block *block,
510 struct request *req)
511{
512 struct dasd_ccw_req *cqr;
513 struct dasd_diag_req *dreq;
514 struct dasd_diag_bio *dbio;
515 struct req_iterator iter;
516 struct bio_vec bv;
517 char *dst;
518 unsigned int count, datasize;
519 sector_t recid, first_rec, last_rec;
520 unsigned int blksize, off;
521 unsigned char rw_cmd;
522
523 if (rq_data_dir(req) == READ)
524 rw_cmd = MDSK_READ_REQ;
525 else if (rq_data_dir(req) == WRITE)
526 rw_cmd = MDSK_WRITE_REQ;
527 else
528 return ERR_PTR(-EINVAL);
529 blksize = block->bp_block;
530
531 first_rec = blk_rq_pos(req) >> block->s2b_shift;
532 last_rec =
533 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
534
535 count = 0;
536 rq_for_each_segment(bv, req, iter) {
537 if (bv.bv_len & (blksize - 1))
538
539 return ERR_PTR(-EINVAL);
540 count += bv.bv_len >> (block->s2b_shift + 9);
541 }
542
543 if (count != last_rec - first_rec + 1)
544 return ERR_PTR(-EINVAL);
545
546 datasize = sizeof(struct dasd_diag_req) +
547 count*sizeof(struct dasd_diag_bio);
548 cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, datasize, memdev,
549 blk_mq_rq_to_pdu(req));
550 if (IS_ERR(cqr))
551 return cqr;
552
553 dreq = (struct dasd_diag_req *) cqr->data;
554 dreq->block_count = count;
555 dbio = dreq->bio;
556 recid = first_rec;
557 rq_for_each_segment(bv, req, iter) {
558 dst = page_address(bv.bv_page) + bv.bv_offset;
559 for (off = 0; off < bv.bv_len; off += blksize) {
560 memset(dbio, 0, sizeof (struct dasd_diag_bio));
561 dbio->type = rw_cmd;
562 dbio->block_number = recid + 1;
563 dbio->buffer = dst;
564 dbio++;
565 dst += blksize;
566 recid++;
567 }
568 }
569 cqr->retries = memdev->default_retries;
570 cqr->buildclk = get_tod_clock();
571 if (blk_noretry_request(req) ||
572 block->base->features & DASD_FEATURE_FAILFAST)
573 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
574 cqr->startdev = memdev;
575 cqr->memdev = memdev;
576 cqr->block = block;
577 cqr->expires = memdev->default_expires * HZ;
578 cqr->status = DASD_CQR_FILLED;
579 return cqr;
580}
581
582
583
584static int
585dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
586{
587 int status;
588
589 status = cqr->status == DASD_CQR_DONE;
590 dasd_sfree_request(cqr, cqr->memdev);
591 return status;
592}
593
594static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
595{
596 if (cqr->retries < 0)
597 cqr->status = DASD_CQR_FAILED;
598 else
599 cqr->status = DASD_CQR_FILLED;
600};
601
602
603static int
604dasd_diag_fill_info(struct dasd_device * device,
605 struct dasd_information2_t * info)
606{
607 struct dasd_diag_private *private = device->private;
608
609 info->label_block = (unsigned int) private->pt_block;
610 info->FBA_layout = 1;
611 info->format = DASD_FORMAT_LDL;
612 info->characteristics_size = sizeof(private->rdc_data);
613 memcpy(info->characteristics, &private->rdc_data,
614 sizeof(private->rdc_data));
615 info->confdata_size = 0;
616 return 0;
617}
618
619static void
620dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
621 struct irb *stat)
622{
623 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
624 "dump sense not available for DIAG data");
625}
626
627
628
629
630static void dasd_diag_setup_blk_queue(struct dasd_block *block)
631{
632 unsigned int logical_block_size = block->bp_block;
633 struct request_queue *q = block->request_queue;
634 int max;
635
636 max = DIAG_MAX_BLOCKS << block->s2b_shift;
637 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
638 q->limits.max_dev_sectors = max;
639 blk_queue_logical_block_size(q, logical_block_size);
640 blk_queue_max_hw_sectors(q, max);
641 blk_queue_max_segments(q, USHRT_MAX);
642
643 blk_queue_max_segment_size(q, PAGE_SIZE);
644 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
645}
646
647static struct dasd_discipline dasd_diag_discipline = {
648 .owner = THIS_MODULE,
649 .name = "DIAG",
650 .ebcname = "DIAG",
651 .check_device = dasd_diag_check_device,
652 .verify_path = dasd_generic_verify_path,
653 .fill_geometry = dasd_diag_fill_geometry,
654 .setup_blk_queue = dasd_diag_setup_blk_queue,
655 .start_IO = dasd_start_diag,
656 .term_IO = dasd_diag_term_IO,
657 .handle_terminated_request = dasd_diag_handle_terminated_request,
658 .erp_action = dasd_diag_erp_action,
659 .erp_postaction = dasd_diag_erp_postaction,
660 .build_cp = dasd_diag_build_cp,
661 .free_cp = dasd_diag_free_cp,
662 .dump_sense = dasd_diag_dump_sense,
663 .fill_info = dasd_diag_fill_info,
664};
665
666static int __init
667dasd_diag_init(void)
668{
669 if (!MACHINE_IS_VM) {
670 pr_info("Discipline %s cannot be used without z/VM\n",
671 dasd_diag_discipline.name);
672 return -ENODEV;
673 }
674 ASCEBC(dasd_diag_discipline.ebcname, 4);
675
676 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
677 register_external_irq(EXT_IRQ_CP_SERVICE, dasd_ext_handler);
678 dasd_diag_discipline_pointer = &dasd_diag_discipline;
679 return 0;
680}
681
682static void __exit
683dasd_diag_cleanup(void)
684{
685 unregister_external_irq(EXT_IRQ_CP_SERVICE, dasd_ext_handler);
686 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL);
687 dasd_diag_discipline_pointer = NULL;
688}
689
690module_init(dasd_diag_init);
691module_exit(dasd_diag_cleanup);
692