Import 2.3.18pre1
[davej-history.git] / drivers / scsi / pluto.c
blob0a19d9332ce5a94af3018677af920a486fc6538e
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 struct proc_dir_entry proc_scsi_pluto = {
43 PROC_SCSI_PLUTO, 5, "pluto",
44 S_IFDIR | S_IRUGO | S_IXUGO, 2
47 static struct ctrl_inquiry {
48 struct Scsi_Host host;
49 struct pluto pluto;
50 Scsi_Cmnd cmd;
51 char inquiry[256];
52 fc_channel *fc;
53 } *fcs __initdata = { 0 };
54 static int fcscount __initdata = 0;
55 static atomic_t fcss __initdata = ATOMIC_INIT(0);
56 static struct timer_list fc_timer __initdata = { 0 };
57 DECLARE_MUTEX_LOCKED(fc_sem);
59 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd);
61 static void __init pluto_detect_timeout(unsigned long data)
63 PLND(("Timeout\n"))
64 up(&fc_sem);
67 static void __init pluto_detect_done(Scsi_Cmnd *SCpnt)
69 /* Do nothing */
72 static void __init pluto_detect_scsi_done(Scsi_Cmnd *SCpnt)
74 SCpnt->request.rq_status = RQ_SCSI_DONE;
75 PLND(("Detect done %08lx\n", (long)SCpnt))
76 if (atomic_dec_and_test (&fcss))
77 up(&fc_sem);
80 static void pluto_select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist)
82 Scsi_Device *device;
84 for (device = devlist; device; device = device->next) {
85 if (device->host != host) continue;
86 if (device->tagged_supported)
87 device->queue_depth = /* 254 */ 8;
88 else
89 device->queue_depth = 2;
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(Scsi_Host_Template *tpnt)
97 int i, retry, nplutos;
98 fc_channel *fc;
99 Scsi_Device dev;
101 tpnt->proc_dir = &proc_scsi_pluto;
102 fcscount = 0;
103 for_each_online_fc_channel(fc) {
104 if (!fc->posmap)
105 fcscount++;
107 PLND(("%d channels online\n", fcscount))
108 if (!fcscount) {
109 #if defined(MODULE) && defined(CONFIG_FC4_SOC_MODULE) && defined(CONFIG_KMOD)
110 request_module("soc");
112 for_each_online_fc_channel(fc) {
113 if (!fc->posmap)
114 fcscount++;
116 if (!fcscount)
117 #endif
118 return 0;
120 fcs = (struct ctrl_inquiry *) scsi_init_malloc (sizeof (struct ctrl_inquiry) * fcscount, GFP_DMA);
121 if (!fcs) {
122 printk ("PLUTO: Not enough memory to probe\n");
123 return 0;
126 memset (fcs, 0, sizeof (struct ctrl_inquiry) * fcscount);
127 memset (&dev, 0, sizeof(dev));
128 atomic_set (&fcss, fcscount);
129 fc_timer.function = pluto_detect_timeout;
131 i = 0;
132 for_each_online_fc_channel(fc) {
133 Scsi_Cmnd *SCpnt;
134 struct Scsi_Host *host;
135 struct pluto *pluto;
137 if (i == fcscount) break;
138 if (fc->posmap) continue;
140 PLD(("trying to find SSA\n"))
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);
146 fcs[i].fc = fc;
148 fc->can_queue = PLUTO_CAN_QUEUE;
149 fc->rsp_size = 64;
150 fc->encode_addr = pluto_encode_addr;
152 fc->fcp_register(fc, TYPE_SCSI_FCP, 0);
154 SCpnt = &(fcs[i].cmd);
155 host = &(fcs[i].host);
156 pluto = (struct pluto *)host->hostdata;
158 pluto->fc = fc;
160 SCpnt->host = host;
161 SCpnt->cmnd[0] = INQUIRY;
162 SCpnt->cmnd[4] = 255;
164 /* FC layer requires this, so that SCpnt->device->tagged_supported is initially 0 */
165 SCpnt->device = &dev;
167 SCpnt->cmd_len = COMMAND_SIZE(INQUIRY);
169 SCpnt->request.rq_status = RQ_SCSI_BUSY;
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++;
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);
192 fc_timer.expires = jiffies + 10 * HZ;
193 add_timer(&fc_timer);
195 down(&fc_sem);
196 PLND(("Woken up\n"))
197 if (!atomic_read(&fcss))
198 break; /* All fc channels have answered us */
200 del_timer(&fc_timer);
202 PLND(("Finished search\n"))
203 for (i = 0, nplutos = 0; i < fcscount; i++) {
204 Scsi_Cmnd *SCpnt;
206 if (!(fc = fcs[i].fc)) break;
208 SCpnt = &(fcs[i].cmd);
210 /* Let FC mid-level free allocated resources */
211 SCpnt->done (SCpnt);
213 if (!SCpnt->result) {
214 struct pluto_inquiry *inq;
215 struct pluto *pluto;
216 struct Scsi_Host *host;
218 inq = (struct pluto_inquiry *)fcs[i].inquiry;
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;
226 ages = kmalloc (((inq->channels + 1) * inq->targets) * sizeof(long), GFP_KERNEL);
227 if (!ages) continue;
229 host = scsi_register (tpnt, sizeof (struct pluto));
230 if (!host) panic ("Cannot register PLUTO host\n");
232 nplutos++;
234 if (fc->module) __MOD_INC_USE_COUNT(fc->module);
236 pluto = (struct pluto *)host->hostdata;
238 host->max_id = inq->targets;
239 host->max_channel = inq->channels;
240 host->irq = fc->irq;
242 #ifdef __sparc_v9__
243 host->unchecked_isa_dma = 1;
244 #endif
246 host->select_queue_depths = pluto_select_queue_depths;
248 fc->channels = inq->channels + 1;
249 fc->targets = inq->targets;
250 fc->ages = ages;
251 memset (ages, 0, ((inq->channels + 1) * inq->targets) * sizeof(long));
253 pluto->fc = fc;
254 memcpy (pluto->rev_str, inq->revision, 4);
255 pluto->rev_str[4] = 0;
256 p = strchr (pluto->rev_str, ' ');
257 if (p) *p = 0;
258 memcpy (pluto->fw_rev_str, inq->fw_revision, 4);
259 pluto->fw_rev_str[4] = 0;
260 p = strchr (pluto->fw_rev_str, ' ');
261 if (p) *p = 0;
262 memcpy (pluto->serial_str, inq->serial, 12);
263 pluto->serial_str[12] = 0;
264 p = strchr (pluto->serial_str, ' ');
265 if (p) *p = 0;
267 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))
268 } else
269 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
270 } else
271 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
273 scsi_init_free((char *)fcs, sizeof (struct ctrl_inquiry) * fcscount);
274 if (nplutos)
275 printk ("PLUTO: Total of %d SparcSTORAGE Arrays found\n", nplutos);
276 return nplutos;
279 int pluto_release(struct Scsi_Host *host)
281 struct pluto *pluto = (struct pluto *)host->hostdata;
282 fc_channel *fc = pluto->fc;
284 if (fc->module) __MOD_DEC_USE_COUNT(fc->module);
286 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
287 PLND((" releasing pluto.\n"));
288 kfree (fc->ages);
289 PLND(("released pluto!\n"));
290 return 0;
293 const char *pluto_info(struct Scsi_Host *host)
295 static char buf[80];
296 struct pluto *pluto = (struct pluto *) host->hostdata;
298 sprintf(buf, "SUN SparcSTORAGE Array %s fw %s serial %s %dx%d on %s",
299 pluto->rev_str, pluto->fw_rev_str, pluto->serial_str,
300 host->max_channel, host->max_id, pluto->fc->name);
301 return buf;
304 /* SSA uses this FC4S addressing:
305 switch (addr[0])
307 case 0: CONTROLLER - All of addr[1]..addr[3] has to be 0
308 case 1: SINGLE DISK - addr[1] channel, addr[2] id, addr[3] 0
309 case 2: DISK GROUP - ???
312 So that SCSI mid-layer can access to these, we reserve
313 channel 0 id 0 lun 0 for CONTROLLER
314 and channels 1 .. max_channel are normal single disks.
316 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd)
318 PLND(("encode addr %d %d %d\n", SCpnt->channel, SCpnt->target, SCpnt->cmnd[1] & 0xe0))
319 /* We don't support LUNs - neither does SSA :) */
320 if (SCpnt->cmnd[1] & 0xe0) return -EINVAL;
321 if (!SCpnt->channel) {
322 if (SCpnt->target) return -EINVAL;
323 memset (addr, 0, 4 * sizeof(u16));
324 } else {
325 addr[0] = 1;
326 addr[1] = SCpnt->channel - 1;
327 addr[2] = SCpnt->target;
328 addr[3] = 0;
330 /* We're Point-to-Point, so target it to the default DID */
331 fcmd->did = fc->did;
332 PLND(("trying %04x%04x%04x%04x\n", addr[0], addr[1], addr[2], addr[3]))
333 return 0;
336 #ifdef MODULE
338 Scsi_Host_Template driver_template = PLUTO;
340 #include "scsi_module.c"
342 EXPORT_NO_SYMBOLS;
343 #endif /* MODULE */