commsup.c 68.7 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
 * Copyright (c) 2000-2010 Adaptec, Inc.
9 10
 *               2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
 *		 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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
30
 *    communication.
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42
 *
 */

#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 已提交
43
#include <linux/delay.h>
44
#include <linux/kthread.h>
45
#include <linux/interrupt.h>
46
#include <linux/semaphore.h>
47
#include <linux/bcd.h>
48
#include <scsi/scsi.h>
49
#include <scsi/scsi_host.h>
50
#include <scsi/scsi_device.h>
51
#include <scsi/scsi_cmnd.h>
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61

#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.
 */
62

L
Linus Torvalds 已提交
63 64
static int fib_map_alloc(struct aac_dev *dev)
{
65 66 67 68
	if (dev->max_fib_size > AAC_MAX_NATIVE_SIZE)
		dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
	else
		dev->max_cmd_size = dev->max_fib_size;
69 70 71 72 73
	if (dev->max_fib_size < AAC_MAX_NATIVE_SIZE) {
		dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
	} else {
		dev->max_cmd_size = dev->max_fib_size;
	}
74

75
	dprintk((KERN_INFO
76 77
	  "allocate hardware fibs dma_alloc_coherent(%p, %d * (%d + %d), %p)\n",
	  &dev->pdev->dev, dev->max_cmd_size, dev->scsi_host_ptr->can_queue,
78
	  AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
79
	dev->hw_fib_va = dma_alloc_coherent(&dev->pdev->dev,
80
		(dev->max_cmd_size + sizeof(struct aac_fib_xporthdr))
81
		* (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
82
		&dev->hw_fib_pa, GFP_KERNEL);
83
	if (dev->hw_fib_va == NULL)
L
Linus Torvalds 已提交
84 85 86 87 88
		return -ENOMEM;
	return 0;
}

/**
89
 *	aac_fib_map_free		-	free the fib objects
L
Linus Torvalds 已提交
90 91 92 93 94 95
 *	@dev: Adapter to free
 *
 *	Free the PCI mappings and the memory allocated for FIB blocks
 *	on this adapter.
 */

96
void aac_fib_map_free(struct aac_dev *dev)
L
Linus Torvalds 已提交
97
{
98 99 100 101 102 103 104 105 106 107 108
	size_t alloc_size;
	size_t fib_size;
	int num_fibs;

	if(!dev->hw_fib_va || !dev->max_cmd_size)
		return;

	num_fibs = dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
	fib_size = dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
	alloc_size = fib_size * num_fibs + ALIGN32 - 1;

109 110
	dma_free_coherent(&dev->pdev->dev, alloc_size, dev->hw_fib_va,
			  dev->hw_fib_pa);
111

112 113
	dev->hw_fib_va = NULL;
	dev->hw_fib_pa = 0;
L
Linus Torvalds 已提交
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
void aac_fib_vector_assign(struct aac_dev *dev)
{
	u32 i = 0;
	u32 vector = 1;
	struct fib *fibptr = NULL;

	for (i = 0, fibptr = &dev->fibs[i];
		i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
		i++, fibptr++) {
		if ((dev->max_msix == 1) ||
		  (i > ((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1)
			- dev->vector_cap))) {
			fibptr->vector_no = 0;
		} else {
			fibptr->vector_no = vector;
			vector++;
			if (vector == dev->max_msix)
				vector = 1;
		}
	}
}

L
Linus Torvalds 已提交
138
/**
139
 *	aac_fib_setup	-	setup the fibs
L
Linus Torvalds 已提交
140 141
 *	@dev: Adapter to set up
 *
142
 *	Allocate the PCI space for the fibs, map it and then initialise the
L
Linus Torvalds 已提交
143 144 145
 *	fib area, the unmapped fib data and also the free list
 */

146
int aac_fib_setup(struct aac_dev * dev)
L
Linus Torvalds 已提交
147 148
{
	struct fib *fibptr;
149
	struct hw_fib *hw_fib;
L
Linus Torvalds 已提交
150 151
	dma_addr_t hw_fib_pa;
	int i;
152
	u32 max_cmds;
153 154 155

	while (((i = fib_map_alloc(dev)) == -ENOMEM)
	 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
156 157 158 159
		max_cmds = (dev->scsi_host_ptr->can_queue+AAC_NUM_MGT_FIB) >> 1;
		dev->scsi_host_ptr->can_queue = max_cmds - AAC_NUM_MGT_FIB;
		if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3)
			dev->init->r7.max_io_commands = cpu_to_le32(max_cmds);
160 161
	}
	if (i<0)
L
Linus Torvalds 已提交
162
		return -ENOMEM;
163

164
	memset(dev->hw_fib_va, 0,
165
		(dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) *
166 167
		(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));

168 169 170 171 172
	/* 32 byte alignment for PMC */
	hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
	hw_fib    = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
					(hw_fib_pa - dev->hw_fib_pa));

173
	/* add Xport header */
174
	hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
175
		sizeof(struct aac_fib_xporthdr));
176
	hw_fib_pa += sizeof(struct aac_fib_xporthdr);
177

L
Linus Torvalds 已提交
178 179 180
	/*
	 *	Initialise the fibs
	 */
181 182 183
	for (i = 0, fibptr = &dev->fibs[i];
		i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
		i++, fibptr++)
L
Linus Torvalds 已提交
184
	{
185
		fibptr->flags = 0;
186
		fibptr->size = sizeof(struct fib);
L
Linus Torvalds 已提交
187
		fibptr->dev = dev;
188 189
		fibptr->hw_fib_va = hw_fib;
		fibptr->data = (void *) fibptr->hw_fib_va->data;
L
Linus Torvalds 已提交
190
		fibptr->next = fibptr+1;	/* Forward chain the fibs */
191
		sema_init(&fibptr->event_wait, 0);
L
Linus Torvalds 已提交
192
		spin_lock_init(&fibptr->event_lock);
193
		hw_fib->header.XferState = cpu_to_le32(0xffffffff);
194 195
		hw_fib->header.SenderSize =
			cpu_to_le16(dev->max_fib_size);	/* ?? max_cmd_size */
L
Linus Torvalds 已提交
196
		fibptr->hw_fib_pa = hw_fib_pa;
197 198 199 200 201 202 203 204 205
		fibptr->hw_sgl_pa = hw_fib_pa +
			offsetof(struct aac_hba_cmd_req, sge[2]);
		/*
		 * one element is for the ptr to the separate sg list,
		 * second element for 32 byte alignment
		 */
		fibptr->hw_error_pa = hw_fib_pa +
			offsetof(struct aac_native_hba, resp.resp_bytes[0]);

206
		hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
207
			dev->max_cmd_size + sizeof(struct aac_fib_xporthdr));
208
		hw_fib_pa = hw_fib_pa +
209
			dev->max_cmd_size + sizeof(struct aac_fib_xporthdr);
L
Linus Torvalds 已提交
210
	}
211 212 213 214 215 216

	/*
	 *Assign vector numbers to fibs
	 */
	aac_fib_vector_assign(dev);

L
Linus Torvalds 已提交
217 218 219
	/*
	 *	Add the fib chain to the free list
	 */
220
	dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
L
Linus Torvalds 已提交
221
	/*
222 223 224
	*	Set 8 fibs aside for management tools
	*/
	dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue];
L
Linus Torvalds 已提交
225 226 227
	return 0;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/**
 *	aac_fib_alloc_tag-allocate a fib using tags
 *	@dev: Adapter to allocate the fib for
 *
 *	Allocate a fib from the adapter fib pool using tags
 *	from the blk layer.
 */

struct fib *aac_fib_alloc_tag(struct aac_dev *dev, struct scsi_cmnd *scmd)
{
	struct fib *fibptr;

	fibptr = &dev->fibs[scmd->request->tag];
	/*
	 *	Null out fields that depend on being zero at the start of
	 *	each I/O
	 */
	fibptr->hw_fib_va->header.XferState = 0;
	fibptr->type = FSAFS_NTC_FIB_CONTEXT;
	fibptr->callback_data = NULL;
	fibptr->callback = NULL;

