blob: eccac1c3b71bb86ef535300a21c4311ebce343a9 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/stddef.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
Horst Hummelfd49f412005-09-03 15:58:00 -070014#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/bio.h>
16#include <linux/module.h>
17#include <linux/init.h>
Horst Hummelfd49f412005-09-03 15:58:00 -070018#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/dasd.h>
21#include <asm/debug.h>
22#include <asm/ebcdic.h>
23#include <asm/io.h>
24#include <asm/s390_ext.h>
25#include <asm/todclk.h>
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080026#include <asm/vtoc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "dasd_int.h"
29#include "dasd_diag.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define PRINTK_HEADER "dasd(diag):"
32
33MODULE_LICENSE("GPL");
34
Horst Hummelfd49f412005-09-03 15:58:00 -070035/* The maximum number of blocks per request (max_blocks) is dependent on the
36 * amount of storage that is available in the static I/O buffer for each
37 * device. Currently each device gets 2 pages. We want to fit two requests
38 * into the available memory so that we can immediately start the next if one
39 * finishes. */
40#define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
41 sizeof(struct dasd_diag_req)) / \
42 sizeof(struct dasd_diag_bio)) / 2)
43#define DIAG_MAX_RETRIES 32
44#define DIAG_TIMEOUT 50 * HZ
45
Heiko Carstens2b67fc42007-02-05 21:16:47 +010046static struct dasd_discipline dasd_diag_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct dasd_diag_private {
49 struct dasd_diag_characteristics rdc_data;
50 struct dasd_diag_rw_io iob;
51 struct dasd_diag_init_io iib;
Horst Hummelfd49f412005-09-03 15:58:00 -070052 blocknum_t pt_block;
Cornelia Huck9a92fe42007-05-10 15:45:42 +020053 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56struct dasd_diag_req {
Horst Hummelfd49f412005-09-03 15:58:00 -070057 unsigned int block_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct dasd_diag_bio bio[0];
59};
60
Horst Hummelfd49f412005-09-03 15:58:00 -070061static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
62
63/* Perform DIAG250 call with block I/O parameter list iob (input and output)
64 * and function code cmd.
65 * In case of an exception return 3. Otherwise return result of bitwise OR of
66 * resulting condition code and DIAG return code. */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020067static inline int dia250(void *iob, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Peter Oberparleiter04439692007-03-26 20:42:38 +020069 register unsigned long reg2 asm ("2") = (unsigned long) iob;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -080070 typedef union {
71 struct dasd_diag_init_io init_io;
72 struct dasd_diag_rw_io rw_io;
Horst Hummelfd49f412005-09-03 15:58:00 -070073 } addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int rc;
75
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020076 rc = 3;
77 asm volatile(
Peter Oberparleiter04439692007-03-26 20:42:38 +020078 " diag 2,%2,0x250\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070079 "0: ipm %0\n"
80 " srl %0,28\n"
Peter Oberparleiter04439692007-03-26 20:42:38 +020081 " or %0,3\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070082 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020083 EX_TABLE(0b,1b)
84 : "+d" (rc), "=m" (*(addr_type *) iob)
Peter Oberparleiter04439692007-03-26 20:42:38 +020085 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
86 : "3", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return rc;
88}
89
Horst Hummelfd49f412005-09-03 15:58:00 -070090/* Initialize block I/O to DIAG device using the specified blocksize and
91 * block offset. On success, return zero and set end_block to contain the
92 * number of blocks on the device minus the specified offset. Return non-zero
93 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010094static inline int
Horst Hummelfd49f412005-09-03 15:58:00 -070095mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
96 blocknum_t offset, blocknum_t *end_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct dasd_diag_private *private;
99 struct dasd_diag_init_io *iib;
100 int rc;
101
102 private = (struct dasd_diag_private *) device->private;
103 iib = &private->iib;
104 memset(iib, 0, sizeof (struct dasd_diag_init_io));
105
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200106 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 iib->block_size = blocksize;
108 iib->offset = offset;
Horst Hummelfd49f412005-09-03 15:58:00 -0700109 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 rc = dia250(iib, INIT_BIO);
112
Horst Hummelfd49f412005-09-03 15:58:00 -0700113 if ((rc & 3) == 0 && end_block)
114 *end_block = iib->end_block;
115
116 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Horst Hummelfd49f412005-09-03 15:58:00 -0700119/* Remove block I/O environment for device. Return zero on success, non-zero
120 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100121static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122mdsk_term_io(struct dasd_device * device)
123{
124 struct dasd_diag_private *private;
125 struct dasd_diag_init_io *iib;
126 int rc;
127
128 private = (struct dasd_diag_private *) device->private;
129 iib = &private->iib;
130 memset(iib, 0, sizeof (struct dasd_diag_init_io));
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200131 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 rc = dia250(iib, TERM_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700133 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Horst Hummelfd49f412005-09-03 15:58:00 -0700136/* 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);
144 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
145 if (rc)
146 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
147 "rc=%d", rc);
148}
149
150/* Start a given request at the device. Return zero on success, non-zero
151 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int
153dasd_start_diag(struct dasd_ccw_req * cqr)
154{
155 struct dasd_device *device;
156 struct dasd_diag_private *private;
157 struct dasd_diag_req *dreq;
158 int rc;
159
160 device = cqr->device;
Horst Hummelfd49f412005-09-03 15:58:00 -0700161 if (cqr->retries < 0) {
162 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
163 "- no retry left)", cqr);
164 cqr->status = DASD_CQR_FAILED;
165 return -EIO;
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 private = (struct dasd_diag_private *) device->private;
168 dreq = (struct dasd_diag_req *) cqr->data;
169
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200170 private->iob.dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 private->iob.key = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700172 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 private->iob.block_count = dreq->block_count;
Horst Hummelfd49f412005-09-03 15:58:00 -0700174 private->iob.interrupt_params = (addr_t) cqr;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800175 private->iob.bio_list = dreq->bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700176 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 cqr->startclk = get_clock();
Horst Hummelfd49f412005-09-03 15:58:00 -0700179 cqr->starttime = jiffies;
180 cqr->retries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 rc = dia250(&private->iob, RW_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700183 switch (rc) {
184 case 0: /* Synchronous I/O finished successfully */
185 cqr->stopclk = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 cqr->status = DASD_CQR_DONE;
Horst Hummelfd49f412005-09-03 15:58:00 -0700187 /* Indicate to calling function that only a dasd_schedule_bh()
188 and no timer is needed */
189 rc = -EACCES;
190 break;
191 case 8: /* Asynchronous I/O was started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 cqr->status = DASD_CQR_IN_IO;
193 rc = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700194 break;
195 default: /* Error condition */
196 cqr->status = DASD_CQR_QUEUED;
197 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
198 dasd_diag_erp(device);
199 rc = -EIO;
200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 return rc;
203}
204
Horst Hummelfd49f412005-09-03 15:58:00 -0700205/* Terminate given request at the device. */
206static int
207dasd_diag_term_IO(struct dasd_ccw_req * cqr)
208{
209 struct dasd_device *device;
210
211 device = cqr->device;
212 mdsk_term_io(device);
213 mdsk_init_io(device, device->bp_block, 0, NULL);
214 cqr->status = DASD_CQR_CLEAR;
215 cqr->stopclk = get_clock();
216 dasd_schedule_bh(device);
217 return 0;
218}
219
220/* Handle external interruption. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static void
Heiko Carstens5a489b92006-10-06 16:38:35 +0200222dasd_ext_handler(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct dasd_ccw_req *cqr, *next;
225 struct dasd_device *device;
226 unsigned long long expires;
227 unsigned long flags;
Horst Hummelfd49f412005-09-03 15:58:00 -0700228 u8 int_code, status;
229 addr_t ip;
230 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Horst Hummelfd49f412005-09-03 15:58:00 -0700232 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
233 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
234 switch (int_code) {
235 case DASD_DIAG_CODE_31BIT:
236 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
237 break;
238 case DASD_DIAG_CODE_64BIT:
239 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
240 break;
241 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return;
Horst Hummelfd49f412005-09-03 15:58:00 -0700243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!ip) { /* no intparm: unsolicited interrupt */
245 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
246 return;
247 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700248 cqr = (struct dasd_ccw_req *) ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 device = (struct dasd_device *) cqr->device;
250 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
251 DEV_MESSAGE(KERN_WARNING, device,
252 " magic number of dasd_ccw_req 0x%08X doesn't"
253 " match discipline 0x%08X",
254 cqr->magic, *(int *) (&device->discipline->name));
255 return;
256 }
257
258 /* get irq lock to modify request queue */
259 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
260
Horst Hummelfd49f412005-09-03 15:58:00 -0700261 /* Check for a pending clear operation */
262 if (cqr->status == DASD_CQR_CLEAR) {
263 cqr->status = DASD_CQR_QUEUED;
264 dasd_clear_timer(device);
265 dasd_schedule_bh(device);
266 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
267 return;
268 }
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cqr->stopclk = get_clock();
271
272 expires = 0;
273 if (status == 0) {
274 cqr->status = DASD_CQR_DONE;
275 /* Start first request on queue if possible -> fast_io. */
276 if (!list_empty(&device->ccw_queue)) {
277 next = list_entry(device->ccw_queue.next,
278 struct dasd_ccw_req, list);
279 if (next->status == DASD_CQR_QUEUED) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700280 rc = dasd_start_diag(next);
281 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 expires = next->expires;
Horst Hummelfd49f412005-09-03 15:58:00 -0700283 else if (rc != -EACCES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 DEV_MESSAGE(KERN_WARNING, device, "%s",
285 "Interrupt fastpath "
286 "failed!");
287 }
288 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700289 } else {
290 cqr->status = DASD_CQR_QUEUED;
291 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
292 "request %p was %d (%d retries left)", cqr, status,
293 cqr->retries);
294 dasd_diag_erp(device);
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (expires != 0)
298 dasd_set_timer(device, expires);
299 else
300 dasd_clear_timer(device);
301 dasd_schedule_bh(device);
302
303 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
304}
305
Horst Hummelfd49f412005-09-03 15:58:00 -0700306/* Check whether device can be controlled by DIAG discipline. Return zero on
307 * success, non-zero otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static int
309dasd_diag_check_device(struct dasd_device *device)
310{
311 struct dasd_diag_private *private;
312 struct dasd_diag_characteristics *rdc_data;
313 struct dasd_diag_bio bio;
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800314 struct vtoc_cms_label *label;
Horst Hummelfd49f412005-09-03 15:58:00 -0700315 blocknum_t end_block;
316 unsigned int sb, bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int rc;
318
319 private = (struct dasd_diag_private *) device->private;
320 if (private == NULL) {
Horst Hummel138c0142006-06-29 14:58:12 +0200321 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (private == NULL) {
323 DEV_MESSAGE(KERN_WARNING, device, "%s",
324 "memory allocation failed for private data");
325 return -ENOMEM;
326 }
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200327 ccw_device_get_id(device->cdev, &private->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 device->private = (void *) private;
329 }
330 /* Read Device Characteristics */
331 rdc_data = (void *) &(private->rdc_data);
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200332 rdc_data->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
334
335 rc = diag210((struct diag210 *) rdc_data);
Horst Hummelfd49f412005-09-03 15:58:00 -0700336 if (rc) {
337 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
338 "information (rc=%d)", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -ENOTSUPP;
Horst Hummelfd49f412005-09-03 15:58:00 -0700340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Figure out position of label block */
343 switch (private->rdc_data.vdev_class) {
344 case DEV_CLASS_FBA:
345 private->pt_block = 1;
346 break;
347 case DEV_CLASS_ECKD:
348 private->pt_block = 2;
349 break;
350 default:
Horst Hummelfd49f412005-09-03 15:58:00 -0700351 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
352 "(class=%d)", private->rdc_data.vdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -ENOTSUPP;
354 }
355
356 DBF_DEV_EVENT(DBF_INFO, device,
357 "%04X: %04X on real %04X/%02X",
358 rdc_data->dev_nr,
359 rdc_data->vdev_type,
360 rdc_data->rdev_type, rdc_data->rdev_model);
361
362 /* terminate all outstanding operations */
363 mdsk_term_io(device);
364
365 /* figure out blocksize of device */
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800366 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (label == NULL) {
368 DEV_MESSAGE(KERN_WARNING, device, "%s",
369 "No memory to allocate initialization request");
370 return -ENOMEM;
371 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700372 rc = 0;
373 end_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* try all sizes - needed for ECKD devices */
375 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700376 mdsk_init_io(device, bsize, 0, &end_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 memset(&bio, 0, sizeof (struct dasd_diag_bio));
378 bio.type = MDSK_READ_REQ;
379 bio.block_number = private->pt_block + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800380 bio.buffer = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
382 private->iob.dev_nr = rdc_data->dev_nr;
383 private->iob.key = 0;
384 private->iob.flags = 0; /* do synchronous io */
385 private->iob.block_count = 1;
386 private->iob.interrupt_params = 0;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800387 private->iob.bio_list = &bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700388 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
389 rc = dia250(&private->iob, RW_BIO);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800390 if (rc == 3) {
391 DEV_MESSAGE(KERN_WARNING, device, "%s",
392 "DIAG call failed");
393 rc = -EOPNOTSUPP;
394 goto out;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 mdsk_term_io(device);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800397 if (rc == 0)
398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800400 if (bsize > PAGE_SIZE) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700401 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
402 "(rc=%d)", rc);
403 rc = -EIO;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800404 goto out;
405 }
406 /* check for label block */
407 if (memcmp(label->label_id, DASD_DIAG_CMS1,
408 sizeof(DASD_DIAG_CMS1)) == 0) {
409 /* get formatted blocksize from label block */
410 bsize = (unsigned int) label->block_size;
411 device->blocks = (unsigned long) label->block_count;
412 } else
413 device->blocks = end_block;
414 device->bp_block = bsize;
415 device->s2b_shift = 0; /* bits to shift 512 to get a block */
416 for (sb = 512; sb < bsize; sb = sb << 1)
417 device->s2b_shift++;
418 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
419 if (rc) {
420 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
421 "failed (rc=%d)", rc);
422 rc = -EIO;
Horst Hummelfd49f412005-09-03 15:58:00 -0700423 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 DEV_MESSAGE(KERN_INFO, device,
Horst Hummelfd49f412005-09-03 15:58:00 -0700425 "(%ld B/blk): %ldkB",
426 (unsigned long) device->bp_block,
427 (unsigned long) (device->blocks <<
428 device->s2b_shift) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800430out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 free_page((long) label);
432 return rc;
433}
434
Horst Hummelfd49f412005-09-03 15:58:00 -0700435/* Fill in virtual disk geometry for device. Return zero on success, non-zero
436 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static int
438dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
439{
440 if (dasd_check_blocksize(device->bp_block) != 0)
441 return -EINVAL;
442 geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
443 geo->heads = 16;
444 geo->sectors = 128 >> device->s2b_shift;
445 return 0;
446}
447
448static dasd_era_t
449dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
450{
451 return dasd_era_fatal;
452}
453
454static dasd_erp_fn_t
455dasd_diag_erp_action(struct dasd_ccw_req * cqr)
456{
457 return dasd_default_erp_action;
458}
459
460static dasd_erp_fn_t
461dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
462{
463 return dasd_default_erp_postaction;
464}
465
Horst Hummelfd49f412005-09-03 15:58:00 -0700466/* Create DASD request from block device request. Return pointer to new
467 * request on success, ERR_PTR otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static struct dasd_ccw_req *
469dasd_diag_build_cp(struct dasd_device * device, struct request *req)
470{
471 struct dasd_ccw_req *cqr;
472 struct dasd_diag_req *dreq;
473 struct dasd_diag_bio *dbio;
474 struct bio *bio;
475 struct bio_vec *bv;
476 char *dst;
Horst Hummelfd49f412005-09-03 15:58:00 -0700477 unsigned int count, datasize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sector_t recid, first_rec, last_rec;
Horst Hummelfd49f412005-09-03 15:58:00 -0700479 unsigned int blksize, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 unsigned char rw_cmd;
481 int i;
482
483 if (rq_data_dir(req) == READ)
484 rw_cmd = MDSK_READ_REQ;
485 else if (rq_data_dir(req) == WRITE)
486 rw_cmd = MDSK_WRITE_REQ;
487 else
488 return ERR_PTR(-EINVAL);
489 blksize = device->bp_block;
490 /* Calculate record id of first and last block. */
491 first_rec = req->sector >> device->s2b_shift;
492 last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
493 /* Check struct bio and count the number of blocks for the request. */
494 count = 0;
495 rq_for_each_bio(bio, req) {
496 bio_for_each_segment(bv, bio, i) {
497 if (bv->bv_len & (blksize - 1))
498 /* Fba can only do full blocks. */
499 return ERR_PTR(-EINVAL);
500 count += bv->bv_len >> (device->s2b_shift + 9);
501 }
502 }
503 /* Paranoia. */
504 if (count != last_rec - first_rec + 1)
505 return ERR_PTR(-EINVAL);
506 /* Build the request */
507 datasize = sizeof(struct dasd_diag_req) +
508 count*sizeof(struct dasd_diag_bio);
509 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
510 datasize, device);
511 if (IS_ERR(cqr))
512 return cqr;
Horst Hummel138c0142006-06-29 14:58:12 +0200513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 dreq = (struct dasd_diag_req *) cqr->data;
515 dreq->block_count = count;
516 dbio = dreq->bio;
517 recid = first_rec;
518 rq_for_each_bio(bio, req) {
519 bio_for_each_segment(bv, bio, i) {
520 dst = page_address(bv->bv_page) + bv->bv_offset;
521 for (off = 0; off < bv->bv_len; off += blksize) {
522 memset(dbio, 0, sizeof (struct dasd_diag_bio));
523 dbio->type = rw_cmd;
524 dbio->block_number = recid + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800525 dbio->buffer = dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 dbio++;
527 dst += blksize;
528 recid++;
529 }
530 }
531 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700532 cqr->retries = DIAG_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 cqr->buildclk = get_clock();
Jens Axboe4aff5e22006-08-10 08:44:47 +0200534 if (req->cmd_flags & REQ_FAILFAST)
Horst Hummel1c01b8a2006-01-06 00:19:15 -0800535 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cqr->device = device;
Horst Hummelfd49f412005-09-03 15:58:00 -0700537 cqr->expires = DIAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 cqr->status = DASD_CQR_FILLED;
539 return cqr;
540}
541
Horst Hummelfd49f412005-09-03 15:58:00 -0700542/* Release DASD request. Return non-zero if request was successful, zero
543 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544static int
545dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
546{
547 int status;
548
549 status = cqr->status == DASD_CQR_DONE;
550 dasd_sfree_request(cqr, cqr->device);
551 return status;
552}
553
Horst Hummelfd49f412005-09-03 15:58:00 -0700554/* Fill in IOCTL data for device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int
556dasd_diag_fill_info(struct dasd_device * device,
557 struct dasd_information2_t * info)
558{
559 struct dasd_diag_private *private;
560
561 private = (struct dasd_diag_private *) device->private;
Horst Hummelfd49f412005-09-03 15:58:00 -0700562 info->label_block = (unsigned int) private->pt_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 info->FBA_layout = 1;
564 info->format = DASD_FORMAT_LDL;
565 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
566 memcpy(info->characteristics,
567 &((struct dasd_diag_private *) device->private)->rdc_data,
568 sizeof (struct dasd_diag_characteristics));
569 info->confdata_size = 0;
570 return 0;
571}
572
573static void
574dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
575 struct irb *stat)
576{
577 DEV_MESSAGE(KERN_ERR, device, "%s",
578 "dump sense not available for DIAG data");
579}
580
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100581static struct dasd_discipline dasd_diag_discipline = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .owner = THIS_MODULE,
583 .name = "DIAG",
584 .ebcname = "DIAG",
Horst Hummelfd49f412005-09-03 15:58:00 -0700585 .max_blocks = DIAG_MAX_BLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 .check_device = dasd_diag_check_device,
587 .fill_geometry = dasd_diag_fill_geometry,
588 .start_IO = dasd_start_diag,
Horst Hummelfd49f412005-09-03 15:58:00 -0700589 .term_IO = dasd_diag_term_IO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 .examine_error = dasd_diag_examine_error,
591 .erp_action = dasd_diag_erp_action,
592 .erp_postaction = dasd_diag_erp_postaction,
593 .build_cp = dasd_diag_build_cp,
594 .free_cp = dasd_diag_free_cp,
595 .dump_sense = dasd_diag_dump_sense,
596 .fill_info = dasd_diag_fill_info,
597};
598
599static int __init
600dasd_diag_init(void)
601{
602 if (!MACHINE_IS_VM) {
603 MESSAGE_LOG(KERN_INFO,
604 "Machine is not VM: %s "
605 "discipline not initializing",
606 dasd_diag_discipline.name);
Horst Hummelfd49f412005-09-03 15:58:00 -0700607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 ASCEBC(dasd_diag_discipline.ebcname, 4);
610
611 ctl_set_bit(0, 9);
612 register_external_interrupt(0x2603, dasd_ext_handler);
613 dasd_diag_discipline_pointer = &dasd_diag_discipline;
614 return 0;
615}
616
617static void __exit
618dasd_diag_cleanup(void)
619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 unregister_external_interrupt(0x2603, dasd_ext_handler);
621 ctl_clear_bit(0, 9);
622 dasd_diag_discipline_pointer = NULL;
623}
624
625module_init(dasd_diag_init);
626module_exit(dasd_diag_cleanup);