1*2874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Linux MegaRAID device driver 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (c) 2003-2004 LSI Logic Corporation. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * FILE : mega_common.h 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Libaray of common routine used by all low-level megaraid drivers 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #ifndef _MEGA_COMMON_H_ 141da177e4SLinus Torvalds #define _MEGA_COMMON_H_ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 171da177e4SLinus Torvalds #include <linux/types.h> 181da177e4SLinus Torvalds #include <linux/pci.h> 191da177e4SLinus Torvalds #include <linux/spinlock.h> 200c2cc433SMatthias Kaehlcke #include <linux/mutex.h> 211da177e4SLinus Torvalds #include <linux/interrupt.h> 221da177e4SLinus Torvalds #include <linux/delay.h> 231da177e4SLinus Torvalds #include <linux/blkdev.h> 241da177e4SLinus Torvalds #include <linux/list.h> 251da177e4SLinus Torvalds #include <linux/moduleparam.h> 26672b2d38SJu, Seokmann #include <linux/dma-mapping.h> 271da177e4SLinus Torvalds #include <scsi/scsi.h> 281da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h> 291da177e4SLinus Torvalds #include <scsi/scsi_device.h> 301da177e4SLinus Torvalds #include <scsi/scsi_host.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #define LSI_MAX_CHANNELS 16 341da177e4SLinus Torvalds #define LSI_MAX_LOGICAL_DRIVES_64LD (64+1) 351da177e4SLinus Torvalds 36fbf60802SJu, Seokmann #define HBA_SIGNATURE_64_BIT 0x299 37fbf60802SJu, Seokmann #define PCI_CONF_AMISIG64 0xa4 38fbf60802SJu, Seokmann 39aa677bc7SJu, Seokmann #define MEGA_SCSI_INQ_EVPD 1 40aa677bc7SJu, Seokmann #define MEGA_INVALID_FIELD_IN_CDB 0x24 41aa677bc7SJu, Seokmann 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /** 441da177e4SLinus Torvalds * scb_t - scsi command control block 45a69b74d3SRandy Dunlap * @ccb : command control block for individual driver 46a69b74d3SRandy Dunlap * @list : list of control blocks 47a69b74d3SRandy Dunlap * @gp : general purpose field for LLDs 48a69b74d3SRandy Dunlap * @sno : all SCBs have a serial number 49a69b74d3SRandy Dunlap * @scp : associated scsi command 50a69b74d3SRandy Dunlap * @state : current state of scb 51a69b74d3SRandy Dunlap * @dma_dir : direction of data transfer 52a69b74d3SRandy Dunlap * @dma_type : transfer with sg list, buffer, or no data transfer 53a69b74d3SRandy Dunlap * @dev_channel : actual channel on the device 54a69b74d3SRandy Dunlap * @dev_target : actual target on the device 55a69b74d3SRandy Dunlap * @status : completion status 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * This is our central data structure to issue commands the each driver. 581da177e4SLinus Torvalds * Driver specific data structures are maintained in the ccb field. 591da177e4SLinus Torvalds * scb provides a field 'gp', which can be used by LLD for its own purposes 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * dev_channel and dev_target must be initialized with the actual channel and 621da177e4SLinus Torvalds * target on the controller. 631da177e4SLinus Torvalds */ 641da177e4SLinus Torvalds typedef struct { 651da177e4SLinus Torvalds caddr_t ccb; 661da177e4SLinus Torvalds struct list_head list; 671da177e4SLinus Torvalds unsigned long gp; 681da177e4SLinus Torvalds unsigned int sno; 691da177e4SLinus Torvalds struct scsi_cmnd *scp; 701da177e4SLinus Torvalds uint32_t state; 711da177e4SLinus Torvalds uint32_t dma_direction; 721da177e4SLinus Torvalds uint32_t dma_type; 731da177e4SLinus Torvalds uint16_t dev_channel; 741da177e4SLinus Torvalds uint16_t dev_target; 751da177e4SLinus Torvalds uint32_t status; 761da177e4SLinus Torvalds } scb_t; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* 791da177e4SLinus Torvalds * SCB states as it transitions from one state to another 801da177e4SLinus Torvalds */ 811da177e4SLinus Torvalds #define SCB_FREE 0x0000 /* on the free list */ 821da177e4SLinus Torvalds #define SCB_ACTIVE 0x0001 /* off the free list */ 831da177e4SLinus Torvalds #define SCB_PENDQ 0x0002 /* on the pending queue */ 841da177e4SLinus Torvalds #define SCB_ISSUED 0x0004 /* issued - owner f/w */ 851da177e4SLinus Torvalds #define SCB_ABORT 0x0008 /* Got an abort for this one */ 861da177e4SLinus Torvalds #define SCB_RESET 0x0010 /* Got a reset for this one */ 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* 891da177e4SLinus Torvalds * DMA types for scb 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds #define MRAID_DMA_NONE 0x0000 /* no data transfer for this command */ 921da177e4SLinus Torvalds #define MRAID_DMA_WSG 0x0001 /* data transfer using a sg list */ 931da177e4SLinus Torvalds #define MRAID_DMA_WBUF 0x0002 /* data transfer using a contiguous buffer */ 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /** 971da177e4SLinus Torvalds * struct adapter_t - driver's initialization structure 98a69b74d3SRandy Dunlap * @aram dpc_h : tasklet handle 99a69b74d3SRandy Dunlap * @pdev : pci configuration pointer for kernel 100a69b74d3SRandy Dunlap * @host : pointer to host structure of mid-layer 101a69b74d3SRandy Dunlap * @lock : synchronization lock for mid-layer and driver 102a69b74d3SRandy Dunlap * @quiescent : driver is quiescent for now. 103a69b74d3SRandy Dunlap * @outstanding_cmds : number of commands pending in the driver 104a69b74d3SRandy Dunlap * @kscb_list : pointer to the bulk of SCBs pointers for IO 105a69b74d3SRandy Dunlap * @kscb_pool : pool of free scbs for IO 106a69b74d3SRandy Dunlap * @kscb_pool_lock : lock for pool of free scbs 107a69b74d3SRandy Dunlap * @pend_list : pending commands list 108a69b74d3SRandy Dunlap * @pend_list_lock : exclusion lock for pending commands list 109a69b74d3SRandy Dunlap * @completed_list : list of completed commands 110a69b74d3SRandy Dunlap * @completed_list_lock : exclusion lock for list of completed commands 111a69b74d3SRandy Dunlap * @sglen : max sg elements supported 112a69b74d3SRandy Dunlap * @device_ids : to convert kernel device addr to our devices. 113a69b74d3SRandy Dunlap * @raid_device : raid adapter specific pointer 114a69b74d3SRandy Dunlap * @max_channel : maximum channel number supported - inclusive 115a69b74d3SRandy Dunlap * @max_target : max target supported - inclusive 116a69b74d3SRandy Dunlap * @max_lun : max lun supported - inclusive 117a69b74d3SRandy Dunlap * @unique_id : unique identifier for each adapter 118a69b74d3SRandy Dunlap * @irq : IRQ for this adapter 119a69b74d3SRandy Dunlap * @ito : internal timeout value, (-1) means no timeout 120a69b74d3SRandy Dunlap * @ibuf : buffer to issue internal commands 121a69b74d3SRandy Dunlap * @ibuf_dma_h : dma handle for the above buffer 122a69b74d3SRandy Dunlap * @uscb_list : SCB pointers for user cmds, common mgmt module 123a69b74d3SRandy Dunlap * @uscb_pool : pool of SCBs for user commands 124a69b74d3SRandy Dunlap * @uscb_pool_lock : exclusion lock for these SCBs 125a69b74d3SRandy Dunlap * @max_cmds : max outstanding commands 126a69b74d3SRandy Dunlap * @fw_version : firmware version 127a69b74d3SRandy Dunlap * @bios_version : bios version 128a69b74d3SRandy Dunlap * @max_cdb_sz : biggest CDB size supported. 129a69b74d3SRandy Dunlap * @ha : is high availability present - clustering 130a69b74d3SRandy Dunlap * @init_id : initiator ID, the default value should be 7 131a69b74d3SRandy Dunlap * @max_sectors : max sectors per request 132a69b74d3SRandy Dunlap * @cmd_per_lun : max outstanding commands per LUN 133a69b74d3SRandy Dunlap * @being_detached : set when unloading, no more mgmt calls 1341da177e4SLinus Torvalds * 1351da177e4SLinus Torvalds * 1361da177e4SLinus Torvalds * mraid_setup_device_map() can be called anytime after the device map is 1371da177e4SLinus Torvalds * available and MRAID_GET_DEVICE_MAP() can be called whenever the mapping is 1381da177e4SLinus Torvalds * required, usually from LLD's queue entry point. The formar API sets up the 1391da177e4SLinus Torvalds * MRAID_IS_LOGICAL(adapter_t *, struct scsi_cmnd *) to find out if the 1401da177e4SLinus Torvalds * device in question is a logical drive. 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * quiescent flag should be set by the driver if it is not accepting more 1431da177e4SLinus Torvalds * commands 1441da177e4SLinus Torvalds * 1451da177e4SLinus Torvalds * NOTE: The fields of this structures are placed to minimize cache misses 1461da177e4SLinus Torvalds */ 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds // amount of space required to store the bios and firmware version strings 1491da177e4SLinus Torvalds #define VERSION_SIZE 16 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds typedef struct { 1521da177e4SLinus Torvalds struct tasklet_struct dpc_h; 1531da177e4SLinus Torvalds struct pci_dev *pdev; 1541da177e4SLinus Torvalds struct Scsi_Host *host; 1551da177e4SLinus Torvalds spinlock_t lock; 1561da177e4SLinus Torvalds uint8_t quiescent; 1571da177e4SLinus Torvalds int outstanding_cmds; 1581da177e4SLinus Torvalds scb_t *kscb_list; 1591da177e4SLinus Torvalds struct list_head kscb_pool; 1601da177e4SLinus Torvalds spinlock_t kscb_pool_lock; 1611da177e4SLinus Torvalds struct list_head pend_list; 1621da177e4SLinus Torvalds spinlock_t pend_list_lock; 1631da177e4SLinus Torvalds struct list_head completed_list; 1641da177e4SLinus Torvalds spinlock_t completed_list_lock; 1651da177e4SLinus Torvalds uint16_t sglen; 1661da177e4SLinus Torvalds int device_ids[LSI_MAX_CHANNELS] 1671da177e4SLinus Torvalds [LSI_MAX_LOGICAL_DRIVES_64LD]; 1681da177e4SLinus Torvalds caddr_t raid_device; 1691da177e4SLinus Torvalds uint8_t max_channel; 1701da177e4SLinus Torvalds uint16_t max_target; 1711da177e4SLinus Torvalds uint8_t max_lun; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds uint32_t unique_id; 174f5ebbeb5SEric W. Biederman int irq; 1751da177e4SLinus Torvalds uint8_t ito; 1761da177e4SLinus Torvalds caddr_t ibuf; 1771da177e4SLinus Torvalds dma_addr_t ibuf_dma_h; 1781da177e4SLinus Torvalds scb_t *uscb_list; 1791da177e4SLinus Torvalds struct list_head uscb_pool; 1801da177e4SLinus Torvalds spinlock_t uscb_pool_lock; 1811da177e4SLinus Torvalds int max_cmds; 1821da177e4SLinus Torvalds uint8_t fw_version[VERSION_SIZE]; 1831da177e4SLinus Torvalds uint8_t bios_version[VERSION_SIZE]; 1841da177e4SLinus Torvalds uint8_t max_cdb_sz; 1851da177e4SLinus Torvalds uint8_t ha; 1861da177e4SLinus Torvalds uint16_t init_id; 1871da177e4SLinus Torvalds uint16_t max_sectors; 1881da177e4SLinus Torvalds uint16_t cmd_per_lun; 1891da177e4SLinus Torvalds atomic_t being_detached; 1901da177e4SLinus Torvalds } adapter_t; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds #define SCSI_FREE_LIST_LOCK(adapter) (&adapter->kscb_pool_lock) 1931da177e4SLinus Torvalds #define USER_FREE_LIST_LOCK(adapter) (&adapter->uscb_pool_lock) 1941da177e4SLinus Torvalds #define PENDING_LIST_LOCK(adapter) (&adapter->pend_list_lock) 1951da177e4SLinus Torvalds #define COMPLETED_LIST_LOCK(adapter) (&adapter->completed_list_lock) 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds // conversion from scsi command 1991da177e4SLinus Torvalds #define SCP2HOST(scp) (scp)->device->host // to host 2001da177e4SLinus Torvalds #define SCP2HOSTDATA(scp) SCP2HOST(scp)->hostdata // to soft state 2011da177e4SLinus Torvalds #define SCP2CHANNEL(scp) (scp)->device->channel // to channel 2021da177e4SLinus Torvalds #define SCP2TARGET(scp) (scp)->device->id // to target 2039cb78c16SHannes Reinecke #define SCP2LUN(scp) (u32)(scp)->device->lun // to LUN 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds // generic macro to convert scsi command and host to controller's soft state 2061da177e4SLinus Torvalds #define SCSIHOST2ADAP(host) (((caddr_t *)(host->hostdata))[0]) 2071da177e4SLinus Torvalds #define SCP2ADAPTER(scp) (adapter_t *)SCSIHOST2ADAP(SCP2HOST(scp)) 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds #define MRAID_IS_LOGICAL(adp, scp) \ 2111da177e4SLinus Torvalds (SCP2CHANNEL(scp) == (adp)->max_channel) ? 1 : 0 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds #define MRAID_IS_LOGICAL_SDEV(adp, sdev) \ 2141da177e4SLinus Torvalds (sdev->channel == (adp)->max_channel) ? 1 : 0 2151da177e4SLinus Torvalds 216a69b74d3SRandy Dunlap /** 217a69b74d3SRandy Dunlap * MRAID_GET_DEVICE_MAP - device ids 218a69b74d3SRandy Dunlap * @adp : adapter's soft state 219a69b74d3SRandy Dunlap * @scp : mid-layer scsi command pointer 220a69b74d3SRandy Dunlap * @p_chan : physical channel on the controller 221a69b74d3SRandy Dunlap * @target : target id of the device or logical drive number 222a69b74d3SRandy Dunlap * @islogical : set if the command is for the logical drive 223a69b74d3SRandy Dunlap * 224a69b74d3SRandy Dunlap * Macro to retrieve information about device class, logical or physical and 225a69b74d3SRandy Dunlap * the corresponding physical channel and target or logical drive number 226a69b74d3SRandy Dunlap */ 2271da177e4SLinus Torvalds #define MRAID_GET_DEVICE_MAP(adp, scp, p_chan, target, islogical) \ 2281da177e4SLinus Torvalds /* \ 2291da177e4SLinus Torvalds * Is the request coming for the virtual channel \ 2301da177e4SLinus Torvalds */ \ 2311da177e4SLinus Torvalds islogical = MRAID_IS_LOGICAL(adp, scp); \ 2321da177e4SLinus Torvalds \ 2331da177e4SLinus Torvalds /* \ 2341da177e4SLinus Torvalds * Get an index into our table of drive ids mapping \ 2351da177e4SLinus Torvalds */ \ 2361da177e4SLinus Torvalds if (islogical) { \ 2371da177e4SLinus Torvalds p_chan = 0xFF; \ 2381da177e4SLinus Torvalds target = \ 2391da177e4SLinus Torvalds (adp)->device_ids[(adp)->max_channel][SCP2TARGET(scp)]; \ 2401da177e4SLinus Torvalds } \ 2411da177e4SLinus Torvalds else { \ 2421da177e4SLinus Torvalds p_chan = ((adp)->device_ids[SCP2CHANNEL(scp)] \ 2431da177e4SLinus Torvalds [SCP2TARGET(scp)] >> 8) & 0xFF; \ 2441da177e4SLinus Torvalds target = ((adp)->device_ids[SCP2CHANNEL(scp)] \ 2451da177e4SLinus Torvalds [SCP2TARGET(scp)] & 0xFF); \ 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /* 2491da177e4SLinus Torvalds * ### Helper routines ### 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds #define LSI_DBGLVL mraid_debug_level // each LLD must define a global 2521da177e4SLinus Torvalds // mraid_debug_level 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds #ifdef DEBUG 2551da177e4SLinus Torvalds #if defined (_ASSERT_PANIC) 2561da177e4SLinus Torvalds #define ASSERT_ACTION panic 2571da177e4SLinus Torvalds #else 2581da177e4SLinus Torvalds #define ASSERT_ACTION printk 2591da177e4SLinus Torvalds #endif 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds #define ASSERT(expression) \ 2621da177e4SLinus Torvalds if (!(expression)) { \ 2631da177e4SLinus Torvalds ASSERT_ACTION("assertion failed:(%s), file: %s, line: %d:%s\n", \ 264cadbd4a5SHarvey Harrison #expression, __FILE__, __LINE__, __func__); \ 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds #else 2671da177e4SLinus Torvalds #define ASSERT(expression) 2681da177e4SLinus Torvalds #endif 2691da177e4SLinus Torvalds 27059f19a9eSRandy Dunlap /** 2711da177e4SLinus Torvalds * struct mraid_pci_blk - structure holds DMA memory block info 272a69b74d3SRandy Dunlap * @vaddr : virtual address to a memory block 273a69b74d3SRandy Dunlap * @dma_addr : DMA handle to a memory block 2741da177e4SLinus Torvalds * 2751da177e4SLinus Torvalds * This structure is filled up for the caller. It is the responsibilty of the 2761da177e4SLinus Torvalds * caller to allocate this array big enough to store addresses for all 2771da177e4SLinus Torvalds * requested elements 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds struct mraid_pci_blk { 2801da177e4SLinus Torvalds caddr_t vaddr; 2811da177e4SLinus Torvalds dma_addr_t dma_addr; 2821da177e4SLinus Torvalds }; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds #endif // _MEGA_COMMON_H_ 285