blob: 0b89796080bec9273ac1c70a072c25c88cac489f [file] [log] [blame]
Horst Hummel138c0142006-06-29 14:58:12 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011#define KMSG_COMPONENT "dasd"
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/stddef.h>
14#include <linux/kernel.h>
15#include <linux/slab.h>
Horst Hummelfd49f412005-09-03 15:58:00 -070016#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/bio.h>
18#include <linux/module.h>
19#include <linux/init.h>
Horst Hummelfd49f412005-09-03 15:58:00 -070020#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/dasd.h>
23#include <asm/debug.h>
24#include <asm/ebcdic.h>
25#include <asm/io.h>
26#include <asm/s390_ext.h>
27#include <asm/todclk.h>
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080028#include <asm/vtoc.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020029#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "dasd_int.h"
32#include "dasd_diag.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define PRINTK_HEADER "dasd(diag):"
35
36MODULE_LICENSE("GPL");
37
Horst Hummelfd49f412005-09-03 15:58:00 -070038/* The maximum number of blocks per request (max_blocks) is dependent on the
39 * amount of storage that is available in the static I/O buffer for each
40 * device. Currently each device gets 2 pages. We want to fit two requests
41 * into the available memory so that we can immediately start the next if one
42 * finishes. */
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 * HZ
48
Heiko Carstens2b67fc42007-02-05 21:16:47 +010049static struct dasd_discipline dasd_diag_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
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;
Horst Hummelfd49f412005-09-03 15:58:00 -070055 blocknum_t pt_block;
Cornelia Huck9a92fe42007-05-10 15:45:42 +020056 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59struct dasd_diag_req {
Horst Hummelfd49f412005-09-03 15:58:00 -070060 unsigned int block_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct dasd_diag_bio bio[0];
62};
63
Horst Hummelfd49f412005-09-03 15:58:00 -070064static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
65
66/* Perform DIAG250 call with block I/O parameter list iob (input and output)
67 * and function code cmd.
68 * In case of an exception return 3. Otherwise return result of bitwise OR of
69 * resulting condition code and DIAG return code. */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020070static inline int dia250(void *iob, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Peter Oberparleiter04439692007-03-26 20:42:38 +020072 register unsigned long reg2 asm ("2") = (unsigned long) iob;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -080073 typedef union {
74 struct dasd_diag_init_io init_io;
75 struct dasd_diag_rw_io rw_io;
Horst Hummelfd49f412005-09-03 15:58:00 -070076 } addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int rc;
78
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020079 rc = 3;
80 asm volatile(
Peter Oberparleiter04439692007-03-26 20:42:38 +020081 " diag 2,%2,0x250\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070082 "0: ipm %0\n"
83 " srl %0,28\n"
Peter Oberparleiter04439692007-03-26 20:42:38 +020084 " or %0,3\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070085 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020086 EX_TABLE(0b,1b)
87 : "+d" (rc), "=m" (*(addr_type *) iob)
Peter Oberparleiter04439692007-03-26 20:42:38 +020088 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
89 : "3", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return rc;
91}
92
Horst Hummelfd49f412005-09-03 15:58:00 -070093/* Initialize block I/O to DIAG device using the specified blocksize and
94 * block offset. On success, return zero and set end_block to contain the
95 * number of blocks on the device minus the specified offset. Return non-zero
96 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010097static inline int
Horst Hummelfd49f412005-09-03 15:58:00 -070098mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
99 blocknum_t offset, blocknum_t *end_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct dasd_diag_private *private;
102 struct dasd_diag_init_io *iib;
103 int rc;
104
105 private = (struct dasd_diag_private *) device->private;
106 iib = &private->iib;
107 memset(iib, 0, sizeof (struct dasd_diag_init_io));
108
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200109 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 iib->block_size = blocksize;
111 iib->offset = offset;
Horst Hummelfd49f412005-09-03 15:58:00 -0700112 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 rc = dia250(iib, INIT_BIO);
115
Horst Hummelfd49f412005-09-03 15:58:00 -0700116 if ((rc & 3) == 0 && end_block)
117 *end_block = iib->end_block;
118
119 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Horst Hummelfd49f412005-09-03 15:58:00 -0700122/* Remove block I/O environment for device. Return zero on success, non-zero
123 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100124static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125mdsk_term_io(struct dasd_device * device)
126{
127 struct dasd_diag_private *private;
128 struct dasd_diag_init_io *iib;
129 int rc;
130
131 private = (struct dasd_diag_private *) device->private;
132 iib = &private->iib;
133 memset(iib, 0, sizeof (struct dasd_diag_init_io));
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200134 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 rc = dia250(iib, TERM_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700136 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Horst Hummelfd49f412005-09-03 15:58:00 -0700139/* Error recovery for failed DIAG requests - try to reestablish the DIAG
140 * environment. */
141static void
142dasd_diag_erp(struct dasd_device *device)
143{
144 int rc;
145
146 mdsk_term_io(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100147 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
Horst Hummelfd49f412005-09-03 15:58:00 -0700148 if (rc)
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100149 dev_warn(&device->cdev->dev, "DIAG ERP failed with "
150 "rc=%d\n", rc);
Horst Hummelfd49f412005-09-03 15:58:00 -0700151}
152
153/* Start a given request at the device. Return zero on success, non-zero
154 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int
156dasd_start_diag(struct dasd_ccw_req * cqr)
157{
158 struct dasd_device *device;
159 struct dasd_diag_private *private;
160 struct dasd_diag_req *dreq;
161 int rc;
162
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100163 device = cqr->startdev;
Horst Hummelfd49f412005-09-03 15:58:00 -0700164 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100165 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
Horst Hummelfd49f412005-09-03 15:58:00 -0700166 "- no retry left)", cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100167 cqr->status = DASD_CQR_ERROR;
Horst Hummelfd49f412005-09-03 15:58:00 -0700168 return -EIO;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 private = (struct dasd_diag_private *) device->private;
171 dreq = (struct dasd_diag_req *) cqr->data;
172
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200173 private->iob.dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 private->iob.key = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700175 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 private->iob.block_count = dreq->block_count;
Horst Hummelfd49f412005-09-03 15:58:00 -0700177 private->iob.interrupt_params = (addr_t) cqr;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800178 private->iob.bio_list = dreq->bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700179 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 cqr->startclk = get_clock();
Horst Hummelfd49f412005-09-03 15:58:00 -0700182 cqr->starttime = jiffies;
183 cqr->retries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 rc = dia250(&private->iob, RW_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700186 switch (rc) {
187 case 0: /* Synchronous I/O finished successfully */
188 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100189 cqr->status = DASD_CQR_SUCCESS;
Horst Hummelfd49f412005-09-03 15:58:00 -0700190 /* Indicate to calling function that only a dasd_schedule_bh()
191 and no timer is needed */
192 rc = -EACCES;
193 break;
194 case 8: /* Asynchronous I/O was started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 cqr->status = DASD_CQR_IN_IO;
196 rc = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700197 break;
198 default: /* Error condition */
199 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100200 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
Horst Hummelfd49f412005-09-03 15:58:00 -0700201 dasd_diag_erp(device);
202 rc = -EIO;
203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200205 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return rc;
207}
208
Horst Hummelfd49f412005-09-03 15:58:00 -0700209/* Terminate given request at the device. */
210static int
211dasd_diag_term_IO(struct dasd_ccw_req * cqr)
212{
213 struct dasd_device *device;
214
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100215 device = cqr->startdev;
Horst Hummelfd49f412005-09-03 15:58:00 -0700216 mdsk_term_io(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217 mdsk_init_io(device, device->block->bp_block, 0, NULL);
218 cqr->status = DASD_CQR_CLEAR_PENDING;
Horst Hummelfd49f412005-09-03 15:58:00 -0700219 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100220 dasd_schedule_device_bh(device);
Horst Hummelfd49f412005-09-03 15:58:00 -0700221 return 0;
222}
223
224/* Handle external interruption. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void
Heiko Carstens5a489b982006-10-06 16:38:35 +0200226dasd_ext_handler(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct dasd_ccw_req *cqr, *next;
229 struct dasd_device *device;
230 unsigned long long expires;
231 unsigned long flags;
Horst Hummelfd49f412005-09-03 15:58:00 -0700232 u8 int_code, status;
233 addr_t ip;
234 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Horst Hummelfd49f412005-09-03 15:58:00 -0700236 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
237 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
238 switch (int_code) {
239 case DASD_DIAG_CODE_31BIT:
240 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
241 break;
242 case DASD_DIAG_CODE_64BIT:
243 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
244 break;
245 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return;
Horst Hummelfd49f412005-09-03 15:58:00 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!ip) { /* no intparm: unsolicited interrupt */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100249 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
250 "interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return;
252 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700253 cqr = (struct dasd_ccw_req *) ip;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100254 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100256 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 " magic number of dasd_ccw_req 0x%08X doesn't"
258 " match discipline 0x%08X",
259 cqr->magic, *(int *) (&device->discipline->name));
260 return;
261 }
262
263 /* get irq lock to modify request queue */
264 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
265
Horst Hummelfd49f412005-09-03 15:58:00 -0700266 /* Check for a pending clear operation */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100267 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
268 cqr->status = DASD_CQR_CLEARED;
269 dasd_device_clear_timer(device);
270 dasd_schedule_device_bh(device);
Horst Hummelfd49f412005-09-03 15:58:00 -0700271 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
272 return;
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 cqr->stopclk = get_clock();
276
277 expires = 0;
278 if (status == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Start first request on queue if possible -> fast_io. */
281 if (!list_empty(&device->ccw_queue)) {
282 next = list_entry(device->ccw_queue.next,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100283 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (next->status == DASD_CQR_QUEUED) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700285 rc = dasd_start_diag(next);
286 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700290 } else {
291 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100292 DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
Horst Hummelfd49f412005-09-03 15:58:00 -0700293 "request %p was %d (%d retries left)", cqr, status,
294 cqr->retries);
295 dasd_diag_erp(device);
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100299 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100301 dasd_device_clear_timer(device);
302 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
305}
306
Horst Hummelfd49f412005-09-03 15:58:00 -0700307/* Check whether device can be controlled by DIAG discipline. Return zero on
308 * success, non-zero otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int
310dasd_diag_check_device(struct dasd_device *device)
311{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100312 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct dasd_diag_private *private;
314 struct dasd_diag_characteristics *rdc_data;
315 struct dasd_diag_bio bio;
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800316 struct vtoc_cms_label *label;
Horst Hummelfd49f412005-09-03 15:58:00 -0700317 blocknum_t end_block;
318 unsigned int sb, bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int rc;
320
321 private = (struct dasd_diag_private *) device->private;
322 if (private == NULL) {
Horst Hummel138c0142006-06-29 14:58:12 +0200323 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (private == NULL) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100325 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
326 "Allocating memory for private DASD data "
327 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return -ENOMEM;
329 }
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200330 ccw_device_get_id(device->cdev, &private->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 device->private = (void *) private;
332 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100333 block = dasd_alloc_block();
334 if (IS_ERR(block)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100335 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336 "could not allocate dasd block structure");
Cornelia Huck73371942008-07-17 17:16:43 +0200337 device->private = NULL;
338 kfree(private);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100339 return PTR_ERR(block);
340 }
341 device->block = block;
342 block->base = device;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Read Device Characteristics */
345 rdc_data = (void *) &(private->rdc_data);
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200346 rdc_data->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
348
349 rc = diag210((struct diag210 *) rdc_data);
Horst Hummelfd49f412005-09-03 15:58:00 -0700350 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100351 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
Horst Hummelfd49f412005-09-03 15:58:00 -0700352 "information (rc=%d)", rc);
Stefan Haberland8586cb62008-07-17 17:16:49 +0200353 rc = -EOPNOTSUPP;
Cornelia Huck73371942008-07-17 17:16:43 +0200354 goto out;
Horst Hummelfd49f412005-09-03 15:58:00 -0700355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Figure out position of label block */
358 switch (private->rdc_data.vdev_class) {
359 case DEV_CLASS_FBA:
360 private->pt_block = 1;
361 break;
362 case DEV_CLASS_ECKD:
363 private->pt_block = 2;
364 break;
365 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100366 dev_warn(&device->cdev->dev, "Device type %d is not supported "
367 "in DIAG mode\n", private->rdc_data.vdev_class);
Stefan Haberland8586cb62008-07-17 17:16:49 +0200368 rc = -EOPNOTSUPP;
Cornelia Huck73371942008-07-17 17:16:43 +0200369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
372 DBF_DEV_EVENT(DBF_INFO, device,
373 "%04X: %04X on real %04X/%02X",
374 rdc_data->dev_nr,
375 rdc_data->vdev_type,
376 rdc_data->rdev_type, rdc_data->rdev_model);
377
378 /* terminate all outstanding operations */
379 mdsk_term_io(device);
380
381 /* figure out blocksize of device */
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800382 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (label == NULL) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100384 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 "No memory to allocate initialization request");
Cornelia Huck73371942008-07-17 17:16:43 +0200386 rc = -ENOMEM;
387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700389 rc = 0;
390 end_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* try all sizes - needed for ECKD devices */
392 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700393 mdsk_init_io(device, bsize, 0, &end_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 memset(&bio, 0, sizeof (struct dasd_diag_bio));
395 bio.type = MDSK_READ_REQ;
396 bio.block_number = private->pt_block + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800397 bio.buffer = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
399 private->iob.dev_nr = rdc_data->dev_nr;
400 private->iob.key = 0;
401 private->iob.flags = 0; /* do synchronous io */
402 private->iob.block_count = 1;
403 private->iob.interrupt_params = 0;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800404 private->iob.bio_list = &bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700405 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
406 rc = dia250(&private->iob, RW_BIO);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800407 if (rc == 3) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100408 dev_warn(&device->cdev->dev,
409 "A 64-bit DIAG call failed\n");
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800410 rc = -EOPNOTSUPP;
Cornelia Huck73371942008-07-17 17:16:43 +0200411 goto out_label;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 mdsk_term_io(device);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800414 if (rc == 0)
415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800417 if (bsize > PAGE_SIZE) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100418 dev_warn(&device->cdev->dev, "Accessing the DASD failed because"
419 " of an incorrect format (rc=%d)\n", rc);
Horst Hummelfd49f412005-09-03 15:58:00 -0700420 rc = -EIO;
Cornelia Huck73371942008-07-17 17:16:43 +0200421 goto out_label;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800422 }
423 /* check for label block */
424 if (memcmp(label->label_id, DASD_DIAG_CMS1,
425 sizeof(DASD_DIAG_CMS1)) == 0) {
426 /* get formatted blocksize from label block */
427 bsize = (unsigned int) label->block_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100428 block->blocks = (unsigned long) label->block_count;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800429 } else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100430 block->blocks = end_block;
431 block->bp_block = bsize;
432 block->s2b_shift = 0; /* bits to shift 512 to get a block */
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800433 for (sb = 512; sb < bsize; sb = sb << 1)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100434 block->s2b_shift++;
435 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800436 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100437 dev_warn(&device->cdev->dev, "DIAG initialization "
438 "failed with rc=%d\n", rc);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800439 rc = -EIO;
Horst Hummelfd49f412005-09-03 15:58:00 -0700440 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100441 dev_info(&device->cdev->dev,
442 "New DASD with %ld byte/block, total size %ld KB\n",
443 (unsigned long) block->bp_block,
444 (unsigned long) (block->blocks <<
445 block->s2b_shift) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Cornelia Huck73371942008-07-17 17:16:43 +0200447out_label:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 free_page((long) label);
Cornelia Huck73371942008-07-17 17:16:43 +0200449out:
450 if (rc) {
451 device->block = NULL;
452 dasd_free_block(block);
453 device->private = NULL;
454 kfree(private);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return rc;
457}
458
Horst Hummelfd49f412005-09-03 15:58:00 -0700459/* Fill in virtual disk geometry for device. Return zero on success, non-zero
460 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static int
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100462dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100464 if (dasd_check_blocksize(block->bp_block) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100466 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 geo->heads = 16;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100468 geo->sectors = 128 >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 0;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static dasd_erp_fn_t
473dasd_diag_erp_action(struct dasd_ccw_req * cqr)
474{
475 return dasd_default_erp_action;
476}
477
478static dasd_erp_fn_t
479dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
480{
481 return dasd_default_erp_postaction;
482}
483
Horst Hummelfd49f412005-09-03 15:58:00 -0700484/* Create DASD request from block device request. Return pointer to new
485 * request on success, ERR_PTR otherwise. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100486static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
487 struct dasd_block *block,
488 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct dasd_ccw_req *cqr;
491 struct dasd_diag_req *dreq;
492 struct dasd_diag_bio *dbio;
NeilBrown5705f702007-09-25 12:35:59 +0200493 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct bio_vec *bv;
495 char *dst;
Horst Hummelfd49f412005-09-03 15:58:00 -0700496 unsigned int count, datasize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 sector_t recid, first_rec, last_rec;
Horst Hummelfd49f412005-09-03 15:58:00 -0700498 unsigned int blksize, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 unsigned char rw_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (rq_data_dir(req) == READ)
502 rw_cmd = MDSK_READ_REQ;
503 else if (rq_data_dir(req) == WRITE)
504 rw_cmd = MDSK_WRITE_REQ;
505 else
506 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100507 blksize = block->bp_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Calculate record id of first and last block. */
Tejun Heo83096eb2009-05-07 22:24:39 +0900509 first_rec = blk_rq_pos(req) >> block->s2b_shift;
510 last_rec =
511 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Check struct bio and count the number of blocks for the request. */
513 count = 0;
NeilBrown5705f702007-09-25 12:35:59 +0200514 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200515 if (bv->bv_len & (blksize - 1))
516 /* Fba can only do full blocks. */
517 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100518 count += bv->bv_len >> (block->s2b_shift + 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 /* Paranoia. */
521 if (count != last_rec - first_rec + 1)
522 return ERR_PTR(-EINVAL);
523 /* Build the request */
524 datasize = sizeof(struct dasd_diag_req) +
525 count*sizeof(struct dasd_diag_bio);
Stefan Haberland68b781f2009-09-11 10:28:29 +0200526 cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, datasize, memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (IS_ERR(cqr))
528 return cqr;
Horst Hummel138c0142006-06-29 14:58:12 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 dreq = (struct dasd_diag_req *) cqr->data;
531 dreq->block_count = count;
532 dbio = dreq->bio;
533 recid = first_rec;
NeilBrown5705f702007-09-25 12:35:59 +0200534 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200535 dst = page_address(bv->bv_page) + bv->bv_offset;
536 for (off = 0; off < bv->bv_len; off += blksize) {
537 memset(dbio, 0, sizeof (struct dasd_diag_bio));
538 dbio->type = rw_cmd;
539 dbio->block_number = recid + 1;
540 dbio->buffer = dst;
541 dbio++;
542 dst += blksize;
543 recid++;
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700546 cqr->retries = DIAG_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 cqr->buildclk = get_clock();
Holger Smolinski13de2272009-01-09 12:14:51 +0100548 if (blk_noretry_request(req) ||
549 block->base->features & DASD_FEATURE_FAILFAST)
Horst Hummel1c01b8a2006-01-06 00:19:15 -0800550 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100551 cqr->startdev = memdev;
552 cqr->memdev = memdev;
553 cqr->block = block;
Horst Hummelfd49f412005-09-03 15:58:00 -0700554 cqr->expires = DIAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 cqr->status = DASD_CQR_FILLED;
556 return cqr;
557}
558
Horst Hummelfd49f412005-09-03 15:58:00 -0700559/* Release DASD request. Return non-zero if request was successful, zero
560 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static int
562dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
563{
564 int status;
565
566 status = cqr->status == DASD_CQR_DONE;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100567 dasd_sfree_request(cqr, cqr->memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return status;
569}
570
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100571static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
572{
573 cqr->status = DASD_CQR_FILLED;
574};
575
Horst Hummelfd49f412005-09-03 15:58:00 -0700576/* Fill in IOCTL data for device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static int
578dasd_diag_fill_info(struct dasd_device * device,
579 struct dasd_information2_t * info)
580{
581 struct dasd_diag_private *private;
582
583 private = (struct dasd_diag_private *) device->private;
Horst Hummelfd49f412005-09-03 15:58:00 -0700584 info->label_block = (unsigned int) private->pt_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 info->FBA_layout = 1;
586 info->format = DASD_FORMAT_LDL;
587 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
588 memcpy(info->characteristics,
589 &((struct dasd_diag_private *) device->private)->rdc_data,
590 sizeof (struct dasd_diag_characteristics));
591 info->confdata_size = 0;
592 return 0;
593}
594
595static void
596dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
597 struct irb *stat)
598{
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100599 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 "dump sense not available for DIAG data");
601}
602
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100603static struct dasd_discipline dasd_diag_discipline = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .owner = THIS_MODULE,
605 .name = "DIAG",
606 .ebcname = "DIAG",
Horst Hummelfd49f412005-09-03 15:58:00 -0700607 .max_blocks = DIAG_MAX_BLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 .check_device = dasd_diag_check_device,
609 .fill_geometry = dasd_diag_fill_geometry,
610 .start_IO = dasd_start_diag,
Horst Hummelfd49f412005-09-03 15:58:00 -0700611 .term_IO = dasd_diag_term_IO,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100612 .handle_terminated_request = dasd_diag_handle_terminated_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 .erp_action = dasd_diag_erp_action,
614 .erp_postaction = dasd_diag_erp_postaction,
615 .build_cp = dasd_diag_build_cp,
616 .free_cp = dasd_diag_free_cp,
617 .dump_sense = dasd_diag_dump_sense,
618 .fill_info = dasd_diag_fill_info,
619};
620
621static int __init
622dasd_diag_init(void)
623{
624 if (!MACHINE_IS_VM) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100625 pr_info("Discipline %s cannot be used without z/VM\n",
626 dasd_diag_discipline.name);
Horst Hummelfd49f412005-09-03 15:58:00 -0700627 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 ASCEBC(dasd_diag_discipline.ebcname, 4);
630
631 ctl_set_bit(0, 9);
632 register_external_interrupt(0x2603, dasd_ext_handler);
633 dasd_diag_discipline_pointer = &dasd_diag_discipline;
634 return 0;
635}
636
637static void __exit
638dasd_diag_cleanup(void)
639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unregister_external_interrupt(0x2603, dasd_ext_handler);
641 ctl_clear_bit(0, 9);
642 dasd_diag_discipline_pointer = NULL;
643}
644
645module_init(dasd_diag_init);
646module_exit(dasd_diag_cleanup);