| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *	Adaptec AAC series RAID controller driver | 
| Alan Cox | fa195af | 2008-10-27 15:16:36 +0000 | [diff] [blame] | 3 |  *	(c) Copyright 2001 Red Hat Inc. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
 | 5 |  * based on the old aacraid driver that is.. | 
 | 6 |  * Adaptec aacraid device driver for Linux. | 
 | 7 |  * | 
| Mark Haverkamp | 03d4433 | 2007-03-15 10:27:45 -0700 | [diff] [blame] | 8 |  * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 |  * | 
 | 10 |  * This program is free software; you can redistribute it and/or modify | 
 | 11 |  * it under the terms of the GNU General Public License as published by | 
 | 12 |  * the Free Software Foundation; either version 2, or (at your option) | 
 | 13 |  * any later version. | 
 | 14 |  * | 
 | 15 |  * This program is distributed in the hope that it will be useful, | 
 | 16 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 17 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 18 |  * GNU General Public License for more details. | 
 | 19 |  * | 
 | 20 |  * You should have received a copy of the GNU General Public License | 
 | 21 |  * along with this program; see the file COPYING.  If not, write to | 
 | 22 |  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 23 |  * | 
 | 24 |  * Module Name: | 
 | 25 |  *  dpcsup.c | 
 | 26 |  * | 
 | 27 |  * Abstract: All DPC processing routines for the cyclone board occur here. | 
 | 28 |  * | 
 | 29 |  * | 
 | 30 |  */ | 
 | 31 |  | 
 | 32 | #include <linux/kernel.h> | 
 | 33 | #include <linux/init.h> | 
 | 34 | #include <linux/types.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/spinlock.h> | 
 | 36 | #include <linux/slab.h> | 
 | 37 | #include <linux/completion.h> | 
 | 38 | #include <linux/blkdev.h> | 
