blob: 687f323cdc38cebac23697c7fc24493aa1c4b40a [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 Haberlandea058542009-12-18 17:43:16 +010011#define KMSG_COMPONENT "dasd"
Stefan Haberlandfc19f382009-03-26 15:23:49 +010012
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>
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080027#include <asm/vtoc.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020028#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "dasd_int.h"
31#include "dasd_diag.h"
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define PRINTK_HEADER "dasd(diag):"
34
35MODULE_LICENSE("GPL");
36
Horst Hummelfd49f412005-09-03 15:58:00 -070037/* The maximum number of blocks per request (max_blocks) is dependent on the
38 * amount of storage that is available in the static I/O buffer for each
39 * device. Currently each device gets 2 pages. We want to fit two requests
40 * into the available memory so that we can immediately start the next if one
41 * finishes. */
42#define DIAG_MAX_BLOCKS (((2 * PAGE_SIZE - sizeof(struct dasd_ccw_req) - \
43 sizeof(struct dasd_diag_req)) / \
44 sizeof(struct dasd_diag_bio)) / 2)
45#define DIAG_MAX_RETRIES 32
46#define DIAG_TIMEOUT 50 * HZ
47
Heiko Carstens2b67fc42007-02-05 21:16:47 +010048static struct dasd_discipline dasd_diag_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50struct dasd_diag_private {
51 struct dasd_diag_characteristics rdc_data;
52 struct dasd_diag_rw_io iob;
53 struct dasd_diag_init_io iib;
Horst Hummelfd49f412005-09-03 15:58:00 -070054 blocknum_t pt_block;
Cornelia Huck9a92fe42007-05-10 15:45:42 +020055 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58struct dasd_diag_req {
Horst Hummelfd49f412005-09-03 15:58:00 -070059 unsigned int block_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct dasd_diag_bio bio[0];
61};
62
Horst Hummelfd49f412005-09-03 15:58:00 -070063static const u8 DASD_DIAG_CMS1[] = { 0xc3, 0xd4, 0xe2, 0xf1 };/* EBCDIC CMS1 */
64
65/* Perform DIAG250 call with block I/O parameter list iob (input and output)
66 * and function code cmd.
67 * In case of an exception return 3. Otherwise return result of bitwise OR of
68 * resulting condition code and DIAG return code. */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020069static inline int dia250(void *iob, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Peter Oberparleiter04439692007-03-26 20:42:38 +020071 register unsigned long reg2 asm ("2") = (unsigned long) iob;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -080072 typedef union {
73 struct dasd_diag_init_io init_io;
74 struct dasd_diag_rw_io rw_io;
Horst Hummelfd49f412005-09-03 15:58:00 -070075 } addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int rc;
77
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020078 rc = 3;
79 asm volatile(
Peter Oberparleiter04439692007-03-26 20:42:38 +020080 " diag 2,%2,0x250\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070081 "0: ipm %0\n"
82 " srl %0,28\n"
Peter Oberparleiter04439692007-03-26 20:42:38 +020083 " or %0,3\n"
Horst Hummelfd49f412005-09-03 15:58:00 -070084 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020085 EX_TABLE(0b,1b)
86 : "+d" (rc), "=m" (*(addr_type *) iob)
Peter Oberparleiter04439692007-03-26 20:42:38 +020087 : "d" (cmd), "d" (reg2), "m" (*(addr_type *) iob)
88 : "3", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return rc;
90}
91
Horst Hummelfd49f412005-09-03 15:58:00 -070092/* Initialize block I/O to DIAG device using the specified blocksize and
93 * block offset. On success, return zero and set end_block to contain the
94 * number of blocks on the device minus the specified offset. Return non-zero
95 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010096static inline int
Horst Hummelfd49f412005-09-03 15:58:00 -070097mdsk_init_io(struct dasd_device *device, unsigned int blocksize,
98 blocknum_t offset, blocknum_t *end_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct dasd_diag_private *private;
101 struct dasd_diag_init_io *iib;
102 int rc;
103
104 private = (struct dasd_diag_private *) device->private;
105 iib = &private->iib;
106 memset(iib, 0, sizeof (struct dasd_diag_init_io));
107
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200108 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 iib->block_size = blocksize;
110 iib->offset = offset;
Horst Hummelfd49f412005-09-03 15:58:00 -0700111 iib->flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 rc = dia250(iib, INIT_BIO);
114
Horst Hummelfd49f412005-09-03 15:58:00 -0700115 if ((rc & 3) == 0 && end_block)
116 *end_block = iib->end_block;
117
118 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Horst Hummelfd49f412005-09-03 15:58:00 -0700121/* Remove block I/O environment for device. Return zero on success, non-zero
122 * otherwise. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100123static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124mdsk_term_io(struct dasd_device * device)
125{
126 struct dasd_diag_private *private;
127 struct dasd_diag_init_io *iib;
128 int rc;
129
130 private = (struct dasd_diag_private *) device->private;
131 iib = &private->iib;
132 memset(iib, 0, sizeof (struct dasd_diag_init_io));
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200133 iib->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 rc = dia250(iib, TERM_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700135 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Horst Hummelfd49f412005-09-03 15:58:00 -0700138/* Error recovery for failed DIAG requests - try to reestablish the DIAG
139 * environment. */
140static void
141dasd_diag_erp(struct dasd_device *device)
142{
143 int rc;
144
145 mdsk_term_io(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100146 rc = mdsk_init_io(device, device->block->bp_block, 0, NULL);
Stefan Weinhuber22825ab2009-12-07 12:51:48 +0100147 if (rc == 4) {
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100148 if (!(test_and_set_bit(DASD_FLAG_DEVICE_RO, &device->flags)))
Stefan Haberlandea058542009-12-18 17:43:16 +0100149 pr_warning("%s: The access mode of a DIAG device "
150 "changed to read-only\n",
151 dev_name(&device->cdev->dev));
Stefan Weinhuber22825ab2009-12-07 12:51:48 +0100152 rc = 0;
153 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700154 if (rc)
Stefan Haberlandea058542009-12-18 17:43:16 +0100155 pr_warning("%s: DIAG ERP failed with "
156 "rc=%d\n", dev_name(&device->cdev->dev), rc);
Horst Hummelfd49f412005-09-03 15:58:00 -0700157}
158
159/* Start a given request at the device. Return zero on success, non-zero
160 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int
162dasd_start_diag(struct dasd_ccw_req * cqr)
163{
164 struct dasd_device *device;
165 struct dasd_diag_private *private;
166 struct dasd_diag_req *dreq;
167 int rc;
168
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100169 device = cqr->startdev;
Horst Hummelfd49f412005-09-03 15:58:00 -0700170 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100171 DBF_DEV_EVENT(DBF_ERR, device, "DIAG start_IO: request %p "
Horst Hummelfd49f412005-09-03 15:58:00 -0700172 "- no retry left)", cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100173 cqr->status = DASD_CQR_ERROR;
Horst Hummelfd49f412005-09-03 15:58:00 -0700174 return -EIO;
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 private = (struct dasd_diag_private *) device->private;
177 dreq = (struct dasd_diag_req *) cqr->data;
178
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200179 private->iob.dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 private->iob.key = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700181 private->iob.flags = DASD_DIAG_RWFLAG_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 private->iob.block_count = dreq->block_count;
Horst Hummelfd49f412005-09-03 15:58:00 -0700183 private->iob.interrupt_params = (addr_t) cqr;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800184 private->iob.bio_list = dreq->bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700185 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 cqr->startclk = get_clock();
Horst Hummelfd49f412005-09-03 15:58:00 -0700188 cqr->starttime = jiffies;
189 cqr->retries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 rc = dia250(&private->iob, RW_BIO);
Horst Hummelfd49f412005-09-03 15:58:00 -0700192 switch (rc) {
193 case 0: /* Synchronous I/O finished successfully */
194 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195 cqr->status = DASD_CQR_SUCCESS;
Horst Hummelfd49f412005-09-03 15:58:00 -0700196 /* Indicate to calling function that only a dasd_schedule_bh()
197 and no timer is needed */
198 rc = -EACCES;
199 break;
200 case 8: /* Asynchronous I/O was started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 cqr->status = DASD_CQR_IN_IO;
202 rc = 0;
Horst Hummelfd49f412005-09-03 15:58:00 -0700203 break;
204 default: /* Error condition */
205 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100206 DBF_DEV_EVENT(DBF_WARNING, device, "dia250 returned rc=%d", rc);
Horst Hummelfd49f412005-09-03 15:58:00 -0700207 dasd_diag_erp(device);
208 rc = -EIO;
209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200211 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return rc;
213}
214
Horst Hummelfd49f412005-09-03 15:58:00 -0700215/* Terminate given request at the device. */
216static int
217dasd_diag_term_IO(struct dasd_ccw_req * cqr)
218{
219 struct dasd_device *device;
220
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100221 device = cqr->startdev;
Horst Hummelfd49f412005-09-03 15:58:00 -0700222 mdsk_term_io(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100223 mdsk_init_io(device, device->block->bp_block, 0, NULL);
224 cqr->status = DASD_CQR_CLEAR_PENDING;
Horst Hummelfd49f412005-09-03 15:58:00 -0700225 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100226 dasd_schedule_device_bh(device);
Horst Hummelfd49f412005-09-03 15:58:00 -0700227 return 0;
228}
229
230/* Handle external interruption. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void
Heiko Carstens5a489b982006-10-06 16:38:35 +0200232dasd_ext_handler(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct dasd_ccw_req *cqr, *next;
235 struct dasd_device *device;
236 unsigned long long expires;
237 unsigned long flags;
Horst Hummelfd49f412005-09-03 15:58:00 -0700238 u8 int_code, status;
239 addr_t ip;
240 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Horst Hummelfd49f412005-09-03 15:58:00 -0700242 int_code = *((u8 *) DASD_DIAG_LC_INT_CODE);
243 status = *((u8 *) DASD_DIAG_LC_INT_STATUS);
244 switch (int_code) {
245 case DASD_DIAG_CODE_31BIT:
246 ip = (addr_t) *((u32 *) DASD_DIAG_LC_INT_PARM_31BIT);
247 break;
248 case DASD_DIAG_CODE_64BIT:
249 ip = (addr_t) *((u64 *) DASD_DIAG_LC_INT_PARM_64BIT);
250 break;
251 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return;
Horst Hummelfd49f412005-09-03 15:58:00 -0700253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!ip) { /* no intparm: unsolicited interrupt */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100255 DBF_EVENT(DBF_NOTICE, "%s", "caught unsolicited "
256 "interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return;
258 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700259 cqr = (struct dasd_ccw_req *) ip;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100260 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100262 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 " magic number of dasd_ccw_req 0x%08X doesn't"
264 " match discipline 0x%08X",
265 cqr->magic, *(int *) (&device->discipline->name));
266 return;
267 }
268
269 /* get irq lock to modify request queue */
270 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
271
Horst Hummelfd49f412005-09-03 15:58:00 -0700272 /* Check for a pending clear operation */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100273 if (cqr->status == DASD_CQR_CLEAR_PENDING) {
274 cqr->status = DASD_CQR_CLEARED;
275 dasd_device_clear_timer(device);
276 dasd_schedule_device_bh(device);
Horst Hummelfd49f412005-09-03 15:58:00 -0700277 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
278 return;
279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cqr->stopclk = get_clock();
282
283 expires = 0;
284 if (status == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100285 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Start first request on queue if possible -> fast_io. */
287 if (!list_empty(&device->ccw_queue)) {
288 next = list_entry(device->ccw_queue.next,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (next->status == DASD_CQR_QUEUED) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700291 rc = dasd_start_diag(next);
292 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700296 } else {
297 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100298 DBF_DEV_EVENT(DBF_DEBUG, device, "interrupt status for "
Horst Hummelfd49f412005-09-03 15:58:00 -0700299 "request %p was %d (%d retries left)", cqr, status,
300 cqr->retries);
301 dasd_diag_erp(device);
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100305 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100307 dasd_device_clear_timer(device);
308 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
311}
312
Horst Hummelfd49f412005-09-03 15:58:00 -0700313/* Check whether device can be controlled by DIAG discipline. Return zero on
314 * success, non-zero otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static int
316dasd_diag_check_device(struct dasd_device *device)
317{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100318 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct dasd_diag_private *private;
320 struct dasd_diag_characteristics *rdc_data;
321 struct dasd_diag_bio bio;
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800322 struct vtoc_cms_label *label;
Horst Hummelfd49f412005-09-03 15:58:00 -0700323 blocknum_t end_block;
324 unsigned int sb, bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int rc;
326
327 private = (struct dasd_diag_private *) device->private;
328 if (private == NULL) {
Horst Hummel138c0142006-06-29 14:58:12 +0200329 private = kzalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (private == NULL) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100331 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
332 "Allocating memory for private DASD data "
333 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -ENOMEM;
335 }
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200336 ccw_device_get_id(device->cdev, &private->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 device->private = (void *) private;
338 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100339 block = dasd_alloc_block();
340 if (IS_ERR(block)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100341 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100342 "could not allocate dasd block structure");
Cornelia Huck73371942008-07-17 17:16:43 +0200343 device->private = NULL;
344 kfree(private);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 return PTR_ERR(block);
346 }
347 device->block = block;
348 block->base = device;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* Read Device Characteristics */
351 rdc_data = (void *) &(private->rdc_data);
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200352 rdc_data->dev_nr = private->dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
354
355 rc = diag210((struct diag210 *) rdc_data);
Horst Hummelfd49f412005-09-03 15:58:00 -0700356 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100357 DBF_DEV_EVENT(DBF_WARNING, device, "failed to retrieve device "
Horst Hummelfd49f412005-09-03 15:58:00 -0700358 "information (rc=%d)", rc);
Stefan Haberland8586cb62008-07-17 17:16:49 +0200359 rc = -EOPNOTSUPP;
Cornelia Huck73371942008-07-17 17:16:43 +0200360 goto out;
Horst Hummelfd49f412005-09-03 15:58:00 -0700361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Figure out position of label block */
364 switch (private->rdc_data.vdev_class) {
365 case DEV_CLASS_FBA:
366 private->pt_block = 1;
367 break;
368 case DEV_CLASS_ECKD:
369 private->pt_block = 2;
370 break;
371 default:
Stefan Haberlandea058542009-12-18 17:43:16 +0100372 pr_warning("%s: Device type %d is not supported "
373 "in DIAG mode\n", dev_name(&device->cdev->dev),
374 private->rdc_data.vdev_class);
Stefan Haberland8586cb62008-07-17 17:16:49 +0200375 rc = -EOPNOTSUPP;
Cornelia Huck73371942008-07-17 17:16:43 +0200376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 DBF_DEV_EVENT(DBF_INFO, device,
380 "%04X: %04X on real %04X/%02X",
381 rdc_data->dev_nr,
382 rdc_data->vdev_type,
383 rdc_data->rdev_type, rdc_data->rdev_model);
384
385 /* terminate all outstanding operations */
386 mdsk_term_io(device);
387
388 /* figure out blocksize of device */
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800389 label = (struct vtoc_cms_label *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (label == NULL) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100391 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 "No memory to allocate initialization request");
Cornelia Huck73371942008-07-17 17:16:43 +0200393 rc = -ENOMEM;
394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700396 rc = 0;
397 end_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* try all sizes - needed for ECKD devices */
399 for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
Horst Hummelfd49f412005-09-03 15:58:00 -0700400 mdsk_init_io(device, bsize, 0, &end_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 memset(&bio, 0, sizeof (struct dasd_diag_bio));
402 bio.type = MDSK_READ_REQ;
403 bio.block_number = private->pt_block + 1;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800404 bio.buffer = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
406 private->iob.dev_nr = rdc_data->dev_nr;
407 private->iob.key = 0;
408 private->iob.flags = 0; /* do synchronous io */
409 private->iob.block_count = 1;
410 private->iob.interrupt_params = 0;
Peter Oberparleiter86b368a2005-11-07 00:59:08 -0800411 private->iob.bio_list = &bio;
Horst Hummelfd49f412005-09-03 15:58:00 -0700412 private->iob.flaga = DASD_DIAG_FLAGA_DEFAULT;
413 rc = dia250(&private->iob, RW_BIO);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800414 if (rc == 3) {
Stefan Haberlandea058542009-12-18 17:43:16 +0100415 pr_warning("%s: A 64-bit DIAG call failed\n",
416 dev_name(&device->cdev->dev));
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800417 rc = -EOPNOTSUPP;
Cornelia Huck73371942008-07-17 17:16:43 +0200418 goto out_label;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mdsk_term_io(device);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800421 if (rc == 0)
422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800424 if (bsize > PAGE_SIZE) {
Stefan Haberlandea058542009-12-18 17:43:16 +0100425 pr_warning("%s: Accessing the DASD failed because of an "
426 "incorrect format (rc=%d)\n",
427 dev_name(&device->cdev->dev), rc);
Horst Hummelfd49f412005-09-03 15:58:00 -0700428 rc = -EIO;
Cornelia Huck73371942008-07-17 17:16:43 +0200429 goto out_label;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800430 }
431 /* check for label block */
432 if (memcmp(label->label_id, DASD_DIAG_CMS1,
433 sizeof(DASD_DIAG_CMS1)) == 0) {
434 /* get formatted blocksize from label block */
435 bsize = (unsigned int) label->block_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100436 block->blocks = (unsigned long) label->block_count;
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800437 } else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100438 block->blocks = end_block;
439 block->bp_block = bsize;
440 block->s2b_shift = 0; /* bits to shift 512 to get a block */
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800441 for (sb = 512; sb < bsize; sb = sb << 1)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100442 block->s2b_shift++;
443 rc = mdsk_init_io(device, block->bp_block, 0, NULL);
Stefan Weinhuber22825ab2009-12-07 12:51:48 +0100444 if (rc && (rc != 4)) {
Stefan Haberlandea058542009-12-18 17:43:16 +0100445 pr_warning("%s: DIAG initialization failed with rc=%d\n",
446 dev_name(&device->cdev->dev), rc);
Peter Oberparleiter1e0291b2005-11-07 00:59:08 -0800447 rc = -EIO;
Horst Hummelfd49f412005-09-03 15:58:00 -0700448 } else {
Stefan Weinhuber22825ab2009-12-07 12:51:48 +0100449 if (rc == 4)
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100450 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
Stefan Haberlandea058542009-12-18 17:43:16 +0100451 pr_info("%s: New DASD with %ld byte/block, total size %ld "
452 "KB%s\n", dev_name(&device->cdev->dev),
453 (unsigned long) block->bp_block,
454 (unsigned long) (block->blocks <<
455 block->s2b_shift) >> 1,
456 (rc == 4) ? ", read-only device" : "");
Stefan Weinhuber22825ab2009-12-07 12:51:48 +0100457 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Cornelia Huck73371942008-07-17 17:16:43 +0200459out_label:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 free_page((long) label);
Cornelia Huck73371942008-07-17 17:16:43 +0200461out:
462 if (rc) {
463 device->block = NULL;
464 dasd_free_block(block);
465 device->private = NULL;
466 kfree(private);
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return rc;
469}
470
Horst Hummelfd49f412005-09-03 15:58:00 -0700471/* Fill in virtual disk geometry for device. Return zero on success, non-zero
472 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static int
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100474dasd_diag_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100476 if (dasd_check_blocksize(block->bp_block) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100478 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 geo->heads = 16;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100480 geo->sectors = 128 >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static dasd_erp_fn_t
485dasd_diag_erp_action(struct dasd_ccw_req * cqr)
486{
487 return dasd_default_erp_action;
488}
489
490static dasd_erp_fn_t
491dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
492{
493 return dasd_default_erp_postaction;
494}
495
Horst Hummelfd49f412005-09-03 15:58:00 -0700496/* Create DASD request from block device request. Return pointer to new
497 * request on success, ERR_PTR otherwise. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100498static struct dasd_ccw_req *dasd_diag_build_cp(struct dasd_device *memdev,
499 struct dasd_block *block,
500 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct dasd_ccw_req *cqr;
503 struct dasd_diag_req *dreq;
504 struct dasd_diag_bio *dbio;
NeilBrown5705f702007-09-25 12:35:59 +0200505 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct bio_vec *bv;
507 char *dst;
Horst Hummelfd49f412005-09-03 15:58:00 -0700508 unsigned int count, datasize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sector_t recid, first_rec, last_rec;
Horst Hummelfd49f412005-09-03 15:58:00 -0700510 unsigned int blksize, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned char rw_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (rq_data_dir(req) == READ)
514 rw_cmd = MDSK_READ_REQ;
515 else if (rq_data_dir(req) == WRITE)
516 rw_cmd = MDSK_WRITE_REQ;
517 else
518 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100519 blksize = block->bp_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Calculate record id of first and last block. */
Tejun Heo83096eb2009-05-07 22:24:39 +0900521 first_rec = blk_rq_pos(req) >> block->s2b_shift;
522 last_rec =
523 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Check struct bio and count the number of blocks for the request. */
525 count = 0;
NeilBrown5705f702007-09-25 12:35:59 +0200526 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200527 if (bv->bv_len & (blksize - 1))
528 /* Fba can only do full blocks. */
529 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100530 count += bv->bv_len >> (block->s2b_shift + 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 /* Paranoia. */
533 if (count != last_rec - first_rec + 1)
534 return ERR_PTR(-EINVAL);
535 /* Build the request */
536 datasize = sizeof(struct dasd_diag_req) +
537 count*sizeof(struct dasd_diag_bio);
Stefan Haberland68b781f2009-09-11 10:28:29 +0200538 cqr = dasd_smalloc_request(DASD_DIAG_MAGIC, 0, datasize, memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (IS_ERR(cqr))
540 return cqr;
Horst Hummel138c0142006-06-29 14:58:12 +0200541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 dreq = (struct dasd_diag_req *) cqr->data;
543 dreq->block_count = count;
544 dbio = dreq->bio;
545 recid = first_rec;
NeilBrown5705f702007-09-25 12:35:59 +0200546 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200547 dst = page_address(bv->bv_page) + bv->bv_offset;
548 for (off = 0; off < bv->bv_len; off += blksize) {
549 memset(dbio, 0, sizeof (struct dasd_diag_bio));
550 dbio->type = rw_cmd;
551 dbio->block_number = recid + 1;
552 dbio->buffer = dst;
553 dbio++;
554 dst += blksize;
555 recid++;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Horst Hummelfd49f412005-09-03 15:58:00 -0700558 cqr->retries = DIAG_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 cqr->buildclk = get_clock();
Holger Smolinski13de2272009-01-09 12:14:51 +0100560 if (blk_noretry_request(req) ||
561 block->base->features & DASD_FEATURE_FAILFAST)
Horst Hummel1c01b8a2006-01-06 00:19:15 -0800562 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100563 cqr->startdev = memdev;
564 cqr->memdev = memdev;
565 cqr->block = block;
Horst Hummelfd49f412005-09-03 15:58:00 -0700566 cqr->expires = DIAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 cqr->status = DASD_CQR_FILLED;
568 return cqr;
569}
570
Horst Hummelfd49f412005-09-03 15:58:00 -0700571/* Release DASD request. Return non-zero if request was successful, zero
572 * otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static int
574dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
575{
576 int status;
577
578 status = cqr->status == DASD_CQR_DONE;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100579 dasd_sfree_request(cqr, cqr->memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return status;
581}
582
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100583static void dasd_diag_handle_terminated_request(struct dasd_ccw_req *cqr)
584{
585 cqr->status = DASD_CQR_FILLED;
586};
587
Horst Hummelfd49f412005-09-03 15:58:00 -0700588/* Fill in IOCTL data for device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589static int
590dasd_diag_fill_info(struct dasd_device * device,
591 struct dasd_information2_t * info)
592{
593 struct dasd_diag_private *private;
594
595 private = (struct dasd_diag_private *) device->private;
Horst Hummelfd49f412005-09-03 15:58:00 -0700596 info->label_block = (unsigned int) private->pt_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 info->FBA_layout = 1;
598 info->format = DASD_FORMAT_LDL;
599 info->characteristics_size = sizeof (struct dasd_diag_characteristics);
600 memcpy(info->characteristics,
601 &((struct dasd_diag_private *) device->private)->rdc_data,
602 sizeof (struct dasd_diag_characteristics));
603 info->confdata_size = 0;
604 return 0;
605}
606
607static void
608dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
609 struct irb *stat)
610{
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100611 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 "dump sense not available for DIAG data");
613}
614
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100615static struct dasd_discipline dasd_diag_discipline = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 .owner = THIS_MODULE,
617 .name = "DIAG",
618 .ebcname = "DIAG",
Horst Hummelfd49f412005-09-03 15:58:00 -0700619 .max_blocks = DIAG_MAX_BLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 .check_device = dasd_diag_check_device,
621 .fill_geometry = dasd_diag_fill_geometry,
622 .start_IO = dasd_start_diag,
Horst Hummelfd49f412005-09-03 15:58:00 -0700623 .term_IO = dasd_diag_term_IO,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100624 .handle_terminated_request = dasd_diag_handle_terminated_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 .erp_action = dasd_diag_erp_action,
626 .erp_postaction = dasd_diag_erp_postaction,
627 .build_cp = dasd_diag_build_cp,
628 .free_cp = dasd_diag_free_cp,
629 .dump_sense = dasd_diag_dump_sense,
630 .fill_info = dasd_diag_fill_info,
631};
632
633static int __init
634dasd_diag_init(void)
635{
636 if (!MACHINE_IS_VM) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100637 pr_info("Discipline %s cannot be used without z/VM\n",
638 dasd_diag_discipline.name);
Horst Hummelfd49f412005-09-03 15:58:00 -0700639 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 ASCEBC(dasd_diag_discipline.ebcname, 4);
642
643 ctl_set_bit(0, 9);
644 register_external_interrupt(0x2603, dasd_ext_handler);
645 dasd_diag_discipline_pointer = &dasd_diag_discipline;
646 return 0;
647}
648
649static void __exit
650dasd_diag_cleanup(void)
651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unregister_external_interrupt(0x2603, dasd_ext_handler);
653 ctl_clear_bit(0, 9);
654 dasd_diag_discipline_pointer = NULL;
655}
656
657module_init(dasd_diag_init);
658module_exit(dasd_diag_cleanup);