commctrl.c 23.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *	Adaptec AAC series RAID controller driver
3
 *	(c) Copyright 2001 Red Hat Inc.
L
Linus Torvalds 已提交
4 5 6 7
 *
 * based on the old aacraid driver that is..
 * Adaptec aacraid device driver for Linux.
 *
8
 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Module Name:
 *  commctrl.c
 *
 * Abstract: Contains all routines for control of the AFA comm layer
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/completion.h>
#include <linux/dma-mapping.h>
#include <linux/blkdev.h>
40
#include <linux/delay.h> /* ssleep prototype */
41
#include <linux/kthread.h>
42
#include <linux/semaphore.h>
L
Linus Torvalds 已提交
43
#include <asm/uaccess.h>
44
#include <scsi/scsi_host.h>
L
Linus Torvalds 已提交
45 46 47 48 49 50 51

#include "aacraid.h"

/**
 *	ioctl_send_fib	-	send a FIB from userspace
 *	@dev:	adapter is being processed
 *	@arg:	arguments to the ioctl call
52
 *
L
Linus Torvalds 已提交
53 54 55
 *	This routine sends a fib to the adapter on behalf of a user level
 *	program.
 */
56 57
# define AAC_DEBUG_PREAMBLE	KERN_INFO
# define AAC_DEBUG_POSTAMBLE
58

L
Linus Torvalds 已提交
59 60 61 62
static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
{
	struct hw_fib * kfib;
	struct fib *fibptr;
63 64 65 66
	struct hw_fib * hw_fib = (struct hw_fib *)0;
	dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
	unsigned size;
	int retval;
L
Linus Torvalds 已提交
67

68 69 70
	if (dev->in_reset) {
		return -EBUSY;
	}
71
	fibptr = aac_fib_alloc(dev);
72
	if(fibptr == NULL) {
L
Linus Torvalds 已提交
73
		return -ENOMEM;
74
	}
75

76
	kfib = fibptr->hw_fib_va;
L
Linus Torvalds 已提交
77 78 79 80
	/*
	 *	First copy in the header so that we can check the size field.
	 */
	if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
81
		aac_fib_free(fibptr);
L
Linus Torvalds 已提交
82 83 84 85 86 87 88
		return -EFAULT;
	}
	/*
	 *	Since we copy based on the fib header size, make sure that we
	 *	will not overrun the buffer when we copy the memory. Return
	 *	an error if we would.
	 */
89 90 91 92
	size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
	if (size < le16_to_cpu(kfib->header.SenderSize))
		size = le16_to_cpu(kfib->header.SenderSize);
	if (size > dev->max_fib_size) {
93 94
		dma_addr_t daddr;

95 96 97 98
		if (size > 2048) {
			retval = -EINVAL;
			goto cleanup;
		}
99 100 101 102 103 104 105

		kfib = pci_alloc_consistent(dev->pdev, size, &daddr);
		if (!kfib) {
			retval = -ENOMEM;
			goto cleanup;
		}

106
		/* Highjack the hw_fib */
107
		hw_fib = fibptr->hw_fib_va;
108
		hw_fib_pa = fibptr->hw_fib_pa;
109 110
		fibptr->hw_fib_va = kfib;
		fibptr->hw_fib_pa = daddr;
111 112
		memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
		memcpy(kfib, hw_fib, dev->max_fib_size);
L
Linus Torvalds 已提交
113 114
	}

115 116 117
	if (copy_from_user(kfib, arg, size)) {
		retval = -EFAULT;
		goto cleanup;
L
Linus Torvalds 已提交
118 119
	}

120
	if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
L
Linus Torvalds 已提交
121 122
		aac_adapter_interrupt(dev);
		/*
123
		 * Since we didn't really send a fib, zero out the state to allow
L
Linus Torvalds 已提交
124 125 126 127
		 * cleanup code not to assert.
		 */
		kfib->header.XferState = 0;
	} else {
128
		retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
L
Linus Torvalds 已提交
129 130 131
				le16_to_cpu(kfib->header.Size) , FsaNormal,
				1, 1, NULL, NULL);
		if (retval) {
132
			goto cleanup;
L
Linus Torvalds 已提交
133
		}
