Merge from Linus' tree
[sfrench/cifs-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 <scsi/scsi_host.h>
43 #include <asm/semaphore.h>
44
45 #include "aacraid.h"
46
47 struct aac_common aac_config = {
48         .irq_mod = 1
49 };
50
51 static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
52 {
53         unsigned char *base;
54         unsigned long size, align;
55         const unsigned long fibsize = 4096;
56         const unsigned long printfbufsiz = 256;
57         struct aac_init *init;
58         dma_addr_t phys;
59
60         size = fibsize + sizeof(struct aac_init) + commsize + commalign + printfbufsiz;
61
62  
63         base = pci_alloc_consistent(dev->pdev, size, &phys);
64
65         if(base == NULL)
66         {
67                 printk(KERN_ERR "aacraid: unable to create mapping.\n");
68                 return 0;
69         }
70         dev->comm_addr = (void *)base;
71         dev->comm_phys = phys;
72         dev->comm_size = size;
73         
74         dev->init = (struct aac_init *)(base + fibsize);
75         dev->init_pa = phys + fibsize;
76
77         init = dev->init;
78
79         init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
80         if (dev->max_fib_size != sizeof(struct hw_fib))
81                 init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
82         init->MiniPortRevision = cpu_to_le32(Sa_MINIPORT_REVISION);
83         init->fsrev = cpu_to_le32(dev->fsrev);
84
85         /*
86          *      Adapter Fibs are the first thing allocated so that they
87          *      start page aligned
88          */
89         dev->aif_base_va = (struct hw_fib *)base;
90         
91         init->AdapterFibsVirtualAddress = 0;
92         init->AdapterFibsPhysicalAddress = cpu_to_le32((u32)phys);
93         init->AdapterFibsSize = cpu_to_le32(fibsize);
94         init->AdapterFibAlign = cpu_to_le32(sizeof(struct hw_fib));
95         /* 
96          * number of 4k pages of host physical memory. The aacraid fw needs
97          * this number to be less than 4gb worth of pages. num_physpages is in
98          * system page units. New firmware doesn't have any issues with the
99          * mapping system, but older Firmware did, and had *troubles* dealing
100          * with the math overloading past 32 bits, thus we must limit this
101          * field.
102          *
103          * This assumes the memory is mapped zero->n, which isnt
104          * always true on real computers. It also has some slight problems
105          * with the GART on x86-64. I've btw never tried DMA from PCI space
106          * on this platform but don't be suprised if its problematic.
107          */
108 #ifndef CONFIG_GART_IOMMU
109         if ((num_physpages << (PAGE_SHIFT - 12)) <= AAC_MAX_HOSTPHYSMEMPAGES) {
110                 init->HostPhysMemPages = 
111                         cpu_to_le32(num_physpages << (PAGE_SHIFT-12));
112         } else 
113 #endif  
114         {
115                 init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
116         }
117
118         init->InitFlags = 0;
119         init->MaxIoCommands = cpu_to_le32(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
120         init->MaxIoSize = cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
121         init->MaxFibSize = cpu_to_le32(dev->max_fib_size);
122
123         /*
124          * Increment the base address by the amount already used
125          */
126         base = base + fibsize + sizeof(struct aac_init);
127         phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
128         /*
129          *      Align the beginning of Headers to commalign
130          */
131         align = (commalign - ((unsigned long)(base) & (commalign - 1)));
132         base = base + align;
133         phys = phys + align;
134         /*
135          *      Fill in addresses of the Comm Area Headers and Queues
136          */
137         *commaddr = base;
138         init->CommHeaderAddress = cpu_to_le32((u32)phys);
139         /*
140          *      Increment the base address by the size of the CommArea
141          */
142         base = base + commsize;
143         phys = phys + commsize;
144         /*
145          *       Place the Printf buffer area after the Fast I/O comm area.
146          */
147         dev->printfbuf = (void *)base;
148         init->printfbuf = cpu_to_le32(phys);
149         init->printfbufsiz = cpu_to_le32(printfbufsiz);
150         memset(base, 0, printfbufsiz);
151         return 1;
152 }
153     
154 static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
155 {
156         q->numpending = 0;
157         q->dev = dev;
158         INIT_LIST_HEAD(&q->pendingq);
159         init_waitqueue_head(&q->cmdready);
160         INIT_LIST_HEAD(&q->cmdq);
161         init_waitqueue_head(&q->qfull);
162         spin_lock_init(&q->lockdata);
163         q->lock = &q->lockdata;
164         q->headers.producer = (__le32 *)mem;
165         q->headers.consumer = (__le32 *)(mem+1);
166         *(q->headers.producer) = cpu_to_le32(qsize);
167         *(q->headers.consumer) = cpu_to_le32(qsize);
168         q->entries = qsize;
169 }
170
171 /**
172  *      aac_send_shutdown               -       shutdown an adapter
173  *      @dev: Adapter to shutdown
174  *
175  *      This routine will send a VM_CloseAll (shutdown) request to the adapter.
176  */
177
178 int aac_send_shutdown(struct aac_dev * dev)
179 {
180         struct fib * fibctx;
181         struct aac_close *cmd;
182         int status;
183
184         fibctx = fib_alloc(dev);
185         if (!fibctx)
186                 return -ENOMEM;
187         fib_init(fibctx);
188
189         cmd = (struct aac_close *) fib_data(fibctx);
190
191         cmd->command = cpu_to_le32(VM_CloseAll);
192         cmd->cid = cpu_to_le32(0xffffffff);
193
194         status = fib_send(ContainerCommand,
195                           fibctx,
196                           sizeof(struct aac_close),
197                           FsaNormal,
198                           -2 /* Timeout silently */, 1,
199                           NULL, NULL);
200
201         if (status == 0)
202                 fib_complete(fibctx);
203         fib_free(fibctx);
204         return status;
205 }
206
207 /**
208  *      aac_comm_init   -       Initialise FSA data structures
209  *      @dev:   Adapter to initialise
210  *
211  *      Initializes the data structures that are required for the FSA commuication
212  *      interface to operate. 
213  *      Returns
214  *              1 - if we were able to init the commuication interface.
215  *              0 - If there were errors initing. This is a fatal error.
216  */
217  
218 static int aac_comm_init(struct aac_dev * dev)
219 {
220         unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
221         unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
222         u32 *headers;
223         struct aac_entry * queues;
224         unsigned long size;
225         struct aac_queue_block * comm = dev->queues;
226         /*
227          *      Now allocate and initialize the zone structures used as our 
228          *      pool of FIB context records.  The size of the zone is based
229          *      on the system memory size.  We also initialize the mutex used
230          *      to protect the zone.
231          */
232         spin_lock_init(&dev->fib_lock);
233
234         /*
235          *      Allocate the physically contigous space for the commuication
236          *      queue headers. 
237          */
238
239         size = hdrsize + queuesize;
240
241         if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
242                 return -ENOMEM;
243
244         queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
245
246         /* Adapter to Host normal priority Command queue */ 
247         comm->queue[HostNormCmdQueue].base = queues;
248         aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
249         queues += HOST_NORM_CMD_ENTRIES;
250         headers += 2;
251
252         /* Adapter to Host high priority command queue */
253         comm->queue[HostHighCmdQueue].base = queues;
254         aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
255     
256         queues += HOST_HIGH_CMD_ENTRIES;
257         headers +=2;
258
259         /* Host to adapter normal priority command queue */
260         comm->queue[AdapNormCmdQueue].base = queues;
261         aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
262     
263         queues += ADAP_NORM_CMD_ENTRIES;
264         headers += 2;
265
266         /* host to adapter high priority command queue */
267         comm->queue[AdapHighCmdQueue].base = queues;
268         aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
269     
270         queues += ADAP_HIGH_CMD_ENTRIES;
271         headers += 2;
272
273         /* adapter to host normal priority response queue */
274         comm->queue[HostNormRespQueue].base = queues;
275         aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
276         queues += HOST_NORM_RESP_ENTRIES;
277         headers += 2;
278
279         /* adapter to host high priority response queue */
280         comm->queue[HostHighRespQueue].base = queues;
281         aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
282    
283         queues += HOST_HIGH_RESP_ENTRIES;
284         headers += 2;
285
286         /* host to adapter normal priority response queue */
287         comm->queue[AdapNormRespQueue].base = queues;
288         aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
289
290         queues += ADAP_NORM_RESP_ENTRIES;
291         headers += 2;
292         
293         /* host to adapter high priority response queue */ 
294         comm->queue[AdapHighRespQueue].base = queues;
295         aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
296
297         comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
298         comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
299         comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
300         comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
301
302         return 0;
303 }
304
305 struct aac_dev *aac_init_adapter(struct aac_dev *dev)
306 {
307         u32 status[5];
308         struct Scsi_Host * host = dev->scsi_host_ptr;
309
310         /*
311          *      Check the preferred comm settings, defaults from template.
312          */
313         dev->max_fib_size = sizeof(struct hw_fib);
314         dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
315                 - sizeof(struct aac_fibhdr)
316                 - sizeof(struct aac_write) + sizeof(struct sgentry))
317                         / sizeof(struct sgentry);
318         dev->raw_io_64 = 0;
319         if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
320                 0, 0, 0, 0, 0, 0, status+0, status+1, status+2, NULL, NULL)) &&
321                         (status[0] == 0x00000001)) {
322                 if (status[1] & AAC_OPT_NEW_COMM_64)
323                         dev->raw_io_64 = 1;
324         }
325         if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
326           0, 0, 0, 0, 0, 0,
327           status+0, status+1, status+2, status+3, status+4))
328          && (status[0] == 0x00000001)) {
329                 /*
330                  *      status[1] >> 16         maximum command size in KB
331                  *      status[1] & 0xFFFF      maximum FIB size
332                  *      status[2] >> 16         maximum SG elements to driver
333                  *      status[2] & 0xFFFF      maximum SG elements from driver
334                  *      status[3] & 0xFFFF      maximum number FIBs outstanding
335                  */
336                 host->max_sectors = (status[1] >> 16) << 1;
337                 dev->max_fib_size = status[1] & 0xFFFF;
338                 host->sg_tablesize = status[2] >> 16;
339                 dev->sg_tablesize = status[2] & 0xFFFF;
340                 host->can_queue = (status[3] & 0xFFFF) - AAC_NUM_MGT_FIB;
341                 /*
342                  *      NOTE:
343                  *      All these overrides are based on a fixed internal
344                  *      knowledge and understanding of existing adapters,
345                  *      acbsize should be set with caution.
346                  */
347                 if (acbsize == 512) {
348                         host->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
349                         dev->max_fib_size = 512;
350                         dev->sg_tablesize = host->sg_tablesize
351                           = (512 - sizeof(struct aac_fibhdr)
352                             - sizeof(struct aac_write) + sizeof(struct sgentry))
353                              / sizeof(struct sgentry);
354                         host->can_queue = AAC_NUM_IO_FIB;
355                 } else if (acbsize == 2048) {
356                         host->max_sectors = 512;
357                         dev->max_fib_size = 2048;
358                         host->sg_tablesize = 65;
359                         dev->sg_tablesize = 81;
360                         host->can_queue = 512 - AAC_NUM_MGT_FIB;
361                 } else if (acbsize == 4096) {
362                         host->max_sectors = 1024;
363                         dev->max_fib_size = 4096;
364                         host->sg_tablesize = 129;
365                         dev->sg_tablesize = 166;
366                         host->can_queue = 256 - AAC_NUM_MGT_FIB;
367                 } else if (acbsize == 8192) {
368                         host->max_sectors = 2048;
369                         dev->max_fib_size = 8192;
370                         host->sg_tablesize = 257;
371                         dev->sg_tablesize = 337;
372                         host->can_queue = 128 - AAC_NUM_MGT_FIB;
373                 } else if (acbsize > 0) {
374                         printk("Illegal acbsize=%d ignored\n", acbsize);
375                 }
376         }
377         {
378
379                 if (numacb > 0) {
380                         if (numacb < host->can_queue)
381                                 host->can_queue = numacb;
382                         else
383                                 printk("numacb=%d ignored\n", numacb);
384                 }
385         }
386
387         /*
388          *      Ok now init the communication subsystem
389          */
390
391         dev->queues = (struct aac_queue_block *) kmalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
392         if (dev->queues == NULL) {
393                 printk(KERN_ERR "Error could not allocate comm region.\n");
394                 return NULL;
395         }
396         memset(dev->queues, 0, sizeof(struct aac_queue_block));
397
398         if (aac_comm_init(dev)<0){
399                 kfree(dev->queues);
400                 return NULL;
401         }
402         /*
403          *      Initialize the list of fibs
404          */
405         if(fib_setup(dev)<0){
406                 kfree(dev->queues);
407                 return NULL;
408         }
409                 
410         INIT_LIST_HEAD(&dev->fib_list);
411         init_completion(&dev->aif_completion);
412
413         return dev;
414 }
415
416