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-2007 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)
 
  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.
 
  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/spinlock.h>
 
  36 #include <linux/slab.h>
 
  37 #include <linux/completion.h>
 
  38 #include <linux/blkdev.h>
 
  39 #include <asm/semaphore.h>
 
  44  *      aac_response_normal     -       Handle command replies
 
  45  *      @q: Queue to read from
 
  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.
 
  53 unsigned int aac_response_normal(struct aac_queue * q)
 
  55         struct aac_dev * dev = q->dev;
 
  56         struct aac_entry *entry;
 
  57         struct hw_fib * hwfib;
 
  62         spin_lock_irqsave(q->lock, flags);      
 
  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.
 
  69         while(aac_consumer_get(dev, q, &entry))
 
  72                 u32 index = le32_to_cpu(entry->addr);
 
  74                 fib = &dev->fibs[index >> 2];
 
  75                 hwfib = fib->hw_fib_va;
 
  77                 aac_consumer_free(dev, q, HostNormRespQueue);
 
  79                  *      Remove this fib from the Outstanding I/O queue.
 
  80                  *      But only if it has not already been timed out.
 
  82                  *      If the fib has been timed out already, then just 
 
  83                  *      continue. The caller has already been notified that
 
  86                 dev->queues->queue[AdapNormCmdQueue].numpending--;
 
  88                 if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
 
  89                         spin_unlock_irqrestore(q->lock, flags);
 
  90                         aac_fib_complete(fib);
 
  92                         spin_lock_irqsave(q->lock, flags);
 
  95                 spin_unlock_irqrestore(q->lock, flags);
 
 101                         *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
 
 102                         hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
 
 105                 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
 
 107                 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
 
 109                         __le32 *pstatus = (__le32 *)hwfib->data;
 
 110                         if (*pstatus & cpu_to_le32(0xffff0000))
 
 111                                 *pstatus = cpu_to_le32(ST_OK);
 
 113                 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async)) 
 
 115                         if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
 
 116                                 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
 
 118                                 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
 
 120                          *      NOTE:  we cannot touch the fib after this
 
 121                          *          call, because it may have been deallocated.
 
 124                         fib->callback(fib->callback_data, fib);
 
 127                         spin_lock_irqsave(&fib->event_lock, flagv);
 
 130                         up(&fib->event_wait);
 
 131                         spin_unlock_irqrestore(&fib->event_lock, flagv);
 
 132                         FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
 
 133                         if (fib->done == 2) {
 
 134                                 aac_fib_complete(fib);
 
 139                 spin_lock_irqsave(q->lock, flags);
 
 142         if (consumed > aac_config.peak_fibs)
 
 143                 aac_config.peak_fibs = consumed;
 
 145                 aac_config.zero_fibs++;
 
 147         spin_unlock_irqrestore(q->lock, flags);
 
 153  *      aac_command_normal      -       handle commands
 
 154  *      @q: queue to process
 
 156  *      This DPC routine will be queued when the adapter interrupts us to 
 
 157  *      let us know there is a command on our normal priority queue. We will 
 
 158  *      pull off all QE there are and wake up all the waiters before exiting.
 
 159  *      We will take a spinlock out on the queue before operating on it.
 
 162 unsigned int aac_command_normal(struct aac_queue *q)
 
 164         struct aac_dev * dev = q->dev;
 
 165         struct aac_entry *entry;
 
 168         spin_lock_irqsave(q->lock, flags);
 
 171          *      Keep pulling response QEs off the response queue and waking
 
 172          *      up the waiters until there are no more QEs. We then return
 
 173          *      back to the system.
 
 175         while(aac_consumer_get(dev, q, &entry))
 
 178                 struct hw_fib * hw_fib;
 
 180                 struct fib *fib = &fibctx;
 
 182                 index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib);
 
 183                 hw_fib = &dev->aif_base_va[index];
 
 186                  *      Allocate a FIB at all costs. For non queued stuff
 
 187                  *      we can just use the stack so we are happy. We need
 
 188                  *      a fib object in order to manage the linked lists
 
 191                         if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL)
 
 194                 memset(fib, 0, sizeof(struct fib));
 
 195                 INIT_LIST_HEAD(&fib->fiblink);
 
 196                 fib->type = FSAFS_NTC_FIB_CONTEXT;
 
 197                 fib->size = sizeof(struct fib);
 
 198                 fib->hw_fib_va = hw_fib;
 
 199                 fib->data = hw_fib->data;
 
 203                 if (dev->aif_thread && fib != &fibctx) {
 
 204                         list_add_tail(&fib->fiblink, &q->cmdq);
 
 205                         aac_consumer_free(dev, q, HostNormCmdQueue);
 
 206                         wake_up_interruptible(&q->cmdready);
 
 208                         aac_consumer_free(dev, q, HostNormCmdQueue);
 
 209                         spin_unlock_irqrestore(q->lock, flags);
 
 211                          *      Set the status of this FIB
 
 213                         *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
 
 214                         aac_fib_adapter_complete(fib, sizeof(u32));
 
 215                         spin_lock_irqsave(q->lock, flags);
 
 218         spin_unlock_irqrestore(q->lock, flags);
 
 224  *      aac_intr_normal -       Handle command replies
 
 226  *      @index: completion reference
 
 228  *      This DPC routine will be run when the adapter interrupts us to let us
 
 229  *      know there is a response on our normal priority queue. We will pull off
 
 230  *      all QE there are and wake up all the waiters before exiting.
 
 233 unsigned int aac_intr_normal(struct aac_dev * dev, u32 index)
 
 235         dprintk((KERN_INFO "aac_intr_normal(%p,%x)\n", dev, index));
 
 236         if ((index & 0x00000002L)) {
 
 237                 struct hw_fib * hw_fib;
 
 239                 struct aac_queue *q = &dev->queues->queue[HostNormCmdQueue];
 
 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 = kzalloc(sizeof(struct fib),GFP_ATOMIC))))
 
 252                 if (!(hw_fib = kzalloc(sizeof(struct hw_fib),GFP_ATOMIC))) {
 
 256                 memcpy(hw_fib, (struct hw_fib *)(((uintptr_t)(dev->regs.sa)) +
 
 257                   (index & ~0x00000002L)), sizeof(struct hw_fib));
 
 258                 INIT_LIST_HEAD(&fib->fiblink);
 
 259                 fib->type = FSAFS_NTC_FIB_CONTEXT;
 
 260                 fib->size = sizeof(struct fib);
 
 261                 fib->hw_fib_va = hw_fib;
 
 262                 fib->data = hw_fib->data;
 
 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);
 
 271                 int fast = index & 0x01;
 
 272                 struct fib * fib = &dev->fibs[index >> 2];
 
 273                 struct hw_fib * hwfib = fib->hw_fib_va;
 
 276                  *      Remove this fib from the Outstanding I/O queue.
 
 277                  *      But only if it has not already been timed out.
 
 279                  *      If the fib has been timed out already, then just 
 
 280                  *      continue. The caller has already been notified that
 
 283                 dev->queues->queue[AdapNormCmdQueue].numpending--;
 
 285                 if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
 
 286                         aac_fib_complete(fib);
 
 295                         *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
 
 296                         hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
 
 299                 FIB_COUNTER_INCREMENT(aac_config.FibRecved);
 
 301                 if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
 
 303                         __le32 *pstatus = (__le32 *)hwfib->data;
 
 304                         if (*pstatus & cpu_to_le32(0xffff0000))
 
 305                                 *pstatus = cpu_to_le32(ST_OK);
 
 307                 if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async)) 
 
 309                         if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
 
 310                                 FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
 
 312                                 FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
 
 314                          *      NOTE:  we cannot touch the fib after this
 
 315                          *          call, because it may have been deallocated.
 
 318                         fib->callback(fib->callback_data, fib);
 
 321                         dprintk((KERN_INFO "event_wait up\n"));
 
 322                         spin_lock_irqsave(&fib->event_lock, flagv);
 
 325                         up(&fib->event_wait);
 
 326                         spin_unlock_irqrestore(&fib->event_lock, flagv);
 
 327                         FIB_COUNTER_INCREMENT(aac_config.NormalRecved);