blob: 35938ee0651a2baaad269185d83c5c501c87365e [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 Asschef98806d2017-08-17 13:12:58 -07001567 dev_dbg(&skdev->pdev->dev,
1568 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
1569 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001570 {
1571 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001572 dev_err(&skdev->pdev->dev,
1573 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
1574 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001575
1576 continue;
1577 }
1578 }
1579
1580 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
1581
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001582 skreq->completion = *skcmp;
1583 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
1584 skreq->err_info = *skerr;
1585 skd_log_check_status(skdev, cmp_status, skerr->key,
1586 skerr->code, skerr->qual,
1587 skerr->fruc);
1588 }
1589 /* Release DMA resources for the request. */
1590 if (skreq->n_sg > 0)
1591 skd_postop_sg_list(skdev, skreq);
1592
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001593 skd_release_skreq(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001594
1595 /*
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001596 * Capture the outcome and post it back to the native request.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001597 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001598 if (likely(cmp_status == SAM_STAT_GOOD))
1599 skd_end_request(skdev, rq, BLK_STS_OK);
1600 else
1601 skd_resolve_req_exception(skdev, skreq, rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001602
1603 /* skd_isr_comp_limit equal zero means no limit */
1604 if (limit) {
1605 if (++processed >= limit) {
1606 rc = 1;
1607 break;
1608 }
1609 }
1610 }
1611
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001612 if (skdev->state == SKD_DRVR_STATE_PAUSING &&
Bart Van Assched4d0f5f2017-08-17 13:13:34 -07001613 skd_in_flight(skdev) == 0) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001614 skdev->state = SKD_DRVR_STATE_PAUSED;
1615 wake_up_interruptible(&skdev->waitq);
1616 }
1617
1618 return rc;
1619}
1620
1621static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001622 struct fit_completion_entry_v1 *skcomp,
1623 struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001624{
1625 u32 req_id = 0;
1626 u32 req_table;
1627 u32 req_slot;
1628 struct skd_special_context *skspcl;
1629
Bart Van Assche760b48c2017-08-17 13:13:00 -07001630 lockdep_assert_held(&skdev->lock);
1631
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001632 req_id = skcomp->tag;
1633 req_table = req_id & SKD_ID_TABLE_MASK;
1634 req_slot = req_id & SKD_ID_SLOT_MASK;
1635
Bart Van Asschef98806d2017-08-17 13:12:58 -07001636 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
1637 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001638
1639 /*
1640 * Based on the request id, determine how to dispatch this completion.
1641 * This swich/case is finding the good cases and forwarding the
1642 * completion entry. Errors are reported below the switch.
1643 */
1644 switch (req_table) {
1645 case SKD_ID_RW_REQUEST:
1646 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07001647 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001648 * handles r/w requests. The only way we get here
1649 * is if the req_slot is out of bounds.
1650 */
1651 break;
1652
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001653 case SKD_ID_INTERNAL:
1654 if (req_slot == 0) {
1655 skspcl = &skdev->internal_skspcl;
1656 if (skspcl->req.id == req_id &&
1657 skspcl->req.state == SKD_REQ_STATE_BUSY) {
1658 skd_complete_internal(skdev,
1659 skcomp, skerr, skspcl);
1660 return;
1661 }
1662 }
1663 break;
1664
1665 case SKD_ID_FIT_MSG:
1666 /*
1667 * These id's should never appear in a completion record.
1668 */
1669 break;
1670
1671 default:
1672 /*
1673 * These id's should never appear anywhere;
1674 */
1675 break;
1676 }
1677
1678 /*
1679 * If we get here it is a bad or stale id.
1680 */
1681}
1682
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001683static void skd_reset_skcomp(struct skd_device *skdev)
1684{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07001685 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001686
1687 skdev->skcomp_ix = 0;
1688 skdev->skcomp_cycle = 1;
1689}
1690
1691/*
1692 *****************************************************************************
1693 * INTERRUPTS
1694 *****************************************************************************
1695 */
1696static void skd_completion_worker(struct work_struct *work)
1697{
1698 struct skd_device *skdev =
1699 container_of(work, struct skd_device, completion_worker);
1700 unsigned long flags;
1701 int flush_enqueued = 0;
1702
1703 spin_lock_irqsave(&skdev->lock, flags);
1704
1705 /*
1706 * pass in limit=0, which means no limit..
1707 * process everything in compq
1708 */
1709 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001710 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001711
1712 spin_unlock_irqrestore(&skdev->lock, flags);
1713}
1714
1715static void skd_isr_msg_from_dev(struct skd_device *skdev);
1716
Arnd Bergmann41c94992016-11-09 13:55:35 +01001717static irqreturn_t
1718skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001719{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07001720 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001721 u32 intstat;
1722 u32 ack;
1723 int rc = 0;
1724 int deferred = 0;
1725 int flush_enqueued = 0;
1726
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001727 spin_lock(&skdev->lock);
1728
1729 for (;; ) {
1730 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
1731
1732 ack = FIT_INT_DEF_MASK;
1733 ack &= intstat;
1734
Bart Van Asschef98806d2017-08-17 13:12:58 -07001735 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
1736 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001737
1738 /* As long as there is an int pending on device, keep
1739 * running loop. When none, get out, but if we've never
1740 * done any processing, call completion handler?
1741 */
1742 if (ack == 0) {
1743 /* No interrupts on device, but run the completion
1744 * processor anyway?
1745 */
1746 if (rc == 0)
1747 if (likely (skdev->state
1748 == SKD_DRVR_STATE_ONLINE))
1749 deferred = 1;
1750 break;
1751 }
1752
1753 rc = IRQ_HANDLED;
1754
1755 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
1756
1757 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
1758 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
1759 if (intstat & FIT_ISH_COMPLETION_POSTED) {
1760 /*
1761 * If we have already deferred completion
1762 * processing, don't bother running it again
1763 */
1764 if (deferred == 0)
1765 deferred =
1766 skd_isr_completion_posted(skdev,
1767 skd_isr_comp_limit, &flush_enqueued);
1768 }
1769
1770 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
1771 skd_isr_fwstate(skdev);
1772 if (skdev->state == SKD_DRVR_STATE_FAULT ||
1773 skdev->state ==
1774 SKD_DRVR_STATE_DISAPPEARED) {
1775 spin_unlock(&skdev->lock);
1776 return rc;
1777 }
1778 }
1779
1780 if (intstat & FIT_ISH_MSG_FROM_DEV)
1781 skd_isr_msg_from_dev(skdev);
1782 }
1783 }
1784
1785 if (unlikely(flush_enqueued))
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001786 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001787
1788 if (deferred)
1789 schedule_work(&skdev->completion_worker);
1790 else if (!flush_enqueued)
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001791 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001792
1793 spin_unlock(&skdev->lock);
1794
1795 return rc;
1796}
1797
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001798static void skd_drive_fault(struct skd_device *skdev)
1799{
1800 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001801 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001802}
1803
1804static void skd_drive_disappeared(struct skd_device *skdev)
1805{
1806 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001807 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001808}
1809
1810static void skd_isr_fwstate(struct skd_device *skdev)
1811{
1812 u32 sense;
1813 u32 state;
1814 u32 mtd;
1815 int prev_driver_state = skdev->state;
1816
1817 sense = SKD_READL(skdev, FIT_STATUS);
1818 state = sense & FIT_SR_DRIVE_STATE_MASK;
1819
Bart Van Asschef98806d2017-08-17 13:12:58 -07001820 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
1821 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
1822 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001823
1824 skdev->drive_state = state;
1825
1826 switch (skdev->drive_state) {
1827 case FIT_SR_DRIVE_INIT:
1828 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
1829 skd_disable_interrupts(skdev);
1830 break;
1831 }
1832 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001833 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001834 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
1835 skdev->timer_countdown = SKD_STARTING_TIMO;
1836 skdev->state = SKD_DRVR_STATE_STARTING;
1837 skd_soft_reset(skdev);
1838 break;
1839 }
1840 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
1841 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1842 skdev->last_mtd = mtd;
1843 break;
1844
1845 case FIT_SR_DRIVE_ONLINE:
1846 skdev->cur_max_queue_depth = skd_max_queue_depth;
1847 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
1848 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
1849
1850 skdev->queue_low_water_mark =
1851 skdev->cur_max_queue_depth * 2 / 3 + 1;
1852 if (skdev->queue_low_water_mark < 1)
1853 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001854 dev_info(&skdev->pdev->dev,
1855 "Queue depth limit=%d dev=%d lowat=%d\n",
1856 skdev->cur_max_queue_depth,
1857 skdev->dev_max_queue_depth,
1858 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001859
1860 skd_refresh_device_data(skdev);
1861 break;
1862
1863 case FIT_SR_DRIVE_BUSY:
1864 skdev->state = SKD_DRVR_STATE_BUSY;
1865 skdev->timer_countdown = SKD_BUSY_TIMO;
1866 skd_quiesce_dev(skdev);
1867 break;
1868 case FIT_SR_DRIVE_BUSY_SANITIZE:
1869 /* set timer for 3 seconds, we'll abort any unfinished
1870 * commands after that expires
1871 */
1872 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
1873 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001874 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001875 break;
1876 case FIT_SR_DRIVE_BUSY_ERASE:
1877 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
1878 skdev->timer_countdown = SKD_BUSY_TIMO;
1879 break;
1880 case FIT_SR_DRIVE_OFFLINE:
1881 skdev->state = SKD_DRVR_STATE_IDLE;
1882 break;
1883 case FIT_SR_DRIVE_SOFT_RESET:
1884 switch (skdev->state) {
1885 case SKD_DRVR_STATE_STARTING:
1886 case SKD_DRVR_STATE_RESTARTING:
1887 /* Expected by a caller of skd_soft_reset() */
1888 break;
1889 default:
1890 skdev->state = SKD_DRVR_STATE_RESTARTING;
1891 break;
1892 }
1893 break;
1894 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001895 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001896 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
1897 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
1898 break;
1899
1900 case FIT_SR_DRIVE_DEGRADED:
1901 case FIT_SR_PCIE_LINK_DOWN:
1902 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
1903 break;
1904
1905 case FIT_SR_DRIVE_FAULT:
1906 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001907 skd_recover_requests(skdev);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001908 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001909 break;
1910
1911 /* PCIe bus returned all Fs? */
1912 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001913 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
1914 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001915 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001916 skd_recover_requests(skdev);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001917 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001918 break;
1919 default:
1920 /*
1921 * Uknown FW State. Wait for a state we recognize.
1922 */
1923 break;
1924 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001925 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
1926 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
1927 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001928}
1929
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001930static void skd_recover_request(struct request *req, void *data, bool reserved)
Bart Van Assche4e54b842017-08-17 13:13:29 -07001931{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001932 struct skd_device *const skdev = data;
1933 struct skd_request_context *skreq = blk_mq_rq_to_pdu(req);
Bart Van Assche4e54b842017-08-17 13:13:29 -07001934
1935 if (skreq->state != SKD_REQ_STATE_BUSY)
1936 return;
1937
1938 skd_log_skreq(skdev, skreq, "recover");
1939
Bart Van Assche4e54b842017-08-17 13:13:29 -07001940 /* Release DMA resources for the request. */
1941 if (skreq->n_sg > 0)
1942 skd_postop_sg_list(skdev, skreq);
1943
Bart Van Assche4e54b842017-08-17 13:13:29 -07001944 skreq->state = SKD_REQ_STATE_IDLE;
1945
1946 skd_end_request(skdev, req, BLK_STS_IOERR);
1947}
1948
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001949static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001950{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07001951 blk_mq_tagset_busy_iter(&skdev->tag_set, skd_recover_request, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001952}
1953
1954static void skd_isr_msg_from_dev(struct skd_device *skdev)
1955{
1956 u32 mfd;
1957 u32 mtd;
1958 u32 data;
1959
1960 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
1961
Bart Van Asschef98806d2017-08-17 13:12:58 -07001962 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
1963 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001964
1965 /* ignore any mtd that is an ack for something we didn't send */
1966 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
1967 return;
1968
1969 switch (FIT_MXD_TYPE(mfd)) {
1970 case FIT_MTD_FITFW_INIT:
1971 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
1972
1973 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001974 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
1975 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
1976 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
1977 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001978 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
1979 skd_soft_reset(skdev);
1980 break;
1981 }
1982 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
1983 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1984 skdev->last_mtd = mtd;
1985 break;
1986
1987 case FIT_MTD_GET_CMDQ_DEPTH:
1988 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
1989 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
1990 SKD_N_COMPLETION_ENTRY);
1991 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1992 skdev->last_mtd = mtd;
1993 break;
1994
1995 case FIT_MTD_SET_COMPQ_DEPTH:
1996 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
1997 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
1998 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1999 skdev->last_mtd = mtd;
2000 break;
2001
2002 case FIT_MTD_SET_COMPQ_ADDR:
2003 skd_reset_skcomp(skdev);
2004 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
2005 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2006 skdev->last_mtd = mtd;
2007 break;
2008
2009 case FIT_MTD_CMD_LOG_HOST_ID:
2010 skdev->connect_time_stamp = get_seconds();
2011 data = skdev->connect_time_stamp & 0xFFFF;
2012 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
2013 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2014 skdev->last_mtd = mtd;
2015 break;
2016
2017 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
2018 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
2019 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
2020 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
2021 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2022 skdev->last_mtd = mtd;
2023 break;
2024
2025 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
2026 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
2027 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
2028 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2029 skdev->last_mtd = mtd;
2030
Bart Van Asschef98806d2017-08-17 13:12:58 -07002031 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
2032 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002033 break;
2034
2035 case FIT_MTD_ARM_QUEUE:
2036 skdev->last_mtd = 0;
2037 /*
2038 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
2039 */
2040 break;
2041
2042 default:
2043 break;
2044 }
2045}
2046
2047static void skd_disable_interrupts(struct skd_device *skdev)
2048{
2049 u32 sense;
2050
2051 sense = SKD_READL(skdev, FIT_CONTROL);
2052 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
2053 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002054 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002055
2056 /* Note that the 1s is written. A 1-bit means
2057 * disable, a 0 means enable.
2058 */
2059 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
2060}
2061
2062static void skd_enable_interrupts(struct skd_device *skdev)
2063{
2064 u32 val;
2065
2066 /* unmask interrupts first */
2067 val = FIT_ISH_FW_STATE_CHANGE +
2068 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
2069
2070 /* Note that the compliment of mask is written. A 1-bit means
2071 * disable, a 0 means enable. */
2072 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002073 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002074
2075 val = SKD_READL(skdev, FIT_CONTROL);
2076 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002077 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002078 SKD_WRITEL(skdev, val, FIT_CONTROL);
2079}
2080
2081/*
2082 *****************************************************************************
2083 * START, STOP, RESTART, QUIESCE, UNQUIESCE
2084 *****************************************************************************
2085 */
2086
2087static void skd_soft_reset(struct skd_device *skdev)
2088{
2089 u32 val;
2090
2091 val = SKD_READL(skdev, FIT_CONTROL);
2092 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002093 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002094 SKD_WRITEL(skdev, val, FIT_CONTROL);
2095}
2096
2097static void skd_start_device(struct skd_device *skdev)
2098{
2099 unsigned long flags;
2100 u32 sense;
2101 u32 state;
2102
2103 spin_lock_irqsave(&skdev->lock, flags);
2104
2105 /* ack all ghost interrupts */
2106 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2107
2108 sense = SKD_READL(skdev, FIT_STATUS);
2109
Bart Van Asschef98806d2017-08-17 13:12:58 -07002110 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002111
2112 state = sense & FIT_SR_DRIVE_STATE_MASK;
2113 skdev->drive_state = state;
2114 skdev->last_mtd = 0;
2115
2116 skdev->state = SKD_DRVR_STATE_STARTING;
2117 skdev->timer_countdown = SKD_STARTING_TIMO;
2118
2119 skd_enable_interrupts(skdev);
2120
2121 switch (skdev->drive_state) {
2122 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002123 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002124 break;
2125
2126 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002127 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002128 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2129 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2130 break;
2131
2132 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002133 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002134 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2135 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2136 break;
2137
2138 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002139 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002140 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2141 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2142 break;
2143
2144 case FIT_SR_DRIVE_INIT:
2145 case FIT_SR_DRIVE_ONLINE:
2146 skd_soft_reset(skdev);
2147 break;
2148
2149 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002150 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002151 skdev->state = SKD_DRVR_STATE_BUSY;
2152 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2153 break;
2154
2155 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002156 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002157 break;
2158
2159 case FIT_SR_DRIVE_FAULT:
2160 /* Fault state is bad...soft reset won't do it...
2161 * Hard reset, maybe, but does it work on device?
2162 * For now, just fault so the system doesn't hang.
2163 */
2164 skd_drive_fault(skdev);
2165 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002166 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002167 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002168 skdev->gendisk_on = -1;
2169 wake_up_interruptible(&skdev->waitq);
2170 break;
2171
2172 case 0xFF:
2173 /* Most likely the device isn't there or isn't responding
2174 * to the BAR1 addresses. */
2175 skd_drive_disappeared(skdev);
2176 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002177 dev_dbg(&skdev->pdev->dev,
2178 "starting queue to error-out reqs\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002179 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002180 skdev->gendisk_on = -1;
2181 wake_up_interruptible(&skdev->waitq);
2182 break;
2183
2184 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002185 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
2186 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002187 break;
2188 }
2189
2190 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002191 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002192
2193 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002194 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002195
2196 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002197 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002198
2199 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002200 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002201
2202 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002203 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002204
2205 spin_unlock_irqrestore(&skdev->lock, flags);
2206}
2207
2208static void skd_stop_device(struct skd_device *skdev)
2209{
2210 unsigned long flags;
2211 struct skd_special_context *skspcl = &skdev->internal_skspcl;
2212 u32 dev_state;
2213 int i;
2214
2215 spin_lock_irqsave(&skdev->lock, flags);
2216
2217 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002218 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002219 goto stop_out;
2220 }
2221
2222 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002223 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002224 goto stop_out;
2225 }
2226
2227 skdev->state = SKD_DRVR_STATE_SYNCING;
2228 skdev->sync_done = 0;
2229
2230 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
2231
2232 spin_unlock_irqrestore(&skdev->lock, flags);
2233
2234 wait_event_interruptible_timeout(skdev->waitq,
2235 (skdev->sync_done), (10 * HZ));
2236
2237 spin_lock_irqsave(&skdev->lock, flags);
2238
2239 switch (skdev->sync_done) {
2240 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002241 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002242 break;
2243 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002244 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002245 break;
2246 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002247 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002248 }
2249
2250stop_out:
2251 skdev->state = SKD_DRVR_STATE_STOPPING;
2252 spin_unlock_irqrestore(&skdev->lock, flags);
2253
2254 skd_kill_timer(skdev);
2255
2256 spin_lock_irqsave(&skdev->lock, flags);
2257 skd_disable_interrupts(skdev);
2258
2259 /* ensure all ints on device are cleared */
2260 /* soft reset the device to unload with a clean slate */
2261 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2262 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
2263
2264 spin_unlock_irqrestore(&skdev->lock, flags);
2265
2266 /* poll every 100ms, 1 second timeout */
2267 for (i = 0; i < 10; i++) {
2268 dev_state =
2269 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
2270 if (dev_state == FIT_SR_DRIVE_INIT)
2271 break;
2272 set_current_state(TASK_INTERRUPTIBLE);
2273 schedule_timeout(msecs_to_jiffies(100));
2274 }
2275
2276 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07002277 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
2278 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002279}
2280
2281/* assume spinlock is held */
2282static void skd_restart_device(struct skd_device *skdev)
2283{
2284 u32 state;
2285
2286 /* ack all ghost interrupts */
2287 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2288
2289 state = SKD_READL(skdev, FIT_STATUS);
2290
Bart Van Asschef98806d2017-08-17 13:12:58 -07002291 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002292
2293 state &= FIT_SR_DRIVE_STATE_MASK;
2294 skdev->drive_state = state;
2295 skdev->last_mtd = 0;
2296
2297 skdev->state = SKD_DRVR_STATE_RESTARTING;
2298 skdev->timer_countdown = SKD_RESTARTING_TIMO;
2299
2300 skd_soft_reset(skdev);
2301}
2302
2303/* assume spinlock is held */
2304static int skd_quiesce_dev(struct skd_device *skdev)
2305{
2306 int rc = 0;
2307
2308 switch (skdev->state) {
2309 case SKD_DRVR_STATE_BUSY:
2310 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002311 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002312 blk_mq_stop_hw_queues(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002313 break;
2314 case SKD_DRVR_STATE_ONLINE:
2315 case SKD_DRVR_STATE_STOPPING:
2316 case SKD_DRVR_STATE_SYNCING:
2317 case SKD_DRVR_STATE_PAUSING:
2318 case SKD_DRVR_STATE_PAUSED:
2319 case SKD_DRVR_STATE_STARTING:
2320 case SKD_DRVR_STATE_RESTARTING:
2321 case SKD_DRVR_STATE_RESUMING:
2322 default:
2323 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002324 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
2325 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002326 }
2327 return rc;
2328}
2329
2330/* assume spinlock is held */
2331static int skd_unquiesce_dev(struct skd_device *skdev)
2332{
2333 int prev_driver_state = skdev->state;
2334
2335 skd_log_skdev(skdev, "unquiesce");
2336 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002337 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002338 return 0;
2339 }
2340 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
2341 /*
2342 * If there has been an state change to other than
2343 * ONLINE, we will rely on controller state change
2344 * to come back online and restart the queue.
2345 * The BUSY state means that driver is ready to
2346 * continue normal processing but waiting for controller
2347 * to become available.
2348 */
2349 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002350 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002351 return 0;
2352 }
2353
2354 /*
2355 * Drive has just come online, driver is either in startup,
2356 * paused performing a task, or bust waiting for hardware.
2357 */
2358 switch (skdev->state) {
2359 case SKD_DRVR_STATE_PAUSED:
2360 case SKD_DRVR_STATE_BUSY:
2361 case SKD_DRVR_STATE_BUSY_IMMINENT:
2362 case SKD_DRVR_STATE_BUSY_ERASE:
2363 case SKD_DRVR_STATE_STARTING:
2364 case SKD_DRVR_STATE_RESTARTING:
2365 case SKD_DRVR_STATE_FAULT:
2366 case SKD_DRVR_STATE_IDLE:
2367 case SKD_DRVR_STATE_LOAD:
2368 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002369 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2370 skd_skdev_state_to_str(prev_driver_state),
2371 prev_driver_state, skd_skdev_state_to_str(skdev->state),
2372 skdev->state);
2373 dev_dbg(&skdev->pdev->dev,
2374 "**** device ONLINE...starting block queue\n");
2375 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2376 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002377 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002378 skdev->gendisk_on = 1;
2379 wake_up_interruptible(&skdev->waitq);
2380 break;
2381
2382 case SKD_DRVR_STATE_DISAPPEARED:
2383 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002384 dev_dbg(&skdev->pdev->dev,
2385 "**** driver state %d, not implemented\n",
2386 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002387 return -EBUSY;
2388 }
2389 return 0;
2390}
2391
2392/*
2393 *****************************************************************************
2394 * PCIe MSI/MSI-X INTERRUPT HANDLERS
2395 *****************************************************************************
2396 */
2397
2398static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
2399{
2400 struct skd_device *skdev = skd_host_data;
2401 unsigned long flags;
2402
2403 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002404 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2405 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2406 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
2407 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002408 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
2409 spin_unlock_irqrestore(&skdev->lock, flags);
2410 return IRQ_HANDLED;
2411}
2412
2413static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
2414{
2415 struct skd_device *skdev = skd_host_data;
2416 unsigned long flags;
2417
2418 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002419 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2420 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002421 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
2422 skd_isr_fwstate(skdev);
2423 spin_unlock_irqrestore(&skdev->lock, flags);
2424 return IRQ_HANDLED;
2425}
2426
2427static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
2428{
2429 struct skd_device *skdev = skd_host_data;
2430 unsigned long flags;
2431 int flush_enqueued = 0;
2432 int deferred;
2433
2434 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002435 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2436 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002437 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
2438 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
2439 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002440 if (flush_enqueued)
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002441 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002442
2443 if (deferred)
2444 schedule_work(&skdev->completion_worker);
2445 else if (!flush_enqueued)
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002446 schedule_work(&skdev->start_queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002447
2448 spin_unlock_irqrestore(&skdev->lock, flags);
2449
2450 return IRQ_HANDLED;
2451}
2452
2453static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
2454{
2455 struct skd_device *skdev = skd_host_data;
2456 unsigned long flags;
2457
2458 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002459 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2460 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002461 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
2462 skd_isr_msg_from_dev(skdev);
2463 spin_unlock_irqrestore(&skdev->lock, flags);
2464 return IRQ_HANDLED;
2465}
2466
2467static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
2468{
2469 struct skd_device *skdev = skd_host_data;
2470 unsigned long flags;
2471
2472 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002473 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2474 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002475 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
2476 spin_unlock_irqrestore(&skdev->lock, flags);
2477 return IRQ_HANDLED;
2478}
2479
2480/*
2481 *****************************************************************************
2482 * PCIe MSI/MSI-X SETUP
2483 *****************************************************************************
2484 */
2485
2486struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002487 char isr_name[30];
2488};
2489
2490struct skd_init_msix_entry {
2491 const char *name;
2492 irq_handler_t handler;
2493};
2494
2495#define SKD_MAX_MSIX_COUNT 13
2496#define SKD_MIN_MSIX_COUNT 7
2497#define SKD_BASE_MSIX_IRQ 4
2498
2499static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
2500 { "(DMA 0)", skd_reserved_isr },
2501 { "(DMA 1)", skd_reserved_isr },
2502 { "(DMA 2)", skd_reserved_isr },
2503 { "(DMA 3)", skd_reserved_isr },
2504 { "(State Change)", skd_statec_isr },
2505 { "(COMPL_Q)", skd_comp_q },
2506 { "(MSG)", skd_msg_isr },
2507 { "(Reserved)", skd_reserved_isr },
2508 { "(Reserved)", skd_reserved_isr },
2509 { "(Queue Full 0)", skd_qfull_isr },
2510 { "(Queue Full 1)", skd_qfull_isr },
2511 { "(Queue Full 2)", skd_qfull_isr },
2512 { "(Queue Full 3)", skd_qfull_isr },
2513};
2514
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002515static int skd_acquire_msix(struct skd_device *skdev)
2516{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01002517 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01002518 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002519
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002520 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
2521 PCI_IRQ_MSIX);
2522 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002523 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002524 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002525 }
Alexander Gordeev46817762014-02-19 09:58:19 +01002526
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002527 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
2528 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002529 if (!skdev->msix_entries) {
2530 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002531 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002532 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002533 }
2534
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002535 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002536 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2537 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
2538
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002539 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
2540 "%s%d-msix %s", DRV_NAME, skdev->devno,
2541 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002542
2543 rc = devm_request_irq(&skdev->pdev->dev,
2544 pci_irq_vector(skdev->pdev, i),
2545 msix_entries[i].handler, 0,
2546 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002547 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002548 dev_err(&skdev->pdev->dev,
2549 "Unable to register(%d) MSI-X handler %d: %s\n",
2550 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002551 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002552 }
2553 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002554
Bart Van Asschef98806d2017-08-17 13:12:58 -07002555 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
2556 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002557 return 0;
2558
2559msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002560 while (--i >= 0)
2561 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002562out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002563 kfree(skdev->msix_entries);
2564 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002565 return rc;
2566}
2567
2568static int skd_acquire_irq(struct skd_device *skdev)
2569{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002570 struct pci_dev *pdev = skdev->pdev;
2571 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002572 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002573
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002574 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002575 rc = skd_acquire_msix(skdev);
2576 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002577 return 0;
2578
Bart Van Asschef98806d2017-08-17 13:12:58 -07002579 dev_err(&skdev->pdev->dev,
2580 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002581 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002582
2583 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
2584 skdev->devno);
2585
2586 if (skd_isr_type != SKD_IRQ_LEGACY)
2587 irq_flag |= PCI_IRQ_MSI;
2588 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
2589 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002590 dev_err(&skdev->pdev->dev,
2591 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002592 return rc;
2593 }
2594
2595 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
2596 pdev->msi_enabled ? 0 : IRQF_SHARED,
2597 skdev->isr_name, skdev);
2598 if (rc) {
2599 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002600 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
2601 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002602 return rc;
2603 }
2604
2605 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002606}
2607
2608static void skd_release_irq(struct skd_device *skdev)
2609{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002610 struct pci_dev *pdev = skdev->pdev;
2611
2612 if (skdev->msix_entries) {
2613 int i;
2614
2615 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2616 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
2617 skdev);
2618 }
2619
2620 kfree(skdev->msix_entries);
2621 skdev->msix_entries = NULL;
2622 } else {
2623 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002624 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002625
2626 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002627}
2628
2629/*
2630 *****************************************************************************
2631 * CONSTRUCT
2632 *****************************************************************************
2633 */
2634
Bart Van Asschea3db1022017-08-17 13:13:35 -07002635static void *skd_alloc_dma(struct skd_device *skdev, struct kmem_cache *s,
2636 dma_addr_t *dma_handle, gfp_t gfp,
2637 enum dma_data_direction dir)
2638{
2639 struct device *dev = &skdev->pdev->dev;
2640 void *buf;
2641
2642 buf = kmem_cache_alloc(s, gfp);
2643 if (!buf)
2644 return NULL;
2645 *dma_handle = dma_map_single(dev, buf, s->size, dir);
2646 if (dma_mapping_error(dev, *dma_handle)) {
2647 kfree(buf);
2648 buf = NULL;
2649 }
2650 return buf;
2651}
2652
2653static void skd_free_dma(struct skd_device *skdev, struct kmem_cache *s,
2654 void *vaddr, dma_addr_t dma_handle,
2655 enum dma_data_direction dir)
2656{
2657 if (!vaddr)
2658 return;
2659
2660 dma_unmap_single(&skdev->pdev->dev, dma_handle, s->size, dir);
2661 kmem_cache_free(s, vaddr);
2662}
2663
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002664static int skd_cons_skcomp(struct skd_device *skdev)
2665{
2666 int rc = 0;
2667 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002668
Bart Van Asschef98806d2017-08-17 13:12:58 -07002669 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002670 "comp pci_alloc, total bytes %zd entries %d\n",
2671 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002672
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002673 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07002674 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002675
2676 if (skcomp == NULL) {
2677 rc = -ENOMEM;
2678 goto err_out;
2679 }
2680
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002681 skdev->skcomp_table = skcomp;
2682 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
2683 sizeof(*skcomp) *
2684 SKD_N_COMPLETION_ENTRY);
2685
2686err_out:
2687 return rc;
2688}
2689
2690static int skd_cons_skmsg(struct skd_device *skdev)
2691{
2692 int rc = 0;
2693 u32 i;
2694
Bart Van Asschef98806d2017-08-17 13:12:58 -07002695 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002696 "skmsg_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002697 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
2698 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002699
Bart Van Assche01433d02017-08-17 13:13:18 -07002700 skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context,
2701 sizeof(struct skd_fitmsg_context),
2702 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002703 if (skdev->skmsg_table == NULL) {
2704 rc = -ENOMEM;
2705 goto err_out;
2706 }
2707
2708 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2709 struct skd_fitmsg_context *skmsg;
2710
2711 skmsg = &skdev->skmsg_table[i];
2712
2713 skmsg->id = i + SKD_ID_FIT_MSG;
2714
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002715 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07002716 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002717 &skmsg->mb_dma_address);
2718
2719 if (skmsg->msg_buf == NULL) {
2720 rc = -ENOMEM;
2721 goto err_out;
2722 }
2723
Bart Van Assche6507f432017-08-17 13:13:06 -07002724 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
2725 (FIT_QCMD_ALIGN - 1),
2726 "not aligned: msg_buf %p mb_dma_address %#llx\n",
2727 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002728 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002729 }
2730
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002731err_out:
2732 return rc;
2733}
2734
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002735static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
2736 u32 n_sg,
2737 dma_addr_t *ret_dma_addr)
2738{
2739 struct fit_sg_descriptor *sg_list;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002740
Bart Van Asschea3db1022017-08-17 13:13:35 -07002741 sg_list = skd_alloc_dma(skdev, skdev->sglist_cache, ret_dma_addr,
2742 GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE);
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002743
2744 if (sg_list != NULL) {
2745 uint64_t dma_address = *ret_dma_addr;
2746 u32 i;
2747
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002748 for (i = 0; i < n_sg - 1; i++) {
2749 uint64_t ndp_off;
2750 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
2751
2752 sg_list[i].next_desc_ptr = dma_address + ndp_off;
2753 }
2754 sg_list[i].next_desc_ptr = 0LL;
2755 }
2756
2757 return sg_list;
2758}
2759
Bart Van Assche5d003242017-08-17 13:13:30 -07002760static void skd_free_sg_list(struct skd_device *skdev,
Bart Van Asschea3db1022017-08-17 13:13:35 -07002761 struct fit_sg_descriptor *sg_list,
Bart Van Assche5d003242017-08-17 13:13:30 -07002762 dma_addr_t dma_addr)
2763{
Bart Van Assche5d003242017-08-17 13:13:30 -07002764 if (WARN_ON_ONCE(!sg_list))
2765 return;
2766
Bart Van Asschea3db1022017-08-17 13:13:35 -07002767 skd_free_dma(skdev, skdev->sglist_cache, sg_list, dma_addr,
2768 DMA_TO_DEVICE);
Bart Van Assche5d003242017-08-17 13:13:30 -07002769}
2770
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002771static int skd_init_request(struct blk_mq_tag_set *set, struct request *rq,
2772 unsigned int hctx_idx, unsigned int numa_node)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002773{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002774 struct skd_device *skdev = set->driver_data;
Bart Van Asschee7278a82017-08-17 13:13:31 -07002775 struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002776
Bart Van Asschee7278a82017-08-17 13:13:31 -07002777 skreq->state = SKD_REQ_STATE_IDLE;
2778 skreq->sg = (void *)(skreq + 1);
2779 sg_init_table(skreq->sg, skd_sgs_per_request);
2780 skreq->sksg_list = skd_cons_sg_list(skdev, skd_sgs_per_request,
2781 &skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002782
Bart Van Asschee7278a82017-08-17 13:13:31 -07002783 return skreq->sksg_list ? 0 : -ENOMEM;
2784}
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002785
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002786static void skd_exit_request(struct blk_mq_tag_set *set, struct request *rq,
2787 unsigned int hctx_idx)
Bart Van Asschee7278a82017-08-17 13:13:31 -07002788{
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002789 struct skd_device *skdev = set->driver_data;
Bart Van Asschee7278a82017-08-17 13:13:31 -07002790 struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002791
Bart Van Asschea3db1022017-08-17 13:13:35 -07002792 skd_free_sg_list(skdev, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002793}
2794
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002795static int skd_cons_sksb(struct skd_device *skdev)
2796{
2797 int rc = 0;
2798 struct skd_special_context *skspcl;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002799
2800 skspcl = &skdev->internal_skspcl;
2801
2802 skspcl->req.id = 0 + SKD_ID_INTERNAL;
2803 skspcl->req.state = SKD_REQ_STATE_IDLE;
2804
Bart Van Asschea3db1022017-08-17 13:13:35 -07002805 skspcl->data_buf = skd_alloc_dma(skdev, skdev->databuf_cache,
2806 &skspcl->db_dma_address,
2807 GFP_DMA | __GFP_ZERO,
2808 DMA_BIDIRECTIONAL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002809 if (skspcl->data_buf == NULL) {
2810 rc = -ENOMEM;
2811 goto err_out;
2812 }
2813
Bart Van Asschea3db1022017-08-17 13:13:35 -07002814 skspcl->msg_buf = skd_alloc_dma(skdev, skdev->msgbuf_cache,
2815 &skspcl->mb_dma_address,
2816 GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002817 if (skspcl->msg_buf == NULL) {
2818 rc = -ENOMEM;
2819 goto err_out;
2820 }
2821
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002822 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
2823 &skspcl->req.sksg_dma_address);
2824 if (skspcl->req.sksg_list == NULL) {
2825 rc = -ENOMEM;
2826 goto err_out;
2827 }
2828
2829 if (!skd_format_internal_skspcl(skdev)) {
2830 rc = -EINVAL;
2831 goto err_out;
2832 }
2833
2834err_out:
2835 return rc;
2836}
2837
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002838static const struct blk_mq_ops skd_mq_ops = {
2839 .queue_rq = skd_mq_queue_rq,
2840 .init_request = skd_init_request,
2841 .exit_request = skd_exit_request,
2842};
2843
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002844static int skd_cons_disk(struct skd_device *skdev)
2845{
2846 int rc = 0;
2847 struct gendisk *disk;
2848 struct request_queue *q;
2849 unsigned long flags;
2850
2851 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
2852 if (!disk) {
2853 rc = -ENOMEM;
2854 goto err_out;
2855 }
2856
2857 skdev->disk = disk;
2858 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
2859
2860 disk->major = skdev->major;
2861 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
2862 disk->fops = &skd_blockdev_ops;
2863 disk->private_data = skdev;
2864
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002865 q = NULL;
2866 memset(&skdev->tag_set, 0, sizeof(skdev->tag_set));
2867 skdev->tag_set.ops = &skd_mq_ops;
2868 skdev->tag_set.nr_hw_queues = 1;
2869 skdev->tag_set.queue_depth = skd_max_queue_depth;
2870 skdev->tag_set.cmd_size = sizeof(struct skd_request_context) +
2871 skdev->sgs_per_request * sizeof(struct scatterlist);
2872 skdev->tag_set.numa_node = NUMA_NO_NODE;
2873 skdev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
2874 BLK_MQ_F_SG_MERGE |
2875 BLK_ALLOC_POLICY_TO_MQ_FLAG(BLK_TAG_ALLOC_FIFO);
2876 skdev->tag_set.driver_data = skdev;
2877 if (blk_mq_alloc_tag_set(&skdev->tag_set) >= 0) {
2878 q = blk_mq_init_queue(&skdev->tag_set);
2879 if (!q)
2880 blk_mq_free_tag_set(&skdev->tag_set);
2881 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002882 if (!q) {
2883 rc = -ENOMEM;
2884 goto err_out;
2885 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02002886 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Bart Van Asschee7278a82017-08-17 13:13:31 -07002887 q->queuedata = skdev;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002888 q->nr_requests = skd_max_queue_depth / 2;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002889
2890 skdev->queue = q;
2891 disk->queue = q;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002892
Jens Axboe6975f732016-03-30 10:11:42 -06002893 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002894 blk_queue_max_segments(q, skdev->sgs_per_request);
2895 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
2896
Bart Van Asschea5c5b392017-08-17 13:12:53 -07002897 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002898 blk_queue_io_opt(q, 8192);
2899
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002900 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06002901 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002902
Bart Van Asschea74d5b72017-08-17 13:13:33 -07002903 blk_queue_rq_timeout(q, 8 * HZ);
2904 blk_queue_rq_timed_out(q, skd_timed_out);
2905 blk_queue_softirq_done(q, skd_softirq_done);
2906
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002907 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002908 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002909 blk_mq_stop_hw_queues(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002910 spin_unlock_irqrestore(&skdev->lock, flags);
2911
2912err_out:
2913 return rc;
2914}
2915
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002916#define SKD_N_DEV_TABLE 16u
2917static u32 skd_next_devno;
2918
2919static struct skd_device *skd_construct(struct pci_dev *pdev)
2920{
2921 struct skd_device *skdev;
2922 int blk_major = skd_major;
Bart Van Asschea3db1022017-08-17 13:13:35 -07002923 size_t size;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002924 int rc;
2925
2926 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
2927
2928 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002929 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002930 return NULL;
2931 }
2932
2933 skdev->state = SKD_DRVR_STATE_LOAD;
2934 skdev->pdev = pdev;
2935 skdev->devno = skd_next_devno++;
2936 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002937 skdev->dev_max_queue_depth = 0;
2938
2939 skdev->num_req_context = skd_max_queue_depth;
2940 skdev->num_fitmsg_context = skd_max_queue_depth;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002941 skdev->cur_max_queue_depth = 1;
2942 skdev->queue_low_water_mark = 1;
2943 skdev->proto_ver = 99;
2944 skdev->sgs_per_request = skd_sgs_per_request;
2945 skdev->dbg_level = skd_dbg_level;
2946
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002947 spin_lock_init(&skdev->lock);
2948
Bart Van Asscheca33dd92017-08-17 13:13:32 -07002949 INIT_WORK(&skdev->start_queue, skd_start_queue);
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002950 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
2951
Bart Van Asschea3db1022017-08-17 13:13:35 -07002952 size = max(SKD_N_FITMSG_BYTES, SKD_N_SPECIAL_FITMSG_BYTES);
2953 skdev->msgbuf_cache = kmem_cache_create("skd-msgbuf", size, 0,
2954 SLAB_HWCACHE_ALIGN, NULL);
2955 if (!skdev->msgbuf_cache)
2956 goto err_out;
2957 WARN_ONCE(kmem_cache_size(skdev->msgbuf_cache) < size,
2958 "skd-msgbuf: %d < %zd\n",
2959 kmem_cache_size(skdev->msgbuf_cache), size);
2960 size = skd_sgs_per_request * sizeof(struct fit_sg_descriptor);
2961 skdev->sglist_cache = kmem_cache_create("skd-sglist", size, 0,
2962 SLAB_HWCACHE_ALIGN, NULL);
2963 if (!skdev->sglist_cache)
2964 goto err_out;
2965 WARN_ONCE(kmem_cache_size(skdev->sglist_cache) < size,
2966 "skd-sglist: %d < %zd\n",
2967 kmem_cache_size(skdev->sglist_cache), size);
2968 size = SKD_N_INTERNAL_BYTES;
2969 skdev->databuf_cache = kmem_cache_create("skd-databuf", size, 0,
2970 SLAB_HWCACHE_ALIGN, NULL);
2971 if (!skdev->databuf_cache)
2972 goto err_out;
2973 WARN_ONCE(kmem_cache_size(skdev->databuf_cache) < size,
2974 "skd-databuf: %d < %zd\n",
2975 kmem_cache_size(skdev->databuf_cache), size);
2976
Bart Van Asschef98806d2017-08-17 13:12:58 -07002977 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002978 rc = skd_cons_skcomp(skdev);
2979 if (rc < 0)
2980 goto err_out;
2981
Bart Van Asschef98806d2017-08-17 13:12:58 -07002982 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002983 rc = skd_cons_skmsg(skdev);
2984 if (rc < 0)
2985 goto err_out;
2986
Bart Van Asschef98806d2017-08-17 13:12:58 -07002987 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002988 rc = skd_cons_sksb(skdev);
2989 if (rc < 0)
2990 goto err_out;
2991
Bart Van Asschef98806d2017-08-17 13:12:58 -07002992 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002993 rc = skd_cons_disk(skdev);
2994 if (rc < 0)
2995 goto err_out;
2996
Bart Van Asschef98806d2017-08-17 13:12:58 -07002997 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002998 return skdev;
2999
3000err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003001 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003002 skd_destruct(skdev);
3003 return NULL;
3004}
3005
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003006/*
3007 *****************************************************************************
3008 * DESTRUCT (FREE)
3009 *****************************************************************************
3010 */
3011
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003012static void skd_free_skcomp(struct skd_device *skdev)
3013{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07003014 if (skdev->skcomp_table)
3015 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003016 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003017
3018 skdev->skcomp_table = NULL;
3019 skdev->cq_dma_address = 0;
3020}
3021
3022static void skd_free_skmsg(struct skd_device *skdev)
3023{
3024 u32 i;
3025
3026 if (skdev->skmsg_table == NULL)
3027 return;
3028
3029 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3030 struct skd_fitmsg_context *skmsg;
3031
3032 skmsg = &skdev->skmsg_table[i];
3033
3034 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003035 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
3036 skmsg->msg_buf,
3037 skmsg->mb_dma_address);
3038 }
3039 skmsg->msg_buf = NULL;
3040 skmsg->mb_dma_address = 0;
3041 }
3042
3043 kfree(skdev->skmsg_table);
3044 skdev->skmsg_table = NULL;
3045}
3046
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003047static void skd_free_sksb(struct skd_device *skdev)
3048{
Bart Van Asschea3db1022017-08-17 13:13:35 -07003049 struct skd_special_context *skspcl = &skdev->internal_skspcl;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003050
Bart Van Asschea3db1022017-08-17 13:13:35 -07003051 skd_free_dma(skdev, skdev->databuf_cache, skspcl->data_buf,
3052 skspcl->db_dma_address, DMA_BIDIRECTIONAL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003053
3054 skspcl->data_buf = NULL;
3055 skspcl->db_dma_address = 0;
3056
Bart Van Asschea3db1022017-08-17 13:13:35 -07003057 skd_free_dma(skdev, skdev->msgbuf_cache, skspcl->msg_buf,
3058 skspcl->mb_dma_address, DMA_TO_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003059
3060 skspcl->msg_buf = NULL;
3061 skspcl->mb_dma_address = 0;
3062
Bart Van Asschea3db1022017-08-17 13:13:35 -07003063 skd_free_sg_list(skdev, skspcl->req.sksg_list,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003064 skspcl->req.sksg_dma_address);
3065
3066 skspcl->req.sksg_list = NULL;
3067 skspcl->req.sksg_dma_address = 0;
3068}
3069
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003070static void skd_free_disk(struct skd_device *skdev)
3071{
3072 struct gendisk *disk = skdev->disk;
3073
Bart Van Assche7277cc62017-08-17 13:12:45 -07003074 if (disk && (disk->flags & GENHD_FL_UP))
3075 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003076
Bart Van Assche7277cc62017-08-17 13:12:45 -07003077 if (skdev->queue) {
3078 blk_cleanup_queue(skdev->queue);
3079 skdev->queue = NULL;
3080 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003081 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07003082
Bart Van Asscheca33dd92017-08-17 13:13:32 -07003083 if (skdev->tag_set.tags)
3084 blk_mq_free_tag_set(&skdev->tag_set);
3085
Bart Van Assche7277cc62017-08-17 13:12:45 -07003086 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003087 skdev->disk = NULL;
3088}
3089
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003090static void skd_destruct(struct skd_device *skdev)
3091{
3092 if (skdev == NULL)
3093 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003094
Bart Van Asscheca33dd92017-08-17 13:13:32 -07003095 cancel_work_sync(&skdev->start_queue);
3096
Bart Van Asschef98806d2017-08-17 13:12:58 -07003097 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003098 skd_free_disk(skdev);
3099
Bart Van Asschef98806d2017-08-17 13:12:58 -07003100 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003101 skd_free_sksb(skdev);
3102
Bart Van Asschef98806d2017-08-17 13:12:58 -07003103 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003104 skd_free_skmsg(skdev);
3105
Bart Van Asschef98806d2017-08-17 13:12:58 -07003106 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003107 skd_free_skcomp(skdev);
3108
Bart Van Asschea3db1022017-08-17 13:13:35 -07003109 kmem_cache_destroy(skdev->databuf_cache);
3110 kmem_cache_destroy(skdev->sglist_cache);
3111 kmem_cache_destroy(skdev->msgbuf_cache);
3112
Bart Van Asschef98806d2017-08-17 13:12:58 -07003113 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003114 kfree(skdev);
3115}
3116
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003117/*
3118 *****************************************************************************
3119 * BLOCK DEVICE (BDEV) GLUE
3120 *****************************************************************************
3121 */
3122
3123static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3124{
3125 struct skd_device *skdev;
3126 u64 capacity;
3127
3128 skdev = bdev->bd_disk->private_data;
3129
Bart Van Asschef98806d2017-08-17 13:12:58 -07003130 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
3131 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003132
3133 if (skdev->read_cap_is_valid) {
3134 capacity = get_capacity(skdev->disk);
3135 geo->heads = 64;
3136 geo->sectors = 255;
3137 geo->cylinders = (capacity) / (255 * 64);
3138
3139 return 0;
3140 }
3141 return -EIO;
3142}
3143
Dan Williams0d52c7562016-06-15 19:44:20 -07003144static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003145{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003146 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07003147 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003148 return 0;
3149}
3150
3151static const struct block_device_operations skd_blockdev_ops = {
3152 .owner = THIS_MODULE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003153 .getgeo = skd_bdev_getgeo,
3154};
3155
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003156/*
3157 *****************************************************************************
3158 * PCIe DRIVER GLUE
3159 *****************************************************************************
3160 */
3161
Benoit Taine9baa3c32014-08-08 15:56:03 +02003162static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003163 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
3164 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
3165 { 0 } /* terminate list */
3166};
3167
3168MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
3169
3170static char *skd_pci_info(struct skd_device *skdev, char *str)
3171{
3172 int pcie_reg;
3173
3174 strcpy(str, "PCIe (");
3175 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
3176
3177 if (pcie_reg) {
3178
3179 char lwstr[6];
3180 uint16_t pcie_lstat, lspeed, lwidth;
3181
3182 pcie_reg += 0x12;
3183 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
3184 lspeed = pcie_lstat & (0xF);
3185 lwidth = (pcie_lstat & 0x3F0) >> 4;
3186
3187 if (lspeed == 1)
3188 strcat(str, "2.5GT/s ");
3189 else if (lspeed == 2)
3190 strcat(str, "5.0GT/s ");
3191 else
3192 strcat(str, "<unknown> ");
3193 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
3194 strcat(str, lwstr);
3195 }
3196 return str;
3197}
3198
3199static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3200{
3201 int i;
3202 int rc = 0;
3203 char pci_str[32];
3204 struct skd_device *skdev;
3205
Bart Van Asschebb9f7dd2017-08-17 13:13:38 -07003206 dev_dbg(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
3207 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003208
3209 rc = pci_enable_device(pdev);
3210 if (rc)
3211 return rc;
3212 rc = pci_request_regions(pdev, DRV_NAME);
3213 if (rc)
3214 goto err_out;
3215 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3216 if (!rc) {
3217 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003218 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3219 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003220 }
3221 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003222 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003223 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003224 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003225 goto err_out_regions;
3226 }
3227 }
3228
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003229 if (!skd_major) {
3230 rc = register_blkdev(0, DRV_NAME);
3231 if (rc < 0)
3232 goto err_out_regions;
3233 BUG_ON(!rc);
3234 skd_major = rc;
3235 }
3236
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003237 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08003238 if (skdev == NULL) {
3239 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003240 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08003241 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003242
3243 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003244 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003245
3246 pci_set_master(pdev);
3247 rc = pci_enable_pcie_error_reporting(pdev);
3248 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003249 dev_err(&pdev->dev,
3250 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003251 skdev->pcie_error_reporting_is_enabled = 0;
3252 } else
3253 skdev->pcie_error_reporting_is_enabled = 1;
3254
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003255 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01003256
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003257 for (i = 0; i < SKD_MAX_BARS; i++) {
3258 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3259 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3260 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3261 skdev->mem_size[i]);
3262 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003263 dev_err(&pdev->dev,
3264 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003265 rc = -ENODEV;
3266 goto err_out_iounmap;
3267 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003268 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3269 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3270 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003271 }
3272
3273 rc = skd_acquire_irq(skdev);
3274 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003275 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003276 goto err_out_iounmap;
3277 }
3278
3279 rc = skd_start_timer(skdev);
3280 if (rc)
3281 goto err_out_timer;
3282
3283 init_waitqueue_head(&skdev->waitq);
3284
3285 skd_start_device(skdev);
3286
3287 rc = wait_event_interruptible_timeout(skdev->waitq,
3288 (skdev->gendisk_on),
3289 (SKD_START_WAIT_SECONDS * HZ));
3290 if (skdev->gendisk_on > 0) {
3291 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07003292 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003293 rc = 0;
3294 } else {
3295 /* we timed out, something is wrong with the device,
3296 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003297 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
3298 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003299 /* in case of no error; we timeout with ENXIO */
3300 if (!rc)
3301 rc = -ENXIO;
3302 goto err_out_timer;
3303 }
3304
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003305 return rc;
3306
3307err_out_timer:
3308 skd_stop_device(skdev);
3309 skd_release_irq(skdev);
3310
3311err_out_iounmap:
3312 for (i = 0; i < SKD_MAX_BARS; i++)
3313 if (skdev->mem_map[i])
3314 iounmap(skdev->mem_map[i]);
3315
3316 if (skdev->pcie_error_reporting_is_enabled)
3317 pci_disable_pcie_error_reporting(pdev);
3318
3319 skd_destruct(skdev);
3320
3321err_out_regions:
3322 pci_release_regions(pdev);
3323
3324err_out:
3325 pci_disable_device(pdev);
3326 pci_set_drvdata(pdev, NULL);
3327 return rc;
3328}
3329
3330static void skd_pci_remove(struct pci_dev *pdev)
3331{
3332 int i;
3333 struct skd_device *skdev;
3334
3335 skdev = pci_get_drvdata(pdev);
3336 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003337 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003338 return;
3339 }
3340 skd_stop_device(skdev);
3341 skd_release_irq(skdev);
3342
3343 for (i = 0; i < SKD_MAX_BARS; i++)
3344 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003345 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003346
3347 if (skdev->pcie_error_reporting_is_enabled)
3348 pci_disable_pcie_error_reporting(pdev);
3349
3350 skd_destruct(skdev);
3351
3352 pci_release_regions(pdev);
3353 pci_disable_device(pdev);
3354 pci_set_drvdata(pdev, NULL);
3355
3356 return;
3357}
3358
3359static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3360{
3361 int i;
3362 struct skd_device *skdev;
3363
3364 skdev = pci_get_drvdata(pdev);
3365 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003366 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003367 return -EIO;
3368 }
3369
3370 skd_stop_device(skdev);
3371
3372 skd_release_irq(skdev);
3373
3374 for (i = 0; i < SKD_MAX_BARS; i++)
3375 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003376 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003377
3378 if (skdev->pcie_error_reporting_is_enabled)
3379 pci_disable_pcie_error_reporting(pdev);
3380
3381 pci_release_regions(pdev);
3382 pci_save_state(pdev);
3383 pci_disable_device(pdev);
3384 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3385 return 0;
3386}
3387
3388static int skd_pci_resume(struct pci_dev *pdev)
3389{
3390 int i;
3391 int rc = 0;
3392 struct skd_device *skdev;
3393
3394 skdev = pci_get_drvdata(pdev);
3395 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003396 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003397 return -1;
3398 }
3399
3400 pci_set_power_state(pdev, PCI_D0);
3401 pci_enable_wake(pdev, PCI_D0, 0);
3402 pci_restore_state(pdev);
3403
3404 rc = pci_enable_device(pdev);
3405 if (rc)
3406 return rc;
3407 rc = pci_request_regions(pdev, DRV_NAME);
3408 if (rc)
3409 goto err_out;
3410 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3411 if (!rc) {
3412 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3413
Bart Van Asschef98806d2017-08-17 13:12:58 -07003414 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3415 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003416 }
3417 } else {
3418 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3419 if (rc) {
3420
Bart Van Asschef98806d2017-08-17 13:12:58 -07003421 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003422 goto err_out_regions;
3423 }
3424 }
3425
3426 pci_set_master(pdev);
3427 rc = pci_enable_pcie_error_reporting(pdev);
3428 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003429 dev_err(&pdev->dev,
3430 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003431 skdev->pcie_error_reporting_is_enabled = 0;
3432 } else
3433 skdev->pcie_error_reporting_is_enabled = 1;
3434
3435 for (i = 0; i < SKD_MAX_BARS; i++) {
3436
3437 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3438 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3439 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3440 skdev->mem_size[i]);
3441 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003442 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003443 rc = -ENODEV;
3444 goto err_out_iounmap;
3445 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003446 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3447 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3448 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003449 }
3450 rc = skd_acquire_irq(skdev);
3451 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003452 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003453 goto err_out_iounmap;
3454 }
3455
3456 rc = skd_start_timer(skdev);
3457 if (rc)
3458 goto err_out_timer;
3459
3460 init_waitqueue_head(&skdev->waitq);
3461
3462 skd_start_device(skdev);
3463
3464 return rc;
3465
3466err_out_timer:
3467 skd_stop_device(skdev);
3468 skd_release_irq(skdev);
3469
3470err_out_iounmap:
3471 for (i = 0; i < SKD_MAX_BARS; i++)
3472 if (skdev->mem_map[i])
3473 iounmap(skdev->mem_map[i]);
3474
3475 if (skdev->pcie_error_reporting_is_enabled)
3476 pci_disable_pcie_error_reporting(pdev);
3477
3478err_out_regions:
3479 pci_release_regions(pdev);
3480
3481err_out:
3482 pci_disable_device(pdev);
3483 return rc;
3484}
3485
3486static void skd_pci_shutdown(struct pci_dev *pdev)
3487{
3488 struct skd_device *skdev;
3489
Bart Van Asschef98806d2017-08-17 13:12:58 -07003490 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003491
3492 skdev = pci_get_drvdata(pdev);
3493 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003494 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003495 return;
3496 }
3497
Bart Van Asschef98806d2017-08-17 13:12:58 -07003498 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003499 skd_stop_device(skdev);
3500}
3501
3502static struct pci_driver skd_driver = {
3503 .name = DRV_NAME,
3504 .id_table = skd_pci_tbl,
3505 .probe = skd_pci_probe,
3506 .remove = skd_pci_remove,
3507 .suspend = skd_pci_suspend,
3508 .resume = skd_pci_resume,
3509 .shutdown = skd_pci_shutdown,
3510};
3511
3512/*
3513 *****************************************************************************
3514 * LOGGING SUPPORT
3515 *****************************************************************************
3516 */
3517
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003518const char *skd_drive_state_to_str(int state)
3519{
3520 switch (state) {
3521 case FIT_SR_DRIVE_OFFLINE:
3522 return "OFFLINE";
3523 case FIT_SR_DRIVE_INIT:
3524 return "INIT";
3525 case FIT_SR_DRIVE_ONLINE:
3526 return "ONLINE";
3527 case FIT_SR_DRIVE_BUSY:
3528 return "BUSY";
3529 case FIT_SR_DRIVE_FAULT:
3530 return "FAULT";
3531 case FIT_SR_DRIVE_DEGRADED:
3532 return "DEGRADED";
3533 case FIT_SR_PCIE_LINK_DOWN:
3534 return "INK_DOWN";
3535 case FIT_SR_DRIVE_SOFT_RESET:
3536 return "SOFT_RESET";
3537 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3538 return "NEED_FW";
3539 case FIT_SR_DRIVE_INIT_FAULT:
3540 return "INIT_FAULT";
3541 case FIT_SR_DRIVE_BUSY_SANITIZE:
3542 return "BUSY_SANITIZE";
3543 case FIT_SR_DRIVE_BUSY_ERASE:
3544 return "BUSY_ERASE";
3545 case FIT_SR_DRIVE_FW_BOOTING:
3546 return "FW_BOOTING";
3547 default:
3548 return "???";
3549 }
3550}
3551
3552const char *skd_skdev_state_to_str(enum skd_drvr_state state)
3553{
3554 switch (state) {
3555 case SKD_DRVR_STATE_LOAD:
3556 return "LOAD";
3557 case SKD_DRVR_STATE_IDLE:
3558 return "IDLE";
3559 case SKD_DRVR_STATE_BUSY:
3560 return "BUSY";
3561 case SKD_DRVR_STATE_STARTING:
3562 return "STARTING";
3563 case SKD_DRVR_STATE_ONLINE:
3564 return "ONLINE";
3565 case SKD_DRVR_STATE_PAUSING:
3566 return "PAUSING";
3567 case SKD_DRVR_STATE_PAUSED:
3568 return "PAUSED";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003569 case SKD_DRVR_STATE_RESTARTING:
3570 return "RESTARTING";
3571 case SKD_DRVR_STATE_RESUMING:
3572 return "RESUMING";
3573 case SKD_DRVR_STATE_STOPPING:
3574 return "STOPPING";
3575 case SKD_DRVR_STATE_SYNCING:
3576 return "SYNCING";
3577 case SKD_DRVR_STATE_FAULT:
3578 return "FAULT";
3579 case SKD_DRVR_STATE_DISAPPEARED:
3580 return "DISAPPEARED";
3581 case SKD_DRVR_STATE_BUSY_ERASE:
3582 return "BUSY_ERASE";
3583 case SKD_DRVR_STATE_BUSY_SANITIZE:
3584 return "BUSY_SANITIZE";
3585 case SKD_DRVR_STATE_BUSY_IMMINENT:
3586 return "BUSY_IMMINENT";
3587 case SKD_DRVR_STATE_WAIT_BOOT:
3588 return "WAIT_BOOT";
3589
3590 default:
3591 return "???";
3592 }
3593}
3594
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05303595static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003596{
3597 switch (state) {
3598 case SKD_REQ_STATE_IDLE:
3599 return "IDLE";
3600 case SKD_REQ_STATE_SETUP:
3601 return "SETUP";
3602 case SKD_REQ_STATE_BUSY:
3603 return "BUSY";
3604 case SKD_REQ_STATE_COMPLETED:
3605 return "COMPLETED";
3606 case SKD_REQ_STATE_TIMEOUT:
3607 return "TIMEOUT";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003608 default:
3609 return "???";
3610 }
3611}
3612
3613static void skd_log_skdev(struct skd_device *skdev, const char *event)
3614{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003615 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
3616 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
3617 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3618 skd_skdev_state_to_str(skdev->state), skdev->state);
3619 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
Bart Van Assched4d0f5f2017-08-17 13:13:34 -07003620 skd_in_flight(skdev), skdev->cur_max_queue_depth,
Bart Van Asschef98806d2017-08-17 13:12:58 -07003621 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
Bart Van Asschea74d5b72017-08-17 13:13:33 -07003622 dev_dbg(&skdev->pdev->dev, " cycle=%d cycle_ix=%d\n",
3623 skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003624}
3625
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003626static void skd_log_skreq(struct skd_device *skdev,
3627 struct skd_request_context *skreq, const char *event)
3628{
Bart Van Asschee7278a82017-08-17 13:13:31 -07003629 struct request *req = blk_mq_rq_from_pdu(skreq);
3630 u32 lba = blk_rq_pos(req);
3631 u32 count = blk_rq_sectors(req);
3632
Bart Van Asschef98806d2017-08-17 13:12:58 -07003633 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
3634 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
3635 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
3636 skreq->fitmsg_id);
Bart Van Asschea74d5b72017-08-17 13:13:33 -07003637 dev_dbg(&skdev->pdev->dev, " sg_dir=%d n_sg=%d\n",
3638 skreq->data_dir, skreq->n_sg);
Bart Van Asscheca33dd92017-08-17 13:13:32 -07003639
Bart Van Asschee7278a82017-08-17 13:13:31 -07003640 dev_dbg(&skdev->pdev->dev,
3641 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba, lba,
3642 count, count, (int)rq_data_dir(req));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003643}
3644
3645/*
3646 *****************************************************************************
3647 * MODULE GLUE
3648 *****************************************************************************
3649 */
3650
3651static int __init skd_init(void)
3652{
Bart Van Assche16a70532017-08-17 13:13:08 -07003653 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
3654 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
3655 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
3656 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
3657 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07003658 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
3659 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
3660 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07003661
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003662 switch (skd_isr_type) {
3663 case SKD_IRQ_LEGACY:
3664 case SKD_IRQ_MSI:
3665 case SKD_IRQ_MSIX:
3666 break;
3667 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003668 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003669 skd_isr_type, SKD_IRQ_DEFAULT);
3670 skd_isr_type = SKD_IRQ_DEFAULT;
3671 }
3672
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003673 if (skd_max_queue_depth < 1 ||
3674 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
3675 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003676 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
3677 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
3678 }
3679
Bart Van Assche2da7b402017-08-17 13:13:01 -07003680 if (skd_max_req_per_msg < 1 ||
3681 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003682 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003683 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
3684 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
3685 }
3686
3687 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003688 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003689 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
3690 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
3691 }
3692
3693 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003694 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003695 skd_dbg_level, 0);
3696 skd_dbg_level = 0;
3697 }
3698
3699 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003700 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003701 skd_isr_comp_limit, 0);
3702 skd_isr_comp_limit = 0;
3703 }
3704
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003705 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003706}
3707
3708static void __exit skd_exit(void)
3709{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003710 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003711
3712 if (skd_major)
3713 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003714}
3715
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003716module_init(skd_init);
3717module_exit(skd_exit);