Merge branch 'master' of /home/sam/kernel/linux-2.6/
[linux-drm-fsl-dcu.git] / drivers / scsi / pluto.c
1 /* pluto.c: SparcSTORAGE Array SCSI host adapter driver.
2  *
3  * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
4  *
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/delay.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/blkdev.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.h>
15 #include <linux/init.h>
16 #ifdef CONFIG_KMOD
17 #include <linux/kmod.h>
18 #endif
19
20 #include <asm/irq.h>
21
22 #include "scsi.h"
23 #include <scsi/scsi_host.h>
24 #include "../fc4/fcp_impl.h"
25 #include "pluto.h"
26
27 #include <linux/module.h>
28
29 #define RQ_SCSI_BUSY            0xffff
30 #define RQ_SCSI_DONE            0xfffe
31
32 /* #define PLUTO_DEBUG */
33
34 #define pluto_printk printk ("PLUTO %s: ", fc->name); printk
35
36 #ifdef PLUTO_DEBUG
37 #define PLD(x)  pluto_printk x;
38 #define PLND(x) printk ("PLUTO: "); printk x;
39 #else
40 #define PLD(x)
41 #define PLND(x)
42 #endif
43
44 static struct ctrl_inquiry {
45         struct Scsi_Host host;
46         struct pluto pluto;
47         Scsi_Cmnd cmd;
48         char inquiry[256];
49         fc_channel *fc;
50 } *fcs __initdata;
51 static int fcscount __initdata = 0;
52 static atomic_t fcss __initdata = ATOMIC_INIT(0);
53 DECLARE_MUTEX_LOCKED(fc_sem);
54
55 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd);
56
57 static void __init pluto_detect_timeout(unsigned long data)
58 {
59         PLND(("Timeout\n"))
60         up(&fc_sem);
61 }
62
63 static void __init pluto_detect_done(Scsi_Cmnd *SCpnt)
64 {
65         /* Do nothing */
66 }
67
68 static void __init pluto_detect_scsi_done(Scsi_Cmnd *SCpnt)
69 {
70         SCpnt->request->rq_status = RQ_SCSI_DONE;
71         PLND(("Detect done %08lx\n", (long)SCpnt))
72         if (atomic_dec_and_test (&fcss))
73                 up(&fc_sem);
74 }
75
76 int pluto_slave_configure(struct scsi_device *device)
77 {
78         int depth_to_use;
79
80         if (device->tagged_supported)
81                 depth_to_use = /* 254 */ 8;
82         else
83                 depth_to_use = 2;
84
85         scsi_adjust_queue_depth(device,
86                                 (device->tagged_supported ?
87                                  MSG_SIMPLE_TAG : 0),
88                                 depth_to_use);
89
90         return 0;
91 }
92
93 /* Detect all SSAs attached to the machine.
94    To be fast, do it on all online FC channels at the same time. */
95 int __init pluto_detect(struct scsi_host_template *tpnt)
96 {
97         int i, retry, nplutos;
98         fc_channel *fc;
99         struct scsi_device dev;
100         DEFINE_TIMER(fc_timer, pluto_detect_timeout, 0, 0);
101
102         tpnt->proc_name = "pluto";
103         fcscount = 0;
104         for_each_online_fc_channel(fc) {
105                 if (!fc->posmap)
106                         fcscount++;
107         }
108         PLND(("%d channels online\n", fcscount))
109         if (!fcscount) {
110 #if defined(MODULE) && defined(CONFIG_FC4_SOC_MODULE) && defined(CONFIG_KMOD)
111                 request_module("soc");
112                 
113                 for_each_online_fc_channel(fc) {
114                         if (!fc->posmap)
115                                 fcscount++;
116                 }
117                 if (!fcscount)
118 #endif
119                         return 0;
120         }
121         fcs = (struct ctrl_inquiry *) kmalloc (sizeof (struct ctrl_inquiry) * fcscount, GFP_DMA);
122         if (!fcs) {
123                 printk ("PLUTO: Not enough memory to probe\n");
124                 return 0;
125         }
126         
127         memset (fcs, 0, sizeof (struct ctrl_inquiry) * fcscount);
128         memset (&dev, 0, sizeof(dev));
129         atomic_set (&fcss, fcscount);
130         
131         i = 0;
132         for_each_online_fc_channel(fc) {
133                 Scsi_Cmnd *SCpnt;
134                 struct Scsi_Host *host;
135                 struct pluto *pluto;
136
137                 if (i == fcscount) break;
138                 if (fc->posmap) continue;
139                 
140                 PLD(("trying to find SSA\n"))
141
142                 /* If this is already registered to some other SCSI host, then it cannot be pluto */
143                 if (fc->scsi_name[0]) continue;
144                 memcpy (fc->scsi_name, "SSA", 4);
145                 
146                 fcs[i].fc = fc;
147                 
148                 fc->can_queue = PLUTO_CAN_QUEUE;
149                 fc->rsp_size = 64;
150                 fc->encode_addr = pluto_encode_addr;
151                 
152                 fc->fcp_register(fc, TYPE_SCSI_FCP, 0);
153         
154                 SCpnt = &(fcs[i].cmd);
155                 host = &(fcs[i].host);
156                 pluto = (struct pluto *)host->hostdata;
157                 
158                 pluto->fc = fc;
159         
160                 SCpnt->cmnd[0] = INQUIRY;
161                 SCpnt->cmnd[4] = 255;
162                 
163                 /* FC layer requires this, so that SCpnt->device->tagged_supported is initially 0 */
164                 SCpnt->device = &dev;
165                 dev.host = host;
166                 
167                 SCpnt->cmd_len = COMMAND_SIZE(INQUIRY);
168         
169                 SCpnt->request->rq_status = RQ_SCSI_BUSY;
170                 
171                 SCpnt->done = pluto_detect_done;
172                 SCpnt->bufflen = 256;
173                 SCpnt->buffer = fcs[i].inquiry;
174                 SCpnt->request_bufflen = 256;
175                 SCpnt->request_buffer = fcs[i].inquiry;
176                 PLD(("set up %d %08lx\n", i, (long)SCpnt))
177                 i++;
178         }
179         
180         for (retry = 0; retry < 5; retry++) {
181                 for (i = 0; i < fcscount; i++) {
182                         if (!fcs[i].fc) break;
183                         if (fcs[i].cmd.request->rq_status != RQ_SCSI_DONE) {
184                                 disable_irq(fcs[i].fc->irq);
185                                 PLND(("queuecommand %d %d\n", retry, i))
186                                 fcp_scsi_queuecommand (&(fcs[i].cmd), 
187                                         pluto_detect_scsi_done);
188                                 enable_irq(fcs[i].fc->irq);
189                         }
190                 }
191             
192                 fc_timer.expires = jiffies + 10 * HZ;
193                 add_timer(&fc_timer);
194                 
195                 down(&fc_sem);
196                 PLND(("Woken up\n"))
197                 if (!atomic_read(&fcss))
198                         break; /* All fc channels have answered us */
199         }
200         del_timer_sync(&fc_timer);
201
202         PLND(("Finished search\n"))
203         for (i = 0, nplutos = 0; i < fcscount; i++) {
204                 Scsi_Cmnd *SCpnt;
205                 
206                 if (!(fc = fcs[i].fc)) break;
207         
208                 SCpnt = &(fcs[i].cmd);
209                 
210                 /* Let FC mid-level free allocated resources */
211                 SCpnt->done (SCpnt);
212                 
213                 if (!SCpnt->result) {
214                         struct pluto_inquiry *inq;
215                         struct pluto *pluto;
216                         struct Scsi_Host *host;
217                         
218                         inq = (struct pluto_inquiry *)fcs[i].inquiry;
219
220                         if ((inq->dtype & 0x1f) == TYPE_PROCESSOR &&
221                             !strncmp (inq->vendor_id, "SUN", 3) &&
222                             !strncmp (inq->product_id, "SSA", 3)) {
223                                 char *p;
224                                 long *ages;
225                                 
226                                 ages = kmalloc (((inq->channels + 1) * inq->targets) * sizeof(long), GFP_KERNEL);
227                                 if (!ages) continue;
228                                 
229                                 host = scsi_register (tpnt, sizeof (struct pluto));
230                                 if(!host)
231                                 {
232                                         kfree(ages);
233                                         continue;
234                                 }
235                                 
236                                 if (!try_module_get(fc->module)) {
237                                         kfree(ages);
238                                         scsi_unregister(host);
239                                         continue;
240                                 }
241
242                                 nplutos++;
243                                 
244                                 pluto = (struct pluto *)host->hostdata;
245                                 
246                                 host->max_id = inq->targets;
247                                 host->max_channel = inq->channels;
248                                 host->irq = fc->irq;
249
250                                 fc->channels = inq->channels + 1;
251                                 fc->targets = inq->targets;
252                                 fc->ages = ages;
253                                 memset (ages, 0, ((inq->channels + 1) * inq->targets) * sizeof(long));
254                                 
255                                 pluto->fc = fc;
256                                 memcpy (pluto->rev_str, inq->revision, 4);
257                                 pluto->rev_str[4] = 0;
258                                 p = strchr (pluto->rev_str, ' ');
259                                 if (p) *p = 0;
260                                 memcpy (pluto->fw_rev_str, inq->fw_revision, 4);
261                                 pluto->fw_rev_str[4] = 0;
262                                 p = strchr (pluto->fw_rev_str, ' ');
263                                 if (p) *p = 0;
264                                 memcpy (pluto->serial_str, inq->serial, 12);
265                                 pluto->serial_str[12] = 0;
266                                 p = strchr (pluto->serial_str, ' ');
267                                 if (p) *p = 0;
268                                 
269                                 PLD(("Found SSA rev %s fw rev %s serial %s %dx%d\n", pluto->rev_str, pluto->fw_rev_str, pluto->serial_str, host->max_channel, host->max_id))
270                         } else
271                                 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
272                 } else
273                         fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
274         }
275         kfree((char *)fcs);
276         if (nplutos)
277                 printk ("PLUTO: Total of %d SparcSTORAGE Arrays found\n", nplutos);
278         return nplutos;
279 }
280
281 int pluto_release(struct Scsi_Host *host)
282 {
283         struct pluto *pluto = (struct pluto *)host->hostdata;
284         fc_channel *fc = pluto->fc;
285
286         module_put(fc->module);
287         
288         fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
289         PLND((" releasing pluto.\n"));
290         kfree (fc->ages);
291         PLND(("released pluto!\n"));
292         return 0;
293 }
294
295 const char *pluto_info(struct Scsi_Host *host)
296 {
297         static char buf[128], *p;
298         struct pluto *pluto = (struct pluto *) host->hostdata;
299
300         sprintf(buf, "SUN SparcSTORAGE Array %s fw %s serial %s %dx%d on %s",
301                 pluto->rev_str, pluto->fw_rev_str, pluto->serial_str,
302                 host->max_channel, host->max_id, pluto->fc->name);
303 #ifdef __sparc__
304         p = strchr(buf, 0);
305         sprintf(p, " PROM node %x", pluto->fc->dev->prom_node);
306 #endif  
307         return buf;
308 }
309
310 /* SSA uses this FC4S addressing:
311    switch (addr[0])
312    {
313    case 0: CONTROLLER - All of addr[1]..addr[3] has to be 0
314    case 1: SINGLE DISK - addr[1] channel, addr[2] id, addr[3] 0
315    case 2: DISK GROUP - ???
316    }
317    
318    So that SCSI mid-layer can access to these, we reserve
319    channel 0 id 0 lun 0 for CONTROLLER
320    and channels 1 .. max_channel are normal single disks.
321  */
322 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd)
323 {
324         PLND(("encode addr %d %d %d\n", SCpnt->device->channel, SCpnt->device->id, SCpnt->cmnd[1] & 0xe0))
325         /* We don't support LUNs - neither does SSA :) */
326         if (SCpnt->cmnd[1] & 0xe0)
327                 return -EINVAL;
328         if (!SCpnt->device->channel) {
329                 if (SCpnt->device->id)
330                         return -EINVAL;
331                 memset (addr, 0, 4 * sizeof(u16));
332         } else {
333                 addr[0] = 1;
334                 addr[1] = SCpnt->device->channel - 1;
335                 addr[2] = SCpnt->device->id;
336                 addr[3] = 0;
337         }
338         /* We're Point-to-Point, so target it to the default DID */
339         fcmd->did = fc->did;
340         PLND(("trying %04x%04x%04x%04x\n", addr[0], addr[1], addr[2], addr[3]))
341         return 0;
342 }
343
344 static struct scsi_host_template driver_template = {
345         .name                   = "Sparc Storage Array 100/200",
346         .detect                 = pluto_detect,
347         .release                = pluto_release,
348         .info                   = pluto_info,
349         .queuecommand           = fcp_scsi_queuecommand,
350         .slave_configure        = pluto_slave_configure,
351         .can_queue              = PLUTO_CAN_QUEUE,
352         .this_id                = -1,
353         .sg_tablesize           = 1,
354         .cmd_per_lun            = 1,
355         .use_clustering         = ENABLE_CLUSTERING,
356         .eh_abort_handler       = fcp_scsi_abort,
357         .eh_device_reset_handler = fcp_scsi_dev_reset,
358         .eh_host_reset_handler  = fcp_scsi_host_reset,
359 };
360
361 #include "scsi_module.c"
362
363 MODULE_LICENSE("GPL");
364