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