Linux 2.6.21
[linux/fpc-iii.git] / drivers / scsi / aacraid / dpcsup.c
blobd38b628be1ada395311d5b18265b66bcd1022c5d
1 /*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
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.
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.
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.
24 * Module Name:
25 * dpcsup.c
27 * Abstract: All DPC processing routines for the cyclone board occur here.
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/completion.h>
39 #include <linux/blkdev.h>
40 #include <asm/semaphore.h>
42 #include "aacraid.h"
44 /**
45 * aac_response_normal - Handle command replies
46 * @q: Queue to read from
48 * This DPC routine will be run when the adapter interrupts us to let us
49 * know there is a response on our normal priority queue. We will pull off
50 * all QE there are and wake up all the waiters before exiting. We will
51 * take a spinlock out on the queue before operating on it.
54 unsigned int aac_response_normal(struct aac_queue * q)
56 struct aac_dev * dev = q->dev;
57 struct aac_entry *entry;
58 struct hw_fib * hwfib;
59 struct fib * fib;
60 int consumed = 0;
61 unsigned long flags;
63 spin_lock_irqsave(q->lock, flags);
65 * Keep pulling response QEs off the response queue and waking
66 * up the waiters until there are no more QEs. We then return
67 * back to the system. If no response was requesed we just
68 * deallocate the Fib here and continue.
70 while(aac_consumer_get(dev, q, &entry))
72 int fast;
73 u32 index = le32_to_cpu(entry->addr);
74 fast = index & 0x01;
75 fib = &dev->fibs[index >> 2];
76 hwfib = fib->hw_fib;
78 aac_consumer_free(dev, q, HostNormRespQueue);
80 * Remove this fib from the Outstanding I/O queue.
81 * But only if it has not already been timed out.
83 * If the fib has been timed out already, then just
84 * continue. The caller has already been notified that
85 * the fib timed out.
87 if (!(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
88 dev->queues->queue[AdapNormCmdQueue].numpending--;
89 else {
90 printk(KERN_WARNING "aacraid: FIB timeout (%x).\n", fib->flags);
91 printk(KERN_DEBUG"aacraid: hwfib=%p fib index=%i fib=%p\n",hwfib, hwfib->header.SenderData,fib);
92 continue;
94 spin_unlock_irqrestore(q->lock, flags);
96 if (fast) {
98 * Doctor the fib
100 *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
101 hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
104 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
106 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
108 __le32 *pstatus = (__le32 *)hwfib->data;
109 if (*pstatus & cpu_to_le32(0xffff0000))
110 *pstatus = cpu_to_le32(ST_OK);
112 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async))
114 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
115 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
116 else
117 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
119 * NOTE: we cannot touch the fib after this
120 * call, because it may have been deallocated.
122 fib->callback(fib->callback_data, fib);
123 } else {
124 unsigned long flagv;
125 spin_lock_irqsave(&fib->event_lock, flagv);
126 if (!fib->done)
127 fib->done = 1;
128 up(&fib->event_wait);
129 spin_unlock_irqrestore(&fib->event_lock, flagv);
130 FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
131 if (fib->done == 2) {
132 aac_fib_complete(fib);
133 aac_fib_free(fib);
136 consumed++;
137 spin_lock_irqsave(q->lock, flags);
140 if (consumed > aac_config.peak_fibs)
141 aac_config.peak_fibs = consumed;
142 if (consumed == 0)
143 aac_config.zero_fibs++;
145 spin_unlock_irqrestore(q->lock, flags);
146 return 0;
151 * aac_command_normal - handle commands
152 * @q: queue to process
154 * This DPC routine will be queued when the adapter interrupts us to
155 * let us know there is a command on our normal priority queue. We will
156 * pull off all QE there are and wake up all the waiters before exiting.
157 * We will take a spinlock out on the queue before operating on it.
160 unsigned int aac_command_normal(struct aac_queue *q)
162 struct aac_dev * dev = q->dev;
163 struct aac_entry *entry;
164 unsigned long flags;
166 spin_lock_irqsave(q->lock, flags);
169 * Keep pulling response QEs off the response queue and waking
170 * up the waiters until there are no more QEs. We then return
171 * back to the system.
173 while(aac_consumer_get(dev, q, &entry))
175 struct fib fibctx;
176 struct hw_fib * hw_fib;
177 u32 index;
178 struct fib *fib = &fibctx;
180 index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib);
181 hw_fib = &dev->aif_base_va[index];
184 * Allocate a FIB at all costs. For non queued stuff
185 * we can just use the stack so we are happy. We need
186 * a fib object in order to manage the linked lists
188 if (dev->aif_thread)
189 if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL)
190 fib = &fibctx;
192 memset(fib, 0, sizeof(struct fib));
193 INIT_LIST_HEAD(&fib->fiblink);
194 fib->type = FSAFS_NTC_FIB_CONTEXT;
195 fib->size = sizeof(struct fib);
196 fib->hw_fib = hw_fib;
197 fib->data = hw_fib->data;
198 fib->dev = dev;
201 if (dev->aif_thread && fib != &fibctx) {
202 list_add_tail(&fib->fiblink, &q->cmdq);
203 aac_consumer_free(dev, q, HostNormCmdQueue);
204 wake_up_interruptible(&q->cmdready);
205 } else {
206 aac_consumer_free(dev, q, HostNormCmdQueue);
207 spin_unlock_irqrestore(q->lock, flags);
209 * Set the status of this FIB
211 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
212 aac_fib_adapter_complete(fib, sizeof(u32));
213 spin_lock_irqsave(q->lock, flags);
216 spin_unlock_irqrestore(q->lock, flags);
217 return 0;
222 * aac_intr_normal - Handle command replies
223 * @dev: Device
224 * @index: completion reference
226 * This DPC routine will be run when the adapter interrupts us to let us
227 * know there is a response on our normal priority queue. We will pull off
228 * all QE there are and wake up all the waiters before exiting.
231 unsigned int aac_intr_normal(struct aac_dev * dev, u32 Index)
233 u32 index = le32_to_cpu(Index);
235 dprintk((KERN_INFO "aac_intr_normal(%p,%x)\n", dev, Index));
236 if ((index & 0x00000002L)) {
237 struct hw_fib * hw_fib;
238 struct fib * fib;
239 struct aac_queue *q = &dev->queues->queue[HostNormCmdQueue];
240 unsigned long flags;
242 if (index == 0xFFFFFFFEL) /* Special Case */
243 return 0; /* Do nothing */
245 * Allocate a FIB. For non queued stuff we can just use
246 * the stack so we are happy. We need a fib object in order to
247 * manage the linked lists.
249 if ((!dev->aif_thread)
250 || (!(fib = kmalloc(sizeof(struct fib),GFP_ATOMIC))))
251 return 1;
252 if (!(hw_fib = kmalloc(sizeof(struct hw_fib),GFP_ATOMIC))) {
253 kfree (fib);
254 return 1;
256 memset(hw_fib, 0, sizeof(struct hw_fib));
257 memcpy(hw_fib, (struct hw_fib *)(((unsigned long)(dev->regs.sa)) + (index & ~0x00000002L)), sizeof(struct hw_fib));
258 memset(fib, 0, sizeof(struct fib));
259 INIT_LIST_HEAD(&fib->fiblink);
260 fib->type = FSAFS_NTC_FIB_CONTEXT;
261 fib->size = sizeof(struct fib);
262 fib->hw_fib = hw_fib;
263 fib->data = hw_fib->data;
264 fib->dev = dev;
266 spin_lock_irqsave(q->lock, flags);
267 list_add_tail(&fib->fiblink, &q->cmdq);
268 wake_up_interruptible(&q->cmdready);
269 spin_unlock_irqrestore(q->lock, flags);
270 return 1;
271 } else {
272 int fast = index & 0x01;
273 struct fib * fib = &dev->fibs[index >> 2];
274 struct hw_fib * hwfib = fib->hw_fib;
277 * Remove this fib from the Outstanding I/O queue.
278 * But only if it has not already been timed out.
280 * If the fib has been timed out already, then just
281 * continue. The caller has already been notified that
282 * the fib timed out.
284 if ((fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
285 printk(KERN_WARNING "aacraid: FIB timeout (%x).\n", fib->flags);
286 printk(KERN_DEBUG"aacraid: hwfib=%p index=%i fib=%p\n",hwfib, hwfib->header.SenderData,fib);
287 return 0;
290 dev->queues->queue[AdapNormCmdQueue].numpending--;
292 if (fast) {
294 * Doctor the fib
296 *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
297 hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
300 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
302 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
304 u32 *pstatus = (u32 *)hwfib->data;
305 if (*pstatus & cpu_to_le32(0xffff0000))
306 *pstatus = cpu_to_le32(ST_OK);
308 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async))
310 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
311 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
312 else
313 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
315 * NOTE: we cannot touch the fib after this
316 * call, because it may have been deallocated.
318 fib->callback(fib->callback_data, fib);
319 } else {
320 unsigned long flagv;
321 dprintk((KERN_INFO "event_wait up\n"));
322 spin_lock_irqsave(&fib->event_lock, flagv);
323 if (!fib->done)
324 fib->done = 1;
325 up(&fib->event_wait);
326 spin_unlock_irqrestore(&fib->event_lock, flagv);
327 FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
329 return 0;