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