commsup.c 53.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *	Adaptec AAC series RAID controller driver
3
 *	(c) Copyright 2001 Red Hat Inc.
L
Linus Torvalds 已提交
4 5 6 7
 *
 * based on the old aacraid driver that is..
 * Adaptec aacraid device driver for Linux.
 *
8 9
 * Copyright (c) 2000-2010 Adaptec, Inc.
 *               2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Module Name:
 *  commsup.c
 *
 * Abstract: Contain all routines that are required for FSA host/adapter
29
 *    communication.
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40 41
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/pci.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/completion.h>
#include <linux/blkdev.h>
A
Al Viro 已提交
42
#include <linux/delay.h>
43
#include <linux/kthread.h>
44
#include <linux/interrupt.h>
45
#include <linux/semaphore.h>
46
#include <scsi/scsi.h>
47
#include <scsi/scsi_host.h>
48
#include <scsi/scsi_device.h>
49
#include <scsi/scsi_cmnd.h>
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59

#include "aacraid.h"

/**
 *	fib_map_alloc		-	allocate the fib objects
 *	@dev: Adapter to allocate for
 *
 *	Allocate and map the shared PCI space for the FIB blocks used to
 *	talk to the Adaptec firmware.
 */
60

L
Linus Torvalds 已提交
61 62
static int fib_map_alloc(struct aac_dev *dev)
{
63 64 65 66
	dprintk((KERN_INFO
	  "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
	  dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
	  AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
67 68 69 70 71
	dev->hw_fib_va = pci_alloc_consistent(dev->pdev,
		(dev->max_fib_size + sizeof(struct aac_fib_xporthdr))
		* (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
		&dev->hw_fib_pa);
	if (dev->hw_fib_va == NULL)
L
Linus Torvalds 已提交
72 73 74 75 76
		return -ENOMEM;
	return 0;
}

/**
77
 *	aac_fib_map_free		-	free the fib objects
L
Linus Torvalds 已提交
78 79 80 81 82 83
 *	@dev: Adapter to free
 *
 *	Free the PCI mappings and the memory allocated for FIB blocks
 *	on this adapter.
 */

84
void aac_fib_map_free(struct aac_dev *dev)
L
Linus Torvalds 已提交
85
{
86 87 88 89 90
	pci_free_consistent(dev->pdev,
	  dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
	  dev->hw_fib_va, dev->hw_fib_pa);
	dev->hw_fib_va = NULL;
	dev->hw_fib_pa = 0;
L
Linus Torvalds 已提交
91 92 93
}

/**
94
 *	aac_fib_setup	-	setup the fibs
L
Linus Torvalds 已提交
95 96
 *	@dev: Adapter to set up
 *
97
 *	Allocate the PCI space for the fibs, map it and then initialise the
L
Linus Torvalds 已提交
98 99 100
 *	fib area, the unmapped fib data and also the free list
 */

101
int aac_fib_setup(struct aac_dev * dev)
L
Linus Torvalds 已提交
102 103
{
	struct fib *fibptr;
104
	struct hw_fib *hw_fib;
L
Linus Torvalds 已提交
105 106
	dma_addr_t hw_fib_pa;
	int i;
107 108 109 110 111 112 113

	while (((i = fib_map_alloc(dev)) == -ENOMEM)
	 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
		dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
		dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
	}
	if (i<0)
L
Linus Torvalds 已提交
114
		return -ENOMEM;
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129
	/* 32 byte alignment for PMC */
	hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
	dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
		(hw_fib_pa - dev->hw_fib_pa));
	dev->hw_fib_pa = hw_fib_pa;
	memset(dev->hw_fib_va, 0,
		(dev->max_fib_size + sizeof(struct aac_fib_xporthdr)) *
		(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));

	/* add Xport header */
	dev->hw_fib_va = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
		sizeof(struct aac_fib_xporthdr));
	dev->hw_fib_pa += sizeof(struct aac_fib_xporthdr);

130
	hw_fib = dev->hw_fib_va;
L
Linus Torvalds 已提交
131 132 133 134
	hw_fib_pa = dev->hw_fib_pa;
	/*
	 *	Initialise the fibs
	 */
135 136 137
	for (i = 0, fibptr = &dev->fibs[i];
		i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
		i++, fibptr++)
L
Linus Torvalds 已提交
138 139
	{
		fibptr->dev = dev;
140 141
		fibptr->hw_fib_va = hw_fib;
		fibptr->data = (void *) fibptr->hw_fib_va->data;
L
Linus Torvalds 已提交
142
		fibptr->next = fibptr+1;	/* Forward chain the fibs */
143
		sema_init(&fibptr->event_wait, 0);
L
Linus Torvalds 已提交
144
		spin_lock_init(&fibptr->event_lock);
145 146
		hw_fib->header.XferState = cpu_to_le32(0xffffffff);
		hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
L
Linus Torvalds 已提交
147
		fibptr->hw_fib_pa = hw_fib_pa;
148 149 150 151
		hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
			dev->max_fib_size + sizeof(struct aac_fib_xporthdr));
		hw_fib_pa = hw_fib_pa +
			dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
L
Linus Torvalds 已提交
152 153 154 155
	}
	/*
	 *	Add the fib chain to the free list
	 */
156
	dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164
	/*
	 *	Enable this to debug out of queue space
	 */
	dev->free_fib = &dev->fibs[0];
	return 0;
}

/**
165
 *	aac_fib_alloc	-	allocate a fib
L
Linus Torvalds 已提交
166 167 168
 *	@dev: Adapter to allocate the fib for
 *
 *	Allocate a fib from the adapter fib pool. If the pool is empty we
169
 *	return NULL.
L
Linus Torvalds 已提交
170
 */
171

172
struct fib *aac_fib_alloc(struct aac_dev *dev)
L
Linus Torvalds 已提交
173 174 175 176
{
	struct fib * fibptr;
	unsigned long flags;
	spin_lock_irqsave(&dev->fib_lock, flags);
177
	fibptr = dev->free_fib;
178 179 180 181
	if(!fibptr){
		spin_unlock_irqrestore(&dev->fib_lock, flags);
		return fibptr;
	}
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191 192
	dev->free_fib = fibptr->next;
	spin_unlock_irqrestore(&dev->fib_lock, flags);
	/*
	 *	Set the proper node type code and node byte size
	 */
	fibptr->type = FSAFS_NTC_FIB_CONTEXT;
	fibptr->size = sizeof(struct fib);
	/*
	 *	Null out fields that depend on being zero at the start of
	 *	each I/O
	 */
193
	fibptr->hw_fib_va->header.XferState = 0;
194
	fibptr->flags = 0;
L
Linus Torvalds 已提交
195 196 197 198 199 200 201
	fibptr->callback = NULL;
	fibptr->callback_data = NULL;

	return fibptr;
}

/**
202
 *	aac_fib_free	-	free a fib
L
Linus Torvalds 已提交
203 204 205 206
 *	@fibptr: fib to free up
 *
 *	Frees up a fib and places it on the appropriate queue
 */
207

208
void aac_fib_free(struct fib *fibptr)
L
Linus Torvalds 已提交
209
{
210 211 212 213 214 215 216 217
	unsigned long flags, flagsv;

	spin_lock_irqsave(&fibptr->event_lock, flagsv);
	if (fibptr->done == 2) {
		spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
		return;
	}
	spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
L
Linus Torvalds 已提交
218 219

	spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
220
	if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
L
Linus Torvalds 已提交
221
		aac_config.fib_timeouts++;
222 223 224 225 226 227 228
	if (fibptr->hw_fib_va->header.XferState != 0) {
		printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
			 (void*)fibptr,
			 le32_to_cpu(fibptr->hw_fib_va->header.XferState));
	}
	fibptr->next = fibptr->dev->free_fib;
	fibptr->dev->free_fib = fibptr;
L
Linus Torvalds 已提交
229 230 231 232
	spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
}