	return fibptr;
}

L
Linus Torvalds 已提交
253
/**
254
 *	aac_fib_alloc	-	allocate a fib
L
Linus Torvalds 已提交
255 256 257
 *	@dev: Adapter to allocate the fib for
 *
 *	Allocate a fib from the adapter fib pool. If the pool is empty we
258
 *	return NULL.
L
Linus Torvalds 已提交
259
 */
260

261
struct fib *aac_fib_alloc(struct aac_dev *dev)
L
Linus Torvalds 已提交
262 263 264 265
{
	struct fib * fibptr;
	unsigned long flags;
	spin_lock_irqsave(&dev->fib_lock, flags);
266
	fibptr = dev->free_fib;
267 268 269 270
	if(!fibptr){
		spin_unlock_irqrestore(&dev->fib_lock, flags);
		return fibptr;
	}
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281
	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
	 */
282
	fibptr->hw_fib_va->header.XferState = 0;
283
	fibptr->flags = 0;
L
Linus Torvalds 已提交
284 285 286 287 288 289 290
	fibptr->callback = NULL;
	fibptr->callback_data = NULL;

	return fibptr;
}

/**
291
 *	aac_fib_free	-	free a fib
L
Linus Torvalds 已提交
292 293 294 295
 *	@fibptr: fib to free up
 *
 *	Frees up a fib and places it on the appropriate queue
 */
296

297
void aac_fib_free(struct fib *fibptr)
L
Linus Torvalds 已提交
298
{
299
	unsigned long flags;
300

301
	if (fibptr->done == 2)
302
		return;
L
Linus Torvalds 已提交
303 304

	spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
305
	if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
L
Linus Torvalds 已提交
306
		aac_config.fib_timeouts++;
307 308
	if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) &&
		fibptr->hw_fib_va->header.XferState != 0) {
309 310 311 312 313 314
		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 已提交
315 316 317 318
	spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
}

/**
319
 *	aac_fib_init	-	initialise a fib
L
Linus Torvalds 已提交
320
 *	@fibptr: The fib to initialize
321
 *
L
Linus Torvalds 已提交
322 323
 *	Set up the generic fib fields ready for use
 */
324

325
void aac_fib_init(struct fib *fibptr)
L
Linus Torvalds 已提交
326
{
327
	struct hw_fib *hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
328

329
	memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
L
Linus Torvalds 已提交
330
	hw_fib->header.StructType = FIB_MAGIC;
331 332
	hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
	hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
333
	hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
334
	hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343
}

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

345
static void fib_dealloc(struct fib * fibptr)
L
Linus Torvalds 已提交
346
{
347
	struct hw_fib *hw_fib = fibptr->hw_fib_va;
348
	hw_fib->header.XferState = 0;
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356
}

/*
 *	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.
 */
357

L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369
/**
 *	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.
 */
370

L
Linus Torvalds 已提交
371 372 373
static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
{
	struct aac_queue * q;
374
	unsigned long idx;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383

	/*
	 *	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];
384 385 386 387 388

	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) {
389
			if (qid == AdapNormCmdQueue)
390
				idx = ADAP_NORM_CMD_ENTRIES;
391
			else
392 393 394
				idx = ADAP_NORM_RESP_ENTRIES;
		}
		if (idx != le32_to_cpu(*(q->headers.consumer)))
395
			*nonotify = 1;
396
	}
L
Linus Torvalds 已提交
397

398
	if (qid == AdapNormCmdQueue) {
399
		if (*index >= ADAP_NORM_CMD_ENTRIES)
L
Linus Torvalds 已提交
400
			*index = 0; /* Wrap to front of the Producer Queue. */
401
	} else {
402
		if (*index >= ADAP_NORM_RESP_ENTRIES)
L
Linus Torvalds 已提交
403 404 405
			*index = 0; /* Wrap to front of the Producer Queue. */
	}

406 407
	/* Queue is full */
	if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
408
		printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
409
				qid, atomic_read(&q->numpending));
L
Linus Torvalds 已提交
410 411
		return 0;
	} else {
412
		*entry = q->base + *index;
L
Linus Torvalds 已提交
413 414
		return 1;
	}
415
}
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

/**
 *	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.
 */

433
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 已提交
434 435 436
{
	struct aac_entry * entry = NULL;
	int map = 0;
437

438
	if (qid == AdapNormCmdQueue) {
L
Linus Torvalds 已提交
439
		/*  if no entries wait for some if caller wants to */
440
		while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
L
Linus Torvalds 已提交
441 442
			printk(KERN_ERR "GetEntries failed\n");
		}
443 444 445 446 447
		/*
		 *	Setup queue entry with a command, status and fib mapped
		 */
		entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
		map = 1;
448
	} else {
449
		while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
L
Linus Torvalds 已提交
450 451
			/* if no entries wait for some if caller wants to */
		}
452 453 454 455 456 457
		/*
		 *	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 */
458
		hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress;  /* Let the adapter now where to find its data */
459
		map = 0;
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468 469
	}
	/*
	 *	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;
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
#ifdef CONFIG_EEH
static inline int aac_check_eeh_failure(struct aac_dev *dev)
{
	/* Check for an EEH failure for the given
	 * device node. Function eeh_dev_check_failure()
	 * returns 0 if there has not been an EEH error
	 * otherwise returns a non-zero value.
	 *
	 * Need to be called before any PCI operation,
	 * i.e.,before aac_adapter_check_health()
	 */
	struct eeh_dev *edev = pci_dev_to_eeh_dev(dev->pdev);

	if (eeh_dev_check_failure(edev)) {
		/* The EEH mechanisms will handle this
		 * error and reset the device if
		 * necessary.
		 */
		return 1;
	}
	return 0;
}
#else
static inline int aac_check_eeh_failure(struct aac_dev *dev)
{
	return 0;
}
#endif

L
Linus Torvalds 已提交
499
/*
500 501
 *	Define the highest level of host to adapter communication routines.
 *	These routines will support host to adapter FS commuication. These
L
Linus Torvalds 已提交
502 503 504 505 506 507
 *	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.
 */

/**
508
 *	aac_fib_send	-	send a fib to the adapter
L
Linus Torvalds 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522
 *	@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.
 */
523

524 525 526
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 已提交
527 528
{
	struct aac_dev * dev = fibptr->dev;
529
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
530
	unsigned long flags = 0;
531
	unsigned long mflags = 0;
532
	unsigned long sflags = 0;
533

L
Linus Torvalds 已提交
534 535
	if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
		return -EBUSY;
536 537 538 539

	if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))
		return -EINVAL;

L
Linus Torvalds 已提交
540
	/*
L
Lucas De Marchi 已提交
541
	 *	There are 5 cases with the wait and response requested flags.
L
Linus Torvalds 已提交
542 543 544 545 546
	 *	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
547
	 *	further processing will be done besides deleting the Fib. We
L
Linus Torvalds 已提交
548 549 550
	 *	will have a debug mode where the adapter can notify the host
	 *	it had a problem and the host can log that fact.
	 */
551
	fibptr->flags = 0;
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559 560 561 562
	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);
563
	}
L
Linus Torvalds 已提交
564 565 566 567
	/*
	 *	Map the fib into 32bits by using the fib number
	 */

568 569 570 571 572 573 574 575 576
	hw_fib->header.SenderFibAddress =
		cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);

	/* use the same shifted value for handle to be compatible
	 * with the new native hba command handle
	 */
	hw_fib->header.Handle =
		cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);

L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	/*
	 *	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);
	/*
	 *	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;
592
	}
L
Linus Torvalds 已提交
593 594 595 596
	/*
	 *	Get a queue entry connect the FIB to it and send an notify
	 *	the adapter a command is ready.
	 */
