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 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Abstract: Contain all routines that are required for FSA host/adapter
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/sched.h>
37 #include <linux/pci.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/completion.h>
41 #include <linux/blkdev.h>
42 #include <linux/delay.h>
43 #include <linux/kthread.h>
44 #include <linux/interrupt.h>
45 #include <linux/semaphore.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_cmnd.h>
54 * fib_map_alloc - allocate the fib objects
55 * @dev: Adapter to allocate for
57 * Allocate and map the shared PCI space for the FIB blocks used to
58 * talk to the Adaptec firmware.
61 static int fib_map_alloc(struct aac_dev *dev)
64 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
65 dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
66 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
67 dev->hw_fib_va = pci_alloc_consistent(dev->pdev,
68 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr))
69 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
71 if (dev->hw_fib_va == NULL)
77 * aac_fib_map_free - free the fib objects
78 * @dev: Adapter to free
80 * Free the PCI mappings and the memory allocated for FIB blocks
84 void aac_fib_map_free(struct aac_dev *dev)
86 if (dev->hw_fib_va && dev->max_fib_size) {
87 pci_free_consistent(dev->pdev,
89 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB)),
90 dev->hw_fib_va, dev->hw_fib_pa);
92 dev->hw_fib_va = NULL;
97 * aac_fib_setup - setup the fibs
98 * @dev: Adapter to set up
100 * Allocate the PCI space for the fibs, map it and then initialise the
101 * fib area, the unmapped fib data and also the free list
104 int aac_fib_setup(struct aac_dev * dev)
107 struct hw_fib *hw_fib;
108 dma_addr_t hw_fib_pa;
111 while (((i = fib_map_alloc(dev)) == -ENOMEM)
112 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
113 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
114 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
119 /* 32 byte alignment for PMC */
120 hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
121 dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
122 (hw_fib_pa - dev->hw_fib_pa));
123 dev->hw_fib_pa = hw_fib_pa;
124 memset(dev->hw_fib_va, 0,
125 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
126 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
128 /* add Xport header */
129 dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
130 sizeof(struct aac_fib_xporthdr));
131 dev->hw_fib_pa += sizeof(struct aac_fib_xporthdr);
133 hw_fib = dev->hw_fib_va;
134 hw_fib_pa = dev->hw_fib_pa;
136 * Initialise the fibs
138 for (i = 0, fibptr = &dev->fibs[i];
139 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
143 fibptr->hw_fib_va = hw_fib;
144 fibptr->data = (void *) fibptr->hw_fib_va->data;
145 fibptr->next = fibptr+1; /* Forward chain the fibs */
146 sema_init(&fibptr->event_wait, 0);
147 spin_lock_init(&fibptr->event_lock);
148 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
149 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
150 fibptr->hw_fib_pa = hw_fib_pa;
151 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
152 dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
153 hw_fib_pa = hw_fib_pa +
154 dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
157 * Add the fib chain to the free list
159 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
161 * Enable this to debug out of queue space
163 dev->free_fib = &dev->fibs[0];
168 * aac_fib_alloc - allocate a fib
169 * @dev: Adapter to allocate the fib for
171 * Allocate a fib from the adapter fib pool. If the pool is empty we
175 struct fib *aac_fib_alloc(struct aac_dev *dev)
179 spin_lock_irqsave(&dev->fib_lock, flags);
180 fibptr = dev->free_fib;
182 spin_unlock_irqrestore(&dev->fib_lock, flags);
185 dev->free_fib = fibptr->next;
186 spin_unlock_irqrestore(&dev->fib_lock, flags);
188 * Set the proper node type code and node byte size
190 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
191 fibptr->size = sizeof(struct fib);
193 * Null out fields that depend on being zero at the start of
196 fibptr->hw_fib_va->header.XferState = 0;
198 fibptr->callback = NULL;
199 fibptr->callback_data = NULL;
205 * aac_fib_free - free a fib
206 * @fibptr: fib to free up
208 * Frees up a fib and places it on the appropriate queue
211 void aac_fib_free(struct fib *fibptr)
213 unsigned long flags, flagsv;
215 spin_lock_irqsave(&fibptr->event_lock, flagsv);
216 if (fibptr->done == 2) {
217 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
220 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
222 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
223 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
224 aac_config.fib_timeouts++;
225 if (fibptr->hw_fib_va->header.XferState != 0) {
226 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
228 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
230 fibptr->next = fibptr->dev->free_fib;
231 fibptr->dev->free_fib = fibptr;
232 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
236 * aac_fib_init - initialise a fib
237 * @fibptr: The fib to initialize
239 * Set up the generic fib fields ready for use
242 void aac_fib_init(struct fib *fibptr)
244 struct hw_fib *hw_fib = fibptr->hw_fib_va;
246 hw_fib->header.StructType = FIB_MAGIC;
247 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
248 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
249 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
250 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
251 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
255 * fib_deallocate - deallocate a fib
256 * @fibptr: fib to deallocate
258 * Will deallocate and return to the free pool the FIB pointed to by the
262 static void fib_dealloc(struct fib * fibptr)
264 struct hw_fib *hw_fib = fibptr->hw_fib_va;
265 BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
266 hw_fib->header.XferState = 0;
270 * Commuication primitives define and support the queuing method we use to
271 * support host to adapter commuication. All queue accesses happen through
272 * these routines and are the only routines which have a knowledge of the
273 * how these queues are implemented.
277 * aac_get_entry - get a queue entry
280 * @entry: Entry return
281 * @index: Index return
282 * @nonotify: notification control
284 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
285 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
289 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
291 struct aac_queue * q;
295 * All of the queues wrap when they reach the end, so we check
296 * to see if they have reached the end and if they have we just
297 * set the index back to zero. This is a wrap. You could or off
298 * the high bits in all updates but this is a bit faster I think.
301 q = &dev->queues->queue[qid];
303 idx = *index = le32_to_cpu(*(q->headers.producer));
304 /* Interrupt Moderation, only interrupt for first two entries */
305 if (idx != le32_to_cpu(*(q->headers.consumer))) {
307 if (qid == AdapNormCmdQueue)
308 idx = ADAP_NORM_CMD_ENTRIES;
310 idx = ADAP_NORM_RESP_ENTRIES;
312 if (idx != le32_to_cpu(*(q->headers.consumer)))
316 if (qid == AdapNormCmdQueue) {
317 if (*index >= ADAP_NORM_CMD_ENTRIES)
318 *index = 0; /* Wrap to front of the Producer Queue. */
320 if (*index >= ADAP_NORM_RESP_ENTRIES)
321 *index = 0; /* Wrap to front of the Producer Queue. */
325 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
326 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
330 *entry = q->base + *index;
336 * aac_queue_get - get the next free QE
338 * @index: Returned index
339 * @priority: Priority of fib
340 * @fib: Fib to associate with the queue entry
341 * @wait: Wait if queue full
342 * @fibptr: Driver fib object to go with fib
343 * @nonotify: Don't notify the adapter
345 * Gets the next free QE off the requested priorty adapter command
346 * queue and associates the Fib with the QE. The QE represented by
347 * index is ready to insert on the queue when this routine returns
351 int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
353 struct aac_entry * entry = NULL;
356 if (qid == AdapNormCmdQueue) {
357 /* if no entries wait for some if caller wants to */
358 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
359 printk(KERN_ERR "GetEntries failed\n");
362 * Setup queue entry with a command, status and fib mapped
364 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
367 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
368 /* if no entries wait for some if caller wants to */
371 * Setup queue entry with command, status and fib mapped
373 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
374 entry->addr = hw_fib->header.SenderFibAddress;
375 /* Restore adapters pointer to the FIB */
376 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
380 * If MapFib is true than we need to map the Fib and put pointers
381 * in the queue entry.
384 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
389 * Define the highest level of host to adapter communication routines.
390 * These routines will support host to adapter FS commuication. These
391 * routines have no knowledge of the commuication method used. This level
392 * sends and receives FIBs. This level has no knowledge of how these FIBs
393 * get passed back and forth.
397 * aac_fib_send - send a fib to the adapter
398 * @command: Command to send
400 * @size: Size of fib data area
401 * @priority: Priority of Fib
402 * @wait: Async/sync select
403 * @reply: True if a reply is wanted
404 * @callback: Called with reply
405 * @callback_data: Passed to callback
407 * Sends the requested FIB to the adapter and optionally will wait for a
408 * response FIB. If the caller does not wish to wait for a response than
409 * an event to wait on must be supplied. This event will be set when a
410 * response FIB is received from the adapter.
413 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
414 int priority, int wait, int reply, fib_callback callback,
417 struct aac_dev * dev = fibptr->dev;
418 struct hw_fib * hw_fib = fibptr->hw_fib_va;
419 unsigned long flags = 0;
420 unsigned long qflags;
421 unsigned long mflags = 0;
424 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
427 * There are 5 cases with the wait and response requested flags.
428 * The only invalid cases are if the caller requests to wait and
429 * does not request a response and if the caller does not want a
430 * response and the Fib is not allocated from pool. If a response
431 * is not requesed the Fib will just be deallocaed by the DPC
432 * routine when the response comes back from the adapter. No
433 * further processing will be done besides deleting the Fib. We
434 * will have a debug mode where the adapter can notify the host
435 * it had a problem and the host can log that fact.
438 if (wait && !reply) {
440 } else if (!wait && reply) {
441 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
442 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
443 } else if (!wait && !reply) {
444 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
445 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
446 } else if (wait && reply) {
447 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
448 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
451 * Map the fib into 32bits by using the fib number
454 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
455 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
457 * Set FIB state to indicate where it came from and if we want a
458 * response from the adapter. Also load the command from the
461 * Map the hw fib pointer as a 32bit value
463 hw_fib->header.Command = cpu_to_le16(command);
464 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
465 fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
467 * Set the size of the Fib we want to send to the adapter
469 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
470 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
474 * Get a queue entry connect the FIB to it and send an notify
475 * the adapter a command is ready.
477 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
480 * Fill in the Callback and CallbackContext if we are not
484 fibptr->callback = callback;
485 fibptr->callback_data = callback_data;
486 fibptr->flags = FIB_CONTEXT_FLAG;
491 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
493 dprintk((KERN_DEBUG "Fib contents:.\n"));
494 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
495 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
496 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
497 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
498 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
499 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
506 spin_lock_irqsave(&dev->manage_lock, mflags);
507 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
508 printk(KERN_INFO "No management Fibs Available:%d\n",
509 dev->management_fib_count);
510 spin_unlock_irqrestore(&dev->manage_lock, mflags);
513 dev->management_fib_count++;
514 spin_unlock_irqrestore(&dev->manage_lock, mflags);
515 spin_lock_irqsave(&fibptr->event_lock, flags);
518 if (aac_adapter_deliver(fibptr) != 0) {
519 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
521 spin_unlock_irqrestore(&fibptr->event_lock, flags);
522 spin_lock_irqsave(&dev->manage_lock, mflags);
523 dev->management_fib_count--;
524 spin_unlock_irqrestore(&dev->manage_lock, mflags);
531 * If the caller wanted us to wait for response wait now.
535 spin_unlock_irqrestore(&fibptr->event_lock, flags);
536 /* Only set for first known interruptable command */
539 * *VERY* Dangerous to time out a command, the
540 * assumption is made that we have no hope of
541 * functioning because an interrupt routing or other
542 * hardware failure has occurred.
544 unsigned long count = 36000000L; /* 3 minutes */
545 while (down_trylock(&fibptr->event_wait)) {
548 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
549 spin_lock_irqsave(q->lock, qflags);
551 spin_unlock_irqrestore(q->lock, qflags);
553 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
554 "Usually a result of a PCI interrupt routing problem;\n"
555 "update mother board BIOS or consider utilizing one of\n"
556 "the SAFE mode kernel options (acpi, apic etc)\n");
560 if ((blink = aac_adapter_check_health(dev)) > 0) {
562 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
563 "Usually a result of a serious unrecoverable hardware problem\n",
570 } else if (down_interruptible(&fibptr->event_wait)) {
571 /* Do nothing ... satisfy
572 * down_interruptible must_check */
575 spin_lock_irqsave(&fibptr->event_lock, flags);
576 if (fibptr->done == 0) {
577 fibptr->done = 2; /* Tell interrupt we aborted */
578 spin_unlock_irqrestore(&fibptr->event_lock, flags);
581 spin_unlock_irqrestore(&fibptr->event_lock, flags);
582 BUG_ON(fibptr->done == 0);
584 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
589 * If the user does not want a response than return success otherwise
599 * aac_consumer_get - get the top of the queue
602 * @entry: Return entry
604 * Will return a pointer to the entry on the top of the queue requested that
605 * we are a consumer of, and return the address of the queue entry. It does
606 * not change the state of the queue.
609 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
613 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
617 * The consumer index must be wrapped if we have reached
618 * the end of the queue, else we just use the entry
619 * pointed to by the header index
621 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
624 index = le32_to_cpu(*q->headers.consumer);
625 *entry = q->base + index;
632 * aac_consumer_free - free consumer entry
637 * Frees up the current top of the queue we are a consumer of. If the
638 * queue was full notify the producer that the queue is no longer full.
641 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
646 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
649 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
650 *q->headers.consumer = cpu_to_le32(1);
652 le32_add_cpu(q->headers.consumer, 1);
657 case HostNormCmdQueue:
658 notify = HostNormCmdNotFull;
660 case HostNormRespQueue:
661 notify = HostNormRespNotFull;
667 aac_adapter_notify(dev, notify);
672 * aac_fib_adapter_complete - complete adapter issued fib
673 * @fibptr: fib to complete
676 * Will do all necessary work to complete a FIB that was sent from
680 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
682 struct hw_fib * hw_fib = fibptr->hw_fib_va;
683 struct aac_dev * dev = fibptr->dev;
684 struct aac_queue * q;
685 unsigned long nointr = 0;
686 unsigned long qflags;
688 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
693 if (hw_fib->header.XferState == 0) {
694 if (dev->comm_interface == AAC_COMM_MESSAGE)
699 * If we plan to do anything check the structure type first.
701 if (hw_fib->header.StructType != FIB_MAGIC) {
702 if (dev->comm_interface == AAC_COMM_MESSAGE)
707 * This block handles the case where the adapter had sent us a
708 * command and we have finished processing the command. We
709 * call completeFib when we are done processing the command
710 * and want to send a response back to the adapter. This will
711 * send the completed cdb to the adapter.
713 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
714 if (dev->comm_interface == AAC_COMM_MESSAGE) {
718 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
720 size += sizeof(struct aac_fibhdr);
721 if (size > le16_to_cpu(hw_fib->header.SenderSize))
723 hw_fib->header.Size = cpu_to_le16(size);
725 q = &dev->queues->queue[AdapNormRespQueue];
726 spin_lock_irqsave(q->lock, qflags);
727 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
728 *(q->headers.producer) = cpu_to_le32(index + 1);
729 spin_unlock_irqrestore(q->lock, qflags);
730 if (!(nointr & (int)aac_config.irq_mod))
731 aac_adapter_notify(dev, AdapNormRespQueue);
734 printk(KERN_WARNING "aac_fib_adapter_complete: "
735 "Unknown xferstate detected.\n");
742 * aac_fib_complete - fib completion handler
743 * @fib: FIB to complete
745 * Will do all necessary work to complete a FIB.
748 int aac_fib_complete(struct fib *fibptr)
751 struct hw_fib * hw_fib = fibptr->hw_fib_va;
754 * Check for a fib which has already been completed
757 if (hw_fib->header.XferState == 0)
760 * If we plan to do anything check the structure type first.
763 if (hw_fib->header.StructType != FIB_MAGIC)
766 * This block completes a cdb which orginated on the host and we
767 * just need to deallocate the cdb or reinit it. At this point the
768 * command is complete that we had sent to the adapter and this
769 * cdb could be reused.
771 spin_lock_irqsave(&fibptr->event_lock, flags);
772 if (fibptr->done == 2) {
773 spin_unlock_irqrestore(&fibptr->event_lock, flags);
776 spin_unlock_irqrestore(&fibptr->event_lock, flags);
778 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
779 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
783 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
786 * This handles the case when the host has aborted the I/O
787 * to the adapter because the adapter is not responding
790 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
799 * aac_printf - handle printf from firmware
803 * Print a message passed to us by the controller firmware on the
807 void aac_printf(struct aac_dev *dev, u32 val)
809 char *cp = dev->printfbuf;
810 if (dev->printf_enabled)
812 int length = val & 0xffff;
813 int level = (val >> 16) & 0xffff;
816 * The size of the printfbuf is set in port.c
817 * There is no variable or define for it
823 if (level == LOG_AAC_HIGH_ERROR)
824 printk(KERN_WARNING "%s:%s", dev->name, cp);
826 printk(KERN_INFO "%s:%s", dev->name, cp);
833 * aac_handle_aif - Handle a message from the firmware
834 * @dev: Which adapter this fib is from
835 * @fibptr: Pointer to fibptr from adapter
837 * This routine handles a driver notify fib from the adapter and
838 * dispatches it to the appropriate routine for handling.
841 #define AIF_SNIFF_TIMEOUT (30*HZ)
842 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
844 struct hw_fib * hw_fib = fibptr->hw_fib_va;
845 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
846 u32 channel, id, lun, container;
847 struct scsi_device *device;
853 } device_config_needed = NOTHING;
855 /* Sniff for container changes */
857 if (!dev || !dev->fsa_dev)
859 container = channel = id = lun = (u32)-1;
862 * We have set this up to try and minimize the number of
863 * re-configures that take place. As a result of this when
864 * certain AIF's come in we will set a flag waiting for another
865 * type of AIF before setting the re-config flag.
867 switch (le32_to_cpu(aifcmd->command)) {
868 case AifCmdDriverNotify:
869 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
871 * Morph or Expand complete
873 case AifDenMorphComplete:
874 case AifDenVolumeExtendComplete:
875 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
876 if (container >= dev->maximum_num_containers)
880 * Find the scsi_device associated with the SCSI
881 * address. Make sure we have the right array, and if
882 * so set the flag to initiate a new re-config once we
883 * see an AifEnConfigChange AIF come through.
886 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
887 device = scsi_device_lookup(dev->scsi_host_ptr,
888 CONTAINER_TO_CHANNEL(container),
889 CONTAINER_TO_ID(container),
890 CONTAINER_TO_LUN(container));
892 dev->fsa_dev[container].config_needed = CHANGE;
893 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
894 dev->fsa_dev[container].config_waiting_stamp = jiffies;
895 scsi_device_put(device);
901 * If we are waiting on something and this happens to be
902 * that thing then set the re-configure flag.
904 if (container != (u32)-1) {
905 if (container >= dev->maximum_num_containers)
907 if ((dev->fsa_dev[container].config_waiting_on ==
908 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
909 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
910 dev->fsa_dev[container].config_waiting_on = 0;
911 } else for (container = 0;
912 container < dev->maximum_num_containers; ++container) {
913 if ((dev->fsa_dev[container].config_waiting_on ==
914 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
915 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
916 dev->fsa_dev[container].config_waiting_on = 0;
920 case AifCmdEventNotify:
921 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
922 case AifEnBatteryEvent:
923 dev->cache_protected =
924 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
929 case AifEnAddContainer:
930 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
931 if (container >= dev->maximum_num_containers)
933 dev->fsa_dev[container].config_needed = ADD;
934 dev->fsa_dev[container].config_waiting_on =
936 dev->fsa_dev[container].config_waiting_stamp = jiffies;
942 case AifEnDeleteContainer:
943 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
944 if (container >= dev->maximum_num_containers)
946 dev->fsa_dev[container].config_needed = DELETE;
947 dev->fsa_dev[container].config_waiting_on =
949 dev->fsa_dev[container].config_waiting_stamp = jiffies;
953 * Container change detected. If we currently are not
954 * waiting on something else, setup to wait on a Config Change.
956 case AifEnContainerChange:
957 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
958 if (container >= dev->maximum_num_containers)
960 if (dev->fsa_dev[container].config_waiting_on &&
961 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
963 dev->fsa_dev[container].config_needed = CHANGE;
964 dev->fsa_dev[container].config_waiting_on =
966 dev->fsa_dev[container].config_waiting_stamp = jiffies;
969 case AifEnConfigChange:
973 case AifEnDeleteJBOD:
974 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
975 if ((container >> 28)) {
979 channel = (container >> 24) & 0xF;
980 if (channel >= dev->maximum_num_channels) {
984 id = container & 0xFFFF;
985 if (id >= dev->maximum_num_physicals) {
989 lun = (container >> 16) & 0xFF;
991 channel = aac_phys_to_logical(channel);
992 device_config_needed =
993 (((__le32 *)aifcmd->data)[0] ==
994 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
995 if (device_config_needed == ADD) {
996 device = scsi_device_lookup(dev->scsi_host_ptr,
1001 scsi_remove_device(device);
1002 scsi_device_put(device);
1007 case AifEnEnclosureManagement:
1009 * If in JBOD mode, automatic exposure of new
1010 * physical target to be suppressed until configured.
1014 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1015 case EM_DRIVE_INSERTION:
1016 case EM_DRIVE_REMOVAL:
1017 container = le32_to_cpu(
1018 ((__le32 *)aifcmd->data)[2]);
1019 if ((container >> 28)) {
1020 container = (u32)-1;
1023 channel = (container >> 24) & 0xF;
1024 if (channel >= dev->maximum_num_channels) {
1025 container = (u32)-1;
1028 id = container & 0xFFFF;
1029 lun = (container >> 16) & 0xFF;
1030 container = (u32)-1;
1031 if (id >= dev->maximum_num_physicals) {
1032 /* legacy dev_t ? */
1033 if ((0x2000 <= id) || lun || channel ||
1034 ((channel = (id >> 7) & 0x3F) >=
1035 dev->maximum_num_channels))
1037 lun = (id >> 4) & 7;
1040 channel = aac_phys_to_logical(channel);
1041 device_config_needed =
1042 (((__le32 *)aifcmd->data)[3]
1043 == cpu_to_le32(EM_DRIVE_INSERTION)) ?
1051 * If we are waiting on something and this happens to be
1052 * that thing then set the re-configure flag.
1054 if (container != (u32)-1) {
1055 if (container >= dev->maximum_num_containers)
1057 if ((dev->fsa_dev[container].config_waiting_on ==
1058 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1059 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1060 dev->fsa_dev[container].config_waiting_on = 0;
1061 } else for (container = 0;
1062 container < dev->maximum_num_containers; ++container) {
1063 if ((dev->fsa_dev[container].config_waiting_on ==
1064 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1065 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1066 dev->fsa_dev[container].config_waiting_on = 0;
1070 case AifCmdJobProgress:
1072 * These are job progress AIF's. When a Clear is being
1073 * done on a container it is initially created then hidden from
1074 * the OS. When the clear completes we don't get a config
1075 * change so we monitor the job status complete on a clear then
1076 * wait for a container change.
1079 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1080 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1081 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1083 container < dev->maximum_num_containers;
1086 * Stomp on all config sequencing for all
1089 dev->fsa_dev[container].config_waiting_on =
1090 AifEnContainerChange;
1091 dev->fsa_dev[container].config_needed = ADD;
1092 dev->fsa_dev[container].config_waiting_stamp =
1096 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1097 ((__le32 *)aifcmd->data)[6] == 0 &&
1098 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1100 container < dev->maximum_num_containers;
1103 * Stomp on all config sequencing for all
1106 dev->fsa_dev[container].config_waiting_on =
1107 AifEnContainerChange;
1108 dev->fsa_dev[container].config_needed = DELETE;
1109 dev->fsa_dev[container].config_waiting_stamp =
1118 if (device_config_needed == NOTHING)
1119 for (; container < dev->maximum_num_containers; ++container) {
1120 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1121 (dev->fsa_dev[container].config_needed != NOTHING) &&
1122 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1123 device_config_needed =
1124 dev->fsa_dev[container].config_needed;
1125 dev->fsa_dev[container].config_needed = NOTHING;
1126 channel = CONTAINER_TO_CHANNEL(container);
1127 id = CONTAINER_TO_ID(container);
1128 lun = CONTAINER_TO_LUN(container);
1132 if (device_config_needed == NOTHING)
1136 * If we decided that a re-configuration needs to be done,
1137 * schedule it here on the way out the door, please close the door
1142 * Find the scsi_device associated with the SCSI address,
1143 * and mark it as changed, invalidating the cache. This deals
1144 * with changes to existing device IDs.
1147 if (!dev || !dev->scsi_host_ptr)
1150 * force reload of disk info via aac_probe_container
1152 if ((channel == CONTAINER_CHANNEL) &&
1153 (device_config_needed != NOTHING)) {
1154 if (dev->fsa_dev[container].valid == 1)
1155 dev->fsa_dev[container].valid = 2;
1156 aac_probe_container(dev, container);
1158 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1160 switch (device_config_needed) {
1162 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1163 scsi_remove_device(device);
1165 if (scsi_device_online(device)) {
1166 scsi_device_set_state(device, SDEV_OFFLINE);
1167 sdev_printk(KERN_INFO, device,
1168 "Device offlined - %s\n",
1169 (channel == CONTAINER_CHANNEL) ?
1171 "enclosure services event");
1176 if (!scsi_device_online(device)) {
1177 sdev_printk(KERN_INFO, device,
1178 "Device online - %s\n",
1179 (channel == CONTAINER_CHANNEL) ?
1181 "enclosure services event");
1182 scsi_device_set_state(device, SDEV_RUNNING);
1186 if ((channel == CONTAINER_CHANNEL)
1187 && (!dev->fsa_dev[container].valid)) {
1188 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1189 scsi_remove_device(device);
1191 if (!scsi_device_online(device))
1193 scsi_device_set_state(device, SDEV_OFFLINE);
1194 sdev_printk(KERN_INFO, device,
1195 "Device offlined - %s\n",
1200 scsi_rescan_device(&device->sdev_gendev);
1205 scsi_device_put(device);
1206 device_config_needed = NOTHING;
1208 if (device_config_needed == ADD)
1209 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1210 if (channel == CONTAINER_CHANNEL) {
1212 device_config_needed = NOTHING;
1217 static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1221 struct Scsi_Host *host;
1222 struct scsi_device *dev;
1223 struct scsi_cmnd *command;
1224 struct scsi_cmnd *command_list;
1229 * - host is locked, unless called by the aacraid thread.
1230 * (a matter of convenience, due to legacy issues surrounding
1231 * eh_host_adapter_reset).
1232 * - in_reset is asserted, so no new i/o is getting to the
1234 * - The card is dead, or will be very shortly ;-/ so no new
1235 * commands are completing in the interrupt service.
1237 host = aac->scsi_host_ptr;
1238 scsi_block_requests(host);
1239 aac_adapter_disable_int(aac);
1240 if (aac->thread->pid != current->pid) {
1241 spin_unlock_irq(host->host_lock);
1242 kthread_stop(aac->thread);
1247 * If a positive health, means in a known DEAD PANIC
1248 * state and the adapter could be reset to `try again'.
1250 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1256 * Loop through the fibs, close the synchronous FIBS
1258 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1259 struct fib *fib = &aac->fibs[index];
1260 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1261 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1262 unsigned long flagv;
1263 spin_lock_irqsave(&fib->event_lock, flagv);
1264 up(&fib->event_wait);
1265 spin_unlock_irqrestore(&fib->event_lock, flagv);
1270 /* Give some extra time for ioctls to complete. */
1273 index = aac->cardtype;
1276 * Re-initialize the adapter, first free resources, then carefully
1277 * apply the initialization sequence to come back again. Only risk
1278 * is a change in Firmware dropping cache, it is assumed the caller
1279 * will ensure that i/o is queisced and the card is flushed in that
1282 aac_fib_map_free(aac);
1283 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1284 aac->comm_addr = NULL;
1288 free_irq(aac->pdev->irq, aac);
1290 pci_disable_msi(aac->pdev);
1291 kfree(aac->fsa_dev);
1292 aac->fsa_dev = NULL;
1293 quirks = aac_get_driver_ident(index)->quirks;
1294 if (quirks & AAC_QUIRK_31BIT) {
1295 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1296 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1299 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1300 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1303 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1305 if (quirks & AAC_QUIRK_31BIT)
1306 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1309 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1310 if (IS_ERR(aac->thread)) {
1311 retval = PTR_ERR(aac->thread);
1315 (void)aac_get_adapter_info(aac);
1316 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1317 host->sg_tablesize = 34;
1318 host->max_sectors = (host->sg_tablesize * 8) + 112;
1320 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1321 host->sg_tablesize = 17;
1322 host->max_sectors = (host->sg_tablesize * 8) + 112;
1324 aac_get_config_status(aac, 1);
1325 aac_get_containers(aac);
1327 * This is where the assumption that the Adapter is quiesced
1330 command_list = NULL;
1331 __shost_for_each_device(dev, host) {
1332 unsigned long flags;
1333 spin_lock_irqsave(&dev->list_lock, flags);
1334 list_for_each_entry(command, &dev->cmd_list, list)
1335 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1336 command->SCp.buffer = (struct scatterlist *)command_list;
1337 command_list = command;
1339 spin_unlock_irqrestore(&dev->list_lock, flags);
1341 while ((command = command_list)) {
1342 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1343 command->SCp.buffer = NULL;
1344 command->result = DID_OK << 16
1345 | COMMAND_COMPLETE << 8
1346 | SAM_STAT_TASK_SET_FULL;
1347 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1348 command->scsi_done(command);
1354 scsi_unblock_requests(host);
1356 spin_lock_irq(host->host_lock);
1361 int aac_reset_adapter(struct aac_dev * aac, int forced)
1363 unsigned long flagv = 0;
1365 struct Scsi_Host * host;
1367 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1370 if (aac->in_reset) {
1371 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1375 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1378 * Wait for all commands to complete to this specific
1379 * target (block maximum 60 seconds). Although not necessary,
1380 * it does make us a good storage citizen.
1382 host = aac->scsi_host_ptr;
1383 scsi_block_requests(host);
1384 if (forced < 2) for (retval = 60; retval; --retval) {
1385 struct scsi_device * dev;
1386 struct scsi_cmnd * command;
1389 __shost_for_each_device(dev, host) {
1390 spin_lock_irqsave(&dev->list_lock, flagv);
1391 list_for_each_entry(command, &dev->cmd_list, list) {
1392 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1397 spin_unlock_irqrestore(&dev->list_lock, flagv);
1403 * We can exit If all the commands are complete
1410 /* Quiesce build, flush cache, write through mode */
1412 aac_send_shutdown(aac);
1413 spin_lock_irqsave(host->host_lock, flagv);
1414 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1415 spin_unlock_irqrestore(host->host_lock, flagv);
1417 if ((forced < 2) && (retval == -ENODEV)) {
1418 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1419 struct fib * fibctx = aac_fib_alloc(aac);
1421 struct aac_pause *cmd;
1424 aac_fib_init(fibctx);
1426 cmd = (struct aac_pause *) fib_data(fibctx);
1428 cmd->command = cpu_to_le32(VM_ContainerConfig);
1429 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1430 cmd->timeout = cpu_to_le32(1);
1431 cmd->min = cpu_to_le32(1);
1432 cmd->noRescan = cpu_to_le32(1);
1433 cmd->count = cpu_to_le32(0);
1435 status = aac_fib_send(ContainerCommand,
1437 sizeof(struct aac_pause),
1439 -2 /* Timeout silently */, 1,
1443 aac_fib_complete(fibctx);
1444 /* FIB should be freed only after getting
1445 * the response from the F/W */
1446 if (status != -ERESTARTSYS)
1447 aac_fib_free(fibctx);
1454 int aac_check_health(struct aac_dev * aac)
1457 unsigned long time_now, flagv = 0;
1458 struct list_head * entry;
1459 struct Scsi_Host * host;
1461 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1462 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1465 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1466 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1473 * aac_aifcmd.command = AifCmdEventNotify = 1
1474 * aac_aifcmd.seqnum = 0xFFFFFFFF
1475 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1476 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1477 * aac.aifcmd.data[2] = AifHighPriority = 3
1478 * aac.aifcmd.data[3] = BlinkLED
1481 time_now = jiffies/HZ;
1482 entry = aac->fib_list.next;
1485 * For each Context that is on the
1486 * fibctxList, make a copy of the
1487 * fib, and then set the event to wake up the
1488 * thread that is waiting for it.
1490 while (entry != &aac->fib_list) {
1492 * Extract the fibctx
1494 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1495 struct hw_fib * hw_fib;
1498 * Check if the queue is getting
1501 if (fibctx->count > 20) {
1503 * It's *not* jiffies folks,
1504 * but jiffies / HZ, so do not
1507 u32 time_last = fibctx->jiffies;
1509 * Has it been > 2 minutes
1510 * since the last read off
1513 if ((time_now - time_last) > aif_timeout) {
1514 entry = entry->next;
1515 aac_close_fib_context(aac, fibctx);
1520 * Warning: no sleep allowed while
1523 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1524 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1525 if (fib && hw_fib) {
1526 struct aac_aifcmd * aif;
1528 fib->hw_fib_va = hw_fib;
1531 fib->type = FSAFS_NTC_FIB_CONTEXT;
1532 fib->size = sizeof (struct fib);
1533 fib->data = hw_fib->data;
1534 aif = (struct aac_aifcmd *)hw_fib->data;
1535 aif->command = cpu_to_le32(AifCmdEventNotify);
1536 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1537 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1538 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1539 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1540 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1543 * Put the FIB onto the
1546 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1549 * Set the event to wake up the
1550 * thread that will waiting.
1552 up(&fibctx->wait_sem);
1554 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1558 entry = entry->next;
1561 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1564 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1568 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1570 if (!aac_check_reset || ((aac_check_reset == 1) &&
1571 (aac->supplement_adapter_info.SupportedOptions2 &
1572 AAC_OPTION_IGNORE_RESET)))
1574 host = aac->scsi_host_ptr;
1575 if (aac->thread->pid != current->pid)
1576 spin_lock_irqsave(host->host_lock, flagv);
1577 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1578 if (aac->thread->pid != current->pid)
1579 spin_unlock_irqrestore(host->host_lock, flagv);
1589 * aac_command_thread - command processing thread
1590 * @dev: Adapter to monitor
1592 * Waits on the commandready event in it's queue. When the event gets set
1593 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1594 * until the queue is empty. When the queue is empty it will wait for
1598 int aac_command_thread(void *data)
1600 struct aac_dev *dev = data;
1601 struct hw_fib *hw_fib, *hw_newfib;
1602 struct fib *fib, *newfib;
1603 struct aac_fib_context *fibctx;
1604 unsigned long flags;
1605 DECLARE_WAITQUEUE(wait, current);
1606 unsigned long next_jiffies = jiffies + HZ;
1607 unsigned long next_check_jiffies = next_jiffies;
1608 long difference = HZ;
1611 * We can only have one thread per adapter for AIF's.
1613 if (dev->aif_thread)
1617 * Let the DPC know it has a place to send the AIF's to.
1619 dev->aif_thread = 1;
1620 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1621 set_current_state(TASK_INTERRUPTIBLE);
1622 dprintk ((KERN_INFO "aac_command_thread start\n"));
1624 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1625 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1626 struct list_head *entry;
1627 struct aac_aifcmd * aifcmd;
1629 set_current_state(TASK_RUNNING);
1631 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1634 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1635 fib = list_entry(entry, struct fib, fiblink);
1637 * We will process the FIB here or pass it to a
1638 * worker thread that is TBD. We Really can't
1639 * do anything at this point since we don't have
1640 * anything defined for this thread to do.
1642 hw_fib = fib->hw_fib_va;
1643 memset(fib, 0, sizeof(struct fib));
1644 fib->type = FSAFS_NTC_FIB_CONTEXT;
1645 fib->size = sizeof(struct fib);
1646 fib->hw_fib_va = hw_fib;
1647 fib->data = hw_fib->data;
1650 * We only handle AifRequest fibs from the adapter.
1652 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1653 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1654 /* Handle Driver Notify Events */
1655 aac_handle_aif(dev, fib);
1656 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1657 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1659 /* The u32 here is important and intended. We are using
1660 32bit wrapping time to fit the adapter field */
1662 u32 time_now, time_last;
1663 unsigned long flagv;
1665 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1666 struct fib ** fib_pool, ** fib_p;
1669 if ((aifcmd->command ==
1670 cpu_to_le32(AifCmdEventNotify)) ||
1672 cpu_to_le32(AifCmdJobProgress))) {
1673 aac_handle_aif(dev, fib);
1676 time_now = jiffies/HZ;
1679 * Warning: no sleep allowed while
1680 * holding spinlock. We take the estimate
1681 * and pre-allocate a set of fibs outside the
1684 num = le32_to_cpu(dev->init->AdapterFibsSize)
1685 / sizeof(struct hw_fib); /* some extra */
1686 spin_lock_irqsave(&dev->fib_lock, flagv);
1687 entry = dev->fib_list.next;
1688 while (entry != &dev->fib_list) {
1689 entry = entry->next;
1692 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1696 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1697 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1698 hw_fib_p = hw_fib_pool;
1700 while (hw_fib_p < &hw_fib_pool[num]) {
1701 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1705 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1706 kfree(*(--hw_fib_p));
1710 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1720 spin_lock_irqsave(&dev->fib_lock, flagv);
1721 entry = dev->fib_list.next;
1723 * For each Context that is on the
1724 * fibctxList, make a copy of the
1725 * fib, and then set the event to wake up the
1726 * thread that is waiting for it.
1728 hw_fib_p = hw_fib_pool;
1730 while (entry != &dev->fib_list) {
1732 * Extract the fibctx
1734 fibctx = list_entry(entry, struct aac_fib_context, next);
1736 * Check if the queue is getting
1739 if (fibctx->count > 20)
1742 * It's *not* jiffies folks,
1743 * but jiffies / HZ so do not
1746 time_last = fibctx->jiffies;
1748 * Has it been > 2 minutes
1749 * since the last read off
1752 if ((time_now - time_last) > aif_timeout) {
1753 entry = entry->next;
1754 aac_close_fib_context(dev, fibctx);
1759 * Warning: no sleep allowed while
1762 if (hw_fib_p < &hw_fib_pool[num]) {
1763 hw_newfib = *hw_fib_p;
1764 *(hw_fib_p++) = NULL;
1768 * Make the copy of the FIB
1770 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1771 memcpy(newfib, fib, sizeof(struct fib));
1772 newfib->hw_fib_va = hw_newfib;
1774 * Put the FIB onto the
1777 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1780 * Set the event to wake up the
1781 * thread that is waiting.
1783 up(&fibctx->wait_sem);
1785 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1787 entry = entry->next;
1790 * Set the status of this FIB
1792 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1793 aac_fib_adapter_complete(fib, sizeof(u32));
1794 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1795 /* Free up the remaining resources */
1796 hw_fib_p = hw_fib_pool;
1798 while (hw_fib_p < &hw_fib_pool[num]) {
1808 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1811 * There are no more AIF's
1813 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1816 * Background activity
1818 if ((time_before(next_check_jiffies,next_jiffies))
1819 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1820 next_check_jiffies = next_jiffies;
1821 if (aac_check_health(dev) == 0) {
1822 difference = ((long)(unsigned)check_interval)
1824 next_check_jiffies = jiffies + difference;
1825 } else if (!dev->queues)
1828 if (!time_before(next_check_jiffies,next_jiffies)
1829 && ((difference = next_jiffies - jiffies) <= 0)) {
1833 /* Don't even try to talk to adapter if its sick */
1834 ret = aac_check_health(dev);
1835 if (!ret && !dev->queues)
1837 next_check_jiffies = jiffies
1838 + ((long)(unsigned)check_interval)
1840 do_gettimeofday(&now);
1842 /* Synchronize our watches */
1843 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1844 && (now.tv_usec > (1000000 / HZ)))
1845 difference = (((1000000 - now.tv_usec) * HZ)
1846 + 500000) / 1000000;
1847 else if (ret == 0) {
1850 if ((fibptr = aac_fib_alloc(dev))) {
1854 aac_fib_init(fibptr);
1856 info = (__le32 *) fib_data(fibptr);
1857 if (now.tv_usec > 500000)
1860 *info = cpu_to_le32(now.tv_sec);
1862 status = aac_fib_send(SendHostTime,
1869 /* Do not set XferState to zero unless
1870 * receives a response from F/W */
1872 aac_fib_complete(fibptr);
1873 /* FIB should be freed only after
1874 * getting the response from the F/W */
1875 if (status != -ERESTARTSYS)
1876 aac_fib_free(fibptr);
1878 difference = (long)(unsigned)update_interval*HZ;
1881 difference = 10 * HZ;
1883 next_jiffies = jiffies + difference;
1884 if (time_before(next_check_jiffies,next_jiffies))
1885 difference = next_check_jiffies - jiffies;
1887 if (difference <= 0)
1889 set_current_state(TASK_INTERRUPTIBLE);
1891 if (kthread_should_stop())
1894 schedule_timeout(difference);
1896 if (kthread_should_stop())
1900 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1901 dev->aif_thread = 0;