/**
233
 *	aac_fib_init	-	initialise a fib
L
Linus Torvalds 已提交
234
 *	@fibptr: The fib to initialize
235
 *
L
Linus Torvalds 已提交
236 237
 *	Set up the generic fib fields ready for use
 */
238

239
void aac_fib_init(struct fib *fibptr)
L
Linus Torvalds 已提交
240
{
241
	struct hw_fib *hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
242 243

	hw_fib->header.StructType = FIB_MAGIC;
244 245
	hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
	hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
246
	hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
L
Linus Torvalds 已提交
247
	hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
248
	hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257
}

/**
 *	fib_deallocate		-	deallocate a fib
 *	@fibptr: fib to deallocate
 *
 *	Will deallocate and return to the free pool the FIB pointed to by the
 *	caller.
 */
258

259
static void fib_dealloc(struct fib * fibptr)
L
Linus Torvalds 已提交
260
{
261
	struct hw_fib *hw_fib = fibptr->hw_fib_va;
E
Eric Sesterhenn 已提交
262
	BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
263
	hw_fib->header.XferState = 0;
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271
}

/*
 *	Commuication primitives define and support the queuing method we use to
 *	support host to adapter commuication. All queue accesses happen through
 *	these routines and are the only routines which have a knowledge of the
 *	 how these queues are implemented.
 */
272

L
Linus Torvalds 已提交
273 274 275 276 277 278 279 280 281 282 283 284
/**
 *	aac_get_entry		-	get a queue entry
 *	@dev: Adapter
 *	@qid: Queue Number
 *	@entry: Entry return
 *	@index: Index return
 *	@nonotify: notification control
 *
 *	With a priority the routine returns a queue entry if the queue has free entries. If the queue
 *	is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
 *	returned.
 */
285

L
Linus Torvalds 已提交
286 287 288
static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
{
	struct aac_queue * q;
289
	unsigned long idx;
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298

	/*
	 *	All of the queues wrap when they reach the end, so we check
	 *	to see if they have reached the end and if they have we just
	 *	set the index back to zero. This is a wrap. You could or off
	 *	the high bits in all updates but this is a bit faster I think.
	 */

	q = &dev->queues->queue[qid];
299 300 301 302 303

	idx = *index = le32_to_cpu(*(q->headers.producer));
	/* Interrupt Moderation, only interrupt for first two entries */
	if (idx != le32_to_cpu(*(q->headers.consumer))) {
		if (--idx == 0) {
304
			if (qid == AdapNormCmdQueue)
305
				idx = ADAP_NORM_CMD_ENTRIES;
306
			else
307 308 309
				idx = ADAP_NORM_RESP_ENTRIES;
		}
		if (idx != le32_to_cpu(*(q->headers.consumer)))
310
			*nonotify = 1;
311
	}
L
Linus Torvalds 已提交
312

313
	if (qid == AdapNormCmdQueue) {
314
		if (*index >= ADAP_NORM_CMD_ENTRIES)
L
Linus Torvalds 已提交
315
			*index = 0; /* Wrap to front of the Producer Queue. */
316
	} else {
317
		if (*index >= ADAP_NORM_RESP_ENTRIES)
L
Linus Torvalds 已提交
318 319 320
			*index = 0; /* Wrap to front of the Producer Queue. */
	}

321 322
	/* Queue is full */
	if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
323
		printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
L
Linus Torvalds 已提交
324 325 326
				qid, q->numpending);
		return 0;
	} else {
327
		*entry = q->base + *index;
L
Linus Torvalds 已提交
328 329
		return 1;
	}
330
}
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

/**
 *	aac_queue_get		-	get the next free QE
 *	@dev: Adapter
 *	@index: Returned index
 *	@priority: Priority of fib
 *	@fib: Fib to associate with the queue entry
 *	@wait: Wait if queue full
 *	@fibptr: Driver fib object to go with fib
 *	@nonotify: Don't notify the adapter
 *
 *	Gets the next free QE off the requested priorty adapter command
 *	queue and associates the Fib with the QE. The QE represented by
 *	index is ready to insert on the queue when this routine returns
 *	success.
 */

348
int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
L
Linus Torvalds 已提交
349 350 351
{
	struct aac_entry * entry = NULL;
	int map = 0;
352

353
	if (qid == AdapNormCmdQueue) {
L
Linus Torvalds 已提交
354
		/*  if no entries wait for some if caller wants to */
355
		while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
L
Linus Torvalds 已提交
356 357
			printk(KERN_ERR "GetEntries failed\n");
		}
358 359 360 361 362
		/*
		 *	Setup queue entry with a command, status and fib mapped
		 */
		entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
		map = 1;
363
	} else {
364
		while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
L
Linus Torvalds 已提交
365 366
			/* if no entries wait for some if caller wants to */
		}
367 368 369 370 371 372
		/*
		 *	Setup queue entry with command, status and fib mapped
		 */
		entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
		entry->addr = hw_fib->header.SenderFibAddress;
			/* Restore adapters pointer to the FIB */
L
Linus Torvalds 已提交
373
		hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress;	/* Let the adapter now where to find its data */
374
		map = 0;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385
	}
	/*
	 *	If MapFib is true than we need to map the Fib and put pointers
	 *	in the queue entry.
	 */
	if (map)
		entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
	return 0;
}

/*
386 387
 *	Define the highest level of host to adapter communication routines.
 *	These routines will support host to adapter FS commuication. These
L
Linus Torvalds 已提交
388 389 390 391 392 393
 *	routines have no knowledge of the commuication method used. This level
 *	sends and receives FIBs. This level has no knowledge of how these FIBs
 *	get passed back and forth.
 */

/**
394
 *	aac_fib_send	-	send a fib to the adapter
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408
 *	@command: Command to send
 *	@fibptr: The fib
 *	@size: Size of fib data area
 *	@priority: Priority of Fib
 *	@wait: Async/sync select
 *	@reply: True if a reply is wanted
 *	@callback: Called with reply
 *	@callback_data: Passed to callback
 *
 *	Sends the requested FIB to the adapter and optionally will wait for a
 *	response FIB. If the caller does not wish to wait for a response than
 *	an event to wait on must be supplied. This event will be set when a
 *	response FIB is received from the adapter.
 */
409

410 411 412
int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
		int priority, int wait, int reply, fib_callback callback,
		void *callback_data)
L
Linus Torvalds 已提交
413 414
{
	struct aac_dev * dev = fibptr->dev;
415
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
416
	unsigned long flags = 0;
417
	unsigned long qflags;
418 419
	unsigned long mflags = 0;

420

L
Linus Torvalds 已提交
421 422 423
	if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
		return -EBUSY;
	/*
424
	 *	There are 5 cases with the wait and reponse requested flags.
L
Linus Torvalds 已提交
425 426 427 428 429
	 *	The only invalid cases are if the caller requests to wait and
	 *	does not request a response and if the caller does not want a
	 *	response and the Fib is not allocated from pool. If a response
	 *	is not requesed the Fib will just be deallocaed by the DPC
	 *	routine when the response comes back from the adapter. No
430
	 *	further processing will be done besides deleting the Fib. We
L
Linus Torvalds 已提交
431 432 433
	 *	will have a debug mode where the adapter can notify the host
	 *	it had a problem and the host can log that fact.
	 */
434
	fibptr->flags = 0;
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445
	if (wait && !reply) {
		return -EINVAL;
	} else if (!wait && reply) {
		hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
		FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
	} else if (!wait && !reply) {
		hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
		FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
	} else if (wait && reply) {
		hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
		FIB_COUNTER_INCREMENT(aac_config.NormalSent);
446
	}
L
Linus Torvalds 已提交
447 448 449 450
	/*
	 *	Map the fib into 32bits by using the fib number
	 */

451
	hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461
	hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
	/*
	 *	Set FIB state to indicate where it came from and if we want a
	 *	response from the adapter. Also load the command from the
	 *	caller.
	 *
	 *	Map the hw fib pointer as a 32bit value
	 */
	hw_fib->header.Command = cpu_to_le16(command);
	hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
462
	fibptr->hw_fib_va->header.Flags = 0;	/* 0 the flags field - internal only*/
L
Linus Torvalds 已提交
463 464 465 466 467 468
	/*
	 *	Set the size of the Fib we want to send to the adapter
	 */
	hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
	if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
		return -EMSGSIZE;
469
	}