134
		if (aac_fib_complete(fibptr) != 0) {
135 136
			retval = -EINVAL;
			goto cleanup;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146
		}
	}
	/*
	 *	Make sure that the size returned by the adapter (which includes
	 *	the header) is less than or equal to the size of a fib, so we
	 *	don't corrupt application data. Then copy that size to the user
	 *	buffer. (Don't try to add the header information again, since it
	 *	was already included by the adapter.)
	 */

147 148 149 150 151 152 153
	retval = 0;
	if (copy_to_user(arg, (void *)kfib, size))
		retval = -EFAULT;
cleanup:
	if (hw_fib) {
		pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
		fibptr->hw_fib_pa = hw_fib_pa;
154
		fibptr->hw_fib_va = hw_fib;
L
Linus Torvalds 已提交
155
	}
156
	if (retval != -ERESTARTSYS)
157
		aac_fib_free(fibptr);
158
	return retval;
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

/**
 *	open_getadapter_fib	-	Get the next fib
 *
 *	This routine will get the next Fib, if available, from the AdapterFibContext
 *	passed in from the user.
 */

static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
{
	struct aac_fib_context * fibctx;
	int status;

	fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
	if (fibctx == NULL) {
		status = -ENOMEM;
	} else {
		unsigned long flags;
		struct list_head * entry;
		struct aac_fib_context * context;

		fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
		fibctx->size = sizeof(struct aac_fib_context);
183
		/*
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		 *	Yes yes, I know this could be an index, but we have a
		 * better guarantee of uniqueness for the locked loop below.
		 * Without the aid of a persistent history, this also helps
		 * reduce the chance that the opaque context would be reused.
		 */
		fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
		/*
		 *	Initialize the mutex used to wait for the next AIF.
		 */
		init_MUTEX_LOCKED(&fibctx->wait_sem);
		fibctx->wait = 0;
		/*
		 *	Initialize the fibs and set the count of fibs on
		 *	the list to 0.
		 */
		fibctx->count = 0;
		INIT_LIST_HEAD(&fibctx->fib_list);
		fibctx->jiffies = jiffies/HZ;
		/*
203
		 *	Now add this context onto the adapter's
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		 *	AdapterFibContext list.
		 */
		spin_lock_irqsave(&dev->fib_lock, flags);
		/* Ensure that we have a unique identifier */
		entry = dev->fib_list.next;
		while (entry != &dev->fib_list) {
			context = list_entry(entry, struct aac_fib_context, next);
			if (context->unique == fibctx->unique) {
				/* Not unique (32 bits) */
				fibctx->unique++;
				entry = dev->fib_list.next;
			} else {
				entry = entry->next;
			}
		}
		list_add_tail(&fibctx->next, &dev->fib_list);
		spin_unlock_irqrestore(&dev->fib_lock, flags);
221
		if (copy_to_user(arg, &fibctx->unique,
L
Linus Torvalds 已提交
222 223 224 225
						sizeof(fibctx->unique))) {
			status = -EFAULT;
		} else {
			status = 0;
226
		}
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234
	}
	return status;
}

/**
 *	next_getadapter_fib	-	get the next fib
 *	@dev: adapter to use
 *	@arg: ioctl argument
235 236
 *
 *	This routine will get the next Fib, if available, from the AdapterFibContext
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247
 *	passed in from the user.
 */

static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
{
	struct fib_ioctl f;
	struct fib *fib;
	struct aac_fib_context *fibctx;
	int status;
	struct list_head * entry;
	unsigned long flags;
248

L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256
	if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
		return -EFAULT;
	/*
	 *	Verify that the HANDLE passed in was a valid AdapterFibContext
	 *
	 *	Search the list of AdapterFibContext addresses on the adapter
	 *	to be sure this is a valid address
	 */
257
	spin_lock_irqsave(&dev->fib_lock, flags);
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265
	entry = dev->fib_list.next;
	fibctx = NULL;

	while (entry != &dev->fib_list) {
		fibctx = list_entry(entry, struct aac_fib_context, next);
		/*
		 *	Extract the AdapterFibContext from the Input parameters.
		 */
266
		if (fibctx->unique == f.fibctx) { /* We found a winner */
L
Linus Torvalds 已提交
267 268 269 270 271 272
			break;
		}
		entry = entry->next;
		fibctx = NULL;
	}
	if (!fibctx) {
273
		spin_unlock_irqrestore(&dev->fib_lock, flags);
L
Linus Torvalds 已提交
274 275 276 277 278 279
		dprintk ((KERN_INFO "Fib Context not found\n"));
		return -EINVAL;
	}

	if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
		 (fibctx->size != sizeof(struct aac_fib_context))) {
280
		spin_unlock_irqrestore(&dev->fib_lock, flags);
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
		dprintk ((KERN_INFO "Fib Context corrupt?\n"));
		return -EINVAL;
	}
	status = 0;
	/*
	 *	If there are no fibs to send back, then either wait or return
	 *	-EAGAIN
	 */