597
	hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605

	/*
	 *	Fill in the Callback and CallbackContext if we are not
	 *	going to wait.
	 */
	if (!wait) {
		fibptr->callback = callback;
		fibptr->callback_data = callback_data;
606
		fibptr->flags = FIB_CONTEXT_FLAG;
L
Linus Torvalds 已提交
607 608 609 610
	}

	fibptr->done = 0;

611 612 613
	FIB_COUNTER_INCREMENT(aac_config.FibsSent);

	dprintk((KERN_DEBUG "Fib contents:.\n"));
614 615 616
	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)));
617
	dprintk((KERN_DEBUG "  hw_fib va being sent=%p\n",fibptr->hw_fib_va));
618 619 620
	dprintk((KERN_DEBUG "  hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
	dprintk((KERN_DEBUG "  fib being sent=%p\n",fibptr));

621
	if (!dev->queues)
M
Mark Haverkamp 已提交
622
		return -EBUSY;
623

624 625 626 627 628 629 630 631 632 633 634
	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);
635
		spin_lock_irqsave(&fibptr->event_lock, flags);
636 637
	}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	if (dev->sync_mode) {
		if (wait)
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
		spin_lock_irqsave(&dev->sync_lock, sflags);
		if (dev->sync_fib) {
			list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
			spin_unlock_irqrestore(&dev->sync_lock, sflags);
		} else {
			dev->sync_fib = fibptr;
			spin_unlock_irqrestore(&dev->sync_lock, sflags);
			aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
				(u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
				NULL, NULL, NULL, NULL, NULL);
		}
		if (wait) {
			fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
			if (down_interruptible(&fibptr->event_wait)) {
				fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
				return -EFAULT;
			}
			return 0;
		}
		return -EINPROGRESS;
	}

663 664 665 666 667 668 669 670 671 672 673
	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;
	}

674

L
Linus Torvalds 已提交
675
	/*
676
	 *	If the caller wanted us to wait for response wait now.
L
Linus Torvalds 已提交
677
	 */
678

L
Linus Torvalds 已提交
679 680
	if (wait) {
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
681 682 683 684 685 686 687 688
		/* 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.
			 */
689
			unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
690
			while (down_trylock(&fibptr->event_wait)) {
691
				int blink;
692
				if (time_is_before_eq_jiffies(timeout)) {
693
					struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
694
					atomic_dec(&q->numpending);
695
					if (wait == -1) {
696
	        				printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
697 698 699 700 701 702
						  "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;
				}
703 704 705 706

				if (aac_check_eeh_failure(dev))
					return -EFAULT;

707 708 709 710 711 712 713 714
				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;
				}
715 716 717 718
				/*
				 * Allow other processes / CPUS to use core
				 */
				schedule();
719
			}
720
		} else if (down_interruptible(&fibptr->event_wait)) {
721 722
			/* Do nothing ... satisfy
			 * down_interruptible must_check */
723
		}
724

725
		spin_lock_irqsave(&fibptr->event_lock, flags);
726
		if (fibptr->done == 0) {
727
			fibptr->done = 2; /* Tell interrupt we aborted */
728
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
729
			return -ERESTARTSYS;
730
		}
731
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
E
Eric Sesterhenn 已提交
732
		BUG_ON(fibptr->done == 0);
733

734
		if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
L
Linus Torvalds 已提交
735
			return -ETIMEDOUT;
736
		return 0;
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747
	}
	/*
	 *	If the user does not want a response than return success otherwise
	 *	return pending
	 */
	if (reply)
		return -EINPROGRESS;
	else
		return 0;
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
int aac_hba_send(u8 command, struct fib *fibptr, fib_callback callback,
		void *callback_data)
{
	struct aac_dev *dev = fibptr->dev;
	int wait;
	unsigned long flags = 0;
	unsigned long mflags = 0;

	fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA);
	if (callback) {
		wait = 0;
		fibptr->callback = callback;
		fibptr->callback_data = callback_data;
	} else
		wait = 1;


	if (command == HBA_IU_TYPE_SCSI_CMD_REQ) {
		struct aac_hba_cmd_req *hbacmd =
			(struct aac_hba_cmd_req *)fibptr->hw_fib_va;

		hbacmd->iu_type = command;
		/* bit1 of request_id must be 0 */
		hbacmd->request_id =
			cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
	} else
		return -EINVAL;


	if (wait) {
		spin_lock_irqsave(&dev->manage_lock, mflags);
		if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
			spin_unlock_irqrestore(&dev->manage_lock, mflags);
			return -EBUSY;
		}
		dev->management_fib_count++;
		spin_unlock_irqrestore(&dev->manage_lock, mflags);
		spin_lock_irqsave(&fibptr->event_lock, flags);
	}

	if (aac_adapter_deliver(fibptr) != 0) {
		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;
	}
	FIB_COUNTER_INCREMENT(aac_config.NativeSent);

	if (wait) {
800

801
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
802 803 804 805

		if (aac_check_eeh_failure(dev))
			return -EFAULT;

806 807
		fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
		if (down_interruptible(&fibptr->event_wait))
808
			fibptr->done = 2;
809 810
		fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
		spin_lock_irqsave(&fibptr->event_lock, flags);
		if ((fibptr->done == 0) || (fibptr->done == 2)) {
			fibptr->done = 2; /* Tell interrupt we aborted */
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
			return -ERESTARTSYS;
		}
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
		WARN_ON(fibptr->done == 0);

		if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
			return -ETIMEDOUT;

		return 0;
	}

	return -EINPROGRESS;
}

829
/**
L
Linus Torvalds 已提交
830 831 832 833 834 835
 *	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
836 837
 *	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 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851
 */

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
		 */
852 853
		if (le32_to_cpu(*q->headers.consumer) >= q->entries)
			index = 0;
L
Linus Torvalds 已提交
854
		else
855
			index = le32_to_cpu(*q->headers.consumer);
L
Linus Torvalds 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
		*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;
879

L
Linus Torvalds 已提交
880 881 882
	if (le32_to_cpu(*q->headers.consumer) >= q->entries)
		*q->headers.consumer = cpu_to_le32(1);
	else
883
		le32_add_cpu(q->headers.consumer, 1);
884

