Linux 4.16.11
[linux/fpc-iii.git] / drivers / s390 / block / dasd_diag.c
blobf035c2f25d35a8c5aa12cb0552aaf3484edb2798
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 * Copyright IBM Corp. 1999, 2000
9 */
11 #define KMSG_COMPONENT "dasd"
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>
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 #include <asm/diag.h>
32 #include "dasd_int.h"
33 #include "dasd_diag.h"
35 #define PRINTK_HEADER "dasd(diag):"
37 MODULE_LICENSE("GPL");
39 /* The maximum number of blocks per request (max_blocks) is dependent on the
40 * amount of storage that is available in the static I/O buffer for each
41 * device. Currently each device gets 2 pages. We want to fit two requests
42 * into the available memory so that we can immediately start the next if one
43 * finishes. */
44 #define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
45 sizeof(struct dasd_diag_req)) / \
46 sizeof(struct dasd_diag_bio)) / 2)
47 #define DIAG_MAX_RETRIES 32
48 #define DIAG_TIMEOUT 50
50 static struct dasd_discipline dasd_diag_discipline;
52 struct dasd_diag_private {
53 struct dasd_diag_characteristics rdc_data;
54 struct dasd_diag_rw_io iob;
55 struct dasd_diag_init_io iib;
56 blocknum_t pt_block;
57 struct ccw_dev_id dev_id;
60 struct dasd_diag_req {
61 unsigned int block_count;
62 struct dasd_diag_bio bio[0];
65 static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
67 /* Perform DIAG250 call with block I/O parameter list iob (input and output)
68 * and function code cmd.
69 * In case of an exception return 3. Otherwise return result of bitwise OR of
70 * resulting condition code and DIAG return code. */
71 static inline int __dia250(void *iob, int cmd)
73 register unsigned long reg2 asm ("2") = (unsigned long) iob;
74 typedef union {
75 struct dasd_diag_init_io init_io;
76 struct dasd_diag_rw_io rw_io;
77 } addr_type;
78 int rc;
80 rc = 3;
81 asm volatile(
82 " diag 2,%2,0x250\n"
83 "0: ipm %0\n"
84 " srl %0,28\n"
85 " or %0,3\n"
86 "1:\n"
87 EX_TABLE(0b,1b)
88 : "+d" (rc), "=m" (*(addr_type *) iob)
89 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
90 : "3", "cc");
91 return rc;
94 static inline int dia250(void *iob, int cmd)
96 diag_stat_inc(DIAG_STAT_X250);
97 return __dia250(iob, cmd);
100 /* Initialize block I/O to DIAG device using the specified blocksize and
101 * block offset. On success, return zero and set end_block to contain the
102 * number of blocks on the device minus the specified offset. Return non-zero
103 * otherwise. */
104 static inline int
105 mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
106 blocknum_t offset, blocknum_t *end_block)
108 struct dasd_diag_private *private = device->private;
109 struct dasd_diag_init_io *iib = &private->iib;
110 int rc;
112 memset(iib, 0, sizeof (struct dasd_diag_init_io));
114 iib->dev_nr = private->dev_id.devno;
115 iib->block_size = blocksize;
116 iib->offset = offset;
117 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
119 rc = dia250(iib, INIT_BIO);
121 if ((rc & 3) == 0 && end_block)
122 *end_block = iib->end_block;
124 return rc;
127 /* Remove block I/O environment for device. Return zero on success, non-zero
128 * otherwise. */
129 static inline int
130 mdsk_term_io(struct dasd_device * device)
132 struct dasd_diag_private *private = device->private;
133 struct dasd_diag_init_io *iib = &private->iib;
134 int rc;
136 memset(iib, 0, sizeof (struct dasd_diag_init_io));
137 iib->dev_nr = private->dev_id.devno;
138 rc = dia250(iib, TERM_BIO);
139 return rc;
142 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
143 * environment. */
144 static void
145 dasd_diag_erp(struct dasd_device *device)
147 int rc;
149 mdsk_term_io(device);
150 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
151 if (rc == 4) {
152 if (!(test_and_set_bit(DASD_FLAG_DEVICE_RO, &device->flags)))
153 pr_warn("%s: The access mode of a DIAG device changed to read-only\n",
154 dev_name(&device->cdev->dev));
155 rc = 0;
157 if (rc)
158 pr_warn("%s: DIAG ERP failed with rc=%d\n",
159 dev_name(&device->cdev->dev), rc);
162 /* Start a given request at the device. Return zero on success, non-zero
163 * otherwise. */
164 static int
165 dasd_start_diag(struct dasd_ccw_req * cqr)
167 struct dasd_device *device;
168 struct dasd_diag_private *private;
169 struct dasd_diag_req *dreq;
170 int rc;
172 device = cqr->startdev;
173 if (cqr->retries < 0) {
174 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
175 "- no retry left)", cqr);
176 cqr->status = DASD_CQR_ERROR;
177 return -EIO;
179 private = device->private;
180 dreq = cqr->data;
182 private->iob.dev_nr = private->dev_id.devno;
183 private->iob.key = 0;
184 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
185 private->iob.block_count = dreq->block_count;
186 private->iob.interrupt_params = (addr_t) cqr;
187 private->iob.bio_list = dreq->bio;
188 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
190 cqr->startclk = get_tod_clock();
191 cqr->starttime = jiffies;
192 cqr->retries--;
194 rc = dia250(&private->iob, RW_BIO);
195 switch (rc) {
196 case 0: /* Synchronous I/O finished successfully */
197 cqr->stopclk = get_tod_clock();
198 cqr->status = DASD_CQR_SUCCESS;
199 /* Indicate to calling function that only a dasd_schedule_bh()
200 and no timer is needed */
201 rc = -EACCES;
202 break;
203 case 8: /* Asynchronous I/O was started */
204 cqr->status = DASD_CQR_IN_IO;
205 rc = 0;
206 break;
207 default: /* Error condition */
208 cqr->status = DASD_CQR_QUEUED;
209 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
210 dasd_diag_erp(device);
211 rc = -EIO;
212 break;
214 cqr->intrc = rc;
215 return rc;
218 /* Terminate given request at the device. */
219 static int
220 dasd_diag_term_IO(struct dasd_ccw_req * cqr)
222 struct dasd_device *device;
224 device = cqr->startdev;
225 mdsk_term_io(device);
226 mdsk_init_io(device, device->block->bp_block, 0, NULL);
227 cqr->status = DASD_CQR_CLEAR_PENDING;
228 cqr->stopclk = get_tod_clock();
229 dasd_schedule_device_bh(device);
230 return 0;
233 /* Handle external interruption. */
234 static void dasd_ext_handler(struct ext_code ext_code,
235 unsigned int param32, unsigned long param64)
237 struct dasd_ccw_req *cqr, *next;
238 struct dasd_device *device;
239 unsigned long expires;
240 unsigned long flags;
241 addr_t ip;
242 int rc;
244 switch (ext_code.subcode >> 8) {
245 case DASD_DIAG_CODE_31BIT:
246 ip = (addr_t) param32;
247 break;
248 case DASD_DIAG_CODE_64BIT:
249 ip = (addr_t) param64;
250 break;
251 default:
252 return;
254 inc_irq_stat(IRQEXT_DSD);
255 if (!ip) { /* no intparm: unsolicited interrupt */
256 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
257 "interrupt");
258 return;
260 cqr = (struct dasd_ccw_req *) ip;
261 device = (struct dasd_device *) cqr->startdev;
262 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
263 DBF_DEV_EVENT(DBF_WARNING, device,
264 " magic number of dasd_ccw_req 0x%08X doesn't"
265 " match discipline 0x%08X",
266 cqr->magic, *(int *) (&device->discipline->name));
267 return;
270 /* get irq lock to modify request queue */
271 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
273 /* Check for a pending clear operation */
274 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
275 cqr->status = DASD_CQR_CLEARED;
276 dasd_device_clear_timer(device);
277 dasd_schedule_device_bh(device);
278 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
279 return;
282 cqr->stopclk = get_tod_clock();
284 expires = 0;
285 if ((ext_code.subcode & 0xff) == 0) {
286 cqr->status = DASD_CQR_SUCCESS;
287 /* Start first request on queue if possible -> fast_io. */
288 if (!list_empty(&device->ccw_queue)) {
289 next = list_entry(device->ccw_queue.next,
290 struct dasd_ccw_req, devlist);
291 if (next->status == DASD_CQR_QUEUED) {
292 rc = dasd_start_diag(next);
293 if (rc == 0)
294 expires = next->expires;
297 } else {
298 cqr->status = DASD_CQR_QUEUED;
299 DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
300 "request %p was %d (%d retries left)", cqr,
301 ext_code.subcode & 0xff, cqr->retries);
302 dasd_diag_erp(device);
305 if (expires != 0)
306 dasd_device_set_timer(device, expires);
307 else
308 dasd_device_clear_timer(device);
309 dasd_schedule_device_bh(device);
311 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
314 /* Check whether device can be controlled by DIAG discipline. Return zero on
315 * success, non-zero otherwise. */
316 static int
317 dasd_diag_check_device(struct dasd_device *device)
319 struct dasd_diag_private *private = device->private;
320 struct dasd_diag_characteristics *rdc_data;
321 struct vtoc_cms_label *label;
322 struct dasd_block *block;
323 struct dasd_diag_bio bio;
324 unsigned int sb, bsize;
325 blocknum_t end_block;
326 int rc;
328 if (private == NULL) {
329 private = kzalloc(sizeof(*private), GFP_KERNEL);
330 if (private == NULL) {
331 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
332 "Allocating memory for private DASD data "
333 "failed\n");
334 return -ENOMEM;
336 ccw_device_get_id(device->cdev, &private->dev_id);
337 device->private = private;
339 block = dasd_alloc_block();
340 if (IS_ERR(block)) {
341 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
342 "could not allocate dasd block structure");
343 device->private = NULL;
344 kfree(private);
345 return PTR_ERR(block);
347 device->block = block;
348 block->base = device;
350 /* Read Device Characteristics */
351 rdc_data = &private->rdc_data;
352 rdc_data->dev_nr = private->dev_id.devno;
353 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
355 rc = diag210((struct diag210 *) rdc_data);
356 if (rc) {
357 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
358 "information (rc=%d)", rc);
359 rc = -EOPNOTSUPP;
360 goto out;
363 device->default_expires = DIAG_TIMEOUT;
364 device->default_retries = DIAG_MAX_RETRIES;
366 /* Figure out position of label block */
367 switch (private->rdc_data.vdev_class) {
368 case DEV_CLASS_FBA:
369 private->pt_block = 1;
370 break;
371 case DEV_CLASS_ECKD:
372 private->pt_block = 2;
373 break;
374 default:
375 pr_warn("%s: Device type %d is not supported in DIAG mode\n",
376 dev_name(&device->cdev->dev),
377 private->rdc_data.vdev_class);
378 rc = -EOPNOTSUPP;
379 goto out;
382 DBF_DEV_EVENT(DBF_INFO, device,
383 "%04X: %04X on real %04X/%02X",
384 rdc_data->dev_nr,
385 rdc_data->vdev_type,
386 rdc_data->rdev_type, rdc_data->rdev_model);
388 /* terminate all outstanding operations */
389 mdsk_term_io(device);
391 /* figure out blocksize of device */
392 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
393 if (label == NULL) {
394 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
395 "No memory to allocate initialization request");
396 rc = -ENOMEM;
397 goto out;
399 rc = 0;
400 end_block = 0;
401 /* try all sizes - needed for ECKD devices */
402 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
403 mdsk_init_io(device, bsize, 0, &end_block);
404 memset(&bio, 0, sizeof (struct dasd_diag_bio));
405 bio.type = MDSK_READ_REQ;
406 bio.block_number = private->pt_block + 1;
407 bio.buffer = label;
408 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
409 private->iob.dev_nr = rdc_data->dev_nr;
410 private->iob.key = 0;
411 private->iob.flags = 0; /* do synchronous io */
412 private->iob.block_count = 1;
413 private->iob.interrupt_params = 0;
414 private->iob.bio_list = &bio;
415 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
416 rc = dia250(&private->iob, RW_BIO);
417 if (rc == 3) {
418 pr_warn("%s: A 64-bit DIAG call failed\n",
419 dev_name(&device->cdev->dev));
420 rc = -EOPNOTSUPP;
421 goto out_label;
423 mdsk_term_io(device);
424 if (rc == 0)
425 break;
427 if (bsize > PAGE_SIZE) {
428 pr_warn("%s: Accessing the DASD failed because of an incorrect format (rc=%d)\n",
429 dev_name(&device->cdev->dev), rc);
430 rc = -EIO;
431 goto out_label;
433 /* check for label block */
434 if (memcmp(label->label_id, DASD_DIAG_CMS1,
435 sizeof(DASD_DIAG_CMS1)) == 0) {
436 /* get formatted blocksize from label block */
437 bsize = (unsigned int) label->block_size;
438 block->blocks = (unsigned long) label->block_count;
439 } else
440 block->blocks = end_block;
441 block->bp_block = bsize;
442 block->s2b_shift = 0; /* bits to shift 512 to get a block */
443 for (sb = 512; sb < bsize; sb = sb << 1)
444 block->s2b_shift++;
445 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
446 if (rc && (rc != 4)) {
447 pr_warn("%s: DIAG initialization failed with rc=%d\n",
448 dev_name(&device->cdev->dev), rc);
449 rc = -EIO;
450 } else {
451 if (rc == 4)
452 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
453 pr_info("%s: New DASD with %ld byte/block, total size %ld "
454 "KB%s\n", dev_name(&device->cdev->dev),
455 (unsigned long) block->bp_block,
456 (unsigned long) (block->blocks <<
457 block->s2b_shift) >> 1,
458 (rc == 4) ? ", read-only device" : "");
459 rc = 0;
461 out_label:
462 free_page((long) label);
463 out:
464 if (rc) {
465 device->block = NULL;
466 dasd_free_block(block);
467 device->private = NULL;
468 kfree(private);
470 return rc;
473 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
474 * otherwise. */
475 static int
476 dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
478 if (dasd_check_blocksize(block->bp_block) != 0)
479 return -EINVAL;
480 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
481 geo->heads = 16;
482 geo->sectors = 128 >> block->s2b_shift;
483 return 0;
486 static dasd_erp_fn_t
487 dasd_diag_erp_action(struct dasd_ccw_req * cqr)
489 return dasd_default_erp_action;
492 static dasd_erp_fn_t
493 dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
495 return dasd_default_erp_postaction;
498 /* Create DASD request from block device request. Return pointer to new
499 * request on success, ERR_PTR otherwise. */
500 static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
501 struct dasd_block *block,
502 struct request *req)
504 struct dasd_ccw_req *cqr;
505 struct dasd_diag_req *dreq;
506 struct dasd_diag_bio *dbio;
507 struct req_iterator iter;
508 struct bio_vec bv;
509 char *dst;
510 unsigned int count, datasize;
511 sector_t recid, first_rec, last_rec;
512 unsigned int blksize, off;
513 unsigned char rw_cmd;
515 if (rq_data_dir(req) == READ)
516 rw_cmd = MDSK_READ_REQ;
517 else if (rq_data_dir(req) == WRITE)
518 rw_cmd = MDSK_WRITE_REQ;
519 else
520 return ERR_PTR(-EINVAL);
521 blksize = block->bp_block;
522 /* Calculate record id of first and last block. */
523 first_rec = blk_rq_pos(req) >> block->s2b_shift;
524 last_rec =
525 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
526 /* Check struct bio and count the number of blocks for the request. */
527 count = 0;
528 rq_for_each_segment(bv, req, iter) {
529 if (bv.bv_len & (blksize - 1))
530 /* Fba can only do full blocks. */
531 return ERR_PTR(-EINVAL);
532 count += bv.bv_len >> (block->s2b_shift + 9);
534 /* Paranoia. */
535 if (count != last_rec - first_rec + 1)
536 return ERR_PTR(-EINVAL);
537 /* Build the request */
538 datasize = sizeof(struct dasd_diag_req) +
539 count*sizeof(struct dasd_diag_bio);
540 cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, datasize, memdev);
541 if (IS_ERR(cqr))
542 return cqr;
544 dreq = (struct dasd_diag_req *) cqr->data;
545 dreq->block_count = count;
546 dbio = dreq->bio;
547 recid = first_rec;
548 rq_for_each_segment(bv, req, iter) {
549 dst = page_address(bv.bv_page) + bv.bv_offset;
550 for (off = 0; off < bv.bv_len; off += blksize) {
551 memset(dbio, 0, sizeof (struct dasd_diag_bio));
552 dbio->type = rw_cmd;
553 dbio->block_number = recid + 1;
554 dbio->buffer = dst;
555 dbio++;
556 dst += blksize;
557 recid++;
560 cqr->retries = memdev->default_retries;
561 cqr->buildclk = get_tod_clock();
562 if (blk_noretry_request(req) ||
563 block->base->features & DASD_FEATURE_FAILFAST)
564 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
565 cqr->startdev = memdev;
566 cqr->memdev = memdev;
567 cqr->block = block;
568 cqr->expires = memdev->default_expires * HZ;
569 cqr->status = DASD_CQR_FILLED;
570 return cqr;
573 /* Release DASD request. Return non-zero if request was successful, zero
574 * otherwise. */
575 static int
576 dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
578 int status;
580 status = cqr->status == DASD_CQR_DONE;
581 dasd_sfree_request(cqr, cqr->memdev);
582 return status;
585 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
587 if (cqr->retries < 0)
588 cqr->status = DASD_CQR_FAILED;
589 else
590 cqr->status = DASD_CQR_FILLED;
593 /* Fill in IOCTL data for device. */
594 static int
595 dasd_diag_fill_info(struct dasd_device * device,
596 struct dasd_information2_t * info)
598 struct dasd_diag_private *private = device->private;
600 info->label_block = (unsigned int) private->pt_block;
601 info->FBA_layout = 1;
602 info->format = DASD_FORMAT_LDL;
603 info->characteristics_size = sizeof(private->rdc_data);
604 memcpy(info->characteristics, &private->rdc_data,
605 sizeof(private->rdc_data));
606 info->confdata_size = 0;
607 return 0;
610 static void
611 dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
612 struct irb *stat)
614 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
615 "dump sense not available for DIAG data");
618 static struct dasd_discipline dasd_diag_discipline = {
619 .owner = THIS_MODULE,
620 .name = "DIAG",
621 .ebcname = "DIAG",
622 .max_blocks = DIAG_MAX_BLOCKS,
623 .check_device = dasd_diag_check_device,
624 .verify_path = dasd_generic_verify_path,
625 .fill_geometry = dasd_diag_fill_geometry,
626 .start_IO = dasd_start_diag,
627 .term_IO = dasd_diag_term_IO,
628 .handle_terminated_request = dasd_diag_handle_terminated_request,
629 .erp_action = dasd_diag_erp_action,
630 .erp_postaction = dasd_diag_erp_postaction,
631 .build_cp = dasd_diag_build_cp,
632 .free_cp = dasd_diag_free_cp,
633 .dump_sense = dasd_diag_dump_sense,
634 .fill_info = dasd_diag_fill_info,
637 static int __init
638 dasd_diag_init(void)
640 if (!MACHINE_IS_VM) {
641 pr_info("Discipline %s cannot be used without z/VM\n",
642 dasd_diag_discipline.name);
643 return -ENODEV;
645 ASCEBC(dasd_diag_discipline.ebcname, 4);
647 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
648 register_external_irq(EXT_IRQ_CP_SERVICE, dasd_ext_handler);
649 dasd_diag_discipline_pointer = &dasd_diag_discipline;
650 return 0;
653 static void __exit
654 dasd_diag_cleanup(void)
656 unregister_external_irq(EXT_IRQ_CP_SERVICE, dasd_ext_handler);
657 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL);
658 dasd_diag_discipline_pointer = NULL;
661 module_init(dasd_diag_init);
662 module_exit(dasd_diag_cleanup);