[SCSI] aacraid: initialization timeout
[safe/jmp/linux-2.6] / drivers / scsi / aacraid / commsup.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  *  commsup.c
26  *
27  * Abstract: Contain all routines that are required for FSA host/adapter
28  *    communication.
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 <scsi/scsi_host.h>
42 #include <scsi/scsi_device.h>
43 #include <asm/semaphore.h>
44 #include <asm/delay.h>
45
46 #include "aacraid.h"
47
48 /**
49  *      fib_map_alloc           -       allocate the fib objects
50  *      @dev: Adapter to allocate for
51  *
52  *      Allocate and map the shared PCI space for the FIB blocks used to
53  *      talk to the Adaptec firmware.
54  */
55  
56 static int fib_map_alloc(struct aac_dev *dev)
57 {
58         dprintk((KERN_INFO
59           "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
60           dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
61           AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
62         if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, dev->max_fib_size
63           * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
64           &dev->hw_fib_pa))==NULL)
65                 return -ENOMEM;
66         return 0;
67 }
68
69 /**
70  *      fib_map_free            -       free the fib objects
71  *      @dev: Adapter to free
72  *
73  *      Free the PCI mappings and the memory allocated for FIB blocks
74  *      on this adapter.
75  */
76
77 void fib_map_free(struct aac_dev *dev)
78 {
79         pci_free_consistent(dev->pdev, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB), dev->hw_fib_va, dev->hw_fib_pa);
80 }
81
82 /**
83  *      fib_setup       -       setup the fibs
84  *      @dev: Adapter to set up
85  *
86  *      Allocate the PCI space for the fibs, map it and then intialise the
87  *      fib area, the unmapped fib data and also the free list
88  */
89
90 int fib_setup(struct aac_dev * dev)
91 {
92         struct fib *fibptr;
93         struct hw_fib *hw_fib_va;
94         dma_addr_t hw_fib_pa;
95         int i;
96
97         while (((i = fib_map_alloc(dev)) == -ENOMEM)
98          && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
99                 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
100                 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
101         }
102         if (i<0)
103                 return -ENOMEM;
104                 
105         hw_fib_va = dev->hw_fib_va;
106         hw_fib_pa = dev->hw_fib_pa;
107         memset(hw_fib_va, 0, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
108         /*
109          *      Initialise the fibs
110          */
111         for (i = 0, fibptr = &dev->fibs[i]; i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); i++, fibptr++) 
112         {
113                 fibptr->dev = dev;
114                 fibptr->hw_fib = hw_fib_va;
115                 fibptr->data = (void *) fibptr->hw_fib->data;
116                 fibptr->next = fibptr+1;        /* Forward chain the fibs */
117                 init_MUTEX_LOCKED(&fibptr->event_wait);
118                 spin_lock_init(&fibptr->event_lock);
119                 hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
120                 hw_fib_va->header.SenderSize = cpu_to_le16(dev->max_fib_size);
121                 fibptr->hw_fib_pa = hw_fib_pa;
122                 hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + dev->max_fib_size);
123                 hw_fib_pa = hw_fib_pa + dev->max_fib_size;
124         }
125         /*
126          *      Add the fib chain to the free list
127          */
128         dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
129         /*
130          *      Enable this to debug out of queue space
131          */
132         dev->free_fib = &dev->fibs[0];
133         return 0;
134 }
135
136 /**
137  *      fib_alloc       -       allocate a fib
138  *      @dev: Adapter to allocate the fib for
139  *
140  *      Allocate a fib from the adapter fib pool. If the pool is empty we
141  *      return NULL.
142  */
143  
144 struct fib * fib_alloc(struct aac_dev *dev)
145 {
146         struct fib * fibptr;
147         unsigned long flags;
148         spin_lock_irqsave(&dev->fib_lock, flags);
149         fibptr = dev->free_fib; 
150         if(!fibptr){
151                 spin_unlock_irqrestore(&dev->fib_lock, flags);
152                 return fibptr;
153         }
154         dev->free_fib = fibptr->next;
155         spin_unlock_irqrestore(&dev->fib_lock, flags);
156         /*
157          *      Set the proper node type code and node byte size
158          */
159         fibptr->type = FSAFS_NTC_FIB_CONTEXT;
160         fibptr->size = sizeof(struct fib);
161         /*
162          *      Null out fields that depend on being zero at the start of
163          *      each I/O
164          */
165         fibptr->hw_fib->header.XferState = 0;
166         fibptr->callback = NULL;
167         fibptr->callback_data = NULL;
168
169         return fibptr;
170 }
171
172 /**
173  *      fib_free        -       free a fib
174  *      @fibptr: fib to free up
175  *
176  *      Frees up a fib and places it on the appropriate queue
177  *      (either free or timed out)
178  */
179  
180 void fib_free(struct fib * fibptr)
181 {
182         unsigned long flags;
183
184         spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
185         if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
186                 aac_config.fib_timeouts++;
187                 fibptr->next = fibptr->dev->timeout_fib;
188                 fibptr->dev->timeout_fib = fibptr;
189         } else {
190                 if (fibptr->hw_fib->header.XferState != 0) {
191                         printk(KERN_WARNING "fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n", 
192                                  (void*)fibptr, 
193                                  le32_to_cpu(fibptr->hw_fib->header.XferState));
194                 }
195                 fibptr->next = fibptr->dev->free_fib;
196                 fibptr->dev->free_fib = fibptr;
197         }       
198         spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
199 }
200
201 /**
202  *      fib_init        -       initialise a fib
203  *      @fibptr: The fib to initialize
204  *      
205  *      Set up the generic fib fields ready for use
206  */
207  
208 void fib_init(struct fib *fibptr)
209 {
210         struct hw_fib *hw_fib = fibptr->hw_fib;
211
212         hw_fib->header.StructType = FIB_MAGIC;
213         hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
214         hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
215         hw_fib->header.SenderFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
216         hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
217         hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
218 }
219
220 /**
221  *      fib_deallocate          -       deallocate a fib
222  *      @fibptr: fib to deallocate
223  *
224  *      Will deallocate and return to the free pool the FIB pointed to by the
225  *      caller.
226  */
227  
228 static void fib_dealloc(struct fib * fibptr)
229 {
230         struct hw_fib *hw_fib = fibptr->hw_fib;
231         if(hw_fib->header.StructType != FIB_MAGIC) 
232                 BUG();
233         hw_fib->header.XferState = 0;        
234 }
235
236 /*
237  *      Commuication primitives define and support the queuing method we use to
238  *      support host to adapter commuication. All queue accesses happen through
239  *      these routines and are the only routines which have a knowledge of the
240  *       how these queues are implemented.
241  */
242  
243 /**
244  *      aac_get_entry           -       get a queue entry
245  *      @dev: Adapter
246  *      @qid: Queue Number
247  *      @entry: Entry return
248  *      @index: Index return
249  *      @nonotify: notification control
250  *
251  *      With a priority the routine returns a queue entry if the queue has free entries. If the queue
252  *      is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
253  *      returned.
254  */
255  
256 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
257 {
258         struct aac_queue * q;
259         unsigned long idx;
260
261         /*
262          *      All of the queues wrap when they reach the end, so we check
263          *      to see if they have reached the end and if they have we just
264          *      set the index back to zero. This is a wrap. You could or off
265          *      the high bits in all updates but this is a bit faster I think.
266          */
267
268         q = &dev->queues->queue[qid];
269
270         idx = *index = le32_to_cpu(*(q->headers.producer));
271         /* Interrupt Moderation, only interrupt for first two entries */
272         if (idx != le32_to_cpu(*(q->headers.consumer))) {
273                 if (--idx == 0) {
274                         if (qid == AdapHighCmdQueue)
275                                 idx = ADAP_HIGH_CMD_ENTRIES;
276                         else if (qid == AdapNormCmdQueue)
277                                 idx = ADAP_NORM_CMD_ENTRIES;
278                         else if (qid == AdapHighRespQueue) 
279                                 idx = ADAP_HIGH_RESP_ENTRIES;
280                         else if (qid == AdapNormRespQueue) 
281                                 idx = ADAP_NORM_RESP_ENTRIES;
282                 }
283                 if (idx != le32_to_cpu(*(q->headers.consumer)))
284                         *nonotify = 1; 
285         }
286
287         if (qid == AdapHighCmdQueue) {
288                 if (*index >= ADAP_HIGH_CMD_ENTRIES)
289                         *index = 0;
290         } else if (qid == AdapNormCmdQueue) {
291                 if (*index >= ADAP_NORM_CMD_ENTRIES) 
292                         *index = 0; /* Wrap to front of the Producer Queue. */
293         }
294         else if (qid == AdapHighRespQueue) 
295         {
296                 if (*index >= ADAP_HIGH_RESP_ENTRIES)
297                         *index = 0;
298         }
299         else if (qid == AdapNormRespQueue) 
300         {
301                 if (*index >= ADAP_NORM_RESP_ENTRIES) 
302                         *index = 0; /* Wrap to front of the Producer Queue. */
303         }
304         else {
305                 printk("aacraid: invalid qid\n");
306                 BUG();
307         }
308
309         if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
310                 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
311                                 qid, q->numpending);
312                 return 0;
313         } else {
314                 *entry = q->base + *index;
315                 return 1;
316         }
317 }   
318
319 /**
320  *      aac_queue_get           -       get the next free QE
321  *      @dev: Adapter
322  *      @index: Returned index
323  *      @priority: Priority of fib
324  *      @fib: Fib to associate with the queue entry
325  *      @wait: Wait if queue full
326  *      @fibptr: Driver fib object to go with fib
327  *      @nonotify: Don't notify the adapter
328  *
329  *      Gets the next free QE off the requested priorty adapter command
330  *      queue and associates the Fib with the QE. The QE represented by
331  *      index is ready to insert on the queue when this routine returns
332  *      success.
333  */
334
335 static int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
336 {
337         struct aac_entry * entry = NULL;
338         int map = 0;
339         struct aac_queue * q = &dev->queues->queue[qid];
340                 
341         spin_lock_irqsave(q->lock, q->SavedIrql);
342             
343         if (qid == AdapHighCmdQueue || qid == AdapNormCmdQueue) 
344         {
345                 /*  if no entries wait for some if caller wants to */
346                 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) 
347                 {
348                         printk(KERN_ERR "GetEntries failed\n");
349                 }
350                 /*
351                  *      Setup queue entry with a command, status and fib mapped
352                  */
353                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
354                 map = 1;
355         }
356         else if (qid == AdapHighRespQueue || qid == AdapNormRespQueue)
357         {
358                 while(!aac_get_entry(dev, qid, &entry, index, nonotify)) 
359                 {
360                         /* if no entries wait for some if caller wants to */
361                 }
362                 /*
363                  *      Setup queue entry with command, status and fib mapped
364                  */
365                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
366                 entry->addr = hw_fib->header.SenderFibAddress;
367                         /* Restore adapters pointer to the FIB */
368                 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;    /* Let the adapter now where to find its data */
369                 map = 0;
370         }
371         /*
372          *      If MapFib is true than we need to map the Fib and put pointers
373          *      in the queue entry.
374          */
375         if (map)
376                 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
377         return 0;
378 }
379
380
381 /**
382  *      aac_insert_entry        -       insert a queue entry
383  *      @dev: Adapter
384  *      @index: Index of entry to insert
385  *      @qid: Queue number
386  *      @nonotify: Suppress adapter notification
387  *
388  *      Gets the next free QE off the requested priorty adapter command
389  *      queue and associates the Fib with the QE. The QE represented by
390  *      index is ready to insert on the queue when this routine returns
391  *      success.
392  */
393  
394 static int aac_insert_entry(struct aac_dev * dev, u32 index, u32 qid, unsigned long nonotify) 
395 {
396         struct aac_queue * q = &dev->queues->queue[qid];
397
398         if(q == NULL)
399                 BUG();
400         *(q->headers.producer) = cpu_to_le32(index + 1);
401         spin_unlock_irqrestore(q->lock, q->SavedIrql);
402
403         if (qid == AdapHighCmdQueue ||
404             qid == AdapNormCmdQueue ||
405             qid == AdapHighRespQueue ||
406             qid == AdapNormRespQueue)
407         {
408                 if (!nonotify)
409                         aac_adapter_notify(dev, qid);
410         }
411         else
412                 printk("Suprise insert!\n");
413         return 0;
414 }
415
416 /*
417  *      Define the highest level of host to adapter communication routines. 
418  *      These routines will support host to adapter FS commuication. These 
419  *      routines have no knowledge of the commuication method used. This level
420  *      sends and receives FIBs. This level has no knowledge of how these FIBs
421  *      get passed back and forth.
422  */
423
424 /**
425  *      fib_send        -       send a fib to the adapter
426  *      @command: Command to send
427  *      @fibptr: The fib
428  *      @size: Size of fib data area
429  *      @priority: Priority of Fib
430  *      @wait: Async/sync select
431  *      @reply: True if a reply is wanted
432  *      @callback: Called with reply
433  *      @callback_data: Passed to callback
434  *
435  *      Sends the requested FIB to the adapter and optionally will wait for a
436  *      response FIB. If the caller does not wish to wait for a response than
437  *      an event to wait on must be supplied. This event will be set when a
438  *      response FIB is received from the adapter.
439  */
440  
441 int fib_send(u16 command, struct fib * fibptr, unsigned long size,  int priority, int wait, int reply, fib_callback callback, void * callback_data)
442 {
443         u32 index;
444         u32 qid;
445         struct aac_dev * dev = fibptr->dev;
446         unsigned long nointr = 0;
447         struct hw_fib * hw_fib = fibptr->hw_fib;
448         struct aac_queue * q;
449         unsigned long flags = 0;
450         if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
451                 return -EBUSY;
452         /*
453          *      There are 5 cases with the wait and reponse requested flags. 
454          *      The only invalid cases are if the caller requests to wait and
455          *      does not request a response and if the caller does not want a
456          *      response and the Fib is not allocated from pool. If a response
457          *      is not requesed the Fib will just be deallocaed by the DPC
458          *      routine when the response comes back from the adapter. No
459          *      further processing will be done besides deleting the Fib. We 
460          *      will have a debug mode where the adapter can notify the host
461          *      it had a problem and the host can log that fact.
462          */
463         if (wait && !reply) {
464                 return -EINVAL;
465         } else if (!wait && reply) {
466                 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
467                 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
468         } else if (!wait && !reply) {
469                 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
470                 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
471         } else if (wait && reply) {
472                 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
473                 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
474         } 
475         /*
476          *      Map the fib into 32bits by using the fib number
477          */
478
479         hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr-dev->fibs)) << 1);
480         hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
481         /*
482          *      Set FIB state to indicate where it came from and if we want a
483          *      response from the adapter. Also load the command from the
484          *      caller.
485          *
486          *      Map the hw fib pointer as a 32bit value
487          */
488         hw_fib->header.Command = cpu_to_le16(command);
489         hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
490         fibptr->hw_fib->header.Flags = 0;       /* 0 the flags field - internal only*/
491         /*
492          *      Set the size of the Fib we want to send to the adapter
493          */
494         hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
495         if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
496                 return -EMSGSIZE;
497         }                
498         /*
499          *      Get a queue entry connect the FIB to it and send an notify
500          *      the adapter a command is ready.
501          */
502         if (priority == FsaHigh) {
503                 hw_fib->header.XferState |= cpu_to_le32(HighPriority);
504                 qid = AdapHighCmdQueue;
505         } else {
506                 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
507                 qid = AdapNormCmdQueue;
508         }
509         q = &dev->queues->queue[qid];
510
511         if(wait)
512                 spin_lock_irqsave(&fibptr->event_lock, flags);
513         if(aac_queue_get( dev, &index, qid, hw_fib, 1, fibptr, &nointr)<0)
514                 return -EWOULDBLOCK;
515         dprintk((KERN_DEBUG "fib_send: inserting a queue entry at index %d.\n",index));
516         dprintk((KERN_DEBUG "Fib contents:.\n"));
517         dprintk((KERN_DEBUG "  Command =               %d.\n", hw_fib->header.Command));
518         dprintk((KERN_DEBUG "  XferState  =            %x.\n", hw_fib->header.XferState));
519         dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib));
520         dprintk((KERN_DEBUG "  hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
521         dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));
522         /*
523          *      Fill in the Callback and CallbackContext if we are not
524          *      going to wait.
525          */
526         if (!wait) {
527                 fibptr->callback = callback;
528                 fibptr->callback_data = callback_data;
529         }
530         FIB_COUNTER_INCREMENT(aac_config.FibsSent);
531         list_add_tail(&fibptr->queue, &q->pendingq);
532         q->numpending++;
533
534         fibptr->done = 0;
535         fibptr->flags = 0;
536
537         if(aac_insert_entry(dev, index, qid, (nointr & aac_config.irq_mod)) < 0)
538                 return -EWOULDBLOCK;
539         /*
540          *      If the caller wanted us to wait for response wait now. 
541          */
542     
543         if (wait) {
544                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
545                 /* Only set for first known interruptable command */
546                 if (wait < 0) {
547                         /*
548                          * *VERY* Dangerous to time out a command, the
549                          * assumption is made that we have no hope of
550                          * functioning because an interrupt routing or other
551                          * hardware failure has occurred.
552                          */
553                         unsigned long count = 36000000L; /* 3 minutes */
554                         unsigned long qflags;
555                         while (down_trylock(&fibptr->event_wait)) {
556                                 if (--count == 0) {
557                                         spin_lock_irqsave(q->lock, qflags);
558                                         q->numpending--;
559                                         list_del(&fibptr->queue);
560                                         spin_unlock_irqrestore(q->lock, qflags);
561                                         if (wait == -1) {
562                                                 printk(KERN_ERR "aacraid: fib_send: first asynchronous command timed out.\n"
563                                                   "Usually a result of a PCI interrupt routing problem;\n"
564                                                   "update mother board BIOS or consider utilizing one of\n"
565                                                   "the SAFE mode kernel options (acpi, apic etc)\n");
566                                         }
567                                         return -ETIMEDOUT;
568                                 }
569                                 udelay(5);
570                         }
571                 } else
572                         down(&fibptr->event_wait);
573                 if(fibptr->done == 0)
574                         BUG();
575                         
576                 if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)){
577                         return -ETIMEDOUT;
578                 } else {
579                         return 0;
580                 }
581         }
582         /*
583          *      If the user does not want a response than return success otherwise
584          *      return pending
585          */
586         if (reply)
587                 return -EINPROGRESS;
588         else
589                 return 0;
590 }
591
592 /** 
593  *      aac_consumer_get        -       get the top of the queue
594  *      @dev: Adapter
595  *      @q: Queue
596  *      @entry: Return entry
597  *
598  *      Will return a pointer to the entry on the top of the queue requested that
599  *      we are a consumer of, and return the address of the queue entry. It does
600  *      not change the state of the queue. 
601  */
602
603 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
604 {
605         u32 index;
606         int status;
607         if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
608                 status = 0;
609         } else {
610                 /*
611                  *      The consumer index must be wrapped if we have reached
612                  *      the end of the queue, else we just use the entry
613                  *      pointed to by the header index
614                  */
615                 if (le32_to_cpu(*q->headers.consumer) >= q->entries) 
616                         index = 0;              
617                 else
618                         index = le32_to_cpu(*q->headers.consumer);
619                 *entry = q->base + index;
620                 status = 1;
621         }
622         return(status);
623 }
624
625 /**
626  *      aac_consumer_free       -       free consumer entry
627  *      @dev: Adapter
628  *      @q: Queue
629  *      @qid: Queue ident
630  *
631  *      Frees up the current top of the queue we are a consumer of. If the
632  *      queue was full notify the producer that the queue is no longer full.
633  */
634
635 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
636 {
637         int wasfull = 0;
638         u32 notify;
639
640         if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
641                 wasfull = 1;
642         
643         if (le32_to_cpu(*q->headers.consumer) >= q->entries)
644                 *q->headers.consumer = cpu_to_le32(1);
645         else
646                 *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
647         
648         if (wasfull) {
649                 switch (qid) {
650
651                 case HostNormCmdQueue:
652                         notify = HostNormCmdNotFull;
653                         break;
654                 case HostHighCmdQueue:
655                         notify = HostHighCmdNotFull;
656                         break;
657                 case HostNormRespQueue:
658                         notify = HostNormRespNotFull;
659                         break;
660                 case HostHighRespQueue:
661                         notify = HostHighRespNotFull;
662                         break;
663                 default:
664                         BUG();
665                         return;
666                 }
667                 aac_adapter_notify(dev, notify);
668         }
669 }        
670
671 /**
672  *      fib_adapter_complete    -       complete adapter issued fib
673  *      @fibptr: fib to complete
674  *      @size: size of fib
675  *
676  *      Will do all necessary work to complete a FIB that was sent from
677  *      the adapter.
678  */
679
680 int fib_adapter_complete(struct fib * fibptr, unsigned short size)
681 {
682         struct hw_fib * hw_fib = fibptr->hw_fib;
683         struct aac_dev * dev = fibptr->dev;
684         unsigned long nointr = 0;
685         if (hw_fib->header.XferState == 0)
686                 return 0;
687         /*
688          *      If we plan to do anything check the structure type first.
689          */ 
690         if ( hw_fib->header.StructType != FIB_MAGIC ) {
691                 return -EINVAL;
692         }
693         /*
694          *      This block handles the case where the adapter had sent us a
695          *      command and we have finished processing the command. We
696          *      call completeFib when we are done processing the command 
697          *      and want to send a response back to the adapter. This will 
698          *      send the completed cdb to the adapter.
699          */
700         if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
701                 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
702                 if (hw_fib->header.XferState & cpu_to_le32(HighPriority)) {
703                         u32 index;
704                         if (size) 
705                         {
706                                 size += sizeof(struct aac_fibhdr);
707                                 if (size > le16_to_cpu(hw_fib->header.SenderSize))
708                                         return -EMSGSIZE;
709                                 hw_fib->header.Size = cpu_to_le16(size);
710                         }
711                         if(aac_queue_get(dev, &index, AdapHighRespQueue, hw_fib, 1, NULL, &nointr) < 0) {
712                                 return -EWOULDBLOCK;
713                         }
714                         if (aac_insert_entry(dev, index, AdapHighRespQueue,  (nointr & (int)aac_config.irq_mod)) != 0) {
715                         }
716                 } else if (hw_fib->header.XferState & 
717                                 cpu_to_le32(NormalPriority)) {
718                         u32 index;
719
720                         if (size) {
721                                 size += sizeof(struct aac_fibhdr);
722                                 if (size > le16_to_cpu(hw_fib->header.SenderSize)) 
723                                         return -EMSGSIZE;
724                                 hw_fib->header.Size = cpu_to_le16(size);
725                         }
726                         if (aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr) < 0) 
727                                 return -EWOULDBLOCK;
728                         if (aac_insert_entry(dev, index, AdapNormRespQueue, (nointr & (int)aac_config.irq_mod)) != 0) 
729                         {
730                         }
731                 }
732         }
733         else 
734         {
735                 printk(KERN_WARNING "fib_adapter_complete: Unknown xferstate detected.\n");
736                 BUG();
737         }   
738         return 0;
739 }
740
741 /**
742  *      fib_complete    -       fib completion handler
743  *      @fib: FIB to complete
744  *
745  *      Will do all necessary work to complete a FIB.
746  */
747  
748 int fib_complete(struct fib * fibptr)
749 {
750         struct hw_fib * hw_fib = fibptr->hw_fib;
751
752         /*
753          *      Check for a fib which has already been completed
754          */
755
756         if (hw_fib->header.XferState == 0)
757                 return 0;
758         /*
759          *      If we plan to do anything check the structure type first.
760          */ 
761
762         if (hw_fib->header.StructType != FIB_MAGIC)
763                 return -EINVAL;
764         /*
765          *      This block completes a cdb which orginated on the host and we 
766          *      just need to deallocate the cdb or reinit it. At this point the
767          *      command is complete that we had sent to the adapter and this
768          *      cdb could be reused.
769          */
770         if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
771                 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
772         {
773                 fib_dealloc(fibptr);
774         }
775         else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
776         {
777                 /*
778                  *      This handles the case when the host has aborted the I/O
779                  *      to the adapter because the adapter is not responding
780                  */
781                 fib_dealloc(fibptr);
782         } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
783                 fib_dealloc(fibptr);
784         } else {
785                 BUG();
786         }   
787         return 0;
788 }
789
790 /**
791  *      aac_printf      -       handle printf from firmware
792  *      @dev: Adapter
793  *      @val: Message info
794  *
795  *      Print a message passed to us by the controller firmware on the
796  *      Adaptec board
797  */
798
799 void aac_printf(struct aac_dev *dev, u32 val)
800 {
801         char *cp = dev->printfbuf;
802         if (dev->printf_enabled)
803         {
804                 int length = val & 0xffff;
805                 int level = (val >> 16) & 0xffff;
806                 
807                 /*
808                  *      The size of the printfbuf is set in port.c
809                  *      There is no variable or define for it
810                  */
811                 if (length > 255)
812                         length = 255;
813                 if (cp[length] != 0)
814                         cp[length] = 0;
815                 if (level == LOG_AAC_HIGH_ERROR)
816                         printk(KERN_WARNING "aacraid:%s", cp);
817                 else
818                         printk(KERN_INFO "aacraid:%s", cp);
819         }
820         memset(cp, 0,  256);
821 }
822
823
824 /**
825  *      aac_handle_aif          -       Handle a message from the firmware
826  *      @dev: Which adapter this fib is from
827  *      @fibptr: Pointer to fibptr from adapter
828  *
829  *      This routine handles a driver notify fib from the adapter and
830  *      dispatches it to the appropriate routine for handling.
831  */
832
833 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
834 {
835         struct hw_fib * hw_fib = fibptr->hw_fib;
836         struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
837         int busy;
838         u32 container;
839         struct scsi_device *device;
840         enum {
841                 NOTHING,
842                 DELETE,
843                 ADD,
844                 CHANGE
845         } device_config_needed;
846
847         /* Sniff for container changes */
848
849         if (!dev)
850                 return;
851         container = (u32)-1;
852
853         /*
854          *      We have set this up to try and minimize the number of
855          * re-configures that take place. As a result of this when
856          * certain AIF's come in we will set a flag waiting for another
857          * type of AIF before setting the re-config flag.
858          */
859         switch (le32_to_cpu(aifcmd->command)) {
860         case AifCmdDriverNotify:
861                 switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
862                 /*
863                  *      Morph or Expand complete
864                  */
865                 case AifDenMorphComplete:
866                 case AifDenVolumeExtendComplete:
867                         container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
868                         if (container >= dev->maximum_num_containers)
869                                 break;
870
871                         /*
872                          *      Find the Scsi_Device associated with the SCSI
873                          * address. Make sure we have the right array, and if
874                          * so set the flag to initiate a new re-config once we
875                          * see an AifEnConfigChange AIF come through.
876                          */
877
878                         if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
879                                 device = scsi_device_lookup(dev->scsi_host_ptr, 
880                                         CONTAINER_TO_CHANNEL(container), 
881                                         CONTAINER_TO_ID(container), 
882                                         CONTAINER_TO_LUN(container));
883                                 if (device) {
884                                         dev->fsa_dev[container].config_needed = CHANGE;
885                                         dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
886                                         scsi_device_put(device);
887                                 }
888                         }
889                 }
890
891                 /*
892                  *      If we are waiting on something and this happens to be
893                  * that thing then set the re-configure flag.
894                  */
895                 if (container != (u32)-1) {
896                         if (container >= dev->maximum_num_containers)
897                                 break;
898                         if (dev->fsa_dev[container].config_waiting_on ==
899                             le32_to_cpu(*(u32 *)aifcmd->data))
900                                 dev->fsa_dev[container].config_waiting_on = 0;
901                 } else for (container = 0;
902                     container < dev->maximum_num_containers; ++container) {
903                         if (dev->fsa_dev[container].config_waiting_on ==
904                             le32_to_cpu(*(u32 *)aifcmd->data))
905                                 dev->fsa_dev[container].config_waiting_on = 0;
906                 }
907                 break;
908
909         case AifCmdEventNotify:
910                 switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
911                 /*
912                  *      Add an Array.
913                  */
914                 case AifEnAddContainer:
915                         container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
916                         if (container >= dev->maximum_num_containers)
917                                 break;
918                         dev->fsa_dev[container].config_needed = ADD;
919                         dev->fsa_dev[container].config_waiting_on =
920                                 AifEnConfigChange;
921                         break;
922
923                 /*
924                  *      Delete an Array.
925                  */
926                 case AifEnDeleteContainer:
927                         container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
928                         if (container >= dev->maximum_num_containers)
929                                 break;
930                         dev->fsa_dev[container].config_needed = DELETE;
931                         dev->fsa_dev[container].config_waiting_on =
932                                 AifEnConfigChange;
933                         break;
934
935                 /*
936                  *      Container change detected. If we currently are not
937                  * waiting on something else, setup to wait on a Config Change.
938                  */
939                 case AifEnContainerChange:
940                         container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
941                         if (container >= dev->maximum_num_containers)
942                                 break;
943                         if (dev->fsa_dev[container].config_waiting_on)
944                                 break;
945                         dev->fsa_dev[container].config_needed = CHANGE;
946                         dev->fsa_dev[container].config_waiting_on =
947                                 AifEnConfigChange;
948                         break;
949
950                 case AifEnConfigChange:
951                         break;
952
953                 }
954
955                 /*
956                  *      If we are waiting on something and this happens to be
957                  * that thing then set the re-configure flag.
958                  */
959                 if (container != (u32)-1) {
960                         if (container >= dev->maximum_num_containers)
961                                 break;
962                         if (dev->fsa_dev[container].config_waiting_on ==
963                             le32_to_cpu(*(u32 *)aifcmd->data))
964                                 dev->fsa_dev[container].config_waiting_on = 0;
965                 } else for (container = 0;
966                     container < dev->maximum_num_containers; ++container) {
967                         if (dev->fsa_dev[container].config_waiting_on ==
968                             le32_to_cpu(*(u32 *)aifcmd->data))
969                                 dev->fsa_dev[container].config_waiting_on = 0;
970                 }
971                 break;
972
973         case AifCmdJobProgress:
974                 /*
975                  *      These are job progress AIF's. When a Clear is being
976                  * done on a container it is initially created then hidden from
977                  * the OS. When the clear completes we don't get a config
978                  * change so we monitor the job status complete on a clear then
979                  * wait for a container change.
980                  */
981
982                 if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
983                  && ((((u32 *)aifcmd->data)[6] == ((u32 *)aifcmd->data)[5])
984                   || (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess)))) {
985                         for (container = 0;
986                             container < dev->maximum_num_containers;
987                             ++container) {
988                                 /*
989                                  * Stomp on all config sequencing for all
990                                  * containers?
991                                  */
992                                 dev->fsa_dev[container].config_waiting_on =
993                                         AifEnContainerChange;
994                                 dev->fsa_dev[container].config_needed = ADD;
995                         }
996                 }
997                 if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
998                  && (((u32 *)aifcmd->data)[6] == 0)
999                  && (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning))) {
1000                         for (container = 0;
1001                             container < dev->maximum_num_containers;
1002                             ++container) {
1003                                 /*
1004                                  * Stomp on all config sequencing for all
1005                                  * containers?
1006                                  */
1007                                 dev->fsa_dev[container].config_waiting_on =
1008                                         AifEnContainerChange;
1009                                 dev->fsa_dev[container].config_needed = DELETE;
1010                         }
1011                 }
1012                 break;
1013         }
1014
1015         device_config_needed = NOTHING;
1016         for (container = 0; container < dev->maximum_num_containers;
1017             ++container) {
1018                 if ((dev->fsa_dev[container].config_waiting_on == 0)
1019                  && (dev->fsa_dev[container].config_needed != NOTHING)) {
1020                         device_config_needed =
1021                                 dev->fsa_dev[container].config_needed;
1022                         dev->fsa_dev[container].config_needed = NOTHING;
1023                         break;
1024                 }
1025         }
1026         if (device_config_needed == NOTHING)
1027                 return;
1028
1029         /*
1030          *      If we decided that a re-configuration needs to be done,
1031          * schedule it here on the way out the door, please close the door
1032          * behind you.
1033          */
1034
1035         busy = 0;
1036
1037
1038         /*
1039          *      Find the Scsi_Device associated with the SCSI address,
1040          * and mark it as changed, invalidating the cache. This deals
1041          * with changes to existing device IDs.
1042          */
1043
1044         if (!dev || !dev->scsi_host_ptr)
1045                 return;
1046         /*
1047          * force reload of disk info via probe_container
1048          */
1049         if ((device_config_needed == CHANGE)
1050          && (dev->fsa_dev[container].valid == 1))
1051                 dev->fsa_dev[container].valid = 2;
1052         if ((device_config_needed == CHANGE) ||
1053                         (device_config_needed == ADD))
1054                 probe_container(dev, container);
1055         device = scsi_device_lookup(dev->scsi_host_ptr, 
1056                 CONTAINER_TO_CHANNEL(container), 
1057                 CONTAINER_TO_ID(container), 
1058                 CONTAINER_TO_LUN(container));
1059         if (device) {
1060                 switch (device_config_needed) {
1061                 case DELETE:
1062                         scsi_remove_device(device);
1063                         break;
1064                 case CHANGE:
1065                         if (!dev->fsa_dev[container].valid) {
1066                                 scsi_remove_device(device);
1067                                 break;
1068                         }
1069                         scsi_rescan_device(&device->sdev_gendev);
1070
1071                 default:
1072                         break;
1073                 }
1074                 scsi_device_put(device);
1075         }
1076         if (device_config_needed == ADD) {
1077                 scsi_add_device(dev->scsi_host_ptr,
1078                   CONTAINER_TO_CHANNEL(container),
1079                   CONTAINER_TO_ID(container),
1080                   CONTAINER_TO_LUN(container));
1081         }
1082
1083 }
1084
1085 /**
1086  *      aac_command_thread      -       command processing thread
1087  *      @dev: Adapter to monitor
1088  *
1089  *      Waits on the commandready event in it's queue. When the event gets set
1090  *      it will pull FIBs off it's queue. It will continue to pull FIBs off
1091  *      until the queue is empty. When the queue is empty it will wait for
1092  *      more FIBs.
1093  */
1094  
1095 int aac_command_thread(struct aac_dev * dev)
1096 {
1097         struct hw_fib *hw_fib, *hw_newfib;
1098         struct fib *fib, *newfib;
1099         struct aac_fib_context *fibctx;
1100         unsigned long flags;
1101         DECLARE_WAITQUEUE(wait, current);
1102
1103         /*
1104          *      We can only have one thread per adapter for AIF's.
1105          */
1106         if (dev->aif_thread)
1107                 return -EINVAL;
1108         /*
1109          *      Set up the name that will appear in 'ps'
1110          *      stored in  task_struct.comm[16].
1111          */
1112         daemonize("aacraid");
1113         allow_signal(SIGKILL);
1114         /*
1115          *      Let the DPC know it has a place to send the AIF's to.
1116          */
1117         dev->aif_thread = 1;
1118         add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1119         set_current_state(TASK_INTERRUPTIBLE);
1120         dprintk ((KERN_INFO "aac_command_thread start\n"));
1121         while(1) 
1122         {
1123                 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1124                 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1125                         struct list_head *entry;
1126                         struct aac_aifcmd * aifcmd;
1127
1128                         set_current_state(TASK_RUNNING);
1129         
1130                         entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1131                         list_del(entry);
1132                 
1133                         spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1134                         fib = list_entry(entry, struct fib, fiblink);
1135                         /*
1136                          *      We will process the FIB here or pass it to a 
1137                          *      worker thread that is TBD. We Really can't 
1138                          *      do anything at this point since we don't have
1139                          *      anything defined for this thread to do.
1140                          */
1141                         hw_fib = fib->hw_fib;
1142                         memset(fib, 0, sizeof(struct fib));
1143                         fib->type = FSAFS_NTC_FIB_CONTEXT;
1144                         fib->size = sizeof( struct fib );
1145                         fib->hw_fib = hw_fib;
1146                         fib->data = hw_fib->data;
1147                         fib->dev = dev;
1148                         /*
1149                          *      We only handle AifRequest fibs from the adapter.
1150                          */
1151                         aifcmd = (struct aac_aifcmd *) hw_fib->data;
1152                         if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1153                                 /* Handle Driver Notify Events */
1154                                 aac_handle_aif(dev, fib);
1155                                 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1156                                 fib_adapter_complete(fib, (u16)sizeof(u32));
1157                         } else {
1158                                 struct list_head *entry;
1159                                 /* The u32 here is important and intended. We are using
1160                                    32bit wrapping time to fit the adapter field */
1161                                    
1162                                 u32 time_now, time_last;
1163                                 unsigned long flagv;
1164                                 unsigned num;
1165                                 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1166                                 struct fib ** fib_pool, ** fib_p;
1167                         
1168                                 /* Sniff events */
1169                                 if ((aifcmd->command == 
1170                                      cpu_to_le32(AifCmdEventNotify)) ||
1171                                     (aifcmd->command == 
1172                                      cpu_to_le32(AifCmdJobProgress))) {
1173                                         aac_handle_aif(dev, fib);
1174                                 }
1175                                 
1176                                 time_now = jiffies/HZ;
1177
1178                                 /*
1179                                  * Warning: no sleep allowed while
1180                                  * holding spinlock. We take the estimate
1181                                  * and pre-allocate a set of fibs outside the
1182                                  * lock.
1183                                  */
1184                                 num = le32_to_cpu(dev->init->AdapterFibsSize)
1185                                     / sizeof(struct hw_fib); /* some extra */
1186                                 spin_lock_irqsave(&dev->fib_lock, flagv);
1187                                 entry = dev->fib_list.next;
1188                                 while (entry != &dev->fib_list) {
1189                                         entry = entry->next;
1190                                         ++num;
1191                                 }
1192                                 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1193                                 hw_fib_pool = NULL;
1194                                 fib_pool = NULL;
1195                                 if (num
1196                                  && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1197                                  && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1198                                         hw_fib_p = hw_fib_pool;
1199                                         fib_p = fib_pool;
1200                                         while (hw_fib_p < &hw_fib_pool[num]) {
1201                                                 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1202                                                         --hw_fib_p;
1203                                                         break;
1204                                                 }
1205                                                 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1206                                                         kfree(*(--hw_fib_p));
1207                                                         break;
1208                                                 }
1209                                         }
1210                                         if ((num = hw_fib_p - hw_fib_pool) == 0) {
1211                                                 kfree(fib_pool);
1212                                                 fib_pool = NULL;
1213                                                 kfree(hw_fib_pool);
1214                                                 hw_fib_pool = NULL;
1215                                         }
1216                                 } else if (hw_fib_pool) {
1217                                         kfree(hw_fib_pool);
1218                                         hw_fib_pool = NULL;
1219                                 }
1220                                 spin_lock_irqsave(&dev->fib_lock, flagv);
1221                                 entry = dev->fib_list.next;
1222                                 /*
1223                                  * For each Context that is on the 
1224                                  * fibctxList, make a copy of the
1225                                  * fib, and then set the event to wake up the
1226                                  * thread that is waiting for it.
1227                                  */
1228                                 hw_fib_p = hw_fib_pool;
1229                                 fib_p = fib_pool;
1230                                 while (entry != &dev->fib_list) {
1231                                         /*
1232                                          * Extract the fibctx
1233                                          */
1234                                         fibctx = list_entry(entry, struct aac_fib_context, next);
1235                                         /*
1236                                          * Check if the queue is getting
1237                                          * backlogged
1238                                          */
1239                                         if (fibctx->count > 20)
1240                                         {
1241                                                 /*
1242                                                  * It's *not* jiffies folks,
1243                                                  * but jiffies / HZ so do not
1244                                                  * panic ...
1245                                                  */
1246                                                 time_last = fibctx->jiffies;
1247                                                 /*
1248                                                  * Has it been > 2 minutes 
1249                                                  * since the last read off
1250                                                  * the queue?
1251                                                  */
1252                                                 if ((time_now - time_last) > 120) {
1253                                                         entry = entry->next;
1254                                                         aac_close_fib_context(dev, fibctx);
1255                                                         continue;
1256                                                 }
1257                                         }
1258                                         /*
1259                                          * Warning: no sleep allowed while
1260                                          * holding spinlock
1261                                          */
1262                                         if (hw_fib_p < &hw_fib_pool[num]) {
1263                                                 hw_newfib = *hw_fib_p;
1264                                                 *(hw_fib_p++) = NULL;
1265                                                 newfib = *fib_p;
1266                                                 *(fib_p++) = NULL;
1267                                                 /*
1268                                                  * Make the copy of the FIB
1269                                                  */
1270                                                 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1271                                                 memcpy(newfib, fib, sizeof(struct fib));
1272                                                 newfib->hw_fib = hw_newfib;
1273                                                 /*
1274                                                  * Put the FIB onto the
1275                                                  * fibctx's fibs
1276                                                  */
1277                                                 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1278                                                 fibctx->count++;
1279                                                 /* 
1280                                                  * Set the event to wake up the
1281                                                  * thread that is waiting.
1282                                                  */
1283                                                 up(&fibctx->wait_sem);
1284                                         } else {
1285                                                 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1286                                         }
1287                                         entry = entry->next;
1288                                 }
1289                                 /*
1290                                  *      Set the status of this FIB
1291                                  */
1292                                 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1293                                 fib_adapter_complete(fib, sizeof(u32));
1294                                 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1295                                 /* Free up the remaining resources */
1296                                 hw_fib_p = hw_fib_pool;
1297                                 fib_p = fib_pool;
1298                                 while (hw_fib_p < &hw_fib_pool[num]) {
1299                                         if (*hw_fib_p)
1300                                                 kfree(*hw_fib_p);
1301                                         if (*fib_p)
1302                                                 kfree(*fib_p);
1303                                         ++fib_p;
1304                                         ++hw_fib_p;
1305                                 }
1306                                 if (hw_fib_pool)
1307                                         kfree(hw_fib_pool);
1308                                 if (fib_pool)
1309                                         kfree(fib_pool);
1310                         }
1311                         kfree(fib);
1312                         spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1313                 }
1314                 /*
1315                  *      There are no more AIF's
1316                  */
1317                 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1318                 schedule();
1319
1320                 if(signal_pending(current))
1321                         break;
1322                 set_current_state(TASK_INTERRUPTIBLE);
1323         }
1324         if (dev->queues)
1325                 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1326         dev->aif_thread = 0;
1327         complete_and_exit(&dev->aif_completion, 0);
1328         return 0;
1329 }