2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc.
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
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)
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.
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.
29 * Abstract: Contains all routines for control of the AFA comm layer
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
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>
42 #include <linux/delay.h> /* ssleep prototype */
43 #include <linux/kthread.h>
44 #include <linux/uaccess.h>
45 #include <scsi/scsi_host.h>
50 * ioctl_send_fib - send a FIB from userspace
51 * @dev: adapter is being processed
52 * @arg: arguments to the ioctl call
54 * This routine sends a fib to the adapter on behalf of a user level
57 # define AAC_DEBUG_PREAMBLE KERN_INFO
58 # define AAC_DEBUG_POSTAMBLE
60 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
64 struct hw_fib * hw_fib = (struct hw_fib *)0;
65 dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
66 unsigned int size, osize;
72 fibptr = aac_fib_alloc(dev);
77 kfib = fibptr->hw_fib_va;
79 * First copy in the header so that we can check the size field.
81 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
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.
90 osize = size = le16_to_cpu(kfib->header.Size) +
91 sizeof(struct aac_fibhdr);
92 if (size < le16_to_cpu(kfib->header.SenderSize))
93 size = le16_to_cpu(kfib->header.SenderSize);
94 if (size > dev->max_fib_size) {
102 kfib = dma_alloc_coherent(&dev->pdev->dev, size, &daddr,
109 /* Highjack the hw_fib */
110 hw_fib = fibptr->hw_fib_va;
111 hw_fib_pa = fibptr->hw_fib_pa;
112 fibptr->hw_fib_va = kfib;
113 fibptr->hw_fib_pa = daddr;
114 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
115 memcpy(kfib, hw_fib, dev->max_fib_size);
118 if (copy_from_user(kfib, arg, size)) {
123 /* Sanity check the second copy */
124 if ((osize != le16_to_cpu(kfib->header.Size) +
125 sizeof(struct aac_fibhdr))
126 || (size < le16_to_cpu(kfib->header.SenderSize))) {
131 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
132 aac_adapter_interrupt(dev);
134 * Since we didn't really send a fib, zero out the state to allow
135 * cleanup code not to assert.
137 kfib->header.XferState = 0;
139 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
140 le16_to_cpu(kfib->header.Size) , FsaNormal,
145 if (aac_fib_complete(fibptr) != 0) {
151 * Make sure that the size returned by the adapter (which includes
152 * the header) is less than or equal to the size of a fib, so we
153 * don't corrupt application data. Then copy that size to the user
154 * buffer. (Don't try to add the header information again, since it
155 * was already included by the adapter.)
159 if (copy_to_user(arg, (void *)kfib, size))
163 dma_free_coherent(&dev->pdev->dev, size, kfib,
165 fibptr->hw_fib_pa = hw_fib_pa;
166 fibptr->hw_fib_va = hw_fib;
168 if (retval != -ERESTARTSYS)
169 aac_fib_free(fibptr);
174 * open_getadapter_fib - Get the next fib
176 * This routine will get the next Fib, if available, from the AdapterFibContext
177 * passed in from the user.
180 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
182 struct aac_fib_context * fibctx;
185 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
186 if (fibctx == NULL) {
190 struct list_head * entry;
191 struct aac_fib_context * context;
193 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
194 fibctx->size = sizeof(struct aac_fib_context);
196 * Yes yes, I know this could be an index, but we have a
197 * better guarantee of uniqueness for the locked loop below.
198 * Without the aid of a persistent history, this also helps
199 * reduce the chance that the opaque context would be reused.
201 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
203 * Initialize the mutex used to wait for the next AIF.
205 init_completion(&fibctx->completion);
208 * Initialize the fibs and set the count of fibs on
212 INIT_LIST_HEAD(&fibctx->fib_list);
213 fibctx->jiffies = jiffies/HZ;
215 * Now add this context onto the adapter's
216 * AdapterFibContext list.
218 spin_lock_irqsave(&dev->fib_lock, flags);
219 /* Ensure that we have a unique identifier */
220 entry = dev->fib_list.next;
221 while (entry != &dev->fib_list) {
222 context = list_entry(entry, struct aac_fib_context, next);
223 if (context->unique == fibctx->unique) {
224 /* Not unique (32 bits) */
226 entry = dev->fib_list.next;
231 list_add_tail(&fibctx->next, &dev->fib_list);
232 spin_unlock_irqrestore(&dev->fib_lock, flags);
233 if (copy_to_user(arg, &fibctx->unique,
234 sizeof(fibctx->unique))) {
244 * next_getadapter_fib - get the next fib
245 * @dev: adapter to use
246 * @arg: ioctl argument
248 * This routine will get the next Fib, if available, from the AdapterFibContext
249 * passed in from the user.
252 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
256 struct aac_fib_context *fibctx;
258 struct list_head * entry;
261 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
264 * Verify that the HANDLE passed in was a valid AdapterFibContext
266 * Search the list of AdapterFibContext addresses on the adapter
267 * to be sure this is a valid address
269 spin_lock_irqsave(&dev->fib_lock, flags);
270 entry = dev->fib_list.next;
273 while (entry != &dev->fib_list) {
274 fibctx = list_entry(entry, struct aac_fib_context, next);
276 * Extract the AdapterFibContext from the Input parameters.
278 if (fibctx->unique == f.fibctx) { /* We found a winner */
285 spin_unlock_irqrestore(&dev->fib_lock, flags);
286 dprintk ((KERN_INFO "Fib Context not found\n"));
290 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
291 (fibctx->size != sizeof(struct aac_fib_context))) {
292 spin_unlock_irqrestore(&dev->fib_lock, flags);
293 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
298 * If there are no fibs to send back, then either wait or return
302 if (!list_empty(&fibctx->fib_list)) {
304 * Pull the next fib from the fibs
306 entry = fibctx->fib_list.next;
309 fib = list_entry(entry, struct fib, fiblink);
311 spin_unlock_irqrestore(&dev->fib_lock, flags);
312 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
313 kfree(fib->hw_fib_va);
318 * Free the space occupied by this copy of the fib.
320 kfree(fib->hw_fib_va);
324 spin_unlock_irqrestore(&dev->fib_lock, flags);
325 /* If someone killed the AIF aacraid thread, restart it */
326 status = !dev->aif_thread;
327 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
328 /* Be paranoid, be very paranoid! */
329 kthread_stop(dev->thread);
332 dev->thread = kthread_run(aac_command_thread, dev,
337 if (wait_for_completion_interruptible(&fibctx->completion) < 0) {
338 status = -ERESTARTSYS;
340 /* Lock again and retry */
341 spin_lock_irqsave(&dev->fib_lock, flags);
348 fibctx->jiffies = jiffies/HZ;
352 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
357 * First free any FIBs that have not been consumed.
359 while (!list_empty(&fibctx->fib_list)) {
360 struct list_head * entry;
362 * Pull the next fib from the fibs
364 entry = fibctx->fib_list.next;
366 fib = list_entry(entry, struct fib, fiblink);
369 * Free the space occupied by this copy of the fib.
371 kfree(fib->hw_fib_va);
375 * Remove the Context from the AdapterFibContext List
377 list_del(&fibctx->next);
383 * Free the space occupied by the Context
390 * close_getadapter_fib - close down user fib context
392 * @arg: ioctl arguments
394 * This routine will close down the fibctx passed in from the user.
397 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
399 struct aac_fib_context *fibctx;
402 struct list_head * entry;
405 * Verify that the HANDLE passed in was a valid AdapterFibContext
407 * Search the list of AdapterFibContext addresses on the adapter
408 * to be sure this is a valid address
411 entry = dev->fib_list.next;
414 while(entry != &dev->fib_list) {
415 fibctx = list_entry(entry, struct aac_fib_context, next);
417 * Extract the fibctx from the input parameters
419 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
426 return 0; /* Already gone */
428 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
429 (fibctx->size != sizeof(struct aac_fib_context)))
431 spin_lock_irqsave(&dev->fib_lock, flags);
432 status = aac_close_fib_context(dev, fibctx);
433 spin_unlock_irqrestore(&dev->fib_lock, flags);
438 * check_revision - close down user fib context
440 * @arg: ioctl arguments
442 * This routine returns the driver version.
443 * Under Linux, there have been no version incompatibilities, so this is
447 static int check_revision(struct aac_dev *dev, void __user *arg)
449 struct revision response;
450 char *driver_version = aac_driver_version;
454 version = (simple_strtol(driver_version,
455 &driver_version, 10) << 24) | 0x00000400;
456 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
457 version += simple_strtol(driver_version + 1, NULL, 10);
458 response.version = cpu_to_le32(version);
459 # ifdef AAC_DRIVER_BUILD
460 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
462 response.build = cpu_to_le32(9999);
465 if (copy_to_user(arg, &response, sizeof(response)))
477 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
481 struct aac_srb *srbcmd = NULL;
482 struct aac_hba_cmd_req *hbacmd = NULL;
483 struct user_aac_srb *user_srbcmd = NULL;
484 struct user_aac_srb __user *user_srb = arg;
485 struct aac_srb_reply __user *user_reply;
491 void __user *sg_user[HBA_MAX_SG_EMBEDDED];
492 void *sg_list[HBA_MAX_SG_EMBEDDED];
493 u32 sg_count[HBA_MAX_SG_EMBEDDED];
496 u32 actual_fibsize64, actual_fibsize = 0;
498 int is_native_device;
503 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
506 if (!capable(CAP_SYS_ADMIN)){
507 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
511 * Allocate and initialize a Fib then setup a SRB command
513 if (!(srbfib = aac_fib_alloc(dev))) {
517 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
518 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
519 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
524 if ((fibsize < (sizeof(struct user_aac_srb) - sizeof(struct user_sgentry))) ||
525 (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))) {
530 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
532 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
536 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
537 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
542 flags = user_srbcmd->flags; /* from user in cpu order */
543 switch (flags & (SRB_DataIn | SRB_DataOut)) {
545 data_dir = DMA_TO_DEVICE;
547 case (SRB_DataIn | SRB_DataOut):
548 data_dir = DMA_BIDIRECTIONAL;
551 data_dir = DMA_FROM_DEVICE;
556 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
557 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
558 user_srbcmd->sg.count));
562 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
563 dprintk((KERN_DEBUG"aacraid:SG with no direction specified\n"));
567 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
568 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
569 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
570 (sizeof(struct sgentry64) - sizeof(struct sgentry));
571 /* User made a mistake - should not continue */
572 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
573 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
574 "Raw SRB command calculated fibsize=%lu;%lu "
575 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
576 "issued fibsize=%d\n",
577 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
578 sizeof(struct aac_srb), sizeof(struct sgentry),
579 sizeof(struct sgentry64), fibsize));
584 chn = user_srbcmd->channel;
585 if (chn < AAC_MAX_BUSES && user_srbcmd->id < AAC_MAX_TARGETS &&
586 dev->hba_map[chn][user_srbcmd->id].devtype ==
587 AAC_DEVTYPE_NATIVE_RAW) {
588 is_native_device = 1;
589 hbacmd = (struct aac_hba_cmd_req *)srbfib->hw_fib_va;
590 memset(hbacmd, 0, 96); /* sizeof(*hbacmd) is not necessary */
592 /* iu_type is a parameter of aac_hba_send */
597 case DMA_FROM_DEVICE:
598 case DMA_BIDIRECTIONAL:
605 hbacmd->lun[1] = cpu_to_le32(user_srbcmd->lun);
606 hbacmd->it_nexus = dev->hba_map[chn][user_srbcmd->id].rmw_nexus;
609 * we fill in reply_qid later in aac_src_deliver_message
610 * we fill in iu_type, request_id later in aac_hba_send
611 * we fill in emb_data_desc_count, data_length later
615 memcpy(hbacmd->cdb, user_srbcmd->cdb, sizeof(hbacmd->cdb));
617 address = (u64)srbfib->hw_error_pa;
618 hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
619 hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
620 hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
621 hbacmd->emb_data_desc_count =
622 cpu_to_le32(user_srbcmd->sg.count);
623 srbfib->hbacmd_size = 64 +
624 user_srbcmd->sg.count * sizeof(struct aac_hba_sgl);
627 is_native_device = 0;
628 aac_fib_init(srbfib);
630 /* raw_srb FIB is not FastResponseCapable */
631 srbfib->hw_fib_va->header.XferState &=
632 ~cpu_to_le32(FastResponseCapable);
634 srbcmd = (struct aac_srb *) fib_data(srbfib);
636 // Fix up srb for endian and force some values
638 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
639 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
640 srbcmd->id = cpu_to_le32(user_srbcmd->id);
641 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
642 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
643 srbcmd->flags = cpu_to_le32(flags);
644 srbcmd->retry_limit = 0; // Obsolete parameter
645 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
646 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
650 if (is_native_device) {
651 struct user_sgmap *usg32 = &user_srbcmd->sg;
652 struct user_sgmap64 *usg64 =
653 (struct user_sgmap64 *)&user_srbcmd->sg;
655 for (i = 0; i < usg32->count; i++) {
659 sg_count[i] = (actual_fibsize64 == fibsize) ?
660 usg64->sg[i].count : usg32->sg[i].count;
662 (dev->scsi_host_ptr->max_sectors << 9)) {
663 pr_err("aacraid: upsg->sg[%d].count=%u>%u\n",
665 dev->scsi_host_ptr->max_sectors << 9);
670 p = kmalloc(sg_count[i], GFP_KERNEL);
676 if (actual_fibsize64 == fibsize) {
677 addr = (u64)usg64->sg[i].addr[0];
678 addr += ((u64)usg64->sg[i].addr[1]) << 32;
680 addr = (u64)usg32->sg[i].addr;
683 sg_user[i] = (void __user *)(uintptr_t)addr;
684 sg_list[i] = p; // save so we can clean up later
687 if (flags & SRB_DataOut) {
688 if (copy_from_user(p, sg_user[i],
694 addr = pci_map_single(dev->pdev, p, sg_count[i],
696 hbacmd->sge[i].addr_hi = cpu_to_le32((u32)(addr>>32));
697 hbacmd->sge[i].addr_lo = cpu_to_le32(
698 (u32)(addr & 0xffffffff));
699 hbacmd->sge[i].len = cpu_to_le32(sg_count[i]);
700 hbacmd->sge[i].flags = 0;
701 byte_count += sg_count[i];
704 if (usg32->count > 0) /* embedded sglist */
705 hbacmd->sge[usg32->count-1].flags =
706 cpu_to_le32(0x40000000);
707 hbacmd->data_length = cpu_to_le32(byte_count);
709 status = aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, srbfib,
712 } else if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
713 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
714 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
717 * This should also catch if user used the 32 bit sgmap
719 if (actual_fibsize64 == fibsize) {
720 actual_fibsize = actual_fibsize64;
721 for (i = 0; i < upsg->count; i++) {
725 sg_count[i] = upsg->sg[i].count;
727 ((dev->adapter_info.options &
729 (dev->scsi_host_ptr->max_sectors << 9) :
735 p = kmalloc(sg_count[i], GFP_KERNEL);
737 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
738 sg_count[i], i, upsg->count));
742 addr = (u64)upsg->sg[i].addr[0];
743 addr += ((u64)upsg->sg[i].addr[1]) << 32;
744 sg_user[i] = (void __user *)(uintptr_t)addr;
745 sg_list[i] = p; // save so we can clean up later
748 if (flags & SRB_DataOut) {
749 if (copy_from_user(p, sg_user[i],
751 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
756 addr = pci_map_single(dev->pdev, p,
757 sg_count[i], data_dir);
759 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
760 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
761 byte_count += sg_count[i];
762 psg->sg[i].count = cpu_to_le32(sg_count[i]);
765 struct user_sgmap* usg;
767 actual_fibsize - sizeof(struct aac_srb)
768 + sizeof(struct sgmap), GFP_KERNEL);
770 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
774 actual_fibsize = actual_fibsize64;
776 for (i = 0; i < usg->count; i++) {
780 sg_count[i] = usg->sg[i].count;
782 ((dev->adapter_info.options &
784 (dev->scsi_host_ptr->max_sectors << 9) :
791 p = kmalloc(sg_count[i], GFP_KERNEL);
793 dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
794 sg_count[i], i, usg->count));
799 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
800 sg_list[i] = p; // save so we can clean up later
803 if (flags & SRB_DataOut) {
804 if (copy_from_user(p, sg_user[i],
807 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
812 addr = pci_map_single(dev->pdev, p,
813 sg_count[i], data_dir);
815 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
816 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
817 byte_count += sg_count[i];
818 psg->sg[i].count = cpu_to_le32(sg_count[i]);
822 srbcmd->count = cpu_to_le32(byte_count);
823 if (user_srbcmd->sg.count)
824 psg->count = cpu_to_le32(sg_indx+1);
827 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
829 struct user_sgmap* upsg = &user_srbcmd->sg;
830 struct sgmap* psg = &srbcmd->sg;
832 if (actual_fibsize64 == fibsize) {
833 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
834 for (i = 0; i < upsg->count; i++) {
838 sg_count[i] = usg->sg[i].count;
840 ((dev->adapter_info.options &
842 (dev->scsi_host_ptr->max_sectors << 9) :
847 p = kmalloc(sg_count[i], GFP_KERNEL);
849 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
850 sg_count[i], i, usg->count));
854 addr = (u64)usg->sg[i].addr[0];
855 addr += ((u64)usg->sg[i].addr[1]) << 32;
856 sg_user[i] = (void __user *)addr;
857 sg_list[i] = p; // save so we can clean up later
860 if (flags & SRB_DataOut) {
861 if (copy_from_user(p, sg_user[i],
863 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
868 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
870 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
871 byte_count += usg->sg[i].count;
872 psg->sg[i].count = cpu_to_le32(sg_count[i]);
875 for (i = 0; i < upsg->count; i++) {
879 sg_count[i] = upsg->sg[i].count;
881 ((dev->adapter_info.options &
883 (dev->scsi_host_ptr->max_sectors << 9) :
888 p = kmalloc(sg_count[i], GFP_KERNEL);
890 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
891 sg_count[i], i, upsg->count));
895 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
896 sg_list[i] = p; // save so we can clean up later
899 if (flags & SRB_DataOut) {
900 if (copy_from_user(p, sg_user[i],
902 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
907 addr = pci_map_single(dev->pdev, p,
908 sg_count[i], data_dir);
910 psg->sg[i].addr = cpu_to_le32(addr);
911 byte_count += sg_count[i];
912 psg->sg[i].count = cpu_to_le32(sg_count[i]);
915 srbcmd->count = cpu_to_le32(byte_count);
916 if (user_srbcmd->sg.count)
917 psg->count = cpu_to_le32(sg_indx+1);
920 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
923 if (status == -ERESTARTSYS) {
924 rcode = -ERESTARTSYS;
929 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
934 if (flags & SRB_DataIn) {
935 for(i = 0 ; i <= sg_indx; i++){
936 if (copy_to_user(sg_user[i], sg_list[i], sg_count[i])) {
937 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
945 user_reply = arg + fibsize;
946 if (is_native_device) {
947 struct aac_hba_resp *err =
948 &((struct aac_native_hba *)srbfib->hw_fib_va)->resp.err;
949 struct aac_srb_reply reply;
951 memset(&reply, 0, sizeof(reply));
952 reply.status = ST_OK;
953 if (srbfib->flags & FIB_CONTEXT_FLAG_FASTRESP) {
955 reply.srb_status = SRB_STATUS_SUCCESS;
956 reply.scsi_status = 0;
957 reply.data_xfer_length = byte_count;
958 reply.sense_data_size = 0;
959 memset(reply.sense_data, 0, AAC_SENSE_BUFFERSIZE);
961 reply.srb_status = err->service_response;
962 reply.scsi_status = err->status;
963 reply.data_xfer_length = byte_count -
964 le32_to_cpu(err->residual_count);
965 reply.sense_data_size = err->sense_response_data_len;
966 memcpy(reply.sense_data, err->sense_response_buf,
967 AAC_SENSE_BUFFERSIZE);
969 if (copy_to_user(user_reply, &reply,
970 sizeof(struct aac_srb_reply))) {
971 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n"));
976 struct aac_srb_reply *reply;
978 reply = (struct aac_srb_reply *) fib_data(srbfib);
979 if (copy_to_user(user_reply, reply,
980 sizeof(struct aac_srb_reply))) {
981 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n"));
989 if (rcode != -ERESTARTSYS) {
990 for (i = 0; i <= sg_indx; i++)
992 aac_fib_complete(srbfib);
993 aac_fib_free(srbfib);
999 struct aac_pci_info {
1005 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
1007 struct aac_pci_info pci_info;
1009 pci_info.bus = dev->pdev->bus->number;
1010 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
1012 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
1013 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
1019 static int aac_get_hba_info(struct aac_dev *dev, void __user *arg)
1021 struct aac_hba_info hbainfo;
1023 memset(&hbainfo, 0, sizeof(hbainfo));
1024 hbainfo.adapter_number = (u8) dev->id;
1025 hbainfo.system_io_bus_number = dev->pdev->bus->number;
1026 hbainfo.device_number = (dev->pdev->devfn >> 3);
1027 hbainfo.function_number = (dev->pdev->devfn & 0x0007);
1029 hbainfo.vendor_id = dev->pdev->vendor;
1030 hbainfo.device_id = dev->pdev->device;
1031 hbainfo.sub_vendor_id = dev->pdev->subsystem_vendor;
1032 hbainfo.sub_system_id = dev->pdev->subsystem_device;
1034 if (copy_to_user(arg, &hbainfo, sizeof(struct aac_hba_info))) {
1035 dprintk((KERN_DEBUG "aacraid: Could not copy hba info\n"));
1042 struct aac_reset_iop {
1046 static int aac_send_reset_adapter(struct aac_dev *dev, void __user *arg)
1048 struct aac_reset_iop reset;
1051 if (copy_from_user((void *)&reset, arg, sizeof(struct aac_reset_iop)))
1054 dev->adapter_shutdown = 1;
1056 mutex_unlock(&dev->ioctl_mutex);
1057 retval = aac_reset_adapter(dev, 0, reset.reset_type);
1058 mutex_lock(&dev->ioctl_mutex);
1063 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
1067 mutex_lock(&dev->ioctl_mutex);
1069 if (dev->adapter_shutdown) {
1075 * HBA gets first crack
1078 status = aac_dev_ioctl(dev, cmd, arg);
1079 if (status != -ENOTTY)
1083 case FSACTL_MINIPORT_REV_CHECK:
1084 status = check_revision(dev, arg);
1086 case FSACTL_SEND_LARGE_FIB:
1087 case FSACTL_SENDFIB:
1088 status = ioctl_send_fib(dev, arg);
1090 case FSACTL_OPEN_GET_ADAPTER_FIB:
1091 status = open_getadapter_fib(dev, arg);
1093 case FSACTL_GET_NEXT_ADAPTER_FIB:
1094 status = next_getadapter_fib(dev, arg);
1096 case FSACTL_CLOSE_GET_ADAPTER_FIB:
1097 status = close_getadapter_fib(dev, arg);
1099 case FSACTL_SEND_RAW_SRB:
1100 status = aac_send_raw_srb(dev,arg);
1102 case FSACTL_GET_PCI_INFO:
1103 status = aac_get_pci_info(dev,arg);
1105 case FSACTL_GET_HBA_INFO:
1106 status = aac_get_hba_info(dev, arg);
1108 case FSACTL_RESET_IOP:
1109 status = aac_send_reset_adapter(dev, arg);
1118 mutex_unlock(&dev->ioctl_mutex);