66aeb57dcc2d1e9f00f6872f21004d2e15046ef7
[linux-2.6.git] / drivers / scsi / aacraid / dpcsup.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  *  dpcsup.c
26  *
27  * Abstract: All DPC processing routines for the cyclone board occur here.
28  *
29  *
30  */
31
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/spinlock.h>
36 #include <linux/slab.h>
37 #include <linux/completion.h>
38 #include <linux/blkdev.h>
39 #include <asm/semaphore.h>
40
41 #include "aacraid.h"
42
43 /**
44  *      aac_response_normal     -       Handle command replies
45  *      @q: Queue to read from
46  *
47  *      This DPC routine will be run when the adapter interrupts us to let us
48  *      know there is a response on our normal priority queue. We will pull off
49  *      all QE there are and wake up all the waiters before exiting. We will
50  *      take a spinlock out on the queue before operating on it.
51  */
52
53 unsigned int aac_response_normal(struct aac_queue * q)
54 {
55         struct aac_dev * dev = q->dev;
56         struct aac_entry *entry;
57         struct hw_fib * hwfib;
58         struct fib * fib;
59         int consumed = 0;
60         unsigned long flags;
61
62         spin_lock_irqsave(q->lock, flags);      
63         /*
64          *      Keep pulling response QEs off the response queue and waking
65          *      up the waiters until there are no more QEs. We then return
66          *      back to the system. If no response was requesed we just
67          *      deallocate the Fib here and continue.
68          */
69         while(aac_consumer_get(dev, q, &entry))
70         {
71                 int fast;
72                 u32 index = le32_to_cpu(entry->addr);
73                 fast = index & 0x01;
74                 fib = &dev->fibs[index >> 2];
75                 hwfib = fib->hw_fib;
76                 
77                 aac_consumer_free(dev, q, HostNormRespQueue);
78                 /*
79                  *      Remove this fib from the Outstanding I/O queue.
80                  *      But only if it has not already been timed out.
81                  *
82                  *      If the fib has been timed out already, then just 
83                  *      continue. The caller has already been notified that
84                  *      the fib timed out.
85                  */
86                 if (!(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
87                         dev->queues->queue[AdapNormCmdQueue].numpending--;
88                 else {
89                         printk(KERN_WARNING "aacraid: FIB timeout (%x).\n", fib->flags);
90                         printk(KERN_DEBUG"aacraid: hwfib=%p fib index=%i fib=%p\n",hwfib, hwfib->header.SenderData,fib);
91                         continue;
92                 }
93                 spin_unlock_irqrestore(q->lock, flags);
94
95                 if (fast) {
96                         /*
97                          *      Doctor the fib
98                          */
99                         *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
100                         hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
101                 }
102
103                 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
104
105                 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
106                 {
107                         __le32 *pstatus = (__le32 *)hwfib->data;
108                         if (*pstatus & cpu_to_le32(0xffff0000))
109                                 *pstatus = cpu_to_le32(ST_OK);
110                 }
111                 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async)) 
112                 {
113                         if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
114                                 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
115                         else 
116                                 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
117                         /*
118                          *      NOTE:  we cannot touch the fib after this
119                          *          call, because it may have been deallocated.
120                          */
121                         fib->callback(fib->callback_data, fib);
122                 } else {
123                         unsigned long flagv;
124                         spin_lock_irqsave(&fib->event_lock, flagv);
125                         if (!fib->done)
126                                 fib->done = 1;
127                         up(&fib->event_wait);
128                         spin_unlock_irqrestore(&fib->event_lock, flagv);
129                         FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
130                         if (fib->done == 2) {
131                                 aac_fib_complete(fib);
132                                 aac_fib_free(fib);
133                         }
134                 }
135                 consumed++;
136                 spin_lock_irqsave(q->lock, flags);
137         }
138
139         if (consumed > aac_config.peak_fibs)
140                 aac_config.peak_fibs = consumed;
141         if (consumed == 0) 
142                 aac_config.zero_fibs++;
143
144         spin_unlock_irqrestore(q->lock, flags);
145         return 0;
146 }
147
148
149 /**
150  *      aac_command_normal      -       handle commands
151  *      @q: queue to process
152  *
153  *      This DPC routine will be queued when the adapter interrupts us to 
154  *      let us know there is a command on our normal priority queue. We will 
155  *      pull off all QE there are and wake up all the waiters before exiting.
156  *      We will take a spinlock out on the queue before operating on it.
157  */
158  
159 unsigned int aac_command_normal(struct aac_queue *q)
160 {
161         struct aac_dev * dev = q->dev;
162         struct aac_entry *entry;
163         unsigned long flags;
164
165         spin_lock_irqsave(q->lock, flags);
166
167         /*
168          *      Keep pulling response QEs off the response queue and waking
169          *      up the waiters until there are no more QEs. We then return
170          *      back to the system.
171          */
172         while(aac_consumer_get(dev, q, &entry))
173         {
174                 struct fib fibctx;
175                 struct hw_fib * hw_fib;
176                 u32 index;
177                 struct fib *fib = &fibctx;
178                 
179                 index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib);
180                 hw_fib = &dev->aif_base_va[index];
181                 
182                 /*
183                  *      Allocate a FIB at all costs. For non queued stuff
184                  *      we can just use the stack so we are happy. We need
185                  *      a fib object in order to manage the linked lists
186                  */
187                 if (dev->aif_thread)
188                         if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL)
189                                 fib = &fibctx;
190                 
191                 memset(fib, 0, sizeof(struct fib));
192                 INIT_LIST_HEAD(&fib->fiblink);
193                 fib->type = FSAFS_NTC_FIB_CONTEXT;
194                 fib->size = sizeof(struct fib);
195                 fib->hw_fib = hw_fib;
196                 fib->data = hw_fib->data;
197                 fib->dev = dev;
198                 
199                                 
200                 if (dev->aif_thread && fib != &fibctx) {
201                         list_add_tail(&fib->fiblink, &q->cmdq);
202                         aac_consumer_free(dev, q, HostNormCmdQueue);
203                         wake_up_interruptible(&q->cmdready);
204                 } else {
205                         aac_consumer_free(dev, q, HostNormCmdQueue);
206                         spin_unlock_irqrestore(q->lock, flags);
207                         /*
208                          *      Set the status of this FIB
209                          */
210                         *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
211                         aac_fib_adapter_complete(fib, sizeof(u32));
212                         spin_lock_irqsave(q->lock, flags);
213                 }               
214         }
215         spin_unlock_irqrestore(q->lock, flags);
216         return 0;
217 }
218
219
220 /**
221  *      aac_intr_normal -       Handle command replies
222  *      @dev: Device
223  *      @index: completion reference
224  *
225  *      This DPC routine will be run when the adapter interrupts us to let us
226  *      know there is a response on our normal priority queue. We will pull off
227  *      all QE there are and wake up all the waiters before exiting.
228  */
229
230 unsigned int aac_intr_normal(struct aac_dev * dev, u32 Index)
231 {
232         u32 index = le32_to_cpu(Index);
233
234         dprintk((KERN_INFO "aac_intr_normal(%p,%x)\n", dev, Index));
235         if ((index & 0x00000002L)) {
236                 struct hw_fib * hw_fib;
237                 struct fib * fib;
238                 struct aac_queue *q = &dev->queues->queue[HostNormCmdQueue];
239                 unsigned long flags;
240
241                 if (index == 0xFFFFFFFEL) /* Special Case */
242                         return 0;         /* Do nothing */
243                 /*
244                  *      Allocate a FIB. For non queued stuff we can just use
245                  * the stack so we are happy. We need a fib object in order to
246                  * manage the linked lists.
247                  */
248                 if ((!dev->aif_thread)
249                  || (!(fib = kmalloc(sizeof(struct fib),GFP_ATOMIC))))
250                         return 1;
251                 if (!(hw_fib = kmalloc(sizeof(struct hw_fib),GFP_ATOMIC))) {
252                         kfree (fib);
253                         return 1;
254                 }
255                 memset(hw_fib, 0, sizeof(struct hw_fib));
256                 memcpy(hw_fib, (struct hw_fib *)(((unsigned long)(dev->regs.sa)) + (index & ~0x00000002L)), sizeof(struct hw_fib));
257                 memset(fib, 0, sizeof(struct fib));
258                 INIT_LIST_HEAD(&fib->fiblink);
259                 fib->type = FSAFS_NTC_FIB_CONTEXT;
260                 fib->size = sizeof(struct fib);
261                 fib->hw_fib = hw_fib;
262                 fib->data = hw_fib->data;
263                 fib->dev = dev;
264         
265                 spin_lock_irqsave(q->lock, flags);
266                 list_add_tail(&fib->fiblink, &q->cmdq);
267                 wake_up_interruptible(&q->cmdready);
268                 spin_unlock_irqrestore(q->lock, flags);
269                 return 1;
270         } else {
271                 int fast = index & 0x01;
272                 struct fib * fib = &dev->fibs[index >> 2];
273                 struct hw_fib * hwfib = fib->hw_fib;
274
275                 /*
276                  *      Remove this fib from the Outstanding I/O queue.
277                  *      But only if it has not already been timed out.
278                  *
279                  *      If the fib has been timed out already, then just 
280                  *      continue. The caller has already been notified that
281                  *      the fib timed out.
282                  */
283                 if ((fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
284                         printk(KERN_WARNING "aacraid: FIB timeout (%x).\n", fib->flags);
285                         printk(KERN_DEBUG"aacraid: hwfib=%p index=%i fib=%p\n",hwfib, hwfib->header.SenderData,fib);
286                         return 0;
287                 }
288
289                 dev->queues->queue[AdapNormCmdQueue].numpending--;
290
291                 if (fast) {
292                         /*
293                          *      Doctor the fib
294                          */
295                         *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
296                         hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
297                 }
298
299                 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
300
301                 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
302                 {
303                         u32 *pstatus = (u32 *)hwfib->data;
304                         if (*pstatus & cpu_to_le32(0xffff0000))
305                                 *pstatus = cpu_to_le32(ST_OK);
306                 }
307                 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async)) 
308                 {
309                         if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
310                                 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
311                         else 
312                                 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
313                         /*
314                          *      NOTE:  we cannot touch the fib after this
315                          *          call, because it may have been deallocated.
316                          */
317                         fib->callback(fib->callback_data, fib);
318                 } else {
319                         unsigned long flagv;
320                         dprintk((KERN_INFO "event_wait up\n"));
321                         spin_lock_irqsave(&fib->event_lock, flagv);
322                         if (!fib->done)
323                                 fib->done = 1;
324                         up(&fib->event_wait);
325                         spin_unlock_irqrestore(&fib->event_lock, flagv);
326                         FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
327                 }
328                 return 0;
329         }
330 }