L
Linus Torvalds 已提交
470 471 472 473
	/*
	 *	Get a queue entry connect the FIB to it and send an notify
	 *	the adapter a command is ready.
	 */
474
	hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482

	/*
	 *	Fill in the Callback and CallbackContext if we are not
	 *	going to wait.
	 */
	if (!wait) {
		fibptr->callback = callback;
		fibptr->callback_data = callback_data;
483
		fibptr->flags = FIB_CONTEXT_FLAG;
L
Linus Torvalds 已提交
484 485 486 487
	}

	fibptr->done = 0;

488 489 490
	FIB_COUNTER_INCREMENT(aac_config.FibsSent);

	dprintk((KERN_DEBUG "Fib contents:.\n"));
491 492 493
	dprintk((KERN_DEBUG "  Command =               %d.\n", le32_to_cpu(hw_fib->header.Command)));
	dprintk((KERN_DEBUG "  SubCommand =            %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
	dprintk((KERN_DEBUG "  XferState  =            %x.\n", le32_to_cpu(hw_fib->header.XferState)));
494
	dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib_va));
495 496 497
	dprintk((KERN_DEBUG "  hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
	dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));

498
	if (!dev->queues)
M
Mark Haverkamp 已提交
499
		return -EBUSY;
500

501 502 503 504 505 506 507 508 509 510 511
	if (wait) {

		spin_lock_irqsave(&dev->manage_lock, mflags);
		if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
			printk(KERN_INFO "No management Fibs Available:%d\n",
						dev->management_fib_count);
			spin_unlock_irqrestore(&dev->manage_lock, mflags);
			return -EBUSY;
		}
		dev->management_fib_count++;
		spin_unlock_irqrestore(&dev->manage_lock, mflags);
512
		spin_lock_irqsave(&fibptr->event_lock, flags);
513 514 515 516 517 518 519 520 521 522 523 524 525
	}

	if (aac_adapter_deliver(fibptr) != 0) {
		printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
		if (wait) {
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
			spin_lock_irqsave(&dev->manage_lock, mflags);
			dev->management_fib_count--;
			spin_unlock_irqrestore(&dev->manage_lock, mflags);
		}
		return -EBUSY;
	}

526

L
Linus Torvalds 已提交
527
	/*
528
	 *	If the caller wanted us to wait for response wait now.
L
Linus Torvalds 已提交
529
	 */
530

L
Linus Torvalds 已提交
531 532
	if (wait) {
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
533 534 535 536 537 538 539 540 541 542
		/* Only set for first known interruptable command */
		if (wait < 0) {
			/*
			 * *VERY* Dangerous to time out a command, the
			 * assumption is made that we have no hope of
			 * functioning because an interrupt routing or other
			 * hardware failure has occurred.
			 */
			unsigned long count = 36000000L; /* 3 minutes */
			while (down_trylock(&fibptr->event_wait)) {
543
				int blink;
544
				if (--count == 0) {
545
					struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
546 547 548 549
					spin_lock_irqsave(q->lock, qflags);
					q->numpending--;
					spin_unlock_irqrestore(q->lock, qflags);
					if (wait == -1) {
550
	        				printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
551 552 553 554 555 556
						  "Usually a result of a PCI interrupt routing problem;\n"
						  "update mother board BIOS or consider utilizing one of\n"
						  "the SAFE mode kernel options (acpi, apic etc)\n");
					}
					return -ETIMEDOUT;
				}
557 558 559 560 561 562 563 564
				if ((blink = aac_adapter_check_health(dev)) > 0) {
					if (wait == -1) {
	        				printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
						  "Usually a result of a serious unrecoverable hardware problem\n",
						  blink);
					}
					return -EFAULT;
				}
565 566
				udelay(5);
			}
567
		} else if (down_interruptible(&fibptr->event_wait)) {
568 569
			/* Do nothing ... satisfy
			 * down_interruptible must_check */
570
		}
571

572
		spin_lock_irqsave(&fibptr->event_lock, flags);
573
		if (fibptr->done == 0) {
574
			fibptr->done = 2; /* Tell interrupt we aborted */
575
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
576
			return -ERESTARTSYS;
577
		}
578
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
E
Eric Sesterhenn 已提交
579
		BUG_ON(fibptr->done == 0);
580

581
		if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
L
Linus Torvalds 已提交
582
			return -ETIMEDOUT;
583
		return 0;
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594
	}
	/*
	 *	If the user does not want a response than return success otherwise
	 *	return pending
	 */
	if (reply)
		return -EINPROGRESS;
	else
		return 0;
}

595
/**
L
Linus Torvalds 已提交
596 597 598 599 600 601
 *	aac_consumer_get	-	get the top of the queue
 *	@dev: Adapter
 *	@q: Queue
 *	@entry: Return entry
 *
 *	Will return a pointer to the entry on the top of the queue requested that
602 603
 *	we are a consumer of, and return the address of the queue entry. It does
 *	not change the state of the queue.
L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617
 */

int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
{
	u32 index;
	int status;
	if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
		status = 0;
	} else {
		/*
		 *	The consumer index must be wrapped if we have reached
		 *	the end of the queue, else we just use the entry
		 *	pointed to by the header index
		 */
618 619
		if (le32_to_cpu(*q->headers.consumer) >= q->entries)
			index = 0;
L
Linus Torvalds 已提交
620
		else
621
			index = le32_to_cpu(*q->headers.consumer);
L
Linus Torvalds 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
		*entry = q->base + index;
		status = 1;
	}
	return(status);
}

/**
 *	aac_consumer_free	-	free consumer entry
 *	@dev: Adapter
 *	@q: Queue
 *	@qid: Queue ident
 *
 *	Frees up the current top of the queue we are a consumer of. If the
 *	queue was full notify the producer that the queue is no longer full.
 */

void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
{
	int wasfull = 0;
	u32 notify;

	if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
		wasfull = 1;
645

L
Linus Torvalds 已提交
646 647 648
	if (le32_to_cpu(*q->headers.consumer) >= q->entries)
		*q->headers.consumer = cpu_to_le32(1);
	else
649
		le32_add_cpu(q->headers.consumer, 1);
650

L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	if (wasfull) {
		switch (qid) {

		case HostNormCmdQueue:
			notify = HostNormCmdNotFull;
			break;
		case HostNormRespQueue:
			notify = HostNormRespNotFull;
			break;
		default:
			BUG();
			return;
		}
		aac_adapter_notify(dev, notify);
	}
666
}
L
Linus Torvalds 已提交
667 668

/**
669
 *	aac_fib_adapter_complete	-	complete adapter issued fib
L
Linus Torvalds 已提交
670 671 672 673 674 675 676
 *	@fibptr: fib to complete
 *	@size: size of fib
 *
 *	Will do all necessary work to complete a FIB that was sent from
 *	the adapter.
 */

677
int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
L
Linus Torvalds 已提交
678
{
679
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
680
	struct aac_dev * dev = fibptr->dev;
681
	struct aac_queue * q;
L
Linus Torvalds 已提交
682
	unsigned long nointr = 0;
683 684
	unsigned long qflags;

685 686 687 688 689
	if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
		kfree(hw_fib);
		return 0;
	}

690
	if (hw_fib->header.XferState == 0) {
691
		if (dev->comm_interface == AAC_COMM_MESSAGE)
692
			kfree(hw_fib);
693
		return 0;
694
	}
L
Linus Torvalds 已提交
695 696
	/*
	 *	If we plan to do anything check the structure type first.
697 698
	 */
	if (hw_fib->header.StructType != FIB_MAGIC) {
699
		if (dev->comm_interface == AAC_COMM_MESSAGE)
700
			kfree(hw_fib);
701
		return -EINVAL;
L
Linus Torvalds 已提交
702 703 704 705
	}
	/*
	 *	This block handles the case where the adapter had sent us a
	 *	command and we have finished processing the command. We
706 707
	 *	call completeFib when we are done processing the command
	 *	and want to send a response back to the adapter. This will
L
Linus Torvalds 已提交
708 709 710
	 *	send the completed cdb to the adapter.
	 */
	if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
