xref: /linux/drivers/scsi/aacraid/commctrl.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
1 /*
2  *	Adaptec AAC series RAID controller driver
3  *	(c) Copyright 2001 Red Hat Inc.	<alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
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  *  commctrl.c
26  *
27  * Abstract: Contains all routines for control of the AFA comm layer
28  *
29  */
30 
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/blkdev.h>
41 #include <asm/semaphore.h>
42 #include <asm/uaccess.h>
43 
44 #include "aacraid.h"
45 
46 /**
47  *	ioctl_send_fib	-	send a FIB from userspace
48  *	@dev:	adapter is being processed
49  *	@arg:	arguments to the ioctl call
50  *
51  *	This routine sends a fib to the adapter on behalf of a user level
52  *	program.
53  */
54 # define AAC_DEBUG_PREAMBLE	KERN_INFO
55 # define AAC_DEBUG_POSTAMBLE
56 
57 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
58 {
59 	struct hw_fib * kfib;
60 	struct fib *fibptr;
61 	struct hw_fib * hw_fib = (struct hw_fib *)0;
62 	dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
63 	unsigned size;
64 	int retval;
65 
66 	fibptr = aac_fib_alloc(dev);
67 	if(fibptr == NULL) {
68 		return -ENOMEM;
69 	}
70 
71 	kfib = fibptr->hw_fib;
72 	/*
73 	 *	First copy in the header so that we can check the size field.
74 	 */
75 	if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
76 		aac_fib_free(fibptr);
77 		return -EFAULT;
78 	}
79 	/*
80 	 *	Since we copy based on the fib header size, make sure that we
81 	 *	will not overrun the buffer when we copy the memory. Return
82 	 *	an error if we would.
83 	 */
84 	size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
85 	if (size < le16_to_cpu(kfib->header.SenderSize))
86 		size = le16_to_cpu(kfib->header.SenderSize);
87 	if (size > dev->max_fib_size) {
88 		if (size > 2048) {
89 			retval = -EINVAL;
90 			goto cleanup;
91 		}
92 		/* Highjack the hw_fib */
93 		hw_fib = fibptr->hw_fib;
94 		hw_fib_pa = fibptr->hw_fib_pa;
95 		fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
96 		memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
97 		memcpy(kfib, hw_fib, dev->max_fib_size);
98 	}
99 
100 	if (copy_from_user(kfib, arg, size)) {
101 		retval = -EFAULT;
102 		goto cleanup;
103 	}
104 
105 	if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
106 		aac_adapter_interrupt(dev);
107 		/*
108 		 * Since we didn't really send a fib, zero out the state to allow
109 		 * cleanup code not to assert.
110 		 */
111 		kfib->header.XferState = 0;
112 	} else {
113 		retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
114 				le16_to_cpu(kfib->header.Size) , FsaNormal,
115 				1, 1, NULL, NULL);
116 		if (retval) {
117 			goto cleanup;
118 		}
119 		if (aac_fib_complete(fibptr) != 0) {
120 			retval = -EINVAL;
121 			goto cleanup;
122 		}
123 	}
124 	/*
125 	 *	Make sure that the size returned by the adapter (which includes
126 	 *	the header) is less than or equal to the size of a fib, so we
127 	 *	don't corrupt application data. Then copy that size to the user
128 	 *	buffer. (Don't try to add the header information again, since it
129 	 *	was already included by the adapter.)
130 	 */
131 
132 	retval = 0;
133 	if (copy_to_user(arg, (void *)kfib, size))
134 		retval = -EFAULT;
135 cleanup:
136 	if (hw_fib) {
137 		pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
138 		fibptr->hw_fib_pa = hw_fib_pa;
139 		fibptr->hw_fib = hw_fib;
140 	}
141 	aac_fib_free(fibptr);
142 	return retval;
143 }
144 
145 /**
146  *	open_getadapter_fib	-	Get the next fib
147  *
148  *	This routine will get the next Fib, if available, from the AdapterFibContext
149  *	passed in from the user.
150  */
151 
152 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
153 {
154 	struct aac_fib_context * fibctx;
155 	int status;
156 
157 	fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
158 	if (fibctx == NULL) {
159 		status = -ENOMEM;
160 	} else {
161 		unsigned long flags;
162 		struct list_head * entry;
163 		struct aac_fib_context * context;
164 
165 		fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
166 		fibctx->size = sizeof(struct aac_fib_context);
167  		/*
168 		 *	Yes yes, I know this could be an index, but we have a
169 		 * better guarantee of uniqueness for the locked loop below.
170 		 * Without the aid of a persistent history, this also helps
171 		 * reduce the chance that the opaque context would be reused.
172 		 */
173 		fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
174 		/*
175 		 *	Initialize the mutex used to wait for the next AIF.
176 		 */
177 		init_MUTEX_LOCKED(&fibctx->wait_sem);
178 		fibctx->wait = 0;
179 		/*
180 		 *	Initialize the fibs and set the count of fibs on
181 		 *	the list to 0.
182 		 */
183 		fibctx->count = 0;
184 		INIT_LIST_HEAD(&fibctx->fib_list);
185 		fibctx->jiffies = jiffies/HZ;
186 		/*
187 		 *	Now add this context onto the adapter's
188 		 *	AdapterFibContext list.
189 		 */
190 		spin_lock_irqsave(&dev->fib_lock, flags);
191 		/* Ensure that we have a unique identifier */
192 		entry = dev->fib_list.next;
193 		while (entry != &dev->fib_list) {
194 			context = list_entry(entry, struct aac_fib_context, next);
195 			if (context->unique == fibctx->unique) {
196 				/* Not unique (32 bits) */
197 				fibctx->unique++;
198 				entry = dev->fib_list.next;
199 			} else {
200 				entry = entry->next;
201 			}
202 		}
203 		list_add_tail(&fibctx->next, &dev->fib_list);
204 		spin_unlock_irqrestore(&dev->fib_lock, flags);
205 		if (copy_to_user(arg,  &fibctx->unique,
206 						sizeof(fibctx->unique))) {
207 			status = -EFAULT;
208 		} else {
209 			status = 0;
210 		}
211 	}
212 	return status;
213 }
214 
215 /**
216  *	next_getadapter_fib	-	get the next fib
217  *	@dev: adapter to use
218  *	@arg: ioctl argument
219  *
220  * 	This routine will get the next Fib, if available, from the AdapterFibContext
221  *	passed in from the user.
222  */
223 
224 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
225 {
226 	struct fib_ioctl f;
227 	struct fib *fib;
228 	struct aac_fib_context *fibctx;
229 	int status;
230 	struct list_head * entry;
231 	unsigned long flags;
232 
233 	if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
234 		return -EFAULT;
235 	/*
236 	 *	Verify that the HANDLE passed in was a valid AdapterFibContext
237 	 *
238 	 *	Search the list of AdapterFibContext addresses on the adapter
239 	 *	to be sure this is a valid address
240 	 */
241 	entry = dev->fib_list.next;
242 	fibctx = NULL;
243 
244 	while (entry != &dev->fib_list) {
245 		fibctx = list_entry(entry, struct aac_fib_context, next);
246 		/*
247 		 *	Extract the AdapterFibContext from the Input parameters.
248 		 */
249 		if (fibctx->unique == f.fibctx) {   /* We found a winner */
250 			break;
251 		}
252 		entry = entry->next;
253 		fibctx = NULL;
254 	}
255 	if (!fibctx) {
256 		dprintk ((KERN_INFO "Fib Context not found\n"));
257 		return -EINVAL;
258 	}
259 
260 	if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
261 		 (fibctx->size != sizeof(struct aac_fib_context))) {
262 		dprintk ((KERN_INFO "Fib Context corrupt?\n"));
263 		return -EINVAL;
264 	}
265 	status = 0;
266 	spin_lock_irqsave(&dev->fib_lock, flags);
267 	/*
268 	 *	If there are no fibs to send back, then either wait or return
269 	 *	-EAGAIN
270 	 */
271 return_fib:
272 	if (!list_empty(&fibctx->fib_list)) {
273 		struct list_head * entry;
274 		/*
275 		 *	Pull the next fib from the fibs
276 		 */
277 		entry = fibctx->fib_list.next;
278 		list_del(entry);
279 
280 		fib = list_entry(entry, struct fib, fiblink);
281 		fibctx->count--;
282 		spin_unlock_irqrestore(&dev->fib_lock, flags);
283 		if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
284 			kfree(fib->hw_fib);
285 			kfree(fib);
286 			return -EFAULT;
287 		}
288 		/*
289 		 *	Free the space occupied by this copy of the fib.
290 		 */
291 		kfree(fib->hw_fib);
292 		kfree(fib);
293 		status = 0;
294 	} else {
295 		spin_unlock_irqrestore(&dev->fib_lock, flags);
296 		if (f.wait) {
297 			if(down_interruptible(&fibctx->wait_sem) < 0) {
298 				status = -EINTR;
299 			} else {
300 				/* Lock again and retry */
301 				spin_lock_irqsave(&dev->fib_lock, flags);
302 				goto return_fib;
303 			}
304 		} else {
305 			status = -EAGAIN;
306 		}
307 	}
308 	fibctx->jiffies = jiffies/HZ;
309 	return status;
310 }
311 
312 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
313 {
314 	struct fib *fib;
315 
316 	/*
317 	 *	First free any FIBs that have not been consumed.
318 	 */
319 	while (!list_empty(&fibctx->fib_list)) {
320 		struct list_head * entry;
321 		/*
322 		 *	Pull the next fib from the fibs
323 		 */
324 		entry = fibctx->fib_list.next;
325 		list_del(entry);
326 		fib = list_entry(entry, struct fib, fiblink);
327 		fibctx->count--;
328 		/*
329 		 *	Free the space occupied by this copy of the fib.
330 		 */
331 		kfree(fib->hw_fib);
332 		kfree(fib);
333 	}
334 	/*
335 	 *	Remove the Context from the AdapterFibContext List
336 	 */
337 	list_del(&fibctx->next);
338 	/*
339 	 *	Invalidate context
340 	 */
341 	fibctx->type = 0;
342 	/*
343 	 *	Free the space occupied by the Context
344 	 */
345 	kfree(fibctx);
346 	return 0;
347 }
348 
349 /**
350  *	close_getadapter_fib	-	close down user fib context
351  *	@dev: adapter
352  *	@arg: ioctl arguments
353  *
354  *	This routine will close down the fibctx passed in from the user.
355  */
356 
357 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
358 {
359 	struct aac_fib_context *fibctx;
360 	int status;
361 	unsigned long flags;
362 	struct list_head * entry;
363 
364 	/*
365 	 *	Verify that the HANDLE passed in was a valid AdapterFibContext
366 	 *
367 	 *	Search the list of AdapterFibContext addresses on the adapter
368 	 *	to be sure this is a valid address
369 	 */
370 
371 	entry = dev->fib_list.next;
372 	fibctx = NULL;
373 
374 	while(entry != &dev->fib_list) {
375 		fibctx = list_entry(entry, struct aac_fib_context, next);
376 		/*
377 		 *	Extract the fibctx from the input parameters
378 		 */
379 		if (fibctx->unique == (u32)(unsigned long)arg) {
380 			/* We found a winner */
381 			break;
382 		}
383 		entry = entry->next;
384 		fibctx = NULL;
385 	}
386 
387 	if (!fibctx)
388 		return 0; /* Already gone */
389 
390 	if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
391 		 (fibctx->size != sizeof(struct aac_fib_context)))
392 		return -EINVAL;
393 	spin_lock_irqsave(&dev->fib_lock, flags);
394 	status = aac_close_fib_context(dev, fibctx);
395 	spin_unlock_irqrestore(&dev->fib_lock, flags);
396 	return status;
397 }
398 
399 /**
400  *	check_revision	-	close down user fib context
401  *	@dev: adapter
402  *	@arg: ioctl arguments
403  *
404  *	This routine returns the driver version.
405  *      Under Linux, there have been no version incompatibilities, so this is
406  *      simple!
407  */
408 
409 static int check_revision(struct aac_dev *dev, void __user *arg)
410 {
411 	struct revision response;
412 	char *driver_version = aac_driver_version;
413 	u32 version;
414 
415 	response.compat = 1;
416 	version = (simple_strtol(driver_version,
417 				&driver_version, 10) << 24) | 0x00000400;
418 	version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
419 	version += simple_strtol(driver_version + 1, NULL, 10);
420 	response.version = cpu_to_le32(version);
421 #	if (defined(AAC_DRIVER_BUILD))
422 		response.build = cpu_to_le32(AAC_DRIVER_BUILD);
423 #	else
424 		response.build = cpu_to_le32(9999);
425 #	endif
426 
427 	if (copy_to_user(arg, &response, sizeof(response)))
428 		return -EFAULT;
429 	return 0;
430 }
431 
432 
433 /**
434  *
435  * aac_send_raw_scb
436  *
437  */
438 
439 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
440 {
441 	struct fib* srbfib;
442 	int status;
443 	struct aac_srb *srbcmd = NULL;
444 	struct user_aac_srb *user_srbcmd = NULL;
445 	struct user_aac_srb __user *user_srb = arg;
446 	struct aac_srb_reply __user *user_reply;
447 	struct aac_srb_reply* reply;
448 	u32 fibsize = 0;
449 	u32 flags = 0;
450 	s32 rcode = 0;
451 	u32 data_dir;
452 	void __user *sg_user[32];
453 	void *sg_list[32];
454 	u32   sg_indx = 0;
455 	u32 byte_count = 0;
456 	u32 actual_fibsize = 0;
457 	int i;
458 
459 
460 	if (!capable(CAP_SYS_ADMIN)){
461 		dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
462 		return -EPERM;
463 	}
464 	/*
465 	 *	Allocate and initialize a Fib then setup a BlockWrite command
466 	 */
467 	if (!(srbfib = aac_fib_alloc(dev))) {
468 		return -ENOMEM;
469 	}
470 	aac_fib_init(srbfib);
471 
472 	srbcmd = (struct aac_srb*) fib_data(srbfib);
473 
474 	memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
475 	if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
476 		dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
477 		rcode = -EFAULT;
478 		goto cleanup;
479 	}
480 
481 	if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
482 		rcode = -EINVAL;
483 		goto cleanup;
484 	}
485 
486 	user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
487 	if (!user_srbcmd) {
488 		dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
489 		rcode = -ENOMEM;
490 		goto cleanup;
491 	}
492 	if(copy_from_user(user_srbcmd, user_srb,fibsize)){
493 		dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
494 		rcode = -EFAULT;
495 		goto cleanup;
496 	}
497 
498 	user_reply = arg+fibsize;
499 
500 	flags = user_srbcmd->flags; /* from user in cpu order */
501 	// Fix up srb for endian and force some values
502 
503 	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);	// Force this
504 	srbcmd->channel  = cpu_to_le32(user_srbcmd->channel);
505 	srbcmd->id	 = cpu_to_le32(user_srbcmd->id);
506 	srbcmd->lun      = cpu_to_le32(user_srbcmd->lun);
507 	srbcmd->timeout  = cpu_to_le32(user_srbcmd->timeout);
508 	srbcmd->flags    = cpu_to_le32(flags);
509 	srbcmd->retry_limit = 0; // Obsolete parameter
510 	srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
511 	memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
512 
513 	switch (flags & (SRB_DataIn | SRB_DataOut)) {
514 	case SRB_DataOut:
515 		data_dir = DMA_TO_DEVICE;
516 		break;
517 	case (SRB_DataIn | SRB_DataOut):
518 		data_dir = DMA_BIDIRECTIONAL;
519 		break;
520 	case SRB_DataIn:
521 		data_dir = DMA_FROM_DEVICE;
522 		break;
523 	default:
524 		data_dir = DMA_NONE;
525 	}
526 	if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) {
527 		dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
528 		  le32_to_cpu(srbcmd->sg.count)));
529 		rcode = -EINVAL;
530 		goto cleanup;
531 	}
532 	if (dev->dac_support == 1) {
533 		struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
534 		struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
535 		struct user_sgmap* usg;
536 		byte_count = 0;
537 
538 		/*
539 		 * This should also catch if user used the 32 bit sgmap
540 		 */
541 		actual_fibsize = sizeof(struct aac_srb) -
542 			sizeof(struct sgentry) +
543 			((upsg->count & 0xff) *
544 		 	sizeof(struct sgentry));
545 		if(actual_fibsize != fibsize){ // User made a mistake - should not continue
546 			dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
547 			rcode = -EINVAL;
548 			goto cleanup;
549 		}
550 		usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
551 		  + sizeof(struct sgmap), GFP_KERNEL);
552 		if (!usg) {
553 			dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
554 			rcode = -ENOMEM;
555 			goto cleanup;
556 		}
557 		memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
558 		  + sizeof(struct sgmap));
559 		actual_fibsize = sizeof(struct aac_srb) -
560 			sizeof(struct sgentry) + ((usg->count & 0xff) *
561 			 	sizeof(struct sgentry64));
562 		if ((data_dir == DMA_NONE) && upsg->count) {
563 			kfree (usg);
564 			dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
565 			rcode = -EINVAL;
566 			goto cleanup;
567 		}
568 
569 		for (i = 0; i < usg->count; i++) {
570 			u64 addr;
571 			void* p;
572 			/* Does this really need to be GFP_DMA? */
573 			p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
574 			if(p == 0) {
575 				kfree (usg);
576 				dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
577 				  usg->sg[i].count,i,usg->count));
578 				rcode = -ENOMEM;
579 				goto cleanup;
580 			}
581 			sg_user[i] = (void __user *)(long)usg->sg[i].addr;
582 			sg_list[i] = p; // save so we can clean up later
583 			sg_indx = i;
584 
585 			if( flags & SRB_DataOut ){
586 				if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
587 					kfree (usg);
588 					dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
589 					rcode = -EFAULT;
590 					goto cleanup;
591 				}
592 			}
593 			addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
594 
595 			psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
596 			psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
597 			psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
598 			byte_count += usg->sg[i].count;
599 		}
600 		kfree (usg);
601 
602 		srbcmd->count = cpu_to_le32(byte_count);
603 		psg->count = cpu_to_le32(sg_indx+1);
604 		status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
605 	} else {
606 		struct user_sgmap* upsg = &user_srbcmd->sg;
607 		struct sgmap* psg = &srbcmd->sg;
608 		byte_count = 0;
609 
610 		actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
611 		if(actual_fibsize != fibsize){ // User made a mistake - should not continue
612 			dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
613 			rcode = -EINVAL;
614 			goto cleanup;
615 		}
616 		if ((data_dir == DMA_NONE) && upsg->count) {
617 			dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
618 			rcode = -EINVAL;
619 			goto cleanup;
620 		}
621 		for (i = 0; i < upsg->count; i++) {
622 			dma_addr_t addr;
623 			void* p;
624 			p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
625 			if(p == 0) {
626 				dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
627 				  upsg->sg[i].count, i, upsg->count));
628 				rcode = -ENOMEM;
629 				goto cleanup;
630 			}
631 			sg_user[i] = (void __user *)(long)upsg->sg[i].addr;
632 			sg_list[i] = p; // save so we can clean up later
633 			sg_indx = i;
634 
635 			if( flags & SRB_DataOut ){
636 				if(copy_from_user(p, sg_user[i],
637 						upsg->sg[i].count)) {
638 					dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
639 					rcode = -EFAULT;
640 					goto cleanup;
641 				}
642 			}
643 			addr = pci_map_single(dev->pdev, p,
644 				upsg->sg[i].count, data_dir);
645 
646 			psg->sg[i].addr = cpu_to_le32(addr);
647 			psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
648 			byte_count += upsg->sg[i].count;
649 		}
650 		srbcmd->count = cpu_to_le32(byte_count);
651 		psg->count = cpu_to_le32(sg_indx+1);
652 		status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
653 	}
654 
655 	if (status != 0){
656 		dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
657 		rcode = -ENXIO;
658 		goto cleanup;
659 	}
660 
661 	if( flags & SRB_DataIn ) {
662 		for(i = 0 ; i <= sg_indx; i++){
663 			byte_count = le32_to_cpu((dev->dac_support == 1)
664 			      ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
665 			      : srbcmd->sg.sg[i].count);
666 			if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
667 				dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
668 				rcode = -EFAULT;
669 				goto cleanup;
670 
671 			}
672 		}
673 	}
674 
675 	reply = (struct aac_srb_reply *) fib_data(srbfib);
676 	if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
677 		dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
678 		rcode = -EFAULT;
679 		goto cleanup;
680 	}
681 
682 cleanup:
683 	kfree(user_srbcmd);
684 	for(i=0; i <= sg_indx; i++){
685 		kfree(sg_list[i]);
686 	}
687 	aac_fib_complete(srbfib);
688 	aac_fib_free(srbfib);
689 
690 	return rcode;
691 }
692 
693 struct aac_pci_info {
694         u32 bus;
695         u32 slot;
696 };
697 
698 
699 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
700 {
701         struct aac_pci_info pci_info;
702 
703 	pci_info.bus = dev->pdev->bus->number;
704 	pci_info.slot = PCI_SLOT(dev->pdev->devfn);
705 
706        if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
707                dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
708                return -EFAULT;
709 	}
710         return 0;
711 }
712 
713 
714 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
715 {
716 	int status;
717 
718 	/*
719 	 *	HBA gets first crack
720 	 */
721 
722 	status = aac_dev_ioctl(dev, cmd, arg);
723 	if(status != -ENOTTY)
724 		return status;
725 
726 	switch (cmd) {
727 	case FSACTL_MINIPORT_REV_CHECK:
728 		status = check_revision(dev, arg);
729 		break;
730 	case FSACTL_SEND_LARGE_FIB:
731 	case FSACTL_SENDFIB:
732 		status = ioctl_send_fib(dev, arg);
733 		break;
734 	case FSACTL_OPEN_GET_ADAPTER_FIB:
735 		status = open_getadapter_fib(dev, arg);
736 		break;
737 	case FSACTL_GET_NEXT_ADAPTER_FIB:
738 		status = next_getadapter_fib(dev, arg);
739 		break;
740 	case FSACTL_CLOSE_GET_ADAPTER_FIB:
741 		status = close_getadapter_fib(dev, arg);
742 		break;
743 	case FSACTL_SEND_RAW_SRB:
744 		status = aac_send_raw_srb(dev,arg);
745 		break;
746 	case FSACTL_GET_PCI_INFO:
747 		status = aac_get_pci_info(dev,arg);
748 		break;
749 	default:
750 		status = -ENOTTY;
751 	  	break;
752 	}
753 	return status;
754 }
755 
756