return_fib:
	if (!list_empty(&fibctx->fib_list)) {
		/*
		 *	Pull the next fib from the fibs
		 */
		entry = fibctx->fib_list.next;
		list_del(entry);
296

L
Linus Torvalds 已提交
297 298 299
		fib = list_entry(entry, struct fib, fiblink);
		fibctx->count--;
		spin_unlock_irqrestore(&dev->fib_lock, flags);
300 301
		if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
			kfree(fib->hw_fib_va);
L
Linus Torvalds 已提交
302 303
			kfree(fib);
			return -EFAULT;
304
		}
L
Linus Torvalds 已提交
305 306 307
		/*
		 *	Free the space occupied by this copy of the fib.
		 */
308
		kfree(fib->hw_fib_va);
L
Linus Torvalds 已提交
309 310 311 312
		kfree(fib);
		status = 0;
	} else {
		spin_unlock_irqrestore(&dev->fib_lock, flags);
313 314
		/* If someone killed the AIF aacraid thread, restart it */
		status = !dev->aif_thread;
315
		if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
316 317 318 319 320 321 322
			/* Be paranoid, be very paranoid! */
			kthread_stop(dev->thread);
			ssleep(1);
			dev->aif_thread = 0;
			dev->thread = kthread_run(aac_command_thread, dev, dev->name);
			ssleep(1);
		}
L
Linus Torvalds 已提交
323 324
		if (f.wait) {
			if(down_interruptible(&fibctx->wait_sem) < 0) {
325
				status = -ERESTARTSYS;
L
Linus Torvalds 已提交
326 327 328 329 330 331 332
			} else {
				/* Lock again and retry */
				spin_lock_irqsave(&dev->fib_lock, flags);
				goto return_fib;
			}
		} else {
			status = -EAGAIN;
333
		}
L
Linus Torvalds 已提交
334
	}
335
	fibctx->jiffies = jiffies/HZ;
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	return status;
}

int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
{
	struct fib *fib;

	/*
	 *	First free any FIBs that have not been consumed.
	 */
	while (!list_empty(&fibctx->fib_list)) {
		struct list_head * entry;
		/*
		 *	Pull the next fib from the fibs
		 */
		entry = fibctx->fib_list.next;
		list_del(entry);
		fib = list_entry(entry, struct fib, fiblink);
		fibctx->count--;
		/*
		 *	Free the space occupied by this copy of the fib.
		 */
358
		kfree(fib->hw_fib_va);
L
Linus Torvalds 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		kfree(fib);
	}
	/*
	 *	Remove the Context from the AdapterFibContext List
	 */
	list_del(&fibctx->next);
	/*
	 *	Invalidate context
	 */
	fibctx->type = 0;
	/*
	 *	Free the space occupied by the Context
	 */
	kfree(fibctx);
	return 0;
}

/**
 *	close_getadapter_fib	-	close down user fib context
 *	@dev: adapter
 *	@arg: ioctl arguments
 *
 *	This routine will close down the fibctx passed in from the user.
 */
383

