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