1 /* 2 * Adaptec AAC series RAID controller driver 3 * (c) Copyright 2001 Red Hat Inc. 4 * 5 * based on the old aacraid driver that is.. 6 * Adaptec aacraid device driver for Linux. 7 * 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) 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> 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> 46 47 #include "aacraid.h" 48 49 /** 50 * ioctl_send_fib - send a FIB from userspace 51 * @dev: adapter is being processed 52 * @arg: arguments to the ioctl call 53 * 54 * This routine sends a fib to the adapter on behalf of a user level 55 * program. 56 */ 57 # define AAC_DEBUG_PREAMBLE KERN_INFO 58 # define AAC_DEBUG_POSTAMBLE 59 60 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg) 61 { 62 struct hw_fib * kfib; 63 struct fib *fibptr; 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; 67 int retval; 68 69 if (dev->in_reset) { 70 return -EBUSY; 71 } 72 fibptr = aac_fib_alloc(dev); 73 if(fibptr == NULL) { 74 return -ENOMEM; 75 } 76 77 kfib = fibptr->hw_fib_va; 78 /* 79 * First copy in the header so that we can check the size field. 80 */ 81 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) { 82 aac_fib_free(fibptr); 83 return -EFAULT; 84 } 85 /* 86 * Since we copy based on the fib header size, make sure that we 87 * will not overrun the buffer when we copy the memory. Return 88 * an error if we would. 89 */ 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) { 95 dma_addr_t daddr; 96 97 if (size > 2048) { 98 retval = -EINVAL; 99 goto cleanup; 100 } 101 102 kfib = dma_alloc_coherent(&dev->pdev->dev, size, &daddr, 103 GFP_KERNEL); 104 if (!kfib) { 105 retval = -ENOMEM; 106 goto cleanup; 107 } 108 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); 116 } 117 118 if (copy_from_user(kfib, arg, size)) { 119 retval = -EFAULT; 120 goto cleanup; 121 } 122 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))) { 127 retval = -EINVAL; 128 goto cleanup; 129 } 130 131 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) { 132 aac_adapter_interrupt(dev); 133 /* 134 * Since we didn't really send a fib, zero out the state to allow 135 * cleanup code not to assert. 136 */ 137 kfib->header.XferState = 0; 138 } else { 139 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr, 140 le16_to_cpu(kfib->header.Size) , FsaNormal, 141 1, 1, NULL, NULL); 142 if (retval) { 143 goto cleanup; 144 } 145 if (aac_fib_complete(fibptr) != 0) { 146 retval = -EINVAL; 147 goto cleanup; 148 } 149 } 150 /* 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.) 156 */ 157 158 retval = 0; 159 if (copy_to_user(arg, (void *)kfib, size)) 160 retval = -EFAULT; 161 cleanup: 162 if (hw_fib) { 163 dma_free_coherent(&dev->pdev->dev, size, kfib, 164 fibptr->hw_fib_pa); 165 fibptr->hw_fib_pa = hw_fib_pa; 166 fibptr->hw_fib_va = hw_fib; 167 } 168 if (retval != -ERESTARTSYS) 169 aac_fib_free(fibptr); 170 return retval; 171 } 172 173 /** 174 * open_getadapter_fib - Get the next fib 175 * 176 * This routine will get the next Fib, if available, from the AdapterFibContext 177 * passed in from the user. 178 */ 179 180 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg) 181 { 182 struct aac_fib_context * fibctx; 183 int status; 184 185 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL); 186 if (fibctx == NULL) { 187 status = -ENOMEM; 188 } else { 189 unsigned long flags; 190 struct list_head * entry; 191 struct aac_fib_context * context; 192 193 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT; 194 fibctx->size = sizeof(struct aac_fib_context); 195 /* 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. 200 */ 201 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF); 202 /* 203 * Initialize the mutex used to wait for the next AIF. 204 */ 205 init_completion(&fibctx->completion); 206 fibctx->wait = 0; 207 /* 208 * Initialize the fibs and set the count of fibs on 209 * the list to 0. 210 */ 211 fibctx->count = 0; 212 INIT_LIST_HEAD(&fibctx->fib_list); 213 fibctx->jiffies = jiffies/HZ; 214 /* 215 * Now add this context onto the adapter's 216 * AdapterFibContext list. 217 */ 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) */ 225 fibctx->unique++; 226 entry = dev->fib_list.next; 227 } else { 228 entry = entry->next; 229 } 230 } 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))) { 235 status = -EFAULT; 236 } else { 237 status = 0; 238 } 239 } 240 return status; 241 } 242 243 /** 244 * next_getadapter_fib - get the next fib 245 * @dev: adapter to use 246 * @arg: ioctl argument 247 * 248 * This routine will get the next Fib, if available, from the AdapterFibContext 249 * passed in from the user. 250 */ 251 252 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg) 253 { 254 struct fib_ioctl f; 255 struct fib *fib; 256 struct aac_fib_context *fibctx; 257 int status; 258 struct list_head * entry; 259 unsigned long flags; 260 261 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl))) 262 return -EFAULT; 263 /* 264 * Verify that the HANDLE passed in was a valid AdapterFibContext 265 * 266 * Search the list of AdapterFibContext addresses on the adapter 267 * to be sure this is a valid address 268 */ 269 spin_lock_irqsave(&dev->fib_lock, flags); 270 entry = dev->fib_list.next; 271 fibctx = NULL; 272 273 while (entry != &dev->fib_list) { 274 fibctx = list_entry(entry, struct aac_fib_context, next); 275 /* 276 * Extract the AdapterFibContext from the Input parameters. 277 */ 278 if (fibctx->unique == f.fibctx) { /* We found a winner */ 279 break; 280 } 281 entry = entry->next; 282 fibctx = NULL; 283 } 284 if (!fibctx) { 285 spin_unlock_irqrestore(&dev->fib_lock, flags); 286 dprintk ((KERN_INFO "Fib Context not found\n")); 287 return -EINVAL; 288 } 289 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")); 294 return -EINVAL; 295 } 296 status = 0; 297 /* 298 * If there are no fibs to send back, then either wait or return 299 * -EAGAIN 300 */ 301 return_fib: 302 if (!list_empty(&fibctx->fib_list)) { 303 /* 304 * Pull the next fib from the fibs 305 */ 306 entry = fibctx->fib_list.next; 307 list_del(entry); 308 309 fib = list_entry(entry, struct fib, fiblink); 310 fibctx->count--; 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); 314 kfree(fib); 315 return -EFAULT; 316 } 317 /* 318 * Free the space occupied by this copy of the fib. 319 */ 320 kfree(fib->hw_fib_va); 321 kfree(fib); 322 status = 0; 323 } else { 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); 330 ssleep(1); 331 dev->aif_thread = 0; 332 dev->thread = kthread_run(aac_command_thread, dev, 333 "%s", dev->name); 334 ssleep(1); 335 } 336 if (f.wait) { 337 if (wait_for_completion_interruptible(&fibctx->completion) < 0) { 338 status = -ERESTARTSYS; 339 } else { 340 /* Lock again and retry */ 341 spin_lock_irqsave(&dev->fib_lock, flags); 342 goto return_fib; 343 } 344 } else { 345 status = -EAGAIN; 346 } 347 } 348 fibctx->jiffies = jiffies/HZ; 349 return status; 350 } 351 352 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx) 353 { 354 struct fib *fib; 355 356 /* 357 * First free any FIBs that have not been consumed. 358 */ 359 while (!list_empty(&fibctx->fib_list)) { 360 struct list_head * entry; 361 /* 362 * Pull the next fib from the fibs 363 */ 364 entry = fibctx->fib_list.next; 365 list_del(entry); 366 fib = list_entry(entry, struct fib, fiblink); 367 fibctx->count--; 368 /* 369 * Free the space occupied by this copy of the fib. 370 */ 371 kfree(fib->hw_fib_va); 372 kfree(fib); 373 } 374 /* 375 * Remove the Context from the AdapterFibContext List 376 */ 377 list_del(&fibctx->next); 378 /* 379 * Invalidate context 380 */ 381 fibctx->type = 0; 382 /* 383 * Free the space occupied by the Context 384 */ 385 kfree(fibctx); 386 return 0; 387 } 388 389 /** 390 * close_getadapter_fib - close down user fib context 391 * @dev: adapter 392 * @arg: ioctl arguments 393 * 394 * This routine will close down the fibctx passed in from the user. 395 */ 396 397 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg) 398 { 399 struct aac_fib_context *fibctx; 400 int status; 401 unsigned long flags; 402 struct list_head * entry; 403 404 /* 405 * Verify that the HANDLE passed in was a valid AdapterFibContext 406 * 407 * Search the list of AdapterFibContext addresses on the adapter 408 * to be sure this is a valid address 409 */ 410 411 entry = dev->fib_list.next; 412 fibctx = NULL; 413 414 while(entry != &dev->fib_list) { 415 fibctx = list_entry(entry, struct aac_fib_context, next); 416 /* 417 * Extract the fibctx from the input parameters 418 */ 419 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */ 420 break; 421 entry = entry->next; 422 fibctx = NULL; 423 } 424 425 if (!fibctx) 426 return 0; /* Already gone */ 427 428 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) || 429 (fibctx->size != sizeof(struct aac_fib_context))) 430 return -EINVAL; 431 spin_lock_irqsave(&dev->fib_lock, flags); 432 status = aac_close_fib_context(dev, fibctx); 433 spin_unlock_irqrestore(&dev->fib_lock, flags); 434 return status; 435 } 436 437 /** 438 * check_revision - close down user fib context 439 * @dev: adapter 440 * @arg: ioctl arguments 441 * 442 * This routine returns the driver version. 443 * Under Linux, there have been no version incompatibilities, so this is 444 * simple! 445 */ 446 447 static int check_revision(struct aac_dev *dev, void __user *arg) 448 { 449 struct revision response; 450 char *driver_version = aac_driver_version; 451 u32 version; 452 453 response.compat = 1; 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); 461 # else 462 response.build = cpu_to_le32(9999); 463 # endif 464 465 if (copy_to_user(arg, &response, sizeof(response))) 466 return -EFAULT; 467 return 0; 468 } 469 470 471 /** 472 * 473 * aac_send_raw_scb 474 * 475 */ 476 477 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg) 478 { 479 struct fib* srbfib; 480 int status; 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; 486 u32 chn; 487 u32 fibsize = 0; 488 u32 flags = 0; 489 s32 rcode = 0; 490 u32 data_dir; 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]; 494 u32 sg_indx = 0; 495 u32 byte_count = 0; 496 u32 actual_fibsize64, actual_fibsize = 0; 497 int i; 498 int is_native_device; 499 u64 address; 500 501 502 if (dev->in_reset) { 503 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n")); 504 return -EBUSY; 505 } 506 if (!capable(CAP_SYS_ADMIN)){ 507 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n")); 508 return -EPERM; 509 } 510 /* 511 * Allocate and initialize a Fib then setup a SRB command 512 */ 513 if (!(srbfib = aac_fib_alloc(dev))) { 514 return -ENOMEM; 515 } 516 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")); 520 rcode = -EFAULT; 521 goto cleanup; 522 } 523 524 if ((fibsize < (sizeof(struct user_aac_srb) - sizeof(struct user_sgentry))) || 525 (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))) { 526 rcode = -EINVAL; 527 goto cleanup; 528 } 529 530 user_srbcmd = kmalloc(fibsize, GFP_KERNEL); 531 if (!user_srbcmd) { 532 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n")); 533 rcode = -ENOMEM; 534 goto cleanup; 535 } 536 if(copy_from_user(user_srbcmd, user_srb,fibsize)){ 537 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n")); 538 rcode = -EFAULT; 539 goto cleanup; 540 } 541 542 flags = user_srbcmd->flags; /* from user in cpu order */ 543 switch (flags & (SRB_DataIn | SRB_DataOut)) { 544 case SRB_DataOut: 545 data_dir = DMA_TO_DEVICE; 546 break; 547 case (SRB_DataIn | SRB_DataOut): 548 data_dir = DMA_BIDIRECTIONAL; 549 break; 550 case SRB_DataIn: 551 data_dir = DMA_FROM_DEVICE; 552 break; 553 default: 554 data_dir = DMA_NONE; 555 } 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)); 559 rcode = -EINVAL; 560 goto cleanup; 561 } 562 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) { 563 dprintk((KERN_DEBUG"aacraid:SG with no direction specified\n")); 564 rcode = -EINVAL; 565 goto cleanup; 566 } 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)); 580 rcode = -EINVAL; 581 goto cleanup; 582 } 583 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 */ 591 592 /* iu_type is a parameter of aac_hba_send */ 593 switch (data_dir) { 594 case DMA_TO_DEVICE: 595 hbacmd->byte1 = 2; 596 break; 597 case DMA_FROM_DEVICE: 598 case DMA_BIDIRECTIONAL: 599 hbacmd->byte1 = 1; 600 break; 601 case DMA_NONE: 602 default: 603 break; 604 } 605 hbacmd->lun[1] = cpu_to_le32(user_srbcmd->lun); 606 hbacmd->it_nexus = dev->hba_map[chn][user_srbcmd->id].rmw_nexus; 607 608 /* 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 612 * in sg list build 613 */ 614 615 memcpy(hbacmd->cdb, user_srbcmd->cdb, sizeof(hbacmd->cdb)); 616 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); 625 626 } else { 627 is_native_device = 0; 628 aac_fib_init(srbfib); 629 630 /* raw_srb FIB is not FastResponseCapable */ 631 srbfib->hw_fib_va->header.XferState &= 632 ~cpu_to_le32(FastResponseCapable); 633 634 srbcmd = (struct aac_srb *) fib_data(srbfib); 635 636 // Fix up srb for endian and force some values 637 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)); 647 } 648 649 byte_count = 0; 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; 654 655 for (i = 0; i < usg32->count; i++) { 656 void *p; 657 u64 addr; 658 659 sg_count[i] = (actual_fibsize64 == fibsize) ? 660 usg64->sg[i].count : usg32->sg[i].count; 661 if (sg_count[i] > 662 (dev->scsi_host_ptr->max_sectors << 9)) { 663 pr_err("aacraid: upsg->sg[%d].count=%u>%u\n", 664 i, sg_count[i], 665 dev->scsi_host_ptr->max_sectors << 9); 666 rcode = -EINVAL; 667 goto cleanup; 668 } 669 670 p = kmalloc(sg_count[i], GFP_KERNEL); 671 if (!p) { 672 rcode = -ENOMEM; 673 goto cleanup; 674 } 675 676 if (actual_fibsize64 == fibsize) { 677 addr = (u64)usg64->sg[i].addr[0]; 678 addr += ((u64)usg64->sg[i].addr[1]) << 32; 679 } else { 680 addr = (u64)usg32->sg[i].addr; 681 } 682 683 sg_user[i] = (void __user *)(uintptr_t)addr; 684 sg_list[i] = p; // save so we can clean up later 685 sg_indx = i; 686 687 if (flags & SRB_DataOut) { 688 if (copy_from_user(p, sg_user[i], 689 sg_count[i])) { 690 rcode = -EFAULT; 691 goto cleanup; 692 } 693 } 694 addr = pci_map_single(dev->pdev, p, sg_count[i], 695 data_dir); 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]; 702 } 703 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); 708 709 status = aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, srbfib, 710 NULL, NULL); 711 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; 715 716 /* 717 * This should also catch if user used the 32 bit sgmap 718 */ 719 if (actual_fibsize64 == fibsize) { 720 actual_fibsize = actual_fibsize64; 721 for (i = 0; i < upsg->count; i++) { 722 u64 addr; 723 void* p; 724 725 sg_count[i] = upsg->sg[i].count; 726 if (sg_count[i] > 727 ((dev->adapter_info.options & 728 AAC_OPT_NEW_COMM) ? 729 (dev->scsi_host_ptr->max_sectors << 9) : 730 65536)) { 731 rcode = -EINVAL; 732 goto cleanup; 733 } 734 735 p = kmalloc(sg_count[i], GFP_KERNEL); 736 if(!p) { 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)); 739 rcode = -ENOMEM; 740 goto cleanup; 741 } 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 746 sg_indx = i; 747 748 if (flags & SRB_DataOut) { 749 if (copy_from_user(p, sg_user[i], 750 sg_count[i])){ 751 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); 752 rcode = -EFAULT; 753 goto cleanup; 754 } 755 } 756 addr = pci_map_single(dev->pdev, p, 757 sg_count[i], data_dir); 758 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]); 763 } 764 } else { 765 struct user_sgmap* usg; 766 usg = kmemdup(upsg, 767 actual_fibsize - sizeof(struct aac_srb) 768 + sizeof(struct sgmap), GFP_KERNEL); 769 if (!usg) { 770 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n")); 771 rcode = -ENOMEM; 772 goto cleanup; 773 } 774 actual_fibsize = actual_fibsize64; 775 776 for (i = 0; i < usg->count; i++) { 777 u64 addr; 778 void* p; 779 780 sg_count[i] = usg->sg[i].count; 781 if (sg_count[i] > 782 ((dev->adapter_info.options & 783 AAC_OPT_NEW_COMM) ? 784 (dev->scsi_host_ptr->max_sectors << 9) : 785 65536)) { 786 kfree(usg); 787 rcode = -EINVAL; 788 goto cleanup; 789 } 790 791 p = kmalloc(sg_count[i], GFP_KERNEL); 792 if(!p) { 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)); 795 kfree(usg); 796 rcode = -ENOMEM; 797 goto cleanup; 798 } 799 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr; 800 sg_list[i] = p; // save so we can clean up later 801 sg_indx = i; 802 803 if (flags & SRB_DataOut) { 804 if (copy_from_user(p, sg_user[i], 805 sg_count[i])) { 806 kfree (usg); 807 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); 808 rcode = -EFAULT; 809 goto cleanup; 810 } 811 } 812 addr = pci_map_single(dev->pdev, p, 813 sg_count[i], data_dir); 814 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]); 819 } 820 kfree (usg); 821 } 822 srbcmd->count = cpu_to_le32(byte_count); 823 if (user_srbcmd->sg.count) 824 psg->count = cpu_to_le32(sg_indx+1); 825 else 826 psg->count = 0; 827 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL); 828 } else { 829 struct user_sgmap* upsg = &user_srbcmd->sg; 830 struct sgmap* psg = &srbcmd->sg; 831 832 if (actual_fibsize64 == fibsize) { 833 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg; 834 for (i = 0; i < upsg->count; i++) { 835 uintptr_t addr; 836 void* p; 837 838 sg_count[i] = usg->sg[i].count; 839 if (sg_count[i] > 840 ((dev->adapter_info.options & 841 AAC_OPT_NEW_COMM) ? 842 (dev->scsi_host_ptr->max_sectors << 9) : 843 65536)) { 844 rcode = -EINVAL; 845 goto cleanup; 846 } 847 p = kmalloc(sg_count[i], GFP_KERNEL); 848 if (!p) { 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)); 851 rcode = -ENOMEM; 852 goto cleanup; 853 } 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 858 sg_indx = i; 859 860 if (flags & SRB_DataOut) { 861 if (copy_from_user(p, sg_user[i], 862 sg_count[i])){ 863 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); 864 rcode = -EFAULT; 865 goto cleanup; 866 } 867 } 868 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir); 869 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]); 873 } 874 } else { 875 for (i = 0; i < upsg->count; i++) { 876 dma_addr_t addr; 877 void* p; 878 879 sg_count[i] = upsg->sg[i].count; 880 if (sg_count[i] > 881 ((dev->adapter_info.options & 882 AAC_OPT_NEW_COMM) ? 883 (dev->scsi_host_ptr->max_sectors << 9) : 884 65536)) { 885 rcode = -EINVAL; 886 goto cleanup; 887 } 888 p = kmalloc(sg_count[i], GFP_KERNEL); 889 if (!p) { 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)); 892 rcode = -ENOMEM; 893 goto cleanup; 894 } 895 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr; 896 sg_list[i] = p; // save so we can clean up later 897 sg_indx = i; 898 899 if (flags & SRB_DataOut) { 900 if (copy_from_user(p, sg_user[i], 901 sg_count[i])) { 902 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); 903 rcode = -EFAULT; 904 goto cleanup; 905 } 906 } 907 addr = pci_map_single(dev->pdev, p, 908 sg_count[i], data_dir); 909 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]); 913 } 914 } 915 srbcmd->count = cpu_to_le32(byte_count); 916 if (user_srbcmd->sg.count) 917 psg->count = cpu_to_le32(sg_indx+1); 918 else 919 psg->count = 0; 920 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL); 921 } 922 923 if (status == -ERESTARTSYS) { 924 rcode = -ERESTARTSYS; 925 goto cleanup; 926 } 927 928 if (status != 0) { 929 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n")); 930 rcode = -ENXIO; 931 goto cleanup; 932 } 933 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")); 938 rcode = -EFAULT; 939 goto cleanup; 940 941 } 942 } 943 } 944 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; 950 951 memset(&reply, 0, sizeof(reply)); 952 reply.status = ST_OK; 953 if (srbfib->flags & FIB_CONTEXT_FLAG_FASTRESP) { 954 /* fast response */ 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); 960 } else { 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); 968 } 969 if (copy_to_user(user_reply, &reply, 970 sizeof(struct aac_srb_reply))) { 971 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n")); 972 rcode = -EFAULT; 973 goto cleanup; 974 } 975 } else { 976 struct aac_srb_reply *reply; 977 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")); 982 rcode = -EFAULT; 983 goto cleanup; 984 } 985 } 986 987 cleanup: 988 kfree(user_srbcmd); 989 if (rcode != -ERESTARTSYS) { 990 for (i = 0; i <= sg_indx; i++) 991 kfree(sg_list[i]); 992 aac_fib_complete(srbfib); 993 aac_fib_free(srbfib); 994 } 995 996 return rcode; 997 } 998 999 struct aac_pci_info { 1000 u32 bus; 1001 u32 slot; 1002 }; 1003 1004 1005 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg) 1006 { 1007 struct aac_pci_info pci_info; 1008 1009 pci_info.bus = dev->pdev->bus->number; 1010 pci_info.slot = PCI_SLOT(dev->pdev->devfn); 1011 1012 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) { 1013 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n")); 1014 return -EFAULT; 1015 } 1016 return 0; 1017 } 1018 1019 static int aac_get_hba_info(struct aac_dev *dev, void __user *arg) 1020 { 1021 struct aac_hba_info hbainfo; 1022 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); 1028 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; 1033 1034 if (copy_to_user(arg, &hbainfo, sizeof(struct aac_hba_info))) { 1035 dprintk((KERN_DEBUG "aacraid: Could not copy hba info\n")); 1036 return -EFAULT; 1037 } 1038 1039 return 0; 1040 } 1041 1042 struct aac_reset_iop { 1043 u8 reset_type; 1044 }; 1045 1046 static int aac_send_reset_adapter(struct aac_dev *dev, void __user *arg) 1047 { 1048 struct aac_reset_iop reset; 1049 int retval; 1050 1051 if (copy_from_user((void *)&reset, arg, sizeof(struct aac_reset_iop))) 1052 return -EFAULT; 1053 1054 dev->adapter_shutdown = 1; 1055 1056 mutex_unlock(&dev->ioctl_mutex); 1057 retval = aac_reset_adapter(dev, 0, reset.reset_type); 1058 mutex_lock(&dev->ioctl_mutex); 1059 1060 return retval; 1061 } 1062 1063 int aac_do_ioctl(struct aac_dev *dev, unsigned int cmd, void __user *arg) 1064 { 1065 int status; 1066 1067 mutex_lock(&dev->ioctl_mutex); 1068 1069 if (dev->adapter_shutdown) { 1070 status = -EACCES; 1071 goto cleanup; 1072 } 1073 1074 /* 1075 * HBA gets first crack 1076 */ 1077 1078 status = aac_dev_ioctl(dev, cmd, arg); 1079 if (status != -ENOTTY) 1080 goto cleanup; 1081 1082 switch (cmd) { 1083 case FSACTL_MINIPORT_REV_CHECK: 1084 status = check_revision(dev, arg); 1085 break; 1086 case FSACTL_SEND_LARGE_FIB: 1087 case FSACTL_SENDFIB: 1088 status = ioctl_send_fib(dev, arg); 1089 break; 1090 case FSACTL_OPEN_GET_ADAPTER_FIB: 1091 status = open_getadapter_fib(dev, arg); 1092 break; 1093 case FSACTL_GET_NEXT_ADAPTER_FIB: 1094 status = next_getadapter_fib(dev, arg); 1095 break; 1096 case FSACTL_CLOSE_GET_ADAPTER_FIB: 1097 status = close_getadapter_fib(dev, arg); 1098 break; 1099 case FSACTL_SEND_RAW_SRB: 1100 status = aac_send_raw_srb(dev,arg); 1101 break; 1102 case FSACTL_GET_PCI_INFO: 1103 status = aac_get_pci_info(dev,arg); 1104 break; 1105 case FSACTL_GET_HBA_INFO: 1106 status = aac_get_hba_info(dev, arg); 1107 break; 1108 case FSACTL_RESET_IOP: 1109 status = aac_send_reset_adapter(dev, arg); 1110 break; 1111 1112 default: 1113 status = -ENOTTY; 1114 break; 1115 } 1116 1117 cleanup: 1118 mutex_unlock(&dev->ioctl_mutex); 1119 1120 return status; 1121 } 1122 1123