blob: ab782bb46ac1a13978d21def078eca1b60cec77c [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55struct dasd_diag_req {
Horst Hummelfd49f412005-09-03 15:58:00 -070056 unsigned int block_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct dasd_diag_bio bio[0];
58};
59
Horst Hummelfd49f412005-09-03 15:58:00 -070060static 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. */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020066static inline int dia250(void *iob, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020068 register unsigned long reg0 asm ("0") = (unsigned long) iob;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -080069 typedef union {
70 struct dasd_diag_init_io init_io;
71 struct dasd_diag_rw_io rw_io;
Horst Hummelfd49f412005-09-03 15:58:00 -070072 } addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int rc;
74
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020075 rc = 3;
76 asm volatile(
Horst Hummelfd49f412005-09-03 15:58:00 -070077 " diag 0,%2,0x250\n"
78 "0: ipm %0\n"
79 " srl %0,28\n"
80 " or %0,1\n"
81 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020082 EX_TABLE(0b,1b)
83 : "+d" (rc), "=m" (*(addr_type *) iob)
84 : "d" (cmd), "d" (reg0), "m" (*(addr_type *) iob)
85 : "1", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return rc;
87}
88
Horst Hummelfd49f412005-09-03 15:58:00 -070089/* Initialize block I/O to DIAG device using the specified blocksize and
90 * block offset. On success, return zero and set end_block to contain the
91 * number of blocks on the device minus the specified offset. Return non-zero
92 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010093static inline int
Horst Hummelfd49f412005-09-03 15:58:00 -070094mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
95 blocknum_t offset, blocknum_t *end_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct dasd_diag_private *private;
98 struct dasd_diag_init_io *iib;
99 int rc;
100
101 private = (struct dasd_diag_private *) device->private;
102 iib = &private->iib;
103 memset(iib, 0, sizeof (struct dasd_diag_init_io));
104
105 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
106 iib->block_size = blocksize;
107 iib->offset = offset;
Horst Hummelfd49f412005-09-03 15:58:00 -0700108 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 rc = dia250(iib, INIT_BIO);
111
Horst Hummelfd49f412005-09-03 15:58:00 -0700112 if ((rc & 3) == 0 && end_block)
113 *end_block = iib->end_block;
114
115 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Horst Hummelfd49f412005-09-03 15:58:00 -0700118/* Remove block I/O environment for device. Return zero on success, non-zero
119 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100120static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121mdsk_term_io(struct dasd_device * device)
122{
123 struct dasd_diag_private *private;
124 struct dasd_diag_init_io *iib;
125 int rc;
126
127 private = (struct dasd_diag_private *) device->private;
128 iib = &private->iib;
129 memset(iib, 0, sizeof (struct dasd_diag_init_io));
130 iib->dev_nr = _ccw_device_get_device_number(device->cdev);
131 rc = dia250(iib, TERM_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700132 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Horst Hummelfd49f412005-09-03 15:58:00 -0700135/* Error recovery for failed DIAG requests - try to reestablish the DIAG
136 * environment. */
137static void
138dasd_diag_erp(struct dasd_device *device)
139{
140 int rc;
141
142 mdsk_term_io(device);
143 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
144 if (rc)
145 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
146 "rc=%d", rc);
147}
148
149/* Start a given request at the device. Return zero on success, non-zero
150 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int
152dasd_start_diag(struct dasd_ccw_req * cqr)
153{
154 struct dasd_device *device;
155 struct dasd_diag_private *private;
156 struct dasd_diag_req *dreq;
157 int rc;
158
159 device = cqr->device;
Horst Hummelfd49f412005-09-03 15:58:00 -0700160 if (cqr->retries < 0) {
161 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
162 "- no retry left)", cqr);
163 cqr->status = DASD_CQR_FAILED;
164 return -EIO;
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 private = (struct dasd_diag_private *) device->private;
167 dreq = (struct dasd_diag_req *) cqr->data;
168
169 private->iob.dev_nr = _ccw_device_get_device_number(device->cdev);
170 private->iob.key = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700171 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 private->iob.block_count = dreq->block_count;
Horst Hummelfd49f412005-09-03 15:58:00 -0700173 private->iob.interrupt_params = (addr_t) cqr;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800174 private->iob.bio_list = dreq->bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700175 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 cqr->startclk = get_clock();
Horst Hummelfd49f412005-09-03 15:58:00 -0700178 cqr->starttime = jiffies;
179 cqr->retries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 rc = dia250(&private->iob, RW_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700182 switch (rc) {
183 case 0: /* Synchronous I/O finished successfully */
184 cqr->stopclk = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 cqr->status = DASD_CQR_DONE;
Horst Hummelfd49f412005-09-03 15:58:00 -0700186 /* Indicate to calling function that only a dasd_schedule_bh()
187 and no timer is needed */
188 rc = -EACCES;
189 break;
190 case 8: /* Asynchronous I/O was started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 cqr->status = DASD_CQR_IN_IO;
192 rc = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700193 break;
194 default: /* Error condition */
195 cqr->status = DASD_CQR_QUEUED;
196 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
197 dasd_diag_erp(device);
198 rc = -EIO;
199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 return rc;
202}
203
Horst Hummelfd49f412005-09-03 15:58:00 -0700204/* Terminate given request at the device. */
205static int
206dasd_diag_term_IO(struct dasd_ccw_req * cqr)
207{
208 struct dasd_device *device;
209
210 device = cqr->device;
211 mdsk_term_io(device);
212 mdsk_init_io(device, device->bp_block, 0, NULL);
213 cqr->status = DASD_CQR_CLEAR;
214 cqr->stopclk = get_clock();
215 dasd_schedule_bh(device);
216 return 0;
217}
218
219/* Handle external interruption. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static void
Heiko Carstens5a489b92006-10-06 16:38:35 +0200221dasd_ext_handler(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct dasd_ccw_req *cqr, *next;
224 struct dasd_device *device;
225 unsigned long long expires;
226 unsigned long flags;
Horst Hummelfd49f412005-09-03 15:58:00 -0700227 u8 int_code, status;
228 addr_t ip;
229 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Horst Hummelfd49f412005-09-03 15:58:00 -0700231 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
232 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
233 switch (int_code) {
234 case DASD_DIAG_CODE_31BIT:
235 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
236 break;
237 case DASD_DIAG_CODE_64BIT:
238 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
239 break;
240 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return;
Horst Hummelfd49f412005-09-03 15:58:00 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!ip) { /* no intparm: unsolicited interrupt */
244 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
245 return;
246 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700247 cqr = (struct dasd_ccw_req *) ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 device = (struct dasd_device *) cqr->device;
249 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
250 DEV_MESSAGE(KERN_WARNING, device,
251 " magic number of dasd_ccw_req 0x%08X doesn't"
252 " match discipline 0x%08X",
253 cqr->magic, *(int *) (&device->discipline->name));
254 return;
255 }
256
257 /* get irq lock to modify request queue */
258 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
259
Horst Hummelfd49f412005-09-03 15:58:00 -0700260 /* Check for a pending clear operation */
261 if (cqr->status == DASD_CQR_CLEAR) {
262 cqr->status = DASD_CQR_QUEUED;
263 dasd_clear_timer(device);
264 dasd_schedule_bh(device);
265 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
266 return;
267 }
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 cqr->stopclk = get_clock();
270
271 expires = 0;
272 if (status == 0) {
273 cqr->status = DASD_CQR_DONE;
274 /* Start first request on queue if possible -> fast_io. */
275 if (!list_empty(&device->ccw_queue)) {
276 next = list_entry(device->ccw_queue.next,
277 struct dasd_ccw_req, list);
278 if (next->status == DASD_CQR_QUEUED) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700279 rc = dasd_start_diag(next);
280 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 expires = next->expires;
Horst Hummelfd49f412005-09-03 15:58:00 -0700282 else if (rc != -EACCES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 DEV_MESSAGE(KERN_WARNING, device, "%s",
284 "Interrupt fastpath "
285 "failed!");
286 }
287 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700288 } else {
289 cqr->status = DASD_CQR_QUEUED;
290 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
291 "request %p was %d (%d retries left)", cqr, status,
292 cqr->retries);
293 dasd_diag_erp(device);
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (expires != 0)
297 dasd_set_timer(device, expires);
298 else
299 dasd_clear_timer(device);
300 dasd_schedule_bh(device);
301
302 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
303}
304
Horst Hummelfd49f412005-09-03 15:58:00 -0700305/* Check whether device can be controlled by DIAG discipline. Return zero on
306 * success, non-zero otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static int
308dasd_diag_check_device(struct dasd_device *device)
309{
310 struct dasd_diag_private *private;
311 struct dasd_diag_characteristics *rdc_data;
312 struct dasd_diag_bio bio;
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800313 struct vtoc_cms_label *label;
Horst Hummelfd49f412005-09-03 15:58:00 -0700314 blocknum_t end_block;
315 unsigned int sb, bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int rc;
317
318 private = (struct dasd_diag_private *) device->private;
319 if (private == NULL) {
Horst Hummel138c0142006-06-29 14:58:12 +0200320 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (private == NULL) {
322 DEV_MESSAGE(KERN_WARNING, device, "%s",
323 "memory allocation failed for private data");
324 return -ENOMEM;
325 }
326 device->private = (void *) private;
327 }
328 /* Read Device Characteristics */
329 rdc_data = (void *) &(private->rdc_data);
330 rdc_data->dev_nr = _ccw_device_get_device_number(device->cdev);
331 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
332
333 rc = diag210((struct diag210 *) rdc_data);
Horst Hummelfd49f412005-09-03 15:58:00 -0700334 if (rc) {
335 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
336 "information (rc=%d)", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -ENOTSUPP;
Horst Hummelfd49f412005-09-03 15:58:00 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Figure out position of label block */
341 switch (private->rdc_data.vdev_class) {
342 case DEV_CLASS_FBA:
343 private->pt_block = 1;
344 break;
345 case DEV_CLASS_ECKD:
346 private->pt_block = 2;
347 break;
348 default:
Horst Hummelfd49f412005-09-03 15:58:00 -0700349 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
350 "(class=%d)", private->rdc_data.vdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -ENOTSUPP;
352 }
353
354 DBF_DEV_EVENT(DBF_INFO, device,
355 "%04X: %04X on real %04X/%02X",
356 rdc_data->dev_nr,
357 rdc_data->vdev_type,
358 rdc_data->rdev_type, rdc_data->rdev_model);
359
360 /* terminate all outstanding operations */
361 mdsk_term_io(device);
362
363 /* figure out blocksize of device */
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800364 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (label == NULL) {
366 DEV_MESSAGE(KERN_WARNING, device, "%s",
367 "No memory to allocate initialization request");
368 return -ENOMEM;
369 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700370 rc = 0;
371 end_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* try all sizes - needed for ECKD devices */
373 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700374 mdsk_init_io(device, bsize, 0, &end_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 memset(&bio, 0, sizeof (struct dasd_diag_bio));
376 bio.type = MDSK_READ_REQ;
377 bio.block_number = private->pt_block + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800378 bio.buffer = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
380 private->iob.dev_nr = rdc_data->dev_nr;
381 private->iob.key = 0;
382 private->iob.flags = 0; /* do synchronous io */
383 private->iob.block_count = 1;
384 private->iob.interrupt_params = 0;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800385 private->iob.bio_list = &bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700386 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
387 rc = dia250(&private->iob, RW_BIO);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800388 if (rc == 3) {
389 DEV_MESSAGE(KERN_WARNING, device, "%s",
390 "DIAG call failed");
391 rc = -EOPNOTSUPP;
392 goto out;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 mdsk_term_io(device);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800395 if (rc == 0)
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800398 if (bsize > PAGE_SIZE) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700399 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
400 "(rc=%d)", rc);
401 rc = -EIO;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800402 goto out;
403 }
404 /* check for label block */
405 if (memcmp(label->label_id, DASD_DIAG_CMS1,
406 sizeof(DASD_DIAG_CMS1)) == 0) {
407 /* get formatted blocksize from label block */
408 bsize = (unsigned int) label->block_size;
409 device->blocks = (unsigned long) label->block_count;
410 } else
411 device->blocks = end_block;
412 device->bp_block = bsize;
413 device->s2b_shift = 0; /* bits to shift 512 to get a block */
414 for (sb = 512; sb < bsize; sb = sb << 1)
415 device->s2b_shift++;
416 rc = mdsk_init_io(device, device->bp_block, 0, NULL);
417 if (rc) {
418 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
419 "failed (rc=%d)", rc);
420 rc = -EIO;
Horst Hummelfd49f412005-09-03 15:58:00 -0700421 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 DEV_MESSAGE(KERN_INFO, device,
Horst Hummelfd49f412005-09-03 15:58:00 -0700423 "(%ld B/blk): %ldkB",
424 (unsigned long) device->bp_block,
425 (unsigned long) (device->blocks <<
426 device->s2b_shift) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800428out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 free_page((long) label);
430 return rc;
431}
432
Horst Hummelfd49f412005-09-03 15:58:00 -0700433/* Fill in virtual disk geometry for device. Return zero on success, non-zero
434 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static int
436dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
437{
438 if (dasd_check_blocksize(device->bp_block) != 0)
439 return -EINVAL;
440 geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
441 geo->heads = 16;
442 geo->sectors = 128 >> device->s2b_shift;
443 return 0;
444}
445
446static dasd_era_t
447dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
448{
449 return dasd_era_fatal;
450}
451
452static dasd_erp_fn_t
453dasd_diag_erp_action(struct dasd_ccw_req * cqr)
454{
455 return dasd_default_erp_action;
456}
457
458static dasd_erp_fn_t
459dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
460{
461 return dasd_default_erp_postaction;
462}
463
Horst Hummelfd49f412005-09-03 15:58:00 -0700464/* Create DASD request from block device request. Return pointer to new
465 * request on success, ERR_PTR otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static struct dasd_ccw_req *
467dasd_diag_build_cp(struct dasd_device * device, struct request *req)
468{
469 struct dasd_ccw_req *cqr;
470 struct dasd_diag_req *dreq;
471 struct dasd_diag_bio *dbio;
472 struct bio *bio;
473 struct bio_vec *bv;
474 char *dst;
Horst Hummelfd49f412005-09-03 15:58:00 -0700475 unsigned int count, datasize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sector_t recid, first_rec, last_rec;
Horst Hummelfd49f412005-09-03 15:58:00 -0700477 unsigned int blksize, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned char rw_cmd;
479 int i;
480
481 if (rq_data_dir(req) == READ)
482 rw_cmd = MDSK_READ_REQ;
483 else if (rq_data_dir(req) == WRITE)
484 rw_cmd = MDSK_WRITE_REQ;
485 else
486 return ERR_PTR(-EINVAL);
487 blksize = device->bp_block;
488 /* Calculate record id of first and last block. */
489 first_rec = req->sector >> device->s2b_shift;
490 last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
491 /* Check struct bio and count the number of blocks for the request. */
492 count = 0;
493 rq_for_each_bio(bio, req) {
494 bio_for_each_segment(bv, bio, i) {
495 if (bv->bv_len & (blksize - 1))
496 /* Fba can only do full blocks. */
497 return ERR_PTR(-EINVAL);
498 count += bv->bv_len >> (device->s2b_shift + 9);
499 }
500 }
501 /* Paranoia. */
502 if (count != last_rec - first_rec + 1)
503 return ERR_PTR(-EINVAL);
504 /* Build the request */
505 datasize = sizeof(struct dasd_diag_req) +
506 count*sizeof(struct dasd_diag_bio);
507 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
508 datasize, device);
509 if (IS_ERR(cqr))
510 return cqr;
Horst Hummel138c0142006-06-29 14:58:12 +0200511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dreq = (struct dasd_diag_req *) cqr->data;
513 dreq->block_count = count;
514 dbio = dreq->bio;
515 recid = first_rec;
516 rq_for_each_bio(bio, req) {
517 bio_for_each_segment(bv, bio, i) {
518 dst = page_address(bv->bv_page) + bv->bv_offset;
519 for (off = 0; off < bv->bv_len; off += blksize) {
520 memset(dbio, 0, sizeof (struct dasd_diag_bio));
521 dbio->type = rw_cmd;
522 dbio->block_number = recid + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800523 dbio->buffer = dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dbio++;
525 dst += blksize;
526 recid++;
527 }
528 }
529 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700530 cqr->retries = DIAG_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 cqr->buildclk = get_clock();
Jens Axboe4aff5e22006-08-10 08:44:47 +0200532 if (req->cmd_flags & REQ_FAILFAST)
Horst Hummel1c01b8a2006-01-06 00:19:15 -0800533 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 cqr->device = device;
Horst Hummelfd49f412005-09-03 15:58:00 -0700535 cqr->expires = DIAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cqr->status = DASD_CQR_FILLED;
537 return cqr;
538}
539
Horst Hummelfd49f412005-09-03 15:58:00 -0700540/* Release DASD request. Return non-zero if request was successful, zero
541 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static int
543dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
544{
545 int status;
546
547 status = cqr->status == DASD_CQR_DONE;
548 dasd_sfree_request(cqr, cqr->device);
549 return status;
550}
551
Horst Hummelfd49f412005-09-03 15:58:00 -0700552/* Fill in IOCTL data for device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int
554dasd_diag_fill_info(struct dasd_device * device,
555 struct dasd_information2_t * info)
556{
557 struct dasd_diag_private *private;
558
559 private = (struct dasd_diag_private *) device->private;
Horst Hummelfd49f412005-09-03 15:58:00 -0700560 info->label_block = (unsigned int) private->pt_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 info->FBA_layout = 1;
562 info->format = DASD_FORMAT_LDL;
563 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
564 memcpy(info->characteristics,
565 &((struct dasd_diag_private *) device->private)->rdc_data,
566 sizeof (struct dasd_diag_characteristics));
567 info->confdata_size = 0;
568 return 0;
569}
570
571static void
572dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
573 struct irb *stat)
574{
575 DEV_MESSAGE(KERN_ERR, device, "%s",
576 "dump sense not available for DIAG data");
577}
578
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100579static struct dasd_discipline dasd_diag_discipline = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 .owner = THIS_MODULE,
581 .name = "DIAG",
582 .ebcname = "DIAG",
Horst Hummelfd49f412005-09-03 15:58:00 -0700583 .max_blocks = DIAG_MAX_BLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .check_device = dasd_diag_check_device,
585 .fill_geometry = dasd_diag_fill_geometry,
586 .start_IO = dasd_start_diag,
Horst Hummelfd49f412005-09-03 15:58:00 -0700587 .term_IO = dasd_diag_term_IO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 .examine_error = dasd_diag_examine_error,
589 .erp_action = dasd_diag_erp_action,
590 .erp_postaction = dasd_diag_erp_postaction,
591 .build_cp = dasd_diag_build_cp,
592 .free_cp = dasd_diag_free_cp,
593 .dump_sense = dasd_diag_dump_sense,
594 .fill_info = dasd_diag_fill_info,
595};
596
597static int __init
598dasd_diag_init(void)
599{
600 if (!MACHINE_IS_VM) {
601 MESSAGE_LOG(KERN_INFO,
602 "Machine is not VM: %s "
603 "discipline not initializing",
604 dasd_diag_discipline.name);
Horst Hummelfd49f412005-09-03 15:58:00 -0700605 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 ASCEBC(dasd_diag_discipline.ebcname, 4);
608
609 ctl_set_bit(0, 9);
610 register_external_interrupt(0x2603, dasd_ext_handler);
611 dasd_diag_discipline_pointer = &dasd_diag_discipline;
612 return 0;
613}
614
615static void __exit
616dasd_diag_cleanup(void)
617{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unregister_external_interrupt(0x2603, dasd_ext_handler);
619 ctl_clear_bit(0, 9);
620 dasd_diag_discipline_pointer = NULL;
621}
622
623module_init(dasd_diag_init);
624module_exit(dasd_diag_cleanup);