blob: b381b3718a98f5d484b1769fc4ef727f5fd4c0bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
Alan Coxfa195af2008-10-27 15:16:36 +00003 * (c) Copyright 2001 Red Hat Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07008 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Module Name:
26 * commctrl.c
27 *
28 * Abstract: Contains all routines for control of the AFA comm layer
29 *
30 */
31
32#include <linux/kernel.h>
33#include <linux/init.h>
34#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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>
Mark Haverkampc8f7b072006-08-03 08:02:24 -070041#include <linux/delay.h> /* ssleep prototype */
Mark Haverkampdc4adbf2006-03-27 09:44:26 -080042#include <linux/kthread.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040043#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
Mark Salyzyn09050712008-05-28 15:32:55 -040045#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "aacraid.h"
48
49/**
50 * ioctl_send_fib - send a FIB from userspace
51 * @dev: adapter is being processed
52 * @arg: arguments to the ioctl call
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * This routine sends a fib to the adapter on behalf of a user level
55 * program.
56 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070057# define AAC_DEBUG_PREAMBLE KERN_INFO
58# define AAC_DEBUG_POSTAMBLE
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
61{
62 struct hw_fib * kfib;
63 struct fib *fibptr;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070064 struct hw_fib * hw_fib = (struct hw_fib *)0;
65 dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
66 unsigned size;
67 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Mark Haverkamp33bb3b22007-03-15 10:27:21 -070069 if (dev->in_reset) {
70 return -EBUSY;
71 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080072 fibptr = aac_fib_alloc(dev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070073 if(fibptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -ENOMEM;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070075 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080076
Mark Haverkampa8166a52007-03-15 10:26:22 -070077 kfib = fibptr->hw_fib_va;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /*
79 * First copy in the header so that we can check the size field.
80 */
81 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -080082 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return -EFAULT;
84 }
85 /*
86 * Since we copy based on the fib header size, make sure that we
87 * will not overrun the buffer when we copy the memory. Return
88 * an error if we would.
89 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -070090 size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
91 if (size < le16_to_cpu(kfib->header.SenderSize))
92 size = le16_to_cpu(kfib->header.SenderSize);
93 if (size > dev->max_fib_size) {
FUJITA Tomonorie9899112008-10-23 17:36:08 +090094 dma_addr_t daddr;
95
Mark Haverkamp6e289a92006-01-11 09:28:07 -080096 if (size > 2048) {
97 retval = -EINVAL;
98 goto cleanup;
99 }
FUJITA Tomonorie9899112008-10-23 17:36:08 +0900100
101 kfib = pci_alloc_consistent(dev->pdev, size, &daddr);
102 if (!kfib) {
103 retval = -ENOMEM;
104 goto cleanup;
105 }
106
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700107 /* Highjack the hw_fib */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700108 hw_fib = fibptr->hw_fib_va;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700109 hw_fib_pa = fibptr->hw_fib_pa;
FUJITA Tomonorie9899112008-10-23 17:36:08 +0900110 fibptr->hw_fib_va = kfib;
111 fibptr->hw_fib_pa = daddr;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700112 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
113 memcpy(kfib, hw_fib, dev->max_fib_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700116 if (copy_from_user(kfib, arg, size)) {
117 retval = -EFAULT;
118 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700121 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 aac_adapter_interrupt(dev);
123 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800124 * Since we didn't really send a fib, zero out the state to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * cleanup code not to assert.
126 */
127 kfib->header.XferState = 0;
128 } else {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800129 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 le16_to_cpu(kfib->header.Size) , FsaNormal,
131 1, 1, NULL, NULL);
132 if (retval) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700133 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800135 if (aac_fib_complete(fibptr) != 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700136 retval = -EINVAL;
137 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140 /*
141 * Make sure that the size returned by the adapter (which includes
142 * the header) is less than or equal to the size of a fib, so we
143 * don't corrupt application data. Then copy that size to the user
144 * buffer. (Don't try to add the header information again, since it
145 * was already included by the adapter.)
146 */
147
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700148 retval = 0;
149 if (copy_to_user(arg, (void *)kfib, size))
150 retval = -EFAULT;
151cleanup:
152 if (hw_fib) {
153 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
154 fibptr->hw_fib_pa = hw_fib_pa;
Mark Haverkampa8166a52007-03-15 10:26:22 -0700155 fibptr->hw_fib_va = hw_fib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530157 if (retval != -ERESTARTSYS)
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700158 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700159 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/**
163 * open_getadapter_fib - Get the next fib
164 *
165 * This routine will get the next Fib, if available, from the AdapterFibContext
166 * passed in from the user.
167 */
168
169static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
170{
171 struct aac_fib_context * fibctx;
172 int status;
173
174 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
175 if (fibctx == NULL) {
176 status = -ENOMEM;
177 } else {
178 unsigned long flags;
179 struct list_head * entry;
180 struct aac_fib_context * context;
181
182 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
183 fibctx->size = sizeof(struct aac_fib_context);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800184 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Yes yes, I know this could be an index, but we have a
186 * better guarantee of uniqueness for the locked loop below.
187 * Without the aid of a persistent history, this also helps
188 * reduce the chance that the opaque context would be reused.
189 */
190 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
191 /*
192 * Initialize the mutex used to wait for the next AIF.
193 */
Thomas Gleixner6de76cf2010-09-07 14:32:47 +0000194 sema_init(&fibctx->wait_sem, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 fibctx->wait = 0;
196 /*
197 * Initialize the fibs and set the count of fibs on
198 * the list to 0.
199 */
200 fibctx->count = 0;
201 INIT_LIST_HEAD(&fibctx->fib_list);
202 fibctx->jiffies = jiffies/HZ;
203 /*
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800204 * Now add this context onto the adapter's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * AdapterFibContext list.
206 */
207 spin_lock_irqsave(&dev->fib_lock, flags);
208 /* Ensure that we have a unique identifier */
209 entry = dev->fib_list.next;
210 while (entry != &dev->fib_list) {
211 context = list_entry(entry, struct aac_fib_context, next);
212 if (context->unique == fibctx->unique) {
213 /* Not unique (32 bits) */
214 fibctx->unique++;
215 entry = dev->fib_list.next;
216 } else {
217 entry = entry->next;
218 }
219 }
220 list_add_tail(&fibctx->next, &dev->fib_list);
221 spin_unlock_irqrestore(&dev->fib_lock, flags);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800222 if (copy_to_user(arg, &fibctx->unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sizeof(fibctx->unique))) {
224 status = -EFAULT;
225 } else {
226 status = 0;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 return status;
230}
231
232/**
233 * next_getadapter_fib - get the next fib
234 * @dev: adapter to use
235 * @arg: ioctl argument
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800236 *
237 * This routine will get the next Fib, if available, from the AdapterFibContext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * passed in from the user.
239 */
240
241static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
242{
243 struct fib_ioctl f;
244 struct fib *fib;
245 struct aac_fib_context *fibctx;
246 int status;
247 struct list_head * entry;
248 unsigned long flags;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
251 return -EFAULT;
252 /*
253 * Verify that the HANDLE passed in was a valid AdapterFibContext
254 *
255 * Search the list of AdapterFibContext addresses on the adapter
256 * to be sure this is a valid address
257 */
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800258 spin_lock_irqsave(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 entry = dev->fib_list.next;
260 fibctx = NULL;
261
262 while (entry != &dev->fib_list) {
263 fibctx = list_entry(entry, struct aac_fib_context, next);
264 /*
265 * Extract the AdapterFibContext from the Input parameters.
266 */
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800267 if (fibctx->unique == f.fibctx) { /* We found a winner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269 }
270 entry = entry->next;
271 fibctx = NULL;
272 }
273 if (!fibctx) {
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800274 spin_unlock_irqrestore(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dprintk ((KERN_INFO "Fib Context not found\n"));
276 return -EINVAL;
277 }
278
279 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
280 (fibctx->size != sizeof(struct aac_fib_context))) {
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800281 spin_unlock_irqrestore(&dev->fib_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
283 return -EINVAL;
284 }
285 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /*
287 * If there are no fibs to send back, then either wait or return
288 * -EAGAIN
289 */
290return_fib:
291 if (!list_empty(&fibctx->fib_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * Pull the next fib from the fibs
294 */
295 entry = fibctx->fib_list.next;
296 list_del(entry);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 fib = list_entry(entry, struct fib, fiblink);
299 fibctx->count--;
300 spin_unlock_irqrestore(&dev->fib_lock, flags);
Mark Haverkampa8166a52007-03-15 10:26:22 -0700301 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
302 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 kfree(fib);
304 return -EFAULT;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /*
307 * Free the space occupied by this copy of the fib.
308 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700309 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 kfree(fib);
311 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else {
313 spin_unlock_irqrestore(&dev->fib_lock, flags);
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800314 /* If someone killed the AIF aacraid thread, restart it */
315 status = !dev->aif_thread;
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700316 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800317 /* Be paranoid, be very paranoid! */
318 kthread_stop(dev->thread);
319 ssleep(1);
320 dev->aif_thread = 0;
Kees Cookf1701682013-07-03 15:04:58 -0700321 dev->thread = kthread_run(aac_command_thread, dev,
322 "%s", dev->name);
Mark Haverkampdc4adbf2006-03-27 09:44:26 -0800323 ssleep(1);
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (f.wait) {
326 if(down_interruptible(&fibctx->wait_sem) < 0) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530327 status = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 } else {
329 /* Lock again and retry */
330 spin_lock_irqsave(&dev->fib_lock, flags);
331 goto return_fib;
332 }
333 } else {
334 status = -EAGAIN;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Mark Haverkamp12a26d02005-08-03 15:39:25 -0700337 fibctx->jiffies = jiffies/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return status;
339}
340
341int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
342{
343 struct fib *fib;
344
345 /*
346 * First free any FIBs that have not been consumed.
347 */
348 while (!list_empty(&fibctx->fib_list)) {
349 struct list_head * entry;
350 /*
351 * Pull the next fib from the fibs
352 */
353 entry = fibctx->fib_list.next;
354 list_del(entry);
355 fib = list_entry(entry, struct fib, fiblink);
356 fibctx->count--;
357 /*
358 * Free the space occupied by this copy of the fib.
359 */
Mark Haverkampa8166a52007-03-15 10:26:22 -0700360 kfree(fib->hw_fib_va);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 kfree(fib);
362 }
363 /*
364 * Remove the Context from the AdapterFibContext List
365 */
366 list_del(&fibctx->next);
367 /*
368 * Invalidate context
369 */
370 fibctx->type = 0;
371 /*
372 * Free the space occupied by the Context
373 */
374 kfree(fibctx);
375 return 0;
376}
377
378/**
379 * close_getadapter_fib - close down user fib context
380 * @dev: adapter
381 * @arg: ioctl arguments
382 *
383 * This routine will close down the fibctx passed in from the user.
384 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
387{
388 struct aac_fib_context *fibctx;
389 int status;
390 unsigned long flags;
391 struct list_head * entry;
392
393 /*
394 * Verify that the HANDLE passed in was a valid AdapterFibContext
395 *
396 * Search the list of AdapterFibContext addresses on the adapter
397 * to be sure this is a valid address
398 */
399
400 entry = dev->fib_list.next;
401 fibctx = NULL;
402
403 while(entry != &dev->fib_list) {
404 fibctx = list_entry(entry, struct aac_fib_context, next);
405 /*
406 * Extract the fibctx from the input parameters
407 */
Al Viro142956a2007-10-29 05:11:28 +0000408 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 entry = entry->next;
411 fibctx = NULL;
412 }
413
414 if (!fibctx)
415 return 0; /* Already gone */
416
417 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
418 (fibctx->size != sizeof(struct aac_fib_context)))
419 return -EINVAL;
420 spin_lock_irqsave(&dev->fib_lock, flags);
421 status = aac_close_fib_context(dev, fibctx);
422 spin_unlock_irqrestore(&dev->fib_lock, flags);
423 return status;
424}
425
426/**
427 * check_revision - close down user fib context
428 * @dev: adapter
429 * @arg: ioctl arguments
430 *
431 * This routine returns the driver version.
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800432 * Under Linux, there have been no version incompatibilities, so this is
433 * simple!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435
436static int check_revision(struct aac_dev *dev, void __user *arg)
437{
438 struct revision response;
Mark Haverkampc7f47602005-08-03 15:38:55 -0700439 char *driver_version = aac_driver_version;
440 u32 version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Mark Haverkamp9f30a322005-10-24 10:52:02 -0700442 response.compat = 1;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800443 version = (simple_strtol(driver_version,
Mark Haverkampc7f47602005-08-03 15:38:55 -0700444 &driver_version, 10) << 24) | 0x00000400;
445 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
446 version += simple_strtol(driver_version + 1, NULL, 10);
447 response.version = cpu_to_le32(version);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800448# ifdef AAC_DRIVER_BUILD
Mark Haverkampc7f47602005-08-03 15:38:55 -0700449 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
450# else
451 response.build = cpu_to_le32(9999);
452# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (copy_to_user(arg, &response, sizeof(response)))
455 return -EFAULT;
456 return 0;
457}
458
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/**
461 *
462 * aac_send_raw_scb
463 *
464 */
465
Adrian Bunk 48338692005-04-25 19:45:58 -0700466static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct fib* srbfib;
469 int status;
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700470 struct aac_srb *srbcmd = NULL;
471 struct user_aac_srb *user_srbcmd = NULL;
472 struct user_aac_srb __user *user_srb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct aac_srb_reply __user *user_reply;
474 struct aac_srb_reply* reply;
475 u32 fibsize = 0;
476 u32 flags = 0;
477 s32 rcode = 0;
478 u32 data_dir;
479 void __user *sg_user[32];
480 void *sg_list[32];
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800481 u32 sg_indx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 u32 byte_count = 0;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700483 u32 actual_fibsize64, actual_fibsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int i;
485
486
Mark Haverkamp33bb3b22007-03-15 10:27:21 -0700487 if (dev->in_reset) {
488 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
489 return -EBUSY;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!capable(CAP_SYS_ADMIN)){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800492 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -EPERM;
494 }
495 /*
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700496 * Allocate and initialize a Fib then setup a SRB command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800498 if (!(srbfib = aac_fib_alloc(dev))) {
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700499 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800501 aac_fib_init(srbfib);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530502 /* raw_srb FIB is not FastResponseCapable */
503 srbfib->hw_fib_va->header.XferState &= ~cpu_to_le32(FastResponseCapable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 srbcmd = (struct aac_srb*) fib_data(srbfib);
506
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700507 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800509 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 rcode = -EFAULT;
511 goto cleanup;
512 }
513
Mahesh Rajashekharab4789b82013-10-31 14:01:02 +0530514 if ((fibsize < (sizeof(struct user_aac_srb) - sizeof(struct user_sgentry))) ||
515 (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 rcode = -EINVAL;
517 goto cleanup;
518 }
519
Dave Jones4645df12005-07-12 13:58:08 -0700520 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700521 if (!user_srbcmd) {
522 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
523 rcode = -ENOMEM;
524 goto cleanup;
525 }
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700526 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800527 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 rcode = -EFAULT;
529 goto cleanup;
530 }
531
532 user_reply = arg+fibsize;
533
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700534 flags = user_srbcmd->flags; /* from user in cpu order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 // Fix up srb for endian and force some values
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800538 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700539 srbcmd->id = cpu_to_le32(user_srbcmd->id);
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800540 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
541 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
542 srbcmd->flags = cpu_to_le32(flags);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700543 srbcmd->retry_limit = 0; // Obsolete parameter
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700544 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700545 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800546
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700547 switch (flags & (SRB_DataIn | SRB_DataOut)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 case SRB_DataOut:
549 data_dir = DMA_TO_DEVICE;
550 break;
551 case (SRB_DataIn | SRB_DataOut):
552 data_dir = DMA_BIDIRECTIONAL;
553 break;
554 case SRB_DataIn:
555 data_dir = DMA_FROM_DEVICE;
556 break;
557 default:
558 data_dir = DMA_NONE;
559 }
Tobias Klauser6391a112006-06-08 22:23:48 -0700560 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700561 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
562 le32_to_cpu(srbcmd->sg.count)));
563 rcode = -EINVAL;
564 goto cleanup;
565 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700566 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
567 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
568 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
569 (sizeof(struct sgentry64) - sizeof(struct sgentry));
570 /* User made a mistake - should not continue */
571 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
572 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
573 "Raw SRB command calculated fibsize=%lu;%lu "
574 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
575 "issued fibsize=%d\n",
576 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
577 sizeof(struct aac_srb), sizeof(struct sgentry),
578 sizeof(struct sgentry64), fibsize));
579 rcode = -EINVAL;
580 goto cleanup;
581 }
582 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
583 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
584 rcode = -EINVAL;
585 goto cleanup;
586 }
587 byte_count = 0;
588 if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700589 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
Mark Haverkamp84e29302005-07-07 13:40:00 -0700590 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
593 * This should also catch if user used the 32 bit sgmap
594 */
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700595 if (actual_fibsize64 == fibsize) {
596 actual_fibsize = actual_fibsize64;
597 for (i = 0; i < upsg->count; i++) {
598 u64 addr;
599 void* p;
Mark Salyzyn09050712008-05-28 15:32:55 -0400600 if (upsg->sg[i].count >
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530601 ((dev->adapter_info.options &
Mark Salyzyn09050712008-05-28 15:32:55 -0400602 AAC_OPT_NEW_COMM) ?
603 (dev->scsi_host_ptr->max_sectors << 9) :
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530604 65536)) {
Mark Salyzyn09050712008-05-28 15:32:55 -0400605 rcode = -EINVAL;
606 goto cleanup;
607 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700608 /* Does this really need to be GFP_DMA? */
609 p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800610 if(!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700611 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
612 upsg->sg[i].count,i,upsg->count));
613 rcode = -ENOMEM;
614 goto cleanup;
615 }
616 addr = (u64)upsg->sg[i].addr[0];
617 addr += ((u64)upsg->sg[i].addr[1]) << 32;
Al Viro142956a2007-10-29 05:11:28 +0000618 sg_user[i] = (void __user *)(uintptr_t)addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700619 sg_list[i] = p; // save so we can clean up later
620 sg_indx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800622 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700623 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
624 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
625 rcode = -EFAULT;
626 goto cleanup;
627 }
628 }
629 addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
630
631 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
632 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
633 byte_count += upsg->sg[i].count;
634 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
635 }
636 } else {
637 struct user_sgmap* usg;
Muhammad Falak R Wani22e9f5a2016-05-19 19:38:33 +0530638 usg = kmemdup(upsg,
639 actual_fibsize - sizeof(struct aac_srb)
640 + sizeof(struct sgmap), GFP_KERNEL);
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700641 if (!usg) {
642 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 rcode = -ENOMEM;
644 goto cleanup;
645 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700646 actual_fibsize = actual_fibsize64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700648 for (i = 0; i < usg->count; i++) {
649 u64 addr;
650 void* p;
Mark Salyzyn09050712008-05-28 15:32:55 -0400651 if (usg->sg[i].count >
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530652 ((dev->adapter_info.options &
Mark Salyzyn09050712008-05-28 15:32:55 -0400653 AAC_OPT_NEW_COMM) ?
654 (dev->scsi_host_ptr->max_sectors << 9) :
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530655 65536)) {
Jesper Juhl7dd72f52012-01-08 22:44:19 +0100656 kfree(usg);
Mark Salyzyn09050712008-05-28 15:32:55 -0400657 rcode = -EINVAL;
658 goto cleanup;
659 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700660 /* Does this really need to be GFP_DMA? */
661 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800662 if(!p) {
Julia Lawall8a52da62010-05-15 11:46:12 +0200663 dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700664 usg->sg[i].count,i,usg->count));
Julia Lawall8a52da62010-05-15 11:46:12 +0200665 kfree(usg);
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700666 rcode = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto cleanup;
668 }
Al Viro142956a2007-10-29 05:11:28 +0000669 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700670 sg_list[i] = p; // save so we can clean up later
671 sg_indx = i;
672
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800673 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700674 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
675 kfree (usg);
676 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
677 rcode = -EFAULT;
678 goto cleanup;
679 }
680 }
681 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
682
683 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
684 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
685 byte_count += usg->sg[i].count;
686 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700688 kfree (usg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 srbcmd->count = cpu_to_le32(byte_count);
Mahesh Rajashekhara2f5d1f72015-03-26 10:41:23 -0400691 if (user_srbcmd->sg.count)
692 psg->count = cpu_to_le32(sg_indx+1);
693 else
694 psg->count = 0;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800695 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 } else {
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700697 struct user_sgmap* upsg = &user_srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct sgmap* psg = &srbcmd->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700700 if (actual_fibsize64 == fibsize) {
701 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
702 for (i = 0; i < upsg->count; i++) {
Al Viro142956a2007-10-29 05:11:28 +0000703 uintptr_t addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700704 void* p;
Mark Salyzyn09050712008-05-28 15:32:55 -0400705 if (usg->sg[i].count >
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530706 ((dev->adapter_info.options &
Mark Salyzyn09050712008-05-28 15:32:55 -0400707 AAC_OPT_NEW_COMM) ?
708 (dev->scsi_host_ptr->max_sectors << 9) :
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530709 65536)) {
Mark Salyzyn09050712008-05-28 15:32:55 -0400710 rcode = -EINVAL;
711 goto cleanup;
712 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700713 /* Does this really need to be GFP_DMA? */
714 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800715 if(!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700716 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
717 usg->sg[i].count,i,usg->count));
718 rcode = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto cleanup;
720 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700721 addr = (u64)usg->sg[i].addr[0];
722 addr += ((u64)usg->sg[i].addr[1]) << 32;
Al Viro142956a2007-10-29 05:11:28 +0000723 sg_user[i] = (void __user *)addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700724 sg_list[i] = p; // save so we can clean up later
725 sg_indx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800727 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700728 if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
729 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
730 rcode = -EFAULT;
731 goto cleanup;
732 }
733 }
734 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
735
736 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
737 byte_count += usg->sg[i].count;
738 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
739 }
740 } else {
741 for (i = 0; i < upsg->count; i++) {
742 dma_addr_t addr;
743 void* p;
Mark Salyzyn09050712008-05-28 15:32:55 -0400744 if (upsg->sg[i].count >
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530745 ((dev->adapter_info.options &
Mark Salyzyn09050712008-05-28 15:32:55 -0400746 AAC_OPT_NEW_COMM) ?
747 (dev->scsi_host_ptr->max_sectors << 9) :
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530748 65536)) {
Mark Salyzyn09050712008-05-28 15:32:55 -0400749 rcode = -EINVAL;
750 goto cleanup;
751 }
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700752 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
Salyzyn, Mark6dcd4a72008-01-08 13:08:04 -0800753 if (!p) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700754 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
755 upsg->sg[i].count, i, upsg->count));
756 rcode = -ENOMEM;
757 goto cleanup;
758 }
Al Viro142956a2007-10-29 05:11:28 +0000759 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700760 sg_list[i] = p; // save so we can clean up later
761 sg_indx = i;
762
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800763 if (flags & SRB_DataOut) {
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700764 if(copy_from_user(p, sg_user[i],
765 upsg->sg[i].count)) {
766 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
767 rcode = -EFAULT;
768 goto cleanup;
769 }
770 }
771 addr = pci_map_single(dev->pdev, p,
772 upsg->sg[i].count, data_dir);
773
774 psg->sg[i].addr = cpu_to_le32(addr);
775 byte_count += upsg->sg[i].count;
776 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 srbcmd->count = cpu_to_le32(byte_count);
Mahesh Rajashekhara2f5d1f72015-03-26 10:41:23 -0400780 if (user_srbcmd->sg.count)
781 psg->count = cpu_to_le32(sg_indx+1);
782 else
783 psg->count = 0;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800784 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530786 if (status == -ERESTARTSYS) {
787 rcode = -ERESTARTSYS;
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700788 goto cleanup;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (status != 0){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800792 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
Mark Haverkamp5d497ce2005-06-17 13:38:04 -0700793 rcode = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto cleanup;
795 }
796
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800797 if (flags & SRB_DataIn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 for(i = 0 ; i <= sg_indx; i++){
Mark Haverkampf2b1a062007-03-15 10:27:32 -0700799 byte_count = le32_to_cpu(
800 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700801 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
802 : srbcmd->sg.sg[i].count);
803 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800804 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 rcode = -EFAULT;
806 goto cleanup;
807
808 }
809 }
810 }
811
812 reply = (struct aac_srb_reply *) fib_data(srbfib);
813 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800814 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 rcode = -EFAULT;
816 goto cleanup;
817 }
818
819cleanup:
Mark Haverkamp 56b58712005-04-27 06:05:51 -0700820 kfree(user_srbcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 for(i=0; i <= sg_indx; i++){
822 kfree(sg_list[i]);
823 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530824 if (rcode != -ERESTARTSYS) {
Mark Haverkampc8f7b072006-08-03 08:02:24 -0700825 aac_fib_complete(srbfib);
826 aac_fib_free(srbfib);
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 return rcode;
830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832struct aac_pci_info {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800833 u32 bus;
834 u32 slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835};
836
837
Adrian Bunk 48338692005-04-25 19:45:58 -0700838static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800840 struct aac_pci_info pci_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 pci_info.bus = dev->pdev->bus->number;
843 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
844
Salyzyn, Mark5234e252008-01-28 12:16:52 -0800845 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
846 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
847 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800849 return 0;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700850}
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
854{
855 int status;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800856
Raghava Aditya Renukunta222a9fb2016-02-03 15:06:05 -0800857 mutex_lock(&dev->ioctl_mutex);
858
Raghava Aditya Renukuntafbd185982016-02-03 15:06:06 -0800859 if (dev->adapter_shutdown) {
860 status = -EACCES;
861 goto cleanup;
862 }
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /*
865 * HBA gets first crack
866 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 status = aac_dev_ioctl(dev, cmd, arg);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530869 if (status != -ENOTTY)
Raghava Aditya Renukunta222a9fb2016-02-03 15:06:05 -0800870 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 switch (cmd) {
873 case FSACTL_MINIPORT_REV_CHECK:
874 status = check_revision(dev, arg);
875 break;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700876 case FSACTL_SEND_LARGE_FIB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 case FSACTL_SENDFIB:
878 status = ioctl_send_fib(dev, arg);
879 break;
880 case FSACTL_OPEN_GET_ADAPTER_FIB:
881 status = open_getadapter_fib(dev, arg);
882 break;
883 case FSACTL_GET_NEXT_ADAPTER_FIB:
884 status = next_getadapter_fib(dev, arg);
885 break;
886 case FSACTL_CLOSE_GET_ADAPTER_FIB:
887 status = close_getadapter_fib(dev, arg);
888 break;
889 case FSACTL_SEND_RAW_SRB:
890 status = aac_send_raw_srb(dev,arg);
891 break;
892 case FSACTL_GET_PCI_INFO:
893 status = aac_get_pci_info(dev,arg);
894 break;
895 default:
896 status = -ENOTTY;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Raghava Aditya Renukunta222a9fb2016-02-03 15:06:05 -0800899
900cleanup:
901 mutex_unlock(&dev->ioctl_mutex);
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return status;
904}
905