blob: fc268a410c27ee2777ca1f43446db08a3bfcda24 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070054# define AAC_DEBUG_PREAMBLE KERN_INFO
55# define AAC_DEBUG_POSTAMBLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
58{
59 struct hw_fib * kfib;
60 struct fib *fibptr;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070061 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 fibptr = fib_alloc(dev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070067 if(fibptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -ENOMEM;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
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 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 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070084 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 /* Highjack the hw_fib */
89 hw_fib = fibptr->hw_fib;
90 hw_fib_pa = fibptr->hw_fib_pa;
91 fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
92 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
93 memcpy(kfib, hw_fib, dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070096 if (copy_from_user(kfib, arg, size)) {
97 retval = -EFAULT;
98 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700101 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 aac_adapter_interrupt(dev);
103 /*
104 * Since we didn't really send a fib, zero out the state to allow
105 * cleanup code not to assert.
106 */
107 kfib->header.XferState = 0;
108 } else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700109 retval = fib_send(le16_to_cpu(kfib->header.Command), fibptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 le16_to_cpu(kfib->header.Size) , FsaNormal,
111 1, 1, NULL, NULL);
112 if (retval) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700113 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 if (fib_complete(fibptr) != 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700116 retval = -EINVAL;
117 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 }
120 /*
121 * Make sure that the size returned by the adapter (which includes
122 * the header) is less than or equal to the size of a fib, so we
123 * don't corrupt application data. Then copy that size to the user
124 * buffer. (Don't try to add the header information again, since it
125 * was already included by the adapter.)
126 */
127
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700128 retval = 0;
129 if (copy_to_user(arg, (void *)kfib, size))
130 retval = -EFAULT;
131cleanup:
132 if (hw_fib) {
133 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
134 fibptr->hw_fib_pa = hw_fib_pa;
135 fibptr->hw_fib = hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700138 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141/**
142 * open_getadapter_fib - Get the next fib
143 *
144 * This routine will get the next Fib, if available, from the AdapterFibContext
145 * passed in from the user.
146 */
147
148static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
149{
150 struct aac_fib_context * fibctx;
151 int status;
152
153 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
154 if (fibctx == NULL) {
155 status = -ENOMEM;
156 } else {
157 unsigned long flags;
158 struct list_head * entry;
159 struct aac_fib_context * context;
160
161 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
162 fibctx->size = sizeof(struct aac_fib_context);
163 /*
164 * Yes yes, I know this could be an index, but we have a
165 * better guarantee of uniqueness for the locked loop below.
166 * Without the aid of a persistent history, this also helps
167 * reduce the chance that the opaque context would be reused.
168 */
169 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
170 /*
171 * Initialize the mutex used to wait for the next AIF.
172 */
173 init_MUTEX_LOCKED(&fibctx->wait_sem);
174 fibctx->wait = 0;
175 /*
176 * Initialize the fibs and set the count of fibs on
177 * the list to 0.
178 */
179 fibctx->count = 0;
180 INIT_LIST_HEAD(&fibctx->fib_list);
181 fibctx->jiffies = jiffies/HZ;
182 /*
183 * Now add this context onto the adapter's
184 * AdapterFibContext list.
185 */
186 spin_lock_irqsave(&dev->fib_lock, flags);
187 /* Ensure that we have a unique identifier */
188 entry = dev->fib_list.next;
189 while (entry != &dev->fib_list) {
190 context = list_entry(entry, struct aac_fib_context, next);
191 if (context->unique == fibctx->unique) {
192 /* Not unique (32 bits) */
193 fibctx->unique++;
194 entry = dev->fib_list.next;
195 } else {
196 entry = entry->next;
197 }
198 }
199 list_add_tail(&fibctx->next, &dev->fib_list);
200 spin_unlock_irqrestore(&dev->fib_lock, flags);
201 if (copy_to_user(arg, &fibctx->unique,
202 sizeof(fibctx->unique))) {
203 status = -EFAULT;
204 } else {
205 status = 0;
206 }
207 }
208 return status;
209}
210
211/**
212 * next_getadapter_fib - get the next fib
213 * @dev: adapter to use
214 * @arg: ioctl argument
215 *
216 * This routine will get the next Fib, if available, from the AdapterFibContext
217 * passed in from the user.
218 */
219
220static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
221{
222 struct fib_ioctl f;
223 struct fib *fib;
224 struct aac_fib_context *fibctx;
225 int status;
226 struct list_head * entry;
227 unsigned long flags;
228
229 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
230 return -EFAULT;
231 /*
232 * Verify that the HANDLE passed in was a valid AdapterFibContext
233 *
234 * Search the list of AdapterFibContext addresses on the adapter
235 * to be sure this is a valid address
236 */
237 entry = dev->fib_list.next;
238 fibctx = NULL;
239
240 while (entry != &dev->fib_list) {
241 fibctx = list_entry(entry, struct aac_fib_context, next);
242 /*
243 * Extract the AdapterFibContext from the Input parameters.
244 */
245 if (fibctx->unique == f.fibctx) { /* We found a winner */
246 break;
247 }
248 entry = entry->next;
249 fibctx = NULL;
250 }
251 if (!fibctx) {
252 dprintk ((KERN_INFO "Fib Context not found\n"));
253 return -EINVAL;
254 }
255
256 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
257 (fibctx->size != sizeof(struct aac_fib_context))) {
258 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
259 return -EINVAL;
260 }
261 status = 0;
262 spin_lock_irqsave(&dev->fib_lock, flags);
263 /*
264 * If there are no fibs to send back, then either wait or return
265 * -EAGAIN
266 */
267return_fib:
268 if (!list_empty(&fibctx->fib_list)) {
269 struct list_head * entry;
270 /*
271 * Pull the next fib from the fibs
272 */
273 entry = fibctx->fib_list.next;
274 list_del(entry);
275
276 fib = list_entry(entry, struct fib, fiblink);
277 fibctx->count--;
278 spin_unlock_irqrestore(&dev->fib_lock, flags);
279 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
280 kfree(fib->hw_fib);
281 kfree(fib);
282 return -EFAULT;
283 }
284 /*
285 * Free the space occupied by this copy of the fib.
286 */
287 kfree(fib->hw_fib);
288 kfree(fib);
289 status = 0;
290 fibctx->jiffies = jiffies/HZ;
291 } else {
292 spin_unlock_irqrestore(&dev->fib_lock, flags);
293 if (f.wait) {
294 if(down_interruptible(&fibctx->wait_sem) < 0) {
295 status = -EINTR;
296 } else {
297 /* Lock again and retry */
298 spin_lock_irqsave(&dev->fib_lock, flags);
299 goto return_fib;
300 }
301 } else {
302 status = -EAGAIN;
303 }
304 }
305 return status;
306}
307
308int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
309{
310 struct fib *fib;
311
312 /*
313 * First free any FIBs that have not been consumed.
314 */
315 while (!list_empty(&fibctx->fib_list)) {
316 struct list_head * entry;
317 /*
318 * Pull the next fib from the fibs
319 */
320 entry = fibctx->fib_list.next;
321 list_del(entry);
322 fib = list_entry(entry, struct fib, fiblink);
323 fibctx->count--;
324 /*
325 * Free the space occupied by this copy of the fib.
326 */
327 kfree(fib->hw_fib);
328 kfree(fib);
329 }
330 /*
331 * Remove the Context from the AdapterFibContext List
332 */
333 list_del(&fibctx->next);
334 /*
335 * Invalidate context
336 */
337 fibctx->type = 0;
338 /*
339 * Free the space occupied by the Context
340 */
341 kfree(fibctx);
342 return 0;
343}
344
345/**
346 * close_getadapter_fib - close down user fib context
347 * @dev: adapter
348 * @arg: ioctl arguments
349 *
350 * This routine will close down the fibctx passed in from the user.
351 */
352
353static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
354{
355 struct aac_fib_context *fibctx;
356 int status;
357 unsigned long flags;
358 struct list_head * entry;
359
360 /*
361 * Verify that the HANDLE passed in was a valid AdapterFibContext
362 *
363 * Search the list of AdapterFibContext addresses on the adapter
364 * to be sure this is a valid address
365 */
366
367 entry = dev->fib_list.next;
368 fibctx = NULL;
369
370 while(entry != &dev->fib_list) {
371 fibctx = list_entry(entry, struct aac_fib_context, next);
372 /*
373 * Extract the fibctx from the input parameters
374 */
375 if (fibctx->unique == (u32)(unsigned long)arg) {
376 /* We found a winner */
377 break;
378 }
379 entry = entry->next;
380 fibctx = NULL;
381 }
382
383 if (!fibctx)
384 return 0; /* Already gone */
385
386 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
387 (fibctx->size != sizeof(struct aac_fib_context)))
388 return -EINVAL;
389 spin_lock_irqsave(&dev->fib_lock, flags);
390 status = aac_close_fib_context(dev, fibctx);
391 spin_unlock_irqrestore(&dev->fib_lock, flags);
392 return status;
393}
394
395/**
396 * check_revision - close down user fib context
397 * @dev: adapter
398 * @arg: ioctl arguments
399 *
400 * This routine returns the driver version.
401 * Under Linux, there have been no version incompatibilities, so this is
402 * simple!
403 */
404
405static int check_revision(struct aac_dev *dev, void __user *arg)
406{
407 struct revision response;
408
409 response.compat = 1;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700410 response.version = le32_to_cpu(dev->adapter_info.kernelrev);
411 response.build = le32_to_cpu(dev->adapter_info.kernelbuild);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (copy_to_user(arg, &response, sizeof(response)))
414 return -EFAULT;
415 return 0;
416}
417
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
420 *
421 * aac_send_raw_scb
422 *
423 */
424
Adrian Bunk 48338692005-04-25 19:45:58 -0700425static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct fib* srbfib;
428 int status;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700429 struct aac_srb *srbcmd = NULL;
430 struct user_aac_srb *user_srbcmd = NULL;
431 struct user_aac_srb __user *user_srb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct aac_srb_reply __user *user_reply;
433 struct aac_srb_reply* reply;
434 u32 fibsize = 0;
435 u32 flags = 0;
436 s32 rcode = 0;
437 u32 data_dir;
438 void __user *sg_user[32];
439 void *sg_list[32];
440 u32 sg_indx = 0;
441 u32 byte_count = 0;
442 u32 actual_fibsize = 0;
443 int i;
444
445
446 if (!capable(CAP_SYS_ADMIN)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700447 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -EPERM;
449 }
450 /*
451 * Allocate and initialize a Fib then setup a BlockWrite command
452 */
453 if (!(srbfib = fib_alloc(dev))) {
454 return -1;
455 }
456 fib_init(srbfib);
457
458 srbcmd = (struct aac_srb*) fib_data(srbfib);
459
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700460 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700462 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 rcode = -EFAULT;
464 goto cleanup;
465 }
466
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700467 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 rcode = -EINVAL;
469 goto cleanup;
470 }
471
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700472 user_srbcmd = kmalloc(GFP_KERNEL, fibsize);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700473 if (!user_srbcmd) {
474 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
475 rcode = -ENOMEM;
476 goto cleanup;
477 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700478 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700479 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 rcode = -EFAULT;
481 goto cleanup;
482 }
483
484 user_reply = arg+fibsize;
485
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700486 flags = user_srbcmd->flags; /* from user in cpu order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 // Fix up srb for endian and force some values
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700490 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
491 srbcmd->id = cpu_to_le32(user_srbcmd->id);
492 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
493 srbcmd->flags = cpu_to_le32(flags);
494 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
495 srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700496 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700498 switch (flags & (SRB_DataIn | SRB_DataOut)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case SRB_DataOut:
500 data_dir = DMA_TO_DEVICE;
501 break;
502 case (SRB_DataIn | SRB_DataOut):
503 data_dir = DMA_BIDIRECTIONAL;
504 break;
505 case SRB_DataIn:
506 data_dir = DMA_FROM_DEVICE;
507 break;
508 default:
509 data_dir = DMA_NONE;
510 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700511 if (le32_to_cpu(srbcmd->sg.count) > (sizeof(sg_list)/sizeof(sg_list[0]))) {
512 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
513 le32_to_cpu(srbcmd->sg.count)));
514 rcode = -EINVAL;
515 goto cleanup;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (dev->dac_support == 1) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700518 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
519 struct sgmap64* psg = (struct sgmap64*)&user_srbcmd->sg;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700520 struct user_sgmap* usg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 byte_count = 0;
522
523 /*
524 * This should also catch if user used the 32 bit sgmap
525 */
526 actual_fibsize = sizeof(struct aac_srb) -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700527 sizeof(struct sgentry) +
528 ((upsg->count & 0xff) *
529 sizeof(struct sgentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700531 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 rcode = -EINVAL;
533 goto cleanup;
534 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700535 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
536 + sizeof(struct sgmap), GFP_KERNEL);
537 if (!usg) {
538 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
539 rcode = -ENOMEM;
540 goto cleanup;
541 }
542 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
543 + sizeof(struct sgmap));
544 actual_fibsize = sizeof(struct aac_srb) -
545 sizeof(struct sgentry) + ((usg->count & 0xff) *
546 sizeof(struct sgentry64));
547 if ((data_dir == DMA_NONE) && upsg->count) {
548 kfree (usg);
549 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 rcode = -EINVAL;
551 goto cleanup;
552 }
553
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700554 for (i = 0; i < usg->count; i++) {
555 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 void* p;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700557 /* Does this really need to be GFP_DMA? */
558 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if(p == 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700560 kfree (usg);
561 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
562 usg->sg[i].count,i,usg->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 rcode = -ENOMEM;
564 goto cleanup;
565 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700566 sg_user[i] = (void __user *)usg->sg[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 sg_list[i] = p; // save so we can clean up later
568 sg_indx = i;
569
570 if( flags & SRB_DataOut ){
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700571 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700572 kfree (usg);
573 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rcode = -EFAULT;
575 goto cleanup;
576 }
577 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700578 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700580 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700581 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
582 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
583 byte_count += usg->sg[i].count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700585 kfree (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700588 psg->count = cpu_to_le32(sg_indx+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
590 } else {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700591 struct user_sgmap* upsg = &user_srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct sgmap* psg = &srbcmd->sg;
593 byte_count = 0;
594
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700595 actual_fibsize = sizeof (struct aac_srb) + (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) * sizeof (struct sgentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700597 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 rcode = -EINVAL;
599 goto cleanup;
600 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700601 if ((data_dir == DMA_NONE) && upsg->count) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700602 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 rcode = -EINVAL;
604 goto cleanup;
605 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700606 for (i = 0; i < upsg->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 dma_addr_t addr;
608 void* p;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700609 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if(p == 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700611 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
612 upsg->sg[i].count, i, upsg->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rcode = -ENOMEM;
614 goto cleanup;
615 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700616 sg_user[i] = (void __user *)upsg->sg[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 sg_list[i] = p; // save so we can clean up later
618 sg_indx = i;
619
620 if( flags & SRB_DataOut ){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700621 if(copy_from_user(p, sg_user[i],
622 upsg->sg[i].count)) {
623 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 rcode = -EFAULT;
625 goto cleanup;
626 }
627 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700628 addr = pci_map_single(dev->pdev, p,
629 upsg->sg[i].count, data_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 psg->sg[i].addr = cpu_to_le32(addr);
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700632 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
633 byte_count += upsg->sg[i].count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700636 psg->count = cpu_to_le32(sg_indx+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
638 }
639
640 if (status != 0){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700641 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 rcode = -1;
643 goto cleanup;
644 }
645
646 if( flags & SRB_DataIn ) {
647 for(i = 0 ; i <= sg_indx; i++){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700648 byte_count = le32_to_cpu((dev->dac_support == 1)
649 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
650 : srbcmd->sg.sg[i].count);
651 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
652 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 rcode = -EFAULT;
654 goto cleanup;
655
656 }
657 }
658 }
659
660 reply = (struct aac_srb_reply *) fib_data(srbfib);
661 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700662 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 rcode = -EFAULT;
664 goto cleanup;
665 }
666
667cleanup:
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700668 kfree(user_srbcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 for(i=0; i <= sg_indx; i++){
670 kfree(sg_list[i]);
671 }
672 fib_complete(srbfib);
673 fib_free(srbfib);
674
675 return rcode;
676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678struct aac_pci_info {
679 u32 bus;
680 u32 slot;
681};
682
683
Adrian Bunk 48338692005-04-25 19:45:58 -0700684static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct aac_pci_info pci_info;
687
688 pci_info.bus = dev->pdev->bus->number;
689 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
690
691 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700692 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EFAULT;
694 }
695 return 0;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700696}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698
699int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
700{
701 int status;
702
703 /*
704 * HBA gets first crack
705 */
706
707 status = aac_dev_ioctl(dev, cmd, arg);
708 if(status != -ENOTTY)
709 return status;
710
711 switch (cmd) {
712 case FSACTL_MINIPORT_REV_CHECK:
713 status = check_revision(dev, arg);
714 break;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700715 case FSACTL_SEND_LARGE_FIB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case FSACTL_SENDFIB:
717 status = ioctl_send_fib(dev, arg);
718 break;
719 case FSACTL_OPEN_GET_ADAPTER_FIB:
720 status = open_getadapter_fib(dev, arg);
721 break;
722 case FSACTL_GET_NEXT_ADAPTER_FIB:
723 status = next_getadapter_fib(dev, arg);
724 break;
725 case FSACTL_CLOSE_GET_ADAPTER_FIB:
726 status = close_getadapter_fib(dev, arg);
727 break;
728 case FSACTL_SEND_RAW_SRB:
729 status = aac_send_raw_srb(dev,arg);
730 break;
731 case FSACTL_GET_PCI_INFO:
732 status = aac_get_pci_info(dev,arg);
733 break;
734 default:
735 status = -ENOTTY;
736 break;
737 }
738 return status;
739}
740