]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/aacraid/commsup.c
aacraid: 4KB sector support
[mirror_ubuntu-artful-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 138 {
85d22bbf 139 fibptr->flags = 0;
1da177e4 140 fibptr->dev = dev;
a8166a52
MH
141 fibptr->hw_fib_va = hw_fib;
142 fibptr->data = (void *) fibptr->hw_fib_va->data;
1da177e4 143 fibptr->next = fibptr+1; /* Forward chain the fibs */
6de76cfc 144 sema_init(&fibptr->event_wait, 0);
1da177e4 145 spin_lock_init(&fibptr->event_lock);
a8166a52
MH
146 hw_fib->header.XferState = cpu_to_le32(0xffffffff);
147 hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
1da177e4 148 fibptr->hw_fib_pa = hw_fib_pa;
e8b12f0f
MR
149 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
150 dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
151 hw_fib_pa = hw_fib_pa +
152 dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
1da177e4
LT
153 }
154 /*
155 * Add the fib chain to the free list
156 */
7c00ffa3 157 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
1da177e4
LT
158 /*
159 * Enable this to debug out of queue space
160 */
161 dev->free_fib = &dev->fibs[0];
162 return 0;
163}
164
165/**
bfb35aa8 166 * aac_fib_alloc - allocate a fib
1da177e4
LT
167 * @dev: Adapter to allocate the fib for
168 *
169 * Allocate a fib from the adapter fib pool. If the pool is empty we
7c00ffa3 170 * return NULL.
1da177e4 171 */
8ce3eca4 172
bfb35aa8 173struct fib *aac_fib_alloc(struct aac_dev *dev)
1da177e4
LT
174{
175 struct fib * fibptr;
176 unsigned long flags;
177 spin_lock_irqsave(&dev->fib_lock, flags);
8ce3eca4 178 fibptr = dev->free_fib;
7c00ffa3
MH
179 if(!fibptr){
180 spin_unlock_irqrestore(&dev->fib_lock, flags);
181 return fibptr;
182 }
1da177e4
LT
183 dev->free_fib = fibptr->next;
184 spin_unlock_irqrestore(&dev->fib_lock, flags);
185 /*
186 * Set the proper node type code and node byte size
187 */
188 fibptr->type = FSAFS_NTC_FIB_CONTEXT;
189 fibptr->size = sizeof(struct fib);
190 /*
191 * Null out fields that depend on being zero at the start of
192 * each I/O
193 */
a8166a52 194 fibptr->hw_fib_va->header.XferState = 0;
b6ef70f3 195 fibptr->flags = 0;
1da177e4
LT
196 fibptr->callback = NULL;
197 fibptr->callback_data = NULL;
198
199 return fibptr;
200}
201
202/**
bfb35aa8 203 * aac_fib_free - free a fib
1da177e4
LT
204 * @fibptr: fib to free up
205 *
206 * Frees up a fib and places it on the appropriate queue
1da177e4 207 */
8ce3eca4 208
bfb35aa8 209void aac_fib_free(struct fib *fibptr)
1da177e4 210{
cacb6dc3
PNRCEH
211 unsigned long flags, flagsv;
212
213 spin_lock_irqsave(&fibptr->event_lock, flagsv);
214 if (fibptr->done == 2) {
215 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
216 return;
217 }
218 spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
1da177e4
LT
219
220 spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
03d44337 221 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
1da177e4 222 aac_config.fib_timeouts++;
03d44337
MH
223 if (fibptr->hw_fib_va->header.XferState != 0) {
224 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
225 (void*)fibptr,
226 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
227 }
228 fibptr->next = fibptr->dev->free_fib;
229 fibptr->dev->free_fib = fibptr;
1da177e4
LT
230 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
231}
232
233/**
bfb35aa8 234 * aac_fib_init - initialise a fib
1da177e4 235 * @fibptr: The fib to initialize
8ce3eca4 236 *
1da177e4
LT
237 * Set up the generic fib fields ready for use
238 */
8ce3eca4 239
bfb35aa8 240void aac_fib_init(struct fib *fibptr)
1da177e4 241{
a8166a52 242 struct hw_fib *hw_fib = fibptr->hw_fib_va;
1da177e4 243
85d22bbf 244 memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
1da177e4 245 hw_fib->header.StructType = FIB_MAGIC;
7c00ffa3
MH
246 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
247 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
85d22bbf 248 hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
7c00ffa3 249 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
1da177e4
LT
250}
251
252/**
253 * fib_deallocate - deallocate a fib
254 * @fibptr: fib to deallocate
255 *
256 * Will deallocate and return to the free pool the FIB pointed to by the
257 * caller.
258 */
8ce3eca4 259
4833869e 260static void fib_dealloc(struct fib * fibptr)
1da177e4 261{
a8166a52 262 struct hw_fib *hw_fib = fibptr->hw_fib_va;
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 */
85d22bbf 373 hw_fib->header.u.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 418 unsigned long mflags = 0;
11604612 419 unsigned long sflags = 0;
cacb6dc3 420
1640a2c3 421
1da177e4
LT
422 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
423 return -EBUSY;
424 /*
25985edc 425 * There are 5 cases with the wait and response requested flags.
1da177e4
LT
426 * The only invalid cases are if the caller requests to wait and
427 * does not request a response and if the caller does not want a
428 * response and the Fib is not allocated from pool. If a response
429 * is not requesed the Fib will just be deallocaed by the DPC
430 * routine when the response comes back from the adapter. No
8ce3eca4 431 * further processing will be done besides deleting the Fib. We
1da177e4
LT
432 * will have a debug mode where the adapter can notify the host
433 * it had a problem and the host can log that fact.
434 */
b6ef70f3 435 fibptr->flags = 0;
1da177e4
LT
436 if (wait && !reply) {
437 return -EINVAL;
438 } else if (!wait && reply) {
439 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
440 FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
441 } else if (!wait && !reply) {
442 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
443 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
444 } else if (wait && reply) {
445 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
446 FIB_COUNTER_INCREMENT(aac_config.NormalSent);
8ce3eca4 447 }
1da177e4
LT
448 /*
449 * Map the fib into 32bits by using the fib number
450 */
451
8e0c5ebd 452 hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
85d22bbf 453 hw_fib->header.Handle = (u32)(fibptr - dev->fibs) + 1;
1da177e4
LT
454 /*
455 * Set FIB state to indicate where it came from and if we want a
456 * response from the adapter. Also load the command from the
457 * caller.
458 *
459 * Map the hw fib pointer as a 32bit value
460 */
461 hw_fib->header.Command = cpu_to_le16(command);
462 hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
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
11604612
MR
515 if (dev->sync_mode) {
516 if (wait)
517 spin_unlock_irqrestore(&fibptr->event_lock, flags);
518 spin_lock_irqsave(&dev->sync_lock, sflags);
519 if (dev->sync_fib) {
520 list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
521 spin_unlock_irqrestore(&dev->sync_lock, sflags);
522 } else {
523 dev->sync_fib = fibptr;
524 spin_unlock_irqrestore(&dev->sync_lock, sflags);
525 aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
526 (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
527 NULL, NULL, NULL, NULL, NULL);
528 }
529 if (wait) {
530 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
531 if (down_interruptible(&fibptr->event_wait)) {
532 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
533 return -EFAULT;
534 }
535 return 0;
536 }
537 return -EINPROGRESS;
538 }
539
cacb6dc3
PNRCEH
540 if (aac_adapter_deliver(fibptr) != 0) {
541 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
542 if (wait) {
543 spin_unlock_irqrestore(&fibptr->event_lock, flags);
544 spin_lock_irqsave(&dev->manage_lock, mflags);
545 dev->management_fib_count--;
546 spin_unlock_irqrestore(&dev->manage_lock, mflags);
547 }
548 return -EBUSY;
549 }
550
8e0c5ebd 551
1da177e4 552 /*
8ce3eca4 553 * If the caller wanted us to wait for response wait now.
1da177e4 554 */
8ce3eca4 555
1da177e4
LT
556 if (wait) {
557 spin_unlock_irqrestore(&fibptr->event_lock, flags);
9203344c
MH
558 /* Only set for first known interruptable command */
559 if (wait < 0) {
560 /*
561 * *VERY* Dangerous to time out a command, the
562 * assumption is made that we have no hope of
563 * functioning because an interrupt routing or other
564 * hardware failure has occurred.
565 */
30002f1c 566 unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
9203344c 567 while (down_trylock(&fibptr->event_wait)) {
33524b70 568 int blink;
30002f1c 569 if (time_is_before_eq_jiffies(timeout)) {
28713324 570 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
9203344c
MH
571 spin_lock_irqsave(q->lock, qflags);
572 q->numpending--;
9203344c
MH
573 spin_unlock_irqrestore(q->lock, qflags);
574 if (wait == -1) {
bfb35aa8 575 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
9203344c
MH
576 "Usually a result of a PCI interrupt routing problem;\n"
577 "update mother board BIOS or consider utilizing one of\n"
578 "the SAFE mode kernel options (acpi, apic etc)\n");
579 }
580 return -ETIMEDOUT;
581 }
33524b70
MH
582 if ((blink = aac_adapter_check_health(dev)) > 0) {
583 if (wait == -1) {
584 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
585 "Usually a result of a serious unrecoverable hardware problem\n",
586 blink);
587 }
588 return -EFAULT;
589 }
30002f1c
BC
590 /* We used to udelay() here but that absorbed
591 * a CPU when a timeout occured. Not very
592 * useful. */
593 cpu_relax();
9203344c 594 }
0462590e 595 } else if (down_interruptible(&fibptr->event_wait)) {
cacb6dc3
PNRCEH
596 /* Do nothing ... satisfy
597 * down_interruptible must_check */
e6990c64 598 }
cacb6dc3 599
33bb3b29 600 spin_lock_irqsave(&fibptr->event_lock, flags);
cacb6dc3 601 if (fibptr->done == 0) {
33bb3b29 602 fibptr->done = 2; /* Tell interrupt we aborted */
c8f7b073 603 spin_unlock_irqrestore(&fibptr->event_lock, flags);
cacb6dc3 604 return -ERESTARTSYS;
c8f7b073 605 }
33bb3b29 606 spin_unlock_irqrestore(&fibptr->event_lock, flags);
125e1874 607 BUG_ON(fibptr->done == 0);
8ce3eca4 608
912d4e88 609 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
1da177e4 610 return -ETIMEDOUT;
912d4e88 611 return 0;
1da177e4
LT
612 }
613 /*
614 * If the user does not want a response than return success otherwise
615 * return pending
616 */
617 if (reply)
618 return -EINPROGRESS;
619 else
620 return 0;
621}
622
8ce3eca4 623/**
1da177e4
LT
624 * aac_consumer_get - get the top of the queue
625 * @dev: Adapter
626 * @q: Queue
627 * @entry: Return entry
628 *
629 * Will return a pointer to the entry on the top of the queue requested that
8ce3eca4
SM
630 * we are a consumer of, and return the address of the queue entry. It does
631 * not change the state of the queue.
1da177e4
LT
632 */
633
634int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
635{
636 u32 index;
637 int status;
638 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
639 status = 0;
640 } else {
641 /*
642 * The consumer index must be wrapped if we have reached
643 * the end of the queue, else we just use the entry
644 * pointed to by the header index
645 */
8ce3eca4
SM
646 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
647 index = 0;
1da177e4 648 else
8ce3eca4 649 index = le32_to_cpu(*q->headers.consumer);
1da177e4
LT
650 *entry = q->base + index;
651 status = 1;
652 }
653 return(status);
654}
655
656/**
657 * aac_consumer_free - free consumer entry
658 * @dev: Adapter
659 * @q: Queue
660 * @qid: Queue ident
661 *
662 * Frees up the current top of the queue we are a consumer of. If the
663 * queue was full notify the producer that the queue is no longer full.
664 */
665
666void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
667{
668 int wasfull = 0;
669 u32 notify;
670
671 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
672 wasfull = 1;
8ce3eca4 673
1da177e4
LT
674 if (le32_to_cpu(*q->headers.consumer) >= q->entries)
675 *q->headers.consumer = cpu_to_le32(1);
676 else
36b8dd1b 677 le32_add_cpu(q->headers.consumer, 1);
8ce3eca4 678
1da177e4
LT
679 if (wasfull) {
680 switch (qid) {
681
682 case HostNormCmdQueue:
683 notify = HostNormCmdNotFull;
684 break;
1da177e4
LT
685 case HostNormRespQueue:
686 notify = HostNormRespNotFull;
687 break;
1da177e4
LT
688 default:
689 BUG();
690 return;
691 }
692 aac_adapter_notify(dev, notify);
693 }
8ce3eca4 694}
1da177e4
LT
695
696/**
bfb35aa8 697 * aac_fib_adapter_complete - complete adapter issued fib
1da177e4
LT
698 * @fibptr: fib to complete
699 * @size: size of fib
700 *
701 * Will do all necessary work to complete a FIB that was sent from
702 * the adapter.
703 */
704
bfb35aa8 705int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
1da177e4 706{
a8166a52 707 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1da177e4 708 struct aac_dev * dev = fibptr->dev;
1640a2c3 709 struct aac_queue * q;
1da177e4 710 unsigned long nointr = 0;
1640a2c3
MH
711 unsigned long qflags;
712
85d22bbf
MR
713 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
714 dev->comm_interface == AAC_COMM_MESSAGE_TYPE2) {
e8b12f0f
MR
715 kfree(hw_fib);
716 return 0;
717 }
718
1640a2c3 719 if (hw_fib->header.XferState == 0) {
28713324 720 if (dev->comm_interface == AAC_COMM_MESSAGE)
e8b12f0f 721 kfree(hw_fib);
8ce3eca4 722 return 0;
1640a2c3 723 }
1da177e4
LT
724 /*
725 * If we plan to do anything check the structure type first.
8ce3eca4 726 */
85d22bbf
MR
727 if (hw_fib->header.StructType != FIB_MAGIC &&
728 hw_fib->header.StructType != FIB_MAGIC2 &&
729 hw_fib->header.StructType != FIB_MAGIC2_64) {
28713324 730 if (dev->comm_interface == AAC_COMM_MESSAGE)
e8b12f0f 731 kfree(hw_fib);
8ce3eca4 732 return -EINVAL;
1da177e4
LT
733 }
734 /*
735 * This block handles the case where the adapter had sent us a
736 * command and we have finished processing the command. We
8ce3eca4
SM
737 * call completeFib when we are done processing the command
738 * and want to send a response back to the adapter. This will
1da177e4
LT
739 * send the completed cdb to the adapter.
740 */
741 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
28713324 742 if (dev->comm_interface == AAC_COMM_MESSAGE) {
8e0c5ebd
MH
743 kfree (hw_fib);
744 } else {
8ce3eca4
SM
745 u32 index;
746 hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
8e0c5ebd
MH
747 if (size) {
748 size += sizeof(struct aac_fibhdr);
8ce3eca4 749 if (size > le16_to_cpu(hw_fib->header.SenderSize))
8e0c5ebd
MH
750 return -EMSGSIZE;
751 hw_fib->header.Size = cpu_to_le16(size);
752 }
753 q = &dev->queues->queue[AdapNormRespQueue];
754 spin_lock_irqsave(q->lock, qflags);
755 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
756 *(q->headers.producer) = cpu_to_le32(index + 1);
757 spin_unlock_irqrestore(q->lock, qflags);
758 if (!(nointr & (int)aac_config.irq_mod))
759 aac_adapter_notify(dev, AdapNormRespQueue);
1da177e4 760 }
8ce3eca4
SM
761 } else {
762 printk(KERN_WARNING "aac_fib_adapter_complete: "
763 "Unknown xferstate detected.\n");
764 BUG();
1da177e4 765 }
1da177e4
LT
766 return 0;
767}
768
769/**
bfb35aa8 770 * aac_fib_complete - fib completion handler
1da177e4
LT
771 * @fib: FIB to complete
772 *
773 * Will do all necessary work to complete a FIB.
774 */
8ce3eca4 775
bfb35aa8 776int aac_fib_complete(struct fib *fibptr)
1da177e4 777{
cacb6dc3 778 unsigned long flags;
a8166a52 779 struct hw_fib * hw_fib = fibptr->hw_fib_va;
1da177e4
LT
780
781 /*
782 * Check for a fib which has already been completed
783 */
784
785 if (hw_fib->header.XferState == 0)
8ce3eca4 786 return 0;
1da177e4
LT
787 /*
788 * If we plan to do anything check the structure type first.
8ce3eca4 789 */
1da177e4 790
85d22bbf
MR
791 if (hw_fib->header.StructType != FIB_MAGIC &&
792 hw_fib->header.StructType != FIB_MAGIC2 &&
793 hw_fib->header.StructType != FIB_MAGIC2_64)
8ce3eca4 794 return -EINVAL;
1da177e4 795 /*
8ce3eca4 796 * This block completes a cdb which orginated on the host and we
1da177e4
LT
797 * just need to deallocate the cdb or reinit it. At this point the
798 * command is complete that we had sent to the adapter and this
799 * cdb could be reused.
800 */
cacb6dc3
PNRCEH
801 spin_lock_irqsave(&fibptr->event_lock, flags);
802 if (fibptr->done == 2) {
803 spin_unlock_irqrestore(&fibptr->event_lock, flags);
804 return 0;
805 }
806 spin_unlock_irqrestore(&fibptr->event_lock, flags);
807
1da177e4
LT
808 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
809 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
810 {
811 fib_dealloc(fibptr);
812 }
813 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
814 {
815 /*
816 * This handles the case when the host has aborted the I/O
817 * to the adapter because the adapter is not responding
818 */
819 fib_dealloc(fibptr);
820 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
821 fib_dealloc(fibptr);
822 } else {
823 BUG();
8ce3eca4 824 }
1da177e4
LT
825 return 0;
826}
827
828/**
829 * aac_printf - handle printf from firmware
830 * @dev: Adapter
831 * @val: Message info
832 *
833 * Print a message passed to us by the controller firmware on the
834 * Adaptec board
835 */
836
837void aac_printf(struct aac_dev *dev, u32 val)
838{
1da177e4 839 char *cp = dev->printfbuf;
7c00ffa3
MH
840 if (dev->printf_enabled)
841 {
842 int length = val & 0xffff;
843 int level = (val >> 16) & 0xffff;
8ce3eca4 844
7c00ffa3
MH
845 /*
846 * The size of the printfbuf is set in port.c
847 * There is no variable or define for it
848 */
849 if (length > 255)
850 length = 255;
851 if (cp[length] != 0)
852 cp[length] = 0;
853 if (level == LOG_AAC_HIGH_ERROR)
1241f359 854 printk(KERN_WARNING "%s:%s", dev->name, cp);
7c00ffa3 855 else
1241f359 856 printk(KERN_INFO "%s:%s", dev->name, cp);
7c00ffa3 857 }
8ce3eca4 858 memset(cp, 0, 256);
1da177e4
LT
859}
860
131256cf
MH
861
862/**
863 * aac_handle_aif - Handle a message from the firmware
864 * @dev: Which adapter this fib is from
865 * @fibptr: Pointer to fibptr from adapter
866 *
867 * This routine handles a driver notify fib from the adapter and
868 * dispatches it to the appropriate routine for handling.
869 */
870
31876f32 871#define AIF_SNIFF_TIMEOUT (30*HZ)
131256cf
MH
872static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
873{
a8166a52 874 struct hw_fib * hw_fib = fibptr->hw_fib_va;
131256cf 875 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
0995ad38 876 u32 channel, id, lun, container;
131256cf
MH
877 struct scsi_device *device;
878 enum {
879 NOTHING,
880 DELETE,
881 ADD,
882 CHANGE
0995ad38 883 } device_config_needed = NOTHING;
131256cf
MH
884
885 /* Sniff for container changes */
886
c8f7b073 887 if (!dev || !dev->fsa_dev)
131256cf 888 return;
0995ad38 889 container = channel = id = lun = (u32)-1;
131256cf
MH
890
891 /*
892 * We have set this up to try and minimize the number of
893 * re-configures that take place. As a result of this when
894 * certain AIF's come in we will set a flag waiting for another
895 * type of AIF before setting the re-config flag.
896 */
897 switch (le32_to_cpu(aifcmd->command)) {
898 case AifCmdDriverNotify:
f3307f72 899 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
131256cf
MH
900 /*
901 * Morph or Expand complete
902 */
903 case AifDenMorphComplete:
904 case AifDenVolumeExtendComplete:
f3307f72 905 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
906 if (container >= dev->maximum_num_containers)
907 break;
908
909 /*
f64a181d 910 * Find the scsi_device associated with the SCSI
131256cf
MH
911 * address. Make sure we have the right array, and if
912 * so set the flag to initiate a new re-config once we
913 * see an AifEnConfigChange AIF come through.
914 */
915
916 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
8ce3eca4
SM
917 device = scsi_device_lookup(dev->scsi_host_ptr,
918 CONTAINER_TO_CHANNEL(container),
919 CONTAINER_TO_ID(container),
131256cf
MH
920 CONTAINER_TO_LUN(container));
921 if (device) {
922 dev->fsa_dev[container].config_needed = CHANGE;
923 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
31876f32 924 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
925 scsi_device_put(device);
926 }
927 }
928 }
929
930 /*
931 * If we are waiting on something and this happens to be
932 * that thing then set the re-configure flag.
933 */
934 if (container != (u32)-1) {
935 if (container >= dev->maximum_num_containers)
936 break;
31876f32 937 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 938 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 939 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
940 dev->fsa_dev[container].config_waiting_on = 0;
941 } else for (container = 0;
942 container < dev->maximum_num_containers; ++container) {
31876f32 943 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 944 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 945 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
946 dev->fsa_dev[container].config_waiting_on = 0;
947 }
948 break;
949
950 case AifCmdEventNotify:
f3307f72 951 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
95e852e1
SM
952 case AifEnBatteryEvent:
953 dev->cache_protected =
954 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
955 break;
131256cf
MH
956 /*
957 * Add an Array.
958 */
959 case AifEnAddContainer:
f3307f72 960 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
961 if (container >= dev->maximum_num_containers)
962 break;
963 dev->fsa_dev[container].config_needed = ADD;
964 dev->fsa_dev[container].config_waiting_on =
965 AifEnConfigChange;
31876f32 966 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
967 break;
968
969 /*
970 * Delete an Array.
971 */
972 case AifEnDeleteContainer:
f3307f72 973 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
974 if (container >= dev->maximum_num_containers)
975 break;
976 dev->fsa_dev[container].config_needed = DELETE;
977 dev->fsa_dev[container].config_waiting_on =
978 AifEnConfigChange;
31876f32 979 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
980 break;
981
982 /*
983 * Container change detected. If we currently are not
984 * waiting on something else, setup to wait on a Config Change.
985 */
986 case AifEnContainerChange:
f3307f72 987 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
131256cf
MH
988 if (container >= dev->maximum_num_containers)
989 break;
31876f32
MH
990 if (dev->fsa_dev[container].config_waiting_on &&
991 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
992 break;
993 dev->fsa_dev[container].config_needed = CHANGE;
994 dev->fsa_dev[container].config_waiting_on =
995 AifEnConfigChange;
31876f32 996 dev->fsa_dev[container].config_waiting_stamp = jiffies;
131256cf
MH
997 break;
998
999 case AifEnConfigChange:
1000 break;
1001
cb1042f2
SM
1002 case AifEnAddJBOD:
1003 case AifEnDeleteJBOD:
1004 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
a4576b5d
MS
1005 if ((container >> 28)) {
1006 container = (u32)-1;
cb1042f2 1007 break;
a4576b5d 1008 }
cb1042f2 1009 channel = (container >> 24) & 0xF;
a4576b5d
MS
1010 if (channel >= dev->maximum_num_channels) {
1011 container = (u32)-1;
cb1042f2 1012 break;
a4576b5d 1013 }
cb1042f2 1014 id = container & 0xFFFF;
a4576b5d
MS
1015 if (id >= dev->maximum_num_physicals) {
1016 container = (u32)-1;
cb1042f2 1017 break;
a4576b5d 1018 }
cb1042f2 1019 lun = (container >> 16) & 0xFF;
a4576b5d 1020 container = (u32)-1;
cb1042f2
SM
1021 channel = aac_phys_to_logical(channel);
1022 device_config_needed =
1023 (((__le32 *)aifcmd->data)[0] ==
1024 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
5ca05594
RM
1025 if (device_config_needed == ADD) {
1026 device = scsi_device_lookup(dev->scsi_host_ptr,
1027 channel,
1028 id,
1029 lun);
1030 if (device) {
1031 scsi_remove_device(device);
1032 scsi_device_put(device);
1033 }
1034 }
cb1042f2
SM
1035 break;
1036
0995ad38 1037 case AifEnEnclosureManagement:
cb1042f2
SM
1038 /*
1039 * If in JBOD mode, automatic exposure of new
1040 * physical target to be suppressed until configured.
1041 */
1042 if (dev->jbod)
1043 break;
0995ad38
SM
1044 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
1045 case EM_DRIVE_INSERTION:
1046 case EM_DRIVE_REMOVAL:
46154a02
MR
1047 case EM_SES_DRIVE_INSERTION:
1048 case EM_SES_DRIVE_REMOVAL:
0995ad38
SM
1049 container = le32_to_cpu(
1050 ((__le32 *)aifcmd->data)[2]);
a4576b5d
MS
1051 if ((container >> 28)) {
1052 container = (u32)-1;
0995ad38 1053 break;
a4576b5d 1054 }
0995ad38 1055 channel = (container >> 24) & 0xF;
a4576b5d
MS
1056 if (channel >= dev->maximum_num_channels) {
1057 container = (u32)-1;
0995ad38 1058 break;
a4576b5d 1059 }
0995ad38
SM
1060 id = container & 0xFFFF;
1061 lun = (container >> 16) & 0xFF;
a4576b5d 1062 container = (u32)-1;
0995ad38
SM
1063 if (id >= dev->maximum_num_physicals) {
1064 /* legacy dev_t ? */
1065 if ((0x2000 <= id) || lun || channel ||
1066 ((channel = (id >> 7) & 0x3F) >=
1067 dev->maximum_num_channels))
1068 break;
1069 lun = (id >> 4) & 7;
1070 id &= 0xF;
1071 }
1072 channel = aac_phys_to_logical(channel);
1073 device_config_needed =
46154a02
MR
1074 ((((__le32 *)aifcmd->data)[3]
1075 == cpu_to_le32(EM_DRIVE_INSERTION)) ||
1076 (((__le32 *)aifcmd->data)[3]
1077 == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
0995ad38
SM
1078 ADD : DELETE;
1079 break;
1080 }
1081 break;
131256cf
MH
1082 }
1083
1084 /*
1085 * If we are waiting on something and this happens to be
1086 * that thing then set the re-configure flag.
1087 */
1088 if (container != (u32)-1) {
1089 if (container >= dev->maximum_num_containers)
1090 break;
31876f32 1091 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 1092 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 1093 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
1094 dev->fsa_dev[container].config_waiting_on = 0;
1095 } else for (container = 0;
1096 container < dev->maximum_num_containers; ++container) {
31876f32 1097 if ((dev->fsa_dev[container].config_waiting_on ==
f3307f72 1098 le32_to_cpu(*(__le32 *)aifcmd->data)) &&
31876f32 1099 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
131256cf
MH
1100 dev->fsa_dev[container].config_waiting_on = 0;
1101 }
1102 break;
1103
1104 case AifCmdJobProgress:
1105 /*
1106 * These are job progress AIF's. When a Clear is being
1107 * done on a container it is initially created then hidden from
1108 * the OS. When the clear completes we don't get a config
1109 * change so we monitor the job status complete on a clear then
1110 * wait for a container change.
1111 */
1112
f3307f72
CH
1113 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1114 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
1115 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
131256cf
MH
1116 for (container = 0;
1117 container < dev->maximum_num_containers;
1118 ++container) {
1119 /*
1120 * Stomp on all config sequencing for all
1121 * containers?
1122 */
1123 dev->fsa_dev[container].config_waiting_on =
1124 AifEnContainerChange;
1125 dev->fsa_dev[container].config_needed = ADD;
31876f32
MH
1126 dev->fsa_dev[container].config_waiting_stamp =
1127 jiffies;
131256cf
MH
1128 }
1129 }
f3307f72
CH
1130 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
1131 ((__le32 *)aifcmd->data)[6] == 0 &&
1132 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
131256cf
MH
1133 for (container = 0;
1134 container < dev->maximum_num_containers;
1135 ++container) {
1136 /*
1137 * Stomp on all config sequencing for all
1138 * containers?
1139 */
1140 dev->fsa_dev[container].config_waiting_on =
1141 AifEnContainerChange;
1142 dev->fsa_dev[container].config_needed = DELETE;
31876f32
MH
1143 dev->fsa_dev[container].config_waiting_stamp =
1144 jiffies;
131256cf
MH
1145 }
1146 }
1147 break;
1148 }
1149
a4576b5d
MS
1150 container = 0;
1151retry_next:
0995ad38 1152 if (device_config_needed == NOTHING)
a4576b5d 1153 for (; container < dev->maximum_num_containers; ++container) {
31876f32
MH
1154 if ((dev->fsa_dev[container].config_waiting_on == 0) &&
1155 (dev->fsa_dev[container].config_needed != NOTHING) &&
1156 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
131256cf
MH
1157 device_config_needed =
1158 dev->fsa_dev[container].config_needed;
1159 dev->fsa_dev[container].config_needed = NOTHING;
0995ad38
SM
1160 channel = CONTAINER_TO_CHANNEL(container);
1161 id = CONTAINER_TO_ID(container);
1162 lun = CONTAINER_TO_LUN(container);
131256cf
MH
1163 break;
1164 }
1165 }
1166 if (device_config_needed == NOTHING)
1167 return;
1168
1169 /*
1170 * If we decided that a re-configuration needs to be done,
1171 * schedule it here on the way out the door, please close the door
1172 * behind you.
1173 */
1174
131256cf 1175 /*
f64a181d 1176 * Find the scsi_device associated with the SCSI address,
131256cf
MH
1177 * and mark it as changed, invalidating the cache. This deals
1178 * with changes to existing device IDs.
1179 */
1180
1181 if (!dev || !dev->scsi_host_ptr)
1182 return;
1183 /*
bfb35aa8 1184 * force reload of disk info via aac_probe_container
131256cf 1185 */
0995ad38
SM
1186 if ((channel == CONTAINER_CHANNEL) &&
1187 (device_config_needed != NOTHING)) {
1188 if (dev->fsa_dev[container].valid == 1)
1189 dev->fsa_dev[container].valid = 2;
bfb35aa8 1190 aac_probe_container(dev, container);
0995ad38
SM
1191 }
1192 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
131256cf
MH
1193 if (device) {
1194 switch (device_config_needed) {
1195 case DELETE:
9cccde93
RM
1196#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1197 scsi_remove_device(device);
1198#else
0995ad38
SM
1199 if (scsi_device_online(device)) {
1200 scsi_device_set_state(device, SDEV_OFFLINE);
1201 sdev_printk(KERN_INFO, device,
1202 "Device offlined - %s\n",
1203 (channel == CONTAINER_CHANNEL) ?
1204 "array deleted" :
1205 "enclosure services event");
1206 }
9cccde93 1207#endif
0995ad38
SM
1208 break;
1209 case ADD:
1210 if (!scsi_device_online(device)) {
1211 sdev_printk(KERN_INFO, device,
1212 "Device online - %s\n",
1213 (channel == CONTAINER_CHANNEL) ?
1214 "array created" :
1215 "enclosure services event");
1216 scsi_device_set_state(device, SDEV_RUNNING);
1217 }
1218 /* FALLTHRU */
131256cf 1219 case CHANGE:
0995ad38
SM
1220 if ((channel == CONTAINER_CHANNEL)
1221 && (!dev->fsa_dev[container].valid)) {
9cccde93
RM
1222#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
1223 scsi_remove_device(device);
1224#else
0995ad38
SM
1225 if (!scsi_device_online(device))
1226 break;
1227 scsi_device_set_state(device, SDEV_OFFLINE);
1228 sdev_printk(KERN_INFO, device,
1229 "Device offlined - %s\n",
1230 "array failed");
9cccde93 1231#endif
0995ad38
SM
1232 break;
1233 }
131256cf
MH
1234 scsi_rescan_device(&device->sdev_gendev);
1235
1236 default:
1237 break;
1238 }
1239 scsi_device_put(device);
0995ad38 1240 device_config_needed = NOTHING;
131256cf 1241 }
0995ad38
SM
1242 if (device_config_needed == ADD)
1243 scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
a4576b5d
MS
1244 if (channel == CONTAINER_CHANNEL) {
1245 container++;
1246 device_config_needed = NOTHING;
1247 goto retry_next;
1248 }
131256cf
MH
1249}
1250
29c97684 1251static int _aac_reset_adapter(struct aac_dev *aac, int forced)
8c867b25
MH
1252{
1253 int index, quirks;
8c867b25
MH
1254 int retval;
1255 struct Scsi_Host *host;
1256 struct scsi_device *dev;
1257 struct scsi_cmnd *command;
1258 struct scsi_cmnd *command_list;
29c97684 1259 int jafo = 0;
8c867b25
MH
1260
1261 /*
1262 * Assumptions:
29c97684
SM
1263 * - host is locked, unless called by the aacraid thread.
1264 * (a matter of convenience, due to legacy issues surrounding
1265 * eh_host_adapter_reset).
8c867b25
MH
1266 * - in_reset is asserted, so no new i/o is getting to the
1267 * card.
29c97684
SM
1268 * - The card is dead, or will be very shortly ;-/ so no new
1269 * commands are completing in the interrupt service.
8c867b25
MH
1270 */
1271 host = aac->scsi_host_ptr;
1272 scsi_block_requests(host);
1273 aac_adapter_disable_int(aac);
29c97684
SM
1274 if (aac->thread->pid != current->pid) {
1275 spin_unlock_irq(host->host_lock);
1276 kthread_stop(aac->thread);
1277 jafo = 1;
1278 }
8c867b25
MH
1279
1280 /*
1281 * If a positive health, means in a known DEAD PANIC
1282 * state and the adapter could be reset to `try again'.
1283 */
29c97684 1284 retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
8c867b25
MH
1285
1286 if (retval)
1287 goto out;
8c867b25 1288
d18b448f
MH
1289 /*
1290 * Loop through the fibs, close the synchronous FIBS
1291 */
33bb3b29 1292 for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
d18b448f 1293 struct fib *fib = &aac->fibs[index];
a8166a52
MH
1294 if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
1295 (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
d18b448f
MH
1296 unsigned long flagv;
1297 spin_lock_irqsave(&fib->event_lock, flagv);
1298 up(&fib->event_wait);
1299 spin_unlock_irqrestore(&fib->event_lock, flagv);
1300 schedule();
33bb3b29 1301 retval = 0;
d18b448f
MH
1302 }
1303 }
33bb3b29
MH
1304 /* Give some extra time for ioctls to complete. */
1305 if (retval == 0)
1306 ssleep(2);
8c867b25
MH
1307 index = aac->cardtype;
1308
1309 /*
1310 * Re-initialize the adapter, first free resources, then carefully
1311 * apply the initialization sequence to come back again. Only risk
1312 * is a change in Firmware dropping cache, it is assumed the caller
1313 * will ensure that i/o is queisced and the card is flushed in that
1314 * case.
1315 */
1316 aac_fib_map_free(aac);
8c867b25
MH
1317 pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
1318 aac->comm_addr = NULL;
1319 aac->comm_phys = 0;
1320 kfree(aac->queues);
1321 aac->queues = NULL;
1322 free_irq(aac->pdev->irq, aac);
d0efab26
VA
1323 if (aac->msi)
1324 pci_disable_msi(aac->pdev);
8c867b25
MH
1325 kfree(aac->fsa_dev);
1326 aac->fsa_dev = NULL;
94cf6ba1
SM
1327 quirks = aac_get_driver_ident(index)->quirks;
1328 if (quirks & AAC_QUIRK_31BIT) {
929a22a5
YH
1329 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
1330 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
8c867b25
MH
1331 goto out;
1332 } else {
284901a9
YH
1333 if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
1334 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
8c867b25
MH
1335 goto out;
1336 }
1337 if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
1338 goto out;
94cf6ba1 1339 if (quirks & AAC_QUIRK_31BIT)
284901a9 1340 if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
8c867b25 1341 goto out;
29c97684 1342 if (jafo) {
f170168b
KC
1343 aac->thread = kthread_run(aac_command_thread, aac, "%s",
1344 aac->name);
29c97684
SM
1345 if (IS_ERR(aac->thread)) {
1346 retval = PTR_ERR(aac->thread);
1347 goto out;
1348 }
8c867b25
MH
1349 }
1350 (void)aac_get_adapter_info(aac);
8c867b25 1351 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
8ce3eca4
SM
1352 host->sg_tablesize = 34;
1353 host->max_sectors = (host->sg_tablesize * 8) + 112;
1354 }
1355 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
1356 host->sg_tablesize = 17;
1357 host->max_sectors = (host->sg_tablesize * 8) + 112;
1358 }
8c867b25
MH
1359 aac_get_config_status(aac, 1);
1360 aac_get_containers(aac);
1361 /*
1362 * This is where the assumption that the Adapter is quiesced
1363 * is important.
1364 */
1365 command_list = NULL;
1366 __shost_for_each_device(dev, host) {
1367 unsigned long flags;
1368 spin_lock_irqsave(&dev->list_lock, flags);
1369 list_for_each_entry(command, &dev->cmd_list, list)
1370 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1371 command->SCp.buffer = (struct scatterlist *)command_list;
1372 command_list = command;
1373 }
1374 spin_unlock_irqrestore(&dev->list_lock, flags);
1375 }
1376 while ((command = command_list)) {
1377 command_list = (struct scsi_cmnd *)command->SCp.buffer;
1378 command->SCp.buffer = NULL;
1379 command->result = DID_OK << 16
1380 | COMMAND_COMPLETE << 8
1381 | SAM_STAT_TASK_SET_FULL;
1382 command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
1383 command->scsi_done(command);
1384 }
1385 retval = 0;
1386
1387out:
1388 aac->in_reset = 0;
1389 scsi_unblock_requests(host);
29c97684
SM
1390 if (jafo) {
1391 spin_lock_irq(host->host_lock);
1392 }
1393 return retval;
1394}
1395
1396int aac_reset_adapter(struct aac_dev * aac, int forced)
1397{
1398 unsigned long flagv = 0;
1399 int retval;
1400 struct Scsi_Host * host;
1401
1402 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1403 return -EBUSY;
1404
1405 if (aac->in_reset) {
1406 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1407 return -EBUSY;
1408 }
1409 aac->in_reset = 1;
1410 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1411
1412 /*
1413 * Wait for all commands to complete to this specific
1414 * target (block maximum 60 seconds). Although not necessary,
1415 * it does make us a good storage citizen.
1416 */
1417 host = aac->scsi_host_ptr;
1418 scsi_block_requests(host);
1419 if (forced < 2) for (retval = 60; retval; --retval) {
1420 struct scsi_device * dev;
1421 struct scsi_cmnd * command;
1422 int active = 0;
1423
1424 __shost_for_each_device(dev, host) {
1425 spin_lock_irqsave(&dev->list_lock, flagv);
1426 list_for_each_entry(command, &dev->cmd_list, list) {
1427 if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
1428 active++;
1429 break;
1430 }
1431 }
1432 spin_unlock_irqrestore(&dev->list_lock, flagv);
1433 if (active)
1434 break;
1435
1436 }
1437 /*
1438 * We can exit If all the commands are complete
1439 */
1440 if (active == 0)
1441 break;
1442 ssleep(1);
1443 }
1444
1445 /* Quiesce build, flush cache, write through mode */
f858317d
SM
1446 if (forced < 2)
1447 aac_send_shutdown(aac);
29c97684 1448 spin_lock_irqsave(host->host_lock, flagv);
f858317d 1449 retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
29c97684
SM
1450 spin_unlock_irqrestore(host->host_lock, flagv);
1451
f858317d 1452 if ((forced < 2) && (retval == -ENODEV)) {
29c97684
SM
1453 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
1454 struct fib * fibctx = aac_fib_alloc(aac);
1455 if (fibctx) {
1456 struct aac_pause *cmd;
1457 int status;
1458
1459 aac_fib_init(fibctx);
1460
1461 cmd = (struct aac_pause *) fib_data(fibctx);
1462
1463 cmd->command = cpu_to_le32(VM_ContainerConfig);
1464 cmd->type = cpu_to_le32(CT_PAUSE_IO);
1465 cmd->timeout = cpu_to_le32(1);
1466 cmd->min = cpu_to_le32(1);
1467 cmd->noRescan = cpu_to_le32(1);
1468 cmd->count = cpu_to_le32(0);
1469
1470 status = aac_fib_send(ContainerCommand,
1471 fibctx,
1472 sizeof(struct aac_pause),
1473 FsaNormal,
1474 -2 /* Timeout silently */, 1,
1475 NULL, NULL);
1476
1477 if (status >= 0)
1478 aac_fib_complete(fibctx);
cacb6dc3
PNRCEH
1479 /* FIB should be freed only after getting
1480 * the response from the F/W */
1481 if (status != -ERESTARTSYS)
1482 aac_fib_free(fibctx);
29c97684
SM
1483 }
1484 }
1485
8c867b25
MH
1486 return retval;
1487}
1488
1489int aac_check_health(struct aac_dev * aac)
1490{
1491 int BlinkLED;
1492 unsigned long time_now, flagv = 0;
1493 struct list_head * entry;
1494 struct Scsi_Host * host;
1495
1496 /* Extending the scope of fib_lock slightly to protect aac->in_reset */
1497 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
1498 return 0;
1499
1500 if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
1501 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1502 return 0; /* OK */
1503 }
1504
1505 aac->in_reset = 1;
1506
1507 /* Fake up an AIF:
1508 * aac_aifcmd.command = AifCmdEventNotify = 1
1509 * aac_aifcmd.seqnum = 0xFFFFFFFF
1510 * aac_aifcmd.data[0] = AifEnExpEvent = 23
1511 * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
1512 * aac.aifcmd.data[2] = AifHighPriority = 3
1513 * aac.aifcmd.data[3] = BlinkLED
1514 */
1515
1516 time_now = jiffies/HZ;
1517 entry = aac->fib_list.next;
1518
1519 /*
1520 * For each Context that is on the
1521 * fibctxList, make a copy of the
1522 * fib, and then set the event to wake up the
1523 * thread that is waiting for it.
1524 */
1525 while (entry != &aac->fib_list) {
1526 /*
1527 * Extract the fibctx
1528 */
1529 struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
1530 struct hw_fib * hw_fib;
1531 struct fib * fib;
1532 /*
1533 * Check if the queue is getting
1534 * backlogged
1535 */
1536 if (fibctx->count > 20) {
1537 /*
1538 * It's *not* jiffies folks,
1539 * but jiffies / HZ, so do not
1540 * panic ...
1541 */
1542 u32 time_last = fibctx->jiffies;
1543 /*
1544 * Has it been > 2 minutes
1545 * since the last read off
1546 * the queue?
1547 */
1548 if ((time_now - time_last) > aif_timeout) {
1549 entry = entry->next;
1550 aac_close_fib_context(aac, fibctx);
1551 continue;
1552 }
1553 }
1554 /*
1555 * Warning: no sleep allowed while
1556 * holding spinlock
1557 */
4dbc22d7
SM
1558 hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
1559 fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
8c867b25
MH
1560 if (fib && hw_fib) {
1561 struct aac_aifcmd * aif;
1562
a8166a52 1563 fib->hw_fib_va = hw_fib;
8c867b25
MH
1564 fib->dev = aac;
1565 aac_fib_init(fib);
1566 fib->type = FSAFS_NTC_FIB_CONTEXT;
1567 fib->size = sizeof (struct fib);
1568 fib->data = hw_fib->data;
1569 aif = (struct aac_aifcmd *)hw_fib->data;
1570 aif->command = cpu_to_le32(AifCmdEventNotify);
a3940da5
SM
1571 aif->seqnum = cpu_to_le32(0xFFFFFFFF);
1572 ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
1573 ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
1574 ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
1575 ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
8c867b25
MH
1576
1577 /*
1578 * Put the FIB onto the
1579 * fibctx's fibs
1580 */
1581 list_add_tail(&fib->fiblink, &fibctx->fib_list);
1582 fibctx->count++;
1583 /*
1584 * Set the event to wake up the
1585 * thread that will waiting.
1586 */
1587 up(&fibctx->wait_sem);
1588 } else {
1589 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1590 kfree(fib);
1591 kfree(hw_fib);
1592 }
1593 entry = entry->next;
1594 }
1595
1596 spin_unlock_irqrestore(&aac->fib_lock, flagv);
1597
1598 if (BlinkLED < 0) {
1599 printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
1600 goto out;
1601 }
1602
1603 printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
1604
2f7ecc55 1605 if (!aac_check_reset || ((aac_check_reset == 1) &&
a3940da5
SM
1606 (aac->supplement_adapter_info.SupportedOptions2 &
1607 AAC_OPTION_IGNORE_RESET)))
29c97684 1608 goto out;
8c867b25 1609 host = aac->scsi_host_ptr;
29c97684
SM
1610 if (aac->thread->pid != current->pid)
1611 spin_lock_irqsave(host->host_lock, flagv);
f858317d 1612 BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
29c97684
SM
1613 if (aac->thread->pid != current->pid)
1614 spin_unlock_irqrestore(host->host_lock, flagv);
8c867b25
MH
1615 return BlinkLED;
1616
1617out:
1618 aac->in_reset = 0;
1619 return BlinkLED;
1620}
1621
1622
1da177e4
LT
1623/**
1624 * aac_command_thread - command processing thread
1625 * @dev: Adapter to monitor
1626 *
1627 * Waits on the commandready event in it's queue. When the event gets set
1628 * it will pull FIBs off it's queue. It will continue to pull FIBs off
1629 * until the queue is empty. When the queue is empty it will wait for
1630 * more FIBs.
1631 */
8ce3eca4 1632
fe27381d 1633int aac_command_thread(void *data)
1da177e4 1634{
fe27381d 1635 struct aac_dev *dev = data;
1da177e4
LT
1636 struct hw_fib *hw_fib, *hw_newfib;
1637 struct fib *fib, *newfib;
1da177e4
LT
1638 struct aac_fib_context *fibctx;
1639 unsigned long flags;
1640 DECLARE_WAITQUEUE(wait, current);
29c97684
SM
1641 unsigned long next_jiffies = jiffies + HZ;
1642 unsigned long next_check_jiffies = next_jiffies;
1643 long difference = HZ;
1da177e4
LT
1644
1645 /*
1646 * We can only have one thread per adapter for AIF's.
1647 */
1648 if (dev->aif_thread)
1649 return -EINVAL;
fe27381d 1650
1da177e4
LT
1651 /*
1652 * Let the DPC know it has a place to send the AIF's to.
1653 */
1654 dev->aif_thread = 1;
2f130980 1655 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1da177e4 1656 set_current_state(TASK_INTERRUPTIBLE);
2f130980 1657 dprintk ((KERN_INFO "aac_command_thread start\n"));
8ce3eca4 1658 while (1) {
2f130980
MH
1659 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1660 while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
1da177e4
LT
1661 struct list_head *entry;
1662 struct aac_aifcmd * aifcmd;
1663
1664 set_current_state(TASK_RUNNING);
8ce3eca4 1665
2f130980 1666 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
1da177e4 1667 list_del(entry);
8ce3eca4 1668
2f130980 1669 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1da177e4
LT
1670 fib = list_entry(entry, struct fib, fiblink);
1671 /*
8ce3eca4
SM
1672 * We will process the FIB here or pass it to a
1673 * worker thread that is TBD. We Really can't
1da177e4
LT
1674 * do anything at this point since we don't have
1675 * anything defined for this thread to do.
1676 */
a8166a52 1677 hw_fib = fib->hw_fib_va;
1da177e4
LT
1678 memset(fib, 0, sizeof(struct fib));
1679 fib->type = FSAFS_NTC_FIB_CONTEXT;
8ce3eca4 1680 fib->size = sizeof(struct fib);
a8166a52 1681 fib->hw_fib_va = hw_fib;
1da177e4
LT
1682 fib->data = hw_fib->data;
1683 fib->dev = dev;
1684 /*
1685 * We only handle AifRequest fibs from the adapter.
1686 */
1687 aifcmd = (struct aac_aifcmd *) hw_fib->data;
1688 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
1689 /* Handle Driver Notify Events */
131256cf 1690 aac_handle_aif(dev, fib);
56b58712 1691 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
bfb35aa8 1692 aac_fib_adapter_complete(fib, (u16)sizeof(u32));
1da177e4 1693 } else {
1da177e4
LT
1694 /* The u32 here is important and intended. We are using
1695 32bit wrapping time to fit the adapter field */
8ce3eca4 1696
1da177e4
LT
1697 u32 time_now, time_last;
1698 unsigned long flagv;
2f130980
MH
1699 unsigned num;
1700 struct hw_fib ** hw_fib_pool, ** hw_fib_p;
1701 struct fib ** fib_pool, ** fib_p;
8ce3eca4 1702
131256cf 1703 /* Sniff events */
8ce3eca4 1704 if ((aifcmd->command ==
131256cf 1705 cpu_to_le32(AifCmdEventNotify)) ||
8ce3eca4 1706 (aifcmd->command ==
131256cf
MH
1707 cpu_to_le32(AifCmdJobProgress))) {
1708 aac_handle_aif(dev, fib);
1709 }
29c97684 1710
1da177e4
LT
1711 time_now = jiffies/HZ;
1712
2f130980
MH
1713 /*
1714 * Warning: no sleep allowed while
1715 * holding spinlock. We take the estimate
1716 * and pre-allocate a set of fibs outside the
1717 * lock.
1718 */
1719 num = le32_to_cpu(dev->init->AdapterFibsSize)
1720 / sizeof(struct hw_fib); /* some extra */
1721 spin_lock_irqsave(&dev->fib_lock, flagv);
1722 entry = dev->fib_list.next;
1723 while (entry != &dev->fib_list) {
1724 entry = entry->next;
1725 ++num;
1726 }
1727 spin_unlock_irqrestore(&dev->fib_lock, flagv);
1728 hw_fib_pool = NULL;
1729 fib_pool = NULL;
1730 if (num
1731 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
1732 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
1733 hw_fib_p = hw_fib_pool;
1734 fib_p = fib_pool;
1735 while (hw_fib_p < &hw_fib_pool[num]) {
1736 if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
1737 --hw_fib_p;
1738 break;
1739 }
1740 if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
1741 kfree(*(--hw_fib_p));
1742 break;
1743 }
1744 }
1745 if ((num = hw_fib_p - hw_fib_pool) == 0) {
1746 kfree(fib_pool);
1747 fib_pool = NULL;
1748 kfree(hw_fib_pool);
1749 hw_fib_pool = NULL;
1750 }
c9475cb0 1751 } else {
2f130980
MH
1752 kfree(hw_fib_pool);
1753 hw_fib_pool = NULL;
1754 }
1da177e4
LT
1755 spin_lock_irqsave(&dev->fib_lock, flagv);
1756 entry = dev->fib_list.next;
1757 /*
8ce3eca4 1758 * For each Context that is on the
1da177e4
LT
1759 * fibctxList, make a copy of the
1760 * fib, and then set the event to wake up the
1761 * thread that is waiting for it.
1762 */
2f130980
MH
1763 hw_fib_p = hw_fib_pool;
1764 fib_p = fib_pool;
1da177e4
LT
1765 while (entry != &dev->fib_list) {
1766 /*
1767 * Extract the fibctx
1768 */
1769 fibctx = list_entry(entry, struct aac_fib_context, next);
1770 /*
1771 * Check if the queue is getting
1772 * backlogged
1773 */
1774 if (fibctx->count > 20)
1775 {
1776 /*
1777 * It's *not* jiffies folks,
1778 * but jiffies / HZ so do not
1779 * panic ...
1780 */
1781 time_last = fibctx->jiffies;
1782 /*
8ce3eca4 1783 * Has it been > 2 minutes
1da177e4
LT
1784 * since the last read off
1785 * the queue?
1786 */
404d9a90 1787 if ((time_now - time_last) > aif_timeout) {
1da177e4
LT
1788 entry = entry->next;
1789 aac_close_fib_context(dev, fibctx);
1790 continue;
1791 }
1792 }
1793 /*
1794 * Warning: no sleep allowed while
1795 * holding spinlock
1796 */
2f130980
MH
1797 if (hw_fib_p < &hw_fib_pool[num]) {
1798 hw_newfib = *hw_fib_p;
1799 *(hw_fib_p++) = NULL;
1800 newfib = *fib_p;
1801 *(fib_p++) = NULL;
1da177e4
LT
1802 /*
1803 * Make the copy of the FIB
1804 */
1805 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
1806 memcpy(newfib, fib, sizeof(struct fib));
a8166a52 1807 newfib->hw_fib_va = hw_newfib;
1da177e4
LT
1808 /*
1809 * Put the FIB onto the
1810 * fibctx's fibs
1811 */
1812 list_add_tail(&newfib->fiblink, &fibctx->fib_list);
1813 fibctx->count++;
8ce3eca4 1814 /*
1da177e4 1815 * Set the event to wake up the
2f130980 1816 * thread that is waiting.
1da177e4
LT
1817 */
1818 up(&fibctx->wait_sem);
1819 } else {
1820 printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
1da177e4
LT
1821 }
1822 entry = entry->next;
1823 }
1824 /*
1825 * Set the status of this FIB
1826 */
56b58712 1827 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
bfb35aa8 1828 aac_fib_adapter_complete(fib, sizeof(u32));
1da177e4 1829 spin_unlock_irqrestore(&dev->fib_lock, flagv);
2f130980
MH
1830 /* Free up the remaining resources */
1831 hw_fib_p = hw_fib_pool;
1832 fib_p = fib_pool;
1833 while (hw_fib_p < &hw_fib_pool[num]) {
c9475cb0
JJ
1834 kfree(*hw_fib_p);
1835 kfree(*fib_p);
2f130980
MH
1836 ++fib_p;
1837 ++hw_fib_p;
1838 }
c9475cb0
JJ
1839 kfree(hw_fib_pool);
1840 kfree(fib_pool);
1da177e4 1841 }
1da177e4 1842 kfree(fib);
2f130980 1843 spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
1da177e4
LT
1844 }
1845 /*
1846 * There are no more AIF's
1847 */
2f130980 1848 spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
29c97684
SM
1849
1850 /*
1851 * Background activity
1852 */
1853 if ((time_before(next_check_jiffies,next_jiffies))
1854 && ((difference = next_check_jiffies - jiffies) <= 0)) {
1855 next_check_jiffies = next_jiffies;
1856 if (aac_check_health(dev) == 0) {
1857 difference = ((long)(unsigned)check_interval)
1858 * HZ;
1859 next_check_jiffies = jiffies + difference;
1860 } else if (!dev->queues)
1861 break;
1862 }
1863 if (!time_before(next_check_jiffies,next_jiffies)
1864 && ((difference = next_jiffies - jiffies) <= 0)) {
1865 struct timeval now;
1866 int ret;
1867
1868 /* Don't even try to talk to adapter if its sick */
1869 ret = aac_check_health(dev);
1870 if (!ret && !dev->queues)
1871 break;
1872 next_check_jiffies = jiffies
1873 + ((long)(unsigned)check_interval)
1874 * HZ;
1875 do_gettimeofday(&now);
1876
1877 /* Synchronize our watches */
1878 if (((1000000 - (1000000 / HZ)) > now.tv_usec)
1879 && (now.tv_usec > (1000000 / HZ)))
1880 difference = (((1000000 - now.tv_usec) * HZ)
1881 + 500000) / 1000000;
1882 else if (ret == 0) {
1883 struct fib *fibptr;
1884
1885 if ((fibptr = aac_fib_alloc(dev))) {
cacb6dc3 1886 int status;
f3307f72 1887 __le32 *info;
29c97684
SM
1888
1889 aac_fib_init(fibptr);
1890
f3307f72 1891 info = (__le32 *) fib_data(fibptr);
29c97684
SM
1892 if (now.tv_usec > 500000)
1893 ++now.tv_sec;
1894
1895 *info = cpu_to_le32(now.tv_sec);
1896
cacb6dc3 1897 status = aac_fib_send(SendHostTime,
29c97684
SM
1898 fibptr,
1899 sizeof(*info),
1900 FsaNormal,
1901 1, 1,
1902 NULL,
1903 NULL);
cacb6dc3
PNRCEH
1904 /* Do not set XferState to zero unless
1905 * receives a response from F/W */
1906 if (status >= 0)
1907 aac_fib_complete(fibptr);
1908 /* FIB should be freed only after
1909 * getting the response from the F/W */
1910 if (status != -ERESTARTSYS)
1911 aac_fib_free(fibptr);
29c97684
SM
1912 }
1913 difference = (long)(unsigned)update_interval*HZ;
1914 } else {
1915 /* retry shortly */
1916 difference = 10 * HZ;
1917 }
1918 next_jiffies = jiffies + difference;
1919 if (time_before(next_check_jiffies,next_jiffies))
1920 difference = next_check_jiffies - jiffies;
1921 }
1922 if (difference <= 0)
1923 difference = 1;
1924 set_current_state(TASK_INTERRUPTIBLE);
1925 schedule_timeout(difference);
1da177e4 1926
fe27381d 1927 if (kthread_should_stop())
1da177e4 1928 break;
1da177e4 1929 }
2f130980
MH
1930 if (dev->queues)
1931 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
1da177e4 1932 dev->aif_thread = 0;
2f130980 1933 return 0;
1da177e4 1934}