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