blob: cbac063551073c4db0a0401d05281e7844b72320 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Salyzyn, Mark4dfb7cb2007-03-27 15:07:28 -04008 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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>
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070041#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "aacraid.h"
44
Mark Haverkampbed30de2005-08-03 15:38:51 -070045struct aac_common aac_config = {
46 .irq_mod = 1
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static 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;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070053 const unsigned long fibsize = 4096;
54 const unsigned long printfbufsiz = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070078 if (dev->max_fib_size != sizeof(struct hw_fib))
79 init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 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));
Salyzyn, Mark12e9b5f2006-06-26 08:37:39 -040093 init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070095 init->InitFlags = 0;
Mark Haverkamp28713322007-01-23 14:59:20 -080096 if (dev->comm_interface == AAC_COMM_MESSAGE) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -070097 init->InitFlags = cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED);
98 dprintk((KERN_WARNING"aacraid: New Comm Interface enabled\n"));
99 }
Mark Salyzyn655d7222008-04-30 16:03:42 -0400100 init->InitFlags |= cpu_to_le32(INITFLAGS_DRIVER_USES_UTC_TIME |
101 INITFLAGS_DRIVER_SUPPORTS_PM);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700102 init->MaxIoCommands = cpu_to_le32(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
103 init->MaxIoSize = cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
104 init->MaxFibSize = cpu_to_le32(dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /*
107 * Increment the base address by the amount already used
108 */
109 base = base + fibsize + sizeof(struct aac_init);
110 phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
111 /*
112 * Align the beginning of Headers to commalign
113 */
Al Viro142956a2007-10-29 05:11:28 +0000114 align = (commalign - ((uintptr_t)(base) & (commalign - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 base = base + align;
116 phys = phys + align;
117 /*
118 * Fill in addresses of the Comm Area Headers and Queues
119 */
120 *commaddr = base;
121 init->CommHeaderAddress = cpu_to_le32((u32)phys);
122 /*
123 * Increment the base address by the size of the CommArea
124 */
125 base = base + commsize;
126 phys = phys + commsize;
127 /*
128 * Place the Printf buffer area after the Fast I/O comm area.
129 */
130 dev->printfbuf = (void *)base;
131 init->printfbuf = cpu_to_le32(phys);
132 init->printfbufsiz = cpu_to_le32(printfbufsiz);
133 memset(base, 0, printfbufsiz);
134 return 1;
135}
136
137static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
138{
139 q->numpending = 0;
140 q->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 init_waitqueue_head(&q->cmdready);
142 INIT_LIST_HEAD(&q->cmdq);
143 init_waitqueue_head(&q->qfull);
144 spin_lock_init(&q->lockdata);
145 q->lock = &q->lockdata;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700146 q->headers.producer = (__le32 *)mem;
147 q->headers.consumer = (__le32 *)(mem+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *(q->headers.producer) = cpu_to_le32(qsize);
149 *(q->headers.consumer) = cpu_to_le32(qsize);
150 q->entries = qsize;
151}
152
153/**
154 * aac_send_shutdown - shutdown an adapter
155 * @dev: Adapter to shutdown
156 *
157 * This routine will send a VM_CloseAll (shutdown) request to the adapter.
158 */
159
160int aac_send_shutdown(struct aac_dev * dev)
161{
162 struct fib * fibctx;
163 struct aac_close *cmd;
164 int status;
165
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800166 fibctx = aac_fib_alloc(dev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700167 if (!fibctx)
168 return -ENOMEM;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800169 aac_fib_init(fibctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 cmd = (struct aac_close *) fib_data(fibctx);
172
173 cmd->command = cpu_to_le32(VM_CloseAll);
174 cmd->cid = cpu_to_le32(0xffffffff);
175
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800176 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 fibctx,
178 sizeof(struct aac_close),
179 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -0700180 -2 /* Timeout silently */, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 NULL, NULL);
182
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700183 if (status >= 0)
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800184 aac_fib_complete(fibctx);
185 aac_fib_free(fibctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return status;
187}
188
189/**
190 * aac_comm_init - Initialise FSA data structures
191 * @dev: Adapter to initialise
192 *
193 * Initializes the data structures that are required for the FSA commuication
194 * interface to operate.
195 * Returns
196 * 1 - if we were able to init the commuication interface.
197 * 0 - If there were errors initing. This is a fatal error.
198 */
199
Adrian Bunk 48338692005-04-25 19:45:58 -0700200static int aac_comm_init(struct aac_dev * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
203 unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
204 u32 *headers;
205 struct aac_entry * queues;
206 unsigned long size;
207 struct aac_queue_block * comm = dev->queues;
208 /*
209 * Now allocate and initialize the zone structures used as our
210 * pool of FIB context records. The size of the zone is based
211 * on the system memory size. We also initialize the mutex used
212 * to protect the zone.
213 */
214 spin_lock_init(&dev->fib_lock);
215
216 /*
217 * Allocate the physically contigous space for the commuication
218 * queue headers.
219 */
220
221 size = hdrsize + queuesize;
222
223 if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
224 return -ENOMEM;
225
226 queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
227
228 /* Adapter to Host normal priority Command queue */
229 comm->queue[HostNormCmdQueue].base = queues;
230 aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
231 queues += HOST_NORM_CMD_ENTRIES;
232 headers += 2;
233
234 /* Adapter to Host high priority command queue */
235 comm->queue[HostHighCmdQueue].base = queues;
236 aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
237
238 queues += HOST_HIGH_CMD_ENTRIES;
239 headers +=2;
240
241 /* Host to adapter normal priority command queue */
242 comm->queue[AdapNormCmdQueue].base = queues;
243 aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
244
245 queues += ADAP_NORM_CMD_ENTRIES;
246 headers += 2;
247
248 /* host to adapter high priority command queue */
249 comm->queue[AdapHighCmdQueue].base = queues;
250 aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
251
252 queues += ADAP_HIGH_CMD_ENTRIES;
253 headers += 2;
254
255 /* adapter to host normal priority response queue */
256 comm->queue[HostNormRespQueue].base = queues;
257 aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
258 queues += HOST_NORM_RESP_ENTRIES;
259 headers += 2;
260
261 /* adapter to host high priority response queue */
262 comm->queue[HostHighRespQueue].base = queues;
263 aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
264
265 queues += HOST_HIGH_RESP_ENTRIES;
266 headers += 2;
267
268 /* host to adapter normal priority response queue */
269 comm->queue[AdapNormRespQueue].base = queues;
270 aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
271
272 queues += ADAP_NORM_RESP_ENTRIES;
273 headers += 2;
274
275 /* host to adapter high priority response queue */
276 comm->queue[AdapHighRespQueue].base = queues;
277 aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
278
279 comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
280 comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
281 comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
282 comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
283
284 return 0;
285}
286
287struct aac_dev *aac_init_adapter(struct aac_dev *dev)
288{
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700289 u32 status[5];
290 struct Scsi_Host * host = dev->scsi_host_ptr;
291
292 /*
293 * Check the preferred comm settings, defaults from template.
294 */
295 dev->max_fib_size = sizeof(struct hw_fib);
296 dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
297 - sizeof(struct aac_fibhdr)
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700298 - sizeof(struct aac_write) + sizeof(struct sgentry))
299 / sizeof(struct sgentry);
Mark Haverkamp28713322007-01-23 14:59:20 -0800300 dev->comm_interface = AAC_COMM_PRODUCER;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700301 dev->raw_io_64 = 0;
302 if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
303 0, 0, 0, 0, 0, 0, status+0, status+1, status+2, NULL, NULL)) &&
304 (status[0] == 0x00000001)) {
Salyzyn, Marka3940da2008-01-08 12:48:25 -0800305 if (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM_64))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700306 dev->raw_io_64 = 1;
Mark Haverkamp28713322007-01-23 14:59:20 -0800307 if (dev->a_ops.adapter_comm &&
Salyzyn, Marka3940da2008-01-08 12:48:25 -0800308 (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM)))
Mark Haverkamp28713322007-01-23 14:59:20 -0800309 dev->comm_interface = AAC_COMM_MESSAGE;
310 if ((dev->comm_interface == AAC_COMM_MESSAGE) &&
311 (status[2] > dev->base_size)) {
Mark Haverkamp76a7f8f2006-09-19 09:00:02 -0700312 aac_adapter_ioremap(dev, 0);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700313 dev->base_size = status[2];
Mark Haverkamp76a7f8f2006-09-19 09:00:02 -0700314 if (aac_adapter_ioremap(dev, status[2])) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700315 /* remap failed, go back ... */
Mark Haverkamp28713322007-01-23 14:59:20 -0800316 dev->comm_interface = AAC_COMM_PRODUCER;
Mark Haverkamp76a7f8f2006-09-19 09:00:02 -0700317 if (aac_adapter_ioremap(dev, AAC_MIN_FOOTPRINT_SIZE)) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700318 printk(KERN_WARNING
319 "aacraid: unable to map adapter.\n");
320 return NULL;
321 }
322 }
323 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700324 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700325 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)
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700352 - sizeof(struct aac_write) + sizeof(struct sgentry))
353 / sizeof(struct sgentry);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700354 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * Ok now init the communication subsystem
389 */
390
Salyzyn, Mark4dbc22d2007-04-16 11:21:50 -0400391 dev->queues = kzalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (dev->queues == NULL) {
393 printk(KERN_ERR "Error could not allocate comm region.\n");
394 return NULL;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (aac_comm_init(dev)<0){
398 kfree(dev->queues);
399 return NULL;
400 }
401 /*
402 * Initialize the list of fibs
403 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800404 if (aac_fib_setup(dev) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 kfree(dev->queues);
406 return NULL;
407 }
408
409 INIT_LIST_HEAD(&dev->fib_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return dev;
412}
413
414