L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
{
	struct aac_fib_context *fibctx;
	int status;
	unsigned long flags;
	struct list_head * entry;

	/*
	 *	Verify that the HANDLE passed in was a valid AdapterFibContext
	 *
	 *	Search the list of AdapterFibContext addresses on the adapter
	 *	to be sure this is a valid address
	 */

	entry = dev->fib_list.next;
	fibctx = NULL;

	while(entry != &dev->fib_list) {
		fibctx = list_entry(entry, struct aac_fib_context, next);
		/*
		 *	Extract the fibctx from the input parameters
		 */
A
Al Viro 已提交
406
		if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
			break;
		entry = entry->next;
		fibctx = NULL;
	}

	if (!fibctx)
		return 0; /* Already gone */

	if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
		 (fibctx->size != sizeof(struct aac_fib_context)))
		return -EINVAL;
	spin_lock_irqsave(&dev->fib_lock, flags);
	status = aac_close_fib_context(dev, fibctx);
	spin_unlock_irqrestore(&dev->fib_lock, flags);
	return status;
}

/**
 *	check_revision	-	close down user fib context
 *	@dev: adapter
 *	@arg: ioctl arguments
 *
 *	This routine returns the driver version.
430 431
 *	Under Linux, there have been no version incompatibilities, so this is
 *	simple!
L
Linus Torvalds 已提交
432 433 434 435 436
 */

static int check_revision(struct aac_dev *dev, void __user *arg)
{
	struct revision response;
437 438 439
	char *driver_version = aac_driver_version;
	u32 version;

440
	response.compat = 1;
441
	version = (simple_strtol(driver_version,
442 443 444 445
				&driver_version, 10) << 24) | 0x00000400;
	version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
	version += simple_strtol(driver_version + 1, NULL, 10);
	response.version = cpu_to_le32(version);
446
#	ifdef AAC_DRIVER_BUILD
447 448 449 450
		response.build = cpu_to_le32(AAC_DRIVER_BUILD);
#	else
		response.build = cpu_to_le32(9999);
#	endif
L
Linus Torvalds 已提交
451 452 453 454 455 456

	if (copy_to_user(arg, &response, sizeof(response)))
		return -EFAULT;
	return 0;
}

457

L
Linus Torvalds 已提交
458 459 460 461 462 463
/**
 *
 * aac_send_raw_scb
 *
 */

464
static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
L
Linus Torvalds 已提交
465 466 467
{
	struct fib* srbfib;
	int status;
468 469 470
	struct aac_srb *srbcmd = NULL;
	struct user_aac_srb *user_srbcmd = NULL;
	struct user_aac_srb __user *user_srb = arg;
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478
	struct aac_srb_reply __user *user_reply;
	struct aac_srb_reply* reply;
	u32 fibsize = 0;
	u32 flags = 0;
	s32 rcode = 0;
	u32 data_dir;
	void __user *sg_user[32];
	void *sg_list[32];
479
	u32 sg_indx = 0;
L
Linus Torvalds 已提交
480
	u32 byte_count = 0;
481
	u32 actual_fibsize64, actual_fibsize = 0;
L
Linus Torvalds 已提交
482 483 484
	int i;


485 486 487 488
	if (dev->in_reset) {
		dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
		return -EBUSY;
	}
L
Linus Torvalds 已提交
489
	if (!capable(CAP_SYS_ADMIN)){
490
		dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
L
Linus Torvalds 已提交
491 492 493
		return -EPERM;
	}
	/*
494
	 *	Allocate and initialize a Fib then setup a SRB command
L
Linus Torvalds 已提交
495
	 */
496
	if (!(srbfib = aac_fib_alloc(dev))) {
M
Mark Haverkamp 已提交
497
		return -ENOMEM;
L
Linus Torvalds 已提交
498
	}
499
	aac_fib_init(srbfib);
L
Linus Torvalds 已提交
500 501 502

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

503
	memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
L
Linus Torvalds 已提交
504
	if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
505
		dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
L
Linus Torvalds 已提交
506 507 508 509
		rcode = -EFAULT;
		goto cleanup;
	}

510
	if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
L
Linus Torvalds 已提交
511 512 513 514
		rcode = -EINVAL;
		goto cleanup;
	}

515
	user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
516 517 518 519 520
	if (!user_srbcmd) {
		dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
		rcode = -ENOMEM;
		goto cleanup;
	}
521
	if(copy_from_user(user_srbcmd, user_srb,fibsize)){
522
		dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
L
Linus Torvalds 已提交
523 524 525 526 527 528
		rcode = -EFAULT;
		goto cleanup;
	}

	user_reply = arg+fibsize;

