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)
90 if(!dev->hw_fib_va || !dev->max_fib_size)
93 num_fibs = dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
94 fib_size = dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
95 alloc_size = fib_size * num_fibs + ALIGN32 - 1;
97 pci_free_consistent(dev->pdev, alloc_size, dev->hw_fib_va,
100 dev->hw_fib_va = NULL;
105 * aac_fib_setup - setup the fibs
106 * @dev: Adapter to set up
108 * Allocate the PCI space for the fibs, map it and then initialise the
109 * fib area, the unmapped fib data and also the free list
112 int aac_fib_setup(struct aac_dev * dev)
115 struct hw_fib *hw_fib;
116 dma_addr_t hw_fib_pa;
119 while (((i = fib_map_alloc(dev)) == -ENOMEM)
120 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
121 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
122 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
127 memset(dev->hw_fib_va, 0,
128 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
129 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
131 /* 32 byte alignment for PMC */
132 hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
133 hw_fib = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
134 (hw_fib_pa - dev->hw_fib_pa));
136 /* add Xport header */
137 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
138 sizeof(struct aac_fib_xporthdr));
139 hw_fib_pa += sizeof(struct aac_fib_xporthdr);
142 * Initialise the fibs
144 for (i = 0, fibptr = &dev->fibs[i];
145 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
149 fibptr->hw_fib_va = hw_fib;
150 fibptr->data = (void *) fibptr->hw_fib_va->data;
151 fibptr->next = fibptr+1; /* Forward chain the fibs */
152 sema_init(&fibptr->event_wait, 0);
153 spin_lock_init(&fibptr->event_lock);
154 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
155 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
156 fibptr->hw_fib_pa = hw_fib_pa;
157 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
158 dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
159 hw_fib_pa = hw_fib_pa +
160 dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
163 * Add the fib chain to the free list
165 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
167 * Enable this to debug out of queue space
169 dev->free_fib = &dev->fibs[0];
174 * aac_fib_alloc - allocate a fib
175 * @dev: Adapter to allocate the fib for
177 * Allocate a fib from the adapter fib pool. If the pool is empty we
181 struct fib *aac_fib_alloc(struct aac_dev *dev)
185 spin_lock_irqsave(&dev->fib_lock, flags);
186 fibptr = dev->free_fib;
188 spin_unlock_irqrestore(&dev->fib_lock, flags);
191 dev->free_fib = fibptr->next;
192 spin_unlock_irqrestore(&dev->fib_lock, flags);
194 * Set the proper node type code and node byte size
196 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
197 fibptr->size = sizeof(struct fib);
199 * Null out fields that depend on being zero at the start of
202 fibptr->hw_fib_va->header.XferState = 0;
204 fibptr->callback = NULL;
205 fibptr->callback_data = NULL;
211 * aac_fib_free - free a fib
212 * @fibptr: fib to free up
214 * Frees up a fib and places it on the appropriate queue
217 void aac_fib_free(struct fib *fibptr)
219 unsigned long flags, flagsv;
221 spin_lock_irqsave(&fibptr->event_lock, flagsv);
222 if (fibptr->done == 2) {
223 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
226 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
228 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
229 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
230 aac_config.fib_timeouts++;
231 if (fibptr->hw_fib_va->header.XferState != 0) {
232 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
234 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
236 fibptr->next = fibptr->dev->free_fib;
237 fibptr->dev->free_fib = fibptr;
238 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
242 * aac_fib_init - initialise a fib
243 * @fibptr: The fib to initialize
245 * Set up the generic fib fields ready for use
248 void aac_fib_init(struct fib *fibptr)
250 struct hw_fib *hw_fib = fibptr->hw_fib_va;
252 hw_fib->header.StructType = FIB_MAGIC;
253 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
254 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
255 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
256 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
257 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
261 * fib_deallocate - deallocate a fib
262 * @fibptr: fib to deallocate
264 * Will deallocate and return to the free pool the FIB pointed to by the
268 static void fib_dealloc(struct fib * fibptr)
270 struct hw_fib *hw_fib = fibptr->hw_fib_va;
271 BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
272 hw_fib->header.XferState = 0;
276 * Commuication primitives define and support the queuing method we use to
277 * support host to adapter commuication. All queue accesses happen through
278 * these routines and are the only routines which have a knowledge of the
279 * how these queues are implemented.
283 * aac_get_entry - get a queue entry
286 * @entry: Entry return
287 * @index: Index return
288 * @nonotify: notification control
290 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
291 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
295 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
297 struct aac_queue * q;
301 * All of the queues wrap when they reach the end, so we check
302 * to see if they have reached the end and if they have we just
303 * set the index back to zero. This is a wrap. You could or off
304 * the high bits in all updates but this is a bit faster I think.
307 q = &dev->queues->queue[qid];
309 idx = *index = le32_to_cpu(*(q->headers.producer));
310 /* Interrupt Moderation, only interrupt for first two entries */
311 if (idx != le32_to_cpu(*(q->headers.consumer))) {
313 if (qid == AdapNormCmdQueue)
314 idx = ADAP_NORM_CMD_ENTRIES;
316 idx = ADAP_NORM_RESP_ENTRIES;
318 if (idx != le32_to_cpu(*(q->headers.consumer)))
322 if (qid == AdapNormCmdQueue) {
323 if (*index >= ADAP_NORM_CMD_ENTRIES)
324 *index = 0; /* Wrap to front of the Producer Queue. */
326 if (*index >= ADAP_NORM_RESP_ENTRIES)
327 *index = 0; /* Wrap to front of the Producer Queue. */
331 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
332 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
336 *entry = q->base + *index;
342 * aac_queue_get - get the next free QE
344 * @index: Returned index
345 * @priority: Priority of fib
346 * @fib: Fib to associate with the queue entry
347 * @wait: Wait if queue full
348 * @fibptr: Driver fib object to go with fib
349 * @nonotify: Don't notify the adapter
351 * Gets the next free QE off the requested priorty adapter command
352 * queue and associates the Fib with the QE. The QE represented by
353 * index is ready to insert on the queue when this routine returns
357 int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
359 struct aac_entry * entry = NULL;
362 if (qid == AdapNormCmdQueue) {
363 /* if no entries wait for some if caller wants to */
364 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
365 printk(KERN_ERR "GetEntries failed\n");
368 * Setup queue entry with a command, status and fib mapped
370 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
373 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
374 /* if no entries wait for some if caller wants to */
377 * Setup queue entry with command, status and fib mapped
379 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
380 entry->addr = hw_fib->header.SenderFibAddress;
381 /* Restore adapters pointer to the FIB */
382 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
386 * If MapFib is true than we need to map the Fib and put pointers
387 * in the queue entry.
390 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
395 * Define the highest level of host to adapter communication routines.
396 * These routines will support host to adapter FS commuication. These
397 * routines have no knowledge of the commuication method used. This level
398 * sends and receives FIBs. This level has no knowledge of how these FIBs
399 * get passed back and forth.
403 * aac_fib_send - send a fib to the adapter
404 * @command: Command to send
406 * @size: Size of fib data area
407 * @priority: Priority of Fib
408 * @wait: Async/sync select
409 * @reply: True if a reply is wanted
410 * @callback: Called with reply
411 * @callback_data: Passed to callback
413 * Sends the requested FIB to the adapter and optionally will wait for a
414 * response FIB. If the caller does not wish to wait for a response than
415 * an event to wait on must be supplied. This event will be set when a
416 * response FIB is received from the adapter.
419 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
420 int priority, int wait, int reply, fib_callback callback,
423 struct aac_dev * dev = fibptr->dev;
424 struct hw_fib * hw_fib = fibptr->hw_fib_va;
425 unsigned long flags = 0;
426 unsigned long qflags;
427 unsigned long mflags = 0;
430 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
433 * There are 5 cases with the wait and response requested flags.
434 * The only invalid cases are if the caller requests to wait and
435 * does not request a response and if the caller does not want a
436 * response and the Fib is not allocated from pool. If a response
437 * is not requesed the Fib will just be deallocaed by the DPC
438 * routine when the response comes back from the adapter. No
439 * further processing will be done besides deleting the Fib. We
440 * will have a debug mode where the adapter can notify the host
441 * it had a problem and the host can log that fact.
444 if (wait && !reply) {
446 } else if (!wait && reply) {
447 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
448 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
449 } else if (!wait && !reply) {
450 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
451 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
452 } else if (wait && reply) {
453 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
454 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
457 * Map the fib into 32bits by using the fib number
460 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
461 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
463 * Set FIB state to indicate where it came from and if we want a
464 * response from the adapter. Also load the command from the
467 * Map the hw fib pointer as a 32bit value
469 hw_fib->header.Command = cpu_to_le16(command);
470 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
471 fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
473 * Set the size of the Fib we want to send to the adapter
475 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
476 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
480 * Get a queue entry connect the FIB to it and send an notify
481 * the adapter a command is ready.
483 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
486 * Fill in the Callback and CallbackContext if we are not
490 fibptr->callback = callback;
491 fibptr->callback_data = callback_data;
492 fibptr->flags = FIB_CONTEXT_FLAG;
497 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
499 dprintk((KERN_DEBUG "Fib contents:.\n"));
500 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
501 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
502 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
503 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
504 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
505 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
512 spin_lock_irqsave(&dev->manage_lock, mflags);
513 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
514 printk(KERN_INFO "No management Fibs Available:%d\n",
515 dev->management_fib_count);
516 spin_unlock_irqrestore(&dev->manage_lock, mflags);
519 dev->management_fib_count++;
520 spin_unlock_irqrestore(&dev->manage_lock, mflags);
521 spin_lock_irqsave(&fibptr->event_lock, flags);
524 if (aac_adapter_deliver(fibptr) != 0) {
525 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
527 spin_unlock_irqrestore(&fibptr->event_lock, flags);
528 spin_lock_irqsave(&dev->manage_lock, mflags);
529 dev->management_fib_count--;
530 spin_unlock_irqrestore(&dev->manage_lock, mflags);
537 * If the caller wanted us to wait for response wait now.
541 spin_unlock_irqrestore(&fibptr->event_lock, flags);
542 /* Only set for first known interruptable command */
545 * *VERY* Dangerous to time out a command, the
546 * assumption is made that we have no hope of
547 * functioning because an interrupt routing or other
548 * hardware failure has occurred.
550 unsigned long count = 36000000L; /* 3 minutes */
551 while (down_trylock(&fibptr->event_wait)) {
554 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
555 spin_lock_irqsave(q->lock, qflags);
557 spin_unlock_irqrestore(q->lock, qflags);
559 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
560 "Usually a result of a PCI interrupt routing problem;\n"
561 "update mother board BIOS or consider utilizing one of\n"
562 "the SAFE mode kernel options (acpi, apic etc)\n");
566 if ((blink = aac_adapter_check_health(dev)) > 0) {
568 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
569 "Usually a result of a serious unrecoverable hardware problem\n",
576 } else if (down_interruptible(&fibptr->event_wait)) {
577 /* Do nothing ... satisfy
578 * down_interruptible must_check */
581 spin_lock_irqsave(&fibptr->event_lock, flags);
582 if (fibptr->done == 0) {
583 fibptr->done = 2; /* Tell interrupt we aborted */
584 spin_unlock_irqrestore(&fibptr->event_lock, flags);
587 spin_unlock_irqrestore(&fibptr->event_lock, flags);
588 BUG_ON(fibptr->done == 0);
590 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
595 * If the user does not want a response than return success otherwise
605 * aac_consumer_get - get the top of the queue
608 * @entry: Return entry
610 * Will return a pointer to the entry on the top of the queue requested that
611 * we are a consumer of, and return the address of the queue entry. It does
612 * not change the state of the queue.
615 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
619 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
623 * The consumer index must be wrapped if we have reached
624 * the end of the queue, else we just use the entry
625 * pointed to by the header index
627 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
630 index = le32_to_cpu(*q->headers.consumer);
631 *entry = q->base + index;
638 * aac_consumer_free - free consumer entry
643 * Frees up the current top of the queue we are a consumer of. If the
644 * queue was full notify the producer that the queue is no longer full.
647 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
652 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
655 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
656 *q->headers.consumer = cpu_to_le32(1);
658 le32_add_cpu(q->headers.consumer, 1);
663 case HostNormCmdQueue:
664 notify = HostNormCmdNotFull;
666 case HostNormRespQueue:
667 notify = HostNormRespNotFull;
673 aac_adapter_notify(dev, notify);
678 * aac_fib_adapter_complete - complete adapter issued fib
679 * @fibptr: fib to complete
682 * Will do all necessary work to complete a FIB that was sent from
686 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
688 struct hw_fib * hw_fib = fibptr->hw_fib_va;
689 struct aac_dev * dev = fibptr->dev;
690 struct aac_queue * q;
691 unsigned long nointr = 0;
692 unsigned long qflags;
694 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
699 if (hw_fib->header.XferState == 0) {
700 if (dev->comm_interface == AAC_COMM_MESSAGE)
705 * If we plan to do anything check the structure type first.
707 if (hw_fib->header.StructType != FIB_MAGIC) {
708 if (dev->comm_interface == AAC_COMM_MESSAGE)
713 * This block handles the case where the adapter had sent us a
714 * command and we have finished processing the command. We
715 * call completeFib when we are done processing the command
716 * and want to send a response back to the adapter. This will
717 * send the completed cdb to the adapter.
719 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
720 if (dev->comm_interface == AAC_COMM_MESSAGE) {
724 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
726 size += sizeof(struct aac_fibhdr);
727 if (size > le16_to_cpu(hw_fib->header.SenderSize))
729 hw_fib->header.Size = cpu_to_le16(size);
731 q = &dev->queues->queue[AdapNormRespQueue];
732 spin_lock_irqsave(q->lock, qflags);
733 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
734 *(q->headers.producer) = cpu_to_le32(index + 1);
735 spin_unlock_irqrestore(q->lock, qflags);
736 if (!(nointr & (int)aac_config.irq_mod))
737 aac_adapter_notify(dev, AdapNormRespQueue);
740 printk(KERN_WARNING "aac_fib_adapter_complete: "
741 "Unknown xferstate detected.\n");
748 * aac_fib_complete - fib completion handler
749 * @fib: FIB to complete
751 * Will do all necessary work to complete a FIB.
754 int aac_fib_complete(struct fib *fibptr)
757 struct hw_fib * hw_fib = fibptr->hw_fib_va;
760 * Check for a fib which has already been completed
763 if (hw_fib->header.XferState == 0)
766 * If we plan to do anything check the structure type first.
769 if (hw_fib->header.StructType != FIB_MAGIC)
772 * This block completes a cdb which orginated on the host and we
773 * just need to deallocate the cdb or reinit it. At this point the
774 * command is complete that we had sent to the adapter and this
775 * cdb could be reused.
777 spin_lock_irqsave(&fibptr->event_lock, flags);
778 if (fibptr->done == 2) {
779 spin_unlock_irqrestore(&fibptr->event_lock, flags);
782 spin_unlock_irqrestore(&fibptr->event_lock, flags);
784 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
785 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
789 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
792 * This handles the case when the host has aborted the I/O
793 * to the adapter because the adapter is not responding
796 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
805 * aac_printf - handle printf from firmware
809 * Print a message passed to us by the controller firmware on the
813 void aac_printf(struct aac_dev *dev, u32 val)
815 char *cp = dev->printfbuf;
816 if (dev->printf_enabled)
818 int length = val & 0xffff;
819 int level = (val >> 16) & 0xffff;
822 * The size of the printfbuf is set in port.c
823 * There is no variable or define for it
829 if (level == LOG_AAC_HIGH_ERROR)
830 printk(KERN_WARNING "%s:%s", dev->name, cp);
832 printk(KERN_INFO "%s:%s", dev->name, cp);
839 * aac_handle_aif - Handle a message from the firmware
840 * @dev: Which adapter this fib is from
841 * @fibptr: Pointer to fibptr from adapter
843 * This routine handles a driver notify fib from the adapter and
844 * dispatches it to the appropriate routine for handling.
847 #define AIF_SNIFF_TIMEOUT (30*HZ)
848 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
850 struct hw_fib * hw_fib = fibptr->hw_fib_va;
851 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
852 u32 channel, id, lun, container;
853 struct scsi_device *device;
859 } device_config_needed = NOTHING;
861 /* Sniff for container changes */
863 if (!dev || !dev->fsa_dev)
865 container = channel = id = lun = (u32)-1;
868 * We have set this up to try and minimize the number of
869 * re-configures that take place. As a result of this when
870 * certain AIF's come in we will set a flag waiting for another
871 * type of AIF before setting the re-config flag.
873 switch (le32_to_cpu(aifcmd->command)) {
874 case AifCmdDriverNotify:
875 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
877 * Morph or Expand complete
879 case AifDenMorphComplete:
880 case AifDenVolumeExtendComplete:
881 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
882 if (container >= dev->maximum_num_containers)
886 * Find the scsi_device associated with the SCSI
887 * address. Make sure we have the right array, and if
888 * so set the flag to initiate a new re-config once we
889 * see an AifEnConfigChange AIF come through.
892 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
893 device = scsi_device_lookup(dev->scsi_host_ptr,
894 CONTAINER_TO_CHANNEL(container),
895 CONTAINER_TO_ID(container),
896 CONTAINER_TO_LUN(container));
898 dev->fsa_dev[container].config_needed = CHANGE;
899 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
900 dev->fsa_dev[container].config_waiting_stamp = jiffies;
901 scsi_device_put(device);
907 * If we are waiting on something and this happens to be
908 * that thing then set the re-configure flag.
910 if (container != (u32)-1) {
911 if (container >= dev->maximum_num_containers)
913 if ((dev->fsa_dev[container].config_waiting_on ==
914 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
915 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
916 dev->fsa_dev[container].config_waiting_on = 0;
917 } else for (container = 0;
918 container < dev->maximum_num_containers; ++container) {
919 if ((dev->fsa_dev[container].config_waiting_on ==
920 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
921 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
922 dev->fsa_dev[container].config_waiting_on = 0;
926 case AifCmdEventNotify:
927 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
928 case AifEnBatteryEvent:
929 dev->cache_protected =
930 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
935 case AifEnAddContainer:
936 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
937 if (container >= dev->maximum_num_containers)
939 dev->fsa_dev[container].config_needed = ADD;
940 dev->fsa_dev[container].config_waiting_on =
942 dev->fsa_dev[container].config_waiting_stamp = jiffies;
948 case AifEnDeleteContainer:
949 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
950 if (container >= dev->maximum_num_containers)
952 dev->fsa_dev[container].config_needed = DELETE;
953 dev->fsa_dev[container].config_waiting_on =
955 dev->fsa_dev[container].config_waiting_stamp = jiffies;
959 * Container change detected. If we currently are not
960 * waiting on something else, setup to wait on a Config Change.
962 case AifEnContainerChange:
963 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
964 if (container >= dev->maximum_num_containers)
966 if (dev->fsa_dev[container].config_waiting_on &&
967 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
969 dev->fsa_dev[container].config_needed = CHANGE;
970 dev->fsa_dev[container].config_waiting_on =
972 dev->fsa_dev[container].config_waiting_stamp = jiffies;
975 case AifEnConfigChange:
979 case AifEnDeleteJBOD:
980 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
981 if ((container >> 28)) {
985 channel = (container >> 24) & 0xF;
986 if (channel >= dev->maximum_num_channels) {
990 id = container & 0xFFFF;
991 if (id >= dev->maximum_num_physicals) {
995 lun = (container >> 16) & 0xFF;
997 channel = aac_phys_to_logical(channel);
998 device_config_needed =
999 (((__le32 *)aifcmd->data)[0] ==
1000 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
1001 if (device_config_needed == ADD) {
1002 device = scsi_device_lookup(dev->scsi_host_ptr,
1007 scsi_remove_device(device);
1008 scsi_device_put(device);
1013 case AifEnEnclosureManagement:
1015 * If in JBOD mode, automatic exposure of new
1016 * physical target to be suppressed until configured.
1020 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1021 case EM_DRIVE_INSERTION:
1022 case EM_DRIVE_REMOVAL:
1023 container = le32_to_cpu(
1024 ((__le32 *)aifcmd->data)[2]);
1025 if ((container >> 28)) {
1026 container = (u32)-1;
1029 channel = (container >> 24) & 0xF;
1030 if (channel >= dev->maximum_num_channels) {
1031 container = (u32)-1;
1034 id = container & 0xFFFF;
1035 lun = (container >> 16) & 0xFF;
1036 container = (u32)-1;
1037 if (id >= dev->maximum_num_physicals) {
1038 /* legacy dev_t ? */
1039 if ((0x2000 <= id) || lun || channel ||
1040 ((channel = (id >> 7) & 0x3F) >=
1041 dev->maximum_num_channels))
1043 lun = (id >> 4) & 7;
1046 channel = aac_phys_to_logical(channel);
1047 device_config_needed =
1048 (((__le32 *)aifcmd->data)[3]
1049 == cpu_to_le32(EM_DRIVE_INSERTION)) ?
1057 * If we are waiting on something and this happens to be
1058 * that thing then set the re-configure flag.
1060 if (container != (u32)-1) {
1061 if (container >= dev->maximum_num_containers)
1063 if ((dev->fsa_dev[container].config_waiting_on ==
1064 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1065 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1066 dev->fsa_dev[container].config_waiting_on = 0;
1067 } else for (container = 0;
1068 container < dev->maximum_num_containers; ++container) {
1069 if ((dev->fsa_dev[container].config_waiting_on ==
1070 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1071 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1072 dev->fsa_dev[container].config_waiting_on = 0;
1076 case AifCmdJobProgress:
1078 * These are job progress AIF's. When a Clear is being
1079 * done on a container it is initially created then hidden from
1080 * the OS. When the clear completes we don't get a config
1081 * change so we monitor the job status complete on a clear then
1082 * wait for a container change.
1085 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1086 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1087 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1089 container < dev->maximum_num_containers;
1092 * Stomp on all config sequencing for all
1095 dev->fsa_dev[container].config_waiting_on =
1096 AifEnContainerChange;
1097 dev->fsa_dev[container].config_needed = ADD;
1098 dev->fsa_dev[container].config_waiting_stamp =
1102 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1103 ((__le32 *)aifcmd->data)[6] == 0 &&
1104 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1106 container < dev->maximum_num_containers;
1109 * Stomp on all config sequencing for all
1112 dev->fsa_dev[container].config_waiting_on =
1113 AifEnContainerChange;
1114 dev->fsa_dev[container].config_needed = DELETE;
1115 dev->fsa_dev[container].config_waiting_stamp =
1124 if (device_config_needed == NOTHING)
1125 for (; container < dev->maximum_num_containers; ++container) {
1126 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1127 (dev->fsa_dev[container].config_needed != NOTHING) &&
1128 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1129 device_config_needed =
1130 dev->fsa_dev[container].config_needed;
1131 dev->fsa_dev[container].config_needed = NOTHING;
1132 channel = CONTAINER_TO_CHANNEL(container);
1133 id = CONTAINER_TO_ID(container);
1134 lun = CONTAINER_TO_LUN(container);
1138 if (device_config_needed == NOTHING)
1142 * If we decided that a re-configuration needs to be done,
1143 * schedule it here on the way out the door, please close the door
1148 * Find the scsi_device associated with the SCSI address,
1149 * and mark it as changed, invalidating the cache. This deals
1150 * with changes to existing device IDs.
1153 if (!dev || !dev->scsi_host_ptr)
1156 * force reload of disk info via aac_probe_container
1158 if ((channel == CONTAINER_CHANNEL) &&
1159 (device_config_needed != NOTHING)) {
1160 if (dev->fsa_dev[container].valid == 1)
1161 dev->fsa_dev[container].valid = 2;
1162 aac_probe_container(dev, container);
1164 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1166 switch (device_config_needed) {
1168 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1169 scsi_remove_device(device);
1171 if (scsi_device_online(device)) {
1172 scsi_device_set_state(device, SDEV_OFFLINE);
1173 sdev_printk(KERN_INFO, device,
1174 "Device offlined - %s\n",
1175 (channel == CONTAINER_CHANNEL) ?
1177 "enclosure services event");
1182 if (!scsi_device_online(device)) {
1183 sdev_printk(KERN_INFO, device,
1184 "Device online - %s\n",
1185 (channel == CONTAINER_CHANNEL) ?
1187 "enclosure services event");
1188 scsi_device_set_state(device, SDEV_RUNNING);
1192 if ((channel == CONTAINER_CHANNEL)
1193 && (!dev->fsa_dev[container].valid)) {
1194 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1195 scsi_remove_device(device);
1197 if (!scsi_device_online(device))
1199 scsi_device_set_state(device, SDEV_OFFLINE);
1200 sdev_printk(KERN_INFO, device,
1201 "Device offlined - %s\n",
1206 scsi_rescan_device(&device->sdev_gendev);
1211 scsi_device_put(device);
1212 device_config_needed = NOTHING;
1214 if (device_config_needed == ADD)
1215 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1216 if (channel == CONTAINER_CHANNEL) {
1218 device_config_needed = NOTHING;
1223 static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1227 struct Scsi_Host *host;
1228 struct scsi_device *dev;
1229 struct scsi_cmnd *command;
1230 struct scsi_cmnd *command_list;
1235 * - host is locked, unless called by the aacraid thread.
1236 * (a matter of convenience, due to legacy issues surrounding
1237 * eh_host_adapter_reset).
1238 * - in_reset is asserted, so no new i/o is getting to the
1240 * - The card is dead, or will be very shortly ;-/ so no new
1241 * commands are completing in the interrupt service.
1243 host = aac->scsi_host_ptr;
1244 scsi_block_requests(host);
1245 aac_adapter_disable_int(aac);
1246 if (aac->thread->pid != current->pid) {
1247 spin_unlock_irq(host->host_lock);
1248 kthread_stop(aac->thread);
1253 * If a positive health, means in a known DEAD PANIC
1254 * state and the adapter could be reset to `try again'.
1256 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1262 * Loop through the fibs, close the synchronous FIBS
1264 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1265 struct fib *fib = &aac->fibs[index];
1266 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1267 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1268 unsigned long flagv;
1269 spin_lock_irqsave(&fib->event_lock, flagv);
1270 up(&fib->event_wait);
1271 spin_unlock_irqrestore(&fib->event_lock, flagv);
1276 /* Give some extra time for ioctls to complete. */
1279 index = aac->cardtype;
1282 * Re-initialize the adapter, first free resources, then carefully
1283 * apply the initialization sequence to come back again. Only risk
1284 * is a change in Firmware dropping cache, it is assumed the caller
1285 * will ensure that i/o is queisced and the card is flushed in that
1288 aac_fib_map_free(aac);
1289 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1290 aac->comm_addr = NULL;
1294 free_irq(aac->pdev->irq, aac);
1296 pci_disable_msi(aac->pdev);
1297 kfree(aac->fsa_dev);
1298 aac->fsa_dev = NULL;
1299 quirks = aac_get_driver_ident(index)->quirks;
1300 if (quirks & AAC_QUIRK_31BIT) {
1301 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1302 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1305 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1306 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1309 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1311 if (quirks & AAC_QUIRK_31BIT)
1312 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1315 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1316 if (IS_ERR(aac->thread)) {
1317 retval = PTR_ERR(aac->thread);
1321 (void)aac_get_adapter_info(aac);
1322 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1323 host->sg_tablesize = 34;
1324 host->max_sectors = (host->sg_tablesize * 8) + 112;
1326 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1327 host->sg_tablesize = 17;
1328 host->max_sectors = (host->sg_tablesize * 8) + 112;
1330 aac_get_config_status(aac, 1);
1331 aac_get_containers(aac);
1333 * This is where the assumption that the Adapter is quiesced
1336 command_list = NULL;
1337 __shost_for_each_device(dev, host) {
1338 unsigned long flags;
1339 spin_lock_irqsave(&dev->list_lock, flags);
1340 list_for_each_entry(command, &dev->cmd_list, list)
1341 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1342 command->SCp.buffer = (struct scatterlist *)command_list;
1343 command_list = command;
1345 spin_unlock_irqrestore(&dev->list_lock, flags);
1347 while ((command = command_list)) {
1348 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1349 command->SCp.buffer = NULL;
1350 command->result = DID_OK << 16
1351 | COMMAND_COMPLETE << 8
1352 | SAM_STAT_TASK_SET_FULL;
1353 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1354 command->scsi_done(command);
1360 scsi_unblock_requests(host);
1362 spin_lock_irq(host->host_lock);
1367 int aac_reset_adapter(struct aac_dev * aac, int forced)
1369 unsigned long flagv = 0;
1371 struct Scsi_Host * host;
1373 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1376 if (aac->in_reset) {
1377 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1381 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1384 * Wait for all commands to complete to this specific
1385 * target (block maximum 60 seconds). Although not necessary,
1386 * it does make us a good storage citizen.
1388 host = aac->scsi_host_ptr;
1389 scsi_block_requests(host);
1390 if (forced < 2) for (retval = 60; retval; --retval) {
1391 struct scsi_device * dev;
1392 struct scsi_cmnd * command;
1395 __shost_for_each_device(dev, host) {
1396 spin_lock_irqsave(&dev->list_lock, flagv);
1397 list_for_each_entry(command, &dev->cmd_list, list) {
1398 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1403 spin_unlock_irqrestore(&dev->list_lock, flagv);
1409 * We can exit If all the commands are complete
1416 /* Quiesce build, flush cache, write through mode */
1418 aac_send_shutdown(aac);
1419 spin_lock_irqsave(host->host_lock, flagv);
1420 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1421 spin_unlock_irqrestore(host->host_lock, flagv);
1423 if ((forced < 2) && (retval == -ENODEV)) {
1424 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1425 struct fib * fibctx = aac_fib_alloc(aac);
1427 struct aac_pause *cmd;
1430 aac_fib_init(fibctx);
1432 cmd = (struct aac_pause *) fib_data(fibctx);
1434 cmd->command = cpu_to_le32(VM_ContainerConfig);
1435 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1436 cmd->timeout = cpu_to_le32(1);
1437 cmd->min = cpu_to_le32(1);
1438 cmd->noRescan = cpu_to_le32(1);
1439 cmd->count = cpu_to_le32(0);
1441 status = aac_fib_send(ContainerCommand,
1443 sizeof(struct aac_pause),
1445 -2 /* Timeout silently */, 1,
1449 aac_fib_complete(fibctx);
1450 /* FIB should be freed only after getting
1451 * the response from the F/W */
1452 if (status != -ERESTARTSYS)
1453 aac_fib_free(fibctx);
1460 int aac_check_health(struct aac_dev * aac)
1463 unsigned long time_now, flagv = 0;
1464 struct list_head * entry;
1465 struct Scsi_Host * host;
1467 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1468 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1471 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1472 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1479 * aac_aifcmd.command = AifCmdEventNotify = 1
1480 * aac_aifcmd.seqnum = 0xFFFFFFFF
1481 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1482 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1483 * aac.aifcmd.data[2] = AifHighPriority = 3
1484 * aac.aifcmd.data[3] = BlinkLED
1487 time_now = jiffies/HZ;
1488 entry = aac->fib_list.next;
1491 * For each Context that is on the
1492 * fibctxList, make a copy of the
1493 * fib, and then set the event to wake up the
1494 * thread that is waiting for it.
1496 while (entry != &aac->fib_list) {
1498 * Extract the fibctx
1500 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1501 struct hw_fib * hw_fib;
1504 * Check if the queue is getting
1507 if (fibctx->count > 20) {
1509 * It's *not* jiffies folks,
1510 * but jiffies / HZ, so do not
1513 u32 time_last = fibctx->jiffies;
1515 * Has it been > 2 minutes
1516 * since the last read off
1519 if ((time_now - time_last) > aif_timeout) {
1520 entry = entry->next;
1521 aac_close_fib_context(aac, fibctx);
1526 * Warning: no sleep allowed while
1529 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1530 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1531 if (fib && hw_fib) {
1532 struct aac_aifcmd * aif;
1534 fib->hw_fib_va = hw_fib;
1537 fib->type = FSAFS_NTC_FIB_CONTEXT;
1538 fib->size = sizeof (struct fib);
1539 fib->data = hw_fib->data;
1540 aif = (struct aac_aifcmd *)hw_fib->data;
1541 aif->command = cpu_to_le32(AifCmdEventNotify);
1542 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1543 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1544 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1545 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1546 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1549 * Put the FIB onto the
1552 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1555 * Set the event to wake up the
1556 * thread that will waiting.
1558 up(&fibctx->wait_sem);
1560 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1564 entry = entry->next;
1567 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1570 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1574 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1576 if (!aac_check_reset || ((aac_check_reset == 1) &&
1577 (aac->supplement_adapter_info.SupportedOptions2 &
1578 AAC_OPTION_IGNORE_RESET)))
1580 host = aac->scsi_host_ptr;
1581 if (aac->thread->pid != current->pid)
1582 spin_lock_irqsave(host->host_lock, flagv);
1583 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1584 if (aac->thread->pid != current->pid)
1585 spin_unlock_irqrestore(host->host_lock, flagv);
1595 * aac_command_thread - command processing thread
1596 * @dev: Adapter to monitor
1598 * Waits on the commandready event in it's queue. When the event gets set
1599 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1600 * until the queue is empty. When the queue is empty it will wait for
1604 int aac_command_thread(void *data)
1606 struct aac_dev *dev = data;
1607 struct hw_fib *hw_fib, *hw_newfib;
1608 struct fib *fib, *newfib;
1609 struct aac_fib_context *fibctx;
1610 unsigned long flags;
1611 DECLARE_WAITQUEUE(wait, current);
1612 unsigned long next_jiffies = jiffies + HZ;
1613 unsigned long next_check_jiffies = next_jiffies;
1614 long difference = HZ;
1617 * We can only have one thread per adapter for AIF's.
1619 if (dev->aif_thread)
1623 * Let the DPC know it has a place to send the AIF's to.
1625 dev->aif_thread = 1;
1626 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1627 set_current_state(TASK_INTERRUPTIBLE);
1628 dprintk ((KERN_INFO "aac_command_thread start\n"));
1630 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1631 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1632 struct list_head *entry;
1633 struct aac_aifcmd * aifcmd;
1635 set_current_state(TASK_RUNNING);
1637 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1640 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1641 fib = list_entry(entry, struct fib, fiblink);
1643 * We will process the FIB here or pass it to a
1644 * worker thread that is TBD. We Really can't
1645 * do anything at this point since we don't have
1646 * anything defined for this thread to do.
1648 hw_fib = fib->hw_fib_va;
1649 memset(fib, 0, sizeof(struct fib));
1650 fib->type = FSAFS_NTC_FIB_CONTEXT;
1651 fib->size = sizeof(struct fib);
1652 fib->hw_fib_va = hw_fib;
1653 fib->data = hw_fib->data;
1656 * We only handle AifRequest fibs from the adapter.
1658 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1659 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1660 /* Handle Driver Notify Events */
1661 aac_handle_aif(dev, fib);
1662 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1663 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1665 /* The u32 here is important and intended. We are using
1666 32bit wrapping time to fit the adapter field */
1668 u32 time_now, time_last;
1669 unsigned long flagv;
1671 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1672 struct fib ** fib_pool, ** fib_p;
1675 if ((aifcmd->command ==
1676 cpu_to_le32(AifCmdEventNotify)) ||
1678 cpu_to_le32(AifCmdJobProgress))) {
1679 aac_handle_aif(dev, fib);
1682 time_now = jiffies/HZ;
1685 * Warning: no sleep allowed while
1686 * holding spinlock. We take the estimate
1687 * and pre-allocate a set of fibs outside the
1690 num = le32_to_cpu(dev->init->AdapterFibsSize)
1691 / sizeof(struct hw_fib); /* some extra */
1692 spin_lock_irqsave(&dev->fib_lock, flagv);
1693 entry = dev->fib_list.next;
1694 while (entry != &dev->fib_list) {
1695 entry = entry->next;
1698 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1702 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1703 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1704 hw_fib_p = hw_fib_pool;
1706 while (hw_fib_p < &hw_fib_pool[num]) {
1707 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1711 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1712 kfree(*(--hw_fib_p));
1716 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1726 spin_lock_irqsave(&dev->fib_lock, flagv);
1727 entry = dev->fib_list.next;
1729 * For each Context that is on the
1730 * fibctxList, make a copy of the
1731 * fib, and then set the event to wake up the
1732 * thread that is waiting for it.
1734 hw_fib_p = hw_fib_pool;
1736 while (entry != &dev->fib_list) {
1738 * Extract the fibctx
1740 fibctx = list_entry(entry, struct aac_fib_context, next);
1742 * Check if the queue is getting
1745 if (fibctx->count > 20)
1748 * It's *not* jiffies folks,
1749 * but jiffies / HZ so do not
1752 time_last = fibctx->jiffies;
1754 * Has it been > 2 minutes
1755 * since the last read off
1758 if ((time_now - time_last) > aif_timeout) {
1759 entry = entry->next;
1760 aac_close_fib_context(dev, fibctx);
1765 * Warning: no sleep allowed while
1768 if (hw_fib_p < &hw_fib_pool[num]) {
1769 hw_newfib = *hw_fib_p;
1770 *(hw_fib_p++) = NULL;
1774 * Make the copy of the FIB
1776 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1777 memcpy(newfib, fib, sizeof(struct fib));
1778 newfib->hw_fib_va = hw_newfib;
1780 * Put the FIB onto the
1783 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1786 * Set the event to wake up the
1787 * thread that is waiting.
1789 up(&fibctx->wait_sem);
1791 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1793 entry = entry->next;
1796 * Set the status of this FIB
1798 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1799 aac_fib_adapter_complete(fib, sizeof(u32));
1800 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1801 /* Free up the remaining resources */
1802 hw_fib_p = hw_fib_pool;
1804 while (hw_fib_p < &hw_fib_pool[num]) {
1814 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1817 * There are no more AIF's
1819 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1822 * Background activity
1824 if ((time_before(next_check_jiffies,next_jiffies))
1825 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1826 next_check_jiffies = next_jiffies;
1827 if (aac_check_health(dev) == 0) {
1828 difference = ((long)(unsigned)check_interval)
1830 next_check_jiffies = jiffies + difference;
1831 } else if (!dev->queues)
1834 if (!time_before(next_check_jiffies,next_jiffies)
1835 && ((difference = next_jiffies - jiffies) <= 0)) {
1839 /* Don't even try to talk to adapter if its sick */
1840 ret = aac_check_health(dev);
1841 if (!ret && !dev->queues)
1843 next_check_jiffies = jiffies
1844 + ((long)(unsigned)check_interval)
1846 do_gettimeofday(&now);
1848 /* Synchronize our watches */
1849 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1850 && (now.tv_usec > (1000000 / HZ)))
1851 difference = (((1000000 - now.tv_usec) * HZ)
1852 + 500000) / 1000000;
1853 else if (ret == 0) {
1856 if ((fibptr = aac_fib_alloc(dev))) {
1860 aac_fib_init(fibptr);
1862 info = (__le32 *) fib_data(fibptr);
1863 if (now.tv_usec > 500000)
1866 *info = cpu_to_le32(now.tv_sec);
1868 status = aac_fib_send(SendHostTime,
1875 /* Do not set XferState to zero unless
1876 * receives a response from F/W */
1878 aac_fib_complete(fibptr);
1879 /* FIB should be freed only after
1880 * getting the response from the F/W */
1881 if (status != -ERESTARTSYS)
1882 aac_fib_free(fibptr);
1884 difference = (long)(unsigned)update_interval*HZ;
1887 difference = 10 * HZ;
1889 next_jiffies = jiffies + difference;
1890 if (time_before(next_check_jiffies,next_jiffies))
1891 difference = next_check_jiffies - jiffies;
1893 if (difference <= 0)
1895 set_current_state(TASK_INTERRUPTIBLE);
1897 if (kthread_should_stop())
1900 schedule_timeout(difference);
1902 if (kthread_should_stop())
1906 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1907 dev->aif_thread = 0;