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