529
	flags = user_srbcmd->flags; /* from user in cpu order */
L
Linus Torvalds 已提交
530
	// Fix up srb for endian and force some values
531

L
Linus Torvalds 已提交
532
	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);	// Force this
533
	srbcmd->channel	 = cpu_to_le32(user_srbcmd->channel);
534
	srbcmd->id	 = cpu_to_le32(user_srbcmd->id);
535 536 537
	srbcmd->lun	 = cpu_to_le32(user_srbcmd->lun);
	srbcmd->timeout	 = cpu_to_le32(user_srbcmd->timeout);
	srbcmd->flags	 = cpu_to_le32(flags);
M
Mark Haverkamp 已提交
538
	srbcmd->retry_limit = 0; // Obsolete parameter
539
	srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
M
Mark Haverkamp 已提交
540
	memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
541

542
	switch (flags & (SRB_DataIn | SRB_DataOut)) {
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554
	case SRB_DataOut:
		data_dir = DMA_TO_DEVICE;
		break;
	case (SRB_DataIn | SRB_DataOut):
		data_dir = DMA_BIDIRECTIONAL;
		break;
	case SRB_DataIn:
		data_dir = DMA_FROM_DEVICE;
		break;
	default:
		data_dir = DMA_NONE;
	}
555
	if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
556 557 558 559 560
		dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
		  le32_to_cpu(srbcmd->sg.count)));
		rcode = -EINVAL;
		goto cleanup;
	}
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
		((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
	actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
	  (sizeof(struct sgentry64) - sizeof(struct sgentry));
	/* User made a mistake - should not continue */
	if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
		dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
		  "Raw SRB command calculated fibsize=%lu;%lu "
		  "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
		  "issued fibsize=%d\n",
		  actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
		  sizeof(struct aac_srb), sizeof(struct sgentry),
		  sizeof(struct sgentry64), fibsize));
		rcode = -EINVAL;
		goto cleanup;
	}
	if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
		dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
		rcode = -EINVAL;
		goto cleanup;
	}
	byte_count = 0;
	if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
584
		struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
M
Mark Haverkamp 已提交
585
		struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
L
Linus Torvalds 已提交
586 587 588 589

		/*
		 * This should also catch if user used the 32 bit sgmap
		 */
590 591 592 593 594
		if (actual_fibsize64 == fibsize) {
			actual_fibsize = actual_fibsize64;
			for (i = 0; i < upsg->count; i++) {
				u64 addr;
				void* p;
595
				if (upsg->sg[i].count >
596
				    ((dev->adapter_info.options &
597 598
				     AAC_OPT_NEW_COMM) ?
				      (dev->scsi_host_ptr->max_sectors << 9) :
599
				      65536)) {
600 601 602
					rcode = -EINVAL;
					goto cleanup;
				}
603 604
				/* Does this really need to be GFP_DMA? */
				p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
605
				if(!p) {
606 607 608 609 610 611 612
					dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
					  upsg->sg[i].count,i,upsg->count));
					rcode = -ENOMEM;
					goto cleanup;
				}
				addr = (u64)upsg->sg[i].addr[0];
				addr += ((u64)upsg->sg[i].addr[1]) << 32;
A
Al Viro 已提交
613
				sg_user[i] = (void __user *)(uintptr_t)addr;
614 615 616
				sg_list[i] = p; // save so we can clean up later
				sg_indx = i;

617
				if (flags & SRB_DataOut) {
618 619 620 621 622 623 624
					if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
						dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
						rcode = -EFAULT;
						goto cleanup;
					}
				}
				addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
L
Linus Torvalds 已提交
625

626 627 628 629 630 631 632 633 634 635 636
				psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
				psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
				byte_count += upsg->sg[i].count;
				psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
			}
		} else {
			struct user_sgmap* usg;
			usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
			  + sizeof(struct sgmap), GFP_KERNEL);
			if (!usg) {
				dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
L
Linus Torvalds 已提交
637 638 639
				rcode = -ENOMEM;
				goto cleanup;
			}
