Linux 2.4.0-test7pre1
[davej-history.git] / drivers / scsi / pluto.c
blobb4ebd88f38e0238b1545848213e4593d587eb791
1 /* pluto.c: SparcSTORAGE Array SCSI host adapter driver.
3 * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 */
7 #include <linux/kernel.h>
8 #include <linux/delay.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/malloc.h>
12 #include <linux/blk.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.h>
15 #include <linux/init.h>
16 #include <linux/config.h>
17 #ifdef CONFIG_KMOD
18 #include <linux/kmod.h>
19 #endif
21 #include <asm/irq.h>
23 #include "scsi.h"
24 #include "hosts.h"
25 #include "../fc4/fcp_impl.h"
26 #include "pluto.h"
28 #include <linux/module.h>
30 /* #define PLUTO_DEBUG */
32 #define pluto_printk printk ("PLUTO %s: ", fc->name); printk
34 #ifdef PLUTO_DEBUG
35 #define PLD(x) pluto_printk x;
36 #define PLND(x) printk ("PLUTO: "); printk x;
37 #else
38 #define PLD(x)
39 #define PLND(x)
40 #endif
42 static struct ctrl_inquiry {
43 struct Scsi_Host host;
44 struct pluto pluto;
45 Scsi_Cmnd cmd;
46 char inquiry[256];
47 fc_channel *fc;
48 } *fcs __initdata = { 0 };
49 static int fcscount __initdata = 0;
50 static atomic_t fcss __initdata = ATOMIC_INIT(0);
51 DECLARE_MUTEX_LOCKED(fc_sem);
53 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd);
55 static void __init pluto_detect_timeout(unsigned long data)
57 PLND(("Timeout\n"))
58 up(&fc_sem);
61 static void __init pluto_detect_done(Scsi_Cmnd *SCpnt)
63 /* Do nothing */
66 static void __init pluto_detect_scsi_done(Scsi_Cmnd *SCpnt)
68 SCpnt->request.rq_status = RQ_SCSI_DONE;
69 PLND(("Detect done %08lx\n", (long)SCpnt))
70 if (atomic_dec_and_test (&fcss))
71 up(&fc_sem);
74 static void pluto_select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist)
76 Scsi_Device *device;
78 for (device = devlist; device; device = device->next) {
79 if (device->host != host) continue;
80 if (device->tagged_supported)
81 device->queue_depth = /* 254 */ 8;
82 else
83 device->queue_depth = 2;
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(Scsi_Host_Template *tpnt)
91 int i, retry, nplutos;
92 fc_channel *fc;
93 Scsi_Device dev;
94 struct timer_list fc_timer = { function: pluto_detect_timeout };
96 tpnt->proc_name = "pluto";
97 fcscount = 0;
98 for_each_online_fc_channel(fc) {
99 if (!fc->posmap)
100 fcscount++;
102 PLND(("%d channels online\n", fcscount))
103 if (!fcscount) {
104 #if defined(MODULE) && defined(CONFIG_FC4_SOC_MODULE) && defined(CONFIG_KMOD)
105 request_module("soc");
107 for_each_online_fc_channel(fc) {
108 if (!fc->posmap)
109 fcscount++;
111 if (!fcscount)
112 #endif
113 return 0;
115 fcs = (struct ctrl_inquiry *) kmalloc (sizeof (struct ctrl_inquiry) * fcscount, GFP_DMA);
116 if (!fcs) {
117 printk ("PLUTO: Not enough memory to probe\n");
118 return 0;
121 memset (fcs, 0, sizeof (struct ctrl_inquiry) * fcscount);
122 memset (&dev, 0, sizeof(dev));
123 atomic_set (&fcss, fcscount);
125 i = 0;
126 for_each_online_fc_channel(fc) {
127 Scsi_Cmnd *SCpnt;
128 struct Scsi_Host *host;
129 struct pluto *pluto;
131 if (i == fcscount) break;
132 if (fc->posmap) continue;
134 PLD(("trying to find SSA\n"))
136 /* If this is already registered to some other SCSI host, then it cannot be pluto */
137 if (fc->scsi_name[0]) continue;
138 memcpy (fc->scsi_name, "SSA", 4);
140 fcs[i].fc = fc;
142 fc->can_queue = PLUTO_CAN_QUEUE;
143 fc->rsp_size = 64;
144 fc->encode_addr = pluto_encode_addr;
146 fc->fcp_register(fc, TYPE_SCSI_FCP, 0);
148 SCpnt = &(fcs[i].cmd);
149 host = &(fcs[i].host);
150 pluto = (struct pluto *)host->hostdata;
152 pluto->fc = fc;
154 SCpnt->host = host;
155 SCpnt->cmnd[0] = INQUIRY;
156 SCpnt->cmnd[4] = 255;
158 /* FC layer requires this, so that SCpnt->device->tagged_supported is initially 0 */
159 SCpnt->device = &dev;
161 SCpnt->cmd_len = COMMAND_SIZE(INQUIRY);
163 SCpnt->request.rq_status = RQ_SCSI_BUSY;
165 SCpnt->done = pluto_detect_done;
166 SCpnt->bufflen = 256;
167 SCpnt->buffer = fcs[i].inquiry;
168 SCpnt->request_bufflen = 256;
169 SCpnt->request_buffer = fcs[i].inquiry;
170 PLD(("set up %d %08lx\n", i, (long)SCpnt))
171 i++;
174 for (retry = 0; retry < 5; retry++) {
175 for (i = 0; i < fcscount; i++) {
176 if (!fcs[i].fc) break;
177 if (fcs[i].cmd.request.rq_status != RQ_SCSI_DONE) {
178 disable_irq(fcs[i].fc->irq);
179 PLND(("queuecommand %d %d\n", retry, i))
180 fcp_scsi_queuecommand (&(fcs[i].cmd),
181 pluto_detect_scsi_done);
182 enable_irq(fcs[i].fc->irq);
186 fc_timer.expires = jiffies + 10 * HZ;
187 add_timer(&fc_timer);
189 down(&fc_sem);
190 PLND(("Woken up\n"))
191 if (!atomic_read(&fcss))
192 break; /* All fc channels have answered us */
194 del_timer_sync(&fc_timer);
196 PLND(("Finished search\n"))
197 for (i = 0, nplutos = 0; i < fcscount; i++) {
198 Scsi_Cmnd *SCpnt;
200 if (!(fc = fcs[i].fc)) break;
202 SCpnt = &(fcs[i].cmd);
204 /* Let FC mid-level free allocated resources */
205 SCpnt->done (SCpnt);
207 if (!SCpnt->result) {
208 struct pluto_inquiry *inq;
209 struct pluto *pluto;
210 struct Scsi_Host *host;
212 inq = (struct pluto_inquiry *)fcs[i].inquiry;
214 if ((inq->dtype & 0x1f) == TYPE_PROCESSOR &&
215 !strncmp (inq->vendor_id, "SUN", 3) &&
216 !strncmp (inq->product_id, "SSA", 3)) {
217 char *p;
218 long *ages;
220 ages = kmalloc (((inq->channels + 1) * inq->targets) * sizeof(long), GFP_KERNEL);
221 if (!ages) continue;
223 host = scsi_register (tpnt, sizeof (struct pluto));
224 if (!host) panic ("Cannot register PLUTO host\n");
226 nplutos++;
228 if (fc->module) __MOD_INC_USE_COUNT(fc->module);
230 pluto = (struct pluto *)host->hostdata;
232 host->max_id = inq->targets;
233 host->max_channel = inq->channels;
234 host->irq = fc->irq;
236 #ifdef __sparc_v9__
237 host->unchecked_isa_dma = 1;
238 #endif
240 host->select_queue_depths = pluto_select_queue_depths;
242 fc->channels = inq->channels + 1;
243 fc->targets = inq->targets;
244 fc->ages = ages;
245 memset (ages, 0, ((inq->channels + 1) * inq->targets) * sizeof(long));
247 pluto->fc = fc;
248 memcpy (pluto->rev_str, inq->revision, 4);
249 pluto->rev_str[4] = 0;
250 p = strchr (pluto->rev_str, ' ');
251 if (p) *p = 0;
252 memcpy (pluto->fw_rev_str, inq->fw_revision, 4);
253 pluto->fw_rev_str[4] = 0;
254 p = strchr (pluto->fw_rev_str, ' ');
255 if (p) *p = 0;
256 memcpy (pluto->serial_str, inq->serial, 12);
257 pluto->serial_str[12] = 0;
258 p = strchr (pluto->serial_str, ' ');
259 if (p) *p = 0;
261 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))
262 } else
263 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
264 } else
265 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
267 kfree((char *)fcs);
268 if (nplutos)
269 printk ("PLUTO: Total of %d SparcSTORAGE Arrays found\n", nplutos);
270 return nplutos;
273 int pluto_release(struct Scsi_Host *host)
275 struct pluto *pluto = (struct pluto *)host->hostdata;
276 fc_channel *fc = pluto->fc;
278 if (fc->module) __MOD_DEC_USE_COUNT(fc->module);
280 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
281 PLND((" releasing pluto.\n"));
282 kfree (fc->ages);
283 PLND(("released pluto!\n"));
284 return 0;
287 const char *pluto_info(struct Scsi_Host *host)
289 static char buf[128], *p;
290 struct pluto *pluto = (struct pluto *) host->hostdata;
292 sprintf(buf, "SUN SparcSTORAGE Array %s fw %s serial %s %dx%d on %s",
293 pluto->rev_str, pluto->fw_rev_str, pluto->serial_str,
294 host->max_channel, host->max_id, pluto->fc->name);
295 #ifdef __sparc__
296 p = strchr(buf, 0);
297 sprintf(p, " PROM node %x", pluto->fc->dev->prom_node);
298 #endif
299 return buf;
302 /* SSA uses this FC4S addressing:
303 switch (addr[0])
305 case 0: CONTROLLER - All of addr[1]..addr[3] has to be 0
306 case 1: SINGLE DISK - addr[1] channel, addr[2] id, addr[3] 0
307 case 2: DISK GROUP - ???
310 So that SCSI mid-layer can access to these, we reserve
311 channel 0 id 0 lun 0 for CONTROLLER
312 and channels 1 .. max_channel are normal single disks.
314 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd)
316 PLND(("encode addr %d %d %d\n", SCpnt->channel, SCpnt->target, SCpnt->cmnd[1] & 0xe0))
317 /* We don't support LUNs - neither does SSA :) */
318 if (SCpnt->cmnd[1] & 0xe0) return -EINVAL;
319 if (!SCpnt->channel) {
320 if (SCpnt->target) return -EINVAL;
321 memset (addr, 0, 4 * sizeof(u16));
322 } else {
323 addr[0] = 1;
324 addr[1] = SCpnt->channel - 1;
325 addr[2] = SCpnt->target;
326 addr[3] = 0;
328 /* We're Point-to-Point, so target it to the default DID */
329 fcmd->did = fc->did;
330 PLND(("trying %04x%04x%04x%04x\n", addr[0], addr[1], addr[2], addr[3]))
331 return 0;
334 #ifdef MODULE
336 Scsi_Host_Template driver_template = PLUTO;
338 #include "scsi_module.c"
340 EXPORT_NO_SYMBOLS;
341 #endif /* MODULE */