1 /* 2 * 3 * Linux MegaRAID device driver 4 * 5 * Copyright (c) 2003-2004 LSI Logic Corporation. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 10 * 2 of the License, or (at your option) any later version. 11 * 12 * FILE : megaraid_mm.c 13 * Version : v2.20.2.6 (Mar 7 2005) 14 * 15 * Common management module 16 */ 17 18 #include "megaraid_mm.h" 19 20 21 // Entry points for char node driver 22 static int mraid_mm_open(struct inode *, struct file *); 23 static int mraid_mm_ioctl(struct inode *, struct file *, uint, unsigned long); 24 25 26 // routines to convert to and from the old the format 27 static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *); 28 static int kioc_to_mimd(uioc_t *, mimd_t __user *); 29 30 31 // Helper functions 32 static int handle_drvrcmd(void __user *, uint8_t, int *); 33 static int lld_ioctl(mraid_mmadp_t *, uioc_t *); 34 static void ioctl_done(uioc_t *); 35 static void lld_timedout(unsigned long); 36 static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *); 37 static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *); 38 static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *); 39 static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *); 40 static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int); 41 static int mraid_mm_setup_dma_pools(mraid_mmadp_t *); 42 static void mraid_mm_free_adp_resources(mraid_mmadp_t *); 43 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *); 44 45 #ifdef CONFIG_COMPAT 46 static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long); 47 #endif 48 49 MODULE_AUTHOR("LSI Logic Corporation"); 50 MODULE_DESCRIPTION("LSI Logic Management Module"); 51 MODULE_LICENSE("GPL"); 52 MODULE_VERSION(LSI_COMMON_MOD_VERSION); 53 54 static int dbglevel = CL_ANN; 55 module_param_named(dlevel, dbglevel, int, 0); 56 MODULE_PARM_DESC(dlevel, "Debug level (default=0)"); 57 58 EXPORT_SYMBOL(mraid_mm_register_adp); 59 EXPORT_SYMBOL(mraid_mm_unregister_adp); 60 EXPORT_SYMBOL(mraid_mm_adapter_app_handle); 61 62 static int majorno; 63 static uint32_t drvr_ver = 0x02200206; 64 65 static int adapters_count_g; 66 static struct list_head adapters_list_g; 67 68 static wait_queue_head_t wait_q; 69 70 static struct file_operations lsi_fops = { 71 .open = mraid_mm_open, 72 .ioctl = mraid_mm_ioctl, 73 #ifdef CONFIG_COMPAT 74 .compat_ioctl = mraid_mm_compat_ioctl, 75 #endif 76 .owner = THIS_MODULE, 77 }; 78 79 /** 80 * mraid_mm_open - open routine for char node interface 81 * @inod : unused 82 * @filep : unused 83 * 84 * allow ioctl operations by apps only if they superuser privilege 85 */ 86 static int 87 mraid_mm_open(struct inode *inode, struct file *filep) 88 { 89 /* 90 * Only allow superuser to access private ioctl interface 91 */ 92 if (!capable(CAP_SYS_ADMIN)) return (-EACCES); 93 94 return 0; 95 } 96 97 /** 98 * mraid_mm_ioctl - module entry-point for ioctls 99 * @inode : inode (ignored) 100 * @filep : file operations pointer (ignored) 101 * @cmd : ioctl command 102 * @arg : user ioctl packet 103 */ 104 static int 105 mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, 106 unsigned long arg) 107 { 108 uioc_t *kioc; 109 char signature[EXT_IOCTL_SIGN_SZ] = {0}; 110 int rval; 111 mraid_mmadp_t *adp; 112 uint8_t old_ioctl; 113 int drvrcmd_rval; 114 void __user *argp = (void __user *)arg; 115 116 /* 117 * Make sure only USCSICMD are issued through this interface. 118 * MIMD application would still fire different command. 119 */ 120 121 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) { 122 return (-EINVAL); 123 } 124 125 /* 126 * Look for signature to see if this is the new or old ioctl format. 127 */ 128 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) { 129 con_log(CL_ANN, (KERN_WARNING 130 "megaraid cmm: copy from usr addr failed\n")); 131 return (-EFAULT); 132 } 133 134 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0) 135 old_ioctl = 0; 136 else 137 old_ioctl = 1; 138 139 /* 140 * At present, we don't support the new ioctl packet 141 */ 142 if (!old_ioctl ) 143 return (-EINVAL); 144 145 /* 146 * If it is a driver ioctl (as opposed to fw ioctls), then we can 147 * handle the command locally. rval > 0 means it is not a drvr cmd 148 */ 149 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval); 150 151 if (rval < 0) 152 return rval; 153 else if (rval == 0) 154 return drvrcmd_rval; 155 156 rval = 0; 157 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) { 158 return rval; 159 } 160 161 /* 162 * Check if adapter can accept ioctl. We may have marked it offline 163 * if any previous kioc had timedout on this controller. 164 */ 165 if (!adp->quiescent) { 166 con_log(CL_ANN, (KERN_WARNING 167 "megaraid cmm: controller cannot accept cmds due to " 168 "earlier errors\n" )); 169 return -EFAULT; 170 } 171 172 /* 173 * The following call will block till a kioc is available 174 */ 175 kioc = mraid_mm_alloc_kioc(adp); 176 177 /* 178 * User sent the old mimd_t ioctl packet. Convert it to uioc_t. 179 */ 180 if ((rval = mimd_to_kioc(argp, adp, kioc))) { 181 mraid_mm_dealloc_kioc(adp, kioc); 182 return rval; 183 } 184 185 kioc->done = ioctl_done; 186 187 /* 188 * Issue the IOCTL to the low level driver. After the IOCTL completes 189 * release the kioc if and only if it was _not_ timedout. If it was 190 * timedout, that means that resources are still with low level driver. 191 */ 192 if ((rval = lld_ioctl(adp, kioc))) { 193 194 if (!kioc->timedout) 195 mraid_mm_dealloc_kioc(adp, kioc); 196 197 return rval; 198 } 199 200 /* 201 * Convert the kioc back to user space 202 */ 203 rval = kioc_to_mimd(kioc, argp); 204 205 /* 206 * Return the kioc to free pool 207 */ 208 mraid_mm_dealloc_kioc(adp, kioc); 209 210 return rval; 211 } 212 213 214 /** 215 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet 216 * @umimd : User space mimd_t ioctl packet 217 * @adapter : pointer to the adapter (OUT) 218 */ 219 static mraid_mmadp_t * 220 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval) 221 { 222 mraid_mmadp_t *adapter; 223 mimd_t mimd; 224 uint32_t adapno; 225 int iterator; 226 227 228 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) { 229 *rval = -EFAULT; 230 return NULL; 231 } 232 233 adapno = GETADAP(mimd.ui.fcs.adapno); 234 235 if (adapno >= adapters_count_g) { 236 *rval = -ENODEV; 237 return NULL; 238 } 239 240 adapter = NULL; 241 iterator = 0; 242 243 list_for_each_entry(adapter, &adapters_list_g, list) { 244 if (iterator++ == adapno) break; 245 } 246 247 if (!adapter) { 248 *rval = -ENODEV; 249 return NULL; 250 } 251 252 return adapter; 253 } 254 255 /* 256 * handle_drvrcmd - This routine checks if the opcode is a driver 257 * cmd and if it is, handles it. 258 * @arg : packet sent by the user app 259 * @old_ioctl : mimd if 1; uioc otherwise 260 */ 261 static int 262 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval) 263 { 264 mimd_t __user *umimd; 265 mimd_t kmimd; 266 uint8_t opcode; 267 uint8_t subopcode; 268 269 if (old_ioctl) 270 goto old_packet; 271 else 272 goto new_packet; 273 274 new_packet: 275 return (-ENOTSUPP); 276 277 old_packet: 278 *rval = 0; 279 umimd = arg; 280 281 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t))) 282 return (-EFAULT); 283 284 opcode = kmimd.ui.fcs.opcode; 285 subopcode = kmimd.ui.fcs.subopcode; 286 287 /* 288 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or 289 * GET_NUMADP, then we can handle. Otherwise we should return 1 to 290 * indicate that we cannot handle this. 291 */ 292 if (opcode != 0x82) 293 return 1; 294 295 switch (subopcode) { 296 297 case MEGAIOC_QDRVRVER: 298 299 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t))) 300 return (-EFAULT); 301 302 return 0; 303 304 case MEGAIOC_QNADAP: 305 306 *rval = adapters_count_g; 307 308 if (copy_to_user(kmimd.data, &adapters_count_g, 309 sizeof(uint32_t))) 310 return (-EFAULT); 311 312 return 0; 313 314 default: 315 /* cannot handle */ 316 return 1; 317 } 318 319 return 0; 320 } 321 322 323 /** 324 * mimd_to_kioc - Converter from old to new ioctl format 325 * 326 * @umimd : user space old MIMD IOCTL 327 * @kioc : kernel space new format IOCTL 328 * 329 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The 330 * new packet is in kernel space so that driver can perform operations on it 331 * freely. 332 */ 333 334 static int 335 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc) 336 { 337 mbox64_t *mbox64; 338 mbox_t *mbox; 339 mraid_passthru_t *pthru32; 340 uint32_t adapno; 341 uint8_t opcode; 342 uint8_t subopcode; 343 mimd_t mimd; 344 345 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) 346 return (-EFAULT); 347 348 /* 349 * Applications are not allowed to send extd pthru 350 */ 351 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) || 352 (mimd.mbox[0] == MBOXCMD_EXTPTHRU)) 353 return (-EINVAL); 354 355 opcode = mimd.ui.fcs.opcode; 356 subopcode = mimd.ui.fcs.subopcode; 357 adapno = GETADAP(mimd.ui.fcs.adapno); 358 359 if (adapno >= adapters_count_g) 360 return (-ENODEV); 361 362 kioc->adapno = adapno; 363 kioc->mb_type = MBOX_LEGACY; 364 kioc->app_type = APPTYPE_MIMD; 365 366 switch (opcode) { 367 368 case 0x82: 369 370 if (subopcode == MEGAIOC_QADAPINFO) { 371 372 kioc->opcode = GET_ADAP_INFO; 373 kioc->data_dir = UIOC_RD; 374 kioc->xferlen = sizeof(mraid_hba_info_t); 375 376 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen)) 377 return (-ENOMEM); 378 } 379 else { 380 con_log(CL_ANN, (KERN_WARNING 381 "megaraid cmm: Invalid subop\n")); 382 return (-EINVAL); 383 } 384 385 break; 386 387 case 0x81: 388 389 kioc->opcode = MBOX_CMD; 390 kioc->xferlen = mimd.ui.fcs.length; 391 kioc->user_data_len = kioc->xferlen; 392 kioc->user_data = mimd.ui.fcs.buffer; 393 394 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen)) 395 return (-ENOMEM); 396 397 if (mimd.outlen) kioc->data_dir = UIOC_RD; 398 if (mimd.inlen) kioc->data_dir |= UIOC_WR; 399 400 break; 401 402 case 0x80: 403 404 kioc->opcode = MBOX_CMD; 405 kioc->xferlen = (mimd.outlen > mimd.inlen) ? 406 mimd.outlen : mimd.inlen; 407 kioc->user_data_len = kioc->xferlen; 408 kioc->user_data = mimd.data; 409 410 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen)) 411 return (-ENOMEM); 412 413 if (mimd.outlen) kioc->data_dir = UIOC_RD; 414 if (mimd.inlen) kioc->data_dir |= UIOC_WR; 415 416 break; 417 418 default: 419 return (-EINVAL); 420 } 421 422 /* 423 * If driver command, nothing else to do 424 */ 425 if (opcode == 0x82) 426 return 0; 427 428 /* 429 * This is a mailbox cmd; copy the mailbox from mimd 430 */ 431 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf); 432 mbox = &mbox64->mbox32; 433 memcpy(mbox, mimd.mbox, 14); 434 435 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD 436 437 mbox->xferaddr = (uint32_t)kioc->buf_paddr; 438 439 if (kioc->data_dir & UIOC_WR) { 440 if (copy_from_user(kioc->buf_vaddr, kioc->user_data, 441 kioc->xferlen)) { 442 return (-EFAULT); 443 } 444 } 445 446 return 0; 447 } 448 449 /* 450 * This is a regular 32-bit pthru cmd; mbox points to pthru struct. 451 * Just like in above case, the beginning for memblk is treated as 452 * a mailbox. The passthru will begin at next 1K boundary. And the 453 * data will start 1K after that. 454 */ 455 pthru32 = kioc->pthru32; 456 kioc->user_pthru = &umimd->pthru; 457 mbox->xferaddr = (uint32_t)kioc->pthru32_h; 458 459 if (copy_from_user(pthru32, kioc->user_pthru, 460 sizeof(mraid_passthru_t))) { 461 return (-EFAULT); 462 } 463 464 pthru32->dataxferaddr = kioc->buf_paddr; 465 if (kioc->data_dir & UIOC_WR) { 466 if (copy_from_user(kioc->buf_vaddr, kioc->user_data, 467 pthru32->dataxferlen)) { 468 return (-EFAULT); 469 } 470 } 471 472 return 0; 473 } 474 475 /** 476 * mraid_mm_attch_buf - Attach a free dma buffer for required size 477 * 478 * @adp : Adapter softstate 479 * @kioc : kioc that the buffer needs to be attached to 480 * @xferlen : required length for buffer 481 * 482 * First we search for a pool with smallest buffer that is >= @xferlen. If 483 * that pool has no free buffer, we will try for the next bigger size. If none 484 * is available, we will try to allocate the smallest buffer that is >= 485 * @xferlen and attach it the pool. 486 */ 487 static int 488 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen) 489 { 490 mm_dmapool_t *pool; 491 int right_pool = -1; 492 unsigned long flags; 493 int i; 494 495 kioc->pool_index = -1; 496 kioc->buf_vaddr = NULL; 497 kioc->buf_paddr = 0; 498 kioc->free_buf = 0; 499 500 /* 501 * We need xferlen amount of memory. See if we can get it from our 502 * dma pools. If we don't get exact size, we will try bigger buffer 503 */ 504 505 for (i = 0; i < MAX_DMA_POOLS; i++) { 506 507 pool = &adp->dma_pool_list[i]; 508 509 if (xferlen > pool->buf_size) 510 continue; 511 512 if (right_pool == -1) 513 right_pool = i; 514 515 spin_lock_irqsave(&pool->lock, flags); 516 517 if (!pool->in_use) { 518 519 pool->in_use = 1; 520 kioc->pool_index = i; 521 kioc->buf_vaddr = pool->vaddr; 522 kioc->buf_paddr = pool->paddr; 523 524 spin_unlock_irqrestore(&pool->lock, flags); 525 return 0; 526 } 527 else { 528 spin_unlock_irqrestore(&pool->lock, flags); 529 continue; 530 } 531 } 532 533 /* 534 * If xferlen doesn't match any of our pools, return error 535 */ 536 if (right_pool == -1) 537 return -EINVAL; 538 539 /* 540 * We did not get any buffer from the preallocated pool. Let us try 541 * to allocate one new buffer. NOTE: This is a blocking call. 542 */ 543 pool = &adp->dma_pool_list[right_pool]; 544 545 spin_lock_irqsave(&pool->lock, flags); 546 547 kioc->pool_index = right_pool; 548 kioc->free_buf = 1; 549 kioc->buf_vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL, 550 &kioc->buf_paddr); 551 spin_unlock_irqrestore(&pool->lock, flags); 552 553 if (!kioc->buf_vaddr) 554 return -ENOMEM; 555 556 return 0; 557 } 558 559 /** 560 * mraid_mm_alloc_kioc - Returns a uioc_t from free list 561 * @adp : Adapter softstate for this module 562 * 563 * The kioc_semaphore is initialized with number of kioc nodes in the 564 * free kioc pool. If the kioc pool is empty, this function blocks till 565 * a kioc becomes free. 566 */ 567 static uioc_t * 568 mraid_mm_alloc_kioc(mraid_mmadp_t *adp) 569 { 570 uioc_t *kioc; 571 struct list_head* head; 572 unsigned long flags; 573 574 down(&adp->kioc_semaphore); 575 576 spin_lock_irqsave(&adp->kioc_pool_lock, flags); 577 578 head = &adp->kioc_pool; 579 580 if (list_empty(head)) { 581 up(&adp->kioc_semaphore); 582 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags); 583 584 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n")); 585 return NULL; 586 } 587 588 kioc = list_entry(head->next, uioc_t, list); 589 list_del_init(&kioc->list); 590 591 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags); 592 593 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t)); 594 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t)); 595 596 kioc->buf_vaddr = NULL; 597 kioc->buf_paddr = 0; 598 kioc->pool_index =-1; 599 kioc->free_buf = 0; 600 kioc->user_data = NULL; 601 kioc->user_data_len = 0; 602 kioc->user_pthru = NULL; 603 kioc->timedout = 0; 604 605 return kioc; 606 } 607 608 /** 609 * mraid_mm_dealloc_kioc - Return kioc to free pool 610 * 611 * @adp : Adapter softstate 612 * @kioc : uioc_t node to be returned to free pool 613 */ 614 static void 615 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc) 616 { 617 mm_dmapool_t *pool; 618 unsigned long flags; 619 620 if (kioc->pool_index != -1) { 621 pool = &adp->dma_pool_list[kioc->pool_index]; 622 623 /* This routine may be called in non-isr context also */ 624 spin_lock_irqsave(&pool->lock, flags); 625 626 /* 627 * While attaching the dma buffer, if we didn't get the 628 * required buffer from the pool, we would have allocated 629 * it at the run time and set the free_buf flag. We must 630 * free that buffer. Otherwise, just mark that the buffer is 631 * not in use 632 */ 633 if (kioc->free_buf == 1) 634 pci_pool_free(pool->handle, kioc->buf_vaddr, 635 kioc->buf_paddr); 636 else 637 pool->in_use = 0; 638 639 spin_unlock_irqrestore(&pool->lock, flags); 640 } 641 642 /* Return the kioc to the free pool */ 643 spin_lock_irqsave(&adp->kioc_pool_lock, flags); 644 list_add(&kioc->list, &adp->kioc_pool); 645 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags); 646 647 /* increment the free kioc count */ 648 up(&adp->kioc_semaphore); 649 650 return; 651 } 652 653 /** 654 * lld_ioctl - Routine to issue ioctl to low level drvr 655 * 656 * @adp : The adapter handle 657 * @kioc : The ioctl packet with kernel addresses 658 */ 659 static int 660 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc) 661 { 662 int rval; 663 struct timer_list timer; 664 struct timer_list *tp = NULL; 665 666 kioc->status = -ENODATA; 667 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE); 668 669 if (rval) return rval; 670 671 /* 672 * Start the timer 673 */ 674 if (adp->timeout > 0) { 675 tp = &timer; 676 init_timer(tp); 677 678 tp->function = lld_timedout; 679 tp->data = (unsigned long)kioc; 680 tp->expires = jiffies + adp->timeout * HZ; 681 682 add_timer(tp); 683 } 684 685 /* 686 * Wait till the low level driver completes the ioctl. After this 687 * call, the ioctl either completed successfully or timedout. 688 */ 689 wait_event(wait_q, (kioc->status != -ENODATA)); 690 if (tp) { 691 del_timer_sync(tp); 692 } 693 694 /* 695 * If the command had timedout, we mark the controller offline 696 * before returning 697 */ 698 if (kioc->timedout) { 699 adp->quiescent = 0; 700 } 701 702 return kioc->status; 703 } 704 705 706 /** 707 * ioctl_done - callback from the low level driver 708 * 709 * @kioc : completed ioctl packet 710 */ 711 static void 712 ioctl_done(uioc_t *kioc) 713 { 714 uint32_t adapno; 715 int iterator; 716 mraid_mmadp_t* adapter; 717 718 /* 719 * When the kioc returns from driver, make sure it still doesn't 720 * have ENODATA in status. Otherwise, driver will hang on wait_event 721 * forever 722 */ 723 if (kioc->status == -ENODATA) { 724 con_log(CL_ANN, (KERN_WARNING 725 "megaraid cmm: lld didn't change status!\n")); 726 727 kioc->status = -EINVAL; 728 } 729 730 /* 731 * Check if this kioc was timedout before. If so, nobody is waiting 732 * on this kioc. We don't have to wake up anybody. Instead, we just 733 * have to free the kioc 734 */ 735 if (kioc->timedout) { 736 iterator = 0; 737 adapter = NULL; 738 adapno = kioc->adapno; 739 740 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed " 741 "ioctl that was timedout before\n")); 742 743 list_for_each_entry(adapter, &adapters_list_g, list) { 744 if (iterator++ == adapno) break; 745 } 746 747 kioc->timedout = 0; 748 749 if (adapter) { 750 mraid_mm_dealloc_kioc( adapter, kioc ); 751 } 752 } 753 else { 754 wake_up(&wait_q); 755 } 756 } 757 758 759 /* 760 * lld_timedout : callback from the expired timer 761 * 762 * @ptr : ioctl packet that timed out 763 */ 764 static void 765 lld_timedout(unsigned long ptr) 766 { 767 uioc_t *kioc = (uioc_t *)ptr; 768 769 kioc->status = -ETIME; 770 kioc->timedout = 1; 771 772 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n")); 773 774 wake_up(&wait_q); 775 } 776 777 778 /** 779 * kioc_to_mimd : Converter from new back to old format 780 * 781 * @kioc : Kernel space IOCTL packet (successfully issued) 782 * @mimd : User space MIMD packet 783 */ 784 static int 785 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd) 786 { 787 mimd_t kmimd; 788 uint8_t opcode; 789 uint8_t subopcode; 790 791 mbox64_t *mbox64; 792 mraid_passthru_t __user *upthru32; 793 mraid_passthru_t *kpthru32; 794 mcontroller_t cinfo; 795 mraid_hba_info_t *hinfo; 796 797 798 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t))) 799 return (-EFAULT); 800 801 opcode = kmimd.ui.fcs.opcode; 802 subopcode = kmimd.ui.fcs.subopcode; 803 804 if (opcode == 0x82) { 805 switch (subopcode) { 806 807 case MEGAIOC_QADAPINFO: 808 809 hinfo = (mraid_hba_info_t *)(unsigned long) 810 kioc->buf_vaddr; 811 812 hinfo_to_cinfo(hinfo, &cinfo); 813 814 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo))) 815 return (-EFAULT); 816 817 return 0; 818 819 default: 820 return (-EINVAL); 821 } 822 823 return 0; 824 } 825 826 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf; 827 828 if (kioc->user_pthru) { 829 830 upthru32 = kioc->user_pthru; 831 kpthru32 = kioc->pthru32; 832 833 if (copy_to_user(&upthru32->scsistatus, 834 &kpthru32->scsistatus, 835 sizeof(uint8_t))) { 836 return (-EFAULT); 837 } 838 } 839 840 if (kioc->user_data) { 841 if (copy_to_user(kioc->user_data, kioc->buf_vaddr, 842 kioc->user_data_len)) { 843 return (-EFAULT); 844 } 845 } 846 847 if (copy_to_user(&mimd->mbox[17], 848 &mbox64->mbox32.status, sizeof(uint8_t))) { 849 return (-EFAULT); 850 } 851 852 return 0; 853 } 854 855 856 /** 857 * hinfo_to_cinfo - Convert new format hba info into old format 858 * 859 * @hinfo : New format, more comprehensive adapter info 860 * @cinfo : Old format adapter info to support mimd_t apps 861 */ 862 static void 863 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo) 864 { 865 if (!hinfo || !cinfo) 866 return; 867 868 cinfo->base = hinfo->baseport; 869 cinfo->irq = hinfo->irq; 870 cinfo->numldrv = hinfo->num_ldrv; 871 cinfo->pcibus = hinfo->pci_bus; 872 cinfo->pcidev = hinfo->pci_slot; 873 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn); 874 cinfo->pciid = hinfo->pci_device_id; 875 cinfo->pcivendor = hinfo->pci_vendor_id; 876 cinfo->pcislot = hinfo->pci_slot; 877 cinfo->uid = hinfo->unique_id; 878 } 879 880 881 /* 882 * mraid_mm_register_adp - Registration routine for low level drvrs 883 * 884 * @adp : Adapter objejct 885 */ 886 int 887 mraid_mm_register_adp(mraid_mmadp_t *lld_adp) 888 { 889 mraid_mmadp_t *adapter; 890 mbox64_t *mbox_list; 891 uioc_t *kioc; 892 uint32_t rval; 893 int i; 894 895 896 if (lld_adp->drvr_type != DRVRTYPE_MBOX) 897 return (-EINVAL); 898 899 adapter = kmalloc(sizeof(mraid_mmadp_t), GFP_KERNEL); 900 901 if (!adapter) { 902 rval = -ENOMEM; 903 goto memalloc_error; 904 } 905 906 memset(adapter, 0, sizeof(mraid_mmadp_t)); 907 908 adapter->unique_id = lld_adp->unique_id; 909 adapter->drvr_type = lld_adp->drvr_type; 910 adapter->drvr_data = lld_adp->drvr_data; 911 adapter->pdev = lld_adp->pdev; 912 adapter->issue_uioc = lld_adp->issue_uioc; 913 adapter->timeout = lld_adp->timeout; 914 adapter->max_kioc = lld_adp->max_kioc; 915 adapter->quiescent = 1; 916 917 /* 918 * Allocate single blocks of memory for all required kiocs, 919 * mailboxes and passthru structures. 920 */ 921 adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc, 922 GFP_KERNEL); 923 adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc, 924 GFP_KERNEL); 925 adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool", 926 adapter->pdev, 927 sizeof(mraid_passthru_t), 928 16, 0); 929 930 if (!adapter->kioc_list || !adapter->mbox_list || 931 !adapter->pthru_dma_pool) { 932 933 con_log(CL_ANN, (KERN_WARNING 934 "megaraid cmm: out of memory, %s %d\n", __FUNCTION__, 935 __LINE__)); 936 937 rval = (-ENOMEM); 938 939 goto memalloc_error; 940 } 941 942 /* 943 * Slice kioc_list and make a kioc_pool with the individiual kiocs 944 */ 945 INIT_LIST_HEAD(&adapter->kioc_pool); 946 spin_lock_init(&adapter->kioc_pool_lock); 947 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc); 948 949 mbox_list = (mbox64_t *)adapter->mbox_list; 950 951 for (i = 0; i < lld_adp->max_kioc; i++) { 952 953 kioc = adapter->kioc_list + i; 954 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i); 955 kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool, 956 GFP_KERNEL, &kioc->pthru32_h); 957 958 if (!kioc->pthru32) { 959 960 con_log(CL_ANN, (KERN_WARNING 961 "megaraid cmm: out of memory, %s %d\n", 962 __FUNCTION__, __LINE__)); 963 964 rval = (-ENOMEM); 965 966 goto pthru_dma_pool_error; 967 } 968 969 list_add_tail(&kioc->list, &adapter->kioc_pool); 970 } 971 972 // Setup the dma pools for data buffers 973 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) { 974 goto dma_pool_error; 975 } 976 977 list_add_tail(&adapter->list, &adapters_list_g); 978 979 adapters_count_g++; 980 981 return 0; 982 983 dma_pool_error: 984 /* Do nothing */ 985 986 pthru_dma_pool_error: 987 988 for (i = 0; i < lld_adp->max_kioc; i++) { 989 kioc = adapter->kioc_list + i; 990 if (kioc->pthru32) { 991 pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32, 992 kioc->pthru32_h); 993 } 994 } 995 996 memalloc_error: 997 998 kfree(adapter->kioc_list); 999 kfree(adapter->mbox_list); 1000 1001 if (adapter->pthru_dma_pool) 1002 pci_pool_destroy(adapter->pthru_dma_pool); 1003 1004 kfree(adapter); 1005 1006 return rval; 1007 } 1008 1009 1010 /** 1011 * mraid_mm_adapter_app_handle - return the application handle for this adapter 1012 * 1013 * For the given driver data, locate the adadpter in our global list and 1014 * return the corresponding handle, which is also used by applications to 1015 * uniquely identify an adapter. 1016 * 1017 * @param unique_id : adapter unique identifier 1018 * 1019 * @return adapter handle if found in the list 1020 * @return 0 if adapter could not be located, should never happen though 1021 */ 1022 uint32_t 1023 mraid_mm_adapter_app_handle(uint32_t unique_id) 1024 { 1025 mraid_mmadp_t *adapter; 1026 mraid_mmadp_t *tmp; 1027 int index = 0; 1028 1029 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) { 1030 1031 if (adapter->unique_id == unique_id) { 1032 1033 return MKADAP(index); 1034 } 1035 1036 index++; 1037 } 1038 1039 return 0; 1040 } 1041 1042 1043 /** 1044 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter 1045 * 1046 * @adp : Adapter softstate 1047 * 1048 * We maintain a pool of dma buffers per each adapter. Each pool has one 1049 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers. 1050 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We 1051 * dont' want to waste too much memory by allocating more buffers per each 1052 * pool. 1053 */ 1054 static int 1055 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp) 1056 { 1057 mm_dmapool_t *pool; 1058 int bufsize; 1059 int i; 1060 1061 /* 1062 * Create MAX_DMA_POOLS number of pools 1063 */ 1064 bufsize = MRAID_MM_INIT_BUFF_SIZE; 1065 1066 for (i = 0; i < MAX_DMA_POOLS; i++){ 1067 1068 pool = &adp->dma_pool_list[i]; 1069 1070 pool->buf_size = bufsize; 1071 spin_lock_init(&pool->lock); 1072 1073 pool->handle = pci_pool_create("megaraid mm data buffer", 1074 adp->pdev, bufsize, 16, 0); 1075 1076 if (!pool->handle) { 1077 goto dma_pool_setup_error; 1078 } 1079 1080 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL, 1081 &pool->paddr); 1082 1083 if (!pool->vaddr) 1084 goto dma_pool_setup_error; 1085 1086 bufsize = bufsize * 2; 1087 } 1088 1089 return 0; 1090 1091 dma_pool_setup_error: 1092 1093 mraid_mm_teardown_dma_pools(adp); 1094 return (-ENOMEM); 1095 } 1096 1097 1098 /* 1099 * mraid_mm_unregister_adp - Unregister routine for low level drivers 1100 * Assume no outstanding ioctls to llds. 1101 * 1102 * @unique_id : UID of the adpater 1103 */ 1104 int 1105 mraid_mm_unregister_adp(uint32_t unique_id) 1106 { 1107 mraid_mmadp_t *adapter; 1108 mraid_mmadp_t *tmp; 1109 1110 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) { 1111 1112 1113 if (adapter->unique_id == unique_id) { 1114 1115 adapters_count_g--; 1116 1117 list_del_init(&adapter->list); 1118 1119 mraid_mm_free_adp_resources(adapter); 1120 1121 kfree(adapter); 1122 1123 con_log(CL_ANN, ( 1124 "megaraid cmm: Unregistered one adapter:%#x\n", 1125 unique_id)); 1126 1127 return 0; 1128 } 1129 } 1130 1131 return (-ENODEV); 1132 } 1133 1134 /** 1135 * mraid_mm_free_adp_resources - Free adapter softstate 1136 * 1137 * @adp : Adapter softstate 1138 */ 1139 static void 1140 mraid_mm_free_adp_resources(mraid_mmadp_t *adp) 1141 { 1142 uioc_t *kioc; 1143 int i; 1144 1145 mraid_mm_teardown_dma_pools(adp); 1146 1147 for (i = 0; i < adp->max_kioc; i++) { 1148 1149 kioc = adp->kioc_list + i; 1150 1151 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32, 1152 kioc->pthru32_h); 1153 } 1154 1155 kfree(adp->kioc_list); 1156 kfree(adp->mbox_list); 1157 1158 pci_pool_destroy(adp->pthru_dma_pool); 1159 1160 1161 return; 1162 } 1163 1164 1165 /** 1166 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers 1167 * 1168 * @adp : Adapter softstate 1169 */ 1170 static void 1171 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp) 1172 { 1173 int i; 1174 mm_dmapool_t *pool; 1175 1176 for (i = 0; i < MAX_DMA_POOLS; i++) { 1177 1178 pool = &adp->dma_pool_list[i]; 1179 1180 if (pool->handle) { 1181 1182 if (pool->vaddr) 1183 pci_pool_free(pool->handle, pool->vaddr, 1184 pool->paddr); 1185 1186 pci_pool_destroy(pool->handle); 1187 pool->handle = NULL; 1188 } 1189 } 1190 1191 return; 1192 } 1193 1194 /** 1195 * mraid_mm_init : Module entry point 1196 */ 1197 static int __init 1198 mraid_mm_init(void) 1199 { 1200 // Announce the driver version 1201 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n", 1202 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION)); 1203 1204 majorno = register_chrdev(0, "megadev", &lsi_fops); 1205 1206 if (majorno < 0) { 1207 con_log(CL_ANN, ("megaraid cmm: cannot get major\n")); 1208 return majorno; 1209 } 1210 1211 init_waitqueue_head(&wait_q); 1212 1213 INIT_LIST_HEAD(&adapters_list_g); 1214 1215 return 0; 1216 } 1217 1218 1219 /** 1220 * mraid_mm_compat_ioctl : 32bit to 64bit ioctl conversion routine 1221 */ 1222 #ifdef CONFIG_COMPAT 1223 static long 1224 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd, 1225 unsigned long arg) 1226 { 1227 int err; 1228 1229 err = mraid_mm_ioctl(NULL, filep, cmd, arg); 1230 1231 return err; 1232 } 1233 #endif 1234 1235 /** 1236 * mraid_mm_exit : Module exit point 1237 */ 1238 static void __exit 1239 mraid_mm_exit(void) 1240 { 1241 con_log(CL_DLEVEL1 , ("exiting common mod\n")); 1242 1243 unregister_chrdev(majorno, "megadev"); 1244 } 1245 1246 module_init(mraid_mm_init); 1247 module_exit(mraid_mm_exit); 1248 1249 /* vi: set ts=8 sw=8 tw=78: */ 1250