2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc.
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
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)
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)
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.
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.
29 * Abstract: Contain all routines that are required for FSA host/adapter
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>
56 * fib_map_alloc - allocate the fib objects
57 * @dev: Adapter to allocate for
59 * Allocate and map the shared PCI space for the FIB blocks used to
60 * talk to the Adaptec firmware.
63 static int fib_map_alloc(struct aac_dev *dev)
65 if (dev->max_fib_size > AAC_MAX_NATIVE_SIZE)
66 dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
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;
72 dev->max_cmd_size = dev->max_fib_size;
76 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
77 dev->pdev, dev->max_cmd_size, dev->scsi_host_ptr->can_queue,
78 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
79 dev->hw_fib_va = pci_alloc_consistent(dev->pdev,
80 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr))
81 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
83 if (dev->hw_fib_va == NULL)
89 * aac_fib_map_free - free the fib objects
90 * @dev: Adapter to free
92 * Free the PCI mappings and the memory allocated for FIB blocks
96 void aac_fib_map_free(struct aac_dev *dev)
102 if(!dev->hw_fib_va || !dev->max_cmd_size)
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;
109 pci_free_consistent(dev->pdev, alloc_size, dev->hw_fib_va,
112 dev->hw_fib_va = NULL;
116 void aac_fib_vector_assign(struct aac_dev *dev)
120 struct fib *fibptr = NULL;
122 for (i = 0, fibptr = &dev->fibs[i];
123 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
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;
130 fibptr->vector_no = vector;
132 if (vector == dev->max_msix)
139 * aac_fib_setup - setup the fibs
140 * @dev: Adapter to set up
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
146 int aac_fib_setup(struct aac_dev * dev)
149 struct hw_fib *hw_fib;
150 dma_addr_t hw_fib_pa;
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);
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));
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));
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);
179 * Initialise the fibs
181 for (i = 0, fibptr = &dev->fibs[i];
182 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
186 fibptr->size = sizeof(struct fib);
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]);
200 * one element is for the ptr to the separate sg list,
201 * second element for 32 byte alignment
203 fibptr->hw_error_pa = hw_fib_pa +
204 offsetof(struct aac_native_hba, resp.resp_bytes[0]);
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);
213 *Assign vector numbers to fibs
215 aac_fib_vector_assign(dev);
218 * Add the fib chain to the free list
220 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
222 * Set 8 fibs aside for management tools
224 dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue];
229 * aac_fib_alloc_tag-allocate a fib using tags
230 * @dev: Adapter to allocate the fib for
232 * Allocate a fib from the adapter fib pool using tags
233 * from the blk layer.
236 struct fib *aac_fib_alloc_tag(struct aac_dev *dev, struct scsi_cmnd *scmd)
240 fibptr = &dev->fibs[scmd->request->tag];
242 * Null out fields that depend on being zero at the start of
245 fibptr->hw_fib_va->header.XferState = 0;
246 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
247 fibptr->callback_data = NULL;
248 fibptr->callback = NULL;
254 * aac_fib_alloc - allocate a fib
255 * @dev: Adapter to allocate the fib for
257 * Allocate a fib from the adapter fib pool. If the pool is empty we
261 struct fib *aac_fib_alloc(struct aac_dev *dev)
265 spin_lock_irqsave(&dev->fib_lock, flags);
266 fibptr = dev->free_fib;
268 spin_unlock_irqrestore(&dev->fib_lock, flags);
271 dev->free_fib = fibptr->next;
272 spin_unlock_irqrestore(&dev->fib_lock, flags);
274 * Set the proper node type code and node byte size
276 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
277 fibptr->size = sizeof(struct fib);
279 * Null out fields that depend on being zero at the start of
282 fibptr->hw_fib_va->header.XferState = 0;
284 fibptr->callback = NULL;
285 fibptr->callback_data = NULL;
291 * aac_fib_free - free a fib
292 * @fibptr: fib to free up
294 * Frees up a fib and places it on the appropriate queue
297 void aac_fib_free(struct fib *fibptr)
301 if (fibptr->done == 2)
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",
311 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
313 fibptr->next = fibptr->dev->free_fib;
314 fibptr->dev->free_fib = fibptr;
315 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
319 * aac_fib_init - initialise a fib
320 * @fibptr: The fib to initialize
322 * Set up the generic fib fields ready for use
325 void aac_fib_init(struct fib *fibptr)
327 struct hw_fib *hw_fib = fibptr->hw_fib_va;
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);
338 * fib_deallocate - deallocate a fib
339 * @fibptr: fib to deallocate
341 * Will deallocate and return to the free pool the FIB pointed to by the
345 static void fib_dealloc(struct fib * fibptr)
347 struct hw_fib *hw_fib = fibptr->hw_fib_va;
348 hw_fib->header.XferState = 0;
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.
359 * aac_get_entry - get a queue entry
362 * @entry: Entry return
363 * @index: Index return
364 * @nonotify: notification control
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
371 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
373 struct aac_queue * q;
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.
383 q = &dev->queues->queue[qid];
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))) {
389 if (qid == AdapNormCmdQueue)
390 idx = ADAP_NORM_CMD_ENTRIES;
392 idx = ADAP_NORM_RESP_ENTRIES;
394 if (idx != le32_to_cpu(*(q->headers.consumer)))
398 if (qid == AdapNormCmdQueue) {
399 if (*index >= ADAP_NORM_CMD_ENTRIES)
400 *index = 0; /* Wrap to front of the Producer Queue. */
402 if (*index >= ADAP_NORM_RESP_ENTRIES)
403 *index = 0; /* Wrap to front of the Producer Queue. */
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));
412 *entry = q->base + *index;
418 * aac_queue_get - get the next free QE
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
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
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)
435 struct aac_entry * entry = NULL;
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");
444 * Setup queue entry with a command, status and fib mapped
446 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
449 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
450 /* if no entries wait for some if caller wants to */
453 * Setup queue entry with command, status and fib mapped
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 */
462 * If MapFib is true than we need to map the Fib and put pointers
463 * in the queue entry.
466 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
471 static inline int aac_check_eeh_failure(struct aac_dev *dev)
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.
478 * Need to be called before any PCI operation,
479 * i.e.,before aac_adapter_check_health()
481 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev->pdev);
483 if (eeh_dev_check_failure(edev)) {
484 /* The EEH mechanisms will handle this
485 * error and reset the device if
493 static inline int aac_check_eeh_failure(struct aac_dev *dev)
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.
508 * aac_fib_send - send a fib to the adapter
509 * @command: Command to send
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
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.
524 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
525 int priority, int wait, int reply, fib_callback callback,
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;
534 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
537 if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))
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.
552 if (wait && !reply) {
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);
565 * Map the fib into 32bits by using the fib number
568 hw_fib->header.SenderFibAddress =
569 cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
571 /* use the same shifted value for handle to be compatible
572 * with the new native hba command handle
574 hw_fib->header.Handle =
575 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
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
582 * Map the hw fib pointer as a 32bit value
584 hw_fib->header.Command = cpu_to_le16(command);
585 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
587 * Set the size of the Fib we want to send to the adapter
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)) {
594 * Get a queue entry connect the FIB to it and send an notify
595 * the adapter a command is ready.
597 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
600 * Fill in the Callback and CallbackContext if we are not
604 fibptr->callback = callback;
605 fibptr->callback_data = callback_data;
606 fibptr->flags = FIB_CONTEXT_FLAG;
611 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
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));
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);
633 dev->management_fib_count++;
634 spin_unlock_irqrestore(&dev->manage_lock, mflags);
635 spin_lock_irqsave(&fibptr->event_lock, flags);
638 if (dev->sync_mode) {
640 spin_unlock_irqrestore(&fibptr->event_lock, flags);
641 spin_lock_irqsave(&dev->sync_lock, sflags);
643 list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
644 spin_unlock_irqrestore(&dev->sync_lock, sflags);
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);
653 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
654 if (down_interruptible(&fibptr->event_wait)) {
655 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
663 if (aac_adapter_deliver(fibptr) != 0) {
664 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
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);
676 * If the caller wanted us to wait for response wait now.
680 spin_unlock_irqrestore(&fibptr->event_lock, flags);
681 /* Only set for first known interruptable command */
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.
689 unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
690 while (down_trylock(&fibptr->event_wait)) {
692 if (time_is_before_eq_jiffies(timeout)) {
693 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
694 atomic_dec(&q->numpending);
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");
704 if (aac_check_eeh_failure(dev))
707 if ((blink = aac_adapter_check_health(dev)) > 0) {
709 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
710 "Usually a result of a serious unrecoverable hardware problem\n",
716 * Allow other processes / CPUS to use core
720 } else if (down_interruptible(&fibptr->event_wait)) {
721 /* Do nothing ... satisfy
722 * down_interruptible must_check */
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);
731 spin_unlock_irqrestore(&fibptr->event_lock, flags);
732 BUG_ON(fibptr->done == 0);
734 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
739 * If the user does not want a response than return success otherwise
748 int aac_hba_send(u8 command, struct fib *fibptr, fib_callback callback,
751 struct aac_dev *dev = fibptr->dev;
753 unsigned long flags = 0;
754 unsigned long mflags = 0;
756 fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA);
759 fibptr->callback = callback;
760 fibptr->callback_data = callback_data;
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;
769 hbacmd->iu_type = command;
770 /* bit1 of request_id must be 0 */
772 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
778 spin_lock_irqsave(&dev->manage_lock, mflags);
779 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
780 spin_unlock_irqrestore(&dev->manage_lock, mflags);
783 dev->management_fib_count++;
784 spin_unlock_irqrestore(&dev->manage_lock, mflags);
785 spin_lock_irqsave(&fibptr->event_lock, flags);
788 if (aac_adapter_deliver(fibptr) != 0) {
790 spin_unlock_irqrestore(&fibptr->event_lock, flags);
791 spin_lock_irqsave(&dev->manage_lock, mflags);
792 dev->management_fib_count--;
793 spin_unlock_irqrestore(&dev->manage_lock, mflags);
797 FIB_COUNTER_INCREMENT(aac_config.NativeSent);
801 spin_unlock_irqrestore(&fibptr->event_lock, flags);
803 if (aac_check_eeh_failure(dev))
806 /* Only set for first known interruptable command */
807 if (down_interruptible(&fibptr->event_wait)) {
809 up(&fibptr->event_wait);
811 spin_lock_irqsave(&fibptr->event_lock, flags);
812 if ((fibptr->done == 0) || (fibptr->done == 2)) {
813 fibptr->done = 2; /* Tell interrupt we aborted */
814 spin_unlock_irqrestore(&fibptr->event_lock, flags);
817 spin_unlock_irqrestore(&fibptr->event_lock, flags);
818 WARN_ON(fibptr->done == 0);
820 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
830 * aac_consumer_get - get the top of the queue
833 * @entry: Return entry
835 * Will return a pointer to the entry on the top of the queue requested that
836 * we are a consumer of, and return the address of the queue entry. It does
837 * not change the state of the queue.
840 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
844 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
848 * The consumer index must be wrapped if we have reached
849 * the end of the queue, else we just use the entry
850 * pointed to by the header index
852 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
855 index = le32_to_cpu(*q->headers.consumer);
856 *entry = q->base + index;
863 * aac_consumer_free - free consumer entry
868 * Frees up the current top of the queue we are a consumer of. If the
869 * queue was full notify the producer that the queue is no longer full.
872 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
877 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
880 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
881 *q->headers.consumer = cpu_to_le32(1);
883 le32_add_cpu(q->headers.consumer, 1);
888 case HostNormCmdQueue:
889 notify = HostNormCmdNotFull;
891 case HostNormRespQueue:
892 notify = HostNormRespNotFull;
898 aac_adapter_notify(dev, notify);
903 * aac_fib_adapter_complete - complete adapter issued fib
904 * @fibptr: fib to complete
907 * Will do all necessary work to complete a FIB that was sent from
911 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
913 struct hw_fib * hw_fib = fibptr->hw_fib_va;
914 struct aac_dev * dev = fibptr->dev;
915 struct aac_queue * q;
916 unsigned long nointr = 0;
917 unsigned long qflags;
919 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
920 dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
921 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
926 if (hw_fib->header.XferState == 0) {
927 if (dev->comm_interface == AAC_COMM_MESSAGE)
932 * If we plan to do anything check the structure type first.
934 if (hw_fib->header.StructType != FIB_MAGIC &&
935 hw_fib->header.StructType != FIB_MAGIC2 &&
936 hw_fib->header.StructType != FIB_MAGIC2_64) {
937 if (dev->comm_interface == AAC_COMM_MESSAGE)
942 * This block handles the case where the adapter had sent us a
943 * command and we have finished processing the command. We
944 * call completeFib when we are done processing the command
945 * and want to send a response back to the adapter. This will
946 * send the completed cdb to the adapter.
948 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
949 if (dev->comm_interface == AAC_COMM_MESSAGE) {
953 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
955 size += sizeof(struct aac_fibhdr);
956 if (size > le16_to_cpu(hw_fib->header.SenderSize))
958 hw_fib->header.Size = cpu_to_le16(size);
960 q = &dev->queues->queue[AdapNormRespQueue];
961 spin_lock_irqsave(q->lock, qflags);
962 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
963 *(q->headers.producer) = cpu_to_le32(index + 1);
964 spin_unlock_irqrestore(q->lock, qflags);
965 if (!(nointr & (int)aac_config.irq_mod))
966 aac_adapter_notify(dev, AdapNormRespQueue);
969 printk(KERN_WARNING "aac_fib_adapter_complete: "
970 "Unknown xferstate detected.\n");
977 * aac_fib_complete - fib completion handler
978 * @fib: FIB to complete
980 * Will do all necessary work to complete a FIB.
983 int aac_fib_complete(struct fib *fibptr)
985 struct hw_fib * hw_fib = fibptr->hw_fib_va;
987 if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
993 * Check for a fib which has already been completed or with a
994 * status wait timeout
997 if (hw_fib->header.XferState == 0 || fibptr->done == 2)
1000 * If we plan to do anything check the structure type first.
1003 if (hw_fib->header.StructType != FIB_MAGIC &&
1004 hw_fib->header.StructType != FIB_MAGIC2 &&
1005 hw_fib->header.StructType != FIB_MAGIC2_64)
1008 * This block completes a cdb which orginated on the host and we
1009 * just need to deallocate the cdb or reinit it. At this point the
1010 * command is complete that we had sent to the adapter and this
1011 * cdb could be reused.
1014 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
1015 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
1017 fib_dealloc(fibptr);
1019 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
1022 * This handles the case when the host has aborted the I/O
1023 * to the adapter because the adapter is not responding
1025 fib_dealloc(fibptr);
1026 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
1027 fib_dealloc(fibptr);
1035 * aac_printf - handle printf from firmware
1037 * @val: Message info
1039 * Print a message passed to us by the controller firmware on the
1043 void aac_printf(struct aac_dev *dev, u32 val)
1045 char *cp = dev->printfbuf;
1046 if (dev->printf_enabled)
1048 int length = val & 0xffff;
1049 int level = (val >> 16) & 0xffff;
1052 * The size of the printfbuf is set in port.c
1053 * There is no variable or define for it
1057 if (cp[length] != 0)
1059 if (level == LOG_AAC_HIGH_ERROR)
1060 printk(KERN_WARNING "%s:%s", dev->name, cp);
1062 printk(KERN_INFO "%s:%s", dev->name, cp);
1067 static inline int aac_aif_data(struct aac_aifcmd *aifcmd, uint32_t index)
1069 return le32_to_cpu(((__le32 *)aifcmd->data)[index]);
1073 static void aac_handle_aif_bu(struct aac_dev *dev, struct aac_aifcmd *aifcmd)
1075 switch (aac_aif_data(aifcmd, 1)) {
1076 case AifBuCacheDataLoss:
1077 if (aac_aif_data(aifcmd, 2))
1078 dev_info(&dev->pdev->dev, "Backup unit had cache data loss - [%d]\n",
1079 aac_aif_data(aifcmd, 2));
1081 dev_info(&dev->pdev->dev, "Backup Unit had cache data loss\n");
1083 case AifBuCacheDataRecover:
1084 if (aac_aif_data(aifcmd, 2))
1085 dev_info(&dev->pdev->dev, "DDR cache data recovered successfully - [%d]\n",
1086 aac_aif_data(aifcmd, 2));
1088 dev_info(&dev->pdev->dev, "DDR cache data recovered successfully\n");
1094 * aac_handle_aif - Handle a message from the firmware
1095 * @dev: Which adapter this fib is from
1096 * @fibptr: Pointer to fibptr from adapter
1098 * This routine handles a driver notify fib from the adapter and
1099 * dispatches it to the appropriate routine for handling.
1102 #define AIF_SNIFF_TIMEOUT (500*HZ)
1103 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
1105 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1106 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
1107 u32 channel, id, lun, container;
1108 struct scsi_device *device;
1114 } device_config_needed = NOTHING;
1116 /* Sniff for container changes */
1118 if (!dev || !dev->fsa_dev)
1120 container = channel = id = lun = (u32)-1;
1123 * We have set this up to try and minimize the number of
1124 * re-configures that take place. As a result of this when
1125 * certain AIF's come in we will set a flag waiting for another
1126 * type of AIF before setting the re-config flag.
1128 switch (le32_to_cpu(aifcmd->command)) {
1129 case AifCmdDriverNotify:
1130 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1131 case AifRawDeviceRemove:
1132 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1133 if ((container >> 28)) {
1134 container = (u32)-1;
1137 channel = (container >> 24) & 0xF;
1138 if (channel >= dev->maximum_num_channels) {
1139 container = (u32)-1;
1142 id = container & 0xFFFF;
1143 if (id >= dev->maximum_num_physicals) {
1144 container = (u32)-1;
1147 lun = (container >> 16) & 0xFF;
1148 container = (u32)-1;
1149 channel = aac_phys_to_logical(channel);
1150 device_config_needed = DELETE;
1154 * Morph or Expand complete
1156 case AifDenMorphComplete:
1157 case AifDenVolumeExtendComplete:
1158 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1159 if (container >= dev->maximum_num_containers)
1163 * Find the scsi_device associated with the SCSI
1164 * address. Make sure we have the right array, and if
1165 * so set the flag to initiate a new re-config once we
1166 * see an AifEnConfigChange AIF come through.
1169 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
1170 device = scsi_device_lookup(dev->scsi_host_ptr,
1171 CONTAINER_TO_CHANNEL(container),
1172 CONTAINER_TO_ID(container),
1173 CONTAINER_TO_LUN(container));
1175 dev->fsa_dev[container].config_needed = CHANGE;
1176 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
1177 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1178 scsi_device_put(device);
1184 * If we are waiting on something and this happens to be
1185 * that thing then set the re-configure flag.
1187 if (container != (u32)-1) {
1188 if (container >= dev->maximum_num_containers)
1190 if ((dev->fsa_dev[container].config_waiting_on ==
1191 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1192 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1193 dev->fsa_dev[container].config_waiting_on = 0;
1194 } else for (container = 0;
1195 container < dev->maximum_num_containers; ++container) {
1196 if ((dev->fsa_dev[container].config_waiting_on ==
1197 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1198 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1199 dev->fsa_dev[container].config_waiting_on = 0;
1203 case AifCmdEventNotify:
1204 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1205 case AifEnBatteryEvent:
1206 dev->cache_protected =
1207 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
1212 case AifEnAddContainer:
1213 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1214 if (container >= dev->maximum_num_containers)
1216 dev->fsa_dev[container].config_needed = ADD;
1217 dev->fsa_dev[container].config_waiting_on =
1219 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1225 case AifEnDeleteContainer:
1226 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1227 if (container >= dev->maximum_num_containers)
1229 dev->fsa_dev[container].config_needed = DELETE;
1230 dev->fsa_dev[container].config_waiting_on =
1232 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1236 * Container change detected. If we currently are not
1237 * waiting on something else, setup to wait on a Config Change.
1239 case AifEnContainerChange:
1240 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1241 if (container >= dev->maximum_num_containers)
1243 if (dev->fsa_dev[container].config_waiting_on &&
1244 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1246 dev->fsa_dev[container].config_needed = CHANGE;
1247 dev->fsa_dev[container].config_waiting_on =
1249 dev->fsa_dev[container].config_waiting_stamp = jiffies;
1252 case AifEnConfigChange:
1256 case AifEnDeleteJBOD:
1257 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1258 if ((container >> 28)) {
1259 container = (u32)-1;
1262 channel = (container >> 24) & 0xF;
1263 if (channel >= dev->maximum_num_channels) {
1264 container = (u32)-1;
1267 id = container & 0xFFFF;
1268 if (id >= dev->maximum_num_physicals) {
1269 container = (u32)-1;
1272 lun = (container >> 16) & 0xFF;
1273 container = (u32)-1;
1274 channel = aac_phys_to_logical(channel);
1275 device_config_needed =
1276 (((__le32 *)aifcmd->data)[0] ==
1277 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
1278 if (device_config_needed == ADD) {
1279 device = scsi_device_lookup(dev->scsi_host_ptr,
1284 scsi_remove_device(device);
1285 scsi_device_put(device);
1290 case AifEnEnclosureManagement:
1292 * If in JBOD mode, automatic exposure of new
1293 * physical target to be suppressed until configured.
1297 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1298 case EM_DRIVE_INSERTION:
1299 case EM_DRIVE_REMOVAL:
1300 case EM_SES_DRIVE_INSERTION:
1301 case EM_SES_DRIVE_REMOVAL:
1302 container = le32_to_cpu(
1303 ((__le32 *)aifcmd->data)[2]);
1304 if ((container >> 28)) {
1305 container = (u32)-1;
1308 channel = (container >> 24) & 0xF;
1309 if (channel >= dev->maximum_num_channels) {
1310 container = (u32)-1;
1313 id = container & 0xFFFF;
1314 lun = (container >> 16) & 0xFF;
1315 container = (u32)-1;
1316 if (id >= dev->maximum_num_physicals) {
1317 /* legacy dev_t ? */
1318 if ((0x2000 <= id) || lun || channel ||
1319 ((channel = (id >> 7) & 0x3F) >=
1320 dev->maximum_num_channels))
1322 lun = (id >> 4) & 7;
1325 channel = aac_phys_to_logical(channel);
1326 device_config_needed =
1327 ((((__le32 *)aifcmd->data)[3]
1328 == cpu_to_le32(EM_DRIVE_INSERTION)) ||
1329 (((__le32 *)aifcmd->data)[3]
1330 == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
1334 case AifBuManagerEvent:
1335 aac_handle_aif_bu(dev, aifcmd);
1340 * If we are waiting on something and this happens to be
1341 * that thing then set the re-configure flag.
1343 if (container != (u32)-1) {
1344 if (container >= dev->maximum_num_containers)
1346 if ((dev->fsa_dev[container].config_waiting_on ==
1347 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1348 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1349 dev->fsa_dev[container].config_waiting_on = 0;
1350 } else for (container = 0;
1351 container < dev->maximum_num_containers; ++container) {
1352 if ((dev->fsa_dev[container].config_waiting_on ==
1353 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1354 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1355 dev->fsa_dev[container].config_waiting_on = 0;
1359 case AifCmdJobProgress:
1361 * These are job progress AIF's. When a Clear is being
1362 * done on a container it is initially created then hidden from
1363 * the OS. When the clear completes we don't get a config
1364 * change so we monitor the job status complete on a clear then
1365 * wait for a container change.
1368 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1369 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1370 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1372 container < dev->maximum_num_containers;
1375 * Stomp on all config sequencing for all
1378 dev->fsa_dev[container].config_waiting_on =
1379 AifEnContainerChange;
1380 dev->fsa_dev[container].config_needed = ADD;
1381 dev->fsa_dev[container].config_waiting_stamp =
1385 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1386 ((__le32 *)aifcmd->data)[6] == 0 &&
1387 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1389 container < dev->maximum_num_containers;
1392 * Stomp on all config sequencing for all
1395 dev->fsa_dev[container].config_waiting_on =
1396 AifEnContainerChange;
1397 dev->fsa_dev[container].config_needed = DELETE;
1398 dev->fsa_dev[container].config_waiting_stamp =
1407 if (device_config_needed == NOTHING)
1408 for (; container < dev->maximum_num_containers; ++container) {
1409 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1410 (dev->fsa_dev[container].config_needed != NOTHING) &&
1411 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1412 device_config_needed =
1413 dev->fsa_dev[container].config_needed;
1414 dev->fsa_dev[container].config_needed = NOTHING;
1415 channel = CONTAINER_TO_CHANNEL(container);
1416 id = CONTAINER_TO_ID(container);
1417 lun = CONTAINER_TO_LUN(container);
1421 if (device_config_needed == NOTHING)
1425 * If we decided that a re-configuration needs to be done,
1426 * schedule it here on the way out the door, please close the door
1431 * Find the scsi_device associated with the SCSI address,
1432 * and mark it as changed, invalidating the cache. This deals
1433 * with changes to existing device IDs.
1436 if (!dev || !dev->scsi_host_ptr)
1439 * force reload of disk info via aac_probe_container
1441 if ((channel == CONTAINER_CHANNEL) &&
1442 (device_config_needed != NOTHING)) {
1443 if (dev->fsa_dev[container].valid == 1)
1444 dev->fsa_dev[container].valid = 2;
1445 aac_probe_container(dev, container);
1447 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1449 switch (device_config_needed) {
1451 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1452 scsi_remove_device(device);
1454 if (scsi_device_online(device)) {
1455 scsi_device_set_state(device, SDEV_OFFLINE);
1456 sdev_printk(KERN_INFO, device,
1457 "Device offlined - %s\n",
1458 (channel == CONTAINER_CHANNEL) ?
1460 "enclosure services event");
1465 if (!scsi_device_online(device)) {
1466 sdev_printk(KERN_INFO, device,
1467 "Device online - %s\n",
1468 (channel == CONTAINER_CHANNEL) ?
1470 "enclosure services event");
1471 scsi_device_set_state(device, SDEV_RUNNING);
1475 if ((channel == CONTAINER_CHANNEL)
1476 && (!dev->fsa_dev[container].valid)) {
1477 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1478 scsi_remove_device(device);
1480 if (!scsi_device_online(device))
1482 scsi_device_set_state(device, SDEV_OFFLINE);
1483 sdev_printk(KERN_INFO, device,
1484 "Device offlined - %s\n",
1489 scsi_rescan_device(&device->sdev_gendev);
1494 scsi_device_put(device);
1495 device_config_needed = NOTHING;
1497 if (device_config_needed == ADD)
1498 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1499 if (channel == CONTAINER_CHANNEL) {
1501 device_config_needed = NOTHING;
1506 static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1510 struct Scsi_Host *host;
1511 struct scsi_device *dev;
1512 struct scsi_cmnd *command;
1513 struct scsi_cmnd *command_list;
1519 * - host is locked, unless called by the aacraid thread.
1520 * (a matter of convenience, due to legacy issues surrounding
1521 * eh_host_adapter_reset).
1522 * - in_reset is asserted, so no new i/o is getting to the
1524 * - The card is dead, or will be very shortly ;-/ so no new
1525 * commands are completing in the interrupt service.
1527 host = aac->scsi_host_ptr;
1528 scsi_block_requests(host);
1529 aac_adapter_disable_int(aac);
1530 if (aac->thread->pid != current->pid) {
1531 spin_unlock_irq(host->host_lock);
1532 kthread_stop(aac->thread);
1537 * If a positive health, means in a known DEAD PANIC
1538 * state and the adapter could be reset to `try again'.
1540 bled = forced ? 0 : aac_adapter_check_health(aac);
1541 retval = aac_adapter_restart(aac, bled, reset_type);
1547 * Loop through the fibs, close the synchronous FIBS
1549 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1550 struct fib *fib = &aac->fibs[index];
1551 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1552 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1553 unsigned long flagv;
1554 spin_lock_irqsave(&fib->event_lock, flagv);
1555 up(&fib->event_wait);
1556 spin_unlock_irqrestore(&fib->event_lock, flagv);
1561 /* Give some extra time for ioctls to complete. */
1564 index = aac->cardtype;
1567 * Re-initialize the adapter, first free resources, then carefully
1568 * apply the initialization sequence to come back again. Only risk
1569 * is a change in Firmware dropping cache, it is assumed the caller
1570 * will ensure that i/o is queisced and the card is flushed in that
1573 aac_fib_map_free(aac);
1574 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1575 aac->comm_addr = NULL;
1580 kfree(aac->fsa_dev);
1581 aac->fsa_dev = NULL;
1582 quirks = aac_get_driver_ident(index)->quirks;
1583 if (quirks & AAC_QUIRK_31BIT) {
1584 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1585 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1588 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1589 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1592 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1594 if (quirks & AAC_QUIRK_31BIT)
1595 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1598 aac->thread = kthread_run(aac_command_thread, aac, "%s",
1600 if (IS_ERR(aac->thread)) {
1601 retval = PTR_ERR(aac->thread);
1605 (void)aac_get_adapter_info(aac);
1606 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1607 host->sg_tablesize = 34;
1608 host->max_sectors = (host->sg_tablesize * 8) + 112;
1610 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1611 host->sg_tablesize = 17;
1612 host->max_sectors = (host->sg_tablesize * 8) + 112;
1614 aac_get_config_status(aac, 1);
1615 aac_get_containers(aac);
1617 * This is where the assumption that the Adapter is quiesced
1620 command_list = NULL;
1621 __shost_for_each_device(dev, host) {
1622 unsigned long flags;
1623 spin_lock_irqsave(&dev->list_lock, flags);
1624 list_for_each_entry(command, &dev->cmd_list, list)
1625 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1626 command->SCp.buffer = (struct scatterlist *)command_list;
1627 command_list = command;
1629 spin_unlock_irqrestore(&dev->list_lock, flags);
1631 while ((command = command_list)) {
1632 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1633 command->SCp.buffer = NULL;
1634 command->result = DID_OK << 16
1635 | COMMAND_COMPLETE << 8
1636 | SAM_STAT_TASK_SET_FULL;
1637 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1638 command->scsi_done(command);
1641 * Any Device that was already marked offline needs to be cleaned up
1643 __shost_for_each_device(dev, host) {
1644 if (!scsi_device_online(dev)) {
1645 sdev_printk(KERN_INFO, dev, "Removing offline device\n");
1646 scsi_remove_device(dev);
1647 scsi_device_put(dev);
1654 scsi_unblock_requests(host);
1656 * Issue bus rescan to catch any configuration that might have
1660 dev_info(&aac->pdev->dev, "Issuing bus rescan\n");
1661 scsi_scan_host(host);
1664 spin_lock_irq(host->host_lock);
1669 int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1671 unsigned long flagv = 0;
1673 struct Scsi_Host * host;
1676 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1679 if (aac->in_reset) {
1680 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1684 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1687 * Wait for all commands to complete to this specific
1688 * target (block maximum 60 seconds). Although not necessary,
1689 * it does make us a good storage citizen.
1691 host = aac->scsi_host_ptr;
1692 scsi_block_requests(host);
1693 if (forced < 2) for (retval = 60; retval; --retval) {
1694 struct scsi_device * dev;
1695 struct scsi_cmnd * command;
1698 __shost_for_each_device(dev, host) {
1699 spin_lock_irqsave(&dev->list_lock, flagv);
1700 list_for_each_entry(command, &dev->cmd_list, list) {
1701 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1706 spin_unlock_irqrestore(&dev->list_lock, flagv);
1712 * We can exit If all the commands are complete
1719 /* Quiesce build, flush cache, write through mode */
1721 aac_send_shutdown(aac);
1722 spin_lock_irqsave(host->host_lock, flagv);
1723 bled = forced ? forced :
1724 (aac_check_reset != 0 && aac_check_reset != 1);
1725 retval = _aac_reset_adapter(aac, bled, reset_type);
1726 spin_unlock_irqrestore(host->host_lock, flagv);
1728 if ((forced < 2) && (retval == -ENODEV)) {
1729 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1730 struct fib * fibctx = aac_fib_alloc(aac);
1732 struct aac_pause *cmd;
1735 aac_fib_init(fibctx);
1737 cmd = (struct aac_pause *) fib_data(fibctx);
1739 cmd->command = cpu_to_le32(VM_ContainerConfig);
1740 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1741 cmd->timeout = cpu_to_le32(1);
1742 cmd->min = cpu_to_le32(1);
1743 cmd->noRescan = cpu_to_le32(1);
1744 cmd->count = cpu_to_le32(0);
1746 status = aac_fib_send(ContainerCommand,
1748 sizeof(struct aac_pause),
1750 -2 /* Timeout silently */, 1,
1754 aac_fib_complete(fibctx);
1755 /* FIB should be freed only after getting
1756 * the response from the F/W */
1757 if (status != -ERESTARTSYS)
1758 aac_fib_free(fibctx);
1765 int aac_check_health(struct aac_dev * aac)
1768 unsigned long time_now, flagv = 0;
1769 struct list_head * entry;
1770 struct Scsi_Host * host;
1773 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1774 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1777 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1778 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1785 * aac_aifcmd.command = AifCmdEventNotify = 1
1786 * aac_aifcmd.seqnum = 0xFFFFFFFF
1787 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1788 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1789 * aac.aifcmd.data[2] = AifHighPriority = 3
1790 * aac.aifcmd.data[3] = BlinkLED
1793 time_now = jiffies/HZ;
1794 entry = aac->fib_list.next;
1797 * For each Context that is on the
1798 * fibctxList, make a copy of the
1799 * fib, and then set the event to wake up the
1800 * thread that is waiting for it.
1802 while (entry != &aac->fib_list) {
1804 * Extract the fibctx
1806 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1807 struct hw_fib * hw_fib;
1810 * Check if the queue is getting
1813 if (fibctx->count > 20) {
1815 * It's *not* jiffies folks,
1816 * but jiffies / HZ, so do not
1819 u32 time_last = fibctx->jiffies;
1821 * Has it been > 2 minutes
1822 * since the last read off
1825 if ((time_now - time_last) > aif_timeout) {
1826 entry = entry->next;
1827 aac_close_fib_context(aac, fibctx);
1832 * Warning: no sleep allowed while
1835 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1836 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1837 if (fib && hw_fib) {
1838 struct aac_aifcmd * aif;
1840 fib->hw_fib_va = hw_fib;
1843 fib->type = FSAFS_NTC_FIB_CONTEXT;
1844 fib->size = sizeof (struct fib);
1845 fib->data = hw_fib->data;
1846 aif = (struct aac_aifcmd *)hw_fib->data;
1847 aif->command = cpu_to_le32(AifCmdEventNotify);
1848 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1849 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1850 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1851 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1852 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1855 * Put the FIB onto the
1858 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1861 * Set the event to wake up the
1862 * thread that will waiting.
1864 up(&fibctx->wait_sem);
1866 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1870 entry = entry->next;
1873 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1876 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1880 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1882 if (!aac_check_reset || ((aac_check_reset == 1) &&
1883 (aac->supplement_adapter_info.supported_options2 &
1884 AAC_OPTION_IGNORE_RESET)))
1886 host = aac->scsi_host_ptr;
1887 if (aac->thread->pid != current->pid)
1888 spin_lock_irqsave(host->host_lock, flagv);
1889 bled = aac_check_reset != 1 ? 1 : 0;
1890 _aac_reset_adapter(aac, bled, IOP_HWSOFT_RESET);
1891 if (aac->thread->pid != current->pid)
1892 spin_unlock_irqrestore(host->host_lock, flagv);
1901 static void aac_resolve_luns(struct aac_dev *dev)
1903 int bus, target, channel;
1904 struct scsi_device *sdev;
1908 for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
1909 for (target = 0; target < AAC_MAX_TARGETS; target++) {
1911 if (bus == CONTAINER_CHANNEL)
1912 channel = CONTAINER_CHANNEL;
1914 channel = aac_phys_to_logical(bus);
1916 devtype = dev->hba_map[bus][target].devtype;
1917 new_devtype = dev->hba_map[bus][target].new_devtype;
1919 sdev = scsi_device_lookup(dev->scsi_host_ptr, channel,
1922 if (!sdev && new_devtype)
1923 scsi_add_device(dev->scsi_host_ptr, channel,
1925 else if (sdev && new_devtype != devtype)
1926 scsi_remove_device(sdev);
1927 else if (sdev && new_devtype == devtype)
1928 scsi_rescan_device(&sdev->sdev_gendev);
1931 scsi_device_put(sdev);
1933 dev->hba_map[bus][target].devtype = new_devtype;
1939 * aac_handle_sa_aif Handle a message from the firmware
1940 * @dev: Which adapter this fib is from
1941 * @fibptr: Pointer to fibptr from adapter
1943 * This routine handles a driver notify fib from the adapter and
1944 * dispatches it to the appropriate routine for handling.
1946 static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr)
1948 int i, bus, target, container, rcode = 0;
1951 struct scsi_device *sdev;
1953 if (fibptr->hbacmd_size & SA_AIF_HOTPLUG)
1954 events = SA_AIF_HOTPLUG;
1955 else if (fibptr->hbacmd_size & SA_AIF_HARDWARE)
1956 events = SA_AIF_HARDWARE;
1957 else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE)
1958 events = SA_AIF_PDEV_CHANGE;
1959 else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE)
1960 events = SA_AIF_LDEV_CHANGE;
1961 else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE)
1962 events = SA_AIF_BPSTAT_CHANGE;
1963 else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE)
1964 events = SA_AIF_BPCFG_CHANGE;
1967 case SA_AIF_HOTPLUG:
1968 case SA_AIF_HARDWARE:
1969 case SA_AIF_PDEV_CHANGE:
1970 case SA_AIF_LDEV_CHANGE:
1971 case SA_AIF_BPCFG_CHANGE:
1973 fib = aac_fib_alloc(dev);
1975 pr_err("aac_handle_sa_aif: out of memory\n");
1978 for (bus = 0; bus < AAC_MAX_BUSES; bus++)
1979 for (target = 0; target < AAC_MAX_TARGETS; target++)
1980 dev->hba_map[bus][target].new_devtype = 0;
1982 rcode = aac_report_phys_luns(dev, fib, AAC_RESCAN);
1984 if (rcode != -ERESTARTSYS)
1987 aac_resolve_luns(dev);
1989 if (events == SA_AIF_LDEV_CHANGE ||
1990 events == SA_AIF_BPCFG_CHANGE) {
1991 aac_get_containers(dev);
1992 for (container = 0; container <
1993 dev->maximum_num_containers; ++container) {
1994 sdev = scsi_device_lookup(dev->scsi_host_ptr,
1997 if (dev->fsa_dev[container].valid && !sdev) {
1998 scsi_add_device(dev->scsi_host_ptr,
2001 } else if (!dev->fsa_dev[container].valid &&
2003 scsi_remove_device(sdev);
2004 scsi_device_put(sdev);
2006 scsi_rescan_device(&sdev->sdev_gendev);
2007 scsi_device_put(sdev);
2013 case SA_AIF_BPSTAT_CHANGE:
2014 /* currently do nothing */
2018 for (i = 1; i <= 10; ++i) {
2019 events = src_readl(dev, MUnit.IDR);
2020 if (events & (1<<23)) {
2021 pr_warn(" AIF not cleared by firmware - %d/%d)\n",
2028 static int get_fib_count(struct aac_dev *dev)
2030 unsigned int num = 0;
2031 struct list_head *entry;
2032 unsigned long flagv;
2035 * Warning: no sleep allowed while
2036 * holding spinlock. We take the estimate
2037 * and pre-allocate a set of fibs outside the
2040 num = le32_to_cpu(dev->init->r7.adapter_fibs_size)
2041 / sizeof(struct hw_fib); /* some extra */
2042 spin_lock_irqsave(&dev->fib_lock, flagv);
2043 entry = dev->fib_list.next;
2044 while (entry != &dev->fib_list) {
2045 entry = entry->next;
2048 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2053 static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool,
2054 struct fib **fib_pool,
2057 struct hw_fib **hw_fib_p;
2061 hw_fib_p = hw_fib_pool;
2063 while (hw_fib_p < &hw_fib_pool[num]) {
2064 *(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL);
2065 if (!(*(hw_fib_p++))) {
2070 *(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL);
2071 if (!(*(fib_p++))) {
2072 kfree(*(--hw_fib_p));
2077 num = hw_fib_p - hw_fib_pool;
2084 static void wakeup_fibctx_threads(struct aac_dev *dev,
2085 struct hw_fib **hw_fib_pool,
2086 struct fib **fib_pool,
2088 struct hw_fib *hw_fib,
2091 unsigned long flagv;
2092 struct list_head *entry;
2093 struct hw_fib **hw_fib_p;
2095 u32 time_now, time_last;
2096 struct hw_fib *hw_newfib;
2098 struct aac_fib_context *fibctx;
2100 time_now = jiffies/HZ;
2101 spin_lock_irqsave(&dev->fib_lock, flagv);
2102 entry = dev->fib_list.next;
2104 * For each Context that is on the
2105 * fibctxList, make a copy of the
2106 * fib, and then set the event to wake up the
2107 * thread that is waiting for it.
2110 hw_fib_p = hw_fib_pool;
2112 while (entry != &dev->fib_list) {
2114 * Extract the fibctx
2116 fibctx = list_entry(entry, struct aac_fib_context,
2119 * Check if the queue is getting
2122 if (fibctx->count > 20) {
2124 * It's *not* jiffies folks,
2125 * but jiffies / HZ so do not
2128 time_last = fibctx->jiffies;
2130 * Has it been > 2 minutes
2131 * since the last read off
2134 if ((time_now - time_last) > aif_timeout) {
2135 entry = entry->next;
2136 aac_close_fib_context(dev, fibctx);
2141 * Warning: no sleep allowed while
2144 if (hw_fib_p >= &hw_fib_pool[num]) {
2145 pr_warn("aifd: didn't allocate NewFib\n");
2146 entry = entry->next;
2150 hw_newfib = *hw_fib_p;
2151 *(hw_fib_p++) = NULL;
2155 * Make the copy of the FIB
2157 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
2158 memcpy(newfib, fib, sizeof(struct fib));
2159 newfib->hw_fib_va = hw_newfib;
2161 * Put the FIB onto the
2164 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
2167 * Set the event to wake up the
2168 * thread that is waiting.
2170 up(&fibctx->wait_sem);
2172 entry = entry->next;
2175 * Set the status of this FIB
2177 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2178 aac_fib_adapter_complete(fib, sizeof(u32));
2179 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2183 static void aac_process_events(struct aac_dev *dev)
2185 struct hw_fib *hw_fib;
2187 unsigned long flags;
2191 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2192 spin_lock_irqsave(t_lock, flags);
2194 while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
2195 struct list_head *entry;
2196 struct aac_aifcmd *aifcmd;
2198 struct hw_fib **hw_fib_pool, **hw_fib_p;
2199 struct fib **fib_pool, **fib_p;
2201 set_current_state(TASK_RUNNING);
2203 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
2206 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2207 spin_unlock_irqrestore(t_lock, flags);
2209 fib = list_entry(entry, struct fib, fiblink);
2210 hw_fib = fib->hw_fib_va;
2211 if (dev->sa_firmware) {
2213 aac_handle_sa_aif(dev, fib);
2214 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2218 * We will process the FIB here or pass it to a
2219 * worker thread that is TBD. We Really can't
2220 * do anything at this point since we don't have
2221 * anything defined for this thread to do.
2223 memset(fib, 0, sizeof(struct fib));
2224 fib->type = FSAFS_NTC_FIB_CONTEXT;
2225 fib->size = sizeof(struct fib);
2226 fib->hw_fib_va = hw_fib;
2227 fib->data = hw_fib->data;
2230 * We only handle AifRequest fibs from the adapter.
2233 aifcmd = (struct aac_aifcmd *) hw_fib->data;
2234 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
2235 /* Handle Driver Notify Events */
2236 aac_handle_aif(dev, fib);
2237 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
2238 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2242 * The u32 here is important and intended. We are using
2243 * 32bit wrapping time to fit the adapter field
2247 if (aifcmd->command == cpu_to_le32(AifCmdEventNotify)
2248 || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) {
2249 aac_handle_aif(dev, fib);
2253 * get number of fibs to process
2255 num = get_fib_count(dev);
2259 hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *),
2264 fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL);
2266 goto free_hw_fib_pool;
2269 * Fill up fib pointer pools with actual fibs
2272 rcode = fillup_pools(dev, hw_fib_pool, fib_pool, num);
2277 * wakeup the thread that is waiting for
2278 * the response from fw (ioctl)
2280 wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool,
2284 /* Free up the remaining resources */
2285 hw_fib_p = hw_fib_pool;
2287 while (hw_fib_p < &hw_fib_pool[num]) {
2298 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2299 spin_lock_irqsave(t_lock, flags);
2302 * There are no more AIF's
2304 t_lock = dev->queues->queue[HostNormCmdQueue].lock;
2305 spin_unlock_irqrestore(t_lock, flags);
2308 static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str,
2311 struct aac_srb *srbcmd;
2312 struct sgmap64 *sg64;
2319 fibptr = aac_fib_alloc(dev);
2323 dma_buf = pci_alloc_consistent(dev->pdev, datasize, &addr);
2327 aac_fib_init(fibptr);
2329 vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
2330 vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
2332 srbcmd = (struct aac_srb *)fib_data(fibptr);
2334 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
2335 srbcmd->channel = cpu_to_le32(vbus);
2336 srbcmd->id = cpu_to_le32(vid);
2338 srbcmd->flags = cpu_to_le32(SRB_DataOut);
2339 srbcmd->timeout = cpu_to_le32(10);
2340 srbcmd->retry_limit = 0;
2341 srbcmd->cdb_size = cpu_to_le32(12);
2342 srbcmd->count = cpu_to_le32(datasize);
2344 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2345 srbcmd->cdb[0] = BMIC_OUT;
2346 srbcmd->cdb[6] = WRITE_HOST_WELLNESS;
2347 memcpy(dma_buf, (char *)wellness_str, datasize);
2349 sg64 = (struct sgmap64 *)&srbcmd->sg;
2350 sg64->count = cpu_to_le32(1);
2351 sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
2352 sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2353 sg64->sg[0].count = cpu_to_le32(datasize);
2355 ret = aac_fib_send(ScsiPortCommand64, fibptr, sizeof(struct aac_srb),
2356 FsaNormal, 1, 1, NULL, NULL);
2358 pci_free_consistent(dev->pdev, datasize, (void *)dma_buf, addr);
2361 * Do not set XferState to zero unless
2362 * receives a response from F/W
2365 aac_fib_complete(fibptr);
2368 * FIB should be freed only after
2369 * getting the response from the F/W
2371 if (ret != -ERESTARTSYS)
2377 aac_fib_free(fibptr);
2381 int aac_send_safw_hostttime(struct aac_dev *dev, struct timeval *now)
2384 char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
2385 u32 datasize = sizeof(wellness_str);
2386 unsigned long local_time;
2389 if (!dev->sa_firmware)
2392 local_time = (u32)(now->tv_sec - (sys_tz.tz_minuteswest * 60));
2393 time_to_tm(local_time, 0, &cur_tm);
2395 cur_tm.tm_year += 1900;
2396 wellness_str[8] = bin2bcd(cur_tm.tm_hour);
2397 wellness_str[9] = bin2bcd(cur_tm.tm_min);
2398 wellness_str[10] = bin2bcd(cur_tm.tm_sec);
2399 wellness_str[12] = bin2bcd(cur_tm.tm_mon);
2400 wellness_str[13] = bin2bcd(cur_tm.tm_mday);
2401 wellness_str[14] = bin2bcd(cur_tm.tm_year / 100);
2402 wellness_str[15] = bin2bcd(cur_tm.tm_year % 100);
2404 ret = aac_send_wellness_command(dev, wellness_str, datasize);
2410 int aac_send_hosttime(struct aac_dev *dev, struct timeval *now)
2416 fibptr = aac_fib_alloc(dev);
2420 aac_fib_init(fibptr);
2421 info = (__le32 *)fib_data(fibptr);
2422 *info = cpu_to_le32(now->tv_sec);
2423 ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal,
2427 * Do not set XferState to zero unless
2428 * receives a response from F/W
2431 aac_fib_complete(fibptr);
2434 * FIB should be freed only after
2435 * getting the response from the F/W
2437 if (ret != -ERESTARTSYS)
2438 aac_fib_free(fibptr);
2445 * aac_command_thread - command processing thread
2446 * @dev: Adapter to monitor
2448 * Waits on the commandready event in it's queue. When the event gets set
2449 * it will pull FIBs off it's queue. It will continue to pull FIBs off
2450 * until the queue is empty. When the queue is empty it will wait for
2454 int aac_command_thread(void *data)
2456 struct aac_dev *dev = data;
2457 DECLARE_WAITQUEUE(wait, current);
2458 unsigned long next_jiffies = jiffies + HZ;
2459 unsigned long next_check_jiffies = next_jiffies;
2460 long difference = HZ;
2463 * We can only have one thread per adapter for AIF's.
2465 if (dev->aif_thread)
2469 * Let the DPC know it has a place to send the AIF's to.
2471 dev->aif_thread = 1;
2472 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
2473 set_current_state(TASK_INTERRUPTIBLE);
2474 dprintk ((KERN_INFO "aac_command_thread start\n"));
2477 aac_process_events(dev);
2480 * Background activity
2482 if ((time_before(next_check_jiffies,next_jiffies))
2483 && ((difference = next_check_jiffies - jiffies) <= 0)) {
2484 next_check_jiffies = next_jiffies;
2485 if (aac_check_health(dev) == 0) {
2486 difference = ((long)(unsigned)check_interval)
2488 next_check_jiffies = jiffies + difference;
2489 } else if (!dev->queues)
2492 if (!time_before(next_check_jiffies,next_jiffies)
2493 && ((difference = next_jiffies - jiffies) <= 0)) {
2497 /* Don't even try to talk to adapter if its sick */
2498 ret = aac_check_health(dev);
2499 if (ret || !dev->queues)
2501 next_check_jiffies = jiffies
2502 + ((long)(unsigned)check_interval)
2504 do_gettimeofday(&now);
2506 /* Synchronize our watches */
2507 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
2508 && (now.tv_usec > (1000000 / HZ)))
2509 difference = (((1000000 - now.tv_usec) * HZ)
2510 + 500000) / 1000000;
2512 if (now.tv_usec > 500000)
2515 if (dev->sa_firmware)
2517 aac_send_safw_hostttime(dev, &now);
2519 ret = aac_send_hosttime(dev, &now);
2521 difference = (long)(unsigned)update_interval*HZ;
2523 next_jiffies = jiffies + difference;
2524 if (time_before(next_check_jiffies,next_jiffies))
2525 difference = next_check_jiffies - jiffies;
2527 if (difference <= 0)
2529 set_current_state(TASK_INTERRUPTIBLE);
2531 if (kthread_should_stop())
2534 schedule_timeout(difference);
2536 if (kthread_should_stop())
2540 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
2541 dev->aif_thread = 0;
2545 int aac_acquire_irq(struct aac_dev *dev)
2551 if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) {
2552 for (i = 0; i < dev->max_msix; i++) {
2553 dev->aac_msix[i].vector_no = i;
2554 dev->aac_msix[i].dev = dev;
2555 if (request_irq(pci_irq_vector(dev->pdev, i),
2556 dev->a_ops.adapter_intr,
2557 0, "aacraid", &(dev->aac_msix[i]))) {
2558 printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n",
2559 dev->name, dev->id, i);
2560 for (j = 0 ; j < i ; j++)
2561 free_irq(pci_irq_vector(dev->pdev, j),
2562 &(dev->aac_msix[j]));
2563 pci_disable_msix(dev->pdev);
2568 dev->aac_msix[0].vector_no = 0;
2569 dev->aac_msix[0].dev = dev;
2571 if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
2572 IRQF_SHARED, "aacraid",
2573 &(dev->aac_msix[0])) < 0) {
2575 pci_disable_msi(dev->pdev);
2576 printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
2577 dev->name, dev->id);
2584 void aac_free_irq(struct aac_dev *dev)
2589 cpu = cpumask_first(cpu_online_mask);
2590 if (dev->pdev->device == PMC_DEVICE_S6 ||
2591 dev->pdev->device == PMC_DEVICE_S7 ||
2592 dev->pdev->device == PMC_DEVICE_S8 ||
2593 dev->pdev->device == PMC_DEVICE_S9) {
2594 if (dev->max_msix > 1) {
2595 for (i = 0; i < dev->max_msix; i++)
2596 free_irq(pci_irq_vector(dev->pdev, i),
2597 &(dev->aac_msix[i]));
2599 free_irq(dev->pdev->irq, &(dev->aac_msix[0]));
2602 free_irq(dev->pdev->irq, dev);
2605 pci_disable_msi(dev->pdev);
2606 else if (dev->max_msix > 1)
2607 pci_disable_msix(dev->pdev);