1 /*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc.
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
8 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Module Name:
26 * 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>
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 <linux/delay.h> /* ssleep prototype */
42 #include <linux/kthread.h>
43 #include <linux/semaphore.h>
44 #include <asm/uaccess.h>
45 #include <scsi/scsi_host.h>
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
53 *
54 * This routine sends a fib to the adapter on behalf of a user level
55 * program.
56 */
57 # define AAC_DEBUG_PREAMBLE KERN_INFO
58 # define AAC_DEBUG_POSTAMBLE
59
ioctl_send_fib(struct aac_dev * dev,void __user * arg)60 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
61 {
62 struct hw_fib * kfib;
63 struct fib *fibptr;
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;
68
69 if (dev->in_reset) {
70 return -EBUSY;
71 }
72 fibptr = aac_fib_alloc(dev);
73 if(fibptr == NULL) {
74 return -ENOMEM;
75 }
76
77 kfib = fibptr->hw_fib_va;
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))) {
82 aac_fib_free(fibptr);
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 */
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) {
94 dma_addr_t daddr;
95
96 if (size > 2048) {
97 retval = -EINVAL;
98 goto cleanup;
99 }
100
101 kfib = pci_alloc_consistent(dev->pdev, size, &daddr);
102 if (!kfib) {
103 retval = -ENOMEM;
104 goto cleanup;
105 }
106
107 /* Highjack the hw_fib */
108 hw_fib = fibptr->hw_fib_va;
109 hw_fib_pa = fibptr->hw_fib_pa;
110 fibptr->hw_fib_va = kfib;
111 fibptr->hw_fib_pa = daddr;
112 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
113 memcpy(kfib, hw_fib, dev->max_fib_size);
114 }
115
116 if (copy_from_user(kfib, arg, size)) {
117 retval = -EFAULT;
118 goto cleanup;
119 }
120
121 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
122 aac_adapter_interrupt(dev);
123 /*
124 * Since we didn't really send a fib, zero out the state to allow
125 * cleanup code not to assert.
126 */
127 kfib->header.XferState = 0;
128 } else {
129 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
130 le16_to_cpu(kfib->header.Size) , FsaNormal,
131 1, 1, NULL, NULL);
132 if (retval) {
133 goto cleanup;
134 }
135 if (aac_fib_complete(fibptr) != 0) {
136 retval = -EINVAL;
137 goto cleanup;
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
148 retval = 0;
149 if (copy_to_user(arg, (void *)kfib, size))
150 retval = -EFAULT;
151 cleanup:
152 if (hw_fib) {
153 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
154 fibptr->hw_fib_pa = hw_fib_pa;
155 fibptr->hw_fib_va = hw_fib;
156 }
157 if (retval != -ERESTARTSYS)
158 aac_fib_free(fibptr);
159 return retval;
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
open_getadapter_fib(struct aac_dev * dev,void __user * arg)169 static 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);
184 /*
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 */
194 sema_init(&fibctx->wait_sem, 0);
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 /*
204 * Now add this context onto the adapter's
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);
222 if (copy_to_user(arg, &fibctx->unique,
223 sizeof(fibctx->unique))) {
224 status = -EFAULT;
225 } else {
226 status = 0;
227 }
228 }
229 return status;
230 }
231
232 /**
233 * next_getadapter_fib - get the next fib
234 * @dev: adapter to use
235 * @arg: ioctl argument
236 *
237 * This routine will get the next Fib, if available, from the AdapterFibContext
238 * passed in from the user.
239 */
240
next_getadapter_fib(struct aac_dev * dev,void __user * arg)241 static 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;
249
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 */
258 spin_lock_irqsave(&dev->fib_lock, flags);
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 */
267 if (fibctx->unique == f.fibctx) { /* We found a winner */
268 break;
269 }
270 entry = entry->next;
271 fibctx = NULL;
272 }
273 if (!fibctx) {
274 spin_unlock_irqrestore(&dev->fib_lock, flags);
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))) {
281 spin_unlock_irqrestore(&dev->fib_lock, flags);
282 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
283 return -EINVAL;
284 }
285 status = 0;
286 /*
287 * If there are no fibs to send back, then either wait or return
288 * -EAGAIN
289 */
290 return_fib:
291 if (!list_empty(&fibctx->fib_list)) {
292 /*
293 * Pull the next fib from the fibs
294 */
295 entry = fibctx->fib_list.next;
296 list_del(entry);
297
298 fib = list_entry(entry, struct fib, fiblink);
299 fibctx->count--;
300 spin_unlock_irqrestore(&dev->fib_lock, flags);
301 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
302 kfree(fib->hw_fib_va);
303 kfree(fib);
304 return -EFAULT;
305 }
306 /*
307 * Free the space occupied by this copy of the fib.
308 */
309 kfree(fib->hw_fib_va);
310 kfree(fib);
311 status = 0;
312 } else {
313 spin_unlock_irqrestore(&dev->fib_lock, flags);
314 /* If someone killed the AIF aacraid thread, restart it */
315 status = !dev->aif_thread;
316 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
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 }
324 if (f.wait) {
325 if(down_interruptible(&fibctx->wait_sem) < 0) {
326 status = -ERESTARTSYS;
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;
334 }
335 }
336 fibctx->jiffies = jiffies/HZ;
337 return status;
338 }
339
aac_close_fib_context(struct aac_dev * dev,struct aac_fib_context * fibctx)340 int 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 */
359 kfree(fib->hw_fib_va);
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 */
384
close_getadapter_fib(struct aac_dev * dev,void __user * arg)385 static 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 */
407 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
408 break;
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.
431 * Under Linux, there have been no version incompatibilities, so this is
432 * simple!
433 */
434
check_revision(struct aac_dev * dev,void __user * arg)435 static int check_revision(struct aac_dev *dev, void __user *arg)
436 {
437 struct revision response;
438 char *driver_version = aac_driver_version;
439 u32 version;
440
441 response.compat = 1;
442 version = (simple_strtol(driver_version,
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);
447 # ifdef AAC_DRIVER_BUILD
448 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
449 # else
450 response.build = cpu_to_le32(9999);
451 # endif
452
453 if (copy_to_user(arg, &response, sizeof(response)))
454 return -EFAULT;
455 return 0;
456 }
457
458
459 /**
460 *
461 * aac_send_raw_scb
462 *
463 */
464
aac_send_raw_srb(struct aac_dev * dev,void __user * arg)465 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
466 {
467 struct fib* srbfib;
468 int status;
469 struct aac_srb *srbcmd = NULL;
470 struct user_aac_srb *user_srbcmd = NULL;
471 struct user_aac_srb __user *user_srb = arg;
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];
480 u32 sg_indx = 0;
481 u32 byte_count = 0;
482 u32 actual_fibsize64, actual_fibsize = 0;
483 int i;
484
485
486 if (dev->in_reset) {
487 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
488 return -EBUSY;
489 }
490 if (!capable(CAP_SYS_ADMIN)){
491 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
492 return -EPERM;
493 }
494 /*
495 * Allocate and initialize a Fib then setup a SRB command
496 */
497 if (!(srbfib = aac_fib_alloc(dev))) {
498 return -ENOMEM;
499 }
500 aac_fib_init(srbfib);
501
502 srbcmd = (struct aac_srb*) fib_data(srbfib);
503
504 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
505 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
506 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
507 rcode = -EFAULT;
508 goto cleanup;
509 }
510
511 if ((fibsize < (sizeof(struct user_aac_srb) - sizeof(struct user_sgentry))) ||
512 (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))) {
513 rcode = -EINVAL;
514 goto cleanup;
515 }
516
517 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
518 if (!user_srbcmd) {
519 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
520 rcode = -ENOMEM;
521 goto cleanup;
522 }
523 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
524 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
525 rcode = -EFAULT;
526 goto cleanup;
527 }
528
529 user_reply = arg+fibsize;
530
531 flags = user_srbcmd->flags; /* from user in cpu order */
532 // Fix up srb for endian and force some values
533
534 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
535 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
536 srbcmd->id = cpu_to_le32(user_srbcmd->id);
537 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
538 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
539 srbcmd->flags = cpu_to_le32(flags);
540 srbcmd->retry_limit = 0; // Obsolete parameter
541 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
542 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
543
544 switch (flags & (SRB_DataIn | SRB_DataOut)) {
545 case SRB_DataOut:
546 data_dir = DMA_TO_DEVICE;
547 break;
548 case (SRB_DataIn | SRB_DataOut):
549 data_dir = DMA_BIDIRECTIONAL;
550 break;
551 case SRB_DataIn:
552 data_dir = DMA_FROM_DEVICE;
553 break;
554 default:
555 data_dir = DMA_NONE;
556 }
557 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
558 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
559 le32_to_cpu(srbcmd->sg.count)));
560 rcode = -EINVAL;
561 goto cleanup;
562 }
563 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
564 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
565 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
566 (sizeof(struct sgentry64) - sizeof(struct sgentry));
567 /* User made a mistake - should not continue */
568 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
569 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
570 "Raw SRB command calculated fibsize=%lu;%lu "
571 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
572 "issued fibsize=%d\n",
573 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
574 sizeof(struct aac_srb), sizeof(struct sgentry),
575 sizeof(struct sgentry64), fibsize));
576 rcode = -EINVAL;
577 goto cleanup;
578 }
579 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
580 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
581 rcode = -EINVAL;
582 goto cleanup;
583 }
584 byte_count = 0;
585 if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
586 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
587 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
588
589 /*
590 * This should also catch if user used the 32 bit sgmap
591 */
592 if (actual_fibsize64 == fibsize) {
593 actual_fibsize = actual_fibsize64;
594 for (i = 0; i < upsg->count; i++) {
595 u64 addr;
596 void* p;
597 if (upsg->sg[i].count >
598 ((dev->adapter_info.options &
599 AAC_OPT_NEW_COMM) ?
600 (dev->scsi_host_ptr->max_sectors << 9) :
601 65536)) {
602 rcode = -EINVAL;
603 goto cleanup;
604 }
605 /* Does this really need to be GFP_DMA? */
606 p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
607 if(!p) {
608 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
609 upsg->sg[i].count,i,upsg->count));
610 rcode = -ENOMEM;
611 goto cleanup;
612 }
613 addr = (u64)upsg->sg[i].addr[0];
614 addr += ((u64)upsg->sg[i].addr[1]) << 32;
615 sg_user[i] = (void __user *)(uintptr_t)addr;
616 sg_list[i] = p; // save so we can clean up later
617 sg_indx = i;
618
619 if (flags & SRB_DataOut) {
620 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
621 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
622 rcode = -EFAULT;
623 goto cleanup;
624 }
625 }
626 addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
627
628 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
629 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
630 byte_count += upsg->sg[i].count;
631 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
632 }
633 } else {
634 struct user_sgmap* usg;
635 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
636 + sizeof(struct sgmap), GFP_KERNEL);
637 if (!usg) {
638 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
639 rcode = -ENOMEM;
640 goto cleanup;
641 }
642 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
643 + sizeof(struct sgmap));
644 actual_fibsize = actual_fibsize64;
645
646 for (i = 0; i < usg->count; i++) {
647 u64 addr;
648 void* p;
649 if (usg->sg[i].count >
650 ((dev->adapter_info.options &
651 AAC_OPT_NEW_COMM) ?
652 (dev->scsi_host_ptr->max_sectors << 9) :
653 65536)) {
654 kfree(usg);
655 rcode = -EINVAL;
656 goto cleanup;
657 }
658 /* Does this really need to be GFP_DMA? */
659 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
660 if(!p) {
661 dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
662 usg->sg[i].count,i,usg->count));
663 kfree(usg);
664 rcode = -ENOMEM;
665 goto cleanup;
666 }
667 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
668 sg_list[i] = p; // save so we can clean up later
669 sg_indx = i;
670
671 if (flags & SRB_DataOut) {
672 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
673 kfree (usg);
674 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
675 rcode = -EFAULT;
676 goto cleanup;
677 }
678 }
679 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
680
681 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
682 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
683 byte_count += usg->sg[i].count;
684 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
685 }
686 kfree (usg);
687 }
688 srbcmd->count = cpu_to_le32(byte_count);
689 psg->count = cpu_to_le32(sg_indx+1);
690 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
691 } else {
692 struct user_sgmap* upsg = &user_srbcmd->sg;
693 struct sgmap* psg = &srbcmd->sg;
694
695 if (actual_fibsize64 == fibsize) {
696 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
697 for (i = 0; i < upsg->count; i++) {
698 uintptr_t addr;
699 void* p;
700 if (usg->sg[i].count >
701 ((dev->adapter_info.options &
702 AAC_OPT_NEW_COMM) ?
703 (dev->scsi_host_ptr->max_sectors << 9) :
704 65536)) {
705 rcode = -EINVAL;
706 goto cleanup;
707 }
708 /* Does this really need to be GFP_DMA? */
709 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
710 if(!p) {
711 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
712 usg->sg[i].count,i,usg->count));
713 rcode = -ENOMEM;
714 goto cleanup;
715 }
716 addr = (u64)usg->sg[i].addr[0];
717 addr += ((u64)usg->sg[i].addr[1]) << 32;
718 sg_user[i] = (void __user *)addr;
719 sg_list[i] = p; // save so we can clean up later
720 sg_indx = i;
721
722 if (flags & SRB_DataOut) {
723 if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
724 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
725 rcode = -EFAULT;
726 goto cleanup;
727 }
728 }
729 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
730
731 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
732 byte_count += usg->sg[i].count;
733 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
734 }
735 } else {
736 for (i = 0; i < upsg->count; i++) {
737 dma_addr_t addr;
738 void* p;
739 if (upsg->sg[i].count >
740 ((dev->adapter_info.options &
741 AAC_OPT_NEW_COMM) ?
742 (dev->scsi_host_ptr->max_sectors << 9) :
743 65536)) {
744 rcode = -EINVAL;
745 goto cleanup;
746 }
747 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
748 if (!p) {
749 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
750 upsg->sg[i].count, i, upsg->count));
751 rcode = -ENOMEM;
752 goto cleanup;
753 }
754 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
755 sg_list[i] = p; // save so we can clean up later
756 sg_indx = i;
757
758 if (flags & SRB_DataOut) {
759 if(copy_from_user(p, sg_user[i],
760 upsg->sg[i].count)) {
761 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
762 rcode = -EFAULT;
763 goto cleanup;
764 }
765 }
766 addr = pci_map_single(dev->pdev, p,
767 upsg->sg[i].count, data_dir);
768
769 psg->sg[i].addr = cpu_to_le32(addr);
770 byte_count += upsg->sg[i].count;
771 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
772 }
773 }
774 srbcmd->count = cpu_to_le32(byte_count);
775 psg->count = cpu_to_le32(sg_indx+1);
776 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
777 }
778 if (status == -ERESTARTSYS) {
779 rcode = -ERESTARTSYS;
780 goto cleanup;
781 }
782
783 if (status != 0){
784 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
785 rcode = -ENXIO;
786 goto cleanup;
787 }
788
789 if (flags & SRB_DataIn) {
790 for(i = 0 ; i <= sg_indx; i++){
791 byte_count = le32_to_cpu(
792 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
793 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
794 : srbcmd->sg.sg[i].count);
795 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
796 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
797 rcode = -EFAULT;
798 goto cleanup;
799
800 }
801 }
802 }
803
804 reply = (struct aac_srb_reply *) fib_data(srbfib);
805 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
806 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
807 rcode = -EFAULT;
808 goto cleanup;
809 }
810
811 cleanup:
812 kfree(user_srbcmd);
813 for(i=0; i <= sg_indx; i++){
814 kfree(sg_list[i]);
815 }
816 if (rcode != -ERESTARTSYS) {
817 aac_fib_complete(srbfib);
818 aac_fib_free(srbfib);
819 }
820
821 return rcode;
822 }
823
824 struct aac_pci_info {
825 u32 bus;
826 u32 slot;
827 };
828
829
aac_get_pci_info(struct aac_dev * dev,void __user * arg)830 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
831 {
832 struct aac_pci_info pci_info;
833
834 pci_info.bus = dev->pdev->bus->number;
835 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
836
837 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
838 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
839 return -EFAULT;
840 }
841 return 0;
842 }
843
844
aac_do_ioctl(struct aac_dev * dev,int cmd,void __user * arg)845 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
846 {
847 int status;
848
849 /*
850 * HBA gets first crack
851 */
852
853 status = aac_dev_ioctl(dev, cmd, arg);
854 if (status != -ENOTTY)
855 return status;
856
857 switch (cmd) {
858 case FSACTL_MINIPORT_REV_CHECK:
859 status = check_revision(dev, arg);
860 break;
861 case FSACTL_SEND_LARGE_FIB:
862 case FSACTL_SENDFIB:
863 status = ioctl_send_fib(dev, arg);
864 break;
865 case FSACTL_OPEN_GET_ADAPTER_FIB:
866 status = open_getadapter_fib(dev, arg);
867 break;
868 case FSACTL_GET_NEXT_ADAPTER_FIB:
869 status = next_getadapter_fib(dev, arg);
870 break;
871 case FSACTL_CLOSE_GET_ADAPTER_FIB:
872 status = close_getadapter_fib(dev, arg);
873 break;
874 case FSACTL_SEND_RAW_SRB:
875 status = aac_send_raw_srb(dev,arg);
876 break;
877 case FSACTL_GET_PCI_INFO:
878 status = aac_get_pci_info(dev,arg);
879 break;
880 default:
881 status = -ENOTTY;
882 break;
883 }
884 return status;
885 }
886
887