USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / scsi / aacraid / commsup.c
blob4b32ca442433036860bc819eff2909efc6155e46
1 /*
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)
14 * any later version.
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.
25 * Module Name:
26 * commsup.c
28 * Abstract: Contain all routines that are required for FSA host/adapter
29 * communication.
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/sched.h>
37 #include <linux/pci.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/completion.h>
41 #include <linux/blkdev.h>
42 #include <linux/delay.h>
43 #include <linux/kthread.h>
44 #include <linux/interrupt.h>
45 #include <linux/semaphore.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_cmnd.h>
51 #include "aacraid.h"
53 /**
54 * fib_map_alloc - allocate the fib objects
55 * @dev: Adapter to allocate for
57 * Allocate and map the shared PCI space for the FIB blocks used to
58 * talk to the Adaptec firmware.
61 static int fib_map_alloc(struct aac_dev *dev)
63 dprintk((KERN_INFO
64 "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
65 dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
66 AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
67 dev->hw_fib_va = pci_alloc_consistent(dev->pdev,
68 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr))
69 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
70 &dev->hw_fib_pa);
71 if (dev->hw_fib_va == NULL)
72 return -ENOMEM;
73 return 0;
76 /**
77 * aac_fib_map_free - free the fib objects
78 * @dev: Adapter to free
80 * Free the PCI mappings and the memory allocated for FIB blocks
81 * on this adapter.
84 void aac_fib_map_free(struct aac_dev *dev)
86 pci_free_consistent(dev->pdev,
87 dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
88 dev->hw_fib_va, dev->hw_fib_pa);
89 dev->hw_fib_va = NULL;
90 dev->hw_fib_pa = 0;
93 /**
94 * aac_fib_setup - setup the fibs
95 * @dev: Adapter to set up
97 * Allocate the PCI space for the fibs, map it and then initialise the
98 * fib area, the unmapped fib data and also the free list
101 int aac_fib_setup(struct aac_dev * dev)
103 struct fib *fibptr;
104 struct hw_fib *hw_fib;
105 dma_addr_t hw_fib_pa;
106 int i;
108 while (((i = fib_map_alloc(dev)) == -ENOMEM)
109 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
110 dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
111 dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
113 if (i<0)
114 return -ENOMEM;
116 /* 32 byte alignment for PMC */
117 hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
118 dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
119 (hw_fib_pa - dev->hw_fib_pa));
120 dev->hw_fib_pa = hw_fib_pa;
121 memset(dev->hw_fib_va, 0,
122 (dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
123 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
125 /* add Xport header */
126 dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
127 sizeof(struct aac_fib_xporthdr));
128 dev->hw_fib_pa += sizeof(struct aac_fib_xporthdr);
130 hw_fib = dev->hw_fib_va;
131 hw_fib_pa = dev->hw_fib_pa;
133 * Initialise the fibs
135 for (i = 0, fibptr = &dev->fibs[i];
136 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
137 i++, fibptr++)
139 fibptr->dev = dev;
140 fibptr->hw_fib_va = hw_fib;
141 fibptr->data = (void *) fibptr->hw_fib_va->data;
142 fibptr->next = fibptr+1; /* Forward chain the fibs */
143 sema_init(&fibptr->event_wait, 0);
144 spin_lock_init(&fibptr->event_lock);
145 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
146 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
147 fibptr->hw_fib_pa = hw_fib_pa;
148 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
149 dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
150 hw_fib_pa = hw_fib_pa +
151 dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
154 * Add the fib chain to the free list
156 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
158 * Enable this to debug out of queue space
160 dev->free_fib = &dev->fibs[0];
161 return 0;
165 * aac_fib_alloc - allocate a fib
166 * @dev: Adapter to allocate the fib for
168 * Allocate a fib from the adapter fib pool. If the pool is empty we
169 * return NULL.
172 struct fib *aac_fib_alloc(struct aac_dev *dev)
174 struct fib * fibptr;
175 unsigned long flags;
176 spin_lock_irqsave(&dev->fib_lock, flags);
177 fibptr = dev->free_fib;
178 if(!fibptr){
179 spin_unlock_irqrestore(&dev->fib_lock, flags);
180 return fibptr;
182 dev->free_fib = fibptr->next;
183 spin_unlock_irqrestore(&dev->fib_lock, flags);
185 * Set the proper node type code and node byte size
187 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
188 fibptr->size = sizeof(struct fib);
190 * Null out fields that depend on being zero at the start of
191 * each I/O
193 fibptr->hw_fib_va->header.XferState = 0;
194 fibptr->flags = 0;
195 fibptr->callback = NULL;
196 fibptr->callback_data = NULL;
198 return fibptr;
202 * aac_fib_free - free a fib
203 * @fibptr: fib to free up
205 * Frees up a fib and places it on the appropriate queue
208 void aac_fib_free(struct fib *fibptr)
210 unsigned long flags, flagsv;
212 spin_lock_irqsave(&fibptr->event_lock, flagsv);
213 if (fibptr->done == 2) {
214 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
215 return;
217 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
219 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
220 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
221 aac_config.fib_timeouts++;
222 if (fibptr->hw_fib_va->header.XferState != 0) {
223 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
224 (void*)fibptr,
225 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
227 fibptr->next = fibptr->dev->free_fib;
228 fibptr->dev->free_fib = fibptr;
229 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
233 * aac_fib_init - initialise a fib
234 * @fibptr: The fib to initialize
236 * Set up the generic fib fields ready for use
239 void aac_fib_init(struct fib *fibptr)
241 struct hw_fib *hw_fib = fibptr->hw_fib_va;
243 hw_fib->header.StructType = FIB_MAGIC;
244 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
245 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
246 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
247 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
248 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
252 * fib_deallocate - deallocate a fib
253 * @fibptr: fib to deallocate
255 * Will deallocate and return to the free pool the FIB pointed to by the
256 * caller.
259 static void fib_dealloc(struct fib * fibptr)
261 struct hw_fib *hw_fib = fibptr->hw_fib_va;
262 BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
263 hw_fib->header.XferState = 0;
267 * Commuication primitives define and support the queuing method we use to
268 * support host to adapter commuication. All queue accesses happen through
269 * these routines and are the only routines which have a knowledge of the
270 * how these queues are implemented.
274 * aac_get_entry - get a queue entry
275 * @dev: Adapter
276 * @qid: Queue Number
277 * @entry: Entry return
278 * @index: Index return
279 * @nonotify: notification control
281 * With a priority the routine returns a queue entry if the queue has free entries. If the queue
282 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
283 * returned.
286 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
288 struct aac_queue * q;
289 unsigned long idx;
292 * All of the queues wrap when they reach the end, so we check
293 * to see if they have reached the end and if they have we just
294 * set the index back to zero. This is a wrap. You could or off
295 * the high bits in all updates but this is a bit faster I think.
298 q = &dev->queues->queue[qid];
300 idx = *index = le32_to_cpu(*(q->headers.producer));
301 /* Interrupt Moderation, only interrupt for first two entries */
302 if (idx != le32_to_cpu(*(q->headers.consumer))) {
303 if (--idx == 0) {
304 if (qid == AdapNormCmdQueue)
305 idx = ADAP_NORM_CMD_ENTRIES;
306 else
307 idx = ADAP_NORM_RESP_ENTRIES;
309 if (idx != le32_to_cpu(*(q->headers.consumer)))
310 *nonotify = 1;
313 if (qid == AdapNormCmdQueue) {
314 if (*index >= ADAP_NORM_CMD_ENTRIES)
315 *index = 0; /* Wrap to front of the Producer Queue. */
316 } else {
317 if (*index >= ADAP_NORM_RESP_ENTRIES)
318 *index = 0; /* Wrap to front of the Producer Queue. */
321 /* Queue is full */
322 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
323 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
324 qid, q->numpending);
325 return 0;
326 } else {
327 *entry = q->base + *index;
328 return 1;
333 * aac_queue_get - get the next free QE
334 * @dev: Adapter
335 * @index: Returned index
336 * @priority: Priority of fib
337 * @fib: Fib to associate with the queue entry
338 * @wait: Wait if queue full
339 * @fibptr: Driver fib object to go with fib
340 * @nonotify: Don't notify the adapter
342 * Gets the next free QE off the requested priorty adapter command
343 * queue and associates the Fib with the QE. The QE represented by
344 * index is ready to insert on the queue when this routine returns
345 * success.
348 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)
350 struct aac_entry * entry = NULL;
351 int map = 0;
353 if (qid == AdapNormCmdQueue) {
354 /* if no entries wait for some if caller wants to */
355 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
356 printk(KERN_ERR "GetEntries failed\n");
359 * Setup queue entry with a command, status and fib mapped
361 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
362 map = 1;
363 } else {
364 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
365 /* if no entries wait for some if caller wants to */
368 * Setup queue entry with command, status and fib mapped
370 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
371 entry->addr = hw_fib->header.SenderFibAddress;
372 /* Restore adapters pointer to the FIB */
373 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
374 map = 0;
377 * If MapFib is true than we need to map the Fib and put pointers
378 * in the queue entry.
380 if (map)
381 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
382 return 0;
386 * Define the highest level of host to adapter communication routines.
387 * These routines will support host to adapter FS commuication. These
388 * routines have no knowledge of the commuication method used. This level
389 * sends and receives FIBs. This level has no knowledge of how these FIBs
390 * get passed back and forth.
394 * aac_fib_send - send a fib to the adapter
395 * @command: Command to send
396 * @fibptr: The fib
397 * @size: Size of fib data area
398 * @priority: Priority of Fib
399 * @wait: Async/sync select
400 * @reply: True if a reply is wanted
401 * @callback: Called with reply
402 * @callback_data: Passed to callback
404 * Sends the requested FIB to the adapter and optionally will wait for a
405 * response FIB. If the caller does not wish to wait for a response than
406 * an event to wait on must be supplied. This event will be set when a
407 * response FIB is received from the adapter.
410 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
411 int priority, int wait, int reply, fib_callback callback,
412 void *callback_data)
414 struct aac_dev * dev = fibptr->dev;
415 struct hw_fib * hw_fib = fibptr->hw_fib_va;
416 unsigned long flags = 0;
417 unsigned long qflags;
418 unsigned long mflags = 0;
419 unsigned long sflags = 0;
422 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
423 return -EBUSY;
425 * There are 5 cases with the wait and response requested flags.
426 * The only invalid cases are if the caller requests to wait and
427 * does not request a response and if the caller does not want a
428 * response and the Fib is not allocated from pool. If a response
429 * is not requesed the Fib will just be deallocaed by the DPC
430 * routine when the response comes back from the adapter. No
431 * further processing will be done besides deleting the Fib. We
432 * will have a debug mode where the adapter can notify the host
433 * it had a problem and the host can log that fact.
435 fibptr->flags = 0;
436 if (wait && !reply) {
437 return -EINVAL;
438 } else if (!wait && reply) {
439 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
440 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
441 } else if (!wait && !reply) {
442 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
443 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
444 } else if (wait && reply) {
445 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
446 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
449 * Map the fib into 32bits by using the fib number
452 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
453 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
455 * Set FIB state to indicate where it came from and if we want a
456 * response from the adapter. Also load the command from the
457 * caller.
459 * Map the hw fib pointer as a 32bit value
461 hw_fib->header.Command = cpu_to_le16(command);
462 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
463 fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
465 * Set the size of the Fib we want to send to the adapter
467 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
468 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
469 return -EMSGSIZE;
472 * Get a queue entry connect the FIB to it and send an notify
473 * the adapter a command is ready.
475 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
478 * Fill in the Callback and CallbackContext if we are not
479 * going to wait.
481 if (!wait) {
482 fibptr->callback = callback;
483 fibptr->callback_data = callback_data;
484 fibptr->flags = FIB_CONTEXT_FLAG;
487 fibptr->done = 0;
489 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
491 dprintk((KERN_DEBUG "Fib contents:.\n"));
492 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
493 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
494 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
495 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
496 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
497 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
499 if (!dev->queues)
500 return -EBUSY;
502 if (wait) {
504 spin_lock_irqsave(&dev->manage_lock, mflags);
505 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
506 printk(KERN_INFO "No management Fibs Available:%d\n",
507 dev->management_fib_count);
508 spin_unlock_irqrestore(&dev->manage_lock, mflags);
509 return -EBUSY;
511 dev->management_fib_count++;
512 spin_unlock_irqrestore(&dev->manage_lock, mflags);
513 spin_lock_irqsave(&fibptr->event_lock, flags);
516 if (dev->sync_mode) {
517 if (wait)
518 spin_unlock_irqrestore(&fibptr->event_lock, flags);
519 spin_lock_irqsave(&dev->sync_lock, sflags);
520 if (dev->sync_fib) {
521 list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
522 spin_unlock_irqrestore(&dev->sync_lock, sflags);
523 } else {
524 dev->sync_fib = fibptr;
525 spin_unlock_irqrestore(&dev->sync_lock, sflags);
526 aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
527 (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
528 NULL, NULL, NULL, NULL, NULL);
530 if (wait) {
531 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
532 if (down_interruptible(&fibptr->event_wait)) {
533 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
534 return -EFAULT;
536 return 0;
538 return -EINPROGRESS;
541 if (aac_adapter_deliver(fibptr) != 0) {
542 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
543 if (wait) {
544 spin_unlock_irqrestore(&fibptr->event_lock, flags);
545 spin_lock_irqsave(&dev->manage_lock, mflags);
546 dev->management_fib_count--;
547 spin_unlock_irqrestore(&dev->manage_lock, mflags);
549 return -EBUSY;
554 * If the caller wanted us to wait for response wait now.
557 if (wait) {
558 spin_unlock_irqrestore(&fibptr->event_lock, flags);
559 /* Only set for first known interruptable command */
560 if (wait < 0) {
562 * *VERY* Dangerous to time out a command, the
563 * assumption is made that we have no hope of
564 * functioning because an interrupt routing or other
565 * hardware failure has occurred.
567 unsigned long count = 36000000L; /* 3 minutes */
568 while (down_trylock(&fibptr->event_wait)) {
569 int blink;
570 if (--count == 0) {
571 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
572 spin_lock_irqsave(q->lock, qflags);
573 q->numpending--;
574 spin_unlock_irqrestore(q->lock, qflags);
575 if (wait == -1) {
576 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
577 "Usually a result of a PCI interrupt routing problem;\n"
578 "update mother board BIOS or consider utilizing one of\n"
579 "the SAFE mode kernel options (acpi, apic etc)\n");
581 return -ETIMEDOUT;
583 if ((blink = aac_adapter_check_health(dev)) > 0) {
584 if (wait == -1) {
585 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
586 "Usually a result of a serious unrecoverable hardware problem\n",
587 blink);
589 return -EFAULT;
591 udelay(5);
593 } else if (down_interruptible(&fibptr->event_wait)) {
594 /* Do nothing ... satisfy
595 * down_interruptible must_check */
598 spin_lock_irqsave(&fibptr->event_lock, flags);
599 if (fibptr->done == 0) {
600 fibptr->done = 2; /* Tell interrupt we aborted */
601 spin_unlock_irqrestore(&fibptr->event_lock, flags);
602 return -ERESTARTSYS;
604 spin_unlock_irqrestore(&fibptr->event_lock, flags);
605 BUG_ON(fibptr->done == 0);
607 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
608 return -ETIMEDOUT;
609 return 0;
612 * If the user does not want a response than return success otherwise
613 * return pending
615 if (reply)
616 return -EINPROGRESS;
617 else
618 return 0;
622 * aac_consumer_get - get the top of the queue
623 * @dev: Adapter
624 * @q: Queue
625 * @entry: Return entry
627 * Will return a pointer to the entry on the top of the queue requested that
628 * we are a consumer of, and return the address of the queue entry. It does
629 * not change the state of the queue.
632 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
634 u32 index;
635 int status;
636 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
637 status = 0;
638 } else {
640 * The consumer index must be wrapped if we have reached
641 * the end of the queue, else we just use the entry
642 * pointed to by the header index
644 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
645 index = 0;
646 else
647 index = le32_to_cpu(*q->headers.consumer);
648 *entry = q->base + index;
649 status = 1;
651 return(status);
655 * aac_consumer_free - free consumer entry
656 * @dev: Adapter
657 * @q: Queue
658 * @qid: Queue ident
660 * Frees up the current top of the queue we are a consumer of. If the
661 * queue was full notify the producer that the queue is no longer full.
664 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
666 int wasfull = 0;
667 u32 notify;
669 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
670 wasfull = 1;
672 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
673 *q->headers.consumer = cpu_to_le32(1);
674 else
675 le32_add_cpu(q->headers.consumer, 1);
677 if (wasfull) {
678 switch (qid) {
680 case HostNormCmdQueue:
681 notify = HostNormCmdNotFull;
682 break;
683 case HostNormRespQueue:
684 notify = HostNormRespNotFull;
685 break;
686 default:
687 BUG();
688 return;
690 aac_adapter_notify(dev, notify);
695 * aac_fib_adapter_complete - complete adapter issued fib
696 * @fibptr: fib to complete
697 * @size: size of fib
699 * Will do all necessary work to complete a FIB that was sent from
700 * the adapter.
703 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
705 struct hw_fib * hw_fib = fibptr->hw_fib_va;
706 struct aac_dev * dev = fibptr->dev;
707 struct aac_queue * q;
708 unsigned long nointr = 0;
709 unsigned long qflags;
711 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
712 kfree(hw_fib);
713 return 0;
716 if (hw_fib->header.XferState == 0) {
717 if (dev->comm_interface == AAC_COMM_MESSAGE)
718 kfree(hw_fib);
719 return 0;
722 * If we plan to do anything check the structure type first.
724 if (hw_fib->header.StructType != FIB_MAGIC) {
725 if (dev->comm_interface == AAC_COMM_MESSAGE)
726 kfree(hw_fib);
727 return -EINVAL;
730 * This block handles the case where the adapter had sent us a
731 * command and we have finished processing the command. We
732 * call completeFib when we are done processing the command
733 * and want to send a response back to the adapter. This will
734 * send the completed cdb to the adapter.
736 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
737 if (dev->comm_interface == AAC_COMM_MESSAGE) {
738 kfree (hw_fib);
739 } else {
740 u32 index;
741 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
742 if (size) {
743 size += sizeof(struct aac_fibhdr);
744 if (size > le16_to_cpu(hw_fib->header.SenderSize))
745 return -EMSGSIZE;
746 hw_fib->header.Size = cpu_to_le16(size);
748 q = &dev->queues->queue[AdapNormRespQueue];
749 spin_lock_irqsave(q->lock, qflags);
750 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
751 *(q->headers.producer) = cpu_to_le32(index + 1);
752 spin_unlock_irqrestore(q->lock, qflags);
753 if (!(nointr & (int)aac_config.irq_mod))
754 aac_adapter_notify(dev, AdapNormRespQueue);
756 } else {
757 printk(KERN_WARNING "aac_fib_adapter_complete: "
758 "Unknown xferstate detected.\n");
759 BUG();
761 return 0;
765 * aac_fib_complete - fib completion handler
766 * @fib: FIB to complete
768 * Will do all necessary work to complete a FIB.
771 int aac_fib_complete(struct fib *fibptr)
773 unsigned long flags;
774 struct hw_fib * hw_fib = fibptr->hw_fib_va;
777 * Check for a fib which has already been completed
780 if (hw_fib->header.XferState == 0)
781 return 0;
783 * If we plan to do anything check the structure type first.
786 if (hw_fib->header.StructType != FIB_MAGIC)
787 return -EINVAL;
789 * This block completes a cdb which orginated on the host and we
790 * just need to deallocate the cdb or reinit it. At this point the
791 * command is complete that we had sent to the adapter and this
792 * cdb could be reused.
794 spin_lock_irqsave(&fibptr->event_lock, flags);
795 if (fibptr->done == 2) {
796 spin_unlock_irqrestore(&fibptr->event_lock, flags);
797 return 0;
799 spin_unlock_irqrestore(&fibptr->event_lock, flags);
801 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
802 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
804 fib_dealloc(fibptr);
806 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
809 * This handles the case when the host has aborted the I/O
810 * to the adapter because the adapter is not responding
812 fib_dealloc(fibptr);
813 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
814 fib_dealloc(fibptr);
815 } else {
816 BUG();
818 return 0;
822 * aac_printf - handle printf from firmware
823 * @dev: Adapter
824 * @val: Message info
826 * Print a message passed to us by the controller firmware on the
827 * Adaptec board
830 void aac_printf(struct aac_dev *dev, u32 val)
832 char *cp = dev->printfbuf;
833 if (dev->printf_enabled)
835 int length = val & 0xffff;
836 int level = (val >> 16) & 0xffff;
839 * The size of the printfbuf is set in port.c
840 * There is no variable or define for it
842 if (length > 255)
843 length = 255;
844 if (cp[length] != 0)
845 cp[length] = 0;
846 if (level == LOG_AAC_HIGH_ERROR)
847 printk(KERN_WARNING "%s:%s", dev->name, cp);
848 else
849 printk(KERN_INFO "%s:%s", dev->name, cp);
851 memset(cp, 0, 256);
856 * aac_handle_aif - Handle a message from the firmware
857 * @dev: Which adapter this fib is from
858 * @fibptr: Pointer to fibptr from adapter
860 * This routine handles a driver notify fib from the adapter and
861 * dispatches it to the appropriate routine for handling.
864 #define AIF_SNIFF_TIMEOUT (30*HZ)
865 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
867 struct hw_fib * hw_fib = fibptr->hw_fib_va;
868 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
869 u32 channel, id, lun, container;
870 struct scsi_device *device;
871 enum {
872 NOTHING,
873 DELETE,
874 ADD,
875 CHANGE
876 } device_config_needed = NOTHING;
878 /* Sniff for container changes */
880 if (!dev || !dev->fsa_dev)
881 return;
882 container = channel = id = lun = (u32)-1;
885 * We have set this up to try and minimize the number of
886 * re-configures that take place. As a result of this when
887 * certain AIF's come in we will set a flag waiting for another
888 * type of AIF before setting the re-config flag.
890 switch (le32_to_cpu(aifcmd->command)) {
891 case AifCmdDriverNotify:
892 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
894 * Morph or Expand complete
896 case AifDenMorphComplete:
897 case AifDenVolumeExtendComplete:
898 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
899 if (container >= dev->maximum_num_containers)
900 break;
903 * Find the scsi_device associated with the SCSI
904 * address. Make sure we have the right array, and if
905 * so set the flag to initiate a new re-config once we
906 * see an AifEnConfigChange AIF come through.
909 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
910 device = scsi_device_lookup(dev->scsi_host_ptr,
911 CONTAINER_TO_CHANNEL(container),
912 CONTAINER_TO_ID(container),
913 CONTAINER_TO_LUN(container));
914 if (device) {
915 dev->fsa_dev[container].config_needed = CHANGE;
916 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
917 dev->fsa_dev[container].config_waiting_stamp = jiffies;
918 scsi_device_put(device);
924 * If we are waiting on something and this happens to be
925 * that thing then set the re-configure flag.
927 if (container != (u32)-1) {
928 if (container >= dev->maximum_num_containers)
929 break;
930 if ((dev->fsa_dev[container].config_waiting_on ==
931 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
932 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
933 dev->fsa_dev[container].config_waiting_on = 0;
934 } else for (container = 0;
935 container < dev->maximum_num_containers; ++container) {
936 if ((dev->fsa_dev[container].config_waiting_on ==
937 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
938 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
939 dev->fsa_dev[container].config_waiting_on = 0;
941 break;
943 case AifCmdEventNotify:
944 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
945 case AifEnBatteryEvent:
946 dev->cache_protected =
947 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
948 break;
950 * Add an Array.
952 case AifEnAddContainer:
953 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
954 if (container >= dev->maximum_num_containers)
955 break;
956 dev->fsa_dev[container].config_needed = ADD;
957 dev->fsa_dev[container].config_waiting_on =
958 AifEnConfigChange;
959 dev->fsa_dev[container].config_waiting_stamp = jiffies;
960 break;
963 * Delete an Array.
965 case AifEnDeleteContainer:
966 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
967 if (container >= dev->maximum_num_containers)
968 break;
969 dev->fsa_dev[container].config_needed = DELETE;
970 dev->fsa_dev[container].config_waiting_on =
971 AifEnConfigChange;
972 dev->fsa_dev[container].config_waiting_stamp = jiffies;
973 break;
976 * Container change detected. If we currently are not
977 * waiting on something else, setup to wait on a Config Change.
979 case AifEnContainerChange:
980 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
981 if (container >= dev->maximum_num_containers)
982 break;
983 if (dev->fsa_dev[container].config_waiting_on &&
984 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
985 break;
986 dev->fsa_dev[container].config_needed = CHANGE;
987 dev->fsa_dev[container].config_waiting_on =
988 AifEnConfigChange;
989 dev->fsa_dev[container].config_waiting_stamp = jiffies;
990 break;
992 case AifEnConfigChange:
993 break;
995 case AifEnAddJBOD:
996 case AifEnDeleteJBOD:
997 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
998 if ((container >> 28)) {
999 container = (u32)-1;
1000 break;
1002 channel = (container >> 24) & 0xF;
1003 if (channel >= dev->maximum_num_channels) {
1004 container = (u32)-1;
1005 break;
1007 id = container & 0xFFFF;
1008 if (id >= dev->maximum_num_physicals) {
1009 container = (u32)-1;
1010 break;
1012 lun = (container >> 16) & 0xFF;
1013 container = (u32)-1;
1014 channel = aac_phys_to_logical(channel);
1015 device_config_needed =
1016 (((__le32 *)aifcmd->data)[0] ==
1017 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
1018 if (device_config_needed == ADD) {
1019 device = scsi_device_lookup(dev->scsi_host_ptr,
1020 channel,
1022 lun);
1023 if (device) {
1024 scsi_remove_device(device);
1025 scsi_device_put(device);
1028 break;
1030 case AifEnEnclosureManagement:
1032 * If in JBOD mode, automatic exposure of new
1033 * physical target to be suppressed until configured.
1035 if (dev->jbod)
1036 break;
1037 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1038 case EM_DRIVE_INSERTION:
1039 case EM_DRIVE_REMOVAL:
1040 container = le32_to_cpu(
1041 ((__le32 *)aifcmd->data)[2]);
1042 if ((container >> 28)) {
1043 container = (u32)-1;
1044 break;
1046 channel = (container >> 24) & 0xF;
1047 if (channel >= dev->maximum_num_channels) {
1048 container = (u32)-1;
1049 break;
1051 id = container & 0xFFFF;
1052 lun = (container >> 16) & 0xFF;
1053 container = (u32)-1;
1054 if (id >= dev->maximum_num_physicals) {
1055 /* legacy dev_t ? */
1056 if ((0x2000 <= id) || lun || channel ||
1057 ((channel = (id >> 7) & 0x3F) >=
1058 dev->maximum_num_channels))
1059 break;
1060 lun = (id >> 4) & 7;
1061 id &= 0xF;
1063 channel = aac_phys_to_logical(channel);
1064 device_config_needed =
1065 (((__le32 *)aifcmd->data)[3]
1066 == cpu_to_le32(EM_DRIVE_INSERTION)) ?
1067 ADD : DELETE;
1068 break;
1070 break;
1074 * If we are waiting on something and this happens to be
1075 * that thing then set the re-configure flag.
1077 if (container != (u32)-1) {
1078 if (container >= dev->maximum_num_containers)
1079 break;
1080 if ((dev->fsa_dev[container].config_waiting_on ==
1081 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1082 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1083 dev->fsa_dev[container].config_waiting_on = 0;
1084 } else for (container = 0;
1085 container < dev->maximum_num_containers; ++container) {
1086 if ((dev->fsa_dev[container].config_waiting_on ==
1087 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1088 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1089 dev->fsa_dev[container].config_waiting_on = 0;
1091 break;
1093 case AifCmdJobProgress:
1095 * These are job progress AIF's. When a Clear is being
1096 * done on a container it is initially created then hidden from
1097 * the OS. When the clear completes we don't get a config
1098 * change so we monitor the job status complete on a clear then
1099 * wait for a container change.
1102 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1103 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1104 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1105 for (container = 0;
1106 container < dev->maximum_num_containers;
1107 ++container) {
1109 * Stomp on all config sequencing for all
1110 * containers?
1112 dev->fsa_dev[container].config_waiting_on =
1113 AifEnContainerChange;
1114 dev->fsa_dev[container].config_needed = ADD;
1115 dev->fsa_dev[container].config_waiting_stamp =
1116 jiffies;
1119 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1120 ((__le32 *)aifcmd->data)[6] == 0 &&
1121 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1122 for (container = 0;
1123 container < dev->maximum_num_containers;
1124 ++container) {
1126 * Stomp on all config sequencing for all
1127 * containers?
1129 dev->fsa_dev[container].config_waiting_on =
1130 AifEnContainerChange;
1131 dev->fsa_dev[container].config_needed = DELETE;
1132 dev->fsa_dev[container].config_waiting_stamp =
1133 jiffies;
1136 break;
1139 container = 0;
1140 retry_next:
1141 if (device_config_needed == NOTHING)
1142 for (; container < dev->maximum_num_containers; ++container) {
1143 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1144 (dev->fsa_dev[container].config_needed != NOTHING) &&
1145 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1146 device_config_needed =
1147 dev->fsa_dev[container].config_needed;
1148 dev->fsa_dev[container].config_needed = NOTHING;
1149 channel = CONTAINER_TO_CHANNEL(container);
1150 id = CONTAINER_TO_ID(container);
1151 lun = CONTAINER_TO_LUN(container);
1152 break;
1155 if (device_config_needed == NOTHING)
1156 return;
1159 * If we decided that a re-configuration needs to be done,
1160 * schedule it here on the way out the door, please close the door
1161 * behind you.
1165 * Find the scsi_device associated with the SCSI address,
1166 * and mark it as changed, invalidating the cache. This deals
1167 * with changes to existing device IDs.
1170 if (!dev || !dev->scsi_host_ptr)
1171 return;
1173 * force reload of disk info via aac_probe_container
1175 if ((channel == CONTAINER_CHANNEL) &&
1176 (device_config_needed != NOTHING)) {
1177 if (dev->fsa_dev[container].valid == 1)
1178 dev->fsa_dev[container].valid = 2;
1179 aac_probe_container(dev, container);
1181 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1182 if (device) {
1183 switch (device_config_needed) {
1184 case DELETE:
1185 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1186 scsi_remove_device(device);
1187 #else
1188 if (scsi_device_online(device)) {
1189 scsi_device_set_state(device, SDEV_OFFLINE);
1190 sdev_printk(KERN_INFO, device,
1191 "Device offlined - %s\n",
1192 (channel == CONTAINER_CHANNEL) ?
1193 "array deleted" :
1194 "enclosure services event");
1196 #endif
1197 break;
1198 case ADD:
1199 if (!scsi_device_online(device)) {
1200 sdev_printk(KERN_INFO, device,
1201 "Device online - %s\n",
1202 (channel == CONTAINER_CHANNEL) ?
1203 "array created" :
1204 "enclosure services event");
1205 scsi_device_set_state(device, SDEV_RUNNING);
1207 /* FALLTHRU */
1208 case CHANGE:
1209 if ((channel == CONTAINER_CHANNEL)
1210 && (!dev->fsa_dev[container].valid)) {
1211 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1212 scsi_remove_device(device);
1213 #else
1214 if (!scsi_device_online(device))
1215 break;
1216 scsi_device_set_state(device, SDEV_OFFLINE);
1217 sdev_printk(KERN_INFO, device,
1218 "Device offlined - %s\n",
1219 "array failed");
1220 #endif
1221 break;
1223 scsi_rescan_device(&device->sdev_gendev);
1225 default:
1226 break;
1228 scsi_device_put(device);
1229 device_config_needed = NOTHING;
1231 if (device_config_needed == ADD)
1232 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1233 if (channel == CONTAINER_CHANNEL) {
1234 container++;
1235 device_config_needed = NOTHING;
1236 goto retry_next;
1240 static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1242 int index, quirks;
1243 int retval;
1244 struct Scsi_Host *host;
1245 struct scsi_device *dev;
1246 struct scsi_cmnd *command;
1247 struct scsi_cmnd *command_list;
1248 int jafo = 0;
1251 * Assumptions:
1252 * - host is locked, unless called by the aacraid thread.
1253 * (a matter of convenience, due to legacy issues surrounding
1254 * eh_host_adapter_reset).
1255 * - in_reset is asserted, so no new i/o is getting to the
1256 * card.
1257 * - The card is dead, or will be very shortly ;-/ so no new
1258 * commands are completing in the interrupt service.
1260 host = aac->scsi_host_ptr;
1261 scsi_block_requests(host);
1262 aac_adapter_disable_int(aac);
1263 if (aac->thread->pid != current->pid) {
1264 spin_unlock_irq(host->host_lock);
1265 kthread_stop(aac->thread);
1266 jafo = 1;
1270 * If a positive health, means in a known DEAD PANIC
1271 * state and the adapter could be reset to `try again'.
1273 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1275 if (retval)
1276 goto out;
1279 * Loop through the fibs, close the synchronous FIBS
1281 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1282 struct fib *fib = &aac->fibs[index];
1283 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1284 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1285 unsigned long flagv;
1286 spin_lock_irqsave(&fib->event_lock, flagv);
1287 up(&fib->event_wait);
1288 spin_unlock_irqrestore(&fib->event_lock, flagv);
1289 schedule();
1290 retval = 0;
1293 /* Give some extra time for ioctls to complete. */
1294 if (retval == 0)
1295 ssleep(2);
1296 index = aac->cardtype;
1299 * Re-initialize the adapter, first free resources, then carefully
1300 * apply the initialization sequence to come back again. Only risk
1301 * is a change in Firmware dropping cache, it is assumed the caller
1302 * will ensure that i/o is queisced and the card is flushed in that
1303 * case.
1305 aac_fib_map_free(aac);
1306 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1307 aac->comm_addr = NULL;
1308 aac->comm_phys = 0;
1309 kfree(aac->queues);
1310 aac->queues = NULL;
1311 free_irq(aac->pdev->irq, aac);
1312 if (aac->msi)
1313 pci_disable_msi(aac->pdev);
1314 kfree(aac->fsa_dev);
1315 aac->fsa_dev = NULL;
1316 quirks = aac_get_driver_ident(index)->quirks;
1317 if (quirks & AAC_QUIRK_31BIT) {
1318 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1319 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1320 goto out;
1321 } else {
1322 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1323 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1324 goto out;
1326 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1327 goto out;
1328 if (quirks & AAC_QUIRK_31BIT)
1329 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1330 goto out;
1331 if (jafo) {
1332 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1333 if (IS_ERR(aac->thread)) {
1334 retval = PTR_ERR(aac->thread);
1335 goto out;
1338 (void)aac_get_adapter_info(aac);
1339 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1340 host->sg_tablesize = 34;
1341 host->max_sectors = (host->sg_tablesize * 8) + 112;
1343 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1344 host->sg_tablesize = 17;
1345 host->max_sectors = (host->sg_tablesize * 8) + 112;
1347 aac_get_config_status(aac, 1);
1348 aac_get_containers(aac);
1350 * This is where the assumption that the Adapter is quiesced
1351 * is important.
1353 command_list = NULL;
1354 __shost_for_each_device(dev, host) {
1355 unsigned long flags;
1356 spin_lock_irqsave(&dev->list_lock, flags);
1357 list_for_each_entry(command, &dev->cmd_list, list)
1358 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1359 command->SCp.buffer = (struct scatterlist *)command_list;
1360 command_list = command;
1362 spin_unlock_irqrestore(&dev->list_lock, flags);
1364 while ((command = command_list)) {
1365 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1366 command->SCp.buffer = NULL;
1367 command->result = DID_OK << 16
1368 | COMMAND_COMPLETE << 8
1369 | SAM_STAT_TASK_SET_FULL;
1370 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1371 command->scsi_done(command);
1373 retval = 0;
1375 out:
1376 aac->in_reset = 0;
1377 scsi_unblock_requests(host);
1378 if (jafo) {
1379 spin_lock_irq(host->host_lock);
1381 return retval;
1384 int aac_reset_adapter(struct aac_dev * aac, int forced)
1386 unsigned long flagv = 0;
1387 int retval;
1388 struct Scsi_Host * host;
1390 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1391 return -EBUSY;
1393 if (aac->in_reset) {
1394 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1395 return -EBUSY;
1397 aac->in_reset = 1;
1398 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1401 * Wait for all commands to complete to this specific
1402 * target (block maximum 60 seconds). Although not necessary,
1403 * it does make us a good storage citizen.
1405 host = aac->scsi_host_ptr;
1406 scsi_block_requests(host);
1407 if (forced < 2) for (retval = 60; retval; --retval) {
1408 struct scsi_device * dev;
1409 struct scsi_cmnd * command;
1410 int active = 0;
1412 __shost_for_each_device(dev, host) {
1413 spin_lock_irqsave(&dev->list_lock, flagv);
1414 list_for_each_entry(command, &dev->cmd_list, list) {
1415 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1416 active++;
1417 break;
1420 spin_unlock_irqrestore(&dev->list_lock, flagv);
1421 if (active)
1422 break;
1426 * We can exit If all the commands are complete
1428 if (active == 0)
1429 break;
1430 ssleep(1);
1433 /* Quiesce build, flush cache, write through mode */
1434 if (forced < 2)
1435 aac_send_shutdown(aac);
1436 spin_lock_irqsave(host->host_lock, flagv);
1437 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1438 spin_unlock_irqrestore(host->host_lock, flagv);
1440 if ((forced < 2) && (retval == -ENODEV)) {
1441 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1442 struct fib * fibctx = aac_fib_alloc(aac);
1443 if (fibctx) {
1444 struct aac_pause *cmd;
1445 int status;
1447 aac_fib_init(fibctx);
1449 cmd = (struct aac_pause *) fib_data(fibctx);
1451 cmd->command = cpu_to_le32(VM_ContainerConfig);
1452 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1453 cmd->timeout = cpu_to_le32(1);
1454 cmd->min = cpu_to_le32(1);
1455 cmd->noRescan = cpu_to_le32(1);
1456 cmd->count = cpu_to_le32(0);
1458 status = aac_fib_send(ContainerCommand,
1459 fibctx,
1460 sizeof(struct aac_pause),
1461 FsaNormal,
1462 -2 /* Timeout silently */, 1,
1463 NULL, NULL);
1465 if (status >= 0)
1466 aac_fib_complete(fibctx);
1467 /* FIB should be freed only after getting
1468 * the response from the F/W */
1469 if (status != -ERESTARTSYS)
1470 aac_fib_free(fibctx);
1474 return retval;
1477 int aac_check_health(struct aac_dev * aac)
1479 int BlinkLED;
1480 unsigned long time_now, flagv = 0;
1481 struct list_head * entry;
1482 struct Scsi_Host * host;
1484 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1485 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1486 return 0;
1488 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1489 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1490 return 0; /* OK */
1493 aac->in_reset = 1;
1495 /* Fake up an AIF:
1496 * aac_aifcmd.command = AifCmdEventNotify = 1
1497 * aac_aifcmd.seqnum = 0xFFFFFFFF
1498 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1499 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1500 * aac.aifcmd.data[2] = AifHighPriority = 3
1501 * aac.aifcmd.data[3] = BlinkLED
1504 time_now = jiffies/HZ;
1505 entry = aac->fib_list.next;
1508 * For each Context that is on the
1509 * fibctxList, make a copy of the
1510 * fib, and then set the event to wake up the
1511 * thread that is waiting for it.
1513 while (entry != &aac->fib_list) {
1515 * Extract the fibctx
1517 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1518 struct hw_fib * hw_fib;
1519 struct fib * fib;
1521 * Check if the queue is getting
1522 * backlogged
1524 if (fibctx->count > 20) {
1526 * It's *not* jiffies folks,
1527 * but jiffies / HZ, so do not
1528 * panic ...
1530 u32 time_last = fibctx->jiffies;
1532 * Has it been > 2 minutes
1533 * since the last read off
1534 * the queue?
1536 if ((time_now - time_last) > aif_timeout) {
1537 entry = entry->next;
1538 aac_close_fib_context(aac, fibctx);
1539 continue;
1543 * Warning: no sleep allowed while
1544 * holding spinlock
1546 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1547 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1548 if (fib && hw_fib) {
1549 struct aac_aifcmd * aif;
1551 fib->hw_fib_va = hw_fib;
1552 fib->dev = aac;
1553 aac_fib_init(fib);
1554 fib->type = FSAFS_NTC_FIB_CONTEXT;
1555 fib->size = sizeof (struct fib);
1556 fib->data = hw_fib->data;
1557 aif = (struct aac_aifcmd *)hw_fib->data;
1558 aif->command = cpu_to_le32(AifCmdEventNotify);
1559 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1560 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1561 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1562 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1563 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1566 * Put the FIB onto the
1567 * fibctx's fibs
1569 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1570 fibctx->count++;
1572 * Set the event to wake up the
1573 * thread that will waiting.
1575 up(&fibctx->wait_sem);
1576 } else {
1577 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1578 kfree(fib);
1579 kfree(hw_fib);
1581 entry = entry->next;
1584 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1586 if (BlinkLED < 0) {
1587 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1588 goto out;
1591 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1593 if (!aac_check_reset || ((aac_check_reset == 1) &&
1594 (aac->supplement_adapter_info.SupportedOptions2 &
1595 AAC_OPTION_IGNORE_RESET)))
1596 goto out;
1597 host = aac->scsi_host_ptr;
1598 if (aac->thread->pid != current->pid)
1599 spin_lock_irqsave(host->host_lock, flagv);
1600 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1601 if (aac->thread->pid != current->pid)
1602 spin_unlock_irqrestore(host->host_lock, flagv);
1603 return BlinkLED;
1605 out:
1606 aac->in_reset = 0;
1607 return BlinkLED;
1612 * aac_command_thread - command processing thread
1613 * @dev: Adapter to monitor
1615 * Waits on the commandready event in it's queue. When the event gets set
1616 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1617 * until the queue is empty. When the queue is empty it will wait for
1618 * more FIBs.
1621 int aac_command_thread(void *data)
1623 struct aac_dev *dev = data;
1624 struct hw_fib *hw_fib, *hw_newfib;
1625 struct fib *fib, *newfib;
1626 struct aac_fib_context *fibctx;
1627 unsigned long flags;
1628 DECLARE_WAITQUEUE(wait, current);
1629 unsigned long next_jiffies = jiffies + HZ;
1630 unsigned long next_check_jiffies = next_jiffies;
1631 long difference = HZ;
1634 * We can only have one thread per adapter for AIF's.
1636 if (dev->aif_thread)
1637 return -EINVAL;
1640 * Let the DPC know it has a place to send the AIF's to.
1642 dev->aif_thread = 1;
1643 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1644 set_current_state(TASK_INTERRUPTIBLE);
1645 dprintk ((KERN_INFO "aac_command_thread start\n"));
1646 while (1) {
1647 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1648 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1649 struct list_head *entry;
1650 struct aac_aifcmd * aifcmd;
1652 set_current_state(TASK_RUNNING);
1654 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1655 list_del(entry);
1657 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1658 fib = list_entry(entry, struct fib, fiblink);
1660 * We will process the FIB here or pass it to a
1661 * worker thread that is TBD. We Really can't
1662 * do anything at this point since we don't have
1663 * anything defined for this thread to do.
1665 hw_fib = fib->hw_fib_va;
1666 memset(fib, 0, sizeof(struct fib));
1667 fib->type = FSAFS_NTC_FIB_CONTEXT;
1668 fib->size = sizeof(struct fib);
1669 fib->hw_fib_va = hw_fib;
1670 fib->data = hw_fib->data;
1671 fib->dev = dev;
1673 * We only handle AifRequest fibs from the adapter.
1675 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1676 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1677 /* Handle Driver Notify Events */
1678 aac_handle_aif(dev, fib);
1679 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1680 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1681 } else {
1682 /* The u32 here is important and intended. We are using
1683 32bit wrapping time to fit the adapter field */
1685 u32 time_now, time_last;
1686 unsigned long flagv;
1687 unsigned num;
1688 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1689 struct fib ** fib_pool, ** fib_p;
1691 /* Sniff events */
1692 if ((aifcmd->command ==
1693 cpu_to_le32(AifCmdEventNotify)) ||
1694 (aifcmd->command ==
1695 cpu_to_le32(AifCmdJobProgress))) {
1696 aac_handle_aif(dev, fib);
1699 time_now = jiffies/HZ;
1702 * Warning: no sleep allowed while
1703 * holding spinlock. We take the estimate
1704 * and pre-allocate a set of fibs outside the
1705 * lock.
1707 num = le32_to_cpu(dev->init->AdapterFibsSize)
1708 / sizeof(struct hw_fib); /* some extra */
1709 spin_lock_irqsave(&dev->fib_lock, flagv);
1710 entry = dev->fib_list.next;
1711 while (entry != &dev->fib_list) {
1712 entry = entry->next;
1713 ++num;
1715 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1716 hw_fib_pool = NULL;
1717 fib_pool = NULL;
1718 if (num
1719 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1720 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1721 hw_fib_p = hw_fib_pool;
1722 fib_p = fib_pool;
1723 while (hw_fib_p < &hw_fib_pool[num]) {
1724 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1725 --hw_fib_p;
1726 break;
1728 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1729 kfree(*(--hw_fib_p));
1730 break;
1733 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1734 kfree(fib_pool);
1735 fib_pool = NULL;
1736 kfree(hw_fib_pool);
1737 hw_fib_pool = NULL;
1739 } else {
1740 kfree(hw_fib_pool);
1741 hw_fib_pool = NULL;
1743 spin_lock_irqsave(&dev->fib_lock, flagv);
1744 entry = dev->fib_list.next;
1746 * For each Context that is on the
1747 * fibctxList, make a copy of the
1748 * fib, and then set the event to wake up the
1749 * thread that is waiting for it.
1751 hw_fib_p = hw_fib_pool;
1752 fib_p = fib_pool;
1753 while (entry != &dev->fib_list) {
1755 * Extract the fibctx
1757 fibctx = list_entry(entry, struct aac_fib_context, next);
1759 * Check if the queue is getting
1760 * backlogged
1762 if (fibctx->count > 20)
1765 * It's *not* jiffies folks,
1766 * but jiffies / HZ so do not
1767 * panic ...
1769 time_last = fibctx->jiffies;
1771 * Has it been > 2 minutes
1772 * since the last read off
1773 * the queue?
1775 if ((time_now - time_last) > aif_timeout) {
1776 entry = entry->next;
1777 aac_close_fib_context(dev, fibctx);
1778 continue;
1782 * Warning: no sleep allowed while
1783 * holding spinlock
1785 if (hw_fib_p < &hw_fib_pool[num]) {
1786 hw_newfib = *hw_fib_p;
1787 *(hw_fib_p++) = NULL;
1788 newfib = *fib_p;
1789 *(fib_p++) = NULL;
1791 * Make the copy of the FIB
1793 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1794 memcpy(newfib, fib, sizeof(struct fib));
1795 newfib->hw_fib_va = hw_newfib;
1797 * Put the FIB onto the
1798 * fibctx's fibs
1800 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1801 fibctx->count++;
1803 * Set the event to wake up the
1804 * thread that is waiting.
1806 up(&fibctx->wait_sem);
1807 } else {
1808 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1810 entry = entry->next;
1813 * Set the status of this FIB
1815 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1816 aac_fib_adapter_complete(fib, sizeof(u32));
1817 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1818 /* Free up the remaining resources */
1819 hw_fib_p = hw_fib_pool;
1820 fib_p = fib_pool;
1821 while (hw_fib_p < &hw_fib_pool[num]) {
1822 kfree(*hw_fib_p);
1823 kfree(*fib_p);
1824 ++fib_p;
1825 ++hw_fib_p;
1827 kfree(hw_fib_pool);
1828 kfree(fib_pool);
1830 kfree(fib);
1831 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1834 * There are no more AIF's
1836 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1839 * Background activity
1841 if ((time_before(next_check_jiffies,next_jiffies))
1842 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1843 next_check_jiffies = next_jiffies;
1844 if (aac_check_health(dev) == 0) {
1845 difference = ((long)(unsigned)check_interval)
1846 * HZ;
1847 next_check_jiffies = jiffies + difference;
1848 } else if (!dev->queues)
1849 break;
1851 if (!time_before(next_check_jiffies,next_jiffies)
1852 && ((difference = next_jiffies - jiffies) <= 0)) {
1853 struct timeval now;
1854 int ret;
1856 /* Don't even try to talk to adapter if its sick */
1857 ret = aac_check_health(dev);
1858 if (!ret && !dev->queues)
1859 break;
1860 next_check_jiffies = jiffies
1861 + ((long)(unsigned)check_interval)
1862 * HZ;
1863 do_gettimeofday(&now);
1865 /* Synchronize our watches */
1866 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1867 && (now.tv_usec > (1000000 / HZ)))
1868 difference = (((1000000 - now.tv_usec) * HZ)
1869 + 500000) / 1000000;
1870 else if (ret == 0) {
1871 struct fib *fibptr;
1873 if ((fibptr = aac_fib_alloc(dev))) {
1874 int status;
1875 __le32 *info;
1877 aac_fib_init(fibptr);
1879 info = (__le32 *) fib_data(fibptr);
1880 if (now.tv_usec > 500000)
1881 ++now.tv_sec;
1883 *info = cpu_to_le32(now.tv_sec);
1885 status = aac_fib_send(SendHostTime,
1886 fibptr,
1887 sizeof(*info),
1888 FsaNormal,
1889 1, 1,
1890 NULL,
1891 NULL);
1892 /* Do not set XferState to zero unless
1893 * receives a response from F/W */
1894 if (status >= 0)
1895 aac_fib_complete(fibptr);
1896 /* FIB should be freed only after
1897 * getting the response from the F/W */
1898 if (status != -ERESTARTSYS)
1899 aac_fib_free(fibptr);
1901 difference = (long)(unsigned)update_interval*HZ;
1902 } else {
1903 /* retry shortly */
1904 difference = 10 * HZ;
1906 next_jiffies = jiffies + difference;
1907 if (time_before(next_check_jiffies,next_jiffies))
1908 difference = next_check_jiffies - jiffies;
1910 if (difference <= 0)
1911 difference = 1;
1912 set_current_state(TASK_INTERRUPTIBLE);
1913 schedule_timeout(difference);
1915 if (kthread_should_stop())
1916 break;
1918 if (dev->queues)
1919 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1920 dev->aif_thread = 0;
1921 return 0;