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 <linux/bcd.h>
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_host.h>
49 #include <scsi/scsi_device.h>
50 #include <scsi/scsi_cmnd.h>
55 * fib_map_alloc - allocate the fib objects
56 * @dev: Adapter to allocate for
58 * Allocate and map the shared PCI space for the FIB blocks used to
59 * talk to the Adaptec firmware.
62 static int fib_map_alloc(struct aac_dev
*dev
)
64 if (dev
->max_fib_size
> AAC_MAX_NATIVE_SIZE
)
65 dev
->max_cmd_size
= AAC_MAX_NATIVE_SIZE
;
67 dev
->max_cmd_size
= dev
->max_fib_size
;
68 if (dev
->max_fib_size
< AAC_MAX_NATIVE_SIZE
) {
69 dev
->max_cmd_size
= AAC_MAX_NATIVE_SIZE
;
71 dev
->max_cmd_size
= dev
->max_fib_size
;
75 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
76 dev
->pdev
, dev
->max_cmd_size
, dev
->scsi_host_ptr
->can_queue
,
77 AAC_NUM_MGT_FIB
, &dev
->hw_fib_pa
));
78 dev
->hw_fib_va
= pci_alloc_consistent(dev
->pdev
,
79 (dev
->max_cmd_size
+ sizeof(struct aac_fib_xporthdr
))
80 * (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
) + (ALIGN32
- 1),
82 if (dev
->hw_fib_va
== NULL
)
88 * aac_fib_map_free - free the fib objects
89 * @dev: Adapter to free
91 * Free the PCI mappings and the memory allocated for FIB blocks
95 void aac_fib_map_free(struct aac_dev
*dev
)
97 if (dev
->hw_fib_va
&& dev
->max_cmd_size
) {
98 pci_free_consistent(dev
->pdev
,
100 (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
)),
101 dev
->hw_fib_va
, dev
->hw_fib_pa
);
103 dev
->hw_fib_va
= NULL
;
107 void aac_fib_vector_assign(struct aac_dev
*dev
)
111 struct fib
*fibptr
= NULL
;
113 for (i
= 0, fibptr
= &dev
->fibs
[i
];
114 i
< (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
);
116 if ((dev
->max_msix
== 1) ||
117 (i
> ((dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
- 1)
118 - dev
->vector_cap
))) {
119 fibptr
->vector_no
= 0;
121 fibptr
->vector_no
= vector
;
123 if (vector
== dev
->max_msix
)
130 * aac_fib_setup - setup the fibs
131 * @dev: Adapter to set up
133 * Allocate the PCI space for the fibs, map it and then initialise the
134 * fib area, the unmapped fib data and also the free list
137 int aac_fib_setup(struct aac_dev
* dev
)
140 struct hw_fib
*hw_fib
;
141 dma_addr_t hw_fib_pa
;
145 while (((i
= fib_map_alloc(dev
)) == -ENOMEM
)
146 && (dev
->scsi_host_ptr
->can_queue
> (64 - AAC_NUM_MGT_FIB
))) {
147 max_cmds
= (dev
->scsi_host_ptr
->can_queue
+AAC_NUM_MGT_FIB
) >> 1;
148 dev
->scsi_host_ptr
->can_queue
= max_cmds
- AAC_NUM_MGT_FIB
;
149 if (dev
->comm_interface
!= AAC_COMM_MESSAGE_TYPE3
)
150 dev
->init
->r7
.max_io_commands
= cpu_to_le32(max_cmds
);
155 /* 32 byte alignment for PMC */
156 hw_fib_pa
= (dev
->hw_fib_pa
+ (ALIGN32
- 1)) & ~(ALIGN32
- 1);
157 dev
->hw_fib_va
= (struct hw_fib
*)((unsigned char *)dev
->hw_fib_va
+
158 (hw_fib_pa
- dev
->hw_fib_pa
));
159 dev
->hw_fib_pa
= hw_fib_pa
;
160 memset(dev
->hw_fib_va
, 0,
161 (dev
->max_cmd_size
+ sizeof(struct aac_fib_xporthdr
)) *
162 (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
));
164 /* add Xport header */
165 dev
->hw_fib_va
= (struct hw_fib
*)((unsigned char *)dev
->hw_fib_va
+
166 sizeof(struct aac_fib_xporthdr
));
167 dev
->hw_fib_pa
+= sizeof(struct aac_fib_xporthdr
);
169 hw_fib
= dev
->hw_fib_va
;
170 hw_fib_pa
= dev
->hw_fib_pa
;
172 * Initialise the fibs
174 for (i
= 0, fibptr
= &dev
->fibs
[i
];
175 i
< (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
);
179 fibptr
->size
= sizeof(struct fib
);
181 fibptr
->hw_fib_va
= hw_fib
;
182 fibptr
->data
= (void *) fibptr
->hw_fib_va
->data
;
183 fibptr
->next
= fibptr
+1; /* Forward chain the fibs */
184 sema_init(&fibptr
->event_wait
, 0);
185 spin_lock_init(&fibptr
->event_lock
);
186 hw_fib
->header
.XferState
= cpu_to_le32(0xffffffff);
187 hw_fib
->header
.SenderSize
=
188 cpu_to_le16(dev
->max_fib_size
); /* ?? max_cmd_size */
189 fibptr
->hw_fib_pa
= hw_fib_pa
;
190 fibptr
->hw_sgl_pa
= hw_fib_pa
+
191 offsetof(struct aac_hba_cmd_req
, sge
[2]);
193 * one element is for the ptr to the separate sg list,
194 * second element for 32 byte alignment
196 fibptr
->hw_error_pa
= hw_fib_pa
+
197 offsetof(struct aac_native_hba
, resp
.resp_bytes
[0]);
199 hw_fib
= (struct hw_fib
*)((unsigned char *)hw_fib
+
200 dev
->max_cmd_size
+ sizeof(struct aac_fib_xporthdr
));
201 hw_fib_pa
= hw_fib_pa
+
202 dev
->max_cmd_size
+ sizeof(struct aac_fib_xporthdr
);
206 *Assign vector numbers to fibs
208 aac_fib_vector_assign(dev
);
211 * Add the fib chain to the free list
213 dev
->fibs
[dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
- 1].next
= NULL
;
215 * Set 8 fibs aside for management tools
217 dev
->free_fib
= &dev
->fibs
[dev
->scsi_host_ptr
->can_queue
];
222 * aac_fib_alloc_tag-allocate a fib using tags
223 * @dev: Adapter to allocate the fib for
225 * Allocate a fib from the adapter fib pool using tags
226 * from the blk layer.
229 struct fib
*aac_fib_alloc_tag(struct aac_dev
*dev
, struct scsi_cmnd
*scmd
)
233 fibptr
= &dev
->fibs
[scmd
->request
->tag
];
235 * Null out fields that depend on being zero at the start of
238 fibptr
->hw_fib_va
->header
.XferState
= 0;
239 fibptr
->type
= FSAFS_NTC_FIB_CONTEXT
;
240 fibptr
->callback_data
= NULL
;
241 fibptr
->callback
= NULL
;
247 * aac_fib_alloc - allocate a fib
248 * @dev: Adapter to allocate the fib for
250 * Allocate a fib from the adapter fib pool. If the pool is empty we
254 struct fib
*aac_fib_alloc(struct aac_dev
*dev
)
258 spin_lock_irqsave(&dev
->fib_lock
, flags
);
259 fibptr
= dev
->free_fib
;
261 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
264 dev
->free_fib
= fibptr
->next
;
265 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
267 * Set the proper node type code and node byte size
269 fibptr
->type
= FSAFS_NTC_FIB_CONTEXT
;
270 fibptr
->size
= sizeof(struct fib
);
272 * Null out fields that depend on being zero at the start of
275 fibptr
->hw_fib_va
->header
.XferState
= 0;
277 fibptr
->callback
= NULL
;
278 fibptr
->callback_data
= NULL
;
284 * aac_fib_free - free a fib
285 * @fibptr: fib to free up
287 * Frees up a fib and places it on the appropriate queue
290 void aac_fib_free(struct fib
*fibptr
)
294 if (fibptr
->done
== 2)
297 spin_lock_irqsave(&fibptr
->dev
->fib_lock
, flags
);
298 if (unlikely(fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
))
299 aac_config
.fib_timeouts
++;
300 if (!(fibptr
->flags
& FIB_CONTEXT_FLAG_NATIVE_HBA
) &&
301 fibptr
->hw_fib_va
->header
.XferState
!= 0) {
302 printk(KERN_WARNING
"aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
304 le32_to_cpu(fibptr
->hw_fib_va
->header
.XferState
));
306 fibptr
->next
= fibptr
->dev
->free_fib
;
307 fibptr
->dev
->free_fib
= fibptr
;
308 spin_unlock_irqrestore(&fibptr
->dev
->fib_lock
, flags
);
312 * aac_fib_init - initialise a fib
313 * @fibptr: The fib to initialize
315 * Set up the generic fib fields ready for use
318 void aac_fib_init(struct fib
*fibptr
)
320 struct hw_fib
*hw_fib
= fibptr
->hw_fib_va
;
322 memset(&hw_fib
->header
, 0, sizeof(struct aac_fibhdr
));
323 hw_fib
->header
.StructType
= FIB_MAGIC
;
324 hw_fib
->header
.Size
= cpu_to_le16(fibptr
->dev
->max_fib_size
);
325 hw_fib
->header
.XferState
= cpu_to_le32(HostOwned
| FibInitialized
| FibEmpty
| FastResponseCapable
);
326 hw_fib
->header
.u
.ReceiverFibAddress
= cpu_to_le32(fibptr
->hw_fib_pa
);
327 hw_fib
->header
.SenderSize
= cpu_to_le16(fibptr
->dev
->max_fib_size
);
331 * fib_deallocate - deallocate a fib
332 * @fibptr: fib to deallocate
334 * Will deallocate and return to the free pool the FIB pointed to by the
338 static void fib_dealloc(struct fib
* fibptr
)
340 struct hw_fib
*hw_fib
= fibptr
->hw_fib_va
;
341 hw_fib
->header
.XferState
= 0;
345 * Commuication primitives define and support the queuing method we use to
346 * support host to adapter commuication. All queue accesses happen through
347 * these routines and are the only routines which have a knowledge of the
348 * how these queues are implemented.
352 * aac_get_entry - get a queue entry
355 * @entry: Entry return
356 * @index: Index return
357 * @nonotify: notification control
359 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
360 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
364 static int aac_get_entry (struct aac_dev
* dev
, u32 qid
, struct aac_entry
**entry
, u32
* index
, unsigned long *nonotify
)
366 struct aac_queue
* q
;
370 * All of the queues wrap when they reach the end, so we check
371 * to see if they have reached the end and if they have we just
372 * set the index back to zero. This is a wrap. You could or off
373 * the high bits in all updates but this is a bit faster I think.
376 q
= &dev
->queues
->queue
[qid
];
378 idx
= *index
= le32_to_cpu(*(q
->headers
.producer
));
379 /* Interrupt Moderation, only interrupt for first two entries */
380 if (idx
!= le32_to_cpu(*(q
->headers
.consumer
))) {
382 if (qid
== AdapNormCmdQueue
)
383 idx
= ADAP_NORM_CMD_ENTRIES
;
385 idx
= ADAP_NORM_RESP_ENTRIES
;
387 if (idx
!= le32_to_cpu(*(q
->headers
.consumer
)))
391 if (qid
== AdapNormCmdQueue
) {
392 if (*index
>= ADAP_NORM_CMD_ENTRIES
)
393 *index
= 0; /* Wrap to front of the Producer Queue. */
395 if (*index
>= ADAP_NORM_RESP_ENTRIES
)
396 *index
= 0; /* Wrap to front of the Producer Queue. */
400 if ((*index
+ 1) == le32_to_cpu(*(q
->headers
.consumer
))) {
401 printk(KERN_WARNING
"Queue %d full, %u outstanding.\n",
402 qid
, atomic_read(&q
->numpending
));
405 *entry
= q
->base
+ *index
;
411 * aac_queue_get - get the next free QE
413 * @index: Returned index
414 * @priority: Priority of fib
415 * @fib: Fib to associate with the queue entry
416 * @wait: Wait if queue full
417 * @fibptr: Driver fib object to go with fib
418 * @nonotify: Don't notify the adapter
420 * Gets the next free QE off the requested priorty adapter command
421 * queue and associates the Fib with the QE. The QE represented by
422 * index is ready to insert on the queue when this routine returns
426 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
)
428 struct aac_entry
* entry
= NULL
;
431 if (qid
== AdapNormCmdQueue
) {
432 /* if no entries wait for some if caller wants to */
433 while (!aac_get_entry(dev
, qid
, &entry
, index
, nonotify
)) {
434 printk(KERN_ERR
"GetEntries failed\n");
437 * Setup queue entry with a command, status and fib mapped
439 entry
->size
= cpu_to_le32(le16_to_cpu(hw_fib
->header
.Size
));
442 while (!aac_get_entry(dev
, qid
, &entry
, index
, nonotify
)) {
443 /* if no entries wait for some if caller wants to */
446 * Setup queue entry with command, status and fib mapped
448 entry
->size
= cpu_to_le32(le16_to_cpu(hw_fib
->header
.Size
));
449 entry
->addr
= hw_fib
->header
.SenderFibAddress
;
450 /* Restore adapters pointer to the FIB */
451 hw_fib
->header
.u
.ReceiverFibAddress
= hw_fib
->header
.SenderFibAddress
; /* Let the adapter now where to find its data */
455 * If MapFib is true than we need to map the Fib and put pointers
456 * in the queue entry.
459 entry
->addr
= cpu_to_le32(fibptr
->hw_fib_pa
);
464 * Define the highest level of host to adapter communication routines.
465 * These routines will support host to adapter FS commuication. These
466 * routines have no knowledge of the commuication method used. This level
467 * sends and receives FIBs. This level has no knowledge of how these FIBs
468 * get passed back and forth.
472 * aac_fib_send - send a fib to the adapter
473 * @command: Command to send
475 * @size: Size of fib data area
476 * @priority: Priority of Fib
477 * @wait: Async/sync select
478 * @reply: True if a reply is wanted
479 * @callback: Called with reply
480 * @callback_data: Passed to callback
482 * Sends the requested FIB to the adapter and optionally will wait for a
483 * response FIB. If the caller does not wish to wait for a response than
484 * an event to wait on must be supplied. This event will be set when a
485 * response FIB is received from the adapter.
488 int aac_fib_send(u16 command
, struct fib
*fibptr
, unsigned long size
,
489 int priority
, int wait
, int reply
, fib_callback callback
,
492 struct aac_dev
* dev
= fibptr
->dev
;
493 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
494 unsigned long flags
= 0;
495 unsigned long mflags
= 0;
496 unsigned long sflags
= 0;
499 if (!(hw_fib
->header
.XferState
& cpu_to_le32(HostOwned
)))
502 * There are 5 cases with the wait and response requested flags.
503 * The only invalid cases are if the caller requests to wait and
504 * does not request a response and if the caller does not want a
505 * response and the Fib is not allocated from pool. If a response
506 * is not requesed the Fib will just be deallocaed by the DPC
507 * routine when the response comes back from the adapter. No
508 * further processing will be done besides deleting the Fib. We
509 * will have a debug mode where the adapter can notify the host
510 * it had a problem and the host can log that fact.
513 if (wait
&& !reply
) {
515 } else if (!wait
&& reply
) {
516 hw_fib
->header
.XferState
|= cpu_to_le32(Async
| ResponseExpected
);
517 FIB_COUNTER_INCREMENT(aac_config
.AsyncSent
);
518 } else if (!wait
&& !reply
) {
519 hw_fib
->header
.XferState
|= cpu_to_le32(NoResponseExpected
);
520 FIB_COUNTER_INCREMENT(aac_config
.NoResponseSent
);
521 } else if (wait
&& reply
) {
522 hw_fib
->header
.XferState
|= cpu_to_le32(ResponseExpected
);
523 FIB_COUNTER_INCREMENT(aac_config
.NormalSent
);
526 * Map the fib into 32bits by using the fib number
529 hw_fib
->header
.SenderFibAddress
=
530 cpu_to_le32(((u32
)(fibptr
- dev
->fibs
)) << 2);
532 /* use the same shifted value for handle to be compatible
533 * with the new native hba command handle
535 hw_fib
->header
.Handle
=
536 cpu_to_le32((((u32
)(fibptr
- dev
->fibs
)) << 2) + 1);
539 * Set FIB state to indicate where it came from and if we want a
540 * response from the adapter. Also load the command from the
543 * Map the hw fib pointer as a 32bit value
545 hw_fib
->header
.Command
= cpu_to_le16(command
);
546 hw_fib
->header
.XferState
|= cpu_to_le32(SentFromHost
);
548 * Set the size of the Fib we want to send to the adapter
550 hw_fib
->header
.Size
= cpu_to_le16(sizeof(struct aac_fibhdr
) + size
);
551 if (le16_to_cpu(hw_fib
->header
.Size
) > le16_to_cpu(hw_fib
->header
.SenderSize
)) {
555 * Get a queue entry connect the FIB to it and send an notify
556 * the adapter a command is ready.
558 hw_fib
->header
.XferState
|= cpu_to_le32(NormalPriority
);
561 * Fill in the Callback and CallbackContext if we are not
565 fibptr
->callback
= callback
;
566 fibptr
->callback_data
= callback_data
;
567 fibptr
->flags
= FIB_CONTEXT_FLAG
;
572 FIB_COUNTER_INCREMENT(aac_config
.FibsSent
);
574 dprintk((KERN_DEBUG
"Fib contents:.\n"));
575 dprintk((KERN_DEBUG
" Command = %d.\n", le32_to_cpu(hw_fib
->header
.Command
)));
576 dprintk((KERN_DEBUG
" SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount
*)fib_data(fibptr
))->command
)));
577 dprintk((KERN_DEBUG
" XferState = %x.\n", le32_to_cpu(hw_fib
->header
.XferState
)));
578 dprintk((KERN_DEBUG
" hw_fib va being sent=%p\n",fibptr
->hw_fib_va
));
579 dprintk((KERN_DEBUG
" hw_fib pa being sent=%lx\n",(ulong
)fibptr
->hw_fib_pa
));
580 dprintk((KERN_DEBUG
" fib being sent=%p\n",fibptr
));
587 spin_lock_irqsave(&dev
->manage_lock
, mflags
);
588 if (dev
->management_fib_count
>= AAC_NUM_MGT_FIB
) {
589 printk(KERN_INFO
"No management Fibs Available:%d\n",
590 dev
->management_fib_count
);
591 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
594 dev
->management_fib_count
++;
595 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
596 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
599 if (dev
->sync_mode
) {
601 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
602 spin_lock_irqsave(&dev
->sync_lock
, sflags
);
604 list_add_tail(&fibptr
->fiblink
, &dev
->sync_fib_list
);
605 spin_unlock_irqrestore(&dev
->sync_lock
, sflags
);
607 dev
->sync_fib
= fibptr
;
608 spin_unlock_irqrestore(&dev
->sync_lock
, sflags
);
609 aac_adapter_sync_cmd(dev
, SEND_SYNCHRONOUS_FIB
,
610 (u32
)fibptr
->hw_fib_pa
, 0, 0, 0, 0, 0,
611 NULL
, NULL
, NULL
, NULL
, NULL
);
614 fibptr
->flags
|= FIB_CONTEXT_FLAG_WAIT
;
615 if (down_interruptible(&fibptr
->event_wait
)) {
616 fibptr
->flags
&= ~FIB_CONTEXT_FLAG_WAIT
;
624 if (aac_adapter_deliver(fibptr
) != 0) {
625 printk(KERN_ERR
"aac_fib_send: returned -EBUSY\n");
627 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
628 spin_lock_irqsave(&dev
->manage_lock
, mflags
);
629 dev
->management_fib_count
--;
630 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
637 * If the caller wanted us to wait for response wait now.
641 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
642 /* Only set for first known interruptable command */
645 * *VERY* Dangerous to time out a command, the
646 * assumption is made that we have no hope of
647 * functioning because an interrupt routing or other
648 * hardware failure has occurred.
650 unsigned long timeout
= jiffies
+ (180 * HZ
); /* 3 minutes */
651 while (down_trylock(&fibptr
->event_wait
)) {
653 if (time_is_before_eq_jiffies(timeout
)) {
654 struct aac_queue
* q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
655 atomic_dec(&q
->numpending
);
657 printk(KERN_ERR
"aacraid: aac_fib_send: first asynchronous command timed out.\n"
658 "Usually a result of a PCI interrupt routing problem;\n"
659 "update mother board BIOS or consider utilizing one of\n"
660 "the SAFE mode kernel options (acpi, apic etc)\n");
664 if ((blink
= aac_adapter_check_health(dev
)) > 0) {
666 printk(KERN_ERR
"aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
667 "Usually a result of a serious unrecoverable hardware problem\n",
673 * Allow other processes / CPUS to use core
677 } else if (down_interruptible(&fibptr
->event_wait
)) {
678 /* Do nothing ... satisfy
679 * down_interruptible must_check */
682 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
683 if (fibptr
->done
== 0) {
684 fibptr
->done
= 2; /* Tell interrupt we aborted */
685 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
688 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
689 BUG_ON(fibptr
->done
== 0);
691 if(unlikely(fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
))
696 * If the user does not want a response than return success otherwise
705 int aac_hba_send(u8 command
, struct fib
*fibptr
, fib_callback callback
,
708 struct aac_dev
*dev
= fibptr
->dev
;
710 unsigned long flags
= 0;
711 unsigned long mflags
= 0;
713 fibptr
->flags
= (FIB_CONTEXT_FLAG
| FIB_CONTEXT_FLAG_NATIVE_HBA
);
716 fibptr
->callback
= callback
;
717 fibptr
->callback_data
= callback_data
;
722 if (command
== HBA_IU_TYPE_SCSI_CMD_REQ
) {
723 struct aac_hba_cmd_req
*hbacmd
=
724 (struct aac_hba_cmd_req
*)fibptr
->hw_fib_va
;
726 hbacmd
->iu_type
= command
;
727 /* bit1 of request_id must be 0 */
729 cpu_to_le32((((u32
)(fibptr
- dev
->fibs
)) << 2) + 1);
735 spin_lock_irqsave(&dev
->manage_lock
, mflags
);
736 if (dev
->management_fib_count
>= AAC_NUM_MGT_FIB
) {
737 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
740 dev
->management_fib_count
++;
741 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
742 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
745 if (aac_adapter_deliver(fibptr
) != 0) {
747 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
748 spin_lock_irqsave(&dev
->manage_lock
, mflags
);
749 dev
->management_fib_count
--;
750 spin_unlock_irqrestore(&dev
->manage_lock
, mflags
);
754 FIB_COUNTER_INCREMENT(aac_config
.NativeSent
);
757 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
758 /* Only set for first known interruptable command */
759 if (down_interruptible(&fibptr
->event_wait
)) {
761 up(&fibptr
->event_wait
);
763 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
764 if ((fibptr
->done
== 0) || (fibptr
->done
== 2)) {
765 fibptr
->done
= 2; /* Tell interrupt we aborted */
766 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
769 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
770 WARN_ON(fibptr
->done
== 0);
772 if (unlikely(fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
))
782 * aac_consumer_get - get the top of the queue
785 * @entry: Return entry
787 * Will return a pointer to the entry on the top of the queue requested that
788 * we are a consumer of, and return the address of the queue entry. It does
789 * not change the state of the queue.
792 int aac_consumer_get(struct aac_dev
* dev
, struct aac_queue
* q
, struct aac_entry
**entry
)
796 if (le32_to_cpu(*q
->headers
.producer
) == le32_to_cpu(*q
->headers
.consumer
)) {
800 * The consumer index must be wrapped if we have reached
801 * the end of the queue, else we just use the entry
802 * pointed to by the header index
804 if (le32_to_cpu(*q
->headers
.consumer
) >= q
->entries
)
807 index
= le32_to_cpu(*q
->headers
.consumer
);
808 *entry
= q
->base
+ index
;
815 * aac_consumer_free - free consumer entry
820 * Frees up the current top of the queue we are a consumer of. If the
821 * queue was full notify the producer that the queue is no longer full.
824 void aac_consumer_free(struct aac_dev
* dev
, struct aac_queue
*q
, u32 qid
)
829 if ((le32_to_cpu(*q
->headers
.producer
)+1) == le32_to_cpu(*q
->headers
.consumer
))
832 if (le32_to_cpu(*q
->headers
.consumer
) >= q
->entries
)
833 *q
->headers
.consumer
= cpu_to_le32(1);
835 le32_add_cpu(q
->headers
.consumer
, 1);
840 case HostNormCmdQueue
:
841 notify
= HostNormCmdNotFull
;
843 case HostNormRespQueue
:
844 notify
= HostNormRespNotFull
;
850 aac_adapter_notify(dev
, notify
);
855 * aac_fib_adapter_complete - complete adapter issued fib
856 * @fibptr: fib to complete
859 * Will do all necessary work to complete a FIB that was sent from
863 int aac_fib_adapter_complete(struct fib
*fibptr
, unsigned short size
)
865 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
866 struct aac_dev
* dev
= fibptr
->dev
;
867 struct aac_queue
* q
;
868 unsigned long nointr
= 0;
869 unsigned long qflags
;
871 if (dev
->comm_interface
== AAC_COMM_MESSAGE_TYPE1
||
872 dev
->comm_interface
== AAC_COMM_MESSAGE_TYPE2
||
873 dev
->comm_interface
== AAC_COMM_MESSAGE_TYPE3
) {
878 if (hw_fib
->header
.XferState
== 0) {
879 if (dev
->comm_interface
== AAC_COMM_MESSAGE
)
884 * If we plan to do anything check the structure type first.
886 if (hw_fib
->header
.StructType
!= FIB_MAGIC
&&
887 hw_fib
->header
.StructType
!= FIB_MAGIC2
&&
888 hw_fib
->header
.StructType
!= FIB_MAGIC2_64
) {
889 if (dev
->comm_interface
== AAC_COMM_MESSAGE
)
894 * This block handles the case where the adapter had sent us a
895 * command and we have finished processing the command. We
896 * call completeFib when we are done processing the command
897 * and want to send a response back to the adapter. This will
898 * send the completed cdb to the adapter.
900 if (hw_fib
->header
.XferState
& cpu_to_le32(SentFromAdapter
)) {
901 if (dev
->comm_interface
== AAC_COMM_MESSAGE
) {
905 hw_fib
->header
.XferState
|= cpu_to_le32(HostProcessed
);
907 size
+= sizeof(struct aac_fibhdr
);
908 if (size
> le16_to_cpu(hw_fib
->header
.SenderSize
))
910 hw_fib
->header
.Size
= cpu_to_le16(size
);
912 q
= &dev
->queues
->queue
[AdapNormRespQueue
];
913 spin_lock_irqsave(q
->lock
, qflags
);
914 aac_queue_get(dev
, &index
, AdapNormRespQueue
, hw_fib
, 1, NULL
, &nointr
);
915 *(q
->headers
.producer
) = cpu_to_le32(index
+ 1);
916 spin_unlock_irqrestore(q
->lock
, qflags
);
917 if (!(nointr
& (int)aac_config
.irq_mod
))
918 aac_adapter_notify(dev
, AdapNormRespQueue
);
921 printk(KERN_WARNING
"aac_fib_adapter_complete: "
922 "Unknown xferstate detected.\n");
929 * aac_fib_complete - fib completion handler
930 * @fib: FIB to complete
932 * Will do all necessary work to complete a FIB.
935 int aac_fib_complete(struct fib
*fibptr
)
937 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
939 if (fibptr
->flags
& FIB_CONTEXT_FLAG_NATIVE_HBA
) {
945 * Check for a fib which has already been completed or with a
946 * status wait timeout
949 if (hw_fib
->header
.XferState
== 0 || fibptr
->done
== 2)
952 * If we plan to do anything check the structure type first.
955 if (hw_fib
->header
.StructType
!= FIB_MAGIC
&&
956 hw_fib
->header
.StructType
!= FIB_MAGIC2
&&
957 hw_fib
->header
.StructType
!= FIB_MAGIC2_64
)
960 * This block completes a cdb which orginated on the host and we
961 * just need to deallocate the cdb or reinit it. At this point the
962 * command is complete that we had sent to the adapter and this
963 * cdb could be reused.
966 if((hw_fib
->header
.XferState
& cpu_to_le32(SentFromHost
)) &&
967 (hw_fib
->header
.XferState
& cpu_to_le32(AdapterProcessed
)))
971 else if(hw_fib
->header
.XferState
& cpu_to_le32(SentFromHost
))
974 * This handles the case when the host has aborted the I/O
975 * to the adapter because the adapter is not responding
978 } else if(hw_fib
->header
.XferState
& cpu_to_le32(HostOwned
)) {
987 * aac_printf - handle printf from firmware
991 * Print a message passed to us by the controller firmware on the
995 void aac_printf(struct aac_dev
*dev
, u32 val
)
997 char *cp
= dev
->printfbuf
;
998 if (dev
->printf_enabled
)
1000 int length
= val
& 0xffff;
1001 int level
= (val
>> 16) & 0xffff;
1004 * The size of the printfbuf is set in port.c
1005 * There is no variable or define for it
1009 if (cp
[length
] != 0)
1011 if (level
== LOG_AAC_HIGH_ERROR
)
1012 printk(KERN_WARNING
"%s:%s", dev
->name
, cp
);
1014 printk(KERN_INFO
"%s:%s", dev
->name
, cp
);
1019 static inline int aac_aif_data(struct aac_aifcmd
*aifcmd
, uint32_t index
)
1021 return le32_to_cpu(((__le32
*)aifcmd
->data
)[index
]);
1025 static void aac_handle_aif_bu(struct aac_dev
*dev
, struct aac_aifcmd
*aifcmd
)
1027 switch (aac_aif_data(aifcmd
, 1)) {
1028 case AifBuCacheDataLoss
:
1029 if (aac_aif_data(aifcmd
, 2))
1030 dev_info(&dev
->pdev
->dev
, "Backup unit had cache data loss - [%d]\n",
1031 aac_aif_data(aifcmd
, 2));
1033 dev_info(&dev
->pdev
->dev
, "Backup Unit had cache data loss\n");
1035 case AifBuCacheDataRecover
:
1036 if (aac_aif_data(aifcmd
, 2))
1037 dev_info(&dev
->pdev
->dev
, "DDR cache data recovered successfully - [%d]\n",
1038 aac_aif_data(aifcmd
, 2));
1040 dev_info(&dev
->pdev
->dev
, "DDR cache data recovered successfully\n");
1046 * aac_handle_aif - Handle a message from the firmware
1047 * @dev: Which adapter this fib is from
1048 * @fibptr: Pointer to fibptr from adapter
1050 * This routine handles a driver notify fib from the adapter and
1051 * dispatches it to the appropriate routine for handling.
1054 #define AIF_SNIFF_TIMEOUT (500*HZ)
1055 static void aac_handle_aif(struct aac_dev
* dev
, struct fib
* fibptr
)
1057 struct hw_fib
* hw_fib
= fibptr
->hw_fib_va
;
1058 struct aac_aifcmd
* aifcmd
= (struct aac_aifcmd
*)hw_fib
->data
;
1059 u32 channel
, id
, lun
, container
;
1060 struct scsi_device
*device
;
1066 } device_config_needed
= NOTHING
;
1068 /* Sniff for container changes */
1070 if (!dev
|| !dev
->fsa_dev
)
1072 container
= channel
= id
= lun
= (u32
)-1;
1075 * We have set this up to try and minimize the number of
1076 * re-configures that take place. As a result of this when
1077 * certain AIF's come in we will set a flag waiting for another
1078 * type of AIF before setting the re-config flag.
1080 switch (le32_to_cpu(aifcmd
->command
)) {
1081 case AifCmdDriverNotify
:
1082 switch (le32_to_cpu(((__le32
*)aifcmd
->data
)[0])) {
1083 case AifRawDeviceRemove
:
1084 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1085 if ((container
>> 28)) {
1086 container
= (u32
)-1;
1089 channel
= (container
>> 24) & 0xF;
1090 if (channel
>= dev
->maximum_num_channels
) {
1091 container
= (u32
)-1;
1094 id
= container
& 0xFFFF;
1095 if (id
>= dev
->maximum_num_physicals
) {
1096 container
= (u32
)-1;
1099 lun
= (container
>> 16) & 0xFF;
1100 container
= (u32
)-1;
1101 channel
= aac_phys_to_logical(channel
);
1102 device_config_needed
= DELETE
;
1106 * Morph or Expand complete
1108 case AifDenMorphComplete
:
1109 case AifDenVolumeExtendComplete
:
1110 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1111 if (container
>= dev
->maximum_num_containers
)
1115 * Find the scsi_device associated with the SCSI
1116 * address. Make sure we have the right array, and if
1117 * so set the flag to initiate a new re-config once we
1118 * see an AifEnConfigChange AIF come through.
1121 if ((dev
!= NULL
) && (dev
->scsi_host_ptr
!= NULL
)) {
1122 device
= scsi_device_lookup(dev
->scsi_host_ptr
,
1123 CONTAINER_TO_CHANNEL(container
),
1124 CONTAINER_TO_ID(container
),
1125 CONTAINER_TO_LUN(container
));
1127 dev
->fsa_dev
[container
].config_needed
= CHANGE
;
1128 dev
->fsa_dev
[container
].config_waiting_on
= AifEnConfigChange
;
1129 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
1130 scsi_device_put(device
);
1136 * If we are waiting on something and this happens to be
1137 * that thing then set the re-configure flag.
1139 if (container
!= (u32
)-1) {
1140 if (container
>= dev
->maximum_num_containers
)
1142 if ((dev
->fsa_dev
[container
].config_waiting_on
==
1143 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
1144 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1145 dev
->fsa_dev
[container
].config_waiting_on
= 0;
1146 } else for (container
= 0;
1147 container
< dev
->maximum_num_containers
; ++container
) {
1148 if ((dev
->fsa_dev
[container
].config_waiting_on
==
1149 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
1150 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1151 dev
->fsa_dev
[container
].config_waiting_on
= 0;
1155 case AifCmdEventNotify
:
1156 switch (le32_to_cpu(((__le32
*)aifcmd
->data
)[0])) {
1157 case AifEnBatteryEvent
:
1158 dev
->cache_protected
=
1159 (((__le32
*)aifcmd
->data
)[1] == cpu_to_le32(3));
1164 case AifEnAddContainer
:
1165 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1166 if (container
>= dev
->maximum_num_containers
)
1168 dev
->fsa_dev
[container
].config_needed
= ADD
;
1169 dev
->fsa_dev
[container
].config_waiting_on
=
1171 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
1177 case AifEnDeleteContainer
:
1178 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1179 if (container
>= dev
->maximum_num_containers
)
1181 dev
->fsa_dev
[container
].config_needed
= DELETE
;
1182 dev
->fsa_dev
[container
].config_waiting_on
=
1184 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
1188 * Container change detected. If we currently are not
1189 * waiting on something else, setup to wait on a Config Change.
1191 case AifEnContainerChange
:
1192 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1193 if (container
>= dev
->maximum_num_containers
)
1195 if (dev
->fsa_dev
[container
].config_waiting_on
&&
1196 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1198 dev
->fsa_dev
[container
].config_needed
= CHANGE
;
1199 dev
->fsa_dev
[container
].config_waiting_on
=
1201 dev
->fsa_dev
[container
].config_waiting_stamp
= jiffies
;
1204 case AifEnConfigChange
:
1208 case AifEnDeleteJBOD
:
1209 container
= le32_to_cpu(((__le32
*)aifcmd
->data
)[1]);
1210 if ((container
>> 28)) {
1211 container
= (u32
)-1;
1214 channel
= (container
>> 24) & 0xF;
1215 if (channel
>= dev
->maximum_num_channels
) {
1216 container
= (u32
)-1;
1219 id
= container
& 0xFFFF;
1220 if (id
>= dev
->maximum_num_physicals
) {
1221 container
= (u32
)-1;
1224 lun
= (container
>> 16) & 0xFF;
1225 container
= (u32
)-1;
1226 channel
= aac_phys_to_logical(channel
);
1227 device_config_needed
=
1228 (((__le32
*)aifcmd
->data
)[0] ==
1229 cpu_to_le32(AifEnAddJBOD
)) ? ADD
: DELETE
;
1230 if (device_config_needed
== ADD
) {
1231 device
= scsi_device_lookup(dev
->scsi_host_ptr
,
1236 scsi_remove_device(device
);
1237 scsi_device_put(device
);
1242 case AifEnEnclosureManagement
:
1244 * If in JBOD mode, automatic exposure of new
1245 * physical target to be suppressed until configured.
1249 switch (le32_to_cpu(((__le32
*)aifcmd
->data
)[3])) {
1250 case EM_DRIVE_INSERTION
:
1251 case EM_DRIVE_REMOVAL
:
1252 case EM_SES_DRIVE_INSERTION
:
1253 case EM_SES_DRIVE_REMOVAL
:
1254 container
= le32_to_cpu(
1255 ((__le32
*)aifcmd
->data
)[2]);
1256 if ((container
>> 28)) {
1257 container
= (u32
)-1;
1260 channel
= (container
>> 24) & 0xF;
1261 if (channel
>= dev
->maximum_num_channels
) {
1262 container
= (u32
)-1;
1265 id
= container
& 0xFFFF;
1266 lun
= (container
>> 16) & 0xFF;
1267 container
= (u32
)-1;
1268 if (id
>= dev
->maximum_num_physicals
) {
1269 /* legacy dev_t ? */
1270 if ((0x2000 <= id
) || lun
|| channel
||
1271 ((channel
= (id
>> 7) & 0x3F) >=
1272 dev
->maximum_num_channels
))
1274 lun
= (id
>> 4) & 7;
1277 channel
= aac_phys_to_logical(channel
);
1278 device_config_needed
=
1279 ((((__le32
*)aifcmd
->data
)[3]
1280 == cpu_to_le32(EM_DRIVE_INSERTION
)) ||
1281 (((__le32
*)aifcmd
->data
)[3]
1282 == cpu_to_le32(EM_SES_DRIVE_INSERTION
))) ?
1286 case AifBuManagerEvent
:
1287 aac_handle_aif_bu(dev
, aifcmd
);
1292 * If we are waiting on something and this happens to be
1293 * that thing then set the re-configure flag.
1295 if (container
!= (u32
)-1) {
1296 if (container
>= dev
->maximum_num_containers
)
1298 if ((dev
->fsa_dev
[container
].config_waiting_on
==
1299 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
1300 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1301 dev
->fsa_dev
[container
].config_waiting_on
= 0;
1302 } else for (container
= 0;
1303 container
< dev
->maximum_num_containers
; ++container
) {
1304 if ((dev
->fsa_dev
[container
].config_waiting_on
==
1305 le32_to_cpu(*(__le32
*)aifcmd
->data
)) &&
1306 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
))
1307 dev
->fsa_dev
[container
].config_waiting_on
= 0;
1311 case AifCmdJobProgress
:
1313 * These are job progress AIF's. When a Clear is being
1314 * done on a container it is initially created then hidden from
1315 * the OS. When the clear completes we don't get a config
1316 * change so we monitor the job status complete on a clear then
1317 * wait for a container change.
1320 if (((__le32
*)aifcmd
->data
)[1] == cpu_to_le32(AifJobCtrZero
) &&
1321 (((__le32
*)aifcmd
->data
)[6] == ((__le32
*)aifcmd
->data
)[5] ||
1322 ((__le32
*)aifcmd
->data
)[4] == cpu_to_le32(AifJobStsSuccess
))) {
1324 container
< dev
->maximum_num_containers
;
1327 * Stomp on all config sequencing for all
1330 dev
->fsa_dev
[container
].config_waiting_on
=
1331 AifEnContainerChange
;
1332 dev
->fsa_dev
[container
].config_needed
= ADD
;
1333 dev
->fsa_dev
[container
].config_waiting_stamp
=
1337 if (((__le32
*)aifcmd
->data
)[1] == cpu_to_le32(AifJobCtrZero
) &&
1338 ((__le32
*)aifcmd
->data
)[6] == 0 &&
1339 ((__le32
*)aifcmd
->data
)[4] == cpu_to_le32(AifJobStsRunning
)) {
1341 container
< dev
->maximum_num_containers
;
1344 * Stomp on all config sequencing for all
1347 dev
->fsa_dev
[container
].config_waiting_on
=
1348 AifEnContainerChange
;
1349 dev
->fsa_dev
[container
].config_needed
= DELETE
;
1350 dev
->fsa_dev
[container
].config_waiting_stamp
=
1359 if (device_config_needed
== NOTHING
)
1360 for (; container
< dev
->maximum_num_containers
; ++container
) {
1361 if ((dev
->fsa_dev
[container
].config_waiting_on
== 0) &&
1362 (dev
->fsa_dev
[container
].config_needed
!= NOTHING
) &&
1363 time_before(jiffies
, dev
->fsa_dev
[container
].config_waiting_stamp
+ AIF_SNIFF_TIMEOUT
)) {
1364 device_config_needed
=
1365 dev
->fsa_dev
[container
].config_needed
;
1366 dev
->fsa_dev
[container
].config_needed
= NOTHING
;
1367 channel
= CONTAINER_TO_CHANNEL(container
);
1368 id
= CONTAINER_TO_ID(container
);
1369 lun
= CONTAINER_TO_LUN(container
);
1373 if (device_config_needed
== NOTHING
)
1377 * If we decided that a re-configuration needs to be done,
1378 * schedule it here on the way out the door, please close the door
1383 * Find the scsi_device associated with the SCSI address,
1384 * and mark it as changed, invalidating the cache. This deals
1385 * with changes to existing device IDs.
1388 if (!dev
|| !dev
->scsi_host_ptr
)
1391 * force reload of disk info via aac_probe_container
1393 if ((channel
== CONTAINER_CHANNEL
) &&
1394 (device_config_needed
!= NOTHING
)) {
1395 if (dev
->fsa_dev
[container
].valid
== 1)
1396 dev
->fsa_dev
[container
].valid
= 2;
1397 aac_probe_container(dev
, container
);
1399 device
= scsi_device_lookup(dev
->scsi_host_ptr
, channel
, id
, lun
);
1401 switch (device_config_needed
) {
1403 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1404 scsi_remove_device(device
);
1406 if (scsi_device_online(device
)) {
1407 scsi_device_set_state(device
, SDEV_OFFLINE
);
1408 sdev_printk(KERN_INFO
, device
,
1409 "Device offlined - %s\n",
1410 (channel
== CONTAINER_CHANNEL
) ?
1412 "enclosure services event");
1417 if (!scsi_device_online(device
)) {
1418 sdev_printk(KERN_INFO
, device
,
1419 "Device online - %s\n",
1420 (channel
== CONTAINER_CHANNEL
) ?
1422 "enclosure services event");
1423 scsi_device_set_state(device
, SDEV_RUNNING
);
1427 if ((channel
== CONTAINER_CHANNEL
)
1428 && (!dev
->fsa_dev
[container
].valid
)) {
1429 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1430 scsi_remove_device(device
);
1432 if (!scsi_device_online(device
))
1434 scsi_device_set_state(device
, SDEV_OFFLINE
);
1435 sdev_printk(KERN_INFO
, device
,
1436 "Device offlined - %s\n",
1441 scsi_rescan_device(&device
->sdev_gendev
);
1446 scsi_device_put(device
);
1447 device_config_needed
= NOTHING
;
1449 if (device_config_needed
== ADD
)
1450 scsi_add_device(dev
->scsi_host_ptr
, channel
, id
, lun
);
1451 if (channel
== CONTAINER_CHANNEL
) {
1453 device_config_needed
= NOTHING
;
1458 static int _aac_reset_adapter(struct aac_dev
*aac
, int forced
, u8 reset_type
)
1462 struct Scsi_Host
*host
;
1463 struct scsi_device
*dev
;
1464 struct scsi_cmnd
*command
;
1465 struct scsi_cmnd
*command_list
;
1471 * - host is locked, unless called by the aacraid thread.
1472 * (a matter of convenience, due to legacy issues surrounding
1473 * eh_host_adapter_reset).
1474 * - in_reset is asserted, so no new i/o is getting to the
1476 * - The card is dead, or will be very shortly ;-/ so no new
1477 * commands are completing in the interrupt service.
1479 host
= aac
->scsi_host_ptr
;
1480 scsi_block_requests(host
);
1481 aac_adapter_disable_int(aac
);
1482 if (aac
->thread
->pid
!= current
->pid
) {
1483 spin_unlock_irq(host
->host_lock
);
1484 kthread_stop(aac
->thread
);
1489 * If a positive health, means in a known DEAD PANIC
1490 * state and the adapter could be reset to `try again'.
1492 bled
= forced
? 0 : aac_adapter_check_health(aac
);
1493 retval
= aac_adapter_restart(aac
, bled
, reset_type
);
1499 * Loop through the fibs, close the synchronous FIBS
1501 for (retval
= 1, index
= 0; index
< (aac
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
); index
++) {
1502 struct fib
*fib
= &aac
->fibs
[index
];
1503 if (!(fib
->hw_fib_va
->header
.XferState
& cpu_to_le32(NoResponseExpected
| Async
)) &&
1504 (fib
->hw_fib_va
->header
.XferState
& cpu_to_le32(ResponseExpected
))) {
1505 unsigned long flagv
;
1506 spin_lock_irqsave(&fib
->event_lock
, flagv
);
1507 up(&fib
->event_wait
);
1508 spin_unlock_irqrestore(&fib
->event_lock
, flagv
);
1513 /* Give some extra time for ioctls to complete. */
1516 index
= aac
->cardtype
;
1519 * Re-initialize the adapter, first free resources, then carefully
1520 * apply the initialization sequence to come back again. Only risk
1521 * is a change in Firmware dropping cache, it is assumed the caller
1522 * will ensure that i/o is queisced and the card is flushed in that
1525 aac_fib_map_free(aac
);
1526 pci_free_consistent(aac
->pdev
, aac
->comm_size
, aac
->comm_addr
, aac
->comm_phys
);
1527 aac
->comm_addr
= NULL
;
1532 kfree(aac
->fsa_dev
);
1533 aac
->fsa_dev
= NULL
;
1534 quirks
= aac_get_driver_ident(index
)->quirks
;
1535 if (quirks
& AAC_QUIRK_31BIT
) {
1536 if (((retval
= pci_set_dma_mask(aac
->pdev
, DMA_BIT_MASK(31)))) ||
1537 ((retval
= pci_set_consistent_dma_mask(aac
->pdev
, DMA_BIT_MASK(31)))))
1540 if (((retval
= pci_set_dma_mask(aac
->pdev
, DMA_BIT_MASK(32)))) ||
1541 ((retval
= pci_set_consistent_dma_mask(aac
->pdev
, DMA_BIT_MASK(32)))))
1544 if ((retval
= (*(aac_get_driver_ident(index
)->init
))(aac
)))
1546 if (quirks
& AAC_QUIRK_31BIT
)
1547 if ((retval
= pci_set_dma_mask(aac
->pdev
, DMA_BIT_MASK(32))))
1550 aac
->thread
= kthread_run(aac_command_thread
, aac
, "%s",
1552 if (IS_ERR(aac
->thread
)) {
1553 retval
= PTR_ERR(aac
->thread
);
1557 (void)aac_get_adapter_info(aac
);
1558 if ((quirks
& AAC_QUIRK_34SG
) && (host
->sg_tablesize
> 34)) {
1559 host
->sg_tablesize
= 34;
1560 host
->max_sectors
= (host
->sg_tablesize
* 8) + 112;
1562 if ((quirks
& AAC_QUIRK_17SG
) && (host
->sg_tablesize
> 17)) {
1563 host
->sg_tablesize
= 17;
1564 host
->max_sectors
= (host
->sg_tablesize
* 8) + 112;
1566 aac_get_config_status(aac
, 1);
1567 aac_get_containers(aac
);
1569 * This is where the assumption that the Adapter is quiesced
1572 command_list
= NULL
;
1573 __shost_for_each_device(dev
, host
) {
1574 unsigned long flags
;
1575 spin_lock_irqsave(&dev
->list_lock
, flags
);
1576 list_for_each_entry(command
, &dev
->cmd_list
, list
)
1577 if (command
->SCp
.phase
== AAC_OWNER_FIRMWARE
) {
1578 command
->SCp
.buffer
= (struct scatterlist
*)command_list
;
1579 command_list
= command
;
1581 spin_unlock_irqrestore(&dev
->list_lock
, flags
);
1583 while ((command
= command_list
)) {
1584 command_list
= (struct scsi_cmnd
*)command
->SCp
.buffer
;
1585 command
->SCp
.buffer
= NULL
;
1586 command
->result
= DID_OK
<< 16
1587 | COMMAND_COMPLETE
<< 8
1588 | SAM_STAT_TASK_SET_FULL
;
1589 command
->SCp
.phase
= AAC_OWNER_ERROR_HANDLER
;
1590 command
->scsi_done(command
);
1596 scsi_unblock_requests(host
);
1598 spin_lock_irq(host
->host_lock
);
1603 int aac_reset_adapter(struct aac_dev
*aac
, int forced
, u8 reset_type
)
1605 unsigned long flagv
= 0;
1607 struct Scsi_Host
* host
;
1610 if (spin_trylock_irqsave(&aac
->fib_lock
, flagv
) == 0)
1613 if (aac
->in_reset
) {
1614 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1618 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1621 * Wait for all commands to complete to this specific
1622 * target (block maximum 60 seconds). Although not necessary,
1623 * it does make us a good storage citizen.
1625 host
= aac
->scsi_host_ptr
;
1626 scsi_block_requests(host
);
1627 if (forced
< 2) for (retval
= 60; retval
; --retval
) {
1628 struct scsi_device
* dev
;
1629 struct scsi_cmnd
* command
;
1632 __shost_for_each_device(dev
, host
) {
1633 spin_lock_irqsave(&dev
->list_lock
, flagv
);
1634 list_for_each_entry(command
, &dev
->cmd_list
, list
) {
1635 if (command
->SCp
.phase
== AAC_OWNER_FIRMWARE
) {
1640 spin_unlock_irqrestore(&dev
->list_lock
, flagv
);
1646 * We can exit If all the commands are complete
1653 /* Quiesce build, flush cache, write through mode */
1655 aac_send_shutdown(aac
);
1656 spin_lock_irqsave(host
->host_lock
, flagv
);
1657 bled
= forced
? forced
:
1658 (aac_check_reset
!= 0 && aac_check_reset
!= 1);
1659 retval
= _aac_reset_adapter(aac
, bled
, reset_type
);
1660 spin_unlock_irqrestore(host
->host_lock
, flagv
);
1662 if ((forced
< 2) && (retval
== -ENODEV
)) {
1663 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1664 struct fib
* fibctx
= aac_fib_alloc(aac
);
1666 struct aac_pause
*cmd
;
1669 aac_fib_init(fibctx
);
1671 cmd
= (struct aac_pause
*) fib_data(fibctx
);
1673 cmd
->command
= cpu_to_le32(VM_ContainerConfig
);
1674 cmd
->type
= cpu_to_le32(CT_PAUSE_IO
);
1675 cmd
->timeout
= cpu_to_le32(1);
1676 cmd
->min
= cpu_to_le32(1);
1677 cmd
->noRescan
= cpu_to_le32(1);
1678 cmd
->count
= cpu_to_le32(0);
1680 status
= aac_fib_send(ContainerCommand
,
1682 sizeof(struct aac_pause
),
1684 -2 /* Timeout silently */, 1,
1688 aac_fib_complete(fibctx
);
1689 /* FIB should be freed only after getting
1690 * the response from the F/W */
1691 if (status
!= -ERESTARTSYS
)
1692 aac_fib_free(fibctx
);
1699 int aac_check_health(struct aac_dev
* aac
)
1702 unsigned long time_now
, flagv
= 0;
1703 struct list_head
* entry
;
1704 struct Scsi_Host
* host
;
1707 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1708 if (spin_trylock_irqsave(&aac
->fib_lock
, flagv
) == 0)
1711 if (aac
->in_reset
|| !(BlinkLED
= aac_adapter_check_health(aac
))) {
1712 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1719 * aac_aifcmd.command = AifCmdEventNotify = 1
1720 * aac_aifcmd.seqnum = 0xFFFFFFFF
1721 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1722 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1723 * aac.aifcmd.data[2] = AifHighPriority = 3
1724 * aac.aifcmd.data[3] = BlinkLED
1727 time_now
= jiffies
/HZ
;
1728 entry
= aac
->fib_list
.next
;
1731 * For each Context that is on the
1732 * fibctxList, make a copy of the
1733 * fib, and then set the event to wake up the
1734 * thread that is waiting for it.
1736 while (entry
!= &aac
->fib_list
) {
1738 * Extract the fibctx
1740 struct aac_fib_context
*fibctx
= list_entry(entry
, struct aac_fib_context
, next
);
1741 struct hw_fib
* hw_fib
;
1744 * Check if the queue is getting
1747 if (fibctx
->count
> 20) {
1749 * It's *not* jiffies folks,
1750 * but jiffies / HZ, so do not
1753 u32 time_last
= fibctx
->jiffies
;
1755 * Has it been > 2 minutes
1756 * since the last read off
1759 if ((time_now
- time_last
) > aif_timeout
) {
1760 entry
= entry
->next
;
1761 aac_close_fib_context(aac
, fibctx
);
1766 * Warning: no sleep allowed while
1769 hw_fib
= kzalloc(sizeof(struct hw_fib
), GFP_ATOMIC
);
1770 fib
= kzalloc(sizeof(struct fib
), GFP_ATOMIC
);
1771 if (fib
&& hw_fib
) {
1772 struct aac_aifcmd
* aif
;
1774 fib
->hw_fib_va
= hw_fib
;
1777 fib
->type
= FSAFS_NTC_FIB_CONTEXT
;
1778 fib
->size
= sizeof (struct fib
);
1779 fib
->data
= hw_fib
->data
;
1780 aif
= (struct aac_aifcmd
*)hw_fib
->data
;
1781 aif
->command
= cpu_to_le32(AifCmdEventNotify
);
1782 aif
->seqnum
= cpu_to_le32(0xFFFFFFFF);
1783 ((__le32
*)aif
->data
)[0] = cpu_to_le32(AifEnExpEvent
);
1784 ((__le32
*)aif
->data
)[1] = cpu_to_le32(AifExeFirmwarePanic
);
1785 ((__le32
*)aif
->data
)[2] = cpu_to_le32(AifHighPriority
);
1786 ((__le32
*)aif
->data
)[3] = cpu_to_le32(BlinkLED
);
1789 * Put the FIB onto the
1792 list_add_tail(&fib
->fiblink
, &fibctx
->fib_list
);
1795 * Set the event to wake up the
1796 * thread that will waiting.
1798 up(&fibctx
->wait_sem
);
1800 printk(KERN_WARNING
"aifd: didn't allocate NewFib.\n");
1804 entry
= entry
->next
;
1807 spin_unlock_irqrestore(&aac
->fib_lock
, flagv
);
1810 printk(KERN_ERR
"%s: Host adapter dead %d\n", aac
->name
, BlinkLED
);
1814 printk(KERN_ERR
"%s: Host adapter BLINK LED 0x%x\n", aac
->name
, BlinkLED
);
1816 if (!aac_check_reset
|| ((aac_check_reset
== 1) &&
1817 (aac
->supplement_adapter_info
.SupportedOptions2
&
1818 AAC_OPTION_IGNORE_RESET
)))
1820 host
= aac
->scsi_host_ptr
;
1821 if (aac
->thread
->pid
!= current
->pid
)
1822 spin_lock_irqsave(host
->host_lock
, flagv
);
1823 bled
= aac_check_reset
!= 1 ? 1 : 0;
1824 _aac_reset_adapter(aac
, bled
, IOP_HWSOFT_RESET
);
1825 if (aac
->thread
->pid
!= current
->pid
)
1826 spin_unlock_irqrestore(host
->host_lock
, flagv
);
1835 static void aac_resolve_luns(struct aac_dev
*dev
)
1837 int bus
, target
, channel
;
1838 struct scsi_device
*sdev
;
1842 for (bus
= 0; bus
< AAC_MAX_BUSES
; bus
++) {
1843 for (target
= 0; target
< AAC_MAX_TARGETS
; target
++) {
1845 if (aac_phys_to_logical(bus
) == ENCLOSURE_CHANNEL
)
1848 if (bus
== CONTAINER_CHANNEL
)
1849 channel
= CONTAINER_CHANNEL
;
1851 channel
= aac_phys_to_logical(bus
);
1853 devtype
= dev
->hba_map
[bus
][target
].devtype
;
1854 new_devtype
= dev
->hba_map
[bus
][target
].new_devtype
;
1856 sdev
= scsi_device_lookup(dev
->scsi_host_ptr
, channel
,
1859 if (!sdev
&& devtype
)
1860 scsi_add_device(dev
->scsi_host_ptr
, channel
,
1862 else if (sdev
&& new_devtype
!= devtype
)
1863 scsi_remove_device(sdev
);
1864 else if (sdev
&& new_devtype
== devtype
)
1865 scsi_rescan_device(&sdev
->sdev_gendev
);
1868 scsi_device_put(sdev
);
1870 dev
->hba_map
[bus
][target
].devtype
= new_devtype
;
1876 * aac_handle_sa_aif Handle a message from the firmware
1877 * @dev: Which adapter this fib is from
1878 * @fibptr: Pointer to fibptr from adapter
1880 * This routine handles a driver notify fib from the adapter and
1881 * dispatches it to the appropriate routine for handling.
1883 static void aac_handle_sa_aif(struct aac_dev
*dev
, struct fib
*fibptr
)
1885 int i
, bus
, target
, container
, rcode
= 0;
1888 struct scsi_device
*sdev
;
1890 if (fibptr
->hbacmd_size
& SA_AIF_HOTPLUG
)
1891 events
= SA_AIF_HOTPLUG
;
1892 else if (fibptr
->hbacmd_size
& SA_AIF_HARDWARE
)
1893 events
= SA_AIF_HARDWARE
;
1894 else if (fibptr
->hbacmd_size
& SA_AIF_PDEV_CHANGE
)
1895 events
= SA_AIF_PDEV_CHANGE
;
1896 else if (fibptr
->hbacmd_size
& SA_AIF_LDEV_CHANGE
)
1897 events
= SA_AIF_LDEV_CHANGE
;
1898 else if (fibptr
->hbacmd_size
& SA_AIF_BPSTAT_CHANGE
)
1899 events
= SA_AIF_BPSTAT_CHANGE
;
1900 else if (fibptr
->hbacmd_size
& SA_AIF_BPCFG_CHANGE
)
1901 events
= SA_AIF_BPCFG_CHANGE
;
1904 case SA_AIF_HOTPLUG
:
1905 case SA_AIF_HARDWARE
:
1906 case SA_AIF_PDEV_CHANGE
:
1907 case SA_AIF_LDEV_CHANGE
:
1908 case SA_AIF_BPCFG_CHANGE
:
1910 fib
= aac_fib_alloc(dev
);
1912 pr_err("aac_handle_sa_aif: out of memory\n");
1915 for (bus
= 0; bus
< AAC_MAX_BUSES
; bus
++)
1916 for (target
= 0; target
< AAC_MAX_TARGETS
; target
++)
1917 dev
->hba_map
[bus
][target
].new_devtype
= 0;
1919 rcode
= aac_report_phys_luns(dev
, fib
, AAC_RESCAN
);
1921 if (rcode
!= -ERESTARTSYS
)
1924 aac_resolve_luns(dev
);
1926 if (events
== SA_AIF_LDEV_CHANGE
||
1927 events
== SA_AIF_BPCFG_CHANGE
) {
1928 aac_get_containers(dev
);
1929 for (container
= 0; container
<
1930 dev
->maximum_num_containers
; ++container
) {
1931 sdev
= scsi_device_lookup(dev
->scsi_host_ptr
,
1934 if (dev
->fsa_dev
[container
].valid
&& !sdev
) {
1935 scsi_add_device(dev
->scsi_host_ptr
,
1938 } else if (!dev
->fsa_dev
[container
].valid
&&
1940 scsi_remove_device(sdev
);
1941 scsi_device_put(sdev
);
1943 scsi_rescan_device(&sdev
->sdev_gendev
);
1944 scsi_device_put(sdev
);
1950 case SA_AIF_BPSTAT_CHANGE
:
1951 /* currently do nothing */
1955 for (i
= 1; i
<= 10; ++i
) {
1956 events
= src_readl(dev
, MUnit
.IDR
);
1957 if (events
& (1<<23)) {
1958 pr_warn(" AIF not cleared by firmware - %d/%d)\n",
1965 static int get_fib_count(struct aac_dev
*dev
)
1967 unsigned int num
= 0;
1968 struct list_head
*entry
;
1969 unsigned long flagv
;
1972 * Warning: no sleep allowed while
1973 * holding spinlock. We take the estimate
1974 * and pre-allocate a set of fibs outside the
1977 num
= le32_to_cpu(dev
->init
->r7
.adapter_fibs_size
)
1978 / sizeof(struct hw_fib
); /* some extra */
1979 spin_lock_irqsave(&dev
->fib_lock
, flagv
);
1980 entry
= dev
->fib_list
.next
;
1981 while (entry
!= &dev
->fib_list
) {
1982 entry
= entry
->next
;
1985 spin_unlock_irqrestore(&dev
->fib_lock
, flagv
);
1990 static int fillup_pools(struct aac_dev
*dev
, struct hw_fib
**hw_fib_pool
,
1991 struct fib
**fib_pool
,
1994 struct hw_fib
**hw_fib_p
;
1998 hw_fib_p
= hw_fib_pool
;
2000 while (hw_fib_p
< &hw_fib_pool
[num
]) {
2001 *(hw_fib_p
) = kmalloc(sizeof(struct hw_fib
), GFP_KERNEL
);
2002 if (!(*(hw_fib_p
++))) {
2007 *(fib_p
) = kmalloc(sizeof(struct fib
), GFP_KERNEL
);
2008 if (!(*(fib_p
++))) {
2009 kfree(*(--hw_fib_p
));
2014 num
= hw_fib_p
- hw_fib_pool
;
2021 static void wakeup_fibctx_threads(struct aac_dev
*dev
,
2022 struct hw_fib
**hw_fib_pool
,
2023 struct fib
**fib_pool
,
2025 struct hw_fib
*hw_fib
,
2028 unsigned long flagv
;
2029 struct list_head
*entry
;
2030 struct hw_fib
**hw_fib_p
;
2032 u32 time_now
, time_last
;
2033 struct hw_fib
*hw_newfib
;
2035 struct aac_fib_context
*fibctx
;
2037 time_now
= jiffies
/HZ
;
2038 spin_lock_irqsave(&dev
->fib_lock
, flagv
);
2039 entry
= dev
->fib_list
.next
;
2041 * For each Context that is on the
2042 * fibctxList, make a copy of the
2043 * fib, and then set the event to wake up the
2044 * thread that is waiting for it.
2047 hw_fib_p
= hw_fib_pool
;
2049 while (entry
!= &dev
->fib_list
) {
2051 * Extract the fibctx
2053 fibctx
= list_entry(entry
, struct aac_fib_context
,
2056 * Check if the queue is getting
2059 if (fibctx
->count
> 20) {
2061 * It's *not* jiffies folks,
2062 * but jiffies / HZ so do not
2065 time_last
= fibctx
->jiffies
;
2067 * Has it been > 2 minutes
2068 * since the last read off
2071 if ((time_now
- time_last
) > aif_timeout
) {
2072 entry
= entry
->next
;
2073 aac_close_fib_context(dev
, fibctx
);
2078 * Warning: no sleep allowed while
2081 if (hw_fib_p
>= &hw_fib_pool
[num
]) {
2082 pr_warn("aifd: didn't allocate NewFib\n");
2083 entry
= entry
->next
;
2087 hw_newfib
= *hw_fib_p
;
2088 *(hw_fib_p
++) = NULL
;
2092 * Make the copy of the FIB
2094 memcpy(hw_newfib
, hw_fib
, sizeof(struct hw_fib
));
2095 memcpy(newfib
, fib
, sizeof(struct fib
));
2096 newfib
->hw_fib_va
= hw_newfib
;
2098 * Put the FIB onto the
2101 list_add_tail(&newfib
->fiblink
, &fibctx
->fib_list
);
2104 * Set the event to wake up the
2105 * thread that is waiting.
2107 up(&fibctx
->wait_sem
);
2109 entry
= entry
->next
;
2112 * Set the status of this FIB
2114 *(__le32
*)hw_fib
->data
= cpu_to_le32(ST_OK
);
2115 aac_fib_adapter_complete(fib
, sizeof(u32
));
2116 spin_unlock_irqrestore(&dev
->fib_lock
, flagv
);
2120 static void aac_process_events(struct aac_dev
*dev
)
2122 struct hw_fib
*hw_fib
;
2124 unsigned long flags
;
2128 t_lock
= dev
->queues
->queue
[HostNormCmdQueue
].lock
;
2129 spin_lock_irqsave(t_lock
, flags
);
2131 while (!list_empty(&(dev
->queues
->queue
[HostNormCmdQueue
].cmdq
))) {
2132 struct list_head
*entry
;
2133 struct aac_aifcmd
*aifcmd
;
2135 struct hw_fib
**hw_fib_pool
, **hw_fib_p
;
2136 struct fib
**fib_pool
, **fib_p
;
2138 set_current_state(TASK_RUNNING
);
2140 entry
= dev
->queues
->queue
[HostNormCmdQueue
].cmdq
.next
;
2143 t_lock
= dev
->queues
->queue
[HostNormCmdQueue
].lock
;
2144 spin_unlock_irqrestore(t_lock
, flags
);
2146 fib
= list_entry(entry
, struct fib
, fiblink
);
2147 hw_fib
= fib
->hw_fib_va
;
2148 if (dev
->sa_firmware
) {
2150 aac_handle_sa_aif(dev
, fib
);
2151 aac_fib_adapter_complete(fib
, (u16
)sizeof(u32
));
2155 * We will process the FIB here or pass it to a
2156 * worker thread that is TBD. We Really can't
2157 * do anything at this point since we don't have
2158 * anything defined for this thread to do.
2160 memset(fib
, 0, sizeof(struct fib
));
2161 fib
->type
= FSAFS_NTC_FIB_CONTEXT
;
2162 fib
->size
= sizeof(struct fib
);
2163 fib
->hw_fib_va
= hw_fib
;
2164 fib
->data
= hw_fib
->data
;
2167 * We only handle AifRequest fibs from the adapter.
2170 aifcmd
= (struct aac_aifcmd
*) hw_fib
->data
;
2171 if (aifcmd
->command
== cpu_to_le32(AifCmdDriverNotify
)) {
2172 /* Handle Driver Notify Events */
2173 aac_handle_aif(dev
, fib
);
2174 *(__le32
*)hw_fib
->data
= cpu_to_le32(ST_OK
);
2175 aac_fib_adapter_complete(fib
, (u16
)sizeof(u32
));
2179 * The u32 here is important and intended. We are using
2180 * 32bit wrapping time to fit the adapter field
2184 if (aifcmd
->command
== cpu_to_le32(AifCmdEventNotify
)
2185 || aifcmd
->command
== cpu_to_le32(AifCmdJobProgress
)) {
2186 aac_handle_aif(dev
, fib
);
2190 * get number of fibs to process
2192 num
= get_fib_count(dev
);
2196 hw_fib_pool
= kmalloc_array(num
, sizeof(struct hw_fib
*),
2201 fib_pool
= kmalloc_array(num
, sizeof(struct fib
*), GFP_KERNEL
);
2203 goto free_hw_fib_pool
;
2206 * Fill up fib pointer pools with actual fibs
2209 rcode
= fillup_pools(dev
, hw_fib_pool
, fib_pool
, num
);
2214 * wakeup the thread that is waiting for
2215 * the response from fw (ioctl)
2217 wakeup_fibctx_threads(dev
, hw_fib_pool
, fib_pool
,
2221 /* Free up the remaining resources */
2222 hw_fib_p
= hw_fib_pool
;
2224 while (hw_fib_p
< &hw_fib_pool
[num
]) {
2235 t_lock
= dev
->queues
->queue
[HostNormCmdQueue
].lock
;
2236 spin_lock_irqsave(t_lock
, flags
);
2239 * There are no more AIF's
2241 t_lock
= dev
->queues
->queue
[HostNormCmdQueue
].lock
;
2242 spin_unlock_irqrestore(t_lock
, flags
);
2245 static int aac_send_wellness_command(struct aac_dev
*dev
, char *wellness_str
,
2248 struct aac_srb
*srbcmd
;
2249 struct sgmap64
*sg64
;
2256 fibptr
= aac_fib_alloc(dev
);
2260 dma_buf
= pci_alloc_consistent(dev
->pdev
, datasize
, &addr
);
2264 aac_fib_init(fibptr
);
2266 vbus
= (u32
)le16_to_cpu(dev
->supplement_adapter_info
.VirtDeviceBus
);
2267 vid
= (u32
)le16_to_cpu(dev
->supplement_adapter_info
.VirtDeviceTarget
);
2269 srbcmd
= (struct aac_srb
*)fib_data(fibptr
);
2271 srbcmd
->function
= cpu_to_le32(SRBF_ExecuteScsi
);
2272 srbcmd
->channel
= cpu_to_le32(vbus
);
2273 srbcmd
->id
= cpu_to_le32(vid
);
2275 srbcmd
->flags
= cpu_to_le32(SRB_DataOut
);
2276 srbcmd
->timeout
= cpu_to_le32(10);
2277 srbcmd
->retry_limit
= 0;
2278 srbcmd
->cdb_size
= cpu_to_le32(12);
2279 srbcmd
->count
= cpu_to_le32(datasize
);
2281 memset(srbcmd
->cdb
, 0, sizeof(srbcmd
->cdb
));
2282 srbcmd
->cdb
[0] = BMIC_OUT
;
2283 srbcmd
->cdb
[6] = WRITE_HOST_WELLNESS
;
2284 memcpy(dma_buf
, (char *)wellness_str
, datasize
);
2286 sg64
= (struct sgmap64
*)&srbcmd
->sg
;
2287 sg64
->count
= cpu_to_le32(1);
2288 sg64
->sg
[0].addr
[1] = cpu_to_le32((u32
)(((addr
) >> 16) >> 16));
2289 sg64
->sg
[0].addr
[0] = cpu_to_le32((u32
)(addr
& 0xffffffff));
2290 sg64
->sg
[0].count
= cpu_to_le32(datasize
);
2292 ret
= aac_fib_send(ScsiPortCommand64
, fibptr
, sizeof(struct aac_srb
),
2293 FsaNormal
, 1, 1, NULL
, NULL
);
2295 pci_free_consistent(dev
->pdev
, datasize
, (void *)dma_buf
, addr
);
2298 * Do not set XferState to zero unless
2299 * receives a response from F/W
2302 aac_fib_complete(fibptr
);
2305 * FIB should be freed only after
2306 * getting the response from the F/W
2308 if (ret
!= -ERESTARTSYS
)
2314 aac_fib_free(fibptr
);
2318 int aac_send_safw_hostttime(struct aac_dev
*dev
, struct timeval
*now
)
2321 char wellness_str
[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
2322 u32 datasize
= sizeof(wellness_str
);
2323 unsigned long local_time
;
2326 if (!dev
->sa_firmware
)
2329 local_time
= (u32
)(now
->tv_sec
- (sys_tz
.tz_minuteswest
* 60));
2330 time_to_tm(local_time
, 0, &cur_tm
);
2332 cur_tm
.tm_year
+= 1900;
2333 wellness_str
[8] = bin2bcd(cur_tm
.tm_hour
);
2334 wellness_str
[9] = bin2bcd(cur_tm
.tm_min
);
2335 wellness_str
[10] = bin2bcd(cur_tm
.tm_sec
);
2336 wellness_str
[12] = bin2bcd(cur_tm
.tm_mon
);
2337 wellness_str
[13] = bin2bcd(cur_tm
.tm_mday
);
2338 wellness_str
[14] = bin2bcd(cur_tm
.tm_year
/ 100);
2339 wellness_str
[15] = bin2bcd(cur_tm
.tm_year
% 100);
2341 ret
= aac_send_wellness_command(dev
, wellness_str
, datasize
);
2347 int aac_send_hosttime(struct aac_dev
*dev
, struct timeval
*now
)
2353 fibptr
= aac_fib_alloc(dev
);
2357 aac_fib_init(fibptr
);
2358 info
= (__le32
*)fib_data(fibptr
);
2359 *info
= cpu_to_le32(now
->tv_sec
);
2360 ret
= aac_fib_send(SendHostTime
, fibptr
, sizeof(*info
), FsaNormal
,
2364 * Do not set XferState to zero unless
2365 * receives a response from F/W
2368 aac_fib_complete(fibptr
);
2371 * FIB should be freed only after
2372 * getting the response from the F/W
2374 if (ret
!= -ERESTARTSYS
)
2375 aac_fib_free(fibptr
);
2382 * aac_command_thread - command processing thread
2383 * @dev: Adapter to monitor
2385 * Waits on the commandready event in it's queue. When the event gets set
2386 * it will pull FIBs off it's queue. It will continue to pull FIBs off
2387 * until the queue is empty. When the queue is empty it will wait for
2391 int aac_command_thread(void *data
)
2393 struct aac_dev
*dev
= data
;
2394 DECLARE_WAITQUEUE(wait
, current
);
2395 unsigned long next_jiffies
= jiffies
+ HZ
;
2396 unsigned long next_check_jiffies
= next_jiffies
;
2397 long difference
= HZ
;
2400 * We can only have one thread per adapter for AIF's.
2402 if (dev
->aif_thread
)
2406 * Let the DPC know it has a place to send the AIF's to.
2408 dev
->aif_thread
= 1;
2409 add_wait_queue(&dev
->queues
->queue
[HostNormCmdQueue
].cmdready
, &wait
);
2410 set_current_state(TASK_INTERRUPTIBLE
);
2411 dprintk ((KERN_INFO
"aac_command_thread start\n"));
2414 aac_process_events(dev
);
2417 * Background activity
2419 if ((time_before(next_check_jiffies
,next_jiffies
))
2420 && ((difference
= next_check_jiffies
- jiffies
) <= 0)) {
2421 next_check_jiffies
= next_jiffies
;
2422 if (aac_check_health(dev
) == 0) {
2423 difference
= ((long)(unsigned)check_interval
)
2425 next_check_jiffies
= jiffies
+ difference
;
2426 } else if (!dev
->queues
)
2429 if (!time_before(next_check_jiffies
,next_jiffies
)
2430 && ((difference
= next_jiffies
- jiffies
) <= 0)) {
2434 /* Don't even try to talk to adapter if its sick */
2435 ret
= aac_check_health(dev
);
2438 next_check_jiffies
= jiffies
2439 + ((long)(unsigned)check_interval
)
2441 do_gettimeofday(&now
);
2443 /* Synchronize our watches */
2444 if (((1000000 - (1000000 / HZ
)) > now
.tv_usec
)
2445 && (now
.tv_usec
> (1000000 / HZ
)))
2446 difference
= (((1000000 - now
.tv_usec
) * HZ
)
2447 + 500000) / 1000000;
2448 else if (ret
== 0) {
2450 if (now
.tv_usec
> 500000)
2453 if (dev
->sa_firmware
)
2455 aac_send_safw_hostttime(dev
, &now
);
2457 ret
= aac_send_hosttime(dev
, &now
);
2459 difference
= (long)(unsigned)update_interval
*HZ
;
2462 difference
= 10 * HZ
;
2464 next_jiffies
= jiffies
+ difference
;
2465 if (time_before(next_check_jiffies
,next_jiffies
))
2466 difference
= next_check_jiffies
- jiffies
;
2468 if (difference
<= 0)
2470 set_current_state(TASK_INTERRUPTIBLE
);
2472 if (kthread_should_stop())
2475 schedule_timeout(difference
);
2477 if (kthread_should_stop())
2481 remove_wait_queue(&dev
->queues
->queue
[HostNormCmdQueue
].cmdready
, &wait
);
2482 dev
->aif_thread
= 0;
2486 int aac_acquire_irq(struct aac_dev
*dev
)
2492 if (!dev
->sync_mode
&& dev
->msi_enabled
&& dev
->max_msix
> 1) {
2493 for (i
= 0; i
< dev
->max_msix
; i
++) {
2494 dev
->aac_msix
[i
].vector_no
= i
;
2495 dev
->aac_msix
[i
].dev
= dev
;
2496 if (request_irq(pci_irq_vector(dev
->pdev
, i
),
2497 dev
->a_ops
.adapter_intr
,
2498 0, "aacraid", &(dev
->aac_msix
[i
]))) {
2499 printk(KERN_ERR
"%s%d: Failed to register IRQ for vector %d.\n",
2500 dev
->name
, dev
->id
, i
);
2501 for (j
= 0 ; j
< i
; j
++)
2502 free_irq(pci_irq_vector(dev
->pdev
, j
),
2503 &(dev
->aac_msix
[j
]));
2504 pci_disable_msix(dev
->pdev
);
2509 dev
->aac_msix
[0].vector_no
= 0;
2510 dev
->aac_msix
[0].dev
= dev
;
2512 if (request_irq(dev
->pdev
->irq
, dev
->a_ops
.adapter_intr
,
2513 IRQF_SHARED
, "aacraid",
2514 &(dev
->aac_msix
[0])) < 0) {
2516 pci_disable_msi(dev
->pdev
);
2517 printk(KERN_ERR
"%s%d: Interrupt unavailable.\n",
2518 dev
->name
, dev
->id
);
2525 void aac_free_irq(struct aac_dev
*dev
)
2530 cpu
= cpumask_first(cpu_online_mask
);
2531 if (dev
->pdev
->device
== PMC_DEVICE_S6
||
2532 dev
->pdev
->device
== PMC_DEVICE_S7
||
2533 dev
->pdev
->device
== PMC_DEVICE_S8
||
2534 dev
->pdev
->device
== PMC_DEVICE_S9
) {
2535 if (dev
->max_msix
> 1) {
2536 for (i
= 0; i
< dev
->max_msix
; i
++)
2537 free_irq(pci_irq_vector(dev
->pdev
, i
),
2538 &(dev
->aac_msix
[i
]));
2540 free_irq(dev
->pdev
->irq
, &(dev
->aac_msix
[0]));
2543 free_irq(dev
->pdev
->irq
, dev
);
2546 pci_disable_msi(dev
->pdev
);
2547 else if (dev
->max_msix
> 1)
2548 pci_disable_msix(dev
->pdev
);