L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	if (wasfull) {
		switch (qid) {

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

/**
903
 *	aac_fib_adapter_complete	-	complete adapter issued fib
L
Linus Torvalds 已提交
904 905 906 907 908 909 910
 *	@fibptr: fib to complete
 *	@size: size of fib
 *
 *	Will do all necessary work to complete a FIB that was sent from
 *	the adapter.
 */

911
int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
L
Linus Torvalds 已提交
912
{
913
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
914
	struct aac_dev * dev = fibptr->dev;
915
	struct aac_queue * q;
L
Linus Torvalds 已提交
916
	unsigned long nointr = 0;
917 918
	unsigned long qflags;

919
	if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
920 921
		dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
		dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
922 923 924 925
		kfree(hw_fib);
		return 0;
	}

926
	if (hw_fib->header.XferState == 0) {
927
		if (dev->comm_interface == AAC_COMM_MESSAGE)
928
			kfree(hw_fib);
929
		return 0;
930
	}
L
Linus Torvalds 已提交
931 932
	/*
	 *	If we plan to do anything check the structure type first.
933
	 */
934 935 936
	if (hw_fib->header.StructType != FIB_MAGIC &&
	    hw_fib->header.StructType != FIB_MAGIC2 &&
	    hw_fib->header.StructType != FIB_MAGIC2_64) {
937
		if (dev->comm_interface == AAC_COMM_MESSAGE)
938
			kfree(hw_fib);
939
		return -EINVAL;
L
Linus Torvalds 已提交
940 941 942 943
	}
	/*
	 *	This block handles the case where the adapter had sent us a
	 *	command and we have finished processing the command. We
944 945
	 *	call completeFib when we are done processing the command
	 *	and want to send a response back to the adapter. This will
L
Linus Torvalds 已提交
946 947 948
	 *	send the completed cdb to the adapter.
	 */
	if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
949
		if (dev->comm_interface == AAC_COMM_MESSAGE) {
950 951
			kfree (hw_fib);
		} else {
952 953
			u32 index;
			hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
954 955
			if (size) {
				size += sizeof(struct aac_fibhdr);
956
				if (size > le16_to_cpu(hw_fib->header.SenderSize))
957 958 959 960 961 962 963 964 965 966
					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 已提交
967
		}
968 969 970 971
	} else {
		printk(KERN_WARNING "aac_fib_adapter_complete: "
			"Unknown xferstate detected.\n");
		BUG();
L
Linus Torvalds 已提交
972 973 974 975 976
	}
	return 0;
}

/**
977
 *	aac_fib_complete	-	fib completion handler
L
Linus Torvalds 已提交
978 979 980 981
 *	@fib: FIB to complete
 *
 *	Will do all necessary work to complete a FIB.
 */
982

983
int aac_fib_complete(struct fib *fibptr)
L
Linus Torvalds 已提交
984
{
985
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
986

987 988 989 990 991
	if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
		fib_dealloc(fibptr);
		return 0;
	}

L
Linus Torvalds 已提交
992
	/*
993 994
	 *	Check for a fib which has already been completed or with a
	 *	status wait timeout
L
Linus Torvalds 已提交
995 996
	 */

997
	if (hw_fib->header.XferState == 0 || fibptr->done == 2)
998
		return 0;
L
Linus Torvalds 已提交
999 1000
	/*
	 *	If we plan to do anything check the structure type first.
1001
	 */
L
Linus Torvalds 已提交
1002

1003 1004 1005
	if (hw_fib->header.StructType != FIB_MAGIC &&
	    hw_fib->header.StructType != FIB_MAGIC2 &&
	    hw_fib->header.StructType != FIB_MAGIC2_64)
1006
		return -EINVAL;
L
Linus Torvalds 已提交
1007
	/*
1008
	 *	This block completes a cdb which orginated on the host and we
L
Linus Torvalds 已提交
1009 1010 1011 1012
	 *	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.
	 */
1013

L
Linus Torvalds 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	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();
1030
	}
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	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;
1046 1047 1048 1049
	if (dev->printf_enabled)
	{
		int length = val & 0xffff;
		int level = (val >> 16) & 0xffff;
1050

1051 1052 1053 1054 1055 1056 1057 1058 1059
		/*
		 *	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)
1060
			printk(KERN_WARNING "%s:%s", dev->name, cp);
1061
		else
1062
			printk(KERN_INFO "%s:%s", dev->name, cp);
1063
	}
1064
	memset(cp, 0, 256);
L
Linus Torvalds 已提交
1065 1066
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
static inline int aac_aif_data(struct aac_aifcmd *aifcmd, uint32_t index)
{
	return le32_to_cpu(((__le32 *)aifcmd->data)[index]);
}


static void aac_handle_aif_bu(struct aac_dev *dev, struct aac_aifcmd *aifcmd)
{
	switch (aac_aif_data(aifcmd, 1)) {
	case AifBuCacheDataLoss:
		if (aac_aif_data(aifcmd, 2))
			dev_info(&dev->pdev->dev, "Backup unit had cache data loss - [%d]\n",
			aac_aif_data(aifcmd, 2));
		else
			dev_info(&dev->pdev->dev, "Backup Unit had cache data loss\n");
		break;
	case AifBuCacheDataRecover:
		if (aac_aif_data(aifcmd, 2))
			dev_info(&dev->pdev->dev, "DDR cache data recovered successfully - [%d]\n",
			aac_aif_data(aifcmd, 2));
		else
			dev_info(&dev->pdev->dev, "DDR cache data recovered successfully\n");
		break;
	}
}
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

/**
 *	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.
 */

M
Mahesh Rajashekhara 已提交
1102
#define AIF_SNIFF_TIMEOUT	(500*HZ)
1103 1104
static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
{
1105
	struct hw_fib * hw_fib = fibptr->hw_fib_va;
1106
	struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
1107
	u32 channel, id, lun, container;
1108 1109 1110 1111 1112 1113
	struct scsi_device *device;
	enum {
		NOTHING,
		DELETE,
		ADD,
		CHANGE
1114
	} device_config_needed = NOTHING;
1115 1116 1117

	/* Sniff for container changes */

1118
	if (!dev || !dev->fsa_dev)
1119
		return;
1120
	container = channel = id = lun = (u32)-1;
1121 1122 1123 1124 1125 1126 1127 1128 1129

	/*
	 *	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:
1130
		switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
		case AifRawDeviceRemove:
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
			if ((container >> 28)) {
				container = (u32)-1;
				break;
			}
			channel = (container >> 24) & 0xF;
			if (channel >= dev->maximum_num_channels) {
				container = (u32)-1;
				break;
			}
			id = container & 0xFFFF;
			if (id >= dev->maximum_num_physicals) {
				container = (u32)-1;
				break;
			}
			lun = (container >> 16) & 0xFF;
			container = (u32)-1;
			channel = aac_phys_to_logical(channel);
1150
			device_config_needed = DELETE;
1151
			break;
1152

1153 1154 1155 1156 1157
		/*
		 *	Morph or Expand complete
		 */
		case AifDenMorphComplete:
		case AifDenVolumeExtendComplete:
1158
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1159 1160 1161 1162
			if (container >= dev->maximum_num_containers)
				break;

			/*
1163
			 *	Find the scsi_device associated with the SCSI
1164 1165 1166 1167 1168 1169
			 * 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)) {
1170 1171 1172
				device = scsi_device_lookup(dev->scsi_host_ptr,
					CONTAINER_TO_CHANNEL(container),
					CONTAINER_TO_ID(container),
1173 1174 1175 1176
					CONTAINER_TO_LUN(container));
				if (device) {
					dev->fsa_dev[container].config_needed = CHANGE;
					dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
1177
					dev->fsa_dev[container].config_waiting_stamp = jiffies;
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
					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;
1190
			if ((dev->fsa_dev[container].config_waiting_on ==
1191
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1192
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1193 1194 1195
				dev->fsa_dev[container].config_waiting_on = 0;
		} else for (container = 0;
		    container < dev->maximum_num_containers; ++container) {
1196
			if ((dev->fsa_dev[container].config_waiting_on ==
1197
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1198
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1199 1200 1201 1202 1203
				dev->fsa_dev[container].config_waiting_on = 0;
		}
		break;

	case AifCmdEventNotify:
1204
		switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
1205 1206 1207 1208
		case AifEnBatteryEvent:
			dev->cache_protected =
				(((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
			break;
1209 1210 1211 1212
		/*
		 *	Add an Array.
		 */
		case AifEnAddContainer:
1213
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1214 1215 1216 1217 1218
			if (container >= dev->maximum_num_containers)
				break;
			dev->fsa_dev[container].config_needed = ADD;
			dev->fsa_dev[container].config_waiting_on =
				AifEnConfigChange;
1219
			dev->fsa_dev[container].config_waiting_stamp = jiffies;
1220 1221 1222 1223 1224 1225
			break;

		/*
		 *	Delete an Array.
		 */
		case AifEnDeleteContainer:
1226
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1227 1228 1229 1230 1231
			if (container >= dev->maximum_num_containers)
				break;
			dev->fsa_dev[container].config_needed = DELETE;
			dev->fsa_dev[container].config_waiting_on =
				AifEnConfigChange;
1232
			dev->fsa_dev[container].config_waiting_stamp = jiffies;
1233 1234 1235 1236 1237 1238 1239
			break;

		/*
		 *	Container change detected. If we currently are not
		 * waiting on something else, setup to wait on a Config Change.
		 */
		case AifEnContainerChange:
1240
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1241 1242
			if (container >= dev->maximum_num_containers)
				break;
1243 1244
			if (dev->fsa_dev[container].config_waiting_on &&
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1245 1246 1247 1248
				break;
			dev->fsa_dev[container].config_needed = CHANGE;
			dev->fsa_dev[container].config_waiting_on =
				AifEnConfigChange;
1249
			dev->fsa_dev[container].config_waiting_stamp = jiffies;
1250 1251 1252 1253 1254
			break;

		case AifEnConfigChange:
			break;

1255 1256 1257
		case AifEnAddJBOD:
		case AifEnDeleteJBOD:
			container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
1258 1259
			if ((container >> 28)) {
				container = (u32)-1;
1260
				break;
1261
			}
1262
			channel = (container >> 24) & 0xF;
1263 1264
			if (channel >= dev->maximum_num_channels) {
				container = (u32)-1;
1265
				break;
1266
			}
1267
			id = container & 0xFFFF;
1268 1269
			if (id >= dev->maximum_num_physicals) {
				container = (u32)-1;
1270
				break;
1271
			}
1272
			lun = (container >> 16) & 0xFF;
1273
			container = (u32)-1;
1274 1275 1276 1277
			channel = aac_phys_to_logical(channel);
			device_config_needed =
			  (((__le32 *)aifcmd->data)[0] ==
			    cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
			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);
				}
			}
1288 1289
			break;

1290
		case AifEnEnclosureManagement:
1291 1292 1293 1294 1295 1296
			/*
			 * If in JBOD mode, automatic exposure of new
			 * physical target to be suppressed until configured.
			 */
			if (dev->jbod)
				break;
1297 1298 1299
			switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
			case EM_DRIVE_INSERTION:
			case EM_DRIVE_REMOVAL:
1300 1301
			case EM_SES_DRIVE_INSERTION:
			case EM_SES_DRIVE_REMOVAL:
1302 1303
				container = le32_to_cpu(
					((__le32 *)aifcmd->data)[2]);
1304 1305
				if ((container >> 28)) {
					container = (u32)-1;
1306
					break;
1307
				}
1308
				channel = (container >> 24) & 0xF;
1309 1310
				if (channel >= dev->maximum_num_channels) {
					container = (u32)-1;
1311
					break;
1312
				}
1313 1314
				id = container & 0xFFFF;
				lun = (container >> 16) & 0xFF;
1315
				container = (u32)-1;
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
				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 =
1327 1328 1329 1330
				  ((((__le32 *)aifcmd->data)[3]
				    == cpu_to_le32(EM_DRIVE_INSERTION)) ||
				    (((__le32 *)aifcmd->data)[3]
				    == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
1331 1332 1333
				  ADD : DELETE;
				break;
			}
1334 1335
			case AifBuManagerEvent:
				aac_handle_aif_bu(dev, aifcmd);
1336
			break;
1337 1338 1339 1340 1341 1342 1343 1344 1345
		}

		/*
		 *	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;
1346
			if ((dev->fsa_dev[container].config_waiting_on ==
1347
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1348
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1349 1350 1351
				dev->fsa_dev[container].config_waiting_on = 0;
		} else for (container = 0;
		    container < dev->maximum_num_containers; ++container) {
1352
			if ((dev->fsa_dev[container].config_waiting_on ==
1353
			    le32_to_cpu(*(__le32 *)aifcmd->data)) &&
1354
			 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
				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.
		 */

1368 1369 1370
		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))) {
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
			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;
1381 1382
				dev->fsa_dev[container].config_waiting_stamp =
					jiffies;
1383 1384
			}
		}
