blob: e2d205b58fe20d13834fb347c8d7998034db91b4 [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{
2163 int i, n;
2164
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? */
2174 n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
2175 for (i = 0; i < n; i++) {
2176 struct sns_info *sns = &skd_chkstat_table[i];
2177
2178 if (sns->mask & 0x10)
2179 if (skerr->type != sns->type)
2180 continue;
2181
2182 if (sns->mask & 0x08)
2183 if (cmp_status != sns->stat)
2184 continue;
2185
2186 if (sns->mask & 0x04)
2187 if (skerr->key != sns->key)
2188 continue;
2189
2190 if (sns->mask & 0x02)
2191 if (skerr->code != sns->asc)
2192 continue;
2193
2194 if (sns->mask & 0x01)
2195 if (skerr->qual != sns->ascq)
2196 continue;
2197
2198 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002199 dev_err(&skdev->pdev->dev,
2200 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
2201 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002202 }
2203 return sns->action;
2204 }
2205
2206 /* No other match, so nonzero status means error,
2207 * zero status means good
2208 */
2209 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002210 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002211 return SKD_CHECK_STATUS_REPORT_ERROR;
2212 }
2213
Bart Van Asschef98806d2017-08-17 13:12:58 -07002214 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002215 return SKD_CHECK_STATUS_REPORT_GOOD;
2216}
2217
2218static void skd_resolve_req_exception(struct skd_device *skdev,
2219 struct skd_request_context *skreq)
2220{
2221 u8 cmp_status = skreq->completion.status;
2222
2223 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2224 case SKD_CHECK_STATUS_REPORT_GOOD:
2225 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002226 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002227 break;
2228
2229 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2230 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002231 blk_requeue_request(skdev->queue, skreq->req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002232 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002233 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2234 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2235 skd_quiesce_dev(skdev);
2236 break;
2237
2238 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002239 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2240 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002241 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002242 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002243 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07002244 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002245
2246 case SKD_CHECK_STATUS_REPORT_ERROR:
2247 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002248 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002249 break;
2250 }
2251}
2252
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002253/* assume spinlock is already held */
2254static void skd_release_skreq(struct skd_device *skdev,
2255 struct skd_request_context *skreq)
2256{
2257 u32 msg_slot;
2258 struct skd_fitmsg_context *skmsg;
2259
2260 u32 timo_slot;
2261
2262 /*
2263 * Reclaim the FIT msg buffer if this is
2264 * the first of the requests it carried to
2265 * be completed. The FIT msg buffer used to
2266 * send this request cannot be reused until
2267 * we are sure the s1120 card has copied
2268 * it to its memory. The FIT msg might have
2269 * contained several requests. As soon as
2270 * any of them are completed we know that
2271 * the entire FIT msg was transferred.
2272 * Only the first completed request will
2273 * match the FIT msg buffer id. The FIT
2274 * msg buffer id is immediately updated.
2275 * When subsequent requests complete the FIT
2276 * msg buffer id won't match, so we know
2277 * quite cheaply that it is already done.
2278 */
2279 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2280 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2281
2282 skmsg = &skdev->skmsg_table[msg_slot];
2283 if (skmsg->id == skreq->fitmsg_id) {
2284 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2285 SKD_ASSERT(skmsg->outstanding > 0);
2286 skmsg->outstanding--;
2287 if (skmsg->outstanding == 0) {
2288 skmsg->state = SKD_MSG_STATE_IDLE;
2289 skmsg->id += SKD_ID_INCR;
2290 skmsg->next = skdev->skmsg_free_list;
2291 skdev->skmsg_free_list = skmsg;
2292 }
2293 }
2294
2295 /*
2296 * Decrease the number of active requests.
2297 * Also decrements the count in the timeout slot.
2298 */
2299 SKD_ASSERT(skdev->in_flight > 0);
2300 skdev->in_flight -= 1;
2301
2302 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2303 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2304 skdev->timeout_slot[timo_slot] -= 1;
2305
2306 /*
2307 * Reset backpointer
2308 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002309 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002310
2311 /*
2312 * Reclaim the skd_request_context
2313 */
2314 skreq->state = SKD_REQ_STATE_IDLE;
2315 skreq->id += SKD_ID_INCR;
2316 skreq->next = skdev->skreq_free_list;
2317 skdev->skreq_free_list = skreq;
2318}
2319
2320#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2321
2322static void skd_do_inq_page_00(struct skd_device *skdev,
2323 volatile struct fit_completion_entry_v1 *skcomp,
2324 volatile struct fit_comp_error_info *skerr,
2325 uint8_t *cdb, uint8_t *buf)
2326{
2327 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2328
2329 /* Caller requested "supported pages". The driver needs to insert
2330 * its page.
2331 */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002332 dev_dbg(&skdev->pdev->dev,
2333 "skd_do_driver_inquiry: modify supported pages.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002334
2335 /* If the device rejected the request because the CDB was
2336 * improperly formed, then just leave.
2337 */
2338 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2339 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2340 return;
2341
2342 /* Get the amount of space the caller allocated */
2343 max_bytes = (cdb[3] << 8) | cdb[4];
2344
2345 /* Get the number of pages actually returned by the device */
2346 drive_pages = (buf[2] << 8) | buf[3];
2347 drive_bytes = drive_pages + 4;
2348 new_size = drive_pages + 1;
2349
2350 /* Supported pages must be in numerical order, so find where
2351 * the driver page needs to be inserted into the list of
2352 * pages returned by the device.
2353 */
2354 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2355 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2356 return; /* Device using this page code. abort */
2357 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2358 break;
2359 }
2360
2361 if (insert_pt < max_bytes) {
2362 uint16_t u;
2363
2364 /* Shift everything up one byte to make room. */
2365 for (u = new_size + 3; u > insert_pt; u--)
2366 buf[u] = buf[u - 1];
2367 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2368
2369 /* SCSI byte order increment of num_returned_bytes by 1 */
2370 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002371 cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002372 }
2373
2374 /* update page length field to reflect the driver's page too */
2375 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2376 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2377}
2378
2379static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2380{
2381 int pcie_reg;
2382 u16 pci_bus_speed;
2383 u8 pci_lanes;
2384
2385 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2386 if (pcie_reg) {
2387 u16 linksta;
2388 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2389
2390 pci_bus_speed = linksta & 0xF;
2391 pci_lanes = (linksta & 0x3F0) >> 4;
2392 } else {
2393 *speed = STEC_LINK_UNKNOWN;
2394 *width = 0xFF;
2395 return;
2396 }
2397
2398 switch (pci_bus_speed) {
2399 case 1:
2400 *speed = STEC_LINK_2_5GTS;
2401 break;
2402 case 2:
2403 *speed = STEC_LINK_5GTS;
2404 break;
2405 case 3:
2406 *speed = STEC_LINK_8GTS;
2407 break;
2408 default:
2409 *speed = STEC_LINK_UNKNOWN;
2410 break;
2411 }
2412
2413 if (pci_lanes <= 0x20)
2414 *width = pci_lanes;
2415 else
2416 *width = 0xFF;
2417}
2418
2419static void skd_do_inq_page_da(struct skd_device *skdev,
2420 volatile struct fit_completion_entry_v1 *skcomp,
2421 volatile struct fit_comp_error_info *skerr,
2422 uint8_t *cdb, uint8_t *buf)
2423{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002424 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002425 unsigned max_bytes;
2426 struct driver_inquiry_data inq;
2427 u16 val;
2428
Bart Van Asschef98806d2017-08-17 13:12:58 -07002429 dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002430
2431 memset(&inq, 0, sizeof(inq));
2432
2433 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2434
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002435 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2436 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2437 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2438 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002439
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002440 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2441 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002442
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002443 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2444 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002445
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002446 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2447 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002448
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002449 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2450 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002451
2452 /* Driver version, fixed lenth, padded with spaces on the right */
2453 inq.driver_version_length = sizeof(inq.driver_version);
2454 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2455 memcpy(inq.driver_version, DRV_VER_COMPL,
2456 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2457
2458 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2459
2460 /* Clear the error set by the device */
2461 skcomp->status = SAM_STAT_GOOD;
2462 memset((void *)skerr, 0, sizeof(*skerr));
2463
2464 /* copy response into output buffer */
2465 max_bytes = (cdb[3] << 8) | cdb[4];
2466 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2467
2468 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002469 cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq)));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002470}
2471
2472static void skd_do_driver_inq(struct skd_device *skdev,
2473 volatile struct fit_completion_entry_v1 *skcomp,
2474 volatile struct fit_comp_error_info *skerr,
2475 uint8_t *cdb, uint8_t *buf)
2476{
2477 if (!buf)
2478 return;
2479 else if (cdb[0] != INQUIRY)
2480 return; /* Not an INQUIRY */
2481 else if ((cdb[1] & 1) == 0)
2482 return; /* EVPD not set */
2483 else if (cdb[2] == 0)
2484 /* Need to add driver's page to supported pages list */
2485 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2486 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2487 /* Caller requested driver's page */
2488 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2489}
2490
2491static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2492{
2493 if (!sg)
2494 return NULL;
2495 if (!sg_page(sg))
2496 return NULL;
2497 return sg_virt(sg);
2498}
2499
2500static void skd_process_scsi_inq(struct skd_device *skdev,
2501 volatile struct fit_completion_entry_v1
2502 *skcomp,
2503 volatile struct fit_comp_error_info *skerr,
2504 struct skd_special_context *skspcl)
2505{
2506 uint8_t *buf;
Bart Van Assched891fe62017-08-17 13:13:07 -07002507 struct skd_scsi_request *scsi_req = &skspcl->msg_buf->scsi[0];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002508
2509 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2510 skspcl->req.sg_data_dir);
2511 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2512
2513 if (buf)
2514 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2515}
2516
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002517static int skd_isr_completion_posted(struct skd_device *skdev,
2518 int limit, int *enqueued)
2519{
2520 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2521 volatile struct fit_comp_error_info *skerr;
2522 u16 req_id;
2523 u32 req_slot;
2524 struct skd_request_context *skreq;
2525 u16 cmp_cntxt = 0;
2526 u8 cmp_status = 0;
2527 u8 cmp_cycle = 0;
2528 u32 cmp_bytes = 0;
2529 int rc = 0;
2530 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002531
Bart Van Assche760b48c2017-08-17 13:13:00 -07002532 lockdep_assert_held(&skdev->lock);
2533
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002534 for (;; ) {
2535 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2536
2537 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2538 cmp_cycle = skcmp->cycle;
2539 cmp_cntxt = skcmp->tag;
2540 cmp_status = skcmp->status;
2541 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2542
2543 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2544
Bart Van Asschef98806d2017-08-17 13:12:58 -07002545 dev_dbg(&skdev->pdev->dev,
2546 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
2547 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
2548 cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
2549 skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002550
2551 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002552 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002553 break;
2554 }
2555 /*
2556 * Update the completion queue head index and possibly
2557 * the completion cycle count. 8-bit wrap-around.
2558 */
2559 skdev->skcomp_ix++;
2560 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2561 skdev->skcomp_ix = 0;
2562 skdev->skcomp_cycle++;
2563 }
2564
2565 /*
2566 * The command context is a unique 32-bit ID. The low order
2567 * bits help locate the request. The request is usually a
2568 * r/w request (see skd_start() above) or a special request.
2569 */
2570 req_id = cmp_cntxt;
2571 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2572
2573 /* Is this other than a r/w request? */
2574 if (req_slot >= skdev->num_req_context) {
2575 /*
2576 * This is not a completion for a r/w request.
2577 */
2578 skd_complete_other(skdev, skcmp, skerr);
2579 continue;
2580 }
2581
2582 skreq = &skdev->skreq_table[req_slot];
2583
2584 /*
2585 * Make sure the request ID for the slot matches.
2586 */
2587 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002588 dev_dbg(&skdev->pdev->dev,
2589 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
2590 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002591 {
2592 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002593 dev_err(&skdev->pdev->dev,
2594 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2595 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002596
2597 continue;
2598 }
2599 }
2600
2601 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2602
2603 if (skreq->state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002604 dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n",
2605 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002606 /* a previously timed out command can
2607 * now be cleaned up */
2608 skd_release_skreq(skdev, skreq);
2609 continue;
2610 }
2611
2612 skreq->completion = *skcmp;
2613 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2614 skreq->err_info = *skerr;
2615 skd_log_check_status(skdev, cmp_status, skerr->key,
2616 skerr->code, skerr->qual,
2617 skerr->fruc);
2618 }
2619 /* Release DMA resources for the request. */
2620 if (skreq->n_sg > 0)
2621 skd_postop_sg_list(skdev, skreq);
2622
Jens Axboefcd37eb2013-11-01 10:14:56 -06002623 if (!skreq->req) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002624 dev_dbg(&skdev->pdev->dev,
2625 "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
2626 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002627 } else {
2628 /*
2629 * Capture the outcome and post it back to the
2630 * native request.
2631 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002632 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002633 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002634 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002635 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002636 }
2637
2638 /*
2639 * Release the skreq, its FIT msg (if one), timeout slot,
2640 * and queue depth.
2641 */
2642 skd_release_skreq(skdev, skreq);
2643
2644 /* skd_isr_comp_limit equal zero means no limit */
2645 if (limit) {
2646 if (++processed >= limit) {
2647 rc = 1;
2648 break;
2649 }
2650 }
2651 }
2652
2653 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2654 && (skdev->in_flight) == 0) {
2655 skdev->state = SKD_DRVR_STATE_PAUSED;
2656 wake_up_interruptible(&skdev->waitq);
2657 }
2658
2659 return rc;
2660}
2661
2662static void skd_complete_other(struct skd_device *skdev,
2663 volatile struct fit_completion_entry_v1 *skcomp,
2664 volatile struct fit_comp_error_info *skerr)
2665{
2666 u32 req_id = 0;
2667 u32 req_table;
2668 u32 req_slot;
2669 struct skd_special_context *skspcl;
2670
Bart Van Assche760b48c2017-08-17 13:13:00 -07002671 lockdep_assert_held(&skdev->lock);
2672
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002673 req_id = skcomp->tag;
2674 req_table = req_id & SKD_ID_TABLE_MASK;
2675 req_slot = req_id & SKD_ID_SLOT_MASK;
2676
Bart Van Asschef98806d2017-08-17 13:12:58 -07002677 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
2678 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002679
2680 /*
2681 * Based on the request id, determine how to dispatch this completion.
2682 * This swich/case is finding the good cases and forwarding the
2683 * completion entry. Errors are reported below the switch.
2684 */
2685 switch (req_table) {
2686 case SKD_ID_RW_REQUEST:
2687 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07002688 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002689 * handles r/w requests. The only way we get here
2690 * is if the req_slot is out of bounds.
2691 */
2692 break;
2693
2694 case SKD_ID_SPECIAL_REQUEST:
2695 /*
2696 * Make sure the req_slot is in bounds and that the id
2697 * matches.
2698 */
2699 if (req_slot < skdev->n_special) {
2700 skspcl = &skdev->skspcl_table[req_slot];
2701 if (skspcl->req.id == req_id &&
2702 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2703 skd_complete_special(skdev,
2704 skcomp, skerr, skspcl);
2705 return;
2706 }
2707 }
2708 break;
2709
2710 case SKD_ID_INTERNAL:
2711 if (req_slot == 0) {
2712 skspcl = &skdev->internal_skspcl;
2713 if (skspcl->req.id == req_id &&
2714 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2715 skd_complete_internal(skdev,
2716 skcomp, skerr, skspcl);
2717 return;
2718 }
2719 }
2720 break;
2721
2722 case SKD_ID_FIT_MSG:
2723 /*
2724 * These id's should never appear in a completion record.
2725 */
2726 break;
2727
2728 default:
2729 /*
2730 * These id's should never appear anywhere;
2731 */
2732 break;
2733 }
2734
2735 /*
2736 * If we get here it is a bad or stale id.
2737 */
2738}
2739
2740static void skd_complete_special(struct skd_device *skdev,
2741 volatile struct fit_completion_entry_v1
2742 *skcomp,
2743 volatile struct fit_comp_error_info *skerr,
2744 struct skd_special_context *skspcl)
2745{
Bart Van Assche760b48c2017-08-17 13:13:00 -07002746 lockdep_assert_held(&skdev->lock);
2747
Bart Van Asschef98806d2017-08-17 13:12:58 -07002748 dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002749 if (skspcl->orphaned) {
2750 /* Discard orphaned request */
2751 /* ?: Can this release directly or does it need
2752 * to use a worker? */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002753 dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002754 skd_release_special(skdev, skspcl);
2755 return;
2756 }
2757
2758 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2759
2760 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2761 skspcl->req.completion = *skcomp;
2762 skspcl->req.err_info = *skerr;
2763
2764 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2765 skerr->code, skerr->qual, skerr->fruc);
2766
2767 wake_up_interruptible(&skdev->waitq);
2768}
2769
2770/* assume spinlock is already held */
2771static void skd_release_special(struct skd_device *skdev,
2772 struct skd_special_context *skspcl)
2773{
2774 int i, was_depleted;
2775
2776 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002777 struct page *page = sg_page(&skspcl->req.sg[i]);
2778 __free_page(page);
2779 }
2780
2781 was_depleted = (skdev->skspcl_free_list == NULL);
2782
2783 skspcl->req.state = SKD_REQ_STATE_IDLE;
2784 skspcl->req.id += SKD_ID_INCR;
2785 skspcl->req.next =
2786 (struct skd_request_context *)skdev->skspcl_free_list;
2787 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2788
2789 if (was_depleted) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002790 dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002791 /* Free list was depleted. Their might be waiters. */
2792 wake_up_interruptible(&skdev->waitq);
2793 }
2794}
2795
2796static void skd_reset_skcomp(struct skd_device *skdev)
2797{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002798 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002799
2800 skdev->skcomp_ix = 0;
2801 skdev->skcomp_cycle = 1;
2802}
2803
2804/*
2805 *****************************************************************************
2806 * INTERRUPTS
2807 *****************************************************************************
2808 */
2809static void skd_completion_worker(struct work_struct *work)
2810{
2811 struct skd_device *skdev =
2812 container_of(work, struct skd_device, completion_worker);
2813 unsigned long flags;
2814 int flush_enqueued = 0;
2815
2816 spin_lock_irqsave(&skdev->lock, flags);
2817
2818 /*
2819 * pass in limit=0, which means no limit..
2820 * process everything in compq
2821 */
2822 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2823 skd_request_fn(skdev->queue);
2824
2825 spin_unlock_irqrestore(&skdev->lock, flags);
2826}
2827
2828static void skd_isr_msg_from_dev(struct skd_device *skdev);
2829
Arnd Bergmann41c94992016-11-09 13:55:35 +01002830static irqreturn_t
2831skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002832{
2833 struct skd_device *skdev;
2834 u32 intstat;
2835 u32 ack;
2836 int rc = 0;
2837 int deferred = 0;
2838 int flush_enqueued = 0;
2839
2840 skdev = (struct skd_device *)ptr;
2841 spin_lock(&skdev->lock);
2842
2843 for (;; ) {
2844 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2845
2846 ack = FIT_INT_DEF_MASK;
2847 ack &= intstat;
2848
Bart Van Asschef98806d2017-08-17 13:12:58 -07002849 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
2850 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002851
2852 /* As long as there is an int pending on device, keep
2853 * running loop. When none, get out, but if we've never
2854 * done any processing, call completion handler?
2855 */
2856 if (ack == 0) {
2857 /* No interrupts on device, but run the completion
2858 * processor anyway?
2859 */
2860 if (rc == 0)
2861 if (likely (skdev->state
2862 == SKD_DRVR_STATE_ONLINE))
2863 deferred = 1;
2864 break;
2865 }
2866
2867 rc = IRQ_HANDLED;
2868
2869 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2870
2871 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2872 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2873 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2874 /*
2875 * If we have already deferred completion
2876 * processing, don't bother running it again
2877 */
2878 if (deferred == 0)
2879 deferred =
2880 skd_isr_completion_posted(skdev,
2881 skd_isr_comp_limit, &flush_enqueued);
2882 }
2883
2884 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2885 skd_isr_fwstate(skdev);
2886 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2887 skdev->state ==
2888 SKD_DRVR_STATE_DISAPPEARED) {
2889 spin_unlock(&skdev->lock);
2890 return rc;
2891 }
2892 }
2893
2894 if (intstat & FIT_ISH_MSG_FROM_DEV)
2895 skd_isr_msg_from_dev(skdev);
2896 }
2897 }
2898
2899 if (unlikely(flush_enqueued))
2900 skd_request_fn(skdev->queue);
2901
2902 if (deferred)
2903 schedule_work(&skdev->completion_worker);
2904 else if (!flush_enqueued)
2905 skd_request_fn(skdev->queue);
2906
2907 spin_unlock(&skdev->lock);
2908
2909 return rc;
2910}
2911
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002912static void skd_drive_fault(struct skd_device *skdev)
2913{
2914 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002915 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002916}
2917
2918static void skd_drive_disappeared(struct skd_device *skdev)
2919{
2920 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002921 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002922}
2923
2924static void skd_isr_fwstate(struct skd_device *skdev)
2925{
2926 u32 sense;
2927 u32 state;
2928 u32 mtd;
2929 int prev_driver_state = skdev->state;
2930
2931 sense = SKD_READL(skdev, FIT_STATUS);
2932 state = sense & FIT_SR_DRIVE_STATE_MASK;
2933
Bart Van Asschef98806d2017-08-17 13:12:58 -07002934 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
2935 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
2936 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002937
2938 skdev->drive_state = state;
2939
2940 switch (skdev->drive_state) {
2941 case FIT_SR_DRIVE_INIT:
2942 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
2943 skd_disable_interrupts(skdev);
2944 break;
2945 }
2946 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
2947 skd_recover_requests(skdev, 0);
2948 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2949 skdev->timer_countdown = SKD_STARTING_TIMO;
2950 skdev->state = SKD_DRVR_STATE_STARTING;
2951 skd_soft_reset(skdev);
2952 break;
2953 }
2954 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2955 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2956 skdev->last_mtd = mtd;
2957 break;
2958
2959 case FIT_SR_DRIVE_ONLINE:
2960 skdev->cur_max_queue_depth = skd_max_queue_depth;
2961 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2962 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2963
2964 skdev->queue_low_water_mark =
2965 skdev->cur_max_queue_depth * 2 / 3 + 1;
2966 if (skdev->queue_low_water_mark < 1)
2967 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002968 dev_info(&skdev->pdev->dev,
2969 "Queue depth limit=%d dev=%d lowat=%d\n",
2970 skdev->cur_max_queue_depth,
2971 skdev->dev_max_queue_depth,
2972 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002973
2974 skd_refresh_device_data(skdev);
2975 break;
2976
2977 case FIT_SR_DRIVE_BUSY:
2978 skdev->state = SKD_DRVR_STATE_BUSY;
2979 skdev->timer_countdown = SKD_BUSY_TIMO;
2980 skd_quiesce_dev(skdev);
2981 break;
2982 case FIT_SR_DRIVE_BUSY_SANITIZE:
2983 /* set timer for 3 seconds, we'll abort any unfinished
2984 * commands after that expires
2985 */
2986 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2987 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002988 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002989 break;
2990 case FIT_SR_DRIVE_BUSY_ERASE:
2991 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2992 skdev->timer_countdown = SKD_BUSY_TIMO;
2993 break;
2994 case FIT_SR_DRIVE_OFFLINE:
2995 skdev->state = SKD_DRVR_STATE_IDLE;
2996 break;
2997 case FIT_SR_DRIVE_SOFT_RESET:
2998 switch (skdev->state) {
2999 case SKD_DRVR_STATE_STARTING:
3000 case SKD_DRVR_STATE_RESTARTING:
3001 /* Expected by a caller of skd_soft_reset() */
3002 break;
3003 default:
3004 skdev->state = SKD_DRVR_STATE_RESTARTING;
3005 break;
3006 }
3007 break;
3008 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003009 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003010 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3011 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3012 break;
3013
3014 case FIT_SR_DRIVE_DEGRADED:
3015 case FIT_SR_PCIE_LINK_DOWN:
3016 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3017 break;
3018
3019 case FIT_SR_DRIVE_FAULT:
3020 skd_drive_fault(skdev);
3021 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003022 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003023 break;
3024
3025 /* PCIe bus returned all Fs? */
3026 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003027 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
3028 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003029 skd_drive_disappeared(skdev);
3030 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003031 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003032 break;
3033 default:
3034 /*
3035 * Uknown FW State. Wait for a state we recognize.
3036 */
3037 break;
3038 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003039 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3040 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3041 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003042}
3043
3044static void skd_recover_requests(struct skd_device *skdev, int requeue)
3045{
3046 int i;
3047
3048 for (i = 0; i < skdev->num_req_context; i++) {
3049 struct skd_request_context *skreq = &skdev->skreq_table[i];
3050
3051 if (skreq->state == SKD_REQ_STATE_BUSY) {
3052 skd_log_skreq(skdev, skreq, "recover");
3053
3054 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003055 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003056
3057 /* Release DMA resources for the request. */
3058 if (skreq->n_sg > 0)
3059 skd_postop_sg_list(skdev, skreq);
3060
Jens Axboefcd37eb2013-11-01 10:14:56 -06003061 if (requeue &&
3062 (unsigned long) ++skreq->req->special <
3063 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003064 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003065 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003066 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003067
Jens Axboefcd37eb2013-11-01 10:14:56 -06003068 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003069
3070 skreq->state = SKD_REQ_STATE_IDLE;
3071 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003072 }
3073 if (i > 0)
3074 skreq[-1].next = skreq;
3075 skreq->next = NULL;
3076 }
3077 skdev->skreq_free_list = skdev->skreq_table;
3078
3079 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3080 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3081
3082 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3083 skd_log_skmsg(skdev, skmsg, "salvaged");
3084 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3085 skmsg->state = SKD_MSG_STATE_IDLE;
3086 skmsg->id += SKD_ID_INCR;
3087 }
3088 if (i > 0)
3089 skmsg[-1].next = skmsg;
3090 skmsg->next = NULL;
3091 }
3092 skdev->skmsg_free_list = skdev->skmsg_table;
3093
3094 for (i = 0; i < skdev->n_special; i++) {
3095 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3096
3097 /* If orphaned, reclaim it because it has already been reported
3098 * to the process as an error (it was just waiting for
3099 * a completion that didn't come, and now it will never come)
3100 * If busy, change to a state that will cause it to error
3101 * out in the wait routine and let it do the normal
3102 * reporting and reclaiming
3103 */
3104 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3105 if (skspcl->orphaned) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003106 dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
3107 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003108 skd_release_special(skdev, skspcl);
3109 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003110 dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
3111 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003112 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3113 }
3114 }
3115 }
3116 skdev->skspcl_free_list = skdev->skspcl_table;
3117
3118 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3119 skdev->timeout_slot[i] = 0;
3120
3121 skdev->in_flight = 0;
3122}
3123
3124static void skd_isr_msg_from_dev(struct skd_device *skdev)
3125{
3126 u32 mfd;
3127 u32 mtd;
3128 u32 data;
3129
3130 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3131
Bart Van Asschef98806d2017-08-17 13:12:58 -07003132 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
3133 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003134
3135 /* ignore any mtd that is an ack for something we didn't send */
3136 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3137 return;
3138
3139 switch (FIT_MXD_TYPE(mfd)) {
3140 case FIT_MTD_FITFW_INIT:
3141 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3142
3143 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003144 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
3145 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
3146 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
3147 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003148 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3149 skd_soft_reset(skdev);
3150 break;
3151 }
3152 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3153 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3154 skdev->last_mtd = mtd;
3155 break;
3156
3157 case FIT_MTD_GET_CMDQ_DEPTH:
3158 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3159 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3160 SKD_N_COMPLETION_ENTRY);
3161 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3162 skdev->last_mtd = mtd;
3163 break;
3164
3165 case FIT_MTD_SET_COMPQ_DEPTH:
3166 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3167 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3168 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3169 skdev->last_mtd = mtd;
3170 break;
3171
3172 case FIT_MTD_SET_COMPQ_ADDR:
3173 skd_reset_skcomp(skdev);
3174 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3175 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3176 skdev->last_mtd = mtd;
3177 break;
3178
3179 case FIT_MTD_CMD_LOG_HOST_ID:
3180 skdev->connect_time_stamp = get_seconds();
3181 data = skdev->connect_time_stamp & 0xFFFF;
3182 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3183 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3184 skdev->last_mtd = mtd;
3185 break;
3186
3187 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3188 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3189 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3190 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3191 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3192 skdev->last_mtd = mtd;
3193 break;
3194
3195 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3196 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3197 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3198 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3199 skdev->last_mtd = mtd;
3200
Bart Van Asschef98806d2017-08-17 13:12:58 -07003201 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
3202 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003203 break;
3204
3205 case FIT_MTD_ARM_QUEUE:
3206 skdev->last_mtd = 0;
3207 /*
3208 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3209 */
3210 break;
3211
3212 default:
3213 break;
3214 }
3215}
3216
3217static void skd_disable_interrupts(struct skd_device *skdev)
3218{
3219 u32 sense;
3220
3221 sense = SKD_READL(skdev, FIT_CONTROL);
3222 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3223 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003224 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003225
3226 /* Note that the 1s is written. A 1-bit means
3227 * disable, a 0 means enable.
3228 */
3229 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3230}
3231
3232static void skd_enable_interrupts(struct skd_device *skdev)
3233{
3234 u32 val;
3235
3236 /* unmask interrupts first */
3237 val = FIT_ISH_FW_STATE_CHANGE +
3238 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3239
3240 /* Note that the compliment of mask is written. A 1-bit means
3241 * disable, a 0 means enable. */
3242 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003243 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003244
3245 val = SKD_READL(skdev, FIT_CONTROL);
3246 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003247 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003248 SKD_WRITEL(skdev, val, FIT_CONTROL);
3249}
3250
3251/*
3252 *****************************************************************************
3253 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3254 *****************************************************************************
3255 */
3256
3257static void skd_soft_reset(struct skd_device *skdev)
3258{
3259 u32 val;
3260
3261 val = SKD_READL(skdev, FIT_CONTROL);
3262 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003263 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003264 SKD_WRITEL(skdev, val, FIT_CONTROL);
3265}
3266
3267static void skd_start_device(struct skd_device *skdev)
3268{
3269 unsigned long flags;
3270 u32 sense;
3271 u32 state;
3272
3273 spin_lock_irqsave(&skdev->lock, flags);
3274
3275 /* ack all ghost interrupts */
3276 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3277
3278 sense = SKD_READL(skdev, FIT_STATUS);
3279
Bart Van Asschef98806d2017-08-17 13:12:58 -07003280 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003281
3282 state = sense & FIT_SR_DRIVE_STATE_MASK;
3283 skdev->drive_state = state;
3284 skdev->last_mtd = 0;
3285
3286 skdev->state = SKD_DRVR_STATE_STARTING;
3287 skdev->timer_countdown = SKD_STARTING_TIMO;
3288
3289 skd_enable_interrupts(skdev);
3290
3291 switch (skdev->drive_state) {
3292 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003293 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003294 break;
3295
3296 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003297 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003298 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3299 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3300 break;
3301
3302 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003303 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003304 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3305 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3306 break;
3307
3308 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003309 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003310 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3311 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3312 break;
3313
3314 case FIT_SR_DRIVE_INIT:
3315 case FIT_SR_DRIVE_ONLINE:
3316 skd_soft_reset(skdev);
3317 break;
3318
3319 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003320 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003321 skdev->state = SKD_DRVR_STATE_BUSY;
3322 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3323 break;
3324
3325 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003326 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003327 break;
3328
3329 case FIT_SR_DRIVE_FAULT:
3330 /* Fault state is bad...soft reset won't do it...
3331 * Hard reset, maybe, but does it work on device?
3332 * For now, just fault so the system doesn't hang.
3333 */
3334 skd_drive_fault(skdev);
3335 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003336 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003337 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003338 skdev->gendisk_on = -1;
3339 wake_up_interruptible(&skdev->waitq);
3340 break;
3341
3342 case 0xFF:
3343 /* Most likely the device isn't there or isn't responding
3344 * to the BAR1 addresses. */
3345 skd_drive_disappeared(skdev);
3346 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003347 dev_dbg(&skdev->pdev->dev,
3348 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003349 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003350 skdev->gendisk_on = -1;
3351 wake_up_interruptible(&skdev->waitq);
3352 break;
3353
3354 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003355 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
3356 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003357 break;
3358 }
3359
3360 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003361 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003362
3363 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003364 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003365
3366 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003367 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003368
3369 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003370 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003371
3372 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003373 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003374
3375 spin_unlock_irqrestore(&skdev->lock, flags);
3376}
3377
3378static void skd_stop_device(struct skd_device *skdev)
3379{
3380 unsigned long flags;
3381 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3382 u32 dev_state;
3383 int i;
3384
3385 spin_lock_irqsave(&skdev->lock, flags);
3386
3387 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003388 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003389 goto stop_out;
3390 }
3391
3392 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003393 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003394 goto stop_out;
3395 }
3396
3397 skdev->state = SKD_DRVR_STATE_SYNCING;
3398 skdev->sync_done = 0;
3399
3400 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3401
3402 spin_unlock_irqrestore(&skdev->lock, flags);
3403
3404 wait_event_interruptible_timeout(skdev->waitq,
3405 (skdev->sync_done), (10 * HZ));
3406
3407 spin_lock_irqsave(&skdev->lock, flags);
3408
3409 switch (skdev->sync_done) {
3410 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003411 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003412 break;
3413 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003414 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003415 break;
3416 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003417 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003418 }
3419
3420stop_out:
3421 skdev->state = SKD_DRVR_STATE_STOPPING;
3422 spin_unlock_irqrestore(&skdev->lock, flags);
3423
3424 skd_kill_timer(skdev);
3425
3426 spin_lock_irqsave(&skdev->lock, flags);
3427 skd_disable_interrupts(skdev);
3428
3429 /* ensure all ints on device are cleared */
3430 /* soft reset the device to unload with a clean slate */
3431 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3432 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3433
3434 spin_unlock_irqrestore(&skdev->lock, flags);
3435
3436 /* poll every 100ms, 1 second timeout */
3437 for (i = 0; i < 10; i++) {
3438 dev_state =
3439 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3440 if (dev_state == FIT_SR_DRIVE_INIT)
3441 break;
3442 set_current_state(TASK_INTERRUPTIBLE);
3443 schedule_timeout(msecs_to_jiffies(100));
3444 }
3445
3446 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07003447 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
3448 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003449}
3450
3451/* assume spinlock is held */
3452static void skd_restart_device(struct skd_device *skdev)
3453{
3454 u32 state;
3455
3456 /* ack all ghost interrupts */
3457 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3458
3459 state = SKD_READL(skdev, FIT_STATUS);
3460
Bart Van Asschef98806d2017-08-17 13:12:58 -07003461 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003462
3463 state &= FIT_SR_DRIVE_STATE_MASK;
3464 skdev->drive_state = state;
3465 skdev->last_mtd = 0;
3466
3467 skdev->state = SKD_DRVR_STATE_RESTARTING;
3468 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3469
3470 skd_soft_reset(skdev);
3471}
3472
3473/* assume spinlock is held */
3474static int skd_quiesce_dev(struct skd_device *skdev)
3475{
3476 int rc = 0;
3477
3478 switch (skdev->state) {
3479 case SKD_DRVR_STATE_BUSY:
3480 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003481 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003482 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003483 break;
3484 case SKD_DRVR_STATE_ONLINE:
3485 case SKD_DRVR_STATE_STOPPING:
3486 case SKD_DRVR_STATE_SYNCING:
3487 case SKD_DRVR_STATE_PAUSING:
3488 case SKD_DRVR_STATE_PAUSED:
3489 case SKD_DRVR_STATE_STARTING:
3490 case SKD_DRVR_STATE_RESTARTING:
3491 case SKD_DRVR_STATE_RESUMING:
3492 default:
3493 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003494 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
3495 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003496 }
3497 return rc;
3498}
3499
3500/* assume spinlock is held */
3501static int skd_unquiesce_dev(struct skd_device *skdev)
3502{
3503 int prev_driver_state = skdev->state;
3504
3505 skd_log_skdev(skdev, "unquiesce");
3506 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003507 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003508 return 0;
3509 }
3510 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3511 /*
3512 * If there has been an state change to other than
3513 * ONLINE, we will rely on controller state change
3514 * to come back online and restart the queue.
3515 * The BUSY state means that driver is ready to
3516 * continue normal processing but waiting for controller
3517 * to become available.
3518 */
3519 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003520 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003521 return 0;
3522 }
3523
3524 /*
3525 * Drive has just come online, driver is either in startup,
3526 * paused performing a task, or bust waiting for hardware.
3527 */
3528 switch (skdev->state) {
3529 case SKD_DRVR_STATE_PAUSED:
3530 case SKD_DRVR_STATE_BUSY:
3531 case SKD_DRVR_STATE_BUSY_IMMINENT:
3532 case SKD_DRVR_STATE_BUSY_ERASE:
3533 case SKD_DRVR_STATE_STARTING:
3534 case SKD_DRVR_STATE_RESTARTING:
3535 case SKD_DRVR_STATE_FAULT:
3536 case SKD_DRVR_STATE_IDLE:
3537 case SKD_DRVR_STATE_LOAD:
3538 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003539 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3540 skd_skdev_state_to_str(prev_driver_state),
3541 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3542 skdev->state);
3543 dev_dbg(&skdev->pdev->dev,
3544 "**** device ONLINE...starting block queue\n");
3545 dev_dbg(&skdev->pdev->dev, "starting queue\n");
3546 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003547 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003548 skdev->gendisk_on = 1;
3549 wake_up_interruptible(&skdev->waitq);
3550 break;
3551
3552 case SKD_DRVR_STATE_DISAPPEARED:
3553 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003554 dev_dbg(&skdev->pdev->dev,
3555 "**** driver state %d, not implemented\n",
3556 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003557 return -EBUSY;
3558 }
3559 return 0;
3560}
3561
3562/*
3563 *****************************************************************************
3564 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3565 *****************************************************************************
3566 */
3567
3568static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3569{
3570 struct skd_device *skdev = skd_host_data;
3571 unsigned long flags;
3572
3573 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003574 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3575 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3576 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
3577 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003578 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3579 spin_unlock_irqrestore(&skdev->lock, flags);
3580 return IRQ_HANDLED;
3581}
3582
3583static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3584{
3585 struct skd_device *skdev = skd_host_data;
3586 unsigned long flags;
3587
3588 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003589 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3590 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003591 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3592 skd_isr_fwstate(skdev);
3593 spin_unlock_irqrestore(&skdev->lock, flags);
3594 return IRQ_HANDLED;
3595}
3596
3597static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3598{
3599 struct skd_device *skdev = skd_host_data;
3600 unsigned long flags;
3601 int flush_enqueued = 0;
3602 int deferred;
3603
3604 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003605 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3606 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003607 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3608 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3609 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003610 if (flush_enqueued)
3611 skd_request_fn(skdev->queue);
3612
3613 if (deferred)
3614 schedule_work(&skdev->completion_worker);
3615 else if (!flush_enqueued)
3616 skd_request_fn(skdev->queue);
3617
3618 spin_unlock_irqrestore(&skdev->lock, flags);
3619
3620 return IRQ_HANDLED;
3621}
3622
3623static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3624{
3625 struct skd_device *skdev = skd_host_data;
3626 unsigned long flags;
3627
3628 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003629 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3630 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003631 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3632 skd_isr_msg_from_dev(skdev);
3633 spin_unlock_irqrestore(&skdev->lock, flags);
3634 return IRQ_HANDLED;
3635}
3636
3637static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3638{
3639 struct skd_device *skdev = skd_host_data;
3640 unsigned long flags;
3641
3642 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003643 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3644 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003645 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3646 spin_unlock_irqrestore(&skdev->lock, flags);
3647 return IRQ_HANDLED;
3648}
3649
3650/*
3651 *****************************************************************************
3652 * PCIe MSI/MSI-X SETUP
3653 *****************************************************************************
3654 */
3655
3656struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003657 char isr_name[30];
3658};
3659
3660struct skd_init_msix_entry {
3661 const char *name;
3662 irq_handler_t handler;
3663};
3664
3665#define SKD_MAX_MSIX_COUNT 13
3666#define SKD_MIN_MSIX_COUNT 7
3667#define SKD_BASE_MSIX_IRQ 4
3668
3669static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3670 { "(DMA 0)", skd_reserved_isr },
3671 { "(DMA 1)", skd_reserved_isr },
3672 { "(DMA 2)", skd_reserved_isr },
3673 { "(DMA 3)", skd_reserved_isr },
3674 { "(State Change)", skd_statec_isr },
3675 { "(COMPL_Q)", skd_comp_q },
3676 { "(MSG)", skd_msg_isr },
3677 { "(Reserved)", skd_reserved_isr },
3678 { "(Reserved)", skd_reserved_isr },
3679 { "(Queue Full 0)", skd_qfull_isr },
3680 { "(Queue Full 1)", skd_qfull_isr },
3681 { "(Queue Full 2)", skd_qfull_isr },
3682 { "(Queue Full 3)", skd_qfull_isr },
3683};
3684
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003685static int skd_acquire_msix(struct skd_device *skdev)
3686{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003687 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003688 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003689
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003690 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3691 PCI_IRQ_MSIX);
3692 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003693 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003694 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003695 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003696
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003697 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3698 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003699 if (!skdev->msix_entries) {
3700 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003701 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003702 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703 }
3704
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003705 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003706 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3707 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3708
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003709 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3710 "%s%d-msix %s", DRV_NAME, skdev->devno,
3711 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003712
3713 rc = devm_request_irq(&skdev->pdev->dev,
3714 pci_irq_vector(skdev->pdev, i),
3715 msix_entries[i].handler, 0,
3716 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003717 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003718 dev_err(&skdev->pdev->dev,
3719 "Unable to register(%d) MSI-X handler %d: %s\n",
3720 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003721 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003722 }
3723 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003724
Bart Van Asschef98806d2017-08-17 13:12:58 -07003725 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
3726 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003727 return 0;
3728
3729msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003730 while (--i >= 0)
3731 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003732out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003733 kfree(skdev->msix_entries);
3734 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003735 return rc;
3736}
3737
3738static int skd_acquire_irq(struct skd_device *skdev)
3739{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003740 struct pci_dev *pdev = skdev->pdev;
3741 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003742 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003743
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003744 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003745 rc = skd_acquire_msix(skdev);
3746 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003747 return 0;
3748
Bart Van Asschef98806d2017-08-17 13:12:58 -07003749 dev_err(&skdev->pdev->dev,
3750 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003751 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003752
3753 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3754 skdev->devno);
3755
3756 if (skd_isr_type != SKD_IRQ_LEGACY)
3757 irq_flag |= PCI_IRQ_MSI;
3758 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3759 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003760 dev_err(&skdev->pdev->dev,
3761 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003762 return rc;
3763 }
3764
3765 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3766 pdev->msi_enabled ? 0 : IRQF_SHARED,
3767 skdev->isr_name, skdev);
3768 if (rc) {
3769 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003770 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
3771 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003772 return rc;
3773 }
3774
3775 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003776}
3777
3778static void skd_release_irq(struct skd_device *skdev)
3779{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003780 struct pci_dev *pdev = skdev->pdev;
3781
3782 if (skdev->msix_entries) {
3783 int i;
3784
3785 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3786 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3787 skdev);
3788 }
3789
3790 kfree(skdev->msix_entries);
3791 skdev->msix_entries = NULL;
3792 } else {
3793 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003794 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003795
3796 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003797}
3798
3799/*
3800 *****************************************************************************
3801 * CONSTRUCT
3802 *****************************************************************************
3803 */
3804
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003805static int skd_cons_skcomp(struct skd_device *skdev)
3806{
3807 int rc = 0;
3808 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003809
Bart Van Asschef98806d2017-08-17 13:12:58 -07003810 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003811 "comp pci_alloc, total bytes %zd entries %d\n",
3812 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003813
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003814 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07003815 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003816
3817 if (skcomp == NULL) {
3818 rc = -ENOMEM;
3819 goto err_out;
3820 }
3821
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003822 skdev->skcomp_table = skcomp;
3823 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3824 sizeof(*skcomp) *
3825 SKD_N_COMPLETION_ENTRY);
3826
3827err_out:
3828 return rc;
3829}
3830
3831static int skd_cons_skmsg(struct skd_device *skdev)
3832{
3833 int rc = 0;
3834 u32 i;
3835
Bart Van Asschef98806d2017-08-17 13:12:58 -07003836 dev_dbg(&skdev->pdev->dev,
3837 "skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3838 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
3839 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003840
3841 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3842 *skdev->num_fitmsg_context, GFP_KERNEL);
3843 if (skdev->skmsg_table == NULL) {
3844 rc = -ENOMEM;
3845 goto err_out;
3846 }
3847
3848 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3849 struct skd_fitmsg_context *skmsg;
3850
3851 skmsg = &skdev->skmsg_table[i];
3852
3853 skmsg->id = i + SKD_ID_FIT_MSG;
3854
3855 skmsg->state = SKD_MSG_STATE_IDLE;
3856 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07003857 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003858 &skmsg->mb_dma_address);
3859
3860 if (skmsg->msg_buf == NULL) {
3861 rc = -ENOMEM;
3862 goto err_out;
3863 }
3864
Bart Van Assche6507f432017-08-17 13:13:06 -07003865 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
3866 (FIT_QCMD_ALIGN - 1),
3867 "not aligned: msg_buf %p mb_dma_address %#llx\n",
3868 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003869 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3870
3871 skmsg->next = &skmsg[1];
3872 }
3873
3874 /* Free list is in order starting with the 0th entry. */
3875 skdev->skmsg_table[i - 1].next = NULL;
3876 skdev->skmsg_free_list = skdev->skmsg_table;
3877
3878err_out:
3879 return rc;
3880}
3881
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003882static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
3883 u32 n_sg,
3884 dma_addr_t *ret_dma_addr)
3885{
3886 struct fit_sg_descriptor *sg_list;
3887 u32 nbytes;
3888
3889 nbytes = sizeof(*sg_list) * n_sg;
3890
3891 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
3892
3893 if (sg_list != NULL) {
3894 uint64_t dma_address = *ret_dma_addr;
3895 u32 i;
3896
3897 memset(sg_list, 0, nbytes);
3898
3899 for (i = 0; i < n_sg - 1; i++) {
3900 uint64_t ndp_off;
3901 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
3902
3903 sg_list[i].next_desc_ptr = dma_address + ndp_off;
3904 }
3905 sg_list[i].next_desc_ptr = 0LL;
3906 }
3907
3908 return sg_list;
3909}
3910
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003911static int skd_cons_skreq(struct skd_device *skdev)
3912{
3913 int rc = 0;
3914 u32 i;
3915
Bart Van Asschef98806d2017-08-17 13:12:58 -07003916 dev_dbg(&skdev->pdev->dev,
3917 "skreq_table kzalloc, struct %lu, count %u total %lu\n",
3918 sizeof(struct skd_request_context), skdev->num_req_context,
3919 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003920
3921 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
3922 * skdev->num_req_context, GFP_KERNEL);
3923 if (skdev->skreq_table == NULL) {
3924 rc = -ENOMEM;
3925 goto err_out;
3926 }
3927
Bart Van Asschef98806d2017-08-17 13:12:58 -07003928 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
3929 skdev->sgs_per_request, sizeof(struct scatterlist),
3930 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003931
3932 for (i = 0; i < skdev->num_req_context; i++) {
3933 struct skd_request_context *skreq;
3934
3935 skreq = &skdev->skreq_table[i];
3936
3937 skreq->id = i + SKD_ID_RW_REQUEST;
3938 skreq->state = SKD_REQ_STATE_IDLE;
3939
3940 skreq->sg = kzalloc(sizeof(struct scatterlist) *
3941 skdev->sgs_per_request, GFP_KERNEL);
3942 if (skreq->sg == NULL) {
3943 rc = -ENOMEM;
3944 goto err_out;
3945 }
3946 sg_init_table(skreq->sg, skdev->sgs_per_request);
3947
3948 skreq->sksg_list = skd_cons_sg_list(skdev,
3949 skdev->sgs_per_request,
3950 &skreq->sksg_dma_address);
3951
3952 if (skreq->sksg_list == NULL) {
3953 rc = -ENOMEM;
3954 goto err_out;
3955 }
3956
3957 skreq->next = &skreq[1];
3958 }
3959
3960 /* Free list is in order starting with the 0th entry. */
3961 skdev->skreq_table[i - 1].next = NULL;
3962 skdev->skreq_free_list = skdev->skreq_table;
3963
3964err_out:
3965 return rc;
3966}
3967
3968static int skd_cons_skspcl(struct skd_device *skdev)
3969{
3970 int rc = 0;
3971 u32 i, nbytes;
3972
Bart Van Asschef98806d2017-08-17 13:12:58 -07003973 dev_dbg(&skdev->pdev->dev,
3974 "skspcl_table kzalloc, struct %lu, count %u total %lu\n",
3975 sizeof(struct skd_special_context), skdev->n_special,
3976 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003977
3978 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
3979 * skdev->n_special, GFP_KERNEL);
3980 if (skdev->skspcl_table == NULL) {
3981 rc = -ENOMEM;
3982 goto err_out;
3983 }
3984
3985 for (i = 0; i < skdev->n_special; i++) {
3986 struct skd_special_context *skspcl;
3987
3988 skspcl = &skdev->skspcl_table[i];
3989
3990 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
3991 skspcl->req.state = SKD_REQ_STATE_IDLE;
3992
3993 skspcl->req.next = &skspcl[1].req;
3994
3995 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3996
Joe Perchesa5bbf612014-08-08 14:24:12 -07003997 skspcl->msg_buf =
3998 pci_zalloc_consistent(skdev->pdev, nbytes,
3999 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004000 if (skspcl->msg_buf == NULL) {
4001 rc = -ENOMEM;
4002 goto err_out;
4003 }
4004
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004005 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4006 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4007 if (skspcl->req.sg == NULL) {
4008 rc = -ENOMEM;
4009 goto err_out;
4010 }
4011
4012 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4013 SKD_N_SG_PER_SPECIAL,
4014 &skspcl->req.
4015 sksg_dma_address);
4016 if (skspcl->req.sksg_list == NULL) {
4017 rc = -ENOMEM;
4018 goto err_out;
4019 }
4020 }
4021
4022 /* Free list is in order starting with the 0th entry. */
4023 skdev->skspcl_table[i - 1].req.next = NULL;
4024 skdev->skspcl_free_list = skdev->skspcl_table;
4025
4026 return rc;
4027
4028err_out:
4029 return rc;
4030}
4031
4032static int skd_cons_sksb(struct skd_device *skdev)
4033{
4034 int rc = 0;
4035 struct skd_special_context *skspcl;
4036 u32 nbytes;
4037
4038 skspcl = &skdev->internal_skspcl;
4039
4040 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4041 skspcl->req.state = SKD_REQ_STATE_IDLE;
4042
4043 nbytes = SKD_N_INTERNAL_BYTES;
4044
Joe Perchesa5bbf612014-08-08 14:24:12 -07004045 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4046 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004047 if (skspcl->data_buf == NULL) {
4048 rc = -ENOMEM;
4049 goto err_out;
4050 }
4051
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004052 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004053 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4054 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004055 if (skspcl->msg_buf == NULL) {
4056 rc = -ENOMEM;
4057 goto err_out;
4058 }
4059
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004060 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4061 &skspcl->req.sksg_dma_address);
4062 if (skspcl->req.sksg_list == NULL) {
4063 rc = -ENOMEM;
4064 goto err_out;
4065 }
4066
4067 if (!skd_format_internal_skspcl(skdev)) {
4068 rc = -EINVAL;
4069 goto err_out;
4070 }
4071
4072err_out:
4073 return rc;
4074}
4075
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004076static int skd_cons_disk(struct skd_device *skdev)
4077{
4078 int rc = 0;
4079 struct gendisk *disk;
4080 struct request_queue *q;
4081 unsigned long flags;
4082
4083 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4084 if (!disk) {
4085 rc = -ENOMEM;
4086 goto err_out;
4087 }
4088
4089 skdev->disk = disk;
4090 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4091
4092 disk->major = skdev->major;
4093 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4094 disk->fops = &skd_blockdev_ops;
4095 disk->private_data = skdev;
4096
Jens Axboefcd37eb2013-11-01 10:14:56 -06004097 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004098 if (!q) {
4099 rc = -ENOMEM;
4100 goto err_out;
4101 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004102 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004103
4104 skdev->queue = q;
4105 disk->queue = q;
4106 q->queuedata = skdev;
4107
Jens Axboe6975f732016-03-30 10:11:42 -06004108 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004109 blk_queue_max_segments(q, skdev->sgs_per_request);
4110 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4111
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004112 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004113 blk_queue_io_opt(q, 8192);
4114
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004115 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004116 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004117
4118 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004119 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06004120 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004121 spin_unlock_irqrestore(&skdev->lock, flags);
4122
4123err_out:
4124 return rc;
4125}
4126
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004127#define SKD_N_DEV_TABLE 16u
4128static u32 skd_next_devno;
4129
4130static struct skd_device *skd_construct(struct pci_dev *pdev)
4131{
4132 struct skd_device *skdev;
4133 int blk_major = skd_major;
4134 int rc;
4135
4136 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4137
4138 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004139 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004140 return NULL;
4141 }
4142
4143 skdev->state = SKD_DRVR_STATE_LOAD;
4144 skdev->pdev = pdev;
4145 skdev->devno = skd_next_devno++;
4146 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004147 skdev->dev_max_queue_depth = 0;
4148
4149 skdev->num_req_context = skd_max_queue_depth;
4150 skdev->num_fitmsg_context = skd_max_queue_depth;
4151 skdev->n_special = skd_max_pass_thru;
4152 skdev->cur_max_queue_depth = 1;
4153 skdev->queue_low_water_mark = 1;
4154 skdev->proto_ver = 99;
4155 skdev->sgs_per_request = skd_sgs_per_request;
4156 skdev->dbg_level = skd_dbg_level;
4157
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004158 spin_lock_init(&skdev->lock);
4159
4160 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4161
Bart Van Asschef98806d2017-08-17 13:12:58 -07004162 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004163 rc = skd_cons_skcomp(skdev);
4164 if (rc < 0)
4165 goto err_out;
4166
Bart Van Asschef98806d2017-08-17 13:12:58 -07004167 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004168 rc = skd_cons_skmsg(skdev);
4169 if (rc < 0)
4170 goto err_out;
4171
Bart Van Asschef98806d2017-08-17 13:12:58 -07004172 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004173 rc = skd_cons_skreq(skdev);
4174 if (rc < 0)
4175 goto err_out;
4176
Bart Van Asschef98806d2017-08-17 13:12:58 -07004177 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004178 rc = skd_cons_skspcl(skdev);
4179 if (rc < 0)
4180 goto err_out;
4181
Bart Van Asschef98806d2017-08-17 13:12:58 -07004182 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004183 rc = skd_cons_sksb(skdev);
4184 if (rc < 0)
4185 goto err_out;
4186
Bart Van Asschef98806d2017-08-17 13:12:58 -07004187 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004188 rc = skd_cons_disk(skdev);
4189 if (rc < 0)
4190 goto err_out;
4191
Bart Van Asschef98806d2017-08-17 13:12:58 -07004192 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004193 return skdev;
4194
4195err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07004196 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004197 skd_destruct(skdev);
4198 return NULL;
4199}
4200
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004201/*
4202 *****************************************************************************
4203 * DESTRUCT (FREE)
4204 *****************************************************************************
4205 */
4206
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004207static void skd_free_skcomp(struct skd_device *skdev)
4208{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07004209 if (skdev->skcomp_table)
4210 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004211 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004212
4213 skdev->skcomp_table = NULL;
4214 skdev->cq_dma_address = 0;
4215}
4216
4217static void skd_free_skmsg(struct skd_device *skdev)
4218{
4219 u32 i;
4220
4221 if (skdev->skmsg_table == NULL)
4222 return;
4223
4224 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4225 struct skd_fitmsg_context *skmsg;
4226
4227 skmsg = &skdev->skmsg_table[i];
4228
4229 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004230 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4231 skmsg->msg_buf,
4232 skmsg->mb_dma_address);
4233 }
4234 skmsg->msg_buf = NULL;
4235 skmsg->mb_dma_address = 0;
4236 }
4237
4238 kfree(skdev->skmsg_table);
4239 skdev->skmsg_table = NULL;
4240}
4241
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004242static void skd_free_sg_list(struct skd_device *skdev,
4243 struct fit_sg_descriptor *sg_list,
4244 u32 n_sg, dma_addr_t dma_addr)
4245{
4246 if (sg_list != NULL) {
4247 u32 nbytes;
4248
4249 nbytes = sizeof(*sg_list) * n_sg;
4250
4251 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4252 }
4253}
4254
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004255static void skd_free_skreq(struct skd_device *skdev)
4256{
4257 u32 i;
4258
4259 if (skdev->skreq_table == NULL)
4260 return;
4261
4262 for (i = 0; i < skdev->num_req_context; i++) {
4263 struct skd_request_context *skreq;
4264
4265 skreq = &skdev->skreq_table[i];
4266
4267 skd_free_sg_list(skdev, skreq->sksg_list,
4268 skdev->sgs_per_request,
4269 skreq->sksg_dma_address);
4270
4271 skreq->sksg_list = NULL;
4272 skreq->sksg_dma_address = 0;
4273
4274 kfree(skreq->sg);
4275 }
4276
4277 kfree(skdev->skreq_table);
4278 skdev->skreq_table = NULL;
4279}
4280
4281static void skd_free_skspcl(struct skd_device *skdev)
4282{
4283 u32 i;
4284 u32 nbytes;
4285
4286 if (skdev->skspcl_table == NULL)
4287 return;
4288
4289 for (i = 0; i < skdev->n_special; i++) {
4290 struct skd_special_context *skspcl;
4291
4292 skspcl = &skdev->skspcl_table[i];
4293
4294 if (skspcl->msg_buf != NULL) {
4295 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4296 pci_free_consistent(skdev->pdev, nbytes,
4297 skspcl->msg_buf,
4298 skspcl->mb_dma_address);
4299 }
4300
4301 skspcl->msg_buf = NULL;
4302 skspcl->mb_dma_address = 0;
4303
4304 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4305 SKD_N_SG_PER_SPECIAL,
4306 skspcl->req.sksg_dma_address);
4307
4308 skspcl->req.sksg_list = NULL;
4309 skspcl->req.sksg_dma_address = 0;
4310
4311 kfree(skspcl->req.sg);
4312 }
4313
4314 kfree(skdev->skspcl_table);
4315 skdev->skspcl_table = NULL;
4316}
4317
4318static void skd_free_sksb(struct skd_device *skdev)
4319{
4320 struct skd_special_context *skspcl;
4321 u32 nbytes;
4322
4323 skspcl = &skdev->internal_skspcl;
4324
4325 if (skspcl->data_buf != NULL) {
4326 nbytes = SKD_N_INTERNAL_BYTES;
4327
4328 pci_free_consistent(skdev->pdev, nbytes,
4329 skspcl->data_buf, skspcl->db_dma_address);
4330 }
4331
4332 skspcl->data_buf = NULL;
4333 skspcl->db_dma_address = 0;
4334
4335 if (skspcl->msg_buf != NULL) {
4336 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4337 pci_free_consistent(skdev->pdev, nbytes,
4338 skspcl->msg_buf, skspcl->mb_dma_address);
4339 }
4340
4341 skspcl->msg_buf = NULL;
4342 skspcl->mb_dma_address = 0;
4343
4344 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4345 skspcl->req.sksg_dma_address);
4346
4347 skspcl->req.sksg_list = NULL;
4348 skspcl->req.sksg_dma_address = 0;
4349}
4350
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004351static void skd_free_disk(struct skd_device *skdev)
4352{
4353 struct gendisk *disk = skdev->disk;
4354
Bart Van Assche7277cc62017-08-17 13:12:45 -07004355 if (disk && (disk->flags & GENHD_FL_UP))
4356 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004357
Bart Van Assche7277cc62017-08-17 13:12:45 -07004358 if (skdev->queue) {
4359 blk_cleanup_queue(skdev->queue);
4360 skdev->queue = NULL;
4361 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004362 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004363
4364 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004365 skdev->disk = NULL;
4366}
4367
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004368static void skd_destruct(struct skd_device *skdev)
4369{
4370 if (skdev == NULL)
4371 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004372
Bart Van Asschef98806d2017-08-17 13:12:58 -07004373 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004374 skd_free_disk(skdev);
4375
Bart Van Asschef98806d2017-08-17 13:12:58 -07004376 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004377 skd_free_sksb(skdev);
4378
Bart Van Asschef98806d2017-08-17 13:12:58 -07004379 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004380 skd_free_skspcl(skdev);
4381
Bart Van Asschef98806d2017-08-17 13:12:58 -07004382 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004383 skd_free_skreq(skdev);
4384
Bart Van Asschef98806d2017-08-17 13:12:58 -07004385 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004386 skd_free_skmsg(skdev);
4387
Bart Van Asschef98806d2017-08-17 13:12:58 -07004388 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004389 skd_free_skcomp(skdev);
4390
Bart Van Asschef98806d2017-08-17 13:12:58 -07004391 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004392 kfree(skdev);
4393}
4394
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004395/*
4396 *****************************************************************************
4397 * BLOCK DEVICE (BDEV) GLUE
4398 *****************************************************************************
4399 */
4400
4401static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4402{
4403 struct skd_device *skdev;
4404 u64 capacity;
4405
4406 skdev = bdev->bd_disk->private_data;
4407
Bart Van Asschef98806d2017-08-17 13:12:58 -07004408 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
4409 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004410
4411 if (skdev->read_cap_is_valid) {
4412 capacity = get_capacity(skdev->disk);
4413 geo->heads = 64;
4414 geo->sectors = 255;
4415 geo->cylinders = (capacity) / (255 * 64);
4416
4417 return 0;
4418 }
4419 return -EIO;
4420}
4421
Dan Williams0d52c7562016-06-15 19:44:20 -07004422static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004423{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004424 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07004425 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004426 return 0;
4427}
4428
4429static const struct block_device_operations skd_blockdev_ops = {
4430 .owner = THIS_MODULE,
4431 .ioctl = skd_bdev_ioctl,
4432 .getgeo = skd_bdev_getgeo,
4433};
4434
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004435/*
4436 *****************************************************************************
4437 * PCIe DRIVER GLUE
4438 *****************************************************************************
4439 */
4440
Benoit Taine9baa3c32014-08-08 15:56:03 +02004441static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004442 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4443 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4444 { 0 } /* terminate list */
4445};
4446
4447MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4448
4449static char *skd_pci_info(struct skd_device *skdev, char *str)
4450{
4451 int pcie_reg;
4452
4453 strcpy(str, "PCIe (");
4454 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4455
4456 if (pcie_reg) {
4457
4458 char lwstr[6];
4459 uint16_t pcie_lstat, lspeed, lwidth;
4460
4461 pcie_reg += 0x12;
4462 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4463 lspeed = pcie_lstat & (0xF);
4464 lwidth = (pcie_lstat & 0x3F0) >> 4;
4465
4466 if (lspeed == 1)
4467 strcat(str, "2.5GT/s ");
4468 else if (lspeed == 2)
4469 strcat(str, "5.0GT/s ");
4470 else
4471 strcat(str, "<unknown> ");
4472 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4473 strcat(str, lwstr);
4474 }
4475 return str;
4476}
4477
4478static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4479{
4480 int i;
4481 int rc = 0;
4482 char pci_str[32];
4483 struct skd_device *skdev;
4484
Bart Van Asschef98806d2017-08-17 13:12:58 -07004485 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
4486 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4487 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
4488 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004489
4490 rc = pci_enable_device(pdev);
4491 if (rc)
4492 return rc;
4493 rc = pci_request_regions(pdev, DRV_NAME);
4494 if (rc)
4495 goto err_out;
4496 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4497 if (!rc) {
4498 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004499 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4500 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004501 }
4502 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004503 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004504 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004505 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004506 goto err_out_regions;
4507 }
4508 }
4509
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004510 if (!skd_major) {
4511 rc = register_blkdev(0, DRV_NAME);
4512 if (rc < 0)
4513 goto err_out_regions;
4514 BUG_ON(!rc);
4515 skd_major = rc;
4516 }
4517
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004518 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004519 if (skdev == NULL) {
4520 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004521 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004522 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004523
4524 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004525 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004526
4527 pci_set_master(pdev);
4528 rc = pci_enable_pcie_error_reporting(pdev);
4529 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004530 dev_err(&pdev->dev,
4531 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004532 skdev->pcie_error_reporting_is_enabled = 0;
4533 } else
4534 skdev->pcie_error_reporting_is_enabled = 1;
4535
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004536 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004537
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004538 for (i = 0; i < SKD_MAX_BARS; i++) {
4539 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4540 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4541 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4542 skdev->mem_size[i]);
4543 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004544 dev_err(&pdev->dev,
4545 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004546 rc = -ENODEV;
4547 goto err_out_iounmap;
4548 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004549 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4550 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4551 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004552 }
4553
4554 rc = skd_acquire_irq(skdev);
4555 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004556 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004557 goto err_out_iounmap;
4558 }
4559
4560 rc = skd_start_timer(skdev);
4561 if (rc)
4562 goto err_out_timer;
4563
4564 init_waitqueue_head(&skdev->waitq);
4565
4566 skd_start_device(skdev);
4567
4568 rc = wait_event_interruptible_timeout(skdev->waitq,
4569 (skdev->gendisk_on),
4570 (SKD_START_WAIT_SECONDS * HZ));
4571 if (skdev->gendisk_on > 0) {
4572 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004573 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004574 rc = 0;
4575 } else {
4576 /* we timed out, something is wrong with the device,
4577 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07004578 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
4579 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004580 /* in case of no error; we timeout with ENXIO */
4581 if (!rc)
4582 rc = -ENXIO;
4583 goto err_out_timer;
4584 }
4585
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004586 return rc;
4587
4588err_out_timer:
4589 skd_stop_device(skdev);
4590 skd_release_irq(skdev);
4591
4592err_out_iounmap:
4593 for (i = 0; i < SKD_MAX_BARS; i++)
4594 if (skdev->mem_map[i])
4595 iounmap(skdev->mem_map[i]);
4596
4597 if (skdev->pcie_error_reporting_is_enabled)
4598 pci_disable_pcie_error_reporting(pdev);
4599
4600 skd_destruct(skdev);
4601
4602err_out_regions:
4603 pci_release_regions(pdev);
4604
4605err_out:
4606 pci_disable_device(pdev);
4607 pci_set_drvdata(pdev, NULL);
4608 return rc;
4609}
4610
4611static void skd_pci_remove(struct pci_dev *pdev)
4612{
4613 int i;
4614 struct skd_device *skdev;
4615
4616 skdev = pci_get_drvdata(pdev);
4617 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004618 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004619 return;
4620 }
4621 skd_stop_device(skdev);
4622 skd_release_irq(skdev);
4623
4624 for (i = 0; i < SKD_MAX_BARS; i++)
4625 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004626 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004627
4628 if (skdev->pcie_error_reporting_is_enabled)
4629 pci_disable_pcie_error_reporting(pdev);
4630
4631 skd_destruct(skdev);
4632
4633 pci_release_regions(pdev);
4634 pci_disable_device(pdev);
4635 pci_set_drvdata(pdev, NULL);
4636
4637 return;
4638}
4639
4640static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4641{
4642 int i;
4643 struct skd_device *skdev;
4644
4645 skdev = pci_get_drvdata(pdev);
4646 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004647 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004648 return -EIO;
4649 }
4650
4651 skd_stop_device(skdev);
4652
4653 skd_release_irq(skdev);
4654
4655 for (i = 0; i < SKD_MAX_BARS; i++)
4656 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004657 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004658
4659 if (skdev->pcie_error_reporting_is_enabled)
4660 pci_disable_pcie_error_reporting(pdev);
4661
4662 pci_release_regions(pdev);
4663 pci_save_state(pdev);
4664 pci_disable_device(pdev);
4665 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4666 return 0;
4667}
4668
4669static int skd_pci_resume(struct pci_dev *pdev)
4670{
4671 int i;
4672 int rc = 0;
4673 struct skd_device *skdev;
4674
4675 skdev = pci_get_drvdata(pdev);
4676 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004677 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004678 return -1;
4679 }
4680
4681 pci_set_power_state(pdev, PCI_D0);
4682 pci_enable_wake(pdev, PCI_D0, 0);
4683 pci_restore_state(pdev);
4684
4685 rc = pci_enable_device(pdev);
4686 if (rc)
4687 return rc;
4688 rc = pci_request_regions(pdev, DRV_NAME);
4689 if (rc)
4690 goto err_out;
4691 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4692 if (!rc) {
4693 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4694
Bart Van Asschef98806d2017-08-17 13:12:58 -07004695 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4696 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004697 }
4698 } else {
4699 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4700 if (rc) {
4701
Bart Van Asschef98806d2017-08-17 13:12:58 -07004702 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004703 goto err_out_regions;
4704 }
4705 }
4706
4707 pci_set_master(pdev);
4708 rc = pci_enable_pcie_error_reporting(pdev);
4709 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004710 dev_err(&pdev->dev,
4711 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004712 skdev->pcie_error_reporting_is_enabled = 0;
4713 } else
4714 skdev->pcie_error_reporting_is_enabled = 1;
4715
4716 for (i = 0; i < SKD_MAX_BARS; i++) {
4717
4718 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4719 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4720 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4721 skdev->mem_size[i]);
4722 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004723 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004724 rc = -ENODEV;
4725 goto err_out_iounmap;
4726 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004727 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4728 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4729 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004730 }
4731 rc = skd_acquire_irq(skdev);
4732 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004733 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004734 goto err_out_iounmap;
4735 }
4736
4737 rc = skd_start_timer(skdev);
4738 if (rc)
4739 goto err_out_timer;
4740
4741 init_waitqueue_head(&skdev->waitq);
4742
4743 skd_start_device(skdev);
4744
4745 return rc;
4746
4747err_out_timer:
4748 skd_stop_device(skdev);
4749 skd_release_irq(skdev);
4750
4751err_out_iounmap:
4752 for (i = 0; i < SKD_MAX_BARS; i++)
4753 if (skdev->mem_map[i])
4754 iounmap(skdev->mem_map[i]);
4755
4756 if (skdev->pcie_error_reporting_is_enabled)
4757 pci_disable_pcie_error_reporting(pdev);
4758
4759err_out_regions:
4760 pci_release_regions(pdev);
4761
4762err_out:
4763 pci_disable_device(pdev);
4764 return rc;
4765}
4766
4767static void skd_pci_shutdown(struct pci_dev *pdev)
4768{
4769 struct skd_device *skdev;
4770
Bart Van Asschef98806d2017-08-17 13:12:58 -07004771 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004772
4773 skdev = pci_get_drvdata(pdev);
4774 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004775 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004776 return;
4777 }
4778
Bart Van Asschef98806d2017-08-17 13:12:58 -07004779 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004780 skd_stop_device(skdev);
4781}
4782
4783static struct pci_driver skd_driver = {
4784 .name = DRV_NAME,
4785 .id_table = skd_pci_tbl,
4786 .probe = skd_pci_probe,
4787 .remove = skd_pci_remove,
4788 .suspend = skd_pci_suspend,
4789 .resume = skd_pci_resume,
4790 .shutdown = skd_pci_shutdown,
4791};
4792
4793/*
4794 *****************************************************************************
4795 * LOGGING SUPPORT
4796 *****************************************************************************
4797 */
4798
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004799const char *skd_drive_state_to_str(int state)
4800{
4801 switch (state) {
4802 case FIT_SR_DRIVE_OFFLINE:
4803 return "OFFLINE";
4804 case FIT_SR_DRIVE_INIT:
4805 return "INIT";
4806 case FIT_SR_DRIVE_ONLINE:
4807 return "ONLINE";
4808 case FIT_SR_DRIVE_BUSY:
4809 return "BUSY";
4810 case FIT_SR_DRIVE_FAULT:
4811 return "FAULT";
4812 case FIT_SR_DRIVE_DEGRADED:
4813 return "DEGRADED";
4814 case FIT_SR_PCIE_LINK_DOWN:
4815 return "INK_DOWN";
4816 case FIT_SR_DRIVE_SOFT_RESET:
4817 return "SOFT_RESET";
4818 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4819 return "NEED_FW";
4820 case FIT_SR_DRIVE_INIT_FAULT:
4821 return "INIT_FAULT";
4822 case FIT_SR_DRIVE_BUSY_SANITIZE:
4823 return "BUSY_SANITIZE";
4824 case FIT_SR_DRIVE_BUSY_ERASE:
4825 return "BUSY_ERASE";
4826 case FIT_SR_DRIVE_FW_BOOTING:
4827 return "FW_BOOTING";
4828 default:
4829 return "???";
4830 }
4831}
4832
4833const char *skd_skdev_state_to_str(enum skd_drvr_state state)
4834{
4835 switch (state) {
4836 case SKD_DRVR_STATE_LOAD:
4837 return "LOAD";
4838 case SKD_DRVR_STATE_IDLE:
4839 return "IDLE";
4840 case SKD_DRVR_STATE_BUSY:
4841 return "BUSY";
4842 case SKD_DRVR_STATE_STARTING:
4843 return "STARTING";
4844 case SKD_DRVR_STATE_ONLINE:
4845 return "ONLINE";
4846 case SKD_DRVR_STATE_PAUSING:
4847 return "PAUSING";
4848 case SKD_DRVR_STATE_PAUSED:
4849 return "PAUSED";
4850 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
4851 return "DRAINING_TIMEOUT";
4852 case SKD_DRVR_STATE_RESTARTING:
4853 return "RESTARTING";
4854 case SKD_DRVR_STATE_RESUMING:
4855 return "RESUMING";
4856 case SKD_DRVR_STATE_STOPPING:
4857 return "STOPPING";
4858 case SKD_DRVR_STATE_SYNCING:
4859 return "SYNCING";
4860 case SKD_DRVR_STATE_FAULT:
4861 return "FAULT";
4862 case SKD_DRVR_STATE_DISAPPEARED:
4863 return "DISAPPEARED";
4864 case SKD_DRVR_STATE_BUSY_ERASE:
4865 return "BUSY_ERASE";
4866 case SKD_DRVR_STATE_BUSY_SANITIZE:
4867 return "BUSY_SANITIZE";
4868 case SKD_DRVR_STATE_BUSY_IMMINENT:
4869 return "BUSY_IMMINENT";
4870 case SKD_DRVR_STATE_WAIT_BOOT:
4871 return "WAIT_BOOT";
4872
4873 default:
4874 return "???";
4875 }
4876}
4877
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304878static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004879{
4880 switch (state) {
4881 case SKD_MSG_STATE_IDLE:
4882 return "IDLE";
4883 case SKD_MSG_STATE_BUSY:
4884 return "BUSY";
4885 default:
4886 return "???";
4887 }
4888}
4889
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304890static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004891{
4892 switch (state) {
4893 case SKD_REQ_STATE_IDLE:
4894 return "IDLE";
4895 case SKD_REQ_STATE_SETUP:
4896 return "SETUP";
4897 case SKD_REQ_STATE_BUSY:
4898 return "BUSY";
4899 case SKD_REQ_STATE_COMPLETED:
4900 return "COMPLETED";
4901 case SKD_REQ_STATE_TIMEOUT:
4902 return "TIMEOUT";
4903 case SKD_REQ_STATE_ABORTED:
4904 return "ABORTED";
4905 default:
4906 return "???";
4907 }
4908}
4909
4910static void skd_log_skdev(struct skd_device *skdev, const char *event)
4911{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004912 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
4913 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
4914 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
4915 skd_skdev_state_to_str(skdev->state), skdev->state);
4916 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
4917 skdev->in_flight, skdev->cur_max_queue_depth,
4918 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
4919 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
4920 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004921}
4922
4923static void skd_log_skmsg(struct skd_device *skdev,
4924 struct skd_fitmsg_context *skmsg, const char *event)
4925{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004926 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
4927 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
4928 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
4929 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004930}
4931
4932static void skd_log_skreq(struct skd_device *skdev,
4933 struct skd_request_context *skreq, const char *event)
4934{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004935 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
4936 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
4937 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
4938 skreq->fitmsg_id);
4939 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
4940 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004941
Jens Axboefcd37eb2013-11-01 10:14:56 -06004942 if (skreq->req != NULL) {
4943 struct request *req = skreq->req;
4944 u32 lba = (u32)blk_rq_pos(req);
4945 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004946
Bart Van Asschef98806d2017-08-17 13:12:58 -07004947 dev_dbg(&skdev->pdev->dev,
4948 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
4949 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06004950 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07004951 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004952}
4953
4954/*
4955 *****************************************************************************
4956 * MODULE GLUE
4957 *****************************************************************************
4958 */
4959
4960static int __init skd_init(void)
4961{
Bart Van Assche16a70532017-08-17 13:13:08 -07004962 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
4963 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
4964 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
4965 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
4966 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07004967 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
4968 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
4969 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07004970
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004971 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
4972
4973 switch (skd_isr_type) {
4974 case SKD_IRQ_LEGACY:
4975 case SKD_IRQ_MSI:
4976 case SKD_IRQ_MSIX:
4977 break;
4978 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004979 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004980 skd_isr_type, SKD_IRQ_DEFAULT);
4981 skd_isr_type = SKD_IRQ_DEFAULT;
4982 }
4983
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004984 if (skd_max_queue_depth < 1 ||
4985 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
4986 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004987 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
4988 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
4989 }
4990
Bart Van Assche2da7b402017-08-17 13:13:01 -07004991 if (skd_max_req_per_msg < 1 ||
4992 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004993 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004994 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
4995 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
4996 }
4997
4998 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004999 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005000 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
5001 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5002 }
5003
5004 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005005 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005006 skd_dbg_level, 0);
5007 skd_dbg_level = 0;
5008 }
5009
5010 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005011 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005012 skd_isr_comp_limit, 0);
5013 skd_isr_comp_limit = 0;
5014 }
5015
5016 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005017 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005018 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5019 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5020 }
5021
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005022 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005023}
5024
5025static void __exit skd_exit(void)
5026{
5027 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5028
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005029 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005030
5031 if (skd_major)
5032 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005033}
5034
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005035module_init(skd_init);
5036module_exit(skd_exit);