| Matthew Wilcox | 6188e10 | 2008-04-18 22:21:05 -0400 | [diff] [blame] | 39 | #include <linux/semaphore.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
 | 41 | #include "aacraid.h" | 
 | 42 |  | 
 | 43 | /** | 
 | 44 |  *	aac_response_normal	-	Handle command replies | 
 | 45 |  *	@q: Queue to read from | 
 | 46 |  * | 
 | 47 |  *	This DPC routine will be run when the adapter interrupts us to let us | 
 | 48 |  *	know there is a response on our normal priority queue. We will pull off | 
 | 49 |  *	all QE there are and wake up all the waiters before exiting. We will | 
 | 50 |  *	take a spinlock out on the queue before operating on it. | 
 | 51 |  */ | 
 | 52 |  | 
 | 53 | unsigned int aac_response_normal(struct aac_queue * q) | 
 | 54 | { | 
 | 55 | 	struct aac_dev * dev = q->dev; | 
 | 56 | 	struct aac_entry *entry; | 
 | 57 | 	struct hw_fib * hwfib; | 
 | 58 | 	struct fib * fib; | 
 | 59 | 	int consumed = 0; | 
 | 60 | 	unsigned long flags; | 
 | 61 |  | 
 | 62 | 	spin_lock_irqsave(q->lock, flags);	 | 
 | 63 | 	/* | 
 | 64 | 	 *	Keep pulling response QEs off the response queue and waking | 
 | 65 | 	 *	up the waiters until there are no more QEs. We then return | 
 | 66 | 	 *	back to the system. If no response was requesed we just | 
 | 67 | 	 *	deallocate the Fib here and continue. | 
 | 68 | 	 */ | 
 | 69 | 	while(aac_consumer_get(dev, q, &entry)) | 
 | 70 | 	{ | 
 | 71 | 		int fast; | 
 | 72 | 		u32 index = le32_to_cpu(entry->addr); | 
 | 73 | 		fast = index & 0x01; | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 74 | 		fib = &dev->fibs[index >> 2]; | 
| Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 75 | 		hwfib = fib->hw_fib_va; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | 		 | 
 | 77 | 		aac_consumer_free(dev, q, HostNormRespQueue); | 
 | 78 | 		/* | 
 | 79 | 		 *	Remove this fib from the Outstanding I/O queue. | 
 | 80 | 		 *	But only if it has not already been timed out. | 
 | 81 | 		 * | 
 | 82 | 		 *	If the fib has been timed out already, then just  | 
 | 83 | 		 *	continue. The caller has already been notified that | 
 | 84 | 		 *	the fib timed out. | 
 | 85 | 		 */ | 
| Mark Haverkamp | 03d4433 | 2007-03-15 10:27:45 -0700 | [diff] [blame] | 86 | 		dev->queues->queue[AdapNormCmdQueue].numpending--; | 
 | 87 |  | 
 | 88 | 		if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) { | 
 | 89 | 			spin_unlock_irqrestore(q->lock, flags); | 
 | 90 | 			aac_fib_complete(fib); | 
 | 91 | 			aac_fib_free(fib); | 
 | 92 | 			spin_lock_irqsave(q->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | 			continue; | 
 | 94 | 		} | 
 | 95 | 		spin_unlock_irqrestore(q->lock, flags); | 
 | 96 |  | 
 | 97 | 		if (fast) { | 
 | 98 | 			/* | 
 | 99 | 			 *	Doctor the fib | 
 | 100 | 			 */ | 
| Mark Haverkamp  | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 101 | 			*(__le32 *)hwfib->data = cpu_to_le32(ST_OK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 			hwfib->header.XferState |= cpu_to_le32(AdapterProcessed); | 
 | 103 | 		} | 
 | 104 |  | 
 | 105 | 		FIB_COUNTER_INCREMENT(aac_config.FibRecved); | 
 | 106 |  | 
 | 107 | 		if (hwfib->header.Command == cpu_to_le16(NuFileSystem)) | 
 | 108 | 		{ | 
| Mark Haverkamp  | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 109 | 			__le32 *pstatus = (__le32 *)hwfib->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | 			if (*pstatus & cpu_to_le32(0xffff0000)) | 
 | 111 | 				*pstatus = cpu_to_le32(ST_OK); | 
 | 112 | 		} | 
 | 113 | 		if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async))  | 
 | 114 | 		{ | 
 | 115 | 	        	if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected)) | 
 | 116 | 				FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved); | 
 | 117 | 			else  | 
 | 118 | 				FIB_COUNTER_INCREMENT(aac_config.AsyncRecved); | 
 | 119 | 			/* | 
 | 120 | 			 *	NOTE:  we cannot touch the fib after this | 
 | 121 | 			 *	    call, because it may have been deallocated. | 
 | 122 | 			 */ | 
