blob: 0639c9f899847b2bd33cc19fd2fd4cb3f3a9e66a [file] [log] [blame]
Bart Van Asschebec9e8a2017-08-17 13:12:47 -07001/*
2 * Driver for sTec s1120 PCIe SSDs. sTec was acquired in 2013 by HGST and HGST
3 * was acquired by Western Digital in 2012.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004 *
Bart Van Asschebec9e8a2017-08-17 13:12:47 -07005 * Copyright 2012 sTec, Inc.
6 * Copyright (c) 2017 Western Digital Corporation or its affiliates.
7 *
8 * This file is part of the Linux kernel, and is made available under
9 * the terms of the GNU General Public License version 2.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060010 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/pci.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/blkdev.h>
19#include <linux/sched.h>
20#include <linux/interrupt.h>
21#include <linux/compiler.h>
22#include <linux/workqueue.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060023#include <linux/delay.h>
24#include <linux/time.h>
25#include <linux/hdreg.h>
26#include <linux/dma-mapping.h>
27#include <linux/completion.h>
28#include <linux/scatterlist.h>
29#include <linux/version.h>
30#include <linux/err.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060031#include <linux/aer.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060032#include <linux/wait.h>
33#include <linux/uio.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 {
47 STEC_LINK_2_5GTS = 0,
48 STEC_LINK_5GTS = 1,
49 STEC_LINK_8GTS = 2,
50 STEC_LINK_UNKNOWN = 0xFF
51};
52
53enum {
54 SKD_FLUSH_INITIALIZER,
55 SKD_FLUSH_ZERO_SIZE_FIRST,
56 SKD_FLUSH_DATA_SECOND,
57};
58
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060059#define SKD_ASSERT(expr) \
60 do { \
61 if (unlikely(!(expr))) { \
62 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
63 # expr, __FILE__, __func__, __LINE__); \
64 } \
65 } while (0)
66
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060067#define DRV_NAME "skd"
68#define DRV_VERSION "2.2.1"
69#define DRV_BUILD_ID "0260"
70#define PFX DRV_NAME ": "
71#define DRV_BIN_VERSION 0x100
72#define DRV_VER_COMPL "2.2.1." DRV_BUILD_ID
73
Bart Van Asschebec9e8a2017-08-17 13:12:47 -070074MODULE_LICENSE("GPL");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060075
Mike Snitzer38d4a1b2013-11-01 15:05:10 -040076MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060077MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
78
79#define PCI_VENDOR_ID_STEC 0x1B39
80#define PCI_DEVICE_ID_S1120 0x0001
81
82#define SKD_FUA_NV (1 << 1)
83#define SKD_MINORS_PER_DEVICE 16
84
85#define SKD_MAX_QUEUE_DEPTH 200u
86
87#define SKD_PAUSE_TIMEOUT (5 * 1000)
88
89#define SKD_N_FITMSG_BYTES (512u)
Bart Van Assche2da7b402017-08-17 13:13:01 -070090#define SKD_MAX_REQ_PER_MSG 14
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060091
92#define SKD_N_SPECIAL_CONTEXT 32u
93#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
94
95/* SG elements are 32 bytes, so we can make this 4096 and still be under the
96 * 128KB limit. That allows 4096*4K = 16M xfer size
97 */
98#define SKD_N_SG_PER_REQ_DEFAULT 256u
99#define SKD_N_SG_PER_SPECIAL 256u
100
101#define SKD_N_COMPLETION_ENTRY 256u
102#define SKD_N_READ_CAP_BYTES (8u)
103
104#define SKD_N_INTERNAL_BYTES (512u)
105
Bart Van Assche6f7c7672017-08-17 13:13:02 -0700106#define SKD_SKCOMP_SIZE \
107 ((sizeof(struct fit_completion_entry_v1) + \
108 sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY)
109
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600110/* 5 bits of uniqifier, 0xF800 */
111#define SKD_ID_INCR (0x400)
112#define SKD_ID_TABLE_MASK (3u << 8u)
113#define SKD_ID_RW_REQUEST (0u << 8u)
114#define SKD_ID_INTERNAL (1u << 8u)
115#define SKD_ID_SPECIAL_REQUEST (2u << 8u)
116#define SKD_ID_FIT_MSG (3u << 8u)
117#define SKD_ID_SLOT_MASK 0x00FFu
118#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
119
120#define SKD_N_TIMEOUT_SLOT 4u
121#define SKD_TIMEOUT_SLOT_MASK 3u
122
123#define SKD_N_MAX_SECTORS 2048u
124
125#define SKD_MAX_RETRIES 2u
126
127#define SKD_TIMER_SECONDS(seconds) (seconds)
128#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
129
130#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600131
132enum skd_drvr_state {
133 SKD_DRVR_STATE_LOAD,
134 SKD_DRVR_STATE_IDLE,
135 SKD_DRVR_STATE_BUSY,
136 SKD_DRVR_STATE_STARTING,
137 SKD_DRVR_STATE_ONLINE,
138 SKD_DRVR_STATE_PAUSING,
139 SKD_DRVR_STATE_PAUSED,
140 SKD_DRVR_STATE_DRAINING_TIMEOUT,
141 SKD_DRVR_STATE_RESTARTING,
142 SKD_DRVR_STATE_RESUMING,
143 SKD_DRVR_STATE_STOPPING,
144 SKD_DRVR_STATE_FAULT,
145 SKD_DRVR_STATE_DISAPPEARED,
146 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
147 SKD_DRVR_STATE_BUSY_ERASE,
148 SKD_DRVR_STATE_BUSY_SANITIZE,
149 SKD_DRVR_STATE_BUSY_IMMINENT,
150 SKD_DRVR_STATE_WAIT_BOOT,
151 SKD_DRVR_STATE_SYNCING,
152};
153
154#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
155#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
156#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
157#define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u)
158#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
159#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
160#define SKD_START_WAIT_SECONDS 90u
161
162enum skd_req_state {
163 SKD_REQ_STATE_IDLE,
164 SKD_REQ_STATE_SETUP,
165 SKD_REQ_STATE_BUSY,
166 SKD_REQ_STATE_COMPLETED,
167 SKD_REQ_STATE_TIMEOUT,
168 SKD_REQ_STATE_ABORTED,
169};
170
171enum skd_fit_msg_state {
172 SKD_MSG_STATE_IDLE,
173 SKD_MSG_STATE_BUSY,
174};
175
176enum skd_check_status_action {
177 SKD_CHECK_STATUS_REPORT_GOOD,
178 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
179 SKD_CHECK_STATUS_REQUEUE_REQUEST,
180 SKD_CHECK_STATUS_REPORT_ERROR,
181 SKD_CHECK_STATUS_BUSY_IMMINENT,
182};
183
Bart Van Assched891fe62017-08-17 13:13:07 -0700184struct skd_msg_buf {
185 struct fit_msg_hdr fmh;
186 struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG];
187};
188
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600189struct skd_fitmsg_context {
190 enum skd_fit_msg_state state;
191
192 struct skd_fitmsg_context *next;
193
194 u32 id;
195 u16 outstanding;
196
197 u32 length;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600198
Bart Van Assched891fe62017-08-17 13:13:07 -0700199 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600200 dma_addr_t mb_dma_address;
201};
202
203struct skd_request_context {
204 enum skd_req_state state;
205
206 struct skd_request_context *next;
207
208 u16 id;
209 u32 fitmsg_id;
210
211 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600212 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600213
214 u32 timeout_stamp;
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700215 enum dma_data_direction data_dir;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600216 struct scatterlist *sg;
217 u32 n_sg;
218 u32 sg_byte_count;
219
220 struct fit_sg_descriptor *sksg_list;
221 dma_addr_t sksg_dma_address;
222
223 struct fit_completion_entry_v1 completion;
224
225 struct fit_comp_error_info err_info;
226
227};
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600228
229struct skd_special_context {
230 struct skd_request_context req;
231
232 u8 orphaned;
233
234 void *data_buf;
235 dma_addr_t db_dma_address;
236
Bart Van Assched891fe62017-08-17 13:13:07 -0700237 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600238 dma_addr_t mb_dma_address;
239};
240
241struct skd_sg_io {
242 fmode_t mode;
243 void __user *argp;
244
245 struct sg_io_hdr sg;
246
247 u8 cdb[16];
248
249 u32 dxfer_len;
250 u32 iovcnt;
251 struct sg_iovec *iov;
252 struct sg_iovec no_iov_iov;
253
254 struct skd_special_context *skspcl;
255};
256
257typedef enum skd_irq_type {
258 SKD_IRQ_LEGACY,
259 SKD_IRQ_MSI,
260 SKD_IRQ_MSIX
261} skd_irq_type_t;
262
263#define SKD_MAX_BARS 2
264
265struct skd_device {
Bart Van Assche85e34112017-08-17 13:13:17 -0700266 void __iomem *mem_map[SKD_MAX_BARS];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600267 resource_size_t mem_phys[SKD_MAX_BARS];
268 u32 mem_size[SKD_MAX_BARS];
269
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600270 struct skd_msix_entry *msix_entries;
271
272 struct pci_dev *pdev;
273 int pcie_error_reporting_is_enabled;
274
275 spinlock_t lock;
276 struct gendisk *disk;
277 struct request_queue *queue;
278 struct device *class_dev;
279 int gendisk_on;
280 int sync_done;
281
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600282 u32 devno;
283 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600284 char isr_name[30];
285
286 enum skd_drvr_state state;
287 u32 drive_state;
288
289 u32 in_flight;
290 u32 cur_max_queue_depth;
291 u32 queue_low_water_mark;
292 u32 dev_max_queue_depth;
293
294 u32 num_fitmsg_context;
295 u32 num_req_context;
296
297 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
298 u32 timeout_stamp;
299 struct skd_fitmsg_context *skmsg_free_list;
300 struct skd_fitmsg_context *skmsg_table;
301
302 struct skd_request_context *skreq_free_list;
303 struct skd_request_context *skreq_table;
304
305 struct skd_special_context *skspcl_free_list;
306 struct skd_special_context *skspcl_table;
307
308 struct skd_special_context internal_skspcl;
309 u32 read_cap_blocksize;
310 u32 read_cap_last_lba;
311 int read_cap_is_valid;
312 int inquiry_is_valid;
313 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600314
315 u8 skcomp_cycle;
316 u32 skcomp_ix;
317 struct fit_completion_entry_v1 *skcomp_table;
318 struct fit_comp_error_info *skerr_table;
319 dma_addr_t cq_dma_address;
320
321 wait_queue_head_t waitq;
322
323 struct timer_list timer;
324 u32 timer_countdown;
325 u32 timer_substate;
326
327 int n_special;
328 int sgs_per_request;
329 u32 last_mtd;
330
331 u32 proto_ver;
332
333 int dbg_level;
334 u32 connect_time_stamp;
335 int connect_retries;
336#define SKD_MAX_CONNECT_RETRIES 16
337 u32 drive_jiffies;
338
339 u32 timo_slot;
340
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600341 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600342};
343
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600344#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
345#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
346#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
347
348static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
349{
Bart Van Assche14262a42017-08-17 13:12:57 -0700350 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600351
Bart Van Assche14262a42017-08-17 13:12:57 -0700352 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700353 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700354 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600355}
356
357static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
358 u32 offset)
359{
Bart Van Assche14262a42017-08-17 13:12:57 -0700360 writel(val, skdev->mem_map[1] + offset);
361 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700362 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600363}
364
365static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
366 u32 offset)
367{
Bart Van Assche14262a42017-08-17 13:12:57 -0700368 writeq(val, skdev->mem_map[1] + offset);
369 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700370 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
371 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600372}
373
374
375#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
376static int skd_isr_type = SKD_IRQ_DEFAULT;
377
378module_param(skd_isr_type, int, 0444);
379MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
380 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
381
382#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
383static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
384
385module_param(skd_max_req_per_msg, int, 0444);
386MODULE_PARM_DESC(skd_max_req_per_msg,
387 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700388 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600389
390#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
391#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
392static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
393
394module_param(skd_max_queue_depth, int, 0444);
395MODULE_PARM_DESC(skd_max_queue_depth,
396 "Maximum SCSI requests issued to s1120."
397 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
398
399static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
400module_param(skd_sgs_per_request, int, 0444);
401MODULE_PARM_DESC(skd_sgs_per_request,
402 "Maximum SG elements per block request."
403 " (1-4096, default==256)");
404
405static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
406module_param(skd_max_pass_thru, int, 0444);
407MODULE_PARM_DESC(skd_max_pass_thru,
408 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
409
410module_param(skd_dbg_level, int, 0444);
411MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
412
413module_param(skd_isr_comp_limit, int, 0444);
414MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
415
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600416/* Major device number dynamically assigned. */
417static u32 skd_major;
418
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600419static void skd_destruct(struct skd_device *skdev);
420static const struct block_device_operations skd_blockdev_ops;
421static void skd_send_fitmsg(struct skd_device *skdev,
422 struct skd_fitmsg_context *skmsg);
423static void skd_send_special_fitmsg(struct skd_device *skdev,
424 struct skd_special_context *skspcl);
425static void skd_request_fn(struct request_queue *rq);
426static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200427 struct skd_request_context *skreq, blk_status_t status);
428static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600429 struct skd_request_context *skreq);
430static void skd_postop_sg_list(struct skd_device *skdev,
431 struct skd_request_context *skreq);
432
433static void skd_restart_device(struct skd_device *skdev);
434static int skd_quiesce_dev(struct skd_device *skdev);
435static int skd_unquiesce_dev(struct skd_device *skdev);
436static void skd_release_special(struct skd_device *skdev,
437 struct skd_special_context *skspcl);
438static void skd_disable_interrupts(struct skd_device *skdev);
439static void skd_isr_fwstate(struct skd_device *skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700440static void skd_recover_requests(struct skd_device *skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600441static void skd_soft_reset(struct skd_device *skdev);
442
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600443const char *skd_drive_state_to_str(int state);
444const char *skd_skdev_state_to_str(enum skd_drvr_state state);
445static void skd_log_skdev(struct skd_device *skdev, const char *event);
446static void skd_log_skmsg(struct skd_device *skdev,
447 struct skd_fitmsg_context *skmsg, const char *event);
448static void skd_log_skreq(struct skd_device *skdev,
449 struct skd_request_context *skreq, const char *event);
450
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600451/*
452 *****************************************************************************
453 * READ/WRITE REQUESTS
454 *****************************************************************************
455 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600456static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600457{
458 struct request_queue *q = skdev->queue;
459 struct request *req;
460
461 for (;; ) {
462 req = blk_peek_request(q);
463 if (req == NULL)
464 break;
465 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200466 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600467 }
468}
469
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600470static void
471skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
472 int data_dir, unsigned lba,
473 unsigned count)
474{
475 if (data_dir == READ)
476 scsi_req->cdb[0] = 0x28;
477 else
478 scsi_req->cdb[0] = 0x2a;
479
480 scsi_req->cdb[1] = 0;
481 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
482 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
483 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
484 scsi_req->cdb[5] = (lba & 0xff);
485 scsi_req->cdb[6] = 0;
486 scsi_req->cdb[7] = (count & 0xff00) >> 8;
487 scsi_req->cdb[8] = count & 0xff;
488 scsi_req->cdb[9] = 0;
489}
490
491static void
492skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400493 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600494{
495 skreq->flush_cmd = 1;
496
497 scsi_req->cdb[0] = 0x35;
498 scsi_req->cdb[1] = 0;
499 scsi_req->cdb[2] = 0;
500 scsi_req->cdb[3] = 0;
501 scsi_req->cdb[4] = 0;
502 scsi_req->cdb[5] = 0;
503 scsi_req->cdb[6] = 0;
504 scsi_req->cdb[7] = 0;
505 scsi_req->cdb[8] = 0;
506 scsi_req->cdb[9] = 0;
507}
508
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600509static void skd_request_fn_not_online(struct request_queue *q);
510
511static void skd_request_fn(struct request_queue *q)
512{
513 struct skd_device *skdev = q->queuedata;
514 struct skd_fitmsg_context *skmsg = NULL;
515 struct fit_msg_hdr *fmh = NULL;
516 struct skd_request_context *skreq;
517 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600518 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600519 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600520 u32 lba;
521 u32 count;
522 int data_dir;
Bart Van Assche4854afe2017-08-17 13:12:59 -0700523 __be64 be_dmaa;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600524 u64 cmdctxt;
525 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600526 int flush, fua;
527
528 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
529 skd_request_fn_not_online(q);
530 return;
531 }
532
Jens Axboe6a5ec652013-11-01 10:38:45 -0600533 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600534 if (skdev->skmsg_free_list == NULL ||
535 skdev->skreq_free_list == NULL ||
536 skdev->in_flight >= skdev->queue_low_water_mark)
537 /* There is still some kind of shortage */
538 return;
539
Jens Axboe6a5ec652013-11-01 10:38:45 -0600540 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600541 }
542
543 /*
544 * Stop conditions:
545 * - There are no more native requests
546 * - There are already the maximum number of requests in progress
547 * - There are no more skd_request_context entries
548 * - There are no more FIT msg buffers
549 */
550 for (;; ) {
551
552 flush = fua = 0;
553
Jens Axboefcd37eb2013-11-01 10:14:56 -0600554 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600555
Jens Axboefcd37eb2013-11-01 10:14:56 -0600556 /* Are there any native requests to start? */
557 if (req == NULL)
558 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600559
Jens Axboefcd37eb2013-11-01 10:14:56 -0600560 lba = (u32)blk_rq_pos(req);
561 count = blk_rq_sectors(req);
562 data_dir = rq_data_dir(req);
563 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600564
Mike Christie3a5e02c2016-06-05 14:32:23 -0500565 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600566 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600567
Jens Axboefcd37eb2013-11-01 10:14:56 -0600568 if (io_flags & REQ_FUA)
569 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600570
Bart Van Asschef98806d2017-08-17 13:12:58 -0700571 dev_dbg(&skdev->pdev->dev,
572 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
573 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600574
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400575 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600576
577 /* Are too many requets already in progress? */
578 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700579 dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
580 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600581 break;
582 }
583
584 /* Is a skd_request_context available? */
585 skreq = skdev->skreq_free_list;
586 if (skreq == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700587 dev_dbg(&skdev->pdev->dev, "Out of req=%p\n", q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600588 break;
589 }
590 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
591 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
592
593 /* Now we check to see if we can get a fit msg */
594 if (skmsg == NULL) {
595 if (skdev->skmsg_free_list == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700596 dev_dbg(&skdev->pdev->dev, "Out of msg\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600597 break;
598 }
599 }
600
601 skreq->flush_cmd = 0;
602 skreq->n_sg = 0;
603 skreq->sg_byte_count = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600604
605 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400606 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600607 *
608 * At this point we are comitted to either start or reject
609 * the native request. Note that skd_request_context is
610 * available but is still at the head of the free list.
611 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600612 blk_start_request(req);
613 skreq->req = req;
614 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600615
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700616 skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE :
617 DMA_TO_DEVICE;
Bart Van Assche19fc85c2017-08-17 13:13:04 -0700618
619 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
620 dev_dbg(&skdev->pdev->dev, "error Out\n");
621 skd_end_request(skdev, skreq, BLK_STS_RESOURCE);
622 continue;
623 }
624
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600625 /* Either a FIT msg is in progress or we have to start one. */
626 if (skmsg == NULL) {
627 /* Are there any FIT msg buffers available? */
628 skmsg = skdev->skmsg_free_list;
629 if (skmsg == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700630 dev_dbg(&skdev->pdev->dev,
631 "Out of msg skdev=%p\n",
632 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600633 break;
634 }
635 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
636 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
637
638 skdev->skmsg_free_list = skmsg->next;
639
640 skmsg->state = SKD_MSG_STATE_BUSY;
641 skmsg->id += SKD_ID_INCR;
642
643 /* Initialize the FIT msg header */
Bart Van Assched891fe62017-08-17 13:13:07 -0700644 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600645 memset(fmh, 0, sizeof(*fmh));
646 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
647 skmsg->length = sizeof(*fmh);
648 }
649
650 skreq->fitmsg_id = skmsg->id;
651
Bart Van Assched891fe62017-08-17 13:13:07 -0700652 scsi_req =
653 &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
654 memset(scsi_req, 0, sizeof(*scsi_req));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600655
Bart Van Assche4854afe2017-08-17 13:12:59 -0700656 be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600657 cmdctxt = skreq->id + SKD_ID_INCR;
658
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600659 scsi_req->hdr.tag = cmdctxt;
660 scsi_req->hdr.sg_list_dma_address = be_dmaa;
661
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600662 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600663 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
664 SKD_ASSERT(skreq->flush_cmd == 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600665 } else {
666 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
667 }
668
669 if (fua)
670 scsi_req->cdb[1] |= SKD_FUA_NV;
671
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600672 scsi_req->hdr.sg_list_len_bytes =
673 cpu_to_be32(skreq->sg_byte_count);
674
675 /* Complete resource allocations. */
676 skdev->skreq_free_list = skreq->next;
677 skreq->state = SKD_REQ_STATE_BUSY;
678 skreq->id += SKD_ID_INCR;
679
680 skmsg->length += sizeof(struct skd_scsi_request);
681 fmh->num_protocol_cmds_coalesced++;
682
683 /*
684 * Update the active request counts.
685 * Capture the timeout timestamp.
686 */
687 skreq->timeout_stamp = skdev->timeout_stamp;
688 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
689 skdev->timeout_slot[timo_slot]++;
690 skdev->in_flight++;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700691 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
692 skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600693
694 /*
695 * If the FIT msg buffer is full send it.
696 */
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700697 if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600698 skd_send_fitmsg(skdev, skmsg);
699 skmsg = NULL;
700 fmh = NULL;
701 }
702 }
703
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700704 /* If the FIT msg buffer is not empty send what we got. */
705 if (skmsg) {
706 WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced);
707 skd_send_fitmsg(skdev, skmsg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600708 skmsg = NULL;
709 fmh = NULL;
710 }
711
712 /*
713 * If req is non-NULL it means there is something to do but
714 * we are out of a resource.
715 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600716 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600717 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600718}
719
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400720static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200721 struct skd_request_context *skreq, blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600722{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600723 if (unlikely(error)) {
724 struct request *req = skreq->req;
725 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
726 u32 lba = (u32)blk_rq_pos(req);
727 u32 count = blk_rq_sectors(req);
728
Bart Van Asschef98806d2017-08-17 13:12:58 -0700729 dev_err(&skdev->pdev->dev,
730 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
731 count, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600732 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -0700733 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", skreq->id,
734 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600735
736 __blk_end_request_all(skreq->req, error);
737}
738
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200739static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400740 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600741{
742 struct request *req = skreq->req;
Bart Van Assche06f824c42017-08-17 13:13:15 -0700743 struct scatterlist *sgl = &skreq->sg[0], *sg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600744 int n_sg;
745 int i;
746
747 skreq->sg_byte_count = 0;
748
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700749 WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE &&
750 skreq->data_dir != DMA_FROM_DEVICE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600751
Bart Van Assche06f824c42017-08-17 13:13:15 -0700752 n_sg = blk_rq_map_sg(skdev->queue, req, sgl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600753 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200754 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600755
756 /*
757 * Map scatterlist to PCI bus addresses.
758 * Note PCI might change the number of entries.
759 */
Bart Van Assche06f824c42017-08-17 13:13:15 -0700760 n_sg = pci_map_sg(skdev->pdev, sgl, n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600761 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200762 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600763
764 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
765
766 skreq->n_sg = n_sg;
767
Bart Van Assche06f824c42017-08-17 13:13:15 -0700768 for_each_sg(sgl, sg, n_sg, i) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600769 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Assche06f824c42017-08-17 13:13:15 -0700770 u32 cnt = sg_dma_len(sg);
771 uint64_t dma_addr = sg_dma_address(sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600772
773 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
774 sgd->byte_count = cnt;
775 skreq->sg_byte_count += cnt;
776 sgd->host_side_addr = dma_addr;
777 sgd->dev_side_addr = 0;
778 }
779
780 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
781 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
782
783 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700784 dev_dbg(&skdev->pdev->dev,
785 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
786 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600787 for (i = 0; i < n_sg; i++) {
788 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700789
790 dev_dbg(&skdev->pdev->dev,
791 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
792 i, sgd->byte_count, sgd->control,
793 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600794 }
795 }
796
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200797 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600798}
799
Jens Axboefcd37eb2013-11-01 10:14:56 -0600800static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400801 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600802{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600803 /*
804 * restore the next ptr for next IO request so we
805 * don't have to set it every time.
806 */
807 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
808 skreq->sksg_dma_address +
809 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
Bart Van Asscheb1824ee2017-08-17 13:13:12 -0700810 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, skreq->data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600811}
812
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600813static void skd_request_fn_not_online(struct request_queue *q)
814{
815 struct skd_device *skdev = q->queuedata;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600816
817 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
818
819 skd_log_skdev(skdev, "req_not_online");
820 switch (skdev->state) {
821 case SKD_DRVR_STATE_PAUSING:
822 case SKD_DRVR_STATE_PAUSED:
823 case SKD_DRVR_STATE_STARTING:
824 case SKD_DRVR_STATE_RESTARTING:
825 case SKD_DRVR_STATE_WAIT_BOOT:
826 /* In case of starting, we haven't started the queue,
827 * so we can't get here... but requests are
828 * possibly hanging out waiting for us because we
829 * reported the dev/skd0 already. They'll wait
830 * forever if connect doesn't complete.
831 * What to do??? delay dev/skd0 ??
832 */
833 case SKD_DRVR_STATE_BUSY:
834 case SKD_DRVR_STATE_BUSY_IMMINENT:
835 case SKD_DRVR_STATE_BUSY_ERASE:
836 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
837 return;
838
839 case SKD_DRVR_STATE_BUSY_SANITIZE:
840 case SKD_DRVR_STATE_STOPPING:
841 case SKD_DRVR_STATE_SYNCING:
842 case SKD_DRVR_STATE_FAULT:
843 case SKD_DRVR_STATE_DISAPPEARED:
844 default:
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600845 break;
846 }
847
848 /* If we get here, terminate all pending block requeusts
849 * with EIO and any scsi pass thru with appropriate sense
850 */
851
852 skd_fail_all_pending(skdev);
853}
854
855/*
856 *****************************************************************************
857 * TIMER
858 *****************************************************************************
859 */
860
861static void skd_timer_tick_not_online(struct skd_device *skdev);
862
863static void skd_timer_tick(ulong arg)
864{
865 struct skd_device *skdev = (struct skd_device *)arg;
866
867 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600868 unsigned long reqflags;
869 u32 state;
870
871 if (skdev->state == SKD_DRVR_STATE_FAULT)
872 /* The driver has declared fault, and we want it to
873 * stay that way until driver is reloaded.
874 */
875 return;
876
877 spin_lock_irqsave(&skdev->lock, reqflags);
878
879 state = SKD_READL(skdev, FIT_STATUS);
880 state &= FIT_SR_DRIVE_STATE_MASK;
881 if (state != skdev->drive_state)
882 skd_isr_fwstate(skdev);
883
884 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
885 skd_timer_tick_not_online(skdev);
886 goto timer_func_out;
887 }
888 skdev->timeout_stamp++;
889 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
890
891 /*
892 * All requests that happened during the previous use of
893 * this slot should be done by now. The previous use was
894 * over 7 seconds ago.
895 */
896 if (skdev->timeout_slot[timo_slot] == 0)
897 goto timer_func_out;
898
899 /* Something is overdue */
Bart Van Asschef98806d2017-08-17 13:12:58 -0700900 dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
901 skdev->timeout_slot[timo_slot], skdev->in_flight);
902 dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
903 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600904
905 skdev->timer_countdown = SKD_DRAINING_TIMO;
906 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
907 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600908 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600909
910timer_func_out:
911 mod_timer(&skdev->timer, (jiffies + HZ));
912
913 spin_unlock_irqrestore(&skdev->lock, reqflags);
914}
915
916static void skd_timer_tick_not_online(struct skd_device *skdev)
917{
918 switch (skdev->state) {
919 case SKD_DRVR_STATE_IDLE:
920 case SKD_DRVR_STATE_LOAD:
921 break;
922 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700923 dev_dbg(&skdev->pdev->dev,
924 "drive busy sanitize[%x], driver[%x]\n",
925 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600926 /* If we've been in sanitize for 3 seconds, we figure we're not
927 * going to get anymore completions, so recover requests now
928 */
929 if (skdev->timer_countdown > 0) {
930 skdev->timer_countdown--;
931 return;
932 }
Bart Van Assche79ce12a2017-08-17 13:13:14 -0700933 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600934 break;
935
936 case SKD_DRVR_STATE_BUSY:
937 case SKD_DRVR_STATE_BUSY_IMMINENT:
938 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700939 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
940 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600941 if (skdev->timer_countdown > 0) {
942 skdev->timer_countdown--;
943 return;
944 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700945 dev_dbg(&skdev->pdev->dev,
946 "busy[%x], timedout=%d, restarting device.",
947 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600948 skd_restart_device(skdev);
949 break;
950
951 case SKD_DRVR_STATE_WAIT_BOOT:
952 case SKD_DRVR_STATE_STARTING:
953 if (skdev->timer_countdown > 0) {
954 skdev->timer_countdown--;
955 return;
956 }
957 /* For now, we fault the drive. Could attempt resets to
958 * revcover at some point. */
959 skdev->state = SKD_DRVR_STATE_FAULT;
960
Bart Van Asschef98806d2017-08-17 13:12:58 -0700961 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
962 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600963
964 /*start the queue so we can respond with error to requests */
965 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600966 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600967 skdev->gendisk_on = -1;
968 wake_up_interruptible(&skdev->waitq);
969 break;
970
971 case SKD_DRVR_STATE_ONLINE:
972 /* shouldn't get here. */
973 break;
974
975 case SKD_DRVR_STATE_PAUSING:
976 case SKD_DRVR_STATE_PAUSED:
977 break;
978
979 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700980 dev_dbg(&skdev->pdev->dev,
981 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
982 skdev->timo_slot, skdev->timer_countdown,
983 skdev->in_flight,
984 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600985 /* if the slot has cleared we can let the I/O continue */
986 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700987 dev_dbg(&skdev->pdev->dev,
988 "Slot drained, starting queue.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600989 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600990 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600991 return;
992 }
993 if (skdev->timer_countdown > 0) {
994 skdev->timer_countdown--;
995 return;
996 }
997 skd_restart_device(skdev);
998 break;
999
1000 case SKD_DRVR_STATE_RESTARTING:
1001 if (skdev->timer_countdown > 0) {
1002 skdev->timer_countdown--;
1003 return;
1004 }
1005 /* For now, we fault the drive. Could attempt resets to
1006 * revcover at some point. */
1007 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001008 dev_err(&skdev->pdev->dev,
1009 "DriveFault Reconnect Timeout (%x)\n",
1010 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001011
1012 /*
1013 * Recovering does two things:
1014 * 1. completes IO with error
1015 * 2. reclaims dma resources
1016 * When is it safe to recover requests?
1017 * - if the drive state is faulted
1018 * - if the state is still soft reset after out timeout
1019 * - if the drive registers are dead (state = FF)
1020 * If it is "unsafe", we still need to recover, so we will
1021 * disable pci bus mastering and disable our interrupts.
1022 */
1023
1024 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1025 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1026 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1027 /* It never came out of soft reset. Try to
1028 * recover the requests and then let them
1029 * fail. This is to mitigate hung processes. */
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001030 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001031 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001032 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
1033 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001034 pci_disable_device(skdev->pdev);
1035 skd_disable_interrupts(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07001036 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001037 }
1038
1039 /*start the queue so we can respond with error to requests */
1040 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001041 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001042 skdev->gendisk_on = -1;
1043 wake_up_interruptible(&skdev->waitq);
1044 break;
1045
1046 case SKD_DRVR_STATE_RESUMING:
1047 case SKD_DRVR_STATE_STOPPING:
1048 case SKD_DRVR_STATE_SYNCING:
1049 case SKD_DRVR_STATE_FAULT:
1050 case SKD_DRVR_STATE_DISAPPEARED:
1051 default:
1052 break;
1053 }
1054}
1055
1056static int skd_start_timer(struct skd_device *skdev)
1057{
1058 int rc;
1059
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001060 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1061
1062 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1063 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001064 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001065 return rc;
1066}
1067
1068static void skd_kill_timer(struct skd_device *skdev)
1069{
1070 del_timer_sync(&skdev->timer);
1071}
1072
1073/*
1074 *****************************************************************************
1075 * IOCTL
1076 *****************************************************************************
1077 */
1078static int skd_ioctl_sg_io(struct skd_device *skdev,
1079 fmode_t mode, void __user *argp);
1080static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1081 struct skd_sg_io *sksgio);
1082static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1083 struct skd_sg_io *sksgio);
1084static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1085 struct skd_sg_io *sksgio);
1086static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1087 struct skd_sg_io *sksgio, int dxfer_dir);
1088static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1089 struct skd_sg_io *sksgio);
1090static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1091static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1092 struct skd_sg_io *sksgio);
1093static int skd_sg_io_put_status(struct skd_device *skdev,
1094 struct skd_sg_io *sksgio);
1095
1096static void skd_complete_special(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001097 struct fit_completion_entry_v1 *skcomp,
1098 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001099 struct skd_special_context *skspcl);
1100
1101static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1102 uint cmd_in, ulong arg)
1103{
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001104 static const int sg_version_num = 30527;
1105 int rc = 0, timeout;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001106 struct gendisk *disk = bdev->bd_disk;
1107 struct skd_device *skdev = disk->private_data;
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001108 int __user *p = (int __user *)arg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001109
Bart Van Asschef98806d2017-08-17 13:12:58 -07001110 dev_dbg(&skdev->pdev->dev,
1111 "%s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1112 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001113
1114 if (!capable(CAP_SYS_ADMIN))
1115 return -EPERM;
1116
1117 switch (cmd_in) {
1118 case SG_SET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001119 rc = get_user(timeout, p);
1120 if (!rc)
1121 disk->queue->sg_timeout = clock_t_to_jiffies(timeout);
1122 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001123 case SG_GET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001124 rc = jiffies_to_clock_t(disk->queue->sg_timeout);
1125 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001126 case SG_GET_VERSION_NUM:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001127 rc = put_user(sg_version_num, p);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001128 break;
1129 case SG_IO:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001130 rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001131 break;
1132
1133 default:
1134 rc = -ENOTTY;
1135 break;
1136 }
1137
Bart Van Asschef98806d2017-08-17 13:12:58 -07001138 dev_dbg(&skdev->pdev->dev, "%s: completion rc %d\n", disk->disk_name,
1139 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001140 return rc;
1141}
1142
1143static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1144 void __user *argp)
1145{
1146 int rc;
1147 struct skd_sg_io sksgio;
1148
1149 memset(&sksgio, 0, sizeof(sksgio));
1150 sksgio.mode = mode;
1151 sksgio.argp = argp;
1152 sksgio.iov = &sksgio.no_iov_iov;
1153
1154 switch (skdev->state) {
1155 case SKD_DRVR_STATE_ONLINE:
1156 case SKD_DRVR_STATE_BUSY_IMMINENT:
1157 break;
1158
1159 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001160 dev_dbg(&skdev->pdev->dev, "drive not online\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001161 rc = -ENXIO;
1162 goto out;
1163 }
1164
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001165 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1166 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001167 goto out;
1168
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001169 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1170 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001171 goto out;
1172
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001173 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1174 if (rc)
1175 goto out;
1176
1177 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1178 if (rc)
1179 goto out;
1180
1181 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1182 if (rc)
1183 goto out;
1184
1185 rc = skd_sg_io_await(skdev, &sksgio);
1186 if (rc)
1187 goto out;
1188
1189 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1190 if (rc)
1191 goto out;
1192
1193 rc = skd_sg_io_put_status(skdev, &sksgio);
1194 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001195 goto out;
1196
1197 rc = 0;
1198
1199out:
1200 skd_sg_io_release_skspcl(skdev, &sksgio);
1201
1202 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1203 kfree(sksgio.iov);
1204 return rc;
1205}
1206
1207static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1208 struct skd_sg_io *sksgio)
1209{
1210 struct sg_io_hdr *sgp = &sksgio->sg;
Bart Van Assche95895e12017-08-17 13:12:55 -07001211 int i, __maybe_unused acc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001212
1213 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001214 dev_dbg(&skdev->pdev->dev, "access sg failed %p\n",
1215 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001216 return -EFAULT;
1217 }
1218
1219 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001220 dev_dbg(&skdev->pdev->dev, "copy_from_user sg failed %p\n",
1221 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001222 return -EFAULT;
1223 }
1224
1225 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001226 dev_dbg(&skdev->pdev->dev, "interface_id invalid 0x%x\n",
1227 sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001228 return -EINVAL;
1229 }
1230
1231 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001232 dev_dbg(&skdev->pdev->dev, "cmd_len invalid %d\n",
1233 sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001234 return -EINVAL;
1235 }
1236
1237 if (sgp->iovec_count > 256) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001238 dev_dbg(&skdev->pdev->dev, "iovec_count invalid %d\n",
1239 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001240 return -EINVAL;
1241 }
1242
1243 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001244 dev_dbg(&skdev->pdev->dev, "dxfer_len invalid %d\n",
1245 sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001246 return -EINVAL;
1247 }
1248
1249 switch (sgp->dxfer_direction) {
1250 case SG_DXFER_NONE:
1251 acc = -1;
1252 break;
1253
1254 case SG_DXFER_TO_DEV:
1255 acc = VERIFY_READ;
1256 break;
1257
1258 case SG_DXFER_FROM_DEV:
1259 case SG_DXFER_TO_FROM_DEV:
1260 acc = VERIFY_WRITE;
1261 break;
1262
1263 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001264 dev_dbg(&skdev->pdev->dev, "dxfer_dir invalid %d\n",
1265 sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001266 return -EINVAL;
1267 }
1268
1269 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001270 dev_dbg(&skdev->pdev->dev, "copy_from_user cmdp failed %p\n",
1271 sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001272 return -EFAULT;
1273 }
1274
1275 if (sgp->mx_sb_len != 0) {
1276 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001277 dev_dbg(&skdev->pdev->dev, "access sbp failed %p\n",
1278 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001279 return -EFAULT;
1280 }
1281 }
1282
1283 if (sgp->iovec_count == 0) {
1284 sksgio->iov[0].iov_base = sgp->dxferp;
1285 sksgio->iov[0].iov_len = sgp->dxfer_len;
1286 sksgio->iovcnt = 1;
1287 sksgio->dxfer_len = sgp->dxfer_len;
1288 } else {
1289 struct sg_iovec *iov;
1290 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1291 size_t iov_data_len;
1292
1293 iov = kmalloc(nbytes, GFP_KERNEL);
1294 if (iov == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001295 dev_dbg(&skdev->pdev->dev, "alloc iovec failed %d\n",
1296 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001297 return -ENOMEM;
1298 }
1299 sksgio->iov = iov;
1300 sksgio->iovcnt = sgp->iovec_count;
1301
1302 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001303 dev_dbg(&skdev->pdev->dev,
1304 "copy_from_user iovec failed %p\n",
1305 sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001306 return -EFAULT;
1307 }
1308
1309 /*
1310 * Sum up the vecs, making sure they don't overflow
1311 */
1312 iov_data_len = 0;
1313 for (i = 0; i < sgp->iovec_count; i++) {
1314 if (iov_data_len + iov[i].iov_len < iov_data_len)
1315 return -EINVAL;
1316 iov_data_len += iov[i].iov_len;
1317 }
1318
1319 /* SG_IO howto says that the shorter of the two wins */
1320 if (sgp->dxfer_len < iov_data_len) {
1321 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1322 sgp->iovec_count,
1323 sgp->dxfer_len);
1324 sksgio->dxfer_len = sgp->dxfer_len;
1325 } else
1326 sksgio->dxfer_len = iov_data_len;
1327 }
1328
1329 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1330 struct sg_iovec *iov = sksgio->iov;
1331 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1332 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001333 dev_dbg(&skdev->pdev->dev,
1334 "access data failed %p/%zd\n",
1335 iov->iov_base, iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001336 return -EFAULT;
1337 }
1338 }
1339 }
1340
1341 return 0;
1342}
1343
1344static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1345 struct skd_sg_io *sksgio)
1346{
1347 struct skd_special_context *skspcl = NULL;
1348 int rc;
1349
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001350 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001351 ulong flags;
1352
1353 spin_lock_irqsave(&skdev->lock, flags);
1354 skspcl = skdev->skspcl_free_list;
1355 if (skspcl != NULL) {
1356 skdev->skspcl_free_list =
1357 (struct skd_special_context *)skspcl->req.next;
1358 skspcl->req.id += SKD_ID_INCR;
1359 skspcl->req.state = SKD_REQ_STATE_SETUP;
1360 skspcl->orphaned = 0;
1361 skspcl->req.n_sg = 0;
1362 }
1363 spin_unlock_irqrestore(&skdev->lock, flags);
1364
1365 if (skspcl != NULL) {
1366 rc = 0;
1367 break;
1368 }
1369
Bart Van Asschef98806d2017-08-17 13:12:58 -07001370 dev_dbg(&skdev->pdev->dev, "blocking\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001371
1372 rc = wait_event_interruptible_timeout(
1373 skdev->waitq,
1374 (skdev->skspcl_free_list != NULL),
1375 msecs_to_jiffies(sksgio->sg.timeout));
1376
Bart Van Asschef98806d2017-08-17 13:12:58 -07001377 dev_dbg(&skdev->pdev->dev, "unblocking, rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001378
1379 if (rc <= 0) {
1380 if (rc == 0)
1381 rc = -ETIMEDOUT;
1382 else
1383 rc = -EINTR;
1384 break;
1385 }
1386 /*
1387 * If we get here rc > 0 meaning the timeout to
1388 * wait_event_interruptible_timeout() had time left, hence the
1389 * sought event -- non-empty free list -- happened.
1390 * Retry the allocation.
1391 */
1392 }
1393 sksgio->skspcl = skspcl;
1394
1395 return rc;
1396}
1397
1398static int skd_skreq_prep_buffering(struct skd_device *skdev,
1399 struct skd_request_context *skreq,
1400 u32 dxfer_len)
1401{
1402 u32 resid = dxfer_len;
1403
1404 /*
1405 * The DMA engine must have aligned addresses and byte counts.
1406 */
1407 resid += (-resid) & 3;
1408 skreq->sg_byte_count = resid;
1409
1410 skreq->n_sg = 0;
1411
1412 while (resid > 0) {
1413 u32 nbytes = PAGE_SIZE;
1414 u32 ix = skreq->n_sg;
1415 struct scatterlist *sg = &skreq->sg[ix];
1416 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1417 struct page *page;
1418
1419 if (nbytes > resid)
1420 nbytes = resid;
1421
1422 page = alloc_page(GFP_KERNEL);
1423 if (page == NULL)
1424 return -ENOMEM;
1425
1426 sg_set_page(sg, page, nbytes, 0);
1427
1428 /* TODO: This should be going through a pci_???()
1429 * routine to do proper mapping. */
1430 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1431 sksg->byte_count = nbytes;
1432
1433 sksg->host_side_addr = sg_phys(sg);
1434
1435 sksg->dev_side_addr = 0;
1436 sksg->next_desc_ptr = skreq->sksg_dma_address +
1437 (ix + 1) * sizeof(*sksg);
1438
1439 skreq->n_sg++;
1440 resid -= nbytes;
1441 }
1442
1443 if (skreq->n_sg > 0) {
1444 u32 ix = skreq->n_sg - 1;
1445 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1446
1447 sksg->control = FIT_SGD_CONTROL_LAST;
1448 sksg->next_desc_ptr = 0;
1449 }
1450
1451 if (unlikely(skdev->dbg_level > 1)) {
1452 u32 i;
1453
Bart Van Asschef98806d2017-08-17 13:12:58 -07001454 dev_dbg(&skdev->pdev->dev,
1455 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
1456 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001457 for (i = 0; i < skreq->n_sg; i++) {
1458 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1459
Bart Van Asschef98806d2017-08-17 13:12:58 -07001460 dev_dbg(&skdev->pdev->dev,
1461 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1462 i, sgd->byte_count, sgd->control,
1463 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001464 }
1465 }
1466
1467 return 0;
1468}
1469
1470static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1471 struct skd_sg_io *sksgio)
1472{
1473 struct skd_special_context *skspcl = sksgio->skspcl;
1474 struct skd_request_context *skreq = &skspcl->req;
1475 u32 dxfer_len = sksgio->dxfer_len;
1476 int rc;
1477
1478 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1479 /*
1480 * Eventually, errors or not, skd_release_special() is called
1481 * to recover allocations including partial allocations.
1482 */
1483 return rc;
1484}
1485
1486static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1487 struct skd_sg_io *sksgio, int dxfer_dir)
1488{
1489 struct skd_special_context *skspcl = sksgio->skspcl;
1490 u32 iov_ix = 0;
1491 struct sg_iovec curiov;
1492 u32 sksg_ix = 0;
1493 u8 *bufp = NULL;
1494 u32 buf_len = 0;
1495 u32 resid = sksgio->dxfer_len;
1496 int rc;
1497
1498 curiov.iov_len = 0;
1499 curiov.iov_base = NULL;
1500
1501 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1502 if (dxfer_dir != SG_DXFER_TO_DEV ||
1503 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1504 return 0;
1505 }
1506
1507 while (resid > 0) {
1508 u32 nbytes = PAGE_SIZE;
1509
1510 if (curiov.iov_len == 0) {
1511 curiov = sksgio->iov[iov_ix++];
1512 continue;
1513 }
1514
1515 if (buf_len == 0) {
1516 struct page *page;
1517 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1518 bufp = page_address(page);
1519 buf_len = PAGE_SIZE;
1520 }
1521
1522 nbytes = min_t(u32, nbytes, resid);
1523 nbytes = min_t(u32, nbytes, curiov.iov_len);
1524 nbytes = min_t(u32, nbytes, buf_len);
1525
1526 if (dxfer_dir == SG_DXFER_TO_DEV)
1527 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1528 else
1529 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1530
1531 if (rc)
1532 return -EFAULT;
1533
1534 resid -= nbytes;
1535 curiov.iov_len -= nbytes;
1536 curiov.iov_base += nbytes;
1537 buf_len -= nbytes;
1538 }
1539
1540 return 0;
1541}
1542
1543static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1544 struct skd_sg_io *sksgio)
1545{
1546 struct skd_special_context *skspcl = sksgio->skspcl;
Bart Van Assched891fe62017-08-17 13:13:07 -07001547 struct fit_msg_hdr *fmh = &skspcl->msg_buf->fmh;
1548 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001549
1550 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1551
1552 /* Initialize the FIT msg header */
1553 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1554 fmh->num_protocol_cmds_coalesced = 1;
1555
1556 /* Initialize the SCSI request */
1557 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1558 scsi_req->hdr.sg_list_dma_address =
1559 cpu_to_be64(skspcl->req.sksg_dma_address);
1560 scsi_req->hdr.tag = skspcl->req.id;
1561 scsi_req->hdr.sg_list_len_bytes =
1562 cpu_to_be32(skspcl->req.sg_byte_count);
1563 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1564
1565 skspcl->req.state = SKD_REQ_STATE_BUSY;
1566 skd_send_special_fitmsg(skdev, skspcl);
1567
1568 return 0;
1569}
1570
1571static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1572{
1573 unsigned long flags;
1574 int rc;
1575
1576 rc = wait_event_interruptible_timeout(skdev->waitq,
1577 (sksgio->skspcl->req.state !=
1578 SKD_REQ_STATE_BUSY),
1579 msecs_to_jiffies(sksgio->sg.
1580 timeout));
1581
1582 spin_lock_irqsave(&skdev->lock, flags);
1583
1584 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001585 dev_dbg(&skdev->pdev->dev, "skspcl %p aborted\n",
1586 sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001587
1588 /* Build check cond, sense and let command finish. */
1589 /* For a timeout, we must fabricate completion and sense
1590 * data to complete the command */
1591 sksgio->skspcl->req.completion.status =
1592 SAM_STAT_CHECK_CONDITION;
1593
1594 memset(&sksgio->skspcl->req.err_info, 0,
1595 sizeof(sksgio->skspcl->req.err_info));
1596 sksgio->skspcl->req.err_info.type = 0x70;
1597 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1598 sksgio->skspcl->req.err_info.code = 0x44;
1599 sksgio->skspcl->req.err_info.qual = 0;
1600 rc = 0;
1601 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1602 /* No longer on the adapter. We finish. */
1603 rc = 0;
1604 else {
1605 /* Something's gone wrong. Still busy. Timeout or
1606 * user interrupted (control-C). Mark as an orphan
1607 * so it will be disposed when completed. */
1608 sksgio->skspcl->orphaned = 1;
1609 sksgio->skspcl = NULL;
1610 if (rc == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001611 dev_dbg(&skdev->pdev->dev, "timed out %p (%u ms)\n",
1612 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001613 rc = -ETIMEDOUT;
1614 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001615 dev_dbg(&skdev->pdev->dev, "cntlc %p\n", sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001616 rc = -EINTR;
1617 }
1618 }
1619
1620 spin_unlock_irqrestore(&skdev->lock, flags);
1621
1622 return rc;
1623}
1624
1625static int skd_sg_io_put_status(struct skd_device *skdev,
1626 struct skd_sg_io *sksgio)
1627{
1628 struct sg_io_hdr *sgp = &sksgio->sg;
1629 struct skd_special_context *skspcl = sksgio->skspcl;
1630 int resid = 0;
1631
1632 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1633
1634 sgp->status = skspcl->req.completion.status;
1635 resid = sksgio->dxfer_len - nb;
1636
1637 sgp->masked_status = sgp->status & STATUS_MASK;
1638 sgp->msg_status = 0;
1639 sgp->host_status = 0;
1640 sgp->driver_status = 0;
1641 sgp->resid = resid;
1642 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1643 sgp->info |= SG_INFO_CHECK;
1644
Bart Van Asschef98806d2017-08-17 13:12:58 -07001645 dev_dbg(&skdev->pdev->dev, "status %x masked %x resid 0x%x\n",
1646 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001647
1648 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1649 if (sgp->mx_sb_len > 0) {
1650 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1651 u32 nbytes = sizeof(*ei);
1652
1653 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1654
1655 sgp->sb_len_wr = nbytes;
1656
1657 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001658 dev_dbg(&skdev->pdev->dev,
1659 "copy_to_user sense failed %p\n",
1660 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001661 return -EFAULT;
1662 }
1663 }
1664 }
1665
1666 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001667 dev_dbg(&skdev->pdev->dev, "copy_to_user sg failed %p\n",
1668 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001669 return -EFAULT;
1670 }
1671
1672 return 0;
1673}
1674
1675static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1676 struct skd_sg_io *sksgio)
1677{
1678 struct skd_special_context *skspcl = sksgio->skspcl;
1679
1680 if (skspcl != NULL) {
1681 ulong flags;
1682
1683 sksgio->skspcl = NULL;
1684
1685 spin_lock_irqsave(&skdev->lock, flags);
1686 skd_release_special(skdev, skspcl);
1687 spin_unlock_irqrestore(&skdev->lock, flags);
1688 }
1689
1690 return 0;
1691}
1692
1693/*
1694 *****************************************************************************
1695 * INTERNAL REQUESTS -- generated by driver itself
1696 *****************************************************************************
1697 */
1698
1699static int skd_format_internal_skspcl(struct skd_device *skdev)
1700{
1701 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1702 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1703 struct fit_msg_hdr *fmh;
1704 uint64_t dma_address;
1705 struct skd_scsi_request *scsi;
1706
Bart Van Assched891fe62017-08-17 13:13:07 -07001707 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001708 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1709 fmh->num_protocol_cmds_coalesced = 1;
1710
Bart Van Assched891fe62017-08-17 13:13:07 -07001711 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001712 memset(scsi, 0, sizeof(*scsi));
1713 dma_address = skspcl->req.sksg_dma_address;
1714 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1715 sgd->control = FIT_SGD_CONTROL_LAST;
1716 sgd->byte_count = 0;
1717 sgd->host_side_addr = skspcl->db_dma_address;
1718 sgd->dev_side_addr = 0;
1719 sgd->next_desc_ptr = 0LL;
1720
1721 return 1;
1722}
1723
1724#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1725
1726static void skd_send_internal_skspcl(struct skd_device *skdev,
1727 struct skd_special_context *skspcl,
1728 u8 opcode)
1729{
1730 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1731 struct skd_scsi_request *scsi;
1732 unsigned char *buf = skspcl->data_buf;
1733 int i;
1734
1735 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1736 /*
1737 * A refresh is already in progress.
1738 * Just wait for it to finish.
1739 */
1740 return;
1741
1742 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1743 skspcl->req.state = SKD_REQ_STATE_BUSY;
1744 skspcl->req.id += SKD_ID_INCR;
1745
Bart Van Assched891fe62017-08-17 13:13:07 -07001746 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001747 scsi->hdr.tag = skspcl->req.id;
1748
1749 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1750
1751 switch (opcode) {
1752 case TEST_UNIT_READY:
1753 scsi->cdb[0] = TEST_UNIT_READY;
1754 sgd->byte_count = 0;
1755 scsi->hdr.sg_list_len_bytes = 0;
1756 break;
1757
1758 case READ_CAPACITY:
1759 scsi->cdb[0] = READ_CAPACITY;
1760 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1761 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1762 break;
1763
1764 case INQUIRY:
1765 scsi->cdb[0] = INQUIRY;
1766 scsi->cdb[1] = 0x01; /* evpd */
1767 scsi->cdb[2] = 0x80; /* serial number page */
1768 scsi->cdb[4] = 0x10;
1769 sgd->byte_count = 16;
1770 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1771 break;
1772
1773 case SYNCHRONIZE_CACHE:
1774 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1775 sgd->byte_count = 0;
1776 scsi->hdr.sg_list_len_bytes = 0;
1777 break;
1778
1779 case WRITE_BUFFER:
1780 scsi->cdb[0] = WRITE_BUFFER;
1781 scsi->cdb[1] = 0x02;
1782 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1783 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1784 sgd->byte_count = WR_BUF_SIZE;
1785 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1786 /* fill incrementing byte pattern */
1787 for (i = 0; i < sgd->byte_count; i++)
1788 buf[i] = i & 0xFF;
1789 break;
1790
1791 case READ_BUFFER:
1792 scsi->cdb[0] = READ_BUFFER;
1793 scsi->cdb[1] = 0x02;
1794 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1795 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1796 sgd->byte_count = WR_BUF_SIZE;
1797 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1798 memset(skspcl->data_buf, 0, sgd->byte_count);
1799 break;
1800
1801 default:
1802 SKD_ASSERT("Don't know what to send");
1803 return;
1804
1805 }
1806 skd_send_special_fitmsg(skdev, skspcl);
1807}
1808
1809static void skd_refresh_device_data(struct skd_device *skdev)
1810{
1811 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1812
1813 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1814}
1815
1816static int skd_chk_read_buf(struct skd_device *skdev,
1817 struct skd_special_context *skspcl)
1818{
1819 unsigned char *buf = skspcl->data_buf;
1820 int i;
1821
1822 /* check for incrementing byte pattern */
1823 for (i = 0; i < WR_BUF_SIZE; i++)
1824 if (buf[i] != (i & 0xFF))
1825 return 1;
1826
1827 return 0;
1828}
1829
1830static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1831 u8 code, u8 qual, u8 fruc)
1832{
1833 /* If the check condition is of special interest, log a message */
1834 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1835 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001836 dev_err(&skdev->pdev->dev,
1837 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1838 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001839 }
1840}
1841
1842static void skd_complete_internal(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07001843 struct fit_completion_entry_v1 *skcomp,
1844 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001845 struct skd_special_context *skspcl)
1846{
1847 u8 *buf = skspcl->data_buf;
1848 u8 status;
1849 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001850 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001851
Bart Van Assche760b48c2017-08-17 13:13:00 -07001852 lockdep_assert_held(&skdev->lock);
1853
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001854 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1855
Bart Van Asschef98806d2017-08-17 13:12:58 -07001856 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001857
1858 skspcl->req.completion = *skcomp;
1859 skspcl->req.state = SKD_REQ_STATE_IDLE;
1860 skspcl->req.id += SKD_ID_INCR;
1861
1862 status = skspcl->req.completion.status;
1863
1864 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1865 skerr->qual, skerr->fruc);
1866
1867 switch (scsi->cdb[0]) {
1868 case TEST_UNIT_READY:
1869 if (status == SAM_STAT_GOOD)
1870 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1871 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1872 (skerr->key == MEDIUM_ERROR))
1873 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1874 else {
1875 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001876 dev_dbg(&skdev->pdev->dev,
1877 "TUR failed, don't send anymore state 0x%x\n",
1878 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001879 return;
1880 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001881 dev_dbg(&skdev->pdev->dev,
1882 "**** TUR failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001883 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1884 }
1885 break;
1886
1887 case WRITE_BUFFER:
1888 if (status == SAM_STAT_GOOD)
1889 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1890 else {
1891 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001892 dev_dbg(&skdev->pdev->dev,
1893 "write buffer failed, don't send anymore state 0x%x\n",
1894 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001895 return;
1896 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001897 dev_dbg(&skdev->pdev->dev,
1898 "**** write buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001899 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1900 }
1901 break;
1902
1903 case READ_BUFFER:
1904 if (status == SAM_STAT_GOOD) {
1905 if (skd_chk_read_buf(skdev, skspcl) == 0)
1906 skd_send_internal_skspcl(skdev, skspcl,
1907 READ_CAPACITY);
1908 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001909 dev_err(&skdev->pdev->dev,
1910 "*** W/R Buffer mismatch %d ***\n",
1911 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001912 if (skdev->connect_retries <
1913 SKD_MAX_CONNECT_RETRIES) {
1914 skdev->connect_retries++;
1915 skd_soft_reset(skdev);
1916 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001917 dev_err(&skdev->pdev->dev,
1918 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001919 return;
1920 }
1921 }
1922
1923 } else {
1924 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001925 dev_dbg(&skdev->pdev->dev,
1926 "read buffer failed, don't send anymore state 0x%x\n",
1927 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001928 return;
1929 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001930 dev_dbg(&skdev->pdev->dev,
1931 "**** read buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001932 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1933 }
1934 break;
1935
1936 case READ_CAPACITY:
1937 skdev->read_cap_is_valid = 0;
1938 if (status == SAM_STAT_GOOD) {
1939 skdev->read_cap_last_lba =
1940 (buf[0] << 24) | (buf[1] << 16) |
1941 (buf[2] << 8) | buf[3];
1942 skdev->read_cap_blocksize =
1943 (buf[4] << 24) | (buf[5] << 16) |
1944 (buf[6] << 8) | buf[7];
1945
Bart Van Asschef98806d2017-08-17 13:12:58 -07001946 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1947 skdev->read_cap_last_lba,
1948 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001949
1950 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1951
1952 skdev->read_cap_is_valid = 1;
1953
1954 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1955 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1956 (skerr->key == MEDIUM_ERROR)) {
1957 skdev->read_cap_last_lba = ~0;
1958 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001959 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 -06001960 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1961 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001962 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001963 skd_send_internal_skspcl(skdev, skspcl,
1964 TEST_UNIT_READY);
1965 }
1966 break;
1967
1968 case INQUIRY:
1969 skdev->inquiry_is_valid = 0;
1970 if (status == SAM_STAT_GOOD) {
1971 skdev->inquiry_is_valid = 1;
1972
1973 for (i = 0; i < 12; i++)
1974 skdev->inq_serial_num[i] = buf[i + 4];
1975 skdev->inq_serial_num[12] = 0;
1976 }
1977
1978 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001979 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001980 /* connection is complete */
1981 skdev->connect_retries = 0;
1982 break;
1983
1984 case SYNCHRONIZE_CACHE:
1985 if (status == SAM_STAT_GOOD)
1986 skdev->sync_done = 1;
1987 else
1988 skdev->sync_done = -1;
1989 wake_up_interruptible(&skdev->waitq);
1990 break;
1991
1992 default:
1993 SKD_ASSERT("we didn't send this");
1994 }
1995}
1996
1997/*
1998 *****************************************************************************
1999 * FIT MESSAGES
2000 *****************************************************************************
2001 */
2002
2003static void skd_send_fitmsg(struct skd_device *skdev,
2004 struct skd_fitmsg_context *skmsg)
2005{
2006 u64 qcmd;
2007 struct fit_msg_hdr *fmh;
2008
Bart Van Asschef98806d2017-08-17 13:12:58 -07002009 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
2010 skmsg->mb_dma_address, skdev->in_flight);
Bart Van Assche6507f432017-08-17 13:13:06 -07002011 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002012
2013 qcmd = skmsg->mb_dma_address;
2014 qcmd |= FIT_QCMD_QID_NORMAL;
2015
Bart Van Assched891fe62017-08-17 13:13:07 -07002016 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002017 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2018
2019 if (unlikely(skdev->dbg_level > 1)) {
2020 u8 *bp = (u8 *)skmsg->msg_buf;
2021 int i;
2022 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002023 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
2024 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002025 if (i == 0)
2026 i = 64 - 8;
2027 }
2028 }
2029
2030 if (skmsg->length > 256)
2031 qcmd |= FIT_QCMD_MSGSIZE_512;
2032 else if (skmsg->length > 128)
2033 qcmd |= FIT_QCMD_MSGSIZE_256;
2034 else if (skmsg->length > 64)
2035 qcmd |= FIT_QCMD_MSGSIZE_128;
2036 else
2037 /*
2038 * This makes no sense because the FIT msg header is
2039 * 64 bytes. If the msg is only 64 bytes long it has
2040 * no payload.
2041 */
2042 qcmd |= FIT_QCMD_MSGSIZE_64;
2043
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002044 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2045 smp_wmb();
2046
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002047 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002048}
2049
2050static void skd_send_special_fitmsg(struct skd_device *skdev,
2051 struct skd_special_context *skspcl)
2052{
2053 u64 qcmd;
2054
2055 if (unlikely(skdev->dbg_level > 1)) {
2056 u8 *bp = (u8 *)skspcl->msg_buf;
2057 int i;
2058
2059 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002060 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
2061 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002062 if (i == 0)
2063 i = 64 - 8;
2064 }
2065
Bart Van Asschef98806d2017-08-17 13:12:58 -07002066 dev_dbg(&skdev->pdev->dev,
2067 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2068 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2069 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002070 for (i = 0; i < skspcl->req.n_sg; i++) {
2071 struct fit_sg_descriptor *sgd =
2072 &skspcl->req.sksg_list[i];
2073
Bart Van Asschef98806d2017-08-17 13:12:58 -07002074 dev_dbg(&skdev->pdev->dev,
2075 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
2076 i, sgd->byte_count, sgd->control,
2077 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002078 }
2079 }
2080
2081 /*
2082 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2083 * and one 64-byte SSDI command.
2084 */
2085 qcmd = skspcl->mb_dma_address;
2086 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2087
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002088 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2089 smp_wmb();
2090
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002091 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2092}
2093
2094/*
2095 *****************************************************************************
2096 * COMPLETION QUEUE
2097 *****************************************************************************
2098 */
2099
2100static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002101 struct fit_completion_entry_v1 *skcomp,
2102 struct fit_comp_error_info *skerr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002103
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002104struct sns_info {
2105 u8 type;
2106 u8 stat;
2107 u8 key;
2108 u8 asc;
2109 u8 ascq;
2110 u8 mask;
2111 enum skd_check_status_action action;
2112};
2113
2114static struct sns_info skd_chkstat_table[] = {
2115 /* Good */
2116 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2117 SKD_CHECK_STATUS_REPORT_GOOD },
2118
2119 /* Smart alerts */
2120 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2121 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2122 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2123 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2124 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2125 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2126
2127 /* Retry (with limits) */
2128 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2129 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2130 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2131 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2132 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2133 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2134 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2135 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2136
2137 /* Busy (or about to be) */
2138 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2139 SKD_CHECK_STATUS_BUSY_IMMINENT },
2140};
2141
2142/*
2143 * Look up status and sense data to decide how to handle the error
2144 * from the device.
2145 * mask says which fields must match e.g., mask=0x18 means check
2146 * type and stat, ignore key, asc, ascq.
2147 */
2148
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002149static enum skd_check_status_action
2150skd_check_status(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002151 u8 cmp_status, struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002152{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07002153 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002154
Bart Van Asschef98806d2017-08-17 13:12:58 -07002155 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2156 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002157
Bart Van Asschef98806d2017-08-17 13:12:58 -07002158 dev_dbg(&skdev->pdev->dev,
2159 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2160 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
2161 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002162
2163 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07002164 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002165 struct sns_info *sns = &skd_chkstat_table[i];
2166
2167 if (sns->mask & 0x10)
2168 if (skerr->type != sns->type)
2169 continue;
2170
2171 if (sns->mask & 0x08)
2172 if (cmp_status != sns->stat)
2173 continue;
2174
2175 if (sns->mask & 0x04)
2176 if (skerr->key != sns->key)
2177 continue;
2178
2179 if (sns->mask & 0x02)
2180 if (skerr->code != sns->asc)
2181 continue;
2182
2183 if (sns->mask & 0x01)
2184 if (skerr->qual != sns->ascq)
2185 continue;
2186
2187 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002188 dev_err(&skdev->pdev->dev,
2189 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
2190 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002191 }
2192 return sns->action;
2193 }
2194
2195 /* No other match, so nonzero status means error,
2196 * zero status means good
2197 */
2198 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002199 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002200 return SKD_CHECK_STATUS_REPORT_ERROR;
2201 }
2202
Bart Van Asschef98806d2017-08-17 13:12:58 -07002203 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002204 return SKD_CHECK_STATUS_REPORT_GOOD;
2205}
2206
2207static void skd_resolve_req_exception(struct skd_device *skdev,
2208 struct skd_request_context *skreq)
2209{
2210 u8 cmp_status = skreq->completion.status;
2211
2212 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2213 case SKD_CHECK_STATUS_REPORT_GOOD:
2214 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002215 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002216 break;
2217
2218 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2219 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002220 blk_requeue_request(skdev->queue, skreq->req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002221 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002222 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2223 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2224 skd_quiesce_dev(skdev);
2225 break;
2226
2227 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002228 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2229 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002230 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002231 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002232 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07002233 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002234
2235 case SKD_CHECK_STATUS_REPORT_ERROR:
2236 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002237 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002238 break;
2239 }
2240}
2241
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002242/* assume spinlock is already held */
2243static void skd_release_skreq(struct skd_device *skdev,
2244 struct skd_request_context *skreq)
2245{
2246 u32 msg_slot;
2247 struct skd_fitmsg_context *skmsg;
2248
2249 u32 timo_slot;
2250
2251 /*
2252 * Reclaim the FIT msg buffer if this is
2253 * the first of the requests it carried to
2254 * be completed. The FIT msg buffer used to
2255 * send this request cannot be reused until
2256 * we are sure the s1120 card has copied
2257 * it to its memory. The FIT msg might have
2258 * contained several requests. As soon as
2259 * any of them are completed we know that
2260 * the entire FIT msg was transferred.
2261 * Only the first completed request will
2262 * match the FIT msg buffer id. The FIT
2263 * msg buffer id is immediately updated.
2264 * When subsequent requests complete the FIT
2265 * msg buffer id won't match, so we know
2266 * quite cheaply that it is already done.
2267 */
2268 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2269 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2270
2271 skmsg = &skdev->skmsg_table[msg_slot];
2272 if (skmsg->id == skreq->fitmsg_id) {
2273 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2274 SKD_ASSERT(skmsg->outstanding > 0);
2275 skmsg->outstanding--;
2276 if (skmsg->outstanding == 0) {
2277 skmsg->state = SKD_MSG_STATE_IDLE;
2278 skmsg->id += SKD_ID_INCR;
2279 skmsg->next = skdev->skmsg_free_list;
2280 skdev->skmsg_free_list = skmsg;
2281 }
2282 }
2283
2284 /*
2285 * Decrease the number of active requests.
2286 * Also decrements the count in the timeout slot.
2287 */
2288 SKD_ASSERT(skdev->in_flight > 0);
2289 skdev->in_flight -= 1;
2290
2291 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2292 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2293 skdev->timeout_slot[timo_slot] -= 1;
2294
2295 /*
2296 * Reset backpointer
2297 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002298 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002299
2300 /*
2301 * Reclaim the skd_request_context
2302 */
2303 skreq->state = SKD_REQ_STATE_IDLE;
2304 skreq->id += SKD_ID_INCR;
2305 skreq->next = skdev->skreq_free_list;
2306 skdev->skreq_free_list = skreq;
2307}
2308
2309#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2310
2311static void skd_do_inq_page_00(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002312 struct fit_completion_entry_v1 *skcomp,
2313 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002314 uint8_t *cdb, uint8_t *buf)
2315{
2316 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2317
2318 /* Caller requested "supported pages". The driver needs to insert
2319 * its page.
2320 */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002321 dev_dbg(&skdev->pdev->dev,
2322 "skd_do_driver_inquiry: modify supported pages.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002323
2324 /* If the device rejected the request because the CDB was
2325 * improperly formed, then just leave.
2326 */
2327 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2328 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2329 return;
2330
2331 /* Get the amount of space the caller allocated */
2332 max_bytes = (cdb[3] << 8) | cdb[4];
2333
2334 /* Get the number of pages actually returned by the device */
2335 drive_pages = (buf[2] << 8) | buf[3];
2336 drive_bytes = drive_pages + 4;
2337 new_size = drive_pages + 1;
2338
2339 /* Supported pages must be in numerical order, so find where
2340 * the driver page needs to be inserted into the list of
2341 * pages returned by the device.
2342 */
2343 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2344 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2345 return; /* Device using this page code. abort */
2346 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2347 break;
2348 }
2349
2350 if (insert_pt < max_bytes) {
2351 uint16_t u;
2352
2353 /* Shift everything up one byte to make room. */
2354 for (u = new_size + 3; u > insert_pt; u--)
2355 buf[u] = buf[u - 1];
2356 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2357
2358 /* SCSI byte order increment of num_returned_bytes by 1 */
2359 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002360 cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002361 }
2362
2363 /* update page length field to reflect the driver's page too */
2364 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2365 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2366}
2367
2368static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2369{
2370 int pcie_reg;
2371 u16 pci_bus_speed;
2372 u8 pci_lanes;
2373
2374 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2375 if (pcie_reg) {
2376 u16 linksta;
2377 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2378
2379 pci_bus_speed = linksta & 0xF;
2380 pci_lanes = (linksta & 0x3F0) >> 4;
2381 } else {
2382 *speed = STEC_LINK_UNKNOWN;
2383 *width = 0xFF;
2384 return;
2385 }
2386
2387 switch (pci_bus_speed) {
2388 case 1:
2389 *speed = STEC_LINK_2_5GTS;
2390 break;
2391 case 2:
2392 *speed = STEC_LINK_5GTS;
2393 break;
2394 case 3:
2395 *speed = STEC_LINK_8GTS;
2396 break;
2397 default:
2398 *speed = STEC_LINK_UNKNOWN;
2399 break;
2400 }
2401
2402 if (pci_lanes <= 0x20)
2403 *width = pci_lanes;
2404 else
2405 *width = 0xFF;
2406}
2407
2408static void skd_do_inq_page_da(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002409 struct fit_completion_entry_v1 *skcomp,
2410 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002411 uint8_t *cdb, uint8_t *buf)
2412{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002413 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002414 unsigned max_bytes;
2415 struct driver_inquiry_data inq;
2416 u16 val;
2417
Bart Van Asschef98806d2017-08-17 13:12:58 -07002418 dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002419
2420 memset(&inq, 0, sizeof(inq));
2421
2422 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2423
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002424 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2425 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2426 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2427 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002428
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002429 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2430 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002431
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002432 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2433 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002434
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002435 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2436 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002437
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002438 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2439 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002440
2441 /* Driver version, fixed lenth, padded with spaces on the right */
2442 inq.driver_version_length = sizeof(inq.driver_version);
2443 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2444 memcpy(inq.driver_version, DRV_VER_COMPL,
2445 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2446
2447 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2448
2449 /* Clear the error set by the device */
2450 skcomp->status = SAM_STAT_GOOD;
2451 memset((void *)skerr, 0, sizeof(*skerr));
2452
2453 /* copy response into output buffer */
2454 max_bytes = (cdb[3] << 8) | cdb[4];
2455 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2456
2457 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002458 cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq)));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002459}
2460
2461static void skd_do_driver_inq(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002462 struct fit_completion_entry_v1 *skcomp,
2463 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002464 uint8_t *cdb, uint8_t *buf)
2465{
2466 if (!buf)
2467 return;
2468 else if (cdb[0] != INQUIRY)
2469 return; /* Not an INQUIRY */
2470 else if ((cdb[1] & 1) == 0)
2471 return; /* EVPD not set */
2472 else if (cdb[2] == 0)
2473 /* Need to add driver's page to supported pages list */
2474 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2475 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2476 /* Caller requested driver's page */
2477 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2478}
2479
2480static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2481{
2482 if (!sg)
2483 return NULL;
2484 if (!sg_page(sg))
2485 return NULL;
2486 return sg_virt(sg);
2487}
2488
2489static void skd_process_scsi_inq(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002490 struct fit_completion_entry_v1 *skcomp,
2491 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002492 struct skd_special_context *skspcl)
2493{
2494 uint8_t *buf;
Bart Van Assched891fe62017-08-17 13:13:07 -07002495 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002496
2497 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07002498 skspcl->req.data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002499 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2500
2501 if (buf)
2502 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2503}
2504
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002505static int skd_isr_completion_posted(struct skd_device *skdev,
2506 int limit, int *enqueued)
2507{
Bart Van Assche85e34112017-08-17 13:13:17 -07002508 struct fit_completion_entry_v1 *skcmp;
2509 struct fit_comp_error_info *skerr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002510 u16 req_id;
2511 u32 req_slot;
2512 struct skd_request_context *skreq;
Bart Van Asschec830da82017-08-17 13:13:13 -07002513 u16 cmp_cntxt;
2514 u8 cmp_status;
2515 u8 cmp_cycle;
2516 u32 cmp_bytes;
2517 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002518 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002519
Bart Van Assche760b48c2017-08-17 13:13:00 -07002520 lockdep_assert_held(&skdev->lock);
2521
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002522 for (;; ) {
2523 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2524
2525 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2526 cmp_cycle = skcmp->cycle;
2527 cmp_cntxt = skcmp->tag;
2528 cmp_status = skcmp->status;
2529 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2530
2531 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2532
Bart Van Asschef98806d2017-08-17 13:12:58 -07002533 dev_dbg(&skdev->pdev->dev,
2534 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
2535 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
2536 cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
2537 skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002538
2539 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002540 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002541 break;
2542 }
2543 /*
2544 * Update the completion queue head index and possibly
2545 * the completion cycle count. 8-bit wrap-around.
2546 */
2547 skdev->skcomp_ix++;
2548 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2549 skdev->skcomp_ix = 0;
2550 skdev->skcomp_cycle++;
2551 }
2552
2553 /*
2554 * The command context is a unique 32-bit ID. The low order
2555 * bits help locate the request. The request is usually a
2556 * r/w request (see skd_start() above) or a special request.
2557 */
2558 req_id = cmp_cntxt;
2559 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2560
2561 /* Is this other than a r/w request? */
2562 if (req_slot >= skdev->num_req_context) {
2563 /*
2564 * This is not a completion for a r/w request.
2565 */
2566 skd_complete_other(skdev, skcmp, skerr);
2567 continue;
2568 }
2569
2570 skreq = &skdev->skreq_table[req_slot];
2571
2572 /*
2573 * Make sure the request ID for the slot matches.
2574 */
2575 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002576 dev_dbg(&skdev->pdev->dev,
2577 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
2578 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002579 {
2580 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002581 dev_err(&skdev->pdev->dev,
2582 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2583 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002584
2585 continue;
2586 }
2587 }
2588
2589 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2590
2591 if (skreq->state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002592 dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n",
2593 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002594 /* a previously timed out command can
2595 * now be cleaned up */
2596 skd_release_skreq(skdev, skreq);
2597 continue;
2598 }
2599
2600 skreq->completion = *skcmp;
2601 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2602 skreq->err_info = *skerr;
2603 skd_log_check_status(skdev, cmp_status, skerr->key,
2604 skerr->code, skerr->qual,
2605 skerr->fruc);
2606 }
2607 /* Release DMA resources for the request. */
2608 if (skreq->n_sg > 0)
2609 skd_postop_sg_list(skdev, skreq);
2610
Jens Axboefcd37eb2013-11-01 10:14:56 -06002611 if (!skreq->req) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002612 dev_dbg(&skdev->pdev->dev,
2613 "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
2614 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002615 } else {
2616 /*
2617 * Capture the outcome and post it back to the
2618 * native request.
2619 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002620 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002621 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002622 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002623 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002624 }
2625
2626 /*
2627 * Release the skreq, its FIT msg (if one), timeout slot,
2628 * and queue depth.
2629 */
2630 skd_release_skreq(skdev, skreq);
2631
2632 /* skd_isr_comp_limit equal zero means no limit */
2633 if (limit) {
2634 if (++processed >= limit) {
2635 rc = 1;
2636 break;
2637 }
2638 }
2639 }
2640
2641 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2642 && (skdev->in_flight) == 0) {
2643 skdev->state = SKD_DRVR_STATE_PAUSED;
2644 wake_up_interruptible(&skdev->waitq);
2645 }
2646
2647 return rc;
2648}
2649
2650static void skd_complete_other(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002651 struct fit_completion_entry_v1 *skcomp,
2652 struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002653{
2654 u32 req_id = 0;
2655 u32 req_table;
2656 u32 req_slot;
2657 struct skd_special_context *skspcl;
2658
Bart Van Assche760b48c2017-08-17 13:13:00 -07002659 lockdep_assert_held(&skdev->lock);
2660
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002661 req_id = skcomp->tag;
2662 req_table = req_id & SKD_ID_TABLE_MASK;
2663 req_slot = req_id & SKD_ID_SLOT_MASK;
2664
Bart Van Asschef98806d2017-08-17 13:12:58 -07002665 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
2666 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002667
2668 /*
2669 * Based on the request id, determine how to dispatch this completion.
2670 * This swich/case is finding the good cases and forwarding the
2671 * completion entry. Errors are reported below the switch.
2672 */
2673 switch (req_table) {
2674 case SKD_ID_RW_REQUEST:
2675 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07002676 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002677 * handles r/w requests. The only way we get here
2678 * is if the req_slot is out of bounds.
2679 */
2680 break;
2681
2682 case SKD_ID_SPECIAL_REQUEST:
2683 /*
2684 * Make sure the req_slot is in bounds and that the id
2685 * matches.
2686 */
2687 if (req_slot < skdev->n_special) {
2688 skspcl = &skdev->skspcl_table[req_slot];
2689 if (skspcl->req.id == req_id &&
2690 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2691 skd_complete_special(skdev,
2692 skcomp, skerr, skspcl);
2693 return;
2694 }
2695 }
2696 break;
2697
2698 case SKD_ID_INTERNAL:
2699 if (req_slot == 0) {
2700 skspcl = &skdev->internal_skspcl;
2701 if (skspcl->req.id == req_id &&
2702 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2703 skd_complete_internal(skdev,
2704 skcomp, skerr, skspcl);
2705 return;
2706 }
2707 }
2708 break;
2709
2710 case SKD_ID_FIT_MSG:
2711 /*
2712 * These id's should never appear in a completion record.
2713 */
2714 break;
2715
2716 default:
2717 /*
2718 * These id's should never appear anywhere;
2719 */
2720 break;
2721 }
2722
2723 /*
2724 * If we get here it is a bad or stale id.
2725 */
2726}
2727
2728static void skd_complete_special(struct skd_device *skdev,
Bart Van Assche85e34112017-08-17 13:13:17 -07002729 struct fit_completion_entry_v1 *skcomp,
2730 struct fit_comp_error_info *skerr,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002731 struct skd_special_context *skspcl)
2732{
Bart Van Assche760b48c2017-08-17 13:13:00 -07002733 lockdep_assert_held(&skdev->lock);
2734
Bart Van Asschef98806d2017-08-17 13:12:58 -07002735 dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002736 if (skspcl->orphaned) {
2737 /* Discard orphaned request */
2738 /* ?: Can this release directly or does it need
2739 * to use a worker? */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002740 dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002741 skd_release_special(skdev, skspcl);
2742 return;
2743 }
2744
2745 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2746
2747 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2748 skspcl->req.completion = *skcomp;
2749 skspcl->req.err_info = *skerr;
2750
2751 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2752 skerr->code, skerr->qual, skerr->fruc);
2753
2754 wake_up_interruptible(&skdev->waitq);
2755}
2756
2757/* assume spinlock is already held */
2758static void skd_release_special(struct skd_device *skdev,
2759 struct skd_special_context *skspcl)
2760{
2761 int i, was_depleted;
2762
2763 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002764 struct page *page = sg_page(&skspcl->req.sg[i]);
2765 __free_page(page);
2766 }
2767
2768 was_depleted = (skdev->skspcl_free_list == NULL);
2769
2770 skspcl->req.state = SKD_REQ_STATE_IDLE;
2771 skspcl->req.id += SKD_ID_INCR;
2772 skspcl->req.next =
2773 (struct skd_request_context *)skdev->skspcl_free_list;
2774 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2775
2776 if (was_depleted) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002777 dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002778 /* Free list was depleted. Their might be waiters. */
2779 wake_up_interruptible(&skdev->waitq);
2780 }
2781}
2782
2783static void skd_reset_skcomp(struct skd_device *skdev)
2784{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002785 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002786
2787 skdev->skcomp_ix = 0;
2788 skdev->skcomp_cycle = 1;
2789}
2790
2791/*
2792 *****************************************************************************
2793 * INTERRUPTS
2794 *****************************************************************************
2795 */
2796static void skd_completion_worker(struct work_struct *work)
2797{
2798 struct skd_device *skdev =
2799 container_of(work, struct skd_device, completion_worker);
2800 unsigned long flags;
2801 int flush_enqueued = 0;
2802
2803 spin_lock_irqsave(&skdev->lock, flags);
2804
2805 /*
2806 * pass in limit=0, which means no limit..
2807 * process everything in compq
2808 */
2809 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2810 skd_request_fn(skdev->queue);
2811
2812 spin_unlock_irqrestore(&skdev->lock, flags);
2813}
2814
2815static void skd_isr_msg_from_dev(struct skd_device *skdev);
2816
Arnd Bergmann41c94992016-11-09 13:55:35 +01002817static irqreturn_t
2818skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002819{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07002820 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002821 u32 intstat;
2822 u32 ack;
2823 int rc = 0;
2824 int deferred = 0;
2825 int flush_enqueued = 0;
2826
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002827 spin_lock(&skdev->lock);
2828
2829 for (;; ) {
2830 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2831
2832 ack = FIT_INT_DEF_MASK;
2833 ack &= intstat;
2834
Bart Van Asschef98806d2017-08-17 13:12:58 -07002835 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
2836 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002837
2838 /* As long as there is an int pending on device, keep
2839 * running loop. When none, get out, but if we've never
2840 * done any processing, call completion handler?
2841 */
2842 if (ack == 0) {
2843 /* No interrupts on device, but run the completion
2844 * processor anyway?
2845 */
2846 if (rc == 0)
2847 if (likely (skdev->state
2848 == SKD_DRVR_STATE_ONLINE))
2849 deferred = 1;
2850 break;
2851 }
2852
2853 rc = IRQ_HANDLED;
2854
2855 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2856
2857 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2858 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2859 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2860 /*
2861 * If we have already deferred completion
2862 * processing, don't bother running it again
2863 */
2864 if (deferred == 0)
2865 deferred =
2866 skd_isr_completion_posted(skdev,
2867 skd_isr_comp_limit, &flush_enqueued);
2868 }
2869
2870 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2871 skd_isr_fwstate(skdev);
2872 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2873 skdev->state ==
2874 SKD_DRVR_STATE_DISAPPEARED) {
2875 spin_unlock(&skdev->lock);
2876 return rc;
2877 }
2878 }
2879
2880 if (intstat & FIT_ISH_MSG_FROM_DEV)
2881 skd_isr_msg_from_dev(skdev);
2882 }
2883 }
2884
2885 if (unlikely(flush_enqueued))
2886 skd_request_fn(skdev->queue);
2887
2888 if (deferred)
2889 schedule_work(&skdev->completion_worker);
2890 else if (!flush_enqueued)
2891 skd_request_fn(skdev->queue);
2892
2893 spin_unlock(&skdev->lock);
2894
2895 return rc;
2896}
2897
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002898static void skd_drive_fault(struct skd_device *skdev)
2899{
2900 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002901 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002902}
2903
2904static void skd_drive_disappeared(struct skd_device *skdev)
2905{
2906 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002907 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002908}
2909
2910static void skd_isr_fwstate(struct skd_device *skdev)
2911{
2912 u32 sense;
2913 u32 state;
2914 u32 mtd;
2915 int prev_driver_state = skdev->state;
2916
2917 sense = SKD_READL(skdev, FIT_STATUS);
2918 state = sense & FIT_SR_DRIVE_STATE_MASK;
2919
Bart Van Asschef98806d2017-08-17 13:12:58 -07002920 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
2921 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
2922 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002923
2924 skdev->drive_state = state;
2925
2926 switch (skdev->drive_state) {
2927 case FIT_SR_DRIVE_INIT:
2928 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
2929 skd_disable_interrupts(skdev);
2930 break;
2931 }
2932 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002933 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002934 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2935 skdev->timer_countdown = SKD_STARTING_TIMO;
2936 skdev->state = SKD_DRVR_STATE_STARTING;
2937 skd_soft_reset(skdev);
2938 break;
2939 }
2940 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2941 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2942 skdev->last_mtd = mtd;
2943 break;
2944
2945 case FIT_SR_DRIVE_ONLINE:
2946 skdev->cur_max_queue_depth = skd_max_queue_depth;
2947 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2948 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2949
2950 skdev->queue_low_water_mark =
2951 skdev->cur_max_queue_depth * 2 / 3 + 1;
2952 if (skdev->queue_low_water_mark < 1)
2953 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002954 dev_info(&skdev->pdev->dev,
2955 "Queue depth limit=%d dev=%d lowat=%d\n",
2956 skdev->cur_max_queue_depth,
2957 skdev->dev_max_queue_depth,
2958 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002959
2960 skd_refresh_device_data(skdev);
2961 break;
2962
2963 case FIT_SR_DRIVE_BUSY:
2964 skdev->state = SKD_DRVR_STATE_BUSY;
2965 skdev->timer_countdown = SKD_BUSY_TIMO;
2966 skd_quiesce_dev(skdev);
2967 break;
2968 case FIT_SR_DRIVE_BUSY_SANITIZE:
2969 /* set timer for 3 seconds, we'll abort any unfinished
2970 * commands after that expires
2971 */
2972 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2973 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002974 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002975 break;
2976 case FIT_SR_DRIVE_BUSY_ERASE:
2977 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2978 skdev->timer_countdown = SKD_BUSY_TIMO;
2979 break;
2980 case FIT_SR_DRIVE_OFFLINE:
2981 skdev->state = SKD_DRVR_STATE_IDLE;
2982 break;
2983 case FIT_SR_DRIVE_SOFT_RESET:
2984 switch (skdev->state) {
2985 case SKD_DRVR_STATE_STARTING:
2986 case SKD_DRVR_STATE_RESTARTING:
2987 /* Expected by a caller of skd_soft_reset() */
2988 break;
2989 default:
2990 skdev->state = SKD_DRVR_STATE_RESTARTING;
2991 break;
2992 }
2993 break;
2994 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07002995 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002996 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2997 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2998 break;
2999
3000 case FIT_SR_DRIVE_DEGRADED:
3001 case FIT_SR_PCIE_LINK_DOWN:
3002 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3003 break;
3004
3005 case FIT_SR_DRIVE_FAULT:
3006 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003007 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003008 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003009 break;
3010
3011 /* PCIe bus returned all Fs? */
3012 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003013 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
3014 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003015 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003016 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003017 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003018 break;
3019 default:
3020 /*
3021 * Uknown FW State. Wait for a state we recognize.
3022 */
3023 break;
3024 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003025 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3026 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3027 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003028}
3029
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003030static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003031{
3032 int i;
3033
3034 for (i = 0; i < skdev->num_req_context; i++) {
3035 struct skd_request_context *skreq = &skdev->skreq_table[i];
3036
3037 if (skreq->state == SKD_REQ_STATE_BUSY) {
3038 skd_log_skreq(skdev, skreq, "recover");
3039
3040 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003041 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003042
3043 /* Release DMA resources for the request. */
3044 if (skreq->n_sg > 0)
3045 skd_postop_sg_list(skdev, skreq);
3046
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003047 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003048
Jens Axboefcd37eb2013-11-01 10:14:56 -06003049 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003050
3051 skreq->state = SKD_REQ_STATE_IDLE;
3052 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003053 }
3054 if (i > 0)
3055 skreq[-1].next = skreq;
3056 skreq->next = NULL;
3057 }
3058 skdev->skreq_free_list = skdev->skreq_table;
3059
3060 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3061 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3062
3063 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3064 skd_log_skmsg(skdev, skmsg, "salvaged");
3065 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3066 skmsg->state = SKD_MSG_STATE_IDLE;
3067 skmsg->id += SKD_ID_INCR;
3068 }
3069 if (i > 0)
3070 skmsg[-1].next = skmsg;
3071 skmsg->next = NULL;
3072 }
3073 skdev->skmsg_free_list = skdev->skmsg_table;
3074
3075 for (i = 0; i < skdev->n_special; i++) {
3076 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3077
3078 /* If orphaned, reclaim it because it has already been reported
3079 * to the process as an error (it was just waiting for
3080 * a completion that didn't come, and now it will never come)
3081 * If busy, change to a state that will cause it to error
3082 * out in the wait routine and let it do the normal
3083 * reporting and reclaiming
3084 */
3085 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3086 if (skspcl->orphaned) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003087 dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
3088 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003089 skd_release_special(skdev, skspcl);
3090 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003091 dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
3092 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003093 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3094 }
3095 }
3096 }
3097 skdev->skspcl_free_list = skdev->skspcl_table;
3098
3099 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3100 skdev->timeout_slot[i] = 0;
3101
3102 skdev->in_flight = 0;
3103}
3104
3105static void skd_isr_msg_from_dev(struct skd_device *skdev)
3106{
3107 u32 mfd;
3108 u32 mtd;
3109 u32 data;
3110
3111 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3112
Bart Van Asschef98806d2017-08-17 13:12:58 -07003113 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
3114 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003115
3116 /* ignore any mtd that is an ack for something we didn't send */
3117 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3118 return;
3119
3120 switch (FIT_MXD_TYPE(mfd)) {
3121 case FIT_MTD_FITFW_INIT:
3122 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3123
3124 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003125 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
3126 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
3127 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
3128 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003129 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3130 skd_soft_reset(skdev);
3131 break;
3132 }
3133 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3134 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3135 skdev->last_mtd = mtd;
3136 break;
3137
3138 case FIT_MTD_GET_CMDQ_DEPTH:
3139 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3140 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3141 SKD_N_COMPLETION_ENTRY);
3142 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3143 skdev->last_mtd = mtd;
3144 break;
3145
3146 case FIT_MTD_SET_COMPQ_DEPTH:
3147 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3148 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3149 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3150 skdev->last_mtd = mtd;
3151 break;
3152
3153 case FIT_MTD_SET_COMPQ_ADDR:
3154 skd_reset_skcomp(skdev);
3155 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3156 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3157 skdev->last_mtd = mtd;
3158 break;
3159
3160 case FIT_MTD_CMD_LOG_HOST_ID:
3161 skdev->connect_time_stamp = get_seconds();
3162 data = skdev->connect_time_stamp & 0xFFFF;
3163 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3164 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3165 skdev->last_mtd = mtd;
3166 break;
3167
3168 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3169 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3170 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3171 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3172 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3173 skdev->last_mtd = mtd;
3174 break;
3175
3176 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3177 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3178 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3179 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3180 skdev->last_mtd = mtd;
3181
Bart Van Asschef98806d2017-08-17 13:12:58 -07003182 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
3183 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003184 break;
3185
3186 case FIT_MTD_ARM_QUEUE:
3187 skdev->last_mtd = 0;
3188 /*
3189 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3190 */
3191 break;
3192
3193 default:
3194 break;
3195 }
3196}
3197
3198static void skd_disable_interrupts(struct skd_device *skdev)
3199{
3200 u32 sense;
3201
3202 sense = SKD_READL(skdev, FIT_CONTROL);
3203 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3204 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003205 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003206
3207 /* Note that the 1s is written. A 1-bit means
3208 * disable, a 0 means enable.
3209 */
3210 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3211}
3212
3213static void skd_enable_interrupts(struct skd_device *skdev)
3214{
3215 u32 val;
3216
3217 /* unmask interrupts first */
3218 val = FIT_ISH_FW_STATE_CHANGE +
3219 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3220
3221 /* Note that the compliment of mask is written. A 1-bit means
3222 * disable, a 0 means enable. */
3223 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003224 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003225
3226 val = SKD_READL(skdev, FIT_CONTROL);
3227 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003228 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003229 SKD_WRITEL(skdev, val, FIT_CONTROL);
3230}
3231
3232/*
3233 *****************************************************************************
3234 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3235 *****************************************************************************
3236 */
3237
3238static void skd_soft_reset(struct skd_device *skdev)
3239{
3240 u32 val;
3241
3242 val = SKD_READL(skdev, FIT_CONTROL);
3243 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003244 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003245 SKD_WRITEL(skdev, val, FIT_CONTROL);
3246}
3247
3248static void skd_start_device(struct skd_device *skdev)
3249{
3250 unsigned long flags;
3251 u32 sense;
3252 u32 state;
3253
3254 spin_lock_irqsave(&skdev->lock, flags);
3255
3256 /* ack all ghost interrupts */
3257 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3258
3259 sense = SKD_READL(skdev, FIT_STATUS);
3260
Bart Van Asschef98806d2017-08-17 13:12:58 -07003261 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003262
3263 state = sense & FIT_SR_DRIVE_STATE_MASK;
3264 skdev->drive_state = state;
3265 skdev->last_mtd = 0;
3266
3267 skdev->state = SKD_DRVR_STATE_STARTING;
3268 skdev->timer_countdown = SKD_STARTING_TIMO;
3269
3270 skd_enable_interrupts(skdev);
3271
3272 switch (skdev->drive_state) {
3273 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003274 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003275 break;
3276
3277 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003278 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003279 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3280 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3281 break;
3282
3283 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003284 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003285 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3286 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3287 break;
3288
3289 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003290 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003291 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3292 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3293 break;
3294
3295 case FIT_SR_DRIVE_INIT:
3296 case FIT_SR_DRIVE_ONLINE:
3297 skd_soft_reset(skdev);
3298 break;
3299
3300 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003301 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003302 skdev->state = SKD_DRVR_STATE_BUSY;
3303 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3304 break;
3305
3306 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003307 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003308 break;
3309
3310 case FIT_SR_DRIVE_FAULT:
3311 /* Fault state is bad...soft reset won't do it...
3312 * Hard reset, maybe, but does it work on device?
3313 * For now, just fault so the system doesn't hang.
3314 */
3315 skd_drive_fault(skdev);
3316 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003317 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003318 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003319 skdev->gendisk_on = -1;
3320 wake_up_interruptible(&skdev->waitq);
3321 break;
3322
3323 case 0xFF:
3324 /* Most likely the device isn't there or isn't responding
3325 * to the BAR1 addresses. */
3326 skd_drive_disappeared(skdev);
3327 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003328 dev_dbg(&skdev->pdev->dev,
3329 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003330 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003331 skdev->gendisk_on = -1;
3332 wake_up_interruptible(&skdev->waitq);
3333 break;
3334
3335 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003336 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
3337 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003338 break;
3339 }
3340
3341 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003342 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003343
3344 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003345 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003346
3347 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003348 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003349
3350 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003351 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003352
3353 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003354 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003355
3356 spin_unlock_irqrestore(&skdev->lock, flags);
3357}
3358
3359static void skd_stop_device(struct skd_device *skdev)
3360{
3361 unsigned long flags;
3362 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3363 u32 dev_state;
3364 int i;
3365
3366 spin_lock_irqsave(&skdev->lock, flags);
3367
3368 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003369 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003370 goto stop_out;
3371 }
3372
3373 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003374 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003375 goto stop_out;
3376 }
3377
3378 skdev->state = SKD_DRVR_STATE_SYNCING;
3379 skdev->sync_done = 0;
3380
3381 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3382
3383 spin_unlock_irqrestore(&skdev->lock, flags);
3384
3385 wait_event_interruptible_timeout(skdev->waitq,
3386 (skdev->sync_done), (10 * HZ));
3387
3388 spin_lock_irqsave(&skdev->lock, flags);
3389
3390 switch (skdev->sync_done) {
3391 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003392 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003393 break;
3394 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003395 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003396 break;
3397 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003398 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003399 }
3400
3401stop_out:
3402 skdev->state = SKD_DRVR_STATE_STOPPING;
3403 spin_unlock_irqrestore(&skdev->lock, flags);
3404
3405 skd_kill_timer(skdev);
3406
3407 spin_lock_irqsave(&skdev->lock, flags);
3408 skd_disable_interrupts(skdev);
3409
3410 /* ensure all ints on device are cleared */
3411 /* soft reset the device to unload with a clean slate */
3412 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3413 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3414
3415 spin_unlock_irqrestore(&skdev->lock, flags);
3416
3417 /* poll every 100ms, 1 second timeout */
3418 for (i = 0; i < 10; i++) {
3419 dev_state =
3420 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3421 if (dev_state == FIT_SR_DRIVE_INIT)
3422 break;
3423 set_current_state(TASK_INTERRUPTIBLE);
3424 schedule_timeout(msecs_to_jiffies(100));
3425 }
3426
3427 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07003428 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
3429 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003430}
3431
3432/* assume spinlock is held */
3433static void skd_restart_device(struct skd_device *skdev)
3434{
3435 u32 state;
3436
3437 /* ack all ghost interrupts */
3438 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3439
3440 state = SKD_READL(skdev, FIT_STATUS);
3441
Bart Van Asschef98806d2017-08-17 13:12:58 -07003442 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003443
3444 state &= FIT_SR_DRIVE_STATE_MASK;
3445 skdev->drive_state = state;
3446 skdev->last_mtd = 0;
3447
3448 skdev->state = SKD_DRVR_STATE_RESTARTING;
3449 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3450
3451 skd_soft_reset(skdev);
3452}
3453
3454/* assume spinlock is held */
3455static int skd_quiesce_dev(struct skd_device *skdev)
3456{
3457 int rc = 0;
3458
3459 switch (skdev->state) {
3460 case SKD_DRVR_STATE_BUSY:
3461 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003462 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003463 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003464 break;
3465 case SKD_DRVR_STATE_ONLINE:
3466 case SKD_DRVR_STATE_STOPPING:
3467 case SKD_DRVR_STATE_SYNCING:
3468 case SKD_DRVR_STATE_PAUSING:
3469 case SKD_DRVR_STATE_PAUSED:
3470 case SKD_DRVR_STATE_STARTING:
3471 case SKD_DRVR_STATE_RESTARTING:
3472 case SKD_DRVR_STATE_RESUMING:
3473 default:
3474 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003475 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
3476 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003477 }
3478 return rc;
3479}
3480
3481/* assume spinlock is held */
3482static int skd_unquiesce_dev(struct skd_device *skdev)
3483{
3484 int prev_driver_state = skdev->state;
3485
3486 skd_log_skdev(skdev, "unquiesce");
3487 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003488 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003489 return 0;
3490 }
3491 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3492 /*
3493 * If there has been an state change to other than
3494 * ONLINE, we will rely on controller state change
3495 * to come back online and restart the queue.
3496 * The BUSY state means that driver is ready to
3497 * continue normal processing but waiting for controller
3498 * to become available.
3499 */
3500 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003501 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003502 return 0;
3503 }
3504
3505 /*
3506 * Drive has just come online, driver is either in startup,
3507 * paused performing a task, or bust waiting for hardware.
3508 */
3509 switch (skdev->state) {
3510 case SKD_DRVR_STATE_PAUSED:
3511 case SKD_DRVR_STATE_BUSY:
3512 case SKD_DRVR_STATE_BUSY_IMMINENT:
3513 case SKD_DRVR_STATE_BUSY_ERASE:
3514 case SKD_DRVR_STATE_STARTING:
3515 case SKD_DRVR_STATE_RESTARTING:
3516 case SKD_DRVR_STATE_FAULT:
3517 case SKD_DRVR_STATE_IDLE:
3518 case SKD_DRVR_STATE_LOAD:
3519 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003520 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3521 skd_skdev_state_to_str(prev_driver_state),
3522 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3523 skdev->state);
3524 dev_dbg(&skdev->pdev->dev,
3525 "**** device ONLINE...starting block queue\n");
3526 dev_dbg(&skdev->pdev->dev, "starting queue\n");
3527 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003528 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003529 skdev->gendisk_on = 1;
3530 wake_up_interruptible(&skdev->waitq);
3531 break;
3532
3533 case SKD_DRVR_STATE_DISAPPEARED:
3534 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003535 dev_dbg(&skdev->pdev->dev,
3536 "**** driver state %d, not implemented\n",
3537 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003538 return -EBUSY;
3539 }
3540 return 0;
3541}
3542
3543/*
3544 *****************************************************************************
3545 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3546 *****************************************************************************
3547 */
3548
3549static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3550{
3551 struct skd_device *skdev = skd_host_data;
3552 unsigned long flags;
3553
3554 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003555 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3556 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3557 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
3558 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003559 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3560 spin_unlock_irqrestore(&skdev->lock, flags);
3561 return IRQ_HANDLED;
3562}
3563
3564static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3565{
3566 struct skd_device *skdev = skd_host_data;
3567 unsigned long flags;
3568
3569 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003570 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3571 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003572 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3573 skd_isr_fwstate(skdev);
3574 spin_unlock_irqrestore(&skdev->lock, flags);
3575 return IRQ_HANDLED;
3576}
3577
3578static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3579{
3580 struct skd_device *skdev = skd_host_data;
3581 unsigned long flags;
3582 int flush_enqueued = 0;
3583 int deferred;
3584
3585 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003586 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3587 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003588 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3589 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3590 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003591 if (flush_enqueued)
3592 skd_request_fn(skdev->queue);
3593
3594 if (deferred)
3595 schedule_work(&skdev->completion_worker);
3596 else if (!flush_enqueued)
3597 skd_request_fn(skdev->queue);
3598
3599 spin_unlock_irqrestore(&skdev->lock, flags);
3600
3601 return IRQ_HANDLED;
3602}
3603
3604static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3605{
3606 struct skd_device *skdev = skd_host_data;
3607 unsigned long flags;
3608
3609 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003610 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3611 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003612 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3613 skd_isr_msg_from_dev(skdev);
3614 spin_unlock_irqrestore(&skdev->lock, flags);
3615 return IRQ_HANDLED;
3616}
3617
3618static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3619{
3620 struct skd_device *skdev = skd_host_data;
3621 unsigned long flags;
3622
3623 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003624 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3625 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003626 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3627 spin_unlock_irqrestore(&skdev->lock, flags);
3628 return IRQ_HANDLED;
3629}
3630
3631/*
3632 *****************************************************************************
3633 * PCIe MSI/MSI-X SETUP
3634 *****************************************************************************
3635 */
3636
3637struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003638 char isr_name[30];
3639};
3640
3641struct skd_init_msix_entry {
3642 const char *name;
3643 irq_handler_t handler;
3644};
3645
3646#define SKD_MAX_MSIX_COUNT 13
3647#define SKD_MIN_MSIX_COUNT 7
3648#define SKD_BASE_MSIX_IRQ 4
3649
3650static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3651 { "(DMA 0)", skd_reserved_isr },
3652 { "(DMA 1)", skd_reserved_isr },
3653 { "(DMA 2)", skd_reserved_isr },
3654 { "(DMA 3)", skd_reserved_isr },
3655 { "(State Change)", skd_statec_isr },
3656 { "(COMPL_Q)", skd_comp_q },
3657 { "(MSG)", skd_msg_isr },
3658 { "(Reserved)", skd_reserved_isr },
3659 { "(Reserved)", skd_reserved_isr },
3660 { "(Queue Full 0)", skd_qfull_isr },
3661 { "(Queue Full 1)", skd_qfull_isr },
3662 { "(Queue Full 2)", skd_qfull_isr },
3663 { "(Queue Full 3)", skd_qfull_isr },
3664};
3665
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003666static int skd_acquire_msix(struct skd_device *skdev)
3667{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003668 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003669 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003670
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003671 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3672 PCI_IRQ_MSIX);
3673 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003674 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003675 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003676 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003677
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003678 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3679 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003680 if (!skdev->msix_entries) {
3681 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003682 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003683 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003684 }
3685
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003686 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003687 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3688 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3689
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003690 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3691 "%s%d-msix %s", DRV_NAME, skdev->devno,
3692 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003693
3694 rc = devm_request_irq(&skdev->pdev->dev,
3695 pci_irq_vector(skdev->pdev, i),
3696 msix_entries[i].handler, 0,
3697 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003698 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003699 dev_err(&skdev->pdev->dev,
3700 "Unable to register(%d) MSI-X handler %d: %s\n",
3701 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003702 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703 }
3704 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003705
Bart Van Asschef98806d2017-08-17 13:12:58 -07003706 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
3707 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003708 return 0;
3709
3710msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003711 while (--i >= 0)
3712 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003713out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003714 kfree(skdev->msix_entries);
3715 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003716 return rc;
3717}
3718
3719static int skd_acquire_irq(struct skd_device *skdev)
3720{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003721 struct pci_dev *pdev = skdev->pdev;
3722 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003723 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003724
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003725 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003726 rc = skd_acquire_msix(skdev);
3727 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003728 return 0;
3729
Bart Van Asschef98806d2017-08-17 13:12:58 -07003730 dev_err(&skdev->pdev->dev,
3731 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003732 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003733
3734 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3735 skdev->devno);
3736
3737 if (skd_isr_type != SKD_IRQ_LEGACY)
3738 irq_flag |= PCI_IRQ_MSI;
3739 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3740 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003741 dev_err(&skdev->pdev->dev,
3742 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003743 return rc;
3744 }
3745
3746 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3747 pdev->msi_enabled ? 0 : IRQF_SHARED,
3748 skdev->isr_name, skdev);
3749 if (rc) {
3750 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003751 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
3752 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003753 return rc;
3754 }
3755
3756 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003757}
3758
3759static void skd_release_irq(struct skd_device *skdev)
3760{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003761 struct pci_dev *pdev = skdev->pdev;
3762
3763 if (skdev->msix_entries) {
3764 int i;
3765
3766 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3767 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3768 skdev);
3769 }
3770
3771 kfree(skdev->msix_entries);
3772 skdev->msix_entries = NULL;
3773 } else {
3774 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003775 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003776
3777 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003778}
3779
3780/*
3781 *****************************************************************************
3782 * CONSTRUCT
3783 *****************************************************************************
3784 */
3785
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003786static int skd_cons_skcomp(struct skd_device *skdev)
3787{
3788 int rc = 0;
3789 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003790
Bart Van Asschef98806d2017-08-17 13:12:58 -07003791 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003792 "comp pci_alloc, total bytes %zd entries %d\n",
3793 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003794
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003795 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07003796 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003797
3798 if (skcomp == NULL) {
3799 rc = -ENOMEM;
3800 goto err_out;
3801 }
3802
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003803 skdev->skcomp_table = skcomp;
3804 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3805 sizeof(*skcomp) *
3806 SKD_N_COMPLETION_ENTRY);
3807
3808err_out:
3809 return rc;
3810}
3811
3812static int skd_cons_skmsg(struct skd_device *skdev)
3813{
3814 int rc = 0;
3815 u32 i;
3816
Bart Van Asschef98806d2017-08-17 13:12:58 -07003817 dev_dbg(&skdev->pdev->dev,
3818 "skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3819 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
3820 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003821
3822 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3823 *skdev->num_fitmsg_context, GFP_KERNEL);
3824 if (skdev->skmsg_table == NULL) {
3825 rc = -ENOMEM;
3826 goto err_out;
3827 }
3828
3829 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3830 struct skd_fitmsg_context *skmsg;
3831
3832 skmsg = &skdev->skmsg_table[i];
3833
3834 skmsg->id = i + SKD_ID_FIT_MSG;
3835
3836 skmsg->state = SKD_MSG_STATE_IDLE;
3837 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07003838 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003839 &skmsg->mb_dma_address);
3840
3841 if (skmsg->msg_buf == NULL) {
3842 rc = -ENOMEM;
3843 goto err_out;
3844 }
3845
Bart Van Assche6507f432017-08-17 13:13:06 -07003846 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
3847 (FIT_QCMD_ALIGN - 1),
3848 "not aligned: msg_buf %p mb_dma_address %#llx\n",
3849 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003850 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3851
3852 skmsg->next = &skmsg[1];
3853 }
3854
3855 /* Free list is in order starting with the 0th entry. */
3856 skdev->skmsg_table[i - 1].next = NULL;
3857 skdev->skmsg_free_list = skdev->skmsg_table;
3858
3859err_out:
3860 return rc;
3861}
3862
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003863static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
3864 u32 n_sg,
3865 dma_addr_t *ret_dma_addr)
3866{
3867 struct fit_sg_descriptor *sg_list;
3868 u32 nbytes;
3869
3870 nbytes = sizeof(*sg_list) * n_sg;
3871
3872 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
3873
3874 if (sg_list != NULL) {
3875 uint64_t dma_address = *ret_dma_addr;
3876 u32 i;
3877
3878 memset(sg_list, 0, nbytes);
3879
3880 for (i = 0; i < n_sg - 1; i++) {
3881 uint64_t ndp_off;
3882 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
3883
3884 sg_list[i].next_desc_ptr = dma_address + ndp_off;
3885 }
3886 sg_list[i].next_desc_ptr = 0LL;
3887 }
3888
3889 return sg_list;
3890}
3891
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003892static int skd_cons_skreq(struct skd_device *skdev)
3893{
3894 int rc = 0;
3895 u32 i;
3896
Bart Van Asschef98806d2017-08-17 13:12:58 -07003897 dev_dbg(&skdev->pdev->dev,
3898 "skreq_table kzalloc, struct %lu, count %u total %lu\n",
3899 sizeof(struct skd_request_context), skdev->num_req_context,
3900 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003901
3902 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
3903 * skdev->num_req_context, GFP_KERNEL);
3904 if (skdev->skreq_table == NULL) {
3905 rc = -ENOMEM;
3906 goto err_out;
3907 }
3908
Bart Van Asschef98806d2017-08-17 13:12:58 -07003909 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
3910 skdev->sgs_per_request, sizeof(struct scatterlist),
3911 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003912
3913 for (i = 0; i < skdev->num_req_context; i++) {
3914 struct skd_request_context *skreq;
3915
3916 skreq = &skdev->skreq_table[i];
3917
3918 skreq->id = i + SKD_ID_RW_REQUEST;
3919 skreq->state = SKD_REQ_STATE_IDLE;
3920
3921 skreq->sg = kzalloc(sizeof(struct scatterlist) *
3922 skdev->sgs_per_request, GFP_KERNEL);
3923 if (skreq->sg == NULL) {
3924 rc = -ENOMEM;
3925 goto err_out;
3926 }
3927 sg_init_table(skreq->sg, skdev->sgs_per_request);
3928
3929 skreq->sksg_list = skd_cons_sg_list(skdev,
3930 skdev->sgs_per_request,
3931 &skreq->sksg_dma_address);
3932
3933 if (skreq->sksg_list == NULL) {
3934 rc = -ENOMEM;
3935 goto err_out;
3936 }
3937
3938 skreq->next = &skreq[1];
3939 }
3940
3941 /* Free list is in order starting with the 0th entry. */
3942 skdev->skreq_table[i - 1].next = NULL;
3943 skdev->skreq_free_list = skdev->skreq_table;
3944
3945err_out:
3946 return rc;
3947}
3948
3949static int skd_cons_skspcl(struct skd_device *skdev)
3950{
3951 int rc = 0;
3952 u32 i, nbytes;
3953
Bart Van Asschef98806d2017-08-17 13:12:58 -07003954 dev_dbg(&skdev->pdev->dev,
3955 "skspcl_table kzalloc, struct %lu, count %u total %lu\n",
3956 sizeof(struct skd_special_context), skdev->n_special,
3957 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003958
3959 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
3960 * skdev->n_special, GFP_KERNEL);
3961 if (skdev->skspcl_table == NULL) {
3962 rc = -ENOMEM;
3963 goto err_out;
3964 }
3965
3966 for (i = 0; i < skdev->n_special; i++) {
3967 struct skd_special_context *skspcl;
3968
3969 skspcl = &skdev->skspcl_table[i];
3970
3971 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
3972 skspcl->req.state = SKD_REQ_STATE_IDLE;
3973
3974 skspcl->req.next = &skspcl[1].req;
3975
3976 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3977
Joe Perchesa5bbf612014-08-08 14:24:12 -07003978 skspcl->msg_buf =
3979 pci_zalloc_consistent(skdev->pdev, nbytes,
3980 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003981 if (skspcl->msg_buf == NULL) {
3982 rc = -ENOMEM;
3983 goto err_out;
3984 }
3985
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003986 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
3987 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
3988 if (skspcl->req.sg == NULL) {
3989 rc = -ENOMEM;
3990 goto err_out;
3991 }
3992
3993 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
3994 SKD_N_SG_PER_SPECIAL,
3995 &skspcl->req.
3996 sksg_dma_address);
3997 if (skspcl->req.sksg_list == NULL) {
3998 rc = -ENOMEM;
3999 goto err_out;
4000 }
4001 }
4002
4003 /* Free list is in order starting with the 0th entry. */
4004 skdev->skspcl_table[i - 1].req.next = NULL;
4005 skdev->skspcl_free_list = skdev->skspcl_table;
4006
4007 return rc;
4008
4009err_out:
4010 return rc;
4011}
4012
4013static int skd_cons_sksb(struct skd_device *skdev)
4014{
4015 int rc = 0;
4016 struct skd_special_context *skspcl;
4017 u32 nbytes;
4018
4019 skspcl = &skdev->internal_skspcl;
4020
4021 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4022 skspcl->req.state = SKD_REQ_STATE_IDLE;
4023
4024 nbytes = SKD_N_INTERNAL_BYTES;
4025
Joe Perchesa5bbf612014-08-08 14:24:12 -07004026 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4027 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004028 if (skspcl->data_buf == NULL) {
4029 rc = -ENOMEM;
4030 goto err_out;
4031 }
4032
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004033 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004034 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4035 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004036 if (skspcl->msg_buf == NULL) {
4037 rc = -ENOMEM;
4038 goto err_out;
4039 }
4040
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004041 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4042 &skspcl->req.sksg_dma_address);
4043 if (skspcl->req.sksg_list == NULL) {
4044 rc = -ENOMEM;
4045 goto err_out;
4046 }
4047
4048 if (!skd_format_internal_skspcl(skdev)) {
4049 rc = -EINVAL;
4050 goto err_out;
4051 }
4052
4053err_out:
4054 return rc;
4055}
4056
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004057static int skd_cons_disk(struct skd_device *skdev)
4058{
4059 int rc = 0;
4060 struct gendisk *disk;
4061 struct request_queue *q;
4062 unsigned long flags;
4063
4064 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4065 if (!disk) {
4066 rc = -ENOMEM;
4067 goto err_out;
4068 }
4069
4070 skdev->disk = disk;
4071 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4072
4073 disk->major = skdev->major;
4074 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4075 disk->fops = &skd_blockdev_ops;
4076 disk->private_data = skdev;
4077
Jens Axboefcd37eb2013-11-01 10:14:56 -06004078 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004079 if (!q) {
4080 rc = -ENOMEM;
4081 goto err_out;
4082 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004083 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004084
4085 skdev->queue = q;
4086 disk->queue = q;
4087 q->queuedata = skdev;
4088
Jens Axboe6975f732016-03-30 10:11:42 -06004089 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004090 blk_queue_max_segments(q, skdev->sgs_per_request);
4091 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4092
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004093 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004094 blk_queue_io_opt(q, 8192);
4095
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004096 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004097 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004098
4099 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004100 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06004101 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004102 spin_unlock_irqrestore(&skdev->lock, flags);
4103
4104err_out:
4105 return rc;
4106}
4107
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004108#define SKD_N_DEV_TABLE 16u
4109static u32 skd_next_devno;
4110
4111static struct skd_device *skd_construct(struct pci_dev *pdev)
4112{
4113 struct skd_device *skdev;
4114 int blk_major = skd_major;
4115 int rc;
4116
4117 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4118
4119 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004120 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004121 return NULL;
4122 }
4123
4124 skdev->state = SKD_DRVR_STATE_LOAD;
4125 skdev->pdev = pdev;
4126 skdev->devno = skd_next_devno++;
4127 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004128 skdev->dev_max_queue_depth = 0;
4129
4130 skdev->num_req_context = skd_max_queue_depth;
4131 skdev->num_fitmsg_context = skd_max_queue_depth;
4132 skdev->n_special = skd_max_pass_thru;
4133 skdev->cur_max_queue_depth = 1;
4134 skdev->queue_low_water_mark = 1;
4135 skdev->proto_ver = 99;
4136 skdev->sgs_per_request = skd_sgs_per_request;
4137 skdev->dbg_level = skd_dbg_level;
4138
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004139 spin_lock_init(&skdev->lock);
4140
4141 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4142
Bart Van Asschef98806d2017-08-17 13:12:58 -07004143 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004144 rc = skd_cons_skcomp(skdev);
4145 if (rc < 0)
4146 goto err_out;
4147
Bart Van Asschef98806d2017-08-17 13:12:58 -07004148 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004149 rc = skd_cons_skmsg(skdev);
4150 if (rc < 0)
4151 goto err_out;
4152
Bart Van Asschef98806d2017-08-17 13:12:58 -07004153 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004154 rc = skd_cons_skreq(skdev);
4155 if (rc < 0)
4156 goto err_out;
4157
Bart Van Asschef98806d2017-08-17 13:12:58 -07004158 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004159 rc = skd_cons_skspcl(skdev);
4160 if (rc < 0)
4161 goto err_out;
4162
Bart Van Asschef98806d2017-08-17 13:12:58 -07004163 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004164 rc = skd_cons_sksb(skdev);
4165 if (rc < 0)
4166 goto err_out;
4167
Bart Van Asschef98806d2017-08-17 13:12:58 -07004168 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004169 rc = skd_cons_disk(skdev);
4170 if (rc < 0)
4171 goto err_out;
4172
Bart Van Asschef98806d2017-08-17 13:12:58 -07004173 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004174 return skdev;
4175
4176err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07004177 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004178 skd_destruct(skdev);
4179 return NULL;
4180}
4181
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004182/*
4183 *****************************************************************************
4184 * DESTRUCT (FREE)
4185 *****************************************************************************
4186 */
4187
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004188static void skd_free_skcomp(struct skd_device *skdev)
4189{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07004190 if (skdev->skcomp_table)
4191 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004192 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004193
4194 skdev->skcomp_table = NULL;
4195 skdev->cq_dma_address = 0;
4196}
4197
4198static void skd_free_skmsg(struct skd_device *skdev)
4199{
4200 u32 i;
4201
4202 if (skdev->skmsg_table == NULL)
4203 return;
4204
4205 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4206 struct skd_fitmsg_context *skmsg;
4207
4208 skmsg = &skdev->skmsg_table[i];
4209
4210 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004211 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4212 skmsg->msg_buf,
4213 skmsg->mb_dma_address);
4214 }
4215 skmsg->msg_buf = NULL;
4216 skmsg->mb_dma_address = 0;
4217 }
4218
4219 kfree(skdev->skmsg_table);
4220 skdev->skmsg_table = NULL;
4221}
4222
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004223static void skd_free_sg_list(struct skd_device *skdev,
4224 struct fit_sg_descriptor *sg_list,
4225 u32 n_sg, dma_addr_t dma_addr)
4226{
4227 if (sg_list != NULL) {
4228 u32 nbytes;
4229
4230 nbytes = sizeof(*sg_list) * n_sg;
4231
4232 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4233 }
4234}
4235
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004236static void skd_free_skreq(struct skd_device *skdev)
4237{
4238 u32 i;
4239
4240 if (skdev->skreq_table == NULL)
4241 return;
4242
4243 for (i = 0; i < skdev->num_req_context; i++) {
4244 struct skd_request_context *skreq;
4245
4246 skreq = &skdev->skreq_table[i];
4247
4248 skd_free_sg_list(skdev, skreq->sksg_list,
4249 skdev->sgs_per_request,
4250 skreq->sksg_dma_address);
4251
4252 skreq->sksg_list = NULL;
4253 skreq->sksg_dma_address = 0;
4254
4255 kfree(skreq->sg);
4256 }
4257
4258 kfree(skdev->skreq_table);
4259 skdev->skreq_table = NULL;
4260}
4261
4262static void skd_free_skspcl(struct skd_device *skdev)
4263{
4264 u32 i;
4265 u32 nbytes;
4266
4267 if (skdev->skspcl_table == NULL)
4268 return;
4269
4270 for (i = 0; i < skdev->n_special; i++) {
4271 struct skd_special_context *skspcl;
4272
4273 skspcl = &skdev->skspcl_table[i];
4274
4275 if (skspcl->msg_buf != NULL) {
4276 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4277 pci_free_consistent(skdev->pdev, nbytes,
4278 skspcl->msg_buf,
4279 skspcl->mb_dma_address);
4280 }
4281
4282 skspcl->msg_buf = NULL;
4283 skspcl->mb_dma_address = 0;
4284
4285 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4286 SKD_N_SG_PER_SPECIAL,
4287 skspcl->req.sksg_dma_address);
4288
4289 skspcl->req.sksg_list = NULL;
4290 skspcl->req.sksg_dma_address = 0;
4291
4292 kfree(skspcl->req.sg);
4293 }
4294
4295 kfree(skdev->skspcl_table);
4296 skdev->skspcl_table = NULL;
4297}
4298
4299static void skd_free_sksb(struct skd_device *skdev)
4300{
4301 struct skd_special_context *skspcl;
4302 u32 nbytes;
4303
4304 skspcl = &skdev->internal_skspcl;
4305
4306 if (skspcl->data_buf != NULL) {
4307 nbytes = SKD_N_INTERNAL_BYTES;
4308
4309 pci_free_consistent(skdev->pdev, nbytes,
4310 skspcl->data_buf, skspcl->db_dma_address);
4311 }
4312
4313 skspcl->data_buf = NULL;
4314 skspcl->db_dma_address = 0;
4315
4316 if (skspcl->msg_buf != NULL) {
4317 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4318 pci_free_consistent(skdev->pdev, nbytes,
4319 skspcl->msg_buf, skspcl->mb_dma_address);
4320 }
4321
4322 skspcl->msg_buf = NULL;
4323 skspcl->mb_dma_address = 0;
4324
4325 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4326 skspcl->req.sksg_dma_address);
4327
4328 skspcl->req.sksg_list = NULL;
4329 skspcl->req.sksg_dma_address = 0;
4330}
4331
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004332static void skd_free_disk(struct skd_device *skdev)
4333{
4334 struct gendisk *disk = skdev->disk;
4335
Bart Van Assche7277cc62017-08-17 13:12:45 -07004336 if (disk && (disk->flags & GENHD_FL_UP))
4337 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004338
Bart Van Assche7277cc62017-08-17 13:12:45 -07004339 if (skdev->queue) {
4340 blk_cleanup_queue(skdev->queue);
4341 skdev->queue = NULL;
4342 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004343 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004344
4345 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004346 skdev->disk = NULL;
4347}
4348
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004349static void skd_destruct(struct skd_device *skdev)
4350{
4351 if (skdev == NULL)
4352 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004353
Bart Van Asschef98806d2017-08-17 13:12:58 -07004354 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004355 skd_free_disk(skdev);
4356
Bart Van Asschef98806d2017-08-17 13:12:58 -07004357 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004358 skd_free_sksb(skdev);
4359
Bart Van Asschef98806d2017-08-17 13:12:58 -07004360 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004361 skd_free_skspcl(skdev);
4362
Bart Van Asschef98806d2017-08-17 13:12:58 -07004363 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004364 skd_free_skreq(skdev);
4365
Bart Van Asschef98806d2017-08-17 13:12:58 -07004366 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004367 skd_free_skmsg(skdev);
4368
Bart Van Asschef98806d2017-08-17 13:12:58 -07004369 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004370 skd_free_skcomp(skdev);
4371
Bart Van Asschef98806d2017-08-17 13:12:58 -07004372 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004373 kfree(skdev);
4374}
4375
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004376/*
4377 *****************************************************************************
4378 * BLOCK DEVICE (BDEV) GLUE
4379 *****************************************************************************
4380 */
4381
4382static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4383{
4384 struct skd_device *skdev;
4385 u64 capacity;
4386
4387 skdev = bdev->bd_disk->private_data;
4388
Bart Van Asschef98806d2017-08-17 13:12:58 -07004389 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
4390 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004391
4392 if (skdev->read_cap_is_valid) {
4393 capacity = get_capacity(skdev->disk);
4394 geo->heads = 64;
4395 geo->sectors = 255;
4396 geo->cylinders = (capacity) / (255 * 64);
4397
4398 return 0;
4399 }
4400 return -EIO;
4401}
4402
Dan Williams0d52c7562016-06-15 19:44:20 -07004403static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004404{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004405 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07004406 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004407 return 0;
4408}
4409
4410static const struct block_device_operations skd_blockdev_ops = {
4411 .owner = THIS_MODULE,
4412 .ioctl = skd_bdev_ioctl,
4413 .getgeo = skd_bdev_getgeo,
4414};
4415
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004416/*
4417 *****************************************************************************
4418 * PCIe DRIVER GLUE
4419 *****************************************************************************
4420 */
4421
Benoit Taine9baa3c32014-08-08 15:56:03 +02004422static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004423 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4424 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4425 { 0 } /* terminate list */
4426};
4427
4428MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4429
4430static char *skd_pci_info(struct skd_device *skdev, char *str)
4431{
4432 int pcie_reg;
4433
4434 strcpy(str, "PCIe (");
4435 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4436
4437 if (pcie_reg) {
4438
4439 char lwstr[6];
4440 uint16_t pcie_lstat, lspeed, lwidth;
4441
4442 pcie_reg += 0x12;
4443 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4444 lspeed = pcie_lstat & (0xF);
4445 lwidth = (pcie_lstat & 0x3F0) >> 4;
4446
4447 if (lspeed == 1)
4448 strcat(str, "2.5GT/s ");
4449 else if (lspeed == 2)
4450 strcat(str, "5.0GT/s ");
4451 else
4452 strcat(str, "<unknown> ");
4453 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4454 strcat(str, lwstr);
4455 }
4456 return str;
4457}
4458
4459static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4460{
4461 int i;
4462 int rc = 0;
4463 char pci_str[32];
4464 struct skd_device *skdev;
4465
Bart Van Asschef98806d2017-08-17 13:12:58 -07004466 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
4467 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4468 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
4469 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004470
4471 rc = pci_enable_device(pdev);
4472 if (rc)
4473 return rc;
4474 rc = pci_request_regions(pdev, DRV_NAME);
4475 if (rc)
4476 goto err_out;
4477 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4478 if (!rc) {
4479 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004480 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4481 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004482 }
4483 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004484 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004485 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004486 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004487 goto err_out_regions;
4488 }
4489 }
4490
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004491 if (!skd_major) {
4492 rc = register_blkdev(0, DRV_NAME);
4493 if (rc < 0)
4494 goto err_out_regions;
4495 BUG_ON(!rc);
4496 skd_major = rc;
4497 }
4498
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004499 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004500 if (skdev == NULL) {
4501 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004502 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004503 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004504
4505 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004506 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004507
4508 pci_set_master(pdev);
4509 rc = pci_enable_pcie_error_reporting(pdev);
4510 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004511 dev_err(&pdev->dev,
4512 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004513 skdev->pcie_error_reporting_is_enabled = 0;
4514 } else
4515 skdev->pcie_error_reporting_is_enabled = 1;
4516
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004517 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004518
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004519 for (i = 0; i < SKD_MAX_BARS; i++) {
4520 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4521 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4522 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4523 skdev->mem_size[i]);
4524 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004525 dev_err(&pdev->dev,
4526 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004527 rc = -ENODEV;
4528 goto err_out_iounmap;
4529 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004530 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4531 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4532 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004533 }
4534
4535 rc = skd_acquire_irq(skdev);
4536 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004537 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004538 goto err_out_iounmap;
4539 }
4540
4541 rc = skd_start_timer(skdev);
4542 if (rc)
4543 goto err_out_timer;
4544
4545 init_waitqueue_head(&skdev->waitq);
4546
4547 skd_start_device(skdev);
4548
4549 rc = wait_event_interruptible_timeout(skdev->waitq,
4550 (skdev->gendisk_on),
4551 (SKD_START_WAIT_SECONDS * HZ));
4552 if (skdev->gendisk_on > 0) {
4553 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004554 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004555 rc = 0;
4556 } else {
4557 /* we timed out, something is wrong with the device,
4558 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07004559 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
4560 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004561 /* in case of no error; we timeout with ENXIO */
4562 if (!rc)
4563 rc = -ENXIO;
4564 goto err_out_timer;
4565 }
4566
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004567 return rc;
4568
4569err_out_timer:
4570 skd_stop_device(skdev);
4571 skd_release_irq(skdev);
4572
4573err_out_iounmap:
4574 for (i = 0; i < SKD_MAX_BARS; i++)
4575 if (skdev->mem_map[i])
4576 iounmap(skdev->mem_map[i]);
4577
4578 if (skdev->pcie_error_reporting_is_enabled)
4579 pci_disable_pcie_error_reporting(pdev);
4580
4581 skd_destruct(skdev);
4582
4583err_out_regions:
4584 pci_release_regions(pdev);
4585
4586err_out:
4587 pci_disable_device(pdev);
4588 pci_set_drvdata(pdev, NULL);
4589 return rc;
4590}
4591
4592static void skd_pci_remove(struct pci_dev *pdev)
4593{
4594 int i;
4595 struct skd_device *skdev;
4596
4597 skdev = pci_get_drvdata(pdev);
4598 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004599 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004600 return;
4601 }
4602 skd_stop_device(skdev);
4603 skd_release_irq(skdev);
4604
4605 for (i = 0; i < SKD_MAX_BARS; i++)
4606 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004607 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004608
4609 if (skdev->pcie_error_reporting_is_enabled)
4610 pci_disable_pcie_error_reporting(pdev);
4611
4612 skd_destruct(skdev);
4613
4614 pci_release_regions(pdev);
4615 pci_disable_device(pdev);
4616 pci_set_drvdata(pdev, NULL);
4617
4618 return;
4619}
4620
4621static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4622{
4623 int i;
4624 struct skd_device *skdev;
4625
4626 skdev = pci_get_drvdata(pdev);
4627 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004628 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004629 return -EIO;
4630 }
4631
4632 skd_stop_device(skdev);
4633
4634 skd_release_irq(skdev);
4635
4636 for (i = 0; i < SKD_MAX_BARS; i++)
4637 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004638 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004639
4640 if (skdev->pcie_error_reporting_is_enabled)
4641 pci_disable_pcie_error_reporting(pdev);
4642
4643 pci_release_regions(pdev);
4644 pci_save_state(pdev);
4645 pci_disable_device(pdev);
4646 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4647 return 0;
4648}
4649
4650static int skd_pci_resume(struct pci_dev *pdev)
4651{
4652 int i;
4653 int rc = 0;
4654 struct skd_device *skdev;
4655
4656 skdev = pci_get_drvdata(pdev);
4657 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004658 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004659 return -1;
4660 }
4661
4662 pci_set_power_state(pdev, PCI_D0);
4663 pci_enable_wake(pdev, PCI_D0, 0);
4664 pci_restore_state(pdev);
4665
4666 rc = pci_enable_device(pdev);
4667 if (rc)
4668 return rc;
4669 rc = pci_request_regions(pdev, DRV_NAME);
4670 if (rc)
4671 goto err_out;
4672 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4673 if (!rc) {
4674 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4675
Bart Van Asschef98806d2017-08-17 13:12:58 -07004676 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4677 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004678 }
4679 } else {
4680 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4681 if (rc) {
4682
Bart Van Asschef98806d2017-08-17 13:12:58 -07004683 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004684 goto err_out_regions;
4685 }
4686 }
4687
4688 pci_set_master(pdev);
4689 rc = pci_enable_pcie_error_reporting(pdev);
4690 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004691 dev_err(&pdev->dev,
4692 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004693 skdev->pcie_error_reporting_is_enabled = 0;
4694 } else
4695 skdev->pcie_error_reporting_is_enabled = 1;
4696
4697 for (i = 0; i < SKD_MAX_BARS; i++) {
4698
4699 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4700 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4701 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4702 skdev->mem_size[i]);
4703 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004704 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004705 rc = -ENODEV;
4706 goto err_out_iounmap;
4707 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004708 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4709 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4710 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004711 }
4712 rc = skd_acquire_irq(skdev);
4713 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004714 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004715 goto err_out_iounmap;
4716 }
4717
4718 rc = skd_start_timer(skdev);
4719 if (rc)
4720 goto err_out_timer;
4721
4722 init_waitqueue_head(&skdev->waitq);
4723
4724 skd_start_device(skdev);
4725
4726 return rc;
4727
4728err_out_timer:
4729 skd_stop_device(skdev);
4730 skd_release_irq(skdev);
4731
4732err_out_iounmap:
4733 for (i = 0; i < SKD_MAX_BARS; i++)
4734 if (skdev->mem_map[i])
4735 iounmap(skdev->mem_map[i]);
4736
4737 if (skdev->pcie_error_reporting_is_enabled)
4738 pci_disable_pcie_error_reporting(pdev);
4739
4740err_out_regions:
4741 pci_release_regions(pdev);
4742
4743err_out:
4744 pci_disable_device(pdev);
4745 return rc;
4746}
4747
4748static void skd_pci_shutdown(struct pci_dev *pdev)
4749{
4750 struct skd_device *skdev;
4751
Bart Van Asschef98806d2017-08-17 13:12:58 -07004752 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004753
4754 skdev = pci_get_drvdata(pdev);
4755 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004756 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004757 return;
4758 }
4759
Bart Van Asschef98806d2017-08-17 13:12:58 -07004760 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004761 skd_stop_device(skdev);
4762}
4763
4764static struct pci_driver skd_driver = {
4765 .name = DRV_NAME,
4766 .id_table = skd_pci_tbl,
4767 .probe = skd_pci_probe,
4768 .remove = skd_pci_remove,
4769 .suspend = skd_pci_suspend,
4770 .resume = skd_pci_resume,
4771 .shutdown = skd_pci_shutdown,
4772};
4773
4774/*
4775 *****************************************************************************
4776 * LOGGING SUPPORT
4777 *****************************************************************************
4778 */
4779
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004780const char *skd_drive_state_to_str(int state)
4781{
4782 switch (state) {
4783 case FIT_SR_DRIVE_OFFLINE:
4784 return "OFFLINE";
4785 case FIT_SR_DRIVE_INIT:
4786 return "INIT";
4787 case FIT_SR_DRIVE_ONLINE:
4788 return "ONLINE";
4789 case FIT_SR_DRIVE_BUSY:
4790 return "BUSY";
4791 case FIT_SR_DRIVE_FAULT:
4792 return "FAULT";
4793 case FIT_SR_DRIVE_DEGRADED:
4794 return "DEGRADED";
4795 case FIT_SR_PCIE_LINK_DOWN:
4796 return "INK_DOWN";
4797 case FIT_SR_DRIVE_SOFT_RESET:
4798 return "SOFT_RESET";
4799 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4800 return "NEED_FW";
4801 case FIT_SR_DRIVE_INIT_FAULT:
4802 return "INIT_FAULT";
4803 case FIT_SR_DRIVE_BUSY_SANITIZE:
4804 return "BUSY_SANITIZE";
4805 case FIT_SR_DRIVE_BUSY_ERASE:
4806 return "BUSY_ERASE";
4807 case FIT_SR_DRIVE_FW_BOOTING:
4808 return "FW_BOOTING";
4809 default:
4810 return "???";
4811 }
4812}
4813
4814const char *skd_skdev_state_to_str(enum skd_drvr_state state)
4815{
4816 switch (state) {
4817 case SKD_DRVR_STATE_LOAD:
4818 return "LOAD";
4819 case SKD_DRVR_STATE_IDLE:
4820 return "IDLE";
4821 case SKD_DRVR_STATE_BUSY:
4822 return "BUSY";
4823 case SKD_DRVR_STATE_STARTING:
4824 return "STARTING";
4825 case SKD_DRVR_STATE_ONLINE:
4826 return "ONLINE";
4827 case SKD_DRVR_STATE_PAUSING:
4828 return "PAUSING";
4829 case SKD_DRVR_STATE_PAUSED:
4830 return "PAUSED";
4831 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
4832 return "DRAINING_TIMEOUT";
4833 case SKD_DRVR_STATE_RESTARTING:
4834 return "RESTARTING";
4835 case SKD_DRVR_STATE_RESUMING:
4836 return "RESUMING";
4837 case SKD_DRVR_STATE_STOPPING:
4838 return "STOPPING";
4839 case SKD_DRVR_STATE_SYNCING:
4840 return "SYNCING";
4841 case SKD_DRVR_STATE_FAULT:
4842 return "FAULT";
4843 case SKD_DRVR_STATE_DISAPPEARED:
4844 return "DISAPPEARED";
4845 case SKD_DRVR_STATE_BUSY_ERASE:
4846 return "BUSY_ERASE";
4847 case SKD_DRVR_STATE_BUSY_SANITIZE:
4848 return "BUSY_SANITIZE";
4849 case SKD_DRVR_STATE_BUSY_IMMINENT:
4850 return "BUSY_IMMINENT";
4851 case SKD_DRVR_STATE_WAIT_BOOT:
4852 return "WAIT_BOOT";
4853
4854 default:
4855 return "???";
4856 }
4857}
4858
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304859static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004860{
4861 switch (state) {
4862 case SKD_MSG_STATE_IDLE:
4863 return "IDLE";
4864 case SKD_MSG_STATE_BUSY:
4865 return "BUSY";
4866 default:
4867 return "???";
4868 }
4869}
4870
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304871static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004872{
4873 switch (state) {
4874 case SKD_REQ_STATE_IDLE:
4875 return "IDLE";
4876 case SKD_REQ_STATE_SETUP:
4877 return "SETUP";
4878 case SKD_REQ_STATE_BUSY:
4879 return "BUSY";
4880 case SKD_REQ_STATE_COMPLETED:
4881 return "COMPLETED";
4882 case SKD_REQ_STATE_TIMEOUT:
4883 return "TIMEOUT";
4884 case SKD_REQ_STATE_ABORTED:
4885 return "ABORTED";
4886 default:
4887 return "???";
4888 }
4889}
4890
4891static void skd_log_skdev(struct skd_device *skdev, const char *event)
4892{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004893 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
4894 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
4895 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
4896 skd_skdev_state_to_str(skdev->state), skdev->state);
4897 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
4898 skdev->in_flight, skdev->cur_max_queue_depth,
4899 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
4900 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
4901 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004902}
4903
4904static void skd_log_skmsg(struct skd_device *skdev,
4905 struct skd_fitmsg_context *skmsg, const char *event)
4906{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004907 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
4908 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
4909 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
4910 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004911}
4912
4913static void skd_log_skreq(struct skd_device *skdev,
4914 struct skd_request_context *skreq, const char *event)
4915{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004916 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
4917 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
4918 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
4919 skreq->fitmsg_id);
4920 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07004921 skreq->timeout_stamp, skreq->data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004922
Jens Axboefcd37eb2013-11-01 10:14:56 -06004923 if (skreq->req != NULL) {
4924 struct request *req = skreq->req;
4925 u32 lba = (u32)blk_rq_pos(req);
4926 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004927
Bart Van Asschef98806d2017-08-17 13:12:58 -07004928 dev_dbg(&skdev->pdev->dev,
4929 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
4930 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06004931 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07004932 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004933}
4934
4935/*
4936 *****************************************************************************
4937 * MODULE GLUE
4938 *****************************************************************************
4939 */
4940
4941static int __init skd_init(void)
4942{
Bart Van Assche16a70532017-08-17 13:13:08 -07004943 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
4944 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
4945 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
4946 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
4947 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07004948 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
4949 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
4950 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07004951
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004952 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
4953
4954 switch (skd_isr_type) {
4955 case SKD_IRQ_LEGACY:
4956 case SKD_IRQ_MSI:
4957 case SKD_IRQ_MSIX:
4958 break;
4959 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004960 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004961 skd_isr_type, SKD_IRQ_DEFAULT);
4962 skd_isr_type = SKD_IRQ_DEFAULT;
4963 }
4964
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004965 if (skd_max_queue_depth < 1 ||
4966 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
4967 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004968 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
4969 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
4970 }
4971
Bart Van Assche2da7b402017-08-17 13:13:01 -07004972 if (skd_max_req_per_msg < 1 ||
4973 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004974 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004975 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
4976 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
4977 }
4978
4979 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004980 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004981 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
4982 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
4983 }
4984
4985 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004986 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004987 skd_dbg_level, 0);
4988 skd_dbg_level = 0;
4989 }
4990
4991 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004992 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004993 skd_isr_comp_limit, 0);
4994 skd_isr_comp_limit = 0;
4995 }
4996
4997 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004998 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004999 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5000 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5001 }
5002
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005003 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005004}
5005
5006static void __exit skd_exit(void)
5007{
5008 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5009
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005010 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005011
5012 if (skd_major)
5013 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005014}
5015
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005016module_init(skd_init);
5017module_exit(skd_exit);