blob: 392c898d86e2b4ddde208cd795d5fd737f88ee33 [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>
19#include <linux/sched.h>
20#include <linux/interrupt.h>
21#include <linux/compiler.h>
22#include <linux/workqueue.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060023#include <linux/delay.h>
24#include <linux/time.h>
25#include <linux/hdreg.h>
26#include <linux/dma-mapping.h>
27#include <linux/completion.h>
28#include <linux/scatterlist.h>
29#include <linux/version.h>
30#include <linux/err.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060031#include <linux/aer.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060032#include <linux/wait.h>
Bart Van Assche2da7b402017-08-17 13:13:01 -070033#include <linux/stringify.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060034#include <scsi/scsi.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060035#include <scsi/sg.h>
36#include <linux/io.h>
37#include <linux/uaccess.h>
Bartlomiej Zolnierkiewicz4ca90b52013-11-05 12:37:04 +010038#include <asm/unaligned.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060039
40#include "skd_s1120.h"
41
42static int skd_dbg_level;
43static int skd_isr_comp_limit = 4;
44
45enum {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060046 SKD_FLUSH_INITIALIZER,
47 SKD_FLUSH_ZERO_SIZE_FIRST,
48 SKD_FLUSH_DATA_SECOND,
49};
50
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060051#define SKD_ASSERT(expr) \
52 do { \
53 if (unlikely(!(expr))) { \
54 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
55 # expr, __FILE__, __func__, __LINE__); \
56 } \
57 } while (0)
58
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060059#define DRV_NAME "skd"
60#define DRV_VERSION "2.2.1"
61#define DRV_BUILD_ID "0260"
62#define PFX DRV_NAME ": "
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060063
Bart Van Asschebec9e8a2017-08-17 13:12:47 -070064MODULE_LICENSE("GPL");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060065
Mike Snitzer38d4a1b2013-11-01 15:05:10 -040066MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060067MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
68
69#define PCI_VENDOR_ID_STEC 0x1B39
70#define PCI_DEVICE_ID_S1120 0x0001
71
72#define SKD_FUA_NV (1 << 1)
73#define SKD_MINORS_PER_DEVICE 16
74
75#define SKD_MAX_QUEUE_DEPTH 200u
76
77#define SKD_PAUSE_TIMEOUT (5 * 1000)
78
79#define SKD_N_FITMSG_BYTES (512u)
Bart Van Assche2da7b402017-08-17 13:13:01 -070080#define SKD_MAX_REQ_PER_MSG 14
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060081
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060082#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
83
84/* SG elements are 32 bytes, so we can make this 4096 and still be under the
85 * 128KB limit. That allows 4096*4K = 16M xfer size
86 */
87#define SKD_N_SG_PER_REQ_DEFAULT 256u
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060088
89#define SKD_N_COMPLETION_ENTRY 256u
90#define SKD_N_READ_CAP_BYTES (8u)
91
92#define SKD_N_INTERNAL_BYTES (512u)
93
Bart Van Assche6f7c7672017-08-17 13:13:02 -070094#define SKD_SKCOMP_SIZE \
95 ((sizeof(struct fit_completion_entry_v1) + \
96 sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY)
97
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060098/* 5 bits of uniqifier, 0xF800 */
99#define SKD_ID_INCR (0x400)
100#define SKD_ID_TABLE_MASK (3u << 8u)
101#define SKD_ID_RW_REQUEST (0u << 8u)
102#define SKD_ID_INTERNAL (1u << 8u)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600103#define SKD_ID_FIT_MSG (3u << 8u)
104#define SKD_ID_SLOT_MASK 0x00FFu
105#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
106
107#define SKD_N_TIMEOUT_SLOT 4u
108#define SKD_TIMEOUT_SLOT_MASK 3u
109
110#define SKD_N_MAX_SECTORS 2048u
111
112#define SKD_MAX_RETRIES 2u
113
114#define SKD_TIMER_SECONDS(seconds) (seconds)
115#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
116
117#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600118
119enum skd_drvr_state {
120 SKD_DRVR_STATE_LOAD,
121 SKD_DRVR_STATE_IDLE,
122 SKD_DRVR_STATE_BUSY,
123 SKD_DRVR_STATE_STARTING,
124 SKD_DRVR_STATE_ONLINE,
125 SKD_DRVR_STATE_PAUSING,
126 SKD_DRVR_STATE_PAUSED,
127 SKD_DRVR_STATE_DRAINING_TIMEOUT,
128 SKD_DRVR_STATE_RESTARTING,
129 SKD_DRVR_STATE_RESUMING,
130 SKD_DRVR_STATE_STOPPING,
131 SKD_DRVR_STATE_FAULT,
132 SKD_DRVR_STATE_DISAPPEARED,
133 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
134 SKD_DRVR_STATE_BUSY_ERASE,
135 SKD_DRVR_STATE_BUSY_SANITIZE,
136 SKD_DRVR_STATE_BUSY_IMMINENT,
137 SKD_DRVR_STATE_WAIT_BOOT,
138 SKD_DRVR_STATE_SYNCING,
139};
140
141#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
142#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
143#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
144#define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u)
145#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
146#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
147#define SKD_START_WAIT_SECONDS 90u
148
149enum skd_req_state {
150 SKD_REQ_STATE_IDLE,
151 SKD_REQ_STATE_SETUP,
152 SKD_REQ_STATE_BUSY,
153 SKD_REQ_STATE_COMPLETED,
154 SKD_REQ_STATE_TIMEOUT,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600155};
156
157enum skd_fit_msg_state {
158 SKD_MSG_STATE_IDLE,
159 SKD_MSG_STATE_BUSY,
160};
161
162enum skd_check_status_action {
163 SKD_CHECK_STATUS_REPORT_GOOD,
164 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
165 SKD_CHECK_STATUS_REQUEUE_REQUEST,
166 SKD_CHECK_STATUS_REPORT_ERROR,
167 SKD_CHECK_STATUS_BUSY_IMMINENT,
168};
169
Bart Van Assched891fe62017-08-17 13:13:07 -0700170struct skd_msg_buf {
171 struct fit_msg_hdr fmh;
172 struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG];
173};
174
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600175struct skd_fitmsg_context {
176 enum skd_fit_msg_state state;
177
178 struct skd_fitmsg_context *next;
179
180 u32 id;
181 u16 outstanding;
182
183 u32 length;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600184
Bart Van Assched891fe62017-08-17 13:13:07 -0700185 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600186 dma_addr_t mb_dma_address;
187};
188
189struct skd_request_context {
190 enum skd_req_state state;
191
192 struct skd_request_context *next;
193
194 u16 id;
195 u32 fitmsg_id;
196
197 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600198 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600199
200 u32 timeout_stamp;
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700201 enum dma_data_direction data_dir;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600202 struct scatterlist *sg;
203 u32 n_sg;
204 u32 sg_byte_count;
205
206 struct fit_sg_descriptor *sksg_list;
207 dma_addr_t sksg_dma_address;
208
209 struct fit_completion_entry_v1 completion;
210
211 struct fit_comp_error_info err_info;
212
213};
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600214
215struct skd_special_context {
216 struct skd_request_context req;
217
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600218 void *data_buf;
219 dma_addr_t db_dma_address;
220
Bart Van Assched891fe62017-08-17 13:13:07 -0700221 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600222 dma_addr_t mb_dma_address;
223};
224
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600225typedef enum skd_irq_type {
226 SKD_IRQ_LEGACY,
227 SKD_IRQ_MSI,
228 SKD_IRQ_MSIX
229} skd_irq_type_t;
230
231#define SKD_MAX_BARS 2
232
233struct skd_device {
Bart Van Assche85e34112017-08-17 13:13:17 -0700234 void __iomem *mem_map[SKD_MAX_BARS];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600235 resource_size_t mem_phys[SKD_MAX_BARS];
236 u32 mem_size[SKD_MAX_BARS];
237
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600238 struct skd_msix_entry *msix_entries;
239
240 struct pci_dev *pdev;
241 int pcie_error_reporting_is_enabled;
242
243 spinlock_t lock;
244 struct gendisk *disk;
245 struct request_queue *queue;
246 struct device *class_dev;
247 int gendisk_on;
248 int sync_done;
249
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600250 u32 devno;
251 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600252 char isr_name[30];
253
254 enum skd_drvr_state state;
255 u32 drive_state;
256
257 u32 in_flight;
258 u32 cur_max_queue_depth;
259 u32 queue_low_water_mark;
260 u32 dev_max_queue_depth;
261
262 u32 num_fitmsg_context;
263 u32 num_req_context;
264
265 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
266 u32 timeout_stamp;
267 struct skd_fitmsg_context *skmsg_free_list;
268 struct skd_fitmsg_context *skmsg_table;
269
270 struct skd_request_context *skreq_free_list;
271 struct skd_request_context *skreq_table;
272
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600273 struct skd_special_context internal_skspcl;
274 u32 read_cap_blocksize;
275 u32 read_cap_last_lba;
276 int read_cap_is_valid;
277 int inquiry_is_valid;
278 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600279
280 u8 skcomp_cycle;
281 u32 skcomp_ix;
282 struct fit_completion_entry_v1 *skcomp_table;
283 struct fit_comp_error_info *skerr_table;
284 dma_addr_t cq_dma_address;
285
286 wait_queue_head_t waitq;
287
288 struct timer_list timer;
289 u32 timer_countdown;
290 u32 timer_substate;
291
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600292 int sgs_per_request;
293 u32 last_mtd;
294
295 u32 proto_ver;
296
297 int dbg_level;
298 u32 connect_time_stamp;
299 int connect_retries;
300#define SKD_MAX_CONNECT_RETRIES 16
301 u32 drive_jiffies;
302
303 u32 timo_slot;
304
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600305 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600306};
307
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600308#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
309#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
310#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
311
312static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
313{
Bart Van Assche14262a42017-08-17 13:12:57 -0700314 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600315
Bart Van Assche14262a42017-08-17 13:12:57 -0700316 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700317 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700318 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600319}
320
321static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
322 u32 offset)
323{
Bart Van Assche14262a42017-08-17 13:12:57 -0700324 writel(val, skdev->mem_map[1] + offset);
325 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700326 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600327}
328
329static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
330 u32 offset)
331{
Bart Van Assche14262a42017-08-17 13:12:57 -0700332 writeq(val, skdev->mem_map[1] + offset);
333 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700334 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
335 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600336}
337
338
339#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
340static int skd_isr_type = SKD_IRQ_DEFAULT;
341
342module_param(skd_isr_type, int, 0444);
343MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
344 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
345
346#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
347static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
348
349module_param(skd_max_req_per_msg, int, 0444);
350MODULE_PARM_DESC(skd_max_req_per_msg,
351 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700352 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600353
354#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
355#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
356static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
357
358module_param(skd_max_queue_depth, int, 0444);
359MODULE_PARM_DESC(skd_max_queue_depth,
360 "Maximum SCSI requests issued to s1120."
361 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
362
363static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
364module_param(skd_sgs_per_request, int, 0444);
365MODULE_PARM_DESC(skd_sgs_per_request,
366 "Maximum SG elements per block request."
367 " (1-4096, default==256)");
368
Bart Van Assche63214122017-08-17 13:13:23 -0700369static int skd_max_pass_thru = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600370module_param(skd_max_pass_thru, int, 0444);
371MODULE_PARM_DESC(skd_max_pass_thru,
Bart Van Assche63214122017-08-17 13:13:23 -0700372 "Maximum SCSI pass-thru at a time. IGNORED");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600373
374module_param(skd_dbg_level, int, 0444);
375MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
376
377module_param(skd_isr_comp_limit, int, 0444);
378MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
379
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600380/* Major device number dynamically assigned. */
381static u32 skd_major;
382
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600383static void skd_destruct(struct skd_device *skdev);
384static const struct block_device_operations skd_blockdev_ops;
385static void skd_send_fitmsg(struct skd_device *skdev,
386 struct skd_fitmsg_context *skmsg);
387static void skd_send_special_fitmsg(struct skd_device *skdev,
388 struct skd_special_context *skspcl);
389static void skd_request_fn(struct request_queue *rq);
390static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200391 struct skd_request_context *skreq, blk_status_t status);
392static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600393 struct skd_request_context *skreq);
394static void skd_postop_sg_list(struct skd_device *skdev,
395 struct skd_request_context *skreq);
396
397static void skd_restart_device(struct skd_device *skdev);
398static int skd_quiesce_dev(struct skd_device *skdev);
399static int skd_unquiesce_dev(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600400static void skd_disable_interrupts(struct skd_device *skdev);
401static void skd_isr_fwstate(struct skd_device *skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700402static void skd_recover_requests(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600403static void skd_soft_reset(struct skd_device *skdev);
404
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600405const char *skd_drive_state_to_str(int state);
406const char *skd_skdev_state_to_str(enum skd_drvr_state state);
407static void skd_log_skdev(struct skd_device *skdev, const char *event);
408static void skd_log_skmsg(struct skd_device *skdev,
409 struct skd_fitmsg_context *skmsg, const char *event);
410static void skd_log_skreq(struct skd_device *skdev,
411 struct skd_request_context *skreq, const char *event);
412
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600413/*
414 *****************************************************************************
415 * READ/WRITE REQUESTS
416 *****************************************************************************
417 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600418static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600419{
420 struct request_queue *q = skdev->queue;
421 struct request *req;
422
423 for (;; ) {
424 req = blk_peek_request(q);
425 if (req == NULL)
426 break;
427 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200428 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600429 }
430}
431
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600432static void
433skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
434 int data_dir, unsigned lba,
435 unsigned count)
436{
437 if (data_dir == READ)
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700438 scsi_req->cdb[0] = READ_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600439 else
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700440 scsi_req->cdb[0] = WRITE_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600441
442 scsi_req->cdb[1] = 0;
443 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
444 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
445 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
446 scsi_req->cdb[5] = (lba & 0xff);
447 scsi_req->cdb[6] = 0;
448 scsi_req->cdb[7] = (count & 0xff00) >> 8;
449 scsi_req->cdb[8] = count & 0xff;
450 scsi_req->cdb[9] = 0;
451}
452
453static void
454skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400455 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600456{
457 skreq->flush_cmd = 1;
458
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700459 scsi_req->cdb[0] = SYNCHRONIZE_CACHE;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600460 scsi_req->cdb[1] = 0;
461 scsi_req->cdb[2] = 0;
462 scsi_req->cdb[3] = 0;
463 scsi_req->cdb[4] = 0;
464 scsi_req->cdb[5] = 0;
465 scsi_req->cdb[6] = 0;
466 scsi_req->cdb[7] = 0;
467 scsi_req->cdb[8] = 0;
468 scsi_req->cdb[9] = 0;
469}
470
Bart Van Assche3d17a672017-08-17 13:13:21 -0700471/*
472 * Return true if and only if all pending requests should be failed.
473 */
474static bool skd_fail_all(struct request_queue *q)
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700475{
476 struct skd_device *skdev = q->queuedata;
477
478 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
479
480 skd_log_skdev(skdev, "req_not_online");
481 switch (skdev->state) {
482 case SKD_DRVR_STATE_PAUSING:
483 case SKD_DRVR_STATE_PAUSED:
484 case SKD_DRVR_STATE_STARTING:
485 case SKD_DRVR_STATE_RESTARTING:
486 case SKD_DRVR_STATE_WAIT_BOOT:
487 /* In case of starting, we haven't started the queue,
488 * so we can't get here... but requests are
489 * possibly hanging out waiting for us because we
490 * reported the dev/skd0 already. They'll wait
491 * forever if connect doesn't complete.
492 * What to do??? delay dev/skd0 ??
493 */
494 case SKD_DRVR_STATE_BUSY:
495 case SKD_DRVR_STATE_BUSY_IMMINENT:
496 case SKD_DRVR_STATE_BUSY_ERASE:
497 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700498 return false;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700499
500 case SKD_DRVR_STATE_BUSY_SANITIZE:
501 case SKD_DRVR_STATE_STOPPING:
502 case SKD_DRVR_STATE_SYNCING:
503 case SKD_DRVR_STATE_FAULT:
504 case SKD_DRVR_STATE_DISAPPEARED:
505 default:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700506 return true;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700507 }
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700508}
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600509
510static void skd_request_fn(struct request_queue *q)
511{
512 struct skd_device *skdev = q->queuedata;
513 struct skd_fitmsg_context *skmsg = NULL;
514 struct fit_msg_hdr *fmh = NULL;
515 struct skd_request_context *skreq;
516 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600517 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600518 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600519 u32 lba;
520 u32 count;
521 int data_dir;
Bart Van Assche4854afe2017-08-17 13:12:59 -0700522 __be64 be_dmaa;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600523 u64 cmdctxt;
524 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600525 int flush, fua;
526
527 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Assche3d17a672017-08-17 13:13:21 -0700528 if (skd_fail_all(q))
529 skd_fail_all_pending(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600530 return;
531 }
532
Jens Axboe6a5ec652013-11-01 10:38:45 -0600533 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600534 if (skdev->skmsg_free_list == NULL ||
535 skdev->skreq_free_list == NULL ||
536 skdev->in_flight >= skdev->queue_low_water_mark)
537 /* There is still some kind of shortage */
538 return;
539
Jens Axboe6a5ec652013-11-01 10:38:45 -0600540 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600541 }
542
543 /*
544 * Stop conditions:
545 * - There are no more native requests
546 * - There are already the maximum number of requests in progress
547 * - There are no more skd_request_context entries
548 * - There are no more FIT msg buffers
549 */
550 for (;; ) {
551
552 flush = fua = 0;
553
Jens Axboefcd37eb2013-11-01 10:14:56 -0600554 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600555
Jens Axboefcd37eb2013-11-01 10:14:56 -0600556 /* Are there any native requests to start? */
557 if (req == NULL)
558 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600559
Jens Axboefcd37eb2013-11-01 10:14:56 -0600560 lba = (u32)blk_rq_pos(req);
561 count = blk_rq_sectors(req);
562 data_dir = rq_data_dir(req);
563 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600564
Mike Christie3a5e02c2016-06-05 14:32:23 -0500565 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600566 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600567
Jens Axboefcd37eb2013-11-01 10:14:56 -0600568 if (io_flags & REQ_FUA)
569 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600570
Bart Van Asschef98806d2017-08-17 13:12:58 -0700571 dev_dbg(&skdev->pdev->dev,
572 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
573 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600574
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400575 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600576
577 /* Are too many requets already in progress? */
578 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700579 dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
580 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600581 break;
582 }
583
584 /* Is a skd_request_context available? */
585 skreq = skdev->skreq_free_list;
586 if (skreq == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700587 dev_dbg(&skdev->pdev->dev, "Out of req=%p\n", q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600588 break;
589 }
590 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
591 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
592
593 /* Now we check to see if we can get a fit msg */
594 if (skmsg == NULL) {
595 if (skdev->skmsg_free_list == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700596 dev_dbg(&skdev->pdev->dev, "Out of msg\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600597 break;
598 }
599 }
600
601 skreq->flush_cmd = 0;
602 skreq->n_sg = 0;
603 skreq->sg_byte_count = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600604
605 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400606 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600607 *
608 * At this point we are comitted to either start or reject
609 * the native request. Note that skd_request_context is
610 * available but is still at the head of the free list.
611 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600612 blk_start_request(req);
613 skreq->req = req;
614 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600615
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700616 skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE :
617 DMA_TO_DEVICE;
Bart Van Assche19fc85c2017-08-17 13:13:04 -0700618
619 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
620 dev_dbg(&skdev->pdev->dev, "error Out\n");
621 skd_end_request(skdev, skreq, BLK_STS_RESOURCE);
622 continue;
623 }
624
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600625 /* Either a FIT msg is in progress or we have to start one. */
626 if (skmsg == NULL) {
627 /* Are there any FIT msg buffers available? */
628 skmsg = skdev->skmsg_free_list;
629 if (skmsg == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700630 dev_dbg(&skdev->pdev->dev,
631 "Out of msg skdev=%p\n",
632 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600633 break;
634 }
635 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
636 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
637
638 skdev->skmsg_free_list = skmsg->next;
639
640 skmsg->state = SKD_MSG_STATE_BUSY;
641 skmsg->id += SKD_ID_INCR;
642
643 /* Initialize the FIT msg header */
Bart Van Assched891fe62017-08-17 13:13:07 -0700644 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600645 memset(fmh, 0, sizeof(*fmh));
646 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
647 skmsg->length = sizeof(*fmh);
648 }
649
650 skreq->fitmsg_id = skmsg->id;
651
Bart Van Assched891fe62017-08-17 13:13:07 -0700652 scsi_req =
653 &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
654 memset(scsi_req, 0, sizeof(*scsi_req));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600655
Bart Van Assche4854afe2017-08-17 13:12:59 -0700656 be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600657 cmdctxt = skreq->id + SKD_ID_INCR;
658
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600659 scsi_req->hdr.tag = cmdctxt;
660 scsi_req->hdr.sg_list_dma_address = be_dmaa;
661
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600662 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600663 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
664 SKD_ASSERT(skreq->flush_cmd == 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600665 } else {
666 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
667 }
668
669 if (fua)
670 scsi_req->cdb[1] |= SKD_FUA_NV;
671
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600672 scsi_req->hdr.sg_list_len_bytes =
673 cpu_to_be32(skreq->sg_byte_count);
674
675 /* Complete resource allocations. */
676 skdev->skreq_free_list = skreq->next;
677 skreq->state = SKD_REQ_STATE_BUSY;
678 skreq->id += SKD_ID_INCR;
679
680 skmsg->length += sizeof(struct skd_scsi_request);
681 fmh->num_protocol_cmds_coalesced++;
682
683 /*
684 * Update the active request counts.
685 * Capture the timeout timestamp.
686 */
687 skreq->timeout_stamp = skdev->timeout_stamp;
688 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
689 skdev->timeout_slot[timo_slot]++;
690 skdev->in_flight++;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700691 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
692 skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600693
694 /*
695 * If the FIT msg buffer is full send it.
696 */
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700697 if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600698 skd_send_fitmsg(skdev, skmsg);
699 skmsg = NULL;
700 fmh = NULL;
701 }
702 }
703
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700704 /* If the FIT msg buffer is not empty send what we got. */
705 if (skmsg) {
706 WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced);
707 skd_send_fitmsg(skdev, skmsg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600708 skmsg = NULL;
709 fmh = NULL;
710 }
711
712 /*
713 * If req is non-NULL it means there is something to do but
714 * we are out of a resource.
715 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600716 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600717 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600718}
719
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400720static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200721 struct skd_request_context *skreq, blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600722{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600723 if (unlikely(error)) {
724 struct request *req = skreq->req;
725 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
726 u32 lba = (u32)blk_rq_pos(req);
727 u32 count = blk_rq_sectors(req);
728
Bart Van Asschef98806d2017-08-17 13:12:58 -0700729 dev_err(&skdev->pdev->dev,
730 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
731 count, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600732 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -0700733 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", skreq->id,
734 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600735
736 __blk_end_request_all(skreq->req, error);
737}
738
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200739static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400740 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600741{
742 struct request *req = skreq->req;
Bart Van Assche06f824c42017-08-17 13:13:15 -0700743 struct scatterlist *sgl = &skreq->sg[0], *sg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600744 int n_sg;
745 int i;
746
747 skreq->sg_byte_count = 0;
748
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700749 WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE &&
750 skreq->data_dir != DMA_FROM_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600751
Bart Van Assche06f824c42017-08-17 13:13:15 -0700752 n_sg = blk_rq_map_sg(skdev->queue, req, sgl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600753 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200754 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600755
756 /*
757 * Map scatterlist to PCI bus addresses.
758 * Note PCI might change the number of entries.
759 */
Bart Van Assche06f824c42017-08-17 13:13:15 -0700760 n_sg = pci_map_sg(skdev->pdev, sgl, n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600761 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200762 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600763
764 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
765
766 skreq->n_sg = n_sg;
767
Bart Van Assche06f824c42017-08-17 13:13:15 -0700768 for_each_sg(sgl, sg, n_sg, i) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600769 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Assche06f824c42017-08-17 13:13:15 -0700770 u32 cnt = sg_dma_len(sg);
771 uint64_t dma_addr = sg_dma_address(sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600772
773 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
774 sgd->byte_count = cnt;
775 skreq->sg_byte_count += cnt;
776 sgd->host_side_addr = dma_addr;
777 sgd->dev_side_addr = 0;
778 }
779
780 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
781 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
782
783 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700784 dev_dbg(&skdev->pdev->dev,
785 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
786 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600787 for (i = 0; i < n_sg; i++) {
788 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700789
790 dev_dbg(&skdev->pdev->dev,
791 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
792 i, sgd->byte_count, sgd->control,
793 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600794 }
795 }
796
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200797 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600798}
799
Jens Axboefcd37eb2013-11-01 10:14:56 -0600800static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400801 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600802{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600803 /*
804 * restore the next ptr for next IO request so we
805 * don't have to set it every time.
806 */
807 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
808 skreq->sksg_dma_address +
809 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700810 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600811}
812
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600813/*
814 *****************************************************************************
815 * TIMER
816 *****************************************************************************
817 */
818
819static void skd_timer_tick_not_online(struct skd_device *skdev);
820
821static void skd_timer_tick(ulong arg)
822{
823 struct skd_device *skdev = (struct skd_device *)arg;
824
825 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600826 unsigned long reqflags;
827 u32 state;
828
829 if (skdev->state == SKD_DRVR_STATE_FAULT)
830 /* The driver has declared fault, and we want it to
831 * stay that way until driver is reloaded.
832 */
833 return;
834
835 spin_lock_irqsave(&skdev->lock, reqflags);
836
837 state = SKD_READL(skdev, FIT_STATUS);
838 state &= FIT_SR_DRIVE_STATE_MASK;
839 if (state != skdev->drive_state)
840 skd_isr_fwstate(skdev);
841
842 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
843 skd_timer_tick_not_online(skdev);
844 goto timer_func_out;
845 }
846 skdev->timeout_stamp++;
847 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
848
849 /*
850 * All requests that happened during the previous use of
851 * this slot should be done by now. The previous use was
852 * over 7 seconds ago.
853 */
854 if (skdev->timeout_slot[timo_slot] == 0)
855 goto timer_func_out;
856
857 /* Something is overdue */
Bart Van Asschef98806d2017-08-17 13:12:58 -0700858 dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
859 skdev->timeout_slot[timo_slot], skdev->in_flight);
860 dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
861 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600862
863 skdev->timer_countdown = SKD_DRAINING_TIMO;
864 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
865 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600866 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600867
868timer_func_out:
869 mod_timer(&skdev->timer, (jiffies + HZ));
870
871 spin_unlock_irqrestore(&skdev->lock, reqflags);
872}
873
874static void skd_timer_tick_not_online(struct skd_device *skdev)
875{
876 switch (skdev->state) {
877 case SKD_DRVR_STATE_IDLE:
878 case SKD_DRVR_STATE_LOAD:
879 break;
880 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700881 dev_dbg(&skdev->pdev->dev,
882 "drive busy sanitize[%x], driver[%x]\n",
883 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600884 /* If we've been in sanitize for 3 seconds, we figure we're not
885 * going to get anymore completions, so recover requests now
886 */
887 if (skdev->timer_countdown > 0) {
888 skdev->timer_countdown--;
889 return;
890 }
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700891 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600892 break;
893
894 case SKD_DRVR_STATE_BUSY:
895 case SKD_DRVR_STATE_BUSY_IMMINENT:
896 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700897 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
898 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600899 if (skdev->timer_countdown > 0) {
900 skdev->timer_countdown--;
901 return;
902 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700903 dev_dbg(&skdev->pdev->dev,
904 "busy[%x], timedout=%d, restarting device.",
905 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600906 skd_restart_device(skdev);
907 break;
908
909 case SKD_DRVR_STATE_WAIT_BOOT:
910 case SKD_DRVR_STATE_STARTING:
911 if (skdev->timer_countdown > 0) {
912 skdev->timer_countdown--;
913 return;
914 }
915 /* For now, we fault the drive. Could attempt resets to
916 * revcover at some point. */
917 skdev->state = SKD_DRVR_STATE_FAULT;
918
Bart Van Asschef98806d2017-08-17 13:12:58 -0700919 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
920 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600921
922 /*start the queue so we can respond with error to requests */
923 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600924 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600925 skdev->gendisk_on = -1;
926 wake_up_interruptible(&skdev->waitq);
927 break;
928
929 case SKD_DRVR_STATE_ONLINE:
930 /* shouldn't get here. */
931 break;
932
933 case SKD_DRVR_STATE_PAUSING:
934 case SKD_DRVR_STATE_PAUSED:
935 break;
936
937 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700938 dev_dbg(&skdev->pdev->dev,
939 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
940 skdev->timo_slot, skdev->timer_countdown,
941 skdev->in_flight,
942 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600943 /* if the slot has cleared we can let the I/O continue */
944 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700945 dev_dbg(&skdev->pdev->dev,
946 "Slot drained, starting queue.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600947 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600948 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600949 return;
950 }
951 if (skdev->timer_countdown > 0) {
952 skdev->timer_countdown--;
953 return;
954 }
955 skd_restart_device(skdev);
956 break;
957
958 case SKD_DRVR_STATE_RESTARTING:
959 if (skdev->timer_countdown > 0) {
960 skdev->timer_countdown--;
961 return;
962 }
963 /* For now, we fault the drive. Could attempt resets to
964 * revcover at some point. */
965 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700966 dev_err(&skdev->pdev->dev,
967 "DriveFault Reconnect Timeout (%x)\n",
968 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600969
970 /*
971 * Recovering does two things:
972 * 1. completes IO with error
973 * 2. reclaims dma resources
974 * When is it safe to recover requests?
975 * - if the drive state is faulted
976 * - if the state is still soft reset after out timeout
977 * - if the drive registers are dead (state = FF)
978 * If it is "unsafe", we still need to recover, so we will
979 * disable pci bus mastering and disable our interrupts.
980 */
981
982 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
983 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
984 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
985 /* It never came out of soft reset. Try to
986 * recover the requests and then let them
987 * fail. This is to mitigate hung processes. */
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700988 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600989 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700990 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
991 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600992 pci_disable_device(skdev->pdev);
993 skd_disable_interrupts(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700994 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600995 }
996
997 /*start the queue so we can respond with error to requests */
998 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600999 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001000 skdev->gendisk_on = -1;
1001 wake_up_interruptible(&skdev->waitq);
1002 break;
1003
1004 case SKD_DRVR_STATE_RESUMING:
1005 case SKD_DRVR_STATE_STOPPING:
1006 case SKD_DRVR_STATE_SYNCING:
1007 case SKD_DRVR_STATE_FAULT:
1008 case SKD_DRVR_STATE_DISAPPEARED:
1009 default:
1010 break;
1011 }
1012}
1013
1014static int skd_start_timer(struct skd_device *skdev)
1015{
1016 int rc;
1017
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001018 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1019
1020 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1021 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001022 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001023 return rc;
1024}
1025
1026static void skd_kill_timer(struct skd_device *skdev)
1027{
1028 del_timer_sync(&skdev->timer);
1029}
1030
1031/*
1032 *****************************************************************************
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001033 * INTERNAL REQUESTS -- generated by driver itself
1034 *****************************************************************************
1035 */
1036
1037static int skd_format_internal_skspcl(struct skd_device *skdev)
1038{
1039 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1040 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1041 struct fit_msg_hdr *fmh;
1042 uint64_t dma_address;
1043 struct skd_scsi_request *scsi;
1044
Bart Van Assched891fe62017-08-17 13:13:07 -07001045 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001046 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1047 fmh->num_protocol_cmds_coalesced = 1;
1048
Bart Van Assched891fe62017-08-17 13:13:07 -07001049 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001050 memset(scsi, 0, sizeof(*scsi));
1051 dma_address = skspcl->req.sksg_dma_address;
1052 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
Bart Van Assche32494df2017-08-17 13:13:25 -07001053 skspcl->req.n_sg = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001054 sgd->control = FIT_SGD_CONTROL_LAST;
1055 sgd->byte_count = 0;
1056 sgd->host_side_addr = skspcl->db_dma_address;
1057 sgd->dev_side_addr = 0;
1058 sgd->next_desc_ptr = 0LL;
1059
1060 return 1;
1061}
1062
1063#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1064
1065static void skd_send_internal_skspcl(struct skd_device *skdev,
1066 struct skd_special_context *skspcl,
1067 u8 opcode)
1068{
1069 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1070 struct skd_scsi_request *scsi;
1071 unsigned char *buf = skspcl->data_buf;
1072 int i;
1073
1074 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1075 /*
1076 * A refresh is already in progress.
1077 * Just wait for it to finish.
1078 */
1079 return;
1080
1081 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1082 skspcl->req.state = SKD_REQ_STATE_BUSY;
1083 skspcl->req.id += SKD_ID_INCR;
1084
Bart Van Assched891fe62017-08-17 13:13:07 -07001085 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001086 scsi->hdr.tag = skspcl->req.id;
1087
1088 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1089
1090 switch (opcode) {
1091 case TEST_UNIT_READY:
1092 scsi->cdb[0] = TEST_UNIT_READY;
1093 sgd->byte_count = 0;
1094 scsi->hdr.sg_list_len_bytes = 0;
1095 break;
1096
1097 case READ_CAPACITY:
1098 scsi->cdb[0] = READ_CAPACITY;
1099 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1100 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1101 break;
1102
1103 case INQUIRY:
1104 scsi->cdb[0] = INQUIRY;
1105 scsi->cdb[1] = 0x01; /* evpd */
1106 scsi->cdb[2] = 0x80; /* serial number page */
1107 scsi->cdb[4] = 0x10;
1108 sgd->byte_count = 16;
1109 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1110 break;
1111
1112 case SYNCHRONIZE_CACHE:
1113 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1114 sgd->byte_count = 0;
1115 scsi->hdr.sg_list_len_bytes = 0;
1116 break;
1117
1118 case WRITE_BUFFER:
1119 scsi->cdb[0] = WRITE_BUFFER;
1120 scsi->cdb[1] = 0x02;
1121 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1122 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1123 sgd->byte_count = WR_BUF_SIZE;
1124 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1125 /* fill incrementing byte pattern */
1126 for (i = 0; i < sgd->byte_count; i++)
1127 buf[i] = i & 0xFF;
1128 break;
1129
1130 case READ_BUFFER:
1131 scsi->cdb[0] = READ_BUFFER;
1132 scsi->cdb[1] = 0x02;
1133 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1134 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1135 sgd->byte_count = WR_BUF_SIZE;
1136 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1137 memset(skspcl->data_buf, 0, sgd->byte_count);
1138 break;
1139
1140 default:
1141 SKD_ASSERT("Don't know what to send");
1142 return;
1143
1144 }
1145 skd_send_special_fitmsg(skdev, skspcl);
1146}
1147
1148static void skd_refresh_device_data(struct skd_device *skdev)
1149{
1150 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1151
1152 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1153}
1154
1155static int skd_chk_read_buf(struct skd_device *skdev,
1156 struct skd_special_context *skspcl)
1157{
1158 unsigned char *buf = skspcl->data_buf;
1159 int i;
1160
1161 /* check for incrementing byte pattern */
1162 for (i = 0; i < WR_BUF_SIZE; i++)
1163 if (buf[i] != (i & 0xFF))
1164 return 1;
1165
1166 return 0;
1167}
1168
1169static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1170 u8 code, u8 qual, u8 fruc)
1171{
1172 /* If the check condition is of special interest, log a message */
1173 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1174 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001175 dev_err(&skdev->pdev->dev,
1176 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1177 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001178 }
1179}
1180
1181static void skd_complete_internal(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001182 struct fit_completion_entry_v1 *skcomp,
1183 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001184 struct skd_special_context *skspcl)
1185{
1186 u8 *buf = skspcl->data_buf;
1187 u8 status;
1188 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001189 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001190
Bart Van Assche760b48c2017-08-17 13:13:00 -07001191 lockdep_assert_held(&skdev->lock);
1192
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001193 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1194
Bart Van Asschef98806d2017-08-17 13:12:58 -07001195 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001196
1197 skspcl->req.completion = *skcomp;
1198 skspcl->req.state = SKD_REQ_STATE_IDLE;
1199 skspcl->req.id += SKD_ID_INCR;
1200
1201 status = skspcl->req.completion.status;
1202
1203 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1204 skerr->qual, skerr->fruc);
1205
1206 switch (scsi->cdb[0]) {
1207 case TEST_UNIT_READY:
1208 if (status == SAM_STAT_GOOD)
1209 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1210 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1211 (skerr->key == MEDIUM_ERROR))
1212 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1213 else {
1214 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001215 dev_dbg(&skdev->pdev->dev,
1216 "TUR failed, don't send anymore state 0x%x\n",
1217 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001218 return;
1219 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001220 dev_dbg(&skdev->pdev->dev,
1221 "**** TUR failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001222 skd_send_internal_skspcl(skdev, skspcl,
1223 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001224 }
1225 break;
1226
1227 case WRITE_BUFFER:
1228 if (status == SAM_STAT_GOOD)
1229 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1230 else {
1231 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001232 dev_dbg(&skdev->pdev->dev,
1233 "write buffer failed, don't send anymore state 0x%x\n",
1234 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001235 return;
1236 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001237 dev_dbg(&skdev->pdev->dev,
1238 "**** write buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001239 skd_send_internal_skspcl(skdev, skspcl,
1240 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001241 }
1242 break;
1243
1244 case READ_BUFFER:
1245 if (status == SAM_STAT_GOOD) {
1246 if (skd_chk_read_buf(skdev, skspcl) == 0)
1247 skd_send_internal_skspcl(skdev, skspcl,
1248 READ_CAPACITY);
1249 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001250 dev_err(&skdev->pdev->dev,
1251 "*** W/R Buffer mismatch %d ***\n",
1252 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001253 if (skdev->connect_retries <
1254 SKD_MAX_CONNECT_RETRIES) {
1255 skdev->connect_retries++;
1256 skd_soft_reset(skdev);
1257 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001258 dev_err(&skdev->pdev->dev,
1259 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001260 return;
1261 }
1262 }
1263
1264 } else {
1265 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001266 dev_dbg(&skdev->pdev->dev,
1267 "read buffer failed, don't send anymore state 0x%x\n",
1268 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001269 return;
1270 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001271 dev_dbg(&skdev->pdev->dev,
1272 "**** read buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001273 skd_send_internal_skspcl(skdev, skspcl,
1274 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001275 }
1276 break;
1277
1278 case READ_CAPACITY:
1279 skdev->read_cap_is_valid = 0;
1280 if (status == SAM_STAT_GOOD) {
1281 skdev->read_cap_last_lba =
1282 (buf[0] << 24) | (buf[1] << 16) |
1283 (buf[2] << 8) | buf[3];
1284 skdev->read_cap_blocksize =
1285 (buf[4] << 24) | (buf[5] << 16) |
1286 (buf[6] << 8) | buf[7];
1287
Bart Van Asschef98806d2017-08-17 13:12:58 -07001288 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1289 skdev->read_cap_last_lba,
1290 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001291
1292 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1293
1294 skdev->read_cap_is_valid = 1;
1295
1296 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1297 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1298 (skerr->key == MEDIUM_ERROR)) {
1299 skdev->read_cap_last_lba = ~0;
1300 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001301 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 -06001302 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1303 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001304 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001305 skd_send_internal_skspcl(skdev, skspcl,
1306 TEST_UNIT_READY);
1307 }
1308 break;
1309
1310 case INQUIRY:
1311 skdev->inquiry_is_valid = 0;
1312 if (status == SAM_STAT_GOOD) {
1313 skdev->inquiry_is_valid = 1;
1314
1315 for (i = 0; i < 12; i++)
1316 skdev->inq_serial_num[i] = buf[i + 4];
1317 skdev->inq_serial_num[12] = 0;
1318 }
1319
1320 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001321 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001322 /* connection is complete */
1323 skdev->connect_retries = 0;
1324 break;
1325
1326 case SYNCHRONIZE_CACHE:
1327 if (status == SAM_STAT_GOOD)
1328 skdev->sync_done = 1;
1329 else
1330 skdev->sync_done = -1;
1331 wake_up_interruptible(&skdev->waitq);
1332 break;
1333
1334 default:
1335 SKD_ASSERT("we didn't send this");
1336 }
1337}
1338
1339/*
1340 *****************************************************************************
1341 * FIT MESSAGES
1342 *****************************************************************************
1343 */
1344
1345static void skd_send_fitmsg(struct skd_device *skdev,
1346 struct skd_fitmsg_context *skmsg)
1347{
1348 u64 qcmd;
1349 struct fit_msg_hdr *fmh;
1350
Bart Van Asschef98806d2017-08-17 13:12:58 -07001351 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
1352 skmsg->mb_dma_address, skdev->in_flight);
Bart Van Assche6507f432017-08-17 13:13:06 -07001353 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001354
1355 qcmd = skmsg->mb_dma_address;
1356 qcmd |= FIT_QCMD_QID_NORMAL;
1357
Bart Van Assched891fe62017-08-17 13:13:07 -07001358 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001359 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
1360
1361 if (unlikely(skdev->dbg_level > 1)) {
1362 u8 *bp = (u8 *)skmsg->msg_buf;
1363 int i;
1364 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001365 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
1366 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001367 if (i == 0)
1368 i = 64 - 8;
1369 }
1370 }
1371
1372 if (skmsg->length > 256)
1373 qcmd |= FIT_QCMD_MSGSIZE_512;
1374 else if (skmsg->length > 128)
1375 qcmd |= FIT_QCMD_MSGSIZE_256;
1376 else if (skmsg->length > 64)
1377 qcmd |= FIT_QCMD_MSGSIZE_128;
1378 else
1379 /*
1380 * This makes no sense because the FIT msg header is
1381 * 64 bytes. If the msg is only 64 bytes long it has
1382 * no payload.
1383 */
1384 qcmd |= FIT_QCMD_MSGSIZE_64;
1385
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001386 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1387 smp_wmb();
1388
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001389 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001390}
1391
1392static void skd_send_special_fitmsg(struct skd_device *skdev,
1393 struct skd_special_context *skspcl)
1394{
1395 u64 qcmd;
1396
1397 if (unlikely(skdev->dbg_level > 1)) {
1398 u8 *bp = (u8 *)skspcl->msg_buf;
1399 int i;
1400
1401 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001402 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
1403 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001404 if (i == 0)
1405 i = 64 - 8;
1406 }
1407
Bart Van Asschef98806d2017-08-17 13:12:58 -07001408 dev_dbg(&skdev->pdev->dev,
1409 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
1410 skspcl, skspcl->req.id, skspcl->req.sksg_list,
1411 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001412 for (i = 0; i < skspcl->req.n_sg; i++) {
1413 struct fit_sg_descriptor *sgd =
1414 &skspcl->req.sksg_list[i];
1415
Bart Van Asschef98806d2017-08-17 13:12:58 -07001416 dev_dbg(&skdev->pdev->dev,
1417 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1418 i, sgd->byte_count, sgd->control,
1419 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001420 }
1421 }
1422
1423 /*
1424 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
1425 * and one 64-byte SSDI command.
1426 */
1427 qcmd = skspcl->mb_dma_address;
1428 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
1429
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001430 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1431 smp_wmb();
1432
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001433 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
1434}
1435
1436/*
1437 *****************************************************************************
1438 * COMPLETION QUEUE
1439 *****************************************************************************
1440 */
1441
1442static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001443 struct fit_completion_entry_v1 *skcomp,
1444 struct fit_comp_error_info *skerr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001445
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001446struct sns_info {
1447 u8 type;
1448 u8 stat;
1449 u8 key;
1450 u8 asc;
1451 u8 ascq;
1452 u8 mask;
1453 enum skd_check_status_action action;
1454};
1455
1456static struct sns_info skd_chkstat_table[] = {
1457 /* Good */
1458 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
1459 SKD_CHECK_STATUS_REPORT_GOOD },
1460
1461 /* Smart alerts */
1462 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
1463 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1464 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
1465 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1466 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
1467 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1468
1469 /* Retry (with limits) */
1470 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
1471 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1472 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
1473 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1474 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
1475 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1476 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
1477 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1478
1479 /* Busy (or about to be) */
1480 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
1481 SKD_CHECK_STATUS_BUSY_IMMINENT },
1482};
1483
1484/*
1485 * Look up status and sense data to decide how to handle the error
1486 * from the device.
1487 * mask says which fields must match e.g., mask=0x18 means check
1488 * type and stat, ignore key, asc, ascq.
1489 */
1490
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001491static enum skd_check_status_action
1492skd_check_status(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001493 u8 cmp_status, struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001494{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001495 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001496
Bart Van Asschef98806d2017-08-17 13:12:58 -07001497 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1498 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001499
Bart Van Asschef98806d2017-08-17 13:12:58 -07001500 dev_dbg(&skdev->pdev->dev,
1501 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
1502 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
1503 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001504
1505 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001506 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001507 struct sns_info *sns = &skd_chkstat_table[i];
1508
1509 if (sns->mask & 0x10)
1510 if (skerr->type != sns->type)
1511 continue;
1512
1513 if (sns->mask & 0x08)
1514 if (cmp_status != sns->stat)
1515 continue;
1516
1517 if (sns->mask & 0x04)
1518 if (skerr->key != sns->key)
1519 continue;
1520
1521 if (sns->mask & 0x02)
1522 if (skerr->code != sns->asc)
1523 continue;
1524
1525 if (sns->mask & 0x01)
1526 if (skerr->qual != sns->ascq)
1527 continue;
1528
1529 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001530 dev_err(&skdev->pdev->dev,
1531 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
1532 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001533 }
1534 return sns->action;
1535 }
1536
1537 /* No other match, so nonzero status means error,
1538 * zero status means good
1539 */
1540 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001541 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001542 return SKD_CHECK_STATUS_REPORT_ERROR;
1543 }
1544
Bart Van Asschef98806d2017-08-17 13:12:58 -07001545 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001546 return SKD_CHECK_STATUS_REPORT_GOOD;
1547}
1548
1549static void skd_resolve_req_exception(struct skd_device *skdev,
1550 struct skd_request_context *skreq)
1551{
1552 u8 cmp_status = skreq->completion.status;
1553
1554 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
1555 case SKD_CHECK_STATUS_REPORT_GOOD:
1556 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001557 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001558 break;
1559
1560 case SKD_CHECK_STATUS_BUSY_IMMINENT:
1561 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001562 blk_requeue_request(skdev->queue, skreq->req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001563 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001564 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
1565 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
1566 skd_quiesce_dev(skdev);
1567 break;
1568
1569 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06001570 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
1571 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001572 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06001573 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001574 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07001575 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001576
1577 case SKD_CHECK_STATUS_REPORT_ERROR:
1578 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001579 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001580 break;
1581 }
1582}
1583
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001584/* assume spinlock is already held */
1585static void skd_release_skreq(struct skd_device *skdev,
1586 struct skd_request_context *skreq)
1587{
1588 u32 msg_slot;
1589 struct skd_fitmsg_context *skmsg;
1590
1591 u32 timo_slot;
1592
1593 /*
1594 * Reclaim the FIT msg buffer if this is
1595 * the first of the requests it carried to
1596 * be completed. The FIT msg buffer used to
1597 * send this request cannot be reused until
1598 * we are sure the s1120 card has copied
1599 * it to its memory. The FIT msg might have
1600 * contained several requests. As soon as
1601 * any of them are completed we know that
1602 * the entire FIT msg was transferred.
1603 * Only the first completed request will
1604 * match the FIT msg buffer id. The FIT
1605 * msg buffer id is immediately updated.
1606 * When subsequent requests complete the FIT
1607 * msg buffer id won't match, so we know
1608 * quite cheaply that it is already done.
1609 */
1610 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
1611 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
1612
1613 skmsg = &skdev->skmsg_table[msg_slot];
1614 if (skmsg->id == skreq->fitmsg_id) {
1615 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
1616 SKD_ASSERT(skmsg->outstanding > 0);
1617 skmsg->outstanding--;
1618 if (skmsg->outstanding == 0) {
1619 skmsg->state = SKD_MSG_STATE_IDLE;
1620 skmsg->id += SKD_ID_INCR;
1621 skmsg->next = skdev->skmsg_free_list;
1622 skdev->skmsg_free_list = skmsg;
1623 }
1624 }
1625
1626 /*
1627 * Decrease the number of active requests.
1628 * Also decrements the count in the timeout slot.
1629 */
1630 SKD_ASSERT(skdev->in_flight > 0);
1631 skdev->in_flight -= 1;
1632
1633 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
1634 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
1635 skdev->timeout_slot[timo_slot] -= 1;
1636
1637 /*
1638 * Reset backpointer
1639 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06001640 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001641
1642 /*
1643 * Reclaim the skd_request_context
1644 */
1645 skreq->state = SKD_REQ_STATE_IDLE;
1646 skreq->id += SKD_ID_INCR;
1647 skreq->next = skdev->skreq_free_list;
1648 skdev->skreq_free_list = skreq;
1649}
1650
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001651static int skd_isr_completion_posted(struct skd_device *skdev,
1652 int limit, int *enqueued)
1653{
Bart Van Assche85e34112017-08-17 13:13:17 -07001654 struct fit_completion_entry_v1 *skcmp;
1655 struct fit_comp_error_info *skerr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001656 u16 req_id;
1657 u32 req_slot;
1658 struct skd_request_context *skreq;
Bart Van Asschec830da82017-08-17 13:13:13 -07001659 u16 cmp_cntxt;
1660 u8 cmp_status;
1661 u8 cmp_cycle;
1662 u32 cmp_bytes;
1663 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001664 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001665
Bart Van Assche760b48c2017-08-17 13:13:00 -07001666 lockdep_assert_held(&skdev->lock);
1667
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001668 for (;; ) {
1669 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
1670
1671 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
1672 cmp_cycle = skcmp->cycle;
1673 cmp_cntxt = skcmp->tag;
1674 cmp_status = skcmp->status;
1675 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
1676
1677 skerr = &skdev->skerr_table[skdev->skcomp_ix];
1678
Bart Van Asschef98806d2017-08-17 13:12:58 -07001679 dev_dbg(&skdev->pdev->dev,
1680 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
1681 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
1682 cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
1683 skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001684
1685 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001686 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001687 break;
1688 }
1689 /*
1690 * Update the completion queue head index and possibly
1691 * the completion cycle count. 8-bit wrap-around.
1692 */
1693 skdev->skcomp_ix++;
1694 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
1695 skdev->skcomp_ix = 0;
1696 skdev->skcomp_cycle++;
1697 }
1698
1699 /*
1700 * The command context is a unique 32-bit ID. The low order
1701 * bits help locate the request. The request is usually a
1702 * r/w request (see skd_start() above) or a special request.
1703 */
1704 req_id = cmp_cntxt;
1705 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
1706
1707 /* Is this other than a r/w request? */
1708 if (req_slot >= skdev->num_req_context) {
1709 /*
1710 * This is not a completion for a r/w request.
1711 */
1712 skd_complete_other(skdev, skcmp, skerr);
1713 continue;
1714 }
1715
1716 skreq = &skdev->skreq_table[req_slot];
1717
1718 /*
1719 * Make sure the request ID for the slot matches.
1720 */
1721 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001722 dev_dbg(&skdev->pdev->dev,
1723 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
1724 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001725 {
1726 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001727 dev_err(&skdev->pdev->dev,
1728 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
1729 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001730
1731 continue;
1732 }
1733 }
1734
1735 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
1736
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001737 skreq->completion = *skcmp;
1738 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
1739 skreq->err_info = *skerr;
1740 skd_log_check_status(skdev, cmp_status, skerr->key,
1741 skerr->code, skerr->qual,
1742 skerr->fruc);
1743 }
1744 /* Release DMA resources for the request. */
1745 if (skreq->n_sg > 0)
1746 skd_postop_sg_list(skdev, skreq);
1747
Jens Axboefcd37eb2013-11-01 10:14:56 -06001748 if (!skreq->req) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001749 dev_dbg(&skdev->pdev->dev,
1750 "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
1751 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001752 } else {
1753 /*
1754 * Capture the outcome and post it back to the
1755 * native request.
1756 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06001757 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001758 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06001759 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001760 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001761 }
1762
1763 /*
1764 * Release the skreq, its FIT msg (if one), timeout slot,
1765 * and queue depth.
1766 */
1767 skd_release_skreq(skdev, skreq);
1768
1769 /* skd_isr_comp_limit equal zero means no limit */
1770 if (limit) {
1771 if (++processed >= limit) {
1772 rc = 1;
1773 break;
1774 }
1775 }
1776 }
1777
1778 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
1779 && (skdev->in_flight) == 0) {
1780 skdev->state = SKD_DRVR_STATE_PAUSED;
1781 wake_up_interruptible(&skdev->waitq);
1782 }
1783
1784 return rc;
1785}
1786
1787static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001788 struct fit_completion_entry_v1 *skcomp,
1789 struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001790{
1791 u32 req_id = 0;
1792 u32 req_table;
1793 u32 req_slot;
1794 struct skd_special_context *skspcl;
1795
Bart Van Assche760b48c2017-08-17 13:13:00 -07001796 lockdep_assert_held(&skdev->lock);
1797
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001798 req_id = skcomp->tag;
1799 req_table = req_id & SKD_ID_TABLE_MASK;
1800 req_slot = req_id & SKD_ID_SLOT_MASK;
1801
Bart Van Asschef98806d2017-08-17 13:12:58 -07001802 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
1803 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001804
1805 /*
1806 * Based on the request id, determine how to dispatch this completion.
1807 * This swich/case is finding the good cases and forwarding the
1808 * completion entry. Errors are reported below the switch.
1809 */
1810 switch (req_table) {
1811 case SKD_ID_RW_REQUEST:
1812 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07001813 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001814 * handles r/w requests. The only way we get here
1815 * is if the req_slot is out of bounds.
1816 */
1817 break;
1818
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001819 case SKD_ID_INTERNAL:
1820 if (req_slot == 0) {
1821 skspcl = &skdev->internal_skspcl;
1822 if (skspcl->req.id == req_id &&
1823 skspcl->req.state == SKD_REQ_STATE_BUSY) {
1824 skd_complete_internal(skdev,
1825 skcomp, skerr, skspcl);
1826 return;
1827 }
1828 }
1829 break;
1830
1831 case SKD_ID_FIT_MSG:
1832 /*
1833 * These id's should never appear in a completion record.
1834 */
1835 break;
1836
1837 default:
1838 /*
1839 * These id's should never appear anywhere;
1840 */
1841 break;
1842 }
1843
1844 /*
1845 * If we get here it is a bad or stale id.
1846 */
1847}
1848
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001849static void skd_reset_skcomp(struct skd_device *skdev)
1850{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07001851 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001852
1853 skdev->skcomp_ix = 0;
1854 skdev->skcomp_cycle = 1;
1855}
1856
1857/*
1858 *****************************************************************************
1859 * INTERRUPTS
1860 *****************************************************************************
1861 */
1862static void skd_completion_worker(struct work_struct *work)
1863{
1864 struct skd_device *skdev =
1865 container_of(work, struct skd_device, completion_worker);
1866 unsigned long flags;
1867 int flush_enqueued = 0;
1868
1869 spin_lock_irqsave(&skdev->lock, flags);
1870
1871 /*
1872 * pass in limit=0, which means no limit..
1873 * process everything in compq
1874 */
1875 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
Bart Van Assche8fe70062017-08-17 13:13:22 -07001876 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001877
1878 spin_unlock_irqrestore(&skdev->lock, flags);
1879}
1880
1881static void skd_isr_msg_from_dev(struct skd_device *skdev);
1882
Arnd Bergmann41c94992016-11-09 13:55:35 +01001883static irqreturn_t
1884skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001885{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07001886 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001887 u32 intstat;
1888 u32 ack;
1889 int rc = 0;
1890 int deferred = 0;
1891 int flush_enqueued = 0;
1892
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001893 spin_lock(&skdev->lock);
1894
1895 for (;; ) {
1896 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
1897
1898 ack = FIT_INT_DEF_MASK;
1899 ack &= intstat;
1900
Bart Van Asschef98806d2017-08-17 13:12:58 -07001901 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
1902 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001903
1904 /* As long as there is an int pending on device, keep
1905 * running loop. When none, get out, but if we've never
1906 * done any processing, call completion handler?
1907 */
1908 if (ack == 0) {
1909 /* No interrupts on device, but run the completion
1910 * processor anyway?
1911 */
1912 if (rc == 0)
1913 if (likely (skdev->state
1914 == SKD_DRVR_STATE_ONLINE))
1915 deferred = 1;
1916 break;
1917 }
1918
1919 rc = IRQ_HANDLED;
1920
1921 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
1922
1923 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
1924 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
1925 if (intstat & FIT_ISH_COMPLETION_POSTED) {
1926 /*
1927 * If we have already deferred completion
1928 * processing, don't bother running it again
1929 */
1930 if (deferred == 0)
1931 deferred =
1932 skd_isr_completion_posted(skdev,
1933 skd_isr_comp_limit, &flush_enqueued);
1934 }
1935
1936 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
1937 skd_isr_fwstate(skdev);
1938 if (skdev->state == SKD_DRVR_STATE_FAULT ||
1939 skdev->state ==
1940 SKD_DRVR_STATE_DISAPPEARED) {
1941 spin_unlock(&skdev->lock);
1942 return rc;
1943 }
1944 }
1945
1946 if (intstat & FIT_ISH_MSG_FROM_DEV)
1947 skd_isr_msg_from_dev(skdev);
1948 }
1949 }
1950
1951 if (unlikely(flush_enqueued))
Bart Van Assche8fe70062017-08-17 13:13:22 -07001952 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001953
1954 if (deferred)
1955 schedule_work(&skdev->completion_worker);
1956 else if (!flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07001957 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001958
1959 spin_unlock(&skdev->lock);
1960
1961 return rc;
1962}
1963
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001964static void skd_drive_fault(struct skd_device *skdev)
1965{
1966 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001967 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001968}
1969
1970static void skd_drive_disappeared(struct skd_device *skdev)
1971{
1972 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001973 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001974}
1975
1976static void skd_isr_fwstate(struct skd_device *skdev)
1977{
1978 u32 sense;
1979 u32 state;
1980 u32 mtd;
1981 int prev_driver_state = skdev->state;
1982
1983 sense = SKD_READL(skdev, FIT_STATUS);
1984 state = sense & FIT_SR_DRIVE_STATE_MASK;
1985
Bart Van Asschef98806d2017-08-17 13:12:58 -07001986 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
1987 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
1988 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001989
1990 skdev->drive_state = state;
1991
1992 switch (skdev->drive_state) {
1993 case FIT_SR_DRIVE_INIT:
1994 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
1995 skd_disable_interrupts(skdev);
1996 break;
1997 }
1998 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001999 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002000 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2001 skdev->timer_countdown = SKD_STARTING_TIMO;
2002 skdev->state = SKD_DRVR_STATE_STARTING;
2003 skd_soft_reset(skdev);
2004 break;
2005 }
2006 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2007 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2008 skdev->last_mtd = mtd;
2009 break;
2010
2011 case FIT_SR_DRIVE_ONLINE:
2012 skdev->cur_max_queue_depth = skd_max_queue_depth;
2013 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2014 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2015
2016 skdev->queue_low_water_mark =
2017 skdev->cur_max_queue_depth * 2 / 3 + 1;
2018 if (skdev->queue_low_water_mark < 1)
2019 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002020 dev_info(&skdev->pdev->dev,
2021 "Queue depth limit=%d dev=%d lowat=%d\n",
2022 skdev->cur_max_queue_depth,
2023 skdev->dev_max_queue_depth,
2024 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002025
2026 skd_refresh_device_data(skdev);
2027 break;
2028
2029 case FIT_SR_DRIVE_BUSY:
2030 skdev->state = SKD_DRVR_STATE_BUSY;
2031 skdev->timer_countdown = SKD_BUSY_TIMO;
2032 skd_quiesce_dev(skdev);
2033 break;
2034 case FIT_SR_DRIVE_BUSY_SANITIZE:
2035 /* set timer for 3 seconds, we'll abort any unfinished
2036 * commands after that expires
2037 */
2038 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2039 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002040 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002041 break;
2042 case FIT_SR_DRIVE_BUSY_ERASE:
2043 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2044 skdev->timer_countdown = SKD_BUSY_TIMO;
2045 break;
2046 case FIT_SR_DRIVE_OFFLINE:
2047 skdev->state = SKD_DRVR_STATE_IDLE;
2048 break;
2049 case FIT_SR_DRIVE_SOFT_RESET:
2050 switch (skdev->state) {
2051 case SKD_DRVR_STATE_STARTING:
2052 case SKD_DRVR_STATE_RESTARTING:
2053 /* Expected by a caller of skd_soft_reset() */
2054 break;
2055 default:
2056 skdev->state = SKD_DRVR_STATE_RESTARTING;
2057 break;
2058 }
2059 break;
2060 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002061 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002062 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2063 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2064 break;
2065
2066 case FIT_SR_DRIVE_DEGRADED:
2067 case FIT_SR_PCIE_LINK_DOWN:
2068 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
2069 break;
2070
2071 case FIT_SR_DRIVE_FAULT:
2072 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002073 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002074 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002075 break;
2076
2077 /* PCIe bus returned all Fs? */
2078 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002079 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
2080 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002081 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002082 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002083 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002084 break;
2085 default:
2086 /*
2087 * Uknown FW State. Wait for a state we recognize.
2088 */
2089 break;
2090 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07002091 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2092 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
2093 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002094}
2095
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002096static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002097{
2098 int i;
2099
2100 for (i = 0; i < skdev->num_req_context; i++) {
2101 struct skd_request_context *skreq = &skdev->skreq_table[i];
2102
2103 if (skreq->state == SKD_REQ_STATE_BUSY) {
2104 skd_log_skreq(skdev, skreq, "recover");
2105
2106 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002107 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002108
2109 /* Release DMA resources for the request. */
2110 if (skreq->n_sg > 0)
2111 skd_postop_sg_list(skdev, skreq);
2112
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002113 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002114
Jens Axboefcd37eb2013-11-01 10:14:56 -06002115 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002116
2117 skreq->state = SKD_REQ_STATE_IDLE;
2118 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002119 }
2120 if (i > 0)
2121 skreq[-1].next = skreq;
2122 skreq->next = NULL;
2123 }
2124 skdev->skreq_free_list = skdev->skreq_table;
2125
2126 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2127 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
2128
2129 if (skmsg->state == SKD_MSG_STATE_BUSY) {
2130 skd_log_skmsg(skdev, skmsg, "salvaged");
2131 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
2132 skmsg->state = SKD_MSG_STATE_IDLE;
2133 skmsg->id += SKD_ID_INCR;
2134 }
2135 if (i > 0)
2136 skmsg[-1].next = skmsg;
2137 skmsg->next = NULL;
2138 }
2139 skdev->skmsg_free_list = skdev->skmsg_table;
2140
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002141 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
2142 skdev->timeout_slot[i] = 0;
2143
2144 skdev->in_flight = 0;
2145}
2146
2147static void skd_isr_msg_from_dev(struct skd_device *skdev)
2148{
2149 u32 mfd;
2150 u32 mtd;
2151 u32 data;
2152
2153 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
2154
Bart Van Asschef98806d2017-08-17 13:12:58 -07002155 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
2156 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002157
2158 /* ignore any mtd that is an ack for something we didn't send */
2159 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
2160 return;
2161
2162 switch (FIT_MXD_TYPE(mfd)) {
2163 case FIT_MTD_FITFW_INIT:
2164 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
2165
2166 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002167 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
2168 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
2169 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
2170 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002171 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
2172 skd_soft_reset(skdev);
2173 break;
2174 }
2175 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
2176 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2177 skdev->last_mtd = mtd;
2178 break;
2179
2180 case FIT_MTD_GET_CMDQ_DEPTH:
2181 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
2182 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
2183 SKD_N_COMPLETION_ENTRY);
2184 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2185 skdev->last_mtd = mtd;
2186 break;
2187
2188 case FIT_MTD_SET_COMPQ_DEPTH:
2189 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
2190 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
2191 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2192 skdev->last_mtd = mtd;
2193 break;
2194
2195 case FIT_MTD_SET_COMPQ_ADDR:
2196 skd_reset_skcomp(skdev);
2197 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
2198 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2199 skdev->last_mtd = mtd;
2200 break;
2201
2202 case FIT_MTD_CMD_LOG_HOST_ID:
2203 skdev->connect_time_stamp = get_seconds();
2204 data = skdev->connect_time_stamp & 0xFFFF;
2205 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
2206 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2207 skdev->last_mtd = mtd;
2208 break;
2209
2210 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
2211 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
2212 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
2213 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
2214 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2215 skdev->last_mtd = mtd;
2216 break;
2217
2218 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
2219 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
2220 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
2221 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2222 skdev->last_mtd = mtd;
2223
Bart Van Asschef98806d2017-08-17 13:12:58 -07002224 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
2225 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002226 break;
2227
2228 case FIT_MTD_ARM_QUEUE:
2229 skdev->last_mtd = 0;
2230 /*
2231 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
2232 */
2233 break;
2234
2235 default:
2236 break;
2237 }
2238}
2239
2240static void skd_disable_interrupts(struct skd_device *skdev)
2241{
2242 u32 sense;
2243
2244 sense = SKD_READL(skdev, FIT_CONTROL);
2245 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
2246 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002247 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002248
2249 /* Note that the 1s is written. A 1-bit means
2250 * disable, a 0 means enable.
2251 */
2252 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
2253}
2254
2255static void skd_enable_interrupts(struct skd_device *skdev)
2256{
2257 u32 val;
2258
2259 /* unmask interrupts first */
2260 val = FIT_ISH_FW_STATE_CHANGE +
2261 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
2262
2263 /* Note that the compliment of mask is written. A 1-bit means
2264 * disable, a 0 means enable. */
2265 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002266 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002267
2268 val = SKD_READL(skdev, FIT_CONTROL);
2269 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002270 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002271 SKD_WRITEL(skdev, val, FIT_CONTROL);
2272}
2273
2274/*
2275 *****************************************************************************
2276 * START, STOP, RESTART, QUIESCE, UNQUIESCE
2277 *****************************************************************************
2278 */
2279
2280static void skd_soft_reset(struct skd_device *skdev)
2281{
2282 u32 val;
2283
2284 val = SKD_READL(skdev, FIT_CONTROL);
2285 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002286 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002287 SKD_WRITEL(skdev, val, FIT_CONTROL);
2288}
2289
2290static void skd_start_device(struct skd_device *skdev)
2291{
2292 unsigned long flags;
2293 u32 sense;
2294 u32 state;
2295
2296 spin_lock_irqsave(&skdev->lock, flags);
2297
2298 /* ack all ghost interrupts */
2299 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2300
2301 sense = SKD_READL(skdev, FIT_STATUS);
2302
Bart Van Asschef98806d2017-08-17 13:12:58 -07002303 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002304
2305 state = sense & FIT_SR_DRIVE_STATE_MASK;
2306 skdev->drive_state = state;
2307 skdev->last_mtd = 0;
2308
2309 skdev->state = SKD_DRVR_STATE_STARTING;
2310 skdev->timer_countdown = SKD_STARTING_TIMO;
2311
2312 skd_enable_interrupts(skdev);
2313
2314 switch (skdev->drive_state) {
2315 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002316 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002317 break;
2318
2319 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002320 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002321 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2322 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2323 break;
2324
2325 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002326 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002327 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2328 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2329 break;
2330
2331 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002332 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002333 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2334 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2335 break;
2336
2337 case FIT_SR_DRIVE_INIT:
2338 case FIT_SR_DRIVE_ONLINE:
2339 skd_soft_reset(skdev);
2340 break;
2341
2342 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002343 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002344 skdev->state = SKD_DRVR_STATE_BUSY;
2345 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2346 break;
2347
2348 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002349 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002350 break;
2351
2352 case FIT_SR_DRIVE_FAULT:
2353 /* Fault state is bad...soft reset won't do it...
2354 * Hard reset, maybe, but does it work on device?
2355 * For now, just fault so the system doesn't hang.
2356 */
2357 skd_drive_fault(skdev);
2358 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002359 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002360 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002361 skdev->gendisk_on = -1;
2362 wake_up_interruptible(&skdev->waitq);
2363 break;
2364
2365 case 0xFF:
2366 /* Most likely the device isn't there or isn't responding
2367 * to the BAR1 addresses. */
2368 skd_drive_disappeared(skdev);
2369 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002370 dev_dbg(&skdev->pdev->dev,
2371 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002372 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002373 skdev->gendisk_on = -1;
2374 wake_up_interruptible(&skdev->waitq);
2375 break;
2376
2377 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002378 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
2379 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002380 break;
2381 }
2382
2383 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002384 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002385
2386 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002387 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002388
2389 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002390 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002391
2392 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002393 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002394
2395 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002396 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002397
2398 spin_unlock_irqrestore(&skdev->lock, flags);
2399}
2400
2401static void skd_stop_device(struct skd_device *skdev)
2402{
2403 unsigned long flags;
2404 struct skd_special_context *skspcl = &skdev->internal_skspcl;
2405 u32 dev_state;
2406 int i;
2407
2408 spin_lock_irqsave(&skdev->lock, flags);
2409
2410 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002411 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002412 goto stop_out;
2413 }
2414
2415 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002416 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002417 goto stop_out;
2418 }
2419
2420 skdev->state = SKD_DRVR_STATE_SYNCING;
2421 skdev->sync_done = 0;
2422
2423 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
2424
2425 spin_unlock_irqrestore(&skdev->lock, flags);
2426
2427 wait_event_interruptible_timeout(skdev->waitq,
2428 (skdev->sync_done), (10 * HZ));
2429
2430 spin_lock_irqsave(&skdev->lock, flags);
2431
2432 switch (skdev->sync_done) {
2433 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002434 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002435 break;
2436 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002437 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002438 break;
2439 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002440 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002441 }
2442
2443stop_out:
2444 skdev->state = SKD_DRVR_STATE_STOPPING;
2445 spin_unlock_irqrestore(&skdev->lock, flags);
2446
2447 skd_kill_timer(skdev);
2448
2449 spin_lock_irqsave(&skdev->lock, flags);
2450 skd_disable_interrupts(skdev);
2451
2452 /* ensure all ints on device are cleared */
2453 /* soft reset the device to unload with a clean slate */
2454 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2455 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
2456
2457 spin_unlock_irqrestore(&skdev->lock, flags);
2458
2459 /* poll every 100ms, 1 second timeout */
2460 for (i = 0; i < 10; i++) {
2461 dev_state =
2462 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
2463 if (dev_state == FIT_SR_DRIVE_INIT)
2464 break;
2465 set_current_state(TASK_INTERRUPTIBLE);
2466 schedule_timeout(msecs_to_jiffies(100));
2467 }
2468
2469 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07002470 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
2471 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002472}
2473
2474/* assume spinlock is held */
2475static void skd_restart_device(struct skd_device *skdev)
2476{
2477 u32 state;
2478
2479 /* ack all ghost interrupts */
2480 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2481
2482 state = SKD_READL(skdev, FIT_STATUS);
2483
Bart Van Asschef98806d2017-08-17 13:12:58 -07002484 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002485
2486 state &= FIT_SR_DRIVE_STATE_MASK;
2487 skdev->drive_state = state;
2488 skdev->last_mtd = 0;
2489
2490 skdev->state = SKD_DRVR_STATE_RESTARTING;
2491 skdev->timer_countdown = SKD_RESTARTING_TIMO;
2492
2493 skd_soft_reset(skdev);
2494}
2495
2496/* assume spinlock is held */
2497static int skd_quiesce_dev(struct skd_device *skdev)
2498{
2499 int rc = 0;
2500
2501 switch (skdev->state) {
2502 case SKD_DRVR_STATE_BUSY:
2503 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002504 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002505 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002506 break;
2507 case SKD_DRVR_STATE_ONLINE:
2508 case SKD_DRVR_STATE_STOPPING:
2509 case SKD_DRVR_STATE_SYNCING:
2510 case SKD_DRVR_STATE_PAUSING:
2511 case SKD_DRVR_STATE_PAUSED:
2512 case SKD_DRVR_STATE_STARTING:
2513 case SKD_DRVR_STATE_RESTARTING:
2514 case SKD_DRVR_STATE_RESUMING:
2515 default:
2516 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002517 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
2518 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002519 }
2520 return rc;
2521}
2522
2523/* assume spinlock is held */
2524static int skd_unquiesce_dev(struct skd_device *skdev)
2525{
2526 int prev_driver_state = skdev->state;
2527
2528 skd_log_skdev(skdev, "unquiesce");
2529 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002530 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002531 return 0;
2532 }
2533 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
2534 /*
2535 * If there has been an state change to other than
2536 * ONLINE, we will rely on controller state change
2537 * to come back online and restart the queue.
2538 * The BUSY state means that driver is ready to
2539 * continue normal processing but waiting for controller
2540 * to become available.
2541 */
2542 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002543 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002544 return 0;
2545 }
2546
2547 /*
2548 * Drive has just come online, driver is either in startup,
2549 * paused performing a task, or bust waiting for hardware.
2550 */
2551 switch (skdev->state) {
2552 case SKD_DRVR_STATE_PAUSED:
2553 case SKD_DRVR_STATE_BUSY:
2554 case SKD_DRVR_STATE_BUSY_IMMINENT:
2555 case SKD_DRVR_STATE_BUSY_ERASE:
2556 case SKD_DRVR_STATE_STARTING:
2557 case SKD_DRVR_STATE_RESTARTING:
2558 case SKD_DRVR_STATE_FAULT:
2559 case SKD_DRVR_STATE_IDLE:
2560 case SKD_DRVR_STATE_LOAD:
2561 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002562 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2563 skd_skdev_state_to_str(prev_driver_state),
2564 prev_driver_state, skd_skdev_state_to_str(skdev->state),
2565 skdev->state);
2566 dev_dbg(&skdev->pdev->dev,
2567 "**** device ONLINE...starting block queue\n");
2568 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2569 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002570 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002571 skdev->gendisk_on = 1;
2572 wake_up_interruptible(&skdev->waitq);
2573 break;
2574
2575 case SKD_DRVR_STATE_DISAPPEARED:
2576 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002577 dev_dbg(&skdev->pdev->dev,
2578 "**** driver state %d, not implemented\n",
2579 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002580 return -EBUSY;
2581 }
2582 return 0;
2583}
2584
2585/*
2586 *****************************************************************************
2587 * PCIe MSI/MSI-X INTERRUPT HANDLERS
2588 *****************************************************************************
2589 */
2590
2591static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
2592{
2593 struct skd_device *skdev = skd_host_data;
2594 unsigned long flags;
2595
2596 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002597 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2598 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2599 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
2600 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002601 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
2602 spin_unlock_irqrestore(&skdev->lock, flags);
2603 return IRQ_HANDLED;
2604}
2605
2606static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
2607{
2608 struct skd_device *skdev = skd_host_data;
2609 unsigned long flags;
2610
2611 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002612 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2613 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002614 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
2615 skd_isr_fwstate(skdev);
2616 spin_unlock_irqrestore(&skdev->lock, flags);
2617 return IRQ_HANDLED;
2618}
2619
2620static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
2621{
2622 struct skd_device *skdev = skd_host_data;
2623 unsigned long flags;
2624 int flush_enqueued = 0;
2625 int deferred;
2626
2627 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002628 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2629 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002630 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
2631 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
2632 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002633 if (flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07002634 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002635
2636 if (deferred)
2637 schedule_work(&skdev->completion_worker);
2638 else if (!flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07002639 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002640
2641 spin_unlock_irqrestore(&skdev->lock, flags);
2642
2643 return IRQ_HANDLED;
2644}
2645
2646static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
2647{
2648 struct skd_device *skdev = skd_host_data;
2649 unsigned long flags;
2650
2651 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002652 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2653 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002654 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
2655 skd_isr_msg_from_dev(skdev);
2656 spin_unlock_irqrestore(&skdev->lock, flags);
2657 return IRQ_HANDLED;
2658}
2659
2660static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
2661{
2662 struct skd_device *skdev = skd_host_data;
2663 unsigned long flags;
2664
2665 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002666 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2667 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002668 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
2669 spin_unlock_irqrestore(&skdev->lock, flags);
2670 return IRQ_HANDLED;
2671}
2672
2673/*
2674 *****************************************************************************
2675 * PCIe MSI/MSI-X SETUP
2676 *****************************************************************************
2677 */
2678
2679struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002680 char isr_name[30];
2681};
2682
2683struct skd_init_msix_entry {
2684 const char *name;
2685 irq_handler_t handler;
2686};
2687
2688#define SKD_MAX_MSIX_COUNT 13
2689#define SKD_MIN_MSIX_COUNT 7
2690#define SKD_BASE_MSIX_IRQ 4
2691
2692static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
2693 { "(DMA 0)", skd_reserved_isr },
2694 { "(DMA 1)", skd_reserved_isr },
2695 { "(DMA 2)", skd_reserved_isr },
2696 { "(DMA 3)", skd_reserved_isr },
2697 { "(State Change)", skd_statec_isr },
2698 { "(COMPL_Q)", skd_comp_q },
2699 { "(MSG)", skd_msg_isr },
2700 { "(Reserved)", skd_reserved_isr },
2701 { "(Reserved)", skd_reserved_isr },
2702 { "(Queue Full 0)", skd_qfull_isr },
2703 { "(Queue Full 1)", skd_qfull_isr },
2704 { "(Queue Full 2)", skd_qfull_isr },
2705 { "(Queue Full 3)", skd_qfull_isr },
2706};
2707
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002708static int skd_acquire_msix(struct skd_device *skdev)
2709{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01002710 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01002711 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002712
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002713 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
2714 PCI_IRQ_MSIX);
2715 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002716 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002717 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002718 }
Alexander Gordeev46817762014-02-19 09:58:19 +01002719
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002720 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
2721 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002722 if (!skdev->msix_entries) {
2723 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002724 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002725 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002726 }
2727
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002728 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002729 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2730 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
2731
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002732 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
2733 "%s%d-msix %s", DRV_NAME, skdev->devno,
2734 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002735
2736 rc = devm_request_irq(&skdev->pdev->dev,
2737 pci_irq_vector(skdev->pdev, i),
2738 msix_entries[i].handler, 0,
2739 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002740 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002741 dev_err(&skdev->pdev->dev,
2742 "Unable to register(%d) MSI-X handler %d: %s\n",
2743 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002744 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002745 }
2746 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002747
Bart Van Asschef98806d2017-08-17 13:12:58 -07002748 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
2749 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002750 return 0;
2751
2752msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002753 while (--i >= 0)
2754 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002755out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002756 kfree(skdev->msix_entries);
2757 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002758 return rc;
2759}
2760
2761static int skd_acquire_irq(struct skd_device *skdev)
2762{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002763 struct pci_dev *pdev = skdev->pdev;
2764 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002765 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002766
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002767 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002768 rc = skd_acquire_msix(skdev);
2769 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002770 return 0;
2771
Bart Van Asschef98806d2017-08-17 13:12:58 -07002772 dev_err(&skdev->pdev->dev,
2773 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002774 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002775
2776 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
2777 skdev->devno);
2778
2779 if (skd_isr_type != SKD_IRQ_LEGACY)
2780 irq_flag |= PCI_IRQ_MSI;
2781 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
2782 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002783 dev_err(&skdev->pdev->dev,
2784 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002785 return rc;
2786 }
2787
2788 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
2789 pdev->msi_enabled ? 0 : IRQF_SHARED,
2790 skdev->isr_name, skdev);
2791 if (rc) {
2792 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002793 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
2794 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002795 return rc;
2796 }
2797
2798 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002799}
2800
2801static void skd_release_irq(struct skd_device *skdev)
2802{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002803 struct pci_dev *pdev = skdev->pdev;
2804
2805 if (skdev->msix_entries) {
2806 int i;
2807
2808 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2809 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
2810 skdev);
2811 }
2812
2813 kfree(skdev->msix_entries);
2814 skdev->msix_entries = NULL;
2815 } else {
2816 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002817 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002818
2819 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002820}
2821
2822/*
2823 *****************************************************************************
2824 * CONSTRUCT
2825 *****************************************************************************
2826 */
2827
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002828static int skd_cons_skcomp(struct skd_device *skdev)
2829{
2830 int rc = 0;
2831 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002832
Bart Van Asschef98806d2017-08-17 13:12:58 -07002833 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002834 "comp pci_alloc, total bytes %zd entries %d\n",
2835 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002836
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002837 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07002838 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002839
2840 if (skcomp == NULL) {
2841 rc = -ENOMEM;
2842 goto err_out;
2843 }
2844
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002845 skdev->skcomp_table = skcomp;
2846 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
2847 sizeof(*skcomp) *
2848 SKD_N_COMPLETION_ENTRY);
2849
2850err_out:
2851 return rc;
2852}
2853
2854static int skd_cons_skmsg(struct skd_device *skdev)
2855{
2856 int rc = 0;
2857 u32 i;
2858
Bart Van Asschef98806d2017-08-17 13:12:58 -07002859 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002860 "skmsg_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002861 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
2862 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002863
Bart Van Assche01433d02017-08-17 13:13:18 -07002864 skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context,
2865 sizeof(struct skd_fitmsg_context),
2866 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002867 if (skdev->skmsg_table == NULL) {
2868 rc = -ENOMEM;
2869 goto err_out;
2870 }
2871
2872 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2873 struct skd_fitmsg_context *skmsg;
2874
2875 skmsg = &skdev->skmsg_table[i];
2876
2877 skmsg->id = i + SKD_ID_FIT_MSG;
2878
2879 skmsg->state = SKD_MSG_STATE_IDLE;
2880 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07002881 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002882 &skmsg->mb_dma_address);
2883
2884 if (skmsg->msg_buf == NULL) {
2885 rc = -ENOMEM;
2886 goto err_out;
2887 }
2888
Bart Van Assche6507f432017-08-17 13:13:06 -07002889 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
2890 (FIT_QCMD_ALIGN - 1),
2891 "not aligned: msg_buf %p mb_dma_address %#llx\n",
2892 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002893 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
2894
2895 skmsg->next = &skmsg[1];
2896 }
2897
2898 /* Free list is in order starting with the 0th entry. */
2899 skdev->skmsg_table[i - 1].next = NULL;
2900 skdev->skmsg_free_list = skdev->skmsg_table;
2901
2902err_out:
2903 return rc;
2904}
2905
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002906static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
2907 u32 n_sg,
2908 dma_addr_t *ret_dma_addr)
2909{
2910 struct fit_sg_descriptor *sg_list;
2911 u32 nbytes;
2912
2913 nbytes = sizeof(*sg_list) * n_sg;
2914
2915 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
2916
2917 if (sg_list != NULL) {
2918 uint64_t dma_address = *ret_dma_addr;
2919 u32 i;
2920
2921 memset(sg_list, 0, nbytes);
2922
2923 for (i = 0; i < n_sg - 1; i++) {
2924 uint64_t ndp_off;
2925 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
2926
2927 sg_list[i].next_desc_ptr = dma_address + ndp_off;
2928 }
2929 sg_list[i].next_desc_ptr = 0LL;
2930 }
2931
2932 return sg_list;
2933}
2934
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002935static int skd_cons_skreq(struct skd_device *skdev)
2936{
2937 int rc = 0;
2938 u32 i;
2939
Bart Van Asschef98806d2017-08-17 13:12:58 -07002940 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002941 "skreq_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002942 sizeof(struct skd_request_context), skdev->num_req_context,
2943 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002944
Bart Van Assche01433d02017-08-17 13:13:18 -07002945 skdev->skreq_table = kcalloc(skdev->num_req_context,
2946 sizeof(struct skd_request_context),
2947 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002948 if (skdev->skreq_table == NULL) {
2949 rc = -ENOMEM;
2950 goto err_out;
2951 }
2952
Bart Van Asschef98806d2017-08-17 13:12:58 -07002953 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
2954 skdev->sgs_per_request, sizeof(struct scatterlist),
2955 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002956
2957 for (i = 0; i < skdev->num_req_context; i++) {
2958 struct skd_request_context *skreq;
2959
2960 skreq = &skdev->skreq_table[i];
2961
2962 skreq->id = i + SKD_ID_RW_REQUEST;
2963 skreq->state = SKD_REQ_STATE_IDLE;
2964
Bart Van Assche01433d02017-08-17 13:13:18 -07002965 skreq->sg = kcalloc(skdev->sgs_per_request,
2966 sizeof(struct scatterlist), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002967 if (skreq->sg == NULL) {
2968 rc = -ENOMEM;
2969 goto err_out;
2970 }
2971 sg_init_table(skreq->sg, skdev->sgs_per_request);
2972
2973 skreq->sksg_list = skd_cons_sg_list(skdev,
2974 skdev->sgs_per_request,
2975 &skreq->sksg_dma_address);
2976
2977 if (skreq->sksg_list == NULL) {
2978 rc = -ENOMEM;
2979 goto err_out;
2980 }
2981
2982 skreq->next = &skreq[1];
2983 }
2984
2985 /* Free list is in order starting with the 0th entry. */
2986 skdev->skreq_table[i - 1].next = NULL;
2987 skdev->skreq_free_list = skdev->skreq_table;
2988
2989err_out:
2990 return rc;
2991}
2992
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002993static int skd_cons_sksb(struct skd_device *skdev)
2994{
2995 int rc = 0;
2996 struct skd_special_context *skspcl;
2997 u32 nbytes;
2998
2999 skspcl = &skdev->internal_skspcl;
3000
3001 skspcl->req.id = 0 + SKD_ID_INTERNAL;
3002 skspcl->req.state = SKD_REQ_STATE_IDLE;
3003
3004 nbytes = SKD_N_INTERNAL_BYTES;
3005
Joe Perchesa5bbf612014-08-08 14:24:12 -07003006 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
3007 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003008 if (skspcl->data_buf == NULL) {
3009 rc = -ENOMEM;
3010 goto err_out;
3011 }
3012
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003013 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07003014 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
3015 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003016 if (skspcl->msg_buf == NULL) {
3017 rc = -ENOMEM;
3018 goto err_out;
3019 }
3020
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003021 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
3022 &skspcl->req.sksg_dma_address);
3023 if (skspcl->req.sksg_list == NULL) {
3024 rc = -ENOMEM;
3025 goto err_out;
3026 }
3027
3028 if (!skd_format_internal_skspcl(skdev)) {
3029 rc = -EINVAL;
3030 goto err_out;
3031 }
3032
3033err_out:
3034 return rc;
3035}
3036
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003037static int skd_cons_disk(struct skd_device *skdev)
3038{
3039 int rc = 0;
3040 struct gendisk *disk;
3041 struct request_queue *q;
3042 unsigned long flags;
3043
3044 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
3045 if (!disk) {
3046 rc = -ENOMEM;
3047 goto err_out;
3048 }
3049
3050 skdev->disk = disk;
3051 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
3052
3053 disk->major = skdev->major;
3054 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
3055 disk->fops = &skd_blockdev_ops;
3056 disk->private_data = skdev;
3057
Jens Axboefcd37eb2013-11-01 10:14:56 -06003058 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003059 if (!q) {
3060 rc = -ENOMEM;
3061 goto err_out;
3062 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02003063 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003064
3065 skdev->queue = q;
3066 disk->queue = q;
3067 q->queuedata = skdev;
3068
Jens Axboe6975f732016-03-30 10:11:42 -06003069 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003070 blk_queue_max_segments(q, skdev->sgs_per_request);
3071 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
3072
Bart Van Asschea5c5b392017-08-17 13:12:53 -07003073 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003074 blk_queue_io_opt(q, 8192);
3075
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003076 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06003077 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003078
3079 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003080 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003081 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003082 spin_unlock_irqrestore(&skdev->lock, flags);
3083
3084err_out:
3085 return rc;
3086}
3087
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003088#define SKD_N_DEV_TABLE 16u
3089static u32 skd_next_devno;
3090
3091static struct skd_device *skd_construct(struct pci_dev *pdev)
3092{
3093 struct skd_device *skdev;
3094 int blk_major = skd_major;
3095 int rc;
3096
3097 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
3098
3099 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003100 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003101 return NULL;
3102 }
3103
3104 skdev->state = SKD_DRVR_STATE_LOAD;
3105 skdev->pdev = pdev;
3106 skdev->devno = skd_next_devno++;
3107 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003108 skdev->dev_max_queue_depth = 0;
3109
3110 skdev->num_req_context = skd_max_queue_depth;
3111 skdev->num_fitmsg_context = skd_max_queue_depth;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003112 skdev->cur_max_queue_depth = 1;
3113 skdev->queue_low_water_mark = 1;
3114 skdev->proto_ver = 99;
3115 skdev->sgs_per_request = skd_sgs_per_request;
3116 skdev->dbg_level = skd_dbg_level;
3117
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003118 spin_lock_init(&skdev->lock);
3119
3120 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
3121
Bart Van Asschef98806d2017-08-17 13:12:58 -07003122 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003123 rc = skd_cons_skcomp(skdev);
3124 if (rc < 0)
3125 goto err_out;
3126
Bart Van Asschef98806d2017-08-17 13:12:58 -07003127 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003128 rc = skd_cons_skmsg(skdev);
3129 if (rc < 0)
3130 goto err_out;
3131
Bart Van Asschef98806d2017-08-17 13:12:58 -07003132 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003133 rc = skd_cons_skreq(skdev);
3134 if (rc < 0)
3135 goto err_out;
3136
Bart Van Asschef98806d2017-08-17 13:12:58 -07003137 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003138 rc = skd_cons_sksb(skdev);
3139 if (rc < 0)
3140 goto err_out;
3141
Bart Van Asschef98806d2017-08-17 13:12:58 -07003142 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003143 rc = skd_cons_disk(skdev);
3144 if (rc < 0)
3145 goto err_out;
3146
Bart Van Asschef98806d2017-08-17 13:12:58 -07003147 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003148 return skdev;
3149
3150err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003151 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003152 skd_destruct(skdev);
3153 return NULL;
3154}
3155
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003156/*
3157 *****************************************************************************
3158 * DESTRUCT (FREE)
3159 *****************************************************************************
3160 */
3161
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003162static void skd_free_skcomp(struct skd_device *skdev)
3163{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07003164 if (skdev->skcomp_table)
3165 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003166 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003167
3168 skdev->skcomp_table = NULL;
3169 skdev->cq_dma_address = 0;
3170}
3171
3172static void skd_free_skmsg(struct skd_device *skdev)
3173{
3174 u32 i;
3175
3176 if (skdev->skmsg_table == NULL)
3177 return;
3178
3179 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3180 struct skd_fitmsg_context *skmsg;
3181
3182 skmsg = &skdev->skmsg_table[i];
3183
3184 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003185 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
3186 skmsg->msg_buf,
3187 skmsg->mb_dma_address);
3188 }
3189 skmsg->msg_buf = NULL;
3190 skmsg->mb_dma_address = 0;
3191 }
3192
3193 kfree(skdev->skmsg_table);
3194 skdev->skmsg_table = NULL;
3195}
3196
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003197static void skd_free_sg_list(struct skd_device *skdev,
3198 struct fit_sg_descriptor *sg_list,
3199 u32 n_sg, dma_addr_t dma_addr)
3200{
3201 if (sg_list != NULL) {
3202 u32 nbytes;
3203
3204 nbytes = sizeof(*sg_list) * n_sg;
3205
3206 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
3207 }
3208}
3209
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003210static void skd_free_skreq(struct skd_device *skdev)
3211{
3212 u32 i;
3213
3214 if (skdev->skreq_table == NULL)
3215 return;
3216
3217 for (i = 0; i < skdev->num_req_context; i++) {
3218 struct skd_request_context *skreq;
3219
3220 skreq = &skdev->skreq_table[i];
3221
3222 skd_free_sg_list(skdev, skreq->sksg_list,
3223 skdev->sgs_per_request,
3224 skreq->sksg_dma_address);
3225
3226 skreq->sksg_list = NULL;
3227 skreq->sksg_dma_address = 0;
3228
3229 kfree(skreq->sg);
3230 }
3231
3232 kfree(skdev->skreq_table);
3233 skdev->skreq_table = NULL;
3234}
3235
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003236static void skd_free_sksb(struct skd_device *skdev)
3237{
3238 struct skd_special_context *skspcl;
3239 u32 nbytes;
3240
3241 skspcl = &skdev->internal_skspcl;
3242
3243 if (skspcl->data_buf != NULL) {
3244 nbytes = SKD_N_INTERNAL_BYTES;
3245
3246 pci_free_consistent(skdev->pdev, nbytes,
3247 skspcl->data_buf, skspcl->db_dma_address);
3248 }
3249
3250 skspcl->data_buf = NULL;
3251 skspcl->db_dma_address = 0;
3252
3253 if (skspcl->msg_buf != NULL) {
3254 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3255 pci_free_consistent(skdev->pdev, nbytes,
3256 skspcl->msg_buf, skspcl->mb_dma_address);
3257 }
3258
3259 skspcl->msg_buf = NULL;
3260 skspcl->mb_dma_address = 0;
3261
3262 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
3263 skspcl->req.sksg_dma_address);
3264
3265 skspcl->req.sksg_list = NULL;
3266 skspcl->req.sksg_dma_address = 0;
3267}
3268
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003269static void skd_free_disk(struct skd_device *skdev)
3270{
3271 struct gendisk *disk = skdev->disk;
3272
Bart Van Assche7277cc62017-08-17 13:12:45 -07003273 if (disk && (disk->flags & GENHD_FL_UP))
3274 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003275
Bart Van Assche7277cc62017-08-17 13:12:45 -07003276 if (skdev->queue) {
3277 blk_cleanup_queue(skdev->queue);
3278 skdev->queue = NULL;
3279 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003280 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07003281
3282 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003283 skdev->disk = NULL;
3284}
3285
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003286static void skd_destruct(struct skd_device *skdev)
3287{
3288 if (skdev == NULL)
3289 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003290
Bart Van Asschef98806d2017-08-17 13:12:58 -07003291 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003292 skd_free_disk(skdev);
3293
Bart Van Asschef98806d2017-08-17 13:12:58 -07003294 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003295 skd_free_sksb(skdev);
3296
Bart Van Asschef98806d2017-08-17 13:12:58 -07003297 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003298 skd_free_skreq(skdev);
3299
Bart Van Asschef98806d2017-08-17 13:12:58 -07003300 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003301 skd_free_skmsg(skdev);
3302
Bart Van Asschef98806d2017-08-17 13:12:58 -07003303 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003304 skd_free_skcomp(skdev);
3305
Bart Van Asschef98806d2017-08-17 13:12:58 -07003306 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003307 kfree(skdev);
3308}
3309
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003310/*
3311 *****************************************************************************
3312 * BLOCK DEVICE (BDEV) GLUE
3313 *****************************************************************************
3314 */
3315
3316static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3317{
3318 struct skd_device *skdev;
3319 u64 capacity;
3320
3321 skdev = bdev->bd_disk->private_data;
3322
Bart Van Asschef98806d2017-08-17 13:12:58 -07003323 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
3324 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003325
3326 if (skdev->read_cap_is_valid) {
3327 capacity = get_capacity(skdev->disk);
3328 geo->heads = 64;
3329 geo->sectors = 255;
3330 geo->cylinders = (capacity) / (255 * 64);
3331
3332 return 0;
3333 }
3334 return -EIO;
3335}
3336
Dan Williams0d52c7562016-06-15 19:44:20 -07003337static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003338{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003339 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07003340 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003341 return 0;
3342}
3343
3344static const struct block_device_operations skd_blockdev_ops = {
3345 .owner = THIS_MODULE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003346 .getgeo = skd_bdev_getgeo,
3347};
3348
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003349/*
3350 *****************************************************************************
3351 * PCIe DRIVER GLUE
3352 *****************************************************************************
3353 */
3354
Benoit Taine9baa3c32014-08-08 15:56:03 +02003355static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003356 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
3357 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
3358 { 0 } /* terminate list */
3359};
3360
3361MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
3362
3363static char *skd_pci_info(struct skd_device *skdev, char *str)
3364{
3365 int pcie_reg;
3366
3367 strcpy(str, "PCIe (");
3368 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
3369
3370 if (pcie_reg) {
3371
3372 char lwstr[6];
3373 uint16_t pcie_lstat, lspeed, lwidth;
3374
3375 pcie_reg += 0x12;
3376 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
3377 lspeed = pcie_lstat & (0xF);
3378 lwidth = (pcie_lstat & 0x3F0) >> 4;
3379
3380 if (lspeed == 1)
3381 strcat(str, "2.5GT/s ");
3382 else if (lspeed == 2)
3383 strcat(str, "5.0GT/s ");
3384 else
3385 strcat(str, "<unknown> ");
3386 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
3387 strcat(str, lwstr);
3388 }
3389 return str;
3390}
3391
3392static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3393{
3394 int i;
3395 int rc = 0;
3396 char pci_str[32];
3397 struct skd_device *skdev;
3398
Bart Van Asschef98806d2017-08-17 13:12:58 -07003399 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
3400 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
3401 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
3402 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003403
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))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003413 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3414 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003415 }
3416 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003417 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003418 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003419 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003420 goto err_out_regions;
3421 }
3422 }
3423
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003424 if (!skd_major) {
3425 rc = register_blkdev(0, DRV_NAME);
3426 if (rc < 0)
3427 goto err_out_regions;
3428 BUG_ON(!rc);
3429 skd_major = rc;
3430 }
3431
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003432 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08003433 if (skdev == NULL) {
3434 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003435 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08003436 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003437
3438 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003439 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003440
3441 pci_set_master(pdev);
3442 rc = pci_enable_pcie_error_reporting(pdev);
3443 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003444 dev_err(&pdev->dev,
3445 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003446 skdev->pcie_error_reporting_is_enabled = 0;
3447 } else
3448 skdev->pcie_error_reporting_is_enabled = 1;
3449
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003450 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01003451
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003452 for (i = 0; i < SKD_MAX_BARS; i++) {
3453 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3454 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3455 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3456 skdev->mem_size[i]);
3457 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003458 dev_err(&pdev->dev,
3459 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003460 rc = -ENODEV;
3461 goto err_out_iounmap;
3462 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003463 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3464 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3465 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003466 }
3467
3468 rc = skd_acquire_irq(skdev);
3469 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003470 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003471 goto err_out_iounmap;
3472 }
3473
3474 rc = skd_start_timer(skdev);
3475 if (rc)
3476 goto err_out_timer;
3477
3478 init_waitqueue_head(&skdev->waitq);
3479
3480 skd_start_device(skdev);
3481
3482 rc = wait_event_interruptible_timeout(skdev->waitq,
3483 (skdev->gendisk_on),
3484 (SKD_START_WAIT_SECONDS * HZ));
3485 if (skdev->gendisk_on > 0) {
3486 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07003487 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003488 rc = 0;
3489 } else {
3490 /* we timed out, something is wrong with the device,
3491 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003492 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
3493 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003494 /* in case of no error; we timeout with ENXIO */
3495 if (!rc)
3496 rc = -ENXIO;
3497 goto err_out_timer;
3498 }
3499
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003500 return rc;
3501
3502err_out_timer:
3503 skd_stop_device(skdev);
3504 skd_release_irq(skdev);
3505
3506err_out_iounmap:
3507 for (i = 0; i < SKD_MAX_BARS; i++)
3508 if (skdev->mem_map[i])
3509 iounmap(skdev->mem_map[i]);
3510
3511 if (skdev->pcie_error_reporting_is_enabled)
3512 pci_disable_pcie_error_reporting(pdev);
3513
3514 skd_destruct(skdev);
3515
3516err_out_regions:
3517 pci_release_regions(pdev);
3518
3519err_out:
3520 pci_disable_device(pdev);
3521 pci_set_drvdata(pdev, NULL);
3522 return rc;
3523}
3524
3525static void skd_pci_remove(struct pci_dev *pdev)
3526{
3527 int i;
3528 struct skd_device *skdev;
3529
3530 skdev = pci_get_drvdata(pdev);
3531 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003532 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003533 return;
3534 }
3535 skd_stop_device(skdev);
3536 skd_release_irq(skdev);
3537
3538 for (i = 0; i < SKD_MAX_BARS; i++)
3539 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003540 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003541
3542 if (skdev->pcie_error_reporting_is_enabled)
3543 pci_disable_pcie_error_reporting(pdev);
3544
3545 skd_destruct(skdev);
3546
3547 pci_release_regions(pdev);
3548 pci_disable_device(pdev);
3549 pci_set_drvdata(pdev, NULL);
3550
3551 return;
3552}
3553
3554static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3555{
3556 int i;
3557 struct skd_device *skdev;
3558
3559 skdev = pci_get_drvdata(pdev);
3560 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003561 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003562 return -EIO;
3563 }
3564
3565 skd_stop_device(skdev);
3566
3567 skd_release_irq(skdev);
3568
3569 for (i = 0; i < SKD_MAX_BARS; i++)
3570 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003571 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003572
3573 if (skdev->pcie_error_reporting_is_enabled)
3574 pci_disable_pcie_error_reporting(pdev);
3575
3576 pci_release_regions(pdev);
3577 pci_save_state(pdev);
3578 pci_disable_device(pdev);
3579 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3580 return 0;
3581}
3582
3583static int skd_pci_resume(struct pci_dev *pdev)
3584{
3585 int i;
3586 int rc = 0;
3587 struct skd_device *skdev;
3588
3589 skdev = pci_get_drvdata(pdev);
3590 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003591 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003592 return -1;
3593 }
3594
3595 pci_set_power_state(pdev, PCI_D0);
3596 pci_enable_wake(pdev, PCI_D0, 0);
3597 pci_restore_state(pdev);
3598
3599 rc = pci_enable_device(pdev);
3600 if (rc)
3601 return rc;
3602 rc = pci_request_regions(pdev, DRV_NAME);
3603 if (rc)
3604 goto err_out;
3605 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3606 if (!rc) {
3607 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3608
Bart Van Asschef98806d2017-08-17 13:12:58 -07003609 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3610 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003611 }
3612 } else {
3613 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3614 if (rc) {
3615
Bart Van Asschef98806d2017-08-17 13:12:58 -07003616 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003617 goto err_out_regions;
3618 }
3619 }
3620
3621 pci_set_master(pdev);
3622 rc = pci_enable_pcie_error_reporting(pdev);
3623 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003624 dev_err(&pdev->dev,
3625 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003626 skdev->pcie_error_reporting_is_enabled = 0;
3627 } else
3628 skdev->pcie_error_reporting_is_enabled = 1;
3629
3630 for (i = 0; i < SKD_MAX_BARS; i++) {
3631
3632 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3633 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3634 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3635 skdev->mem_size[i]);
3636 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003637 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003638 rc = -ENODEV;
3639 goto err_out_iounmap;
3640 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003641 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3642 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3643 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003644 }
3645 rc = skd_acquire_irq(skdev);
3646 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003647 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003648 goto err_out_iounmap;
3649 }
3650
3651 rc = skd_start_timer(skdev);
3652 if (rc)
3653 goto err_out_timer;
3654
3655 init_waitqueue_head(&skdev->waitq);
3656
3657 skd_start_device(skdev);
3658
3659 return rc;
3660
3661err_out_timer:
3662 skd_stop_device(skdev);
3663 skd_release_irq(skdev);
3664
3665err_out_iounmap:
3666 for (i = 0; i < SKD_MAX_BARS; i++)
3667 if (skdev->mem_map[i])
3668 iounmap(skdev->mem_map[i]);
3669
3670 if (skdev->pcie_error_reporting_is_enabled)
3671 pci_disable_pcie_error_reporting(pdev);
3672
3673err_out_regions:
3674 pci_release_regions(pdev);
3675
3676err_out:
3677 pci_disable_device(pdev);
3678 return rc;
3679}
3680
3681static void skd_pci_shutdown(struct pci_dev *pdev)
3682{
3683 struct skd_device *skdev;
3684
Bart Van Asschef98806d2017-08-17 13:12:58 -07003685 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003686
3687 skdev = pci_get_drvdata(pdev);
3688 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003689 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003690 return;
3691 }
3692
Bart Van Asschef98806d2017-08-17 13:12:58 -07003693 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003694 skd_stop_device(skdev);
3695}
3696
3697static struct pci_driver skd_driver = {
3698 .name = DRV_NAME,
3699 .id_table = skd_pci_tbl,
3700 .probe = skd_pci_probe,
3701 .remove = skd_pci_remove,
3702 .suspend = skd_pci_suspend,
3703 .resume = skd_pci_resume,
3704 .shutdown = skd_pci_shutdown,
3705};
3706
3707/*
3708 *****************************************************************************
3709 * LOGGING SUPPORT
3710 *****************************************************************************
3711 */
3712
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003713const char *skd_drive_state_to_str(int state)
3714{
3715 switch (state) {
3716 case FIT_SR_DRIVE_OFFLINE:
3717 return "OFFLINE";
3718 case FIT_SR_DRIVE_INIT:
3719 return "INIT";
3720 case FIT_SR_DRIVE_ONLINE:
3721 return "ONLINE";
3722 case FIT_SR_DRIVE_BUSY:
3723 return "BUSY";
3724 case FIT_SR_DRIVE_FAULT:
3725 return "FAULT";
3726 case FIT_SR_DRIVE_DEGRADED:
3727 return "DEGRADED";
3728 case FIT_SR_PCIE_LINK_DOWN:
3729 return "INK_DOWN";
3730 case FIT_SR_DRIVE_SOFT_RESET:
3731 return "SOFT_RESET";
3732 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3733 return "NEED_FW";
3734 case FIT_SR_DRIVE_INIT_FAULT:
3735 return "INIT_FAULT";
3736 case FIT_SR_DRIVE_BUSY_SANITIZE:
3737 return "BUSY_SANITIZE";
3738 case FIT_SR_DRIVE_BUSY_ERASE:
3739 return "BUSY_ERASE";
3740 case FIT_SR_DRIVE_FW_BOOTING:
3741 return "FW_BOOTING";
3742 default:
3743 return "???";
3744 }
3745}
3746
3747const char *skd_skdev_state_to_str(enum skd_drvr_state state)
3748{
3749 switch (state) {
3750 case SKD_DRVR_STATE_LOAD:
3751 return "LOAD";
3752 case SKD_DRVR_STATE_IDLE:
3753 return "IDLE";
3754 case SKD_DRVR_STATE_BUSY:
3755 return "BUSY";
3756 case SKD_DRVR_STATE_STARTING:
3757 return "STARTING";
3758 case SKD_DRVR_STATE_ONLINE:
3759 return "ONLINE";
3760 case SKD_DRVR_STATE_PAUSING:
3761 return "PAUSING";
3762 case SKD_DRVR_STATE_PAUSED:
3763 return "PAUSED";
3764 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
3765 return "DRAINING_TIMEOUT";
3766 case SKD_DRVR_STATE_RESTARTING:
3767 return "RESTARTING";
3768 case SKD_DRVR_STATE_RESUMING:
3769 return "RESUMING";
3770 case SKD_DRVR_STATE_STOPPING:
3771 return "STOPPING";
3772 case SKD_DRVR_STATE_SYNCING:
3773 return "SYNCING";
3774 case SKD_DRVR_STATE_FAULT:
3775 return "FAULT";
3776 case SKD_DRVR_STATE_DISAPPEARED:
3777 return "DISAPPEARED";
3778 case SKD_DRVR_STATE_BUSY_ERASE:
3779 return "BUSY_ERASE";
3780 case SKD_DRVR_STATE_BUSY_SANITIZE:
3781 return "BUSY_SANITIZE";
3782 case SKD_DRVR_STATE_BUSY_IMMINENT:
3783 return "BUSY_IMMINENT";
3784 case SKD_DRVR_STATE_WAIT_BOOT:
3785 return "WAIT_BOOT";
3786
3787 default:
3788 return "???";
3789 }
3790}
3791
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05303792static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003793{
3794 switch (state) {
3795 case SKD_MSG_STATE_IDLE:
3796 return "IDLE";
3797 case SKD_MSG_STATE_BUSY:
3798 return "BUSY";
3799 default:
3800 return "???";
3801 }
3802}
3803
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05303804static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003805{
3806 switch (state) {
3807 case SKD_REQ_STATE_IDLE:
3808 return "IDLE";
3809 case SKD_REQ_STATE_SETUP:
3810 return "SETUP";
3811 case SKD_REQ_STATE_BUSY:
3812 return "BUSY";
3813 case SKD_REQ_STATE_COMPLETED:
3814 return "COMPLETED";
3815 case SKD_REQ_STATE_TIMEOUT:
3816 return "TIMEOUT";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003817 default:
3818 return "???";
3819 }
3820}
3821
3822static void skd_log_skdev(struct skd_device *skdev, const char *event)
3823{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003824 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
3825 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
3826 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3827 skd_skdev_state_to_str(skdev->state), skdev->state);
3828 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
3829 skdev->in_flight, skdev->cur_max_queue_depth,
3830 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3831 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
3832 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003833}
3834
3835static void skd_log_skmsg(struct skd_device *skdev,
3836 struct skd_fitmsg_context *skmsg, const char *event)
3837{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003838 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
3839 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
3840 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
3841 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003842}
3843
3844static void skd_log_skreq(struct skd_device *skdev,
3845 struct skd_request_context *skreq, const char *event)
3846{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003847 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
3848 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
3849 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
3850 skreq->fitmsg_id);
3851 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07003852 skreq->timeout_stamp, skreq->data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003853
Jens Axboefcd37eb2013-11-01 10:14:56 -06003854 if (skreq->req != NULL) {
3855 struct request *req = skreq->req;
3856 u32 lba = (u32)blk_rq_pos(req);
3857 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003858
Bart Van Asschef98806d2017-08-17 13:12:58 -07003859 dev_dbg(&skdev->pdev->dev,
3860 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
3861 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06003862 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07003863 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003864}
3865
3866/*
3867 *****************************************************************************
3868 * MODULE GLUE
3869 *****************************************************************************
3870 */
3871
3872static int __init skd_init(void)
3873{
Bart Van Assche16a70532017-08-17 13:13:08 -07003874 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
3875 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
3876 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
3877 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
3878 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07003879 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
3880 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
3881 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07003882
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003883 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
3884
3885 switch (skd_isr_type) {
3886 case SKD_IRQ_LEGACY:
3887 case SKD_IRQ_MSI:
3888 case SKD_IRQ_MSIX:
3889 break;
3890 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003891 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003892 skd_isr_type, SKD_IRQ_DEFAULT);
3893 skd_isr_type = SKD_IRQ_DEFAULT;
3894 }
3895
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003896 if (skd_max_queue_depth < 1 ||
3897 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
3898 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003899 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
3900 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
3901 }
3902
Bart Van Assche2da7b402017-08-17 13:13:01 -07003903 if (skd_max_req_per_msg < 1 ||
3904 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003905 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003906 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
3907 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
3908 }
3909
3910 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003911 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003912 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
3913 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
3914 }
3915
3916 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003917 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003918 skd_dbg_level, 0);
3919 skd_dbg_level = 0;
3920 }
3921
3922 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003923 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003924 skd_isr_comp_limit, 0);
3925 skd_isr_comp_limit = 0;
3926 }
3927
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003928 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003929}
3930
3931static void __exit skd_exit(void)
3932{
3933 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
3934
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003935 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003936
3937 if (skd_major)
3938 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003939}
3940
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003941module_init(skd_init);
3942module_exit(skd_exit);