Merge tag 'vfio-v4.14-rc1' of git://github.com/awilliam/linux-vfio
[linux-2.6-microblaze.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-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10  *               2016-2017 Microsemi Corp. (aacraid@microsemi.com)
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; see the file COPYING.  If not, write to
24  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Module Name:
27  *  commsup.c
28  *
29  * Abstract: Contain all routines that are required for FSA host/adapter
30  *    communication.
31  *
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/sched.h>
38 #include <linux/pci.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/completion.h>
42 #include <linux/blkdev.h>
43 #include <linux/delay.h>
44 #include <linux/kthread.h>
45 #include <linux/interrupt.h>
46 #include <linux/semaphore.h>
47 #include <linux/bcd.h>
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_host.h>
50 #include <scsi/scsi_device.h>
51 #include <scsi/scsi_cmnd.h>
52
53 #include "aacraid.h"
54
55 /**
56  *      fib_map_alloc           -       allocate the fib objects
57  *      @dev: Adapter to allocate for
58  *
59  *      Allocate and map the shared PCI space for the FIB blocks used to
60  *      talk to the Adaptec firmware.
61  */
62
63 static int fib_map_alloc(struct aac_dev *dev)
64 {
65         if (dev->max_fib_size > AAC_MAX_NATIVE_SIZE)
66                 dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
67         else
68                 dev->max_cmd_size = dev->max_fib_size;
69         if (dev->max_fib_size < AAC_MAX_NATIVE_SIZE) {
70                 dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
71         } else {
72                 dev->max_cmd_size = dev->max_fib_size;
73         }
74
75         dprintk((KERN_INFO
76           "allocate hardware fibs dma_alloc_coherent(%p, %d * (%d + %d), %p)\n",
77           &dev->pdev->dev, dev->max_cmd_size, dev->scsi_host_ptr->can_queue,
78           AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
79         dev->hw_fib_va = dma_alloc_coherent(&dev->pdev->dev,
80                 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr))
81                 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
82                 &dev->hw_fib_pa, GFP_KERNEL);
83         if (dev->hw_fib_va == NULL)
84                 return -ENOMEM;
85         return 0;
86 }
87
88 /**
89  *      aac_fib_map_free                -       free the fib objects
90  *      @dev: Adapter to free
91  *
92  *      Free the PCI mappings and the memory allocated for FIB blocks
93  *      on this adapter.
94  */
95
96 void aac_fib_map_free(struct aac_dev *dev)
97 {
98         size_t alloc_size;
99         size_t fib_size;
100         int num_fibs;
101
102         if(!dev->hw_fib_va || !dev->max_cmd_size)
103                 return;
104
105         num_fibs = dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
106         fib_size = dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
107         alloc_size = fib_size * num_fibs + ALIGN32 - 1;
108
109         dma_free_coherent(&dev->pdev->dev, alloc_size, dev->hw_fib_va,
110                           dev->hw_fib_pa);
111
112         dev->hw_fib_va = NULL;
113         dev->hw_fib_pa = 0;
114 }
115
116 void aac_fib_vector_assign(struct aac_dev *dev)
117 {
118         u32 i = 0;
119         u32 vector = 1;
120         struct fib *fibptr = NULL;
121
122         for (i = 0, fibptr = &dev->fibs[i];
123                 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
124                 i++, fibptr++) {
125                 if ((dev->max_msix == 1) ||
126                   (i > ((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1)
127                         - dev->vector_cap))) {
128                         fibptr->vector_no = 0;
129                 } else {
130                         fibptr->vector_no = vector;
131                         vector++;
132                         if (vector == dev->max_msix)
133                                 vector = 1;
134                 }
135         }
136 }
137
138 /**
139  *      aac_fib_setup   -       setup the fibs
140  *      @dev: Adapter to set up
141  *
142  *      Allocate the PCI space for the fibs, map it and then initialise the
143  *      fib area, the unmapped fib data and also the free list
144  */
145
146 int aac_fib_setup(struct aac_dev * dev)
147 {
148         struct fib *fibptr;
149         struct hw_fib *hw_fib;
150         dma_addr_t hw_fib_pa;
151         int i;
152         u32 max_cmds;
153
154         while (((i = fib_map_alloc(dev)) == -ENOMEM)
155          && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
156                 max_cmds = (dev->scsi_host_ptr->can_queue+AAC_NUM_MGT_FIB) >> 1;
157                 dev->scsi_host_ptr->can_queue = max_cmds - AAC_NUM_MGT_FIB;
158                 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3)
159                         dev->init->r7.max_io_commands = cpu_to_le32(max_cmds);
160         }
161         if (i<0)
162                 return -ENOMEM;
163
164         memset(dev->hw_fib_va, 0,
165                 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) *
166                 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
167
168         /* 32 byte alignment for PMC */
169         hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
170         hw_fib    = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
171                                         (hw_fib_pa - dev->hw_fib_pa));
172
173         /* add Xport header */
174         hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
175                 sizeof(struct aac_fib_xporthdr));
176         hw_fib_pa += sizeof(struct aac_fib_xporthdr);
177
178         /*
179          *      Initialise the fibs
180          */
181         for (i = 0, fibptr = &dev->fibs[i];
182                 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
183                 i++, fibptr++)
184         {
185                 fibptr->flags = 0;
186                 fibptr->size = sizeof(struct fib);
187                 fibptr->dev = dev;
188                 fibptr->hw_fib_va = hw_fib;
189                 fibptr->data = (void *) fibptr->hw_fib_va->data;
190                 fibptr->next = fibptr+1;        /* Forward chain the fibs */
191                 sema_init(&fibptr->event_wait, 0);
192                 spin_lock_init(&fibptr->event_lock);
193                 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
194                 hw_fib->header.SenderSize =
195                         cpu_to_le16(dev->max_fib_size); /* ?? max_cmd_size */
196                 fibptr->hw_fib_pa = hw_fib_pa;
197                 fibptr->hw_sgl_pa = hw_fib_pa +
198                         offsetof(struct aac_hba_cmd_req, sge[2]);
199                 /*
200                  * one element is for the ptr to the separate sg list,
201                  * second element for 32 byte alignment
202                  */
203                 fibptr->hw_error_pa = hw_fib_pa +
204                         offsetof(struct aac_native_hba, resp.resp_bytes[0]);
205
206                 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
207                         dev->max_cmd_size + sizeof(struct aac_fib_xporthdr));
208                 hw_fib_pa = hw_fib_pa +
209                         dev->max_cmd_size + sizeof(struct aac_fib_xporthdr);
210         }
211
212         /*
213          *Assign vector numbers to fibs
214          */
215         aac_fib_vector_assign(dev);
216
217         /*
218          *      Add the fib chain to the free list
219          */
220         dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
221         /*
222         *       Set 8 fibs aside for management tools
223         */
224         dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue];
225         return 0;
226 }
227
228 /**
229  *      aac_fib_alloc_tag-allocate a fib using tags
230  *      @dev: Adapter to allocate the fib for
231  *
232  *      Allocate a fib from the adapter fib pool using tags
233  *      from the blk layer.
234  */
235
236 struct fib *aac_fib_alloc_tag(struct aac_dev *dev, struct scsi_cmnd *scmd)
237 {
238         struct fib *fibptr;
239
240         fibptr = &dev->fibs[scmd->request->tag];
241         /*
242          *      Null out fields that depend on being zero at the start of
243          *      each I/O
244          */
245         fibptr->hw_fib_va->header.XferState = 0;
246         fibptr->type = FSAFS_NTC_FIB_CONTEXT;
247         fibptr->callback_data = NULL;
248         fibptr->callback = NULL;
249
250         return fibptr;
251 }
252
253 /**
254  *      aac_fib_alloc   -       allocate a fib
255  *      @dev: Adapter to allocate the fib for
256  *
257  *      Allocate a fib from the adapter fib pool. If the pool is empty we
258  *      return NULL.
259  */
260
261 struct fib *aac_fib_alloc(struct aac_dev *dev)
262 {
263         struct fib * fibptr;
264         unsigned long flags;
265         spin_lock_irqsave(&dev->fib_lock, flags);
266         fibptr = dev->free_fib;
267         if(!fibptr){
268                 spin_unlock_irqrestore(&dev->fib_lock, flags);
269                 return fibptr;
270         }
271         dev->free_fib = fibptr->next;
272         spin_unlock_irqrestore(&dev->fib_lock, flags);
273         /*
274          *      Set the proper node type code and node byte size
275          */
276         fibptr->type = FSAFS_NTC_FIB_CONTEXT;
277         fibptr->size = sizeof(struct fib);
278         /*
279          *      Null out fields that depend on being zero at the start of
280          *      each I/O
281          */
282         fibptr->hw_fib_va->header.XferState = 0;
283         fibptr->flags = 0;
284         fibptr->callback = NULL;
285         fibptr->callback_data = NULL;
286
287         return fibptr;
288 }
289
290 /**
291  *      aac_fib_free    -       free a fib
292  *      @fibptr: fib to free up
293  *
294  *      Frees up a fib and places it on the appropriate queue
295  */
296
297 void aac_fib_free(struct fib *fibptr)
298 {
299         unsigned long flags;
300
301         if (fibptr->done == 2)
302                 return;
303
304         spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
305         if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
306                 aac_config.fib_timeouts++;
307         if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) &&
308                 fibptr->hw_fib_va->header.XferState != 0) {
309                 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
310                          (void*)fibptr,
311                          le32_to_cpu(fibptr->hw_fib_va->header.XferState));
312         }
313         fibptr->next = fibptr->dev->free_fib;
314         fibptr->dev->free_fib = fibptr;
315         spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
316 }
317
318 /**
319  *      aac_fib_init    -       initialise a fib
320  *      @fibptr: The fib to initialize
321  *
322  *      Set up the generic fib fields ready for use
323  */
324
325 void aac_fib_init(struct fib *fibptr)
326 {
327         struct hw_fib *hw_fib = fibptr->hw_fib_va;
328
329         memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
330         hw_fib->header.StructType = FIB_MAGIC;
331         hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
332         hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
333         hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
334         hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
335 }
336
337 /**
338  *      fib_deallocate          -       deallocate a fib
339  *      @fibptr: fib to deallocate
340  *
341  *      Will deallocate and return to the free pool the FIB pointed to by the
342  *      caller.
343  */
344
345 static void fib_dealloc(struct fib * fibptr)
346 {
347         struct hw_fib *hw_fib = fibptr->hw_fib_va;
348         hw_fib->header.XferState = 0;
349 }
350
351 /*
352  *      Commuication primitives define and support the queuing method we use to
353  *      support host to adapter commuication. All queue accesses happen through
354  *      these routines and are the only routines which have a knowledge of the
355  *       how these queues are implemented.
356  */
357
358 /**
359  *      aac_get_entry           -       get a queue entry
360  *      @dev: Adapter
361  *      @qid: Queue Number
362  *      @entry: Entry return
363  *      @index: Index return
364  *      @nonotify: notification control
365  *
366  *      With a priority the routine returns a queue entry if the queue has free entries. If the queue
367  *      is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
368  *      returned.
369  */
370
371 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
372 {
373         struct aac_queue * q;
374         unsigned long idx;
375
376         /*
377          *      All of the queues wrap when they reach the end, so we check
378          *      to see if they have reached the end and if they have we just
379          *      set the index back to zero. This is a wrap. You could or off
380          *      the high bits in all updates but this is a bit faster I think.
381          */
382
383         q = &dev->queues->queue[qid];
384
385         idx = *index = le32_to_cpu(*(q->headers.producer));
386         /* Interrupt Moderation, only interrupt for first two entries */
387         if (idx != le32_to_cpu(*(q->headers.consumer))) {
388                 if (--idx == 0) {
389                         if (qid == AdapNormCmdQueue)
390                                 idx = ADAP_NORM_CMD_ENTRIES;
391                         else
392                                 idx = ADAP_NORM_RESP_ENTRIES;
393                 }
394                 if (idx != le32_to_cpu(*(q->headers.consumer)))
395                         *nonotify = 1;
396         }
397
398         if (qid == AdapNormCmdQueue) {
399                 if (*index >= ADAP_NORM_CMD_ENTRIES)
400                         *index = 0; /* Wrap to front of the Producer Queue. */
401         } else {
402                 if (*index >= ADAP_NORM_RESP_ENTRIES)
403                         *index = 0; /* Wrap to front of the Producer Queue. */
404         }
405
406         /* Queue is full */
407         if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
408                 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
409                                 qid, atomic_read(&q->numpending));
410                 return 0;
411         } else {
412                 *entry = q->base + *index;
413                 return 1;
414         }
415 }
416
417 /**
418  *      aac_queue_get           -       get the next free QE
419  *      @dev: Adapter
420  *      @index: Returned index
421  *      @priority: Priority of fib
422  *      @fib: Fib to associate with the queue entry
423  *      @wait: Wait if queue full
424  *      @fibptr: Driver fib object to go with fib
425  *      @nonotify: Don't notify the adapter
426  *
427  *      Gets the next free QE off the requested priorty adapter command
428  *      queue and associates the Fib with the QE. The QE represented by
429  *      index is ready to insert on the queue when this routine returns
430  *      success.
431  */
432
433 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)
434 {
435         struct aac_entry * entry = NULL;
436         int map = 0;
437
438         if (qid == AdapNormCmdQueue) {
439                 /*  if no entries wait for some if caller wants to */
440                 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
441                         printk(KERN_ERR "GetEntries failed\n");
442                 }
443                 /*
444                  *      Setup queue entry with a command, status and fib mapped
445                  */
446                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
447                 map = 1;
448         } else {
449                 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
450                         /* if no entries wait for some if caller wants to */
451                 }
452                 /*
453                  *      Setup queue entry with command, status and fib mapped
454                  */
455                 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
456                 entry->addr = hw_fib->header.SenderFibAddress;
457                         /* Restore adapters pointer to the FIB */
458                 hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress;  /* Let the adapter now where to find its data */
459                 map = 0;
460         }
461         /*
462          *      If MapFib is true than we need to map the Fib and put pointers
463          *      in the queue entry.
464          */
465         if (map)
466                 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
467         return 0;
468 }
469
470 #ifdef CONFIG_EEH
471 static inline int aac_check_eeh_failure(struct aac_dev *dev)
472 {
473         /* Check for an EEH failure for the given
474          * device node. Function eeh_dev_check_failure()
475          * returns 0 if there has not been an EEH error
476          * otherwise returns a non-zero value.
477          *
478          * Need to be called before any PCI operation,
479          * i.e.,before aac_adapter_check_health()
480          */
481         struct eeh_dev *edev = pci_dev_to_eeh_dev(dev->pdev);
482
483         if (eeh_dev_check_failure(edev)) {
484                 /* The EEH mechanisms will handle this
485                  * error and reset the device if
486                  * necessary.
487                  */
488                 return 1;
489         }
490         return 0;
491 }
492 #else
493 static inline int aac_check_eeh_failure(struct aac_dev *dev)
494 {
495         return 0;
496 }
497 #endif
498
499 /*
500  *      Define the highest level of host to adapter communication routines.
501  *      These routines will support host to adapter FS commuication. These
502  *      routines have no knowledge of the commuication method used. This level
503  *      sends and receives FIBs. This level has no knowledge of how these FIBs
504  *      get passed back and forth.
505  */
506
507 /**
508  *      aac_fib_send    -       send a fib to the adapter
509  *      @command: Command to send
510  *      @fibptr: The fib
511  *      @size: Size of fib data area
512  *      @priority: Priority of Fib
513  *      @wait: Async/sync select
514  *      @reply: True if a reply is wanted
515  *      @callback: Called with reply
516  *      @callback_data: Passed to callback
517  *
518  *      Sends the requested FIB to the adapter and optionally will wait for a
519  *      response FIB. If the caller does not wish to wait for a response than
520  *      an event to wait on must be supplied. This event will be set when a
521  *      response FIB is received from the adapter.
522  */
523
524 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
525                 int priority, int wait, int reply, fib_callback callback,
526                 void *callback_data)
527 {
528         struct aac_dev * dev = fibptr->dev;
529         struct hw_fib * hw_fib = fibptr->hw_fib_va;
530         unsigned long flags = 0;
531         unsigned long mflags = 0;
532         unsigned long sflags = 0;
533
534         if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
535                 return -EBUSY;
536
537         if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))
538                 return -EINVAL;
539
540         /*
541          *      There are 5 cases with the wait and response requested flags.
542          *      The only invalid cases are if the caller requests to wait and
543          *      does not request a response and if the caller does not want a
544          *      response and the Fib is not allocated from pool. If a response
545          *      is not requesed the Fib will just be deallocaed by the DPC
546          *      routine when the response comes back from the adapter. No
547          *      further processing will be done besides deleting the Fib. We
548          *      will have a debug mode where the adapter can notify the host
549          *      it had a problem and the host can log that fact.
550          */
551         fibptr->flags = 0;
552         if (wait && !reply) {
553                 return -EINVAL;
554         } else if (!wait && reply) {
555                 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
556                 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
557         } else if (!wait && !reply) {
558                 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
559                 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
560         } else if (wait && reply) {
561                 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
562                 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
563         }
564         /*
565          *      Map the fib into 32bits by using the fib number
566          */
567
568         hw_fib->header.SenderFibAddress =
569                 cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
570
571         /* use the same shifted value for handle to be compatible
572          * with the new native hba command handle
573          */
574         hw_fib->header.Handle =
575                 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
576
577         /*
578          *      Set FIB state to indicate where it came from and if we want a
579          *      response from the adapter. Also load the command from the
580          *      caller.
581          *
582          *      Map the hw fib pointer as a 32bit value
583          */
584         hw_fib->header.Command = cpu_to_le16(command);
585         hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
586         /*
587          *      Set the size of the Fib we want to send to the adapter
588          */
589         hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
590         if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
591                 return -EMSGSIZE;
592         }
593         /*
594          *      Get a queue entry connect the FIB to it and send an notify
595          *      the adapter a command is ready.
596          */
597         hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
598
599         /*
600          *      Fill in the Callback and CallbackContext if we are not
601          *      going to wait.
602          */
603         if (!wait) {
604                 fibptr->callback = callback;
605                 fibptr->callback_data = callback_data;
606                 fibptr->flags = FIB_CONTEXT_FLAG;
607         }
608
609         fibptr->done = 0;
610
611         FIB_COUNTER_INCREMENT(aac_config.FibsSent);
612
613         dprintk((KERN_DEBUG "Fib contents:.\n"));
614         dprintk((KERN_DEBUG "  Command =               %d.\n", le32_to_cpu(hw_fib->header.Command)));
615         dprintk((KERN_DEBUG "  SubCommand =            %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
616         dprintk((KERN_DEBUG "  XferState  =            %x.\n", le32_to_cpu(hw_fib->header.XferState)));
617         dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib_va));
618         dprintk((KERN_DEBUG "  hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
619         dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));
620
621         if (!dev->queues)
622                 return -EBUSY;
623
624         if (wait) {
625
626                 spin_lock_irqsave(&dev->manage_lock, mflags);
627                 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
628                         printk(KERN_INFO "No management Fibs Available:%d\n",
629                                                 dev->management_fib_count);
630                         spin_unlock_irqrestore(&dev->manage_lock, mflags);
631                         return -EBUSY;
632                 }
633                 dev->management_fib_count++;
634                 spin_unlock_irqrestore(&dev->manage_lock, mflags);
635                 spin_lock_irqsave(&fibptr->event_lock, flags);
636         }
637
638         if (dev->sync_mode) {
639                 if (wait)
640                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
641                 spin_lock_irqsave(&dev->sync_lock, sflags);
642                 if (dev->sync_fib) {
643                         list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
644                         spin_unlock_irqrestore(&dev->sync_lock, sflags);
645                 } else {
646                         dev->sync_fib = fibptr;
647                         spin_unlock_irqrestore(&dev->sync_lock, sflags);
648                         aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
649                                 (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
650                                 NULL, NULL, NULL, NULL, NULL);
651                 }
652                 if (wait) {
653                         fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
654                         if (down_interruptible(&fibptr->event_wait)) {
655                                 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
656                                 return -EFAULT;
657                         }
658                         return 0;
659                 }
660                 return -EINPROGRESS;
661         }
662
663         if (aac_adapter_deliver(fibptr) != 0) {
664                 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
665                 if (wait) {
666                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
667                         spin_lock_irqsave(&dev->manage_lock, mflags);
668                         dev->management_fib_count--;
669                         spin_unlock_irqrestore(&dev->manage_lock, mflags);
670                 }
671                 return -EBUSY;
672         }
673
674
675         /*
676          *      If the caller wanted us to wait for response wait now.
677          */
678
679         if (wait) {
680                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
681                 /* Only set for first known interruptable command */
682                 if (wait < 0) {
683                         /*
684                          * *VERY* Dangerous to time out a command, the
685                          * assumption is made that we have no hope of
686                          * functioning because an interrupt routing or other
687                          * hardware failure has occurred.
688                          */
689                         unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
690                         while (down_trylock(&fibptr->event_wait)) {
691                                 int blink;
692                                 if (time_is_before_eq_jiffies(timeout)) {
693                                         struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
694                                         atomic_dec(&q->numpending);
695                                         if (wait == -1) {
696                                                 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
697                                                   "Usually a result of a PCI interrupt routing problem;\n"
698                                                   "update mother board BIOS or consider utilizing one of\n"
699                                                   "the SAFE mode kernel options (acpi, apic etc)\n");
700                                         }
701                                         return -ETIMEDOUT;
702                                 }
703
704                                 if (aac_check_eeh_failure(dev))
705                                         return -EFAULT;
706
707                                 if ((blink = aac_adapter_check_health(dev)) > 0) {
708                                         if (wait == -1) {
709                                                 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
710                                                   "Usually a result of a serious unrecoverable hardware problem\n",
711                                                   blink);
712                                         }
713                                         return -EFAULT;
714                                 }
715                                 /*
716                                  * Allow other processes / CPUS to use core
717                                  */
718                                 schedule();
719                         }
720                 } else if (down_interruptible(&fibptr->event_wait)) {
721                         /* Do nothing ... satisfy
722                          * down_interruptible must_check */
723                 }
724
725                 spin_lock_irqsave(&fibptr->event_lock, flags);
726                 if (fibptr->done == 0) {
727                         fibptr->done = 2; /* Tell interrupt we aborted */
728                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
729                         return -ERESTARTSYS;
730                 }
731                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
732                 BUG_ON(fibptr->done == 0);
733
734                 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
735                         return -ETIMEDOUT;
736                 return 0;
737         }
738         /*
739          *      If the user does not want a response than return success otherwise
740          *      return pending
741          */
742         if (reply)
743                 return -EINPROGRESS;
744         else
745                 return 0;
746 }
747
748 int aac_hba_send(u8 command, struct fib *fibptr, fib_callback callback,
749                 void *callback_data)
750 {
751         struct aac_dev *dev = fibptr->dev;
752         int wait;
753         unsigned long flags = 0;
754         unsigned long mflags = 0;
755
756         fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA);
757         if (callback) {
758                 wait = 0;
759                 fibptr->callback = callback;
760                 fibptr->callback_data = callback_data;
761         } else
762                 wait = 1;
763
764
765         if (command == HBA_IU_TYPE_SCSI_CMD_REQ) {
766                 struct aac_hba_cmd_req *hbacmd =
767                         (struct aac_hba_cmd_req *)fibptr->hw_fib_va;
768
769                 hbacmd->iu_type = command;
770                 /* bit1 of request_id must be 0 */
771                 hbacmd->request_id =
772                         cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
773                 fibptr->flags |= FIB_CONTEXT_FLAG_SCSI_CMD;
774         } else if (command != HBA_IU_TYPE_SCSI_TM_REQ)
775                 return -EINVAL;
776
777
778         if (wait) {
779                 spin_lock_irqsave(&dev->manage_lock, mflags);
780                 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
781                         spin_unlock_irqrestore(&dev->manage_lock, mflags);
782                         return -EBUSY;
783                 }
784                 dev->management_fib_count++;
785                 spin_unlock_irqrestore(&dev->manage_lock, mflags);
786                 spin_lock_irqsave(&fibptr->event_lock, flags);
787         }
788
789         if (aac_adapter_deliver(fibptr) != 0) {
790                 if (wait) {
791                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
792                         spin_lock_irqsave(&dev->manage_lock, mflags);
793                         dev->management_fib_count--;
794                         spin_unlock_irqrestore(&dev->manage_lock, mflags);
795                 }
796                 return -EBUSY;
797         }
798         FIB_COUNTER_INCREMENT(aac_config.NativeSent);
799
800         if (wait) {
801
802                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
803
804                 if (aac_check_eeh_failure(dev))
805                         return -EFAULT;
806
807                 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
808                 if (down_interruptible(&fibptr->event_wait))
809                         fibptr->done = 2;
810                 fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);
811
812                 spin_lock_irqsave(&fibptr->event_lock, flags);
813                 if ((fibptr->done == 0) || (fibptr->done == 2)) {
814                         fibptr->done = 2; /* Tell interrupt we aborted */
815                         spin_unlock_irqrestore(&fibptr->event_lock, flags);
816                         return -ERESTARTSYS;
817                 }
818                 spin_unlock_irqrestore(&fibptr->event_lock, flags);
819                 WARN_ON(fibptr->done == 0);
820
821                 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
822                         return -ETIMEDOUT;
823
824                 return 0;
825         }
826
827         return -EINPROGRESS;
828 }
829
830 /**
831  *      aac_consumer_get        -       get the top of the queue
832  *      @dev: Adapter
833  *      @q: Queue
834  *      @entry: Return entry
835  *
836  *      Will return a pointer to the entry on the top of the queue requested that
837  *      we are a consumer of, and return the address of the queue entry. It does
838  *      not change the state of the queue.
839  */
840
841 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
842 {
843         u32 index;
844         int status;
845         if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
846                 status = 0;
847         } else {
848                 /*
849                  *      The consumer index must be wrapped if we have reached
850                  *      the end of the queue, else we just use the entry
851                  *      pointed to by the header index
852                  */
853                 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
854                         index = 0;
855                 else
856                         index = le32_to_cpu(*q->headers.consumer);
857                 *entry = q->base + index;
858                 status = 1;
859         }
860         return(status);
861 }
862
863 /**
864  *      aac_consumer_free       -       free consumer entry
865  *      @dev: Adapter
866  *      @q: Queue
867  *      @qid: Queue ident
868  *
869  *      Frees up the current top of the queue we are a consumer of. If the
870  *      queue was full notify the producer that the queue is no longer full.
871  */
872
873 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
874 {
875         int wasfull = 0;
876         u32 notify;
877
878         if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
879                 wasfull = 1;
880
881         if (le32_to_cpu(*q->headers.consumer) >= q->entries)
882                 *q->headers.consumer = cpu_to_le32(1);
883         else
884                 le32_add_cpu(q->headers.consumer, 1);
885
886         if (wasfull) {
887                 switch (qid) {
888
889                 case HostNormCmdQueue:
890                         notify = HostNormCmdNotFull;
891                         break;
892                 case HostNormRespQueue:
893                         notify = HostNormRespNotFull;
894                         break;
895                 default:
896                         BUG();
897                         return;
898                 }
899                 aac_adapter_notify(dev, notify);
900         }
901 }
902
903 /**
904  *      aac_fib_adapter_complete        -       complete adapter issued fib
905  *      @fibptr: fib to complete
906  *      @size: size of fib
907  *
908  *      Will do all necessary work to complete a FIB that was sent from
909  *      the adapter.
910  */
911
912 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
913 {
914         struct hw_fib * hw_fib = fibptr->hw_fib_va;
915         struct aac_dev * dev = fibptr->dev;
916         struct aac_queue * q;
917         unsigned long nointr = 0;
918         unsigned long qflags;
919
920         if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
921                 dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
922                 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
923                 kfree(hw_fib);
924                 return 0;
925         }
926
927         if (hw_fib->header.XferState == 0) {
928                 if (dev->comm_interface == AAC_COMM_MESSAGE)
929                         kfree(hw_fib);
930                 return 0;
931         }
932         /*
933          *      If we plan to do anything check the structure type first.
934          */
935         if (hw_fib->header.StructType != FIB_MAGIC &&
936             hw_fib->header.StructType != FIB_MAGIC2 &&
937             hw_fib->header.StructType != FIB_MAGIC2_64) {
938                 if (dev->comm_interface == AAC_COMM_MESSAGE)
939                         kfree(hw_fib);
940                 return -EINVAL;
941         }
942         /*
943          *      This block handles the case where the adapter had sent us a
944          *      command and we have finished processing the command. We
945          *      call completeFib when we are done processing the command
946          *      and want to send a response back to the adapter. This will
947          *      send the completed cdb to the adapter.
948          */
949         if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
950                 if (dev->comm_interface == AAC_COMM_MESSAGE) {
951                         kfree (hw_fib);
952                 } else {
953                         u32 index;
954                         hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
955                         if (size) {
956                                 size += sizeof(struct aac_fibhdr);
957                                 if (size > le16_to_cpu(hw_fib->header.SenderSize))
958                                         return -EMSGSIZE;
959                                 hw_fib->header.Size = cpu_to_le16(size);
960                         }
961                         q = &dev->queues->queue[AdapNormRespQueue];
962                         spin_lock_irqsave(q->lock, qflags);
963                         aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
964                         *(q->headers.producer) = cpu_to_le32(index + 1);
965                         spin_unlock_irqrestore(q->lock, qflags);
966                         if (!(nointr & (int)aac_config.irq_mod))
967                                 aac_adapter_notify(dev, AdapNormRespQueue);
968                 }
969         } else {
970                 printk(KERN_WARNING "aac_fib_adapter_complete: "
971                         "Unknown xferstate detected.\n");
972                 BUG();
973         }
974         return 0;
975 }
976
977 /**
978  *      aac_fib_complete        -       fib completion handler
979  *      @fib: FIB to complete
980  *
981  *      Will do all necessary work to complete a FIB.
982  */
983
984 int aac_fib_complete(struct fib *fibptr)
985 {
986         struct hw_fib * hw_fib = fibptr->hw_fib_va;
987
988         if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
989                 fib_dealloc(fibptr);
990                 return 0;
991         }
992
993         /*
994          *      Check for a fib which has already been completed or with a
995          *      status wait timeout
996          */
997
998         if (hw_fib->header.XferState == 0 || fibptr->done == 2)
999                 return 0;
1000         /*
1001          *      If we plan to do anything check the structure type first.
1002          */
1003
1004         if (hw_fib->header.StructType != FIB_MAGIC &&
1005             hw_fib->header.StructType != FIB_MAGIC2 &&
1006             hw_fib->header.StructType != FIB_MAGIC2_64)
1007                 return -EINVAL;
1008         /*
1009          *      This block completes a cdb which orginated on the host and we
1010          *      just need to deallocate the cdb or reinit it. At this point the
1011          *      command is complete that we had sent to the adapter and this
1012          *      cdb could be reused.
1013          */
1014
1015         if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
1016                 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
1017         {
1018                 fib_dealloc(fibptr);
1019         }
1020         else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
1021         {
1022                 /*
1023                  *      This handles the case when the host has aborted the I/O
1024                  *      to the adapter because the adapter is not responding
1025                  */
1026                 fib_dealloc(fibptr);
1027         } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
1028                 fib_dealloc(fibptr);
1029         } else {
1030                 BUG();
1031         }
1032         return 0;
1033 }
1034
1035 /**
1036  *      aac_printf      -       handle printf from firmware
1037  *      @dev: Adapter
1038  *      @val: Message info
1039  *
1040  *      Print a message passed to us by the controller firmware on the
1041  *      Adaptec board
1042  */
1043
1044 void aac_printf(struct aac_dev *dev, u32 val)
1045 {
1046         char *cp = dev->printfbuf;
1047         if (dev->printf_enabled)
1048         {
1049                 int length = val & 0xffff;
1050                 int level = (val >> 16) & 0xffff;
1051
1052                 /*
1053                  *      The size of the printfbuf is set in port.c
1054                  *      There is no variable or define for it
1055                  */
1056                 if (length > 255)
1057                         length = 255;
1058                 if (cp[length] != 0)
1059                         cp[length] = 0;
1060                 if (level == LOG_AAC_HIGH_ERROR)
1061                         printk(KERN_WARNING "%s:%s", dev->name, cp);
1062                 else
1063                         printk(KERN_INFO "%s:%s", dev->name, cp);
1064         }
1065         memset(cp, 0, 256);
1066 }
1067
1068 static inline int aac_aif_data(struct aac_aifcmd *aifcmd, uint32_t index)
1069 {
1070         return le32_to_cpu(((__le32 *)aifcmd->data)[index]);
1071 }
1072
1073
1074 static void aac_handle_aif_bu(struct aac_dev *dev, struct aac_aifcmd *aifcmd)
1075 {
1076         switch (aac_aif_data(aifcmd, 1)) {
1077         case AifBuCacheDataLoss:
1078                 if (aac_aif_data(aifcmd, 2))
1079                         dev_info(&dev->pdev->dev, "Backup unit had cache data loss - [%d]\n",
1080                         aac_aif_data(aifcmd, 2));
1081                 else
1082                         dev_info(&dev->pdev->dev, "Backup Unit had cache data loss\n");
1083                 break;
1084         case AifBuCacheDataRecover:
1085                 if (aac_aif_data(aifcmd, 2))
1086                         dev_info(&dev->pdev->dev, "DDR cache data recovered successfully - [%d]\n",
1087                         aac_aif_data(aifcmd, 2));
1088                 else
1089                         dev_info(&dev->pdev->dev, "DDR cache data recovered successfully\n");
1090                 break;
1091         }
1092 }
1093
1094 /**
1095  *      aac_handle_aif          -       Handle a message from the firmware
1096  *      @dev: Which adapter this fib is from
1097  *      @fibptr: Pointer to fibptr from adapter
1098  *
1099  *      This routine handles a driver notify fib from the adapter and
1100  *      dispatches it to the appropriate routine for handling.
1101  */
1102
1103 #define AIF_SNIFF_TIMEOUT       (500*HZ)
1104 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
1105 {
1106         struct hw_fib * hw_fib = fibptr->hw_fib_va;
1107         struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
1108         u32 channel, id, lun, container;
1109         struct scsi_device *device;
1110         enum {
1111                 NOTHING,
1112                 DELETE,
1113                 ADD,
1114                 CHANGE
1115         } device_config_needed = NOTHING;
1116
1117         /* Sniff for container changes */
1118
1119         if (!dev || !dev->fsa_dev)
1120                 return;
1121         container = channel = id = lun = (u32)-1;
1122
1123         /*
1124          *      We have set this up to try and minimize the number of
1125          * re-configures that take place. As a result of this when
1126          * certain AIF's come in we will set a flag waiting for another
1127          * type of AIF before setting the re-config flag.
1128          */
1129         switch (le32_to_cpu(aifcmd->command)) {
1130         case AifCmdDriverNotify:
1131                 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1132                 case AifRawDeviceRemove:
1133                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1134                         if ((container >> 28)) {
1135                                 container = (u32)-1;
1136                                 break;
1137                         }
1138                         channel = (container >> 24) & 0xF;
1139                         if (channel >= dev->maximum_num_channels) {
1140                                 container = (u32)-1;
1141                                 break;
1142                         }
1143                         id = container & 0xFFFF;
1144                         if (id >= dev->maximum_num_physicals) {
1145                                 container = (u32)-1;
1146                                 break;
1147                         }
1148                         lun = (container >> 16) & 0xFF;
1149                         container = (u32)-1;
1150                         channel = aac_phys_to_logical(channel);
1151                         device_config_needed = DELETE;
1152                         break;
1153
1154                 /*
1155                  *      Morph or Expand complete
1156                  */
1157                 case AifDenMorphComplete:
1158                 case AifDenVolumeExtendComplete:
1159                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1160                         if (container >= dev->maximum_num_containers)
1161                                 break;
1162
1163                         /*
1164                          *      Find the scsi_device associated with the SCSI
1165                          * address. Make sure we have the right array, and if
1166                          * so set the flag to initiate a new re-config once we
1167                          * see an AifEnConfigChange AIF come through.
1168                          */
1169
1170                         if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
1171                                 device = scsi_device_lookup(dev->scsi_host_ptr,
1172                                         CONTAINER_TO_CHANNEL(container),
1173                                         CONTAINER_TO_ID(container),
1174                                         CONTAINER_TO_LUN(container));
1175                                 if (device) {
1176                                         dev->fsa_dev[container].config_needed = CHANGE;
1177                                         dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
1178                                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
1179                                         scsi_device_put(device);
1180                                 }
1181                         }
1182                 }
1183
1184                 /*
1185                  *      If we are waiting on something and this happens to be
1186                  * that thing then set the re-configure flag.
1187                  */
1188                 if (container != (u32)-1) {
1189                         if (container >= dev->maximum_num_containers)
1190                                 break;
1191                         if ((dev->fsa_dev[container].config_waiting_on ==
1192                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1193                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1194                                 dev->fsa_dev[container].config_waiting_on = 0;
1195                 } else for (container = 0;
1196                     container < dev->maximum_num_containers; ++container) {
1197                         if ((dev->fsa_dev[container].config_waiting_on ==
1198                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1199                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1200                                 dev->fsa_dev[container].config_waiting_on = 0;
1201                 }
1202                 break;
1203
1204         case AifCmdEventNotify:
1205                 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1206                 case AifEnBatteryEvent:
1207                         dev->cache_protected =
1208                                 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
1209                         break;
1210                 /*
1211                  *      Add an Array.
1212                  */
1213                 case AifEnAddContainer:
1214                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1215                         if (container >= dev->maximum_num_containers)
1216                                 break;
1217                         dev->fsa_dev[container].config_needed = ADD;
1218                         dev->fsa_dev[container].config_waiting_on =
1219                                 AifEnConfigChange;
1220                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
1221                         break;
1222
1223                 /*
1224                  *      Delete an Array.
1225                  */
1226                 case AifEnDeleteContainer:
1227                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1228                         if (container >= dev->maximum_num_containers)
1229                                 break;
1230                         dev->fsa_dev[container].config_needed = DELETE;
1231                         dev->fsa_dev[container].config_waiting_on =
1232                                 AifEnConfigChange;
1233                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
1234                         break;
1235
1236                 /*
1237                  *      Container change detected. If we currently are not
1238                  * waiting on something else, setup to wait on a Config Change.
1239                  */
1240                 case AifEnContainerChange:
1241                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1242                         if (container >= dev->maximum_num_containers)
1243                                 break;
1244                         if (dev->fsa_dev[container].config_waiting_on &&
1245                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1246                                 break;
1247                         dev->fsa_dev[container].config_needed = CHANGE;
1248                         dev->fsa_dev[container].config_waiting_on =
1249                                 AifEnConfigChange;
1250                         dev->fsa_dev[container].config_waiting_stamp = jiffies;
1251                         break;
1252
1253                 case AifEnConfigChange:
1254                         break;
1255
1256                 case AifEnAddJBOD:
1257                 case AifEnDeleteJBOD:
1258                         container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1259                         if ((container >> 28)) {
1260                                 container = (u32)-1;
1261                                 break;
1262                         }
1263                         channel = (container >> 24) & 0xF;
1264                         if (channel >= dev->maximum_num_channels) {
1265                                 container = (u32)-1;
1266                                 break;
1267                         }
1268                         id = container & 0xFFFF;
1269                         if (id >= dev->maximum_num_physicals) {
1270                                 container = (u32)-1;
1271                                 break;
1272                         }
1273                         lun = (container >> 16) & 0xFF;
1274                         container = (u32)-1;
1275                         channel = aac_phys_to_logical(channel);
1276                         device_config_needed =
1277                           (((__le32 *)aifcmd->data)[0] ==
1278                             cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
1279                         if (device_config_needed == ADD) {
1280                                 device = scsi_device_lookup(dev->scsi_host_ptr,
1281                                         channel,
1282                                         id,
1283                                         lun);
1284                                 if (device) {
1285                                         scsi_remove_device(device);
1286                                         scsi_device_put(device);
1287                                 }
1288                         }
1289                         break;
1290
1291                 case AifEnEnclosureManagement:
1292                         /*
1293                          * If in JBOD mode, automatic exposure of new
1294                          * physical target to be suppressed until configured.
1295                          */
1296                         if (dev->jbod)
1297                                 break;
1298                         switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1299                         case EM_DRIVE_INSERTION:
1300                         case EM_DRIVE_REMOVAL:
1301                         case EM_SES_DRIVE_INSERTION:
1302                         case EM_SES_DRIVE_REMOVAL:
1303                                 container = le32_to_cpu(
1304                                         ((__le32 *)aifcmd->data)[2]);
1305                                 if ((container >> 28)) {
1306                                         container = (u32)-1;
1307                                         break;
1308                                 }
1309                                 channel = (container >> 24) & 0xF;
1310                                 if (channel >= dev->maximum_num_channels) {
1311                                         container = (u32)-1;
1312                                         break;
1313                                 }
1314                                 id = container & 0xFFFF;
1315                                 lun = (container >> 16) & 0xFF;
1316                                 container = (u32)-1;
1317                                 if (id >= dev->maximum_num_physicals) {
1318                                         /* legacy dev_t ? */
1319                                         if ((0x2000 <= id) || lun || channel ||
1320                                           ((channel = (id >> 7) & 0x3F) >=
1321                                           dev->maximum_num_channels))
1322                                                 break;
1323                                         lun = (id >> 4) & 7;
1324                                         id &= 0xF;
1325                                 }
1326                                 channel = aac_phys_to_logical(channel);
1327                                 device_config_needed =
1328                                   ((((__le32 *)aifcmd->data)[3]
1329                                     == cpu_to_le32(EM_DRIVE_INSERTION)) ||
1330                                     (((__le32 *)aifcmd->data)[3]
1331                                     == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
1332                                   ADD : DELETE;
1333                                 break;
1334                         }
1335                         case AifBuManagerEvent:
1336                                 aac_handle_aif_bu(dev, aifcmd);
1337                         break;
1338                 }
1339
1340                 /*
1341                  *      If we are waiting on something and this happens to be
1342                  * that thing then set the re-configure flag.
1343                  */
1344                 if (container != (u32)-1) {
1345                         if (container >= dev->maximum_num_containers)
1346                                 break;
1347                         if ((dev->fsa_dev[container].config_waiting_on ==
1348                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1349                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1350                                 dev->fsa_dev[container].config_waiting_on = 0;
1351                 } else for (container = 0;
1352                     container < dev->maximum_num_containers; ++container) {
1353                         if ((dev->fsa_dev[container].config_waiting_on ==
1354                             le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1355                          time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1356                                 dev->fsa_dev[container].config_waiting_on = 0;
1357                 }
1358                 break;
1359
1360         case AifCmdJobProgress:
1361                 /*
1362                  *      These are job progress AIF's. When a Clear is being
1363                  * done on a container it is initially created then hidden from
1364                  * the OS. When the clear completes we don't get a config
1365                  * change so we monitor the job status complete on a clear then
1366                  * wait for a container change.
1367                  */
1368
1369                 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1370                     (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1371                      ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1372                         for (container = 0;
1373                             container < dev->maximum_num_containers;
1374                             ++container) {
1375                                 /*
1376                                  * Stomp on all config sequencing for all
1377                                  * containers?
1378                                  */
1379                                 dev->fsa_dev[container].config_waiting_on =
1380                                         AifEnContainerChange;
1381                                 dev->fsa_dev[container].config_needed = ADD;
1382                                 dev->fsa_dev[container].config_waiting_stamp =
1383                                         jiffies;
1384                         }
1385                 }
1386                 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1387                     ((__le32 *)aifcmd->data)[6] == 0 &&
1388                     ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1389                         for (container = 0;
1390                             container < dev->maximum_num_containers;
1391                             ++container) {
1392                                 /*
1393                                  * Stomp on all config sequencing for all
1394                                  * containers?
1395                                  */
1396                                 dev->fsa_dev[container].config_waiting_on =
1397                                         AifEnContainerChange;
1398                                 dev->fsa_dev[container].config_needed = DELETE;
1399                                 dev->fsa_dev[container].config_waiting_stamp =
1400                                         jiffies;
1401                         }
1402                 }
1403                 break;
1404         }
1405
1406         container = 0;
1407 retry_next:
1408         if (device_config_needed == NOTHING)
1409         for (; container < dev->maximum_num_containers; ++container) {
1410                 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1411                         (dev->fsa_dev[container].config_needed != NOTHING) &&
1412                         time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1413                         device_config_needed =
1414                                 dev->fsa_dev[container].config_needed;
1415                         dev->fsa_dev[container].config_needed = NOTHING;
1416                         channel = CONTAINER_TO_CHANNEL(container);
1417                         id = CONTAINER_TO_ID(container);
1418                         lun = CONTAINER_TO_LUN(container);
1419                         break;
1420                 }
1421         }
1422         if (device_config_needed == NOTHING)
1423                 return;
1424
1425         /*
1426          *      If we decided that a re-configuration needs to be done,
1427          * schedule it here on the way out the door, please close the door
1428          * behind you.
1429          */
1430
1431         /*
1432          *      Find the scsi_device associated with the SCSI address,
1433          * and mark it as changed, invalidating the cache. This deals
1434          * with changes to existing device IDs.
1435          */
1436
1437         if (!dev || !dev->scsi_host_ptr)
1438                 return;
1439         /*
1440          * force reload of disk info via aac_probe_container
1441          */
1442         if ((channel == CONTAINER_CHANNEL) &&
1443           (device_config_needed != NOTHING)) {
1444                 if (dev->fsa_dev[container].valid == 1)
1445                         dev->fsa_dev[container].valid = 2;
1446                 aac_probe_container(dev, container);
1447         }
1448         device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1449         if (device) {
1450                 switch (device_config_needed) {
1451                 case DELETE:
1452 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1453                         scsi_remove_device(device);
1454 #else
1455                         if (scsi_device_online(device)) {
1456                                 scsi_device_set_state(device, SDEV_OFFLINE);
1457                                 sdev_printk(KERN_INFO, device,
1458                                         "Device offlined - %s\n",
1459                                         (channel == CONTAINER_CHANNEL) ?
1460                                                 "array deleted" :
1461                                                 "enclosure services event");
1462                         }
1463 #endif
1464                         break;
1465                 case ADD:
1466                         if (!scsi_device_online(device)) {
1467                                 sdev_printk(KERN_INFO, device,
1468                                         "Device online - %s\n",
1469                                         (channel == CONTAINER_CHANNEL) ?
1470                                                 "array created" :
1471                                                 "enclosure services event");
1472                                 scsi_device_set_state(device, SDEV_RUNNING);
1473                         }
1474                         /* FALLTHRU */
1475                 case CHANGE:
1476                         if ((channel == CONTAINER_CHANNEL)
1477                          && (!dev->fsa_dev[container].valid)) {
1478 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1479                                 scsi_remove_device(device);
1480 #else
1481                                 if (!scsi_device_online(device))
1482                                         break;
1483                                 scsi_device_set_state(device, SDEV_OFFLINE);
1484                                 sdev_printk(KERN_INFO, device,
1485                                         "Device offlined - %s\n",
1486                                         "array failed");
1487 #endif
1488                                 break;
1489                         }
1490                         scsi_rescan_device(&device->sdev_gendev);
1491
1492                 default:
1493                         break;
1494                 }
1495                 scsi_device_put(device);
1496                 device_config_needed = NOTHING;
1497         }
1498         if (device_config_needed == ADD)
1499                 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1500         if (channel == CONTAINER_CHANNEL) {
1501                 container++;
1502                 device_config_needed = NOTHING;
1503                 goto retry_next;
1504         }
1505 }
1506
1507 static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1508 {
1509         int index, quirks;
1510         int retval;
1511         struct Scsi_Host *host;
1512         struct scsi_device *dev;
1513         struct scsi_cmnd *command;
1514         struct scsi_cmnd *command_list;
1515         int jafo = 0;
1516         int bled;
1517         u64 dmamask;
1518         int num_of_fibs = 0;
1519
1520         /*
1521          * Assumptions:
1522          *      - host is locked, unless called by the aacraid thread.
1523          *        (a matter of convenience, due to legacy issues surrounding
1524          *        eh_host_adapter_reset).
1525          *      - in_reset is asserted, so no new i/o is getting to the
1526          *        card.
1527          *      - The card is dead, or will be very shortly ;-/ so no new
1528          *        commands are completing in the interrupt service.
1529          */
1530         host = aac->scsi_host_ptr;
1531         scsi_block_requests(host);
1532         aac_adapter_disable_int(aac);
1533         if (aac->thread->pid != current->pid) {
1534                 spin_unlock_irq(host->host_lock);
1535                 kthread_stop(aac->thread);
1536                 jafo = 1;
1537         }
1538
1539         /*
1540          *      If a positive health, means in a known DEAD PANIC
1541          * state and the adapter could be reset to `try again'.
1542          */
1543         bled = forced ? 0 : aac_adapter_check_health(aac);
1544         retval = aac_adapter_restart(aac, bled, reset_type);
1545
1546         if (retval)
1547                 goto out;
1548
1549         /*
1550          *      Loop through the fibs, close the synchronous FIBS
1551          */
1552         retval = 1;
1553         num_of_fibs = aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
1554         for (index = 0; index <  num_of_fibs; index++) {
1555
1556                 struct fib *fib = &aac->fibs[index];
1557                 __le32 XferState = fib->hw_fib_va->header.XferState;
1558                 bool is_response_expected = false;
1559
1560                 if (!(XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1561                    (XferState & cpu_to_le32(ResponseExpected)))
1562                         is_response_expected = true;
1563
1564                 if (is_response_expected
1565                   || fib->flags & FIB_CONTEXT_FLAG_WAIT) {
1566                         unsigned long flagv;
1567                         spin_lock_irqsave(&fib->event_lock, flagv);
1568                         up(&fib->event_wait);
1569                         spin_unlock_irqrestore(&fib->event_lock, flagv);
1570                         schedule();
1571                         retval = 0;
1572                 }
1573         }
1574         /* Give some extra time for ioctls to complete. */
1575         if (retval == 0)
1576                 ssleep(2);
1577         index = aac->cardtype;
1578
1579         /*
1580          * Re-initialize the adapter, first free resources, then carefully
1581          * apply the initialization sequence to come back again. Only risk
1582          * is a change in Firmware dropping cache, it is assumed the caller
1583          * will ensure that i/o is queisced and the card is flushed in that
1584          * case.
1585          */
1586         aac_fib_map_free(aac);
1587         dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr,
1588                           aac->comm_phys);
1589         aac->comm_addr = NULL;
1590         aac->comm_phys = 0;
1591         kfree(aac->queues);
1592         aac->queues = NULL;
1593         aac_free_irq(aac);
1594         kfree(aac->fsa_dev);
1595         aac->fsa_dev = NULL;
1596
1597         dmamask = DMA_BIT_MASK(32);
1598         quirks = aac_get_driver_ident(index)->quirks;
1599         if (quirks & AAC_QUIRK_31BIT)
1600                 retval = pci_set_dma_mask(aac->pdev, dmamask);
1601         else if (!(quirks & AAC_QUIRK_SRC))
1602                 retval = pci_set_dma_mask(aac->pdev, dmamask);
1603         else
1604                 retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
1605
1606         if (quirks & AAC_QUIRK_31BIT && !retval) {
1607                 dmamask = DMA_BIT_MASK(31);
1608                 retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
1609         }
1610
1611         if (retval)
1612                 goto out;
1613
1614         if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1615                 goto out;
1616
1617         if (jafo) {
1618                 aac->thread = kthread_run(aac_command_thread, aac, "%s",
1619                                           aac->name);
1620                 if (IS_ERR(aac->thread)) {
1621                         retval = PTR_ERR(aac->thread);
1622                         goto out;
1623                 }
1624         }
1625         (void)aac_get_adapter_info(aac);
1626         if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1627                 host->sg_tablesize = 34;
1628                 host->max_sectors = (host->sg_tablesize * 8) + 112;
1629         }
1630         if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1631                 host->sg_tablesize = 17;
1632                 host->max_sectors = (host->sg_tablesize * 8) + 112;
1633         }
1634         aac_get_config_status(aac, 1);
1635         aac_get_containers(aac);
1636         /*
1637          * This is where the assumption that the Adapter is quiesced
1638          * is important.
1639          */
1640         command_list = NULL;
1641         __shost_for_each_device(dev, host) {
1642                 unsigned long flags;
1643                 spin_lock_irqsave(&dev->list_lock, flags);
1644                 list_for_each_entry(command, &dev->cmd_list, list)
1645                         if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1646                                 command->SCp.buffer = (struct scatterlist *)command_list;
1647                                 command_list = command;
1648                         }
1649                 spin_unlock_irqrestore(&dev->list_lock, flags);
1650         }
1651         while ((command = command_list)) {
1652                 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1653                 command->SCp.buffer = NULL;
1654                 command->result = DID_OK << 16
1655                   | COMMAND_COMPLETE << 8
1656                   | SAM_STAT_TASK_SET_FULL;
1657                 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1658                 command->scsi_done(command);
1659         }
1660         /*
1661          * Any Device that was already marked offline needs to be cleaned up
1662          */
1663         __shost_for_each_device(dev, host) {
1664                 if (!scsi_device_online(dev)) {
1665                         sdev_printk(KERN_INFO, dev, "Removing offline device\n");
1666                         scsi_remove_device(dev);
1667                         scsi_device_put(dev);
1668                 }
1669         }
1670         retval = 0;
1671
1672 out:
1673         aac->in_reset = 0;
1674         scsi_unblock_requests(host);
1675         /*
1676          * Issue bus rescan to catch any configuration that might have
1677          * occurred
1678          */
1679         if (!retval) {
1680                 dev_info(&aac->pdev->dev, "Issuing bus rescan\n");
1681                 scsi_scan_host(host);
1682         }
1683         if (jafo) {
1684                 spin_lock_irq(host->host_lock);
1685         }
1686         return retval;
1687 }
1688
1689 int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1690 {
1691         unsigned long flagv = 0;
1692         int retval;
1693         struct Scsi_Host * host;
1694         int bled;
1695
1696         if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1697                 return -EBUSY;
1698
1699         if (aac->in_reset) {
1700                 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1701                 return -EBUSY;
1702         }
1703         aac->in_reset = 1;
1704         spin_unlock_irqrestore(&aac->fib_lock, flagv);
1705
1706         /*
1707          * Wait for all commands to complete to this specific
1708          * target (block maximum 60 seconds). Although not necessary,
1709          * it does make us a good storage citizen.
1710          */
1711         host = aac->scsi_host_ptr;
1712         scsi_block_requests(host);
1713         if (forced < 2) for (retval = 60; retval; --retval) {
1714                 struct scsi_device * dev;
1715                 struct scsi_cmnd * command;
1716                 int active = 0;
1717
1718                 __shost_for_each_device(dev, host) {
1719                         spin_lock_irqsave(&dev->list_lock, flagv);
1720                         list_for_each_entry(command, &dev->cmd_list, list) {
1721                                 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1722                                         active++;
1723                                         break;
1724                                 }
1725                         }
1726                         spin_unlock_irqrestore(&dev->list_lock, flagv);
1727                         if (active)
1728                                 break;
1729
1730                 }
1731                 /*
1732                  * We can exit If all the commands are complete
1733                  */
1734                 if (active == 0)
1735                         break;
1736                 ssleep(1);
1737         }
1738
1739         /* Quiesce build, flush cache, write through mode */
1740         if (forced < 2)
1741                 aac_send_shutdown(aac);
1742         spin_lock_irqsave(host->host_lock, flagv);
1743         bled = forced ? forced :
1744                         (aac_check_reset != 0 && aac_check_reset != 1);
1745         retval = _aac_reset_adapter(aac, bled, reset_type);
1746         spin_unlock_irqrestore(host->host_lock, flagv);
1747
1748         if ((forced < 2) && (retval == -ENODEV)) {
1749                 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1750                 struct fib * fibctx = aac_fib_alloc(aac);
1751                 if (fibctx) {
1752                         struct aac_pause *cmd;
1753                         int status;
1754
1755                         aac_fib_init(fibctx);
1756
1757                         cmd = (struct aac_pause *) fib_data(fibctx);
1758
1759                         cmd->command = cpu_to_le32(VM_ContainerConfig);
1760                         cmd->type = cpu_to_le32(CT_PAUSE_IO);
1761                         cmd->timeout = cpu_to_le32(1);
1762                         cmd->min = cpu_to_le32(1);
1763                         cmd->noRescan = cpu_to_le32(1);
1764                         cmd->count = cpu_to_le32(0);
1765
1766                         status = aac_fib_send(ContainerCommand,
1767                           fibctx,
1768                           sizeof(struct aac_pause),
1769                           FsaNormal,
1770                           -2 /* Timeout silently */, 1,
1771                           NULL, NULL);
1772
1773                         if (status >= 0)
1774                                 aac_fib_complete(fibctx);
1775                         /* FIB should be freed only after getting
1776                          * the response from the F/W */
1777                         if (status != -ERESTARTSYS)
1778                                 aac_fib_free(fibctx);
1779                 }
1780         }
1781
1782         return retval;
1783 }
1784
1785 int aac_check_health(struct aac_dev * aac)
1786 {
1787         int BlinkLED;
1788         unsigned long time_now, flagv = 0;
1789         struct list_head * entry;
1790
1791         /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1792         if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1793                 return 0;
1794
1795         if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1796                 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1797                 return 0; /* OK */
1798         }
1799
1800         aac->in_reset = 1;
1801
1802         /* Fake up an AIF:
1803          *      aac_aifcmd.command = AifCmdEventNotify = 1
1804          *      aac_aifcmd.seqnum = 0xFFFFFFFF
1805          *      aac_aifcmd.data[0] = AifEnExpEvent = 23
1806          *      aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1807          *      aac.aifcmd.data[2] = AifHighPriority = 3
1808          *      aac.aifcmd.data[3] = BlinkLED
1809          */
1810
1811         time_now = jiffies/HZ;
1812         entry = aac->fib_list.next;
1813
1814         /*
1815          * For each Context that is on the
1816          * fibctxList, make a copy of the
1817          * fib, and then set the event to wake up the
1818          * thread that is waiting for it.
1819          */
1820         while (entry != &aac->fib_list) {
1821                 /*
1822                  * Extract the fibctx
1823                  */
1824                 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1825                 struct hw_fib * hw_fib;
1826                 struct fib * fib;
1827                 /*
1828                  * Check if the queue is getting
1829                  * backlogged
1830                  */
1831                 if (fibctx->count > 20) {
1832                         /*
1833                          * It's *not* jiffies folks,
1834                          * but jiffies / HZ, so do not
1835                          * panic ...
1836                          */
1837                         u32 time_last = fibctx->jiffies;
1838                         /*
1839                          * Has it been > 2 minutes
1840                          * since the last read off
1841                          * the queue?
1842                          */
1843                         if ((time_now - time_last) > aif_timeout) {
1844                                 entry = entry->next;
1845                                 aac_close_fib_context(aac, fibctx);
1846                                 continue;
1847                         }
1848                 }
1849                 /*
1850                  * Warning: no sleep allowed while
1851                  * holding spinlock
1852                  */
1853                 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1854                 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1855                 if (fib && hw_fib) {
1856                         struct aac_aifcmd * aif;
1857
1858                         fib->hw_fib_va = hw_fib;
1859                         fib->dev = aac;
1860                         aac_fib_init(fib);
1861                         fib->type = FSAFS_NTC_FIB_CONTEXT;
1862                         fib->size = sizeof (struct fib);
1863                         fib->data = hw_fib->data;
1864                         aif = (struct aac_aifcmd *)hw_fib->data;
1865                         aif->command = cpu_to_le32(AifCmdEventNotify);
1866                         aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1867                         ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1868                         ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1869                         ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1870                         ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1871
1872                         /*
1873                          * Put the FIB onto the
1874                          * fibctx's fibs
1875                          */
1876                         list_add_tail(&fib->fiblink, &fibctx->fib_list);
1877                         fibctx->count++;
1878                         /*
1879                          * Set the event to wake up the
1880                          * thread that will waiting.
1881                          */
1882                         up(&fibctx->wait_sem);
1883                 } else {
1884                         printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1885                         kfree(fib);
1886                         kfree(hw_fib);
1887                 }
1888                 entry = entry->next;
1889         }
1890
1891         spin_unlock_irqrestore(&aac->fib_lock, flagv);
1892
1893         if (BlinkLED < 0) {
1894                 printk(KERN_ERR "%s: Host adapter is dead (or got a PCI error) %d\n",
1895                                 aac->name, BlinkLED);
1896                 goto out;
1897         }
1898
1899         printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1900
1901 out:
1902         aac->in_reset = 0;
1903         return BlinkLED;
1904 }
1905
1906
1907 static void aac_resolve_luns(struct aac_dev *dev)
1908 {
1909         int bus, target, channel;
1910         struct scsi_device *sdev;
1911         u8 devtype;
1912         u8 new_devtype;
1913
1914         for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
1915                 for (target = 0; target < AAC_MAX_TARGETS; target++) {
1916
1917                         if (bus == CONTAINER_CHANNEL)
1918                                 channel = CONTAINER_CHANNEL;
1919                         else
1920                                 channel = aac_phys_to_logical(bus);
1921
1922                         devtype = dev->hba_map[bus][target].devtype;
1923                         new_devtype = dev->hba_map[bus][target].new_devtype;
1924
1925                         sdev = scsi_device_lookup(dev->scsi_host_ptr, channel,
1926                                         target, 0);
1927
1928                         if (!sdev && new_devtype)
1929                                 scsi_add_device(dev->scsi_host_ptr, channel,
1930                                                 target, 0);
1931                         else if (sdev && new_devtype != devtype)
1932                                 scsi_remove_device(sdev);
1933                         else if (sdev && new_devtype == devtype)
1934                                 scsi_rescan_device(&sdev->sdev_gendev);
1935
1936                         if (sdev)
1937                                 scsi_device_put(sdev);
1938
1939                         dev->hba_map[bus][target].devtype = new_devtype;
1940                 }
1941         }
1942 }
1943
1944 /**
1945  *      aac_handle_sa_aif       Handle a message from the firmware
1946  *      @dev: Which adapter this fib is from
1947  *      @fibptr: Pointer to fibptr from adapter
1948  *
1949  *      This routine handles a driver notify fib from the adapter and
1950  *      dispatches it to the appropriate routine for handling.
1951  */
1952 static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr)
1953 {
1954         int i, bus, target, container, rcode = 0;
1955         u32 events = 0;
1956         struct fib *fib;
1957         struct scsi_device *sdev;
1958
1959         if (fibptr->hbacmd_size & SA_AIF_HOTPLUG)
1960                 events = SA_AIF_HOTPLUG;
1961         else if (fibptr->hbacmd_size & SA_AIF_HARDWARE)
1962                 events = SA_AIF_HARDWARE;
1963         else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE)
1964                 events = SA_AIF_PDEV_CHANGE;
1965         else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE)
1966                 events = SA_AIF_LDEV_CHANGE;
1967         else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE)
1968                 events = SA_AIF_BPSTAT_CHANGE;
1969         else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE)
1970                 events = SA_AIF_BPCFG_CHANGE;
1971
1972         switch (events) {
1973         case SA_AIF_HOTPLUG:
1974         case SA_AIF_HARDWARE:
1975         case SA_AIF_PDEV_CHANGE:
1976         case SA_AIF_LDEV_CHANGE:
1977         case SA_AIF_BPCFG_CHANGE:
1978
1979                 fib = aac_fib_alloc(dev);
1980                 if (!fib) {
1981                         pr_err("aac_handle_sa_aif: out of memory\n");
1982                         return;
1983                 }
1984                 for (bus = 0; bus < AAC_MAX_BUSES; bus++)
1985                         for (target = 0; target < AAC_MAX_TARGETS; target++)
1986                                 dev->hba_map[bus][target].new_devtype = 0;
1987
1988                 rcode = aac_report_phys_luns(dev, fib, AAC_RESCAN);
1989
1990                 if (rcode != -ERESTARTSYS)
1991                         aac_fib_free(fib);
1992
1993                 aac_resolve_luns(dev);
1994
1995                 if (events == SA_AIF_LDEV_CHANGE ||
1996                     events == SA_AIF_BPCFG_CHANGE) {
1997                         aac_get_containers(dev);
1998                         for (container = 0; container <
1999                         dev->maximum_num_containers; ++container) {
2000                                 sdev = scsi_device_lookup(dev->scsi_host_ptr,
2001                                                 CONTAINER_CHANNEL,
2002                                                 container, 0);
2003                                 if (dev->fsa_dev[container].valid && !sdev) {
2004                                         scsi_add_device(dev->scsi_host_ptr,
2005                                                 CONTAINER_CHANNEL,
2006                                                 container, 0);
2007                                 } else if (!dev->fsa_dev[container].valid &&
2008                                         sdev) {
2009                                         scsi_remove_device(sdev);
2010                                         scsi_device_put(sdev);
2011                                 } else if (sdev) {
2012                                         scsi_rescan_device(&sdev->sdev_gendev);
2013                                         scsi_device_put(sdev);
2014                                 }
2015                         }
2016                 }
2017                 break;
2018
2019         case SA_AIF_BPSTAT_CHANGE:
2020                 /* currently do nothing */
2021                 break;
2022         }
2023
2024         for (i = 1; i <= 10; ++i) {
2025                 events = src_readl(dev, MUnit.IDR);
2026                 if (events & (1<<23)) {
2027                         pr_warn(" AIF not cleared by firmware - %d/%d)\n",
2028                                 i, 10);
2029                         ssleep(1);
2030                 }
2031         }
2032 }
2033
2034 static int get_fib_count(struct aac_dev *dev)
2035 {
2036         unsigned int num = 0;
2037         struct list_head *entry;
2038         unsigned long flagv;
2039
2040         /*
2041          * Warning: no sleep allowed while
2042          * holding spinlock. We take the estimate
2043          * and pre-allocate a set of fibs outside the
2044          * lock.
2045          */
2046         num = le32_to_cpu(dev->init->r7.adapter_fibs_size)
2047                         / sizeof(struct hw_fib); /* some extra */
2048         spin_lock_irqsave(&dev->fib_lock, flagv);
2049         entry = dev->fib_list.next;
2050         while (entry != &dev->fib_list) {
2051                 entry = entry->next;
2052                 ++num;
2053         }
2054         spin_unlock_irqrestore(&dev->fib_lock, flagv);
2055
2056         return num;
2057 }
2058
2059 static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool,
2060                                                 struct fib **fib_pool,
2061                                                 unsigned int num)
2062 {
2063         struct hw_fib **hw_fib_p;
2064         struct fib **fib_p;
2065
2066         hw_fib_p = hw_fib_pool;
2067         fib_p = fib_pool;
2068         while (hw_fib_p < &hw_fib_pool[num]) {
2069                 *(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL);
2070                 if (!(*(hw_fib_p++))) {
2071                         --hw_fib_p;
2072                         break;
2073                 }
2074
2075                 *(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL);
2076                 if (!(*(fib_p++))) {
2077                         kfree(*(--hw_fib_p));
2078                         break;
2079                 }
2080         }
2081
2082         /*
2083          * Get the actual number of allocated fibs
2084          */
2085         num = hw_fib_p - hw_fib_pool;
2086         return num;
2087 }
2088
2089 static void wakeup_fibctx_threads(struct aac_dev *dev,
2090                                                 struct hw_fib **hw_fib_pool,
2091                                                 struct fib **fib_pool,
2092                                                 struct fib *fib,
2093                                                 struct hw_fib *hw_fib,
2094                                                 unsigned int num)
2095 {
2096         unsigned long flagv;
2097         struct list_head *entry;
2098         struct hw_fib **hw_fib_p;
2099         struct fib **fib_p;
2100         u32 time_now, time_last;
2101         struct hw_fib *hw_newfib;
2102         struct fib *newfib;
2103         struct aac_fib_context *fibctx;
2104
2105         time_now = jiffies/HZ;
2106         spin_lock_irqsave(&dev->fib_lock, flagv);
2107         entry = dev->fib_list.next;
2108         /*
2109          * For each Context that is on the
2110          * fibctxList, make a copy of the
2111          * fib, and then set the event to wake up the
2112          * thread that is waiting for it.
2113          */
2114
2115         hw_fib_p = hw_fib_pool;
2116         fib_p = fib_pool;
2117         while (entry != &dev->fib_list) {
2118                 /*
2119                  * Extract the fibctx
2120                  */
2121                 fibctx = list_entry(entry, struct aac_fib_context,
2122                                 next);
2123                 /*
2124                  * Check if the queue is getting
2125                  * backlogged
2126                  */
2127                 if (fibctx->count > 20) {
2128                         /*
2129                          * It's *not* jiffies folks,
2130                          * but jiffies / HZ so do not
2131                          * panic ...
2132                          */
2133                         time_last = fibctx->jiffies;
2134                         /*
2135                          * Has it been > 2 minutes
2136                          * since the last read off
2137                          * the queue?
2138                          */
2139                         if ((time_now - time_last) > aif_timeout) {
2140                                 entry = entry->next;
2141                                 aac_close_fib_context(dev, fibctx);
2142                                 continue;
2143                         }
2144                 }
2145                 /*
2146                  * Warning: no sleep allowed while
2147                  * holding spinlock
2148                  */
2149                 if (hw_fib_p >= &hw_fib_pool[num]) {
2150                         pr_warn("aifd: didn't allocate NewFib\n");
2151                         entry = entry->next;
2152                         continue;
2153                 }
2154
2155                 hw_newfib = *hw_fib_p;
2156                 *(hw_fib_p++) = NULL;
2157                 newfib = *fib_p;
2158                 *(fib_p++) = NULL;
2159                 /*
2160                  * Make the copy of the FIB
2161                  */
2162                 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
2163                 memcpy(newfib, fib, sizeof(struct fib));
2164                 newfib->hw_fib_va = hw_newfib;
2165                 /*
2166                  * Put the FIB onto the
2167                  * fibctx's fibs
2168                  */
2169                 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
2170                 fibctx->count++;
2171                 /*
2172                  * Set the event to wake up the
2173                  * thread that is waiting.
2174                  */
2175                 up(&fibctx->wait_sem);
2176
2177                 entry = entry->next;
2178         }
2179         /*
2180          *      Set the status of this FIB
2181          */
2182         *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2183         aac_fib_adapter_complete(fib, sizeof(u32));
2184         spin_unlock_irqrestore(&dev->fib_lock, flagv);
2185
2186 }
2187
2188 static void aac_process_events(struct aac_dev *dev)
2189 {
2190         struct hw_fib *hw_fib;
2191         struct fib *fib;
2192         unsigned long flags;
2193         spinlock_t *t_lock;
2194
2195         t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2196         spin_lock_irqsave(t_lock, flags);
2197
2198         while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
2199                 struct list_head *entry;
2200                 struct aac_aifcmd *aifcmd;
2201                 unsigned int  num;
2202                 struct hw_fib **hw_fib_pool, **hw_fib_p;
2203                 struct fib **fib_pool, **fib_p;
2204
2205                 set_current_state(TASK_RUNNING);
2206
2207                 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
2208                 list_del(entry);
2209
2210                 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2211                 spin_unlock_irqrestore(t_lock, flags);
2212
2213                 fib = list_entry(entry, struct fib, fiblink);
2214                 hw_fib = fib->hw_fib_va;
2215                 if (dev->sa_firmware) {
2216                         /* Thor AIF */
2217                         aac_handle_sa_aif(dev, fib);
2218                         aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2219                         goto free_fib;
2220                 }
2221                 /*
2222                  *      We will process the FIB here or pass it to a
2223                  *      worker thread that is TBD. We Really can't
2224                  *      do anything at this point since we don't have
2225                  *      anything defined for this thread to do.
2226                  */
2227                 memset(fib, 0, sizeof(struct fib));
2228                 fib->type = FSAFS_NTC_FIB_CONTEXT;
2229                 fib->size = sizeof(struct fib);
2230                 fib->hw_fib_va = hw_fib;
2231                 fib->data = hw_fib->data;
2232                 fib->dev = dev;
2233                 /*
2234                  *      We only handle AifRequest fibs from the adapter.
2235                  */
2236
2237                 aifcmd = (struct aac_aifcmd *) hw_fib->data;
2238                 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
2239                         /* Handle Driver Notify Events */
2240                         aac_handle_aif(dev, fib);
2241                         *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2242                         aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2243                         goto free_fib;
2244                 }
2245                 /*
2246                  * The u32 here is important and intended. We are using
2247                  * 32bit wrapping time to fit the adapter field
2248                  */
2249
2250                 /* Sniff events */
2251                 if (aifcmd->command == cpu_to_le32(AifCmdEventNotify)
2252                  || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) {
2253                         aac_handle_aif(dev, fib);
2254                 }
2255
2256                 /*
2257                  * get number of fibs to process
2258                  */
2259                 num = get_fib_count(dev);
2260                 if (!num)
2261                         goto free_fib;
2262
2263                 hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *),
2264                                                 GFP_KERNEL);
2265                 if (!hw_fib_pool)
2266                         goto free_fib;
2267
2268                 fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL);
2269                 if (!fib_pool)
2270                         goto free_hw_fib_pool;
2271
2272                 /*
2273                  * Fill up fib pointer pools with actual fibs
2274                  * and hw_fibs
2275                  */
2276                 num = fillup_pools(dev, hw_fib_pool, fib_pool, num);
2277                 if (!num)
2278                         goto free_mem;
2279
2280                 /*
2281                  * wakeup the thread that is waiting for
2282                  * the response from fw (ioctl)
2283                  */
2284                 wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool,
2285                                                             fib, hw_fib, num);
2286
2287 free_mem:
2288                 /* Free up the remaining resources */
2289                 hw_fib_p = hw_fib_pool;
2290                 fib_p = fib_pool;
2291                 while (hw_fib_p < &hw_fib_pool[num]) {
2292                         kfree(*hw_fib_p);
2293                         kfree(*fib_p);
2294                         ++fib_p;
2295                         ++hw_fib_p;
2296                 }
2297                 kfree(fib_pool);
2298 free_hw_fib_pool:
2299                 kfree(hw_fib_pool);
2300 free_fib:
2301                 kfree(fib);
2302                 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2303                 spin_lock_irqsave(t_lock, flags);
2304         }
2305         /*
2306          *      There are no more AIF's
2307          */
2308         t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2309         spin_unlock_irqrestore(t_lock, flags);
2310 }
2311
2312 static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str,
2313                                                         u32 datasize)
2314 {
2315         struct aac_srb *srbcmd;
2316         struct sgmap64 *sg64;
2317         dma_addr_t addr;
2318         char *dma_buf;
2319         struct fib *fibptr;
2320         int ret = -ENOMEM;
2321         u32 vbus, vid;
2322
2323         fibptr = aac_fib_alloc(dev);
2324         if (!fibptr)
2325                 goto out;
2326
2327         dma_buf = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
2328                                      GFP_KERNEL);
2329         if (!dma_buf)
2330                 goto fib_free_out;
2331
2332         aac_fib_init(fibptr);
2333
2334         vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
2335         vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
2336
2337         srbcmd = (struct aac_srb *)fib_data(fibptr);
2338
2339         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
2340         srbcmd->channel = cpu_to_le32(vbus);
2341         srbcmd->id = cpu_to_le32(vid);
2342         srbcmd->lun = 0;
2343         srbcmd->flags = cpu_to_le32(SRB_DataOut);
2344         srbcmd->timeout = cpu_to_le32(10);
2345         srbcmd->retry_limit = 0;
2346         srbcmd->cdb_size = cpu_to_le32(12);
2347         srbcmd->count = cpu_to_le32(datasize);
2348
2349         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2350         srbcmd->cdb[0] = BMIC_OUT;
2351         srbcmd->cdb[6] = WRITE_HOST_WELLNESS;
2352         memcpy(dma_buf, (char *)wellness_str, datasize);
2353
2354         sg64 = (struct sgmap64 *)&srbcmd->sg;
2355         sg64->count = cpu_to_le32(1);
2356         sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
2357         sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2358         sg64->sg[0].count = cpu_to_le32(datasize);
2359
2360         ret = aac_fib_send(ScsiPortCommand64, fibptr, sizeof(struct aac_srb),
2361                                 FsaNormal, 1, 1, NULL, NULL);
2362
2363         dma_free_coherent(&dev->pdev->dev, datasize, dma_buf, addr);
2364
2365         /*
2366          * Do not set XferState to zero unless
2367          * receives a response from F/W
2368          */
2369         if (ret >= 0)
2370                 aac_fib_complete(fibptr);
2371
2372         /*
2373          * FIB should be freed only after
2374          * getting the response from the F/W
2375          */
2376         if (ret != -ERESTARTSYS)
2377                 goto fib_free_out;
2378
2379 out:
2380         return ret;
2381 fib_free_out:
2382         aac_fib_free(fibptr);
2383         goto out;
2384 }
2385
2386 int aac_send_safw_hostttime(struct aac_dev *dev, struct timeval *now)
2387 {
2388         struct tm cur_tm;
2389         char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
2390         u32 datasize = sizeof(wellness_str);
2391         unsigned long local_time;
2392         int ret = -ENODEV;
2393
2394         if (!dev->sa_firmware)
2395                 goto out;
2396
2397         local_time = (u32)(now->tv_sec - (sys_tz.tz_minuteswest * 60));
2398         time_to_tm(local_time, 0, &cur_tm);
2399         cur_tm.tm_mon += 1;
2400         cur_tm.tm_year += 1900;
2401         wellness_str[8] = bin2bcd(cur_tm.tm_hour);
2402         wellness_str[9] = bin2bcd(cur_tm.tm_min);
2403         wellness_str[10] = bin2bcd(cur_tm.tm_sec);
2404         wellness_str[12] = bin2bcd(cur_tm.tm_mon);
2405         wellness_str[13] = bin2bcd(cur_tm.tm_mday);
2406         wellness_str[14] = bin2bcd(cur_tm.tm_year / 100);
2407         wellness_str[15] = bin2bcd(cur_tm.tm_year % 100);
2408
2409         ret = aac_send_wellness_command(dev, wellness_str, datasize);
2410
2411 out:
2412         return ret;
2413 }
2414
2415 int aac_send_hosttime(struct aac_dev *dev, struct timeval *now)
2416 {
2417         int ret = -ENOMEM;
2418         struct fib *fibptr;
2419         __le32 *info;
2420
2421         fibptr = aac_fib_alloc(dev);
2422         if (!fibptr)
2423                 goto out;
2424
2425         aac_fib_init(fibptr);
2426         info = (__le32 *)fib_data(fibptr);
2427         *info = cpu_to_le32(now->tv_sec);
2428         ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal,
2429                                         1, 1, NULL, NULL);
2430
2431         /*
2432          * Do not set XferState to zero unless
2433          * receives a response from F/W
2434          */
2435         if (ret >= 0)
2436                 aac_fib_complete(fibptr);
2437
2438         /*
2439          * FIB should be freed only after
2440          * getting the response from the F/W
2441          */
2442         if (ret != -ERESTARTSYS)
2443                 aac_fib_free(fibptr);
2444
2445 out:
2446         return ret;
2447 }
2448
2449 /**
2450  *      aac_command_thread      -       command processing thread
2451  *      @dev: Adapter to monitor
2452  *
2453  *      Waits on the commandready event in it's queue. When the event gets set
2454  *      it will pull FIBs off it's queue. It will continue to pull FIBs off
2455  *      until the queue is empty. When the queue is empty it will wait for
2456  *      more FIBs.
2457  */
2458
2459 int aac_command_thread(void *data)
2460 {
2461         struct aac_dev *dev = data;
2462         DECLARE_WAITQUEUE(wait, current);
2463         unsigned long next_jiffies = jiffies + HZ;
2464         unsigned long next_check_jiffies = next_jiffies;
2465         long difference = HZ;
2466
2467         /*
2468          *      We can only have one thread per adapter for AIF's.
2469          */
2470         if (dev->aif_thread)
2471                 return -EINVAL;
2472
2473         /*
2474          *      Let the DPC know it has a place to send the AIF's to.
2475          */
2476         dev->aif_thread = 1;
2477         add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
2478         set_current_state(TASK_INTERRUPTIBLE);
2479         dprintk ((KERN_INFO "aac_command_thread start\n"));
2480         while (1) {
2481
2482                 aac_process_events(dev);
2483
2484                 /*
2485                  *      Background activity
2486                  */
2487                 if ((time_before(next_check_jiffies,next_jiffies))
2488                  && ((difference = next_check_jiffies - jiffies) <= 0)) {
2489                         next_check_jiffies = next_jiffies;
2490                         if (aac_adapter_check_health(dev) == 0) {
2491                                 difference = ((long)(unsigned)check_interval)
2492                                            * HZ;
2493                                 next_check_jiffies = jiffies + difference;
2494                         } else if (!dev->queues)
2495                                 break;
2496                 }
2497                 if (!time_before(next_check_jiffies,next_jiffies)
2498                  && ((difference = next_jiffies - jiffies) <= 0)) {
2499                         struct timeval now;
2500                         int ret;
2501
2502                         /* Don't even try to talk to adapter if its sick */
2503                         ret = aac_adapter_check_health(dev);
2504                         if (ret || !dev->queues)
2505                                 break;
2506                         next_check_jiffies = jiffies
2507                                            + ((long)(unsigned)check_interval)
2508                                            * HZ;
2509                         do_gettimeofday(&now);
2510
2511                         /* Synchronize our watches */
2512                         if (((1000000 - (1000000 / HZ)) > now.tv_usec)
2513                          && (now.tv_usec > (1000000 / HZ)))
2514                                 difference = (((1000000 - now.tv_usec) * HZ)
2515                                   + 500000) / 1000000;
2516                         else {
2517                                 if (now.tv_usec > 500000)
2518                                         ++now.tv_sec;
2519
2520                                 if (dev->sa_firmware)
2521                                         ret =
2522                                         aac_send_safw_hostttime(dev, &now);
2523                                 else
2524                                         ret = aac_send_hosttime(dev, &now);
2525
2526                                 difference = (long)(unsigned)update_interval*HZ;
2527                         }
2528                         next_jiffies = jiffies + difference;
2529                         if (time_before(next_check_jiffies,next_jiffies))
2530                                 difference = next_check_jiffies - jiffies;
2531                 }
2532                 if (difference <= 0)
2533                         difference = 1;
2534                 set_current_state(TASK_INTERRUPTIBLE);
2535
2536                 if (kthread_should_stop())
2537                         break;
2538
2539                 schedule_timeout(difference);
2540
2541                 if (kthread_should_stop())
2542                         break;
2543         }
2544         if (dev->queues)
2545                 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
2546         dev->aif_thread = 0;
2547         return 0;
2548 }
2549
2550 int aac_acquire_irq(struct aac_dev *dev)
2551 {
2552         int i;
2553         int j;
2554         int ret = 0;
2555
2556         if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) {
2557                 for (i = 0; i < dev->max_msix; i++) {
2558                         dev->aac_msix[i].vector_no = i;
2559                         dev->aac_msix[i].dev = dev;
2560                         if (request_irq(pci_irq_vector(dev->pdev, i),
2561                                         dev->a_ops.adapter_intr,
2562                                         0, "aacraid", &(dev->aac_msix[i]))) {
2563                                 printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n",
2564                                                 dev->name, dev->id, i);
2565                                 for (j = 0 ; j < i ; j++)
2566                                         free_irq(pci_irq_vector(dev->pdev, j),
2567                                                  &(dev->aac_msix[j]));
2568                                 pci_disable_msix(dev->pdev);
2569                                 ret = -1;
2570                         }
2571                 }
2572         } else {
2573                 dev->aac_msix[0].vector_no = 0;
2574                 dev->aac_msix[0].dev = dev;
2575
2576                 if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
2577                         IRQF_SHARED, "aacraid",
2578                         &(dev->aac_msix[0])) < 0) {
2579                         if (dev->msi)
2580                                 pci_disable_msi(dev->pdev);
2581                         printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
2582                                         dev->name, dev->id);
2583                         ret = -1;
2584                 }
2585         }
2586         return ret;
2587 }
2588
2589 void aac_free_irq(struct aac_dev *dev)
2590 {
2591         int i;
2592         int cpu;
2593
2594         cpu = cpumask_first(cpu_online_mask);
2595         if (aac_is_src(dev)) {
2596                 if (dev->max_msix > 1) {
2597                         for (i = 0; i < dev->max_msix; i++)
2598                                 free_irq(pci_irq_vector(dev->pdev, i),
2599                                          &(dev->aac_msix[i]));
2600                 } else {
2601                         free_irq(dev->pdev->irq, &(dev->aac_msix[0]));
2602                 }
2603         } else {
2604                 free_irq(dev->pdev->irq, dev);
2605         }
2606         if (dev->msi)
2607                 pci_disable_msi(dev->pdev);
2608         else if (dev->max_msix > 1)
2609                 pci_disable_msix(dev->pdev);
2610 }