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 pci_free_consistent(dev
->pdev
,
87 dev
->max_fib_size
* (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
),
88 dev
->hw_fib_va
, dev
->hw_fib_pa
);
89 dev
->hw_fib_va
= NULL
;
94 * aac_fib_setup - setup the fibs
95 * @dev: Adapter to set up
97 * Allocate the PCI space for the fibs, map it and then initialise the
98 * fib area, the unmapped fib data and also the free list
101 int aac_fib_setup(struct aac_dev
* dev
)
104 struct hw_fib
*hw_fib
;
105 dma_addr_t hw_fib_pa
;
108 while (((i
= fib_map_alloc(dev
)) == -ENOMEM
)
109 && (dev
->scsi_host_ptr
->can_queue
> (64 - AAC_NUM_MGT_FIB
))) {
110 dev
->init
->MaxIoCommands
= cpu_to_le32((dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
) >> 1);
111 dev
->scsi_host_ptr
->can_queue
= le32_to_cpu(dev
->init
->MaxIoCommands
) - AAC_NUM_MGT_FIB
;
116 /* 32 byte alignment for PMC */
117 hw_fib_pa
= (dev
->hw_fib_pa
+ (ALIGN32
- 1)) & ~(ALIGN32
- 1);
118 dev
->hw_fib_va
= (struct hw_fib
*)((unsigned char *)dev
->hw_fib_va
+
119 (hw_fib_pa
- dev
->hw_fib_pa
));
120 dev
->hw_fib_pa
= hw_fib_pa
;
121 memset(dev
->hw_fib_va
, 0,
122 (dev
->max_fib_size
+ sizeof(struct aac_fib_xporthdr
)) *
123 (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
));
125 /* add Xport header */
126 dev
->hw_fib_va
= (struct hw_fib
*)((unsigned char *)dev
->hw_fib_va
+
127 sizeof(struct aac_fib_xporthdr
));
128 dev
->hw_fib_pa
+= sizeof(struct aac_fib_xporthdr
);
130 hw_fib
= dev
->hw_fib_va
;
131 hw_fib_pa
= dev
->hw_fib_pa
;
133 * Initialise the fibs
135 for (i
= 0, fibptr
= &dev
->fibs
[i
];
136 i
< (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
);
141 fibptr
->hw_fib_va
= hw_fib
;
142 fibptr
->data
= (void *) fibptr
->hw_fib_va
->data
;
143 fibptr
->next
= fibptr
+1; /* Forward chain the fibs */
144 sema_init(&fibptr
->event_wait
, 0);
145 spin_lock_init(&fibptr
->event_lock
);
146 hw_fib
->header
.XferState
= cpu_to_le32(0xffffffff);
147 hw_fib
->header
.SenderSize
= cpu_to_le16(dev
->max_fib_size
);
148 fibptr
->hw_fib_pa
= hw_fib_pa
;
149 hw_fib
= (struct hw_fib
*)((unsigned char *)hw_fib
+
150 dev
->max_fib_size
+ sizeof(struct aac_fib_xporthdr
));
151 hw_fib_pa
= hw_fib_pa
+
152 dev
->max_fib_size
+ sizeof(struct aac_fib_xporthdr
);
155 * Add the fib chain to the free list
157 dev
->fibs
[dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
- 1].next
= NULL
;
159 * Enable this to debug out of queue space
161 dev
->free_fib
= &dev
->fibs
[0];
166 * aac_fib_alloc - allocate a fib
167 * @dev: Adapter to allocate the fib for
169 * Allocate a fib from the adapter fib pool. If the pool is empty we
173 struct fib
*aac_fib_alloc(struct aac_dev
*dev
)
177 spin_lock_irqsave(&dev
->fib_lock
, flags
);
178 fibptr
= dev
->free_fib
;
180 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
183 dev
->free_fib
= fibptr
->next
;
184 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
186 * Set the proper node type code and node byte size
188 fibptr
->type
= FSAFS_NTC_FIB_CONTEXT
;
189 fibptr
->size
= sizeof(struct fib
);
191 * Null out fields that depend on being zero at the start of
194 fibptr
->hw_fib_va
->header
.XferState
= 0;
196 fibptr
->callback
= NULL
;
197 fibptr
->callback_data
= NULL
;
203 * aac_fib_free - free a fib
204 * @fibptr: fib to free up
206 * Frees up a fib and places it on the appropriate queue
209 void aac_fib_free(struct fib
*fibptr
)
213 if (fibptr
->done
== 2)
216 spin_lock_irqsave(&fibptr
->dev
->fib_lock
, flags
);
217 if (unlikely(fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
))
218 aac_config
.fib_timeouts
++;
219 if (fibptr
->hw_fib_va
->header
.XferState
!= 0) {
220 printk(KERN_WARNING
"aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
222 le32_to_cpu(fibptr
->hw_fib_va
->header
.XferState
));
224 fibptr
->next
= fibptr
->dev
->free_fib
;
225 fibptr
->dev
->free_fib
= fibptr
;
226 spin_unlock_irqrestore(&fibptr
->dev
->fib_lock
, flags
);
230 * aac_fib_init - initialise a fib
231 * @fibptr: The fib to initialize
233 * Set up the generic fib fields ready for use
236 void aac_fib_init(struct fib
*fibptr
)
238 struct hw_fib
*hw_fib
= fibptr
->hw_fib_va
;
240 memset(&hw_fib
->header
, 0, sizeof(struct aac_fibhdr
));
241 hw_fib
->header
.StructType
= FIB_MAGIC
;
242 hw_fib
->header
.Size
= cpu_to_le16(fibptr
->dev
->max_fib_size
);
243 hw_fib
->header
.XferState
= cpu_to_le32(HostOwned
| FibInitialized
| FibEmpty
| FastResponseCapable
);
244 hw_fib
->header
.u
.ReceiverFibAddress
= cpu_to_le32(fibptr
->hw_fib_pa
);
245 hw_fib
->header
.SenderSize
= cpu_to_le16(fibptr
->dev
->max_fib_size
);
249 * fib_deallocate - deallocate a fib
250 * @fibptr: fib to deallocate
252 * Will deallocate and return to the free pool the FIB pointed to by the
256 static void fib_dealloc(struct fib
* fibptr
)
258 struct hw_fib
*hw_fib
= fibptr
->hw_fib_va
;
259 hw_fib
->header
.XferState
= 0;
263 * Commuication primitives define and support the queuing method we use to
264 * support host to adapter commuication. All queue accesses happen through
265 * these routines and are the only routines which have a knowledge of the
266 * how these queues are implemented.
270 * aac_get_entry - get a queue entry
273 * @entry: Entry return
274 * @index: Index return
275 * @nonotify: notification control
277 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
278 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
282 static int aac_get_entry (struct aac_dev
* dev
, u32 qid
, struct aac_entry
**entry
, u32
* index
, unsigned long *nonotify
)
284 struct aac_queue
* q
;
288 * All of the queues wrap when they reach the end, so we check
289 * to see if they have reached the end and if they have we just
290 * set the index back to zero. This is a wrap. You could or off
291 * the high bits in all updates but this is a bit faster I think.
294 q
= &dev
->queues
->queue
[qid
];
296 idx
= *index
= le32_to_cpu(*(q
->headers
.producer
));
297 /* Interrupt Moderation, only interrupt for first two entries */
298 if (idx
!= le32_to_cpu(*(q
->headers
.consumer
))) {
300 if (qid
== AdapNormCmdQueue
)
301 idx
= ADAP_NORM_CMD_ENTRIES
;
303 idx
= ADAP_NORM_RESP_ENTRIES
;
305 if (idx
!= le32_to_cpu(*(q
->headers
.consumer
)))
309 if (qid
== AdapNormCmdQueue
) {
310 if (*index
>= ADAP_NORM_CMD_ENTRIES
)
311 *index
= 0; /* Wrap to front of the Producer Queue. */
313 if (*index
>= ADAP_NORM_RESP_ENTRIES
)
314 *index
= 0; /* Wrap to front of the Producer Queue. */
318 if ((*index
+ 1) == le32_to_cpu(*(q
->headers
.consumer
))) {
319 printk(KERN_WARNING
"Queue %d full, %u outstanding.\n",
320 qid
, atomic_read(&q
->numpending
));
323 *entry
= q
->base
+ *index
;
329 * aac_queue_get - get the next free QE
331 * @index: Returned index
332 * @priority: Priority of fib
333 * @fib: Fib to associate with the queue entry
334 * @wait: Wait if queue full
335 * @fibptr: Driver fib object to go with fib
336 * @nonotify: Don't notify the adapter
338 * Gets the next free QE off the requested priorty adapter command
339 * queue and associates the Fib with the QE. The QE represented by
340 * index is ready to insert on the queue when this routine returns
344 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
)
346 struct aac_entry
* entry
= NULL
;
349 if (qid
== AdapNormCmdQueue
) {
350 /* if no entries wait for some if caller wants to */
351 while (!aac_get_entry(dev
, qid
, &entry
, index
, nonotify
)) {
352 printk(KERN_ERR
"GetEntries failed\n");
355 * Setup queue entry with a command, status and fib mapped
357 entry
->size
= cpu_to_le32(le16_to_cpu(hw_fib
->header
.Size
));
360 while (!aac_get_entry(dev
, qid
, &entry
, index
, nonotify
)) {
361 /* if no entries wait for some if caller wants to */
364 * Setup queue entry with command, status and fib mapped
366 entry
->size
= cpu_to_le32(le16_to_cpu(hw_fib
->header
.Size
));
367 entry
->addr
= hw_fib
->header
.SenderFibAddress
;
368 /* Restore adapters pointer to the FIB */
369 hw_fib
->header
.u
.ReceiverFibAddress
= hw_fib
->header
.SenderFibAddress
; /* Let the adapter now where to find its data */
373 * If MapFib is true than we need to map the Fib and put pointers
374 * in the queue entry.
377 entry
->addr
= cpu_to_le32(fibptr
->hw_fib_pa
);
382 * Define the highest level of host to adapter communication routines.
383 * These routines will support host to adapter FS commuication. These
384 * routines have no knowledge of the commuication method used. This level
385 * sends and receives FIBs. This level has no knowledge of how these FIBs
386 * get passed back and forth.
390 * aac_fib_send - send a fib to the adapter
391 * @command: Command to send
393 * @size: Size of fib data area
394 * @priority: Priority of Fib
395 * @wait: Async/sync select
396 * @reply: True if a reply is wanted
397 * @callback: Called with reply
398 * @callback_data: Passed to callback
400 * Sends the requested FIB to the adapter and optionally will wait for a
401 * response FIB. If the caller does not wish to wait for a response than
402 * an event to wait on must be supplied. This event will be set when a
403 * response FIB is received from the adapter.
406 int aac_fib_send(u16 command
, struct fib
*fibptr
, unsigned long size
,
407 int priority
, int wait
, int reply
, fib_callback callback
,
410 struct aac_dev
* dev
= fibptr
->dev
;
411 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
412 unsigned long flags
= 0;
413 unsigned long mflags
= 0;
414 unsigned long sflags
= 0;
417 if (!(hw_fib
->header
.XferState
& cpu_to_le32(HostOwned
)))
420 * There are 5 cases with the wait and response requested flags.
421 * The only invalid cases are if the caller requests to wait and
422 * does not request a response and if the caller does not want a
423 * response and the Fib is not allocated from pool. If a response
424 * is not requesed the Fib will just be deallocaed by the DPC
425 * routine when the response comes back from the adapter. No
426 * further processing will be done besides deleting the Fib. We
427 * will have a debug mode where the adapter can notify the host
428 * it had a problem and the host can log that fact.
431 if (wait
&& !reply
) {
433 } else if (!wait
&& reply
) {
434 hw_fib
->header
.XferState
|= cpu_to_le32(Async
| ResponseExpected
);
435 FIB_COUNTER_INCREMENT(aac_config
.AsyncSent
);
436 } else if (!wait
&& !reply
) {
437 hw_fib
->header
.XferState
|= cpu_to_le32(NoResponseExpected
);
438 FIB_COUNTER_INCREMENT(aac_config
.NoResponseSent
);
439 } else if (wait
&& reply
) {
440 hw_fib
->header
.XferState
|= cpu_to_le32(ResponseExpected
);
441 FIB_COUNTER_INCREMENT(aac_config
.NormalSent
);
444 * Map the fib into 32bits by using the fib number
447 hw_fib
->header
.SenderFibAddress
= cpu_to_le32(((u32
)(fibptr
- dev
->fibs
)) << 2);
448 hw_fib
->header
.Handle
= (u32
)(fibptr
- dev
->fibs
) + 1;
450 * Set FIB state to indicate where it came from and if we want a
451 * response from the adapter. Also load the command from the
454 * Map the hw fib pointer as a 32bit value
456 hw_fib
->header
.Command
= cpu_to_le16(command
);
457 hw_fib
->header
.XferState
|= cpu_to_le32(SentFromHost
);
459 * Set the size of the Fib we want to send to the adapter
461 hw_fib
->header
.Size
= cpu_to_le16(sizeof(struct aac_fibhdr
) + size
);
462 if (le16_to_cpu(hw_fib
->header
.Size
) > le16_to_cpu(hw_fib
->header
.SenderSize
)) {
466 * Get a queue entry connect the FIB to it and send an notify
467 * the adapter a command is ready.
469 hw_fib
->header
.XferState
|= cpu_to_le32(NormalPriority
);
472 * Fill in the Callback and CallbackContext if we are not
476 fibptr
->callback
= callback
;
477 fibptr
->callback_data
= callback_data
;
478 fibptr
->flags
= FIB_CONTEXT_FLAG
;
483 FIB_COUNTER_INCREMENT(aac_config
.FibsSent
);
485 dprintk((KERN_DEBUG
"Fib contents:.\n"));
486 dprintk((KERN_DEBUG
" Command = %d.\n", le32_to_cpu(hw_fib
->header
.Command
)));
487 dprintk((KERN_DEBUG
" SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount
*)fib_data(fibptr
))->command
)));
488 dprintk((KERN_DEBUG
" XferState = %x.\n", le32_to_cpu(hw_fib
->header
.XferState
)));
489 dprintk((KERN_DEBUG
" hw_fib va being sent=%p\n",fibptr
->hw_fib_va
));
490 dprintk((KERN_DEBUG
" hw_fib pa being sent=%lx\n",(ulong
)fibptr
->hw_fib_pa
));
491 dprintk((KERN_DEBUG
" fib being sent=%p\n",fibptr
));
498 spin_lock_irqsave(&dev
->manage_lock
, mflags
);
499 if (dev
->management_fib_count
>= AAC_NUM_MGT_FIB
) {
500 printk(KERN_INFO
"No management Fibs Available:%d\n",
501 dev
->management_fib_count
);
502 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
505 dev
->management_fib_count
++;
506 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
507 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
510 if (dev
->sync_mode
) {
512 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
513 spin_lock_irqsave(&dev
->sync_lock
, sflags
);
515 list_add_tail(&fibptr
->fiblink
, &dev
->sync_fib_list
);
516 spin_unlock_irqrestore(&dev
->sync_lock
, sflags
);
518 dev
->sync_fib
= fibptr
;
519 spin_unlock_irqrestore(&dev
->sync_lock
, sflags
);
520 aac_adapter_sync_cmd(dev
, SEND_SYNCHRONOUS_FIB
,
521 (u32
)fibptr
->hw_fib_pa
, 0, 0, 0, 0, 0,
522 NULL
, NULL
, NULL
, NULL
, NULL
);
525 fibptr
->flags
|= FIB_CONTEXT_FLAG_WAIT
;
526 if (down_interruptible(&fibptr
->event_wait
)) {
527 fibptr
->flags
&= ~FIB_CONTEXT_FLAG_WAIT
;
535 if (aac_adapter_deliver(fibptr
) != 0) {
536 printk(KERN_ERR
"aac_fib_send: returned -EBUSY\n");
538 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
539 spin_lock_irqsave(&dev
->manage_lock
, mflags
);
540 dev
->management_fib_count
--;
541 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
548 * If the caller wanted us to wait for response wait now.
552 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
553 /* Only set for first known interruptable command */
556 * *VERY* Dangerous to time out a command, the
557 * assumption is made that we have no hope of
558 * functioning because an interrupt routing or other
559 * hardware failure has occurred.
561 unsigned long timeout
= jiffies
+ (180 * HZ
); /* 3 minutes */
562 while (down_trylock(&fibptr
->event_wait
)) {
564 if (time_is_before_eq_jiffies(timeout
)) {
565 struct aac_queue
* q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
566 atomic_dec(&q
->numpending
);
568 printk(KERN_ERR
"aacraid: aac_fib_send: first asynchronous command timed out.\n"
569 "Usually a result of a PCI interrupt routing problem;\n"
570 "update mother board BIOS or consider utilizing one of\n"
571 "the SAFE mode kernel options (acpi, apic etc)\n");
575 if ((blink
= aac_adapter_check_health(dev
)) > 0) {
577 printk(KERN_ERR
"aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
578 "Usually a result of a serious unrecoverable hardware problem\n",
583 /* We used to udelay() here but that absorbed
584 * a CPU when a timeout occured. Not very
588 } else if (down_interruptible(&fibptr
->event_wait
)) {
589 /* Do nothing ... satisfy
590 * down_interruptible must_check */
593 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
594 if (fibptr
->done
== 0) {
595 fibptr
->done
= 2; /* Tell interrupt we aborted */
596 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
599 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
600 BUG_ON(fibptr
->done
== 0);
602 if(unlikely(fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
))
607 * If the user does not want a response than return success otherwise
617 * aac_consumer_get - get the top of the queue
620 * @entry: Return entry
622 * Will return a pointer to the entry on the top of the queue requested that
623 * we are a consumer of, and return the address of the queue entry. It does
624 * not change the state of the queue.
627 int aac_consumer_get(struct aac_dev
* dev
, struct aac_queue
* q
, struct aac_entry
**entry
)
631 if (le32_to_cpu(*q
->headers
.producer
) == le32_to_cpu(*q
->headers
.consumer
)) {
635 * The consumer index must be wrapped if we have reached
636 * the end of the queue, else we just use the entry
637 * pointed to by the header index
639 if (le32_to_cpu(*q
->headers
.consumer
) >= q
->entries
)
642 index
= le32_to_cpu(*q
->headers
.consumer
);
643 *entry
= q
->base
+ index
;
650 * aac_consumer_free - free consumer entry
655 * Frees up the current top of the queue we are a consumer of. If the
656 * queue was full notify the producer that the queue is no longer full.
659 void aac_consumer_free(struct aac_dev
* dev
, struct aac_queue
*q
, u32 qid
)
664 if ((le32_to_cpu(*q
->headers
.producer
)+1) == le32_to_cpu(*q
->headers
.consumer
))
667 if (le32_to_cpu(*q
->headers
.consumer
) >= q
->entries
)
668 *q
->headers
.consumer
= cpu_to_le32(1);
670 le32_add_cpu(q
->headers
.consumer
, 1);
675 case HostNormCmdQueue
:
676 notify
= HostNormCmdNotFull
;
678 case HostNormRespQueue
:
679 notify
= HostNormRespNotFull
;
685 aac_adapter_notify(dev
, notify
);
690 * aac_fib_adapter_complete - complete adapter issued fib
691 * @fibptr: fib to complete
694 * Will do all necessary work to complete a FIB that was sent from
698 int aac_fib_adapter_complete(struct fib
*fibptr
, unsigned short size
)
700 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
701 struct aac_dev
* dev
= fibptr
->dev
;
702 struct aac_queue
* q
;
703 unsigned long nointr
= 0;
704 unsigned long qflags
;
706 if (dev
->comm_interface
== AAC_COMM_MESSAGE_TYPE1
||
707 dev
->comm_interface
== AAC_COMM_MESSAGE_TYPE2
) {
712 if (hw_fib
->header
.XferState
== 0) {
713 if (dev
->comm_interface
== AAC_COMM_MESSAGE
)
718 * If we plan to do anything check the structure type first.
720 if (hw_fib
->header
.StructType
!= FIB_MAGIC
&&
721 hw_fib
->header
.StructType
!= FIB_MAGIC2
&&
722 hw_fib
->header
.StructType
!= FIB_MAGIC2_64
) {
723 if (dev
->comm_interface
== AAC_COMM_MESSAGE
)
728 * This block handles the case where the adapter had sent us a
729 * command and we have finished processing the command. We
730 * call completeFib when we are done processing the command
731 * and want to send a response back to the adapter. This will
732 * send the completed cdb to the adapter.
734 if (hw_fib
->header
.XferState
& cpu_to_le32(SentFromAdapter
)) {
735 if (dev
->comm_interface
== AAC_COMM_MESSAGE
) {
739 hw_fib
->header
.XferState
|= cpu_to_le32(HostProcessed
);
741 size
+= sizeof(struct aac_fibhdr
);
742 if (size
> le16_to_cpu(hw_fib
->header
.SenderSize
))
744 hw_fib
->header
.Size
= cpu_to_le16(size
);
746 q
= &dev
->queues
->queue
[AdapNormRespQueue
];
747 spin_lock_irqsave(q
->lock
, qflags
);
748 aac_queue_get(dev
, &index
, AdapNormRespQueue
, hw_fib
, 1, NULL
, &nointr
);
749 *(q
->headers
.producer
) = cpu_to_le32(index
+ 1);
750 spin_unlock_irqrestore(q
->lock
, qflags
);
751 if (!(nointr
& (int)aac_config
.irq_mod
))
752 aac_adapter_notify(dev
, AdapNormRespQueue
);
755 printk(KERN_WARNING
"aac_fib_adapter_complete: "
756 "Unknown xferstate detected.\n");
763 * aac_fib_complete - fib completion handler
764 * @fib: FIB to complete
766 * Will do all necessary work to complete a FIB.
769 int aac_fib_complete(struct fib
*fibptr
)
771 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
774 * Check for a fib which has already been completed
777 if (hw_fib
->header
.XferState
== 0)
780 * If we plan to do anything check the structure type first.
783 if (hw_fib
->header
.StructType
!= FIB_MAGIC
&&
784 hw_fib
->header
.StructType
!= FIB_MAGIC2
&&
785 hw_fib
->header
.StructType
!= FIB_MAGIC2_64
)
788 * This block completes a cdb which orginated on the host and we
789 * just need to deallocate the cdb or reinit it. At this point the
790 * command is complete that we had sent to the adapter and this
791 * cdb could be reused.
794 if((hw_fib
->header
.XferState
& cpu_to_le32(SentFromHost
)) &&
795 (hw_fib
->header
.XferState
& cpu_to_le32(AdapterProcessed
)))
799 else if(hw_fib
->header
.XferState
& cpu_to_le32(SentFromHost
))
802 * This handles the case when the host has aborted the I/O
803 * to the adapter because the adapter is not responding
806 } else if(hw_fib
->header
.XferState
& cpu_to_le32(HostOwned
)) {
815 * aac_printf - handle printf from firmware
819 * Print a message passed to us by the controller firmware on the
823 void aac_printf(struct aac_dev
*dev
, u32 val
)
825 char *cp
= dev
->printfbuf
;
826 if (dev
->printf_enabled
)
828 int length
= val
& 0xffff;
829 int level
= (val
>> 16) & 0xffff;
832 * The size of the printfbuf is set in port.c
833 * There is no variable or define for it
839 if (level
== LOG_AAC_HIGH_ERROR
)
840 printk(KERN_WARNING
"%s:%s", dev
->name
, cp
);
842 printk(KERN_INFO
"%s:%s", dev
->name
, cp
);
849 * aac_handle_aif - Handle a message from the firmware
850 * @dev: Which adapter this fib is from
851 * @fibptr: Pointer to fibptr from adapter
853 * This routine handles a driver notify fib from the adapter and
854 * dispatches it to the appropriate routine for handling.
857 #define AIF_SNIFF_TIMEOUT (500*HZ)
858 static void aac_handle_aif(struct aac_dev
* dev
, struct fib
* fibptr
)
860 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
861 struct aac_aifcmd
* aifcmd
= (struct aac_aifcmd
*)hw_fib
->data
;
862 u32 channel
, id
, lun
, container
;
863 struct scsi_device
*device
;
869 } device_config_needed
= NOTHING
;
871 /* Sniff for container changes */
873 if (!dev
|| !dev
->fsa_dev
)
875 container
= channel
= id
= lun
= (u32
)-1;
878 * We have set this up to try and minimize the number of
879 * re-configures that take place. As a result of this when
880 * certain AIF's come in we will set a flag waiting for another
881 * type of AIF before setting the re-config flag.
883 switch (le32_to_cpu(aifcmd
->command
)) {
884 case AifCmdDriverNotify
:
885 switch (le32_to_cpu(((__le32
*)aifcmd
->data
)[0])) {
886 case AifRawDeviceRemove
:
887 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
888 if ((container
>> 28)) {
892 channel
= (container
>> 24) & 0xF;
893 if (channel
>= dev
->maximum_num_channels
) {
897 id
= container
& 0xFFFF;
898 if (id
>= dev
->maximum_num_physicals
) {
902 lun
= (container
>> 16) & 0xFF;
904 channel
= aac_phys_to_logical(channel
);
905 device_config_needed
=
906 (((__le32
*)aifcmd
->data
)[0] ==
907 cpu_to_le32(AifRawDeviceRemove
)) ? DELETE
: ADD
;
909 if (device_config_needed
== ADD
) {
910 device
= scsi_device_lookup(
914 scsi_remove_device(device
);
915 scsi_device_put(device
);
920 * Morph or Expand complete
922 case AifDenMorphComplete
:
923 case AifDenVolumeExtendComplete
:
924 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
925 if (container
>= dev
->maximum_num_containers
)
929 * Find the scsi_device associated with the SCSI
930 * address. Make sure we have the right array, and if
931 * so set the flag to initiate a new re-config once we
932 * see an AifEnConfigChange AIF come through.
935 if ((dev
!= NULL
) && (dev
->scsi_host_ptr
!= NULL
)) {
936 device
= scsi_device_lookup(dev
->scsi_host_ptr
,
937 CONTAINER_TO_CHANNEL(container
),
938 CONTAINER_TO_ID(container
),
939 CONTAINER_TO_LUN(container
));
941 dev
->fsa_dev
[container
].config_needed
= CHANGE
;
942 dev
->fsa_dev
[container
].config_waiting_on
= AifEnConfigChange
;
943 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
944 scsi_device_put(device
);
950 * If we are waiting on something and this happens to be
951 * that thing then set the re-configure flag.
953 if (container
!= (u32
)-1) {
954 if (container
>= dev
->maximum_num_containers
)
956 if ((dev
->fsa_dev
[container
].config_waiting_on
==
957 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
958 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
959 dev
->fsa_dev
[container
].config_waiting_on
= 0;
960 } else for (container
= 0;
961 container
< dev
->maximum_num_containers
; ++container
) {
962 if ((dev
->fsa_dev
[container
].config_waiting_on
==
963 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
964 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
965 dev
->fsa_dev
[container
].config_waiting_on
= 0;
969 case AifCmdEventNotify
:
970 switch (le32_to_cpu(((__le32
*)aifcmd
->data
)[0])) {
971 case AifEnBatteryEvent
:
972 dev
->cache_protected
=
973 (((__le32
*)aifcmd
->data
)[1] == cpu_to_le32(3));
978 case AifEnAddContainer
:
979 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
980 if (container
>= dev
->maximum_num_containers
)
982 dev
->fsa_dev
[container
].config_needed
= ADD
;
983 dev
->fsa_dev
[container
].config_waiting_on
=
985 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
991 case AifEnDeleteContainer
:
992 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
993 if (container
>= dev
->maximum_num_containers
)
995 dev
->fsa_dev
[container
].config_needed
= DELETE
;
996 dev
->fsa_dev
[container
].config_waiting_on
=
998 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
1002 * Container change detected. If we currently are not
1003 * waiting on something else, setup to wait on a Config Change.
1005 case AifEnContainerChange
:
1006 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1007 if (container
>= dev
->maximum_num_containers
)
1009 if (dev
->fsa_dev
[container
].config_waiting_on
&&
1010 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1012 dev
->fsa_dev
[container
].config_needed
= CHANGE
;
1013 dev
->fsa_dev
[container
].config_waiting_on
=
1015 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
1018 case AifEnConfigChange
:
1022 case AifEnDeleteJBOD
:
1023 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1024 if ((container
>> 28)) {
1025 container
= (u32
)-1;
1028 channel
= (container
>> 24) & 0xF;
1029 if (channel
>= dev
->maximum_num_channels
) {
1030 container
= (u32
)-1;
1033 id
= container
& 0xFFFF;
1034 if (id
>= dev
->maximum_num_physicals
) {
1035 container
= (u32
)-1;
1038 lun
= (container
>> 16) & 0xFF;
1039 container
= (u32
)-1;
1040 channel
= aac_phys_to_logical(channel
);
1041 device_config_needed
=
1042 (((__le32
*)aifcmd
->data
)[0] ==
1043 cpu_to_le32(AifEnAddJBOD
)) ? ADD
: DELETE
;
1044 if (device_config_needed
== ADD
) {
1045 device
= scsi_device_lookup(dev
->scsi_host_ptr
,
1050 scsi_remove_device(device
);
1051 scsi_device_put(device
);
1056 case AifEnEnclosureManagement
:
1058 * If in JBOD mode, automatic exposure of new
1059 * physical target to be suppressed until configured.
1063 switch (le32_to_cpu(((__le32
*)aifcmd
->data
)[3])) {
1064 case EM_DRIVE_INSERTION
:
1065 case EM_DRIVE_REMOVAL
:
1066 case EM_SES_DRIVE_INSERTION
:
1067 case EM_SES_DRIVE_REMOVAL
:
1068 container
= le32_to_cpu(
1069 ((__le32
*)aifcmd
->data
)[2]);
1070 if ((container
>> 28)) {
1071 container
= (u32
)-1;
1074 channel
= (container
>> 24) & 0xF;
1075 if (channel
>= dev
->maximum_num_channels
) {
1076 container
= (u32
)-1;
1079 id
= container
& 0xFFFF;
1080 lun
= (container
>> 16) & 0xFF;
1081 container
= (u32
)-1;
1082 if (id
>= dev
->maximum_num_physicals
) {
1083 /* legacy dev_t ? */
1084 if ((0x2000 <= id
) || lun
|| channel
||
1085 ((channel
= (id
>> 7) & 0x3F) >=
1086 dev
->maximum_num_channels
))
1088 lun
= (id
>> 4) & 7;
1091 channel
= aac_phys_to_logical(channel
);
1092 device_config_needed
=
1093 ((((__le32
*)aifcmd
->data
)[3]
1094 == cpu_to_le32(EM_DRIVE_INSERTION
)) ||
1095 (((__le32
*)aifcmd
->data
)[3]
1096 == cpu_to_le32(EM_SES_DRIVE_INSERTION
))) ?
1104 * If we are waiting on something and this happens to be
1105 * that thing then set the re-configure flag.
1107 if (container
!= (u32
)-1) {
1108 if (container
>= dev
->maximum_num_containers
)
1110 if ((dev
->fsa_dev
[container
].config_waiting_on
==
1111 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
1112 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1113 dev
->fsa_dev
[container
].config_waiting_on
= 0;
1114 } else for (container
= 0;
1115 container
< dev
->maximum_num_containers
; ++container
) {
1116 if ((dev
->fsa_dev
[container
].config_waiting_on
==
1117 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
1118 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1119 dev
->fsa_dev
[container
].config_waiting_on
= 0;
1123 case AifCmdJobProgress
:
1125 * These are job progress AIF's. When a Clear is being
1126 * done on a container it is initially created then hidden from
1127 * the OS. When the clear completes we don't get a config
1128 * change so we monitor the job status complete on a clear then
1129 * wait for a container change.
1132 if (((__le32
*)aifcmd
->data
)[1] == cpu_to_le32(AifJobCtrZero
) &&
1133 (((__le32
*)aifcmd
->data
)[6] == ((__le32
*)aifcmd
->data
)[5] ||
1134 ((__le32
*)aifcmd
->data
)[4] == cpu_to_le32(AifJobStsSuccess
))) {
1136 container
< dev
->maximum_num_containers
;
1139 * Stomp on all config sequencing for all
1142 dev
->fsa_dev
[container
].config_waiting_on
=
1143 AifEnContainerChange
;
1144 dev
->fsa_dev
[container
].config_needed
= ADD
;
1145 dev
->fsa_dev
[container
].config_waiting_stamp
=
1149 if (((__le32
*)aifcmd
->data
)[1] == cpu_to_le32(AifJobCtrZero
) &&
1150 ((__le32
*)aifcmd
->data
)[6] == 0 &&
1151 ((__le32
*)aifcmd
->data
)[4] == cpu_to_le32(AifJobStsRunning
)) {
1153 container
< dev
->maximum_num_containers
;
1156 * Stomp on all config sequencing for all
1159 dev
->fsa_dev
[container
].config_waiting_on
=
1160 AifEnContainerChange
;
1161 dev
->fsa_dev
[container
].config_needed
= DELETE
;
1162 dev
->fsa_dev
[container
].config_waiting_stamp
=
1171 if (device_config_needed
== NOTHING
)
1172 for (; container
< dev
->maximum_num_containers
; ++container
) {
1173 if ((dev
->fsa_dev
[container
].config_waiting_on
== 0) &&
1174 (dev
->fsa_dev
[container
].config_needed
!= NOTHING
) &&
1175 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
)) {
1176 device_config_needed
=
1177 dev
->fsa_dev
[container
].config_needed
;
1178 dev
->fsa_dev
[container
].config_needed
= NOTHING
;
1179 channel
= CONTAINER_TO_CHANNEL(container
);
1180 id
= CONTAINER_TO_ID(container
);
1181 lun
= CONTAINER_TO_LUN(container
);
1185 if (device_config_needed
== NOTHING
)
1189 * If we decided that a re-configuration needs to be done,
1190 * schedule it here on the way out the door, please close the door
1195 * Find the scsi_device associated with the SCSI address,
1196 * and mark it as changed, invalidating the cache. This deals
1197 * with changes to existing device IDs.
1200 if (!dev
|| !dev
->scsi_host_ptr
)
1203 * force reload of disk info via aac_probe_container
1205 if ((channel
== CONTAINER_CHANNEL
) &&
1206 (device_config_needed
!= NOTHING
)) {
1207 if (dev
->fsa_dev
[container
].valid
== 1)
1208 dev
->fsa_dev
[container
].valid
= 2;
1209 aac_probe_container(dev
, container
);
1211 device
= scsi_device_lookup(dev
->scsi_host_ptr
, channel
, id
, lun
);
1213 switch (device_config_needed
) {
1215 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1216 scsi_remove_device(device
);
1218 if (scsi_device_online(device
)) {
1219 scsi_device_set_state(device
, SDEV_OFFLINE
);
1220 sdev_printk(KERN_INFO
, device
,
1221 "Device offlined - %s\n",
1222 (channel
== CONTAINER_CHANNEL
) ?
1224 "enclosure services event");
1229 if (!scsi_device_online(device
)) {
1230 sdev_printk(KERN_INFO
, device
,
1231 "Device online - %s\n",
1232 (channel
== CONTAINER_CHANNEL
) ?
1234 "enclosure services event");
1235 scsi_device_set_state(device
, SDEV_RUNNING
);
1239 if ((channel
== CONTAINER_CHANNEL
)
1240 && (!dev
->fsa_dev
[container
].valid
)) {
1241 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1242 scsi_remove_device(device
);
1244 if (!scsi_device_online(device
))
1246 scsi_device_set_state(device
, SDEV_OFFLINE
);
1247 sdev_printk(KERN_INFO
, device
,
1248 "Device offlined - %s\n",
1253 scsi_rescan_device(&device
->sdev_gendev
);
1258 scsi_device_put(device
);
1259 device_config_needed
= NOTHING
;
1261 if (device_config_needed
== ADD
)
1262 scsi_add_device(dev
->scsi_host_ptr
, channel
, id
, lun
);
1263 if (channel
== CONTAINER_CHANNEL
) {
1265 device_config_needed
= NOTHING
;
1270 static int _aac_reset_adapter(struct aac_dev
*aac
, int forced
)
1274 struct Scsi_Host
*host
;
1275 struct scsi_device
*dev
;
1276 struct scsi_cmnd
*command
;
1277 struct scsi_cmnd
*command_list
;
1282 * - host is locked, unless called by the aacraid thread.
1283 * (a matter of convenience, due to legacy issues surrounding
1284 * eh_host_adapter_reset).
1285 * - in_reset is asserted, so no new i/o is getting to the
1287 * - The card is dead, or will be very shortly ;-/ so no new
1288 * commands are completing in the interrupt service.
1290 host
= aac
->scsi_host_ptr
;
1291 scsi_block_requests(host
);
1292 aac_adapter_disable_int(aac
);
1293 if (aac
->thread
->pid
!= current
->pid
) {
1294 spin_unlock_irq(host
->host_lock
);
1295 kthread_stop(aac
->thread
);
1300 * If a positive health, means in a known DEAD PANIC
1301 * state and the adapter could be reset to `try again'.
1303 retval
= aac_adapter_restart(aac
, forced
? 0 : aac_adapter_check_health(aac
));
1309 * Loop through the fibs, close the synchronous FIBS
1311 for (retval
= 1, index
= 0; index
< (aac
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
); index
++) {
1312 struct fib
*fib
= &aac
->fibs
[index
];
1313 if (!(fib
->hw_fib_va
->header
.XferState
& cpu_to_le32(NoResponseExpected
| Async
)) &&
1314 (fib
->hw_fib_va
->header
.XferState
& cpu_to_le32(ResponseExpected
))) {
1315 unsigned long flagv
;
1316 spin_lock_irqsave(&fib
->event_lock
, flagv
);
1317 up(&fib
->event_wait
);
1318 spin_unlock_irqrestore(&fib
->event_lock
, flagv
);
1323 /* Give some extra time for ioctls to complete. */
1326 index
= aac
->cardtype
;
1329 * Re-initialize the adapter, first free resources, then carefully
1330 * apply the initialization sequence to come back again. Only risk
1331 * is a change in Firmware dropping cache, it is assumed the caller
1332 * will ensure that i/o is queisced and the card is flushed in that
1335 aac_fib_map_free(aac
);
1336 pci_free_consistent(aac
->pdev
, aac
->comm_size
, aac
->comm_addr
, aac
->comm_phys
);
1337 aac
->comm_addr
= NULL
;
1342 kfree(aac
->fsa_dev
);
1343 aac
->fsa_dev
= NULL
;
1344 quirks
= aac_get_driver_ident(index
)->quirks
;
1345 if (quirks
& AAC_QUIRK_31BIT
) {
1346 if (((retval
= pci_set_dma_mask(aac
->pdev
, DMA_BIT_MASK(31)))) ||
1347 ((retval
= pci_set_consistent_dma_mask(aac
->pdev
, DMA_BIT_MASK(31)))))
1350 if (((retval
= pci_set_dma_mask(aac
->pdev
, DMA_BIT_MASK(32)))) ||
1351 ((retval
= pci_set_consistent_dma_mask(aac
->pdev
, DMA_BIT_MASK(32)))))
1354 if ((retval
= (*(aac_get_driver_ident(index
)->init
))(aac
)))
1356 if (quirks
& AAC_QUIRK_31BIT
)
1357 if ((retval
= pci_set_dma_mask(aac
->pdev
, DMA_BIT_MASK(32))))
1360 aac
->thread
= kthread_run(aac_command_thread
, aac
, "%s",
1362 if (IS_ERR(aac
->thread
)) {
1363 retval
= PTR_ERR(aac
->thread
);
1367 (void)aac_get_adapter_info(aac
);
1368 if ((quirks
& AAC_QUIRK_34SG
) && (host
->sg_tablesize
> 34)) {
1369 host
->sg_tablesize
= 34;
1370 host
->max_sectors
= (host
->sg_tablesize
* 8) + 112;
1372 if ((quirks
& AAC_QUIRK_17SG
) && (host
->sg_tablesize
> 17)) {
1373 host
->sg_tablesize
= 17;
1374 host
->max_sectors
= (host
->sg_tablesize
* 8) + 112;
1376 aac_get_config_status(aac
, 1);
1377 aac_get_containers(aac
);
1379 * This is where the assumption that the Adapter is quiesced
1382 command_list
= NULL
;
1383 __shost_for_each_device(dev
, host
) {
1384 unsigned long flags
;
1385 spin_lock_irqsave(&dev
->list_lock
, flags
);
1386 list_for_each_entry(command
, &dev
->cmd_list
, list
)
1387 if (command
->SCp
.phase
== AAC_OWNER_FIRMWARE
) {
1388 command
->SCp
.buffer
= (struct scatterlist
*)command_list
;
1389 command_list
= command
;
1391 spin_unlock_irqrestore(&dev
->list_lock
, flags
);
1393 while ((command
= command_list
)) {
1394 command_list
= (struct scsi_cmnd
*)command
->SCp
.buffer
;
1395 command
->SCp
.buffer
= NULL
;
1396 command
->result
= DID_OK
<< 16
1397 | COMMAND_COMPLETE
<< 8
1398 | SAM_STAT_TASK_SET_FULL
;
1399 command
->SCp
.phase
= AAC_OWNER_ERROR_HANDLER
;
1400 command
->scsi_done(command
);
1406 scsi_unblock_requests(host
);
1408 spin_lock_irq(host
->host_lock
);
1413 int aac_reset_adapter(struct aac_dev
* aac
, int forced
)
1415 unsigned long flagv
= 0;
1417 struct Scsi_Host
* host
;
1419 if (spin_trylock_irqsave(&aac
->fib_lock
, flagv
) == 0)
1422 if (aac
->in_reset
) {
1423 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1427 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1430 * Wait for all commands to complete to this specific
1431 * target (block maximum 60 seconds). Although not necessary,
1432 * it does make us a good storage citizen.
1434 host
= aac
->scsi_host_ptr
;
1435 scsi_block_requests(host
);
1436 if (forced
< 2) for (retval
= 60; retval
; --retval
) {
1437 struct scsi_device
* dev
;
1438 struct scsi_cmnd
* command
;
1441 __shost_for_each_device(dev
, host
) {
1442 spin_lock_irqsave(&dev
->list_lock
, flagv
);
1443 list_for_each_entry(command
, &dev
->cmd_list
, list
) {
1444 if (command
->SCp
.phase
== AAC_OWNER_FIRMWARE
) {
1449 spin_unlock_irqrestore(&dev
->list_lock
, flagv
);
1455 * We can exit If all the commands are complete
1462 /* Quiesce build, flush cache, write through mode */
1464 aac_send_shutdown(aac
);
1465 spin_lock_irqsave(host
->host_lock
, flagv
);
1466 retval
= _aac_reset_adapter(aac
, forced
? forced
: ((aac_check_reset
!= 0) && (aac_check_reset
!= 1)));
1467 spin_unlock_irqrestore(host
->host_lock
, flagv
);
1469 if ((forced
< 2) && (retval
== -ENODEV
)) {
1470 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1471 struct fib
* fibctx
= aac_fib_alloc(aac
);
1473 struct aac_pause
*cmd
;
1476 aac_fib_init(fibctx
);
1478 cmd
= (struct aac_pause
*) fib_data(fibctx
);
1480 cmd
->command
= cpu_to_le32(VM_ContainerConfig
);
1481 cmd
->type
= cpu_to_le32(CT_PAUSE_IO
);
1482 cmd
->timeout
= cpu_to_le32(1);
1483 cmd
->min
= cpu_to_le32(1);
1484 cmd
->noRescan
= cpu_to_le32(1);
1485 cmd
->count
= cpu_to_le32(0);
1487 status
= aac_fib_send(ContainerCommand
,
1489 sizeof(struct aac_pause
),
1491 -2 /* Timeout silently */, 1,
1495 aac_fib_complete(fibctx
);
1496 /* FIB should be freed only after getting
1497 * the response from the F/W */
1498 if (status
!= -ERESTARTSYS
)
1499 aac_fib_free(fibctx
);
1506 int aac_check_health(struct aac_dev
* aac
)
1509 unsigned long time_now
, flagv
= 0;
1510 struct list_head
* entry
;
1511 struct Scsi_Host
* host
;
1513 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1514 if (spin_trylock_irqsave(&aac
->fib_lock
, flagv
) == 0)
1517 if (aac
->in_reset
|| !(BlinkLED
= aac_adapter_check_health(aac
))) {
1518 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1525 * aac_aifcmd.command = AifCmdEventNotify = 1
1526 * aac_aifcmd.seqnum = 0xFFFFFFFF
1527 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1528 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1529 * aac.aifcmd.data[2] = AifHighPriority = 3
1530 * aac.aifcmd.data[3] = BlinkLED
1533 time_now
= jiffies
/HZ
;
1534 entry
= aac
->fib_list
.next
;
1537 * For each Context that is on the
1538 * fibctxList, make a copy of the
1539 * fib, and then set the event to wake up the
1540 * thread that is waiting for it.
1542 while (entry
!= &aac
->fib_list
) {
1544 * Extract the fibctx
1546 struct aac_fib_context
*fibctx
= list_entry(entry
, struct aac_fib_context
, next
);
1547 struct hw_fib
* hw_fib
;
1550 * Check if the queue is getting
1553 if (fibctx
->count
> 20) {
1555 * It's *not* jiffies folks,
1556 * but jiffies / HZ, so do not
1559 u32 time_last
= fibctx
->jiffies
;
1561 * Has it been > 2 minutes
1562 * since the last read off
1565 if ((time_now
- time_last
) > aif_timeout
) {
1566 entry
= entry
->next
;
1567 aac_close_fib_context(aac
, fibctx
);
1572 * Warning: no sleep allowed while
1575 hw_fib
= kzalloc(sizeof(struct hw_fib
), GFP_ATOMIC
);
1576 fib
= kzalloc(sizeof(struct fib
), GFP_ATOMIC
);
1577 if (fib
&& hw_fib
) {
1578 struct aac_aifcmd
* aif
;
1580 fib
->hw_fib_va
= hw_fib
;
1583 fib
->type
= FSAFS_NTC_FIB_CONTEXT
;
1584 fib
->size
= sizeof (struct fib
);
1585 fib
->data
= hw_fib
->data
;
1586 aif
= (struct aac_aifcmd
*)hw_fib
->data
;
1587 aif
->command
= cpu_to_le32(AifCmdEventNotify
);
1588 aif
->seqnum
= cpu_to_le32(0xFFFFFFFF);
1589 ((__le32
*)aif
->data
)[0] = cpu_to_le32(AifEnExpEvent
);
1590 ((__le32
*)aif
->data
)[1] = cpu_to_le32(AifExeFirmwarePanic
);
1591 ((__le32
*)aif
->data
)[2] = cpu_to_le32(AifHighPriority
);
1592 ((__le32
*)aif
->data
)[3] = cpu_to_le32(BlinkLED
);
1595 * Put the FIB onto the
1598 list_add_tail(&fib
->fiblink
, &fibctx
->fib_list
);
1601 * Set the event to wake up the
1602 * thread that will waiting.
1604 up(&fibctx
->wait_sem
);
1606 printk(KERN_WARNING
"aifd: didn't allocate NewFib.\n");
1610 entry
= entry
->next
;
1613 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1616 printk(KERN_ERR
"%s: Host adapter dead %d\n", aac
->name
, BlinkLED
);
1620 printk(KERN_ERR
"%s: Host adapter BLINK LED 0x%x\n", aac
->name
, BlinkLED
);
1622 if (!aac_check_reset
|| ((aac_check_reset
== 1) &&
1623 (aac
->supplement_adapter_info
.SupportedOptions2
&
1624 AAC_OPTION_IGNORE_RESET
)))
1626 host
= aac
->scsi_host_ptr
;
1627 if (aac
->thread
->pid
!= current
->pid
)
1628 spin_lock_irqsave(host
->host_lock
, flagv
);
1629 BlinkLED
= _aac_reset_adapter(aac
, aac_check_reset
!= 1);
1630 if (aac
->thread
->pid
!= current
->pid
)
1631 spin_unlock_irqrestore(host
->host_lock
, flagv
);
1641 * aac_command_thread - command processing thread
1642 * @dev: Adapter to monitor
1644 * Waits on the commandready event in it's queue. When the event gets set
1645 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1646 * until the queue is empty. When the queue is empty it will wait for
1650 int aac_command_thread(void *data
)
1652 struct aac_dev
*dev
= data
;
1653 struct hw_fib
*hw_fib
, *hw_newfib
;
1654 struct fib
*fib
, *newfib
;
1655 struct aac_fib_context
*fibctx
;
1656 unsigned long flags
;
1657 DECLARE_WAITQUEUE(wait
, current
);
1658 unsigned long next_jiffies
= jiffies
+ HZ
;
1659 unsigned long next_check_jiffies
= next_jiffies
;
1660 long difference
= HZ
;
1663 * We can only have one thread per adapter for AIF's.
1665 if (dev
->aif_thread
)
1669 * Let the DPC know it has a place to send the AIF's to.
1671 dev
->aif_thread
= 1;
1672 add_wait_queue(&dev
->queues
->queue
[HostNormCmdQueue
].cmdready
, &wait
);
1673 set_current_state(TASK_INTERRUPTIBLE
);
1674 dprintk ((KERN_INFO
"aac_command_thread start\n"));
1676 spin_lock_irqsave(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1677 while(!list_empty(&(dev
->queues
->queue
[HostNormCmdQueue
].cmdq
))) {
1678 struct list_head
*entry
;
1679 struct aac_aifcmd
* aifcmd
;
1681 set_current_state(TASK_RUNNING
);
1683 entry
= dev
->queues
->queue
[HostNormCmdQueue
].cmdq
.next
;
1686 spin_unlock_irqrestore(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1687 fib
= list_entry(entry
, struct fib
, fiblink
);
1689 * We will process the FIB here or pass it to a
1690 * worker thread that is TBD. We Really can't
1691 * do anything at this point since we don't have
1692 * anything defined for this thread to do.
1694 hw_fib
= fib
->hw_fib_va
;
1695 memset(fib
, 0, sizeof(struct fib
));
1696 fib
->type
= FSAFS_NTC_FIB_CONTEXT
;
1697 fib
->size
= sizeof(struct fib
);
1698 fib
->hw_fib_va
= hw_fib
;
1699 fib
->data
= hw_fib
->data
;
1702 * We only handle AifRequest fibs from the adapter.
1704 aifcmd
= (struct aac_aifcmd
*) hw_fib
->data
;
1705 if (aifcmd
->command
== cpu_to_le32(AifCmdDriverNotify
)) {
1706 /* Handle Driver Notify Events */
1707 aac_handle_aif(dev
, fib
);
1708 *(__le32
*)hw_fib
->data
= cpu_to_le32(ST_OK
);
1709 aac_fib_adapter_complete(fib
, (u16
)sizeof(u32
));
1711 /* The u32 here is important and intended. We are using
1712 32bit wrapping time to fit the adapter field */
1714 u32 time_now
, time_last
;
1715 unsigned long flagv
;
1717 struct hw_fib
** hw_fib_pool
, ** hw_fib_p
;
1718 struct fib
** fib_pool
, ** fib_p
;
1721 if ((aifcmd
->command
==
1722 cpu_to_le32(AifCmdEventNotify
)) ||
1724 cpu_to_le32(AifCmdJobProgress
))) {
1725 aac_handle_aif(dev
, fib
);
1728 time_now
= jiffies
/HZ
;
1731 * Warning: no sleep allowed while
1732 * holding spinlock. We take the estimate
1733 * and pre-allocate a set of fibs outside the
1736 num
= le32_to_cpu(dev
->init
->AdapterFibsSize
)
1737 / sizeof(struct hw_fib
); /* some extra */
1738 spin_lock_irqsave(&dev
->fib_lock
, flagv
);
1739 entry
= dev
->fib_list
.next
;
1740 while (entry
!= &dev
->fib_list
) {
1741 entry
= entry
->next
;
1744 spin_unlock_irqrestore(&dev
->fib_lock
, flagv
);
1748 && ((hw_fib_pool
= kmalloc(sizeof(struct hw_fib
*) * num
, GFP_KERNEL
)))
1749 && ((fib_pool
= kmalloc(sizeof(struct fib
*) * num
, GFP_KERNEL
)))) {
1750 hw_fib_p
= hw_fib_pool
;
1752 while (hw_fib_p
< &hw_fib_pool
[num
]) {
1753 if (!(*(hw_fib_p
++) = kmalloc(sizeof(struct hw_fib
), GFP_KERNEL
))) {
1757 if (!(*(fib_p
++) = kmalloc(sizeof(struct fib
), GFP_KERNEL
))) {
1758 kfree(*(--hw_fib_p
));
1762 if ((num
= hw_fib_p
- hw_fib_pool
) == 0) {
1772 spin_lock_irqsave(&dev
->fib_lock
, flagv
);
1773 entry
= dev
->fib_list
.next
;
1775 * For each Context that is on the
1776 * fibctxList, make a copy of the
1777 * fib, and then set the event to wake up the
1778 * thread that is waiting for it.
1780 hw_fib_p
= hw_fib_pool
;
1782 while (entry
!= &dev
->fib_list
) {
1784 * Extract the fibctx
1786 fibctx
= list_entry(entry
, struct aac_fib_context
, next
);
1788 * Check if the queue is getting
1791 if (fibctx
->count
> 20)
1794 * It's *not* jiffies folks,
1795 * but jiffies / HZ so do not
1798 time_last
= fibctx
->jiffies
;
1800 * Has it been > 2 minutes
1801 * since the last read off
1804 if ((time_now
- time_last
) > aif_timeout
) {
1805 entry
= entry
->next
;
1806 aac_close_fib_context(dev
, fibctx
);
1811 * Warning: no sleep allowed while
1814 if (hw_fib_p
< &hw_fib_pool
[num
]) {
1815 hw_newfib
= *hw_fib_p
;
1816 *(hw_fib_p
++) = NULL
;
1820 * Make the copy of the FIB
1822 memcpy(hw_newfib
, hw_fib
, sizeof(struct hw_fib
));
1823 memcpy(newfib
, fib
, sizeof(struct fib
));
1824 newfib
->hw_fib_va
= hw_newfib
;
1826 * Put the FIB onto the
1829 list_add_tail(&newfib
->fiblink
, &fibctx
->fib_list
);
1832 * Set the event to wake up the
1833 * thread that is waiting.
1835 up(&fibctx
->wait_sem
);
1837 printk(KERN_WARNING
"aifd: didn't allocate NewFib.\n");
1839 entry
= entry
->next
;
1842 * Set the status of this FIB
1844 *(__le32
*)hw_fib
->data
= cpu_to_le32(ST_OK
);
1845 aac_fib_adapter_complete(fib
, sizeof(u32
));
1846 spin_unlock_irqrestore(&dev
->fib_lock
, flagv
);
1847 /* Free up the remaining resources */
1848 hw_fib_p
= hw_fib_pool
;
1850 while (hw_fib_p
< &hw_fib_pool
[num
]) {
1860 spin_lock_irqsave(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1863 * There are no more AIF's
1865 spin_unlock_irqrestore(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1868 * Background activity
1870 if ((time_before(next_check_jiffies
,next_jiffies
))
1871 && ((difference
= next_check_jiffies
- jiffies
) <= 0)) {
1872 next_check_jiffies
= next_jiffies
;
1873 if (aac_check_health(dev
) == 0) {
1874 difference
= ((long)(unsigned)check_interval
)
1876 next_check_jiffies
= jiffies
+ difference
;
1877 } else if (!dev
->queues
)
1880 if (!time_before(next_check_jiffies
,next_jiffies
)
1881 && ((difference
= next_jiffies
- jiffies
) <= 0)) {
1885 /* Don't even try to talk to adapter if its sick */
1886 ret
= aac_check_health(dev
);
1887 if (!ret
&& !dev
->queues
)
1889 next_check_jiffies
= jiffies
1890 + ((long)(unsigned)check_interval
)
1892 do_gettimeofday(&now
);
1894 /* Synchronize our watches */
1895 if (((1000000 - (1000000 / HZ
)) > now
.tv_usec
)
1896 && (now
.tv_usec
> (1000000 / HZ
)))
1897 difference
= (((1000000 - now
.tv_usec
) * HZ
)
1898 + 500000) / 1000000;
1899 else if (ret
== 0) {
1902 if ((fibptr
= aac_fib_alloc(dev
))) {
1906 aac_fib_init(fibptr
);
1908 info
= (__le32
*) fib_data(fibptr
);
1909 if (now
.tv_usec
> 500000)
1912 *info
= cpu_to_le32(now
.tv_sec
);
1914 status
= aac_fib_send(SendHostTime
,
1921 /* Do not set XferState to zero unless
1922 * receives a response from F/W */
1924 aac_fib_complete(fibptr
);
1925 /* FIB should be freed only after
1926 * getting the response from the F/W */
1927 if (status
!= -ERESTARTSYS
)
1928 aac_fib_free(fibptr
);
1930 difference
= (long)(unsigned)update_interval
*HZ
;
1933 difference
= 10 * HZ
;
1935 next_jiffies
= jiffies
+ difference
;
1936 if (time_before(next_check_jiffies
,next_jiffies
))
1937 difference
= next_check_jiffies
- jiffies
;
1939 if (difference
<= 0)
1941 set_current_state(TASK_INTERRUPTIBLE
);
1942 schedule_timeout(difference
);
1944 if (kthread_should_stop())
1948 remove_wait_queue(&dev
->queues
->queue
[HostNormCmdQueue
].cmdready
, &wait
);
1949 dev
->aif_thread
= 0;
1953 int aac_acquire_irq(struct aac_dev
*dev
)
1960 cpu
= cpumask_first(cpu_online_mask
);
1961 if (!dev
->sync_mode
&& dev
->msi_enabled
&& dev
->max_msix
> 1) {
1962 for (i
= 0; i
< dev
->max_msix
; i
++) {
1963 dev
->aac_msix
[i
].vector_no
= i
;
1964 dev
->aac_msix
[i
].dev
= dev
;
1965 if (request_irq(dev
->msixentry
[i
].vector
,
1966 dev
->a_ops
.adapter_intr
,
1967 0, "aacraid", &(dev
->aac_msix
[i
]))) {
1968 printk(KERN_ERR
"%s%d: Failed to register IRQ for vector %d.\n",
1969 dev
->name
, dev
->id
, i
);
1970 for (j
= 0 ; j
< i
; j
++)
1971 free_irq(dev
->msixentry
[j
].vector
,
1972 &(dev
->aac_msix
[j
]));
1973 pci_disable_msix(dev
->pdev
);
1976 if (irq_set_affinity_hint(dev
->msixentry
[i
].vector
,
1977 get_cpu_mask(cpu
))) {
1978 printk(KERN_ERR
"%s%d: Failed to set IRQ affinity for cpu %d\n",
1979 dev
->name
, dev
->id
, cpu
);
1981 cpu
= cpumask_next(cpu
, cpu_online_mask
);
1984 dev
->aac_msix
[0].vector_no
= 0;
1985 dev
->aac_msix
[0].dev
= dev
;
1987 if (request_irq(dev
->pdev
->irq
, dev
->a_ops
.adapter_intr
,
1988 IRQF_SHARED
, "aacraid",
1989 &(dev
->aac_msix
[0])) < 0) {
1991 pci_disable_msi(dev
->pdev
);
1992 printk(KERN_ERR
"%s%d: Interrupt unavailable.\n",
1993 dev
->name
, dev
->id
);
2000 void aac_free_irq(struct aac_dev
*dev
)
2005 cpu
= cpumask_first(cpu_online_mask
);
2006 if (dev
->pdev
->device
== PMC_DEVICE_S6
||
2007 dev
->pdev
->device
== PMC_DEVICE_S7
||
2008 dev
->pdev
->device
== PMC_DEVICE_S8
||
2009 dev
->pdev
->device
== PMC_DEVICE_S9
) {
2010 if (dev
->max_msix
> 1) {
2011 for (i
= 0; i
< dev
->max_msix
; i
++) {
2012 if (irq_set_affinity_hint(
2013 dev
->msixentry
[i
].vector
, NULL
)) {
2014 printk(KERN_ERR
"%s%d: Failed to reset IRQ affinity for cpu %d\n",
2015 dev
->name
, dev
->id
, cpu
);
2017 cpu
= cpumask_next(cpu
, cpu_online_mask
);
2018 free_irq(dev
->msixentry
[i
].vector
,
2019 &(dev
->aac_msix
[i
]));
2022 free_irq(dev
->pdev
->irq
, &(dev
->aac_msix
[0]));
2025 free_irq(dev
->pdev
->irq
, dev
);
2028 pci_disable_msi(dev
->pdev
);
2029 else if (dev
->max_msix
> 1)
2030 pci_disable_msix(dev
->pdev
);