711
		if (dev->comm_interface == AAC_COMM_MESSAGE) {
712 713
			kfree (hw_fib);
		} else {
714 715
			u32 index;
			hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
716 717
			if (size) {
				size += sizeof(struct aac_fibhdr);
718
				if (size > le16_to_cpu(hw_fib->header.SenderSize))
719 720 721 722 723 724 725 726 727 728
					return -EMSGSIZE;
				hw_fib->header.Size = cpu_to_le16(size);
			}
			q = &dev->queues->queue[AdapNormRespQueue];
			spin_lock_irqsave(q->lock, qflags);
			aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
			*(q->headers.producer) = cpu_to_le32(index + 1);
			spin_unlock_irqrestore(q->lock, qflags);
			if (!(nointr & (int)aac_config.irq_mod))
				aac_adapter_notify(dev, AdapNormRespQueue);
L
Linus Torvalds 已提交
729
		}
730 731 732 733
	} else {
		printk(KERN_WARNING "aac_fib_adapter_complete: "
			"Unknown xferstate detected.\n");
		BUG();
L
Linus Torvalds 已提交
734 735 736 737 738
	}
	return 0;
}

/**
739
 *	aac_fib_complete	-	fib completion handler
L
Linus Torvalds 已提交
740 741 742 743
 *	@fib: FIB to complete
 *
 *	Will do all necessary work to complete a FIB.
 */
744

745
int aac_fib_complete(struct fib *fibptr)
L
Linus Torvalds 已提交
746
{
747
	unsigned long flags;
748
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
749 750 751 752 753 754

	/*
	 *	Check for a fib which has already been completed
	 */

	if (hw_fib->header.XferState == 0)
755
		return 0;
L
Linus Torvalds 已提交
756 757
	/*
	 *	If we plan to do anything check the structure type first.
758
	 */
L
Linus Torvalds 已提交
759 760

	if (hw_fib->header.StructType != FIB_MAGIC)
761
		return -EINVAL;
L
Linus Torvalds 已提交
762
	/*
763
	 *	This block completes a cdb which orginated on the host and we
L
Linus Torvalds 已提交
764 765 766 767
	 *	just need to deallocate the cdb or reinit it. At this point the
	 *	command is complete that we had sent to the adapter and this
	 *	cdb could be reused.
	 */
768 769 770 771 772 773 774
	spin_lock_irqsave(&fibptr->event_lock, flags);
	if (fibptr->done == 2) {
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
		return 0;
	}
	spin_unlock_irqrestore(&fibptr->event_lock, flags);

L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
		(hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
	{
		fib_dealloc(fibptr);
	}
	else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
	{
		/*
		 *	This handles the case when the host has aborted the I/O
		 *	to the adapter because the adapter is not responding
		 */
		fib_dealloc(fibptr);
	} else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
		fib_dealloc(fibptr);
	} else {
		BUG();
791
	}
L
Linus Torvalds 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	return 0;
}

/**
 *	aac_printf	-	handle printf from firmware
 *	@dev: Adapter
 *	@val: Message info
 *
 *	Print a message passed to us by the controller firmware on the
 *	Adaptec board
 */

void aac_printf(struct aac_dev *dev, u32 val)
{
	char *cp = dev->printfbuf;
807 808 809 810
	if (dev->printf_enabled)
	{
		int length = val & 0xffff;
		int level = (val >> 16) & 0xffff;
811

812 813 814 815 816 817 818 819 820
		/*
		 *	The size of the printfbuf is set in port.c
		 *	There is no variable or define for it
		 */
		if (length > 255)
			length = 255;
		if (cp[length] != 0)
			cp[length] = 0;
		if (level == LOG_AAC_HIGH_ERROR)
821
			printk(KERN_WARNING "%s:%s", dev->name, cp);
822
		else
823
			printk(KERN_INFO "%s:%s", dev->name, cp);
824
	}
825
	memset(cp, 0, 256);
L
Linus Torvalds 已提交
826 827
}

828 829 830 831 832 833 834 835 836 837

/**
 *	aac_handle_aif		-	Handle a message from the firmware
 *	@dev: Which adapter this fib is from
 *	@fibptr: Pointer to fibptr from adapter
 *
 *	This routine handles a driver notify fib from the adapter and
 *	dispatches it to the appropriate routine for handling.
 */

