Pull ec into release branch
[linux-2.6] / drivers / s390 / cio / device_id.c
1 /*
2  * drivers/s390/cio/device_id.c
3  *
4  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
5  *                       IBM Corporation
6  *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  *
9  * Sense ID functions.
10  */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15
16 #include <asm/ccwdev.h>
17 #include <asm/delay.h>
18 #include <asm/cio.h>
19 #include <asm/lowcore.h>
20
21 #include "cio.h"
22 #include "cio_debug.h"
23 #include "css.h"
24 #include "device.h"
25 #include "ioasm.h"
26
27 /*
28  * diag210 is used under VM to get information about a virtual device
29  */
30 int
31 diag210(struct diag210 * addr)
32 {
33         /*
34          * diag 210 needs its data below the 2GB border, so we
35          * use a static data area to be sure
36          */
37         static struct diag210 diag210_tmp;
38         static DEFINE_SPINLOCK(diag210_lock);
39         unsigned long flags;
40         int ccode;
41
42         spin_lock_irqsave(&diag210_lock, flags);
43         diag210_tmp = *addr;
44
45 #ifdef CONFIG_64BIT
46         asm volatile(
47                 "       lhi     %0,-1\n"
48                 "       sam31\n"
49                 "       diag    %1,0,0x210\n"
50                 "0:     ipm     %0\n"
51                 "       srl     %0,28\n"
52                 "1:     sam64\n"
53                 EX_TABLE(0b,1b)
54                 : "=&d" (ccode) : "a" (&diag210_tmp) : "cc", "memory");
55 #else
56         asm volatile(
57                 "       lhi     %0,-1\n"
58                 "       diag    %1,0,0x210\n"
59                 "0:     ipm     %0\n"
60                 "       srl     %0,28\n"
61                 "1:\n"
62                 EX_TABLE(0b,1b)
63                 : "=&d" (ccode) : "a" (&diag210_tmp) : "cc", "memory");
64 #endif
65
66         *addr = diag210_tmp;
67         spin_unlock_irqrestore(&diag210_lock, flags);
68
69         return ccode;
70 }
71
72 /*
73  * Input :
74  *   devno - device number
75  *   ps    - pointer to sense ID data area
76  * Output : none
77  */
78 static void
79 VM_virtual_device_info (__u16 devno, struct senseid *ps)
80 {
81         static struct {
82                 int vrdcvcla, vrdcvtyp, cu_type;
83         } vm_devices[] = {
84                 { 0x08, 0x01, 0x3480 },
85                 { 0x08, 0x02, 0x3430 },
86                 { 0x08, 0x10, 0x3420 },
87                 { 0x08, 0x42, 0x3424 },
88                 { 0x08, 0x44, 0x9348 },
89                 { 0x08, 0x81, 0x3490 },
90                 { 0x08, 0x82, 0x3422 },
91                 { 0x10, 0x41, 0x1403 },
92                 { 0x10, 0x42, 0x3211 },
93                 { 0x10, 0x43, 0x3203 },
94                 { 0x10, 0x45, 0x3800 },
95                 { 0x10, 0x47, 0x3262 },
96                 { 0x10, 0x48, 0x3820 },
97                 { 0x10, 0x49, 0x3800 },
98                 { 0x10, 0x4a, 0x4245 },
99                 { 0x10, 0x4b, 0x4248 },
100                 { 0x10, 0x4d, 0x3800 },
101                 { 0x10, 0x4e, 0x3820 },
102                 { 0x10, 0x4f, 0x3820 },
103                 { 0x10, 0x82, 0x2540 },
104                 { 0x10, 0x84, 0x3525 },
105                 { 0x20, 0x81, 0x2501 },
106                 { 0x20, 0x82, 0x2540 },
107                 { 0x20, 0x84, 0x3505 },
108                 { 0x40, 0x01, 0x3278 },
109                 { 0x40, 0x04, 0x3277 },
110                 { 0x40, 0x80, 0x2250 },
111                 { 0x40, 0xc0, 0x5080 },
112                 { 0x80, 0x00, 0x3215 },
113         };
114         struct diag210 diag_data;
115         int ccode, i;
116
117         CIO_TRACE_EVENT (4, "VMvdinf");
118
119         diag_data = (struct diag210) {
120                 .vrdcdvno = devno,
121                 .vrdclen = sizeof (diag_data),
122         };
123
124         ccode = diag210 (&diag_data);
125         ps->reserved = 0xff;
126
127         /* Special case for bloody osa devices. */
128         if (diag_data.vrdcvcla == 0x02 &&
129             diag_data.vrdcvtyp == 0x20) {
130                 ps->cu_type = 0x3088;
131                 ps->cu_model = 0x60;
132                 return;
133         }
134         for (i = 0; i < ARRAY_SIZE(vm_devices); i++)
135                 if (diag_data.vrdcvcla == vm_devices[i].vrdcvcla &&
136                     diag_data.vrdcvtyp == vm_devices[i].vrdcvtyp) {
137                         ps->cu_type = vm_devices[i].cu_type;
138                         return;
139                 }
140         CIO_MSG_EVENT(0, "DIAG X'210' for device %04X returned (cc = %d):"
141                       "vdev class : %02X, vdev type : %04X \n ...  "
142                       "rdev class : %02X, rdev type : %04X, "
143                       "rdev model: %02X\n",
144                       devno, ccode,
145                       diag_data.vrdcvcla, diag_data.vrdcvtyp,
146                       diag_data.vrdcrccl, diag_data.vrdccrty,
147                       diag_data.vrdccrmd);
148 }
149
150 /*
151  * Start Sense ID helper function.
152  * Try to obtain the 'control unit'/'device type' information
153  *  associated with the subchannel.
154  */
155 static int
156 __ccw_device_sense_id_start(struct ccw_device *cdev)
157 {
158         struct subchannel *sch;
159         struct ccw1 *ccw;
160         int ret;
161
162         sch = to_subchannel(cdev->dev.parent);
163         /* Setup sense channel program. */
164         ccw = cdev->private->iccws;
165         if (sch->schib.pmcw.pim != 0x80) {
166                 /* more than one path installed. */
167                 ccw->cmd_code = CCW_CMD_SUSPEND_RECONN;
168                 ccw->cda = 0;
169                 ccw->count = 0;
170                 ccw->flags = CCW_FLAG_SLI | CCW_FLAG_CC;
171                 ccw++;
172         }
173         ccw->cmd_code = CCW_CMD_SENSE_ID;
174         ccw->cda = (__u32) __pa (&cdev->private->senseid);
175         ccw->count = sizeof (struct senseid);
176         ccw->flags = CCW_FLAG_SLI;
177
178         /* Reset device status. */
179         memset(&cdev->private->irb, 0, sizeof(struct irb));
180
181         /* Try on every path. */
182         ret = -ENODEV;
183         while (cdev->private->imask != 0) {
184                 if ((sch->opm & cdev->private->imask) != 0 &&
185                     cdev->private->iretry > 0) {
186                         cdev->private->iretry--;
187                         /* Reset internal retry indication. */
188                         cdev->private->flags.intretry = 0;
189                         ret = cio_start (sch, cdev->private->iccws,
190                                          cdev->private->imask);
191                         /* ret is 0, -EBUSY, -EACCES or -ENODEV */
192                         if (ret != -EACCES)
193                                 return ret;
194                 }
195                 cdev->private->imask >>= 1;
196                 cdev->private->iretry = 5;
197         }
198         return ret;
199 }
200
201 void
202 ccw_device_sense_id_start(struct ccw_device *cdev)
203 {
204         int ret;
205
206         memset (&cdev->private->senseid, 0, sizeof (struct senseid));
207         cdev->private->senseid.cu_type = 0xFFFF;
208         cdev->private->imask = 0x80;
209         cdev->private->iretry = 5;
210         ret = __ccw_device_sense_id_start(cdev);
211         if (ret && ret != -EBUSY)
212                 ccw_device_sense_id_done(cdev, ret);
213 }
214
215 /*
216  * Called from interrupt context to check if a valid answer
217  * to Sense ID was received.
218  */
219 static int
220 ccw_device_check_sense_id(struct ccw_device *cdev)
221 {
222         struct subchannel *sch;
223         struct irb *irb;
224
225         sch = to_subchannel(cdev->dev.parent);
226         irb = &cdev->private->irb;
227         /* Did we get a proper answer ? */
228         if (cdev->private->senseid.cu_type != 0xFFFF && 
229             cdev->private->senseid.reserved == 0xFF) {
230                 if (irb->scsw.count < sizeof (struct senseid) - 8)
231                         cdev->private->flags.esid = 1;
232                 return 0; /* Success */
233         }
234         /* Check the error cases. */
235         if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC)) {
236                 /* Retry Sense ID if requested. */
237                 if (cdev->private->flags.intretry) {
238                         cdev->private->flags.intretry = 0;
239                         return -EAGAIN;
240                 }
241                 return -ETIME;
242         }
243         if (irb->esw.esw0.erw.cons && (irb->ecw[0] & SNS0_CMD_REJECT)) {
244                 /*
245                  * if the device doesn't support the SenseID
246                  *  command further retries wouldn't help ...
247                  * NB: We don't check here for intervention required like we
248                  *     did before, because tape devices with no tape inserted
249                  *     may present this status *in conjunction with* the
250                  *     sense id information. So, for intervention required,
251                  *     we use the "whack it until it talks" strategy...
252                  */
253                 CIO_MSG_EVENT(2, "SenseID : device %04x on Subchannel "
254                               "0.%x.%04x reports cmd reject\n",
255                               cdev->private->dev_id.devno, sch->schid.ssid,
256                               sch->schid.sch_no);
257                 return -EOPNOTSUPP;
258         }
259         if (irb->esw.esw0.erw.cons) {
260                 CIO_MSG_EVENT(2, "SenseID : UC on dev 0.%x.%04x, "
261                               "lpum %02X, cnt %02d, sns :"
262                               " %02X%02X%02X%02X %02X%02X%02X%02X ...\n",
263                               cdev->private->dev_id.ssid,
264                               cdev->private->dev_id.devno,
265                               irb->esw.esw0.sublog.lpum,
266                               irb->esw.esw0.erw.scnt,
267                               irb->ecw[0], irb->ecw[1],
268                               irb->ecw[2], irb->ecw[3],
269                               irb->ecw[4], irb->ecw[5],
270                               irb->ecw[6], irb->ecw[7]);
271                 return -EAGAIN;
272         }
273         if (irb->scsw.cc == 3) {
274                 if ((sch->orb.lpm &
275                      sch->schib.pmcw.pim & sch->schib.pmcw.pam) != 0)
276                         CIO_MSG_EVENT(2, "SenseID : path %02X for device %04x "
277                                       "on subchannel 0.%x.%04x is "
278                                       "'not operational'\n", sch->orb.lpm,
279                                       cdev->private->dev_id.devno,
280                                       sch->schid.ssid, sch->schid.sch_no);
281                 return -EACCES;
282         }
283         /* Hmm, whatever happened, try again. */
284         CIO_MSG_EVENT(2, "SenseID : start_IO() for device %04x on "
285                       "subchannel 0.%x.%04x returns status %02X%02X\n",
286                       cdev->private->dev_id.devno, sch->schid.ssid,
287                       sch->schid.sch_no,
288                       irb->scsw.dstat, irb->scsw.cstat);
289         return -EAGAIN;
290 }
291
292 /*
293  * Got interrupt for Sense ID.
294  */
295 void
296 ccw_device_sense_id_irq(struct ccw_device *cdev, enum dev_event dev_event)
297 {
298         struct subchannel *sch;
299         struct irb *irb;
300         int ret;
301
302         sch = to_subchannel(cdev->dev.parent);
303         irb = (struct irb *) __LC_IRB;
304         /* Retry sense id, if needed. */
305         if (irb->scsw.stctl ==
306             (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
307                 if ((irb->scsw.cc == 1) || !irb->scsw.actl) {
308                         ret = __ccw_device_sense_id_start(cdev);
309                         if (ret && ret != -EBUSY)
310                                 ccw_device_sense_id_done(cdev, ret);
311                 }
312                 return;
313         }
314         if (ccw_device_accumulate_and_sense(cdev, irb) != 0)
315                 return;
316         ret = ccw_device_check_sense_id(cdev);
317         memset(&cdev->private->irb, 0, sizeof(struct irb));
318         switch (ret) {
319         /* 0, -ETIME, -EOPNOTSUPP, -EAGAIN or -EACCES */
320         case 0:                 /* Sense id succeeded. */
321         case -ETIME:            /* Sense id stopped by timeout. */
322                 ccw_device_sense_id_done(cdev, ret);
323                 break;
324         case -EACCES:           /* channel is not operational. */
325                 sch->lpm &= ~cdev->private->imask;
326                 cdev->private->imask >>= 1;
327                 cdev->private->iretry = 5;
328                 /* fall through. */
329         case -EAGAIN:           /* try again. */
330                 ret = __ccw_device_sense_id_start(cdev);
331                 if (ret == 0 || ret == -EBUSY)
332                         break;
333                 /* fall through. */
334         default:                /* Sense ID failed. Try asking VM. */
335                 if (MACHINE_IS_VM) {
336                         VM_virtual_device_info (cdev->private->dev_id.devno,
337                                                 &cdev->private->senseid);
338                         if (cdev->private->senseid.cu_type != 0xFFFF) {
339                                 /* Got the device information from VM. */
340                                 ccw_device_sense_id_done(cdev, 0);
341                                 return;
342                         }
343                 }
344                 /*
345                  * If we can't couldn't identify the device type we
346                  *  consider the device "not operational".
347                  */
348                 ccw_device_sense_id_done(cdev, -ENODEV);
349                 break;
350         }
351 }
352
353 EXPORT_SYMBOL(diag210);