]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/scsi/aacraid/commsup.c
Fix common misspellings
[mirror_ubuntu-hirsute-kernel.git] / drivers / scsi / aacraid / commsup.c
CommitLineData
1da177e4
LT
1/*
2 * Adaptec AAC series RAID controller driver
fa195afe 3 * (c) Copyright 2001 Red Hat Inc.
1da177e4
LT
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
e8b12f0f
MR
8 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
1da177e4
LT
10 *
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
7c00ffa3 29 * communication.
1da177e4
LT
30 *
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>
164006da 42#include <linux/delay.h>
fe27381d 43#include <linux/kthread.h>
6a3670c4 44#include <linux/interrupt.h>
6188e10d 45#include <linux/semaphore.h>
8c867b25 46#include <scsi/scsi.h>
7c00ffa3 47#include <scsi/scsi_host.h>
131256cf 48#include <scsi/scsi_device.h>
8c867b25 49#include <scsi/scsi_cmnd.h>
1da177e4
LT
50
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 */
8ce3eca4 60
1da177e4
LT
61static int fib_map_alloc(struct aac_dev *dev)
62{
7c00ffa3
MH
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));
e8b12f0f
MR
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)
1da177e4
LT
72 return -ENOMEM;
73 return 0;
74}
75
76/**
bfb35aa8 77 * aac_fib_map_free - free the fib objects
1da177e4
LT
78 * @dev: Adapter to free
79 *
80 * Free the PCI mappings and the memory allocated for FIB blocks
81 * on this adapter.
82 */
83
bfb35aa8 84void aac_fib_map_free(struct aac_dev *dev)
1da177e4 85{
9ad5204d
SM
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;
1da177e4
LT
91}
92
93/**
bfb35aa8 94 * aac_fib_setup - setup the fibs
1da177e4
LT
95 * @dev: Adapter to set up
96 *
b595076a 97 * Allocate the PCI space for the fibs, map it and then initialise the
1da177e4
LT
98 * fib area, the unmapped fib data and also the free list
99 */
100
bfb35aa8 101int aac_fib_setup(struct aac_dev * dev)
1da177e4
LT
102{
103 struct fib *fibptr;
a8166a52 104 struct hw_fib *hw_fib;
1da177e4
LT
105 dma_addr_t hw_fib_pa;
106 int i;
7c00ffa3
MH
107
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)
1da177e4 114 return -ENOMEM;
8ce3eca4 115
e8b12f0f
MR
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));
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
a8166a52 130 hw_fib = dev->hw_fib_va;
1da177e4 131 hw_fib_pa = dev->hw_fib_pa;
1da177e4
LT
132 /*
133 * Initialise the fibs
134 */
8ce3eca4
SM
135 for (i = 0, fibptr = &dev->fibs[i];
136 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
137 i++, fibptr++)
1da177e4
LT
138 {
139 fibptr->dev = dev;
a8166a52
MH
140 fibptr->hw_fib_va = hw_fib;
141 fibptr->data = (void *) fibptr->hw_fib_va->data;
1da177e4 142 fibptr->next = fibptr+1; /* Forward chain the fibs */
6de76cfc 143 sema_init(&fibptr->event_wait, 0);
1da177e4 144 spin_lock_init(&fibptr->event_lock);
a8166a52
MH
145 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
146 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
1da177e4 147 fibptr->hw_fib_pa = hw_fib_pa;
e8b12f0f
MR
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);
1da177e4
LT
152 }
153 /*
154 * Add the fib chain to the free list
155 */
7c00ffa3 156 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
1da177e4
LT
157 /*
158 * Enable this to debug out of queue space
159 */
160 dev->free_fib = &dev->fibs[0];
161 return 0;
162}
163
164/**
bfb35aa8 165 * aac_fib_alloc - allocate a fib
1da177e4
LT
166 * @dev: Adapter to allocate the fib for
167 *
168 * Allocate a fib from the adapter fib pool. If the pool is empty we
7c00ffa3 169 * return NULL.
1da177e4 170 */
8ce3eca4 171
bfb35aa8 172struct fib *aac_fib_alloc(struct aac_dev *dev)
1da177e4
LT
173{
174 struct fib * fibptr;
175 unsigned long flags;
176 spin_lock_irqsave(&dev->fib_lock, flags);
8ce3eca4 177 fibptr = dev->free_fib;
7c00ffa3
MH
178 if(!fibptr){
179 spin_unlock_irqrestore(&dev->fib_lock, flags);
180 return fibptr;
181 }
1da177e4
LT
182 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 */
a8166a52 193 fibptr->hw_fib_va->header.XferState = 0;
b6ef70f3 194 fibptr->flags = 0;
1da177e4
LT
195 fibptr->callback = NULL;
196 fibptr->callback_data = NULL;
197
198 return fibptr;
199}
200
201/**
bfb35aa8 202 * aac_fib_free - free a fib
1da177e4
LT
203 * @fibptr: fib to free up
204 *
205 * Frees up a fib and places it on the appropriate queue
1da177e4 206 */
8ce3eca4 207
bfb35aa8 208void aac_fib_free(struct fib *fibptr)
1da177e4 209{
cacb6dc3
PNRCEH
210 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);
1da177e4
LT
218
219 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
03d44337 220 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
1da177e4 221 aac_config.fib_timeouts++;
03d44337
MH
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));
226 }
227 fibptr->next = fibptr->dev->free_fib;
228 fibptr->dev->free_fib = fibptr;
1da177e4
LT
229 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
230}
231
232/**
bfb35aa8 233 * aac_fib_init - initialise a fib
1da177e4 234 * @fibptr: The fib to initialize
8ce3eca4 235 *
1da177e4
LT
236 * Set up the generic fib fields ready for use
237 */
8ce3eca4 238
bfb35aa8 239void aac_fib_init(struct fib *fibptr)
1da177e4 240{
a8166a52 241 struct hw_fib *hw_fib = fibptr->hw_fib_va;
1da177e4
LT
242
243 hw_fib->header.StructType = FIB_MAGIC;
7c00ffa3
MH
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);
8e0c5ebd 246 hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
1da177e4 247 hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
7c00ffa3 248 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
1da177e4
LT
249}
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 */
8ce3eca4 258
4833869e 259static void fib_dealloc(struct fib * fibptr)
1da177e4 260{
a8166a52 261 struct hw_fib *hw_fib = fibptr->hw_fib_va;
125e1874 262 BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
8ce3eca4 263 hw_fib->header.XferState = 0;
1da177e4
LT
264}
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 */
8ce3eca4 272
1da177e4
LT
273/**
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 */
8ce3eca4 285
1da177e4
LT
286static 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;
bed30de4 289 unsigned long idx;
1da177e4
LT
290
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];
bed30de4
MH
299
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) {
1640a2c3 304 if (qid == AdapNormCmdQueue)
bed30de4 305 idx = ADAP_NORM_CMD_ENTRIES;
1640a2c3 306 else
bed30de4
MH
307 idx = ADAP_NORM_RESP_ENTRIES;
308 }
309 if (idx != le32_to_cpu(*(q->headers.consumer)))
8ce3eca4 310 *nonotify = 1;
bed30de4 311 }
1da177e4 312
1640a2c3 313 if (qid == AdapNormCmdQueue) {
8ce3eca4 314 if (*index >= ADAP_NORM_CMD_ENTRIES)
1da177e4 315 *index = 0; /* Wrap to front of the Producer Queue. */
1640a2c3 316 } else {
8ce3eca4 317 if (*index >= ADAP_NORM_RESP_ENTRIES)
1da177e4
LT
318 *index = 0; /* Wrap to front of the Producer Queue. */
319 }
1da177e4 320
8ce3eca4
SM
321 /* Queue is full */
322 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
7c00ffa3 323 printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
1da177e4
LT
324 qid, q->numpending);
325 return 0;
326 } else {
8ce3eca4 327 *entry = q->base + *index;
1da177e4
LT
328 return 1;
329 }
8ce3eca4 330}
1da177e4
LT
331
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
28713324 348int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
1da177e4
LT
349{
350 struct aac_entry * entry = NULL;
351 int map = 0;
8ce3eca4 352
1640a2c3 353 if (qid == AdapNormCmdQueue) {
1da177e4 354 /* if no entries wait for some if caller wants to */
8ce3eca4 355 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
1da177e4
LT
356 printk(KERN_ERR "GetEntries failed\n");
357 }
8ce3eca4
SM
358 /*
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;
1640a2c3 363 } else {
8ce3eca4 364 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
1da177e4
LT
365 /* if no entries wait for some if caller wants to */
366 }
8ce3eca4
SM
367 /*
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 */
1da177e4 373 hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
8ce3eca4 374 map = 0;
1da177e4
LT
375 }
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
1da177e4 385/*
8ce3eca4
SM
386 * Define the highest level of host to adapter communication routines.
387 * These routines will support host to adapter FS commuication. These
1da177e4
LT
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.
391 */
392
393/**
bfb35aa8 394 * aac_fib_send - send a fib to the adapter
1da177e4
LT
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
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 */
8ce3eca4 409
bfb35aa8
MH
410int 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)
1da177e4 413{
1da177e4 414 struct aac_dev * dev = fibptr->dev;
a8166a52 415 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1da177e4 416 unsigned long flags = 0;
1640a2c3 417 unsigned long qflags;
cacb6dc3
PNRCEH
418 unsigned long mflags = 0;
419
1640a2c3 420
1da177e4
LT
421 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
422 return -EBUSY;
423 /*
25985edc 424 * There are 5 cases with the wait and response requested flags.
1da177e4
LT
425 * 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
8ce3eca4 430 * further processing will be done besides deleting the Fib. We
1da177e4
LT
431 * 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 */
b6ef70f3 434 fibptr->flags = 0;
1da177e4
LT
435 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);
8ce3eca4 446 }
1da177e4
LT
447 /*
448 * Map the fib into 32bits by using the fib number
449 */
450
8e0c5ebd 451 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
1da177e4
LT
452 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);
a8166a52 462 fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
1da177e4
LT
463 /*
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;
8ce3eca4 469 }
1da177e4
LT
470 /*
471 * Get a queue entry connect the FIB to it and send an notify
472 * the adapter a command is ready.
473 */
1640a2c3 474 hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
1da177e4 475
1da177e4
LT
476 /*
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;
b6ef70f3 483 fibptr->flags = FIB_CONTEXT_FLAG;
1da177e4 484 }
1da177e4
LT
485
486 fibptr->done = 0;
1da177e4 487
1640a2c3
MH
488 FIB_COUNTER_INCREMENT(aac_config.FibsSent);
489
1640a2c3 490 dprintk((KERN_DEBUG "Fib contents:.\n"));
8e0c5ebd
MH
491 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)));
a8166a52 494 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
1640a2c3
MH
495 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
c8f7b073 498 if (!dev->queues)
65101355 499 return -EBUSY;
1640a2c3 500
cacb6dc3
PNRCEH
501 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);
1640a2c3 512 spin_lock_irqsave(&fibptr->event_lock, flags);
cacb6dc3
PNRCEH
513 }
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
8e0c5ebd 526
1da177e4 527 /*
8ce3eca4 528 * If the caller wanted us to wait for response wait now.
1da177e4 529 */
8ce3eca4 530
1da177e4
LT
531 if (wait) {
532 spin_unlock_irqrestore(&fibptr->event_lock, flags);
9203344c
MH
533 /* 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 */
9203344c 542 while (down_trylock(&fibptr->event_wait)) {
33524b70 543 int blink;
9203344c 544 if (--count == 0) {
28713324 545 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
9203344c
MH
546 spin_lock_irqsave(q->lock, qflags);
547 q->numpending--;
9203344c
MH
548 spin_unlock_irqrestore(q->lock, qflags);
549 if (wait == -1) {
bfb35aa8 550 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
9203344c
MH
551 "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 }
33524b70
MH
557 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 }
9203344c
MH
565 udelay(5);
566 }
0462590e 567 } else if (down_interruptible(&fibptr->event_wait)) {
cacb6dc3
PNRCEH
568 /* Do nothing ... satisfy
569 * down_interruptible must_check */
e6990c64 570 }
cacb6dc3 571
33bb3b29 572 spin_lock_irqsave(&fibptr->event_lock, flags);
cacb6dc3 573 if (fibptr->done == 0) {
33bb3b29 574 fibptr->done = 2; /* Tell interrupt we aborted */
c8f7b073 575 spin_unlock_irqrestore(&fibptr->event_lock, flags);
cacb6dc3 576 return -ERESTARTSYS;
c8f7b073 577 }
33bb3b29 578 spin_unlock_irqrestore(&fibptr->event_lock, flags);
125e1874 579 BUG_ON(fibptr->done == 0);
8ce3eca4 580
912d4e88 581 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
1da177e4 582 return -ETIMEDOUT;
912d4e88 583 return 0;
1da177e4
LT
584 }
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
8ce3eca4 595/**
1da177e4
LT
596 * 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
8ce3eca4
SM
602 * we are a consumer of, and return the address of the queue entry. It does
603 * not change the state of the queue.
1da177e4
LT
604 */
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 */
8ce3eca4
SM
618 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
619 index = 0;
1da177e4 620 else
8ce3eca4 621 index = le32_to_cpu(*q->headers.consumer);
1da177e4
LT
622 *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;
8ce3eca4 645
1da177e4
LT
646 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
647 *q->headers.consumer = cpu_to_le32(1);
648 else
36b8dd1b 649 le32_add_cpu(q->headers.consumer, 1);
8ce3eca4 650
1da177e4
LT
651 if (wasfull) {
652 switch (qid) {
653
654 case HostNormCmdQueue:
655 notify = HostNormCmdNotFull;
656 break;
1da177e4
LT
657 case HostNormRespQueue:
658 notify = HostNormRespNotFull;
659 break;
1da177e4
LT
660 default:
661 BUG();
662 return;
663 }
664 aac_adapter_notify(dev, notify);
665 }
8ce3eca4 666}
1da177e4
LT
667
668/**
bfb35aa8 669 * aac_fib_adapter_complete - complete adapter issued fib
1da177e4
LT
670 * @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
bfb35aa8 677int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
1da177e4 678{
a8166a52 679 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1da177e4 680 struct aac_dev * dev = fibptr->dev;
1640a2c3 681 struct aac_queue * q;
1da177e4 682 unsigned long nointr = 0;
1640a2c3
MH
683 unsigned long qflags;
684
e8b12f0f
MR
685 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
686 kfree(hw_fib);
687 return 0;
688 }
689
1640a2c3 690 if (hw_fib->header.XferState == 0) {
28713324 691 if (dev->comm_interface == AAC_COMM_MESSAGE)
e8b12f0f 692 kfree(hw_fib);
8ce3eca4 693 return 0;
1640a2c3 694 }
1da177e4
LT
695 /*
696 * If we plan to do anything check the structure type first.
8ce3eca4
SM
697 */
698 if (hw_fib->header.StructType != FIB_MAGIC) {
28713324 699 if (dev->comm_interface == AAC_COMM_MESSAGE)
e8b12f0f 700 kfree(hw_fib);
8ce3eca4 701 return -EINVAL;
1da177e4
LT
702 }
703 /*
704 * This block handles the case where the adapter had sent us a
705 * command and we have finished processing the command. We
8ce3eca4
SM
706 * call completeFib when we are done processing the command
707 * and want to send a response back to the adapter. This will
1da177e4
LT
708 * send the completed cdb to the adapter.
709 */
710 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
28713324 711 if (dev->comm_interface == AAC_COMM_MESSAGE) {
8e0c5ebd
MH
712 kfree (hw_fib);
713 } else {
8ce3eca4
SM
714 u32 index;
715 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
8e0c5ebd
MH
716 if (size) {
717 size += sizeof(struct aac_fibhdr);
8ce3eca4 718 if (size > le16_to_cpu(hw_fib->header.SenderSize))
8e0c5ebd
MH
719 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);
1da177e4 729 }
8ce3eca4
SM
730 } else {
731 printk(KERN_WARNING "aac_fib_adapter_complete: "
732 "Unknown xferstate detected.\n");
733 BUG();
1da177e4 734 }
1da177e4
LT
735 return 0;
736}
737
738/**
bfb35aa8 739 * aac_fib_complete - fib completion handler
1da177e4
LT
740 * @fib: FIB to complete
741 *
742 * Will do all necessary work to complete a FIB.
743 */
8ce3eca4 744
bfb35aa8 745int aac_fib_complete(struct fib *fibptr)
1da177e4 746{
cacb6dc3 747 unsigned long flags;
a8166a52 748 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1da177e4
LT
749
750 /*
751 * Check for a fib which has already been completed
752 */
753
754 if (hw_fib->header.XferState == 0)
8ce3eca4 755 return 0;
1da177e4
LT
756 /*
757 * If we plan to do anything check the structure type first.
8ce3eca4 758 */
1da177e4
LT
759
760 if (hw_fib->header.StructType != FIB_MAGIC)
8ce3eca4 761 return -EINVAL;
1da177e4 762 /*
8ce3eca4 763 * This block completes a cdb which orginated on the host and we
1da177e4
LT
764 * 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 */
cacb6dc3
PNRCEH
768 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
1da177e4
LT
775 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();
8ce3eca4 791 }
1da177e4
LT
792 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{
1da177e4 806 char *cp = dev->printfbuf;
7c00ffa3
MH
807 if (dev->printf_enabled)
808 {
809 int length = val & 0xffff;
810 int level = (val >> 16) & 0xffff;
8ce3eca4 811
7c00ffa3
MH
812 /*
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)
1241f359 821 printk(KERN_WARNING "%s:%s", dev->name, cp);
7c00ffa3 822 else
1241f359 823 printk(KERN_INFO "%s:%s", dev->name, cp);
7c00ffa3 824 }
8ce3eca4 825 memset(cp, 0, 256);
1da177e4
LT
826}
827
131256cf
MH
828
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
31876f32 838#define AIF_SNIFF_TIMEOUT (30*HZ)
131256cf
MH
839static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
840{
a8166a52 841 struct hw_fib * hw_fib = fibptr->hw_fib_va;
131256cf 842 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
0995ad38 843 u32 channel, id, lun, container;
131256cf
MH
844 struct scsi_device *device;
845 enum {
846 NOTHING,
847 DELETE,
848 ADD,
849 CHANGE
0995ad38 850 } device_config_needed = NOTHING;
131256cf
MH
851
852 /* Sniff for container changes */
853
c8f7b073 854 if (!dev || !dev->fsa_dev)
131256cf 855 return;
0995ad38 856 container = channel = id = lun = (u32)-1;
131256cf
MH
857
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:
f3307f72 866 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
131256cf
MH
867 /*
868 * Morph or Expand complete
869 */
870 case AifDenMorphComplete:
871 case AifDenVolumeExtendComplete:
f3307f72 872 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
873 if (container >= dev->maximum_num_containers)
874 break;
875
876 /*
f64a181d 877 * Find the scsi_device associated with the SCSI
131256cf
MH
878 * 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)) {
8ce3eca4
SM
884 device = scsi_device_lookup(dev->scsi_host_ptr,
885 CONTAINER_TO_CHANNEL(container),
886 CONTAINER_TO_ID(container),
131256cf
MH
887 CONTAINER_TO_LUN(container));
888 if (device) {
889 dev->fsa_dev[container].config_needed = CHANGE;
890 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
31876f32 891 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
892 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;
31876f32 904 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 905 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 906 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
907 dev->fsa_dev[container].config_waiting_on = 0;
908 } else for (container = 0;
909 container < dev->maximum_num_containers; ++container) {
31876f32 910 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 911 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 912 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
913 dev->fsa_dev[container].config_waiting_on = 0;
914 }
915 break;
916
917 case AifCmdEventNotify:
f3307f72 918 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
95e852e1
SM
919 case AifEnBatteryEvent:
920 dev->cache_protected =
921 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
922 break;
131256cf
MH
923 /*
924 * Add an Array.
925 */
926 case AifEnAddContainer:
f3307f72 927 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
928 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;
31876f32 933 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
934 break;
935
936 /*
937 * Delete an Array.
938 */
939 case AifEnDeleteContainer:
f3307f72 940 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
941 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;
31876f32 946 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
947 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:
f3307f72 954 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
955 if (container >= dev->maximum_num_containers)
956 break;
31876f32
MH
957 if (dev->fsa_dev[container].config_waiting_on &&
958 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
959 break;
960 dev->fsa_dev[container].config_needed = CHANGE;
961 dev->fsa_dev[container].config_waiting_on =
962 AifEnConfigChange;
31876f32 963 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
964 break;
965
966 case AifEnConfigChange:
967 break;
968
cb1042f2
SM
969 case AifEnAddJBOD:
970 case AifEnDeleteJBOD:
971 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
a4576b5d
MS
972 if ((container >> 28)) {
973 container = (u32)-1;
cb1042f2 974 break;
a4576b5d 975 }
cb1042f2 976 channel = (container >> 24) & 0xF;
a4576b5d
MS
977 if (channel >= dev->maximum_num_channels) {
978 container = (u32)-1;
cb1042f2 979 break;
a4576b5d 980 }
cb1042f2 981 id = container & 0xFFFF;
a4576b5d
MS
982 if (id >= dev->maximum_num_physicals) {
983 container = (u32)-1;
cb1042f2 984 break;
a4576b5d 985 }
cb1042f2 986 lun = (container >> 16) & 0xFF;
a4576b5d 987 container = (u32)-1;
cb1042f2
SM
988 channel = aac_phys_to_logical(channel);
989 device_config_needed =
990 (((__le32 *)aifcmd->data)[0] ==
991 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
5ca05594
RM
992 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 }
cb1042f2
SM
1002 break;
1003
0995ad38 1004 case AifEnEnclosureManagement:
cb1042f2
SM
1005 /*
1006 * If in JBOD mode, automatic exposure of new
1007 * physical target to be suppressed until configured.
1008 */
1009 if (dev->jbod)
1010 break;
0995ad38
SM
1011 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]);
a4576b5d
MS
1016 if ((container >> 28)) {
1017 container = (u32)-1;
0995ad38 1018 break;
a4576b5d 1019 }
0995ad38 1020 channel = (container >> 24) & 0xF;
a4576b5d
MS
1021 if (channel >= dev->maximum_num_channels) {
1022 container = (u32)-1;
0995ad38 1023 break;
a4576b5d 1024 }
0995ad38
SM
1025 id = container & 0xFFFF;
1026 lun = (container >> 16) & 0xFF;
a4576b5d 1027 container = (u32)-1;
0995ad38
SM
1028 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;
131256cf
MH
1045 }
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;
31876f32 1054 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 1055 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 1056 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
1057 dev->fsa_dev[container].config_waiting_on = 0;
1058 } else for (container = 0;
1059 container < dev->maximum_num_containers; ++container) {
31876f32 1060 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 1061 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 1062 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
1063 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
f3307f72
CH
1076 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))) {
131256cf
MH
1079 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;
31876f32
MH
1089 dev->fsa_dev[container].config_waiting_stamp =
1090 jiffies;
131256cf
MH
1091 }
1092 }
f3307f72
CH
1093 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1094 ((__le32 *)aifcmd->data)[6] == 0 &&
1095 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
131256cf
MH
1096 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;
31876f32
MH
1106 dev->fsa_dev[container].config_waiting_stamp =
1107 jiffies;
131256cf
MH
1108 }
1109 }
1110 break;
1111 }
1112
a4576b5d
MS
1113 container = 0;
1114retry_next:
0995ad38 1115 if (device_config_needed == NOTHING)
a4576b5d 1116 for (; container < dev->maximum_num_containers; ++container) {
31876f32
MH
1117 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)) {
131256cf
MH
1120 device_config_needed =
1121 dev->fsa_dev[container].config_needed;
1122 dev->fsa_dev[container].config_needed = NOTHING;
0995ad38
SM
1123 channel = CONTAINER_TO_CHANNEL(container);
1124 id = CONTAINER_TO_ID(container);
1125 lun = CONTAINER_TO_LUN(container);
131256cf
MH
1126 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
131256cf 1138 /*
f64a181d 1139 * Find the scsi_device associated with the SCSI address,
131256cf
MH
1140 * 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 /*
bfb35aa8 1147 * force reload of disk info via aac_probe_container
131256cf 1148 */
0995ad38
SM
1149 if ((channel == CONTAINER_CHANNEL) &&
1150 (device_config_needed != NOTHING)) {
1151 if (dev->fsa_dev[container].valid == 1)
1152 dev->fsa_dev[container].valid = 2;
bfb35aa8 1153 aac_probe_container(dev, container);
0995ad38
SM
1154 }
1155 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
131256cf
MH
1156 if (device) {
1157 switch (device_config_needed) {
1158 case DELETE:
9cccde93
RM
1159#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1160 scsi_remove_device(device);
1161#else
0995ad38
SM
1162 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 }
9cccde93 1170#endif
0995ad38
SM
1171 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 */
131256cf 1182 case CHANGE:
0995ad38
SM
1183 if ((channel == CONTAINER_CHANNEL)
1184 && (!dev->fsa_dev[container].valid)) {
9cccde93
RM
1185#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1186 scsi_remove_device(device);
1187#else
0995ad38
SM
1188 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");
9cccde93 1194#endif
0995ad38
SM
1195 break;
1196 }
131256cf
MH
1197 scsi_rescan_device(&device->sdev_gendev);
1198
1199 default:
1200 break;
1201 }
1202 scsi_device_put(device);
0995ad38 1203 device_config_needed = NOTHING;
131256cf 1204 }
0995ad38
SM
1205 if (device_config_needed == ADD)
1206 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
a4576b5d
MS
1207 if (channel == CONTAINER_CHANNEL) {
1208 container++;
1209 device_config_needed = NOTHING;
1210 goto retry_next;
1211 }
131256cf
MH
1212}
1213
29c97684 1214static int _aac_reset_adapter(struct aac_dev *aac, int forced)
8c867b25
MH
1215{
1216 int index, quirks;
8c867b25
MH
1217 int retval;
1218 struct Scsi_Host *host;
1219 struct scsi_device *dev;
1220 struct scsi_cmnd *command;
1221 struct scsi_cmnd *command_list;
29c97684 1222 int jafo = 0;
8c867b25
MH
1223
1224 /*
1225 * Assumptions:
29c97684
SM
1226 * - host is locked, unless called by the aacraid thread.
1227 * (a matter of convenience, due to legacy issues surrounding
1228 * eh_host_adapter_reset).
8c867b25
MH
1229 * - in_reset is asserted, so no new i/o is getting to the
1230 * card.
29c97684
SM
1231 * - The card is dead, or will be very shortly ;-/ so no new
1232 * commands are completing in the interrupt service.
8c867b25
MH
1233 */
1234 host = aac->scsi_host_ptr;
1235 scsi_block_requests(host);
1236 aac_adapter_disable_int(aac);
29c97684
SM
1237 if (aac->thread->pid != current->pid) {
1238 spin_unlock_irq(host->host_lock);
1239 kthread_stop(aac->thread);
1240 jafo = 1;
1241 }
8c867b25
MH
1242
1243 /*
1244 * If a positive health, means in a known DEAD PANIC
1245 * state and the adapter could be reset to `try again'.
1246 */
29c97684 1247 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
8c867b25
MH
1248
1249 if (retval)
1250 goto out;
8c867b25 1251
d18b448f
MH
1252 /*
1253 * Loop through the fibs, close the synchronous FIBS
1254 */
33bb3b29 1255 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
d18b448f 1256 struct fib *fib = &aac->fibs[index];
a8166a52
MH
1257 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1258 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
d18b448f
MH
1259 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();
33bb3b29 1264 retval = 0;
d18b448f
MH
1265 }
1266 }
33bb3b29
MH
1267 /* Give some extra time for ioctls to complete. */
1268 if (retval == 0)
1269 ssleep(2);
8c867b25
MH
1270 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);
8c867b25
MH
1280 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);
1286 kfree(aac->fsa_dev);
1287 aac->fsa_dev = NULL;
94cf6ba1
SM
1288 quirks = aac_get_driver_ident(index)->quirks;
1289 if (quirks & AAC_QUIRK_31BIT) {
929a22a5
YH
1290 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1291 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
8c867b25
MH
1292 goto out;
1293 } else {
284901a9
YH
1294 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1295 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
8c867b25
MH
1296 goto out;
1297 }
1298 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1299 goto out;
94cf6ba1 1300 if (quirks & AAC_QUIRK_31BIT)
284901a9 1301 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
8c867b25 1302 goto out;
29c97684
SM
1303 if (jafo) {
1304 aac->thread = kthread_run(aac_command_thread, aac, aac->name);
1305 if (IS_ERR(aac->thread)) {
1306 retval = PTR_ERR(aac->thread);
1307 goto out;
1308 }
8c867b25
MH
1309 }
1310 (void)aac_get_adapter_info(aac);
8c867b25 1311 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
8ce3eca4
SM
1312 host->sg_tablesize = 34;
1313 host->max_sectors = (host->sg_tablesize * 8) + 112;
1314 }
1315 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1316 host->sg_tablesize = 17;
1317 host->max_sectors = (host->sg_tablesize * 8) + 112;
1318 }
8c867b25
MH
1319 aac_get_config_status(aac, 1);
1320 aac_get_containers(aac);
1321 /*
1322 * This is where the assumption that the Adapter is quiesced
1323 * is important.
1324 */
1325 command_list = NULL;
1326 __shost_for_each_device(dev, host) {
1327 unsigned long flags;
1328 spin_lock_irqsave(&dev->list_lock, flags);
1329 list_for_each_entry(command, &dev->cmd_list, list)
1330 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1331 command->SCp.buffer = (struct scatterlist *)command_list;
1332 command_list = command;
1333 }
1334 spin_unlock_irqrestore(&dev->list_lock, flags);
1335 }
1336 while ((command = command_list)) {
1337 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1338 command->SCp.buffer = NULL;
1339 command->result = DID_OK << 16
1340 | COMMAND_COMPLETE << 8
1341 | SAM_STAT_TASK_SET_FULL;
1342 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1343 command->scsi_done(command);
1344 }
1345 retval = 0;
1346
1347out:
1348 aac->in_reset = 0;
1349 scsi_unblock_requests(host);
29c97684
SM
1350 if (jafo) {
1351 spin_lock_irq(host->host_lock);
1352 }
1353 return retval;
1354}
1355
1356int aac_reset_adapter(struct aac_dev * aac, int forced)
1357{
1358 unsigned long flagv = 0;
1359 int retval;
1360 struct Scsi_Host * host;
1361
1362 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1363 return -EBUSY;
1364
1365 if (aac->in_reset) {
1366 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1367 return -EBUSY;
1368 }
1369 aac->in_reset = 1;
1370 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1371
1372 /*
1373 * Wait for all commands to complete to this specific
1374 * target (block maximum 60 seconds). Although not necessary,
1375 * it does make us a good storage citizen.
1376 */
1377 host = aac->scsi_host_ptr;
1378 scsi_block_requests(host);
1379 if (forced < 2) for (retval = 60; retval; --retval) {
1380 struct scsi_device * dev;
1381 struct scsi_cmnd * command;
1382 int active = 0;
1383
1384 __shost_for_each_device(dev, host) {
1385 spin_lock_irqsave(&dev->list_lock, flagv);
1386 list_for_each_entry(command, &dev->cmd_list, list) {
1387 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1388 active++;
1389 break;
1390 }
1391 }
1392 spin_unlock_irqrestore(&dev->list_lock, flagv);
1393 if (active)
1394 break;
1395
1396 }
1397 /*
1398 * We can exit If all the commands are complete
1399 */
1400 if (active == 0)
1401 break;
1402 ssleep(1);
1403 }
1404
1405 /* Quiesce build, flush cache, write through mode */
f858317d
SM
1406 if (forced < 2)
1407 aac_send_shutdown(aac);
29c97684 1408 spin_lock_irqsave(host->host_lock, flagv);
f858317d 1409 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
29c97684
SM
1410 spin_unlock_irqrestore(host->host_lock, flagv);
1411
f858317d 1412 if ((forced < 2) && (retval == -ENODEV)) {
29c97684
SM
1413 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1414 struct fib * fibctx = aac_fib_alloc(aac);
1415 if (fibctx) {
1416 struct aac_pause *cmd;
1417 int status;
1418
1419 aac_fib_init(fibctx);
1420
1421 cmd = (struct aac_pause *) fib_data(fibctx);
1422
1423 cmd->command = cpu_to_le32(VM_ContainerConfig);
1424 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1425 cmd->timeout = cpu_to_le32(1);
1426 cmd->min = cpu_to_le32(1);
1427 cmd->noRescan = cpu_to_le32(1);
1428 cmd->count = cpu_to_le32(0);
1429
1430 status = aac_fib_send(ContainerCommand,
1431 fibctx,
1432 sizeof(struct aac_pause),
1433 FsaNormal,
1434 -2 /* Timeout silently */, 1,
1435 NULL, NULL);
1436
1437 if (status >= 0)
1438 aac_fib_complete(fibctx);
cacb6dc3
PNRCEH
1439 /* FIB should be freed only after getting
1440 * the response from the F/W */
1441 if (status != -ERESTARTSYS)
1442 aac_fib_free(fibctx);
29c97684
SM
1443 }
1444 }
1445
8c867b25
MH
1446 return retval;
1447}
1448
1449int aac_check_health(struct aac_dev * aac)
1450{
1451 int BlinkLED;
1452 unsigned long time_now, flagv = 0;
1453 struct list_head * entry;
1454 struct Scsi_Host * host;
1455
1456 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1457 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1458 return 0;
1459
1460 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1461 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1462 return 0; /* OK */
1463 }
1464
1465 aac->in_reset = 1;
1466
1467 /* Fake up an AIF:
1468 * aac_aifcmd.command = AifCmdEventNotify = 1
1469 * aac_aifcmd.seqnum = 0xFFFFFFFF
1470 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1471 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1472 * aac.aifcmd.data[2] = AifHighPriority = 3
1473 * aac.aifcmd.data[3] = BlinkLED
1474 */
1475
1476 time_now = jiffies/HZ;
1477 entry = aac->fib_list.next;
1478
1479 /*
1480 * For each Context that is on the
1481 * fibctxList, make a copy of the
1482 * fib, and then set the event to wake up the
1483 * thread that is waiting for it.
1484 */
1485 while (entry != &aac->fib_list) {
1486 /*
1487 * Extract the fibctx
1488 */
1489 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1490 struct hw_fib * hw_fib;
1491 struct fib * fib;
1492 /*
1493 * Check if the queue is getting
1494 * backlogged
1495 */
1496 if (fibctx->count > 20) {
1497 /*
1498 * It's *not* jiffies folks,
1499 * but jiffies / HZ, so do not
1500 * panic ...
1501 */
1502 u32 time_last = fibctx->jiffies;
1503 /*
1504 * Has it been > 2 minutes
1505 * since the last read off
1506 * the queue?
1507 */
1508 if ((time_now - time_last) > aif_timeout) {
1509 entry = entry->next;
1510 aac_close_fib_context(aac, fibctx);
1511 continue;
1512 }
1513 }
1514 /*
1515 * Warning: no sleep allowed while
1516 * holding spinlock
1517 */
4dbc22d7
SM
1518 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1519 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
8c867b25
MH
1520 if (fib && hw_fib) {
1521 struct aac_aifcmd * aif;
1522
a8166a52 1523 fib->hw_fib_va = hw_fib;
8c867b25
MH
1524 fib->dev = aac;
1525 aac_fib_init(fib);
1526 fib->type = FSAFS_NTC_FIB_CONTEXT;
1527 fib->size = sizeof (struct fib);
1528 fib->data = hw_fib->data;
1529 aif = (struct aac_aifcmd *)hw_fib->data;
1530 aif->command = cpu_to_le32(AifCmdEventNotify);
a3940da5
SM
1531 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1532 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1533 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1534 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1535 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
8c867b25
MH
1536
1537 /*
1538 * Put the FIB onto the
1539 * fibctx's fibs
1540 */
1541 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1542 fibctx->count++;
1543 /*
1544 * Set the event to wake up the
1545 * thread that will waiting.
1546 */
1547 up(&fibctx->wait_sem);
1548 } else {
1549 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1550 kfree(fib);
1551 kfree(hw_fib);
1552 }
1553 entry = entry->next;
1554 }
1555
1556 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1557
1558 if (BlinkLED < 0) {
1559 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1560 goto out;
1561 }
1562
1563 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1564
2f7ecc55 1565 if (!aac_check_reset || ((aac_check_reset == 1) &&
a3940da5
SM
1566 (aac->supplement_adapter_info.SupportedOptions2 &
1567 AAC_OPTION_IGNORE_RESET)))
29c97684 1568 goto out;
8c867b25 1569 host = aac->scsi_host_ptr;
29c97684
SM
1570 if (aac->thread->pid != current->pid)
1571 spin_lock_irqsave(host->host_lock, flagv);
f858317d 1572 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
29c97684
SM
1573 if (aac->thread->pid != current->pid)
1574 spin_unlock_irqrestore(host->host_lock, flagv);
8c867b25
MH
1575 return BlinkLED;
1576
1577out:
1578 aac->in_reset = 0;
1579 return BlinkLED;
1580}
1581
1582
1da177e4
LT
1583/**
1584 * aac_command_thread - command processing thread
1585 * @dev: Adapter to monitor
1586 *
1587 * Waits on the commandready event in it's queue. When the event gets set
1588 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1589 * until the queue is empty. When the queue is empty it will wait for
1590 * more FIBs.
1591 */
8ce3eca4 1592
fe27381d 1593int aac_command_thread(void *data)
1da177e4 1594{
fe27381d 1595 struct aac_dev *dev = data;
1da177e4
LT
1596 struct hw_fib *hw_fib, *hw_newfib;
1597 struct fib *fib, *newfib;
1da177e4
LT
1598 struct aac_fib_context *fibctx;
1599 unsigned long flags;
1600 DECLARE_WAITQUEUE(wait, current);
29c97684
SM
1601 unsigned long next_jiffies = jiffies + HZ;
1602 unsigned long next_check_jiffies = next_jiffies;
1603 long difference = HZ;
1da177e4
LT
1604
1605 /*
1606 * We can only have one thread per adapter for AIF's.
1607 */
1608 if (dev->aif_thread)
1609 return -EINVAL;
fe27381d 1610
1da177e4
LT
1611 /*
1612 * Let the DPC know it has a place to send the AIF's to.
1613 */
1614 dev->aif_thread = 1;
2f130980 1615 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1da177e4 1616 set_current_state(TASK_INTERRUPTIBLE);
2f130980 1617 dprintk ((KERN_INFO "aac_command_thread start\n"));
8ce3eca4 1618 while (1) {
2f130980
MH
1619 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1620 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1da177e4
LT
1621 struct list_head *entry;
1622 struct aac_aifcmd * aifcmd;
1623
1624 set_current_state(TASK_RUNNING);
8ce3eca4 1625
2f130980 1626 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1da177e4 1627 list_del(entry);
8ce3eca4 1628
2f130980 1629 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1da177e4
LT
1630 fib = list_entry(entry, struct fib, fiblink);
1631 /*
8ce3eca4
SM
1632 * We will process the FIB here or pass it to a
1633 * worker thread that is TBD. We Really can't
1da177e4
LT
1634 * do anything at this point since we don't have
1635 * anything defined for this thread to do.
1636 */
a8166a52 1637 hw_fib = fib->hw_fib_va;
1da177e4
LT
1638 memset(fib, 0, sizeof(struct fib));
1639 fib->type = FSAFS_NTC_FIB_CONTEXT;
8ce3eca4 1640 fib->size = sizeof(struct fib);
a8166a52 1641 fib->hw_fib_va = hw_fib;
1da177e4
LT
1642 fib->data = hw_fib->data;
1643 fib->dev = dev;
1644 /*
1645 * We only handle AifRequest fibs from the adapter.
1646 */
1647 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1648 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1649 /* Handle Driver Notify Events */
131256cf 1650 aac_handle_aif(dev, fib);
56b58712 1651 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
bfb35aa8 1652 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1da177e4 1653 } else {
1da177e4
LT
1654 /* The u32 here is important and intended. We are using
1655 32bit wrapping time to fit the adapter field */
8ce3eca4 1656
1da177e4
LT
1657 u32 time_now, time_last;
1658 unsigned long flagv;
2f130980
MH
1659 unsigned num;
1660 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1661 struct fib ** fib_pool, ** fib_p;
8ce3eca4 1662
131256cf 1663 /* Sniff events */
8ce3eca4 1664 if ((aifcmd->command ==
131256cf 1665 cpu_to_le32(AifCmdEventNotify)) ||
8ce3eca4 1666 (aifcmd->command ==
131256cf
MH
1667 cpu_to_le32(AifCmdJobProgress))) {
1668 aac_handle_aif(dev, fib);
1669 }
29c97684 1670
1da177e4
LT
1671 time_now = jiffies/HZ;
1672
2f130980
MH
1673 /*
1674 * Warning: no sleep allowed while
1675 * holding spinlock. We take the estimate
1676 * and pre-allocate a set of fibs outside the
1677 * lock.
1678 */
1679 num = le32_to_cpu(dev->init->AdapterFibsSize)
1680 / sizeof(struct hw_fib); /* some extra */
1681 spin_lock_irqsave(&dev->fib_lock, flagv);
1682 entry = dev->fib_list.next;
1683 while (entry != &dev->fib_list) {
1684 entry = entry->next;
1685 ++num;
1686 }
1687 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1688 hw_fib_pool = NULL;
1689 fib_pool = NULL;
1690 if (num
1691 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1692 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1693 hw_fib_p = hw_fib_pool;
1694 fib_p = fib_pool;
1695 while (hw_fib_p < &hw_fib_pool[num]) {
1696 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1697 --hw_fib_p;
1698 break;
1699 }
1700 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1701 kfree(*(--hw_fib_p));
1702 break;
1703 }
1704 }
1705 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1706 kfree(fib_pool);
1707 fib_pool = NULL;
1708 kfree(hw_fib_pool);
1709 hw_fib_pool = NULL;
1710 }
c9475cb0 1711 } else {
2f130980
MH
1712 kfree(hw_fib_pool);
1713 hw_fib_pool = NULL;
1714 }
1da177e4
LT
1715 spin_lock_irqsave(&dev->fib_lock, flagv);
1716 entry = dev->fib_list.next;
1717 /*
8ce3eca4 1718 * For each Context that is on the
1da177e4
LT
1719 * fibctxList, make a copy of the
1720 * fib, and then set the event to wake up the
1721 * thread that is waiting for it.
1722 */
2f130980
MH
1723 hw_fib_p = hw_fib_pool;
1724 fib_p = fib_pool;
1da177e4
LT
1725 while (entry != &dev->fib_list) {
1726 /*
1727 * Extract the fibctx
1728 */
1729 fibctx = list_entry(entry, struct aac_fib_context, next);
1730 /*
1731 * Check if the queue is getting
1732 * backlogged
1733 */
1734 if (fibctx->count > 20)
1735 {
1736 /*
1737 * It's *not* jiffies folks,
1738 * but jiffies / HZ so do not
1739 * panic ...
1740 */
1741 time_last = fibctx->jiffies;
1742 /*
8ce3eca4 1743 * Has it been > 2 minutes
1da177e4
LT
1744 * since the last read off
1745 * the queue?
1746 */
404d9a90 1747 if ((time_now - time_last) > aif_timeout) {
1da177e4
LT
1748 entry = entry->next;
1749 aac_close_fib_context(dev, fibctx);
1750 continue;
1751 }
1752 }
1753 /*
1754 * Warning: no sleep allowed while
1755 * holding spinlock
1756 */
2f130980
MH
1757 if (hw_fib_p < &hw_fib_pool[num]) {
1758 hw_newfib = *hw_fib_p;
1759 *(hw_fib_p++) = NULL;
1760 newfib = *fib_p;
1761 *(fib_p++) = NULL;
1da177e4
LT
1762 /*
1763 * Make the copy of the FIB
1764 */
1765 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1766 memcpy(newfib, fib, sizeof(struct fib));
a8166a52 1767 newfib->hw_fib_va = hw_newfib;
1da177e4
LT
1768 /*
1769 * Put the FIB onto the
1770 * fibctx's fibs
1771 */
1772 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1773 fibctx->count++;
8ce3eca4 1774 /*
1da177e4 1775 * Set the event to wake up the
2f130980 1776 * thread that is waiting.
1da177e4
LT
1777 */
1778 up(&fibctx->wait_sem);
1779 } else {
1780 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1da177e4
LT
1781 }
1782 entry = entry->next;
1783 }
1784 /*
1785 * Set the status of this FIB
1786 */
56b58712 1787 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
bfb35aa8 1788 aac_fib_adapter_complete(fib, sizeof(u32));
1da177e4 1789 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2f130980
MH
1790 /* Free up the remaining resources */
1791 hw_fib_p = hw_fib_pool;
1792 fib_p = fib_pool;
1793 while (hw_fib_p < &hw_fib_pool[num]) {
c9475cb0
JJ
1794 kfree(*hw_fib_p);
1795 kfree(*fib_p);
2f130980
MH
1796 ++fib_p;
1797 ++hw_fib_p;
1798 }
c9475cb0
JJ
1799 kfree(hw_fib_pool);
1800 kfree(fib_pool);
1da177e4 1801 }
1da177e4 1802 kfree(fib);
2f130980 1803 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1da177e4
LT
1804 }
1805 /*
1806 * There are no more AIF's
1807 */
2f130980 1808 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
29c97684
SM
1809
1810 /*
1811 * Background activity
1812 */
1813 if ((time_before(next_check_jiffies,next_jiffies))
1814 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1815 next_check_jiffies = next_jiffies;
1816 if (aac_check_health(dev) == 0) {
1817 difference = ((long)(unsigned)check_interval)
1818 * HZ;
1819 next_check_jiffies = jiffies + difference;
1820 } else if (!dev->queues)
1821 break;
1822 }
1823 if (!time_before(next_check_jiffies,next_jiffies)
1824 && ((difference = next_jiffies - jiffies) <= 0)) {
1825 struct timeval now;
1826 int ret;
1827
1828 /* Don't even try to talk to adapter if its sick */
1829 ret = aac_check_health(dev);
1830 if (!ret && !dev->queues)
1831 break;
1832 next_check_jiffies = jiffies
1833 + ((long)(unsigned)check_interval)
1834 * HZ;
1835 do_gettimeofday(&now);
1836
1837 /* Synchronize our watches */
1838 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1839 && (now.tv_usec > (1000000 / HZ)))
1840 difference = (((1000000 - now.tv_usec) * HZ)
1841 + 500000) / 1000000;
1842 else if (ret == 0) {
1843 struct fib *fibptr;
1844
1845 if ((fibptr = aac_fib_alloc(dev))) {
cacb6dc3 1846 int status;
f3307f72 1847 __le32 *info;
29c97684
SM
1848
1849 aac_fib_init(fibptr);
1850
f3307f72 1851 info = (__le32 *) fib_data(fibptr);
29c97684
SM
1852 if (now.tv_usec > 500000)
1853 ++now.tv_sec;
1854
1855 *info = cpu_to_le32(now.tv_sec);
1856
cacb6dc3 1857 status = aac_fib_send(SendHostTime,
29c97684
SM
1858 fibptr,
1859 sizeof(*info),
1860 FsaNormal,
1861 1, 1,
1862 NULL,
1863 NULL);
cacb6dc3
PNRCEH
1864 /* Do not set XferState to zero unless
1865 * receives a response from F/W */
1866 if (status >= 0)
1867 aac_fib_complete(fibptr);
1868 /* FIB should be freed only after
1869 * getting the response from the F/W */
1870 if (status != -ERESTARTSYS)
1871 aac_fib_free(fibptr);
29c97684
SM
1872 }
1873 difference = (long)(unsigned)update_interval*HZ;
1874 } else {
1875 /* retry shortly */
1876 difference = 10 * HZ;
1877 }
1878 next_jiffies = jiffies + difference;
1879 if (time_before(next_check_jiffies,next_jiffies))
1880 difference = next_check_jiffies - jiffies;
1881 }
1882 if (difference <= 0)
1883 difference = 1;
1884 set_current_state(TASK_INTERRUPTIBLE);
1885 schedule_timeout(difference);
1da177e4 1886
fe27381d 1887 if (kthread_should_stop())
1da177e4 1888 break;
1da177e4 1889 }
2f130980
MH
1890 if (dev->queues)
1891 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1da177e4 1892 dev->aif_thread = 0;
2f130980 1893 return 0;
1da177e4 1894}