[SCSI] kmalloc + memset conversion to kzalloc
[cascardo/linux.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/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->done = pluto_detect_done;
164                 SCpnt->request_bufflen = 256;
165                 SCpnt->request_buffer = fcs[i].inquiry;
166                 PLD(("set up %d %08lx\n", i, (long)SCpnt))
167                 i++;
168         }
169         
170         for (retry = 0; retry < 5; retry++) {
171                 for (i = 0; i < fcscount; i++) {
172                         if (!fcs[i].fc) break;
173                         if (!(fcs[i].cmd.request->cmd_flags & REQ_STARTED)) {
174                                 fcs[i].cmd.request->cmd_flags |= REQ_STARTED;
175                                 disable_irq(fcs[i].fc->irq);
176                                 PLND(("queuecommand %d %d\n", retry, i))
177                                 fcp_scsi_queuecommand (&(fcs[i].cmd), 
178                                         pluto_detect_scsi_done);
179                                 enable_irq(fcs[i].fc->irq);
180                         }
181                 }
182             
183                 wait_for_completion_timeout(&fc_detect_complete, 10 * HZ);
184                 PLND(("Woken up\n"))
185                 if (!atomic_read(&fcss))
186                         break; /* All fc channels have answered us */
187         }
188
189         PLND(("Finished search\n"))
190         for (i = 0, nplutos = 0; i < fcscount; i++) {
191                 Scsi_Cmnd *SCpnt;
192                 
193                 if (!(fc = fcs[i].fc)) break;
194         
195                 SCpnt = &(fcs[i].cmd);
196                 
197                 /* Let FC mid-level free allocated resources */
198                 SCpnt->done (SCpnt);
199                 
200                 if (!SCpnt->result) {
201                         struct pluto_inquiry *inq;
202                         struct pluto *pluto;
203                         struct Scsi_Host *host;
204                         
205                         inq = (struct pluto_inquiry *)fcs[i].inquiry;
206
207                         if ((inq->dtype & 0x1f) == TYPE_PROCESSOR &&
208                             !strncmp (inq->vendor_id, "SUN", 3) &&
209                             !strncmp (inq->product_id, "SSA", 3)) {
210                                 char *p;
211                                 long *ages;
212                                 
213                                 ages = kcalloc((inq->channels + 1) * inq->targets, sizeof(long), GFP_KERNEL);
214                                 if (!ages) continue;
215                                 
216                                 host = scsi_register (tpnt, sizeof (struct pluto));
217                                 if(!host)
218                                 {
219                                         kfree(ages);
220                                         continue;
221                                 }
222                                 
223                                 if (!try_module_get(fc->module)) {
224                                         kfree(ages);
225                                         scsi_unregister(host);
226                                         continue;
227                                 }
228
229                                 nplutos++;
230                                 
231                                 pluto = (struct pluto *)host->hostdata;
232                                 
233                                 host->max_id = inq->targets;
234                                 host->max_channel = inq->channels;
235                                 host->irq = fc->irq;
236
237                                 fc->channels = inq->channels + 1;
238                                 fc->targets = inq->targets;
239                                 fc->ages = ages;
240                                 
241                                 pluto->fc = fc;
242                                 memcpy (pluto->rev_str, inq->revision, 4);
243                                 pluto->rev_str[4] = 0;
244                                 p = strchr (pluto->rev_str, ' ');
245                                 if (p) *p = 0;
246                                 memcpy (pluto->fw_rev_str, inq->fw_revision, 4);
247                                 pluto->fw_rev_str[4] = 0;
248                                 p = strchr (pluto->fw_rev_str, ' ');
249                                 if (p) *p = 0;
250                                 memcpy (pluto->serial_str, inq->serial, 12);
251                                 pluto->serial_str[12] = 0;
252                                 p = strchr (pluto->serial_str, ' ');
253                                 if (p) *p = 0;
254                                 
255                                 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))
256                         } else
257                                 fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
258                 } else
259                         fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
260         }
261         kfree(fcs);
262         if (nplutos)
263                 printk ("PLUTO: Total of %d SparcSTORAGE Arrays found\n", nplutos);
264         return nplutos;
265 }
266
267 int pluto_release(struct Scsi_Host *host)
268 {
269         struct pluto *pluto = (struct pluto *)host->hostdata;
270         fc_channel *fc = pluto->fc;
271
272         module_put(fc->module);
273         
274         fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
275         PLND((" releasing pluto.\n"));
276         kfree (fc->ages);
277         PLND(("released pluto!\n"));
278         return 0;
279 }
280
281 const char *pluto_info(struct Scsi_Host *host)
282 {
283         static char buf[128], *p;
284         struct pluto *pluto = (struct pluto *) host->hostdata;
285
286         sprintf(buf, "SUN SparcSTORAGE Array %s fw %s serial %s %dx%d on %s",
287                 pluto->rev_str, pluto->fw_rev_str, pluto->serial_str,
288                 host->max_channel, host->max_id, pluto->fc->name);
289 #ifdef __sparc__
290         p = strchr(buf, 0);
291         sprintf(p, " PROM node %x", pluto->fc->dev->prom_node);
292 #endif  
293         return buf;
294 }
295
296 /* SSA uses this FC4S addressing:
297    switch (addr[0])
298    {
299    case 0: CONTROLLER - All of addr[1]..addr[3] has to be 0
300    case 1: SINGLE DISK - addr[1] channel, addr[2] id, addr[3] 0
301    case 2: DISK GROUP - ???
302    }
303    
304    So that SCSI mid-layer can access to these, we reserve
305    channel 0 id 0 lun 0 for CONTROLLER
306    and channels 1 .. max_channel are normal single disks.
307  */
308 static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd)
309 {
310         PLND(("encode addr %d %d %d\n", SCpnt->device->channel, SCpnt->device->id, SCpnt->cmnd[1] & 0xe0))
311         /* We don't support LUNs - neither does SSA :) */
312         if (SCpnt->cmnd[1] & 0xe0)
313                 return -EINVAL;
314         if (!SCpnt->device->channel) {
315                 if (SCpnt->device->id)
316                         return -EINVAL;
317                 memset (addr, 0, 4 * sizeof(u16));
318         } else {
319                 addr[0] = 1;
320                 addr[1] = SCpnt->device->channel - 1;
321                 addr[2] = SCpnt->device->id;
322                 addr[3] = 0;
323         }
324         /* We're Point-to-Point, so target it to the default DID */
325         fcmd->did = fc->did;
326         PLND(("trying %04x%04x%04x%04x\n", addr[0], addr[1], addr[2], addr[3]))
327         return 0;
328 }
329
330 static struct scsi_host_template driver_template = {
331         .name                   = "Sparc Storage Array 100/200",
332         .detect                 = pluto_detect,
333         .release                = pluto_release,
334         .info                   = pluto_info,
335         .queuecommand           = fcp_scsi_queuecommand,
336         .slave_configure        = pluto_slave_configure,
337         .can_queue              = PLUTO_CAN_QUEUE,
338         .this_id                = -1,
339         .sg_tablesize           = 1,
340         .cmd_per_lun            = 1,
341         .use_clustering         = ENABLE_CLUSTERING,
342         .eh_abort_handler       = fcp_scsi_abort,
343         .eh_device_reset_handler = fcp_scsi_dev_reset,
344         .eh_host_reset_handler  = fcp_scsi_host_reset,
345 };
346
347 #include "scsi_module.c"
348
349 MODULE_LICENSE("GPL");
350