1385 1386 1387
		if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
		    ((__le32 *)aifcmd->data)[6] == 0 &&
		    ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
			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;
1398 1399
				dev->fsa_dev[container].config_waiting_stamp =
					jiffies;
1400 1401 1402 1403 1404
			}
		}
		break;
	}

1405 1406
	container = 0;
retry_next:
1407
	if (device_config_needed == NOTHING)
1408
	for (; container < dev->maximum_num_containers; ++container) {
1409 1410 1411
		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)) {
1412 1413 1414
			device_config_needed =
				dev->fsa_dev[container].config_needed;
			dev->fsa_dev[container].config_needed = NOTHING;
1415 1416 1417
			channel = CONTAINER_TO_CHANNEL(container);
			id = CONTAINER_TO_ID(container);
			lun = CONTAINER_TO_LUN(container);
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
			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.
	 */

	/*
1431
	 *	Find the scsi_device associated with the SCSI address,
1432 1433 1434 1435 1436 1437 1438
	 * and mark it as changed, invalidating the cache. This deals
	 * with changes to existing device IDs.
	 */

	if (!dev || !dev->scsi_host_ptr)
		return;
	/*
1439
	 * force reload of disk info via aac_probe_container
1440
	 */
1441 1442 1443 1444
	if ((channel == CONTAINER_CHANNEL) &&
	  (device_config_needed != NOTHING)) {
		if (dev->fsa_dev[container].valid == 1)
			dev->fsa_dev[container].valid = 2;
1445
		aac_probe_container(dev, container);
1446 1447
	}
	device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
1448 1449 1450
	if (device) {
		switch (device_config_needed) {
		case DELETE:
1451 1452 1453
#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
			scsi_remove_device(device);
#else
1454 1455 1456 1457 1458 1459 1460 1461
			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");
			}
1462
#endif
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
			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 */
1474
		case CHANGE:
1475 1476
			if ((channel == CONTAINER_CHANNEL)
			 && (!dev->fsa_dev[container].valid)) {
1477 1478 1479
#if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
				scsi_remove_device(device);
#else
1480 1481 1482 1483 1484 1485
				if (!scsi_device_online(device))
					break;
				scsi_device_set_state(device, SDEV_OFFLINE);
				sdev_printk(KERN_INFO, device,
					"Device offlined - %s\n",
					"array failed");
1486
#endif
1487 1488
				break;
			}
1489 1490 1491 1492 1493 1494
			scsi_rescan_device(&device->sdev_gendev);

		default:
			break;
		}
		scsi_device_put(device);
1495
		device_config_needed = NOTHING;
1496
	}
1497 1498
	if (device_config_needed == ADD)
		scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
1499 1500 1501 1502 1503
	if (channel == CONTAINER_CHANNEL) {
		container++;
		device_config_needed = NOTHING;
		goto retry_next;
	}
1504 1505
}

1506
static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1507 1508
{
	int index, quirks;
1509
	int retval;
1510 1511 1512 1513
	struct Scsi_Host *host;
	struct scsi_device *dev;
	struct scsi_cmnd *command;
	struct scsi_cmnd *command_list;
1514
	int jafo = 0;
1515
	int bled;
1516
	u64 dmamask;
1517
	int num_of_fibs = 0;
1518 1519 1520

	/*
	 * Assumptions:
1521 1522 1523
	 *	- host is locked, unless called by the aacraid thread.
	 *	  (a matter of convenience, due to legacy issues surrounding
	 *	  eh_host_adapter_reset).
1524 1525
	 *	- in_reset is asserted, so no new i/o is getting to the
	 *	  card.
1526 1527
	 *	- The card is dead, or will be very shortly ;-/ so no new
	 *	  commands are completing in the interrupt service.
1528 1529 1530 1531
	 */
	host = aac->scsi_host_ptr;
	scsi_block_requests(host);
	aac_adapter_disable_int(aac);
1532 1533 1534 1535 1536
	if (aac->thread->pid != current->pid) {
		spin_unlock_irq(host->host_lock);
		kthread_stop(aac->thread);
		jafo = 1;
	}
1537 1538 1539 1540 1541

	/*
	 *	If a positive health, means in a known DEAD PANIC
	 * state and the adapter could be reset to `try again'.
	 */
1542 1543
	bled = forced ? 0 : aac_adapter_check_health(aac);
	retval = aac_adapter_restart(aac, bled, reset_type);
1544 1545 1546 1547

	if (retval)
		goto out;

1548 1549 1550
	/*
	 *	Loop through the fibs, close the synchronous FIBS
	 */
1551 1552 1553 1554
	retval = 1;
	num_of_fibs = aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
	for (index = 0; index <  num_of_fibs; index++) {

1555
		struct fib *fib = &aac->fibs[index];
1556 1557 1558 1559 1560 1561 1562 1563 1564
		__le32 XferState = fib->hw_fib_va->header.XferState;
		bool is_response_expected = false;

		if (!(XferState & cpu_to_le32(NoResponseExpected | Async)) &&
		   (XferState & cpu_to_le32(ResponseExpected)))
			is_response_expected = true;

		if (is_response_expected
		  || fib->flags & FIB_CONTEXT_FLAG_WAIT) {
1565 1566 1567 1568 1569
			unsigned long flagv;
			spin_lock_irqsave(&fib->event_lock, flagv);
			up(&fib->event_wait);
			spin_unlock_irqrestore(&fib->event_lock, flagv);
			schedule();
1570
			retval = 0;
1571 1572
		}
	}
1573 1574 1575
	/* Give some extra time for ioctls to complete. */
	if (retval == 0)
		ssleep(2);
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
	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);