640 641 642 643 644 645 646
			memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
			  + sizeof(struct sgmap));
			actual_fibsize = actual_fibsize64;

			for (i = 0; i < usg->count; i++) {
				u64 addr;
				void* p;
647
				if (usg->sg[i].count >
648
				    ((dev->adapter_info.options &
649 650
				     AAC_OPT_NEW_COMM) ?
				      (dev->scsi_host_ptr->max_sectors << 9) :
651
				      65536)) {
652 653 654
					rcode = -EINVAL;
					goto cleanup;
				}
655 656
				/* Does this really need to be GFP_DMA? */
				p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
657
				if(!p) {
658
					dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
659
					  usg->sg[i].count,i,usg->count));
660
					kfree(usg);
661
					rcode = -ENOMEM;
L
Linus Torvalds 已提交
662 663
					goto cleanup;
				}
A
Al Viro 已提交
664
				sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
665 666 667
				sg_list[i] = p; // save so we can clean up later
				sg_indx = i;

668
				if (flags & SRB_DataOut) {
669 670 671 672 673 674 675 676
					if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
						kfree (usg);
						dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
						rcode = -EFAULT;
						goto cleanup;
					}
				}
				addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
L
Linus Torvalds 已提交
677

678 679 680 681 682 683
				psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
				psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
				byte_count += usg->sg[i].count;
				psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
			}
			kfree (usg);
L
Linus Torvalds 已提交
684 685
		}
		srbcmd->count = cpu_to_le32(byte_count);
686
		psg->count = cpu_to_le32(sg_indx+1);
687
		status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
L
Linus Torvalds 已提交
688
	} else {
689
		struct user_sgmap* upsg = &user_srbcmd->sg;
L
Linus Torvalds 已提交
690
		struct sgmap* psg = &srbcmd->sg;
691 692 693 694

		if (actual_fibsize64 == fibsize) {
			struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
			for (i = 0; i < upsg->count; i++) {
A
Al Viro 已提交
695
				uintptr_t addr;
696
				void* p;
697
				if (usg->sg[i].count >
698
				    ((dev->adapter_info.options &
699 700
				     AAC_OPT_NEW_COMM) ?
				      (dev->scsi_host_ptr->max_sectors << 9) :
701
				      65536)) {
702 703 704
					rcode = -EINVAL;
					goto cleanup;
				}
705 706
				/* Does this really need to be GFP_DMA? */
				p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
707
				if(!p) {
708 709 710
					dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
					  usg->sg[i].count,i,usg->count));
					rcode = -ENOMEM;
L
Linus Torvalds 已提交
711 712
					goto cleanup;
				}
713 714
				addr = (u64)usg->sg[i].addr[0];
				addr += ((u64)usg->sg[i].addr[1]) << 32;
A
Al Viro 已提交
715
				sg_user[i] = (void __user *)addr;
716 717 718
				sg_list[i] = p; // save so we can clean up later
				sg_indx = i;

719
				if (flags & SRB_DataOut) {
720 721 722 723 724 725 726 727 728 729 730
					if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
						dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
						rcode = -EFAULT;
						goto cleanup;
					}
				}
				addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);

				psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
				byte_count += usg->sg[i].count;
				psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
L
Linus Torvalds 已提交
731
			}
732 733 734 735
		} else {
			for (i = 0; i < upsg->count; i++) {
				dma_addr_t addr;
				void* p;
736
				if (upsg->sg[i].count >
737
				    ((dev->adapter_info.options &
738 739
				     AAC_OPT_NEW_COMM) ?
				      (dev->scsi_host_ptr->max_sectors << 9) :
740
				      65536)) {
741 742 743
					rcode = -EINVAL;
					goto cleanup;
				}
744
				p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
745
				if (!p) {
746 747 748 749 750
					dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
					  upsg->sg[i].count, i, upsg->count));
					rcode = -ENOMEM;
					goto cleanup;
				}
A
Al Viro 已提交
751
				sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
752 753 754
				sg_list[i] = p; // save so we can clean up later
				sg_indx = i;

