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