Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.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 surprised if its problematic.
107          * [AK: something is very very wrong when a driver tests this symbol.
108          *  Someone should figure out what the comment writer really meant here and fix
109          *  the code. Or just remove that bad code. ]
110          */
111 #ifndef CONFIG_IOMMU
112         if ((num_physpages << (PAGE_SHIFT - 12)) <= AAC_MAX_HOSTPHYSMEMPAGES) {
113                 init->HostPhysMemPages = 
114                         cpu_to_le32(num_physpages << (PAGE_SHIFT-12));
115         } else 
116 #endif  
117         {
118                 init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
119         }
120
121         init->InitFlags = 0;
122         if (dev->new_comm_interface) {
123                 init->InitFlags = cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED);
124                 dprintk((KERN_WARNING"aacraid: New Comm Interface enabled\n"));
125         }
126         init->MaxIoCommands = cpu_to_le32(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
127         init->MaxIoSize = cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
128         init->MaxFibSize = cpu_to_le32(dev->max_fib_size);
129
130         /*
131          * Increment the base address by the amount already used
132          */
133         base = base + fibsize + sizeof(struct aac_init);
134         phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
135         /*
136          *      Align the beginning of Headers to commalign
137          */
138         align = (commalign - ((unsigned long)(base) & (commalign - 1)));
139         base = base + align;
140         phys = phys + align;
141         /*
142          *      Fill in addresses of the Comm Area Headers and Queues
143          */
144         *commaddr = base;
145         init->CommHeaderAddress = cpu_to_le32((u32)phys);
146         /*
147          *      Increment the base address by the size of the CommArea
148          */
149         base = base + commsize;
150         phys = phys + commsize;
151         /*
152          *       Place the Printf buffer area after the Fast I/O comm area.
153          */
154         dev->printfbuf = (void *)base;
155         init->printfbuf = cpu_to_le32(phys);
156         init->printfbufsiz = cpu_to_le32(printfbufsiz);
157         memset(base, 0, printfbufsiz);
158         return 1;
159 }
160     
161 static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
162 {
163         q->numpending = 0;
164         q->dev = dev;
165         init_waitqueue_head(&q->cmdready);
166         INIT_LIST_HEAD(&q->cmdq);
167         init_waitqueue_head(&q->qfull);
168         spin_lock_init(&q->lockdata);
169         q->lock = &q->lockdata;
170         q->headers.producer = (__le32 *)mem;
171         q->headers.consumer = (__le32 *)(mem+1);
172         *(q->headers.producer) = cpu_to_le32(qsize);
173         *(q->headers.consumer) = cpu_to_le32(qsize);
174         q->entries = qsize;
175 }
176
177 /**
178  *      aac_send_shutdown               -       shutdown an adapter
179  *      @dev: Adapter to shutdown
180  *
181  *      This routine will send a VM_CloseAll (shutdown) request to the adapter.
182  */
183
184 int aac_send_shutdown(struct aac_dev * dev)
185 {
186         struct fib * fibctx;
187         struct aac_close *cmd;
188         int status;
189
190         fibctx = aac_fib_alloc(dev);
191         if (!fibctx)
192                 return -ENOMEM;
193         aac_fib_init(fibctx);
194
195         cmd = (struct aac_close *) fib_data(fibctx);
196
197         cmd->command = cpu_to_le32(VM_CloseAll);
198         cmd->cid = cpu_to_le32(0xffffffff);
199
200         status = aac_fib_send(ContainerCommand,
201                           fibctx,
202                           sizeof(struct aac_close),
203                           FsaNormal,
204                           -2 /* Timeout silently */, 1,
205                           NULL, NULL);
206
207         if (status == 0)
208                 aac_fib_complete(fibctx);
209         aac_fib_free(fibctx);
210         return status;
211 }
212
213 /**
214  *      aac_comm_init   -       Initialise FSA data structures
215  *      @dev:   Adapter to initialise
216  *
217  *      Initializes the data structures that are required for the FSA commuication
218  *      interface to operate. 
219  *      Returns
220  *              1 - if we were able to init the commuication interface.
221  *              0 - If there were errors initing. This is a fatal error.
222  */
223  
224 static int aac_comm_init(struct aac_dev * dev)
225 {
226         unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
227         unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
228         u32 *headers;
229         struct aac_entry * queues;
230         unsigned long size;
231         struct aac_queue_block * comm = dev->queues;
232         /*
233          *      Now allocate and initialize the zone structures used as our 
234          *      pool of FIB context records.  The size of the zone is based
235          *      on the system memory size.  We also initialize the mutex used
236          *      to protect the zone.
237          */
238         spin_lock_init(&dev->fib_lock);
239
240         /*
241          *      Allocate the physically contigous space for the commuication
242          *      queue headers. 
243          */
244
245         size = hdrsize + queuesize;
246
247         if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
248                 return -ENOMEM;
249
250         queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
251
252         /* Adapter to Host normal priority Command queue */ 
253         comm->queue[HostNormCmdQueue].base = queues;
254         aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
255         queues += HOST_NORM_CMD_ENTRIES;
256         headers += 2;
257
258         /* Adapter to Host high priority command queue */
259         comm->queue[HostHighCmdQueue].base = queues;
260         aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
261     
262         queues += HOST_HIGH_CMD_ENTRIES;
263         headers +=2;
264
265         /* Host to adapter normal priority command queue */
266         comm->queue[AdapNormCmdQueue].base = queues;
267         aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
268     
269         queues += ADAP_NORM_CMD_ENTRIES;
270         headers += 2;
271
272         /* host to adapter high priority command queue */
273         comm->queue[AdapHighCmdQueue].base = queues;
274         aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
275     
276         queues += ADAP_HIGH_CMD_ENTRIES;
277         headers += 2;
278
279         /* adapter to host normal priority response queue */
280         comm->queue[HostNormRespQueue].base = queues;
281         aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
282         queues += HOST_NORM_RESP_ENTRIES;
283         headers += 2;
284
285         /* adapter to host high priority response queue */
286         comm->queue[HostHighRespQueue].base = queues;
287         aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
288    
289         queues += HOST_HIGH_RESP_ENTRIES;
290         headers += 2;
291
292         /* host to adapter normal priority response queue */
293         comm->queue[AdapNormRespQueue].base = queues;
294         aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
295
296         queues += ADAP_NORM_RESP_ENTRIES;
297         headers += 2;
298         
299         /* host to adapter high priority response queue */ 
300         comm->queue[AdapHighRespQueue].base = queues;
301         aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
302
303         comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
304         comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
305         comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
306         comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
307
308         return 0;
309 }
310
311 struct aac_dev *aac_init_adapter(struct aac_dev *dev)
312 {
313         u32 status[5];
314         struct Scsi_Host * host = dev->scsi_host_ptr;
315
316         /*
317          *      Check the preferred comm settings, defaults from template.
318          */
319         dev->max_fib_size = sizeof(struct hw_fib);
320         dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
321                 - sizeof(struct aac_fibhdr)
322                 - sizeof(struct aac_write) + sizeof(struct sgentry))
323                         / sizeof(struct sgentry);
324         dev->new_comm_interface = 0;
325         dev->raw_io_64 = 0;
326         if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
327                 0, 0, 0, 0, 0, 0, status+0, status+1, status+2, NULL, NULL)) &&
328                         (status[0] == 0x00000001)) {
329                 if (status[1] & AAC_OPT_NEW_COMM_64)
330                         dev->raw_io_64 = 1;
331                 if (status[1] & AAC_OPT_NEW_COMM)
332                         dev->new_comm_interface = dev->a_ops.adapter_send != 0;
333                 if (dev->new_comm_interface && (status[2] > dev->base_size)) {
334                         iounmap(dev->regs.sa);
335                         dev->base_size = status[2];
336                         dprintk((KERN_DEBUG "ioremap(%lx,%d)\n",
337                           host->base, status[2]));
338                         dev->regs.sa = ioremap(host->base, status[2]);
339                         if (dev->regs.sa == NULL) {
340                                 /* remap failed, go back ... */
341                                 dev->new_comm_interface = 0;
342                                 dev->regs.sa = ioremap(host->base, 
343                                                 AAC_MIN_FOOTPRINT_SIZE);
344                                 if (dev->regs.sa == NULL) {     
345                                         printk(KERN_WARNING
346                                           "aacraid: unable to map adapter.\n");
347                                         return NULL;
348                                 }
349                         }
350                 }
351         }
352         if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
353           0, 0, 0, 0, 0, 0,
354           status+0, status+1, status+2, status+3, status+4))
355          && (status[0] == 0x00000001)) {
356                 /*
357                  *      status[1] >> 16         maximum command size in KB
358                  *      status[1] & 0xFFFF      maximum FIB size
359                  *      status[2] >> 16         maximum SG elements to driver
360                  *      status[2] & 0xFFFF      maximum SG elements from driver
361                  *      status[3] & 0xFFFF      maximum number FIBs outstanding
362                  */
363                 host->max_sectors = (status[1] >> 16) << 1;
364                 dev->max_fib_size = status[1] & 0xFFFF;
365                 host->sg_tablesize = status[2] >> 16;
366                 dev->sg_tablesize = status[2] & 0xFFFF;
367                 host->can_queue = (status[3] & 0xFFFF) - AAC_NUM_MGT_FIB;
368                 /*
369                  *      NOTE:
370                  *      All these overrides are based on a fixed internal
371                  *      knowledge and understanding of existing adapters,
372                  *      acbsize should be set with caution.
373                  */
374                 if (acbsize == 512) {
375                         host->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
376                         dev->max_fib_size = 512;
377                         dev->sg_tablesize = host->sg_tablesize
378                           = (512 - sizeof(struct aac_fibhdr)
379                             - sizeof(struct aac_write) + sizeof(struct sgentry))
380                              / sizeof(struct sgentry);
381                         host->can_queue = AAC_NUM_IO_FIB;
382                 } else if (acbsize == 2048) {
383                         host->max_sectors = 512;
384                         dev->max_fib_size = 2048;
385                         host->sg_tablesize = 65;
386                         dev->sg_tablesize = 81;
387                         host->can_queue = 512 - AAC_NUM_MGT_FIB;
388                 } else if (acbsize == 4096) {
389                         host->max_sectors = 1024;
390                         dev->max_fib_size = 4096;
391                         host->sg_tablesize = 129;
392                         dev->sg_tablesize = 166;
393                         host->can_queue = 256 - AAC_NUM_MGT_FIB;
394                 } else if (acbsize == 8192) {
395                         host->max_sectors = 2048;
396                         dev->max_fib_size = 8192;
397                         host->sg_tablesize = 257;
398                         dev->sg_tablesize = 337;
399                         host->can_queue = 128 - AAC_NUM_MGT_FIB;
400                 } else if (acbsize > 0) {
401                         printk("Illegal acbsize=%d ignored\n", acbsize);
402                 }
403         }
404         {
405
406                 if (numacb > 0) {
407                         if (numacb < host->can_queue)
408                                 host->can_queue = numacb;
409                         else
410                                 printk("numacb=%d ignored\n", numacb);
411                 }
412         }
413
414         /*
415          *      Ok now init the communication subsystem
416          */
417
418         dev->queues = (struct aac_queue_block *) kmalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
419         if (dev->queues == NULL) {
420                 printk(KERN_ERR "Error could not allocate comm region.\n");
421                 return NULL;
422         }
423         memset(dev->queues, 0, sizeof(struct aac_queue_block));
424
425         if (aac_comm_init(dev)<0){
426                 kfree(dev->queues);
427                 return NULL;
428         }
429         /*
430          *      Initialize the list of fibs
431          */
432         if (aac_fib_setup(dev) < 0) {
433                 kfree(dev->queues);
434                 return NULL;
435         }
436                 
437         INIT_LIST_HEAD(&dev->fib_list);
438
439         return dev;
440 }
441
442