2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Abstract: Contain all routines that are required for FSA host/adapter
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/sched.h>
36 #include <linux/pci.h>
37 #include <linux/spinlock.h>
38 #include <linux/slab.h>
39 #include <linux/completion.h>
40 #include <linux/blkdev.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi_device.h>
43 #include <asm/semaphore.h>
44 #include <asm/delay.h>
49 * fib_map_alloc - allocate the fib objects
50 * @dev: Adapter to allocate for
52 * Allocate and map the shared PCI space for the FIB blocks used to
53 * talk to the Adaptec firmware.
56 static int fib_map_alloc(struct aac_dev
*dev
)
59 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
60 dev
->pdev
, dev
->max_fib_size
, dev
->scsi_host_ptr
->can_queue
,
61 AAC_NUM_MGT_FIB
, &dev
->hw_fib_pa
));
62 if((dev
->hw_fib_va
= pci_alloc_consistent(dev
->pdev
, dev
->max_fib_size
63 * (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
),
64 &dev
->hw_fib_pa
))==NULL
)
70 * fib_map_free - free the fib objects
71 * @dev: Adapter to free
73 * Free the PCI mappings and the memory allocated for FIB blocks
77 void fib_map_free(struct aac_dev
*dev
)
79 pci_free_consistent(dev
->pdev
, dev
->max_fib_size
* (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
), dev
->hw_fib_va
, dev
->hw_fib_pa
);
83 * fib_setup - setup the fibs
84 * @dev: Adapter to set up
86 * Allocate the PCI space for the fibs, map it and then intialise the
87 * fib area, the unmapped fib data and also the free list
90 int fib_setup(struct aac_dev
* dev
)
93 struct hw_fib
*hw_fib_va
;
97 while (((i
= fib_map_alloc(dev
)) == -ENOMEM
)
98 && (dev
->scsi_host_ptr
->can_queue
> (64 - AAC_NUM_MGT_FIB
))) {
99 dev
->init
->MaxIoCommands
= cpu_to_le32((dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
) >> 1);
100 dev
->scsi_host_ptr
->can_queue
= le32_to_cpu(dev
->init
->MaxIoCommands
) - AAC_NUM_MGT_FIB
;
105 hw_fib_va
= dev
->hw_fib_va
;
106 hw_fib_pa
= dev
->hw_fib_pa
;
107 memset(hw_fib_va
, 0, dev
->max_fib_size
* (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
));
109 * Initialise the fibs
111 for (i
= 0, fibptr
= &dev
->fibs
[i
]; i
< (dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
); i
++, fibptr
++)
114 fibptr
->hw_fib
= hw_fib_va
;
115 fibptr
->data
= (void *) fibptr
->hw_fib
->data
;
116 fibptr
->next
= fibptr
+1; /* Forward chain the fibs */
117 init_MUTEX_LOCKED(&fibptr
->event_wait
);
118 spin_lock_init(&fibptr
->event_lock
);
119 hw_fib_va
->header
.XferState
= cpu_to_le32(0xffffffff);
120 hw_fib_va
->header
.SenderSize
= cpu_to_le16(dev
->max_fib_size
);
121 fibptr
->hw_fib_pa
= hw_fib_pa
;
122 hw_fib_va
= (struct hw_fib
*)((unsigned char *)hw_fib_va
+ dev
->max_fib_size
);
123 hw_fib_pa
= hw_fib_pa
+ dev
->max_fib_size
;
126 * Add the fib chain to the free list
128 dev
->fibs
[dev
->scsi_host_ptr
->can_queue
+ AAC_NUM_MGT_FIB
- 1].next
= NULL
;
130 * Enable this to debug out of queue space
132 dev
->free_fib
= &dev
->fibs
[0];
137 * fib_alloc - allocate a fib
138 * @dev: Adapter to allocate the fib for
140 * Allocate a fib from the adapter fib pool. If the pool is empty we
144 struct fib
* fib_alloc(struct aac_dev
*dev
)
148 spin_lock_irqsave(&dev
->fib_lock
, flags
);
149 fibptr
= dev
->free_fib
;
151 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
154 dev
->free_fib
= fibptr
->next
;
155 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
157 * Set the proper node type code and node byte size
159 fibptr
->type
= FSAFS_NTC_FIB_CONTEXT
;
160 fibptr
->size
= sizeof(struct fib
);
162 * Null out fields that depend on being zero at the start of
165 fibptr
->hw_fib
->header
.XferState
= 0;
166 fibptr
->callback
= NULL
;
167 fibptr
->callback_data
= NULL
;
173 * fib_free - free a fib
174 * @fibptr: fib to free up
176 * Frees up a fib and places it on the appropriate queue
177 * (either free or timed out)
180 void fib_free(struct fib
* fibptr
)
184 spin_lock_irqsave(&fibptr
->dev
->fib_lock
, flags
);
185 if (fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
) {
186 aac_config
.fib_timeouts
++;
187 fibptr
->next
= fibptr
->dev
->timeout_fib
;
188 fibptr
->dev
->timeout_fib
= fibptr
;
190 if (fibptr
->hw_fib
->header
.XferState
!= 0) {
191 printk(KERN_WARNING
"fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
193 le32_to_cpu(fibptr
->hw_fib
->header
.XferState
));
195 fibptr
->next
= fibptr
->dev
->free_fib
;
196 fibptr
->dev
->free_fib
= fibptr
;
198 spin_unlock_irqrestore(&fibptr
->dev
->fib_lock
, flags
);
202 * fib_init - initialise a fib
203 * @fibptr: The fib to initialize
205 * Set up the generic fib fields ready for use
208 void fib_init(struct fib
*fibptr
)
210 struct hw_fib
*hw_fib
= fibptr
->hw_fib
;
212 hw_fib
->header
.StructType
= FIB_MAGIC
;
213 hw_fib
->header
.Size
= cpu_to_le16(fibptr
->dev
->max_fib_size
);
214 hw_fib
->header
.XferState
= cpu_to_le32(HostOwned
| FibInitialized
| FibEmpty
| FastResponseCapable
);
215 hw_fib
->header
.SenderFibAddress
= cpu_to_le32(fibptr
->hw_fib_pa
);
216 hw_fib
->header
.ReceiverFibAddress
= cpu_to_le32(fibptr
->hw_fib_pa
);
217 hw_fib
->header
.SenderSize
= cpu_to_le16(fibptr
->dev
->max_fib_size
);
221 * fib_deallocate - deallocate a fib
222 * @fibptr: fib to deallocate
224 * Will deallocate and return to the free pool the FIB pointed to by the
228 static void fib_dealloc(struct fib
* fibptr
)
230 struct hw_fib
*hw_fib
= fibptr
->hw_fib
;
231 if(hw_fib
->header
.StructType
!= FIB_MAGIC
)
233 hw_fib
->header
.XferState
= 0;
237 * Commuication primitives define and support the queuing method we use to
238 * support host to adapter commuication. All queue accesses happen through
239 * these routines and are the only routines which have a knowledge of the
240 * how these queues are implemented.
244 * aac_get_entry - get a queue entry
247 * @entry: Entry return
248 * @index: Index return
249 * @nonotify: notification control
251 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
252 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
256 static int aac_get_entry (struct aac_dev
* dev
, u32 qid
, struct aac_entry
**entry
, u32
* index
, unsigned long *nonotify
)
258 struct aac_queue
* q
;
262 * All of the queues wrap when they reach the end, so we check
263 * to see if they have reached the end and if they have we just
264 * set the index back to zero. This is a wrap. You could or off
265 * the high bits in all updates but this is a bit faster I think.
268 q
= &dev
->queues
->queue
[qid
];
270 idx
= *index
= le32_to_cpu(*(q
->headers
.producer
));
271 /* Interrupt Moderation, only interrupt for first two entries */
272 if (idx
!= le32_to_cpu(*(q
->headers
.consumer
))) {
274 if (qid
== AdapNormCmdQueue
)
275 idx
= ADAP_NORM_CMD_ENTRIES
;
277 idx
= ADAP_NORM_RESP_ENTRIES
;
279 if (idx
!= le32_to_cpu(*(q
->headers
.consumer
)))
283 if (qid
== AdapNormCmdQueue
) {
284 if (*index
>= ADAP_NORM_CMD_ENTRIES
)
285 *index
= 0; /* Wrap to front of the Producer Queue. */
287 if (*index
>= ADAP_NORM_RESP_ENTRIES
)
288 *index
= 0; /* Wrap to front of the Producer Queue. */
291 if ((*index
+ 1) == le32_to_cpu(*(q
->headers
.consumer
))) { /* Queue is full */
292 printk(KERN_WARNING
"Queue %d full, %u outstanding.\n",
296 *entry
= q
->base
+ *index
;
302 * aac_queue_get - get the next free QE
304 * @index: Returned index
305 * @priority: Priority of fib
306 * @fib: Fib to associate with the queue entry
307 * @wait: Wait if queue full
308 * @fibptr: Driver fib object to go with fib
309 * @nonotify: Don't notify the adapter
311 * Gets the next free QE off the requested priorty adapter command
312 * queue and associates the Fib with the QE. The QE represented by
313 * index is ready to insert on the queue when this routine returns
317 static 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
)
319 struct aac_entry
* entry
= NULL
;
322 if (qid
== AdapNormCmdQueue
) {
323 /* if no entries wait for some if caller wants to */
324 while (!aac_get_entry(dev
, qid
, &entry
, index
, nonotify
))
326 printk(KERN_ERR
"GetEntries failed\n");
329 * Setup queue entry with a command, status and fib mapped
331 entry
->size
= cpu_to_le32(le16_to_cpu(hw_fib
->header
.Size
));
334 while(!aac_get_entry(dev
, qid
, &entry
, index
, nonotify
))
336 /* if no entries wait for some if caller wants to */
339 * Setup queue entry with command, status and fib mapped
341 entry
->size
= cpu_to_le32(le16_to_cpu(hw_fib
->header
.Size
));
342 entry
->addr
= hw_fib
->header
.SenderFibAddress
;
343 /* Restore adapters pointer to the FIB */
344 hw_fib
->header
.ReceiverFibAddress
= hw_fib
->header
.SenderFibAddress
; /* Let the adapter now where to find its data */
348 * If MapFib is true than we need to map the Fib and put pointers
349 * in the queue entry.
352 entry
->addr
= cpu_to_le32(fibptr
->hw_fib_pa
);
357 * Define the highest level of host to adapter communication routines.
358 * These routines will support host to adapter FS commuication. These
359 * routines have no knowledge of the commuication method used. This level
360 * sends and receives FIBs. This level has no knowledge of how these FIBs
361 * get passed back and forth.
365 * fib_send - send a fib to the adapter
366 * @command: Command to send
368 * @size: Size of fib data area
369 * @priority: Priority of Fib
370 * @wait: Async/sync select
371 * @reply: True if a reply is wanted
372 * @callback: Called with reply
373 * @callback_data: Passed to callback
375 * Sends the requested FIB to the adapter and optionally will wait for a
376 * response FIB. If the caller does not wish to wait for a response than
377 * an event to wait on must be supplied. This event will be set when a
378 * response FIB is received from the adapter.
381 int fib_send(u16 command
, struct fib
* fibptr
, unsigned long size
, int priority
, int wait
, int reply
, fib_callback callback
, void * callback_data
)
384 struct aac_dev
* dev
= fibptr
->dev
;
385 unsigned long nointr
= 0;
386 struct hw_fib
* hw_fib
= fibptr
->hw_fib
;
387 struct aac_queue
* q
;
388 unsigned long flags
= 0;
389 unsigned long qflags
;
391 if (!(hw_fib
->header
.XferState
& cpu_to_le32(HostOwned
)))
394 * There are 5 cases with the wait and reponse requested flags.
395 * The only invalid cases are if the caller requests to wait and
396 * does not request a response and if the caller does not want a
397 * response and the Fib is not allocated from pool. If a response
398 * is not requesed the Fib will just be deallocaed by the DPC
399 * routine when the response comes back from the adapter. No
400 * further processing will be done besides deleting the Fib. We
401 * will have a debug mode where the adapter can notify the host
402 * it had a problem and the host can log that fact.
404 if (wait
&& !reply
) {
406 } else if (!wait
&& reply
) {
407 hw_fib
->header
.XferState
|= cpu_to_le32(Async
| ResponseExpected
);
408 FIB_COUNTER_INCREMENT(aac_config
.AsyncSent
);
409 } else if (!wait
&& !reply
) {
410 hw_fib
->header
.XferState
|= cpu_to_le32(NoResponseExpected
);
411 FIB_COUNTER_INCREMENT(aac_config
.NoResponseSent
);
412 } else if (wait
&& reply
) {
413 hw_fib
->header
.XferState
|= cpu_to_le32(ResponseExpected
);
414 FIB_COUNTER_INCREMENT(aac_config
.NormalSent
);
417 * Map the fib into 32bits by using the fib number
420 hw_fib
->header
.SenderFibAddress
= cpu_to_le32(((u32
)(fibptr
-dev
->fibs
)) << 1);
421 hw_fib
->header
.SenderData
= (u32
)(fibptr
- dev
->fibs
);
423 * Set FIB state to indicate where it came from and if we want a
424 * response from the adapter. Also load the command from the
427 * Map the hw fib pointer as a 32bit value
429 hw_fib
->header
.Command
= cpu_to_le16(command
);
430 hw_fib
->header
.XferState
|= cpu_to_le32(SentFromHost
);
431 fibptr
->hw_fib
->header
.Flags
= 0; /* 0 the flags field - internal only*/
433 * Set the size of the Fib we want to send to the adapter
435 hw_fib
->header
.Size
= cpu_to_le16(sizeof(struct aac_fibhdr
) + size
);
436 if (le16_to_cpu(hw_fib
->header
.Size
) > le16_to_cpu(hw_fib
->header
.SenderSize
)) {
440 * Get a queue entry connect the FIB to it and send an notify
441 * the adapter a command is ready.
443 hw_fib
->header
.XferState
|= cpu_to_le32(NormalPriority
);
446 * Fill in the Callback and CallbackContext if we are not
450 fibptr
->callback
= callback
;
451 fibptr
->callback_data
= callback_data
;
457 FIB_COUNTER_INCREMENT(aac_config
.FibsSent
);
459 dprintk((KERN_DEBUG
"fib_send: inserting a queue entry at index %d.\n",index
));
460 dprintk((KERN_DEBUG
"Fib contents:.\n"));
461 dprintk((KERN_DEBUG
" Command = %d.\n", hw_fib
->header
.Command
));
462 dprintk((KERN_DEBUG
" XferState = %x.\n", hw_fib
->header
.XferState
));
463 dprintk((KERN_DEBUG
" hw_fib va being sent=%p\n",fibptr
->hw_fib
));
464 dprintk((KERN_DEBUG
" hw_fib pa being sent=%lx\n",(ulong
)fibptr
->hw_fib_pa
));
465 dprintk((KERN_DEBUG
" fib being sent=%p\n",fibptr
));
467 q
= &dev
->queues
->queue
[AdapNormCmdQueue
];
470 spin_lock_irqsave(&fibptr
->event_lock
, flags
);
471 spin_lock_irqsave(q
->lock
, qflags
);
472 aac_queue_get( dev
, &index
, AdapNormCmdQueue
, hw_fib
, 1, fibptr
, &nointr
);
474 list_add_tail(&fibptr
->queue
, &q
->pendingq
);
476 *(q
->headers
.producer
) = cpu_to_le32(index
+ 1);
477 spin_unlock_irqrestore(q
->lock
, qflags
);
478 if (!(nointr
& aac_config
.irq_mod
))
479 aac_adapter_notify(dev
, AdapNormCmdQueue
);
481 * If the caller wanted us to wait for response wait now.
485 spin_unlock_irqrestore(&fibptr
->event_lock
, flags
);
486 /* Only set for first known interruptable command */
489 * *VERY* Dangerous to time out a command, the
490 * assumption is made that we have no hope of
491 * functioning because an interrupt routing or other
492 * hardware failure has occurred.
494 unsigned long count
= 36000000L; /* 3 minutes */
495 unsigned long qflags
;
496 while (down_trylock(&fibptr
->event_wait
)) {
498 spin_lock_irqsave(q
->lock
, qflags
);
500 list_del(&fibptr
->queue
);
501 spin_unlock_irqrestore(q
->lock
, qflags
);
503 printk(KERN_ERR
"aacraid: fib_send: first asynchronous command timed out.\n"
504 "Usually a result of a PCI interrupt routing problem;\n"
505 "update mother board BIOS or consider utilizing one of\n"
506 "the SAFE mode kernel options (acpi, apic etc)\n");
513 down(&fibptr
->event_wait
);
514 if(fibptr
->done
== 0)
517 if((fibptr
->flags
& FIB_CONTEXT_FLAG_TIMED_OUT
)){
524 * If the user does not want a response than return success otherwise
534 * aac_consumer_get - get the top of the queue
537 * @entry: Return entry
539 * Will return a pointer to the entry on the top of the queue requested that
540 * we are a consumer of, and return the address of the queue entry. It does
541 * not change the state of the queue.
544 int aac_consumer_get(struct aac_dev
* dev
, struct aac_queue
* q
, struct aac_entry
**entry
)
548 if (le32_to_cpu(*q
->headers
.producer
) == le32_to_cpu(*q
->headers
.consumer
)) {
552 * The consumer index must be wrapped if we have reached
553 * the end of the queue, else we just use the entry
554 * pointed to by the header index
556 if (le32_to_cpu(*q
->headers
.consumer
) >= q
->entries
)
559 index
= le32_to_cpu(*q
->headers
.consumer
);
560 *entry
= q
->base
+ index
;
567 * aac_consumer_free - free consumer entry
572 * Frees up the current top of the queue we are a consumer of. If the
573 * queue was full notify the producer that the queue is no longer full.
576 void aac_consumer_free(struct aac_dev
* dev
, struct aac_queue
*q
, u32 qid
)
581 if ((le32_to_cpu(*q
->headers
.producer
)+1) == le32_to_cpu(*q
->headers
.consumer
))
584 if (le32_to_cpu(*q
->headers
.consumer
) >= q
->entries
)
585 *q
->headers
.consumer
= cpu_to_le32(1);
587 *q
->headers
.consumer
= cpu_to_le32(le32_to_cpu(*q
->headers
.consumer
)+1);
592 case HostNormCmdQueue
:
593 notify
= HostNormCmdNotFull
;
595 case HostNormRespQueue
:
596 notify
= HostNormRespNotFull
;
602 aac_adapter_notify(dev
, notify
);
607 * fib_adapter_complete - complete adapter issued fib
608 * @fibptr: fib to complete
611 * Will do all necessary work to complete a FIB that was sent from
615 int fib_adapter_complete(struct fib
* fibptr
, unsigned short size
)
617 struct hw_fib
* hw_fib
= fibptr
->hw_fib
;
618 struct aac_dev
* dev
= fibptr
->dev
;
619 struct aac_queue
* q
;
620 unsigned long nointr
= 0;
621 unsigned long qflags
;
623 if (hw_fib
->header
.XferState
== 0) {
627 * If we plan to do anything check the structure type first.
629 if ( hw_fib
->header
.StructType
!= FIB_MAGIC
) {
633 * This block handles the case where the adapter had sent us a
634 * command and we have finished processing the command. We
635 * call completeFib when we are done processing the command
636 * and want to send a response back to the adapter. This will
637 * send the completed cdb to the adapter.
639 if (hw_fib
->header
.XferState
& cpu_to_le32(SentFromAdapter
)) {
641 hw_fib
->header
.XferState
|= cpu_to_le32(HostProcessed
);
643 size
+= sizeof(struct aac_fibhdr
);
644 if (size
> le16_to_cpu(hw_fib
->header
.SenderSize
))
646 hw_fib
->header
.Size
= cpu_to_le16(size
);
648 q
= &dev
->queues
->queue
[AdapNormRespQueue
];
649 spin_lock_irqsave(q
->lock
, qflags
);
650 aac_queue_get(dev
, &index
, AdapNormRespQueue
, hw_fib
, 1, NULL
, &nointr
);
651 *(q
->headers
.producer
) = cpu_to_le32(index
+ 1);
652 spin_unlock_irqrestore(q
->lock
, qflags
);
653 if (!(nointr
& (int)aac_config
.irq_mod
))
654 aac_adapter_notify(dev
, AdapNormRespQueue
);
658 printk(KERN_WARNING
"fib_adapter_complete: Unknown xferstate detected.\n");
665 * fib_complete - fib completion handler
666 * @fib: FIB to complete
668 * Will do all necessary work to complete a FIB.
671 int fib_complete(struct fib
* fibptr
)
673 struct hw_fib
* hw_fib
= fibptr
->hw_fib
;
676 * Check for a fib which has already been completed
679 if (hw_fib
->header
.XferState
== 0)
682 * If we plan to do anything check the structure type first.
685 if (hw_fib
->header
.StructType
!= FIB_MAGIC
)
688 * This block completes a cdb which orginated on the host and we
689 * just need to deallocate the cdb or reinit it. At this point the
690 * command is complete that we had sent to the adapter and this
691 * cdb could be reused.
693 if((hw_fib
->header
.XferState
& cpu_to_le32(SentFromHost
)) &&
694 (hw_fib
->header
.XferState
& cpu_to_le32(AdapterProcessed
)))
698 else if(hw_fib
->header
.XferState
& cpu_to_le32(SentFromHost
))
701 * This handles the case when the host has aborted the I/O
702 * to the adapter because the adapter is not responding
705 } else if(hw_fib
->header
.XferState
& cpu_to_le32(HostOwned
)) {
714 * aac_printf - handle printf from firmware
718 * Print a message passed to us by the controller firmware on the
722 void aac_printf(struct aac_dev
*dev
, u32 val
)
724 char *cp
= dev
->printfbuf
;
725 if (dev
->printf_enabled
)
727 int length
= val
& 0xffff;
728 int level
= (val
>> 16) & 0xffff;
731 * The size of the printfbuf is set in port.c
732 * There is no variable or define for it
738 if (level
== LOG_AAC_HIGH_ERROR
)
739 printk(KERN_WARNING
"aacraid:%s", cp
);
741 printk(KERN_INFO
"aacraid:%s", cp
);
748 * aac_handle_aif - Handle a message from the firmware
749 * @dev: Which adapter this fib is from
750 * @fibptr: Pointer to fibptr from adapter
752 * This routine handles a driver notify fib from the adapter and
753 * dispatches it to the appropriate routine for handling.
756 static void aac_handle_aif(struct aac_dev
* dev
, struct fib
* fibptr
)
758 struct hw_fib
* hw_fib
= fibptr
->hw_fib
;
759 struct aac_aifcmd
* aifcmd
= (struct aac_aifcmd
*)hw_fib
->data
;
762 struct scsi_device
*device
;
768 } device_config_needed
;
770 /* Sniff for container changes */
777 * We have set this up to try and minimize the number of
778 * re-configures that take place. As a result of this when
779 * certain AIF's come in we will set a flag waiting for another
780 * type of AIF before setting the re-config flag.
782 switch (le32_to_cpu(aifcmd
->command
)) {
783 case AifCmdDriverNotify
:
784 switch (le32_to_cpu(((u32
*)aifcmd
->data
)[0])) {
786 * Morph or Expand complete
788 case AifDenMorphComplete
:
789 case AifDenVolumeExtendComplete
:
790 container
= le32_to_cpu(((u32
*)aifcmd
->data
)[1]);
791 if (container
>= dev
->maximum_num_containers
)
795 * Find the Scsi_Device associated with the SCSI
796 * address. Make sure we have the right array, and if
797 * so set the flag to initiate a new re-config once we
798 * see an AifEnConfigChange AIF come through.
801 if ((dev
!= NULL
) && (dev
->scsi_host_ptr
!= NULL
)) {
802 device
= scsi_device_lookup(dev
->scsi_host_ptr
,
803 CONTAINER_TO_CHANNEL(container
),
804 CONTAINER_TO_ID(container
),
805 CONTAINER_TO_LUN(container
));
807 dev
->fsa_dev
[container
].config_needed
= CHANGE
;
808 dev
->fsa_dev
[container
].config_waiting_on
= AifEnConfigChange
;
809 scsi_device_put(device
);
815 * If we are waiting on something and this happens to be
816 * that thing then set the re-configure flag.
818 if (container
!= (u32
)-1) {
819 if (container
>= dev
->maximum_num_containers
)
821 if (dev
->fsa_dev
[container
].config_waiting_on
==
822 le32_to_cpu(*(u32
*)aifcmd
->data
))
823 dev
->fsa_dev
[container
].config_waiting_on
= 0;
824 } else for (container
= 0;
825 container
< dev
->maximum_num_containers
; ++container
) {
826 if (dev
->fsa_dev
[container
].config_waiting_on
==
827 le32_to_cpu(*(u32
*)aifcmd
->data
))
828 dev
->fsa_dev
[container
].config_waiting_on
= 0;
832 case AifCmdEventNotify
:
833 switch (le32_to_cpu(((u32
*)aifcmd
->data
)[0])) {
837 case AifEnAddContainer
:
838 container
= le32_to_cpu(((u32
*)aifcmd
->data
)[1]);
839 if (container
>= dev
->maximum_num_containers
)
841 dev
->fsa_dev
[container
].config_needed
= ADD
;
842 dev
->fsa_dev
[container
].config_waiting_on
=
849 case AifEnDeleteContainer
:
850 container
= le32_to_cpu(((u32
*)aifcmd
->data
)[1]);
851 if (container
>= dev
->maximum_num_containers
)
853 dev
->fsa_dev
[container
].config_needed
= DELETE
;
854 dev
->fsa_dev
[container
].config_waiting_on
=
859 * Container change detected. If we currently are not
860 * waiting on something else, setup to wait on a Config Change.
862 case AifEnContainerChange
:
863 container
= le32_to_cpu(((u32
*)aifcmd
->data
)[1]);
864 if (container
>= dev
->maximum_num_containers
)
866 if (dev
->fsa_dev
[container
].config_waiting_on
)
868 dev
->fsa_dev
[container
].config_needed
= CHANGE
;
869 dev
->fsa_dev
[container
].config_waiting_on
=
873 case AifEnConfigChange
:
879 * If we are waiting on something and this happens to be
880 * that thing then set the re-configure flag.
882 if (container
!= (u32
)-1) {
883 if (container
>= dev
->maximum_num_containers
)
885 if (dev
->fsa_dev
[container
].config_waiting_on
==
886 le32_to_cpu(*(u32
*)aifcmd
->data
))
887 dev
->fsa_dev
[container
].config_waiting_on
= 0;
888 } else for (container
= 0;
889 container
< dev
->maximum_num_containers
; ++container
) {
890 if (dev
->fsa_dev
[container
].config_waiting_on
==
891 le32_to_cpu(*(u32
*)aifcmd
->data
))
892 dev
->fsa_dev
[container
].config_waiting_on
= 0;
896 case AifCmdJobProgress
:
898 * These are job progress AIF's. When a Clear is being
899 * done on a container it is initially created then hidden from
900 * the OS. When the clear completes we don't get a config
901 * change so we monitor the job status complete on a clear then
902 * wait for a container change.
905 if ((((u32
*)aifcmd
->data
)[1] == cpu_to_le32(AifJobCtrZero
))
906 && ((((u32
*)aifcmd
->data
)[6] == ((u32
*)aifcmd
->data
)[5])
907 || (((u32
*)aifcmd
->data
)[4] == cpu_to_le32(AifJobStsSuccess
)))) {
909 container
< dev
->maximum_num_containers
;
912 * Stomp on all config sequencing for all
915 dev
->fsa_dev
[container
].config_waiting_on
=
916 AifEnContainerChange
;
917 dev
->fsa_dev
[container
].config_needed
= ADD
;
920 if ((((u32
*)aifcmd
->data
)[1] == cpu_to_le32(AifJobCtrZero
))
921 && (((u32
*)aifcmd
->data
)[6] == 0)
922 && (((u32
*)aifcmd
->data
)[4] == cpu_to_le32(AifJobStsRunning
))) {
924 container
< dev
->maximum_num_containers
;
927 * Stomp on all config sequencing for all
930 dev
->fsa_dev
[container
].config_waiting_on
=
931 AifEnContainerChange
;
932 dev
->fsa_dev
[container
].config_needed
= DELETE
;
938 device_config_needed
= NOTHING
;
939 for (container
= 0; container
< dev
->maximum_num_containers
;
941 if ((dev
->fsa_dev
[container
].config_waiting_on
== 0)
942 && (dev
->fsa_dev
[container
].config_needed
!= NOTHING
)) {
943 device_config_needed
=
944 dev
->fsa_dev
[container
].config_needed
;
945 dev
->fsa_dev
[container
].config_needed
= NOTHING
;
949 if (device_config_needed
== NOTHING
)
953 * If we decided that a re-configuration needs to be done,
954 * schedule it here on the way out the door, please close the door
962 * Find the Scsi_Device associated with the SCSI address,
963 * and mark it as changed, invalidating the cache. This deals
964 * with changes to existing device IDs.
967 if (!dev
|| !dev
->scsi_host_ptr
)
970 * force reload of disk info via probe_container
972 if ((device_config_needed
== CHANGE
)
973 && (dev
->fsa_dev
[container
].valid
== 1))
974 dev
->fsa_dev
[container
].valid
= 2;
975 if ((device_config_needed
== CHANGE
) ||
976 (device_config_needed
== ADD
))
977 probe_container(dev
, container
);
978 device
= scsi_device_lookup(dev
->scsi_host_ptr
,
979 CONTAINER_TO_CHANNEL(container
),
980 CONTAINER_TO_ID(container
),
981 CONTAINER_TO_LUN(container
));
983 switch (device_config_needed
) {
985 scsi_remove_device(device
);
988 if (!dev
->fsa_dev
[container
].valid
) {
989 scsi_remove_device(device
);
992 scsi_rescan_device(&device
->sdev_gendev
);
997 scsi_device_put(device
);
999 if (device_config_needed
== ADD
) {
1000 scsi_add_device(dev
->scsi_host_ptr
,
1001 CONTAINER_TO_CHANNEL(container
),
1002 CONTAINER_TO_ID(container
),
1003 CONTAINER_TO_LUN(container
));
1009 * aac_command_thread - command processing thread
1010 * @dev: Adapter to monitor
1012 * Waits on the commandready event in it's queue. When the event gets set
1013 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1014 * until the queue is empty. When the queue is empty it will wait for
1018 int aac_command_thread(struct aac_dev
* dev
)
1020 struct hw_fib
*hw_fib
, *hw_newfib
;
1021 struct fib
*fib
, *newfib
;
1022 struct aac_fib_context
*fibctx
;
1023 unsigned long flags
;
1024 DECLARE_WAITQUEUE(wait
, current
);
1027 * We can only have one thread per adapter for AIF's.
1029 if (dev
->aif_thread
)
1032 * Set up the name that will appear in 'ps'
1033 * stored in task_struct.comm[16].
1035 daemonize("aacraid");
1036 allow_signal(SIGKILL
);
1038 * Let the DPC know it has a place to send the AIF's to.
1040 dev
->aif_thread
= 1;
1041 add_wait_queue(&dev
->queues
->queue
[HostNormCmdQueue
].cmdready
, &wait
);
1042 set_current_state(TASK_INTERRUPTIBLE
);
1043 dprintk ((KERN_INFO
"aac_command_thread start\n"));
1046 spin_lock_irqsave(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1047 while(!list_empty(&(dev
->queues
->queue
[HostNormCmdQueue
].cmdq
))) {
1048 struct list_head
*entry
;
1049 struct aac_aifcmd
* aifcmd
;
1051 set_current_state(TASK_RUNNING
);
1053 entry
= dev
->queues
->queue
[HostNormCmdQueue
].cmdq
.next
;
1056 spin_unlock_irqrestore(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1057 fib
= list_entry(entry
, struct fib
, fiblink
);
1059 * We will process the FIB here or pass it to a
1060 * worker thread that is TBD. We Really can't
1061 * do anything at this point since we don't have
1062 * anything defined for this thread to do.
1064 hw_fib
= fib
->hw_fib
;
1065 memset(fib
, 0, sizeof(struct fib
));
1066 fib
->type
= FSAFS_NTC_FIB_CONTEXT
;
1067 fib
->size
= sizeof( struct fib
);
1068 fib
->hw_fib
= hw_fib
;
1069 fib
->data
= hw_fib
->data
;
1072 * We only handle AifRequest fibs from the adapter.
1074 aifcmd
= (struct aac_aifcmd
*) hw_fib
->data
;
1075 if (aifcmd
->command
== cpu_to_le32(AifCmdDriverNotify
)) {
1076 /* Handle Driver Notify Events */
1077 aac_handle_aif(dev
, fib
);
1078 *(__le32
*)hw_fib
->data
= cpu_to_le32(ST_OK
);
1079 fib_adapter_complete(fib
, (u16
)sizeof(u32
));
1081 struct list_head
*entry
;
1082 /* The u32 here is important and intended. We are using
1083 32bit wrapping time to fit the adapter field */
1085 u32 time_now
, time_last
;
1086 unsigned long flagv
;
1088 struct hw_fib
** hw_fib_pool
, ** hw_fib_p
;
1089 struct fib
** fib_pool
, ** fib_p
;
1092 if ((aifcmd
->command
==
1093 cpu_to_le32(AifCmdEventNotify
)) ||
1095 cpu_to_le32(AifCmdJobProgress
))) {
1096 aac_handle_aif(dev
, fib
);
1099 time_now
= jiffies
/HZ
;
1102 * Warning: no sleep allowed while
1103 * holding spinlock. We take the estimate
1104 * and pre-allocate a set of fibs outside the
1107 num
= le32_to_cpu(dev
->init
->AdapterFibsSize
)
1108 / sizeof(struct hw_fib
); /* some extra */
1109 spin_lock_irqsave(&dev
->fib_lock
, flagv
);
1110 entry
= dev
->fib_list
.next
;
1111 while (entry
!= &dev
->fib_list
) {
1112 entry
= entry
->next
;
1115 spin_unlock_irqrestore(&dev
->fib_lock
, flagv
);
1119 && ((hw_fib_pool
= kmalloc(sizeof(struct hw_fib
*) * num
, GFP_KERNEL
)))
1120 && ((fib_pool
= kmalloc(sizeof(struct fib
*) * num
, GFP_KERNEL
)))) {
1121 hw_fib_p
= hw_fib_pool
;
1123 while (hw_fib_p
< &hw_fib_pool
[num
]) {
1124 if (!(*(hw_fib_p
++) = kmalloc(sizeof(struct hw_fib
), GFP_KERNEL
))) {
1128 if (!(*(fib_p
++) = kmalloc(sizeof(struct fib
), GFP_KERNEL
))) {
1129 kfree(*(--hw_fib_p
));
1133 if ((num
= hw_fib_p
- hw_fib_pool
) == 0) {
1139 } else if (hw_fib_pool
) {
1143 spin_lock_irqsave(&dev
->fib_lock
, flagv
);
1144 entry
= dev
->fib_list
.next
;
1146 * For each Context that is on the
1147 * fibctxList, make a copy of the
1148 * fib, and then set the event to wake up the
1149 * thread that is waiting for it.
1151 hw_fib_p
= hw_fib_pool
;
1153 while (entry
!= &dev
->fib_list
) {
1155 * Extract the fibctx
1157 fibctx
= list_entry(entry
, struct aac_fib_context
, next
);
1159 * Check if the queue is getting
1162 if (fibctx
->count
> 20)
1165 * It's *not* jiffies folks,
1166 * but jiffies / HZ so do not
1169 time_last
= fibctx
->jiffies
;
1171 * Has it been > 2 minutes
1172 * since the last read off
1175 if ((time_now
- time_last
) > 120) {
1176 entry
= entry
->next
;
1177 aac_close_fib_context(dev
, fibctx
);
1182 * Warning: no sleep allowed while
1185 if (hw_fib_p
< &hw_fib_pool
[num
]) {
1186 hw_newfib
= *hw_fib_p
;
1187 *(hw_fib_p
++) = NULL
;
1191 * Make the copy of the FIB
1193 memcpy(hw_newfib
, hw_fib
, sizeof(struct hw_fib
));
1194 memcpy(newfib
, fib
, sizeof(struct fib
));
1195 newfib
->hw_fib
= hw_newfib
;
1197 * Put the FIB onto the
1200 list_add_tail(&newfib
->fiblink
, &fibctx
->fib_list
);
1203 * Set the event to wake up the
1204 * thread that is waiting.
1206 up(&fibctx
->wait_sem
);
1208 printk(KERN_WARNING
"aifd: didn't allocate NewFib.\n");
1210 entry
= entry
->next
;
1213 * Set the status of this FIB
1215 *(__le32
*)hw_fib
->data
= cpu_to_le32(ST_OK
);
1216 fib_adapter_complete(fib
, sizeof(u32
));
1217 spin_unlock_irqrestore(&dev
->fib_lock
, flagv
);
1218 /* Free up the remaining resources */
1219 hw_fib_p
= hw_fib_pool
;
1221 while (hw_fib_p
< &hw_fib_pool
[num
]) {
1235 spin_lock_irqsave(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1238 * There are no more AIF's
1240 spin_unlock_irqrestore(dev
->queues
->queue
[HostNormCmdQueue
].lock
, flags
);
1243 if(signal_pending(current
))
1245 set_current_state(TASK_INTERRUPTIBLE
);
1248 remove_wait_queue(&dev
->queues
->queue
[HostNormCmdQueue
].cmdready
, &wait
);
1249 dev
->aif_thread
= 0;
1250 complete_and_exit(&dev
->aif_completion
, 0);