755
				if (flags & SRB_DataOut) {
756 757 758 759 760 761 762 763 764
					if(copy_from_user(p, sg_user[i],
							upsg->sg[i].count)) {
						dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
						rcode = -EFAULT;
						goto cleanup;
					}
				}
				addr = pci_map_single(dev->pdev, p,
					upsg->sg[i].count, data_dir);
L
Linus Torvalds 已提交
765

766 767 768 769
				psg->sg[i].addr = cpu_to_le32(addr);
				byte_count += upsg->sg[i].count;
				psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
			}
L
Linus Torvalds 已提交
770 771
		}
		srbcmd->count = cpu_to_le32(byte_count);
772
		psg->count = cpu_to_le32(sg_indx+1);
773
		status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
L
Linus Torvalds 已提交
774
	}
775 776
	if (status == -ERESTARTSYS) {
		rcode = -ERESTARTSYS;
777 778
		goto cleanup;
	}
L
Linus Torvalds 已提交
779 780

	if (status != 0){
781
		dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
M
Mark Haverkamp 已提交
782
		rcode = -ENXIO;
L
Linus Torvalds 已提交
783 784 785
		goto cleanup;
	}

786
	if (flags & SRB_DataIn) {
L
Linus Torvalds 已提交
787
		for(i = 0 ; i <= sg_indx; i++){
788 789
			byte_count = le32_to_cpu(
			  (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
790 791 792
			      ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
			      : srbcmd->sg.sg[i].count);
			if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
793
				dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801 802
				rcode = -EFAULT;
				goto cleanup;

			}
		}
	}

	reply = (struct aac_srb_reply *) fib_data(srbfib);
	if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
803
		dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
L
Linus Torvalds 已提交
804 805 806 807 808
		rcode = -EFAULT;
		goto cleanup;
	}

cleanup:
809
	kfree(user_srbcmd);
L
Linus Torvalds 已提交
810 811 812
	for(i=0; i <= sg_indx; i++){
		kfree(sg_list[i]);
	}
813
	if (rcode != -ERESTARTSYS) {
814 815 816
		aac_fib_complete(srbfib);
		aac_fib_free(srbfib);
	}
L
Linus Torvalds 已提交
817 818 819 820 821

	return rcode;
}

struct aac_pci_info {
822 823
	u32 bus;
	u32 slot;
L
Linus Torvalds 已提交
824 825 826
};


827
static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
L
Linus Torvalds 已提交
828
{
829
	struct aac_pci_info pci_info;
L
Linus Torvalds 已提交
830 831 832 833

	pci_info.bus = dev->pdev->bus->number;
	pci_info.slot = PCI_SLOT(dev->pdev->devfn);

834 835 836
	if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
		dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
		return -EFAULT;
L
Linus Torvalds 已提交
837
	}
838
	return 0;
839
}
840

L
Linus Torvalds 已提交
841 842 843 844

int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
{
	int status;
845

L
Linus Torvalds 已提交
846 847 848
	/*
	 *	HBA gets first crack
	 */
849

L
Linus Torvalds 已提交
850
	status = aac_dev_ioctl(dev, cmd, arg);
851
	if (status != -ENOTTY)
L
Linus Torvalds 已提交
852 853 854 855 856 857
		return status;

	switch (cmd) {
	case FSACTL_MINIPORT_REV_CHECK:
		status = check_revision(dev, arg);
		break;
858
	case FSACTL_SEND_LARGE_FIB:
L
Linus Torvalds 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	case FSACTL_SENDFIB:
		status = ioctl_send_fib(dev, arg);
		break;
	case FSACTL_OPEN_GET_ADAPTER_FIB:
		status = open_getadapter_fib(dev, arg);
		break;
	case FSACTL_GET_NEXT_ADAPTER_FIB:
		status = next_getadapter_fib(dev, arg);
		break;
	case FSACTL_CLOSE_GET_ADAPTER_FIB:
		status = close_getadapter_fib(dev, arg);
		break;
	case FSACTL_SEND_RAW_SRB:
		status = aac_send_raw_srb(dev,arg);
		break;
	case FSACTL_GET_PCI_INFO:
		status = aac_get_pci_info(dev,arg);
		break;
	default:
		status = -ENOTTY;
879
		break;
L
Linus Torvalds 已提交
880 881 882 883
	}
	return status;
}