[SCSI] aacraid: add sysfs report of RAID level
[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-2007 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_va;
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                 dev->queues->queue[AdapNormCmdQueue].numpending--;
87
88                 if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
89                         spin_unlock_irqrestore(q->lock, flags);
90                         aac_fib_complete(fib);
91                         aac_fib_free(fib);
92                         spin_lock_irqsave(q->lock, flags);
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                         if (!fib->done)
128                                 fib->done = 1;
129                         up(&fib->event_wait);
130                         spin_unlock_irqrestore(&fib->event_lock, flagv);
131                         FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
132                         if (fib->done == 2) {
133                                 aac_fib_complete(fib);
134                                 aac_fib_free(fib);
135                         }
136                 }
137                 consumed++;
138                 spin_lock_irqsave(q->lock, flags);
139         }
140
141         if (consumed > aac_config.peak_fibs)
142                 aac_config.peak_fibs = consumed;
143         if (consumed == 0) 
144                 aac_config.zero_fibs++;
145
146         spin_unlock_irqrestore(q->lock, flags);
147         return 0;
148 }
149
150
151 /**
152  *      aac_command_normal      -       handle commands
153  *      @q: queue to process
154  *
155  *      This DPC routine will be queued when the adapter interrupts us to 
156  *      let us know there is a command on our normal priority queue. We will 
157  *      pull off all QE there are and wake up all the waiters before exiting.
158  *      We will take a spinlock out on the queue before operating on it.
159  */
160  
161 unsigned int aac_command_normal(struct aac_queue *q)
162 {
163         struct aac_dev * dev = q->dev;
164         struct aac_entry *entry;
165         unsigned long flags;
166
167         spin_lock_irqsave(q->lock, flags);
168
169         /*
170          *      Keep pulling response QEs off the response queue and waking
171          *      up the waiters until there are no more QEs. We then return
172          *      back to the system.
173          */
174         while(aac_consumer_get(dev, q, &entry))
175         {
176                 struct fib fibctx;
177                 struct hw_fib * hw_fib;
178                 u32 index;
179                 struct fib *fib = &fibctx;
180                 
181                 index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib);
182                 hw_fib = &dev->aif_base_va[index];
183                 
184                 /*
185                  *      Allocate a FIB at all costs. For non queued stuff
186                  *      we can just use the stack so we are happy. We need
187                  *      a fib object in order to manage the linked lists
188                  */
189                 if (dev->aif_thread)
190                         if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL)
191                                 fib = &fibctx;
192                 
193                 memset(fib, 0, sizeof(struct fib));
194                 INIT_LIST_HEAD(&fib->fiblink);
195                 fib->type = FSAFS_NTC_FIB_CONTEXT;
196                 fib->size = sizeof(struct fib);
197                 fib->hw_fib_va = hw_fib;
198                 fib->data = hw_fib->data;
199                 fib->dev = dev;
200                 
201                                 
202                 if (dev->aif_thread && fib != &fibctx) {
203                         list_add_tail(&fib->fiblink, &q->cmdq);
204                         aac_consumer_free(dev, q, HostNormCmdQueue);
205                         wake_up_interruptible(&q->cmdready);
206                 } else {
207                         aac_consumer_free(dev, q, HostNormCmdQueue);
208                         spin_unlock_irqrestore(q->lock, flags);
209                         /*
210                          *      Set the status of this FIB
211                          */
212                         *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
213                         aac_fib_adapter_complete(fib, sizeof(u32));
214                         spin_lock_irqsave(q->lock, flags);
215                 }               
216         }
217         spin_unlock_irqrestore(q->lock, flags);
218         return 0;
219 }
220
221
222 /**
223  *      aac_intr_normal -       Handle command replies
224  *      @dev: Device
225  *      @index: completion reference
226  *
227  *      This DPC routine will be run when the adapter interrupts us to let us
228  *      know there is a response on our normal priority queue. We will pull off
229  *      all QE there are and wake up all the waiters before exiting.
230  */
231
232 unsigned int aac_intr_normal(struct aac_dev *dev, u32 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 = kzalloc(sizeof(struct fib),GFP_ATOMIC))))
250                         return 1;
251                 if (!(hw_fib = kzalloc(sizeof(struct hw_fib),GFP_ATOMIC))) {
252                         kfree (fib);
253                         return 1;
254                 }
255                 memcpy(hw_fib, (struct hw_fib *)(((uintptr_t)(dev->regs.sa)) +
256                   (index & ~0x00000002L)), sizeof(struct hw_fib));
257                 INIT_LIST_HEAD(&fib->fiblink);
258                 fib->type = FSAFS_NTC_FIB_CONTEXT;
259                 fib->size = sizeof(struct fib);
260                 fib->hw_fib_va = hw_fib;
261                 fib->data = hw_fib->data;
262                 fib->dev = dev;
263         
264                 spin_lock_irqsave(q->lock, flags);
265                 list_add_tail(&fib->fiblink, &q->cmdq);
266                 wake_up_interruptible(&q->cmdready);
267                 spin_unlock_irqrestore(q->lock, flags);
268                 return 1;
269         } else {
270                 int fast = index & 0x01;
271                 struct fib * fib = &dev->fibs[index >> 2];
272                 struct hw_fib * hwfib = fib->hw_fib_va;
273
274                 /*
275                  *      Remove this fib from the Outstanding I/O queue.
276                  *      But only if it has not already been timed out.
277                  *
278                  *      If the fib has been timed out already, then just 
279                  *      continue. The caller has already been notified that
280                  *      the fib timed out.
281                  */
282                 dev->queues->queue[AdapNormCmdQueue].numpending--;
283
284                 if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
285                         aac_fib_complete(fib);
286                         aac_fib_free(fib);
287                         return 0;
288                 }
289
290                 if (fast) {
291                         /*
292                          *      Doctor the fib
293                          */
294                         *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
295                         hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
296                 }
297
298                 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
299
300                 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
301                 {
302                         __le32 *pstatus = (__le32 *)hwfib->data;
303                         if (*pstatus & cpu_to_le32(0xffff0000))
304                                 *pstatus = cpu_to_le32(ST_OK);
305                 }
306                 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async)) 
307                 {
308                         if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
309                                 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
310                         else 
311                                 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
312                         /*
313                          *      NOTE:  we cannot touch the fib after this
314                          *          call, because it may have been deallocated.
315                          */
316                         fib->callback(fib->callback_data, fib);
317                 } else {
318                         unsigned long flagv;
319                         dprintk((KERN_INFO "event_wait up\n"));
320                         spin_lock_irqsave(&fib->event_lock, flagv);
321                         if (!fib->done)
322                                 fib->done = 1;
323                         up(&fib->event_wait);
324                         spin_unlock_irqrestore(&fib->event_lock, flagv);
325                         FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
326                 }
327                 return 0;
328         }
329 }