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> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/compiler.h> |
| 22 | #include <linux/workqueue.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <linux/hdreg.h> |
| 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/completion.h> |
| 28 | #include <linux/scatterlist.h> |
| 29 | #include <linux/version.h> |
| 30 | #include <linux/err.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 31 | #include <linux/aer.h> |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 32 | #include <linux/wait.h> |
| 33 | #include <linux/uio.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 { |
| 47 | STEC_LINK_2_5GTS = 0, |
| 48 | STEC_LINK_5GTS = 1, |
| 49 | STEC_LINK_8GTS = 2, |
| 50 | STEC_LINK_UNKNOWN = 0xFF |
| 51 | }; |
| 52 | |
| 53 | enum { |
| 54 | SKD_FLUSH_INITIALIZER, |
| 55 | SKD_FLUSH_ZERO_SIZE_FIRST, |
| 56 | SKD_FLUSH_DATA_SECOND, |
| 57 | }; |
| 58 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 59 | #define SKD_ASSERT(expr) \ |
| 60 | do { \ |
| 61 | if (unlikely(!(expr))) { \ |
| 62 | pr_err("Assertion failed! %s,%s,%s,line=%d\n", \ |
| 63 | # expr, __FILE__, __func__, __LINE__); \ |
| 64 | } \ |
| 65 | } while (0) |
| 66 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 67 | #define DRV_NAME "skd" |
| 68 | #define DRV_VERSION "2.2.1" |
| 69 | #define DRV_BUILD_ID "0260" |
| 70 | #define PFX DRV_NAME ": " |
| 71 | #define DRV_BIN_VERSION 0x100 |
| 72 | #define DRV_VER_COMPL "2.2.1." DRV_BUILD_ID |
| 73 | |
Bart Van Assche | bec9e8a | 2017-08-17 13:12:47 -0700 | [diff] [blame] | 74 | MODULE_LICENSE("GPL"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 75 | |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 76 | MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 77 | MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID); |
| 78 | |
| 79 | #define PCI_VENDOR_ID_STEC 0x1B39 |
| 80 | #define PCI_DEVICE_ID_S1120 0x0001 |
| 81 | |
| 82 | #define SKD_FUA_NV (1 << 1) |
| 83 | #define SKD_MINORS_PER_DEVICE 16 |
| 84 | |
| 85 | #define SKD_MAX_QUEUE_DEPTH 200u |
| 86 | |
| 87 | #define SKD_PAUSE_TIMEOUT (5 * 1000) |
| 88 | |
| 89 | #define SKD_N_FITMSG_BYTES (512u) |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 90 | #define SKD_MAX_REQ_PER_MSG 14 |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 91 | |
| 92 | #define SKD_N_SPECIAL_CONTEXT 32u |
| 93 | #define SKD_N_SPECIAL_FITMSG_BYTES (128u) |
| 94 | |
| 95 | /* SG elements are 32 bytes, so we can make this 4096 and still be under the |
| 96 | * 128KB limit. That allows 4096*4K = 16M xfer size |
| 97 | */ |
| 98 | #define SKD_N_SG_PER_REQ_DEFAULT 256u |
| 99 | #define SKD_N_SG_PER_SPECIAL 256u |
| 100 | |
| 101 | #define SKD_N_COMPLETION_ENTRY 256u |
| 102 | #define SKD_N_READ_CAP_BYTES (8u) |
| 103 | |
| 104 | #define SKD_N_INTERNAL_BYTES (512u) |
| 105 | |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 106 | #define SKD_SKCOMP_SIZE \ |
| 107 | ((sizeof(struct fit_completion_entry_v1) + \ |
| 108 | sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY) |
| 109 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 110 | /* 5 bits of uniqifier, 0xF800 */ |
| 111 | #define SKD_ID_INCR (0x400) |
| 112 | #define SKD_ID_TABLE_MASK (3u << 8u) |
| 113 | #define SKD_ID_RW_REQUEST (0u << 8u) |
| 114 | #define SKD_ID_INTERNAL (1u << 8u) |
| 115 | #define SKD_ID_SPECIAL_REQUEST (2u << 8u) |
| 116 | #define SKD_ID_FIT_MSG (3u << 8u) |
| 117 | #define SKD_ID_SLOT_MASK 0x00FFu |
| 118 | #define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu |
| 119 | |
| 120 | #define SKD_N_TIMEOUT_SLOT 4u |
| 121 | #define SKD_TIMEOUT_SLOT_MASK 3u |
| 122 | |
| 123 | #define SKD_N_MAX_SECTORS 2048u |
| 124 | |
| 125 | #define SKD_MAX_RETRIES 2u |
| 126 | |
| 127 | #define SKD_TIMER_SECONDS(seconds) (seconds) |
| 128 | #define SKD_TIMER_MINUTES(minutes) ((minutes) * (60)) |
| 129 | |
| 130 | #define INQ_STD_NBYTES 36 |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 131 | |
| 132 | enum skd_drvr_state { |
| 133 | SKD_DRVR_STATE_LOAD, |
| 134 | SKD_DRVR_STATE_IDLE, |
| 135 | SKD_DRVR_STATE_BUSY, |
| 136 | SKD_DRVR_STATE_STARTING, |
| 137 | SKD_DRVR_STATE_ONLINE, |
| 138 | SKD_DRVR_STATE_PAUSING, |
| 139 | SKD_DRVR_STATE_PAUSED, |
| 140 | SKD_DRVR_STATE_DRAINING_TIMEOUT, |
| 141 | SKD_DRVR_STATE_RESTARTING, |
| 142 | SKD_DRVR_STATE_RESUMING, |
| 143 | SKD_DRVR_STATE_STOPPING, |
| 144 | SKD_DRVR_STATE_FAULT, |
| 145 | SKD_DRVR_STATE_DISAPPEARED, |
| 146 | SKD_DRVR_STATE_PROTOCOL_MISMATCH, |
| 147 | SKD_DRVR_STATE_BUSY_ERASE, |
| 148 | SKD_DRVR_STATE_BUSY_SANITIZE, |
| 149 | SKD_DRVR_STATE_BUSY_IMMINENT, |
| 150 | SKD_DRVR_STATE_WAIT_BOOT, |
| 151 | SKD_DRVR_STATE_SYNCING, |
| 152 | }; |
| 153 | |
| 154 | #define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u) |
| 155 | #define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u) |
| 156 | #define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u) |
| 157 | #define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u) |
| 158 | #define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u) |
| 159 | #define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u) |
| 160 | #define SKD_START_WAIT_SECONDS 90u |
| 161 | |
| 162 | enum skd_req_state { |
| 163 | SKD_REQ_STATE_IDLE, |
| 164 | SKD_REQ_STATE_SETUP, |
| 165 | SKD_REQ_STATE_BUSY, |
| 166 | SKD_REQ_STATE_COMPLETED, |
| 167 | SKD_REQ_STATE_TIMEOUT, |
| 168 | SKD_REQ_STATE_ABORTED, |
| 169 | }; |
| 170 | |
| 171 | enum skd_fit_msg_state { |
| 172 | SKD_MSG_STATE_IDLE, |
| 173 | SKD_MSG_STATE_BUSY, |
| 174 | }; |
| 175 | |
| 176 | enum skd_check_status_action { |
| 177 | SKD_CHECK_STATUS_REPORT_GOOD, |
| 178 | SKD_CHECK_STATUS_REPORT_SMART_ALERT, |
| 179 | SKD_CHECK_STATUS_REQUEUE_REQUEST, |
| 180 | SKD_CHECK_STATUS_REPORT_ERROR, |
| 181 | SKD_CHECK_STATUS_BUSY_IMMINENT, |
| 182 | }; |
| 183 | |
| 184 | struct skd_fitmsg_context { |
| 185 | enum skd_fit_msg_state state; |
| 186 | |
| 187 | struct skd_fitmsg_context *next; |
| 188 | |
| 189 | u32 id; |
| 190 | u16 outstanding; |
| 191 | |
| 192 | u32 length; |
| 193 | u32 offset; |
| 194 | |
| 195 | u8 *msg_buf; |
| 196 | dma_addr_t mb_dma_address; |
| 197 | }; |
| 198 | |
| 199 | struct skd_request_context { |
| 200 | enum skd_req_state state; |
| 201 | |
| 202 | struct skd_request_context *next; |
| 203 | |
| 204 | u16 id; |
| 205 | u32 fitmsg_id; |
| 206 | |
| 207 | struct request *req; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 208 | u8 flush_cmd; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 209 | |
| 210 | u32 timeout_stamp; |
| 211 | u8 sg_data_dir; |
| 212 | struct scatterlist *sg; |
| 213 | u32 n_sg; |
| 214 | u32 sg_byte_count; |
| 215 | |
| 216 | struct fit_sg_descriptor *sksg_list; |
| 217 | dma_addr_t sksg_dma_address; |
| 218 | |
| 219 | struct fit_completion_entry_v1 completion; |
| 220 | |
| 221 | struct fit_comp_error_info err_info; |
| 222 | |
| 223 | }; |
| 224 | #define SKD_DATA_DIR_HOST_TO_CARD 1 |
| 225 | #define SKD_DATA_DIR_CARD_TO_HOST 2 |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 226 | |
| 227 | struct skd_special_context { |
| 228 | struct skd_request_context req; |
| 229 | |
| 230 | u8 orphaned; |
| 231 | |
| 232 | void *data_buf; |
| 233 | dma_addr_t db_dma_address; |
| 234 | |
| 235 | u8 *msg_buf; |
| 236 | dma_addr_t mb_dma_address; |
| 237 | }; |
| 238 | |
| 239 | struct skd_sg_io { |
| 240 | fmode_t mode; |
| 241 | void __user *argp; |
| 242 | |
| 243 | struct sg_io_hdr sg; |
| 244 | |
| 245 | u8 cdb[16]; |
| 246 | |
| 247 | u32 dxfer_len; |
| 248 | u32 iovcnt; |
| 249 | struct sg_iovec *iov; |
| 250 | struct sg_iovec no_iov_iov; |
| 251 | |
| 252 | struct skd_special_context *skspcl; |
| 253 | }; |
| 254 | |
| 255 | typedef enum skd_irq_type { |
| 256 | SKD_IRQ_LEGACY, |
| 257 | SKD_IRQ_MSI, |
| 258 | SKD_IRQ_MSIX |
| 259 | } skd_irq_type_t; |
| 260 | |
| 261 | #define SKD_MAX_BARS 2 |
| 262 | |
| 263 | struct skd_device { |
| 264 | volatile void __iomem *mem_map[SKD_MAX_BARS]; |
| 265 | resource_size_t mem_phys[SKD_MAX_BARS]; |
| 266 | u32 mem_size[SKD_MAX_BARS]; |
| 267 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 268 | struct skd_msix_entry *msix_entries; |
| 269 | |
| 270 | struct pci_dev *pdev; |
| 271 | int pcie_error_reporting_is_enabled; |
| 272 | |
| 273 | spinlock_t lock; |
| 274 | struct gendisk *disk; |
| 275 | struct request_queue *queue; |
| 276 | struct device *class_dev; |
| 277 | int gendisk_on; |
| 278 | int sync_done; |
| 279 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 280 | u32 devno; |
| 281 | u32 major; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 282 | char isr_name[30]; |
| 283 | |
| 284 | enum skd_drvr_state state; |
| 285 | u32 drive_state; |
| 286 | |
| 287 | u32 in_flight; |
| 288 | u32 cur_max_queue_depth; |
| 289 | u32 queue_low_water_mark; |
| 290 | u32 dev_max_queue_depth; |
| 291 | |
| 292 | u32 num_fitmsg_context; |
| 293 | u32 num_req_context; |
| 294 | |
| 295 | u32 timeout_slot[SKD_N_TIMEOUT_SLOT]; |
| 296 | u32 timeout_stamp; |
| 297 | struct skd_fitmsg_context *skmsg_free_list; |
| 298 | struct skd_fitmsg_context *skmsg_table; |
| 299 | |
| 300 | struct skd_request_context *skreq_free_list; |
| 301 | struct skd_request_context *skreq_table; |
| 302 | |
| 303 | struct skd_special_context *skspcl_free_list; |
| 304 | struct skd_special_context *skspcl_table; |
| 305 | |
| 306 | struct skd_special_context internal_skspcl; |
| 307 | u32 read_cap_blocksize; |
| 308 | u32 read_cap_last_lba; |
| 309 | int read_cap_is_valid; |
| 310 | int inquiry_is_valid; |
| 311 | u8 inq_serial_num[13]; /*12 chars plus null term */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 312 | |
| 313 | u8 skcomp_cycle; |
| 314 | u32 skcomp_ix; |
| 315 | struct fit_completion_entry_v1 *skcomp_table; |
| 316 | struct fit_comp_error_info *skerr_table; |
| 317 | dma_addr_t cq_dma_address; |
| 318 | |
| 319 | wait_queue_head_t waitq; |
| 320 | |
| 321 | struct timer_list timer; |
| 322 | u32 timer_countdown; |
| 323 | u32 timer_substate; |
| 324 | |
| 325 | int n_special; |
| 326 | int sgs_per_request; |
| 327 | u32 last_mtd; |
| 328 | |
| 329 | u32 proto_ver; |
| 330 | |
| 331 | int dbg_level; |
| 332 | u32 connect_time_stamp; |
| 333 | int connect_retries; |
| 334 | #define SKD_MAX_CONNECT_RETRIES 16 |
| 335 | u32 drive_jiffies; |
| 336 | |
| 337 | u32 timo_slot; |
| 338 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 339 | struct work_struct completion_worker; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 340 | }; |
| 341 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 342 | #define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF) |
| 343 | #define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF) |
| 344 | #define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF) |
| 345 | |
| 346 | static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset) |
| 347 | { |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 348 | u32 val = readl(skdev->mem_map[1] + offset); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 349 | |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 350 | if (unlikely(skdev->dbg_level >= 2)) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 351 | dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val); |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 352 | return val; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static inline void skd_reg_write32(struct skd_device *skdev, u32 val, |
| 356 | u32 offset) |
| 357 | { |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 358 | writel(val, skdev->mem_map[1] + offset); |
| 359 | if (unlikely(skdev->dbg_level >= 2)) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 360 | dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static inline void skd_reg_write64(struct skd_device *skdev, u64 val, |
| 364 | u32 offset) |
| 365 | { |
Bart Van Assche | 14262a4 | 2017-08-17 13:12:57 -0700 | [diff] [blame] | 366 | writeq(val, skdev->mem_map[1] + offset); |
| 367 | if (unlikely(skdev->dbg_level >= 2)) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 368 | dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset, |
| 369 | val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
| 373 | #define SKD_IRQ_DEFAULT SKD_IRQ_MSI |
| 374 | static int skd_isr_type = SKD_IRQ_DEFAULT; |
| 375 | |
| 376 | module_param(skd_isr_type, int, 0444); |
| 377 | MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability." |
| 378 | " (0==legacy, 1==MSI, 2==MSI-X, default==1)"); |
| 379 | |
| 380 | #define SKD_MAX_REQ_PER_MSG_DEFAULT 1 |
| 381 | static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT; |
| 382 | |
| 383 | module_param(skd_max_req_per_msg, int, 0444); |
| 384 | MODULE_PARM_DESC(skd_max_req_per_msg, |
| 385 | "Maximum SCSI requests packed in a single message." |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 386 | " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 387 | |
| 388 | #define SKD_MAX_QUEUE_DEPTH_DEFAULT 64 |
| 389 | #define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64" |
| 390 | static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT; |
| 391 | |
| 392 | module_param(skd_max_queue_depth, int, 0444); |
| 393 | MODULE_PARM_DESC(skd_max_queue_depth, |
| 394 | "Maximum SCSI requests issued to s1120." |
| 395 | " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")"); |
| 396 | |
| 397 | static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT; |
| 398 | module_param(skd_sgs_per_request, int, 0444); |
| 399 | MODULE_PARM_DESC(skd_sgs_per_request, |
| 400 | "Maximum SG elements per block request." |
| 401 | " (1-4096, default==256)"); |
| 402 | |
| 403 | static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT; |
| 404 | module_param(skd_max_pass_thru, int, 0444); |
| 405 | MODULE_PARM_DESC(skd_max_pass_thru, |
| 406 | "Maximum SCSI pass-thru at a time." " (1-50, default==32)"); |
| 407 | |
| 408 | module_param(skd_dbg_level, int, 0444); |
| 409 | MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)"); |
| 410 | |
| 411 | module_param(skd_isr_comp_limit, int, 0444); |
| 412 | MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4"); |
| 413 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 414 | /* Major device number dynamically assigned. */ |
| 415 | static u32 skd_major; |
| 416 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 417 | static void skd_destruct(struct skd_device *skdev); |
| 418 | static const struct block_device_operations skd_blockdev_ops; |
| 419 | static void skd_send_fitmsg(struct skd_device *skdev, |
| 420 | struct skd_fitmsg_context *skmsg); |
| 421 | static void skd_send_special_fitmsg(struct skd_device *skdev, |
| 422 | struct skd_special_context *skspcl); |
| 423 | static void skd_request_fn(struct request_queue *rq); |
| 424 | static void skd_end_request(struct skd_device *skdev, |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 425 | struct skd_request_context *skreq, blk_status_t status); |
| 426 | static bool skd_preop_sg_list(struct skd_device *skdev, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 427 | struct skd_request_context *skreq); |
| 428 | static void skd_postop_sg_list(struct skd_device *skdev, |
| 429 | struct skd_request_context *skreq); |
| 430 | |
| 431 | static void skd_restart_device(struct skd_device *skdev); |
| 432 | static int skd_quiesce_dev(struct skd_device *skdev); |
| 433 | static int skd_unquiesce_dev(struct skd_device *skdev); |
| 434 | static void skd_release_special(struct skd_device *skdev, |
| 435 | struct skd_special_context *skspcl); |
| 436 | static void skd_disable_interrupts(struct skd_device *skdev); |
| 437 | static void skd_isr_fwstate(struct skd_device *skdev); |
| 438 | static void skd_recover_requests(struct skd_device *skdev, int requeue); |
| 439 | static void skd_soft_reset(struct skd_device *skdev); |
| 440 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 441 | const char *skd_drive_state_to_str(int state); |
| 442 | const char *skd_skdev_state_to_str(enum skd_drvr_state state); |
| 443 | static void skd_log_skdev(struct skd_device *skdev, const char *event); |
| 444 | static void skd_log_skmsg(struct skd_device *skdev, |
| 445 | struct skd_fitmsg_context *skmsg, const char *event); |
| 446 | static void skd_log_skreq(struct skd_device *skdev, |
| 447 | struct skd_request_context *skreq, const char *event); |
| 448 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 449 | /* |
| 450 | ***************************************************************************** |
| 451 | * READ/WRITE REQUESTS |
| 452 | ***************************************************************************** |
| 453 | */ |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 454 | static void skd_fail_all_pending(struct skd_device *skdev) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 455 | { |
| 456 | struct request_queue *q = skdev->queue; |
| 457 | struct request *req; |
| 458 | |
| 459 | for (;; ) { |
| 460 | req = blk_peek_request(q); |
| 461 | if (req == NULL) |
| 462 | break; |
| 463 | blk_start_request(req); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 464 | __blk_end_request_all(req, BLK_STS_IOERR); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 468 | static void |
| 469 | skd_prep_rw_cdb(struct skd_scsi_request *scsi_req, |
| 470 | int data_dir, unsigned lba, |
| 471 | unsigned count) |
| 472 | { |
| 473 | if (data_dir == READ) |
| 474 | scsi_req->cdb[0] = 0x28; |
| 475 | else |
| 476 | scsi_req->cdb[0] = 0x2a; |
| 477 | |
| 478 | scsi_req->cdb[1] = 0; |
| 479 | scsi_req->cdb[2] = (lba & 0xff000000) >> 24; |
| 480 | scsi_req->cdb[3] = (lba & 0xff0000) >> 16; |
| 481 | scsi_req->cdb[4] = (lba & 0xff00) >> 8; |
| 482 | scsi_req->cdb[5] = (lba & 0xff); |
| 483 | scsi_req->cdb[6] = 0; |
| 484 | scsi_req->cdb[7] = (count & 0xff00) >> 8; |
| 485 | scsi_req->cdb[8] = count & 0xff; |
| 486 | scsi_req->cdb[9] = 0; |
| 487 | } |
| 488 | |
| 489 | static void |
| 490 | skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req, |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 491 | struct skd_request_context *skreq) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 492 | { |
| 493 | skreq->flush_cmd = 1; |
| 494 | |
| 495 | scsi_req->cdb[0] = 0x35; |
| 496 | scsi_req->cdb[1] = 0; |
| 497 | scsi_req->cdb[2] = 0; |
| 498 | scsi_req->cdb[3] = 0; |
| 499 | scsi_req->cdb[4] = 0; |
| 500 | scsi_req->cdb[5] = 0; |
| 501 | scsi_req->cdb[6] = 0; |
| 502 | scsi_req->cdb[7] = 0; |
| 503 | scsi_req->cdb[8] = 0; |
| 504 | scsi_req->cdb[9] = 0; |
| 505 | } |
| 506 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 507 | static void skd_request_fn_not_online(struct request_queue *q); |
| 508 | |
| 509 | static void skd_request_fn(struct request_queue *q) |
| 510 | { |
| 511 | struct skd_device *skdev = q->queuedata; |
| 512 | struct skd_fitmsg_context *skmsg = NULL; |
| 513 | struct fit_msg_hdr *fmh = NULL; |
| 514 | struct skd_request_context *skreq; |
| 515 | struct request *req = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 516 | struct skd_scsi_request *scsi_req; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 517 | unsigned long io_flags; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 518 | u32 lba; |
| 519 | u32 count; |
| 520 | int data_dir; |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 521 | __be64 be_dmaa; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 522 | u64 cmdctxt; |
| 523 | u32 timo_slot; |
| 524 | void *cmd_ptr; |
| 525 | int flush, fua; |
| 526 | |
| 527 | if (skdev->state != SKD_DRVR_STATE_ONLINE) { |
| 528 | skd_request_fn_not_online(q); |
| 529 | return; |
| 530 | } |
| 531 | |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 532 | if (blk_queue_stopped(skdev->queue)) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 533 | if (skdev->skmsg_free_list == NULL || |
| 534 | skdev->skreq_free_list == NULL || |
| 535 | skdev->in_flight >= skdev->queue_low_water_mark) |
| 536 | /* There is still some kind of shortage */ |
| 537 | return; |
| 538 | |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 539 | queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Stop conditions: |
| 544 | * - There are no more native requests |
| 545 | * - There are already the maximum number of requests in progress |
| 546 | * - There are no more skd_request_context entries |
| 547 | * - There are no more FIT msg buffers |
| 548 | */ |
| 549 | for (;; ) { |
| 550 | |
| 551 | flush = fua = 0; |
| 552 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 553 | req = blk_peek_request(q); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 554 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 555 | /* Are there any native requests to start? */ |
| 556 | if (req == NULL) |
| 557 | break; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 558 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 559 | lba = (u32)blk_rq_pos(req); |
| 560 | count = blk_rq_sectors(req); |
| 561 | data_dir = rq_data_dir(req); |
| 562 | io_flags = req->cmd_flags; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 563 | |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 564 | if (req_op(req) == REQ_OP_FLUSH) |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 565 | flush++; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 566 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 567 | if (io_flags & REQ_FUA) |
| 568 | fua++; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 569 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 570 | dev_dbg(&skdev->pdev->dev, |
| 571 | "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", |
| 572 | req, lba, lba, count, count, data_dir); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 573 | |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 574 | /* At this point we know there is a request */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 575 | |
| 576 | /* Are too many requets already in progress? */ |
| 577 | if (skdev->in_flight >= skdev->cur_max_queue_depth) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 578 | dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n", |
| 579 | skdev->in_flight, skdev->cur_max_queue_depth); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | |
| 583 | /* Is a skd_request_context available? */ |
| 584 | skreq = skdev->skreq_free_list; |
| 585 | if (skreq == NULL) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 586 | dev_dbg(&skdev->pdev->dev, "Out of req=%p\n", q); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE); |
| 590 | SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0); |
| 591 | |
| 592 | /* Now we check to see if we can get a fit msg */ |
| 593 | if (skmsg == NULL) { |
| 594 | if (skdev->skmsg_free_list == NULL) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 595 | dev_dbg(&skdev->pdev->dev, "Out of msg\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | skreq->flush_cmd = 0; |
| 601 | skreq->n_sg = 0; |
| 602 | skreq->sg_byte_count = 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 603 | |
| 604 | /* |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 605 | * OK to now dequeue request from q. |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 606 | * |
| 607 | * At this point we are comitted to either start or reject |
| 608 | * the native request. Note that skd_request_context is |
| 609 | * available but is still at the head of the free list. |
| 610 | */ |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 611 | blk_start_request(req); |
| 612 | skreq->req = req; |
| 613 | skreq->fitmsg_id = 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 614 | |
Bart Van Assche | 19fc85c | 2017-08-17 13:13:04 -0700 | [diff] [blame] | 615 | skreq->sg_data_dir = data_dir == READ ? |
| 616 | SKD_DATA_DIR_CARD_TO_HOST : SKD_DATA_DIR_HOST_TO_CARD; |
| 617 | |
| 618 | if (req->bio && !skd_preop_sg_list(skdev, skreq)) { |
| 619 | dev_dbg(&skdev->pdev->dev, "error Out\n"); |
| 620 | skd_end_request(skdev, skreq, BLK_STS_RESOURCE); |
| 621 | continue; |
| 622 | } |
| 623 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 624 | /* Either a FIT msg is in progress or we have to start one. */ |
| 625 | if (skmsg == NULL) { |
| 626 | /* Are there any FIT msg buffers available? */ |
| 627 | skmsg = skdev->skmsg_free_list; |
| 628 | if (skmsg == NULL) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 629 | dev_dbg(&skdev->pdev->dev, |
| 630 | "Out of msg skdev=%p\n", |
| 631 | skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE); |
| 635 | SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0); |
| 636 | |
| 637 | skdev->skmsg_free_list = skmsg->next; |
| 638 | |
| 639 | skmsg->state = SKD_MSG_STATE_BUSY; |
| 640 | skmsg->id += SKD_ID_INCR; |
| 641 | |
| 642 | /* Initialize the FIT msg header */ |
| 643 | fmh = (struct fit_msg_hdr *)skmsg->msg_buf; |
| 644 | memset(fmh, 0, sizeof(*fmh)); |
| 645 | fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT; |
| 646 | skmsg->length = sizeof(*fmh); |
| 647 | } |
| 648 | |
| 649 | skreq->fitmsg_id = skmsg->id; |
| 650 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 651 | cmd_ptr = &skmsg->msg_buf[skmsg->length]; |
| 652 | memset(cmd_ptr, 0, 32); |
| 653 | |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 654 | be_dmaa = cpu_to_be64(skreq->sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 655 | cmdctxt = skreq->id + SKD_ID_INCR; |
| 656 | |
| 657 | scsi_req = cmd_ptr; |
| 658 | scsi_req->hdr.tag = cmdctxt; |
| 659 | scsi_req->hdr.sg_list_dma_address = be_dmaa; |
| 660 | |
Jeff Moyer | 49bdedb | 2016-04-25 19:12:38 -0600 | [diff] [blame] | 661 | if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 662 | skd_prep_zerosize_flush_cdb(scsi_req, skreq); |
| 663 | SKD_ASSERT(skreq->flush_cmd == 1); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 664 | } else { |
| 665 | skd_prep_rw_cdb(scsi_req, data_dir, lba, count); |
| 666 | } |
| 667 | |
| 668 | if (fua) |
| 669 | scsi_req->cdb[1] |= SKD_FUA_NV; |
| 670 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 671 | scsi_req->hdr.sg_list_len_bytes = |
| 672 | cpu_to_be32(skreq->sg_byte_count); |
| 673 | |
| 674 | /* Complete resource allocations. */ |
| 675 | skdev->skreq_free_list = skreq->next; |
| 676 | skreq->state = SKD_REQ_STATE_BUSY; |
| 677 | skreq->id += SKD_ID_INCR; |
| 678 | |
| 679 | skmsg->length += sizeof(struct skd_scsi_request); |
| 680 | fmh->num_protocol_cmds_coalesced++; |
| 681 | |
| 682 | /* |
| 683 | * Update the active request counts. |
| 684 | * Capture the timeout timestamp. |
| 685 | */ |
| 686 | skreq->timeout_stamp = skdev->timeout_stamp; |
| 687 | timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK; |
| 688 | skdev->timeout_slot[timo_slot]++; |
| 689 | skdev->in_flight++; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 690 | dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id, |
| 691 | skdev->in_flight); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 692 | |
| 693 | /* |
| 694 | * If the FIT msg buffer is full send it. |
| 695 | */ |
Bart Van Assche | fe4fd72 | 2017-08-17 13:13:05 -0700 | [diff] [blame^] | 696 | if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 697 | skd_send_fitmsg(skdev, skmsg); |
| 698 | skmsg = NULL; |
| 699 | fmh = NULL; |
| 700 | } |
| 701 | } |
| 702 | |
Bart Van Assche | fe4fd72 | 2017-08-17 13:13:05 -0700 | [diff] [blame^] | 703 | /* If the FIT msg buffer is not empty send what we got. */ |
| 704 | if (skmsg) { |
| 705 | WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced); |
| 706 | skd_send_fitmsg(skdev, skmsg); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 707 | skmsg = NULL; |
| 708 | fmh = NULL; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * If req is non-NULL it means there is something to do but |
| 713 | * we are out of a resource. |
| 714 | */ |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 715 | if (req) |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 716 | blk_stop_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 717 | } |
| 718 | |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 719 | static void skd_end_request(struct skd_device *skdev, |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 720 | struct skd_request_context *skreq, blk_status_t error) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 721 | { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 722 | if (unlikely(error)) { |
| 723 | struct request *req = skreq->req; |
| 724 | char *cmd = (rq_data_dir(req) == READ) ? "read" : "write"; |
| 725 | u32 lba = (u32)blk_rq_pos(req); |
| 726 | u32 count = blk_rq_sectors(req); |
| 727 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 728 | dev_err(&skdev->pdev->dev, |
| 729 | "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba, |
| 730 | count, skreq->id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 731 | } else |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 732 | dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", skreq->id, |
| 733 | error); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 734 | |
| 735 | __blk_end_request_all(skreq->req, error); |
| 736 | } |
| 737 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 738 | static bool skd_preop_sg_list(struct skd_device *skdev, |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 739 | struct skd_request_context *skreq) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 740 | { |
| 741 | struct request *req = skreq->req; |
| 742 | int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD; |
| 743 | int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE; |
| 744 | struct scatterlist *sg = &skreq->sg[0]; |
| 745 | int n_sg; |
| 746 | int i; |
| 747 | |
| 748 | skreq->sg_byte_count = 0; |
| 749 | |
| 750 | /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD || |
| 751 | skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */ |
| 752 | |
| 753 | n_sg = blk_rq_map_sg(skdev->queue, req, sg); |
| 754 | if (n_sg <= 0) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 755 | return false; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * Map scatterlist to PCI bus addresses. |
| 759 | * Note PCI might change the number of entries. |
| 760 | */ |
| 761 | n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir); |
| 762 | if (n_sg <= 0) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 763 | return false; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 764 | |
| 765 | SKD_ASSERT(n_sg <= skdev->sgs_per_request); |
| 766 | |
| 767 | skreq->n_sg = n_sg; |
| 768 | |
| 769 | for (i = 0; i < n_sg; i++) { |
| 770 | struct fit_sg_descriptor *sgd = &skreq->sksg_list[i]; |
| 771 | u32 cnt = sg_dma_len(&sg[i]); |
| 772 | uint64_t dma_addr = sg_dma_address(&sg[i]); |
| 773 | |
| 774 | sgd->control = FIT_SGD_CONTROL_NOT_LAST; |
| 775 | sgd->byte_count = cnt; |
| 776 | skreq->sg_byte_count += cnt; |
| 777 | sgd->host_side_addr = dma_addr; |
| 778 | sgd->dev_side_addr = 0; |
| 779 | } |
| 780 | |
| 781 | skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL; |
| 782 | skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST; |
| 783 | |
| 784 | if (unlikely(skdev->dbg_level > 1)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 785 | dev_dbg(&skdev->pdev->dev, |
| 786 | "skreq=%x sksg_list=%p sksg_dma=%llx\n", |
| 787 | skreq->id, skreq->sksg_list, skreq->sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 788 | for (i = 0; i < n_sg; i++) { |
| 789 | struct fit_sg_descriptor *sgd = &skreq->sksg_list[i]; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 790 | |
| 791 | dev_dbg(&skdev->pdev->dev, |
| 792 | " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n", |
| 793 | i, sgd->byte_count, sgd->control, |
| 794 | sgd->host_side_addr, sgd->next_desc_ptr); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 798 | return true; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 799 | } |
| 800 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 801 | static void skd_postop_sg_list(struct skd_device *skdev, |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 802 | struct skd_request_context *skreq) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 803 | { |
| 804 | int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD; |
| 805 | int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE; |
| 806 | |
| 807 | /* |
| 808 | * restore the next ptr for next IO request so we |
| 809 | * don't have to set it every time. |
| 810 | */ |
| 811 | skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr = |
| 812 | skreq->sksg_dma_address + |
| 813 | ((skreq->n_sg) * sizeof(struct fit_sg_descriptor)); |
| 814 | pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir); |
| 815 | } |
| 816 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 817 | static void skd_request_fn_not_online(struct request_queue *q) |
| 818 | { |
| 819 | struct skd_device *skdev = q->queuedata; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 820 | |
| 821 | SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE); |
| 822 | |
| 823 | skd_log_skdev(skdev, "req_not_online"); |
| 824 | switch (skdev->state) { |
| 825 | case SKD_DRVR_STATE_PAUSING: |
| 826 | case SKD_DRVR_STATE_PAUSED: |
| 827 | case SKD_DRVR_STATE_STARTING: |
| 828 | case SKD_DRVR_STATE_RESTARTING: |
| 829 | case SKD_DRVR_STATE_WAIT_BOOT: |
| 830 | /* In case of starting, we haven't started the queue, |
| 831 | * so we can't get here... but requests are |
| 832 | * possibly hanging out waiting for us because we |
| 833 | * reported the dev/skd0 already. They'll wait |
| 834 | * forever if connect doesn't complete. |
| 835 | * What to do??? delay dev/skd0 ?? |
| 836 | */ |
| 837 | case SKD_DRVR_STATE_BUSY: |
| 838 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 839 | case SKD_DRVR_STATE_BUSY_ERASE: |
| 840 | case SKD_DRVR_STATE_DRAINING_TIMEOUT: |
| 841 | return; |
| 842 | |
| 843 | case SKD_DRVR_STATE_BUSY_SANITIZE: |
| 844 | case SKD_DRVR_STATE_STOPPING: |
| 845 | case SKD_DRVR_STATE_SYNCING: |
| 846 | case SKD_DRVR_STATE_FAULT: |
| 847 | case SKD_DRVR_STATE_DISAPPEARED: |
| 848 | default: |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 849 | break; |
| 850 | } |
| 851 | |
| 852 | /* If we get here, terminate all pending block requeusts |
| 853 | * with EIO and any scsi pass thru with appropriate sense |
| 854 | */ |
| 855 | |
| 856 | skd_fail_all_pending(skdev); |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | ***************************************************************************** |
| 861 | * TIMER |
| 862 | ***************************************************************************** |
| 863 | */ |
| 864 | |
| 865 | static void skd_timer_tick_not_online(struct skd_device *skdev); |
| 866 | |
| 867 | static void skd_timer_tick(ulong arg) |
| 868 | { |
| 869 | struct skd_device *skdev = (struct skd_device *)arg; |
| 870 | |
| 871 | u32 timo_slot; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 872 | unsigned long reqflags; |
| 873 | u32 state; |
| 874 | |
| 875 | if (skdev->state == SKD_DRVR_STATE_FAULT) |
| 876 | /* The driver has declared fault, and we want it to |
| 877 | * stay that way until driver is reloaded. |
| 878 | */ |
| 879 | return; |
| 880 | |
| 881 | spin_lock_irqsave(&skdev->lock, reqflags); |
| 882 | |
| 883 | state = SKD_READL(skdev, FIT_STATUS); |
| 884 | state &= FIT_SR_DRIVE_STATE_MASK; |
| 885 | if (state != skdev->drive_state) |
| 886 | skd_isr_fwstate(skdev); |
| 887 | |
| 888 | if (skdev->state != SKD_DRVR_STATE_ONLINE) { |
| 889 | skd_timer_tick_not_online(skdev); |
| 890 | goto timer_func_out; |
| 891 | } |
| 892 | skdev->timeout_stamp++; |
| 893 | timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK; |
| 894 | |
| 895 | /* |
| 896 | * All requests that happened during the previous use of |
| 897 | * this slot should be done by now. The previous use was |
| 898 | * over 7 seconds ago. |
| 899 | */ |
| 900 | if (skdev->timeout_slot[timo_slot] == 0) |
| 901 | goto timer_func_out; |
| 902 | |
| 903 | /* Something is overdue */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 904 | dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n", |
| 905 | skdev->timeout_slot[timo_slot], skdev->in_flight); |
| 906 | dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n", |
| 907 | skdev->timeout_slot[timo_slot], skdev->in_flight); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 908 | |
| 909 | skdev->timer_countdown = SKD_DRAINING_TIMO; |
| 910 | skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT; |
| 911 | skdev->timo_slot = timo_slot; |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 912 | blk_stop_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 913 | |
| 914 | timer_func_out: |
| 915 | mod_timer(&skdev->timer, (jiffies + HZ)); |
| 916 | |
| 917 | spin_unlock_irqrestore(&skdev->lock, reqflags); |
| 918 | } |
| 919 | |
| 920 | static void skd_timer_tick_not_online(struct skd_device *skdev) |
| 921 | { |
| 922 | switch (skdev->state) { |
| 923 | case SKD_DRVR_STATE_IDLE: |
| 924 | case SKD_DRVR_STATE_LOAD: |
| 925 | break; |
| 926 | case SKD_DRVR_STATE_BUSY_SANITIZE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 927 | dev_dbg(&skdev->pdev->dev, |
| 928 | "drive busy sanitize[%x], driver[%x]\n", |
| 929 | skdev->drive_state, skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 930 | /* If we've been in sanitize for 3 seconds, we figure we're not |
| 931 | * going to get anymore completions, so recover requests now |
| 932 | */ |
| 933 | if (skdev->timer_countdown > 0) { |
| 934 | skdev->timer_countdown--; |
| 935 | return; |
| 936 | } |
| 937 | skd_recover_requests(skdev, 0); |
| 938 | break; |
| 939 | |
| 940 | case SKD_DRVR_STATE_BUSY: |
| 941 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 942 | case SKD_DRVR_STATE_BUSY_ERASE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 943 | dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n", |
| 944 | skdev->state, skdev->timer_countdown); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 945 | if (skdev->timer_countdown > 0) { |
| 946 | skdev->timer_countdown--; |
| 947 | return; |
| 948 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 949 | dev_dbg(&skdev->pdev->dev, |
| 950 | "busy[%x], timedout=%d, restarting device.", |
| 951 | skdev->state, skdev->timer_countdown); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 952 | skd_restart_device(skdev); |
| 953 | break; |
| 954 | |
| 955 | case SKD_DRVR_STATE_WAIT_BOOT: |
| 956 | case SKD_DRVR_STATE_STARTING: |
| 957 | if (skdev->timer_countdown > 0) { |
| 958 | skdev->timer_countdown--; |
| 959 | return; |
| 960 | } |
| 961 | /* For now, we fault the drive. Could attempt resets to |
| 962 | * revcover at some point. */ |
| 963 | skdev->state = SKD_DRVR_STATE_FAULT; |
| 964 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 965 | dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n", |
| 966 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 967 | |
| 968 | /*start the queue so we can respond with error to requests */ |
| 969 | /* wakeup anyone waiting for startup complete */ |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 970 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 971 | skdev->gendisk_on = -1; |
| 972 | wake_up_interruptible(&skdev->waitq); |
| 973 | break; |
| 974 | |
| 975 | case SKD_DRVR_STATE_ONLINE: |
| 976 | /* shouldn't get here. */ |
| 977 | break; |
| 978 | |
| 979 | case SKD_DRVR_STATE_PAUSING: |
| 980 | case SKD_DRVR_STATE_PAUSED: |
| 981 | break; |
| 982 | |
| 983 | case SKD_DRVR_STATE_DRAINING_TIMEOUT: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 984 | dev_dbg(&skdev->pdev->dev, |
| 985 | "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n", |
| 986 | skdev->timo_slot, skdev->timer_countdown, |
| 987 | skdev->in_flight, |
| 988 | skdev->timeout_slot[skdev->timo_slot]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 989 | /* if the slot has cleared we can let the I/O continue */ |
| 990 | if (skdev->timeout_slot[skdev->timo_slot] == 0) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 991 | dev_dbg(&skdev->pdev->dev, |
| 992 | "Slot drained, starting queue.\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 993 | skdev->state = SKD_DRVR_STATE_ONLINE; |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 994 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 995 | return; |
| 996 | } |
| 997 | if (skdev->timer_countdown > 0) { |
| 998 | skdev->timer_countdown--; |
| 999 | return; |
| 1000 | } |
| 1001 | skd_restart_device(skdev); |
| 1002 | break; |
| 1003 | |
| 1004 | case SKD_DRVR_STATE_RESTARTING: |
| 1005 | if (skdev->timer_countdown > 0) { |
| 1006 | skdev->timer_countdown--; |
| 1007 | return; |
| 1008 | } |
| 1009 | /* For now, we fault the drive. Could attempt resets to |
| 1010 | * revcover at some point. */ |
| 1011 | skdev->state = SKD_DRVR_STATE_FAULT; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1012 | dev_err(&skdev->pdev->dev, |
| 1013 | "DriveFault Reconnect Timeout (%x)\n", |
| 1014 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1015 | |
| 1016 | /* |
| 1017 | * Recovering does two things: |
| 1018 | * 1. completes IO with error |
| 1019 | * 2. reclaims dma resources |
| 1020 | * When is it safe to recover requests? |
| 1021 | * - if the drive state is faulted |
| 1022 | * - if the state is still soft reset after out timeout |
| 1023 | * - if the drive registers are dead (state = FF) |
| 1024 | * If it is "unsafe", we still need to recover, so we will |
| 1025 | * disable pci bus mastering and disable our interrupts. |
| 1026 | */ |
| 1027 | |
| 1028 | if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) || |
| 1029 | (skdev->drive_state == FIT_SR_DRIVE_FAULT) || |
| 1030 | (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK)) |
| 1031 | /* It never came out of soft reset. Try to |
| 1032 | * recover the requests and then let them |
| 1033 | * fail. This is to mitigate hung processes. */ |
| 1034 | skd_recover_requests(skdev, 0); |
| 1035 | else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1036 | dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n", |
| 1037 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1038 | pci_disable_device(skdev->pdev); |
| 1039 | skd_disable_interrupts(skdev); |
| 1040 | skd_recover_requests(skdev, 0); |
| 1041 | } |
| 1042 | |
| 1043 | /*start the queue so we can respond with error to requests */ |
| 1044 | /* wakeup anyone waiting for startup complete */ |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 1045 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1046 | skdev->gendisk_on = -1; |
| 1047 | wake_up_interruptible(&skdev->waitq); |
| 1048 | break; |
| 1049 | |
| 1050 | case SKD_DRVR_STATE_RESUMING: |
| 1051 | case SKD_DRVR_STATE_STOPPING: |
| 1052 | case SKD_DRVR_STATE_SYNCING: |
| 1053 | case SKD_DRVR_STATE_FAULT: |
| 1054 | case SKD_DRVR_STATE_DISAPPEARED: |
| 1055 | default: |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | static int skd_start_timer(struct skd_device *skdev) |
| 1061 | { |
| 1062 | int rc; |
| 1063 | |
| 1064 | init_timer(&skdev->timer); |
| 1065 | setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev); |
| 1066 | |
| 1067 | rc = mod_timer(&skdev->timer, (jiffies + HZ)); |
| 1068 | if (rc) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1069 | dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1070 | return rc; |
| 1071 | } |
| 1072 | |
| 1073 | static void skd_kill_timer(struct skd_device *skdev) |
| 1074 | { |
| 1075 | del_timer_sync(&skdev->timer); |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | ***************************************************************************** |
| 1080 | * IOCTL |
| 1081 | ***************************************************************************** |
| 1082 | */ |
| 1083 | static int skd_ioctl_sg_io(struct skd_device *skdev, |
| 1084 | fmode_t mode, void __user *argp); |
| 1085 | static int skd_sg_io_get_and_check_args(struct skd_device *skdev, |
| 1086 | struct skd_sg_io *sksgio); |
| 1087 | static int skd_sg_io_obtain_skspcl(struct skd_device *skdev, |
| 1088 | struct skd_sg_io *sksgio); |
| 1089 | static int skd_sg_io_prep_buffering(struct skd_device *skdev, |
| 1090 | struct skd_sg_io *sksgio); |
| 1091 | static int skd_sg_io_copy_buffer(struct skd_device *skdev, |
| 1092 | struct skd_sg_io *sksgio, int dxfer_dir); |
| 1093 | static int skd_sg_io_send_fitmsg(struct skd_device *skdev, |
| 1094 | struct skd_sg_io *sksgio); |
| 1095 | static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio); |
| 1096 | static int skd_sg_io_release_skspcl(struct skd_device *skdev, |
| 1097 | struct skd_sg_io *sksgio); |
| 1098 | static int skd_sg_io_put_status(struct skd_device *skdev, |
| 1099 | struct skd_sg_io *sksgio); |
| 1100 | |
| 1101 | static void skd_complete_special(struct skd_device *skdev, |
| 1102 | volatile struct fit_completion_entry_v1 |
| 1103 | *skcomp, |
| 1104 | volatile struct fit_comp_error_info *skerr, |
| 1105 | struct skd_special_context *skspcl); |
| 1106 | |
| 1107 | static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode, |
| 1108 | uint cmd_in, ulong arg) |
| 1109 | { |
Christoph Hellwig | 3719fa8 | 2017-01-28 09:32:50 +0100 | [diff] [blame] | 1110 | static const int sg_version_num = 30527; |
| 1111 | int rc = 0, timeout; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1112 | struct gendisk *disk = bdev->bd_disk; |
| 1113 | struct skd_device *skdev = disk->private_data; |
Christoph Hellwig | 3719fa8 | 2017-01-28 09:32:50 +0100 | [diff] [blame] | 1114 | int __user *p = (int __user *)arg; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1115 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1116 | dev_dbg(&skdev->pdev->dev, |
| 1117 | "%s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n", |
| 1118 | disk->disk_name, current->comm, mode, cmd_in, arg); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1119 | |
| 1120 | if (!capable(CAP_SYS_ADMIN)) |
| 1121 | return -EPERM; |
| 1122 | |
| 1123 | switch (cmd_in) { |
| 1124 | case SG_SET_TIMEOUT: |
Christoph Hellwig | 3719fa8 | 2017-01-28 09:32:50 +0100 | [diff] [blame] | 1125 | rc = get_user(timeout, p); |
| 1126 | if (!rc) |
| 1127 | disk->queue->sg_timeout = clock_t_to_jiffies(timeout); |
| 1128 | break; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1129 | case SG_GET_TIMEOUT: |
Christoph Hellwig | 3719fa8 | 2017-01-28 09:32:50 +0100 | [diff] [blame] | 1130 | rc = jiffies_to_clock_t(disk->queue->sg_timeout); |
| 1131 | break; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1132 | case SG_GET_VERSION_NUM: |
Christoph Hellwig | 3719fa8 | 2017-01-28 09:32:50 +0100 | [diff] [blame] | 1133 | rc = put_user(sg_version_num, p); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1134 | break; |
| 1135 | case SG_IO: |
Christoph Hellwig | 3719fa8 | 2017-01-28 09:32:50 +0100 | [diff] [blame] | 1136 | rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1137 | break; |
| 1138 | |
| 1139 | default: |
| 1140 | rc = -ENOTTY; |
| 1141 | break; |
| 1142 | } |
| 1143 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1144 | dev_dbg(&skdev->pdev->dev, "%s: completion rc %d\n", disk->disk_name, |
| 1145 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1146 | return rc; |
| 1147 | } |
| 1148 | |
| 1149 | static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode, |
| 1150 | void __user *argp) |
| 1151 | { |
| 1152 | int rc; |
| 1153 | struct skd_sg_io sksgio; |
| 1154 | |
| 1155 | memset(&sksgio, 0, sizeof(sksgio)); |
| 1156 | sksgio.mode = mode; |
| 1157 | sksgio.argp = argp; |
| 1158 | sksgio.iov = &sksgio.no_iov_iov; |
| 1159 | |
| 1160 | switch (skdev->state) { |
| 1161 | case SKD_DRVR_STATE_ONLINE: |
| 1162 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 1163 | break; |
| 1164 | |
| 1165 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1166 | dev_dbg(&skdev->pdev->dev, "drive not online\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1167 | rc = -ENXIO; |
| 1168 | goto out; |
| 1169 | } |
| 1170 | |
Akhil Bhansali | f721bb0 | 2013-10-23 13:00:08 +0100 | [diff] [blame] | 1171 | rc = skd_sg_io_get_and_check_args(skdev, &sksgio); |
| 1172 | if (rc) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1173 | goto out; |
| 1174 | |
Akhil Bhansali | f721bb0 | 2013-10-23 13:00:08 +0100 | [diff] [blame] | 1175 | rc = skd_sg_io_obtain_skspcl(skdev, &sksgio); |
| 1176 | if (rc) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1177 | goto out; |
| 1178 | |
Akhil Bhansali | f721bb0 | 2013-10-23 13:00:08 +0100 | [diff] [blame] | 1179 | rc = skd_sg_io_prep_buffering(skdev, &sksgio); |
| 1180 | if (rc) |
| 1181 | goto out; |
| 1182 | |
| 1183 | rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV); |
| 1184 | if (rc) |
| 1185 | goto out; |
| 1186 | |
| 1187 | rc = skd_sg_io_send_fitmsg(skdev, &sksgio); |
| 1188 | if (rc) |
| 1189 | goto out; |
| 1190 | |
| 1191 | rc = skd_sg_io_await(skdev, &sksgio); |
| 1192 | if (rc) |
| 1193 | goto out; |
| 1194 | |
| 1195 | rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV); |
| 1196 | if (rc) |
| 1197 | goto out; |
| 1198 | |
| 1199 | rc = skd_sg_io_put_status(skdev, &sksgio); |
| 1200 | if (rc) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1201 | goto out; |
| 1202 | |
| 1203 | rc = 0; |
| 1204 | |
| 1205 | out: |
| 1206 | skd_sg_io_release_skspcl(skdev, &sksgio); |
| 1207 | |
| 1208 | if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov) |
| 1209 | kfree(sksgio.iov); |
| 1210 | return rc; |
| 1211 | } |
| 1212 | |
| 1213 | static int skd_sg_io_get_and_check_args(struct skd_device *skdev, |
| 1214 | struct skd_sg_io *sksgio) |
| 1215 | { |
| 1216 | struct sg_io_hdr *sgp = &sksgio->sg; |
Bart Van Assche | 95895e1 | 2017-08-17 13:12:55 -0700 | [diff] [blame] | 1217 | int i, __maybe_unused acc; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1218 | |
| 1219 | if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1220 | dev_dbg(&skdev->pdev->dev, "access sg failed %p\n", |
| 1221 | sksgio->argp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1222 | return -EFAULT; |
| 1223 | } |
| 1224 | |
| 1225 | if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1226 | dev_dbg(&skdev->pdev->dev, "copy_from_user sg failed %p\n", |
| 1227 | sksgio->argp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1228 | return -EFAULT; |
| 1229 | } |
| 1230 | |
| 1231 | if (sgp->interface_id != SG_INTERFACE_ID_ORIG) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1232 | dev_dbg(&skdev->pdev->dev, "interface_id invalid 0x%x\n", |
| 1233 | sgp->interface_id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | |
| 1237 | if (sgp->cmd_len > sizeof(sksgio->cdb)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1238 | dev_dbg(&skdev->pdev->dev, "cmd_len invalid %d\n", |
| 1239 | sgp->cmd_len); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1240 | return -EINVAL; |
| 1241 | } |
| 1242 | |
| 1243 | if (sgp->iovec_count > 256) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1244 | dev_dbg(&skdev->pdev->dev, "iovec_count invalid %d\n", |
| 1245 | sgp->iovec_count); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | |
| 1249 | if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1250 | dev_dbg(&skdev->pdev->dev, "dxfer_len invalid %d\n", |
| 1251 | sgp->dxfer_len); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | |
| 1255 | switch (sgp->dxfer_direction) { |
| 1256 | case SG_DXFER_NONE: |
| 1257 | acc = -1; |
| 1258 | break; |
| 1259 | |
| 1260 | case SG_DXFER_TO_DEV: |
| 1261 | acc = VERIFY_READ; |
| 1262 | break; |
| 1263 | |
| 1264 | case SG_DXFER_FROM_DEV: |
| 1265 | case SG_DXFER_TO_FROM_DEV: |
| 1266 | acc = VERIFY_WRITE; |
| 1267 | break; |
| 1268 | |
| 1269 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1270 | dev_dbg(&skdev->pdev->dev, "dxfer_dir invalid %d\n", |
| 1271 | sgp->dxfer_direction); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1272 | return -EINVAL; |
| 1273 | } |
| 1274 | |
| 1275 | if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1276 | dev_dbg(&skdev->pdev->dev, "copy_from_user cmdp failed %p\n", |
| 1277 | sgp->cmdp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1278 | return -EFAULT; |
| 1279 | } |
| 1280 | |
| 1281 | if (sgp->mx_sb_len != 0) { |
| 1282 | if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1283 | dev_dbg(&skdev->pdev->dev, "access sbp failed %p\n", |
| 1284 | sgp->sbp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1285 | return -EFAULT; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | if (sgp->iovec_count == 0) { |
| 1290 | sksgio->iov[0].iov_base = sgp->dxferp; |
| 1291 | sksgio->iov[0].iov_len = sgp->dxfer_len; |
| 1292 | sksgio->iovcnt = 1; |
| 1293 | sksgio->dxfer_len = sgp->dxfer_len; |
| 1294 | } else { |
| 1295 | struct sg_iovec *iov; |
| 1296 | uint nbytes = sizeof(*iov) * sgp->iovec_count; |
| 1297 | size_t iov_data_len; |
| 1298 | |
| 1299 | iov = kmalloc(nbytes, GFP_KERNEL); |
| 1300 | if (iov == NULL) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1301 | dev_dbg(&skdev->pdev->dev, "alloc iovec failed %d\n", |
| 1302 | sgp->iovec_count); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1303 | return -ENOMEM; |
| 1304 | } |
| 1305 | sksgio->iov = iov; |
| 1306 | sksgio->iovcnt = sgp->iovec_count; |
| 1307 | |
| 1308 | if (copy_from_user(iov, sgp->dxferp, nbytes)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1309 | dev_dbg(&skdev->pdev->dev, |
| 1310 | "copy_from_user iovec failed %p\n", |
| 1311 | sgp->dxferp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1312 | return -EFAULT; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * Sum up the vecs, making sure they don't overflow |
| 1317 | */ |
| 1318 | iov_data_len = 0; |
| 1319 | for (i = 0; i < sgp->iovec_count; i++) { |
| 1320 | if (iov_data_len + iov[i].iov_len < iov_data_len) |
| 1321 | return -EINVAL; |
| 1322 | iov_data_len += iov[i].iov_len; |
| 1323 | } |
| 1324 | |
| 1325 | /* SG_IO howto says that the shorter of the two wins */ |
| 1326 | if (sgp->dxfer_len < iov_data_len) { |
| 1327 | sksgio->iovcnt = iov_shorten((struct iovec *)iov, |
| 1328 | sgp->iovec_count, |
| 1329 | sgp->dxfer_len); |
| 1330 | sksgio->dxfer_len = sgp->dxfer_len; |
| 1331 | } else |
| 1332 | sksgio->dxfer_len = iov_data_len; |
| 1333 | } |
| 1334 | |
| 1335 | if (sgp->dxfer_direction != SG_DXFER_NONE) { |
| 1336 | struct sg_iovec *iov = sksgio->iov; |
| 1337 | for (i = 0; i < sksgio->iovcnt; i++, iov++) { |
| 1338 | if (!access_ok(acc, iov->iov_base, iov->iov_len)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1339 | dev_dbg(&skdev->pdev->dev, |
| 1340 | "access data failed %p/%zd\n", |
| 1341 | iov->iov_base, iov->iov_len); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1342 | return -EFAULT; |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | static int skd_sg_io_obtain_skspcl(struct skd_device *skdev, |
| 1351 | struct skd_sg_io *sksgio) |
| 1352 | { |
| 1353 | struct skd_special_context *skspcl = NULL; |
| 1354 | int rc; |
| 1355 | |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 1356 | for (;;) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1357 | ulong flags; |
| 1358 | |
| 1359 | spin_lock_irqsave(&skdev->lock, flags); |
| 1360 | skspcl = skdev->skspcl_free_list; |
| 1361 | if (skspcl != NULL) { |
| 1362 | skdev->skspcl_free_list = |
| 1363 | (struct skd_special_context *)skspcl->req.next; |
| 1364 | skspcl->req.id += SKD_ID_INCR; |
| 1365 | skspcl->req.state = SKD_REQ_STATE_SETUP; |
| 1366 | skspcl->orphaned = 0; |
| 1367 | skspcl->req.n_sg = 0; |
| 1368 | } |
| 1369 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 1370 | |
| 1371 | if (skspcl != NULL) { |
| 1372 | rc = 0; |
| 1373 | break; |
| 1374 | } |
| 1375 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1376 | dev_dbg(&skdev->pdev->dev, "blocking\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1377 | |
| 1378 | rc = wait_event_interruptible_timeout( |
| 1379 | skdev->waitq, |
| 1380 | (skdev->skspcl_free_list != NULL), |
| 1381 | msecs_to_jiffies(sksgio->sg.timeout)); |
| 1382 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1383 | dev_dbg(&skdev->pdev->dev, "unblocking, rc=%d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1384 | |
| 1385 | if (rc <= 0) { |
| 1386 | if (rc == 0) |
| 1387 | rc = -ETIMEDOUT; |
| 1388 | else |
| 1389 | rc = -EINTR; |
| 1390 | break; |
| 1391 | } |
| 1392 | /* |
| 1393 | * If we get here rc > 0 meaning the timeout to |
| 1394 | * wait_event_interruptible_timeout() had time left, hence the |
| 1395 | * sought event -- non-empty free list -- happened. |
| 1396 | * Retry the allocation. |
| 1397 | */ |
| 1398 | } |
| 1399 | sksgio->skspcl = skspcl; |
| 1400 | |
| 1401 | return rc; |
| 1402 | } |
| 1403 | |
| 1404 | static int skd_skreq_prep_buffering(struct skd_device *skdev, |
| 1405 | struct skd_request_context *skreq, |
| 1406 | u32 dxfer_len) |
| 1407 | { |
| 1408 | u32 resid = dxfer_len; |
| 1409 | |
| 1410 | /* |
| 1411 | * The DMA engine must have aligned addresses and byte counts. |
| 1412 | */ |
| 1413 | resid += (-resid) & 3; |
| 1414 | skreq->sg_byte_count = resid; |
| 1415 | |
| 1416 | skreq->n_sg = 0; |
| 1417 | |
| 1418 | while (resid > 0) { |
| 1419 | u32 nbytes = PAGE_SIZE; |
| 1420 | u32 ix = skreq->n_sg; |
| 1421 | struct scatterlist *sg = &skreq->sg[ix]; |
| 1422 | struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix]; |
| 1423 | struct page *page; |
| 1424 | |
| 1425 | if (nbytes > resid) |
| 1426 | nbytes = resid; |
| 1427 | |
| 1428 | page = alloc_page(GFP_KERNEL); |
| 1429 | if (page == NULL) |
| 1430 | return -ENOMEM; |
| 1431 | |
| 1432 | sg_set_page(sg, page, nbytes, 0); |
| 1433 | |
| 1434 | /* TODO: This should be going through a pci_???() |
| 1435 | * routine to do proper mapping. */ |
| 1436 | sksg->control = FIT_SGD_CONTROL_NOT_LAST; |
| 1437 | sksg->byte_count = nbytes; |
| 1438 | |
| 1439 | sksg->host_side_addr = sg_phys(sg); |
| 1440 | |
| 1441 | sksg->dev_side_addr = 0; |
| 1442 | sksg->next_desc_ptr = skreq->sksg_dma_address + |
| 1443 | (ix + 1) * sizeof(*sksg); |
| 1444 | |
| 1445 | skreq->n_sg++; |
| 1446 | resid -= nbytes; |
| 1447 | } |
| 1448 | |
| 1449 | if (skreq->n_sg > 0) { |
| 1450 | u32 ix = skreq->n_sg - 1; |
| 1451 | struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix]; |
| 1452 | |
| 1453 | sksg->control = FIT_SGD_CONTROL_LAST; |
| 1454 | sksg->next_desc_ptr = 0; |
| 1455 | } |
| 1456 | |
| 1457 | if (unlikely(skdev->dbg_level > 1)) { |
| 1458 | u32 i; |
| 1459 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1460 | dev_dbg(&skdev->pdev->dev, |
| 1461 | "skreq=%x sksg_list=%p sksg_dma=%llx\n", |
| 1462 | skreq->id, skreq->sksg_list, skreq->sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1463 | for (i = 0; i < skreq->n_sg; i++) { |
| 1464 | struct fit_sg_descriptor *sgd = &skreq->sksg_list[i]; |
| 1465 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1466 | dev_dbg(&skdev->pdev->dev, |
| 1467 | " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n", |
| 1468 | i, sgd->byte_count, sgd->control, |
| 1469 | sgd->host_side_addr, sgd->next_desc_ptr); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | static int skd_sg_io_prep_buffering(struct skd_device *skdev, |
| 1477 | struct skd_sg_io *sksgio) |
| 1478 | { |
| 1479 | struct skd_special_context *skspcl = sksgio->skspcl; |
| 1480 | struct skd_request_context *skreq = &skspcl->req; |
| 1481 | u32 dxfer_len = sksgio->dxfer_len; |
| 1482 | int rc; |
| 1483 | |
| 1484 | rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len); |
| 1485 | /* |
| 1486 | * Eventually, errors or not, skd_release_special() is called |
| 1487 | * to recover allocations including partial allocations. |
| 1488 | */ |
| 1489 | return rc; |
| 1490 | } |
| 1491 | |
| 1492 | static int skd_sg_io_copy_buffer(struct skd_device *skdev, |
| 1493 | struct skd_sg_io *sksgio, int dxfer_dir) |
| 1494 | { |
| 1495 | struct skd_special_context *skspcl = sksgio->skspcl; |
| 1496 | u32 iov_ix = 0; |
| 1497 | struct sg_iovec curiov; |
| 1498 | u32 sksg_ix = 0; |
| 1499 | u8 *bufp = NULL; |
| 1500 | u32 buf_len = 0; |
| 1501 | u32 resid = sksgio->dxfer_len; |
| 1502 | int rc; |
| 1503 | |
| 1504 | curiov.iov_len = 0; |
| 1505 | curiov.iov_base = NULL; |
| 1506 | |
| 1507 | if (dxfer_dir != sksgio->sg.dxfer_direction) { |
| 1508 | if (dxfer_dir != SG_DXFER_TO_DEV || |
| 1509 | sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV) |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | while (resid > 0) { |
| 1514 | u32 nbytes = PAGE_SIZE; |
| 1515 | |
| 1516 | if (curiov.iov_len == 0) { |
| 1517 | curiov = sksgio->iov[iov_ix++]; |
| 1518 | continue; |
| 1519 | } |
| 1520 | |
| 1521 | if (buf_len == 0) { |
| 1522 | struct page *page; |
| 1523 | page = sg_page(&skspcl->req.sg[sksg_ix++]); |
| 1524 | bufp = page_address(page); |
| 1525 | buf_len = PAGE_SIZE; |
| 1526 | } |
| 1527 | |
| 1528 | nbytes = min_t(u32, nbytes, resid); |
| 1529 | nbytes = min_t(u32, nbytes, curiov.iov_len); |
| 1530 | nbytes = min_t(u32, nbytes, buf_len); |
| 1531 | |
| 1532 | if (dxfer_dir == SG_DXFER_TO_DEV) |
| 1533 | rc = __copy_from_user(bufp, curiov.iov_base, nbytes); |
| 1534 | else |
| 1535 | rc = __copy_to_user(curiov.iov_base, bufp, nbytes); |
| 1536 | |
| 1537 | if (rc) |
| 1538 | return -EFAULT; |
| 1539 | |
| 1540 | resid -= nbytes; |
| 1541 | curiov.iov_len -= nbytes; |
| 1542 | curiov.iov_base += nbytes; |
| 1543 | buf_len -= nbytes; |
| 1544 | } |
| 1545 | |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | static int skd_sg_io_send_fitmsg(struct skd_device *skdev, |
| 1550 | struct skd_sg_io *sksgio) |
| 1551 | { |
| 1552 | struct skd_special_context *skspcl = sksgio->skspcl; |
| 1553 | struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf; |
| 1554 | struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1]; |
| 1555 | |
| 1556 | memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES); |
| 1557 | |
| 1558 | /* Initialize the FIT msg header */ |
| 1559 | fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT; |
| 1560 | fmh->num_protocol_cmds_coalesced = 1; |
| 1561 | |
| 1562 | /* Initialize the SCSI request */ |
| 1563 | if (sksgio->sg.dxfer_direction != SG_DXFER_NONE) |
| 1564 | scsi_req->hdr.sg_list_dma_address = |
| 1565 | cpu_to_be64(skspcl->req.sksg_dma_address); |
| 1566 | scsi_req->hdr.tag = skspcl->req.id; |
| 1567 | scsi_req->hdr.sg_list_len_bytes = |
| 1568 | cpu_to_be32(skspcl->req.sg_byte_count); |
| 1569 | memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb)); |
| 1570 | |
| 1571 | skspcl->req.state = SKD_REQ_STATE_BUSY; |
| 1572 | skd_send_special_fitmsg(skdev, skspcl); |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio) |
| 1578 | { |
| 1579 | unsigned long flags; |
| 1580 | int rc; |
| 1581 | |
| 1582 | rc = wait_event_interruptible_timeout(skdev->waitq, |
| 1583 | (sksgio->skspcl->req.state != |
| 1584 | SKD_REQ_STATE_BUSY), |
| 1585 | msecs_to_jiffies(sksgio->sg. |
| 1586 | timeout)); |
| 1587 | |
| 1588 | spin_lock_irqsave(&skdev->lock, flags); |
| 1589 | |
| 1590 | if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1591 | dev_dbg(&skdev->pdev->dev, "skspcl %p aborted\n", |
| 1592 | sksgio->skspcl); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1593 | |
| 1594 | /* Build check cond, sense and let command finish. */ |
| 1595 | /* For a timeout, we must fabricate completion and sense |
| 1596 | * data to complete the command */ |
| 1597 | sksgio->skspcl->req.completion.status = |
| 1598 | SAM_STAT_CHECK_CONDITION; |
| 1599 | |
| 1600 | memset(&sksgio->skspcl->req.err_info, 0, |
| 1601 | sizeof(sksgio->skspcl->req.err_info)); |
| 1602 | sksgio->skspcl->req.err_info.type = 0x70; |
| 1603 | sksgio->skspcl->req.err_info.key = ABORTED_COMMAND; |
| 1604 | sksgio->skspcl->req.err_info.code = 0x44; |
| 1605 | sksgio->skspcl->req.err_info.qual = 0; |
| 1606 | rc = 0; |
| 1607 | } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY) |
| 1608 | /* No longer on the adapter. We finish. */ |
| 1609 | rc = 0; |
| 1610 | else { |
| 1611 | /* Something's gone wrong. Still busy. Timeout or |
| 1612 | * user interrupted (control-C). Mark as an orphan |
| 1613 | * so it will be disposed when completed. */ |
| 1614 | sksgio->skspcl->orphaned = 1; |
| 1615 | sksgio->skspcl = NULL; |
| 1616 | if (rc == 0) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1617 | dev_dbg(&skdev->pdev->dev, "timed out %p (%u ms)\n", |
| 1618 | sksgio, sksgio->sg.timeout); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1619 | rc = -ETIMEDOUT; |
| 1620 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1621 | dev_dbg(&skdev->pdev->dev, "cntlc %p\n", sksgio); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1622 | rc = -EINTR; |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 1627 | |
| 1628 | return rc; |
| 1629 | } |
| 1630 | |
| 1631 | static int skd_sg_io_put_status(struct skd_device *skdev, |
| 1632 | struct skd_sg_io *sksgio) |
| 1633 | { |
| 1634 | struct sg_io_hdr *sgp = &sksgio->sg; |
| 1635 | struct skd_special_context *skspcl = sksgio->skspcl; |
| 1636 | int resid = 0; |
| 1637 | |
| 1638 | u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes); |
| 1639 | |
| 1640 | sgp->status = skspcl->req.completion.status; |
| 1641 | resid = sksgio->dxfer_len - nb; |
| 1642 | |
| 1643 | sgp->masked_status = sgp->status & STATUS_MASK; |
| 1644 | sgp->msg_status = 0; |
| 1645 | sgp->host_status = 0; |
| 1646 | sgp->driver_status = 0; |
| 1647 | sgp->resid = resid; |
| 1648 | if (sgp->masked_status || sgp->host_status || sgp->driver_status) |
| 1649 | sgp->info |= SG_INFO_CHECK; |
| 1650 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1651 | dev_dbg(&skdev->pdev->dev, "status %x masked %x resid 0x%x\n", |
| 1652 | sgp->status, sgp->masked_status, sgp->resid); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1653 | |
| 1654 | if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) { |
| 1655 | if (sgp->mx_sb_len > 0) { |
| 1656 | struct fit_comp_error_info *ei = &skspcl->req.err_info; |
| 1657 | u32 nbytes = sizeof(*ei); |
| 1658 | |
| 1659 | nbytes = min_t(u32, nbytes, sgp->mx_sb_len); |
| 1660 | |
| 1661 | sgp->sb_len_wr = nbytes; |
| 1662 | |
| 1663 | if (__copy_to_user(sgp->sbp, ei, nbytes)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1664 | dev_dbg(&skdev->pdev->dev, |
| 1665 | "copy_to_user sense failed %p\n", |
| 1666 | sgp->sbp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1667 | return -EFAULT; |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1673 | dev_dbg(&skdev->pdev->dev, "copy_to_user sg failed %p\n", |
| 1674 | sksgio->argp); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1675 | return -EFAULT; |
| 1676 | } |
| 1677 | |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | static int skd_sg_io_release_skspcl(struct skd_device *skdev, |
| 1682 | struct skd_sg_io *sksgio) |
| 1683 | { |
| 1684 | struct skd_special_context *skspcl = sksgio->skspcl; |
| 1685 | |
| 1686 | if (skspcl != NULL) { |
| 1687 | ulong flags; |
| 1688 | |
| 1689 | sksgio->skspcl = NULL; |
| 1690 | |
| 1691 | spin_lock_irqsave(&skdev->lock, flags); |
| 1692 | skd_release_special(skdev, skspcl); |
| 1693 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 1694 | } |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | ***************************************************************************** |
| 1701 | * INTERNAL REQUESTS -- generated by driver itself |
| 1702 | ***************************************************************************** |
| 1703 | */ |
| 1704 | |
| 1705 | static int skd_format_internal_skspcl(struct skd_device *skdev) |
| 1706 | { |
| 1707 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
| 1708 | struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0]; |
| 1709 | struct fit_msg_hdr *fmh; |
| 1710 | uint64_t dma_address; |
| 1711 | struct skd_scsi_request *scsi; |
| 1712 | |
| 1713 | fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0]; |
| 1714 | fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT; |
| 1715 | fmh->num_protocol_cmds_coalesced = 1; |
| 1716 | |
| 1717 | scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64]; |
| 1718 | memset(scsi, 0, sizeof(*scsi)); |
| 1719 | dma_address = skspcl->req.sksg_dma_address; |
| 1720 | scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address); |
| 1721 | sgd->control = FIT_SGD_CONTROL_LAST; |
| 1722 | sgd->byte_count = 0; |
| 1723 | sgd->host_side_addr = skspcl->db_dma_address; |
| 1724 | sgd->dev_side_addr = 0; |
| 1725 | sgd->next_desc_ptr = 0LL; |
| 1726 | |
| 1727 | return 1; |
| 1728 | } |
| 1729 | |
| 1730 | #define WR_BUF_SIZE SKD_N_INTERNAL_BYTES |
| 1731 | |
| 1732 | static void skd_send_internal_skspcl(struct skd_device *skdev, |
| 1733 | struct skd_special_context *skspcl, |
| 1734 | u8 opcode) |
| 1735 | { |
| 1736 | struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0]; |
| 1737 | struct skd_scsi_request *scsi; |
| 1738 | unsigned char *buf = skspcl->data_buf; |
| 1739 | int i; |
| 1740 | |
| 1741 | if (skspcl->req.state != SKD_REQ_STATE_IDLE) |
| 1742 | /* |
| 1743 | * A refresh is already in progress. |
| 1744 | * Just wait for it to finish. |
| 1745 | */ |
| 1746 | return; |
| 1747 | |
| 1748 | SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0); |
| 1749 | skspcl->req.state = SKD_REQ_STATE_BUSY; |
| 1750 | skspcl->req.id += SKD_ID_INCR; |
| 1751 | |
| 1752 | scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64]; |
| 1753 | scsi->hdr.tag = skspcl->req.id; |
| 1754 | |
| 1755 | memset(scsi->cdb, 0, sizeof(scsi->cdb)); |
| 1756 | |
| 1757 | switch (opcode) { |
| 1758 | case TEST_UNIT_READY: |
| 1759 | scsi->cdb[0] = TEST_UNIT_READY; |
| 1760 | sgd->byte_count = 0; |
| 1761 | scsi->hdr.sg_list_len_bytes = 0; |
| 1762 | break; |
| 1763 | |
| 1764 | case READ_CAPACITY: |
| 1765 | scsi->cdb[0] = READ_CAPACITY; |
| 1766 | sgd->byte_count = SKD_N_READ_CAP_BYTES; |
| 1767 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 1768 | break; |
| 1769 | |
| 1770 | case INQUIRY: |
| 1771 | scsi->cdb[0] = INQUIRY; |
| 1772 | scsi->cdb[1] = 0x01; /* evpd */ |
| 1773 | scsi->cdb[2] = 0x80; /* serial number page */ |
| 1774 | scsi->cdb[4] = 0x10; |
| 1775 | sgd->byte_count = 16; |
| 1776 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 1777 | break; |
| 1778 | |
| 1779 | case SYNCHRONIZE_CACHE: |
| 1780 | scsi->cdb[0] = SYNCHRONIZE_CACHE; |
| 1781 | sgd->byte_count = 0; |
| 1782 | scsi->hdr.sg_list_len_bytes = 0; |
| 1783 | break; |
| 1784 | |
| 1785 | case WRITE_BUFFER: |
| 1786 | scsi->cdb[0] = WRITE_BUFFER; |
| 1787 | scsi->cdb[1] = 0x02; |
| 1788 | scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8; |
| 1789 | scsi->cdb[8] = WR_BUF_SIZE & 0xFF; |
| 1790 | sgd->byte_count = WR_BUF_SIZE; |
| 1791 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 1792 | /* fill incrementing byte pattern */ |
| 1793 | for (i = 0; i < sgd->byte_count; i++) |
| 1794 | buf[i] = i & 0xFF; |
| 1795 | break; |
| 1796 | |
| 1797 | case READ_BUFFER: |
| 1798 | scsi->cdb[0] = READ_BUFFER; |
| 1799 | scsi->cdb[1] = 0x02; |
| 1800 | scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8; |
| 1801 | scsi->cdb[8] = WR_BUF_SIZE & 0xFF; |
| 1802 | sgd->byte_count = WR_BUF_SIZE; |
| 1803 | scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count); |
| 1804 | memset(skspcl->data_buf, 0, sgd->byte_count); |
| 1805 | break; |
| 1806 | |
| 1807 | default: |
| 1808 | SKD_ASSERT("Don't know what to send"); |
| 1809 | return; |
| 1810 | |
| 1811 | } |
| 1812 | skd_send_special_fitmsg(skdev, skspcl); |
| 1813 | } |
| 1814 | |
| 1815 | static void skd_refresh_device_data(struct skd_device *skdev) |
| 1816 | { |
| 1817 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
| 1818 | |
| 1819 | skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY); |
| 1820 | } |
| 1821 | |
| 1822 | static int skd_chk_read_buf(struct skd_device *skdev, |
| 1823 | struct skd_special_context *skspcl) |
| 1824 | { |
| 1825 | unsigned char *buf = skspcl->data_buf; |
| 1826 | int i; |
| 1827 | |
| 1828 | /* check for incrementing byte pattern */ |
| 1829 | for (i = 0; i < WR_BUF_SIZE; i++) |
| 1830 | if (buf[i] != (i & 0xFF)) |
| 1831 | return 1; |
| 1832 | |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key, |
| 1837 | u8 code, u8 qual, u8 fruc) |
| 1838 | { |
| 1839 | /* If the check condition is of special interest, log a message */ |
| 1840 | if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02) |
| 1841 | && (code == 0x04) && (qual == 0x06)) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1842 | dev_err(&skdev->pdev->dev, |
| 1843 | "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n", |
| 1844 | key, code, qual, fruc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | static void skd_complete_internal(struct skd_device *skdev, |
| 1849 | volatile struct fit_completion_entry_v1 |
| 1850 | *skcomp, |
| 1851 | volatile struct fit_comp_error_info *skerr, |
| 1852 | struct skd_special_context *skspcl) |
| 1853 | { |
| 1854 | u8 *buf = skspcl->data_buf; |
| 1855 | u8 status; |
| 1856 | int i; |
| 1857 | struct skd_scsi_request *scsi = |
| 1858 | (struct skd_scsi_request *)&skspcl->msg_buf[64]; |
| 1859 | |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 1860 | lockdep_assert_held(&skdev->lock); |
| 1861 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1862 | SKD_ASSERT(skspcl == &skdev->internal_skspcl); |
| 1863 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1864 | dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1865 | |
| 1866 | skspcl->req.completion = *skcomp; |
| 1867 | skspcl->req.state = SKD_REQ_STATE_IDLE; |
| 1868 | skspcl->req.id += SKD_ID_INCR; |
| 1869 | |
| 1870 | status = skspcl->req.completion.status; |
| 1871 | |
| 1872 | skd_log_check_status(skdev, status, skerr->key, skerr->code, |
| 1873 | skerr->qual, skerr->fruc); |
| 1874 | |
| 1875 | switch (scsi->cdb[0]) { |
| 1876 | case TEST_UNIT_READY: |
| 1877 | if (status == SAM_STAT_GOOD) |
| 1878 | skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER); |
| 1879 | else if ((status == SAM_STAT_CHECK_CONDITION) && |
| 1880 | (skerr->key == MEDIUM_ERROR)) |
| 1881 | skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER); |
| 1882 | else { |
| 1883 | if (skdev->state == SKD_DRVR_STATE_STOPPING) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1884 | dev_dbg(&skdev->pdev->dev, |
| 1885 | "TUR failed, don't send anymore state 0x%x\n", |
| 1886 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1887 | return; |
| 1888 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1889 | dev_dbg(&skdev->pdev->dev, |
| 1890 | "**** TUR failed, retry skerr\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1891 | skd_send_internal_skspcl(skdev, skspcl, 0x00); |
| 1892 | } |
| 1893 | break; |
| 1894 | |
| 1895 | case WRITE_BUFFER: |
| 1896 | if (status == SAM_STAT_GOOD) |
| 1897 | skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER); |
| 1898 | else { |
| 1899 | if (skdev->state == SKD_DRVR_STATE_STOPPING) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1900 | dev_dbg(&skdev->pdev->dev, |
| 1901 | "write buffer failed, don't send anymore state 0x%x\n", |
| 1902 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1903 | return; |
| 1904 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1905 | dev_dbg(&skdev->pdev->dev, |
| 1906 | "**** write buffer failed, retry skerr\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1907 | skd_send_internal_skspcl(skdev, skspcl, 0x00); |
| 1908 | } |
| 1909 | break; |
| 1910 | |
| 1911 | case READ_BUFFER: |
| 1912 | if (status == SAM_STAT_GOOD) { |
| 1913 | if (skd_chk_read_buf(skdev, skspcl) == 0) |
| 1914 | skd_send_internal_skspcl(skdev, skspcl, |
| 1915 | READ_CAPACITY); |
| 1916 | else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1917 | dev_err(&skdev->pdev->dev, |
| 1918 | "*** W/R Buffer mismatch %d ***\n", |
| 1919 | skdev->connect_retries); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1920 | if (skdev->connect_retries < |
| 1921 | SKD_MAX_CONNECT_RETRIES) { |
| 1922 | skdev->connect_retries++; |
| 1923 | skd_soft_reset(skdev); |
| 1924 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1925 | dev_err(&skdev->pdev->dev, |
| 1926 | "W/R Buffer Connect Error\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1927 | return; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | } else { |
| 1932 | if (skdev->state == SKD_DRVR_STATE_STOPPING) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1933 | dev_dbg(&skdev->pdev->dev, |
| 1934 | "read buffer failed, don't send anymore state 0x%x\n", |
| 1935 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1936 | return; |
| 1937 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1938 | dev_dbg(&skdev->pdev->dev, |
| 1939 | "**** read buffer failed, retry skerr\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1940 | skd_send_internal_skspcl(skdev, skspcl, 0x00); |
| 1941 | } |
| 1942 | break; |
| 1943 | |
| 1944 | case READ_CAPACITY: |
| 1945 | skdev->read_cap_is_valid = 0; |
| 1946 | if (status == SAM_STAT_GOOD) { |
| 1947 | skdev->read_cap_last_lba = |
| 1948 | (buf[0] << 24) | (buf[1] << 16) | |
| 1949 | (buf[2] << 8) | buf[3]; |
| 1950 | skdev->read_cap_blocksize = |
| 1951 | (buf[4] << 24) | (buf[5] << 16) | |
| 1952 | (buf[6] << 8) | buf[7]; |
| 1953 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1954 | dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n", |
| 1955 | skdev->read_cap_last_lba, |
| 1956 | skdev->read_cap_blocksize); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1957 | |
| 1958 | set_capacity(skdev->disk, skdev->read_cap_last_lba + 1); |
| 1959 | |
| 1960 | skdev->read_cap_is_valid = 1; |
| 1961 | |
| 1962 | skd_send_internal_skspcl(skdev, skspcl, INQUIRY); |
| 1963 | } else if ((status == SAM_STAT_CHECK_CONDITION) && |
| 1964 | (skerr->key == MEDIUM_ERROR)) { |
| 1965 | skdev->read_cap_last_lba = ~0; |
| 1966 | set_capacity(skdev->disk, skdev->read_cap_last_lba + 1); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1967 | 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] | 1968 | skd_send_internal_skspcl(skdev, skspcl, INQUIRY); |
| 1969 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1970 | dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1971 | skd_send_internal_skspcl(skdev, skspcl, |
| 1972 | TEST_UNIT_READY); |
| 1973 | } |
| 1974 | break; |
| 1975 | |
| 1976 | case INQUIRY: |
| 1977 | skdev->inquiry_is_valid = 0; |
| 1978 | if (status == SAM_STAT_GOOD) { |
| 1979 | skdev->inquiry_is_valid = 1; |
| 1980 | |
| 1981 | for (i = 0; i < 12; i++) |
| 1982 | skdev->inq_serial_num[i] = buf[i + 4]; |
| 1983 | skdev->inq_serial_num[12] = 0; |
| 1984 | } |
| 1985 | |
| 1986 | if (skd_unquiesce_dev(skdev) < 0) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 1987 | dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 1988 | /* connection is complete */ |
| 1989 | skdev->connect_retries = 0; |
| 1990 | break; |
| 1991 | |
| 1992 | case SYNCHRONIZE_CACHE: |
| 1993 | if (status == SAM_STAT_GOOD) |
| 1994 | skdev->sync_done = 1; |
| 1995 | else |
| 1996 | skdev->sync_done = -1; |
| 1997 | wake_up_interruptible(&skdev->waitq); |
| 1998 | break; |
| 1999 | |
| 2000 | default: |
| 2001 | SKD_ASSERT("we didn't send this"); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | /* |
| 2006 | ***************************************************************************** |
| 2007 | * FIT MESSAGES |
| 2008 | ***************************************************************************** |
| 2009 | */ |
| 2010 | |
| 2011 | static void skd_send_fitmsg(struct skd_device *skdev, |
| 2012 | struct skd_fitmsg_context *skmsg) |
| 2013 | { |
| 2014 | u64 qcmd; |
| 2015 | struct fit_msg_hdr *fmh; |
| 2016 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2017 | dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n", |
| 2018 | skmsg->mb_dma_address, skdev->in_flight); |
| 2019 | dev_dbg(&skdev->pdev->dev, "msg_buf 0x%p, offset %x\n", skmsg->msg_buf, |
| 2020 | skmsg->offset); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2021 | |
| 2022 | qcmd = skmsg->mb_dma_address; |
| 2023 | qcmd |= FIT_QCMD_QID_NORMAL; |
| 2024 | |
| 2025 | fmh = (struct fit_msg_hdr *)skmsg->msg_buf; |
| 2026 | skmsg->outstanding = fmh->num_protocol_cmds_coalesced; |
| 2027 | |
| 2028 | if (unlikely(skdev->dbg_level > 1)) { |
| 2029 | u8 *bp = (u8 *)skmsg->msg_buf; |
| 2030 | int i; |
| 2031 | for (i = 0; i < skmsg->length; i += 8) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2032 | dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i, |
| 2033 | &bp[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2034 | if (i == 0) |
| 2035 | i = 64 - 8; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | if (skmsg->length > 256) |
| 2040 | qcmd |= FIT_QCMD_MSGSIZE_512; |
| 2041 | else if (skmsg->length > 128) |
| 2042 | qcmd |= FIT_QCMD_MSGSIZE_256; |
| 2043 | else if (skmsg->length > 64) |
| 2044 | qcmd |= FIT_QCMD_MSGSIZE_128; |
| 2045 | else |
| 2046 | /* |
| 2047 | * This makes no sense because the FIT msg header is |
| 2048 | * 64 bytes. If the msg is only 64 bytes long it has |
| 2049 | * no payload. |
| 2050 | */ |
| 2051 | qcmd |= FIT_QCMD_MSGSIZE_64; |
| 2052 | |
Bart Van Assche | 5fbd545 | 2017-08-17 13:12:46 -0700 | [diff] [blame] | 2053 | /* Make sure skd_msg_buf is written before the doorbell is triggered. */ |
| 2054 | smp_wmb(); |
| 2055 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2056 | SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | static void skd_send_special_fitmsg(struct skd_device *skdev, |
| 2060 | struct skd_special_context *skspcl) |
| 2061 | { |
| 2062 | u64 qcmd; |
| 2063 | |
| 2064 | if (unlikely(skdev->dbg_level > 1)) { |
| 2065 | u8 *bp = (u8 *)skspcl->msg_buf; |
| 2066 | int i; |
| 2067 | |
| 2068 | for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2069 | dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i, |
| 2070 | &bp[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2071 | if (i == 0) |
| 2072 | i = 64 - 8; |
| 2073 | } |
| 2074 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2075 | dev_dbg(&skdev->pdev->dev, |
| 2076 | "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n", |
| 2077 | skspcl, skspcl->req.id, skspcl->req.sksg_list, |
| 2078 | skspcl->req.sksg_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2079 | for (i = 0; i < skspcl->req.n_sg; i++) { |
| 2080 | struct fit_sg_descriptor *sgd = |
| 2081 | &skspcl->req.sksg_list[i]; |
| 2082 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2083 | dev_dbg(&skdev->pdev->dev, |
| 2084 | " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n", |
| 2085 | i, sgd->byte_count, sgd->control, |
| 2086 | sgd->host_side_addr, sgd->next_desc_ptr); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr |
| 2092 | * and one 64-byte SSDI command. |
| 2093 | */ |
| 2094 | qcmd = skspcl->mb_dma_address; |
| 2095 | qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128; |
| 2096 | |
Bart Van Assche | 5fbd545 | 2017-08-17 13:12:46 -0700 | [diff] [blame] | 2097 | /* Make sure skd_msg_buf is written before the doorbell is triggered. */ |
| 2098 | smp_wmb(); |
| 2099 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2100 | SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND); |
| 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | ***************************************************************************** |
| 2105 | * COMPLETION QUEUE |
| 2106 | ***************************************************************************** |
| 2107 | */ |
| 2108 | |
| 2109 | static void skd_complete_other(struct skd_device *skdev, |
| 2110 | volatile struct fit_completion_entry_v1 *skcomp, |
| 2111 | volatile struct fit_comp_error_info *skerr); |
| 2112 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2113 | struct sns_info { |
| 2114 | u8 type; |
| 2115 | u8 stat; |
| 2116 | u8 key; |
| 2117 | u8 asc; |
| 2118 | u8 ascq; |
| 2119 | u8 mask; |
| 2120 | enum skd_check_status_action action; |
| 2121 | }; |
| 2122 | |
| 2123 | static struct sns_info skd_chkstat_table[] = { |
| 2124 | /* Good */ |
| 2125 | { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c, |
| 2126 | SKD_CHECK_STATUS_REPORT_GOOD }, |
| 2127 | |
| 2128 | /* Smart alerts */ |
| 2129 | { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */ |
| 2130 | SKD_CHECK_STATUS_REPORT_SMART_ALERT }, |
| 2131 | { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */ |
| 2132 | SKD_CHECK_STATUS_REPORT_SMART_ALERT }, |
| 2133 | { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */ |
| 2134 | SKD_CHECK_STATUS_REPORT_SMART_ALERT }, |
| 2135 | |
| 2136 | /* Retry (with limits) */ |
| 2137 | { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */ |
| 2138 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 2139 | { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */ |
| 2140 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 2141 | { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */ |
| 2142 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 2143 | { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */ |
| 2144 | SKD_CHECK_STATUS_REQUEUE_REQUEST }, |
| 2145 | |
| 2146 | /* Busy (or about to be) */ |
| 2147 | { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */ |
| 2148 | SKD_CHECK_STATUS_BUSY_IMMINENT }, |
| 2149 | }; |
| 2150 | |
| 2151 | /* |
| 2152 | * Look up status and sense data to decide how to handle the error |
| 2153 | * from the device. |
| 2154 | * mask says which fields must match e.g., mask=0x18 means check |
| 2155 | * type and stat, ignore key, asc, ascq. |
| 2156 | */ |
| 2157 | |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 2158 | static enum skd_check_status_action |
| 2159 | skd_check_status(struct skd_device *skdev, |
| 2160 | u8 cmp_status, volatile struct fit_comp_error_info *skerr) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2161 | { |
| 2162 | int i, n; |
| 2163 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2164 | dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n", |
| 2165 | skerr->key, skerr->code, skerr->qual, skerr->fruc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2166 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2167 | dev_dbg(&skdev->pdev->dev, |
| 2168 | "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n", |
| 2169 | skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual, |
| 2170 | skerr->fruc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2171 | |
| 2172 | /* Does the info match an entry in the good category? */ |
| 2173 | n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]); |
| 2174 | for (i = 0; i < n; i++) { |
| 2175 | struct sns_info *sns = &skd_chkstat_table[i]; |
| 2176 | |
| 2177 | if (sns->mask & 0x10) |
| 2178 | if (skerr->type != sns->type) |
| 2179 | continue; |
| 2180 | |
| 2181 | if (sns->mask & 0x08) |
| 2182 | if (cmp_status != sns->stat) |
| 2183 | continue; |
| 2184 | |
| 2185 | if (sns->mask & 0x04) |
| 2186 | if (skerr->key != sns->key) |
| 2187 | continue; |
| 2188 | |
| 2189 | if (sns->mask & 0x02) |
| 2190 | if (skerr->code != sns->asc) |
| 2191 | continue; |
| 2192 | |
| 2193 | if (sns->mask & 0x01) |
| 2194 | if (skerr->qual != sns->ascq) |
| 2195 | continue; |
| 2196 | |
| 2197 | if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2198 | dev_err(&skdev->pdev->dev, |
| 2199 | "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n", |
| 2200 | skerr->key, skerr->code, skerr->qual); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2201 | } |
| 2202 | return sns->action; |
| 2203 | } |
| 2204 | |
| 2205 | /* No other match, so nonzero status means error, |
| 2206 | * zero status means good |
| 2207 | */ |
| 2208 | if (cmp_status) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2209 | dev_dbg(&skdev->pdev->dev, "status check: error\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2210 | return SKD_CHECK_STATUS_REPORT_ERROR; |
| 2211 | } |
| 2212 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2213 | dev_dbg(&skdev->pdev->dev, "status check good default\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2214 | return SKD_CHECK_STATUS_REPORT_GOOD; |
| 2215 | } |
| 2216 | |
| 2217 | static void skd_resolve_req_exception(struct skd_device *skdev, |
| 2218 | struct skd_request_context *skreq) |
| 2219 | { |
| 2220 | u8 cmp_status = skreq->completion.status; |
| 2221 | |
| 2222 | switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) { |
| 2223 | case SKD_CHECK_STATUS_REPORT_GOOD: |
| 2224 | case SKD_CHECK_STATUS_REPORT_SMART_ALERT: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2225 | skd_end_request(skdev, skreq, BLK_STS_OK); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2226 | break; |
| 2227 | |
| 2228 | case SKD_CHECK_STATUS_BUSY_IMMINENT: |
| 2229 | skd_log_skreq(skdev, skreq, "retry(busy)"); |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 2230 | blk_requeue_request(skdev->queue, skreq->req); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2231 | dev_info(&skdev->pdev->dev, "drive BUSY imminent\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2232 | skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT; |
| 2233 | skdev->timer_countdown = SKD_TIMER_MINUTES(20); |
| 2234 | skd_quiesce_dev(skdev); |
| 2235 | break; |
| 2236 | |
| 2237 | case SKD_CHECK_STATUS_REQUEUE_REQUEST: |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 2238 | if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) { |
| 2239 | skd_log_skreq(skdev, skreq, "retry"); |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 2240 | blk_requeue_request(skdev->queue, skreq->req); |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 2241 | break; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2242 | } |
Bart Van Assche | ce6882b | 2017-08-17 13:12:52 -0700 | [diff] [blame] | 2243 | /* fall through */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2244 | |
| 2245 | case SKD_CHECK_STATUS_REPORT_ERROR: |
| 2246 | default: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2247 | skd_end_request(skdev, skreq, BLK_STS_IOERR); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2248 | break; |
| 2249 | } |
| 2250 | } |
| 2251 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2252 | /* assume spinlock is already held */ |
| 2253 | static void skd_release_skreq(struct skd_device *skdev, |
| 2254 | struct skd_request_context *skreq) |
| 2255 | { |
| 2256 | u32 msg_slot; |
| 2257 | struct skd_fitmsg_context *skmsg; |
| 2258 | |
| 2259 | u32 timo_slot; |
| 2260 | |
| 2261 | /* |
| 2262 | * Reclaim the FIT msg buffer if this is |
| 2263 | * the first of the requests it carried to |
| 2264 | * be completed. The FIT msg buffer used to |
| 2265 | * send this request cannot be reused until |
| 2266 | * we are sure the s1120 card has copied |
| 2267 | * it to its memory. The FIT msg might have |
| 2268 | * contained several requests. As soon as |
| 2269 | * any of them are completed we know that |
| 2270 | * the entire FIT msg was transferred. |
| 2271 | * Only the first completed request will |
| 2272 | * match the FIT msg buffer id. The FIT |
| 2273 | * msg buffer id is immediately updated. |
| 2274 | * When subsequent requests complete the FIT |
| 2275 | * msg buffer id won't match, so we know |
| 2276 | * quite cheaply that it is already done. |
| 2277 | */ |
| 2278 | msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK; |
| 2279 | SKD_ASSERT(msg_slot < skdev->num_fitmsg_context); |
| 2280 | |
| 2281 | skmsg = &skdev->skmsg_table[msg_slot]; |
| 2282 | if (skmsg->id == skreq->fitmsg_id) { |
| 2283 | SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY); |
| 2284 | SKD_ASSERT(skmsg->outstanding > 0); |
| 2285 | skmsg->outstanding--; |
| 2286 | if (skmsg->outstanding == 0) { |
| 2287 | skmsg->state = SKD_MSG_STATE_IDLE; |
| 2288 | skmsg->id += SKD_ID_INCR; |
| 2289 | skmsg->next = skdev->skmsg_free_list; |
| 2290 | skdev->skmsg_free_list = skmsg; |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | /* |
| 2295 | * Decrease the number of active requests. |
| 2296 | * Also decrements the count in the timeout slot. |
| 2297 | */ |
| 2298 | SKD_ASSERT(skdev->in_flight > 0); |
| 2299 | skdev->in_flight -= 1; |
| 2300 | |
| 2301 | timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK; |
| 2302 | SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0); |
| 2303 | skdev->timeout_slot[timo_slot] -= 1; |
| 2304 | |
| 2305 | /* |
| 2306 | * Reset backpointer |
| 2307 | */ |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 2308 | skreq->req = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2309 | |
| 2310 | /* |
| 2311 | * Reclaim the skd_request_context |
| 2312 | */ |
| 2313 | skreq->state = SKD_REQ_STATE_IDLE; |
| 2314 | skreq->id += SKD_ID_INCR; |
| 2315 | skreq->next = skdev->skreq_free_list; |
| 2316 | skdev->skreq_free_list = skreq; |
| 2317 | } |
| 2318 | |
| 2319 | #define DRIVER_INQ_EVPD_PAGE_CODE 0xDA |
| 2320 | |
| 2321 | static void skd_do_inq_page_00(struct skd_device *skdev, |
| 2322 | volatile struct fit_completion_entry_v1 *skcomp, |
| 2323 | volatile struct fit_comp_error_info *skerr, |
| 2324 | uint8_t *cdb, uint8_t *buf) |
| 2325 | { |
| 2326 | uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size; |
| 2327 | |
| 2328 | /* Caller requested "supported pages". The driver needs to insert |
| 2329 | * its page. |
| 2330 | */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2331 | dev_dbg(&skdev->pdev->dev, |
| 2332 | "skd_do_driver_inquiry: modify supported pages.\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2333 | |
| 2334 | /* If the device rejected the request because the CDB was |
| 2335 | * improperly formed, then just leave. |
| 2336 | */ |
| 2337 | if (skcomp->status == SAM_STAT_CHECK_CONDITION && |
| 2338 | skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24) |
| 2339 | return; |
| 2340 | |
| 2341 | /* Get the amount of space the caller allocated */ |
| 2342 | max_bytes = (cdb[3] << 8) | cdb[4]; |
| 2343 | |
| 2344 | /* Get the number of pages actually returned by the device */ |
| 2345 | drive_pages = (buf[2] << 8) | buf[3]; |
| 2346 | drive_bytes = drive_pages + 4; |
| 2347 | new_size = drive_pages + 1; |
| 2348 | |
| 2349 | /* Supported pages must be in numerical order, so find where |
| 2350 | * the driver page needs to be inserted into the list of |
| 2351 | * pages returned by the device. |
| 2352 | */ |
| 2353 | for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) { |
| 2354 | if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE) |
| 2355 | return; /* Device using this page code. abort */ |
| 2356 | else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE) |
| 2357 | break; |
| 2358 | } |
| 2359 | |
| 2360 | if (insert_pt < max_bytes) { |
| 2361 | uint16_t u; |
| 2362 | |
| 2363 | /* Shift everything up one byte to make room. */ |
| 2364 | for (u = new_size + 3; u > insert_pt; u--) |
| 2365 | buf[u] = buf[u - 1]; |
| 2366 | buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE; |
| 2367 | |
| 2368 | /* SCSI byte order increment of num_returned_bytes by 1 */ |
| 2369 | skcomp->num_returned_bytes = |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 2370 | cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | /* update page length field to reflect the driver's page too */ |
| 2374 | buf[2] = (uint8_t)((new_size >> 8) & 0xFF); |
| 2375 | buf[3] = (uint8_t)((new_size >> 0) & 0xFF); |
| 2376 | } |
| 2377 | |
| 2378 | static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width) |
| 2379 | { |
| 2380 | int pcie_reg; |
| 2381 | u16 pci_bus_speed; |
| 2382 | u8 pci_lanes; |
| 2383 | |
| 2384 | pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP); |
| 2385 | if (pcie_reg) { |
| 2386 | u16 linksta; |
| 2387 | pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta); |
| 2388 | |
| 2389 | pci_bus_speed = linksta & 0xF; |
| 2390 | pci_lanes = (linksta & 0x3F0) >> 4; |
| 2391 | } else { |
| 2392 | *speed = STEC_LINK_UNKNOWN; |
| 2393 | *width = 0xFF; |
| 2394 | return; |
| 2395 | } |
| 2396 | |
| 2397 | switch (pci_bus_speed) { |
| 2398 | case 1: |
| 2399 | *speed = STEC_LINK_2_5GTS; |
| 2400 | break; |
| 2401 | case 2: |
| 2402 | *speed = STEC_LINK_5GTS; |
| 2403 | break; |
| 2404 | case 3: |
| 2405 | *speed = STEC_LINK_8GTS; |
| 2406 | break; |
| 2407 | default: |
| 2408 | *speed = STEC_LINK_UNKNOWN; |
| 2409 | break; |
| 2410 | } |
| 2411 | |
| 2412 | if (pci_lanes <= 0x20) |
| 2413 | *width = pci_lanes; |
| 2414 | else |
| 2415 | *width = 0xFF; |
| 2416 | } |
| 2417 | |
| 2418 | static void skd_do_inq_page_da(struct skd_device *skdev, |
| 2419 | volatile struct fit_completion_entry_v1 *skcomp, |
| 2420 | volatile struct fit_comp_error_info *skerr, |
| 2421 | uint8_t *cdb, uint8_t *buf) |
| 2422 | { |
Bartlomiej Zolnierkiewicz | fec23f6 | 2013-11-05 12:37:07 +0100 | [diff] [blame] | 2423 | struct pci_dev *pdev = skdev->pdev; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2424 | unsigned max_bytes; |
| 2425 | struct driver_inquiry_data inq; |
| 2426 | u16 val; |
| 2427 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2428 | dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2429 | |
| 2430 | memset(&inq, 0, sizeof(inq)); |
| 2431 | |
| 2432 | inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE; |
| 2433 | |
Bartlomiej Zolnierkiewicz | fec23f6 | 2013-11-05 12:37:07 +0100 | [diff] [blame] | 2434 | skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes); |
| 2435 | inq.pcie_bus_number = cpu_to_be16(pdev->bus->number); |
| 2436 | inq.pcie_device_number = PCI_SLOT(pdev->devfn); |
| 2437 | inq.pcie_function_number = PCI_FUNC(pdev->devfn); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2438 | |
Bartlomiej Zolnierkiewicz | fec23f6 | 2013-11-05 12:37:07 +0100 | [diff] [blame] | 2439 | pci_read_config_word(pdev, PCI_VENDOR_ID, &val); |
| 2440 | inq.pcie_vendor_id = cpu_to_be16(val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2441 | |
Bartlomiej Zolnierkiewicz | fec23f6 | 2013-11-05 12:37:07 +0100 | [diff] [blame] | 2442 | pci_read_config_word(pdev, PCI_DEVICE_ID, &val); |
| 2443 | inq.pcie_device_id = cpu_to_be16(val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2444 | |
Bartlomiej Zolnierkiewicz | fec23f6 | 2013-11-05 12:37:07 +0100 | [diff] [blame] | 2445 | pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val); |
| 2446 | inq.pcie_subsystem_vendor_id = cpu_to_be16(val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2447 | |
Bartlomiej Zolnierkiewicz | fec23f6 | 2013-11-05 12:37:07 +0100 | [diff] [blame] | 2448 | pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val); |
| 2449 | inq.pcie_subsystem_device_id = cpu_to_be16(val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2450 | |
| 2451 | /* Driver version, fixed lenth, padded with spaces on the right */ |
| 2452 | inq.driver_version_length = sizeof(inq.driver_version); |
| 2453 | memset(&inq.driver_version, ' ', sizeof(inq.driver_version)); |
| 2454 | memcpy(inq.driver_version, DRV_VER_COMPL, |
| 2455 | min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL))); |
| 2456 | |
| 2457 | inq.page_length = cpu_to_be16((sizeof(inq) - 4)); |
| 2458 | |
| 2459 | /* Clear the error set by the device */ |
| 2460 | skcomp->status = SAM_STAT_GOOD; |
| 2461 | memset((void *)skerr, 0, sizeof(*skerr)); |
| 2462 | |
| 2463 | /* copy response into output buffer */ |
| 2464 | max_bytes = (cdb[3] << 8) | cdb[4]; |
| 2465 | memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq))); |
| 2466 | |
| 2467 | skcomp->num_returned_bytes = |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 2468 | cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq))); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | static void skd_do_driver_inq(struct skd_device *skdev, |
| 2472 | volatile struct fit_completion_entry_v1 *skcomp, |
| 2473 | volatile struct fit_comp_error_info *skerr, |
| 2474 | uint8_t *cdb, uint8_t *buf) |
| 2475 | { |
| 2476 | if (!buf) |
| 2477 | return; |
| 2478 | else if (cdb[0] != INQUIRY) |
| 2479 | return; /* Not an INQUIRY */ |
| 2480 | else if ((cdb[1] & 1) == 0) |
| 2481 | return; /* EVPD not set */ |
| 2482 | else if (cdb[2] == 0) |
| 2483 | /* Need to add driver's page to supported pages list */ |
| 2484 | skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf); |
| 2485 | else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE) |
| 2486 | /* Caller requested driver's page */ |
| 2487 | skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf); |
| 2488 | } |
| 2489 | |
| 2490 | static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg) |
| 2491 | { |
| 2492 | if (!sg) |
| 2493 | return NULL; |
| 2494 | if (!sg_page(sg)) |
| 2495 | return NULL; |
| 2496 | return sg_virt(sg); |
| 2497 | } |
| 2498 | |
| 2499 | static void skd_process_scsi_inq(struct skd_device *skdev, |
| 2500 | volatile struct fit_completion_entry_v1 |
| 2501 | *skcomp, |
| 2502 | volatile struct fit_comp_error_info *skerr, |
| 2503 | struct skd_special_context *skspcl) |
| 2504 | { |
| 2505 | uint8_t *buf; |
| 2506 | struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf; |
| 2507 | struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1]; |
| 2508 | |
| 2509 | dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg, |
| 2510 | skspcl->req.sg_data_dir); |
| 2511 | buf = skd_sg_1st_page_ptr(skspcl->req.sg); |
| 2512 | |
| 2513 | if (buf) |
| 2514 | skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf); |
| 2515 | } |
| 2516 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2517 | static int skd_isr_completion_posted(struct skd_device *skdev, |
| 2518 | int limit, int *enqueued) |
| 2519 | { |
| 2520 | volatile struct fit_completion_entry_v1 *skcmp = NULL; |
| 2521 | volatile struct fit_comp_error_info *skerr; |
| 2522 | u16 req_id; |
| 2523 | u32 req_slot; |
| 2524 | struct skd_request_context *skreq; |
| 2525 | u16 cmp_cntxt = 0; |
| 2526 | u8 cmp_status = 0; |
| 2527 | u8 cmp_cycle = 0; |
| 2528 | u32 cmp_bytes = 0; |
| 2529 | int rc = 0; |
| 2530 | int processed = 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2531 | |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 2532 | lockdep_assert_held(&skdev->lock); |
| 2533 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2534 | for (;; ) { |
| 2535 | SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY); |
| 2536 | |
| 2537 | skcmp = &skdev->skcomp_table[skdev->skcomp_ix]; |
| 2538 | cmp_cycle = skcmp->cycle; |
| 2539 | cmp_cntxt = skcmp->tag; |
| 2540 | cmp_status = skcmp->status; |
| 2541 | cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes); |
| 2542 | |
| 2543 | skerr = &skdev->skerr_table[skdev->skcomp_ix]; |
| 2544 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2545 | dev_dbg(&skdev->pdev->dev, |
| 2546 | "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n", |
| 2547 | skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle, |
| 2548 | cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes, |
| 2549 | skdev->proto_ver); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2550 | |
| 2551 | if (cmp_cycle != skdev->skcomp_cycle) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2552 | dev_dbg(&skdev->pdev->dev, "end of completions\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2553 | break; |
| 2554 | } |
| 2555 | /* |
| 2556 | * Update the completion queue head index and possibly |
| 2557 | * the completion cycle count. 8-bit wrap-around. |
| 2558 | */ |
| 2559 | skdev->skcomp_ix++; |
| 2560 | if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) { |
| 2561 | skdev->skcomp_ix = 0; |
| 2562 | skdev->skcomp_cycle++; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * The command context is a unique 32-bit ID. The low order |
| 2567 | * bits help locate the request. The request is usually a |
| 2568 | * r/w request (see skd_start() above) or a special request. |
| 2569 | */ |
| 2570 | req_id = cmp_cntxt; |
| 2571 | req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK; |
| 2572 | |
| 2573 | /* Is this other than a r/w request? */ |
| 2574 | if (req_slot >= skdev->num_req_context) { |
| 2575 | /* |
| 2576 | * This is not a completion for a r/w request. |
| 2577 | */ |
| 2578 | skd_complete_other(skdev, skcmp, skerr); |
| 2579 | continue; |
| 2580 | } |
| 2581 | |
| 2582 | skreq = &skdev->skreq_table[req_slot]; |
| 2583 | |
| 2584 | /* |
| 2585 | * Make sure the request ID for the slot matches. |
| 2586 | */ |
| 2587 | if (skreq->id != req_id) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2588 | dev_dbg(&skdev->pdev->dev, |
| 2589 | "mismatch comp_id=0x%x req_id=0x%x\n", req_id, |
| 2590 | skreq->id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2591 | { |
| 2592 | u16 new_id = cmp_cntxt; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2593 | dev_err(&skdev->pdev->dev, |
| 2594 | "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n", |
| 2595 | req_id, skreq->id, new_id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2596 | |
| 2597 | continue; |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY); |
| 2602 | |
| 2603 | if (skreq->state == SKD_REQ_STATE_ABORTED) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2604 | dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n", |
| 2605 | skreq, skreq->id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2606 | /* a previously timed out command can |
| 2607 | * now be cleaned up */ |
| 2608 | skd_release_skreq(skdev, skreq); |
| 2609 | continue; |
| 2610 | } |
| 2611 | |
| 2612 | skreq->completion = *skcmp; |
| 2613 | if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) { |
| 2614 | skreq->err_info = *skerr; |
| 2615 | skd_log_check_status(skdev, cmp_status, skerr->key, |
| 2616 | skerr->code, skerr->qual, |
| 2617 | skerr->fruc); |
| 2618 | } |
| 2619 | /* Release DMA resources for the request. */ |
| 2620 | if (skreq->n_sg > 0) |
| 2621 | skd_postop_sg_list(skdev, skreq); |
| 2622 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 2623 | if (!skreq->req) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2624 | dev_dbg(&skdev->pdev->dev, |
| 2625 | "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n", |
| 2626 | skreq, skreq->id, req_id); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2627 | } else { |
| 2628 | /* |
| 2629 | * Capture the outcome and post it back to the |
| 2630 | * native request. |
| 2631 | */ |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 2632 | if (likely(cmp_status == SAM_STAT_GOOD)) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2633 | skd_end_request(skdev, skreq, BLK_STS_OK); |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 2634 | else |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2635 | skd_resolve_req_exception(skdev, skreq); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * Release the skreq, its FIT msg (if one), timeout slot, |
| 2640 | * and queue depth. |
| 2641 | */ |
| 2642 | skd_release_skreq(skdev, skreq); |
| 2643 | |
| 2644 | /* skd_isr_comp_limit equal zero means no limit */ |
| 2645 | if (limit) { |
| 2646 | if (++processed >= limit) { |
| 2647 | rc = 1; |
| 2648 | break; |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | if ((skdev->state == SKD_DRVR_STATE_PAUSING) |
| 2654 | && (skdev->in_flight) == 0) { |
| 2655 | skdev->state = SKD_DRVR_STATE_PAUSED; |
| 2656 | wake_up_interruptible(&skdev->waitq); |
| 2657 | } |
| 2658 | |
| 2659 | return rc; |
| 2660 | } |
| 2661 | |
| 2662 | static void skd_complete_other(struct skd_device *skdev, |
| 2663 | volatile struct fit_completion_entry_v1 *skcomp, |
| 2664 | volatile struct fit_comp_error_info *skerr) |
| 2665 | { |
| 2666 | u32 req_id = 0; |
| 2667 | u32 req_table; |
| 2668 | u32 req_slot; |
| 2669 | struct skd_special_context *skspcl; |
| 2670 | |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 2671 | lockdep_assert_held(&skdev->lock); |
| 2672 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2673 | req_id = skcomp->tag; |
| 2674 | req_table = req_id & SKD_ID_TABLE_MASK; |
| 2675 | req_slot = req_id & SKD_ID_SLOT_MASK; |
| 2676 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2677 | dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table, |
| 2678 | req_id, req_slot); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2679 | |
| 2680 | /* |
| 2681 | * Based on the request id, determine how to dispatch this completion. |
| 2682 | * This swich/case is finding the good cases and forwarding the |
| 2683 | * completion entry. Errors are reported below the switch. |
| 2684 | */ |
| 2685 | switch (req_table) { |
| 2686 | case SKD_ID_RW_REQUEST: |
| 2687 | /* |
Bart Van Assche | e1d06f2 | 2017-08-17 13:12:54 -0700 | [diff] [blame] | 2688 | * The caller, skd_isr_completion_posted() above, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2689 | * handles r/w requests. The only way we get here |
| 2690 | * is if the req_slot is out of bounds. |
| 2691 | */ |
| 2692 | break; |
| 2693 | |
| 2694 | case SKD_ID_SPECIAL_REQUEST: |
| 2695 | /* |
| 2696 | * Make sure the req_slot is in bounds and that the id |
| 2697 | * matches. |
| 2698 | */ |
| 2699 | if (req_slot < skdev->n_special) { |
| 2700 | skspcl = &skdev->skspcl_table[req_slot]; |
| 2701 | if (skspcl->req.id == req_id && |
| 2702 | skspcl->req.state == SKD_REQ_STATE_BUSY) { |
| 2703 | skd_complete_special(skdev, |
| 2704 | skcomp, skerr, skspcl); |
| 2705 | return; |
| 2706 | } |
| 2707 | } |
| 2708 | break; |
| 2709 | |
| 2710 | case SKD_ID_INTERNAL: |
| 2711 | if (req_slot == 0) { |
| 2712 | skspcl = &skdev->internal_skspcl; |
| 2713 | if (skspcl->req.id == req_id && |
| 2714 | skspcl->req.state == SKD_REQ_STATE_BUSY) { |
| 2715 | skd_complete_internal(skdev, |
| 2716 | skcomp, skerr, skspcl); |
| 2717 | return; |
| 2718 | } |
| 2719 | } |
| 2720 | break; |
| 2721 | |
| 2722 | case SKD_ID_FIT_MSG: |
| 2723 | /* |
| 2724 | * These id's should never appear in a completion record. |
| 2725 | */ |
| 2726 | break; |
| 2727 | |
| 2728 | default: |
| 2729 | /* |
| 2730 | * These id's should never appear anywhere; |
| 2731 | */ |
| 2732 | break; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * If we get here it is a bad or stale id. |
| 2737 | */ |
| 2738 | } |
| 2739 | |
| 2740 | static void skd_complete_special(struct skd_device *skdev, |
| 2741 | volatile struct fit_completion_entry_v1 |
| 2742 | *skcomp, |
| 2743 | volatile struct fit_comp_error_info *skerr, |
| 2744 | struct skd_special_context *skspcl) |
| 2745 | { |
Bart Van Assche | 760b48c | 2017-08-17 13:13:00 -0700 | [diff] [blame] | 2746 | lockdep_assert_held(&skdev->lock); |
| 2747 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2748 | dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2749 | if (skspcl->orphaned) { |
| 2750 | /* Discard orphaned request */ |
| 2751 | /* ?: Can this release directly or does it need |
| 2752 | * to use a worker? */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2753 | dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2754 | skd_release_special(skdev, skspcl); |
| 2755 | return; |
| 2756 | } |
| 2757 | |
| 2758 | skd_process_scsi_inq(skdev, skcomp, skerr, skspcl); |
| 2759 | |
| 2760 | skspcl->req.state = SKD_REQ_STATE_COMPLETED; |
| 2761 | skspcl->req.completion = *skcomp; |
| 2762 | skspcl->req.err_info = *skerr; |
| 2763 | |
| 2764 | skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key, |
| 2765 | skerr->code, skerr->qual, skerr->fruc); |
| 2766 | |
| 2767 | wake_up_interruptible(&skdev->waitq); |
| 2768 | } |
| 2769 | |
| 2770 | /* assume spinlock is already held */ |
| 2771 | static void skd_release_special(struct skd_device *skdev, |
| 2772 | struct skd_special_context *skspcl) |
| 2773 | { |
| 2774 | int i, was_depleted; |
| 2775 | |
| 2776 | for (i = 0; i < skspcl->req.n_sg; i++) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2777 | struct page *page = sg_page(&skspcl->req.sg[i]); |
| 2778 | __free_page(page); |
| 2779 | } |
| 2780 | |
| 2781 | was_depleted = (skdev->skspcl_free_list == NULL); |
| 2782 | |
| 2783 | skspcl->req.state = SKD_REQ_STATE_IDLE; |
| 2784 | skspcl->req.id += SKD_ID_INCR; |
| 2785 | skspcl->req.next = |
| 2786 | (struct skd_request_context *)skdev->skspcl_free_list; |
| 2787 | skdev->skspcl_free_list = (struct skd_special_context *)skspcl; |
| 2788 | |
| 2789 | if (was_depleted) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2790 | dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2791 | /* Free list was depleted. Their might be waiters. */ |
| 2792 | wake_up_interruptible(&skdev->waitq); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | static void skd_reset_skcomp(struct skd_device *skdev) |
| 2797 | { |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 2798 | memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2799 | |
| 2800 | skdev->skcomp_ix = 0; |
| 2801 | skdev->skcomp_cycle = 1; |
| 2802 | } |
| 2803 | |
| 2804 | /* |
| 2805 | ***************************************************************************** |
| 2806 | * INTERRUPTS |
| 2807 | ***************************************************************************** |
| 2808 | */ |
| 2809 | static void skd_completion_worker(struct work_struct *work) |
| 2810 | { |
| 2811 | struct skd_device *skdev = |
| 2812 | container_of(work, struct skd_device, completion_worker); |
| 2813 | unsigned long flags; |
| 2814 | int flush_enqueued = 0; |
| 2815 | |
| 2816 | spin_lock_irqsave(&skdev->lock, flags); |
| 2817 | |
| 2818 | /* |
| 2819 | * pass in limit=0, which means no limit.. |
| 2820 | * process everything in compq |
| 2821 | */ |
| 2822 | skd_isr_completion_posted(skdev, 0, &flush_enqueued); |
| 2823 | skd_request_fn(skdev->queue); |
| 2824 | |
| 2825 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 2826 | } |
| 2827 | |
| 2828 | static void skd_isr_msg_from_dev(struct skd_device *skdev); |
| 2829 | |
Arnd Bergmann | 41c9499 | 2016-11-09 13:55:35 +0100 | [diff] [blame] | 2830 | static irqreturn_t |
| 2831 | skd_isr(int irq, void *ptr) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2832 | { |
| 2833 | struct skd_device *skdev; |
| 2834 | u32 intstat; |
| 2835 | u32 ack; |
| 2836 | int rc = 0; |
| 2837 | int deferred = 0; |
| 2838 | int flush_enqueued = 0; |
| 2839 | |
| 2840 | skdev = (struct skd_device *)ptr; |
| 2841 | spin_lock(&skdev->lock); |
| 2842 | |
| 2843 | for (;; ) { |
| 2844 | intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST); |
| 2845 | |
| 2846 | ack = FIT_INT_DEF_MASK; |
| 2847 | ack &= intstat; |
| 2848 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2849 | dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat, |
| 2850 | ack); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2851 | |
| 2852 | /* As long as there is an int pending on device, keep |
| 2853 | * running loop. When none, get out, but if we've never |
| 2854 | * done any processing, call completion handler? |
| 2855 | */ |
| 2856 | if (ack == 0) { |
| 2857 | /* No interrupts on device, but run the completion |
| 2858 | * processor anyway? |
| 2859 | */ |
| 2860 | if (rc == 0) |
| 2861 | if (likely (skdev->state |
| 2862 | == SKD_DRVR_STATE_ONLINE)) |
| 2863 | deferred = 1; |
| 2864 | break; |
| 2865 | } |
| 2866 | |
| 2867 | rc = IRQ_HANDLED; |
| 2868 | |
| 2869 | SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST); |
| 2870 | |
| 2871 | if (likely((skdev->state != SKD_DRVR_STATE_LOAD) && |
| 2872 | (skdev->state != SKD_DRVR_STATE_STOPPING))) { |
| 2873 | if (intstat & FIT_ISH_COMPLETION_POSTED) { |
| 2874 | /* |
| 2875 | * If we have already deferred completion |
| 2876 | * processing, don't bother running it again |
| 2877 | */ |
| 2878 | if (deferred == 0) |
| 2879 | deferred = |
| 2880 | skd_isr_completion_posted(skdev, |
| 2881 | skd_isr_comp_limit, &flush_enqueued); |
| 2882 | } |
| 2883 | |
| 2884 | if (intstat & FIT_ISH_FW_STATE_CHANGE) { |
| 2885 | skd_isr_fwstate(skdev); |
| 2886 | if (skdev->state == SKD_DRVR_STATE_FAULT || |
| 2887 | skdev->state == |
| 2888 | SKD_DRVR_STATE_DISAPPEARED) { |
| 2889 | spin_unlock(&skdev->lock); |
| 2890 | return rc; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | if (intstat & FIT_ISH_MSG_FROM_DEV) |
| 2895 | skd_isr_msg_from_dev(skdev); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | if (unlikely(flush_enqueued)) |
| 2900 | skd_request_fn(skdev->queue); |
| 2901 | |
| 2902 | if (deferred) |
| 2903 | schedule_work(&skdev->completion_worker); |
| 2904 | else if (!flush_enqueued) |
| 2905 | skd_request_fn(skdev->queue); |
| 2906 | |
| 2907 | spin_unlock(&skdev->lock); |
| 2908 | |
| 2909 | return rc; |
| 2910 | } |
| 2911 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2912 | static void skd_drive_fault(struct skd_device *skdev) |
| 2913 | { |
| 2914 | skdev->state = SKD_DRVR_STATE_FAULT; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2915 | dev_err(&skdev->pdev->dev, "Drive FAULT\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | static void skd_drive_disappeared(struct skd_device *skdev) |
| 2919 | { |
| 2920 | skdev->state = SKD_DRVR_STATE_DISAPPEARED; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2921 | dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | static void skd_isr_fwstate(struct skd_device *skdev) |
| 2925 | { |
| 2926 | u32 sense; |
| 2927 | u32 state; |
| 2928 | u32 mtd; |
| 2929 | int prev_driver_state = skdev->state; |
| 2930 | |
| 2931 | sense = SKD_READL(skdev, FIT_STATUS); |
| 2932 | state = sense & FIT_SR_DRIVE_STATE_MASK; |
| 2933 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2934 | dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n", |
| 2935 | skd_drive_state_to_str(skdev->drive_state), skdev->drive_state, |
| 2936 | skd_drive_state_to_str(state), state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2937 | |
| 2938 | skdev->drive_state = state; |
| 2939 | |
| 2940 | switch (skdev->drive_state) { |
| 2941 | case FIT_SR_DRIVE_INIT: |
| 2942 | if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) { |
| 2943 | skd_disable_interrupts(skdev); |
| 2944 | break; |
| 2945 | } |
| 2946 | if (skdev->state == SKD_DRVR_STATE_RESTARTING) |
| 2947 | skd_recover_requests(skdev, 0); |
| 2948 | if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) { |
| 2949 | skdev->timer_countdown = SKD_STARTING_TIMO; |
| 2950 | skdev->state = SKD_DRVR_STATE_STARTING; |
| 2951 | skd_soft_reset(skdev); |
| 2952 | break; |
| 2953 | } |
| 2954 | mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0); |
| 2955 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 2956 | skdev->last_mtd = mtd; |
| 2957 | break; |
| 2958 | |
| 2959 | case FIT_SR_DRIVE_ONLINE: |
| 2960 | skdev->cur_max_queue_depth = skd_max_queue_depth; |
| 2961 | if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth) |
| 2962 | skdev->cur_max_queue_depth = skdev->dev_max_queue_depth; |
| 2963 | |
| 2964 | skdev->queue_low_water_mark = |
| 2965 | skdev->cur_max_queue_depth * 2 / 3 + 1; |
| 2966 | if (skdev->queue_low_water_mark < 1) |
| 2967 | skdev->queue_low_water_mark = 1; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 2968 | dev_info(&skdev->pdev->dev, |
| 2969 | "Queue depth limit=%d dev=%d lowat=%d\n", |
| 2970 | skdev->cur_max_queue_depth, |
| 2971 | skdev->dev_max_queue_depth, |
| 2972 | skdev->queue_low_water_mark); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2973 | |
| 2974 | skd_refresh_device_data(skdev); |
| 2975 | break; |
| 2976 | |
| 2977 | case FIT_SR_DRIVE_BUSY: |
| 2978 | skdev->state = SKD_DRVR_STATE_BUSY; |
| 2979 | skdev->timer_countdown = SKD_BUSY_TIMO; |
| 2980 | skd_quiesce_dev(skdev); |
| 2981 | break; |
| 2982 | case FIT_SR_DRIVE_BUSY_SANITIZE: |
| 2983 | /* set timer for 3 seconds, we'll abort any unfinished |
| 2984 | * commands after that expires |
| 2985 | */ |
| 2986 | skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE; |
| 2987 | skdev->timer_countdown = SKD_TIMER_SECONDS(3); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 2988 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 2989 | break; |
| 2990 | case FIT_SR_DRIVE_BUSY_ERASE: |
| 2991 | skdev->state = SKD_DRVR_STATE_BUSY_ERASE; |
| 2992 | skdev->timer_countdown = SKD_BUSY_TIMO; |
| 2993 | break; |
| 2994 | case FIT_SR_DRIVE_OFFLINE: |
| 2995 | skdev->state = SKD_DRVR_STATE_IDLE; |
| 2996 | break; |
| 2997 | case FIT_SR_DRIVE_SOFT_RESET: |
| 2998 | switch (skdev->state) { |
| 2999 | case SKD_DRVR_STATE_STARTING: |
| 3000 | case SKD_DRVR_STATE_RESTARTING: |
| 3001 | /* Expected by a caller of skd_soft_reset() */ |
| 3002 | break; |
| 3003 | default: |
| 3004 | skdev->state = SKD_DRVR_STATE_RESTARTING; |
| 3005 | break; |
| 3006 | } |
| 3007 | break; |
| 3008 | case FIT_SR_DRIVE_FW_BOOTING: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3009 | dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3010 | skdev->state = SKD_DRVR_STATE_WAIT_BOOT; |
| 3011 | skdev->timer_countdown = SKD_WAIT_BOOT_TIMO; |
| 3012 | break; |
| 3013 | |
| 3014 | case FIT_SR_DRIVE_DEGRADED: |
| 3015 | case FIT_SR_PCIE_LINK_DOWN: |
| 3016 | case FIT_SR_DRIVE_NEED_FW_DOWNLOAD: |
| 3017 | break; |
| 3018 | |
| 3019 | case FIT_SR_DRIVE_FAULT: |
| 3020 | skd_drive_fault(skdev); |
| 3021 | skd_recover_requests(skdev, 0); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 3022 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3023 | break; |
| 3024 | |
| 3025 | /* PCIe bus returned all Fs? */ |
| 3026 | case 0xFF: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3027 | dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state, |
| 3028 | sense); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3029 | skd_drive_disappeared(skdev); |
| 3030 | skd_recover_requests(skdev, 0); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 3031 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3032 | break; |
| 3033 | default: |
| 3034 | /* |
| 3035 | * Uknown FW State. Wait for a state we recognize. |
| 3036 | */ |
| 3037 | break; |
| 3038 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3039 | dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n", |
| 3040 | skd_skdev_state_to_str(prev_driver_state), prev_driver_state, |
| 3041 | skd_skdev_state_to_str(skdev->state), skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | static void skd_recover_requests(struct skd_device *skdev, int requeue) |
| 3045 | { |
| 3046 | int i; |
| 3047 | |
| 3048 | for (i = 0; i < skdev->num_req_context; i++) { |
| 3049 | struct skd_request_context *skreq = &skdev->skreq_table[i]; |
| 3050 | |
| 3051 | if (skreq->state == SKD_REQ_STATE_BUSY) { |
| 3052 | skd_log_skreq(skdev, skreq, "recover"); |
| 3053 | |
| 3054 | SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0); |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 3055 | SKD_ASSERT(skreq->req != NULL); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3056 | |
| 3057 | /* Release DMA resources for the request. */ |
| 3058 | if (skreq->n_sg > 0) |
| 3059 | skd_postop_sg_list(skdev, skreq); |
| 3060 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 3061 | if (requeue && |
| 3062 | (unsigned long) ++skreq->req->special < |
| 3063 | SKD_MAX_RETRIES) |
Mike Snitzer | 38d4a1b | 2013-11-01 15:05:10 -0400 | [diff] [blame] | 3064 | blk_requeue_request(skdev->queue, skreq->req); |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 3065 | else |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3066 | skd_end_request(skdev, skreq, BLK_STS_IOERR); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3067 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 3068 | skreq->req = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3069 | |
| 3070 | skreq->state = SKD_REQ_STATE_IDLE; |
| 3071 | skreq->id += SKD_ID_INCR; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3072 | } |
| 3073 | if (i > 0) |
| 3074 | skreq[-1].next = skreq; |
| 3075 | skreq->next = NULL; |
| 3076 | } |
| 3077 | skdev->skreq_free_list = skdev->skreq_table; |
| 3078 | |
| 3079 | for (i = 0; i < skdev->num_fitmsg_context; i++) { |
| 3080 | struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i]; |
| 3081 | |
| 3082 | if (skmsg->state == SKD_MSG_STATE_BUSY) { |
| 3083 | skd_log_skmsg(skdev, skmsg, "salvaged"); |
| 3084 | SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0); |
| 3085 | skmsg->state = SKD_MSG_STATE_IDLE; |
| 3086 | skmsg->id += SKD_ID_INCR; |
| 3087 | } |
| 3088 | if (i > 0) |
| 3089 | skmsg[-1].next = skmsg; |
| 3090 | skmsg->next = NULL; |
| 3091 | } |
| 3092 | skdev->skmsg_free_list = skdev->skmsg_table; |
| 3093 | |
| 3094 | for (i = 0; i < skdev->n_special; i++) { |
| 3095 | struct skd_special_context *skspcl = &skdev->skspcl_table[i]; |
| 3096 | |
| 3097 | /* If orphaned, reclaim it because it has already been reported |
| 3098 | * to the process as an error (it was just waiting for |
| 3099 | * a completion that didn't come, and now it will never come) |
| 3100 | * If busy, change to a state that will cause it to error |
| 3101 | * out in the wait routine and let it do the normal |
| 3102 | * reporting and reclaiming |
| 3103 | */ |
| 3104 | if (skspcl->req.state == SKD_REQ_STATE_BUSY) { |
| 3105 | if (skspcl->orphaned) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3106 | dev_dbg(&skdev->pdev->dev, "orphaned %p\n", |
| 3107 | skspcl); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3108 | skd_release_special(skdev, skspcl); |
| 3109 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3110 | dev_dbg(&skdev->pdev->dev, "not orphaned %p\n", |
| 3111 | skspcl); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3112 | skspcl->req.state = SKD_REQ_STATE_ABORTED; |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | skdev->skspcl_free_list = skdev->skspcl_table; |
| 3117 | |
| 3118 | for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++) |
| 3119 | skdev->timeout_slot[i] = 0; |
| 3120 | |
| 3121 | skdev->in_flight = 0; |
| 3122 | } |
| 3123 | |
| 3124 | static void skd_isr_msg_from_dev(struct skd_device *skdev) |
| 3125 | { |
| 3126 | u32 mfd; |
| 3127 | u32 mtd; |
| 3128 | u32 data; |
| 3129 | |
| 3130 | mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE); |
| 3131 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3132 | dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd, |
| 3133 | skdev->last_mtd); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3134 | |
| 3135 | /* ignore any mtd that is an ack for something we didn't send */ |
| 3136 | if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd)) |
| 3137 | return; |
| 3138 | |
| 3139 | switch (FIT_MXD_TYPE(mfd)) { |
| 3140 | case FIT_MTD_FITFW_INIT: |
| 3141 | skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd); |
| 3142 | |
| 3143 | if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3144 | dev_err(&skdev->pdev->dev, "protocol mismatch\n"); |
| 3145 | dev_err(&skdev->pdev->dev, " got=%d support=%d\n", |
| 3146 | skdev->proto_ver, FIT_PROTOCOL_VERSION_1); |
| 3147 | dev_err(&skdev->pdev->dev, " please upgrade driver\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3148 | skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH; |
| 3149 | skd_soft_reset(skdev); |
| 3150 | break; |
| 3151 | } |
| 3152 | mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0); |
| 3153 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3154 | skdev->last_mtd = mtd; |
| 3155 | break; |
| 3156 | |
| 3157 | case FIT_MTD_GET_CMDQ_DEPTH: |
| 3158 | skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd); |
| 3159 | mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0, |
| 3160 | SKD_N_COMPLETION_ENTRY); |
| 3161 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3162 | skdev->last_mtd = mtd; |
| 3163 | break; |
| 3164 | |
| 3165 | case FIT_MTD_SET_COMPQ_DEPTH: |
| 3166 | SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG); |
| 3167 | mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0); |
| 3168 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3169 | skdev->last_mtd = mtd; |
| 3170 | break; |
| 3171 | |
| 3172 | case FIT_MTD_SET_COMPQ_ADDR: |
| 3173 | skd_reset_skcomp(skdev); |
| 3174 | mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno); |
| 3175 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3176 | skdev->last_mtd = mtd; |
| 3177 | break; |
| 3178 | |
| 3179 | case FIT_MTD_CMD_LOG_HOST_ID: |
| 3180 | skdev->connect_time_stamp = get_seconds(); |
| 3181 | data = skdev->connect_time_stamp & 0xFFFF; |
| 3182 | mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data); |
| 3183 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3184 | skdev->last_mtd = mtd; |
| 3185 | break; |
| 3186 | |
| 3187 | case FIT_MTD_CMD_LOG_TIME_STAMP_LO: |
| 3188 | skdev->drive_jiffies = FIT_MXD_DATA(mfd); |
| 3189 | data = (skdev->connect_time_stamp >> 16) & 0xFFFF; |
| 3190 | mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data); |
| 3191 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3192 | skdev->last_mtd = mtd; |
| 3193 | break; |
| 3194 | |
| 3195 | case FIT_MTD_CMD_LOG_TIME_STAMP_HI: |
| 3196 | skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16); |
| 3197 | mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0); |
| 3198 | SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE); |
| 3199 | skdev->last_mtd = mtd; |
| 3200 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3201 | dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n", |
| 3202 | skdev->connect_time_stamp, skdev->drive_jiffies); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3203 | break; |
| 3204 | |
| 3205 | case FIT_MTD_ARM_QUEUE: |
| 3206 | skdev->last_mtd = 0; |
| 3207 | /* |
| 3208 | * State should be, or soon will be, FIT_SR_DRIVE_ONLINE. |
| 3209 | */ |
| 3210 | break; |
| 3211 | |
| 3212 | default: |
| 3213 | break; |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | static void skd_disable_interrupts(struct skd_device *skdev) |
| 3218 | { |
| 3219 | u32 sense; |
| 3220 | |
| 3221 | sense = SKD_READL(skdev, FIT_CONTROL); |
| 3222 | sense &= ~FIT_CR_ENABLE_INTERRUPTS; |
| 3223 | SKD_WRITEL(skdev, sense, FIT_CONTROL); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3224 | dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3225 | |
| 3226 | /* Note that the 1s is written. A 1-bit means |
| 3227 | * disable, a 0 means enable. |
| 3228 | */ |
| 3229 | SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST); |
| 3230 | } |
| 3231 | |
| 3232 | static void skd_enable_interrupts(struct skd_device *skdev) |
| 3233 | { |
| 3234 | u32 val; |
| 3235 | |
| 3236 | /* unmask interrupts first */ |
| 3237 | val = FIT_ISH_FW_STATE_CHANGE + |
| 3238 | FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV; |
| 3239 | |
| 3240 | /* Note that the compliment of mask is written. A 1-bit means |
| 3241 | * disable, a 0 means enable. */ |
| 3242 | SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3243 | dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3244 | |
| 3245 | val = SKD_READL(skdev, FIT_CONTROL); |
| 3246 | val |= FIT_CR_ENABLE_INTERRUPTS; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3247 | dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3248 | SKD_WRITEL(skdev, val, FIT_CONTROL); |
| 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | ***************************************************************************** |
| 3253 | * START, STOP, RESTART, QUIESCE, UNQUIESCE |
| 3254 | ***************************************************************************** |
| 3255 | */ |
| 3256 | |
| 3257 | static void skd_soft_reset(struct skd_device *skdev) |
| 3258 | { |
| 3259 | u32 val; |
| 3260 | |
| 3261 | val = SKD_READL(skdev, FIT_CONTROL); |
| 3262 | val |= (FIT_CR_SOFT_RESET); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3263 | dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3264 | SKD_WRITEL(skdev, val, FIT_CONTROL); |
| 3265 | } |
| 3266 | |
| 3267 | static void skd_start_device(struct skd_device *skdev) |
| 3268 | { |
| 3269 | unsigned long flags; |
| 3270 | u32 sense; |
| 3271 | u32 state; |
| 3272 | |
| 3273 | spin_lock_irqsave(&skdev->lock, flags); |
| 3274 | |
| 3275 | /* ack all ghost interrupts */ |
| 3276 | SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST); |
| 3277 | |
| 3278 | sense = SKD_READL(skdev, FIT_STATUS); |
| 3279 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3280 | dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3281 | |
| 3282 | state = sense & FIT_SR_DRIVE_STATE_MASK; |
| 3283 | skdev->drive_state = state; |
| 3284 | skdev->last_mtd = 0; |
| 3285 | |
| 3286 | skdev->state = SKD_DRVR_STATE_STARTING; |
| 3287 | skdev->timer_countdown = SKD_STARTING_TIMO; |
| 3288 | |
| 3289 | skd_enable_interrupts(skdev); |
| 3290 | |
| 3291 | switch (skdev->drive_state) { |
| 3292 | case FIT_SR_DRIVE_OFFLINE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3293 | dev_err(&skdev->pdev->dev, "Drive offline...\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3294 | break; |
| 3295 | |
| 3296 | case FIT_SR_DRIVE_FW_BOOTING: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3297 | dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3298 | skdev->state = SKD_DRVR_STATE_WAIT_BOOT; |
| 3299 | skdev->timer_countdown = SKD_WAIT_BOOT_TIMO; |
| 3300 | break; |
| 3301 | |
| 3302 | case FIT_SR_DRIVE_BUSY_SANITIZE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3303 | dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3304 | skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE; |
| 3305 | skdev->timer_countdown = SKD_STARTED_BUSY_TIMO; |
| 3306 | break; |
| 3307 | |
| 3308 | case FIT_SR_DRIVE_BUSY_ERASE: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3309 | dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3310 | skdev->state = SKD_DRVR_STATE_BUSY_ERASE; |
| 3311 | skdev->timer_countdown = SKD_STARTED_BUSY_TIMO; |
| 3312 | break; |
| 3313 | |
| 3314 | case FIT_SR_DRIVE_INIT: |
| 3315 | case FIT_SR_DRIVE_ONLINE: |
| 3316 | skd_soft_reset(skdev); |
| 3317 | break; |
| 3318 | |
| 3319 | case FIT_SR_DRIVE_BUSY: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3320 | dev_err(&skdev->pdev->dev, "Drive Busy...\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3321 | skdev->state = SKD_DRVR_STATE_BUSY; |
| 3322 | skdev->timer_countdown = SKD_STARTED_BUSY_TIMO; |
| 3323 | break; |
| 3324 | |
| 3325 | case FIT_SR_DRIVE_SOFT_RESET: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3326 | dev_err(&skdev->pdev->dev, "drive soft reset in prog\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3327 | break; |
| 3328 | |
| 3329 | case FIT_SR_DRIVE_FAULT: |
| 3330 | /* Fault state is bad...soft reset won't do it... |
| 3331 | * Hard reset, maybe, but does it work on device? |
| 3332 | * For now, just fault so the system doesn't hang. |
| 3333 | */ |
| 3334 | skd_drive_fault(skdev); |
| 3335 | /*start the queue so we can respond with error to requests */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3336 | dev_dbg(&skdev->pdev->dev, "starting queue\n"); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 3337 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3338 | skdev->gendisk_on = -1; |
| 3339 | wake_up_interruptible(&skdev->waitq); |
| 3340 | break; |
| 3341 | |
| 3342 | case 0xFF: |
| 3343 | /* Most likely the device isn't there or isn't responding |
| 3344 | * to the BAR1 addresses. */ |
| 3345 | skd_drive_disappeared(skdev); |
| 3346 | /*start the queue so we can respond with error to requests */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3347 | dev_dbg(&skdev->pdev->dev, |
| 3348 | "starting queue to error-out reqs\n"); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 3349 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3350 | skdev->gendisk_on = -1; |
| 3351 | wake_up_interruptible(&skdev->waitq); |
| 3352 | break; |
| 3353 | |
| 3354 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3355 | dev_err(&skdev->pdev->dev, "Start: unknown state %x\n", |
| 3356 | skdev->drive_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3357 | break; |
| 3358 | } |
| 3359 | |
| 3360 | state = SKD_READL(skdev, FIT_CONTROL); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3361 | dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3362 | |
| 3363 | state = SKD_READL(skdev, FIT_INT_STATUS_HOST); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3364 | dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3365 | |
| 3366 | state = SKD_READL(skdev, FIT_INT_MASK_HOST); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3367 | dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3368 | |
| 3369 | state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3370 | dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3371 | |
| 3372 | state = SKD_READL(skdev, FIT_HW_VERSION); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3373 | dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3374 | |
| 3375 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3376 | } |
| 3377 | |
| 3378 | static void skd_stop_device(struct skd_device *skdev) |
| 3379 | { |
| 3380 | unsigned long flags; |
| 3381 | struct skd_special_context *skspcl = &skdev->internal_skspcl; |
| 3382 | u32 dev_state; |
| 3383 | int i; |
| 3384 | |
| 3385 | spin_lock_irqsave(&skdev->lock, flags); |
| 3386 | |
| 3387 | if (skdev->state != SKD_DRVR_STATE_ONLINE) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3388 | dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3389 | goto stop_out; |
| 3390 | } |
| 3391 | |
| 3392 | if (skspcl->req.state != SKD_REQ_STATE_IDLE) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3393 | dev_err(&skdev->pdev->dev, "%s no special\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3394 | goto stop_out; |
| 3395 | } |
| 3396 | |
| 3397 | skdev->state = SKD_DRVR_STATE_SYNCING; |
| 3398 | skdev->sync_done = 0; |
| 3399 | |
| 3400 | skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE); |
| 3401 | |
| 3402 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3403 | |
| 3404 | wait_event_interruptible_timeout(skdev->waitq, |
| 3405 | (skdev->sync_done), (10 * HZ)); |
| 3406 | |
| 3407 | spin_lock_irqsave(&skdev->lock, flags); |
| 3408 | |
| 3409 | switch (skdev->sync_done) { |
| 3410 | case 0: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3411 | dev_err(&skdev->pdev->dev, "%s no sync\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3412 | break; |
| 3413 | case 1: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3414 | dev_err(&skdev->pdev->dev, "%s sync done\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3415 | break; |
| 3416 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3417 | dev_err(&skdev->pdev->dev, "%s sync error\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | stop_out: |
| 3421 | skdev->state = SKD_DRVR_STATE_STOPPING; |
| 3422 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3423 | |
| 3424 | skd_kill_timer(skdev); |
| 3425 | |
| 3426 | spin_lock_irqsave(&skdev->lock, flags); |
| 3427 | skd_disable_interrupts(skdev); |
| 3428 | |
| 3429 | /* ensure all ints on device are cleared */ |
| 3430 | /* soft reset the device to unload with a clean slate */ |
| 3431 | SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST); |
| 3432 | SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL); |
| 3433 | |
| 3434 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3435 | |
| 3436 | /* poll every 100ms, 1 second timeout */ |
| 3437 | for (i = 0; i < 10; i++) { |
| 3438 | dev_state = |
| 3439 | SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK; |
| 3440 | if (dev_state == FIT_SR_DRIVE_INIT) |
| 3441 | break; |
| 3442 | set_current_state(TASK_INTERRUPTIBLE); |
| 3443 | schedule_timeout(msecs_to_jiffies(100)); |
| 3444 | } |
| 3445 | |
| 3446 | if (dev_state != FIT_SR_DRIVE_INIT) |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3447 | dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__, |
| 3448 | dev_state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | /* assume spinlock is held */ |
| 3452 | static void skd_restart_device(struct skd_device *skdev) |
| 3453 | { |
| 3454 | u32 state; |
| 3455 | |
| 3456 | /* ack all ghost interrupts */ |
| 3457 | SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST); |
| 3458 | |
| 3459 | state = SKD_READL(skdev, FIT_STATUS); |
| 3460 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3461 | dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3462 | |
| 3463 | state &= FIT_SR_DRIVE_STATE_MASK; |
| 3464 | skdev->drive_state = state; |
| 3465 | skdev->last_mtd = 0; |
| 3466 | |
| 3467 | skdev->state = SKD_DRVR_STATE_RESTARTING; |
| 3468 | skdev->timer_countdown = SKD_RESTARTING_TIMO; |
| 3469 | |
| 3470 | skd_soft_reset(skdev); |
| 3471 | } |
| 3472 | |
| 3473 | /* assume spinlock is held */ |
| 3474 | static int skd_quiesce_dev(struct skd_device *skdev) |
| 3475 | { |
| 3476 | int rc = 0; |
| 3477 | |
| 3478 | switch (skdev->state) { |
| 3479 | case SKD_DRVR_STATE_BUSY: |
| 3480 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3481 | dev_dbg(&skdev->pdev->dev, "stopping queue\n"); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 3482 | blk_stop_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3483 | break; |
| 3484 | case SKD_DRVR_STATE_ONLINE: |
| 3485 | case SKD_DRVR_STATE_STOPPING: |
| 3486 | case SKD_DRVR_STATE_SYNCING: |
| 3487 | case SKD_DRVR_STATE_PAUSING: |
| 3488 | case SKD_DRVR_STATE_PAUSED: |
| 3489 | case SKD_DRVR_STATE_STARTING: |
| 3490 | case SKD_DRVR_STATE_RESTARTING: |
| 3491 | case SKD_DRVR_STATE_RESUMING: |
| 3492 | default: |
| 3493 | rc = -EINVAL; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3494 | dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n", |
| 3495 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3496 | } |
| 3497 | return rc; |
| 3498 | } |
| 3499 | |
| 3500 | /* assume spinlock is held */ |
| 3501 | static int skd_unquiesce_dev(struct skd_device *skdev) |
| 3502 | { |
| 3503 | int prev_driver_state = skdev->state; |
| 3504 | |
| 3505 | skd_log_skdev(skdev, "unquiesce"); |
| 3506 | if (skdev->state == SKD_DRVR_STATE_ONLINE) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3507 | dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3508 | return 0; |
| 3509 | } |
| 3510 | if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) { |
| 3511 | /* |
| 3512 | * If there has been an state change to other than |
| 3513 | * ONLINE, we will rely on controller state change |
| 3514 | * to come back online and restart the queue. |
| 3515 | * The BUSY state means that driver is ready to |
| 3516 | * continue normal processing but waiting for controller |
| 3517 | * to become available. |
| 3518 | */ |
| 3519 | skdev->state = SKD_DRVR_STATE_BUSY; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3520 | dev_dbg(&skdev->pdev->dev, "drive BUSY state\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3521 | return 0; |
| 3522 | } |
| 3523 | |
| 3524 | /* |
| 3525 | * Drive has just come online, driver is either in startup, |
| 3526 | * paused performing a task, or bust waiting for hardware. |
| 3527 | */ |
| 3528 | switch (skdev->state) { |
| 3529 | case SKD_DRVR_STATE_PAUSED: |
| 3530 | case SKD_DRVR_STATE_BUSY: |
| 3531 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 3532 | case SKD_DRVR_STATE_BUSY_ERASE: |
| 3533 | case SKD_DRVR_STATE_STARTING: |
| 3534 | case SKD_DRVR_STATE_RESTARTING: |
| 3535 | case SKD_DRVR_STATE_FAULT: |
| 3536 | case SKD_DRVR_STATE_IDLE: |
| 3537 | case SKD_DRVR_STATE_LOAD: |
| 3538 | skdev->state = SKD_DRVR_STATE_ONLINE; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3539 | dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n", |
| 3540 | skd_skdev_state_to_str(prev_driver_state), |
| 3541 | prev_driver_state, skd_skdev_state_to_str(skdev->state), |
| 3542 | skdev->state); |
| 3543 | dev_dbg(&skdev->pdev->dev, |
| 3544 | "**** device ONLINE...starting block queue\n"); |
| 3545 | dev_dbg(&skdev->pdev->dev, "starting queue\n"); |
| 3546 | dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n"); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 3547 | blk_start_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3548 | skdev->gendisk_on = 1; |
| 3549 | wake_up_interruptible(&skdev->waitq); |
| 3550 | break; |
| 3551 | |
| 3552 | case SKD_DRVR_STATE_DISAPPEARED: |
| 3553 | default: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3554 | dev_dbg(&skdev->pdev->dev, |
| 3555 | "**** driver state %d, not implemented\n", |
| 3556 | skdev->state); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3557 | return -EBUSY; |
| 3558 | } |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
| 3562 | /* |
| 3563 | ***************************************************************************** |
| 3564 | * PCIe MSI/MSI-X INTERRUPT HANDLERS |
| 3565 | ***************************************************************************** |
| 3566 | */ |
| 3567 | |
| 3568 | static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data) |
| 3569 | { |
| 3570 | struct skd_device *skdev = skd_host_data; |
| 3571 | unsigned long flags; |
| 3572 | |
| 3573 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3574 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 3575 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
| 3576 | dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq, |
| 3577 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3578 | SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST); |
| 3579 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3580 | return IRQ_HANDLED; |
| 3581 | } |
| 3582 | |
| 3583 | static irqreturn_t skd_statec_isr(int irq, void *skd_host_data) |
| 3584 | { |
| 3585 | struct skd_device *skdev = skd_host_data; |
| 3586 | unsigned long flags; |
| 3587 | |
| 3588 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3589 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 3590 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3591 | SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST); |
| 3592 | skd_isr_fwstate(skdev); |
| 3593 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3594 | return IRQ_HANDLED; |
| 3595 | } |
| 3596 | |
| 3597 | static irqreturn_t skd_comp_q(int irq, void *skd_host_data) |
| 3598 | { |
| 3599 | struct skd_device *skdev = skd_host_data; |
| 3600 | unsigned long flags; |
| 3601 | int flush_enqueued = 0; |
| 3602 | int deferred; |
| 3603 | |
| 3604 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3605 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 3606 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3607 | SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST); |
| 3608 | deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit, |
| 3609 | &flush_enqueued); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3610 | if (flush_enqueued) |
| 3611 | skd_request_fn(skdev->queue); |
| 3612 | |
| 3613 | if (deferred) |
| 3614 | schedule_work(&skdev->completion_worker); |
| 3615 | else if (!flush_enqueued) |
| 3616 | skd_request_fn(skdev->queue); |
| 3617 | |
| 3618 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3619 | |
| 3620 | return IRQ_HANDLED; |
| 3621 | } |
| 3622 | |
| 3623 | static irqreturn_t skd_msg_isr(int irq, void *skd_host_data) |
| 3624 | { |
| 3625 | struct skd_device *skdev = skd_host_data; |
| 3626 | unsigned long flags; |
| 3627 | |
| 3628 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3629 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 3630 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3631 | SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST); |
| 3632 | skd_isr_msg_from_dev(skdev); |
| 3633 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3634 | return IRQ_HANDLED; |
| 3635 | } |
| 3636 | |
| 3637 | static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data) |
| 3638 | { |
| 3639 | struct skd_device *skdev = skd_host_data; |
| 3640 | unsigned long flags; |
| 3641 | |
| 3642 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3643 | dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n", |
| 3644 | SKD_READL(skdev, FIT_INT_STATUS_HOST)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3645 | SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST); |
| 3646 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 3647 | return IRQ_HANDLED; |
| 3648 | } |
| 3649 | |
| 3650 | /* |
| 3651 | ***************************************************************************** |
| 3652 | * PCIe MSI/MSI-X SETUP |
| 3653 | ***************************************************************************** |
| 3654 | */ |
| 3655 | |
| 3656 | struct skd_msix_entry { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3657 | char isr_name[30]; |
| 3658 | }; |
| 3659 | |
| 3660 | struct skd_init_msix_entry { |
| 3661 | const char *name; |
| 3662 | irq_handler_t handler; |
| 3663 | }; |
| 3664 | |
| 3665 | #define SKD_MAX_MSIX_COUNT 13 |
| 3666 | #define SKD_MIN_MSIX_COUNT 7 |
| 3667 | #define SKD_BASE_MSIX_IRQ 4 |
| 3668 | |
| 3669 | static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = { |
| 3670 | { "(DMA 0)", skd_reserved_isr }, |
| 3671 | { "(DMA 1)", skd_reserved_isr }, |
| 3672 | { "(DMA 2)", skd_reserved_isr }, |
| 3673 | { "(DMA 3)", skd_reserved_isr }, |
| 3674 | { "(State Change)", skd_statec_isr }, |
| 3675 | { "(COMPL_Q)", skd_comp_q }, |
| 3676 | { "(MSG)", skd_msg_isr }, |
| 3677 | { "(Reserved)", skd_reserved_isr }, |
| 3678 | { "(Reserved)", skd_reserved_isr }, |
| 3679 | { "(Queue Full 0)", skd_qfull_isr }, |
| 3680 | { "(Queue Full 1)", skd_qfull_isr }, |
| 3681 | { "(Queue Full 2)", skd_qfull_isr }, |
| 3682 | { "(Queue Full 3)", skd_qfull_isr }, |
| 3683 | }; |
| 3684 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3685 | static int skd_acquire_msix(struct skd_device *skdev) |
| 3686 | { |
Alexander Gordeev | a9df862 | 2014-02-19 09:58:21 +0100 | [diff] [blame] | 3687 | int i, rc; |
Alexander Gordeev | 4681776 | 2014-02-19 09:58:19 +0100 | [diff] [blame] | 3688 | struct pci_dev *pdev = skdev->pdev; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3689 | |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3690 | rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT, |
| 3691 | PCI_IRQ_MSIX); |
| 3692 | if (rc < 0) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3693 | 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] | 3694 | goto out; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3695 | } |
Alexander Gordeev | 4681776 | 2014-02-19 09:58:19 +0100 | [diff] [blame] | 3696 | |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3697 | skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT, |
| 3698 | sizeof(struct skd_msix_entry), GFP_KERNEL); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3699 | if (!skdev->msix_entries) { |
| 3700 | rc = -ENOMEM; |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3701 | dev_err(&skdev->pdev->dev, "msix table allocation error\n"); |
Arnd Bergmann | 3bc8492 | 2016-11-09 13:55:34 +0100 | [diff] [blame] | 3702 | goto out; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3703 | } |
| 3704 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3705 | /* Enable MSI-X vectors for the base queue */ |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3706 | for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) { |
| 3707 | struct skd_msix_entry *qentry = &skdev->msix_entries[i]; |
| 3708 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3709 | snprintf(qentry->isr_name, sizeof(qentry->isr_name), |
| 3710 | "%s%d-msix %s", DRV_NAME, skdev->devno, |
| 3711 | msix_entries[i].name); |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3712 | |
| 3713 | rc = devm_request_irq(&skdev->pdev->dev, |
| 3714 | pci_irq_vector(skdev->pdev, i), |
| 3715 | msix_entries[i].handler, 0, |
| 3716 | qentry->isr_name, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3717 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3718 | dev_err(&skdev->pdev->dev, |
| 3719 | "Unable to register(%d) MSI-X handler %d: %s\n", |
| 3720 | rc, i, qentry->isr_name); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3721 | goto msix_out; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3722 | } |
| 3723 | } |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3724 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3725 | dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n", |
| 3726 | SKD_MAX_MSIX_COUNT); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3727 | return 0; |
| 3728 | |
| 3729 | msix_out: |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3730 | while (--i >= 0) |
| 3731 | devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev); |
Arnd Bergmann | 3bc8492 | 2016-11-09 13:55:34 +0100 | [diff] [blame] | 3732 | out: |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3733 | kfree(skdev->msix_entries); |
| 3734 | skdev->msix_entries = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3735 | return rc; |
| 3736 | } |
| 3737 | |
| 3738 | static int skd_acquire_irq(struct skd_device *skdev) |
| 3739 | { |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3740 | struct pci_dev *pdev = skdev->pdev; |
| 3741 | unsigned int irq_flag = PCI_IRQ_LEGACY; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3742 | int rc; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3743 | |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3744 | if (skd_isr_type == SKD_IRQ_MSIX) { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3745 | rc = skd_acquire_msix(skdev); |
| 3746 | if (!rc) |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3747 | return 0; |
| 3748 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3749 | dev_err(&skdev->pdev->dev, |
| 3750 | "failed to enable MSI-X, re-trying with MSI %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3751 | } |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3752 | |
| 3753 | snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME, |
| 3754 | skdev->devno); |
| 3755 | |
| 3756 | if (skd_isr_type != SKD_IRQ_LEGACY) |
| 3757 | irq_flag |= PCI_IRQ_MSI; |
| 3758 | rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag); |
| 3759 | if (rc < 0) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3760 | dev_err(&skdev->pdev->dev, |
| 3761 | "failed to allocate the MSI interrupt %d\n", rc); |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3762 | return rc; |
| 3763 | } |
| 3764 | |
| 3765 | rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr, |
| 3766 | pdev->msi_enabled ? 0 : IRQF_SHARED, |
| 3767 | skdev->isr_name, skdev); |
| 3768 | if (rc) { |
| 3769 | pci_free_irq_vectors(pdev); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3770 | dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n", |
| 3771 | rc); |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3772 | return rc; |
| 3773 | } |
| 3774 | |
| 3775 | return 0; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | static void skd_release_irq(struct skd_device *skdev) |
| 3779 | { |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3780 | struct pci_dev *pdev = skdev->pdev; |
| 3781 | |
| 3782 | if (skdev->msix_entries) { |
| 3783 | int i; |
| 3784 | |
| 3785 | for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) { |
| 3786 | devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), |
| 3787 | skdev); |
| 3788 | } |
| 3789 | |
| 3790 | kfree(skdev->msix_entries); |
| 3791 | skdev->msix_entries = NULL; |
| 3792 | } else { |
| 3793 | devm_free_irq(&pdev->dev, pdev->irq, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3794 | } |
Christoph Hellwig | 180b0ae | 2016-11-07 11:14:07 -0800 | [diff] [blame] | 3795 | |
| 3796 | pci_free_irq_vectors(pdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | /* |
| 3800 | ***************************************************************************** |
| 3801 | * CONSTRUCT |
| 3802 | ***************************************************************************** |
| 3803 | */ |
| 3804 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3805 | static int skd_cons_skcomp(struct skd_device *skdev) |
| 3806 | { |
| 3807 | int rc = 0; |
| 3808 | struct fit_completion_entry_v1 *skcomp; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3809 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3810 | dev_dbg(&skdev->pdev->dev, |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 3811 | "comp pci_alloc, total bytes %zd entries %d\n", |
| 3812 | SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3813 | |
Bart Van Assche | 6f7c767 | 2017-08-17 13:13:02 -0700 | [diff] [blame] | 3814 | skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE, |
Joe Perches | a5bbf61 | 2014-08-08 14:24:12 -0700 | [diff] [blame] | 3815 | &skdev->cq_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3816 | |
| 3817 | if (skcomp == NULL) { |
| 3818 | rc = -ENOMEM; |
| 3819 | goto err_out; |
| 3820 | } |
| 3821 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3822 | skdev->skcomp_table = skcomp; |
| 3823 | skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp + |
| 3824 | sizeof(*skcomp) * |
| 3825 | SKD_N_COMPLETION_ENTRY); |
| 3826 | |
| 3827 | err_out: |
| 3828 | return rc; |
| 3829 | } |
| 3830 | |
| 3831 | static int skd_cons_skmsg(struct skd_device *skdev) |
| 3832 | { |
| 3833 | int rc = 0; |
| 3834 | u32 i; |
| 3835 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3836 | dev_dbg(&skdev->pdev->dev, |
| 3837 | "skmsg_table kzalloc, struct %lu, count %u total %lu\n", |
| 3838 | sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context, |
| 3839 | sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3840 | |
| 3841 | skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context) |
| 3842 | *skdev->num_fitmsg_context, GFP_KERNEL); |
| 3843 | if (skdev->skmsg_table == NULL) { |
| 3844 | rc = -ENOMEM; |
| 3845 | goto err_out; |
| 3846 | } |
| 3847 | |
| 3848 | for (i = 0; i < skdev->num_fitmsg_context; i++) { |
| 3849 | struct skd_fitmsg_context *skmsg; |
| 3850 | |
| 3851 | skmsg = &skdev->skmsg_table[i]; |
| 3852 | |
| 3853 | skmsg->id = i + SKD_ID_FIT_MSG; |
| 3854 | |
| 3855 | skmsg->state = SKD_MSG_STATE_IDLE; |
| 3856 | skmsg->msg_buf = pci_alloc_consistent(skdev->pdev, |
| 3857 | SKD_N_FITMSG_BYTES + 64, |
| 3858 | &skmsg->mb_dma_address); |
| 3859 | |
| 3860 | if (skmsg->msg_buf == NULL) { |
| 3861 | rc = -ENOMEM; |
| 3862 | goto err_out; |
| 3863 | } |
| 3864 | |
| 3865 | skmsg->offset = (u32)((u64)skmsg->msg_buf & |
| 3866 | (~FIT_QCMD_BASE_ADDRESS_MASK)); |
| 3867 | skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK; |
| 3868 | skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf & |
| 3869 | FIT_QCMD_BASE_ADDRESS_MASK); |
| 3870 | skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK; |
| 3871 | skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK; |
| 3872 | memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES); |
| 3873 | |
| 3874 | skmsg->next = &skmsg[1]; |
| 3875 | } |
| 3876 | |
| 3877 | /* Free list is in order starting with the 0th entry. */ |
| 3878 | skdev->skmsg_table[i - 1].next = NULL; |
| 3879 | skdev->skmsg_free_list = skdev->skmsg_table; |
| 3880 | |
| 3881 | err_out: |
| 3882 | return rc; |
| 3883 | } |
| 3884 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 3885 | static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev, |
| 3886 | u32 n_sg, |
| 3887 | dma_addr_t *ret_dma_addr) |
| 3888 | { |
| 3889 | struct fit_sg_descriptor *sg_list; |
| 3890 | u32 nbytes; |
| 3891 | |
| 3892 | nbytes = sizeof(*sg_list) * n_sg; |
| 3893 | |
| 3894 | sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr); |
| 3895 | |
| 3896 | if (sg_list != NULL) { |
| 3897 | uint64_t dma_address = *ret_dma_addr; |
| 3898 | u32 i; |
| 3899 | |
| 3900 | memset(sg_list, 0, nbytes); |
| 3901 | |
| 3902 | for (i = 0; i < n_sg - 1; i++) { |
| 3903 | uint64_t ndp_off; |
| 3904 | ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor); |
| 3905 | |
| 3906 | sg_list[i].next_desc_ptr = dma_address + ndp_off; |
| 3907 | } |
| 3908 | sg_list[i].next_desc_ptr = 0LL; |
| 3909 | } |
| 3910 | |
| 3911 | return sg_list; |
| 3912 | } |
| 3913 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3914 | static int skd_cons_skreq(struct skd_device *skdev) |
| 3915 | { |
| 3916 | int rc = 0; |
| 3917 | u32 i; |
| 3918 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3919 | dev_dbg(&skdev->pdev->dev, |
| 3920 | "skreq_table kzalloc, struct %lu, count %u total %lu\n", |
| 3921 | sizeof(struct skd_request_context), skdev->num_req_context, |
| 3922 | sizeof(struct skd_request_context) * skdev->num_req_context); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3923 | |
| 3924 | skdev->skreq_table = kzalloc(sizeof(struct skd_request_context) |
| 3925 | * skdev->num_req_context, GFP_KERNEL); |
| 3926 | if (skdev->skreq_table == NULL) { |
| 3927 | rc = -ENOMEM; |
| 3928 | goto err_out; |
| 3929 | } |
| 3930 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3931 | dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n", |
| 3932 | skdev->sgs_per_request, sizeof(struct scatterlist), |
| 3933 | skdev->sgs_per_request * sizeof(struct scatterlist)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3934 | |
| 3935 | for (i = 0; i < skdev->num_req_context; i++) { |
| 3936 | struct skd_request_context *skreq; |
| 3937 | |
| 3938 | skreq = &skdev->skreq_table[i]; |
| 3939 | |
| 3940 | skreq->id = i + SKD_ID_RW_REQUEST; |
| 3941 | skreq->state = SKD_REQ_STATE_IDLE; |
| 3942 | |
| 3943 | skreq->sg = kzalloc(sizeof(struct scatterlist) * |
| 3944 | skdev->sgs_per_request, GFP_KERNEL); |
| 3945 | if (skreq->sg == NULL) { |
| 3946 | rc = -ENOMEM; |
| 3947 | goto err_out; |
| 3948 | } |
| 3949 | sg_init_table(skreq->sg, skdev->sgs_per_request); |
| 3950 | |
| 3951 | skreq->sksg_list = skd_cons_sg_list(skdev, |
| 3952 | skdev->sgs_per_request, |
| 3953 | &skreq->sksg_dma_address); |
| 3954 | |
| 3955 | if (skreq->sksg_list == NULL) { |
| 3956 | rc = -ENOMEM; |
| 3957 | goto err_out; |
| 3958 | } |
| 3959 | |
| 3960 | skreq->next = &skreq[1]; |
| 3961 | } |
| 3962 | |
| 3963 | /* Free list is in order starting with the 0th entry. */ |
| 3964 | skdev->skreq_table[i - 1].next = NULL; |
| 3965 | skdev->skreq_free_list = skdev->skreq_table; |
| 3966 | |
| 3967 | err_out: |
| 3968 | return rc; |
| 3969 | } |
| 3970 | |
| 3971 | static int skd_cons_skspcl(struct skd_device *skdev) |
| 3972 | { |
| 3973 | int rc = 0; |
| 3974 | u32 i, nbytes; |
| 3975 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 3976 | dev_dbg(&skdev->pdev->dev, |
| 3977 | "skspcl_table kzalloc, struct %lu, count %u total %lu\n", |
| 3978 | sizeof(struct skd_special_context), skdev->n_special, |
| 3979 | sizeof(struct skd_special_context) * skdev->n_special); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 3980 | |
| 3981 | skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context) |
| 3982 | * skdev->n_special, GFP_KERNEL); |
| 3983 | if (skdev->skspcl_table == NULL) { |
| 3984 | rc = -ENOMEM; |
| 3985 | goto err_out; |
| 3986 | } |
| 3987 | |
| 3988 | for (i = 0; i < skdev->n_special; i++) { |
| 3989 | struct skd_special_context *skspcl; |
| 3990 | |
| 3991 | skspcl = &skdev->skspcl_table[i]; |
| 3992 | |
| 3993 | skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST; |
| 3994 | skspcl->req.state = SKD_REQ_STATE_IDLE; |
| 3995 | |
| 3996 | skspcl->req.next = &skspcl[1].req; |
| 3997 | |
| 3998 | nbytes = SKD_N_SPECIAL_FITMSG_BYTES; |
| 3999 | |
Joe Perches | a5bbf61 | 2014-08-08 14:24:12 -0700 | [diff] [blame] | 4000 | skspcl->msg_buf = |
| 4001 | pci_zalloc_consistent(skdev->pdev, nbytes, |
| 4002 | &skspcl->mb_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4003 | if (skspcl->msg_buf == NULL) { |
| 4004 | rc = -ENOMEM; |
| 4005 | goto err_out; |
| 4006 | } |
| 4007 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4008 | skspcl->req.sg = kzalloc(sizeof(struct scatterlist) * |
| 4009 | SKD_N_SG_PER_SPECIAL, GFP_KERNEL); |
| 4010 | if (skspcl->req.sg == NULL) { |
| 4011 | rc = -ENOMEM; |
| 4012 | goto err_out; |
| 4013 | } |
| 4014 | |
| 4015 | skspcl->req.sksg_list = skd_cons_sg_list(skdev, |
| 4016 | SKD_N_SG_PER_SPECIAL, |
| 4017 | &skspcl->req. |
| 4018 | sksg_dma_address); |
| 4019 | if (skspcl->req.sksg_list == NULL) { |
| 4020 | rc = -ENOMEM; |
| 4021 | goto err_out; |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | /* Free list is in order starting with the 0th entry. */ |
| 4026 | skdev->skspcl_table[i - 1].req.next = NULL; |
| 4027 | skdev->skspcl_free_list = skdev->skspcl_table; |
| 4028 | |
| 4029 | return rc; |
| 4030 | |
| 4031 | err_out: |
| 4032 | return rc; |
| 4033 | } |
| 4034 | |
| 4035 | static int skd_cons_sksb(struct skd_device *skdev) |
| 4036 | { |
| 4037 | int rc = 0; |
| 4038 | struct skd_special_context *skspcl; |
| 4039 | u32 nbytes; |
| 4040 | |
| 4041 | skspcl = &skdev->internal_skspcl; |
| 4042 | |
| 4043 | skspcl->req.id = 0 + SKD_ID_INTERNAL; |
| 4044 | skspcl->req.state = SKD_REQ_STATE_IDLE; |
| 4045 | |
| 4046 | nbytes = SKD_N_INTERNAL_BYTES; |
| 4047 | |
Joe Perches | a5bbf61 | 2014-08-08 14:24:12 -0700 | [diff] [blame] | 4048 | skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes, |
| 4049 | &skspcl->db_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4050 | if (skspcl->data_buf == NULL) { |
| 4051 | rc = -ENOMEM; |
| 4052 | goto err_out; |
| 4053 | } |
| 4054 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4055 | nbytes = SKD_N_SPECIAL_FITMSG_BYTES; |
Joe Perches | a5bbf61 | 2014-08-08 14:24:12 -0700 | [diff] [blame] | 4056 | skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes, |
| 4057 | &skspcl->mb_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4058 | if (skspcl->msg_buf == NULL) { |
| 4059 | rc = -ENOMEM; |
| 4060 | goto err_out; |
| 4061 | } |
| 4062 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4063 | skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1, |
| 4064 | &skspcl->req.sksg_dma_address); |
| 4065 | if (skspcl->req.sksg_list == NULL) { |
| 4066 | rc = -ENOMEM; |
| 4067 | goto err_out; |
| 4068 | } |
| 4069 | |
| 4070 | if (!skd_format_internal_skspcl(skdev)) { |
| 4071 | rc = -EINVAL; |
| 4072 | goto err_out; |
| 4073 | } |
| 4074 | |
| 4075 | err_out: |
| 4076 | return rc; |
| 4077 | } |
| 4078 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4079 | static int skd_cons_disk(struct skd_device *skdev) |
| 4080 | { |
| 4081 | int rc = 0; |
| 4082 | struct gendisk *disk; |
| 4083 | struct request_queue *q; |
| 4084 | unsigned long flags; |
| 4085 | |
| 4086 | disk = alloc_disk(SKD_MINORS_PER_DEVICE); |
| 4087 | if (!disk) { |
| 4088 | rc = -ENOMEM; |
| 4089 | goto err_out; |
| 4090 | } |
| 4091 | |
| 4092 | skdev->disk = disk; |
| 4093 | sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno); |
| 4094 | |
| 4095 | disk->major = skdev->major; |
| 4096 | disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE; |
| 4097 | disk->fops = &skd_blockdev_ops; |
| 4098 | disk->private_data = skdev; |
| 4099 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 4100 | q = blk_init_queue(skd_request_fn, &skdev->lock); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4101 | if (!q) { |
| 4102 | rc = -ENOMEM; |
| 4103 | goto err_out; |
| 4104 | } |
Christoph Hellwig | 8fc4504 | 2017-06-19 09:26:26 +0200 | [diff] [blame] | 4105 | blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4106 | |
| 4107 | skdev->queue = q; |
| 4108 | disk->queue = q; |
| 4109 | q->queuedata = skdev; |
| 4110 | |
Jens Axboe | 6975f73 | 2016-03-30 10:11:42 -0600 | [diff] [blame] | 4111 | blk_queue_write_cache(q, true, true); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4112 | blk_queue_max_segments(q, skdev->sgs_per_request); |
| 4113 | blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS); |
| 4114 | |
Bart Van Assche | a5c5b39 | 2017-08-17 13:12:53 -0700 | [diff] [blame] | 4115 | /* set optimal I/O size to 8KB */ |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4116 | blk_queue_io_opt(q, 8192); |
| 4117 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4118 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 4119 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4120 | |
| 4121 | spin_lock_irqsave(&skdev->lock, flags); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4122 | dev_dbg(&skdev->pdev->dev, "stopping queue\n"); |
Jens Axboe | 6a5ec65 | 2013-11-01 10:38:45 -0600 | [diff] [blame] | 4123 | blk_stop_queue(skdev->queue); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4124 | spin_unlock_irqrestore(&skdev->lock, flags); |
| 4125 | |
| 4126 | err_out: |
| 4127 | return rc; |
| 4128 | } |
| 4129 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4130 | #define SKD_N_DEV_TABLE 16u |
| 4131 | static u32 skd_next_devno; |
| 4132 | |
| 4133 | static struct skd_device *skd_construct(struct pci_dev *pdev) |
| 4134 | { |
| 4135 | struct skd_device *skdev; |
| 4136 | int blk_major = skd_major; |
| 4137 | int rc; |
| 4138 | |
| 4139 | skdev = kzalloc(sizeof(*skdev), GFP_KERNEL); |
| 4140 | |
| 4141 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4142 | dev_err(&pdev->dev, "memory alloc failure\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4143 | return NULL; |
| 4144 | } |
| 4145 | |
| 4146 | skdev->state = SKD_DRVR_STATE_LOAD; |
| 4147 | skdev->pdev = pdev; |
| 4148 | skdev->devno = skd_next_devno++; |
| 4149 | skdev->major = blk_major; |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4150 | skdev->dev_max_queue_depth = 0; |
| 4151 | |
| 4152 | skdev->num_req_context = skd_max_queue_depth; |
| 4153 | skdev->num_fitmsg_context = skd_max_queue_depth; |
| 4154 | skdev->n_special = skd_max_pass_thru; |
| 4155 | skdev->cur_max_queue_depth = 1; |
| 4156 | skdev->queue_low_water_mark = 1; |
| 4157 | skdev->proto_ver = 99; |
| 4158 | skdev->sgs_per_request = skd_sgs_per_request; |
| 4159 | skdev->dbg_level = skd_dbg_level; |
| 4160 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4161 | spin_lock_init(&skdev->lock); |
| 4162 | |
| 4163 | INIT_WORK(&skdev->completion_worker, skd_completion_worker); |
| 4164 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4165 | dev_dbg(&skdev->pdev->dev, "skcomp\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4166 | rc = skd_cons_skcomp(skdev); |
| 4167 | if (rc < 0) |
| 4168 | goto err_out; |
| 4169 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4170 | dev_dbg(&skdev->pdev->dev, "skmsg\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4171 | rc = skd_cons_skmsg(skdev); |
| 4172 | if (rc < 0) |
| 4173 | goto err_out; |
| 4174 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4175 | dev_dbg(&skdev->pdev->dev, "skreq\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4176 | rc = skd_cons_skreq(skdev); |
| 4177 | if (rc < 0) |
| 4178 | goto err_out; |
| 4179 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4180 | dev_dbg(&skdev->pdev->dev, "skspcl\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4181 | rc = skd_cons_skspcl(skdev); |
| 4182 | if (rc < 0) |
| 4183 | goto err_out; |
| 4184 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4185 | dev_dbg(&skdev->pdev->dev, "sksb\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4186 | rc = skd_cons_sksb(skdev); |
| 4187 | if (rc < 0) |
| 4188 | goto err_out; |
| 4189 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4190 | dev_dbg(&skdev->pdev->dev, "disk\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4191 | rc = skd_cons_disk(skdev); |
| 4192 | if (rc < 0) |
| 4193 | goto err_out; |
| 4194 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4195 | dev_dbg(&skdev->pdev->dev, "VICTORY\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4196 | return skdev; |
| 4197 | |
| 4198 | err_out: |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4199 | dev_dbg(&skdev->pdev->dev, "construct failed\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4200 | skd_destruct(skdev); |
| 4201 | return NULL; |
| 4202 | } |
| 4203 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4204 | /* |
| 4205 | ***************************************************************************** |
| 4206 | * DESTRUCT (FREE) |
| 4207 | ***************************************************************************** |
| 4208 | */ |
| 4209 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4210 | static void skd_free_skcomp(struct skd_device *skdev) |
| 4211 | { |
Bart Van Assche | 7f13bda | 2017-08-17 13:13:03 -0700 | [diff] [blame] | 4212 | if (skdev->skcomp_table) |
| 4213 | pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE, |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4214 | skdev->skcomp_table, skdev->cq_dma_address); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4215 | |
| 4216 | skdev->skcomp_table = NULL; |
| 4217 | skdev->cq_dma_address = 0; |
| 4218 | } |
| 4219 | |
| 4220 | static void skd_free_skmsg(struct skd_device *skdev) |
| 4221 | { |
| 4222 | u32 i; |
| 4223 | |
| 4224 | if (skdev->skmsg_table == NULL) |
| 4225 | return; |
| 4226 | |
| 4227 | for (i = 0; i < skdev->num_fitmsg_context; i++) { |
| 4228 | struct skd_fitmsg_context *skmsg; |
| 4229 | |
| 4230 | skmsg = &skdev->skmsg_table[i]; |
| 4231 | |
| 4232 | if (skmsg->msg_buf != NULL) { |
| 4233 | skmsg->msg_buf += skmsg->offset; |
| 4234 | skmsg->mb_dma_address += skmsg->offset; |
| 4235 | pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES, |
| 4236 | skmsg->msg_buf, |
| 4237 | skmsg->mb_dma_address); |
| 4238 | } |
| 4239 | skmsg->msg_buf = NULL; |
| 4240 | skmsg->mb_dma_address = 0; |
| 4241 | } |
| 4242 | |
| 4243 | kfree(skdev->skmsg_table); |
| 4244 | skdev->skmsg_table = NULL; |
| 4245 | } |
| 4246 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4247 | static void skd_free_sg_list(struct skd_device *skdev, |
| 4248 | struct fit_sg_descriptor *sg_list, |
| 4249 | u32 n_sg, dma_addr_t dma_addr) |
| 4250 | { |
| 4251 | if (sg_list != NULL) { |
| 4252 | u32 nbytes; |
| 4253 | |
| 4254 | nbytes = sizeof(*sg_list) * n_sg; |
| 4255 | |
| 4256 | pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr); |
| 4257 | } |
| 4258 | } |
| 4259 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4260 | static void skd_free_skreq(struct skd_device *skdev) |
| 4261 | { |
| 4262 | u32 i; |
| 4263 | |
| 4264 | if (skdev->skreq_table == NULL) |
| 4265 | return; |
| 4266 | |
| 4267 | for (i = 0; i < skdev->num_req_context; i++) { |
| 4268 | struct skd_request_context *skreq; |
| 4269 | |
| 4270 | skreq = &skdev->skreq_table[i]; |
| 4271 | |
| 4272 | skd_free_sg_list(skdev, skreq->sksg_list, |
| 4273 | skdev->sgs_per_request, |
| 4274 | skreq->sksg_dma_address); |
| 4275 | |
| 4276 | skreq->sksg_list = NULL; |
| 4277 | skreq->sksg_dma_address = 0; |
| 4278 | |
| 4279 | kfree(skreq->sg); |
| 4280 | } |
| 4281 | |
| 4282 | kfree(skdev->skreq_table); |
| 4283 | skdev->skreq_table = NULL; |
| 4284 | } |
| 4285 | |
| 4286 | static void skd_free_skspcl(struct skd_device *skdev) |
| 4287 | { |
| 4288 | u32 i; |
| 4289 | u32 nbytes; |
| 4290 | |
| 4291 | if (skdev->skspcl_table == NULL) |
| 4292 | return; |
| 4293 | |
| 4294 | for (i = 0; i < skdev->n_special; i++) { |
| 4295 | struct skd_special_context *skspcl; |
| 4296 | |
| 4297 | skspcl = &skdev->skspcl_table[i]; |
| 4298 | |
| 4299 | if (skspcl->msg_buf != NULL) { |
| 4300 | nbytes = SKD_N_SPECIAL_FITMSG_BYTES; |
| 4301 | pci_free_consistent(skdev->pdev, nbytes, |
| 4302 | skspcl->msg_buf, |
| 4303 | skspcl->mb_dma_address); |
| 4304 | } |
| 4305 | |
| 4306 | skspcl->msg_buf = NULL; |
| 4307 | skspcl->mb_dma_address = 0; |
| 4308 | |
| 4309 | skd_free_sg_list(skdev, skspcl->req.sksg_list, |
| 4310 | SKD_N_SG_PER_SPECIAL, |
| 4311 | skspcl->req.sksg_dma_address); |
| 4312 | |
| 4313 | skspcl->req.sksg_list = NULL; |
| 4314 | skspcl->req.sksg_dma_address = 0; |
| 4315 | |
| 4316 | kfree(skspcl->req.sg); |
| 4317 | } |
| 4318 | |
| 4319 | kfree(skdev->skspcl_table); |
| 4320 | skdev->skspcl_table = NULL; |
| 4321 | } |
| 4322 | |
| 4323 | static void skd_free_sksb(struct skd_device *skdev) |
| 4324 | { |
| 4325 | struct skd_special_context *skspcl; |
| 4326 | u32 nbytes; |
| 4327 | |
| 4328 | skspcl = &skdev->internal_skspcl; |
| 4329 | |
| 4330 | if (skspcl->data_buf != NULL) { |
| 4331 | nbytes = SKD_N_INTERNAL_BYTES; |
| 4332 | |
| 4333 | pci_free_consistent(skdev->pdev, nbytes, |
| 4334 | skspcl->data_buf, skspcl->db_dma_address); |
| 4335 | } |
| 4336 | |
| 4337 | skspcl->data_buf = NULL; |
| 4338 | skspcl->db_dma_address = 0; |
| 4339 | |
| 4340 | if (skspcl->msg_buf != NULL) { |
| 4341 | nbytes = SKD_N_SPECIAL_FITMSG_BYTES; |
| 4342 | pci_free_consistent(skdev->pdev, nbytes, |
| 4343 | skspcl->msg_buf, skspcl->mb_dma_address); |
| 4344 | } |
| 4345 | |
| 4346 | skspcl->msg_buf = NULL; |
| 4347 | skspcl->mb_dma_address = 0; |
| 4348 | |
| 4349 | skd_free_sg_list(skdev, skspcl->req.sksg_list, 1, |
| 4350 | skspcl->req.sksg_dma_address); |
| 4351 | |
| 4352 | skspcl->req.sksg_list = NULL; |
| 4353 | skspcl->req.sksg_dma_address = 0; |
| 4354 | } |
| 4355 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4356 | static void skd_free_disk(struct skd_device *skdev) |
| 4357 | { |
| 4358 | struct gendisk *disk = skdev->disk; |
| 4359 | |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 4360 | if (disk && (disk->flags & GENHD_FL_UP)) |
| 4361 | del_gendisk(disk); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4362 | |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 4363 | if (skdev->queue) { |
| 4364 | blk_cleanup_queue(skdev->queue); |
| 4365 | skdev->queue = NULL; |
| 4366 | disk->queue = NULL; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4367 | } |
Bart Van Assche | 7277cc6 | 2017-08-17 13:12:45 -0700 | [diff] [blame] | 4368 | |
| 4369 | put_disk(disk); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4370 | skdev->disk = NULL; |
| 4371 | } |
| 4372 | |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4373 | static void skd_destruct(struct skd_device *skdev) |
| 4374 | { |
| 4375 | if (skdev == NULL) |
| 4376 | return; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4377 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4378 | dev_dbg(&skdev->pdev->dev, "disk\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4379 | skd_free_disk(skdev); |
| 4380 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4381 | dev_dbg(&skdev->pdev->dev, "sksb\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4382 | skd_free_sksb(skdev); |
| 4383 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4384 | dev_dbg(&skdev->pdev->dev, "skspcl\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4385 | skd_free_skspcl(skdev); |
| 4386 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4387 | dev_dbg(&skdev->pdev->dev, "skreq\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4388 | skd_free_skreq(skdev); |
| 4389 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4390 | dev_dbg(&skdev->pdev->dev, "skmsg\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4391 | skd_free_skmsg(skdev); |
| 4392 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4393 | dev_dbg(&skdev->pdev->dev, "skcomp\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4394 | skd_free_skcomp(skdev); |
| 4395 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4396 | dev_dbg(&skdev->pdev->dev, "skdev\n"); |
Bartlomiej Zolnierkiewicz | 542d7b0 | 2013-11-05 12:37:08 +0100 | [diff] [blame] | 4397 | kfree(skdev); |
| 4398 | } |
| 4399 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4400 | /* |
| 4401 | ***************************************************************************** |
| 4402 | * BLOCK DEVICE (BDEV) GLUE |
| 4403 | ***************************************************************************** |
| 4404 | */ |
| 4405 | |
| 4406 | static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4407 | { |
| 4408 | struct skd_device *skdev; |
| 4409 | u64 capacity; |
| 4410 | |
| 4411 | skdev = bdev->bd_disk->private_data; |
| 4412 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4413 | dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n", |
| 4414 | bdev->bd_disk->disk_name, current->comm); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4415 | |
| 4416 | if (skdev->read_cap_is_valid) { |
| 4417 | capacity = get_capacity(skdev->disk); |
| 4418 | geo->heads = 64; |
| 4419 | geo->sectors = 255; |
| 4420 | geo->cylinders = (capacity) / (255 * 64); |
| 4421 | |
| 4422 | return 0; |
| 4423 | } |
| 4424 | return -EIO; |
| 4425 | } |
| 4426 | |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 4427 | static int skd_bdev_attach(struct device *parent, struct skd_device *skdev) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4428 | { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4429 | dev_dbg(&skdev->pdev->dev, "add_disk\n"); |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 4430 | device_add_disk(parent, skdev->disk); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4431 | return 0; |
| 4432 | } |
| 4433 | |
| 4434 | static const struct block_device_operations skd_blockdev_ops = { |
| 4435 | .owner = THIS_MODULE, |
| 4436 | .ioctl = skd_bdev_ioctl, |
| 4437 | .getgeo = skd_bdev_getgeo, |
| 4438 | }; |
| 4439 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4440 | /* |
| 4441 | ***************************************************************************** |
| 4442 | * PCIe DRIVER GLUE |
| 4443 | ***************************************************************************** |
| 4444 | */ |
| 4445 | |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 4446 | static const struct pci_device_id skd_pci_tbl[] = { |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4447 | { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120, |
| 4448 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, }, |
| 4449 | { 0 } /* terminate list */ |
| 4450 | }; |
| 4451 | |
| 4452 | MODULE_DEVICE_TABLE(pci, skd_pci_tbl); |
| 4453 | |
| 4454 | static char *skd_pci_info(struct skd_device *skdev, char *str) |
| 4455 | { |
| 4456 | int pcie_reg; |
| 4457 | |
| 4458 | strcpy(str, "PCIe ("); |
| 4459 | pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP); |
| 4460 | |
| 4461 | if (pcie_reg) { |
| 4462 | |
| 4463 | char lwstr[6]; |
| 4464 | uint16_t pcie_lstat, lspeed, lwidth; |
| 4465 | |
| 4466 | pcie_reg += 0x12; |
| 4467 | pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat); |
| 4468 | lspeed = pcie_lstat & (0xF); |
| 4469 | lwidth = (pcie_lstat & 0x3F0) >> 4; |
| 4470 | |
| 4471 | if (lspeed == 1) |
| 4472 | strcat(str, "2.5GT/s "); |
| 4473 | else if (lspeed == 2) |
| 4474 | strcat(str, "5.0GT/s "); |
| 4475 | else |
| 4476 | strcat(str, "<unknown> "); |
| 4477 | snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth); |
| 4478 | strcat(str, lwstr); |
| 4479 | } |
| 4480 | return str; |
| 4481 | } |
| 4482 | |
| 4483 | static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 4484 | { |
| 4485 | int i; |
| 4486 | int rc = 0; |
| 4487 | char pci_str[32]; |
| 4488 | struct skd_device *skdev; |
| 4489 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4490 | dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n", |
| 4491 | DRV_NAME, DRV_VERSION, DRV_BUILD_ID); |
| 4492 | dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor, |
| 4493 | pdev->device); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4494 | |
| 4495 | rc = pci_enable_device(pdev); |
| 4496 | if (rc) |
| 4497 | return rc; |
| 4498 | rc = pci_request_regions(pdev, DRV_NAME); |
| 4499 | if (rc) |
| 4500 | goto err_out; |
| 4501 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 4502 | if (!rc) { |
| 4503 | if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4504 | dev_err(&pdev->dev, "consistent DMA mask error %d\n", |
| 4505 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4506 | } |
| 4507 | } else { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4508 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4509 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4510 | dev_err(&pdev->dev, "DMA mask error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4511 | goto err_out_regions; |
| 4512 | } |
| 4513 | } |
| 4514 | |
Bartlomiej Zolnierkiewicz | b8df664 | 2013-11-05 12:37:02 +0100 | [diff] [blame] | 4515 | if (!skd_major) { |
| 4516 | rc = register_blkdev(0, DRV_NAME); |
| 4517 | if (rc < 0) |
| 4518 | goto err_out_regions; |
| 4519 | BUG_ON(!rc); |
| 4520 | skd_major = rc; |
| 4521 | } |
| 4522 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4523 | skdev = skd_construct(pdev); |
Wei Yongjun | 1762b57 | 2013-10-30 13:23:53 +0800 | [diff] [blame] | 4524 | if (skdev == NULL) { |
| 4525 | rc = -ENOMEM; |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4526 | goto err_out_regions; |
Wei Yongjun | 1762b57 | 2013-10-30 13:23:53 +0800 | [diff] [blame] | 4527 | } |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4528 | |
| 4529 | skd_pci_info(skdev, pci_str); |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4530 | dev_info(&pdev->dev, "%s 64bit\n", pci_str); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4531 | |
| 4532 | pci_set_master(pdev); |
| 4533 | rc = pci_enable_pcie_error_reporting(pdev); |
| 4534 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4535 | dev_err(&pdev->dev, |
| 4536 | "bad enable of PCIe error reporting rc=%d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4537 | skdev->pcie_error_reporting_is_enabled = 0; |
| 4538 | } else |
| 4539 | skdev->pcie_error_reporting_is_enabled = 1; |
| 4540 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4541 | pci_set_drvdata(pdev, skdev); |
Bartlomiej Zolnierkiewicz | ebedd16 | 2013-11-05 12:37:05 +0100 | [diff] [blame] | 4542 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4543 | for (i = 0; i < SKD_MAX_BARS; i++) { |
| 4544 | skdev->mem_phys[i] = pci_resource_start(pdev, i); |
| 4545 | skdev->mem_size[i] = (u32)pci_resource_len(pdev, i); |
| 4546 | skdev->mem_map[i] = ioremap(skdev->mem_phys[i], |
| 4547 | skdev->mem_size[i]); |
| 4548 | if (!skdev->mem_map[i]) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4549 | dev_err(&pdev->dev, |
| 4550 | "Unable to map adapter memory!\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4551 | rc = -ENODEV; |
| 4552 | goto err_out_iounmap; |
| 4553 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4554 | dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n", |
| 4555 | skdev->mem_map[i], (uint64_t)skdev->mem_phys[i], |
| 4556 | skdev->mem_size[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | rc = skd_acquire_irq(skdev); |
| 4560 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4561 | dev_err(&pdev->dev, "interrupt resource error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4562 | goto err_out_iounmap; |
| 4563 | } |
| 4564 | |
| 4565 | rc = skd_start_timer(skdev); |
| 4566 | if (rc) |
| 4567 | goto err_out_timer; |
| 4568 | |
| 4569 | init_waitqueue_head(&skdev->waitq); |
| 4570 | |
| 4571 | skd_start_device(skdev); |
| 4572 | |
| 4573 | rc = wait_event_interruptible_timeout(skdev->waitq, |
| 4574 | (skdev->gendisk_on), |
| 4575 | (SKD_START_WAIT_SECONDS * HZ)); |
| 4576 | if (skdev->gendisk_on > 0) { |
| 4577 | /* device came on-line after reset */ |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 4578 | skd_bdev_attach(&pdev->dev, skdev); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4579 | rc = 0; |
| 4580 | } else { |
| 4581 | /* we timed out, something is wrong with the device, |
| 4582 | don't add the disk structure */ |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4583 | dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n", |
| 4584 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4585 | /* in case of no error; we timeout with ENXIO */ |
| 4586 | if (!rc) |
| 4587 | rc = -ENXIO; |
| 4588 | goto err_out_timer; |
| 4589 | } |
| 4590 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4591 | return rc; |
| 4592 | |
| 4593 | err_out_timer: |
| 4594 | skd_stop_device(skdev); |
| 4595 | skd_release_irq(skdev); |
| 4596 | |
| 4597 | err_out_iounmap: |
| 4598 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 4599 | if (skdev->mem_map[i]) |
| 4600 | iounmap(skdev->mem_map[i]); |
| 4601 | |
| 4602 | if (skdev->pcie_error_reporting_is_enabled) |
| 4603 | pci_disable_pcie_error_reporting(pdev); |
| 4604 | |
| 4605 | skd_destruct(skdev); |
| 4606 | |
| 4607 | err_out_regions: |
| 4608 | pci_release_regions(pdev); |
| 4609 | |
| 4610 | err_out: |
| 4611 | pci_disable_device(pdev); |
| 4612 | pci_set_drvdata(pdev, NULL); |
| 4613 | return rc; |
| 4614 | } |
| 4615 | |
| 4616 | static void skd_pci_remove(struct pci_dev *pdev) |
| 4617 | { |
| 4618 | int i; |
| 4619 | struct skd_device *skdev; |
| 4620 | |
| 4621 | skdev = pci_get_drvdata(pdev); |
| 4622 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4623 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4624 | return; |
| 4625 | } |
| 4626 | skd_stop_device(skdev); |
| 4627 | skd_release_irq(skdev); |
| 4628 | |
| 4629 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 4630 | if (skdev->mem_map[i]) |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 4631 | iounmap(skdev->mem_map[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4632 | |
| 4633 | if (skdev->pcie_error_reporting_is_enabled) |
| 4634 | pci_disable_pcie_error_reporting(pdev); |
| 4635 | |
| 4636 | skd_destruct(skdev); |
| 4637 | |
| 4638 | pci_release_regions(pdev); |
| 4639 | pci_disable_device(pdev); |
| 4640 | pci_set_drvdata(pdev, NULL); |
| 4641 | |
| 4642 | return; |
| 4643 | } |
| 4644 | |
| 4645 | static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state) |
| 4646 | { |
| 4647 | int i; |
| 4648 | struct skd_device *skdev; |
| 4649 | |
| 4650 | skdev = pci_get_drvdata(pdev); |
| 4651 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4652 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4653 | return -EIO; |
| 4654 | } |
| 4655 | |
| 4656 | skd_stop_device(skdev); |
| 4657 | |
| 4658 | skd_release_irq(skdev); |
| 4659 | |
| 4660 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 4661 | if (skdev->mem_map[i]) |
Bart Van Assche | 4854afe | 2017-08-17 13:12:59 -0700 | [diff] [blame] | 4662 | iounmap(skdev->mem_map[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4663 | |
| 4664 | if (skdev->pcie_error_reporting_is_enabled) |
| 4665 | pci_disable_pcie_error_reporting(pdev); |
| 4666 | |
| 4667 | pci_release_regions(pdev); |
| 4668 | pci_save_state(pdev); |
| 4669 | pci_disable_device(pdev); |
| 4670 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); |
| 4671 | return 0; |
| 4672 | } |
| 4673 | |
| 4674 | static int skd_pci_resume(struct pci_dev *pdev) |
| 4675 | { |
| 4676 | int i; |
| 4677 | int rc = 0; |
| 4678 | struct skd_device *skdev; |
| 4679 | |
| 4680 | skdev = pci_get_drvdata(pdev); |
| 4681 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4682 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4683 | return -1; |
| 4684 | } |
| 4685 | |
| 4686 | pci_set_power_state(pdev, PCI_D0); |
| 4687 | pci_enable_wake(pdev, PCI_D0, 0); |
| 4688 | pci_restore_state(pdev); |
| 4689 | |
| 4690 | rc = pci_enable_device(pdev); |
| 4691 | if (rc) |
| 4692 | return rc; |
| 4693 | rc = pci_request_regions(pdev, DRV_NAME); |
| 4694 | if (rc) |
| 4695 | goto err_out; |
| 4696 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 4697 | if (!rc) { |
| 4698 | if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) { |
| 4699 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4700 | dev_err(&pdev->dev, "consistent DMA mask error %d\n", |
| 4701 | rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4702 | } |
| 4703 | } else { |
| 4704 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 4705 | if (rc) { |
| 4706 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4707 | dev_err(&pdev->dev, "DMA mask error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4708 | goto err_out_regions; |
| 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | pci_set_master(pdev); |
| 4713 | rc = pci_enable_pcie_error_reporting(pdev); |
| 4714 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4715 | dev_err(&pdev->dev, |
| 4716 | "bad enable of PCIe error reporting rc=%d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4717 | skdev->pcie_error_reporting_is_enabled = 0; |
| 4718 | } else |
| 4719 | skdev->pcie_error_reporting_is_enabled = 1; |
| 4720 | |
| 4721 | for (i = 0; i < SKD_MAX_BARS; i++) { |
| 4722 | |
| 4723 | skdev->mem_phys[i] = pci_resource_start(pdev, i); |
| 4724 | skdev->mem_size[i] = (u32)pci_resource_len(pdev, i); |
| 4725 | skdev->mem_map[i] = ioremap(skdev->mem_phys[i], |
| 4726 | skdev->mem_size[i]); |
| 4727 | if (!skdev->mem_map[i]) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4728 | dev_err(&pdev->dev, "Unable to map adapter memory!\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4729 | rc = -ENODEV; |
| 4730 | goto err_out_iounmap; |
| 4731 | } |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4732 | dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n", |
| 4733 | skdev->mem_map[i], (uint64_t)skdev->mem_phys[i], |
| 4734 | skdev->mem_size[i]); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4735 | } |
| 4736 | rc = skd_acquire_irq(skdev); |
| 4737 | if (rc) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4738 | dev_err(&pdev->dev, "interrupt resource error %d\n", rc); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4739 | goto err_out_iounmap; |
| 4740 | } |
| 4741 | |
| 4742 | rc = skd_start_timer(skdev); |
| 4743 | if (rc) |
| 4744 | goto err_out_timer; |
| 4745 | |
| 4746 | init_waitqueue_head(&skdev->waitq); |
| 4747 | |
| 4748 | skd_start_device(skdev); |
| 4749 | |
| 4750 | return rc; |
| 4751 | |
| 4752 | err_out_timer: |
| 4753 | skd_stop_device(skdev); |
| 4754 | skd_release_irq(skdev); |
| 4755 | |
| 4756 | err_out_iounmap: |
| 4757 | for (i = 0; i < SKD_MAX_BARS; i++) |
| 4758 | if (skdev->mem_map[i]) |
| 4759 | iounmap(skdev->mem_map[i]); |
| 4760 | |
| 4761 | if (skdev->pcie_error_reporting_is_enabled) |
| 4762 | pci_disable_pcie_error_reporting(pdev); |
| 4763 | |
| 4764 | err_out_regions: |
| 4765 | pci_release_regions(pdev); |
| 4766 | |
| 4767 | err_out: |
| 4768 | pci_disable_device(pdev); |
| 4769 | return rc; |
| 4770 | } |
| 4771 | |
| 4772 | static void skd_pci_shutdown(struct pci_dev *pdev) |
| 4773 | { |
| 4774 | struct skd_device *skdev; |
| 4775 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4776 | dev_err(&pdev->dev, "%s called\n", __func__); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4777 | |
| 4778 | skdev = pci_get_drvdata(pdev); |
| 4779 | if (!skdev) { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4780 | dev_err(&pdev->dev, "no device data for PCI\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4781 | return; |
| 4782 | } |
| 4783 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4784 | dev_err(&pdev->dev, "calling stop\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4785 | skd_stop_device(skdev); |
| 4786 | } |
| 4787 | |
| 4788 | static struct pci_driver skd_driver = { |
| 4789 | .name = DRV_NAME, |
| 4790 | .id_table = skd_pci_tbl, |
| 4791 | .probe = skd_pci_probe, |
| 4792 | .remove = skd_pci_remove, |
| 4793 | .suspend = skd_pci_suspend, |
| 4794 | .resume = skd_pci_resume, |
| 4795 | .shutdown = skd_pci_shutdown, |
| 4796 | }; |
| 4797 | |
| 4798 | /* |
| 4799 | ***************************************************************************** |
| 4800 | * LOGGING SUPPORT |
| 4801 | ***************************************************************************** |
| 4802 | */ |
| 4803 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4804 | const char *skd_drive_state_to_str(int state) |
| 4805 | { |
| 4806 | switch (state) { |
| 4807 | case FIT_SR_DRIVE_OFFLINE: |
| 4808 | return "OFFLINE"; |
| 4809 | case FIT_SR_DRIVE_INIT: |
| 4810 | return "INIT"; |
| 4811 | case FIT_SR_DRIVE_ONLINE: |
| 4812 | return "ONLINE"; |
| 4813 | case FIT_SR_DRIVE_BUSY: |
| 4814 | return "BUSY"; |
| 4815 | case FIT_SR_DRIVE_FAULT: |
| 4816 | return "FAULT"; |
| 4817 | case FIT_SR_DRIVE_DEGRADED: |
| 4818 | return "DEGRADED"; |
| 4819 | case FIT_SR_PCIE_LINK_DOWN: |
| 4820 | return "INK_DOWN"; |
| 4821 | case FIT_SR_DRIVE_SOFT_RESET: |
| 4822 | return "SOFT_RESET"; |
| 4823 | case FIT_SR_DRIVE_NEED_FW_DOWNLOAD: |
| 4824 | return "NEED_FW"; |
| 4825 | case FIT_SR_DRIVE_INIT_FAULT: |
| 4826 | return "INIT_FAULT"; |
| 4827 | case FIT_SR_DRIVE_BUSY_SANITIZE: |
| 4828 | return "BUSY_SANITIZE"; |
| 4829 | case FIT_SR_DRIVE_BUSY_ERASE: |
| 4830 | return "BUSY_ERASE"; |
| 4831 | case FIT_SR_DRIVE_FW_BOOTING: |
| 4832 | return "FW_BOOTING"; |
| 4833 | default: |
| 4834 | return "???"; |
| 4835 | } |
| 4836 | } |
| 4837 | |
| 4838 | const char *skd_skdev_state_to_str(enum skd_drvr_state state) |
| 4839 | { |
| 4840 | switch (state) { |
| 4841 | case SKD_DRVR_STATE_LOAD: |
| 4842 | return "LOAD"; |
| 4843 | case SKD_DRVR_STATE_IDLE: |
| 4844 | return "IDLE"; |
| 4845 | case SKD_DRVR_STATE_BUSY: |
| 4846 | return "BUSY"; |
| 4847 | case SKD_DRVR_STATE_STARTING: |
| 4848 | return "STARTING"; |
| 4849 | case SKD_DRVR_STATE_ONLINE: |
| 4850 | return "ONLINE"; |
| 4851 | case SKD_DRVR_STATE_PAUSING: |
| 4852 | return "PAUSING"; |
| 4853 | case SKD_DRVR_STATE_PAUSED: |
| 4854 | return "PAUSED"; |
| 4855 | case SKD_DRVR_STATE_DRAINING_TIMEOUT: |
| 4856 | return "DRAINING_TIMEOUT"; |
| 4857 | case SKD_DRVR_STATE_RESTARTING: |
| 4858 | return "RESTARTING"; |
| 4859 | case SKD_DRVR_STATE_RESUMING: |
| 4860 | return "RESUMING"; |
| 4861 | case SKD_DRVR_STATE_STOPPING: |
| 4862 | return "STOPPING"; |
| 4863 | case SKD_DRVR_STATE_SYNCING: |
| 4864 | return "SYNCING"; |
| 4865 | case SKD_DRVR_STATE_FAULT: |
| 4866 | return "FAULT"; |
| 4867 | case SKD_DRVR_STATE_DISAPPEARED: |
| 4868 | return "DISAPPEARED"; |
| 4869 | case SKD_DRVR_STATE_BUSY_ERASE: |
| 4870 | return "BUSY_ERASE"; |
| 4871 | case SKD_DRVR_STATE_BUSY_SANITIZE: |
| 4872 | return "BUSY_SANITIZE"; |
| 4873 | case SKD_DRVR_STATE_BUSY_IMMINENT: |
| 4874 | return "BUSY_IMMINENT"; |
| 4875 | case SKD_DRVR_STATE_WAIT_BOOT: |
| 4876 | return "WAIT_BOOT"; |
| 4877 | |
| 4878 | default: |
| 4879 | return "???"; |
| 4880 | } |
| 4881 | } |
| 4882 | |
Rashika Kheria | a26ba7f | 2013-12-19 15:02:22 +0530 | [diff] [blame] | 4883 | static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state) |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4884 | { |
| 4885 | switch (state) { |
| 4886 | case SKD_MSG_STATE_IDLE: |
| 4887 | return "IDLE"; |
| 4888 | case SKD_MSG_STATE_BUSY: |
| 4889 | return "BUSY"; |
| 4890 | default: |
| 4891 | return "???"; |
| 4892 | } |
| 4893 | } |
| 4894 | |
Rashika Kheria | a26ba7f | 2013-12-19 15:02:22 +0530 | [diff] [blame] | 4895 | 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] | 4896 | { |
| 4897 | switch (state) { |
| 4898 | case SKD_REQ_STATE_IDLE: |
| 4899 | return "IDLE"; |
| 4900 | case SKD_REQ_STATE_SETUP: |
| 4901 | return "SETUP"; |
| 4902 | case SKD_REQ_STATE_BUSY: |
| 4903 | return "BUSY"; |
| 4904 | case SKD_REQ_STATE_COMPLETED: |
| 4905 | return "COMPLETED"; |
| 4906 | case SKD_REQ_STATE_TIMEOUT: |
| 4907 | return "TIMEOUT"; |
| 4908 | case SKD_REQ_STATE_ABORTED: |
| 4909 | return "ABORTED"; |
| 4910 | default: |
| 4911 | return "???"; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | static void skd_log_skdev(struct skd_device *skdev, const char *event) |
| 4916 | { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4917 | dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event); |
| 4918 | dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n", |
| 4919 | skd_drive_state_to_str(skdev->drive_state), skdev->drive_state, |
| 4920 | skd_skdev_state_to_str(skdev->state), skdev->state); |
| 4921 | dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n", |
| 4922 | skdev->in_flight, skdev->cur_max_queue_depth, |
| 4923 | skdev->dev_max_queue_depth, skdev->queue_low_water_mark); |
| 4924 | dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n", |
| 4925 | skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | static void skd_log_skmsg(struct skd_device *skdev, |
| 4929 | struct skd_fitmsg_context *skmsg, const char *event) |
| 4930 | { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4931 | dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event); |
| 4932 | dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n", |
| 4933 | skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id, |
| 4934 | skmsg->length); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | static void skd_log_skreq(struct skd_device *skdev, |
| 4938 | struct skd_request_context *skreq, const char *event) |
| 4939 | { |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4940 | dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event); |
| 4941 | dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n", |
| 4942 | skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id, |
| 4943 | skreq->fitmsg_id); |
| 4944 | dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n", |
| 4945 | skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4946 | |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 4947 | if (skreq->req != NULL) { |
| 4948 | struct request *req = skreq->req; |
| 4949 | u32 lba = (u32)blk_rq_pos(req); |
| 4950 | u32 count = blk_rq_sectors(req); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4951 | |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4952 | dev_dbg(&skdev->pdev->dev, |
| 4953 | "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, |
| 4954 | lba, lba, count, count, (int)rq_data_dir(req)); |
Jens Axboe | fcd37eb | 2013-11-01 10:14:56 -0600 | [diff] [blame] | 4955 | } else |
Bart Van Assche | f98806d | 2017-08-17 13:12:58 -0700 | [diff] [blame] | 4956 | dev_dbg(&skdev->pdev->dev, "req=NULL\n"); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | /* |
| 4960 | ***************************************************************************** |
| 4961 | * MODULE GLUE |
| 4962 | ***************************************************************************** |
| 4963 | */ |
| 4964 | |
| 4965 | static int __init skd_init(void) |
| 4966 | { |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 4967 | BUILD_BUG_ON(sizeof(struct fit_msg_hdr) + SKD_MAX_REQ_PER_MSG * |
| 4968 | sizeof(struct skd_scsi_request) != SKD_N_FITMSG_BYTES); |
| 4969 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 4970 | pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID); |
| 4971 | |
| 4972 | switch (skd_isr_type) { |
| 4973 | case SKD_IRQ_LEGACY: |
| 4974 | case SKD_IRQ_MSI: |
| 4975 | case SKD_IRQ_MSIX: |
| 4976 | break; |
| 4977 | default: |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 4978 | 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] | 4979 | skd_isr_type, SKD_IRQ_DEFAULT); |
| 4980 | skd_isr_type = SKD_IRQ_DEFAULT; |
| 4981 | } |
| 4982 | |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 4983 | if (skd_max_queue_depth < 1 || |
| 4984 | skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) { |
| 4985 | 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] | 4986 | skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT); |
| 4987 | skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT; |
| 4988 | } |
| 4989 | |
Bart Van Assche | 2da7b40 | 2017-08-17 13:13:01 -0700 | [diff] [blame] | 4990 | if (skd_max_req_per_msg < 1 || |
| 4991 | skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 4992 | 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] | 4993 | skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT); |
| 4994 | skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT; |
| 4995 | } |
| 4996 | |
| 4997 | if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 4998 | 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] | 4999 | skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT); |
| 5000 | skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT; |
| 5001 | } |
| 5002 | |
| 5003 | if (skd_dbg_level < 0 || skd_dbg_level > 2) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 5004 | 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] | 5005 | skd_dbg_level, 0); |
| 5006 | skd_dbg_level = 0; |
| 5007 | } |
| 5008 | |
| 5009 | if (skd_isr_comp_limit < 0) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 5010 | 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] | 5011 | skd_isr_comp_limit, 0); |
| 5012 | skd_isr_comp_limit = 0; |
| 5013 | } |
| 5014 | |
| 5015 | if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) { |
Bartlomiej Zolnierkiewicz | fbed149 | 2013-11-05 12:37:01 +0100 | [diff] [blame] | 5016 | pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n", |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 5017 | skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT); |
| 5018 | skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT; |
| 5019 | } |
| 5020 | |
Bartlomiej Zolnierkiewicz | b8df664 | 2013-11-05 12:37:02 +0100 | [diff] [blame] | 5021 | return pci_register_driver(&skd_driver); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | static void __exit skd_exit(void) |
| 5025 | { |
| 5026 | pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID); |
| 5027 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 5028 | pci_unregister_driver(&skd_driver); |
Bartlomiej Zolnierkiewicz | b8df664 | 2013-11-05 12:37:02 +0100 | [diff] [blame] | 5029 | |
| 5030 | if (skd_major) |
| 5031 | unregister_blkdev(skd_major, DRV_NAME); |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 5032 | } |
| 5033 | |
Akhil Bhansali | e67f86b | 2013-10-15 14:19:07 -0600 | [diff] [blame] | 5034 | module_init(skd_init); |
| 5035 | module_exit(skd_exit); |