blob: a363d5f6bcb5c82985733ae6449144e9e6ec3d50 [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 {
266 volatile void __iomem *mem_map[SKD_MAX_BARS];
267 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;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600743 struct scatterlist *sg = &skreq->sg[0];
744 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
752 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
753 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 Asscheb1824ee2017-08-17 13:13:12 -0700760 n_sg = pci_map_sg(skdev->pdev, sg, 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
768 for (i = 0; i < n_sg; i++) {
769 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
770 u32 cnt = sg_dma_len(&sg[i]);
771 uint64_t dma_addr = sg_dma_address(&sg[i]);
772
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
1060 init_timer(&skdev->timer);
1061 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1062
1063 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1064 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001065 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001066 return rc;
1067}
1068
1069static void skd_kill_timer(struct skd_device *skdev)
1070{
1071 del_timer_sync(&skdev->timer);
1072}
1073
1074/*
1075 *****************************************************************************
1076 * IOCTL
1077 *****************************************************************************
1078 */
1079static int skd_ioctl_sg_io(struct skd_device *skdev,
1080 fmode_t mode, void __user *argp);
1081static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1082 struct skd_sg_io *sksgio);
1083static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1084 struct skd_sg_io *sksgio);
1085static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1086 struct skd_sg_io *sksgio);
1087static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1088 struct skd_sg_io *sksgio, int dxfer_dir);
1089static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1090 struct skd_sg_io *sksgio);
1091static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1092static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1093 struct skd_sg_io *sksgio);
1094static int skd_sg_io_put_status(struct skd_device *skdev,
1095 struct skd_sg_io *sksgio);
1096
1097static void skd_complete_special(struct skd_device *skdev,
1098 volatile struct fit_completion_entry_v1
1099 *skcomp,
1100 volatile struct fit_comp_error_info *skerr,
1101 struct skd_special_context *skspcl);
1102
1103static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1104 uint cmd_in, ulong arg)
1105{
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001106 static const int sg_version_num = 30527;
1107 int rc = 0, timeout;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001108 struct gendisk *disk = bdev->bd_disk;
1109 struct skd_device *skdev = disk->private_data;
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001110 int __user *p = (int __user *)arg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001111
Bart Van Asschef98806d2017-08-17 13:12:58 -07001112 dev_dbg(&skdev->pdev->dev,
1113 "%s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1114 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001115
1116 if (!capable(CAP_SYS_ADMIN))
1117 return -EPERM;
1118
1119 switch (cmd_in) {
1120 case SG_SET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001121 rc = get_user(timeout, p);
1122 if (!rc)
1123 disk->queue->sg_timeout = clock_t_to_jiffies(timeout);
1124 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001125 case SG_GET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001126 rc = jiffies_to_clock_t(disk->queue->sg_timeout);
1127 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001128 case SG_GET_VERSION_NUM:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001129 rc = put_user(sg_version_num, p);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001130 break;
1131 case SG_IO:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001132 rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001133 break;
1134
1135 default:
1136 rc = -ENOTTY;
1137 break;
1138 }
1139
Bart Van Asschef98806d2017-08-17 13:12:58 -07001140 dev_dbg(&skdev->pdev->dev, "%s: completion rc %d\n", disk->disk_name,
1141 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001142 return rc;
1143}
1144
1145static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1146 void __user *argp)
1147{
1148 int rc;
1149 struct skd_sg_io sksgio;
1150
1151 memset(&sksgio, 0, sizeof(sksgio));
1152 sksgio.mode = mode;
1153 sksgio.argp = argp;
1154 sksgio.iov = &sksgio.no_iov_iov;
1155
1156 switch (skdev->state) {
1157 case SKD_DRVR_STATE_ONLINE:
1158 case SKD_DRVR_STATE_BUSY_IMMINENT:
1159 break;
1160
1161 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001162 dev_dbg(&skdev->pdev->dev, "drive not online\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001163 rc = -ENXIO;
1164 goto out;
1165 }
1166
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001167 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1168 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001169 goto out;
1170
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001171 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1172 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001173 goto out;
1174
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001175 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1176 if (rc)
1177 goto out;
1178
1179 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1180 if (rc)
1181 goto out;
1182
1183 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1184 if (rc)
1185 goto out;
1186
1187 rc = skd_sg_io_await(skdev, &sksgio);
1188 if (rc)
1189 goto out;
1190
1191 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1192 if (rc)
1193 goto out;
1194
1195 rc = skd_sg_io_put_status(skdev, &sksgio);
1196 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001197 goto out;
1198
1199 rc = 0;
1200
1201out:
1202 skd_sg_io_release_skspcl(skdev, &sksgio);
1203
1204 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1205 kfree(sksgio.iov);
1206 return rc;
1207}
1208
1209static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1210 struct skd_sg_io *sksgio)
1211{
1212 struct sg_io_hdr *sgp = &sksgio->sg;
Bart Van Assche95895e12017-08-17 13:12:55 -07001213 int i, __maybe_unused acc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001214
1215 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001216 dev_dbg(&skdev->pdev->dev, "access sg failed %p\n",
1217 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001218 return -EFAULT;
1219 }
1220
1221 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001222 dev_dbg(&skdev->pdev->dev, "copy_from_user sg failed %p\n",
1223 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001224 return -EFAULT;
1225 }
1226
1227 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001228 dev_dbg(&skdev->pdev->dev, "interface_id invalid 0x%x\n",
1229 sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001230 return -EINVAL;
1231 }
1232
1233 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001234 dev_dbg(&skdev->pdev->dev, "cmd_len invalid %d\n",
1235 sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001236 return -EINVAL;
1237 }
1238
1239 if (sgp->iovec_count > 256) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001240 dev_dbg(&skdev->pdev->dev, "iovec_count invalid %d\n",
1241 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001242 return -EINVAL;
1243 }
1244
1245 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001246 dev_dbg(&skdev->pdev->dev, "dxfer_len invalid %d\n",
1247 sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001248 return -EINVAL;
1249 }
1250
1251 switch (sgp->dxfer_direction) {
1252 case SG_DXFER_NONE:
1253 acc = -1;
1254 break;
1255
1256 case SG_DXFER_TO_DEV:
1257 acc = VERIFY_READ;
1258 break;
1259
1260 case SG_DXFER_FROM_DEV:
1261 case SG_DXFER_TO_FROM_DEV:
1262 acc = VERIFY_WRITE;
1263 break;
1264
1265 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001266 dev_dbg(&skdev->pdev->dev, "dxfer_dir invalid %d\n",
1267 sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001268 return -EINVAL;
1269 }
1270
1271 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001272 dev_dbg(&skdev->pdev->dev, "copy_from_user cmdp failed %p\n",
1273 sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001274 return -EFAULT;
1275 }
1276
1277 if (sgp->mx_sb_len != 0) {
1278 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001279 dev_dbg(&skdev->pdev->dev, "access sbp failed %p\n",
1280 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001281 return -EFAULT;
1282 }
1283 }
1284
1285 if (sgp->iovec_count == 0) {
1286 sksgio->iov[0].iov_base = sgp->dxferp;
1287 sksgio->iov[0].iov_len = sgp->dxfer_len;
1288 sksgio->iovcnt = 1;
1289 sksgio->dxfer_len = sgp->dxfer_len;
1290 } else {
1291 struct sg_iovec *iov;
1292 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1293 size_t iov_data_len;
1294
1295 iov = kmalloc(nbytes, GFP_KERNEL);
1296 if (iov == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001297 dev_dbg(&skdev->pdev->dev, "alloc iovec failed %d\n",
1298 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001299 return -ENOMEM;
1300 }
1301 sksgio->iov = iov;
1302 sksgio->iovcnt = sgp->iovec_count;
1303
1304 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001305 dev_dbg(&skdev->pdev->dev,
1306 "copy_from_user iovec failed %p\n",
1307 sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001308 return -EFAULT;
1309 }
1310
1311 /*
1312 * Sum up the vecs, making sure they don't overflow
1313 */
1314 iov_data_len = 0;
1315 for (i = 0; i < sgp->iovec_count; i++) {
1316 if (iov_data_len + iov[i].iov_len < iov_data_len)
1317 return -EINVAL;
1318 iov_data_len += iov[i].iov_len;
1319 }
1320
1321 /* SG_IO howto says that the shorter of the two wins */
1322 if (sgp->dxfer_len < iov_data_len) {
1323 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1324 sgp->iovec_count,
1325 sgp->dxfer_len);
1326 sksgio->dxfer_len = sgp->dxfer_len;
1327 } else
1328 sksgio->dxfer_len = iov_data_len;
1329 }
1330
1331 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1332 struct sg_iovec *iov = sksgio->iov;
1333 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1334 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001335 dev_dbg(&skdev->pdev->dev,
1336 "access data failed %p/%zd\n",
1337 iov->iov_base, iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001338 return -EFAULT;
1339 }
1340 }
1341 }
1342
1343 return 0;
1344}
1345
1346static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1347 struct skd_sg_io *sksgio)
1348{
1349 struct skd_special_context *skspcl = NULL;
1350 int rc;
1351
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001352 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001353 ulong flags;
1354
1355 spin_lock_irqsave(&skdev->lock, flags);
1356 skspcl = skdev->skspcl_free_list;
1357 if (skspcl != NULL) {
1358 skdev->skspcl_free_list =
1359 (struct skd_special_context *)skspcl->req.next;
1360 skspcl->req.id += SKD_ID_INCR;
1361 skspcl->req.state = SKD_REQ_STATE_SETUP;
1362 skspcl->orphaned = 0;
1363 skspcl->req.n_sg = 0;
1364 }
1365 spin_unlock_irqrestore(&skdev->lock, flags);
1366
1367 if (skspcl != NULL) {
1368 rc = 0;
1369 break;
1370 }
1371
Bart Van Asschef98806d2017-08-17 13:12:58 -07001372 dev_dbg(&skdev->pdev->dev, "blocking\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001373
1374 rc = wait_event_interruptible_timeout(
1375 skdev->waitq,
1376 (skdev->skspcl_free_list != NULL),
1377 msecs_to_jiffies(sksgio->sg.timeout));
1378
Bart Van Asschef98806d2017-08-17 13:12:58 -07001379 dev_dbg(&skdev->pdev->dev, "unblocking, rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001380
1381 if (rc <= 0) {
1382 if (rc == 0)
1383 rc = -ETIMEDOUT;
1384 else
1385 rc = -EINTR;
1386 break;
1387 }
1388 /*
1389 * If we get here rc > 0 meaning the timeout to
1390 * wait_event_interruptible_timeout() had time left, hence the
1391 * sought event -- non-empty free list -- happened.
1392 * Retry the allocation.
1393 */
1394 }
1395 sksgio->skspcl = skspcl;
1396
1397 return rc;
1398}
1399
1400static int skd_skreq_prep_buffering(struct skd_device *skdev,
1401 struct skd_request_context *skreq,
1402 u32 dxfer_len)
1403{
1404 u32 resid = dxfer_len;
1405
1406 /*
1407 * The DMA engine must have aligned addresses and byte counts.
1408 */
1409 resid += (-resid) & 3;
1410 skreq->sg_byte_count = resid;
1411
1412 skreq->n_sg = 0;
1413
1414 while (resid > 0) {
1415 u32 nbytes = PAGE_SIZE;
1416 u32 ix = skreq->n_sg;
1417 struct scatterlist *sg = &skreq->sg[ix];
1418 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1419 struct page *page;
1420
1421 if (nbytes > resid)
1422 nbytes = resid;
1423
1424 page = alloc_page(GFP_KERNEL);
1425 if (page == NULL)
1426 return -ENOMEM;
1427
1428 sg_set_page(sg, page, nbytes, 0);
1429
1430 /* TODO: This should be going through a pci_???()
1431 * routine to do proper mapping. */
1432 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1433 sksg->byte_count = nbytes;
1434
1435 sksg->host_side_addr = sg_phys(sg);
1436
1437 sksg->dev_side_addr = 0;
1438 sksg->next_desc_ptr = skreq->sksg_dma_address +
1439 (ix + 1) * sizeof(*sksg);
1440
1441 skreq->n_sg++;
1442 resid -= nbytes;
1443 }
1444
1445 if (skreq->n_sg > 0) {
1446 u32 ix = skreq->n_sg - 1;
1447 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1448
1449 sksg->control = FIT_SGD_CONTROL_LAST;
1450 sksg->next_desc_ptr = 0;
1451 }
1452
1453 if (unlikely(skdev->dbg_level > 1)) {
1454 u32 i;
1455
Bart Van Asschef98806d2017-08-17 13:12:58 -07001456 dev_dbg(&skdev->pdev->dev,
1457 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
1458 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001459 for (i = 0; i < skreq->n_sg; i++) {
1460 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1461
Bart Van Asschef98806d2017-08-17 13:12:58 -07001462 dev_dbg(&skdev->pdev->dev,
1463 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1464 i, sgd->byte_count, sgd->control,
1465 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001466 }
1467 }
1468
1469 return 0;
1470}
1471
1472static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1473 struct skd_sg_io *sksgio)
1474{
1475 struct skd_special_context *skspcl = sksgio->skspcl;
1476 struct skd_request_context *skreq = &skspcl->req;
1477 u32 dxfer_len = sksgio->dxfer_len;
1478 int rc;
1479
1480 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1481 /*
1482 * Eventually, errors or not, skd_release_special() is called
1483 * to recover allocations including partial allocations.
1484 */
1485 return rc;
1486}
1487
1488static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1489 struct skd_sg_io *sksgio, int dxfer_dir)
1490{
1491 struct skd_special_context *skspcl = sksgio->skspcl;
1492 u32 iov_ix = 0;
1493 struct sg_iovec curiov;
1494 u32 sksg_ix = 0;
1495 u8 *bufp = NULL;
1496 u32 buf_len = 0;
1497 u32 resid = sksgio->dxfer_len;
1498 int rc;
1499
1500 curiov.iov_len = 0;
1501 curiov.iov_base = NULL;
1502
1503 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1504 if (dxfer_dir != SG_DXFER_TO_DEV ||
1505 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1506 return 0;
1507 }
1508
1509 while (resid > 0) {
1510 u32 nbytes = PAGE_SIZE;
1511
1512 if (curiov.iov_len == 0) {
1513 curiov = sksgio->iov[iov_ix++];
1514 continue;
1515 }
1516
1517 if (buf_len == 0) {
1518 struct page *page;
1519 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1520 bufp = page_address(page);
1521 buf_len = PAGE_SIZE;
1522 }
1523
1524 nbytes = min_t(u32, nbytes, resid);
1525 nbytes = min_t(u32, nbytes, curiov.iov_len);
1526 nbytes = min_t(u32, nbytes, buf_len);
1527
1528 if (dxfer_dir == SG_DXFER_TO_DEV)
1529 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1530 else
1531 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1532
1533 if (rc)
1534 return -EFAULT;
1535
1536 resid -= nbytes;
1537 curiov.iov_len -= nbytes;
1538 curiov.iov_base += nbytes;
1539 buf_len -= nbytes;
1540 }
1541
1542 return 0;
1543}
1544
1545static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1546 struct skd_sg_io *sksgio)
1547{
1548 struct skd_special_context *skspcl = sksgio->skspcl;
Bart Van Assched891fe62017-08-17 13:13:07 -07001549 struct fit_msg_hdr *fmh = &skspcl->msg_buf->fmh;
1550 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001551
1552 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1553
1554 /* Initialize the FIT msg header */
1555 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1556 fmh->num_protocol_cmds_coalesced = 1;
1557
1558 /* Initialize the SCSI request */
1559 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1560 scsi_req->hdr.sg_list_dma_address =
1561 cpu_to_be64(skspcl->req.sksg_dma_address);
1562 scsi_req->hdr.tag = skspcl->req.id;
1563 scsi_req->hdr.sg_list_len_bytes =
1564 cpu_to_be32(skspcl->req.sg_byte_count);
1565 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1566
1567 skspcl->req.state = SKD_REQ_STATE_BUSY;
1568 skd_send_special_fitmsg(skdev, skspcl);
1569
1570 return 0;
1571}
1572
1573static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1574{
1575 unsigned long flags;
1576 int rc;
1577
1578 rc = wait_event_interruptible_timeout(skdev->waitq,
1579 (sksgio->skspcl->req.state !=
1580 SKD_REQ_STATE_BUSY),
1581 msecs_to_jiffies(sksgio->sg.
1582 timeout));
1583
1584 spin_lock_irqsave(&skdev->lock, flags);
1585
1586 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001587 dev_dbg(&skdev->pdev->dev, "skspcl %p aborted\n",
1588 sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001589
1590 /* Build check cond, sense and let command finish. */
1591 /* For a timeout, we must fabricate completion and sense
1592 * data to complete the command */
1593 sksgio->skspcl->req.completion.status =
1594 SAM_STAT_CHECK_CONDITION;
1595
1596 memset(&sksgio->skspcl->req.err_info, 0,
1597 sizeof(sksgio->skspcl->req.err_info));
1598 sksgio->skspcl->req.err_info.type = 0x70;
1599 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1600 sksgio->skspcl->req.err_info.code = 0x44;
1601 sksgio->skspcl->req.err_info.qual = 0;
1602 rc = 0;
1603 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1604 /* No longer on the adapter. We finish. */
1605 rc = 0;
1606 else {
1607 /* Something's gone wrong. Still busy. Timeout or
1608 * user interrupted (control-C). Mark as an orphan
1609 * so it will be disposed when completed. */
1610 sksgio->skspcl->orphaned = 1;
1611 sksgio->skspcl = NULL;
1612 if (rc == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001613 dev_dbg(&skdev->pdev->dev, "timed out %p (%u ms)\n",
1614 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001615 rc = -ETIMEDOUT;
1616 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001617 dev_dbg(&skdev->pdev->dev, "cntlc %p\n", sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001618 rc = -EINTR;
1619 }
1620 }
1621
1622 spin_unlock_irqrestore(&skdev->lock, flags);
1623
1624 return rc;
1625}
1626
1627static int skd_sg_io_put_status(struct skd_device *skdev,
1628 struct skd_sg_io *sksgio)
1629{
1630 struct sg_io_hdr *sgp = &sksgio->sg;
1631 struct skd_special_context *skspcl = sksgio->skspcl;
1632 int resid = 0;
1633
1634 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1635
1636 sgp->status = skspcl->req.completion.status;
1637 resid = sksgio->dxfer_len - nb;
1638
1639 sgp->masked_status = sgp->status & STATUS_MASK;
1640 sgp->msg_status = 0;
1641 sgp->host_status = 0;
1642 sgp->driver_status = 0;
1643 sgp->resid = resid;
1644 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1645 sgp->info |= SG_INFO_CHECK;
1646
Bart Van Asschef98806d2017-08-17 13:12:58 -07001647 dev_dbg(&skdev->pdev->dev, "status %x masked %x resid 0x%x\n",
1648 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001649
1650 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1651 if (sgp->mx_sb_len > 0) {
1652 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1653 u32 nbytes = sizeof(*ei);
1654
1655 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1656
1657 sgp->sb_len_wr = nbytes;
1658
1659 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001660 dev_dbg(&skdev->pdev->dev,
1661 "copy_to_user sense failed %p\n",
1662 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001663 return -EFAULT;
1664 }
1665 }
1666 }
1667
1668 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001669 dev_dbg(&skdev->pdev->dev, "copy_to_user sg failed %p\n",
1670 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001671 return -EFAULT;
1672 }
1673
1674 return 0;
1675}
1676
1677static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1678 struct skd_sg_io *sksgio)
1679{
1680 struct skd_special_context *skspcl = sksgio->skspcl;
1681
1682 if (skspcl != NULL) {
1683 ulong flags;
1684
1685 sksgio->skspcl = NULL;
1686
1687 spin_lock_irqsave(&skdev->lock, flags);
1688 skd_release_special(skdev, skspcl);
1689 spin_unlock_irqrestore(&skdev->lock, flags);
1690 }
1691
1692 return 0;
1693}
1694
1695/*
1696 *****************************************************************************
1697 * INTERNAL REQUESTS -- generated by driver itself
1698 *****************************************************************************
1699 */
1700
1701static int skd_format_internal_skspcl(struct skd_device *skdev)
1702{
1703 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1704 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1705 struct fit_msg_hdr *fmh;
1706 uint64_t dma_address;
1707 struct skd_scsi_request *scsi;
1708
Bart Van Assched891fe62017-08-17 13:13:07 -07001709 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001710 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1711 fmh->num_protocol_cmds_coalesced = 1;
1712
Bart Van Assched891fe62017-08-17 13:13:07 -07001713 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001714 memset(scsi, 0, sizeof(*scsi));
1715 dma_address = skspcl->req.sksg_dma_address;
1716 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1717 sgd->control = FIT_SGD_CONTROL_LAST;
1718 sgd->byte_count = 0;
1719 sgd->host_side_addr = skspcl->db_dma_address;
1720 sgd->dev_side_addr = 0;
1721 sgd->next_desc_ptr = 0LL;
1722
1723 return 1;
1724}
1725
1726#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1727
1728static void skd_send_internal_skspcl(struct skd_device *skdev,
1729 struct skd_special_context *skspcl,
1730 u8 opcode)
1731{
1732 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1733 struct skd_scsi_request *scsi;
1734 unsigned char *buf = skspcl->data_buf;
1735 int i;
1736
1737 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1738 /*
1739 * A refresh is already in progress.
1740 * Just wait for it to finish.
1741 */
1742 return;
1743
1744 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1745 skspcl->req.state = SKD_REQ_STATE_BUSY;
1746 skspcl->req.id += SKD_ID_INCR;
1747
Bart Van Assched891fe62017-08-17 13:13:07 -07001748 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001749 scsi->hdr.tag = skspcl->req.id;
1750
1751 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1752
1753 switch (opcode) {
1754 case TEST_UNIT_READY:
1755 scsi->cdb[0] = TEST_UNIT_READY;
1756 sgd->byte_count = 0;
1757 scsi->hdr.sg_list_len_bytes = 0;
1758 break;
1759
1760 case READ_CAPACITY:
1761 scsi->cdb[0] = READ_CAPACITY;
1762 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1763 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1764 break;
1765
1766 case INQUIRY:
1767 scsi->cdb[0] = INQUIRY;
1768 scsi->cdb[1] = 0x01; /* evpd */
1769 scsi->cdb[2] = 0x80; /* serial number page */
1770 scsi->cdb[4] = 0x10;
1771 sgd->byte_count = 16;
1772 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1773 break;
1774
1775 case SYNCHRONIZE_CACHE:
1776 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1777 sgd->byte_count = 0;
1778 scsi->hdr.sg_list_len_bytes = 0;
1779 break;
1780
1781 case WRITE_BUFFER:
1782 scsi->cdb[0] = WRITE_BUFFER;
1783 scsi->cdb[1] = 0x02;
1784 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1785 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1786 sgd->byte_count = WR_BUF_SIZE;
1787 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1788 /* fill incrementing byte pattern */
1789 for (i = 0; i < sgd->byte_count; i++)
1790 buf[i] = i & 0xFF;
1791 break;
1792
1793 case READ_BUFFER:
1794 scsi->cdb[0] = READ_BUFFER;
1795 scsi->cdb[1] = 0x02;
1796 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1797 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1798 sgd->byte_count = WR_BUF_SIZE;
1799 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1800 memset(skspcl->data_buf, 0, sgd->byte_count);
1801 break;
1802
1803 default:
1804 SKD_ASSERT("Don't know what to send");
1805 return;
1806
1807 }
1808 skd_send_special_fitmsg(skdev, skspcl);
1809}
1810
1811static void skd_refresh_device_data(struct skd_device *skdev)
1812{
1813 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1814
1815 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1816}
1817
1818static int skd_chk_read_buf(struct skd_device *skdev,
1819 struct skd_special_context *skspcl)
1820{
1821 unsigned char *buf = skspcl->data_buf;
1822 int i;
1823
1824 /* check for incrementing byte pattern */
1825 for (i = 0; i < WR_BUF_SIZE; i++)
1826 if (buf[i] != (i & 0xFF))
1827 return 1;
1828
1829 return 0;
1830}
1831
1832static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1833 u8 code, u8 qual, u8 fruc)
1834{
1835 /* If the check condition is of special interest, log a message */
1836 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1837 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001838 dev_err(&skdev->pdev->dev,
1839 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1840 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001841 }
1842}
1843
1844static void skd_complete_internal(struct skd_device *skdev,
1845 volatile struct fit_completion_entry_v1
1846 *skcomp,
1847 volatile struct fit_comp_error_info *skerr,
1848 struct skd_special_context *skspcl)
1849{
1850 u8 *buf = skspcl->data_buf;
1851 u8 status;
1852 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001853 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001854
Bart Van Assche760b48c2017-08-17 13:13:00 -07001855 lockdep_assert_held(&skdev->lock);
1856
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001857 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1858
Bart Van Asschef98806d2017-08-17 13:12:58 -07001859 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001860
1861 skspcl->req.completion = *skcomp;
1862 skspcl->req.state = SKD_REQ_STATE_IDLE;
1863 skspcl->req.id += SKD_ID_INCR;
1864
1865 status = skspcl->req.completion.status;
1866
1867 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1868 skerr->qual, skerr->fruc);
1869
1870 switch (scsi->cdb[0]) {
1871 case TEST_UNIT_READY:
1872 if (status == SAM_STAT_GOOD)
1873 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1874 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1875 (skerr->key == MEDIUM_ERROR))
1876 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1877 else {
1878 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001879 dev_dbg(&skdev->pdev->dev,
1880 "TUR failed, don't send anymore state 0x%x\n",
1881 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001882 return;
1883 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001884 dev_dbg(&skdev->pdev->dev,
1885 "**** TUR failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001886 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1887 }
1888 break;
1889
1890 case WRITE_BUFFER:
1891 if (status == SAM_STAT_GOOD)
1892 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1893 else {
1894 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001895 dev_dbg(&skdev->pdev->dev,
1896 "write buffer failed, don't send anymore state 0x%x\n",
1897 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001898 return;
1899 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001900 dev_dbg(&skdev->pdev->dev,
1901 "**** write buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001902 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1903 }
1904 break;
1905
1906 case READ_BUFFER:
1907 if (status == SAM_STAT_GOOD) {
1908 if (skd_chk_read_buf(skdev, skspcl) == 0)
1909 skd_send_internal_skspcl(skdev, skspcl,
1910 READ_CAPACITY);
1911 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001912 dev_err(&skdev->pdev->dev,
1913 "*** W/R Buffer mismatch %d ***\n",
1914 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001915 if (skdev->connect_retries <
1916 SKD_MAX_CONNECT_RETRIES) {
1917 skdev->connect_retries++;
1918 skd_soft_reset(skdev);
1919 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001920 dev_err(&skdev->pdev->dev,
1921 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001922 return;
1923 }
1924 }
1925
1926 } else {
1927 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001928 dev_dbg(&skdev->pdev->dev,
1929 "read buffer failed, don't send anymore state 0x%x\n",
1930 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001931 return;
1932 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001933 dev_dbg(&skdev->pdev->dev,
1934 "**** read buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001935 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1936 }
1937 break;
1938
1939 case READ_CAPACITY:
1940 skdev->read_cap_is_valid = 0;
1941 if (status == SAM_STAT_GOOD) {
1942 skdev->read_cap_last_lba =
1943 (buf[0] << 24) | (buf[1] << 16) |
1944 (buf[2] << 8) | buf[3];
1945 skdev->read_cap_blocksize =
1946 (buf[4] << 24) | (buf[5] << 16) |
1947 (buf[6] << 8) | buf[7];
1948
Bart Van Asschef98806d2017-08-17 13:12:58 -07001949 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1950 skdev->read_cap_last_lba,
1951 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001952
1953 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1954
1955 skdev->read_cap_is_valid = 1;
1956
1957 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1958 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1959 (skerr->key == MEDIUM_ERROR)) {
1960 skdev->read_cap_last_lba = ~0;
1961 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001962 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 -06001963 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1964 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001965 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001966 skd_send_internal_skspcl(skdev, skspcl,
1967 TEST_UNIT_READY);
1968 }
1969 break;
1970
1971 case INQUIRY:
1972 skdev->inquiry_is_valid = 0;
1973 if (status == SAM_STAT_GOOD) {
1974 skdev->inquiry_is_valid = 1;
1975
1976 for (i = 0; i < 12; i++)
1977 skdev->inq_serial_num[i] = buf[i + 4];
1978 skdev->inq_serial_num[12] = 0;
1979 }
1980
1981 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001982 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001983 /* connection is complete */
1984 skdev->connect_retries = 0;
1985 break;
1986
1987 case SYNCHRONIZE_CACHE:
1988 if (status == SAM_STAT_GOOD)
1989 skdev->sync_done = 1;
1990 else
1991 skdev->sync_done = -1;
1992 wake_up_interruptible(&skdev->waitq);
1993 break;
1994
1995 default:
1996 SKD_ASSERT("we didn't send this");
1997 }
1998}
1999
2000/*
2001 *****************************************************************************
2002 * FIT MESSAGES
2003 *****************************************************************************
2004 */
2005
2006static void skd_send_fitmsg(struct skd_device *skdev,
2007 struct skd_fitmsg_context *skmsg)
2008{
2009 u64 qcmd;
2010 struct fit_msg_hdr *fmh;
2011
Bart Van Asschef98806d2017-08-17 13:12:58 -07002012 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
2013 skmsg->mb_dma_address, skdev->in_flight);
Bart Van Assche6507f432017-08-17 13:13:06 -07002014 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002015
2016 qcmd = skmsg->mb_dma_address;
2017 qcmd |= FIT_QCMD_QID_NORMAL;
2018
Bart Van Assched891fe62017-08-17 13:13:07 -07002019 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002020 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2021
2022 if (unlikely(skdev->dbg_level > 1)) {
2023 u8 *bp = (u8 *)skmsg->msg_buf;
2024 int i;
2025 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002026 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
2027 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002028 if (i == 0)
2029 i = 64 - 8;
2030 }
2031 }
2032
2033 if (skmsg->length > 256)
2034 qcmd |= FIT_QCMD_MSGSIZE_512;
2035 else if (skmsg->length > 128)
2036 qcmd |= FIT_QCMD_MSGSIZE_256;
2037 else if (skmsg->length > 64)
2038 qcmd |= FIT_QCMD_MSGSIZE_128;
2039 else
2040 /*
2041 * This makes no sense because the FIT msg header is
2042 * 64 bytes. If the msg is only 64 bytes long it has
2043 * no payload.
2044 */
2045 qcmd |= FIT_QCMD_MSGSIZE_64;
2046
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002047 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2048 smp_wmb();
2049
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002050 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002051}
2052
2053static void skd_send_special_fitmsg(struct skd_device *skdev,
2054 struct skd_special_context *skspcl)
2055{
2056 u64 qcmd;
2057
2058 if (unlikely(skdev->dbg_level > 1)) {
2059 u8 *bp = (u8 *)skspcl->msg_buf;
2060 int i;
2061
2062 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002063 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
2064 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002065 if (i == 0)
2066 i = 64 - 8;
2067 }
2068
Bart Van Asschef98806d2017-08-17 13:12:58 -07002069 dev_dbg(&skdev->pdev->dev,
2070 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2071 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2072 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002073 for (i = 0; i < skspcl->req.n_sg; i++) {
2074 struct fit_sg_descriptor *sgd =
2075 &skspcl->req.sksg_list[i];
2076
Bart Van Asschef98806d2017-08-17 13:12:58 -07002077 dev_dbg(&skdev->pdev->dev,
2078 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
2079 i, sgd->byte_count, sgd->control,
2080 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002081 }
2082 }
2083
2084 /*
2085 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2086 * and one 64-byte SSDI command.
2087 */
2088 qcmd = skspcl->mb_dma_address;
2089 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2090
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002091 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2092 smp_wmb();
2093
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002094 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2095}
2096
2097/*
2098 *****************************************************************************
2099 * COMPLETION QUEUE
2100 *****************************************************************************
2101 */
2102
2103static void skd_complete_other(struct skd_device *skdev,
2104 volatile struct fit_completion_entry_v1 *skcomp,
2105 volatile struct fit_comp_error_info *skerr);
2106
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002107struct sns_info {
2108 u8 type;
2109 u8 stat;
2110 u8 key;
2111 u8 asc;
2112 u8 ascq;
2113 u8 mask;
2114 enum skd_check_status_action action;
2115};
2116
2117static struct sns_info skd_chkstat_table[] = {
2118 /* Good */
2119 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2120 SKD_CHECK_STATUS_REPORT_GOOD },
2121
2122 /* Smart alerts */
2123 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2124 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2125 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2126 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2127 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2128 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2129
2130 /* Retry (with limits) */
2131 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2132 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2133 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2134 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2135 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2136 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2137 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2138 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2139
2140 /* Busy (or about to be) */
2141 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2142 SKD_CHECK_STATUS_BUSY_IMMINENT },
2143};
2144
2145/*
2146 * Look up status and sense data to decide how to handle the error
2147 * from the device.
2148 * mask says which fields must match e.g., mask=0x18 means check
2149 * type and stat, ignore key, asc, ascq.
2150 */
2151
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002152static enum skd_check_status_action
2153skd_check_status(struct skd_device *skdev,
2154 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002155{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07002156 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002157
Bart Van Asschef98806d2017-08-17 13:12:58 -07002158 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2159 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002160
Bart Van Asschef98806d2017-08-17 13:12:58 -07002161 dev_dbg(&skdev->pdev->dev,
2162 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2163 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
2164 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002165
2166 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07002167 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002168 struct sns_info *sns = &skd_chkstat_table[i];
2169
2170 if (sns->mask & 0x10)
2171 if (skerr->type != sns->type)
2172 continue;
2173
2174 if (sns->mask & 0x08)
2175 if (cmp_status != sns->stat)
2176 continue;
2177
2178 if (sns->mask & 0x04)
2179 if (skerr->key != sns->key)
2180 continue;
2181
2182 if (sns->mask & 0x02)
2183 if (skerr->code != sns->asc)
2184 continue;
2185
2186 if (sns->mask & 0x01)
2187 if (skerr->qual != sns->ascq)
2188 continue;
2189
2190 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002191 dev_err(&skdev->pdev->dev,
2192 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
2193 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002194 }
2195 return sns->action;
2196 }
2197
2198 /* No other match, so nonzero status means error,
2199 * zero status means good
2200 */
2201 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002202 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002203 return SKD_CHECK_STATUS_REPORT_ERROR;
2204 }
2205
Bart Van Asschef98806d2017-08-17 13:12:58 -07002206 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002207 return SKD_CHECK_STATUS_REPORT_GOOD;
2208}
2209
2210static void skd_resolve_req_exception(struct skd_device *skdev,
2211 struct skd_request_context *skreq)
2212{
2213 u8 cmp_status = skreq->completion.status;
2214
2215 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2216 case SKD_CHECK_STATUS_REPORT_GOOD:
2217 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002218 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002219 break;
2220
2221 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2222 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002223 blk_requeue_request(skdev->queue, skreq->req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002224 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002225 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2226 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2227 skd_quiesce_dev(skdev);
2228 break;
2229
2230 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002231 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2232 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002233 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002234 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002235 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07002236 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002237
2238 case SKD_CHECK_STATUS_REPORT_ERROR:
2239 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002240 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002241 break;
2242 }
2243}
2244
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002245/* assume spinlock is already held */
2246static void skd_release_skreq(struct skd_device *skdev,
2247 struct skd_request_context *skreq)
2248{
2249 u32 msg_slot;
2250 struct skd_fitmsg_context *skmsg;
2251
2252 u32 timo_slot;
2253
2254 /*
2255 * Reclaim the FIT msg buffer if this is
2256 * the first of the requests it carried to
2257 * be completed. The FIT msg buffer used to
2258 * send this request cannot be reused until
2259 * we are sure the s1120 card has copied
2260 * it to its memory. The FIT msg might have
2261 * contained several requests. As soon as
2262 * any of them are completed we know that
2263 * the entire FIT msg was transferred.
2264 * Only the first completed request will
2265 * match the FIT msg buffer id. The FIT
2266 * msg buffer id is immediately updated.
2267 * When subsequent requests complete the FIT
2268 * msg buffer id won't match, so we know
2269 * quite cheaply that it is already done.
2270 */
2271 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2272 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2273
2274 skmsg = &skdev->skmsg_table[msg_slot];
2275 if (skmsg->id == skreq->fitmsg_id) {
2276 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2277 SKD_ASSERT(skmsg->outstanding > 0);
2278 skmsg->outstanding--;
2279 if (skmsg->outstanding == 0) {
2280 skmsg->state = SKD_MSG_STATE_IDLE;
2281 skmsg->id += SKD_ID_INCR;
2282 skmsg->next = skdev->skmsg_free_list;
2283 skdev->skmsg_free_list = skmsg;
2284 }
2285 }
2286
2287 /*
2288 * Decrease the number of active requests.
2289 * Also decrements the count in the timeout slot.
2290 */
2291 SKD_ASSERT(skdev->in_flight > 0);
2292 skdev->in_flight -= 1;
2293
2294 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2295 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2296 skdev->timeout_slot[timo_slot] -= 1;
2297
2298 /*
2299 * Reset backpointer
2300 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002301 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002302
2303 /*
2304 * Reclaim the skd_request_context
2305 */
2306 skreq->state = SKD_REQ_STATE_IDLE;
2307 skreq->id += SKD_ID_INCR;
2308 skreq->next = skdev->skreq_free_list;
2309 skdev->skreq_free_list = skreq;
2310}
2311
2312#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2313
2314static void skd_do_inq_page_00(struct skd_device *skdev,
2315 volatile struct fit_completion_entry_v1 *skcomp,
2316 volatile struct fit_comp_error_info *skerr,
2317 uint8_t *cdb, uint8_t *buf)
2318{
2319 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2320
2321 /* Caller requested "supported pages". The driver needs to insert
2322 * its page.
2323 */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002324 dev_dbg(&skdev->pdev->dev,
2325 "skd_do_driver_inquiry: modify supported pages.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002326
2327 /* If the device rejected the request because the CDB was
2328 * improperly formed, then just leave.
2329 */
2330 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2331 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2332 return;
2333
2334 /* Get the amount of space the caller allocated */
2335 max_bytes = (cdb[3] << 8) | cdb[4];
2336
2337 /* Get the number of pages actually returned by the device */
2338 drive_pages = (buf[2] << 8) | buf[3];
2339 drive_bytes = drive_pages + 4;
2340 new_size = drive_pages + 1;
2341
2342 /* Supported pages must be in numerical order, so find where
2343 * the driver page needs to be inserted into the list of
2344 * pages returned by the device.
2345 */
2346 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2347 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2348 return; /* Device using this page code. abort */
2349 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2350 break;
2351 }
2352
2353 if (insert_pt < max_bytes) {
2354 uint16_t u;
2355
2356 /* Shift everything up one byte to make room. */
2357 for (u = new_size + 3; u > insert_pt; u--)
2358 buf[u] = buf[u - 1];
2359 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2360
2361 /* SCSI byte order increment of num_returned_bytes by 1 */
2362 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002363 cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002364 }
2365
2366 /* update page length field to reflect the driver's page too */
2367 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2368 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2369}
2370
2371static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2372{
2373 int pcie_reg;
2374 u16 pci_bus_speed;
2375 u8 pci_lanes;
2376
2377 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2378 if (pcie_reg) {
2379 u16 linksta;
2380 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2381
2382 pci_bus_speed = linksta & 0xF;
2383 pci_lanes = (linksta & 0x3F0) >> 4;
2384 } else {
2385 *speed = STEC_LINK_UNKNOWN;
2386 *width = 0xFF;
2387 return;
2388 }
2389
2390 switch (pci_bus_speed) {
2391 case 1:
2392 *speed = STEC_LINK_2_5GTS;
2393 break;
2394 case 2:
2395 *speed = STEC_LINK_5GTS;
2396 break;
2397 case 3:
2398 *speed = STEC_LINK_8GTS;
2399 break;
2400 default:
2401 *speed = STEC_LINK_UNKNOWN;
2402 break;
2403 }
2404
2405 if (pci_lanes <= 0x20)
2406 *width = pci_lanes;
2407 else
2408 *width = 0xFF;
2409}
2410
2411static void skd_do_inq_page_da(struct skd_device *skdev,
2412 volatile struct fit_completion_entry_v1 *skcomp,
2413 volatile struct fit_comp_error_info *skerr,
2414 uint8_t *cdb, uint8_t *buf)
2415{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002416 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002417 unsigned max_bytes;
2418 struct driver_inquiry_data inq;
2419 u16 val;
2420
Bart Van Asschef98806d2017-08-17 13:12:58 -07002421 dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002422
2423 memset(&inq, 0, sizeof(inq));
2424
2425 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2426
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002427 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2428 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2429 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2430 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002431
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002432 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2433 inq.pcie_vendor_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_DEVICE_ID, &val);
2436 inq.pcie_device_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_VENDOR_ID, &val);
2439 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002440
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002441 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2442 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002443
2444 /* Driver version, fixed lenth, padded with spaces on the right */
2445 inq.driver_version_length = sizeof(inq.driver_version);
2446 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2447 memcpy(inq.driver_version, DRV_VER_COMPL,
2448 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2449
2450 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2451
2452 /* Clear the error set by the device */
2453 skcomp->status = SAM_STAT_GOOD;
2454 memset((void *)skerr, 0, sizeof(*skerr));
2455
2456 /* copy response into output buffer */
2457 max_bytes = (cdb[3] << 8) | cdb[4];
2458 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2459
2460 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002461 cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq)));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002462}
2463
2464static void skd_do_driver_inq(struct skd_device *skdev,
2465 volatile struct fit_completion_entry_v1 *skcomp,
2466 volatile struct fit_comp_error_info *skerr,
2467 uint8_t *cdb, uint8_t *buf)
2468{
2469 if (!buf)
2470 return;
2471 else if (cdb[0] != INQUIRY)
2472 return; /* Not an INQUIRY */
2473 else if ((cdb[1] & 1) == 0)
2474 return; /* EVPD not set */
2475 else if (cdb[2] == 0)
2476 /* Need to add driver's page to supported pages list */
2477 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2478 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2479 /* Caller requested driver's page */
2480 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2481}
2482
2483static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2484{
2485 if (!sg)
2486 return NULL;
2487 if (!sg_page(sg))
2488 return NULL;
2489 return sg_virt(sg);
2490}
2491
2492static void skd_process_scsi_inq(struct skd_device *skdev,
2493 volatile struct fit_completion_entry_v1
2494 *skcomp,
2495 volatile struct fit_comp_error_info *skerr,
2496 struct skd_special_context *skspcl)
2497{
2498 uint8_t *buf;
Bart Van Assched891fe62017-08-17 13:13:07 -07002499 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002500
2501 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07002502 skspcl->req.data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002503 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2504
2505 if (buf)
2506 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2507}
2508
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002509static int skd_isr_completion_posted(struct skd_device *skdev,
2510 int limit, int *enqueued)
2511{
Bart Van Asschec830da82017-08-17 13:13:13 -07002512 volatile struct fit_completion_entry_v1 *skcmp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002513 volatile struct fit_comp_error_info *skerr;
2514 u16 req_id;
2515 u32 req_slot;
2516 struct skd_request_context *skreq;
Bart Van Asschec830da82017-08-17 13:13:13 -07002517 u16 cmp_cntxt;
2518 u8 cmp_status;
2519 u8 cmp_cycle;
2520 u32 cmp_bytes;
2521 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002522 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002523
Bart Van Assche760b48c2017-08-17 13:13:00 -07002524 lockdep_assert_held(&skdev->lock);
2525
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002526 for (;; ) {
2527 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2528
2529 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2530 cmp_cycle = skcmp->cycle;
2531 cmp_cntxt = skcmp->tag;
2532 cmp_status = skcmp->status;
2533 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2534
2535 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2536
Bart Van Asschef98806d2017-08-17 13:12:58 -07002537 dev_dbg(&skdev->pdev->dev,
2538 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
2539 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
2540 cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
2541 skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002542
2543 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002544 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002545 break;
2546 }
2547 /*
2548 * Update the completion queue head index and possibly
2549 * the completion cycle count. 8-bit wrap-around.
2550 */
2551 skdev->skcomp_ix++;
2552 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2553 skdev->skcomp_ix = 0;
2554 skdev->skcomp_cycle++;
2555 }
2556
2557 /*
2558 * The command context is a unique 32-bit ID. The low order
2559 * bits help locate the request. The request is usually a
2560 * r/w request (see skd_start() above) or a special request.
2561 */
2562 req_id = cmp_cntxt;
2563 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2564
2565 /* Is this other than a r/w request? */
2566 if (req_slot >= skdev->num_req_context) {
2567 /*
2568 * This is not a completion for a r/w request.
2569 */
2570 skd_complete_other(skdev, skcmp, skerr);
2571 continue;
2572 }
2573
2574 skreq = &skdev->skreq_table[req_slot];
2575
2576 /*
2577 * Make sure the request ID for the slot matches.
2578 */
2579 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002580 dev_dbg(&skdev->pdev->dev,
2581 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
2582 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002583 {
2584 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002585 dev_err(&skdev->pdev->dev,
2586 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2587 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002588
2589 continue;
2590 }
2591 }
2592
2593 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2594
2595 if (skreq->state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002596 dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n",
2597 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002598 /* a previously timed out command can
2599 * now be cleaned up */
2600 skd_release_skreq(skdev, skreq);
2601 continue;
2602 }
2603
2604 skreq->completion = *skcmp;
2605 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2606 skreq->err_info = *skerr;
2607 skd_log_check_status(skdev, cmp_status, skerr->key,
2608 skerr->code, skerr->qual,
2609 skerr->fruc);
2610 }
2611 /* Release DMA resources for the request. */
2612 if (skreq->n_sg > 0)
2613 skd_postop_sg_list(skdev, skreq);
2614
Jens Axboefcd37eb2013-11-01 10:14:56 -06002615 if (!skreq->req) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002616 dev_dbg(&skdev->pdev->dev,
2617 "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
2618 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002619 } else {
2620 /*
2621 * Capture the outcome and post it back to the
2622 * native request.
2623 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002624 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002625 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002626 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002627 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002628 }
2629
2630 /*
2631 * Release the skreq, its FIT msg (if one), timeout slot,
2632 * and queue depth.
2633 */
2634 skd_release_skreq(skdev, skreq);
2635
2636 /* skd_isr_comp_limit equal zero means no limit */
2637 if (limit) {
2638 if (++processed >= limit) {
2639 rc = 1;
2640 break;
2641 }
2642 }
2643 }
2644
2645 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2646 && (skdev->in_flight) == 0) {
2647 skdev->state = SKD_DRVR_STATE_PAUSED;
2648 wake_up_interruptible(&skdev->waitq);
2649 }
2650
2651 return rc;
2652}
2653
2654static void skd_complete_other(struct skd_device *skdev,
2655 volatile struct fit_completion_entry_v1 *skcomp,
2656 volatile struct fit_comp_error_info *skerr)
2657{
2658 u32 req_id = 0;
2659 u32 req_table;
2660 u32 req_slot;
2661 struct skd_special_context *skspcl;
2662
Bart Van Assche760b48c2017-08-17 13:13:00 -07002663 lockdep_assert_held(&skdev->lock);
2664
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002665 req_id = skcomp->tag;
2666 req_table = req_id & SKD_ID_TABLE_MASK;
2667 req_slot = req_id & SKD_ID_SLOT_MASK;
2668
Bart Van Asschef98806d2017-08-17 13:12:58 -07002669 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
2670 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002671
2672 /*
2673 * Based on the request id, determine how to dispatch this completion.
2674 * This swich/case is finding the good cases and forwarding the
2675 * completion entry. Errors are reported below the switch.
2676 */
2677 switch (req_table) {
2678 case SKD_ID_RW_REQUEST:
2679 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07002680 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002681 * handles r/w requests. The only way we get here
2682 * is if the req_slot is out of bounds.
2683 */
2684 break;
2685
2686 case SKD_ID_SPECIAL_REQUEST:
2687 /*
2688 * Make sure the req_slot is in bounds and that the id
2689 * matches.
2690 */
2691 if (req_slot < skdev->n_special) {
2692 skspcl = &skdev->skspcl_table[req_slot];
2693 if (skspcl->req.id == req_id &&
2694 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2695 skd_complete_special(skdev,
2696 skcomp, skerr, skspcl);
2697 return;
2698 }
2699 }
2700 break;
2701
2702 case SKD_ID_INTERNAL:
2703 if (req_slot == 0) {
2704 skspcl = &skdev->internal_skspcl;
2705 if (skspcl->req.id == req_id &&
2706 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2707 skd_complete_internal(skdev,
2708 skcomp, skerr, skspcl);
2709 return;
2710 }
2711 }
2712 break;
2713
2714 case SKD_ID_FIT_MSG:
2715 /*
2716 * These id's should never appear in a completion record.
2717 */
2718 break;
2719
2720 default:
2721 /*
2722 * These id's should never appear anywhere;
2723 */
2724 break;
2725 }
2726
2727 /*
2728 * If we get here it is a bad or stale id.
2729 */
2730}
2731
2732static void skd_complete_special(struct skd_device *skdev,
2733 volatile struct fit_completion_entry_v1
2734 *skcomp,
2735 volatile struct fit_comp_error_info *skerr,
2736 struct skd_special_context *skspcl)
2737{
Bart Van Assche760b48c2017-08-17 13:13:00 -07002738 lockdep_assert_held(&skdev->lock);
2739
Bart Van Asschef98806d2017-08-17 13:12:58 -07002740 dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002741 if (skspcl->orphaned) {
2742 /* Discard orphaned request */
2743 /* ?: Can this release directly or does it need
2744 * to use a worker? */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002745 dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002746 skd_release_special(skdev, skspcl);
2747 return;
2748 }
2749
2750 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2751
2752 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2753 skspcl->req.completion = *skcomp;
2754 skspcl->req.err_info = *skerr;
2755
2756 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2757 skerr->code, skerr->qual, skerr->fruc);
2758
2759 wake_up_interruptible(&skdev->waitq);
2760}
2761
2762/* assume spinlock is already held */
2763static void skd_release_special(struct skd_device *skdev,
2764 struct skd_special_context *skspcl)
2765{
2766 int i, was_depleted;
2767
2768 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002769 struct page *page = sg_page(&skspcl->req.sg[i]);
2770 __free_page(page);
2771 }
2772
2773 was_depleted = (skdev->skspcl_free_list == NULL);
2774
2775 skspcl->req.state = SKD_REQ_STATE_IDLE;
2776 skspcl->req.id += SKD_ID_INCR;
2777 skspcl->req.next =
2778 (struct skd_request_context *)skdev->skspcl_free_list;
2779 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2780
2781 if (was_depleted) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002782 dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002783 /* Free list was depleted. Their might be waiters. */
2784 wake_up_interruptible(&skdev->waitq);
2785 }
2786}
2787
2788static void skd_reset_skcomp(struct skd_device *skdev)
2789{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002790 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002791
2792 skdev->skcomp_ix = 0;
2793 skdev->skcomp_cycle = 1;
2794}
2795
2796/*
2797 *****************************************************************************
2798 * INTERRUPTS
2799 *****************************************************************************
2800 */
2801static void skd_completion_worker(struct work_struct *work)
2802{
2803 struct skd_device *skdev =
2804 container_of(work, struct skd_device, completion_worker);
2805 unsigned long flags;
2806 int flush_enqueued = 0;
2807
2808 spin_lock_irqsave(&skdev->lock, flags);
2809
2810 /*
2811 * pass in limit=0, which means no limit..
2812 * process everything in compq
2813 */
2814 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2815 skd_request_fn(skdev->queue);
2816
2817 spin_unlock_irqrestore(&skdev->lock, flags);
2818}
2819
2820static void skd_isr_msg_from_dev(struct skd_device *skdev);
2821
Arnd Bergmann41c94992016-11-09 13:55:35 +01002822static irqreturn_t
2823skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002824{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07002825 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002826 u32 intstat;
2827 u32 ack;
2828 int rc = 0;
2829 int deferred = 0;
2830 int flush_enqueued = 0;
2831
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002832 spin_lock(&skdev->lock);
2833
2834 for (;; ) {
2835 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2836
2837 ack = FIT_INT_DEF_MASK;
2838 ack &= intstat;
2839
Bart Van Asschef98806d2017-08-17 13:12:58 -07002840 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
2841 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002842
2843 /* As long as there is an int pending on device, keep
2844 * running loop. When none, get out, but if we've never
2845 * done any processing, call completion handler?
2846 */
2847 if (ack == 0) {
2848 /* No interrupts on device, but run the completion
2849 * processor anyway?
2850 */
2851 if (rc == 0)
2852 if (likely (skdev->state
2853 == SKD_DRVR_STATE_ONLINE))
2854 deferred = 1;
2855 break;
2856 }
2857
2858 rc = IRQ_HANDLED;
2859
2860 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2861
2862 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2863 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2864 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2865 /*
2866 * If we have already deferred completion
2867 * processing, don't bother running it again
2868 */
2869 if (deferred == 0)
2870 deferred =
2871 skd_isr_completion_posted(skdev,
2872 skd_isr_comp_limit, &flush_enqueued);
2873 }
2874
2875 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2876 skd_isr_fwstate(skdev);
2877 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2878 skdev->state ==
2879 SKD_DRVR_STATE_DISAPPEARED) {
2880 spin_unlock(&skdev->lock);
2881 return rc;
2882 }
2883 }
2884
2885 if (intstat & FIT_ISH_MSG_FROM_DEV)
2886 skd_isr_msg_from_dev(skdev);
2887 }
2888 }
2889
2890 if (unlikely(flush_enqueued))
2891 skd_request_fn(skdev->queue);
2892
2893 if (deferred)
2894 schedule_work(&skdev->completion_worker);
2895 else if (!flush_enqueued)
2896 skd_request_fn(skdev->queue);
2897
2898 spin_unlock(&skdev->lock);
2899
2900 return rc;
2901}
2902
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002903static void skd_drive_fault(struct skd_device *skdev)
2904{
2905 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002906 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002907}
2908
2909static void skd_drive_disappeared(struct skd_device *skdev)
2910{
2911 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002912 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002913}
2914
2915static void skd_isr_fwstate(struct skd_device *skdev)
2916{
2917 u32 sense;
2918 u32 state;
2919 u32 mtd;
2920 int prev_driver_state = skdev->state;
2921
2922 sense = SKD_READL(skdev, FIT_STATUS);
2923 state = sense & FIT_SR_DRIVE_STATE_MASK;
2924
Bart Van Asschef98806d2017-08-17 13:12:58 -07002925 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
2926 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
2927 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002928
2929 skdev->drive_state = state;
2930
2931 switch (skdev->drive_state) {
2932 case FIT_SR_DRIVE_INIT:
2933 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
2934 skd_disable_interrupts(skdev);
2935 break;
2936 }
2937 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
Bart Van Assche79ce12a2017-08-17 13:13:14 -07002938 skd_recover_requests(skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002939 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2940 skdev->timer_countdown = SKD_STARTING_TIMO;
2941 skdev->state = SKD_DRVR_STATE_STARTING;
2942 skd_soft_reset(skdev);
2943 break;
2944 }
2945 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2946 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2947 skdev->last_mtd = mtd;
2948 break;
2949
2950 case FIT_SR_DRIVE_ONLINE:
2951 skdev->cur_max_queue_depth = skd_max_queue_depth;
2952 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2953 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2954
2955 skdev->queue_low_water_mark =
2956 skdev->cur_max_queue_depth * 2 / 3 + 1;
2957 if (skdev->queue_low_water_mark < 1)
2958 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002959 dev_info(&skdev->pdev->dev,
2960 "Queue depth limit=%d dev=%d lowat=%d\n",
2961 skdev->cur_max_queue_depth,
2962 skdev->dev_max_queue_depth,
2963 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002964
2965 skd_refresh_device_data(skdev);
2966 break;
2967
2968 case FIT_SR_DRIVE_BUSY:
2969 skdev->state = SKD_DRVR_STATE_BUSY;
2970 skdev->timer_countdown = SKD_BUSY_TIMO;
2971 skd_quiesce_dev(skdev);
2972 break;
2973 case FIT_SR_DRIVE_BUSY_SANITIZE:
2974 /* set timer for 3 seconds, we'll abort any unfinished
2975 * commands after that expires
2976 */
2977 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2978 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002979 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002980 break;
2981 case FIT_SR_DRIVE_BUSY_ERASE:
2982 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2983 skdev->timer_countdown = SKD_BUSY_TIMO;
2984 break;
2985 case FIT_SR_DRIVE_OFFLINE:
2986 skdev->state = SKD_DRVR_STATE_IDLE;
2987 break;
2988 case FIT_SR_DRIVE_SOFT_RESET:
2989 switch (skdev->state) {
2990 case SKD_DRVR_STATE_STARTING:
2991 case SKD_DRVR_STATE_RESTARTING:
2992 /* Expected by a caller of skd_soft_reset() */
2993 break;
2994 default:
2995 skdev->state = SKD_DRVR_STATE_RESTARTING;
2996 break;
2997 }
2998 break;
2999 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003000 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003001 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3002 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3003 break;
3004
3005 case FIT_SR_DRIVE_DEGRADED:
3006 case FIT_SR_PCIE_LINK_DOWN:
3007 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3008 break;
3009
3010 case FIT_SR_DRIVE_FAULT:
3011 skd_drive_fault(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003012 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003013 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003014 break;
3015
3016 /* PCIe bus returned all Fs? */
3017 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003018 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
3019 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003020 skd_drive_disappeared(skdev);
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003021 skd_recover_requests(skdev);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003022 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003023 break;
3024 default:
3025 /*
3026 * Uknown FW State. Wait for a state we recognize.
3027 */
3028 break;
3029 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003030 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3031 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3032 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003033}
3034
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003035static void skd_recover_requests(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003036{
3037 int i;
3038
3039 for (i = 0; i < skdev->num_req_context; i++) {
3040 struct skd_request_context *skreq = &skdev->skreq_table[i];
3041
3042 if (skreq->state == SKD_REQ_STATE_BUSY) {
3043 skd_log_skreq(skdev, skreq, "recover");
3044
3045 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003046 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003047
3048 /* Release DMA resources for the request. */
3049 if (skreq->n_sg > 0)
3050 skd_postop_sg_list(skdev, skreq);
3051
Bart Van Assche79ce12a2017-08-17 13:13:14 -07003052 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003053
Jens Axboefcd37eb2013-11-01 10:14:56 -06003054 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003055
3056 skreq->state = SKD_REQ_STATE_IDLE;
3057 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003058 }
3059 if (i > 0)
3060 skreq[-1].next = skreq;
3061 skreq->next = NULL;
3062 }
3063 skdev->skreq_free_list = skdev->skreq_table;
3064
3065 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3066 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3067
3068 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3069 skd_log_skmsg(skdev, skmsg, "salvaged");
3070 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3071 skmsg->state = SKD_MSG_STATE_IDLE;
3072 skmsg->id += SKD_ID_INCR;
3073 }
3074 if (i > 0)
3075 skmsg[-1].next = skmsg;
3076 skmsg->next = NULL;
3077 }
3078 skdev->skmsg_free_list = skdev->skmsg_table;
3079
3080 for (i = 0; i < skdev->n_special; i++) {
3081 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3082
3083 /* If orphaned, reclaim it because it has already been reported
3084 * to the process as an error (it was just waiting for
3085 * a completion that didn't come, and now it will never come)
3086 * If busy, change to a state that will cause it to error
3087 * out in the wait routine and let it do the normal
3088 * reporting and reclaiming
3089 */
3090 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3091 if (skspcl->orphaned) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003092 dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
3093 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003094 skd_release_special(skdev, skspcl);
3095 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003096 dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
3097 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003098 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3099 }
3100 }
3101 }
3102 skdev->skspcl_free_list = skdev->skspcl_table;
3103
3104 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3105 skdev->timeout_slot[i] = 0;
3106
3107 skdev->in_flight = 0;
3108}
3109
3110static void skd_isr_msg_from_dev(struct skd_device *skdev)
3111{
3112 u32 mfd;
3113 u32 mtd;
3114 u32 data;
3115
3116 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3117
Bart Van Asschef98806d2017-08-17 13:12:58 -07003118 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
3119 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003120
3121 /* ignore any mtd that is an ack for something we didn't send */
3122 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3123 return;
3124
3125 switch (FIT_MXD_TYPE(mfd)) {
3126 case FIT_MTD_FITFW_INIT:
3127 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3128
3129 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003130 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
3131 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
3132 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
3133 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003134 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3135 skd_soft_reset(skdev);
3136 break;
3137 }
3138 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3139 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3140 skdev->last_mtd = mtd;
3141 break;
3142
3143 case FIT_MTD_GET_CMDQ_DEPTH:
3144 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3145 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3146 SKD_N_COMPLETION_ENTRY);
3147 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3148 skdev->last_mtd = mtd;
3149 break;
3150
3151 case FIT_MTD_SET_COMPQ_DEPTH:
3152 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3153 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3154 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3155 skdev->last_mtd = mtd;
3156 break;
3157
3158 case FIT_MTD_SET_COMPQ_ADDR:
3159 skd_reset_skcomp(skdev);
3160 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3161 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3162 skdev->last_mtd = mtd;
3163 break;
3164
3165 case FIT_MTD_CMD_LOG_HOST_ID:
3166 skdev->connect_time_stamp = get_seconds();
3167 data = skdev->connect_time_stamp & 0xFFFF;
3168 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3169 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3170 skdev->last_mtd = mtd;
3171 break;
3172
3173 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3174 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3175 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3176 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3177 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3178 skdev->last_mtd = mtd;
3179 break;
3180
3181 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3182 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3183 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3184 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3185 skdev->last_mtd = mtd;
3186
Bart Van Asschef98806d2017-08-17 13:12:58 -07003187 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
3188 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003189 break;
3190
3191 case FIT_MTD_ARM_QUEUE:
3192 skdev->last_mtd = 0;
3193 /*
3194 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3195 */
3196 break;
3197
3198 default:
3199 break;
3200 }
3201}
3202
3203static void skd_disable_interrupts(struct skd_device *skdev)
3204{
3205 u32 sense;
3206
3207 sense = SKD_READL(skdev, FIT_CONTROL);
3208 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3209 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003210 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003211
3212 /* Note that the 1s is written. A 1-bit means
3213 * disable, a 0 means enable.
3214 */
3215 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3216}
3217
3218static void skd_enable_interrupts(struct skd_device *skdev)
3219{
3220 u32 val;
3221
3222 /* unmask interrupts first */
3223 val = FIT_ISH_FW_STATE_CHANGE +
3224 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3225
3226 /* Note that the compliment of mask is written. A 1-bit means
3227 * disable, a 0 means enable. */
3228 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003229 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003230
3231 val = SKD_READL(skdev, FIT_CONTROL);
3232 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003233 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003234 SKD_WRITEL(skdev, val, FIT_CONTROL);
3235}
3236
3237/*
3238 *****************************************************************************
3239 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3240 *****************************************************************************
3241 */
3242
3243static void skd_soft_reset(struct skd_device *skdev)
3244{
3245 u32 val;
3246
3247 val = SKD_READL(skdev, FIT_CONTROL);
3248 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003249 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003250 SKD_WRITEL(skdev, val, FIT_CONTROL);
3251}
3252
3253static void skd_start_device(struct skd_device *skdev)
3254{
3255 unsigned long flags;
3256 u32 sense;
3257 u32 state;
3258
3259 spin_lock_irqsave(&skdev->lock, flags);
3260
3261 /* ack all ghost interrupts */
3262 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3263
3264 sense = SKD_READL(skdev, FIT_STATUS);
3265
Bart Van Asschef98806d2017-08-17 13:12:58 -07003266 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003267
3268 state = sense & FIT_SR_DRIVE_STATE_MASK;
3269 skdev->drive_state = state;
3270 skdev->last_mtd = 0;
3271
3272 skdev->state = SKD_DRVR_STATE_STARTING;
3273 skdev->timer_countdown = SKD_STARTING_TIMO;
3274
3275 skd_enable_interrupts(skdev);
3276
3277 switch (skdev->drive_state) {
3278 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003279 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003280 break;
3281
3282 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003283 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003284 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3285 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3286 break;
3287
3288 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003289 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003290 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3291 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3292 break;
3293
3294 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003295 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003296 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3297 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3298 break;
3299
3300 case FIT_SR_DRIVE_INIT:
3301 case FIT_SR_DRIVE_ONLINE:
3302 skd_soft_reset(skdev);
3303 break;
3304
3305 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003306 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003307 skdev->state = SKD_DRVR_STATE_BUSY;
3308 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3309 break;
3310
3311 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003312 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003313 break;
3314
3315 case FIT_SR_DRIVE_FAULT:
3316 /* Fault state is bad...soft reset won't do it...
3317 * Hard reset, maybe, but does it work on device?
3318 * For now, just fault so the system doesn't hang.
3319 */
3320 skd_drive_fault(skdev);
3321 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003322 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003323 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003324 skdev->gendisk_on = -1;
3325 wake_up_interruptible(&skdev->waitq);
3326 break;
3327
3328 case 0xFF:
3329 /* Most likely the device isn't there or isn't responding
3330 * to the BAR1 addresses. */
3331 skd_drive_disappeared(skdev);
3332 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003333 dev_dbg(&skdev->pdev->dev,
3334 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003335 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003336 skdev->gendisk_on = -1;
3337 wake_up_interruptible(&skdev->waitq);
3338 break;
3339
3340 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003341 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
3342 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003343 break;
3344 }
3345
3346 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003347 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003348
3349 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003350 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003351
3352 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003353 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003354
3355 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003356 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003357
3358 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003359 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003360
3361 spin_unlock_irqrestore(&skdev->lock, flags);
3362}
3363
3364static void skd_stop_device(struct skd_device *skdev)
3365{
3366 unsigned long flags;
3367 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3368 u32 dev_state;
3369 int i;
3370
3371 spin_lock_irqsave(&skdev->lock, flags);
3372
3373 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003374 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003375 goto stop_out;
3376 }
3377
3378 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003379 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003380 goto stop_out;
3381 }
3382
3383 skdev->state = SKD_DRVR_STATE_SYNCING;
3384 skdev->sync_done = 0;
3385
3386 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3387
3388 spin_unlock_irqrestore(&skdev->lock, flags);
3389
3390 wait_event_interruptible_timeout(skdev->waitq,
3391 (skdev->sync_done), (10 * HZ));
3392
3393 spin_lock_irqsave(&skdev->lock, flags);
3394
3395 switch (skdev->sync_done) {
3396 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003397 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003398 break;
3399 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003400 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003401 break;
3402 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003403 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003404 }
3405
3406stop_out:
3407 skdev->state = SKD_DRVR_STATE_STOPPING;
3408 spin_unlock_irqrestore(&skdev->lock, flags);
3409
3410 skd_kill_timer(skdev);
3411
3412 spin_lock_irqsave(&skdev->lock, flags);
3413 skd_disable_interrupts(skdev);
3414
3415 /* ensure all ints on device are cleared */
3416 /* soft reset the device to unload with a clean slate */
3417 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3418 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3419
3420 spin_unlock_irqrestore(&skdev->lock, flags);
3421
3422 /* poll every 100ms, 1 second timeout */
3423 for (i = 0; i < 10; i++) {
3424 dev_state =
3425 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3426 if (dev_state == FIT_SR_DRIVE_INIT)
3427 break;
3428 set_current_state(TASK_INTERRUPTIBLE);
3429 schedule_timeout(msecs_to_jiffies(100));
3430 }
3431
3432 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07003433 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
3434 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003435}
3436
3437/* assume spinlock is held */
3438static void skd_restart_device(struct skd_device *skdev)
3439{
3440 u32 state;
3441
3442 /* ack all ghost interrupts */
3443 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3444
3445 state = SKD_READL(skdev, FIT_STATUS);
3446
Bart Van Asschef98806d2017-08-17 13:12:58 -07003447 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003448
3449 state &= FIT_SR_DRIVE_STATE_MASK;
3450 skdev->drive_state = state;
3451 skdev->last_mtd = 0;
3452
3453 skdev->state = SKD_DRVR_STATE_RESTARTING;
3454 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3455
3456 skd_soft_reset(skdev);
3457}
3458
3459/* assume spinlock is held */
3460static int skd_quiesce_dev(struct skd_device *skdev)
3461{
3462 int rc = 0;
3463
3464 switch (skdev->state) {
3465 case SKD_DRVR_STATE_BUSY:
3466 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003467 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003468 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003469 break;
3470 case SKD_DRVR_STATE_ONLINE:
3471 case SKD_DRVR_STATE_STOPPING:
3472 case SKD_DRVR_STATE_SYNCING:
3473 case SKD_DRVR_STATE_PAUSING:
3474 case SKD_DRVR_STATE_PAUSED:
3475 case SKD_DRVR_STATE_STARTING:
3476 case SKD_DRVR_STATE_RESTARTING:
3477 case SKD_DRVR_STATE_RESUMING:
3478 default:
3479 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003480 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
3481 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003482 }
3483 return rc;
3484}
3485
3486/* assume spinlock is held */
3487static int skd_unquiesce_dev(struct skd_device *skdev)
3488{
3489 int prev_driver_state = skdev->state;
3490
3491 skd_log_skdev(skdev, "unquiesce");
3492 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003493 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003494 return 0;
3495 }
3496 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3497 /*
3498 * If there has been an state change to other than
3499 * ONLINE, we will rely on controller state change
3500 * to come back online and restart the queue.
3501 * The BUSY state means that driver is ready to
3502 * continue normal processing but waiting for controller
3503 * to become available.
3504 */
3505 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003506 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003507 return 0;
3508 }
3509
3510 /*
3511 * Drive has just come online, driver is either in startup,
3512 * paused performing a task, or bust waiting for hardware.
3513 */
3514 switch (skdev->state) {
3515 case SKD_DRVR_STATE_PAUSED:
3516 case SKD_DRVR_STATE_BUSY:
3517 case SKD_DRVR_STATE_BUSY_IMMINENT:
3518 case SKD_DRVR_STATE_BUSY_ERASE:
3519 case SKD_DRVR_STATE_STARTING:
3520 case SKD_DRVR_STATE_RESTARTING:
3521 case SKD_DRVR_STATE_FAULT:
3522 case SKD_DRVR_STATE_IDLE:
3523 case SKD_DRVR_STATE_LOAD:
3524 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003525 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3526 skd_skdev_state_to_str(prev_driver_state),
3527 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3528 skdev->state);
3529 dev_dbg(&skdev->pdev->dev,
3530 "**** device ONLINE...starting block queue\n");
3531 dev_dbg(&skdev->pdev->dev, "starting queue\n");
3532 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003533 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003534 skdev->gendisk_on = 1;
3535 wake_up_interruptible(&skdev->waitq);
3536 break;
3537
3538 case SKD_DRVR_STATE_DISAPPEARED:
3539 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003540 dev_dbg(&skdev->pdev->dev,
3541 "**** driver state %d, not implemented\n",
3542 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003543 return -EBUSY;
3544 }
3545 return 0;
3546}
3547
3548/*
3549 *****************************************************************************
3550 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3551 *****************************************************************************
3552 */
3553
3554static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3555{
3556 struct skd_device *skdev = skd_host_data;
3557 unsigned long flags;
3558
3559 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003560 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3561 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3562 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
3563 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003564 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3565 spin_unlock_irqrestore(&skdev->lock, flags);
3566 return IRQ_HANDLED;
3567}
3568
3569static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3570{
3571 struct skd_device *skdev = skd_host_data;
3572 unsigned long flags;
3573
3574 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003575 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3576 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003577 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3578 skd_isr_fwstate(skdev);
3579 spin_unlock_irqrestore(&skdev->lock, flags);
3580 return IRQ_HANDLED;
3581}
3582
3583static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3584{
3585 struct skd_device *skdev = skd_host_data;
3586 unsigned long flags;
3587 int flush_enqueued = 0;
3588 int deferred;
3589
3590 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003591 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3592 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003593 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3594 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3595 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003596 if (flush_enqueued)
3597 skd_request_fn(skdev->queue);
3598
3599 if (deferred)
3600 schedule_work(&skdev->completion_worker);
3601 else if (!flush_enqueued)
3602 skd_request_fn(skdev->queue);
3603
3604 spin_unlock_irqrestore(&skdev->lock, flags);
3605
3606 return IRQ_HANDLED;
3607}
3608
3609static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3610{
3611 struct skd_device *skdev = skd_host_data;
3612 unsigned long flags;
3613
3614 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003615 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3616 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003617 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3618 skd_isr_msg_from_dev(skdev);
3619 spin_unlock_irqrestore(&skdev->lock, flags);
3620 return IRQ_HANDLED;
3621}
3622
3623static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3624{
3625 struct skd_device *skdev = skd_host_data;
3626 unsigned long flags;
3627
3628 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003629 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3630 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003631 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3632 spin_unlock_irqrestore(&skdev->lock, flags);
3633 return IRQ_HANDLED;
3634}
3635
3636/*
3637 *****************************************************************************
3638 * PCIe MSI/MSI-X SETUP
3639 *****************************************************************************
3640 */
3641
3642struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003643 char isr_name[30];
3644};
3645
3646struct skd_init_msix_entry {
3647 const char *name;
3648 irq_handler_t handler;
3649};
3650
3651#define SKD_MAX_MSIX_COUNT 13
3652#define SKD_MIN_MSIX_COUNT 7
3653#define SKD_BASE_MSIX_IRQ 4
3654
3655static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3656 { "(DMA 0)", skd_reserved_isr },
3657 { "(DMA 1)", skd_reserved_isr },
3658 { "(DMA 2)", skd_reserved_isr },
3659 { "(DMA 3)", skd_reserved_isr },
3660 { "(State Change)", skd_statec_isr },
3661 { "(COMPL_Q)", skd_comp_q },
3662 { "(MSG)", skd_msg_isr },
3663 { "(Reserved)", skd_reserved_isr },
3664 { "(Reserved)", skd_reserved_isr },
3665 { "(Queue Full 0)", skd_qfull_isr },
3666 { "(Queue Full 1)", skd_qfull_isr },
3667 { "(Queue Full 2)", skd_qfull_isr },
3668 { "(Queue Full 3)", skd_qfull_isr },
3669};
3670
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003671static int skd_acquire_msix(struct skd_device *skdev)
3672{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003673 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003674 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003675
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003676 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3677 PCI_IRQ_MSIX);
3678 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003679 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003680 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003681 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003682
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003683 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3684 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003685 if (!skdev->msix_entries) {
3686 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003687 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003688 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003689 }
3690
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003691 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003692 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3693 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3694
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003695 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3696 "%s%d-msix %s", DRV_NAME, skdev->devno,
3697 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003698
3699 rc = devm_request_irq(&skdev->pdev->dev,
3700 pci_irq_vector(skdev->pdev, i),
3701 msix_entries[i].handler, 0,
3702 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003704 dev_err(&skdev->pdev->dev,
3705 "Unable to register(%d) MSI-X handler %d: %s\n",
3706 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003707 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003708 }
3709 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003710
Bart Van Asschef98806d2017-08-17 13:12:58 -07003711 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
3712 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003713 return 0;
3714
3715msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003716 while (--i >= 0)
3717 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003718out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003719 kfree(skdev->msix_entries);
3720 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003721 return rc;
3722}
3723
3724static int skd_acquire_irq(struct skd_device *skdev)
3725{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003726 struct pci_dev *pdev = skdev->pdev;
3727 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003728 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003729
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003730 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003731 rc = skd_acquire_msix(skdev);
3732 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003733 return 0;
3734
Bart Van Asschef98806d2017-08-17 13:12:58 -07003735 dev_err(&skdev->pdev->dev,
3736 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003737 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003738
3739 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3740 skdev->devno);
3741
3742 if (skd_isr_type != SKD_IRQ_LEGACY)
3743 irq_flag |= PCI_IRQ_MSI;
3744 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3745 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003746 dev_err(&skdev->pdev->dev,
3747 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003748 return rc;
3749 }
3750
3751 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3752 pdev->msi_enabled ? 0 : IRQF_SHARED,
3753 skdev->isr_name, skdev);
3754 if (rc) {
3755 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003756 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
3757 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003758 return rc;
3759 }
3760
3761 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003762}
3763
3764static void skd_release_irq(struct skd_device *skdev)
3765{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003766 struct pci_dev *pdev = skdev->pdev;
3767
3768 if (skdev->msix_entries) {
3769 int i;
3770
3771 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3772 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3773 skdev);
3774 }
3775
3776 kfree(skdev->msix_entries);
3777 skdev->msix_entries = NULL;
3778 } else {
3779 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003780 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003781
3782 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003783}
3784
3785/*
3786 *****************************************************************************
3787 * CONSTRUCT
3788 *****************************************************************************
3789 */
3790
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003791static int skd_cons_skcomp(struct skd_device *skdev)
3792{
3793 int rc = 0;
3794 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003795
Bart Van Asschef98806d2017-08-17 13:12:58 -07003796 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003797 "comp pci_alloc, total bytes %zd entries %d\n",
3798 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003799
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003800 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07003801 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003802
3803 if (skcomp == NULL) {
3804 rc = -ENOMEM;
3805 goto err_out;
3806 }
3807
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003808 skdev->skcomp_table = skcomp;
3809 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3810 sizeof(*skcomp) *
3811 SKD_N_COMPLETION_ENTRY);
3812
3813err_out:
3814 return rc;
3815}
3816
3817static int skd_cons_skmsg(struct skd_device *skdev)
3818{
3819 int rc = 0;
3820 u32 i;
3821
Bart Van Asschef98806d2017-08-17 13:12:58 -07003822 dev_dbg(&skdev->pdev->dev,
3823 "skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3824 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
3825 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003826
3827 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3828 *skdev->num_fitmsg_context, GFP_KERNEL);
3829 if (skdev->skmsg_table == NULL) {
3830 rc = -ENOMEM;
3831 goto err_out;
3832 }
3833
3834 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3835 struct skd_fitmsg_context *skmsg;
3836
3837 skmsg = &skdev->skmsg_table[i];
3838
3839 skmsg->id = i + SKD_ID_FIT_MSG;
3840
3841 skmsg->state = SKD_MSG_STATE_IDLE;
3842 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07003843 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003844 &skmsg->mb_dma_address);
3845
3846 if (skmsg->msg_buf == NULL) {
3847 rc = -ENOMEM;
3848 goto err_out;
3849 }
3850
Bart Van Assche6507f432017-08-17 13:13:06 -07003851 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
3852 (FIT_QCMD_ALIGN - 1),
3853 "not aligned: msg_buf %p mb_dma_address %#llx\n",
3854 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003855 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3856
3857 skmsg->next = &skmsg[1];
3858 }
3859
3860 /* Free list is in order starting with the 0th entry. */
3861 skdev->skmsg_table[i - 1].next = NULL;
3862 skdev->skmsg_free_list = skdev->skmsg_table;
3863
3864err_out:
3865 return rc;
3866}
3867
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003868static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
3869 u32 n_sg,
3870 dma_addr_t *ret_dma_addr)
3871{
3872 struct fit_sg_descriptor *sg_list;
3873 u32 nbytes;
3874
3875 nbytes = sizeof(*sg_list) * n_sg;
3876
3877 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
3878
3879 if (sg_list != NULL) {
3880 uint64_t dma_address = *ret_dma_addr;
3881 u32 i;
3882
3883 memset(sg_list, 0, nbytes);
3884
3885 for (i = 0; i < n_sg - 1; i++) {
3886 uint64_t ndp_off;
3887 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
3888
3889 sg_list[i].next_desc_ptr = dma_address + ndp_off;
3890 }
3891 sg_list[i].next_desc_ptr = 0LL;
3892 }
3893
3894 return sg_list;
3895}
3896
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003897static int skd_cons_skreq(struct skd_device *skdev)
3898{
3899 int rc = 0;
3900 u32 i;
3901
Bart Van Asschef98806d2017-08-17 13:12:58 -07003902 dev_dbg(&skdev->pdev->dev,
3903 "skreq_table kzalloc, struct %lu, count %u total %lu\n",
3904 sizeof(struct skd_request_context), skdev->num_req_context,
3905 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003906
3907 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
3908 * skdev->num_req_context, GFP_KERNEL);
3909 if (skdev->skreq_table == NULL) {
3910 rc = -ENOMEM;
3911 goto err_out;
3912 }
3913
Bart Van Asschef98806d2017-08-17 13:12:58 -07003914 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
3915 skdev->sgs_per_request, sizeof(struct scatterlist),
3916 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003917
3918 for (i = 0; i < skdev->num_req_context; i++) {
3919 struct skd_request_context *skreq;
3920
3921 skreq = &skdev->skreq_table[i];
3922
3923 skreq->id = i + SKD_ID_RW_REQUEST;
3924 skreq->state = SKD_REQ_STATE_IDLE;
3925
3926 skreq->sg = kzalloc(sizeof(struct scatterlist) *
3927 skdev->sgs_per_request, GFP_KERNEL);
3928 if (skreq->sg == NULL) {
3929 rc = -ENOMEM;
3930 goto err_out;
3931 }
3932 sg_init_table(skreq->sg, skdev->sgs_per_request);
3933
3934 skreq->sksg_list = skd_cons_sg_list(skdev,
3935 skdev->sgs_per_request,
3936 &skreq->sksg_dma_address);
3937
3938 if (skreq->sksg_list == NULL) {
3939 rc = -ENOMEM;
3940 goto err_out;
3941 }
3942
3943 skreq->next = &skreq[1];
3944 }
3945
3946 /* Free list is in order starting with the 0th entry. */
3947 skdev->skreq_table[i - 1].next = NULL;
3948 skdev->skreq_free_list = skdev->skreq_table;
3949
3950err_out:
3951 return rc;
3952}
3953
3954static int skd_cons_skspcl(struct skd_device *skdev)
3955{
3956 int rc = 0;
3957 u32 i, nbytes;
3958
Bart Van Asschef98806d2017-08-17 13:12:58 -07003959 dev_dbg(&skdev->pdev->dev,
3960 "skspcl_table kzalloc, struct %lu, count %u total %lu\n",
3961 sizeof(struct skd_special_context), skdev->n_special,
3962 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003963
3964 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
3965 * skdev->n_special, GFP_KERNEL);
3966 if (skdev->skspcl_table == NULL) {
3967 rc = -ENOMEM;
3968 goto err_out;
3969 }
3970
3971 for (i = 0; i < skdev->n_special; i++) {
3972 struct skd_special_context *skspcl;
3973
3974 skspcl = &skdev->skspcl_table[i];
3975
3976 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
3977 skspcl->req.state = SKD_REQ_STATE_IDLE;
3978
3979 skspcl->req.next = &skspcl[1].req;
3980
3981 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3982
Joe Perchesa5bbf612014-08-08 14:24:12 -07003983 skspcl->msg_buf =
3984 pci_zalloc_consistent(skdev->pdev, nbytes,
3985 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003986 if (skspcl->msg_buf == NULL) {
3987 rc = -ENOMEM;
3988 goto err_out;
3989 }
3990
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003991 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
3992 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
3993 if (skspcl->req.sg == NULL) {
3994 rc = -ENOMEM;
3995 goto err_out;
3996 }
3997
3998 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
3999 SKD_N_SG_PER_SPECIAL,
4000 &skspcl->req.
4001 sksg_dma_address);
4002 if (skspcl->req.sksg_list == NULL) {
4003 rc = -ENOMEM;
4004 goto err_out;
4005 }
4006 }
4007
4008 /* Free list is in order starting with the 0th entry. */
4009 skdev->skspcl_table[i - 1].req.next = NULL;
4010 skdev->skspcl_free_list = skdev->skspcl_table;
4011
4012 return rc;
4013
4014err_out:
4015 return rc;
4016}
4017
4018static int skd_cons_sksb(struct skd_device *skdev)
4019{
4020 int rc = 0;
4021 struct skd_special_context *skspcl;
4022 u32 nbytes;
4023
4024 skspcl = &skdev->internal_skspcl;
4025
4026 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4027 skspcl->req.state = SKD_REQ_STATE_IDLE;
4028
4029 nbytes = SKD_N_INTERNAL_BYTES;
4030
Joe Perchesa5bbf612014-08-08 14:24:12 -07004031 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4032 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004033 if (skspcl->data_buf == NULL) {
4034 rc = -ENOMEM;
4035 goto err_out;
4036 }
4037
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004038 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004039 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4040 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004041 if (skspcl->msg_buf == NULL) {
4042 rc = -ENOMEM;
4043 goto err_out;
4044 }
4045
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004046 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4047 &skspcl->req.sksg_dma_address);
4048 if (skspcl->req.sksg_list == NULL) {
4049 rc = -ENOMEM;
4050 goto err_out;
4051 }
4052
4053 if (!skd_format_internal_skspcl(skdev)) {
4054 rc = -EINVAL;
4055 goto err_out;
4056 }
4057
4058err_out:
4059 return rc;
4060}
4061
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004062static int skd_cons_disk(struct skd_device *skdev)
4063{
4064 int rc = 0;
4065 struct gendisk *disk;
4066 struct request_queue *q;
4067 unsigned long flags;
4068
4069 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4070 if (!disk) {
4071 rc = -ENOMEM;
4072 goto err_out;
4073 }
4074
4075 skdev->disk = disk;
4076 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4077
4078 disk->major = skdev->major;
4079 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4080 disk->fops = &skd_blockdev_ops;
4081 disk->private_data = skdev;
4082
Jens Axboefcd37eb2013-11-01 10:14:56 -06004083 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004084 if (!q) {
4085 rc = -ENOMEM;
4086 goto err_out;
4087 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004088 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004089
4090 skdev->queue = q;
4091 disk->queue = q;
4092 q->queuedata = skdev;
4093
Jens Axboe6975f732016-03-30 10:11:42 -06004094 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004095 blk_queue_max_segments(q, skdev->sgs_per_request);
4096 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4097
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004098 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004099 blk_queue_io_opt(q, 8192);
4100
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004101 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004102 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004103
4104 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004105 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06004106 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004107 spin_unlock_irqrestore(&skdev->lock, flags);
4108
4109err_out:
4110 return rc;
4111}
4112
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004113#define SKD_N_DEV_TABLE 16u
4114static u32 skd_next_devno;
4115
4116static struct skd_device *skd_construct(struct pci_dev *pdev)
4117{
4118 struct skd_device *skdev;
4119 int blk_major = skd_major;
4120 int rc;
4121
4122 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4123
4124 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004125 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004126 return NULL;
4127 }
4128
4129 skdev->state = SKD_DRVR_STATE_LOAD;
4130 skdev->pdev = pdev;
4131 skdev->devno = skd_next_devno++;
4132 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004133 skdev->dev_max_queue_depth = 0;
4134
4135 skdev->num_req_context = skd_max_queue_depth;
4136 skdev->num_fitmsg_context = skd_max_queue_depth;
4137 skdev->n_special = skd_max_pass_thru;
4138 skdev->cur_max_queue_depth = 1;
4139 skdev->queue_low_water_mark = 1;
4140 skdev->proto_ver = 99;
4141 skdev->sgs_per_request = skd_sgs_per_request;
4142 skdev->dbg_level = skd_dbg_level;
4143
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004144 spin_lock_init(&skdev->lock);
4145
4146 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4147
Bart Van Asschef98806d2017-08-17 13:12:58 -07004148 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004149 rc = skd_cons_skcomp(skdev);
4150 if (rc < 0)
4151 goto err_out;
4152
Bart Van Asschef98806d2017-08-17 13:12:58 -07004153 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004154 rc = skd_cons_skmsg(skdev);
4155 if (rc < 0)
4156 goto err_out;
4157
Bart Van Asschef98806d2017-08-17 13:12:58 -07004158 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004159 rc = skd_cons_skreq(skdev);
4160 if (rc < 0)
4161 goto err_out;
4162
Bart Van Asschef98806d2017-08-17 13:12:58 -07004163 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004164 rc = skd_cons_skspcl(skdev);
4165 if (rc < 0)
4166 goto err_out;
4167
Bart Van Asschef98806d2017-08-17 13:12:58 -07004168 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004169 rc = skd_cons_sksb(skdev);
4170 if (rc < 0)
4171 goto err_out;
4172
Bart Van Asschef98806d2017-08-17 13:12:58 -07004173 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004174 rc = skd_cons_disk(skdev);
4175 if (rc < 0)
4176 goto err_out;
4177
Bart Van Asschef98806d2017-08-17 13:12:58 -07004178 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004179 return skdev;
4180
4181err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07004182 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004183 skd_destruct(skdev);
4184 return NULL;
4185}
4186
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004187/*
4188 *****************************************************************************
4189 * DESTRUCT (FREE)
4190 *****************************************************************************
4191 */
4192
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004193static void skd_free_skcomp(struct skd_device *skdev)
4194{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07004195 if (skdev->skcomp_table)
4196 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004197 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004198
4199 skdev->skcomp_table = NULL;
4200 skdev->cq_dma_address = 0;
4201}
4202
4203static void skd_free_skmsg(struct skd_device *skdev)
4204{
4205 u32 i;
4206
4207 if (skdev->skmsg_table == NULL)
4208 return;
4209
4210 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4211 struct skd_fitmsg_context *skmsg;
4212
4213 skmsg = &skdev->skmsg_table[i];
4214
4215 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004216 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4217 skmsg->msg_buf,
4218 skmsg->mb_dma_address);
4219 }
4220 skmsg->msg_buf = NULL;
4221 skmsg->mb_dma_address = 0;
4222 }
4223
4224 kfree(skdev->skmsg_table);
4225 skdev->skmsg_table = NULL;
4226}
4227
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004228static void skd_free_sg_list(struct skd_device *skdev,
4229 struct fit_sg_descriptor *sg_list,
4230 u32 n_sg, dma_addr_t dma_addr)
4231{
4232 if (sg_list != NULL) {
4233 u32 nbytes;
4234
4235 nbytes = sizeof(*sg_list) * n_sg;
4236
4237 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4238 }
4239}
4240
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004241static void skd_free_skreq(struct skd_device *skdev)
4242{
4243 u32 i;
4244
4245 if (skdev->skreq_table == NULL)
4246 return;
4247
4248 for (i = 0; i < skdev->num_req_context; i++) {
4249 struct skd_request_context *skreq;
4250
4251 skreq = &skdev->skreq_table[i];
4252
4253 skd_free_sg_list(skdev, skreq->sksg_list,
4254 skdev->sgs_per_request,
4255 skreq->sksg_dma_address);
4256
4257 skreq->sksg_list = NULL;
4258 skreq->sksg_dma_address = 0;
4259
4260 kfree(skreq->sg);
4261 }
4262
4263 kfree(skdev->skreq_table);
4264 skdev->skreq_table = NULL;
4265}
4266
4267static void skd_free_skspcl(struct skd_device *skdev)
4268{
4269 u32 i;
4270 u32 nbytes;
4271
4272 if (skdev->skspcl_table == NULL)
4273 return;
4274
4275 for (i = 0; i < skdev->n_special; i++) {
4276 struct skd_special_context *skspcl;
4277
4278 skspcl = &skdev->skspcl_table[i];
4279
4280 if (skspcl->msg_buf != NULL) {
4281 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4282 pci_free_consistent(skdev->pdev, nbytes,
4283 skspcl->msg_buf,
4284 skspcl->mb_dma_address);
4285 }
4286
4287 skspcl->msg_buf = NULL;
4288 skspcl->mb_dma_address = 0;
4289
4290 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4291 SKD_N_SG_PER_SPECIAL,
4292 skspcl->req.sksg_dma_address);
4293
4294 skspcl->req.sksg_list = NULL;
4295 skspcl->req.sksg_dma_address = 0;
4296
4297 kfree(skspcl->req.sg);
4298 }
4299
4300 kfree(skdev->skspcl_table);
4301 skdev->skspcl_table = NULL;
4302}
4303
4304static void skd_free_sksb(struct skd_device *skdev)
4305{
4306 struct skd_special_context *skspcl;
4307 u32 nbytes;
4308
4309 skspcl = &skdev->internal_skspcl;
4310
4311 if (skspcl->data_buf != NULL) {
4312 nbytes = SKD_N_INTERNAL_BYTES;
4313
4314 pci_free_consistent(skdev->pdev, nbytes,
4315 skspcl->data_buf, skspcl->db_dma_address);
4316 }
4317
4318 skspcl->data_buf = NULL;
4319 skspcl->db_dma_address = 0;
4320
4321 if (skspcl->msg_buf != NULL) {
4322 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4323 pci_free_consistent(skdev->pdev, nbytes,
4324 skspcl->msg_buf, skspcl->mb_dma_address);
4325 }
4326
4327 skspcl->msg_buf = NULL;
4328 skspcl->mb_dma_address = 0;
4329
4330 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4331 skspcl->req.sksg_dma_address);
4332
4333 skspcl->req.sksg_list = NULL;
4334 skspcl->req.sksg_dma_address = 0;
4335}
4336
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004337static void skd_free_disk(struct skd_device *skdev)
4338{
4339 struct gendisk *disk = skdev->disk;
4340
Bart Van Assche7277cc62017-08-17 13:12:45 -07004341 if (disk && (disk->flags & GENHD_FL_UP))
4342 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004343
Bart Van Assche7277cc62017-08-17 13:12:45 -07004344 if (skdev->queue) {
4345 blk_cleanup_queue(skdev->queue);
4346 skdev->queue = NULL;
4347 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004348 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004349
4350 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004351 skdev->disk = NULL;
4352}
4353
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004354static void skd_destruct(struct skd_device *skdev)
4355{
4356 if (skdev == NULL)
4357 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004358
Bart Van Asschef98806d2017-08-17 13:12:58 -07004359 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004360 skd_free_disk(skdev);
4361
Bart Van Asschef98806d2017-08-17 13:12:58 -07004362 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004363 skd_free_sksb(skdev);
4364
Bart Van Asschef98806d2017-08-17 13:12:58 -07004365 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004366 skd_free_skspcl(skdev);
4367
Bart Van Asschef98806d2017-08-17 13:12:58 -07004368 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004369 skd_free_skreq(skdev);
4370
Bart Van Asschef98806d2017-08-17 13:12:58 -07004371 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004372 skd_free_skmsg(skdev);
4373
Bart Van Asschef98806d2017-08-17 13:12:58 -07004374 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004375 skd_free_skcomp(skdev);
4376
Bart Van Asschef98806d2017-08-17 13:12:58 -07004377 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004378 kfree(skdev);
4379}
4380
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004381/*
4382 *****************************************************************************
4383 * BLOCK DEVICE (BDEV) GLUE
4384 *****************************************************************************
4385 */
4386
4387static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4388{
4389 struct skd_device *skdev;
4390 u64 capacity;
4391
4392 skdev = bdev->bd_disk->private_data;
4393
Bart Van Asschef98806d2017-08-17 13:12:58 -07004394 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
4395 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004396
4397 if (skdev->read_cap_is_valid) {
4398 capacity = get_capacity(skdev->disk);
4399 geo->heads = 64;
4400 geo->sectors = 255;
4401 geo->cylinders = (capacity) / (255 * 64);
4402
4403 return 0;
4404 }
4405 return -EIO;
4406}
4407
Dan Williams0d52c7562016-06-15 19:44:20 -07004408static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004409{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004410 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07004411 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004412 return 0;
4413}
4414
4415static const struct block_device_operations skd_blockdev_ops = {
4416 .owner = THIS_MODULE,
4417 .ioctl = skd_bdev_ioctl,
4418 .getgeo = skd_bdev_getgeo,
4419};
4420
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004421/*
4422 *****************************************************************************
4423 * PCIe DRIVER GLUE
4424 *****************************************************************************
4425 */
4426
Benoit Taine9baa3c32014-08-08 15:56:03 +02004427static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004428 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4429 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4430 { 0 } /* terminate list */
4431};
4432
4433MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4434
4435static char *skd_pci_info(struct skd_device *skdev, char *str)
4436{
4437 int pcie_reg;
4438
4439 strcpy(str, "PCIe (");
4440 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4441
4442 if (pcie_reg) {
4443
4444 char lwstr[6];
4445 uint16_t pcie_lstat, lspeed, lwidth;
4446
4447 pcie_reg += 0x12;
4448 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4449 lspeed = pcie_lstat & (0xF);
4450 lwidth = (pcie_lstat & 0x3F0) >> 4;
4451
4452 if (lspeed == 1)
4453 strcat(str, "2.5GT/s ");
4454 else if (lspeed == 2)
4455 strcat(str, "5.0GT/s ");
4456 else
4457 strcat(str, "<unknown> ");
4458 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4459 strcat(str, lwstr);
4460 }
4461 return str;
4462}
4463
4464static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4465{
4466 int i;
4467 int rc = 0;
4468 char pci_str[32];
4469 struct skd_device *skdev;
4470
Bart Van Asschef98806d2017-08-17 13:12:58 -07004471 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
4472 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4473 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
4474 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004475
4476 rc = pci_enable_device(pdev);
4477 if (rc)
4478 return rc;
4479 rc = pci_request_regions(pdev, DRV_NAME);
4480 if (rc)
4481 goto err_out;
4482 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4483 if (!rc) {
4484 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004485 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4486 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004487 }
4488 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004489 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004490 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004491 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004492 goto err_out_regions;
4493 }
4494 }
4495
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004496 if (!skd_major) {
4497 rc = register_blkdev(0, DRV_NAME);
4498 if (rc < 0)
4499 goto err_out_regions;
4500 BUG_ON(!rc);
4501 skd_major = rc;
4502 }
4503
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004504 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004505 if (skdev == NULL) {
4506 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004507 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004508 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004509
4510 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004511 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004512
4513 pci_set_master(pdev);
4514 rc = pci_enable_pcie_error_reporting(pdev);
4515 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004516 dev_err(&pdev->dev,
4517 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004518 skdev->pcie_error_reporting_is_enabled = 0;
4519 } else
4520 skdev->pcie_error_reporting_is_enabled = 1;
4521
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004522 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004523
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004524 for (i = 0; i < SKD_MAX_BARS; i++) {
4525 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4526 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4527 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4528 skdev->mem_size[i]);
4529 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004530 dev_err(&pdev->dev,
4531 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004532 rc = -ENODEV;
4533 goto err_out_iounmap;
4534 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004535 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4536 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4537 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004538 }
4539
4540 rc = skd_acquire_irq(skdev);
4541 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004542 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004543 goto err_out_iounmap;
4544 }
4545
4546 rc = skd_start_timer(skdev);
4547 if (rc)
4548 goto err_out_timer;
4549
4550 init_waitqueue_head(&skdev->waitq);
4551
4552 skd_start_device(skdev);
4553
4554 rc = wait_event_interruptible_timeout(skdev->waitq,
4555 (skdev->gendisk_on),
4556 (SKD_START_WAIT_SECONDS * HZ));
4557 if (skdev->gendisk_on > 0) {
4558 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004559 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004560 rc = 0;
4561 } else {
4562 /* we timed out, something is wrong with the device,
4563 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07004564 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
4565 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004566 /* in case of no error; we timeout with ENXIO */
4567 if (!rc)
4568 rc = -ENXIO;
4569 goto err_out_timer;
4570 }
4571
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004572 return rc;
4573
4574err_out_timer:
4575 skd_stop_device(skdev);
4576 skd_release_irq(skdev);
4577
4578err_out_iounmap:
4579 for (i = 0; i < SKD_MAX_BARS; i++)
4580 if (skdev->mem_map[i])
4581 iounmap(skdev->mem_map[i]);
4582
4583 if (skdev->pcie_error_reporting_is_enabled)
4584 pci_disable_pcie_error_reporting(pdev);
4585
4586 skd_destruct(skdev);
4587
4588err_out_regions:
4589 pci_release_regions(pdev);
4590
4591err_out:
4592 pci_disable_device(pdev);
4593 pci_set_drvdata(pdev, NULL);
4594 return rc;
4595}
4596
4597static void skd_pci_remove(struct pci_dev *pdev)
4598{
4599 int i;
4600 struct skd_device *skdev;
4601
4602 skdev = pci_get_drvdata(pdev);
4603 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004604 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004605 return;
4606 }
4607 skd_stop_device(skdev);
4608 skd_release_irq(skdev);
4609
4610 for (i = 0; i < SKD_MAX_BARS; i++)
4611 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004612 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004613
4614 if (skdev->pcie_error_reporting_is_enabled)
4615 pci_disable_pcie_error_reporting(pdev);
4616
4617 skd_destruct(skdev);
4618
4619 pci_release_regions(pdev);
4620 pci_disable_device(pdev);
4621 pci_set_drvdata(pdev, NULL);
4622
4623 return;
4624}
4625
4626static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4627{
4628 int i;
4629 struct skd_device *skdev;
4630
4631 skdev = pci_get_drvdata(pdev);
4632 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004633 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004634 return -EIO;
4635 }
4636
4637 skd_stop_device(skdev);
4638
4639 skd_release_irq(skdev);
4640
4641 for (i = 0; i < SKD_MAX_BARS; i++)
4642 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004643 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004644
4645 if (skdev->pcie_error_reporting_is_enabled)
4646 pci_disable_pcie_error_reporting(pdev);
4647
4648 pci_release_regions(pdev);
4649 pci_save_state(pdev);
4650 pci_disable_device(pdev);
4651 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4652 return 0;
4653}
4654
4655static int skd_pci_resume(struct pci_dev *pdev)
4656{
4657 int i;
4658 int rc = 0;
4659 struct skd_device *skdev;
4660
4661 skdev = pci_get_drvdata(pdev);
4662 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004663 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004664 return -1;
4665 }
4666
4667 pci_set_power_state(pdev, PCI_D0);
4668 pci_enable_wake(pdev, PCI_D0, 0);
4669 pci_restore_state(pdev);
4670
4671 rc = pci_enable_device(pdev);
4672 if (rc)
4673 return rc;
4674 rc = pci_request_regions(pdev, DRV_NAME);
4675 if (rc)
4676 goto err_out;
4677 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4678 if (!rc) {
4679 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4680
Bart Van Asschef98806d2017-08-17 13:12:58 -07004681 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4682 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004683 }
4684 } else {
4685 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4686 if (rc) {
4687
Bart Van Asschef98806d2017-08-17 13:12:58 -07004688 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004689 goto err_out_regions;
4690 }
4691 }
4692
4693 pci_set_master(pdev);
4694 rc = pci_enable_pcie_error_reporting(pdev);
4695 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004696 dev_err(&pdev->dev,
4697 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004698 skdev->pcie_error_reporting_is_enabled = 0;
4699 } else
4700 skdev->pcie_error_reporting_is_enabled = 1;
4701
4702 for (i = 0; i < SKD_MAX_BARS; i++) {
4703
4704 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4705 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4706 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4707 skdev->mem_size[i]);
4708 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004709 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004710 rc = -ENODEV;
4711 goto err_out_iounmap;
4712 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004713 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4714 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4715 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004716 }
4717 rc = skd_acquire_irq(skdev);
4718 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004719 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004720 goto err_out_iounmap;
4721 }
4722
4723 rc = skd_start_timer(skdev);
4724 if (rc)
4725 goto err_out_timer;
4726
4727 init_waitqueue_head(&skdev->waitq);
4728
4729 skd_start_device(skdev);
4730
4731 return rc;
4732
4733err_out_timer:
4734 skd_stop_device(skdev);
4735 skd_release_irq(skdev);
4736
4737err_out_iounmap:
4738 for (i = 0; i < SKD_MAX_BARS; i++)
4739 if (skdev->mem_map[i])
4740 iounmap(skdev->mem_map[i]);
4741
4742 if (skdev->pcie_error_reporting_is_enabled)
4743 pci_disable_pcie_error_reporting(pdev);
4744
4745err_out_regions:
4746 pci_release_regions(pdev);
4747
4748err_out:
4749 pci_disable_device(pdev);
4750 return rc;
4751}
4752
4753static void skd_pci_shutdown(struct pci_dev *pdev)
4754{
4755 struct skd_device *skdev;
4756
Bart Van Asschef98806d2017-08-17 13:12:58 -07004757 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004758
4759 skdev = pci_get_drvdata(pdev);
4760 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004761 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004762 return;
4763 }
4764
Bart Van Asschef98806d2017-08-17 13:12:58 -07004765 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004766 skd_stop_device(skdev);
4767}
4768
4769static struct pci_driver skd_driver = {
4770 .name = DRV_NAME,
4771 .id_table = skd_pci_tbl,
4772 .probe = skd_pci_probe,
4773 .remove = skd_pci_remove,
4774 .suspend = skd_pci_suspend,
4775 .resume = skd_pci_resume,
4776 .shutdown = skd_pci_shutdown,
4777};
4778
4779/*
4780 *****************************************************************************
4781 * LOGGING SUPPORT
4782 *****************************************************************************
4783 */
4784
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004785const char *skd_drive_state_to_str(int state)
4786{
4787 switch (state) {
4788 case FIT_SR_DRIVE_OFFLINE:
4789 return "OFFLINE";
4790 case FIT_SR_DRIVE_INIT:
4791 return "INIT";
4792 case FIT_SR_DRIVE_ONLINE:
4793 return "ONLINE";
4794 case FIT_SR_DRIVE_BUSY:
4795 return "BUSY";
4796 case FIT_SR_DRIVE_FAULT:
4797 return "FAULT";
4798 case FIT_SR_DRIVE_DEGRADED:
4799 return "DEGRADED";
4800 case FIT_SR_PCIE_LINK_DOWN:
4801 return "INK_DOWN";
4802 case FIT_SR_DRIVE_SOFT_RESET:
4803 return "SOFT_RESET";
4804 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4805 return "NEED_FW";
4806 case FIT_SR_DRIVE_INIT_FAULT:
4807 return "INIT_FAULT";
4808 case FIT_SR_DRIVE_BUSY_SANITIZE:
4809 return "BUSY_SANITIZE";
4810 case FIT_SR_DRIVE_BUSY_ERASE:
4811 return "BUSY_ERASE";
4812 case FIT_SR_DRIVE_FW_BOOTING:
4813 return "FW_BOOTING";
4814 default:
4815 return "???";
4816 }
4817}
4818
4819const char *skd_skdev_state_to_str(enum skd_drvr_state state)
4820{
4821 switch (state) {
4822 case SKD_DRVR_STATE_LOAD:
4823 return "LOAD";
4824 case SKD_DRVR_STATE_IDLE:
4825 return "IDLE";
4826 case SKD_DRVR_STATE_BUSY:
4827 return "BUSY";
4828 case SKD_DRVR_STATE_STARTING:
4829 return "STARTING";
4830 case SKD_DRVR_STATE_ONLINE:
4831 return "ONLINE";
4832 case SKD_DRVR_STATE_PAUSING:
4833 return "PAUSING";
4834 case SKD_DRVR_STATE_PAUSED:
4835 return "PAUSED";
4836 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
4837 return "DRAINING_TIMEOUT";
4838 case SKD_DRVR_STATE_RESTARTING:
4839 return "RESTARTING";
4840 case SKD_DRVR_STATE_RESUMING:
4841 return "RESUMING";
4842 case SKD_DRVR_STATE_STOPPING:
4843 return "STOPPING";
4844 case SKD_DRVR_STATE_SYNCING:
4845 return "SYNCING";
4846 case SKD_DRVR_STATE_FAULT:
4847 return "FAULT";
4848 case SKD_DRVR_STATE_DISAPPEARED:
4849 return "DISAPPEARED";
4850 case SKD_DRVR_STATE_BUSY_ERASE:
4851 return "BUSY_ERASE";
4852 case SKD_DRVR_STATE_BUSY_SANITIZE:
4853 return "BUSY_SANITIZE";
4854 case SKD_DRVR_STATE_BUSY_IMMINENT:
4855 return "BUSY_IMMINENT";
4856 case SKD_DRVR_STATE_WAIT_BOOT:
4857 return "WAIT_BOOT";
4858
4859 default:
4860 return "???";
4861 }
4862}
4863
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304864static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004865{
4866 switch (state) {
4867 case SKD_MSG_STATE_IDLE:
4868 return "IDLE";
4869 case SKD_MSG_STATE_BUSY:
4870 return "BUSY";
4871 default:
4872 return "???";
4873 }
4874}
4875
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304876static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004877{
4878 switch (state) {
4879 case SKD_REQ_STATE_IDLE:
4880 return "IDLE";
4881 case SKD_REQ_STATE_SETUP:
4882 return "SETUP";
4883 case SKD_REQ_STATE_BUSY:
4884 return "BUSY";
4885 case SKD_REQ_STATE_COMPLETED:
4886 return "COMPLETED";
4887 case SKD_REQ_STATE_TIMEOUT:
4888 return "TIMEOUT";
4889 case SKD_REQ_STATE_ABORTED:
4890 return "ABORTED";
4891 default:
4892 return "???";
4893 }
4894}
4895
4896static void skd_log_skdev(struct skd_device *skdev, const char *event)
4897{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004898 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
4899 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
4900 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
4901 skd_skdev_state_to_str(skdev->state), skdev->state);
4902 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
4903 skdev->in_flight, skdev->cur_max_queue_depth,
4904 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
4905 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
4906 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004907}
4908
4909static void skd_log_skmsg(struct skd_device *skdev,
4910 struct skd_fitmsg_context *skmsg, const char *event)
4911{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004912 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
4913 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
4914 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
4915 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004916}
4917
4918static void skd_log_skreq(struct skd_device *skdev,
4919 struct skd_request_context *skreq, const char *event)
4920{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004921 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
4922 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
4923 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
4924 skreq->fitmsg_id);
4925 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
Bart Van Asscheb1824ee2017-08-17 13:13:12 -07004926 skreq->timeout_stamp, skreq->data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004927
Jens Axboefcd37eb2013-11-01 10:14:56 -06004928 if (skreq->req != NULL) {
4929 struct request *req = skreq->req;
4930 u32 lba = (u32)blk_rq_pos(req);
4931 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004932
Bart Van Asschef98806d2017-08-17 13:12:58 -07004933 dev_dbg(&skdev->pdev->dev,
4934 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
4935 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06004936 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07004937 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004938}
4939
4940/*
4941 *****************************************************************************
4942 * MODULE GLUE
4943 *****************************************************************************
4944 */
4945
4946static int __init skd_init(void)
4947{
Bart Van Assche16a70532017-08-17 13:13:08 -07004948 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
4949 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
4950 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
4951 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
4952 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07004953 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
4954 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
4955 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07004956
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004957 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
4958
4959 switch (skd_isr_type) {
4960 case SKD_IRQ_LEGACY:
4961 case SKD_IRQ_MSI:
4962 case SKD_IRQ_MSIX:
4963 break;
4964 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004965 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004966 skd_isr_type, SKD_IRQ_DEFAULT);
4967 skd_isr_type = SKD_IRQ_DEFAULT;
4968 }
4969
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004970 if (skd_max_queue_depth < 1 ||
4971 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
4972 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004973 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
4974 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
4975 }
4976
Bart Van Assche2da7b402017-08-17 13:13:01 -07004977 if (skd_max_req_per_msg < 1 ||
4978 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004979 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004980 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
4981 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
4982 }
4983
4984 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004985 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004986 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
4987 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
4988 }
4989
4990 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004991 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004992 skd_dbg_level, 0);
4993 skd_dbg_level = 0;
4994 }
4995
4996 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004997 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004998 skd_isr_comp_limit, 0);
4999 skd_isr_comp_limit = 0;
5000 }
5001
5002 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005003 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005004 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5005 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5006 }
5007
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005008 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005009}
5010
5011static void __exit skd_exit(void)
5012{
5013 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5014
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005015 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005016
5017 if (skd_major)
5018 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005019}
5020
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005021module_init(skd_init);
5022module_exit(skd_exit);