1586 1587
	dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr,
			  aac->comm_phys);
1588 1589 1590 1591
	aac->comm_addr = NULL;
	aac->comm_phys = 0;
	kfree(aac->queues);
	aac->queues = NULL;
1592
	aac_free_irq(aac);
1593 1594
	kfree(aac->fsa_dev);
	aac->fsa_dev = NULL;
1595 1596

	dmamask = DMA_BIT_MASK(32);
1597
	quirks = aac_get_driver_ident(index)->quirks;
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	if (quirks & AAC_QUIRK_31BIT)
		retval = pci_set_dma_mask(aac->pdev, dmamask);
	else if (!(quirks & AAC_QUIRK_SRC))
		retval = pci_set_dma_mask(aac->pdev, dmamask);
	else
		retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);

	if (quirks & AAC_QUIRK_31BIT && !retval) {
		dmamask = DMA_BIT_MASK(31);
		retval = pci_set_consistent_dma_mask(aac->pdev, dmamask);
1608
	}
1609 1610 1611 1612

	if (retval)
		goto out;

1613 1614
	if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
		goto out;
1615

1616
	if (jafo) {
1617 1618
		aac->thread = kthread_run(aac_command_thread, aac, "%s",
					  aac->name);
1619 1620 1621 1622
		if (IS_ERR(aac->thread)) {
			retval = PTR_ERR(aac->thread);
			goto out;
		}
1623 1624 1625
	}
	(void)aac_get_adapter_info(aac);
	if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
1626 1627 1628 1629 1630 1631 1632
		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;
	}
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
	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);
	}
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
	/*
	 * Any Device that was already marked offline needs to be cleaned up
	 */
	__shost_for_each_device(dev, host) {
		if (!scsi_device_online(dev)) {
			sdev_printk(KERN_INFO, dev, "Removing offline device\n");
			scsi_remove_device(dev);
			scsi_device_put(dev);
		}
	}
1669 1670 1671 1672 1673
	retval = 0;

out:
	aac->in_reset = 0;
	scsi_unblock_requests(host);
1674 1675 1676 1677 1678 1679 1680 1681
	/*
	 * Issue bus rescan to catch any configuration that might have
	 * occurred
	 */
	if (!retval) {
		dev_info(&aac->pdev->dev, "Issuing bus rescan\n");
		scsi_scan_host(host);
	}
1682 1683 1684 1685 1686 1687
	if (jafo) {
		spin_lock_irq(host->host_lock);
	}
	return retval;
}

1688
int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
1689 1690 1691 1692
{
	unsigned long flagv = 0;
	int retval;
	struct Scsi_Host * host;
1693
	int bled;
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

	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 */
1739 1740
	if (forced < 2)
		aac_send_shutdown(aac);
1741
	spin_lock_irqsave(host->host_lock, flagv);
1742 1743 1744
	bled = forced ? forced :
			(aac_check_reset != 0 && aac_check_reset != 1);
	retval = _aac_reset_adapter(aac, bled, reset_type);
1745 1746
	spin_unlock_irqrestore(host->host_lock, flagv);

1747
	if ((forced < 2) && (retval == -ENODEV)) {
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
		/* 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);
1774 1775 1776 1777
			/* FIB should be freed only after getting
			 * the response from the F/W */
			if (status != -ERESTARTSYS)
				aac_fib_free(fibctx);
1778 1779 1780
		}
	}

1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 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 1846 1847 1848 1849 1850 1851
	return retval;
}

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

	/* 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
		 */
1852 1853
		hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
		fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
1854 1855 1856
		if (fib && hw_fib) {
			struct aac_aifcmd * aif;

1857
			fib->hw_fib_va = hw_fib;
1858 1859 1860 1861 1862 1863 1864
			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);
1865 1866 1867 1868 1869
			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);
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892

			/*
			 * 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) {
1893 1894
		printk(KERN_ERR "%s: Host adapter is dead (or got a PCI error) %d\n",
				aac->name, BlinkLED);
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
		goto out;
	}

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

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

1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

static void aac_resolve_luns(struct aac_dev *dev)
{
	int bus, target, channel;
	struct scsi_device *sdev;
	u8 devtype;
	u8 new_devtype;

	for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
		for (target = 0; target < AAC_MAX_TARGETS; target++) {

			if (bus == CONTAINER_CHANNEL)
				channel = CONTAINER_CHANNEL;
			else
				channel = aac_phys_to_logical(bus);

			devtype = dev->hba_map[bus][target].devtype;
			new_devtype = dev->hba_map[bus][target].new_devtype;

			sdev = scsi_device_lookup(dev->scsi_host_ptr, channel,
					target, 0);

1927
			if (!sdev && new_devtype)
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
				scsi_add_device(dev->scsi_host_ptr, channel,
						target, 0);
			else if (sdev && new_devtype != devtype)
				scsi_remove_device(sdev);
			else if (sdev && new_devtype == devtype)
				scsi_rescan_device(&sdev->sdev_gendev);

			if (sdev)
				scsi_device_put(sdev);

			dev->hba_map[bus][target].devtype = new_devtype;
		}
	}
}

/**
 *	aac_handle_sa_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.
 */
