defconfig: exynos9610: Re-add dropped Wi-Fi AP options lost
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / scsi / aacraid / commctrl.c
CommitLineData
1da177e4
LT
1/*
2 * Adaptec AAC series RAID controller driver
fa195afe 3 * (c) Copyright 2001 Red Hat Inc.
1da177e4
LT
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
e8b12f0f 8 * Copyright (c) 2000-2010 Adaptec, Inc.
f4babba0
RAR
9 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
1da177e4
LT
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * Module Name:
27 * commctrl.c
28 *
29 * Abstract: Contains all routines for control of the AFA comm layer
30 *
31 */
32
33#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/types.h>
1da177e4
LT
36#include <linux/pci.h>
37#include <linux/spinlock.h>
38#include <linux/slab.h>
39#include <linux/completion.h>
40#include <linux/dma-mapping.h>
41#include <linux/blkdev.h>
c8f7b073 42#include <linux/delay.h> /* ssleep prototype */
dc4adbf4 43#include <linux/kthread.h>
6188e10d 44#include <linux/semaphore.h>
7c0f6ba6 45#include <linux/uaccess.h>
09050715 46#include <scsi/scsi_host.h>
1da177e4
LT
47
48#include "aacraid.h"
49
50/**
51 * ioctl_send_fib - send a FIB from userspace
52 * @dev: adapter is being processed
53 * @arg: arguments to the ioctl call
8ce3eca4 54 *
1da177e4
LT
55 * This routine sends a fib to the adapter on behalf of a user level
56 * program.
57 */
7c00ffa3
MH
58# define AAC_DEBUG_PREAMBLE KERN_INFO
59# define AAC_DEBUG_POSTAMBLE
8ce3eca4 60
1da177e4
LT
61static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
62{
63 struct hw_fib * kfib;
64 struct fib *fibptr;
7c00ffa3
MH
65 struct hw_fib * hw_fib = (struct hw_fib *)0;
66 dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
fa00c437 67 unsigned int size, osize;
7c00ffa3 68 int retval;
1da177e4 69
33bb3b29
MH
70 if (dev->in_reset) {
71 return -EBUSY;
72 }
bfb35aa8 73 fibptr = aac_fib_alloc(dev);
7c00ffa3 74 if(fibptr == NULL) {
1da177e4 75 return -ENOMEM;
7c00ffa3 76 }
8ce3eca4 77
a8166a52 78 kfib = fibptr->hw_fib_va;
1da177e4
LT
79 /*
80 * First copy in the header so that we can check the size field.
81 */
82 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
bfb35aa8 83 aac_fib_free(fibptr);
1da177e4
LT
84 return -EFAULT;
85 }
86 /*
87 * Since we copy based on the fib header size, make sure that we
88 * will not overrun the buffer when we copy the memory. Return
89 * an error if we would.
90 */
fa00c437
DC
91 osize = size = le16_to_cpu(kfib->header.Size) +
92 sizeof(struct aac_fibhdr);
7c00ffa3
MH
93 if (size < le16_to_cpu(kfib->header.SenderSize))
94 size = le16_to_cpu(kfib->header.SenderSize);
95 if (size > dev->max_fib_size) {
e9899113
FT
96 dma_addr_t daddr;
97
6e289a90
MH
98 if (size > 2048) {
99 retval = -EINVAL;
100 goto cleanup;
101 }
e9899113 102
f481973d
MR
103 kfib = dma_alloc_coherent(&dev->pdev->dev, size, &daddr,
104 GFP_KERNEL);
e9899113
FT
105 if (!kfib) {
106 retval = -ENOMEM;
107 goto cleanup;
108 }
109
7c00ffa3 110 /* Highjack the hw_fib */
a8166a52 111 hw_fib = fibptr->hw_fib_va;
7c00ffa3 112 hw_fib_pa = fibptr->hw_fib_pa;
e9899113
FT
113 fibptr->hw_fib_va = kfib;
114 fibptr->hw_fib_pa = daddr;
7c00ffa3
MH
115 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
116 memcpy(kfib, hw_fib, dev->max_fib_size);
1da177e4
LT
117 }
118
7c00ffa3
MH
119 if (copy_from_user(kfib, arg, size)) {
120 retval = -EFAULT;
121 goto cleanup;
1da177e4
LT
122 }
123
fa00c437
DC
124 /* Sanity check the second copy */
125 if ((osize != le16_to_cpu(kfib->header.Size) +
126 sizeof(struct aac_fibhdr))
127 || (size < le16_to_cpu(kfib->header.SenderSize))) {
128 retval = -EINVAL;
129 goto cleanup;
130 }
131
56b58712 132 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
1da177e4
LT
133 aac_adapter_interrupt(dev);
134 /*
8ce3eca4 135 * Since we didn't really send a fib, zero out the state to allow
1da177e4
LT
136 * cleanup code not to assert.
137 */
138 kfib->header.XferState = 0;
139 } else {
bfb35aa8 140 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
1da177e4
LT
141 le16_to_cpu(kfib->header.Size) , FsaNormal,
142 1, 1, NULL, NULL);
143 if (retval) {
7c00ffa3 144 goto cleanup;
1da177e4 145 }
bfb35aa8 146 if (aac_fib_complete(fibptr) != 0) {
7c00ffa3
MH
147 retval = -EINVAL;
148 goto cleanup;
1da177e4
LT
149 }
150 }
151 /*
152 * Make sure that the size returned by the adapter (which includes
153 * the header) is less than or equal to the size of a fib, so we
154 * don't corrupt application data. Then copy that size to the user
155 * buffer. (Don't try to add the header information again, since it
156 * was already included by the adapter.)
157 */
158
7c00ffa3
MH
159 retval = 0;
160 if (copy_to_user(arg, (void *)kfib, size))
161 retval = -EFAULT;
162cleanup:
163 if (hw_fib) {
f481973d
MR
164 dma_free_coherent(&dev->pdev->dev, size, kfib,
165 fibptr->hw_fib_pa);
7c00ffa3 166 fibptr->hw_fib_pa = hw_fib_pa;
a8166a52 167 fibptr->hw_fib_va = hw_fib;
1da177e4 168 }
cacb6dc3 169 if (retval != -ERESTARTSYS)
c8f7b073 170 aac_fib_free(fibptr);
7c00ffa3 171 return retval;
1da177e4
LT
172}
173
174/**
175 * open_getadapter_fib - Get the next fib
176 *
177 * This routine will get the next Fib, if available, from the AdapterFibContext
178 * passed in from the user.
179 */
180
181static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
182{
183 struct aac_fib_context * fibctx;
184 int status;
185
186 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
187 if (fibctx == NULL) {
188 status = -ENOMEM;
189 } else {
190 unsigned long flags;
191 struct list_head * entry;
192 struct aac_fib_context * context;
193
194 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
195 fibctx->size = sizeof(struct aac_fib_context);
8ce3eca4 196 /*
1da177e4
LT
197 * Yes yes, I know this could be an index, but we have a
198 * better guarantee of uniqueness for the locked loop below.
199 * Without the aid of a persistent history, this also helps
200 * reduce the chance that the opaque context would be reused.
201 */
202 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
203 /*
204 * Initialize the mutex used to wait for the next AIF.
205 */
6de76cfc 206 sema_init(&fibctx->wait_sem, 0);
1da177e4
LT
207 fibctx->wait = 0;
208 /*
209 * Initialize the fibs and set the count of fibs on
210 * the list to 0.
211 */
212 fibctx->count = 0;
213 INIT_LIST_HEAD(&fibctx->fib_list);
214 fibctx->jiffies = jiffies/HZ;
215 /*
8ce3eca4 216 * Now add this context onto the adapter's
1da177e4
LT
217 * AdapterFibContext list.
218 */
219 spin_lock_irqsave(&dev->fib_lock, flags);
220 /* Ensure that we have a unique identifier */
221 entry = dev->fib_list.next;
222 while (entry != &dev->fib_list) {
223 context = list_entry(entry, struct aac_fib_context, next);
224 if (context->unique == fibctx->unique) {
225 /* Not unique (32 bits) */
226 fibctx->unique++;
227 entry = dev->fib_list.next;
228 } else {
229 entry = entry->next;
230 }
231 }
232 list_add_tail(&fibctx->next, &dev->fib_list);
233 spin_unlock_irqrestore(&dev->fib_lock, flags);
8ce3eca4 234 if (copy_to_user(arg, &fibctx->unique,
1da177e4
LT
235 sizeof(fibctx->unique))) {
236 status = -EFAULT;
237 } else {
238 status = 0;
8ce3eca4 239 }
1da177e4
LT
240 }
241 return status;
242}
243
244/**
245 * next_getadapter_fib - get the next fib
246 * @dev: adapter to use
247 * @arg: ioctl argument
8ce3eca4
SM
248 *
249 * This routine will get the next Fib, if available, from the AdapterFibContext
1da177e4
LT
250 * passed in from the user.
251 */
252
253static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
254{
255 struct fib_ioctl f;
256 struct fib *fib;
257 struct aac_fib_context *fibctx;
258 int status;
259 struct list_head * entry;
260 unsigned long flags;
8ce3eca4 261
1da177e4
LT
262 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
263 return -EFAULT;
264 /*
265 * Verify that the HANDLE passed in was a valid AdapterFibContext
266 *
267 * Search the list of AdapterFibContext addresses on the adapter
268 * to be sure this is a valid address
269 */
5234e25c 270 spin_lock_irqsave(&dev->fib_lock, flags);
1da177e4
LT
271 entry = dev->fib_list.next;
272 fibctx = NULL;
273
274 while (entry != &dev->fib_list) {
275 fibctx = list_entry(entry, struct aac_fib_context, next);
276 /*
277 * Extract the AdapterFibContext from the Input parameters.
278 */
5234e25c 279 if (fibctx->unique == f.fibctx) { /* We found a winner */
1da177e4
LT
280 break;
281 }
282 entry = entry->next;
283 fibctx = NULL;
284 }
285 if (!fibctx) {
5234e25c 286 spin_unlock_irqrestore(&dev->fib_lock, flags);
1da177e4
LT
287 dprintk ((KERN_INFO "Fib Context not found\n"));
288 return -EINVAL;
289 }
290
291 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
292 (fibctx->size != sizeof(struct aac_fib_context))) {
5234e25c 293 spin_unlock_irqrestore(&dev->fib_lock, flags);
1da177e4
LT
294 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
295 return -EINVAL;
296 }
297 status = 0;
1da177e4
LT
298 /*
299 * If there are no fibs to send back, then either wait or return
300 * -EAGAIN
301 */
302return_fib:
303 if (!list_empty(&fibctx->fib_list)) {
1da177e4
LT
304 /*
305 * Pull the next fib from the fibs
306 */
307 entry = fibctx->fib_list.next;
308 list_del(entry);
8ce3eca4 309
1da177e4
LT
310 fib = list_entry(entry, struct fib, fiblink);
311 fibctx->count--;
312 spin_unlock_irqrestore(&dev->fib_lock, flags);
a8166a52
MH
313 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
314 kfree(fib->hw_fib_va);
1da177e4
LT
315 kfree(fib);
316 return -EFAULT;
8ce3eca4 317 }
1da177e4
LT
318 /*
319 * Free the space occupied by this copy of the fib.
320 */
a8166a52 321 kfree(fib->hw_fib_va);
1da177e4
LT
322 kfree(fib);
323 status = 0;
1da177e4
LT
324 } else {
325 spin_unlock_irqrestore(&dev->fib_lock, flags);
dc4adbf4
MH
326 /* If someone killed the AIF aacraid thread, restart it */
327 status = !dev->aif_thread;
8c867b25 328 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
dc4adbf4
MH
329 /* Be paranoid, be very paranoid! */
330 kthread_stop(dev->thread);
331 ssleep(1);
332 dev->aif_thread = 0;
f170168b
KC
333 dev->thread = kthread_run(aac_command_thread, dev,
334 "%s", dev->name);
dc4adbf4
MH
335 ssleep(1);
336 }
1da177e4
LT
337 if (f.wait) {
338 if(down_interruptible(&fibctx->wait_sem) < 0) {
cacb6dc3 339 status = -ERESTARTSYS;
1da177e4
LT
340 } else {
341 /* Lock again and retry */
342 spin_lock_irqsave(&dev->fib_lock, flags);
343 goto return_fib;
344 }
345 } else {
346 status = -EAGAIN;
8ce3eca4 347 }
1da177e4 348 }
12a26d08 349 fibctx->jiffies = jiffies/HZ;
1da177e4
LT
350 return status;
351}
352
353int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
354{
355 struct fib *fib;
356
357 /*
358 * First free any FIBs that have not been consumed.
359 */
360 while (!list_empty(&fibctx->fib_list)) {
361 struct list_head * entry;
362 /*
363 * Pull the next fib from the fibs
364 */
365 entry = fibctx->fib_list.next;
366 list_del(entry);
367 fib = list_entry(entry, struct fib, fiblink);
368 fibctx->count--;
369 /*
370 * Free the space occupied by this copy of the fib.
371 */
a8166a52 372 kfree(fib->hw_fib_va);
1da177e4
LT
373 kfree(fib);
374 }
375 /*
376 * Remove the Context from the AdapterFibContext List
377 */
378 list_del(&fibctx->next);
379 /*
380 * Invalidate context
381 */
382 fibctx->type = 0;
383 /*
384 * Free the space occupied by the Context
385 */
386 kfree(fibctx);
387 return 0;
388}
389
390/**
391 * close_getadapter_fib - close down user fib context
392 * @dev: adapter
393 * @arg: ioctl arguments
394 *
395 * This routine will close down the fibctx passed in from the user.
396 */
8ce3eca4 397
1da177e4
LT
398static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
399{
400 struct aac_fib_context *fibctx;
401 int status;
402 unsigned long flags;
403 struct list_head * entry;
404
405 /*
406 * Verify that the HANDLE passed in was a valid AdapterFibContext
407 *
408 * Search the list of AdapterFibContext addresses on the adapter
409 * to be sure this is a valid address
410 */
411
412 entry = dev->fib_list.next;
413 fibctx = NULL;
414
415 while(entry != &dev->fib_list) {
416 fibctx = list_entry(entry, struct aac_fib_context, next);
417 /*
418 * Extract the fibctx from the input parameters
419 */
142956af 420 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
1da177e4 421 break;
1da177e4
LT
422 entry = entry->next;
423 fibctx = NULL;
424 }
425
426 if (!fibctx)
427 return 0; /* Already gone */
428
429 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
430 (fibctx->size != sizeof(struct aac_fib_context)))
431 return -EINVAL;
432 spin_lock_irqsave(&dev->fib_lock, flags);
433 status = aac_close_fib_context(dev, fibctx);
434 spin_unlock_irqrestore(&dev->fib_lock, flags);
435 return status;
436}
437
438/**
439 * check_revision - close down user fib context
440 * @dev: adapter
441 * @arg: ioctl arguments
442 *
443 * This routine returns the driver version.
5234e25c
SM
444 * Under Linux, there have been no version incompatibilities, so this is
445 * simple!
1da177e4
LT
446 */
447
448static int check_revision(struct aac_dev *dev, void __user *arg)
449{
450 struct revision response;
c7f47602
MH
451 char *driver_version = aac_driver_version;
452 u32 version;
453
9f30a323 454 response.compat = 1;
8ce3eca4 455 version = (simple_strtol(driver_version,
c7f47602
MH
456 &driver_version, 10) << 24) | 0x00000400;
457 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
458 version += simple_strtol(driver_version + 1, NULL, 10);
459 response.version = cpu_to_le32(version);
8ce3eca4 460# ifdef AAC_DRIVER_BUILD
c7f47602
MH
461 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
462# else
463 response.build = cpu_to_le32(9999);
464# endif
1da177e4
LT
465
466 if (copy_to_user(arg, &response, sizeof(response)))
467 return -EFAULT;
468 return 0;
469}
470
7c00ffa3 471
1da177e4
LT
472/**
473 *
474 * aac_send_raw_scb
475 *
476 */
477
4833869e 478static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
1da177e4
LT
479{
480 struct fib* srbfib;
481 int status;
56b58712 482 struct aac_srb *srbcmd = NULL;
423400e6 483 struct aac_hba_cmd_req *hbacmd = NULL;
56b58712
MH
484 struct user_aac_srb *user_srbcmd = NULL;
485 struct user_aac_srb __user *user_srb = arg;
1da177e4 486 struct aac_srb_reply __user *user_reply;
423400e6 487 u32 chn;
1da177e4
LT
488 u32 fibsize = 0;
489 u32 flags = 0;
490 s32 rcode = 0;
491 u32 data_dir;
423400e6
RAR
492 void __user *sg_user[HBA_MAX_SG_EMBEDDED];
493 void *sg_list[HBA_MAX_SG_EMBEDDED];
494 u32 sg_count[HBA_MAX_SG_EMBEDDED];
5234e25c 495 u32 sg_indx = 0;
1da177e4 496 u32 byte_count = 0;
f2b1a06a 497 u32 actual_fibsize64, actual_fibsize = 0;
1da177e4 498 int i;
423400e6
RAR
499 int is_native_device;
500 u64 address;
1da177e4
LT
501
502
33bb3b29
MH
503 if (dev->in_reset) {
504 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
505 return -EBUSY;
506 }
1da177e4 507 if (!capable(CAP_SYS_ADMIN)){
8ce3eca4 508 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
1da177e4
LT
509 return -EPERM;
510 }
511 /*
f2b1a06a 512 * Allocate and initialize a Fib then setup a SRB command
1da177e4 513 */
bfb35aa8 514 if (!(srbfib = aac_fib_alloc(dev))) {
5d497cec 515 return -ENOMEM;
1da177e4 516 }
1da177e4 517
7c00ffa3 518 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
1da177e4 519 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
8ce3eca4 520 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
1da177e4
LT
521 rcode = -EFAULT;
522 goto cleanup;
523 }
524
b4789b8e
MR
525 if ((fibsize < (sizeof(struct user_aac_srb) - sizeof(struct user_sgentry))) ||
526 (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))) {
1da177e4
LT
527 rcode = -EINVAL;
528 goto cleanup;
529 }
530
4645df10 531 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
7c00ffa3
MH
532 if (!user_srbcmd) {
533 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
534 rcode = -ENOMEM;
535 goto cleanup;
536 }
56b58712 537 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
8ce3eca4 538 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
1da177e4
LT
539 rcode = -EFAULT;
540 goto cleanup;
541 }
542
56b58712 543 flags = user_srbcmd->flags; /* from user in cpu order */
56b58712 544 switch (flags & (SRB_DataIn | SRB_DataOut)) {
1da177e4
LT
545 case SRB_DataOut:
546 data_dir = DMA_TO_DEVICE;
547 break;
548 case (SRB_DataIn | SRB_DataOut):
549 data_dir = DMA_BIDIRECTIONAL;
550 break;
551 case SRB_DataIn:
552 data_dir = DMA_FROM_DEVICE;
553 break;
554 default:
555 data_dir = DMA_NONE;
556 }
6391a113 557 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
7c00ffa3 558 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
423400e6
RAR
559 user_srbcmd->sg.count));
560 rcode = -EINVAL;
561 goto cleanup;
562 }
563 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
564 dprintk((KERN_DEBUG"aacraid:SG with no direction specified\n"));
7c00ffa3
MH
565 rcode = -EINVAL;
566 goto cleanup;
567 }
f2b1a06a
MH
568 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
569 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
570 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
571 (sizeof(struct sgentry64) - sizeof(struct sgentry));
572 /* User made a mistake - should not continue */
573 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
574 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
575 "Raw SRB command calculated fibsize=%lu;%lu "
576 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
577 "issued fibsize=%d\n",
578 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
579 sizeof(struct aac_srb), sizeof(struct sgentry),
580 sizeof(struct sgentry64), fibsize));
581 rcode = -EINVAL;
582 goto cleanup;
583 }
423400e6 584
f3ef4a74 585 chn = user_srbcmd->channel;
423400e6
RAR
586 if (chn < AAC_MAX_BUSES && user_srbcmd->id < AAC_MAX_TARGETS &&
587 dev->hba_map[chn][user_srbcmd->id].devtype ==
588 AAC_DEVTYPE_NATIVE_RAW) {
589 is_native_device = 1;
590 hbacmd = (struct aac_hba_cmd_req *)srbfib->hw_fib_va;
591 memset(hbacmd, 0, 96); /* sizeof(*hbacmd) is not necessary */
592
593 /* iu_type is a parameter of aac_hba_send */
594 switch (data_dir) {
595 case DMA_TO_DEVICE:
596 hbacmd->byte1 = 2;
597 break;
598 case DMA_FROM_DEVICE:
599 case DMA_BIDIRECTIONAL:
600 hbacmd->byte1 = 1;
601 break;
602 case DMA_NONE:
603 default:
604 break;
605 }
606 hbacmd->lun[1] = cpu_to_le32(user_srbcmd->lun);
607 hbacmd->it_nexus = dev->hba_map[chn][user_srbcmd->id].rmw_nexus;
608
609 /*
610 * we fill in reply_qid later in aac_src_deliver_message
611 * we fill in iu_type, request_id later in aac_hba_send
612 * we fill in emb_data_desc_count, data_length later
613 * in sg list build
614 */
615
616 memcpy(hbacmd->cdb, user_srbcmd->cdb, sizeof(hbacmd->cdb));
617
618 address = (u64)srbfib->hw_error_pa;
619 hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
620 hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
621 hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
622 hbacmd->emb_data_desc_count =
623 cpu_to_le32(user_srbcmd->sg.count);
624 srbfib->hbacmd_size = 64 +
625 user_srbcmd->sg.count * sizeof(struct aac_hba_sgl);
626
627 } else {
628 is_native_device = 0;
629 aac_fib_init(srbfib);
630
631 /* raw_srb FIB is not FastResponseCapable */
632 srbfib->hw_fib_va->header.XferState &=
633 ~cpu_to_le32(FastResponseCapable);
634
635 srbcmd = (struct aac_srb *) fib_data(srbfib);
636
637 // Fix up srb for endian and force some values
638
639 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
640 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
641 srbcmd->id = cpu_to_le32(user_srbcmd->id);
642 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
643 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
644 srbcmd->flags = cpu_to_le32(flags);
645 srbcmd->retry_limit = 0; // Obsolete parameter
646 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
647 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
f2b1a06a 648 }
423400e6 649
f2b1a06a 650 byte_count = 0;
423400e6
RAR
651 if (is_native_device) {
652 struct user_sgmap *usg32 = &user_srbcmd->sg;
653 struct user_sgmap64 *usg64 =
654 (struct user_sgmap64 *)&user_srbcmd->sg;
655
656 for (i = 0; i < usg32->count; i++) {
657 void *p;
658 u64 addr;
659
660 sg_count[i] = (actual_fibsize64 == fibsize) ?
661 usg64->sg[i].count : usg32->sg[i].count;
662 if (sg_count[i] >
663 (dev->scsi_host_ptr->max_sectors << 9)) {
664 pr_err("aacraid: upsg->sg[%d].count=%u>%u\n",
665 i, sg_count[i],
666 dev->scsi_host_ptr->max_sectors << 9);
667 rcode = -EINVAL;
668 goto cleanup;
669 }
670
c831a4a0 671 p = kmalloc(sg_count[i], GFP_KERNEL);
423400e6
RAR
672 if (!p) {
673 rcode = -ENOMEM;
674 goto cleanup;
675 }
676
677 if (actual_fibsize64 == fibsize) {
678 addr = (u64)usg64->sg[i].addr[0];
679 addr += ((u64)usg64->sg[i].addr[1]) << 32;
680 } else {
681 addr = (u64)usg32->sg[i].addr;
682 }
683
684 sg_user[i] = (void __user *)(uintptr_t)addr;
685 sg_list[i] = p; // save so we can clean up later
686 sg_indx = i;
687
688 if (flags & SRB_DataOut) {
689 if (copy_from_user(p, sg_user[i],
690 sg_count[i])) {
691 rcode = -EFAULT;
692 goto cleanup;
693 }
694 }
695 addr = pci_map_single(dev->pdev, p, sg_count[i],
696 data_dir);
697 hbacmd->sge[i].addr_hi = cpu_to_le32((u32)(addr>>32));
698 hbacmd->sge[i].addr_lo = cpu_to_le32(
699 (u32)(addr & 0xffffffff));
700 hbacmd->sge[i].len = cpu_to_le32(sg_count[i]);
701 hbacmd->sge[i].flags = 0;
702 byte_count += sg_count[i];
703 }
704
705 if (usg32->count > 0) /* embedded sglist */
706 hbacmd->sge[usg32->count-1].flags =
707 cpu_to_le32(0x40000000);
708 hbacmd->data_length = cpu_to_le32(byte_count);
709
710 status = aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, srbfib,
711 NULL, NULL);
712
713 } else if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
56b58712 714 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
84e29308 715 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
1da177e4
LT
716
717 /*
718 * This should also catch if user used the 32 bit sgmap
719 */
f2b1a06a
MH
720 if (actual_fibsize64 == fibsize) {
721 actual_fibsize = actual_fibsize64;
722 for (i = 0; i < upsg->count; i++) {
723 u64 addr;
724 void* p;
423400e6
RAR
725
726 sg_count[i] = upsg->sg[i].count;
727 if (sg_count[i] >
cacb6dc3 728 ((dev->adapter_info.options &
09050715
MS
729 AAC_OPT_NEW_COMM) ?
730 (dev->scsi_host_ptr->max_sectors << 9) :
cacb6dc3 731 65536)) {
09050715
MS
732 rcode = -EINVAL;
733 goto cleanup;
734 }
c831a4a0
RAR
735
736 p = kmalloc(sg_count[i], GFP_KERNEL);
6dcd4a7f 737 if(!p) {
f2b1a06a 738 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
423400e6 739 sg_count[i], i, upsg->count));
f2b1a06a
MH
740 rcode = -ENOMEM;
741 goto cleanup;
742 }
743 addr = (u64)upsg->sg[i].addr[0];
744 addr += ((u64)upsg->sg[i].addr[1]) << 32;
142956af 745 sg_user[i] = (void __user *)(uintptr_t)addr;
f2b1a06a
MH
746 sg_list[i] = p; // save so we can clean up later
747 sg_indx = i;
748
8ce3eca4 749 if (flags & SRB_DataOut) {
423400e6
RAR
750 if (copy_from_user(p, sg_user[i],
751 sg_count[i])){
f2b1a06a
MH
752 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
753 rcode = -EFAULT;
754 goto cleanup;
755 }
756 }
423400e6
RAR
757 addr = pci_map_single(dev->pdev, p,
758 sg_count[i], data_dir);
1da177e4 759
f2b1a06a
MH
760 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
761 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
423400e6
RAR
762 byte_count += sg_count[i];
763 psg->sg[i].count = cpu_to_le32(sg_count[i]);
f2b1a06a
MH
764 }
765 } else {
766 struct user_sgmap* usg;
22e9f5a6
MFW
767 usg = kmemdup(upsg,
768 actual_fibsize - sizeof(struct aac_srb)
769 + sizeof(struct sgmap), GFP_KERNEL);
f2b1a06a
MH
770 if (!usg) {
771 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
1da177e4
LT
772 rcode = -ENOMEM;
773 goto cleanup;
774 }
f2b1a06a
MH
775 actual_fibsize = actual_fibsize64;
776
777 for (i = 0; i < usg->count; i++) {
778 u64 addr;
779 void* p;
423400e6
RAR
780
781 sg_count[i] = usg->sg[i].count;
782 if (sg_count[i] >
cacb6dc3 783 ((dev->adapter_info.options &
09050715
MS
784 AAC_OPT_NEW_COMM) ?
785 (dev->scsi_host_ptr->max_sectors << 9) :
cacb6dc3 786 65536)) {
7dd72f51 787 kfree(usg);
09050715
MS
788 rcode = -EINVAL;
789 goto cleanup;
790 }
c831a4a0
RAR
791
792 p = kmalloc(sg_count[i], GFP_KERNEL);
6dcd4a7f 793 if(!p) {
8a52da63 794 dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
423400e6 795 sg_count[i], i, usg->count));
8a52da63 796 kfree(usg);
f2b1a06a 797 rcode = -ENOMEM;
1da177e4
LT
798 goto cleanup;
799 }
142956af 800 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
f2b1a06a
MH
801 sg_list[i] = p; // save so we can clean up later
802 sg_indx = i;
803
8ce3eca4 804 if (flags & SRB_DataOut) {
423400e6
RAR
805 if (copy_from_user(p, sg_user[i],
806 sg_count[i])) {
f2b1a06a
MH
807 kfree (usg);
808 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
809 rcode = -EFAULT;
810 goto cleanup;
811 }
812 }
423400e6
RAR
813 addr = pci_map_single(dev->pdev, p,
814 sg_count[i], data_dir);
1da177e4 815
f2b1a06a
MH
816 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
817 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
423400e6
RAR
818 byte_count += sg_count[i];
819 psg->sg[i].count = cpu_to_le32(sg_count[i]);
f2b1a06a
MH
820 }
821 kfree (usg);
1da177e4 822 }
1da177e4 823 srbcmd->count = cpu_to_le32(byte_count);
2f5d1f79
MR
824 if (user_srbcmd->sg.count)
825 psg->count = cpu_to_le32(sg_indx+1);
826 else
827 psg->count = 0;
bfb35aa8 828 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
1da177e4 829 } else {
56b58712 830 struct user_sgmap* upsg = &user_srbcmd->sg;
1da177e4 831 struct sgmap* psg = &srbcmd->sg;
f2b1a06a
MH
832
833 if (actual_fibsize64 == fibsize) {
834 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
835 for (i = 0; i < upsg->count; i++) {
142956af 836 uintptr_t addr;
f2b1a06a 837 void* p;
423400e6
RAR
838
839 sg_count[i] = usg->sg[i].count;
840 if (sg_count[i] >
cacb6dc3 841 ((dev->adapter_info.options &
09050715
MS
842 AAC_OPT_NEW_COMM) ?
843 (dev->scsi_host_ptr->max_sectors << 9) :
cacb6dc3 844 65536)) {
09050715
MS
845 rcode = -EINVAL;
846 goto cleanup;
847 }
c831a4a0 848 p = kmalloc(sg_count[i], GFP_KERNEL|GFP_DMA32);
423400e6 849 if (!p) {
f2b1a06a 850 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
423400e6 851 sg_count[i], i, usg->count));
f2b1a06a 852 rcode = -ENOMEM;
1da177e4
LT
853 goto cleanup;
854 }
f2b1a06a
MH
855 addr = (u64)usg->sg[i].addr[0];
856 addr += ((u64)usg->sg[i].addr[1]) << 32;
142956af 857 sg_user[i] = (void __user *)addr;
f2b1a06a
MH
858 sg_list[i] = p; // save so we can clean up later
859 sg_indx = i;
860
8ce3eca4 861 if (flags & SRB_DataOut) {
423400e6
RAR
862 if (copy_from_user(p, sg_user[i],
863 sg_count[i])){
f2b1a06a
MH
864 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
865 rcode = -EFAULT;
866 goto cleanup;
867 }
868 }
869 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
870
871 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
872 byte_count += usg->sg[i].count;
423400e6 873 psg->sg[i].count = cpu_to_le32(sg_count[i]);
1da177e4 874 }
f2b1a06a
MH
875 } else {
876 for (i = 0; i < upsg->count; i++) {
877 dma_addr_t addr;
878 void* p;
423400e6
RAR
879
880 sg_count[i] = upsg->sg[i].count;
881 if (sg_count[i] >
cacb6dc3 882 ((dev->adapter_info.options &
09050715
MS
883 AAC_OPT_NEW_COMM) ?
884 (dev->scsi_host_ptr->max_sectors << 9) :
cacb6dc3 885 65536)) {
09050715
MS
886 rcode = -EINVAL;
887 goto cleanup;
888 }
c831a4a0 889 p = kmalloc(sg_count[i], GFP_KERNEL|GFP_DMA32);
6dcd4a7f 890 if (!p) {
f2b1a06a 891 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
423400e6 892 sg_count[i], i, upsg->count));
f2b1a06a
MH
893 rcode = -ENOMEM;
894 goto cleanup;
895 }
142956af 896 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
f2b1a06a
MH
897 sg_list[i] = p; // save so we can clean up later
898 sg_indx = i;
899
8ce3eca4 900 if (flags & SRB_DataOut) {
423400e6
RAR
901 if (copy_from_user(p, sg_user[i],
902 sg_count[i])) {
f2b1a06a
MH
903 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
904 rcode = -EFAULT;
905 goto cleanup;
906 }
907 }
908 addr = pci_map_single(dev->pdev, p,
423400e6 909 sg_count[i], data_dir);
1da177e4 910
f2b1a06a 911 psg->sg[i].addr = cpu_to_le32(addr);
423400e6
RAR
912 byte_count += sg_count[i];
913 psg->sg[i].count = cpu_to_le32(sg_count[i]);
f2b1a06a 914 }
1da177e4
LT
915 }
916 srbcmd->count = cpu_to_le32(byte_count);
2f5d1f79
MR
917 if (user_srbcmd->sg.count)
918 psg->count = cpu_to_le32(sg_indx+1);
919 else
920 psg->count = 0;
bfb35aa8 921 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
1da177e4 922 }
423400e6 923
cacb6dc3
PNRCEH
924 if (status == -ERESTARTSYS) {
925 rcode = -ERESTARTSYS;
c8f7b073
MH
926 goto cleanup;
927 }
1da177e4 928
423400e6 929 if (status != 0) {
8ce3eca4 930 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
5d497cec 931 rcode = -ENXIO;
1da177e4
LT
932 goto cleanup;
933 }
934
8ce3eca4 935 if (flags & SRB_DataIn) {
1da177e4 936 for(i = 0 ; i <= sg_indx; i++){
423400e6 937 if (copy_to_user(sg_user[i], sg_list[i], sg_count[i])) {
8ce3eca4 938 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
1da177e4
LT
939 rcode = -EFAULT;
940 goto cleanup;
941
942 }
943 }
944 }
945
423400e6
RAR
946 user_reply = arg + fibsize;
947 if (is_native_device) {
948 struct aac_hba_resp *err =
949 &((struct aac_native_hba *)srbfib->hw_fib_va)->resp.err;
950 struct aac_srb_reply reply;
951
342ffc26 952 memset(&reply, 0, sizeof(reply));
423400e6
RAR
953 reply.status = ST_OK;
954 if (srbfib->flags & FIB_CONTEXT_FLAG_FASTRESP) {
955 /* fast response */
956 reply.srb_status = SRB_STATUS_SUCCESS;
957 reply.scsi_status = 0;
958 reply.data_xfer_length = byte_count;
5cc973f0
CIK
959 reply.sense_data_size = 0;
960 memset(reply.sense_data, 0, AAC_SENSE_BUFFERSIZE);
423400e6
RAR
961 } else {
962 reply.srb_status = err->service_response;
963 reply.scsi_status = err->status;
964 reply.data_xfer_length = byte_count -
965 le32_to_cpu(err->residual_count);
966 reply.sense_data_size = err->sense_response_data_len;
967 memcpy(reply.sense_data, err->sense_response_buf,
968 AAC_SENSE_BUFFERSIZE);
969 }
970 if (copy_to_user(user_reply, &reply,
971 sizeof(struct aac_srb_reply))) {
972 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n"));
973 rcode = -EFAULT;
974 goto cleanup;
975 }
976 } else {
977 struct aac_srb_reply *reply;
978
979 reply = (struct aac_srb_reply *) fib_data(srbfib);
980 if (copy_to_user(user_reply, reply,
981 sizeof(struct aac_srb_reply))) {
982 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n"));
983 rcode = -EFAULT;
984 goto cleanup;
985 }
1da177e4
LT
986 }
987
988cleanup:
56b58712 989 kfree(user_srbcmd);
cacb6dc3 990 if (rcode != -ERESTARTSYS) {
423400e6
RAR
991 for (i = 0; i <= sg_indx; i++)
992 kfree(sg_list[i]);
c8f7b073
MH
993 aac_fib_complete(srbfib);
994 aac_fib_free(srbfib);
995 }
1da177e4
LT
996
997 return rcode;
998}
999
1da177e4 1000struct aac_pci_info {
8ce3eca4
SM
1001 u32 bus;
1002 u32 slot;
1da177e4
LT
1003};
1004
1005
4833869e 1006static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
1da177e4 1007{
8ce3eca4 1008 struct aac_pci_info pci_info;
1da177e4
LT
1009
1010 pci_info.bus = dev->pdev->bus->number;
1011 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
1012
5234e25c
SM
1013 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
1014 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
1015 return -EFAULT;
1da177e4 1016 }
8ce3eca4 1017 return 0;
7c00ffa3 1018}
c799d519
RAR
1019
1020static int aac_get_hba_info(struct aac_dev *dev, void __user *arg)
1021{
1022 struct aac_hba_info hbainfo;
1023
342ffc26 1024 memset(&hbainfo, 0, sizeof(hbainfo));
c799d519
RAR
1025 hbainfo.adapter_number = (u8) dev->id;
1026 hbainfo.system_io_bus_number = dev->pdev->bus->number;
1027 hbainfo.device_number = (dev->pdev->devfn >> 3);
1028 hbainfo.function_number = (dev->pdev->devfn & 0x0007);
1029
1030 hbainfo.vendor_id = dev->pdev->vendor;
1031 hbainfo.device_id = dev->pdev->device;
1032 hbainfo.sub_vendor_id = dev->pdev->subsystem_vendor;
1033 hbainfo.sub_system_id = dev->pdev->subsystem_device;
1034
1035 if (copy_to_user(arg, &hbainfo, sizeof(struct aac_hba_info))) {
1036 dprintk((KERN_DEBUG "aacraid: Could not copy hba info\n"));
1037 return -EFAULT;
1038 }
1039
1040 return 0;
1041}
1042
09867a0e
RAR
1043struct aac_reset_iop {
1044 u8 reset_type;
1045};
1046
1047static int aac_send_reset_adapter(struct aac_dev *dev, void __user *arg)
1048{
1049 struct aac_reset_iop reset;
1050 int retval;
8ce3eca4 1051
09867a0e
RAR
1052 if (copy_from_user((void *)&reset, arg, sizeof(struct aac_reset_iop)))
1053 return -EFAULT;
1054
1055 retval = aac_reset_adapter(dev, 0, reset.reset_type);
1056 return retval;
1057
1058}
1da177e4
LT
1059
1060int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
1061{
1062 int status;
8ce3eca4 1063
222a9fb3
RAR
1064 mutex_lock(&dev->ioctl_mutex);
1065
fbd18598
RAR
1066 if (dev->adapter_shutdown) {
1067 status = -EACCES;
1068 goto cleanup;
1069 }
1070
1da177e4
LT
1071 /*
1072 * HBA gets first crack
1073 */
8ce3eca4 1074
1da177e4 1075 status = aac_dev_ioctl(dev, cmd, arg);
cacb6dc3 1076 if (status != -ENOTTY)
222a9fb3 1077 goto cleanup;
1da177e4
LT
1078
1079 switch (cmd) {
1080 case FSACTL_MINIPORT_REV_CHECK:
1081 status = check_revision(dev, arg);
1082 break;
7c00ffa3 1083 case FSACTL_SEND_LARGE_FIB:
1da177e4
LT
1084 case FSACTL_SENDFIB:
1085 status = ioctl_send_fib(dev, arg);
1086 break;
1087 case FSACTL_OPEN_GET_ADAPTER_FIB:
1088 status = open_getadapter_fib(dev, arg);
1089 break;
1090 case FSACTL_GET_NEXT_ADAPTER_FIB:
1091 status = next_getadapter_fib(dev, arg);
1092 break;
1093 case FSACTL_CLOSE_GET_ADAPTER_FIB:
1094 status = close_getadapter_fib(dev, arg);
1095 break;
1096 case FSACTL_SEND_RAW_SRB:
1097 status = aac_send_raw_srb(dev,arg);
1098 break;
1099 case FSACTL_GET_PCI_INFO:
1100 status = aac_get_pci_info(dev,arg);
1101 break;
c799d519
RAR
1102 case FSACTL_GET_HBA_INFO:
1103 status = aac_get_hba_info(dev, arg);
1104 break;
09867a0e
RAR
1105 case FSACTL_RESET_IOP:
1106 status = aac_send_reset_adapter(dev, arg);
1107 break;
1108
1da177e4
LT
1109 default:
1110 status = -ENOTTY;
8ce3eca4 1111 break;
1da177e4 1112 }
222a9fb3
RAR
1113
1114cleanup:
1115 mutex_unlock(&dev->ioctl_mutex);
1116
1da177e4
LT
1117 return status;
1118}
1119