838
#define AIF_SNIFF_TIMEOUT	(30*HZ)
839 840
static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
{
841
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
842
	struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
843
	u32 channel, id, lun, container;
844 845 846 847 848 849
	struct scsi_device *device;
	enum {
		NOTHING,
		DELETE,
		ADD,
		CHANGE
850
	} device_config_needed = NOTHING;
851 852 853

	/* Sniff for container changes */

854
	if (!dev || !dev->fsa_dev)
855
		return;
856
	container = channel = id = lun = (u32)-1;
857 858 859 860 861 862 863 864 865

	/*
	 *	We have set this up to try and minimize the number of
	 * re-configures that take place. As a result of this when
	 * certain AIF's come in we will set a flag waiting for another
	 * type of AIF before setting the re-config flag.
	 */
	switch (le32_to_cpu(aifcmd->command)) {
	case AifCmdDriverNotify:
866
		switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
867 868 869 870 871
		/*
		 *	Morph or Expand complete
		 */
		case AifDenMorphComplete:
		case AifDenVolumeExtendComplete:
872
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
873 874 875 876
			if (container >= dev->maximum_num_containers)
				break;

			/*
877
			 *	Find the scsi_device associated with the SCSI
878 879 880 881 882 883
			 * address. Make sure we have the right array, and if
			 * so set the flag to initiate a new re-config once we
			 * see an AifEnConfigChange AIF come through.
			 */

			if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
884 885 886
				device = scsi_device_lookup(dev->scsi_host_ptr,
					CONTAINER_TO_CHANNEL(container),
					CONTAINER_TO_ID(container),
887 888 889 890
					CONTAINER_TO_LUN(container));
				if (device) {
					dev->fsa_dev[container].config_needed = CHANGE;
					dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
891
					dev->fsa_dev[container].config_waiting_stamp = jiffies;
892 893 894 895 896 897 898 899 900 901 902 903
					scsi_device_put(device);
				}
			}
		}

		/*
		 *	If we are waiting on something and this happens to be
		 * that thing then set the re-configure flag.
		 */
		if (container != (u32)-1) {
			if (container >= dev->maximum_num_containers)
				break;
904
			if ((dev->fsa_dev[container].config_waiting_on ==
905
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
906
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
907 908 909
				dev->fsa_dev[container].config_waiting_on = 0;
		} else for (container = 0;
		    container < dev->maximum_num_containers; ++container) {
910
			if ((dev->fsa_dev[container].config_waiting_on ==
911
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
912
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
913 914 915 916 917
				dev->fsa_dev[container].config_waiting_on = 0;
		}
		break;

	case AifCmdEventNotify:
918
		switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
919 920 921 922
		case AifEnBatteryEvent:
			dev->cache_protected =
				(((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
			break;
923 924 925 926
		/*
		 *	Add an Array.
		 */
		case AifEnAddContainer:
927
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
928 929 930 931 932
			if (container >= dev->maximum_num_containers)
				break;
			dev->fsa_dev[container].config_needed = ADD;
			dev->fsa_dev[container].config_waiting_on =
				AifEnConfigChange;
933
			dev->fsa_dev[container].config_waiting_stamp = jiffies;
934 935 936 937 938 939
			break;

		/*
		 *	Delete an Array.
		 */
		case AifEnDeleteContainer:
940
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
941 942 943 944 945
			if (container >= dev->maximum_num_containers)
				break;
			dev->fsa_dev[container].config_needed = DELETE;
			dev->fsa_dev[container].config_waiting_on =
				AifEnConfigChange;
946
			dev->fsa_dev[container].config_waiting_stamp = jiffies;
947 948 949 950 951 952 953
			break;

		/*
		 *	Container change detected. If we currently are not
		 * waiting on something else, setup to wait on a Config Change.
		 */
		case AifEnContainerChange:
954
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
955 956
			if (container >= dev->maximum_num_containers)
				break;
957 958
			if (dev->fsa_dev[container].config_waiting_on &&
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
959 960 961 962
				break;
			dev->fsa_dev[container].config_needed = CHANGE;
			dev->fsa_dev[container].config_waiting_on =
				AifEnConfigChange;
963
			dev->fsa_dev[container].config_waiting_stamp = jiffies;
964 965 966 967 968
			break;

		case AifEnConfigChange:
			break;

969 970 971
		case AifEnAddJBOD:
		case AifEnDeleteJBOD:
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
972 973
			if ((container >> 28)) {
				container = (u32)-1;
974
				break;
975
			}
976
			channel = (container >> 24) & 0xF;
977 978
			if (channel >= dev->maximum_num_channels) {
				container = (u32)-1;
979
				break;
980
			}
981
			id = container & 0xFFFF;
982 983
			if (id >= dev->maximum_num_physicals) {
				container = (u32)-1;
984
				break;
985
			}
986
			lun = (container >> 16) & 0xFF;
987
			container = (u32)-1;
988 989 990 991
			channel = aac_phys_to_logical(channel);
			device_config_needed =
			  (((__le32 *)aifcmd->data)[0] ==
			    cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
992 993 994 995 996 997 998 999 1000 1001
			if (device_config_needed == ADD) {
				device = scsi_device_lookup(dev->scsi_host_ptr,
					channel,
					id,
					lun);
				if (device) {
					scsi_remove_device(device);
					scsi_device_put(device);
				}
			}
1002 1003
			break;

1004
		case AifEnEnclosureManagement:
1005 1006 1007 1008 1009 1010
			/*
			 * If in JBOD mode, automatic exposure of new
			 * physical target to be suppressed until configured.
			 */
			if (dev->jbod)
				break;
1011 1012 1013 1014 1015
			switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
			case EM_DRIVE_INSERTION:
			case EM_DRIVE_REMOVAL:
				container = le32_to_cpu(
					((__le32 *)aifcmd->data)[2]);
1016 1017
				if ((container >> 28)) {
					container = (u32)-1;
1018
					break;
1019
				}
1020
				channel = (container >> 24) & 0xF;
1021 1022
				if (channel >= dev->maximum_num_channels) {
					container = (u32)-1;
1023
					break;
1024
				}
1025 1026
				id = container & 0xFFFF;
				lun = (container >> 16) & 0xFF;
1027
				container = (u32)-1;
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
				if (id >= dev->maximum_num_physicals) {
					/* legacy dev_t ? */
					if ((0x2000 <= id) || lun || channel ||
					  ((channel = (id >> 7) & 0x3F) >=
					  dev->maximum_num_channels))
						break;
					lun = (id >> 4) & 7;
					id &= 0xF;
				}
				channel = aac_phys_to_logical(channel);
				device_config_needed =
				  (((__le32 *)aifcmd->data)[3]
				    == cpu_to_le32(EM_DRIVE_INSERTION)) ?
				  ADD : DELETE;
				break;
			}
			break;
1045 1046 1047 1048 1049 1050 1051 1052 1053
		}

		/*
		 *	If we are waiting on something and this happens to be
		 * that thing then set the re-configure flag.
		 */
		if (container != (u32)-1) {
			if (container >= dev->maximum_num_containers)
				break;
1054
			if ((dev->fsa_dev[container].config_waiting_on ==
1055
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1056
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1057 1058 1059
				dev->fsa_dev[container].config_waiting_on = 0;
		} else for (container = 0;
		    container < dev->maximum_num_containers; ++container) {
1060
			if ((dev->fsa_dev[container].config_waiting_on ==
1061
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1062
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
				dev->fsa_dev[container].config_waiting_on = 0;
		}
		break;

	case AifCmdJobProgress:
		/*
		 *	These are job progress AIF's. When a Clear is being
		 * done on a container it is initially created then hidden from
		 * the OS. When the clear completes we don't get a config
		 * change so we monitor the job status complete on a clear then
		 * wait for a container change.
		 */

1076 1077 1078
		if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
		    (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
		     ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
			for (container = 0;
			    container < dev->maximum_num_containers;
			    ++container) {
				/*
				 * Stomp on all config sequencing for all
				 * containers?
				 */
				dev->fsa_dev[container].config_waiting_on =
					AifEnContainerChange;
				dev->fsa_dev[container].config_needed = ADD;
1089 1090
				dev->fsa_dev[container].config_waiting_stamp =
					jiffies;
1091 1092
			}
		}
1093 1094 1095
		if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
		    ((__le32 *)aifcmd->data)[6] == 0 &&
		    ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
			for (container = 0;
			    container < dev->maximum_num_containers;
			    ++container) {
				/*
				 * Stomp on all config sequencing for all
				 * containers?
				 */
				dev->fsa_dev[container].config_waiting_on =
					AifEnContainerChange;
				dev->fsa_dev[container].config_needed = DELETE;
1106 1107
				dev->fsa_dev[container].config_waiting_stamp =
					jiffies;
1108 1109 1110 1111 1112
			}
		}
		break;
	}

1113 1114
	container = 0;
retry_next:
1115
	if (device_config_needed == NOTHING)
1116
	for (; container < dev->maximum_num_containers; ++container) {
1117 1118 1119
		if ((dev->fsa_dev[container].config_waiting_on == 0) &&
			(dev->fsa_dev[container].config_needed != NOTHING) &&
			time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
1120 1121 1122
			device_config_needed =
				dev->fsa_dev[container].config_needed;
			dev->fsa_dev[container].config_needed = NOTHING;
1123 1124 1125
			channel = CONTAINER_TO_CHANNEL(container);
			id = CONTAINER_TO_ID(container);
			lun = CONTAINER_TO_LUN(container);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
			break;
		}
	}
	if (device_config_needed == NOTHING)
		return;

	/*
	 *	If we decided that a re-configuration needs to be done,
	 * schedule it here on the way out the door, please close the door
	 * behind you.
	 */

	/*
1139
	 *	Find the scsi_device associated with the SCSI address,
1140 1141 1142 1143 1144 1145 1146
	 * and mark it as changed, invalidating the cache. This deals
	 * with changes to existing device IDs.
	 */

	if (!dev || !dev->scsi_host_ptr)
		return;
	/*
1147
	 * force reload of disk info via aac_probe_container
1148
	 */
1149 1150 1151 1152
	if ((channel == CONTAINER_CHANNEL) &&
	  (device_config_needed != NOTHING)) {
		if (dev->fsa_dev[container].valid == 1)
			dev->fsa_dev[container].valid = 2;
1153
		aac_probe_container(dev, container);
1154 1155
	}
	device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1156 1157 1158
	if (device) {
		switch (device_config_needed) {
		case DELETE:
1159 1160 1161
#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
			scsi_remove_device(device);
#else
1162 1163 1164 1165 1166 1167 1168 1169
			if (scsi_device_online(device)) {
				scsi_device_set_state(device, SDEV_OFFLINE);
				sdev_printk(KERN_INFO, device,
					"Device offlined - %s\n",
					(channel == CONTAINER_CHANNEL) ?
						"array deleted" :
						"enclosure services event");
			}
1170
#endif
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
			break;
		case ADD:
			if (!scsi_device_online(device)) {
				sdev_printk(KERN_INFO, device,
					"Device online - %s\n",
					(channel == CONTAINER_CHANNEL) ?
						"array created" :
						"enclosure services event");
				scsi_device_set_state(device, SDEV_RUNNING);
			}
			/* FALLTHRU */
1182
		case CHANGE:
1183 1184
			if ((channel == CONTAINER_CHANNEL)
			 && (!dev->fsa_dev[container].valid)) {
1185 1186 1187
#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
				scsi_remove_device(device);
#else
1188 1189 1190 1191 1192 1193
				if (!scsi_device_online(device))
					break;
				scsi_device_set_state(device, SDEV_OFFLINE);
				sdev_printk(KERN_INFO, device,
					"Device offlined - %s\n",
					"array failed");
1194
#endif
1195 1196
				break;
			}
1197 1198 1199 1200 1201 1202
			scsi_rescan_device(&device->sdev_gendev);

		default:
			break;
		}
		scsi_device_put(device);
1203
		device_config_needed = NOTHING;
1204
	}
1205 1206
	if (device_config_needed == ADD)
		scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1207 1208 1209 1210 1211
	if (channel == CONTAINER_CHANNEL) {
		container++;
		device_config_needed = NOTHING;
		goto retry_next;
	}
1212 1213
}

1214
static int _aac_reset_adapter(struct aac_dev *aac, int forced)
1215 1216 1217 1218 1219 1220 1221
{
	int index, quirks;
	int retval;
	struct Scsi_Host *host;
	struct scsi_device *dev;
	struct scsi_cmnd *command;
	struct scsi_cmnd *command_list;
1222
	int jafo = 0;
1223 1224 1225

	/*
	 * Assumptions:
1226 1227 1228
	 *	- host is locked, unless called by the aacraid thread.
	 *	  (a matter of convenience, due to legacy issues surrounding
	 *	  eh_host_adapter_reset).
1229 1230
	 *	- in_reset is asserted, so no new i/o is getting to the
	 *	  card.
1231 1232
	 *	- The card is dead, or will be very shortly ;-/ so no new
	 *	  commands are completing in the interrupt service.
1233 1234 1235 1236
	 */
	host = aac->scsi_host_ptr;
	scsi_block_requests(host);
	aac_adapter_disable_int(aac);
1237 1238 1239 1240 1241
	if (aac->thread->pid != current->pid) {
		spin_unlock_irq(host->host_lock);
		kthread_stop(aac->thread);
		jafo = 1;
	}
1242 1243 1244 1245 1246

	/*
	 *	If a positive health, means in a known DEAD PANIC
	 * state and the adapter could be reset to `try again'.
	 */
1247
	retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
1248 1249 1250 1251

	if (retval)
		goto out;

1252 1253 1254
	/*
	 *	Loop through the fibs, close the synchronous FIBS
	 */
1255
	for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
1256
		struct fib *fib = &aac->fibs[index];
1257 1258
		if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
		  (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
1259 1260 1261 1262 1263
			unsigned long flagv;
			spin_lock_irqsave(&fib->event_lock, flagv);
			up(&fib->event_wait);
			spin_unlock_irqrestore(&fib->event_lock, flagv);
			schedule();
1264
			retval = 0;
1265 1266
		}
	}
1267 1268 1269
	/* Give some extra time for ioctls to complete. */
	if (retval == 0)
		ssleep(2);
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	index = aac->cardtype;

	/*
	 * Re-initialize the adapter, first free resources, then carefully
	 * apply the initialization sequence to come back again. Only risk
	 * is a change in Firmware dropping cache, it is assumed the caller
	 * will ensure that i/o is queisced and the card is flushed in that
	 * case.
	 */
	aac_fib_map_free(aac);
	pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
	aac->comm_addr = NULL;
	aac->comm_phys = 0;
	kfree(aac->queues);
	aac->queues = NULL;
	free_irq(aac->pdev->irq, aac);
	kfree(aac->fsa_dev);
	aac->fsa_dev = NULL;
1288 1289
	quirks = aac_get_driver_ident(index)->quirks;
	if (quirks & AAC_QUIRK_31BIT) {
1290 1291
		if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
		  ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
1292 1293
			goto out;
	} else {
1294 1295
		if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
		  ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
1296 1297 1298 1299
			goto out;
	}
	if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
		goto out;
1300
	if (quirks & AAC_QUIRK_31BIT)
1301
		if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
1302
			goto out;
1303 1304 1305 1306 1307 1308
	if (jafo) {
		aac->thread = kthread_run(aac_command_thread, aac, aac->name);
		if (IS_ERR(aac->thread)) {
			retval = PTR_ERR(aac->thread);
			goto out;
		}
1309 1310 1311
	}
	(void)aac_get_adapter_info(aac);
	if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1312 1313 1314 1315 1316 1317 1318
		host->sg_tablesize = 34;
		host->max_sectors = (host->sg_tablesize * 8) + 112;
	}
	if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
		host->sg_tablesize = 17;
		host->max_sectors = (host->sg_tablesize * 8) + 112;
	}
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	aac_get_config_status(aac, 1);
	aac_get_containers(aac);
	/*
	 * This is where the assumption that the Adapter is quiesced
	 * is important.
	 */
	command_list = NULL;
	__shost_for_each_device(dev, host) {
		unsigned long flags;
		spin_lock_irqsave(&dev->list_lock, flags);
		list_for_each_entry(command, &dev->cmd_list, list)
			if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
				command->SCp.buffer = (struct scatterlist *)command_list;
				command_list = command;
			}
		spin_unlock_irqrestore(&dev->list_lock, flags);
	}
	while ((command = command_list)) {
		command_list = (struct scsi_cmnd *)command->SCp.buffer;
		command->SCp.buffer = NULL;
		command->result = DID_OK << 16
		  | COMMAND_COMPLETE << 8
		  | SAM_STAT_TASK_SET_FULL;
		command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
		command->scsi_done(command);
	}
	retval = 0;

out:
	aac->in_reset = 0;
	scsi_unblock_requests(host);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	if (jafo) {
		spin_lock_irq(host->host_lock);
	}
	return retval;
}

int aac_reset_adapter(struct aac_dev * aac, int forced)
{
	unsigned long flagv = 0;
	int retval;
	struct Scsi_Host * host;

	if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
		return -EBUSY;

	if (aac->in_reset) {
		spin_unlock_irqrestore(&aac->fib_lock, flagv);
		return -EBUSY;
	}
	aac->in_reset = 1;
	spin_unlock_irqrestore(&aac->fib_lock, flagv);

	/*
	 * Wait for all commands to complete to this specific
	 * target (block maximum 60 seconds). Although not necessary,
	 * it does make us a good storage citizen.
	 */
	host = aac->scsi_host_ptr;
	scsi_block_requests(host);
	if (forced < 2) for (retval = 60; retval; --retval) {
		struct scsi_device * dev;
		struct scsi_cmnd * command;
		int active = 0;

		__shost_for_each_device(dev, host) {
			spin_lock_irqsave(&dev->list_lock, flagv);
			list_for_each_entry(command, &dev->cmd_list, list) {
				if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
					active++;
					break;
				}
			}
			spin_unlock_irqrestore(&dev->list_lock, flagv);
			if (active)
				break;

		}
		/*
		 * We can exit If all the commands are complete
		 */
		if (active == 0)
			break;
		ssleep(1);
	}

	/* Quiesce build, flush cache, write through mode */
1406 1407
	if (forced < 2)
		aac_send_shutdown(aac);
1408
	spin_lock_irqsave(host->host_lock, flagv);
1409
	retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
1410 1411
	spin_unlock_irqrestore(host->host_lock, flagv);

1412
	if ((forced < 2) && (retval == -ENODEV)) {
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
		/* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
		struct fib * fibctx = aac_fib_alloc(aac);
		if (fibctx) {
			struct aac_pause *cmd;
			int status;

			aac_fib_init(fibctx);

			cmd = (struct aac_pause *) fib_data(fibctx);

			cmd->command = cpu_to_le32(VM_ContainerConfig);
			cmd->type = cpu_to_le32(CT_PAUSE_IO);
			cmd->timeout = cpu_to_le32(1);
			cmd->min = cpu_to_le32(1);
			cmd->noRescan = cpu_to_le32(1);
			cmd->count = cpu_to_le32(0);

			status = aac_fib_send(ContainerCommand,
			  fibctx,
			  sizeof(struct aac_pause),
			  FsaNormal,
			  -2 /* Timeout silently */, 1,
			  NULL, NULL);

			if (status >= 0)
				aac_fib_complete(fibctx);
1439 1440 1441 1442
			/* FIB should be freed only after getting
			 * the response from the F/W */
			if (status != -ERESTARTSYS)
				aac_fib_free(fibctx);
1443 1444 1445
		}
	}

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	return retval;
}

int aac_check_health(struct aac_dev * aac)
{
	int BlinkLED;
	unsigned long time_now, flagv = 0;
	struct list_head * entry;
	struct Scsi_Host * host;

	/* Extending the scope of fib_lock slightly to protect aac->in_reset */
	if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
		return 0;

	if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
		spin_unlock_irqrestore(&aac->fib_lock, flagv);
		return 0; /* OK */
	}

	aac->in_reset = 1;

	/* Fake up an AIF:
	 *	aac_aifcmd.command = AifCmdEventNotify = 1
	 *	aac_aifcmd.seqnum = 0xFFFFFFFF
	 *	aac_aifcmd.data[0] = AifEnExpEvent = 23
	 *	aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
	 *	aac.aifcmd.data[2] = AifHighPriority = 3
	 *	aac.aifcmd.data[3] = BlinkLED
	 */

	time_now = jiffies/HZ;
	entry = aac->fib_list.next;

	/*
	 * For each Context that is on the
	 * fibctxList, make a copy of the
	 * fib, and then set the event to wake up the
	 * thread that is waiting for it.
	 */
	while (entry != &aac->fib_list) {
		/*
		 * Extract the fibctx
		 */
		struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
		struct hw_fib * hw_fib;
		struct fib * fib;
		/*
		 * Check if the queue is getting
		 * backlogged
		 */
		if (fibctx->count > 20) {
			/*
			 * It's *not* jiffies folks,
			 * but jiffies / HZ, so do not
			 * panic ...
			 */
			u32 time_last = fibctx->jiffies;
			/*
			 * Has it been > 2 minutes
			 * since the last read off
			 * the queue?
			 */
			if ((time_now - time_last) > aif_timeout) {
				entry = entry->next;
				aac_close_fib_context(aac, fibctx);
				continue;
			}
		}
		/*
		 * Warning: no sleep allowed while
		 * holding spinlock
		 */
1518 1519
		hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
		fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1520 1521 1522
		if (fib && hw_fib) {
			struct aac_aifcmd * aif;

1523
			fib->hw_fib_va = hw_fib;
1524 1525 1526 1527 1528 1529 1530
			fib->dev = aac;
			aac_fib_init(fib);
			fib->type = FSAFS_NTC_FIB_CONTEXT;
			fib->size = sizeof (struct fib);
			fib->data = hw_fib->data;
			aif = (struct aac_aifcmd *)hw_fib->data;
			aif->command = cpu_to_le32(AifCmdEventNotify);
1531 1532 1533 1534 1535
			aif->seqnum = cpu_to_le32(0xFFFFFFFF);
			((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
			((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
			((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
			((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564

			/*
			 * Put the FIB onto the
			 * fibctx's fibs
			 */
			list_add_tail(&fib->fiblink, &fibctx->fib_list);
			fibctx->count++;
			/*
			 * Set the event to wake up the
			 * thread that will waiting.
			 */
			up(&fibctx->wait_sem);
		} else {
			printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
			kfree(fib);
			kfree(hw_fib);
		}
		entry = entry->next;
	}

	spin_unlock_irqrestore(&aac->fib_lock, flagv);

	if (BlinkLED < 0) {
		printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
		goto out;
	}

	printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);

1565
	if (!aac_check_reset || ((aac_check_reset == 1) &&
1566 1567
		(aac->supplement_adapter_info.SupportedOptions2 &
			AAC_OPTION_IGNORE_RESET)))
1568
		goto out;
1569
	host = aac->scsi_host_ptr;
1570 1571
	if (aac->thread->pid != current->pid)
		spin_lock_irqsave(host->host_lock, flagv);
1572
	BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
1573 1574
	if (aac->thread->pid != current->pid)
		spin_unlock_irqrestore(host->host_lock, flagv);
1575 1576 1577 1578 1579 1580 1581 1582
	return BlinkLED;

out:
	aac->in_reset = 0;
	return BlinkLED;
}


L
Linus Torvalds 已提交
1583 1584 1585 1586 1587 1588 1589 1590 1591
/**
 *	aac_command_thread	-	command processing thread
 *	@dev: Adapter to monitor
 *
 *	Waits on the commandready event in it's queue. When the event gets set
 *	it will pull FIBs off it's queue. It will continue to pull FIBs off
 *	until the queue is empty. When the queue is empty it will wait for
 *	more FIBs.
 */
1592

1593
int aac_command_thread(void *data)
L
Linus Torvalds 已提交
1594
{
1595
	struct aac_dev *dev = data;
L
Linus Torvalds 已提交
1596 1597 1598 1599 1600
	struct hw_fib *hw_fib, *hw_newfib;
	struct fib *fib, *newfib;
	struct aac_fib_context *fibctx;
	unsigned long flags;
	DECLARE_WAITQUEUE(wait, current);
1601 1602 1603
	unsigned long next_jiffies = jiffies + HZ;
	unsigned long next_check_jiffies = next_jiffies;
	long difference = HZ;
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609

	/*
	 *	We can only have one thread per adapter for AIF's.
	 */
	if (dev->aif_thread)
		return -EINVAL;
1610

L
Linus Torvalds 已提交
1611 1612 1613 1614
	/*
	 *	Let the DPC know it has a place to send the AIF's to.
	 */
	dev->aif_thread = 1;
1615
	add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
L
Linus Torvalds 已提交
1616
	set_current_state(TASK_INTERRUPTIBLE);
1617
	dprintk ((KERN_INFO "aac_command_thread start\n"));
1618
	while (1) {
1619 1620
		spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
		while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
L
Linus Torvalds 已提交
1621 1622 1623 1624
			struct list_head *entry;
			struct aac_aifcmd * aifcmd;

			set_current_state(TASK_RUNNING);
1625

1626
			entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
L
Linus Torvalds 已提交
1627
			list_del(entry);
1628

1629
			spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
L
Linus Torvalds 已提交
1630 1631
			fib = list_entry(entry, struct fib, fiblink);
			/*
1632 1633
			 *	We will process the FIB here or pass it to a
			 *	worker thread that is TBD. We Really can't
L
Linus Torvalds 已提交
1634 1635 1636
			 *	do anything at this point since we don't have
			 *	anything defined for this thread to do.
			 */
1637
			hw_fib = fib->hw_fib_va;
L
Linus Torvalds 已提交
1638 1639
			memset(fib, 0, sizeof(struct fib));
			fib->type = FSAFS_NTC_FIB_CONTEXT;
1640
			fib->size = sizeof(struct fib);
1641
			fib->hw_fib_va = hw_fib;
L
Linus Torvalds 已提交
1642 1643 1644 1645 1646 1647 1648 1649
			fib->data = hw_fib->data;
			fib->dev = dev;
			/*
			 *	We only handle AifRequest fibs from the adapter.
			 */
			aifcmd = (struct aac_aifcmd *) hw_fib->data;
			if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
				/* Handle Driver Notify Events */
1650
				aac_handle_aif(dev, fib);
1651
				*(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1652
				aac_fib_adapter_complete(fib, (u16)sizeof(u32));
L
Linus Torvalds 已提交
1653 1654 1655
			} else {
				/* The u32 here is important and intended. We are using
				   32bit wrapping time to fit the adapter field */
1656

L
Linus Torvalds 已提交
1657 1658
				u32 time_now, time_last;
				unsigned long flagv;
1659 1660 1661
				unsigned num;
				struct hw_fib ** hw_fib_pool, ** hw_fib_p;
				struct fib ** fib_pool, ** fib_p;
1662

1663
				/* Sniff events */
1664
				if ((aifcmd->command ==
1665
				     cpu_to_le32(AifCmdEventNotify)) ||
1666
				    (aifcmd->command ==
1667 1668 1669
				     cpu_to_le32(AifCmdJobProgress))) {
					aac_handle_aif(dev, fib);
				}
1670

L
Linus Torvalds 已提交
1671 1672
				time_now = jiffies/HZ;

1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
				/*
				 * Warning: no sleep allowed while
				 * holding spinlock. We take the estimate
				 * and pre-allocate a set of fibs outside the
				 * lock.
				 */
				num = le32_to_cpu(dev->init->AdapterFibsSize)
				    / sizeof(struct hw_fib); /* some extra */
				spin_lock_irqsave(&dev->fib_lock, flagv);
				entry = dev->fib_list.next;
				while (entry != &dev->fib_list) {
					entry = entry->next;
					++num;
				}
				spin_unlock_irqrestore(&dev->fib_lock, flagv);
				hw_fib_pool = NULL;
				fib_pool = NULL;
				if (num
				 && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
				 && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
					hw_fib_p = hw_fib_pool;
					fib_p = fib_pool;
					while (hw_fib_p < &hw_fib_pool[num]) {
						if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
							--hw_fib_p;
							break;
						}
						if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
							kfree(*(--hw_fib_p));
							break;
						}
					}
					if ((num = hw_fib_p - hw_fib_pool) == 0) {
						kfree(fib_pool);
						fib_pool = NULL;
						kfree(hw_fib_pool);
						hw_fib_pool = NULL;
					}
J
Jesper Juhl 已提交
1711
				} else {
1712 1713 1714
					kfree(hw_fib_pool);
					hw_fib_pool = NULL;
				}
L
Linus Torvalds 已提交
1715 1716 1717
				spin_lock_irqsave(&dev->fib_lock, flagv);
				entry = dev->fib_list.next;
				/*
1718
				 * For each Context that is on the
L
Linus Torvalds 已提交
1719 1720 1721 1722
				 * fibctxList, make a copy of the
				 * fib, and then set the event to wake up the
				 * thread that is waiting for it.
				 */
1723 1724
				hw_fib_p = hw_fib_pool;
				fib_p = fib_pool;
L
Linus Torvalds 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
				while (entry != &dev->fib_list) {
					/*
					 * Extract the fibctx
					 */
					fibctx = list_entry(entry, struct aac_fib_context, next);
					/*
					 * Check if the queue is getting
					 * backlogged
					 */
					if (fibctx->count > 20)
					{
						/*
						 * It's *not* jiffies folks,
						 * but jiffies / HZ so do not
						 * panic ...
						 */
						time_last = fibctx->jiffies;
						/*
1743
						 * Has it been > 2 minutes
L
Linus Torvalds 已提交
1744 1745 1746
						 * since the last read off
						 * the queue?
						 */
1747
						if ((time_now - time_last) > aif_timeout) {
L
Linus Torvalds 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756
							entry = entry->next;
							aac_close_fib_context(dev, fibctx);
							continue;
						}
					}
					/*
					 * Warning: no sleep allowed while
					 * holding spinlock
					 */
1757 1758 1759 1760 1761
					if (hw_fib_p < &hw_fib_pool[num]) {
						hw_newfib = *hw_fib_p;
						*(hw_fib_p++) = NULL;
						newfib = *fib_p;
						*(fib_p++) = NULL;
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766
						/*
						 * Make the copy of the FIB
						 */
						memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
						memcpy(newfib, fib, sizeof(struct fib));
1767
						newfib->hw_fib_va = hw_newfib;
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773
						/*
						 * Put the FIB onto the
						 * fibctx's fibs
						 */
						list_add_tail(&newfib->fiblink, &fibctx->fib_list);
						fibctx->count++;
1774
						/*
L
Linus Torvalds 已提交
1775
						 * Set the event to wake up the
1776
						 * thread that is waiting.
L
Linus Torvalds 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
						 */
						up(&fibctx->wait_sem);
					} else {
						printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
					}
					entry = entry->next;
				}
				/*
				 *	Set the status of this FIB
				 */
1787
				*(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
1788
				aac_fib_adapter_complete(fib, sizeof(u32));
L
Linus Torvalds 已提交
1789
				spin_unlock_irqrestore(&dev->fib_lock, flagv);
1790 1791 1792 1793
				/* Free up the remaining resources */
				hw_fib_p = hw_fib_pool;
				fib_p = fib_pool;
				while (hw_fib_p < &hw_fib_pool[num]) {
J
Jesper Juhl 已提交
1794 1795
					kfree(*hw_fib_p);
					kfree(*fib_p);
1796 1797 1798
					++fib_p;
					++hw_fib_p;
				}
J
Jesper Juhl 已提交
1799 1800
				kfree(hw_fib_pool);
				kfree(fib_pool);
L
Linus Torvalds 已提交
1801 1802
			}
			kfree(fib);
1803
			spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
L
Linus Torvalds 已提交
1804 1805 1806 1807
		}
		/*
		 *	There are no more AIF's
		 */
1808
		spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845

		/*
		 *	Background activity
		 */
		if ((time_before(next_check_jiffies,next_jiffies))
		 && ((difference = next_check_jiffies - jiffies) <= 0)) {
			next_check_jiffies = next_jiffies;
			if (aac_check_health(dev) == 0) {
				difference = ((long)(unsigned)check_interval)
					   * HZ;
				next_check_jiffies = jiffies + difference;
			} else if (!dev->queues)
				break;
		}
		if (!time_before(next_check_jiffies,next_jiffies)
		 && ((difference = next_jiffies - jiffies) <= 0)) {
			struct timeval now;
			int ret;

			/* Don't even try to talk to adapter if its sick */
			ret = aac_check_health(dev);
			if (!ret && !dev->queues)
				break;
			next_check_jiffies = jiffies
					   + ((long)(unsigned)check_interval)
					   * HZ;
			do_gettimeofday(&now);

			/* Synchronize our watches */
			if (((1000000 - (1000000 / HZ)) > now.tv_usec)
			 && (now.tv_usec > (1000000 / HZ)))
				difference = (((1000000 - now.tv_usec) * HZ)
				  + 500000) / 1000000;
			else if (ret == 0) {
				struct fib *fibptr;

				if ((fibptr = aac_fib_alloc(dev))) {
1846
					int status;
1847
					__le32 *info;
1848 1849 1850

					aac_fib_init(fibptr);

1851
					info = (__le32 *) fib_data(fibptr);
1852 1853 1854 1855 1856
					if (now.tv_usec > 500000)
						++now.tv_sec;

					*info = cpu_to_le32(now.tv_sec);

1857
					status = aac_fib_send(SendHostTime,
1858 1859 1860 1861 1862 1863
						fibptr,
						sizeof(*info),
						FsaNormal,
						1, 1,
						NULL,
						NULL);
1864 1865 1866 1867 1868 1869 1870 1871
					/* Do not set XferState to zero unless
					 * receives a response from F/W */
					if (status >= 0)
						aac_fib_complete(fibptr);
					/* FIB should be freed only after
					 * getting the response from the F/W */
					if (status != -ERESTARTSYS)
						aac_fib_free(fibptr);
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
				}
				difference = (long)(unsigned)update_interval*HZ;
			} else {
				/* retry shortly */
				difference = 10 * HZ;
			}
			next_jiffies = jiffies + difference;
			if (time_before(next_check_jiffies,next_jiffies))
				difference = next_check_jiffies - jiffies;
		}
		if (difference <= 0)
			difference = 1;
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(difference);
L
Linus Torvalds 已提交
1886

1887
		if (kthread_should_stop())
L
Linus Torvalds 已提交
1888 1889
			break;
	}
1890 1891
	if (dev->queues)
		remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
L
Linus Torvalds 已提交
1892
	dev->aif_thread = 0;
1893
	return 0;
L
Linus Torvalds 已提交
1894
}