blob: d91df38ee4f7cb4ed418a043e689fc95126e8c7e [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>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020027#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "dasd_int.h"
30#include "dasd_diag.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define PRINTK_HEADER "dasd(diag):"
33
34MODULE_LICENSE("GPL");
35
Horst Hummelfd49f412005-09-03 15:58:00 -070036/* The maximum number of blocks per request (max_blocks) is dependent on the
37 * amount of storage that is available in the static I/O buffer for each
38 * device. Currently each device gets 2 pages. We want to fit two requests
39 * into the available memory so that we can immediately start the next if one
40 * finishes. */
41#define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
42 sizeof(struct dasd_diag_req)) / \
43 sizeof(struct dasd_diag_bio)) / 2)
44#define DIAG_MAX_RETRIES 32
45#define DIAG_TIMEOUT 50 * HZ
46
Heiko Carstens2b67fc42007-02-05 21:16:47 +010047static struct dasd_discipline dasd_diag_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49struct dasd_diag_private {
50 struct dasd_diag_characteristics rdc_data;
51 struct dasd_diag_rw_io iob;
52 struct dasd_diag_init_io iib;
Horst Hummelfd49f412005-09-03 15:58:00 -070053 blocknum_t pt_block;
Cornelia Huck9a92fe42007-05-10 15:45:42 +020054 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57struct dasd_diag_req {
Horst Hummelfd49f412005-09-03 15:58:00 -070058 unsigned int block_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct dasd_diag_bio bio[0];
60};
61
Horst Hummelfd49f412005-09-03 15:58:00 -070062static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
63
64/* Perform DIAG250 call with block I/O parameter list iob (input and output)
65 * and function code cmd.
66 * In case of an exception return 3. Otherwise return result of bitwise OR of
67 * resulting condition code and DIAG return code. */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020068static inline int dia250(void *iob, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Peter Oberparleiter04439692007-03-26 20:42:38 +020070 register unsigned long reg2 asm ("2") = (unsigned long) iob;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -080071 typedef union {
72 struct dasd_diag_init_io init_io;
73 struct dasd_diag_rw_io rw_io;
Horst Hummelfd49f412005-09-03 15:58:00 -070074 } addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int rc;
76
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020077 rc = 3;
78 asm volatile(
Peter Oberparleiter04439692007-03-26 20:42:38 +020079 " diag 2,%2,0x250\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070080 "0: ipm %0\n"
81 " srl %0,28\n"
Peter Oberparleiter04439692007-03-26 20:42:38 +020082 " or %0,3\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070083 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020084 EX_TABLE(0b,1b)
85 : "+d" (rc), "=m" (*(addr_type *) iob)
Peter Oberparleiter04439692007-03-26 20:42:38 +020086 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
87 : "3", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return rc;
89}
90
Horst Hummelfd49f412005-09-03 15:58:00 -070091/* Initialize block I/O to DIAG device using the specified blocksize and
92 * block offset. On success, return zero and set end_block to contain the
93 * number of blocks on the device minus the specified offset. Return non-zero
94 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010095static inline int
Horst Hummelfd49f412005-09-03 15:58:00 -070096mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
97 blocknum_t offset, blocknum_t *end_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct dasd_diag_private *private;
100 struct dasd_diag_init_io *iib;
101 int rc;
102
103 private = (struct dasd_diag_private *) device->private;
104 iib = &private->iib;
105 memset(iib, 0, sizeof (struct dasd_diag_init_io));
106
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200107 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 iib->block_size = blocksize;
109 iib->offset = offset;
Horst Hummelfd49f412005-09-03 15:58:00 -0700110 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 rc = dia250(iib, INIT_BIO);
113
Horst Hummelfd49f412005-09-03 15:58:00 -0700114 if ((rc & 3) == 0 && end_block)
115 *end_block = iib->end_block;
116
117 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Horst Hummelfd49f412005-09-03 15:58:00 -0700120/* Remove block I/O environment for device. Return zero on success, non-zero
121 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100122static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123mdsk_term_io(struct dasd_device * device)
124{
125 struct dasd_diag_private *private;
126 struct dasd_diag_init_io *iib;
127 int rc;
128
129 private = (struct dasd_diag_private *) device->private;
130 iib = &private->iib;
131 memset(iib, 0, sizeof (struct dasd_diag_init_io));
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200132 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 rc = dia250(iib, TERM_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700134 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Horst Hummelfd49f412005-09-03 15:58:00 -0700137/* Error recovery for failed DIAG requests - try to reestablish the DIAG
138 * environment. */
139static void
140dasd_diag_erp(struct dasd_device *device)
141{
142 int rc;
143
144 mdsk_term_io(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100145 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
Horst Hummelfd49f412005-09-03 15:58:00 -0700146 if (rc)
147 DEV_MESSAGE(KERN_WARNING, device, "DIAG ERP unsuccessful, "
148 "rc=%d", rc);
149}
150
151/* Start a given request at the device. Return zero on success, non-zero
152 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int
154dasd_start_diag(struct dasd_ccw_req * cqr)
155{
156 struct dasd_device *device;
157 struct dasd_diag_private *private;
158 struct dasd_diag_req *dreq;
159 int rc;
160
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100161 device = cqr->startdev;
Horst Hummelfd49f412005-09-03 15:58:00 -0700162 if (cqr->retries < 0) {
163 DEV_MESSAGE(KERN_WARNING, device, "DIAG start_IO: request %p "
164 "- no retry left)", cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100165 cqr->status = DASD_CQR_ERROR;
Horst Hummelfd49f412005-09-03 15:58:00 -0700166 return -EIO;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 private = (struct dasd_diag_private *) device->private;
169 dreq = (struct dasd_diag_req *) cqr->data;
170
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200171 private->iob.dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 private->iob.key = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700173 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 private->iob.block_count = dreq->block_count;
Horst Hummelfd49f412005-09-03 15:58:00 -0700175 private->iob.interrupt_params = (addr_t) cqr;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800176 private->iob.bio_list = dreq->bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700177 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 cqr->startclk = get_clock();
Horst Hummelfd49f412005-09-03 15:58:00 -0700180 cqr->starttime = jiffies;
181 cqr->retries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 rc = dia250(&private->iob, RW_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700184 switch (rc) {
185 case 0: /* Synchronous I/O finished successfully */
186 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100187 cqr->status = DASD_CQR_SUCCESS;
Horst Hummelfd49f412005-09-03 15:58:00 -0700188 /* Indicate to calling function that only a dasd_schedule_bh()
189 and no timer is needed */
190 rc = -EACCES;
191 break;
192 case 8: /* Asynchronous I/O was started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 cqr->status = DASD_CQR_IN_IO;
194 rc = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700195 break;
196 default: /* Error condition */
197 cqr->status = DASD_CQR_QUEUED;
198 DEV_MESSAGE(KERN_WARNING, device, "dia250 returned rc=%d", rc);
199 dasd_diag_erp(device);
200 rc = -EIO;
201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 return rc;
204}
205
Horst Hummelfd49f412005-09-03 15:58:00 -0700206/* Terminate given request at the device. */
207static int
208dasd_diag_term_IO(struct dasd_ccw_req * cqr)
209{
210 struct dasd_device *device;
211
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 device = cqr->startdev;
Horst Hummelfd49f412005-09-03 15:58:00 -0700213 mdsk_term_io(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100214 mdsk_init_io(device, device->block->bp_block, 0, NULL);
215 cqr->status = DASD_CQR_CLEAR_PENDING;
Horst Hummelfd49f412005-09-03 15:58:00 -0700216 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217 dasd_schedule_device_bh(device);
Horst Hummelfd49f412005-09-03 15:58:00 -0700218 return 0;
219}
220
221/* Handle external interruption. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void
Heiko Carstens5a489b92006-10-06 16:38:35 +0200223dasd_ext_handler(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct dasd_ccw_req *cqr, *next;
226 struct dasd_device *device;
227 unsigned long long expires;
228 unsigned long flags;
Horst Hummelfd49f412005-09-03 15:58:00 -0700229 u8 int_code, status;
230 addr_t ip;
231 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Horst Hummelfd49f412005-09-03 15:58:00 -0700233 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
234 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
235 switch (int_code) {
236 case DASD_DIAG_CODE_31BIT:
237 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
238 break;
239 case DASD_DIAG_CODE_64BIT:
240 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
241 break;
242 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return;
Horst Hummelfd49f412005-09-03 15:58:00 -0700244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!ip) { /* no intparm: unsolicited interrupt */
246 MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
247 return;
248 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700249 cqr = (struct dasd_ccw_req *) ip;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100250 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
252 DEV_MESSAGE(KERN_WARNING, device,
253 " magic number of dasd_ccw_req 0x%08X doesn't"
254 " match discipline 0x%08X",
255 cqr->magic, *(int *) (&device->discipline->name));
256 return;
257 }
258
259 /* get irq lock to modify request queue */
260 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
261
Horst Hummelfd49f412005-09-03 15:58:00 -0700262 /* Check for a pending clear operation */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100263 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
264 cqr->status = DASD_CQR_CLEARED;
265 dasd_device_clear_timer(device);
266 dasd_schedule_device_bh(device);
Horst Hummelfd49f412005-09-03 15:58:00 -0700267 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
268 return;
269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 cqr->stopclk = get_clock();
272
273 expires = 0;
274 if (status == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100275 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Start first request on queue if possible -> fast_io. */
277 if (!list_empty(&device->ccw_queue)) {
278 next = list_entry(device->ccw_queue.next,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (next->status == DASD_CQR_QUEUED) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700281 rc = dasd_start_diag(next);
282 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 expires = next->expires;
Horst Hummelfd49f412005-09-03 15:58:00 -0700284 else if (rc != -EACCES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 DEV_MESSAGE(KERN_WARNING, device, "%s",
286 "Interrupt fastpath "
287 "failed!");
288 }
289 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700290 } else {
291 cqr->status = DASD_CQR_QUEUED;
292 DEV_MESSAGE(KERN_WARNING, device, "interrupt status for "
293 "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) {
325 DEV_MESSAGE(KERN_WARNING, device, "%s",
326 "memory allocation failed for private data");
327 return -ENOMEM;
328 }
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200329 ccw_device_get_id(device->cdev, &private->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 device->private = (void *) private;
331 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 block = dasd_alloc_block();
333 if (IS_ERR(block)) {
334 DEV_MESSAGE(KERN_WARNING, device, "%s",
335 "could not allocate dasd block structure");
336 kfree(device->private);
337 return PTR_ERR(block);
338 }
339 device->block = block;
340 block->base = device;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Read Device Characteristics */
343 rdc_data = (void *) &(private->rdc_data);
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200344 rdc_data->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
346
347 rc = diag210((struct diag210 *) rdc_data);
Horst Hummelfd49f412005-09-03 15:58:00 -0700348 if (rc) {
349 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
350 "information (rc=%d)", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -ENOTSUPP;
Horst Hummelfd49f412005-09-03 15:58:00 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Figure out position of label block */
355 switch (private->rdc_data.vdev_class) {
356 case DEV_CLASS_FBA:
357 private->pt_block = 1;
358 break;
359 case DEV_CLASS_ECKD:
360 private->pt_block = 2;
361 break;
362 default:
Horst Hummelfd49f412005-09-03 15:58:00 -0700363 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
364 "(class=%d)", private->rdc_data.vdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ENOTSUPP;
366 }
367
368 DBF_DEV_EVENT(DBF_INFO, device,
369 "%04X: %04X on real %04X/%02X",
370 rdc_data->dev_nr,
371 rdc_data->vdev_type,
372 rdc_data->rdev_type, rdc_data->rdev_model);
373
374 /* terminate all outstanding operations */
375 mdsk_term_io(device);
376
377 /* figure out blocksize of device */
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800378 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (label == NULL) {
380 DEV_MESSAGE(KERN_WARNING, device, "%s",
381 "No memory to allocate initialization request");
382 return -ENOMEM;
383 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700384 rc = 0;
385 end_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* try all sizes - needed for ECKD devices */
387 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700388 mdsk_init_io(device, bsize, 0, &end_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 memset(&bio, 0, sizeof (struct dasd_diag_bio));
390 bio.type = MDSK_READ_REQ;
391 bio.block_number = private->pt_block + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800392 bio.buffer = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
394 private->iob.dev_nr = rdc_data->dev_nr;
395 private->iob.key = 0;
396 private->iob.flags = 0; /* do synchronous io */
397 private->iob.block_count = 1;
398 private->iob.interrupt_params = 0;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800399 private->iob.bio_list = &bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700400 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
401 rc = dia250(&private->iob, RW_BIO);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800402 if (rc == 3) {
403 DEV_MESSAGE(KERN_WARNING, device, "%s",
404 "DIAG call failed");
405 rc = -EOPNOTSUPP;
406 goto out;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 mdsk_term_io(device);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800409 if (rc == 0)
410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800412 if (bsize > PAGE_SIZE) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700413 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
414 "(rc=%d)", rc);
415 rc = -EIO;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800416 goto out;
417 }
418 /* check for label block */
419 if (memcmp(label->label_id, DASD_DIAG_CMS1,
420 sizeof(DASD_DIAG_CMS1)) == 0) {
421 /* get formatted blocksize from label block */
422 bsize = (unsigned int) label->block_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100423 block->blocks = (unsigned long) label->block_count;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800424 } else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100425 block->blocks = end_block;
426 block->bp_block = bsize;
427 block->s2b_shift = 0; /* bits to shift 512 to get a block */
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800428 for (sb = 512; sb < bsize; sb = sb << 1)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100429 block->s2b_shift++;
430 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800431 if (rc) {
432 DEV_MESSAGE(KERN_WARNING, device, "DIAG initialization "
433 "failed (rc=%d)", rc);
434 rc = -EIO;
Horst Hummelfd49f412005-09-03 15:58:00 -0700435 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 DEV_MESSAGE(KERN_INFO, device,
Horst Hummelfd49f412005-09-03 15:58:00 -0700437 "(%ld B/blk): %ldkB",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100438 (unsigned long) block->bp_block,
439 (unsigned long) (block->blocks <<
440 block->s2b_shift) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800442out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 free_page((long) label);
444 return rc;
445}
446
Horst Hummelfd49f412005-09-03 15:58:00 -0700447/* Fill in virtual disk geometry for device. Return zero on success, non-zero
448 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static int
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100450dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100452 if (dasd_check_blocksize(block->bp_block) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100454 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 geo->heads = 16;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100456 geo->sectors = 128 >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static dasd_erp_fn_t
461dasd_diag_erp_action(struct dasd_ccw_req * cqr)
462{
463 return dasd_default_erp_action;
464}
465
466static dasd_erp_fn_t
467dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
468{
469 return dasd_default_erp_postaction;
470}
471
Horst Hummelfd49f412005-09-03 15:58:00 -0700472/* Create DASD request from block device request. Return pointer to new
473 * request on success, ERR_PTR otherwise. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100474static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
475 struct dasd_block *block,
476 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct dasd_ccw_req *cqr;
479 struct dasd_diag_req *dreq;
480 struct dasd_diag_bio *dbio;
NeilBrown5705f702007-09-25 12:35:59 +0200481 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct bio_vec *bv;
483 char *dst;
Horst Hummelfd49f412005-09-03 15:58:00 -0700484 unsigned int count, datasize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 sector_t recid, first_rec, last_rec;
Horst Hummelfd49f412005-09-03 15:58:00 -0700486 unsigned int blksize, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unsigned char rw_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (rq_data_dir(req) == READ)
490 rw_cmd = MDSK_READ_REQ;
491 else if (rq_data_dir(req) == WRITE)
492 rw_cmd = MDSK_WRITE_REQ;
493 else
494 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100495 blksize = block->bp_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Calculate record id of first and last block. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100497 first_rec = req->sector >> block->s2b_shift;
498 last_rec = (req->sector + req->nr_sectors - 1) >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Check struct bio and count the number of blocks for the request. */
500 count = 0;
NeilBrown5705f702007-09-25 12:35:59 +0200501 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200502 if (bv->bv_len & (blksize - 1))
503 /* Fba can only do full blocks. */
504 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100505 count += bv->bv_len >> (block->s2b_shift + 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 /* Paranoia. */
508 if (count != last_rec - first_rec + 1)
509 return ERR_PTR(-EINVAL);
510 /* Build the request */
511 datasize = sizeof(struct dasd_diag_req) +
512 count*sizeof(struct dasd_diag_bio);
513 cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100514 datasize, memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (IS_ERR(cqr))
516 return cqr;
Horst Hummel138c0142006-06-29 14:58:12 +0200517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 dreq = (struct dasd_diag_req *) cqr->data;
519 dreq->block_count = count;
520 dbio = dreq->bio;
521 recid = first_rec;
NeilBrown5705f702007-09-25 12:35:59 +0200522 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200523 dst = page_address(bv->bv_page) + bv->bv_offset;
524 for (off = 0; off < bv->bv_len; off += blksize) {
525 memset(dbio, 0, sizeof (struct dasd_diag_bio));
526 dbio->type = rw_cmd;
527 dbio->block_number = recid + 1;
528 dbio->buffer = dst;
529 dbio++;
530 dst += blksize;
531 recid++;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700534 cqr->retries = DIAG_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 cqr->buildclk = get_clock();
Jens Axboe4aff5e22006-08-10 08:44:47 +0200536 if (req->cmd_flags & REQ_FAILFAST)
Horst Hummel1c01b8a2006-01-06 00:19:15 -0800537 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100538 cqr->startdev = memdev;
539 cqr->memdev = memdev;
540 cqr->block = block;
Horst Hummelfd49f412005-09-03 15:58:00 -0700541 cqr->expires = DIAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 cqr->status = DASD_CQR_FILLED;
543 return cqr;
544}
545
Horst Hummelfd49f412005-09-03 15:58:00 -0700546/* Release DASD request. Return non-zero if request was successful, zero
547 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int
549dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
550{
551 int status;
552
553 status = cqr->status == DASD_CQR_DONE;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100554 dasd_sfree_request(cqr, cqr->memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return status;
556}
557
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100558static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
559{
560 cqr->status = DASD_CQR_FILLED;
561};
562
Horst Hummelfd49f412005-09-03 15:58:00 -0700563/* Fill in IOCTL data for device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static int
565dasd_diag_fill_info(struct dasd_device * device,
566 struct dasd_information2_t * info)
567{
568 struct dasd_diag_private *private;
569
570 private = (struct dasd_diag_private *) device->private;
Horst Hummelfd49f412005-09-03 15:58:00 -0700571 info->label_block = (unsigned int) private->pt_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 info->FBA_layout = 1;
573 info->format = DASD_FORMAT_LDL;
574 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
575 memcpy(info->characteristics,
576 &((struct dasd_diag_private *) device->private)->rdc_data,
577 sizeof (struct dasd_diag_characteristics));
578 info->confdata_size = 0;
579 return 0;
580}
581
582static void
583dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
584 struct irb *stat)
585{
586 DEV_MESSAGE(KERN_ERR, device, "%s",
587 "dump sense not available for DIAG data");
588}
589
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100590static struct dasd_discipline dasd_diag_discipline = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .owner = THIS_MODULE,
592 .name = "DIAG",
593 .ebcname = "DIAG",
Horst Hummelfd49f412005-09-03 15:58:00 -0700594 .max_blocks = DIAG_MAX_BLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .check_device = dasd_diag_check_device,
596 .fill_geometry = dasd_diag_fill_geometry,
597 .start_IO = dasd_start_diag,
Horst Hummelfd49f412005-09-03 15:58:00 -0700598 .term_IO = dasd_diag_term_IO,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100599 .handle_terminated_request = dasd_diag_handle_terminated_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .erp_action = dasd_diag_erp_action,
601 .erp_postaction = dasd_diag_erp_postaction,
602 .build_cp = dasd_diag_build_cp,
603 .free_cp = dasd_diag_free_cp,
604 .dump_sense = dasd_diag_dump_sense,
605 .fill_info = dasd_diag_fill_info,
606};
607
608static int __init
609dasd_diag_init(void)
610{
611 if (!MACHINE_IS_VM) {
612 MESSAGE_LOG(KERN_INFO,
613 "Machine is not VM: %s "
614 "discipline not initializing",
615 dasd_diag_discipline.name);
Horst Hummelfd49f412005-09-03 15:58:00 -0700616 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 ASCEBC(dasd_diag_discipline.ebcname, 4);
619
620 ctl_set_bit(0, 9);
621 register_external_interrupt(0x2603, dasd_ext_handler);
622 dasd_diag_discipline_pointer = &dasd_diag_discipline;
623 return 0;
624}
625
626static void __exit
627dasd_diag_cleanup(void)
628{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 unregister_external_interrupt(0x2603, dasd_ext_handler);
630 ctl_clear_bit(0, 9);
631 dasd_diag_discipline_pointer = NULL;
632}
633
634module_init(dasd_diag_init);
635module_exit(dasd_diag_cleanup);