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