blob: 1bdaa614e34fef0dbebe6d6f5f03aa7de7ed25a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device_id.c
3 *
4 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
5 * IBM Corporation
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Martin Schwidefsky (schwidefsky@de.ibm.com)
8 *
9 * Sense ID functions.
10 */
11
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
Ahmed S. Darwish045236a2007-02-12 15:49:51 +010014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/ccwdev.h>
17#include <asm/delay.h>
18#include <asm/cio.h>
19#include <asm/lowcore.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020020#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "cio.h"
23#include "cio_debug.h"
24#include "css.h"
25#include "device.h"
26#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010027#include "io_sch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +010029/**
30 * vm_vdev_to_cu_type - Convert vm virtual device into control unit type
31 * for certain devices.
32 * @class: virtual device class
33 * @type: virtual device type
34 *
35 * Returns control unit type if a match was made or %0xffff otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +010037static int vm_vdev_to_cu_type(int class, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 static struct {
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +010040 int class, type, cu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 } vm_devices[] = {
42 { 0x08, 0x01, 0x3480 },
43 { 0x08, 0x02, 0x3430 },
44 { 0x08, 0x10, 0x3420 },
45 { 0x08, 0x42, 0x3424 },
46 { 0x08, 0x44, 0x9348 },
47 { 0x08, 0x81, 0x3490 },
48 { 0x08, 0x82, 0x3422 },
49 { 0x10, 0x41, 0x1403 },
50 { 0x10, 0x42, 0x3211 },
51 { 0x10, 0x43, 0x3203 },
52 { 0x10, 0x45, 0x3800 },
53 { 0x10, 0x47, 0x3262 },
54 { 0x10, 0x48, 0x3820 },
55 { 0x10, 0x49, 0x3800 },
56 { 0x10, 0x4a, 0x4245 },
57 { 0x10, 0x4b, 0x4248 },
58 { 0x10, 0x4d, 0x3800 },
59 { 0x10, 0x4e, 0x3820 },
60 { 0x10, 0x4f, 0x3820 },
61 { 0x10, 0x82, 0x2540 },
62 { 0x10, 0x84, 0x3525 },
63 { 0x20, 0x81, 0x2501 },
64 { 0x20, 0x82, 0x2540 },
65 { 0x20, 0x84, 0x3505 },
66 { 0x40, 0x01, 0x3278 },
67 { 0x40, 0x04, 0x3277 },
68 { 0x40, 0x80, 0x2250 },
69 { 0x40, 0xc0, 0x5080 },
70 { 0x80, 0x00, 0x3215 },
71 };
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +010072 int i;
73
74 for (i = 0; i < ARRAY_SIZE(vm_devices); i++)
75 if (class == vm_devices[i].class && type == vm_devices[i].type)
76 return vm_devices[i].cu_type;
77
78 return 0xffff;
79}
80
81/**
82 * diag_get_dev_info - retrieve device information via DIAG X'210'
83 * @devno: device number
84 * @ps: pointer to sense ID data area
85 *
86 * Returns zero on success, non-zero otherwise.
87 */
88static int diag_get_dev_info(u16 devno, struct senseid *ps)
89{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct diag210 diag_data;
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +010091 int ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 CIO_TRACE_EVENT (4, "VMvdinf");
94
95 diag_data = (struct diag210) {
96 .vrdcdvno = devno,
97 .vrdclen = sizeof (diag_data),
98 };
99
100 ccode = diag210 (&diag_data);
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100101 if ((ccode == 0) || (ccode == 2)) {
102 ps->reserved = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100104 /* Special case for osa devices. */
105 if (diag_data.vrdcvcla == 0x02 && diag_data.vrdcvtyp == 0x20) {
106 ps->cu_type = 0x3088;
107 ps->cu_model = 0x60;
108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100110 ps->cu_type = vm_vdev_to_cu_type(diag_data.vrdcvcla,
111 diag_data.vrdcvtyp);
112 if (ps->cu_type != 0xffff)
113 return 0;
114 }
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 CIO_MSG_EVENT(0, "DIAG X'210' for device %04X returned (cc = %d):"
117 "vdev class : %02X, vdev type : %04X \n ... "
118 "rdev class : %02X, rdev type : %04X, "
119 "rdev model: %02X\n",
120 devno, ccode,
121 diag_data.vrdcvcla, diag_data.vrdcvtyp,
122 diag_data.vrdcrccl, diag_data.vrdccrty,
123 diag_data.vrdccrmd);
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100124
125 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/*
129 * Start Sense ID helper function.
130 * Try to obtain the 'control unit'/'device type' information
131 * associated with the subchannel.
132 */
133static int
134__ccw_device_sense_id_start(struct ccw_device *cdev)
135{
136 struct subchannel *sch;
137 struct ccw1 *ccw;
Cornelia Huckc94dec92007-12-04 16:09:01 +0100138 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 sch = to_subchannel(cdev->dev.parent);
141 /* Setup sense channel program. */
142 ccw = cdev->private->iccws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 ccw->cmd_code = CCW_CMD_SENSE_ID;
144 ccw->cda = (__u32) __pa (&cdev->private->senseid);
145 ccw->count = sizeof (struct senseid);
146 ccw->flags = CCW_FLAG_SLI;
147
148 /* Reset device status. */
149 memset(&cdev->private->irb, 0, sizeof(struct irb));
150
Cornelia Huckc94dec92007-12-04 16:09:01 +0100151 /* Try on every path. */
152 ret = -ENODEV;
153 while (cdev->private->imask != 0) {
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100154 cdev->private->senseid.cu_type = 0xFFFF;
Cornelia Huckc94dec92007-12-04 16:09:01 +0100155 if ((sch->opm & cdev->private->imask) != 0 &&
156 cdev->private->iretry > 0) {
157 cdev->private->iretry--;
158 /* Reset internal retry indication. */
159 cdev->private->flags.intretry = 0;
160 ret = cio_start (sch, cdev->private->iccws,
161 cdev->private->imask);
162 /* ret is 0, -EBUSY, -EACCES or -ENODEV */
163 if (ret != -EACCES)
164 return ret;
165 }
166 cdev->private->imask >>= 1;
167 cdev->private->iretry = 5;
168 }
169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172void
173ccw_device_sense_id_start(struct ccw_device *cdev)
174{
175 int ret;
176
177 memset (&cdev->private->senseid, 0, sizeof (struct senseid));
Cornelia Huckc94dec92007-12-04 16:09:01 +0100178 cdev->private->imask = 0x80;
179 cdev->private->iretry = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ret = __ccw_device_sense_id_start(cdev);
181 if (ret && ret != -EBUSY)
182 ccw_device_sense_id_done(cdev, ret);
183}
184
185/*
186 * Called from interrupt context to check if a valid answer
187 * to Sense ID was received.
188 */
189static int
190ccw_device_check_sense_id(struct ccw_device *cdev)
191{
192 struct subchannel *sch;
193 struct irb *irb;
194
195 sch = to_subchannel(cdev->dev.parent);
196 irb = &cdev->private->irb;
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Check the error cases. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200199 if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100200 /* Retry Sense ID if requested. */
201 if (cdev->private->flags.intretry) {
202 cdev->private->flags.intretry = 0;
203 return -EAGAIN;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -ETIME;
Cornelia Huckd23861f2006-12-04 15:41:04 +0100206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (irb->esw.esw0.erw.cons && (irb->ecw[0] & SNS0_CMD_REJECT)) {
208 /*
209 * if the device doesn't support the SenseID
210 * command further retries wouldn't help ...
211 * NB: We don't check here for intervention required like we
212 * did before, because tape devices with no tape inserted
213 * may present this status *in conjunction with* the
214 * sense id information. So, for intervention required,
215 * we use the "whack it until it talks" strategy...
216 */
Michael Ernst139b83d2008-05-07 09:22:54 +0200217 CIO_MSG_EVENT(0, "SenseID : device %04x on Subchannel "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800218 "0.%x.%04x reports cmd reject\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200219 cdev->private->dev_id.devno, sch->schid.ssid,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800220 sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EOPNOTSUPP;
222 }
223 if (irb->esw.esw0.erw.cons) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800224 CIO_MSG_EVENT(2, "SenseID : UC on dev 0.%x.%04x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 "lpum %02X, cnt %02d, sns :"
226 " %02X%02X%02X%02X %02X%02X%02X%02X ...\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200227 cdev->private->dev_id.ssid,
228 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 irb->esw.esw0.sublog.lpum,
230 irb->esw.esw0.erw.scnt,
231 irb->ecw[0], irb->ecw[1],
232 irb->ecw[2], irb->ecw[3],
233 irb->ecw[4], irb->ecw[5],
234 irb->ecw[6], irb->ecw[7]);
235 return -EAGAIN;
236 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200237 if (irb->scsw.cmd.cc == 3) {
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100238 u8 lpm;
239
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200240 lpm = to_io_private(sch)->orb.cmd.lpm;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100241 if ((lpm & sch->schib.pmcw.pim & sch->schib.pmcw.pam) != 0)
Michael Ernst139b83d2008-05-07 09:22:54 +0200242 CIO_MSG_EVENT(4, "SenseID : path %02X for device %04x "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800243 "on subchannel 0.%x.%04x is "
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100244 "'not operational'\n", lpm,
Cornelia Huck78964262006-10-11 15:31:38 +0200245 cdev->private->dev_id.devno,
246 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EACCES;
248 }
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100249
250 /* Did we get a proper answer ? */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200251 if (irb->scsw.cmd.cc == 0 && cdev->private->senseid.cu_type != 0xFFFF &&
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100252 cdev->private->senseid.reserved == 0xFF) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200253 if (irb->scsw.cmd.count < sizeof(struct senseid) - 8)
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100254 cdev->private->flags.esid = 1;
255 return 0; /* Success */
256 }
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Hmm, whatever happened, try again. */
259 CIO_MSG_EVENT(2, "SenseID : start_IO() for device %04x on "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800260 "subchannel 0.%x.%04x returns status %02X%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200261 cdev->private->dev_id.devno, sch->schid.ssid,
262 sch->schid.sch_no,
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200263 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -EAGAIN;
265}
266
267/*
268 * Got interrupt for Sense ID.
269 */
270void
271ccw_device_sense_id_irq(struct ccw_device *cdev, enum dev_event dev_event)
272{
273 struct subchannel *sch;
274 struct irb *irb;
275 int ret;
276
277 sch = to_subchannel(cdev->dev.parent);
278 irb = (struct irb *) __LC_IRB;
279 /* Retry sense id, if needed. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200280 if (irb->scsw.cmd.stctl ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200282 if ((irb->scsw.cmd.cc == 1) || !irb->scsw.cmd.actl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ret = __ccw_device_sense_id_start(cdev);
284 if (ret && ret != -EBUSY)
285 ccw_device_sense_id_done(cdev, ret);
286 }
287 return;
288 }
289 if (ccw_device_accumulate_and_sense(cdev, irb) != 0)
290 return;
291 ret = ccw_device_check_sense_id(cdev);
292 memset(&cdev->private->irb, 0, sizeof(struct irb));
293 switch (ret) {
294 /* 0, -ETIME, -EOPNOTSUPP, -EAGAIN or -EACCES */
295 case 0: /* Sense id succeeded. */
296 case -ETIME: /* Sense id stopped by timeout. */
297 ccw_device_sense_id_done(cdev, ret);
298 break;
299 case -EACCES: /* channel is not operational. */
Cornelia Huckc94dec92007-12-04 16:09:01 +0100300 sch->lpm &= ~cdev->private->imask;
301 cdev->private->imask >>= 1;
302 cdev->private->iretry = 5;
303 /* fall through. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -EAGAIN: /* try again. */
Cornelia Huckc94dec92007-12-04 16:09:01 +0100305 ret = __ccw_device_sense_id_start(cdev);
306 if (ret == 0 || ret == -EBUSY)
307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* fall through. */
309 default: /* Sense ID failed. Try asking VM. */
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100310 if (MACHINE_IS_VM)
311 ret = diag_get_dev_info(cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 &cdev->private->senseid);
Peter Oberparleiter6f52ac22008-02-05 16:50:33 +0100313 else
314 /*
315 * If we can't couldn't identify the device type we
316 * consider the device "not operational".
317 */
318 ret = -ENODEV;
319
320 ccw_device_sense_id_done(cdev, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
322 }
323}