blob: 96d7b43cfcf21b691ac884ebb1478ad2ec4dfa2f [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;
215 u8 sg_data_dir;
216 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};
228#define SKD_DATA_DIR_HOST_TO_CARD 1
229#define SKD_DATA_DIR_CARD_TO_HOST 2
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600230
231struct skd_special_context {
232 struct skd_request_context req;
233
234 u8 orphaned;
235
236 void *data_buf;
237 dma_addr_t db_dma_address;
238
Bart Van Assched891fe62017-08-17 13:13:07 -0700239 struct skd_msg_buf *msg_buf;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600240 dma_addr_t mb_dma_address;
241};
242
243struct skd_sg_io {
244 fmode_t mode;
245 void __user *argp;
246
247 struct sg_io_hdr sg;
248
249 u8 cdb[16];
250
251 u32 dxfer_len;
252 u32 iovcnt;
253 struct sg_iovec *iov;
254 struct sg_iovec no_iov_iov;
255
256 struct skd_special_context *skspcl;
257};
258
259typedef enum skd_irq_type {
260 SKD_IRQ_LEGACY,
261 SKD_IRQ_MSI,
262 SKD_IRQ_MSIX
263} skd_irq_type_t;
264
265#define SKD_MAX_BARS 2
266
267struct skd_device {
268 volatile void __iomem *mem_map[SKD_MAX_BARS];
269 resource_size_t mem_phys[SKD_MAX_BARS];
270 u32 mem_size[SKD_MAX_BARS];
271
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600272 struct skd_msix_entry *msix_entries;
273
274 struct pci_dev *pdev;
275 int pcie_error_reporting_is_enabled;
276
277 spinlock_t lock;
278 struct gendisk *disk;
279 struct request_queue *queue;
280 struct device *class_dev;
281 int gendisk_on;
282 int sync_done;
283
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600284 u32 devno;
285 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600286 char isr_name[30];
287
288 enum skd_drvr_state state;
289 u32 drive_state;
290
291 u32 in_flight;
292 u32 cur_max_queue_depth;
293 u32 queue_low_water_mark;
294 u32 dev_max_queue_depth;
295
296 u32 num_fitmsg_context;
297 u32 num_req_context;
298
299 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
300 u32 timeout_stamp;
301 struct skd_fitmsg_context *skmsg_free_list;
302 struct skd_fitmsg_context *skmsg_table;
303
304 struct skd_request_context *skreq_free_list;
305 struct skd_request_context *skreq_table;
306
307 struct skd_special_context *skspcl_free_list;
308 struct skd_special_context *skspcl_table;
309
310 struct skd_special_context internal_skspcl;
311 u32 read_cap_blocksize;
312 u32 read_cap_last_lba;
313 int read_cap_is_valid;
314 int inquiry_is_valid;
315 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600316
317 u8 skcomp_cycle;
318 u32 skcomp_ix;
319 struct fit_completion_entry_v1 *skcomp_table;
320 struct fit_comp_error_info *skerr_table;
321 dma_addr_t cq_dma_address;
322
323 wait_queue_head_t waitq;
324
325 struct timer_list timer;
326 u32 timer_countdown;
327 u32 timer_substate;
328
329 int n_special;
330 int sgs_per_request;
331 u32 last_mtd;
332
333 u32 proto_ver;
334
335 int dbg_level;
336 u32 connect_time_stamp;
337 int connect_retries;
338#define SKD_MAX_CONNECT_RETRIES 16
339 u32 drive_jiffies;
340
341 u32 timo_slot;
342
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600343 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600344};
345
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600346#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
347#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
348#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
349
350static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
351{
Bart Van Assche14262a42017-08-17 13:12:57 -0700352 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600353
Bart Van Assche14262a42017-08-17 13:12:57 -0700354 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700355 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700356 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600357}
358
359static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
360 u32 offset)
361{
Bart Van Assche14262a42017-08-17 13:12:57 -0700362 writel(val, skdev->mem_map[1] + offset);
363 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700364 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600365}
366
367static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
368 u32 offset)
369{
Bart Van Assche14262a42017-08-17 13:12:57 -0700370 writeq(val, skdev->mem_map[1] + offset);
371 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700372 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
373 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600374}
375
376
377#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
378static int skd_isr_type = SKD_IRQ_DEFAULT;
379
380module_param(skd_isr_type, int, 0444);
381MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
382 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
383
384#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
385static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
386
387module_param(skd_max_req_per_msg, int, 0444);
388MODULE_PARM_DESC(skd_max_req_per_msg,
389 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700390 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600391
392#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
393#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
394static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
395
396module_param(skd_max_queue_depth, int, 0444);
397MODULE_PARM_DESC(skd_max_queue_depth,
398 "Maximum SCSI requests issued to s1120."
399 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
400
401static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
402module_param(skd_sgs_per_request, int, 0444);
403MODULE_PARM_DESC(skd_sgs_per_request,
404 "Maximum SG elements per block request."
405 " (1-4096, default==256)");
406
407static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
408module_param(skd_max_pass_thru, int, 0444);
409MODULE_PARM_DESC(skd_max_pass_thru,
410 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
411
412module_param(skd_dbg_level, int, 0444);
413MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
414
415module_param(skd_isr_comp_limit, int, 0444);
416MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
417
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600418/* Major device number dynamically assigned. */
419static u32 skd_major;
420
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600421static void skd_destruct(struct skd_device *skdev);
422static const struct block_device_operations skd_blockdev_ops;
423static void skd_send_fitmsg(struct skd_device *skdev,
424 struct skd_fitmsg_context *skmsg);
425static void skd_send_special_fitmsg(struct skd_device *skdev,
426 struct skd_special_context *skspcl);
427static void skd_request_fn(struct request_queue *rq);
428static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200429 struct skd_request_context *skreq, blk_status_t status);
430static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600431 struct skd_request_context *skreq);
432static void skd_postop_sg_list(struct skd_device *skdev,
433 struct skd_request_context *skreq);
434
435static void skd_restart_device(struct skd_device *skdev);
436static int skd_quiesce_dev(struct skd_device *skdev);
437static int skd_unquiesce_dev(struct skd_device *skdev);
438static void skd_release_special(struct skd_device *skdev,
439 struct skd_special_context *skspcl);
440static void skd_disable_interrupts(struct skd_device *skdev);
441static void skd_isr_fwstate(struct skd_device *skdev);
442static void skd_recover_requests(struct skd_device *skdev, int requeue);
443static void skd_soft_reset(struct skd_device *skdev);
444
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600445const char *skd_drive_state_to_str(int state);
446const char *skd_skdev_state_to_str(enum skd_drvr_state state);
447static void skd_log_skdev(struct skd_device *skdev, const char *event);
448static void skd_log_skmsg(struct skd_device *skdev,
449 struct skd_fitmsg_context *skmsg, const char *event);
450static void skd_log_skreq(struct skd_device *skdev,
451 struct skd_request_context *skreq, const char *event);
452
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600453/*
454 *****************************************************************************
455 * READ/WRITE REQUESTS
456 *****************************************************************************
457 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600458static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600459{
460 struct request_queue *q = skdev->queue;
461 struct request *req;
462
463 for (;; ) {
464 req = blk_peek_request(q);
465 if (req == NULL)
466 break;
467 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200468 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600469 }
470}
471
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600472static void
473skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
474 int data_dir, unsigned lba,
475 unsigned count)
476{
477 if (data_dir == READ)
478 scsi_req->cdb[0] = 0x28;
479 else
480 scsi_req->cdb[0] = 0x2a;
481
482 scsi_req->cdb[1] = 0;
483 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
484 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
485 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
486 scsi_req->cdb[5] = (lba & 0xff);
487 scsi_req->cdb[6] = 0;
488 scsi_req->cdb[7] = (count & 0xff00) >> 8;
489 scsi_req->cdb[8] = count & 0xff;
490 scsi_req->cdb[9] = 0;
491}
492
493static void
494skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400495 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600496{
497 skreq->flush_cmd = 1;
498
499 scsi_req->cdb[0] = 0x35;
500 scsi_req->cdb[1] = 0;
501 scsi_req->cdb[2] = 0;
502 scsi_req->cdb[3] = 0;
503 scsi_req->cdb[4] = 0;
504 scsi_req->cdb[5] = 0;
505 scsi_req->cdb[6] = 0;
506 scsi_req->cdb[7] = 0;
507 scsi_req->cdb[8] = 0;
508 scsi_req->cdb[9] = 0;
509}
510
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600511static void skd_request_fn_not_online(struct request_queue *q);
512
513static void skd_request_fn(struct request_queue *q)
514{
515 struct skd_device *skdev = q->queuedata;
516 struct skd_fitmsg_context *skmsg = NULL;
517 struct fit_msg_hdr *fmh = NULL;
518 struct skd_request_context *skreq;
519 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600520 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600521 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600522 u32 lba;
523 u32 count;
524 int data_dir;
Bart Van Assche4854afe2017-08-17 13:12:59 -0700525 __be64 be_dmaa;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600526 u64 cmdctxt;
527 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600528 int flush, fua;
529
530 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
531 skd_request_fn_not_online(q);
532 return;
533 }
534
Jens Axboe6a5ec652013-11-01 10:38:45 -0600535 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600536 if (skdev->skmsg_free_list == NULL ||
537 skdev->skreq_free_list == NULL ||
538 skdev->in_flight >= skdev->queue_low_water_mark)
539 /* There is still some kind of shortage */
540 return;
541
Jens Axboe6a5ec652013-11-01 10:38:45 -0600542 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600543 }
544
545 /*
546 * Stop conditions:
547 * - There are no more native requests
548 * - There are already the maximum number of requests in progress
549 * - There are no more skd_request_context entries
550 * - There are no more FIT msg buffers
551 */
552 for (;; ) {
553
554 flush = fua = 0;
555
Jens Axboefcd37eb2013-11-01 10:14:56 -0600556 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600557
Jens Axboefcd37eb2013-11-01 10:14:56 -0600558 /* Are there any native requests to start? */
559 if (req == NULL)
560 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600561
Jens Axboefcd37eb2013-11-01 10:14:56 -0600562 lba = (u32)blk_rq_pos(req);
563 count = blk_rq_sectors(req);
564 data_dir = rq_data_dir(req);
565 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600566
Mike Christie3a5e02c2016-06-05 14:32:23 -0500567 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600568 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600569
Jens Axboefcd37eb2013-11-01 10:14:56 -0600570 if (io_flags & REQ_FUA)
571 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600572
Bart Van Asschef98806d2017-08-17 13:12:58 -0700573 dev_dbg(&skdev->pdev->dev,
574 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
575 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600576
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400577 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600578
579 /* Are too many requets already in progress? */
580 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700581 dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
582 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600583 break;
584 }
585
586 /* Is a skd_request_context available? */
587 skreq = skdev->skreq_free_list;
588 if (skreq == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700589 dev_dbg(&skdev->pdev->dev, "Out of req=%p\n", q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600590 break;
591 }
592 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
593 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
594
595 /* Now we check to see if we can get a fit msg */
596 if (skmsg == NULL) {
597 if (skdev->skmsg_free_list == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700598 dev_dbg(&skdev->pdev->dev, "Out of msg\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600599 break;
600 }
601 }
602
603 skreq->flush_cmd = 0;
604 skreq->n_sg = 0;
605 skreq->sg_byte_count = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600606
607 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400608 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600609 *
610 * At this point we are comitted to either start or reject
611 * the native request. Note that skd_request_context is
612 * available but is still at the head of the free list.
613 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600614 blk_start_request(req);
615 skreq->req = req;
616 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600617
Bart Van Assche19fc85c2017-08-17 13:13:04 -0700618 skreq->sg_data_dir = data_dir == READ ?
619 SKD_DATA_DIR_CARD_TO_HOST : SKD_DATA_DIR_HOST_TO_CARD;
620
621 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
622 dev_dbg(&skdev->pdev->dev, "error Out\n");
623 skd_end_request(skdev, skreq, BLK_STS_RESOURCE);
624 continue;
625 }
626
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600627 /* Either a FIT msg is in progress or we have to start one. */
628 if (skmsg == NULL) {
629 /* Are there any FIT msg buffers available? */
630 skmsg = skdev->skmsg_free_list;
631 if (skmsg == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700632 dev_dbg(&skdev->pdev->dev,
633 "Out of msg skdev=%p\n",
634 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600635 break;
636 }
637 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
638 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
639
640 skdev->skmsg_free_list = skmsg->next;
641
642 skmsg->state = SKD_MSG_STATE_BUSY;
643 skmsg->id += SKD_ID_INCR;
644
645 /* Initialize the FIT msg header */
Bart Van Assched891fe62017-08-17 13:13:07 -0700646 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600647 memset(fmh, 0, sizeof(*fmh));
648 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
649 skmsg->length = sizeof(*fmh);
650 }
651
652 skreq->fitmsg_id = skmsg->id;
653
Bart Van Assched891fe62017-08-17 13:13:07 -0700654 scsi_req =
655 &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
656 memset(scsi_req, 0, sizeof(*scsi_req));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600657
Bart Van Assche4854afe2017-08-17 13:12:59 -0700658 be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600659 cmdctxt = skreq->id + SKD_ID_INCR;
660
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600661 scsi_req->hdr.tag = cmdctxt;
662 scsi_req->hdr.sg_list_dma_address = be_dmaa;
663
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600664 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600665 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
666 SKD_ASSERT(skreq->flush_cmd == 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600667 } else {
668 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
669 }
670
671 if (fua)
672 scsi_req->cdb[1] |= SKD_FUA_NV;
673
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600674 scsi_req->hdr.sg_list_len_bytes =
675 cpu_to_be32(skreq->sg_byte_count);
676
677 /* Complete resource allocations. */
678 skdev->skreq_free_list = skreq->next;
679 skreq->state = SKD_REQ_STATE_BUSY;
680 skreq->id += SKD_ID_INCR;
681
682 skmsg->length += sizeof(struct skd_scsi_request);
683 fmh->num_protocol_cmds_coalesced++;
684
685 /*
686 * Update the active request counts.
687 * Capture the timeout timestamp.
688 */
689 skreq->timeout_stamp = skdev->timeout_stamp;
690 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
691 skdev->timeout_slot[timo_slot]++;
692 skdev->in_flight++;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700693 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
694 skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600695
696 /*
697 * If the FIT msg buffer is full send it.
698 */
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700699 if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600700 skd_send_fitmsg(skdev, skmsg);
701 skmsg = NULL;
702 fmh = NULL;
703 }
704 }
705
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700706 /* If the FIT msg buffer is not empty send what we got. */
707 if (skmsg) {
708 WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced);
709 skd_send_fitmsg(skdev, skmsg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600710 skmsg = NULL;
711 fmh = NULL;
712 }
713
714 /*
715 * If req is non-NULL it means there is something to do but
716 * we are out of a resource.
717 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600718 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600719 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600720}
721
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400722static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200723 struct skd_request_context *skreq, blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600724{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600725 if (unlikely(error)) {
726 struct request *req = skreq->req;
727 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
728 u32 lba = (u32)blk_rq_pos(req);
729 u32 count = blk_rq_sectors(req);
730
Bart Van Asschef98806d2017-08-17 13:12:58 -0700731 dev_err(&skdev->pdev->dev,
732 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
733 count, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600734 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -0700735 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", skreq->id,
736 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600737
738 __blk_end_request_all(skreq->req, error);
739}
740
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200741static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400742 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600743{
744 struct request *req = skreq->req;
745 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
746 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
747 struct scatterlist *sg = &skreq->sg[0];
748 int n_sg;
749 int i;
750
751 skreq->sg_byte_count = 0;
752
753 /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
754 skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */
755
756 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
757 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200758 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600759
760 /*
761 * Map scatterlist to PCI bus addresses.
762 * Note PCI might change the number of entries.
763 */
764 n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
765 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200766 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600767
768 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
769
770 skreq->n_sg = n_sg;
771
772 for (i = 0; i < n_sg; i++) {
773 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
774 u32 cnt = sg_dma_len(&sg[i]);
775 uint64_t dma_addr = sg_dma_address(&sg[i]);
776
777 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
778 sgd->byte_count = cnt;
779 skreq->sg_byte_count += cnt;
780 sgd->host_side_addr = dma_addr;
781 sgd->dev_side_addr = 0;
782 }
783
784 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
785 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
786
787 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700788 dev_dbg(&skdev->pdev->dev,
789 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
790 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600791 for (i = 0; i < n_sg; i++) {
792 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700793
794 dev_dbg(&skdev->pdev->dev,
795 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
796 i, sgd->byte_count, sgd->control,
797 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600798 }
799 }
800
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200801 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600802}
803
Jens Axboefcd37eb2013-11-01 10:14:56 -0600804static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400805 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600806{
807 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
808 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
809
810 /*
811 * restore the next ptr for next IO request so we
812 * don't have to set it every time.
813 */
814 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
815 skreq->sksg_dma_address +
816 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
817 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
818}
819
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600820static void skd_request_fn_not_online(struct request_queue *q)
821{
822 struct skd_device *skdev = q->queuedata;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600823
824 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
825
826 skd_log_skdev(skdev, "req_not_online");
827 switch (skdev->state) {
828 case SKD_DRVR_STATE_PAUSING:
829 case SKD_DRVR_STATE_PAUSED:
830 case SKD_DRVR_STATE_STARTING:
831 case SKD_DRVR_STATE_RESTARTING:
832 case SKD_DRVR_STATE_WAIT_BOOT:
833 /* In case of starting, we haven't started the queue,
834 * so we can't get here... but requests are
835 * possibly hanging out waiting for us because we
836 * reported the dev/skd0 already. They'll wait
837 * forever if connect doesn't complete.
838 * What to do??? delay dev/skd0 ??
839 */
840 case SKD_DRVR_STATE_BUSY:
841 case SKD_DRVR_STATE_BUSY_IMMINENT:
842 case SKD_DRVR_STATE_BUSY_ERASE:
843 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
844 return;
845
846 case SKD_DRVR_STATE_BUSY_SANITIZE:
847 case SKD_DRVR_STATE_STOPPING:
848 case SKD_DRVR_STATE_SYNCING:
849 case SKD_DRVR_STATE_FAULT:
850 case SKD_DRVR_STATE_DISAPPEARED:
851 default:
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600852 break;
853 }
854
855 /* If we get here, terminate all pending block requeusts
856 * with EIO and any scsi pass thru with appropriate sense
857 */
858
859 skd_fail_all_pending(skdev);
860}
861
862/*
863 *****************************************************************************
864 * TIMER
865 *****************************************************************************
866 */
867
868static void skd_timer_tick_not_online(struct skd_device *skdev);
869
870static void skd_timer_tick(ulong arg)
871{
872 struct skd_device *skdev = (struct skd_device *)arg;
873
874 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600875 unsigned long reqflags;
876 u32 state;
877
878 if (skdev->state == SKD_DRVR_STATE_FAULT)
879 /* The driver has declared fault, and we want it to
880 * stay that way until driver is reloaded.
881 */
882 return;
883
884 spin_lock_irqsave(&skdev->lock, reqflags);
885
886 state = SKD_READL(skdev, FIT_STATUS);
887 state &= FIT_SR_DRIVE_STATE_MASK;
888 if (state != skdev->drive_state)
889 skd_isr_fwstate(skdev);
890
891 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
892 skd_timer_tick_not_online(skdev);
893 goto timer_func_out;
894 }
895 skdev->timeout_stamp++;
896 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
897
898 /*
899 * All requests that happened during the previous use of
900 * this slot should be done by now. The previous use was
901 * over 7 seconds ago.
902 */
903 if (skdev->timeout_slot[timo_slot] == 0)
904 goto timer_func_out;
905
906 /* Something is overdue */
Bart Van Asschef98806d2017-08-17 13:12:58 -0700907 dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
908 skdev->timeout_slot[timo_slot], skdev->in_flight);
909 dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
910 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600911
912 skdev->timer_countdown = SKD_DRAINING_TIMO;
913 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
914 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600915 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600916
917timer_func_out:
918 mod_timer(&skdev->timer, (jiffies + HZ));
919
920 spin_unlock_irqrestore(&skdev->lock, reqflags);
921}
922
923static void skd_timer_tick_not_online(struct skd_device *skdev)
924{
925 switch (skdev->state) {
926 case SKD_DRVR_STATE_IDLE:
927 case SKD_DRVR_STATE_LOAD:
928 break;
929 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700930 dev_dbg(&skdev->pdev->dev,
931 "drive busy sanitize[%x], driver[%x]\n",
932 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600933 /* If we've been in sanitize for 3 seconds, we figure we're not
934 * going to get anymore completions, so recover requests now
935 */
936 if (skdev->timer_countdown > 0) {
937 skdev->timer_countdown--;
938 return;
939 }
940 skd_recover_requests(skdev, 0);
941 break;
942
943 case SKD_DRVR_STATE_BUSY:
944 case SKD_DRVR_STATE_BUSY_IMMINENT:
945 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700946 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
947 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600948 if (skdev->timer_countdown > 0) {
949 skdev->timer_countdown--;
950 return;
951 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700952 dev_dbg(&skdev->pdev->dev,
953 "busy[%x], timedout=%d, restarting device.",
954 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600955 skd_restart_device(skdev);
956 break;
957
958 case SKD_DRVR_STATE_WAIT_BOOT:
959 case SKD_DRVR_STATE_STARTING:
960 if (skdev->timer_countdown > 0) {
961 skdev->timer_countdown--;
962 return;
963 }
964 /* For now, we fault the drive. Could attempt resets to
965 * revcover at some point. */
966 skdev->state = SKD_DRVR_STATE_FAULT;
967
Bart Van Asschef98806d2017-08-17 13:12:58 -0700968 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
969 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600970
971 /*start the queue so we can respond with error to requests */
972 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600973 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600974 skdev->gendisk_on = -1;
975 wake_up_interruptible(&skdev->waitq);
976 break;
977
978 case SKD_DRVR_STATE_ONLINE:
979 /* shouldn't get here. */
980 break;
981
982 case SKD_DRVR_STATE_PAUSING:
983 case SKD_DRVR_STATE_PAUSED:
984 break;
985
986 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700987 dev_dbg(&skdev->pdev->dev,
988 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
989 skdev->timo_slot, skdev->timer_countdown,
990 skdev->in_flight,
991 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600992 /* if the slot has cleared we can let the I/O continue */
993 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700994 dev_dbg(&skdev->pdev->dev,
995 "Slot drained, starting queue.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600996 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600997 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600998 return;
999 }
1000 if (skdev->timer_countdown > 0) {
1001 skdev->timer_countdown--;
1002 return;
1003 }
1004 skd_restart_device(skdev);
1005 break;
1006
1007 case SKD_DRVR_STATE_RESTARTING:
1008 if (skdev->timer_countdown > 0) {
1009 skdev->timer_countdown--;
1010 return;
1011 }
1012 /* For now, we fault the drive. Could attempt resets to
1013 * revcover at some point. */
1014 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001015 dev_err(&skdev->pdev->dev,
1016 "DriveFault Reconnect Timeout (%x)\n",
1017 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001018
1019 /*
1020 * Recovering does two things:
1021 * 1. completes IO with error
1022 * 2. reclaims dma resources
1023 * When is it safe to recover requests?
1024 * - if the drive state is faulted
1025 * - if the state is still soft reset after out timeout
1026 * - if the drive registers are dead (state = FF)
1027 * If it is "unsafe", we still need to recover, so we will
1028 * disable pci bus mastering and disable our interrupts.
1029 */
1030
1031 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1032 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1033 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1034 /* It never came out of soft reset. Try to
1035 * recover the requests and then let them
1036 * fail. This is to mitigate hung processes. */
1037 skd_recover_requests(skdev, 0);
1038 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001039 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
1040 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001041 pci_disable_device(skdev->pdev);
1042 skd_disable_interrupts(skdev);
1043 skd_recover_requests(skdev, 0);
1044 }
1045
1046 /*start the queue so we can respond with error to requests */
1047 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001048 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001049 skdev->gendisk_on = -1;
1050 wake_up_interruptible(&skdev->waitq);
1051 break;
1052
1053 case SKD_DRVR_STATE_RESUMING:
1054 case SKD_DRVR_STATE_STOPPING:
1055 case SKD_DRVR_STATE_SYNCING:
1056 case SKD_DRVR_STATE_FAULT:
1057 case SKD_DRVR_STATE_DISAPPEARED:
1058 default:
1059 break;
1060 }
1061}
1062
1063static int skd_start_timer(struct skd_device *skdev)
1064{
1065 int rc;
1066
1067 init_timer(&skdev->timer);
1068 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1069
1070 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1071 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001072 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001073 return rc;
1074}
1075
1076static void skd_kill_timer(struct skd_device *skdev)
1077{
1078 del_timer_sync(&skdev->timer);
1079}
1080
1081/*
1082 *****************************************************************************
1083 * IOCTL
1084 *****************************************************************************
1085 */
1086static int skd_ioctl_sg_io(struct skd_device *skdev,
1087 fmode_t mode, void __user *argp);
1088static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1089 struct skd_sg_io *sksgio);
1090static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1091 struct skd_sg_io *sksgio);
1092static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1093 struct skd_sg_io *sksgio);
1094static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1095 struct skd_sg_io *sksgio, int dxfer_dir);
1096static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1097 struct skd_sg_io *sksgio);
1098static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1099static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1100 struct skd_sg_io *sksgio);
1101static int skd_sg_io_put_status(struct skd_device *skdev,
1102 struct skd_sg_io *sksgio);
1103
1104static void skd_complete_special(struct skd_device *skdev,
1105 volatile struct fit_completion_entry_v1
1106 *skcomp,
1107 volatile struct fit_comp_error_info *skerr,
1108 struct skd_special_context *skspcl);
1109
1110static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1111 uint cmd_in, ulong arg)
1112{
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001113 static const int sg_version_num = 30527;
1114 int rc = 0, timeout;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001115 struct gendisk *disk = bdev->bd_disk;
1116 struct skd_device *skdev = disk->private_data;
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001117 int __user *p = (int __user *)arg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001118
Bart Van Asschef98806d2017-08-17 13:12:58 -07001119 dev_dbg(&skdev->pdev->dev,
1120 "%s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1121 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001122
1123 if (!capable(CAP_SYS_ADMIN))
1124 return -EPERM;
1125
1126 switch (cmd_in) {
1127 case SG_SET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001128 rc = get_user(timeout, p);
1129 if (!rc)
1130 disk->queue->sg_timeout = clock_t_to_jiffies(timeout);
1131 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001132 case SG_GET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001133 rc = jiffies_to_clock_t(disk->queue->sg_timeout);
1134 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001135 case SG_GET_VERSION_NUM:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001136 rc = put_user(sg_version_num, p);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001137 break;
1138 case SG_IO:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001139 rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001140 break;
1141
1142 default:
1143 rc = -ENOTTY;
1144 break;
1145 }
1146
Bart Van Asschef98806d2017-08-17 13:12:58 -07001147 dev_dbg(&skdev->pdev->dev, "%s: completion rc %d\n", disk->disk_name,
1148 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001149 return rc;
1150}
1151
1152static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1153 void __user *argp)
1154{
1155 int rc;
1156 struct skd_sg_io sksgio;
1157
1158 memset(&sksgio, 0, sizeof(sksgio));
1159 sksgio.mode = mode;
1160 sksgio.argp = argp;
1161 sksgio.iov = &sksgio.no_iov_iov;
1162
1163 switch (skdev->state) {
1164 case SKD_DRVR_STATE_ONLINE:
1165 case SKD_DRVR_STATE_BUSY_IMMINENT:
1166 break;
1167
1168 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001169 dev_dbg(&skdev->pdev->dev, "drive not online\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001170 rc = -ENXIO;
1171 goto out;
1172 }
1173
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001174 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1175 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001176 goto out;
1177
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001178 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1179 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001180 goto out;
1181
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001182 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1183 if (rc)
1184 goto out;
1185
1186 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1187 if (rc)
1188 goto out;
1189
1190 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1191 if (rc)
1192 goto out;
1193
1194 rc = skd_sg_io_await(skdev, &sksgio);
1195 if (rc)
1196 goto out;
1197
1198 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1199 if (rc)
1200 goto out;
1201
1202 rc = skd_sg_io_put_status(skdev, &sksgio);
1203 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001204 goto out;
1205
1206 rc = 0;
1207
1208out:
1209 skd_sg_io_release_skspcl(skdev, &sksgio);
1210
1211 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1212 kfree(sksgio.iov);
1213 return rc;
1214}
1215
1216static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1217 struct skd_sg_io *sksgio)
1218{
1219 struct sg_io_hdr *sgp = &sksgio->sg;
Bart Van Assche95895e12017-08-17 13:12:55 -07001220 int i, __maybe_unused acc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001221
1222 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001223 dev_dbg(&skdev->pdev->dev, "access sg failed %p\n",
1224 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001225 return -EFAULT;
1226 }
1227
1228 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001229 dev_dbg(&skdev->pdev->dev, "copy_from_user sg failed %p\n",
1230 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001231 return -EFAULT;
1232 }
1233
1234 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001235 dev_dbg(&skdev->pdev->dev, "interface_id invalid 0x%x\n",
1236 sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001237 return -EINVAL;
1238 }
1239
1240 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001241 dev_dbg(&skdev->pdev->dev, "cmd_len invalid %d\n",
1242 sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001243 return -EINVAL;
1244 }
1245
1246 if (sgp->iovec_count > 256) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001247 dev_dbg(&skdev->pdev->dev, "iovec_count invalid %d\n",
1248 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001249 return -EINVAL;
1250 }
1251
1252 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001253 dev_dbg(&skdev->pdev->dev, "dxfer_len invalid %d\n",
1254 sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001255 return -EINVAL;
1256 }
1257
1258 switch (sgp->dxfer_direction) {
1259 case SG_DXFER_NONE:
1260 acc = -1;
1261 break;
1262
1263 case SG_DXFER_TO_DEV:
1264 acc = VERIFY_READ;
1265 break;
1266
1267 case SG_DXFER_FROM_DEV:
1268 case SG_DXFER_TO_FROM_DEV:
1269 acc = VERIFY_WRITE;
1270 break;
1271
1272 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001273 dev_dbg(&skdev->pdev->dev, "dxfer_dir invalid %d\n",
1274 sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001275 return -EINVAL;
1276 }
1277
1278 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001279 dev_dbg(&skdev->pdev->dev, "copy_from_user cmdp failed %p\n",
1280 sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001281 return -EFAULT;
1282 }
1283
1284 if (sgp->mx_sb_len != 0) {
1285 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001286 dev_dbg(&skdev->pdev->dev, "access sbp failed %p\n",
1287 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001288 return -EFAULT;
1289 }
1290 }
1291
1292 if (sgp->iovec_count == 0) {
1293 sksgio->iov[0].iov_base = sgp->dxferp;
1294 sksgio->iov[0].iov_len = sgp->dxfer_len;
1295 sksgio->iovcnt = 1;
1296 sksgio->dxfer_len = sgp->dxfer_len;
1297 } else {
1298 struct sg_iovec *iov;
1299 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1300 size_t iov_data_len;
1301
1302 iov = kmalloc(nbytes, GFP_KERNEL);
1303 if (iov == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001304 dev_dbg(&skdev->pdev->dev, "alloc iovec failed %d\n",
1305 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001306 return -ENOMEM;
1307 }
1308 sksgio->iov = iov;
1309 sksgio->iovcnt = sgp->iovec_count;
1310
1311 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001312 dev_dbg(&skdev->pdev->dev,
1313 "copy_from_user iovec failed %p\n",
1314 sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001315 return -EFAULT;
1316 }
1317
1318 /*
1319 * Sum up the vecs, making sure they don't overflow
1320 */
1321 iov_data_len = 0;
1322 for (i = 0; i < sgp->iovec_count; i++) {
1323 if (iov_data_len + iov[i].iov_len < iov_data_len)
1324 return -EINVAL;
1325 iov_data_len += iov[i].iov_len;
1326 }
1327
1328 /* SG_IO howto says that the shorter of the two wins */
1329 if (sgp->dxfer_len < iov_data_len) {
1330 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1331 sgp->iovec_count,
1332 sgp->dxfer_len);
1333 sksgio->dxfer_len = sgp->dxfer_len;
1334 } else
1335 sksgio->dxfer_len = iov_data_len;
1336 }
1337
1338 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1339 struct sg_iovec *iov = sksgio->iov;
1340 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1341 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001342 dev_dbg(&skdev->pdev->dev,
1343 "access data failed %p/%zd\n",
1344 iov->iov_base, iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001345 return -EFAULT;
1346 }
1347 }
1348 }
1349
1350 return 0;
1351}
1352
1353static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1354 struct skd_sg_io *sksgio)
1355{
1356 struct skd_special_context *skspcl = NULL;
1357 int rc;
1358
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001359 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001360 ulong flags;
1361
1362 spin_lock_irqsave(&skdev->lock, flags);
1363 skspcl = skdev->skspcl_free_list;
1364 if (skspcl != NULL) {
1365 skdev->skspcl_free_list =
1366 (struct skd_special_context *)skspcl->req.next;
1367 skspcl->req.id += SKD_ID_INCR;
1368 skspcl->req.state = SKD_REQ_STATE_SETUP;
1369 skspcl->orphaned = 0;
1370 skspcl->req.n_sg = 0;
1371 }
1372 spin_unlock_irqrestore(&skdev->lock, flags);
1373
1374 if (skspcl != NULL) {
1375 rc = 0;
1376 break;
1377 }
1378
Bart Van Asschef98806d2017-08-17 13:12:58 -07001379 dev_dbg(&skdev->pdev->dev, "blocking\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001380
1381 rc = wait_event_interruptible_timeout(
1382 skdev->waitq,
1383 (skdev->skspcl_free_list != NULL),
1384 msecs_to_jiffies(sksgio->sg.timeout));
1385
Bart Van Asschef98806d2017-08-17 13:12:58 -07001386 dev_dbg(&skdev->pdev->dev, "unblocking, rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001387
1388 if (rc <= 0) {
1389 if (rc == 0)
1390 rc = -ETIMEDOUT;
1391 else
1392 rc = -EINTR;
1393 break;
1394 }
1395 /*
1396 * If we get here rc > 0 meaning the timeout to
1397 * wait_event_interruptible_timeout() had time left, hence the
1398 * sought event -- non-empty free list -- happened.
1399 * Retry the allocation.
1400 */
1401 }
1402 sksgio->skspcl = skspcl;
1403
1404 return rc;
1405}
1406
1407static int skd_skreq_prep_buffering(struct skd_device *skdev,
1408 struct skd_request_context *skreq,
1409 u32 dxfer_len)
1410{
1411 u32 resid = dxfer_len;
1412
1413 /*
1414 * The DMA engine must have aligned addresses and byte counts.
1415 */
1416 resid += (-resid) & 3;
1417 skreq->sg_byte_count = resid;
1418
1419 skreq->n_sg = 0;
1420
1421 while (resid > 0) {
1422 u32 nbytes = PAGE_SIZE;
1423 u32 ix = skreq->n_sg;
1424 struct scatterlist *sg = &skreq->sg[ix];
1425 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1426 struct page *page;
1427
1428 if (nbytes > resid)
1429 nbytes = resid;
1430
1431 page = alloc_page(GFP_KERNEL);
1432 if (page == NULL)
1433 return -ENOMEM;
1434
1435 sg_set_page(sg, page, nbytes, 0);
1436
1437 /* TODO: This should be going through a pci_???()
1438 * routine to do proper mapping. */
1439 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1440 sksg->byte_count = nbytes;
1441
1442 sksg->host_side_addr = sg_phys(sg);
1443
1444 sksg->dev_side_addr = 0;
1445 sksg->next_desc_ptr = skreq->sksg_dma_address +
1446 (ix + 1) * sizeof(*sksg);
1447
1448 skreq->n_sg++;
1449 resid -= nbytes;
1450 }
1451
1452 if (skreq->n_sg > 0) {
1453 u32 ix = skreq->n_sg - 1;
1454 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1455
1456 sksg->control = FIT_SGD_CONTROL_LAST;
1457 sksg->next_desc_ptr = 0;
1458 }
1459
1460 if (unlikely(skdev->dbg_level > 1)) {
1461 u32 i;
1462
Bart Van Asschef98806d2017-08-17 13:12:58 -07001463 dev_dbg(&skdev->pdev->dev,
1464 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
1465 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001466 for (i = 0; i < skreq->n_sg; i++) {
1467 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1468
Bart Van Asschef98806d2017-08-17 13:12:58 -07001469 dev_dbg(&skdev->pdev->dev,
1470 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1471 i, sgd->byte_count, sgd->control,
1472 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001473 }
1474 }
1475
1476 return 0;
1477}
1478
1479static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1480 struct skd_sg_io *sksgio)
1481{
1482 struct skd_special_context *skspcl = sksgio->skspcl;
1483 struct skd_request_context *skreq = &skspcl->req;
1484 u32 dxfer_len = sksgio->dxfer_len;
1485 int rc;
1486
1487 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1488 /*
1489 * Eventually, errors or not, skd_release_special() is called
1490 * to recover allocations including partial allocations.
1491 */
1492 return rc;
1493}
1494
1495static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1496 struct skd_sg_io *sksgio, int dxfer_dir)
1497{
1498 struct skd_special_context *skspcl = sksgio->skspcl;
1499 u32 iov_ix = 0;
1500 struct sg_iovec curiov;
1501 u32 sksg_ix = 0;
1502 u8 *bufp = NULL;
1503 u32 buf_len = 0;
1504 u32 resid = sksgio->dxfer_len;
1505 int rc;
1506
1507 curiov.iov_len = 0;
1508 curiov.iov_base = NULL;
1509
1510 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1511 if (dxfer_dir != SG_DXFER_TO_DEV ||
1512 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1513 return 0;
1514 }
1515
1516 while (resid > 0) {
1517 u32 nbytes = PAGE_SIZE;
1518
1519 if (curiov.iov_len == 0) {
1520 curiov = sksgio->iov[iov_ix++];
1521 continue;
1522 }
1523
1524 if (buf_len == 0) {
1525 struct page *page;
1526 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1527 bufp = page_address(page);
1528 buf_len = PAGE_SIZE;
1529 }
1530
1531 nbytes = min_t(u32, nbytes, resid);
1532 nbytes = min_t(u32, nbytes, curiov.iov_len);
1533 nbytes = min_t(u32, nbytes, buf_len);
1534
1535 if (dxfer_dir == SG_DXFER_TO_DEV)
1536 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1537 else
1538 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1539
1540 if (rc)
1541 return -EFAULT;
1542
1543 resid -= nbytes;
1544 curiov.iov_len -= nbytes;
1545 curiov.iov_base += nbytes;
1546 buf_len -= nbytes;
1547 }
1548
1549 return 0;
1550}
1551
1552static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1553 struct skd_sg_io *sksgio)
1554{
1555 struct skd_special_context *skspcl = sksgio->skspcl;
Bart Van Assched891fe62017-08-17 13:13:07 -07001556 struct fit_msg_hdr *fmh = &skspcl->msg_buf->fmh;
1557 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001558
1559 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1560
1561 /* Initialize the FIT msg header */
1562 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1563 fmh->num_protocol_cmds_coalesced = 1;
1564
1565 /* Initialize the SCSI request */
1566 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1567 scsi_req->hdr.sg_list_dma_address =
1568 cpu_to_be64(skspcl->req.sksg_dma_address);
1569 scsi_req->hdr.tag = skspcl->req.id;
1570 scsi_req->hdr.sg_list_len_bytes =
1571 cpu_to_be32(skspcl->req.sg_byte_count);
1572 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1573
1574 skspcl->req.state = SKD_REQ_STATE_BUSY;
1575 skd_send_special_fitmsg(skdev, skspcl);
1576
1577 return 0;
1578}
1579
1580static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1581{
1582 unsigned long flags;
1583 int rc;
1584
1585 rc = wait_event_interruptible_timeout(skdev->waitq,
1586 (sksgio->skspcl->req.state !=
1587 SKD_REQ_STATE_BUSY),
1588 msecs_to_jiffies(sksgio->sg.
1589 timeout));
1590
1591 spin_lock_irqsave(&skdev->lock, flags);
1592
1593 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001594 dev_dbg(&skdev->pdev->dev, "skspcl %p aborted\n",
1595 sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001596
1597 /* Build check cond, sense and let command finish. */
1598 /* For a timeout, we must fabricate completion and sense
1599 * data to complete the command */
1600 sksgio->skspcl->req.completion.status =
1601 SAM_STAT_CHECK_CONDITION;
1602
1603 memset(&sksgio->skspcl->req.err_info, 0,
1604 sizeof(sksgio->skspcl->req.err_info));
1605 sksgio->skspcl->req.err_info.type = 0x70;
1606 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1607 sksgio->skspcl->req.err_info.code = 0x44;
1608 sksgio->skspcl->req.err_info.qual = 0;
1609 rc = 0;
1610 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1611 /* No longer on the adapter. We finish. */
1612 rc = 0;
1613 else {
1614 /* Something's gone wrong. Still busy. Timeout or
1615 * user interrupted (control-C). Mark as an orphan
1616 * so it will be disposed when completed. */
1617 sksgio->skspcl->orphaned = 1;
1618 sksgio->skspcl = NULL;
1619 if (rc == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001620 dev_dbg(&skdev->pdev->dev, "timed out %p (%u ms)\n",
1621 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001622 rc = -ETIMEDOUT;
1623 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001624 dev_dbg(&skdev->pdev->dev, "cntlc %p\n", sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001625 rc = -EINTR;
1626 }
1627 }
1628
1629 spin_unlock_irqrestore(&skdev->lock, flags);
1630
1631 return rc;
1632}
1633
1634static int skd_sg_io_put_status(struct skd_device *skdev,
1635 struct skd_sg_io *sksgio)
1636{
1637 struct sg_io_hdr *sgp = &sksgio->sg;
1638 struct skd_special_context *skspcl = sksgio->skspcl;
1639 int resid = 0;
1640
1641 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1642
1643 sgp->status = skspcl->req.completion.status;
1644 resid = sksgio->dxfer_len - nb;
1645
1646 sgp->masked_status = sgp->status & STATUS_MASK;
1647 sgp->msg_status = 0;
1648 sgp->host_status = 0;
1649 sgp->driver_status = 0;
1650 sgp->resid = resid;
1651 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1652 sgp->info |= SG_INFO_CHECK;
1653
Bart Van Asschef98806d2017-08-17 13:12:58 -07001654 dev_dbg(&skdev->pdev->dev, "status %x masked %x resid 0x%x\n",
1655 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001656
1657 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1658 if (sgp->mx_sb_len > 0) {
1659 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1660 u32 nbytes = sizeof(*ei);
1661
1662 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1663
1664 sgp->sb_len_wr = nbytes;
1665
1666 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001667 dev_dbg(&skdev->pdev->dev,
1668 "copy_to_user sense failed %p\n",
1669 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001670 return -EFAULT;
1671 }
1672 }
1673 }
1674
1675 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001676 dev_dbg(&skdev->pdev->dev, "copy_to_user sg failed %p\n",
1677 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001678 return -EFAULT;
1679 }
1680
1681 return 0;
1682}
1683
1684static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1685 struct skd_sg_io *sksgio)
1686{
1687 struct skd_special_context *skspcl = sksgio->skspcl;
1688
1689 if (skspcl != NULL) {
1690 ulong flags;
1691
1692 sksgio->skspcl = NULL;
1693
1694 spin_lock_irqsave(&skdev->lock, flags);
1695 skd_release_special(skdev, skspcl);
1696 spin_unlock_irqrestore(&skdev->lock, flags);
1697 }
1698
1699 return 0;
1700}
1701
1702/*
1703 *****************************************************************************
1704 * INTERNAL REQUESTS -- generated by driver itself
1705 *****************************************************************************
1706 */
1707
1708static int skd_format_internal_skspcl(struct skd_device *skdev)
1709{
1710 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1711 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1712 struct fit_msg_hdr *fmh;
1713 uint64_t dma_address;
1714 struct skd_scsi_request *scsi;
1715
Bart Van Assched891fe62017-08-17 13:13:07 -07001716 fmh = &skspcl->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001717 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1718 fmh->num_protocol_cmds_coalesced = 1;
1719
Bart Van Assched891fe62017-08-17 13:13:07 -07001720 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001721 memset(scsi, 0, sizeof(*scsi));
1722 dma_address = skspcl->req.sksg_dma_address;
1723 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1724 sgd->control = FIT_SGD_CONTROL_LAST;
1725 sgd->byte_count = 0;
1726 sgd->host_side_addr = skspcl->db_dma_address;
1727 sgd->dev_side_addr = 0;
1728 sgd->next_desc_ptr = 0LL;
1729
1730 return 1;
1731}
1732
1733#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1734
1735static void skd_send_internal_skspcl(struct skd_device *skdev,
1736 struct skd_special_context *skspcl,
1737 u8 opcode)
1738{
1739 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1740 struct skd_scsi_request *scsi;
1741 unsigned char *buf = skspcl->data_buf;
1742 int i;
1743
1744 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1745 /*
1746 * A refresh is already in progress.
1747 * Just wait for it to finish.
1748 */
1749 return;
1750
1751 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1752 skspcl->req.state = SKD_REQ_STATE_BUSY;
1753 skspcl->req.id += SKD_ID_INCR;
1754
Bart Van Assched891fe62017-08-17 13:13:07 -07001755 scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001756 scsi->hdr.tag = skspcl->req.id;
1757
1758 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1759
1760 switch (opcode) {
1761 case TEST_UNIT_READY:
1762 scsi->cdb[0] = TEST_UNIT_READY;
1763 sgd->byte_count = 0;
1764 scsi->hdr.sg_list_len_bytes = 0;
1765 break;
1766
1767 case READ_CAPACITY:
1768 scsi->cdb[0] = READ_CAPACITY;
1769 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1770 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1771 break;
1772
1773 case INQUIRY:
1774 scsi->cdb[0] = INQUIRY;
1775 scsi->cdb[1] = 0x01; /* evpd */
1776 scsi->cdb[2] = 0x80; /* serial number page */
1777 scsi->cdb[4] = 0x10;
1778 sgd->byte_count = 16;
1779 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1780 break;
1781
1782 case SYNCHRONIZE_CACHE:
1783 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1784 sgd->byte_count = 0;
1785 scsi->hdr.sg_list_len_bytes = 0;
1786 break;
1787
1788 case WRITE_BUFFER:
1789 scsi->cdb[0] = WRITE_BUFFER;
1790 scsi->cdb[1] = 0x02;
1791 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1792 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1793 sgd->byte_count = WR_BUF_SIZE;
1794 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1795 /* fill incrementing byte pattern */
1796 for (i = 0; i < sgd->byte_count; i++)
1797 buf[i] = i & 0xFF;
1798 break;
1799
1800 case READ_BUFFER:
1801 scsi->cdb[0] = READ_BUFFER;
1802 scsi->cdb[1] = 0x02;
1803 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1804 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1805 sgd->byte_count = WR_BUF_SIZE;
1806 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1807 memset(skspcl->data_buf, 0, sgd->byte_count);
1808 break;
1809
1810 default:
1811 SKD_ASSERT("Don't know what to send");
1812 return;
1813
1814 }
1815 skd_send_special_fitmsg(skdev, skspcl);
1816}
1817
1818static void skd_refresh_device_data(struct skd_device *skdev)
1819{
1820 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1821
1822 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1823}
1824
1825static int skd_chk_read_buf(struct skd_device *skdev,
1826 struct skd_special_context *skspcl)
1827{
1828 unsigned char *buf = skspcl->data_buf;
1829 int i;
1830
1831 /* check for incrementing byte pattern */
1832 for (i = 0; i < WR_BUF_SIZE; i++)
1833 if (buf[i] != (i & 0xFF))
1834 return 1;
1835
1836 return 0;
1837}
1838
1839static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1840 u8 code, u8 qual, u8 fruc)
1841{
1842 /* If the check condition is of special interest, log a message */
1843 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1844 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001845 dev_err(&skdev->pdev->dev,
1846 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1847 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001848 }
1849}
1850
1851static void skd_complete_internal(struct skd_device *skdev,
1852 volatile struct fit_completion_entry_v1
1853 *skcomp,
1854 volatile struct fit_comp_error_info *skerr,
1855 struct skd_special_context *skspcl)
1856{
1857 u8 *buf = skspcl->data_buf;
1858 u8 status;
1859 int i;
Bart Van Assched891fe62017-08-17 13:13:07 -07001860 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001861
Bart Van Assche760b48c2017-08-17 13:13:00 -07001862 lockdep_assert_held(&skdev->lock);
1863
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001864 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1865
Bart Van Asschef98806d2017-08-17 13:12:58 -07001866 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001867
1868 skspcl->req.completion = *skcomp;
1869 skspcl->req.state = SKD_REQ_STATE_IDLE;
1870 skspcl->req.id += SKD_ID_INCR;
1871
1872 status = skspcl->req.completion.status;
1873
1874 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1875 skerr->qual, skerr->fruc);
1876
1877 switch (scsi->cdb[0]) {
1878 case TEST_UNIT_READY:
1879 if (status == SAM_STAT_GOOD)
1880 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1881 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1882 (skerr->key == MEDIUM_ERROR))
1883 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1884 else {
1885 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001886 dev_dbg(&skdev->pdev->dev,
1887 "TUR failed, don't send anymore state 0x%x\n",
1888 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001889 return;
1890 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001891 dev_dbg(&skdev->pdev->dev,
1892 "**** TUR failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001893 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1894 }
1895 break;
1896
1897 case WRITE_BUFFER:
1898 if (status == SAM_STAT_GOOD)
1899 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1900 else {
1901 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001902 dev_dbg(&skdev->pdev->dev,
1903 "write buffer failed, don't send anymore state 0x%x\n",
1904 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001905 return;
1906 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001907 dev_dbg(&skdev->pdev->dev,
1908 "**** write buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001909 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1910 }
1911 break;
1912
1913 case READ_BUFFER:
1914 if (status == SAM_STAT_GOOD) {
1915 if (skd_chk_read_buf(skdev, skspcl) == 0)
1916 skd_send_internal_skspcl(skdev, skspcl,
1917 READ_CAPACITY);
1918 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001919 dev_err(&skdev->pdev->dev,
1920 "*** W/R Buffer mismatch %d ***\n",
1921 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001922 if (skdev->connect_retries <
1923 SKD_MAX_CONNECT_RETRIES) {
1924 skdev->connect_retries++;
1925 skd_soft_reset(skdev);
1926 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001927 dev_err(&skdev->pdev->dev,
1928 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001929 return;
1930 }
1931 }
1932
1933 } else {
1934 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001935 dev_dbg(&skdev->pdev->dev,
1936 "read buffer failed, don't send anymore state 0x%x\n",
1937 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001938 return;
1939 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001940 dev_dbg(&skdev->pdev->dev,
1941 "**** read buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001942 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1943 }
1944 break;
1945
1946 case READ_CAPACITY:
1947 skdev->read_cap_is_valid = 0;
1948 if (status == SAM_STAT_GOOD) {
1949 skdev->read_cap_last_lba =
1950 (buf[0] << 24) | (buf[1] << 16) |
1951 (buf[2] << 8) | buf[3];
1952 skdev->read_cap_blocksize =
1953 (buf[4] << 24) | (buf[5] << 16) |
1954 (buf[6] << 8) | buf[7];
1955
Bart Van Asschef98806d2017-08-17 13:12:58 -07001956 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1957 skdev->read_cap_last_lba,
1958 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001959
1960 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1961
1962 skdev->read_cap_is_valid = 1;
1963
1964 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1965 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1966 (skerr->key == MEDIUM_ERROR)) {
1967 skdev->read_cap_last_lba = ~0;
1968 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001969 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 -06001970 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1971 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001972 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001973 skd_send_internal_skspcl(skdev, skspcl,
1974 TEST_UNIT_READY);
1975 }
1976 break;
1977
1978 case INQUIRY:
1979 skdev->inquiry_is_valid = 0;
1980 if (status == SAM_STAT_GOOD) {
1981 skdev->inquiry_is_valid = 1;
1982
1983 for (i = 0; i < 12; i++)
1984 skdev->inq_serial_num[i] = buf[i + 4];
1985 skdev->inq_serial_num[12] = 0;
1986 }
1987
1988 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001989 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001990 /* connection is complete */
1991 skdev->connect_retries = 0;
1992 break;
1993
1994 case SYNCHRONIZE_CACHE:
1995 if (status == SAM_STAT_GOOD)
1996 skdev->sync_done = 1;
1997 else
1998 skdev->sync_done = -1;
1999 wake_up_interruptible(&skdev->waitq);
2000 break;
2001
2002 default:
2003 SKD_ASSERT("we didn't send this");
2004 }
2005}
2006
2007/*
2008 *****************************************************************************
2009 * FIT MESSAGES
2010 *****************************************************************************
2011 */
2012
2013static void skd_send_fitmsg(struct skd_device *skdev,
2014 struct skd_fitmsg_context *skmsg)
2015{
2016 u64 qcmd;
2017 struct fit_msg_hdr *fmh;
2018
Bart Van Asschef98806d2017-08-17 13:12:58 -07002019 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
2020 skmsg->mb_dma_address, skdev->in_flight);
Bart Van Assche6507f432017-08-17 13:13:06 -07002021 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002022
2023 qcmd = skmsg->mb_dma_address;
2024 qcmd |= FIT_QCMD_QID_NORMAL;
2025
Bart Van Assched891fe62017-08-17 13:13:07 -07002026 fmh = &skmsg->msg_buf->fmh;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002027 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2028
2029 if (unlikely(skdev->dbg_level > 1)) {
2030 u8 *bp = (u8 *)skmsg->msg_buf;
2031 int i;
2032 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002033 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
2034 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002035 if (i == 0)
2036 i = 64 - 8;
2037 }
2038 }
2039
2040 if (skmsg->length > 256)
2041 qcmd |= FIT_QCMD_MSGSIZE_512;
2042 else if (skmsg->length > 128)
2043 qcmd |= FIT_QCMD_MSGSIZE_256;
2044 else if (skmsg->length > 64)
2045 qcmd |= FIT_QCMD_MSGSIZE_128;
2046 else
2047 /*
2048 * This makes no sense because the FIT msg header is
2049 * 64 bytes. If the msg is only 64 bytes long it has
2050 * no payload.
2051 */
2052 qcmd |= FIT_QCMD_MSGSIZE_64;
2053
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002054 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2055 smp_wmb();
2056
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002057 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002058}
2059
2060static void skd_send_special_fitmsg(struct skd_device *skdev,
2061 struct skd_special_context *skspcl)
2062{
2063 u64 qcmd;
2064
2065 if (unlikely(skdev->dbg_level > 1)) {
2066 u8 *bp = (u8 *)skspcl->msg_buf;
2067 int i;
2068
2069 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002070 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
2071 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002072 if (i == 0)
2073 i = 64 - 8;
2074 }
2075
Bart Van Asschef98806d2017-08-17 13:12:58 -07002076 dev_dbg(&skdev->pdev->dev,
2077 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2078 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2079 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002080 for (i = 0; i < skspcl->req.n_sg; i++) {
2081 struct fit_sg_descriptor *sgd =
2082 &skspcl->req.sksg_list[i];
2083
Bart Van Asschef98806d2017-08-17 13:12:58 -07002084 dev_dbg(&skdev->pdev->dev,
2085 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
2086 i, sgd->byte_count, sgd->control,
2087 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002088 }
2089 }
2090
2091 /*
2092 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2093 * and one 64-byte SSDI command.
2094 */
2095 qcmd = skspcl->mb_dma_address;
2096 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2097
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002098 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2099 smp_wmb();
2100
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002101 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2102}
2103
2104/*
2105 *****************************************************************************
2106 * COMPLETION QUEUE
2107 *****************************************************************************
2108 */
2109
2110static void skd_complete_other(struct skd_device *skdev,
2111 volatile struct fit_completion_entry_v1 *skcomp,
2112 volatile struct fit_comp_error_info *skerr);
2113
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002114struct sns_info {
2115 u8 type;
2116 u8 stat;
2117 u8 key;
2118 u8 asc;
2119 u8 ascq;
2120 u8 mask;
2121 enum skd_check_status_action action;
2122};
2123
2124static struct sns_info skd_chkstat_table[] = {
2125 /* Good */
2126 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2127 SKD_CHECK_STATUS_REPORT_GOOD },
2128
2129 /* Smart alerts */
2130 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2131 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2132 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2133 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2134 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2135 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2136
2137 /* Retry (with limits) */
2138 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2139 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2140 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2141 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2142 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2143 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2144 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2145 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2146
2147 /* Busy (or about to be) */
2148 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2149 SKD_CHECK_STATUS_BUSY_IMMINENT },
2150};
2151
2152/*
2153 * Look up status and sense data to decide how to handle the error
2154 * from the device.
2155 * mask says which fields must match e.g., mask=0x18 means check
2156 * type and stat, ignore key, asc, ascq.
2157 */
2158
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002159static enum skd_check_status_action
2160skd_check_status(struct skd_device *skdev,
2161 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002162{
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07002163 int i;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002164
Bart Van Asschef98806d2017-08-17 13:12:58 -07002165 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2166 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002167
Bart Van Asschef98806d2017-08-17 13:12:58 -07002168 dev_dbg(&skdev->pdev->dev,
2169 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2170 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
2171 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002172
2173 /* Does the info match an entry in the good category? */
Bart Van Assche0b2e0c02017-08-17 13:13:11 -07002174 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002175 struct sns_info *sns = &skd_chkstat_table[i];
2176
2177 if (sns->mask & 0x10)
2178 if (skerr->type != sns->type)
2179 continue;
2180
2181 if (sns->mask & 0x08)
2182 if (cmp_status != sns->stat)
2183 continue;
2184
2185 if (sns->mask & 0x04)
2186 if (skerr->key != sns->key)
2187 continue;
2188
2189 if (sns->mask & 0x02)
2190 if (skerr->code != sns->asc)
2191 continue;
2192
2193 if (sns->mask & 0x01)
2194 if (skerr->qual != sns->ascq)
2195 continue;
2196
2197 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002198 dev_err(&skdev->pdev->dev,
2199 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
2200 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002201 }
2202 return sns->action;
2203 }
2204
2205 /* No other match, so nonzero status means error,
2206 * zero status means good
2207 */
2208 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002209 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002210 return SKD_CHECK_STATUS_REPORT_ERROR;
2211 }
2212
Bart Van Asschef98806d2017-08-17 13:12:58 -07002213 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002214 return SKD_CHECK_STATUS_REPORT_GOOD;
2215}
2216
2217static void skd_resolve_req_exception(struct skd_device *skdev,
2218 struct skd_request_context *skreq)
2219{
2220 u8 cmp_status = skreq->completion.status;
2221
2222 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2223 case SKD_CHECK_STATUS_REPORT_GOOD:
2224 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002225 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002226 break;
2227
2228 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2229 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002230 blk_requeue_request(skdev->queue, skreq->req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002231 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002232 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2233 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2234 skd_quiesce_dev(skdev);
2235 break;
2236
2237 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002238 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2239 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002240 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002241 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002242 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07002243 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002244
2245 case SKD_CHECK_STATUS_REPORT_ERROR:
2246 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002247 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002248 break;
2249 }
2250}
2251
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002252/* assume spinlock is already held */
2253static void skd_release_skreq(struct skd_device *skdev,
2254 struct skd_request_context *skreq)
2255{
2256 u32 msg_slot;
2257 struct skd_fitmsg_context *skmsg;
2258
2259 u32 timo_slot;
2260
2261 /*
2262 * Reclaim the FIT msg buffer if this is
2263 * the first of the requests it carried to
2264 * be completed. The FIT msg buffer used to
2265 * send this request cannot be reused until
2266 * we are sure the s1120 card has copied
2267 * it to its memory. The FIT msg might have
2268 * contained several requests. As soon as
2269 * any of them are completed we know that
2270 * the entire FIT msg was transferred.
2271 * Only the first completed request will
2272 * match the FIT msg buffer id. The FIT
2273 * msg buffer id is immediately updated.
2274 * When subsequent requests complete the FIT
2275 * msg buffer id won't match, so we know
2276 * quite cheaply that it is already done.
2277 */
2278 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2279 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2280
2281 skmsg = &skdev->skmsg_table[msg_slot];
2282 if (skmsg->id == skreq->fitmsg_id) {
2283 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2284 SKD_ASSERT(skmsg->outstanding > 0);
2285 skmsg->outstanding--;
2286 if (skmsg->outstanding == 0) {
2287 skmsg->state = SKD_MSG_STATE_IDLE;
2288 skmsg->id += SKD_ID_INCR;
2289 skmsg->next = skdev->skmsg_free_list;
2290 skdev->skmsg_free_list = skmsg;
2291 }
2292 }
2293
2294 /*
2295 * Decrease the number of active requests.
2296 * Also decrements the count in the timeout slot.
2297 */
2298 SKD_ASSERT(skdev->in_flight > 0);
2299 skdev->in_flight -= 1;
2300
2301 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2302 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2303 skdev->timeout_slot[timo_slot] -= 1;
2304
2305 /*
2306 * Reset backpointer
2307 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002308 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002309
2310 /*
2311 * Reclaim the skd_request_context
2312 */
2313 skreq->state = SKD_REQ_STATE_IDLE;
2314 skreq->id += SKD_ID_INCR;
2315 skreq->next = skdev->skreq_free_list;
2316 skdev->skreq_free_list = skreq;
2317}
2318
2319#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2320
2321static void skd_do_inq_page_00(struct skd_device *skdev,
2322 volatile struct fit_completion_entry_v1 *skcomp,
2323 volatile struct fit_comp_error_info *skerr,
2324 uint8_t *cdb, uint8_t *buf)
2325{
2326 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2327
2328 /* Caller requested "supported pages". The driver needs to insert
2329 * its page.
2330 */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002331 dev_dbg(&skdev->pdev->dev,
2332 "skd_do_driver_inquiry: modify supported pages.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002333
2334 /* If the device rejected the request because the CDB was
2335 * improperly formed, then just leave.
2336 */
2337 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2338 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2339 return;
2340
2341 /* Get the amount of space the caller allocated */
2342 max_bytes = (cdb[3] << 8) | cdb[4];
2343
2344 /* Get the number of pages actually returned by the device */
2345 drive_pages = (buf[2] << 8) | buf[3];
2346 drive_bytes = drive_pages + 4;
2347 new_size = drive_pages + 1;
2348
2349 /* Supported pages must be in numerical order, so find where
2350 * the driver page needs to be inserted into the list of
2351 * pages returned by the device.
2352 */
2353 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2354 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2355 return; /* Device using this page code. abort */
2356 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2357 break;
2358 }
2359
2360 if (insert_pt < max_bytes) {
2361 uint16_t u;
2362
2363 /* Shift everything up one byte to make room. */
2364 for (u = new_size + 3; u > insert_pt; u--)
2365 buf[u] = buf[u - 1];
2366 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2367
2368 /* SCSI byte order increment of num_returned_bytes by 1 */
2369 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002370 cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002371 }
2372
2373 /* update page length field to reflect the driver's page too */
2374 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2375 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2376}
2377
2378static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2379{
2380 int pcie_reg;
2381 u16 pci_bus_speed;
2382 u8 pci_lanes;
2383
2384 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2385 if (pcie_reg) {
2386 u16 linksta;
2387 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2388
2389 pci_bus_speed = linksta & 0xF;
2390 pci_lanes = (linksta & 0x3F0) >> 4;
2391 } else {
2392 *speed = STEC_LINK_UNKNOWN;
2393 *width = 0xFF;
2394 return;
2395 }
2396
2397 switch (pci_bus_speed) {
2398 case 1:
2399 *speed = STEC_LINK_2_5GTS;
2400 break;
2401 case 2:
2402 *speed = STEC_LINK_5GTS;
2403 break;
2404 case 3:
2405 *speed = STEC_LINK_8GTS;
2406 break;
2407 default:
2408 *speed = STEC_LINK_UNKNOWN;
2409 break;
2410 }
2411
2412 if (pci_lanes <= 0x20)
2413 *width = pci_lanes;
2414 else
2415 *width = 0xFF;
2416}
2417
2418static void skd_do_inq_page_da(struct skd_device *skdev,
2419 volatile struct fit_completion_entry_v1 *skcomp,
2420 volatile struct fit_comp_error_info *skerr,
2421 uint8_t *cdb, uint8_t *buf)
2422{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002423 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002424 unsigned max_bytes;
2425 struct driver_inquiry_data inq;
2426 u16 val;
2427
Bart Van Asschef98806d2017-08-17 13:12:58 -07002428 dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002429
2430 memset(&inq, 0, sizeof(inq));
2431
2432 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2433
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002434 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2435 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2436 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2437 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002438
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002439 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2440 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002441
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002442 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2443 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002444
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002445 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2446 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002447
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002448 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2449 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002450
2451 /* Driver version, fixed lenth, padded with spaces on the right */
2452 inq.driver_version_length = sizeof(inq.driver_version);
2453 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2454 memcpy(inq.driver_version, DRV_VER_COMPL,
2455 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2456
2457 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2458
2459 /* Clear the error set by the device */
2460 skcomp->status = SAM_STAT_GOOD;
2461 memset((void *)skerr, 0, sizeof(*skerr));
2462
2463 /* copy response into output buffer */
2464 max_bytes = (cdb[3] << 8) | cdb[4];
2465 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2466
2467 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002468 cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq)));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002469}
2470
2471static void skd_do_driver_inq(struct skd_device *skdev,
2472 volatile struct fit_completion_entry_v1 *skcomp,
2473 volatile struct fit_comp_error_info *skerr,
2474 uint8_t *cdb, uint8_t *buf)
2475{
2476 if (!buf)
2477 return;
2478 else if (cdb[0] != INQUIRY)
2479 return; /* Not an INQUIRY */
2480 else if ((cdb[1] & 1) == 0)
2481 return; /* EVPD not set */
2482 else if (cdb[2] == 0)
2483 /* Need to add driver's page to supported pages list */
2484 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2485 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2486 /* Caller requested driver's page */
2487 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2488}
2489
2490static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2491{
2492 if (!sg)
2493 return NULL;
2494 if (!sg_page(sg))
2495 return NULL;
2496 return sg_virt(sg);
2497}
2498
2499static void skd_process_scsi_inq(struct skd_device *skdev,
2500 volatile struct fit_completion_entry_v1
2501 *skcomp,
2502 volatile struct fit_comp_error_info *skerr,
2503 struct skd_special_context *skspcl)
2504{
2505 uint8_t *buf;
Bart Van Assched891fe62017-08-17 13:13:07 -07002506 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002507
2508 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2509 skspcl->req.sg_data_dir);
2510 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2511
2512 if (buf)
2513 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2514}
2515
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002516static int skd_isr_completion_posted(struct skd_device *skdev,
2517 int limit, int *enqueued)
2518{
2519 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2520 volatile struct fit_comp_error_info *skerr;
2521 u16 req_id;
2522 u32 req_slot;
2523 struct skd_request_context *skreq;
2524 u16 cmp_cntxt = 0;
2525 u8 cmp_status = 0;
2526 u8 cmp_cycle = 0;
2527 u32 cmp_bytes = 0;
2528 int rc = 0;
2529 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002530
Bart Van Assche760b48c2017-08-17 13:13:00 -07002531 lockdep_assert_held(&skdev->lock);
2532
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002533 for (;; ) {
2534 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2535
2536 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2537 cmp_cycle = skcmp->cycle;
2538 cmp_cntxt = skcmp->tag;
2539 cmp_status = skcmp->status;
2540 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2541
2542 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2543
Bart Van Asschef98806d2017-08-17 13:12:58 -07002544 dev_dbg(&skdev->pdev->dev,
2545 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
2546 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
2547 cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
2548 skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002549
2550 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002551 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002552 break;
2553 }
2554 /*
2555 * Update the completion queue head index and possibly
2556 * the completion cycle count. 8-bit wrap-around.
2557 */
2558 skdev->skcomp_ix++;
2559 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2560 skdev->skcomp_ix = 0;
2561 skdev->skcomp_cycle++;
2562 }
2563
2564 /*
2565 * The command context is a unique 32-bit ID. The low order
2566 * bits help locate the request. The request is usually a
2567 * r/w request (see skd_start() above) or a special request.
2568 */
2569 req_id = cmp_cntxt;
2570 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2571
2572 /* Is this other than a r/w request? */
2573 if (req_slot >= skdev->num_req_context) {
2574 /*
2575 * This is not a completion for a r/w request.
2576 */
2577 skd_complete_other(skdev, skcmp, skerr);
2578 continue;
2579 }
2580
2581 skreq = &skdev->skreq_table[req_slot];
2582
2583 /*
2584 * Make sure the request ID for the slot matches.
2585 */
2586 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002587 dev_dbg(&skdev->pdev->dev,
2588 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
2589 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002590 {
2591 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002592 dev_err(&skdev->pdev->dev,
2593 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2594 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002595
2596 continue;
2597 }
2598 }
2599
2600 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2601
2602 if (skreq->state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002603 dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n",
2604 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002605 /* a previously timed out command can
2606 * now be cleaned up */
2607 skd_release_skreq(skdev, skreq);
2608 continue;
2609 }
2610
2611 skreq->completion = *skcmp;
2612 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2613 skreq->err_info = *skerr;
2614 skd_log_check_status(skdev, cmp_status, skerr->key,
2615 skerr->code, skerr->qual,
2616 skerr->fruc);
2617 }
2618 /* Release DMA resources for the request. */
2619 if (skreq->n_sg > 0)
2620 skd_postop_sg_list(skdev, skreq);
2621
Jens Axboefcd37eb2013-11-01 10:14:56 -06002622 if (!skreq->req) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002623 dev_dbg(&skdev->pdev->dev,
2624 "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
2625 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002626 } else {
2627 /*
2628 * Capture the outcome and post it back to the
2629 * native request.
2630 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002631 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002632 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002633 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002634 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002635 }
2636
2637 /*
2638 * Release the skreq, its FIT msg (if one), timeout slot,
2639 * and queue depth.
2640 */
2641 skd_release_skreq(skdev, skreq);
2642
2643 /* skd_isr_comp_limit equal zero means no limit */
2644 if (limit) {
2645 if (++processed >= limit) {
2646 rc = 1;
2647 break;
2648 }
2649 }
2650 }
2651
2652 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2653 && (skdev->in_flight) == 0) {
2654 skdev->state = SKD_DRVR_STATE_PAUSED;
2655 wake_up_interruptible(&skdev->waitq);
2656 }
2657
2658 return rc;
2659}
2660
2661static void skd_complete_other(struct skd_device *skdev,
2662 volatile struct fit_completion_entry_v1 *skcomp,
2663 volatile struct fit_comp_error_info *skerr)
2664{
2665 u32 req_id = 0;
2666 u32 req_table;
2667 u32 req_slot;
2668 struct skd_special_context *skspcl;
2669
Bart Van Assche760b48c2017-08-17 13:13:00 -07002670 lockdep_assert_held(&skdev->lock);
2671
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002672 req_id = skcomp->tag;
2673 req_table = req_id & SKD_ID_TABLE_MASK;
2674 req_slot = req_id & SKD_ID_SLOT_MASK;
2675
Bart Van Asschef98806d2017-08-17 13:12:58 -07002676 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
2677 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002678
2679 /*
2680 * Based on the request id, determine how to dispatch this completion.
2681 * This swich/case is finding the good cases and forwarding the
2682 * completion entry. Errors are reported below the switch.
2683 */
2684 switch (req_table) {
2685 case SKD_ID_RW_REQUEST:
2686 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07002687 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002688 * handles r/w requests. The only way we get here
2689 * is if the req_slot is out of bounds.
2690 */
2691 break;
2692
2693 case SKD_ID_SPECIAL_REQUEST:
2694 /*
2695 * Make sure the req_slot is in bounds and that the id
2696 * matches.
2697 */
2698 if (req_slot < skdev->n_special) {
2699 skspcl = &skdev->skspcl_table[req_slot];
2700 if (skspcl->req.id == req_id &&
2701 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2702 skd_complete_special(skdev,
2703 skcomp, skerr, skspcl);
2704 return;
2705 }
2706 }
2707 break;
2708
2709 case SKD_ID_INTERNAL:
2710 if (req_slot == 0) {
2711 skspcl = &skdev->internal_skspcl;
2712 if (skspcl->req.id == req_id &&
2713 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2714 skd_complete_internal(skdev,
2715 skcomp, skerr, skspcl);
2716 return;
2717 }
2718 }
2719 break;
2720
2721 case SKD_ID_FIT_MSG:
2722 /*
2723 * These id's should never appear in a completion record.
2724 */
2725 break;
2726
2727 default:
2728 /*
2729 * These id's should never appear anywhere;
2730 */
2731 break;
2732 }
2733
2734 /*
2735 * If we get here it is a bad or stale id.
2736 */
2737}
2738
2739static void skd_complete_special(struct skd_device *skdev,
2740 volatile struct fit_completion_entry_v1
2741 *skcomp,
2742 volatile struct fit_comp_error_info *skerr,
2743 struct skd_special_context *skspcl)
2744{
Bart Van Assche760b48c2017-08-17 13:13:00 -07002745 lockdep_assert_held(&skdev->lock);
2746
Bart Van Asschef98806d2017-08-17 13:12:58 -07002747 dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002748 if (skspcl->orphaned) {
2749 /* Discard orphaned request */
2750 /* ?: Can this release directly or does it need
2751 * to use a worker? */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002752 dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002753 skd_release_special(skdev, skspcl);
2754 return;
2755 }
2756
2757 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2758
2759 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2760 skspcl->req.completion = *skcomp;
2761 skspcl->req.err_info = *skerr;
2762
2763 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2764 skerr->code, skerr->qual, skerr->fruc);
2765
2766 wake_up_interruptible(&skdev->waitq);
2767}
2768
2769/* assume spinlock is already held */
2770static void skd_release_special(struct skd_device *skdev,
2771 struct skd_special_context *skspcl)
2772{
2773 int i, was_depleted;
2774
2775 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002776 struct page *page = sg_page(&skspcl->req.sg[i]);
2777 __free_page(page);
2778 }
2779
2780 was_depleted = (skdev->skspcl_free_list == NULL);
2781
2782 skspcl->req.state = SKD_REQ_STATE_IDLE;
2783 skspcl->req.id += SKD_ID_INCR;
2784 skspcl->req.next =
2785 (struct skd_request_context *)skdev->skspcl_free_list;
2786 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2787
2788 if (was_depleted) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002789 dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002790 /* Free list was depleted. Their might be waiters. */
2791 wake_up_interruptible(&skdev->waitq);
2792 }
2793}
2794
2795static void skd_reset_skcomp(struct skd_device *skdev)
2796{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002797 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002798
2799 skdev->skcomp_ix = 0;
2800 skdev->skcomp_cycle = 1;
2801}
2802
2803/*
2804 *****************************************************************************
2805 * INTERRUPTS
2806 *****************************************************************************
2807 */
2808static void skd_completion_worker(struct work_struct *work)
2809{
2810 struct skd_device *skdev =
2811 container_of(work, struct skd_device, completion_worker);
2812 unsigned long flags;
2813 int flush_enqueued = 0;
2814
2815 spin_lock_irqsave(&skdev->lock, flags);
2816
2817 /*
2818 * pass in limit=0, which means no limit..
2819 * process everything in compq
2820 */
2821 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2822 skd_request_fn(skdev->queue);
2823
2824 spin_unlock_irqrestore(&skdev->lock, flags);
2825}
2826
2827static void skd_isr_msg_from_dev(struct skd_device *skdev);
2828
Arnd Bergmann41c94992016-11-09 13:55:35 +01002829static irqreturn_t
2830skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002831{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07002832 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002833 u32 intstat;
2834 u32 ack;
2835 int rc = 0;
2836 int deferred = 0;
2837 int flush_enqueued = 0;
2838
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002839 spin_lock(&skdev->lock);
2840
2841 for (;; ) {
2842 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2843
2844 ack = FIT_INT_DEF_MASK;
2845 ack &= intstat;
2846
Bart Van Asschef98806d2017-08-17 13:12:58 -07002847 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
2848 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002849
2850 /* As long as there is an int pending on device, keep
2851 * running loop. When none, get out, but if we've never
2852 * done any processing, call completion handler?
2853 */
2854 if (ack == 0) {
2855 /* No interrupts on device, but run the completion
2856 * processor anyway?
2857 */
2858 if (rc == 0)
2859 if (likely (skdev->state
2860 == SKD_DRVR_STATE_ONLINE))
2861 deferred = 1;
2862 break;
2863 }
2864
2865 rc = IRQ_HANDLED;
2866
2867 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2868
2869 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2870 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2871 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2872 /*
2873 * If we have already deferred completion
2874 * processing, don't bother running it again
2875 */
2876 if (deferred == 0)
2877 deferred =
2878 skd_isr_completion_posted(skdev,
2879 skd_isr_comp_limit, &flush_enqueued);
2880 }
2881
2882 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2883 skd_isr_fwstate(skdev);
2884 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2885 skdev->state ==
2886 SKD_DRVR_STATE_DISAPPEARED) {
2887 spin_unlock(&skdev->lock);
2888 return rc;
2889 }
2890 }
2891
2892 if (intstat & FIT_ISH_MSG_FROM_DEV)
2893 skd_isr_msg_from_dev(skdev);
2894 }
2895 }
2896
2897 if (unlikely(flush_enqueued))
2898 skd_request_fn(skdev->queue);
2899
2900 if (deferred)
2901 schedule_work(&skdev->completion_worker);
2902 else if (!flush_enqueued)
2903 skd_request_fn(skdev->queue);
2904
2905 spin_unlock(&skdev->lock);
2906
2907 return rc;
2908}
2909
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002910static void skd_drive_fault(struct skd_device *skdev)
2911{
2912 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002913 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002914}
2915
2916static void skd_drive_disappeared(struct skd_device *skdev)
2917{
2918 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002919 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002920}
2921
2922static void skd_isr_fwstate(struct skd_device *skdev)
2923{
2924 u32 sense;
2925 u32 state;
2926 u32 mtd;
2927 int prev_driver_state = skdev->state;
2928
2929 sense = SKD_READL(skdev, FIT_STATUS);
2930 state = sense & FIT_SR_DRIVE_STATE_MASK;
2931
Bart Van Asschef98806d2017-08-17 13:12:58 -07002932 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
2933 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
2934 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002935
2936 skdev->drive_state = state;
2937
2938 switch (skdev->drive_state) {
2939 case FIT_SR_DRIVE_INIT:
2940 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
2941 skd_disable_interrupts(skdev);
2942 break;
2943 }
2944 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
2945 skd_recover_requests(skdev, 0);
2946 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2947 skdev->timer_countdown = SKD_STARTING_TIMO;
2948 skdev->state = SKD_DRVR_STATE_STARTING;
2949 skd_soft_reset(skdev);
2950 break;
2951 }
2952 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2953 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2954 skdev->last_mtd = mtd;
2955 break;
2956
2957 case FIT_SR_DRIVE_ONLINE:
2958 skdev->cur_max_queue_depth = skd_max_queue_depth;
2959 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2960 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2961
2962 skdev->queue_low_water_mark =
2963 skdev->cur_max_queue_depth * 2 / 3 + 1;
2964 if (skdev->queue_low_water_mark < 1)
2965 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002966 dev_info(&skdev->pdev->dev,
2967 "Queue depth limit=%d dev=%d lowat=%d\n",
2968 skdev->cur_max_queue_depth,
2969 skdev->dev_max_queue_depth,
2970 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002971
2972 skd_refresh_device_data(skdev);
2973 break;
2974
2975 case FIT_SR_DRIVE_BUSY:
2976 skdev->state = SKD_DRVR_STATE_BUSY;
2977 skdev->timer_countdown = SKD_BUSY_TIMO;
2978 skd_quiesce_dev(skdev);
2979 break;
2980 case FIT_SR_DRIVE_BUSY_SANITIZE:
2981 /* set timer for 3 seconds, we'll abort any unfinished
2982 * commands after that expires
2983 */
2984 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2985 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002986 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002987 break;
2988 case FIT_SR_DRIVE_BUSY_ERASE:
2989 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2990 skdev->timer_countdown = SKD_BUSY_TIMO;
2991 break;
2992 case FIT_SR_DRIVE_OFFLINE:
2993 skdev->state = SKD_DRVR_STATE_IDLE;
2994 break;
2995 case FIT_SR_DRIVE_SOFT_RESET:
2996 switch (skdev->state) {
2997 case SKD_DRVR_STATE_STARTING:
2998 case SKD_DRVR_STATE_RESTARTING:
2999 /* Expected by a caller of skd_soft_reset() */
3000 break;
3001 default:
3002 skdev->state = SKD_DRVR_STATE_RESTARTING;
3003 break;
3004 }
3005 break;
3006 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003007 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003008 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3009 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3010 break;
3011
3012 case FIT_SR_DRIVE_DEGRADED:
3013 case FIT_SR_PCIE_LINK_DOWN:
3014 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3015 break;
3016
3017 case FIT_SR_DRIVE_FAULT:
3018 skd_drive_fault(skdev);
3019 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003020 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003021 break;
3022
3023 /* PCIe bus returned all Fs? */
3024 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003025 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
3026 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003027 skd_drive_disappeared(skdev);
3028 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003029 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003030 break;
3031 default:
3032 /*
3033 * Uknown FW State. Wait for a state we recognize.
3034 */
3035 break;
3036 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003037 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3038 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3039 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003040}
3041
3042static void skd_recover_requests(struct skd_device *skdev, int requeue)
3043{
3044 int i;
3045
3046 for (i = 0; i < skdev->num_req_context; i++) {
3047 struct skd_request_context *skreq = &skdev->skreq_table[i];
3048
3049 if (skreq->state == SKD_REQ_STATE_BUSY) {
3050 skd_log_skreq(skdev, skreq, "recover");
3051
3052 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003053 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003054
3055 /* Release DMA resources for the request. */
3056 if (skreq->n_sg > 0)
3057 skd_postop_sg_list(skdev, skreq);
3058
Jens Axboefcd37eb2013-11-01 10:14:56 -06003059 if (requeue &&
3060 (unsigned long) ++skreq->req->special <
3061 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003062 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003063 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003064 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003065
Jens Axboefcd37eb2013-11-01 10:14:56 -06003066 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003067
3068 skreq->state = SKD_REQ_STATE_IDLE;
3069 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003070 }
3071 if (i > 0)
3072 skreq[-1].next = skreq;
3073 skreq->next = NULL;
3074 }
3075 skdev->skreq_free_list = skdev->skreq_table;
3076
3077 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3078 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3079
3080 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3081 skd_log_skmsg(skdev, skmsg, "salvaged");
3082 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3083 skmsg->state = SKD_MSG_STATE_IDLE;
3084 skmsg->id += SKD_ID_INCR;
3085 }
3086 if (i > 0)
3087 skmsg[-1].next = skmsg;
3088 skmsg->next = NULL;
3089 }
3090 skdev->skmsg_free_list = skdev->skmsg_table;
3091
3092 for (i = 0; i < skdev->n_special; i++) {
3093 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3094
3095 /* If orphaned, reclaim it because it has already been reported
3096 * to the process as an error (it was just waiting for
3097 * a completion that didn't come, and now it will never come)
3098 * If busy, change to a state that will cause it to error
3099 * out in the wait routine and let it do the normal
3100 * reporting and reclaiming
3101 */
3102 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3103 if (skspcl->orphaned) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003104 dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
3105 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003106 skd_release_special(skdev, skspcl);
3107 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003108 dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
3109 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003110 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3111 }
3112 }
3113 }
3114 skdev->skspcl_free_list = skdev->skspcl_table;
3115
3116 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3117 skdev->timeout_slot[i] = 0;
3118
3119 skdev->in_flight = 0;
3120}
3121
3122static void skd_isr_msg_from_dev(struct skd_device *skdev)
3123{
3124 u32 mfd;
3125 u32 mtd;
3126 u32 data;
3127
3128 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3129
Bart Van Asschef98806d2017-08-17 13:12:58 -07003130 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
3131 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003132
3133 /* ignore any mtd that is an ack for something we didn't send */
3134 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3135 return;
3136
3137 switch (FIT_MXD_TYPE(mfd)) {
3138 case FIT_MTD_FITFW_INIT:
3139 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3140
3141 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003142 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
3143 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
3144 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
3145 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003146 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3147 skd_soft_reset(skdev);
3148 break;
3149 }
3150 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3151 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3152 skdev->last_mtd = mtd;
3153 break;
3154
3155 case FIT_MTD_GET_CMDQ_DEPTH:
3156 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3157 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3158 SKD_N_COMPLETION_ENTRY);
3159 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3160 skdev->last_mtd = mtd;
3161 break;
3162
3163 case FIT_MTD_SET_COMPQ_DEPTH:
3164 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3165 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3166 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3167 skdev->last_mtd = mtd;
3168 break;
3169
3170 case FIT_MTD_SET_COMPQ_ADDR:
3171 skd_reset_skcomp(skdev);
3172 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3173 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3174 skdev->last_mtd = mtd;
3175 break;
3176
3177 case FIT_MTD_CMD_LOG_HOST_ID:
3178 skdev->connect_time_stamp = get_seconds();
3179 data = skdev->connect_time_stamp & 0xFFFF;
3180 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3181 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3182 skdev->last_mtd = mtd;
3183 break;
3184
3185 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3186 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3187 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3188 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3189 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3190 skdev->last_mtd = mtd;
3191 break;
3192
3193 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3194 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3195 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3196 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3197 skdev->last_mtd = mtd;
3198
Bart Van Asschef98806d2017-08-17 13:12:58 -07003199 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
3200 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003201 break;
3202
3203 case FIT_MTD_ARM_QUEUE:
3204 skdev->last_mtd = 0;
3205 /*
3206 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3207 */
3208 break;
3209
3210 default:
3211 break;
3212 }
3213}
3214
3215static void skd_disable_interrupts(struct skd_device *skdev)
3216{
3217 u32 sense;
3218
3219 sense = SKD_READL(skdev, FIT_CONTROL);
3220 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3221 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003222 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003223
3224 /* Note that the 1s is written. A 1-bit means
3225 * disable, a 0 means enable.
3226 */
3227 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3228}
3229
3230static void skd_enable_interrupts(struct skd_device *skdev)
3231{
3232 u32 val;
3233
3234 /* unmask interrupts first */
3235 val = FIT_ISH_FW_STATE_CHANGE +
3236 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3237
3238 /* Note that the compliment of mask is written. A 1-bit means
3239 * disable, a 0 means enable. */
3240 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003241 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003242
3243 val = SKD_READL(skdev, FIT_CONTROL);
3244 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003245 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003246 SKD_WRITEL(skdev, val, FIT_CONTROL);
3247}
3248
3249/*
3250 *****************************************************************************
3251 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3252 *****************************************************************************
3253 */
3254
3255static void skd_soft_reset(struct skd_device *skdev)
3256{
3257 u32 val;
3258
3259 val = SKD_READL(skdev, FIT_CONTROL);
3260 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003261 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003262 SKD_WRITEL(skdev, val, FIT_CONTROL);
3263}
3264
3265static void skd_start_device(struct skd_device *skdev)
3266{
3267 unsigned long flags;
3268 u32 sense;
3269 u32 state;
3270
3271 spin_lock_irqsave(&skdev->lock, flags);
3272
3273 /* ack all ghost interrupts */
3274 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3275
3276 sense = SKD_READL(skdev, FIT_STATUS);
3277
Bart Van Asschef98806d2017-08-17 13:12:58 -07003278 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003279
3280 state = sense & FIT_SR_DRIVE_STATE_MASK;
3281 skdev->drive_state = state;
3282 skdev->last_mtd = 0;
3283
3284 skdev->state = SKD_DRVR_STATE_STARTING;
3285 skdev->timer_countdown = SKD_STARTING_TIMO;
3286
3287 skd_enable_interrupts(skdev);
3288
3289 switch (skdev->drive_state) {
3290 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003291 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003292 break;
3293
3294 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003295 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003296 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3297 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3298 break;
3299
3300 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003301 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003302 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3303 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3304 break;
3305
3306 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003307 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003308 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3309 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3310 break;
3311
3312 case FIT_SR_DRIVE_INIT:
3313 case FIT_SR_DRIVE_ONLINE:
3314 skd_soft_reset(skdev);
3315 break;
3316
3317 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003318 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003319 skdev->state = SKD_DRVR_STATE_BUSY;
3320 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3321 break;
3322
3323 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003324 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003325 break;
3326
3327 case FIT_SR_DRIVE_FAULT:
3328 /* Fault state is bad...soft reset won't do it...
3329 * Hard reset, maybe, but does it work on device?
3330 * For now, just fault so the system doesn't hang.
3331 */
3332 skd_drive_fault(skdev);
3333 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003334 dev_dbg(&skdev->pdev->dev, "starting queue\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 case 0xFF:
3341 /* Most likely the device isn't there or isn't responding
3342 * to the BAR1 addresses. */
3343 skd_drive_disappeared(skdev);
3344 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003345 dev_dbg(&skdev->pdev->dev,
3346 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003347 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003348 skdev->gendisk_on = -1;
3349 wake_up_interruptible(&skdev->waitq);
3350 break;
3351
3352 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003353 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
3354 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003355 break;
3356 }
3357
3358 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003359 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003360
3361 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003362 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003363
3364 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003365 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003366
3367 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003368 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003369
3370 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003371 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003372
3373 spin_unlock_irqrestore(&skdev->lock, flags);
3374}
3375
3376static void skd_stop_device(struct skd_device *skdev)
3377{
3378 unsigned long flags;
3379 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3380 u32 dev_state;
3381 int i;
3382
3383 spin_lock_irqsave(&skdev->lock, flags);
3384
3385 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003386 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003387 goto stop_out;
3388 }
3389
3390 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003391 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003392 goto stop_out;
3393 }
3394
3395 skdev->state = SKD_DRVR_STATE_SYNCING;
3396 skdev->sync_done = 0;
3397
3398 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3399
3400 spin_unlock_irqrestore(&skdev->lock, flags);
3401
3402 wait_event_interruptible_timeout(skdev->waitq,
3403 (skdev->sync_done), (10 * HZ));
3404
3405 spin_lock_irqsave(&skdev->lock, flags);
3406
3407 switch (skdev->sync_done) {
3408 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003409 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003410 break;
3411 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003412 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003413 break;
3414 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003415 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003416 }
3417
3418stop_out:
3419 skdev->state = SKD_DRVR_STATE_STOPPING;
3420 spin_unlock_irqrestore(&skdev->lock, flags);
3421
3422 skd_kill_timer(skdev);
3423
3424 spin_lock_irqsave(&skdev->lock, flags);
3425 skd_disable_interrupts(skdev);
3426
3427 /* ensure all ints on device are cleared */
3428 /* soft reset the device to unload with a clean slate */
3429 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3430 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3431
3432 spin_unlock_irqrestore(&skdev->lock, flags);
3433
3434 /* poll every 100ms, 1 second timeout */
3435 for (i = 0; i < 10; i++) {
3436 dev_state =
3437 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3438 if (dev_state == FIT_SR_DRIVE_INIT)
3439 break;
3440 set_current_state(TASK_INTERRUPTIBLE);
3441 schedule_timeout(msecs_to_jiffies(100));
3442 }
3443
3444 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07003445 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
3446 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003447}
3448
3449/* assume spinlock is held */
3450static void skd_restart_device(struct skd_device *skdev)
3451{
3452 u32 state;
3453
3454 /* ack all ghost interrupts */
3455 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3456
3457 state = SKD_READL(skdev, FIT_STATUS);
3458
Bart Van Asschef98806d2017-08-17 13:12:58 -07003459 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003460
3461 state &= FIT_SR_DRIVE_STATE_MASK;
3462 skdev->drive_state = state;
3463 skdev->last_mtd = 0;
3464
3465 skdev->state = SKD_DRVR_STATE_RESTARTING;
3466 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3467
3468 skd_soft_reset(skdev);
3469}
3470
3471/* assume spinlock is held */
3472static int skd_quiesce_dev(struct skd_device *skdev)
3473{
3474 int rc = 0;
3475
3476 switch (skdev->state) {
3477 case SKD_DRVR_STATE_BUSY:
3478 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003479 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003480 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003481 break;
3482 case SKD_DRVR_STATE_ONLINE:
3483 case SKD_DRVR_STATE_STOPPING:
3484 case SKD_DRVR_STATE_SYNCING:
3485 case SKD_DRVR_STATE_PAUSING:
3486 case SKD_DRVR_STATE_PAUSED:
3487 case SKD_DRVR_STATE_STARTING:
3488 case SKD_DRVR_STATE_RESTARTING:
3489 case SKD_DRVR_STATE_RESUMING:
3490 default:
3491 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003492 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
3493 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003494 }
3495 return rc;
3496}
3497
3498/* assume spinlock is held */
3499static int skd_unquiesce_dev(struct skd_device *skdev)
3500{
3501 int prev_driver_state = skdev->state;
3502
3503 skd_log_skdev(skdev, "unquiesce");
3504 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003505 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003506 return 0;
3507 }
3508 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3509 /*
3510 * If there has been an state change to other than
3511 * ONLINE, we will rely on controller state change
3512 * to come back online and restart the queue.
3513 * The BUSY state means that driver is ready to
3514 * continue normal processing but waiting for controller
3515 * to become available.
3516 */
3517 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003518 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003519 return 0;
3520 }
3521
3522 /*
3523 * Drive has just come online, driver is either in startup,
3524 * paused performing a task, or bust waiting for hardware.
3525 */
3526 switch (skdev->state) {
3527 case SKD_DRVR_STATE_PAUSED:
3528 case SKD_DRVR_STATE_BUSY:
3529 case SKD_DRVR_STATE_BUSY_IMMINENT:
3530 case SKD_DRVR_STATE_BUSY_ERASE:
3531 case SKD_DRVR_STATE_STARTING:
3532 case SKD_DRVR_STATE_RESTARTING:
3533 case SKD_DRVR_STATE_FAULT:
3534 case SKD_DRVR_STATE_IDLE:
3535 case SKD_DRVR_STATE_LOAD:
3536 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003537 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3538 skd_skdev_state_to_str(prev_driver_state),
3539 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3540 skdev->state);
3541 dev_dbg(&skdev->pdev->dev,
3542 "**** device ONLINE...starting block queue\n");
3543 dev_dbg(&skdev->pdev->dev, "starting queue\n");
3544 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003545 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003546 skdev->gendisk_on = 1;
3547 wake_up_interruptible(&skdev->waitq);
3548 break;
3549
3550 case SKD_DRVR_STATE_DISAPPEARED:
3551 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003552 dev_dbg(&skdev->pdev->dev,
3553 "**** driver state %d, not implemented\n",
3554 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003555 return -EBUSY;
3556 }
3557 return 0;
3558}
3559
3560/*
3561 *****************************************************************************
3562 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3563 *****************************************************************************
3564 */
3565
3566static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3567{
3568 struct skd_device *skdev = skd_host_data;
3569 unsigned long flags;
3570
3571 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003572 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3573 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3574 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
3575 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003576 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3577 spin_unlock_irqrestore(&skdev->lock, flags);
3578 return IRQ_HANDLED;
3579}
3580
3581static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3582{
3583 struct skd_device *skdev = skd_host_data;
3584 unsigned long flags;
3585
3586 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003587 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3588 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003589 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3590 skd_isr_fwstate(skdev);
3591 spin_unlock_irqrestore(&skdev->lock, flags);
3592 return IRQ_HANDLED;
3593}
3594
3595static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3596{
3597 struct skd_device *skdev = skd_host_data;
3598 unsigned long flags;
3599 int flush_enqueued = 0;
3600 int deferred;
3601
3602 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003603 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3604 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003605 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3606 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3607 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003608 if (flush_enqueued)
3609 skd_request_fn(skdev->queue);
3610
3611 if (deferred)
3612 schedule_work(&skdev->completion_worker);
3613 else if (!flush_enqueued)
3614 skd_request_fn(skdev->queue);
3615
3616 spin_unlock_irqrestore(&skdev->lock, flags);
3617
3618 return IRQ_HANDLED;
3619}
3620
3621static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3622{
3623 struct skd_device *skdev = skd_host_data;
3624 unsigned long flags;
3625
3626 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003627 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3628 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003629 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3630 skd_isr_msg_from_dev(skdev);
3631 spin_unlock_irqrestore(&skdev->lock, flags);
3632 return IRQ_HANDLED;
3633}
3634
3635static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3636{
3637 struct skd_device *skdev = skd_host_data;
3638 unsigned long flags;
3639
3640 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003641 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3642 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003643 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3644 spin_unlock_irqrestore(&skdev->lock, flags);
3645 return IRQ_HANDLED;
3646}
3647
3648/*
3649 *****************************************************************************
3650 * PCIe MSI/MSI-X SETUP
3651 *****************************************************************************
3652 */
3653
3654struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003655 char isr_name[30];
3656};
3657
3658struct skd_init_msix_entry {
3659 const char *name;
3660 irq_handler_t handler;
3661};
3662
3663#define SKD_MAX_MSIX_COUNT 13
3664#define SKD_MIN_MSIX_COUNT 7
3665#define SKD_BASE_MSIX_IRQ 4
3666
3667static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3668 { "(DMA 0)", skd_reserved_isr },
3669 { "(DMA 1)", skd_reserved_isr },
3670 { "(DMA 2)", skd_reserved_isr },
3671 { "(DMA 3)", skd_reserved_isr },
3672 { "(State Change)", skd_statec_isr },
3673 { "(COMPL_Q)", skd_comp_q },
3674 { "(MSG)", skd_msg_isr },
3675 { "(Reserved)", skd_reserved_isr },
3676 { "(Reserved)", skd_reserved_isr },
3677 { "(Queue Full 0)", skd_qfull_isr },
3678 { "(Queue Full 1)", skd_qfull_isr },
3679 { "(Queue Full 2)", skd_qfull_isr },
3680 { "(Queue Full 3)", skd_qfull_isr },
3681};
3682
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003683static int skd_acquire_msix(struct skd_device *skdev)
3684{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003685 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003686 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003687
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003688 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3689 PCI_IRQ_MSIX);
3690 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003691 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003692 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003693 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003694
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003695 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3696 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003697 if (!skdev->msix_entries) {
3698 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003699 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003700 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003701 }
3702
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003704 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3705 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3706
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003707 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3708 "%s%d-msix %s", DRV_NAME, skdev->devno,
3709 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003710
3711 rc = devm_request_irq(&skdev->pdev->dev,
3712 pci_irq_vector(skdev->pdev, i),
3713 msix_entries[i].handler, 0,
3714 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003715 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003716 dev_err(&skdev->pdev->dev,
3717 "Unable to register(%d) MSI-X handler %d: %s\n",
3718 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003719 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003720 }
3721 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003722
Bart Van Asschef98806d2017-08-17 13:12:58 -07003723 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
3724 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003725 return 0;
3726
3727msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003728 while (--i >= 0)
3729 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003730out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003731 kfree(skdev->msix_entries);
3732 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003733 return rc;
3734}
3735
3736static int skd_acquire_irq(struct skd_device *skdev)
3737{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003738 struct pci_dev *pdev = skdev->pdev;
3739 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003740 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003741
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003742 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003743 rc = skd_acquire_msix(skdev);
3744 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003745 return 0;
3746
Bart Van Asschef98806d2017-08-17 13:12:58 -07003747 dev_err(&skdev->pdev->dev,
3748 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003749 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003750
3751 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3752 skdev->devno);
3753
3754 if (skd_isr_type != SKD_IRQ_LEGACY)
3755 irq_flag |= PCI_IRQ_MSI;
3756 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3757 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003758 dev_err(&skdev->pdev->dev,
3759 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003760 return rc;
3761 }
3762
3763 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3764 pdev->msi_enabled ? 0 : IRQF_SHARED,
3765 skdev->isr_name, skdev);
3766 if (rc) {
3767 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003768 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
3769 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003770 return rc;
3771 }
3772
3773 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003774}
3775
3776static void skd_release_irq(struct skd_device *skdev)
3777{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003778 struct pci_dev *pdev = skdev->pdev;
3779
3780 if (skdev->msix_entries) {
3781 int i;
3782
3783 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3784 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3785 skdev);
3786 }
3787
3788 kfree(skdev->msix_entries);
3789 skdev->msix_entries = NULL;
3790 } else {
3791 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003792 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003793
3794 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003795}
3796
3797/*
3798 *****************************************************************************
3799 * CONSTRUCT
3800 *****************************************************************************
3801 */
3802
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003803static int skd_cons_skcomp(struct skd_device *skdev)
3804{
3805 int rc = 0;
3806 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003807
Bart Van Asschef98806d2017-08-17 13:12:58 -07003808 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003809 "comp pci_alloc, total bytes %zd entries %d\n",
3810 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003811
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003812 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07003813 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003814
3815 if (skcomp == NULL) {
3816 rc = -ENOMEM;
3817 goto err_out;
3818 }
3819
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003820 skdev->skcomp_table = skcomp;
3821 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3822 sizeof(*skcomp) *
3823 SKD_N_COMPLETION_ENTRY);
3824
3825err_out:
3826 return rc;
3827}
3828
3829static int skd_cons_skmsg(struct skd_device *skdev)
3830{
3831 int rc = 0;
3832 u32 i;
3833
Bart Van Asschef98806d2017-08-17 13:12:58 -07003834 dev_dbg(&skdev->pdev->dev,
3835 "skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3836 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
3837 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003838
3839 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3840 *skdev->num_fitmsg_context, GFP_KERNEL);
3841 if (skdev->skmsg_table == NULL) {
3842 rc = -ENOMEM;
3843 goto err_out;
3844 }
3845
3846 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3847 struct skd_fitmsg_context *skmsg;
3848
3849 skmsg = &skdev->skmsg_table[i];
3850
3851 skmsg->id = i + SKD_ID_FIT_MSG;
3852
3853 skmsg->state = SKD_MSG_STATE_IDLE;
3854 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07003855 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003856 &skmsg->mb_dma_address);
3857
3858 if (skmsg->msg_buf == NULL) {
3859 rc = -ENOMEM;
3860 goto err_out;
3861 }
3862
Bart Van Assche6507f432017-08-17 13:13:06 -07003863 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
3864 (FIT_QCMD_ALIGN - 1),
3865 "not aligned: msg_buf %p mb_dma_address %#llx\n",
3866 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003867 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3868
3869 skmsg->next = &skmsg[1];
3870 }
3871
3872 /* Free list is in order starting with the 0th entry. */
3873 skdev->skmsg_table[i - 1].next = NULL;
3874 skdev->skmsg_free_list = skdev->skmsg_table;
3875
3876err_out:
3877 return rc;
3878}
3879
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003880static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
3881 u32 n_sg,
3882 dma_addr_t *ret_dma_addr)
3883{
3884 struct fit_sg_descriptor *sg_list;
3885 u32 nbytes;
3886
3887 nbytes = sizeof(*sg_list) * n_sg;
3888
3889 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
3890
3891 if (sg_list != NULL) {
3892 uint64_t dma_address = *ret_dma_addr;
3893 u32 i;
3894
3895 memset(sg_list, 0, nbytes);
3896
3897 for (i = 0; i < n_sg - 1; i++) {
3898 uint64_t ndp_off;
3899 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
3900
3901 sg_list[i].next_desc_ptr = dma_address + ndp_off;
3902 }
3903 sg_list[i].next_desc_ptr = 0LL;
3904 }
3905
3906 return sg_list;
3907}
3908
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003909static int skd_cons_skreq(struct skd_device *skdev)
3910{
3911 int rc = 0;
3912 u32 i;
3913
Bart Van Asschef98806d2017-08-17 13:12:58 -07003914 dev_dbg(&skdev->pdev->dev,
3915 "skreq_table kzalloc, struct %lu, count %u total %lu\n",
3916 sizeof(struct skd_request_context), skdev->num_req_context,
3917 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003918
3919 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
3920 * skdev->num_req_context, GFP_KERNEL);
3921 if (skdev->skreq_table == NULL) {
3922 rc = -ENOMEM;
3923 goto err_out;
3924 }
3925
Bart Van Asschef98806d2017-08-17 13:12:58 -07003926 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
3927 skdev->sgs_per_request, sizeof(struct scatterlist),
3928 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003929
3930 for (i = 0; i < skdev->num_req_context; i++) {
3931 struct skd_request_context *skreq;
3932
3933 skreq = &skdev->skreq_table[i];
3934
3935 skreq->id = i + SKD_ID_RW_REQUEST;
3936 skreq->state = SKD_REQ_STATE_IDLE;
3937
3938 skreq->sg = kzalloc(sizeof(struct scatterlist) *
3939 skdev->sgs_per_request, GFP_KERNEL);
3940 if (skreq->sg == NULL) {
3941 rc = -ENOMEM;
3942 goto err_out;
3943 }
3944 sg_init_table(skreq->sg, skdev->sgs_per_request);
3945
3946 skreq->sksg_list = skd_cons_sg_list(skdev,
3947 skdev->sgs_per_request,
3948 &skreq->sksg_dma_address);
3949
3950 if (skreq->sksg_list == NULL) {
3951 rc = -ENOMEM;
3952 goto err_out;
3953 }
3954
3955 skreq->next = &skreq[1];
3956 }
3957
3958 /* Free list is in order starting with the 0th entry. */
3959 skdev->skreq_table[i - 1].next = NULL;
3960 skdev->skreq_free_list = skdev->skreq_table;
3961
3962err_out:
3963 return rc;
3964}
3965
3966static int skd_cons_skspcl(struct skd_device *skdev)
3967{
3968 int rc = 0;
3969 u32 i, nbytes;
3970
Bart Van Asschef98806d2017-08-17 13:12:58 -07003971 dev_dbg(&skdev->pdev->dev,
3972 "skspcl_table kzalloc, struct %lu, count %u total %lu\n",
3973 sizeof(struct skd_special_context), skdev->n_special,
3974 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003975
3976 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
3977 * skdev->n_special, GFP_KERNEL);
3978 if (skdev->skspcl_table == NULL) {
3979 rc = -ENOMEM;
3980 goto err_out;
3981 }
3982
3983 for (i = 0; i < skdev->n_special; i++) {
3984 struct skd_special_context *skspcl;
3985
3986 skspcl = &skdev->skspcl_table[i];
3987
3988 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
3989 skspcl->req.state = SKD_REQ_STATE_IDLE;
3990
3991 skspcl->req.next = &skspcl[1].req;
3992
3993 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3994
Joe Perchesa5bbf612014-08-08 14:24:12 -07003995 skspcl->msg_buf =
3996 pci_zalloc_consistent(skdev->pdev, nbytes,
3997 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003998 if (skspcl->msg_buf == NULL) {
3999 rc = -ENOMEM;
4000 goto err_out;
4001 }
4002
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004003 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4004 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4005 if (skspcl->req.sg == NULL) {
4006 rc = -ENOMEM;
4007 goto err_out;
4008 }
4009
4010 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4011 SKD_N_SG_PER_SPECIAL,
4012 &skspcl->req.
4013 sksg_dma_address);
4014 if (skspcl->req.sksg_list == NULL) {
4015 rc = -ENOMEM;
4016 goto err_out;
4017 }
4018 }
4019
4020 /* Free list is in order starting with the 0th entry. */
4021 skdev->skspcl_table[i - 1].req.next = NULL;
4022 skdev->skspcl_free_list = skdev->skspcl_table;
4023
4024 return rc;
4025
4026err_out:
4027 return rc;
4028}
4029
4030static int skd_cons_sksb(struct skd_device *skdev)
4031{
4032 int rc = 0;
4033 struct skd_special_context *skspcl;
4034 u32 nbytes;
4035
4036 skspcl = &skdev->internal_skspcl;
4037
4038 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4039 skspcl->req.state = SKD_REQ_STATE_IDLE;
4040
4041 nbytes = SKD_N_INTERNAL_BYTES;
4042
Joe Perchesa5bbf612014-08-08 14:24:12 -07004043 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4044 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004045 if (skspcl->data_buf == NULL) {
4046 rc = -ENOMEM;
4047 goto err_out;
4048 }
4049
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004050 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004051 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4052 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004053 if (skspcl->msg_buf == NULL) {
4054 rc = -ENOMEM;
4055 goto err_out;
4056 }
4057
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004058 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4059 &skspcl->req.sksg_dma_address);
4060 if (skspcl->req.sksg_list == NULL) {
4061 rc = -ENOMEM;
4062 goto err_out;
4063 }
4064
4065 if (!skd_format_internal_skspcl(skdev)) {
4066 rc = -EINVAL;
4067 goto err_out;
4068 }
4069
4070err_out:
4071 return rc;
4072}
4073
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004074static int skd_cons_disk(struct skd_device *skdev)
4075{
4076 int rc = 0;
4077 struct gendisk *disk;
4078 struct request_queue *q;
4079 unsigned long flags;
4080
4081 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4082 if (!disk) {
4083 rc = -ENOMEM;
4084 goto err_out;
4085 }
4086
4087 skdev->disk = disk;
4088 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4089
4090 disk->major = skdev->major;
4091 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4092 disk->fops = &skd_blockdev_ops;
4093 disk->private_data = skdev;
4094
Jens Axboefcd37eb2013-11-01 10:14:56 -06004095 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004096 if (!q) {
4097 rc = -ENOMEM;
4098 goto err_out;
4099 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004100 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004101
4102 skdev->queue = q;
4103 disk->queue = q;
4104 q->queuedata = skdev;
4105
Jens Axboe6975f732016-03-30 10:11:42 -06004106 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004107 blk_queue_max_segments(q, skdev->sgs_per_request);
4108 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4109
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004110 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004111 blk_queue_io_opt(q, 8192);
4112
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004113 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004114 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004115
4116 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004117 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06004118 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004119 spin_unlock_irqrestore(&skdev->lock, flags);
4120
4121err_out:
4122 return rc;
4123}
4124
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004125#define SKD_N_DEV_TABLE 16u
4126static u32 skd_next_devno;
4127
4128static struct skd_device *skd_construct(struct pci_dev *pdev)
4129{
4130 struct skd_device *skdev;
4131 int blk_major = skd_major;
4132 int rc;
4133
4134 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4135
4136 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004137 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004138 return NULL;
4139 }
4140
4141 skdev->state = SKD_DRVR_STATE_LOAD;
4142 skdev->pdev = pdev;
4143 skdev->devno = skd_next_devno++;
4144 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004145 skdev->dev_max_queue_depth = 0;
4146
4147 skdev->num_req_context = skd_max_queue_depth;
4148 skdev->num_fitmsg_context = skd_max_queue_depth;
4149 skdev->n_special = skd_max_pass_thru;
4150 skdev->cur_max_queue_depth = 1;
4151 skdev->queue_low_water_mark = 1;
4152 skdev->proto_ver = 99;
4153 skdev->sgs_per_request = skd_sgs_per_request;
4154 skdev->dbg_level = skd_dbg_level;
4155
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004156 spin_lock_init(&skdev->lock);
4157
4158 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4159
Bart Van Asschef98806d2017-08-17 13:12:58 -07004160 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004161 rc = skd_cons_skcomp(skdev);
4162 if (rc < 0)
4163 goto err_out;
4164
Bart Van Asschef98806d2017-08-17 13:12:58 -07004165 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004166 rc = skd_cons_skmsg(skdev);
4167 if (rc < 0)
4168 goto err_out;
4169
Bart Van Asschef98806d2017-08-17 13:12:58 -07004170 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004171 rc = skd_cons_skreq(skdev);
4172 if (rc < 0)
4173 goto err_out;
4174
Bart Van Asschef98806d2017-08-17 13:12:58 -07004175 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004176 rc = skd_cons_skspcl(skdev);
4177 if (rc < 0)
4178 goto err_out;
4179
Bart Van Asschef98806d2017-08-17 13:12:58 -07004180 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004181 rc = skd_cons_sksb(skdev);
4182 if (rc < 0)
4183 goto err_out;
4184
Bart Van Asschef98806d2017-08-17 13:12:58 -07004185 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004186 rc = skd_cons_disk(skdev);
4187 if (rc < 0)
4188 goto err_out;
4189
Bart Van Asschef98806d2017-08-17 13:12:58 -07004190 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004191 return skdev;
4192
4193err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07004194 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004195 skd_destruct(skdev);
4196 return NULL;
4197}
4198
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004199/*
4200 *****************************************************************************
4201 * DESTRUCT (FREE)
4202 *****************************************************************************
4203 */
4204
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004205static void skd_free_skcomp(struct skd_device *skdev)
4206{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07004207 if (skdev->skcomp_table)
4208 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004209 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004210
4211 skdev->skcomp_table = NULL;
4212 skdev->cq_dma_address = 0;
4213}
4214
4215static void skd_free_skmsg(struct skd_device *skdev)
4216{
4217 u32 i;
4218
4219 if (skdev->skmsg_table == NULL)
4220 return;
4221
4222 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4223 struct skd_fitmsg_context *skmsg;
4224
4225 skmsg = &skdev->skmsg_table[i];
4226
4227 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004228 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4229 skmsg->msg_buf,
4230 skmsg->mb_dma_address);
4231 }
4232 skmsg->msg_buf = NULL;
4233 skmsg->mb_dma_address = 0;
4234 }
4235
4236 kfree(skdev->skmsg_table);
4237 skdev->skmsg_table = NULL;
4238}
4239
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004240static void skd_free_sg_list(struct skd_device *skdev,
4241 struct fit_sg_descriptor *sg_list,
4242 u32 n_sg, dma_addr_t dma_addr)
4243{
4244 if (sg_list != NULL) {
4245 u32 nbytes;
4246
4247 nbytes = sizeof(*sg_list) * n_sg;
4248
4249 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4250 }
4251}
4252
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004253static void skd_free_skreq(struct skd_device *skdev)
4254{
4255 u32 i;
4256
4257 if (skdev->skreq_table == NULL)
4258 return;
4259
4260 for (i = 0; i < skdev->num_req_context; i++) {
4261 struct skd_request_context *skreq;
4262
4263 skreq = &skdev->skreq_table[i];
4264
4265 skd_free_sg_list(skdev, skreq->sksg_list,
4266 skdev->sgs_per_request,
4267 skreq->sksg_dma_address);
4268
4269 skreq->sksg_list = NULL;
4270 skreq->sksg_dma_address = 0;
4271
4272 kfree(skreq->sg);
4273 }
4274
4275 kfree(skdev->skreq_table);
4276 skdev->skreq_table = NULL;
4277}
4278
4279static void skd_free_skspcl(struct skd_device *skdev)
4280{
4281 u32 i;
4282 u32 nbytes;
4283
4284 if (skdev->skspcl_table == NULL)
4285 return;
4286
4287 for (i = 0; i < skdev->n_special; i++) {
4288 struct skd_special_context *skspcl;
4289
4290 skspcl = &skdev->skspcl_table[i];
4291
4292 if (skspcl->msg_buf != NULL) {
4293 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4294 pci_free_consistent(skdev->pdev, nbytes,
4295 skspcl->msg_buf,
4296 skspcl->mb_dma_address);
4297 }
4298
4299 skspcl->msg_buf = NULL;
4300 skspcl->mb_dma_address = 0;
4301
4302 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4303 SKD_N_SG_PER_SPECIAL,
4304 skspcl->req.sksg_dma_address);
4305
4306 skspcl->req.sksg_list = NULL;
4307 skspcl->req.sksg_dma_address = 0;
4308
4309 kfree(skspcl->req.sg);
4310 }
4311
4312 kfree(skdev->skspcl_table);
4313 skdev->skspcl_table = NULL;
4314}
4315
4316static void skd_free_sksb(struct skd_device *skdev)
4317{
4318 struct skd_special_context *skspcl;
4319 u32 nbytes;
4320
4321 skspcl = &skdev->internal_skspcl;
4322
4323 if (skspcl->data_buf != NULL) {
4324 nbytes = SKD_N_INTERNAL_BYTES;
4325
4326 pci_free_consistent(skdev->pdev, nbytes,
4327 skspcl->data_buf, skspcl->db_dma_address);
4328 }
4329
4330 skspcl->data_buf = NULL;
4331 skspcl->db_dma_address = 0;
4332
4333 if (skspcl->msg_buf != NULL) {
4334 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4335 pci_free_consistent(skdev->pdev, nbytes,
4336 skspcl->msg_buf, skspcl->mb_dma_address);
4337 }
4338
4339 skspcl->msg_buf = NULL;
4340 skspcl->mb_dma_address = 0;
4341
4342 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4343 skspcl->req.sksg_dma_address);
4344
4345 skspcl->req.sksg_list = NULL;
4346 skspcl->req.sksg_dma_address = 0;
4347}
4348
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004349static void skd_free_disk(struct skd_device *skdev)
4350{
4351 struct gendisk *disk = skdev->disk;
4352
Bart Van Assche7277cc62017-08-17 13:12:45 -07004353 if (disk && (disk->flags & GENHD_FL_UP))
4354 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004355
Bart Van Assche7277cc62017-08-17 13:12:45 -07004356 if (skdev->queue) {
4357 blk_cleanup_queue(skdev->queue);
4358 skdev->queue = NULL;
4359 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004360 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004361
4362 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004363 skdev->disk = NULL;
4364}
4365
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004366static void skd_destruct(struct skd_device *skdev)
4367{
4368 if (skdev == NULL)
4369 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004370
Bart Van Asschef98806d2017-08-17 13:12:58 -07004371 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004372 skd_free_disk(skdev);
4373
Bart Van Asschef98806d2017-08-17 13:12:58 -07004374 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004375 skd_free_sksb(skdev);
4376
Bart Van Asschef98806d2017-08-17 13:12:58 -07004377 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004378 skd_free_skspcl(skdev);
4379
Bart Van Asschef98806d2017-08-17 13:12:58 -07004380 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004381 skd_free_skreq(skdev);
4382
Bart Van Asschef98806d2017-08-17 13:12:58 -07004383 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004384 skd_free_skmsg(skdev);
4385
Bart Van Asschef98806d2017-08-17 13:12:58 -07004386 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004387 skd_free_skcomp(skdev);
4388
Bart Van Asschef98806d2017-08-17 13:12:58 -07004389 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004390 kfree(skdev);
4391}
4392
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004393/*
4394 *****************************************************************************
4395 * BLOCK DEVICE (BDEV) GLUE
4396 *****************************************************************************
4397 */
4398
4399static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4400{
4401 struct skd_device *skdev;
4402 u64 capacity;
4403
4404 skdev = bdev->bd_disk->private_data;
4405
Bart Van Asschef98806d2017-08-17 13:12:58 -07004406 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
4407 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004408
4409 if (skdev->read_cap_is_valid) {
4410 capacity = get_capacity(skdev->disk);
4411 geo->heads = 64;
4412 geo->sectors = 255;
4413 geo->cylinders = (capacity) / (255 * 64);
4414
4415 return 0;
4416 }
4417 return -EIO;
4418}
4419
Dan Williams0d52c7562016-06-15 19:44:20 -07004420static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004421{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004422 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07004423 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004424 return 0;
4425}
4426
4427static const struct block_device_operations skd_blockdev_ops = {
4428 .owner = THIS_MODULE,
4429 .ioctl = skd_bdev_ioctl,
4430 .getgeo = skd_bdev_getgeo,
4431};
4432
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004433/*
4434 *****************************************************************************
4435 * PCIe DRIVER GLUE
4436 *****************************************************************************
4437 */
4438
Benoit Taine9baa3c32014-08-08 15:56:03 +02004439static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004440 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4441 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4442 { 0 } /* terminate list */
4443};
4444
4445MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4446
4447static char *skd_pci_info(struct skd_device *skdev, char *str)
4448{
4449 int pcie_reg;
4450
4451 strcpy(str, "PCIe (");
4452 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4453
4454 if (pcie_reg) {
4455
4456 char lwstr[6];
4457 uint16_t pcie_lstat, lspeed, lwidth;
4458
4459 pcie_reg += 0x12;
4460 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4461 lspeed = pcie_lstat & (0xF);
4462 lwidth = (pcie_lstat & 0x3F0) >> 4;
4463
4464 if (lspeed == 1)
4465 strcat(str, "2.5GT/s ");
4466 else if (lspeed == 2)
4467 strcat(str, "5.0GT/s ");
4468 else
4469 strcat(str, "<unknown> ");
4470 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4471 strcat(str, lwstr);
4472 }
4473 return str;
4474}
4475
4476static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4477{
4478 int i;
4479 int rc = 0;
4480 char pci_str[32];
4481 struct skd_device *skdev;
4482
Bart Van Asschef98806d2017-08-17 13:12:58 -07004483 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
4484 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4485 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
4486 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004487
4488 rc = pci_enable_device(pdev);
4489 if (rc)
4490 return rc;
4491 rc = pci_request_regions(pdev, DRV_NAME);
4492 if (rc)
4493 goto err_out;
4494 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4495 if (!rc) {
4496 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004497 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4498 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004499 }
4500 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004501 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004502 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004503 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004504 goto err_out_regions;
4505 }
4506 }
4507
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004508 if (!skd_major) {
4509 rc = register_blkdev(0, DRV_NAME);
4510 if (rc < 0)
4511 goto err_out_regions;
4512 BUG_ON(!rc);
4513 skd_major = rc;
4514 }
4515
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004516 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004517 if (skdev == NULL) {
4518 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004519 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004520 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004521
4522 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004523 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004524
4525 pci_set_master(pdev);
4526 rc = pci_enable_pcie_error_reporting(pdev);
4527 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004528 dev_err(&pdev->dev,
4529 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004530 skdev->pcie_error_reporting_is_enabled = 0;
4531 } else
4532 skdev->pcie_error_reporting_is_enabled = 1;
4533
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004534 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004535
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004536 for (i = 0; i < SKD_MAX_BARS; i++) {
4537 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4538 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4539 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4540 skdev->mem_size[i]);
4541 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004542 dev_err(&pdev->dev,
4543 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004544 rc = -ENODEV;
4545 goto err_out_iounmap;
4546 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004547 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4548 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4549 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004550 }
4551
4552 rc = skd_acquire_irq(skdev);
4553 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004554 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004555 goto err_out_iounmap;
4556 }
4557
4558 rc = skd_start_timer(skdev);
4559 if (rc)
4560 goto err_out_timer;
4561
4562 init_waitqueue_head(&skdev->waitq);
4563
4564 skd_start_device(skdev);
4565
4566 rc = wait_event_interruptible_timeout(skdev->waitq,
4567 (skdev->gendisk_on),
4568 (SKD_START_WAIT_SECONDS * HZ));
4569 if (skdev->gendisk_on > 0) {
4570 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004571 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004572 rc = 0;
4573 } else {
4574 /* we timed out, something is wrong with the device,
4575 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07004576 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
4577 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004578 /* in case of no error; we timeout with ENXIO */
4579 if (!rc)
4580 rc = -ENXIO;
4581 goto err_out_timer;
4582 }
4583
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004584 return rc;
4585
4586err_out_timer:
4587 skd_stop_device(skdev);
4588 skd_release_irq(skdev);
4589
4590err_out_iounmap:
4591 for (i = 0; i < SKD_MAX_BARS; i++)
4592 if (skdev->mem_map[i])
4593 iounmap(skdev->mem_map[i]);
4594
4595 if (skdev->pcie_error_reporting_is_enabled)
4596 pci_disable_pcie_error_reporting(pdev);
4597
4598 skd_destruct(skdev);
4599
4600err_out_regions:
4601 pci_release_regions(pdev);
4602
4603err_out:
4604 pci_disable_device(pdev);
4605 pci_set_drvdata(pdev, NULL);
4606 return rc;
4607}
4608
4609static void skd_pci_remove(struct pci_dev *pdev)
4610{
4611 int i;
4612 struct skd_device *skdev;
4613
4614 skdev = pci_get_drvdata(pdev);
4615 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004616 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004617 return;
4618 }
4619 skd_stop_device(skdev);
4620 skd_release_irq(skdev);
4621
4622 for (i = 0; i < SKD_MAX_BARS; i++)
4623 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004624 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004625
4626 if (skdev->pcie_error_reporting_is_enabled)
4627 pci_disable_pcie_error_reporting(pdev);
4628
4629 skd_destruct(skdev);
4630
4631 pci_release_regions(pdev);
4632 pci_disable_device(pdev);
4633 pci_set_drvdata(pdev, NULL);
4634
4635 return;
4636}
4637
4638static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4639{
4640 int i;
4641 struct skd_device *skdev;
4642
4643 skdev = pci_get_drvdata(pdev);
4644 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004645 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004646 return -EIO;
4647 }
4648
4649 skd_stop_device(skdev);
4650
4651 skd_release_irq(skdev);
4652
4653 for (i = 0; i < SKD_MAX_BARS; i++)
4654 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004655 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004656
4657 if (skdev->pcie_error_reporting_is_enabled)
4658 pci_disable_pcie_error_reporting(pdev);
4659
4660 pci_release_regions(pdev);
4661 pci_save_state(pdev);
4662 pci_disable_device(pdev);
4663 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4664 return 0;
4665}
4666
4667static int skd_pci_resume(struct pci_dev *pdev)
4668{
4669 int i;
4670 int rc = 0;
4671 struct skd_device *skdev;
4672
4673 skdev = pci_get_drvdata(pdev);
4674 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004675 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004676 return -1;
4677 }
4678
4679 pci_set_power_state(pdev, PCI_D0);
4680 pci_enable_wake(pdev, PCI_D0, 0);
4681 pci_restore_state(pdev);
4682
4683 rc = pci_enable_device(pdev);
4684 if (rc)
4685 return rc;
4686 rc = pci_request_regions(pdev, DRV_NAME);
4687 if (rc)
4688 goto err_out;
4689 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4690 if (!rc) {
4691 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4692
Bart Van Asschef98806d2017-08-17 13:12:58 -07004693 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4694 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004695 }
4696 } else {
4697 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4698 if (rc) {
4699
Bart Van Asschef98806d2017-08-17 13:12:58 -07004700 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004701 goto err_out_regions;
4702 }
4703 }
4704
4705 pci_set_master(pdev);
4706 rc = pci_enable_pcie_error_reporting(pdev);
4707 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004708 dev_err(&pdev->dev,
4709 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004710 skdev->pcie_error_reporting_is_enabled = 0;
4711 } else
4712 skdev->pcie_error_reporting_is_enabled = 1;
4713
4714 for (i = 0; i < SKD_MAX_BARS; i++) {
4715
4716 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4717 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4718 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4719 skdev->mem_size[i]);
4720 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004721 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004722 rc = -ENODEV;
4723 goto err_out_iounmap;
4724 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004725 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4726 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4727 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004728 }
4729 rc = skd_acquire_irq(skdev);
4730 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004731 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004732 goto err_out_iounmap;
4733 }
4734
4735 rc = skd_start_timer(skdev);
4736 if (rc)
4737 goto err_out_timer;
4738
4739 init_waitqueue_head(&skdev->waitq);
4740
4741 skd_start_device(skdev);
4742
4743 return rc;
4744
4745err_out_timer:
4746 skd_stop_device(skdev);
4747 skd_release_irq(skdev);
4748
4749err_out_iounmap:
4750 for (i = 0; i < SKD_MAX_BARS; i++)
4751 if (skdev->mem_map[i])
4752 iounmap(skdev->mem_map[i]);
4753
4754 if (skdev->pcie_error_reporting_is_enabled)
4755 pci_disable_pcie_error_reporting(pdev);
4756
4757err_out_regions:
4758 pci_release_regions(pdev);
4759
4760err_out:
4761 pci_disable_device(pdev);
4762 return rc;
4763}
4764
4765static void skd_pci_shutdown(struct pci_dev *pdev)
4766{
4767 struct skd_device *skdev;
4768
Bart Van Asschef98806d2017-08-17 13:12:58 -07004769 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004770
4771 skdev = pci_get_drvdata(pdev);
4772 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004773 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004774 return;
4775 }
4776
Bart Van Asschef98806d2017-08-17 13:12:58 -07004777 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004778 skd_stop_device(skdev);
4779}
4780
4781static struct pci_driver skd_driver = {
4782 .name = DRV_NAME,
4783 .id_table = skd_pci_tbl,
4784 .probe = skd_pci_probe,
4785 .remove = skd_pci_remove,
4786 .suspend = skd_pci_suspend,
4787 .resume = skd_pci_resume,
4788 .shutdown = skd_pci_shutdown,
4789};
4790
4791/*
4792 *****************************************************************************
4793 * LOGGING SUPPORT
4794 *****************************************************************************
4795 */
4796
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004797const char *skd_drive_state_to_str(int state)
4798{
4799 switch (state) {
4800 case FIT_SR_DRIVE_OFFLINE:
4801 return "OFFLINE";
4802 case FIT_SR_DRIVE_INIT:
4803 return "INIT";
4804 case FIT_SR_DRIVE_ONLINE:
4805 return "ONLINE";
4806 case FIT_SR_DRIVE_BUSY:
4807 return "BUSY";
4808 case FIT_SR_DRIVE_FAULT:
4809 return "FAULT";
4810 case FIT_SR_DRIVE_DEGRADED:
4811 return "DEGRADED";
4812 case FIT_SR_PCIE_LINK_DOWN:
4813 return "INK_DOWN";
4814 case FIT_SR_DRIVE_SOFT_RESET:
4815 return "SOFT_RESET";
4816 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4817 return "NEED_FW";
4818 case FIT_SR_DRIVE_INIT_FAULT:
4819 return "INIT_FAULT";
4820 case FIT_SR_DRIVE_BUSY_SANITIZE:
4821 return "BUSY_SANITIZE";
4822 case FIT_SR_DRIVE_BUSY_ERASE:
4823 return "BUSY_ERASE";
4824 case FIT_SR_DRIVE_FW_BOOTING:
4825 return "FW_BOOTING";
4826 default:
4827 return "???";
4828 }
4829}
4830
4831const char *skd_skdev_state_to_str(enum skd_drvr_state state)
4832{
4833 switch (state) {
4834 case SKD_DRVR_STATE_LOAD:
4835 return "LOAD";
4836 case SKD_DRVR_STATE_IDLE:
4837 return "IDLE";
4838 case SKD_DRVR_STATE_BUSY:
4839 return "BUSY";
4840 case SKD_DRVR_STATE_STARTING:
4841 return "STARTING";
4842 case SKD_DRVR_STATE_ONLINE:
4843 return "ONLINE";
4844 case SKD_DRVR_STATE_PAUSING:
4845 return "PAUSING";
4846 case SKD_DRVR_STATE_PAUSED:
4847 return "PAUSED";
4848 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
4849 return "DRAINING_TIMEOUT";
4850 case SKD_DRVR_STATE_RESTARTING:
4851 return "RESTARTING";
4852 case SKD_DRVR_STATE_RESUMING:
4853 return "RESUMING";
4854 case SKD_DRVR_STATE_STOPPING:
4855 return "STOPPING";
4856 case SKD_DRVR_STATE_SYNCING:
4857 return "SYNCING";
4858 case SKD_DRVR_STATE_FAULT:
4859 return "FAULT";
4860 case SKD_DRVR_STATE_DISAPPEARED:
4861 return "DISAPPEARED";
4862 case SKD_DRVR_STATE_BUSY_ERASE:
4863 return "BUSY_ERASE";
4864 case SKD_DRVR_STATE_BUSY_SANITIZE:
4865 return "BUSY_SANITIZE";
4866 case SKD_DRVR_STATE_BUSY_IMMINENT:
4867 return "BUSY_IMMINENT";
4868 case SKD_DRVR_STATE_WAIT_BOOT:
4869 return "WAIT_BOOT";
4870
4871 default:
4872 return "???";
4873 }
4874}
4875
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304876static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004877{
4878 switch (state) {
4879 case SKD_MSG_STATE_IDLE:
4880 return "IDLE";
4881 case SKD_MSG_STATE_BUSY:
4882 return "BUSY";
4883 default:
4884 return "???";
4885 }
4886}
4887
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304888static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004889{
4890 switch (state) {
4891 case SKD_REQ_STATE_IDLE:
4892 return "IDLE";
4893 case SKD_REQ_STATE_SETUP:
4894 return "SETUP";
4895 case SKD_REQ_STATE_BUSY:
4896 return "BUSY";
4897 case SKD_REQ_STATE_COMPLETED:
4898 return "COMPLETED";
4899 case SKD_REQ_STATE_TIMEOUT:
4900 return "TIMEOUT";
4901 case SKD_REQ_STATE_ABORTED:
4902 return "ABORTED";
4903 default:
4904 return "???";
4905 }
4906}
4907
4908static void skd_log_skdev(struct skd_device *skdev, const char *event)
4909{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004910 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
4911 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
4912 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
4913 skd_skdev_state_to_str(skdev->state), skdev->state);
4914 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
4915 skdev->in_flight, skdev->cur_max_queue_depth,
4916 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
4917 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
4918 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004919}
4920
4921static void skd_log_skmsg(struct skd_device *skdev,
4922 struct skd_fitmsg_context *skmsg, const char *event)
4923{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004924 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
4925 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
4926 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
4927 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004928}
4929
4930static void skd_log_skreq(struct skd_device *skdev,
4931 struct skd_request_context *skreq, const char *event)
4932{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004933 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
4934 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
4935 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
4936 skreq->fitmsg_id);
4937 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
4938 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004939
Jens Axboefcd37eb2013-11-01 10:14:56 -06004940 if (skreq->req != NULL) {
4941 struct request *req = skreq->req;
4942 u32 lba = (u32)blk_rq_pos(req);
4943 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004944
Bart Van Asschef98806d2017-08-17 13:12:58 -07004945 dev_dbg(&skdev->pdev->dev,
4946 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
4947 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06004948 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07004949 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004950}
4951
4952/*
4953 *****************************************************************************
4954 * MODULE GLUE
4955 *****************************************************************************
4956 */
4957
4958static int __init skd_init(void)
4959{
Bart Van Assche16a70532017-08-17 13:13:08 -07004960 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
4961 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
4962 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
4963 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
4964 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07004965 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
4966 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
4967 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07004968
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004969 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
4970
4971 switch (skd_isr_type) {
4972 case SKD_IRQ_LEGACY:
4973 case SKD_IRQ_MSI:
4974 case SKD_IRQ_MSIX:
4975 break;
4976 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004977 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004978 skd_isr_type, SKD_IRQ_DEFAULT);
4979 skd_isr_type = SKD_IRQ_DEFAULT;
4980 }
4981
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004982 if (skd_max_queue_depth < 1 ||
4983 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
4984 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004985 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
4986 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
4987 }
4988
Bart Van Assche2da7b402017-08-17 13:13:01 -07004989 if (skd_max_req_per_msg < 1 ||
4990 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004991 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004992 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
4993 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
4994 }
4995
4996 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004997 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004998 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
4999 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5000 }
5001
5002 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005003 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005004 skd_dbg_level, 0);
5005 skd_dbg_level = 0;
5006 }
5007
5008 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005009 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005010 skd_isr_comp_limit, 0);
5011 skd_isr_comp_limit = 0;
5012 }
5013
5014 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005015 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005016 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5017 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5018 }
5019
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005020 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005021}
5022
5023static void __exit skd_exit(void)
5024{
5025 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5026
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005027 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005028
5029 if (skd_major)
5030 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005031}
5032
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005033module_init(skd_init);
5034module_exit(skd_exit);