ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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  *    commuication.
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  *      fib_map_alloc           -       allocate the fib objects
47  *      @dev: Adapter to allocate for
48  *
49  *      Allocate and map the shared PCI space for the FIB blocks used to
50  *      talk to the Adaptec firmware.
51  */
52  
53 static int fib_map_alloc(struct aac_dev *dev)
54 {
55         if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, &dev->hw_fib_pa))==NULL)
56                 return -ENOMEM;
57         return 0;
58 }
59
60 /**
61  *      fib_map_free            -       free the fib objects
62  *      @dev: Adapter to free
63  *
64  *      Free the PCI mappings and the memory allocated for FIB blocks
65  *      on this adapter.
66  */
67
68 void fib_map_free(struct aac_dev *dev)
69 {
70         pci_free_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, dev->hw_fib_va, dev->hw_fib_pa);
71 }
72
73 /**
74  *      fib_setup       -       setup the fibs
75  *      @dev: Adapter to set up
76  *
77  *      Allocate the PCI space for the fibs, map it and then intialise the
78  *      fib area, the unmapped fib data and also the free list
79  */
80
81 int fib_setup(struct aac_dev * dev)
82 {
83         struct fib *fibptr;
84         struct hw_fib *hw_fib_va;
85         dma_addr_t hw_fib_pa;
86         int i;
87         
88         if(fib_map_alloc(dev)<0)
89                 return -ENOMEM;
90                 
91         hw_fib_va = dev->hw_fib_va;
92         hw_fib_pa = dev->hw_fib_pa;
93         memset(hw_fib_va, 0, sizeof(struct hw_fib) * AAC_NUM_FIB);
94         /*
95          *      Initialise the fibs
96          */
97         for (i = 0, fibptr = &dev->fibs[i]; i < AAC_NUM_FIB; i++, fibptr++) 
98         {
99                 fibptr->dev = dev;
100                 fibptr->hw_fib = hw_fib_va;
101                 fibptr->data = (void *) fibptr->hw_fib->data;
102                 fibptr->next = fibptr+1;        /* Forward chain the fibs */
103                 init_MUTEX_LOCKED(&fibptr->event_wait);
104                 spin_lock_init(&fibptr->event_lock);
105                 hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
106                 hw_fib_va->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
107                 fibptr->hw_fib_pa = hw_fib_pa;
108                 hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + sizeof(struct hw_fib));
109                 hw_fib_pa = hw_fib_pa + sizeof(struct hw_fib); 
110         }
111         /*
112          *      Add the fib chain to the free list
113          */
114         dev->fibs[AAC_NUM_FIB-1].next = NULL;
115         /*
116          *      Enable this to debug out of queue space
117          */
118         dev->free_fib = &dev->fibs[0];
119         return 0;
120 }
121
122 /**
123  *      fib_alloc       -       allocate a fib
124  *      @dev: Adapter to allocate the fib for
125  *
126  *      Allocate a fib from the adapter fib pool. If the pool is empty we
127  *      wait for fibs to become free.
128  */
129  
130 struct fib * fib_alloc(struct aac_dev *dev)
131 {
132         struct fib * fibptr;
133         unsigned long flags;
134         spin_lock_irqsave(&dev->fib_lock, flags);
135         fibptr = dev->free_fib; 
136         while(!fibptr){
137                 spin_unlock_irqrestore(&dev->fib_lock, flags);
138                 set_current_state(TASK_UNINTERRUPTIBLE);
139                 schedule_timeout(1);
140                 spin_lock_irqsave(&dev->fib_lock, flags);
141                 fibptr = dev->free_fib; 
142         }
143         dev->free_fib = fibptr->next;
144         spin_unlock_irqrestore(&dev->fib_lock, flags);
145         /*
146          *      Set the proper node type code and node byte size
147          */
148         fibptr->type = FSAFS_NTC_FIB_CONTEXT;
149         fibptr->size = sizeof(struct fib);
150         /*
151          *      Null out fields that depend on being zero at the start of
152          *      each I/O
153          */
154         fibptr->hw_fib->header.XferState = cpu_to_le32(0);
155         fibptr->callback = NULL;
156         fibptr->callback_data = NULL;
157
158         return fibptr;
159 }
160
161 /**
162  *      fib_free        -       free a fib
163  *      @fibptr: fib to free up
164  *
165  *      Frees up a fib and places it on the appropriate queue
166  *      (either free or timed out)
167  */
168  
169 void fib_free(struct fib * fibptr)
170 {
171         unsigned long flags;
172
173         spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
174         if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
175                 aac_config.fib_timeouts++;
176                 fibptr->next = fibptr->dev->timeout_fib;
177                 fibptr->dev->timeout_fib = fibptr;
178         } else {
179                 if (fibptr->hw_fib->header.XferState != 0) {
180                         printk(KERN_WARNING "fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n", 
181                                  (void*)fibptr, fibptr->hw_fib->header.XferState);
182                 }
183                 fibptr->next = fibptr->dev->free_fib;
184                 fibptr->dev->free_fib = fibptr;
185         }       
186         spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
187 }
188
189 /**
190  *      fib_init        -       initialise a fib
191  *      @fibptr: The fib to initialize
192  *      
193  *      Set up the generic fib fields ready for use
194  */
195  
196 void fib_init(struct fib *fibptr)
197 {
198         struct hw_fib *hw_fib = fibptr->hw_fib;
199
200         hw_fib->header.StructType = FIB_MAGIC;
201         hw_fib->header.Size = cpu_to_le16(sizeof(struct hw_fib));
202         hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
203         hw_fib->header.SenderFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
204         hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
205         hw_fib->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
206 }
207
208 /**
209  *      fib_deallocate          -       deallocate a fib
210  *      @fibptr: fib to deallocate
211  *
212  *      Will deallocate and return to the free pool the FIB pointed to by the
213  *      caller.
214  */
215  
216 void fib_dealloc(struct fib * fibptr)
217 {
218         struct hw_fib *hw_fib = fibptr->hw_fib;
219         if(hw_fib->header.StructType != FIB_MAGIC) 
220                 BUG();
221         hw_fib->header.XferState = cpu_to_le32(0);        
222 }
223
224 /*
225  *      Commuication primitives define and support the queuing method we use to
226  *      support host to adapter commuication. All queue accesses happen through
227  *      these routines and are the only routines which have a knowledge of the
228  *       how these queues are implemented.
229  */
230  
231 /**
232  *      aac_get_entry           -       get a queue entry
233  *      @dev: Adapter
234  *      @qid: Queue Number
235  *      @entry: Entry return
236  *      @index: Index return
237  *      @nonotify: notification control
238  *
239  *      With a priority the routine returns a queue entry if the queue has free entries. If the queue
240  *      is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
241  *      returned.
242  */
243  
244 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
245 {
246         struct aac_queue * q;
247
248         /*
249          *      All of the queues wrap when they reach the end, so we check
250          *      to see if they have reached the end and if they have we just
251          *      set the index back to zero. This is a wrap. You could or off
252          *      the high bits in all updates but this is a bit faster I think.
253          */
254
255         q = &dev->queues->queue[qid];
256         
257         *index = le32_to_cpu(*(q->headers.producer));
258         if ((*index - 2) == le32_to_cpu(*(q->headers.consumer)))
259                         *nonotify = 1; 
260
261         if (qid == AdapHighCmdQueue) {
262                 if (*index >= ADAP_HIGH_CMD_ENTRIES)
263                         *index = 0;
264         } else if (qid == AdapNormCmdQueue) {
265                 if (*index >= ADAP_NORM_CMD_ENTRIES) 
266                         *index = 0; /* Wrap to front of the Producer Queue. */
267         }
268         else if (qid == AdapHighRespQueue) 
269         {
270                 if (*index >= ADAP_HIGH_RESP_ENTRIES)
271                         *index = 0;
272         }
273         else if (qid == AdapNormRespQueue) 
274         {
275                 if (*index >= ADAP_NORM_RESP_ENTRIES) 
276                         *index = 0; /* Wrap to front of the Producer Queue. */
277         }
278         else {
279                 printk("aacraid: invalid qid\n");
280                 BUG();
281         }
282
283         if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
284                 printk(KERN_WARNING "Queue %d full, %d outstanding.\n",
285                                 qid, q->numpending);
286                 return 0;
287         } else {
288                 *entry = q->base + *index;
289                 return 1;
290         }
291 }   
292
293 /*Command thread: *
294  *      aac_queue_get           -       get the next free QE
295  *      @dev: Adapter
296  *      @index: Returned index
297  *      @priority: Priority of fib
298  *      @fib: Fib to associate with the queue entry
299  *      @wait: Wait if queue full
300  *      @fibptr: Driver fib object to go with fib
301  *      @nonotify: Don't notify the adapter
302  *
303  *      Gets the next free QE off the requested priorty adapter command
304  *      queue and associates the Fib with the QE. The QE represented by
305  *      index is ready to insert on the queue when this routine returns
306  *      success.
307  */
308
309 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)
310 {
311         struct aac_entry * entry = NULL;
312         int map = 0;
313         struct aac_queue * q = &dev->queues->queue[qid];
314                 
315         spin_lock_irqsave(q->lock, q->SavedIrql);
316             
317         if (qid == AdapHighCmdQueue || qid == AdapNormCmdQueue) 
318         {
319                 /*  if no entries wait for some if caller wants to */
320                 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) 
321                 {
322                         printk(KERN_ERR "GetEntries failed\n");
323                 }
324                 /*
325                  *      Setup queue entry with a command, status and fib mapped
326                  */
327                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
328                 map = 1;
329         }
330         else if (qid == AdapHighRespQueue || qid == AdapNormRespQueue)
331         {
332                 while(!aac_get_entry(dev, qid, &entry, index, nonotify)) 
333                 {
334                         /* if no entries wait for some if caller wants to */
335                 }
336                 /*
337                  *      Setup queue entry with command, status and fib mapped
338                  */
339                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
340                 entry->addr = hw_fib->header.SenderFibAddress;
341                         /* Restore adapters pointer to the FIB */
342                 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;    /* Let the adapter now where to find its data */
343                 map = 0;
344         }
345         /*
346          *      If MapFib is true than we need to map the Fib and put pointers
347          *      in the queue entry.
348          */
349         if (map)
350                 entry->addr = fibptr->hw_fib_pa;
351         return 0;
352 }
353
354
355 /**
356  *      aac_insert_entry        -       insert a queue entry
357  *      @dev: Adapter
358  *      @index: Index of entry to insert
359  *      @qid: Queue number
360  *      @nonotify: Suppress adapter notification
361  *
362  *      Gets the next free QE off the requested priorty adapter command
363  *      queue and associates the Fib with the QE. The QE represented by
364  *      index is ready to insert on the queue when this routine returns
365  *      success.
366  */
367  
368 static int aac_insert_entry(struct aac_dev * dev, u32 index, u32 qid, unsigned long nonotify) 
369 {
370         struct aac_queue * q = &dev->queues->queue[qid];
371
372         if(q == NULL)
373                 BUG();
374         *(q->headers.producer) = cpu_to_le32(index + 1);
375         spin_unlock_irqrestore(q->lock, q->SavedIrql);
376
377         if (qid == AdapHighCmdQueue ||
378             qid == AdapNormCmdQueue ||
379             qid == AdapHighRespQueue ||
380             qid == AdapNormRespQueue)
381         {
382                 if (!nonotify)
383                         aac_adapter_notify(dev, qid);
384         }
385         else
386                 printk("Suprise insert!\n");
387         return 0;
388 }
389
390 /*
391  *      Define the highest level of host to adapter communication routines. 
392  *      These routines will support host to adapter FS commuication. These 
393  *      routines have no knowledge of the commuication method used. This level
394  *      sends and receives FIBs. This level has no knowledge of how these FIBs
395  *      get passed back and forth.
396  */
397
398 /**
399  *      fib_send        -       send a fib to the adapter
400  *      @command: Command to send
401  *      @fibptr: The fib
402  *      @size: Size of fib data area
403  *      @priority: Priority of Fib
404  *      @wait: Async/sync select
405  *      @reply: True if a reply is wanted
406  *      @callback: Called with reply
407  *      @callback_data: Passed to callback
408  *
409  *      Sends the requested FIB to the adapter and optionally will wait for a
410  *      response FIB. If the caller does not wish to wait for a response than
411  *      an event to wait on must be supplied. This event will be set when a
412  *      response FIB is received from the adapter.
413  */
414  
415 int fib_send(u16 command, struct fib * fibptr, unsigned long size,  int priority, int wait, int reply, fib_callback callback, void * callback_data)
416 {
417         u32 index;
418         u32 qid;
419         struct aac_dev * dev = fibptr->dev;
420         unsigned long nointr = 0;
421         struct hw_fib * hw_fib = fibptr->hw_fib;
422         struct aac_queue * q;
423         unsigned long flags = 0;
424         if (!(le32_to_cpu(hw_fib->header.XferState) & HostOwned))
425                 return -EBUSY;
426         /*
427          *      There are 5 cases with the wait and reponse requested flags. 
428          *      The only invalid cases are if the caller requests to wait and
429          *      does not request a response and if the caller does not want a
430          *      response and the Fibis not allocated from pool. If a response
431          *      is not requesed the Fib will just be deallocaed by the DPC
432          *      routine when the response comes back from the adapter. No
433          *      further processing will be done besides deleting the Fib. We 
434          *      will have a debug mode where the adapter can notify the host
435          *      it had a problem and the host can log that fact.
436          */
437         if (wait && !reply) {
438                 return -EINVAL;
439         } else if (!wait && reply) {
440                 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
441                 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
442         } else if (!wait && !reply) {
443                 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
444                 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
445         } else if (wait && reply) {
446                 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
447                 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
448         } 
449         /*
450          *      Map the fib into 32bits by using the fib number
451          */
452
453 //      hw_fib->header.SenderFibAddress = ((u32)(fibptr-dev->fibs)) << 1;
454         hw_fib->header.SenderFibAddress = cpu_to_le32((u32)(ulong)fibptr->hw_fib_pa);
455         hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
456         /*
457          *      Set FIB state to indicate where it came from and if we want a
458          *      response from the adapter. Also load the command from the
459          *      caller.
460          *
461          *      Map the hw fib pointer as a 32bit value
462          */
463         hw_fib->header.Command = cpu_to_le16(command);
464         hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
465         fibptr->hw_fib->header.Flags = 0;       /* 0 the flags field - internal only*/
466         /*
467          *      Set the size of the Fib we want to send to the adapter
468          */
469         hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
470         if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
471                 return -EMSGSIZE;
472         }                
473         /*
474          *      Get a queue entry connect the FIB to it and send an notify
475          *      the adapter a command is ready.
476          */
477         if (priority == FsaHigh) {
478                 hw_fib->header.XferState |= cpu_to_le32(HighPriority);
479                 qid = AdapHighCmdQueue;
480         } else {
481                 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
482                 qid = AdapNormCmdQueue;
483         }
484         q = &dev->queues->queue[qid];
485
486         if(wait)
487                 spin_lock_irqsave(&fibptr->event_lock, flags);
488         if(aac_queue_get( dev, &index, qid, hw_fib, 1, fibptr, &nointr)<0)
489                 return -EWOULDBLOCK;
490         dprintk((KERN_DEBUG "fib_send: inserting a queue entry at index %d.\n",index));
491         dprintk((KERN_DEBUG "Fib contents:.\n"));
492         dprintk((KERN_DEBUG "  Command =               %d.\n", hw_fib->header.Command));
493         dprintk((KERN_DEBUG "  XferState  =            %x.\n", hw_fib->header.XferState));
494         dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib));
495         dprintk((KERN_DEBUG "  hw_fib pa being sent=%xl\n",(ulong)fibptr->hw_fib_pa));
496         dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));
497         /*
498          *      Fill in the Callback and CallbackContext if we are not
499          *      going to wait.
500          */
501         if (!wait) {
502                 fibptr->callback = callback;
503                 fibptr->callback_data = callback_data;
504         }
505         FIB_COUNTER_INCREMENT(aac_config.FibsSent);
506         list_add_tail(&fibptr->queue, &q->pendingq);
507         q->numpending++;
508
509         fibptr->done = 0;
510         fibptr->flags = 0;
511
512         if(aac_insert_entry(dev, index, qid, (nointr & aac_config.irq_mod)) < 0)
513                 return -EWOULDBLOCK;
514         /*
515          *      If the caller wanted us to wait for response wait now. 
516          */
517     
518         if (wait) {
519                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
520                 down(&fibptr->event_wait);
521                 if(fibptr->done == 0)
522                         BUG();
523                         
524                 if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)){
525                         return -ETIMEDOUT;
526                 } else {
527                         return 0;
528                 }
529         }
530         /*
531          *      If the user does not want a response than return success otherwise
532          *      return pending
533          */
534         if (reply)
535                 return -EINPROGRESS;
536         else
537                 return 0;
538 }
539
540 /** 
541  *      aac_consumer_get        -       get the top of the queue
542  *      @dev: Adapter
543  *      @q: Queue
544  *      @entry: Return entry
545  *
546  *      Will return a pointer to the entry on the top of the queue requested that
547  *      we are a consumer of, and return the address of the queue entry. It does
548  *      not change the state of the queue. 
549  */
550
551 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
552 {
553         u32 index;
554         int status;
555         if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
556                 status = 0;
557         } else {
558                 /*
559                  *      The consumer index must be wrapped if we have reached
560                  *      the end of the queue, else we just use the entry
561                  *      pointed to by the header index
562                  */
563                 if (le32_to_cpu(*q->headers.consumer) >= q->entries) 
564                         index = 0;              
565                 else
566                         index = le32_to_cpu(*q->headers.consumer);
567                 *entry = q->base + index;
568                 status = 1;
569         }
570         return(status);
571 }
572
573 int aac_consumer_avail(struct aac_dev *dev, struct aac_queue * q)
574 {
575         return (le32_to_cpu(*q->headers.producer) != le32_to_cpu(*q->headers.consumer));
576 }
577
578
579 /**
580  *      aac_consumer_free       -       free consumer entry
581  *      @dev: Adapter
582  *      @q: Queue
583  *      @qid: Queue ident
584  *
585  *      Frees up the current top of the queue we are a consumer of. If the
586  *      queue was full notify the producer that the queue is no longer full.
587  */
588
589 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
590 {
591         int wasfull = 0;
592         u32 notify;
593
594         if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
595                 wasfull = 1;
596         
597         if (le32_to_cpu(*q->headers.consumer) >= q->entries)
598                 *q->headers.consumer = cpu_to_le32(1);
599         else
600                 *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
601         
602         if (wasfull) {
603                 switch (qid) {
604
605                 case HostNormCmdQueue:
606                         notify = HostNormCmdNotFull;
607                         break;
608                 case HostHighCmdQueue:
609                         notify = HostHighCmdNotFull;
610                         break;
611                 case HostNormRespQueue:
612                         notify = HostNormRespNotFull;
613                         break;
614                 case HostHighRespQueue:
615                         notify = HostHighRespNotFull;
616                         break;
617                 default:
618                         BUG();
619                         return;
620                 }
621                 aac_adapter_notify(dev, notify);
622         }
623 }        
624
625 /**
626  *      fib_adapter_complete    -       complete adapter issued fib
627  *      @fibptr: fib to complete
628  *      @size: size of fib
629  *
630  *      Will do all necessary work to complete a FIB that was sent from
631  *      the adapter.
632  */
633
634 int fib_adapter_complete(struct fib * fibptr, unsigned short size)
635 {
636         struct hw_fib * hw_fib = fibptr->hw_fib;
637         struct aac_dev * dev = fibptr->dev;
638         unsigned long nointr = 0;
639         if (le32_to_cpu(hw_fib->header.XferState) == 0)
640                 return 0;
641         /*
642          *      If we plan to do anything check the structure type first.
643          */ 
644         if ( hw_fib->header.StructType != FIB_MAGIC ) {
645                 return -EINVAL;
646         }
647         /*
648          *      This block handles the case where the adapter had sent us a
649          *      command and we have finished processing the command. We
650          *      call completeFib when we are done processing the command 
651          *      and want to send a response back to the adapter. This will 
652          *      send the completed cdb to the adapter.
653          */
654         if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
655                 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
656                 if (hw_fib->header.XferState & cpu_to_le32(HighPriority)) {
657                         u32 index;
658                         if (size) 
659                         {
660                                 size += sizeof(struct aac_fibhdr);
661                                 if (size > le16_to_cpu(hw_fib->header.SenderSize))
662                                         return -EMSGSIZE;
663                                 hw_fib->header.Size = cpu_to_le16(size);
664                         }
665                         if(aac_queue_get(dev, &index, AdapHighRespQueue, hw_fib, 1, NULL, &nointr) < 0) {
666                                 return -EWOULDBLOCK;
667                         }
668                         if (aac_insert_entry(dev, index, AdapHighRespQueue,  (nointr & (int)aac_config.irq_mod)) != 0) {
669                         }
670                 }
671                 else if (hw_fib->header.XferState & NormalPriority) 
672                 {
673                         u32 index;
674
675                         if (size) {
676                                 size += sizeof(struct aac_fibhdr);
677                                 if (size > le16_to_cpu(hw_fib->header.SenderSize)) 
678                                         return -EMSGSIZE;
679                                 hw_fib->header.Size = cpu_to_le16(size);
680                         }
681                         if (aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr) < 0) 
682                                 return -EWOULDBLOCK;
683                         if (aac_insert_entry(dev, index, AdapNormRespQueue, (nointr & (int)aac_config.irq_mod)) != 0) 
684                         {
685                         }
686                 }
687         }
688         else 
689         {
690                 printk(KERN_WARNING "fib_adapter_complete: Unknown xferstate detected.\n");
691                 BUG();
692         }   
693         return 0;
694 }
695
696 /**
697  *      fib_complete    -       fib completion handler
698  *      @fib: FIB to complete
699  *
700  *      Will do all necessary work to complete a FIB.
701  */
702  
703 int fib_complete(struct fib * fibptr)
704 {
705         struct hw_fib * hw_fib = fibptr->hw_fib;
706
707         /*
708          *      Check for a fib which has already been completed
709          */
710
711         if (hw_fib->header.XferState == cpu_to_le32(0))
712                 return 0;
713         /*
714          *      If we plan to do anything check the structure type first.
715          */ 
716
717         if (hw_fib->header.StructType != FIB_MAGIC)
718                 return -EINVAL;
719         /*
720          *      This block completes a cdb which orginated on the host and we 
721          *      just need to deallocate the cdb or reinit it. At this point the
722          *      command is complete that we had sent to the adapter and this
723          *      cdb could be reused.
724          */
725         if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
726                 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
727         {
728                 fib_dealloc(fibptr);
729         }
730         else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
731         {
732                 /*
733                  *      This handles the case when the host has aborted the I/O
734                  *      to the adapter because the adapter is not responding
735                  */
736                 fib_dealloc(fibptr);
737         } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
738                 fib_dealloc(fibptr);
739         } else {
740                 BUG();
741         }   
742         return 0;
743 }
744
745 /**
746  *      aac_printf      -       handle printf from firmware
747  *      @dev: Adapter
748  *      @val: Message info
749  *
750  *      Print a message passed to us by the controller firmware on the
751  *      Adaptec board
752  */
753
754 void aac_printf(struct aac_dev *dev, u32 val)
755 {
756         int length = val & 0xffff;
757         int level = (val >> 16) & 0xffff;
758         char *cp = dev->printfbuf;
759         
760         /*
761          *      The size of the printfbuf is set in port.c
762          *      There is no variable or define for it
763          */
764         if (length > 255)
765                 length = 255;
766         if (cp[length] != 0)
767                 cp[length] = 0;
768         if (level == LOG_HIGH_ERROR)
769                 printk(KERN_WARNING "aacraid:%s", cp);
770         else
771                 printk(KERN_INFO "aacraid:%s", cp);
772         memset(cp, 0,  256);
773 }
774
775
776 /**
777  *      aac_handle_aif          -       Handle a message from the firmware
778  *      @dev: Which adapter this fib is from
779  *      @fibptr: Pointer to fibptr from adapter
780  *
781  *      This routine handles a driver notify fib from the adapter and
782  *      dispatches it to the appropriate routine for handling.
783  */
784
785 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
786 {
787         struct hw_fib * hw_fib = fibptr->hw_fib;
788         /*
789          * Set the status of this FIB to be Invalid parameter.
790          *
791          *      *(u32 *)fib->data = ST_INVAL;
792          */
793         *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
794         fib_adapter_complete(fibptr, sizeof(u32));
795 }
796
797 /**
798  *      aac_command_thread      -       command processing thread
799  *      @dev: Adapter to monitor
800  *
801  *      Waits on the commandready event in it's queue. When the event gets set
802  *      it will pull FIBs off it's queue. It will continue to pull FIBs off
803  *      until the queue is empty. When the queue is empty it will wait for
804  *      more FIBs.
805  */
806  
807 int aac_command_thread(struct aac_dev * dev)
808 {
809         struct hw_fib *hw_fib, *newfib;
810         struct fib fibptr; /* for error logging */
811         struct aac_queue_block *queues = dev->queues;
812         struct aac_fib_context *fibctx;
813         unsigned long flags;
814         DECLARE_WAITQUEUE(wait, current);
815
816         /*
817          *      We can only have one thread per adapter for AIF's.
818          */
819         if (dev->aif_thread)
820                 return -EINVAL;
821         /*
822          *      Set up the name that will appear in 'ps'
823          *      stored in  task_struct.comm[16].
824          */
825         daemonize("aacraid");
826         allow_signal(SIGKILL);
827         /*
828          *      Let the DPC know it has a place to send the AIF's to.
829          */
830         dev->aif_thread = 1;
831         memset(&fibptr, 0, sizeof(struct fib));
832         add_wait_queue(&queues->queue[HostNormCmdQueue].cmdready, &wait);
833         set_current_state(TASK_INTERRUPTIBLE);
834         while(1) 
835         {
836                 spin_lock_irqsave(queues->queue[HostNormCmdQueue].lock, flags);
837                 while(!aac_list_empty(&(queues->queue[HostNormCmdQueue].cmdq))) {
838                         struct aac_list_head *entry;
839                         struct aac_aifcmd * aifcmd;
840
841                         set_current_state(TASK_RUNNING);
842                 
843                         entry = (struct aac_list_head*)(ulong)(queues->queue[HostNormCmdQueue].cmdq.next);
844                         dprintk(("aacraid: Command thread: removing fib from cmdq (%p)\n",entry));
845                         aac_list_del(entry);
846                         
847                         spin_unlock_irqrestore(queues->queue[HostNormCmdQueue].lock, flags);
848                         hw_fib = aac_list_entry(entry, struct hw_fib, header.FibLinks);
849                         /*
850                          *      We will process the FIB here or pass it to a 
851                          *      worker thread that is TBD. We Really can't 
852                          *      do anything at this point since we don't have
853                          *      anything defined for this thread to do.
854                          */
855                         memset(&fibptr, 0, sizeof(struct fib));
856                         fibptr.type = FSAFS_NTC_FIB_CONTEXT;
857                         fibptr.size = sizeof( struct fib );
858                         fibptr.hw_fib = hw_fib;
859                         fibptr.data = hw_fib->data;
860                         fibptr.dev = dev;
861                         /*
862                          *      We only handle AifRequest fibs from the adapter.
863                          */
864                         aifcmd = (struct aac_aifcmd *) hw_fib->data;
865                         if (aifcmd->command == le16_to_cpu(AifCmdDriverNotify)) {
866                                 aac_handle_aif(dev, &fibptr);
867                         } else {
868                                 struct list_head *entry;
869                                 /* The u32 here is important and intended. We are using
870                                    32bit wrapping time to fit the adapter field */
871                                    
872                                 u32 time_now, time_last;
873                                 unsigned long flagv;
874                                 
875                                 time_now = jiffies/HZ;
876
877                                 spin_lock_irqsave(&dev->fib_lock, flagv);
878                                 entry = dev->fib_list.next;
879                                 /*
880                                  * For each Context that is on the 
881                                  * fibctxList, make a copy of the
882                                  * fib, and then set the event to wake up the
883                                  * thread that is waiting for it.
884                                  */
885                                 while (entry != &dev->fib_list) {
886                                         /*
887                                          * Extract the fibctx
888                                          */
889                                         fibctx = list_entry(entry, struct aac_fib_context, next);
890                                         /*
891                                          * Check if the queue is getting
892                                          * backlogged
893                                          */
894                                         if (fibctx->count > 20)
895                                         {
896                                                 time_last = fibctx->jiffies;
897                                                 /*
898                                                  * Has it been > 2 minutes 
899                                                  * since the last read off
900                                                  * the queue?
901                                                  */
902                                                 if ((time_now - time_last) > 120) {
903                                                         entry = entry->next;
904                                                         aac_close_fib_context(dev, fibctx);
905                                                         continue;
906                                                 }
907                                         }
908                                         /*
909                                          * Warning: no sleep allowed while
910                                          * holding spinlock
911                                          */
912                                         newfib = kmalloc(sizeof(struct hw_fib), GFP_ATOMIC);
913                                         if (newfib) {
914                                                 /*
915                                                  * Make the copy of the FIB
916                                                  */
917                                                 memcpy(newfib, hw_fib, sizeof(struct hw_fib));
918                                                 /*
919                                                  * Put the FIB onto the
920                                                  * fibctx's fibs
921                                                  */
922                                                 aac_list_add_tail(&newfib->header.FibLinks, &fibctx->hw_fib_list);
923                                                 fibctx->count++;
924                                                 /* 
925                                                  * Set the event to wake up the
926                                                  * thread that will waiting.
927                                                  */
928                                                 up(&fibctx->wait_sem);
929                                         } else {
930                                                 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
931                                         }
932                                         entry = entry->next;
933                                 }
934                                 /*
935                                  *      Set the status of this FIB
936                                  */
937                                 *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
938                                 fib_adapter_complete(&fibptr, sizeof(u32));
939                                 spin_unlock_irqrestore(&dev->fib_lock, flagv);
940                         }
941                         spin_lock_irqsave(queues->queue[HostNormCmdQueue].lock, flags);
942                 }
943                 /*
944                  *      There are no more AIF's
945                  */
946                 spin_unlock_irqrestore(queues->queue[HostNormCmdQueue].lock, flags);
947                 schedule();
948
949                 if(signal_pending(current))
950                         break;
951                 set_current_state(TASK_INTERRUPTIBLE);
952         }
953         remove_wait_queue(&queues->queue[HostNormCmdQueue].cmdready, &wait);
954         dev->aif_thread = 0;
955         complete_and_exit(&dev->aif_completion, 0);
956 }