blob: e5f2d7d9002ec4df139d9db4260069d58d2fcfce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
Alan Coxfa195af2008-10-27 15:16:36 +00003 * (c) Copyright 2001 Red Hat Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07008 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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.
15 *
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.
20 *
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.
24 *
25 * Module Name:
26 * commsup.c
27 *
28 * Abstract: Contain all routines that are required for FSA host/adapter
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070029 * communication.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 */
32
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>
Al Viro164006d2005-11-30 23:47:05 -050042#include <linux/delay.h>
Christoph Hellwigfe273812006-02-14 18:45:06 +010043#include <linux/kthread.h>
Al Viro6a3670c2006-09-24 23:45:29 +010044#include <linux/interrupt.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040045#include <linux/semaphore.h>
Mark Haverkamp8c867b22006-08-03 08:03:30 -070046#include <scsi/scsi.h>
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070047#include <scsi/scsi_host.h>
Mark Haverkamp131256c2005-09-26 13:04:56 -070048#include <scsi/scsi_device.h>
Mark Haverkamp8c867b22006-08-03 08:03:30 -070049#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "aacraid.h"
52
53/**
54 * fib_map_alloc - allocate the fib objects
55 * @dev: Adapter to allocate for
56 *
57 * Allocate and map the shared PCI space for the FIB blocks used to
58 * talk to the Adaptec firmware.
59 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int fib_map_alloc(struct aac_dev *dev)
62{
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070063 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));
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -070067 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -ENOMEM;
73 return 0;
74}
75
76/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080077 * aac_fib_map_free - free the fib objects
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * @dev: Adapter to free
79 *
80 * Free the PCI mappings and the memory allocated for FIB blocks
81 * on this adapter.
82 */
83
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080084void aac_fib_map_free(struct aac_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Salyzyn, Mark9ad52042007-07-17 11:15:08 -040086 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080094 * aac_fib_setup - setup the fibs
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * @dev: Adapter to set up
96 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040097 * Allocate the PCI space for the fibs, map it and then initialise the
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * fib area, the unmapped fib data and also the free list
99 */
100
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800101int aac_fib_setup(struct aac_dev * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct fib *fibptr;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700104 struct hw_fib *hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dma_addr_t hw_fib_pa;
106 int i;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700107
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;
112 }
113 if (i<0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -ENOMEM;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800115
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700116 /* 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));
124
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);
129
Mark Haverkampa8166a52007-03-15 10:26:22 -0700130 hw_fib = dev->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 hw_fib_pa = dev->hw_fib_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /*
133 * Initialise the fibs
134 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800135 for (i = 0, fibptr = &dev->fibs[i];
136 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
137 i++, fibptr++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 {
139 fibptr->dev = dev;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700140 fibptr->hw_fib_va = hw_fib;
141 fibptr->data = (void *) fibptr->hw_fib_va->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 fibptr->next = fibptr+1; /* Forward chain the fibs */
Thomas Gleixner6de76cf2010-09-07 14:32:47 +0000143 sema_init(&fibptr->event_wait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 spin_lock_init(&fibptr->event_lock);
Mark Haverkampa8166a52007-03-15 10:26:22 -0700145 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
146 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 fibptr->hw_fib_pa = hw_fib_pa;
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700148 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 /*
154 * Add the fib chain to the free list
155 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700156 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /*
158 * Enable this to debug out of queue space
159 */
160 dev->free_fib = &dev->fibs[0];
161 return 0;
162}
163
164/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800165 * aac_fib_alloc - allocate a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @dev: Adapter to allocate the fib for
167 *
168 * Allocate a fib from the adapter fib pool. If the pool is empty we
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700169 * return NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800171
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800172struct fib *aac_fib_alloc(struct aac_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct fib * fibptr;
175 unsigned long flags;
176 spin_lock_irqsave(&dev->fib_lock, flags);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800177 fibptr = dev->free_fib;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700178 if(!fibptr){
179 spin_unlock_irqrestore(&dev->fib_lock, flags);
180 return fibptr;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 dev->free_fib = fibptr->next;
183 spin_unlock_irqrestore(&dev->fib_lock, flags);
184 /*
185 * Set the proper node type code and node byte size
186 */
187 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
188 fibptr->size = sizeof(struct fib);
189 /*
190 * Null out fields that depend on being zero at the start of
191 * each I/O
192 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700193 fibptr->hw_fib_va->header.XferState = 0;
Salyzyn, Markb6ef70f2008-01-08 13:26:43 -0800194 fibptr->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 fibptr->callback = NULL;
196 fibptr->callback_data = NULL;
197
198 return fibptr;
199}
200
201/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800202 * aac_fib_free - free a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * @fibptr: fib to free up
204 *
205 * Frees up a fib and places it on the appropriate queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800207
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800208void aac_fib_free(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530210 unsigned long flags, flagsv;
211
212 spin_lock_irqsave(&fibptr->event_lock, flagsv);
213 if (fibptr->done == 2) {
214 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
215 return;
216 }
217 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700220 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 aac_config.fib_timeouts++;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700222 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));
226 }
227 fibptr->next = fibptr->dev->free_fib;
228 fibptr->dev->free_fib = fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
230}
231
232/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800233 * aac_fib_init - initialise a fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * @fibptr: The fib to initialize
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800235 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Set up the generic fib fields ready for use
237 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800238
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800239void aac_fib_init(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700241 struct hw_fib *hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 hw_fib->header.StructType = FIB_MAGIC;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700244 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
245 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700246 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700248 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
252 * fib_deallocate - deallocate a fib
253 * @fibptr: fib to deallocate
254 *
255 * Will deallocate and return to the free pool the FIB pointed to by the
256 * caller.
257 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800258
Adrian Bunk 48338692005-04-25 19:45:58 -0700259static void fib_dealloc(struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700261 struct hw_fib *hw_fib = fibptr->hw_fib_va;
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700262 BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800263 hw_fib->header.XferState = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*
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.
271 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
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
280 *
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.
284 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
287{
288 struct aac_queue * q;
Mark Haverkampbed30de2005-08-03 15:38:51 -0700289 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /*
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.
296 */
297
298 q = &dev->queues->queue[qid];
Mark Haverkampbed30de2005-08-03 15:38:51 -0700299
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) {
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700304 if (qid == AdapNormCmdQueue)
Mark Haverkampbed30de2005-08-03 15:38:51 -0700305 idx = ADAP_NORM_CMD_ENTRIES;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700306 else
Mark Haverkampbed30de2005-08-03 15:38:51 -0700307 idx = ADAP_NORM_RESP_ENTRIES;
308 }
309 if (idx != le32_to_cpu(*(q->headers.consumer)))
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800310 *nonotify = 1;
Mark Haverkampbed30de2005-08-03 15:38:51 -0700311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700313 if (qid == AdapNormCmdQueue) {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800314 if (*index >= ADAP_NORM_CMD_ENTRIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *index = 0; /* Wrap to front of the Producer Queue. */
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700316 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800317 if (*index >= ADAP_NORM_RESP_ENTRIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *index = 0; /* Wrap to front of the Producer Queue. */
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800321 /* Queue is full */
322 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700323 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 qid, q->numpending);
325 return 0;
326 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800327 *entry = q->base + *index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 1;
329 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/**
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
341 *
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.
346 */
347
Mark Haverkamp28713322007-01-23 14:59:20 -0800348int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct aac_entry * entry = NULL;
351 int map = 0;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800352
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700353 if (qid == AdapNormCmdQueue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* if no entries wait for some if caller wants to */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800355 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk(KERN_ERR "GetEntries failed\n");
357 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800358 /*
359 * Setup queue entry with a command, status and fib mapped
360 */
361 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
362 map = 1;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700363 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800364 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* if no entries wait for some if caller wants to */
366 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800367 /*
368 * Setup queue entry with command, status and fib mapped
369 */
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800374 map = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 /*
377 * If MapFib is true than we need to map the Fib and put pointers
378 * in the queue entry.
379 */
380 if (map)
381 entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
382 return 0;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800386 * Define the highest level of host to adapter communication routines.
387 * These routines will support host to adapter FS commuication. These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * 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.
391 */
392
393/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800394 * aac_fib_send - send a fib to the adapter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * @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
403 *
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.
408 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800409
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800410int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct aac_dev * dev = fibptr->dev;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700415 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long flags = 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700417 unsigned long qflags;
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530418 unsigned long mflags = 0;
419
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
422 return -EBUSY;
423 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300424 * There are 5 cases with the wait and response requested flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * The only invalid cases are if the caller requests to wait and
426 * does not request a response and if the caller does not want a
427 * response and the Fib is not allocated from pool. If a response
428 * is not requesed the Fib will just be deallocaed by the DPC
429 * routine when the response comes back from the adapter. No
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800430 * further processing will be done besides deleting the Fib. We
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * will have a debug mode where the adapter can notify the host
432 * it had a problem and the host can log that fact.
433 */
Salyzyn, Markb6ef70f2008-01-08 13:26:43 -0800434 fibptr->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (wait && !reply) {
436 return -EINVAL;
437 } else if (!wait && reply) {
438 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
439 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
440 } else if (!wait && !reply) {
441 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
442 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
443 } else if (wait && reply) {
444 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
445 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Map the fib into 32bits by using the fib number
449 */
450
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700451 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
453 /*
454 * Set FIB state to indicate where it came from and if we want a
455 * response from the adapter. Also load the command from the
456 * caller.
457 *
458 * Map the hw fib pointer as a 32bit value
459 */
460 hw_fib->header.Command = cpu_to_le16(command);
461 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
Mark Haverkampa8166a52007-03-15 10:26:22 -0700462 fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Set the size of the Fib we want to send to the adapter
465 */
466 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
467 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
468 return -EMSGSIZE;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Get a queue entry connect the FIB to it and send an notify
472 * the adapter a command is ready.
473 */
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700474 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * Fill in the Callback and CallbackContext if we are not
478 * going to wait.
479 */
480 if (!wait) {
481 fibptr->callback = callback;
482 fibptr->callback_data = callback_data;
Salyzyn, Markb6ef70f2008-01-08 13:26:43 -0800483 fibptr->flags = FIB_CONTEXT_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 fibptr->done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700488 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
489
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700490 dprintk((KERN_DEBUG "Fib contents:.\n"));
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700491 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
492 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
493 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
Mark Haverkampa8166a52007-03-15 10:26:22 -0700494 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700495 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
496 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
497
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700498 if (!dev->queues)
Mark Haverkamp651013552006-09-19 08:59:23 -0700499 return -EBUSY;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700500
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530501 if (wait) {
502
503 spin_lock_irqsave(&dev->manage_lock, mflags);
504 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
505 printk(KERN_INFO "No management Fibs Available:%d\n",
506 dev->management_fib_count);
507 spin_unlock_irqrestore(&dev->manage_lock, mflags);
508 return -EBUSY;
509 }
510 dev->management_fib_count++;
511 spin_unlock_irqrestore(&dev->manage_lock, mflags);
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700512 spin_lock_irqsave(&fibptr->event_lock, flags);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530513 }
514
515 if (aac_adapter_deliver(fibptr) != 0) {
516 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
517 if (wait) {
518 spin_unlock_irqrestore(&fibptr->event_lock, flags);
519 spin_lock_irqsave(&dev->manage_lock, mflags);
520 dev->management_fib_count--;
521 spin_unlock_irqrestore(&dev->manage_lock, mflags);
522 }
523 return -EBUSY;
524 }
525
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800528 * If the caller wanted us to wait for response wait now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (wait) {
532 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Mark Haverkamp92033442005-09-20 12:56:50 -0700533 /* Only set for first known interruptable command */
534 if (wait < 0) {
535 /*
536 * *VERY* Dangerous to time out a command, the
537 * assumption is made that we have no hope of
538 * functioning because an interrupt routing or other
539 * hardware failure has occurred.
540 */
541 unsigned long count = 36000000L; /* 3 minutes */
Mark Haverkamp92033442005-09-20 12:56:50 -0700542 while (down_trylock(&fibptr->event_wait)) {
Mark Haverkamp33524b72006-11-21 10:40:08 -0800543 int blink;
Mark Haverkamp92033442005-09-20 12:56:50 -0700544 if (--count == 0) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800545 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
Mark Haverkamp92033442005-09-20 12:56:50 -0700546 spin_lock_irqsave(q->lock, qflags);
547 q->numpending--;
Mark Haverkamp92033442005-09-20 12:56:50 -0700548 spin_unlock_irqrestore(q->lock, qflags);
549 if (wait == -1) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800550 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
Mark Haverkamp92033442005-09-20 12:56:50 -0700551 "Usually a result of a PCI interrupt routing problem;\n"
552 "update mother board BIOS or consider utilizing one of\n"
553 "the SAFE mode kernel options (acpi, apic etc)\n");
554 }
555 return -ETIMEDOUT;
556 }
Mark Haverkamp33524b72006-11-21 10:40:08 -0800557 if ((blink = aac_adapter_check_health(dev)) > 0) {
558 if (wait == -1) {
559 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
560 "Usually a result of a serious unrecoverable hardware problem\n",
561 blink);
562 }
563 return -EFAULT;
564 }
Mark Haverkamp92033442005-09-20 12:56:50 -0700565 udelay(5);
566 }
Mark Salyzyn04625902008-04-23 08:16:06 -0400567 } else if (down_interruptible(&fibptr->event_wait)) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530568 /* Do nothing ... satisfy
569 * down_interruptible must_check */
Mark Salyzyne6990c62008-04-14 14:20:16 -0400570 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530571
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700572 spin_lock_irqsave(&fibptr->event_lock, flags);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530573 if (fibptr->done == 0) {
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700574 fibptr->done = 2; /* Tell interrupt we aborted */
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700575 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530576 return -ERESTARTSYS;
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700577 }
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700578 spin_unlock_irqrestore(&fibptr->event_lock, flags);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700579 BUG_ON(fibptr->done == 0);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800580
Salyzyn, Mark912d4e82007-03-26 09:21:14 -0400581 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -ETIMEDOUT;
Salyzyn, Mark912d4e82007-03-26 09:21:14 -0400583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 /*
586 * If the user does not want a response than return success otherwise
587 * return pending
588 */
589 if (reply)
590 return -EINPROGRESS;
591 else
592 return 0;
593}
594
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800595/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * aac_consumer_get - get the top of the queue
597 * @dev: Adapter
598 * @q: Queue
599 * @entry: Return entry
600 *
601 * Will return a pointer to the entry on the top of the queue requested that
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800602 * we are a consumer of, and return the address of the queue entry. It does
603 * not change the state of the queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605
606int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
607{
608 u32 index;
609 int status;
610 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
611 status = 0;
612 } else {
613 /*
614 * The consumer index must be wrapped if we have reached
615 * the end of the queue, else we just use the entry
616 * pointed to by the header index
617 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800618 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
619 index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 else
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800621 index = le32_to_cpu(*q->headers.consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *entry = q->base + index;
623 status = 1;
624 }
625 return(status);
626}
627
628/**
629 * aac_consumer_free - free consumer entry
630 * @dev: Adapter
631 * @q: Queue
632 * @qid: Queue ident
633 *
634 * Frees up the current top of the queue we are a consumer of. If the
635 * queue was full notify the producer that the queue is no longer full.
636 */
637
638void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
639{
640 int wasfull = 0;
641 u32 notify;
642
643 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
644 wasfull = 1;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
647 *q->headers.consumer = cpu_to_le32(1);
648 else
Marcin Slusarz36b8dd12008-03-28 14:48:35 -0700649 le32_add_cpu(q->headers.consumer, 1);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (wasfull) {
652 switch (qid) {
653
654 case HostNormCmdQueue:
655 notify = HostNormCmdNotFull;
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 case HostNormRespQueue:
658 notify = HostNormRespNotFull;
659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 default:
661 BUG();
662 return;
663 }
664 aac_adapter_notify(dev, notify);
665 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800666}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800669 * aac_fib_adapter_complete - complete adapter issued fib
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * @fibptr: fib to complete
671 * @size: size of fib
672 *
673 * Will do all necessary work to complete a FIB that was sent from
674 * the adapter.
675 */
676
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800677int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700679 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct aac_dev * dev = fibptr->dev;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700681 struct aac_queue * q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned long nointr = 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700683 unsigned long qflags;
684
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700685 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
686 kfree(hw_fib);
687 return 0;
688 }
689
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700690 if (hw_fib->header.XferState == 0) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800691 if (dev->comm_interface == AAC_COMM_MESSAGE)
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700692 kfree(hw_fib);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800693 return 0;
Mark Haverkamp1640a2c2005-09-20 12:57:11 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * If we plan to do anything check the structure type first.
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800697 */
698 if (hw_fib->header.StructType != FIB_MAGIC) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800699 if (dev->comm_interface == AAC_COMM_MESSAGE)
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -0700700 kfree(hw_fib);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800701 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 /*
704 * This block handles the case where the adapter had sent us a
705 * command and we have finished processing the command. We
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800706 * call completeFib when we are done processing the command
707 * and want to send a response back to the adapter. This will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * send the completed cdb to the adapter.
709 */
710 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
Mark Haverkamp28713322007-01-23 14:59:20 -0800711 if (dev->comm_interface == AAC_COMM_MESSAGE) {
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700712 kfree (hw_fib);
713 } else {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800714 u32 index;
715 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700716 if (size) {
717 size += sizeof(struct aac_fibhdr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800718 if (size > le16_to_cpu(hw_fib->header.SenderSize))
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700719 return -EMSGSIZE;
720 hw_fib->header.Size = cpu_to_le16(size);
721 }
722 q = &dev->queues->queue[AdapNormRespQueue];
723 spin_lock_irqsave(q->lock, qflags);
724 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
725 *(q->headers.producer) = cpu_to_le32(index + 1);
726 spin_unlock_irqrestore(q->lock, qflags);
727 if (!(nointr & (int)aac_config.irq_mod))
728 aac_adapter_notify(dev, AdapNormRespQueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800730 } else {
731 printk(KERN_WARNING "aac_fib_adapter_complete: "
732 "Unknown xferstate detected.\n");
733 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
738/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800739 * aac_fib_complete - fib completion handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * @fib: FIB to complete
741 *
742 * Will do all necessary work to complete a FIB.
743 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800744
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800745int aac_fib_complete(struct fib *fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530747 unsigned long flags;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700748 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /*
751 * Check for a fib which has already been completed
752 */
753
754 if (hw_fib->header.XferState == 0)
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * If we plan to do anything check the structure type first.
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800758 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (hw_fib->header.StructType != FIB_MAGIC)
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800763 * This block completes a cdb which orginated on the host and we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * just need to deallocate the cdb or reinit it. At this point the
765 * command is complete that we had sent to the adapter and this
766 * cdb could be reused.
767 */
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530768 spin_lock_irqsave(&fibptr->event_lock, flags);
769 if (fibptr->done == 2) {
770 spin_unlock_irqrestore(&fibptr->event_lock, flags);
771 return 0;
772 }
773 spin_unlock_irqrestore(&fibptr->event_lock, flags);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
776 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
777 {
778 fib_dealloc(fibptr);
779 }
780 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
781 {
782 /*
783 * This handles the case when the host has aborted the I/O
784 * to the adapter because the adapter is not responding
785 */
786 fib_dealloc(fibptr);
787 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
788 fib_dealloc(fibptr);
789 } else {
790 BUG();
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
795/**
796 * aac_printf - handle printf from firmware
797 * @dev: Adapter
798 * @val: Message info
799 *
800 * Print a message passed to us by the controller firmware on the
801 * Adaptec board
802 */
803
804void aac_printf(struct aac_dev *dev, u32 val)
805{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 char *cp = dev->printfbuf;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700807 if (dev->printf_enabled)
808 {
809 int length = val & 0xffff;
810 int level = (val >> 16) & 0xffff;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800811
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700812 /*
813 * The size of the printfbuf is set in port.c
814 * There is no variable or define for it
815 */
816 if (length > 255)
817 length = 255;
818 if (cp[length] != 0)
819 cp[length] = 0;
820 if (level == LOG_AAC_HIGH_ERROR)
Mark Haverkamp1241f352006-03-27 09:44:23 -0800821 printk(KERN_WARNING "%s:%s", dev->name, cp);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700822 else
Mark Haverkamp1241f352006-03-27 09:44:23 -0800823 printk(KERN_INFO "%s:%s", dev->name, cp);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700824 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800825 memset(cp, 0, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Mark Haverkamp131256c2005-09-26 13:04:56 -0700828
829/**
830 * aac_handle_aif - Handle a message from the firmware
831 * @dev: Which adapter this fib is from
832 * @fibptr: Pointer to fibptr from adapter
833 *
834 * This routine handles a driver notify fib from the adapter and
835 * dispatches it to the appropriate routine for handling.
836 */
837
Mark Haverkamp31876f32006-03-27 09:43:55 -0800838#define AIF_SNIFF_TIMEOUT (30*HZ)
Mark Haverkamp131256c2005-09-26 13:04:56 -0700839static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
840{
Mark Haverkampa8166a52007-03-15 10:26:22 -0700841 struct hw_fib * hw_fib = fibptr->hw_fib_va;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700842 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -0800843 u32 channel, id, lun, container;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700844 struct scsi_device *device;
845 enum {
846 NOTHING,
847 DELETE,
848 ADD,
849 CHANGE
Salyzyn, Mark0995ad32008-01-11 11:56:07 -0800850 } device_config_needed = NOTHING;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700851
852 /* Sniff for container changes */
853
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700854 if (!dev || !dev->fsa_dev)
Mark Haverkamp131256c2005-09-26 13:04:56 -0700855 return;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -0800856 container = channel = id = lun = (u32)-1;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700857
858 /*
859 * We have set this up to try and minimize the number of
860 * re-configures that take place. As a result of this when
861 * certain AIF's come in we will set a flag waiting for another
862 * type of AIF before setting the re-config flag.
863 */
864 switch (le32_to_cpu(aifcmd->command)) {
865 case AifCmdDriverNotify:
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000866 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
Mark Haverkamp131256c2005-09-26 13:04:56 -0700867 /*
868 * Morph or Expand complete
869 */
870 case AifDenMorphComplete:
871 case AifDenVolumeExtendComplete:
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000872 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -0700873 if (container >= dev->maximum_num_containers)
874 break;
875
876 /*
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100877 * Find the scsi_device associated with the SCSI
Mark Haverkamp131256c2005-09-26 13:04:56 -0700878 * address. Make sure we have the right array, and if
879 * so set the flag to initiate a new re-config once we
880 * see an AifEnConfigChange AIF come through.
881 */
882
883 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800884 device = scsi_device_lookup(dev->scsi_host_ptr,
885 CONTAINER_TO_CHANNEL(container),
886 CONTAINER_TO_ID(container),
Mark Haverkamp131256c2005-09-26 13:04:56 -0700887 CONTAINER_TO_LUN(container));
888 if (device) {
889 dev->fsa_dev[container].config_needed = CHANGE;
890 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800891 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700892 scsi_device_put(device);
893 }
894 }
895 }
896
897 /*
898 * If we are waiting on something and this happens to be
899 * that thing then set the re-configure flag.
900 */
901 if (container != (u32)-1) {
902 if (container >= dev->maximum_num_containers)
903 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800904 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000905 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -0800906 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700907 dev->fsa_dev[container].config_waiting_on = 0;
908 } else for (container = 0;
909 container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -0800910 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000911 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -0800912 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700913 dev->fsa_dev[container].config_waiting_on = 0;
914 }
915 break;
916
917 case AifCmdEventNotify:
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000918 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800919 case AifEnBatteryEvent:
920 dev->cache_protected =
921 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
922 break;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700923 /*
924 * Add an Array.
925 */
926 case AifEnAddContainer:
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000927 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -0700928 if (container >= dev->maximum_num_containers)
929 break;
930 dev->fsa_dev[container].config_needed = ADD;
931 dev->fsa_dev[container].config_waiting_on =
932 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800933 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700934 break;
935
936 /*
937 * Delete an Array.
938 */
939 case AifEnDeleteContainer:
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000940 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -0700941 if (container >= dev->maximum_num_containers)
942 break;
943 dev->fsa_dev[container].config_needed = DELETE;
944 dev->fsa_dev[container].config_waiting_on =
945 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800946 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700947 break;
948
949 /*
950 * Container change detected. If we currently are not
951 * waiting on something else, setup to wait on a Config Change.
952 */
953 case AifEnContainerChange:
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000954 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Haverkamp131256c2005-09-26 13:04:56 -0700955 if (container >= dev->maximum_num_containers)
956 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800957 if (dev->fsa_dev[container].config_waiting_on &&
958 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -0700959 break;
960 dev->fsa_dev[container].config_needed = CHANGE;
961 dev->fsa_dev[container].config_waiting_on =
962 AifEnConfigChange;
Mark Haverkamp31876f32006-03-27 09:43:55 -0800963 dev->fsa_dev[container].config_waiting_stamp = jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -0700964 break;
965
966 case AifEnConfigChange:
967 break;
968
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800969 case AifEnAddJBOD:
970 case AifEnDeleteJBOD:
971 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
Mark Salyzyna4576b52008-04-30 15:47:35 -0400972 if ((container >> 28)) {
973 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800974 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -0400975 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800976 channel = (container >> 24) & 0xF;
Mark Salyzyna4576b52008-04-30 15:47:35 -0400977 if (channel >= dev->maximum_num_channels) {
978 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800979 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -0400980 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800981 id = container & 0xFFFF;
Mark Salyzyna4576b52008-04-30 15:47:35 -0400982 if (id >= dev->maximum_num_physicals) {
983 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800984 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -0400985 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800986 lun = (container >> 16) & 0xFF;
Mark Salyzyna4576b52008-04-30 15:47:35 -0400987 container = (u32)-1;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -0800988 channel = aac_phys_to_logical(channel);
989 device_config_needed =
990 (((__le32 *)aifcmd->data)[0] ==
991 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
Rajashekhara, Mahesh5ca05592010-05-10 04:05:50 -0700992 if (device_config_needed == ADD) {
993 device = scsi_device_lookup(dev->scsi_host_ptr,
994 channel,
995 id,
996 lun);
997 if (device) {
998 scsi_remove_device(device);
999 scsi_device_put(device);
1000 }
1001 }
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001002 break;
1003
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001004 case AifEnEnclosureManagement:
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001005 /*
1006 * If in JBOD mode, automatic exposure of new
1007 * physical target to be suppressed until configured.
1008 */
1009 if (dev->jbod)
1010 break;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001011 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1012 case EM_DRIVE_INSERTION:
1013 case EM_DRIVE_REMOVAL:
1014 container = le32_to_cpu(
1015 ((__le32 *)aifcmd->data)[2]);
Mark Salyzyna4576b52008-04-30 15:47:35 -04001016 if ((container >> 28)) {
1017 container = (u32)-1;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001018 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001019 }
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001020 channel = (container >> 24) & 0xF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001021 if (channel >= dev->maximum_num_channels) {
1022 container = (u32)-1;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001023 break;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001024 }
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001025 id = container & 0xFFFF;
1026 lun = (container >> 16) & 0xFF;
Mark Salyzyna4576b52008-04-30 15:47:35 -04001027 container = (u32)-1;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001028 if (id >= dev->maximum_num_physicals) {
1029 /* legacy dev_t ? */
1030 if ((0x2000 <= id) || lun || channel ||
1031 ((channel = (id >> 7) & 0x3F) >=
1032 dev->maximum_num_channels))
1033 break;
1034 lun = (id >> 4) & 7;
1035 id &= 0xF;
1036 }
1037 channel = aac_phys_to_logical(channel);
1038 device_config_needed =
1039 (((__le32 *)aifcmd->data)[3]
1040 == cpu_to_le32(EM_DRIVE_INSERTION)) ?
1041 ADD : DELETE;
1042 break;
1043 }
1044 break;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001045 }
1046
1047 /*
1048 * If we are waiting on something and this happens to be
1049 * that thing then set the re-configure flag.
1050 */
1051 if (container != (u32)-1) {
1052 if (container >= dev->maximum_num_containers)
1053 break;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001054 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001055 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -08001056 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001057 dev->fsa_dev[container].config_waiting_on = 0;
1058 } else for (container = 0;
1059 container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -08001060 if ((dev->fsa_dev[container].config_waiting_on ==
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001061 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
Mark Haverkamp31876f32006-03-27 09:43:55 -08001062 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
Mark Haverkamp131256c2005-09-26 13:04:56 -07001063 dev->fsa_dev[container].config_waiting_on = 0;
1064 }
1065 break;
1066
1067 case AifCmdJobProgress:
1068 /*
1069 * These are job progress AIF's. When a Clear is being
1070 * done on a container it is initially created then hidden from
1071 * the OS. When the clear completes we don't get a config
1072 * change so we monitor the job status complete on a clear then
1073 * wait for a container change.
1074 */
1075
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001076 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1077 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1078 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
Mark Haverkamp131256c2005-09-26 13:04:56 -07001079 for (container = 0;
1080 container < dev->maximum_num_containers;
1081 ++container) {
1082 /*
1083 * Stomp on all config sequencing for all
1084 * containers?
1085 */
1086 dev->fsa_dev[container].config_waiting_on =
1087 AifEnContainerChange;
1088 dev->fsa_dev[container].config_needed = ADD;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001089 dev->fsa_dev[container].config_waiting_stamp =
1090 jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001091 }
1092 }
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001093 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1094 ((__le32 *)aifcmd->data)[6] == 0 &&
1095 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
Mark Haverkamp131256c2005-09-26 13:04:56 -07001096 for (container = 0;
1097 container < dev->maximum_num_containers;
1098 ++container) {
1099 /*
1100 * Stomp on all config sequencing for all
1101 * containers?
1102 */
1103 dev->fsa_dev[container].config_waiting_on =
1104 AifEnContainerChange;
1105 dev->fsa_dev[container].config_needed = DELETE;
Mark Haverkamp31876f32006-03-27 09:43:55 -08001106 dev->fsa_dev[container].config_waiting_stamp =
1107 jiffies;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001108 }
1109 }
1110 break;
1111 }
1112
Mark Salyzyna4576b52008-04-30 15:47:35 -04001113 container = 0;
1114retry_next:
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001115 if (device_config_needed == NOTHING)
Mark Salyzyna4576b52008-04-30 15:47:35 -04001116 for (; container < dev->maximum_num_containers; ++container) {
Mark Haverkamp31876f32006-03-27 09:43:55 -08001117 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1118 (dev->fsa_dev[container].config_needed != NOTHING) &&
1119 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
Mark Haverkamp131256c2005-09-26 13:04:56 -07001120 device_config_needed =
1121 dev->fsa_dev[container].config_needed;
1122 dev->fsa_dev[container].config_needed = NOTHING;
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001123 channel = CONTAINER_TO_CHANNEL(container);
1124 id = CONTAINER_TO_ID(container);
1125 lun = CONTAINER_TO_LUN(container);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001126 break;
1127 }
1128 }
1129 if (device_config_needed == NOTHING)
1130 return;
1131
1132 /*
1133 * If we decided that a re-configuration needs to be done,
1134 * schedule it here on the way out the door, please close the door
1135 * behind you.
1136 */
1137
Mark Haverkamp131256c2005-09-26 13:04:56 -07001138 /*
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001139 * Find the scsi_device associated with the SCSI address,
Mark Haverkamp131256c2005-09-26 13:04:56 -07001140 * and mark it as changed, invalidating the cache. This deals
1141 * with changes to existing device IDs.
1142 */
1143
1144 if (!dev || !dev->scsi_host_ptr)
1145 return;
1146 /*
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001147 * force reload of disk info via aac_probe_container
Mark Haverkamp131256c2005-09-26 13:04:56 -07001148 */
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001149 if ((channel == CONTAINER_CHANNEL) &&
1150 (device_config_needed != NOTHING)) {
1151 if (dev->fsa_dev[container].valid == 1)
1152 dev->fsa_dev[container].valid = 2;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001153 aac_probe_container(dev, container);
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001154 }
1155 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001156 if (device) {
1157 switch (device_config_needed) {
1158 case DELETE:
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001159#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1160 scsi_remove_device(device);
1161#else
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001162 if (scsi_device_online(device)) {
1163 scsi_device_set_state(device, SDEV_OFFLINE);
1164 sdev_printk(KERN_INFO, device,
1165 "Device offlined - %s\n",
1166 (channel == CONTAINER_CHANNEL) ?
1167 "array deleted" :
1168 "enclosure services event");
1169 }
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001170#endif
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001171 break;
1172 case ADD:
1173 if (!scsi_device_online(device)) {
1174 sdev_printk(KERN_INFO, device,
1175 "Device online - %s\n",
1176 (channel == CONTAINER_CHANNEL) ?
1177 "array created" :
1178 "enclosure services event");
1179 scsi_device_set_state(device, SDEV_RUNNING);
1180 }
1181 /* FALLTHRU */
Mark Haverkamp131256c2005-09-26 13:04:56 -07001182 case CHANGE:
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001183 if ((channel == CONTAINER_CHANNEL)
1184 && (!dev->fsa_dev[container].valid)) {
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001185#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1186 scsi_remove_device(device);
1187#else
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001188 if (!scsi_device_online(device))
1189 break;
1190 scsi_device_set_state(device, SDEV_OFFLINE);
1191 sdev_printk(KERN_INFO, device,
1192 "Device offlined - %s\n",
1193 "array failed");
Rajashekhara, Mahesh9cccde92010-05-10 04:29:25 -07001194#endif
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001195 break;
1196 }
Mark Haverkamp131256c2005-09-26 13:04:56 -07001197 scsi_rescan_device(&device->sdev_gendev);
1198
1199 default:
1200 break;
1201 }
1202 scsi_device_put(device);
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001203 device_config_needed = NOTHING;
Mark Haverkamp131256c2005-09-26 13:04:56 -07001204 }
Salyzyn, Mark0995ad32008-01-11 11:56:07 -08001205 if (device_config_needed == ADD)
1206 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
Mark Salyzyna4576b52008-04-30 15:47:35 -04001207 if (channel == CONTAINER_CHANNEL) {
1208 container++;
1209 device_config_needed = NOTHING;
1210 goto retry_next;
1211 }
Mark Haverkamp131256c2005-09-26 13:04:56 -07001212}
1213
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001214static int _aac_reset_adapter(struct aac_dev *aac, int forced)
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001215{
1216 int index, quirks;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001217 int retval;
1218 struct Scsi_Host *host;
1219 struct scsi_device *dev;
1220 struct scsi_cmnd *command;
1221 struct scsi_cmnd *command_list;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001222 int jafo = 0;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001223
1224 /*
1225 * Assumptions:
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001226 * - host is locked, unless called by the aacraid thread.
1227 * (a matter of convenience, due to legacy issues surrounding
1228 * eh_host_adapter_reset).
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001229 * - in_reset is asserted, so no new i/o is getting to the
1230 * card.
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001231 * - The card is dead, or will be very shortly ;-/ so no new
1232 * commands are completing in the interrupt service.
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001233 */
1234 host = aac->scsi_host_ptr;
1235 scsi_block_requests(host);
1236 aac_adapter_disable_int(aac);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001237 if (aac->thread->pid != current->pid) {
1238 spin_unlock_irq(host->host_lock);
1239 kthread_stop(aac->thread);
1240 jafo = 1;
1241 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001242
1243 /*
1244 * If a positive health, means in a known DEAD PANIC
1245 * state and the adapter could be reset to `try again'.
1246 */
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001247 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001248
1249 if (retval)
1250 goto out;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001251
Mark Haverkampd18b4482006-11-21 10:40:31 -08001252 /*
1253 * Loop through the fibs, close the synchronous FIBS
1254 */
Mark Haverkamp33bb3b22007-03-15 10:27:21 -07001255 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
Mark Haverkampd18b4482006-11-21 10:40:31 -08001256 struct fib *fib = &aac->fibs[index];
Mark Haverkampa8166a52007-03-15 10:26:22 -07001257 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1258 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
Mark Haverkampd18b4482006-11-21 10:40:31 -08001259 unsigned long flagv;
1260 spin_lock_irqsave(&fib->event_lock, flagv);
1261 up(&fib->event_wait);
1262 spin_unlock_irqrestore(&fib->event_lock, flagv);
1263 schedule();
Mark Haverkamp33bb3b22007-03-15 10:27:21 -07001264 retval = 0;
Mark Haverkampd18b4482006-11-21 10:40:31 -08001265 }
1266 }
Mark Haverkamp33bb3b22007-03-15 10:27:21 -07001267 /* Give some extra time for ioctls to complete. */
1268 if (retval == 0)
1269 ssleep(2);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001270 index = aac->cardtype;
1271
1272 /*
1273 * Re-initialize the adapter, first free resources, then carefully
1274 * apply the initialization sequence to come back again. Only risk
1275 * is a change in Firmware dropping cache, it is assumed the caller
1276 * will ensure that i/o is queisced and the card is flushed in that
1277 * case.
1278 */
1279 aac_fib_map_free(aac);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001280 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1281 aac->comm_addr = NULL;
1282 aac->comm_phys = 0;
1283 kfree(aac->queues);
1284 aac->queues = NULL;
1285 free_irq(aac->pdev->irq, aac);
Vasily Averind0efab22011-09-02 19:31:46 +04001286 if (aac->msi)
1287 pci_disable_msi(aac->pdev);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001288 kfree(aac->fsa_dev);
1289 aac->fsa_dev = NULL;
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001290 quirks = aac_get_driver_ident(index)->quirks;
1291 if (quirks & AAC_QUIRK_31BIT) {
Yang Hongyang929a22a2009-04-06 19:01:16 -07001292 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1293 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001294 goto out;
1295 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07001296 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1297 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001298 goto out;
1299 }
1300 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1301 goto out;
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001302 if (quirks & AAC_QUIRK_31BIT)
Yang Hongyang284901a2009-04-06 19:01:15 -07001303 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001304 goto out;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001305 if (jafo) {
1306 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1307 if (IS_ERR(aac->thread)) {
1308 retval = PTR_ERR(aac->thread);
1309 goto out;
1310 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001311 }
1312 (void)aac_get_adapter_info(aac);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001313 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001314 host->sg_tablesize = 34;
1315 host->max_sectors = (host->sg_tablesize * 8) + 112;
1316 }
1317 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1318 host->sg_tablesize = 17;
1319 host->max_sectors = (host->sg_tablesize * 8) + 112;
1320 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001321 aac_get_config_status(aac, 1);
1322 aac_get_containers(aac);
1323 /*
1324 * This is where the assumption that the Adapter is quiesced
1325 * is important.
1326 */
1327 command_list = NULL;
1328 __shost_for_each_device(dev, host) {
1329 unsigned long flags;
1330 spin_lock_irqsave(&dev->list_lock, flags);
1331 list_for_each_entry(command, &dev->cmd_list, list)
1332 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1333 command->SCp.buffer = (struct scatterlist *)command_list;
1334 command_list = command;
1335 }
1336 spin_unlock_irqrestore(&dev->list_lock, flags);
1337 }
1338 while ((command = command_list)) {
1339 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1340 command->SCp.buffer = NULL;
1341 command->result = DID_OK << 16
1342 | COMMAND_COMPLETE << 8
1343 | SAM_STAT_TASK_SET_FULL;
1344 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1345 command->scsi_done(command);
1346 }
1347 retval = 0;
1348
1349out:
1350 aac->in_reset = 0;
1351 scsi_unblock_requests(host);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001352 if (jafo) {
1353 spin_lock_irq(host->host_lock);
1354 }
1355 return retval;
1356}
1357
1358int aac_reset_adapter(struct aac_dev * aac, int forced)
1359{
1360 unsigned long flagv = 0;
1361 int retval;
1362 struct Scsi_Host * host;
1363
1364 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1365 return -EBUSY;
1366
1367 if (aac->in_reset) {
1368 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1369 return -EBUSY;
1370 }
1371 aac->in_reset = 1;
1372 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1373
1374 /*
1375 * Wait for all commands to complete to this specific
1376 * target (block maximum 60 seconds). Although not necessary,
1377 * it does make us a good storage citizen.
1378 */
1379 host = aac->scsi_host_ptr;
1380 scsi_block_requests(host);
1381 if (forced < 2) for (retval = 60; retval; --retval) {
1382 struct scsi_device * dev;
1383 struct scsi_cmnd * command;
1384 int active = 0;
1385
1386 __shost_for_each_device(dev, host) {
1387 spin_lock_irqsave(&dev->list_lock, flagv);
1388 list_for_each_entry(command, &dev->cmd_list, list) {
1389 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1390 active++;
1391 break;
1392 }
1393 }
1394 spin_unlock_irqrestore(&dev->list_lock, flagv);
1395 if (active)
1396 break;
1397
1398 }
1399 /*
1400 * We can exit If all the commands are complete
1401 */
1402 if (active == 0)
1403 break;
1404 ssleep(1);
1405 }
1406
1407 /* Quiesce build, flush cache, write through mode */
Salyzyn, Markf8583172007-10-30 15:50:49 -04001408 if (forced < 2)
1409 aac_send_shutdown(aac);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001410 spin_lock_irqsave(host->host_lock, flagv);
Salyzyn, Markf8583172007-10-30 15:50:49 -04001411 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001412 spin_unlock_irqrestore(host->host_lock, flagv);
1413
Salyzyn, Markf8583172007-10-30 15:50:49 -04001414 if ((forced < 2) && (retval == -ENODEV)) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001415 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1416 struct fib * fibctx = aac_fib_alloc(aac);
1417 if (fibctx) {
1418 struct aac_pause *cmd;
1419 int status;
1420
1421 aac_fib_init(fibctx);
1422
1423 cmd = (struct aac_pause *) fib_data(fibctx);
1424
1425 cmd->command = cpu_to_le32(VM_ContainerConfig);
1426 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1427 cmd->timeout = cpu_to_le32(1);
1428 cmd->min = cpu_to_le32(1);
1429 cmd->noRescan = cpu_to_le32(1);
1430 cmd->count = cpu_to_le32(0);
1431
1432 status = aac_fib_send(ContainerCommand,
1433 fibctx,
1434 sizeof(struct aac_pause),
1435 FsaNormal,
1436 -2 /* Timeout silently */, 1,
1437 NULL, NULL);
1438
1439 if (status >= 0)
1440 aac_fib_complete(fibctx);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301441 /* FIB should be freed only after getting
1442 * the response from the F/W */
1443 if (status != -ERESTARTSYS)
1444 aac_fib_free(fibctx);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001445 }
1446 }
1447
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001448 return retval;
1449}
1450
1451int aac_check_health(struct aac_dev * aac)
1452{
1453 int BlinkLED;
1454 unsigned long time_now, flagv = 0;
1455 struct list_head * entry;
1456 struct Scsi_Host * host;
1457
1458 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1459 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1460 return 0;
1461
1462 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1463 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1464 return 0; /* OK */
1465 }
1466
1467 aac->in_reset = 1;
1468
1469 /* Fake up an AIF:
1470 * aac_aifcmd.command = AifCmdEventNotify = 1
1471 * aac_aifcmd.seqnum = 0xFFFFFFFF
1472 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1473 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1474 * aac.aifcmd.data[2] = AifHighPriority = 3
1475 * aac.aifcmd.data[3] = BlinkLED
1476 */
1477
1478 time_now = jiffies/HZ;
1479 entry = aac->fib_list.next;
1480
1481 /*
1482 * For each Context that is on the
1483 * fibctxList, make a copy of the
1484 * fib, and then set the event to wake up the
1485 * thread that is waiting for it.
1486 */
1487 while (entry != &aac->fib_list) {
1488 /*
1489 * Extract the fibctx
1490 */
1491 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1492 struct hw_fib * hw_fib;
1493 struct fib * fib;
1494 /*
1495 * Check if the queue is getting
1496 * backlogged
1497 */
1498 if (fibctx->count > 20) {
1499 /*
1500 * It's *not* jiffies folks,
1501 * but jiffies / HZ, so do not
1502 * panic ...
1503 */
1504 u32 time_last = fibctx->jiffies;
1505 /*
1506 * Has it been > 2 minutes
1507 * since the last read off
1508 * the queue?
1509 */
1510 if ((time_now - time_last) > aif_timeout) {
1511 entry = entry->next;
1512 aac_close_fib_context(aac, fibctx);
1513 continue;
1514 }
1515 }
1516 /*
1517 * Warning: no sleep allowed while
1518 * holding spinlock
1519 */
Salyzyn, Mark4dbc22d2007-04-16 11:21:50 -04001520 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1521 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001522 if (fib && hw_fib) {
1523 struct aac_aifcmd * aif;
1524
Mark Haverkampa8166a52007-03-15 10:26:22 -07001525 fib->hw_fib_va = hw_fib;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001526 fib->dev = aac;
1527 aac_fib_init(fib);
1528 fib->type = FSAFS_NTC_FIB_CONTEXT;
1529 fib->size = sizeof (struct fib);
1530 fib->data = hw_fib->data;
1531 aif = (struct aac_aifcmd *)hw_fib->data;
1532 aif->command = cpu_to_le32(AifCmdEventNotify);
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001533 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1534 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1535 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1536 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1537 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001538
1539 /*
1540 * Put the FIB onto the
1541 * fibctx's fibs
1542 */
1543 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1544 fibctx->count++;
1545 /*
1546 * Set the event to wake up the
1547 * thread that will waiting.
1548 */
1549 up(&fibctx->wait_sem);
1550 } else {
1551 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1552 kfree(fib);
1553 kfree(hw_fib);
1554 }
1555 entry = entry->next;
1556 }
1557
1558 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1559
1560 if (BlinkLED < 0) {
1561 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1562 goto out;
1563 }
1564
1565 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1566
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001567 if (!aac_check_reset || ((aac_check_reset == 1) &&
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001568 (aac->supplement_adapter_info.SupportedOptions2 &
1569 AAC_OPTION_IGNORE_RESET)))
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001570 goto out;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001571 host = aac->scsi_host_ptr;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001572 if (aac->thread->pid != current->pid)
1573 spin_lock_irqsave(host->host_lock, flagv);
Salyzyn, Markf8583172007-10-30 15:50:49 -04001574 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001575 if (aac->thread->pid != current->pid)
1576 spin_unlock_irqrestore(host->host_lock, flagv);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001577 return BlinkLED;
1578
1579out:
1580 aac->in_reset = 0;
1581 return BlinkLED;
1582}
1583
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585/**
1586 * aac_command_thread - command processing thread
1587 * @dev: Adapter to monitor
1588 *
1589 * Waits on the commandready event in it's queue. When the event gets set
1590 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1591 * until the queue is empty. When the queue is empty it will wait for
1592 * more FIBs.
1593 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001594
Christoph Hellwigfe273812006-02-14 18:45:06 +01001595int aac_command_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Christoph Hellwigfe273812006-02-14 18:45:06 +01001597 struct aac_dev *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct hw_fib *hw_fib, *hw_newfib;
1599 struct fib *fib, *newfib;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 struct aac_fib_context *fibctx;
1601 unsigned long flags;
1602 DECLARE_WAITQUEUE(wait, current);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001603 unsigned long next_jiffies = jiffies + HZ;
1604 unsigned long next_check_jiffies = next_jiffies;
1605 long difference = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /*
1608 * We can only have one thread per adapter for AIF's.
1609 */
1610 if (dev->aif_thread)
1611 return -EINVAL;
Christoph Hellwigfe273812006-02-14 18:45:06 +01001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 /*
1614 * Let the DPC know it has a place to send the AIF's to.
1615 */
1616 dev->aif_thread = 1;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001617 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 set_current_state(TASK_INTERRUPTIBLE);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001619 dprintk ((KERN_INFO "aac_command_thread start\n"));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001620 while (1) {
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001621 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1622 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct list_head *entry;
1624 struct aac_aifcmd * aifcmd;
1625
1626 set_current_state(TASK_RUNNING);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001627
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001628 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 list_del(entry);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001630
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001631 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 fib = list_entry(entry, struct fib, fiblink);
1633 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001634 * We will process the FIB here or pass it to a
1635 * worker thread that is TBD. We Really can't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * do anything at this point since we don't have
1637 * anything defined for this thread to do.
1638 */
Mark Haverkampa8166a52007-03-15 10:26:22 -07001639 hw_fib = fib->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 memset(fib, 0, sizeof(struct fib));
1641 fib->type = FSAFS_NTC_FIB_CONTEXT;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001642 fib->size = sizeof(struct fib);
Mark Haverkampa8166a52007-03-15 10:26:22 -07001643 fib->hw_fib_va = hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 fib->data = hw_fib->data;
1645 fib->dev = dev;
1646 /*
1647 * We only handle AifRequest fibs from the adapter.
1648 */
1649 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1650 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1651 /* Handle Driver Notify Events */
Mark Haverkamp131256c2005-09-26 13:04:56 -07001652 aac_handle_aif(dev, fib);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07001653 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001654 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* The u32 here is important and intended. We are using
1657 32bit wrapping time to fit the adapter field */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 u32 time_now, time_last;
1660 unsigned long flagv;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001661 unsigned num;
1662 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1663 struct fib ** fib_pool, ** fib_p;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001664
Mark Haverkamp131256c2005-09-26 13:04:56 -07001665 /* Sniff events */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001666 if ((aifcmd->command ==
Mark Haverkamp131256c2005-09-26 13:04:56 -07001667 cpu_to_le32(AifCmdEventNotify)) ||
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001668 (aifcmd->command ==
Mark Haverkamp131256c2005-09-26 13:04:56 -07001669 cpu_to_le32(AifCmdJobProgress))) {
1670 aac_handle_aif(dev, fib);
1671 }
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 time_now = jiffies/HZ;
1674
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001675 /*
1676 * Warning: no sleep allowed while
1677 * holding spinlock. We take the estimate
1678 * and pre-allocate a set of fibs outside the
1679 * lock.
1680 */
1681 num = le32_to_cpu(dev->init->AdapterFibsSize)
1682 / sizeof(struct hw_fib); /* some extra */
1683 spin_lock_irqsave(&dev->fib_lock, flagv);
1684 entry = dev->fib_list.next;
1685 while (entry != &dev->fib_list) {
1686 entry = entry->next;
1687 ++num;
1688 }
1689 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1690 hw_fib_pool = NULL;
1691 fib_pool = NULL;
1692 if (num
1693 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1694 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1695 hw_fib_p = hw_fib_pool;
1696 fib_p = fib_pool;
1697 while (hw_fib_p < &hw_fib_pool[num]) {
1698 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1699 --hw_fib_p;
1700 break;
1701 }
1702 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1703 kfree(*(--hw_fib_p));
1704 break;
1705 }
1706 }
1707 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1708 kfree(fib_pool);
1709 fib_pool = NULL;
1710 kfree(hw_fib_pool);
1711 hw_fib_pool = NULL;
1712 }
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001713 } else {
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001714 kfree(hw_fib_pool);
1715 hw_fib_pool = NULL;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 spin_lock_irqsave(&dev->fib_lock, flagv);
1718 entry = dev->fib_list.next;
1719 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001720 * For each Context that is on the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * fibctxList, make a copy of the
1722 * fib, and then set the event to wake up the
1723 * thread that is waiting for it.
1724 */
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001725 hw_fib_p = hw_fib_pool;
1726 fib_p = fib_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 while (entry != &dev->fib_list) {
1728 /*
1729 * Extract the fibctx
1730 */
1731 fibctx = list_entry(entry, struct aac_fib_context, next);
1732 /*
1733 * Check if the queue is getting
1734 * backlogged
1735 */
1736 if (fibctx->count > 20)
1737 {
1738 /*
1739 * It's *not* jiffies folks,
1740 * but jiffies / HZ so do not
1741 * panic ...
1742 */
1743 time_last = fibctx->jiffies;
1744 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001745 * Has it been > 2 minutes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * since the last read off
1747 * the queue?
1748 */
Mark Haverkamp404d9a92006-05-10 09:12:48 -07001749 if ((time_now - time_last) > aif_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 entry = entry->next;
1751 aac_close_fib_context(dev, fibctx);
1752 continue;
1753 }
1754 }
1755 /*
1756 * Warning: no sleep allowed while
1757 * holding spinlock
1758 */
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001759 if (hw_fib_p < &hw_fib_pool[num]) {
1760 hw_newfib = *hw_fib_p;
1761 *(hw_fib_p++) = NULL;
1762 newfib = *fib_p;
1763 *(fib_p++) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Make the copy of the FIB
1766 */
1767 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1768 memcpy(newfib, fib, sizeof(struct fib));
Mark Haverkampa8166a52007-03-15 10:26:22 -07001769 newfib->hw_fib_va = hw_newfib;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * Put the FIB onto the
1772 * fibctx's fibs
1773 */
1774 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1775 fibctx->count++;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001776 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 * Set the event to wake up the
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001778 * thread that is waiting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
1780 up(&fibctx->wait_sem);
1781 } else {
1782 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 entry = entry->next;
1785 }
1786 /*
1787 * Set the status of this FIB
1788 */
Mark Haverkamp 56b58712005-04-27 06:05:51 -07001789 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001790 aac_fib_adapter_complete(fib, sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 spin_unlock_irqrestore(&dev->fib_lock, flagv);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001792 /* Free up the remaining resources */
1793 hw_fib_p = hw_fib_pool;
1794 fib_p = fib_pool;
1795 while (hw_fib_p < &hw_fib_pool[num]) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001796 kfree(*hw_fib_p);
1797 kfree(*fib_p);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001798 ++fib_p;
1799 ++hw_fib_p;
1800 }
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001801 kfree(hw_fib_pool);
1802 kfree(fib_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 kfree(fib);
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001805 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807 /*
1808 * There are no more AIF's
1809 */
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001810 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001811
1812 /*
1813 * Background activity
1814 */
1815 if ((time_before(next_check_jiffies,next_jiffies))
1816 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1817 next_check_jiffies = next_jiffies;
1818 if (aac_check_health(dev) == 0) {
1819 difference = ((long)(unsigned)check_interval)
1820 * HZ;
1821 next_check_jiffies = jiffies + difference;
1822 } else if (!dev->queues)
1823 break;
1824 }
1825 if (!time_before(next_check_jiffies,next_jiffies)
1826 && ((difference = next_jiffies - jiffies) <= 0)) {
1827 struct timeval now;
1828 int ret;
1829
1830 /* Don't even try to talk to adapter if its sick */
1831 ret = aac_check_health(dev);
1832 if (!ret && !dev->queues)
1833 break;
1834 next_check_jiffies = jiffies
1835 + ((long)(unsigned)check_interval)
1836 * HZ;
1837 do_gettimeofday(&now);
1838
1839 /* Synchronize our watches */
1840 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1841 && (now.tv_usec > (1000000 / HZ)))
1842 difference = (((1000000 - now.tv_usec) * HZ)
1843 + 500000) / 1000000;
1844 else if (ret == 0) {
1845 struct fib *fibptr;
1846
1847 if ((fibptr = aac_fib_alloc(dev))) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301848 int status;
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001849 __le32 *info;
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001850
1851 aac_fib_init(fibptr);
1852
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001853 info = (__le32 *) fib_data(fibptr);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001854 if (now.tv_usec > 500000)
1855 ++now.tv_sec;
1856
1857 *info = cpu_to_le32(now.tv_sec);
1858
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301859 status = aac_fib_send(SendHostTime,
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001860 fibptr,
1861 sizeof(*info),
1862 FsaNormal,
1863 1, 1,
1864 NULL,
1865 NULL);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301866 /* Do not set XferState to zero unless
1867 * receives a response from F/W */
1868 if (status >= 0)
1869 aac_fib_complete(fibptr);
1870 /* FIB should be freed only after
1871 * getting the response from the F/W */
1872 if (status != -ERESTARTSYS)
1873 aac_fib_free(fibptr);
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001874 }
1875 difference = (long)(unsigned)update_interval*HZ;
1876 } else {
1877 /* retry shortly */
1878 difference = 10 * HZ;
1879 }
1880 next_jiffies = jiffies + difference;
1881 if (time_before(next_check_jiffies,next_jiffies))
1882 difference = next_check_jiffies - jiffies;
1883 }
1884 if (difference <= 0)
1885 difference = 1;
1886 set_current_state(TASK_INTERRUPTIBLE);
1887 schedule_timeout(difference);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Christoph Hellwigfe273812006-02-14 18:45:06 +01001889 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001892 if (dev->queues)
1893 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 dev->aif_thread = 0;
Mark Haverkamp2f1309802005-09-26 13:02:15 -07001895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}