blob: 4b92d711d2d34e33fb288d7af6775f942eb5273e [file] [log] [blame]
Bart Van Asschebec9e8a2017-08-17 13:12:47 -07001/*
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 Bhansalie67f86b2013-10-15 14:19:07 -06004 *
Bart Van Asschebec9e8a2017-08-17 13:12:47 -07005 * 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 Bhansalie67f86b2013-10-15 14:19:07 -060010 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/pci.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/blkdev.h>
Bart Van Asschef18c17c2017-08-17 13:13:26 -070019#include <linux/blk-mq.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060020#include <linux/sched.h>
21#include <linux/interrupt.h>
22#include <linux/compiler.h>
23#include <linux/workqueue.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060024#include <linux/delay.h>
25#include <linux/time.h>
26#include <linux/hdreg.h>
27#include <linux/dma-mapping.h>
28#include <linux/completion.h>
29#include <linux/scatterlist.h>
30#include <linux/version.h>
31#include <linux/err.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060032#include <linux/aer.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060033#include <linux/wait.h>
Bart Van Assche2da7b402017-08-17 13:13:01 -070034#include <linux/stringify.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060035#include <scsi/scsi.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060036#include <scsi/sg.h>
37#include <linux/io.h>
38#include <linux/uaccess.h>
Bartlomiej Zolnierkiewicz4ca90b52013-11-05 12:37:04 +010039#include <asm/unaligned.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060040
41#include "skd_s1120.h"
42
43static int skd_dbg_level;
44static int skd_isr_comp_limit = 4;
45
46enum {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060047 SKD_FLUSH_INITIALIZER,
48 SKD_FLUSH_ZERO_SIZE_FIRST,
49 SKD_FLUSH_DATA_SECOND,
50};
51
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060052#define SKD_ASSERT(expr) \
53 do { \
54 if (unlikely(!(expr))) { \
55 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
56 # expr, __FILE__, __func__, __LINE__); \
57 } \
58 } while (0)
59
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060060#define DRV_NAME "skd"
61#define DRV_VERSION "2.2.1"
62#define DRV_BUILD_ID "0260"
63#define PFX DRV_NAME ": "
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060064
Bart Van Asschebec9e8a2017-08-17 13:12:47 -070065MODULE_LICENSE("GPL");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060066
Mike Snitzer38d4a1b2013-11-01 15:05:10 -040067MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060068MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
69
70#define PCI_VENDOR_ID_STEC 0x1B39
71#define PCI_DEVICE_ID_S1120 0x0001
72
73#define SKD_FUA_NV (1 << 1)
74#define SKD_MINORS_PER_DEVICE 16
75
76#define SKD_MAX_QUEUE_DEPTH 200u
77
78#define SKD_PAUSE_TIMEOUT (5 * 1000)
79
80#define SKD_N_FITMSG_BYTES (512u)
Bart Van Assche2da7b402017-08-17 13:13:01 -070081#define SKD_MAX_REQ_PER_MSG 14
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060082
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060083#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
84
85/* SG elements are 32 bytes, so we can make this 4096 and still be under the
86 * 128KB limit. That allows 4096*4K = 16M xfer size
87 */
88#define SKD_N_SG_PER_REQ_DEFAULT 256u
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060089
90#define SKD_N_COMPLETION_ENTRY 256u
91#define SKD_N_READ_CAP_BYTES (8u)
92
93#define SKD_N_INTERNAL_BYTES (512u)
94
Bart Van Assche6f7c7672017-08-17 13:13:02 -070095#define SKD_SKCOMP_SIZE \
96 ((sizeof(struct fit_completion_entry_v1) + \
97 sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY)
98
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060099/* 5 bits of uniqifier, 0xF800 */
100#define SKD_ID_INCR (0x400)
101#define SKD_ID_TABLE_MASK (3u << 8u)
102#define SKD_ID_RW_REQUEST (0u << 8u)
103#define SKD_ID_INTERNAL (1u << 8u)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600104#define SKD_ID_FIT_MSG (3u << 8u)
105#define SKD_ID_SLOT_MASK 0x00FFu
106#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
107
108#define SKD_N_TIMEOUT_SLOT 4u
109#define SKD_TIMEOUT_SLOT_MASK 3u
110
111#define SKD_N_MAX_SECTORS 2048u
112
113#define SKD_MAX_RETRIES 2u
114
115#define SKD_TIMER_SECONDS(seconds) (seconds)
116#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
117
118#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600119
120enum skd_drvr_state {
121 SKD_DRVR_STATE_LOAD,
122 SKD_DRVR_STATE_IDLE,
123 SKD_DRVR_STATE_BUSY,
124 SKD_DRVR_STATE_STARTING,
125 SKD_DRVR_STATE_ONLINE,
126 SKD_DRVR_STATE_PAUSING,
127 SKD_DRVR_STATE_PAUSED,
128 SKD_DRVR_STATE_DRAINING_TIMEOUT,
129 SKD_DRVR_STATE_RESTARTING,
130 SKD_DRVR_STATE_RESUMING,
131 SKD_DRVR_STATE_STOPPING,
132 SKD_DRVR_STATE_FAULT,
133 SKD_DRVR_STATE_DISAPPEARED,
134 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
135 SKD_DRVR_STATE_BUSY_ERASE,
136 SKD_DRVR_STATE_BUSY_SANITIZE,
137 SKD_DRVR_STATE_BUSY_IMMINENT,
138 SKD_DRVR_STATE_WAIT_BOOT,
139 SKD_DRVR_STATE_SYNCING,
140};
141
142#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
143#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
144#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
145#define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u)
146#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
147#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
148#define SKD_START_WAIT_SECONDS 90u
149
150enum skd_req_state {
151 SKD_REQ_STATE_IDLE,
152 SKD_REQ_STATE_SETUP,
153 SKD_REQ_STATE_BUSY,
154 SKD_REQ_STATE_COMPLETED,
155 SKD_REQ_STATE_TIMEOUT,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600156};
157
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600158enum skd_check_status_action {
159 SKD_CHECK_STATUS_REPORT_GOOD,
160 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
161 SKD_CHECK_STATUS_REQUEUE_REQUEST,
162 SKD_CHECK_STATUS_REPORT_ERROR,
163 SKD_CHECK_STATUS_BUSY_IMMINENT,
164};
165
Bart Van Assched891fe62017-08-17 13:13:07 -0700166struct skd_msg_buf {
167 struct fit_msg_hdr fmh;
168 struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG];
169};
170
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600171struct skd_fitmsg_context {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600172 u32 id;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600173
174 u32 length;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600175
Bart Van Assched891fe62017-08-17 13:13:07 -0700176 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600177 dma_addr_t mb_dma_address;
178};
179
180struct skd_request_context {
181 enum skd_req_state state;
182
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600183 u16 id;
184 u32 fitmsg_id;
185
186 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600187 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600188
189 u32 timeout_stamp;
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700190 enum dma_data_direction data_dir;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600191 struct scatterlist *sg;
192 u32 n_sg;
193 u32 sg_byte_count;
194
195 struct fit_sg_descriptor *sksg_list;
196 dma_addr_t sksg_dma_address;
197
198 struct fit_completion_entry_v1 completion;
199
200 struct fit_comp_error_info err_info;
201
202};
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600203
204struct skd_special_context {
205 struct skd_request_context req;
206
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600207 void *data_buf;
208 dma_addr_t db_dma_address;
209
Bart Van Assched891fe62017-08-17 13:13:07 -0700210 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600211 dma_addr_t mb_dma_address;
212};
213
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600214typedef enum skd_irq_type {
215 SKD_IRQ_LEGACY,
216 SKD_IRQ_MSI,
217 SKD_IRQ_MSIX
218} skd_irq_type_t;
219
220#define SKD_MAX_BARS 2
221
222struct skd_device {
Bart Van Assche85e34112017-08-17 13:13:17 -0700223 void __iomem *mem_map[SKD_MAX_BARS];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600224 resource_size_t mem_phys[SKD_MAX_BARS];
225 u32 mem_size[SKD_MAX_BARS];
226
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600227 struct skd_msix_entry *msix_entries;
228
229 struct pci_dev *pdev;
230 int pcie_error_reporting_is_enabled;
231
232 spinlock_t lock;
233 struct gendisk *disk;
234 struct request_queue *queue;
235 struct device *class_dev;
236 int gendisk_on;
237 int sync_done;
238
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600239 u32 devno;
240 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600241 char isr_name[30];
242
243 enum skd_drvr_state state;
244 u32 drive_state;
245
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700246 atomic_t in_flight;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600247 u32 cur_max_queue_depth;
248 u32 queue_low_water_mark;
249 u32 dev_max_queue_depth;
250
251 u32 num_fitmsg_context;
252 u32 num_req_context;
253
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700254 atomic_t timeout_slot[SKD_N_TIMEOUT_SLOT];
255 atomic_t timeout_stamp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600256 struct skd_fitmsg_context *skmsg_table;
257
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600258 struct skd_request_context *skreq_table;
259
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600260 struct skd_special_context internal_skspcl;
261 u32 read_cap_blocksize;
262 u32 read_cap_last_lba;
263 int read_cap_is_valid;
264 int inquiry_is_valid;
265 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600266
267 u8 skcomp_cycle;
268 u32 skcomp_ix;
269 struct fit_completion_entry_v1 *skcomp_table;
270 struct fit_comp_error_info *skerr_table;
271 dma_addr_t cq_dma_address;
272
273 wait_queue_head_t waitq;
274
275 struct timer_list timer;
276 u32 timer_countdown;
277 u32 timer_substate;
278
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600279 int sgs_per_request;
280 u32 last_mtd;
281
282 u32 proto_ver;
283
284 int dbg_level;
285 u32 connect_time_stamp;
286 int connect_retries;
287#define SKD_MAX_CONNECT_RETRIES 16
288 u32 drive_jiffies;
289
290 u32 timo_slot;
291
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600292 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600293};
294
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600295#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
296#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
297#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
298
299static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
300{
Bart Van Assche14262a42017-08-17 13:12:57 -0700301 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600302
Bart Van Assche14262a42017-08-17 13:12:57 -0700303 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700304 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700305 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600306}
307
308static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
309 u32 offset)
310{
Bart Van Assche14262a42017-08-17 13:12:57 -0700311 writel(val, skdev->mem_map[1] + offset);
312 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700313 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600314}
315
316static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
317 u32 offset)
318{
Bart Van Assche14262a42017-08-17 13:12:57 -0700319 writeq(val, skdev->mem_map[1] + offset);
320 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700321 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
322 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600323}
324
325
326#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
327static int skd_isr_type = SKD_IRQ_DEFAULT;
328
329module_param(skd_isr_type, int, 0444);
330MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
331 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
332
333#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
334static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
335
336module_param(skd_max_req_per_msg, int, 0444);
337MODULE_PARM_DESC(skd_max_req_per_msg,
338 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700339 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600340
341#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
342#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
343static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
344
345module_param(skd_max_queue_depth, int, 0444);
346MODULE_PARM_DESC(skd_max_queue_depth,
347 "Maximum SCSI requests issued to s1120."
348 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
349
350static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
351module_param(skd_sgs_per_request, int, 0444);
352MODULE_PARM_DESC(skd_sgs_per_request,
353 "Maximum SG elements per block request."
354 " (1-4096, default==256)");
355
Bart Van Assche63214122017-08-17 13:13:23 -0700356static int skd_max_pass_thru = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600357module_param(skd_max_pass_thru, int, 0444);
358MODULE_PARM_DESC(skd_max_pass_thru,
Bart Van Assche63214122017-08-17 13:13:23 -0700359 "Maximum SCSI pass-thru at a time. IGNORED");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600360
361module_param(skd_dbg_level, int, 0444);
362MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
363
364module_param(skd_isr_comp_limit, int, 0444);
365MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
366
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600367/* Major device number dynamically assigned. */
368static u32 skd_major;
369
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600370static void skd_destruct(struct skd_device *skdev);
371static const struct block_device_operations skd_blockdev_ops;
372static void skd_send_fitmsg(struct skd_device *skdev,
373 struct skd_fitmsg_context *skmsg);
374static void skd_send_special_fitmsg(struct skd_device *skdev,
375 struct skd_special_context *skspcl);
376static void skd_request_fn(struct request_queue *rq);
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700377static void skd_end_request(struct skd_device *skdev, struct request *req,
378 blk_status_t status);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200379static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600380 struct skd_request_context *skreq);
381static void skd_postop_sg_list(struct skd_device *skdev,
382 struct skd_request_context *skreq);
383
384static void skd_restart_device(struct skd_device *skdev);
385static int skd_quiesce_dev(struct skd_device *skdev);
386static int skd_unquiesce_dev(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600387static void skd_disable_interrupts(struct skd_device *skdev);
388static void skd_isr_fwstate(struct skd_device *skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700389static void skd_recover_requests(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600390static void skd_soft_reset(struct skd_device *skdev);
391
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600392const char *skd_drive_state_to_str(int state);
393const char *skd_skdev_state_to_str(enum skd_drvr_state state);
394static void skd_log_skdev(struct skd_device *skdev, const char *event);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600395static void skd_log_skreq(struct skd_device *skdev,
396 struct skd_request_context *skreq, const char *event);
397
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600398/*
399 *****************************************************************************
400 * READ/WRITE REQUESTS
401 *****************************************************************************
402 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600403static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600404{
405 struct request_queue *q = skdev->queue;
406 struct request *req;
407
408 for (;; ) {
409 req = blk_peek_request(q);
410 if (req == NULL)
411 break;
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700412 WARN_ON_ONCE(blk_queue_start_tag(q, req));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200413 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600414 }
415}
416
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600417static void
418skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
419 int data_dir, unsigned lba,
420 unsigned count)
421{
422 if (data_dir == READ)
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700423 scsi_req->cdb[0] = READ_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600424 else
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700425 scsi_req->cdb[0] = WRITE_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600426
427 scsi_req->cdb[1] = 0;
428 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
429 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
430 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
431 scsi_req->cdb[5] = (lba & 0xff);
432 scsi_req->cdb[6] = 0;
433 scsi_req->cdb[7] = (count & 0xff00) >> 8;
434 scsi_req->cdb[8] = count & 0xff;
435 scsi_req->cdb[9] = 0;
436}
437
438static void
439skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400440 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600441{
442 skreq->flush_cmd = 1;
443
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700444 scsi_req->cdb[0] = SYNCHRONIZE_CACHE;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600445 scsi_req->cdb[1] = 0;
446 scsi_req->cdb[2] = 0;
447 scsi_req->cdb[3] = 0;
448 scsi_req->cdb[4] = 0;
449 scsi_req->cdb[5] = 0;
450 scsi_req->cdb[6] = 0;
451 scsi_req->cdb[7] = 0;
452 scsi_req->cdb[8] = 0;
453 scsi_req->cdb[9] = 0;
454}
455
Bart Van Assche3d17a672017-08-17 13:13:21 -0700456/*
457 * Return true if and only if all pending requests should be failed.
458 */
459static bool skd_fail_all(struct request_queue *q)
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700460{
461 struct skd_device *skdev = q->queuedata;
462
463 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
464
465 skd_log_skdev(skdev, "req_not_online");
466 switch (skdev->state) {
467 case SKD_DRVR_STATE_PAUSING:
468 case SKD_DRVR_STATE_PAUSED:
469 case SKD_DRVR_STATE_STARTING:
470 case SKD_DRVR_STATE_RESTARTING:
471 case SKD_DRVR_STATE_WAIT_BOOT:
472 /* In case of starting, we haven't started the queue,
473 * so we can't get here... but requests are
474 * possibly hanging out waiting for us because we
475 * reported the dev/skd0 already. They'll wait
476 * forever if connect doesn't complete.
477 * What to do??? delay dev/skd0 ??
478 */
479 case SKD_DRVR_STATE_BUSY:
480 case SKD_DRVR_STATE_BUSY_IMMINENT:
481 case SKD_DRVR_STATE_BUSY_ERASE:
482 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700483 return false;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700484
485 case SKD_DRVR_STATE_BUSY_SANITIZE:
486 case SKD_DRVR_STATE_STOPPING:
487 case SKD_DRVR_STATE_SYNCING:
488 case SKD_DRVR_STATE_FAULT:
489 case SKD_DRVR_STATE_DISAPPEARED:
490 default:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700491 return true;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700492 }
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700493}
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600494
495static void skd_request_fn(struct request_queue *q)
496{
497 struct skd_device *skdev = q->queuedata;
498 struct skd_fitmsg_context *skmsg = NULL;
499 struct fit_msg_hdr *fmh = NULL;
500 struct skd_request_context *skreq;
501 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600502 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600503 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600504 u32 lba;
505 u32 count;
506 int data_dir;
Bart Van Assche4854afe2017-08-17 13:12:59 -0700507 __be64 be_dmaa;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600508 u64 cmdctxt;
509 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600510 int flush, fua;
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700511 u32 tag;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600512
513 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Assche3d17a672017-08-17 13:13:21 -0700514 if (skd_fail_all(q))
515 skd_fail_all_pending(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600516 return;
517 }
518
Jens Axboe6a5ec652013-11-01 10:38:45 -0600519 if (blk_queue_stopped(skdev->queue)) {
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700520 if (atomic_read(&skdev->in_flight) >=
521 skdev->queue_low_water_mark)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600522 /* There is still some kind of shortage */
523 return;
524
Jens Axboe6a5ec652013-11-01 10:38:45 -0600525 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600526 }
527
528 /*
529 * Stop conditions:
530 * - There are no more native requests
531 * - There are already the maximum number of requests in progress
532 * - There are no more skd_request_context entries
533 * - There are no more FIT msg buffers
534 */
535 for (;; ) {
536
537 flush = fua = 0;
538
Jens Axboefcd37eb2013-11-01 10:14:56 -0600539 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600540
Jens Axboefcd37eb2013-11-01 10:14:56 -0600541 /* Are there any native requests to start? */
542 if (req == NULL)
543 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600544
Jens Axboefcd37eb2013-11-01 10:14:56 -0600545 lba = (u32)blk_rq_pos(req);
546 count = blk_rq_sectors(req);
547 data_dir = rq_data_dir(req);
548 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600549
Mike Christie3a5e02c2016-06-05 14:32:23 -0500550 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600551 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600552
Jens Axboefcd37eb2013-11-01 10:14:56 -0600553 if (io_flags & REQ_FUA)
554 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600555
Bart Van Asschef98806d2017-08-17 13:12:58 -0700556 dev_dbg(&skdev->pdev->dev,
557 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
558 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600559
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400560 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600561
562 /* Are too many requets already in progress? */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700563 if (atomic_read(&skdev->in_flight) >=
564 skdev->cur_max_queue_depth) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700565 dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700566 atomic_read(&skdev->in_flight),
567 skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600568 break;
569 }
570
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600571 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400572 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600573 *
574 * At this point we are comitted to either start or reject
575 * the native request. Note that skd_request_context is
576 * available but is still at the head of the free list.
577 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700578 WARN_ON_ONCE(blk_queue_start_tag(q, req));
579
580 tag = blk_mq_unique_tag(req);
581 WARN_ONCE(tag >= skd_max_queue_depth,
582 "%#x > %#x (nr_requests = %lu)\n", tag,
583 skd_max_queue_depth, q->nr_requests);
584
585 skreq = &skdev->skreq_table[tag];
586 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
587 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
588
589 skreq->id = tag + SKD_ID_RW_REQUEST;
590 skreq->flush_cmd = 0;
591 skreq->n_sg = 0;
592 skreq->sg_byte_count = 0;
593
Jens Axboefcd37eb2013-11-01 10:14:56 -0600594 skreq->req = req;
595 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600596
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700597 skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE :
598 DMA_TO_DEVICE;
Bart Van Assche19fc85c2017-08-17 13:13:04 -0700599
600 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
601 dev_dbg(&skdev->pdev->dev, "error Out\n");
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700602 skd_end_request(skdev, skreq->req, BLK_STS_RESOURCE);
Bart Van Assche19fc85c2017-08-17 13:13:04 -0700603 continue;
604 }
605
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600606 /* Either a FIT msg is in progress or we have to start one. */
607 if (skmsg == NULL) {
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700608 skmsg = &skdev->skmsg_table[tag];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600609
610 /* Initialize the FIT msg header */
Bart Van Assched891fe62017-08-17 13:13:07 -0700611 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600612 memset(fmh, 0, sizeof(*fmh));
613 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
614 skmsg->length = sizeof(*fmh);
615 }
616
617 skreq->fitmsg_id = skmsg->id;
618
Bart Van Assched891fe62017-08-17 13:13:07 -0700619 scsi_req =
620 &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
621 memset(scsi_req, 0, sizeof(*scsi_req));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600622
Bart Van Assche4854afe2017-08-17 13:12:59 -0700623 be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600624 cmdctxt = skreq->id + SKD_ID_INCR;
625
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600626 scsi_req->hdr.tag = cmdctxt;
627 scsi_req->hdr.sg_list_dma_address = be_dmaa;
628
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600629 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600630 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
631 SKD_ASSERT(skreq->flush_cmd == 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600632 } else {
633 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
634 }
635
636 if (fua)
637 scsi_req->cdb[1] |= SKD_FUA_NV;
638
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600639 scsi_req->hdr.sg_list_len_bytes =
640 cpu_to_be32(skreq->sg_byte_count);
641
642 /* Complete resource allocations. */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600643 skreq->state = SKD_REQ_STATE_BUSY;
644 skreq->id += SKD_ID_INCR;
645
646 skmsg->length += sizeof(struct skd_scsi_request);
647 fmh->num_protocol_cmds_coalesced++;
648
649 /*
650 * Update the active request counts.
651 * Capture the timeout timestamp.
652 */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700653 skreq->timeout_stamp = atomic_read(&skdev->timeout_stamp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600654 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700655 atomic_inc(&skdev->timeout_slot[timo_slot]);
656 atomic_inc(&skdev->in_flight);
Bart Van Asschef98806d2017-08-17 13:12:58 -0700657 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700658 atomic_read(&skdev->in_flight));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600659
660 /*
661 * If the FIT msg buffer is full send it.
662 */
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700663 if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600664 skd_send_fitmsg(skdev, skmsg);
665 skmsg = NULL;
666 fmh = NULL;
667 }
668 }
669
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700670 /* If the FIT msg buffer is not empty send what we got. */
671 if (skmsg) {
672 WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced);
673 skd_send_fitmsg(skdev, skmsg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600674 skmsg = NULL;
675 fmh = NULL;
676 }
677
678 /*
679 * If req is non-NULL it means there is something to do but
680 * we are out of a resource.
681 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600682 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600683 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600684}
685
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700686static void skd_end_request(struct skd_device *skdev, struct request *req,
687 blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600688{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600689 if (unlikely(error)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600690 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
691 u32 lba = (u32)blk_rq_pos(req);
692 u32 count = blk_rq_sectors(req);
693
Bart Van Asschef98806d2017-08-17 13:12:58 -0700694 dev_err(&skdev->pdev->dev,
695 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700696 count, req->tag);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600697 } else
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700698 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", req->tag,
Bart Van Asschef98806d2017-08-17 13:12:58 -0700699 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600700
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700701 __blk_end_request_all(req, error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600702}
703
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200704static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400705 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600706{
707 struct request *req = skreq->req;
Bart Van Assche06f824c42017-08-17 13:13:15 -0700708 struct scatterlist *sgl = &skreq->sg[0], *sg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600709 int n_sg;
710 int i;
711
712 skreq->sg_byte_count = 0;
713
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700714 WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE &&
715 skreq->data_dir != DMA_FROM_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600716
Bart Van Assche06f824c42017-08-17 13:13:15 -0700717 n_sg = blk_rq_map_sg(skdev->queue, req, sgl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600718 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200719 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600720
721 /*
722 * Map scatterlist to PCI bus addresses.
723 * Note PCI might change the number of entries.
724 */
Bart Van Assche06f824c42017-08-17 13:13:15 -0700725 n_sg = pci_map_sg(skdev->pdev, sgl, n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600726 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200727 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600728
729 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
730
731 skreq->n_sg = n_sg;
732
Bart Van Assche06f824c42017-08-17 13:13:15 -0700733 for_each_sg(sgl, sg, n_sg, i) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600734 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Assche06f824c42017-08-17 13:13:15 -0700735 u32 cnt = sg_dma_len(sg);
736 uint64_t dma_addr = sg_dma_address(sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600737
738 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
739 sgd->byte_count = cnt;
740 skreq->sg_byte_count += cnt;
741 sgd->host_side_addr = dma_addr;
742 sgd->dev_side_addr = 0;
743 }
744
745 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
746 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
747
748 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700749 dev_dbg(&skdev->pdev->dev,
750 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
751 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600752 for (i = 0; i < n_sg; i++) {
753 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700754
755 dev_dbg(&skdev->pdev->dev,
756 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
757 i, sgd->byte_count, sgd->control,
758 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600759 }
760 }
761
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200762 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600763}
764
Jens Axboefcd37eb2013-11-01 10:14:56 -0600765static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400766 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600767{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600768 /*
769 * restore the next ptr for next IO request so we
770 * don't have to set it every time.
771 */
772 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
773 skreq->sksg_dma_address +
774 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700775 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600776}
777
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600778/*
779 *****************************************************************************
780 * TIMER
781 *****************************************************************************
782 */
783
784static void skd_timer_tick_not_online(struct skd_device *skdev);
785
786static void skd_timer_tick(ulong arg)
787{
788 struct skd_device *skdev = (struct skd_device *)arg;
789
790 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600791 unsigned long reqflags;
792 u32 state;
793
794 if (skdev->state == SKD_DRVR_STATE_FAULT)
795 /* The driver has declared fault, and we want it to
796 * stay that way until driver is reloaded.
797 */
798 return;
799
800 spin_lock_irqsave(&skdev->lock, reqflags);
801
802 state = SKD_READL(skdev, FIT_STATUS);
803 state &= FIT_SR_DRIVE_STATE_MASK;
804 if (state != skdev->drive_state)
805 skd_isr_fwstate(skdev);
806
807 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
808 skd_timer_tick_not_online(skdev);
809 goto timer_func_out;
810 }
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700811 timo_slot = atomic_inc_return(&skdev->timeout_stamp) &
812 SKD_TIMEOUT_SLOT_MASK;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600813
814 /*
815 * All requests that happened during the previous use of
816 * this slot should be done by now. The previous use was
817 * over 7 seconds ago.
818 */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700819 if (atomic_read(&skdev->timeout_slot[timo_slot]) == 0)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600820 goto timer_func_out;
821
822 /* Something is overdue */
Bart Van Asschef98806d2017-08-17 13:12:58 -0700823 dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700824 atomic_read(&skdev->timeout_slot[timo_slot]),
825 atomic_read(&skdev->in_flight));
Bart Van Asschef98806d2017-08-17 13:12:58 -0700826 dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700827 atomic_read(&skdev->timeout_slot[timo_slot]),
828 atomic_read(&skdev->in_flight));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600829
830 skdev->timer_countdown = SKD_DRAINING_TIMO;
831 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
832 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600833 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600834
835timer_func_out:
836 mod_timer(&skdev->timer, (jiffies + HZ));
837
838 spin_unlock_irqrestore(&skdev->lock, reqflags);
839}
840
841static void skd_timer_tick_not_online(struct skd_device *skdev)
842{
843 switch (skdev->state) {
844 case SKD_DRVR_STATE_IDLE:
845 case SKD_DRVR_STATE_LOAD:
846 break;
847 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700848 dev_dbg(&skdev->pdev->dev,
849 "drive busy sanitize[%x], driver[%x]\n",
850 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600851 /* If we've been in sanitize for 3 seconds, we figure we're not
852 * going to get anymore completions, so recover requests now
853 */
854 if (skdev->timer_countdown > 0) {
855 skdev->timer_countdown--;
856 return;
857 }
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700858 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600859 break;
860
861 case SKD_DRVR_STATE_BUSY:
862 case SKD_DRVR_STATE_BUSY_IMMINENT:
863 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700864 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
865 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600866 if (skdev->timer_countdown > 0) {
867 skdev->timer_countdown--;
868 return;
869 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700870 dev_dbg(&skdev->pdev->dev,
871 "busy[%x], timedout=%d, restarting device.",
872 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600873 skd_restart_device(skdev);
874 break;
875
876 case SKD_DRVR_STATE_WAIT_BOOT:
877 case SKD_DRVR_STATE_STARTING:
878 if (skdev->timer_countdown > 0) {
879 skdev->timer_countdown--;
880 return;
881 }
882 /* For now, we fault the drive. Could attempt resets to
883 * revcover at some point. */
884 skdev->state = SKD_DRVR_STATE_FAULT;
885
Bart Van Asschef98806d2017-08-17 13:12:58 -0700886 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
887 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600888
889 /*start the queue so we can respond with error to requests */
890 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600891 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600892 skdev->gendisk_on = -1;
893 wake_up_interruptible(&skdev->waitq);
894 break;
895
896 case SKD_DRVR_STATE_ONLINE:
897 /* shouldn't get here. */
898 break;
899
900 case SKD_DRVR_STATE_PAUSING:
901 case SKD_DRVR_STATE_PAUSED:
902 break;
903
904 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700905 dev_dbg(&skdev->pdev->dev,
906 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
907 skdev->timo_slot, skdev->timer_countdown,
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700908 atomic_read(&skdev->in_flight),
909 atomic_read(&skdev->timeout_slot[skdev->timo_slot]));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600910 /* if the slot has cleared we can let the I/O continue */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700911 if (atomic_read(&skdev->timeout_slot[skdev->timo_slot]) == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700912 dev_dbg(&skdev->pdev->dev,
913 "Slot drained, starting queue.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600914 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600915 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600916 return;
917 }
918 if (skdev->timer_countdown > 0) {
919 skdev->timer_countdown--;
920 return;
921 }
922 skd_restart_device(skdev);
923 break;
924
925 case SKD_DRVR_STATE_RESTARTING:
926 if (skdev->timer_countdown > 0) {
927 skdev->timer_countdown--;
928 return;
929 }
930 /* For now, we fault the drive. Could attempt resets to
931 * revcover at some point. */
932 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700933 dev_err(&skdev->pdev->dev,
934 "DriveFault Reconnect Timeout (%x)\n",
935 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600936
937 /*
938 * Recovering does two things:
939 * 1. completes IO with error
940 * 2. reclaims dma resources
941 * When is it safe to recover requests?
942 * - if the drive state is faulted
943 * - if the state is still soft reset after out timeout
944 * - if the drive registers are dead (state = FF)
945 * If it is "unsafe", we still need to recover, so we will
946 * disable pci bus mastering and disable our interrupts.
947 */
948
949 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
950 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
951 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
952 /* It never came out of soft reset. Try to
953 * recover the requests and then let them
954 * fail. This is to mitigate hung processes. */
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700955 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600956 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700957 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
958 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600959 pci_disable_device(skdev->pdev);
960 skd_disable_interrupts(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700961 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600962 }
963
964 /*start the queue so we can respond with error to requests */
965 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600966 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600967 skdev->gendisk_on = -1;
968 wake_up_interruptible(&skdev->waitq);
969 break;
970
971 case SKD_DRVR_STATE_RESUMING:
972 case SKD_DRVR_STATE_STOPPING:
973 case SKD_DRVR_STATE_SYNCING:
974 case SKD_DRVR_STATE_FAULT:
975 case SKD_DRVR_STATE_DISAPPEARED:
976 default:
977 break;
978 }
979}
980
981static int skd_start_timer(struct skd_device *skdev)
982{
983 int rc;
984
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600985 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
986
987 rc = mod_timer(&skdev->timer, (jiffies + HZ));
988 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -0700989 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600990 return rc;
991}
992
993static void skd_kill_timer(struct skd_device *skdev)
994{
995 del_timer_sync(&skdev->timer);
996}
997
998/*
999 *****************************************************************************
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001000 * INTERNAL REQUESTS -- generated by driver itself
1001 *****************************************************************************
1002 */
1003
1004static int skd_format_internal_skspcl(struct skd_device *skdev)
1005{
1006 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1007 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1008 struct fit_msg_hdr *fmh;
1009 uint64_t dma_address;
1010 struct skd_scsi_request *scsi;
1011
Bart Van Assched891fe62017-08-17 13:13:07 -07001012 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001013 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1014 fmh->num_protocol_cmds_coalesced = 1;
1015
Bart Van Assched891fe62017-08-17 13:13:07 -07001016 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001017 memset(scsi, 0, sizeof(*scsi));
1018 dma_address = skspcl->req.sksg_dma_address;
1019 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
Bart Van Assche32494df2017-08-17 13:13:25 -07001020 skspcl->req.n_sg = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001021 sgd->control = FIT_SGD_CONTROL_LAST;
1022 sgd->byte_count = 0;
1023 sgd->host_side_addr = skspcl->db_dma_address;
1024 sgd->dev_side_addr = 0;
1025 sgd->next_desc_ptr = 0LL;
1026
1027 return 1;
1028}
1029
1030#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1031
1032static void skd_send_internal_skspcl(struct skd_device *skdev,
1033 struct skd_special_context *skspcl,
1034 u8 opcode)
1035{
1036 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1037 struct skd_scsi_request *scsi;
1038 unsigned char *buf = skspcl->data_buf;
1039 int i;
1040
1041 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1042 /*
1043 * A refresh is already in progress.
1044 * Just wait for it to finish.
1045 */
1046 return;
1047
1048 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1049 skspcl->req.state = SKD_REQ_STATE_BUSY;
1050 skspcl->req.id += SKD_ID_INCR;
1051
Bart Van Assched891fe62017-08-17 13:13:07 -07001052 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001053 scsi->hdr.tag = skspcl->req.id;
1054
1055 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1056
1057 switch (opcode) {
1058 case TEST_UNIT_READY:
1059 scsi->cdb[0] = TEST_UNIT_READY;
1060 sgd->byte_count = 0;
1061 scsi->hdr.sg_list_len_bytes = 0;
1062 break;
1063
1064 case READ_CAPACITY:
1065 scsi->cdb[0] = READ_CAPACITY;
1066 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1067 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1068 break;
1069
1070 case INQUIRY:
1071 scsi->cdb[0] = INQUIRY;
1072 scsi->cdb[1] = 0x01; /* evpd */
1073 scsi->cdb[2] = 0x80; /* serial number page */
1074 scsi->cdb[4] = 0x10;
1075 sgd->byte_count = 16;
1076 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1077 break;
1078
1079 case SYNCHRONIZE_CACHE:
1080 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1081 sgd->byte_count = 0;
1082 scsi->hdr.sg_list_len_bytes = 0;
1083 break;
1084
1085 case WRITE_BUFFER:
1086 scsi->cdb[0] = WRITE_BUFFER;
1087 scsi->cdb[1] = 0x02;
1088 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1089 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1090 sgd->byte_count = WR_BUF_SIZE;
1091 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1092 /* fill incrementing byte pattern */
1093 for (i = 0; i < sgd->byte_count; i++)
1094 buf[i] = i & 0xFF;
1095 break;
1096
1097 case READ_BUFFER:
1098 scsi->cdb[0] = READ_BUFFER;
1099 scsi->cdb[1] = 0x02;
1100 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1101 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1102 sgd->byte_count = WR_BUF_SIZE;
1103 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1104 memset(skspcl->data_buf, 0, sgd->byte_count);
1105 break;
1106
1107 default:
1108 SKD_ASSERT("Don't know what to send");
1109 return;
1110
1111 }
1112 skd_send_special_fitmsg(skdev, skspcl);
1113}
1114
1115static void skd_refresh_device_data(struct skd_device *skdev)
1116{
1117 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1118
1119 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1120}
1121
1122static int skd_chk_read_buf(struct skd_device *skdev,
1123 struct skd_special_context *skspcl)
1124{
1125 unsigned char *buf = skspcl->data_buf;
1126 int i;
1127
1128 /* check for incrementing byte pattern */
1129 for (i = 0; i < WR_BUF_SIZE; i++)
1130 if (buf[i] != (i & 0xFF))
1131 return 1;
1132
1133 return 0;
1134}
1135
1136static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1137 u8 code, u8 qual, u8 fruc)
1138{
1139 /* If the check condition is of special interest, log a message */
1140 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1141 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001142 dev_err(&skdev->pdev->dev,
1143 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1144 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001145 }
1146}
1147
1148static void skd_complete_internal(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001149 struct fit_completion_entry_v1 *skcomp,
1150 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001151 struct skd_special_context *skspcl)
1152{
1153 u8 *buf = skspcl->data_buf;
1154 u8 status;
1155 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001156 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001157
Bart Van Assche760b48c2017-08-17 13:13:00 -07001158 lockdep_assert_held(&skdev->lock);
1159
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001160 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1161
Bart Van Asschef98806d2017-08-17 13:12:58 -07001162 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001163
1164 skspcl->req.completion = *skcomp;
1165 skspcl->req.state = SKD_REQ_STATE_IDLE;
1166 skspcl->req.id += SKD_ID_INCR;
1167
1168 status = skspcl->req.completion.status;
1169
1170 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1171 skerr->qual, skerr->fruc);
1172
1173 switch (scsi->cdb[0]) {
1174 case TEST_UNIT_READY:
1175 if (status == SAM_STAT_GOOD)
1176 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1177 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1178 (skerr->key == MEDIUM_ERROR))
1179 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1180 else {
1181 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001182 dev_dbg(&skdev->pdev->dev,
1183 "TUR failed, don't send anymore state 0x%x\n",
1184 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001185 return;
1186 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001187 dev_dbg(&skdev->pdev->dev,
1188 "**** TUR failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001189 skd_send_internal_skspcl(skdev, skspcl,
1190 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001191 }
1192 break;
1193
1194 case WRITE_BUFFER:
1195 if (status == SAM_STAT_GOOD)
1196 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1197 else {
1198 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001199 dev_dbg(&skdev->pdev->dev,
1200 "write buffer failed, don't send anymore state 0x%x\n",
1201 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001202 return;
1203 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001204 dev_dbg(&skdev->pdev->dev,
1205 "**** write buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001206 skd_send_internal_skspcl(skdev, skspcl,
1207 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001208 }
1209 break;
1210
1211 case READ_BUFFER:
1212 if (status == SAM_STAT_GOOD) {
1213 if (skd_chk_read_buf(skdev, skspcl) == 0)
1214 skd_send_internal_skspcl(skdev, skspcl,
1215 READ_CAPACITY);
1216 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001217 dev_err(&skdev->pdev->dev,
1218 "*** W/R Buffer mismatch %d ***\n",
1219 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001220 if (skdev->connect_retries <
1221 SKD_MAX_CONNECT_RETRIES) {
1222 skdev->connect_retries++;
1223 skd_soft_reset(skdev);
1224 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001225 dev_err(&skdev->pdev->dev,
1226 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001227 return;
1228 }
1229 }
1230
1231 } else {
1232 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001233 dev_dbg(&skdev->pdev->dev,
1234 "read buffer failed, don't send anymore state 0x%x\n",
1235 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001236 return;
1237 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001238 dev_dbg(&skdev->pdev->dev,
1239 "**** read buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001240 skd_send_internal_skspcl(skdev, skspcl,
1241 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001242 }
1243 break;
1244
1245 case READ_CAPACITY:
1246 skdev->read_cap_is_valid = 0;
1247 if (status == SAM_STAT_GOOD) {
1248 skdev->read_cap_last_lba =
1249 (buf[0] << 24) | (buf[1] << 16) |
1250 (buf[2] << 8) | buf[3];
1251 skdev->read_cap_blocksize =
1252 (buf[4] << 24) | (buf[5] << 16) |
1253 (buf[6] << 8) | buf[7];
1254
Bart Van Asschef98806d2017-08-17 13:12:58 -07001255 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1256 skdev->read_cap_last_lba,
1257 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001258
1259 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1260
1261 skdev->read_cap_is_valid = 1;
1262
1263 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1264 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1265 (skerr->key == MEDIUM_ERROR)) {
1266 skdev->read_cap_last_lba = ~0;
1267 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001268 dev_dbg(&skdev->pdev->dev, "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001269 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1270 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001271 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001272 skd_send_internal_skspcl(skdev, skspcl,
1273 TEST_UNIT_READY);
1274 }
1275 break;
1276
1277 case INQUIRY:
1278 skdev->inquiry_is_valid = 0;
1279 if (status == SAM_STAT_GOOD) {
1280 skdev->inquiry_is_valid = 1;
1281
1282 for (i = 0; i < 12; i++)
1283 skdev->inq_serial_num[i] = buf[i + 4];
1284 skdev->inq_serial_num[12] = 0;
1285 }
1286
1287 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001288 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001289 /* connection is complete */
1290 skdev->connect_retries = 0;
1291 break;
1292
1293 case SYNCHRONIZE_CACHE:
1294 if (status == SAM_STAT_GOOD)
1295 skdev->sync_done = 1;
1296 else
1297 skdev->sync_done = -1;
1298 wake_up_interruptible(&skdev->waitq);
1299 break;
1300
1301 default:
1302 SKD_ASSERT("we didn't send this");
1303 }
1304}
1305
1306/*
1307 *****************************************************************************
1308 * FIT MESSAGES
1309 *****************************************************************************
1310 */
1311
1312static void skd_send_fitmsg(struct skd_device *skdev,
1313 struct skd_fitmsg_context *skmsg)
1314{
1315 u64 qcmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001316
Bart Van Asschef98806d2017-08-17 13:12:58 -07001317 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001318 skmsg->mb_dma_address, atomic_read(&skdev->in_flight));
Bart Van Assche6507f432017-08-17 13:13:06 -07001319 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001320
1321 qcmd = skmsg->mb_dma_address;
1322 qcmd |= FIT_QCMD_QID_NORMAL;
1323
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001324 if (unlikely(skdev->dbg_level > 1)) {
1325 u8 *bp = (u8 *)skmsg->msg_buf;
1326 int i;
1327 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001328 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
1329 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001330 if (i == 0)
1331 i = 64 - 8;
1332 }
1333 }
1334
1335 if (skmsg->length > 256)
1336 qcmd |= FIT_QCMD_MSGSIZE_512;
1337 else if (skmsg->length > 128)
1338 qcmd |= FIT_QCMD_MSGSIZE_256;
1339 else if (skmsg->length > 64)
1340 qcmd |= FIT_QCMD_MSGSIZE_128;
1341 else
1342 /*
1343 * This makes no sense because the FIT msg header is
1344 * 64 bytes. If the msg is only 64 bytes long it has
1345 * no payload.
1346 */
1347 qcmd |= FIT_QCMD_MSGSIZE_64;
1348
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001349 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1350 smp_wmb();
1351
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001352 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001353}
1354
1355static void skd_send_special_fitmsg(struct skd_device *skdev,
1356 struct skd_special_context *skspcl)
1357{
1358 u64 qcmd;
1359
1360 if (unlikely(skdev->dbg_level > 1)) {
1361 u8 *bp = (u8 *)skspcl->msg_buf;
1362 int i;
1363
1364 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001365 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
1366 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001367 if (i == 0)
1368 i = 64 - 8;
1369 }
1370
Bart Van Asschef98806d2017-08-17 13:12:58 -07001371 dev_dbg(&skdev->pdev->dev,
1372 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
1373 skspcl, skspcl->req.id, skspcl->req.sksg_list,
1374 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001375 for (i = 0; i < skspcl->req.n_sg; i++) {
1376 struct fit_sg_descriptor *sgd =
1377 &skspcl->req.sksg_list[i];
1378
Bart Van Asschef98806d2017-08-17 13:12:58 -07001379 dev_dbg(&skdev->pdev->dev,
1380 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1381 i, sgd->byte_count, sgd->control,
1382 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001383 }
1384 }
1385
1386 /*
1387 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
1388 * and one 64-byte SSDI command.
1389 */
1390 qcmd = skspcl->mb_dma_address;
1391 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
1392
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001393 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1394 smp_wmb();
1395
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001396 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
1397}
1398
1399/*
1400 *****************************************************************************
1401 * COMPLETION QUEUE
1402 *****************************************************************************
1403 */
1404
1405static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001406 struct fit_completion_entry_v1 *skcomp,
1407 struct fit_comp_error_info *skerr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001408
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001409struct sns_info {
1410 u8 type;
1411 u8 stat;
1412 u8 key;
1413 u8 asc;
1414 u8 ascq;
1415 u8 mask;
1416 enum skd_check_status_action action;
1417};
1418
1419static struct sns_info skd_chkstat_table[] = {
1420 /* Good */
1421 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
1422 SKD_CHECK_STATUS_REPORT_GOOD },
1423
1424 /* Smart alerts */
1425 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
1426 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1427 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
1428 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1429 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
1430 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1431
1432 /* Retry (with limits) */
1433 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
1434 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1435 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
1436 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1437 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
1438 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1439 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
1440 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1441
1442 /* Busy (or about to be) */
1443 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
1444 SKD_CHECK_STATUS_BUSY_IMMINENT },
1445};
1446
1447/*
1448 * Look up status and sense data to decide how to handle the error
1449 * from the device.
1450 * mask says which fields must match e.g., mask=0x18 means check
1451 * type and stat, ignore key, asc, ascq.
1452 */
1453
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001454static enum skd_check_status_action
1455skd_check_status(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001456 u8 cmp_status, struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001457{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001458 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001459
Bart Van Asschef98806d2017-08-17 13:12:58 -07001460 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1461 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001462
Bart Van Asschef98806d2017-08-17 13:12:58 -07001463 dev_dbg(&skdev->pdev->dev,
1464 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
1465 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
1466 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001467
1468 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001469 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001470 struct sns_info *sns = &skd_chkstat_table[i];
1471
1472 if (sns->mask & 0x10)
1473 if (skerr->type != sns->type)
1474 continue;
1475
1476 if (sns->mask & 0x08)
1477 if (cmp_status != sns->stat)
1478 continue;
1479
1480 if (sns->mask & 0x04)
1481 if (skerr->key != sns->key)
1482 continue;
1483
1484 if (sns->mask & 0x02)
1485 if (skerr->code != sns->asc)
1486 continue;
1487
1488 if (sns->mask & 0x01)
1489 if (skerr->qual != sns->ascq)
1490 continue;
1491
1492 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001493 dev_err(&skdev->pdev->dev,
1494 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
1495 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001496 }
1497 return sns->action;
1498 }
1499
1500 /* No other match, so nonzero status means error,
1501 * zero status means good
1502 */
1503 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001504 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001505 return SKD_CHECK_STATUS_REPORT_ERROR;
1506 }
1507
Bart Van Asschef98806d2017-08-17 13:12:58 -07001508 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001509 return SKD_CHECK_STATUS_REPORT_GOOD;
1510}
1511
1512static void skd_resolve_req_exception(struct skd_device *skdev,
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001513 struct skd_request_context *skreq,
1514 struct request *req)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001515{
1516 u8 cmp_status = skreq->completion.status;
1517
1518 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
1519 case SKD_CHECK_STATUS_REPORT_GOOD:
1520 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001521 skd_end_request(skdev, req, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001522 break;
1523
1524 case SKD_CHECK_STATUS_BUSY_IMMINENT:
1525 skd_log_skreq(skdev, skreq, "retry(busy)");
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001526 blk_requeue_request(skdev->queue, req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001527 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001528 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
1529 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
1530 skd_quiesce_dev(skdev);
1531 break;
1532
1533 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001534 if ((unsigned long) ++req->special < SKD_MAX_RETRIES) {
Jens Axboefcd37eb2013-11-01 10:14:56 -06001535 skd_log_skreq(skdev, skreq, "retry");
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001536 blk_requeue_request(skdev->queue, req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06001537 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001538 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07001539 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001540
1541 case SKD_CHECK_STATUS_REPORT_ERROR:
1542 default:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001543 skd_end_request(skdev, req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001544 break;
1545 }
1546}
1547
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001548/* assume spinlock is already held */
1549static void skd_release_skreq(struct skd_device *skdev,
1550 struct skd_request_context *skreq)
1551{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001552 u32 timo_slot;
1553
1554 /*
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001555 * Decrease the number of active requests.
1556 * Also decrements the count in the timeout slot.
1557 */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001558 SKD_ASSERT(atomic_read(&skdev->in_flight) > 0);
1559 atomic_dec(&skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001560
1561 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001562 SKD_ASSERT(atomic_read(&skdev->timeout_slot[timo_slot]) > 0);
1563 atomic_dec(&skdev->timeout_slot[timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001564
1565 /*
1566 * Reset backpointer
1567 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06001568 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001569
1570 /*
1571 * Reclaim the skd_request_context
1572 */
1573 skreq->state = SKD_REQ_STATE_IDLE;
1574 skreq->id += SKD_ID_INCR;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001575}
1576
1577static struct skd_request_context *skd_skreq_from_rq(struct skd_device *skdev,
1578 struct request *rq)
1579{
1580 struct skd_request_context *skreq;
1581 int i;
1582
1583 for (i = 0, skreq = skdev->skreq_table; i < skdev->num_fitmsg_context;
1584 i++, skreq++)
1585 if (skreq->req == rq)
1586 return skreq;
1587
1588 return NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001589}
1590
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001591static int skd_isr_completion_posted(struct skd_device *skdev,
1592 int limit, int *enqueued)
1593{
Bart Van Assche85e34112017-08-17 13:13:17 -07001594 struct fit_completion_entry_v1 *skcmp;
1595 struct fit_comp_error_info *skerr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001596 u16 req_id;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001597 u32 tag;
1598 struct request *rq;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001599 struct skd_request_context *skreq;
Bart Van Asschec830da82017-08-17 13:13:13 -07001600 u16 cmp_cntxt;
1601 u8 cmp_status;
1602 u8 cmp_cycle;
1603 u32 cmp_bytes;
1604 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001605 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001606
Bart Van Assche760b48c2017-08-17 13:13:00 -07001607 lockdep_assert_held(&skdev->lock);
1608
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001609 for (;; ) {
1610 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
1611
1612 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
1613 cmp_cycle = skcmp->cycle;
1614 cmp_cntxt = skcmp->tag;
1615 cmp_status = skcmp->status;
1616 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
1617
1618 skerr = &skdev->skerr_table[skdev->skcomp_ix];
1619
Bart Van Asschef98806d2017-08-17 13:12:58 -07001620 dev_dbg(&skdev->pdev->dev,
1621 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
1622 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001623 cmp_cntxt, cmp_status, atomic_read(&skdev->in_flight),
1624 cmp_bytes, skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001625
1626 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001627 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001628 break;
1629 }
1630 /*
1631 * Update the completion queue head index and possibly
1632 * the completion cycle count. 8-bit wrap-around.
1633 */
1634 skdev->skcomp_ix++;
1635 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
1636 skdev->skcomp_ix = 0;
1637 skdev->skcomp_cycle++;
1638 }
1639
1640 /*
1641 * The command context is a unique 32-bit ID. The low order
1642 * bits help locate the request. The request is usually a
1643 * r/w request (see skd_start() above) or a special request.
1644 */
1645 req_id = cmp_cntxt;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001646 tag = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001647
1648 /* Is this other than a r/w request? */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001649 if (tag >= skdev->num_req_context) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001650 /*
1651 * This is not a completion for a r/w request.
1652 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001653 WARN_ON_ONCE(blk_map_queue_find_tag(skdev->queue->
1654 queue_tags, tag));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001655 skd_complete_other(skdev, skcmp, skerr);
1656 continue;
1657 }
1658
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001659 rq = blk_map_queue_find_tag(skdev->queue->queue_tags, tag);
1660 if (WARN(!rq, "No request for tag %#x -> %#x\n", cmp_cntxt,
1661 tag))
1662 continue;
1663 skreq = skd_skreq_from_rq(skdev, rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001664
1665 /*
1666 * Make sure the request ID for the slot matches.
1667 */
1668 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001669 dev_dbg(&skdev->pdev->dev,
1670 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
1671 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001672 {
1673 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001674 dev_err(&skdev->pdev->dev,
1675 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
1676 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001677
1678 continue;
1679 }
1680 }
1681
1682 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
1683
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001684 skreq->completion = *skcmp;
1685 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
1686 skreq->err_info = *skerr;
1687 skd_log_check_status(skdev, cmp_status, skerr->key,
1688 skerr->code, skerr->qual,
1689 skerr->fruc);
1690 }
1691 /* Release DMA resources for the request. */
1692 if (skreq->n_sg > 0)
1693 skd_postop_sg_list(skdev, skreq);
1694
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001695 /* Mark the FIT msg and timeout slot as free. */
1696 skd_release_skreq(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001697
1698 /*
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001699 * Capture the outcome and post it back to the native request.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001700 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001701 if (likely(cmp_status == SAM_STAT_GOOD))
1702 skd_end_request(skdev, rq, BLK_STS_OK);
1703 else
1704 skd_resolve_req_exception(skdev, skreq, rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001705
1706 /* skd_isr_comp_limit equal zero means no limit */
1707 if (limit) {
1708 if (++processed >= limit) {
1709 rc = 1;
1710 break;
1711 }
1712 }
1713 }
1714
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001715 if (skdev->state == SKD_DRVR_STATE_PAUSING &&
1716 atomic_read(&skdev->in_flight) == 0) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001717 skdev->state = SKD_DRVR_STATE_PAUSED;
1718 wake_up_interruptible(&skdev->waitq);
1719 }
1720
1721 return rc;
1722}
1723
1724static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001725 struct fit_completion_entry_v1 *skcomp,
1726 struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001727{
1728 u32 req_id = 0;
1729 u32 req_table;
1730 u32 req_slot;
1731 struct skd_special_context *skspcl;
1732
Bart Van Assche760b48c2017-08-17 13:13:00 -07001733 lockdep_assert_held(&skdev->lock);
1734
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001735 req_id = skcomp->tag;
1736 req_table = req_id & SKD_ID_TABLE_MASK;
1737 req_slot = req_id & SKD_ID_SLOT_MASK;
1738
Bart Van Asschef98806d2017-08-17 13:12:58 -07001739 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
1740 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001741
1742 /*
1743 * Based on the request id, determine how to dispatch this completion.
1744 * This swich/case is finding the good cases and forwarding the
1745 * completion entry. Errors are reported below the switch.
1746 */
1747 switch (req_table) {
1748 case SKD_ID_RW_REQUEST:
1749 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07001750 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001751 * handles r/w requests. The only way we get here
1752 * is if the req_slot is out of bounds.
1753 */
1754 break;
1755
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001756 case SKD_ID_INTERNAL:
1757 if (req_slot == 0) {
1758 skspcl = &skdev->internal_skspcl;
1759 if (skspcl->req.id == req_id &&
1760 skspcl->req.state == SKD_REQ_STATE_BUSY) {
1761 skd_complete_internal(skdev,
1762 skcomp, skerr, skspcl);
1763 return;
1764 }
1765 }
1766 break;
1767
1768 case SKD_ID_FIT_MSG:
1769 /*
1770 * These id's should never appear in a completion record.
1771 */
1772 break;
1773
1774 default:
1775 /*
1776 * These id's should never appear anywhere;
1777 */
1778 break;
1779 }
1780
1781 /*
1782 * If we get here it is a bad or stale id.
1783 */
1784}
1785
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001786static void skd_reset_skcomp(struct skd_device *skdev)
1787{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07001788 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001789
1790 skdev->skcomp_ix = 0;
1791 skdev->skcomp_cycle = 1;
1792}
1793
1794/*
1795 *****************************************************************************
1796 * INTERRUPTS
1797 *****************************************************************************
1798 */
1799static void skd_completion_worker(struct work_struct *work)
1800{
1801 struct skd_device *skdev =
1802 container_of(work, struct skd_device, completion_worker);
1803 unsigned long flags;
1804 int flush_enqueued = 0;
1805
1806 spin_lock_irqsave(&skdev->lock, flags);
1807
1808 /*
1809 * pass in limit=0, which means no limit..
1810 * process everything in compq
1811 */
1812 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
Bart Van Assche8fe70062017-08-17 13:13:22 -07001813 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001814
1815 spin_unlock_irqrestore(&skdev->lock, flags);
1816}
1817
1818static void skd_isr_msg_from_dev(struct skd_device *skdev);
1819
Arnd Bergmann41c94992016-11-09 13:55:35 +01001820static irqreturn_t
1821skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001822{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07001823 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001824 u32 intstat;
1825 u32 ack;
1826 int rc = 0;
1827 int deferred = 0;
1828 int flush_enqueued = 0;
1829
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001830 spin_lock(&skdev->lock);
1831
1832 for (;; ) {
1833 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
1834
1835 ack = FIT_INT_DEF_MASK;
1836 ack &= intstat;
1837
Bart Van Asschef98806d2017-08-17 13:12:58 -07001838 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
1839 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001840
1841 /* As long as there is an int pending on device, keep
1842 * running loop. When none, get out, but if we've never
1843 * done any processing, call completion handler?
1844 */
1845 if (ack == 0) {
1846 /* No interrupts on device, but run the completion
1847 * processor anyway?
1848 */
1849 if (rc == 0)
1850 if (likely (skdev->state
1851 == SKD_DRVR_STATE_ONLINE))
1852 deferred = 1;
1853 break;
1854 }
1855
1856 rc = IRQ_HANDLED;
1857
1858 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
1859
1860 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
1861 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
1862 if (intstat & FIT_ISH_COMPLETION_POSTED) {
1863 /*
1864 * If we have already deferred completion
1865 * processing, don't bother running it again
1866 */
1867 if (deferred == 0)
1868 deferred =
1869 skd_isr_completion_posted(skdev,
1870 skd_isr_comp_limit, &flush_enqueued);
1871 }
1872
1873 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
1874 skd_isr_fwstate(skdev);
1875 if (skdev->state == SKD_DRVR_STATE_FAULT ||
1876 skdev->state ==
1877 SKD_DRVR_STATE_DISAPPEARED) {
1878 spin_unlock(&skdev->lock);
1879 return rc;
1880 }
1881 }
1882
1883 if (intstat & FIT_ISH_MSG_FROM_DEV)
1884 skd_isr_msg_from_dev(skdev);
1885 }
1886 }
1887
1888 if (unlikely(flush_enqueued))
Bart Van Assche8fe70062017-08-17 13:13:22 -07001889 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001890
1891 if (deferred)
1892 schedule_work(&skdev->completion_worker);
1893 else if (!flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07001894 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001895
1896 spin_unlock(&skdev->lock);
1897
1898 return rc;
1899}
1900
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001901static void skd_drive_fault(struct skd_device *skdev)
1902{
1903 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001904 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001905}
1906
1907static void skd_drive_disappeared(struct skd_device *skdev)
1908{
1909 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001910 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001911}
1912
1913static void skd_isr_fwstate(struct skd_device *skdev)
1914{
1915 u32 sense;
1916 u32 state;
1917 u32 mtd;
1918 int prev_driver_state = skdev->state;
1919
1920 sense = SKD_READL(skdev, FIT_STATUS);
1921 state = sense & FIT_SR_DRIVE_STATE_MASK;
1922
Bart Van Asschef98806d2017-08-17 13:12:58 -07001923 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
1924 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
1925 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001926
1927 skdev->drive_state = state;
1928
1929 switch (skdev->drive_state) {
1930 case FIT_SR_DRIVE_INIT:
1931 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
1932 skd_disable_interrupts(skdev);
1933 break;
1934 }
1935 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001936 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001937 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
1938 skdev->timer_countdown = SKD_STARTING_TIMO;
1939 skdev->state = SKD_DRVR_STATE_STARTING;
1940 skd_soft_reset(skdev);
1941 break;
1942 }
1943 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
1944 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1945 skdev->last_mtd = mtd;
1946 break;
1947
1948 case FIT_SR_DRIVE_ONLINE:
1949 skdev->cur_max_queue_depth = skd_max_queue_depth;
1950 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
1951 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
1952
1953 skdev->queue_low_water_mark =
1954 skdev->cur_max_queue_depth * 2 / 3 + 1;
1955 if (skdev->queue_low_water_mark < 1)
1956 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001957 dev_info(&skdev->pdev->dev,
1958 "Queue depth limit=%d dev=%d lowat=%d\n",
1959 skdev->cur_max_queue_depth,
1960 skdev->dev_max_queue_depth,
1961 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001962
1963 skd_refresh_device_data(skdev);
1964 break;
1965
1966 case FIT_SR_DRIVE_BUSY:
1967 skdev->state = SKD_DRVR_STATE_BUSY;
1968 skdev->timer_countdown = SKD_BUSY_TIMO;
1969 skd_quiesce_dev(skdev);
1970 break;
1971 case FIT_SR_DRIVE_BUSY_SANITIZE:
1972 /* set timer for 3 seconds, we'll abort any unfinished
1973 * commands after that expires
1974 */
1975 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
1976 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06001977 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001978 break;
1979 case FIT_SR_DRIVE_BUSY_ERASE:
1980 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
1981 skdev->timer_countdown = SKD_BUSY_TIMO;
1982 break;
1983 case FIT_SR_DRIVE_OFFLINE:
1984 skdev->state = SKD_DRVR_STATE_IDLE;
1985 break;
1986 case FIT_SR_DRIVE_SOFT_RESET:
1987 switch (skdev->state) {
1988 case SKD_DRVR_STATE_STARTING:
1989 case SKD_DRVR_STATE_RESTARTING:
1990 /* Expected by a caller of skd_soft_reset() */
1991 break;
1992 default:
1993 skdev->state = SKD_DRVR_STATE_RESTARTING;
1994 break;
1995 }
1996 break;
1997 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001998 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001999 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2000 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2001 break;
2002
2003 case FIT_SR_DRIVE_DEGRADED:
2004 case FIT_SR_PCIE_LINK_DOWN:
2005 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
2006 break;
2007
2008 case FIT_SR_DRIVE_FAULT:
2009 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002010 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002011 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002012 break;
2013
2014 /* PCIe bus returned all Fs? */
2015 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002016 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
2017 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002018 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002019 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002020 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002021 break;
2022 default:
2023 /*
2024 * Uknown FW State. Wait for a state we recognize.
2025 */
2026 break;
2027 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07002028 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2029 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
2030 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002031}
2032
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002033static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002034{
2035 int i;
2036
2037 for (i = 0; i < skdev->num_req_context; i++) {
2038 struct skd_request_context *skreq = &skdev->skreq_table[i];
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002039 struct request *req = skreq->req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002040
2041 if (skreq->state == SKD_REQ_STATE_BUSY) {
2042 skd_log_skreq(skdev, skreq, "recover");
2043
2044 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002045 SKD_ASSERT(req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002046
2047 /* Release DMA resources for the request. */
2048 if (skreq->n_sg > 0)
2049 skd_postop_sg_list(skdev, skreq);
2050
Jens Axboefcd37eb2013-11-01 10:14:56 -06002051 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002052
2053 skreq->state = SKD_REQ_STATE_IDLE;
2054 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002055
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002056 skd_end_request(skdev, req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002057 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002058 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002059
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002060 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07002061 atomic_set(&skdev->timeout_slot[i], 0);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002062
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07002063 atomic_set(&skdev->in_flight, 0);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002064}
2065
2066static void skd_isr_msg_from_dev(struct skd_device *skdev)
2067{
2068 u32 mfd;
2069 u32 mtd;
2070 u32 data;
2071
2072 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
2073
Bart Van Asschef98806d2017-08-17 13:12:58 -07002074 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
2075 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002076
2077 /* ignore any mtd that is an ack for something we didn't send */
2078 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
2079 return;
2080
2081 switch (FIT_MXD_TYPE(mfd)) {
2082 case FIT_MTD_FITFW_INIT:
2083 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
2084
2085 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002086 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
2087 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
2088 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
2089 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002090 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
2091 skd_soft_reset(skdev);
2092 break;
2093 }
2094 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
2095 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2096 skdev->last_mtd = mtd;
2097 break;
2098
2099 case FIT_MTD_GET_CMDQ_DEPTH:
2100 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
2101 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
2102 SKD_N_COMPLETION_ENTRY);
2103 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2104 skdev->last_mtd = mtd;
2105 break;
2106
2107 case FIT_MTD_SET_COMPQ_DEPTH:
2108 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
2109 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
2110 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2111 skdev->last_mtd = mtd;
2112 break;
2113
2114 case FIT_MTD_SET_COMPQ_ADDR:
2115 skd_reset_skcomp(skdev);
2116 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
2117 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2118 skdev->last_mtd = mtd;
2119 break;
2120
2121 case FIT_MTD_CMD_LOG_HOST_ID:
2122 skdev->connect_time_stamp = get_seconds();
2123 data = skdev->connect_time_stamp & 0xFFFF;
2124 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
2125 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2126 skdev->last_mtd = mtd;
2127 break;
2128
2129 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
2130 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
2131 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
2132 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
2133 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2134 skdev->last_mtd = mtd;
2135 break;
2136
2137 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
2138 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
2139 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
2140 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2141 skdev->last_mtd = mtd;
2142
Bart Van Asschef98806d2017-08-17 13:12:58 -07002143 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
2144 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002145 break;
2146
2147 case FIT_MTD_ARM_QUEUE:
2148 skdev->last_mtd = 0;
2149 /*
2150 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
2151 */
2152 break;
2153
2154 default:
2155 break;
2156 }
2157}
2158
2159static void skd_disable_interrupts(struct skd_device *skdev)
2160{
2161 u32 sense;
2162
2163 sense = SKD_READL(skdev, FIT_CONTROL);
2164 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
2165 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002166 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002167
2168 /* Note that the 1s is written. A 1-bit means
2169 * disable, a 0 means enable.
2170 */
2171 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
2172}
2173
2174static void skd_enable_interrupts(struct skd_device *skdev)
2175{
2176 u32 val;
2177
2178 /* unmask interrupts first */
2179 val = FIT_ISH_FW_STATE_CHANGE +
2180 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
2181
2182 /* Note that the compliment of mask is written. A 1-bit means
2183 * disable, a 0 means enable. */
2184 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002185 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002186
2187 val = SKD_READL(skdev, FIT_CONTROL);
2188 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002189 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002190 SKD_WRITEL(skdev, val, FIT_CONTROL);
2191}
2192
2193/*
2194 *****************************************************************************
2195 * START, STOP, RESTART, QUIESCE, UNQUIESCE
2196 *****************************************************************************
2197 */
2198
2199static void skd_soft_reset(struct skd_device *skdev)
2200{
2201 u32 val;
2202
2203 val = SKD_READL(skdev, FIT_CONTROL);
2204 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002205 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002206 SKD_WRITEL(skdev, val, FIT_CONTROL);
2207}
2208
2209static void skd_start_device(struct skd_device *skdev)
2210{
2211 unsigned long flags;
2212 u32 sense;
2213 u32 state;
2214
2215 spin_lock_irqsave(&skdev->lock, flags);
2216
2217 /* ack all ghost interrupts */
2218 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2219
2220 sense = SKD_READL(skdev, FIT_STATUS);
2221
Bart Van Asschef98806d2017-08-17 13:12:58 -07002222 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002223
2224 state = sense & FIT_SR_DRIVE_STATE_MASK;
2225 skdev->drive_state = state;
2226 skdev->last_mtd = 0;
2227
2228 skdev->state = SKD_DRVR_STATE_STARTING;
2229 skdev->timer_countdown = SKD_STARTING_TIMO;
2230
2231 skd_enable_interrupts(skdev);
2232
2233 switch (skdev->drive_state) {
2234 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002235 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002236 break;
2237
2238 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002239 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002240 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2241 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2242 break;
2243
2244 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002245 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002246 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2247 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2248 break;
2249
2250 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002251 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002252 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2253 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2254 break;
2255
2256 case FIT_SR_DRIVE_INIT:
2257 case FIT_SR_DRIVE_ONLINE:
2258 skd_soft_reset(skdev);
2259 break;
2260
2261 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002262 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002263 skdev->state = SKD_DRVR_STATE_BUSY;
2264 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2265 break;
2266
2267 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002268 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002269 break;
2270
2271 case FIT_SR_DRIVE_FAULT:
2272 /* Fault state is bad...soft reset won't do it...
2273 * Hard reset, maybe, but does it work on device?
2274 * For now, just fault so the system doesn't hang.
2275 */
2276 skd_drive_fault(skdev);
2277 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002278 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002279 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002280 skdev->gendisk_on = -1;
2281 wake_up_interruptible(&skdev->waitq);
2282 break;
2283
2284 case 0xFF:
2285 /* Most likely the device isn't there or isn't responding
2286 * to the BAR1 addresses. */
2287 skd_drive_disappeared(skdev);
2288 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002289 dev_dbg(&skdev->pdev->dev,
2290 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002291 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002292 skdev->gendisk_on = -1;
2293 wake_up_interruptible(&skdev->waitq);
2294 break;
2295
2296 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002297 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
2298 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002299 break;
2300 }
2301
2302 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002303 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002304
2305 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002306 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002307
2308 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002309 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002310
2311 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002312 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002313
2314 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002315 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002316
2317 spin_unlock_irqrestore(&skdev->lock, flags);
2318}
2319
2320static void skd_stop_device(struct skd_device *skdev)
2321{
2322 unsigned long flags;
2323 struct skd_special_context *skspcl = &skdev->internal_skspcl;
2324 u32 dev_state;
2325 int i;
2326
2327 spin_lock_irqsave(&skdev->lock, flags);
2328
2329 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002330 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002331 goto stop_out;
2332 }
2333
2334 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002335 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002336 goto stop_out;
2337 }
2338
2339 skdev->state = SKD_DRVR_STATE_SYNCING;
2340 skdev->sync_done = 0;
2341
2342 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
2343
2344 spin_unlock_irqrestore(&skdev->lock, flags);
2345
2346 wait_event_interruptible_timeout(skdev->waitq,
2347 (skdev->sync_done), (10 * HZ));
2348
2349 spin_lock_irqsave(&skdev->lock, flags);
2350
2351 switch (skdev->sync_done) {
2352 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002353 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002354 break;
2355 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002356 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002357 break;
2358 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002359 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002360 }
2361
2362stop_out:
2363 skdev->state = SKD_DRVR_STATE_STOPPING;
2364 spin_unlock_irqrestore(&skdev->lock, flags);
2365
2366 skd_kill_timer(skdev);
2367
2368 spin_lock_irqsave(&skdev->lock, flags);
2369 skd_disable_interrupts(skdev);
2370
2371 /* ensure all ints on device are cleared */
2372 /* soft reset the device to unload with a clean slate */
2373 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2374 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
2375
2376 spin_unlock_irqrestore(&skdev->lock, flags);
2377
2378 /* poll every 100ms, 1 second timeout */
2379 for (i = 0; i < 10; i++) {
2380 dev_state =
2381 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
2382 if (dev_state == FIT_SR_DRIVE_INIT)
2383 break;
2384 set_current_state(TASK_INTERRUPTIBLE);
2385 schedule_timeout(msecs_to_jiffies(100));
2386 }
2387
2388 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07002389 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
2390 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002391}
2392
2393/* assume spinlock is held */
2394static void skd_restart_device(struct skd_device *skdev)
2395{
2396 u32 state;
2397
2398 /* ack all ghost interrupts */
2399 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2400
2401 state = SKD_READL(skdev, FIT_STATUS);
2402
Bart Van Asschef98806d2017-08-17 13:12:58 -07002403 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002404
2405 state &= FIT_SR_DRIVE_STATE_MASK;
2406 skdev->drive_state = state;
2407 skdev->last_mtd = 0;
2408
2409 skdev->state = SKD_DRVR_STATE_RESTARTING;
2410 skdev->timer_countdown = SKD_RESTARTING_TIMO;
2411
2412 skd_soft_reset(skdev);
2413}
2414
2415/* assume spinlock is held */
2416static int skd_quiesce_dev(struct skd_device *skdev)
2417{
2418 int rc = 0;
2419
2420 switch (skdev->state) {
2421 case SKD_DRVR_STATE_BUSY:
2422 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002423 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002424 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002425 break;
2426 case SKD_DRVR_STATE_ONLINE:
2427 case SKD_DRVR_STATE_STOPPING:
2428 case SKD_DRVR_STATE_SYNCING:
2429 case SKD_DRVR_STATE_PAUSING:
2430 case SKD_DRVR_STATE_PAUSED:
2431 case SKD_DRVR_STATE_STARTING:
2432 case SKD_DRVR_STATE_RESTARTING:
2433 case SKD_DRVR_STATE_RESUMING:
2434 default:
2435 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002436 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
2437 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002438 }
2439 return rc;
2440}
2441
2442/* assume spinlock is held */
2443static int skd_unquiesce_dev(struct skd_device *skdev)
2444{
2445 int prev_driver_state = skdev->state;
2446
2447 skd_log_skdev(skdev, "unquiesce");
2448 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002449 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002450 return 0;
2451 }
2452 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
2453 /*
2454 * If there has been an state change to other than
2455 * ONLINE, we will rely on controller state change
2456 * to come back online and restart the queue.
2457 * The BUSY state means that driver is ready to
2458 * continue normal processing but waiting for controller
2459 * to become available.
2460 */
2461 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002462 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002463 return 0;
2464 }
2465
2466 /*
2467 * Drive has just come online, driver is either in startup,
2468 * paused performing a task, or bust waiting for hardware.
2469 */
2470 switch (skdev->state) {
2471 case SKD_DRVR_STATE_PAUSED:
2472 case SKD_DRVR_STATE_BUSY:
2473 case SKD_DRVR_STATE_BUSY_IMMINENT:
2474 case SKD_DRVR_STATE_BUSY_ERASE:
2475 case SKD_DRVR_STATE_STARTING:
2476 case SKD_DRVR_STATE_RESTARTING:
2477 case SKD_DRVR_STATE_FAULT:
2478 case SKD_DRVR_STATE_IDLE:
2479 case SKD_DRVR_STATE_LOAD:
2480 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002481 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2482 skd_skdev_state_to_str(prev_driver_state),
2483 prev_driver_state, skd_skdev_state_to_str(skdev->state),
2484 skdev->state);
2485 dev_dbg(&skdev->pdev->dev,
2486 "**** device ONLINE...starting block queue\n");
2487 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2488 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002489 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002490 skdev->gendisk_on = 1;
2491 wake_up_interruptible(&skdev->waitq);
2492 break;
2493
2494 case SKD_DRVR_STATE_DISAPPEARED:
2495 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002496 dev_dbg(&skdev->pdev->dev,
2497 "**** driver state %d, not implemented\n",
2498 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002499 return -EBUSY;
2500 }
2501 return 0;
2502}
2503
2504/*
2505 *****************************************************************************
2506 * PCIe MSI/MSI-X INTERRUPT HANDLERS
2507 *****************************************************************************
2508 */
2509
2510static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
2511{
2512 struct skd_device *skdev = skd_host_data;
2513 unsigned long flags;
2514
2515 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002516 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2517 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2518 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
2519 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002520 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
2521 spin_unlock_irqrestore(&skdev->lock, flags);
2522 return IRQ_HANDLED;
2523}
2524
2525static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
2526{
2527 struct skd_device *skdev = skd_host_data;
2528 unsigned long flags;
2529
2530 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002531 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2532 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002533 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
2534 skd_isr_fwstate(skdev);
2535 spin_unlock_irqrestore(&skdev->lock, flags);
2536 return IRQ_HANDLED;
2537}
2538
2539static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
2540{
2541 struct skd_device *skdev = skd_host_data;
2542 unsigned long flags;
2543 int flush_enqueued = 0;
2544 int deferred;
2545
2546 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002547 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2548 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002549 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
2550 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
2551 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002552 if (flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07002553 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002554
2555 if (deferred)
2556 schedule_work(&skdev->completion_worker);
2557 else if (!flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07002558 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002559
2560 spin_unlock_irqrestore(&skdev->lock, flags);
2561
2562 return IRQ_HANDLED;
2563}
2564
2565static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
2566{
2567 struct skd_device *skdev = skd_host_data;
2568 unsigned long flags;
2569
2570 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002571 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2572 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002573 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
2574 skd_isr_msg_from_dev(skdev);
2575 spin_unlock_irqrestore(&skdev->lock, flags);
2576 return IRQ_HANDLED;
2577}
2578
2579static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
2580{
2581 struct skd_device *skdev = skd_host_data;
2582 unsigned long flags;
2583
2584 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002585 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2586 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002587 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
2588 spin_unlock_irqrestore(&skdev->lock, flags);
2589 return IRQ_HANDLED;
2590}
2591
2592/*
2593 *****************************************************************************
2594 * PCIe MSI/MSI-X SETUP
2595 *****************************************************************************
2596 */
2597
2598struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002599 char isr_name[30];
2600};
2601
2602struct skd_init_msix_entry {
2603 const char *name;
2604 irq_handler_t handler;
2605};
2606
2607#define SKD_MAX_MSIX_COUNT 13
2608#define SKD_MIN_MSIX_COUNT 7
2609#define SKD_BASE_MSIX_IRQ 4
2610
2611static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
2612 { "(DMA 0)", skd_reserved_isr },
2613 { "(DMA 1)", skd_reserved_isr },
2614 { "(DMA 2)", skd_reserved_isr },
2615 { "(DMA 3)", skd_reserved_isr },
2616 { "(State Change)", skd_statec_isr },
2617 { "(COMPL_Q)", skd_comp_q },
2618 { "(MSG)", skd_msg_isr },
2619 { "(Reserved)", skd_reserved_isr },
2620 { "(Reserved)", skd_reserved_isr },
2621 { "(Queue Full 0)", skd_qfull_isr },
2622 { "(Queue Full 1)", skd_qfull_isr },
2623 { "(Queue Full 2)", skd_qfull_isr },
2624 { "(Queue Full 3)", skd_qfull_isr },
2625};
2626
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002627static int skd_acquire_msix(struct skd_device *skdev)
2628{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01002629 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01002630 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002631
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002632 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
2633 PCI_IRQ_MSIX);
2634 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002635 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002636 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002637 }
Alexander Gordeev46817762014-02-19 09:58:19 +01002638
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002639 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
2640 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002641 if (!skdev->msix_entries) {
2642 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002643 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002644 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002645 }
2646
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002647 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002648 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2649 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
2650
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002651 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
2652 "%s%d-msix %s", DRV_NAME, skdev->devno,
2653 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002654
2655 rc = devm_request_irq(&skdev->pdev->dev,
2656 pci_irq_vector(skdev->pdev, i),
2657 msix_entries[i].handler, 0,
2658 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002659 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002660 dev_err(&skdev->pdev->dev,
2661 "Unable to register(%d) MSI-X handler %d: %s\n",
2662 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002663 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002664 }
2665 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002666
Bart Van Asschef98806d2017-08-17 13:12:58 -07002667 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
2668 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002669 return 0;
2670
2671msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002672 while (--i >= 0)
2673 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002674out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002675 kfree(skdev->msix_entries);
2676 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002677 return rc;
2678}
2679
2680static int skd_acquire_irq(struct skd_device *skdev)
2681{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002682 struct pci_dev *pdev = skdev->pdev;
2683 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002684 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002685
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002686 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002687 rc = skd_acquire_msix(skdev);
2688 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002689 return 0;
2690
Bart Van Asschef98806d2017-08-17 13:12:58 -07002691 dev_err(&skdev->pdev->dev,
2692 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002693 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002694
2695 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
2696 skdev->devno);
2697
2698 if (skd_isr_type != SKD_IRQ_LEGACY)
2699 irq_flag |= PCI_IRQ_MSI;
2700 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
2701 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002702 dev_err(&skdev->pdev->dev,
2703 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002704 return rc;
2705 }
2706
2707 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
2708 pdev->msi_enabled ? 0 : IRQF_SHARED,
2709 skdev->isr_name, skdev);
2710 if (rc) {
2711 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002712 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
2713 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002714 return rc;
2715 }
2716
2717 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002718}
2719
2720static void skd_release_irq(struct skd_device *skdev)
2721{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002722 struct pci_dev *pdev = skdev->pdev;
2723
2724 if (skdev->msix_entries) {
2725 int i;
2726
2727 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2728 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
2729 skdev);
2730 }
2731
2732 kfree(skdev->msix_entries);
2733 skdev->msix_entries = NULL;
2734 } else {
2735 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002736 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002737
2738 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002739}
2740
2741/*
2742 *****************************************************************************
2743 * CONSTRUCT
2744 *****************************************************************************
2745 */
2746
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002747static int skd_cons_skcomp(struct skd_device *skdev)
2748{
2749 int rc = 0;
2750 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002751
Bart Van Asschef98806d2017-08-17 13:12:58 -07002752 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002753 "comp pci_alloc, total bytes %zd entries %d\n",
2754 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002755
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002756 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07002757 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002758
2759 if (skcomp == NULL) {
2760 rc = -ENOMEM;
2761 goto err_out;
2762 }
2763
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002764 skdev->skcomp_table = skcomp;
2765 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
2766 sizeof(*skcomp) *
2767 SKD_N_COMPLETION_ENTRY);
2768
2769err_out:
2770 return rc;
2771}
2772
2773static int skd_cons_skmsg(struct skd_device *skdev)
2774{
2775 int rc = 0;
2776 u32 i;
2777
Bart Van Asschef98806d2017-08-17 13:12:58 -07002778 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002779 "skmsg_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002780 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
2781 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002782
Bart Van Assche01433d02017-08-17 13:13:18 -07002783 skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context,
2784 sizeof(struct skd_fitmsg_context),
2785 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002786 if (skdev->skmsg_table == NULL) {
2787 rc = -ENOMEM;
2788 goto err_out;
2789 }
2790
2791 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2792 struct skd_fitmsg_context *skmsg;
2793
2794 skmsg = &skdev->skmsg_table[i];
2795
2796 skmsg->id = i + SKD_ID_FIT_MSG;
2797
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002798 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07002799 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002800 &skmsg->mb_dma_address);
2801
2802 if (skmsg->msg_buf == NULL) {
2803 rc = -ENOMEM;
2804 goto err_out;
2805 }
2806
Bart Van Assche6507f432017-08-17 13:13:06 -07002807 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
2808 (FIT_QCMD_ALIGN - 1),
2809 "not aligned: msg_buf %p mb_dma_address %#llx\n",
2810 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002811 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002812 }
2813
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002814err_out:
2815 return rc;
2816}
2817
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002818static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
2819 u32 n_sg,
2820 dma_addr_t *ret_dma_addr)
2821{
2822 struct fit_sg_descriptor *sg_list;
2823 u32 nbytes;
2824
2825 nbytes = sizeof(*sg_list) * n_sg;
2826
2827 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
2828
2829 if (sg_list != NULL) {
2830 uint64_t dma_address = *ret_dma_addr;
2831 u32 i;
2832
2833 memset(sg_list, 0, nbytes);
2834
2835 for (i = 0; i < n_sg - 1; i++) {
2836 uint64_t ndp_off;
2837 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
2838
2839 sg_list[i].next_desc_ptr = dma_address + ndp_off;
2840 }
2841 sg_list[i].next_desc_ptr = 0LL;
2842 }
2843
2844 return sg_list;
2845}
2846
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002847static int skd_cons_skreq(struct skd_device *skdev)
2848{
2849 int rc = 0;
2850 u32 i;
2851
Bart Van Asschef98806d2017-08-17 13:12:58 -07002852 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002853 "skreq_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002854 sizeof(struct skd_request_context), skdev->num_req_context,
2855 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002856
Bart Van Assche01433d02017-08-17 13:13:18 -07002857 skdev->skreq_table = kcalloc(skdev->num_req_context,
2858 sizeof(struct skd_request_context),
2859 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002860 if (skdev->skreq_table == NULL) {
2861 rc = -ENOMEM;
2862 goto err_out;
2863 }
2864
Bart Van Asschef98806d2017-08-17 13:12:58 -07002865 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
2866 skdev->sgs_per_request, sizeof(struct scatterlist),
2867 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002868
2869 for (i = 0; i < skdev->num_req_context; i++) {
2870 struct skd_request_context *skreq;
2871
2872 skreq = &skdev->skreq_table[i];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002873 skreq->state = SKD_REQ_STATE_IDLE;
Bart Van Assche01433d02017-08-17 13:13:18 -07002874 skreq->sg = kcalloc(skdev->sgs_per_request,
2875 sizeof(struct scatterlist), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002876 if (skreq->sg == NULL) {
2877 rc = -ENOMEM;
2878 goto err_out;
2879 }
2880 sg_init_table(skreq->sg, skdev->sgs_per_request);
2881
2882 skreq->sksg_list = skd_cons_sg_list(skdev,
2883 skdev->sgs_per_request,
2884 &skreq->sksg_dma_address);
2885
2886 if (skreq->sksg_list == NULL) {
2887 rc = -ENOMEM;
2888 goto err_out;
2889 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002890 }
2891
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002892err_out:
2893 return rc;
2894}
2895
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002896static int skd_cons_sksb(struct skd_device *skdev)
2897{
2898 int rc = 0;
2899 struct skd_special_context *skspcl;
2900 u32 nbytes;
2901
2902 skspcl = &skdev->internal_skspcl;
2903
2904 skspcl->req.id = 0 + SKD_ID_INTERNAL;
2905 skspcl->req.state = SKD_REQ_STATE_IDLE;
2906
2907 nbytes = SKD_N_INTERNAL_BYTES;
2908
Joe Perchesa5bbf612014-08-08 14:24:12 -07002909 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
2910 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002911 if (skspcl->data_buf == NULL) {
2912 rc = -ENOMEM;
2913 goto err_out;
2914 }
2915
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002916 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07002917 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
2918 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002919 if (skspcl->msg_buf == NULL) {
2920 rc = -ENOMEM;
2921 goto err_out;
2922 }
2923
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002924 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
2925 &skspcl->req.sksg_dma_address);
2926 if (skspcl->req.sksg_list == NULL) {
2927 rc = -ENOMEM;
2928 goto err_out;
2929 }
2930
2931 if (!skd_format_internal_skspcl(skdev)) {
2932 rc = -EINVAL;
2933 goto err_out;
2934 }
2935
2936err_out:
2937 return rc;
2938}
2939
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002940static int skd_cons_disk(struct skd_device *skdev)
2941{
2942 int rc = 0;
2943 struct gendisk *disk;
2944 struct request_queue *q;
2945 unsigned long flags;
2946
2947 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
2948 if (!disk) {
2949 rc = -ENOMEM;
2950 goto err_out;
2951 }
2952
2953 skdev->disk = disk;
2954 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
2955
2956 disk->major = skdev->major;
2957 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
2958 disk->fops = &skd_blockdev_ops;
2959 disk->private_data = skdev;
2960
Jens Axboefcd37eb2013-11-01 10:14:56 -06002961 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002962 if (!q) {
2963 rc = -ENOMEM;
2964 goto err_out;
2965 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02002966 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002967 q->nr_requests = skd_max_queue_depth / 2;
2968 blk_queue_init_tags(q, skd_max_queue_depth, NULL, BLK_TAG_ALLOC_FIFO);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002969
2970 skdev->queue = q;
2971 disk->queue = q;
2972 q->queuedata = skdev;
2973
Jens Axboe6975f732016-03-30 10:11:42 -06002974 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002975 blk_queue_max_segments(q, skdev->sgs_per_request);
2976 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
2977
Bart Van Asschea5c5b392017-08-17 13:12:53 -07002978 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002979 blk_queue_io_opt(q, 8192);
2980
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002981 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06002982 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002983
2984 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002985 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002986 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002987 spin_unlock_irqrestore(&skdev->lock, flags);
2988
2989err_out:
2990 return rc;
2991}
2992
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002993#define SKD_N_DEV_TABLE 16u
2994static u32 skd_next_devno;
2995
2996static struct skd_device *skd_construct(struct pci_dev *pdev)
2997{
2998 struct skd_device *skdev;
2999 int blk_major = skd_major;
3000 int rc;
3001
3002 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
3003
3004 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003005 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003006 return NULL;
3007 }
3008
3009 skdev->state = SKD_DRVR_STATE_LOAD;
3010 skdev->pdev = pdev;
3011 skdev->devno = skd_next_devno++;
3012 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003013 skdev->dev_max_queue_depth = 0;
3014
3015 skdev->num_req_context = skd_max_queue_depth;
3016 skdev->num_fitmsg_context = skd_max_queue_depth;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003017 skdev->cur_max_queue_depth = 1;
3018 skdev->queue_low_water_mark = 1;
3019 skdev->proto_ver = 99;
3020 skdev->sgs_per_request = skd_sgs_per_request;
3021 skdev->dbg_level = skd_dbg_level;
3022
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003023 spin_lock_init(&skdev->lock);
3024
3025 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
3026
Bart Van Asschef98806d2017-08-17 13:12:58 -07003027 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003028 rc = skd_cons_skcomp(skdev);
3029 if (rc < 0)
3030 goto err_out;
3031
Bart Van Asschef98806d2017-08-17 13:12:58 -07003032 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003033 rc = skd_cons_skmsg(skdev);
3034 if (rc < 0)
3035 goto err_out;
3036
Bart Van Asschef98806d2017-08-17 13:12:58 -07003037 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003038 rc = skd_cons_skreq(skdev);
3039 if (rc < 0)
3040 goto err_out;
3041
Bart Van Asschef98806d2017-08-17 13:12:58 -07003042 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003043 rc = skd_cons_sksb(skdev);
3044 if (rc < 0)
3045 goto err_out;
3046
Bart Van Asschef98806d2017-08-17 13:12:58 -07003047 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003048 rc = skd_cons_disk(skdev);
3049 if (rc < 0)
3050 goto err_out;
3051
Bart Van Asschef98806d2017-08-17 13:12:58 -07003052 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003053 return skdev;
3054
3055err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003056 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003057 skd_destruct(skdev);
3058 return NULL;
3059}
3060
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003061/*
3062 *****************************************************************************
3063 * DESTRUCT (FREE)
3064 *****************************************************************************
3065 */
3066
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003067static void skd_free_skcomp(struct skd_device *skdev)
3068{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07003069 if (skdev->skcomp_table)
3070 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003071 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003072
3073 skdev->skcomp_table = NULL;
3074 skdev->cq_dma_address = 0;
3075}
3076
3077static void skd_free_skmsg(struct skd_device *skdev)
3078{
3079 u32 i;
3080
3081 if (skdev->skmsg_table == NULL)
3082 return;
3083
3084 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3085 struct skd_fitmsg_context *skmsg;
3086
3087 skmsg = &skdev->skmsg_table[i];
3088
3089 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003090 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
3091 skmsg->msg_buf,
3092 skmsg->mb_dma_address);
3093 }
3094 skmsg->msg_buf = NULL;
3095 skmsg->mb_dma_address = 0;
3096 }
3097
3098 kfree(skdev->skmsg_table);
3099 skdev->skmsg_table = NULL;
3100}
3101
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003102static void skd_free_sg_list(struct skd_device *skdev,
3103 struct fit_sg_descriptor *sg_list,
3104 u32 n_sg, dma_addr_t dma_addr)
3105{
3106 if (sg_list != NULL) {
3107 u32 nbytes;
3108
3109 nbytes = sizeof(*sg_list) * n_sg;
3110
3111 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
3112 }
3113}
3114
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003115static void skd_free_skreq(struct skd_device *skdev)
3116{
3117 u32 i;
3118
3119 if (skdev->skreq_table == NULL)
3120 return;
3121
3122 for (i = 0; i < skdev->num_req_context; i++) {
3123 struct skd_request_context *skreq;
3124
3125 skreq = &skdev->skreq_table[i];
3126
3127 skd_free_sg_list(skdev, skreq->sksg_list,
3128 skdev->sgs_per_request,
3129 skreq->sksg_dma_address);
3130
3131 skreq->sksg_list = NULL;
3132 skreq->sksg_dma_address = 0;
3133
3134 kfree(skreq->sg);
3135 }
3136
3137 kfree(skdev->skreq_table);
3138 skdev->skreq_table = NULL;
3139}
3140
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003141static void skd_free_sksb(struct skd_device *skdev)
3142{
3143 struct skd_special_context *skspcl;
3144 u32 nbytes;
3145
3146 skspcl = &skdev->internal_skspcl;
3147
3148 if (skspcl->data_buf != NULL) {
3149 nbytes = SKD_N_INTERNAL_BYTES;
3150
3151 pci_free_consistent(skdev->pdev, nbytes,
3152 skspcl->data_buf, skspcl->db_dma_address);
3153 }
3154
3155 skspcl->data_buf = NULL;
3156 skspcl->db_dma_address = 0;
3157
3158 if (skspcl->msg_buf != NULL) {
3159 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3160 pci_free_consistent(skdev->pdev, nbytes,
3161 skspcl->msg_buf, skspcl->mb_dma_address);
3162 }
3163
3164 skspcl->msg_buf = NULL;
3165 skspcl->mb_dma_address = 0;
3166
3167 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
3168 skspcl->req.sksg_dma_address);
3169
3170 skspcl->req.sksg_list = NULL;
3171 skspcl->req.sksg_dma_address = 0;
3172}
3173
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003174static void skd_free_disk(struct skd_device *skdev)
3175{
3176 struct gendisk *disk = skdev->disk;
3177
Bart Van Assche7277cc62017-08-17 13:12:45 -07003178 if (disk && (disk->flags & GENHD_FL_UP))
3179 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003180
Bart Van Assche7277cc62017-08-17 13:12:45 -07003181 if (skdev->queue) {
3182 blk_cleanup_queue(skdev->queue);
3183 skdev->queue = NULL;
3184 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003185 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07003186
3187 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003188 skdev->disk = NULL;
3189}
3190
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003191static void skd_destruct(struct skd_device *skdev)
3192{
3193 if (skdev == NULL)
3194 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003195
Bart Van Asschef98806d2017-08-17 13:12:58 -07003196 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003197 skd_free_disk(skdev);
3198
Bart Van Asschef98806d2017-08-17 13:12:58 -07003199 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003200 skd_free_sksb(skdev);
3201
Bart Van Asschef98806d2017-08-17 13:12:58 -07003202 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003203 skd_free_skreq(skdev);
3204
Bart Van Asschef98806d2017-08-17 13:12:58 -07003205 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003206 skd_free_skmsg(skdev);
3207
Bart Van Asschef98806d2017-08-17 13:12:58 -07003208 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003209 skd_free_skcomp(skdev);
3210
Bart Van Asschef98806d2017-08-17 13:12:58 -07003211 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003212 kfree(skdev);
3213}
3214
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003215/*
3216 *****************************************************************************
3217 * BLOCK DEVICE (BDEV) GLUE
3218 *****************************************************************************
3219 */
3220
3221static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3222{
3223 struct skd_device *skdev;
3224 u64 capacity;
3225
3226 skdev = bdev->bd_disk->private_data;
3227
Bart Van Asschef98806d2017-08-17 13:12:58 -07003228 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
3229 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003230
3231 if (skdev->read_cap_is_valid) {
3232 capacity = get_capacity(skdev->disk);
3233 geo->heads = 64;
3234 geo->sectors = 255;
3235 geo->cylinders = (capacity) / (255 * 64);
3236
3237 return 0;
3238 }
3239 return -EIO;
3240}
3241
Dan Williams0d52c7562016-06-15 19:44:20 -07003242static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003243{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003244 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07003245 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003246 return 0;
3247}
3248
3249static const struct block_device_operations skd_blockdev_ops = {
3250 .owner = THIS_MODULE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003251 .getgeo = skd_bdev_getgeo,
3252};
3253
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003254/*
3255 *****************************************************************************
3256 * PCIe DRIVER GLUE
3257 *****************************************************************************
3258 */
3259
Benoit Taine9baa3c32014-08-08 15:56:03 +02003260static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003261 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
3262 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
3263 { 0 } /* terminate list */
3264};
3265
3266MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
3267
3268static char *skd_pci_info(struct skd_device *skdev, char *str)
3269{
3270 int pcie_reg;
3271
3272 strcpy(str, "PCIe (");
3273 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
3274
3275 if (pcie_reg) {
3276
3277 char lwstr[6];
3278 uint16_t pcie_lstat, lspeed, lwidth;
3279
3280 pcie_reg += 0x12;
3281 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
3282 lspeed = pcie_lstat & (0xF);
3283 lwidth = (pcie_lstat & 0x3F0) >> 4;
3284
3285 if (lspeed == 1)
3286 strcat(str, "2.5GT/s ");
3287 else if (lspeed == 2)
3288 strcat(str, "5.0GT/s ");
3289 else
3290 strcat(str, "<unknown> ");
3291 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
3292 strcat(str, lwstr);
3293 }
3294 return str;
3295}
3296
3297static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3298{
3299 int i;
3300 int rc = 0;
3301 char pci_str[32];
3302 struct skd_device *skdev;
3303
Bart Van Asschef98806d2017-08-17 13:12:58 -07003304 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
3305 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
3306 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
3307 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003308
3309 rc = pci_enable_device(pdev);
3310 if (rc)
3311 return rc;
3312 rc = pci_request_regions(pdev, DRV_NAME);
3313 if (rc)
3314 goto err_out;
3315 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3316 if (!rc) {
3317 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003318 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3319 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003320 }
3321 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003322 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003323 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003324 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003325 goto err_out_regions;
3326 }
3327 }
3328
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003329 if (!skd_major) {
3330 rc = register_blkdev(0, DRV_NAME);
3331 if (rc < 0)
3332 goto err_out_regions;
3333 BUG_ON(!rc);
3334 skd_major = rc;
3335 }
3336
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003337 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08003338 if (skdev == NULL) {
3339 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003340 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08003341 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003342
3343 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003344 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003345
3346 pci_set_master(pdev);
3347 rc = pci_enable_pcie_error_reporting(pdev);
3348 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003349 dev_err(&pdev->dev,
3350 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003351 skdev->pcie_error_reporting_is_enabled = 0;
3352 } else
3353 skdev->pcie_error_reporting_is_enabled = 1;
3354
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003355 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01003356
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003357 for (i = 0; i < SKD_MAX_BARS; i++) {
3358 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3359 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3360 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3361 skdev->mem_size[i]);
3362 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003363 dev_err(&pdev->dev,
3364 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003365 rc = -ENODEV;
3366 goto err_out_iounmap;
3367 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003368 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3369 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3370 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003371 }
3372
3373 rc = skd_acquire_irq(skdev);
3374 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003375 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003376 goto err_out_iounmap;
3377 }
3378
3379 rc = skd_start_timer(skdev);
3380 if (rc)
3381 goto err_out_timer;
3382
3383 init_waitqueue_head(&skdev->waitq);
3384
3385 skd_start_device(skdev);
3386
3387 rc = wait_event_interruptible_timeout(skdev->waitq,
3388 (skdev->gendisk_on),
3389 (SKD_START_WAIT_SECONDS * HZ));
3390 if (skdev->gendisk_on > 0) {
3391 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07003392 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003393 rc = 0;
3394 } else {
3395 /* we timed out, something is wrong with the device,
3396 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003397 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
3398 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003399 /* in case of no error; we timeout with ENXIO */
3400 if (!rc)
3401 rc = -ENXIO;
3402 goto err_out_timer;
3403 }
3404
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003405 return rc;
3406
3407err_out_timer:
3408 skd_stop_device(skdev);
3409 skd_release_irq(skdev);
3410
3411err_out_iounmap:
3412 for (i = 0; i < SKD_MAX_BARS; i++)
3413 if (skdev->mem_map[i])
3414 iounmap(skdev->mem_map[i]);
3415
3416 if (skdev->pcie_error_reporting_is_enabled)
3417 pci_disable_pcie_error_reporting(pdev);
3418
3419 skd_destruct(skdev);
3420
3421err_out_regions:
3422 pci_release_regions(pdev);
3423
3424err_out:
3425 pci_disable_device(pdev);
3426 pci_set_drvdata(pdev, NULL);
3427 return rc;
3428}
3429
3430static void skd_pci_remove(struct pci_dev *pdev)
3431{
3432 int i;
3433 struct skd_device *skdev;
3434
3435 skdev = pci_get_drvdata(pdev);
3436 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003437 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003438 return;
3439 }
3440 skd_stop_device(skdev);
3441 skd_release_irq(skdev);
3442
3443 for (i = 0; i < SKD_MAX_BARS; i++)
3444 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003445 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003446
3447 if (skdev->pcie_error_reporting_is_enabled)
3448 pci_disable_pcie_error_reporting(pdev);
3449
3450 skd_destruct(skdev);
3451
3452 pci_release_regions(pdev);
3453 pci_disable_device(pdev);
3454 pci_set_drvdata(pdev, NULL);
3455
3456 return;
3457}
3458
3459static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3460{
3461 int i;
3462 struct skd_device *skdev;
3463
3464 skdev = pci_get_drvdata(pdev);
3465 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003466 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003467 return -EIO;
3468 }
3469
3470 skd_stop_device(skdev);
3471
3472 skd_release_irq(skdev);
3473
3474 for (i = 0; i < SKD_MAX_BARS; i++)
3475 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003476 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003477
3478 if (skdev->pcie_error_reporting_is_enabled)
3479 pci_disable_pcie_error_reporting(pdev);
3480
3481 pci_release_regions(pdev);
3482 pci_save_state(pdev);
3483 pci_disable_device(pdev);
3484 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3485 return 0;
3486}
3487
3488static int skd_pci_resume(struct pci_dev *pdev)
3489{
3490 int i;
3491 int rc = 0;
3492 struct skd_device *skdev;
3493
3494 skdev = pci_get_drvdata(pdev);
3495 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003496 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003497 return -1;
3498 }
3499
3500 pci_set_power_state(pdev, PCI_D0);
3501 pci_enable_wake(pdev, PCI_D0, 0);
3502 pci_restore_state(pdev);
3503
3504 rc = pci_enable_device(pdev);
3505 if (rc)
3506 return rc;
3507 rc = pci_request_regions(pdev, DRV_NAME);
3508 if (rc)
3509 goto err_out;
3510 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3511 if (!rc) {
3512 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3513
Bart Van Asschef98806d2017-08-17 13:12:58 -07003514 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3515 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003516 }
3517 } else {
3518 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3519 if (rc) {
3520
Bart Van Asschef98806d2017-08-17 13:12:58 -07003521 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003522 goto err_out_regions;
3523 }
3524 }
3525
3526 pci_set_master(pdev);
3527 rc = pci_enable_pcie_error_reporting(pdev);
3528 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003529 dev_err(&pdev->dev,
3530 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003531 skdev->pcie_error_reporting_is_enabled = 0;
3532 } else
3533 skdev->pcie_error_reporting_is_enabled = 1;
3534
3535 for (i = 0; i < SKD_MAX_BARS; i++) {
3536
3537 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3538 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3539 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3540 skdev->mem_size[i]);
3541 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003542 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003543 rc = -ENODEV;
3544 goto err_out_iounmap;
3545 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003546 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3547 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3548 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003549 }
3550 rc = skd_acquire_irq(skdev);
3551 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003552 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003553 goto err_out_iounmap;
3554 }
3555
3556 rc = skd_start_timer(skdev);
3557 if (rc)
3558 goto err_out_timer;
3559
3560 init_waitqueue_head(&skdev->waitq);
3561
3562 skd_start_device(skdev);
3563
3564 return rc;
3565
3566err_out_timer:
3567 skd_stop_device(skdev);
3568 skd_release_irq(skdev);
3569
3570err_out_iounmap:
3571 for (i = 0; i < SKD_MAX_BARS; i++)
3572 if (skdev->mem_map[i])
3573 iounmap(skdev->mem_map[i]);
3574
3575 if (skdev->pcie_error_reporting_is_enabled)
3576 pci_disable_pcie_error_reporting(pdev);
3577
3578err_out_regions:
3579 pci_release_regions(pdev);
3580
3581err_out:
3582 pci_disable_device(pdev);
3583 return rc;
3584}
3585
3586static void skd_pci_shutdown(struct pci_dev *pdev)
3587{
3588 struct skd_device *skdev;
3589
Bart Van Asschef98806d2017-08-17 13:12:58 -07003590 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003591
3592 skdev = pci_get_drvdata(pdev);
3593 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003594 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003595 return;
3596 }
3597
Bart Van Asschef98806d2017-08-17 13:12:58 -07003598 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003599 skd_stop_device(skdev);
3600}
3601
3602static struct pci_driver skd_driver = {
3603 .name = DRV_NAME,
3604 .id_table = skd_pci_tbl,
3605 .probe = skd_pci_probe,
3606 .remove = skd_pci_remove,
3607 .suspend = skd_pci_suspend,
3608 .resume = skd_pci_resume,
3609 .shutdown = skd_pci_shutdown,
3610};
3611
3612/*
3613 *****************************************************************************
3614 * LOGGING SUPPORT
3615 *****************************************************************************
3616 */
3617
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003618const char *skd_drive_state_to_str(int state)
3619{
3620 switch (state) {
3621 case FIT_SR_DRIVE_OFFLINE:
3622 return "OFFLINE";
3623 case FIT_SR_DRIVE_INIT:
3624 return "INIT";
3625 case FIT_SR_DRIVE_ONLINE:
3626 return "ONLINE";
3627 case FIT_SR_DRIVE_BUSY:
3628 return "BUSY";
3629 case FIT_SR_DRIVE_FAULT:
3630 return "FAULT";
3631 case FIT_SR_DRIVE_DEGRADED:
3632 return "DEGRADED";
3633 case FIT_SR_PCIE_LINK_DOWN:
3634 return "INK_DOWN";
3635 case FIT_SR_DRIVE_SOFT_RESET:
3636 return "SOFT_RESET";
3637 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3638 return "NEED_FW";
3639 case FIT_SR_DRIVE_INIT_FAULT:
3640 return "INIT_FAULT";
3641 case FIT_SR_DRIVE_BUSY_SANITIZE:
3642 return "BUSY_SANITIZE";
3643 case FIT_SR_DRIVE_BUSY_ERASE:
3644 return "BUSY_ERASE";
3645 case FIT_SR_DRIVE_FW_BOOTING:
3646 return "FW_BOOTING";
3647 default:
3648 return "???";
3649 }
3650}
3651
3652const char *skd_skdev_state_to_str(enum skd_drvr_state state)
3653{
3654 switch (state) {
3655 case SKD_DRVR_STATE_LOAD:
3656 return "LOAD";
3657 case SKD_DRVR_STATE_IDLE:
3658 return "IDLE";
3659 case SKD_DRVR_STATE_BUSY:
3660 return "BUSY";
3661 case SKD_DRVR_STATE_STARTING:
3662 return "STARTING";
3663 case SKD_DRVR_STATE_ONLINE:
3664 return "ONLINE";
3665 case SKD_DRVR_STATE_PAUSING:
3666 return "PAUSING";
3667 case SKD_DRVR_STATE_PAUSED:
3668 return "PAUSED";
3669 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
3670 return "DRAINING_TIMEOUT";
3671 case SKD_DRVR_STATE_RESTARTING:
3672 return "RESTARTING";
3673 case SKD_DRVR_STATE_RESUMING:
3674 return "RESUMING";
3675 case SKD_DRVR_STATE_STOPPING:
3676 return "STOPPING";
3677 case SKD_DRVR_STATE_SYNCING:
3678 return "SYNCING";
3679 case SKD_DRVR_STATE_FAULT:
3680 return "FAULT";
3681 case SKD_DRVR_STATE_DISAPPEARED:
3682 return "DISAPPEARED";
3683 case SKD_DRVR_STATE_BUSY_ERASE:
3684 return "BUSY_ERASE";
3685 case SKD_DRVR_STATE_BUSY_SANITIZE:
3686 return "BUSY_SANITIZE";
3687 case SKD_DRVR_STATE_BUSY_IMMINENT:
3688 return "BUSY_IMMINENT";
3689 case SKD_DRVR_STATE_WAIT_BOOT:
3690 return "WAIT_BOOT";
3691
3692 default:
3693 return "???";
3694 }
3695}
3696
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05303697static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003698{
3699 switch (state) {
3700 case SKD_REQ_STATE_IDLE:
3701 return "IDLE";
3702 case SKD_REQ_STATE_SETUP:
3703 return "SETUP";
3704 case SKD_REQ_STATE_BUSY:
3705 return "BUSY";
3706 case SKD_REQ_STATE_COMPLETED:
3707 return "COMPLETED";
3708 case SKD_REQ_STATE_TIMEOUT:
3709 return "TIMEOUT";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003710 default:
3711 return "???";
3712 }
3713}
3714
3715static void skd_log_skdev(struct skd_device *skdev, const char *event)
3716{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003717 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
3718 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
3719 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3720 skd_skdev_state_to_str(skdev->state), skdev->state);
3721 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07003722 atomic_read(&skdev->in_flight), skdev->cur_max_queue_depth,
Bart Van Asschef98806d2017-08-17 13:12:58 -07003723 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3724 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07003725 atomic_read(&skdev->timeout_stamp), skdev->skcomp_cycle,
3726 skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003727}
3728
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003729static void skd_log_skreq(struct skd_device *skdev,
3730 struct skd_request_context *skreq, const char *event)
3731{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003732 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
3733 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
3734 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
3735 skreq->fitmsg_id);
3736 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07003737 skreq->timeout_stamp, skreq->data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003738
Jens Axboefcd37eb2013-11-01 10:14:56 -06003739 if (skreq->req != NULL) {
3740 struct request *req = skreq->req;
3741 u32 lba = (u32)blk_rq_pos(req);
3742 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003743
Bart Van Asschef98806d2017-08-17 13:12:58 -07003744 dev_dbg(&skdev->pdev->dev,
3745 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
3746 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06003747 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07003748 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003749}
3750
3751/*
3752 *****************************************************************************
3753 * MODULE GLUE
3754 *****************************************************************************
3755 */
3756
3757static int __init skd_init(void)
3758{
Bart Van Assche16a70532017-08-17 13:13:08 -07003759 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
3760 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
3761 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
3762 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
3763 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07003764 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
3765 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
3766 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07003767
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003768 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
3769
3770 switch (skd_isr_type) {
3771 case SKD_IRQ_LEGACY:
3772 case SKD_IRQ_MSI:
3773 case SKD_IRQ_MSIX:
3774 break;
3775 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003776 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003777 skd_isr_type, SKD_IRQ_DEFAULT);
3778 skd_isr_type = SKD_IRQ_DEFAULT;
3779 }
3780
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003781 if (skd_max_queue_depth < 1 ||
3782 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
3783 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003784 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
3785 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
3786 }
3787
Bart Van Assche2da7b402017-08-17 13:13:01 -07003788 if (skd_max_req_per_msg < 1 ||
3789 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003790 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003791 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
3792 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
3793 }
3794
3795 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003796 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003797 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
3798 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
3799 }
3800
3801 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003802 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003803 skd_dbg_level, 0);
3804 skd_dbg_level = 0;
3805 }
3806
3807 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003808 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003809 skd_isr_comp_limit, 0);
3810 skd_isr_comp_limit = 0;
3811 }
3812
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003813 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003814}
3815
3816static void __exit skd_exit(void)
3817{
3818 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
3819
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003820 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003821
3822 if (skd_major)
3823 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003824}
3825
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003826module_init(skd_init);
3827module_exit(skd_exit);