blob: 7d5048d95037c9c13ab367cefad574cd5bf7d4ac [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{
Bart Van Assche1cd3c1a2017-08-17 13:13:10 -07002833 struct skd_device *skdev = ptr;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002834 u32 intstat;
2835 u32 ack;
2836 int rc = 0;
2837 int deferred = 0;
2838 int flush_enqueued = 0;
2839
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002840 spin_lock(&skdev->lock);
2841
2842 for (;; ) {
2843 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2844
2845 ack = FIT_INT_DEF_MASK;
2846 ack &= intstat;
2847
Bart Van Asschef98806d2017-08-17 13:12:58 -07002848 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
2849 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002850
2851 /* As long as there is an int pending on device, keep
2852 * running loop. When none, get out, but if we've never
2853 * done any processing, call completion handler?
2854 */
2855 if (ack == 0) {
2856 /* No interrupts on device, but run the completion
2857 * processor anyway?
2858 */
2859 if (rc == 0)
2860 if (likely (skdev->state
2861 == SKD_DRVR_STATE_ONLINE))
2862 deferred = 1;
2863 break;
2864 }
2865
2866 rc = IRQ_HANDLED;
2867
2868 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2869
2870 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2871 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2872 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2873 /*
2874 * If we have already deferred completion
2875 * processing, don't bother running it again
2876 */
2877 if (deferred == 0)
2878 deferred =
2879 skd_isr_completion_posted(skdev,
2880 skd_isr_comp_limit, &flush_enqueued);
2881 }
2882
2883 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2884 skd_isr_fwstate(skdev);
2885 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2886 skdev->state ==
2887 SKD_DRVR_STATE_DISAPPEARED) {
2888 spin_unlock(&skdev->lock);
2889 return rc;
2890 }
2891 }
2892
2893 if (intstat & FIT_ISH_MSG_FROM_DEV)
2894 skd_isr_msg_from_dev(skdev);
2895 }
2896 }
2897
2898 if (unlikely(flush_enqueued))
2899 skd_request_fn(skdev->queue);
2900
2901 if (deferred)
2902 schedule_work(&skdev->completion_worker);
2903 else if (!flush_enqueued)
2904 skd_request_fn(skdev->queue);
2905
2906 spin_unlock(&skdev->lock);
2907
2908 return rc;
2909}
2910
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002911static void skd_drive_fault(struct skd_device *skdev)
2912{
2913 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002914 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002915}
2916
2917static void skd_drive_disappeared(struct skd_device *skdev)
2918{
2919 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002920 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002921}
2922
2923static void skd_isr_fwstate(struct skd_device *skdev)
2924{
2925 u32 sense;
2926 u32 state;
2927 u32 mtd;
2928 int prev_driver_state = skdev->state;
2929
2930 sense = SKD_READL(skdev, FIT_STATUS);
2931 state = sense & FIT_SR_DRIVE_STATE_MASK;
2932
Bart Van Asschef98806d2017-08-17 13:12:58 -07002933 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
2934 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
2935 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002936
2937 skdev->drive_state = state;
2938
2939 switch (skdev->drive_state) {
2940 case FIT_SR_DRIVE_INIT:
2941 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
2942 skd_disable_interrupts(skdev);
2943 break;
2944 }
2945 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
2946 skd_recover_requests(skdev, 0);
2947 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2948 skdev->timer_countdown = SKD_STARTING_TIMO;
2949 skdev->state = SKD_DRVR_STATE_STARTING;
2950 skd_soft_reset(skdev);
2951 break;
2952 }
2953 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2954 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2955 skdev->last_mtd = mtd;
2956 break;
2957
2958 case FIT_SR_DRIVE_ONLINE:
2959 skdev->cur_max_queue_depth = skd_max_queue_depth;
2960 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2961 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2962
2963 skdev->queue_low_water_mark =
2964 skdev->cur_max_queue_depth * 2 / 3 + 1;
2965 if (skdev->queue_low_water_mark < 1)
2966 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002967 dev_info(&skdev->pdev->dev,
2968 "Queue depth limit=%d dev=%d lowat=%d\n",
2969 skdev->cur_max_queue_depth,
2970 skdev->dev_max_queue_depth,
2971 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002972
2973 skd_refresh_device_data(skdev);
2974 break;
2975
2976 case FIT_SR_DRIVE_BUSY:
2977 skdev->state = SKD_DRVR_STATE_BUSY;
2978 skdev->timer_countdown = SKD_BUSY_TIMO;
2979 skd_quiesce_dev(skdev);
2980 break;
2981 case FIT_SR_DRIVE_BUSY_SANITIZE:
2982 /* set timer for 3 seconds, we'll abort any unfinished
2983 * commands after that expires
2984 */
2985 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2986 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002987 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002988 break;
2989 case FIT_SR_DRIVE_BUSY_ERASE:
2990 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2991 skdev->timer_countdown = SKD_BUSY_TIMO;
2992 break;
2993 case FIT_SR_DRIVE_OFFLINE:
2994 skdev->state = SKD_DRVR_STATE_IDLE;
2995 break;
2996 case FIT_SR_DRIVE_SOFT_RESET:
2997 switch (skdev->state) {
2998 case SKD_DRVR_STATE_STARTING:
2999 case SKD_DRVR_STATE_RESTARTING:
3000 /* Expected by a caller of skd_soft_reset() */
3001 break;
3002 default:
3003 skdev->state = SKD_DRVR_STATE_RESTARTING;
3004 break;
3005 }
3006 break;
3007 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003008 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003009 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3010 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3011 break;
3012
3013 case FIT_SR_DRIVE_DEGRADED:
3014 case FIT_SR_PCIE_LINK_DOWN:
3015 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3016 break;
3017
3018 case FIT_SR_DRIVE_FAULT:
3019 skd_drive_fault(skdev);
3020 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003021 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003022 break;
3023
3024 /* PCIe bus returned all Fs? */
3025 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003026 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
3027 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003028 skd_drive_disappeared(skdev);
3029 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003030 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003031 break;
3032 default:
3033 /*
3034 * Uknown FW State. Wait for a state we recognize.
3035 */
3036 break;
3037 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003038 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3039 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3040 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003041}
3042
3043static void skd_recover_requests(struct skd_device *skdev, int requeue)
3044{
3045 int i;
3046
3047 for (i = 0; i < skdev->num_req_context; i++) {
3048 struct skd_request_context *skreq = &skdev->skreq_table[i];
3049
3050 if (skreq->state == SKD_REQ_STATE_BUSY) {
3051 skd_log_skreq(skdev, skreq, "recover");
3052
3053 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003054 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003055
3056 /* Release DMA resources for the request. */
3057 if (skreq->n_sg > 0)
3058 skd_postop_sg_list(skdev, skreq);
3059
Jens Axboefcd37eb2013-11-01 10:14:56 -06003060 if (requeue &&
3061 (unsigned long) ++skreq->req->special <
3062 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003063 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003064 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003065 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003066
Jens Axboefcd37eb2013-11-01 10:14:56 -06003067 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003068
3069 skreq->state = SKD_REQ_STATE_IDLE;
3070 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003071 }
3072 if (i > 0)
3073 skreq[-1].next = skreq;
3074 skreq->next = NULL;
3075 }
3076 skdev->skreq_free_list = skdev->skreq_table;
3077
3078 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3079 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3080
3081 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3082 skd_log_skmsg(skdev, skmsg, "salvaged");
3083 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3084 skmsg->state = SKD_MSG_STATE_IDLE;
3085 skmsg->id += SKD_ID_INCR;
3086 }
3087 if (i > 0)
3088 skmsg[-1].next = skmsg;
3089 skmsg->next = NULL;
3090 }
3091 skdev->skmsg_free_list = skdev->skmsg_table;
3092
3093 for (i = 0; i < skdev->n_special; i++) {
3094 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3095
3096 /* If orphaned, reclaim it because it has already been reported
3097 * to the process as an error (it was just waiting for
3098 * a completion that didn't come, and now it will never come)
3099 * If busy, change to a state that will cause it to error
3100 * out in the wait routine and let it do the normal
3101 * reporting and reclaiming
3102 */
3103 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3104 if (skspcl->orphaned) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003105 dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
3106 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003107 skd_release_special(skdev, skspcl);
3108 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003109 dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
3110 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003111 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3112 }
3113 }
3114 }
3115 skdev->skspcl_free_list = skdev->skspcl_table;
3116
3117 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3118 skdev->timeout_slot[i] = 0;
3119
3120 skdev->in_flight = 0;
3121}
3122
3123static void skd_isr_msg_from_dev(struct skd_device *skdev)
3124{
3125 u32 mfd;
3126 u32 mtd;
3127 u32 data;
3128
3129 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3130
Bart Van Asschef98806d2017-08-17 13:12:58 -07003131 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
3132 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003133
3134 /* ignore any mtd that is an ack for something we didn't send */
3135 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3136 return;
3137
3138 switch (FIT_MXD_TYPE(mfd)) {
3139 case FIT_MTD_FITFW_INIT:
3140 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3141
3142 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003143 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
3144 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
3145 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
3146 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003147 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3148 skd_soft_reset(skdev);
3149 break;
3150 }
3151 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3152 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3153 skdev->last_mtd = mtd;
3154 break;
3155
3156 case FIT_MTD_GET_CMDQ_DEPTH:
3157 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3158 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3159 SKD_N_COMPLETION_ENTRY);
3160 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3161 skdev->last_mtd = mtd;
3162 break;
3163
3164 case FIT_MTD_SET_COMPQ_DEPTH:
3165 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3166 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3167 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3168 skdev->last_mtd = mtd;
3169 break;
3170
3171 case FIT_MTD_SET_COMPQ_ADDR:
3172 skd_reset_skcomp(skdev);
3173 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3174 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3175 skdev->last_mtd = mtd;
3176 break;
3177
3178 case FIT_MTD_CMD_LOG_HOST_ID:
3179 skdev->connect_time_stamp = get_seconds();
3180 data = skdev->connect_time_stamp & 0xFFFF;
3181 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3182 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3183 skdev->last_mtd = mtd;
3184 break;
3185
3186 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3187 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3188 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3189 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3190 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3191 skdev->last_mtd = mtd;
3192 break;
3193
3194 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3195 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3196 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3197 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3198 skdev->last_mtd = mtd;
3199
Bart Van Asschef98806d2017-08-17 13:12:58 -07003200 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
3201 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003202 break;
3203
3204 case FIT_MTD_ARM_QUEUE:
3205 skdev->last_mtd = 0;
3206 /*
3207 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3208 */
3209 break;
3210
3211 default:
3212 break;
3213 }
3214}
3215
3216static void skd_disable_interrupts(struct skd_device *skdev)
3217{
3218 u32 sense;
3219
3220 sense = SKD_READL(skdev, FIT_CONTROL);
3221 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3222 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003223 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003224
3225 /* Note that the 1s is written. A 1-bit means
3226 * disable, a 0 means enable.
3227 */
3228 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3229}
3230
3231static void skd_enable_interrupts(struct skd_device *skdev)
3232{
3233 u32 val;
3234
3235 /* unmask interrupts first */
3236 val = FIT_ISH_FW_STATE_CHANGE +
3237 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3238
3239 /* Note that the compliment of mask is written. A 1-bit means
3240 * disable, a 0 means enable. */
3241 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003242 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003243
3244 val = SKD_READL(skdev, FIT_CONTROL);
3245 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003246 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003247 SKD_WRITEL(skdev, val, FIT_CONTROL);
3248}
3249
3250/*
3251 *****************************************************************************
3252 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3253 *****************************************************************************
3254 */
3255
3256static void skd_soft_reset(struct skd_device *skdev)
3257{
3258 u32 val;
3259
3260 val = SKD_READL(skdev, FIT_CONTROL);
3261 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003262 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003263 SKD_WRITEL(skdev, val, FIT_CONTROL);
3264}
3265
3266static void skd_start_device(struct skd_device *skdev)
3267{
3268 unsigned long flags;
3269 u32 sense;
3270 u32 state;
3271
3272 spin_lock_irqsave(&skdev->lock, flags);
3273
3274 /* ack all ghost interrupts */
3275 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3276
3277 sense = SKD_READL(skdev, FIT_STATUS);
3278
Bart Van Asschef98806d2017-08-17 13:12:58 -07003279 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003280
3281 state = sense & FIT_SR_DRIVE_STATE_MASK;
3282 skdev->drive_state = state;
3283 skdev->last_mtd = 0;
3284
3285 skdev->state = SKD_DRVR_STATE_STARTING;
3286 skdev->timer_countdown = SKD_STARTING_TIMO;
3287
3288 skd_enable_interrupts(skdev);
3289
3290 switch (skdev->drive_state) {
3291 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003292 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003293 break;
3294
3295 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003296 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003297 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3298 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3299 break;
3300
3301 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003302 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003303 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3304 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3305 break;
3306
3307 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003308 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003309 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3310 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3311 break;
3312
3313 case FIT_SR_DRIVE_INIT:
3314 case FIT_SR_DRIVE_ONLINE:
3315 skd_soft_reset(skdev);
3316 break;
3317
3318 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003319 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003320 skdev->state = SKD_DRVR_STATE_BUSY;
3321 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3322 break;
3323
3324 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003325 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003326 break;
3327
3328 case FIT_SR_DRIVE_FAULT:
3329 /* Fault state is bad...soft reset won't do it...
3330 * Hard reset, maybe, but does it work on device?
3331 * For now, just fault so the system doesn't hang.
3332 */
3333 skd_drive_fault(skdev);
3334 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003335 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003336 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003337 skdev->gendisk_on = -1;
3338 wake_up_interruptible(&skdev->waitq);
3339 break;
3340
3341 case 0xFF:
3342 /* Most likely the device isn't there or isn't responding
3343 * to the BAR1 addresses. */
3344 skd_drive_disappeared(skdev);
3345 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003346 dev_dbg(&skdev->pdev->dev,
3347 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003348 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003349 skdev->gendisk_on = -1;
3350 wake_up_interruptible(&skdev->waitq);
3351 break;
3352
3353 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003354 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
3355 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003356 break;
3357 }
3358
3359 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003360 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003361
3362 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003363 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003364
3365 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003366 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003367
3368 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003369 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003370
3371 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003372 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003373
3374 spin_unlock_irqrestore(&skdev->lock, flags);
3375}
3376
3377static void skd_stop_device(struct skd_device *skdev)
3378{
3379 unsigned long flags;
3380 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3381 u32 dev_state;
3382 int i;
3383
3384 spin_lock_irqsave(&skdev->lock, flags);
3385
3386 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003387 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003388 goto stop_out;
3389 }
3390
3391 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003392 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003393 goto stop_out;
3394 }
3395
3396 skdev->state = SKD_DRVR_STATE_SYNCING;
3397 skdev->sync_done = 0;
3398
3399 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3400
3401 spin_unlock_irqrestore(&skdev->lock, flags);
3402
3403 wait_event_interruptible_timeout(skdev->waitq,
3404 (skdev->sync_done), (10 * HZ));
3405
3406 spin_lock_irqsave(&skdev->lock, flags);
3407
3408 switch (skdev->sync_done) {
3409 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003410 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003411 break;
3412 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003413 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003414 break;
3415 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003416 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003417 }
3418
3419stop_out:
3420 skdev->state = SKD_DRVR_STATE_STOPPING;
3421 spin_unlock_irqrestore(&skdev->lock, flags);
3422
3423 skd_kill_timer(skdev);
3424
3425 spin_lock_irqsave(&skdev->lock, flags);
3426 skd_disable_interrupts(skdev);
3427
3428 /* ensure all ints on device are cleared */
3429 /* soft reset the device to unload with a clean slate */
3430 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3431 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3432
3433 spin_unlock_irqrestore(&skdev->lock, flags);
3434
3435 /* poll every 100ms, 1 second timeout */
3436 for (i = 0; i < 10; i++) {
3437 dev_state =
3438 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3439 if (dev_state == FIT_SR_DRIVE_INIT)
3440 break;
3441 set_current_state(TASK_INTERRUPTIBLE);
3442 schedule_timeout(msecs_to_jiffies(100));
3443 }
3444
3445 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07003446 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
3447 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003448}
3449
3450/* assume spinlock is held */
3451static void skd_restart_device(struct skd_device *skdev)
3452{
3453 u32 state;
3454
3455 /* ack all ghost interrupts */
3456 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3457
3458 state = SKD_READL(skdev, FIT_STATUS);
3459
Bart Van Asschef98806d2017-08-17 13:12:58 -07003460 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003461
3462 state &= FIT_SR_DRIVE_STATE_MASK;
3463 skdev->drive_state = state;
3464 skdev->last_mtd = 0;
3465
3466 skdev->state = SKD_DRVR_STATE_RESTARTING;
3467 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3468
3469 skd_soft_reset(skdev);
3470}
3471
3472/* assume spinlock is held */
3473static int skd_quiesce_dev(struct skd_device *skdev)
3474{
3475 int rc = 0;
3476
3477 switch (skdev->state) {
3478 case SKD_DRVR_STATE_BUSY:
3479 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003480 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003481 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003482 break;
3483 case SKD_DRVR_STATE_ONLINE:
3484 case SKD_DRVR_STATE_STOPPING:
3485 case SKD_DRVR_STATE_SYNCING:
3486 case SKD_DRVR_STATE_PAUSING:
3487 case SKD_DRVR_STATE_PAUSED:
3488 case SKD_DRVR_STATE_STARTING:
3489 case SKD_DRVR_STATE_RESTARTING:
3490 case SKD_DRVR_STATE_RESUMING:
3491 default:
3492 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003493 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
3494 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003495 }
3496 return rc;
3497}
3498
3499/* assume spinlock is held */
3500static int skd_unquiesce_dev(struct skd_device *skdev)
3501{
3502 int prev_driver_state = skdev->state;
3503
3504 skd_log_skdev(skdev, "unquiesce");
3505 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003506 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003507 return 0;
3508 }
3509 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3510 /*
3511 * If there has been an state change to other than
3512 * ONLINE, we will rely on controller state change
3513 * to come back online and restart the queue.
3514 * The BUSY state means that driver is ready to
3515 * continue normal processing but waiting for controller
3516 * to become available.
3517 */
3518 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003519 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003520 return 0;
3521 }
3522
3523 /*
3524 * Drive has just come online, driver is either in startup,
3525 * paused performing a task, or bust waiting for hardware.
3526 */
3527 switch (skdev->state) {
3528 case SKD_DRVR_STATE_PAUSED:
3529 case SKD_DRVR_STATE_BUSY:
3530 case SKD_DRVR_STATE_BUSY_IMMINENT:
3531 case SKD_DRVR_STATE_BUSY_ERASE:
3532 case SKD_DRVR_STATE_STARTING:
3533 case SKD_DRVR_STATE_RESTARTING:
3534 case SKD_DRVR_STATE_FAULT:
3535 case SKD_DRVR_STATE_IDLE:
3536 case SKD_DRVR_STATE_LOAD:
3537 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003538 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3539 skd_skdev_state_to_str(prev_driver_state),
3540 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3541 skdev->state);
3542 dev_dbg(&skdev->pdev->dev,
3543 "**** device ONLINE...starting block queue\n");
3544 dev_dbg(&skdev->pdev->dev, "starting queue\n");
3545 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003546 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003547 skdev->gendisk_on = 1;
3548 wake_up_interruptible(&skdev->waitq);
3549 break;
3550
3551 case SKD_DRVR_STATE_DISAPPEARED:
3552 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003553 dev_dbg(&skdev->pdev->dev,
3554 "**** driver state %d, not implemented\n",
3555 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003556 return -EBUSY;
3557 }
3558 return 0;
3559}
3560
3561/*
3562 *****************************************************************************
3563 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3564 *****************************************************************************
3565 */
3566
3567static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3568{
3569 struct skd_device *skdev = skd_host_data;
3570 unsigned long flags;
3571
3572 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003573 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3574 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3575 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
3576 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003577 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3578 spin_unlock_irqrestore(&skdev->lock, flags);
3579 return IRQ_HANDLED;
3580}
3581
3582static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3583{
3584 struct skd_device *skdev = skd_host_data;
3585 unsigned long flags;
3586
3587 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003588 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3589 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003590 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3591 skd_isr_fwstate(skdev);
3592 spin_unlock_irqrestore(&skdev->lock, flags);
3593 return IRQ_HANDLED;
3594}
3595
3596static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3597{
3598 struct skd_device *skdev = skd_host_data;
3599 unsigned long flags;
3600 int flush_enqueued = 0;
3601 int deferred;
3602
3603 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003604 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3605 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003606 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3607 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3608 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003609 if (flush_enqueued)
3610 skd_request_fn(skdev->queue);
3611
3612 if (deferred)
3613 schedule_work(&skdev->completion_worker);
3614 else if (!flush_enqueued)
3615 skd_request_fn(skdev->queue);
3616
3617 spin_unlock_irqrestore(&skdev->lock, flags);
3618
3619 return IRQ_HANDLED;
3620}
3621
3622static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3623{
3624 struct skd_device *skdev = skd_host_data;
3625 unsigned long flags;
3626
3627 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003628 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3629 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003630 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3631 skd_isr_msg_from_dev(skdev);
3632 spin_unlock_irqrestore(&skdev->lock, flags);
3633 return IRQ_HANDLED;
3634}
3635
3636static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3637{
3638 struct skd_device *skdev = skd_host_data;
3639 unsigned long flags;
3640
3641 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003642 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3643 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003644 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3645 spin_unlock_irqrestore(&skdev->lock, flags);
3646 return IRQ_HANDLED;
3647}
3648
3649/*
3650 *****************************************************************************
3651 * PCIe MSI/MSI-X SETUP
3652 *****************************************************************************
3653 */
3654
3655struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003656 char isr_name[30];
3657};
3658
3659struct skd_init_msix_entry {
3660 const char *name;
3661 irq_handler_t handler;
3662};
3663
3664#define SKD_MAX_MSIX_COUNT 13
3665#define SKD_MIN_MSIX_COUNT 7
3666#define SKD_BASE_MSIX_IRQ 4
3667
3668static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3669 { "(DMA 0)", skd_reserved_isr },
3670 { "(DMA 1)", skd_reserved_isr },
3671 { "(DMA 2)", skd_reserved_isr },
3672 { "(DMA 3)", skd_reserved_isr },
3673 { "(State Change)", skd_statec_isr },
3674 { "(COMPL_Q)", skd_comp_q },
3675 { "(MSG)", skd_msg_isr },
3676 { "(Reserved)", skd_reserved_isr },
3677 { "(Reserved)", skd_reserved_isr },
3678 { "(Queue Full 0)", skd_qfull_isr },
3679 { "(Queue Full 1)", skd_qfull_isr },
3680 { "(Queue Full 2)", skd_qfull_isr },
3681 { "(Queue Full 3)", skd_qfull_isr },
3682};
3683
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003684static int skd_acquire_msix(struct skd_device *skdev)
3685{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003686 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003687 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003688
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003689 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3690 PCI_IRQ_MSIX);
3691 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003692 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003693 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003694 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003695
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003696 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3697 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003698 if (!skdev->msix_entries) {
3699 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003700 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003701 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003702 }
3703
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003704 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003705 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3706 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3707
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003708 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3709 "%s%d-msix %s", DRV_NAME, skdev->devno,
3710 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003711
3712 rc = devm_request_irq(&skdev->pdev->dev,
3713 pci_irq_vector(skdev->pdev, i),
3714 msix_entries[i].handler, 0,
3715 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003716 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003717 dev_err(&skdev->pdev->dev,
3718 "Unable to register(%d) MSI-X handler %d: %s\n",
3719 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003720 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003721 }
3722 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003723
Bart Van Asschef98806d2017-08-17 13:12:58 -07003724 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
3725 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003726 return 0;
3727
3728msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003729 while (--i >= 0)
3730 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003731out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003732 kfree(skdev->msix_entries);
3733 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003734 return rc;
3735}
3736
3737static int skd_acquire_irq(struct skd_device *skdev)
3738{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003739 struct pci_dev *pdev = skdev->pdev;
3740 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003741 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003742
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003743 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003744 rc = skd_acquire_msix(skdev);
3745 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003746 return 0;
3747
Bart Van Asschef98806d2017-08-17 13:12:58 -07003748 dev_err(&skdev->pdev->dev,
3749 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003750 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003751
3752 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3753 skdev->devno);
3754
3755 if (skd_isr_type != SKD_IRQ_LEGACY)
3756 irq_flag |= PCI_IRQ_MSI;
3757 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3758 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003759 dev_err(&skdev->pdev->dev,
3760 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003761 return rc;
3762 }
3763
3764 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3765 pdev->msi_enabled ? 0 : IRQF_SHARED,
3766 skdev->isr_name, skdev);
3767 if (rc) {
3768 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003769 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
3770 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003771 return rc;
3772 }
3773
3774 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003775}
3776
3777static void skd_release_irq(struct skd_device *skdev)
3778{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003779 struct pci_dev *pdev = skdev->pdev;
3780
3781 if (skdev->msix_entries) {
3782 int i;
3783
3784 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3785 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3786 skdev);
3787 }
3788
3789 kfree(skdev->msix_entries);
3790 skdev->msix_entries = NULL;
3791 } else {
3792 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003793 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003794
3795 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003796}
3797
3798/*
3799 *****************************************************************************
3800 * CONSTRUCT
3801 *****************************************************************************
3802 */
3803
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003804static int skd_cons_skcomp(struct skd_device *skdev)
3805{
3806 int rc = 0;
3807 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003808
Bart Van Asschef98806d2017-08-17 13:12:58 -07003809 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003810 "comp pci_alloc, total bytes %zd entries %d\n",
3811 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003812
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003813 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07003814 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003815
3816 if (skcomp == NULL) {
3817 rc = -ENOMEM;
3818 goto err_out;
3819 }
3820
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003821 skdev->skcomp_table = skcomp;
3822 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3823 sizeof(*skcomp) *
3824 SKD_N_COMPLETION_ENTRY);
3825
3826err_out:
3827 return rc;
3828}
3829
3830static int skd_cons_skmsg(struct skd_device *skdev)
3831{
3832 int rc = 0;
3833 u32 i;
3834
Bart Van Asschef98806d2017-08-17 13:12:58 -07003835 dev_dbg(&skdev->pdev->dev,
3836 "skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3837 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
3838 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003839
3840 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3841 *skdev->num_fitmsg_context, GFP_KERNEL);
3842 if (skdev->skmsg_table == NULL) {
3843 rc = -ENOMEM;
3844 goto err_out;
3845 }
3846
3847 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3848 struct skd_fitmsg_context *skmsg;
3849
3850 skmsg = &skdev->skmsg_table[i];
3851
3852 skmsg->id = i + SKD_ID_FIT_MSG;
3853
3854 skmsg->state = SKD_MSG_STATE_IDLE;
3855 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
Bart Van Assche6507f432017-08-17 13:13:06 -07003856 SKD_N_FITMSG_BYTES,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003857 &skmsg->mb_dma_address);
3858
3859 if (skmsg->msg_buf == NULL) {
3860 rc = -ENOMEM;
3861 goto err_out;
3862 }
3863
Bart Van Assche6507f432017-08-17 13:13:06 -07003864 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
3865 (FIT_QCMD_ALIGN - 1),
3866 "not aligned: msg_buf %p mb_dma_address %#llx\n",
3867 skmsg->msg_buf, skmsg->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003868 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3869
3870 skmsg->next = &skmsg[1];
3871 }
3872
3873 /* Free list is in order starting with the 0th entry. */
3874 skdev->skmsg_table[i - 1].next = NULL;
3875 skdev->skmsg_free_list = skdev->skmsg_table;
3876
3877err_out:
3878 return rc;
3879}
3880
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003881static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
3882 u32 n_sg,
3883 dma_addr_t *ret_dma_addr)
3884{
3885 struct fit_sg_descriptor *sg_list;
3886 u32 nbytes;
3887
3888 nbytes = sizeof(*sg_list) * n_sg;
3889
3890 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
3891
3892 if (sg_list != NULL) {
3893 uint64_t dma_address = *ret_dma_addr;
3894 u32 i;
3895
3896 memset(sg_list, 0, nbytes);
3897
3898 for (i = 0; i < n_sg - 1; i++) {
3899 uint64_t ndp_off;
3900 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
3901
3902 sg_list[i].next_desc_ptr = dma_address + ndp_off;
3903 }
3904 sg_list[i].next_desc_ptr = 0LL;
3905 }
3906
3907 return sg_list;
3908}
3909
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003910static int skd_cons_skreq(struct skd_device *skdev)
3911{
3912 int rc = 0;
3913 u32 i;
3914
Bart Van Asschef98806d2017-08-17 13:12:58 -07003915 dev_dbg(&skdev->pdev->dev,
3916 "skreq_table kzalloc, struct %lu, count %u total %lu\n",
3917 sizeof(struct skd_request_context), skdev->num_req_context,
3918 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003919
3920 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
3921 * skdev->num_req_context, GFP_KERNEL);
3922 if (skdev->skreq_table == NULL) {
3923 rc = -ENOMEM;
3924 goto err_out;
3925 }
3926
Bart Van Asschef98806d2017-08-17 13:12:58 -07003927 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
3928 skdev->sgs_per_request, sizeof(struct scatterlist),
3929 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003930
3931 for (i = 0; i < skdev->num_req_context; i++) {
3932 struct skd_request_context *skreq;
3933
3934 skreq = &skdev->skreq_table[i];
3935
3936 skreq->id = i + SKD_ID_RW_REQUEST;
3937 skreq->state = SKD_REQ_STATE_IDLE;
3938
3939 skreq->sg = kzalloc(sizeof(struct scatterlist) *
3940 skdev->sgs_per_request, GFP_KERNEL);
3941 if (skreq->sg == NULL) {
3942 rc = -ENOMEM;
3943 goto err_out;
3944 }
3945 sg_init_table(skreq->sg, skdev->sgs_per_request);
3946
3947 skreq->sksg_list = skd_cons_sg_list(skdev,
3948 skdev->sgs_per_request,
3949 &skreq->sksg_dma_address);
3950
3951 if (skreq->sksg_list == NULL) {
3952 rc = -ENOMEM;
3953 goto err_out;
3954 }
3955
3956 skreq->next = &skreq[1];
3957 }
3958
3959 /* Free list is in order starting with the 0th entry. */
3960 skdev->skreq_table[i - 1].next = NULL;
3961 skdev->skreq_free_list = skdev->skreq_table;
3962
3963err_out:
3964 return rc;
3965}
3966
3967static int skd_cons_skspcl(struct skd_device *skdev)
3968{
3969 int rc = 0;
3970 u32 i, nbytes;
3971
Bart Van Asschef98806d2017-08-17 13:12:58 -07003972 dev_dbg(&skdev->pdev->dev,
3973 "skspcl_table kzalloc, struct %lu, count %u total %lu\n",
3974 sizeof(struct skd_special_context), skdev->n_special,
3975 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003976
3977 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
3978 * skdev->n_special, GFP_KERNEL);
3979 if (skdev->skspcl_table == NULL) {
3980 rc = -ENOMEM;
3981 goto err_out;
3982 }
3983
3984 for (i = 0; i < skdev->n_special; i++) {
3985 struct skd_special_context *skspcl;
3986
3987 skspcl = &skdev->skspcl_table[i];
3988
3989 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
3990 skspcl->req.state = SKD_REQ_STATE_IDLE;
3991
3992 skspcl->req.next = &skspcl[1].req;
3993
3994 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3995
Joe Perchesa5bbf612014-08-08 14:24:12 -07003996 skspcl->msg_buf =
3997 pci_zalloc_consistent(skdev->pdev, nbytes,
3998 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003999 if (skspcl->msg_buf == NULL) {
4000 rc = -ENOMEM;
4001 goto err_out;
4002 }
4003
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004004 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4005 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4006 if (skspcl->req.sg == NULL) {
4007 rc = -ENOMEM;
4008 goto err_out;
4009 }
4010
4011 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4012 SKD_N_SG_PER_SPECIAL,
4013 &skspcl->req.
4014 sksg_dma_address);
4015 if (skspcl->req.sksg_list == NULL) {
4016 rc = -ENOMEM;
4017 goto err_out;
4018 }
4019 }
4020
4021 /* Free list is in order starting with the 0th entry. */
4022 skdev->skspcl_table[i - 1].req.next = NULL;
4023 skdev->skspcl_free_list = skdev->skspcl_table;
4024
4025 return rc;
4026
4027err_out:
4028 return rc;
4029}
4030
4031static int skd_cons_sksb(struct skd_device *skdev)
4032{
4033 int rc = 0;
4034 struct skd_special_context *skspcl;
4035 u32 nbytes;
4036
4037 skspcl = &skdev->internal_skspcl;
4038
4039 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4040 skspcl->req.state = SKD_REQ_STATE_IDLE;
4041
4042 nbytes = SKD_N_INTERNAL_BYTES;
4043
Joe Perchesa5bbf612014-08-08 14:24:12 -07004044 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4045 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004046 if (skspcl->data_buf == NULL) {
4047 rc = -ENOMEM;
4048 goto err_out;
4049 }
4050
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004051 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004052 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4053 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004054 if (skspcl->msg_buf == NULL) {
4055 rc = -ENOMEM;
4056 goto err_out;
4057 }
4058
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004059 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4060 &skspcl->req.sksg_dma_address);
4061 if (skspcl->req.sksg_list == NULL) {
4062 rc = -ENOMEM;
4063 goto err_out;
4064 }
4065
4066 if (!skd_format_internal_skspcl(skdev)) {
4067 rc = -EINVAL;
4068 goto err_out;
4069 }
4070
4071err_out:
4072 return rc;
4073}
4074
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004075static int skd_cons_disk(struct skd_device *skdev)
4076{
4077 int rc = 0;
4078 struct gendisk *disk;
4079 struct request_queue *q;
4080 unsigned long flags;
4081
4082 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4083 if (!disk) {
4084 rc = -ENOMEM;
4085 goto err_out;
4086 }
4087
4088 skdev->disk = disk;
4089 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4090
4091 disk->major = skdev->major;
4092 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4093 disk->fops = &skd_blockdev_ops;
4094 disk->private_data = skdev;
4095
Jens Axboefcd37eb2013-11-01 10:14:56 -06004096 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004097 if (!q) {
4098 rc = -ENOMEM;
4099 goto err_out;
4100 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004101 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004102
4103 skdev->queue = q;
4104 disk->queue = q;
4105 q->queuedata = skdev;
4106
Jens Axboe6975f732016-03-30 10:11:42 -06004107 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004108 blk_queue_max_segments(q, skdev->sgs_per_request);
4109 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4110
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004111 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004112 blk_queue_io_opt(q, 8192);
4113
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004114 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004115 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004116
4117 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004118 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06004119 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004120 spin_unlock_irqrestore(&skdev->lock, flags);
4121
4122err_out:
4123 return rc;
4124}
4125
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004126#define SKD_N_DEV_TABLE 16u
4127static u32 skd_next_devno;
4128
4129static struct skd_device *skd_construct(struct pci_dev *pdev)
4130{
4131 struct skd_device *skdev;
4132 int blk_major = skd_major;
4133 int rc;
4134
4135 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4136
4137 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004138 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004139 return NULL;
4140 }
4141
4142 skdev->state = SKD_DRVR_STATE_LOAD;
4143 skdev->pdev = pdev;
4144 skdev->devno = skd_next_devno++;
4145 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004146 skdev->dev_max_queue_depth = 0;
4147
4148 skdev->num_req_context = skd_max_queue_depth;
4149 skdev->num_fitmsg_context = skd_max_queue_depth;
4150 skdev->n_special = skd_max_pass_thru;
4151 skdev->cur_max_queue_depth = 1;
4152 skdev->queue_low_water_mark = 1;
4153 skdev->proto_ver = 99;
4154 skdev->sgs_per_request = skd_sgs_per_request;
4155 skdev->dbg_level = skd_dbg_level;
4156
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004157 spin_lock_init(&skdev->lock);
4158
4159 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4160
Bart Van Asschef98806d2017-08-17 13:12:58 -07004161 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004162 rc = skd_cons_skcomp(skdev);
4163 if (rc < 0)
4164 goto err_out;
4165
Bart Van Asschef98806d2017-08-17 13:12:58 -07004166 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004167 rc = skd_cons_skmsg(skdev);
4168 if (rc < 0)
4169 goto err_out;
4170
Bart Van Asschef98806d2017-08-17 13:12:58 -07004171 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004172 rc = skd_cons_skreq(skdev);
4173 if (rc < 0)
4174 goto err_out;
4175
Bart Van Asschef98806d2017-08-17 13:12:58 -07004176 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004177 rc = skd_cons_skspcl(skdev);
4178 if (rc < 0)
4179 goto err_out;
4180
Bart Van Asschef98806d2017-08-17 13:12:58 -07004181 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004182 rc = skd_cons_sksb(skdev);
4183 if (rc < 0)
4184 goto err_out;
4185
Bart Van Asschef98806d2017-08-17 13:12:58 -07004186 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004187 rc = skd_cons_disk(skdev);
4188 if (rc < 0)
4189 goto err_out;
4190
Bart Van Asschef98806d2017-08-17 13:12:58 -07004191 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004192 return skdev;
4193
4194err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07004195 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004196 skd_destruct(skdev);
4197 return NULL;
4198}
4199
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004200/*
4201 *****************************************************************************
4202 * DESTRUCT (FREE)
4203 *****************************************************************************
4204 */
4205
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004206static void skd_free_skcomp(struct skd_device *skdev)
4207{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07004208 if (skdev->skcomp_table)
4209 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004210 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004211
4212 skdev->skcomp_table = NULL;
4213 skdev->cq_dma_address = 0;
4214}
4215
4216static void skd_free_skmsg(struct skd_device *skdev)
4217{
4218 u32 i;
4219
4220 if (skdev->skmsg_table == NULL)
4221 return;
4222
4223 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4224 struct skd_fitmsg_context *skmsg;
4225
4226 skmsg = &skdev->skmsg_table[i];
4227
4228 if (skmsg->msg_buf != NULL) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004229 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4230 skmsg->msg_buf,
4231 skmsg->mb_dma_address);
4232 }
4233 skmsg->msg_buf = NULL;
4234 skmsg->mb_dma_address = 0;
4235 }
4236
4237 kfree(skdev->skmsg_table);
4238 skdev->skmsg_table = NULL;
4239}
4240
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004241static void skd_free_sg_list(struct skd_device *skdev,
4242 struct fit_sg_descriptor *sg_list,
4243 u32 n_sg, dma_addr_t dma_addr)
4244{
4245 if (sg_list != NULL) {
4246 u32 nbytes;
4247
4248 nbytes = sizeof(*sg_list) * n_sg;
4249
4250 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4251 }
4252}
4253
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004254static void skd_free_skreq(struct skd_device *skdev)
4255{
4256 u32 i;
4257
4258 if (skdev->skreq_table == NULL)
4259 return;
4260
4261 for (i = 0; i < skdev->num_req_context; i++) {
4262 struct skd_request_context *skreq;
4263
4264 skreq = &skdev->skreq_table[i];
4265
4266 skd_free_sg_list(skdev, skreq->sksg_list,
4267 skdev->sgs_per_request,
4268 skreq->sksg_dma_address);
4269
4270 skreq->sksg_list = NULL;
4271 skreq->sksg_dma_address = 0;
4272
4273 kfree(skreq->sg);
4274 }
4275
4276 kfree(skdev->skreq_table);
4277 skdev->skreq_table = NULL;
4278}
4279
4280static void skd_free_skspcl(struct skd_device *skdev)
4281{
4282 u32 i;
4283 u32 nbytes;
4284
4285 if (skdev->skspcl_table == NULL)
4286 return;
4287
4288 for (i = 0; i < skdev->n_special; i++) {
4289 struct skd_special_context *skspcl;
4290
4291 skspcl = &skdev->skspcl_table[i];
4292
4293 if (skspcl->msg_buf != NULL) {
4294 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4295 pci_free_consistent(skdev->pdev, nbytes,
4296 skspcl->msg_buf,
4297 skspcl->mb_dma_address);
4298 }
4299
4300 skspcl->msg_buf = NULL;
4301 skspcl->mb_dma_address = 0;
4302
4303 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4304 SKD_N_SG_PER_SPECIAL,
4305 skspcl->req.sksg_dma_address);
4306
4307 skspcl->req.sksg_list = NULL;
4308 skspcl->req.sksg_dma_address = 0;
4309
4310 kfree(skspcl->req.sg);
4311 }
4312
4313 kfree(skdev->skspcl_table);
4314 skdev->skspcl_table = NULL;
4315}
4316
4317static void skd_free_sksb(struct skd_device *skdev)
4318{
4319 struct skd_special_context *skspcl;
4320 u32 nbytes;
4321
4322 skspcl = &skdev->internal_skspcl;
4323
4324 if (skspcl->data_buf != NULL) {
4325 nbytes = SKD_N_INTERNAL_BYTES;
4326
4327 pci_free_consistent(skdev->pdev, nbytes,
4328 skspcl->data_buf, skspcl->db_dma_address);
4329 }
4330
4331 skspcl->data_buf = NULL;
4332 skspcl->db_dma_address = 0;
4333
4334 if (skspcl->msg_buf != NULL) {
4335 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4336 pci_free_consistent(skdev->pdev, nbytes,
4337 skspcl->msg_buf, skspcl->mb_dma_address);
4338 }
4339
4340 skspcl->msg_buf = NULL;
4341 skspcl->mb_dma_address = 0;
4342
4343 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4344 skspcl->req.sksg_dma_address);
4345
4346 skspcl->req.sksg_list = NULL;
4347 skspcl->req.sksg_dma_address = 0;
4348}
4349
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004350static void skd_free_disk(struct skd_device *skdev)
4351{
4352 struct gendisk *disk = skdev->disk;
4353
Bart Van Assche7277cc62017-08-17 13:12:45 -07004354 if (disk && (disk->flags & GENHD_FL_UP))
4355 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004356
Bart Van Assche7277cc62017-08-17 13:12:45 -07004357 if (skdev->queue) {
4358 blk_cleanup_queue(skdev->queue);
4359 skdev->queue = NULL;
4360 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004361 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004362
4363 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004364 skdev->disk = NULL;
4365}
4366
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004367static void skd_destruct(struct skd_device *skdev)
4368{
4369 if (skdev == NULL)
4370 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004371
Bart Van Asschef98806d2017-08-17 13:12:58 -07004372 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004373 skd_free_disk(skdev);
4374
Bart Van Asschef98806d2017-08-17 13:12:58 -07004375 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004376 skd_free_sksb(skdev);
4377
Bart Van Asschef98806d2017-08-17 13:12:58 -07004378 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004379 skd_free_skspcl(skdev);
4380
Bart Van Asschef98806d2017-08-17 13:12:58 -07004381 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004382 skd_free_skreq(skdev);
4383
Bart Van Asschef98806d2017-08-17 13:12:58 -07004384 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004385 skd_free_skmsg(skdev);
4386
Bart Van Asschef98806d2017-08-17 13:12:58 -07004387 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004388 skd_free_skcomp(skdev);
4389
Bart Van Asschef98806d2017-08-17 13:12:58 -07004390 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004391 kfree(skdev);
4392}
4393
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004394/*
4395 *****************************************************************************
4396 * BLOCK DEVICE (BDEV) GLUE
4397 *****************************************************************************
4398 */
4399
4400static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4401{
4402 struct skd_device *skdev;
4403 u64 capacity;
4404
4405 skdev = bdev->bd_disk->private_data;
4406
Bart Van Asschef98806d2017-08-17 13:12:58 -07004407 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
4408 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004409
4410 if (skdev->read_cap_is_valid) {
4411 capacity = get_capacity(skdev->disk);
4412 geo->heads = 64;
4413 geo->sectors = 255;
4414 geo->cylinders = (capacity) / (255 * 64);
4415
4416 return 0;
4417 }
4418 return -EIO;
4419}
4420
Dan Williams0d52c7562016-06-15 19:44:20 -07004421static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004422{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004423 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07004424 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004425 return 0;
4426}
4427
4428static const struct block_device_operations skd_blockdev_ops = {
4429 .owner = THIS_MODULE,
4430 .ioctl = skd_bdev_ioctl,
4431 .getgeo = skd_bdev_getgeo,
4432};
4433
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004434/*
4435 *****************************************************************************
4436 * PCIe DRIVER GLUE
4437 *****************************************************************************
4438 */
4439
Benoit Taine9baa3c32014-08-08 15:56:03 +02004440static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004441 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4442 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4443 { 0 } /* terminate list */
4444};
4445
4446MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4447
4448static char *skd_pci_info(struct skd_device *skdev, char *str)
4449{
4450 int pcie_reg;
4451
4452 strcpy(str, "PCIe (");
4453 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4454
4455 if (pcie_reg) {
4456
4457 char lwstr[6];
4458 uint16_t pcie_lstat, lspeed, lwidth;
4459
4460 pcie_reg += 0x12;
4461 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4462 lspeed = pcie_lstat & (0xF);
4463 lwidth = (pcie_lstat & 0x3F0) >> 4;
4464
4465 if (lspeed == 1)
4466 strcat(str, "2.5GT/s ");
4467 else if (lspeed == 2)
4468 strcat(str, "5.0GT/s ");
4469 else
4470 strcat(str, "<unknown> ");
4471 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4472 strcat(str, lwstr);
4473 }
4474 return str;
4475}
4476
4477static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4478{
4479 int i;
4480 int rc = 0;
4481 char pci_str[32];
4482 struct skd_device *skdev;
4483
Bart Van Asschef98806d2017-08-17 13:12:58 -07004484 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
4485 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4486 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
4487 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004488
4489 rc = pci_enable_device(pdev);
4490 if (rc)
4491 return rc;
4492 rc = pci_request_regions(pdev, DRV_NAME);
4493 if (rc)
4494 goto err_out;
4495 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4496 if (!rc) {
4497 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004498 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4499 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004500 }
4501 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004502 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004503 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004504 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004505 goto err_out_regions;
4506 }
4507 }
4508
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004509 if (!skd_major) {
4510 rc = register_blkdev(0, DRV_NAME);
4511 if (rc < 0)
4512 goto err_out_regions;
4513 BUG_ON(!rc);
4514 skd_major = rc;
4515 }
4516
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004517 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004518 if (skdev == NULL) {
4519 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004520 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004521 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004522
4523 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004524 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004525
4526 pci_set_master(pdev);
4527 rc = pci_enable_pcie_error_reporting(pdev);
4528 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004529 dev_err(&pdev->dev,
4530 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004531 skdev->pcie_error_reporting_is_enabled = 0;
4532 } else
4533 skdev->pcie_error_reporting_is_enabled = 1;
4534
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004535 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004536
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004537 for (i = 0; i < SKD_MAX_BARS; i++) {
4538 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4539 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4540 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4541 skdev->mem_size[i]);
4542 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004543 dev_err(&pdev->dev,
4544 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004545 rc = -ENODEV;
4546 goto err_out_iounmap;
4547 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004548 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4549 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4550 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004551 }
4552
4553 rc = skd_acquire_irq(skdev);
4554 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004555 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004556 goto err_out_iounmap;
4557 }
4558
4559 rc = skd_start_timer(skdev);
4560 if (rc)
4561 goto err_out_timer;
4562
4563 init_waitqueue_head(&skdev->waitq);
4564
4565 skd_start_device(skdev);
4566
4567 rc = wait_event_interruptible_timeout(skdev->waitq,
4568 (skdev->gendisk_on),
4569 (SKD_START_WAIT_SECONDS * HZ));
4570 if (skdev->gendisk_on > 0) {
4571 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004572 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004573 rc = 0;
4574 } else {
4575 /* we timed out, something is wrong with the device,
4576 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07004577 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
4578 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004579 /* in case of no error; we timeout with ENXIO */
4580 if (!rc)
4581 rc = -ENXIO;
4582 goto err_out_timer;
4583 }
4584
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004585 return rc;
4586
4587err_out_timer:
4588 skd_stop_device(skdev);
4589 skd_release_irq(skdev);
4590
4591err_out_iounmap:
4592 for (i = 0; i < SKD_MAX_BARS; i++)
4593 if (skdev->mem_map[i])
4594 iounmap(skdev->mem_map[i]);
4595
4596 if (skdev->pcie_error_reporting_is_enabled)
4597 pci_disable_pcie_error_reporting(pdev);
4598
4599 skd_destruct(skdev);
4600
4601err_out_regions:
4602 pci_release_regions(pdev);
4603
4604err_out:
4605 pci_disable_device(pdev);
4606 pci_set_drvdata(pdev, NULL);
4607 return rc;
4608}
4609
4610static void skd_pci_remove(struct pci_dev *pdev)
4611{
4612 int i;
4613 struct skd_device *skdev;
4614
4615 skdev = pci_get_drvdata(pdev);
4616 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004617 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004618 return;
4619 }
4620 skd_stop_device(skdev);
4621 skd_release_irq(skdev);
4622
4623 for (i = 0; i < SKD_MAX_BARS; i++)
4624 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004625 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004626
4627 if (skdev->pcie_error_reporting_is_enabled)
4628 pci_disable_pcie_error_reporting(pdev);
4629
4630 skd_destruct(skdev);
4631
4632 pci_release_regions(pdev);
4633 pci_disable_device(pdev);
4634 pci_set_drvdata(pdev, NULL);
4635
4636 return;
4637}
4638
4639static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4640{
4641 int i;
4642 struct skd_device *skdev;
4643
4644 skdev = pci_get_drvdata(pdev);
4645 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004646 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004647 return -EIO;
4648 }
4649
4650 skd_stop_device(skdev);
4651
4652 skd_release_irq(skdev);
4653
4654 for (i = 0; i < SKD_MAX_BARS; i++)
4655 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004656 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004657
4658 if (skdev->pcie_error_reporting_is_enabled)
4659 pci_disable_pcie_error_reporting(pdev);
4660
4661 pci_release_regions(pdev);
4662 pci_save_state(pdev);
4663 pci_disable_device(pdev);
4664 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4665 return 0;
4666}
4667
4668static int skd_pci_resume(struct pci_dev *pdev)
4669{
4670 int i;
4671 int rc = 0;
4672 struct skd_device *skdev;
4673
4674 skdev = pci_get_drvdata(pdev);
4675 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004676 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004677 return -1;
4678 }
4679
4680 pci_set_power_state(pdev, PCI_D0);
4681 pci_enable_wake(pdev, PCI_D0, 0);
4682 pci_restore_state(pdev);
4683
4684 rc = pci_enable_device(pdev);
4685 if (rc)
4686 return rc;
4687 rc = pci_request_regions(pdev, DRV_NAME);
4688 if (rc)
4689 goto err_out;
4690 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4691 if (!rc) {
4692 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4693
Bart Van Asschef98806d2017-08-17 13:12:58 -07004694 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4695 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004696 }
4697 } else {
4698 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4699 if (rc) {
4700
Bart Van Asschef98806d2017-08-17 13:12:58 -07004701 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004702 goto err_out_regions;
4703 }
4704 }
4705
4706 pci_set_master(pdev);
4707 rc = pci_enable_pcie_error_reporting(pdev);
4708 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004709 dev_err(&pdev->dev,
4710 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004711 skdev->pcie_error_reporting_is_enabled = 0;
4712 } else
4713 skdev->pcie_error_reporting_is_enabled = 1;
4714
4715 for (i = 0; i < SKD_MAX_BARS; i++) {
4716
4717 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4718 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4719 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4720 skdev->mem_size[i]);
4721 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004722 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004723 rc = -ENODEV;
4724 goto err_out_iounmap;
4725 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004726 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4727 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4728 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004729 }
4730 rc = skd_acquire_irq(skdev);
4731 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004732 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004733 goto err_out_iounmap;
4734 }
4735
4736 rc = skd_start_timer(skdev);
4737 if (rc)
4738 goto err_out_timer;
4739
4740 init_waitqueue_head(&skdev->waitq);
4741
4742 skd_start_device(skdev);
4743
4744 return rc;
4745
4746err_out_timer:
4747 skd_stop_device(skdev);
4748 skd_release_irq(skdev);
4749
4750err_out_iounmap:
4751 for (i = 0; i < SKD_MAX_BARS; i++)
4752 if (skdev->mem_map[i])
4753 iounmap(skdev->mem_map[i]);
4754
4755 if (skdev->pcie_error_reporting_is_enabled)
4756 pci_disable_pcie_error_reporting(pdev);
4757
4758err_out_regions:
4759 pci_release_regions(pdev);
4760
4761err_out:
4762 pci_disable_device(pdev);
4763 return rc;
4764}
4765
4766static void skd_pci_shutdown(struct pci_dev *pdev)
4767{
4768 struct skd_device *skdev;
4769
Bart Van Asschef98806d2017-08-17 13:12:58 -07004770 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004771
4772 skdev = pci_get_drvdata(pdev);
4773 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004774 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004775 return;
4776 }
4777
Bart Van Asschef98806d2017-08-17 13:12:58 -07004778 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004779 skd_stop_device(skdev);
4780}
4781
4782static struct pci_driver skd_driver = {
4783 .name = DRV_NAME,
4784 .id_table = skd_pci_tbl,
4785 .probe = skd_pci_probe,
4786 .remove = skd_pci_remove,
4787 .suspend = skd_pci_suspend,
4788 .resume = skd_pci_resume,
4789 .shutdown = skd_pci_shutdown,
4790};
4791
4792/*
4793 *****************************************************************************
4794 * LOGGING SUPPORT
4795 *****************************************************************************
4796 */
4797
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004798const char *skd_drive_state_to_str(int state)
4799{
4800 switch (state) {
4801 case FIT_SR_DRIVE_OFFLINE:
4802 return "OFFLINE";
4803 case FIT_SR_DRIVE_INIT:
4804 return "INIT";
4805 case FIT_SR_DRIVE_ONLINE:
4806 return "ONLINE";
4807 case FIT_SR_DRIVE_BUSY:
4808 return "BUSY";
4809 case FIT_SR_DRIVE_FAULT:
4810 return "FAULT";
4811 case FIT_SR_DRIVE_DEGRADED:
4812 return "DEGRADED";
4813 case FIT_SR_PCIE_LINK_DOWN:
4814 return "INK_DOWN";
4815 case FIT_SR_DRIVE_SOFT_RESET:
4816 return "SOFT_RESET";
4817 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4818 return "NEED_FW";
4819 case FIT_SR_DRIVE_INIT_FAULT:
4820 return "INIT_FAULT";
4821 case FIT_SR_DRIVE_BUSY_SANITIZE:
4822 return "BUSY_SANITIZE";
4823 case FIT_SR_DRIVE_BUSY_ERASE:
4824 return "BUSY_ERASE";
4825 case FIT_SR_DRIVE_FW_BOOTING:
4826 return "FW_BOOTING";
4827 default:
4828 return "???";
4829 }
4830}
4831
4832const char *skd_skdev_state_to_str(enum skd_drvr_state state)
4833{
4834 switch (state) {
4835 case SKD_DRVR_STATE_LOAD:
4836 return "LOAD";
4837 case SKD_DRVR_STATE_IDLE:
4838 return "IDLE";
4839 case SKD_DRVR_STATE_BUSY:
4840 return "BUSY";
4841 case SKD_DRVR_STATE_STARTING:
4842 return "STARTING";
4843 case SKD_DRVR_STATE_ONLINE:
4844 return "ONLINE";
4845 case SKD_DRVR_STATE_PAUSING:
4846 return "PAUSING";
4847 case SKD_DRVR_STATE_PAUSED:
4848 return "PAUSED";
4849 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
4850 return "DRAINING_TIMEOUT";
4851 case SKD_DRVR_STATE_RESTARTING:
4852 return "RESTARTING";
4853 case SKD_DRVR_STATE_RESUMING:
4854 return "RESUMING";
4855 case SKD_DRVR_STATE_STOPPING:
4856 return "STOPPING";
4857 case SKD_DRVR_STATE_SYNCING:
4858 return "SYNCING";
4859 case SKD_DRVR_STATE_FAULT:
4860 return "FAULT";
4861 case SKD_DRVR_STATE_DISAPPEARED:
4862 return "DISAPPEARED";
4863 case SKD_DRVR_STATE_BUSY_ERASE:
4864 return "BUSY_ERASE";
4865 case SKD_DRVR_STATE_BUSY_SANITIZE:
4866 return "BUSY_SANITIZE";
4867 case SKD_DRVR_STATE_BUSY_IMMINENT:
4868 return "BUSY_IMMINENT";
4869 case SKD_DRVR_STATE_WAIT_BOOT:
4870 return "WAIT_BOOT";
4871
4872 default:
4873 return "???";
4874 }
4875}
4876
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304877static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004878{
4879 switch (state) {
4880 case SKD_MSG_STATE_IDLE:
4881 return "IDLE";
4882 case SKD_MSG_STATE_BUSY:
4883 return "BUSY";
4884 default:
4885 return "???";
4886 }
4887}
4888
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304889static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004890{
4891 switch (state) {
4892 case SKD_REQ_STATE_IDLE:
4893 return "IDLE";
4894 case SKD_REQ_STATE_SETUP:
4895 return "SETUP";
4896 case SKD_REQ_STATE_BUSY:
4897 return "BUSY";
4898 case SKD_REQ_STATE_COMPLETED:
4899 return "COMPLETED";
4900 case SKD_REQ_STATE_TIMEOUT:
4901 return "TIMEOUT";
4902 case SKD_REQ_STATE_ABORTED:
4903 return "ABORTED";
4904 default:
4905 return "???";
4906 }
4907}
4908
4909static void skd_log_skdev(struct skd_device *skdev, const char *event)
4910{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004911 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
4912 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
4913 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
4914 skd_skdev_state_to_str(skdev->state), skdev->state);
4915 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
4916 skdev->in_flight, skdev->cur_max_queue_depth,
4917 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
4918 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
4919 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004920}
4921
4922static void skd_log_skmsg(struct skd_device *skdev,
4923 struct skd_fitmsg_context *skmsg, const char *event)
4924{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004925 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
4926 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
4927 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
4928 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004929}
4930
4931static void skd_log_skreq(struct skd_device *skdev,
4932 struct skd_request_context *skreq, const char *event)
4933{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004934 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
4935 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
4936 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
4937 skreq->fitmsg_id);
4938 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
4939 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004940
Jens Axboefcd37eb2013-11-01 10:14:56 -06004941 if (skreq->req != NULL) {
4942 struct request *req = skreq->req;
4943 u32 lba = (u32)blk_rq_pos(req);
4944 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004945
Bart Van Asschef98806d2017-08-17 13:12:58 -07004946 dev_dbg(&skdev->pdev->dev,
4947 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
4948 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06004949 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07004950 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004951}
4952
4953/*
4954 *****************************************************************************
4955 * MODULE GLUE
4956 *****************************************************************************
4957 */
4958
4959static int __init skd_init(void)
4960{
Bart Van Assche16a70532017-08-17 13:13:08 -07004961 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
4962 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
4963 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
4964 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
4965 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
Bart Van Assched891fe62017-08-17 13:13:07 -07004966 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
4967 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
4968 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
Bart Van Assche2da7b402017-08-17 13:13:01 -07004969
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004970 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
4971
4972 switch (skd_isr_type) {
4973 case SKD_IRQ_LEGACY:
4974 case SKD_IRQ_MSI:
4975 case SKD_IRQ_MSIX:
4976 break;
4977 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004978 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004979 skd_isr_type, SKD_IRQ_DEFAULT);
4980 skd_isr_type = SKD_IRQ_DEFAULT;
4981 }
4982
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004983 if (skd_max_queue_depth < 1 ||
4984 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
4985 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004986 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
4987 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
4988 }
4989
Bart Van Assche2da7b402017-08-17 13:13:01 -07004990 if (skd_max_req_per_msg < 1 ||
4991 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004992 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004993 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
4994 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
4995 }
4996
4997 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01004998 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004999 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
5000 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5001 }
5002
5003 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005004 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005005 skd_dbg_level, 0);
5006 skd_dbg_level = 0;
5007 }
5008
5009 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005010 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005011 skd_isr_comp_limit, 0);
5012 skd_isr_comp_limit = 0;
5013 }
5014
5015 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005016 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005017 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5018 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5019 }
5020
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005021 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005022}
5023
5024static void __exit skd_exit(void)
5025{
5026 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5027
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005028 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005029
5030 if (skd_major)
5031 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005032}
5033
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005034module_init(skd_init);
5035module_exit(skd_exit);