vserver 2.0 rc7
[linux-2.6.git] / 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 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/sched.h>
36 #include <linux/pci.h>
37 #include <linux/spinlock.h>
38 #include <linux/slab.h>
39 #include <linux/blkdev.h>
40 #include <linux/completion.h>
41 #include <linux/mm.h>
42 #include <asm/semaphore.h>
43
44 #include "aacraid.h"
45
46 struct aac_common aac_config;
47
48 static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
49 {
50         unsigned char *base;
51         unsigned long size, align;
52         unsigned long fibsize = 4096;
53         unsigned long printfbufsiz = 256;
54         struct aac_init *init;
55         dma_addr_t phys;
56
57         size = fibsize + sizeof(struct aac_init) + commsize + commalign + printfbufsiz;
58
59  
60         base = pci_alloc_consistent(dev->pdev, size, &phys);
61
62         if(base == NULL)
63         {
64                 printk(KERN_ERR "aacraid: unable to create mapping.\n");
65                 return 0;
66         }
67         dev->comm_addr = (void *)base;
68         dev->comm_phys = phys;
69         dev->comm_size = size;
70         
71         dev->init = (struct aac_init *)(base + fibsize);
72         dev->init_pa = phys + fibsize;
73
74         init = dev->init;
75
76         init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
77         init->MiniPortRevision = cpu_to_le32(Sa_MINIPORT_REVISION);
78         init->fsrev = cpu_to_le32(dev->fsrev);
79
80         /*
81          *      Adapter Fibs are the first thing allocated so that they
82          *      start page aligned
83          */
84         dev->aif_base_va = (struct hw_fib *)base;
85         
86         init->AdapterFibsVirtualAddress = 0;
87         init->AdapterFibsPhysicalAddress = cpu_to_le32((u32)phys);
88         init->AdapterFibsSize = cpu_to_le32(fibsize);
89         init->AdapterFibAlign = cpu_to_le32(sizeof(struct hw_fib));
90         /* 
91          * number of 4k pages of host physical memory. The aacraid fw needs
92          * this number to be less than 4gb worth of pages. num_physpages is in
93          * system page units. New firmware doesn't have any issues with the
94          * mapping system, but older Firmware did, and had *troubles* dealing
95          * with the math overloading past 32 bits, thus we must limit this
96          * field.
97          *
98          * This assumes the memory is mapped zero->n, which isnt
99          * always true on real computers. It also has some slight problems
100          * with the GART on x86-64. I've btw never tried DMA from PCI space
101          * on this platform but don't be suprised if its problematic.
102          */
103 #ifndef CONFIG_GART_IOMMU
104         if ((num_physpages << (PAGE_SHIFT - 12)) <= AAC_MAX_HOSTPHYSMEMPAGES) {
105                 init->HostPhysMemPages = 
106                         cpu_to_le32(num_physpages << (PAGE_SHIFT-12));
107         } else 
108 #endif  
109         {
110                 init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
111         }
112
113
114         /*
115          * Increment the base address by the amount already used
116          */
117         base = base + fibsize + sizeof(struct aac_init);
118         phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
119         /*
120          *      Align the beginning of Headers to commalign
121          */
122         align = (commalign - ((unsigned long)(base) & (commalign - 1)));
123         base = base + align;
124         phys = phys + align;
125         /*
126          *      Fill in addresses of the Comm Area Headers and Queues
127          */
128         *commaddr = base;
129         init->CommHeaderAddress = cpu_to_le32((u32)phys);
130         /*
131          *      Increment the base address by the size of the CommArea
132          */
133         base = base + commsize;
134         phys = phys + commsize;
135         /*
136          *       Place the Printf buffer area after the Fast I/O comm area.
137          */
138         dev->printfbuf = (void *)base;
139         init->printfbuf = cpu_to_le32(phys);
140         init->printfbufsiz = cpu_to_le32(printfbufsiz);
141         memset(base, 0, printfbufsiz);
142         return 1;
143 }
144     
145 static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
146 {
147         q->numpending = 0;
148         q->dev = dev;
149         INIT_LIST_HEAD(&q->pendingq);
150         init_waitqueue_head(&q->cmdready);
151         INIT_LIST_HEAD(&q->cmdq);
152         init_waitqueue_head(&q->qfull);
153         spin_lock_init(&q->lockdata);
154         q->lock = &q->lockdata;
155         q->headers.producer = mem;
156         q->headers.consumer = mem+1;
157         *(q->headers.producer) = cpu_to_le32(qsize);
158         *(q->headers.consumer) = cpu_to_le32(qsize);
159         q->entries = qsize;
160 }
161
162 /**
163  *      aac_send_shutdown               -       shutdown an adapter
164  *      @dev: Adapter to shutdown
165  *
166  *      This routine will send a VM_CloseAll (shutdown) request to the adapter.
167  */
168
169 int aac_send_shutdown(struct aac_dev * dev)
170 {
171         struct fib * fibctx;
172         struct aac_close *cmd;
173         int status;
174
175         fibctx = fib_alloc(dev);
176         fib_init(fibctx);
177
178         cmd = (struct aac_close *) fib_data(fibctx);
179
180         cmd->command = cpu_to_le32(VM_CloseAll);
181         cmd->cid = cpu_to_le32(0xffffffff);
182
183         status = fib_send(ContainerCommand,
184                           fibctx,
185                           sizeof(struct aac_close),
186                           FsaNormal,
187                           1, 1,
188                           NULL, NULL);
189
190         if (status == 0)
191                 fib_complete(fibctx);
192         fib_free(fibctx);
193         return status;
194 }
195
196 /**
197  *      aac_comm_init   -       Initialise FSA data structures
198  *      @dev:   Adapter to initialise
199  *
200  *      Initializes the data structures that are required for the FSA commuication
201  *      interface to operate. 
202  *      Returns
203  *              1 - if we were able to init the commuication interface.
204  *              0 - If there were errors initing. This is a fatal error.
205  */
206  
207 int aac_comm_init(struct aac_dev * dev)
208 {
209         unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
210         unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
211         u32 *headers;
212         struct aac_entry * queues;
213         unsigned long size;
214         struct aac_queue_block * comm = dev->queues;
215         /*
216          *      Now allocate and initialize the zone structures used as our 
217          *      pool of FIB context records.  The size of the zone is based
218          *      on the system memory size.  We also initialize the mutex used
219          *      to protect the zone.
220          */
221         spin_lock_init(&dev->fib_lock);
222
223         /*
224          *      Allocate the physically contigous space for the commuication
225          *      queue headers. 
226          */
227
228         size = hdrsize + queuesize;
229
230         if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
231                 return -ENOMEM;
232
233         queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
234
235         /* Adapter to Host normal priority Command queue */ 
236         comm->queue[HostNormCmdQueue].base = queues;
237         aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
238         queues += HOST_NORM_CMD_ENTRIES;
239         headers += 2;
240
241         /* Adapter to Host high priority command queue */
242         comm->queue[HostHighCmdQueue].base = queues;
243         aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
244     
245         queues += HOST_HIGH_CMD_ENTRIES;
246         headers +=2;
247
248         /* Host to adapter normal priority command queue */
249         comm->queue[AdapNormCmdQueue].base = queues;
250         aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
251     
252         queues += ADAP_NORM_CMD_ENTRIES;
253         headers += 2;
254
255         /* host to adapter high priority command queue */
256         comm->queue[AdapHighCmdQueue].base = queues;
257         aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
258     
259         queues += ADAP_HIGH_CMD_ENTRIES;
260         headers += 2;
261
262         /* adapter to host normal priority response queue */
263         comm->queue[HostNormRespQueue].base = queues;
264         aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
265         queues += HOST_NORM_RESP_ENTRIES;
266         headers += 2;
267
268         /* adapter to host high priority response queue */
269         comm->queue[HostHighRespQueue].base = queues;
270         aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
271    
272         queues += HOST_HIGH_RESP_ENTRIES;
273         headers += 2;
274
275         /* host to adapter normal priority response queue */
276         comm->queue[AdapNormRespQueue].base = queues;
277         aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
278
279         queues += ADAP_NORM_RESP_ENTRIES;
280         headers += 2;
281         
282         /* host to adapter high priority response queue */ 
283         comm->queue[AdapHighRespQueue].base = queues;
284         aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
285
286         comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
287         comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
288         comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
289         comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
290
291         return 0;
292 }
293
294 struct aac_dev *aac_init_adapter(struct aac_dev *dev)
295 {
296         /*
297          *      Ok now init the communication subsystem
298          */
299
300         dev->queues = (struct aac_queue_block *) kmalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
301         if (dev->queues == NULL) {
302                 printk(KERN_ERR "Error could not allocate comm region.\n");
303                 return NULL;
304         }
305         memset(dev->queues, 0, sizeof(struct aac_queue_block));
306
307         if (aac_comm_init(dev)<0){
308                 kfree(dev->queues);
309                 return NULL;
310         }
311         /*
312          *      Initialize the list of fibs
313          */
314         if(fib_setup(dev)<0){
315                 kfree(dev->queues);
316                 return NULL;
317         }
318                 
319         INIT_LIST_HEAD(&dev->fib_list);
320         init_completion(&dev->aif_completion);
321
322         return dev;
323 }
324
325