Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / scsi / aacraid / comminit.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Module Name:
25  *  comminit.c
26  *
27  * Abstract: This supports the initialization of the host adapter commuication interface.
28  *    This is a platform dependent module for the pci cyclone board.
29  *
30  */
31
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/blkdev.h>
39 #include <linux/completion.h>
40 #include <linux/mm.h>
41 #include <scsi/scsi_host.h>
42
43 #include "aacraid.h"
44
45 struct aac_common aac_config = {
46         .irq_mod = 1
47 };
48
49 static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
50 {
51         unsigned char *base;
52         unsigned long size, align;
53         const unsigned long fibsize = 4096;
54         const unsigned long printfbufsiz = 256;
55         struct aac_init *init;
56         dma_addr_t phys;
57
58         size = fibsize + sizeof(struct aac_init) + commsize + commalign + printfbufsiz;
59
60  
61         base = pci_alloc_consistent(dev->pdev, size, &phys);
62
63         if(base == NULL)
64         {
65                 printk(KERN_ERR "aacraid: unable to create mapping.\n");
66                 return 0;
67         }
68         dev->comm_addr = (void *)base;
69         dev->comm_phys = phys;
70         dev->comm_size = size;
71         
72         dev->init = (struct aac_init *)(base + fibsize);
73         dev->init_pa = phys + fibsize;
74
75         init = dev->init;
76
77         init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
78         if (dev->max_fib_size != sizeof(struct hw_fib))
79                 init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
80         init->MiniPortRevision = cpu_to_le32(Sa_MINIPORT_REVISION);
81         init->fsrev = cpu_to_le32(dev->fsrev);
82
83         /*
84          *      Adapter Fibs are the first thing allocated so that they
85          *      start page aligned
86          */
87         dev->aif_base_va = (struct hw_fib *)base;
88         
89         init->AdapterFibsVirtualAddress = 0;
90         init->AdapterFibsPhysicalAddress = cpu_to_le32((u32)phys);
91         init->AdapterFibsSize = cpu_to_le32(fibsize);
92         init->AdapterFibAlign = cpu_to_le32(sizeof(struct hw_fib));
93         init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
94
95         init->InitFlags = 0;
96         if (dev->comm_interface == AAC_COMM_MESSAGE) {
97                 init->InitFlags = cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED);
98                 dprintk((KERN_WARNING"aacraid: New Comm Interface enabled\n"));
99         }
100         init->MaxIoCommands = cpu_to_le32(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
101         init->MaxIoSize = cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
102         init->MaxFibSize = cpu_to_le32(dev->max_fib_size);
103
104         /*
105          * Increment the base address by the amount already used
106          */
107         base = base + fibsize + sizeof(struct aac_init);
108         phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
109         /*
110          *      Align the beginning of Headers to commalign
111          */
112         align = (commalign - ((uintptr_t)(base) & (commalign - 1)));
113         base = base + align;
114         phys = phys + align;
115         /*
116          *      Fill in addresses of the Comm Area Headers and Queues
117          */
118         *commaddr = base;
119         init->CommHeaderAddress = cpu_to_le32((u32)phys);
120         /*
121          *      Increment the base address by the size of the CommArea
122          */
123         base = base + commsize;
124         phys = phys + commsize;
125         /*
126          *       Place the Printf buffer area after the Fast I/O comm area.
127          */
128         dev->printfbuf = (void *)base;
129         init->printfbuf = cpu_to_le32(phys);
130         init->printfbufsiz = cpu_to_le32(printfbufsiz);
131         memset(base, 0, printfbufsiz);
132         return 1;
133 }
134     
135 static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
136 {
137         q->numpending = 0;
138         q->dev = dev;
139         init_waitqueue_head(&q->cmdready);
140         INIT_LIST_HEAD(&q->cmdq);
141         init_waitqueue_head(&q->qfull);
142         spin_lock_init(&q->lockdata);
143         q->lock = &q->lockdata;
144         q->headers.producer = (__le32 *)mem;
145         q->headers.consumer = (__le32 *)(mem+1);
146         *(q->headers.producer) = cpu_to_le32(qsize);
147         *(q->headers.consumer) = cpu_to_le32(qsize);
148         q->entries = qsize;
149 }
150
151 /**
152  *      aac_send_shutdown               -       shutdown an adapter
153  *      @dev: Adapter to shutdown
154  *
155  *      This routine will send a VM_CloseAll (shutdown) request to the adapter.
156  */
157
158 int aac_send_shutdown(struct aac_dev * dev)
159 {
160         struct fib * fibctx;
161         struct aac_close *cmd;
162         int status;
163
164         fibctx = aac_fib_alloc(dev);
165         if (!fibctx)
166                 return -ENOMEM;
167         aac_fib_init(fibctx);
168
169         cmd = (struct aac_close *) fib_data(fibctx);
170
171         cmd->command = cpu_to_le32(VM_CloseAll);
172         cmd->cid = cpu_to_le32(0xffffffff);
173
174         status = aac_fib_send(ContainerCommand,
175                           fibctx,
176                           sizeof(struct aac_close),
177                           FsaNormal,
178                           -2 /* Timeout silently */, 1,
179                           NULL, NULL);
180
181         if (status >= 0)
182                 aac_fib_complete(fibctx);
183         aac_fib_free(fibctx);
184         return status;
185 }
186
187 /**
188  *      aac_comm_init   -       Initialise FSA data structures
189  *      @dev:   Adapter to initialise
190  *
191  *      Initializes the data structures that are required for the FSA commuication
192  *      interface to operate. 
193  *      Returns
194  *              1 - if we were able to init the commuication interface.
195  *              0 - If there were errors initing. This is a fatal error.
196  */
197  
198 static int aac_comm_init(struct aac_dev * dev)
199 {
200         unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
201         unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
202         u32 *headers;
203         struct aac_entry * queues;
204         unsigned long size;
205         struct aac_queue_block * comm = dev->queues;
206         /*
207          *      Now allocate and initialize the zone structures used as our 
208          *      pool of FIB context records.  The size of the zone is based
209          *      on the system memory size.  We also initialize the mutex used
210          *      to protect the zone.
211          */
212         spin_lock_init(&dev->fib_lock);
213
214         /*
215          *      Allocate the physically contigous space for the commuication
216          *      queue headers. 
217          */
218
219         size = hdrsize + queuesize;
220
221         if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
222                 return -ENOMEM;
223
224         queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
225
226         /* Adapter to Host normal priority Command queue */ 
227         comm->queue[HostNormCmdQueue].base = queues;
228         aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
229         queues += HOST_NORM_CMD_ENTRIES;
230         headers += 2;
231
232         /* Adapter to Host high priority command queue */
233         comm->queue[HostHighCmdQueue].base = queues;
234         aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
235     
236         queues += HOST_HIGH_CMD_ENTRIES;
237         headers +=2;
238
239         /* Host to adapter normal priority command queue */
240         comm->queue[AdapNormCmdQueue].base = queues;
241         aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
242     
243         queues += ADAP_NORM_CMD_ENTRIES;
244         headers += 2;
245
246         /* host to adapter high priority command queue */
247         comm->queue[AdapHighCmdQueue].base = queues;
248         aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
249     
250         queues += ADAP_HIGH_CMD_ENTRIES;
251         headers += 2;
252
253         /* adapter to host normal priority response queue */
254         comm->queue[HostNormRespQueue].base = queues;
255         aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
256         queues += HOST_NORM_RESP_ENTRIES;
257         headers += 2;
258
259         /* adapter to host high priority response queue */
260         comm->queue[HostHighRespQueue].base = queues;
261         aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
262    
263         queues += HOST_HIGH_RESP_ENTRIES;
264         headers += 2;
265
266         /* host to adapter normal priority response queue */
267         comm->queue[AdapNormRespQueue].base = queues;
268         aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
269
270         queues += ADAP_NORM_RESP_ENTRIES;
271         headers += 2;
272         
273         /* host to adapter high priority response queue */ 
274         comm->queue[AdapHighRespQueue].base = queues;
275         aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
276
277         comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
278         comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
279         comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
280         comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
281
282         return 0;
283 }
284
285 struct aac_dev *aac_init_adapter(struct aac_dev *dev)
286 {
287         u32 status[5];
288         struct Scsi_Host * host = dev->scsi_host_ptr;
289
290         /*
291          *      Check the preferred comm settings, defaults from template.
292          */
293         dev->max_fib_size = sizeof(struct hw_fib);
294         dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
295                 - sizeof(struct aac_fibhdr)
296                 - sizeof(struct aac_write) + sizeof(struct sgentry))
297                         / sizeof(struct sgentry);
298         dev->comm_interface = AAC_COMM_PRODUCER;
299         dev->raw_io_64 = 0;
300         if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
301                 0, 0, 0, 0, 0, 0, status+0, status+1, status+2, NULL, NULL)) &&
302                         (status[0] == 0x00000001)) {
303                 if (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM_64))
304                         dev->raw_io_64 = 1;
305                 if (dev->a_ops.adapter_comm &&
306                     (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM)))
307                         dev->comm_interface = AAC_COMM_MESSAGE;
308                 if ((dev->comm_interface == AAC_COMM_MESSAGE) &&
309                     (status[2] > dev->base_size)) {
310                         aac_adapter_ioremap(dev, 0);
311                         dev->base_size = status[2];
312                         if (aac_adapter_ioremap(dev, status[2])) {
313                                 /* remap failed, go back ... */
314                                 dev->comm_interface = AAC_COMM_PRODUCER;
315                                 if (aac_adapter_ioremap(dev, AAC_MIN_FOOTPRINT_SIZE)) {
316                                         printk(KERN_WARNING
317                                           "aacraid: unable to map adapter.\n");
318                                         return NULL;
319                                 }
320                         }
321                 }
322         }
323         if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
324           0, 0, 0, 0, 0, 0,
325           status+0, status+1, status+2, status+3, status+4))
326          && (status[0] == 0x00000001)) {
327                 /*
328                  *      status[1] >> 16         maximum command size in KB
329                  *      status[1] & 0xFFFF      maximum FIB size
330                  *      status[2] >> 16         maximum SG elements to driver
331                  *      status[2] & 0xFFFF      maximum SG elements from driver
332                  *      status[3] & 0xFFFF      maximum number FIBs outstanding
333                  */
334                 host->max_sectors = (status[1] >> 16) << 1;
335                 dev->max_fib_size = status[1] & 0xFFFF;
336                 host->sg_tablesize = status[2] >> 16;
337                 dev->sg_tablesize = status[2] & 0xFFFF;
338                 host->can_queue = (status[3] & 0xFFFF) - AAC_NUM_MGT_FIB;
339                 /*
340                  *      NOTE:
341                  *      All these overrides are based on a fixed internal
342                  *      knowledge and understanding of existing adapters,
343                  *      acbsize should be set with caution.
344                  */
345                 if (acbsize == 512) {
346                         host->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
347                         dev->max_fib_size = 512;
348                         dev->sg_tablesize = host->sg_tablesize
349                           = (512 - sizeof(struct aac_fibhdr)
350                             - sizeof(struct aac_write) + sizeof(struct sgentry))
351                              / sizeof(struct sgentry);
352                         host->can_queue = AAC_NUM_IO_FIB;
353                 } else if (acbsize == 2048) {
354                         host->max_sectors = 512;
355                         dev->max_fib_size = 2048;
356                         host->sg_tablesize = 65;
357                         dev->sg_tablesize = 81;
358                         host->can_queue = 512 - AAC_NUM_MGT_FIB;
359                 } else if (acbsize == 4096) {
360                         host->max_sectors = 1024;
361                         dev->max_fib_size = 4096;
362                         host->sg_tablesize = 129;
363                         dev->sg_tablesize = 166;
364                         host->can_queue = 256 - AAC_NUM_MGT_FIB;
365                 } else if (acbsize == 8192) {
366                         host->max_sectors = 2048;
367                         dev->max_fib_size = 8192;
368                         host->sg_tablesize = 257;
369                         dev->sg_tablesize = 337;
370                         host->can_queue = 128 - AAC_NUM_MGT_FIB;
371                 } else if (acbsize > 0) {
372                         printk("Illegal acbsize=%d ignored\n", acbsize);
373                 }
374         }
375         {
376
377                 if (numacb > 0) {
378                         if (numacb < host->can_queue)
379                                 host->can_queue = numacb;
380                         else
381                                 printk("numacb=%d ignored\n", numacb);
382                 }
383         }
384
385         /*
386          *      Ok now init the communication subsystem
387          */
388
389         dev->queues = kzalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
390         if (dev->queues == NULL) {
391                 printk(KERN_ERR "Error could not allocate comm region.\n");
392                 return NULL;
393         }
394
395         if (aac_comm_init(dev)<0){
396                 kfree(dev->queues);
397                 return NULL;
398         }
399         /*
400          *      Initialize the list of fibs
401          */
402         if (aac_fib_setup(dev) < 0) {
403                 kfree(dev->queues);
404                 return NULL;
405         }
406                 
407         INIT_LIST_HEAD(&dev->fib_list);
408
409         return dev;
410 }
411
412