blob: 3fc6ec9477c7c80576297a98c20c43792091bf47 [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
184struct skd_fitmsg_context {
185 enum skd_fit_msg_state state;
186
187 struct skd_fitmsg_context *next;
188
189 u32 id;
190 u16 outstanding;
191
192 u32 length;
193 u32 offset;
194
195 u8 *msg_buf;
196 dma_addr_t mb_dma_address;
197};
198
199struct skd_request_context {
200 enum skd_req_state state;
201
202 struct skd_request_context *next;
203
204 u16 id;
205 u32 fitmsg_id;
206
207 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600208 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600209
210 u32 timeout_stamp;
211 u8 sg_data_dir;
212 struct scatterlist *sg;
213 u32 n_sg;
214 u32 sg_byte_count;
215
216 struct fit_sg_descriptor *sksg_list;
217 dma_addr_t sksg_dma_address;
218
219 struct fit_completion_entry_v1 completion;
220
221 struct fit_comp_error_info err_info;
222
223};
224#define SKD_DATA_DIR_HOST_TO_CARD 1
225#define SKD_DATA_DIR_CARD_TO_HOST 2
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600226
227struct skd_special_context {
228 struct skd_request_context req;
229
230 u8 orphaned;
231
232 void *data_buf;
233 dma_addr_t db_dma_address;
234
235 u8 *msg_buf;
236 dma_addr_t mb_dma_address;
237};
238
239struct skd_sg_io {
240 fmode_t mode;
241 void __user *argp;
242
243 struct sg_io_hdr sg;
244
245 u8 cdb[16];
246
247 u32 dxfer_len;
248 u32 iovcnt;
249 struct sg_iovec *iov;
250 struct sg_iovec no_iov_iov;
251
252 struct skd_special_context *skspcl;
253};
254
255typedef enum skd_irq_type {
256 SKD_IRQ_LEGACY,
257 SKD_IRQ_MSI,
258 SKD_IRQ_MSIX
259} skd_irq_type_t;
260
261#define SKD_MAX_BARS 2
262
263struct skd_device {
264 volatile void __iomem *mem_map[SKD_MAX_BARS];
265 resource_size_t mem_phys[SKD_MAX_BARS];
266 u32 mem_size[SKD_MAX_BARS];
267
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600268 struct skd_msix_entry *msix_entries;
269
270 struct pci_dev *pdev;
271 int pcie_error_reporting_is_enabled;
272
273 spinlock_t lock;
274 struct gendisk *disk;
275 struct request_queue *queue;
276 struct device *class_dev;
277 int gendisk_on;
278 int sync_done;
279
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600280 u32 devno;
281 u32 major;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600282 char isr_name[30];
283
284 enum skd_drvr_state state;
285 u32 drive_state;
286
287 u32 in_flight;
288 u32 cur_max_queue_depth;
289 u32 queue_low_water_mark;
290 u32 dev_max_queue_depth;
291
292 u32 num_fitmsg_context;
293 u32 num_req_context;
294
295 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
296 u32 timeout_stamp;
297 struct skd_fitmsg_context *skmsg_free_list;
298 struct skd_fitmsg_context *skmsg_table;
299
300 struct skd_request_context *skreq_free_list;
301 struct skd_request_context *skreq_table;
302
303 struct skd_special_context *skspcl_free_list;
304 struct skd_special_context *skspcl_table;
305
306 struct skd_special_context internal_skspcl;
307 u32 read_cap_blocksize;
308 u32 read_cap_last_lba;
309 int read_cap_is_valid;
310 int inquiry_is_valid;
311 u8 inq_serial_num[13]; /*12 chars plus null term */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600312
313 u8 skcomp_cycle;
314 u32 skcomp_ix;
315 struct fit_completion_entry_v1 *skcomp_table;
316 struct fit_comp_error_info *skerr_table;
317 dma_addr_t cq_dma_address;
318
319 wait_queue_head_t waitq;
320
321 struct timer_list timer;
322 u32 timer_countdown;
323 u32 timer_substate;
324
325 int n_special;
326 int sgs_per_request;
327 u32 last_mtd;
328
329 u32 proto_ver;
330
331 int dbg_level;
332 u32 connect_time_stamp;
333 int connect_retries;
334#define SKD_MAX_CONNECT_RETRIES 16
335 u32 drive_jiffies;
336
337 u32 timo_slot;
338
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600339 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600340};
341
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600342#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
343#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
344#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
345
346static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
347{
Bart Van Assche14262a42017-08-17 13:12:57 -0700348 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600349
Bart Van Assche14262a42017-08-17 13:12:57 -0700350 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700351 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Bart Van Assche14262a42017-08-17 13:12:57 -0700352 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600353}
354
355static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
356 u32 offset)
357{
Bart Van Assche14262a42017-08-17 13:12:57 -0700358 writel(val, skdev->mem_map[1] + offset);
359 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700360 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600361}
362
363static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
364 u32 offset)
365{
Bart Van Assche14262a42017-08-17 13:12:57 -0700366 writeq(val, skdev->mem_map[1] + offset);
367 if (unlikely(skdev->dbg_level >= 2))
Bart Van Asschef98806d2017-08-17 13:12:58 -0700368 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
369 val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600370}
371
372
373#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
374static int skd_isr_type = SKD_IRQ_DEFAULT;
375
376module_param(skd_isr_type, int, 0444);
377MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
378 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
379
380#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
381static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
382
383module_param(skd_max_req_per_msg, int, 0444);
384MODULE_PARM_DESC(skd_max_req_per_msg,
385 "Maximum SCSI requests packed in a single message."
Bart Van Assche2da7b402017-08-17 13:13:01 -0700386 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600387
388#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
389#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
390static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
391
392module_param(skd_max_queue_depth, int, 0444);
393MODULE_PARM_DESC(skd_max_queue_depth,
394 "Maximum SCSI requests issued to s1120."
395 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
396
397static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
398module_param(skd_sgs_per_request, int, 0444);
399MODULE_PARM_DESC(skd_sgs_per_request,
400 "Maximum SG elements per block request."
401 " (1-4096, default==256)");
402
403static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
404module_param(skd_max_pass_thru, int, 0444);
405MODULE_PARM_DESC(skd_max_pass_thru,
406 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
407
408module_param(skd_dbg_level, int, 0444);
409MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
410
411module_param(skd_isr_comp_limit, int, 0444);
412MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
413
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600414/* Major device number dynamically assigned. */
415static u32 skd_major;
416
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600417static void skd_destruct(struct skd_device *skdev);
418static const struct block_device_operations skd_blockdev_ops;
419static void skd_send_fitmsg(struct skd_device *skdev,
420 struct skd_fitmsg_context *skmsg);
421static void skd_send_special_fitmsg(struct skd_device *skdev,
422 struct skd_special_context *skspcl);
423static void skd_request_fn(struct request_queue *rq);
424static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200425 struct skd_request_context *skreq, blk_status_t status);
426static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600427 struct skd_request_context *skreq);
428static void skd_postop_sg_list(struct skd_device *skdev,
429 struct skd_request_context *skreq);
430
431static void skd_restart_device(struct skd_device *skdev);
432static int skd_quiesce_dev(struct skd_device *skdev);
433static int skd_unquiesce_dev(struct skd_device *skdev);
434static void skd_release_special(struct skd_device *skdev,
435 struct skd_special_context *skspcl);
436static void skd_disable_interrupts(struct skd_device *skdev);
437static void skd_isr_fwstate(struct skd_device *skdev);
438static void skd_recover_requests(struct skd_device *skdev, int requeue);
439static void skd_soft_reset(struct skd_device *skdev);
440
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600441const char *skd_drive_state_to_str(int state);
442const char *skd_skdev_state_to_str(enum skd_drvr_state state);
443static void skd_log_skdev(struct skd_device *skdev, const char *event);
444static void skd_log_skmsg(struct skd_device *skdev,
445 struct skd_fitmsg_context *skmsg, const char *event);
446static void skd_log_skreq(struct skd_device *skdev,
447 struct skd_request_context *skreq, const char *event);
448
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600449/*
450 *****************************************************************************
451 * READ/WRITE REQUESTS
452 *****************************************************************************
453 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600454static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600455{
456 struct request_queue *q = skdev->queue;
457 struct request *req;
458
459 for (;; ) {
460 req = blk_peek_request(q);
461 if (req == NULL)
462 break;
463 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200464 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600465 }
466}
467
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600468static void
469skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
470 int data_dir, unsigned lba,
471 unsigned count)
472{
473 if (data_dir == READ)
474 scsi_req->cdb[0] = 0x28;
475 else
476 scsi_req->cdb[0] = 0x2a;
477
478 scsi_req->cdb[1] = 0;
479 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
480 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
481 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
482 scsi_req->cdb[5] = (lba & 0xff);
483 scsi_req->cdb[6] = 0;
484 scsi_req->cdb[7] = (count & 0xff00) >> 8;
485 scsi_req->cdb[8] = count & 0xff;
486 scsi_req->cdb[9] = 0;
487}
488
489static void
490skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400491 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600492{
493 skreq->flush_cmd = 1;
494
495 scsi_req->cdb[0] = 0x35;
496 scsi_req->cdb[1] = 0;
497 scsi_req->cdb[2] = 0;
498 scsi_req->cdb[3] = 0;
499 scsi_req->cdb[4] = 0;
500 scsi_req->cdb[5] = 0;
501 scsi_req->cdb[6] = 0;
502 scsi_req->cdb[7] = 0;
503 scsi_req->cdb[8] = 0;
504 scsi_req->cdb[9] = 0;
505}
506
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600507static void skd_request_fn_not_online(struct request_queue *q);
508
509static void skd_request_fn(struct request_queue *q)
510{
511 struct skd_device *skdev = q->queuedata;
512 struct skd_fitmsg_context *skmsg = NULL;
513 struct fit_msg_hdr *fmh = NULL;
514 struct skd_request_context *skreq;
515 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600516 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600517 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600518 u32 lba;
519 u32 count;
520 int data_dir;
Bart Van Assche4854afe2017-08-17 13:12:59 -0700521 __be64 be_dmaa;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600522 u64 cmdctxt;
523 u32 timo_slot;
524 void *cmd_ptr;
525 int flush, fua;
526
527 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
528 skd_request_fn_not_online(q);
529 return;
530 }
531
Jens Axboe6a5ec652013-11-01 10:38:45 -0600532 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600533 if (skdev->skmsg_free_list == NULL ||
534 skdev->skreq_free_list == NULL ||
535 skdev->in_flight >= skdev->queue_low_water_mark)
536 /* There is still some kind of shortage */
537 return;
538
Jens Axboe6a5ec652013-11-01 10:38:45 -0600539 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600540 }
541
542 /*
543 * Stop conditions:
544 * - There are no more native requests
545 * - There are already the maximum number of requests in progress
546 * - There are no more skd_request_context entries
547 * - There are no more FIT msg buffers
548 */
549 for (;; ) {
550
551 flush = fua = 0;
552
Jens Axboefcd37eb2013-11-01 10:14:56 -0600553 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600554
Jens Axboefcd37eb2013-11-01 10:14:56 -0600555 /* Are there any native requests to start? */
556 if (req == NULL)
557 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600558
Jens Axboefcd37eb2013-11-01 10:14:56 -0600559 lba = (u32)blk_rq_pos(req);
560 count = blk_rq_sectors(req);
561 data_dir = rq_data_dir(req);
562 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600563
Mike Christie3a5e02c2016-06-05 14:32:23 -0500564 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600565 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600566
Jens Axboefcd37eb2013-11-01 10:14:56 -0600567 if (io_flags & REQ_FUA)
568 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600569
Bart Van Asschef98806d2017-08-17 13:12:58 -0700570 dev_dbg(&skdev->pdev->dev,
571 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
572 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600573
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400574 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600575
576 /* Are too many requets already in progress? */
577 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700578 dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
579 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600580 break;
581 }
582
583 /* Is a skd_request_context available? */
584 skreq = skdev->skreq_free_list;
585 if (skreq == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700586 dev_dbg(&skdev->pdev->dev, "Out of req=%p\n", q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600587 break;
588 }
589 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
590 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
591
592 /* Now we check to see if we can get a fit msg */
593 if (skmsg == NULL) {
594 if (skdev->skmsg_free_list == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700595 dev_dbg(&skdev->pdev->dev, "Out of msg\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600596 break;
597 }
598 }
599
600 skreq->flush_cmd = 0;
601 skreq->n_sg = 0;
602 skreq->sg_byte_count = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600603
604 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400605 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600606 *
607 * At this point we are comitted to either start or reject
608 * the native request. Note that skd_request_context is
609 * available but is still at the head of the free list.
610 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600611 blk_start_request(req);
612 skreq->req = req;
613 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600614
Bart Van Assche19fc85c2017-08-17 13:13:04 -0700615 skreq->sg_data_dir = data_dir == READ ?
616 SKD_DATA_DIR_CARD_TO_HOST : SKD_DATA_DIR_HOST_TO_CARD;
617
618 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
619 dev_dbg(&skdev->pdev->dev, "error Out\n");
620 skd_end_request(skdev, skreq, BLK_STS_RESOURCE);
621 continue;
622 }
623
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600624 /* Either a FIT msg is in progress or we have to start one. */
625 if (skmsg == NULL) {
626 /* Are there any FIT msg buffers available? */
627 skmsg = skdev->skmsg_free_list;
628 if (skmsg == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700629 dev_dbg(&skdev->pdev->dev,
630 "Out of msg skdev=%p\n",
631 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600632 break;
633 }
634 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
635 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
636
637 skdev->skmsg_free_list = skmsg->next;
638
639 skmsg->state = SKD_MSG_STATE_BUSY;
640 skmsg->id += SKD_ID_INCR;
641
642 /* Initialize the FIT msg header */
643 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
644 memset(fmh, 0, sizeof(*fmh));
645 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
646 skmsg->length = sizeof(*fmh);
647 }
648
649 skreq->fitmsg_id = skmsg->id;
650
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600651 cmd_ptr = &skmsg->msg_buf[skmsg->length];
652 memset(cmd_ptr, 0, 32);
653
Bart Van Assche4854afe2017-08-17 13:12:59 -0700654 be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600655 cmdctxt = skreq->id + SKD_ID_INCR;
656
657 scsi_req = cmd_ptr;
658 scsi_req->hdr.tag = cmdctxt;
659 scsi_req->hdr.sg_list_dma_address = be_dmaa;
660
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600661 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600662 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
663 SKD_ASSERT(skreq->flush_cmd == 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600664 } else {
665 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
666 }
667
668 if (fua)
669 scsi_req->cdb[1] |= SKD_FUA_NV;
670
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600671 scsi_req->hdr.sg_list_len_bytes =
672 cpu_to_be32(skreq->sg_byte_count);
673
674 /* Complete resource allocations. */
675 skdev->skreq_free_list = skreq->next;
676 skreq->state = SKD_REQ_STATE_BUSY;
677 skreq->id += SKD_ID_INCR;
678
679 skmsg->length += sizeof(struct skd_scsi_request);
680 fmh->num_protocol_cmds_coalesced++;
681
682 /*
683 * Update the active request counts.
684 * Capture the timeout timestamp.
685 */
686 skreq->timeout_stamp = skdev->timeout_stamp;
687 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
688 skdev->timeout_slot[timo_slot]++;
689 skdev->in_flight++;
Bart Van Asschef98806d2017-08-17 13:12:58 -0700690 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
691 skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600692
693 /*
694 * If the FIT msg buffer is full send it.
695 */
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700696 if (fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600697 skd_send_fitmsg(skdev, skmsg);
698 skmsg = NULL;
699 fmh = NULL;
700 }
701 }
702
Bart Van Asschefe4fd722017-08-17 13:13:05 -0700703 /* If the FIT msg buffer is not empty send what we got. */
704 if (skmsg) {
705 WARN_ON_ONCE(!fmh->num_protocol_cmds_coalesced);
706 skd_send_fitmsg(skdev, skmsg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600707 skmsg = NULL;
708 fmh = NULL;
709 }
710
711 /*
712 * If req is non-NULL it means there is something to do but
713 * we are out of a resource.
714 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600715 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600716 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600717}
718
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400719static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200720 struct skd_request_context *skreq, blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600721{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600722 if (unlikely(error)) {
723 struct request *req = skreq->req;
724 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
725 u32 lba = (u32)blk_rq_pos(req);
726 u32 count = blk_rq_sectors(req);
727
Bart Van Asschef98806d2017-08-17 13:12:58 -0700728 dev_err(&skdev->pdev->dev,
729 "Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
730 count, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600731 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -0700732 dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", skreq->id,
733 error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600734
735 __blk_end_request_all(skreq->req, error);
736}
737
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200738static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400739 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600740{
741 struct request *req = skreq->req;
742 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
743 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
744 struct scatterlist *sg = &skreq->sg[0];
745 int n_sg;
746 int i;
747
748 skreq->sg_byte_count = 0;
749
750 /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
751 skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */
752
753 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
754 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200755 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600756
757 /*
758 * Map scatterlist to PCI bus addresses.
759 * Note PCI might change the number of entries.
760 */
761 n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
762 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200763 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600764
765 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
766
767 skreq->n_sg = n_sg;
768
769 for (i = 0; i < n_sg; i++) {
770 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
771 u32 cnt = sg_dma_len(&sg[i]);
772 uint64_t dma_addr = sg_dma_address(&sg[i]);
773
774 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
775 sgd->byte_count = cnt;
776 skreq->sg_byte_count += cnt;
777 sgd->host_side_addr = dma_addr;
778 sgd->dev_side_addr = 0;
779 }
780
781 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
782 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
783
784 if (unlikely(skdev->dbg_level > 1)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700785 dev_dbg(&skdev->pdev->dev,
786 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
787 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600788 for (i = 0; i < n_sg; i++) {
789 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
Bart Van Asschef98806d2017-08-17 13:12:58 -0700790
791 dev_dbg(&skdev->pdev->dev,
792 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
793 i, sgd->byte_count, sgd->control,
794 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600795 }
796 }
797
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200798 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600799}
800
Jens Axboefcd37eb2013-11-01 10:14:56 -0600801static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400802 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600803{
804 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
805 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
806
807 /*
808 * restore the next ptr for next IO request so we
809 * don't have to set it every time.
810 */
811 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
812 skreq->sksg_dma_address +
813 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
814 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
815}
816
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600817static void skd_request_fn_not_online(struct request_queue *q)
818{
819 struct skd_device *skdev = q->queuedata;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600820
821 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
822
823 skd_log_skdev(skdev, "req_not_online");
824 switch (skdev->state) {
825 case SKD_DRVR_STATE_PAUSING:
826 case SKD_DRVR_STATE_PAUSED:
827 case SKD_DRVR_STATE_STARTING:
828 case SKD_DRVR_STATE_RESTARTING:
829 case SKD_DRVR_STATE_WAIT_BOOT:
830 /* In case of starting, we haven't started the queue,
831 * so we can't get here... but requests are
832 * possibly hanging out waiting for us because we
833 * reported the dev/skd0 already. They'll wait
834 * forever if connect doesn't complete.
835 * What to do??? delay dev/skd0 ??
836 */
837 case SKD_DRVR_STATE_BUSY:
838 case SKD_DRVR_STATE_BUSY_IMMINENT:
839 case SKD_DRVR_STATE_BUSY_ERASE:
840 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
841 return;
842
843 case SKD_DRVR_STATE_BUSY_SANITIZE:
844 case SKD_DRVR_STATE_STOPPING:
845 case SKD_DRVR_STATE_SYNCING:
846 case SKD_DRVR_STATE_FAULT:
847 case SKD_DRVR_STATE_DISAPPEARED:
848 default:
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600849 break;
850 }
851
852 /* If we get here, terminate all pending block requeusts
853 * with EIO and any scsi pass thru with appropriate sense
854 */
855
856 skd_fail_all_pending(skdev);
857}
858
859/*
860 *****************************************************************************
861 * TIMER
862 *****************************************************************************
863 */
864
865static void skd_timer_tick_not_online(struct skd_device *skdev);
866
867static void skd_timer_tick(ulong arg)
868{
869 struct skd_device *skdev = (struct skd_device *)arg;
870
871 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600872 unsigned long reqflags;
873 u32 state;
874
875 if (skdev->state == SKD_DRVR_STATE_FAULT)
876 /* The driver has declared fault, and we want it to
877 * stay that way until driver is reloaded.
878 */
879 return;
880
881 spin_lock_irqsave(&skdev->lock, reqflags);
882
883 state = SKD_READL(skdev, FIT_STATUS);
884 state &= FIT_SR_DRIVE_STATE_MASK;
885 if (state != skdev->drive_state)
886 skd_isr_fwstate(skdev);
887
888 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
889 skd_timer_tick_not_online(skdev);
890 goto timer_func_out;
891 }
892 skdev->timeout_stamp++;
893 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
894
895 /*
896 * All requests that happened during the previous use of
897 * this slot should be done by now. The previous use was
898 * over 7 seconds ago.
899 */
900 if (skdev->timeout_slot[timo_slot] == 0)
901 goto timer_func_out;
902
903 /* Something is overdue */
Bart Van Asschef98806d2017-08-17 13:12:58 -0700904 dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
905 skdev->timeout_slot[timo_slot], skdev->in_flight);
906 dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
907 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600908
909 skdev->timer_countdown = SKD_DRAINING_TIMO;
910 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
911 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600912 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600913
914timer_func_out:
915 mod_timer(&skdev->timer, (jiffies + HZ));
916
917 spin_unlock_irqrestore(&skdev->lock, reqflags);
918}
919
920static void skd_timer_tick_not_online(struct skd_device *skdev)
921{
922 switch (skdev->state) {
923 case SKD_DRVR_STATE_IDLE:
924 case SKD_DRVR_STATE_LOAD:
925 break;
926 case SKD_DRVR_STATE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700927 dev_dbg(&skdev->pdev->dev,
928 "drive busy sanitize[%x], driver[%x]\n",
929 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600930 /* If we've been in sanitize for 3 seconds, we figure we're not
931 * going to get anymore completions, so recover requests now
932 */
933 if (skdev->timer_countdown > 0) {
934 skdev->timer_countdown--;
935 return;
936 }
937 skd_recover_requests(skdev, 0);
938 break;
939
940 case SKD_DRVR_STATE_BUSY:
941 case SKD_DRVR_STATE_BUSY_IMMINENT:
942 case SKD_DRVR_STATE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700943 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
944 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600945 if (skdev->timer_countdown > 0) {
946 skdev->timer_countdown--;
947 return;
948 }
Bart Van Asschef98806d2017-08-17 13:12:58 -0700949 dev_dbg(&skdev->pdev->dev,
950 "busy[%x], timedout=%d, restarting device.",
951 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600952 skd_restart_device(skdev);
953 break;
954
955 case SKD_DRVR_STATE_WAIT_BOOT:
956 case SKD_DRVR_STATE_STARTING:
957 if (skdev->timer_countdown > 0) {
958 skdev->timer_countdown--;
959 return;
960 }
961 /* For now, we fault the drive. Could attempt resets to
962 * revcover at some point. */
963 skdev->state = SKD_DRVR_STATE_FAULT;
964
Bart Van Asschef98806d2017-08-17 13:12:58 -0700965 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
966 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600967
968 /*start the queue so we can respond with error to requests */
969 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -0600970 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600971 skdev->gendisk_on = -1;
972 wake_up_interruptible(&skdev->waitq);
973 break;
974
975 case SKD_DRVR_STATE_ONLINE:
976 /* shouldn't get here. */
977 break;
978
979 case SKD_DRVR_STATE_PAUSING:
980 case SKD_DRVR_STATE_PAUSED:
981 break;
982
983 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
Bart Van Asschef98806d2017-08-17 13:12:58 -0700984 dev_dbg(&skdev->pdev->dev,
985 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
986 skdev->timo_slot, skdev->timer_countdown,
987 skdev->in_flight,
988 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600989 /* if the slot has cleared we can let the I/O continue */
990 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -0700991 dev_dbg(&skdev->pdev->dev,
992 "Slot drained, starting queue.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600993 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600994 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600995 return;
996 }
997 if (skdev->timer_countdown > 0) {
998 skdev->timer_countdown--;
999 return;
1000 }
1001 skd_restart_device(skdev);
1002 break;
1003
1004 case SKD_DRVR_STATE_RESTARTING:
1005 if (skdev->timer_countdown > 0) {
1006 skdev->timer_countdown--;
1007 return;
1008 }
1009 /* For now, we fault the drive. Could attempt resets to
1010 * revcover at some point. */
1011 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07001012 dev_err(&skdev->pdev->dev,
1013 "DriveFault Reconnect Timeout (%x)\n",
1014 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001015
1016 /*
1017 * Recovering does two things:
1018 * 1. completes IO with error
1019 * 2. reclaims dma resources
1020 * When is it safe to recover requests?
1021 * - if the drive state is faulted
1022 * - if the state is still soft reset after out timeout
1023 * - if the drive registers are dead (state = FF)
1024 * If it is "unsafe", we still need to recover, so we will
1025 * disable pci bus mastering and disable our interrupts.
1026 */
1027
1028 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1029 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1030 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1031 /* It never came out of soft reset. Try to
1032 * recover the requests and then let them
1033 * fail. This is to mitigate hung processes. */
1034 skd_recover_requests(skdev, 0);
1035 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001036 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
1037 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001038 pci_disable_device(skdev->pdev);
1039 skd_disable_interrupts(skdev);
1040 skd_recover_requests(skdev, 0);
1041 }
1042
1043 /*start the queue so we can respond with error to requests */
1044 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001045 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001046 skdev->gendisk_on = -1;
1047 wake_up_interruptible(&skdev->waitq);
1048 break;
1049
1050 case SKD_DRVR_STATE_RESUMING:
1051 case SKD_DRVR_STATE_STOPPING:
1052 case SKD_DRVR_STATE_SYNCING:
1053 case SKD_DRVR_STATE_FAULT:
1054 case SKD_DRVR_STATE_DISAPPEARED:
1055 default:
1056 break;
1057 }
1058}
1059
1060static int skd_start_timer(struct skd_device *skdev)
1061{
1062 int rc;
1063
1064 init_timer(&skdev->timer);
1065 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1066
1067 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1068 if (rc)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001069 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001070 return rc;
1071}
1072
1073static void skd_kill_timer(struct skd_device *skdev)
1074{
1075 del_timer_sync(&skdev->timer);
1076}
1077
1078/*
1079 *****************************************************************************
1080 * IOCTL
1081 *****************************************************************************
1082 */
1083static int skd_ioctl_sg_io(struct skd_device *skdev,
1084 fmode_t mode, void __user *argp);
1085static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1086 struct skd_sg_io *sksgio);
1087static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1088 struct skd_sg_io *sksgio);
1089static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1090 struct skd_sg_io *sksgio);
1091static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1092 struct skd_sg_io *sksgio, int dxfer_dir);
1093static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1094 struct skd_sg_io *sksgio);
1095static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1096static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1097 struct skd_sg_io *sksgio);
1098static int skd_sg_io_put_status(struct skd_device *skdev,
1099 struct skd_sg_io *sksgio);
1100
1101static void skd_complete_special(struct skd_device *skdev,
1102 volatile struct fit_completion_entry_v1
1103 *skcomp,
1104 volatile struct fit_comp_error_info *skerr,
1105 struct skd_special_context *skspcl);
1106
1107static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1108 uint cmd_in, ulong arg)
1109{
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001110 static const int sg_version_num = 30527;
1111 int rc = 0, timeout;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001112 struct gendisk *disk = bdev->bd_disk;
1113 struct skd_device *skdev = disk->private_data;
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001114 int __user *p = (int __user *)arg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001115
Bart Van Asschef98806d2017-08-17 13:12:58 -07001116 dev_dbg(&skdev->pdev->dev,
1117 "%s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1118 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001119
1120 if (!capable(CAP_SYS_ADMIN))
1121 return -EPERM;
1122
1123 switch (cmd_in) {
1124 case SG_SET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001125 rc = get_user(timeout, p);
1126 if (!rc)
1127 disk->queue->sg_timeout = clock_t_to_jiffies(timeout);
1128 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001129 case SG_GET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001130 rc = jiffies_to_clock_t(disk->queue->sg_timeout);
1131 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001132 case SG_GET_VERSION_NUM:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001133 rc = put_user(sg_version_num, p);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001134 break;
1135 case SG_IO:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001136 rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001137 break;
1138
1139 default:
1140 rc = -ENOTTY;
1141 break;
1142 }
1143
Bart Van Asschef98806d2017-08-17 13:12:58 -07001144 dev_dbg(&skdev->pdev->dev, "%s: completion rc %d\n", disk->disk_name,
1145 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001146 return rc;
1147}
1148
1149static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1150 void __user *argp)
1151{
1152 int rc;
1153 struct skd_sg_io sksgio;
1154
1155 memset(&sksgio, 0, sizeof(sksgio));
1156 sksgio.mode = mode;
1157 sksgio.argp = argp;
1158 sksgio.iov = &sksgio.no_iov_iov;
1159
1160 switch (skdev->state) {
1161 case SKD_DRVR_STATE_ONLINE:
1162 case SKD_DRVR_STATE_BUSY_IMMINENT:
1163 break;
1164
1165 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001166 dev_dbg(&skdev->pdev->dev, "drive not online\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001167 rc = -ENXIO;
1168 goto out;
1169 }
1170
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001171 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1172 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001173 goto out;
1174
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001175 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1176 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001177 goto out;
1178
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001179 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1180 if (rc)
1181 goto out;
1182
1183 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1184 if (rc)
1185 goto out;
1186
1187 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1188 if (rc)
1189 goto out;
1190
1191 rc = skd_sg_io_await(skdev, &sksgio);
1192 if (rc)
1193 goto out;
1194
1195 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1196 if (rc)
1197 goto out;
1198
1199 rc = skd_sg_io_put_status(skdev, &sksgio);
1200 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001201 goto out;
1202
1203 rc = 0;
1204
1205out:
1206 skd_sg_io_release_skspcl(skdev, &sksgio);
1207
1208 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1209 kfree(sksgio.iov);
1210 return rc;
1211}
1212
1213static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1214 struct skd_sg_io *sksgio)
1215{
1216 struct sg_io_hdr *sgp = &sksgio->sg;
Bart Van Assche95895e12017-08-17 13:12:55 -07001217 int i, __maybe_unused acc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001218
1219 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001220 dev_dbg(&skdev->pdev->dev, "access sg failed %p\n",
1221 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001222 return -EFAULT;
1223 }
1224
1225 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001226 dev_dbg(&skdev->pdev->dev, "copy_from_user sg failed %p\n",
1227 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001228 return -EFAULT;
1229 }
1230
1231 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001232 dev_dbg(&skdev->pdev->dev, "interface_id invalid 0x%x\n",
1233 sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001234 return -EINVAL;
1235 }
1236
1237 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001238 dev_dbg(&skdev->pdev->dev, "cmd_len invalid %d\n",
1239 sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001240 return -EINVAL;
1241 }
1242
1243 if (sgp->iovec_count > 256) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001244 dev_dbg(&skdev->pdev->dev, "iovec_count invalid %d\n",
1245 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001246 return -EINVAL;
1247 }
1248
1249 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001250 dev_dbg(&skdev->pdev->dev, "dxfer_len invalid %d\n",
1251 sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001252 return -EINVAL;
1253 }
1254
1255 switch (sgp->dxfer_direction) {
1256 case SG_DXFER_NONE:
1257 acc = -1;
1258 break;
1259
1260 case SG_DXFER_TO_DEV:
1261 acc = VERIFY_READ;
1262 break;
1263
1264 case SG_DXFER_FROM_DEV:
1265 case SG_DXFER_TO_FROM_DEV:
1266 acc = VERIFY_WRITE;
1267 break;
1268
1269 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07001270 dev_dbg(&skdev->pdev->dev, "dxfer_dir invalid %d\n",
1271 sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001272 return -EINVAL;
1273 }
1274
1275 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001276 dev_dbg(&skdev->pdev->dev, "copy_from_user cmdp failed %p\n",
1277 sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001278 return -EFAULT;
1279 }
1280
1281 if (sgp->mx_sb_len != 0) {
1282 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001283 dev_dbg(&skdev->pdev->dev, "access sbp failed %p\n",
1284 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001285 return -EFAULT;
1286 }
1287 }
1288
1289 if (sgp->iovec_count == 0) {
1290 sksgio->iov[0].iov_base = sgp->dxferp;
1291 sksgio->iov[0].iov_len = sgp->dxfer_len;
1292 sksgio->iovcnt = 1;
1293 sksgio->dxfer_len = sgp->dxfer_len;
1294 } else {
1295 struct sg_iovec *iov;
1296 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1297 size_t iov_data_len;
1298
1299 iov = kmalloc(nbytes, GFP_KERNEL);
1300 if (iov == NULL) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001301 dev_dbg(&skdev->pdev->dev, "alloc iovec failed %d\n",
1302 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001303 return -ENOMEM;
1304 }
1305 sksgio->iov = iov;
1306 sksgio->iovcnt = sgp->iovec_count;
1307
1308 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001309 dev_dbg(&skdev->pdev->dev,
1310 "copy_from_user iovec failed %p\n",
1311 sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001312 return -EFAULT;
1313 }
1314
1315 /*
1316 * Sum up the vecs, making sure they don't overflow
1317 */
1318 iov_data_len = 0;
1319 for (i = 0; i < sgp->iovec_count; i++) {
1320 if (iov_data_len + iov[i].iov_len < iov_data_len)
1321 return -EINVAL;
1322 iov_data_len += iov[i].iov_len;
1323 }
1324
1325 /* SG_IO howto says that the shorter of the two wins */
1326 if (sgp->dxfer_len < iov_data_len) {
1327 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1328 sgp->iovec_count,
1329 sgp->dxfer_len);
1330 sksgio->dxfer_len = sgp->dxfer_len;
1331 } else
1332 sksgio->dxfer_len = iov_data_len;
1333 }
1334
1335 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1336 struct sg_iovec *iov = sksgio->iov;
1337 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1338 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001339 dev_dbg(&skdev->pdev->dev,
1340 "access data failed %p/%zd\n",
1341 iov->iov_base, iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001342 return -EFAULT;
1343 }
1344 }
1345 }
1346
1347 return 0;
1348}
1349
1350static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1351 struct skd_sg_io *sksgio)
1352{
1353 struct skd_special_context *skspcl = NULL;
1354 int rc;
1355
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001356 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001357 ulong flags;
1358
1359 spin_lock_irqsave(&skdev->lock, flags);
1360 skspcl = skdev->skspcl_free_list;
1361 if (skspcl != NULL) {
1362 skdev->skspcl_free_list =
1363 (struct skd_special_context *)skspcl->req.next;
1364 skspcl->req.id += SKD_ID_INCR;
1365 skspcl->req.state = SKD_REQ_STATE_SETUP;
1366 skspcl->orphaned = 0;
1367 skspcl->req.n_sg = 0;
1368 }
1369 spin_unlock_irqrestore(&skdev->lock, flags);
1370
1371 if (skspcl != NULL) {
1372 rc = 0;
1373 break;
1374 }
1375
Bart Van Asschef98806d2017-08-17 13:12:58 -07001376 dev_dbg(&skdev->pdev->dev, "blocking\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001377
1378 rc = wait_event_interruptible_timeout(
1379 skdev->waitq,
1380 (skdev->skspcl_free_list != NULL),
1381 msecs_to_jiffies(sksgio->sg.timeout));
1382
Bart Van Asschef98806d2017-08-17 13:12:58 -07001383 dev_dbg(&skdev->pdev->dev, "unblocking, rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001384
1385 if (rc <= 0) {
1386 if (rc == 0)
1387 rc = -ETIMEDOUT;
1388 else
1389 rc = -EINTR;
1390 break;
1391 }
1392 /*
1393 * If we get here rc > 0 meaning the timeout to
1394 * wait_event_interruptible_timeout() had time left, hence the
1395 * sought event -- non-empty free list -- happened.
1396 * Retry the allocation.
1397 */
1398 }
1399 sksgio->skspcl = skspcl;
1400
1401 return rc;
1402}
1403
1404static int skd_skreq_prep_buffering(struct skd_device *skdev,
1405 struct skd_request_context *skreq,
1406 u32 dxfer_len)
1407{
1408 u32 resid = dxfer_len;
1409
1410 /*
1411 * The DMA engine must have aligned addresses and byte counts.
1412 */
1413 resid += (-resid) & 3;
1414 skreq->sg_byte_count = resid;
1415
1416 skreq->n_sg = 0;
1417
1418 while (resid > 0) {
1419 u32 nbytes = PAGE_SIZE;
1420 u32 ix = skreq->n_sg;
1421 struct scatterlist *sg = &skreq->sg[ix];
1422 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1423 struct page *page;
1424
1425 if (nbytes > resid)
1426 nbytes = resid;
1427
1428 page = alloc_page(GFP_KERNEL);
1429 if (page == NULL)
1430 return -ENOMEM;
1431
1432 sg_set_page(sg, page, nbytes, 0);
1433
1434 /* TODO: This should be going through a pci_???()
1435 * routine to do proper mapping. */
1436 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1437 sksg->byte_count = nbytes;
1438
1439 sksg->host_side_addr = sg_phys(sg);
1440
1441 sksg->dev_side_addr = 0;
1442 sksg->next_desc_ptr = skreq->sksg_dma_address +
1443 (ix + 1) * sizeof(*sksg);
1444
1445 skreq->n_sg++;
1446 resid -= nbytes;
1447 }
1448
1449 if (skreq->n_sg > 0) {
1450 u32 ix = skreq->n_sg - 1;
1451 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1452
1453 sksg->control = FIT_SGD_CONTROL_LAST;
1454 sksg->next_desc_ptr = 0;
1455 }
1456
1457 if (unlikely(skdev->dbg_level > 1)) {
1458 u32 i;
1459
Bart Van Asschef98806d2017-08-17 13:12:58 -07001460 dev_dbg(&skdev->pdev->dev,
1461 "skreq=%x sksg_list=%p sksg_dma=%llx\n",
1462 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001463 for (i = 0; i < skreq->n_sg; i++) {
1464 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1465
Bart Van Asschef98806d2017-08-17 13:12:58 -07001466 dev_dbg(&skdev->pdev->dev,
1467 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1468 i, sgd->byte_count, sgd->control,
1469 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001470 }
1471 }
1472
1473 return 0;
1474}
1475
1476static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1477 struct skd_sg_io *sksgio)
1478{
1479 struct skd_special_context *skspcl = sksgio->skspcl;
1480 struct skd_request_context *skreq = &skspcl->req;
1481 u32 dxfer_len = sksgio->dxfer_len;
1482 int rc;
1483
1484 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1485 /*
1486 * Eventually, errors or not, skd_release_special() is called
1487 * to recover allocations including partial allocations.
1488 */
1489 return rc;
1490}
1491
1492static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1493 struct skd_sg_io *sksgio, int dxfer_dir)
1494{
1495 struct skd_special_context *skspcl = sksgio->skspcl;
1496 u32 iov_ix = 0;
1497 struct sg_iovec curiov;
1498 u32 sksg_ix = 0;
1499 u8 *bufp = NULL;
1500 u32 buf_len = 0;
1501 u32 resid = sksgio->dxfer_len;
1502 int rc;
1503
1504 curiov.iov_len = 0;
1505 curiov.iov_base = NULL;
1506
1507 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1508 if (dxfer_dir != SG_DXFER_TO_DEV ||
1509 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1510 return 0;
1511 }
1512
1513 while (resid > 0) {
1514 u32 nbytes = PAGE_SIZE;
1515
1516 if (curiov.iov_len == 0) {
1517 curiov = sksgio->iov[iov_ix++];
1518 continue;
1519 }
1520
1521 if (buf_len == 0) {
1522 struct page *page;
1523 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1524 bufp = page_address(page);
1525 buf_len = PAGE_SIZE;
1526 }
1527
1528 nbytes = min_t(u32, nbytes, resid);
1529 nbytes = min_t(u32, nbytes, curiov.iov_len);
1530 nbytes = min_t(u32, nbytes, buf_len);
1531
1532 if (dxfer_dir == SG_DXFER_TO_DEV)
1533 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1534 else
1535 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1536
1537 if (rc)
1538 return -EFAULT;
1539
1540 resid -= nbytes;
1541 curiov.iov_len -= nbytes;
1542 curiov.iov_base += nbytes;
1543 buf_len -= nbytes;
1544 }
1545
1546 return 0;
1547}
1548
1549static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1550 struct skd_sg_io *sksgio)
1551{
1552 struct skd_special_context *skspcl = sksgio->skspcl;
1553 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
1554 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
1555
1556 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1557
1558 /* Initialize the FIT msg header */
1559 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1560 fmh->num_protocol_cmds_coalesced = 1;
1561
1562 /* Initialize the SCSI request */
1563 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1564 scsi_req->hdr.sg_list_dma_address =
1565 cpu_to_be64(skspcl->req.sksg_dma_address);
1566 scsi_req->hdr.tag = skspcl->req.id;
1567 scsi_req->hdr.sg_list_len_bytes =
1568 cpu_to_be32(skspcl->req.sg_byte_count);
1569 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1570
1571 skspcl->req.state = SKD_REQ_STATE_BUSY;
1572 skd_send_special_fitmsg(skdev, skspcl);
1573
1574 return 0;
1575}
1576
1577static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1578{
1579 unsigned long flags;
1580 int rc;
1581
1582 rc = wait_event_interruptible_timeout(skdev->waitq,
1583 (sksgio->skspcl->req.state !=
1584 SKD_REQ_STATE_BUSY),
1585 msecs_to_jiffies(sksgio->sg.
1586 timeout));
1587
1588 spin_lock_irqsave(&skdev->lock, flags);
1589
1590 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001591 dev_dbg(&skdev->pdev->dev, "skspcl %p aborted\n",
1592 sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001593
1594 /* Build check cond, sense and let command finish. */
1595 /* For a timeout, we must fabricate completion and sense
1596 * data to complete the command */
1597 sksgio->skspcl->req.completion.status =
1598 SAM_STAT_CHECK_CONDITION;
1599
1600 memset(&sksgio->skspcl->req.err_info, 0,
1601 sizeof(sksgio->skspcl->req.err_info));
1602 sksgio->skspcl->req.err_info.type = 0x70;
1603 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1604 sksgio->skspcl->req.err_info.code = 0x44;
1605 sksgio->skspcl->req.err_info.qual = 0;
1606 rc = 0;
1607 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1608 /* No longer on the adapter. We finish. */
1609 rc = 0;
1610 else {
1611 /* Something's gone wrong. Still busy. Timeout or
1612 * user interrupted (control-C). Mark as an orphan
1613 * so it will be disposed when completed. */
1614 sksgio->skspcl->orphaned = 1;
1615 sksgio->skspcl = NULL;
1616 if (rc == 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001617 dev_dbg(&skdev->pdev->dev, "timed out %p (%u ms)\n",
1618 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001619 rc = -ETIMEDOUT;
1620 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001621 dev_dbg(&skdev->pdev->dev, "cntlc %p\n", sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001622 rc = -EINTR;
1623 }
1624 }
1625
1626 spin_unlock_irqrestore(&skdev->lock, flags);
1627
1628 return rc;
1629}
1630
1631static int skd_sg_io_put_status(struct skd_device *skdev,
1632 struct skd_sg_io *sksgio)
1633{
1634 struct sg_io_hdr *sgp = &sksgio->sg;
1635 struct skd_special_context *skspcl = sksgio->skspcl;
1636 int resid = 0;
1637
1638 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1639
1640 sgp->status = skspcl->req.completion.status;
1641 resid = sksgio->dxfer_len - nb;
1642
1643 sgp->masked_status = sgp->status & STATUS_MASK;
1644 sgp->msg_status = 0;
1645 sgp->host_status = 0;
1646 sgp->driver_status = 0;
1647 sgp->resid = resid;
1648 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1649 sgp->info |= SG_INFO_CHECK;
1650
Bart Van Asschef98806d2017-08-17 13:12:58 -07001651 dev_dbg(&skdev->pdev->dev, "status %x masked %x resid 0x%x\n",
1652 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001653
1654 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1655 if (sgp->mx_sb_len > 0) {
1656 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1657 u32 nbytes = sizeof(*ei);
1658
1659 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1660
1661 sgp->sb_len_wr = nbytes;
1662
1663 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001664 dev_dbg(&skdev->pdev->dev,
1665 "copy_to_user sense failed %p\n",
1666 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001667 return -EFAULT;
1668 }
1669 }
1670 }
1671
1672 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001673 dev_dbg(&skdev->pdev->dev, "copy_to_user sg failed %p\n",
1674 sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001675 return -EFAULT;
1676 }
1677
1678 return 0;
1679}
1680
1681static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1682 struct skd_sg_io *sksgio)
1683{
1684 struct skd_special_context *skspcl = sksgio->skspcl;
1685
1686 if (skspcl != NULL) {
1687 ulong flags;
1688
1689 sksgio->skspcl = NULL;
1690
1691 spin_lock_irqsave(&skdev->lock, flags);
1692 skd_release_special(skdev, skspcl);
1693 spin_unlock_irqrestore(&skdev->lock, flags);
1694 }
1695
1696 return 0;
1697}
1698
1699/*
1700 *****************************************************************************
1701 * INTERNAL REQUESTS -- generated by driver itself
1702 *****************************************************************************
1703 */
1704
1705static int skd_format_internal_skspcl(struct skd_device *skdev)
1706{
1707 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1708 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1709 struct fit_msg_hdr *fmh;
1710 uint64_t dma_address;
1711 struct skd_scsi_request *scsi;
1712
1713 fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0];
1714 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1715 fmh->num_protocol_cmds_coalesced = 1;
1716
1717 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1718 memset(scsi, 0, sizeof(*scsi));
1719 dma_address = skspcl->req.sksg_dma_address;
1720 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1721 sgd->control = FIT_SGD_CONTROL_LAST;
1722 sgd->byte_count = 0;
1723 sgd->host_side_addr = skspcl->db_dma_address;
1724 sgd->dev_side_addr = 0;
1725 sgd->next_desc_ptr = 0LL;
1726
1727 return 1;
1728}
1729
1730#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1731
1732static void skd_send_internal_skspcl(struct skd_device *skdev,
1733 struct skd_special_context *skspcl,
1734 u8 opcode)
1735{
1736 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1737 struct skd_scsi_request *scsi;
1738 unsigned char *buf = skspcl->data_buf;
1739 int i;
1740
1741 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1742 /*
1743 * A refresh is already in progress.
1744 * Just wait for it to finish.
1745 */
1746 return;
1747
1748 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1749 skspcl->req.state = SKD_REQ_STATE_BUSY;
1750 skspcl->req.id += SKD_ID_INCR;
1751
1752 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1753 scsi->hdr.tag = skspcl->req.id;
1754
1755 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1756
1757 switch (opcode) {
1758 case TEST_UNIT_READY:
1759 scsi->cdb[0] = TEST_UNIT_READY;
1760 sgd->byte_count = 0;
1761 scsi->hdr.sg_list_len_bytes = 0;
1762 break;
1763
1764 case READ_CAPACITY:
1765 scsi->cdb[0] = READ_CAPACITY;
1766 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1767 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1768 break;
1769
1770 case INQUIRY:
1771 scsi->cdb[0] = INQUIRY;
1772 scsi->cdb[1] = 0x01; /* evpd */
1773 scsi->cdb[2] = 0x80; /* serial number page */
1774 scsi->cdb[4] = 0x10;
1775 sgd->byte_count = 16;
1776 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1777 break;
1778
1779 case SYNCHRONIZE_CACHE:
1780 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1781 sgd->byte_count = 0;
1782 scsi->hdr.sg_list_len_bytes = 0;
1783 break;
1784
1785 case WRITE_BUFFER:
1786 scsi->cdb[0] = WRITE_BUFFER;
1787 scsi->cdb[1] = 0x02;
1788 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1789 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1790 sgd->byte_count = WR_BUF_SIZE;
1791 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1792 /* fill incrementing byte pattern */
1793 for (i = 0; i < sgd->byte_count; i++)
1794 buf[i] = i & 0xFF;
1795 break;
1796
1797 case READ_BUFFER:
1798 scsi->cdb[0] = READ_BUFFER;
1799 scsi->cdb[1] = 0x02;
1800 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1801 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1802 sgd->byte_count = WR_BUF_SIZE;
1803 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1804 memset(skspcl->data_buf, 0, sgd->byte_count);
1805 break;
1806
1807 default:
1808 SKD_ASSERT("Don't know what to send");
1809 return;
1810
1811 }
1812 skd_send_special_fitmsg(skdev, skspcl);
1813}
1814
1815static void skd_refresh_device_data(struct skd_device *skdev)
1816{
1817 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1818
1819 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1820}
1821
1822static int skd_chk_read_buf(struct skd_device *skdev,
1823 struct skd_special_context *skspcl)
1824{
1825 unsigned char *buf = skspcl->data_buf;
1826 int i;
1827
1828 /* check for incrementing byte pattern */
1829 for (i = 0; i < WR_BUF_SIZE; i++)
1830 if (buf[i] != (i & 0xFF))
1831 return 1;
1832
1833 return 0;
1834}
1835
1836static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1837 u8 code, u8 qual, u8 fruc)
1838{
1839 /* If the check condition is of special interest, log a message */
1840 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1841 && (code == 0x04) && (qual == 0x06)) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001842 dev_err(&skdev->pdev->dev,
1843 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1844 key, code, qual, fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001845 }
1846}
1847
1848static void skd_complete_internal(struct skd_device *skdev,
1849 volatile struct fit_completion_entry_v1
1850 *skcomp,
1851 volatile struct fit_comp_error_info *skerr,
1852 struct skd_special_context *skspcl)
1853{
1854 u8 *buf = skspcl->data_buf;
1855 u8 status;
1856 int i;
1857 struct skd_scsi_request *scsi =
1858 (struct skd_scsi_request *)&skspcl->msg_buf[64];
1859
Bart Van Assche760b48c2017-08-17 13:13:00 -07001860 lockdep_assert_held(&skdev->lock);
1861
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001862 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1863
Bart Van Asschef98806d2017-08-17 13:12:58 -07001864 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001865
1866 skspcl->req.completion = *skcomp;
1867 skspcl->req.state = SKD_REQ_STATE_IDLE;
1868 skspcl->req.id += SKD_ID_INCR;
1869
1870 status = skspcl->req.completion.status;
1871
1872 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1873 skerr->qual, skerr->fruc);
1874
1875 switch (scsi->cdb[0]) {
1876 case TEST_UNIT_READY:
1877 if (status == SAM_STAT_GOOD)
1878 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1879 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1880 (skerr->key == MEDIUM_ERROR))
1881 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1882 else {
1883 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001884 dev_dbg(&skdev->pdev->dev,
1885 "TUR failed, don't send anymore state 0x%x\n",
1886 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001887 return;
1888 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001889 dev_dbg(&skdev->pdev->dev,
1890 "**** TUR failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001891 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1892 }
1893 break;
1894
1895 case WRITE_BUFFER:
1896 if (status == SAM_STAT_GOOD)
1897 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1898 else {
1899 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001900 dev_dbg(&skdev->pdev->dev,
1901 "write buffer failed, don't send anymore state 0x%x\n",
1902 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001903 return;
1904 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001905 dev_dbg(&skdev->pdev->dev,
1906 "**** write buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001907 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1908 }
1909 break;
1910
1911 case READ_BUFFER:
1912 if (status == SAM_STAT_GOOD) {
1913 if (skd_chk_read_buf(skdev, skspcl) == 0)
1914 skd_send_internal_skspcl(skdev, skspcl,
1915 READ_CAPACITY);
1916 else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001917 dev_err(&skdev->pdev->dev,
1918 "*** W/R Buffer mismatch %d ***\n",
1919 skdev->connect_retries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001920 if (skdev->connect_retries <
1921 SKD_MAX_CONNECT_RETRIES) {
1922 skdev->connect_retries++;
1923 skd_soft_reset(skdev);
1924 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001925 dev_err(&skdev->pdev->dev,
1926 "W/R Buffer Connect Error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001927 return;
1928 }
1929 }
1930
1931 } else {
1932 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001933 dev_dbg(&skdev->pdev->dev,
1934 "read buffer failed, don't send anymore state 0x%x\n",
1935 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001936 return;
1937 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07001938 dev_dbg(&skdev->pdev->dev,
1939 "**** read buffer failed, retry skerr\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001940 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1941 }
1942 break;
1943
1944 case READ_CAPACITY:
1945 skdev->read_cap_is_valid = 0;
1946 if (status == SAM_STAT_GOOD) {
1947 skdev->read_cap_last_lba =
1948 (buf[0] << 24) | (buf[1] << 16) |
1949 (buf[2] << 8) | buf[3];
1950 skdev->read_cap_blocksize =
1951 (buf[4] << 24) | (buf[5] << 16) |
1952 (buf[6] << 8) | buf[7];
1953
Bart Van Asschef98806d2017-08-17 13:12:58 -07001954 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1955 skdev->read_cap_last_lba,
1956 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001957
1958 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1959
1960 skdev->read_cap_is_valid = 1;
1961
1962 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1963 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1964 (skerr->key == MEDIUM_ERROR)) {
1965 skdev->read_cap_last_lba = ~0;
1966 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
Bart Van Asschef98806d2017-08-17 13:12:58 -07001967 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 -06001968 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1969 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07001970 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001971 skd_send_internal_skspcl(skdev, skspcl,
1972 TEST_UNIT_READY);
1973 }
1974 break;
1975
1976 case INQUIRY:
1977 skdev->inquiry_is_valid = 0;
1978 if (status == SAM_STAT_GOOD) {
1979 skdev->inquiry_is_valid = 1;
1980
1981 for (i = 0; i < 12; i++)
1982 skdev->inq_serial_num[i] = buf[i + 4];
1983 skdev->inq_serial_num[12] = 0;
1984 }
1985
1986 if (skd_unquiesce_dev(skdev) < 0)
Bart Van Asschef98806d2017-08-17 13:12:58 -07001987 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001988 /* connection is complete */
1989 skdev->connect_retries = 0;
1990 break;
1991
1992 case SYNCHRONIZE_CACHE:
1993 if (status == SAM_STAT_GOOD)
1994 skdev->sync_done = 1;
1995 else
1996 skdev->sync_done = -1;
1997 wake_up_interruptible(&skdev->waitq);
1998 break;
1999
2000 default:
2001 SKD_ASSERT("we didn't send this");
2002 }
2003}
2004
2005/*
2006 *****************************************************************************
2007 * FIT MESSAGES
2008 *****************************************************************************
2009 */
2010
2011static void skd_send_fitmsg(struct skd_device *skdev,
2012 struct skd_fitmsg_context *skmsg)
2013{
2014 u64 qcmd;
2015 struct fit_msg_hdr *fmh;
2016
Bart Van Asschef98806d2017-08-17 13:12:58 -07002017 dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
2018 skmsg->mb_dma_address, skdev->in_flight);
2019 dev_dbg(&skdev->pdev->dev, "msg_buf 0x%p, offset %x\n", skmsg->msg_buf,
2020 skmsg->offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002021
2022 qcmd = skmsg->mb_dma_address;
2023 qcmd |= FIT_QCMD_QID_NORMAL;
2024
2025 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
2026 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2027
2028 if (unlikely(skdev->dbg_level > 1)) {
2029 u8 *bp = (u8 *)skmsg->msg_buf;
2030 int i;
2031 for (i = 0; i < skmsg->length; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002032 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
2033 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002034 if (i == 0)
2035 i = 64 - 8;
2036 }
2037 }
2038
2039 if (skmsg->length > 256)
2040 qcmd |= FIT_QCMD_MSGSIZE_512;
2041 else if (skmsg->length > 128)
2042 qcmd |= FIT_QCMD_MSGSIZE_256;
2043 else if (skmsg->length > 64)
2044 qcmd |= FIT_QCMD_MSGSIZE_128;
2045 else
2046 /*
2047 * This makes no sense because the FIT msg header is
2048 * 64 bytes. If the msg is only 64 bytes long it has
2049 * no payload.
2050 */
2051 qcmd |= FIT_QCMD_MSGSIZE_64;
2052
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002053 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2054 smp_wmb();
2055
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002056 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002057}
2058
2059static void skd_send_special_fitmsg(struct skd_device *skdev,
2060 struct skd_special_context *skspcl)
2061{
2062 u64 qcmd;
2063
2064 if (unlikely(skdev->dbg_level > 1)) {
2065 u8 *bp = (u8 *)skspcl->msg_buf;
2066 int i;
2067
2068 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002069 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
2070 &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002071 if (i == 0)
2072 i = 64 - 8;
2073 }
2074
Bart Van Asschef98806d2017-08-17 13:12:58 -07002075 dev_dbg(&skdev->pdev->dev,
2076 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2077 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2078 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002079 for (i = 0; i < skspcl->req.n_sg; i++) {
2080 struct fit_sg_descriptor *sgd =
2081 &skspcl->req.sksg_list[i];
2082
Bart Van Asschef98806d2017-08-17 13:12:58 -07002083 dev_dbg(&skdev->pdev->dev,
2084 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
2085 i, sgd->byte_count, sgd->control,
2086 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002087 }
2088 }
2089
2090 /*
2091 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2092 * and one 64-byte SSDI command.
2093 */
2094 qcmd = skspcl->mb_dma_address;
2095 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2096
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002097 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2098 smp_wmb();
2099
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002100 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2101}
2102
2103/*
2104 *****************************************************************************
2105 * COMPLETION QUEUE
2106 *****************************************************************************
2107 */
2108
2109static void skd_complete_other(struct skd_device *skdev,
2110 volatile struct fit_completion_entry_v1 *skcomp,
2111 volatile struct fit_comp_error_info *skerr);
2112
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002113struct sns_info {
2114 u8 type;
2115 u8 stat;
2116 u8 key;
2117 u8 asc;
2118 u8 ascq;
2119 u8 mask;
2120 enum skd_check_status_action action;
2121};
2122
2123static struct sns_info skd_chkstat_table[] = {
2124 /* Good */
2125 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2126 SKD_CHECK_STATUS_REPORT_GOOD },
2127
2128 /* Smart alerts */
2129 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2130 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2131 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2132 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2133 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2134 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2135
2136 /* Retry (with limits) */
2137 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2138 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2139 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2140 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2141 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2142 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2143 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2144 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2145
2146 /* Busy (or about to be) */
2147 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2148 SKD_CHECK_STATUS_BUSY_IMMINENT },
2149};
2150
2151/*
2152 * Look up status and sense data to decide how to handle the error
2153 * from the device.
2154 * mask says which fields must match e.g., mask=0x18 means check
2155 * type and stat, ignore key, asc, ascq.
2156 */
2157
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002158static enum skd_check_status_action
2159skd_check_status(struct skd_device *skdev,
2160 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002161{
2162 int i, n;
2163
Bart Van Asschef98806d2017-08-17 13:12:58 -07002164 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2165 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002166
Bart Van Asschef98806d2017-08-17 13:12:58 -07002167 dev_dbg(&skdev->pdev->dev,
2168 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2169 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
2170 skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002171
2172 /* Does the info match an entry in the good category? */
2173 n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
2174 for (i = 0; i < n; i++) {
2175 struct sns_info *sns = &skd_chkstat_table[i];
2176
2177 if (sns->mask & 0x10)
2178 if (skerr->type != sns->type)
2179 continue;
2180
2181 if (sns->mask & 0x08)
2182 if (cmp_status != sns->stat)
2183 continue;
2184
2185 if (sns->mask & 0x04)
2186 if (skerr->key != sns->key)
2187 continue;
2188
2189 if (sns->mask & 0x02)
2190 if (skerr->code != sns->asc)
2191 continue;
2192
2193 if (sns->mask & 0x01)
2194 if (skerr->qual != sns->ascq)
2195 continue;
2196
2197 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002198 dev_err(&skdev->pdev->dev,
2199 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
2200 skerr->key, skerr->code, skerr->qual);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002201 }
2202 return sns->action;
2203 }
2204
2205 /* No other match, so nonzero status means error,
2206 * zero status means good
2207 */
2208 if (cmp_status) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002209 dev_dbg(&skdev->pdev->dev, "status check: error\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002210 return SKD_CHECK_STATUS_REPORT_ERROR;
2211 }
2212
Bart Van Asschef98806d2017-08-17 13:12:58 -07002213 dev_dbg(&skdev->pdev->dev, "status check good default\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002214 return SKD_CHECK_STATUS_REPORT_GOOD;
2215}
2216
2217static void skd_resolve_req_exception(struct skd_device *skdev,
2218 struct skd_request_context *skreq)
2219{
2220 u8 cmp_status = skreq->completion.status;
2221
2222 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2223 case SKD_CHECK_STATUS_REPORT_GOOD:
2224 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002225 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002226 break;
2227
2228 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2229 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002230 blk_requeue_request(skdev->queue, skreq->req);
Bart Van Asschef98806d2017-08-17 13:12:58 -07002231 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002232 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2233 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2234 skd_quiesce_dev(skdev);
2235 break;
2236
2237 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002238 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2239 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002240 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002241 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002242 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07002243 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002244
2245 case SKD_CHECK_STATUS_REPORT_ERROR:
2246 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002247 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002248 break;
2249 }
2250}
2251
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002252/* assume spinlock is already held */
2253static void skd_release_skreq(struct skd_device *skdev,
2254 struct skd_request_context *skreq)
2255{
2256 u32 msg_slot;
2257 struct skd_fitmsg_context *skmsg;
2258
2259 u32 timo_slot;
2260
2261 /*
2262 * Reclaim the FIT msg buffer if this is
2263 * the first of the requests it carried to
2264 * be completed. The FIT msg buffer used to
2265 * send this request cannot be reused until
2266 * we are sure the s1120 card has copied
2267 * it to its memory. The FIT msg might have
2268 * contained several requests. As soon as
2269 * any of them are completed we know that
2270 * the entire FIT msg was transferred.
2271 * Only the first completed request will
2272 * match the FIT msg buffer id. The FIT
2273 * msg buffer id is immediately updated.
2274 * When subsequent requests complete the FIT
2275 * msg buffer id won't match, so we know
2276 * quite cheaply that it is already done.
2277 */
2278 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2279 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2280
2281 skmsg = &skdev->skmsg_table[msg_slot];
2282 if (skmsg->id == skreq->fitmsg_id) {
2283 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2284 SKD_ASSERT(skmsg->outstanding > 0);
2285 skmsg->outstanding--;
2286 if (skmsg->outstanding == 0) {
2287 skmsg->state = SKD_MSG_STATE_IDLE;
2288 skmsg->id += SKD_ID_INCR;
2289 skmsg->next = skdev->skmsg_free_list;
2290 skdev->skmsg_free_list = skmsg;
2291 }
2292 }
2293
2294 /*
2295 * Decrease the number of active requests.
2296 * Also decrements the count in the timeout slot.
2297 */
2298 SKD_ASSERT(skdev->in_flight > 0);
2299 skdev->in_flight -= 1;
2300
2301 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2302 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2303 skdev->timeout_slot[timo_slot] -= 1;
2304
2305 /*
2306 * Reset backpointer
2307 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002308 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002309
2310 /*
2311 * Reclaim the skd_request_context
2312 */
2313 skreq->state = SKD_REQ_STATE_IDLE;
2314 skreq->id += SKD_ID_INCR;
2315 skreq->next = skdev->skreq_free_list;
2316 skdev->skreq_free_list = skreq;
2317}
2318
2319#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2320
2321static void skd_do_inq_page_00(struct skd_device *skdev,
2322 volatile struct fit_completion_entry_v1 *skcomp,
2323 volatile struct fit_comp_error_info *skerr,
2324 uint8_t *cdb, uint8_t *buf)
2325{
2326 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2327
2328 /* Caller requested "supported pages". The driver needs to insert
2329 * its page.
2330 */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002331 dev_dbg(&skdev->pdev->dev,
2332 "skd_do_driver_inquiry: modify supported pages.\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002333
2334 /* If the device rejected the request because the CDB was
2335 * improperly formed, then just leave.
2336 */
2337 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2338 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2339 return;
2340
2341 /* Get the amount of space the caller allocated */
2342 max_bytes = (cdb[3] << 8) | cdb[4];
2343
2344 /* Get the number of pages actually returned by the device */
2345 drive_pages = (buf[2] << 8) | buf[3];
2346 drive_bytes = drive_pages + 4;
2347 new_size = drive_pages + 1;
2348
2349 /* Supported pages must be in numerical order, so find where
2350 * the driver page needs to be inserted into the list of
2351 * pages returned by the device.
2352 */
2353 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2354 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2355 return; /* Device using this page code. abort */
2356 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2357 break;
2358 }
2359
2360 if (insert_pt < max_bytes) {
2361 uint16_t u;
2362
2363 /* Shift everything up one byte to make room. */
2364 for (u = new_size + 3; u > insert_pt; u--)
2365 buf[u] = buf[u - 1];
2366 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2367
2368 /* SCSI byte order increment of num_returned_bytes by 1 */
2369 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002370 cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002371 }
2372
2373 /* update page length field to reflect the driver's page too */
2374 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2375 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2376}
2377
2378static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2379{
2380 int pcie_reg;
2381 u16 pci_bus_speed;
2382 u8 pci_lanes;
2383
2384 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2385 if (pcie_reg) {
2386 u16 linksta;
2387 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2388
2389 pci_bus_speed = linksta & 0xF;
2390 pci_lanes = (linksta & 0x3F0) >> 4;
2391 } else {
2392 *speed = STEC_LINK_UNKNOWN;
2393 *width = 0xFF;
2394 return;
2395 }
2396
2397 switch (pci_bus_speed) {
2398 case 1:
2399 *speed = STEC_LINK_2_5GTS;
2400 break;
2401 case 2:
2402 *speed = STEC_LINK_5GTS;
2403 break;
2404 case 3:
2405 *speed = STEC_LINK_8GTS;
2406 break;
2407 default:
2408 *speed = STEC_LINK_UNKNOWN;
2409 break;
2410 }
2411
2412 if (pci_lanes <= 0x20)
2413 *width = pci_lanes;
2414 else
2415 *width = 0xFF;
2416}
2417
2418static void skd_do_inq_page_da(struct skd_device *skdev,
2419 volatile struct fit_completion_entry_v1 *skcomp,
2420 volatile struct fit_comp_error_info *skerr,
2421 uint8_t *cdb, uint8_t *buf)
2422{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002423 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002424 unsigned max_bytes;
2425 struct driver_inquiry_data inq;
2426 u16 val;
2427
Bart Van Asschef98806d2017-08-17 13:12:58 -07002428 dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002429
2430 memset(&inq, 0, sizeof(inq));
2431
2432 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2433
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002434 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2435 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2436 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2437 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002438
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002439 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2440 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002441
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002442 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2443 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002444
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002445 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2446 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002447
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002448 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2449 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002450
2451 /* Driver version, fixed lenth, padded with spaces on the right */
2452 inq.driver_version_length = sizeof(inq.driver_version);
2453 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2454 memcpy(inq.driver_version, DRV_VER_COMPL,
2455 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2456
2457 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2458
2459 /* Clear the error set by the device */
2460 skcomp->status = SAM_STAT_GOOD;
2461 memset((void *)skerr, 0, sizeof(*skerr));
2462
2463 /* copy response into output buffer */
2464 max_bytes = (cdb[3] << 8) | cdb[4];
2465 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2466
2467 skcomp->num_returned_bytes =
Bart Van Assche4854afe2017-08-17 13:12:59 -07002468 cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq)));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002469}
2470
2471static void skd_do_driver_inq(struct skd_device *skdev,
2472 volatile struct fit_completion_entry_v1 *skcomp,
2473 volatile struct fit_comp_error_info *skerr,
2474 uint8_t *cdb, uint8_t *buf)
2475{
2476 if (!buf)
2477 return;
2478 else if (cdb[0] != INQUIRY)
2479 return; /* Not an INQUIRY */
2480 else if ((cdb[1] & 1) == 0)
2481 return; /* EVPD not set */
2482 else if (cdb[2] == 0)
2483 /* Need to add driver's page to supported pages list */
2484 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2485 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2486 /* Caller requested driver's page */
2487 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2488}
2489
2490static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2491{
2492 if (!sg)
2493 return NULL;
2494 if (!sg_page(sg))
2495 return NULL;
2496 return sg_virt(sg);
2497}
2498
2499static void skd_process_scsi_inq(struct skd_device *skdev,
2500 volatile struct fit_completion_entry_v1
2501 *skcomp,
2502 volatile struct fit_comp_error_info *skerr,
2503 struct skd_special_context *skspcl)
2504{
2505 uint8_t *buf;
2506 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
2507 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
2508
2509 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2510 skspcl->req.sg_data_dir);
2511 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2512
2513 if (buf)
2514 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2515}
2516
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002517static int skd_isr_completion_posted(struct skd_device *skdev,
2518 int limit, int *enqueued)
2519{
2520 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2521 volatile struct fit_comp_error_info *skerr;
2522 u16 req_id;
2523 u32 req_slot;
2524 struct skd_request_context *skreq;
2525 u16 cmp_cntxt = 0;
2526 u8 cmp_status = 0;
2527 u8 cmp_cycle = 0;
2528 u32 cmp_bytes = 0;
2529 int rc = 0;
2530 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002531
Bart Van Assche760b48c2017-08-17 13:13:00 -07002532 lockdep_assert_held(&skdev->lock);
2533
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002534 for (;; ) {
2535 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2536
2537 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2538 cmp_cycle = skcmp->cycle;
2539 cmp_cntxt = skcmp->tag;
2540 cmp_status = skcmp->status;
2541 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2542
2543 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2544
Bart Van Asschef98806d2017-08-17 13:12:58 -07002545 dev_dbg(&skdev->pdev->dev,
2546 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
2547 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
2548 cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
2549 skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002550
2551 if (cmp_cycle != skdev->skcomp_cycle) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002552 dev_dbg(&skdev->pdev->dev, "end of completions\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002553 break;
2554 }
2555 /*
2556 * Update the completion queue head index and possibly
2557 * the completion cycle count. 8-bit wrap-around.
2558 */
2559 skdev->skcomp_ix++;
2560 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2561 skdev->skcomp_ix = 0;
2562 skdev->skcomp_cycle++;
2563 }
2564
2565 /*
2566 * The command context is a unique 32-bit ID. The low order
2567 * bits help locate the request. The request is usually a
2568 * r/w request (see skd_start() above) or a special request.
2569 */
2570 req_id = cmp_cntxt;
2571 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2572
2573 /* Is this other than a r/w request? */
2574 if (req_slot >= skdev->num_req_context) {
2575 /*
2576 * This is not a completion for a r/w request.
2577 */
2578 skd_complete_other(skdev, skcmp, skerr);
2579 continue;
2580 }
2581
2582 skreq = &skdev->skreq_table[req_slot];
2583
2584 /*
2585 * Make sure the request ID for the slot matches.
2586 */
2587 if (skreq->id != req_id) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002588 dev_dbg(&skdev->pdev->dev,
2589 "mismatch comp_id=0x%x req_id=0x%x\n", req_id,
2590 skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002591 {
2592 u16 new_id = cmp_cntxt;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002593 dev_err(&skdev->pdev->dev,
2594 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2595 req_id, skreq->id, new_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002596
2597 continue;
2598 }
2599 }
2600
2601 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2602
2603 if (skreq->state == SKD_REQ_STATE_ABORTED) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002604 dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n",
2605 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002606 /* a previously timed out command can
2607 * now be cleaned up */
2608 skd_release_skreq(skdev, skreq);
2609 continue;
2610 }
2611
2612 skreq->completion = *skcmp;
2613 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2614 skreq->err_info = *skerr;
2615 skd_log_check_status(skdev, cmp_status, skerr->key,
2616 skerr->code, skerr->qual,
2617 skerr->fruc);
2618 }
2619 /* Release DMA resources for the request. */
2620 if (skreq->n_sg > 0)
2621 skd_postop_sg_list(skdev, skreq);
2622
Jens Axboefcd37eb2013-11-01 10:14:56 -06002623 if (!skreq->req) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002624 dev_dbg(&skdev->pdev->dev,
2625 "NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
2626 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002627 } else {
2628 /*
2629 * Capture the outcome and post it back to the
2630 * native request.
2631 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002632 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002633 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002634 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002635 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002636 }
2637
2638 /*
2639 * Release the skreq, its FIT msg (if one), timeout slot,
2640 * and queue depth.
2641 */
2642 skd_release_skreq(skdev, skreq);
2643
2644 /* skd_isr_comp_limit equal zero means no limit */
2645 if (limit) {
2646 if (++processed >= limit) {
2647 rc = 1;
2648 break;
2649 }
2650 }
2651 }
2652
2653 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2654 && (skdev->in_flight) == 0) {
2655 skdev->state = SKD_DRVR_STATE_PAUSED;
2656 wake_up_interruptible(&skdev->waitq);
2657 }
2658
2659 return rc;
2660}
2661
2662static void skd_complete_other(struct skd_device *skdev,
2663 volatile struct fit_completion_entry_v1 *skcomp,
2664 volatile struct fit_comp_error_info *skerr)
2665{
2666 u32 req_id = 0;
2667 u32 req_table;
2668 u32 req_slot;
2669 struct skd_special_context *skspcl;
2670
Bart Van Assche760b48c2017-08-17 13:13:00 -07002671 lockdep_assert_held(&skdev->lock);
2672
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002673 req_id = skcomp->tag;
2674 req_table = req_id & SKD_ID_TABLE_MASK;
2675 req_slot = req_id & SKD_ID_SLOT_MASK;
2676
Bart Van Asschef98806d2017-08-17 13:12:58 -07002677 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
2678 req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002679
2680 /*
2681 * Based on the request id, determine how to dispatch this completion.
2682 * This swich/case is finding the good cases and forwarding the
2683 * completion entry. Errors are reported below the switch.
2684 */
2685 switch (req_table) {
2686 case SKD_ID_RW_REQUEST:
2687 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07002688 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002689 * handles r/w requests. The only way we get here
2690 * is if the req_slot is out of bounds.
2691 */
2692 break;
2693
2694 case SKD_ID_SPECIAL_REQUEST:
2695 /*
2696 * Make sure the req_slot is in bounds and that the id
2697 * matches.
2698 */
2699 if (req_slot < skdev->n_special) {
2700 skspcl = &skdev->skspcl_table[req_slot];
2701 if (skspcl->req.id == req_id &&
2702 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2703 skd_complete_special(skdev,
2704 skcomp, skerr, skspcl);
2705 return;
2706 }
2707 }
2708 break;
2709
2710 case SKD_ID_INTERNAL:
2711 if (req_slot == 0) {
2712 skspcl = &skdev->internal_skspcl;
2713 if (skspcl->req.id == req_id &&
2714 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2715 skd_complete_internal(skdev,
2716 skcomp, skerr, skspcl);
2717 return;
2718 }
2719 }
2720 break;
2721
2722 case SKD_ID_FIT_MSG:
2723 /*
2724 * These id's should never appear in a completion record.
2725 */
2726 break;
2727
2728 default:
2729 /*
2730 * These id's should never appear anywhere;
2731 */
2732 break;
2733 }
2734
2735 /*
2736 * If we get here it is a bad or stale id.
2737 */
2738}
2739
2740static void skd_complete_special(struct skd_device *skdev,
2741 volatile struct fit_completion_entry_v1
2742 *skcomp,
2743 volatile struct fit_comp_error_info *skerr,
2744 struct skd_special_context *skspcl)
2745{
Bart Van Assche760b48c2017-08-17 13:13:00 -07002746 lockdep_assert_held(&skdev->lock);
2747
Bart Van Asschef98806d2017-08-17 13:12:58 -07002748 dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002749 if (skspcl->orphaned) {
2750 /* Discard orphaned request */
2751 /* ?: Can this release directly or does it need
2752 * to use a worker? */
Bart Van Asschef98806d2017-08-17 13:12:58 -07002753 dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002754 skd_release_special(skdev, skspcl);
2755 return;
2756 }
2757
2758 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2759
2760 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2761 skspcl->req.completion = *skcomp;
2762 skspcl->req.err_info = *skerr;
2763
2764 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2765 skerr->code, skerr->qual, skerr->fruc);
2766
2767 wake_up_interruptible(&skdev->waitq);
2768}
2769
2770/* assume spinlock is already held */
2771static void skd_release_special(struct skd_device *skdev,
2772 struct skd_special_context *skspcl)
2773{
2774 int i, was_depleted;
2775
2776 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002777 struct page *page = sg_page(&skspcl->req.sg[i]);
2778 __free_page(page);
2779 }
2780
2781 was_depleted = (skdev->skspcl_free_list == NULL);
2782
2783 skspcl->req.state = SKD_REQ_STATE_IDLE;
2784 skspcl->req.id += SKD_ID_INCR;
2785 skspcl->req.next =
2786 (struct skd_request_context *)skdev->skspcl_free_list;
2787 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2788
2789 if (was_depleted) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07002790 dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002791 /* Free list was depleted. Their might be waiters. */
2792 wake_up_interruptible(&skdev->waitq);
2793 }
2794}
2795
2796static void skd_reset_skcomp(struct skd_device *skdev)
2797{
Bart Van Assche6f7c7672017-08-17 13:13:02 -07002798 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002799
2800 skdev->skcomp_ix = 0;
2801 skdev->skcomp_cycle = 1;
2802}
2803
2804/*
2805 *****************************************************************************
2806 * INTERRUPTS
2807 *****************************************************************************
2808 */
2809static void skd_completion_worker(struct work_struct *work)
2810{
2811 struct skd_device *skdev =
2812 container_of(work, struct skd_device, completion_worker);
2813 unsigned long flags;
2814 int flush_enqueued = 0;
2815
2816 spin_lock_irqsave(&skdev->lock, flags);
2817
2818 /*
2819 * pass in limit=0, which means no limit..
2820 * process everything in compq
2821 */
2822 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2823 skd_request_fn(skdev->queue);
2824
2825 spin_unlock_irqrestore(&skdev->lock, flags);
2826}
2827
2828static void skd_isr_msg_from_dev(struct skd_device *skdev);
2829
Arnd Bergmann41c94992016-11-09 13:55:35 +01002830static irqreturn_t
2831skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002832{
2833 struct skd_device *skdev;
2834 u32 intstat;
2835 u32 ack;
2836 int rc = 0;
2837 int deferred = 0;
2838 int flush_enqueued = 0;
2839
2840 skdev = (struct skd_device *)ptr;
2841 spin_lock(&skdev->lock);
2842
2843 for (;; ) {
2844 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2845
2846 ack = FIT_INT_DEF_MASK;
2847 ack &= intstat;
2848
Bart Van Asschef98806d2017-08-17 13:12:58 -07002849 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
2850 ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002851
2852 /* As long as there is an int pending on device, keep
2853 * running loop. When none, get out, but if we've never
2854 * done any processing, call completion handler?
2855 */
2856 if (ack == 0) {
2857 /* No interrupts on device, but run the completion
2858 * processor anyway?
2859 */
2860 if (rc == 0)
2861 if (likely (skdev->state
2862 == SKD_DRVR_STATE_ONLINE))
2863 deferred = 1;
2864 break;
2865 }
2866
2867 rc = IRQ_HANDLED;
2868
2869 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2870
2871 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2872 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2873 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2874 /*
2875 * If we have already deferred completion
2876 * processing, don't bother running it again
2877 */
2878 if (deferred == 0)
2879 deferred =
2880 skd_isr_completion_posted(skdev,
2881 skd_isr_comp_limit, &flush_enqueued);
2882 }
2883
2884 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2885 skd_isr_fwstate(skdev);
2886 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2887 skdev->state ==
2888 SKD_DRVR_STATE_DISAPPEARED) {
2889 spin_unlock(&skdev->lock);
2890 return rc;
2891 }
2892 }
2893
2894 if (intstat & FIT_ISH_MSG_FROM_DEV)
2895 skd_isr_msg_from_dev(skdev);
2896 }
2897 }
2898
2899 if (unlikely(flush_enqueued))
2900 skd_request_fn(skdev->queue);
2901
2902 if (deferred)
2903 schedule_work(&skdev->completion_worker);
2904 else if (!flush_enqueued)
2905 skd_request_fn(skdev->queue);
2906
2907 spin_unlock(&skdev->lock);
2908
2909 return rc;
2910}
2911
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002912static void skd_drive_fault(struct skd_device *skdev)
2913{
2914 skdev->state = SKD_DRVR_STATE_FAULT;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002915 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002916}
2917
2918static void skd_drive_disappeared(struct skd_device *skdev)
2919{
2920 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002921 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002922}
2923
2924static void skd_isr_fwstate(struct skd_device *skdev)
2925{
2926 u32 sense;
2927 u32 state;
2928 u32 mtd;
2929 int prev_driver_state = skdev->state;
2930
2931 sense = SKD_READL(skdev, FIT_STATUS);
2932 state = sense & FIT_SR_DRIVE_STATE_MASK;
2933
Bart Van Asschef98806d2017-08-17 13:12:58 -07002934 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
2935 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
2936 skd_drive_state_to_str(state), state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002937
2938 skdev->drive_state = state;
2939
2940 switch (skdev->drive_state) {
2941 case FIT_SR_DRIVE_INIT:
2942 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
2943 skd_disable_interrupts(skdev);
2944 break;
2945 }
2946 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
2947 skd_recover_requests(skdev, 0);
2948 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
2949 skdev->timer_countdown = SKD_STARTING_TIMO;
2950 skdev->state = SKD_DRVR_STATE_STARTING;
2951 skd_soft_reset(skdev);
2952 break;
2953 }
2954 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
2955 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
2956 skdev->last_mtd = mtd;
2957 break;
2958
2959 case FIT_SR_DRIVE_ONLINE:
2960 skdev->cur_max_queue_depth = skd_max_queue_depth;
2961 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
2962 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
2963
2964 skdev->queue_low_water_mark =
2965 skdev->cur_max_queue_depth * 2 / 3 + 1;
2966 if (skdev->queue_low_water_mark < 1)
2967 skdev->queue_low_water_mark = 1;
Bart Van Asschef98806d2017-08-17 13:12:58 -07002968 dev_info(&skdev->pdev->dev,
2969 "Queue depth limit=%d dev=%d lowat=%d\n",
2970 skdev->cur_max_queue_depth,
2971 skdev->dev_max_queue_depth,
2972 skdev->queue_low_water_mark);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002973
2974 skd_refresh_device_data(skdev);
2975 break;
2976
2977 case FIT_SR_DRIVE_BUSY:
2978 skdev->state = SKD_DRVR_STATE_BUSY;
2979 skdev->timer_countdown = SKD_BUSY_TIMO;
2980 skd_quiesce_dev(skdev);
2981 break;
2982 case FIT_SR_DRIVE_BUSY_SANITIZE:
2983 /* set timer for 3 seconds, we'll abort any unfinished
2984 * commands after that expires
2985 */
2986 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2987 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06002988 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002989 break;
2990 case FIT_SR_DRIVE_BUSY_ERASE:
2991 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2992 skdev->timer_countdown = SKD_BUSY_TIMO;
2993 break;
2994 case FIT_SR_DRIVE_OFFLINE:
2995 skdev->state = SKD_DRVR_STATE_IDLE;
2996 break;
2997 case FIT_SR_DRIVE_SOFT_RESET:
2998 switch (skdev->state) {
2999 case SKD_DRVR_STATE_STARTING:
3000 case SKD_DRVR_STATE_RESTARTING:
3001 /* Expected by a caller of skd_soft_reset() */
3002 break;
3003 default:
3004 skdev->state = SKD_DRVR_STATE_RESTARTING;
3005 break;
3006 }
3007 break;
3008 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003009 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003010 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3011 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3012 break;
3013
3014 case FIT_SR_DRIVE_DEGRADED:
3015 case FIT_SR_PCIE_LINK_DOWN:
3016 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3017 break;
3018
3019 case FIT_SR_DRIVE_FAULT:
3020 skd_drive_fault(skdev);
3021 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003022 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003023 break;
3024
3025 /* PCIe bus returned all Fs? */
3026 case 0xFF:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003027 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
3028 sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003029 skd_drive_disappeared(skdev);
3030 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003031 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003032 break;
3033 default:
3034 /*
3035 * Uknown FW State. Wait for a state we recognize.
3036 */
3037 break;
3038 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07003039 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3040 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3041 skd_skdev_state_to_str(skdev->state), skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003042}
3043
3044static void skd_recover_requests(struct skd_device *skdev, int requeue)
3045{
3046 int i;
3047
3048 for (i = 0; i < skdev->num_req_context; i++) {
3049 struct skd_request_context *skreq = &skdev->skreq_table[i];
3050
3051 if (skreq->state == SKD_REQ_STATE_BUSY) {
3052 skd_log_skreq(skdev, skreq, "recover");
3053
3054 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003055 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003056
3057 /* Release DMA resources for the request. */
3058 if (skreq->n_sg > 0)
3059 skd_postop_sg_list(skdev, skreq);
3060
Jens Axboefcd37eb2013-11-01 10:14:56 -06003061 if (requeue &&
3062 (unsigned long) ++skreq->req->special <
3063 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003064 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003065 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003066 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003067
Jens Axboefcd37eb2013-11-01 10:14:56 -06003068 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003069
3070 skreq->state = SKD_REQ_STATE_IDLE;
3071 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003072 }
3073 if (i > 0)
3074 skreq[-1].next = skreq;
3075 skreq->next = NULL;
3076 }
3077 skdev->skreq_free_list = skdev->skreq_table;
3078
3079 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3080 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3081
3082 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3083 skd_log_skmsg(skdev, skmsg, "salvaged");
3084 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3085 skmsg->state = SKD_MSG_STATE_IDLE;
3086 skmsg->id += SKD_ID_INCR;
3087 }
3088 if (i > 0)
3089 skmsg[-1].next = skmsg;
3090 skmsg->next = NULL;
3091 }
3092 skdev->skmsg_free_list = skdev->skmsg_table;
3093
3094 for (i = 0; i < skdev->n_special; i++) {
3095 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3096
3097 /* If orphaned, reclaim it because it has already been reported
3098 * to the process as an error (it was just waiting for
3099 * a completion that didn't come, and now it will never come)
3100 * If busy, change to a state that will cause it to error
3101 * out in the wait routine and let it do the normal
3102 * reporting and reclaiming
3103 */
3104 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3105 if (skspcl->orphaned) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003106 dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
3107 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003108 skd_release_special(skdev, skspcl);
3109 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003110 dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
3111 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003112 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3113 }
3114 }
3115 }
3116 skdev->skspcl_free_list = skdev->skspcl_table;
3117
3118 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3119 skdev->timeout_slot[i] = 0;
3120
3121 skdev->in_flight = 0;
3122}
3123
3124static void skd_isr_msg_from_dev(struct skd_device *skdev)
3125{
3126 u32 mfd;
3127 u32 mtd;
3128 u32 data;
3129
3130 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3131
Bart Van Asschef98806d2017-08-17 13:12:58 -07003132 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
3133 skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003134
3135 /* ignore any mtd that is an ack for something we didn't send */
3136 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3137 return;
3138
3139 switch (FIT_MXD_TYPE(mfd)) {
3140 case FIT_MTD_FITFW_INIT:
3141 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3142
3143 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003144 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
3145 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
3146 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
3147 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003148 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3149 skd_soft_reset(skdev);
3150 break;
3151 }
3152 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3153 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3154 skdev->last_mtd = mtd;
3155 break;
3156
3157 case FIT_MTD_GET_CMDQ_DEPTH:
3158 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3159 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3160 SKD_N_COMPLETION_ENTRY);
3161 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3162 skdev->last_mtd = mtd;
3163 break;
3164
3165 case FIT_MTD_SET_COMPQ_DEPTH:
3166 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3167 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3168 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3169 skdev->last_mtd = mtd;
3170 break;
3171
3172 case FIT_MTD_SET_COMPQ_ADDR:
3173 skd_reset_skcomp(skdev);
3174 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3175 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3176 skdev->last_mtd = mtd;
3177 break;
3178
3179 case FIT_MTD_CMD_LOG_HOST_ID:
3180 skdev->connect_time_stamp = get_seconds();
3181 data = skdev->connect_time_stamp & 0xFFFF;
3182 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3183 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3184 skdev->last_mtd = mtd;
3185 break;
3186
3187 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3188 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3189 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3190 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3191 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3192 skdev->last_mtd = mtd;
3193 break;
3194
3195 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3196 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3197 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3198 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3199 skdev->last_mtd = mtd;
3200
Bart Van Asschef98806d2017-08-17 13:12:58 -07003201 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
3202 skdev->connect_time_stamp, skdev->drive_jiffies);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003203 break;
3204
3205 case FIT_MTD_ARM_QUEUE:
3206 skdev->last_mtd = 0;
3207 /*
3208 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3209 */
3210 break;
3211
3212 default:
3213 break;
3214 }
3215}
3216
3217static void skd_disable_interrupts(struct skd_device *skdev)
3218{
3219 u32 sense;
3220
3221 sense = SKD_READL(skdev, FIT_CONTROL);
3222 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3223 SKD_WRITEL(skdev, sense, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003224 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003225
3226 /* Note that the 1s is written. A 1-bit means
3227 * disable, a 0 means enable.
3228 */
3229 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3230}
3231
3232static void skd_enable_interrupts(struct skd_device *skdev)
3233{
3234 u32 val;
3235
3236 /* unmask interrupts first */
3237 val = FIT_ISH_FW_STATE_CHANGE +
3238 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3239
3240 /* Note that the compliment of mask is written. A 1-bit means
3241 * disable, a 0 means enable. */
3242 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003243 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003244
3245 val = SKD_READL(skdev, FIT_CONTROL);
3246 val |= FIT_CR_ENABLE_INTERRUPTS;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003247 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003248 SKD_WRITEL(skdev, val, FIT_CONTROL);
3249}
3250
3251/*
3252 *****************************************************************************
3253 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3254 *****************************************************************************
3255 */
3256
3257static void skd_soft_reset(struct skd_device *skdev)
3258{
3259 u32 val;
3260
3261 val = SKD_READL(skdev, FIT_CONTROL);
3262 val |= (FIT_CR_SOFT_RESET);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003263 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003264 SKD_WRITEL(skdev, val, FIT_CONTROL);
3265}
3266
3267static void skd_start_device(struct skd_device *skdev)
3268{
3269 unsigned long flags;
3270 u32 sense;
3271 u32 state;
3272
3273 spin_lock_irqsave(&skdev->lock, flags);
3274
3275 /* ack all ghost interrupts */
3276 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3277
3278 sense = SKD_READL(skdev, FIT_STATUS);
3279
Bart Van Asschef98806d2017-08-17 13:12:58 -07003280 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003281
3282 state = sense & FIT_SR_DRIVE_STATE_MASK;
3283 skdev->drive_state = state;
3284 skdev->last_mtd = 0;
3285
3286 skdev->state = SKD_DRVR_STATE_STARTING;
3287 skdev->timer_countdown = SKD_STARTING_TIMO;
3288
3289 skd_enable_interrupts(skdev);
3290
3291 switch (skdev->drive_state) {
3292 case FIT_SR_DRIVE_OFFLINE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003293 dev_err(&skdev->pdev->dev, "Drive offline...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003294 break;
3295
3296 case FIT_SR_DRIVE_FW_BOOTING:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003297 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003298 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3299 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3300 break;
3301
3302 case FIT_SR_DRIVE_BUSY_SANITIZE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003303 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003304 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3305 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3306 break;
3307
3308 case FIT_SR_DRIVE_BUSY_ERASE:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003309 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003310 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3311 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3312 break;
3313
3314 case FIT_SR_DRIVE_INIT:
3315 case FIT_SR_DRIVE_ONLINE:
3316 skd_soft_reset(skdev);
3317 break;
3318
3319 case FIT_SR_DRIVE_BUSY:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003320 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003321 skdev->state = SKD_DRVR_STATE_BUSY;
3322 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3323 break;
3324
3325 case FIT_SR_DRIVE_SOFT_RESET:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003326 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003327 break;
3328
3329 case FIT_SR_DRIVE_FAULT:
3330 /* Fault state is bad...soft reset won't do it...
3331 * Hard reset, maybe, but does it work on device?
3332 * For now, just fault so the system doesn't hang.
3333 */
3334 skd_drive_fault(skdev);
3335 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003336 dev_dbg(&skdev->pdev->dev, "starting queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003337 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003338 skdev->gendisk_on = -1;
3339 wake_up_interruptible(&skdev->waitq);
3340 break;
3341
3342 case 0xFF:
3343 /* Most likely the device isn't there or isn't responding
3344 * to the BAR1 addresses. */
3345 skd_drive_disappeared(skdev);
3346 /*start the queue so we can respond with error to requests */
Bart Van Asschef98806d2017-08-17 13:12:58 -07003347 dev_dbg(&skdev->pdev->dev,
3348 "starting queue to error-out reqs\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003349 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003350 skdev->gendisk_on = -1;
3351 wake_up_interruptible(&skdev->waitq);
3352 break;
3353
3354 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003355 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
3356 skdev->drive_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003357 break;
3358 }
3359
3360 state = SKD_READL(skdev, FIT_CONTROL);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003361 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003362
3363 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003364 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003365
3366 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003367 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003368
3369 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003370 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003371
3372 state = SKD_READL(skdev, FIT_HW_VERSION);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003373 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003374
3375 spin_unlock_irqrestore(&skdev->lock, flags);
3376}
3377
3378static void skd_stop_device(struct skd_device *skdev)
3379{
3380 unsigned long flags;
3381 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3382 u32 dev_state;
3383 int i;
3384
3385 spin_lock_irqsave(&skdev->lock, flags);
3386
3387 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003388 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003389 goto stop_out;
3390 }
3391
3392 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003393 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003394 goto stop_out;
3395 }
3396
3397 skdev->state = SKD_DRVR_STATE_SYNCING;
3398 skdev->sync_done = 0;
3399
3400 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3401
3402 spin_unlock_irqrestore(&skdev->lock, flags);
3403
3404 wait_event_interruptible_timeout(skdev->waitq,
3405 (skdev->sync_done), (10 * HZ));
3406
3407 spin_lock_irqsave(&skdev->lock, flags);
3408
3409 switch (skdev->sync_done) {
3410 case 0:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003411 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003412 break;
3413 case 1:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003414 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003415 break;
3416 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003417 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003418 }
3419
3420stop_out:
3421 skdev->state = SKD_DRVR_STATE_STOPPING;
3422 spin_unlock_irqrestore(&skdev->lock, flags);
3423
3424 skd_kill_timer(skdev);
3425
3426 spin_lock_irqsave(&skdev->lock, flags);
3427 skd_disable_interrupts(skdev);
3428
3429 /* ensure all ints on device are cleared */
3430 /* soft reset the device to unload with a clean slate */
3431 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3432 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3433
3434 spin_unlock_irqrestore(&skdev->lock, flags);
3435
3436 /* poll every 100ms, 1 second timeout */
3437 for (i = 0; i < 10; i++) {
3438 dev_state =
3439 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3440 if (dev_state == FIT_SR_DRIVE_INIT)
3441 break;
3442 set_current_state(TASK_INTERRUPTIBLE);
3443 schedule_timeout(msecs_to_jiffies(100));
3444 }
3445
3446 if (dev_state != FIT_SR_DRIVE_INIT)
Bart Van Asschef98806d2017-08-17 13:12:58 -07003447 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
3448 dev_state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003449}
3450
3451/* assume spinlock is held */
3452static void skd_restart_device(struct skd_device *skdev)
3453{
3454 u32 state;
3455
3456 /* ack all ghost interrupts */
3457 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3458
3459 state = SKD_READL(skdev, FIT_STATUS);
3460
Bart Van Asschef98806d2017-08-17 13:12:58 -07003461 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003462
3463 state &= FIT_SR_DRIVE_STATE_MASK;
3464 skdev->drive_state = state;
3465 skdev->last_mtd = 0;
3466
3467 skdev->state = SKD_DRVR_STATE_RESTARTING;
3468 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3469
3470 skd_soft_reset(skdev);
3471}
3472
3473/* assume spinlock is held */
3474static int skd_quiesce_dev(struct skd_device *skdev)
3475{
3476 int rc = 0;
3477
3478 switch (skdev->state) {
3479 case SKD_DRVR_STATE_BUSY:
3480 case SKD_DRVR_STATE_BUSY_IMMINENT:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003481 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003482 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003483 break;
3484 case SKD_DRVR_STATE_ONLINE:
3485 case SKD_DRVR_STATE_STOPPING:
3486 case SKD_DRVR_STATE_SYNCING:
3487 case SKD_DRVR_STATE_PAUSING:
3488 case SKD_DRVR_STATE_PAUSED:
3489 case SKD_DRVR_STATE_STARTING:
3490 case SKD_DRVR_STATE_RESTARTING:
3491 case SKD_DRVR_STATE_RESUMING:
3492 default:
3493 rc = -EINVAL;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003494 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
3495 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003496 }
3497 return rc;
3498}
3499
3500/* assume spinlock is held */
3501static int skd_unquiesce_dev(struct skd_device *skdev)
3502{
3503 int prev_driver_state = skdev->state;
3504
3505 skd_log_skdev(skdev, "unquiesce");
3506 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003507 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003508 return 0;
3509 }
3510 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3511 /*
3512 * If there has been an state change to other than
3513 * ONLINE, we will rely on controller state change
3514 * to come back online and restart the queue.
3515 * The BUSY state means that driver is ready to
3516 * continue normal processing but waiting for controller
3517 * to become available.
3518 */
3519 skdev->state = SKD_DRVR_STATE_BUSY;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003520 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003521 return 0;
3522 }
3523
3524 /*
3525 * Drive has just come online, driver is either in startup,
3526 * paused performing a task, or bust waiting for hardware.
3527 */
3528 switch (skdev->state) {
3529 case SKD_DRVR_STATE_PAUSED:
3530 case SKD_DRVR_STATE_BUSY:
3531 case SKD_DRVR_STATE_BUSY_IMMINENT:
3532 case SKD_DRVR_STATE_BUSY_ERASE:
3533 case SKD_DRVR_STATE_STARTING:
3534 case SKD_DRVR_STATE_RESTARTING:
3535 case SKD_DRVR_STATE_FAULT:
3536 case SKD_DRVR_STATE_IDLE:
3537 case SKD_DRVR_STATE_LOAD:
3538 skdev->state = SKD_DRVR_STATE_ONLINE;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003539 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
3540 skd_skdev_state_to_str(prev_driver_state),
3541 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3542 skdev->state);
3543 dev_dbg(&skdev->pdev->dev,
3544 "**** device ONLINE...starting block queue\n");
3545 dev_dbg(&skdev->pdev->dev, "starting queue\n");
3546 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06003547 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003548 skdev->gendisk_on = 1;
3549 wake_up_interruptible(&skdev->waitq);
3550 break;
3551
3552 case SKD_DRVR_STATE_DISAPPEARED:
3553 default:
Bart Van Asschef98806d2017-08-17 13:12:58 -07003554 dev_dbg(&skdev->pdev->dev,
3555 "**** driver state %d, not implemented\n",
3556 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003557 return -EBUSY;
3558 }
3559 return 0;
3560}
3561
3562/*
3563 *****************************************************************************
3564 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3565 *****************************************************************************
3566 */
3567
3568static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3569{
3570 struct skd_device *skdev = skd_host_data;
3571 unsigned long flags;
3572
3573 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003574 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3575 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3576 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
3577 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003578 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3579 spin_unlock_irqrestore(&skdev->lock, flags);
3580 return IRQ_HANDLED;
3581}
3582
3583static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3584{
3585 struct skd_device *skdev = skd_host_data;
3586 unsigned long flags;
3587
3588 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003589 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3590 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003591 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3592 skd_isr_fwstate(skdev);
3593 spin_unlock_irqrestore(&skdev->lock, flags);
3594 return IRQ_HANDLED;
3595}
3596
3597static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3598{
3599 struct skd_device *skdev = skd_host_data;
3600 unsigned long flags;
3601 int flush_enqueued = 0;
3602 int deferred;
3603
3604 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003605 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3606 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003607 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3608 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3609 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003610 if (flush_enqueued)
3611 skd_request_fn(skdev->queue);
3612
3613 if (deferred)
3614 schedule_work(&skdev->completion_worker);
3615 else if (!flush_enqueued)
3616 skd_request_fn(skdev->queue);
3617
3618 spin_unlock_irqrestore(&skdev->lock, flags);
3619
3620 return IRQ_HANDLED;
3621}
3622
3623static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3624{
3625 struct skd_device *skdev = skd_host_data;
3626 unsigned long flags;
3627
3628 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003629 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3630 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003631 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3632 skd_isr_msg_from_dev(skdev);
3633 spin_unlock_irqrestore(&skdev->lock, flags);
3634 return IRQ_HANDLED;
3635}
3636
3637static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3638{
3639 struct skd_device *skdev = skd_host_data;
3640 unsigned long flags;
3641
3642 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003643 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
3644 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003645 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3646 spin_unlock_irqrestore(&skdev->lock, flags);
3647 return IRQ_HANDLED;
3648}
3649
3650/*
3651 *****************************************************************************
3652 * PCIe MSI/MSI-X SETUP
3653 *****************************************************************************
3654 */
3655
3656struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003657 char isr_name[30];
3658};
3659
3660struct skd_init_msix_entry {
3661 const char *name;
3662 irq_handler_t handler;
3663};
3664
3665#define SKD_MAX_MSIX_COUNT 13
3666#define SKD_MIN_MSIX_COUNT 7
3667#define SKD_BASE_MSIX_IRQ 4
3668
3669static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3670 { "(DMA 0)", skd_reserved_isr },
3671 { "(DMA 1)", skd_reserved_isr },
3672 { "(DMA 2)", skd_reserved_isr },
3673 { "(DMA 3)", skd_reserved_isr },
3674 { "(State Change)", skd_statec_isr },
3675 { "(COMPL_Q)", skd_comp_q },
3676 { "(MSG)", skd_msg_isr },
3677 { "(Reserved)", skd_reserved_isr },
3678 { "(Reserved)", skd_reserved_isr },
3679 { "(Queue Full 0)", skd_qfull_isr },
3680 { "(Queue Full 1)", skd_qfull_isr },
3681 { "(Queue Full 2)", skd_qfull_isr },
3682 { "(Queue Full 3)", skd_qfull_isr },
3683};
3684
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003685static int skd_acquire_msix(struct skd_device *skdev)
3686{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003687 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003688 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003689
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003690 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3691 PCI_IRQ_MSIX);
3692 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003693 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003694 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003695 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003696
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003697 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3698 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003699 if (!skdev->msix_entries) {
3700 rc = -ENOMEM;
Bart Van Asschef98806d2017-08-17 13:12:58 -07003701 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003702 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003703 }
3704
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003705 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003706 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3707 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3708
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003709 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3710 "%s%d-msix %s", DRV_NAME, skdev->devno,
3711 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003712
3713 rc = devm_request_irq(&skdev->pdev->dev,
3714 pci_irq_vector(skdev->pdev, i),
3715 msix_entries[i].handler, 0,
3716 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003717 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003718 dev_err(&skdev->pdev->dev,
3719 "Unable to register(%d) MSI-X handler %d: %s\n",
3720 rc, i, qentry->isr_name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003721 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003722 }
3723 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003724
Bart Van Asschef98806d2017-08-17 13:12:58 -07003725 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
3726 SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003727 return 0;
3728
3729msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003730 while (--i >= 0)
3731 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003732out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003733 kfree(skdev->msix_entries);
3734 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003735 return rc;
3736}
3737
3738static int skd_acquire_irq(struct skd_device *skdev)
3739{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003740 struct pci_dev *pdev = skdev->pdev;
3741 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003742 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003743
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003744 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003745 rc = skd_acquire_msix(skdev);
3746 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003747 return 0;
3748
Bart Van Asschef98806d2017-08-17 13:12:58 -07003749 dev_err(&skdev->pdev->dev,
3750 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003751 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003752
3753 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3754 skdev->devno);
3755
3756 if (skd_isr_type != SKD_IRQ_LEGACY)
3757 irq_flag |= PCI_IRQ_MSI;
3758 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3759 if (rc < 0) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07003760 dev_err(&skdev->pdev->dev,
3761 "failed to allocate the MSI interrupt %d\n", rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003762 return rc;
3763 }
3764
3765 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3766 pdev->msi_enabled ? 0 : IRQF_SHARED,
3767 skdev->isr_name, skdev);
3768 if (rc) {
3769 pci_free_irq_vectors(pdev);
Bart Van Asschef98806d2017-08-17 13:12:58 -07003770 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
3771 rc);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003772 return rc;
3773 }
3774
3775 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003776}
3777
3778static void skd_release_irq(struct skd_device *skdev)
3779{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003780 struct pci_dev *pdev = skdev->pdev;
3781
3782 if (skdev->msix_entries) {
3783 int i;
3784
3785 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3786 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3787 skdev);
3788 }
3789
3790 kfree(skdev->msix_entries);
3791 skdev->msix_entries = NULL;
3792 } else {
3793 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003794 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003795
3796 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003797}
3798
3799/*
3800 *****************************************************************************
3801 * CONSTRUCT
3802 *****************************************************************************
3803 */
3804
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003805static int skd_cons_skcomp(struct skd_device *skdev)
3806{
3807 int rc = 0;
3808 struct fit_completion_entry_v1 *skcomp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003809
Bart Van Asschef98806d2017-08-17 13:12:58 -07003810 dev_dbg(&skdev->pdev->dev,
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003811 "comp pci_alloc, total bytes %zd entries %d\n",
3812 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003813
Bart Van Assche6f7c7672017-08-17 13:13:02 -07003814 skcomp = pci_zalloc_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Joe Perchesa5bbf612014-08-08 14:24:12 -07003815 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003816
3817 if (skcomp == NULL) {
3818 rc = -ENOMEM;
3819 goto err_out;
3820 }
3821
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003822 skdev->skcomp_table = skcomp;
3823 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3824 sizeof(*skcomp) *
3825 SKD_N_COMPLETION_ENTRY);
3826
3827err_out:
3828 return rc;
3829}
3830
3831static int skd_cons_skmsg(struct skd_device *skdev)
3832{
3833 int rc = 0;
3834 u32 i;
3835
Bart Van Asschef98806d2017-08-17 13:12:58 -07003836 dev_dbg(&skdev->pdev->dev,
3837 "skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3838 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
3839 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003840
3841 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3842 *skdev->num_fitmsg_context, GFP_KERNEL);
3843 if (skdev->skmsg_table == NULL) {
3844 rc = -ENOMEM;
3845 goto err_out;
3846 }
3847
3848 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3849 struct skd_fitmsg_context *skmsg;
3850
3851 skmsg = &skdev->skmsg_table[i];
3852
3853 skmsg->id = i + SKD_ID_FIT_MSG;
3854
3855 skmsg->state = SKD_MSG_STATE_IDLE;
3856 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
3857 SKD_N_FITMSG_BYTES + 64,
3858 &skmsg->mb_dma_address);
3859
3860 if (skmsg->msg_buf == NULL) {
3861 rc = -ENOMEM;
3862 goto err_out;
3863 }
3864
3865 skmsg->offset = (u32)((u64)skmsg->msg_buf &
3866 (~FIT_QCMD_BASE_ADDRESS_MASK));
3867 skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK;
3868 skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf &
3869 FIT_QCMD_BASE_ADDRESS_MASK);
3870 skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK;
3871 skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK;
3872 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3873
3874 skmsg->next = &skmsg[1];
3875 }
3876
3877 /* Free list is in order starting with the 0th entry. */
3878 skdev->skmsg_table[i - 1].next = NULL;
3879 skdev->skmsg_free_list = skdev->skmsg_table;
3880
3881err_out:
3882 return rc;
3883}
3884
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01003885static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
3886 u32 n_sg,
3887 dma_addr_t *ret_dma_addr)
3888{
3889 struct fit_sg_descriptor *sg_list;
3890 u32 nbytes;
3891
3892 nbytes = sizeof(*sg_list) * n_sg;
3893
3894 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
3895
3896 if (sg_list != NULL) {
3897 uint64_t dma_address = *ret_dma_addr;
3898 u32 i;
3899
3900 memset(sg_list, 0, nbytes);
3901
3902 for (i = 0; i < n_sg - 1; i++) {
3903 uint64_t ndp_off;
3904 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
3905
3906 sg_list[i].next_desc_ptr = dma_address + ndp_off;
3907 }
3908 sg_list[i].next_desc_ptr = 0LL;
3909 }
3910
3911 return sg_list;
3912}
3913
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003914static int skd_cons_skreq(struct skd_device *skdev)
3915{
3916 int rc = 0;
3917 u32 i;
3918
Bart Van Asschef98806d2017-08-17 13:12:58 -07003919 dev_dbg(&skdev->pdev->dev,
3920 "skreq_table kzalloc, struct %lu, count %u total %lu\n",
3921 sizeof(struct skd_request_context), skdev->num_req_context,
3922 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003923
3924 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
3925 * skdev->num_req_context, GFP_KERNEL);
3926 if (skdev->skreq_table == NULL) {
3927 rc = -ENOMEM;
3928 goto err_out;
3929 }
3930
Bart Van Asschef98806d2017-08-17 13:12:58 -07003931 dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
3932 skdev->sgs_per_request, sizeof(struct scatterlist),
3933 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003934
3935 for (i = 0; i < skdev->num_req_context; i++) {
3936 struct skd_request_context *skreq;
3937
3938 skreq = &skdev->skreq_table[i];
3939
3940 skreq->id = i + SKD_ID_RW_REQUEST;
3941 skreq->state = SKD_REQ_STATE_IDLE;
3942
3943 skreq->sg = kzalloc(sizeof(struct scatterlist) *
3944 skdev->sgs_per_request, GFP_KERNEL);
3945 if (skreq->sg == NULL) {
3946 rc = -ENOMEM;
3947 goto err_out;
3948 }
3949 sg_init_table(skreq->sg, skdev->sgs_per_request);
3950
3951 skreq->sksg_list = skd_cons_sg_list(skdev,
3952 skdev->sgs_per_request,
3953 &skreq->sksg_dma_address);
3954
3955 if (skreq->sksg_list == NULL) {
3956 rc = -ENOMEM;
3957 goto err_out;
3958 }
3959
3960 skreq->next = &skreq[1];
3961 }
3962
3963 /* Free list is in order starting with the 0th entry. */
3964 skdev->skreq_table[i - 1].next = NULL;
3965 skdev->skreq_free_list = skdev->skreq_table;
3966
3967err_out:
3968 return rc;
3969}
3970
3971static int skd_cons_skspcl(struct skd_device *skdev)
3972{
3973 int rc = 0;
3974 u32 i, nbytes;
3975
Bart Van Asschef98806d2017-08-17 13:12:58 -07003976 dev_dbg(&skdev->pdev->dev,
3977 "skspcl_table kzalloc, struct %lu, count %u total %lu\n",
3978 sizeof(struct skd_special_context), skdev->n_special,
3979 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003980
3981 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
3982 * skdev->n_special, GFP_KERNEL);
3983 if (skdev->skspcl_table == NULL) {
3984 rc = -ENOMEM;
3985 goto err_out;
3986 }
3987
3988 for (i = 0; i < skdev->n_special; i++) {
3989 struct skd_special_context *skspcl;
3990
3991 skspcl = &skdev->skspcl_table[i];
3992
3993 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
3994 skspcl->req.state = SKD_REQ_STATE_IDLE;
3995
3996 skspcl->req.next = &skspcl[1].req;
3997
3998 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
3999
Joe Perchesa5bbf612014-08-08 14:24:12 -07004000 skspcl->msg_buf =
4001 pci_zalloc_consistent(skdev->pdev, nbytes,
4002 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004003 if (skspcl->msg_buf == NULL) {
4004 rc = -ENOMEM;
4005 goto err_out;
4006 }
4007
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004008 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4009 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4010 if (skspcl->req.sg == NULL) {
4011 rc = -ENOMEM;
4012 goto err_out;
4013 }
4014
4015 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4016 SKD_N_SG_PER_SPECIAL,
4017 &skspcl->req.
4018 sksg_dma_address);
4019 if (skspcl->req.sksg_list == NULL) {
4020 rc = -ENOMEM;
4021 goto err_out;
4022 }
4023 }
4024
4025 /* Free list is in order starting with the 0th entry. */
4026 skdev->skspcl_table[i - 1].req.next = NULL;
4027 skdev->skspcl_free_list = skdev->skspcl_table;
4028
4029 return rc;
4030
4031err_out:
4032 return rc;
4033}
4034
4035static int skd_cons_sksb(struct skd_device *skdev)
4036{
4037 int rc = 0;
4038 struct skd_special_context *skspcl;
4039 u32 nbytes;
4040
4041 skspcl = &skdev->internal_skspcl;
4042
4043 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4044 skspcl->req.state = SKD_REQ_STATE_IDLE;
4045
4046 nbytes = SKD_N_INTERNAL_BYTES;
4047
Joe Perchesa5bbf612014-08-08 14:24:12 -07004048 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4049 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004050 if (skspcl->data_buf == NULL) {
4051 rc = -ENOMEM;
4052 goto err_out;
4053 }
4054
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004055 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004056 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4057 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004058 if (skspcl->msg_buf == NULL) {
4059 rc = -ENOMEM;
4060 goto err_out;
4061 }
4062
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004063 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4064 &skspcl->req.sksg_dma_address);
4065 if (skspcl->req.sksg_list == NULL) {
4066 rc = -ENOMEM;
4067 goto err_out;
4068 }
4069
4070 if (!skd_format_internal_skspcl(skdev)) {
4071 rc = -EINVAL;
4072 goto err_out;
4073 }
4074
4075err_out:
4076 return rc;
4077}
4078
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004079static int skd_cons_disk(struct skd_device *skdev)
4080{
4081 int rc = 0;
4082 struct gendisk *disk;
4083 struct request_queue *q;
4084 unsigned long flags;
4085
4086 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4087 if (!disk) {
4088 rc = -ENOMEM;
4089 goto err_out;
4090 }
4091
4092 skdev->disk = disk;
4093 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4094
4095 disk->major = skdev->major;
4096 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4097 disk->fops = &skd_blockdev_ops;
4098 disk->private_data = skdev;
4099
Jens Axboefcd37eb2013-11-01 10:14:56 -06004100 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004101 if (!q) {
4102 rc = -ENOMEM;
4103 goto err_out;
4104 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004105 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004106
4107 skdev->queue = q;
4108 disk->queue = q;
4109 q->queuedata = skdev;
4110
Jens Axboe6975f732016-03-30 10:11:42 -06004111 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004112 blk_queue_max_segments(q, skdev->sgs_per_request);
4113 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4114
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004115 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004116 blk_queue_io_opt(q, 8192);
4117
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004118 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004119 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004120
4121 spin_lock_irqsave(&skdev->lock, flags);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004122 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
Jens Axboe6a5ec652013-11-01 10:38:45 -06004123 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004124 spin_unlock_irqrestore(&skdev->lock, flags);
4125
4126err_out:
4127 return rc;
4128}
4129
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004130#define SKD_N_DEV_TABLE 16u
4131static u32 skd_next_devno;
4132
4133static struct skd_device *skd_construct(struct pci_dev *pdev)
4134{
4135 struct skd_device *skdev;
4136 int blk_major = skd_major;
4137 int rc;
4138
4139 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4140
4141 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004142 dev_err(&pdev->dev, "memory alloc failure\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004143 return NULL;
4144 }
4145
4146 skdev->state = SKD_DRVR_STATE_LOAD;
4147 skdev->pdev = pdev;
4148 skdev->devno = skd_next_devno++;
4149 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004150 skdev->dev_max_queue_depth = 0;
4151
4152 skdev->num_req_context = skd_max_queue_depth;
4153 skdev->num_fitmsg_context = skd_max_queue_depth;
4154 skdev->n_special = skd_max_pass_thru;
4155 skdev->cur_max_queue_depth = 1;
4156 skdev->queue_low_water_mark = 1;
4157 skdev->proto_ver = 99;
4158 skdev->sgs_per_request = skd_sgs_per_request;
4159 skdev->dbg_level = skd_dbg_level;
4160
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004161 spin_lock_init(&skdev->lock);
4162
4163 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4164
Bart Van Asschef98806d2017-08-17 13:12:58 -07004165 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004166 rc = skd_cons_skcomp(skdev);
4167 if (rc < 0)
4168 goto err_out;
4169
Bart Van Asschef98806d2017-08-17 13:12:58 -07004170 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004171 rc = skd_cons_skmsg(skdev);
4172 if (rc < 0)
4173 goto err_out;
4174
Bart Van Asschef98806d2017-08-17 13:12:58 -07004175 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004176 rc = skd_cons_skreq(skdev);
4177 if (rc < 0)
4178 goto err_out;
4179
Bart Van Asschef98806d2017-08-17 13:12:58 -07004180 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004181 rc = skd_cons_skspcl(skdev);
4182 if (rc < 0)
4183 goto err_out;
4184
Bart Van Asschef98806d2017-08-17 13:12:58 -07004185 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004186 rc = skd_cons_sksb(skdev);
4187 if (rc < 0)
4188 goto err_out;
4189
Bart Van Asschef98806d2017-08-17 13:12:58 -07004190 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004191 rc = skd_cons_disk(skdev);
4192 if (rc < 0)
4193 goto err_out;
4194
Bart Van Asschef98806d2017-08-17 13:12:58 -07004195 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004196 return skdev;
4197
4198err_out:
Bart Van Asschef98806d2017-08-17 13:12:58 -07004199 dev_dbg(&skdev->pdev->dev, "construct failed\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004200 skd_destruct(skdev);
4201 return NULL;
4202}
4203
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004204/*
4205 *****************************************************************************
4206 * DESTRUCT (FREE)
4207 *****************************************************************************
4208 */
4209
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004210static void skd_free_skcomp(struct skd_device *skdev)
4211{
Bart Van Assche7f13bda2017-08-17 13:13:03 -07004212 if (skdev->skcomp_table)
4213 pci_free_consistent(skdev->pdev, SKD_SKCOMP_SIZE,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004214 skdev->skcomp_table, skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004215
4216 skdev->skcomp_table = NULL;
4217 skdev->cq_dma_address = 0;
4218}
4219
4220static void skd_free_skmsg(struct skd_device *skdev)
4221{
4222 u32 i;
4223
4224 if (skdev->skmsg_table == NULL)
4225 return;
4226
4227 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4228 struct skd_fitmsg_context *skmsg;
4229
4230 skmsg = &skdev->skmsg_table[i];
4231
4232 if (skmsg->msg_buf != NULL) {
4233 skmsg->msg_buf += skmsg->offset;
4234 skmsg->mb_dma_address += skmsg->offset;
4235 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4236 skmsg->msg_buf,
4237 skmsg->mb_dma_address);
4238 }
4239 skmsg->msg_buf = NULL;
4240 skmsg->mb_dma_address = 0;
4241 }
4242
4243 kfree(skdev->skmsg_table);
4244 skdev->skmsg_table = NULL;
4245}
4246
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004247static void skd_free_sg_list(struct skd_device *skdev,
4248 struct fit_sg_descriptor *sg_list,
4249 u32 n_sg, dma_addr_t dma_addr)
4250{
4251 if (sg_list != NULL) {
4252 u32 nbytes;
4253
4254 nbytes = sizeof(*sg_list) * n_sg;
4255
4256 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4257 }
4258}
4259
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004260static void skd_free_skreq(struct skd_device *skdev)
4261{
4262 u32 i;
4263
4264 if (skdev->skreq_table == NULL)
4265 return;
4266
4267 for (i = 0; i < skdev->num_req_context; i++) {
4268 struct skd_request_context *skreq;
4269
4270 skreq = &skdev->skreq_table[i];
4271
4272 skd_free_sg_list(skdev, skreq->sksg_list,
4273 skdev->sgs_per_request,
4274 skreq->sksg_dma_address);
4275
4276 skreq->sksg_list = NULL;
4277 skreq->sksg_dma_address = 0;
4278
4279 kfree(skreq->sg);
4280 }
4281
4282 kfree(skdev->skreq_table);
4283 skdev->skreq_table = NULL;
4284}
4285
4286static void skd_free_skspcl(struct skd_device *skdev)
4287{
4288 u32 i;
4289 u32 nbytes;
4290
4291 if (skdev->skspcl_table == NULL)
4292 return;
4293
4294 for (i = 0; i < skdev->n_special; i++) {
4295 struct skd_special_context *skspcl;
4296
4297 skspcl = &skdev->skspcl_table[i];
4298
4299 if (skspcl->msg_buf != NULL) {
4300 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4301 pci_free_consistent(skdev->pdev, nbytes,
4302 skspcl->msg_buf,
4303 skspcl->mb_dma_address);
4304 }
4305
4306 skspcl->msg_buf = NULL;
4307 skspcl->mb_dma_address = 0;
4308
4309 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4310 SKD_N_SG_PER_SPECIAL,
4311 skspcl->req.sksg_dma_address);
4312
4313 skspcl->req.sksg_list = NULL;
4314 skspcl->req.sksg_dma_address = 0;
4315
4316 kfree(skspcl->req.sg);
4317 }
4318
4319 kfree(skdev->skspcl_table);
4320 skdev->skspcl_table = NULL;
4321}
4322
4323static void skd_free_sksb(struct skd_device *skdev)
4324{
4325 struct skd_special_context *skspcl;
4326 u32 nbytes;
4327
4328 skspcl = &skdev->internal_skspcl;
4329
4330 if (skspcl->data_buf != NULL) {
4331 nbytes = SKD_N_INTERNAL_BYTES;
4332
4333 pci_free_consistent(skdev->pdev, nbytes,
4334 skspcl->data_buf, skspcl->db_dma_address);
4335 }
4336
4337 skspcl->data_buf = NULL;
4338 skspcl->db_dma_address = 0;
4339
4340 if (skspcl->msg_buf != NULL) {
4341 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4342 pci_free_consistent(skdev->pdev, nbytes,
4343 skspcl->msg_buf, skspcl->mb_dma_address);
4344 }
4345
4346 skspcl->msg_buf = NULL;
4347 skspcl->mb_dma_address = 0;
4348
4349 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4350 skspcl->req.sksg_dma_address);
4351
4352 skspcl->req.sksg_list = NULL;
4353 skspcl->req.sksg_dma_address = 0;
4354}
4355
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004356static void skd_free_disk(struct skd_device *skdev)
4357{
4358 struct gendisk *disk = skdev->disk;
4359
Bart Van Assche7277cc62017-08-17 13:12:45 -07004360 if (disk && (disk->flags & GENHD_FL_UP))
4361 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004362
Bart Van Assche7277cc62017-08-17 13:12:45 -07004363 if (skdev->queue) {
4364 blk_cleanup_queue(skdev->queue);
4365 skdev->queue = NULL;
4366 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004367 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004368
4369 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004370 skdev->disk = NULL;
4371}
4372
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004373static void skd_destruct(struct skd_device *skdev)
4374{
4375 if (skdev == NULL)
4376 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004377
Bart Van Asschef98806d2017-08-17 13:12:58 -07004378 dev_dbg(&skdev->pdev->dev, "disk\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004379 skd_free_disk(skdev);
4380
Bart Van Asschef98806d2017-08-17 13:12:58 -07004381 dev_dbg(&skdev->pdev->dev, "sksb\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004382 skd_free_sksb(skdev);
4383
Bart Van Asschef98806d2017-08-17 13:12:58 -07004384 dev_dbg(&skdev->pdev->dev, "skspcl\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004385 skd_free_skspcl(skdev);
4386
Bart Van Asschef98806d2017-08-17 13:12:58 -07004387 dev_dbg(&skdev->pdev->dev, "skreq\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004388 skd_free_skreq(skdev);
4389
Bart Van Asschef98806d2017-08-17 13:12:58 -07004390 dev_dbg(&skdev->pdev->dev, "skmsg\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004391 skd_free_skmsg(skdev);
4392
Bart Van Asschef98806d2017-08-17 13:12:58 -07004393 dev_dbg(&skdev->pdev->dev, "skcomp\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004394 skd_free_skcomp(skdev);
4395
Bart Van Asschef98806d2017-08-17 13:12:58 -07004396 dev_dbg(&skdev->pdev->dev, "skdev\n");
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004397 kfree(skdev);
4398}
4399
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004400/*
4401 *****************************************************************************
4402 * BLOCK DEVICE (BDEV) GLUE
4403 *****************************************************************************
4404 */
4405
4406static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4407{
4408 struct skd_device *skdev;
4409 u64 capacity;
4410
4411 skdev = bdev->bd_disk->private_data;
4412
Bart Van Asschef98806d2017-08-17 13:12:58 -07004413 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
4414 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004415
4416 if (skdev->read_cap_is_valid) {
4417 capacity = get_capacity(skdev->disk);
4418 geo->heads = 64;
4419 geo->sectors = 255;
4420 geo->cylinders = (capacity) / (255 * 64);
4421
4422 return 0;
4423 }
4424 return -EIO;
4425}
4426
Dan Williams0d52c7562016-06-15 19:44:20 -07004427static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004428{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004429 dev_dbg(&skdev->pdev->dev, "add_disk\n");
Dan Williams0d52c7562016-06-15 19:44:20 -07004430 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004431 return 0;
4432}
4433
4434static const struct block_device_operations skd_blockdev_ops = {
4435 .owner = THIS_MODULE,
4436 .ioctl = skd_bdev_ioctl,
4437 .getgeo = skd_bdev_getgeo,
4438};
4439
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004440/*
4441 *****************************************************************************
4442 * PCIe DRIVER GLUE
4443 *****************************************************************************
4444 */
4445
Benoit Taine9baa3c32014-08-08 15:56:03 +02004446static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004447 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4448 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4449 { 0 } /* terminate list */
4450};
4451
4452MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4453
4454static char *skd_pci_info(struct skd_device *skdev, char *str)
4455{
4456 int pcie_reg;
4457
4458 strcpy(str, "PCIe (");
4459 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4460
4461 if (pcie_reg) {
4462
4463 char lwstr[6];
4464 uint16_t pcie_lstat, lspeed, lwidth;
4465
4466 pcie_reg += 0x12;
4467 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4468 lspeed = pcie_lstat & (0xF);
4469 lwidth = (pcie_lstat & 0x3F0) >> 4;
4470
4471 if (lspeed == 1)
4472 strcat(str, "2.5GT/s ");
4473 else if (lspeed == 2)
4474 strcat(str, "5.0GT/s ");
4475 else
4476 strcat(str, "<unknown> ");
4477 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4478 strcat(str, lwstr);
4479 }
4480 return str;
4481}
4482
4483static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4484{
4485 int i;
4486 int rc = 0;
4487 char pci_str[32];
4488 struct skd_device *skdev;
4489
Bart Van Asschef98806d2017-08-17 13:12:58 -07004490 dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
4491 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4492 dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
4493 pdev->device);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004494
4495 rc = pci_enable_device(pdev);
4496 if (rc)
4497 return rc;
4498 rc = pci_request_regions(pdev, DRV_NAME);
4499 if (rc)
4500 goto err_out;
4501 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4502 if (!rc) {
4503 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004504 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4505 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004506 }
4507 } else {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004508 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004509 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004510 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004511 goto err_out_regions;
4512 }
4513 }
4514
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004515 if (!skd_major) {
4516 rc = register_blkdev(0, DRV_NAME);
4517 if (rc < 0)
4518 goto err_out_regions;
4519 BUG_ON(!rc);
4520 skd_major = rc;
4521 }
4522
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004523 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004524 if (skdev == NULL) {
4525 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004526 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004527 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004528
4529 skd_pci_info(skdev, pci_str);
Bart Van Asschef98806d2017-08-17 13:12:58 -07004530 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004531
4532 pci_set_master(pdev);
4533 rc = pci_enable_pcie_error_reporting(pdev);
4534 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004535 dev_err(&pdev->dev,
4536 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004537 skdev->pcie_error_reporting_is_enabled = 0;
4538 } else
4539 skdev->pcie_error_reporting_is_enabled = 1;
4540
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004541 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004542
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004543 for (i = 0; i < SKD_MAX_BARS; i++) {
4544 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4545 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4546 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4547 skdev->mem_size[i]);
4548 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004549 dev_err(&pdev->dev,
4550 "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004551 rc = -ENODEV;
4552 goto err_out_iounmap;
4553 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004554 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4555 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4556 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004557 }
4558
4559 rc = skd_acquire_irq(skdev);
4560 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004561 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004562 goto err_out_iounmap;
4563 }
4564
4565 rc = skd_start_timer(skdev);
4566 if (rc)
4567 goto err_out_timer;
4568
4569 init_waitqueue_head(&skdev->waitq);
4570
4571 skd_start_device(skdev);
4572
4573 rc = wait_event_interruptible_timeout(skdev->waitq,
4574 (skdev->gendisk_on),
4575 (SKD_START_WAIT_SECONDS * HZ));
4576 if (skdev->gendisk_on > 0) {
4577 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004578 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004579 rc = 0;
4580 } else {
4581 /* we timed out, something is wrong with the device,
4582 don't add the disk structure */
Bart Van Asschef98806d2017-08-17 13:12:58 -07004583 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
4584 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004585 /* in case of no error; we timeout with ENXIO */
4586 if (!rc)
4587 rc = -ENXIO;
4588 goto err_out_timer;
4589 }
4590
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004591 return rc;
4592
4593err_out_timer:
4594 skd_stop_device(skdev);
4595 skd_release_irq(skdev);
4596
4597err_out_iounmap:
4598 for (i = 0; i < SKD_MAX_BARS; i++)
4599 if (skdev->mem_map[i])
4600 iounmap(skdev->mem_map[i]);
4601
4602 if (skdev->pcie_error_reporting_is_enabled)
4603 pci_disable_pcie_error_reporting(pdev);
4604
4605 skd_destruct(skdev);
4606
4607err_out_regions:
4608 pci_release_regions(pdev);
4609
4610err_out:
4611 pci_disable_device(pdev);
4612 pci_set_drvdata(pdev, NULL);
4613 return rc;
4614}
4615
4616static void skd_pci_remove(struct pci_dev *pdev)
4617{
4618 int i;
4619 struct skd_device *skdev;
4620
4621 skdev = pci_get_drvdata(pdev);
4622 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004623 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004624 return;
4625 }
4626 skd_stop_device(skdev);
4627 skd_release_irq(skdev);
4628
4629 for (i = 0; i < SKD_MAX_BARS; i++)
4630 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004631 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004632
4633 if (skdev->pcie_error_reporting_is_enabled)
4634 pci_disable_pcie_error_reporting(pdev);
4635
4636 skd_destruct(skdev);
4637
4638 pci_release_regions(pdev);
4639 pci_disable_device(pdev);
4640 pci_set_drvdata(pdev, NULL);
4641
4642 return;
4643}
4644
4645static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4646{
4647 int i;
4648 struct skd_device *skdev;
4649
4650 skdev = pci_get_drvdata(pdev);
4651 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004652 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004653 return -EIO;
4654 }
4655
4656 skd_stop_device(skdev);
4657
4658 skd_release_irq(skdev);
4659
4660 for (i = 0; i < SKD_MAX_BARS; i++)
4661 if (skdev->mem_map[i])
Bart Van Assche4854afe2017-08-17 13:12:59 -07004662 iounmap(skdev->mem_map[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004663
4664 if (skdev->pcie_error_reporting_is_enabled)
4665 pci_disable_pcie_error_reporting(pdev);
4666
4667 pci_release_regions(pdev);
4668 pci_save_state(pdev);
4669 pci_disable_device(pdev);
4670 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4671 return 0;
4672}
4673
4674static int skd_pci_resume(struct pci_dev *pdev)
4675{
4676 int i;
4677 int rc = 0;
4678 struct skd_device *skdev;
4679
4680 skdev = pci_get_drvdata(pdev);
4681 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004682 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004683 return -1;
4684 }
4685
4686 pci_set_power_state(pdev, PCI_D0);
4687 pci_enable_wake(pdev, PCI_D0, 0);
4688 pci_restore_state(pdev);
4689
4690 rc = pci_enable_device(pdev);
4691 if (rc)
4692 return rc;
4693 rc = pci_request_regions(pdev, DRV_NAME);
4694 if (rc)
4695 goto err_out;
4696 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4697 if (!rc) {
4698 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4699
Bart Van Asschef98806d2017-08-17 13:12:58 -07004700 dev_err(&pdev->dev, "consistent DMA mask error %d\n",
4701 rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004702 }
4703 } else {
4704 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4705 if (rc) {
4706
Bart Van Asschef98806d2017-08-17 13:12:58 -07004707 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004708 goto err_out_regions;
4709 }
4710 }
4711
4712 pci_set_master(pdev);
4713 rc = pci_enable_pcie_error_reporting(pdev);
4714 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004715 dev_err(&pdev->dev,
4716 "bad enable of PCIe error reporting rc=%d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004717 skdev->pcie_error_reporting_is_enabled = 0;
4718 } else
4719 skdev->pcie_error_reporting_is_enabled = 1;
4720
4721 for (i = 0; i < SKD_MAX_BARS; i++) {
4722
4723 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4724 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4725 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4726 skdev->mem_size[i]);
4727 if (!skdev->mem_map[i]) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004728 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004729 rc = -ENODEV;
4730 goto err_out_iounmap;
4731 }
Bart Van Asschef98806d2017-08-17 13:12:58 -07004732 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
4733 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
4734 skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004735 }
4736 rc = skd_acquire_irq(skdev);
4737 if (rc) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004738 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004739 goto err_out_iounmap;
4740 }
4741
4742 rc = skd_start_timer(skdev);
4743 if (rc)
4744 goto err_out_timer;
4745
4746 init_waitqueue_head(&skdev->waitq);
4747
4748 skd_start_device(skdev);
4749
4750 return rc;
4751
4752err_out_timer:
4753 skd_stop_device(skdev);
4754 skd_release_irq(skdev);
4755
4756err_out_iounmap:
4757 for (i = 0; i < SKD_MAX_BARS; i++)
4758 if (skdev->mem_map[i])
4759 iounmap(skdev->mem_map[i]);
4760
4761 if (skdev->pcie_error_reporting_is_enabled)
4762 pci_disable_pcie_error_reporting(pdev);
4763
4764err_out_regions:
4765 pci_release_regions(pdev);
4766
4767err_out:
4768 pci_disable_device(pdev);
4769 return rc;
4770}
4771
4772static void skd_pci_shutdown(struct pci_dev *pdev)
4773{
4774 struct skd_device *skdev;
4775
Bart Van Asschef98806d2017-08-17 13:12:58 -07004776 dev_err(&pdev->dev, "%s called\n", __func__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004777
4778 skdev = pci_get_drvdata(pdev);
4779 if (!skdev) {
Bart Van Asschef98806d2017-08-17 13:12:58 -07004780 dev_err(&pdev->dev, "no device data for PCI\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004781 return;
4782 }
4783
Bart Van Asschef98806d2017-08-17 13:12:58 -07004784 dev_err(&pdev->dev, "calling stop\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004785 skd_stop_device(skdev);
4786}
4787
4788static struct pci_driver skd_driver = {
4789 .name = DRV_NAME,
4790 .id_table = skd_pci_tbl,
4791 .probe = skd_pci_probe,
4792 .remove = skd_pci_remove,
4793 .suspend = skd_pci_suspend,
4794 .resume = skd_pci_resume,
4795 .shutdown = skd_pci_shutdown,
4796};
4797
4798/*
4799 *****************************************************************************
4800 * LOGGING SUPPORT
4801 *****************************************************************************
4802 */
4803
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004804const char *skd_drive_state_to_str(int state)
4805{
4806 switch (state) {
4807 case FIT_SR_DRIVE_OFFLINE:
4808 return "OFFLINE";
4809 case FIT_SR_DRIVE_INIT:
4810 return "INIT";
4811 case FIT_SR_DRIVE_ONLINE:
4812 return "ONLINE";
4813 case FIT_SR_DRIVE_BUSY:
4814 return "BUSY";
4815 case FIT_SR_DRIVE_FAULT:
4816 return "FAULT";
4817 case FIT_SR_DRIVE_DEGRADED:
4818 return "DEGRADED";
4819 case FIT_SR_PCIE_LINK_DOWN:
4820 return "INK_DOWN";
4821 case FIT_SR_DRIVE_SOFT_RESET:
4822 return "SOFT_RESET";
4823 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4824 return "NEED_FW";
4825 case FIT_SR_DRIVE_INIT_FAULT:
4826 return "INIT_FAULT";
4827 case FIT_SR_DRIVE_BUSY_SANITIZE:
4828 return "BUSY_SANITIZE";
4829 case FIT_SR_DRIVE_BUSY_ERASE:
4830 return "BUSY_ERASE";
4831 case FIT_SR_DRIVE_FW_BOOTING:
4832 return "FW_BOOTING";
4833 default:
4834 return "???";
4835 }
4836}
4837
4838const char *skd_skdev_state_to_str(enum skd_drvr_state state)
4839{
4840 switch (state) {
4841 case SKD_DRVR_STATE_LOAD:
4842 return "LOAD";
4843 case SKD_DRVR_STATE_IDLE:
4844 return "IDLE";
4845 case SKD_DRVR_STATE_BUSY:
4846 return "BUSY";
4847 case SKD_DRVR_STATE_STARTING:
4848 return "STARTING";
4849 case SKD_DRVR_STATE_ONLINE:
4850 return "ONLINE";
4851 case SKD_DRVR_STATE_PAUSING:
4852 return "PAUSING";
4853 case SKD_DRVR_STATE_PAUSED:
4854 return "PAUSED";
4855 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
4856 return "DRAINING_TIMEOUT";
4857 case SKD_DRVR_STATE_RESTARTING:
4858 return "RESTARTING";
4859 case SKD_DRVR_STATE_RESUMING:
4860 return "RESUMING";
4861 case SKD_DRVR_STATE_STOPPING:
4862 return "STOPPING";
4863 case SKD_DRVR_STATE_SYNCING:
4864 return "SYNCING";
4865 case SKD_DRVR_STATE_FAULT:
4866 return "FAULT";
4867 case SKD_DRVR_STATE_DISAPPEARED:
4868 return "DISAPPEARED";
4869 case SKD_DRVR_STATE_BUSY_ERASE:
4870 return "BUSY_ERASE";
4871 case SKD_DRVR_STATE_BUSY_SANITIZE:
4872 return "BUSY_SANITIZE";
4873 case SKD_DRVR_STATE_BUSY_IMMINENT:
4874 return "BUSY_IMMINENT";
4875 case SKD_DRVR_STATE_WAIT_BOOT:
4876 return "WAIT_BOOT";
4877
4878 default:
4879 return "???";
4880 }
4881}
4882
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304883static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004884{
4885 switch (state) {
4886 case SKD_MSG_STATE_IDLE:
4887 return "IDLE";
4888 case SKD_MSG_STATE_BUSY:
4889 return "BUSY";
4890 default:
4891 return "???";
4892 }
4893}
4894
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05304895static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004896{
4897 switch (state) {
4898 case SKD_REQ_STATE_IDLE:
4899 return "IDLE";
4900 case SKD_REQ_STATE_SETUP:
4901 return "SETUP";
4902 case SKD_REQ_STATE_BUSY:
4903 return "BUSY";
4904 case SKD_REQ_STATE_COMPLETED:
4905 return "COMPLETED";
4906 case SKD_REQ_STATE_TIMEOUT:
4907 return "TIMEOUT";
4908 case SKD_REQ_STATE_ABORTED:
4909 return "ABORTED";
4910 default:
4911 return "???";
4912 }
4913}
4914
4915static void skd_log_skdev(struct skd_device *skdev, const char *event)
4916{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004917 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
4918 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
4919 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
4920 skd_skdev_state_to_str(skdev->state), skdev->state);
4921 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
4922 skdev->in_flight, skdev->cur_max_queue_depth,
4923 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
4924 dev_dbg(&skdev->pdev->dev, " timestamp=0x%x cycle=%d cycle_ix=%d\n",
4925 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004926}
4927
4928static void skd_log_skmsg(struct skd_device *skdev,
4929 struct skd_fitmsg_context *skmsg, const char *event)
4930{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004931 dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
4932 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x length=%d\n",
4933 skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
4934 skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004935}
4936
4937static void skd_log_skreq(struct skd_device *skdev,
4938 struct skd_request_context *skreq, const char *event)
4939{
Bart Van Asschef98806d2017-08-17 13:12:58 -07004940 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
4941 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
4942 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
4943 skreq->fitmsg_id);
4944 dev_dbg(&skdev->pdev->dev, " timo=0x%x sg_dir=%d n_sg=%d\n",
4945 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004946
Jens Axboefcd37eb2013-11-01 10:14:56 -06004947 if (skreq->req != NULL) {
4948 struct request *req = skreq->req;
4949 u32 lba = (u32)blk_rq_pos(req);
4950 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004951
Bart Van Asschef98806d2017-08-17 13:12:58 -07004952 dev_dbg(&skdev->pdev->dev,
4953 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
4954 lba, lba, count, count, (int)rq_data_dir(req));
Jens Axboefcd37eb2013-11-01 10:14:56 -06004955 } else
Bart Van Asschef98806d2017-08-17 13:12:58 -07004956 dev_dbg(&skdev->pdev->dev, "req=NULL\n");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004957}
4958
4959/*
4960 *****************************************************************************
4961 * MODULE GLUE
4962 *****************************************************************************
4963 */
4964
4965static int __init skd_init(void)
4966{
Bart Van Assche2da7b402017-08-17 13:13:01 -07004967 BUILD_BUG_ON(sizeof(struct fit_msg_hdr) + SKD_MAX_REQ_PER_MSG *
4968 sizeof(struct skd_scsi_request) != SKD_N_FITMSG_BYTES);
4969
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);