blob: a27207e27c304e82ea673e84ff6637a35326a507 [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
51 *
52 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
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 /*
112 * Since we didn't really send a fib, zero out the state to allow
113 * 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);
172 /*
173 * 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 /*
192 * Now add this context onto the adapter's
193 * 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);
210 if (copy_to_user(arg, &fibctx->unique,
211 sizeof(fibctx->unique))) {
212 status = -EFAULT;
213 } else {
214 status = 0;
215 }
216 }
217 return status;
218}
219
220/**
221 * next_getadapter_fib - get the next fib
222 * @dev: adapter to use
223 * @arg: ioctl argument
224 *
225 * This routine will get the next Fib, if available, from the AdapterFibContext
226 * 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;
237
238 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 */
246 entry = dev->fib_list.next;
247 fibctx = NULL;
248
249 while (entry != &dev->fib_list) {
250 fibctx = list_entry(entry, struct aac_fib_context, next);
251 /*
252 * Extract the AdapterFibContext from the Input parameters.
253 */
254 if (fibctx->unique == f.fibctx) { /* We found a winner */
255 break;
256 }
257 entry = entry->next;
258 fibctx = NULL;
259 }
260 if (!fibctx) {
261 dprintk ((KERN_INFO "Fib Context not found\n"));
262 return -EINVAL;
263 }
264
265 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
266 (fibctx->size != sizeof(struct aac_fib_context))) {
267 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
268 return -EINVAL;
269 }
270 status = 0;
271 spin_lock_irqsave(&dev->fib_lock, flags);
272 /*
273 * If there are no fibs to send back, then either wait or return
274 * -EAGAIN
275 */
276return_fib:
277 if (!list_empty(&fibctx->fib_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /*
279 * Pull the next fib from the fibs
280 */
281 entry = fibctx->fib_list.next;
282 list_del(entry);
283
284 fib = list_entry(entry, struct fib, fiblink);
285 fibctx->count--;
286 spin_unlock_irqrestore(&dev->fib_lock, flags);
Mark Haverkampa8166a52007-03-15 10:26:22 -0700287 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
288 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 kfree(fib);
290 return -EFAULT;
291 }
292 /*
293 * Free the space occupied by this copy of the fib.
294 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700295 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 kfree(fib);
297 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } else {
299 spin_unlock_irqrestore(&dev->fib_lock, flags);
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800300 /* If someone killed the AIF aacraid thread, restart it */
301 status = !dev->aif_thread;
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700302 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800303 /* Be paranoid, be very paranoid! */
304 kthread_stop(dev->thread);
305 ssleep(1);
306 dev->aif_thread = 0;
307 dev->thread = kthread_run(aac_command_thread, dev, dev->name);
308 ssleep(1);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (f.wait) {
311 if(down_interruptible(&fibctx->wait_sem) < 0) {
312 status = -EINTR;
313 } else {
314 /* Lock again and retry */
315 spin_lock_irqsave(&dev->fib_lock, flags);
316 goto return_fib;
317 }
318 } else {
319 status = -EAGAIN;
320 }
321 }
Mark Haverkamp12a26d02005-08-03 15:39:25 -0700322 fibctx->jiffies = jiffies/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return status;
324}
325
326int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
327{
328 struct fib *fib;
329
330 /*
331 * First free any FIBs that have not been consumed.
332 */
333 while (!list_empty(&fibctx->fib_list)) {
334 struct list_head * entry;
335 /*
336 * Pull the next fib from the fibs
337 */
338 entry = fibctx->fib_list.next;
339 list_del(entry);
340 fib = list_entry(entry, struct fib, fiblink);
341 fibctx->count--;
342 /*
343 * Free the space occupied by this copy of the fib.
344 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700345 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 kfree(fib);
347 }
348 /*
349 * Remove the Context from the AdapterFibContext List
350 */
351 list_del(&fibctx->next);
352 /*
353 * Invalidate context
354 */
355 fibctx->type = 0;
356 /*
357 * Free the space occupied by the Context
358 */
359 kfree(fibctx);
360 return 0;
361}
362
363/**
364 * close_getadapter_fib - close down user fib context
365 * @dev: adapter
366 * @arg: ioctl arguments
367 *
368 * This routine will close down the fibctx passed in from the user.
369 */
370
371static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
372{
373 struct aac_fib_context *fibctx;
374 int status;
375 unsigned long flags;
376 struct list_head * entry;
377
378 /*
379 * Verify that the HANDLE passed in was a valid AdapterFibContext
380 *
381 * Search the list of AdapterFibContext addresses on the adapter
382 * to be sure this is a valid address
383 */
384
385 entry = dev->fib_list.next;
386 fibctx = NULL;
387
388 while(entry != &dev->fib_list) {
389 fibctx = list_entry(entry, struct aac_fib_context, next);
390 /*
391 * Extract the fibctx from the input parameters
392 */
Al Viro142956a2007-10-29 05:11:28 +0000393 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 entry = entry->next;
396 fibctx = NULL;
397 }
398
399 if (!fibctx)
400 return 0; /* Already gone */
401
402 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
403 (fibctx->size != sizeof(struct aac_fib_context)))
404 return -EINVAL;
405 spin_lock_irqsave(&dev->fib_lock, flags);
406 status = aac_close_fib_context(dev, fibctx);
407 spin_unlock_irqrestore(&dev->fib_lock, flags);
408 return status;
409}
410
411/**
412 * check_revision - close down user fib context
413 * @dev: adapter
414 * @arg: ioctl arguments
415 *
416 * This routine returns the driver version.
417 * Under Linux, there have been no version incompatibilities, so this is
418 * simple!
419 */
420
421static int check_revision(struct aac_dev *dev, void __user *arg)
422{
423 struct revision response;
Mark Haverkampc7f47602005-08-03 15:38:55 -0700424 char *driver_version = aac_driver_version;
425 u32 version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Mark Haverkamp9f30a322005-10-24 10:52:02 -0700427 response.compat = 1;
Mark Haverkampc7f47602005-08-03 15:38:55 -0700428 version = (simple_strtol(driver_version,
429 &driver_version, 10) << 24) | 0x00000400;
430 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
431 version += simple_strtol(driver_version + 1, NULL, 10);
432 response.version = cpu_to_le32(version);
433# if (defined(AAC_DRIVER_BUILD))
434 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
435# else
436 response.build = cpu_to_le32(9999);
437# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (copy_to_user(arg, &response, sizeof(response)))
440 return -EFAULT;
441 return 0;
442}
443
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/**
446 *
447 * aac_send_raw_scb
448 *
449 */
450
Adrian Bunk 48338692005-04-25 19:45:58 -0700451static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct fib* srbfib;
454 int status;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700455 struct aac_srb *srbcmd = NULL;
456 struct user_aac_srb *user_srbcmd = NULL;
457 struct user_aac_srb __user *user_srb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct aac_srb_reply __user *user_reply;
459 struct aac_srb_reply* reply;
460 u32 fibsize = 0;
461 u32 flags = 0;
462 s32 rcode = 0;
463 u32 data_dir;
464 void __user *sg_user[32];
465 void *sg_list[32];
466 u32 sg_indx = 0;
467 u32 byte_count = 0;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700468 u32 actual_fibsize64, actual_fibsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int i;
470
471
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700472 if (dev->in_reset) {
473 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
474 return -EBUSY;
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!capable(CAP_SYS_ADMIN)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700477 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EPERM;
479 }
480 /*
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700481 * Allocate and initialize a Fib then setup a SRB command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800483 if (!(srbfib = aac_fib_alloc(dev))) {
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700484 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800486 aac_fib_init(srbfib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 srbcmd = (struct aac_srb*) fib_data(srbfib);
489
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700490 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700492 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 rcode = -EFAULT;
494 goto cleanup;
495 }
496
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700497 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 rcode = -EINVAL;
499 goto cleanup;
500 }
501
Dave Jones4645df12005-07-12 13:58:08 -0700502 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700503 if (!user_srbcmd) {
504 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
505 rcode = -ENOMEM;
506 goto cleanup;
507 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700508 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700509 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 rcode = -EFAULT;
511 goto cleanup;
512 }
513
514 user_reply = arg+fibsize;
515
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700516 flags = user_srbcmd->flags; /* from user in cpu order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 // Fix up srb for endian and force some values
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700520 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
521 srbcmd->id = cpu_to_le32(user_srbcmd->id);
522 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700523 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700524 srbcmd->flags = cpu_to_le32(flags);
525 srbcmd->retry_limit = 0; // Obsolete parameter
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700526 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700527 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700529 switch (flags & (SRB_DataIn | SRB_DataOut)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case SRB_DataOut:
531 data_dir = DMA_TO_DEVICE;
532 break;
533 case (SRB_DataIn | SRB_DataOut):
534 data_dir = DMA_BIDIRECTIONAL;
535 break;
536 case SRB_DataIn:
537 data_dir = DMA_FROM_DEVICE;
538 break;
539 default:
540 data_dir = DMA_NONE;
541 }
Tobias Klauser6391a112006-06-08 22:23:48 -0700542 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700543 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
544 le32_to_cpu(srbcmd->sg.count)));
545 rcode = -EINVAL;
546 goto cleanup;
547 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700548 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
549 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
550 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
551 (sizeof(struct sgentry64) - sizeof(struct sgentry));
552 /* User made a mistake - should not continue */
553 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
554 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
555 "Raw SRB command calculated fibsize=%lu;%lu "
556 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
557 "issued fibsize=%d\n",
558 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
559 sizeof(struct aac_srb), sizeof(struct sgentry),
560 sizeof(struct sgentry64), fibsize));
561 rcode = -EINVAL;
562 goto cleanup;
563 }
564 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
565 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
566 rcode = -EINVAL;
567 goto cleanup;
568 }
569 byte_count = 0;
570 if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700571 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
Mark Haverkamp84e29302005-07-07 13:40:00 -0700572 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /*
575 * This should also catch if user used the 32 bit sgmap
576 */
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700577 if (actual_fibsize64 == fibsize) {
578 actual_fibsize = actual_fibsize64;
579 for (i = 0; i < upsg->count; i++) {
580 u64 addr;
581 void* p;
582 /* Does this really need to be GFP_DMA? */
583 p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800584 if(!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700585 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
586 upsg->sg[i].count,i,upsg->count));
587 rcode = -ENOMEM;
588 goto cleanup;
589 }
590 addr = (u64)upsg->sg[i].addr[0];
591 addr += ((u64)upsg->sg[i].addr[1]) << 32;
Al Viro142956a2007-10-29 05:11:28 +0000592 sg_user[i] = (void __user *)(uintptr_t)addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700593 sg_list[i] = p; // save so we can clean up later
594 sg_indx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700596 if( flags & SRB_DataOut ){
597 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
598 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
599 rcode = -EFAULT;
600 goto cleanup;
601 }
602 }
603 addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
604
605 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
606 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
607 byte_count += upsg->sg[i].count;
608 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
609 }
610 } else {
611 struct user_sgmap* usg;
612 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
613 + sizeof(struct sgmap), GFP_KERNEL);
614 if (!usg) {
615 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 rcode = -ENOMEM;
617 goto cleanup;
618 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700619 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
620 + sizeof(struct sgmap));
621 actual_fibsize = actual_fibsize64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700623 for (i = 0; i < usg->count; i++) {
624 u64 addr;
625 void* p;
626 /* Does this really need to be GFP_DMA? */
627 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800628 if(!p) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700629 kfree (usg);
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700630 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
631 usg->sg[i].count,i,usg->count));
632 rcode = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto cleanup;
634 }
Al Viro142956a2007-10-29 05:11:28 +0000635 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700636 sg_list[i] = p; // save so we can clean up later
637 sg_indx = i;
638
639 if( flags & SRB_DataOut ){
640 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
641 kfree (usg);
642 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
643 rcode = -EFAULT;
644 goto cleanup;
645 }
646 }
647 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
648
649 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
650 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
651 byte_count += usg->sg[i].count;
652 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700654 kfree (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700657 psg->count = cpu_to_le32(sg_indx+1);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800658 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700660 struct user_sgmap* upsg = &user_srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct sgmap* psg = &srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700663 if (actual_fibsize64 == fibsize) {
664 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
665 for (i = 0; i < upsg->count; i++) {
Al Viro142956a2007-10-29 05:11:28 +0000666 uintptr_t addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700667 void* p;
668 /* Does this really need to be GFP_DMA? */
669 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800670 if(!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700671 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
672 usg->sg[i].count,i,usg->count));
673 rcode = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto cleanup;
675 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700676 addr = (u64)usg->sg[i].addr[0];
677 addr += ((u64)usg->sg[i].addr[1]) << 32;
Al Viro142956a2007-10-29 05:11:28 +0000678 sg_user[i] = (void __user *)addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700679 sg_list[i] = p; // save so we can clean up later
680 sg_indx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700682 if( flags & SRB_DataOut ){
683 if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
684 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
685 rcode = -EFAULT;
686 goto cleanup;
687 }
688 }
689 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
690
691 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
692 byte_count += usg->sg[i].count;
693 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
694 }
695 } else {
696 for (i = 0; i < upsg->count; i++) {
697 dma_addr_t addr;
698 void* p;
699 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800700 if (!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700701 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
702 upsg->sg[i].count, i, upsg->count));
703 rcode = -ENOMEM;
704 goto cleanup;
705 }
Al Viro142956a2007-10-29 05:11:28 +0000706 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700707 sg_list[i] = p; // save so we can clean up later
708 sg_indx = i;
709
710 if( flags & SRB_DataOut ){
711 if(copy_from_user(p, sg_user[i],
712 upsg->sg[i].count)) {
713 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
714 rcode = -EFAULT;
715 goto cleanup;
716 }
717 }
718 addr = pci_map_single(dev->pdev, p,
719 upsg->sg[i].count, data_dir);
720
721 psg->sg[i].addr = cpu_to_le32(addr);
722 byte_count += upsg->sg[i].count;
723 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 srbcmd->count = cpu_to_le32(byte_count);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700727 psg->count = cpu_to_le32(sg_indx+1);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800728 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700730 if (status == -EINTR) {
731 rcode = -EINTR;
732 goto cleanup;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (status != 0){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700736 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700737 rcode = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto cleanup;
739 }
740
741 if( flags & SRB_DataIn ) {
742 for(i = 0 ; i <= sg_indx; i++){
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700743 byte_count = le32_to_cpu(
744 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700745 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
746 : srbcmd->sg.sg[i].count);
747 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
748 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 rcode = -EFAULT;
750 goto cleanup;
751
752 }
753 }
754 }
755
756 reply = (struct aac_srb_reply *) fib_data(srbfib);
757 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700758 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 rcode = -EFAULT;
760 goto cleanup;
761 }
762
763cleanup:
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700764 kfree(user_srbcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 for(i=0; i <= sg_indx; i++){
766 kfree(sg_list[i]);
767 }
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700768 if (rcode != -EINTR) {
769 aac_fib_complete(srbfib);
770 aac_fib_free(srbfib);
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return rcode;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776struct aac_pci_info {
777 u32 bus;
778 u32 slot;
779};
780
781
Adrian Bunk 48338692005-04-25 19:45:58 -0700782static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct aac_pci_info pci_info;
785
786 pci_info.bus = dev->pdev->bus->number;
787 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
788
789 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700790 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return -EFAULT;
792 }
793 return 0;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796
797int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
798{
799 int status;
800
801 /*
802 * HBA gets first crack
803 */
804
805 status = aac_dev_ioctl(dev, cmd, arg);
806 if(status != -ENOTTY)
807 return status;
808
809 switch (cmd) {
810 case FSACTL_MINIPORT_REV_CHECK:
811 status = check_revision(dev, arg);
812 break;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700813 case FSACTL_SEND_LARGE_FIB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 case FSACTL_SENDFIB:
815 status = ioctl_send_fib(dev, arg);
816 break;
817 case FSACTL_OPEN_GET_ADAPTER_FIB:
818 status = open_getadapter_fib(dev, arg);
819 break;
820 case FSACTL_GET_NEXT_ADAPTER_FIB:
821 status = next_getadapter_fib(dev, arg);
822 break;
823 case FSACTL_CLOSE_GET_ADAPTER_FIB:
824 status = close_getadapter_fib(dev, arg);
825 break;
826 case FSACTL_SEND_RAW_SRB:
827 status = aac_send_raw_srb(dev,arg);
828 break;
829 case FSACTL_GET_PCI_INFO:
830 status = aac_get_pci_info(dev,arg);
831 break;
832 default:
833 status = -ENOTTY;
834 break;
835 }
836 return status;
837}
838