| Salyzyn, Mark | b6ef70f | 2008-01-08 13:26:43 -0800 | [diff] [blame] | 123 | 			fib->flags = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 			fib->callback(fib->callback_data, fib); | 
 | 125 | 		} else { | 
 | 126 | 			unsigned long flagv; | 
 | 127 | 			spin_lock_irqsave(&fib->event_lock, flagv); | 
| Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 128 | 			if (!fib->done) | 
 | 129 | 				fib->done = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | 			up(&fib->event_wait); | 
 | 131 | 			spin_unlock_irqrestore(&fib->event_lock, flagv); | 
 | 132 | 			FIB_COUNTER_INCREMENT(aac_config.NormalRecved); | 
| Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 133 | 			if (fib->done == 2) { | 
 | 134 | 				aac_fib_complete(fib); | 
 | 135 | 				aac_fib_free(fib); | 
 | 136 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | 		} | 
 | 138 | 		consumed++; | 
 | 139 | 		spin_lock_irqsave(q->lock, flags); | 
 | 140 | 	} | 
 | 141 |  | 
 | 142 | 	if (consumed > aac_config.peak_fibs) | 
 | 143 | 		aac_config.peak_fibs = consumed; | 
 | 144 | 	if (consumed == 0)  | 
 | 145 | 		aac_config.zero_fibs++; | 
 | 146 |  | 
 | 147 | 	spin_unlock_irqrestore(q->lock, flags); | 
 | 148 | 	return 0; | 
 | 149 | } | 
 | 150 |  | 
 | 151 |  | 
 | 152 | /** | 
 | 153 |  *	aac_command_normal	-	handle commands | 
 | 154 |  *	@q: queue to process | 
 | 155 |  * | 
 | 156 |  *	This DPC routine will be queued when the adapter interrupts us to  | 
 | 157 |  *	let us know there is a command on our normal priority queue. We will  | 
 | 158 |  *	pull off all QE there are and wake up all the waiters before exiting. | 
 | 159 |  *	We will take a spinlock out on the queue before operating on it. | 
 | 160 |  */ | 
 | 161 |   | 
 | 162 | unsigned int aac_command_normal(struct aac_queue *q) | 
 | 163 | { | 
 | 164 | 	struct aac_dev * dev = q->dev; | 
 | 165 | 	struct aac_entry *entry; | 
 | 166 | 	unsigned long flags; | 
 | 167 |  | 
 | 168 | 	spin_lock_irqsave(q->lock, flags); | 
 | 169 |  | 
 | 170 | 	/* | 
 | 171 | 	 *	Keep pulling response QEs off the response queue and waking | 
 | 172 | 	 *	up the waiters until there are no more QEs. We then return | 
 | 173 | 	 *	back to the system. | 
 | 174 | 	 */ | 
 | 175 | 	while(aac_consumer_get(dev, q, &entry)) | 
 | 176 | 	{ | 
 | 177 | 		struct fib fibctx; | 
 | 178 | 		struct hw_fib * hw_fib; | 
 | 179 | 		u32 index; | 
 | 180 | 		struct fib *fib = &fibctx; | 
 | 181 | 		 | 
 | 182 | 		index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib); | 
 | 183 | 		hw_fib = &dev->aif_base_va[index]; | 
 | 184 | 		 | 
 | 185 | 		/* | 
 | 186 | 		 *	Allocate a FIB at all costs. For non queued stuff | 
 | 187 | 		 *	we can just use the stack so we are happy. We need | 
 | 188 | 		 *	a fib object in order to manage the linked lists | 
 | 189 | 		 */ | 
 | 190 | 		if (dev->aif_thread) | 
 | 191 | 			if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL) | 
 | 192 | 				fib = &fibctx; | 
 | 193 | 		 | 
 | 194 | 		memset(fib, 0, sizeof(struct fib)); | 
 | 195 | 		INIT_LIST_HEAD(&fib->fiblink); | 
 | 196 | 		fib->type = FSAFS_NTC_FIB_CONTEXT; | 
 | 197 | 		fib->size = sizeof(struct fib); | 
| Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 198 | 		fib->hw_fib_va = hw_fib; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 		fib->data = hw_fib->data; | 
 | 200 | 		fib->dev = dev; | 
 | 201 | 		 | 
 | 202 | 				 | 
 | 203 | 		if (dev->aif_thread && fib != &fibctx) { | 
 | 204 | 		        list_add_tail(&fib->fiblink, &q->cmdq); | 
 | 205 | 	 	        aac_consumer_free(dev, q, HostNormCmdQueue); | 
 | 206 | 		        wake_up_interruptible(&q->cmdready); | 
 | 207 | 		} else { | 
 | 208 | 	 	        aac_consumer_free(dev, q, HostNormCmdQueue); | 
 | 209 | 			spin_unlock_irqrestore(q->lock, flags); | 
 | 210 | 			/* | 
 | 211 | 			 *	Set the status of this FIB | 
 | 212 | 			 */ | 
| Mark Haverkamp  | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 213 | 			*(__le32 *)hw_fib->data = cpu_to_le32(ST_OK); | 
| Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 214 | 			aac_fib_adapter_complete(fib, sizeof(u32)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | 			spin_lock_irqsave(q->lock, flags); | 
 | 216 | 		}		 | 
 | 217 | 	} | 
 | 218 | 	spin_unlock_irqrestore(q->lock, flags); | 
 | 219 | 	return 0; | 
 | 220 | } | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 221 |  | 
 | 222 |  | 
 | 223 | /** | 
 | 224 |  *	aac_intr_normal	-	Handle command replies | 
 | 225 |  *	@dev: Device | 
 | 226 |  *	@index: completion reference | 
 | 227 |  * | 
 | 228 |  *	This DPC routine will be run when the adapter interrupts us to let us | 
 | 229 |  *	know there is a response on our normal priority queue. We will pull off | 
 | 230 |  *	all QE there are and wake up all the waiters before exiting. | 
 | 231 |  */ | 
 | 232 |  | 
