blob: b69b1a041c8f4c58211228b5e64f41872e45bce7 [file] [log] [blame]
Bart Van Asschebec9e8a2017-08-17 13:12:47 -07001/*
2 * Driver for sTec s1120 PCIe SSDs. sTec was acquired in 2013 by HGST and HGST
3 * was acquired by Western Digital in 2012.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004 *
Bart Van Asschebec9e8a2017-08-17 13:12:47 -07005 * Copyright 2012 sTec, Inc.
6 * Copyright (c) 2017 Western Digital Corporation or its affiliates.
7 *
8 * This file is part of the Linux kernel, and is made available under
9 * the terms of the GNU General Public License version 2.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060010 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/pci.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/blkdev.h>
Bart Van Asschef18c17c2017-08-17 13:13:26 -070019#include <linux/blk-mq.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060020#include <linux/sched.h>
21#include <linux/interrupt.h>
22#include <linux/compiler.h>
23#include <linux/workqueue.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060024#include <linux/delay.h>
25#include <linux/time.h>
26#include <linux/hdreg.h>
27#include <linux/dma-mapping.h>
28#include <linux/completion.h>
29#include <linux/scatterlist.h>
30#include <linux/version.h>
31#include <linux/err.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060032#include <linux/aer.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060033#include <linux/wait.h>
Bart Van Assche2da7b402017-08-17 13:13:01 -070034#include <linux/stringify.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060035#include <scsi/scsi.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060036#include <scsi/sg.h>
37#include <linux/io.h>
38#include <linux/uaccess.h>
Bartlomiej Zolnierkiewicz4ca90b52013-11-05 12:37:04 +010039#include <asm/unaligned.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060040
41#include "skd_s1120.h"
42
43static int skd_dbg_level;
44static int skd_isr_comp_limit = 4;
45
46enum {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060047 SKD_FLUSH_INITIALIZER,
48 SKD_FLUSH_ZERO_SIZE_FIRST,
49 SKD_FLUSH_DATA_SECOND,
50};
51
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060052#define SKD_ASSERT(expr) \
53 do { \
54 if (unlikely(!(expr))) { \
55 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
56 # expr, __FILE__, __func__, __LINE__); \
57 } \
58 } while (0)
59
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060060#define DRV_NAME "skd"
61#define DRV_VERSION "2.2.1"
62#define DRV_BUILD_ID "0260"
63#define PFX DRV_NAME ": "
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060064
Bart Van Asschebec9e8a2017-08-17 13:12:47 -070065MODULE_LICENSE("GPL");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060066
Mike Snitzer38d4a1b2013-11-01 15:05:10 -040067MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060068MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
69
70#define PCI_VENDOR_ID_STEC 0x1B39
71#define PCI_DEVICE_ID_S1120 0x0001
72
73#define SKD_FUA_NV (1 << 1)
74#define SKD_MINORS_PER_DEVICE 16
75
76#define SKD_MAX_QUEUE_DEPTH 200u
77
78#define SKD_PAUSE_TIMEOUT (5 * 1000)
79
80#define SKD_N_FITMSG_BYTES (512u)
Bart Van Assche2da7b402017-08-17 13:13:01 -070081#define SKD_MAX_REQ_PER_MSG 14
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060082
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060083#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
84
85/* SG elements are 32 bytes, so we can make this 4096 and still be under the
86 * 128KB limit. That allows 4096*4K = 16M xfer size
87 */
88#define SKD_N_SG_PER_REQ_DEFAULT 256u
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060089
90#define SKD_N_COMPLETION_ENTRY 256u
91#define SKD_N_READ_CAP_BYTES (8u)
92
93#define SKD_N_INTERNAL_BYTES (512u)
94
Bart Van Assche6f7c7672017-08-17 13:13:02 -070095#define SKD_SKCOMP_SIZE \
96 ((sizeof(struct fit_completion_entry_v1) + \
97 sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY)
98
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060099/* 5 bits of uniqifier, 0xF800 */
100#define SKD_ID_INCR (0x400)
101#define SKD_ID_TABLE_MASK (3u << 8u)
102#define SKD_ID_RW_REQUEST (0u << 8u)
103#define SKD_ID_INTERNAL (1u << 8u)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600104#define SKD_ID_FIT_MSG (3u << 8u)
105#define SKD_ID_SLOT_MASK 0x00FFu
106#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
107
108#define SKD_N_TIMEOUT_SLOT 4u
109#define SKD_TIMEOUT_SLOT_MASK 3u
110
111#define SKD_N_MAX_SECTORS 2048u
112
113#define SKD_MAX_RETRIES 2u
114
115#define SKD_TIMER_SECONDS(seconds) (seconds)
116#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
117
118#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600119
120enum skd_drvr_state {
121 SKD_DRVR_STATE_LOAD,
122 SKD_DRVR_STATE_IDLE,
123 SKD_DRVR_STATE_BUSY,
124 SKD_DRVR_STATE_STARTING,
125 SKD_DRVR_STATE_ONLINE,
126 SKD_DRVR_STATE_PAUSING,
127 SKD_DRVR_STATE_PAUSED,
128 SKD_DRVR_STATE_DRAINING_TIMEOUT,
129 SKD_DRVR_STATE_RESTARTING,
130 SKD_DRVR_STATE_RESUMING,
131 SKD_DRVR_STATE_STOPPING,
132 SKD_DRVR_STATE_FAULT,
133 SKD_DRVR_STATE_DISAPPEARED,
134 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
135 SKD_DRVR_STATE_BUSY_ERASE,
136 SKD_DRVR_STATE_BUSY_SANITIZE,
137 SKD_DRVR_STATE_BUSY_IMMINENT,
138 SKD_DRVR_STATE_WAIT_BOOT,
139 SKD_DRVR_STATE_SYNCING,
140};
141
142#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
143#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
144#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
145#define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u)
146#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
147#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
148#define SKD_START_WAIT_SECONDS 90u
149
150enum skd_req_state {
151 SKD_REQ_STATE_IDLE,
152 SKD_REQ_STATE_SETUP,
153 SKD_REQ_STATE_BUSY,
154 SKD_REQ_STATE_COMPLETED,
155 SKD_REQ_STATE_TIMEOUT,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600156};
157
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600158enum skd_check_status_action {
159 SKD_CHECK_STATUS_REPORT_GOOD,
160 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
161 SKD_CHECK_STATUS_REQUEUE_REQUEST,
162 SKD_CHECK_STATUS_REPORT_ERROR,
163 SKD_CHECK_STATUS_BUSY_IMMINENT,
164};
165
Bart Van Assched891fe62017-08-17 13:13:07 -0700166struct skd_msg_buf {
167 struct fit_msg_hdr fmh;
168 struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG];
169};
170
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600171struct skd_fitmsg_context {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600172 u32 id;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600173
174 u32 length;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600175
Bart Van Assched891fe62017-08-17 13:13:07 -0700176 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600177 dma_addr_t mb_dma_address;
178};
179
180struct skd_request_context {
181 enum skd_req_state state;
182
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600183 u16 id;
184 u32 fitmsg_id;
185
186 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600187 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600188
189 u32 timeout_stamp;
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700190 enum dma_data_direction data_dir;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600191 struct scatterlist *sg;
192 u32 n_sg;
193 u32 sg_byte_count;
194
195 struct fit_sg_descriptor *sksg_list;
196 dma_addr_t sksg_dma_address;
197
198 struct fit_completion_entry_v1 completion;
199
200 struct fit_comp_error_info err_info;
201
202};
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600203
204struct skd_special_context {
205 struct skd_request_context req;
206
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600207 void *data_buf;
208 dma_addr_t db_dma_address;
209
Bart Van Assched891fe62017-08-17 13:13:07 -0700210 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600211 dma_addr_t mb_dma_address;
212};
213
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600214typedef enum skd_irq_type {
215 SKD_IRQ_LEGACY,
216 SKD_IRQ_MSI,
217 SKD_IRQ_MSIX
218} skd_irq_type_t;
219
220#define SKD_MAX_BARS 2
221
222struct skd_device {
Bart Van Assche85e34112017-08-17 13:13:17 -0700223 void __iomem *mem_map[SKD_MAX_BARS];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600224 resource_size_t mem_phys[SKD_MAX_BARS];
225 u32 mem_size[SKD_MAX_BARS];
226
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600227 struct skd_msix_entry *msix_entries;
228
229 struct pci_dev *pdev;
230 int pcie_error_reporting_is_enabled;
231
232 spinlock_t lock;
233 struct gendisk *disk;
234 struct request_queue *queue;
Bart Van Assche91f85da2017-08-17 13:13:28 -0700235 struct skd_fitmsg_context *skmsg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600236 struct device *class_dev;
237 int gendisk_on;
238 int sync_done;
239
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600240 u32 devno;
241 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600242 char isr_name[30];
243
244 enum skd_drvr_state state;
245 u32 drive_state;
246
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700247 atomic_t in_flight;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600248 u32 cur_max_queue_depth;
249 u32 queue_low_water_mark;
250 u32 dev_max_queue_depth;
251
252 u32 num_fitmsg_context;
253 u32 num_req_context;
254
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700255 atomic_t timeout_slot[SKD_N_TIMEOUT_SLOT];
256 atomic_t timeout_stamp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600257 struct skd_fitmsg_context *skmsg_table;
258
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600259 struct skd_request_context *skreq_table;
260
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600261 struct skd_special_context internal_skspcl;
262 u32 read_cap_blocksize;
263 u32 read_cap_last_lba;
264 int read_cap_is_valid;
265 int inquiry_is_valid;
266 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600267
268 u8 skcomp_cycle;
269 u32 skcomp_ix;
270 struct fit_completion_entry_v1 *skcomp_table;
271 struct fit_comp_error_info *skerr_table;
272 dma_addr_t cq_dma_address;
273
274 wait_queue_head_t waitq;
275
276 struct timer_list timer;
277 u32 timer_countdown;
278 u32 timer_substate;
279
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600280 int sgs_per_request;
281 u32 last_mtd;
282
283 u32 proto_ver;
284
285 int dbg_level;
286 u32 connect_time_stamp;
287 int connect_retries;
288#define SKD_MAX_CONNECT_RETRIES 16
289 u32 drive_jiffies;
290
291 u32 timo_slot;
292
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600293 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600294};
295
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600296#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
297#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
298#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
299
300static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
301{
Bart Van Assche14262a42017-08-17 13:12:57 -0700302 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600303
Bart Van Assche14262a42017-08-17 13:12:57 -0700304 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700305 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700306 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600307}
308
309static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
310 u32 offset)
311{
Bart Van Assche14262a42017-08-17 13:12:57 -0700312 writel(val, skdev->mem_map[1] + offset);
313 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700314 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600315}
316
317static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
318 u32 offset)
319{
Bart Van Assche14262a42017-08-17 13:12:57 -0700320 writeq(val, skdev->mem_map[1] + offset);
321 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700322 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
323 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600324}
325
326
327#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
328static int skd_isr_type = SKD_IRQ_DEFAULT;
329
330module_param(skd_isr_type, int, 0444);
331MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
332 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
333
334#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
335static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
336
337module_param(skd_max_req_per_msg, int, 0444);
338MODULE_PARM_DESC(skd_max_req_per_msg,
339 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700340 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600341
342#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
343#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
344static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
345
346module_param(skd_max_queue_depth, int, 0444);
347MODULE_PARM_DESC(skd_max_queue_depth,
348 "Maximum SCSI requests issued to s1120."
349 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
350
351static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
352module_param(skd_sgs_per_request, int, 0444);
353MODULE_PARM_DESC(skd_sgs_per_request,
354 "Maximum SG elements per block request."
355 " (1-4096, default==256)");
356
Bart Van Assche63214122017-08-17 13:13:23 -0700357static int skd_max_pass_thru = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600358module_param(skd_max_pass_thru, int, 0444);
359MODULE_PARM_DESC(skd_max_pass_thru,
Bart Van Assche63214122017-08-17 13:13:23 -0700360 "Maximum SCSI pass-thru at a time. IGNORED");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600361
362module_param(skd_dbg_level, int, 0444);
363MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
364
365module_param(skd_isr_comp_limit, int, 0444);
366MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
367
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600368/* Major device number dynamically assigned. */
369static u32 skd_major;
370
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600371static void skd_destruct(struct skd_device *skdev);
372static const struct block_device_operations skd_blockdev_ops;
373static void skd_send_fitmsg(struct skd_device *skdev,
374 struct skd_fitmsg_context *skmsg);
375static void skd_send_special_fitmsg(struct skd_device *skdev,
376 struct skd_special_context *skspcl);
377static void skd_request_fn(struct request_queue *rq);
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700378static void skd_end_request(struct skd_device *skdev, struct request *req,
379 blk_status_t status);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200380static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600381 struct skd_request_context *skreq);
382static void skd_postop_sg_list(struct skd_device *skdev,
383 struct skd_request_context *skreq);
384
385static void skd_restart_device(struct skd_device *skdev);
386static int skd_quiesce_dev(struct skd_device *skdev);
387static int skd_unquiesce_dev(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600388static void skd_disable_interrupts(struct skd_device *skdev);
389static void skd_isr_fwstate(struct skd_device *skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700390static void skd_recover_requests(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600391static void skd_soft_reset(struct skd_device *skdev);
392
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600393const char *skd_drive_state_to_str(int state);
394const char *skd_skdev_state_to_str(enum skd_drvr_state state);
395static void skd_log_skdev(struct skd_device *skdev, const char *event);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600396static void skd_log_skreq(struct skd_device *skdev,
397 struct skd_request_context *skreq, const char *event);
398
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600399/*
400 *****************************************************************************
401 * READ/WRITE REQUESTS
402 *****************************************************************************
403 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600404static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600405{
406 struct request_queue *q = skdev->queue;
407 struct request *req;
408
409 for (;; ) {
410 req = blk_peek_request(q);
411 if (req == NULL)
412 break;
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700413 WARN_ON_ONCE(blk_queue_start_tag(q, req));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200414 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600415 }
416}
417
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600418static void
419skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
420 int data_dir, unsigned lba,
421 unsigned count)
422{
423 if (data_dir == READ)
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700424 scsi_req->cdb[0] = READ_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600425 else
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700426 scsi_req->cdb[0] = WRITE_10;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600427
428 scsi_req->cdb[1] = 0;
429 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
430 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
431 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
432 scsi_req->cdb[5] = (lba & 0xff);
433 scsi_req->cdb[6] = 0;
434 scsi_req->cdb[7] = (count & 0xff00) >> 8;
435 scsi_req->cdb[8] = count & 0xff;
436 scsi_req->cdb[9] = 0;
437}
438
439static void
440skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400441 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600442{
443 skreq->flush_cmd = 1;
444
Bart Van Asschefb4844b2017-08-17 13:13:19 -0700445 scsi_req->cdb[0] = SYNCHRONIZE_CACHE;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600446 scsi_req->cdb[1] = 0;
447 scsi_req->cdb[2] = 0;
448 scsi_req->cdb[3] = 0;
449 scsi_req->cdb[4] = 0;
450 scsi_req->cdb[5] = 0;
451 scsi_req->cdb[6] = 0;
452 scsi_req->cdb[7] = 0;
453 scsi_req->cdb[8] = 0;
454 scsi_req->cdb[9] = 0;
455}
456
Bart Van Assche3d17a672017-08-17 13:13:21 -0700457/*
458 * Return true if and only if all pending requests should be failed.
459 */
460static bool skd_fail_all(struct request_queue *q)
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700461{
462 struct skd_device *skdev = q->queuedata;
463
464 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
465
466 skd_log_skdev(skdev, "req_not_online");
467 switch (skdev->state) {
468 case SKD_DRVR_STATE_PAUSING:
469 case SKD_DRVR_STATE_PAUSED:
470 case SKD_DRVR_STATE_STARTING:
471 case SKD_DRVR_STATE_RESTARTING:
472 case SKD_DRVR_STATE_WAIT_BOOT:
473 /* In case of starting, we haven't started the queue,
474 * so we can't get here... but requests are
475 * possibly hanging out waiting for us because we
476 * reported the dev/skd0 already. They'll wait
477 * forever if connect doesn't complete.
478 * What to do??? delay dev/skd0 ??
479 */
480 case SKD_DRVR_STATE_BUSY:
481 case SKD_DRVR_STATE_BUSY_IMMINENT:
482 case SKD_DRVR_STATE_BUSY_ERASE:
483 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700484 return false;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700485
486 case SKD_DRVR_STATE_BUSY_SANITIZE:
487 case SKD_DRVR_STATE_STOPPING:
488 case SKD_DRVR_STATE_SYNCING:
489 case SKD_DRVR_STATE_FAULT:
490 case SKD_DRVR_STATE_DISAPPEARED:
491 default:
Bart Van Assche3d17a672017-08-17 13:13:21 -0700492 return true;
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700493 }
Bart Van Asschecb6981b2017-08-17 13:13:20 -0700494}
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600495
Bart Van Assche91f85da2017-08-17 13:13:28 -0700496static void skd_process_request(struct request *req)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600497{
Bart Van Assche91f85da2017-08-17 13:13:28 -0700498 struct request_queue *const q = req->q;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600499 struct skd_device *skdev = q->queuedata;
Bart Van Assche91f85da2017-08-17 13:13:28 -0700500 struct skd_fitmsg_context *skmsg;
501 struct fit_msg_hdr *fmh;
502 const u32 tag = blk_mq_unique_tag(req);
503 struct skd_request_context *const skreq = &skdev->skreq_table[tag];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600504 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600505 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600506 u32 lba;
507 u32 count;
508 int data_dir;
Bart Van Assche4854afe2017-08-17 13:12:59 -0700509 __be64 be_dmaa;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600510 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600511 int flush, fua;
Bart Van Assche91f85da2017-08-17 13:13:28 -0700512
513 WARN_ONCE(tag >= skd_max_queue_depth, "%#x > %#x (nr_requests = %lu)\n",
514 tag, skd_max_queue_depth, q->nr_requests);
515
516 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
517
518 flush = fua = 0;
519
520 lba = (u32)blk_rq_pos(req);
521 count = blk_rq_sectors(req);
522 data_dir = rq_data_dir(req);
523 io_flags = req->cmd_flags;
524
525 if (req_op(req) == REQ_OP_FLUSH)
526 flush++;
527
528 if (io_flags & REQ_FUA)
529 fua++;
530
531 dev_dbg(&skdev->pdev->dev,
532 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba,
533 lba, count, count, data_dir);
534
535 skreq->id = tag + SKD_ID_RW_REQUEST;
536 skreq->flush_cmd = 0;
537 skreq->n_sg = 0;
538 skreq->sg_byte_count = 0;
539
540 skreq->req = req;
541 skreq->fitmsg_id = 0;
542
543 skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
544
545 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
546 dev_dbg(&skdev->pdev->dev, "error Out\n");
547 skd_end_request(skdev, skreq->req, BLK_STS_RESOURCE);
548 return;
549 }
550
551 /* Either a FIT msg is in progress or we have to start one. */
552 skmsg = skdev->skmsg;
553 if (!skmsg) {
554 skmsg = &skdev->skmsg_table[tag];
555 skdev->skmsg = skmsg;
556
557 /* Initialize the FIT msg header */
558 fmh = &skmsg->msg_buf->fmh;
559 memset(fmh, 0, sizeof(*fmh));
560 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
561 skmsg->length = sizeof(*fmh);
562 } else {
563 fmh = &skmsg->msg_buf->fmh;
564 }
565
566 skreq->fitmsg_id = skmsg->id;
567
568 scsi_req = &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
569 memset(scsi_req, 0, sizeof(*scsi_req));
570
571 be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
572
573 scsi_req->hdr.tag = skreq->id;
574 scsi_req->hdr.sg_list_dma_address = be_dmaa;
575
576 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
577 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
578 SKD_ASSERT(skreq->flush_cmd == 1);
579 } else {
580 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
581 }
582
583 if (fua)
584 scsi_req->cdb[1] |= SKD_FUA_NV;
585
586 scsi_req->hdr.sg_list_len_bytes = cpu_to_be32(skreq->sg_byte_count);
587
588 /* Complete resource allocations. */
589 skreq->state = SKD_REQ_STATE_BUSY;
590
591 skmsg->length += sizeof(struct skd_scsi_request);
592 fmh->num_protocol_cmds_coalesced++;
593
594 /*
595 * Update the active request counts.
596 * Capture the timeout timestamp.
597 */
598 skreq->timeout_stamp = atomic_read(&skdev->timeout_stamp);
599 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
600 atomic_inc(&skdev->timeout_slot[timo_slot]);
601 atomic_inc(&skdev->in_flight);
602 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
603 atomic_read(&skdev->in_flight));
604
605 /*
606 * If the FIT msg buffer is full send it.
607 */
608 if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
609 skd_send_fitmsg(skdev, skmsg);
610 skdev->skmsg = NULL;
611 }
612}
613
614static void skd_request_fn(struct request_queue *q)
615{
616 struct skd_device *skdev = q->queuedata;
617 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600618
619 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Assche3d17a672017-08-17 13:13:21 -0700620 if (skd_fail_all(q))
621 skd_fail_all_pending(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600622 return;
623 }
624
Jens Axboe6a5ec652013-11-01 10:38:45 -0600625 if (blk_queue_stopped(skdev->queue)) {
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700626 if (atomic_read(&skdev->in_flight) >=
627 skdev->queue_low_water_mark)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600628 /* There is still some kind of shortage */
629 return;
630
Jens Axboe6a5ec652013-11-01 10:38:45 -0600631 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600632 }
633
634 /*
635 * Stop conditions:
636 * - There are no more native requests
637 * - There are already the maximum number of requests in progress
638 * - There are no more skd_request_context entries
639 * - There are no more FIT msg buffers
640 */
641 for (;; ) {
Jens Axboefcd37eb2013-11-01 10:14:56 -0600642 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600643
Jens Axboefcd37eb2013-11-01 10:14:56 -0600644 /* Are there any native requests to start? */
645 if (req == NULL)
646 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600647
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400648 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600649
650 /* Are too many requets already in progress? */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700651 if (atomic_read(&skdev->in_flight) >=
652 skdev->cur_max_queue_depth) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700653 dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700654 atomic_read(&skdev->in_flight),
655 skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600656 break;
657 }
658
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600659 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400660 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600661 *
662 * At this point we are comitted to either start or reject
663 * the native request. Note that skd_request_context is
664 * available but is still at the head of the free list.
665 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700666 WARN_ON_ONCE(blk_queue_start_tag(q, req));
Bart Van Assche91f85da2017-08-17 13:13:28 -0700667 skd_process_request(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600668 }
669
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700670 /* If the FIT msg buffer is not empty send what we got. */
Bart Van Assche91f85da2017-08-17 13:13:28 -0700671 if (skdev->skmsg) {
672 struct fit_msg_hdr *fmh = &skdev->skmsg->msg_buf->fmh;
673
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700674 WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced);
Bart Van Assche91f85da2017-08-17 13:13:28 -0700675 skd_send_fitmsg(skdev, skdev->skmsg);
676 skdev->skmsg = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600677 }
678
679 /*
680 * If req is non-NULL it means there is something to do but
681 * we are out of a resource.
682 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600683 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600684 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600685}
686
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700687static void skd_end_request(struct skd_device *skdev, struct request *req,
688 blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600689{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600690 if (unlikely(error)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600691 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
692 u32 lba = (u32)blk_rq_pos(req);
693 u32 count = blk_rq_sectors(req);
694
Bart Van Asschef98806d2017-08-17 13:12:58 -0700695 dev_err(&skdev->pdev->dev,
696 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700697 count, req->tag);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600698 } else
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700699 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", req->tag,
Bart Van Asschef98806d2017-08-17 13:12:58 -0700700 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600701
Bart Van Asschef18c17c2017-08-17 13:13:26 -0700702 __blk_end_request_all(req, error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600703}
704
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200705static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400706 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600707{
708 struct request *req = skreq->req;
Bart Van Assche06f824c42017-08-17 13:13:15 -0700709 struct scatterlist *sgl = &skreq->sg[0], *sg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600710 int n_sg;
711 int i;
712
713 skreq->sg_byte_count = 0;
714
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700715 WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE &&
716 skreq->data_dir != DMA_FROM_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600717
Bart Van Assche06f824c42017-08-17 13:13:15 -0700718 n_sg = blk_rq_map_sg(skdev->queue, req, sgl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600719 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200720 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600721
722 /*
723 * Map scatterlist to PCI bus addresses.
724 * Note PCI might change the number of entries.
725 */
Bart Van Assche06f824c42017-08-17 13:13:15 -0700726 n_sg = pci_map_sg(skdev->pdev, sgl, n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600727 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200728 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600729
730 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
731
732 skreq->n_sg = n_sg;
733
Bart Van Assche06f824c42017-08-17 13:13:15 -0700734 for_each_sg(sgl, sg, n_sg, i) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600735 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Assche06f824c42017-08-17 13:13:15 -0700736 u32 cnt = sg_dma_len(sg);
737 uint64_t dma_addr = sg_dma_address(sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600738
739 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
740 sgd->byte_count = cnt;
741 skreq->sg_byte_count += cnt;
742 sgd->host_side_addr = dma_addr;
743 sgd->dev_side_addr = 0;
744 }
745
746 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
747 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
748
749 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700750 dev_dbg(&skdev->pdev->dev,
751 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
752 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600753 for (i = 0; i < n_sg; i++) {
754 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700755
756 dev_dbg(&skdev->pdev->dev,
757 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
758 i, sgd->byte_count, sgd->control,
759 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600760 }
761 }
762
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200763 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600764}
765
Jens Axboefcd37eb2013-11-01 10:14:56 -0600766static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400767 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600768{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600769 /*
770 * restore the next ptr for next IO request so we
771 * don't have to set it every time.
772 */
773 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
774 skreq->sksg_dma_address +
775 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700776 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600777}
778
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600779/*
780 *****************************************************************************
781 * TIMER
782 *****************************************************************************
783 */
784
785static void skd_timer_tick_not_online(struct skd_device *skdev);
786
787static void skd_timer_tick(ulong arg)
788{
789 struct skd_device *skdev = (struct skd_device *)arg;
790
791 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600792 unsigned long reqflags;
793 u32 state;
794
795 if (skdev->state == SKD_DRVR_STATE_FAULT)
796 /* The driver has declared fault, and we want it to
797 * stay that way until driver is reloaded.
798 */
799 return;
800
801 spin_lock_irqsave(&skdev->lock, reqflags);
802
803 state = SKD_READL(skdev, FIT_STATUS);
804 state &= FIT_SR_DRIVE_STATE_MASK;
805 if (state != skdev->drive_state)
806 skd_isr_fwstate(skdev);
807
808 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
809 skd_timer_tick_not_online(skdev);
810 goto timer_func_out;
811 }
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700812 timo_slot = atomic_inc_return(&skdev->timeout_stamp) &
813 SKD_TIMEOUT_SLOT_MASK;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600814
815 /*
816 * All requests that happened during the previous use of
817 * this slot should be done by now. The previous use was
818 * over 7 seconds ago.
819 */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700820 if (atomic_read(&skdev->timeout_slot[timo_slot]) == 0)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600821 goto timer_func_out;
822
823 /* Something is overdue */
Bart Van Asschef98806d2017-08-17 13:12:58 -0700824 dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700825 atomic_read(&skdev->timeout_slot[timo_slot]),
826 atomic_read(&skdev->in_flight));
Bart Van Asschef98806d2017-08-17 13:12:58 -0700827 dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700828 atomic_read(&skdev->timeout_slot[timo_slot]),
829 atomic_read(&skdev->in_flight));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600830
831 skdev->timer_countdown = SKD_DRAINING_TIMO;
832 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
833 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600834 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600835
836timer_func_out:
837 mod_timer(&skdev->timer, (jiffies + HZ));
838
839 spin_unlock_irqrestore(&skdev->lock, reqflags);
840}
841
842static void skd_timer_tick_not_online(struct skd_device *skdev)
843{
844 switch (skdev->state) {
845 case SKD_DRVR_STATE_IDLE:
846 case SKD_DRVR_STATE_LOAD:
847 break;
848 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700849 dev_dbg(&skdev->pdev->dev,
850 "drive busy sanitize[%x], driver[%x]\n",
851 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600852 /* If we've been in sanitize for 3 seconds, we figure we're not
853 * going to get anymore completions, so recover requests now
854 */
855 if (skdev->timer_countdown > 0) {
856 skdev->timer_countdown--;
857 return;
858 }
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700859 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600860 break;
861
862 case SKD_DRVR_STATE_BUSY:
863 case SKD_DRVR_STATE_BUSY_IMMINENT:
864 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700865 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
866 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600867 if (skdev->timer_countdown > 0) {
868 skdev->timer_countdown--;
869 return;
870 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700871 dev_dbg(&skdev->pdev->dev,
872 "busy[%x], timedout=%d, restarting device.",
873 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600874 skd_restart_device(skdev);
875 break;
876
877 case SKD_DRVR_STATE_WAIT_BOOT:
878 case SKD_DRVR_STATE_STARTING:
879 if (skdev->timer_countdown > 0) {
880 skdev->timer_countdown--;
881 return;
882 }
883 /* For now, we fault the drive. Could attempt resets to
884 * revcover at some point. */
885 skdev->state = SKD_DRVR_STATE_FAULT;
886
Bart Van Asschef98806d2017-08-17 13:12:58 -0700887 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
888 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600889
890 /*start the queue so we can respond with error to requests */
891 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600892 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600893 skdev->gendisk_on = -1;
894 wake_up_interruptible(&skdev->waitq);
895 break;
896
897 case SKD_DRVR_STATE_ONLINE:
898 /* shouldn't get here. */
899 break;
900
901 case SKD_DRVR_STATE_PAUSING:
902 case SKD_DRVR_STATE_PAUSED:
903 break;
904
905 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700906 dev_dbg(&skdev->pdev->dev,
907 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
908 skdev->timo_slot, skdev->timer_countdown,
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700909 atomic_read(&skdev->in_flight),
910 atomic_read(&skdev->timeout_slot[skdev->timo_slot]));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600911 /* if the slot has cleared we can let the I/O continue */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -0700912 if (atomic_read(&skdev->timeout_slot[skdev->timo_slot]) == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700913 dev_dbg(&skdev->pdev->dev,
914 "Slot drained, starting queue.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600915 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600916 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600917 return;
918 }
919 if (skdev->timer_countdown > 0) {
920 skdev->timer_countdown--;
921 return;
922 }
923 skd_restart_device(skdev);
924 break;
925
926 case SKD_DRVR_STATE_RESTARTING:
927 if (skdev->timer_countdown > 0) {
928 skdev->timer_countdown--;
929 return;
930 }
931 /* For now, we fault the drive. Could attempt resets to
932 * revcover at some point. */
933 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700934 dev_err(&skdev->pdev->dev,
935 "DriveFault Reconnect Timeout (%x)\n",
936 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600937
938 /*
939 * Recovering does two things:
940 * 1. completes IO with error
941 * 2. reclaims dma resources
942 * When is it safe to recover requests?
943 * - if the drive state is faulted
944 * - if the state is still soft reset after out timeout
945 * - if the drive registers are dead (state = FF)
946 * If it is "unsafe", we still need to recover, so we will
947 * disable pci bus mastering and disable our interrupts.
948 */
949
950 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
951 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
952 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
953 /* It never came out of soft reset. Try to
954 * recover the requests and then let them
955 * fail. This is to mitigate hung processes. */
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700956 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600957 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700958 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
959 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600960 pci_disable_device(skdev->pdev);
961 skd_disable_interrupts(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700962 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600963 }
964
965 /*start the queue so we can respond with error to requests */
966 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600967 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600968 skdev->gendisk_on = -1;
969 wake_up_interruptible(&skdev->waitq);
970 break;
971
972 case SKD_DRVR_STATE_RESUMING:
973 case SKD_DRVR_STATE_STOPPING:
974 case SKD_DRVR_STATE_SYNCING:
975 case SKD_DRVR_STATE_FAULT:
976 case SKD_DRVR_STATE_DISAPPEARED:
977 default:
978 break;
979 }
980}
981
982static int skd_start_timer(struct skd_device *skdev)
983{
984 int rc;
985
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600986 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
987
988 rc = mod_timer(&skdev->timer, (jiffies + HZ));
989 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -0700990 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600991 return rc;
992}
993
994static void skd_kill_timer(struct skd_device *skdev)
995{
996 del_timer_sync(&skdev->timer);
997}
998
999/*
1000 *****************************************************************************
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001001 * INTERNAL REQUESTS -- generated by driver itself
1002 *****************************************************************************
1003 */
1004
1005static int skd_format_internal_skspcl(struct skd_device *skdev)
1006{
1007 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1008 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1009 struct fit_msg_hdr *fmh;
1010 uint64_t dma_address;
1011 struct skd_scsi_request *scsi;
1012
Bart Van Assched891fe62017-08-17 13:13:07 -07001013 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001014 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1015 fmh->num_protocol_cmds_coalesced = 1;
1016
Bart Van Assched891fe62017-08-17 13:13:07 -07001017 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001018 memset(scsi, 0, sizeof(*scsi));
1019 dma_address = skspcl->req.sksg_dma_address;
1020 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
Bart Van Assche32494df2017-08-17 13:13:25 -07001021 skspcl->req.n_sg = 1;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001022 sgd->control = FIT_SGD_CONTROL_LAST;
1023 sgd->byte_count = 0;
1024 sgd->host_side_addr = skspcl->db_dma_address;
1025 sgd->dev_side_addr = 0;
1026 sgd->next_desc_ptr = 0LL;
1027
1028 return 1;
1029}
1030
1031#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1032
1033static void skd_send_internal_skspcl(struct skd_device *skdev,
1034 struct skd_special_context *skspcl,
1035 u8 opcode)
1036{
1037 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1038 struct skd_scsi_request *scsi;
1039 unsigned char *buf = skspcl->data_buf;
1040 int i;
1041
1042 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1043 /*
1044 * A refresh is already in progress.
1045 * Just wait for it to finish.
1046 */
1047 return;
1048
1049 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1050 skspcl->req.state = SKD_REQ_STATE_BUSY;
1051 skspcl->req.id += SKD_ID_INCR;
1052
Bart Van Assched891fe62017-08-17 13:13:07 -07001053 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001054 scsi->hdr.tag = skspcl->req.id;
1055
1056 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1057
1058 switch (opcode) {
1059 case TEST_UNIT_READY:
1060 scsi->cdb[0] = TEST_UNIT_READY;
1061 sgd->byte_count = 0;
1062 scsi->hdr.sg_list_len_bytes = 0;
1063 break;
1064
1065 case READ_CAPACITY:
1066 scsi->cdb[0] = READ_CAPACITY;
1067 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1068 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1069 break;
1070
1071 case INQUIRY:
1072 scsi->cdb[0] = INQUIRY;
1073 scsi->cdb[1] = 0x01; /* evpd */
1074 scsi->cdb[2] = 0x80; /* serial number page */
1075 scsi->cdb[4] = 0x10;
1076 sgd->byte_count = 16;
1077 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1078 break;
1079
1080 case SYNCHRONIZE_CACHE:
1081 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1082 sgd->byte_count = 0;
1083 scsi->hdr.sg_list_len_bytes = 0;
1084 break;
1085
1086 case WRITE_BUFFER:
1087 scsi->cdb[0] = WRITE_BUFFER;
1088 scsi->cdb[1] = 0x02;
1089 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1090 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1091 sgd->byte_count = WR_BUF_SIZE;
1092 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1093 /* fill incrementing byte pattern */
1094 for (i = 0; i < sgd->byte_count; i++)
1095 buf[i] = i & 0xFF;
1096 break;
1097
1098 case READ_BUFFER:
1099 scsi->cdb[0] = READ_BUFFER;
1100 scsi->cdb[1] = 0x02;
1101 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1102 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1103 sgd->byte_count = WR_BUF_SIZE;
1104 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1105 memset(skspcl->data_buf, 0, sgd->byte_count);
1106 break;
1107
1108 default:
1109 SKD_ASSERT("Don't know what to send");
1110 return;
1111
1112 }
1113 skd_send_special_fitmsg(skdev, skspcl);
1114}
1115
1116static void skd_refresh_device_data(struct skd_device *skdev)
1117{
1118 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1119
1120 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1121}
1122
1123static int skd_chk_read_buf(struct skd_device *skdev,
1124 struct skd_special_context *skspcl)
1125{
1126 unsigned char *buf = skspcl->data_buf;
1127 int i;
1128
1129 /* check for incrementing byte pattern */
1130 for (i = 0; i < WR_BUF_SIZE; i++)
1131 if (buf[i] != (i & 0xFF))
1132 return 1;
1133
1134 return 0;
1135}
1136
1137static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1138 u8 code, u8 qual, u8 fruc)
1139{
1140 /* If the check condition is of special interest, log a message */
1141 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1142 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001143 dev_err(&skdev->pdev->dev,
1144 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1145 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001146 }
1147}
1148
1149static void skd_complete_internal(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001150 struct fit_completion_entry_v1 *skcomp,
1151 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001152 struct skd_special_context *skspcl)
1153{
1154 u8 *buf = skspcl->data_buf;
1155 u8 status;
1156 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001157 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001158
Bart Van Assche760b48c2017-08-17 13:13:00 -07001159 lockdep_assert_held(&skdev->lock);
1160
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001161 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1162
Bart Van Asschef98806d2017-08-17 13:12:58 -07001163 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001164
1165 skspcl->req.completion = *skcomp;
1166 skspcl->req.state = SKD_REQ_STATE_IDLE;
1167 skspcl->req.id += SKD_ID_INCR;
1168
1169 status = skspcl->req.completion.status;
1170
1171 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1172 skerr->qual, skerr->fruc);
1173
1174 switch (scsi->cdb[0]) {
1175 case TEST_UNIT_READY:
1176 if (status == SAM_STAT_GOOD)
1177 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1178 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1179 (skerr->key == MEDIUM_ERROR))
1180 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1181 else {
1182 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001183 dev_dbg(&skdev->pdev->dev,
1184 "TUR failed, don't send anymore state 0x%x\n",
1185 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001186 return;
1187 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001188 dev_dbg(&skdev->pdev->dev,
1189 "**** TUR failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001190 skd_send_internal_skspcl(skdev, skspcl,
1191 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001192 }
1193 break;
1194
1195 case WRITE_BUFFER:
1196 if (status == SAM_STAT_GOOD)
1197 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1198 else {
1199 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001200 dev_dbg(&skdev->pdev->dev,
1201 "write buffer failed, don't send anymore state 0x%x\n",
1202 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001203 return;
1204 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001205 dev_dbg(&skdev->pdev->dev,
1206 "**** write buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001207 skd_send_internal_skspcl(skdev, skspcl,
1208 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001209 }
1210 break;
1211
1212 case READ_BUFFER:
1213 if (status == SAM_STAT_GOOD) {
1214 if (skd_chk_read_buf(skdev, skspcl) == 0)
1215 skd_send_internal_skspcl(skdev, skspcl,
1216 READ_CAPACITY);
1217 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001218 dev_err(&skdev->pdev->dev,
1219 "*** W/R Buffer mismatch %d ***\n",
1220 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001221 if (skdev->connect_retries <
1222 SKD_MAX_CONNECT_RETRIES) {
1223 skdev->connect_retries++;
1224 skd_soft_reset(skdev);
1225 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001226 dev_err(&skdev->pdev->dev,
1227 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001228 return;
1229 }
1230 }
1231
1232 } else {
1233 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001234 dev_dbg(&skdev->pdev->dev,
1235 "read buffer failed, don't send anymore state 0x%x\n",
1236 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001237 return;
1238 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001239 dev_dbg(&skdev->pdev->dev,
1240 "**** read buffer failed, retry skerr\n");
Bart Van Asschefb4844b2017-08-17 13:13:19 -07001241 skd_send_internal_skspcl(skdev, skspcl,
1242 TEST_UNIT_READY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001243 }
1244 break;
1245
1246 case READ_CAPACITY:
1247 skdev->read_cap_is_valid = 0;
1248 if (status == SAM_STAT_GOOD) {
1249 skdev->read_cap_last_lba =
1250 (buf[0] << 24) | (buf[1] << 16) |
1251 (buf[2] << 8) | buf[3];
1252 skdev->read_cap_blocksize =
1253 (buf[4] << 24) | (buf[5] << 16) |
1254 (buf[6] << 8) | buf[7];
1255
Bart Van Asschef98806d2017-08-17 13:12:58 -07001256 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1257 skdev->read_cap_last_lba,
1258 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001259
1260 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1261
1262 skdev->read_cap_is_valid = 1;
1263
1264 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1265 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1266 (skerr->key == MEDIUM_ERROR)) {
1267 skdev->read_cap_last_lba = ~0;
1268 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001269 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 -06001270 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1271 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001272 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001273 skd_send_internal_skspcl(skdev, skspcl,
1274 TEST_UNIT_READY);
1275 }
1276 break;
1277
1278 case INQUIRY:
1279 skdev->inquiry_is_valid = 0;
1280 if (status == SAM_STAT_GOOD) {
1281 skdev->inquiry_is_valid = 1;
1282
1283 for (i = 0; i < 12; i++)
1284 skdev->inq_serial_num[i] = buf[i + 4];
1285 skdev->inq_serial_num[12] = 0;
1286 }
1287
1288 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001289 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001290 /* connection is complete */
1291 skdev->connect_retries = 0;
1292 break;
1293
1294 case SYNCHRONIZE_CACHE:
1295 if (status == SAM_STAT_GOOD)
1296 skdev->sync_done = 1;
1297 else
1298 skdev->sync_done = -1;
1299 wake_up_interruptible(&skdev->waitq);
1300 break;
1301
1302 default:
1303 SKD_ASSERT("we didn't send this");
1304 }
1305}
1306
1307/*
1308 *****************************************************************************
1309 * FIT MESSAGES
1310 *****************************************************************************
1311 */
1312
1313static void skd_send_fitmsg(struct skd_device *skdev,
1314 struct skd_fitmsg_context *skmsg)
1315{
1316 u64 qcmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001317
Bart Van Asschef98806d2017-08-17 13:12:58 -07001318 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001319 skmsg->mb_dma_address, atomic_read(&skdev->in_flight));
Bart Van Assche6507f432017-08-17 13:13:06 -07001320 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001321
1322 qcmd = skmsg->mb_dma_address;
1323 qcmd |= FIT_QCMD_QID_NORMAL;
1324
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001325 if (unlikely(skdev->dbg_level > 1)) {
1326 u8 *bp = (u8 *)skmsg->msg_buf;
1327 int i;
1328 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001329 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
1330 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001331 if (i == 0)
1332 i = 64 - 8;
1333 }
1334 }
1335
1336 if (skmsg->length > 256)
1337 qcmd |= FIT_QCMD_MSGSIZE_512;
1338 else if (skmsg->length > 128)
1339 qcmd |= FIT_QCMD_MSGSIZE_256;
1340 else if (skmsg->length > 64)
1341 qcmd |= FIT_QCMD_MSGSIZE_128;
1342 else
1343 /*
1344 * This makes no sense because the FIT msg header is
1345 * 64 bytes. If the msg is only 64 bytes long it has
1346 * no payload.
1347 */
1348 qcmd |= FIT_QCMD_MSGSIZE_64;
1349
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001350 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1351 smp_wmb();
1352
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001353 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001354}
1355
1356static void skd_send_special_fitmsg(struct skd_device *skdev,
1357 struct skd_special_context *skspcl)
1358{
1359 u64 qcmd;
1360
1361 if (unlikely(skdev->dbg_level > 1)) {
1362 u8 *bp = (u8 *)skspcl->msg_buf;
1363 int i;
1364
1365 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001366 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
1367 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001368 if (i == 0)
1369 i = 64 - 8;
1370 }
1371
Bart Van Asschef98806d2017-08-17 13:12:58 -07001372 dev_dbg(&skdev->pdev->dev,
1373 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
1374 skspcl, skspcl->req.id, skspcl->req.sksg_list,
1375 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001376 for (i = 0; i < skspcl->req.n_sg; i++) {
1377 struct fit_sg_descriptor *sgd =
1378 &skspcl->req.sksg_list[i];
1379
Bart Van Asschef98806d2017-08-17 13:12:58 -07001380 dev_dbg(&skdev->pdev->dev,
1381 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1382 i, sgd->byte_count, sgd->control,
1383 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001384 }
1385 }
1386
1387 /*
1388 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
1389 * and one 64-byte SSDI command.
1390 */
1391 qcmd = skspcl->mb_dma_address;
1392 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
1393
Bart Van Assche5fbd5452017-08-17 13:12:46 -07001394 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1395 smp_wmb();
1396
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001397 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
1398}
1399
1400/*
1401 *****************************************************************************
1402 * COMPLETION QUEUE
1403 *****************************************************************************
1404 */
1405
1406static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001407 struct fit_completion_entry_v1 *skcomp,
1408 struct fit_comp_error_info *skerr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001409
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001410struct sns_info {
1411 u8 type;
1412 u8 stat;
1413 u8 key;
1414 u8 asc;
1415 u8 ascq;
1416 u8 mask;
1417 enum skd_check_status_action action;
1418};
1419
1420static struct sns_info skd_chkstat_table[] = {
1421 /* Good */
1422 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
1423 SKD_CHECK_STATUS_REPORT_GOOD },
1424
1425 /* Smart alerts */
1426 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
1427 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1428 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
1429 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1430 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
1431 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1432
1433 /* Retry (with limits) */
1434 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
1435 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1436 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
1437 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1438 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
1439 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1440 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
1441 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1442
1443 /* Busy (or about to be) */
1444 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
1445 SKD_CHECK_STATUS_BUSY_IMMINENT },
1446};
1447
1448/*
1449 * Look up status and sense data to decide how to handle the error
1450 * from the device.
1451 * mask says which fields must match e.g., mask=0x18 means check
1452 * type and stat, ignore key, asc, ascq.
1453 */
1454
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001455static enum skd_check_status_action
1456skd_check_status(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001457 u8 cmp_status, struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001458{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001459 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001460
Bart Van Asschef98806d2017-08-17 13:12:58 -07001461 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1462 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001463
Bart Van Asschef98806d2017-08-17 13:12:58 -07001464 dev_dbg(&skdev->pdev->dev,
1465 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
1466 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
1467 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001468
1469 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07001470 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001471 struct sns_info *sns = &skd_chkstat_table[i];
1472
1473 if (sns->mask & 0x10)
1474 if (skerr->type != sns->type)
1475 continue;
1476
1477 if (sns->mask & 0x08)
1478 if (cmp_status != sns->stat)
1479 continue;
1480
1481 if (sns->mask & 0x04)
1482 if (skerr->key != sns->key)
1483 continue;
1484
1485 if (sns->mask & 0x02)
1486 if (skerr->code != sns->asc)
1487 continue;
1488
1489 if (sns->mask & 0x01)
1490 if (skerr->qual != sns->ascq)
1491 continue;
1492
1493 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001494 dev_err(&skdev->pdev->dev,
1495 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
1496 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001497 }
1498 return sns->action;
1499 }
1500
1501 /* No other match, so nonzero status means error,
1502 * zero status means good
1503 */
1504 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001505 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001506 return SKD_CHECK_STATUS_REPORT_ERROR;
1507 }
1508
Bart Van Asschef98806d2017-08-17 13:12:58 -07001509 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001510 return SKD_CHECK_STATUS_REPORT_GOOD;
1511}
1512
1513static void skd_resolve_req_exception(struct skd_device *skdev,
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001514 struct skd_request_context *skreq,
1515 struct request *req)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001516{
1517 u8 cmp_status = skreq->completion.status;
1518
1519 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
1520 case SKD_CHECK_STATUS_REPORT_GOOD:
1521 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001522 skd_end_request(skdev, req, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001523 break;
1524
1525 case SKD_CHECK_STATUS_BUSY_IMMINENT:
1526 skd_log_skreq(skdev, skreq, "retry(busy)");
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001527 blk_requeue_request(skdev->queue, req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001528 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001529 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
1530 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
1531 skd_quiesce_dev(skdev);
1532 break;
1533
1534 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001535 if ((unsigned long) ++req->special < SKD_MAX_RETRIES) {
Jens Axboefcd37eb2013-11-01 10:14:56 -06001536 skd_log_skreq(skdev, skreq, "retry");
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001537 blk_requeue_request(skdev->queue, req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06001538 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001539 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07001540 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001541
1542 case SKD_CHECK_STATUS_REPORT_ERROR:
1543 default:
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001544 skd_end_request(skdev, req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001545 break;
1546 }
1547}
1548
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001549/* assume spinlock is already held */
1550static void skd_release_skreq(struct skd_device *skdev,
1551 struct skd_request_context *skreq)
1552{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001553 u32 timo_slot;
1554
1555 /*
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001556 * Decrease the number of active requests.
1557 * Also decrements the count in the timeout slot.
1558 */
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001559 SKD_ASSERT(atomic_read(&skdev->in_flight) > 0);
1560 atomic_dec(&skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001561
1562 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001563 SKD_ASSERT(atomic_read(&skdev->timeout_slot[timo_slot]) > 0);
1564 atomic_dec(&skdev->timeout_slot[timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001565
1566 /*
1567 * Reset backpointer
1568 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06001569 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001570
1571 /*
1572 * Reclaim the skd_request_context
1573 */
1574 skreq->state = SKD_REQ_STATE_IDLE;
1575 skreq->id += SKD_ID_INCR;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001576}
1577
1578static struct skd_request_context *skd_skreq_from_rq(struct skd_device *skdev,
1579 struct request *rq)
1580{
1581 struct skd_request_context *skreq;
1582 int i;
1583
1584 for (i = 0, skreq = skdev->skreq_table; i < skdev->num_fitmsg_context;
1585 i++, skreq++)
1586 if (skreq->req == rq)
1587 return skreq;
1588
1589 return NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001590}
1591
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001592static int skd_isr_completion_posted(struct skd_device *skdev,
1593 int limit, int *enqueued)
1594{
Bart Van Assche85e34112017-08-17 13:13:17 -07001595 struct fit_completion_entry_v1 *skcmp;
1596 struct fit_comp_error_info *skerr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001597 u16 req_id;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001598 u32 tag;
1599 struct request *rq;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001600 struct skd_request_context *skreq;
Bart Van Asschec830da82017-08-17 13:13:13 -07001601 u16 cmp_cntxt;
1602 u8 cmp_status;
1603 u8 cmp_cycle;
1604 u32 cmp_bytes;
1605 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001606 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001607
Bart Van Assche760b48c2017-08-17 13:13:00 -07001608 lockdep_assert_held(&skdev->lock);
1609
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001610 for (;; ) {
1611 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
1612
1613 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
1614 cmp_cycle = skcmp->cycle;
1615 cmp_cntxt = skcmp->tag;
1616 cmp_status = skcmp->status;
1617 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
1618
1619 skerr = &skdev->skerr_table[skdev->skcomp_ix];
1620
Bart Van Asschef98806d2017-08-17 13:12:58 -07001621 dev_dbg(&skdev->pdev->dev,
1622 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
1623 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001624 cmp_cntxt, cmp_status, atomic_read(&skdev->in_flight),
1625 cmp_bytes, skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001626
1627 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001628 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001629 break;
1630 }
1631 /*
1632 * Update the completion queue head index and possibly
1633 * the completion cycle count. 8-bit wrap-around.
1634 */
1635 skdev->skcomp_ix++;
1636 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
1637 skdev->skcomp_ix = 0;
1638 skdev->skcomp_cycle++;
1639 }
1640
1641 /*
1642 * The command context is a unique 32-bit ID. The low order
1643 * bits help locate the request. The request is usually a
1644 * r/w request (see skd_start() above) or a special request.
1645 */
1646 req_id = cmp_cntxt;
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001647 tag = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001648
1649 /* Is this other than a r/w request? */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001650 if (tag >= skdev->num_req_context) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001651 /*
1652 * This is not a completion for a r/w request.
1653 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001654 WARN_ON_ONCE(blk_map_queue_find_tag(skdev->queue->
1655 queue_tags, tag));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001656 skd_complete_other(skdev, skcmp, skerr);
1657 continue;
1658 }
1659
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001660 rq = blk_map_queue_find_tag(skdev->queue->queue_tags, tag);
1661 if (WARN(!rq, "No request for tag %#x -> %#x\n", cmp_cntxt,
1662 tag))
1663 continue;
1664 skreq = skd_skreq_from_rq(skdev, rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001665
1666 /*
1667 * Make sure the request ID for the slot matches.
1668 */
1669 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001670 dev_dbg(&skdev->pdev->dev,
1671 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
1672 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001673 {
1674 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001675 dev_err(&skdev->pdev->dev,
1676 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
1677 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001678
1679 continue;
1680 }
1681 }
1682
1683 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
1684
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001685 skreq->completion = *skcmp;
1686 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
1687 skreq->err_info = *skerr;
1688 skd_log_check_status(skdev, cmp_status, skerr->key,
1689 skerr->code, skerr->qual,
1690 skerr->fruc);
1691 }
1692 /* Release DMA resources for the request. */
1693 if (skreq->n_sg > 0)
1694 skd_postop_sg_list(skdev, skreq);
1695
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001696 /* Mark the FIT msg and timeout slot as free. */
1697 skd_release_skreq(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001698
1699 /*
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001700 * Capture the outcome and post it back to the native request.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001701 */
Bart Van Asschef18c17c2017-08-17 13:13:26 -07001702 if (likely(cmp_status == SAM_STAT_GOOD))
1703 skd_end_request(skdev, rq, BLK_STS_OK);
1704 else
1705 skd_resolve_req_exception(skdev, skreq, rq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001706
1707 /* skd_isr_comp_limit equal zero means no limit */
1708 if (limit) {
1709 if (++processed >= limit) {
1710 rc = 1;
1711 break;
1712 }
1713 }
1714 }
1715
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07001716 if (skdev->state == SKD_DRVR_STATE_PAUSING &&
1717 atomic_read(&skdev->in_flight) == 0) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001718 skdev->state = SKD_DRVR_STATE_PAUSED;
1719 wake_up_interruptible(&skdev->waitq);
1720 }
1721
1722 return rc;
1723}
1724
1725static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001726 struct fit_completion_entry_v1 *skcomp,
1727 struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001728{
1729 u32 req_id = 0;
1730 u32 req_table;
1731 u32 req_slot;
1732 struct skd_special_context *skspcl;
1733
Bart Van Assche760b48c2017-08-17 13:13:00 -07001734 lockdep_assert_held(&skdev->lock);
1735
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001736 req_id = skcomp->tag;
1737 req_table = req_id & SKD_ID_TABLE_MASK;
1738 req_slot = req_id & SKD_ID_SLOT_MASK;
1739
Bart Van Asschef98806d2017-08-17 13:12:58 -07001740 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
1741 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001742
1743 /*
1744 * Based on the request id, determine how to dispatch this completion.
1745 * This swich/case is finding the good cases and forwarding the
1746 * completion entry. Errors are reported below the switch.
1747 */
1748 switch (req_table) {
1749 case SKD_ID_RW_REQUEST:
1750 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07001751 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001752 * handles r/w requests. The only way we get here
1753 * is if the req_slot is out of bounds.
1754 */
1755 break;
1756
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001757 case SKD_ID_INTERNAL:
1758 if (req_slot == 0) {
1759 skspcl = &skdev->internal_skspcl;
1760 if (skspcl->req.id == req_id &&
1761 skspcl->req.state == SKD_REQ_STATE_BUSY) {
1762 skd_complete_internal(skdev,
1763 skcomp, skerr, skspcl);
1764 return;
1765 }
1766 }
1767 break;
1768
1769 case SKD_ID_FIT_MSG:
1770 /*
1771 * These id's should never appear in a completion record.
1772 */
1773 break;
1774
1775 default:
1776 /*
1777 * These id's should never appear anywhere;
1778 */
1779 break;
1780 }
1781
1782 /*
1783 * If we get here it is a bad or stale id.
1784 */
1785}
1786
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001787static void skd_reset_skcomp(struct skd_device *skdev)
1788{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07001789 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001790
1791 skdev->skcomp_ix = 0;
1792 skdev->skcomp_cycle = 1;
1793}
1794
1795/*
1796 *****************************************************************************
1797 * INTERRUPTS
1798 *****************************************************************************
1799 */
1800static void skd_completion_worker(struct work_struct *work)
1801{
1802 struct skd_device *skdev =
1803 container_of(work, struct skd_device, completion_worker);
1804 unsigned long flags;
1805 int flush_enqueued = 0;
1806
1807 spin_lock_irqsave(&skdev->lock, flags);
1808
1809 /*
1810 * pass in limit=0, which means no limit..
1811 * process everything in compq
1812 */
1813 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
Bart Van Assche8fe70062017-08-17 13:13:22 -07001814 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001815
1816 spin_unlock_irqrestore(&skdev->lock, flags);
1817}
1818
1819static void skd_isr_msg_from_dev(struct skd_device *skdev);
1820
Arnd Bergmann41c94992016-11-09 13:55:35 +01001821static irqreturn_t
1822skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001823{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07001824 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001825 u32 intstat;
1826 u32 ack;
1827 int rc = 0;
1828 int deferred = 0;
1829 int flush_enqueued = 0;
1830
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001831 spin_lock(&skdev->lock);
1832
1833 for (;; ) {
1834 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
1835
1836 ack = FIT_INT_DEF_MASK;
1837 ack &= intstat;
1838
Bart Van Asschef98806d2017-08-17 13:12:58 -07001839 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
1840 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001841
1842 /* As long as there is an int pending on device, keep
1843 * running loop. When none, get out, but if we've never
1844 * done any processing, call completion handler?
1845 */
1846 if (ack == 0) {
1847 /* No interrupts on device, but run the completion
1848 * processor anyway?
1849 */
1850 if (rc == 0)
1851 if (likely (skdev->state
1852 == SKD_DRVR_STATE_ONLINE))
1853 deferred = 1;
1854 break;
1855 }
1856
1857 rc = IRQ_HANDLED;
1858
1859 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
1860
1861 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
1862 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
1863 if (intstat & FIT_ISH_COMPLETION_POSTED) {
1864 /*
1865 * If we have already deferred completion
1866 * processing, don't bother running it again
1867 */
1868 if (deferred == 0)
1869 deferred =
1870 skd_isr_completion_posted(skdev,
1871 skd_isr_comp_limit, &flush_enqueued);
1872 }
1873
1874 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
1875 skd_isr_fwstate(skdev);
1876 if (skdev->state == SKD_DRVR_STATE_FAULT ||
1877 skdev->state ==
1878 SKD_DRVR_STATE_DISAPPEARED) {
1879 spin_unlock(&skdev->lock);
1880 return rc;
1881 }
1882 }
1883
1884 if (intstat & FIT_ISH_MSG_FROM_DEV)
1885 skd_isr_msg_from_dev(skdev);
1886 }
1887 }
1888
1889 if (unlikely(flush_enqueued))
Bart Van Assche8fe70062017-08-17 13:13:22 -07001890 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001891
1892 if (deferred)
1893 schedule_work(&skdev->completion_worker);
1894 else if (!flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07001895 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001896
1897 spin_unlock(&skdev->lock);
1898
1899 return rc;
1900}
1901
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001902static void skd_drive_fault(struct skd_device *skdev)
1903{
1904 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001905 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001906}
1907
1908static void skd_drive_disappeared(struct skd_device *skdev)
1909{
1910 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001911 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001912}
1913
1914static void skd_isr_fwstate(struct skd_device *skdev)
1915{
1916 u32 sense;
1917 u32 state;
1918 u32 mtd;
1919 int prev_driver_state = skdev->state;
1920
1921 sense = SKD_READL(skdev, FIT_STATUS);
1922 state = sense & FIT_SR_DRIVE_STATE_MASK;
1923
Bart Van Asschef98806d2017-08-17 13:12:58 -07001924 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
1925 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
1926 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001927
1928 skdev->drive_state = state;
1929
1930 switch (skdev->drive_state) {
1931 case FIT_SR_DRIVE_INIT:
1932 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
1933 skd_disable_interrupts(skdev);
1934 break;
1935 }
1936 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001937 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001938 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
1939 skdev->timer_countdown = SKD_STARTING_TIMO;
1940 skdev->state = SKD_DRVR_STATE_STARTING;
1941 skd_soft_reset(skdev);
1942 break;
1943 }
1944 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
1945 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1946 skdev->last_mtd = mtd;
1947 break;
1948
1949 case FIT_SR_DRIVE_ONLINE:
1950 skdev->cur_max_queue_depth = skd_max_queue_depth;
1951 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
1952 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
1953
1954 skdev->queue_low_water_mark =
1955 skdev->cur_max_queue_depth * 2 / 3 + 1;
1956 if (skdev->queue_low_water_mark < 1)
1957 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001958 dev_info(&skdev->pdev->dev,
1959 "Queue depth limit=%d dev=%d lowat=%d\n",
1960 skdev->cur_max_queue_depth,
1961 skdev->dev_max_queue_depth,
1962 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001963
1964 skd_refresh_device_data(skdev);
1965 break;
1966
1967 case FIT_SR_DRIVE_BUSY:
1968 skdev->state = SKD_DRVR_STATE_BUSY;
1969 skdev->timer_countdown = SKD_BUSY_TIMO;
1970 skd_quiesce_dev(skdev);
1971 break;
1972 case FIT_SR_DRIVE_BUSY_SANITIZE:
1973 /* set timer for 3 seconds, we'll abort any unfinished
1974 * commands after that expires
1975 */
1976 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
1977 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06001978 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001979 break;
1980 case FIT_SR_DRIVE_BUSY_ERASE:
1981 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
1982 skdev->timer_countdown = SKD_BUSY_TIMO;
1983 break;
1984 case FIT_SR_DRIVE_OFFLINE:
1985 skdev->state = SKD_DRVR_STATE_IDLE;
1986 break;
1987 case FIT_SR_DRIVE_SOFT_RESET:
1988 switch (skdev->state) {
1989 case SKD_DRVR_STATE_STARTING:
1990 case SKD_DRVR_STATE_RESTARTING:
1991 /* Expected by a caller of skd_soft_reset() */
1992 break;
1993 default:
1994 skdev->state = SKD_DRVR_STATE_RESTARTING;
1995 break;
1996 }
1997 break;
1998 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001999 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002000 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2001 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2002 break;
2003
2004 case FIT_SR_DRIVE_DEGRADED:
2005 case FIT_SR_PCIE_LINK_DOWN:
2006 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
2007 break;
2008
2009 case FIT_SR_DRIVE_FAULT:
2010 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002011 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002012 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002013 break;
2014
2015 /* PCIe bus returned all Fs? */
2016 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002017 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
2018 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002019 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002020 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002021 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002022 break;
2023 default:
2024 /*
2025 * Uknown FW State. Wait for a state we recognize.
2026 */
2027 break;
2028 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07002029 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2030 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
2031 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002032}
2033
Bart Van Assche4e54b842017-08-17 13:13:29 -07002034static void skd_recover_request(struct skd_device *skdev,
2035 struct skd_request_context *skreq)
2036{
2037 struct request *req = skreq->req;
2038
2039 if (skreq->state != SKD_REQ_STATE_BUSY)
2040 return;
2041
2042 skd_log_skreq(skdev, skreq, "recover");
2043
2044 SKD_ASSERT(req != NULL);
2045
2046 /* Release DMA resources for the request. */
2047 if (skreq->n_sg > 0)
2048 skd_postop_sg_list(skdev, skreq);
2049
2050 skreq->req = NULL;
2051 skreq->state = SKD_REQ_STATE_IDLE;
2052
2053 skd_end_request(skdev, req, BLK_STS_IOERR);
2054}
2055
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002056static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002057{
2058 int i;
2059
2060 for (i = 0; i < skdev->num_req_context; i++) {
2061 struct skd_request_context *skreq = &skdev->skreq_table[i];
2062
Bart Van Assche4e54b842017-08-17 13:13:29 -07002063 skd_recover_request(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002064 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002065
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002066 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07002067 atomic_set(&skdev->timeout_slot[i], 0);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002068
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07002069 atomic_set(&skdev->in_flight, 0);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002070}
2071
2072static void skd_isr_msg_from_dev(struct skd_device *skdev)
2073{
2074 u32 mfd;
2075 u32 mtd;
2076 u32 data;
2077
2078 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
2079
Bart Van Asschef98806d2017-08-17 13:12:58 -07002080 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
2081 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002082
2083 /* ignore any mtd that is an ack for something we didn't send */
2084 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
2085 return;
2086
2087 switch (FIT_MXD_TYPE(mfd)) {
2088 case FIT_MTD_FITFW_INIT:
2089 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
2090
2091 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002092 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
2093 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
2094 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
2095 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002096 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
2097 skd_soft_reset(skdev);
2098 break;
2099 }
2100 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
2101 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2102 skdev->last_mtd = mtd;
2103 break;
2104
2105 case FIT_MTD_GET_CMDQ_DEPTH:
2106 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
2107 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
2108 SKD_N_COMPLETION_ENTRY);
2109 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2110 skdev->last_mtd = mtd;
2111 break;
2112
2113 case FIT_MTD_SET_COMPQ_DEPTH:
2114 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
2115 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
2116 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2117 skdev->last_mtd = mtd;
2118 break;
2119
2120 case FIT_MTD_SET_COMPQ_ADDR:
2121 skd_reset_skcomp(skdev);
2122 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
2123 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2124 skdev->last_mtd = mtd;
2125 break;
2126
2127 case FIT_MTD_CMD_LOG_HOST_ID:
2128 skdev->connect_time_stamp = get_seconds();
2129 data = skdev->connect_time_stamp & 0xFFFF;
2130 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
2131 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2132 skdev->last_mtd = mtd;
2133 break;
2134
2135 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
2136 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
2137 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
2138 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
2139 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2140 skdev->last_mtd = mtd;
2141 break;
2142
2143 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
2144 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
2145 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
2146 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2147 skdev->last_mtd = mtd;
2148
Bart Van Asschef98806d2017-08-17 13:12:58 -07002149 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
2150 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002151 break;
2152
2153 case FIT_MTD_ARM_QUEUE:
2154 skdev->last_mtd = 0;
2155 /*
2156 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
2157 */
2158 break;
2159
2160 default:
2161 break;
2162 }
2163}
2164
2165static void skd_disable_interrupts(struct skd_device *skdev)
2166{
2167 u32 sense;
2168
2169 sense = SKD_READL(skdev, FIT_CONTROL);
2170 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
2171 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002172 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002173
2174 /* Note that the 1s is written. A 1-bit means
2175 * disable, a 0 means enable.
2176 */
2177 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
2178}
2179
2180static void skd_enable_interrupts(struct skd_device *skdev)
2181{
2182 u32 val;
2183
2184 /* unmask interrupts first */
2185 val = FIT_ISH_FW_STATE_CHANGE +
2186 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
2187
2188 /* Note that the compliment of mask is written. A 1-bit means
2189 * disable, a 0 means enable. */
2190 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002191 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002192
2193 val = SKD_READL(skdev, FIT_CONTROL);
2194 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002195 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002196 SKD_WRITEL(skdev, val, FIT_CONTROL);
2197}
2198
2199/*
2200 *****************************************************************************
2201 * START, STOP, RESTART, QUIESCE, UNQUIESCE
2202 *****************************************************************************
2203 */
2204
2205static void skd_soft_reset(struct skd_device *skdev)
2206{
2207 u32 val;
2208
2209 val = SKD_READL(skdev, FIT_CONTROL);
2210 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002211 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002212 SKD_WRITEL(skdev, val, FIT_CONTROL);
2213}
2214
2215static void skd_start_device(struct skd_device *skdev)
2216{
2217 unsigned long flags;
2218 u32 sense;
2219 u32 state;
2220
2221 spin_lock_irqsave(&skdev->lock, flags);
2222
2223 /* ack all ghost interrupts */
2224 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2225
2226 sense = SKD_READL(skdev, FIT_STATUS);
2227
Bart Van Asschef98806d2017-08-17 13:12:58 -07002228 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002229
2230 state = sense & FIT_SR_DRIVE_STATE_MASK;
2231 skdev->drive_state = state;
2232 skdev->last_mtd = 0;
2233
2234 skdev->state = SKD_DRVR_STATE_STARTING;
2235 skdev->timer_countdown = SKD_STARTING_TIMO;
2236
2237 skd_enable_interrupts(skdev);
2238
2239 switch (skdev->drive_state) {
2240 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002241 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002242 break;
2243
2244 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002245 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002246 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2247 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2248 break;
2249
2250 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002251 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002252 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2253 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2254 break;
2255
2256 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002257 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002258 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2259 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2260 break;
2261
2262 case FIT_SR_DRIVE_INIT:
2263 case FIT_SR_DRIVE_ONLINE:
2264 skd_soft_reset(skdev);
2265 break;
2266
2267 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002268 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002269 skdev->state = SKD_DRVR_STATE_BUSY;
2270 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2271 break;
2272
2273 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002274 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002275 break;
2276
2277 case FIT_SR_DRIVE_FAULT:
2278 /* Fault state is bad...soft reset won't do it...
2279 * Hard reset, maybe, but does it work on device?
2280 * For now, just fault so the system doesn't hang.
2281 */
2282 skd_drive_fault(skdev);
2283 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002284 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002285 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002286 skdev->gendisk_on = -1;
2287 wake_up_interruptible(&skdev->waitq);
2288 break;
2289
2290 case 0xFF:
2291 /* Most likely the device isn't there or isn't responding
2292 * to the BAR1 addresses. */
2293 skd_drive_disappeared(skdev);
2294 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002295 dev_dbg(&skdev->pdev->dev,
2296 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002297 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002298 skdev->gendisk_on = -1;
2299 wake_up_interruptible(&skdev->waitq);
2300 break;
2301
2302 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002303 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
2304 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002305 break;
2306 }
2307
2308 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002309 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002310
2311 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002312 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002313
2314 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002315 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002316
2317 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002318 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002319
2320 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002321 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002322
2323 spin_unlock_irqrestore(&skdev->lock, flags);
2324}
2325
2326static void skd_stop_device(struct skd_device *skdev)
2327{
2328 unsigned long flags;
2329 struct skd_special_context *skspcl = &skdev->internal_skspcl;
2330 u32 dev_state;
2331 int i;
2332
2333 spin_lock_irqsave(&skdev->lock, flags);
2334
2335 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002336 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002337 goto stop_out;
2338 }
2339
2340 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002341 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002342 goto stop_out;
2343 }
2344
2345 skdev->state = SKD_DRVR_STATE_SYNCING;
2346 skdev->sync_done = 0;
2347
2348 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
2349
2350 spin_unlock_irqrestore(&skdev->lock, flags);
2351
2352 wait_event_interruptible_timeout(skdev->waitq,
2353 (skdev->sync_done), (10 * HZ));
2354
2355 spin_lock_irqsave(&skdev->lock, flags);
2356
2357 switch (skdev->sync_done) {
2358 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002359 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002360 break;
2361 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002362 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002363 break;
2364 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002365 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002366 }
2367
2368stop_out:
2369 skdev->state = SKD_DRVR_STATE_STOPPING;
2370 spin_unlock_irqrestore(&skdev->lock, flags);
2371
2372 skd_kill_timer(skdev);
2373
2374 spin_lock_irqsave(&skdev->lock, flags);
2375 skd_disable_interrupts(skdev);
2376
2377 /* ensure all ints on device are cleared */
2378 /* soft reset the device to unload with a clean slate */
2379 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2380 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
2381
2382 spin_unlock_irqrestore(&skdev->lock, flags);
2383
2384 /* poll every 100ms, 1 second timeout */
2385 for (i = 0; i < 10; i++) {
2386 dev_state =
2387 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
2388 if (dev_state == FIT_SR_DRIVE_INIT)
2389 break;
2390 set_current_state(TASK_INTERRUPTIBLE);
2391 schedule_timeout(msecs_to_jiffies(100));
2392 }
2393
2394 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07002395 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
2396 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002397}
2398
2399/* assume spinlock is held */
2400static void skd_restart_device(struct skd_device *skdev)
2401{
2402 u32 state;
2403
2404 /* ack all ghost interrupts */
2405 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2406
2407 state = SKD_READL(skdev, FIT_STATUS);
2408
Bart Van Asschef98806d2017-08-17 13:12:58 -07002409 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002410
2411 state &= FIT_SR_DRIVE_STATE_MASK;
2412 skdev->drive_state = state;
2413 skdev->last_mtd = 0;
2414
2415 skdev->state = SKD_DRVR_STATE_RESTARTING;
2416 skdev->timer_countdown = SKD_RESTARTING_TIMO;
2417
2418 skd_soft_reset(skdev);
2419}
2420
2421/* assume spinlock is held */
2422static int skd_quiesce_dev(struct skd_device *skdev)
2423{
2424 int rc = 0;
2425
2426 switch (skdev->state) {
2427 case SKD_DRVR_STATE_BUSY:
2428 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002429 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002430 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002431 break;
2432 case SKD_DRVR_STATE_ONLINE:
2433 case SKD_DRVR_STATE_STOPPING:
2434 case SKD_DRVR_STATE_SYNCING:
2435 case SKD_DRVR_STATE_PAUSING:
2436 case SKD_DRVR_STATE_PAUSED:
2437 case SKD_DRVR_STATE_STARTING:
2438 case SKD_DRVR_STATE_RESTARTING:
2439 case SKD_DRVR_STATE_RESUMING:
2440 default:
2441 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002442 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
2443 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002444 }
2445 return rc;
2446}
2447
2448/* assume spinlock is held */
2449static int skd_unquiesce_dev(struct skd_device *skdev)
2450{
2451 int prev_driver_state = skdev->state;
2452
2453 skd_log_skdev(skdev, "unquiesce");
2454 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002455 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002456 return 0;
2457 }
2458 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
2459 /*
2460 * If there has been an state change to other than
2461 * ONLINE, we will rely on controller state change
2462 * to come back online and restart the queue.
2463 * The BUSY state means that driver is ready to
2464 * continue normal processing but waiting for controller
2465 * to become available.
2466 */
2467 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002468 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002469 return 0;
2470 }
2471
2472 /*
2473 * Drive has just come online, driver is either in startup,
2474 * paused performing a task, or bust waiting for hardware.
2475 */
2476 switch (skdev->state) {
2477 case SKD_DRVR_STATE_PAUSED:
2478 case SKD_DRVR_STATE_BUSY:
2479 case SKD_DRVR_STATE_BUSY_IMMINENT:
2480 case SKD_DRVR_STATE_BUSY_ERASE:
2481 case SKD_DRVR_STATE_STARTING:
2482 case SKD_DRVR_STATE_RESTARTING:
2483 case SKD_DRVR_STATE_FAULT:
2484 case SKD_DRVR_STATE_IDLE:
2485 case SKD_DRVR_STATE_LOAD:
2486 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002487 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2488 skd_skdev_state_to_str(prev_driver_state),
2489 prev_driver_state, skd_skdev_state_to_str(skdev->state),
2490 skdev->state);
2491 dev_dbg(&skdev->pdev->dev,
2492 "**** device ONLINE...starting block queue\n");
2493 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2494 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06002495 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002496 skdev->gendisk_on = 1;
2497 wake_up_interruptible(&skdev->waitq);
2498 break;
2499
2500 case SKD_DRVR_STATE_DISAPPEARED:
2501 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002502 dev_dbg(&skdev->pdev->dev,
2503 "**** driver state %d, not implemented\n",
2504 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002505 return -EBUSY;
2506 }
2507 return 0;
2508}
2509
2510/*
2511 *****************************************************************************
2512 * PCIe MSI/MSI-X INTERRUPT HANDLERS
2513 *****************************************************************************
2514 */
2515
2516static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
2517{
2518 struct skd_device *skdev = skd_host_data;
2519 unsigned long flags;
2520
2521 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002522 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2523 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2524 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
2525 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002526 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
2527 spin_unlock_irqrestore(&skdev->lock, flags);
2528 return IRQ_HANDLED;
2529}
2530
2531static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
2532{
2533 struct skd_device *skdev = skd_host_data;
2534 unsigned long flags;
2535
2536 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002537 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2538 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002539 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
2540 skd_isr_fwstate(skdev);
2541 spin_unlock_irqrestore(&skdev->lock, flags);
2542 return IRQ_HANDLED;
2543}
2544
2545static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
2546{
2547 struct skd_device *skdev = skd_host_data;
2548 unsigned long flags;
2549 int flush_enqueued = 0;
2550 int deferred;
2551
2552 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002553 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2554 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002555 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
2556 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
2557 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002558 if (flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07002559 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002560
2561 if (deferred)
2562 schedule_work(&skdev->completion_worker);
2563 else if (!flush_enqueued)
Bart Van Assche8fe70062017-08-17 13:13:22 -07002564 blk_run_queue_async(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002565
2566 spin_unlock_irqrestore(&skdev->lock, flags);
2567
2568 return IRQ_HANDLED;
2569}
2570
2571static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
2572{
2573 struct skd_device *skdev = skd_host_data;
2574 unsigned long flags;
2575
2576 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002577 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2578 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002579 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
2580 skd_isr_msg_from_dev(skdev);
2581 spin_unlock_irqrestore(&skdev->lock, flags);
2582 return IRQ_HANDLED;
2583}
2584
2585static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
2586{
2587 struct skd_device *skdev = skd_host_data;
2588 unsigned long flags;
2589
2590 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002591 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2592 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002593 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
2594 spin_unlock_irqrestore(&skdev->lock, flags);
2595 return IRQ_HANDLED;
2596}
2597
2598/*
2599 *****************************************************************************
2600 * PCIe MSI/MSI-X SETUP
2601 *****************************************************************************
2602 */
2603
2604struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002605 char isr_name[30];
2606};
2607
2608struct skd_init_msix_entry {
2609 const char *name;
2610 irq_handler_t handler;
2611};
2612
2613#define SKD_MAX_MSIX_COUNT 13
2614#define SKD_MIN_MSIX_COUNT 7
2615#define SKD_BASE_MSIX_IRQ 4
2616
2617static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
2618 { "(DMA 0)", skd_reserved_isr },
2619 { "(DMA 1)", skd_reserved_isr },
2620 { "(DMA 2)", skd_reserved_isr },
2621 { "(DMA 3)", skd_reserved_isr },
2622 { "(State Change)", skd_statec_isr },
2623 { "(COMPL_Q)", skd_comp_q },
2624 { "(MSG)", skd_msg_isr },
2625 { "(Reserved)", skd_reserved_isr },
2626 { "(Reserved)", skd_reserved_isr },
2627 { "(Queue Full 0)", skd_qfull_isr },
2628 { "(Queue Full 1)", skd_qfull_isr },
2629 { "(Queue Full 2)", skd_qfull_isr },
2630 { "(Queue Full 3)", skd_qfull_isr },
2631};
2632
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002633static int skd_acquire_msix(struct skd_device *skdev)
2634{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01002635 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01002636 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002637
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002638 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
2639 PCI_IRQ_MSIX);
2640 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002641 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002642 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002643 }
Alexander Gordeev46817762014-02-19 09:58:19 +01002644
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002645 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
2646 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002647 if (!skdev->msix_entries) {
2648 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002649 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002650 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002651 }
2652
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002653 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002654 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2655 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
2656
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002657 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
2658 "%s%d-msix %s", DRV_NAME, skdev->devno,
2659 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002660
2661 rc = devm_request_irq(&skdev->pdev->dev,
2662 pci_irq_vector(skdev->pdev, i),
2663 msix_entries[i].handler, 0,
2664 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002665 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002666 dev_err(&skdev->pdev->dev,
2667 "Unable to register(%d) MSI-X handler %d: %s\n",
2668 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002669 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002670 }
2671 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002672
Bart Van Asschef98806d2017-08-17 13:12:58 -07002673 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
2674 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002675 return 0;
2676
2677msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002678 while (--i >= 0)
2679 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01002680out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002681 kfree(skdev->msix_entries);
2682 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002683 return rc;
2684}
2685
2686static int skd_acquire_irq(struct skd_device *skdev)
2687{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002688 struct pci_dev *pdev = skdev->pdev;
2689 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002690 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002691
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002692 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002693 rc = skd_acquire_msix(skdev);
2694 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002695 return 0;
2696
Bart Van Asschef98806d2017-08-17 13:12:58 -07002697 dev_err(&skdev->pdev->dev,
2698 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002699 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002700
2701 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
2702 skdev->devno);
2703
2704 if (skd_isr_type != SKD_IRQ_LEGACY)
2705 irq_flag |= PCI_IRQ_MSI;
2706 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
2707 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002708 dev_err(&skdev->pdev->dev,
2709 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002710 return rc;
2711 }
2712
2713 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
2714 pdev->msi_enabled ? 0 : IRQF_SHARED,
2715 skdev->isr_name, skdev);
2716 if (rc) {
2717 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002718 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
2719 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002720 return rc;
2721 }
2722
2723 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002724}
2725
2726static void skd_release_irq(struct skd_device *skdev)
2727{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002728 struct pci_dev *pdev = skdev->pdev;
2729
2730 if (skdev->msix_entries) {
2731 int i;
2732
2733 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2734 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
2735 skdev);
2736 }
2737
2738 kfree(skdev->msix_entries);
2739 skdev->msix_entries = NULL;
2740 } else {
2741 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002742 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08002743
2744 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002745}
2746
2747/*
2748 *****************************************************************************
2749 * CONSTRUCT
2750 *****************************************************************************
2751 */
2752
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002753static int skd_cons_skcomp(struct skd_device *skdev)
2754{
2755 int rc = 0;
2756 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002757
Bart Van Asschef98806d2017-08-17 13:12:58 -07002758 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002759 "comp pci_alloc, total bytes %zd entries %d\n",
2760 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002761
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002762 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07002763 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002764
2765 if (skcomp == NULL) {
2766 rc = -ENOMEM;
2767 goto err_out;
2768 }
2769
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002770 skdev->skcomp_table = skcomp;
2771 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
2772 sizeof(*skcomp) *
2773 SKD_N_COMPLETION_ENTRY);
2774
2775err_out:
2776 return rc;
2777}
2778
2779static int skd_cons_skmsg(struct skd_device *skdev)
2780{
2781 int rc = 0;
2782 u32 i;
2783
Bart Van Asschef98806d2017-08-17 13:12:58 -07002784 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002785 "skmsg_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002786 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
2787 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002788
Bart Van Assche01433d02017-08-17 13:13:18 -07002789 skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context,
2790 sizeof(struct skd_fitmsg_context),
2791 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002792 if (skdev->skmsg_table == NULL) {
2793 rc = -ENOMEM;
2794 goto err_out;
2795 }
2796
2797 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2798 struct skd_fitmsg_context *skmsg;
2799
2800 skmsg = &skdev->skmsg_table[i];
2801
2802 skmsg->id = i + SKD_ID_FIT_MSG;
2803
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002804 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07002805 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002806 &skmsg->mb_dma_address);
2807
2808 if (skmsg->msg_buf == NULL) {
2809 rc = -ENOMEM;
2810 goto err_out;
2811 }
2812
Bart Van Assche6507f432017-08-17 13:13:06 -07002813 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
2814 (FIT_QCMD_ALIGN - 1),
2815 "not aligned: msg_buf %p mb_dma_address %#llx\n",
2816 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002817 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002818 }
2819
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002820err_out:
2821 return rc;
2822}
2823
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01002824static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
2825 u32 n_sg,
2826 dma_addr_t *ret_dma_addr)
2827{
2828 struct fit_sg_descriptor *sg_list;
2829 u32 nbytes;
2830
2831 nbytes = sizeof(*sg_list) * n_sg;
2832
2833 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
2834
2835 if (sg_list != NULL) {
2836 uint64_t dma_address = *ret_dma_addr;
2837 u32 i;
2838
2839 memset(sg_list, 0, nbytes);
2840
2841 for (i = 0; i < n_sg - 1; i++) {
2842 uint64_t ndp_off;
2843 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
2844
2845 sg_list[i].next_desc_ptr = dma_address + ndp_off;
2846 }
2847 sg_list[i].next_desc_ptr = 0LL;
2848 }
2849
2850 return sg_list;
2851}
2852
Bart Van Assche5d003242017-08-17 13:13:30 -07002853static void skd_free_sg_list(struct skd_device *skdev,
2854 struct fit_sg_descriptor *sg_list, u32 n_sg,
2855 dma_addr_t dma_addr)
2856{
2857 u32 nbytes = sizeof(*sg_list) * n_sg;
2858
2859 if (WARN_ON_ONCE(!sg_list))
2860 return;
2861
2862 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
2863}
2864
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002865static int skd_cons_skreq(struct skd_device *skdev)
2866{
2867 int rc = 0;
2868 u32 i;
2869
Bart Van Asschef98806d2017-08-17 13:12:58 -07002870 dev_dbg(&skdev->pdev->dev,
Bart Van Assche01433d02017-08-17 13:13:18 -07002871 "skreq_table kcalloc, struct %lu, count %u total %lu\n",
Bart Van Asschef98806d2017-08-17 13:12:58 -07002872 sizeof(struct skd_request_context), skdev->num_req_context,
2873 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002874
Bart Van Assche01433d02017-08-17 13:13:18 -07002875 skdev->skreq_table = kcalloc(skdev->num_req_context,
2876 sizeof(struct skd_request_context),
2877 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002878 if (skdev->skreq_table == NULL) {
2879 rc = -ENOMEM;
2880 goto err_out;
2881 }
2882
Bart Van Asschef98806d2017-08-17 13:12:58 -07002883 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
2884 skdev->sgs_per_request, sizeof(struct scatterlist),
2885 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002886
2887 for (i = 0; i < skdev->num_req_context; i++) {
2888 struct skd_request_context *skreq;
2889
2890 skreq = &skdev->skreq_table[i];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002891 skreq->state = SKD_REQ_STATE_IDLE;
Bart Van Assche01433d02017-08-17 13:13:18 -07002892 skreq->sg = kcalloc(skdev->sgs_per_request,
2893 sizeof(struct scatterlist), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002894 if (skreq->sg == NULL) {
2895 rc = -ENOMEM;
2896 goto err_out;
2897 }
2898 sg_init_table(skreq->sg, skdev->sgs_per_request);
2899
2900 skreq->sksg_list = skd_cons_sg_list(skdev,
2901 skdev->sgs_per_request,
2902 &skreq->sksg_dma_address);
2903
2904 if (skreq->sksg_list == NULL) {
2905 rc = -ENOMEM;
2906 goto err_out;
2907 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002908 }
2909
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002910err_out:
2911 return rc;
2912}
2913
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002914static int skd_cons_sksb(struct skd_device *skdev)
2915{
2916 int rc = 0;
2917 struct skd_special_context *skspcl;
2918 u32 nbytes;
2919
2920 skspcl = &skdev->internal_skspcl;
2921
2922 skspcl->req.id = 0 + SKD_ID_INTERNAL;
2923 skspcl->req.state = SKD_REQ_STATE_IDLE;
2924
2925 nbytes = SKD_N_INTERNAL_BYTES;
2926
Joe Perchesa5bbf612014-08-08 14:24:12 -07002927 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
2928 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002929 if (skspcl->data_buf == NULL) {
2930 rc = -ENOMEM;
2931 goto err_out;
2932 }
2933
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002934 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07002935 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
2936 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002937 if (skspcl->msg_buf == NULL) {
2938 rc = -ENOMEM;
2939 goto err_out;
2940 }
2941
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002942 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
2943 &skspcl->req.sksg_dma_address);
2944 if (skspcl->req.sksg_list == NULL) {
2945 rc = -ENOMEM;
2946 goto err_out;
2947 }
2948
2949 if (!skd_format_internal_skspcl(skdev)) {
2950 rc = -EINVAL;
2951 goto err_out;
2952 }
2953
2954err_out:
2955 return rc;
2956}
2957
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002958static int skd_cons_disk(struct skd_device *skdev)
2959{
2960 int rc = 0;
2961 struct gendisk *disk;
2962 struct request_queue *q;
2963 unsigned long flags;
2964
2965 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
2966 if (!disk) {
2967 rc = -ENOMEM;
2968 goto err_out;
2969 }
2970
2971 skdev->disk = disk;
2972 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
2973
2974 disk->major = skdev->major;
2975 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
2976 disk->fops = &skd_blockdev_ops;
2977 disk->private_data = skdev;
2978
Jens Axboefcd37eb2013-11-01 10:14:56 -06002979 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002980 if (!q) {
2981 rc = -ENOMEM;
2982 goto err_out;
2983 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02002984 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Bart Van Asschef18c17c2017-08-17 13:13:26 -07002985 q->nr_requests = skd_max_queue_depth / 2;
2986 blk_queue_init_tags(q, skd_max_queue_depth, NULL, BLK_TAG_ALLOC_FIFO);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002987
2988 skdev->queue = q;
2989 disk->queue = q;
2990 q->queuedata = skdev;
2991
Jens Axboe6975f732016-03-30 10:11:42 -06002992 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002993 blk_queue_max_segments(q, skdev->sgs_per_request);
2994 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
2995
Bart Van Asschea5c5b392017-08-17 13:12:53 -07002996 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002997 blk_queue_io_opt(q, 8192);
2998
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002999 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06003000 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003001
3002 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003003 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003004 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003005 spin_unlock_irqrestore(&skdev->lock, flags);
3006
3007err_out:
3008 return rc;
3009}
3010
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003011#define SKD_N_DEV_TABLE 16u
3012static u32 skd_next_devno;
3013
3014static struct skd_device *skd_construct(struct pci_dev *pdev)
3015{
3016 struct skd_device *skdev;
3017 int blk_major = skd_major;
3018 int rc;
3019
3020 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
3021
3022 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003023 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003024 return NULL;
3025 }
3026
3027 skdev->state = SKD_DRVR_STATE_LOAD;
3028 skdev->pdev = pdev;
3029 skdev->devno = skd_next_devno++;
3030 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003031 skdev->dev_max_queue_depth = 0;
3032
3033 skdev->num_req_context = skd_max_queue_depth;
3034 skdev->num_fitmsg_context = skd_max_queue_depth;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003035 skdev->cur_max_queue_depth = 1;
3036 skdev->queue_low_water_mark = 1;
3037 skdev->proto_ver = 99;
3038 skdev->sgs_per_request = skd_sgs_per_request;
3039 skdev->dbg_level = skd_dbg_level;
3040
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003041 spin_lock_init(&skdev->lock);
3042
3043 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
3044
Bart Van Asschef98806d2017-08-17 13:12:58 -07003045 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003046 rc = skd_cons_skcomp(skdev);
3047 if (rc < 0)
3048 goto err_out;
3049
Bart Van Asschef98806d2017-08-17 13:12:58 -07003050 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003051 rc = skd_cons_skmsg(skdev);
3052 if (rc < 0)
3053 goto err_out;
3054
Bart Van Asschef98806d2017-08-17 13:12:58 -07003055 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003056 rc = skd_cons_skreq(skdev);
3057 if (rc < 0)
3058 goto err_out;
3059
Bart Van Asschef98806d2017-08-17 13:12:58 -07003060 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003061 rc = skd_cons_sksb(skdev);
3062 if (rc < 0)
3063 goto err_out;
3064
Bart Van Asschef98806d2017-08-17 13:12:58 -07003065 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003066 rc = skd_cons_disk(skdev);
3067 if (rc < 0)
3068 goto err_out;
3069
Bart Van Asschef98806d2017-08-17 13:12:58 -07003070 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003071 return skdev;
3072
3073err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003074 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003075 skd_destruct(skdev);
3076 return NULL;
3077}
3078
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003079/*
3080 *****************************************************************************
3081 * DESTRUCT (FREE)
3082 *****************************************************************************
3083 */
3084
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003085static void skd_free_skcomp(struct skd_device *skdev)
3086{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07003087 if (skdev->skcomp_table)
3088 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003089 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003090
3091 skdev->skcomp_table = NULL;
3092 skdev->cq_dma_address = 0;
3093}
3094
3095static void skd_free_skmsg(struct skd_device *skdev)
3096{
3097 u32 i;
3098
3099 if (skdev->skmsg_table == NULL)
3100 return;
3101
3102 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3103 struct skd_fitmsg_context *skmsg;
3104
3105 skmsg = &skdev->skmsg_table[i];
3106
3107 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003108 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
3109 skmsg->msg_buf,
3110 skmsg->mb_dma_address);
3111 }
3112 skmsg->msg_buf = NULL;
3113 skmsg->mb_dma_address = 0;
3114 }
3115
3116 kfree(skdev->skmsg_table);
3117 skdev->skmsg_table = NULL;
3118}
3119
3120static void skd_free_skreq(struct skd_device *skdev)
3121{
3122 u32 i;
3123
3124 if (skdev->skreq_table == NULL)
3125 return;
3126
3127 for (i = 0; i < skdev->num_req_context; i++) {
3128 struct skd_request_context *skreq;
3129
3130 skreq = &skdev->skreq_table[i];
3131
3132 skd_free_sg_list(skdev, skreq->sksg_list,
3133 skdev->sgs_per_request,
3134 skreq->sksg_dma_address);
3135
3136 skreq->sksg_list = NULL;
3137 skreq->sksg_dma_address = 0;
3138
3139 kfree(skreq->sg);
3140 }
3141
3142 kfree(skdev->skreq_table);
3143 skdev->skreq_table = NULL;
3144}
3145
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003146static void skd_free_sksb(struct skd_device *skdev)
3147{
3148 struct skd_special_context *skspcl;
3149 u32 nbytes;
3150
3151 skspcl = &skdev->internal_skspcl;
3152
3153 if (skspcl->data_buf != NULL) {
3154 nbytes = SKD_N_INTERNAL_BYTES;
3155
3156 pci_free_consistent(skdev->pdev, nbytes,
3157 skspcl->data_buf, skspcl->db_dma_address);
3158 }
3159
3160 skspcl->data_buf = NULL;
3161 skspcl->db_dma_address = 0;
3162
3163 if (skspcl->msg_buf != NULL) {
3164 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3165 pci_free_consistent(skdev->pdev, nbytes,
3166 skspcl->msg_buf, skspcl->mb_dma_address);
3167 }
3168
3169 skspcl->msg_buf = NULL;
3170 skspcl->mb_dma_address = 0;
3171
3172 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
3173 skspcl->req.sksg_dma_address);
3174
3175 skspcl->req.sksg_list = NULL;
3176 skspcl->req.sksg_dma_address = 0;
3177}
3178
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003179static void skd_free_disk(struct skd_device *skdev)
3180{
3181 struct gendisk *disk = skdev->disk;
3182
Bart Van Assche7277cc62017-08-17 13:12:45 -07003183 if (disk && (disk->flags & GENHD_FL_UP))
3184 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003185
Bart Van Assche7277cc62017-08-17 13:12:45 -07003186 if (skdev->queue) {
3187 blk_cleanup_queue(skdev->queue);
3188 skdev->queue = NULL;
3189 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003190 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07003191
3192 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003193 skdev->disk = NULL;
3194}
3195
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003196static void skd_destruct(struct skd_device *skdev)
3197{
3198 if (skdev == NULL)
3199 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003200
Bart Van Asschef98806d2017-08-17 13:12:58 -07003201 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003202 skd_free_disk(skdev);
3203
Bart Van Asschef98806d2017-08-17 13:12:58 -07003204 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003205 skd_free_sksb(skdev);
3206
Bart Van Asschef98806d2017-08-17 13:12:58 -07003207 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003208 skd_free_skreq(skdev);
3209
Bart Van Asschef98806d2017-08-17 13:12:58 -07003210 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003211 skd_free_skmsg(skdev);
3212
Bart Van Asschef98806d2017-08-17 13:12:58 -07003213 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003214 skd_free_skcomp(skdev);
3215
Bart Van Asschef98806d2017-08-17 13:12:58 -07003216 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003217 kfree(skdev);
3218}
3219
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003220/*
3221 *****************************************************************************
3222 * BLOCK DEVICE (BDEV) GLUE
3223 *****************************************************************************
3224 */
3225
3226static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3227{
3228 struct skd_device *skdev;
3229 u64 capacity;
3230
3231 skdev = bdev->bd_disk->private_data;
3232
Bart Van Asschef98806d2017-08-17 13:12:58 -07003233 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
3234 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003235
3236 if (skdev->read_cap_is_valid) {
3237 capacity = get_capacity(skdev->disk);
3238 geo->heads = 64;
3239 geo->sectors = 255;
3240 geo->cylinders = (capacity) / (255 * 64);
3241
3242 return 0;
3243 }
3244 return -EIO;
3245}
3246
Dan Williams0d52c7562016-06-15 19:44:20 -07003247static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003248{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003249 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07003250 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003251 return 0;
3252}
3253
3254static const struct block_device_operations skd_blockdev_ops = {
3255 .owner = THIS_MODULE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003256 .getgeo = skd_bdev_getgeo,
3257};
3258
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003259/*
3260 *****************************************************************************
3261 * PCIe DRIVER GLUE
3262 *****************************************************************************
3263 */
3264
Benoit Taine9baa3c32014-08-08 15:56:03 +02003265static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003266 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
3267 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
3268 { 0 } /* terminate list */
3269};
3270
3271MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
3272
3273static char *skd_pci_info(struct skd_device *skdev, char *str)
3274{
3275 int pcie_reg;
3276
3277 strcpy(str, "PCIe (");
3278 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
3279
3280 if (pcie_reg) {
3281
3282 char lwstr[6];
3283 uint16_t pcie_lstat, lspeed, lwidth;
3284
3285 pcie_reg += 0x12;
3286 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
3287 lspeed = pcie_lstat & (0xF);
3288 lwidth = (pcie_lstat & 0x3F0) >> 4;
3289
3290 if (lspeed == 1)
3291 strcat(str, "2.5GT/s ");
3292 else if (lspeed == 2)
3293 strcat(str, "5.0GT/s ");
3294 else
3295 strcat(str, "<unknown> ");
3296 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
3297 strcat(str, lwstr);
3298 }
3299 return str;
3300}
3301
3302static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3303{
3304 int i;
3305 int rc = 0;
3306 char pci_str[32];
3307 struct skd_device *skdev;
3308
Bart Van Asschef98806d2017-08-17 13:12:58 -07003309 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
3310 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
3311 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
3312 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003313
3314 rc = pci_enable_device(pdev);
3315 if (rc)
3316 return rc;
3317 rc = pci_request_regions(pdev, DRV_NAME);
3318 if (rc)
3319 goto err_out;
3320 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3321 if (!rc) {
3322 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003323 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3324 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003325 }
3326 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003327 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003328 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003329 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003330 goto err_out_regions;
3331 }
3332 }
3333
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003334 if (!skd_major) {
3335 rc = register_blkdev(0, DRV_NAME);
3336 if (rc < 0)
3337 goto err_out_regions;
3338 BUG_ON(!rc);
3339 skd_major = rc;
3340 }
3341
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003342 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08003343 if (skdev == NULL) {
3344 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003345 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08003346 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003347
3348 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003349 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003350
3351 pci_set_master(pdev);
3352 rc = pci_enable_pcie_error_reporting(pdev);
3353 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003354 dev_err(&pdev->dev,
3355 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003356 skdev->pcie_error_reporting_is_enabled = 0;
3357 } else
3358 skdev->pcie_error_reporting_is_enabled = 1;
3359
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003360 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01003361
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003362 for (i = 0; i < SKD_MAX_BARS; i++) {
3363 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3364 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3365 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3366 skdev->mem_size[i]);
3367 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003368 dev_err(&pdev->dev,
3369 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003370 rc = -ENODEV;
3371 goto err_out_iounmap;
3372 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003373 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3374 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3375 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003376 }
3377
3378 rc = skd_acquire_irq(skdev);
3379 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003380 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003381 goto err_out_iounmap;
3382 }
3383
3384 rc = skd_start_timer(skdev);
3385 if (rc)
3386 goto err_out_timer;
3387
3388 init_waitqueue_head(&skdev->waitq);
3389
3390 skd_start_device(skdev);
3391
3392 rc = wait_event_interruptible_timeout(skdev->waitq,
3393 (skdev->gendisk_on),
3394 (SKD_START_WAIT_SECONDS * HZ));
3395 if (skdev->gendisk_on > 0) {
3396 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07003397 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003398 rc = 0;
3399 } else {
3400 /* we timed out, something is wrong with the device,
3401 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003402 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
3403 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003404 /* in case of no error; we timeout with ENXIO */
3405 if (!rc)
3406 rc = -ENXIO;
3407 goto err_out_timer;
3408 }
3409
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003410 return rc;
3411
3412err_out_timer:
3413 skd_stop_device(skdev);
3414 skd_release_irq(skdev);
3415
3416err_out_iounmap:
3417 for (i = 0; i < SKD_MAX_BARS; i++)
3418 if (skdev->mem_map[i])
3419 iounmap(skdev->mem_map[i]);
3420
3421 if (skdev->pcie_error_reporting_is_enabled)
3422 pci_disable_pcie_error_reporting(pdev);
3423
3424 skd_destruct(skdev);
3425
3426err_out_regions:
3427 pci_release_regions(pdev);
3428
3429err_out:
3430 pci_disable_device(pdev);
3431 pci_set_drvdata(pdev, NULL);
3432 return rc;
3433}
3434
3435static void skd_pci_remove(struct pci_dev *pdev)
3436{
3437 int i;
3438 struct skd_device *skdev;
3439
3440 skdev = pci_get_drvdata(pdev);
3441 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003442 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003443 return;
3444 }
3445 skd_stop_device(skdev);
3446 skd_release_irq(skdev);
3447
3448 for (i = 0; i < SKD_MAX_BARS; i++)
3449 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003450 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003451
3452 if (skdev->pcie_error_reporting_is_enabled)
3453 pci_disable_pcie_error_reporting(pdev);
3454
3455 skd_destruct(skdev);
3456
3457 pci_release_regions(pdev);
3458 pci_disable_device(pdev);
3459 pci_set_drvdata(pdev, NULL);
3460
3461 return;
3462}
3463
3464static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3465{
3466 int i;
3467 struct skd_device *skdev;
3468
3469 skdev = pci_get_drvdata(pdev);
3470 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003471 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003472 return -EIO;
3473 }
3474
3475 skd_stop_device(skdev);
3476
3477 skd_release_irq(skdev);
3478
3479 for (i = 0; i < SKD_MAX_BARS; i++)
3480 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07003481 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003482
3483 if (skdev->pcie_error_reporting_is_enabled)
3484 pci_disable_pcie_error_reporting(pdev);
3485
3486 pci_release_regions(pdev);
3487 pci_save_state(pdev);
3488 pci_disable_device(pdev);
3489 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3490 return 0;
3491}
3492
3493static int skd_pci_resume(struct pci_dev *pdev)
3494{
3495 int i;
3496 int rc = 0;
3497 struct skd_device *skdev;
3498
3499 skdev = pci_get_drvdata(pdev);
3500 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003501 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003502 return -1;
3503 }
3504
3505 pci_set_power_state(pdev, PCI_D0);
3506 pci_enable_wake(pdev, PCI_D0, 0);
3507 pci_restore_state(pdev);
3508
3509 rc = pci_enable_device(pdev);
3510 if (rc)
3511 return rc;
3512 rc = pci_request_regions(pdev, DRV_NAME);
3513 if (rc)
3514 goto err_out;
3515 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3516 if (!rc) {
3517 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3518
Bart Van Asschef98806d2017-08-17 13:12:58 -07003519 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
3520 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003521 }
3522 } else {
3523 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3524 if (rc) {
3525
Bart Van Asschef98806d2017-08-17 13:12:58 -07003526 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003527 goto err_out_regions;
3528 }
3529 }
3530
3531 pci_set_master(pdev);
3532 rc = pci_enable_pcie_error_reporting(pdev);
3533 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003534 dev_err(&pdev->dev,
3535 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003536 skdev->pcie_error_reporting_is_enabled = 0;
3537 } else
3538 skdev->pcie_error_reporting_is_enabled = 1;
3539
3540 for (i = 0; i < SKD_MAX_BARS; i++) {
3541
3542 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3543 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3544 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3545 skdev->mem_size[i]);
3546 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003547 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003548 rc = -ENODEV;
3549 goto err_out_iounmap;
3550 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003551 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3552 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3553 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003554 }
3555 rc = skd_acquire_irq(skdev);
3556 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003557 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003558 goto err_out_iounmap;
3559 }
3560
3561 rc = skd_start_timer(skdev);
3562 if (rc)
3563 goto err_out_timer;
3564
3565 init_waitqueue_head(&skdev->waitq);
3566
3567 skd_start_device(skdev);
3568
3569 return rc;
3570
3571err_out_timer:
3572 skd_stop_device(skdev);
3573 skd_release_irq(skdev);
3574
3575err_out_iounmap:
3576 for (i = 0; i < SKD_MAX_BARS; i++)
3577 if (skdev->mem_map[i])
3578 iounmap(skdev->mem_map[i]);
3579
3580 if (skdev->pcie_error_reporting_is_enabled)
3581 pci_disable_pcie_error_reporting(pdev);
3582
3583err_out_regions:
3584 pci_release_regions(pdev);
3585
3586err_out:
3587 pci_disable_device(pdev);
3588 return rc;
3589}
3590
3591static void skd_pci_shutdown(struct pci_dev *pdev)
3592{
3593 struct skd_device *skdev;
3594
Bart Van Asschef98806d2017-08-17 13:12:58 -07003595 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003596
3597 skdev = pci_get_drvdata(pdev);
3598 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003599 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003600 return;
3601 }
3602
Bart Van Asschef98806d2017-08-17 13:12:58 -07003603 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003604 skd_stop_device(skdev);
3605}
3606
3607static struct pci_driver skd_driver = {
3608 .name = DRV_NAME,
3609 .id_table = skd_pci_tbl,
3610 .probe = skd_pci_probe,
3611 .remove = skd_pci_remove,
3612 .suspend = skd_pci_suspend,
3613 .resume = skd_pci_resume,
3614 .shutdown = skd_pci_shutdown,
3615};
3616
3617/*
3618 *****************************************************************************
3619 * LOGGING SUPPORT
3620 *****************************************************************************
3621 */
3622
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003623const char *skd_drive_state_to_str(int state)
3624{
3625 switch (state) {
3626 case FIT_SR_DRIVE_OFFLINE:
3627 return "OFFLINE";
3628 case FIT_SR_DRIVE_INIT:
3629 return "INIT";
3630 case FIT_SR_DRIVE_ONLINE:
3631 return "ONLINE";
3632 case FIT_SR_DRIVE_BUSY:
3633 return "BUSY";
3634 case FIT_SR_DRIVE_FAULT:
3635 return "FAULT";
3636 case FIT_SR_DRIVE_DEGRADED:
3637 return "DEGRADED";
3638 case FIT_SR_PCIE_LINK_DOWN:
3639 return "INK_DOWN";
3640 case FIT_SR_DRIVE_SOFT_RESET:
3641 return "SOFT_RESET";
3642 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3643 return "NEED_FW";
3644 case FIT_SR_DRIVE_INIT_FAULT:
3645 return "INIT_FAULT";
3646 case FIT_SR_DRIVE_BUSY_SANITIZE:
3647 return "BUSY_SANITIZE";
3648 case FIT_SR_DRIVE_BUSY_ERASE:
3649 return "BUSY_ERASE";
3650 case FIT_SR_DRIVE_FW_BOOTING:
3651 return "FW_BOOTING";
3652 default:
3653 return "???";
3654 }
3655}
3656
3657const char *skd_skdev_state_to_str(enum skd_drvr_state state)
3658{
3659 switch (state) {
3660 case SKD_DRVR_STATE_LOAD:
3661 return "LOAD";
3662 case SKD_DRVR_STATE_IDLE:
3663 return "IDLE";
3664 case SKD_DRVR_STATE_BUSY:
3665 return "BUSY";
3666 case SKD_DRVR_STATE_STARTING:
3667 return "STARTING";
3668 case SKD_DRVR_STATE_ONLINE:
3669 return "ONLINE";
3670 case SKD_DRVR_STATE_PAUSING:
3671 return "PAUSING";
3672 case SKD_DRVR_STATE_PAUSED:
3673 return "PAUSED";
3674 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
3675 return "DRAINING_TIMEOUT";
3676 case SKD_DRVR_STATE_RESTARTING:
3677 return "RESTARTING";
3678 case SKD_DRVR_STATE_RESUMING:
3679 return "RESUMING";
3680 case SKD_DRVR_STATE_STOPPING:
3681 return "STOPPING";
3682 case SKD_DRVR_STATE_SYNCING:
3683 return "SYNCING";
3684 case SKD_DRVR_STATE_FAULT:
3685 return "FAULT";
3686 case SKD_DRVR_STATE_DISAPPEARED:
3687 return "DISAPPEARED";
3688 case SKD_DRVR_STATE_BUSY_ERASE:
3689 return "BUSY_ERASE";
3690 case SKD_DRVR_STATE_BUSY_SANITIZE:
3691 return "BUSY_SANITIZE";
3692 case SKD_DRVR_STATE_BUSY_IMMINENT:
3693 return "BUSY_IMMINENT";
3694 case SKD_DRVR_STATE_WAIT_BOOT:
3695 return "WAIT_BOOT";
3696
3697 default:
3698 return "???";
3699 }
3700}
3701
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05303702static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703{
3704 switch (state) {
3705 case SKD_REQ_STATE_IDLE:
3706 return "IDLE";
3707 case SKD_REQ_STATE_SETUP:
3708 return "SETUP";
3709 case SKD_REQ_STATE_BUSY:
3710 return "BUSY";
3711 case SKD_REQ_STATE_COMPLETED:
3712 return "COMPLETED";
3713 case SKD_REQ_STATE_TIMEOUT:
3714 return "TIMEOUT";
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003715 default:
3716 return "???";
3717 }
3718}
3719
3720static void skd_log_skdev(struct skd_device *skdev, const char *event)
3721{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003722 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
3723 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
3724 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3725 skd_skdev_state_to_str(skdev->state), skdev->state);
3726 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07003727 atomic_read(&skdev->in_flight), skdev->cur_max_queue_depth,
Bart Van Asschef98806d2017-08-17 13:12:58 -07003728 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3729 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
Bart Van Assche6fbb2de2017-08-17 13:13:27 -07003730 atomic_read(&skdev->timeout_stamp), skdev->skcomp_cycle,
3731 skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003732}
3733
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003734static void skd_log_skreq(struct skd_device *skdev,
3735 struct skd_request_context *skreq, const char *event)
3736{
Bart Van Asschef98806d2017-08-17 13:12:58 -07003737 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
3738 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
3739 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
3740 skreq->fitmsg_id);
3741 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07003742 skreq->timeout_stamp, skreq->data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003743
Jens Axboefcd37eb2013-11-01 10:14:56 -06003744 if (skreq->req != NULL) {
3745 struct request *req = skreq->req;
3746 u32 lba = (u32)blk_rq_pos(req);
3747 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003748
Bart Van Asschef98806d2017-08-17 13:12:58 -07003749 dev_dbg(&skdev->pdev->dev,
3750 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
3751 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06003752 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07003753 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003754}
3755
3756/*
3757 *****************************************************************************
3758 * MODULE GLUE
3759 *****************************************************************************
3760 */
3761
3762static int __init skd_init(void)
3763{
Bart Van Assche16a70532017-08-17 13:13:08 -07003764 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
3765 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
3766 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
3767 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
3768 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07003769 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
3770 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
3771 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07003772
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003773 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
3774
3775 switch (skd_isr_type) {
3776 case SKD_IRQ_LEGACY:
3777 case SKD_IRQ_MSI:
3778 case SKD_IRQ_MSIX:
3779 break;
3780 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003781 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003782 skd_isr_type, SKD_IRQ_DEFAULT);
3783 skd_isr_type = SKD_IRQ_DEFAULT;
3784 }
3785
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003786 if (skd_max_queue_depth < 1 ||
3787 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
3788 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003789 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
3790 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
3791 }
3792
Bart Van Assche2da7b402017-08-17 13:13:01 -07003793 if (skd_max_req_per_msg < 1 ||
3794 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003795 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003796 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
3797 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
3798 }
3799
3800 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003801 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003802 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
3803 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
3804 }
3805
3806 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003807 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003808 skd_dbg_level, 0);
3809 skd_dbg_level = 0;
3810 }
3811
3812 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01003813 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003814 skd_isr_comp_limit, 0);
3815 skd_isr_comp_limit = 0;
3816 }
3817
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003818 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003819}
3820
3821static void __exit skd_exit(void)
3822{
3823 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
3824
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003825 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01003826
3827 if (skd_major)
3828 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003829}
3830
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003831module_init(skd_init);
3832module_exit(skd_exit);