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