blob: 851a7e599c500bc5f4e24b2a3aba4a23a4e153f9 [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 *
Mark Haverkampf2b1a062007-03-15 10:27:32 -07008 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/pci.h>
35#include <linux/spinlock.h>
36#include <linux/slab.h>
37#include <linux/completion.h>
38#include <linux/dma-mapping.h>
39#include <linux/blkdev.h>
Mark Haverkampc8f7b072006-08-03 08:02:24 -070040#include <linux/delay.h> /* ssleep prototype */
Mark Haverkampdc4adbf2006-03-27 09:44:26 -080041#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/semaphore.h>
43#include <asm/uaccess.h>
44
45#include "aacraid.h"
46
47/**
48 * ioctl_send_fib - send a FIB from userspace
49 * @dev: adapter is being processed
50 * @arg: arguments to the ioctl call
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * This routine sends a fib to the adapter on behalf of a user level
53 * program.
54 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070055# define AAC_DEBUG_PREAMBLE KERN_INFO
56# define AAC_DEBUG_POSTAMBLE
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
59{
60 struct hw_fib * kfib;
61 struct fib *fibptr;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070062 struct hw_fib * hw_fib = (struct hw_fib *)0;
63 dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
64 unsigned size;
65 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Mark Haverkamp33bb3b22007-03-15 10:27:21 -070067 if (dev->in_reset) {
68 return -EBUSY;
69 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080070 fibptr = aac_fib_alloc(dev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070071 if(fibptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -ENOMEM;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070073 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080074
Mark Haverkampa8166a52007-03-15 10:26:22 -070075 kfib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /*
77 * First copy in the header so that we can check the size field.
78 */
79 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080080 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EFAULT;
82 }
83 /*
84 * Since we copy based on the fib header size, make sure that we
85 * will not overrun the buffer when we copy the memory. Return
86 * an error if we would.
87 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070088 size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
89 if (size < le16_to_cpu(kfib->header.SenderSize))
90 size = le16_to_cpu(kfib->header.SenderSize);
91 if (size > dev->max_fib_size) {
Mark Haverkamp6e289a92006-01-11 09:28:07 -080092 if (size > 2048) {
93 retval = -EINVAL;
94 goto cleanup;
95 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070096 /* Highjack the hw_fib */
Mark Haverkampa8166a52007-03-15 10:26:22 -070097 hw_fib = fibptr->hw_fib_va;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070098 hw_fib_pa = fibptr->hw_fib_pa;
Mark Haverkampa8166a52007-03-15 10:26:22 -070099 fibptr->hw_fib_va = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700100 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
101 memcpy(kfib, hw_fib, dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700104 if (copy_from_user(kfib, arg, size)) {
105 retval = -EFAULT;
106 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700109 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 aac_adapter_interrupt(dev);
111 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800112 * Since we didn't really send a fib, zero out the state to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * cleanup code not to assert.
114 */
115 kfib->header.XferState = 0;
116 } else {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800117 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 le16_to_cpu(kfib->header.Size) , FsaNormal,
119 1, 1, NULL, NULL);
120 if (retval) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700121 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800123 if (aac_fib_complete(fibptr) != 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700124 retval = -EINVAL;
125 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 }
128 /*
129 * Make sure that the size returned by the adapter (which includes
130 * the header) is less than or equal to the size of a fib, so we
131 * don't corrupt application data. Then copy that size to the user
132 * buffer. (Don't try to add the header information again, since it
133 * was already included by the adapter.)
134 */
135
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700136 retval = 0;
137 if (copy_to_user(arg, (void *)kfib, size))
138 retval = -EFAULT;
139cleanup:
140 if (hw_fib) {
141 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
142 fibptr->hw_fib_pa = hw_fib_pa;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700143 fibptr->hw_fib_va = hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700145 if (retval != -EINTR)
146 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700147 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/**
151 * open_getadapter_fib - Get the next fib
152 *
153 * This routine will get the next Fib, if available, from the AdapterFibContext
154 * passed in from the user.
155 */
156
157static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
158{
159 struct aac_fib_context * fibctx;
160 int status;
161
162 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
163 if (fibctx == NULL) {
164 status = -ENOMEM;
165 } else {
166 unsigned long flags;
167 struct list_head * entry;
168 struct aac_fib_context * context;
169
170 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
171 fibctx->size = sizeof(struct aac_fib_context);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800172 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Yes yes, I know this could be an index, but we have a
174 * better guarantee of uniqueness for the locked loop below.
175 * Without the aid of a persistent history, this also helps
176 * reduce the chance that the opaque context would be reused.
177 */
178 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
179 /*
180 * Initialize the mutex used to wait for the next AIF.
181 */
182 init_MUTEX_LOCKED(&fibctx->wait_sem);
183 fibctx->wait = 0;
184 /*
185 * Initialize the fibs and set the count of fibs on
186 * the list to 0.
187 */
188 fibctx->count = 0;
189 INIT_LIST_HEAD(&fibctx->fib_list);
190 fibctx->jiffies = jiffies/HZ;
191 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800192 * Now add this context onto the adapter's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * AdapterFibContext list.
194 */
195 spin_lock_irqsave(&dev->fib_lock, flags);
196 /* Ensure that we have a unique identifier */
197 entry = dev->fib_list.next;
198 while (entry != &dev->fib_list) {
199 context = list_entry(entry, struct aac_fib_context, next);
200 if (context->unique == fibctx->unique) {
201 /* Not unique (32 bits) */
202 fibctx->unique++;
203 entry = dev->fib_list.next;
204 } else {
205 entry = entry->next;
206 }
207 }
208 list_add_tail(&fibctx->next, &dev->fib_list);
209 spin_unlock_irqrestore(&dev->fib_lock, flags);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800210 if (copy_to_user(arg, &fibctx->unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sizeof(fibctx->unique))) {
212 status = -EFAULT;
213 } else {
214 status = 0;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 return status;
218}
219
220/**
221 * next_getadapter_fib - get the next fib
222 * @dev: adapter to use
223 * @arg: ioctl argument
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800224 *
225 * This routine will get the next Fib, if available, from the AdapterFibContext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * passed in from the user.
227 */
228
229static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
230{
231 struct fib_ioctl f;
232 struct fib *fib;
233 struct aac_fib_context *fibctx;
234 int status;
235 struct list_head * entry;
236 unsigned long flags;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
239 return -EFAULT;
240 /*
241 * Verify that the HANDLE passed in was a valid AdapterFibContext
242 *
243 * Search the list of AdapterFibContext addresses on the adapter
244 * to be sure this is a valid address
245 */
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800246 spin_lock_irqsave(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 entry = dev->fib_list.next;
248 fibctx = NULL;
249
250 while (entry != &dev->fib_list) {
251 fibctx = list_entry(entry, struct aac_fib_context, next);
252 /*
253 * Extract the AdapterFibContext from the Input parameters.
254 */
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800255 if (fibctx->unique == f.fibctx) { /* We found a winner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 }
258 entry = entry->next;
259 fibctx = NULL;
260 }
261 if (!fibctx) {
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800262 spin_unlock_irqrestore(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 dprintk ((KERN_INFO "Fib Context not found\n"));
264 return -EINVAL;
265 }
266
267 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
268 (fibctx->size != sizeof(struct aac_fib_context))) {
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800269 spin_unlock_irqrestore(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
271 return -EINVAL;
272 }
273 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
275 * If there are no fibs to send back, then either wait or return
276 * -EAGAIN
277 */
278return_fib:
279 if (!list_empty(&fibctx->fib_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * Pull the next fib from the fibs
282 */
283 entry = fibctx->fib_list.next;
284 list_del(entry);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 fib = list_entry(entry, struct fib, fiblink);
287 fibctx->count--;
288 spin_unlock_irqrestore(&dev->fib_lock, flags);
Mark Haverkampa8166a52007-03-15 10:26:22 -0700289 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
290 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 kfree(fib);
292 return -EFAULT;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * Free the space occupied by this copy of the fib.
296 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700297 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 kfree(fib);
299 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } else {
301 spin_unlock_irqrestore(&dev->fib_lock, flags);
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800302 /* If someone killed the AIF aacraid thread, restart it */
303 status = !dev->aif_thread;
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700304 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800305 /* Be paranoid, be very paranoid! */
306 kthread_stop(dev->thread);
307 ssleep(1);
308 dev->aif_thread = 0;
309 dev->thread = kthread_run(aac_command_thread, dev, dev->name);
310 ssleep(1);
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (f.wait) {
313 if(down_interruptible(&fibctx->wait_sem) < 0) {
314 status = -EINTR;
315 } else {
316 /* Lock again and retry */
317 spin_lock_irqsave(&dev->fib_lock, flags);
318 goto return_fib;
319 }
320 } else {
321 status = -EAGAIN;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Mark Haverkamp12a26d02005-08-03 15:39:25 -0700324 fibctx->jiffies = jiffies/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return status;
326}
327
328int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
329{
330 struct fib *fib;
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800331 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800333 spin_lock_irqsave(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * First free any FIBs that have not been consumed.
336 */
337 while (!list_empty(&fibctx->fib_list)) {
338 struct list_head * entry;
339 /*
340 * Pull the next fib from the fibs
341 */
342 entry = fibctx->fib_list.next;
343 list_del(entry);
344 fib = list_entry(entry, struct fib, fiblink);
345 fibctx->count--;
346 /*
347 * Free the space occupied by this copy of the fib.
348 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700349 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(fib);
351 }
352 /*
353 * Remove the Context from the AdapterFibContext List
354 */
355 list_del(&fibctx->next);
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800356 spin_unlock_irqrestore(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * Invalidate context
359 */
360 fibctx->type = 0;
361 /*
362 * Free the space occupied by the Context
363 */
364 kfree(fibctx);
365 return 0;
366}
367
368/**
369 * close_getadapter_fib - close down user fib context
370 * @dev: adapter
371 * @arg: ioctl arguments
372 *
373 * This routine will close down the fibctx passed in from the user.
374 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
377{
378 struct aac_fib_context *fibctx;
379 int status;
380 unsigned long flags;
381 struct list_head * entry;
382
383 /*
384 * Verify that the HANDLE passed in was a valid AdapterFibContext
385 *
386 * Search the list of AdapterFibContext addresses on the adapter
387 * to be sure this is a valid address
388 */
389
390 entry = dev->fib_list.next;
391 fibctx = NULL;
392
393 while(entry != &dev->fib_list) {
394 fibctx = list_entry(entry, struct aac_fib_context, next);
395 /*
396 * Extract the fibctx from the input parameters
397 */
Al Viro142956a2007-10-29 05:11:28 +0000398 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 entry = entry->next;
401 fibctx = NULL;
402 }
403
404 if (!fibctx)
405 return 0; /* Already gone */
406
407 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
408 (fibctx->size != sizeof(struct aac_fib_context)))
409 return -EINVAL;
410 spin_lock_irqsave(&dev->fib_lock, flags);
411 status = aac_close_fib_context(dev, fibctx);
412 spin_unlock_irqrestore(&dev->fib_lock, flags);
413 return status;
414}
415
416/**
417 * check_revision - close down user fib context
418 * @dev: adapter
419 * @arg: ioctl arguments
420 *
421 * This routine returns the driver version.
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800422 * Under Linux, there have been no version incompatibilities, so this is
423 * simple!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
425
426static int check_revision(struct aac_dev *dev, void __user *arg)
427{
428 struct revision response;
Mark Haverkampc7f47602005-08-03 15:38:55 -0700429 char *driver_version = aac_driver_version;
430 u32 version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Mark Haverkamp9f30a322005-10-24 10:52:02 -0700432 response.compat = 1;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800433 version = (simple_strtol(driver_version,
Mark Haverkampc7f47602005-08-03 15:38:55 -0700434 &driver_version, 10) << 24) | 0x00000400;
435 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
436 version += simple_strtol(driver_version + 1, NULL, 10);
437 response.version = cpu_to_le32(version);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800438# ifdef AAC_DRIVER_BUILD
Mark Haverkampc7f47602005-08-03 15:38:55 -0700439 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
440# else
441 response.build = cpu_to_le32(9999);
442# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (copy_to_user(arg, &response, sizeof(response)))
445 return -EFAULT;
446 return 0;
447}
448
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/**
451 *
452 * aac_send_raw_scb
453 *
454 */
455
Adrian Bunk 48338692005-04-25 19:45:58 -0700456static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct fib* srbfib;
459 int status;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700460 struct aac_srb *srbcmd = NULL;
461 struct user_aac_srb *user_srbcmd = NULL;
462 struct user_aac_srb __user *user_srb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct aac_srb_reply __user *user_reply;
464 struct aac_srb_reply* reply;
465 u32 fibsize = 0;
466 u32 flags = 0;
467 s32 rcode = 0;
468 u32 data_dir;
469 void __user *sg_user[32];
470 void *sg_list[32];
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800471 u32 sg_indx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 u32 byte_count = 0;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700473 u32 actual_fibsize64, actual_fibsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int i;
475
476
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700477 if (dev->in_reset) {
478 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
479 return -EBUSY;
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!capable(CAP_SYS_ADMIN)){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800482 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EPERM;
484 }
485 /*
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700486 * Allocate and initialize a Fib then setup a SRB command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800488 if (!(srbfib = aac_fib_alloc(dev))) {
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700489 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800491 aac_fib_init(srbfib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 srbcmd = (struct aac_srb*) fib_data(srbfib);
494
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700495 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800497 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 rcode = -EFAULT;
499 goto cleanup;
500 }
501
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700502 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rcode = -EINVAL;
504 goto cleanup;
505 }
506
Dave Jones4645df12005-07-12 13:58:08 -0700507 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700508 if (!user_srbcmd) {
509 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
510 rcode = -ENOMEM;
511 goto cleanup;
512 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700513 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800514 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 rcode = -EFAULT;
516 goto cleanup;
517 }
518
519 user_reply = arg+fibsize;
520
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700521 flags = user_srbcmd->flags; /* from user in cpu order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 // Fix up srb for endian and force some values
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800525 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700526 srbcmd->id = cpu_to_le32(user_srbcmd->id);
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800527 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
528 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
529 srbcmd->flags = cpu_to_le32(flags);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700530 srbcmd->retry_limit = 0; // Obsolete parameter
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700531 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700532 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800533
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700534 switch (flags & (SRB_DataIn | SRB_DataOut)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 case SRB_DataOut:
536 data_dir = DMA_TO_DEVICE;
537 break;
538 case (SRB_DataIn | SRB_DataOut):
539 data_dir = DMA_BIDIRECTIONAL;
540 break;
541 case SRB_DataIn:
542 data_dir = DMA_FROM_DEVICE;
543 break;
544 default:
545 data_dir = DMA_NONE;
546 }
Tobias Klauser6391a112006-06-08 22:23:48 -0700547 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700548 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
549 le32_to_cpu(srbcmd->sg.count)));
550 rcode = -EINVAL;
551 goto cleanup;
552 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700553 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
554 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
555 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
556 (sizeof(struct sgentry64) - sizeof(struct sgentry));
557 /* User made a mistake - should not continue */
558 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
559 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
560 "Raw SRB command calculated fibsize=%lu;%lu "
561 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
562 "issued fibsize=%d\n",
563 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
564 sizeof(struct aac_srb), sizeof(struct sgentry),
565 sizeof(struct sgentry64), fibsize));
566 rcode = -EINVAL;
567 goto cleanup;
568 }
569 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
570 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
571 rcode = -EINVAL;
572 goto cleanup;
573 }
574 byte_count = 0;
575 if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700576 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
Mark Haverkamp84e29302005-07-07 13:40:00 -0700577 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /*
580 * This should also catch if user used the 32 bit sgmap
581 */
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700582 if (actual_fibsize64 == fibsize) {
583 actual_fibsize = actual_fibsize64;
584 for (i = 0; i < upsg->count; i++) {
585 u64 addr;
586 void* p;
587 /* Does this really need to be GFP_DMA? */
588 p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800589 if(!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700590 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
591 upsg->sg[i].count,i,upsg->count));
592 rcode = -ENOMEM;
593 goto cleanup;
594 }
595 addr = (u64)upsg->sg[i].addr[0];
596 addr += ((u64)upsg->sg[i].addr[1]) << 32;
Al Viro142956a2007-10-29 05:11:28 +0000597 sg_user[i] = (void __user *)(uintptr_t)addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700598 sg_list[i] = p; // save so we can clean up later
599 sg_indx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800601 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700602 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
603 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
604 rcode = -EFAULT;
605 goto cleanup;
606 }
607 }
608 addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
609
610 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
611 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
612 byte_count += upsg->sg[i].count;
613 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
614 }
615 } else {
616 struct user_sgmap* usg;
617 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
618 + sizeof(struct sgmap), GFP_KERNEL);
619 if (!usg) {
620 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 rcode = -ENOMEM;
622 goto cleanup;
623 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700624 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
625 + sizeof(struct sgmap));
626 actual_fibsize = actual_fibsize64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700628 for (i = 0; i < usg->count; i++) {
629 u64 addr;
630 void* p;
631 /* Does this really need to be GFP_DMA? */
632 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800633 if(!p) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700634 kfree (usg);
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700635 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
636 usg->sg[i].count,i,usg->count));
637 rcode = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto cleanup;
639 }
Al Viro142956a2007-10-29 05:11:28 +0000640 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700641 sg_list[i] = p; // save so we can clean up later
642 sg_indx = i;
643
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800644 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700645 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
646 kfree (usg);
647 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
648 rcode = -EFAULT;
649 goto cleanup;
650 }
651 }
652 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
653
654 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
655 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
656 byte_count += usg->sg[i].count;
657 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700659 kfree (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700662 psg->count = cpu_to_le32(sg_indx+1);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800663 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700665 struct user_sgmap* upsg = &user_srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct sgmap* psg = &srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700668 if (actual_fibsize64 == fibsize) {
669 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
670 for (i = 0; i < upsg->count; i++) {
Al Viro142956a2007-10-29 05:11:28 +0000671 uintptr_t addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700672 void* p;
673 /* Does this really need to be GFP_DMA? */
674 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800675 if(!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700676 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
677 usg->sg[i].count,i,usg->count));
678 rcode = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto cleanup;
680 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700681 addr = (u64)usg->sg[i].addr[0];
682 addr += ((u64)usg->sg[i].addr[1]) << 32;
Al Viro142956a2007-10-29 05:11:28 +0000683 sg_user[i] = (void __user *)addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700684 sg_list[i] = p; // save so we can clean up later
685 sg_indx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800687 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700688 if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
689 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
690 rcode = -EFAULT;
691 goto cleanup;
692 }
693 }
694 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
695
696 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
697 byte_count += usg->sg[i].count;
698 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
699 }
700 } else {
701 for (i = 0; i < upsg->count; i++) {
702 dma_addr_t addr;
703 void* p;
704 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800705 if (!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700706 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
707 upsg->sg[i].count, i, upsg->count));
708 rcode = -ENOMEM;
709 goto cleanup;
710 }
Al Viro142956a2007-10-29 05:11:28 +0000711 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700712 sg_list[i] = p; // save so we can clean up later
713 sg_indx = i;
714
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800715 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700716 if(copy_from_user(p, sg_user[i],
717 upsg->sg[i].count)) {
718 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
719 rcode = -EFAULT;
720 goto cleanup;
721 }
722 }
723 addr = pci_map_single(dev->pdev, p,
724 upsg->sg[i].count, data_dir);
725
726 psg->sg[i].addr = cpu_to_le32(addr);
727 byte_count += upsg->sg[i].count;
728 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700732 psg->count = cpu_to_le32(sg_indx+1);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800733 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700735 if (status == -EINTR) {
736 rcode = -EINTR;
737 goto cleanup;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (status != 0){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800741 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700742 rcode = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto cleanup;
744 }
745
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800746 if (flags & SRB_DataIn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 for(i = 0 ; i <= sg_indx; i++){
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700748 byte_count = le32_to_cpu(
749 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700750 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
751 : srbcmd->sg.sg[i].count);
752 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800753 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rcode = -EFAULT;
755 goto cleanup;
756
757 }
758 }
759 }
760
761 reply = (struct aac_srb_reply *) fib_data(srbfib);
762 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800763 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 rcode = -EFAULT;
765 goto cleanup;
766 }
767
768cleanup:
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700769 kfree(user_srbcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 for(i=0; i <= sg_indx; i++){
771 kfree(sg_list[i]);
772 }
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700773 if (rcode != -EINTR) {
774 aac_fib_complete(srbfib);
775 aac_fib_free(srbfib);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 return rcode;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781struct aac_pci_info {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800782 u32 bus;
783 u32 slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784};
785
786
Adrian Bunk 48338692005-04-25 19:45:58 -0700787static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800789 struct aac_pci_info pci_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 pci_info.bus = dev->pdev->bus->number;
792 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
793
Salyzyn, Marka119ee82008-01-24 07:40:10 -0800794 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
795 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
796 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800798 return 0;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700799}
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
803{
804 int status;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * HBA gets first crack
808 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 status = aac_dev_ioctl(dev, cmd, arg);
811 if(status != -ENOTTY)
812 return status;
813
814 switch (cmd) {
815 case FSACTL_MINIPORT_REV_CHECK:
816 status = check_revision(dev, arg);
817 break;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700818 case FSACTL_SEND_LARGE_FIB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 case FSACTL_SENDFIB:
820 status = ioctl_send_fib(dev, arg);
821 break;
822 case FSACTL_OPEN_GET_ADAPTER_FIB:
823 status = open_getadapter_fib(dev, arg);
824 break;
825 case FSACTL_GET_NEXT_ADAPTER_FIB:
826 status = next_getadapter_fib(dev, arg);
827 break;
828 case FSACTL_CLOSE_GET_ADAPTER_FIB:
829 status = close_getadapter_fib(dev, arg);
830 break;
831 case FSACTL_SEND_RAW_SRB:
832 status = aac_send_raw_srb(dev,arg);
833 break;
834 case FSACTL_GET_PCI_INFO:
835 status = aac_get_pci_info(dev,arg);
836 break;
837 default:
838 status = -ENOTTY;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 return status;
842}
843