Bart Van Assche | bec9e8a | 2017-08-17 13:12:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for sTec s1120 PCIe SSDs. sTec was acquired in 2013 by HGST and HGST |
| 3 | * was acquired by Western Digital in 2012. |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4 | * |
Bart Van Assche | bec9e8a | 2017-08-17 13:12:47 -0700 | [diff] [blame] | 5 | * Copyright 2012 sTec, Inc. |
| 6 | * Copyright (c) 2017 Western Digital Corporation or its affiliates. |
| 7 | * |
| 8 | * This file is part of the Linux kernel, and is made available under |
| 9 | * the terms of the GNU General Public License version 2. |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/pci.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/blkdev.h> |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 19 | #include <linux/blk-mq.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 20 | #include <linux/sched.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/compiler.h> |
| 23 | #include <linux/workqueue.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
| 25 | #include <linux/time.h> |
| 26 | #include <linux/hdreg.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/completion.h> |
| 29 | #include <linux/scatterlist.h> |
| 30 | #include <linux/version.h> |
| 31 | #include <linux/err.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 32 | #include <linux/aer.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 33 | #include <linux/wait.h> |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 34 | #include <linux/stringify.h> |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 35 | #include <linux/slab_def.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 36 | #include <scsi/scsi.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 37 | #include <scsi/sg.h> |
| 38 | #include <linux/io.h> |
| 39 | #include <linux/uaccess.h> |
Bartlomiej Zolnierkiewicz | 4ca90b5 | 2013-11-05 12:37:04 +0100 | [diff] [blame] | 40 | #include <asm/unaligned.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 41 | |
| 42 | #include "skd_s1120.h" |
| 43 | |
| 44 | static int skd_dbg_level; |
| 45 | static int skd_isr_comp_limit = 4; |
| 46 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 47 | #define SKD_ASSERT(expr) \ |
| 48 | do { \ |
| 49 | if (unlikely(!(expr))) { \ |
| 50 | pr_err("Assertion failed! %s,%s,%s,line=%d\n", \ |
| 51 | # expr, __FILE__, __func__, __LINE__); \ |
| 52 | } \ |
| 53 | } while (0) |
| 54 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 55 | #define DRV_NAME "skd" |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 56 | #define PFX DRV_NAME ": " |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 57 | |
Bart Van Assche | bec9e8a | 2017-08-17 13:12:47 -0700 | [diff] [blame] | 58 | MODULE_LICENSE("GPL"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 59 | |
Bart Van Assche | bb9f7dd | 2017-08-17 13:13:38 -0700 | [diff] [blame] | 60 | MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 61 | |
| 62 | #define PCI_VENDOR_ID_STEC 0x1B39 |
| 63 | #define PCI_DEVICE_ID_S1120 0x0001 |
| 64 | |
| 65 | #define SKD_FUA_NV (1 << 1) |
| 66 | #define SKD_MINORS_PER_DEVICE 16 |
| 67 | |
| 68 | #define SKD_MAX_QUEUE_DEPTH 200u |
| 69 | |
| 70 | #define SKD_PAUSE_TIMEOUT (5 * 1000) |
| 71 | |
| 72 | #define SKD_N_FITMSG_BYTES (512u) |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 73 | #define SKD_MAX_REQ_PER_MSG 14 |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 74 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 75 | #define SKD_N_SPECIAL_FITMSG_BYTES (128u) |
| 76 | |
| 77 | /* SG elements are 32 bytes, so we can make this 4096 and still be under the |
| 78 | * 128KB limit. That allows 4096*4K = 16M xfer size |
| 79 | */ |
| 80 | #define SKD_N_SG_PER_REQ_DEFAULT 256u |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 81 | |
| 82 | #define SKD_N_COMPLETION_ENTRY 256u |
| 83 | #define SKD_N_READ_CAP_BYTES (8u) |
| 84 | |
| 85 | #define SKD_N_INTERNAL_BYTES (512u) |
| 86 | |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 87 | #define SKD_SKCOMP_SIZE \ |
| 88 | ((sizeof(struct fit_completion_entry_v1) + \ |
| 89 | sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY) |
| 90 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 91 | /* 5 bits of uniqifier, 0xF800 */ |
| 92 | #define SKD_ID_INCR (0x400) |
| 93 | #define SKD_ID_TABLE_MASK (3u << 8u) |
| 94 | #define SKD_ID_RW_REQUEST (0u << 8u) |
| 95 | #define SKD_ID_INTERNAL (1u << 8u) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 96 | #define SKD_ID_FIT_MSG (3u << 8u) |
| 97 | #define SKD_ID_SLOT_MASK 0x00FFu |
| 98 | #define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu |
| 99 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 100 | #define SKD_N_MAX_SECTORS 2048u |
| 101 | |
| 102 | #define SKD_MAX_RETRIES 2u |
| 103 | |
| 104 | #define SKD_TIMER_SECONDS(seconds) (seconds) |
| 105 | #define SKD_TIMER_MINUTES(minutes) ((minutes) * (60)) |
| 106 | |
| 107 | #define INQ_STD_NBYTES 36 |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 108 | |
| 109 | enum skd_drvr_state { |
| 110 | SKD_DRVR_STATE_LOAD, |
| 111 | SKD_DRVR_STATE_IDLE, |
| 112 | SKD_DRVR_STATE_BUSY, |
| 113 | SKD_DRVR_STATE_STARTING, |
| 114 | SKD_DRVR_STATE_ONLINE, |
| 115 | SKD_DRVR_STATE_PAUSING, |
| 116 | SKD_DRVR_STATE_PAUSED, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 117 | SKD_DRVR_STATE_RESTARTING, |
| 118 | SKD_DRVR_STATE_RESUMING, |
| 119 | SKD_DRVR_STATE_STOPPING, |
| 120 | SKD_DRVR_STATE_FAULT, |
| 121 | SKD_DRVR_STATE_DISAPPEARED, |
| 122 | SKD_DRVR_STATE_PROTOCOL_MISMATCH, |
| 123 | SKD_DRVR_STATE_BUSY_ERASE, |
| 124 | SKD_DRVR_STATE_BUSY_SANITIZE, |
| 125 | SKD_DRVR_STATE_BUSY_IMMINENT, |
| 126 | SKD_DRVR_STATE_WAIT_BOOT, |
| 127 | SKD_DRVR_STATE_SYNCING, |
| 128 | }; |
| 129 | |
| 130 | #define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u) |
| 131 | #define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u) |
| 132 | #define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 133 | #define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u) |
| 134 | #define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u) |
| 135 | #define SKD_START_WAIT_SECONDS 90u |
| 136 | |
| 137 | enum skd_req_state { |
| 138 | SKD_REQ_STATE_IDLE, |
| 139 | SKD_REQ_STATE_SETUP, |
| 140 | SKD_REQ_STATE_BUSY, |
| 141 | SKD_REQ_STATE_COMPLETED, |
| 142 | SKD_REQ_STATE_TIMEOUT, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 143 | }; |
| 144 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 145 | enum skd_check_status_action { |
| 146 | SKD_CHECK_STATUS_REPORT_GOOD, |
| 147 | SKD_CHECK_STATUS_REPORT_SMART_ALERT, |
| 148 | SKD_CHECK_STATUS_REQUEUE_REQUEST, |
| 149 | SKD_CHECK_STATUS_REPORT_ERROR, |
| 150 | SKD_CHECK_STATUS_BUSY_IMMINENT, |
| 151 | }; |
| 152 | |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 153 | struct skd_msg_buf { |
| 154 | struct fit_msg_hdr fmh; |
| 155 | struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG]; |
| 156 | }; |
| 157 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 158 | struct skd_fitmsg_context { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 159 | u32 id; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 160 | |
| 161 | u32 length; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 162 | |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 163 | struct skd_msg_buf *msg_buf; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 164 | dma_addr_t mb_dma_address; |
| 165 | }; |
| 166 | |
| 167 | struct skd_request_context { |
| 168 | enum skd_req_state state; |
| 169 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 170 | u16 id; |
| 171 | u32 fitmsg_id; |
| 172 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 173 | u8 flush_cmd; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 174 | |
Bart Van Assche | b1824ee | 2017-08-17 13:13:12 -0700 | [diff] [blame] | 175 | enum dma_data_direction data_dir; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 176 | struct scatterlist *sg; |
| 177 | u32 n_sg; |
| 178 | u32 sg_byte_count; |
| 179 | |
| 180 | struct fit_sg_descriptor *sksg_list; |
| 181 | dma_addr_t sksg_dma_address; |
| 182 | |
| 183 | struct fit_completion_entry_v1 completion; |
| 184 | |
| 185 | struct fit_comp_error_info err_info; |
| 186 | |
| 187 | }; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 188 | |
| 189 | struct skd_special_context { |
| 190 | struct skd_request_context req; |
| 191 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 192 | void *data_buf; |
| 193 | dma_addr_t db_dma_address; |
| 194 | |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 195 | struct skd_msg_buf *msg_buf; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 196 | dma_addr_t mb_dma_address; |
| 197 | }; |
| 198 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 199 | typedef enum skd_irq_type { |
| 200 | SKD_IRQ_LEGACY, |
| 201 | SKD_IRQ_MSI, |
| 202 | SKD_IRQ_MSIX |
| 203 | } skd_irq_type_t; |
| 204 | |
| 205 | #define SKD_MAX_BARS 2 |
| 206 | |
| 207 | struct skd_device { |
Bart Van Assche | 85e3411 | 2017-08-17 13:13:17 -0700 | [diff] [blame] | 208 | void __iomem *mem_map[SKD_MAX_BARS]; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 209 | resource_size_t mem_phys[SKD_MAX_BARS]; |
| 210 | u32 mem_size[SKD_MAX_BARS]; |
| 211 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 212 | struct skd_msix_entry *msix_entries; |
| 213 | |
| 214 | struct pci_dev *pdev; |
| 215 | int pcie_error_reporting_is_enabled; |
| 216 | |
| 217 | spinlock_t lock; |
| 218 | struct gendisk *disk; |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 219 | struct blk_mq_tag_set tag_set; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 220 | struct request_queue *queue; |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 221 | struct skd_fitmsg_context *skmsg; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 222 | struct device *class_dev; |
| 223 | int gendisk_on; |
| 224 | int sync_done; |
| 225 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 226 | u32 devno; |
| 227 | u32 major; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 228 | char isr_name[30]; |
| 229 | |
| 230 | enum skd_drvr_state state; |
| 231 | u32 drive_state; |
| 232 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 233 | u32 cur_max_queue_depth; |
| 234 | u32 queue_low_water_mark; |
| 235 | u32 dev_max_queue_depth; |
| 236 | |
| 237 | u32 num_fitmsg_context; |
| 238 | u32 num_req_context; |
| 239 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 240 | struct skd_fitmsg_context *skmsg_table; |
| 241 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 242 | struct skd_special_context internal_skspcl; |
| 243 | u32 read_cap_blocksize; |
| 244 | u32 read_cap_last_lba; |
| 245 | int read_cap_is_valid; |
| 246 | int inquiry_is_valid; |
| 247 | u8 inq_serial_num[13]; /*12 chars plus null term */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 248 | |
| 249 | u8 skcomp_cycle; |
| 250 | u32 skcomp_ix; |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 251 | struct kmem_cache *msgbuf_cache; |
| 252 | struct kmem_cache *sglist_cache; |
| 253 | struct kmem_cache *databuf_cache; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 254 | struct fit_completion_entry_v1 *skcomp_table; |
| 255 | struct fit_comp_error_info *skerr_table; |
| 256 | dma_addr_t cq_dma_address; |
| 257 | |
| 258 | wait_queue_head_t waitq; |
| 259 | |
| 260 | struct timer_list timer; |
| 261 | u32 timer_countdown; |
| 262 | u32 timer_substate; |
| 263 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 264 | int sgs_per_request; |
| 265 | u32 last_mtd; |
| 266 | |
| 267 | u32 proto_ver; |
| 268 | |
| 269 | int dbg_level; |
| 270 | u32 connect_time_stamp; |
| 271 | int connect_retries; |
| 272 | #define SKD_MAX_CONNECT_RETRIES 16 |
| 273 | u32 drive_jiffies; |
| 274 | |
| 275 | u32 timo_slot; |
| 276 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 277 | struct work_struct start_queue; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 278 | struct work_struct completion_worker; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 279 | }; |
| 280 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 281 | #define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF) |
| 282 | #define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF) |
| 283 | #define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF) |
| 284 | |
| 285 | static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset) |
| 286 | { |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 287 | u32 val = readl(skdev->mem_map[1] + offset); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 288 | |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 289 | if (unlikely(skdev->dbg_level >= 2)) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 290 | dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val); |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 291 | return val; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static inline void skd_reg_write32(struct skd_device *skdev, u32 val, |
| 295 | u32 offset) |
| 296 | { |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 297 | writel(val, skdev->mem_map[1] + offset); |
| 298 | if (unlikely(skdev->dbg_level >= 2)) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 299 | dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static inline void skd_reg_write64(struct skd_device *skdev, u64 val, |
| 303 | u32 offset) |
| 304 | { |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 305 | writeq(val, skdev->mem_map[1] + offset); |
| 306 | if (unlikely(skdev->dbg_level >= 2)) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 307 | dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset, |
| 308 | val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
| 312 | #define SKD_IRQ_DEFAULT SKD_IRQ_MSI |
| 313 | static int skd_isr_type = SKD_IRQ_DEFAULT; |
| 314 | |
| 315 | module_param(skd_isr_type, int, 0444); |
| 316 | MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability." |
| 317 | " (0==legacy, 1==MSI, 2==MSI-X, default==1)"); |
| 318 | |
| 319 | #define SKD_MAX_REQ_PER_MSG_DEFAULT 1 |
| 320 | static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT; |
| 321 | |
| 322 | module_param(skd_max_req_per_msg, int, 0444); |
| 323 | MODULE_PARM_DESC(skd_max_req_per_msg, |
| 324 | "Maximum SCSI requests packed in a single message." |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 325 | " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 326 | |
| 327 | #define SKD_MAX_QUEUE_DEPTH_DEFAULT 64 |
| 328 | #define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64" |
| 329 | static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT; |
| 330 | |
| 331 | module_param(skd_max_queue_depth, int, 0444); |
| 332 | MODULE_PARM_DESC(skd_max_queue_depth, |
| 333 | "Maximum SCSI requests issued to s1120." |
| 334 | " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")"); |
| 335 | |
| 336 | static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT; |
| 337 | module_param(skd_sgs_per_request, int, 0444); |
| 338 | MODULE_PARM_DESC(skd_sgs_per_request, |
| 339 | "Maximum SG elements per block request." |
| 340 | " (1-4096, default==256)"); |
| 341 | |
Bart Van Assche | 6321412 | 2017-08-17 13:13:23 -0700 | [diff] [blame] | 342 | static int skd_max_pass_thru = 1; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 343 | module_param(skd_max_pass_thru, int, 0444); |
| 344 | MODULE_PARM_DESC(skd_max_pass_thru, |
Bart Van Assche | 6321412 | 2017-08-17 13:13:23 -0700 | [diff] [blame] | 345 | "Maximum SCSI pass-thru at a time. IGNORED"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 346 | |
| 347 | module_param(skd_dbg_level, int, 0444); |
| 348 | MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)"); |
| 349 | |
| 350 | module_param(skd_isr_comp_limit, int, 0444); |
| 351 | MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4"); |
| 352 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 353 | /* Major device number dynamically assigned. */ |
| 354 | static u32 skd_major; |
| 355 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 356 | static void skd_destruct(struct skd_device *skdev); |
| 357 | static const struct block_device_operations skd_blockdev_ops; |
| 358 | static void skd_send_fitmsg(struct skd_device *skdev, |
| 359 | struct skd_fitmsg_context *skmsg); |
| 360 | static void skd_send_special_fitmsg(struct skd_device *skdev, |
| 361 | struct skd_special_context *skspcl); |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 362 | static void skd_end_request(struct skd_device *skdev, struct request *req, |
| 363 | blk_status_t status); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 364 | static bool skd_preop_sg_list(struct skd_device *skdev, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 365 | struct skd_request_context *skreq); |
| 366 | static void skd_postop_sg_list(struct skd_device *skdev, |
| 367 | struct skd_request_context *skreq); |
| 368 | |
| 369 | static void skd_restart_device(struct skd_device *skdev); |
| 370 | static int skd_quiesce_dev(struct skd_device *skdev); |
| 371 | static int skd_unquiesce_dev(struct skd_device *skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 372 | static void skd_disable_interrupts(struct skd_device *skdev); |
| 373 | static void skd_isr_fwstate(struct skd_device *skdev); |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 374 | static void skd_recover_requests(struct skd_device *skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 375 | static void skd_soft_reset(struct skd_device *skdev); |
| 376 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 377 | const char *skd_drive_state_to_str(int state); |
| 378 | const char *skd_skdev_state_to_str(enum skd_drvr_state state); |
| 379 | static void skd_log_skdev(struct skd_device *skdev, const char *event); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 380 | static void skd_log_skreq(struct skd_device *skdev, |
| 381 | struct skd_request_context *skreq, const char *event); |
| 382 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 383 | /* |
| 384 | ***************************************************************************** |
| 385 | * READ/WRITE REQUESTS |
| 386 | ***************************************************************************** |
| 387 | */ |
Bart Van Assche | d4d0f5f | 2017-08-17 13:13:34 -0700 | [diff] [blame] | 388 | static void skd_inc_in_flight(struct request *rq, void *data, bool reserved) |
| 389 | { |
| 390 | int *count = data; |
| 391 | |
| 392 | count++; |
| 393 | } |
| 394 | |
| 395 | static int skd_in_flight(struct skd_device *skdev) |
| 396 | { |
| 397 | int count = 0; |
| 398 | |
| 399 | blk_mq_tagset_busy_iter(&skdev->tag_set, skd_inc_in_flight, &count); |
| 400 | |
| 401 | return count; |
| 402 | } |
| 403 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 404 | static void |
| 405 | skd_prep_rw_cdb(struct skd_scsi_request *scsi_req, |
| 406 | int data_dir, unsigned lba, |
| 407 | unsigned count) |
| 408 | { |
| 409 | if (data_dir == READ) |
Bart Van Assche | fb4844b | 2017-08-17 13:13:19 -0700 | [diff] [blame] | 410 | scsi_req->cdb[0] = READ_10; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 411 | else |
Bart Van Assche | fb4844b | 2017-08-17 13:13:19 -0700 | [diff] [blame] | 412 | scsi_req->cdb[0] = WRITE_10; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 413 | |
| 414 | scsi_req->cdb[1] = 0; |
| 415 | scsi_req->cdb[2] = (lba & 0xff000000) >> 24; |
| 416 | scsi_req->cdb[3] = (lba & 0xff0000) >> 16; |
| 417 | scsi_req->cdb[4] = (lba & 0xff00) >> 8; |
| 418 | scsi_req->cdb[5] = (lba & 0xff); |
| 419 | scsi_req->cdb[6] = 0; |
| 420 | scsi_req->cdb[7] = (count & 0xff00) >> 8; |
| 421 | scsi_req->cdb[8] = count & 0xff; |
| 422 | scsi_req->cdb[9] = 0; |
| 423 | } |
| 424 | |
| 425 | static void |
| 426 | skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req, |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 427 | struct skd_request_context *skreq) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 428 | { |
| 429 | skreq->flush_cmd = 1; |
| 430 | |
Bart Van Assche | fb4844b | 2017-08-17 13:13:19 -0700 | [diff] [blame] | 431 | scsi_req->cdb[0] = SYNCHRONIZE_CACHE; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 432 | scsi_req->cdb[1] = 0; |
| 433 | scsi_req->cdb[2] = 0; |
| 434 | scsi_req->cdb[3] = 0; |
| 435 | scsi_req->cdb[4] = 0; |
| 436 | scsi_req->cdb[5] = 0; |
| 437 | scsi_req->cdb[6] = 0; |
| 438 | scsi_req->cdb[7] = 0; |
| 439 | scsi_req->cdb[8] = 0; |
| 440 | scsi_req->cdb[9] = 0; |
| 441 | } |
| 442 | |
Bart Van Assche | 3d17a67 | 2017-08-17 13:13:21 -0700 | [diff] [blame] | 443 | /* |
| 444 | * Return true if and only if all pending requests should be failed. |
| 445 | */ |
| 446 | static bool skd_fail_all(struct request_queue *q) |
Bart Van Assche | cb6981b | 2017-08-17 13:13:20 -0700 | [diff] [blame] | 447 | { |
| 448 | struct skd_device *skdev = q->queuedata; |
| 449 | |
| 450 | SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE); |
| 451 | |
| 452 | skd_log_skdev(skdev, "req_not_online"); |
| 453 | switch (skdev->state) { |
| 454 | case SKD_DRVR_STATE_PAUSING: |
| 455 | case SKD_DRVR_STATE_PAUSED: |
| 456 | case SKD_DRVR_STATE_STARTING: |
| 457 | case SKD_DRVR_STATE_RESTARTING: |
| 458 | case SKD_DRVR_STATE_WAIT_BOOT: |
| 459 | /* In case of starting, we haven't started the queue, |
| 460 | * so we can't get here... but requests are |
| 461 | * possibly hanging out waiting for us because we |
| 462 | * reported the dev/skd0 already. They'll wait |
| 463 | * forever if connect doesn't complete. |
| 464 | * What to do??? delay dev/skd0 ?? |
| 465 | */ |
| 466 | case SKD_DRVR_STATE_BUSY: |
| 467 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 468 | case SKD_DRVR_STATE_BUSY_ERASE: |
Bart Van Assche | 3d17a67 | 2017-08-17 13:13:21 -0700 | [diff] [blame] | 469 | return false; |
Bart Van Assche | cb6981b | 2017-08-17 13:13:20 -0700 | [diff] [blame] | 470 | |
| 471 | case SKD_DRVR_STATE_BUSY_SANITIZE: |
| 472 | case SKD_DRVR_STATE_STOPPING: |
| 473 | case SKD_DRVR_STATE_SYNCING: |
| 474 | case SKD_DRVR_STATE_FAULT: |
| 475 | case SKD_DRVR_STATE_DISAPPEARED: |
| 476 | default: |
Bart Van Assche | 3d17a67 | 2017-08-17 13:13:21 -0700 | [diff] [blame] | 477 | return true; |
Bart Van Assche | cb6981b | 2017-08-17 13:13:20 -0700 | [diff] [blame] | 478 | } |
Bart Van Assche | cb6981b | 2017-08-17 13:13:20 -0700 | [diff] [blame] | 479 | } |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 480 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 481 | static void skd_process_request(struct request *req, bool last) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 482 | { |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 483 | struct request_queue *const q = req->q; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 484 | struct skd_device *skdev = q->queuedata; |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 485 | struct skd_fitmsg_context *skmsg; |
| 486 | struct fit_msg_hdr *fmh; |
| 487 | const u32 tag = blk_mq_unique_tag(req); |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 488 | struct skd_request_context *const skreq = blk_mq_rq_to_pdu(req); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 489 | struct skd_scsi_request *scsi_req; |
Bart Van Assche | 74c7428 | 2017-08-17 13:13:37 -0700 | [diff] [blame] | 490 | unsigned long flags = 0; |
Bart Van Assche | e2bb554 | 2017-08-17 13:13:36 -0700 | [diff] [blame] | 491 | const u32 lba = blk_rq_pos(req); |
| 492 | const u32 count = blk_rq_sectors(req); |
| 493 | const int data_dir = rq_data_dir(req); |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 494 | |
| 495 | WARN_ONCE(tag >= skd_max_queue_depth, "%#x > %#x (nr_requests = %lu)\n", |
| 496 | tag, skd_max_queue_depth, q->nr_requests); |
| 497 | |
| 498 | SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE); |
| 499 | |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 500 | dev_dbg(&skdev->pdev->dev, |
| 501 | "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba, |
| 502 | lba, count, count, data_dir); |
| 503 | |
| 504 | skreq->id = tag + SKD_ID_RW_REQUEST; |
| 505 | skreq->flush_cmd = 0; |
| 506 | skreq->n_sg = 0; |
| 507 | skreq->sg_byte_count = 0; |
| 508 | |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 509 | skreq->fitmsg_id = 0; |
| 510 | |
| 511 | skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 512 | |
| 513 | if (req->bio && !skd_preop_sg_list(skdev, skreq)) { |
| 514 | dev_dbg(&skdev->pdev->dev, "error Out\n"); |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 515 | skd_end_request(skdev, blk_mq_rq_from_pdu(skreq), |
| 516 | BLK_STS_RESOURCE); |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 520 | dma_sync_single_for_device(&skdev->pdev->dev, skreq->sksg_dma_address, |
| 521 | skreq->n_sg * |
| 522 | sizeof(struct fit_sg_descriptor), |
| 523 | DMA_TO_DEVICE); |
| 524 | |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 525 | /* Either a FIT msg is in progress or we have to start one. */ |
Bart Van Assche | 74c7428 | 2017-08-17 13:13:37 -0700 | [diff] [blame] | 526 | if (skd_max_req_per_msg == 1) { |
| 527 | skmsg = NULL; |
| 528 | } else { |
| 529 | spin_lock_irqsave(&skdev->lock, flags); |
| 530 | skmsg = skdev->skmsg; |
| 531 | } |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 532 | if (!skmsg) { |
| 533 | skmsg = &skdev->skmsg_table[tag]; |
| 534 | skdev->skmsg = skmsg; |
| 535 | |
| 536 | /* Initialize the FIT msg header */ |
| 537 | fmh = &skmsg->msg_buf->fmh; |
| 538 | memset(fmh, 0, sizeof(*fmh)); |
| 539 | fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT; |
| 540 | skmsg->length = sizeof(*fmh); |
| 541 | } else { |
| 542 | fmh = &skmsg->msg_buf->fmh; |
| 543 | } |
| 544 | |
| 545 | skreq->fitmsg_id = skmsg->id; |
| 546 | |
| 547 | scsi_req = &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced]; |
| 548 | memset(scsi_req, 0, sizeof(*scsi_req)); |
| 549 | |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 550 | scsi_req->hdr.tag = skreq->id; |
Bart Van Assche | e2bb554 | 2017-08-17 13:13:36 -0700 | [diff] [blame] | 551 | scsi_req->hdr.sg_list_dma_address = |
| 552 | cpu_to_be64(skreq->sksg_dma_address); |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 553 | |
Bart Van Assche | e2bb554 | 2017-08-17 13:13:36 -0700 | [diff] [blame] | 554 | if (req_op(req) == REQ_OP_FLUSH) { |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 555 | skd_prep_zerosize_flush_cdb(scsi_req, skreq); |
| 556 | SKD_ASSERT(skreq->flush_cmd == 1); |
| 557 | } else { |
| 558 | skd_prep_rw_cdb(scsi_req, data_dir, lba, count); |
| 559 | } |
| 560 | |
Bart Van Assche | e2bb554 | 2017-08-17 13:13:36 -0700 | [diff] [blame] | 561 | if (req->cmd_flags & REQ_FUA) |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 562 | scsi_req->cdb[1] |= SKD_FUA_NV; |
| 563 | |
| 564 | scsi_req->hdr.sg_list_len_bytes = cpu_to_be32(skreq->sg_byte_count); |
| 565 | |
| 566 | /* Complete resource allocations. */ |
| 567 | skreq->state = SKD_REQ_STATE_BUSY; |
| 568 | |
| 569 | skmsg->length += sizeof(struct skd_scsi_request); |
| 570 | fmh->num_protocol_cmds_coalesced++; |
| 571 | |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 572 | dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id, |
Bart Van Assche | d4d0f5f | 2017-08-17 13:13:34 -0700 | [diff] [blame] | 573 | skd_in_flight(skdev)); |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * If the FIT msg buffer is full send it. |
| 577 | */ |
Bart Van Assche | 74c7428 | 2017-08-17 13:13:37 -0700 | [diff] [blame] | 578 | if (skd_max_req_per_msg == 1) { |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 579 | skd_send_fitmsg(skdev, skmsg); |
Bart Van Assche | 74c7428 | 2017-08-17 13:13:37 -0700 | [diff] [blame] | 580 | } else { |
| 581 | if (last || |
| 582 | fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) { |
| 583 | skd_send_fitmsg(skdev, skmsg); |
| 584 | skdev->skmsg = NULL; |
| 585 | } |
| 586 | spin_unlock_irqrestore(&skdev->lock, flags); |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 590 | static blk_status_t skd_mq_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 591 | const struct blk_mq_queue_data *mqd) |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 592 | { |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 593 | struct request *req = mqd->rq; |
| 594 | struct request_queue *q = req->q; |
Bart Van Assche | 91f85da | 2017-08-17 13:13:28 -0700 | [diff] [blame] | 595 | struct skd_device *skdev = q->queuedata; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 596 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 597 | if (skdev->state == SKD_DRVR_STATE_ONLINE) { |
| 598 | blk_mq_start_request(req); |
| 599 | skd_process_request(req, mqd->last); |
| 600 | |
| 601 | return BLK_STS_OK; |
| 602 | } else { |
| 603 | return skd_fail_all(q) ? BLK_STS_IOERR : BLK_STS_RESOURCE; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 604 | } |
| 605 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 606 | return BLK_STS_OK; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 607 | } |
| 608 | |
Bart Van Assche | a74d5b7 | 2017-08-17 13:13:33 -0700 | [diff] [blame] | 609 | static enum blk_eh_timer_return skd_timed_out(struct request *req) |
| 610 | { |
| 611 | struct skd_device *skdev = req->q->queuedata; |
| 612 | |
| 613 | dev_err(&skdev->pdev->dev, "request with tag %#x timed out\n", |
| 614 | blk_mq_unique_tag(req)); |
| 615 | |
| 616 | return BLK_EH_HANDLED; |
| 617 | } |
| 618 | |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 619 | static void skd_end_request(struct skd_device *skdev, struct request *req, |
| 620 | blk_status_t error) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 621 | { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 622 | if (unlikely(error)) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 623 | char *cmd = (rq_data_dir(req) == READ) ? "read" : "write"; |
| 624 | u32 lba = (u32)blk_rq_pos(req); |
| 625 | u32 count = blk_rq_sectors(req); |
| 626 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 627 | dev_err(&skdev->pdev->dev, |
| 628 | "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba, |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 629 | count, req->tag); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 630 | } else |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 631 | dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", req->tag, |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 632 | error); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 633 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 634 | blk_mq_end_request(req, error); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 635 | } |
| 636 | |
Bart Van Assche | a74d5b7 | 2017-08-17 13:13:33 -0700 | [diff] [blame] | 637 | /* Only called in case of a request timeout */ |
| 638 | static void skd_softirq_done(struct request *req) |
| 639 | { |
| 640 | struct skd_device *skdev = req->q->queuedata; |
| 641 | struct skd_request_context *skreq = blk_mq_rq_to_pdu(req); |
| 642 | unsigned long flags; |
| 643 | |
| 644 | spin_lock_irqsave(&skdev->lock, flags); |
| 645 | skd_end_request(skdev, blk_mq_rq_from_pdu(skreq), BLK_STS_TIMEOUT); |
| 646 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 647 | } |
| 648 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 649 | static bool skd_preop_sg_list(struct skd_device *skdev, |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 650 | struct skd_request_context *skreq) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 651 | { |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 652 | struct request *req = blk_mq_rq_from_pdu(skreq); |
Bart Van Assche | 06f824c4 | 2017-08-17 13:13:15 -0700 | [diff] [blame] | 653 | struct scatterlist *sgl = &skreq->sg[0], *sg; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 654 | int n_sg; |
| 655 | int i; |
| 656 | |
| 657 | skreq->sg_byte_count = 0; |
| 658 | |
Bart Van Assche | b1824ee | 2017-08-17 13:13:12 -0700 | [diff] [blame] | 659 | WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE && |
| 660 | skreq->data_dir != DMA_FROM_DEVICE); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 661 | |
Bart Van Assche | 06f824c4 | 2017-08-17 13:13:15 -0700 | [diff] [blame] | 662 | n_sg = blk_rq_map_sg(skdev->queue, req, sgl); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 663 | if (n_sg <= 0) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 664 | return false; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 665 | |
| 666 | /* |
| 667 | * Map scatterlist to PCI bus addresses. |
| 668 | * Note PCI might change the number of entries. |
| 669 | */ |
Bart Van Assche | 06f824c4 | 2017-08-17 13:13:15 -0700 | [diff] [blame] | 670 | n_sg = pci_map_sg(skdev->pdev, sgl, n_sg, skreq->data_dir); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 671 | if (n_sg <= 0) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 672 | return false; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 673 | |
| 674 | SKD_ASSERT(n_sg <= skdev->sgs_per_request); |
| 675 | |
| 676 | skreq->n_sg = n_sg; |
| 677 | |
Bart Van Assche | 06f824c4 | 2017-08-17 13:13:15 -0700 | [diff] [blame] | 678 | for_each_sg(sgl, sg, n_sg, i) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 679 | struct fit_sg_descriptor *sgd = &skreq->sksg_list[i]; |
Bart Van Assche | 06f824c4 | 2017-08-17 13:13:15 -0700 | [diff] [blame] | 680 | u32 cnt = sg_dma_len(sg); |
| 681 | uint64_t dma_addr = sg_dma_address(sg); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 682 | |
| 683 | sgd->control = FIT_SGD_CONTROL_NOT_LAST; |
| 684 | sgd->byte_count = cnt; |
| 685 | skreq->sg_byte_count += cnt; |
| 686 | sgd->host_side_addr = dma_addr; |
| 687 | sgd->dev_side_addr = 0; |
| 688 | } |
| 689 | |
| 690 | skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL; |
| 691 | skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST; |
| 692 | |
| 693 | if (unlikely(skdev->dbg_level > 1)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 694 | dev_dbg(&skdev->pdev->dev, |
| 695 | "skreq=%x sksg_list=%p sksg_dma=%llx\n", |
| 696 | skreq->id, skreq->sksg_list, skreq->sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 697 | for (i = 0; i < n_sg; i++) { |
| 698 | struct fit_sg_descriptor *sgd = &skreq->sksg_list[i]; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 699 | |
| 700 | dev_dbg(&skdev->pdev->dev, |
| 701 | " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n", |
| 702 | i, sgd->byte_count, sgd->control, |
| 703 | sgd->host_side_addr, sgd->next_desc_ptr); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 707 | return true; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 708 | } |
| 709 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 710 | static void skd_postop_sg_list(struct skd_device *skdev, |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 711 | struct skd_request_context *skreq) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 712 | { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 713 | /* |
| 714 | * restore the next ptr for next IO request so we |
| 715 | * don't have to set it every time. |
| 716 | */ |
| 717 | skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr = |
| 718 | skreq->sksg_dma_address + |
| 719 | ((skreq->n_sg) * sizeof(struct fit_sg_descriptor)); |
Bart Van Assche | b1824ee | 2017-08-17 13:13:12 -0700 | [diff] [blame] | 720 | pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, skreq->data_dir); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 721 | } |
| 722 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 723 | /* |
| 724 | ***************************************************************************** |
| 725 | * TIMER |
| 726 | ***************************************************************************** |
| 727 | */ |
| 728 | |
| 729 | static void skd_timer_tick_not_online(struct skd_device *skdev); |
| 730 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 731 | static void skd_start_queue(struct work_struct *work) |
| 732 | { |
| 733 | struct skd_device *skdev = container_of(work, typeof(*skdev), |
| 734 | start_queue); |
| 735 | |
| 736 | /* |
| 737 | * Although it is safe to call blk_start_queue() from interrupt |
| 738 | * context, blk_mq_start_hw_queues() must not be called from |
| 739 | * interrupt context. |
| 740 | */ |
| 741 | blk_mq_start_hw_queues(skdev->queue); |
| 742 | } |
| 743 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 744 | static void skd_timer_tick(ulong arg) |
| 745 | { |
| 746 | struct skd_device *skdev = (struct skd_device *)arg; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 747 | unsigned long reqflags; |
| 748 | u32 state; |
| 749 | |
| 750 | if (skdev->state == SKD_DRVR_STATE_FAULT) |
| 751 | /* The driver has declared fault, and we want it to |
| 752 | * stay that way until driver is reloaded. |
| 753 | */ |
| 754 | return; |
| 755 | |
| 756 | spin_lock_irqsave(&skdev->lock, reqflags); |
| 757 | |
| 758 | state = SKD_READL(skdev, FIT_STATUS); |
| 759 | state &= FIT_SR_DRIVE_STATE_MASK; |
| 760 | if (state != skdev->drive_state) |
| 761 | skd_isr_fwstate(skdev); |
| 762 | |
Bart Van Assche | a74d5b7 | 2017-08-17 13:13:33 -0700 | [diff] [blame] | 763 | if (skdev->state != SKD_DRVR_STATE_ONLINE) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 764 | skd_timer_tick_not_online(skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 765 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 766 | mod_timer(&skdev->timer, (jiffies + HZ)); |
| 767 | |
| 768 | spin_unlock_irqrestore(&skdev->lock, reqflags); |
| 769 | } |
| 770 | |
| 771 | static void skd_timer_tick_not_online(struct skd_device *skdev) |
| 772 | { |
| 773 | switch (skdev->state) { |
| 774 | case SKD_DRVR_STATE_IDLE: |
| 775 | case SKD_DRVR_STATE_LOAD: |
| 776 | break; |
| 777 | case SKD_DRVR_STATE_BUSY_SANITIZE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 778 | dev_dbg(&skdev->pdev->dev, |
| 779 | "drive busy sanitize[%x], driver[%x]\n", |
| 780 | skdev->drive_state, skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 781 | /* If we've been in sanitize for 3 seconds, we figure we're not |
| 782 | * going to get anymore completions, so recover requests now |
| 783 | */ |
| 784 | if (skdev->timer_countdown > 0) { |
| 785 | skdev->timer_countdown--; |
| 786 | return; |
| 787 | } |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 788 | skd_recover_requests(skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 789 | break; |
| 790 | |
| 791 | case SKD_DRVR_STATE_BUSY: |
| 792 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 793 | case SKD_DRVR_STATE_BUSY_ERASE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 794 | dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n", |
| 795 | skdev->state, skdev->timer_countdown); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 796 | if (skdev->timer_countdown > 0) { |
| 797 | skdev->timer_countdown--; |
| 798 | return; |
| 799 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 800 | dev_dbg(&skdev->pdev->dev, |
| 801 | "busy[%x], timedout=%d, restarting device.", |
| 802 | skdev->state, skdev->timer_countdown); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 803 | skd_restart_device(skdev); |
| 804 | break; |
| 805 | |
| 806 | case SKD_DRVR_STATE_WAIT_BOOT: |
| 807 | case SKD_DRVR_STATE_STARTING: |
| 808 | if (skdev->timer_countdown > 0) { |
| 809 | skdev->timer_countdown--; |
| 810 | return; |
| 811 | } |
| 812 | /* For now, we fault the drive. Could attempt resets to |
| 813 | * revcover at some point. */ |
| 814 | skdev->state = SKD_DRVR_STATE_FAULT; |
| 815 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 816 | dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n", |
| 817 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 818 | |
| 819 | /*start the queue so we can respond with error to requests */ |
| 820 | /* wakeup anyone waiting for startup complete */ |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 821 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 822 | skdev->gendisk_on = -1; |
| 823 | wake_up_interruptible(&skdev->waitq); |
| 824 | break; |
| 825 | |
| 826 | case SKD_DRVR_STATE_ONLINE: |
| 827 | /* shouldn't get here. */ |
| 828 | break; |
| 829 | |
| 830 | case SKD_DRVR_STATE_PAUSING: |
| 831 | case SKD_DRVR_STATE_PAUSED: |
| 832 | break; |
| 833 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 834 | case SKD_DRVR_STATE_RESTARTING: |
| 835 | if (skdev->timer_countdown > 0) { |
| 836 | skdev->timer_countdown--; |
| 837 | return; |
| 838 | } |
| 839 | /* For now, we fault the drive. Could attempt resets to |
| 840 | * revcover at some point. */ |
| 841 | skdev->state = SKD_DRVR_STATE_FAULT; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 842 | dev_err(&skdev->pdev->dev, |
| 843 | "DriveFault Reconnect Timeout (%x)\n", |
| 844 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * Recovering does two things: |
| 848 | * 1. completes IO with error |
| 849 | * 2. reclaims dma resources |
| 850 | * When is it safe to recover requests? |
| 851 | * - if the drive state is faulted |
| 852 | * - if the state is still soft reset after out timeout |
| 853 | * - if the drive registers are dead (state = FF) |
| 854 | * If it is "unsafe", we still need to recover, so we will |
| 855 | * disable pci bus mastering and disable our interrupts. |
| 856 | */ |
| 857 | |
| 858 | if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) || |
| 859 | (skdev->drive_state == FIT_SR_DRIVE_FAULT) || |
| 860 | (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK)) |
| 861 | /* It never came out of soft reset. Try to |
| 862 | * recover the requests and then let them |
| 863 | * fail. This is to mitigate hung processes. */ |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 864 | skd_recover_requests(skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 865 | else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 866 | dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n", |
| 867 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 868 | pci_disable_device(skdev->pdev); |
| 869 | skd_disable_interrupts(skdev); |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 870 | skd_recover_requests(skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /*start the queue so we can respond with error to requests */ |
| 874 | /* wakeup anyone waiting for startup complete */ |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 875 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 876 | skdev->gendisk_on = -1; |
| 877 | wake_up_interruptible(&skdev->waitq); |
| 878 | break; |
| 879 | |
| 880 | case SKD_DRVR_STATE_RESUMING: |
| 881 | case SKD_DRVR_STATE_STOPPING: |
| 882 | case SKD_DRVR_STATE_SYNCING: |
| 883 | case SKD_DRVR_STATE_FAULT: |
| 884 | case SKD_DRVR_STATE_DISAPPEARED: |
| 885 | default: |
| 886 | break; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | static int skd_start_timer(struct skd_device *skdev) |
| 891 | { |
| 892 | int rc; |
| 893 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 894 | setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev); |
| 895 | |
| 896 | rc = mod_timer(&skdev->timer, (jiffies + HZ)); |
| 897 | if (rc) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 898 | dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 899 | return rc; |
| 900 | } |
| 901 | |
| 902 | static void skd_kill_timer(struct skd_device *skdev) |
| 903 | { |
| 904 | del_timer_sync(&skdev->timer); |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | ***************************************************************************** |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 909 | * INTERNAL REQUESTS -- generated by driver itself |
| 910 | ***************************************************************************** |
| 911 | */ |
| 912 | |
| 913 | static int skd_format_internal_skspcl(struct skd_device *skdev) |
| 914 | { |
| 915 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
| 916 | struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0]; |
| 917 | struct fit_msg_hdr *fmh; |
| 918 | uint64_t dma_address; |
| 919 | struct skd_scsi_request *scsi; |
| 920 | |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 921 | fmh = &skspcl->msg_buf->fmh; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 922 | fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT; |
| 923 | fmh->num_protocol_cmds_coalesced = 1; |
| 924 | |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 925 | scsi = &skspcl->msg_buf->scsi[0]; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 926 | memset(scsi, 0, sizeof(*scsi)); |
| 927 | dma_address = skspcl->req.sksg_dma_address; |
| 928 | scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address); |
Bart Van Assche | 32494df | 2017-08-17 13:13:25 -0700 | [diff] [blame] | 929 | skspcl->req.n_sg = 1; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 930 | sgd->control = FIT_SGD_CONTROL_LAST; |
| 931 | sgd->byte_count = 0; |
| 932 | sgd->host_side_addr = skspcl->db_dma_address; |
| 933 | sgd->dev_side_addr = 0; |
| 934 | sgd->next_desc_ptr = 0LL; |
| 935 | |
| 936 | return 1; |
| 937 | } |
| 938 | |
| 939 | #define WR_BUF_SIZE SKD_N_INTERNAL_BYTES |
| 940 | |
| 941 | static void skd_send_internal_skspcl(struct skd_device *skdev, |
| 942 | struct skd_special_context *skspcl, |
| 943 | u8 opcode) |
| 944 | { |
| 945 | struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0]; |
| 946 | struct skd_scsi_request *scsi; |
| 947 | unsigned char *buf = skspcl->data_buf; |
| 948 | int i; |
| 949 | |
| 950 | if (skspcl->req.state != SKD_REQ_STATE_IDLE) |
| 951 | /* |
| 952 | * A refresh is already in progress. |
| 953 | * Just wait for it to finish. |
| 954 | */ |
| 955 | return; |
| 956 | |
| 957 | SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0); |
| 958 | skspcl->req.state = SKD_REQ_STATE_BUSY; |
| 959 | skspcl->req.id += SKD_ID_INCR; |
| 960 | |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 961 | scsi = &skspcl->msg_buf->scsi[0]; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 962 | scsi->hdr.tag = skspcl->req.id; |
| 963 | |
| 964 | memset(scsi->cdb, 0, sizeof(scsi->cdb)); |
| 965 | |
| 966 | switch (opcode) { |
| 967 | case TEST_UNIT_READY: |
| 968 | scsi->cdb[0] = TEST_UNIT_READY; |
| 969 | sgd->byte_count = 0; |
| 970 | scsi->hdr.sg_list_len_bytes = 0; |
| 971 | break; |
| 972 | |
| 973 | case READ_CAPACITY: |
| 974 | scsi->cdb[0] = READ_CAPACITY; |
| 975 | sgd->byte_count = SKD_N_READ_CAP_BYTES; |
| 976 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 977 | break; |
| 978 | |
| 979 | case INQUIRY: |
| 980 | scsi->cdb[0] = INQUIRY; |
| 981 | scsi->cdb[1] = 0x01; /* evpd */ |
| 982 | scsi->cdb[2] = 0x80; /* serial number page */ |
| 983 | scsi->cdb[4] = 0x10; |
| 984 | sgd->byte_count = 16; |
| 985 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 986 | break; |
| 987 | |
| 988 | case SYNCHRONIZE_CACHE: |
| 989 | scsi->cdb[0] = SYNCHRONIZE_CACHE; |
| 990 | sgd->byte_count = 0; |
| 991 | scsi->hdr.sg_list_len_bytes = 0; |
| 992 | break; |
| 993 | |
| 994 | case WRITE_BUFFER: |
| 995 | scsi->cdb[0] = WRITE_BUFFER; |
| 996 | scsi->cdb[1] = 0x02; |
| 997 | scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8; |
| 998 | scsi->cdb[8] = WR_BUF_SIZE & 0xFF; |
| 999 | sgd->byte_count = WR_BUF_SIZE; |
| 1000 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 1001 | /* fill incrementing byte pattern */ |
| 1002 | for (i = 0; i < sgd->byte_count; i++) |
| 1003 | buf[i] = i & 0xFF; |
| 1004 | break; |
| 1005 | |
| 1006 | case READ_BUFFER: |
| 1007 | scsi->cdb[0] = READ_BUFFER; |
| 1008 | scsi->cdb[1] = 0x02; |
| 1009 | scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8; |
| 1010 | scsi->cdb[8] = WR_BUF_SIZE & 0xFF; |
| 1011 | sgd->byte_count = WR_BUF_SIZE; |
| 1012 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 1013 | memset(skspcl->data_buf, 0, sgd->byte_count); |
| 1014 | break; |
| 1015 | |
| 1016 | default: |
| 1017 | SKD_ASSERT("Don't know what to send"); |
| 1018 | return; |
| 1019 | |
| 1020 | } |
| 1021 | skd_send_special_fitmsg(skdev, skspcl); |
| 1022 | } |
| 1023 | |
| 1024 | static void skd_refresh_device_data(struct skd_device *skdev) |
| 1025 | { |
| 1026 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
| 1027 | |
| 1028 | skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY); |
| 1029 | } |
| 1030 | |
| 1031 | static int skd_chk_read_buf(struct skd_device *skdev, |
| 1032 | struct skd_special_context *skspcl) |
| 1033 | { |
| 1034 | unsigned char *buf = skspcl->data_buf; |
| 1035 | int i; |
| 1036 | |
| 1037 | /* check for incrementing byte pattern */ |
| 1038 | for (i = 0; i < WR_BUF_SIZE; i++) |
| 1039 | if (buf[i] != (i & 0xFF)) |
| 1040 | return 1; |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key, |
| 1046 | u8 code, u8 qual, u8 fruc) |
| 1047 | { |
| 1048 | /* If the check condition is of special interest, log a message */ |
| 1049 | if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02) |
| 1050 | && (code == 0x04) && (qual == 0x06)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1051 | dev_err(&skdev->pdev->dev, |
| 1052 | "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n", |
| 1053 | key, code, qual, fruc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | static void skd_complete_internal(struct skd_device *skdev, |
Bart Van Assche | 85e3411 | 2017-08-17 13:13:17 -0700 | [diff] [blame] | 1058 | struct fit_completion_entry_v1 *skcomp, |
| 1059 | struct fit_comp_error_info *skerr, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1060 | struct skd_special_context *skspcl) |
| 1061 | { |
| 1062 | u8 *buf = skspcl->data_buf; |
| 1063 | u8 status; |
| 1064 | int i; |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 1065 | struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0]; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1066 | |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 1067 | lockdep_assert_held(&skdev->lock); |
| 1068 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1069 | SKD_ASSERT(skspcl == &skdev->internal_skspcl); |
| 1070 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1071 | dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1072 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 1073 | dma_sync_single_for_cpu(&skdev->pdev->dev, |
| 1074 | skspcl->db_dma_address, |
| 1075 | skspcl->req.sksg_list[0].byte_count, |
| 1076 | DMA_BIDIRECTIONAL); |
| 1077 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1078 | skspcl->req.completion = *skcomp; |
| 1079 | skspcl->req.state = SKD_REQ_STATE_IDLE; |
| 1080 | skspcl->req.id += SKD_ID_INCR; |
| 1081 | |
| 1082 | status = skspcl->req.completion.status; |
| 1083 | |
| 1084 | skd_log_check_status(skdev, status, skerr->key, skerr->code, |
| 1085 | skerr->qual, skerr->fruc); |
| 1086 | |
| 1087 | switch (scsi->cdb[0]) { |
| 1088 | case TEST_UNIT_READY: |
| 1089 | if (status == SAM_STAT_GOOD) |
| 1090 | skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER); |
| 1091 | else if ((status == SAM_STAT_CHECK_CONDITION) && |
| 1092 | (skerr->key == MEDIUM_ERROR)) |
| 1093 | skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER); |
| 1094 | else { |
| 1095 | if (skdev->state == SKD_DRVR_STATE_STOPPING) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1096 | dev_dbg(&skdev->pdev->dev, |
| 1097 | "TUR failed, don't send anymore state 0x%x\n", |
| 1098 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1099 | return; |
| 1100 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1101 | dev_dbg(&skdev->pdev->dev, |
| 1102 | "**** TUR failed, retry skerr\n"); |
Bart Van Assche | fb4844b | 2017-08-17 13:13:19 -0700 | [diff] [blame] | 1103 | skd_send_internal_skspcl(skdev, skspcl, |
| 1104 | TEST_UNIT_READY); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1105 | } |
| 1106 | break; |
| 1107 | |
| 1108 | case WRITE_BUFFER: |
| 1109 | if (status == SAM_STAT_GOOD) |
| 1110 | skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER); |
| 1111 | else { |
| 1112 | if (skdev->state == SKD_DRVR_STATE_STOPPING) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1113 | dev_dbg(&skdev->pdev->dev, |
| 1114 | "write buffer failed, don't send anymore state 0x%x\n", |
| 1115 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1116 | return; |
| 1117 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1118 | dev_dbg(&skdev->pdev->dev, |
| 1119 | "**** write buffer failed, retry skerr\n"); |
Bart Van Assche | fb4844b | 2017-08-17 13:13:19 -0700 | [diff] [blame] | 1120 | skd_send_internal_skspcl(skdev, skspcl, |
| 1121 | TEST_UNIT_READY); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1122 | } |
| 1123 | break; |
| 1124 | |
| 1125 | case READ_BUFFER: |
| 1126 | if (status == SAM_STAT_GOOD) { |
| 1127 | if (skd_chk_read_buf(skdev, skspcl) == 0) |
| 1128 | skd_send_internal_skspcl(skdev, skspcl, |
| 1129 | READ_CAPACITY); |
| 1130 | else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1131 | dev_err(&skdev->pdev->dev, |
| 1132 | "*** W/R Buffer mismatch %d ***\n", |
| 1133 | skdev->connect_retries); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1134 | if (skdev->connect_retries < |
| 1135 | SKD_MAX_CONNECT_RETRIES) { |
| 1136 | skdev->connect_retries++; |
| 1137 | skd_soft_reset(skdev); |
| 1138 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1139 | dev_err(&skdev->pdev->dev, |
| 1140 | "W/R Buffer Connect Error\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1141 | return; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | } else { |
| 1146 | if (skdev->state == SKD_DRVR_STATE_STOPPING) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1147 | dev_dbg(&skdev->pdev->dev, |
| 1148 | "read buffer failed, don't send anymore state 0x%x\n", |
| 1149 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1150 | return; |
| 1151 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1152 | dev_dbg(&skdev->pdev->dev, |
| 1153 | "**** read buffer failed, retry skerr\n"); |
Bart Van Assche | fb4844b | 2017-08-17 13:13:19 -0700 | [diff] [blame] | 1154 | skd_send_internal_skspcl(skdev, skspcl, |
| 1155 | TEST_UNIT_READY); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1156 | } |
| 1157 | break; |
| 1158 | |
| 1159 | case READ_CAPACITY: |
| 1160 | skdev->read_cap_is_valid = 0; |
| 1161 | if (status == SAM_STAT_GOOD) { |
| 1162 | skdev->read_cap_last_lba = |
| 1163 | (buf[0] << 24) | (buf[1] << 16) | |
| 1164 | (buf[2] << 8) | buf[3]; |
| 1165 | skdev->read_cap_blocksize = |
| 1166 | (buf[4] << 24) | (buf[5] << 16) | |
| 1167 | (buf[6] << 8) | buf[7]; |
| 1168 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1169 | dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n", |
| 1170 | skdev->read_cap_last_lba, |
| 1171 | skdev->read_cap_blocksize); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1172 | |
| 1173 | set_capacity(skdev->disk, skdev->read_cap_last_lba + 1); |
| 1174 | |
| 1175 | skdev->read_cap_is_valid = 1; |
| 1176 | |
| 1177 | skd_send_internal_skspcl(skdev, skspcl, INQUIRY); |
| 1178 | } else if ((status == SAM_STAT_CHECK_CONDITION) && |
| 1179 | (skerr->key == MEDIUM_ERROR)) { |
| 1180 | skdev->read_cap_last_lba = ~0; |
| 1181 | set_capacity(skdev->disk, skdev->read_cap_last_lba + 1); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1182 | dev_dbg(&skdev->pdev->dev, "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1183 | skd_send_internal_skspcl(skdev, skspcl, INQUIRY); |
| 1184 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1185 | dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1186 | skd_send_internal_skspcl(skdev, skspcl, |
| 1187 | TEST_UNIT_READY); |
| 1188 | } |
| 1189 | break; |
| 1190 | |
| 1191 | case INQUIRY: |
| 1192 | skdev->inquiry_is_valid = 0; |
| 1193 | if (status == SAM_STAT_GOOD) { |
| 1194 | skdev->inquiry_is_valid = 1; |
| 1195 | |
| 1196 | for (i = 0; i < 12; i++) |
| 1197 | skdev->inq_serial_num[i] = buf[i + 4]; |
| 1198 | skdev->inq_serial_num[12] = 0; |
| 1199 | } |
| 1200 | |
| 1201 | if (skd_unquiesce_dev(skdev) < 0) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1202 | dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1203 | /* connection is complete */ |
| 1204 | skdev->connect_retries = 0; |
| 1205 | break; |
| 1206 | |
| 1207 | case SYNCHRONIZE_CACHE: |
| 1208 | if (status == SAM_STAT_GOOD) |
| 1209 | skdev->sync_done = 1; |
| 1210 | else |
| 1211 | skdev->sync_done = -1; |
| 1212 | wake_up_interruptible(&skdev->waitq); |
| 1213 | break; |
| 1214 | |
| 1215 | default: |
| 1216 | SKD_ASSERT("we didn't send this"); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | ***************************************************************************** |
| 1222 | * FIT MESSAGES |
| 1223 | ***************************************************************************** |
| 1224 | */ |
| 1225 | |
| 1226 | static void skd_send_fitmsg(struct skd_device *skdev, |
| 1227 | struct skd_fitmsg_context *skmsg) |
| 1228 | { |
| 1229 | u64 qcmd; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1230 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1231 | dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n", |
Bart Van Assche | d4d0f5f | 2017-08-17 13:13:34 -0700 | [diff] [blame] | 1232 | skmsg->mb_dma_address, skd_in_flight(skdev)); |
Bart Van Assche | 6507f43 | 2017-08-17 13:13:06 -0700 | [diff] [blame] | 1233 | dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1234 | |
| 1235 | qcmd = skmsg->mb_dma_address; |
| 1236 | qcmd |= FIT_QCMD_QID_NORMAL; |
| 1237 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1238 | if (unlikely(skdev->dbg_level > 1)) { |
| 1239 | u8 *bp = (u8 *)skmsg->msg_buf; |
| 1240 | int i; |
| 1241 | for (i = 0; i < skmsg->length; i += 8) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1242 | dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i, |
| 1243 | &bp[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1244 | if (i == 0) |
| 1245 | i = 64 - 8; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | if (skmsg->length > 256) |
| 1250 | qcmd |= FIT_QCMD_MSGSIZE_512; |
| 1251 | else if (skmsg->length > 128) |
| 1252 | qcmd |= FIT_QCMD_MSGSIZE_256; |
| 1253 | else if (skmsg->length > 64) |
| 1254 | qcmd |= FIT_QCMD_MSGSIZE_128; |
| 1255 | else |
| 1256 | /* |
| 1257 | * This makes no sense because the FIT msg header is |
| 1258 | * 64 bytes. If the msg is only 64 bytes long it has |
| 1259 | * no payload. |
| 1260 | */ |
| 1261 | qcmd |= FIT_QCMD_MSGSIZE_64; |
| 1262 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 1263 | dma_sync_single_for_device(&skdev->pdev->dev, skmsg->mb_dma_address, |
| 1264 | skmsg->length, DMA_TO_DEVICE); |
| 1265 | |
Bart Van Assche | 5fbd545 | 2017-08-17 13:12:46 -0700 | [diff] [blame] | 1266 | /* Make sure skd_msg_buf is written before the doorbell is triggered. */ |
| 1267 | smp_wmb(); |
| 1268 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1269 | SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | static void skd_send_special_fitmsg(struct skd_device *skdev, |
| 1273 | struct skd_special_context *skspcl) |
| 1274 | { |
| 1275 | u64 qcmd; |
| 1276 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 1277 | WARN_ON_ONCE(skspcl->req.n_sg != 1); |
| 1278 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1279 | if (unlikely(skdev->dbg_level > 1)) { |
| 1280 | u8 *bp = (u8 *)skspcl->msg_buf; |
| 1281 | int i; |
| 1282 | |
| 1283 | for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1284 | dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i, |
| 1285 | &bp[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1286 | if (i == 0) |
| 1287 | i = 64 - 8; |
| 1288 | } |
| 1289 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1290 | dev_dbg(&skdev->pdev->dev, |
| 1291 | "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n", |
| 1292 | skspcl, skspcl->req.id, skspcl->req.sksg_list, |
| 1293 | skspcl->req.sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1294 | for (i = 0; i < skspcl->req.n_sg; i++) { |
| 1295 | struct fit_sg_descriptor *sgd = |
| 1296 | &skspcl->req.sksg_list[i]; |
| 1297 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1298 | dev_dbg(&skdev->pdev->dev, |
| 1299 | " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n", |
| 1300 | i, sgd->byte_count, sgd->control, |
| 1301 | sgd->host_side_addr, sgd->next_desc_ptr); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr |
| 1307 | * and one 64-byte SSDI command. |
| 1308 | */ |
| 1309 | qcmd = skspcl->mb_dma_address; |
| 1310 | qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128; |
| 1311 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 1312 | dma_sync_single_for_device(&skdev->pdev->dev, skspcl->mb_dma_address, |
| 1313 | SKD_N_SPECIAL_FITMSG_BYTES, DMA_TO_DEVICE); |
| 1314 | dma_sync_single_for_device(&skdev->pdev->dev, |
| 1315 | skspcl->req.sksg_dma_address, |
| 1316 | 1 * sizeof(struct fit_sg_descriptor), |
| 1317 | DMA_TO_DEVICE); |
| 1318 | dma_sync_single_for_device(&skdev->pdev->dev, |
| 1319 | skspcl->db_dma_address, |
| 1320 | skspcl->req.sksg_list[0].byte_count, |
| 1321 | DMA_BIDIRECTIONAL); |
| 1322 | |
Bart Van Assche | 5fbd545 | 2017-08-17 13:12:46 -0700 | [diff] [blame] | 1323 | /* Make sure skd_msg_buf is written before the doorbell is triggered. */ |
| 1324 | smp_wmb(); |
| 1325 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1326 | SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND); |
| 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | ***************************************************************************** |
| 1331 | * COMPLETION QUEUE |
| 1332 | ***************************************************************************** |
| 1333 | */ |
| 1334 | |
| 1335 | static void skd_complete_other(struct skd_device *skdev, |
Bart Van Assche | 85e3411 | 2017-08-17 13:13:17 -0700 | [diff] [blame] | 1336 | struct fit_completion_entry_v1 *skcomp, |
| 1337 | struct fit_comp_error_info *skerr); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1338 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1339 | struct sns_info { |
| 1340 | u8 type; |
| 1341 | u8 stat; |
| 1342 | u8 key; |
| 1343 | u8 asc; |
| 1344 | u8 ascq; |
| 1345 | u8 mask; |
| 1346 | enum skd_check_status_action action; |
| 1347 | }; |
| 1348 | |
| 1349 | static struct sns_info skd_chkstat_table[] = { |
| 1350 | /* Good */ |
| 1351 | { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c, |
| 1352 | SKD_CHECK_STATUS_REPORT_GOOD }, |
| 1353 | |
| 1354 | /* Smart alerts */ |
| 1355 | { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */ |
| 1356 | SKD_CHECK_STATUS_REPORT_SMART_ALERT }, |
| 1357 | { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */ |
| 1358 | SKD_CHECK_STATUS_REPORT_SMART_ALERT }, |
| 1359 | { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */ |
| 1360 | SKD_CHECK_STATUS_REPORT_SMART_ALERT }, |
| 1361 | |
| 1362 | /* Retry (with limits) */ |
| 1363 | { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */ |
| 1364 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 1365 | { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */ |
| 1366 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 1367 | { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */ |
| 1368 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 1369 | { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */ |
| 1370 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 1371 | |
| 1372 | /* Busy (or about to be) */ |
| 1373 | { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */ |
| 1374 | SKD_CHECK_STATUS_BUSY_IMMINENT }, |
| 1375 | }; |
| 1376 | |
| 1377 | /* |
| 1378 | * Look up status and sense data to decide how to handle the error |
| 1379 | * from the device. |
| 1380 | * mask says which fields must match e.g., mask=0x18 means check |
| 1381 | * type and stat, ignore key, asc, ascq. |
| 1382 | */ |
| 1383 | |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 1384 | static enum skd_check_status_action |
| 1385 | skd_check_status(struct skd_device *skdev, |
Bart Van Assche | 85e3411 | 2017-08-17 13:13:17 -0700 | [diff] [blame] | 1386 | u8 cmp_status, struct fit_comp_error_info *skerr) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1387 | { |
Bart Van Assche | 0b2e0c0 | 2017-08-17 13:13:11 -0700 | [diff] [blame] | 1388 | int i; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1389 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1390 | dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n", |
| 1391 | skerr->key, skerr->code, skerr->qual, skerr->fruc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1392 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1393 | dev_dbg(&skdev->pdev->dev, |
| 1394 | "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n", |
| 1395 | skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual, |
| 1396 | skerr->fruc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1397 | |
| 1398 | /* Does the info match an entry in the good category? */ |
Bart Van Assche | 0b2e0c0 | 2017-08-17 13:13:11 -0700 | [diff] [blame] | 1399 | for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1400 | struct sns_info *sns = &skd_chkstat_table[i]; |
| 1401 | |
| 1402 | if (sns->mask & 0x10) |
| 1403 | if (skerr->type != sns->type) |
| 1404 | continue; |
| 1405 | |
| 1406 | if (sns->mask & 0x08) |
| 1407 | if (cmp_status != sns->stat) |
| 1408 | continue; |
| 1409 | |
| 1410 | if (sns->mask & 0x04) |
| 1411 | if (skerr->key != sns->key) |
| 1412 | continue; |
| 1413 | |
| 1414 | if (sns->mask & 0x02) |
| 1415 | if (skerr->code != sns->asc) |
| 1416 | continue; |
| 1417 | |
| 1418 | if (sns->mask & 0x01) |
| 1419 | if (skerr->qual != sns->ascq) |
| 1420 | continue; |
| 1421 | |
| 1422 | if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1423 | dev_err(&skdev->pdev->dev, |
| 1424 | "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n", |
| 1425 | skerr->key, skerr->code, skerr->qual); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1426 | } |
| 1427 | return sns->action; |
| 1428 | } |
| 1429 | |
| 1430 | /* No other match, so nonzero status means error, |
| 1431 | * zero status means good |
| 1432 | */ |
| 1433 | if (cmp_status) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1434 | dev_dbg(&skdev->pdev->dev, "status check: error\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1435 | return SKD_CHECK_STATUS_REPORT_ERROR; |
| 1436 | } |
| 1437 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1438 | dev_dbg(&skdev->pdev->dev, "status check good default\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1439 | return SKD_CHECK_STATUS_REPORT_GOOD; |
| 1440 | } |
| 1441 | |
| 1442 | static void skd_resolve_req_exception(struct skd_device *skdev, |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1443 | struct skd_request_context *skreq, |
| 1444 | struct request *req) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1445 | { |
| 1446 | u8 cmp_status = skreq->completion.status; |
| 1447 | |
| 1448 | switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) { |
| 1449 | case SKD_CHECK_STATUS_REPORT_GOOD: |
| 1450 | case SKD_CHECK_STATUS_REPORT_SMART_ALERT: |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1451 | skd_end_request(skdev, req, BLK_STS_OK); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1452 | break; |
| 1453 | |
| 1454 | case SKD_CHECK_STATUS_BUSY_IMMINENT: |
| 1455 | skd_log_skreq(skdev, skreq, "retry(busy)"); |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1456 | blk_requeue_request(skdev->queue, req); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1457 | dev_info(&skdev->pdev->dev, "drive BUSY imminent\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1458 | skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT; |
| 1459 | skdev->timer_countdown = SKD_TIMER_MINUTES(20); |
| 1460 | skd_quiesce_dev(skdev); |
| 1461 | break; |
| 1462 | |
| 1463 | case SKD_CHECK_STATUS_REQUEUE_REQUEST: |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1464 | if ((unsigned long) ++req->special < SKD_MAX_RETRIES) { |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 1465 | skd_log_skreq(skdev, skreq, "retry"); |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1466 | blk_requeue_request(skdev->queue, req); |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 1467 | break; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1468 | } |
Bart Van Assche | ce6882b | 2017-08-17 13:12:52 -0700 | [diff] [blame] | 1469 | /* fall through */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1470 | |
| 1471 | case SKD_CHECK_STATUS_REPORT_ERROR: |
| 1472 | default: |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1473 | skd_end_request(skdev, req, BLK_STS_IOERR); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1478 | static void skd_release_skreq(struct skd_device *skdev, |
| 1479 | struct skd_request_context *skreq) |
| 1480 | { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1481 | /* |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1482 | * Reclaim the skd_request_context |
| 1483 | */ |
| 1484 | skreq->state = SKD_REQ_STATE_IDLE; |
| 1485 | skreq->id += SKD_ID_INCR; |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1488 | static int skd_isr_completion_posted(struct skd_device *skdev, |
| 1489 | int limit, int *enqueued) |
| 1490 | { |
Bart Van Assche | 85e3411 | 2017-08-17 13:13:17 -0700 | [diff] [blame] | 1491 | struct fit_completion_entry_v1 *skcmp; |
| 1492 | struct fit_comp_error_info *skerr; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1493 | u16 req_id; |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1494 | u32 tag; |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1495 | u16 hwq = 0; |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1496 | struct request *rq; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1497 | struct skd_request_context *skreq; |
Bart Van Assche | c830da8 | 2017-08-17 13:13:13 -0700 | [diff] [blame] | 1498 | u16 cmp_cntxt; |
| 1499 | u8 cmp_status; |
| 1500 | u8 cmp_cycle; |
| 1501 | u32 cmp_bytes; |
Dan Carpenter | c0b3dda | 2017-08-23 13:44:20 +0300 | [diff] [blame^] | 1502 | int rc = 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1503 | int processed = 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1504 | |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 1505 | lockdep_assert_held(&skdev->lock); |
| 1506 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1507 | for (;; ) { |
| 1508 | SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY); |
| 1509 | |
| 1510 | skcmp = &skdev->skcomp_table[skdev->skcomp_ix]; |
| 1511 | cmp_cycle = skcmp->cycle; |
| 1512 | cmp_cntxt = skcmp->tag; |
| 1513 | cmp_status = skcmp->status; |
| 1514 | cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes); |
| 1515 | |
| 1516 | skerr = &skdev->skerr_table[skdev->skcomp_ix]; |
| 1517 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1518 | dev_dbg(&skdev->pdev->dev, |
| 1519 | "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n", |
| 1520 | skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle, |
Bart Van Assche | d4d0f5f | 2017-08-17 13:13:34 -0700 | [diff] [blame] | 1521 | cmp_cntxt, cmp_status, skd_in_flight(skdev), |
Bart Van Assche | 6fbb2de | 2017-08-17 13:13:27 -0700 | [diff] [blame] | 1522 | cmp_bytes, skdev->proto_ver); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1523 | |
| 1524 | if (cmp_cycle != skdev->skcomp_cycle) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1525 | dev_dbg(&skdev->pdev->dev, "end of completions\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1526 | break; |
| 1527 | } |
| 1528 | /* |
| 1529 | * Update the completion queue head index and possibly |
| 1530 | * the completion cycle count. 8-bit wrap-around. |
| 1531 | */ |
| 1532 | skdev->skcomp_ix++; |
| 1533 | if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) { |
| 1534 | skdev->skcomp_ix = 0; |
| 1535 | skdev->skcomp_cycle++; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * The command context is a unique 32-bit ID. The low order |
| 1540 | * bits help locate the request. The request is usually a |
| 1541 | * r/w request (see skd_start() above) or a special request. |
| 1542 | */ |
| 1543 | req_id = cmp_cntxt; |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1544 | tag = req_id & SKD_ID_SLOT_AND_TABLE_MASK; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1545 | |
| 1546 | /* Is this other than a r/w request? */ |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1547 | if (tag >= skdev->num_req_context) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1548 | /* |
| 1549 | * This is not a completion for a r/w request. |
| 1550 | */ |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1551 | WARN_ON_ONCE(blk_mq_tag_to_rq(skdev->tag_set.tags[hwq], |
| 1552 | tag)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1553 | skd_complete_other(skdev, skcmp, skerr); |
| 1554 | continue; |
| 1555 | } |
| 1556 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1557 | rq = blk_mq_tag_to_rq(skdev->tag_set.tags[hwq], tag); |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1558 | if (WARN(!rq, "No request for tag %#x -> %#x\n", cmp_cntxt, |
| 1559 | tag)) |
| 1560 | continue; |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 1561 | skreq = blk_mq_rq_to_pdu(rq); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1562 | |
| 1563 | /* |
| 1564 | * Make sure the request ID for the slot matches. |
| 1565 | */ |
| 1566 | if (skreq->id != req_id) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1567 | dev_dbg(&skdev->pdev->dev, |
| 1568 | "mismatch comp_id=0x%x req_id=0x%x\n", req_id, |
| 1569 | skreq->id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1570 | { |
| 1571 | u16 new_id = cmp_cntxt; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1572 | dev_err(&skdev->pdev->dev, |
| 1573 | "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n", |
| 1574 | req_id, skreq->id, new_id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1575 | |
| 1576 | continue; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY); |
| 1581 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1582 | skreq->completion = *skcmp; |
| 1583 | if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) { |
| 1584 | skreq->err_info = *skerr; |
| 1585 | skd_log_check_status(skdev, cmp_status, skerr->key, |
| 1586 | skerr->code, skerr->qual, |
| 1587 | skerr->fruc); |
| 1588 | } |
| 1589 | /* Release DMA resources for the request. */ |
| 1590 | if (skreq->n_sg > 0) |
| 1591 | skd_postop_sg_list(skdev, skreq); |
| 1592 | |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1593 | skd_release_skreq(skdev, skreq); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1594 | |
| 1595 | /* |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1596 | * Capture the outcome and post it back to the native request. |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1597 | */ |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 1598 | if (likely(cmp_status == SAM_STAT_GOOD)) |
| 1599 | skd_end_request(skdev, rq, BLK_STS_OK); |
| 1600 | else |
| 1601 | skd_resolve_req_exception(skdev, skreq, rq); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1602 | |
| 1603 | /* skd_isr_comp_limit equal zero means no limit */ |
| 1604 | if (limit) { |
| 1605 | if (++processed >= limit) { |
| 1606 | rc = 1; |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | |
Bart Van Assche | 6fbb2de | 2017-08-17 13:13:27 -0700 | [diff] [blame] | 1612 | if (skdev->state == SKD_DRVR_STATE_PAUSING && |
Bart Van Assche | d4d0f5f | 2017-08-17 13:13:34 -0700 | [diff] [blame] | 1613 | skd_in_flight(skdev) == 0) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1614 | skdev->state = SKD_DRVR_STATE_PAUSED; |
| 1615 | wake_up_interruptible(&skdev->waitq); |
| 1616 | } |
| 1617 | |
| 1618 | return rc; |
| 1619 | } |
| 1620 | |
| 1621 | static void skd_complete_other(struct skd_device *skdev, |
Bart Van Assche | 85e3411 | 2017-08-17 13:13:17 -0700 | [diff] [blame] | 1622 | struct fit_completion_entry_v1 *skcomp, |
| 1623 | struct fit_comp_error_info *skerr) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1624 | { |
| 1625 | u32 req_id = 0; |
| 1626 | u32 req_table; |
| 1627 | u32 req_slot; |
| 1628 | struct skd_special_context *skspcl; |
| 1629 | |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 1630 | lockdep_assert_held(&skdev->lock); |
| 1631 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1632 | req_id = skcomp->tag; |
| 1633 | req_table = req_id & SKD_ID_TABLE_MASK; |
| 1634 | req_slot = req_id & SKD_ID_SLOT_MASK; |
| 1635 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1636 | dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table, |
| 1637 | req_id, req_slot); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1638 | |
| 1639 | /* |
| 1640 | * Based on the request id, determine how to dispatch this completion. |
| 1641 | * This swich/case is finding the good cases and forwarding the |
| 1642 | * completion entry. Errors are reported below the switch. |
| 1643 | */ |
| 1644 | switch (req_table) { |
| 1645 | case SKD_ID_RW_REQUEST: |
| 1646 | /* |
Bart Van Assche | e1d06f2 | 2017-08-17 13:12:54 -0700 | [diff] [blame] | 1647 | * The caller, skd_isr_completion_posted() above, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1648 | * handles r/w requests. The only way we get here |
| 1649 | * is if the req_slot is out of bounds. |
| 1650 | */ |
| 1651 | break; |
| 1652 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1653 | case SKD_ID_INTERNAL: |
| 1654 | if (req_slot == 0) { |
| 1655 | skspcl = &skdev->internal_skspcl; |
| 1656 | if (skspcl->req.id == req_id && |
| 1657 | skspcl->req.state == SKD_REQ_STATE_BUSY) { |
| 1658 | skd_complete_internal(skdev, |
| 1659 | skcomp, skerr, skspcl); |
| 1660 | return; |
| 1661 | } |
| 1662 | } |
| 1663 | break; |
| 1664 | |
| 1665 | case SKD_ID_FIT_MSG: |
| 1666 | /* |
| 1667 | * These id's should never appear in a completion record. |
| 1668 | */ |
| 1669 | break; |
| 1670 | |
| 1671 | default: |
| 1672 | /* |
| 1673 | * These id's should never appear anywhere; |
| 1674 | */ |
| 1675 | break; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * If we get here it is a bad or stale id. |
| 1680 | */ |
| 1681 | } |
| 1682 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1683 | static void skd_reset_skcomp(struct skd_device *skdev) |
| 1684 | { |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 1685 | memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1686 | |
| 1687 | skdev->skcomp_ix = 0; |
| 1688 | skdev->skcomp_cycle = 1; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | ***************************************************************************** |
| 1693 | * INTERRUPTS |
| 1694 | ***************************************************************************** |
| 1695 | */ |
| 1696 | static void skd_completion_worker(struct work_struct *work) |
| 1697 | { |
| 1698 | struct skd_device *skdev = |
| 1699 | container_of(work, struct skd_device, completion_worker); |
| 1700 | unsigned long flags; |
| 1701 | int flush_enqueued = 0; |
| 1702 | |
| 1703 | spin_lock_irqsave(&skdev->lock, flags); |
| 1704 | |
| 1705 | /* |
| 1706 | * pass in limit=0, which means no limit.. |
| 1707 | * process everything in compq |
| 1708 | */ |
| 1709 | skd_isr_completion_posted(skdev, 0, &flush_enqueued); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1710 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1711 | |
| 1712 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 1713 | } |
| 1714 | |
| 1715 | static void skd_isr_msg_from_dev(struct skd_device *skdev); |
| 1716 | |
Arnd Bergmann | 41c9499 | 2016-11-09 13:55:35 +0100 | [diff] [blame] | 1717 | static irqreturn_t |
| 1718 | skd_isr(int irq, void *ptr) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1719 | { |
Bart Van Assche | 1cd3c1a | 2017-08-17 13:13:10 -0700 | [diff] [blame] | 1720 | struct skd_device *skdev = ptr; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1721 | u32 intstat; |
| 1722 | u32 ack; |
| 1723 | int rc = 0; |
| 1724 | int deferred = 0; |
| 1725 | int flush_enqueued = 0; |
| 1726 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1727 | spin_lock(&skdev->lock); |
| 1728 | |
| 1729 | for (;; ) { |
| 1730 | intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST); |
| 1731 | |
| 1732 | ack = FIT_INT_DEF_MASK; |
| 1733 | ack &= intstat; |
| 1734 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1735 | dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat, |
| 1736 | ack); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1737 | |
| 1738 | /* As long as there is an int pending on device, keep |
| 1739 | * running loop. When none, get out, but if we've never |
| 1740 | * done any processing, call completion handler? |
| 1741 | */ |
| 1742 | if (ack == 0) { |
| 1743 | /* No interrupts on device, but run the completion |
| 1744 | * processor anyway? |
| 1745 | */ |
| 1746 | if (rc == 0) |
| 1747 | if (likely (skdev->state |
| 1748 | == SKD_DRVR_STATE_ONLINE)) |
| 1749 | deferred = 1; |
| 1750 | break; |
| 1751 | } |
| 1752 | |
| 1753 | rc = IRQ_HANDLED; |
| 1754 | |
| 1755 | SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST); |
| 1756 | |
| 1757 | if (likely((skdev->state != SKD_DRVR_STATE_LOAD) && |
| 1758 | (skdev->state != SKD_DRVR_STATE_STOPPING))) { |
| 1759 | if (intstat & FIT_ISH_COMPLETION_POSTED) { |
| 1760 | /* |
| 1761 | * If we have already deferred completion |
| 1762 | * processing, don't bother running it again |
| 1763 | */ |
| 1764 | if (deferred == 0) |
| 1765 | deferred = |
| 1766 | skd_isr_completion_posted(skdev, |
| 1767 | skd_isr_comp_limit, &flush_enqueued); |
| 1768 | } |
| 1769 | |
| 1770 | if (intstat & FIT_ISH_FW_STATE_CHANGE) { |
| 1771 | skd_isr_fwstate(skdev); |
| 1772 | if (skdev->state == SKD_DRVR_STATE_FAULT || |
| 1773 | skdev->state == |
| 1774 | SKD_DRVR_STATE_DISAPPEARED) { |
| 1775 | spin_unlock(&skdev->lock); |
| 1776 | return rc; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | if (intstat & FIT_ISH_MSG_FROM_DEV) |
| 1781 | skd_isr_msg_from_dev(skdev); |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | if (unlikely(flush_enqueued)) |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1786 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1787 | |
| 1788 | if (deferred) |
| 1789 | schedule_work(&skdev->completion_worker); |
| 1790 | else if (!flush_enqueued) |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1791 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1792 | |
| 1793 | spin_unlock(&skdev->lock); |
| 1794 | |
| 1795 | return rc; |
| 1796 | } |
| 1797 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1798 | static void skd_drive_fault(struct skd_device *skdev) |
| 1799 | { |
| 1800 | skdev->state = SKD_DRVR_STATE_FAULT; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1801 | dev_err(&skdev->pdev->dev, "Drive FAULT\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | static void skd_drive_disappeared(struct skd_device *skdev) |
| 1805 | { |
| 1806 | skdev->state = SKD_DRVR_STATE_DISAPPEARED; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1807 | dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | static void skd_isr_fwstate(struct skd_device *skdev) |
| 1811 | { |
| 1812 | u32 sense; |
| 1813 | u32 state; |
| 1814 | u32 mtd; |
| 1815 | int prev_driver_state = skdev->state; |
| 1816 | |
| 1817 | sense = SKD_READL(skdev, FIT_STATUS); |
| 1818 | state = sense & FIT_SR_DRIVE_STATE_MASK; |
| 1819 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1820 | dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n", |
| 1821 | skd_drive_state_to_str(skdev->drive_state), skdev->drive_state, |
| 1822 | skd_drive_state_to_str(state), state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1823 | |
| 1824 | skdev->drive_state = state; |
| 1825 | |
| 1826 | switch (skdev->drive_state) { |
| 1827 | case FIT_SR_DRIVE_INIT: |
| 1828 | if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) { |
| 1829 | skd_disable_interrupts(skdev); |
| 1830 | break; |
| 1831 | } |
| 1832 | if (skdev->state == SKD_DRVR_STATE_RESTARTING) |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 1833 | skd_recover_requests(skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1834 | if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) { |
| 1835 | skdev->timer_countdown = SKD_STARTING_TIMO; |
| 1836 | skdev->state = SKD_DRVR_STATE_STARTING; |
| 1837 | skd_soft_reset(skdev); |
| 1838 | break; |
| 1839 | } |
| 1840 | mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0); |
| 1841 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 1842 | skdev->last_mtd = mtd; |
| 1843 | break; |
| 1844 | |
| 1845 | case FIT_SR_DRIVE_ONLINE: |
| 1846 | skdev->cur_max_queue_depth = skd_max_queue_depth; |
| 1847 | if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth) |
| 1848 | skdev->cur_max_queue_depth = skdev->dev_max_queue_depth; |
| 1849 | |
| 1850 | skdev->queue_low_water_mark = |
| 1851 | skdev->cur_max_queue_depth * 2 / 3 + 1; |
| 1852 | if (skdev->queue_low_water_mark < 1) |
| 1853 | skdev->queue_low_water_mark = 1; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1854 | dev_info(&skdev->pdev->dev, |
| 1855 | "Queue depth limit=%d dev=%d lowat=%d\n", |
| 1856 | skdev->cur_max_queue_depth, |
| 1857 | skdev->dev_max_queue_depth, |
| 1858 | skdev->queue_low_water_mark); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1859 | |
| 1860 | skd_refresh_device_data(skdev); |
| 1861 | break; |
| 1862 | |
| 1863 | case FIT_SR_DRIVE_BUSY: |
| 1864 | skdev->state = SKD_DRVR_STATE_BUSY; |
| 1865 | skdev->timer_countdown = SKD_BUSY_TIMO; |
| 1866 | skd_quiesce_dev(skdev); |
| 1867 | break; |
| 1868 | case FIT_SR_DRIVE_BUSY_SANITIZE: |
| 1869 | /* set timer for 3 seconds, we'll abort any unfinished |
| 1870 | * commands after that expires |
| 1871 | */ |
| 1872 | skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE; |
| 1873 | skdev->timer_countdown = SKD_TIMER_SECONDS(3); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1874 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1875 | break; |
| 1876 | case FIT_SR_DRIVE_BUSY_ERASE: |
| 1877 | skdev->state = SKD_DRVR_STATE_BUSY_ERASE; |
| 1878 | skdev->timer_countdown = SKD_BUSY_TIMO; |
| 1879 | break; |
| 1880 | case FIT_SR_DRIVE_OFFLINE: |
| 1881 | skdev->state = SKD_DRVR_STATE_IDLE; |
| 1882 | break; |
| 1883 | case FIT_SR_DRIVE_SOFT_RESET: |
| 1884 | switch (skdev->state) { |
| 1885 | case SKD_DRVR_STATE_STARTING: |
| 1886 | case SKD_DRVR_STATE_RESTARTING: |
| 1887 | /* Expected by a caller of skd_soft_reset() */ |
| 1888 | break; |
| 1889 | default: |
| 1890 | skdev->state = SKD_DRVR_STATE_RESTARTING; |
| 1891 | break; |
| 1892 | } |
| 1893 | break; |
| 1894 | case FIT_SR_DRIVE_FW_BOOTING: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1895 | dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1896 | skdev->state = SKD_DRVR_STATE_WAIT_BOOT; |
| 1897 | skdev->timer_countdown = SKD_WAIT_BOOT_TIMO; |
| 1898 | break; |
| 1899 | |
| 1900 | case FIT_SR_DRIVE_DEGRADED: |
| 1901 | case FIT_SR_PCIE_LINK_DOWN: |
| 1902 | case FIT_SR_DRIVE_NEED_FW_DOWNLOAD: |
| 1903 | break; |
| 1904 | |
| 1905 | case FIT_SR_DRIVE_FAULT: |
| 1906 | skd_drive_fault(skdev); |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 1907 | skd_recover_requests(skdev); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1908 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1909 | break; |
| 1910 | |
| 1911 | /* PCIe bus returned all Fs? */ |
| 1912 | case 0xFF: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1913 | dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state, |
| 1914 | sense); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1915 | skd_drive_disappeared(skdev); |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 1916 | skd_recover_requests(skdev); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1917 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1918 | break; |
| 1919 | default: |
| 1920 | /* |
| 1921 | * Uknown FW State. Wait for a state we recognize. |
| 1922 | */ |
| 1923 | break; |
| 1924 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1925 | dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n", |
| 1926 | skd_skdev_state_to_str(prev_driver_state), prev_driver_state, |
| 1927 | skd_skdev_state_to_str(skdev->state), skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1928 | } |
| 1929 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1930 | static void skd_recover_request(struct request *req, void *data, bool reserved) |
Bart Van Assche | 4e54b84 | 2017-08-17 13:13:29 -0700 | [diff] [blame] | 1931 | { |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1932 | struct skd_device *const skdev = data; |
| 1933 | struct skd_request_context *skreq = blk_mq_rq_to_pdu(req); |
Bart Van Assche | 4e54b84 | 2017-08-17 13:13:29 -0700 | [diff] [blame] | 1934 | |
| 1935 | if (skreq->state != SKD_REQ_STATE_BUSY) |
| 1936 | return; |
| 1937 | |
| 1938 | skd_log_skreq(skdev, skreq, "recover"); |
| 1939 | |
Bart Van Assche | 4e54b84 | 2017-08-17 13:13:29 -0700 | [diff] [blame] | 1940 | /* Release DMA resources for the request. */ |
| 1941 | if (skreq->n_sg > 0) |
| 1942 | skd_postop_sg_list(skdev, skreq); |
| 1943 | |
Bart Van Assche | 4e54b84 | 2017-08-17 13:13:29 -0700 | [diff] [blame] | 1944 | skreq->state = SKD_REQ_STATE_IDLE; |
| 1945 | |
| 1946 | skd_end_request(skdev, req, BLK_STS_IOERR); |
| 1947 | } |
| 1948 | |
Bart Van Assche | 79ce12a | 2017-08-17 13:13:14 -0700 | [diff] [blame] | 1949 | static void skd_recover_requests(struct skd_device *skdev) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1950 | { |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 1951 | blk_mq_tagset_busy_iter(&skdev->tag_set, skd_recover_request, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | static void skd_isr_msg_from_dev(struct skd_device *skdev) |
| 1955 | { |
| 1956 | u32 mfd; |
| 1957 | u32 mtd; |
| 1958 | u32 data; |
| 1959 | |
| 1960 | mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE); |
| 1961 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1962 | dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd, |
| 1963 | skdev->last_mtd); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1964 | |
| 1965 | /* ignore any mtd that is an ack for something we didn't send */ |
| 1966 | if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd)) |
| 1967 | return; |
| 1968 | |
| 1969 | switch (FIT_MXD_TYPE(mfd)) { |
| 1970 | case FIT_MTD_FITFW_INIT: |
| 1971 | skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd); |
| 1972 | |
| 1973 | if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1974 | dev_err(&skdev->pdev->dev, "protocol mismatch\n"); |
| 1975 | dev_err(&skdev->pdev->dev, " got=%d support=%d\n", |
| 1976 | skdev->proto_ver, FIT_PROTOCOL_VERSION_1); |
| 1977 | dev_err(&skdev->pdev->dev, " please upgrade driver\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1978 | skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH; |
| 1979 | skd_soft_reset(skdev); |
| 1980 | break; |
| 1981 | } |
| 1982 | mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0); |
| 1983 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 1984 | skdev->last_mtd = mtd; |
| 1985 | break; |
| 1986 | |
| 1987 | case FIT_MTD_GET_CMDQ_DEPTH: |
| 1988 | skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd); |
| 1989 | mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0, |
| 1990 | SKD_N_COMPLETION_ENTRY); |
| 1991 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 1992 | skdev->last_mtd = mtd; |
| 1993 | break; |
| 1994 | |
| 1995 | case FIT_MTD_SET_COMPQ_DEPTH: |
| 1996 | SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG); |
| 1997 | mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0); |
| 1998 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 1999 | skdev->last_mtd = mtd; |
| 2000 | break; |
| 2001 | |
| 2002 | case FIT_MTD_SET_COMPQ_ADDR: |
| 2003 | skd_reset_skcomp(skdev); |
| 2004 | mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno); |
| 2005 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 2006 | skdev->last_mtd = mtd; |
| 2007 | break; |
| 2008 | |
| 2009 | case FIT_MTD_CMD_LOG_HOST_ID: |
| 2010 | skdev->connect_time_stamp = get_seconds(); |
| 2011 | data = skdev->connect_time_stamp & 0xFFFF; |
| 2012 | mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data); |
| 2013 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 2014 | skdev->last_mtd = mtd; |
| 2015 | break; |
| 2016 | |
| 2017 | case FIT_MTD_CMD_LOG_TIME_STAMP_LO: |
| 2018 | skdev->drive_jiffies = FIT_MXD_DATA(mfd); |
| 2019 | data = (skdev->connect_time_stamp >> 16) & 0xFFFF; |
| 2020 | mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data); |
| 2021 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 2022 | skdev->last_mtd = mtd; |
| 2023 | break; |
| 2024 | |
| 2025 | case FIT_MTD_CMD_LOG_TIME_STAMP_HI: |
| 2026 | skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16); |
| 2027 | mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0); |
| 2028 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 2029 | skdev->last_mtd = mtd; |
| 2030 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2031 | dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n", |
| 2032 | skdev->connect_time_stamp, skdev->drive_jiffies); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2033 | break; |
| 2034 | |
| 2035 | case FIT_MTD_ARM_QUEUE: |
| 2036 | skdev->last_mtd = 0; |
| 2037 | /* |
| 2038 | * State should be, or soon will be, FIT_SR_DRIVE_ONLINE. |
| 2039 | */ |
| 2040 | break; |
| 2041 | |
| 2042 | default: |
| 2043 | break; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | static void skd_disable_interrupts(struct skd_device *skdev) |
| 2048 | { |
| 2049 | u32 sense; |
| 2050 | |
| 2051 | sense = SKD_READL(skdev, FIT_CONTROL); |
| 2052 | sense &= ~FIT_CR_ENABLE_INTERRUPTS; |
| 2053 | SKD_WRITEL(skdev, sense, FIT_CONTROL); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2054 | dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2055 | |
| 2056 | /* Note that the 1s is written. A 1-bit means |
| 2057 | * disable, a 0 means enable. |
| 2058 | */ |
| 2059 | SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST); |
| 2060 | } |
| 2061 | |
| 2062 | static void skd_enable_interrupts(struct skd_device *skdev) |
| 2063 | { |
| 2064 | u32 val; |
| 2065 | |
| 2066 | /* unmask interrupts first */ |
| 2067 | val = FIT_ISH_FW_STATE_CHANGE + |
| 2068 | FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV; |
| 2069 | |
| 2070 | /* Note that the compliment of mask is written. A 1-bit means |
| 2071 | * disable, a 0 means enable. */ |
| 2072 | SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2073 | dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2074 | |
| 2075 | val = SKD_READL(skdev, FIT_CONTROL); |
| 2076 | val |= FIT_CR_ENABLE_INTERRUPTS; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2077 | dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2078 | SKD_WRITEL(skdev, val, FIT_CONTROL); |
| 2079 | } |
| 2080 | |
| 2081 | /* |
| 2082 | ***************************************************************************** |
| 2083 | * START, STOP, RESTART, QUIESCE, UNQUIESCE |
| 2084 | ***************************************************************************** |
| 2085 | */ |
| 2086 | |
| 2087 | static void skd_soft_reset(struct skd_device *skdev) |
| 2088 | { |
| 2089 | u32 val; |
| 2090 | |
| 2091 | val = SKD_READL(skdev, FIT_CONTROL); |
| 2092 | val |= (FIT_CR_SOFT_RESET); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2093 | dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2094 | SKD_WRITEL(skdev, val, FIT_CONTROL); |
| 2095 | } |
| 2096 | |
| 2097 | static void skd_start_device(struct skd_device *skdev) |
| 2098 | { |
| 2099 | unsigned long flags; |
| 2100 | u32 sense; |
| 2101 | u32 state; |
| 2102 | |
| 2103 | spin_lock_irqsave(&skdev->lock, flags); |
| 2104 | |
| 2105 | /* ack all ghost interrupts */ |
| 2106 | SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST); |
| 2107 | |
| 2108 | sense = SKD_READL(skdev, FIT_STATUS); |
| 2109 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2110 | dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2111 | |
| 2112 | state = sense & FIT_SR_DRIVE_STATE_MASK; |
| 2113 | skdev->drive_state = state; |
| 2114 | skdev->last_mtd = 0; |
| 2115 | |
| 2116 | skdev->state = SKD_DRVR_STATE_STARTING; |
| 2117 | skdev->timer_countdown = SKD_STARTING_TIMO; |
| 2118 | |
| 2119 | skd_enable_interrupts(skdev); |
| 2120 | |
| 2121 | switch (skdev->drive_state) { |
| 2122 | case FIT_SR_DRIVE_OFFLINE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2123 | dev_err(&skdev->pdev->dev, "Drive offline...\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2124 | break; |
| 2125 | |
| 2126 | case FIT_SR_DRIVE_FW_BOOTING: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2127 | dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2128 | skdev->state = SKD_DRVR_STATE_WAIT_BOOT; |
| 2129 | skdev->timer_countdown = SKD_WAIT_BOOT_TIMO; |
| 2130 | break; |
| 2131 | |
| 2132 | case FIT_SR_DRIVE_BUSY_SANITIZE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2133 | dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2134 | skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE; |
| 2135 | skdev->timer_countdown = SKD_STARTED_BUSY_TIMO; |
| 2136 | break; |
| 2137 | |
| 2138 | case FIT_SR_DRIVE_BUSY_ERASE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2139 | dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2140 | skdev->state = SKD_DRVR_STATE_BUSY_ERASE; |
| 2141 | skdev->timer_countdown = SKD_STARTED_BUSY_TIMO; |
| 2142 | break; |
| 2143 | |
| 2144 | case FIT_SR_DRIVE_INIT: |
| 2145 | case FIT_SR_DRIVE_ONLINE: |
| 2146 | skd_soft_reset(skdev); |
| 2147 | break; |
| 2148 | |
| 2149 | case FIT_SR_DRIVE_BUSY: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2150 | dev_err(&skdev->pdev->dev, "Drive Busy...\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2151 | skdev->state = SKD_DRVR_STATE_BUSY; |
| 2152 | skdev->timer_countdown = SKD_STARTED_BUSY_TIMO; |
| 2153 | break; |
| 2154 | |
| 2155 | case FIT_SR_DRIVE_SOFT_RESET: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2156 | dev_err(&skdev->pdev->dev, "drive soft reset in prog\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2157 | break; |
| 2158 | |
| 2159 | case FIT_SR_DRIVE_FAULT: |
| 2160 | /* Fault state is bad...soft reset won't do it... |
| 2161 | * Hard reset, maybe, but does it work on device? |
| 2162 | * For now, just fault so the system doesn't hang. |
| 2163 | */ |
| 2164 | skd_drive_fault(skdev); |
| 2165 | /*start the queue so we can respond with error to requests */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2166 | dev_dbg(&skdev->pdev->dev, "starting queue\n"); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2167 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2168 | skdev->gendisk_on = -1; |
| 2169 | wake_up_interruptible(&skdev->waitq); |
| 2170 | break; |
| 2171 | |
| 2172 | case 0xFF: |
| 2173 | /* Most likely the device isn't there or isn't responding |
| 2174 | * to the BAR1 addresses. */ |
| 2175 | skd_drive_disappeared(skdev); |
| 2176 | /*start the queue so we can respond with error to requests */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2177 | dev_dbg(&skdev->pdev->dev, |
| 2178 | "starting queue to error-out reqs\n"); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2179 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2180 | skdev->gendisk_on = -1; |
| 2181 | wake_up_interruptible(&skdev->waitq); |
| 2182 | break; |
| 2183 | |
| 2184 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2185 | dev_err(&skdev->pdev->dev, "Start: unknown state %x\n", |
| 2186 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2187 | break; |
| 2188 | } |
| 2189 | |
| 2190 | state = SKD_READL(skdev, FIT_CONTROL); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2191 | dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2192 | |
| 2193 | state = SKD_READL(skdev, FIT_INT_STATUS_HOST); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2194 | dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2195 | |
| 2196 | state = SKD_READL(skdev, FIT_INT_MASK_HOST); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2197 | dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2198 | |
| 2199 | state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2200 | dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2201 | |
| 2202 | state = SKD_READL(skdev, FIT_HW_VERSION); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2203 | dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2204 | |
| 2205 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2206 | } |
| 2207 | |
| 2208 | static void skd_stop_device(struct skd_device *skdev) |
| 2209 | { |
| 2210 | unsigned long flags; |
| 2211 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
| 2212 | u32 dev_state; |
| 2213 | int i; |
| 2214 | |
| 2215 | spin_lock_irqsave(&skdev->lock, flags); |
| 2216 | |
| 2217 | if (skdev->state != SKD_DRVR_STATE_ONLINE) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2218 | dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2219 | goto stop_out; |
| 2220 | } |
| 2221 | |
| 2222 | if (skspcl->req.state != SKD_REQ_STATE_IDLE) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2223 | dev_err(&skdev->pdev->dev, "%s no special\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2224 | goto stop_out; |
| 2225 | } |
| 2226 | |
| 2227 | skdev->state = SKD_DRVR_STATE_SYNCING; |
| 2228 | skdev->sync_done = 0; |
| 2229 | |
| 2230 | skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE); |
| 2231 | |
| 2232 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2233 | |
| 2234 | wait_event_interruptible_timeout(skdev->waitq, |
| 2235 | (skdev->sync_done), (10 * HZ)); |
| 2236 | |
| 2237 | spin_lock_irqsave(&skdev->lock, flags); |
| 2238 | |
| 2239 | switch (skdev->sync_done) { |
| 2240 | case 0: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2241 | dev_err(&skdev->pdev->dev, "%s no sync\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2242 | break; |
| 2243 | case 1: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2244 | dev_err(&skdev->pdev->dev, "%s sync done\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2245 | break; |
| 2246 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2247 | dev_err(&skdev->pdev->dev, "%s sync error\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | stop_out: |
| 2251 | skdev->state = SKD_DRVR_STATE_STOPPING; |
| 2252 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2253 | |
| 2254 | skd_kill_timer(skdev); |
| 2255 | |
| 2256 | spin_lock_irqsave(&skdev->lock, flags); |
| 2257 | skd_disable_interrupts(skdev); |
| 2258 | |
| 2259 | /* ensure all ints on device are cleared */ |
| 2260 | /* soft reset the device to unload with a clean slate */ |
| 2261 | SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST); |
| 2262 | SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL); |
| 2263 | |
| 2264 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2265 | |
| 2266 | /* poll every 100ms, 1 second timeout */ |
| 2267 | for (i = 0; i < 10; i++) { |
| 2268 | dev_state = |
| 2269 | SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK; |
| 2270 | if (dev_state == FIT_SR_DRIVE_INIT) |
| 2271 | break; |
| 2272 | set_current_state(TASK_INTERRUPTIBLE); |
| 2273 | schedule_timeout(msecs_to_jiffies(100)); |
| 2274 | } |
| 2275 | |
| 2276 | if (dev_state != FIT_SR_DRIVE_INIT) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2277 | dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__, |
| 2278 | dev_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /* assume spinlock is held */ |
| 2282 | static void skd_restart_device(struct skd_device *skdev) |
| 2283 | { |
| 2284 | u32 state; |
| 2285 | |
| 2286 | /* ack all ghost interrupts */ |
| 2287 | SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST); |
| 2288 | |
| 2289 | state = SKD_READL(skdev, FIT_STATUS); |
| 2290 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2291 | dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2292 | |
| 2293 | state &= FIT_SR_DRIVE_STATE_MASK; |
| 2294 | skdev->drive_state = state; |
| 2295 | skdev->last_mtd = 0; |
| 2296 | |
| 2297 | skdev->state = SKD_DRVR_STATE_RESTARTING; |
| 2298 | skdev->timer_countdown = SKD_RESTARTING_TIMO; |
| 2299 | |
| 2300 | skd_soft_reset(skdev); |
| 2301 | } |
| 2302 | |
| 2303 | /* assume spinlock is held */ |
| 2304 | static int skd_quiesce_dev(struct skd_device *skdev) |
| 2305 | { |
| 2306 | int rc = 0; |
| 2307 | |
| 2308 | switch (skdev->state) { |
| 2309 | case SKD_DRVR_STATE_BUSY: |
| 2310 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2311 | dev_dbg(&skdev->pdev->dev, "stopping queue\n"); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2312 | blk_mq_stop_hw_queues(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2313 | break; |
| 2314 | case SKD_DRVR_STATE_ONLINE: |
| 2315 | case SKD_DRVR_STATE_STOPPING: |
| 2316 | case SKD_DRVR_STATE_SYNCING: |
| 2317 | case SKD_DRVR_STATE_PAUSING: |
| 2318 | case SKD_DRVR_STATE_PAUSED: |
| 2319 | case SKD_DRVR_STATE_STARTING: |
| 2320 | case SKD_DRVR_STATE_RESTARTING: |
| 2321 | case SKD_DRVR_STATE_RESUMING: |
| 2322 | default: |
| 2323 | rc = -EINVAL; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2324 | dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n", |
| 2325 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2326 | } |
| 2327 | return rc; |
| 2328 | } |
| 2329 | |
| 2330 | /* assume spinlock is held */ |
| 2331 | static int skd_unquiesce_dev(struct skd_device *skdev) |
| 2332 | { |
| 2333 | int prev_driver_state = skdev->state; |
| 2334 | |
| 2335 | skd_log_skdev(skdev, "unquiesce"); |
| 2336 | if (skdev->state == SKD_DRVR_STATE_ONLINE) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2337 | dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
| 2340 | if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) { |
| 2341 | /* |
| 2342 | * If there has been an state change to other than |
| 2343 | * ONLINE, we will rely on controller state change |
| 2344 | * to come back online and restart the queue. |
| 2345 | * The BUSY state means that driver is ready to |
| 2346 | * continue normal processing but waiting for controller |
| 2347 | * to become available. |
| 2348 | */ |
| 2349 | skdev->state = SKD_DRVR_STATE_BUSY; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2350 | dev_dbg(&skdev->pdev->dev, "drive BUSY state\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | /* |
| 2355 | * Drive has just come online, driver is either in startup, |
| 2356 | * paused performing a task, or bust waiting for hardware. |
| 2357 | */ |
| 2358 | switch (skdev->state) { |
| 2359 | case SKD_DRVR_STATE_PAUSED: |
| 2360 | case SKD_DRVR_STATE_BUSY: |
| 2361 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 2362 | case SKD_DRVR_STATE_BUSY_ERASE: |
| 2363 | case SKD_DRVR_STATE_STARTING: |
| 2364 | case SKD_DRVR_STATE_RESTARTING: |
| 2365 | case SKD_DRVR_STATE_FAULT: |
| 2366 | case SKD_DRVR_STATE_IDLE: |
| 2367 | case SKD_DRVR_STATE_LOAD: |
| 2368 | skdev->state = SKD_DRVR_STATE_ONLINE; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2369 | dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n", |
| 2370 | skd_skdev_state_to_str(prev_driver_state), |
| 2371 | prev_driver_state, skd_skdev_state_to_str(skdev->state), |
| 2372 | skdev->state); |
| 2373 | dev_dbg(&skdev->pdev->dev, |
| 2374 | "**** device ONLINE...starting block queue\n"); |
| 2375 | dev_dbg(&skdev->pdev->dev, "starting queue\n"); |
| 2376 | dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n"); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2377 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2378 | skdev->gendisk_on = 1; |
| 2379 | wake_up_interruptible(&skdev->waitq); |
| 2380 | break; |
| 2381 | |
| 2382 | case SKD_DRVR_STATE_DISAPPEARED: |
| 2383 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2384 | dev_dbg(&skdev->pdev->dev, |
| 2385 | "**** driver state %d, not implemented\n", |
| 2386 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2387 | return -EBUSY; |
| 2388 | } |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | ***************************************************************************** |
| 2394 | * PCIe MSI/MSI-X INTERRUPT HANDLERS |
| 2395 | ***************************************************************************** |
| 2396 | */ |
| 2397 | |
| 2398 | static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data) |
| 2399 | { |
| 2400 | struct skd_device *skdev = skd_host_data; |
| 2401 | unsigned long flags; |
| 2402 | |
| 2403 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2404 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 2405 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
| 2406 | dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq, |
| 2407 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2408 | SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST); |
| 2409 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2410 | return IRQ_HANDLED; |
| 2411 | } |
| 2412 | |
| 2413 | static irqreturn_t skd_statec_isr(int irq, void *skd_host_data) |
| 2414 | { |
| 2415 | struct skd_device *skdev = skd_host_data; |
| 2416 | unsigned long flags; |
| 2417 | |
| 2418 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2419 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 2420 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2421 | SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST); |
| 2422 | skd_isr_fwstate(skdev); |
| 2423 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2424 | return IRQ_HANDLED; |
| 2425 | } |
| 2426 | |
| 2427 | static irqreturn_t skd_comp_q(int irq, void *skd_host_data) |
| 2428 | { |
| 2429 | struct skd_device *skdev = skd_host_data; |
| 2430 | unsigned long flags; |
| 2431 | int flush_enqueued = 0; |
| 2432 | int deferred; |
| 2433 | |
| 2434 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2435 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 2436 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2437 | SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST); |
| 2438 | deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit, |
| 2439 | &flush_enqueued); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2440 | if (flush_enqueued) |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2441 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2442 | |
| 2443 | if (deferred) |
| 2444 | schedule_work(&skdev->completion_worker); |
| 2445 | else if (!flush_enqueued) |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2446 | schedule_work(&skdev->start_queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2447 | |
| 2448 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2449 | |
| 2450 | return IRQ_HANDLED; |
| 2451 | } |
| 2452 | |
| 2453 | static irqreturn_t skd_msg_isr(int irq, void *skd_host_data) |
| 2454 | { |
| 2455 | struct skd_device *skdev = skd_host_data; |
| 2456 | unsigned long flags; |
| 2457 | |
| 2458 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2459 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 2460 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2461 | SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST); |
| 2462 | skd_isr_msg_from_dev(skdev); |
| 2463 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2464 | return IRQ_HANDLED; |
| 2465 | } |
| 2466 | |
| 2467 | static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data) |
| 2468 | { |
| 2469 | struct skd_device *skdev = skd_host_data; |
| 2470 | unsigned long flags; |
| 2471 | |
| 2472 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2473 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 2474 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2475 | SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST); |
| 2476 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2477 | return IRQ_HANDLED; |
| 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | ***************************************************************************** |
| 2482 | * PCIe MSI/MSI-X SETUP |
| 2483 | ***************************************************************************** |
| 2484 | */ |
| 2485 | |
| 2486 | struct skd_msix_entry { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2487 | char isr_name[30]; |
| 2488 | }; |
| 2489 | |
| 2490 | struct skd_init_msix_entry { |
| 2491 | const char *name; |
| 2492 | irq_handler_t handler; |
| 2493 | }; |
| 2494 | |
| 2495 | #define SKD_MAX_MSIX_COUNT 13 |
| 2496 | #define SKD_MIN_MSIX_COUNT 7 |
| 2497 | #define SKD_BASE_MSIX_IRQ 4 |
| 2498 | |
| 2499 | static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = { |
| 2500 | { "(DMA 0)", skd_reserved_isr }, |
| 2501 | { "(DMA 1)", skd_reserved_isr }, |
| 2502 | { "(DMA 2)", skd_reserved_isr }, |
| 2503 | { "(DMA 3)", skd_reserved_isr }, |
| 2504 | { "(State Change)", skd_statec_isr }, |
| 2505 | { "(COMPL_Q)", skd_comp_q }, |
| 2506 | { "(MSG)", skd_msg_isr }, |
| 2507 | { "(Reserved)", skd_reserved_isr }, |
| 2508 | { "(Reserved)", skd_reserved_isr }, |
| 2509 | { "(Queue Full 0)", skd_qfull_isr }, |
| 2510 | { "(Queue Full 1)", skd_qfull_isr }, |
| 2511 | { "(Queue Full 2)", skd_qfull_isr }, |
| 2512 | { "(Queue Full 3)", skd_qfull_isr }, |
| 2513 | }; |
| 2514 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2515 | static int skd_acquire_msix(struct skd_device *skdev) |
| 2516 | { |
Alexander Gordeev | a9df862 | 2014-02-19 09:58:21 +0100 | [diff] [blame] | 2517 | int i, rc; |
Alexander Gordeev | 4681776 | 2014-02-19 09:58:19 +0100 | [diff] [blame] | 2518 | struct pci_dev *pdev = skdev->pdev; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2519 | |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2520 | rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT, |
| 2521 | PCI_IRQ_MSIX); |
| 2522 | if (rc < 0) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2523 | dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc); |
Arnd Bergmann | 3bc8492 | 2016-11-09 13:55:34 +0100 | [diff] [blame] | 2524 | goto out; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2525 | } |
Alexander Gordeev | 4681776 | 2014-02-19 09:58:19 +0100 | [diff] [blame] | 2526 | |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2527 | skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT, |
| 2528 | sizeof(struct skd_msix_entry), GFP_KERNEL); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2529 | if (!skdev->msix_entries) { |
| 2530 | rc = -ENOMEM; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2531 | dev_err(&skdev->pdev->dev, "msix table allocation error\n"); |
Arnd Bergmann | 3bc8492 | 2016-11-09 13:55:34 +0100 | [diff] [blame] | 2532 | goto out; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2533 | } |
| 2534 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2535 | /* Enable MSI-X vectors for the base queue */ |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2536 | for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) { |
| 2537 | struct skd_msix_entry *qentry = &skdev->msix_entries[i]; |
| 2538 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2539 | snprintf(qentry->isr_name, sizeof(qentry->isr_name), |
| 2540 | "%s%d-msix %s", DRV_NAME, skdev->devno, |
| 2541 | msix_entries[i].name); |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2542 | |
| 2543 | rc = devm_request_irq(&skdev->pdev->dev, |
| 2544 | pci_irq_vector(skdev->pdev, i), |
| 2545 | msix_entries[i].handler, 0, |
| 2546 | qentry->isr_name, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2547 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2548 | dev_err(&skdev->pdev->dev, |
| 2549 | "Unable to register(%d) MSI-X handler %d: %s\n", |
| 2550 | rc, i, qentry->isr_name); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2551 | goto msix_out; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2552 | } |
| 2553 | } |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2554 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2555 | dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n", |
| 2556 | SKD_MAX_MSIX_COUNT); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2557 | return 0; |
| 2558 | |
| 2559 | msix_out: |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2560 | while (--i >= 0) |
| 2561 | devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev); |
Arnd Bergmann | 3bc8492 | 2016-11-09 13:55:34 +0100 | [diff] [blame] | 2562 | out: |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2563 | kfree(skdev->msix_entries); |
| 2564 | skdev->msix_entries = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2565 | return rc; |
| 2566 | } |
| 2567 | |
| 2568 | static int skd_acquire_irq(struct skd_device *skdev) |
| 2569 | { |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2570 | struct pci_dev *pdev = skdev->pdev; |
| 2571 | unsigned int irq_flag = PCI_IRQ_LEGACY; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2572 | int rc; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2573 | |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2574 | if (skd_isr_type == SKD_IRQ_MSIX) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2575 | rc = skd_acquire_msix(skdev); |
| 2576 | if (!rc) |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2577 | return 0; |
| 2578 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2579 | dev_err(&skdev->pdev->dev, |
| 2580 | "failed to enable MSI-X, re-trying with MSI %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2581 | } |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2582 | |
| 2583 | snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME, |
| 2584 | skdev->devno); |
| 2585 | |
| 2586 | if (skd_isr_type != SKD_IRQ_LEGACY) |
| 2587 | irq_flag |= PCI_IRQ_MSI; |
| 2588 | rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag); |
| 2589 | if (rc < 0) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2590 | dev_err(&skdev->pdev->dev, |
| 2591 | "failed to allocate the MSI interrupt %d\n", rc); |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2592 | return rc; |
| 2593 | } |
| 2594 | |
| 2595 | rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr, |
| 2596 | pdev->msi_enabled ? 0 : IRQF_SHARED, |
| 2597 | skdev->isr_name, skdev); |
| 2598 | if (rc) { |
| 2599 | pci_free_irq_vectors(pdev); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2600 | dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n", |
| 2601 | rc); |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2602 | return rc; |
| 2603 | } |
| 2604 | |
| 2605 | return 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | static void skd_release_irq(struct skd_device *skdev) |
| 2609 | { |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2610 | struct pci_dev *pdev = skdev->pdev; |
| 2611 | |
| 2612 | if (skdev->msix_entries) { |
| 2613 | int i; |
| 2614 | |
| 2615 | for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) { |
| 2616 | devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), |
| 2617 | skdev); |
| 2618 | } |
| 2619 | |
| 2620 | kfree(skdev->msix_entries); |
| 2621 | skdev->msix_entries = NULL; |
| 2622 | } else { |
| 2623 | devm_free_irq(&pdev->dev, pdev->irq, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2624 | } |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 2625 | |
| 2626 | pci_free_irq_vectors(pdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | /* |
| 2630 | ***************************************************************************** |
| 2631 | * CONSTRUCT |
| 2632 | ***************************************************************************** |
| 2633 | */ |
| 2634 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2635 | static void *skd_alloc_dma(struct skd_device *skdev, struct kmem_cache *s, |
| 2636 | dma_addr_t *dma_handle, gfp_t gfp, |
| 2637 | enum dma_data_direction dir) |
| 2638 | { |
| 2639 | struct device *dev = &skdev->pdev->dev; |
| 2640 | void *buf; |
| 2641 | |
| 2642 | buf = kmem_cache_alloc(s, gfp); |
| 2643 | if (!buf) |
| 2644 | return NULL; |
| 2645 | *dma_handle = dma_map_single(dev, buf, s->size, dir); |
| 2646 | if (dma_mapping_error(dev, *dma_handle)) { |
| 2647 | kfree(buf); |
| 2648 | buf = NULL; |
| 2649 | } |
| 2650 | return buf; |
| 2651 | } |
| 2652 | |
| 2653 | static void skd_free_dma(struct skd_device *skdev, struct kmem_cache *s, |
| 2654 | void *vaddr, dma_addr_t dma_handle, |
| 2655 | enum dma_data_direction dir) |
| 2656 | { |
| 2657 | if (!vaddr) |
| 2658 | return; |
| 2659 | |
| 2660 | dma_unmap_single(&skdev->pdev->dev, dma_handle, s->size, dir); |
| 2661 | kmem_cache_free(s, vaddr); |
| 2662 | } |
| 2663 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2664 | static int skd_cons_skcomp(struct skd_device *skdev) |
| 2665 | { |
| 2666 | int rc = 0; |
| 2667 | struct fit_completion_entry_v1 *skcomp; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2668 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2669 | dev_dbg(&skdev->pdev->dev, |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 2670 | "comp pci_alloc, total bytes %zd entries %d\n", |
| 2671 | SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2672 | |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 2673 | skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE, |
Joe Perches | a5bbf61 | 2014-08-08 14:24:12 -0700 | [diff] [blame] | 2674 | &skdev->cq_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2675 | |
| 2676 | if (skcomp == NULL) { |
| 2677 | rc = -ENOMEM; |
| 2678 | goto err_out; |
| 2679 | } |
| 2680 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2681 | skdev->skcomp_table = skcomp; |
| 2682 | skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp + |
| 2683 | sizeof(*skcomp) * |
| 2684 | SKD_N_COMPLETION_ENTRY); |
| 2685 | |
| 2686 | err_out: |
| 2687 | return rc; |
| 2688 | } |
| 2689 | |
| 2690 | static int skd_cons_skmsg(struct skd_device *skdev) |
| 2691 | { |
| 2692 | int rc = 0; |
| 2693 | u32 i; |
| 2694 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2695 | dev_dbg(&skdev->pdev->dev, |
Bart Van Assche | 01433d0 | 2017-08-17 13:13:18 -0700 | [diff] [blame] | 2696 | "skmsg_table kcalloc, struct %lu, count %u total %lu\n", |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2697 | sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context, |
| 2698 | sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2699 | |
Bart Van Assche | 01433d0 | 2017-08-17 13:13:18 -0700 | [diff] [blame] | 2700 | skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context, |
| 2701 | sizeof(struct skd_fitmsg_context), |
| 2702 | GFP_KERNEL); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2703 | if (skdev->skmsg_table == NULL) { |
| 2704 | rc = -ENOMEM; |
| 2705 | goto err_out; |
| 2706 | } |
| 2707 | |
| 2708 | for (i = 0; i < skdev->num_fitmsg_context; i++) { |
| 2709 | struct skd_fitmsg_context *skmsg; |
| 2710 | |
| 2711 | skmsg = &skdev->skmsg_table[i]; |
| 2712 | |
| 2713 | skmsg->id = i + SKD_ID_FIT_MSG; |
| 2714 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2715 | skmsg->msg_buf = pci_alloc_consistent(skdev->pdev, |
Bart Van Assche | 6507f43 | 2017-08-17 13:13:06 -0700 | [diff] [blame] | 2716 | SKD_N_FITMSG_BYTES, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2717 | &skmsg->mb_dma_address); |
| 2718 | |
| 2719 | if (skmsg->msg_buf == NULL) { |
| 2720 | rc = -ENOMEM; |
| 2721 | goto err_out; |
| 2722 | } |
| 2723 | |
Bart Van Assche | 6507f43 | 2017-08-17 13:13:06 -0700 | [diff] [blame] | 2724 | WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) & |
| 2725 | (FIT_QCMD_ALIGN - 1), |
| 2726 | "not aligned: msg_buf %p mb_dma_address %#llx\n", |
| 2727 | skmsg->msg_buf, skmsg->mb_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2728 | memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2729 | } |
| 2730 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2731 | err_out: |
| 2732 | return rc; |
| 2733 | } |
| 2734 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2735 | static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev, |
| 2736 | u32 n_sg, |
| 2737 | dma_addr_t *ret_dma_addr) |
| 2738 | { |
| 2739 | struct fit_sg_descriptor *sg_list; |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2740 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2741 | sg_list = skd_alloc_dma(skdev, skdev->sglist_cache, ret_dma_addr, |
| 2742 | GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2743 | |
| 2744 | if (sg_list != NULL) { |
| 2745 | uint64_t dma_address = *ret_dma_addr; |
| 2746 | u32 i; |
| 2747 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2748 | for (i = 0; i < n_sg - 1; i++) { |
| 2749 | uint64_t ndp_off; |
| 2750 | ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor); |
| 2751 | |
| 2752 | sg_list[i].next_desc_ptr = dma_address + ndp_off; |
| 2753 | } |
| 2754 | sg_list[i].next_desc_ptr = 0LL; |
| 2755 | } |
| 2756 | |
| 2757 | return sg_list; |
| 2758 | } |
| 2759 | |
Bart Van Assche | 5d00324 | 2017-08-17 13:13:30 -0700 | [diff] [blame] | 2760 | static void skd_free_sg_list(struct skd_device *skdev, |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2761 | struct fit_sg_descriptor *sg_list, |
Bart Van Assche | 5d00324 | 2017-08-17 13:13:30 -0700 | [diff] [blame] | 2762 | dma_addr_t dma_addr) |
| 2763 | { |
Bart Van Assche | 5d00324 | 2017-08-17 13:13:30 -0700 | [diff] [blame] | 2764 | if (WARN_ON_ONCE(!sg_list)) |
| 2765 | return; |
| 2766 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2767 | skd_free_dma(skdev, skdev->sglist_cache, sg_list, dma_addr, |
| 2768 | DMA_TO_DEVICE); |
Bart Van Assche | 5d00324 | 2017-08-17 13:13:30 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2771 | static int skd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 2772 | unsigned int hctx_idx, unsigned int numa_node) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2773 | { |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2774 | struct skd_device *skdev = set->driver_data; |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 2775 | struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2776 | |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 2777 | skreq->state = SKD_REQ_STATE_IDLE; |
| 2778 | skreq->sg = (void *)(skreq + 1); |
| 2779 | sg_init_table(skreq->sg, skd_sgs_per_request); |
| 2780 | skreq->sksg_list = skd_cons_sg_list(skdev, skd_sgs_per_request, |
| 2781 | &skreq->sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2782 | |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 2783 | return skreq->sksg_list ? 0 : -ENOMEM; |
| 2784 | } |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2785 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2786 | static void skd_exit_request(struct blk_mq_tag_set *set, struct request *rq, |
| 2787 | unsigned int hctx_idx) |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 2788 | { |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2789 | struct skd_device *skdev = set->driver_data; |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 2790 | struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2791 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2792 | skd_free_sg_list(skdev, skreq->sksg_list, skreq->sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2793 | } |
| 2794 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2795 | static int skd_cons_sksb(struct skd_device *skdev) |
| 2796 | { |
| 2797 | int rc = 0; |
| 2798 | struct skd_special_context *skspcl; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2799 | |
| 2800 | skspcl = &skdev->internal_skspcl; |
| 2801 | |
| 2802 | skspcl->req.id = 0 + SKD_ID_INTERNAL; |
| 2803 | skspcl->req.state = SKD_REQ_STATE_IDLE; |
| 2804 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2805 | skspcl->data_buf = skd_alloc_dma(skdev, skdev->databuf_cache, |
| 2806 | &skspcl->db_dma_address, |
| 2807 | GFP_DMA | __GFP_ZERO, |
| 2808 | DMA_BIDIRECTIONAL); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2809 | if (skspcl->data_buf == NULL) { |
| 2810 | rc = -ENOMEM; |
| 2811 | goto err_out; |
| 2812 | } |
| 2813 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2814 | skspcl->msg_buf = skd_alloc_dma(skdev, skdev->msgbuf_cache, |
| 2815 | &skspcl->mb_dma_address, |
| 2816 | GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2817 | if (skspcl->msg_buf == NULL) { |
| 2818 | rc = -ENOMEM; |
| 2819 | goto err_out; |
| 2820 | } |
| 2821 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2822 | skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1, |
| 2823 | &skspcl->req.sksg_dma_address); |
| 2824 | if (skspcl->req.sksg_list == NULL) { |
| 2825 | rc = -ENOMEM; |
| 2826 | goto err_out; |
| 2827 | } |
| 2828 | |
| 2829 | if (!skd_format_internal_skspcl(skdev)) { |
| 2830 | rc = -EINVAL; |
| 2831 | goto err_out; |
| 2832 | } |
| 2833 | |
| 2834 | err_out: |
| 2835 | return rc; |
| 2836 | } |
| 2837 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2838 | static const struct blk_mq_ops skd_mq_ops = { |
| 2839 | .queue_rq = skd_mq_queue_rq, |
| 2840 | .init_request = skd_init_request, |
| 2841 | .exit_request = skd_exit_request, |
| 2842 | }; |
| 2843 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2844 | static int skd_cons_disk(struct skd_device *skdev) |
| 2845 | { |
| 2846 | int rc = 0; |
| 2847 | struct gendisk *disk; |
| 2848 | struct request_queue *q; |
| 2849 | unsigned long flags; |
| 2850 | |
| 2851 | disk = alloc_disk(SKD_MINORS_PER_DEVICE); |
| 2852 | if (!disk) { |
| 2853 | rc = -ENOMEM; |
| 2854 | goto err_out; |
| 2855 | } |
| 2856 | |
| 2857 | skdev->disk = disk; |
| 2858 | sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno); |
| 2859 | |
| 2860 | disk->major = skdev->major; |
| 2861 | disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE; |
| 2862 | disk->fops = &skd_blockdev_ops; |
| 2863 | disk->private_data = skdev; |
| 2864 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2865 | q = NULL; |
| 2866 | memset(&skdev->tag_set, 0, sizeof(skdev->tag_set)); |
| 2867 | skdev->tag_set.ops = &skd_mq_ops; |
| 2868 | skdev->tag_set.nr_hw_queues = 1; |
| 2869 | skdev->tag_set.queue_depth = skd_max_queue_depth; |
| 2870 | skdev->tag_set.cmd_size = sizeof(struct skd_request_context) + |
| 2871 | skdev->sgs_per_request * sizeof(struct scatterlist); |
| 2872 | skdev->tag_set.numa_node = NUMA_NO_NODE; |
| 2873 | skdev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | |
| 2874 | BLK_MQ_F_SG_MERGE | |
| 2875 | BLK_ALLOC_POLICY_TO_MQ_FLAG(BLK_TAG_ALLOC_FIFO); |
| 2876 | skdev->tag_set.driver_data = skdev; |
| 2877 | if (blk_mq_alloc_tag_set(&skdev->tag_set) >= 0) { |
| 2878 | q = blk_mq_init_queue(&skdev->tag_set); |
| 2879 | if (!q) |
| 2880 | blk_mq_free_tag_set(&skdev->tag_set); |
| 2881 | } |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2882 | if (!q) { |
| 2883 | rc = -ENOMEM; |
| 2884 | goto err_out; |
| 2885 | } |
Christoph Hellwig | 8fc4504 | 2017-06-19 09:26:26 +0200 | [diff] [blame] | 2886 | blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 2887 | q->queuedata = skdev; |
Bart Van Assche | f18c17c | 2017-08-17 13:13:26 -0700 | [diff] [blame] | 2888 | q->nr_requests = skd_max_queue_depth / 2; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2889 | |
| 2890 | skdev->queue = q; |
| 2891 | disk->queue = q; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2892 | |
Jens Axboe | 6975f73 | 2016-03-30 10:11:42 -0600 | [diff] [blame] | 2893 | blk_queue_write_cache(q, true, true); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2894 | blk_queue_max_segments(q, skdev->sgs_per_request); |
| 2895 | blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS); |
| 2896 | |
Bart Van Assche | a5c5b39 | 2017-08-17 13:12:53 -0700 | [diff] [blame] | 2897 | /* set optimal I/O size to 8KB */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2898 | blk_queue_io_opt(q, 8192); |
| 2899 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2900 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 2901 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2902 | |
Bart Van Assche | a74d5b7 | 2017-08-17 13:13:33 -0700 | [diff] [blame] | 2903 | blk_queue_rq_timeout(q, 8 * HZ); |
| 2904 | blk_queue_rq_timed_out(q, skd_timed_out); |
| 2905 | blk_queue_softirq_done(q, skd_softirq_done); |
| 2906 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2907 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2908 | dev_dbg(&skdev->pdev->dev, "stopping queue\n"); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2909 | blk_mq_stop_hw_queues(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2910 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2911 | |
| 2912 | err_out: |
| 2913 | return rc; |
| 2914 | } |
| 2915 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2916 | #define SKD_N_DEV_TABLE 16u |
| 2917 | static u32 skd_next_devno; |
| 2918 | |
| 2919 | static struct skd_device *skd_construct(struct pci_dev *pdev) |
| 2920 | { |
| 2921 | struct skd_device *skdev; |
| 2922 | int blk_major = skd_major; |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2923 | size_t size; |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2924 | int rc; |
| 2925 | |
| 2926 | skdev = kzalloc(sizeof(*skdev), GFP_KERNEL); |
| 2927 | |
| 2928 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2929 | dev_err(&pdev->dev, "memory alloc failure\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2930 | return NULL; |
| 2931 | } |
| 2932 | |
| 2933 | skdev->state = SKD_DRVR_STATE_LOAD; |
| 2934 | skdev->pdev = pdev; |
| 2935 | skdev->devno = skd_next_devno++; |
| 2936 | skdev->major = blk_major; |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2937 | skdev->dev_max_queue_depth = 0; |
| 2938 | |
| 2939 | skdev->num_req_context = skd_max_queue_depth; |
| 2940 | skdev->num_fitmsg_context = skd_max_queue_depth; |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2941 | skdev->cur_max_queue_depth = 1; |
| 2942 | skdev->queue_low_water_mark = 1; |
| 2943 | skdev->proto_ver = 99; |
| 2944 | skdev->sgs_per_request = skd_sgs_per_request; |
| 2945 | skdev->dbg_level = skd_dbg_level; |
| 2946 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2947 | spin_lock_init(&skdev->lock); |
| 2948 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 2949 | INIT_WORK(&skdev->start_queue, skd_start_queue); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2950 | INIT_WORK(&skdev->completion_worker, skd_completion_worker); |
| 2951 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 2952 | size = max(SKD_N_FITMSG_BYTES, SKD_N_SPECIAL_FITMSG_BYTES); |
| 2953 | skdev->msgbuf_cache = kmem_cache_create("skd-msgbuf", size, 0, |
| 2954 | SLAB_HWCACHE_ALIGN, NULL); |
| 2955 | if (!skdev->msgbuf_cache) |
| 2956 | goto err_out; |
| 2957 | WARN_ONCE(kmem_cache_size(skdev->msgbuf_cache) < size, |
| 2958 | "skd-msgbuf: %d < %zd\n", |
| 2959 | kmem_cache_size(skdev->msgbuf_cache), size); |
| 2960 | size = skd_sgs_per_request * sizeof(struct fit_sg_descriptor); |
| 2961 | skdev->sglist_cache = kmem_cache_create("skd-sglist", size, 0, |
| 2962 | SLAB_HWCACHE_ALIGN, NULL); |
| 2963 | if (!skdev->sglist_cache) |
| 2964 | goto err_out; |
| 2965 | WARN_ONCE(kmem_cache_size(skdev->sglist_cache) < size, |
| 2966 | "skd-sglist: %d < %zd\n", |
| 2967 | kmem_cache_size(skdev->sglist_cache), size); |
| 2968 | size = SKD_N_INTERNAL_BYTES; |
| 2969 | skdev->databuf_cache = kmem_cache_create("skd-databuf", size, 0, |
| 2970 | SLAB_HWCACHE_ALIGN, NULL); |
| 2971 | if (!skdev->databuf_cache) |
| 2972 | goto err_out; |
| 2973 | WARN_ONCE(kmem_cache_size(skdev->databuf_cache) < size, |
| 2974 | "skd-databuf: %d < %zd\n", |
| 2975 | kmem_cache_size(skdev->databuf_cache), size); |
| 2976 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2977 | dev_dbg(&skdev->pdev->dev, "skcomp\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2978 | rc = skd_cons_skcomp(skdev); |
| 2979 | if (rc < 0) |
| 2980 | goto err_out; |
| 2981 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2982 | dev_dbg(&skdev->pdev->dev, "skmsg\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2983 | rc = skd_cons_skmsg(skdev); |
| 2984 | if (rc < 0) |
| 2985 | goto err_out; |
| 2986 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2987 | dev_dbg(&skdev->pdev->dev, "sksb\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2988 | rc = skd_cons_sksb(skdev); |
| 2989 | if (rc < 0) |
| 2990 | goto err_out; |
| 2991 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2992 | dev_dbg(&skdev->pdev->dev, "disk\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2993 | rc = skd_cons_disk(skdev); |
| 2994 | if (rc < 0) |
| 2995 | goto err_out; |
| 2996 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2997 | dev_dbg(&skdev->pdev->dev, "VICTORY\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 2998 | return skdev; |
| 2999 | |
| 3000 | err_out: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3001 | dev_dbg(&skdev->pdev->dev, "construct failed\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3002 | skd_destruct(skdev); |
| 3003 | return NULL; |
| 3004 | } |
| 3005 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3006 | /* |
| 3007 | ***************************************************************************** |
| 3008 | * DESTRUCT (FREE) |
| 3009 | ***************************************************************************** |
| 3010 | */ |
| 3011 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3012 | static void skd_free_skcomp(struct skd_device *skdev) |
| 3013 | { |
Bart Van Assche | 7f13bda | 2017-08-17 13:13:03 -0700 | [diff] [blame] | 3014 | if (skdev->skcomp_table) |
| 3015 | pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3016 | skdev->skcomp_table, skdev->cq_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3017 | |
| 3018 | skdev->skcomp_table = NULL; |
| 3019 | skdev->cq_dma_address = 0; |
| 3020 | } |
| 3021 | |
| 3022 | static void skd_free_skmsg(struct skd_device *skdev) |
| 3023 | { |
| 3024 | u32 i; |
| 3025 | |
| 3026 | if (skdev->skmsg_table == NULL) |
| 3027 | return; |
| 3028 | |
| 3029 | for (i = 0; i < skdev->num_fitmsg_context; i++) { |
| 3030 | struct skd_fitmsg_context *skmsg; |
| 3031 | |
| 3032 | skmsg = &skdev->skmsg_table[i]; |
| 3033 | |
| 3034 | if (skmsg->msg_buf != NULL) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3035 | pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES, |
| 3036 | skmsg->msg_buf, |
| 3037 | skmsg->mb_dma_address); |
| 3038 | } |
| 3039 | skmsg->msg_buf = NULL; |
| 3040 | skmsg->mb_dma_address = 0; |
| 3041 | } |
| 3042 | |
| 3043 | kfree(skdev->skmsg_table); |
| 3044 | skdev->skmsg_table = NULL; |
| 3045 | } |
| 3046 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3047 | static void skd_free_sksb(struct skd_device *skdev) |
| 3048 | { |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 3049 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3050 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 3051 | skd_free_dma(skdev, skdev->databuf_cache, skspcl->data_buf, |
| 3052 | skspcl->db_dma_address, DMA_BIDIRECTIONAL); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3053 | |
| 3054 | skspcl->data_buf = NULL; |
| 3055 | skspcl->db_dma_address = 0; |
| 3056 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 3057 | skd_free_dma(skdev, skdev->msgbuf_cache, skspcl->msg_buf, |
| 3058 | skspcl->mb_dma_address, DMA_TO_DEVICE); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3059 | |
| 3060 | skspcl->msg_buf = NULL; |
| 3061 | skspcl->mb_dma_address = 0; |
| 3062 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 3063 | skd_free_sg_list(skdev, skspcl->req.sksg_list, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3064 | skspcl->req.sksg_dma_address); |
| 3065 | |
| 3066 | skspcl->req.sksg_list = NULL; |
| 3067 | skspcl->req.sksg_dma_address = 0; |
| 3068 | } |
| 3069 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3070 | static void skd_free_disk(struct skd_device *skdev) |
| 3071 | { |
| 3072 | struct gendisk *disk = skdev->disk; |
| 3073 | |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 3074 | if (disk && (disk->flags & GENHD_FL_UP)) |
| 3075 | del_gendisk(disk); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3076 | |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 3077 | if (skdev->queue) { |
| 3078 | blk_cleanup_queue(skdev->queue); |
| 3079 | skdev->queue = NULL; |
| 3080 | disk->queue = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3081 | } |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 3082 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 3083 | if (skdev->tag_set.tags) |
| 3084 | blk_mq_free_tag_set(&skdev->tag_set); |
| 3085 | |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 3086 | put_disk(disk); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3087 | skdev->disk = NULL; |
| 3088 | } |
| 3089 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3090 | static void skd_destruct(struct skd_device *skdev) |
| 3091 | { |
| 3092 | if (skdev == NULL) |
| 3093 | return; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3094 | |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 3095 | cancel_work_sync(&skdev->start_queue); |
| 3096 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3097 | dev_dbg(&skdev->pdev->dev, "disk\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3098 | skd_free_disk(skdev); |
| 3099 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3100 | dev_dbg(&skdev->pdev->dev, "sksb\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3101 | skd_free_sksb(skdev); |
| 3102 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3103 | dev_dbg(&skdev->pdev->dev, "skmsg\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3104 | skd_free_skmsg(skdev); |
| 3105 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3106 | dev_dbg(&skdev->pdev->dev, "skcomp\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3107 | skd_free_skcomp(skdev); |
| 3108 | |
Bart Van Assche | a3db102 | 2017-08-17 13:13:35 -0700 | [diff] [blame] | 3109 | kmem_cache_destroy(skdev->databuf_cache); |
| 3110 | kmem_cache_destroy(skdev->sglist_cache); |
| 3111 | kmem_cache_destroy(skdev->msgbuf_cache); |
| 3112 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3113 | dev_dbg(&skdev->pdev->dev, "skdev\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3114 | kfree(skdev); |
| 3115 | } |
| 3116 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3117 | /* |
| 3118 | ***************************************************************************** |
| 3119 | * BLOCK DEVICE (BDEV) GLUE |
| 3120 | ***************************************************************************** |
| 3121 | */ |
| 3122 | |
| 3123 | static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 3124 | { |
| 3125 | struct skd_device *skdev; |
| 3126 | u64 capacity; |
| 3127 | |
| 3128 | skdev = bdev->bd_disk->private_data; |
| 3129 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3130 | dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n", |
| 3131 | bdev->bd_disk->disk_name, current->comm); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3132 | |
| 3133 | if (skdev->read_cap_is_valid) { |
| 3134 | capacity = get_capacity(skdev->disk); |
| 3135 | geo->heads = 64; |
| 3136 | geo->sectors = 255; |
| 3137 | geo->cylinders = (capacity) / (255 * 64); |
| 3138 | |
| 3139 | return 0; |
| 3140 | } |
| 3141 | return -EIO; |
| 3142 | } |
| 3143 | |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 3144 | static int skd_bdev_attach(struct device *parent, struct skd_device *skdev) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3145 | { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3146 | dev_dbg(&skdev->pdev->dev, "add_disk\n"); |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 3147 | device_add_disk(parent, skdev->disk); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3148 | return 0; |
| 3149 | } |
| 3150 | |
| 3151 | static const struct block_device_operations skd_blockdev_ops = { |
| 3152 | .owner = THIS_MODULE, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3153 | .getgeo = skd_bdev_getgeo, |
| 3154 | }; |
| 3155 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3156 | /* |
| 3157 | ***************************************************************************** |
| 3158 | * PCIe DRIVER GLUE |
| 3159 | ***************************************************************************** |
| 3160 | */ |
| 3161 | |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 3162 | static const struct pci_device_id skd_pci_tbl[] = { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3163 | { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120, |
| 3164 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, }, |
| 3165 | { 0 } /* terminate list */ |
| 3166 | }; |
| 3167 | |
| 3168 | MODULE_DEVICE_TABLE(pci, skd_pci_tbl); |
| 3169 | |
| 3170 | static char *skd_pci_info(struct skd_device *skdev, char *str) |
| 3171 | { |
| 3172 | int pcie_reg; |
| 3173 | |
| 3174 | strcpy(str, "PCIe ("); |
| 3175 | pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP); |
| 3176 | |
| 3177 | if (pcie_reg) { |
| 3178 | |
| 3179 | char lwstr[6]; |
| 3180 | uint16_t pcie_lstat, lspeed, lwidth; |
| 3181 | |
| 3182 | pcie_reg += 0x12; |
| 3183 | pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat); |
| 3184 | lspeed = pcie_lstat & (0xF); |
| 3185 | lwidth = (pcie_lstat & 0x3F0) >> 4; |
| 3186 | |
| 3187 | if (lspeed == 1) |
| 3188 | strcat(str, "2.5GT/s "); |
| 3189 | else if (lspeed == 2) |
| 3190 | strcat(str, "5.0GT/s "); |
| 3191 | else |
| 3192 | strcat(str, "<unknown> "); |
| 3193 | snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth); |
| 3194 | strcat(str, lwstr); |
| 3195 | } |
| 3196 | return str; |
| 3197 | } |
| 3198 | |
| 3199 | static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 3200 | { |
| 3201 | int i; |
| 3202 | int rc = 0; |
| 3203 | char pci_str[32]; |
| 3204 | struct skd_device *skdev; |
| 3205 | |
Bart Van Assche | bb9f7dd | 2017-08-17 13:13:38 -0700 | [diff] [blame] | 3206 | dev_dbg(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor, |
| 3207 | pdev->device); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3208 | |
| 3209 | rc = pci_enable_device(pdev); |
| 3210 | if (rc) |
| 3211 | return rc; |
| 3212 | rc = pci_request_regions(pdev, DRV_NAME); |
| 3213 | if (rc) |
| 3214 | goto err_out; |
| 3215 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 3216 | if (!rc) { |
| 3217 | if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3218 | dev_err(&pdev->dev, "consistent DMA mask error %d\n", |
| 3219 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3220 | } |
| 3221 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3222 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3223 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3224 | dev_err(&pdev->dev, "DMA mask error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3225 | goto err_out_regions; |
| 3226 | } |
| 3227 | } |
| 3228 | |
Bartlomiej Zolnierkiewicz | b8df664 | 2013-11-05 12:37:02 +0100 | [diff] [blame] | 3229 | if (!skd_major) { |
| 3230 | rc = register_blkdev(0, DRV_NAME); |
| 3231 | if (rc < 0) |
| 3232 | goto err_out_regions; |
| 3233 | BUG_ON(!rc); |
| 3234 | skd_major = rc; |
| 3235 | } |
| 3236 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3237 | skdev = skd_construct(pdev); |
Wei Yongjun | 1762b57 | 2013-10-30 13:23:53 +0800 | [diff] [blame] | 3238 | if (skdev == NULL) { |
| 3239 | rc = -ENOMEM; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3240 | goto err_out_regions; |
Wei Yongjun | 1762b57 | 2013-10-30 13:23:53 +0800 | [diff] [blame] | 3241 | } |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3242 | |
| 3243 | skd_pci_info(skdev, pci_str); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3244 | dev_info(&pdev->dev, "%s 64bit\n", pci_str); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3245 | |
| 3246 | pci_set_master(pdev); |
| 3247 | rc = pci_enable_pcie_error_reporting(pdev); |
| 3248 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3249 | dev_err(&pdev->dev, |
| 3250 | "bad enable of PCIe error reporting rc=%d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3251 | skdev->pcie_error_reporting_is_enabled = 0; |
| 3252 | } else |
| 3253 | skdev->pcie_error_reporting_is_enabled = 1; |
| 3254 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3255 | pci_set_drvdata(pdev, skdev); |
Bartlomiej Zolnierkiewicz | ebedd16 | 2013-11-05 12:37:05 +0100 | [diff] [blame] | 3256 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3257 | for (i = 0; i < SKD_MAX_BARS; i++) { |
| 3258 | skdev->mem_phys[i] = pci_resource_start(pdev, i); |
| 3259 | skdev->mem_size[i] = (u32)pci_resource_len(pdev, i); |
| 3260 | skdev->mem_map[i] = ioremap(skdev->mem_phys[i], |
| 3261 | skdev->mem_size[i]); |
| 3262 | if (!skdev->mem_map[i]) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3263 | dev_err(&pdev->dev, |
| 3264 | "Unable to map adapter memory!\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3265 | rc = -ENODEV; |
| 3266 | goto err_out_iounmap; |
| 3267 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3268 | dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n", |
| 3269 | skdev->mem_map[i], (uint64_t)skdev->mem_phys[i], |
| 3270 | skdev->mem_size[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | rc = skd_acquire_irq(skdev); |
| 3274 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3275 | dev_err(&pdev->dev, "interrupt resource error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3276 | goto err_out_iounmap; |
| 3277 | } |
| 3278 | |
| 3279 | rc = skd_start_timer(skdev); |
| 3280 | if (rc) |
| 3281 | goto err_out_timer; |
| 3282 | |
| 3283 | init_waitqueue_head(&skdev->waitq); |
| 3284 | |
| 3285 | skd_start_device(skdev); |
| 3286 | |
| 3287 | rc = wait_event_interruptible_timeout(skdev->waitq, |
| 3288 | (skdev->gendisk_on), |
| 3289 | (SKD_START_WAIT_SECONDS * HZ)); |
| 3290 | if (skdev->gendisk_on > 0) { |
| 3291 | /* device came on-line after reset */ |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 3292 | skd_bdev_attach(&pdev->dev, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3293 | rc = 0; |
| 3294 | } else { |
| 3295 | /* we timed out, something is wrong with the device, |
| 3296 | don't add the disk structure */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3297 | dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n", |
| 3298 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3299 | /* in case of no error; we timeout with ENXIO */ |
| 3300 | if (!rc) |
| 3301 | rc = -ENXIO; |
| 3302 | goto err_out_timer; |
| 3303 | } |
| 3304 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3305 | return rc; |
| 3306 | |
| 3307 | err_out_timer: |
| 3308 | skd_stop_device(skdev); |
| 3309 | skd_release_irq(skdev); |
| 3310 | |
| 3311 | err_out_iounmap: |
| 3312 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 3313 | if (skdev->mem_map[i]) |
| 3314 | iounmap(skdev->mem_map[i]); |
| 3315 | |
| 3316 | if (skdev->pcie_error_reporting_is_enabled) |
| 3317 | pci_disable_pcie_error_reporting(pdev); |
| 3318 | |
| 3319 | skd_destruct(skdev); |
| 3320 | |
| 3321 | err_out_regions: |
| 3322 | pci_release_regions(pdev); |
| 3323 | |
| 3324 | err_out: |
| 3325 | pci_disable_device(pdev); |
| 3326 | pci_set_drvdata(pdev, NULL); |
| 3327 | return rc; |
| 3328 | } |
| 3329 | |
| 3330 | static void skd_pci_remove(struct pci_dev *pdev) |
| 3331 | { |
| 3332 | int i; |
| 3333 | struct skd_device *skdev; |
| 3334 | |
| 3335 | skdev = pci_get_drvdata(pdev); |
| 3336 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3337 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3338 | return; |
| 3339 | } |
| 3340 | skd_stop_device(skdev); |
| 3341 | skd_release_irq(skdev); |
| 3342 | |
| 3343 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 3344 | if (skdev->mem_map[i]) |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 3345 | iounmap(skdev->mem_map[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3346 | |
| 3347 | if (skdev->pcie_error_reporting_is_enabled) |
| 3348 | pci_disable_pcie_error_reporting(pdev); |
| 3349 | |
| 3350 | skd_destruct(skdev); |
| 3351 | |
| 3352 | pci_release_regions(pdev); |
| 3353 | pci_disable_device(pdev); |
| 3354 | pci_set_drvdata(pdev, NULL); |
| 3355 | |
| 3356 | return; |
| 3357 | } |
| 3358 | |
| 3359 | static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state) |
| 3360 | { |
| 3361 | int i; |
| 3362 | struct skd_device *skdev; |
| 3363 | |
| 3364 | skdev = pci_get_drvdata(pdev); |
| 3365 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3366 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3367 | return -EIO; |
| 3368 | } |
| 3369 | |
| 3370 | skd_stop_device(skdev); |
| 3371 | |
| 3372 | skd_release_irq(skdev); |
| 3373 | |
| 3374 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 3375 | if (skdev->mem_map[i]) |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 3376 | iounmap(skdev->mem_map[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3377 | |
| 3378 | if (skdev->pcie_error_reporting_is_enabled) |
| 3379 | pci_disable_pcie_error_reporting(pdev); |
| 3380 | |
| 3381 | pci_release_regions(pdev); |
| 3382 | pci_save_state(pdev); |
| 3383 | pci_disable_device(pdev); |
| 3384 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); |
| 3385 | return 0; |
| 3386 | } |
| 3387 | |
| 3388 | static int skd_pci_resume(struct pci_dev *pdev) |
| 3389 | { |
| 3390 | int i; |
| 3391 | int rc = 0; |
| 3392 | struct skd_device *skdev; |
| 3393 | |
| 3394 | skdev = pci_get_drvdata(pdev); |
| 3395 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3396 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3397 | return -1; |
| 3398 | } |
| 3399 | |
| 3400 | pci_set_power_state(pdev, PCI_D0); |
| 3401 | pci_enable_wake(pdev, PCI_D0, 0); |
| 3402 | pci_restore_state(pdev); |
| 3403 | |
| 3404 | rc = pci_enable_device(pdev); |
| 3405 | if (rc) |
| 3406 | return rc; |
| 3407 | rc = pci_request_regions(pdev, DRV_NAME); |
| 3408 | if (rc) |
| 3409 | goto err_out; |
| 3410 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 3411 | if (!rc) { |
| 3412 | if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) { |
| 3413 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3414 | dev_err(&pdev->dev, "consistent DMA mask error %d\n", |
| 3415 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3416 | } |
| 3417 | } else { |
| 3418 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 3419 | if (rc) { |
| 3420 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3421 | dev_err(&pdev->dev, "DMA mask error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3422 | goto err_out_regions; |
| 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | pci_set_master(pdev); |
| 3427 | rc = pci_enable_pcie_error_reporting(pdev); |
| 3428 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3429 | dev_err(&pdev->dev, |
| 3430 | "bad enable of PCIe error reporting rc=%d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3431 | skdev->pcie_error_reporting_is_enabled = 0; |
| 3432 | } else |
| 3433 | skdev->pcie_error_reporting_is_enabled = 1; |
| 3434 | |
| 3435 | for (i = 0; i < SKD_MAX_BARS; i++) { |
| 3436 | |
| 3437 | skdev->mem_phys[i] = pci_resource_start(pdev, i); |
| 3438 | skdev->mem_size[i] = (u32)pci_resource_len(pdev, i); |
| 3439 | skdev->mem_map[i] = ioremap(skdev->mem_phys[i], |
| 3440 | skdev->mem_size[i]); |
| 3441 | if (!skdev->mem_map[i]) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3442 | dev_err(&pdev->dev, "Unable to map adapter memory!\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3443 | rc = -ENODEV; |
| 3444 | goto err_out_iounmap; |
| 3445 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3446 | dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n", |
| 3447 | skdev->mem_map[i], (uint64_t)skdev->mem_phys[i], |
| 3448 | skdev->mem_size[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3449 | } |
| 3450 | rc = skd_acquire_irq(skdev); |
| 3451 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3452 | dev_err(&pdev->dev, "interrupt resource error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3453 | goto err_out_iounmap; |
| 3454 | } |
| 3455 | |
| 3456 | rc = skd_start_timer(skdev); |
| 3457 | if (rc) |
| 3458 | goto err_out_timer; |
| 3459 | |
| 3460 | init_waitqueue_head(&skdev->waitq); |
| 3461 | |
| 3462 | skd_start_device(skdev); |
| 3463 | |
| 3464 | return rc; |
| 3465 | |
| 3466 | err_out_timer: |
| 3467 | skd_stop_device(skdev); |
| 3468 | skd_release_irq(skdev); |
| 3469 | |
| 3470 | err_out_iounmap: |
| 3471 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 3472 | if (skdev->mem_map[i]) |
| 3473 | iounmap(skdev->mem_map[i]); |
| 3474 | |
| 3475 | if (skdev->pcie_error_reporting_is_enabled) |
| 3476 | pci_disable_pcie_error_reporting(pdev); |
| 3477 | |
| 3478 | err_out_regions: |
| 3479 | pci_release_regions(pdev); |
| 3480 | |
| 3481 | err_out: |
| 3482 | pci_disable_device(pdev); |
| 3483 | return rc; |
| 3484 | } |
| 3485 | |
| 3486 | static void skd_pci_shutdown(struct pci_dev *pdev) |
| 3487 | { |
| 3488 | struct skd_device *skdev; |
| 3489 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3490 | dev_err(&pdev->dev, "%s called\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3491 | |
| 3492 | skdev = pci_get_drvdata(pdev); |
| 3493 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3494 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3495 | return; |
| 3496 | } |
| 3497 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3498 | dev_err(&pdev->dev, "calling stop\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3499 | skd_stop_device(skdev); |
| 3500 | } |
| 3501 | |
| 3502 | static struct pci_driver skd_driver = { |
| 3503 | .name = DRV_NAME, |
| 3504 | .id_table = skd_pci_tbl, |
| 3505 | .probe = skd_pci_probe, |
| 3506 | .remove = skd_pci_remove, |
| 3507 | .suspend = skd_pci_suspend, |
| 3508 | .resume = skd_pci_resume, |
| 3509 | .shutdown = skd_pci_shutdown, |
| 3510 | }; |
| 3511 | |
| 3512 | /* |
| 3513 | ***************************************************************************** |
| 3514 | * LOGGING SUPPORT |
| 3515 | ***************************************************************************** |
| 3516 | */ |
| 3517 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3518 | const char *skd_drive_state_to_str(int state) |
| 3519 | { |
| 3520 | switch (state) { |
| 3521 | case FIT_SR_DRIVE_OFFLINE: |
| 3522 | return "OFFLINE"; |
| 3523 | case FIT_SR_DRIVE_INIT: |
| 3524 | return "INIT"; |
| 3525 | case FIT_SR_DRIVE_ONLINE: |
| 3526 | return "ONLINE"; |
| 3527 | case FIT_SR_DRIVE_BUSY: |
| 3528 | return "BUSY"; |
| 3529 | case FIT_SR_DRIVE_FAULT: |
| 3530 | return "FAULT"; |
| 3531 | case FIT_SR_DRIVE_DEGRADED: |
| 3532 | return "DEGRADED"; |
| 3533 | case FIT_SR_PCIE_LINK_DOWN: |
| 3534 | return "INK_DOWN"; |
| 3535 | case FIT_SR_DRIVE_SOFT_RESET: |
| 3536 | return "SOFT_RESET"; |
| 3537 | case FIT_SR_DRIVE_NEED_FW_DOWNLOAD: |
| 3538 | return "NEED_FW"; |
| 3539 | case FIT_SR_DRIVE_INIT_FAULT: |
| 3540 | return "INIT_FAULT"; |
| 3541 | case FIT_SR_DRIVE_BUSY_SANITIZE: |
| 3542 | return "BUSY_SANITIZE"; |
| 3543 | case FIT_SR_DRIVE_BUSY_ERASE: |
| 3544 | return "BUSY_ERASE"; |
| 3545 | case FIT_SR_DRIVE_FW_BOOTING: |
| 3546 | return "FW_BOOTING"; |
| 3547 | default: |
| 3548 | return "???"; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | const char *skd_skdev_state_to_str(enum skd_drvr_state state) |
| 3553 | { |
| 3554 | switch (state) { |
| 3555 | case SKD_DRVR_STATE_LOAD: |
| 3556 | return "LOAD"; |
| 3557 | case SKD_DRVR_STATE_IDLE: |
| 3558 | return "IDLE"; |
| 3559 | case SKD_DRVR_STATE_BUSY: |
| 3560 | return "BUSY"; |
| 3561 | case SKD_DRVR_STATE_STARTING: |
| 3562 | return "STARTING"; |
| 3563 | case SKD_DRVR_STATE_ONLINE: |
| 3564 | return "ONLINE"; |
| 3565 | case SKD_DRVR_STATE_PAUSING: |
| 3566 | return "PAUSING"; |
| 3567 | case SKD_DRVR_STATE_PAUSED: |
| 3568 | return "PAUSED"; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3569 | case SKD_DRVR_STATE_RESTARTING: |
| 3570 | return "RESTARTING"; |
| 3571 | case SKD_DRVR_STATE_RESUMING: |
| 3572 | return "RESUMING"; |
| 3573 | case SKD_DRVR_STATE_STOPPING: |
| 3574 | return "STOPPING"; |
| 3575 | case SKD_DRVR_STATE_SYNCING: |
| 3576 | return "SYNCING"; |
| 3577 | case SKD_DRVR_STATE_FAULT: |
| 3578 | return "FAULT"; |
| 3579 | case SKD_DRVR_STATE_DISAPPEARED: |
| 3580 | return "DISAPPEARED"; |
| 3581 | case SKD_DRVR_STATE_BUSY_ERASE: |
| 3582 | return "BUSY_ERASE"; |
| 3583 | case SKD_DRVR_STATE_BUSY_SANITIZE: |
| 3584 | return "BUSY_SANITIZE"; |
| 3585 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 3586 | return "BUSY_IMMINENT"; |
| 3587 | case SKD_DRVR_STATE_WAIT_BOOT: |
| 3588 | return "WAIT_BOOT"; |
| 3589 | |
| 3590 | default: |
| 3591 | return "???"; |
| 3592 | } |
| 3593 | } |
| 3594 | |
Rashika Kheria | a26ba7f | 2013-12-19 15:02:22 +0530 | [diff] [blame] | 3595 | static const char *skd_skreq_state_to_str(enum skd_req_state state) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3596 | { |
| 3597 | switch (state) { |
| 3598 | case SKD_REQ_STATE_IDLE: |
| 3599 | return "IDLE"; |
| 3600 | case SKD_REQ_STATE_SETUP: |
| 3601 | return "SETUP"; |
| 3602 | case SKD_REQ_STATE_BUSY: |
| 3603 | return "BUSY"; |
| 3604 | case SKD_REQ_STATE_COMPLETED: |
| 3605 | return "COMPLETED"; |
| 3606 | case SKD_REQ_STATE_TIMEOUT: |
| 3607 | return "TIMEOUT"; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3608 | default: |
| 3609 | return "???"; |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | static void skd_log_skdev(struct skd_device *skdev, const char *event) |
| 3614 | { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3615 | dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event); |
| 3616 | dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n", |
| 3617 | skd_drive_state_to_str(skdev->drive_state), skdev->drive_state, |
| 3618 | skd_skdev_state_to_str(skdev->state), skdev->state); |
| 3619 | dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n", |
Bart Van Assche | d4d0f5f | 2017-08-17 13:13:34 -0700 | [diff] [blame] | 3620 | skd_in_flight(skdev), skdev->cur_max_queue_depth, |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3621 | skdev->dev_max_queue_depth, skdev->queue_low_water_mark); |
Bart Van Assche | a74d5b7 | 2017-08-17 13:13:33 -0700 | [diff] [blame] | 3622 | dev_dbg(&skdev->pdev->dev, " cycle=%d cycle_ix=%d\n", |
| 3623 | skdev->skcomp_cycle, skdev->skcomp_ix); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3624 | } |
| 3625 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3626 | static void skd_log_skreq(struct skd_device *skdev, |
| 3627 | struct skd_request_context *skreq, const char *event) |
| 3628 | { |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 3629 | struct request *req = blk_mq_rq_from_pdu(skreq); |
| 3630 | u32 lba = blk_rq_pos(req); |
| 3631 | u32 count = blk_rq_sectors(req); |
| 3632 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3633 | dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event); |
| 3634 | dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n", |
| 3635 | skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id, |
| 3636 | skreq->fitmsg_id); |
Bart Van Assche | a74d5b7 | 2017-08-17 13:13:33 -0700 | [diff] [blame] | 3637 | dev_dbg(&skdev->pdev->dev, " sg_dir=%d n_sg=%d\n", |
| 3638 | skreq->data_dir, skreq->n_sg); |
Bart Van Assche | ca33dd9 | 2017-08-17 13:13:32 -0700 | [diff] [blame] | 3639 | |
Bart Van Assche | e7278a8 | 2017-08-17 13:13:31 -0700 | [diff] [blame] | 3640 | dev_dbg(&skdev->pdev->dev, |
| 3641 | "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba, lba, |
| 3642 | count, count, (int)rq_data_dir(req)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | /* |
| 3646 | ***************************************************************************** |
| 3647 | * MODULE GLUE |
| 3648 | ***************************************************************************** |
| 3649 | */ |
| 3650 | |
| 3651 | static int __init skd_init(void) |
| 3652 | { |
Bart Van Assche | 16a7053 | 2017-08-17 13:13:08 -0700 | [diff] [blame] | 3653 | BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8); |
| 3654 | BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32); |
| 3655 | BUILD_BUG_ON(sizeof(struct skd_command_header) != 16); |
| 3656 | BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32); |
| 3657 | BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44); |
Bart Van Assche | d891fe6 | 2017-08-17 13:13:07 -0700 | [diff] [blame] | 3658 | BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0); |
| 3659 | BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64); |
| 3660 | BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES); |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 3661 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3662 | switch (skd_isr_type) { |
| 3663 | case SKD_IRQ_LEGACY: |
| 3664 | case SKD_IRQ_MSI: |
| 3665 | case SKD_IRQ_MSIX: |
| 3666 | break; |
| 3667 | default: |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 3668 | pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3669 | skd_isr_type, SKD_IRQ_DEFAULT); |
| 3670 | skd_isr_type = SKD_IRQ_DEFAULT; |
| 3671 | } |
| 3672 | |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 3673 | if (skd_max_queue_depth < 1 || |
| 3674 | skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) { |
| 3675 | pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3676 | skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT); |
| 3677 | skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT; |
| 3678 | } |
| 3679 | |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 3680 | if (skd_max_req_per_msg < 1 || |
| 3681 | skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 3682 | pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3683 | skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT); |
| 3684 | skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT; |
| 3685 | } |
| 3686 | |
| 3687 | if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 3688 | pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3689 | skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT); |
| 3690 | skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT; |
| 3691 | } |
| 3692 | |
| 3693 | if (skd_dbg_level < 0 || skd_dbg_level > 2) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 3694 | pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3695 | skd_dbg_level, 0); |
| 3696 | skd_dbg_level = 0; |
| 3697 | } |
| 3698 | |
| 3699 | if (skd_isr_comp_limit < 0) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 3700 | pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3701 | skd_isr_comp_limit, 0); |
| 3702 | skd_isr_comp_limit = 0; |
| 3703 | } |
| 3704 | |
Bartlomiej Zolnierkiewicz | b8df664 | 2013-11-05 12:37:02 +0100 | [diff] [blame] | 3705 | return pci_register_driver(&skd_driver); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | static void __exit skd_exit(void) |
| 3709 | { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3710 | pci_unregister_driver(&skd_driver); |
Bartlomiej Zolnierkiewicz | b8df664 | 2013-11-05 12:37:02 +0100 | [diff] [blame] | 3711 | |
| 3712 | if (skd_major) |
| 3713 | unregister_blkdev(skd_major, DRV_NAME); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3714 | } |
| 3715 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3716 | module_init(skd_init); |
| 3717 | module_exit(skd_exit); |