aacraid: Fix for aac_command_thread hang
[pandora-kernel.git] / drivers / scsi / aacraid / commsup.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc.
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2010 Adaptec, Inc.
9  *               2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Module Name:
26  *  commsup.c
27  *
28  * Abstract: Contain all routines that are required for FSA host/adapter
29  *    communication.
30  *
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/sched.h>
37 #include <linux/pci.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/completion.h>
41 #include <linux/blkdev.h>
42 #include <linux/delay.h>
43 #include <linux/kthread.h>
44 #include <linux/interrupt.h>
45 #include <linux/semaphore.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_cmnd.h>
50
51 #include "aacraid.h"
52
53 /**
54  *      fib_map_alloc           -       allocate the fib objects
55  *      @dev: Adapter to allocate for
56  *
57  *      Allocate and map the shared PCI space for the FIB blocks used to
58  *      talk to the Adaptec firmware.
59  */
60
61 static int fib_map_alloc(struct aac_dev *dev)
62 {
63         dprintk((KERN_INFO
64           "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
65           dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
66           AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
67         dev->hw_fib_va = pci_alloc_consistent(dev->pdev,
68                 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr))
69                 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
70                 &dev->hw_fib_pa);
71         if (dev->hw_fib_va == NULL)
72                 return -ENOMEM;
73         return 0;
74 }
75
76 /**
77  *      aac_fib_map_free                -       free the fib objects
78  *      @dev: Adapter to free
79  *
80  *      Free the PCI mappings and the memory allocated for FIB blocks
81  *      on this adapter.
82  */
83
84 void aac_fib_map_free(struct aac_dev *dev)
85 {
86         if (dev->hw_fib_va && dev->max_fib_size) {
87                 pci_free_consistent(dev->pdev,
88                 (dev->max_fib_size *
89                 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB)),
90                 dev->hw_fib_va, dev->hw_fib_pa);
91         }
92         dev->hw_fib_va = NULL;
93         dev->hw_fib_pa = 0;
94 }
95
96 /**
97  *      aac_fib_setup   -       setup the fibs
98  *      @dev: Adapter to set up
99  *
100  *      Allocate the PCI space for the fibs, map it and then initialise the
101  *      fib area, the unmapped fib data and also the free list
102  */
103
104 int aac_fib_setup(struct aac_dev * dev)
105 {
106         struct fib *fibptr;
107         struct hw_fib *hw_fib;
108         dma_addr_t hw_fib_pa;
109         int i;
110
111         while (((i = fib_map_alloc(dev)) == -ENOMEM)
112          && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
113                 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
114                 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
115         }
116         if (i<0)
117                 return -ENOMEM;
118
119         /* 32 byte alignment for PMC */
120         hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
121         dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
122                 (hw_fib_pa - dev->hw_fib_pa));
123         dev->hw_fib_pa = hw_fib_pa;
124         memset(dev->hw_fib_va, 0,
125                 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
126                 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
127
128         /* add Xport header */
129         dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
130                 sizeof(struct aac_fib_xporthdr));
131         dev->hw_fib_pa += sizeof(struct aac_fib_xporthdr);
132
133         hw_fib = dev->hw_fib_va;
134         hw_fib_pa = dev->hw_fib_pa;
135         /*
136          *      Initialise the fibs
137          */
138         for (i = 0, fibptr = &dev->fibs[i];
139                 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
140                 i++, fibptr++)
141         {
142                 fibptr->dev = dev;
143                 fibptr->hw_fib_va = hw_fib;
144                 fibptr->data = (void *) fibptr->hw_fib_va->data;
145                 fibptr->next = fibptr+1;        /* Forward chain the fibs */
146                 sema_init(&fibptr->event_wait, 0);
147                 spin_lock_init(&fibptr->event_lock);
148                 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
149                 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
150                 fibptr->hw_fib_pa = hw_fib_pa;
151                 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
152                         dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
153                 hw_fib_pa = hw_fib_pa +
154                         dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
155         }
156         /*
157          *      Add the fib chain to the free list
158          */
159         dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
160         /*
161          *      Enable this to debug out of queue space
162          */
163         dev->free_fib = &dev->fibs[0];
164         return 0;
165 }
166
167 /**
168  *      aac_fib_alloc   -       allocate a fib
169  *      @dev: Adapter to allocate the fib for
170  *
171  *      Allocate a fib from the adapter fib pool. If the pool is empty we
172  *      return NULL.
173  */
174
175 struct fib *aac_fib_alloc(struct aac_dev *dev)
176 {
177         struct fib * fibptr;
178         unsigned long flags;
179         spin_lock_irqsave(&dev->fib_lock, flags);
180         fibptr = dev->free_fib;
181         if(!fibptr){
182                 spin_unlock_irqrestore(&dev->fib_lock, flags);
183                 return fibptr;
184         }
185         dev->free_fib = fibptr->next;
186         spin_unlock_irqrestore(&dev->fib_lock, flags);
187         /*
188          *      Set the proper node type code and node byte size
189          */
190         fibptr->type = FSAFS_NTC_FIB_CONTEXT;
191         fibptr->size = sizeof(struct fib);
192         /*
193          *      Null out fields that depend on being zero at the start of
194          *      each I/O
195          */
196         fibptr->hw_fib_va->header.XferState = 0;
197         fibptr->flags = 0;
198         fibptr->callback = NULL;
199         fibptr->callback_data = NULL;
200
201         return fibptr;
202 }
203
204 /**
205  *      aac_fib_free    -       free a fib
206  *      @fibptr: fib to free up
207  *
208  *      Frees up a fib and places it on the appropriate queue
209  */
210
211 void aac_fib_free(struct fib *fibptr)
212 {
213         unsigned long flags, flagsv;
214
215         spin_lock_irqsave(&fibptr->event_lock, flagsv);
216         if (fibptr->done == 2) {
217                 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
218                 return;
219         }
220         spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
221
222         spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
223         if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
224                 aac_config.fib_timeouts++;
225         if (fibptr->hw_fib_va->header.XferState != 0) {
226                 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
227                          (void*)fibptr,
228                          le32_to_cpu(fibptr->hw_fib_va->header.XferState));
229         }
230         fibptr->next = fibptr->dev->free_fib;
231         fibptr->dev->free_fib = fibptr;
232         spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
233 }
234
235 /**
236  *      aac_fib_init    -       initialise a fib
237  *      @fibptr: The fib to initialize
238  *
239  *      Set up the generic fib fields ready for use
240  */
241
242 void aac_fib_init(struct fib *fibptr)
243 {
244         struct hw_fib *hw_fib = fibptr->hw_fib_va;
245
246         hw_fib->header.StructType = FIB_MAGIC;
247         hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
248         hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
249         hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
250         hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
251         hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
252 }
253
254 /**
255  *      fib_deallocate          -       deallocate a fib
256  *      @fibptr: fib to deallocate
257  *
258  *      Will deallocate and return to the free pool the FIB pointed to by the
259  *      caller.
260  */
261
262 static void fib_dealloc(struct fib * fibptr)
263 {
264         struct hw_fib *hw_fib = fibptr->hw_fib_va;
265         BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
266         hw_fib->header.XferState = 0;
267 }
268
269 /*
270  *      Commuication primitives define and support the queuing method we use to
271  *      support host to adapter commuication. All queue accesses happen through
272  *      these routines and are the only routines which have a knowledge of the
273  *       how these queues are implemented.
274  */
275
276 /**
277  *      aac_get_entry           -       get a queue entry
278  *      @dev: Adapter
279  *      @qid: Queue Number
280  *      @entry: Entry return
281  *      @index: Index return
282  *      @nonotify: notification control
283  *
284  *      With a priority the routine returns a queue entry if the queue has free entries. If the queue
285  *      is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
286  *      returned.
287  */
288
289 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
290 {
291         struct aac_queue * q;
292         unsigned long idx;
293
294         /*
295          *      All of the queues wrap when they reach the end, so we check
296          *      to see if they have reached the end and if they have we just
297          *      set the index back to zero. This is a wrap. You could or off
298          *      the high bits in all updates but this is a bit faster I think.
299          */
300
301         q = &dev->queues->queue[qid];
302
303         idx = *index = le32_to_cpu(*(q->headers.producer));
304         /* Interrupt Moderation, only interrupt for first two entries */
305         if (idx != le32_to_cpu(*(q->headers.consumer))) {
306                 if (--idx == 0) {
307                         if (qid == AdapNormCmdQueue)
308                                 idx = ADAP_NORM_CMD_ENTRIES;
309                         else
310                                 idx = ADAP_NORM_RESP_ENTRIES;
311                 }
312                 if (idx != le32_to_cpu(*(q->headers.consumer)))
313                         *nonotify = 1;
314         }
315
316         if (qid == AdapNormCmdQueue) {
317                 if (*index >= ADAP_NORM_CMD_ENTRIES)
318                         *index = 0; /* Wrap to front of the Producer Queue. */
319         } else {
320                 if (*index >= ADAP_NORM_RESP_ENTRIES)
321                         *index = 0; /* Wrap to front of the Producer Queue. */
322         }
323
324         /* Queue is full */
325         if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
326                 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
327                                 qid, q->numpending);
328                 return 0;
329         } else {
330                 *entry = q->base + *index;
331                 return 1;
332         }
333 }
334
335 /**
336  *      aac_queue_get           -       get the next free QE
337  *      @dev: Adapter
338  *      @index: Returned index
339  *      @priority: Priority of fib
340  *      @fib: Fib to associate with the queue entry
341  *      @wait: Wait if queue full
342  *      @fibptr: Driver fib object to go with fib
343  *      @nonotify: Don't notify the adapter
344  *
345  *      Gets the next free QE off the requested priorty adapter command
346  *      queue and associates the Fib with the QE. The QE represented by
347  *      index is ready to insert on the queue when this routine returns
348  *      success.
349  */
350
351 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)
352 {
353         struct aac_entry * entry = NULL;
354         int map = 0;
355
356         if (qid == AdapNormCmdQueue) {
357                 /*  if no entries wait for some if caller wants to */
358                 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
359                         printk(KERN_ERR "GetEntries failed\n");
360                 }
361                 /*
362                  *      Setup queue entry with a command, status and fib mapped
363                  */
364                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
365                 map = 1;
366         } else {
367                 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
368                         /* if no entries wait for some if caller wants to */
369                 }
370                 /*
371                  *      Setup queue entry with command, status and fib mapped
372                  */
373                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
374                 entry->addr = hw_fib->header.SenderFibAddress;
375                         /* Restore adapters pointer to the FIB */
376                 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;    /* Let the adapter now where to find its data */
377                 map = 0;
378         }
379         /*
380          *      If MapFib is true than we need to map the Fib and put pointers
381          *      in the queue entry.
382          */
383         if (map)
384                 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
385         return 0;
386 }
387
388 /*
389  *      Define the highest level of host to adapter communication routines.
390  *      These routines will support host to adapter FS commuication. These
391  *      routines have no knowledge of the commuication method used. This level
392  *      sends and receives FIBs. This level has no knowledge of how these FIBs
393  *      get passed back and forth.
394  */
395
396 /**
397  *      aac_fib_send    -       send a fib to the adapter
398  *      @command: Command to send
399  *      @fibptr: The fib
400  *      @size: Size of fib data area
401  *      @priority: Priority of Fib
402  *      @wait: Async/sync select
403  *      @reply: True if a reply is wanted
404  *      @callback: Called with reply
405  *      @callback_data: Passed to callback
406  *
407  *      Sends the requested FIB to the adapter and optionally will wait for a
408  *      response FIB. If the caller does not wish to wait for a response than
409  *      an event to wait on must be supplied. This event will be set when a
410  *      response FIB is received from the adapter.
411  */
412
413 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
414                 int priority, int wait, int reply, fib_callback callback,
415                 void *callback_data)
416 {
417         struct aac_dev * dev = fibptr->dev;
418         struct hw_fib * hw_fib = fibptr->hw_fib_va;
419         unsigned long flags = 0;
420         unsigned long qflags;
421         unsigned long mflags = 0;
422
423
424         if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
425                 return -EBUSY;
426         /*
427          *      There are 5 cases with the wait and response 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 Fib is 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         fibptr->flags = 0;
438         if (wait && !reply) {
439                 return -EINVAL;
440         } else if (!wait && reply) {
441                 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
442                 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
443         } else if (!wait && !reply) {
444                 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
445                 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
446         } else if (wait && reply) {
447                 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
448                 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
449         }
450         /*
451          *      Map the fib into 32bits by using the fib number
452          */
453
454         hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
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_va->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         hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
478
479         /*
480          *      Fill in the Callback and CallbackContext if we are not
481          *      going to wait.
482          */
483         if (!wait) {
484                 fibptr->callback = callback;
485                 fibptr->callback_data = callback_data;
486                 fibptr->flags = FIB_CONTEXT_FLAG;
487         }
488
489         fibptr->done = 0;
490
491         FIB_COUNTER_INCREMENT(aac_config.FibsSent);
492
493         dprintk((KERN_DEBUG "Fib contents:.\n"));
494         dprintk((KERN_DEBUG "  Command =               %d.\n", le32_to_cpu(hw_fib->header.Command)));
495         dprintk((KERN_DEBUG "  SubCommand =            %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
496         dprintk((KERN_DEBUG "  XferState  =            %x.\n", le32_to_cpu(hw_fib->header.XferState)));
497         dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib_va));
498         dprintk((KERN_DEBUG "  hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
499         dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));
500
501         if (!dev->queues)
502                 return -EBUSY;
503
504         if (wait) {
505
506                 spin_lock_irqsave(&dev->manage_lock, mflags);
507                 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
508                         printk(KERN_INFO "No management Fibs Available:%d\n",
509                                                 dev->management_fib_count);
510                         spin_unlock_irqrestore(&dev->manage_lock, mflags);
511                         return -EBUSY;
512                 }
513                 dev->management_fib_count++;
514                 spin_unlock_irqrestore(&dev->manage_lock, mflags);
515                 spin_lock_irqsave(&fibptr->event_lock, flags);
516         }
517
518         if (aac_adapter_deliver(fibptr) != 0) {
519                 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
520                 if (wait) {
521                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
522                         spin_lock_irqsave(&dev->manage_lock, mflags);
523                         dev->management_fib_count--;
524                         spin_unlock_irqrestore(&dev->manage_lock, mflags);
525                 }
526                 return -EBUSY;
527         }
528
529
530         /*
531          *      If the caller wanted us to wait for response wait now.
532          */
533
534         if (wait) {
535                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
536                 /* Only set for first known interruptable command */
537                 if (wait < 0) {
538                         /*
539                          * *VERY* Dangerous to time out a command, the
540                          * assumption is made that we have no hope of
541                          * functioning because an interrupt routing or other
542                          * hardware failure has occurred.
543                          */
544                         unsigned long count = 36000000L; /* 3 minutes */
545                         while (down_trylock(&fibptr->event_wait)) {
546                                 int blink;
547                                 if (--count == 0) {
548                                         struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
549                                         spin_lock_irqsave(q->lock, qflags);
550                                         q->numpending--;
551                                         spin_unlock_irqrestore(q->lock, qflags);
552                                         if (wait == -1) {
553                                                 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
554                                                   "Usually a result of a PCI interrupt routing problem;\n"
555                                                   "update mother board BIOS or consider utilizing one of\n"
556                                                   "the SAFE mode kernel options (acpi, apic etc)\n");
557                                         }
558                                         return -ETIMEDOUT;
559                                 }
560                                 if ((blink = aac_adapter_check_health(dev)) > 0) {
561                                         if (wait == -1) {
562                                                 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
563                                                   "Usually a result of a serious unrecoverable hardware problem\n",
564                                                   blink);
565                                         }
566                                         return -EFAULT;
567                                 }
568                                 udelay(5);
569                         }
570                 } else if (down_interruptible(&fibptr->event_wait)) {
571                         /* Do nothing ... satisfy
572                          * down_interruptible must_check */
573                 }
574
575                 spin_lock_irqsave(&fibptr->event_lock, flags);
576                 if (fibptr->done == 0) {
577                         fibptr->done = 2; /* Tell interrupt we aborted */
578                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
579                         return -ERESTARTSYS;
580                 }
581                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
582                 BUG_ON(fibptr->done == 0);
583
584                 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
585                         return -ETIMEDOUT;
586                 return 0;
587         }
588         /*
589          *      If the user does not want a response than return success otherwise
590          *      return pending
591          */
592         if (reply)
593                 return -EINPROGRESS;
594         else
595                 return 0;
596 }
597
598 /**
599  *      aac_consumer_get        -       get the top of the queue
600  *      @dev: Adapter
601  *      @q: Queue
602  *      @entry: Return entry
603  *
604  *      Will return a pointer to the entry on the top of the queue requested that
605  *      we are a consumer of, and return the address of the queue entry. It does
606  *      not change the state of the queue.
607  */
608
609 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
610 {
611         u32 index;
612         int status;
613         if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
614                 status = 0;
615         } else {
616                 /*
617                  *      The consumer index must be wrapped if we have reached
618                  *      the end of the queue, else we just use the entry
619                  *      pointed to by the header index
620                  */
621                 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
622                         index = 0;
623                 else
624                         index = le32_to_cpu(*q->headers.consumer);
625                 *entry = q->base + index;
626                 status = 1;
627         }
628         return(status);
629 }
630
631 /**
632  *      aac_consumer_free       -       free consumer entry
633  *      @dev: Adapter
634  *      @q: Queue
635  *      @qid: Queue ident
636  *
637  *      Frees up the current top of the queue we are a consumer of. If the
638  *      queue was full notify the producer that the queue is no longer full.
639  */
640
641 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
642 {
643         int wasfull = 0;
644         u32 notify;
645
646         if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
647                 wasfull = 1;
648
649         if (le32_to_cpu(*q->headers.consumer) >= q->entries)
650                 *q->headers.consumer = cpu_to_le32(1);
651         else
652                 le32_add_cpu(q->headers.consumer, 1);
653
654         if (wasfull) {
655                 switch (qid) {
656
657                 case HostNormCmdQueue:
658                         notify = HostNormCmdNotFull;
659                         break;
660                 case HostNormRespQueue:
661                         notify = HostNormRespNotFull;
662                         break;
663                 default:
664                         BUG();
665                         return;
666                 }
667                 aac_adapter_notify(dev, notify);
668         }
669 }
670
671 /**
672  *      aac_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 aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
681 {
682         struct hw_fib * hw_fib = fibptr->hw_fib_va;
683         struct aac_dev * dev = fibptr->dev;
684         struct aac_queue * q;
685         unsigned long nointr = 0;
686         unsigned long qflags;
687
688         if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
689                 kfree(hw_fib);
690                 return 0;
691         }
692
693         if (hw_fib->header.XferState == 0) {
694                 if (dev->comm_interface == AAC_COMM_MESSAGE)
695                         kfree(hw_fib);
696                 return 0;
697         }
698         /*
699          *      If we plan to do anything check the structure type first.
700          */
701         if (hw_fib->header.StructType != FIB_MAGIC) {
702                 if (dev->comm_interface == AAC_COMM_MESSAGE)
703                         kfree(hw_fib);
704                 return -EINVAL;
705         }
706         /*
707          *      This block handles the case where the adapter had sent us a
708          *      command and we have finished processing the command. We
709          *      call completeFib when we are done processing the command
710          *      and want to send a response back to the adapter. This will
711          *      send the completed cdb to the adapter.
712          */
713         if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
714                 if (dev->comm_interface == AAC_COMM_MESSAGE) {
715                         kfree (hw_fib);
716                 } else {
717                         u32 index;
718                         hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
719                         if (size) {
720                                 size += sizeof(struct aac_fibhdr);
721                                 if (size > le16_to_cpu(hw_fib->header.SenderSize))
722                                         return -EMSGSIZE;
723                                 hw_fib->header.Size = cpu_to_le16(size);
724                         }
725                         q = &dev->queues->queue[AdapNormRespQueue];
726                         spin_lock_irqsave(q->lock, qflags);
727                         aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
728                         *(q->headers.producer) = cpu_to_le32(index + 1);
729                         spin_unlock_irqrestore(q->lock, qflags);
730                         if (!(nointr & (int)aac_config.irq_mod))
731                                 aac_adapter_notify(dev, AdapNormRespQueue);
732                 }
733         } else {
734                 printk(KERN_WARNING "aac_fib_adapter_complete: "
735                         "Unknown xferstate detected.\n");
736                 BUG();
737         }
738         return 0;
739 }
740
741 /**
742  *      aac_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 aac_fib_complete(struct fib *fibptr)
749 {
750         unsigned long flags;
751         struct hw_fib * hw_fib = fibptr->hw_fib_va;
752
753         /*
754          *      Check for a fib which has already been completed
755          */
756
757         if (hw_fib->header.XferState == 0)
758                 return 0;
759         /*
760          *      If we plan to do anything check the structure type first.
761          */
762
763         if (hw_fib->header.StructType != FIB_MAGIC)
764                 return -EINVAL;
765         /*
766          *      This block completes a cdb which orginated on the host and we
767          *      just need to deallocate the cdb or reinit it. At this point the
768          *      command is complete that we had sent to the adapter and this
769          *      cdb could be reused.
770          */
771         spin_lock_irqsave(&fibptr->event_lock, flags);
772         if (fibptr->done == 2) {
773                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
774                 return 0;
775         }
776         spin_unlock_irqrestore(&fibptr->event_lock, flags);
777
778         if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
779                 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
780         {
781                 fib_dealloc(fibptr);
782         }
783         else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
784         {
785                 /*
786                  *      This handles the case when the host has aborted the I/O
787                  *      to the adapter because the adapter is not responding
788                  */
789                 fib_dealloc(fibptr);
790         } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
791                 fib_dealloc(fibptr);
792         } else {
793                 BUG();
794         }
795         return 0;
796 }
797
798 /**
799  *      aac_printf      -       handle printf from firmware
800  *      @dev: Adapter
801  *      @val: Message info
802  *
803  *      Print a message passed to us by the controller firmware on the
804  *      Adaptec board
805  */
806
807 void aac_printf(struct aac_dev *dev, u32 val)
808 {
809         char *cp = dev->printfbuf;
810         if (dev->printf_enabled)
811         {
812                 int length = val & 0xffff;
813                 int level = (val >> 16) & 0xffff;
814
815                 /*
816                  *      The size of the printfbuf is set in port.c
817                  *      There is no variable or define for it
818                  */
819                 if (length > 255)
820                         length = 255;
821                 if (cp[length] != 0)
822                         cp[length] = 0;
823                 if (level == LOG_AAC_HIGH_ERROR)
824                         printk(KERN_WARNING "%s:%s", dev->name, cp);
825                 else
826                         printk(KERN_INFO "%s:%s", dev->name, cp);
827         }
828         memset(cp, 0, 256);
829 }
830
831
832 /**
833  *      aac_handle_aif          -       Handle a message from the firmware
834  *      @dev: Which adapter this fib is from
835  *      @fibptr: Pointer to fibptr from adapter
836  *
837  *      This routine handles a driver notify fib from the adapter and
838  *      dispatches it to the appropriate routine for handling.
839  */
840
841 #define AIF_SNIFF_TIMEOUT       (30*HZ)
842 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
843 {
844         struct hw_fib * hw_fib = fibptr->hw_fib_va;
845         struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
846         u32 channel, id, lun, container;
847         struct scsi_device *device;
848         enum {
849                 NOTHING,
850                 DELETE,
851                 ADD,
852                 CHANGE
853         } device_config_needed = NOTHING;
854
855         /* Sniff for container changes */
856
857         if (!dev || !dev->fsa_dev)
858                 return;
859         container = channel = id = lun = (u32)-1;
860
861         /*
862          *      We have set this up to try and minimize the number of
863          * re-configures that take place. As a result of this when
864          * certain AIF's come in we will set a flag waiting for another
865          * type of AIF before setting the re-config flag.
866          */
867         switch (le32_to_cpu(aifcmd->command)) {
868         case AifCmdDriverNotify:
869                 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
870                 /*
871                  *      Morph or Expand complete
872                  */
873                 case AifDenMorphComplete:
874                 case AifDenVolumeExtendComplete:
875                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
876                         if (container >= dev->maximum_num_containers)
877                                 break;
878
879                         /*
880                          *      Find the scsi_device associated with the SCSI
881                          * address. Make sure we have the right array, and if
882                          * so set the flag to initiate a new re-config once we
883                          * see an AifEnConfigChange AIF come through.
884                          */
885
886                         if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
887                                 device = scsi_device_lookup(dev->scsi_host_ptr,
888                                         CONTAINER_TO_CHANNEL(container),
889                                         CONTAINER_TO_ID(container),
890                                         CONTAINER_TO_LUN(container));
891                                 if (device) {
892                                         dev->fsa_dev[container].config_needed = CHANGE;
893                                         dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
894                                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
895                                         scsi_device_put(device);
896                                 }
897                         }
898                 }
899
900                 /*
901                  *      If we are waiting on something and this happens to be
902                  * that thing then set the re-configure flag.
903                  */
904                 if (container != (u32)-1) {
905                         if (container >= dev->maximum_num_containers)
906                                 break;
907                         if ((dev->fsa_dev[container].config_waiting_on ==
908                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
909                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
910                                 dev->fsa_dev[container].config_waiting_on = 0;
911                 } else for (container = 0;
912                     container < dev->maximum_num_containers; ++container) {
913                         if ((dev->fsa_dev[container].config_waiting_on ==
914                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
915                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
916                                 dev->fsa_dev[container].config_waiting_on = 0;
917                 }
918                 break;
919
920         case AifCmdEventNotify:
921                 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
922                 case AifEnBatteryEvent:
923                         dev->cache_protected =
924                                 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
925                         break;
926                 /*
927                  *      Add an Array.
928                  */
929                 case AifEnAddContainer:
930                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
931                         if (container >= dev->maximum_num_containers)
932                                 break;
933                         dev->fsa_dev[container].config_needed = ADD;
934                         dev->fsa_dev[container].config_waiting_on =
935                                 AifEnConfigChange;
936                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
937                         break;
938
939                 /*
940                  *      Delete an Array.
941                  */
942                 case AifEnDeleteContainer:
943                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
944                         if (container >= dev->maximum_num_containers)
945                                 break;
946                         dev->fsa_dev[container].config_needed = DELETE;
947                         dev->fsa_dev[container].config_waiting_on =
948                                 AifEnConfigChange;
949                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
950                         break;
951
952                 /*
953                  *      Container change detected. If we currently are not
954                  * waiting on something else, setup to wait on a Config Change.
955                  */
956                 case AifEnContainerChange:
957                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
958                         if (container >= dev->maximum_num_containers)
959                                 break;
960                         if (dev->fsa_dev[container].config_waiting_on &&
961                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
962                                 break;
963                         dev->fsa_dev[container].config_needed = CHANGE;
964                         dev->fsa_dev[container].config_waiting_on =
965                                 AifEnConfigChange;
966                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
967                         break;
968
969                 case AifEnConfigChange:
970                         break;
971
972                 case AifEnAddJBOD:
973                 case AifEnDeleteJBOD:
974                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
975                         if ((container >> 28)) {
976                                 container = (u32)-1;
977                                 break;
978                         }
979                         channel = (container >> 24) & 0xF;
980                         if (channel >= dev->maximum_num_channels) {
981                                 container = (u32)-1;
982                                 break;
983                         }
984                         id = container & 0xFFFF;
985                         if (id >= dev->maximum_num_physicals) {
986                                 container = (u32)-1;
987                                 break;
988                         }
989                         lun = (container >> 16) & 0xFF;
990                         container = (u32)-1;
991                         channel = aac_phys_to_logical(channel);
992                         device_config_needed =
993                           (((__le32 *)aifcmd->data)[0] ==
994                             cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
995                         if (device_config_needed == ADD) {
996                                 device = scsi_device_lookup(dev->scsi_host_ptr,
997                                         channel,
998                                         id,
999                                         lun);
1000                                 if (device) {
1001                                         scsi_remove_device(device);
1002                                         scsi_device_put(device);
1003                                 }
1004                         }
1005                         break;
1006
1007                 case AifEnEnclosureManagement:
1008                         /*
1009                          * If in JBOD mode, automatic exposure of new
1010                          * physical target to be suppressed until configured.
1011                          */
1012                         if (dev->jbod)
1013                                 break;
1014                         switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1015                         case EM_DRIVE_INSERTION:
1016                         case EM_DRIVE_REMOVAL:
1017                                 container = le32_to_cpu(
1018                                         ((__le32 *)aifcmd->data)[2]);
1019                                 if ((container >> 28)) {
1020                                         container = (u32)-1;
1021                                         break;
1022                                 }
1023                                 channel = (container >> 24) & 0xF;
1024                                 if (channel >= dev->maximum_num_channels) {
1025                                         container = (u32)-1;
1026                                         break;
1027                                 }
1028                                 id = container & 0xFFFF;
1029                                 lun = (container >> 16) & 0xFF;
1030                                 container = (u32)-1;
1031                                 if (id >= dev->maximum_num_physicals) {
1032                                         /* legacy dev_t ? */
1033                                         if ((0x2000 <= id) || lun || channel ||
1034                                           ((channel = (id >> 7) & 0x3F) >=
1035                                           dev->maximum_num_channels))
1036                                                 break;
1037                                         lun = (id >> 4) & 7;
1038                                         id &= 0xF;
1039                                 }
1040                                 channel = aac_phys_to_logical(channel);
1041                                 device_config_needed =
1042                                   (((__le32 *)aifcmd->data)[3]
1043                                     == cpu_to_le32(EM_DRIVE_INSERTION)) ?
1044                                   ADD : DELETE;
1045                                 break;
1046                         }
1047                         break;
1048                 }
1049
1050                 /*
1051                  *      If we are waiting on something and this happens to be
1052                  * that thing then set the re-configure flag.
1053                  */
1054                 if (container != (u32)-1) {
1055                         if (container >= dev->maximum_num_containers)
1056                                 break;
1057                         if ((dev->fsa_dev[container].config_waiting_on ==
1058                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1059                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1060                                 dev->fsa_dev[container].config_waiting_on = 0;
1061                 } else for (container = 0;
1062                     container < dev->maximum_num_containers; ++container) {
1063                         if ((dev->fsa_dev[container].config_waiting_on ==
1064                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1065                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1066                                 dev->fsa_dev[container].config_waiting_on = 0;
1067                 }
1068                 break;
1069
1070         case AifCmdJobProgress:
1071                 /*
1072                  *      These are job progress AIF's. When a Clear is being
1073                  * done on a container it is initially created then hidden from
1074                  * the OS. When the clear completes we don't get a config
1075                  * change so we monitor the job status complete on a clear then
1076                  * wait for a container change.
1077                  */
1078
1079                 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1080                     (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1081                      ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1082                         for (container = 0;
1083                             container < dev->maximum_num_containers;
1084                             ++container) {
1085                                 /*
1086                                  * Stomp on all config sequencing for all
1087                                  * containers?
1088                                  */
1089                                 dev->fsa_dev[container].config_waiting_on =
1090                                         AifEnContainerChange;
1091                                 dev->fsa_dev[container].config_needed = ADD;
1092                                 dev->fsa_dev[container].config_waiting_stamp =
1093                                         jiffies;
1094                         }
1095                 }
1096                 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1097                     ((__le32 *)aifcmd->data)[6] == 0 &&
1098                     ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1099                         for (container = 0;
1100                             container < dev->maximum_num_containers;
1101                             ++container) {
1102                                 /*
1103                                  * Stomp on all config sequencing for all
1104                                  * containers?
1105                                  */
1106                                 dev->fsa_dev[container].config_waiting_on =
1107                                         AifEnContainerChange;
1108                                 dev->fsa_dev[container].config_needed = DELETE;
1109                                 dev->fsa_dev[container].config_waiting_stamp =
1110                                         jiffies;
1111                         }
1112                 }
1113                 break;
1114         }
1115
1116         container = 0;
1117 retry_next:
1118         if (device_config_needed == NOTHING)
1119         for (; container < dev->maximum_num_containers; ++container) {
1120                 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1121                         (dev->fsa_dev[container].config_needed != NOTHING) &&
1122                         time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1123                         device_config_needed =
1124                                 dev->fsa_dev[container].config_needed;
1125                         dev->fsa_dev[container].config_needed = NOTHING;
1126                         channel = CONTAINER_TO_CHANNEL(container);
1127                         id = CONTAINER_TO_ID(container);
1128                         lun = CONTAINER_TO_LUN(container);
1129                         break;
1130                 }
1131         }
1132         if (device_config_needed == NOTHING)
1133                 return;
1134
1135         /*
1136          *      If we decided that a re-configuration needs to be done,
1137          * schedule it here on the way out the door, please close the door
1138          * behind you.
1139          */
1140
1141         /*
1142          *      Find the scsi_device associated with the SCSI address,
1143          * and mark it as changed, invalidating the cache. This deals
1144          * with changes to existing device IDs.
1145          */
1146
1147         if (!dev || !dev->scsi_host_ptr)
1148                 return;
1149         /*
1150          * force reload of disk info via aac_probe_container
1151          */
1152         if ((channel == CONTAINER_CHANNEL) &&
1153           (device_config_needed != NOTHING)) {
1154                 if (dev->fsa_dev[container].valid == 1)
1155                         dev->fsa_dev[container].valid = 2;
1156                 aac_probe_container(dev, container);
1157         }
1158         device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1159         if (device) {
1160                 switch (device_config_needed) {
1161                 case DELETE:
1162 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1163                         scsi_remove_device(device);
1164 #else
1165                         if (scsi_device_online(device)) {
1166                                 scsi_device_set_state(device, SDEV_OFFLINE);
1167                                 sdev_printk(KERN_INFO, device,
1168                                         "Device offlined - %s\n",
1169                                         (channel == CONTAINER_CHANNEL) ?
1170                                                 "array deleted" :
1171                                                 "enclosure services event");
1172                         }
1173 #endif
1174                         break;
1175                 case ADD:
1176                         if (!scsi_device_online(device)) {
1177                                 sdev_printk(KERN_INFO, device,
1178                                         "Device online - %s\n",
1179                                         (channel == CONTAINER_CHANNEL) ?
1180                                                 "array created" :
1181                                                 "enclosure services event");
1182                                 scsi_device_set_state(device, SDEV_RUNNING);
1183                         }
1184                         /* FALLTHRU */
1185                 case CHANGE:
1186                         if ((channel == CONTAINER_CHANNEL)
1187                          && (!dev->fsa_dev[container].valid)) {
1188 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1189                                 scsi_remove_device(device);
1190 #else
1191                                 if (!scsi_device_online(device))
1192                                         break;
1193                                 scsi_device_set_state(device, SDEV_OFFLINE);
1194                                 sdev_printk(KERN_INFO, device,
1195                                         "Device offlined - %s\n",
1196                                         "array failed");
1197 #endif
1198                                 break;
1199                         }
1200                         scsi_rescan_device(&device->sdev_gendev);
1201
1202                 default:
1203                         break;
1204                 }
1205                 scsi_device_put(device);
1206                 device_config_needed = NOTHING;
1207         }
1208         if (device_config_needed == ADD)
1209                 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1210         if (channel == CONTAINER_CHANNEL) {
1211                 container++;
1212                 device_config_needed = NOTHING;
1213                 goto retry_next;
1214         }
1215 }
1216
1217 static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1218 {
1219         int index, quirks;
1220         int retval;
1221         struct Scsi_Host *host;
1222         struct scsi_device *dev;
1223         struct scsi_cmnd *command;
1224         struct scsi_cmnd *command_list;
1225         int jafo = 0;
1226
1227         /*
1228          * Assumptions:
1229          *      - host is locked, unless called by the aacraid thread.
1230          *        (a matter of convenience, due to legacy issues surrounding
1231          *        eh_host_adapter_reset).
1232          *      - in_reset is asserted, so no new i/o is getting to the
1233          *        card.
1234          *      - The card is dead, or will be very shortly ;-/ so no new
1235          *        commands are completing in the interrupt service.
1236          */
1237         host = aac->scsi_host_ptr;
1238         scsi_block_requests(host);
1239         aac_adapter_disable_int(aac);
1240         if (aac->thread->pid != current->pid) {
1241                 spin_unlock_irq(host->host_lock);
1242                 kthread_stop(aac->thread);
1243                 jafo = 1;
1244         }
1245
1246         /*
1247          *      If a positive health, means in a known DEAD PANIC
1248          * state and the adapter could be reset to `try again'.
1249          */
1250         retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1251
1252         if (retval)
1253                 goto out;
1254
1255         /*
1256          *      Loop through the fibs, close the synchronous FIBS
1257          */
1258         for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1259                 struct fib *fib = &aac->fibs[index];
1260                 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1261                   (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1262                         unsigned long flagv;
1263                         spin_lock_irqsave(&fib->event_lock, flagv);
1264                         up(&fib->event_wait);
1265                         spin_unlock_irqrestore(&fib->event_lock, flagv);
1266                         schedule();
1267                         retval = 0;
1268                 }
1269         }
1270         /* Give some extra time for ioctls to complete. */
1271         if (retval == 0)
1272                 ssleep(2);
1273         index = aac->cardtype;
1274
1275         /*
1276          * Re-initialize the adapter, first free resources, then carefully
1277          * apply the initialization sequence to come back again. Only risk
1278          * is a change in Firmware dropping cache, it is assumed the caller
1279          * will ensure that i/o is queisced and the card is flushed in that
1280          * case.
1281          */
1282         aac_fib_map_free(aac);
1283         pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1284         aac->comm_addr = NULL;
1285         aac->comm_phys = 0;
1286         kfree(aac->queues);
1287         aac->queues = NULL;
1288         free_irq(aac->pdev->irq, aac);
1289         if (aac->msi)
1290                 pci_disable_msi(aac->pdev);
1291         kfree(aac->fsa_dev);
1292         aac->fsa_dev = NULL;
1293         quirks = aac_get_driver_ident(index)->quirks;
1294         if (quirks & AAC_QUIRK_31BIT) {
1295                 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1296                   ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1297                         goto out;
1298         } else {
1299                 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1300                   ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1301                         goto out;
1302         }
1303         if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1304                 goto out;
1305         if (quirks & AAC_QUIRK_31BIT)
1306                 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1307                         goto out;
1308         if (jafo) {
1309                 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1310                 if (IS_ERR(aac->thread)) {
1311                         retval = PTR_ERR(aac->thread);
1312                         goto out;
1313                 }
1314         }
1315         (void)aac_get_adapter_info(aac);
1316         if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1317                 host->sg_tablesize = 34;
1318                 host->max_sectors = (host->sg_tablesize * 8) + 112;
1319         }
1320         if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1321                 host->sg_tablesize = 17;
1322                 host->max_sectors = (host->sg_tablesize * 8) + 112;
1323         }
1324         aac_get_config_status(aac, 1);
1325         aac_get_containers(aac);
1326         /*
1327          * This is where the assumption that the Adapter is quiesced
1328          * is important.
1329          */
1330         command_list = NULL;
1331         __shost_for_each_device(dev, host) {
1332                 unsigned long flags;
1333                 spin_lock_irqsave(&dev->list_lock, flags);
1334                 list_for_each_entry(command, &dev->cmd_list, list)
1335                         if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1336                                 command->SCp.buffer = (struct scatterlist *)command_list;
1337                                 command_list = command;
1338                         }
1339                 spin_unlock_irqrestore(&dev->list_lock, flags);
1340         }
1341         while ((command = command_list)) {
1342                 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1343                 command->SCp.buffer = NULL;
1344                 command->result = DID_OK << 16
1345                   | COMMAND_COMPLETE << 8
1346                   | SAM_STAT_TASK_SET_FULL;
1347                 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1348                 command->scsi_done(command);
1349         }
1350         retval = 0;
1351
1352 out:
1353         aac->in_reset = 0;
1354         scsi_unblock_requests(host);
1355         if (jafo) {
1356                 spin_lock_irq(host->host_lock);
1357         }
1358         return retval;
1359 }
1360
1361 int aac_reset_adapter(struct aac_dev * aac, int forced)
1362 {
1363         unsigned long flagv = 0;
1364         int retval;
1365         struct Scsi_Host * host;
1366
1367         if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1368                 return -EBUSY;
1369
1370         if (aac->in_reset) {
1371                 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1372                 return -EBUSY;
1373         }
1374         aac->in_reset = 1;
1375         spin_unlock_irqrestore(&aac->fib_lock, flagv);
1376
1377         /*
1378          * Wait for all commands to complete to this specific
1379          * target (block maximum 60 seconds). Although not necessary,
1380          * it does make us a good storage citizen.
1381          */
1382         host = aac->scsi_host_ptr;
1383         scsi_block_requests(host);
1384         if (forced < 2) for (retval = 60; retval; --retval) {
1385                 struct scsi_device * dev;
1386                 struct scsi_cmnd * command;
1387                 int active = 0;
1388
1389                 __shost_for_each_device(dev, host) {
1390                         spin_lock_irqsave(&dev->list_lock, flagv);
1391                         list_for_each_entry(command, &dev->cmd_list, list) {
1392                                 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1393                                         active++;
1394                                         break;
1395                                 }
1396                         }
1397                         spin_unlock_irqrestore(&dev->list_lock, flagv);
1398                         if (active)
1399                                 break;
1400
1401                 }
1402                 /*
1403                  * We can exit If all the commands are complete
1404                  */
1405                 if (active == 0)
1406                         break;
1407                 ssleep(1);
1408         }
1409
1410         /* Quiesce build, flush cache, write through mode */
1411         if (forced < 2)
1412                 aac_send_shutdown(aac);
1413         spin_lock_irqsave(host->host_lock, flagv);
1414         retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1415         spin_unlock_irqrestore(host->host_lock, flagv);
1416
1417         if ((forced < 2) && (retval == -ENODEV)) {
1418                 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1419                 struct fib * fibctx = aac_fib_alloc(aac);
1420                 if (fibctx) {
1421                         struct aac_pause *cmd;
1422                         int status;
1423
1424                         aac_fib_init(fibctx);
1425
1426                         cmd = (struct aac_pause *) fib_data(fibctx);
1427
1428                         cmd->command = cpu_to_le32(VM_ContainerConfig);
1429                         cmd->type = cpu_to_le32(CT_PAUSE_IO);
1430                         cmd->timeout = cpu_to_le32(1);
1431                         cmd->min = cpu_to_le32(1);
1432                         cmd->noRescan = cpu_to_le32(1);
1433                         cmd->count = cpu_to_le32(0);
1434
1435                         status = aac_fib_send(ContainerCommand,
1436                           fibctx,
1437                           sizeof(struct aac_pause),
1438                           FsaNormal,
1439                           -2 /* Timeout silently */, 1,
1440                           NULL, NULL);
1441
1442                         if (status >= 0)
1443                                 aac_fib_complete(fibctx);
1444                         /* FIB should be freed only after getting
1445                          * the response from the F/W */
1446                         if (status != -ERESTARTSYS)
1447                                 aac_fib_free(fibctx);
1448                 }
1449         }
1450
1451         return retval;
1452 }
1453
1454 int aac_check_health(struct aac_dev * aac)
1455 {
1456         int BlinkLED;
1457         unsigned long time_now, flagv = 0;
1458         struct list_head * entry;
1459         struct Scsi_Host * host;
1460
1461         /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1462         if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1463                 return 0;
1464
1465         if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1466                 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1467                 return 0; /* OK */
1468         }
1469
1470         aac->in_reset = 1;
1471
1472         /* Fake up an AIF:
1473          *      aac_aifcmd.command = AifCmdEventNotify = 1
1474          *      aac_aifcmd.seqnum = 0xFFFFFFFF
1475          *      aac_aifcmd.data[0] = AifEnExpEvent = 23
1476          *      aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1477          *      aac.aifcmd.data[2] = AifHighPriority = 3
1478          *      aac.aifcmd.data[3] = BlinkLED
1479          */
1480
1481         time_now = jiffies/HZ;
1482         entry = aac->fib_list.next;
1483
1484         /*
1485          * For each Context that is on the
1486          * fibctxList, make a copy of the
1487          * fib, and then set the event to wake up the
1488          * thread that is waiting for it.
1489          */
1490         while (entry != &aac->fib_list) {
1491                 /*
1492                  * Extract the fibctx
1493                  */
1494                 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1495                 struct hw_fib * hw_fib;
1496                 struct fib * fib;
1497                 /*
1498                  * Check if the queue is getting
1499                  * backlogged
1500                  */
1501                 if (fibctx->count > 20) {
1502                         /*
1503                          * It's *not* jiffies folks,
1504                          * but jiffies / HZ, so do not
1505                          * panic ...
1506                          */
1507                         u32 time_last = fibctx->jiffies;
1508                         /*
1509                          * Has it been > 2 minutes
1510                          * since the last read off
1511                          * the queue?
1512                          */
1513                         if ((time_now - time_last) > aif_timeout) {
1514                                 entry = entry->next;
1515                                 aac_close_fib_context(aac, fibctx);
1516                                 continue;
1517                         }
1518                 }
1519                 /*
1520                  * Warning: no sleep allowed while
1521                  * holding spinlock
1522                  */
1523                 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1524                 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1525                 if (fib && hw_fib) {
1526                         struct aac_aifcmd * aif;
1527
1528                         fib->hw_fib_va = hw_fib;
1529                         fib->dev = aac;
1530                         aac_fib_init(fib);
1531                         fib->type = FSAFS_NTC_FIB_CONTEXT;
1532                         fib->size = sizeof (struct fib);
1533                         fib->data = hw_fib->data;
1534                         aif = (struct aac_aifcmd *)hw_fib->data;
1535                         aif->command = cpu_to_le32(AifCmdEventNotify);
1536                         aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1537                         ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1538                         ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1539                         ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1540                         ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1541
1542                         /*
1543                          * Put the FIB onto the
1544                          * fibctx's fibs
1545                          */
1546                         list_add_tail(&fib->fiblink, &fibctx->fib_list);
1547                         fibctx->count++;
1548                         /*
1549                          * Set the event to wake up the
1550                          * thread that will waiting.
1551                          */
1552                         up(&fibctx->wait_sem);
1553                 } else {
1554                         printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1555                         kfree(fib);
1556                         kfree(hw_fib);
1557                 }
1558                 entry = entry->next;
1559         }
1560
1561         spin_unlock_irqrestore(&aac->fib_lock, flagv);
1562
1563         if (BlinkLED < 0) {
1564                 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1565                 goto out;
1566         }
1567
1568         printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1569
1570         if (!aac_check_reset || ((aac_check_reset == 1) &&
1571                 (aac->supplement_adapter_info.SupportedOptions2 &
1572                         AAC_OPTION_IGNORE_RESET)))
1573                 goto out;
1574         host = aac->scsi_host_ptr;
1575         if (aac->thread->pid != current->pid)
1576                 spin_lock_irqsave(host->host_lock, flagv);
1577         BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1578         if (aac->thread->pid != current->pid)
1579                 spin_unlock_irqrestore(host->host_lock, flagv);
1580         return BlinkLED;
1581
1582 out:
1583         aac->in_reset = 0;
1584         return BlinkLED;
1585 }
1586
1587
1588 /**
1589  *      aac_command_thread      -       command processing thread
1590  *      @dev: Adapter to monitor
1591  *
1592  *      Waits on the commandready event in it's queue. When the event gets set
1593  *      it will pull FIBs off it's queue. It will continue to pull FIBs off
1594  *      until the queue is empty. When the queue is empty it will wait for
1595  *      more FIBs.
1596  */
1597
1598 int aac_command_thread(void *data)
1599 {
1600         struct aac_dev *dev = data;
1601         struct hw_fib *hw_fib, *hw_newfib;
1602         struct fib *fib, *newfib;
1603         struct aac_fib_context *fibctx;
1604         unsigned long flags;
1605         DECLARE_WAITQUEUE(wait, current);
1606         unsigned long next_jiffies = jiffies + HZ;
1607         unsigned long next_check_jiffies = next_jiffies;
1608         long difference = HZ;
1609
1610         /*
1611          *      We can only have one thread per adapter for AIF's.
1612          */
1613         if (dev->aif_thread)
1614                 return -EINVAL;
1615
1616         /*
1617          *      Let the DPC know it has a place to send the AIF's to.
1618          */
1619         dev->aif_thread = 1;
1620         add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1621         set_current_state(TASK_INTERRUPTIBLE);
1622         dprintk ((KERN_INFO "aac_command_thread start\n"));
1623         while (1) {
1624                 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1625                 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1626                         struct list_head *entry;
1627                         struct aac_aifcmd * aifcmd;
1628
1629                         set_current_state(TASK_RUNNING);
1630
1631                         entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1632                         list_del(entry);
1633
1634                         spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1635                         fib = list_entry(entry, struct fib, fiblink);
1636                         /*
1637                          *      We will process the FIB here or pass it to a
1638                          *      worker thread that is TBD. We Really can't
1639                          *      do anything at this point since we don't have
1640                          *      anything defined for this thread to do.
1641                          */
1642                         hw_fib = fib->hw_fib_va;
1643                         memset(fib, 0, sizeof(struct fib));
1644                         fib->type = FSAFS_NTC_FIB_CONTEXT;
1645                         fib->size = sizeof(struct fib);
1646                         fib->hw_fib_va = hw_fib;
1647                         fib->data = hw_fib->data;
1648                         fib->dev = dev;
1649                         /*
1650                          *      We only handle AifRequest fibs from the adapter.
1651                          */
1652                         aifcmd = (struct aac_aifcmd *) hw_fib->data;
1653                         if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1654                                 /* Handle Driver Notify Events */
1655                                 aac_handle_aif(dev, fib);
1656                                 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1657                                 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1658                         } else {
1659                                 /* The u32 here is important and intended. We are using
1660                                    32bit wrapping time to fit the adapter field */
1661
1662                                 u32 time_now, time_last;
1663                                 unsigned long flagv;
1664                                 unsigned num;
1665                                 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1666                                 struct fib ** fib_pool, ** fib_p;
1667
1668                                 /* Sniff events */
1669                                 if ((aifcmd->command ==
1670                                      cpu_to_le32(AifCmdEventNotify)) ||
1671                                     (aifcmd->command ==
1672                                      cpu_to_le32(AifCmdJobProgress))) {
1673                                         aac_handle_aif(dev, fib);
1674                                 }
1675
1676                                 time_now = jiffies/HZ;
1677
1678                                 /*
1679                                  * Warning: no sleep allowed while
1680                                  * holding spinlock. We take the estimate
1681                                  * and pre-allocate a set of fibs outside the
1682                                  * lock.
1683                                  */
1684                                 num = le32_to_cpu(dev->init->AdapterFibsSize)
1685                                     / sizeof(struct hw_fib); /* some extra */
1686                                 spin_lock_irqsave(&dev->fib_lock, flagv);
1687                                 entry = dev->fib_list.next;
1688                                 while (entry != &dev->fib_list) {
1689                                         entry = entry->next;
1690                                         ++num;
1691                                 }
1692                                 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1693                                 hw_fib_pool = NULL;
1694                                 fib_pool = NULL;
1695                                 if (num
1696                                  && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1697                                  && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1698                                         hw_fib_p = hw_fib_pool;
1699                                         fib_p = fib_pool;
1700                                         while (hw_fib_p < &hw_fib_pool[num]) {
1701                                                 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1702                                                         --hw_fib_p;
1703                                                         break;
1704                                                 }
1705                                                 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1706                                                         kfree(*(--hw_fib_p));
1707                                                         break;
1708                                                 }
1709                                         }
1710                                         if ((num = hw_fib_p - hw_fib_pool) == 0) {
1711                                                 kfree(fib_pool);
1712                                                 fib_pool = NULL;
1713                                                 kfree(hw_fib_pool);
1714                                                 hw_fib_pool = NULL;
1715                                         }
1716                                 } else {
1717                                         kfree(hw_fib_pool);
1718                                         hw_fib_pool = NULL;
1719                                 }
1720                                 spin_lock_irqsave(&dev->fib_lock, flagv);
1721                                 entry = dev->fib_list.next;
1722                                 /*
1723                                  * For each Context that is on the
1724                                  * fibctxList, make a copy of the
1725                                  * fib, and then set the event to wake up the
1726                                  * thread that is waiting for it.
1727                                  */
1728                                 hw_fib_p = hw_fib_pool;
1729                                 fib_p = fib_pool;
1730                                 while (entry != &dev->fib_list) {
1731                                         /*
1732                                          * Extract the fibctx
1733                                          */
1734                                         fibctx = list_entry(entry, struct aac_fib_context, next);
1735                                         /*
1736                                          * Check if the queue is getting
1737                                          * backlogged
1738                                          */
1739                                         if (fibctx->count > 20)
1740                                         {
1741                                                 /*
1742                                                  * It's *not* jiffies folks,
1743                                                  * but jiffies / HZ so do not
1744                                                  * panic ...
1745                                                  */
1746                                                 time_last = fibctx->jiffies;
1747                                                 /*
1748                                                  * Has it been > 2 minutes
1749                                                  * since the last read off
1750                                                  * the queue?
1751                                                  */
1752                                                 if ((time_now - time_last) > aif_timeout) {
1753                                                         entry = entry->next;
1754                                                         aac_close_fib_context(dev, fibctx);
1755                                                         continue;
1756                                                 }
1757                                         }
1758                                         /*
1759                                          * Warning: no sleep allowed while
1760                                          * holding spinlock
1761                                          */
1762                                         if (hw_fib_p < &hw_fib_pool[num]) {
1763                                                 hw_newfib = *hw_fib_p;
1764                                                 *(hw_fib_p++) = NULL;
1765                                                 newfib = *fib_p;
1766                                                 *(fib_p++) = NULL;
1767                                                 /*
1768                                                  * Make the copy of the FIB
1769                                                  */
1770                                                 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1771                                                 memcpy(newfib, fib, sizeof(struct fib));
1772                                                 newfib->hw_fib_va = hw_newfib;
1773                                                 /*
1774                                                  * Put the FIB onto the
1775                                                  * fibctx's fibs
1776                                                  */
1777                                                 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1778                                                 fibctx->count++;
1779                                                 /*
1780                                                  * Set the event to wake up the
1781                                                  * thread that is waiting.
1782                                                  */
1783                                                 up(&fibctx->wait_sem);
1784                                         } else {
1785                                                 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1786                                         }
1787                                         entry = entry->next;
1788                                 }
1789                                 /*
1790                                  *      Set the status of this FIB
1791                                  */
1792                                 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1793                                 aac_fib_adapter_complete(fib, sizeof(u32));
1794                                 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1795                                 /* Free up the remaining resources */
1796                                 hw_fib_p = hw_fib_pool;
1797                                 fib_p = fib_pool;
1798                                 while (hw_fib_p < &hw_fib_pool[num]) {
1799                                         kfree(*hw_fib_p);
1800                                         kfree(*fib_p);
1801                                         ++fib_p;
1802                                         ++hw_fib_p;
1803                                 }
1804                                 kfree(hw_fib_pool);
1805                                 kfree(fib_pool);
1806                         }
1807                         kfree(fib);
1808                         spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1809                 }
1810                 /*
1811                  *      There are no more AIF's
1812                  */
1813                 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1814
1815                 /*
1816                  *      Background activity
1817                  */
1818                 if ((time_before(next_check_jiffies,next_jiffies))
1819                  && ((difference = next_check_jiffies - jiffies) <= 0)) {
1820                         next_check_jiffies = next_jiffies;
1821                         if (aac_check_health(dev) == 0) {
1822                                 difference = ((long)(unsigned)check_interval)
1823                                            * HZ;
1824                                 next_check_jiffies = jiffies + difference;
1825                         } else if (!dev->queues)
1826                                 break;
1827                 }
1828                 if (!time_before(next_check_jiffies,next_jiffies)
1829                  && ((difference = next_jiffies - jiffies) <= 0)) {
1830                         struct timeval now;
1831                         int ret;
1832
1833                         /* Don't even try to talk to adapter if its sick */
1834                         ret = aac_check_health(dev);
1835                         if (!ret && !dev->queues)
1836                                 break;
1837                         next_check_jiffies = jiffies
1838                                            + ((long)(unsigned)check_interval)
1839                                            * HZ;
1840                         do_gettimeofday(&now);
1841
1842                         /* Synchronize our watches */
1843                         if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1844                          && (now.tv_usec > (1000000 / HZ)))
1845                                 difference = (((1000000 - now.tv_usec) * HZ)
1846                                   + 500000) / 1000000;
1847                         else if (ret == 0) {
1848                                 struct fib *fibptr;
1849
1850                                 if ((fibptr = aac_fib_alloc(dev))) {
1851                                         int status;
1852                                         __le32 *info;
1853
1854                                         aac_fib_init(fibptr);
1855
1856                                         info = (__le32 *) fib_data(fibptr);
1857                                         if (now.tv_usec > 500000)
1858                                                 ++now.tv_sec;
1859
1860                                         *info = cpu_to_le32(now.tv_sec);
1861
1862                                         status = aac_fib_send(SendHostTime,
1863                                                 fibptr,
1864                                                 sizeof(*info),
1865                                                 FsaNormal,
1866                                                 1, 1,
1867                                                 NULL,
1868                                                 NULL);
1869                                         /* Do not set XferState to zero unless
1870                                          * receives a response from F/W */
1871                                         if (status >= 0)
1872                                                 aac_fib_complete(fibptr);
1873                                         /* FIB should be freed only after
1874                                          * getting the response from the F/W */
1875                                         if (status != -ERESTARTSYS)
1876                                                 aac_fib_free(fibptr);
1877                                 }
1878                                 difference = (long)(unsigned)update_interval*HZ;
1879                         } else {
1880                                 /* retry shortly */
1881                                 difference = 10 * HZ;
1882                         }
1883                         next_jiffies = jiffies + difference;
1884                         if (time_before(next_check_jiffies,next_jiffies))
1885                                 difference = next_check_jiffies - jiffies;
1886                 }
1887                 if (difference <= 0)
1888                         difference = 1;
1889                 set_current_state(TASK_INTERRUPTIBLE);
1890
1891                 if (kthread_should_stop())
1892                         break;
1893
1894                 schedule_timeout(difference);
1895
1896                 if (kthread_should_stop())
1897                         break;
1898         }
1899         if (dev->queues)
1900                 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1901         dev->aif_thread = 0;
1902         return 0;
1903 }