static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr)
{
	int i, bus, target, container, rcode = 0;
	u32 events = 0;
	struct fib *fib;
	struct scsi_device *sdev;

	if (fibptr->hbacmd_size & SA_AIF_HOTPLUG)
		events = SA_AIF_HOTPLUG;
	else if (fibptr->hbacmd_size & SA_AIF_HARDWARE)
		events = SA_AIF_HARDWARE;
	else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE)
		events = SA_AIF_PDEV_CHANGE;
	else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE)
		events = SA_AIF_LDEV_CHANGE;
	else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE)
		events = SA_AIF_BPSTAT_CHANGE;
	else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE)
		events = SA_AIF_BPCFG_CHANGE;

	switch (events) {
	case SA_AIF_HOTPLUG:
	case SA_AIF_HARDWARE:
	case SA_AIF_PDEV_CHANGE:
	case SA_AIF_LDEV_CHANGE:
	case SA_AIF_BPCFG_CHANGE:

		fib = aac_fib_alloc(dev);
		if (!fib) {
			pr_err("aac_handle_sa_aif: out of memory\n");
			return;
		}
		for (bus = 0; bus < AAC_MAX_BUSES; bus++)
			for (target = 0; target < AAC_MAX_TARGETS; target++)
				dev->hba_map[bus][target].new_devtype = 0;

		rcode = aac_report_phys_luns(dev, fib, AAC_RESCAN);

		if (rcode != -ERESTARTSYS)
			aac_fib_free(fib);

		aac_resolve_luns(dev);

		if (events == SA_AIF_LDEV_CHANGE ||
		    events == SA_AIF_BPCFG_CHANGE) {
			aac_get_containers(dev);
			for (container = 0; container <
			dev->maximum_num_containers; ++container) {
				sdev = scsi_device_lookup(dev->scsi_host_ptr,
						CONTAINER_CHANNEL,
						container, 0);
				if (dev->fsa_dev[container].valid && !sdev) {
					scsi_add_device(dev->scsi_host_ptr,
						CONTAINER_CHANNEL,
						container, 0);
				} else if (!dev->fsa_dev[container].valid &&
					sdev) {
					scsi_remove_device(sdev);
					scsi_device_put(sdev);
				} else if (sdev) {
					scsi_rescan_device(&sdev->sdev_gendev);
					scsi_device_put(sdev);
				}
			}
		}
		break;

	case SA_AIF_BPSTAT_CHANGE:
		/* currently do nothing */
		break;
	}

	for (i = 1; i <= 10; ++i) {
		events = src_readl(dev, MUnit.IDR);
		if (events & (1<<23)) {
			pr_warn(" AIF not cleared by firmware - %d/%d)\n",
				i, 10);
			ssleep(1);
		}
	}
}

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
static int get_fib_count(struct aac_dev *dev)
{
	unsigned int num = 0;
	struct list_head *entry;
	unsigned long flagv;

	/*
	 * 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->r7.adapter_fibs_size)
			/ 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);

	return num;
}

static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool,
						struct fib **fib_pool,
						unsigned int num)
{
	struct hw_fib **hw_fib_p;
	struct fib **fib_p;

	hw_fib_p = hw_fib_pool;
	fib_p = fib_pool;
	while (hw_fib_p < &hw_fib_pool[num]) {
		*(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL);
		if (!(*(hw_fib_p++))) {
			--hw_fib_p;
			break;
		}

		*(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL);
		if (!(*(fib_p++))) {
			kfree(*(--hw_fib_p));
			break;
		}
	}

2081 2082 2083
	/*
	 * Get the actual number of allocated fibs
	 */
2084
	num = hw_fib_p - hw_fib_pool;
2085
	return num;
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
}

static void wakeup_fibctx_threads(struct aac_dev *dev,
						struct hw_fib **hw_fib_pool,
						struct fib **fib_pool,
						struct fib *fib,
						struct hw_fib *hw_fib,
						unsigned int num)
{
	unsigned long flagv;
	struct list_head *entry;
	struct hw_fib **hw_fib_p;
	struct fib **fib_p;
	u32 time_now, time_last;
	struct hw_fib *hw_newfib;
	struct fib *newfib;
	struct aac_fib_context *fibctx;

	time_now = jiffies/HZ;
	spin_lock_irqsave(&dev->fib_lock, flagv);
	entry = dev->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.
	 */

	hw_fib_p = hw_fib_pool;
	fib_p = fib_pool;
	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;
			/*
			 * 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(dev, fibctx);
				continue;
			}
		}
		/*
		 * Warning: no sleep allowed while
		 * holding spinlock
		 */
		if (hw_fib_p >= &hw_fib_pool[num]) {
			pr_warn("aifd: didn't allocate NewFib\n");
			entry = entry->next;
			continue;
		}

		hw_newfib = *hw_fib_p;
		*(hw_fib_p++) = NULL;
		newfib = *fib_p;
		*(fib_p++) = NULL;
		/*
		 * Make the copy of the FIB
		 */
		memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
		memcpy(newfib, fib, sizeof(struct fib));
		newfib->hw_fib_va = hw_newfib;
		/*
		 * Put the FIB onto the
		 * fibctx's fibs
		 */
		list_add_tail(&newfib->fiblink, &fibctx->fib_list);
		fibctx->count++;
		/*
		 * Set the event to wake up the
		 * thread that is waiting.
		 */
		up(&fibctx->wait_sem);

		entry = entry->next;
	}
	/*
	 *	Set the status of this FIB
	 */
	*(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
	aac_fib_adapter_complete(fib, sizeof(u32));
	spin_unlock_irqrestore(&dev->fib_lock, flagv);

}

static void aac_process_events(struct aac_dev *dev)
{
	struct hw_fib *hw_fib;
	struct fib *fib;
	unsigned long flags;
	spinlock_t *t_lock;

	t_lock = dev->queues->queue[HostNormCmdQueue].lock;
	spin_lock_irqsave(t_lock, flags);

	while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
		struct list_head *entry;
		struct aac_aifcmd *aifcmd;
		unsigned int  num;
		struct hw_fib **hw_fib_pool, **hw_fib_p;
		struct fib **fib_pool, **fib_p;

		set_current_state(TASK_RUNNING);

		entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
		list_del(entry);

		t_lock = dev->queues->queue[HostNormCmdQueue].lock;
		spin_unlock_irqrestore(t_lock, flags);

		fib = list_entry(entry, struct fib, fiblink);
		hw_fib = fib->hw_fib_va;
2214 2215 2216 2217
		if (dev->sa_firmware) {
			/* Thor AIF */
			aac_handle_sa_aif(dev, fib);
			aac_fib_adapter_complete(fib, (u16)sizeof(u32));
2218
			goto free_fib;
2219
		}
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
		/*
		 *	We will process the FIB here or pass it to a
		 *	worker thread that is TBD. We Really can't
		 *	do anything at this point since we don't have
		 *	anything defined for this thread to do.
		 */
		memset(fib, 0, sizeof(struct fib));
		fib->type = FSAFS_NTC_FIB_CONTEXT;
		fib->size = sizeof(struct fib);
		fib->hw_fib_va = hw_fib;
		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 */
			aac_handle_aif(dev, fib);
			*(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
			aac_fib_adapter_complete(fib, (u16)sizeof(u32));
			goto free_fib;
		}
		/*
		 * The u32 here is important and intended. We are using
		 * 32bit wrapping time to fit the adapter field
		 */

		/* Sniff events */
		if (aifcmd->command == cpu_to_le32(AifCmdEventNotify)
		 || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) {
			aac_handle_aif(dev, fib);
		}

		/*
		 * get number of fibs to process
		 */
		num = get_fib_count(dev);
		if (!num)
			goto free_fib;

		hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *),
						GFP_KERNEL);
		if (!hw_fib_pool)
			goto free_fib;

		fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL);
		if (!fib_pool)
			goto free_hw_fib_pool;

		/*
		 * Fill up fib pointer pools with actual fibs
		 * and hw_fibs
		 */
2275 2276
		num = fillup_pools(dev, hw_fib_pool, fib_pool, num);
		if (!num)
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
			goto free_mem;

		/*
		 * wakeup the thread that is waiting for
		 * the response from fw (ioctl)
		 */
		wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool,
							    fib, hw_fib, num);

free_mem:
		/* Free up the remaining resources */
		hw_fib_p = hw_fib_pool;
		fib_p = fib_pool;
		while (hw_fib_p < &hw_fib_pool[num]) {
			kfree(*hw_fib_p);
			kfree(*fib_p);
			++fib_p;
			++hw_fib_p;
		}
		kfree(fib_pool);
free_hw_fib_pool:
		kfree(hw_fib_pool);
free_fib:
		kfree(fib);
		t_lock = dev->queues->queue[HostNormCmdQueue].lock;
		spin_lock_irqsave(t_lock, flags);
	}
	/*
	 *	There are no more AIF's
	 */
	t_lock = dev->queues->queue[HostNormCmdQueue].lock;
	spin_unlock_irqrestore(t_lock, flags);
}
2310

2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str,
							u32 datasize)
{
	struct aac_srb *srbcmd;
	struct sgmap64 *sg64;
	dma_addr_t addr;
	char *dma_buf;
	struct fib *fibptr;
	int ret = -ENOMEM;
	u32 vbus, vid;

	fibptr = aac_fib_alloc(dev);
	if (!fibptr)
		goto out;

2326 2327
	dma_buf = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
				     GFP_KERNEL);
2328 2329 2330 2331 2332
	if (!dma_buf)
		goto fib_free_out;

	aac_fib_init(fibptr);

