blob: d21fc76c5ed9184723e484d523aafe651c3472f2 [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>
Bart Van Asschea3db1022017-08-17 13:13:35 -070035#include <linux/slab_def.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060036#include <scsi/scsi.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060037#include <scsi/sg.h>
38#include <linux/io.h>
39#include <linux/uaccess.h>
Bartlomiej Zolnierkiewicz4ca90b52013-11-05 12:37:04 +010040#include <asm/unaligned.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060041
42#include "skd_s1120.h"
43
44static int skd_dbg_level;
45static int skd_isr_comp_limit = 4;
46
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060047#define SKD_ASSERT(expr) \
48 do { \
49 if (unlikely(!(expr))) { \
50 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
51 # expr, __FILE__, __func__, __LINE__); \
52 } \
53 } while (0)
54
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060055#define DRV_NAME "skd"
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060056#define PFX DRV_NAME ": "
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060057
Bart Van Asschebec9e8a2017-08-17 13:12:47 -070058MODULE_LICENSE("GPL");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060059
Bart Van Asschebb9f7dd2017-08-17 13:13:38 -070060MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060061
62#define PCI_VENDOR_ID_STEC 0x1B39
63#define PCI_DEVICE_ID_S1120 0x0001
64
65#define SKD_FUA_NV (1 << 1)
66#define SKD_MINORS_PER_DEVICE 16
67
68#define SKD_MAX_QUEUE_DEPTH 200u
69
70#define SKD_PAUSE_TIMEOUT (5 * 1000)
71
72#define SKD_N_FITMSG_BYTES (512u)
Bart Van Assche2da7b402017-08-17 13:13:01 -070073#define SKD_MAX_REQ_PER_MSG 14
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060074
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060075#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
76
77/* SG elements are 32 bytes, so we can make this 4096 and still be under the
78 * 128KB limit. That allows 4096*4K = 16M xfer size
79 */
80#define SKD_N_SG_PER_REQ_DEFAULT 256u
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060081
82#define SKD_N_COMPLETION_ENTRY 256u
83#define SKD_N_READ_CAP_BYTES (8u)
84
85#define SKD_N_INTERNAL_BYTES (512u)
86
Bart Van Assche6f7c7672017-08-17 13:13:02 -070087#define SKD_SKCOMP_SIZE \
88 ((sizeof(struct fit_completion_entry_v1) + \
89 sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY)
90
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060091/* 5 bits of uniqifier, 0xF800 */
92#define SKD_ID_INCR (0x400)
93#define SKD_ID_TABLE_MASK (3u << 8u)
94#define SKD_ID_RW_REQUEST (0u << 8u)
95#define SKD_ID_INTERNAL (1u << 8u)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060096#define SKD_ID_FIT_MSG (3u << 8u)
97#define SKD_ID_SLOT_MASK 0x00FFu
98#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
99
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600100#define SKD_N_MAX_SECTORS 2048u
101
102#define SKD_MAX_RETRIES 2u
103
104#define SKD_TIMER_SECONDS(seconds) (seconds)
105#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
106
107#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600108
109enum skd_drvr_state {
110 SKD_DRVR_STATE_LOAD,
111 SKD_DRVR_STATE_IDLE,
112 SKD_DRVR_STATE_BUSY,
113 SKD_DRVR_STATE_STARTING,
114 SKD_DRVR_STATE_ONLINE,
115 SKD_DRVR_STATE_PAUSING,
116 SKD_DRVR_STATE_PAUSED,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600117 SKD_DRVR_STATE_RESTARTING,
118 SKD_DRVR_STATE_RESUMING,
119 SKD_DRVR_STATE_STOPPING,
120 SKD_DRVR_STATE_FAULT,
121 SKD_DRVR_STATE_DISAPPEARED,
122 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
123 SKD_DRVR_STATE_BUSY_ERASE,
124 SKD_DRVR_STATE_BUSY_SANITIZE,
125 SKD_DRVR_STATE_BUSY_IMMINENT,
126 SKD_DRVR_STATE_WAIT_BOOT,
127 SKD_DRVR_STATE_SYNCING,
128};
129
130#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
131#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
132#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600133#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
134#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
135#define SKD_START_WAIT_SECONDS 90u
136
137enum skd_req_state {
138 SKD_REQ_STATE_IDLE,
139 SKD_REQ_STATE_SETUP,
140 SKD_REQ_STATE_BUSY,
141 SKD_REQ_STATE_COMPLETED,
142 SKD_REQ_STATE_TIMEOUT,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600143};
144
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600145enum skd_check_status_action {
146 SKD_CHECK_STATUS_REPORT_GOOD,
147 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
148 SKD_CHECK_STATUS_REQUEUE_REQUEST,
149 SKD_CHECK_STATUS_REPORT_ERROR,
150 SKD_CHECK_STATUS_BUSY_IMMINENT,
151};
152
Bart Van Assched891fe62017-08-17 13:13:07 -0700153struct skd_msg_buf {
154 struct fit_msg_hdr fmh;
155 struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG];
156};
157
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600158struct skd_fitmsg_context {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600159 u32 id;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600160
161 u32 length;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600162
Bart Van Assched891fe62017-08-17 13:13:07 -0700163 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600164 dma_addr_t mb_dma_address;
165};
166
167struct skd_request_context {
168 enum skd_req_state state;
169
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600170 u16 id;
171 u32 fitmsg_id;
172
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600173 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600174
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700175 enum dma_data_direction data_dir;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600176 struct scatterlist *sg;
177 u32 n_sg;
178 u32 sg_byte_count;
179
180 struct fit_sg_descriptor *sksg_list;
181 dma_addr_t sksg_dma_address;
182
183 struct fit_completion_entry_v1 completion;
184
185 struct fit_comp_error_info err_info;
186
187};
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600188
189struct skd_special_context {
190 struct skd_request_context req;
191
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600192 void *data_buf;
193 dma_addr_t db_dma_address;
194
Bart Van Assched891fe62017-08-17 13:13:07 -0700195 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600196 dma_addr_t mb_dma_address;
197};
198
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600199typedef enum skd_irq_type {
200 SKD_IRQ_LEGACY,
201 SKD_IRQ_MSI,
202 SKD_IRQ_MSIX
203} skd_irq_type_t;
204
205#define SKD_MAX_BARS 2
206
207struct skd_device {
Bart Van Assche85e34112017-08-17 13:13:17 -0700208 void __iomem *mem_map[SKD_MAX_BARS];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600209 resource_size_t mem_phys[SKD_MAX_BARS];
210 u32 mem_size[SKD_MAX_BARS];
211
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600212 struct skd_msix_entry *msix_entries;
213
214 struct pci_dev *pdev;
215 int pcie_error_reporting_is_enabled;
216
217 spinlock_t lock;
218 struct gendisk *disk;
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700219 struct blk_mq_tag_set tag_set;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600220 struct request_queue *queue;
Bart Van Assche91f85da2017-08-17 13:13:28 -0700221 struct skd_fitmsg_context *skmsg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600222 struct device *class_dev;
223 int gendisk_on;
224 int sync_done;
225
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600226 u32 devno;
227 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600228 char isr_name[30];
229
230 enum skd_drvr_state state;
231 u32 drive_state;
232
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600233 u32 cur_max_queue_depth;
234 u32 queue_low_water_mark;
235 u32 dev_max_queue_depth;
236
237 u32 num_fitmsg_context;
238 u32 num_req_context;
239
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600240 struct skd_fitmsg_context *skmsg_table;
241
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600242 struct skd_special_context internal_skspcl;
243 u32 read_cap_blocksize;
244 u32 read_cap_last_lba;
245 int read_cap_is_valid;
246 int inquiry_is_valid;
247 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600248
249 u8 skcomp_cycle;
250 u32 skcomp_ix;
Bart Van Asschea3db1022017-08-17 13:13:35 -0700251 struct kmem_cache *msgbuf_cache;
252 struct kmem_cache *sglist_cache;
253 struct kmem_cache *databuf_cache;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600254 struct fit_completion_entry_v1 *skcomp_table;
255 struct fit_comp_error_info *skerr_table;
256 dma_addr_t cq_dma_address;
257
258 wait_queue_head_t waitq;
259
260 struct timer_list timer;
261 u32 timer_countdown;
262 u32 timer_substate;
263
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600264 int sgs_per_request;
265 u32 last_mtd;
266
267 u32 proto_ver;
268
269 int dbg_level;
270 u32 connect_time_stamp;
271 int connect_retries;
272#define SKD_MAX_CONNECT_RETRIES 16
273 u32 drive_jiffies;
274
275 u32 timo_slot;
276
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700277 struct work_struct start_queue;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600278 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600279};
280
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600281#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
282#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
283#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
284
285static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
286{
Bart Van Assche14262a42017-08-17 13:12:57 -0700287 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600288
Bart Van Assche14262a42017-08-17 13:12:57 -0700289 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700290 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700291 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600292}
293
294static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
295 u32 offset)
296{
Bart Van Assche14262a42017-08-17 13:12:57 -0700297 writel(val, skdev->mem_map[1] + offset);
298 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700299 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600300}
301
302static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
303 u32 offset)
304{
Bart Van Assche14262a42017-08-17 13:12:57 -0700305 writeq(val, skdev->mem_map[1] + offset);
306 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700307 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
308 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600309}
310
311
312#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
313static int skd_isr_type = SKD_IRQ_DEFAULT;
314
315module_param(skd_isr_type, int, 0444);
316MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
317 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
318
319#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
320static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
321
322module_param(skd_max_req_per_msg, int, 0444);
323MODULE_PARM_DESC(skd_max_req_per_msg,
324 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700325 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600326
327#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
328#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
329static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
330
331module_param(skd_max_queue_depth, int, 0444);
332MODULE_PARM_DESC(skd_max_queue_depth,
333 "Maximum SCSI requests issued to s1120."
334 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
335
336static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
337module_param(skd_sgs_per_request, int, 0444);
338MODULE_PARM_DESC(skd_sgs_per_request,
339 "Maximum SG elements per block request."
340 " (1-4096, default==256)");
341
Bart Van Assche63214122017-08-17 13:13:23 -0700342static int skd_max_pass_thru = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600343module_param(skd_max_pass_thru, int, 0444);
344MODULE_PARM_DESC(skd_max_pass_thru,
Bart Van Assche63214122017-08-17 13:13:23 -0700345 "Maximum SCSI pass-thru at a time. IGNORED");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600346
347module_param(skd_dbg_level, int, 0444);
348MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
349
350module_param(skd_isr_comp_limit, int, 0444);
351MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
352
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600353/* Major device number dynamically assigned. */
354static u32 skd_major;
355
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600356static void skd_destruct(struct skd_device *skdev);
357static const struct block_device_operations skd_blockdev_ops;
358static void skd_send_fitmsg(struct skd_device *skdev,
359 struct skd_fitmsg_context *skmsg);
360static void skd_send_special_fitmsg(struct skd_device *skdev,
361 struct skd_special_context *skspcl);
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700362static void skd_end_request(struct skd_device *skdev, struct request *req,
363 blk_status_t status);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200364static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600365 struct skd_request_context *skreq);
366static void skd_postop_sg_list(struct skd_device *skdev,
367 struct skd_request_context *skreq);
368
369static void skd_restart_device(struct skd_device *skdev);
370static int skd_quiesce_dev(struct skd_device *skdev);
371static int skd_unquiesce_dev(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600372static void skd_disable_interrupts(struct skd_device *skdev);
373static void skd_isr_fwstate(struct skd_device *skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700374static void skd_recover_requests(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600375static void skd_soft_reset(struct skd_device *skdev);
376
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600377const char *skd_drive_state_to_str(int state);
378const char *skd_skdev_state_to_str(enum skd_drvr_state state);
379static void skd_log_skdev(struct skd_device *skdev, const char *event);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600380static void skd_log_skreq(struct skd_device *skdev,
381 struct skd_request_context *skreq, const char *event);
382
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600383/*
384 *****************************************************************************
385 * READ/WRITE REQUESTS
386 *****************************************************************************
387 */
Bart Van Assched4d0f5f2017-08-17 13:13:34 -0700388static void skd_inc_in_flight(struct request *rq, void *data, bool reserved)
389{
390 int *count = data;
391
392 count++;
393}
394
395static int skd_in_flight(struct skd_device *skdev)
396{
397 int count = 0;
398
399 blk_mq_tagset_busy_iter(&skdev->tag_set, skd_inc_in_flight, &count);
400
401 return count;
402}
403
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600404static void
405skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
406 int data_dir, unsigned lba,
407 unsigned count)
408{
409 if (data_dir == READ)
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700410 scsi_req->cdb[0] = READ_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600411 else
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700412 scsi_req->cdb[0] = WRITE_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600413
414 scsi_req->cdb[1] = 0;
415 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
416 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
417 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
418 scsi_req->cdb[5] = (lba & 0xff);
419 scsi_req->cdb[6] = 0;
420 scsi_req->cdb[7] = (count & 0xff00) >> 8;
421 scsi_req->cdb[8] = count & 0xff;
422 scsi_req->cdb[9] = 0;
423}
424
425static void
426skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400427 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600428{
429 skreq->flush_cmd = 1;
430
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700431 scsi_req->cdb[0] = SYNCHRONIZE_CACHE;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600432 scsi_req->cdb[1] = 0;
433 scsi_req->cdb[2] = 0;
434 scsi_req->cdb[3] = 0;
435 scsi_req->cdb[4] = 0;
436 scsi_req->cdb[5] = 0;
437 scsi_req->cdb[6] = 0;
438 scsi_req->cdb[7] = 0;
439 scsi_req->cdb[8] = 0;
440 scsi_req->cdb[9] = 0;
441}
442
Bart Van Assche3d17a672017-08-17 13:13:21 -0700443/*
444 * Return true if and only if all pending requests should be failed.
445 */
446static bool skd_fail_all(struct request_queue *q)
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700447{
448 struct skd_device *skdev = q->queuedata;
449
450 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
451
452 skd_log_skdev(skdev, "req_not_online");
453 switch (skdev->state) {
454 case SKD_DRVR_STATE_PAUSING:
455 case SKD_DRVR_STATE_PAUSED:
456 case SKD_DRVR_STATE_STARTING:
457 case SKD_DRVR_STATE_RESTARTING:
458 case SKD_DRVR_STATE_WAIT_BOOT:
459 /* In case of starting, we haven't started the queue,
460 * so we can't get here... but requests are
461 * possibly hanging out waiting for us because we
462 * reported the dev/skd0 already. They'll wait
463 * forever if connect doesn't complete.
464 * What to do??? delay dev/skd0 ??
465 */
466 case SKD_DRVR_STATE_BUSY:
467 case SKD_DRVR_STATE_BUSY_IMMINENT:
468 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700469 return false;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700470
471 case SKD_DRVR_STATE_BUSY_SANITIZE:
472 case SKD_DRVR_STATE_STOPPING:
473 case SKD_DRVR_STATE_SYNCING:
474 case SKD_DRVR_STATE_FAULT:
475 case SKD_DRVR_STATE_DISAPPEARED:
476 default:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700477 return true;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700478 }
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700479}
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600480
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700481static void skd_process_request(struct request *req, bool last)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600482{
Bart Van Assche91f85da2017-08-17 13:13:28 -0700483 struct request_queue *const q = req->q;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600484 struct skd_device *skdev = q->queuedata;
Bart Van Assche91f85da2017-08-17 13:13:28 -0700485 struct skd_fitmsg_context *skmsg;
486 struct fit_msg_hdr *fmh;
487 const u32 tag = blk_mq_unique_tag(req);
Bart Van Asschee7278a82017-08-17 13:13:31 -0700488 struct skd_request_context *const skreq = blk_mq_rq_to_pdu(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600489 struct skd_scsi_request *scsi_req;
Bart Van Assche74c74282017-08-17 13:13:37 -0700490 unsigned long flags = 0;
Bart Van Asschee2bb5542017-08-17 13:13:36 -0700491 const u32 lba = blk_rq_pos(req);
492 const u32 count = blk_rq_sectors(req);
493 const int data_dir = rq_data_dir(req);
Bart Van Assche91f85da2017-08-17 13:13:28 -0700494
495 WARN_ONCE(tag >= skd_max_queue_depth, "%#x > %#x (nr_requests = %lu)\n",
496 tag, skd_max_queue_depth, q->nr_requests);
497
498 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
499
Bart Van Assche91f85da2017-08-17 13:13:28 -0700500 dev_dbg(&skdev->pdev->dev,
501 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba,
502 lba, count, count, data_dir);
503
504 skreq->id = tag + SKD_ID_RW_REQUEST;
505 skreq->flush_cmd = 0;
506 skreq->n_sg = 0;
507 skreq->sg_byte_count = 0;
508
Bart Van Assche91f85da2017-08-17 13:13:28 -0700509 skreq->fitmsg_id = 0;
510
511 skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
512
513 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
514 dev_dbg(&skdev->pdev->dev, "error Out\n");
Bart Van Asschee7278a82017-08-17 13:13:31 -0700515 skd_end_request(skdev, blk_mq_rq_from_pdu(skreq),
516 BLK_STS_RESOURCE);
Bart Van Assche91f85da2017-08-17 13:13:28 -0700517 return;
518 }
519
Bart Van Asschea3db1022017-08-17 13:13:35 -0700520 dma_sync_single_for_device(&skdev->pdev->dev, skreq->sksg_dma_address,
521 skreq->n_sg *
522 sizeof(struct fit_sg_descriptor),
523 DMA_TO_DEVICE);
524
Bart Van Assche91f85da2017-08-17 13:13:28 -0700525 /* Either a FIT msg is in progress or we have to start one. */
Bart Van Assche74c74282017-08-17 13:13:37 -0700526 if (skd_max_req_per_msg == 1) {
527 skmsg = NULL;
528 } else {
529 spin_lock_irqsave(&skdev->lock, flags);
530 skmsg = skdev->skmsg;
531 }
Bart Van Assche91f85da2017-08-17 13:13:28 -0700532 if (!skmsg) {
533 skmsg = &skdev->skmsg_table[tag];
534 skdev->skmsg = skmsg;
535
536 /* Initialize the FIT msg header */
537 fmh = &skmsg->msg_buf->fmh;
538 memset(fmh, 0, sizeof(*fmh));
539 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
540 skmsg->length = sizeof(*fmh);
541 } else {
542 fmh = &skmsg->msg_buf->fmh;
543 }
544
545 skreq->fitmsg_id = skmsg->id;
546
547 scsi_req = &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
548 memset(scsi_req, 0, sizeof(*scsi_req));
549
Bart Van Assche91f85da2017-08-17 13:13:28 -0700550 scsi_req->hdr.tag = skreq->id;
Bart Van Asschee2bb5542017-08-17 13:13:36 -0700551 scsi_req->hdr.sg_list_dma_address =
552 cpu_to_be64(skreq->sksg_dma_address);
Bart Van Assche91f85da2017-08-17 13:13:28 -0700553
Bart Van Asschee2bb5542017-08-17 13:13:36 -0700554 if (req_op(req) == REQ_OP_FLUSH) {
Bart Van Assche91f85da2017-08-17 13:13:28 -0700555 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
556 SKD_ASSERT(skreq->flush_cmd == 1);
557 } else {
558 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
559 }
560
Bart Van Asschee2bb5542017-08-17 13:13:36 -0700561 if (req->cmd_flags & REQ_FUA)
Bart Van Assche91f85da2017-08-17 13:13:28 -0700562 scsi_req->cdb[1] |= SKD_FUA_NV;
563
564 scsi_req->hdr.sg_list_len_bytes = cpu_to_be32(skreq->sg_byte_count);
565
566 /* Complete resource allocations. */
567 skreq->state = SKD_REQ_STATE_BUSY;
568
569 skmsg->length += sizeof(struct skd_scsi_request);
570 fmh->num_protocol_cmds_coalesced++;
571
Bart Van Assche91f85da2017-08-17 13:13:28 -0700572 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
Bart Van Assched4d0f5f2017-08-17 13:13:34 -0700573 skd_in_flight(skdev));
Bart Van Assche91f85da2017-08-17 13:13:28 -0700574
575 /*
576 * If the FIT msg buffer is full send it.
577 */
Bart Van Assche74c74282017-08-17 13:13:37 -0700578 if (skd_max_req_per_msg == 1) {
Bart Van Assche91f85da2017-08-17 13:13:28 -0700579 skd_send_fitmsg(skdev, skmsg);
Bart Van Assche74c74282017-08-17 13:13:37 -0700580 } else {
581 if (last ||
582 fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
583 skd_send_fitmsg(skdev, skmsg);
584 skdev->skmsg = NULL;
585 }
586 spin_unlock_irqrestore(&skdev->lock, flags);
Bart Van Assche91f85da2017-08-17 13:13:28 -0700587 }
588}
589
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700590static blk_status_t skd_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
591 const struct blk_mq_queue_data *mqd)
Bart Van Assche91f85da2017-08-17 13:13:28 -0700592{
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700593 struct request *req = mqd->rq;
594 struct request_queue *q = req->q;
Bart Van Assche91f85da2017-08-17 13:13:28 -0700595 struct skd_device *skdev = q->queuedata;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600596
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700597 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
598 blk_mq_start_request(req);
599 skd_process_request(req, mqd->last);
600
601 return BLK_STS_OK;
602 } else {
603 return skd_fail_all(q) ? BLK_STS_IOERR : BLK_STS_RESOURCE;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600604 }
605
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700606 return BLK_STS_OK;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600607}
608
Bart Van Asschea74d5b72017-08-17 13:13:33 -0700609static enum blk_eh_timer_return skd_timed_out(struct request *req)
610{
611 struct skd_device *skdev = req->q->queuedata;
612
613 dev_err(&skdev->pdev->dev, "request with tag %#x timed out\n",
614 blk_mq_unique_tag(req));
615
616 return BLK_EH_HANDLED;
617}
618
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700619static void skd_end_request(struct skd_device *skdev, struct request *req,
620 blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600621{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600622 if (unlikely(error)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600623 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
624 u32 lba = (u32)blk_rq_pos(req);
625 u32 count = blk_rq_sectors(req);
626
Bart Van Asschef98806d2017-08-17 13:12:58 -0700627 dev_err(&skdev->pdev->dev,
628 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700629 count, req->tag);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600630 } else
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700631 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", req->tag,
Bart Van Asschef98806d2017-08-17 13:12:58 -0700632 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600633
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700634 blk_mq_end_request(req, error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600635}
636
Bart Van Asschea74d5b72017-08-17 13:13:33 -0700637/* Only called in case of a request timeout */
638static void skd_softirq_done(struct request *req)
639{
640 struct skd_device *skdev = req->q->queuedata;
641 struct skd_request_context *skreq = blk_mq_rq_to_pdu(req);
642 unsigned long flags;
643
644 spin_lock_irqsave(&skdev->lock, flags);
645 skd_end_request(skdev, blk_mq_rq_from_pdu(skreq), BLK_STS_TIMEOUT);
646 spin_unlock_irqrestore(&skdev->lock, flags);
647}
648
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200649static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400650 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600651{
Bart Van Asschee7278a82017-08-17 13:13:31 -0700652 struct request *req = blk_mq_rq_from_pdu(skreq);
Bart Van Assche06f824c42017-08-17 13:13:15 -0700653 struct scatterlist *sgl = &skreq->sg[0], *sg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600654 int n_sg;
655 int i;
656
657 skreq->sg_byte_count = 0;
658
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700659 WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE &&
660 skreq->data_dir != DMA_FROM_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600661
Bart Van Assche06f824c42017-08-17 13:13:15 -0700662 n_sg = blk_rq_map_sg(skdev->queue, req, sgl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600663 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200664 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600665
666 /*
667 * Map scatterlist to PCI bus addresses.
668 * Note PCI might change the number of entries.
669 */
Bart Van Assche06f824c42017-08-17 13:13:15 -0700670 n_sg = pci_map_sg(skdev->pdev, sgl, n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600671 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200672 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600673
674 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
675
676 skreq->n_sg = n_sg;
677
Bart Van Assche06f824c42017-08-17 13:13:15 -0700678 for_each_sg(sgl, sg, n_sg, i) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600679 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Assche06f824c42017-08-17 13:13:15 -0700680 u32 cnt = sg_dma_len(sg);
681 uint64_t dma_addr = sg_dma_address(sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600682
683 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
684 sgd->byte_count = cnt;
685 skreq->sg_byte_count += cnt;
686 sgd->host_side_addr = dma_addr;
687 sgd->dev_side_addr = 0;
688 }
689
690 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
691 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
692
693 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700694 dev_dbg(&skdev->pdev->dev,
695 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
696 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600697 for (i = 0; i < n_sg; i++) {
698 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700699
700 dev_dbg(&skdev->pdev->dev,
701 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
702 i, sgd->byte_count, sgd->control,
703 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600704 }
705 }
706
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200707 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600708}
709
Jens Axboefcd37eb2013-11-01 10:14:56 -0600710static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400711 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600712{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600713 /*
714 * restore the next ptr for next IO request so we
715 * don't have to set it every time.
716 */
717 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
718 skreq->sksg_dma_address +
719 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700720 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600721}
722
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600723/*
724 *****************************************************************************
725 * TIMER
726 *****************************************************************************
727 */
728
729static void skd_timer_tick_not_online(struct skd_device *skdev);
730
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700731static void skd_start_queue(struct work_struct *work)
732{
733 struct skd_device *skdev = container_of(work, typeof(*skdev),
734 start_queue);
735
736 /*
737 * Although it is safe to call blk_start_queue() from interrupt
738 * context, blk_mq_start_hw_queues() must not be called from
739 * interrupt context.
740 */
741 blk_mq_start_hw_queues(skdev->queue);
742}
743
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600744static void skd_timer_tick(ulong arg)
745{
746 struct skd_device *skdev = (struct skd_device *)arg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600747 unsigned long reqflags;
748 u32 state;
749
750 if (skdev->state == SKD_DRVR_STATE_FAULT)
751 /* The driver has declared fault, and we want it to
752 * stay that way until driver is reloaded.
753 */
754 return;
755
756 spin_lock_irqsave(&skdev->lock, reqflags);
757
758 state = SKD_READL(skdev, FIT_STATUS);
759 state &= FIT_SR_DRIVE_STATE_MASK;
760 if (state != skdev->drive_state)
761 skd_isr_fwstate(skdev);
762
Bart Van Asschea74d5b72017-08-17 13:13:33 -0700763 if (skdev->state != SKD_DRVR_STATE_ONLINE)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600764 skd_timer_tick_not_online(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600765
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600766 mod_timer(&skdev->timer, (jiffies + HZ));
767
768 spin_unlock_irqrestore(&skdev->lock, reqflags);
769}
770
771static void skd_timer_tick_not_online(struct skd_device *skdev)
772{
773 switch (skdev->state) {
774 case SKD_DRVR_STATE_IDLE:
775 case SKD_DRVR_STATE_LOAD:
776 break;
777 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700778 dev_dbg(&skdev->pdev->dev,
779 "drive busy sanitize[%x], driver[%x]\n",
780 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600781 /* If we've been in sanitize for 3 seconds, we figure we're not
782 * going to get anymore completions, so recover requests now
783 */
784 if (skdev->timer_countdown > 0) {
785 skdev->timer_countdown--;
786 return;
787 }
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700788 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600789 break;
790
791 case SKD_DRVR_STATE_BUSY:
792 case SKD_DRVR_STATE_BUSY_IMMINENT:
793 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700794 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
795 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600796 if (skdev->timer_countdown > 0) {
797 skdev->timer_countdown--;
798 return;
799 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700800 dev_dbg(&skdev->pdev->dev,
801 "busy[%x], timedout=%d, restarting device.",
802 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600803 skd_restart_device(skdev);
804 break;
805
806 case SKD_DRVR_STATE_WAIT_BOOT:
807 case SKD_DRVR_STATE_STARTING:
808 if (skdev->timer_countdown > 0) {
809 skdev->timer_countdown--;
810 return;
811 }
812 /* For now, we fault the drive. Could attempt resets to
813 * revcover at some point. */
814 skdev->state = SKD_DRVR_STATE_FAULT;
815
Bart Van Asschef98806d2017-08-17 13:12:58 -0700816 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
817 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600818
819 /*start the queue so we can respond with error to requests */
820 /* wakeup anyone waiting for startup complete */
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700821 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600822 skdev->gendisk_on = -1;
823 wake_up_interruptible(&skdev->waitq);
824 break;
825
826 case SKD_DRVR_STATE_ONLINE:
827 /* shouldn't get here. */
828 break;
829
830 case SKD_DRVR_STATE_PAUSING:
831 case SKD_DRVR_STATE_PAUSED:
832 break;
833
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600834 case SKD_DRVR_STATE_RESTARTING:
835 if (skdev->timer_countdown > 0) {
836 skdev->timer_countdown--;
837 return;
838 }
839 /* For now, we fault the drive. Could attempt resets to
840 * revcover at some point. */
841 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700842 dev_err(&skdev->pdev->dev,
843 "DriveFault Reconnect Timeout (%x)\n",
844 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600845
846 /*
847 * Recovering does two things:
848 * 1. completes IO with error
849 * 2. reclaims dma resources
850 * When is it safe to recover requests?
851 * - if the drive state is faulted
852 * - if the state is still soft reset after out timeout
853 * - if the drive registers are dead (state = FF)
854 * If it is "unsafe", we still need to recover, so we will
855 * disable pci bus mastering and disable our interrupts.
856 */
857
858 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
859 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
860 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
861 /* It never came out of soft reset. Try to
862 * recover the requests and then let them
863 * fail. This is to mitigate hung processes. */
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700864 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600865 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700866 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
867 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600868 pci_disable_device(skdev->pdev);
869 skd_disable_interrupts(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700870 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600871 }
872
873 /*start the queue so we can respond with error to requests */
874 /* wakeup anyone waiting for startup complete */
Bart Van Asscheca33dd92017-08-17 13:13:32 -0700875 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600876 skdev->gendisk_on = -1;
877 wake_up_interruptible(&skdev->waitq);
878 break;
879
880 case SKD_DRVR_STATE_RESUMING:
881 case SKD_DRVR_STATE_STOPPING:
882 case SKD_DRVR_STATE_SYNCING:
883 case SKD_DRVR_STATE_FAULT:
884 case SKD_DRVR_STATE_DISAPPEARED:
885 default:
886 break;
887 }
888}
889
890static int skd_start_timer(struct skd_device *skdev)
891{
892 int rc;
893
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600894 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
895
896 rc = mod_timer(&skdev->timer, (jiffies + HZ));
897 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -0700898 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600899 return rc;
900}
901
902static void skd_kill_timer(struct skd_device *skdev)
903{
904 del_timer_sync(&skdev->timer);
905}
906
907/*
908 *****************************************************************************
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600909 * INTERNAL REQUESTS -- generated by driver itself
910 *****************************************************************************
911 */
912
913static int skd_format_internal_skspcl(struct skd_device *skdev)
914{
915 struct skd_special_context *skspcl = &skdev->internal_skspcl;
916 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
917 struct fit_msg_hdr *fmh;
918 uint64_t dma_address;
919 struct skd_scsi_request *scsi;
920
Bart Van Assched891fe62017-08-17 13:13:07 -0700921 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600922 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
923 fmh->num_protocol_cmds_coalesced = 1;
924
Bart Van Assched891fe62017-08-17 13:13:07 -0700925 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600926 memset(scsi, 0, sizeof(*scsi));
927 dma_address = skspcl->req.sksg_dma_address;
928 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
Bart Van Assche32494df2017-08-17 13:13:25 -0700929 skspcl->req.n_sg = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600930 sgd->control = FIT_SGD_CONTROL_LAST;
931 sgd->byte_count = 0;
932 sgd->host_side_addr = skspcl->db_dma_address;
933 sgd->dev_side_addr = 0;
934 sgd->next_desc_ptr = 0LL;
935
936 return 1;
937}
938
939#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
940
941static void skd_send_internal_skspcl(struct skd_device *skdev,
942 struct skd_special_context *skspcl,
943 u8 opcode)
944{
945 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
946 struct skd_scsi_request *scsi;
947 unsigned char *buf = skspcl->data_buf;
948 int i;
949
950 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
951 /*
952 * A refresh is already in progress.
953 * Just wait for it to finish.
954 */
955 return;
956
957 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
958 skspcl->req.state = SKD_REQ_STATE_BUSY;
959 skspcl->req.id += SKD_ID_INCR;
960
Bart Van Assched891fe62017-08-17 13:13:07 -0700961 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600962 scsi->hdr.tag = skspcl->req.id;
963
964 memset(scsi->cdb, 0, sizeof(scsi->cdb));
965
966 switch (opcode) {
967 case TEST_UNIT_READY:
968 scsi->cdb[0] = TEST_UNIT_READY;
969 sgd->byte_count = 0;
970 scsi->hdr.sg_list_len_bytes = 0;
971 break;
972
973 case READ_CAPACITY:
974 scsi->cdb[0] = READ_CAPACITY;
975 sgd->byte_count = SKD_N_READ_CAP_BYTES;
976 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
977 break;
978
979 case INQUIRY:
980 scsi->cdb[0] = INQUIRY;
981 scsi->cdb[1] = 0x01; /* evpd */
982 scsi->cdb[2] = 0x80; /* serial number page */
983 scsi->cdb[4] = 0x10;
984 sgd->byte_count = 16;
985 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
986 break;
987
988 case SYNCHRONIZE_CACHE:
989 scsi->cdb[0] = SYNCHRONIZE_CACHE;
990 sgd->byte_count = 0;
991 scsi->hdr.sg_list_len_bytes = 0;
992 break;
993
994 case WRITE_BUFFER:
995 scsi->cdb[0] = WRITE_BUFFER;
996 scsi->cdb[1] = 0x02;
997 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
998 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
999 sgd->byte_count = WR_BUF_SIZE;
1000 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1001 /* fill incrementing byte pattern */
1002 for (i = 0; i < sgd->byte_count; i++)
1003 buf[i] = i & 0xFF;
1004 break;
1005
1006 case READ_BUFFER:
1007 scsi->cdb[0] = READ_BUFFER;
1008 scsi->cdb[1] = 0x02;
1009 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1010 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1011 sgd->byte_count = WR_BUF_SIZE;
1012 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1013 memset(skspcl->data_buf, 0, sgd->byte_count);
1014 break;
1015
1016 default:
1017 SKD_ASSERT("Don't know what to send");
1018 return;
1019
1020 }
1021 skd_send_special_fitmsg(skdev, skspcl);
1022}
1023
1024static void skd_refresh_device_data(struct skd_device *skdev)
1025{
1026 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1027
1028 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1029}
1030
1031static int skd_chk_read_buf(struct skd_device *skdev,
1032 struct skd_special_context *skspcl)
1033{
1034 unsigned char *buf = skspcl->data_buf;
1035 int i;
1036
1037 /* check for incrementing byte pattern */
1038 for (i = 0; i < WR_BUF_SIZE; i++)
1039 if (buf[i] != (i & 0xFF))
1040 return 1;
1041
1042 return 0;
1043}
1044
1045static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1046 u8 code, u8 qual, u8 fruc)
1047{
1048 /* If the check condition is of special interest, log a message */
1049 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1050 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001051 dev_err(&skdev->pdev->dev,
1052 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1053 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001054 }
1055}
1056
1057static void skd_complete_internal(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001058 struct fit_completion_entry_v1 *skcomp,
1059 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001060 struct skd_special_context *skspcl)
1061{
1062 u8 *buf = skspcl->data_buf;
1063 u8 status;
1064 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001065 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001066
Bart Van Assche760b48c2017-08-17 13:13:00 -07001067 lockdep_assert_held(&skdev->lock);
1068
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001069 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1070
Bart Van Asschef98806d2017-08-17 13:12:58 -07001071 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001072
Bart Van Asschea3db1022017-08-17 13:13:35 -07001073 dma_sync_single_for_cpu(&skdev->pdev->dev,
1074 skspcl->db_dma_address,
1075 skspcl->req.sksg_list[0].byte_count,
1076 DMA_BIDIRECTIONAL);
1077
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001078 skspcl->req.completion = *skcomp;
1079 skspcl->req.state = SKD_REQ_STATE_IDLE;
1080 skspcl->req.id += SKD_ID_INCR;
1081
1082 status = skspcl->req.completion.status;
1083
1084 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1085 skerr->qual, skerr->fruc);
1086
1087 switch (scsi->cdb[0]) {
1088 case TEST_UNIT_READY:
1089 if (status == SAM_STAT_GOOD)
1090 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1091 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1092 (skerr->key == MEDIUM_ERROR))
1093 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1094 else {
1095 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001096 dev_dbg(&skdev->pdev->dev,
1097 "TUR failed, don't send anymore state 0x%x\n",
1098 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001099 return;
1100 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001101 dev_dbg(&skdev->pdev->dev,
1102 "**** TUR failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001103 skd_send_internal_skspcl(skdev, skspcl,
1104 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001105 }
1106 break;
1107
1108 case WRITE_BUFFER:
1109 if (status == SAM_STAT_GOOD)
1110 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1111 else {
1112 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001113 dev_dbg(&skdev->pdev->dev,
1114 "write buffer failed, don't send anymore state 0x%x\n",
1115 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001116 return;
1117 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001118 dev_dbg(&skdev->pdev->dev,
1119 "**** write buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001120 skd_send_internal_skspcl(skdev, skspcl,
1121 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001122 }
1123 break;
1124
1125 case READ_BUFFER:
1126 if (status == SAM_STAT_GOOD) {
1127 if (skd_chk_read_buf(skdev, skspcl) == 0)
1128 skd_send_internal_skspcl(skdev, skspcl,
1129 READ_CAPACITY);
1130 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001131 dev_err(&skdev->pdev->dev,
1132 "*** W/R Buffer mismatch %d ***\n",
1133 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001134 if (skdev->connect_retries <
1135 SKD_MAX_CONNECT_RETRIES) {
1136 skdev->connect_retries++;
1137 skd_soft_reset(skdev);
1138 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001139 dev_err(&skdev->pdev->dev,
1140 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001141 return;
1142 }
1143 }
1144
1145 } else {
1146 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001147 dev_dbg(&skdev->pdev->dev,
1148 "read buffer failed, don't send anymore state 0x%x\n",
1149 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001150 return;
1151 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001152 dev_dbg(&skdev->pdev->dev,
1153 "**** read buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001154 skd_send_internal_skspcl(skdev, skspcl,
1155 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001156 }
1157 break;
1158
1159 case READ_CAPACITY:
1160 skdev->read_cap_is_valid = 0;
1161 if (status == SAM_STAT_GOOD) {
1162 skdev->read_cap_last_lba =
1163 (buf[0] << 24) | (buf[1] << 16) |
1164 (buf[2] << 8) | buf[3];
1165 skdev->read_cap_blocksize =
1166 (buf[4] << 24) | (buf[5] << 16) |
1167 (buf[6] << 8) | buf[7];
1168
Bart Van Asschef98806d2017-08-17 13:12:58 -07001169 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1170 skdev->read_cap_last_lba,
1171 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001172
1173 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1174
1175 skdev->read_cap_is_valid = 1;
1176
1177 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1178 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1179 (skerr->key == MEDIUM_ERROR)) {
1180 skdev->read_cap_last_lba = ~0;
1181 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001182 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 -06001183 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1184 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001185 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001186 skd_send_internal_skspcl(skdev, skspcl,
1187 TEST_UNIT_READY);
1188 }
1189 break;
1190
1191 case INQUIRY:
1192 skdev->inquiry_is_valid = 0;
1193 if (status == SAM_STAT_GOOD) {
1194 skdev->inquiry_is_valid = 1;
1195
1196 for (i = 0; i < 12; i++)
1197 skdev->inq_serial_num[i] = buf[i + 4];
1198 skdev->inq_serial_num[12] = 0;
1199 }
1200
1201 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001202 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001203 /* connection is complete */
1204 skdev->connect_retries = 0;
1205 break;
1206
1207 case SYNCHRONIZE_CACHE:
1208 if (status == SAM_STAT_GOOD)
1209 skdev->sync_done = 1;
1210 else
1211 skdev->sync_done = -1;
1212 wake_up_interruptible(&skdev->waitq);
1213 break;
1214
1215 default:
1216 SKD_ASSERT("we didn't send this");
1217 }
1218}
1219
1220/*
1221 *****************************************************************************
1222 * FIT MESSAGES
1223 *****************************************************************************
1224 */
1225
1226static void skd_send_fitmsg(struct skd_device *skdev,
1227 struct skd_fitmsg_context *skmsg)
1228{
1229 u64 qcmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001230
Bart Van Asschef98806d2017-08-17 13:12:58 -07001231 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
Bart Van Assched4d0f5f2017-08-17 13:13:34 -07001232 skmsg->mb_dma_address, skd_in_flight(skdev));
Bart Van Assche6507f432017-08-17 13:13:06 -07001233 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001234
1235 qcmd = skmsg->mb_dma_address;
1236 qcmd |= FIT_QCMD_QID_NORMAL;
1237
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001238 if (unlikely(skdev->dbg_level > 1)) {
1239 u8 *bp = (u8 *)skmsg->msg_buf;
1240 int i;
1241 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001242 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
1243 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001244 if (i == 0)
1245 i = 64 - 8;
1246 }
1247 }
1248
1249 if (skmsg->length > 256)
1250 qcmd |= FIT_QCMD_MSGSIZE_512;
1251 else if (skmsg->length > 128)
1252 qcmd |= FIT_QCMD_MSGSIZE_256;
1253 else if (skmsg->length > 64)
1254 qcmd |= FIT_QCMD_MSGSIZE_128;
1255 else
1256 /*
1257 * This makes no sense because the FIT msg header is
1258 * 64 bytes. If the msg is only 64 bytes long it has
1259 * no payload.
1260 */
1261 qcmd |= FIT_QCMD_MSGSIZE_64;
1262
Bart Van Asschea3db1022017-08-17 13:13:35 -07001263 dma_sync_single_for_device(&skdev->pdev->dev, skmsg->mb_dma_address,
1264 skmsg->length, DMA_TO_DEVICE);
1265
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001266 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1267 smp_wmb();
1268
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001269 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001270}
1271
1272static void skd_send_special_fitmsg(struct skd_device *skdev,
1273 struct skd_special_context *skspcl)
1274{
1275 u64 qcmd;
1276
Bart Van Asschea3db1022017-08-17 13:13:35 -07001277 WARN_ON_ONCE(skspcl->req.n_sg != 1);
1278
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001279 if (unlikely(skdev->dbg_level > 1)) {
1280 u8 *bp = (u8 *)skspcl->msg_buf;
1281 int i;
1282
1283 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001284 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
1285 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001286 if (i == 0)
1287 i = 64 - 8;
1288 }
1289
Bart Van Asschef98806d2017-08-17 13:12:58 -07001290 dev_dbg(&skdev->pdev->dev,
1291 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
1292 skspcl, skspcl->req.id, skspcl->req.sksg_list,
1293 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001294 for (i = 0; i < skspcl->req.n_sg; i++) {
1295 struct fit_sg_descriptor *sgd =
1296 &skspcl->req.sksg_list[i];
1297
Bart Van Asschef98806d2017-08-17 13:12:58 -07001298 dev_dbg(&skdev->pdev->dev,
1299 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1300 i, sgd->byte_count, sgd->control,
1301 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001302 }
1303 }
1304
1305 /*
1306 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
1307 * and one 64-byte SSDI command.
1308 */
1309 qcmd = skspcl->mb_dma_address;
1310 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
1311
Bart Van Asschea3db1022017-08-17 13:13:35 -07001312 dma_sync_single_for_device(&skdev->pdev->dev, skspcl->mb_dma_address,
1313 SKD_N_SPECIAL_FITMSG_BYTES, DMA_TO_DEVICE);
1314 dma_sync_single_for_device(&skdev->pdev->dev,
1315 skspcl->req.sksg_dma_address,
1316 1 * sizeof(struct fit_sg_descriptor),
1317 DMA_TO_DEVICE);
1318 dma_sync_single_for_device(&skdev->pdev->dev,
1319 skspcl->db_dma_address,
1320 skspcl->req.sksg_list[0].byte_count,
1321 DMA_BIDIRECTIONAL);
1322
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001323 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1324 smp_wmb();
1325
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001326 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
1327}
1328
1329/*
1330 *****************************************************************************
1331 * COMPLETION QUEUE
1332 *****************************************************************************
1333 */
1334
1335static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001336 struct fit_completion_entry_v1 *skcomp,
1337 struct fit_comp_error_info *skerr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001338
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001339struct sns_info {
1340 u8 type;
1341 u8 stat;
1342 u8 key;
1343 u8 asc;
1344 u8 ascq;
1345 u8 mask;
1346 enum skd_check_status_action action;
1347};
1348
1349static struct sns_info skd_chkstat_table[] = {
1350 /* Good */
1351 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
1352 SKD_CHECK_STATUS_REPORT_GOOD },
1353
1354 /* Smart alerts */
1355 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
1356 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1357 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
1358 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1359 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
1360 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1361
1362 /* Retry (with limits) */
1363 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
1364 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1365 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
1366 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1367 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
1368 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1369 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
1370 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1371
1372 /* Busy (or about to be) */
1373 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
1374 SKD_CHECK_STATUS_BUSY_IMMINENT },
1375};
1376
1377/*
1378 * Look up status and sense data to decide how to handle the error
1379 * from the device.
1380 * mask says which fields must match e.g., mask=0x18 means check
1381 * type and stat, ignore key, asc, ascq.
1382 */
1383
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001384static enum skd_check_status_action
1385skd_check_status(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001386 u8 cmp_status, struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001387{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001388 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001389
Bart Van Asschef98806d2017-08-17 13:12:58 -07001390 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1391 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001392
Bart Van Asschef98806d2017-08-17 13:12:58 -07001393 dev_dbg(&skdev->pdev->dev,
1394 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
1395 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
1396 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001397
1398 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001399 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001400 struct sns_info *sns = &skd_chkstat_table[i];
1401
1402 if (sns->mask & 0x10)
1403 if (skerr->type != sns->type)
1404 continue;
1405
1406 if (sns->mask & 0x08)
1407 if (cmp_status != sns->stat)
1408 continue;
1409
1410 if (sns->mask & 0x04)
1411 if (skerr->key != sns->key)
1412 continue;
1413
1414 if (sns->mask & 0x02)
1415 if (skerr->code != sns->asc)
1416 continue;
1417
1418 if (sns->mask & 0x01)
1419 if (skerr->qual != sns->ascq)
1420 continue;
1421
1422 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001423 dev_err(&skdev->pdev->dev,
1424 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
1425 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001426 }
1427 return sns->action;
1428 }
1429
1430 /* No other match, so nonzero status means error,
1431 * zero status means good
1432 */
1433 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001434 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001435 return SKD_CHECK_STATUS_REPORT_ERROR;
1436 }
1437
Bart Van Asschef98806d2017-08-17 13:12:58 -07001438 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001439 return SKD_CHECK_STATUS_REPORT_GOOD;
1440}
1441
1442static void skd_resolve_req_exception(struct skd_device *skdev,
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001443 struct skd_request_context *skreq,
1444 struct request *req)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001445{
1446 u8 cmp_status = skreq->completion.status;
1447
1448 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
1449 case SKD_CHECK_STATUS_REPORT_GOOD:
1450 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001451 skd_end_request(skdev, req, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001452 break;
1453
1454 case SKD_CHECK_STATUS_BUSY_IMMINENT:
1455 skd_log_skreq(skdev, skreq, "retry(busy)");
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001456 blk_requeue_request(skdev->queue, req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001457 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001458 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
1459 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
1460 skd_quiesce_dev(skdev);
1461 break;
1462
1463 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001464 if ((unsigned long) ++req->special < SKD_MAX_RETRIES) {
Jens Axboefcd37eb2013-11-01 10:14:56 -06001465 skd_log_skreq(skdev, skreq, "retry");
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001466 blk_requeue_request(skdev->queue, req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06001467 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001468 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07001469 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001470
1471 case SKD_CHECK_STATUS_REPORT_ERROR:
1472 default:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001473 skd_end_request(skdev, req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001474 break;
1475 }
1476}
1477
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001478static void skd_release_skreq(struct skd_device *skdev,
1479 struct skd_request_context *skreq)
1480{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001481 /*
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001482 * Reclaim the skd_request_context
1483 */
1484 skreq->state = SKD_REQ_STATE_IDLE;
1485 skreq->id += SKD_ID_INCR;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001486}
1487
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001488static int skd_isr_completion_posted(struct skd_device *skdev,
1489 int limit, int *enqueued)
1490{
Bart Van Assche85e34112017-08-17 13:13:17 -07001491 struct fit_completion_entry_v1 *skcmp;
1492 struct fit_comp_error_info *skerr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001493 u16 req_id;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001494 u32 tag;
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001495 u16 hwq = 0;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001496 struct request *rq;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001497 struct skd_request_context *skreq;
Bart Van Asschec830da82017-08-17 13:13:13 -07001498 u16 cmp_cntxt;
1499 u8 cmp_status;
1500 u8 cmp_cycle;
1501 u32 cmp_bytes;
Dan Carpenterc0b3dda2017-08-23 13:44:20 +03001502 int rc = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001503 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001504
Bart Van Assche760b48c2017-08-17 13:13:00 -07001505 lockdep_assert_held(&skdev->lock);
1506
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001507 for (;; ) {
1508 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
1509
1510 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
1511 cmp_cycle = skcmp->cycle;
1512 cmp_cntxt = skcmp->tag;
1513 cmp_status = skcmp->status;
1514 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
1515
1516 skerr = &skdev->skerr_table[skdev->skcomp_ix];
1517
Bart Van Asschef98806d2017-08-17 13:12:58 -07001518 dev_dbg(&skdev->pdev->dev,
1519 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
1520 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
Bart Van Assched4d0f5f2017-08-17 13:13:34 -07001521 cmp_cntxt, cmp_status, skd_in_flight(skdev),
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001522 cmp_bytes, skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001523
1524 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001525 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001526 break;
1527 }
1528 /*
1529 * Update the completion queue head index and possibly
1530 * the completion cycle count. 8-bit wrap-around.
1531 */
1532 skdev->skcomp_ix++;
1533 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
1534 skdev->skcomp_ix = 0;
1535 skdev->skcomp_cycle++;
1536 }
1537
1538 /*
1539 * The command context is a unique 32-bit ID. The low order
1540 * bits help locate the request. The request is usually a
1541 * r/w request (see skd_start() above) or a special request.
1542 */
1543 req_id = cmp_cntxt;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001544 tag = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001545
1546 /* Is this other than a r/w request? */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001547 if (tag >= skdev->num_req_context) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001548 /*
1549 * This is not a completion for a r/w request.
1550 */
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001551 WARN_ON_ONCE(blk_mq_tag_to_rq(skdev->tag_set.tags[hwq],
1552 tag));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001553 skd_complete_other(skdev, skcmp, skerr);
1554 continue;
1555 }
1556
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001557 rq = blk_mq_tag_to_rq(skdev->tag_set.tags[hwq], tag);
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001558 if (WARN(!rq, "No request for tag %#x -> %#x\n", cmp_cntxt,
1559 tag))
1560 continue;
Bart Van Asschee7278a82017-08-17 13:13:31 -07001561 skreq = blk_mq_rq_to_pdu(rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001562
1563 /*
1564 * Make sure the request ID for the slot matches.
1565 */
1566 if (skreq->id != req_id) {
Bart Van Assche49f16e22017-08-23 10:56:30 -07001567 dev_err(&skdev->pdev->dev,
1568 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
1569 req_id, skreq->id, cmp_cntxt);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001570
Bart Van Assche49f16e22017-08-23 10:56:30 -07001571 continue;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001572 }
1573
1574 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
1575
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001576 skreq->completion = *skcmp;
1577 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
1578 skreq->err_info = *skerr;
1579 skd_log_check_status(skdev, cmp_status, skerr->key,
1580 skerr->code, skerr->qual,
1581 skerr->fruc);
1582 }
1583 /* Release DMA resources for the request. */
1584 if (skreq->n_sg > 0)
1585 skd_postop_sg_list(skdev, skreq);
1586
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001587 skd_release_skreq(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001588
1589 /*
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001590 * Capture the outcome and post it back to the native request.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001591 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001592 if (likely(cmp_status == SAM_STAT_GOOD))
1593 skd_end_request(skdev, rq, BLK_STS_OK);
1594 else
1595 skd_resolve_req_exception(skdev, skreq, rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001596
1597 /* skd_isr_comp_limit equal zero means no limit */
1598 if (limit) {
1599 if (++processed >= limit) {
1600 rc = 1;
1601 break;
1602 }
1603 }
1604 }
1605
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001606 if (skdev->state == SKD_DRVR_STATE_PAUSING &&
Bart Van Assched4d0f5f2017-08-17 13:13:34 -07001607 skd_in_flight(skdev) == 0) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001608 skdev->state = SKD_DRVR_STATE_PAUSED;
1609 wake_up_interruptible(&skdev->waitq);
1610 }
1611
1612 return rc;
1613}
1614
1615static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001616 struct fit_completion_entry_v1 *skcomp,
1617 struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001618{
1619 u32 req_id = 0;
1620 u32 req_table;
1621 u32 req_slot;
1622 struct skd_special_context *skspcl;
1623
Bart Van Assche760b48c2017-08-17 13:13:00 -07001624 lockdep_assert_held(&skdev->lock);
1625
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001626 req_id = skcomp->tag;
1627 req_table = req_id & SKD_ID_TABLE_MASK;
1628 req_slot = req_id & SKD_ID_SLOT_MASK;
1629
Bart Van Asschef98806d2017-08-17 13:12:58 -07001630 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
1631 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001632
1633 /*
1634 * Based on the request id, determine how to dispatch this completion.
1635 * This swich/case is finding the good cases and forwarding the
1636 * completion entry. Errors are reported below the switch.
1637 */
1638 switch (req_table) {
1639 case SKD_ID_RW_REQUEST:
1640 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07001641 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001642 * handles r/w requests. The only way we get here
1643 * is if the req_slot is out of bounds.
1644 */
1645 break;
1646
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001647 case SKD_ID_INTERNAL:
1648 if (req_slot == 0) {
1649 skspcl = &skdev->internal_skspcl;
1650 if (skspcl->req.id == req_id &&
1651 skspcl->req.state == SKD_REQ_STATE_BUSY) {
1652 skd_complete_internal(skdev,
1653 skcomp, skerr, skspcl);
1654 return;
1655 }
1656 }
1657 break;
1658
1659 case SKD_ID_FIT_MSG:
1660 /*
1661 * These id's should never appear in a completion record.
1662 */
1663 break;
1664
1665 default:
1666 /*
1667 * These id's should never appear anywhere;
1668 */
1669 break;
1670 }
1671
1672 /*
1673 * If we get here it is a bad or stale id.
1674 */
1675}
1676
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001677static void skd_reset_skcomp(struct skd_device *skdev)
1678{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07001679 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001680
1681 skdev->skcomp_ix = 0;
1682 skdev->skcomp_cycle = 1;
1683}
1684
1685/*
1686 *****************************************************************************
1687 * INTERRUPTS
1688 *****************************************************************************
1689 */
1690static void skd_completion_worker(struct work_struct *work)
1691{
1692 struct skd_device *skdev =
1693 container_of(work, struct skd_device, completion_worker);
1694 unsigned long flags;
1695 int flush_enqueued = 0;
1696
1697 spin_lock_irqsave(&skdev->lock, flags);
1698
1699 /*
1700 * pass in limit=0, which means no limit..
1701 * process everything in compq
1702 */
1703 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001704 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001705
1706 spin_unlock_irqrestore(&skdev->lock, flags);
1707}
1708
1709static void skd_isr_msg_from_dev(struct skd_device *skdev);
1710
Arnd Bergmann41c94992016-11-09 13:55:35 +01001711static irqreturn_t
1712skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001713{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07001714 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001715 u32 intstat;
1716 u32 ack;
1717 int rc = 0;
1718 int deferred = 0;
1719 int flush_enqueued = 0;
1720
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001721 spin_lock(&skdev->lock);
1722
1723 for (;; ) {
1724 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
1725
1726 ack = FIT_INT_DEF_MASK;
1727 ack &= intstat;
1728
Bart Van Asschef98806d2017-08-17 13:12:58 -07001729 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
1730 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001731
1732 /* As long as there is an int pending on device, keep
1733 * running loop. When none, get out, but if we've never
1734 * done any processing, call completion handler?
1735 */
1736 if (ack == 0) {
1737 /* No interrupts on device, but run the completion
1738 * processor anyway?
1739 */
1740 if (rc == 0)
1741 if (likely (skdev->state
1742 == SKD_DRVR_STATE_ONLINE))
1743 deferred = 1;
1744 break;
1745 }
1746
1747 rc = IRQ_HANDLED;
1748
1749 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
1750
1751 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
1752 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
1753 if (intstat & FIT_ISH_COMPLETION_POSTED) {
1754 /*
1755 * If we have already deferred completion
1756 * processing, don't bother running it again
1757 */
1758 if (deferred == 0)
1759 deferred =
1760 skd_isr_completion_posted(skdev,
1761 skd_isr_comp_limit, &flush_enqueued);
1762 }
1763
1764 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
1765 skd_isr_fwstate(skdev);
1766 if (skdev->state == SKD_DRVR_STATE_FAULT ||
1767 skdev->state ==
1768 SKD_DRVR_STATE_DISAPPEARED) {
1769 spin_unlock(&skdev->lock);
1770 return rc;
1771 }
1772 }
1773
1774 if (intstat & FIT_ISH_MSG_FROM_DEV)
1775 skd_isr_msg_from_dev(skdev);
1776 }
1777 }
1778
1779 if (unlikely(flush_enqueued))
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001780 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001781
1782 if (deferred)
1783 schedule_work(&skdev->completion_worker);
1784 else if (!flush_enqueued)
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001785 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001786
1787 spin_unlock(&skdev->lock);
1788
1789 return rc;
1790}
1791
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001792static void skd_drive_fault(struct skd_device *skdev)
1793{
1794 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001795 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001796}
1797
1798static void skd_drive_disappeared(struct skd_device *skdev)
1799{
1800 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001801 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001802}
1803
1804static void skd_isr_fwstate(struct skd_device *skdev)
1805{
1806 u32 sense;
1807 u32 state;
1808 u32 mtd;
1809 int prev_driver_state = skdev->state;
1810
1811 sense = SKD_READL(skdev, FIT_STATUS);
1812 state = sense & FIT_SR_DRIVE_STATE_MASK;
1813
Bart Van Asschef98806d2017-08-17 13:12:58 -07001814 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
1815 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
1816 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001817
1818 skdev->drive_state = state;
1819
1820 switch (skdev->drive_state) {
1821 case FIT_SR_DRIVE_INIT:
1822 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
1823 skd_disable_interrupts(skdev);
1824 break;
1825 }
1826 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001827 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001828 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
1829 skdev->timer_countdown = SKD_STARTING_TIMO;
1830 skdev->state = SKD_DRVR_STATE_STARTING;
1831 skd_soft_reset(skdev);
1832 break;
1833 }
1834 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
1835 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1836 skdev->last_mtd = mtd;
1837 break;
1838
1839 case FIT_SR_DRIVE_ONLINE:
1840 skdev->cur_max_queue_depth = skd_max_queue_depth;
1841 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
1842 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
1843
1844 skdev->queue_low_water_mark =
1845 skdev->cur_max_queue_depth * 2 / 3 + 1;
1846 if (skdev->queue_low_water_mark < 1)
1847 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001848 dev_info(&skdev->pdev->dev,
1849 "Queue depth limit=%d dev=%d lowat=%d\n",
1850 skdev->cur_max_queue_depth,
1851 skdev->dev_max_queue_depth,
1852 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001853
1854 skd_refresh_device_data(skdev);
1855 break;
1856
1857 case FIT_SR_DRIVE_BUSY:
1858 skdev->state = SKD_DRVR_STATE_BUSY;
1859 skdev->timer_countdown = SKD_BUSY_TIMO;
1860 skd_quiesce_dev(skdev);
1861 break;
1862 case FIT_SR_DRIVE_BUSY_SANITIZE:
1863 /* set timer for 3 seconds, we'll abort any unfinished
1864 * commands after that expires
1865 */
1866 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
1867 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001868 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001869 break;
1870 case FIT_SR_DRIVE_BUSY_ERASE:
1871 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
1872 skdev->timer_countdown = SKD_BUSY_TIMO;
1873 break;
1874 case FIT_SR_DRIVE_OFFLINE:
1875 skdev->state = SKD_DRVR_STATE_IDLE;
1876 break;
1877 case FIT_SR_DRIVE_SOFT_RESET:
1878 switch (skdev->state) {
1879 case SKD_DRVR_STATE_STARTING:
1880 case SKD_DRVR_STATE_RESTARTING:
1881 /* Expected by a caller of skd_soft_reset() */
1882 break;
1883 default:
1884 skdev->state = SKD_DRVR_STATE_RESTARTING;
1885 break;
1886 }
1887 break;
1888 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001889 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001890 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
1891 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
1892 break;
1893
1894 case FIT_SR_DRIVE_DEGRADED:
1895 case FIT_SR_PCIE_LINK_DOWN:
1896 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
1897 break;
1898
1899 case FIT_SR_DRIVE_FAULT:
1900 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001901 skd_recover_requests(skdev);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001902 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001903 break;
1904
1905 /* PCIe bus returned all Fs? */
1906 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001907 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
1908 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001909 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001910 skd_recover_requests(skdev);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001911 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001912 break;
1913 default:
1914 /*
1915 * Uknown FW State. Wait for a state we recognize.
1916 */
1917 break;
1918 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001919 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
1920 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
1921 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001922}
1923
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001924static void skd_recover_request(struct request *req, void *data, bool reserved)
Bart Van Assche4e54b842017-08-17 13:13:29 -07001925{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001926 struct skd_device *const skdev = data;
1927 struct skd_request_context *skreq = blk_mq_rq_to_pdu(req);
Bart Van Assche4e54b842017-08-17 13:13:29 -07001928
1929 if (skreq->state != SKD_REQ_STATE_BUSY)
1930 return;
1931
1932 skd_log_skreq(skdev, skreq, "recover");
1933
Bart Van Assche4e54b842017-08-17 13:13:29 -07001934 /* Release DMA resources for the request. */
1935 if (skreq->n_sg > 0)
1936 skd_postop_sg_list(skdev, skreq);
1937
Bart Van Assche4e54b842017-08-17 13:13:29 -07001938 skreq->state = SKD_REQ_STATE_IDLE;
1939
1940 skd_end_request(skdev, req, BLK_STS_IOERR);
1941}
1942
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001943static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001944{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001945 blk_mq_tagset_busy_iter(&skdev->tag_set, skd_recover_request, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001946}
1947
1948static void skd_isr_msg_from_dev(struct skd_device *skdev)
1949{
1950 u32 mfd;
1951 u32 mtd;
1952 u32 data;
1953
1954 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
1955
Bart Van Asschef98806d2017-08-17 13:12:58 -07001956 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
1957 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001958
1959 /* ignore any mtd that is an ack for something we didn't send */
1960 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
1961 return;
1962
1963 switch (FIT_MXD_TYPE(mfd)) {
1964 case FIT_MTD_FITFW_INIT:
1965 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
1966
1967 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001968 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
1969 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
1970 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
1971 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001972 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
1973 skd_soft_reset(skdev);
1974 break;
1975 }
1976 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
1977 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1978 skdev->last_mtd = mtd;
1979 break;
1980
1981 case FIT_MTD_GET_CMDQ_DEPTH:
1982 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
1983 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
1984 SKD_N_COMPLETION_ENTRY);
1985 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1986 skdev->last_mtd = mtd;
1987 break;
1988
1989 case FIT_MTD_SET_COMPQ_DEPTH:
1990 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
1991 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
1992 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1993 skdev->last_mtd = mtd;
1994 break;
1995
1996 case FIT_MTD_SET_COMPQ_ADDR:
1997 skd_reset_skcomp(skdev);
1998 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
1999 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2000 skdev->last_mtd = mtd;
2001 break;
2002
2003 case FIT_MTD_CMD_LOG_HOST_ID:
2004 skdev->connect_time_stamp = get_seconds();
2005 data = skdev->connect_time_stamp & 0xFFFF;
2006 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
2007 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2008 skdev->last_mtd = mtd;
2009 break;
2010
2011 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
2012 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
2013 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
2014 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
2015 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2016 skdev->last_mtd = mtd;
2017 break;
2018
2019 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
2020 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
2021 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
2022 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2023 skdev->last_mtd = mtd;
2024
Bart Van Asschef98806d2017-08-17 13:12:58 -07002025 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
2026 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002027 break;
2028
2029 case FIT_MTD_ARM_QUEUE:
2030 skdev->last_mtd = 0;
2031 /*
2032 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
2033 */
2034 break;
2035
2036 default:
2037 break;
2038 }
2039}
2040
2041static void skd_disable_interrupts(struct skd_device *skdev)
2042{
2043 u32 sense;
2044
2045 sense = SKD_READL(skdev, FIT_CONTROL);
2046 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
2047 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002048 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002049
2050 /* Note that the 1s is written. A 1-bit means
2051 * disable, a 0 means enable.
2052 */
2053 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
2054}
2055
2056static void skd_enable_interrupts(struct skd_device *skdev)
2057{
2058 u32 val;
2059
2060 /* unmask interrupts first */
2061 val = FIT_ISH_FW_STATE_CHANGE +
2062 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
2063
2064 /* Note that the compliment of mask is written. A 1-bit means
2065 * disable, a 0 means enable. */
2066 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002067 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002068
2069 val = SKD_READL(skdev, FIT_CONTROL);
2070 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002071 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002072 SKD_WRITEL(skdev, val, FIT_CONTROL);
2073}
2074
2075/*
2076 *****************************************************************************
2077 * START, STOP, RESTART, QUIESCE, UNQUIESCE
2078 *****************************************************************************
2079 */
2080
2081static void skd_soft_reset(struct skd_device *skdev)
2082{
2083 u32 val;
2084
2085 val = SKD_READL(skdev, FIT_CONTROL);
2086 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002087 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002088 SKD_WRITEL(skdev, val, FIT_CONTROL);
2089}
2090
2091static void skd_start_device(struct skd_device *skdev)
2092{
2093 unsigned long flags;
2094 u32 sense;
2095 u32 state;
2096
2097 spin_lock_irqsave(&skdev->lock, flags);
2098
2099 /* ack all ghost interrupts */
2100 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2101
2102 sense = SKD_READL(skdev, FIT_STATUS);
2103
Bart Van Asschef98806d2017-08-17 13:12:58 -07002104 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002105
2106 state = sense & FIT_SR_DRIVE_STATE_MASK;
2107 skdev->drive_state = state;
2108 skdev->last_mtd = 0;
2109
2110 skdev->state = SKD_DRVR_STATE_STARTING;
2111 skdev->timer_countdown = SKD_STARTING_TIMO;
2112
2113 skd_enable_interrupts(skdev);
2114
2115 switch (skdev->drive_state) {
2116 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002117 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002118 break;
2119
2120 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002121 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002122 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2123 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2124 break;
2125
2126 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002127 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002128 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2129 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2130 break;
2131
2132 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002133 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002134 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2135 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2136 break;
2137
2138 case FIT_SR_DRIVE_INIT:
2139 case FIT_SR_DRIVE_ONLINE:
2140 skd_soft_reset(skdev);
2141 break;
2142
2143 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002144 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002145 skdev->state = SKD_DRVR_STATE_BUSY;
2146 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2147 break;
2148
2149 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002150 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002151 break;
2152
2153 case FIT_SR_DRIVE_FAULT:
2154 /* Fault state is bad...soft reset won't do it...
2155 * Hard reset, maybe, but does it work on device?
2156 * For now, just fault so the system doesn't hang.
2157 */
2158 skd_drive_fault(skdev);
2159 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002160 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002161 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002162 skdev->gendisk_on = -1;
2163 wake_up_interruptible(&skdev->waitq);
2164 break;
2165
2166 case 0xFF:
2167 /* Most likely the device isn't there or isn't responding
2168 * to the BAR1 addresses. */
2169 skd_drive_disappeared(skdev);
2170 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002171 dev_dbg(&skdev->pdev->dev,
2172 "starting queue to error-out reqs\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002173 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002174 skdev->gendisk_on = -1;
2175 wake_up_interruptible(&skdev->waitq);
2176 break;
2177
2178 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002179 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
2180 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002181 break;
2182 }
2183
2184 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002185 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002186
2187 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002188 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002189
2190 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002191 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002192
2193 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002194 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002195
2196 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002197 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002198
2199 spin_unlock_irqrestore(&skdev->lock, flags);
2200}
2201
2202static void skd_stop_device(struct skd_device *skdev)
2203{
2204 unsigned long flags;
2205 struct skd_special_context *skspcl = &skdev->internal_skspcl;
2206 u32 dev_state;
2207 int i;
2208
2209 spin_lock_irqsave(&skdev->lock, flags);
2210
2211 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002212 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002213 goto stop_out;
2214 }
2215
2216 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002217 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002218 goto stop_out;
2219 }
2220
2221 skdev->state = SKD_DRVR_STATE_SYNCING;
2222 skdev->sync_done = 0;
2223
2224 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
2225
2226 spin_unlock_irqrestore(&skdev->lock, flags);
2227
2228 wait_event_interruptible_timeout(skdev->waitq,
2229 (skdev->sync_done), (10 * HZ));
2230
2231 spin_lock_irqsave(&skdev->lock, flags);
2232
2233 switch (skdev->sync_done) {
2234 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002235 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002236 break;
2237 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002238 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002239 break;
2240 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002241 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002242 }
2243
2244stop_out:
2245 skdev->state = SKD_DRVR_STATE_STOPPING;
2246 spin_unlock_irqrestore(&skdev->lock, flags);
2247
2248 skd_kill_timer(skdev);
2249
2250 spin_lock_irqsave(&skdev->lock, flags);
2251 skd_disable_interrupts(skdev);
2252
2253 /* ensure all ints on device are cleared */
2254 /* soft reset the device to unload with a clean slate */
2255 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2256 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
2257
2258 spin_unlock_irqrestore(&skdev->lock, flags);
2259
2260 /* poll every 100ms, 1 second timeout */
2261 for (i = 0; i < 10; i++) {
2262 dev_state =
2263 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
2264 if (dev_state == FIT_SR_DRIVE_INIT)
2265 break;
2266 set_current_state(TASK_INTERRUPTIBLE);
2267 schedule_timeout(msecs_to_jiffies(100));
2268 }
2269
2270 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07002271 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
2272 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002273}
2274
2275/* assume spinlock is held */
2276static void skd_restart_device(struct skd_device *skdev)
2277{
2278 u32 state;
2279
2280 /* ack all ghost interrupts */
2281 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2282
2283 state = SKD_READL(skdev, FIT_STATUS);
2284
Bart Van Asschef98806d2017-08-17 13:12:58 -07002285 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002286
2287 state &= FIT_SR_DRIVE_STATE_MASK;
2288 skdev->drive_state = state;
2289 skdev->last_mtd = 0;
2290
2291 skdev->state = SKD_DRVR_STATE_RESTARTING;
2292 skdev->timer_countdown = SKD_RESTARTING_TIMO;
2293
2294 skd_soft_reset(skdev);
2295}
2296
2297/* assume spinlock is held */
2298static int skd_quiesce_dev(struct skd_device *skdev)
2299{
2300 int rc = 0;
2301
2302 switch (skdev->state) {
2303 case SKD_DRVR_STATE_BUSY:
2304 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002305 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002306 blk_mq_stop_hw_queues(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002307 break;
2308 case SKD_DRVR_STATE_ONLINE:
2309 case SKD_DRVR_STATE_STOPPING:
2310 case SKD_DRVR_STATE_SYNCING:
2311 case SKD_DRVR_STATE_PAUSING:
2312 case SKD_DRVR_STATE_PAUSED:
2313 case SKD_DRVR_STATE_STARTING:
2314 case SKD_DRVR_STATE_RESTARTING:
2315 case SKD_DRVR_STATE_RESUMING:
2316 default:
2317 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002318 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
2319 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002320 }
2321 return rc;
2322}
2323
2324/* assume spinlock is held */
2325static int skd_unquiesce_dev(struct skd_device *skdev)
2326{
2327 int prev_driver_state = skdev->state;
2328
2329 skd_log_skdev(skdev, "unquiesce");
2330 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002331 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002332 return 0;
2333 }
2334 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
2335 /*
2336 * If there has been an state change to other than
2337 * ONLINE, we will rely on controller state change
2338 * to come back online and restart the queue.
2339 * The BUSY state means that driver is ready to
2340 * continue normal processing but waiting for controller
2341 * to become available.
2342 */
2343 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002344 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002345 return 0;
2346 }
2347
2348 /*
2349 * Drive has just come online, driver is either in startup,
2350 * paused performing a task, or bust waiting for hardware.
2351 */
2352 switch (skdev->state) {
2353 case SKD_DRVR_STATE_PAUSED:
2354 case SKD_DRVR_STATE_BUSY:
2355 case SKD_DRVR_STATE_BUSY_IMMINENT:
2356 case SKD_DRVR_STATE_BUSY_ERASE:
2357 case SKD_DRVR_STATE_STARTING:
2358 case SKD_DRVR_STATE_RESTARTING:
2359 case SKD_DRVR_STATE_FAULT:
2360 case SKD_DRVR_STATE_IDLE:
2361 case SKD_DRVR_STATE_LOAD:
2362 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002363 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2364 skd_skdev_state_to_str(prev_driver_state),
2365 prev_driver_state, skd_skdev_state_to_str(skdev->state),
2366 skdev->state);
2367 dev_dbg(&skdev->pdev->dev,
2368 "**** device ONLINE...starting block queue\n");
2369 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2370 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002371 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002372 skdev->gendisk_on = 1;
2373 wake_up_interruptible(&skdev->waitq);
2374 break;
2375
2376 case SKD_DRVR_STATE_DISAPPEARED:
2377 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002378 dev_dbg(&skdev->pdev->dev,
2379 "**** driver state %d, not implemented\n",
2380 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002381 return -EBUSY;
2382 }
2383 return 0;
2384}
2385
2386/*
2387 *****************************************************************************
2388 * PCIe MSI/MSI-X INTERRUPT HANDLERS
2389 *****************************************************************************
2390 */
2391
2392static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
2393{
2394 struct skd_device *skdev = skd_host_data;
2395 unsigned long flags;
2396
2397 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002398 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2399 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2400 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
2401 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002402 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
2403 spin_unlock_irqrestore(&skdev->lock, flags);
2404 return IRQ_HANDLED;
2405}
2406
2407static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
2408{
2409 struct skd_device *skdev = skd_host_data;
2410 unsigned long flags;
2411
2412 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002413 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2414 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002415 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
2416 skd_isr_fwstate(skdev);
2417 spin_unlock_irqrestore(&skdev->lock, flags);
2418 return IRQ_HANDLED;
2419}
2420
2421static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
2422{
2423 struct skd_device *skdev = skd_host_data;
2424 unsigned long flags;
2425 int flush_enqueued = 0;
2426 int deferred;
2427
2428 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002429 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2430 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002431 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
2432 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
2433 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002434 if (flush_enqueued)
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002435 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002436
2437 if (deferred)
2438 schedule_work(&skdev->completion_worker);
2439 else if (!flush_enqueued)
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002440 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002441
2442 spin_unlock_irqrestore(&skdev->lock, flags);
2443
2444 return IRQ_HANDLED;
2445}
2446
2447static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
2448{
2449 struct skd_device *skdev = skd_host_data;
2450 unsigned long flags;
2451
2452 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002453 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2454 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002455 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
2456 skd_isr_msg_from_dev(skdev);
2457 spin_unlock_irqrestore(&skdev->lock, flags);
2458 return IRQ_HANDLED;
2459}
2460
2461static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
2462{
2463 struct skd_device *skdev = skd_host_data;
2464 unsigned long flags;
2465
2466 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002467 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2468 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002469 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
2470 spin_unlock_irqrestore(&skdev->lock, flags);
2471 return IRQ_HANDLED;
2472}
2473
2474/*
2475 *****************************************************************************
2476 * PCIe MSI/MSI-X SETUP
2477 *****************************************************************************
2478 */
2479
2480struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002481 char isr_name[30];
2482};
2483
2484struct skd_init_msix_entry {
2485 const char *name;
2486 irq_handler_t handler;
2487};
2488
2489#define SKD_MAX_MSIX_COUNT 13
2490#define SKD_MIN_MSIX_COUNT 7
2491#define SKD_BASE_MSIX_IRQ 4
2492
2493static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
2494 { "(DMA 0)", skd_reserved_isr },
2495 { "(DMA 1)", skd_reserved_isr },
2496 { "(DMA 2)", skd_reserved_isr },
2497 { "(DMA 3)", skd_reserved_isr },
2498 { "(State Change)", skd_statec_isr },
2499 { "(COMPL_Q)", skd_comp_q },
2500 { "(MSG)", skd_msg_isr },
2501 { "(Reserved)", skd_reserved_isr },
2502 { "(Reserved)", skd_reserved_isr },
2503 { "(Queue Full 0)", skd_qfull_isr },
2504 { "(Queue Full 1)", skd_qfull_isr },
2505 { "(Queue Full 2)", skd_qfull_isr },
2506 { "(Queue Full 3)", skd_qfull_isr },
2507};
2508
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002509static int skd_acquire_msix(struct skd_device *skdev)
2510{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01002511 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01002512 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002513
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002514 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
2515 PCI_IRQ_MSIX);
2516 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002517 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002518 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002519 }
Alexander Gordeev46817762014-02-19 09:58:19 +01002520
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002521 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
2522 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002523 if (!skdev->msix_entries) {
2524 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002525 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002526 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002527 }
2528
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002529 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002530 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2531 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
2532
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002533 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
2534 "%s%d-msix %s", DRV_NAME, skdev->devno,
2535 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002536
2537 rc = devm_request_irq(&skdev->pdev->dev,
2538 pci_irq_vector(skdev->pdev, i),
2539 msix_entries[i].handler, 0,
2540 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002541 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002542 dev_err(&skdev->pdev->dev,
2543 "Unable to register(%d) MSI-X handler %d: %s\n",
2544 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002545 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002546 }
2547 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002548
Bart Van Asschef98806d2017-08-17 13:12:58 -07002549 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
2550 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002551 return 0;
2552
2553msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002554 while (--i >= 0)
2555 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002556out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002557 kfree(skdev->msix_entries);
2558 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002559 return rc;
2560}
2561
2562static int skd_acquire_irq(struct skd_device *skdev)
2563{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002564 struct pci_dev *pdev = skdev->pdev;
2565 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002566 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002567
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002568 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002569 rc = skd_acquire_msix(skdev);
2570 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002571 return 0;
2572
Bart Van Asschef98806d2017-08-17 13:12:58 -07002573 dev_err(&skdev->pdev->dev,
2574 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002575 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002576
2577 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
2578 skdev->devno);
2579
2580 if (skd_isr_type != SKD_IRQ_LEGACY)
2581 irq_flag |= PCI_IRQ_MSI;
2582 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
2583 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002584 dev_err(&skdev->pdev->dev,
2585 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002586 return rc;
2587 }
2588
2589 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
2590 pdev->msi_enabled ? 0 : IRQF_SHARED,
2591 skdev->isr_name, skdev);
2592 if (rc) {
2593 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002594 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
2595 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002596 return rc;
2597 }
2598
2599 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002600}
2601
2602static void skd_release_irq(struct skd_device *skdev)
2603{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002604 struct pci_dev *pdev = skdev->pdev;
2605
2606 if (skdev->msix_entries) {
2607 int i;
2608
2609 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2610 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
2611 skdev);
2612 }
2613
2614 kfree(skdev->msix_entries);
2615 skdev->msix_entries = NULL;
2616 } else {
2617 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002618 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002619
2620 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002621}
2622
2623/*
2624 *****************************************************************************
2625 * CONSTRUCT
2626 *****************************************************************************
2627 */
2628
Bart Van Asschea3db1022017-08-17 13:13:35 -07002629static void *skd_alloc_dma(struct skd_device *skdev, struct kmem_cache *s,
2630 dma_addr_t *dma_handle, gfp_t gfp,
2631 enum dma_data_direction dir)
2632{
2633 struct device *dev = &skdev->pdev->dev;
2634 void *buf;
2635
2636 buf = kmem_cache_alloc(s, gfp);
2637 if (!buf)
2638 return NULL;
2639 *dma_handle = dma_map_single(dev, buf, s->size, dir);
2640 if (dma_mapping_error(dev, *dma_handle)) {
2641 kfree(buf);
2642 buf = NULL;
2643 }
2644 return buf;
2645}
2646
2647static void skd_free_dma(struct skd_device *skdev, struct kmem_cache *s,
2648 void *vaddr, dma_addr_t dma_handle,
2649 enum dma_data_direction dir)
2650{
2651 if (!vaddr)
2652 return;
2653
2654 dma_unmap_single(&skdev->pdev->dev, dma_handle, s->size, dir);
2655 kmem_cache_free(s, vaddr);
2656}
2657
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002658static int skd_cons_skcomp(struct skd_device *skdev)
2659{
2660 int rc = 0;
2661 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002662
Bart Van Asschef98806d2017-08-17 13:12:58 -07002663 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002664 "comp pci_alloc, total bytes %zd entries %d\n",
2665 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002666
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002667 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07002668 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002669
2670 if (skcomp == NULL) {
2671 rc = -ENOMEM;
2672 goto err_out;
2673 }
2674
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002675 skdev->skcomp_table = skcomp;
2676 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
2677 sizeof(*skcomp) *
2678 SKD_N_COMPLETION_ENTRY);
2679
2680err_out:
2681 return rc;
2682}
2683
2684static int skd_cons_skmsg(struct skd_device *skdev)
2685{
2686 int rc = 0;
2687 u32 i;
2688
Bart Van Asschef98806d2017-08-17 13:12:58 -07002689 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002690 "skmsg_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002691 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
2692 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002693
Bart Van Assche01433d02017-08-17 13:13:18 -07002694 skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context,
2695 sizeof(struct skd_fitmsg_context),
2696 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002697 if (skdev->skmsg_table == NULL) {
2698 rc = -ENOMEM;
2699 goto err_out;
2700 }
2701
2702 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2703 struct skd_fitmsg_context *skmsg;
2704
2705 skmsg = &skdev->skmsg_table[i];
2706
2707 skmsg->id = i + SKD_ID_FIT_MSG;
2708
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002709 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07002710 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002711 &skmsg->mb_dma_address);
2712
2713 if (skmsg->msg_buf == NULL) {
2714 rc = -ENOMEM;
2715 goto err_out;
2716 }
2717
Bart Van Assche6507f432017-08-17 13:13:06 -07002718 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
2719 (FIT_QCMD_ALIGN - 1),
2720 "not aligned: msg_buf %p mb_dma_address %#llx\n",
2721 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002722 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002723 }
2724
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002725err_out:
2726 return rc;
2727}
2728
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002729static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
2730 u32 n_sg,
2731 dma_addr_t *ret_dma_addr)
2732{
2733 struct fit_sg_descriptor *sg_list;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002734
Bart Van Asschea3db1022017-08-17 13:13:35 -07002735 sg_list = skd_alloc_dma(skdev, skdev->sglist_cache, ret_dma_addr,
2736 GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE);
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002737
2738 if (sg_list != NULL) {
2739 uint64_t dma_address = *ret_dma_addr;
2740 u32 i;
2741
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002742 for (i = 0; i < n_sg - 1; i++) {
2743 uint64_t ndp_off;
2744 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
2745
2746 sg_list[i].next_desc_ptr = dma_address + ndp_off;
2747 }
2748 sg_list[i].next_desc_ptr = 0LL;
2749 }
2750
2751 return sg_list;
2752}
2753
Bart Van Assche5d003242017-08-17 13:13:30 -07002754static void skd_free_sg_list(struct skd_device *skdev,
Bart Van Asschea3db1022017-08-17 13:13:35 -07002755 struct fit_sg_descriptor *sg_list,
Bart Van Assche5d003242017-08-17 13:13:30 -07002756 dma_addr_t dma_addr)
2757{
Bart Van Assche5d003242017-08-17 13:13:30 -07002758 if (WARN_ON_ONCE(!sg_list))
2759 return;
2760
Bart Van Asschea3db1022017-08-17 13:13:35 -07002761 skd_free_dma(skdev, skdev->sglist_cache, sg_list, dma_addr,
2762 DMA_TO_DEVICE);
Bart Van Assche5d003242017-08-17 13:13:30 -07002763}
2764
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002765static int skd_init_request(struct blk_mq_tag_set *set, struct request *rq,
2766 unsigned int hctx_idx, unsigned int numa_node)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002767{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002768 struct skd_device *skdev = set->driver_data;
Bart Van Asschee7278a82017-08-17 13:13:31 -07002769 struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002770
Bart Van Asschee7278a82017-08-17 13:13:31 -07002771 skreq->state = SKD_REQ_STATE_IDLE;
2772 skreq->sg = (void *)(skreq + 1);
2773 sg_init_table(skreq->sg, skd_sgs_per_request);
2774 skreq->sksg_list = skd_cons_sg_list(skdev, skd_sgs_per_request,
2775 &skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002776
Bart Van Asschee7278a82017-08-17 13:13:31 -07002777 return skreq->sksg_list ? 0 : -ENOMEM;
2778}
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002779
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002780static void skd_exit_request(struct blk_mq_tag_set *set, struct request *rq,
2781 unsigned int hctx_idx)
Bart Van Asschee7278a82017-08-17 13:13:31 -07002782{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002783 struct skd_device *skdev = set->driver_data;
Bart Van Asschee7278a82017-08-17 13:13:31 -07002784 struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002785
Bart Van Asschea3db1022017-08-17 13:13:35 -07002786 skd_free_sg_list(skdev, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002787}
2788
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002789static int skd_cons_sksb(struct skd_device *skdev)
2790{
2791 int rc = 0;
2792 struct skd_special_context *skspcl;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002793
2794 skspcl = &skdev->internal_skspcl;
2795
2796 skspcl->req.id = 0 + SKD_ID_INTERNAL;
2797 skspcl->req.state = SKD_REQ_STATE_IDLE;
2798
Bart Van Asschea3db1022017-08-17 13:13:35 -07002799 skspcl->data_buf = skd_alloc_dma(skdev, skdev->databuf_cache,
2800 &skspcl->db_dma_address,
2801 GFP_DMA | __GFP_ZERO,
2802 DMA_BIDIRECTIONAL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002803 if (skspcl->data_buf == NULL) {
2804 rc = -ENOMEM;
2805 goto err_out;
2806 }
2807
Bart Van Asschea3db1022017-08-17 13:13:35 -07002808 skspcl->msg_buf = skd_alloc_dma(skdev, skdev->msgbuf_cache,
2809 &skspcl->mb_dma_address,
2810 GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002811 if (skspcl->msg_buf == NULL) {
2812 rc = -ENOMEM;
2813 goto err_out;
2814 }
2815
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002816 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
2817 &skspcl->req.sksg_dma_address);
2818 if (skspcl->req.sksg_list == NULL) {
2819 rc = -ENOMEM;
2820 goto err_out;
2821 }
2822
2823 if (!skd_format_internal_skspcl(skdev)) {
2824 rc = -EINVAL;
2825 goto err_out;
2826 }
2827
2828err_out:
2829 return rc;
2830}
2831
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002832static const struct blk_mq_ops skd_mq_ops = {
2833 .queue_rq = skd_mq_queue_rq,
2834 .init_request = skd_init_request,
2835 .exit_request = skd_exit_request,
2836};
2837
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002838static int skd_cons_disk(struct skd_device *skdev)
2839{
2840 int rc = 0;
2841 struct gendisk *disk;
2842 struct request_queue *q;
2843 unsigned long flags;
2844
2845 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
2846 if (!disk) {
2847 rc = -ENOMEM;
2848 goto err_out;
2849 }
2850
2851 skdev->disk = disk;
2852 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
2853
2854 disk->major = skdev->major;
2855 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
2856 disk->fops = &skd_blockdev_ops;
2857 disk->private_data = skdev;
2858
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002859 memset(&skdev->tag_set, 0, sizeof(skdev->tag_set));
2860 skdev->tag_set.ops = &skd_mq_ops;
2861 skdev->tag_set.nr_hw_queues = 1;
2862 skdev->tag_set.queue_depth = skd_max_queue_depth;
2863 skdev->tag_set.cmd_size = sizeof(struct skd_request_context) +
2864 skdev->sgs_per_request * sizeof(struct scatterlist);
2865 skdev->tag_set.numa_node = NUMA_NO_NODE;
2866 skdev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
2867 BLK_MQ_F_SG_MERGE |
2868 BLK_ALLOC_POLICY_TO_MQ_FLAG(BLK_TAG_ALLOC_FIFO);
2869 skdev->tag_set.driver_data = skdev;
Dan Carpenter92d499d42017-08-23 14:20:57 +03002870 rc = blk_mq_alloc_tag_set(&skdev->tag_set);
2871 if (rc)
2872 goto err_out;
2873 q = blk_mq_init_queue(&skdev->tag_set);
2874 if (IS_ERR(q)) {
2875 blk_mq_free_tag_set(&skdev->tag_set);
2876 rc = PTR_ERR(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002877 goto err_out;
2878 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02002879 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Bart Van Asschee7278a82017-08-17 13:13:31 -07002880 q->queuedata = skdev;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002881 q->nr_requests = skd_max_queue_depth / 2;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002882
2883 skdev->queue = q;
2884 disk->queue = q;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002885
Jens Axboe6975f732016-03-30 10:11:42 -06002886 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002887 blk_queue_max_segments(q, skdev->sgs_per_request);
2888 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
2889
Bart Van Asschea5c5b392017-08-17 13:12:53 -07002890 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002891 blk_queue_io_opt(q, 8192);
2892
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002893 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06002894 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002895
Bart Van Asschea74d5b72017-08-17 13:13:33 -07002896 blk_queue_rq_timeout(q, 8 * HZ);
2897 blk_queue_rq_timed_out(q, skd_timed_out);
2898 blk_queue_softirq_done(q, skd_softirq_done);
2899
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002900 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002901 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002902 blk_mq_stop_hw_queues(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002903 spin_unlock_irqrestore(&skdev->lock, flags);
2904
2905err_out:
2906 return rc;
2907}
2908
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002909#define SKD_N_DEV_TABLE 16u
2910static u32 skd_next_devno;
2911
2912static struct skd_device *skd_construct(struct pci_dev *pdev)
2913{
2914 struct skd_device *skdev;
2915 int blk_major = skd_major;
Bart Van Asschea3db1022017-08-17 13:13:35 -07002916 size_t size;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002917 int rc;
2918
2919 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
2920
2921 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002922 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002923 return NULL;
2924 }
2925
2926 skdev->state = SKD_DRVR_STATE_LOAD;
2927 skdev->pdev = pdev;
2928 skdev->devno = skd_next_devno++;
2929 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002930 skdev->dev_max_queue_depth = 0;
2931
2932 skdev->num_req_context = skd_max_queue_depth;
2933 skdev->num_fitmsg_context = skd_max_queue_depth;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002934 skdev->cur_max_queue_depth = 1;
2935 skdev->queue_low_water_mark = 1;
2936 skdev->proto_ver = 99;
2937 skdev->sgs_per_request = skd_sgs_per_request;
2938 skdev->dbg_level = skd_dbg_level;
2939
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002940 spin_lock_init(&skdev->lock);
2941
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002942 INIT_WORK(&skdev->start_queue, skd_start_queue);
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002943 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
2944
Bart Van Asschea3db1022017-08-17 13:13:35 -07002945 size = max(SKD_N_FITMSG_BYTES, SKD_N_SPECIAL_FITMSG_BYTES);
2946 skdev->msgbuf_cache = kmem_cache_create("skd-msgbuf", size, 0,
2947 SLAB_HWCACHE_ALIGN, NULL);
2948 if (!skdev->msgbuf_cache)
2949 goto err_out;
2950 WARN_ONCE(kmem_cache_size(skdev->msgbuf_cache) < size,
2951 "skd-msgbuf: %d < %zd\n",
2952 kmem_cache_size(skdev->msgbuf_cache), size);
2953 size = skd_sgs_per_request * sizeof(struct fit_sg_descriptor);
2954 skdev->sglist_cache = kmem_cache_create("skd-sglist", size, 0,
2955 SLAB_HWCACHE_ALIGN, NULL);
2956 if (!skdev->sglist_cache)
2957 goto err_out;
2958 WARN_ONCE(kmem_cache_size(skdev->sglist_cache) < size,
2959 "skd-sglist: %d < %zd\n",
2960 kmem_cache_size(skdev->sglist_cache), size);
2961 size = SKD_N_INTERNAL_BYTES;
2962 skdev->databuf_cache = kmem_cache_create("skd-databuf", size, 0,
2963 SLAB_HWCACHE_ALIGN, NULL);
2964 if (!skdev->databuf_cache)
2965 goto err_out;
2966 WARN_ONCE(kmem_cache_size(skdev->databuf_cache) < size,
2967 "skd-databuf: %d < %zd\n",
2968 kmem_cache_size(skdev->databuf_cache), size);
2969
Bart Van Asschef98806d2017-08-17 13:12:58 -07002970 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002971 rc = skd_cons_skcomp(skdev);
2972 if (rc < 0)
2973 goto err_out;
2974
Bart Van Asschef98806d2017-08-17 13:12:58 -07002975 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002976 rc = skd_cons_skmsg(skdev);
2977 if (rc < 0)
2978 goto err_out;
2979
Bart Van Asschef98806d2017-08-17 13:12:58 -07002980 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002981 rc = skd_cons_sksb(skdev);
2982 if (rc < 0)
2983 goto err_out;
2984
Bart Van Asschef98806d2017-08-17 13:12:58 -07002985 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002986 rc = skd_cons_disk(skdev);
2987 if (rc < 0)
2988 goto err_out;
2989
Bart Van Asschef98806d2017-08-17 13:12:58 -07002990 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002991 return skdev;
2992
2993err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002994 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002995 skd_destruct(skdev);
2996 return NULL;
2997}
2998
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002999/*
3000 *****************************************************************************
3001 * DESTRUCT (FREE)
3002 *****************************************************************************
3003 */
3004
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003005static void skd_free_skcomp(struct skd_device *skdev)
3006{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07003007 if (skdev->skcomp_table)
3008 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003009 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003010
3011 skdev->skcomp_table = NULL;
3012 skdev->cq_dma_address = 0;
3013}
3014
3015static void skd_free_skmsg(struct skd_device *skdev)
3016{
3017 u32 i;
3018
3019 if (skdev->skmsg_table == NULL)
3020 return;
3021
3022 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3023 struct skd_fitmsg_context *skmsg;
3024
3025 skmsg = &skdev->skmsg_table[i];
3026
3027 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003028 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
3029 skmsg->msg_buf,
3030 skmsg->mb_dma_address);
3031 }
3032 skmsg->msg_buf = NULL;
3033 skmsg->mb_dma_address = 0;
3034 }
3035
3036 kfree(skdev->skmsg_table);
3037 skdev->skmsg_table = NULL;
3038}
3039
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003040static void skd_free_sksb(struct skd_device *skdev)
3041{
Bart Van Asschea3db1022017-08-17 13:13:35 -07003042 struct skd_special_context *skspcl = &skdev->internal_skspcl;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003043
Bart Van Asschea3db1022017-08-17 13:13:35 -07003044 skd_free_dma(skdev, skdev->databuf_cache, skspcl->data_buf,
3045 skspcl->db_dma_address, DMA_BIDIRECTIONAL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003046
3047 skspcl->data_buf = NULL;
3048 skspcl->db_dma_address = 0;
3049
Bart Van Asschea3db1022017-08-17 13:13:35 -07003050 skd_free_dma(skdev, skdev->msgbuf_cache, skspcl->msg_buf,
3051 skspcl->mb_dma_address, DMA_TO_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003052
3053 skspcl->msg_buf = NULL;
3054 skspcl->mb_dma_address = 0;
3055
Bart Van Asschea3db1022017-08-17 13:13:35 -07003056 skd_free_sg_list(skdev, skspcl->req.sksg_list,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003057 skspcl->req.sksg_dma_address);
3058
3059 skspcl->req.sksg_list = NULL;
3060 skspcl->req.sksg_dma_address = 0;
3061}
3062
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003063static void skd_free_disk(struct skd_device *skdev)
3064{
3065 struct gendisk *disk = skdev->disk;
3066
Bart Van Assche7277cc62017-08-17 13:12:45 -07003067 if (disk && (disk->flags & GENHD_FL_UP))
3068 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003069
Bart Van Assche7277cc62017-08-17 13:12:45 -07003070 if (skdev->queue) {
3071 blk_cleanup_queue(skdev->queue);
3072 skdev->queue = NULL;
3073 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003074 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07003075
Bart Van Asscheca33dd92017-08-17 13:13:32 -07003076 if (skdev->tag_set.tags)
3077 blk_mq_free_tag_set(&skdev->tag_set);
3078
Bart Van Assche7277cc62017-08-17 13:12:45 -07003079 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003080 skdev->disk = NULL;
3081}
3082
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003083static void skd_destruct(struct skd_device *skdev)
3084{
3085 if (skdev == NULL)
3086 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003087
Bart Van Asscheca33dd92017-08-17 13:13:32 -07003088 cancel_work_sync(&skdev->start_queue);
3089
Bart Van Asschef98806d2017-08-17 13:12:58 -07003090 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003091 skd_free_disk(skdev);
3092
Bart Van Asschef98806d2017-08-17 13:12:58 -07003093 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003094 skd_free_sksb(skdev);
3095
Bart Van Asschef98806d2017-08-17 13:12:58 -07003096 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003097 skd_free_skmsg(skdev);
3098
Bart Van Asschef98806d2017-08-17 13:12:58 -07003099 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003100 skd_free_skcomp(skdev);
3101
Bart Van Asschea3db1022017-08-17 13:13:35 -07003102 kmem_cache_destroy(skdev->databuf_cache);
3103 kmem_cache_destroy(skdev->sglist_cache);
3104 kmem_cache_destroy(skdev->msgbuf_cache);
3105
Bart Van Asschef98806d2017-08-17 13:12:58 -07003106 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003107 kfree(skdev);
3108}
3109
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003110/*
3111 *****************************************************************************
3112 * BLOCK DEVICE (BDEV) GLUE
3113 *****************************************************************************
3114 */
3115
3116static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3117{
3118 struct skd_device *skdev;
3119 u64 capacity;
3120
3121 skdev = bdev->bd_disk->private_data;
3122
Bart Van Asschef98806d2017-08-17 13:12:58 -07003123 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
3124 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003125
3126 if (skdev->read_cap_is_valid) {
3127 capacity = get_capacity(skdev->disk);
3128 geo->heads = 64;
3129 geo->sectors = 255;
3130 geo->cylinders = (capacity) / (255 * 64);
3131
3132 return 0;
3133 }
3134 return -EIO;
3135}
3136
Dan Williams0d52c7562016-06-15 19:44:20 -07003137static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003138{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003139 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07003140 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003141 return 0;
3142}
3143
3144static const struct block_device_operations skd_blockdev_ops = {
3145 .owner = THIS_MODULE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003146 .getgeo = skd_bdev_getgeo,
3147};
3148
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003149/*
3150 *****************************************************************************
3151 * PCIe DRIVER GLUE
3152 *****************************************************************************
3153 */
3154
Benoit Taine9baa3c32014-08-08 15:56:03 +02003155static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003156 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
3157 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
3158 { 0 } /* terminate list */
3159};
3160
3161MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
3162
3163static char *skd_pci_info(struct skd_device *skdev, char *str)
3164{
3165 int pcie_reg;
3166
3167 strcpy(str, "PCIe (");
3168 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
3169
3170 if (pcie_reg) {
3171
3172 char lwstr[6];
3173 uint16_t pcie_lstat, lspeed, lwidth;
3174
3175 pcie_reg += 0x12;
3176 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
3177 lspeed = pcie_lstat & (0xF);
3178 lwidth = (pcie_lstat & 0x3F0) >> 4;
3179
3180 if (lspeed == 1)
3181 strcat(str, "2.5GT/s ");
3182 else if (lspeed == 2)
3183 strcat(str, "5.0GT/s ");
3184 else
3185 strcat(str, "<unknown> ");
3186 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
3187 strcat(str, lwstr);
3188 }
3189 return str;
3190}
3191
3192static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3193{
3194 int i;
3195 int rc = 0;
3196 char pci_str[32];
3197 struct skd_device *skdev;
3198
Bart Van Asschebb9f7dd2017-08-17 13:13:38 -07003199 dev_dbg(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
3200 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003201
3202 rc = pci_enable_device(pdev);
3203 if (rc)
3204 return rc;
3205 rc = pci_request_regions(pdev, DRV_NAME);
3206 if (rc)
3207 goto err_out;
3208 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3209 if (!rc) {
3210 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003211 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3212 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003213 }
3214 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003215 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003216 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003217 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003218 goto err_out_regions;
3219 }
3220 }
3221
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003222 if (!skd_major) {
3223 rc = register_blkdev(0, DRV_NAME);
3224 if (rc < 0)
3225 goto err_out_regions;
3226 BUG_ON(!rc);
3227 skd_major = rc;
3228 }
3229
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003230 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08003231 if (skdev == NULL) {
3232 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003233 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08003234 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003235
3236 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003237 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003238
3239 pci_set_master(pdev);
3240 rc = pci_enable_pcie_error_reporting(pdev);
3241 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003242 dev_err(&pdev->dev,
3243 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003244 skdev->pcie_error_reporting_is_enabled = 0;
3245 } else
3246 skdev->pcie_error_reporting_is_enabled = 1;
3247
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003248 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01003249
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003250 for (i = 0; i < SKD_MAX_BARS; i++) {
3251 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3252 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3253 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3254 skdev->mem_size[i]);
3255 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003256 dev_err(&pdev->dev,
3257 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003258 rc = -ENODEV;
3259 goto err_out_iounmap;
3260 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003261 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3262 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3263 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003264 }
3265
3266 rc = skd_acquire_irq(skdev);
3267 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003268 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003269 goto err_out_iounmap;
3270 }
3271
3272 rc = skd_start_timer(skdev);
3273 if (rc)
3274 goto err_out_timer;
3275
3276 init_waitqueue_head(&skdev->waitq);
3277
3278 skd_start_device(skdev);
3279
3280 rc = wait_event_interruptible_timeout(skdev->waitq,
3281 (skdev->gendisk_on),
3282 (SKD_START_WAIT_SECONDS * HZ));
3283 if (skdev->gendisk_on > 0) {
3284 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07003285 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003286 rc = 0;
3287 } else {
3288 /* we timed out, something is wrong with the device,
3289 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003290 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
3291 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003292 /* in case of no error; we timeout with ENXIO */
3293 if (!rc)
3294 rc = -ENXIO;
3295 goto err_out_timer;
3296 }
3297
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003298 return rc;
3299
3300err_out_timer:
3301 skd_stop_device(skdev);
3302 skd_release_irq(skdev);
3303
3304err_out_iounmap:
3305 for (i = 0; i < SKD_MAX_BARS; i++)
3306 if (skdev->mem_map[i])
3307 iounmap(skdev->mem_map[i]);
3308
3309 if (skdev->pcie_error_reporting_is_enabled)
3310 pci_disable_pcie_error_reporting(pdev);
3311
3312 skd_destruct(skdev);
3313
3314err_out_regions:
3315 pci_release_regions(pdev);
3316
3317err_out:
3318 pci_disable_device(pdev);
3319 pci_set_drvdata(pdev, NULL);
3320 return rc;
3321}
3322
3323static void skd_pci_remove(struct pci_dev *pdev)
3324{
3325 int i;
3326 struct skd_device *skdev;
3327
3328 skdev = pci_get_drvdata(pdev);
3329 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003330 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003331 return;
3332 }
3333 skd_stop_device(skdev);
3334 skd_release_irq(skdev);
3335
3336 for (i = 0; i < SKD_MAX_BARS; i++)
3337 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003338 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003339
3340 if (skdev->pcie_error_reporting_is_enabled)
3341 pci_disable_pcie_error_reporting(pdev);
3342
3343 skd_destruct(skdev);
3344
3345 pci_release_regions(pdev);
3346 pci_disable_device(pdev);
3347 pci_set_drvdata(pdev, NULL);
3348
3349 return;
3350}
3351
3352static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3353{
3354 int i;
3355 struct skd_device *skdev;
3356
3357 skdev = pci_get_drvdata(pdev);
3358 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003359 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003360 return -EIO;
3361 }
3362
3363 skd_stop_device(skdev);
3364
3365 skd_release_irq(skdev);
3366
3367 for (i = 0; i < SKD_MAX_BARS; i++)
3368 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003369 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003370
3371 if (skdev->pcie_error_reporting_is_enabled)
3372 pci_disable_pcie_error_reporting(pdev);
3373
3374 pci_release_regions(pdev);
3375 pci_save_state(pdev);
3376 pci_disable_device(pdev);
3377 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3378 return 0;
3379}
3380
3381static int skd_pci_resume(struct pci_dev *pdev)
3382{
3383 int i;
3384 int rc = 0;
3385 struct skd_device *skdev;
3386
3387 skdev = pci_get_drvdata(pdev);
3388 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003389 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003390 return -1;
3391 }
3392
3393 pci_set_power_state(pdev, PCI_D0);
3394 pci_enable_wake(pdev, PCI_D0, 0);
3395 pci_restore_state(pdev);
3396
3397 rc = pci_enable_device(pdev);
3398 if (rc)
3399 return rc;
3400 rc = pci_request_regions(pdev, DRV_NAME);
3401 if (rc)
3402 goto err_out;
3403 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3404 if (!rc) {
3405 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3406
Bart Van Asschef98806d2017-08-17 13:12:58 -07003407 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3408 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003409 }
3410 } else {
3411 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3412 if (rc) {
3413
Bart Van Asschef98806d2017-08-17 13:12:58 -07003414 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003415 goto err_out_regions;
3416 }
3417 }
3418
3419 pci_set_master(pdev);
3420 rc = pci_enable_pcie_error_reporting(pdev);
3421 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003422 dev_err(&pdev->dev,
3423 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003424 skdev->pcie_error_reporting_is_enabled = 0;
3425 } else
3426 skdev->pcie_error_reporting_is_enabled = 1;
3427
3428 for (i = 0; i < SKD_MAX_BARS; i++) {
3429
3430 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3431 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3432 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3433 skdev->mem_size[i]);
3434 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003435 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003436 rc = -ENODEV;
3437 goto err_out_iounmap;
3438 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003439 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3440 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3441 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003442 }
3443 rc = skd_acquire_irq(skdev);
3444 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003445 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003446 goto err_out_iounmap;
3447 }
3448
3449 rc = skd_start_timer(skdev);
3450 if (rc)
3451 goto err_out_timer;
3452
3453 init_waitqueue_head(&skdev->waitq);
3454
3455 skd_start_device(skdev);
3456
3457 return rc;
3458
3459err_out_timer:
3460 skd_stop_device(skdev);
3461 skd_release_irq(skdev);
3462
3463err_out_iounmap:
3464 for (i = 0; i < SKD_MAX_BARS; i++)
3465 if (skdev->mem_map[i])
3466 iounmap(skdev->mem_map[i]);
3467
3468 if (skdev->pcie_error_reporting_is_enabled)
3469 pci_disable_pcie_error_reporting(pdev);
3470
3471err_out_regions:
3472 pci_release_regions(pdev);
3473
3474err_out:
3475 pci_disable_device(pdev);
3476 return rc;
3477}
3478
3479static void skd_pci_shutdown(struct pci_dev *pdev)
3480{
3481 struct skd_device *skdev;
3482
Bart Van Asschef98806d2017-08-17 13:12:58 -07003483 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003484
3485 skdev = pci_get_drvdata(pdev);
3486 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003487 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003488 return;
3489 }
3490
Bart Van Asschef98806d2017-08-17 13:12:58 -07003491 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003492 skd_stop_device(skdev);
3493}
3494
3495static struct pci_driver skd_driver = {
3496 .name = DRV_NAME,
3497 .id_table = skd_pci_tbl,
3498 .probe = skd_pci_probe,
3499 .remove = skd_pci_remove,
3500 .suspend = skd_pci_suspend,
3501 .resume = skd_pci_resume,
3502 .shutdown = skd_pci_shutdown,
3503};
3504
3505/*
3506 *****************************************************************************
3507 * LOGGING SUPPORT
3508 *****************************************************************************
3509 */
3510
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003511const char *skd_drive_state_to_str(int state)
3512{
3513 switch (state) {
3514 case FIT_SR_DRIVE_OFFLINE:
3515 return "OFFLINE";
3516 case FIT_SR_DRIVE_INIT:
3517 return "INIT";
3518 case FIT_SR_DRIVE_ONLINE:
3519 return "ONLINE";
3520 case FIT_SR_DRIVE_BUSY:
3521 return "BUSY";
3522 case FIT_SR_DRIVE_FAULT:
3523 return "FAULT";
3524 case FIT_SR_DRIVE_DEGRADED:
3525 return "DEGRADED";
3526 case FIT_SR_PCIE_LINK_DOWN:
3527 return "INK_DOWN";
3528 case FIT_SR_DRIVE_SOFT_RESET:
3529 return "SOFT_RESET";
3530 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3531 return "NEED_FW";
3532 case FIT_SR_DRIVE_INIT_FAULT:
3533 return "INIT_FAULT";
3534 case FIT_SR_DRIVE_BUSY_SANITIZE:
3535 return "BUSY_SANITIZE";
3536 case FIT_SR_DRIVE_BUSY_ERASE:
3537 return "BUSY_ERASE";
3538 case FIT_SR_DRIVE_FW_BOOTING:
3539 return "FW_BOOTING";
3540 default:
3541 return "???";
3542 }
3543}
3544
3545const char *skd_skdev_state_to_str(enum skd_drvr_state state)
3546{
3547 switch (state) {
3548 case SKD_DRVR_STATE_LOAD:
3549 return "LOAD";
3550 case SKD_DRVR_STATE_IDLE:
3551 return "IDLE";
3552 case SKD_DRVR_STATE_BUSY:
3553 return "BUSY";
3554 case SKD_DRVR_STATE_STARTING:
3555 return "STARTING";
3556 case SKD_DRVR_STATE_ONLINE:
3557 return "ONLINE";
3558 case SKD_DRVR_STATE_PAUSING:
3559 return "PAUSING";
3560 case SKD_DRVR_STATE_PAUSED:
3561 return "PAUSED";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003562 case SKD_DRVR_STATE_RESTARTING:
3563 return "RESTARTING";
3564 case SKD_DRVR_STATE_RESUMING:
3565 return "RESUMING";
3566 case SKD_DRVR_STATE_STOPPING:
3567 return "STOPPING";
3568 case SKD_DRVR_STATE_SYNCING:
3569 return "SYNCING";
3570 case SKD_DRVR_STATE_FAULT:
3571 return "FAULT";
3572 case SKD_DRVR_STATE_DISAPPEARED:
3573 return "DISAPPEARED";
3574 case SKD_DRVR_STATE_BUSY_ERASE:
3575 return "BUSY_ERASE";
3576 case SKD_DRVR_STATE_BUSY_SANITIZE:
3577 return "BUSY_SANITIZE";
3578 case SKD_DRVR_STATE_BUSY_IMMINENT:
3579 return "BUSY_IMMINENT";
3580 case SKD_DRVR_STATE_WAIT_BOOT:
3581 return "WAIT_BOOT";
3582
3583 default:
3584 return "???";
3585 }
3586}
3587
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05303588static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003589{
3590 switch (state) {
3591 case SKD_REQ_STATE_IDLE:
3592 return "IDLE";
3593 case SKD_REQ_STATE_SETUP:
3594 return "SETUP";
3595 case SKD_REQ_STATE_BUSY:
3596 return "BUSY";
3597 case SKD_REQ_STATE_COMPLETED:
3598 return "COMPLETED";
3599 case SKD_REQ_STATE_TIMEOUT:
3600 return "TIMEOUT";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003601 default:
3602 return "???";
3603 }
3604}
3605
3606static void skd_log_skdev(struct skd_device *skdev, const char *event)
3607{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003608 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
3609 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
3610 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3611 skd_skdev_state_to_str(skdev->state), skdev->state);
3612 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
Bart Van Assched4d0f5f2017-08-17 13:13:34 -07003613 skd_in_flight(skdev), skdev->cur_max_queue_depth,
Bart Van Asschef98806d2017-08-17 13:12:58 -07003614 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
Bart Van Asschea74d5b72017-08-17 13:13:33 -07003615 dev_dbg(&skdev->pdev->dev, " cycle=%d cycle_ix=%d\n",
3616 skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003617}
3618
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003619static void skd_log_skreq(struct skd_device *skdev,
3620 struct skd_request_context *skreq, const char *event)
3621{
Bart Van Asschee7278a82017-08-17 13:13:31 -07003622 struct request *req = blk_mq_rq_from_pdu(skreq);
3623 u32 lba = blk_rq_pos(req);
3624 u32 count = blk_rq_sectors(req);
3625
Bart Van Asschef98806d2017-08-17 13:12:58 -07003626 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
3627 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
3628 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
3629 skreq->fitmsg_id);
Bart Van Asschea74d5b72017-08-17 13:13:33 -07003630 dev_dbg(&skdev->pdev->dev, " sg_dir=%d n_sg=%d\n",
3631 skreq->data_dir, skreq->n_sg);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07003632
Bart Van Asschee7278a82017-08-17 13:13:31 -07003633 dev_dbg(&skdev->pdev->dev,
3634 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba, lba,
3635 count, count, (int)rq_data_dir(req));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003636}
3637
3638/*
3639 *****************************************************************************
3640 * MODULE GLUE
3641 *****************************************************************************
3642 */
3643
3644static int __init skd_init(void)
3645{
Bart Van Assche16a70532017-08-17 13:13:08 -07003646 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
3647 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
3648 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
3649 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
3650 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07003651 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
3652 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
3653 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07003654
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003655 switch (skd_isr_type) {
3656 case SKD_IRQ_LEGACY:
3657 case SKD_IRQ_MSI:
3658 case SKD_IRQ_MSIX:
3659 break;
3660 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003661 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003662 skd_isr_type, SKD_IRQ_DEFAULT);
3663 skd_isr_type = SKD_IRQ_DEFAULT;
3664 }
3665
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003666 if (skd_max_queue_depth < 1 ||
3667 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
3668 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003669 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
3670 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
3671 }
3672
Bart Van Assche2da7b402017-08-17 13:13:01 -07003673 if (skd_max_req_per_msg < 1 ||
3674 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003675 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003676 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
3677 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
3678 }
3679
3680 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003681 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003682 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
3683 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
3684 }
3685
3686 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003687 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003688 skd_dbg_level, 0);
3689 skd_dbg_level = 0;
3690 }
3691
3692 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003693 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003694 skd_isr_comp_limit, 0);
3695 skd_isr_comp_limit = 0;
3696 }
3697
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003698 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003699}
3700
3701static void __exit skd_exit(void)
3702{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003704
3705 if (skd_major)
3706 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003707}
3708
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003709module_init(skd_init);
3710module_exit(skd_exit);