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..
7 * Adaptec aacraid device driver for Linux.
9 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.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
34 #include <linux/config.h>
35 #include <linux/kernel.h>
36 #include <linux/init.h>
37 #include <linux/types.h>
38 #include <linux/sched.h>
39 #include <linux/pci.h>
40 #include <linux/spinlock.h>
41 #include <linux/slab.h>
42 #include <linux/completion.h>
43 #include <asm/semaphore.h>
44 #include <linux/blk.h>
45 #include <asm/uaccess.h>
52 * fib_map_alloc - allocate the fib objects
53 * @dev: Adapter to allocate for
55 * Allocate and map the shared PCI space for the FIB blocks used to
56 * talk to the Adaptec firmware.
59 static int fib_map_alloc(struct aac_dev *dev)
61 if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, &dev->hw_fib_pa))==NULL)
67 * fib_map_free - free the fib objects
68 * @dev: Adapter to free
70 * Free the PCI mappings and the memory allocated for FIB blocks
74 void fib_map_free(struct aac_dev *dev)
76 pci_free_consistent(dev->pdev, sizeof(struct hw_fib) * AAC_NUM_FIB, dev->hw_fib_va, dev->hw_fib_pa);
80 * fib_setup - setup the fibs
81 * @dev: Adapter to set up
83 * Allocate the PCI space for the fibs, map it and then intialise the
84 * fib area, the unmapped fib data and also the free list
87 int fib_setup(struct aac_dev * dev)
90 struct hw_fib *hw_fib_va;
94 if(fib_map_alloc(dev)<0)
97 hw_fib_va = dev->hw_fib_va;
98 hw_fib_pa = dev->hw_fib_pa;
99 memset(hw_fib_va, 0, sizeof(struct hw_fib) * AAC_NUM_FIB);
101 * Initialise the fibs
103 for (i = 0, fibptr = &dev->fibs[i]; i < AAC_NUM_FIB; i++, fibptr++)
106 fibptr->hw_fib = hw_fib_va;
107 fibptr->data = (void *) fibptr->hw_fib->data;
108 fibptr->next = fibptr+1; /* Forward chain the fibs */
109 init_MUTEX_LOCKED(&fibptr->event_wait);
110 spin_lock_init(&fibptr->event_lock);
111 hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
112 hw_fib_va->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
113 fibptr->hw_fib_pa = hw_fib_pa;
114 hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + sizeof(struct hw_fib));
115 hw_fib_pa = hw_fib_pa + sizeof(struct hw_fib);
118 * Add the fib chain to the free list
120 dev->fibs[AAC_NUM_FIB-1].next = NULL;
122 * Enable this to debug out of queue space
124 dev->free_fib = &dev->fibs[0];
129 * fib_alloc - allocate a fib
130 * @dev: Adapter to allocate the fib for
132 * Allocate a fib from the adapter fib pool. If the pool is empty we
133 * wait for fibs to become free.
136 struct fib * fib_alloc(struct aac_dev *dev)
141 spin_lock_irqsave(&dev->fib_lock, flags);
142 fibptr = dev->free_fib;
145 dev->free_fib = fibptr->next;
146 spin_unlock_irqrestore(&dev->fib_lock, flags);
148 * Set the proper node type code and node byte size
150 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
151 fibptr->size = sizeof(struct fib);
153 * Null out fields that depend on being zero at the start of
156 fibptr->hw_fib->header.XferState = cpu_to_le32(0);
157 fibptr->callback = NULL;
158 fibptr->callback_data = NULL;
164 * fib_free - free a fib
165 * @fibptr: fib to free up
167 * Frees up a fib and places it on the appropriate queue
168 * (either free or timed out)
171 void fib_free(struct fib * fibptr)
175 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
177 if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
178 aac_config.fib_timeouts++;
179 fibptr->next = fibptr->dev->timeout_fib;
180 fibptr->dev->timeout_fib = fibptr;
182 if (fibptr->hw_fib->header.XferState != 0) {
183 printk(KERN_WARNING "fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
184 (void*)fibptr, fibptr->hw_fib->header.XferState);
186 fibptr->next = fibptr->dev->free_fib;
187 fibptr->dev->free_fib = fibptr;
189 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
193 * fib_init - initialise a fib
194 * @fibptr: The fib to initialize
196 * Set up the generic fib fields ready for use
199 void fib_init(struct fib *fibptr)
201 struct hw_fib *hw_fib = fibptr->hw_fib;
203 hw_fib->header.StructType = FIB_MAGIC;
204 hw_fib->header.Size = cpu_to_le16(sizeof(struct hw_fib));
205 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
206 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
207 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
208 hw_fib->header.SenderSize = cpu_to_le16(sizeof(struct hw_fib));
212 * fib_deallocate - deallocate a fib
213 * @fibptr: fib to deallocate
215 * Will deallocate and return to the free pool the FIB pointed to by the
219 void fib_dealloc(struct fib * fibptr)
221 struct hw_fib *hw_fib = fibptr->hw_fib;
222 if(hw_fib->header.StructType != FIB_MAGIC)
224 hw_fib->header.XferState = cpu_to_le32(0);
228 * Commuication primitives define and support the queuing method we use to
229 * support host to adapter commuication. All queue accesses happen through
230 * these routines and are the only routines which have a knowledge of the
231 * how these queues are implemented.
235 * aac_get_entry - get a queue entry
238 * @entry: Entry return
239 * @index: Index return
240 * @nonotify: notification control
242 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
243 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
247 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
249 struct aac_queue * q;
252 * All of the queues wrap when they reach the end, so we check
253 * to see if they have reached the end and if they have we just
254 * set the index back to zero. This is a wrap. You could or off
255 * the high bits in all updates but this is a bit faster I think.
258 q = &dev->queues->queue[qid];
260 *index = le32_to_cpu(*(q->headers.producer));
261 if ((*index - 2) == le32_to_cpu(*(q->headers.consumer)))
264 if (qid == AdapHighCmdQueue) {
265 if (*index >= ADAP_HIGH_CMD_ENTRIES)
267 } else if (qid == AdapNormCmdQueue) {
268 if (*index >= ADAP_NORM_CMD_ENTRIES)
269 *index = 0; /* Wrap to front of the Producer Queue. */
271 else if (qid == AdapHighRespQueue)
273 if (*index >= ADAP_HIGH_RESP_ENTRIES)
276 else if (qid == AdapNormRespQueue)
278 if (*index >= ADAP_NORM_RESP_ENTRIES)
279 *index = 0; /* Wrap to front of the Producer Queue. */
283 if (*index + 1 == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
284 printk(KERN_WARNING "Queue %d full, %ld outstanding.\n", qid, q->numpending);
287 *entry = q->base + *index;
293 * aac_queue_get - get the next free QE
295 * @index: Returned index
296 * @priority: Priority of fib
297 * @fib: Fib to associate with the queue entry
298 * @wait: Wait if queue full
299 * @fibptr: Driver fib object to go with fib
300 * @nonotify: Don't notify the adapter
302 * Gets the next free QE off the requested priorty adapter command
303 * queue and associates the Fib with the QE. The QE represented by
304 * index is ready to insert on the queue when this routine returns
308 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)
310 struct aac_entry * entry = NULL;
312 struct aac_queue * q = &dev->queues->queue[qid];
314 spin_lock_irqsave(q->lock, q->SavedIrql);
316 if (qid == AdapHighCmdQueue || qid == AdapNormCmdQueue)
318 /* if no entries wait for some if caller wants to */
319 while (!aac_get_entry(dev, qid, &entry, index, nonotify))
321 printk(KERN_ERR "GetEntries failed\n");
324 * Setup queue entry with a command, status and fib mapped
326 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
329 else if (qid == AdapHighRespQueue || qid == AdapNormRespQueue)
331 while(!aac_get_entry(dev, qid, &entry, index, nonotify))
333 /* if no entries wait for some if caller wants to */
336 * Setup queue entry with command, status and fib mapped
338 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
339 entry->addr = hw_fib->header.SenderFibAddress;
340 /* Restore adapters pointer to the FIB */
341 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
345 * If MapFib is true than we need to map the Fib and put pointers
346 * in the queue entry.
349 entry->addr = fibptr->hw_fib_pa;
355 * aac_insert_entry - insert a queue entry
357 * @index: Index of entry to insert
359 * @nonotify: Suppress adapter notification
361 * Gets the next free QE off the requested priorty adapter command
362 * queue and associates the Fib with the QE. The QE represented by
363 * index is ready to insert on the queue when this routine returns
367 static int aac_insert_entry(struct aac_dev * dev, u32 index, u32 qid, unsigned long nonotify)
369 struct aac_queue * q = &dev->queues->queue[qid];
373 *(q->headers.producer) = cpu_to_le32(index + 1);
374 spin_unlock_irqrestore(q->lock, q->SavedIrql);
376 if (qid == AdapHighCmdQueue ||
377 qid == AdapNormCmdQueue ||
378 qid == AdapHighRespQueue ||
379 qid == AdapNormRespQueue)
382 aac_adapter_notify(dev, qid);
385 printk("Suprise insert!\n");
390 * Define the highest level of host to adapter communication routines.
391 * These routines will support host to adapter FS commuication. These
392 * routines have no knowledge of the commuication method used. This level
393 * sends and receives FIBs. This level has no knowledge of how these FIBs
394 * get passed back and forth.
398 * fib_send - send a fib to the adapter
399 * @command: Command to send
401 * @size: Size of fib data area
402 * @priority: Priority of Fib
403 * @wait: Async/sync select
404 * @reply: True if a reply is wanted
405 * @callback: Called with reply
406 * @callback_data: Passed to callback
408 * Sends the requested FIB to the adapter and optionally will wait for a
409 * response FIB. If the caller does not wish to wait for a response than
410 * an event to wait on must be supplied. This event will be set when a
411 * response FIB is received from the adapter.
414 int fib_send(u16 command, struct fib * fibptr, unsigned long size, int priority, int wait, int reply, fib_callback callback, void * callback_data)
418 struct aac_dev * dev = fibptr->dev;
419 unsigned long nointr = 0;
420 struct hw_fib * hw_fib = fibptr->hw_fib;
421 struct aac_queue * q;
422 unsigned long flags = 0;
424 if (!(le32_to_cpu(hw_fib->header.XferState) & HostOwned))
427 * There are 5 cases with the wait and reponse requested flags.
428 * The only invalid cases are if the caller requests to wait and
429 * does not request a response and if the caller does not want a
430 * response and the Fibis not allocated from pool. If a response
431 * is not requesed the Fib will just be deallocaed by the DPC
432 * routine when the response comes back from the adapter. No
433 * further processing will be done besides deleting the Fib. We
434 * will have a debug mode where the adapter can notify the host
435 * it had a problem and the host can log that fact.
437 if (wait && !reply) {
439 } else if (!wait && reply) {
440 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
441 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
442 } else if (!wait && !reply) {
443 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
444 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
445 } else if (wait && reply) {
446 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
447 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
450 * Map the fib into 32bits by using the fib number
453 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 1);
454 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
456 * Set FIB state to indicate where it came from and if we want a
457 * response from the adapter. Also load the command from the
460 * Map the hw fib pointer as a 32bit value
462 hw_fib->header.Command = cpu_to_le16(command);
463 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
464 fibptr->hw_fib->header.Flags = 0; /* 0 the flags field - internal only*/
466 * Set the size of the Fib we want to send to the adapter
468 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
469 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
473 * Get a queue entry connect the FIB to it and send an notify
474 * the adapter a command is ready.
476 if (priority == FsaHigh) {
477 hw_fib->header.XferState |= cpu_to_le32(HighPriority);
478 qid = AdapHighCmdQueue;
480 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
481 qid = AdapNormCmdQueue;
483 q = &dev->queues->queue[qid];
486 spin_lock_irqsave(&fibptr->event_lock, flags);
487 if(aac_queue_get( dev, &index, qid, hw_fib, 1, fibptr, &nointr)<0)
489 dprintk((KERN_DEBUG "fib_send: inserting a queue entry at index %d.\n",index));
490 dprintk((KERN_DEBUG "Fib contents:.\n"));
491 dprintk((KERN_DEBUG " Command = %d.\n", hw_fib->header.Command));
492 dprintk((KERN_DEBUG " XferState = %x.\n", hw_fib->header.XferState));
493 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib));
494 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
495 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
497 * Fill in the Callback and CallbackContext if we are not
501 fibptr->callback = callback;
502 fibptr->callback_data = callback_data;
504 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
505 list_add_tail(&fibptr->queue, &q->pendingq);
511 if(aac_insert_entry(dev, index, qid, (nointr & aac_config.irq_mod)) < 0)
514 * If the caller wanted us to wait for response wait now.
518 spin_unlock_irqrestore(&fibptr->event_lock, flags);
519 down(&fibptr->event_wait);
520 if(fibptr->done == 0)
523 if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
529 * If the user does not want a response than return success otherwise
539 * aac_consumer_get - get the top of the queue
542 * @entry: Return entry
544 * Will return a pointer to the entry on the top of the queue requested that
545 * we are a consumer of, and return the address of the queue entry. It does
546 * not change the state of the queue.
549 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
553 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
557 * The consumer index must be wrapped if we have reached
558 * the end of the queue, else we just use the entry
559 * pointed to by the header index
561 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
564 index = le32_to_cpu(*q->headers.consumer);
565 *entry = q->base + index;
571 int aac_consumer_avail(struct aac_dev *dev, struct aac_queue * q)
573 return (le32_to_cpu(*q->headers.producer) != le32_to_cpu(*q->headers.consumer));
578 * aac_consumer_free - free consumer entry
583 * Frees up the current top of the queue we are a consumer of. If the
584 * queue was full notify the producer that the queue is no longer full.
587 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
592 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
595 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
596 *q->headers.consumer = cpu_to_le32(1);
598 *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
603 case HostNormCmdQueue:
604 notify = HostNormCmdNotFull;
606 case HostHighCmdQueue:
607 notify = HostHighCmdNotFull;
609 case HostNormRespQueue:
610 notify = HostNormRespNotFull;
612 case HostHighRespQueue:
613 notify = HostHighRespNotFull;
619 aac_adapter_notify(dev, notify);
624 * fib_adapter_complete - complete adapter issued fib
625 * @fibptr: fib to complete
628 * Will do all necessary work to complete a FIB that was sent from
632 int fib_adapter_complete(struct fib * fibptr, unsigned short size)
634 struct hw_fib * hw_fib = fibptr->hw_fib;
635 struct aac_dev * dev = fibptr->dev;
636 unsigned long nointr = 0;
637 if (le32_to_cpu(hw_fib->header.XferState) == 0)
640 * If we plan to do anything check the structure type first.
642 if ( hw_fib->header.StructType != FIB_MAGIC ) {
646 * This block handles the case where the adapter had sent us a
647 * command and we have finished processing the command. We
648 * call completeFib when we are done processing the command
649 * and want to send a response back to the adapter. This will
650 * send the completed cdb to the adapter.
652 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
653 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
654 if (hw_fib->header.XferState & cpu_to_le32(HighPriority)) {
658 size += sizeof(struct aac_fibhdr);
659 if (size > le16_to_cpu(hw_fib->header.SenderSize))
661 hw_fib->header.Size = cpu_to_le16(size);
663 if(aac_queue_get(dev, &index, AdapHighRespQueue, hw_fib, 1, NULL, &nointr) < 0) {
666 if (aac_insert_entry(dev, index, AdapHighRespQueue, (nointr & (int)aac_config.irq_mod)) != 0) {
669 else if (hw_fib->header.XferState & NormalPriority)
674 size += sizeof(struct aac_fibhdr);
675 if (size > le16_to_cpu(hw_fib->header.SenderSize))
677 hw_fib->header.Size = cpu_to_le16(size);
679 if (aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr) < 0)
681 if (aac_insert_entry(dev, index, AdapNormRespQueue,
682 (nointr & (int)aac_config.irq_mod)) != 0)
689 printk(KERN_WARNING "fib_adapter_complete: Unknown xferstate detected.\n");
696 * fib_complete - fib completion handler
697 * @fib: FIB to complete
699 * Will do all necessary work to complete a FIB.
702 int fib_complete(struct fib * fibptr)
704 struct hw_fib * hw_fib = fibptr->hw_fib;
707 * Check for a fib which has already been completed
710 if (hw_fib->header.XferState == cpu_to_le32(0))
713 * If we plan to do anything check the structure type first.
716 if (hw_fib->header.StructType != FIB_MAGIC)
719 * This block completes a cdb which orginated on the host and we
720 * just need to deallocate the cdb or reinit it. At this point the
721 * command is complete that we had sent to the adapter and this
722 * cdb could be reused.
724 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
725 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
729 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
732 * This handles the case when the host has aborted the I/O
733 * to the adapter because the adapter is not responding
736 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
745 * aac_printf - handle printf from firmware
749 * Print a message passed to us by the controller firmware on the
753 void aac_printf(struct aac_dev *dev, u32 val)
755 int length = val & 0xffff;
756 int level = (val >> 16) & 0xffff;
757 char *cp = dev->printfbuf;
760 * The size of the printfbuf is set in port.c
761 * There is no variable or define for it
767 if (level == LOG_HIGH_ERROR)
768 printk(KERN_WARNING "aacraid:%s", cp);
770 printk(KERN_INFO "aacraid:%s", cp);
776 * aac_handle_aif - Handle a message from the firmware
777 * @dev: Which adapter this fib is from
778 * @fibptr: Pointer to fibptr from adapter
780 * This routine handles a driver notify fib from the adapter and
781 * dispatches it to the appropriate routine for handling.
784 #define CONTAINER_TO_BUS(cont) (0)
785 #define CONTAINER_TO_TARGET(cont) ((cont))
786 #define CONTAINER_TO_LUN(cont) (0)
788 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
790 struct hw_fib * hw_fib = fibptr->hw_fib;
791 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
795 /* Sniff for container changes */
796 dprintk ((KERN_INFO "AifCmdDriverNotify=%x\n", le32_to_cpu(*(u32 *)aifcmd->data)));
797 switch (le32_to_cpu(*(u32 *)aifcmd->data)) {
798 case AifDenMorphComplete:
799 case AifDenVolumeExtendComplete:
800 case AifEnContainerChange: /* Not really a driver notify Event */
803 container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
804 dprintk ((KERN_INFO "container=%d(%d,%d,%d,%d) ",
806 (dev && dev->scsi_host_ptr)
807 ? dev->scsi_host_ptr->host_no
809 CONTAINER_TO_BUS(container),
810 CONTAINER_TO_TARGET(container),
811 CONTAINER_TO_LUN(container)));
814 * Find the Scsi_Device associated with the SCSI address,
815 * and mark it as changed, invalidating the cache. This deals
816 * with changes to existing device IDs.
819 if ((dev != (struct aac_dev *)NULL)
820 && (dev->scsi_host_ptr != (struct Scsi_Host *)NULL)) {
821 Scsi_Device * device;
823 for (device = dev->scsi_host_ptr->host_queue;
824 device != (Scsi_Device *)NULL;
825 device = device->next) {
827 "aifd: device (%d,%d,%d,%d)?\n",
828 dev->scsi_host_ptr->host_no,
832 if ((device->channel == CONTAINER_TO_BUS(container))
833 && (device->id == CONTAINER_TO_TARGET(container))
834 && (device->lun == CONTAINER_TO_LUN(container))) {
835 busy |= (device->access_count != 0);
837 device->removable = TRUE;
842 dprintk (("busy=%d\n", busy));
846 * scan_scsis(dev->scsi_host_ptr, 1,
847 * CONTAINER_TO_BUS(container),
848 * CONTAINER_TO_TARGET(container),
849 * CONTAINER_TO_LUN(container));
851 * is not exported as accessible, so we need to go around it
852 * another way. So, we look for the "proc/scsi/scsi" entry in
853 * the proc filesystem (using proc_scsi as a shortcut) and send
854 * it a message. This deals with new devices that have
855 * appeared. If the device has gone offline, scan_scsis will
856 * also discover this, but we do not want the device to
857 * go away. We need to check the access_count for the
858 * device since we are not wanting the devices to go away.
861 && (proc_scsi != (struct proc_dir_entry *)NULL)) {
862 struct proc_dir_entry * entry;
864 dprintk((KERN_INFO "proc_scsi=%p ", proc_scsi));
865 for (entry = proc_scsi->subdir;
866 entry != (struct proc_dir_entry *)NULL;
867 entry = entry->next) {
868 dprintk(("\"%.*s\"[%d]=%x ", entry->namelen,
869 entry->name, entry->namelen, entry->low_ino));
870 if ((entry->low_ino != 0)
871 && (entry->namelen == 4)
872 && (memcmp ("scsi", entry->name, 4) == 0)) {
873 dprintk(("%p->write_proc=%p ", entry, entry->write_proc));
874 if (entry->write_proc != (int (*)(struct file *, const char *, unsigned long, void *))NULL) {
880 "scsi add-single-device %d %d %d %d\n",
881 dev->scsi_host_ptr->host_no,
882 CONTAINER_TO_BUS(container),
883 CONTAINER_TO_TARGET(container),
884 CONTAINER_TO_LUN(container));
885 length = strlen (buffer);
887 "echo %.*s > /proc/scsi/scsi\n",
892 length = entry->write_proc(
893 NULL, buffer, length, NULL);
896 "returns %d\n", length));
906 * aac_command_thread - command processing thread
907 * @dev: Adapter to monitor
909 * Waits on the commandready event in it's queue. When the event gets set
910 * it will pull FIBs off it's queue. It will continue to pull FIBs off
911 * until the queue is empty. When the queue is empty it will wait for
915 int aac_command_thread(struct aac_dev * dev)
917 struct hw_fib *hw_fib, *hw_newfib;
918 struct fib *fib, *newfib;
919 struct aac_queue_block *queues = dev->queues;
920 struct aac_fib_context *fibctx;
922 DECLARE_WAITQUEUE(wait, current);
925 * We can only have one thread per adapter for AIF's.
930 * Set up the name that will appear in 'ps'
931 * stored in task_struct.comm[16].
933 sprintf(current->comm, "aacraid");
936 * Let the DPC know it has a place to send the AIF's to.
939 add_wait_queue(&queues->queue[HostNormCmdQueue].cmdready, &wait);
940 set_current_state(TASK_INTERRUPTIBLE);
941 dprintk ((KERN_INFO "aac_command_thread start\n"));
944 spin_lock_irqsave(queues->queue[HostNormCmdQueue].lock, flags);
945 while(!list_empty(&(queues->queue[HostNormCmdQueue].cmdq))) {
946 struct list_head *entry;
947 struct aac_aifcmd * aifcmd;
949 set_current_state(TASK_RUNNING);
951 entry = queues->queue[HostNormCmdQueue].cmdq.next;
954 spin_unlock_irqrestore(queues->queue[HostNormCmdQueue].lock, flags);
955 fib = list_entry(entry, struct fib, fiblink);
957 * We will process the FIB here or pass it to a
958 * worker thread that is TBD. We Really can't
959 * do anything at this point since we don't have
960 * anything defined for this thread to do.
962 hw_fib = fib->hw_fib;
964 memset(fib, 0, sizeof(struct fib));
965 fib->type = FSAFS_NTC_FIB_CONTEXT;
966 fib->size = sizeof( struct fib );
967 fib->hw_fib = hw_fib;
968 fib->data = hw_fib->data;
971 * We only handle AifRequest fibs from the adapter.
973 aifcmd = (struct aac_aifcmd *) hw_fib->data;
974 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
975 /* Handle Driver Notify Events */
976 aac_handle_aif(dev, fib);
977 *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
978 fib_adapter_complete(fib, sizeof(u32));
980 struct list_head *entry;
981 /* The u32 here is important and intended. We are using
982 32bit wrapping time to fit the adapter field */
984 u32 time_now, time_last;
988 if (aifcmd->command == cpu_to_le32(AifCmdEventNotify))
989 aac_handle_aif(dev, fib);
991 time_now = jiffies/HZ;
993 spin_lock_irqsave(&dev->fib_lock, flagv);
994 entry = dev->fib_list.next;
996 * For each Context that is on the
997 * fibctxList, make a copy of the
998 * fib, and then set the event to wake up the
999 * thread that is waiting for it.
1001 while (entry != &dev->fib_list) {
1003 * Extract the fibctx
1005 fibctx = list_entry(entry, struct aac_fib_context, next);
1007 * Check if the queue is getting
1010 if (fibctx->count > 20)
1013 * It's *not* jiffies folks,
1014 * but jiffies / HZ, so do not
1017 time_last = fibctx->jiffies;
1019 * Has it been > 2 minutes
1020 * since the last read off
1023 if ((time_now - time_last) > 120) {
1024 entry = entry->next;
1025 aac_close_fib_context(dev, fibctx);
1030 * Warning: no sleep allowed while
1033 hw_newfib = kmalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1034 newfib = kmalloc(sizeof(struct fib), GFP_ATOMIC);
1035 if (newfib && hw_newfib) {
1037 * Make the copy of the FIB
1038 * FIXME: check if we need to fix other fields up
1040 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1041 memcpy(newfib, fib, sizeof(struct fib));
1042 newfib->hw_fib = hw_newfib;
1044 * Put the FIB onto the
1047 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1050 * Set the event to wake up the
1051 * thread that will waiting.
1053 up(&fibctx->wait_sem);
1055 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1061 entry = entry->next;
1064 * Set the status of this FIB
1066 *(u32 *)hw_fib->data = cpu_to_le32(ST_OK);
1067 fib_adapter_complete(fib, sizeof(u32));
1068 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1070 spin_lock_irqsave(queues->queue[HostNormCmdQueue].lock, flags);
1074 * There are no more AIF's
1076 spin_unlock_irqrestore(queues->queue[HostNormCmdQueue].lock, flags);
1079 if(signal_pending(current))
1081 set_current_state(TASK_INTERRUPTIBLE);
1083 remove_wait_queue(&queues->queue[HostNormCmdQueue].cmdready, &wait);
1084 dev->aif_thread = 0;
1085 complete_and_exit(&dev->aif_completion, 0);