/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* mtip32xx.h - Header file for the P320 SSD Block Driver3* Copyright (C) 2011 Micron Technology, Inc.4*5* Portions of this code were derived from works subjected to the6* following copyright:7* Copyright (C) 2009 Integrated Device Technology, Inc.8*/910#ifndef __MTIP32XX_H__11#define __MTIP32XX_H__1213#include <linux/spinlock.h>14#include <linux/rwsem.h>15#include <linux/ata.h>16#include <linux/interrupt.h>1718/* Offset of Subsystem Device ID in pci confoguration space */19#define PCI_SUBSYSTEM_DEVICEID 0x2E2021/* offset of Device Control register in PCIe extended capabilites space */22#define PCIE_CONFIG_EXT_DEVICE_CONTROL_OFFSET 0x482324/* check for erase mode support during secure erase */25#define MTIP_SEC_ERASE_MODE 0x22627/* # of times to retry timed out/failed IOs */28#define MTIP_MAX_RETRIES 22930/* Various timeout values in ms */31#define MTIP_NCQ_CMD_TIMEOUT_MS 1500032#define MTIP_IOCTL_CMD_TIMEOUT_MS 500033#define MTIP_INT_CMD_TIMEOUT_MS 500034#define MTIP_QUIESCE_IO_TIMEOUT_MS (MTIP_NCQ_CMD_TIMEOUT_MS * \35(MTIP_MAX_RETRIES + 1))3637/* check for timeouts every 500ms */38#define MTIP_TIMEOUT_CHECK_PERIOD 5003940/* ftl rebuild */41#define MTIP_FTL_REBUILD_OFFSET 14242#define MTIP_FTL_REBUILD_MAGIC 0xED5143#define MTIP_FTL_REBUILD_TIMEOUT_MS 24000004445/* unaligned IO handling */46#define MTIP_MAX_UNALIGNED_SLOTS 24748/* Macro to extract the tag bit number from a tag value. */49#define MTIP_TAG_BIT(tag) (tag & 0x1F)5051/*52* Macro to extract the tag index from a tag value. The index53* is used to access the correct s_active/Command Issue register based54* on the tag value.55*/56#define MTIP_TAG_INDEX(tag) (tag >> 5)5758/*59* Maximum number of scatter gather entries60* a single command may have.61*/62#define MTIP_MAX_SG 5046364/*65* Maximum number of slot groups (Command Issue & s_active registers)66* NOTE: This is the driver maximum; check dd->slot_groups for actual value.67*/68#define MTIP_MAX_SLOT_GROUPS 86970/* Internal command tag. */71#define MTIP_TAG_INTERNAL 07273/* Micron Vendor ID & P320x SSD Device ID */74#define PCI_VENDOR_ID_MICRON 0x134475#define P320H_DEVICE_ID 0x515076#define P320M_DEVICE_ID 0x515177#define P320S_DEVICE_ID 0x515278#define P325M_DEVICE_ID 0x515379#define P420H_DEVICE_ID 0x516080#define P420M_DEVICE_ID 0x516181#define P425M_DEVICE_ID 0x51638283/* Driver name and version strings */84#define MTIP_DRV_NAME "mtip32xx"85#define MTIP_DRV_VERSION "1.3.1"8687/* Maximum number of minor device numbers per device. */88#define MTIP_MAX_MINORS 168990/* Maximum number of supported command slots. */91#define MTIP_MAX_COMMAND_SLOTS (MTIP_MAX_SLOT_GROUPS * 32)9293/*94* Per-tag bitfield size in longs.95* Linux bit manipulation functions96* (i.e. test_and_set_bit, find_next_zero_bit)97* manipulate memory in longs, so we try to make the math work.98* take the slot groups and find the number of longs, rounding up.99* Careful! i386 and x86_64 use different size longs!100*/101#define U32_PER_LONG (sizeof(long) / sizeof(u32))102#define SLOTBITS_IN_LONGS ((MTIP_MAX_SLOT_GROUPS + \103(U32_PER_LONG-1))/U32_PER_LONG)104105/* BAR number used to access the HBA registers. */106#define MTIP_ABAR 5107108#ifdef DEBUG109#define dbg_printk(format, arg...) \110printk(pr_fmt(format), ##arg);111#else112#define dbg_printk(format, arg...)113#endif114115#define MTIP_DFS_MAX_BUF_SIZE 1024116117enum {118/* below are bit numbers in 'flags' defined in mtip_port */119MTIP_PF_IC_ACTIVE_BIT = 0, /* pio/ioctl */120MTIP_PF_EH_ACTIVE_BIT = 1, /* error handling */121MTIP_PF_SE_ACTIVE_BIT = 2, /* secure erase */122MTIP_PF_DM_ACTIVE_BIT = 3, /* download microcde */123MTIP_PF_TO_ACTIVE_BIT = 9, /* timeout handling */124MTIP_PF_PAUSE_IO = ((1 << MTIP_PF_IC_ACTIVE_BIT) |125(1 << MTIP_PF_EH_ACTIVE_BIT) |126(1 << MTIP_PF_SE_ACTIVE_BIT) |127(1 << MTIP_PF_DM_ACTIVE_BIT) |128(1 << MTIP_PF_TO_ACTIVE_BIT)),129MTIP_PF_HOST_CAP_64 = 10, /* cache HOST_CAP_64 */130131MTIP_PF_SVC_THD_ACTIVE_BIT = 4,132MTIP_PF_ISSUE_CMDS_BIT = 5,133MTIP_PF_REBUILD_BIT = 6,134MTIP_PF_SVC_THD_STOP_BIT = 8,135136MTIP_PF_SVC_THD_WORK = ((1 << MTIP_PF_EH_ACTIVE_BIT) |137(1 << MTIP_PF_ISSUE_CMDS_BIT) |138(1 << MTIP_PF_REBUILD_BIT) |139(1 << MTIP_PF_SVC_THD_STOP_BIT) |140(1 << MTIP_PF_TO_ACTIVE_BIT)),141142/* below are bit numbers in 'dd_flag' defined in driver_data */143MTIP_DDF_SEC_LOCK_BIT = 0,144MTIP_DDF_REMOVE_PENDING_BIT = 1,145MTIP_DDF_OVER_TEMP_BIT = 2,146MTIP_DDF_WRITE_PROTECT_BIT = 3,147MTIP_DDF_CLEANUP_BIT = 5,148MTIP_DDF_RESUME_BIT = 6,149MTIP_DDF_INIT_DONE_BIT = 7,150MTIP_DDF_REBUILD_FAILED_BIT = 8,151152MTIP_DDF_STOP_IO = ((1 << MTIP_DDF_REMOVE_PENDING_BIT) |153(1 << MTIP_DDF_SEC_LOCK_BIT) |154(1 << MTIP_DDF_OVER_TEMP_BIT) |155(1 << MTIP_DDF_WRITE_PROTECT_BIT) |156(1 << MTIP_DDF_REBUILD_FAILED_BIT)),157158};159160struct smart_attr {161u8 attr_id;162__le16 flags;163u8 cur;164u8 worst;165__le32 data;166u8 res[3];167} __packed;168169struct mtip_work {170struct work_struct work;171void *port;172int cpu_binding;173u32 completed;174} ____cacheline_aligned_in_smp;175176#define DEFINE_HANDLER(group) \177void mtip_workq_sdbf##group(struct work_struct *work) \178{ \179struct mtip_work *w = (struct mtip_work *) work; \180mtip_workq_sdbfx(w->port, group, w->completed); \181}182183/* Register Frame Information Structure (FIS), host to device. */184struct host_to_dev_fis {185/*186* FIS type.187* - 27h Register FIS, host to device.188* - 34h Register FIS, device to host.189* - 39h DMA Activate FIS, device to host.190* - 41h DMA Setup FIS, bi-directional.191* - 46h Data FIS, bi-directional.192* - 58h BIST Activate FIS, bi-directional.193* - 5Fh PIO Setup FIS, device to host.194* - A1h Set Device Bits FIS, device to host.195*/196unsigned char type;197unsigned char opts;198unsigned char command;199unsigned char features;200201union {202unsigned char lba_low;203unsigned char sector;204};205union {206unsigned char lba_mid;207unsigned char cyl_low;208};209union {210unsigned char lba_hi;211unsigned char cyl_hi;212};213union {214unsigned char device;215unsigned char head;216};217218union {219unsigned char lba_low_ex;220unsigned char sector_ex;221};222union {223unsigned char lba_mid_ex;224unsigned char cyl_low_ex;225};226union {227unsigned char lba_hi_ex;228unsigned char cyl_hi_ex;229};230unsigned char features_ex;231232unsigned char sect_count;233unsigned char sect_cnt_ex;234unsigned char res2;235unsigned char control;236237unsigned int res3;238};239240/* Command header structure. */241struct mtip_cmd_hdr {242/*243* Command options.244* - Bits 31:16 Number of PRD entries.245* - Bits 15:8 Unused in this implementation.246* - Bit 7 Prefetch bit, informs the drive to prefetch PRD entries.247* - Bit 6 Write bit, should be set when writing data to the device.248* - Bit 5 Unused in this implementation.249* - Bits 4:0 Length of the command FIS in DWords (DWord = 4 bytes).250*/251__le32 opts;252/* This field is unsed when using NCQ. */253union {254__le32 byte_count;255__le32 status;256};257/*258* Lower 32 bits of the command table address associated with this259* header. The command table addresses must be 128 byte aligned.260*/261__le32 ctba;262/*263* If 64 bit addressing is used this field is the upper 32 bits264* of the command table address associated with this command.265*/266__le32 ctbau;267/* Reserved and unused. */268u32 res[4];269};270271/* Command scatter gather structure (PRD). */272struct mtip_cmd_sg {273/*274* Low 32 bits of the data buffer address. For P320 this275* address must be 8 byte aligned signified by bits 2:0 being276* set to 0.277*/278__le32 dba;279/*280* When 64 bit addressing is used this field is the upper281* 32 bits of the data buffer address.282*/283__le32 dba_upper;284/* Unused. */285__le32 reserved;286/*287* Bit 31: interrupt when this data block has been transferred.288* Bits 30..22: reserved289* Bits 21..0: byte count (minus 1). For P320 the byte count must be290* 8 byte aligned signified by bits 2:0 being set to 1.291*/292__le32 info;293};294struct mtip_port;295296struct mtip_int_cmd;297298/* Structure used to describe a command. */299struct mtip_cmd {300void *command; /* ptr to command table entry */301302dma_addr_t command_dma; /* corresponding physical address */303304int scatter_ents; /* Number of scatter list entries used */305306int unaligned; /* command is unaligned on 4k boundary */307308union {309struct scatterlist sg[MTIP_MAX_SG]; /* Scatter list entries */310struct mtip_int_cmd *icmd;311};312313int retries; /* The number of retries left for this command. */314315int direction; /* Data transfer direction */316blk_status_t status;317};318319/* Structure used to describe a port. */320struct mtip_port {321/* Pointer back to the driver data for this port. */322struct driver_data *dd;323/*324* Used to determine if the data pointed to by the325* identify field is valid.326*/327unsigned long identify_valid;328/* Base address of the memory mapped IO for the port. */329void __iomem *mmio;330/* Array of pointers to the memory mapped s_active registers. */331void __iomem *s_active[MTIP_MAX_SLOT_GROUPS];332/* Array of pointers to the memory mapped completed registers. */333void __iomem *completed[MTIP_MAX_SLOT_GROUPS];334/* Array of pointers to the memory mapped Command Issue registers. */335void __iomem *cmd_issue[MTIP_MAX_SLOT_GROUPS];336/*337* Pointer to the beginning of the command header memory as used338* by the driver.339*/340void *command_list;341/*342* Pointer to the beginning of the command header memory as used343* by the DMA.344*/345dma_addr_t command_list_dma;346/*347* Pointer to the beginning of the RX FIS memory as used348* by the driver.349*/350void *rxfis;351/*352* Pointer to the beginning of the RX FIS memory as used353* by the DMA.354*/355dma_addr_t rxfis_dma;356/*357* Pointer to the DMA region for RX Fis, Identify, RLE10, and SMART358*/359void *block1;360/*361* DMA address of region for RX Fis, Identify, RLE10, and SMART362*/363dma_addr_t block1_dma;364/*365* Pointer to the beginning of the identify data memory as used366* by the driver.367*/368u16 *identify;369/*370* Pointer to the beginning of the identify data memory as used371* by the DMA.372*/373dma_addr_t identify_dma;374/*375* Pointer to the beginning of a sector buffer that is used376* by the driver when issuing internal commands.377*/378u16 *sector_buffer;379/*380* Pointer to the beginning of a sector buffer that is used381* by the DMA when the driver issues internal commands.382*/383dma_addr_t sector_buffer_dma;384385u16 *log_buf;386dma_addr_t log_buf_dma;387388u8 *smart_buf;389dma_addr_t smart_buf_dma;390391/*392* used to queue commands when an internal command is in progress393* or error handling is active394*/395unsigned long cmds_to_issue[SLOTBITS_IN_LONGS];396/* Used by mtip_service_thread to wait for an event */397wait_queue_head_t svc_wait;398/*399* indicates the state of the port. Also, helps the service thread400* to determine its action on wake up.401*/402unsigned long flags;403/*404* Timer used to complete commands that have been active for too long.405*/406unsigned long ic_pause_timer;407408/* Counter to control queue depth of unaligned IOs */409atomic_t cmd_slot_unal;410411/* Spinlock for working around command-issue bug. */412spinlock_t cmd_issue_lock[MTIP_MAX_SLOT_GROUPS];413};414415/*416* Driver private data structure.417*418* One structure is allocated per probed device.419*/420struct driver_data {421void __iomem *mmio; /* Base address of the HBA registers. */422423int major; /* Major device number. */424425int instance; /* Instance number. First device probed is 0, ... */426427struct gendisk *disk; /* Pointer to our gendisk structure. */428429struct pci_dev *pdev; /* Pointer to the PCI device structure. */430431struct request_queue *queue; /* Our request queue. */432433struct blk_mq_tag_set tags; /* blk_mq tags */434435struct mtip_port *port; /* Pointer to the port data structure. */436437unsigned product_type; /* magic value declaring the product type */438439unsigned slot_groups; /* number of slot groups the product supports */440441unsigned long index; /* Index to determine the disk name */442443unsigned long dd_flag; /* NOTE: use atomic bit operations on this */444445struct task_struct *mtip_svc_handler; /* task_struct of svc thd */446447struct dentry *dfs_node;448449bool sr;450451int numa_node; /* NUMA support */452453char workq_name[32];454455struct workqueue_struct *isr_workq;456457atomic_t irq_workers_active;458459struct mtip_work work[MTIP_MAX_SLOT_GROUPS];460461int isr_binding;462463int unal_qdepth; /* qdepth of unaligned IO queue */464};465466#endif467468469