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