blob: 47fefca72695d02f3959b7a4e5c846a9c611c9bf [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
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080066 fibptr = aac_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))) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080076 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 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) {
Mark Haverkamp6e289a92006-01-11 09:28:07 -080088 if (size > 2048) {
89 retval = -EINVAL;
90 goto cleanup;
91 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070092 /* Highjack the hw_fib */
93 hw_fib = fibptr->hw_fib;
94 hw_fib_pa = fibptr->hw_fib_pa;
95 fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
96 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
97 memcpy(kfib, hw_fib, dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700100 if (copy_from_user(kfib, arg, size)) {
101 retval = -EFAULT;
102 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700105 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 aac_adapter_interrupt(dev);
107 /*
108 * Since we didn't really send a fib, zero out the state to allow
109 * cleanup code not to assert.
110 */
111 kfib->header.XferState = 0;
112 } else {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800113 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 le16_to_cpu(kfib->header.Size) , FsaNormal,
115 1, 1, NULL, NULL);
116 if (retval) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700117 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800119 if (aac_fib_complete(fibptr) != 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700120 retval = -EINVAL;
121 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
124 /*
125 * Make sure that the size returned by the adapter (which includes
126 * the header) is less than or equal to the size of a fib, so we
127 * don't corrupt application data. Then copy that size to the user
128 * buffer. (Don't try to add the header information again, since it
129 * was already included by the adapter.)
130 */
131
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700132 retval = 0;
133 if (copy_to_user(arg, (void *)kfib, size))
134 retval = -EFAULT;
135cleanup:
136 if (hw_fib) {
137 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
138 fibptr->hw_fib_pa = hw_fib_pa;
139 fibptr->hw_fib = hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800141 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700142 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/**
146 * open_getadapter_fib - Get the next fib
147 *
148 * This routine will get the next Fib, if available, from the AdapterFibContext
149 * passed in from the user.
150 */
151
152static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
153{
154 struct aac_fib_context * fibctx;
155 int status;
156
157 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
158 if (fibctx == NULL) {
159 status = -ENOMEM;
160 } else {
161 unsigned long flags;
162 struct list_head * entry;
163 struct aac_fib_context * context;
164
165 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
166 fibctx->size = sizeof(struct aac_fib_context);
167 /*
168 * Yes yes, I know this could be an index, but we have a
169 * better guarantee of uniqueness for the locked loop below.
170 * Without the aid of a persistent history, this also helps
171 * reduce the chance that the opaque context would be reused.
172 */
173 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
174 /*
175 * Initialize the mutex used to wait for the next AIF.
176 */
177 init_MUTEX_LOCKED(&fibctx->wait_sem);
178 fibctx->wait = 0;
179 /*
180 * Initialize the fibs and set the count of fibs on
181 * the list to 0.
182 */
183 fibctx->count = 0;
184 INIT_LIST_HEAD(&fibctx->fib_list);
185 fibctx->jiffies = jiffies/HZ;
186 /*
187 * Now add this context onto the adapter's
188 * AdapterFibContext list.
189 */
190 spin_lock_irqsave(&dev->fib_lock, flags);
191 /* Ensure that we have a unique identifier */
192 entry = dev->fib_list.next;
193 while (entry != &dev->fib_list) {
194 context = list_entry(entry, struct aac_fib_context, next);
195 if (context->unique == fibctx->unique) {
196 /* Not unique (32 bits) */
197 fibctx->unique++;
198 entry = dev->fib_list.next;
199 } else {
200 entry = entry->next;
201 }
202 }
203 list_add_tail(&fibctx->next, &dev->fib_list);
204 spin_unlock_irqrestore(&dev->fib_lock, flags);
205 if (copy_to_user(arg, &fibctx->unique,
206 sizeof(fibctx->unique))) {
207 status = -EFAULT;
208 } else {
209 status = 0;
210 }
211 }
212 return status;
213}
214
215/**
216 * next_getadapter_fib - get the next fib
217 * @dev: adapter to use
218 * @arg: ioctl argument
219 *
220 * This routine will get the next Fib, if available, from the AdapterFibContext
221 * passed in from the user.
222 */
223
224static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
225{
226 struct fib_ioctl f;
227 struct fib *fib;
228 struct aac_fib_context *fibctx;
229 int status;
230 struct list_head * entry;
231 unsigned long flags;
232
233 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
234 return -EFAULT;
235 /*
236 * Verify that the HANDLE passed in was a valid AdapterFibContext
237 *
238 * Search the list of AdapterFibContext addresses on the adapter
239 * to be sure this is a valid address
240 */
241 entry = dev->fib_list.next;
242 fibctx = NULL;
243
244 while (entry != &dev->fib_list) {
245 fibctx = list_entry(entry, struct aac_fib_context, next);
246 /*
247 * Extract the AdapterFibContext from the Input parameters.
248 */
249 if (fibctx->unique == f.fibctx) { /* We found a winner */
250 break;
251 }
252 entry = entry->next;
253 fibctx = NULL;
254 }
255 if (!fibctx) {
256 dprintk ((KERN_INFO "Fib Context not found\n"));
257 return -EINVAL;
258 }
259
260 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
261 (fibctx->size != sizeof(struct aac_fib_context))) {
262 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
263 return -EINVAL;
264 }
265 status = 0;
266 spin_lock_irqsave(&dev->fib_lock, flags);
267 /*
268 * If there are no fibs to send back, then either wait or return
269 * -EAGAIN
270 */
271return_fib:
272 if (!list_empty(&fibctx->fib_list)) {
273 struct list_head * entry;
274 /*
275 * Pull the next fib from the fibs
276 */
277 entry = fibctx->fib_list.next;
278 list_del(entry);
279
280 fib = list_entry(entry, struct fib, fiblink);
281 fibctx->count--;
282 spin_unlock_irqrestore(&dev->fib_lock, flags);
283 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
284 kfree(fib->hw_fib);
285 kfree(fib);
286 return -EFAULT;
287 }
288 /*
289 * Free the space occupied by this copy of the fib.
290 */
291 kfree(fib->hw_fib);
292 kfree(fib);
293 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 } else {
295 spin_unlock_irqrestore(&dev->fib_lock, flags);
296 if (f.wait) {
297 if(down_interruptible(&fibctx->wait_sem) < 0) {
298 status = -EINTR;
299 } else {
300 /* Lock again and retry */
301 spin_lock_irqsave(&dev->fib_lock, flags);
302 goto return_fib;
303 }
304 } else {
305 status = -EAGAIN;
306 }
307 }
Mark Haverkamp12a26d02005-08-03 15:39:25 -0700308 fibctx->jiffies = jiffies/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return status;
310}
311
312int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
313{
314 struct fib *fib;
315
316 /*
317 * First free any FIBs that have not been consumed.
318 */
319 while (!list_empty(&fibctx->fib_list)) {
320 struct list_head * entry;
321 /*
322 * Pull the next fib from the fibs
323 */
324 entry = fibctx->fib_list.next;
325 list_del(entry);
326 fib = list_entry(entry, struct fib, fiblink);
327 fibctx->count--;
328 /*
329 * Free the space occupied by this copy of the fib.
330 */
331 kfree(fib->hw_fib);
332 kfree(fib);
333 }
334 /*
335 * Remove the Context from the AdapterFibContext List
336 */
337 list_del(&fibctx->next);
338 /*
339 * Invalidate context
340 */
341 fibctx->type = 0;
342 /*
343 * Free the space occupied by the Context
344 */
345 kfree(fibctx);
346 return 0;
347}
348
349/**
350 * close_getadapter_fib - close down user fib context
351 * @dev: adapter
352 * @arg: ioctl arguments
353 *
354 * This routine will close down the fibctx passed in from the user.
355 */
356
357static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
358{
359 struct aac_fib_context *fibctx;
360 int status;
361 unsigned long flags;
362 struct list_head * entry;
363
364 /*
365 * Verify that the HANDLE passed in was a valid AdapterFibContext
366 *
367 * Search the list of AdapterFibContext addresses on the adapter
368 * to be sure this is a valid address
369 */
370
371 entry = dev->fib_list.next;
372 fibctx = NULL;
373
374 while(entry != &dev->fib_list) {
375 fibctx = list_entry(entry, struct aac_fib_context, next);
376 /*
377 * Extract the fibctx from the input parameters
378 */
379 if (fibctx->unique == (u32)(unsigned long)arg) {
380 /* We found a winner */
381 break;
382 }
383 entry = entry->next;
384 fibctx = NULL;
385 }
386
387 if (!fibctx)
388 return 0; /* Already gone */
389
390 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
391 (fibctx->size != sizeof(struct aac_fib_context)))
392 return -EINVAL;
393 spin_lock_irqsave(&dev->fib_lock, flags);
394 status = aac_close_fib_context(dev, fibctx);
395 spin_unlock_irqrestore(&dev->fib_lock, flags);
396 return status;
397}
398
399/**
400 * check_revision - close down user fib context
401 * @dev: adapter
402 * @arg: ioctl arguments
403 *
404 * This routine returns the driver version.
405 * Under Linux, there have been no version incompatibilities, so this is
406 * simple!
407 */
408
409static int check_revision(struct aac_dev *dev, void __user *arg)
410{
411 struct revision response;
Mark Haverkampc7f47602005-08-03 15:38:55 -0700412 char *driver_version = aac_driver_version;
413 u32 version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Mark Haverkamp9f30a322005-10-24 10:52:02 -0700415 response.compat = 1;
Mark Haverkampc7f47602005-08-03 15:38:55 -0700416 version = (simple_strtol(driver_version,
417 &driver_version, 10) << 24) | 0x00000400;
418 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
419 version += simple_strtol(driver_version + 1, NULL, 10);
420 response.version = cpu_to_le32(version);
421# if (defined(AAC_DRIVER_BUILD))
422 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
423# else
424 response.build = cpu_to_le32(9999);
425# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (copy_to_user(arg, &response, sizeof(response)))
428 return -EFAULT;
429 return 0;
430}
431
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/**
434 *
435 * aac_send_raw_scb
436 *
437 */
438
Adrian Bunk 48338692005-04-25 19:45:58 -0700439static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct fib* srbfib;
442 int status;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700443 struct aac_srb *srbcmd = NULL;
444 struct user_aac_srb *user_srbcmd = NULL;
445 struct user_aac_srb __user *user_srb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct aac_srb_reply __user *user_reply;
447 struct aac_srb_reply* reply;
448 u32 fibsize = 0;
449 u32 flags = 0;
450 s32 rcode = 0;
451 u32 data_dir;
452 void __user *sg_user[32];
453 void *sg_list[32];
454 u32 sg_indx = 0;
455 u32 byte_count = 0;
456 u32 actual_fibsize = 0;
457 int i;
458
459
460 if (!capable(CAP_SYS_ADMIN)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700461 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -EPERM;
463 }
464 /*
465 * Allocate and initialize a Fib then setup a BlockWrite command
466 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800467 if (!(srbfib = aac_fib_alloc(dev))) {
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700468 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800470 aac_fib_init(srbfib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 srbcmd = (struct aac_srb*) fib_data(srbfib);
473
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700474 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700476 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 rcode = -EFAULT;
478 goto cleanup;
479 }
480
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700481 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 rcode = -EINVAL;
483 goto cleanup;
484 }
485
Dave Jones4645df12005-07-12 13:58:08 -0700486 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700487 if (!user_srbcmd) {
488 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
489 rcode = -ENOMEM;
490 goto cleanup;
491 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700492 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700493 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 rcode = -EFAULT;
495 goto cleanup;
496 }
497
498 user_reply = arg+fibsize;
499
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700500 flags = user_srbcmd->flags; /* from user in cpu order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 // Fix up srb for endian and force some values
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700504 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
505 srbcmd->id = cpu_to_le32(user_srbcmd->id);
506 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700507 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700508 srbcmd->flags = cpu_to_le32(flags);
509 srbcmd->retry_limit = 0; // Obsolete parameter
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700510 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700511 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700513 switch (flags & (SRB_DataIn | SRB_DataOut)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case SRB_DataOut:
515 data_dir = DMA_TO_DEVICE;
516 break;
517 case (SRB_DataIn | SRB_DataOut):
518 data_dir = DMA_BIDIRECTIONAL;
519 break;
520 case SRB_DataIn:
521 data_dir = DMA_FROM_DEVICE;
522 break;
523 default:
524 data_dir = DMA_NONE;
525 }
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700526 if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700527 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
528 le32_to_cpu(srbcmd->sg.count)));
529 rcode = -EINVAL;
530 goto cleanup;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (dev->dac_support == 1) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700533 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
Mark Haverkamp84e29302005-07-07 13:40:00 -0700534 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700535 struct user_sgmap* usg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 byte_count = 0;
537
538 /*
539 * This should also catch if user used the 32 bit sgmap
540 */
541 actual_fibsize = sizeof(struct aac_srb) -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700542 sizeof(struct sgentry) +
543 ((upsg->count & 0xff) *
544 sizeof(struct sgentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700546 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 rcode = -EINVAL;
548 goto cleanup;
549 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700550 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
551 + sizeof(struct sgmap), GFP_KERNEL);
552 if (!usg) {
553 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
554 rcode = -ENOMEM;
555 goto cleanup;
556 }
557 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
558 + sizeof(struct sgmap));
559 actual_fibsize = sizeof(struct aac_srb) -
560 sizeof(struct sgentry) + ((usg->count & 0xff) *
561 sizeof(struct sgentry64));
562 if ((data_dir == DMA_NONE) && upsg->count) {
563 kfree (usg);
564 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rcode = -EINVAL;
566 goto cleanup;
567 }
568
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700569 for (i = 0; i < usg->count; i++) {
570 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 void* p;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700572 /* Does this really need to be GFP_DMA? */
573 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if(p == 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700575 kfree (usg);
576 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
577 usg->sg[i].count,i,usg->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 rcode = -ENOMEM;
579 goto cleanup;
580 }
Mark Haverkampe75d5172005-10-24 10:52:11 -0700581 sg_user[i] = (void __user *)(long)usg->sg[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 sg_list[i] = p; // save so we can clean up later
583 sg_indx = i;
584
585 if( flags & SRB_DataOut ){
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700586 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700587 kfree (usg);
588 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rcode = -EFAULT;
590 goto cleanup;
591 }
592 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700593 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700595 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700596 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
597 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
598 byte_count += usg->sg[i].count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700600 kfree (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700603 psg->count = cpu_to_le32(sg_indx+1);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800604 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 } else {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700606 struct user_sgmap* upsg = &user_srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct sgmap* psg = &srbcmd->sg;
608 byte_count = 0;
609
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700610 actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700612 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rcode = -EINVAL;
614 goto cleanup;
615 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700616 if ((data_dir == DMA_NONE) && upsg->count) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700617 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 rcode = -EINVAL;
619 goto cleanup;
620 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700621 for (i = 0; i < upsg->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dma_addr_t addr;
623 void* p;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700624 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if(p == 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700626 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
627 upsg->sg[i].count, i, upsg->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 rcode = -ENOMEM;
629 goto cleanup;
630 }
Mark Haverkampe75d5172005-10-24 10:52:11 -0700631 sg_user[i] = (void __user *)(long)upsg->sg[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 sg_list[i] = p; // save so we can clean up later
633 sg_indx = i;
634
635 if( flags & SRB_DataOut ){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700636 if(copy_from_user(p, sg_user[i],
637 upsg->sg[i].count)) {
638 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 rcode = -EFAULT;
640 goto cleanup;
641 }
642 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700643 addr = pci_map_single(dev->pdev, p,
644 upsg->sg[i].count, data_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 psg->sg[i].addr = cpu_to_le32(addr);
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700647 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
648 byte_count += upsg->sg[i].count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700651 psg->count = cpu_to_le32(sg_indx+1);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800652 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 if (status != 0){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700656 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700657 rcode = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto cleanup;
659 }
660
661 if( flags & SRB_DataIn ) {
662 for(i = 0 ; i <= sg_indx; i++){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700663 byte_count = le32_to_cpu((dev->dac_support == 1)
664 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
665 : srbcmd->sg.sg[i].count);
666 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
667 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 rcode = -EFAULT;
669 goto cleanup;
670
671 }
672 }
673 }
674
675 reply = (struct aac_srb_reply *) fib_data(srbfib);
676 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700677 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 rcode = -EFAULT;
679 goto cleanup;
680 }
681
682cleanup:
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700683 kfree(user_srbcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 for(i=0; i <= sg_indx; i++){
685 kfree(sg_list[i]);
686 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800687 aac_fib_complete(srbfib);
688 aac_fib_free(srbfib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 return rcode;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693struct aac_pci_info {
694 u32 bus;
695 u32 slot;
696};
697
698
Adrian Bunk 48338692005-04-25 19:45:58 -0700699static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct aac_pci_info pci_info;
702
703 pci_info.bus = dev->pdev->bus->number;
704 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
705
706 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700707 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -EFAULT;
709 }
710 return 0;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713
714int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
715{
716 int status;
717
718 /*
719 * HBA gets first crack
720 */
721
722 status = aac_dev_ioctl(dev, cmd, arg);
723 if(status != -ENOTTY)
724 return status;
725
726 switch (cmd) {
727 case FSACTL_MINIPORT_REV_CHECK:
728 status = check_revision(dev, arg);
729 break;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700730 case FSACTL_SEND_LARGE_FIB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 case FSACTL_SENDFIB:
732 status = ioctl_send_fib(dev, arg);
733 break;
734 case FSACTL_OPEN_GET_ADAPTER_FIB:
735 status = open_getadapter_fib(dev, arg);
736 break;
737 case FSACTL_GET_NEXT_ADAPTER_FIB:
738 status = next_getadapter_fib(dev, arg);
739 break;
740 case FSACTL_CLOSE_GET_ADAPTER_FIB:
741 status = close_getadapter_fib(dev, arg);
742 break;
743 case FSACTL_SEND_RAW_SRB:
744 status = aac_send_raw_srb(dev,arg);
745 break;
746 case FSACTL_GET_PCI_INFO:
747 status = aac_get_pci_info(dev,arg);
748 break;
749 default:
750 status = -ENOTTY;
751 break;
752 }
753 return status;
754}
755