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