megaraid.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:136k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*===================================================================
  2.  *
  3.  *                    Linux MegaRAID device driver
  4.  *
  5.  * Copyright 2001  American Megatrends Inc.
  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.  * Version : v1.18 (Oct 11, 2001)
  13.  *
  14.  * Description: Linux device driver for LSI Logic MegaRAID controller
  15.  *
  16.  * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490
  17.  *                                      493.
  18.  * History:
  19.  *
  20.  * Version 0.90:
  21.  *     Original source contributed by Dell; integrated it into the kernel and
  22.  *     cleaned up some things.  Added support for 438/466 controllers.
  23.  * Version 0.91:
  24.  *     Aligned mailbox area on 16-byte boundary.
  25.  *     Added schedule() at the end to properly clean up.
  26.  *     Made improvements for conformity to linux driver standards.
  27.  *
  28.  * Version 0.92:
  29.  *     Added support for 2.1 kernels.
  30.  *         Reads from pci_dev struct, so it's not dependent on pcibios.
  31.  *         Added some missing virt_to_bus() translations.
  32.  *     Added support for SMP.
  33.  *         Changed global cli()'s to spinlocks for 2.1, and simulated
  34.  *          spinlocks for 2.0.
  35.  *     Removed setting of SA_INTERRUPT flag when requesting Irq.
  36.  *
  37.  * Version 0.92ac:
  38.  *     Small changes to the comments/formatting. Plus a couple of
  39.  *      added notes. Returned to the authors. No actual code changes
  40.  *      save printk levels.
  41.  *     8 Oct 98        Alan Cox <alan.cox@linux.org>
  42.  *
  43.  *     Merged with 2.1.131 source tree.
  44.  *     12 Dec 98       K. Baranowski <kgb@knm.org.pl>
  45.  *
  46.  * Version 0.93:
  47.  *     Added support for vendor specific ioctl commands (M_RD_IOCTL_CMD+xxh)
  48.  *     Changed some fields in MEGARAID struct to better values.
  49.  *     Added signature check for Rp controllers under 2.0 kernels
  50.  *     Changed busy-wait loop to be time-based
  51.  *     Fixed SMP race condition in isr
  52.  *     Added kfree (sgList) on release
  53.  *     Added #include linux/version.h to megaraid.h for hosts.h
  54.  *     Changed max_id to represent max logical drives instead of targets.
  55.  *
  56.  * Version 0.94:
  57.  *     Got rid of some excess locking/unlocking
  58.  *     Fixed slight memory corruption problem while memcpy'ing into mailbox
  59.  *     Changed logical drives to be reported as luns rather than targets
  60.  *     Changed max_id to 16 since it is now max targets/chan again.
  61.  *     Improved ioctl interface for upcoming megamgr
  62.  *
  63.  * Version 0.95:
  64.  *     Fixed problem of queueing multiple commands to adapter;
  65.  *       still has some strange problems on some setups, so still
  66.  *       defaults to single.  To enable parallel commands change
  67.  *       #define MULTI_IO in megaraid.h
  68.  *     Changed kmalloc allocation to be done in beginning.
  69.  *     Got rid of C++ style comments
  70.  *
  71.  * Version 0.96:
  72.  *     762 fully supported.
  73.  *
  74.  * Version 0.97:
  75.  *     Changed megaraid_command to use wait_queue.
  76.  *
  77.  * Version 1.00:
  78.  *     Checks to see if an irq occurred while in isr, and runs through
  79.  *       routine again.
  80.  *     Copies mailbox to temp area before processing in isr
  81.  *     Added barrier() in busy wait to fix volatility bug
  82.  *     Uses separate list for freed Scbs, keeps track of cmd state
  83.  *     Put spinlocks around entire queue function for now...
  84.  *     Full multi-io commands working stablely without previous problems
  85.  *     Added skipXX LILO option for Madrona motherboard support
  86.  *
  87.  * Version 1.01:
  88.  *     Fixed bug in mega_cmd_done() for megamgr control commands,
  89.  *       the host_byte in the result code from the scsi request to
  90.  *       scsi midlayer is set to DID_BAD_TARGET when adapter's
  91.  *       returned codes are 0xF0 and 0xF4.
  92.  *
  93.  * Version 1.02:
  94.  *     Fixed the tape drive bug by extending the adapter timeout value
  95.  *       for passthrough command to 60 seconds in mega_build_cmd().
  96.  *
  97.  * Version 1.03:
  98.  *    Fixed Madrona support.
  99.  *    Changed the adapter timeout value from 60 sec in 1.02 to 10 min
  100.  *      for bigger and slower tape drive.
  101.  *    Added driver version printout at driver loadup time
  102.  *
  103.  * Version 1.04
  104.  *    Added code for 40 ld FW support.
  105.  *    Added new ioctl command 0x81 to support NEW_READ/WRITE_CONFIG with
  106.  *      data area greater than 4 KB, which is the upper bound for data
  107.  *      tranfer through scsi_ioctl interface.
  108.  *    The additional 32 bit field for 64bit address in the newly defined
  109.  *      mailbox64 structure is set to 0 at this point.
  110.  *
  111.  * Version 1.05
  112.  *    Changed the queing implementation for handling SCBs and completed
  113.  *      commands.
  114.  *    Added spinlocks in the interrupt service routine to enable the driver
  115.  *      function in the SMP environment.
  116.  *    Fixed the problem of unnecessary aborts in the abort entry point, which
  117.  *      also enables the driver to handle large amount of I/O requests for
  118.  *      long duration of time.
  119.  * Version 1.06
  120.  *              Intel Release
  121.  * Version 1.07
  122.  *    Removed the usage of uaccess.h file for kernel versions less than
  123.  *    2.0.36, as this file is not present in those versions.
  124.  *
  125.  * Version 108
  126.  *    Modified mega_ioctl so that 40LD megamanager would run
  127.  *    Made some changes for 2.3.XX compilation , esp wait structures
  128.  *    Code merge between 1.05 and 1.06 .
  129.  *    Bug fixed problem with ioctl interface for concurrency between
  130.  *    8ld and 40ld firwmare
  131.  *    Removed the flawed semaphore logic for handling new config command
  132.  *    Added support for building own scatter / gather list for big user
  133.  *    mode buffers
  134.  *    Added /proc file system support ,so that information is available in
  135.  *    human readable format
  136.  *
  137.  * Version 1a08
  138.  *    Changes for IA64 kernels. Checked for CONFIG_PROC_FS flag
  139.  *
  140.  * Version 1b08
  141.  *    Include file changes.
  142.  * Version 1b08b
  143.  *    Change PCI ID value for the 471 card, use #defines when searching
  144.  *    for megaraid cards.
  145.  *
  146.  * Version 1.10
  147.  *
  148.  *      I) Changes made to make following ioctl commands work in 0x81 interface
  149.  *              a)DCMD_DELETE_LOGDRV
  150.  *              b)DCMD_GET_DISK_CONFIG
  151.  *              c)DCMD_DELETE_DRIVEGROUP
  152.  *              d)NC_SUBOP_ENQUIRY3
  153.  *              e)DCMD_CHANGE_LDNO
  154.  *              f)DCMD_CHANGE_LOOPID
  155.  *              g)DCMD_FC_READ_NVRAM_CONFIG
  156.  *      h)DCMD_WRITE_CONFIG
  157.  *      II) Added mega_build_kernel_sg function
  158.  *  III)Firmware flashing option added
  159.  *
  160.  * Version 1.10a
  161.  *
  162.  *      I)Dell updates included in the source code.
  163.  *              Note:   This change is not tested due to the unavailability of IA64 kernel
  164.  *      and it is in the #ifdef DELL_MODIFICATION macro which is not defined
  165.  *
  166.  * Version 1.10b
  167.  *
  168.  *      I)In M_RD_IOCTL_CMD_NEW command the wrong way of copying the data
  169.  *    to the user address corrected
  170.  *
  171.  * Version 1.10c
  172.  *
  173.  *      I) DCMD_GET_DISK_CONFIG opcode updated for the firmware changes.
  174.  *
  175.  * Version 1.11
  176.  *      I)  Version number changed from 1.10c to 1.11
  177.  *  II) DCMD_WRITE_CONFIG(0x0D) command in the driver changed from
  178.  *      scatter/gather list mode to direct pointer mode..
  179.  *     Fixed bug of undesirably detecting HP onboard controllers which
  180.  *       are disabled.
  181.  *
  182.  *      Version 1.12 (Sep 21, 2000)
  183.  *
  184.  *     I. Changes have been made for Dynamic DMA mapping in IA64 platform.
  185.  *                To enable all these changes define M_RD_DYNAMIC_DMA_SUPPORT in megaraid.h
  186.  *        II. Got rid of windows mode comments
  187.  *       III. Removed unwanted code segments
  188.  *    IV. Fixed bug of HP onboard controller information (commented with
  189.  *                 MEGA_HP_FIX)
  190.  *
  191.  *      Version 1a12
  192.  *      I.      reboot notifier and new ioctl changes ported from 1c09
  193.  *
  194.  *      Version 1b12
  195.  *      I.      Changes in new ioctl interface routines ( Nov 06, 2000 )
  196.  *
  197.  *      Version 1c12
  198.  *      I.      Changes in new ioctl interface routines ( Nov 07, 2000 )
  199.  *
  200.  *      Version 1d12
  201.  *      I.      Compilation error under kernel 2.4.0 for 32-bit machine in mega_ioctl
  202.  *
  203.  *      Version 1e12, 1f12
  204.  *      1.  Fixes for pci_map_single, pci_alloc_consistent along with mailbox
  205.  *          alignment
  206.  *
  207.  * Version 1.13beta
  208.  * Added Support for Full 64bit address space support. If firmware
  209.  * supports 64bit, it goes to 64 bit mode even on x86 32bit 
  210.  * systems. Data Corruption Issues while running on test9 kernel
  211.  * on IA64 systems. This issue not seen on test11 on x86 system
  212.  *
  213.  * Version 1.13c
  214.  * 1. Resolved Memory Leak when using M_RD_IOCTL_CMD interface
  215.  * 2. Resolved Queuing problem when MailBox Blocks
  216.  * 3. Added unregister_reboot_notifier support
  217.  * 
  218.  * Version 1.13d
  219.  * Experimental changes in interfacing with the controller in ISR
  220.  *
  221.  * Version 1.13e
  222.  * Fixed Broken 2.2.XX compilation changes + misc changes
  223.  *
  224.  * Version 1.13f to 1.13i
  225.  * misc changes + code clean up
  226.  * Cleaned up the ioctl code and added set_mbox_xfer_addr()
  227.  * Support for START_DEV (6)
  228.  * 
  229.  * Version 1.13j
  230.  * Moved some code to megaraid.h file, replaced some hard coded values 
  231.  *      with respective macros. Changed some functions to static
  232.  *
  233.  * Version 1.13k
  234.  * Only some idendation correction to 1.13j 
  235.  *
  236.  * Version 1.13l , 1.13m, 1.13n, 1.13o
  237.  * Minor Identation changes + misc changes
  238.  *
  239.  * Version 1.13q
  240.  * Paded the new uioctl_t MIMD structure for maintaining alignment 
  241.  * and size across 32 / 64 bit platforms
  242.  * Changed the way MIMD IOCTL interface used virt_to_bus() to use pci
  243.  * memory location
  244.  *
  245.  * Version 1.13r
  246.  * 2.4.xx SCSI Changes.
  247.  *
  248.  * Version 1.13s
  249.  * Stats counter fixes
  250.  * Temporary fix for some 64 bit firmwares in 2.4.XX kernels
  251.  *
  252.  * Version 1.13t
  253.  * Support for 64bit version of READ/WRITE/VIEW DISK CONFIG
  254.  *
  255.  * Version 1.14
  256.  * Did away with MEGADEV_IOCTL flag. It is now standard part of driver
  257.  * without need for a special #define flag
  258.  * Disabled old scsi ioctl path for kernel versions > 2.3.xx. This is due
  259.  * to the nature in which the new scsi code queues a new scsi command to 
  260.  * controller during SCSI IO Completion
  261.  * Driver now checks for sub-system vendor id before taking ownership of
  262.  * the controller
  263.  *
  264.  * Version 1.14a
  265.  * Added Host re-ordering
  266.  *
  267.  * Version 1.14b
  268.  * Corrected some issue which caused the older cards not to work
  269.  *
  270.  * Version 1.14c
  271.  * IOCTL changes for not handling the non-64bit firmwares under 2.4.XX
  272.  * kernel
  273.  *
  274.  * Version 1.14d
  275.  * Fixed Various MIMD Synchronization Issues
  276.  *
  277.  * Version 1.14e
  278.  * Fixed the error handling during card initialization
  279.  *
  280.  * Version 1.14f
  281.  * Multiple invocations of mimd phase I ioctl stalls the cpu. Replaced
  282.  * spinlock with semaphore(mutex)
  283.  *
  284.  * Version 1.14g
  285.  * Fixed running out of scbs issues while running MIMD apps under heavy IO
  286.  *
  287.  * Version 1.14g-ac - 02/03/01
  288.  * Reformatted to Linux format so I could compare to old one and cross
  289.  * check bug fixes
  290.  * Re fixed the assorted missing 'static' cases
  291.  * Removed some unneeded version checks
  292.  * Cleaned up some of the VERSION checks in the code
  293.  * Left 2.0 support but removed 2.1.x support.
  294.  * Collected much of the compat glue into one spot
  295.  *
  296.  * Version 1.14g-ac2 - 22/03/01
  297.  * Fixed a non obvious dereference after free in the driver unload path
  298.  *
  299.  * Version 1.14i
  300.  * changes for making 32bit application run on IA64
  301.  *
  302.  * Version 1.14j
  303.  * Tue Mar 13 14:27:54 EST 2001 - AM
  304.  * Changes made in the driver to be able to run applications if the
  305.  * system has memory >4GB.
  306.  *
  307.  *
  308.  * Version 1.14k
  309.  * Thu Mar 15 18:38:11 EST 2001 - AM
  310.  *
  311.  * Firmware version check removed if subsysid==0x1111 and
  312.  * subsysvid==0x1111, since its not yet initialized.
  313.  *
  314.  * changes made to correctly calculate the base in mega_findCard.
  315.  *
  316.  * Driver informational messages now appear on the console as well as
  317.  * with dmesg
  318.  *
  319.  * Older ioctl interface is returned failure on newer(2.4.xx) kernels.
  320.  *
  321.  * Inclusion of "modversions.h" is still a debatable question. It is
  322.  * included anyway with this release.
  323.  *
  324.  * Version 1.14l
  325.  * Mon Mar 19 17:39:46 EST 2001 - AM
  326.  *
  327.  * Assorted changes to remove compilation error in 1.14k when compiled
  328.  * with kernel < 2.4.0
  329.  *
  330.  * Version 1.14m
  331.  * Tue Mar 27 12:09:22 EST 2001 - AM
  332.  *
  333.  * Added support for extended CDBs ( > 10 bytes ) and OBDR ( One Button
  334.  * Disaster Recovery ) feature.
  335.  *
  336.  *
  337.  * Version 1.14n
  338.  * Tue Apr 10 14:28:13 EDT 2001 - AM
  339.  *
  340.  * "modeversions.h" is no longer included in the code.
  341.  * 2.4.xx style mutex initialization used for older kernels also
  342.  *
  343.  * Version 1.14o
  344.  * Wed Apr 18 17:47:26 EDT 2001 - PJ
  345.  *
  346.  * Before returning status for 'inquiry', we first check if request buffer
  347.  * is SG list, and then return appropriate status
  348.  *
  349.  * Version 1.14p
  350.  * Wed Apr 25 13:44:48 EDT 2001 - PJ
  351.  *
  352.  * SCSI result made appropriate in case of check conditions for extended
  353.  * passthru commands
  354.  *
  355.  * Do not support lun >7 for physically accessed devices 
  356.  *
  357.  *
  358.  * Version 1.15
  359.  * Thu Apr 19 09:38:38 EDT 2001 - AM
  360.  *
  361.  * 1.14l rollover to 1.15 - merged with main trunk after 1.15d
  362.  *
  363.  * Version 1.15b
  364.  *  Wed May 16 20:10:01 EDT 2001 - AM
  365.  *
  366.  * "modeversions.h" is no longer included in the code.
  367.  * 2.4.xx style mutex initialization used for older kernels also
  368.  * Brought in-sync with Alan's changes in 2.4.4
  369.  * Note: 1.15a is on OBDR branch(main trunk), and is not merged with yet.
  370.  *
  371.  * Version 1.15c
  372.  * Mon May 21 23:10:42 EDT 2001 - AM
  373.  *
  374.  * ioctl interface uses 2.4.x conforming pci dma calls
  375.  * similar calls used for older kernels
  376.  *
  377.  * Version 1.15d
  378.  * Wed May 30 17:30:41 EDT 2001 - AM
  379.  *
  380.  * NULL is not a valid first argument for pci_alloc_consistent() on
  381.  * IA64(2.4.3-2.10.1). Code shuffling done in ioctl interface to get
  382.  * "pci_dev" before making calls to pci interface routines.
  383.  *
  384.  * Version 1.16pre
  385.  * Fri Jun  1 19:40:48 EDT 2001 - AM
  386.  *
  387.  * 1.14p and 1.15d merged
  388.  * ROMB support added
  389.  *
  390.  * Version 1.16-pre1
  391.  * Mon Jun  4 15:01:01 EDT 2001 - AM
  392.  *
  393.  * Non-ROMB firmware do no DMA support 0xA9 command. Value 0xFF
  394.  * (all channels are raid ) is chosen for those firmware.
  395.  *
  396.  * Version 1.16-pre2
  397.  * Mon Jun 11 18:15:31 EDT 2001 - AM
  398.  *
  399.  * Changes for boot from any logical drive
  400.  *
  401.  * Version 1.16
  402.  * Tue Jun 26 18:07:02 EDT 2001 - AM
  403.  *
  404.  * branched at 1.14p
  405.  *
  406.  * Check added for HP 1M/2M controllers if having firmware H.01.07 or
  407.  * H.01.08. If found, disable 64 bit support since these firmware have
  408.  * limitations for 64 bit addressing
  409.  *
  410.  *
  411.  * Version 1.17
  412.  * Thu Jul 12 11:14:09 EDT 2001 - AM
  413.  *
  414.  * 1.16pre2 and 1.16 merged.
  415.  *
  416.  * init_MUTEX and init_MUTEX_LOCKED are defined in 2.2.19. Pre-processor
  417.  * statements are added for them
  418.  *
  419.  * Linus's 2.4.7pre3 kernel introduces a new field 'max_sectors' in Scsi_Host
  420.  * structure, to improve IO performance.
  421.  *
  422.  *
  423.  * Version 1.17a
  424.  * Fri Jul 13 18:44:01 EDT 2001 - AM
  425.  *
  426.  * Starting from kernel 2.4.x, LUN is not < 8 - following SCSI-III. So to have
  427.  * our current formula working to calculate logical drive number, return
  428.  * failure for LUN > 7
  429.  *
  430.  *
  431.  * Version 1.17b
  432.  * Mon Jul 30 19:24:02 EDT 2001 - AM
  433.  *
  434.  * Added support for random deletion of logical drives
  435.  *
  436.  * Version 1.17c
  437.  * Tue Sep 25 09:37:49 EDT 2001 - Atul Mukker <atulm@lsil.com>
  438.  *
  439.  * With single and dual channel controllers, some virtaul channels are
  440.  * displayed negative.
  441.  *
  442.  * Version 1.17a-ac
  443.  * Mon Aug 6 14:59:29 BST 2001 - "Michael Johnson" <johnsom@home.com>
  444.  *
  445.  * Make the HP print formatting and check for buggy firmware runtime not
  446.  * ifdef dependant.
  447.  *
  448.  *
  449.  * Version 1.17d
  450.  * Thu Oct 11 10:48:45 EDT 2001 - Atul Mukker <atulm@lsil.com>
  451.  *
  452.  * Driver 1.17c oops when loaded without controller.
  453.  *
  454.  * Special case for "use_sg == 1" removed while building the scatter gather
  455.  * list.
  456.  *
  457.  * Version 1.18
  458.  * Thu Oct 11 15:02:53 EDT 2001 - Atul Mukker <atulm@lsil.com>
  459.  *
  460.  * References to AMI have been changed to LSI Logic.
  461.  *
  462.  *
  463.  * BUGS:
  464.  *     Some older 2.1 kernels (eg. 2.1.90) have a bug in pci.c that
  465.  *     fails to detect the controller as a pci device on the system.
  466.  *
  467.  *     Timeout period for upper scsi layer, i.e. SD_TIMEOUT in
  468.  *     /drivers/scsi/sd.c, is too short for this controller. SD_TIMEOUT
  469.  *     value must be increased to (30 * HZ) otherwise false timeouts
  470.  *     will occur in the upper layer.
  471.  *
  472.  *     Never set skip_id. The existing PCI code the megaraid uses fails
  473.  *     to properly check the vendor subid in some cases. Setting this then
  474.  *     makes it steal other i960's and crashes some boxes
  475.  *
  476.  *     Far too many ifdefs for versions.
  477.  *
  478.  *===================================================================*/
  479. #include <linux/config.h>
  480. #include <linux/version.h>
  481. #include <linux/module.h>
  482. #include <linux/types.h>
  483. #include <linux/errno.h>
  484. #include <linux/string.h>
  485. #include <linux/kernel.h>
  486. #include <linux/ioport.h>
  487. #include <linux/fcntl.h>
  488. #include <linux/delay.h>
  489. #include <linux/pci.h>
  490. #include <linux/proc_fs.h>
  491. #include <linux/blk.h>
  492. #include <linux/wait.h>
  493. #include <linux/tqueue.h>
  494. #include <linux/interrupt.h>
  495. #include <linux/mm.h>
  496. #include <asm/pgtable.h>
  497. #include <linux/sched.h>
  498. #include <linux/stat.h>
  499. #include <linux/slab.h> /* for kmalloc() */
  500. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0) /* 0x20100 */
  501. #include <linux/bios32.h>
  502. #else
  503. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0) /* 0x20300 */
  504. #include <asm/spinlock.h>
  505. #else
  506. #include <linux/spinlock.h>
  507. #endif
  508. #endif
  509. #include <asm/io.h>
  510. #include <asm/irq.h>
  511. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,0,24) /* 0x020024 */
  512. #include <asm/uaccess.h>
  513. #endif
  514. /*
  515.  * These header files are required for Shutdown Notification routines
  516.  */
  517. #include <linux/notifier.h>
  518. #include <linux/reboot.h>
  519. #include <linux/init.h>
  520. #include "sd.h"
  521. #include "scsi.h"
  522. #include "hosts.h"
  523. #include "megaraid.h"
  524. /*
  525.  *================================================================
  526.  *  #Defines
  527.  *================================================================
  528.  */
  529. #define MAX_SERBUF 160
  530. #define COM_BASE 0x2f8
  531. static ulong RDINDOOR (mega_host_config * megaCfg)
  532. {
  533. return readl (megaCfg->base + 0x20);
  534. }
  535. static void WRINDOOR (mega_host_config * megaCfg, ulong value)
  536. {
  537. writel (value, megaCfg->base + 0x20);
  538. }
  539. static ulong RDOUTDOOR (mega_host_config * megaCfg)
  540. {
  541. return readl (megaCfg->base + 0x2C);
  542. }
  543. static void WROUTDOOR (mega_host_config * megaCfg, ulong value)
  544. {
  545. writel (value, megaCfg->base + 0x2C);
  546. }
  547. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) /* 0x020200 */
  548. #include <linux/smp.h>
  549. #define cpuid smp_processor_id()
  550. #endif
  551. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
  552. #define scsi_set_pci_device(x,y)
  553. #endif
  554. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) /* 0x020400 */
  555. /*
  556.  * Linux 2.4 and higher
  557.  *
  558.  * No driver private lock
  559.  * Use the io_request_lock not cli/sti
  560.  * queue task is a simple api without irq forms
  561.  */
  562. MODULE_AUTHOR ("LSI Logic Corporation");
  563. MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
  564. MODULE_LICENSE ("GPL");
  565. #define DRIVER_LOCK_T
  566. #define DRIVER_LOCK_INIT(p)
  567. #define DRIVER_LOCK(p)
  568. #define DRIVER_UNLOCK(p)
  569. #define IO_LOCK_T unsigned long io_flags = 0
  570. #define IO_LOCK spin_lock_irqsave(&io_request_lock,io_flags);
  571. #define IO_UNLOCK spin_unlock_irqrestore(&io_request_lock,io_flags);
  572. #define queue_task_irq(a,b)     queue_task(a,b)
  573. #define queue_task_irq_off(a,b) queue_task(a,b)
  574. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) /* 0x020200 */
  575. /*
  576.  * Linux 2.2 and higher
  577.  *
  578.  * No driver private lock
  579.  * Use the io_request_lock not cli/sti
  580.  * No pci region api
  581.  * queue_task is now a single simple API
  582.  */
  583. static char kernel_version[] = UTS_RELEASE;
  584. MODULE_AUTHOR ("LSI Logic Corporation");
  585. MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
  586. #define DRIVER_LOCK_T
  587. #define DRIVER_LOCK_INIT(p)
  588. #define DRIVER_LOCK(p)
  589. #define DRIVER_UNLOCK(p)
  590. #define IO_LOCK_T unsigned long io_flags = 0
  591. #define IO_LOCK spin_lock_irqsave(&io_request_lock,io_flags);
  592. #define IO_UNLOCK spin_unlock_irqrestore(&io_request_lock,io_flags);
  593. #define pci_free_consistent(a,b,c,d)
  594. #define pci_unmap_single(a,b,c,d)
  595. #define pci_enable_device(x) (0)
  596. #define queue_task_irq(a,b)     queue_task(a,b)
  597. #define queue_task_irq_off(a,b) queue_task(a,b)
  598. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,2,19) /* 0x020219 */
  599. #define init_MUTEX_LOCKED(x)    (*(x)=MUTEX_LOCKED)
  600. #define init_MUTEX(x)           (*(x)=MUTEX)
  601. #define DECLARE_WAIT_QUEUE_HEAD(x) struct wait_queue *x = NULL
  602. #endif
  603. #else
  604. /*
  605.  * Linux 2.0 macros. Here we have to provide some of our own
  606.  * functionality. We also only work little endian 32bit.
  607.  * Again no pci_alloc/free api
  608.  * IO_LOCK/IO_LOCK_T were never used in 2.0 so now are empty 
  609.  */
  610.  
  611. #define cpuid 0
  612. #define DRIVER_LOCK_T long cpu_flags;
  613. #define DRIVER_LOCK_INIT(p)
  614. #define DRIVER_LOCK(p) 
  615.         save_flags(cpu_flags); 
  616.         cli();
  617. #define DRIVER_UNLOCK(p) 
  618.         restore_flags(cpu_flags);
  619. #define IO_LOCK_T
  620. #define IO_LOCK(p)
  621. #define IO_UNLOCK(p)
  622. #define le32_to_cpu(x) (x)
  623. #define cpu_to_le32(x) (x)
  624. #define pci_free_consistent(a,b,c,d)
  625. #define pci_unmap_single(a,b,c,d)
  626. #define init_MUTEX_LOCKED(x)    (*(x)=MUTEX_LOCKED)
  627. #define init_MUTEX(x)           (*(x)=MUTEX)
  628. #define pci_enable_device(x) (0)
  629. /*
  630.  * 2.0 lacks spinlocks, iounmap/ioremap
  631.  */
  632. #define ioremap vremap
  633. #define iounmap vfree
  634.  /* simulate spin locks */
  635. typedef struct {
  636. volatile char lock;
  637. } spinlock_t;
  638. #define spin_lock_init(x) { (x)->lock = 0;}
  639. #define spin_lock_irqsave(x,flags) { while ((x)->lock) barrier();
  640.                                         (x)->lock=1; save_flags(flags);
  641.                                         cli();}
  642. #define spin_unlock_irqrestore(x,flags) { (x)->lock=0; restore_flags(flags);}
  643. #define DECLARE_WAIT_QUEUE_HEAD(x) struct wait_queue *x = NULL
  644. #endif
  645. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) /* 0x020400 */
  646. #define dma_alloc_consistent pci_alloc_consistent
  647. #define dma_free_consistent pci_free_consistent
  648. #else
  649. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,2,19) /* 0x020219 */
  650. typedef unsigned long dma_addr_t;
  651. #endif
  652. void *dma_alloc_consistent(void *, size_t, dma_addr_t *);
  653. void dma_free_consistent(void *, size_t, void *, dma_addr_t);
  654. int mega_get_order(int);
  655. int pow_2(int);
  656. #endif
  657. /* set SERDEBUG to 1 to enable serial debugging */
  658. #define SERDEBUG 0
  659. #if SERDEBUG
  660. static void ser_init (void);
  661. static void ser_puts (char *str);
  662. static void ser_putc (char c);
  663. static int ser_printk (const char *fmt, ...);
  664. #endif
  665. #ifdef CONFIG_PROC_FS
  666. #define COPY_BACK if (offset > megaCfg->procidx) { 
  667. *eof = TRUE; 
  668.         megaCfg->procidx = 0; 
  669.         megaCfg->procbuf[0] = 0; 
  670.         return 0;} 
  671.  if ((count + offset) > megaCfg->procidx) { 
  672.       count = megaCfg->procidx - offset; 
  673.       *eof = TRUE; } 
  674.       memcpy(page, &megaCfg->procbuf[offset], count); 
  675.       megaCfg->procidx = 0; 
  676.       megaCfg->procbuf[0] = 0;
  677. #endif
  678. /*
  679.  * ================================================================
  680.  *                    Global variables
  681.  *================================================================
  682.  */
  683. /*  Use "megaraid=skipXX" as LILO option to prohibit driver from scanning
  684.     XX scsi id on each channel.  Used for Madrona motherboard, where SAF_TE
  685.     processor id cannot be scanned */
  686. static char *megaraid;
  687. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,0) /* 0x20100 */
  688. #ifdef MODULE
  689. MODULE_PARM (megaraid, "s");
  690. #endif
  691. #endif
  692. static int skip_id = -1;
  693. static int numCtlrs = 0;
  694. static mega_host_config *megaCtlrs[FC_MAX_CHANNELS] = { 0 };
  695. static struct proc_dir_entry *mega_proc_dir_entry;
  696. #if DEBUG
  697. static u32 maxCmdTime = 0;
  698. #endif
  699. static mega_scb *pLastScb = NULL;
  700. static struct notifier_block mega_notifier = {
  701. megaraid_reboot_notify,
  702. NULL,
  703. 0
  704. };
  705. /* For controller re-ordering */
  706. struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  707. /*
  708.  * The File Operations structure for the serial/ioctl interface of the driver
  709.  */
  710. /* For controller re-ordering */ 
  711. static struct file_operations megadev_fops = {
  712. ioctl:megadev_ioctl_entry,
  713. open:megadev_open,
  714. release:megadev_close,
  715. };
  716. /*
  717.  * Array to structures for storing the information about the controllers. This
  718.  * information is sent to the user level applications, when they do an ioctl
  719.  * for this information.
  720.  */
  721. static struct mcontroller mcontroller[MAX_CONTROLLERS];
  722. /* The current driver version */
  723. static u32 driver_ver = 114;
  724. /* major number used by the device for character interface */
  725. static int major;
  726. static struct semaphore mimd_ioctl_sem;
  727. static struct semaphore mimd_entry_mtx;
  728. #if SERDEBUG
  729. volatile static spinlock_t serial_lock;
  730. #endif
  731. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) /* 0x20300 */
  732. static struct proc_dir_entry proc_scsi_megaraid = {
  733. PROC_SCSI_MEGARAID, 8, "megaraid",
  734. S_IFDIR | S_IRUGO | S_IXUGO, 2
  735. };
  736. #endif
  737. #ifdef CONFIG_PROC_FS
  738. extern struct proc_dir_entry proc_root;
  739. #endif
  740. static char mega_ch_class; /* channels are raid or scsi */
  741. #define IS_RAID_CH(ch) ( (mega_ch_class >> (ch)) & 0x01 )
  742. #if SERDEBUG
  743. static char strbuf[MAX_SERBUF + 1];
  744. static void ser_init (void)
  745. {
  746. unsigned port = COM_BASE;
  747. outb (0x80, port + 3);
  748. outb (0, port + 1);
  749. /* 9600 Baud, if 19200: outb(6,port) */
  750. outb (12, port);
  751. outb (3, port + 3);
  752. outb (0, port + 1);
  753. }
  754. static void ser_puts (char *str)
  755. {
  756. char *ptr;
  757. ser_init ();
  758. for (ptr = str; *ptr; ++ptr)
  759. ser_putc (*ptr);
  760. }
  761. static void ser_putc (char c)
  762. {
  763. unsigned port = COM_BASE;
  764. while ((inb (port + 5) & 0x20) == 0) ;
  765. outb (c, port);
  766. if (c == 0x0a) {
  767. while ((inb (port + 5) & 0x20) == 0) ;
  768. outb (0x0d, port);
  769. }
  770. }
  771. static int ser_printk (const char *fmt, ...)
  772. {
  773. va_list args;
  774. int i;
  775. long flags;
  776. spin_lock_irqsave (&serial_lock, flags);
  777. va_start (args, fmt);
  778. i = vsprintf (strbuf, fmt, args);
  779. ser_puts (strbuf);
  780. va_end (args);
  781. spin_unlock_irqrestore (&serial_lock, flags);
  782. return i;
  783. }
  784. #define TRACE(a)    { ser_printk a;}
  785. #else
  786. #define TRACE(A)
  787. #endif
  788. #define TRACE1(a)
  789. static void callDone (Scsi_Cmnd * SCpnt)
  790. {
  791. if (SCpnt->result) {
  792. TRACE (("*** %.08lx %.02x <%d.%d.%d> = %xn",
  793. SCpnt->serial_number, SCpnt->cmnd[0], SCpnt->channel,
  794. SCpnt->target, SCpnt->lun, SCpnt->result));
  795. }
  796. SCpnt->scsi_done (SCpnt);
  797. }
  798. /*-------------------------------------------------------------------------
  799.  *
  800.  *                      Local functions
  801.  *
  802.  *-------------------------------------------------------------------------*/
  803. /*=======================
  804.  * Free a SCB structure
  805.  *=======================
  806.  */
  807. static void mega_freeSCB (mega_host_config * megaCfg, mega_scb * pScb)
  808. {
  809. mega_scb *pScbtmp;
  810. if ((pScb == NULL) || (pScb->idx >= 0xFE)) {
  811. return;
  812. }
  813. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  814. switch (pScb->dma_type) {
  815. case M_RD_DMA_TYPE_NONE:
  816. break;
  817. case M_RD_PTHRU_WITH_BULK_DATA:
  818. pci_unmap_single (megaCfg->dev, pScb->dma_h_bulkdata,
  819.   pScb->pthru->dataxferlen,
  820.   pScb->dma_direction);
  821. break;
  822. case M_RD_EPTHRU_WITH_BULK_DATA:
  823. pci_unmap_single (megaCfg->dev, pScb->dma_h_bulkdata,
  824.   pScb->epthru->dataxferlen,
  825.   pScb->dma_direction);
  826. break;
  827. case M_RD_PTHRU_WITH_SGLIST:
  828. {
  829. int count;
  830. for (count = 0; count < pScb->sglist_count; count++) {
  831. pci_unmap_single (megaCfg->dev,
  832.   pScb->dma_h_sglist[count],
  833.   pScb->sgList[count].length,
  834.   pScb->dma_direction);
  835. }
  836. break;
  837. }
  838. case M_RD_BULK_DATA_ONLY:
  839. pci_unmap_single (megaCfg->dev,
  840.   pScb->dma_h_bulkdata,
  841.   pScb->iDataSize, pScb->dma_direction);
  842. break;
  843. case M_RD_SGLIST_ONLY:
  844. pci_unmap_sg (megaCfg->dev,
  845.       pScb->SCpnt->request_buffer,
  846.       pScb->SCpnt->use_sg, pScb->dma_direction);
  847. break;
  848. default:
  849. break;
  850. }
  851. #endif
  852. /* Unlink from pending queue */
  853. if (pScb == megaCfg->qPendingH) {
  854. if (megaCfg->qPendingH == megaCfg->qPendingT)
  855. megaCfg->qPendingH = megaCfg->qPendingT = NULL;
  856. else
  857. megaCfg->qPendingH = megaCfg->qPendingH->next;
  858. megaCfg->qPcnt--;
  859. } else {
  860. for (pScbtmp = megaCfg->qPendingH; pScbtmp;
  861.      pScbtmp = pScbtmp->next) {
  862. if (pScbtmp->next == pScb) {
  863. pScbtmp->next = pScb->next;
  864. if (pScb == megaCfg->qPendingT) {
  865. megaCfg->qPendingT = pScbtmp;
  866. }
  867. megaCfg->qPcnt--;
  868. break;
  869. }
  870. }
  871. }
  872. /* Link back into free list */
  873. pScb->state = SCB_FREE;
  874. pScb->SCpnt = NULL;
  875. if (megaCfg->qFreeH == (mega_scb *) NULL) {
  876. megaCfg->qFreeH = megaCfg->qFreeT = pScb;
  877. } else {
  878. megaCfg->qFreeT->next = pScb;
  879. megaCfg->qFreeT = pScb;
  880. }
  881. megaCfg->qFreeT->next = NULL;
  882. megaCfg->qFcnt++;
  883. }
  884. /*===========================
  885.  * Allocate a SCB structure
  886.  *===========================
  887.  */
  888. static mega_scb *mega_allocateSCB (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt)
  889. {
  890. mega_scb *pScb;
  891. /* Unlink command from Free List */
  892. if ((pScb = megaCfg->qFreeH) != NULL) {
  893. megaCfg->qFreeH = pScb->next;
  894. megaCfg->qFcnt--;
  895. pScb->isrcount = jiffies;
  896. pScb->next = NULL;
  897. pScb->state = SCB_ACTIVE;
  898. pScb->SCpnt = SCpnt;
  899. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  900. pScb->dma_type = M_RD_DMA_TYPE_NONE;
  901. #endif
  902. return pScb;
  903. }
  904. printk (KERN_WARNING "Megaraid: Could not allocate free SCB!!!n");
  905. return NULL;
  906. }
  907. /* Run through the list of completed requests  and finish it */
  908. static void mega_rundoneq (mega_host_config * megaCfg)
  909. {
  910. Scsi_Cmnd *SCpnt;
  911. while ((SCpnt = megaCfg->qCompletedH) != NULL) {
  912. megaCfg->qCompletedH = (Scsi_Cmnd *) SCpnt->host_scribble;
  913. megaCfg->qCcnt--;
  914. SCpnt->host_scribble = (unsigned char *) NULL; /* XC : sep 14 */
  915. /* Callback */
  916. callDone (SCpnt);
  917. }
  918. megaCfg->qCompletedH = megaCfg->qCompletedT = NULL;
  919. }
  920. /*
  921.  * Runs through the list of pending requests
  922.  * Assumes that mega_lock spin_lock has been acquired.
  923.  */
  924. static int mega_runpendq (mega_host_config * megaCfg)
  925. {
  926. mega_scb *pScb;
  927. int rc;
  928. /* Issue any pending commands to the card */
  929. for (pScb = megaCfg->qPendingH; pScb; pScb = pScb->next) {
  930. if (pScb->state == SCB_ACTIVE) {
  931. if ((rc =
  932.      megaIssueCmd (megaCfg, pScb->mboxData, pScb, 1)) == -1)
  933. return rc;
  934. }
  935. }
  936. return 0;
  937. }
  938. /* Add command to the list of completed requests */
  939. static void mega_cmd_done (mega_host_config * megaCfg, mega_scb * pScb, int status)
  940. {
  941. int islogical;
  942. Scsi_Cmnd *SCpnt;
  943. mega_passthru *pthru;
  944. mega_ext_passthru *epthru;
  945. mega_mailbox *mbox;
  946. struct scatterlist *sgList;
  947. u8 c;
  948. if (pScb == NULL) {
  949. TRACE (("NULL pScb in mega_cmd_done!"));
  950. printk(KERN_CRIT "NULL pScb in mega_cmd_done!");
  951. }
  952. SCpnt = pScb->SCpnt;
  953. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  954. pthru = pScb->pthru;
  955. epthru = pScb->epthru;
  956. #else
  957. pthru = &pScb->pthru;
  958. epthru = &pScb->epthru;
  959. #endif
  960. mbox = (mega_mailbox *) & pScb->mboxData;
  961. if (SCpnt == NULL) {
  962. TRACE (("NULL SCpnt in mega_cmd_done!"));
  963. TRACE (("pScb->idx = ", pScb->idx));
  964. TRACE (("pScb->state = ", pScb->state));
  965. TRACE (("pScb->state = ", pScb->state));
  966. panic(KERN_ERR "megaraid:Problem...!n");
  967. }
  968. islogical = ( (SCpnt->channel >= megaCfg->productInfo.SCSIChanPresent) &&
  969. (SCpnt->channel <= megaCfg->host->max_channel) );
  970. #if 0
  971. islogical = (SCpnt->channel == megaCfg->host->max_channel);
  972. #endif
  973. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  974. /* Special Case to handle PassThrough->XferAddrress > 4GB */
  975. switch (SCpnt->cmnd[0]) {
  976. case INQUIRY:
  977. case READ_CAPACITY:
  978. memcpy (SCpnt->request_buffer,
  979. pScb->bounce_buffer, SCpnt->request_bufflen);
  980. break;
  981. }
  982. #endif
  983. mega_freeSCB (megaCfg, pScb);
  984. /*
  985.  * Do not return the presence of hard disk on the channel so, inquiry
  986.  * sent, and returned data==hard disk or removable hard disk and not
  987.  * logical, request should return failure! - PJ
  988.  */
  989. #if 0
  990. if (SCpnt->cmnd[0] == INQUIRY && ((((u_char *) SCpnt->request_buffer)[0] & 0x1F) == TYPE_DISK) && !islogical) {
  991. status = 0xF0;
  992. }
  993. #endif
  994. if (SCpnt->cmnd[0] == INQUIRY && !islogical) {
  995. if ( SCpnt->use_sg ) {
  996. sgList = (struct scatterlist *)SCpnt->request_buffer;
  997. memcpy(&c, sgList[0].address, 0x1);
  998. } else {
  999. memcpy(&c, SCpnt->request_buffer, 0x1);
  1000. }
  1001. #if 0
  1002. if( (c & 0x1F ) == TYPE_DISK ) {
  1003. status = 0xF0;
  1004. }
  1005. #endif
  1006. if( IS_RAID_CH(SCpnt->channel) && ((c & 0x1F ) == TYPE_DISK) ) {
  1007. status = 0xF0;
  1008. }
  1009. }
  1010. /* clear result; otherwise, success returns corrupt value */
  1011. SCpnt->result = 0;
  1012. if ((SCpnt->cmnd[0] & M_RD_IOCTL_CMD)) { /* i.e. ioctl cmd such as M_RD_IOCTL_CMD, M_RD_IOCTL_CMD_NEW of megamgr */
  1013. switch (status) {
  1014. case 2:
  1015. case 0xF0:
  1016. case 0xF4:
  1017. SCpnt->result = (DID_BAD_TARGET << 16) | status;
  1018. break;
  1019. default:
  1020. SCpnt->result |= status;
  1021. } /*end of switch */
  1022. } else {
  1023. /* Convert MegaRAID status to Linux error code */
  1024. switch (status) {
  1025. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1026. SCpnt->result |= (DID_OK << 16);
  1027. break;
  1028. case 0x02: /* ERROR_ABORTED, i.e. SCSI_STATUS_CHECK_CONDITION */
  1029. /*set sense_buffer and result fields */
  1030. if (mbox->cmd == MEGA_MBOXCMD_PASSTHRU) {
  1031. memcpy (SCpnt->sense_buffer, pthru->reqsensearea, 14);
  1032. } else if (mbox->cmd == MEGA_MBOXCMD_EXTPASSTHRU) {
  1033. SCpnt->result = (DRIVER_SENSE << 24) | (DID_OK << 16) | (CHECK_CONDITION << 1);
  1034. memcpy(
  1035. SCpnt->sense_buffer,
  1036. epthru->reqsensearea, 14
  1037. );
  1038. SCpnt->result = (DRIVER_SENSE << 24) | (DID_OK << 16) | (CHECK_CONDITION << 1);
  1039. /*SCpnt->result =
  1040. (DRIVER_SENSE << 24) |
  1041. (DID_ERROR << 16) | status;*/
  1042. } else {
  1043. SCpnt->sense_buffer[0] = 0x70;
  1044. SCpnt->sense_buffer[2] = ABORTED_COMMAND;
  1045. SCpnt->result |= (CHECK_CONDITION << 1);
  1046. }
  1047. break;
  1048. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e. SCSI_STATUS_BUSY */
  1049. SCpnt->result |= (DID_BUS_BUSY << 16) | status;
  1050. break;
  1051. default:
  1052. SCpnt->result |= (DID_BAD_TARGET << 16) | status;
  1053. break;
  1054. }
  1055. }
  1056. /* Add Scsi_Command to end of completed queue */
  1057. if (megaCfg->qCompletedH == NULL) {
  1058. megaCfg->qCompletedH = megaCfg->qCompletedT = SCpnt;
  1059. } else {
  1060. megaCfg->qCompletedT->host_scribble = (unsigned char *) SCpnt;
  1061. megaCfg->qCompletedT = SCpnt;
  1062. }
  1063. megaCfg->qCompletedT->host_scribble = (unsigned char *) NULL;
  1064. megaCfg->qCcnt++;
  1065. }
  1066. /*-------------------------------------------------------------------
  1067.  *
  1068.  *                 Build a SCB from a Scsi_Cmnd
  1069.  *
  1070.  * Returns a SCB pointer, or NULL
  1071.  * If NULL is returned, the scsi_done function MUST have been called
  1072.  *
  1073.  *-------------------------------------------------------------------*/
  1074. static mega_scb *mega_build_cmd (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt)
  1075. {
  1076. mega_scb *pScb;
  1077. mega_mailbox *mbox;
  1078. mega_passthru *pthru;
  1079. mega_ext_passthru *epthru;
  1080. long seg;
  1081. char islogical;
  1082. int lun = SCpnt->lun;
  1083. int max_lun;
  1084. if ((SCpnt->cmnd[0] == MEGADEVIOC))
  1085. return megadev_doioctl (megaCfg, SCpnt);
  1086. if ((SCpnt->cmnd[0] == M_RD_IOCTL_CMD)
  1087.     || (SCpnt->cmnd[0] == M_RD_IOCTL_CMD_NEW))
  1088. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)  
  1089. return mega_ioctl (megaCfg, SCpnt); /* Handle IOCTL command */
  1090. #else
  1091. {
  1092. printk(KERN_WARNING "megaraid ioctl: older interface - "
  1093. "not supported.n");
  1094. return NULL;
  1095. }
  1096. #endif
  1097. islogical = ( (SCpnt->channel >= megaCfg->productInfo.SCSIChanPresent) &&
  1098. (SCpnt->channel <= megaCfg->host->max_channel) );
  1099. #if 0
  1100. islogical = (IS_RAID_CH(SCpnt->channel) && /* virtual ch is raid - AM */
  1101. (SCpnt->channel == megaCfg->host->max_channel));
  1102. #endif
  1103. if ( ! megaCfg->support_ext_cdb ) {
  1104. if (!islogical && lun != 0) {
  1105. SCpnt->result = (DID_BAD_TARGET << 16);
  1106. callDone (SCpnt);
  1107. return NULL;
  1108. }
  1109. }
  1110. if (!islogical && SCpnt->target == skip_id) {
  1111. SCpnt->result = (DID_BAD_TARGET << 16);
  1112. callDone (SCpnt);
  1113. return NULL;
  1114. }
  1115. if (islogical) {
  1116. /* have just LUN 0 for each target on virtual channels */
  1117. if( SCpnt->lun != 0 ) {
  1118. SCpnt->result = (DID_BAD_TARGET << 16);
  1119. callDone (SCpnt);
  1120. return NULL;
  1121. }
  1122. lun = mega_get_lun(megaCfg, SCpnt);
  1123.     max_lun = (megaCfg->flag & BOARD_40LD) ?
  1124. FC_MAX_LOGICAL_DRIVES : MAX_LOGICAL_DRIVES;
  1125.  /*
  1126.   * max_lun increases by 0x80 if some logical drive was deleted.
  1127.   */
  1128. if(megaCfg->read_ldidmap) {
  1129. max_lun += 0x80;
  1130. }
  1131. if( lun > max_lun ) {
  1132. SCpnt->result = (DID_BAD_TARGET << 16);
  1133. callDone (SCpnt);
  1134. return NULL;
  1135. }
  1136. /*
  1137.  * If we have a logical drive with boot enabled, project it first
  1138.  */
  1139. if( megaCfg->boot_ldrv_enabled ) {
  1140. if( lun == 0 ) {
  1141. lun = megaCfg->boot_ldrv;
  1142. }
  1143. else {
  1144. if( lun <= megaCfg->boot_ldrv ) {
  1145. lun--;
  1146. }
  1147. }
  1148. }
  1149. } else {
  1150. if ( lun > 7) {
  1151. /* Do not support lun >7 for physically accessed devices */
  1152. SCpnt->result = (DID_BAD_TARGET << 16);
  1153. callDone (SCpnt);
  1154. return NULL;
  1155. }
  1156. }
  1157. /*-----------------------------------------------------
  1158.  *
  1159.  *               Logical drive commands
  1160.  *
  1161.  *-----------------------------------------------------*/
  1162. if (islogical) {
  1163. switch (SCpnt->cmnd[0]) {
  1164. case TEST_UNIT_READY:
  1165. memset (SCpnt->request_buffer, 0, SCpnt->request_bufflen);
  1166. SCpnt->result = (DID_OK << 16);
  1167. callDone (SCpnt);
  1168. return NULL;
  1169. case MODE_SENSE:
  1170. memset (SCpnt->request_buffer, 0, SCpnt->cmnd[4]);
  1171. SCpnt->result = (DID_OK << 16);
  1172. callDone (SCpnt);
  1173. return NULL;
  1174. case READ_CAPACITY:
  1175. case INQUIRY:
  1176. /* Allocate a SCB and initialize passthru */
  1177. if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
  1178. SCpnt->result = (DID_ERROR << 16);
  1179. callDone (SCpnt);
  1180. return NULL;
  1181. }
  1182. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1183. pthru = pScb->pthru;
  1184. #else
  1185. pthru = &pScb->pthru;
  1186. #endif
  1187. mbox = (mega_mailbox *) & pScb->mboxData;
  1188. memset (mbox, 0, sizeof (pScb->mboxData));
  1189. memset (pthru, 0, sizeof (mega_passthru));
  1190. pthru->timeout = 0;
  1191. pthru->ars = 1;
  1192. pthru->reqsenselen = 14;
  1193. pthru->islogical = 1;
  1194. pthru->logdrv = lun;
  1195. pthru->cdblen = SCpnt->cmd_len;
  1196. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1197. /*Not sure about the direction */
  1198. pScb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  1199. pScb->dma_type = M_RD_PTHRU_WITH_BULK_DATA;
  1200. #if 0
  1201. /* Normal Code w/o the need for bounce buffer */
  1202. pScb->dma_h_bulkdata
  1203.     = pci_map_single (megaCfg->dev,
  1204.       SCpnt->request_buffer,
  1205.       SCpnt->request_bufflen,
  1206.       pScb->dma_direction);
  1207. pthru->dataxferaddr = pScb->dma_h_bulkdata;
  1208. #else
  1209. /* Special Code to use bounce buffer for READ_CAPA/INQ */
  1210. pthru->dataxferaddr = pScb->dma_bounce_buffer;
  1211. pScb->dma_type = M_RD_DMA_TYPE_NONE;
  1212. #endif
  1213. #else
  1214. pthru->dataxferaddr =
  1215.     virt_to_bus (SCpnt->request_buffer);
  1216. #endif
  1217. pthru->dataxferlen = SCpnt->request_bufflen;
  1218. memcpy (pthru->cdb, SCpnt->cmnd, SCpnt->cmd_len);
  1219. /* Initialize mailbox area */
  1220. mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
  1221. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1222. mbox->xferaddr = pScb->dma_passthruhandle64;
  1223. TRACE1 (("M_RD_PTHRU_WITH_BULK_DATA Enabled n"));
  1224. #else
  1225. mbox->xferaddr = virt_to_bus (pthru);
  1226. #endif
  1227. return pScb;
  1228. case READ_6:
  1229. case WRITE_6:
  1230. case READ_10:
  1231. case WRITE_10:
  1232. /* Allocate a SCB and initialize mailbox */
  1233. if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
  1234. SCpnt->result = (DID_ERROR << 16);
  1235. callDone (SCpnt);
  1236. return NULL;
  1237. }
  1238. mbox = (mega_mailbox *) & pScb->mboxData;
  1239. memset (mbox, 0, sizeof (pScb->mboxData));
  1240. mbox->logdrv = lun;
  1241. if (megaCfg->flag & BOARD_64BIT) {
  1242. mbox->cmd = (*SCpnt->cmnd == READ_6
  1243.      || *SCpnt->cmnd ==
  1244.      READ_10) ? MEGA_MBOXCMD_LREAD64 :
  1245.     MEGA_MBOXCMD_LWRITE64;
  1246. } else {
  1247. mbox->cmd = (*SCpnt->cmnd == READ_6
  1248.      || *SCpnt->cmnd ==
  1249.      READ_10) ? MEGA_MBOXCMD_LREAD :
  1250.     MEGA_MBOXCMD_LWRITE;
  1251. }
  1252. /* 6-byte */
  1253. if (*SCpnt->cmnd == READ_6 || *SCpnt->cmnd == WRITE_6) {
  1254. mbox->numsectors = (u32) SCpnt->cmnd[4];
  1255. mbox->lba =
  1256.     ((u32) SCpnt->cmnd[1] << 16) |
  1257.     ((u32) SCpnt->cmnd[2] << 8) |
  1258.     (u32) SCpnt->cmnd[3];
  1259. mbox->lba &= 0x1FFFFF;
  1260. if (*SCpnt->cmnd == READ_6) {
  1261. megaCfg->nReads[(int) lun]++;
  1262. megaCfg->nReadBlocks[(int) lun] +=
  1263.     mbox->numsectors;
  1264. } else {
  1265. megaCfg->nWrites[(int) lun]++;
  1266. megaCfg->nWriteBlocks[(int) lun] +=
  1267.     mbox->numsectors;
  1268. }
  1269. }
  1270. /* 10-byte */
  1271. if (*SCpnt->cmnd == READ_10 || *SCpnt->cmnd == WRITE_10) {
  1272. mbox->numsectors =
  1273.     (u32) SCpnt->cmnd[8] |
  1274.     ((u32) SCpnt->cmnd[7] << 8);
  1275. mbox->lba =
  1276.     ((u32) SCpnt->cmnd[2] << 24) |
  1277.     ((u32) SCpnt->cmnd[3] << 16) |
  1278.     ((u32) SCpnt->cmnd[4] << 8) |
  1279.     (u32) SCpnt->cmnd[5];
  1280. if (*SCpnt->cmnd == READ_10) {
  1281. megaCfg->nReads[(int) lun]++;
  1282. megaCfg->nReadBlocks[(int) lun] +=
  1283.     mbox->numsectors;
  1284. } else {
  1285. megaCfg->nWrites[(int) lun]++;
  1286. megaCfg->nWriteBlocks[(int) lun] +=
  1287.     mbox->numsectors;
  1288. }
  1289. }
  1290. /* 12-byte */
  1291. if (*SCpnt->cmnd == READ_12 || *SCpnt->cmnd == WRITE_12) {
  1292. mbox->lba =
  1293.     ((u32) SCpnt->cmnd[2] << 24) |
  1294.     ((u32) SCpnt->cmnd[3] << 16) |
  1295.     ((u32) SCpnt->cmnd[4] << 8) |
  1296.     (u32) SCpnt->cmnd[5];
  1297. mbox->numsectors =
  1298.     ((u32) SCpnt->cmnd[6] << 24) |
  1299.     ((u32) SCpnt->cmnd[7] << 16) |
  1300.     ((u32) SCpnt->cmnd[8] << 8) |
  1301.     (u32) SCpnt->cmnd[9];
  1302. if (*SCpnt->cmnd == READ_12) {
  1303. megaCfg->nReads[(int) lun]++;
  1304. megaCfg->nReadBlocks[(int) lun] +=
  1305.     mbox->numsectors;
  1306. } else {
  1307. megaCfg->nWrites[(int) lun]++;
  1308. megaCfg->nWriteBlocks[(int) lun] +=
  1309.     mbox->numsectors;
  1310. }
  1311. }
  1312. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1313. if (*SCpnt->cmnd == READ_6 || *SCpnt->cmnd == READ_10
  1314. || *SCpnt->cmnd == READ_12) {
  1315. pScb->dma_direction = PCI_DMA_FROMDEVICE;
  1316. } else { /*WRITE_6 or WRITE_10 */
  1317. pScb->dma_direction = PCI_DMA_TODEVICE;
  1318. }
  1319. #endif
  1320. /* Calculate Scatter-Gather info */
  1321. mbox->numsgelements = mega_build_sglist (megaCfg, pScb,
  1322.  (u32 *)&mbox->xferaddr, (u32 *)&seg);
  1323. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1324. pScb->iDataSize = seg;
  1325. if (mbox->numsgelements) {
  1326. pScb->dma_type = M_RD_SGLIST_ONLY;
  1327. TRACE1 (("M_RD_SGLIST_ONLY Enabled n"));
  1328. } else {
  1329. pScb->dma_type = M_RD_BULK_DATA_ONLY;
  1330. TRACE1 (("M_RD_BULK_DATA_ONLY Enabled n"));
  1331. }
  1332. #endif
  1333. return pScb;
  1334. default:
  1335. SCpnt->result = (DID_BAD_TARGET << 16);
  1336. callDone (SCpnt);
  1337. return NULL;
  1338. }
  1339. }
  1340. /*-----------------------------------------------------
  1341.  *
  1342.  *               Passthru drive commands
  1343.  *
  1344.  *-----------------------------------------------------*/
  1345. else {
  1346. /* Allocate a SCB and initialize passthru */
  1347. if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
  1348. SCpnt->result = (DID_ERROR << 16);
  1349. callDone (SCpnt);
  1350. return NULL;
  1351. }
  1352. mbox = (mega_mailbox *) pScb->mboxData;
  1353. memset (mbox, 0, sizeof (pScb->mboxData));
  1354. if ( megaCfg->support_ext_cdb && SCpnt->cmd_len > 10 ) {
  1355. epthru = mega_prepare_extpassthru(megaCfg, pScb, SCpnt);
  1356. mbox->cmd = MEGA_MBOXCMD_EXTPASSTHRU;
  1357. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1358. mbox->xferaddr = pScb->dma_ext_passthruhandle64;
  1359. if(epthru->numsgelements) {
  1360. pScb->dma_type = M_RD_PTHRU_WITH_SGLIST;
  1361. } else {
  1362. pScb->dma_type = M_RD_EPTHRU_WITH_BULK_DATA;
  1363. }
  1364. #else
  1365. mbox->xferaddr = virt_to_bus(epthru);
  1366. #endif
  1367. }
  1368. else {
  1369. pthru = mega_prepare_passthru(megaCfg, pScb, SCpnt);
  1370. /* Initialize mailbox */
  1371. mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
  1372. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1373. mbox->xferaddr = pScb->dma_passthruhandle64;
  1374. if (pthru->numsgelements) {
  1375. pScb->dma_type = M_RD_PTHRU_WITH_SGLIST;
  1376. } else {
  1377. pScb->dma_type = M_RD_PTHRU_WITH_BULK_DATA;
  1378. }
  1379. #else
  1380. mbox->xferaddr = virt_to_bus(pthru);
  1381. #endif
  1382. }
  1383. return pScb;
  1384. }
  1385. return NULL;
  1386. }
  1387. static int
  1388. mega_get_lun(mega_host_config *this_hba, Scsi_Cmnd *sc)
  1389. {
  1390. int tgt;
  1391. int lun;
  1392. int virt_chan;
  1393. tgt = sc->target;
  1394. if ( tgt > 7 ) tgt--; /* we do not get inquires for tgt 7 */
  1395. virt_chan = sc->channel - this_hba->productInfo.SCSIChanPresent;
  1396. lun = (virt_chan * 15) + tgt;
  1397. /*
  1398.  * If "delete logical drive" feature is enabled on this controller.
  1399.  * Do only if at least one delete logical drive operation was done.
  1400.  *
  1401.  * Also, after logical drive deletion, instead of logical drive number,
  1402.  * the value returned should be 0x80+logical drive id.
  1403.  *
  1404.  * These is valid only for IO commands.
  1405.  */
  1406.  if( this_hba->support_random_del && this_hba->read_ldidmap ) {
  1407. switch(sc->cmnd[0]) {
  1408. case READ_6: /* fall through */
  1409. case WRITE_6: /* fall through */
  1410. case READ_10: /* fall through */
  1411. case WRITE_10:
  1412. lun += 0x80;
  1413. }
  1414.  }
  1415.  return lun;
  1416. }
  1417. static mega_passthru *
  1418. mega_prepare_passthru(mega_host_config *megacfg, mega_scb *scb, Scsi_Cmnd *sc)
  1419. {
  1420. mega_passthru *pthru;
  1421. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1422. pthru = scb->pthru;
  1423. #else
  1424. pthru = &scb->pthru;
  1425. #endif
  1426. memset (pthru, 0, sizeof (mega_passthru));
  1427. /* set adapter timeout value to 10 min. for tape drive */
  1428. /* 0=6sec/1=60sec/2=10min/3=3hrs  */
  1429. pthru->timeout = 2;
  1430. pthru->ars = 1;
  1431. pthru->reqsenselen = 14;
  1432. pthru->islogical = 0;
  1433. pthru->channel = (megacfg->flag & BOARD_40LD) ? 0 : sc->channel;
  1434. pthru->target = (megacfg->flag & BOARD_40LD) ?
  1435.     (sc->channel << 4) | sc->target : sc->target;
  1436. pthru->cdblen = sc->cmd_len;
  1437. pthru->logdrv = sc->lun;
  1438. memcpy (pthru->cdb, sc->cmnd, sc->cmd_len);
  1439. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1440. /* Not sure about the direction */
  1441. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  1442. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  1443. switch (sc->cmnd[0]) {
  1444. case INQUIRY:
  1445. case READ_CAPACITY:
  1446. pthru->numsgelements = 0;
  1447. pthru->dataxferaddr = scb->dma_bounce_buffer;
  1448. pthru->dataxferlen = sc->request_bufflen;
  1449. break;
  1450. default:
  1451. pthru->numsgelements =
  1452. mega_build_sglist(
  1453. megacfg, scb, (u32 *)&pthru->dataxferaddr,
  1454. (u32 *)&pthru->dataxferlen
  1455. );
  1456. break;
  1457. }
  1458. #else
  1459. pthru->numsgelements =
  1460. mega_build_sglist(
  1461. megacfg, scb, (u32 *)&pthru->dataxferaddr,
  1462. (u32 *)&pthru->dataxferlen
  1463. );
  1464. #endif
  1465. return pthru;
  1466. }
  1467. static mega_ext_passthru *
  1468. mega_prepare_extpassthru(mega_host_config *megacfg, mega_scb *scb, Scsi_Cmnd *sc)
  1469. {
  1470. mega_ext_passthru *epthru;
  1471. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1472. epthru = scb->epthru;
  1473. #else
  1474. epthru = &scb->epthru;
  1475. #endif
  1476. memset(epthru, 0, sizeof(mega_ext_passthru));
  1477. /* set adapter timeout value to 10 min. for tape drive */
  1478. /* 0=6sec/1=60sec/2=10min/3=3hrs  */
  1479. epthru->timeout = 2;
  1480. epthru->ars = 1;
  1481. epthru->reqsenselen = 14;
  1482. epthru->islogical = 0;
  1483. epthru->channel = (megacfg->flag & BOARD_40LD) ? 0 : sc->channel;
  1484. epthru->target = (megacfg->flag & BOARD_40LD) ?
  1485.     (sc->channel << 4) | sc->target : sc->target;
  1486. epthru->cdblen = sc->cmd_len;
  1487. epthru->logdrv = sc->lun;
  1488. memcpy(epthru->cdb, sc->cmnd, sc->cmd_len);
  1489. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1490. /* Not sure about the direction */
  1491. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  1492. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  1493. switch (sc->cmnd[0]) {
  1494. case INQUIRY:
  1495. case READ_CAPACITY:
  1496. epthru->numsgelements = 0;
  1497. epthru->dataxferaddr = scb->dma_bounce_buffer;
  1498. epthru->dataxferlen = sc->request_bufflen;
  1499. break;
  1500. default:
  1501. epthru->numsgelements =
  1502. mega_build_sglist(
  1503. megacfg, scb, (u32 *)&epthru->dataxferaddr,
  1504. (u32 *)&epthru->dataxferlen
  1505. );
  1506. break;
  1507. }
  1508. #else
  1509. epthru->numsgelements =
  1510. mega_build_sglist(
  1511. megacfg, scb, (u32 *)&epthru->dataxferaddr,
  1512. (u32 *)&epthru->dataxferlen
  1513. );
  1514. #endif
  1515. return epthru;
  1516. }
  1517. /* Handle Driver Level IOCTLs
  1518.  * Return value of 0 indicates this function could not handle , so continue
  1519.  * processing
  1520. */
  1521. static int mega_driver_ioctl (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt)
  1522. {
  1523. unsigned char *data = (unsigned char *) SCpnt->request_buffer;
  1524. mega_driver_info driver_info;
  1525. /* If this is not our command dont do anything */
  1526. if (SCpnt->cmnd[0] != M_RD_DRIVER_IOCTL_INTERFACE)
  1527. return 0;
  1528. switch (SCpnt->cmnd[1]) {
  1529. case GET_DRIVER_INFO:
  1530. if (SCpnt->request_bufflen < sizeof (driver_info)) {
  1531. SCpnt->result = DID_BAD_TARGET << 16;
  1532. callDone (SCpnt);
  1533. return 1;
  1534. }
  1535. driver_info.size = sizeof (driver_info) - sizeof (int);
  1536. driver_info.version = MEGARAID_IOCTL_VERSION;
  1537. memcpy (data, &driver_info, sizeof (driver_info));
  1538. break;
  1539. default:
  1540. SCpnt->result = DID_BAD_TARGET << 16;
  1541. }
  1542. callDone (SCpnt);
  1543. return 1;
  1544. }
  1545. static void inline set_mbox_xfer_addr (mega_host_config * megaCfg, mega_scb * pScb,
  1546.     mega_ioctl_mbox * mbox, u32 direction)
  1547. {
  1548. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1549. switch (direction) {
  1550. case TO_DEVICE:
  1551. pScb->dma_direction = PCI_DMA_TODEVICE;
  1552. break;
  1553. case FROM_DEVICE:
  1554. pScb->dma_direction = PCI_DMA_FROMDEVICE;
  1555. break;
  1556. case FROMTO_DEVICE:
  1557. pScb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  1558. break;
  1559. }
  1560. pScb->dma_h_bulkdata
  1561.     = pci_map_single (megaCfg->dev,
  1562.       pScb->buff_ptr,
  1563.       pScb->iDataSize, pScb->dma_direction);
  1564. mbox->xferaddr = pScb->dma_h_bulkdata;
  1565. pScb->dma_type = M_RD_BULK_DATA_ONLY;
  1566. TRACE1 (("M_RD_BULK_DATA_ONLY Enabled n"));
  1567. #else
  1568. mbox->xferaddr = virt_to_bus (pScb->buff_ptr);
  1569. #endif
  1570. }
  1571. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
  1572. /*--------------------------------------------------------------------
  1573.  * build RAID commands for controller, passed down through ioctl()
  1574.  *--------------------------------------------------------------------*/
  1575. static mega_scb *mega_ioctl (mega_host_config * megaCfg, Scsi_Cmnd * SCpnt)
  1576. {
  1577. mega_scb *pScb;
  1578. mega_ioctl_mbox *mbox;
  1579. mega_mailbox *mailbox;
  1580. mega_passthru *pthru;
  1581. u8 *mboxdata;
  1582. long seg, i = 0;
  1583. unsigned char *data = (unsigned char *) SCpnt->request_buffer;
  1584. if ((pScb = mega_allocateSCB (megaCfg, SCpnt)) == NULL) {
  1585. SCpnt->result = (DID_ERROR << 16);
  1586. callDone (SCpnt);
  1587. return NULL;
  1588. }
  1589. pthru = &pScb->pthru;
  1590. mboxdata = (u8 *) & pScb->mboxData;
  1591. mbox = (mega_ioctl_mbox *) & pScb->mboxData;
  1592. mailbox = (mega_mailbox *) & pScb->mboxData;
  1593. memset (mailbox, 0, sizeof (pScb->mboxData));
  1594. if (data[0] == 0x03) { /* passthrough command */
  1595. unsigned char cdblen = data[2];
  1596. memset (pthru, 0, sizeof (mega_passthru));
  1597. pthru->islogical = (data[cdblen + 3] & 0x80) ? 1 : 0;
  1598. pthru->timeout = data[cdblen + 3] & 0x07;
  1599. pthru->reqsenselen = 14;
  1600. pthru->ars = (data[cdblen + 3] & 0x08) ? 1 : 0;
  1601. pthru->logdrv = data[cdblen + 4];
  1602. pthru->channel = data[cdblen + 5];
  1603. pthru->target = data[cdblen + 6];
  1604. pthru->cdblen = cdblen;
  1605. memcpy (pthru->cdb, &data[3], cdblen);
  1606. mailbox->cmd = MEGA_MBOXCMD_PASSTHRU;
  1607. pthru->numsgelements = mega_build_sglist (megaCfg, pScb,
  1608.   (u32 *) & pthru->
  1609.   dataxferaddr,
  1610.   (u32 *) & pthru->
  1611.   dataxferlen);
  1612. mailbox->xferaddr = virt_to_bus (pthru);
  1613. for (i = 0; i < (SCpnt->request_bufflen - cdblen - 7); i++) {
  1614. data[i] = data[i + cdblen + 7];
  1615. }
  1616. return pScb;
  1617. }
  1618. /* else normal (nonpassthru) command */
  1619. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,0,24) /*0x020024 */
  1620. /*
  1621.  *usage of the function copy from user is used in case of data more than
  1622.  *4KB.This is used only with adapters which supports more than 8 logical
  1623.  * drives.This feature is disabled on kernels earlier or same as 2.0.36
  1624.  * as the uaccess.h file is not available with those kernels.
  1625.  */
  1626. if (SCpnt->cmnd[0] == M_RD_IOCTL_CMD_NEW) {
  1627. /* use external data area for large xfers  */
  1628. /* If cmnd[0] is set to M_RD_IOCTL_CMD_NEW then *
  1629.  *   cmnd[4..7] = external user buffer     *
  1630.  *   cmnd[8..11] = length of buffer        *
  1631.  *                                         */
  1632.        char *user_area = (char *)*((u32*)&SCpnt->cmnd[4]);
  1633. u32 xfer_size = *((u32 *) & SCpnt->cmnd[8]);
  1634. switch (data[0]) {
  1635. case FW_FIRE_WRITE:
  1636. case FW_FIRE_FLASH:
  1637. if ((ulong) user_area & (PAGE_SIZE - 1)) {
  1638. printk
  1639.     ("megaraid:user address not aligned on 4K boundary.Error.n");
  1640. SCpnt->result = (DID_ERROR << 16);
  1641. callDone (SCpnt);
  1642. return NULL;
  1643. }
  1644. break;
  1645. default:
  1646. break;
  1647. }
  1648. if (!(pScb->buff_ptr = kmalloc (xfer_size, GFP_KERNEL))) {
  1649. printk
  1650.     ("megaraid: Insufficient mem for M_RD_IOCTL_CMD_NEW.n");
  1651. SCpnt->result = (DID_ERROR << 16);
  1652. callDone (SCpnt);
  1653. return NULL;
  1654. }
  1655. copy_from_user (pScb->buff_ptr, user_area, xfer_size);
  1656. pScb->iDataSize = xfer_size;
  1657. switch (data[0]) {
  1658. case DCMD_FC_CMD:
  1659. switch (data[1]) {
  1660. case DCMD_FC_READ_NVRAM_CONFIG:
  1661. case DCMD_GET_DISK_CONFIG:
  1662. {
  1663. if ((ulong) pScb->
  1664.     buff_ptr & (PAGE_SIZE - 1)) {
  1665. printk
  1666.     ("megaraid:user address not sufficient Error.n");
  1667. SCpnt->result =
  1668.     (DID_ERROR << 16);
  1669. callDone (SCpnt);
  1670. return NULL;
  1671. }
  1672. /*building SG list */
  1673. mega_build_kernel_sg (pScb->buff_ptr,
  1674.       xfer_size,
  1675.       pScb, mbox);
  1676. break;
  1677. }
  1678. default:
  1679. break;
  1680. } /*switch (data[1]) */
  1681. break;
  1682. }
  1683. }
  1684. #endif
  1685. mbox->cmd = data[0];
  1686. mbox->channel = data[1];
  1687. mbox->param = data[2];
  1688. mbox->pad[0] = data[3];
  1689. mbox->logdrv = data[4];
  1690. if (SCpnt->cmnd[0] == M_RD_IOCTL_CMD_NEW) {
  1691. switch (data[0]) {
  1692. case FW_FIRE_WRITE:
  1693. mbox->cmd = FW_FIRE_WRITE;
  1694. mbox->channel = data[1]; /* Current Block Number */
  1695. set_mbox_xfer_addr (megaCfg, pScb, mbox, TO_DEVICE);
  1696. mbox->numsgelements = 0;
  1697. break;
  1698. case FW_FIRE_FLASH:
  1699. mbox->cmd = FW_FIRE_FLASH;
  1700. mbox->channel = data[1] | 0x80; /* Origin */
  1701. set_mbox_xfer_addr (megaCfg, pScb, mbox, TO_DEVICE);
  1702. mbox->numsgelements = 0;
  1703. break;
  1704. case DCMD_FC_CMD:
  1705. *(mboxdata + 0) = data[0]; /*mailbox byte 0: DCMD_FC_CMD */
  1706. *(mboxdata + 2) = data[1]; /*sub command */
  1707. switch (data[1]) {
  1708. case DCMD_FC_READ_NVRAM_CONFIG:
  1709. case DCMD_FC_READ_NVRAM_CONFIG_64:
  1710. /* number of elements in SG list */
  1711. *(mboxdata + 3) = mbox->numsgelements;
  1712. if (megaCfg->flag & BOARD_64BIT)
  1713. *(mboxdata + 2) =
  1714.     DCMD_FC_READ_NVRAM_CONFIG_64;
  1715. break;
  1716. case DCMD_WRITE_CONFIG:
  1717. case DCMD_WRITE_CONFIG_64:
  1718. if (megaCfg->flag & BOARD_64BIT)
  1719. *(mboxdata + 2) = DCMD_WRITE_CONFIG_64;
  1720. set_mbox_xfer_addr (megaCfg, pScb, mbox,
  1721.     TO_DEVICE);
  1722. mbox->numsgelements = 0;
  1723. break;
  1724. case DCMD_GET_DISK_CONFIG:
  1725. case DCMD_GET_DISK_CONFIG_64:
  1726. if (megaCfg->flag & BOARD_64BIT)
  1727. *(mboxdata + 2) =
  1728.     DCMD_GET_DISK_CONFIG_64;
  1729. *(mboxdata + 3) = data[2]; /*number of elements in SG list */
  1730. /*nr of elements in SG list */
  1731. *(mboxdata + 4) = mbox->numsgelements;
  1732. break;
  1733. case DCMD_DELETE_LOGDRV:
  1734. case DCMD_DELETE_DRIVEGROUP:
  1735. case NC_SUBOP_ENQUIRY3:
  1736. *(mboxdata + 3) = data[2];
  1737. set_mbox_xfer_addr (megaCfg, pScb, mbox,
  1738.     FROMTO_DEVICE);
  1739. mbox->numsgelements = 0;
  1740. break;
  1741. case DCMD_CHANGE_LDNO:
  1742. case DCMD_CHANGE_LOOPID:
  1743. *(mboxdata + 3) = data[2];
  1744. *(mboxdata + 4) = data[3];
  1745. set_mbox_xfer_addr (megaCfg, pScb, mbox,
  1746.     TO_DEVICE);
  1747. mbox->numsgelements = 0;
  1748. break;
  1749. default:
  1750. set_mbox_xfer_addr (megaCfg, pScb, mbox,
  1751.     FROMTO_DEVICE);
  1752. mbox->numsgelements = 0;
  1753. break;
  1754. } /*switch */
  1755. break;
  1756. default:
  1757. set_mbox_xfer_addr (megaCfg, pScb, mbox, FROMTO_DEVICE);
  1758. mbox->numsgelements = 0;
  1759. break;
  1760. }
  1761. } else {
  1762. mbox->numsgelements = mega_build_sglist (megaCfg, pScb,
  1763.  (u32 *) & mbox->
  1764.  xferaddr,
  1765.  (u32 *) & seg);
  1766. /* Handling some of the fw special commands */
  1767. switch (data[0]) {
  1768. case 6: /* START_DEV */
  1769. mbox->xferaddr = *((u32 *) & data[i + 6]);
  1770. break;
  1771. default:
  1772. break;
  1773. }
  1774. for (i = 0; i < (SCpnt->request_bufflen - 6); i++) {
  1775. data[i] = data[i + 6];
  1776. }
  1777. }
  1778. return (pScb);
  1779. }
  1780. static void mega_build_kernel_sg (char *barea, ulong xfersize, mega_scb * pScb, mega_ioctl_mbox * mbox)
  1781. {
  1782. ulong i, buffer_area, len, end, end_page, x, idx = 0;
  1783. buffer_area = (ulong) barea;
  1784. i = buffer_area;
  1785. end = buffer_area + xfersize;
  1786. end_page = (end) & ~(PAGE_SIZE - 1);
  1787. do {
  1788. len = PAGE_SIZE - (i % PAGE_SIZE);
  1789. x = pScb->sgList[idx].address =
  1790.     virt_to_bus ((volatile void *) i);
  1791. pScb->sgList[idx].length = len;
  1792. i += len;
  1793. idx++;
  1794. } while (i < end_page);
  1795. if ((end - i) < 0) {
  1796. printk ("megaraid:Error in user addressn");
  1797. }
  1798. if (end - i) {
  1799. pScb->sgList[idx].address = virt_to_bus ((volatile void *) i);
  1800. pScb->sgList[idx].length = end - i;
  1801. idx++;
  1802. }
  1803. mbox->xferaddr = virt_to_bus (pScb->sgList);
  1804. mbox->numsgelements = idx;
  1805. }
  1806. #endif
  1807. #if DEBUG
  1808. static unsigned int cum_time = 0;
  1809. static unsigned int cum_time_cnt = 0;
  1810. static void showMbox (mega_scb * pScb)
  1811. {
  1812. mega_mailbox *mbox;
  1813. if (pScb == NULL)
  1814. return;
  1815. mbox = (mega_mailbox *) pScb->mboxData;
  1816. printk ("%u cmd:%x id:%x #scts:%x lba:%x addr:%x logdrv:%x #sg:%xn",
  1817. pScb->SCpnt->pid,
  1818. mbox->cmd, mbox->cmdid, mbox->numsectors,
  1819. mbox->lba, mbox->xferaddr, mbox->logdrv, mbox->numsgelements);
  1820. }
  1821. #endif
  1822. /*--------------------------------------------------------------------
  1823.  * Interrupt service routine
  1824.  *--------------------------------------------------------------------*/
  1825. static void megaraid_isr (int irq, void *devp, struct pt_regs *regs)
  1826. {
  1827. IO_LOCK_T;
  1828. mega_host_config * megaCfg;
  1829. u_char byte, idx, sIdx, tmpBox[MAILBOX_SIZE];
  1830. u32 dword = 0;
  1831. mega_mailbox *mbox;
  1832. mega_scb *pScb;
  1833. u_char qCnt, qStatus;
  1834. u_char completed[MAX_FIRMWARE_STATUS];
  1835. Scsi_Cmnd *SCpnt;
  1836. megaCfg = (mega_host_config *) devp;
  1837. mbox = (mega_mailbox *) tmpBox;
  1838. if (megaCfg->host->irq == irq) {
  1839. if (megaCfg->flag & IN_ISR) {
  1840. TRACE (("ISR called reentrantly!!n"));
  1841. printk ("ISR called reentrantly!!n");
  1842. }
  1843. megaCfg->flag |= IN_ISR;
  1844. if (mega_busyWaitMbox (megaCfg)) {
  1845. printk (KERN_WARNING "Error: mailbox busy in isr!n");
  1846. }
  1847. /* Check if a valid interrupt is pending */
  1848. if (megaCfg->flag & BOARD_QUARTZ) {
  1849. dword = RDOUTDOOR (megaCfg);
  1850. if (dword != 0x10001234) {
  1851. /* Spurious interrupt */
  1852. megaCfg->flag &= ~IN_ISR;
  1853. return;
  1854. }
  1855. } else {
  1856. byte = READ_PORT (megaCfg->host->io_port, INTR_PORT);
  1857. if ((byte & VALID_INTR_BYTE) == 0) {
  1858. /* Spurious interrupt */
  1859. megaCfg->flag &= ~IN_ISR;
  1860. return;
  1861. }
  1862. WRITE_PORT (megaCfg->host->io_port, INTR_PORT, byte);
  1863. }
  1864. for (idx = 0; idx < MAX_FIRMWARE_STATUS; idx++)
  1865. completed[idx] = 0;
  1866. IO_LOCK;
  1867. megaCfg->nInterrupts++;
  1868. qCnt = 0xff;
  1869. while ((qCnt = megaCfg->mbox->numstatus) == 0xFF) ;
  1870. qStatus = 0xff;
  1871. while ((qStatus = megaCfg->mbox->status) == 0xFF) ;
  1872. /* Get list of completed requests */
  1873. for (idx = 0; idx < qCnt; idx++) {
  1874. while ((sIdx = megaCfg->mbox->completed[idx]) == 0xFF) {
  1875. printk ("p");
  1876. }
  1877. completed[idx] = sIdx;
  1878. sIdx = 0xFF;
  1879. }
  1880. if (megaCfg->flag & BOARD_QUARTZ) {
  1881. WROUTDOOR (megaCfg, dword);
  1882. /* Acknowledge interrupt */
  1883. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  1884. /* In this case mbox contains physical address */
  1885. #if 0
  1886. WRINDOOR (megaCfg, megaCfg->adjdmahandle64 | 0x2);
  1887. #else
  1888. WRINDOOR (megaCfg, 0x2);
  1889. #endif
  1890. #else
  1891. #if 0
  1892. WRINDOOR (megaCfg, virt_to_bus (megaCfg->mbox) | 0x2);
  1893. #else
  1894. WRINDOOR (megaCfg, 0x2);
  1895. #endif
  1896. #endif
  1897. #if 0
  1898. while (RDINDOOR (megaCfg) & 0x02) ;
  1899. #endif
  1900. } else {
  1901. CLEAR_INTR (megaCfg->host->io_port);
  1902. }
  1903. #if DEBUG
  1904. if (qCnt >= MAX_FIRMWARE_STATUS) {
  1905. printk ("megaraid_isr: cmplt=%d ", qCnt);
  1906. }
  1907. #endif
  1908. for (idx = 0; idx < qCnt; idx++) {
  1909. sIdx = completed[idx];
  1910. if ((sIdx > 0) && (sIdx <= MAX_COMMANDS)) {
  1911. pScb = &megaCfg->scbList[sIdx - 1];
  1912. /* ASSERT(pScb->state == SCB_ISSUED); */
  1913. #if DEBUG
  1914. if (((jiffies) - pScb->isrcount) > maxCmdTime) {
  1915. maxCmdTime = (jiffies) - pScb->isrcount;
  1916. printk
  1917.     ("megaraid_isr : cmd time = %un",
  1918.      maxCmdTime);
  1919. }
  1920. #endif
  1921. /*
  1922.  * Assuming that the scsi command, for which 
  1923.  * an abort request was received earlier, has 
  1924.  * completed.
  1925.  */
  1926. if (pScb->state == SCB_ABORTED) {
  1927. SCpnt = pScb->SCpnt;
  1928. }
  1929. if (pScb->state == SCB_RESET) {
  1930. SCpnt = pScb->SCpnt;
  1931. mega_freeSCB (megaCfg, pScb);
  1932. SCpnt->result = (DID_RESET << 16);
  1933. if (megaCfg->qCompletedH == NULL) {
  1934. megaCfg->qCompletedH =
  1935.     megaCfg->qCompletedT =
  1936.     SCpnt;
  1937. } else {
  1938. megaCfg->qCompletedT->
  1939.     host_scribble =
  1940.     (unsigned char *) SCpnt;
  1941. megaCfg->qCompletedT = SCpnt;
  1942. }
  1943. megaCfg->qCompletedT->host_scribble =
  1944.     (unsigned char *) NULL;
  1945. megaCfg->qCcnt++;
  1946. continue;
  1947. }
  1948. /* We don't want the ISR routine to touch M_RD_IOCTL_CMD_NEW commands, so
  1949.  * don't mark them as complete, instead we pop their semaphore so
  1950.  * that the queue routine can finish them off
  1951.  */
  1952. if (pScb->SCpnt->cmnd[0] == M_RD_IOCTL_CMD_NEW) {
  1953. /* save the status byte for the queue routine to use */
  1954. pScb->SCpnt->result = qStatus;
  1955. up (&pScb->ioctl_sem);
  1956. } else {
  1957. /* Mark command as completed */
  1958. mega_cmd_done (megaCfg, pScb, qStatus);
  1959. }
  1960. } else {
  1961. printk
  1962.     ("megaraid: wrong cmd id completed from firmware:id=%xn",
  1963.      sIdx);
  1964. }
  1965. }
  1966. mega_rundoneq (megaCfg);
  1967. megaCfg->flag &= ~IN_ISR;
  1968. /* Loop through any pending requests */
  1969. mega_runpendq (megaCfg);
  1970. IO_UNLOCK;
  1971. }
  1972. }
  1973. /*==================================================*/
  1974. /* Wait until the controller's mailbox is available */
  1975. /*==================================================*/
  1976. static int mega_busyWaitMbox (mega_host_config * megaCfg)
  1977. {
  1978. mega_mailbox *mbox = (mega_mailbox *) megaCfg->mbox;
  1979. long counter;
  1980. for (counter = 0; counter < 10000; counter++) {
  1981. if (!mbox->busy) {
  1982. return 0;
  1983. }
  1984. udelay (100);
  1985. barrier ();
  1986. }
  1987. return -1; /* give up after 1 second */
  1988. }
  1989. /*=====================================================
  1990.  * Post a command to the card
  1991.  *
  1992.  * Arguments:
  1993.  *   mega_host_config *megaCfg - Controller structure
  1994.  *   u_char *mboxData - Mailbox area, 16 bytes
  1995.  *   mega_scb *pScb   - SCB posting (or NULL if N/A)
  1996.  *   int intr         - if 1, interrupt, 0 is blocking
  1997.  * Return Value: (added on 7/26 for 40ld/64bit)
  1998.  *   -1: the command was not actually issued out
  1999.  *   other cases:
  2000.  *     intr==0, return ScsiStatus, i.e. mbox->status
  2001.  *     intr==1, return 0
  2002.  *=====================================================
  2003.  */
  2004. static int megaIssueCmd (mega_host_config * megaCfg, u_char * mboxData, 
  2005. mega_scb * pScb, int intr)
  2006. {
  2007. volatile mega_mailbox *mbox = (mega_mailbox *) megaCfg->mbox;
  2008. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2009. volatile mega_mailbox64 *mbox64 = (mega_mailbox64 *) megaCfg->mbox64;
  2010. #endif
  2011. u_char byte;
  2012. #if BITS_PER_LONG==64
  2013. u64 phys_mbox;
  2014. #else
  2015. u32 phys_mbox;
  2016. #endif
  2017. u8 retval = -1;
  2018. mboxData[0x1] = (pScb ? pScb->idx + 1 : 0xFE); /* Set cmdid */
  2019. mboxData[0xF] = 1; /* Set busy */
  2020. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2021. /* In this case mbox contains physical address */
  2022. phys_mbox = megaCfg->adjdmahandle64;
  2023. #else
  2024. phys_mbox = virt_to_bus (megaCfg->mbox);
  2025. #endif
  2026. #if DEBUG
  2027. ShowMbox (pScb);
  2028. #endif
  2029. /* Wait until mailbox is free */
  2030. if (mega_busyWaitMbox (megaCfg)) {
  2031. printk ("Blocked mailbox......!!n");
  2032. udelay (1000);
  2033. #if DEBUG
  2034. showMbox (pLastScb);
  2035. #endif
  2036. /* Abort command */
  2037. if (pScb == NULL) {
  2038. TRACE (("NULL pScb in megaIssuen"));
  2039. printk ("NULL pScb in megaIssuen");
  2040. }
  2041. mega_cmd_done (megaCfg, pScb, 0x08);
  2042. return -1;
  2043. }
  2044. pLastScb = pScb;
  2045. /* Copy mailbox data into host structure */
  2046. megaCfg->mbox64->xferSegment_lo = 0;
  2047. megaCfg->mbox64->xferSegment_hi = 0;
  2048. memcpy ((char *) mbox, mboxData, 16);
  2049. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2050. switch (mboxData[0]) {
  2051. case MEGA_MBOXCMD_LREAD64:
  2052. case MEGA_MBOXCMD_LWRITE64:
  2053. mbox64->xferSegment_lo = mbox->xferaddr;
  2054. mbox64->xferSegment_hi = 0;
  2055. mbox->xferaddr = 0xFFFFFFFF;
  2056. break;
  2057. }
  2058. #endif
  2059. /* Kick IO */
  2060. if (intr) {
  2061. /* Issue interrupt (non-blocking) command */
  2062. if (megaCfg->flag & BOARD_QUARTZ) {
  2063. mbox->mraid_poll = 0;
  2064. mbox->mraid_ack = 0;
  2065. WRINDOOR (megaCfg, phys_mbox | 0x1);
  2066. } else {
  2067. ENABLE_INTR (megaCfg->host->io_port);
  2068. ISSUE_COMMAND (megaCfg->host->io_port);
  2069. }
  2070. pScb->state = SCB_ISSUED;
  2071. retval = 0;
  2072. } else { /* Issue non-ISR (blocking) command */
  2073. disable_irq (megaCfg->host->irq);
  2074. if (megaCfg->flag & BOARD_QUARTZ) {
  2075. mbox->mraid_poll = 0;
  2076. mbox->mraid_ack = 0;
  2077. mbox->numstatus = 0xFF;
  2078. mbox->status = 0xFF;
  2079. WRINDOOR (megaCfg, phys_mbox | 0x1);
  2080. while (mbox->numstatus == 0xFF) ;
  2081. while (mbox->status == 0xFF) ;
  2082. while (mbox->mraid_poll != 0x77) ;
  2083. mbox->mraid_poll = 0;
  2084. mbox->mraid_ack = 0x77;
  2085. /* while ((cmdDone = RDOUTDOOR (megaCfg)) != 0x10001234);
  2086.    WROUTDOOR (megaCfg, cmdDone); */
  2087. if (pScb) {
  2088. mega_cmd_done (megaCfg, pScb, mbox->status);
  2089. }
  2090. WRINDOOR (megaCfg, phys_mbox | 0x2);
  2091. while (RDINDOOR (megaCfg) & 0x2) ;
  2092. } else {
  2093. DISABLE_INTR (megaCfg->host->io_port);
  2094. ISSUE_COMMAND (megaCfg->host->io_port);
  2095. while (!
  2096.        ((byte =
  2097.  READ_PORT (megaCfg->host->io_port,
  2098.     INTR_PORT)) & INTR_VALID)) ;
  2099. WRITE_PORT (megaCfg->host->io_port, INTR_PORT, byte);
  2100. ENABLE_INTR (megaCfg->host->io_port);
  2101. CLEAR_INTR (megaCfg->host->io_port);
  2102. if (pScb) {
  2103. mega_cmd_done (megaCfg, pScb, mbox->status);
  2104. } else {
  2105. TRACE (("Error: NULL pScb!n"));
  2106. }
  2107. }
  2108. enable_irq (megaCfg->host->irq);
  2109. retval = mbox->status;
  2110. }
  2111. #if DEBUG
  2112. while (mega_busyWaitMbox (megaCfg)) {
  2113. printk(KERN_ERR "Blocked mailbox on exit......!n");
  2114. udelay (1000);
  2115. }
  2116. #endif
  2117. return retval;
  2118. }
  2119. /*-------------------------------------------------------------------
  2120.  * Copies data to SGLIST
  2121.  *-------------------------------------------------------------------*/
  2122. /* Note:
  2123. For 64 bit cards, we need a minimum of one SG element for read/write
  2124. */
  2125. static int
  2126. mega_build_sglist (mega_host_config * megaCfg, mega_scb * scb,
  2127.    u32 * buffer, u32 * length)
  2128. {
  2129. struct scatterlist *sgList;
  2130. int idx;
  2131. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2132. int sgcnt;
  2133. #endif
  2134. mega_mailbox *mbox = NULL;
  2135. mbox = (mega_mailbox *) scb->mboxData;
  2136. /* Scatter-gather not used */
  2137. if (scb->SCpnt->use_sg == 0) {
  2138. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2139. scb->dma_h_bulkdata = pci_map_single (megaCfg->dev,
  2140.       scb->SCpnt->request_buffer,
  2141.       scb->SCpnt->request_bufflen,
  2142.       scb->dma_direction);
  2143. /* We need to handle special commands like READ64, WRITE64
  2144.    as they need a minimum of 1 SG irrespective of actually SG
  2145.  */
  2146. if ((megaCfg->flag & BOARD_64BIT) &&
  2147.     ((mbox->cmd == MEGA_MBOXCMD_LREAD64) ||
  2148.      (mbox->cmd == MEGA_MBOXCMD_LWRITE64))) {
  2149. scb->sg64List[0].address = scb->dma_h_bulkdata;
  2150. scb->sg64List[0].length = scb->SCpnt->request_bufflen;
  2151. *buffer = scb->dma_sghandle64;
  2152. *length = 0;
  2153. scb->sglist_count = 1;
  2154. return 1;
  2155. } else {
  2156. *buffer = scb->dma_h_bulkdata;
  2157. *length = (u32) scb->SCpnt->request_bufflen;
  2158. }
  2159. #else
  2160. *buffer = virt_to_bus (scb->SCpnt->request_buffer);
  2161. *length = (u32) scb->SCpnt->request_bufflen;
  2162. #endif
  2163. return 0;
  2164. }
  2165. sgList = (struct scatterlist *) scb->SCpnt->request_buffer;
  2166. #if 0
  2167. if (scb->SCpnt->use_sg == 1) {
  2168. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2169. scb->dma_h_bulkdata = pci_map_single (megaCfg->dev,
  2170.       sgList[0].address,
  2171.       sgList[0].length, scb->dma_direction);
  2172. if ((megaCfg->flag & BOARD_64BIT) &&
  2173.     ((mbox->cmd == MEGA_MBOXCMD_LREAD64) ||
  2174.      (mbox->cmd == MEGA_MBOXCMD_LWRITE64))) {
  2175. scb->sg64List[0].address = scb->dma_h_bulkdata;
  2176. scb->sg64List[0].length = scb->SCpnt->request_bufflen;
  2177. *buffer = scb->dma_sghandle64;
  2178. *length = 0;
  2179. scb->sglist_count = 1;
  2180. return 1;
  2181. } else {
  2182. *buffer = scb->dma_h_bulkdata;
  2183. *length = (u32) sgList[0].length;
  2184. }
  2185. #else
  2186. *buffer = virt_to_bus (sgList[0].address);
  2187. *length = (u32) sgList[0].length;
  2188. #endif
  2189. return 0;
  2190. }
  2191. #endif
  2192. /* Copy Scatter-Gather list info into controller structure */
  2193. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2194. sgcnt = pci_map_sg (megaCfg->dev,
  2195.     sgList, scb->SCpnt->use_sg, scb->dma_direction);
  2196. /* Determine the validity of the new count  */
  2197. if (sgcnt == 0)
  2198. printk ("pci_map_sg returned zero!!! ");
  2199. for (idx = 0; idx < sgcnt; idx++, sgList++) {
  2200. if ((megaCfg->flag & BOARD_64BIT) &&
  2201.     ((mbox->cmd == MEGA_MBOXCMD_LREAD64) ||
  2202.      (mbox->cmd == MEGA_MBOXCMD_LWRITE64))) {
  2203. scb->sg64List[idx].address = sg_dma_address (sgList);
  2204. scb->sg64List[idx].length = sg_dma_len (sgList);
  2205. } else {
  2206. scb->sgList[idx].address = sg_dma_address (sgList);
  2207. scb->sgList[idx].length = sg_dma_len (sgList);
  2208. }
  2209. }
  2210. #else
  2211. for (idx = 0; idx < scb->SCpnt->use_sg; idx++) {
  2212. scb->sgList[idx].address = virt_to_bus (sgList[idx].address);
  2213. scb->sgList[idx].length = (u32) sgList[idx].length;
  2214. }
  2215. #endif
  2216. /* Reset pointer and length fields */
  2217. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2218. *buffer = scb->dma_sghandle64;
  2219. scb->sglist_count = scb->SCpnt->use_sg;
  2220. #else
  2221. *buffer = virt_to_bus (scb->sgList);
  2222. #endif
  2223. *length = 0;
  2224. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2225. /* Return count of SG requests */
  2226. return sgcnt;
  2227. #else
  2228. /* Return count of SG requests */
  2229. return scb->SCpnt->use_sg;
  2230. #endif
  2231. }
  2232. /*--------------------------------------------------------------------
  2233.  * Initializes the address of the controller's mailbox register
  2234.  *  The mailbox register is used to issue commands to the card.
  2235.  *  Format of the mailbox area:
  2236.  *   00 01 command
  2237.  *   01 01 command id
  2238.  *   02 02 # of sectors
  2239.  *   04 04 logical bus address
  2240.  *   08 04 physical buffer address
  2241.  *   0C 01 logical drive #
  2242.  *   0D 01 length of scatter/gather list
  2243.  *   0E 01 reserved
  2244.  *   0F 01 mailbox busy
  2245.  *   10 01 numstatus byte
  2246.  *   11 01 status byte
  2247.  *--------------------------------------------------------------------*/
  2248. static int
  2249. mega_register_mailbox (mega_host_config * megaCfg, u32 paddr)
  2250. {
  2251. /* align on 16-byte boundary */
  2252. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2253. megaCfg->mbox = &megaCfg->mailbox64ptr->mailbox;
  2254. #else
  2255. megaCfg->mbox = &megaCfg->mailbox64.mailbox;
  2256. #endif
  2257. #if BITS_PER_LONG==64
  2258. megaCfg->mbox = (mega_mailbox *) ((((u64) megaCfg->mbox) + 16) & ((u64) (-1) ^ 0x0F));
  2259. megaCfg->adjdmahandle64 = (megaCfg->dma_handle64 + 16) & ((u64) (-1) ^ 0x0F);
  2260. megaCfg->mbox64 = (mega_mailbox64 *) ((u_char *) megaCfg->mbox - sizeof (u64));
  2261. paddr = (paddr + 4 + 16) & ((u64) (-1) ^ 0x0F);
  2262. #else
  2263. megaCfg->mbox
  2264.     = (mega_mailbox *) ((((u32) megaCfg->mbox) + 16) & 0xFFFFFFF0);
  2265. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2266. megaCfg->adjdmahandle64 = ((megaCfg->dma_handle64 + 16) & 0xFFFFFFF0);
  2267. #endif
  2268. megaCfg->mbox64 = (mega_mailbox64 *) ((u_char *) megaCfg->mbox - 8);
  2269. paddr = (paddr + 4 + 16) & 0xFFFFFFF0;
  2270. #endif
  2271. /* Register mailbox area with the firmware */
  2272. if (!(megaCfg->flag & BOARD_QUARTZ)) {
  2273. WRITE_PORT (megaCfg->host->io_port, MBOX_PORT0, paddr & 0xFF);
  2274. WRITE_PORT (megaCfg->host->io_port, MBOX_PORT1,
  2275.     (paddr >> 8) & 0xFF);
  2276. WRITE_PORT (megaCfg->host->io_port, MBOX_PORT2,
  2277.     (paddr >> 16) & 0xFF);
  2278. WRITE_PORT (megaCfg->host->io_port, MBOX_PORT3,
  2279.     (paddr >> 24) & 0xFF);
  2280. WRITE_PORT (megaCfg->host->io_port, ENABLE_MBOX_REGION,
  2281.     ENABLE_MBOX_BYTE);
  2282. CLEAR_INTR (megaCfg->host->io_port);
  2283. ENABLE_INTR (megaCfg->host->io_port);
  2284. }
  2285. return 0;
  2286. }
  2287. /*---------------------------------------------------------------------------
  2288.  * mega_Convert8ldTo40ld() -- takes all info in AdapterInquiry structure and
  2289.  * puts it into ProductInfo and Enquiry3 structures for later use
  2290.  *---------------------------------------------------------------------------*/
  2291. static void mega_Convert8ldTo40ld (mega_RAIDINQ * inquiry,
  2292.        mega_Enquiry3 * enquiry3,
  2293.        megaRaidProductInfo * productInfo)
  2294. {
  2295. int i;
  2296. productInfo->MaxConcCmds = inquiry->AdpInfo.MaxConcCmds;
  2297. enquiry3->rbldRate = inquiry->AdpInfo.RbldRate;
  2298. productInfo->SCSIChanPresent = inquiry->AdpInfo.ChanPresent;
  2299. for (i = 0; i < 4; i++) {
  2300. productInfo->FwVer[i] = inquiry->AdpInfo.FwVer[i];
  2301. productInfo->BiosVer[i] = inquiry->AdpInfo.BiosVer[i];
  2302. }
  2303. enquiry3->cacheFlushInterval = inquiry->AdpInfo.CacheFlushInterval;
  2304. productInfo->DramSize = inquiry->AdpInfo.DramSize;
  2305. enquiry3->numLDrv = inquiry->LogdrvInfo.NumLDrv;
  2306. for (i = 0; i < MAX_LOGICAL_DRIVES; i++) {
  2307. enquiry3->lDrvSize[i] = inquiry->LogdrvInfo.LDrvSize[i];
  2308. enquiry3->lDrvProp[i] = inquiry->LogdrvInfo.LDrvProp[i];
  2309. enquiry3->lDrvState[i]
  2310.     = inquiry->LogdrvInfo.LDrvState[i];
  2311. }
  2312. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++) {
  2313. enquiry3->pDrvState[i]
  2314.     = inquiry->PhysdrvInfo.PDrvState[i];
  2315. }
  2316. }
  2317. /*-------------------------------------------------------------------
  2318.  * Issue an adapter info query to the controller
  2319.  *-------------------------------------------------------------------*/
  2320. static int mega_i_query_adapter (mega_host_config * megaCfg)
  2321. {
  2322. mega_Enquiry3 *enquiry3Pnt;
  2323. mega_mailbox *mbox;
  2324. u_char mboxData[16];
  2325. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  2326. dma_addr_t raid_inq_dma_handle = 0, prod_info_dma_handle = 0, enquiry3_dma_handle = 0;
  2327. #endif
  2328. u8 retval;
  2329. /* Initialize adapter inquiry mailbox */
  2330. mbox = (mega_mailbox *) mboxData;
  2331. memset ((void *) megaCfg->mega_buffer, 0,
  2332. sizeof (megaCfg->mega_buffer));