| Salyzyn, Mark | b6ef70f | 2008-01-08 13:26:43 -0800 | [diff] [blame] | 233 | unsigned int aac_intr_normal(struct aac_dev * dev, u32 index) | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 234 | { | 
| Christoph Hellwig | f3307f7 | 2007-11-08 17:27:47 +0000 | [diff] [blame] | 235 | 	dprintk((KERN_INFO "aac_intr_normal(%p,%x)\n", dev, index)); | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 236 | 	if ((index & 0x00000002L)) { | 
 | 237 | 		struct hw_fib * hw_fib; | 
 | 238 | 		struct fib * fib; | 
 | 239 | 		struct aac_queue *q = &dev->queues->queue[HostNormCmdQueue]; | 
 | 240 | 		unsigned long flags; | 
 | 241 |  | 
 | 242 | 		if (index == 0xFFFFFFFEL) /* Special Case */ | 
 | 243 | 			return 0;	  /* Do nothing */ | 
 | 244 | 		/* | 
 | 245 | 		 *	Allocate a FIB. For non queued stuff we can just use | 
 | 246 | 		 * the stack so we are happy. We need a fib object in order to | 
 | 247 | 		 * manage the linked lists. | 
 | 248 | 		 */ | 
 | 249 | 		if ((!dev->aif_thread) | 
| Salyzyn, Mark | 4dbc22d | 2007-04-16 11:21:50 -0400 | [diff] [blame] | 250 | 		 || (!(fib = kzalloc(sizeof(struct fib),GFP_ATOMIC)))) | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 251 | 			return 1; | 
| Salyzyn, Mark | 4dbc22d | 2007-04-16 11:21:50 -0400 | [diff] [blame] | 252 | 		if (!(hw_fib = kzalloc(sizeof(struct hw_fib),GFP_ATOMIC))) { | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 253 | 			kfree (fib); | 
 | 254 | 			return 1; | 
 | 255 | 		} | 
| Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 256 | 		memcpy(hw_fib, (struct hw_fib *)(((uintptr_t)(dev->regs.sa)) + | 
| Salyzyn, Mark | 4dfb7cb | 2007-03-27 15:07:28 -0400 | [diff] [blame] | 257 | 		  (index & ~0x00000002L)), sizeof(struct hw_fib)); | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 258 | 		INIT_LIST_HEAD(&fib->fiblink); | 
 | 259 | 		fib->type = FSAFS_NTC_FIB_CONTEXT; | 
 | 260 | 		fib->size = sizeof(struct fib); | 
| Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 261 | 		fib->hw_fib_va = hw_fib; | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 262 | 		fib->data = hw_fib->data; | 
 | 263 | 		fib->dev = dev; | 
 | 264 | 	 | 
 | 265 | 		spin_lock_irqsave(q->lock, flags); | 
 | 266 | 		list_add_tail(&fib->fiblink, &q->cmdq); | 
 | 267 | 	        wake_up_interruptible(&q->cmdready); | 
 | 268 | 		spin_unlock_irqrestore(q->lock, flags); | 
 | 269 | 		return 1; | 
 | 270 | 	} else { | 
 | 271 | 		int fast = index & 0x01; | 
 | 272 | 		struct fib * fib = &dev->fibs[index >> 2]; | 
| Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 273 | 		struct hw_fib * hwfib = fib->hw_fib_va; | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 274 |  | 
 | 275 | 		/* | 
 | 276 | 		 *	Remove this fib from the Outstanding I/O queue. | 
 | 277 | 		 *	But only if it has not already been timed out. | 
 | 278 | 		 * | 
 | 279 | 		 *	If the fib has been timed out already, then just  | 
 | 280 | 		 *	continue. The caller has already been notified that | 
 | 281 | 		 *	the fib timed out. | 
 | 282 | 		 */ | 
| Mark Haverkamp | 03d4433 | 2007-03-15 10:27:45 -0700 | [diff] [blame] | 283 | 		dev->queues->queue[AdapNormCmdQueue].numpending--; | 
 | 284 |  | 
 | 285 | 		if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) { | 
 | 286 | 			aac_fib_complete(fib); | 
 | 287 | 			aac_fib_free(fib); | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 288 | 			return 0; | 
 | 289 | 		} | 
 | 290 |  | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 291 | 		if (fast) { | 
 | 292 | 			/* | 
 | 293 | 			 *	Doctor the fib | 
 | 294 | 			 */ | 
 | 295 | 			*(__le32 *)hwfib->data = cpu_to_le32(ST_OK); | 
 | 296 | 			hwfib->header.XferState |= cpu_to_le32(AdapterProcessed); | 
 | 297 | 		} | 
 | 298 |  | 
 | 299 | 		FIB_COUNTER_INCREMENT(aac_config.FibRecved); | 
 | 300 |  | 
 | 301 | 		if (hwfib->header.Command == cpu_to_le16(NuFileSystem)) | 
 | 302 | 		{ | 
| Christoph Hellwig | f3307f7 | 2007-11-08 17:27:47 +0000 | [diff] [blame] | 303 | 			__le32 *pstatus = (__le32 *)hwfib->data; | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 304 | 			if (*pstatus & cpu_to_le32(0xffff0000)) | 
 | 305 | 				*pstatus = cpu_to_le32(ST_OK); | 
 | 306 | 		} | 
 | 307 | 		if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async))  | 
 | 308 | 		{ | 
 | 309 | 	        	if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected)) | 
 | 310 | 				FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved); | 
 | 311 | 			else  | 
 | 312 | 				FIB_COUNTER_INCREMENT(aac_config.AsyncRecved); | 
 | 313 | 			/* | 
 | 314 | 			 *	NOTE:  we cannot touch the fib after this | 
 | 315 | 			 *	    call, because it may have been deallocated. | 
 | 316 | 			 */ | 
| Salyzyn, Mark | b6ef70f | 2008-01-08 13:26:43 -0800 | [diff] [blame] | 317 | 			fib->flags = 0; | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 318 | 			fib->callback(fib->callback_data, fib); | 
 | 319 | 		} else { | 
 | 320 | 			unsigned long flagv; | 
 | 321 | 	  		dprintk((KERN_INFO "event_wait up\n")); | 
 | 322 | 			spin_lock_irqsave(&fib->event_lock, flagv); | 
| Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 323 | 			if (!fib->done) | 
 | 324 | 				fib->done = 1; | 
| Mark Haverkamp | 8e0c5eb | 2005-10-24 10:52:22 -0700 | [diff] [blame] | 325 | 			up(&fib->event_wait); | 
 | 326 | 			spin_unlock_irqrestore(&fib->event_lock, flagv); | 
 | 327 | 			FIB_COUNTER_INCREMENT(aac_config.NormalRecved); | 
 | 328 | 		} | 
 | 329 | 		return 0; | 
 | 330 | 	} | 
 | 331 | } |