1 // SPDX-License-Identifier: GPL-2.0
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
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>
24 #include <asm/debug.h>
26 #include <asm/ebcdic.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
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
;
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
;
75 struct dasd_diag_init_io init_io
;
76 struct dasd_diag_rw_io rw_io
;
88 : "+d" (rc
), "=m" (*(addr_type
*) iob
)
89 : "d" (cmd
), "d" (reg2
), "m" (*(addr_type
*) iob
)
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
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
;
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
;
127 /* Remove block I/O environment for device. Return zero on success, non-zero
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
;
136 memset(iib
, 0, sizeof (struct dasd_diag_init_io
));
137 iib
->dev_nr
= private->dev_id
.devno
;
138 rc
= dia250(iib
, TERM_BIO
);
142 /* Error recovery for failed DIAG requests - try to reestablish the DIAG
145 dasd_diag_erp(struct dasd_device
*device
)
149 mdsk_term_io(device
);
150 rc
= mdsk_init_io(device
, device
->block
->bp_block
, 0, NULL
);
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
));
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
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
;
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
;
179 private = device
->private;
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
;
194 rc
= dia250(&private->iob
, RW_BIO
);
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 */
203 case 8: /* Asynchronous I/O was started */
204 cqr
->status
= DASD_CQR_IN_IO
;
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
);
218 /* Terminate given request at the device. */
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
);
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
;
244 switch (ext_code
.subcode
>> 8) {
245 case DASD_DIAG_CODE_31BIT
:
246 ip
= (addr_t
) param32
;
248 case DASD_DIAG_CODE_64BIT
:
249 ip
= (addr_t
) param64
;
254 inc_irq_stat(IRQEXT_DSD
);
255 if (!ip
) { /* no intparm: unsolicited interrupt */
256 DBF_EVENT(DBF_NOTICE
, "%s", "caught unsolicited "
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
));
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
);
282 cqr
->stopclk
= get_tod_clock();
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
);
294 expires
= next
->expires
;
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
);
306 dasd_device_set_timer(device
, expires
);
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. */
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
;
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 "
336 ccw_device_get_id(device
->cdev
, &private->dev_id
);
337 device
->private = private;
339 block
= dasd_alloc_block();
341 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
342 "could not allocate dasd block structure");
343 device
->private = NULL
;
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
);
357 DBF_DEV_EVENT(DBF_WARNING
, device
, "failed to retrieve device "
358 "information (rc=%d)", rc
);
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
) {
369 private->pt_block
= 1;
372 private->pt_block
= 2;
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
);
382 DBF_DEV_EVENT(DBF_INFO
, device
,
383 "%04X: %04X on real %04X/%02X",
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
);
394 DBF_DEV_EVENT(DBF_WARNING
, device
, "%s",
395 "No memory to allocate initialization request");
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;
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
);
418 pr_warn("%s: A 64-bit DIAG call failed\n",
419 dev_name(&device
->cdev
->dev
));
423 mdsk_term_io(device
);
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
);
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
;
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)
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
);
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" : "");
462 free_page((long) label
);
465 device
->block
= NULL
;
466 dasd_free_block(block
);
467 device
->private = NULL
;
473 /* Fill in virtual disk geometry for device. Return zero on success, non-zero
476 dasd_diag_fill_geometry(struct dasd_block
*block
, struct hd_geometry
*geo
)
478 if (dasd_check_blocksize(block
->bp_block
) != 0)
480 geo
->cylinders
= (block
->blocks
<< block
->s2b_shift
) >> 10;
482 geo
->sectors
= 128 >> block
->s2b_shift
;
487 dasd_diag_erp_action(struct dasd_ccw_req
* cqr
)
489 return dasd_default_erp_action
;
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
,
504 struct dasd_ccw_req
*cqr
;
505 struct dasd_diag_req
*dreq
;
506 struct dasd_diag_bio
*dbio
;
507 struct req_iterator iter
;
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
;
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
;
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. */
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);
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
);
544 dreq
= (struct dasd_diag_req
*) cqr
->data
;
545 dreq
->block_count
= count
;
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
));
553 dbio
->block_number
= recid
+ 1;
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
;
568 cqr
->expires
= memdev
->default_expires
* HZ
;
569 cqr
->status
= DASD_CQR_FILLED
;
573 /* Release DASD request. Return non-zero if request was successful, zero
576 dasd_diag_free_cp(struct dasd_ccw_req
*cqr
, struct request
*req
)
580 status
= cqr
->status
== DASD_CQR_DONE
;
581 dasd_sfree_request(cqr
, cqr
->memdev
);
585 static void dasd_diag_handle_terminated_request(struct dasd_ccw_req
*cqr
)
587 if (cqr
->retries
< 0)
588 cqr
->status
= DASD_CQR_FAILED
;
590 cqr
->status
= DASD_CQR_FILLED
;
593 /* Fill in IOCTL data for device. */
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;
611 dasd_diag_dump_sense(struct dasd_device
*device
, struct dasd_ccw_req
* req
,
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
,
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
,
640 if (!MACHINE_IS_VM
) {
641 pr_info("Discipline %s cannot be used without z/VM\n",
642 dasd_diag_discipline
.name
);
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
;
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
);