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