dasd: move queue setup to common code
[linux-block.git] / drivers / s390 / block / dasd_diag.c
CommitLineData
6a55d2cd 1// SPDX-License-Identifier: GPL-2.0
138c014d 2/*
1da177e4
LT
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>
a53c8fab 7 * Copyright IBM Corp. 1999, 2000
1da177e4 8 *
1da177e4
LT
9 */
10
052ff461 11#include <linux/kernel_stat.h>
1da177e4
LT
12#include <linux/stddef.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
fd49f41a 15#include <linux/hdreg.h>
1da177e4
LT
16#include <linux/bio.h>
17#include <linux/module.h>
18#include <linux/init.h>
fd49f41a 19#include <linux/jiffies.h>
d09a307f 20#include <asm/asm-extable.h>
1da177e4
LT
21#include <asm/dasd.h>
22#include <asm/debug.h>
1ec2772e 23#include <asm/diag.h>
1da177e4 24#include <asm/ebcdic.h>
b378a982 25#include <linux/io.h>
d7b250e2 26#include <asm/irq.h>
56dc6a88 27#include <asm/vtoc.h>
1da177e4
LT
28
29#include "dasd_int.h"
30#include "dasd_diag.h"
31
1da177e4
LT
32MODULE_LICENSE("GPL");
33
fd49f41a
HH
34/* The maximum number of blocks per request (max_blocks) is dependent on the
35 * amount of storage that is available in the static I/O buffer for each
36 * device. Currently each device gets 2 pages. We want to fit two requests
37 * into the available memory so that we can immediately start the next if one
38 * finishes. */
39#define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
40 sizeof(struct dasd_diag_req)) / \
41 sizeof(struct dasd_diag_bio)) / 2)
42#define DIAG_MAX_RETRIES 32
7c8faa86 43#define DIAG_TIMEOUT 50
fd49f41a 44
2b67fc46 45static struct dasd_discipline dasd_diag_discipline;
1da177e4
LT
46
47struct dasd_diag_private {
48 struct dasd_diag_characteristics rdc_data;
49 struct dasd_diag_rw_io iob;
50 struct dasd_diag_init_io iib;
fd49f41a 51 blocknum_t pt_block;
9a92fe48 52 struct ccw_dev_id dev_id;
1da177e4
LT
53};
54
55struct dasd_diag_req {
fd49f41a 56 unsigned int block_count;
fa226f1d 57 struct dasd_diag_bio bio[];
1da177e4
LT
58};
59
fd49f41a
HH
60static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
61
62/* Perform DIAG250 call with block I/O parameter list iob (input and output)
63 * and function code cmd.
64 * In case of an exception return 3. Otherwise return result of bitwise OR of
65 * resulting condition code and DIAG return code. */
ce0c12b6 66static inline int __dia250(void *iob, int cmd)
1da177e4 67{
d4a01902 68 union register_pair rx = { .even = (unsigned long)iob, };
86b368a5
PO
69 typedef union {
70 struct dasd_diag_init_io init_io;
71 struct dasd_diag_rw_io rw_io;
fd49f41a 72 } addr_type;
d4a01902 73 int cc;
1da177e4 74
d4a01902 75 cc = 3;
94c12cc7 76 asm volatile(
d4a01902
HC
77 " diag %[rx],%[cmd],0x250\n"
78 "0: ipm %[cc]\n"
79 " srl %[cc],28\n"
fd49f41a 80 "1:\n"
94c12cc7 81 EX_TABLE(0b,1b)
d4a01902
HC
82 : [cc] "+&d" (cc), [rx] "+&d" (rx.pair), "+m" (*(addr_type *)iob)
83 : [cmd] "d" (cmd)
84 : "cc");
85 return cc | rx.odd;
1da177e4
LT
86}
87
ce0c12b6
HC
88static inline int dia250(void *iob, int cmd)
89{
90 diag_stat_inc(DIAG_STAT_X250);
91 return __dia250(iob, cmd);
92}
93
fd49f41a
HH
94/* Initialize block I/O to DIAG device using the specified blocksize and
95 * block offset. On success, return zero and set end_block to contain the
96 * number of blocks on the device minus the specified offset. Return non-zero
97 * otherwise. */
4d284cac 98static inline int
fd49f41a
HH
99mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
100 blocknum_t offset, blocknum_t *end_block)
1da177e4 101{
543691a4
SO
102 struct dasd_diag_private *private = device->private;
103 struct dasd_diag_init_io *iib = &private->iib;
1da177e4
LT
104 int rc;
105
1da177e4
LT
106 memset(iib, 0, sizeof (struct dasd_diag_init_io));
107
9a92fe48 108 iib->dev_nr = private->dev_id.devno;
1da177e4
LT
109 iib->block_size = blocksize;
110 iib->offset = offset;
fd49f41a 111 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
1da177e4
LT
112
113 rc = dia250(iib, INIT_BIO);
114
fd49f41a
HH
115 if ((rc & 3) == 0 && end_block)
116 *end_block = iib->end_block;
117
118 return rc;
1da177e4
LT
119}
120
fd49f41a
HH
121/* Remove block I/O environment for device. Return zero on success, non-zero
122 * otherwise. */
4d284cac 123static inline int
1da177e4
LT
124mdsk_term_io(struct dasd_device * device)
125{
543691a4
SO
126 struct dasd_diag_private *private = device->private;
127 struct dasd_diag_init_io *iib = &private->iib;
1da177e4
LT
128 int rc;
129
1da177e4 130 memset(iib, 0, sizeof (struct dasd_diag_init_io));
9a92fe48 131 iib->dev_nr = private->dev_id.devno;
1da177e4 132 rc = dia250(iib, TERM_BIO);
fd49f41a
HH
133 return rc;
134}
135
136/* Error recovery for failed DIAG requests - try to reestablish the DIAG
137 * environment. */
138static void
139dasd_diag_erp(struct dasd_device *device)
140{
141 int rc;
142
143 mdsk_term_io(device);
8e09f215 144 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
22825ab7 145 if (rc == 4) {
33b62a30 146 if (!(test_and_set_bit(DASD_FLAG_DEVICE_RO, &device->flags)))
baebc70a
JP
147 pr_warn("%s: The access mode of a DIAG device changed to read-only\n",
148 dev_name(&device->cdev->dev));
22825ab7
SW
149 rc = 0;
150 }
fd49f41a 151 if (rc)
baebc70a
JP
152 pr_warn("%s: DIAG ERP failed with rc=%d\n",
153 dev_name(&device->cdev->dev), rc);
1da177e4
LT
154}
155
fd49f41a
HH
156/* Start a given request at the device. Return zero on success, non-zero
157 * otherwise. */
1da177e4
LT
158static int
159dasd_start_diag(struct dasd_ccw_req * cqr)
160{
161 struct dasd_device *device;
162 struct dasd_diag_private *private;
163 struct dasd_diag_req *dreq;
164 int rc;
165
8e09f215 166 device = cqr->startdev;
fd49f41a 167 if (cqr->retries < 0) {
fc19f381 168 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
fd49f41a 169 "- no retry left)", cqr);
8e09f215 170 cqr->status = DASD_CQR_ERROR;
fd49f41a
HH
171 return -EIO;
172 }
543691a4
SO
173 private = device->private;
174 dreq = cqr->data;
1da177e4 175
9a92fe48 176 private->iob.dev_nr = private->dev_id.devno;
1da177e4 177 private->iob.key = 0;
fd49f41a 178 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
1da177e4 179 private->iob.block_count = dreq->block_count;
fd49f41a 180 private->iob.interrupt_params = (addr_t) cqr;
86b368a5 181 private->iob.bio_list = dreq->bio;
fd49f41a 182 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
1da177e4 183
1aae0560 184 cqr->startclk = get_tod_clock();
fd49f41a
HH
185 cqr->starttime = jiffies;
186 cqr->retries--;
1da177e4
LT
187
188 rc = dia250(&private->iob, RW_BIO);
fd49f41a
HH
189 switch (rc) {
190 case 0: /* Synchronous I/O finished successfully */
1aae0560 191 cqr->stopclk = get_tod_clock();
8e09f215 192 cqr->status = DASD_CQR_SUCCESS;
fd49f41a
HH
193 /* Indicate to calling function that only a dasd_schedule_bh()
194 and no timer is needed */
195 rc = -EACCES;
196 break;
197 case 8: /* Asynchronous I/O was started */
1da177e4
LT
198 cqr->status = DASD_CQR_IN_IO;
199 rc = 0;
fd49f41a
HH
200 break;
201 default: /* Error condition */
202 cqr->status = DASD_CQR_QUEUED;
fc19f381 203 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
fd49f41a
HH
204 dasd_diag_erp(device);
205 rc = -EIO;
206 break;
1da177e4 207 }
6cc7f168 208 cqr->intrc = rc;
1da177e4
LT
209 return rc;
210}
211
fd49f41a
HH
212/* Terminate given request at the device. */
213static int
214dasd_diag_term_IO(struct dasd_ccw_req * cqr)
215{
216 struct dasd_device *device;
217
8e09f215 218 device = cqr->startdev;
fd49f41a 219 mdsk_term_io(device);
8e09f215
SW
220 mdsk_init_io(device, device->block->bp_block, 0, NULL);
221 cqr->status = DASD_CQR_CLEAR_PENDING;
1aae0560 222 cqr->stopclk = get_tod_clock();
8e09f215 223 dasd_schedule_device_bh(device);
fd49f41a
HH
224 return 0;
225}
226
227/* Handle external interruption. */
fde15c3a 228static void dasd_ext_handler(struct ext_code ext_code,
f6649a7e 229 unsigned int param32, unsigned long param64)
1da177e4
LT
230{
231 struct dasd_ccw_req *cqr, *next;
232 struct dasd_device *device;
7bf76f01 233 unsigned long expires;
1da177e4 234 unsigned long flags;
fd49f41a
HH
235 addr_t ip;
236 int rc;
1da177e4 237
fde15c3a 238 switch (ext_code.subcode >> 8) {
fd49f41a 239 case DASD_DIAG_CODE_31BIT:
f6649a7e 240 ip = (addr_t) param32;
fd49f41a
HH
241 break;
242 case DASD_DIAG_CODE_64BIT:
f6649a7e 243 ip = (addr_t) param64;
fd49f41a
HH
244 break;
245 default:
246 return;
247 }
420f42ec 248 inc_irq_stat(IRQEXT_DSD);
1da177e4 249 if (!ip) { /* no intparm: unsolicited interrupt */
fc19f381
SH
250 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
251 "interrupt");
1da177e4
LT
252 return;
253 }
fd49f41a 254 cqr = (struct dasd_ccw_req *) ip;
8e09f215 255 device = (struct dasd_device *) cqr->startdev;
1da177e4 256 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
fc19f381 257 DBF_DEV_EVENT(DBF_WARNING, device,
1da177e4
LT
258 " magic number of dasd_ccw_req 0x%08X doesn't"
259 " match discipline 0x%08X",
260 cqr->magic, *(int *) (&device->discipline->name));
261 return;
262 }
263
264 /* get irq lock to modify request queue */
265 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
266
fd49f41a 267 /* Check for a pending clear operation */
8e09f215
SW
268 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
269 cqr->status = DASD_CQR_CLEARED;
270 dasd_device_clear_timer(device);
271 dasd_schedule_device_bh(device);
fd49f41a
HH
272 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
273 return;
274 }
275
1aae0560 276 cqr->stopclk = get_tod_clock();
1da177e4
LT
277
278 expires = 0;
fde15c3a 279 if ((ext_code.subcode & 0xff) == 0) {
8e09f215 280 cqr->status = DASD_CQR_SUCCESS;
1da177e4
LT
281 /* Start first request on queue if possible -> fast_io. */
282 if (!list_empty(&device->ccw_queue)) {
283 next = list_entry(device->ccw_queue.next,
8e09f215 284 struct dasd_ccw_req, devlist);
1da177e4 285 if (next->status == DASD_CQR_QUEUED) {
fd49f41a
HH
286 rc = dasd_start_diag(next);
287 if (rc == 0)
1da177e4 288 expires = next->expires;
1da177e4
LT
289 }
290 }
fd49f41a
HH
291 } else {
292 cqr->status = DASD_CQR_QUEUED;
fc19f381 293 DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
f6649a7e 294 "request %p was %d (%d retries left)", cqr,
fde15c3a 295 ext_code.subcode & 0xff, cqr->retries);
fd49f41a
HH
296 dasd_diag_erp(device);
297 }
1da177e4
LT
298
299 if (expires != 0)
8e09f215 300 dasd_device_set_timer(device, expires);
1da177e4 301 else
8e09f215
SW
302 dasd_device_clear_timer(device);
303 dasd_schedule_device_bh(device);
1da177e4
LT
304
305 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
306}
307
fd49f41a
HH
308/* Check whether device can be controlled by DIAG discipline. Return zero on
309 * success, non-zero otherwise. */
1da177e4
LT
310static int
311dasd_diag_check_device(struct dasd_device *device)
312{
543691a4 313 struct dasd_diag_private *private = device->private;
1da177e4 314 struct dasd_diag_characteristics *rdc_data;
56dc6a88 315 struct vtoc_cms_label *label;
543691a4 316 struct dasd_block *block;
9f4aa523 317 struct dasd_diag_bio *bio;
fd49f41a 318 unsigned int sb, bsize;
543691a4 319 blocknum_t end_block;
1da177e4
LT
320 int rc;
321
1da177e4 322 if (private == NULL) {
543691a4 323 private = kzalloc(sizeof(*private), GFP_KERNEL);
1da177e4 324 if (private == NULL) {
fc19f381
SH
325 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
326 "Allocating memory for private DASD data "
327 "failed\n");
1da177e4
LT
328 return -ENOMEM;
329 }
9a92fe48 330 ccw_device_get_id(device->cdev, &private->dev_id);
543691a4 331 device->private = private;
1da177e4 332 }
8e09f215
SW
333 block = dasd_alloc_block();
334 if (IS_ERR(block)) {
fc19f381 335 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
8e09f215 336 "could not allocate dasd block structure");
7337194f
CH
337 device->private = NULL;
338 kfree(private);
8e09f215
SW
339 return PTR_ERR(block);
340 }
341 device->block = block;
342 block->base = device;
343
1da177e4 344 /* Read Device Characteristics */
543691a4 345 rdc_data = &private->rdc_data;
9a92fe48 346 rdc_data->dev_nr = private->dev_id.devno;
1da177e4
LT
347 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
348
349 rc = diag210((struct diag210 *) rdc_data);
fd49f41a 350 if (rc) {
fc19f381 351 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
fd49f41a 352 "information (rc=%d)", rc);
8586cb60 353 rc = -EOPNOTSUPP;
7337194f 354 goto out;
fd49f41a 355 }
1da177e4 356
7c8faa86 357 device->default_expires = DIAG_TIMEOUT;
1f1ee9ad 358 device->default_retries = DIAG_MAX_RETRIES;
7c8faa86 359
1da177e4
LT
360 /* Figure out position of label block */
361 switch (private->rdc_data.vdev_class) {
362 case DEV_CLASS_FBA:
363 private->pt_block = 1;
364 break;
365 case DEV_CLASS_ECKD:
366 private->pt_block = 2;
367 break;
368 default:
baebc70a
JP
369 pr_warn("%s: Device type %d is not supported in DIAG mode\n",
370 dev_name(&device->cdev->dev),
371 private->rdc_data.vdev_class);
8586cb60 372 rc = -EOPNOTSUPP;
7337194f 373 goto out;
1da177e4
LT
374 }
375
376 DBF_DEV_EVENT(DBF_INFO, device,
377 "%04X: %04X on real %04X/%02X",
378 rdc_data->dev_nr,
379 rdc_data->vdev_type,
380 rdc_data->rdev_type, rdc_data->rdev_model);
381
382 /* terminate all outstanding operations */
383 mdsk_term_io(device);
384
385 /* figure out blocksize of device */
56dc6a88 386 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
1da177e4 387 if (label == NULL) {
fc19f381 388 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4 389 "No memory to allocate initialization request");
7337194f
CH
390 rc = -ENOMEM;
391 goto out;
1da177e4 392 }
9f4aa523
SH
393 bio = kzalloc(sizeof(*bio), GFP_KERNEL);
394 if (bio == NULL) {
395 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
396 "No memory to allocate initialization bio");
397 rc = -ENOMEM;
398 goto out_label;
399 }
fd49f41a
HH
400 rc = 0;
401 end_block = 0;
1da177e4
LT
402 /* try all sizes - needed for ECKD devices */
403 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
fd49f41a 404 mdsk_init_io(device, bsize, 0, &end_block);
9f4aa523
SH
405 memset(bio, 0, sizeof(*bio));
406 bio->type = MDSK_READ_REQ;
407 bio->block_number = private->pt_block + 1;
408 bio->buffer = label;
1da177e4
LT
409 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
410 private->iob.dev_nr = rdc_data->dev_nr;
411 private->iob.key = 0;
412 private->iob.flags = 0; /* do synchronous io */
413 private->iob.block_count = 1;
414 private->iob.interrupt_params = 0;
9f4aa523 415 private->iob.bio_list = bio;
fd49f41a
HH
416 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
417 rc = dia250(&private->iob, RW_BIO);
1e0291ba 418 if (rc == 3) {
baebc70a
JP
419 pr_warn("%s: A 64-bit DIAG call failed\n",
420 dev_name(&device->cdev->dev));
1e0291ba 421 rc = -EOPNOTSUPP;
9f4aa523 422 goto out_bio;
1e0291ba 423 }
1da177e4 424 mdsk_term_io(device);
1e0291ba
PO
425 if (rc == 0)
426 break;
1da177e4 427 }
1e0291ba 428 if (bsize > PAGE_SIZE) {
baebc70a
JP
429 pr_warn("%s: Accessing the DASD failed because of an incorrect format (rc=%d)\n",
430 dev_name(&device->cdev->dev), rc);
fd49f41a 431 rc = -EIO;
9f4aa523 432 goto out_bio;
1e0291ba
PO
433 }
434 /* check for label block */
435 if (memcmp(label->label_id, DASD_DIAG_CMS1,
436 sizeof(DASD_DIAG_CMS1)) == 0) {
437 /* get formatted blocksize from label block */
438 bsize = (unsigned int) label->block_size;
8e09f215 439 block->blocks = (unsigned long) label->block_count;
1e0291ba 440 } else
8e09f215
SW
441 block->blocks = end_block;
442 block->bp_block = bsize;
443 block->s2b_shift = 0; /* bits to shift 512 to get a block */
1e0291ba 444 for (sb = 512; sb < bsize; sb = sb << 1)
8e09f215
SW
445 block->s2b_shift++;
446 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
22825ab7 447 if (rc && (rc != 4)) {
baebc70a
JP
448 pr_warn("%s: DIAG initialization failed with rc=%d\n",
449 dev_name(&device->cdev->dev), rc);
1e0291ba 450 rc = -EIO;
fd49f41a 451 } else {
22825ab7 452 if (rc == 4)
33b62a30 453 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
ea058544
SH
454 pr_info("%s: New DASD with %ld byte/block, total size %ld "
455 "KB%s\n", dev_name(&device->cdev->dev),
456 (unsigned long) block->bp_block,
457 (unsigned long) (block->blocks <<
458 block->s2b_shift) >> 1,
459 (rc == 4) ? ", read-only device" : "");
22825ab7 460 rc = 0;
1da177e4 461 }
9f4aa523
SH
462out_bio:
463 kfree(bio);
7337194f 464out_label:
1da177e4 465 free_page((long) label);
7337194f
CH
466out:
467 if (rc) {
468 device->block = NULL;
469 dasd_free_block(block);
470 device->private = NULL;
471 kfree(private);
472 }
1da177e4
LT
473 return rc;
474}
475
fd49f41a
HH
476/* Fill in virtual disk geometry for device. Return zero on success, non-zero
477 * otherwise. */
1da177e4 478static int
8e09f215 479dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
1da177e4 480{
8e09f215 481 if (dasd_check_blocksize(block->bp_block) != 0)
1da177e4 482 return -EINVAL;
8e09f215 483 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
1da177e4 484 geo->heads = 16;
8e09f215 485 geo->sectors = 128 >> block->s2b_shift;
1da177e4
LT
486 return 0;
487}
488
1da177e4
LT
489static dasd_erp_fn_t
490dasd_diag_erp_action(struct dasd_ccw_req * cqr)
491{
492 return dasd_default_erp_action;
493}
494
495static dasd_erp_fn_t
496dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
497{
498 return dasd_default_erp_postaction;
499}
500
fd49f41a
HH
501/* Create DASD request from block device request. Return pointer to new
502 * request on success, ERR_PTR otherwise. */
8e09f215
SW
503static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
504 struct dasd_block *block,
505 struct request *req)
1da177e4
LT
506{
507 struct dasd_ccw_req *cqr;
508 struct dasd_diag_req *dreq;
509 struct dasd_diag_bio *dbio;
5705f702 510 struct req_iterator iter;
7988613b 511 struct bio_vec bv;
1da177e4 512 char *dst;
10321aa1 513 unsigned int count;
1da177e4 514 sector_t recid, first_rec, last_rec;
fd49f41a 515 unsigned int blksize, off;
1da177e4 516 unsigned char rw_cmd;
1da177e4
LT
517
518 if (rq_data_dir(req) == READ)
519 rw_cmd = MDSK_READ_REQ;
520 else if (rq_data_dir(req) == WRITE)
521 rw_cmd = MDSK_WRITE_REQ;
522 else
523 return ERR_PTR(-EINVAL);
8e09f215 524 blksize = block->bp_block;
1da177e4 525 /* Calculate record id of first and last block. */
83096ebf
TH
526 first_rec = blk_rq_pos(req) >> block->s2b_shift;
527 last_rec =
528 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
1da177e4
LT
529 /* Check struct bio and count the number of blocks for the request. */
530 count = 0;
5705f702 531 rq_for_each_segment(bv, req, iter) {
7988613b 532 if (bv.bv_len & (blksize - 1))
6c92e699
JA
533 /* Fba can only do full blocks. */
534 return ERR_PTR(-EINVAL);
7988613b 535 count += bv.bv_len >> (block->s2b_shift + 9);
1da177e4
LT
536 }
537 /* Paranoia. */
538 if (count != last_rec - first_rec + 1)
539 return ERR_PTR(-EINVAL);
540 /* Build the request */
10321aa1
GS
541 cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, struct_size(dreq, bio, count),
542 memdev, blk_mq_rq_to_pdu(req));
1da177e4
LT
543 if (IS_ERR(cqr))
544 return cqr;
138c014d 545
1da177e4
LT
546 dreq = (struct dasd_diag_req *) cqr->data;
547 dreq->block_count = count;
548 dbio = dreq->bio;
549 recid = first_rec;
5705f702 550 rq_for_each_segment(bv, req, iter) {
bf5fb875 551 dst = bvec_virt(&bv);
7988613b 552 for (off = 0; off < bv.bv_len; off += blksize) {
6c92e699
JA
553 memset(dbio, 0, sizeof (struct dasd_diag_bio));
554 dbio->type = rw_cmd;
555 dbio->block_number = recid + 1;
556 dbio->buffer = dst;
557 dbio++;
558 dst += blksize;
559 recid++;
560 }
1da177e4 561 }
1f1ee9ad 562 cqr->retries = memdev->default_retries;
1aae0560 563 cqr->buildclk = get_tod_clock();
13de227b
HS
564 if (blk_noretry_request(req) ||
565 block->base->features & DASD_FEATURE_FAILFAST)
1c01b8a5 566 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
8e09f215
SW
567 cqr->startdev = memdev;
568 cqr->memdev = memdev;
569 cqr->block = block;
7c8faa86 570 cqr->expires = memdev->default_expires * HZ;
1da177e4
LT
571 cqr->status = DASD_CQR_FILLED;
572 return cqr;
573}
574
fd49f41a
HH
575/* Release DASD request. Return non-zero if request was successful, zero
576 * otherwise. */
1da177e4
LT
577static int
578dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
579{
580 int status;
581
582 status = cqr->status == DASD_CQR_DONE;
8e09f215 583 dasd_sfree_request(cqr, cqr->memdev);
1da177e4
LT
584 return status;
585}
586
8e09f215
SW
587static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
588{
a2ace466
HR
589 if (cqr->retries < 0)
590 cqr->status = DASD_CQR_FAILED;
591 else
592 cqr->status = DASD_CQR_FILLED;
8e09f215
SW
593};
594
fd49f41a 595/* Fill in IOCTL data for device. */
1da177e4
LT
596static int
597dasd_diag_fill_info(struct dasd_device * device,
598 struct dasd_information2_t * info)
599{
543691a4 600 struct dasd_diag_private *private = device->private;
1da177e4 601
fd49f41a 602 info->label_block = (unsigned int) private->pt_block;
1da177e4
LT
603 info->FBA_layout = 1;
604 info->format = DASD_FORMAT_LDL;
543691a4
SO
605 info->characteristics_size = sizeof(private->rdc_data);
606 memcpy(info->characteristics, &private->rdc_data,
607 sizeof(private->rdc_data));
1da177e4
LT
608 info->confdata_size = 0;
609 return 0;
610}
611
612static void
613dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
614 struct irb *stat)
615{
fc19f381 616 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
617 "dump sense not available for DIAG data");
618}
619
0127a47f 620static unsigned int dasd_diag_max_sectors(struct dasd_block *block)
a0610a8a 621{
0127a47f 622 return DIAG_MAX_BLOCKS << block->s2b_shift;
a0610a8a
JH
623}
624
c0c8a839
SH
625static int dasd_diag_pe_handler(struct dasd_device *device,
626 __u8 tbvpm, __u8 fcsecpm)
627{
628 return dasd_generic_verify_path(device, tbvpm);
629}
630
2b67fc46 631static struct dasd_discipline dasd_diag_discipline = {
1da177e4
LT
632 .owner = THIS_MODULE,
633 .name = "DIAG",
634 .ebcname = "DIAG",
0127a47f 635 .max_sectors = dasd_diag_max_sectors,
1da177e4 636 .check_device = dasd_diag_check_device,
c0c8a839 637 .pe_handler = dasd_diag_pe_handler,
1da177e4
LT
638 .fill_geometry = dasd_diag_fill_geometry,
639 .start_IO = dasd_start_diag,
fd49f41a 640 .term_IO = dasd_diag_term_IO,
8e09f215 641 .handle_terminated_request = dasd_diag_handle_terminated_request,
1da177e4
LT
642 .erp_action = dasd_diag_erp_action,
643 .erp_postaction = dasd_diag_erp_postaction,
644 .build_cp = dasd_diag_build_cp,
645 .free_cp = dasd_diag_free_cp,
646 .dump_sense = dasd_diag_dump_sense,
647 .fill_info = dasd_diag_fill_info,
648};
649
650static int __init
651dasd_diag_init(void)
652{
653 if (!MACHINE_IS_VM) {
fc19f381
SH
654 pr_info("Discipline %s cannot be used without z/VM\n",
655 dasd_diag_discipline.name);
fd49f41a 656 return -ENODEV;
1da177e4
LT
657 }
658 ASCEBC(dasd_diag_discipline.ebcname, 4);
659
82003c3e 660 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
1dad093b 661 register_external_irq(EXT_IRQ_CP_SERVICE, dasd_ext_handler);
1da177e4
LT
662 dasd_diag_discipline_pointer = &dasd_diag_discipline;
663 return 0;
664}
665
666static void __exit
667dasd_diag_cleanup(void)
668{
1dad093b 669 unregister_external_irq(EXT_IRQ_CP_SERVICE, dasd_ext_handler);
82003c3e 670 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL);
1da177e4
LT
671 dasd_diag_discipline_pointer = NULL;
672}
673
674module_init(dasd_diag_init);
675module_exit(dasd_diag_cleanup);