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