2333 2334
	vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
	vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361

	srbcmd = (struct aac_srb *)fib_data(fibptr);

	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
	srbcmd->channel = cpu_to_le32(vbus);
	srbcmd->id = cpu_to_le32(vid);
	srbcmd->lun = 0;
	srbcmd->flags = cpu_to_le32(SRB_DataOut);
	srbcmd->timeout = cpu_to_le32(10);
	srbcmd->retry_limit = 0;
	srbcmd->cdb_size = cpu_to_le32(12);
	srbcmd->count = cpu_to_le32(datasize);

	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
	srbcmd->cdb[0] = BMIC_OUT;
	srbcmd->cdb[6] = WRITE_HOST_WELLNESS;
	memcpy(dma_buf, (char *)wellness_str, datasize);

	sg64 = (struct sgmap64 *)&srbcmd->sg;
	sg64->count = cpu_to_le32(1);
	sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
	sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
	sg64->sg[0].count = cpu_to_le32(datasize);

	ret = aac_fib_send(ScsiPortCommand64, fibptr, sizeof(struct aac_srb),
				FsaNormal, 1, 1, NULL, NULL);

2362
	dma_free_coherent(&dev->pdev->dev, datasize, dma_buf, addr);
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447

	/*
	 * Do not set XferState to zero unless
	 * receives a response from F/W
	 */
	if (ret >= 0)
		aac_fib_complete(fibptr);

	/*
	 * FIB should be freed only after
	 * getting the response from the F/W
	 */
	if (ret != -ERESTARTSYS)
		goto fib_free_out;

out:
	return ret;
fib_free_out:
	aac_fib_free(fibptr);
	goto out;
}

int aac_send_safw_hostttime(struct aac_dev *dev, struct timeval *now)
{
	struct tm cur_tm;
	char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
	u32 datasize = sizeof(wellness_str);
	unsigned long local_time;
	int ret = -ENODEV;

	if (!dev->sa_firmware)
		goto out;

	local_time = (u32)(now->tv_sec - (sys_tz.tz_minuteswest * 60));
	time_to_tm(local_time, 0, &cur_tm);
	cur_tm.tm_mon += 1;
	cur_tm.tm_year += 1900;
	wellness_str[8] = bin2bcd(cur_tm.tm_hour);
	wellness_str[9] = bin2bcd(cur_tm.tm_min);
	wellness_str[10] = bin2bcd(cur_tm.tm_sec);
	wellness_str[12] = bin2bcd(cur_tm.tm_mon);
	wellness_str[13] = bin2bcd(cur_tm.tm_mday);
	wellness_str[14] = bin2bcd(cur_tm.tm_year / 100);
	wellness_str[15] = bin2bcd(cur_tm.tm_year % 100);

	ret = aac_send_wellness_command(dev, wellness_str, datasize);

out:
	return ret;
}

int aac_send_hosttime(struct aac_dev *dev, struct timeval *now)
{
	int ret = -ENOMEM;
	struct fib *fibptr;
	__le32 *info;

	fibptr = aac_fib_alloc(dev);
	if (!fibptr)
		goto out;

	aac_fib_init(fibptr);
	info = (__le32 *)fib_data(fibptr);
	*info = cpu_to_le32(now->tv_sec);
	ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal,
					1, 1, NULL, NULL);

	/*
	 * Do not set XferState to zero unless
	 * receives a response from F/W
	 */
	if (ret >= 0)
		aac_fib_complete(fibptr);

	/*
	 * FIB should be freed only after
	 * getting the response from the F/W
	 */
	if (ret != -ERESTARTSYS)
		aac_fib_free(fibptr);

out:
	return ret;
}

L
Linus Torvalds 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456
/**
 *	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.
 */
2457

2458
int aac_command_thread(void *data)
L
Linus Torvalds 已提交
2459
{
2460
	struct aac_dev *dev = data;
L
Linus Torvalds 已提交
2461
	DECLARE_WAITQUEUE(wait, current);
2462 2463 2464
	unsigned long next_jiffies = jiffies + HZ;
	unsigned long next_check_jiffies = next_jiffies;
	long difference = HZ;
L
Linus Torvalds 已提交
2465 2466 2467 2468 2469 2470

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

L
Linus Torvalds 已提交
2472 2473 2474 2475
	/*
	 *	Let the DPC know it has a place to send the AIF's to.
	 */
	dev->aif_thread = 1;
2476
	add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
L
Linus Torvalds 已提交
2477
	set_current_state(TASK_INTERRUPTIBLE);
2478
	dprintk ((KERN_INFO "aac_command_thread start\n"));
2479
	while (1) {
L
Linus Torvalds 已提交
2480

2481
		aac_process_events(dev);
2482 2483 2484 2485 2486 2487 2488

		/*
		 *	Background activity
		 */
		if ((time_before(next_check_jiffies,next_jiffies))
		 && ((difference = next_check_jiffies - jiffies) <= 0)) {
			next_check_jiffies = next_jiffies;
2489
			if (aac_adapter_check_health(dev) == 0) {
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
				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 */
2502
			ret = aac_adapter_check_health(dev);
2503
			if (ret || !dev->queues)
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
				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;
2515
			else {
2516 2517 2518 2519 2520 2521 2522 2523 2524
				if (now.tv_usec > 500000)
					++now.tv_sec;

				if (dev->sa_firmware)
					ret =
					aac_send_safw_hostttime(dev, &now);
				else
					ret = aac_send_hosttime(dev, &now);

2525 2526 2527 2528 2529 2530 2531 2532 2533
				difference = (long)(unsigned)update_interval*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);
2534 2535 2536 2537

		if (kthread_should_stop())
			break;

2538
		schedule_timeout(difference);
L
Linus Torvalds 已提交
2539

2540
		if (kthread_should_stop())
L
Linus Torvalds 已提交
2541 2542
			break;
	}
2543 2544
	if (dev->queues)
		remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
L
Linus Torvalds 已提交
2545
	dev->aif_thread = 0;
2546
	return 0;
L
Linus Torvalds 已提交
2547
}
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558

int aac_acquire_irq(struct aac_dev *dev)
{
	int i;
	int j;
	int ret = 0;

	if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) {
		for (i = 0; i < dev->max_msix; i++) {
			dev->aac_msix[i].vector_no = i;
			dev->aac_msix[i].dev = dev;
2559
			if (request_irq(pci_irq_vector(dev->pdev, i),
2560 2561 2562 2563 2564
					dev->a_ops.adapter_intr,
					0, "aacraid", &(dev->aac_msix[i]))) {
				printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n",
						dev->name, dev->id, i);
				for (j = 0 ; j < i ; j++)
2565
					free_irq(pci_irq_vector(dev->pdev, j),
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
						 &(dev->aac_msix[j]));
				pci_disable_msix(dev->pdev);
				ret = -1;
			}
		}
	} else {
		dev->aac_msix[0].vector_no = 0;
		dev->aac_msix[0].dev = dev;

		if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
			IRQF_SHARED, "aacraid",
			&(dev->aac_msix[0])) < 0) {
			if (dev->msi)
				pci_disable_msi(dev->pdev);
			printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
					dev->name, dev->id);
			ret = -1;
		}
	}
	return ret;
}

void aac_free_irq(struct aac_dev *dev)
{
	int i;
	int cpu;

	cpu = cpumask_first(cpu_online_mask);
2594
	if (aac_is_src(dev)) {
2595
		if (dev->max_msix > 1) {
2596 2597 2598
			for (i = 0; i < dev->max_msix; i++)
				free_irq(pci_irq_vector(dev->pdev, i),
					 &(dev->aac_msix[i]));
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
		} else {
			free_irq(dev->pdev->irq, &(dev->aac_msix[0]));
		}
	} else {
		free_irq(dev->pdev->irq, dev);
	}
	if (dev->msi)
		pci_disable_msi(dev->pdev);
	else if (dev->max_msix > 1)
		pci_disable_msix(dev->pdev);
}