blob: 54c6711a42d1d2a49dcf5f356e5b6a35caa0afab [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>
34#include <scsi/scsi.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060035#include <scsi/sg.h>
36#include <linux/io.h>
37#include <linux/uaccess.h>
Bartlomiej Zolnierkiewicz4ca90b52013-11-05 12:37:04 +010038#include <asm/unaligned.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060039
40#include "skd_s1120.h"
41
42static int skd_dbg_level;
43static int skd_isr_comp_limit = 4;
44
45enum {
46 STEC_LINK_2_5GTS = 0,
47 STEC_LINK_5GTS = 1,
48 STEC_LINK_8GTS = 2,
49 STEC_LINK_UNKNOWN = 0xFF
50};
51
52enum {
53 SKD_FLUSH_INITIALIZER,
54 SKD_FLUSH_ZERO_SIZE_FIRST,
55 SKD_FLUSH_DATA_SECOND,
56};
57
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060058#define SKD_ASSERT(expr) \
59 do { \
60 if (unlikely(!(expr))) { \
61 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
62 # expr, __FILE__, __func__, __LINE__); \
63 } \
64 } while (0)
65
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060066#define DRV_NAME "skd"
67#define DRV_VERSION "2.2.1"
68#define DRV_BUILD_ID "0260"
69#define PFX DRV_NAME ": "
70#define DRV_BIN_VERSION 0x100
71#define DRV_VER_COMPL "2.2.1." DRV_BUILD_ID
72
Bart Van Asschebec9e8a2017-08-17 13:12:47 -070073MODULE_LICENSE("GPL");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060074
Mike Snitzer38d4a1b2013-11-01 15:05:10 -040075MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060076MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
77
78#define PCI_VENDOR_ID_STEC 0x1B39
79#define PCI_DEVICE_ID_S1120 0x0001
80
81#define SKD_FUA_NV (1 << 1)
82#define SKD_MINORS_PER_DEVICE 16
83
84#define SKD_MAX_QUEUE_DEPTH 200u
85
86#define SKD_PAUSE_TIMEOUT (5 * 1000)
87
88#define SKD_N_FITMSG_BYTES (512u)
89
90#define SKD_N_SPECIAL_CONTEXT 32u
91#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
92
93/* SG elements are 32 bytes, so we can make this 4096 and still be under the
94 * 128KB limit. That allows 4096*4K = 16M xfer size
95 */
96#define SKD_N_SG_PER_REQ_DEFAULT 256u
97#define SKD_N_SG_PER_SPECIAL 256u
98
99#define SKD_N_COMPLETION_ENTRY 256u
100#define SKD_N_READ_CAP_BYTES (8u)
101
102#define SKD_N_INTERNAL_BYTES (512u)
103
104/* 5 bits of uniqifier, 0xF800 */
105#define SKD_ID_INCR (0x400)
106#define SKD_ID_TABLE_MASK (3u << 8u)
107#define SKD_ID_RW_REQUEST (0u << 8u)
108#define SKD_ID_INTERNAL (1u << 8u)
109#define SKD_ID_SPECIAL_REQUEST (2u << 8u)
110#define SKD_ID_FIT_MSG (3u << 8u)
111#define SKD_ID_SLOT_MASK 0x00FFu
112#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
113
114#define SKD_N_TIMEOUT_SLOT 4u
115#define SKD_TIMEOUT_SLOT_MASK 3u
116
117#define SKD_N_MAX_SECTORS 2048u
118
119#define SKD_MAX_RETRIES 2u
120
121#define SKD_TIMER_SECONDS(seconds) (seconds)
122#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
123
124#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600125
126enum skd_drvr_state {
127 SKD_DRVR_STATE_LOAD,
128 SKD_DRVR_STATE_IDLE,
129 SKD_DRVR_STATE_BUSY,
130 SKD_DRVR_STATE_STARTING,
131 SKD_DRVR_STATE_ONLINE,
132 SKD_DRVR_STATE_PAUSING,
133 SKD_DRVR_STATE_PAUSED,
134 SKD_DRVR_STATE_DRAINING_TIMEOUT,
135 SKD_DRVR_STATE_RESTARTING,
136 SKD_DRVR_STATE_RESUMING,
137 SKD_DRVR_STATE_STOPPING,
138 SKD_DRVR_STATE_FAULT,
139 SKD_DRVR_STATE_DISAPPEARED,
140 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
141 SKD_DRVR_STATE_BUSY_ERASE,
142 SKD_DRVR_STATE_BUSY_SANITIZE,
143 SKD_DRVR_STATE_BUSY_IMMINENT,
144 SKD_DRVR_STATE_WAIT_BOOT,
145 SKD_DRVR_STATE_SYNCING,
146};
147
148#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
149#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
150#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
151#define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u)
152#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
153#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
154#define SKD_START_WAIT_SECONDS 90u
155
156enum skd_req_state {
157 SKD_REQ_STATE_IDLE,
158 SKD_REQ_STATE_SETUP,
159 SKD_REQ_STATE_BUSY,
160 SKD_REQ_STATE_COMPLETED,
161 SKD_REQ_STATE_TIMEOUT,
162 SKD_REQ_STATE_ABORTED,
163};
164
165enum skd_fit_msg_state {
166 SKD_MSG_STATE_IDLE,
167 SKD_MSG_STATE_BUSY,
168};
169
170enum skd_check_status_action {
171 SKD_CHECK_STATUS_REPORT_GOOD,
172 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
173 SKD_CHECK_STATUS_REQUEUE_REQUEST,
174 SKD_CHECK_STATUS_REPORT_ERROR,
175 SKD_CHECK_STATUS_BUSY_IMMINENT,
176};
177
178struct skd_fitmsg_context {
179 enum skd_fit_msg_state state;
180
181 struct skd_fitmsg_context *next;
182
183 u32 id;
184 u16 outstanding;
185
186 u32 length;
187 u32 offset;
188
189 u8 *msg_buf;
190 dma_addr_t mb_dma_address;
191};
192
193struct skd_request_context {
194 enum skd_req_state state;
195
196 struct skd_request_context *next;
197
198 u16 id;
199 u32 fitmsg_id;
200
201 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600202 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600203
204 u32 timeout_stamp;
205 u8 sg_data_dir;
206 struct scatterlist *sg;
207 u32 n_sg;
208 u32 sg_byte_count;
209
210 struct fit_sg_descriptor *sksg_list;
211 dma_addr_t sksg_dma_address;
212
213 struct fit_completion_entry_v1 completion;
214
215 struct fit_comp_error_info err_info;
216
217};
218#define SKD_DATA_DIR_HOST_TO_CARD 1
219#define SKD_DATA_DIR_CARD_TO_HOST 2
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600220
221struct skd_special_context {
222 struct skd_request_context req;
223
224 u8 orphaned;
225
226 void *data_buf;
227 dma_addr_t db_dma_address;
228
229 u8 *msg_buf;
230 dma_addr_t mb_dma_address;
231};
232
233struct skd_sg_io {
234 fmode_t mode;
235 void __user *argp;
236
237 struct sg_io_hdr sg;
238
239 u8 cdb[16];
240
241 u32 dxfer_len;
242 u32 iovcnt;
243 struct sg_iovec *iov;
244 struct sg_iovec no_iov_iov;
245
246 struct skd_special_context *skspcl;
247};
248
249typedef enum skd_irq_type {
250 SKD_IRQ_LEGACY,
251 SKD_IRQ_MSI,
252 SKD_IRQ_MSIX
253} skd_irq_type_t;
254
255#define SKD_MAX_BARS 2
256
257struct skd_device {
258 volatile void __iomem *mem_map[SKD_MAX_BARS];
259 resource_size_t mem_phys[SKD_MAX_BARS];
260 u32 mem_size[SKD_MAX_BARS];
261
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600262 struct skd_msix_entry *msix_entries;
263
264 struct pci_dev *pdev;
265 int pcie_error_reporting_is_enabled;
266
267 spinlock_t lock;
268 struct gendisk *disk;
269 struct request_queue *queue;
270 struct device *class_dev;
271 int gendisk_on;
272 int sync_done;
273
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600274 u32 devno;
275 u32 major;
276 char name[32];
277 char isr_name[30];
278
279 enum skd_drvr_state state;
280 u32 drive_state;
281
282 u32 in_flight;
283 u32 cur_max_queue_depth;
284 u32 queue_low_water_mark;
285 u32 dev_max_queue_depth;
286
287 u32 num_fitmsg_context;
288 u32 num_req_context;
289
290 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
291 u32 timeout_stamp;
292 struct skd_fitmsg_context *skmsg_free_list;
293 struct skd_fitmsg_context *skmsg_table;
294
295 struct skd_request_context *skreq_free_list;
296 struct skd_request_context *skreq_table;
297
298 struct skd_special_context *skspcl_free_list;
299 struct skd_special_context *skspcl_table;
300
301 struct skd_special_context internal_skspcl;
302 u32 read_cap_blocksize;
303 u32 read_cap_last_lba;
304 int read_cap_is_valid;
305 int inquiry_is_valid;
306 u8 inq_serial_num[13]; /*12 chars plus null term */
307 u8 id_str[80]; /* holds a composite name (pci + sernum) */
308
309 u8 skcomp_cycle;
310 u32 skcomp_ix;
311 struct fit_completion_entry_v1 *skcomp_table;
312 struct fit_comp_error_info *skerr_table;
313 dma_addr_t cq_dma_address;
314
315 wait_queue_head_t waitq;
316
317 struct timer_list timer;
318 u32 timer_countdown;
319 u32 timer_substate;
320
321 int n_special;
322 int sgs_per_request;
323 u32 last_mtd;
324
325 u32 proto_ver;
326
327 int dbg_level;
328 u32 connect_time_stamp;
329 int connect_retries;
330#define SKD_MAX_CONNECT_RETRIES 16
331 u32 drive_jiffies;
332
333 u32 timo_slot;
334
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600335 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600336};
337
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600338#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
339#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
340#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
341
342static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
343{
Bart Van Assche14262a42017-08-17 13:12:57 -0700344 u32 val = readl(skdev->mem_map[1] + offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600345
Bart Van Assche14262a42017-08-17 13:12:57 -0700346 if (unlikely(skdev->dbg_level >= 2))
347 pr_debug("%s offset %x = %x\n", skdev->name, offset, val);
348 return val;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600349}
350
351static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
352 u32 offset)
353{
Bart Van Assche14262a42017-08-17 13:12:57 -0700354 writel(val, skdev->mem_map[1] + offset);
355 if (unlikely(skdev->dbg_level >= 2))
356 pr_debug("%s offset %x = %x\n", skdev->name, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600357}
358
359static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
360 u32 offset)
361{
Bart Van Assche14262a42017-08-17 13:12:57 -0700362 writeq(val, skdev->mem_map[1] + offset);
363 if (unlikely(skdev->dbg_level >= 2))
364 pr_debug("%s offset %x = %016llx\n", skdev->name, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600365}
366
367
368#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
369static int skd_isr_type = SKD_IRQ_DEFAULT;
370
371module_param(skd_isr_type, int, 0444);
372MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
373 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
374
375#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
376static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
377
378module_param(skd_max_req_per_msg, int, 0444);
379MODULE_PARM_DESC(skd_max_req_per_msg,
380 "Maximum SCSI requests packed in a single message."
381 " (1-14, default==1)");
382
383#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
384#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
385static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
386
387module_param(skd_max_queue_depth, int, 0444);
388MODULE_PARM_DESC(skd_max_queue_depth,
389 "Maximum SCSI requests issued to s1120."
390 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
391
392static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
393module_param(skd_sgs_per_request, int, 0444);
394MODULE_PARM_DESC(skd_sgs_per_request,
395 "Maximum SG elements per block request."
396 " (1-4096, default==256)");
397
398static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
399module_param(skd_max_pass_thru, int, 0444);
400MODULE_PARM_DESC(skd_max_pass_thru,
401 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
402
403module_param(skd_dbg_level, int, 0444);
404MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
405
406module_param(skd_isr_comp_limit, int, 0444);
407MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
408
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600409/* Major device number dynamically assigned. */
410static u32 skd_major;
411
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600412static void skd_destruct(struct skd_device *skdev);
413static const struct block_device_operations skd_blockdev_ops;
414static void skd_send_fitmsg(struct skd_device *skdev,
415 struct skd_fitmsg_context *skmsg);
416static void skd_send_special_fitmsg(struct skd_device *skdev,
417 struct skd_special_context *skspcl);
418static void skd_request_fn(struct request_queue *rq);
419static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200420 struct skd_request_context *skreq, blk_status_t status);
421static bool skd_preop_sg_list(struct skd_device *skdev,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600422 struct skd_request_context *skreq);
423static void skd_postop_sg_list(struct skd_device *skdev,
424 struct skd_request_context *skreq);
425
426static void skd_restart_device(struct skd_device *skdev);
427static int skd_quiesce_dev(struct skd_device *skdev);
428static int skd_unquiesce_dev(struct skd_device *skdev);
429static void skd_release_special(struct skd_device *skdev,
430 struct skd_special_context *skspcl);
431static void skd_disable_interrupts(struct skd_device *skdev);
432static void skd_isr_fwstate(struct skd_device *skdev);
433static void skd_recover_requests(struct skd_device *skdev, int requeue);
434static void skd_soft_reset(struct skd_device *skdev);
435
436static const char *skd_name(struct skd_device *skdev);
437const char *skd_drive_state_to_str(int state);
438const char *skd_skdev_state_to_str(enum skd_drvr_state state);
439static void skd_log_skdev(struct skd_device *skdev, const char *event);
440static void skd_log_skmsg(struct skd_device *skdev,
441 struct skd_fitmsg_context *skmsg, const char *event);
442static void skd_log_skreq(struct skd_device *skdev,
443 struct skd_request_context *skreq, const char *event);
444
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600445/*
446 *****************************************************************************
447 * READ/WRITE REQUESTS
448 *****************************************************************************
449 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600450static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600451{
452 struct request_queue *q = skdev->queue;
453 struct request *req;
454
455 for (;; ) {
456 req = blk_peek_request(q);
457 if (req == NULL)
458 break;
459 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200460 __blk_end_request_all(req, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600461 }
462}
463
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600464static void
465skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
466 int data_dir, unsigned lba,
467 unsigned count)
468{
469 if (data_dir == READ)
470 scsi_req->cdb[0] = 0x28;
471 else
472 scsi_req->cdb[0] = 0x2a;
473
474 scsi_req->cdb[1] = 0;
475 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
476 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
477 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
478 scsi_req->cdb[5] = (lba & 0xff);
479 scsi_req->cdb[6] = 0;
480 scsi_req->cdb[7] = (count & 0xff00) >> 8;
481 scsi_req->cdb[8] = count & 0xff;
482 scsi_req->cdb[9] = 0;
483}
484
485static void
486skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400487 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600488{
489 skreq->flush_cmd = 1;
490
491 scsi_req->cdb[0] = 0x35;
492 scsi_req->cdb[1] = 0;
493 scsi_req->cdb[2] = 0;
494 scsi_req->cdb[3] = 0;
495 scsi_req->cdb[4] = 0;
496 scsi_req->cdb[5] = 0;
497 scsi_req->cdb[6] = 0;
498 scsi_req->cdb[7] = 0;
499 scsi_req->cdb[8] = 0;
500 scsi_req->cdb[9] = 0;
501}
502
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600503static void skd_request_fn_not_online(struct request_queue *q);
504
505static void skd_request_fn(struct request_queue *q)
506{
507 struct skd_device *skdev = q->queuedata;
508 struct skd_fitmsg_context *skmsg = NULL;
509 struct fit_msg_hdr *fmh = NULL;
510 struct skd_request_context *skreq;
511 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600512 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600513 unsigned long io_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600514 u32 lba;
515 u32 count;
516 int data_dir;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600517 u64 be_dmaa;
518 u64 cmdctxt;
519 u32 timo_slot;
520 void *cmd_ptr;
521 int flush, fua;
522
523 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
524 skd_request_fn_not_online(q);
525 return;
526 }
527
Jens Axboe6a5ec652013-11-01 10:38:45 -0600528 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600529 if (skdev->skmsg_free_list == NULL ||
530 skdev->skreq_free_list == NULL ||
531 skdev->in_flight >= skdev->queue_low_water_mark)
532 /* There is still some kind of shortage */
533 return;
534
Jens Axboe6a5ec652013-11-01 10:38:45 -0600535 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600536 }
537
538 /*
539 * Stop conditions:
540 * - There are no more native requests
541 * - There are already the maximum number of requests in progress
542 * - There are no more skd_request_context entries
543 * - There are no more FIT msg buffers
544 */
545 for (;; ) {
546
547 flush = fua = 0;
548
Jens Axboefcd37eb2013-11-01 10:14:56 -0600549 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600550
Jens Axboefcd37eb2013-11-01 10:14:56 -0600551 /* Are there any native requests to start? */
552 if (req == NULL)
553 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600554
Jens Axboefcd37eb2013-11-01 10:14:56 -0600555 lba = (u32)blk_rq_pos(req);
556 count = blk_rq_sectors(req);
557 data_dir = rq_data_dir(req);
558 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600559
Mike Christie3a5e02c2016-06-05 14:32:23 -0500560 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600561 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600562
Jens Axboefcd37eb2013-11-01 10:14:56 -0600563 if (io_flags & REQ_FUA)
564 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600565
Jens Axboefcd37eb2013-11-01 10:14:56 -0600566 pr_debug("%s:%s:%d new req=%p lba=%u(0x%x) "
567 "count=%u(0x%x) dir=%d\n",
568 skdev->name, __func__, __LINE__,
569 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600570
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400571 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600572
573 /* Are too many requets already in progress? */
574 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100575 pr_debug("%s:%s:%d qdepth %d, limit %d\n",
576 skdev->name, __func__, __LINE__,
577 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600578 break;
579 }
580
581 /* Is a skd_request_context available? */
582 skreq = skdev->skreq_free_list;
583 if (skreq == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100584 pr_debug("%s:%s:%d Out of req=%p\n",
585 skdev->name, __func__, __LINE__, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600586 break;
587 }
588 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
589 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
590
591 /* Now we check to see if we can get a fit msg */
592 if (skmsg == NULL) {
593 if (skdev->skmsg_free_list == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100594 pr_debug("%s:%s:%d Out of msg\n",
595 skdev->name, __func__, __LINE__);
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
615 /* Either a FIT msg is in progress or we have to start one. */
616 if (skmsg == NULL) {
617 /* Are there any FIT msg buffers available? */
618 skmsg = skdev->skmsg_free_list;
619 if (skmsg == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100620 pr_debug("%s:%s:%d Out of msg skdev=%p\n",
621 skdev->name, __func__, __LINE__,
622 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600623 break;
624 }
625 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
626 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
627
628 skdev->skmsg_free_list = skmsg->next;
629
630 skmsg->state = SKD_MSG_STATE_BUSY;
631 skmsg->id += SKD_ID_INCR;
632
633 /* Initialize the FIT msg header */
634 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
635 memset(fmh, 0, sizeof(*fmh));
636 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
637 skmsg->length = sizeof(*fmh);
638 }
639
640 skreq->fitmsg_id = skmsg->id;
641
642 /*
643 * Note that a FIT msg may have just been started
644 * but contains no SoFIT requests yet.
645 */
646
647 /*
648 * Transcode the request, checking as we go. The outcome of
649 * the transcoding is represented by the error variable.
650 */
651 cmd_ptr = &skmsg->msg_buf[skmsg->length];
652 memset(cmd_ptr, 0, 32);
653
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600654 be_dmaa = cpu_to_be64((u64)skreq->sksg_dma_address);
655 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
661 if (data_dir == READ)
662 skreq->sg_data_dir = SKD_DATA_DIR_CARD_TO_HOST;
663 else
664 skreq->sg_data_dir = SKD_DATA_DIR_HOST_TO_CARD;
665
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600666 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600667 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
668 SKD_ASSERT(skreq->flush_cmd == 1);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600669 } else {
670 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
671 }
672
673 if (fua)
674 scsi_req->cdb[1] |= SKD_FUA_NV;
675
Jens Axboefcd37eb2013-11-01 10:14:56 -0600676 if (!req->bio)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600677 goto skip_sg;
678
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200679 if (!skd_preop_sg_list(skdev, skreq)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600680 /*
681 * Complete the native request with error.
682 * Note that the request context is still at the
683 * head of the free list, and that the SoFIT request
684 * was encoded into the FIT msg buffer but the FIT
685 * msg length has not been updated. In short, the
686 * only resource that has been allocated but might
687 * not be used is that the FIT msg could be empty.
688 */
rchinthekindi2e44b422013-10-24 12:51:23 +0100689 pr_debug("%s:%s:%d error Out\n",
690 skdev->name, __func__, __LINE__);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200691 skd_end_request(skdev, skreq, BLK_STS_RESOURCE);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600692 continue;
693 }
694
695skip_sg:
696 scsi_req->hdr.sg_list_len_bytes =
697 cpu_to_be32(skreq->sg_byte_count);
698
699 /* Complete resource allocations. */
700 skdev->skreq_free_list = skreq->next;
701 skreq->state = SKD_REQ_STATE_BUSY;
702 skreq->id += SKD_ID_INCR;
703
704 skmsg->length += sizeof(struct skd_scsi_request);
705 fmh->num_protocol_cmds_coalesced++;
706
707 /*
708 * Update the active request counts.
709 * Capture the timeout timestamp.
710 */
711 skreq->timeout_stamp = skdev->timeout_stamp;
712 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
713 skdev->timeout_slot[timo_slot]++;
714 skdev->in_flight++;
rchinthekindi2e44b422013-10-24 12:51:23 +0100715 pr_debug("%s:%s:%d req=0x%x busy=%d\n",
716 skdev->name, __func__, __LINE__,
717 skreq->id, skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600718
719 /*
720 * If the FIT msg buffer is full send it.
721 */
722 if (skmsg->length >= SKD_N_FITMSG_BYTES ||
723 fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
724 skd_send_fitmsg(skdev, skmsg);
725 skmsg = NULL;
726 fmh = NULL;
727 }
728 }
729
730 /*
731 * Is a FIT msg in progress? If it is empty put the buffer back
732 * on the free list. If it is non-empty send what we got.
733 * This minimizes latency when there are fewer requests than
734 * what fits in a FIT msg.
735 */
736 if (skmsg != NULL) {
737 /* Bigger than just a FIT msg header? */
738 if (skmsg->length > sizeof(struct fit_msg_hdr)) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100739 pr_debug("%s:%s:%d sending msg=%p, len %d\n",
740 skdev->name, __func__, __LINE__,
741 skmsg, skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600742 skd_send_fitmsg(skdev, skmsg);
743 } else {
744 /*
745 * The FIT msg is empty. It means we got started
746 * on the msg, but the requests were rejected.
747 */
748 skmsg->state = SKD_MSG_STATE_IDLE;
749 skmsg->id += SKD_ID_INCR;
750 skmsg->next = skdev->skmsg_free_list;
751 skdev->skmsg_free_list = skmsg;
752 }
753 skmsg = NULL;
754 fmh = NULL;
755 }
756
757 /*
758 * If req is non-NULL it means there is something to do but
759 * we are out of a resource.
760 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600761 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600762 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600763}
764
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400765static void skd_end_request(struct skd_device *skdev,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200766 struct skd_request_context *skreq, blk_status_t error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600767{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600768 if (unlikely(error)) {
769 struct request *req = skreq->req;
770 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
771 u32 lba = (u32)blk_rq_pos(req);
772 u32 count = blk_rq_sectors(req);
773
774 pr_err("(%s): Error cmd=%s sect=%u count=%u id=0x%x\n",
775 skd_name(skdev), cmd, lba, count, skreq->id);
776 } else
rchinthekindi2e44b422013-10-24 12:51:23 +0100777 pr_debug("%s:%s:%d id=0x%x error=%d\n",
778 skdev->name, __func__, __LINE__, skreq->id, error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600779
780 __blk_end_request_all(skreq->req, error);
781}
782
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200783static bool skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400784 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600785{
786 struct request *req = skreq->req;
787 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
788 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
789 struct scatterlist *sg = &skreq->sg[0];
790 int n_sg;
791 int i;
792
793 skreq->sg_byte_count = 0;
794
795 /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
796 skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */
797
798 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
799 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200800 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600801
802 /*
803 * Map scatterlist to PCI bus addresses.
804 * Note PCI might change the number of entries.
805 */
806 n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
807 if (n_sg <= 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200808 return false;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600809
810 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
811
812 skreq->n_sg = n_sg;
813
814 for (i = 0; i < n_sg; i++) {
815 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
816 u32 cnt = sg_dma_len(&sg[i]);
817 uint64_t dma_addr = sg_dma_address(&sg[i]);
818
819 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
820 sgd->byte_count = cnt;
821 skreq->sg_byte_count += cnt;
822 sgd->host_side_addr = dma_addr;
823 sgd->dev_side_addr = 0;
824 }
825
826 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
827 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
828
829 if (unlikely(skdev->dbg_level > 1)) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100830 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
831 skdev->name, __func__, __LINE__,
832 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600833 for (i = 0; i < n_sg; i++) {
834 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
rchinthekindi2e44b422013-10-24 12:51:23 +0100835 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
836 "addr=0x%llx next=0x%llx\n",
837 skdev->name, __func__, __LINE__,
838 i, sgd->byte_count, sgd->control,
839 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600840 }
841 }
842
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200843 return true;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600844}
845
Jens Axboefcd37eb2013-11-01 10:14:56 -0600846static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400847 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600848{
849 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
850 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
851
852 /*
853 * restore the next ptr for next IO request so we
854 * don't have to set it every time.
855 */
856 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
857 skreq->sksg_dma_address +
858 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
859 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
860}
861
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600862static void skd_request_fn_not_online(struct request_queue *q)
863{
864 struct skd_device *skdev = q->queuedata;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600865
866 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
867
868 skd_log_skdev(skdev, "req_not_online");
869 switch (skdev->state) {
870 case SKD_DRVR_STATE_PAUSING:
871 case SKD_DRVR_STATE_PAUSED:
872 case SKD_DRVR_STATE_STARTING:
873 case SKD_DRVR_STATE_RESTARTING:
874 case SKD_DRVR_STATE_WAIT_BOOT:
875 /* In case of starting, we haven't started the queue,
876 * so we can't get here... but requests are
877 * possibly hanging out waiting for us because we
878 * reported the dev/skd0 already. They'll wait
879 * forever if connect doesn't complete.
880 * What to do??? delay dev/skd0 ??
881 */
882 case SKD_DRVR_STATE_BUSY:
883 case SKD_DRVR_STATE_BUSY_IMMINENT:
884 case SKD_DRVR_STATE_BUSY_ERASE:
885 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
886 return;
887
888 case SKD_DRVR_STATE_BUSY_SANITIZE:
889 case SKD_DRVR_STATE_STOPPING:
890 case SKD_DRVR_STATE_SYNCING:
891 case SKD_DRVR_STATE_FAULT:
892 case SKD_DRVR_STATE_DISAPPEARED:
893 default:
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600894 break;
895 }
896
897 /* If we get here, terminate all pending block requeusts
898 * with EIO and any scsi pass thru with appropriate sense
899 */
900
901 skd_fail_all_pending(skdev);
902}
903
904/*
905 *****************************************************************************
906 * TIMER
907 *****************************************************************************
908 */
909
910static void skd_timer_tick_not_online(struct skd_device *skdev);
911
912static void skd_timer_tick(ulong arg)
913{
914 struct skd_device *skdev = (struct skd_device *)arg;
915
916 u32 timo_slot;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600917 unsigned long reqflags;
918 u32 state;
919
920 if (skdev->state == SKD_DRVR_STATE_FAULT)
921 /* The driver has declared fault, and we want it to
922 * stay that way until driver is reloaded.
923 */
924 return;
925
926 spin_lock_irqsave(&skdev->lock, reqflags);
927
928 state = SKD_READL(skdev, FIT_STATUS);
929 state &= FIT_SR_DRIVE_STATE_MASK;
930 if (state != skdev->drive_state)
931 skd_isr_fwstate(skdev);
932
933 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
934 skd_timer_tick_not_online(skdev);
935 goto timer_func_out;
936 }
937 skdev->timeout_stamp++;
938 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
939
940 /*
941 * All requests that happened during the previous use of
942 * this slot should be done by now. The previous use was
943 * over 7 seconds ago.
944 */
945 if (skdev->timeout_slot[timo_slot] == 0)
946 goto timer_func_out;
947
948 /* Something is overdue */
rchinthekindi2e44b422013-10-24 12:51:23 +0100949 pr_debug("%s:%s:%d found %d timeouts, draining busy=%d\n",
950 skdev->name, __func__, __LINE__,
951 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600952 pr_err("(%s): Overdue IOs (%d), busy %d\n",
953 skd_name(skdev), skdev->timeout_slot[timo_slot],
954 skdev->in_flight);
955
956 skdev->timer_countdown = SKD_DRAINING_TIMO;
957 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
958 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -0600959 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600960
961timer_func_out:
962 mod_timer(&skdev->timer, (jiffies + HZ));
963
964 spin_unlock_irqrestore(&skdev->lock, reqflags);
965}
966
967static void skd_timer_tick_not_online(struct skd_device *skdev)
968{
969 switch (skdev->state) {
970 case SKD_DRVR_STATE_IDLE:
971 case SKD_DRVR_STATE_LOAD:
972 break;
973 case SKD_DRVR_STATE_BUSY_SANITIZE:
rchinthekindi2e44b422013-10-24 12:51:23 +0100974 pr_debug("%s:%s:%d drive busy sanitize[%x], driver[%x]\n",
975 skdev->name, __func__, __LINE__,
976 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600977 /* If we've been in sanitize for 3 seconds, we figure we're not
978 * going to get anymore completions, so recover requests now
979 */
980 if (skdev->timer_countdown > 0) {
981 skdev->timer_countdown--;
982 return;
983 }
984 skd_recover_requests(skdev, 0);
985 break;
986
987 case SKD_DRVR_STATE_BUSY:
988 case SKD_DRVR_STATE_BUSY_IMMINENT:
989 case SKD_DRVR_STATE_BUSY_ERASE:
rchinthekindi2e44b422013-10-24 12:51:23 +0100990 pr_debug("%s:%s:%d busy[%x], countdown=%d\n",
991 skdev->name, __func__, __LINE__,
992 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600993 if (skdev->timer_countdown > 0) {
994 skdev->timer_countdown--;
995 return;
996 }
rchinthekindi2e44b422013-10-24 12:51:23 +0100997 pr_debug("%s:%s:%d busy[%x], timedout=%d, restarting device.",
998 skdev->name, __func__, __LINE__,
999 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001000 skd_restart_device(skdev);
1001 break;
1002
1003 case SKD_DRVR_STATE_WAIT_BOOT:
1004 case SKD_DRVR_STATE_STARTING:
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;
1012
1013 pr_err("(%s): DriveFault Connect Timeout (%x)\n",
1014 skd_name(skdev), skdev->drive_state);
1015
1016 /*start the queue so we can respond with error to requests */
1017 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001018 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001019 skdev->gendisk_on = -1;
1020 wake_up_interruptible(&skdev->waitq);
1021 break;
1022
1023 case SKD_DRVR_STATE_ONLINE:
1024 /* shouldn't get here. */
1025 break;
1026
1027 case SKD_DRVR_STATE_PAUSING:
1028 case SKD_DRVR_STATE_PAUSED:
1029 break;
1030
1031 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
rchinthekindi2e44b422013-10-24 12:51:23 +01001032 pr_debug("%s:%s:%d "
1033 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
1034 skdev->name, __func__, __LINE__,
1035 skdev->timo_slot,
1036 skdev->timer_countdown,
1037 skdev->in_flight,
1038 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001039 /* if the slot has cleared we can let the I/O continue */
1040 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001041 pr_debug("%s:%s:%d Slot drained, starting queue.\n",
1042 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001043 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -06001044 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001045 return;
1046 }
1047 if (skdev->timer_countdown > 0) {
1048 skdev->timer_countdown--;
1049 return;
1050 }
1051 skd_restart_device(skdev);
1052 break;
1053
1054 case SKD_DRVR_STATE_RESTARTING:
1055 if (skdev->timer_countdown > 0) {
1056 skdev->timer_countdown--;
1057 return;
1058 }
1059 /* For now, we fault the drive. Could attempt resets to
1060 * revcover at some point. */
1061 skdev->state = SKD_DRVR_STATE_FAULT;
1062 pr_err("(%s): DriveFault Reconnect Timeout (%x)\n",
1063 skd_name(skdev), skdev->drive_state);
1064
1065 /*
1066 * Recovering does two things:
1067 * 1. completes IO with error
1068 * 2. reclaims dma resources
1069 * When is it safe to recover requests?
1070 * - if the drive state is faulted
1071 * - if the state is still soft reset after out timeout
1072 * - if the drive registers are dead (state = FF)
1073 * If it is "unsafe", we still need to recover, so we will
1074 * disable pci bus mastering and disable our interrupts.
1075 */
1076
1077 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1078 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1079 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1080 /* It never came out of soft reset. Try to
1081 * recover the requests and then let them
1082 * fail. This is to mitigate hung processes. */
1083 skd_recover_requests(skdev, 0);
1084 else {
1085 pr_err("(%s): Disable BusMaster (%x)\n",
1086 skd_name(skdev), skdev->drive_state);
1087 pci_disable_device(skdev->pdev);
1088 skd_disable_interrupts(skdev);
1089 skd_recover_requests(skdev, 0);
1090 }
1091
1092 /*start the queue so we can respond with error to requests */
1093 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001094 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001095 skdev->gendisk_on = -1;
1096 wake_up_interruptible(&skdev->waitq);
1097 break;
1098
1099 case SKD_DRVR_STATE_RESUMING:
1100 case SKD_DRVR_STATE_STOPPING:
1101 case SKD_DRVR_STATE_SYNCING:
1102 case SKD_DRVR_STATE_FAULT:
1103 case SKD_DRVR_STATE_DISAPPEARED:
1104 default:
1105 break;
1106 }
1107}
1108
1109static int skd_start_timer(struct skd_device *skdev)
1110{
1111 int rc;
1112
1113 init_timer(&skdev->timer);
1114 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1115
1116 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1117 if (rc)
1118 pr_err("%s: failed to start timer %d\n",
1119 __func__, rc);
1120 return rc;
1121}
1122
1123static void skd_kill_timer(struct skd_device *skdev)
1124{
1125 del_timer_sync(&skdev->timer);
1126}
1127
1128/*
1129 *****************************************************************************
1130 * IOCTL
1131 *****************************************************************************
1132 */
1133static int skd_ioctl_sg_io(struct skd_device *skdev,
1134 fmode_t mode, void __user *argp);
1135static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1136 struct skd_sg_io *sksgio);
1137static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1138 struct skd_sg_io *sksgio);
1139static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1140 struct skd_sg_io *sksgio);
1141static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1142 struct skd_sg_io *sksgio, int dxfer_dir);
1143static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1144 struct skd_sg_io *sksgio);
1145static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1146static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1147 struct skd_sg_io *sksgio);
1148static int skd_sg_io_put_status(struct skd_device *skdev,
1149 struct skd_sg_io *sksgio);
1150
1151static void skd_complete_special(struct skd_device *skdev,
1152 volatile struct fit_completion_entry_v1
1153 *skcomp,
1154 volatile struct fit_comp_error_info *skerr,
1155 struct skd_special_context *skspcl);
1156
1157static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1158 uint cmd_in, ulong arg)
1159{
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001160 static const int sg_version_num = 30527;
1161 int rc = 0, timeout;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001162 struct gendisk *disk = bdev->bd_disk;
1163 struct skd_device *skdev = disk->private_data;
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001164 int __user *p = (int __user *)arg;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001165
rchinthekindi2e44b422013-10-24 12:51:23 +01001166 pr_debug("%s:%s:%d %s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1167 skdev->name, __func__, __LINE__,
1168 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001169
1170 if (!capable(CAP_SYS_ADMIN))
1171 return -EPERM;
1172
1173 switch (cmd_in) {
1174 case SG_SET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001175 rc = get_user(timeout, p);
1176 if (!rc)
1177 disk->queue->sg_timeout = clock_t_to_jiffies(timeout);
1178 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001179 case SG_GET_TIMEOUT:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001180 rc = jiffies_to_clock_t(disk->queue->sg_timeout);
1181 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001182 case SG_GET_VERSION_NUM:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001183 rc = put_user(sg_version_num, p);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001184 break;
1185 case SG_IO:
Christoph Hellwig3719fa82017-01-28 09:32:50 +01001186 rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001187 break;
1188
1189 default:
1190 rc = -ENOTTY;
1191 break;
1192 }
1193
rchinthekindi2e44b422013-10-24 12:51:23 +01001194 pr_debug("%s:%s:%d %s: completion rc %d\n",
1195 skdev->name, __func__, __LINE__, disk->disk_name, rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001196 return rc;
1197}
1198
1199static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1200 void __user *argp)
1201{
1202 int rc;
1203 struct skd_sg_io sksgio;
1204
1205 memset(&sksgio, 0, sizeof(sksgio));
1206 sksgio.mode = mode;
1207 sksgio.argp = argp;
1208 sksgio.iov = &sksgio.no_iov_iov;
1209
1210 switch (skdev->state) {
1211 case SKD_DRVR_STATE_ONLINE:
1212 case SKD_DRVR_STATE_BUSY_IMMINENT:
1213 break;
1214
1215 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01001216 pr_debug("%s:%s:%d drive not online\n",
1217 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001218 rc = -ENXIO;
1219 goto out;
1220 }
1221
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001222 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1223 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001224 goto out;
1225
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001226 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1227 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001228 goto out;
1229
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001230 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1231 if (rc)
1232 goto out;
1233
1234 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1235 if (rc)
1236 goto out;
1237
1238 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1239 if (rc)
1240 goto out;
1241
1242 rc = skd_sg_io_await(skdev, &sksgio);
1243 if (rc)
1244 goto out;
1245
1246 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1247 if (rc)
1248 goto out;
1249
1250 rc = skd_sg_io_put_status(skdev, &sksgio);
1251 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001252 goto out;
1253
1254 rc = 0;
1255
1256out:
1257 skd_sg_io_release_skspcl(skdev, &sksgio);
1258
1259 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1260 kfree(sksgio.iov);
1261 return rc;
1262}
1263
1264static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1265 struct skd_sg_io *sksgio)
1266{
1267 struct sg_io_hdr *sgp = &sksgio->sg;
Bart Van Assche95895e12017-08-17 13:12:55 -07001268 int i, __maybe_unused acc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001269
1270 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001271 pr_debug("%s:%s:%d access sg failed %p\n",
1272 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001273 return -EFAULT;
1274 }
1275
1276 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001277 pr_debug("%s:%s:%d copy_from_user sg failed %p\n",
1278 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001279 return -EFAULT;
1280 }
1281
1282 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001283 pr_debug("%s:%s:%d interface_id invalid 0x%x\n",
1284 skdev->name, __func__, __LINE__, sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001285 return -EINVAL;
1286 }
1287
1288 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001289 pr_debug("%s:%s:%d cmd_len invalid %d\n",
1290 skdev->name, __func__, __LINE__, sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001291 return -EINVAL;
1292 }
1293
1294 if (sgp->iovec_count > 256) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001295 pr_debug("%s:%s:%d iovec_count invalid %d\n",
1296 skdev->name, __func__, __LINE__, sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001297 return -EINVAL;
1298 }
1299
1300 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001301 pr_debug("%s:%s:%d dxfer_len invalid %d\n",
1302 skdev->name, __func__, __LINE__, sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001303 return -EINVAL;
1304 }
1305
1306 switch (sgp->dxfer_direction) {
1307 case SG_DXFER_NONE:
1308 acc = -1;
1309 break;
1310
1311 case SG_DXFER_TO_DEV:
1312 acc = VERIFY_READ;
1313 break;
1314
1315 case SG_DXFER_FROM_DEV:
1316 case SG_DXFER_TO_FROM_DEV:
1317 acc = VERIFY_WRITE;
1318 break;
1319
1320 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01001321 pr_debug("%s:%s:%d dxfer_dir invalid %d\n",
1322 skdev->name, __func__, __LINE__, sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001323 return -EINVAL;
1324 }
1325
1326 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001327 pr_debug("%s:%s:%d copy_from_user cmdp failed %p\n",
1328 skdev->name, __func__, __LINE__, sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001329 return -EFAULT;
1330 }
1331
1332 if (sgp->mx_sb_len != 0) {
1333 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001334 pr_debug("%s:%s:%d access sbp failed %p\n",
1335 skdev->name, __func__, __LINE__, sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001336 return -EFAULT;
1337 }
1338 }
1339
1340 if (sgp->iovec_count == 0) {
1341 sksgio->iov[0].iov_base = sgp->dxferp;
1342 sksgio->iov[0].iov_len = sgp->dxfer_len;
1343 sksgio->iovcnt = 1;
1344 sksgio->dxfer_len = sgp->dxfer_len;
1345 } else {
1346 struct sg_iovec *iov;
1347 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1348 size_t iov_data_len;
1349
1350 iov = kmalloc(nbytes, GFP_KERNEL);
1351 if (iov == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001352 pr_debug("%s:%s:%d alloc iovec failed %d\n",
1353 skdev->name, __func__, __LINE__,
1354 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001355 return -ENOMEM;
1356 }
1357 sksgio->iov = iov;
1358 sksgio->iovcnt = sgp->iovec_count;
1359
1360 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001361 pr_debug("%s:%s:%d copy_from_user iovec failed %p\n",
1362 skdev->name, __func__, __LINE__, sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001363 return -EFAULT;
1364 }
1365
1366 /*
1367 * Sum up the vecs, making sure they don't overflow
1368 */
1369 iov_data_len = 0;
1370 for (i = 0; i < sgp->iovec_count; i++) {
1371 if (iov_data_len + iov[i].iov_len < iov_data_len)
1372 return -EINVAL;
1373 iov_data_len += iov[i].iov_len;
1374 }
1375
1376 /* SG_IO howto says that the shorter of the two wins */
1377 if (sgp->dxfer_len < iov_data_len) {
1378 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1379 sgp->iovec_count,
1380 sgp->dxfer_len);
1381 sksgio->dxfer_len = sgp->dxfer_len;
1382 } else
1383 sksgio->dxfer_len = iov_data_len;
1384 }
1385
1386 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1387 struct sg_iovec *iov = sksgio->iov;
1388 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1389 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001390 pr_debug("%s:%s:%d access data failed %p/%d\n",
1391 skdev->name, __func__, __LINE__,
1392 iov->iov_base, (int)iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001393 return -EFAULT;
1394 }
1395 }
1396 }
1397
1398 return 0;
1399}
1400
1401static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1402 struct skd_sg_io *sksgio)
1403{
1404 struct skd_special_context *skspcl = NULL;
1405 int rc;
1406
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001407 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001408 ulong flags;
1409
1410 spin_lock_irqsave(&skdev->lock, flags);
1411 skspcl = skdev->skspcl_free_list;
1412 if (skspcl != NULL) {
1413 skdev->skspcl_free_list =
1414 (struct skd_special_context *)skspcl->req.next;
1415 skspcl->req.id += SKD_ID_INCR;
1416 skspcl->req.state = SKD_REQ_STATE_SETUP;
1417 skspcl->orphaned = 0;
1418 skspcl->req.n_sg = 0;
1419 }
1420 spin_unlock_irqrestore(&skdev->lock, flags);
1421
1422 if (skspcl != NULL) {
1423 rc = 0;
1424 break;
1425 }
1426
rchinthekindi2e44b422013-10-24 12:51:23 +01001427 pr_debug("%s:%s:%d blocking\n",
1428 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001429
1430 rc = wait_event_interruptible_timeout(
1431 skdev->waitq,
1432 (skdev->skspcl_free_list != NULL),
1433 msecs_to_jiffies(sksgio->sg.timeout));
1434
rchinthekindi2e44b422013-10-24 12:51:23 +01001435 pr_debug("%s:%s:%d unblocking, rc=%d\n",
1436 skdev->name, __func__, __LINE__, rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001437
1438 if (rc <= 0) {
1439 if (rc == 0)
1440 rc = -ETIMEDOUT;
1441 else
1442 rc = -EINTR;
1443 break;
1444 }
1445 /*
1446 * If we get here rc > 0 meaning the timeout to
1447 * wait_event_interruptible_timeout() had time left, hence the
1448 * sought event -- non-empty free list -- happened.
1449 * Retry the allocation.
1450 */
1451 }
1452 sksgio->skspcl = skspcl;
1453
1454 return rc;
1455}
1456
1457static int skd_skreq_prep_buffering(struct skd_device *skdev,
1458 struct skd_request_context *skreq,
1459 u32 dxfer_len)
1460{
1461 u32 resid = dxfer_len;
1462
1463 /*
1464 * The DMA engine must have aligned addresses and byte counts.
1465 */
1466 resid += (-resid) & 3;
1467 skreq->sg_byte_count = resid;
1468
1469 skreq->n_sg = 0;
1470
1471 while (resid > 0) {
1472 u32 nbytes = PAGE_SIZE;
1473 u32 ix = skreq->n_sg;
1474 struct scatterlist *sg = &skreq->sg[ix];
1475 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1476 struct page *page;
1477
1478 if (nbytes > resid)
1479 nbytes = resid;
1480
1481 page = alloc_page(GFP_KERNEL);
1482 if (page == NULL)
1483 return -ENOMEM;
1484
1485 sg_set_page(sg, page, nbytes, 0);
1486
1487 /* TODO: This should be going through a pci_???()
1488 * routine to do proper mapping. */
1489 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1490 sksg->byte_count = nbytes;
1491
1492 sksg->host_side_addr = sg_phys(sg);
1493
1494 sksg->dev_side_addr = 0;
1495 sksg->next_desc_ptr = skreq->sksg_dma_address +
1496 (ix + 1) * sizeof(*sksg);
1497
1498 skreq->n_sg++;
1499 resid -= nbytes;
1500 }
1501
1502 if (skreq->n_sg > 0) {
1503 u32 ix = skreq->n_sg - 1;
1504 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1505
1506 sksg->control = FIT_SGD_CONTROL_LAST;
1507 sksg->next_desc_ptr = 0;
1508 }
1509
1510 if (unlikely(skdev->dbg_level > 1)) {
1511 u32 i;
1512
rchinthekindi2e44b422013-10-24 12:51:23 +01001513 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
1514 skdev->name, __func__, __LINE__,
1515 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001516 for (i = 0; i < skreq->n_sg; i++) {
1517 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1518
rchinthekindi2e44b422013-10-24 12:51:23 +01001519 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
1520 "addr=0x%llx next=0x%llx\n",
1521 skdev->name, __func__, __LINE__,
1522 i, sgd->byte_count, sgd->control,
1523 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001524 }
1525 }
1526
1527 return 0;
1528}
1529
1530static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1531 struct skd_sg_io *sksgio)
1532{
1533 struct skd_special_context *skspcl = sksgio->skspcl;
1534 struct skd_request_context *skreq = &skspcl->req;
1535 u32 dxfer_len = sksgio->dxfer_len;
1536 int rc;
1537
1538 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1539 /*
1540 * Eventually, errors or not, skd_release_special() is called
1541 * to recover allocations including partial allocations.
1542 */
1543 return rc;
1544}
1545
1546static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1547 struct skd_sg_io *sksgio, int dxfer_dir)
1548{
1549 struct skd_special_context *skspcl = sksgio->skspcl;
1550 u32 iov_ix = 0;
1551 struct sg_iovec curiov;
1552 u32 sksg_ix = 0;
1553 u8 *bufp = NULL;
1554 u32 buf_len = 0;
1555 u32 resid = sksgio->dxfer_len;
1556 int rc;
1557
1558 curiov.iov_len = 0;
1559 curiov.iov_base = NULL;
1560
1561 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1562 if (dxfer_dir != SG_DXFER_TO_DEV ||
1563 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1564 return 0;
1565 }
1566
1567 while (resid > 0) {
1568 u32 nbytes = PAGE_SIZE;
1569
1570 if (curiov.iov_len == 0) {
1571 curiov = sksgio->iov[iov_ix++];
1572 continue;
1573 }
1574
1575 if (buf_len == 0) {
1576 struct page *page;
1577 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1578 bufp = page_address(page);
1579 buf_len = PAGE_SIZE;
1580 }
1581
1582 nbytes = min_t(u32, nbytes, resid);
1583 nbytes = min_t(u32, nbytes, curiov.iov_len);
1584 nbytes = min_t(u32, nbytes, buf_len);
1585
1586 if (dxfer_dir == SG_DXFER_TO_DEV)
1587 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1588 else
1589 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1590
1591 if (rc)
1592 return -EFAULT;
1593
1594 resid -= nbytes;
1595 curiov.iov_len -= nbytes;
1596 curiov.iov_base += nbytes;
1597 buf_len -= nbytes;
1598 }
1599
1600 return 0;
1601}
1602
1603static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1604 struct skd_sg_io *sksgio)
1605{
1606 struct skd_special_context *skspcl = sksgio->skspcl;
1607 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
1608 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
1609
1610 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1611
1612 /* Initialize the FIT msg header */
1613 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1614 fmh->num_protocol_cmds_coalesced = 1;
1615
1616 /* Initialize the SCSI request */
1617 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1618 scsi_req->hdr.sg_list_dma_address =
1619 cpu_to_be64(skspcl->req.sksg_dma_address);
1620 scsi_req->hdr.tag = skspcl->req.id;
1621 scsi_req->hdr.sg_list_len_bytes =
1622 cpu_to_be32(skspcl->req.sg_byte_count);
1623 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1624
1625 skspcl->req.state = SKD_REQ_STATE_BUSY;
1626 skd_send_special_fitmsg(skdev, skspcl);
1627
1628 return 0;
1629}
1630
1631static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1632{
1633 unsigned long flags;
1634 int rc;
1635
1636 rc = wait_event_interruptible_timeout(skdev->waitq,
1637 (sksgio->skspcl->req.state !=
1638 SKD_REQ_STATE_BUSY),
1639 msecs_to_jiffies(sksgio->sg.
1640 timeout));
1641
1642 spin_lock_irqsave(&skdev->lock, flags);
1643
1644 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001645 pr_debug("%s:%s:%d skspcl %p aborted\n",
1646 skdev->name, __func__, __LINE__, sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001647
1648 /* Build check cond, sense and let command finish. */
1649 /* For a timeout, we must fabricate completion and sense
1650 * data to complete the command */
1651 sksgio->skspcl->req.completion.status =
1652 SAM_STAT_CHECK_CONDITION;
1653
1654 memset(&sksgio->skspcl->req.err_info, 0,
1655 sizeof(sksgio->skspcl->req.err_info));
1656 sksgio->skspcl->req.err_info.type = 0x70;
1657 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1658 sksgio->skspcl->req.err_info.code = 0x44;
1659 sksgio->skspcl->req.err_info.qual = 0;
1660 rc = 0;
1661 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1662 /* No longer on the adapter. We finish. */
1663 rc = 0;
1664 else {
1665 /* Something's gone wrong. Still busy. Timeout or
1666 * user interrupted (control-C). Mark as an orphan
1667 * so it will be disposed when completed. */
1668 sksgio->skspcl->orphaned = 1;
1669 sksgio->skspcl = NULL;
1670 if (rc == 0) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001671 pr_debug("%s:%s:%d timed out %p (%u ms)\n",
1672 skdev->name, __func__, __LINE__,
1673 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001674 rc = -ETIMEDOUT;
1675 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01001676 pr_debug("%s:%s:%d cntlc %p\n",
1677 skdev->name, __func__, __LINE__, sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001678 rc = -EINTR;
1679 }
1680 }
1681
1682 spin_unlock_irqrestore(&skdev->lock, flags);
1683
1684 return rc;
1685}
1686
1687static int skd_sg_io_put_status(struct skd_device *skdev,
1688 struct skd_sg_io *sksgio)
1689{
1690 struct sg_io_hdr *sgp = &sksgio->sg;
1691 struct skd_special_context *skspcl = sksgio->skspcl;
1692 int resid = 0;
1693
1694 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1695
1696 sgp->status = skspcl->req.completion.status;
1697 resid = sksgio->dxfer_len - nb;
1698
1699 sgp->masked_status = sgp->status & STATUS_MASK;
1700 sgp->msg_status = 0;
1701 sgp->host_status = 0;
1702 sgp->driver_status = 0;
1703 sgp->resid = resid;
1704 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1705 sgp->info |= SG_INFO_CHECK;
1706
rchinthekindi2e44b422013-10-24 12:51:23 +01001707 pr_debug("%s:%s:%d status %x masked %x resid 0x%x\n",
1708 skdev->name, __func__, __LINE__,
1709 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001710
1711 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1712 if (sgp->mx_sb_len > 0) {
1713 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1714 u32 nbytes = sizeof(*ei);
1715
1716 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1717
1718 sgp->sb_len_wr = nbytes;
1719
1720 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001721 pr_debug("%s:%s:%d copy_to_user sense failed %p\n",
1722 skdev->name, __func__, __LINE__,
1723 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001724 return -EFAULT;
1725 }
1726 }
1727 }
1728
1729 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001730 pr_debug("%s:%s:%d copy_to_user sg failed %p\n",
1731 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001732 return -EFAULT;
1733 }
1734
1735 return 0;
1736}
1737
1738static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1739 struct skd_sg_io *sksgio)
1740{
1741 struct skd_special_context *skspcl = sksgio->skspcl;
1742
1743 if (skspcl != NULL) {
1744 ulong flags;
1745
1746 sksgio->skspcl = NULL;
1747
1748 spin_lock_irqsave(&skdev->lock, flags);
1749 skd_release_special(skdev, skspcl);
1750 spin_unlock_irqrestore(&skdev->lock, flags);
1751 }
1752
1753 return 0;
1754}
1755
1756/*
1757 *****************************************************************************
1758 * INTERNAL REQUESTS -- generated by driver itself
1759 *****************************************************************************
1760 */
1761
1762static int skd_format_internal_skspcl(struct skd_device *skdev)
1763{
1764 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1765 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1766 struct fit_msg_hdr *fmh;
1767 uint64_t dma_address;
1768 struct skd_scsi_request *scsi;
1769
1770 fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0];
1771 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1772 fmh->num_protocol_cmds_coalesced = 1;
1773
1774 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1775 memset(scsi, 0, sizeof(*scsi));
1776 dma_address = skspcl->req.sksg_dma_address;
1777 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1778 sgd->control = FIT_SGD_CONTROL_LAST;
1779 sgd->byte_count = 0;
1780 sgd->host_side_addr = skspcl->db_dma_address;
1781 sgd->dev_side_addr = 0;
1782 sgd->next_desc_ptr = 0LL;
1783
1784 return 1;
1785}
1786
1787#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1788
1789static void skd_send_internal_skspcl(struct skd_device *skdev,
1790 struct skd_special_context *skspcl,
1791 u8 opcode)
1792{
1793 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1794 struct skd_scsi_request *scsi;
1795 unsigned char *buf = skspcl->data_buf;
1796 int i;
1797
1798 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1799 /*
1800 * A refresh is already in progress.
1801 * Just wait for it to finish.
1802 */
1803 return;
1804
1805 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1806 skspcl->req.state = SKD_REQ_STATE_BUSY;
1807 skspcl->req.id += SKD_ID_INCR;
1808
1809 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1810 scsi->hdr.tag = skspcl->req.id;
1811
1812 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1813
1814 switch (opcode) {
1815 case TEST_UNIT_READY:
1816 scsi->cdb[0] = TEST_UNIT_READY;
1817 sgd->byte_count = 0;
1818 scsi->hdr.sg_list_len_bytes = 0;
1819 break;
1820
1821 case READ_CAPACITY:
1822 scsi->cdb[0] = READ_CAPACITY;
1823 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1824 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1825 break;
1826
1827 case INQUIRY:
1828 scsi->cdb[0] = INQUIRY;
1829 scsi->cdb[1] = 0x01; /* evpd */
1830 scsi->cdb[2] = 0x80; /* serial number page */
1831 scsi->cdb[4] = 0x10;
1832 sgd->byte_count = 16;
1833 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1834 break;
1835
1836 case SYNCHRONIZE_CACHE:
1837 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1838 sgd->byte_count = 0;
1839 scsi->hdr.sg_list_len_bytes = 0;
1840 break;
1841
1842 case WRITE_BUFFER:
1843 scsi->cdb[0] = WRITE_BUFFER;
1844 scsi->cdb[1] = 0x02;
1845 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1846 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1847 sgd->byte_count = WR_BUF_SIZE;
1848 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1849 /* fill incrementing byte pattern */
1850 for (i = 0; i < sgd->byte_count; i++)
1851 buf[i] = i & 0xFF;
1852 break;
1853
1854 case READ_BUFFER:
1855 scsi->cdb[0] = READ_BUFFER;
1856 scsi->cdb[1] = 0x02;
1857 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1858 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1859 sgd->byte_count = WR_BUF_SIZE;
1860 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1861 memset(skspcl->data_buf, 0, sgd->byte_count);
1862 break;
1863
1864 default:
1865 SKD_ASSERT("Don't know what to send");
1866 return;
1867
1868 }
1869 skd_send_special_fitmsg(skdev, skspcl);
1870}
1871
1872static void skd_refresh_device_data(struct skd_device *skdev)
1873{
1874 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1875
1876 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1877}
1878
1879static int skd_chk_read_buf(struct skd_device *skdev,
1880 struct skd_special_context *skspcl)
1881{
1882 unsigned char *buf = skspcl->data_buf;
1883 int i;
1884
1885 /* check for incrementing byte pattern */
1886 for (i = 0; i < WR_BUF_SIZE; i++)
1887 if (buf[i] != (i & 0xFF))
1888 return 1;
1889
1890 return 0;
1891}
1892
1893static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1894 u8 code, u8 qual, u8 fruc)
1895{
1896 /* If the check condition is of special interest, log a message */
1897 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1898 && (code == 0x04) && (qual == 0x06)) {
1899 pr_err("(%s): *** LOST_WRITE_DATA ERROR *** key/asc/"
1900 "ascq/fruc %02x/%02x/%02x/%02x\n",
1901 skd_name(skdev), key, code, qual, fruc);
1902 }
1903}
1904
1905static void skd_complete_internal(struct skd_device *skdev,
1906 volatile struct fit_completion_entry_v1
1907 *skcomp,
1908 volatile struct fit_comp_error_info *skerr,
1909 struct skd_special_context *skspcl)
1910{
1911 u8 *buf = skspcl->data_buf;
1912 u8 status;
1913 int i;
1914 struct skd_scsi_request *scsi =
1915 (struct skd_scsi_request *)&skspcl->msg_buf[64];
1916
1917 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1918
rchinthekindi2e44b422013-10-24 12:51:23 +01001919 pr_debug("%s:%s:%d complete internal %x\n",
1920 skdev->name, __func__, __LINE__, scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001921
1922 skspcl->req.completion = *skcomp;
1923 skspcl->req.state = SKD_REQ_STATE_IDLE;
1924 skspcl->req.id += SKD_ID_INCR;
1925
1926 status = skspcl->req.completion.status;
1927
1928 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1929 skerr->qual, skerr->fruc);
1930
1931 switch (scsi->cdb[0]) {
1932 case TEST_UNIT_READY:
1933 if (status == SAM_STAT_GOOD)
1934 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1935 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1936 (skerr->key == MEDIUM_ERROR))
1937 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1938 else {
1939 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001940 pr_debug("%s:%s:%d TUR failed, don't send anymore state 0x%x\n",
1941 skdev->name, __func__, __LINE__,
1942 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001943 return;
1944 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001945 pr_debug("%s:%s:%d **** TUR failed, retry skerr\n",
1946 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001947 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1948 }
1949 break;
1950
1951 case WRITE_BUFFER:
1952 if (status == SAM_STAT_GOOD)
1953 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1954 else {
1955 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001956 pr_debug("%s:%s:%d write buffer failed, don't send anymore state 0x%x\n",
1957 skdev->name, __func__, __LINE__,
1958 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001959 return;
1960 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001961 pr_debug("%s:%s:%d **** write buffer failed, retry skerr\n",
1962 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001963 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1964 }
1965 break;
1966
1967 case READ_BUFFER:
1968 if (status == SAM_STAT_GOOD) {
1969 if (skd_chk_read_buf(skdev, skspcl) == 0)
1970 skd_send_internal_skspcl(skdev, skspcl,
1971 READ_CAPACITY);
1972 else {
Bart Van Assche5477e1b2017-08-17 13:12:51 -07001973 pr_err("(%s):*** W/R Buffer mismatch %d ***\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001974 skd_name(skdev), skdev->connect_retries);
1975 if (skdev->connect_retries <
1976 SKD_MAX_CONNECT_RETRIES) {
1977 skdev->connect_retries++;
1978 skd_soft_reset(skdev);
1979 } else {
Bart Van Assche5477e1b2017-08-17 13:12:51 -07001980 pr_err("(%s): W/R Buffer Connect Error\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001981 skd_name(skdev));
1982 return;
1983 }
1984 }
1985
1986 } else {
1987 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001988 pr_debug("%s:%s:%d "
1989 "read buffer failed, don't send anymore state 0x%x\n",
1990 skdev->name, __func__, __LINE__,
1991 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001992 return;
1993 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001994 pr_debug("%s:%s:%d "
1995 "**** read buffer failed, retry skerr\n",
1996 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001997 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1998 }
1999 break;
2000
2001 case READ_CAPACITY:
2002 skdev->read_cap_is_valid = 0;
2003 if (status == SAM_STAT_GOOD) {
2004 skdev->read_cap_last_lba =
2005 (buf[0] << 24) | (buf[1] << 16) |
2006 (buf[2] << 8) | buf[3];
2007 skdev->read_cap_blocksize =
2008 (buf[4] << 24) | (buf[5] << 16) |
2009 (buf[6] << 8) | buf[7];
2010
rchinthekindi2e44b422013-10-24 12:51:23 +01002011 pr_debug("%s:%s:%d last lba %d, bs %d\n",
2012 skdev->name, __func__, __LINE__,
2013 skdev->read_cap_last_lba,
2014 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002015
2016 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
2017
2018 skdev->read_cap_is_valid = 1;
2019
2020 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2021 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
2022 (skerr->key == MEDIUM_ERROR)) {
2023 skdev->read_cap_last_lba = ~0;
2024 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
rchinthekindi2e44b422013-10-24 12:51:23 +01002025 pr_debug("%s:%s:%d "
2026 "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n",
2027 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002028 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2029 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01002030 pr_debug("%s:%s:%d **** READCAP failed, retry TUR\n",
2031 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002032 skd_send_internal_skspcl(skdev, skspcl,
2033 TEST_UNIT_READY);
2034 }
2035 break;
2036
2037 case INQUIRY:
2038 skdev->inquiry_is_valid = 0;
2039 if (status == SAM_STAT_GOOD) {
2040 skdev->inquiry_is_valid = 1;
2041
2042 for (i = 0; i < 12; i++)
2043 skdev->inq_serial_num[i] = buf[i + 4];
2044 skdev->inq_serial_num[12] = 0;
2045 }
2046
2047 if (skd_unquiesce_dev(skdev) < 0)
rchinthekindi2e44b422013-10-24 12:51:23 +01002048 pr_debug("%s:%s:%d **** failed, to ONLINE device\n",
2049 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002050 /* connection is complete */
2051 skdev->connect_retries = 0;
2052 break;
2053
2054 case SYNCHRONIZE_CACHE:
2055 if (status == SAM_STAT_GOOD)
2056 skdev->sync_done = 1;
2057 else
2058 skdev->sync_done = -1;
2059 wake_up_interruptible(&skdev->waitq);
2060 break;
2061
2062 default:
2063 SKD_ASSERT("we didn't send this");
2064 }
2065}
2066
2067/*
2068 *****************************************************************************
2069 * FIT MESSAGES
2070 *****************************************************************************
2071 */
2072
2073static void skd_send_fitmsg(struct skd_device *skdev,
2074 struct skd_fitmsg_context *skmsg)
2075{
2076 u64 qcmd;
2077 struct fit_msg_hdr *fmh;
2078
rchinthekindi2e44b422013-10-24 12:51:23 +01002079 pr_debug("%s:%s:%d dma address 0x%llx, busy=%d\n",
2080 skdev->name, __func__, __LINE__,
2081 skmsg->mb_dma_address, skdev->in_flight);
2082 pr_debug("%s:%s:%d msg_buf 0x%p, offset %x\n",
2083 skdev->name, __func__, __LINE__,
2084 skmsg->msg_buf, skmsg->offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002085
2086 qcmd = skmsg->mb_dma_address;
2087 qcmd |= FIT_QCMD_QID_NORMAL;
2088
2089 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
2090 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2091
2092 if (unlikely(skdev->dbg_level > 1)) {
2093 u8 *bp = (u8 *)skmsg->msg_buf;
2094 int i;
2095 for (i = 0; i < skmsg->length; i += 8) {
Andy Shevchenko8e1de262016-10-22 19:52:01 +03002096 pr_debug("%s:%s:%d msg[%2d] %8ph\n",
2097 skdev->name, __func__, __LINE__, i, &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002098 if (i == 0)
2099 i = 64 - 8;
2100 }
2101 }
2102
2103 if (skmsg->length > 256)
2104 qcmd |= FIT_QCMD_MSGSIZE_512;
2105 else if (skmsg->length > 128)
2106 qcmd |= FIT_QCMD_MSGSIZE_256;
2107 else if (skmsg->length > 64)
2108 qcmd |= FIT_QCMD_MSGSIZE_128;
2109 else
2110 /*
2111 * This makes no sense because the FIT msg header is
2112 * 64 bytes. If the msg is only 64 bytes long it has
2113 * no payload.
2114 */
2115 qcmd |= FIT_QCMD_MSGSIZE_64;
2116
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002117 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2118 smp_wmb();
2119
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002120 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002121}
2122
2123static void skd_send_special_fitmsg(struct skd_device *skdev,
2124 struct skd_special_context *skspcl)
2125{
2126 u64 qcmd;
2127
2128 if (unlikely(skdev->dbg_level > 1)) {
2129 u8 *bp = (u8 *)skspcl->msg_buf;
2130 int i;
2131
2132 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
Andy Shevchenko8e1de262016-10-22 19:52:01 +03002133 pr_debug("%s:%s:%d spcl[%2d] %8ph\n",
2134 skdev->name, __func__, __LINE__, i, &bp[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002135 if (i == 0)
2136 i = 64 - 8;
2137 }
2138
rchinthekindi2e44b422013-10-24 12:51:23 +01002139 pr_debug("%s:%s:%d skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2140 skdev->name, __func__, __LINE__,
2141 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2142 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002143 for (i = 0; i < skspcl->req.n_sg; i++) {
2144 struct fit_sg_descriptor *sgd =
2145 &skspcl->req.sksg_list[i];
2146
rchinthekindi2e44b422013-10-24 12:51:23 +01002147 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
2148 "addr=0x%llx next=0x%llx\n",
2149 skdev->name, __func__, __LINE__,
2150 i, sgd->byte_count, sgd->control,
2151 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002152 }
2153 }
2154
2155 /*
2156 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2157 * and one 64-byte SSDI command.
2158 */
2159 qcmd = skspcl->mb_dma_address;
2160 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2161
Bart Van Assche5fbd5452017-08-17 13:12:46 -07002162 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
2163 smp_wmb();
2164
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002165 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2166}
2167
2168/*
2169 *****************************************************************************
2170 * COMPLETION QUEUE
2171 *****************************************************************************
2172 */
2173
2174static void skd_complete_other(struct skd_device *skdev,
2175 volatile struct fit_completion_entry_v1 *skcomp,
2176 volatile struct fit_comp_error_info *skerr);
2177
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002178struct sns_info {
2179 u8 type;
2180 u8 stat;
2181 u8 key;
2182 u8 asc;
2183 u8 ascq;
2184 u8 mask;
2185 enum skd_check_status_action action;
2186};
2187
2188static struct sns_info skd_chkstat_table[] = {
2189 /* Good */
2190 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2191 SKD_CHECK_STATUS_REPORT_GOOD },
2192
2193 /* Smart alerts */
2194 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2195 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2196 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2197 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2198 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2199 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2200
2201 /* Retry (with limits) */
2202 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2203 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2204 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2205 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2206 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2207 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2208 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2209 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2210
2211 /* Busy (or about to be) */
2212 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2213 SKD_CHECK_STATUS_BUSY_IMMINENT },
2214};
2215
2216/*
2217 * Look up status and sense data to decide how to handle the error
2218 * from the device.
2219 * mask says which fields must match e.g., mask=0x18 means check
2220 * type and stat, ignore key, asc, ascq.
2221 */
2222
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002223static enum skd_check_status_action
2224skd_check_status(struct skd_device *skdev,
2225 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002226{
2227 int i, n;
2228
2229 pr_err("(%s): key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2230 skd_name(skdev), skerr->key, skerr->code, skerr->qual,
2231 skerr->fruc);
2232
rchinthekindi2e44b422013-10-24 12:51:23 +01002233 pr_debug("%s:%s:%d stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2234 skdev->name, __func__, __LINE__, skerr->type, cmp_status,
2235 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002236
2237 /* Does the info match an entry in the good category? */
2238 n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
2239 for (i = 0; i < n; i++) {
2240 struct sns_info *sns = &skd_chkstat_table[i];
2241
2242 if (sns->mask & 0x10)
2243 if (skerr->type != sns->type)
2244 continue;
2245
2246 if (sns->mask & 0x08)
2247 if (cmp_status != sns->stat)
2248 continue;
2249
2250 if (sns->mask & 0x04)
2251 if (skerr->key != sns->key)
2252 continue;
2253
2254 if (sns->mask & 0x02)
2255 if (skerr->code != sns->asc)
2256 continue;
2257
2258 if (sns->mask & 0x01)
2259 if (skerr->qual != sns->ascq)
2260 continue;
2261
2262 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
2263 pr_err("(%s): SMART Alert: sense key/asc/ascq "
2264 "%02x/%02x/%02x\n",
2265 skd_name(skdev), skerr->key,
2266 skerr->code, skerr->qual);
2267 }
2268 return sns->action;
2269 }
2270
2271 /* No other match, so nonzero status means error,
2272 * zero status means good
2273 */
2274 if (cmp_status) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002275 pr_debug("%s:%s:%d status check: error\n",
2276 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002277 return SKD_CHECK_STATUS_REPORT_ERROR;
2278 }
2279
rchinthekindi2e44b422013-10-24 12:51:23 +01002280 pr_debug("%s:%s:%d status check good default\n",
2281 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002282 return SKD_CHECK_STATUS_REPORT_GOOD;
2283}
2284
2285static void skd_resolve_req_exception(struct skd_device *skdev,
2286 struct skd_request_context *skreq)
2287{
2288 u8 cmp_status = skreq->completion.status;
2289
2290 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2291 case SKD_CHECK_STATUS_REPORT_GOOD:
2292 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002293 skd_end_request(skdev, skreq, BLK_STS_OK);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002294 break;
2295
2296 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2297 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002298 blk_requeue_request(skdev->queue, skreq->req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002299 pr_info("(%s) drive BUSY imminent\n", skd_name(skdev));
2300 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2301 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2302 skd_quiesce_dev(skdev);
2303 break;
2304
2305 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002306 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2307 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002308 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002309 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002310 }
Bart Van Asschece6882b2017-08-17 13:12:52 -07002311 /* fall through */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002312
2313 case SKD_CHECK_STATUS_REPORT_ERROR:
2314 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002315 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002316 break;
2317 }
2318}
2319
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002320/* assume spinlock is already held */
2321static void skd_release_skreq(struct skd_device *skdev,
2322 struct skd_request_context *skreq)
2323{
2324 u32 msg_slot;
2325 struct skd_fitmsg_context *skmsg;
2326
2327 u32 timo_slot;
2328
2329 /*
2330 * Reclaim the FIT msg buffer if this is
2331 * the first of the requests it carried to
2332 * be completed. The FIT msg buffer used to
2333 * send this request cannot be reused until
2334 * we are sure the s1120 card has copied
2335 * it to its memory. The FIT msg might have
2336 * contained several requests. As soon as
2337 * any of them are completed we know that
2338 * the entire FIT msg was transferred.
2339 * Only the first completed request will
2340 * match the FIT msg buffer id. The FIT
2341 * msg buffer id is immediately updated.
2342 * When subsequent requests complete the FIT
2343 * msg buffer id won't match, so we know
2344 * quite cheaply that it is already done.
2345 */
2346 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2347 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2348
2349 skmsg = &skdev->skmsg_table[msg_slot];
2350 if (skmsg->id == skreq->fitmsg_id) {
2351 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2352 SKD_ASSERT(skmsg->outstanding > 0);
2353 skmsg->outstanding--;
2354 if (skmsg->outstanding == 0) {
2355 skmsg->state = SKD_MSG_STATE_IDLE;
2356 skmsg->id += SKD_ID_INCR;
2357 skmsg->next = skdev->skmsg_free_list;
2358 skdev->skmsg_free_list = skmsg;
2359 }
2360 }
2361
2362 /*
2363 * Decrease the number of active requests.
2364 * Also decrements the count in the timeout slot.
2365 */
2366 SKD_ASSERT(skdev->in_flight > 0);
2367 skdev->in_flight -= 1;
2368
2369 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2370 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2371 skdev->timeout_slot[timo_slot] -= 1;
2372
2373 /*
2374 * Reset backpointer
2375 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002376 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002377
2378 /*
2379 * Reclaim the skd_request_context
2380 */
2381 skreq->state = SKD_REQ_STATE_IDLE;
2382 skreq->id += SKD_ID_INCR;
2383 skreq->next = skdev->skreq_free_list;
2384 skdev->skreq_free_list = skreq;
2385}
2386
2387#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2388
2389static void skd_do_inq_page_00(struct skd_device *skdev,
2390 volatile struct fit_completion_entry_v1 *skcomp,
2391 volatile struct fit_comp_error_info *skerr,
2392 uint8_t *cdb, uint8_t *buf)
2393{
2394 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2395
2396 /* Caller requested "supported pages". The driver needs to insert
2397 * its page.
2398 */
rchinthekindi2e44b422013-10-24 12:51:23 +01002399 pr_debug("%s:%s:%d skd_do_driver_inquiry: modify supported pages.\n",
2400 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002401
2402 /* If the device rejected the request because the CDB was
2403 * improperly formed, then just leave.
2404 */
2405 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2406 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2407 return;
2408
2409 /* Get the amount of space the caller allocated */
2410 max_bytes = (cdb[3] << 8) | cdb[4];
2411
2412 /* Get the number of pages actually returned by the device */
2413 drive_pages = (buf[2] << 8) | buf[3];
2414 drive_bytes = drive_pages + 4;
2415 new_size = drive_pages + 1;
2416
2417 /* Supported pages must be in numerical order, so find where
2418 * the driver page needs to be inserted into the list of
2419 * pages returned by the device.
2420 */
2421 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2422 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2423 return; /* Device using this page code. abort */
2424 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2425 break;
2426 }
2427
2428 if (insert_pt < max_bytes) {
2429 uint16_t u;
2430
2431 /* Shift everything up one byte to make room. */
2432 for (u = new_size + 3; u > insert_pt; u--)
2433 buf[u] = buf[u - 1];
2434 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2435
2436 /* SCSI byte order increment of num_returned_bytes by 1 */
2437 skcomp->num_returned_bytes =
2438 be32_to_cpu(skcomp->num_returned_bytes) + 1;
2439 skcomp->num_returned_bytes =
2440 be32_to_cpu(skcomp->num_returned_bytes);
2441 }
2442
2443 /* update page length field to reflect the driver's page too */
2444 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2445 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2446}
2447
2448static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2449{
2450 int pcie_reg;
2451 u16 pci_bus_speed;
2452 u8 pci_lanes;
2453
2454 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2455 if (pcie_reg) {
2456 u16 linksta;
2457 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2458
2459 pci_bus_speed = linksta & 0xF;
2460 pci_lanes = (linksta & 0x3F0) >> 4;
2461 } else {
2462 *speed = STEC_LINK_UNKNOWN;
2463 *width = 0xFF;
2464 return;
2465 }
2466
2467 switch (pci_bus_speed) {
2468 case 1:
2469 *speed = STEC_LINK_2_5GTS;
2470 break;
2471 case 2:
2472 *speed = STEC_LINK_5GTS;
2473 break;
2474 case 3:
2475 *speed = STEC_LINK_8GTS;
2476 break;
2477 default:
2478 *speed = STEC_LINK_UNKNOWN;
2479 break;
2480 }
2481
2482 if (pci_lanes <= 0x20)
2483 *width = pci_lanes;
2484 else
2485 *width = 0xFF;
2486}
2487
2488static void skd_do_inq_page_da(struct skd_device *skdev,
2489 volatile struct fit_completion_entry_v1 *skcomp,
2490 volatile struct fit_comp_error_info *skerr,
2491 uint8_t *cdb, uint8_t *buf)
2492{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002493 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002494 unsigned max_bytes;
2495 struct driver_inquiry_data inq;
2496 u16 val;
2497
rchinthekindi2e44b422013-10-24 12:51:23 +01002498 pr_debug("%s:%s:%d skd_do_driver_inquiry: return driver page\n",
2499 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002500
2501 memset(&inq, 0, sizeof(inq));
2502
2503 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2504
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002505 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2506 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2507 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2508 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002509
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002510 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2511 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002512
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002513 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2514 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002515
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002516 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2517 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002518
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002519 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2520 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002521
2522 /* Driver version, fixed lenth, padded with spaces on the right */
2523 inq.driver_version_length = sizeof(inq.driver_version);
2524 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2525 memcpy(inq.driver_version, DRV_VER_COMPL,
2526 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2527
2528 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2529
2530 /* Clear the error set by the device */
2531 skcomp->status = SAM_STAT_GOOD;
2532 memset((void *)skerr, 0, sizeof(*skerr));
2533
2534 /* copy response into output buffer */
2535 max_bytes = (cdb[3] << 8) | cdb[4];
2536 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2537
2538 skcomp->num_returned_bytes =
2539 be32_to_cpu(min_t(uint16_t, max_bytes, sizeof(inq)));
2540}
2541
2542static void skd_do_driver_inq(struct skd_device *skdev,
2543 volatile struct fit_completion_entry_v1 *skcomp,
2544 volatile struct fit_comp_error_info *skerr,
2545 uint8_t *cdb, uint8_t *buf)
2546{
2547 if (!buf)
2548 return;
2549 else if (cdb[0] != INQUIRY)
2550 return; /* Not an INQUIRY */
2551 else if ((cdb[1] & 1) == 0)
2552 return; /* EVPD not set */
2553 else if (cdb[2] == 0)
2554 /* Need to add driver's page to supported pages list */
2555 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2556 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2557 /* Caller requested driver's page */
2558 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2559}
2560
2561static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2562{
2563 if (!sg)
2564 return NULL;
2565 if (!sg_page(sg))
2566 return NULL;
2567 return sg_virt(sg);
2568}
2569
2570static void skd_process_scsi_inq(struct skd_device *skdev,
2571 volatile struct fit_completion_entry_v1
2572 *skcomp,
2573 volatile struct fit_comp_error_info *skerr,
2574 struct skd_special_context *skspcl)
2575{
2576 uint8_t *buf;
2577 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
2578 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
2579
2580 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2581 skspcl->req.sg_data_dir);
2582 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2583
2584 if (buf)
2585 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2586}
2587
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002588static int skd_isr_completion_posted(struct skd_device *skdev,
2589 int limit, int *enqueued)
2590{
2591 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2592 volatile struct fit_comp_error_info *skerr;
2593 u16 req_id;
2594 u32 req_slot;
2595 struct skd_request_context *skreq;
2596 u16 cmp_cntxt = 0;
2597 u8 cmp_status = 0;
2598 u8 cmp_cycle = 0;
2599 u32 cmp_bytes = 0;
2600 int rc = 0;
2601 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002602
2603 for (;; ) {
2604 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2605
2606 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2607 cmp_cycle = skcmp->cycle;
2608 cmp_cntxt = skcmp->tag;
2609 cmp_status = skcmp->status;
2610 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2611
2612 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2613
rchinthekindi2e44b422013-10-24 12:51:23 +01002614 pr_debug("%s:%s:%d "
2615 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d "
2616 "busy=%d rbytes=0x%x proto=%d\n",
2617 skdev->name, __func__, __LINE__, skdev->skcomp_cycle,
2618 skdev->skcomp_ix, cmp_cycle, cmp_cntxt, cmp_status,
2619 skdev->in_flight, cmp_bytes, skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002620
2621 if (cmp_cycle != skdev->skcomp_cycle) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002622 pr_debug("%s:%s:%d end of completions\n",
2623 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002624 break;
2625 }
2626 /*
2627 * Update the completion queue head index and possibly
2628 * the completion cycle count. 8-bit wrap-around.
2629 */
2630 skdev->skcomp_ix++;
2631 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2632 skdev->skcomp_ix = 0;
2633 skdev->skcomp_cycle++;
2634 }
2635
2636 /*
2637 * The command context is a unique 32-bit ID. The low order
2638 * bits help locate the request. The request is usually a
2639 * r/w request (see skd_start() above) or a special request.
2640 */
2641 req_id = cmp_cntxt;
2642 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2643
2644 /* Is this other than a r/w request? */
2645 if (req_slot >= skdev->num_req_context) {
2646 /*
2647 * This is not a completion for a r/w request.
2648 */
2649 skd_complete_other(skdev, skcmp, skerr);
2650 continue;
2651 }
2652
2653 skreq = &skdev->skreq_table[req_slot];
2654
2655 /*
2656 * Make sure the request ID for the slot matches.
2657 */
2658 if (skreq->id != req_id) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002659 pr_debug("%s:%s:%d mismatch comp_id=0x%x req_id=0x%x\n",
2660 skdev->name, __func__, __LINE__,
2661 req_id, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002662 {
2663 u16 new_id = cmp_cntxt;
2664 pr_err("(%s): Completion mismatch "
2665 "comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2666 skd_name(skdev), req_id,
2667 skreq->id, new_id);
2668
2669 continue;
2670 }
2671 }
2672
2673 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2674
2675 if (skreq->state == SKD_REQ_STATE_ABORTED) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002676 pr_debug("%s:%s:%d reclaim req %p id=%04x\n",
2677 skdev->name, __func__, __LINE__,
2678 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002679 /* a previously timed out command can
2680 * now be cleaned up */
2681 skd_release_skreq(skdev, skreq);
2682 continue;
2683 }
2684
2685 skreq->completion = *skcmp;
2686 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2687 skreq->err_info = *skerr;
2688 skd_log_check_status(skdev, cmp_status, skerr->key,
2689 skerr->code, skerr->qual,
2690 skerr->fruc);
2691 }
2692 /* Release DMA resources for the request. */
2693 if (skreq->n_sg > 0)
2694 skd_postop_sg_list(skdev, skreq);
2695
Jens Axboefcd37eb2013-11-01 10:14:56 -06002696 if (!skreq->req) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002697 pr_debug("%s:%s:%d NULL backptr skdreq %p, "
2698 "req=0x%x req_id=0x%x\n",
2699 skdev->name, __func__, __LINE__,
2700 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002701 } else {
2702 /*
2703 * Capture the outcome and post it back to the
2704 * native request.
2705 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002706 if (likely(cmp_status == SAM_STAT_GOOD))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002707 skd_end_request(skdev, skreq, BLK_STS_OK);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002708 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002709 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002710 }
2711
2712 /*
2713 * Release the skreq, its FIT msg (if one), timeout slot,
2714 * and queue depth.
2715 */
2716 skd_release_skreq(skdev, skreq);
2717
2718 /* skd_isr_comp_limit equal zero means no limit */
2719 if (limit) {
2720 if (++processed >= limit) {
2721 rc = 1;
2722 break;
2723 }
2724 }
2725 }
2726
2727 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2728 && (skdev->in_flight) == 0) {
2729 skdev->state = SKD_DRVR_STATE_PAUSED;
2730 wake_up_interruptible(&skdev->waitq);
2731 }
2732
2733 return rc;
2734}
2735
2736static void skd_complete_other(struct skd_device *skdev,
2737 volatile struct fit_completion_entry_v1 *skcomp,
2738 volatile struct fit_comp_error_info *skerr)
2739{
2740 u32 req_id = 0;
2741 u32 req_table;
2742 u32 req_slot;
2743 struct skd_special_context *skspcl;
2744
2745 req_id = skcomp->tag;
2746 req_table = req_id & SKD_ID_TABLE_MASK;
2747 req_slot = req_id & SKD_ID_SLOT_MASK;
2748
rchinthekindi2e44b422013-10-24 12:51:23 +01002749 pr_debug("%s:%s:%d table=0x%x id=0x%x slot=%d\n",
2750 skdev->name, __func__, __LINE__,
2751 req_table, req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002752
2753 /*
2754 * Based on the request id, determine how to dispatch this completion.
2755 * This swich/case is finding the good cases and forwarding the
2756 * completion entry. Errors are reported below the switch.
2757 */
2758 switch (req_table) {
2759 case SKD_ID_RW_REQUEST:
2760 /*
Bart Van Asschee1d06f22017-08-17 13:12:54 -07002761 * The caller, skd_isr_completion_posted() above,
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002762 * handles r/w requests. The only way we get here
2763 * is if the req_slot is out of bounds.
2764 */
2765 break;
2766
2767 case SKD_ID_SPECIAL_REQUEST:
2768 /*
2769 * Make sure the req_slot is in bounds and that the id
2770 * matches.
2771 */
2772 if (req_slot < skdev->n_special) {
2773 skspcl = &skdev->skspcl_table[req_slot];
2774 if (skspcl->req.id == req_id &&
2775 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2776 skd_complete_special(skdev,
2777 skcomp, skerr, skspcl);
2778 return;
2779 }
2780 }
2781 break;
2782
2783 case SKD_ID_INTERNAL:
2784 if (req_slot == 0) {
2785 skspcl = &skdev->internal_skspcl;
2786 if (skspcl->req.id == req_id &&
2787 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2788 skd_complete_internal(skdev,
2789 skcomp, skerr, skspcl);
2790 return;
2791 }
2792 }
2793 break;
2794
2795 case SKD_ID_FIT_MSG:
2796 /*
2797 * These id's should never appear in a completion record.
2798 */
2799 break;
2800
2801 default:
2802 /*
2803 * These id's should never appear anywhere;
2804 */
2805 break;
2806 }
2807
2808 /*
2809 * If we get here it is a bad or stale id.
2810 */
2811}
2812
2813static void skd_complete_special(struct skd_device *skdev,
2814 volatile struct fit_completion_entry_v1
2815 *skcomp,
2816 volatile struct fit_comp_error_info *skerr,
2817 struct skd_special_context *skspcl)
2818{
rchinthekindi2e44b422013-10-24 12:51:23 +01002819 pr_debug("%s:%s:%d completing special request %p\n",
2820 skdev->name, __func__, __LINE__, skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002821 if (skspcl->orphaned) {
2822 /* Discard orphaned request */
2823 /* ?: Can this release directly or does it need
2824 * to use a worker? */
rchinthekindi2e44b422013-10-24 12:51:23 +01002825 pr_debug("%s:%s:%d release orphaned %p\n",
2826 skdev->name, __func__, __LINE__, skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002827 skd_release_special(skdev, skspcl);
2828 return;
2829 }
2830
2831 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2832
2833 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2834 skspcl->req.completion = *skcomp;
2835 skspcl->req.err_info = *skerr;
2836
2837 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2838 skerr->code, skerr->qual, skerr->fruc);
2839
2840 wake_up_interruptible(&skdev->waitq);
2841}
2842
2843/* assume spinlock is already held */
2844static void skd_release_special(struct skd_device *skdev,
2845 struct skd_special_context *skspcl)
2846{
2847 int i, was_depleted;
2848
2849 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002850 struct page *page = sg_page(&skspcl->req.sg[i]);
2851 __free_page(page);
2852 }
2853
2854 was_depleted = (skdev->skspcl_free_list == NULL);
2855
2856 skspcl->req.state = SKD_REQ_STATE_IDLE;
2857 skspcl->req.id += SKD_ID_INCR;
2858 skspcl->req.next =
2859 (struct skd_request_context *)skdev->skspcl_free_list;
2860 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2861
2862 if (was_depleted) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002863 pr_debug("%s:%s:%d skspcl was depleted\n",
2864 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002865 /* Free list was depleted. Their might be waiters. */
2866 wake_up_interruptible(&skdev->waitq);
2867 }
2868}
2869
2870static void skd_reset_skcomp(struct skd_device *skdev)
2871{
2872 u32 nbytes;
2873 struct fit_completion_entry_v1 *skcomp;
2874
2875 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
2876 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
2877
2878 memset(skdev->skcomp_table, 0, nbytes);
2879
2880 skdev->skcomp_ix = 0;
2881 skdev->skcomp_cycle = 1;
2882}
2883
2884/*
2885 *****************************************************************************
2886 * INTERRUPTS
2887 *****************************************************************************
2888 */
2889static void skd_completion_worker(struct work_struct *work)
2890{
2891 struct skd_device *skdev =
2892 container_of(work, struct skd_device, completion_worker);
2893 unsigned long flags;
2894 int flush_enqueued = 0;
2895
2896 spin_lock_irqsave(&skdev->lock, flags);
2897
2898 /*
2899 * pass in limit=0, which means no limit..
2900 * process everything in compq
2901 */
2902 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2903 skd_request_fn(skdev->queue);
2904
2905 spin_unlock_irqrestore(&skdev->lock, flags);
2906}
2907
2908static void skd_isr_msg_from_dev(struct skd_device *skdev);
2909
Arnd Bergmann41c94992016-11-09 13:55:35 +01002910static irqreturn_t
2911skd_isr(int irq, void *ptr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002912{
2913 struct skd_device *skdev;
2914 u32 intstat;
2915 u32 ack;
2916 int rc = 0;
2917 int deferred = 0;
2918 int flush_enqueued = 0;
2919
2920 skdev = (struct skd_device *)ptr;
2921 spin_lock(&skdev->lock);
2922
2923 for (;; ) {
2924 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2925
2926 ack = FIT_INT_DEF_MASK;
2927 ack &= intstat;
2928
rchinthekindi2e44b422013-10-24 12:51:23 +01002929 pr_debug("%s:%s:%d intstat=0x%x ack=0x%x\n",
2930 skdev->name, __func__, __LINE__, intstat, ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002931
2932 /* As long as there is an int pending on device, keep
2933 * running loop. When none, get out, but if we've never
2934 * done any processing, call completion handler?
2935 */
2936 if (ack == 0) {
2937 /* No interrupts on device, but run the completion
2938 * processor anyway?
2939 */
2940 if (rc == 0)
2941 if (likely (skdev->state
2942 == SKD_DRVR_STATE_ONLINE))
2943 deferred = 1;
2944 break;
2945 }
2946
2947 rc = IRQ_HANDLED;
2948
2949 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2950
2951 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2952 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2953 if (intstat & FIT_ISH_COMPLETION_POSTED) {
2954 /*
2955 * If we have already deferred completion
2956 * processing, don't bother running it again
2957 */
2958 if (deferred == 0)
2959 deferred =
2960 skd_isr_completion_posted(skdev,
2961 skd_isr_comp_limit, &flush_enqueued);
2962 }
2963
2964 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
2965 skd_isr_fwstate(skdev);
2966 if (skdev->state == SKD_DRVR_STATE_FAULT ||
2967 skdev->state ==
2968 SKD_DRVR_STATE_DISAPPEARED) {
2969 spin_unlock(&skdev->lock);
2970 return rc;
2971 }
2972 }
2973
2974 if (intstat & FIT_ISH_MSG_FROM_DEV)
2975 skd_isr_msg_from_dev(skdev);
2976 }
2977 }
2978
2979 if (unlikely(flush_enqueued))
2980 skd_request_fn(skdev->queue);
2981
2982 if (deferred)
2983 schedule_work(&skdev->completion_worker);
2984 else if (!flush_enqueued)
2985 skd_request_fn(skdev->queue);
2986
2987 spin_unlock(&skdev->lock);
2988
2989 return rc;
2990}
2991
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002992static void skd_drive_fault(struct skd_device *skdev)
2993{
2994 skdev->state = SKD_DRVR_STATE_FAULT;
2995 pr_err("(%s): Drive FAULT\n", skd_name(skdev));
2996}
2997
2998static void skd_drive_disappeared(struct skd_device *skdev)
2999{
3000 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
3001 pr_err("(%s): Drive DISAPPEARED\n", skd_name(skdev));
3002}
3003
3004static void skd_isr_fwstate(struct skd_device *skdev)
3005{
3006 u32 sense;
3007 u32 state;
3008 u32 mtd;
3009 int prev_driver_state = skdev->state;
3010
3011 sense = SKD_READL(skdev, FIT_STATUS);
3012 state = sense & FIT_SR_DRIVE_STATE_MASK;
3013
3014 pr_err("(%s): s1120 state %s(%d)=>%s(%d)\n",
3015 skd_name(skdev),
3016 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3017 skd_drive_state_to_str(state), state);
3018
3019 skdev->drive_state = state;
3020
3021 switch (skdev->drive_state) {
3022 case FIT_SR_DRIVE_INIT:
3023 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
3024 skd_disable_interrupts(skdev);
3025 break;
3026 }
3027 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
3028 skd_recover_requests(skdev, 0);
3029 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
3030 skdev->timer_countdown = SKD_STARTING_TIMO;
3031 skdev->state = SKD_DRVR_STATE_STARTING;
3032 skd_soft_reset(skdev);
3033 break;
3034 }
3035 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
3036 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3037 skdev->last_mtd = mtd;
3038 break;
3039
3040 case FIT_SR_DRIVE_ONLINE:
3041 skdev->cur_max_queue_depth = skd_max_queue_depth;
3042 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
3043 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
3044
3045 skdev->queue_low_water_mark =
3046 skdev->cur_max_queue_depth * 2 / 3 + 1;
3047 if (skdev->queue_low_water_mark < 1)
3048 skdev->queue_low_water_mark = 1;
Bart Van Assche5477e1b2017-08-17 13:12:51 -07003049 pr_info("(%s): Queue depth limit=%d dev=%d lowat=%d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003050 skd_name(skdev),
3051 skdev->cur_max_queue_depth,
3052 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3053
3054 skd_refresh_device_data(skdev);
3055 break;
3056
3057 case FIT_SR_DRIVE_BUSY:
3058 skdev->state = SKD_DRVR_STATE_BUSY;
3059 skdev->timer_countdown = SKD_BUSY_TIMO;
3060 skd_quiesce_dev(skdev);
3061 break;
3062 case FIT_SR_DRIVE_BUSY_SANITIZE:
3063 /* set timer for 3 seconds, we'll abort any unfinished
3064 * commands after that expires
3065 */
3066 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3067 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003068 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003069 break;
3070 case FIT_SR_DRIVE_BUSY_ERASE:
3071 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3072 skdev->timer_countdown = SKD_BUSY_TIMO;
3073 break;
3074 case FIT_SR_DRIVE_OFFLINE:
3075 skdev->state = SKD_DRVR_STATE_IDLE;
3076 break;
3077 case FIT_SR_DRIVE_SOFT_RESET:
3078 switch (skdev->state) {
3079 case SKD_DRVR_STATE_STARTING:
3080 case SKD_DRVR_STATE_RESTARTING:
3081 /* Expected by a caller of skd_soft_reset() */
3082 break;
3083 default:
3084 skdev->state = SKD_DRVR_STATE_RESTARTING;
3085 break;
3086 }
3087 break;
3088 case FIT_SR_DRIVE_FW_BOOTING:
rchinthekindi2e44b422013-10-24 12:51:23 +01003089 pr_debug("%s:%s:%d ISR FIT_SR_DRIVE_FW_BOOTING %s\n",
3090 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003091 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3092 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3093 break;
3094
3095 case FIT_SR_DRIVE_DEGRADED:
3096 case FIT_SR_PCIE_LINK_DOWN:
3097 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3098 break;
3099
3100 case FIT_SR_DRIVE_FAULT:
3101 skd_drive_fault(skdev);
3102 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003103 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003104 break;
3105
3106 /* PCIe bus returned all Fs? */
3107 case 0xFF:
3108 pr_info("(%s): state=0x%x sense=0x%x\n",
3109 skd_name(skdev), state, sense);
3110 skd_drive_disappeared(skdev);
3111 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003112 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003113 break;
3114 default:
3115 /*
3116 * Uknown FW State. Wait for a state we recognize.
3117 */
3118 break;
3119 }
3120 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3121 skd_name(skdev),
3122 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3123 skd_skdev_state_to_str(skdev->state), skdev->state);
3124}
3125
3126static void skd_recover_requests(struct skd_device *skdev, int requeue)
3127{
3128 int i;
3129
3130 for (i = 0; i < skdev->num_req_context; i++) {
3131 struct skd_request_context *skreq = &skdev->skreq_table[i];
3132
3133 if (skreq->state == SKD_REQ_STATE_BUSY) {
3134 skd_log_skreq(skdev, skreq, "recover");
3135
3136 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003137 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003138
3139 /* Release DMA resources for the request. */
3140 if (skreq->n_sg > 0)
3141 skd_postop_sg_list(skdev, skreq);
3142
Jens Axboefcd37eb2013-11-01 10:14:56 -06003143 if (requeue &&
3144 (unsigned long) ++skreq->req->special <
3145 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003146 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003147 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003148 skd_end_request(skdev, skreq, BLK_STS_IOERR);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003149
Jens Axboefcd37eb2013-11-01 10:14:56 -06003150 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003151
3152 skreq->state = SKD_REQ_STATE_IDLE;
3153 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003154 }
3155 if (i > 0)
3156 skreq[-1].next = skreq;
3157 skreq->next = NULL;
3158 }
3159 skdev->skreq_free_list = skdev->skreq_table;
3160
3161 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3162 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3163
3164 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3165 skd_log_skmsg(skdev, skmsg, "salvaged");
3166 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3167 skmsg->state = SKD_MSG_STATE_IDLE;
3168 skmsg->id += SKD_ID_INCR;
3169 }
3170 if (i > 0)
3171 skmsg[-1].next = skmsg;
3172 skmsg->next = NULL;
3173 }
3174 skdev->skmsg_free_list = skdev->skmsg_table;
3175
3176 for (i = 0; i < skdev->n_special; i++) {
3177 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3178
3179 /* If orphaned, reclaim it because it has already been reported
3180 * to the process as an error (it was just waiting for
3181 * a completion that didn't come, and now it will never come)
3182 * If busy, change to a state that will cause it to error
3183 * out in the wait routine and let it do the normal
3184 * reporting and reclaiming
3185 */
3186 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3187 if (skspcl->orphaned) {
rchinthekindi2e44b422013-10-24 12:51:23 +01003188 pr_debug("%s:%s:%d orphaned %p\n",
3189 skdev->name, __func__, __LINE__,
3190 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003191 skd_release_special(skdev, skspcl);
3192 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01003193 pr_debug("%s:%s:%d not orphaned %p\n",
3194 skdev->name, __func__, __LINE__,
3195 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003196 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3197 }
3198 }
3199 }
3200 skdev->skspcl_free_list = skdev->skspcl_table;
3201
3202 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3203 skdev->timeout_slot[i] = 0;
3204
3205 skdev->in_flight = 0;
3206}
3207
3208static void skd_isr_msg_from_dev(struct skd_device *skdev)
3209{
3210 u32 mfd;
3211 u32 mtd;
3212 u32 data;
3213
3214 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3215
rchinthekindi2e44b422013-10-24 12:51:23 +01003216 pr_debug("%s:%s:%d mfd=0x%x last_mtd=0x%x\n",
3217 skdev->name, __func__, __LINE__, mfd, skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003218
3219 /* ignore any mtd that is an ack for something we didn't send */
3220 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3221 return;
3222
3223 switch (FIT_MXD_TYPE(mfd)) {
3224 case FIT_MTD_FITFW_INIT:
3225 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3226
3227 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
3228 pr_err("(%s): protocol mismatch\n",
3229 skdev->name);
3230 pr_err("(%s): got=%d support=%d\n",
3231 skdev->name, skdev->proto_ver,
3232 FIT_PROTOCOL_VERSION_1);
3233 pr_err("(%s): please upgrade driver\n",
3234 skdev->name);
3235 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3236 skd_soft_reset(skdev);
3237 break;
3238 }
3239 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3240 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3241 skdev->last_mtd = mtd;
3242 break;
3243
3244 case FIT_MTD_GET_CMDQ_DEPTH:
3245 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3246 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3247 SKD_N_COMPLETION_ENTRY);
3248 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3249 skdev->last_mtd = mtd;
3250 break;
3251
3252 case FIT_MTD_SET_COMPQ_DEPTH:
3253 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3254 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3255 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3256 skdev->last_mtd = mtd;
3257 break;
3258
3259 case FIT_MTD_SET_COMPQ_ADDR:
3260 skd_reset_skcomp(skdev);
3261 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3262 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3263 skdev->last_mtd = mtd;
3264 break;
3265
3266 case FIT_MTD_CMD_LOG_HOST_ID:
3267 skdev->connect_time_stamp = get_seconds();
3268 data = skdev->connect_time_stamp & 0xFFFF;
3269 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3270 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3271 skdev->last_mtd = mtd;
3272 break;
3273
3274 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3275 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3276 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3277 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3278 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3279 skdev->last_mtd = mtd;
3280 break;
3281
3282 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3283 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3284 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3285 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3286 skdev->last_mtd = mtd;
3287
3288 pr_err("(%s): Time sync driver=0x%x device=0x%x\n",
3289 skd_name(skdev),
3290 skdev->connect_time_stamp, skdev->drive_jiffies);
3291 break;
3292
3293 case FIT_MTD_ARM_QUEUE:
3294 skdev->last_mtd = 0;
3295 /*
3296 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3297 */
3298 break;
3299
3300 default:
3301 break;
3302 }
3303}
3304
3305static void skd_disable_interrupts(struct skd_device *skdev)
3306{
3307 u32 sense;
3308
3309 sense = SKD_READL(skdev, FIT_CONTROL);
3310 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3311 SKD_WRITEL(skdev, sense, FIT_CONTROL);
rchinthekindi2e44b422013-10-24 12:51:23 +01003312 pr_debug("%s:%s:%d sense 0x%x\n",
3313 skdev->name, __func__, __LINE__, sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003314
3315 /* Note that the 1s is written. A 1-bit means
3316 * disable, a 0 means enable.
3317 */
3318 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3319}
3320
3321static void skd_enable_interrupts(struct skd_device *skdev)
3322{
3323 u32 val;
3324
3325 /* unmask interrupts first */
3326 val = FIT_ISH_FW_STATE_CHANGE +
3327 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3328
3329 /* Note that the compliment of mask is written. A 1-bit means
3330 * disable, a 0 means enable. */
3331 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003332 pr_debug("%s:%s:%d interrupt mask=0x%x\n",
3333 skdev->name, __func__, __LINE__, ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003334
3335 val = SKD_READL(skdev, FIT_CONTROL);
3336 val |= FIT_CR_ENABLE_INTERRUPTS;
rchinthekindi2e44b422013-10-24 12:51:23 +01003337 pr_debug("%s:%s:%d control=0x%x\n",
3338 skdev->name, __func__, __LINE__, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003339 SKD_WRITEL(skdev, val, FIT_CONTROL);
3340}
3341
3342/*
3343 *****************************************************************************
3344 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3345 *****************************************************************************
3346 */
3347
3348static void skd_soft_reset(struct skd_device *skdev)
3349{
3350 u32 val;
3351
3352 val = SKD_READL(skdev, FIT_CONTROL);
3353 val |= (FIT_CR_SOFT_RESET);
rchinthekindi2e44b422013-10-24 12:51:23 +01003354 pr_debug("%s:%s:%d control=0x%x\n",
3355 skdev->name, __func__, __LINE__, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003356 SKD_WRITEL(skdev, val, FIT_CONTROL);
3357}
3358
3359static void skd_start_device(struct skd_device *skdev)
3360{
3361 unsigned long flags;
3362 u32 sense;
3363 u32 state;
3364
3365 spin_lock_irqsave(&skdev->lock, flags);
3366
3367 /* ack all ghost interrupts */
3368 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3369
3370 sense = SKD_READL(skdev, FIT_STATUS);
3371
rchinthekindi2e44b422013-10-24 12:51:23 +01003372 pr_debug("%s:%s:%d initial status=0x%x\n",
3373 skdev->name, __func__, __LINE__, sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003374
3375 state = sense & FIT_SR_DRIVE_STATE_MASK;
3376 skdev->drive_state = state;
3377 skdev->last_mtd = 0;
3378
3379 skdev->state = SKD_DRVR_STATE_STARTING;
3380 skdev->timer_countdown = SKD_STARTING_TIMO;
3381
3382 skd_enable_interrupts(skdev);
3383
3384 switch (skdev->drive_state) {
3385 case FIT_SR_DRIVE_OFFLINE:
3386 pr_err("(%s): Drive offline...\n", skd_name(skdev));
3387 break;
3388
3389 case FIT_SR_DRIVE_FW_BOOTING:
rchinthekindi2e44b422013-10-24 12:51:23 +01003390 pr_debug("%s:%s:%d FIT_SR_DRIVE_FW_BOOTING %s\n",
3391 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003392 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3393 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3394 break;
3395
3396 case FIT_SR_DRIVE_BUSY_SANITIZE:
3397 pr_info("(%s): Start: BUSY_SANITIZE\n",
3398 skd_name(skdev));
3399 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3400 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3401 break;
3402
3403 case FIT_SR_DRIVE_BUSY_ERASE:
3404 pr_info("(%s): Start: BUSY_ERASE\n", skd_name(skdev));
3405 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3406 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3407 break;
3408
3409 case FIT_SR_DRIVE_INIT:
3410 case FIT_SR_DRIVE_ONLINE:
3411 skd_soft_reset(skdev);
3412 break;
3413
3414 case FIT_SR_DRIVE_BUSY:
3415 pr_err("(%s): Drive Busy...\n", skd_name(skdev));
3416 skdev->state = SKD_DRVR_STATE_BUSY;
3417 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3418 break;
3419
3420 case FIT_SR_DRIVE_SOFT_RESET:
3421 pr_err("(%s) drive soft reset in prog\n",
3422 skd_name(skdev));
3423 break;
3424
3425 case FIT_SR_DRIVE_FAULT:
3426 /* Fault state is bad...soft reset won't do it...
3427 * Hard reset, maybe, but does it work on device?
3428 * For now, just fault so the system doesn't hang.
3429 */
3430 skd_drive_fault(skdev);
3431 /*start the queue so we can respond with error to requests */
rchinthekindi2e44b422013-10-24 12:51:23 +01003432 pr_debug("%s:%s:%d starting %s queue\n",
3433 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003434 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003435 skdev->gendisk_on = -1;
3436 wake_up_interruptible(&skdev->waitq);
3437 break;
3438
3439 case 0xFF:
3440 /* Most likely the device isn't there or isn't responding
3441 * to the BAR1 addresses. */
3442 skd_drive_disappeared(skdev);
3443 /*start the queue so we can respond with error to requests */
rchinthekindi2e44b422013-10-24 12:51:23 +01003444 pr_debug("%s:%s:%d starting %s queue to error-out reqs\n",
3445 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003446 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003447 skdev->gendisk_on = -1;
3448 wake_up_interruptible(&skdev->waitq);
3449 break;
3450
3451 default:
3452 pr_err("(%s) Start: unknown state %x\n",
3453 skd_name(skdev), skdev->drive_state);
3454 break;
3455 }
3456
3457 state = SKD_READL(skdev, FIT_CONTROL);
rchinthekindi2e44b422013-10-24 12:51:23 +01003458 pr_debug("%s:%s:%d FIT Control Status=0x%x\n",
3459 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003460
3461 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003462 pr_debug("%s:%s:%d Intr Status=0x%x\n",
3463 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003464
3465 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003466 pr_debug("%s:%s:%d Intr Mask=0x%x\n",
3467 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003468
3469 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
rchinthekindi2e44b422013-10-24 12:51:23 +01003470 pr_debug("%s:%s:%d Msg from Dev=0x%x\n",
3471 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003472
3473 state = SKD_READL(skdev, FIT_HW_VERSION);
rchinthekindi2e44b422013-10-24 12:51:23 +01003474 pr_debug("%s:%s:%d HW version=0x%x\n",
3475 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003476
3477 spin_unlock_irqrestore(&skdev->lock, flags);
3478}
3479
3480static void skd_stop_device(struct skd_device *skdev)
3481{
3482 unsigned long flags;
3483 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3484 u32 dev_state;
3485 int i;
3486
3487 spin_lock_irqsave(&skdev->lock, flags);
3488
3489 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
3490 pr_err("(%s): skd_stop_device not online no sync\n",
3491 skd_name(skdev));
3492 goto stop_out;
3493 }
3494
3495 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
3496 pr_err("(%s): skd_stop_device no special\n",
3497 skd_name(skdev));
3498 goto stop_out;
3499 }
3500
3501 skdev->state = SKD_DRVR_STATE_SYNCING;
3502 skdev->sync_done = 0;
3503
3504 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3505
3506 spin_unlock_irqrestore(&skdev->lock, flags);
3507
3508 wait_event_interruptible_timeout(skdev->waitq,
3509 (skdev->sync_done), (10 * HZ));
3510
3511 spin_lock_irqsave(&skdev->lock, flags);
3512
3513 switch (skdev->sync_done) {
3514 case 0:
3515 pr_err("(%s): skd_stop_device no sync\n",
3516 skd_name(skdev));
3517 break;
3518 case 1:
3519 pr_err("(%s): skd_stop_device sync done\n",
3520 skd_name(skdev));
3521 break;
3522 default:
3523 pr_err("(%s): skd_stop_device sync error\n",
3524 skd_name(skdev));
3525 }
3526
3527stop_out:
3528 skdev->state = SKD_DRVR_STATE_STOPPING;
3529 spin_unlock_irqrestore(&skdev->lock, flags);
3530
3531 skd_kill_timer(skdev);
3532
3533 spin_lock_irqsave(&skdev->lock, flags);
3534 skd_disable_interrupts(skdev);
3535
3536 /* ensure all ints on device are cleared */
3537 /* soft reset the device to unload with a clean slate */
3538 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3539 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3540
3541 spin_unlock_irqrestore(&skdev->lock, flags);
3542
3543 /* poll every 100ms, 1 second timeout */
3544 for (i = 0; i < 10; i++) {
3545 dev_state =
3546 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3547 if (dev_state == FIT_SR_DRIVE_INIT)
3548 break;
3549 set_current_state(TASK_INTERRUPTIBLE);
3550 schedule_timeout(msecs_to_jiffies(100));
3551 }
3552
3553 if (dev_state != FIT_SR_DRIVE_INIT)
3554 pr_err("(%s): skd_stop_device state error 0x%02x\n",
3555 skd_name(skdev), dev_state);
3556}
3557
3558/* assume spinlock is held */
3559static void skd_restart_device(struct skd_device *skdev)
3560{
3561 u32 state;
3562
3563 /* ack all ghost interrupts */
3564 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3565
3566 state = SKD_READL(skdev, FIT_STATUS);
3567
rchinthekindi2e44b422013-10-24 12:51:23 +01003568 pr_debug("%s:%s:%d drive status=0x%x\n",
3569 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003570
3571 state &= FIT_SR_DRIVE_STATE_MASK;
3572 skdev->drive_state = state;
3573 skdev->last_mtd = 0;
3574
3575 skdev->state = SKD_DRVR_STATE_RESTARTING;
3576 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3577
3578 skd_soft_reset(skdev);
3579}
3580
3581/* assume spinlock is held */
3582static int skd_quiesce_dev(struct skd_device *skdev)
3583{
3584 int rc = 0;
3585
3586 switch (skdev->state) {
3587 case SKD_DRVR_STATE_BUSY:
3588 case SKD_DRVR_STATE_BUSY_IMMINENT:
rchinthekindi2e44b422013-10-24 12:51:23 +01003589 pr_debug("%s:%s:%d stopping %s queue\n",
3590 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003591 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003592 break;
3593 case SKD_DRVR_STATE_ONLINE:
3594 case SKD_DRVR_STATE_STOPPING:
3595 case SKD_DRVR_STATE_SYNCING:
3596 case SKD_DRVR_STATE_PAUSING:
3597 case SKD_DRVR_STATE_PAUSED:
3598 case SKD_DRVR_STATE_STARTING:
3599 case SKD_DRVR_STATE_RESTARTING:
3600 case SKD_DRVR_STATE_RESUMING:
3601 default:
3602 rc = -EINVAL;
rchinthekindi2e44b422013-10-24 12:51:23 +01003603 pr_debug("%s:%s:%d state [%d] not implemented\n",
3604 skdev->name, __func__, __LINE__, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003605 }
3606 return rc;
3607}
3608
3609/* assume spinlock is held */
3610static int skd_unquiesce_dev(struct skd_device *skdev)
3611{
3612 int prev_driver_state = skdev->state;
3613
3614 skd_log_skdev(skdev, "unquiesce");
3615 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
rchinthekindi2e44b422013-10-24 12:51:23 +01003616 pr_debug("%s:%s:%d **** device already ONLINE\n",
3617 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003618 return 0;
3619 }
3620 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3621 /*
3622 * If there has been an state change to other than
3623 * ONLINE, we will rely on controller state change
3624 * to come back online and restart the queue.
3625 * The BUSY state means that driver is ready to
3626 * continue normal processing but waiting for controller
3627 * to become available.
3628 */
3629 skdev->state = SKD_DRVR_STATE_BUSY;
rchinthekindi2e44b422013-10-24 12:51:23 +01003630 pr_debug("%s:%s:%d drive BUSY state\n",
3631 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003632 return 0;
3633 }
3634
3635 /*
3636 * Drive has just come online, driver is either in startup,
3637 * paused performing a task, or bust waiting for hardware.
3638 */
3639 switch (skdev->state) {
3640 case SKD_DRVR_STATE_PAUSED:
3641 case SKD_DRVR_STATE_BUSY:
3642 case SKD_DRVR_STATE_BUSY_IMMINENT:
3643 case SKD_DRVR_STATE_BUSY_ERASE:
3644 case SKD_DRVR_STATE_STARTING:
3645 case SKD_DRVR_STATE_RESTARTING:
3646 case SKD_DRVR_STATE_FAULT:
3647 case SKD_DRVR_STATE_IDLE:
3648 case SKD_DRVR_STATE_LOAD:
3649 skdev->state = SKD_DRVR_STATE_ONLINE;
3650 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3651 skd_name(skdev),
3652 skd_skdev_state_to_str(prev_driver_state),
3653 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3654 skdev->state);
rchinthekindi2e44b422013-10-24 12:51:23 +01003655 pr_debug("%s:%s:%d **** device ONLINE...starting block queue\n",
3656 skdev->name, __func__, __LINE__);
3657 pr_debug("%s:%s:%d starting %s queue\n",
3658 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003659 pr_info("(%s): STEC s1120 ONLINE\n", skd_name(skdev));
Jens Axboe6a5ec652013-11-01 10:38:45 -06003660 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003661 skdev->gendisk_on = 1;
3662 wake_up_interruptible(&skdev->waitq);
3663 break;
3664
3665 case SKD_DRVR_STATE_DISAPPEARED:
3666 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01003667 pr_debug("%s:%s:%d **** driver state %d, not implemented \n",
3668 skdev->name, __func__, __LINE__,
3669 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003670 return -EBUSY;
3671 }
3672 return 0;
3673}
3674
3675/*
3676 *****************************************************************************
3677 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3678 *****************************************************************************
3679 */
3680
3681static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3682{
3683 struct skd_device *skdev = skd_host_data;
3684 unsigned long flags;
3685
3686 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003687 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3688 skdev->name, __func__, __LINE__,
3689 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003690 pr_err("(%s): MSIX reserved irq %d = 0x%x\n", skd_name(skdev),
3691 irq, SKD_READL(skdev, FIT_INT_STATUS_HOST));
3692 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3693 spin_unlock_irqrestore(&skdev->lock, flags);
3694 return IRQ_HANDLED;
3695}
3696
3697static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3698{
3699 struct skd_device *skdev = skd_host_data;
3700 unsigned long flags;
3701
3702 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003703 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3704 skdev->name, __func__, __LINE__,
3705 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003706 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3707 skd_isr_fwstate(skdev);
3708 spin_unlock_irqrestore(&skdev->lock, flags);
3709 return IRQ_HANDLED;
3710}
3711
3712static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3713{
3714 struct skd_device *skdev = skd_host_data;
3715 unsigned long flags;
3716 int flush_enqueued = 0;
3717 int deferred;
3718
3719 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003720 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3721 skdev->name, __func__, __LINE__,
3722 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003723 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3724 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3725 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003726 if (flush_enqueued)
3727 skd_request_fn(skdev->queue);
3728
3729 if (deferred)
3730 schedule_work(&skdev->completion_worker);
3731 else if (!flush_enqueued)
3732 skd_request_fn(skdev->queue);
3733
3734 spin_unlock_irqrestore(&skdev->lock, flags);
3735
3736 return IRQ_HANDLED;
3737}
3738
3739static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3740{
3741 struct skd_device *skdev = skd_host_data;
3742 unsigned long flags;
3743
3744 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003745 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3746 skdev->name, __func__, __LINE__,
3747 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003748 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3749 skd_isr_msg_from_dev(skdev);
3750 spin_unlock_irqrestore(&skdev->lock, flags);
3751 return IRQ_HANDLED;
3752}
3753
3754static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3755{
3756 struct skd_device *skdev = skd_host_data;
3757 unsigned long flags;
3758
3759 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003760 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3761 skdev->name, __func__, __LINE__,
3762 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003763 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3764 spin_unlock_irqrestore(&skdev->lock, flags);
3765 return IRQ_HANDLED;
3766}
3767
3768/*
3769 *****************************************************************************
3770 * PCIe MSI/MSI-X SETUP
3771 *****************************************************************************
3772 */
3773
3774struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003775 char isr_name[30];
3776};
3777
3778struct skd_init_msix_entry {
3779 const char *name;
3780 irq_handler_t handler;
3781};
3782
3783#define SKD_MAX_MSIX_COUNT 13
3784#define SKD_MIN_MSIX_COUNT 7
3785#define SKD_BASE_MSIX_IRQ 4
3786
3787static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3788 { "(DMA 0)", skd_reserved_isr },
3789 { "(DMA 1)", skd_reserved_isr },
3790 { "(DMA 2)", skd_reserved_isr },
3791 { "(DMA 3)", skd_reserved_isr },
3792 { "(State Change)", skd_statec_isr },
3793 { "(COMPL_Q)", skd_comp_q },
3794 { "(MSG)", skd_msg_isr },
3795 { "(Reserved)", skd_reserved_isr },
3796 { "(Reserved)", skd_reserved_isr },
3797 { "(Queue Full 0)", skd_qfull_isr },
3798 { "(Queue Full 1)", skd_qfull_isr },
3799 { "(Queue Full 2)", skd_qfull_isr },
3800 { "(Queue Full 3)", skd_qfull_isr },
3801};
3802
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003803static int skd_acquire_msix(struct skd_device *skdev)
3804{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003805 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003806 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003807
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003808 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3809 PCI_IRQ_MSIX);
3810 if (rc < 0) {
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003811 pr_err("(%s): failed to enable MSI-X %d\n",
3812 skd_name(skdev), rc);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003813 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003814 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003815
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003816 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3817 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003818 if (!skdev->msix_entries) {
3819 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003820 pr_err("(%s): msix table allocation error\n",
3821 skd_name(skdev));
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003822 goto out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003823 }
3824
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003825 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003826 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3827 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3828
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003829 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3830 "%s%d-msix %s", DRV_NAME, skdev->devno,
3831 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003832
3833 rc = devm_request_irq(&skdev->pdev->dev,
3834 pci_irq_vector(skdev->pdev, i),
3835 msix_entries[i].handler, 0,
3836 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003837 if (rc) {
3838 pr_err("(%s): Unable to register(%d) MSI-X "
3839 "handler %d: %s\n",
3840 skd_name(skdev), rc, i, qentry->isr_name);
3841 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003842 }
3843 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003844
rchinthekindi2e44b422013-10-24 12:51:23 +01003845 pr_debug("%s:%s:%d %s: <%s> msix %d irq(s) enabled\n",
3846 skdev->name, __func__, __LINE__,
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003847 pci_name(pdev), skdev->name, SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003848 return 0;
3849
3850msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003851 while (--i >= 0)
3852 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
Arnd Bergmann3bc84922016-11-09 13:55:34 +01003853out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003854 kfree(skdev->msix_entries);
3855 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003856 return rc;
3857}
3858
3859static int skd_acquire_irq(struct skd_device *skdev)
3860{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003861 struct pci_dev *pdev = skdev->pdev;
3862 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003863 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003864
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003865 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003866 rc = skd_acquire_msix(skdev);
3867 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003868 return 0;
3869
3870 pr_err("(%s): failed to enable MSI-X, re-trying with MSI %d\n",
3871 skd_name(skdev), rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003872 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003873
3874 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3875 skdev->devno);
3876
3877 if (skd_isr_type != SKD_IRQ_LEGACY)
3878 irq_flag |= PCI_IRQ_MSI;
3879 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3880 if (rc < 0) {
3881 pr_err("(%s): failed to allocate the MSI interrupt %d\n",
3882 skd_name(skdev), rc);
3883 return rc;
3884 }
3885
3886 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3887 pdev->msi_enabled ? 0 : IRQF_SHARED,
3888 skdev->isr_name, skdev);
3889 if (rc) {
3890 pci_free_irq_vectors(pdev);
3891 pr_err("(%s): failed to allocate interrupt %d\n",
3892 skd_name(skdev), rc);
3893 return rc;
3894 }
3895
3896 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003897}
3898
3899static void skd_release_irq(struct skd_device *skdev)
3900{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003901 struct pci_dev *pdev = skdev->pdev;
3902
3903 if (skdev->msix_entries) {
3904 int i;
3905
3906 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3907 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3908 skdev);
3909 }
3910
3911 kfree(skdev->msix_entries);
3912 skdev->msix_entries = NULL;
3913 } else {
3914 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003915 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003916
3917 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003918}
3919
3920/*
3921 *****************************************************************************
3922 * CONSTRUCT
3923 *****************************************************************************
3924 */
3925
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003926static int skd_cons_skcomp(struct skd_device *skdev)
3927{
3928 int rc = 0;
3929 struct fit_completion_entry_v1 *skcomp;
3930 u32 nbytes;
3931
3932 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
3933 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
3934
rchinthekindi2e44b422013-10-24 12:51:23 +01003935 pr_debug("%s:%s:%d comp pci_alloc, total bytes %d entries %d\n",
3936 skdev->name, __func__, __LINE__,
3937 nbytes, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003938
Joe Perchesa5bbf612014-08-08 14:24:12 -07003939 skcomp = pci_zalloc_consistent(skdev->pdev, nbytes,
3940 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003941
3942 if (skcomp == NULL) {
3943 rc = -ENOMEM;
3944 goto err_out;
3945 }
3946
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003947 skdev->skcomp_table = skcomp;
3948 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3949 sizeof(*skcomp) *
3950 SKD_N_COMPLETION_ENTRY);
3951
3952err_out:
3953 return rc;
3954}
3955
3956static int skd_cons_skmsg(struct skd_device *skdev)
3957{
3958 int rc = 0;
3959 u32 i;
3960
rchinthekindi2e44b422013-10-24 12:51:23 +01003961 pr_debug("%s:%s:%d skmsg_table kzalloc, struct %lu, count %u total %lu\n",
3962 skdev->name, __func__, __LINE__,
3963 sizeof(struct skd_fitmsg_context),
3964 skdev->num_fitmsg_context,
3965 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003966
3967 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
3968 *skdev->num_fitmsg_context, GFP_KERNEL);
3969 if (skdev->skmsg_table == NULL) {
3970 rc = -ENOMEM;
3971 goto err_out;
3972 }
3973
3974 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3975 struct skd_fitmsg_context *skmsg;
3976
3977 skmsg = &skdev->skmsg_table[i];
3978
3979 skmsg->id = i + SKD_ID_FIT_MSG;
3980
3981 skmsg->state = SKD_MSG_STATE_IDLE;
3982 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
3983 SKD_N_FITMSG_BYTES + 64,
3984 &skmsg->mb_dma_address);
3985
3986 if (skmsg->msg_buf == NULL) {
3987 rc = -ENOMEM;
3988 goto err_out;
3989 }
3990
3991 skmsg->offset = (u32)((u64)skmsg->msg_buf &
3992 (~FIT_QCMD_BASE_ADDRESS_MASK));
3993 skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK;
3994 skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf &
3995 FIT_QCMD_BASE_ADDRESS_MASK);
3996 skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK;
3997 skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK;
3998 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
3999
4000 skmsg->next = &skmsg[1];
4001 }
4002
4003 /* Free list is in order starting with the 0th entry. */
4004 skdev->skmsg_table[i - 1].next = NULL;
4005 skdev->skmsg_free_list = skdev->skmsg_table;
4006
4007err_out:
4008 return rc;
4009}
4010
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004011static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
4012 u32 n_sg,
4013 dma_addr_t *ret_dma_addr)
4014{
4015 struct fit_sg_descriptor *sg_list;
4016 u32 nbytes;
4017
4018 nbytes = sizeof(*sg_list) * n_sg;
4019
4020 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
4021
4022 if (sg_list != NULL) {
4023 uint64_t dma_address = *ret_dma_addr;
4024 u32 i;
4025
4026 memset(sg_list, 0, nbytes);
4027
4028 for (i = 0; i < n_sg - 1; i++) {
4029 uint64_t ndp_off;
4030 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
4031
4032 sg_list[i].next_desc_ptr = dma_address + ndp_off;
4033 }
4034 sg_list[i].next_desc_ptr = 0LL;
4035 }
4036
4037 return sg_list;
4038}
4039
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004040static int skd_cons_skreq(struct skd_device *skdev)
4041{
4042 int rc = 0;
4043 u32 i;
4044
rchinthekindi2e44b422013-10-24 12:51:23 +01004045 pr_debug("%s:%s:%d skreq_table kzalloc, struct %lu, count %u total %lu\n",
4046 skdev->name, __func__, __LINE__,
4047 sizeof(struct skd_request_context),
4048 skdev->num_req_context,
4049 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004050
4051 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
4052 * skdev->num_req_context, GFP_KERNEL);
4053 if (skdev->skreq_table == NULL) {
4054 rc = -ENOMEM;
4055 goto err_out;
4056 }
4057
rchinthekindi2e44b422013-10-24 12:51:23 +01004058 pr_debug("%s:%s:%d alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
4059 skdev->name, __func__, __LINE__,
4060 skdev->sgs_per_request, sizeof(struct scatterlist),
4061 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004062
4063 for (i = 0; i < skdev->num_req_context; i++) {
4064 struct skd_request_context *skreq;
4065
4066 skreq = &skdev->skreq_table[i];
4067
4068 skreq->id = i + SKD_ID_RW_REQUEST;
4069 skreq->state = SKD_REQ_STATE_IDLE;
4070
4071 skreq->sg = kzalloc(sizeof(struct scatterlist) *
4072 skdev->sgs_per_request, GFP_KERNEL);
4073 if (skreq->sg == NULL) {
4074 rc = -ENOMEM;
4075 goto err_out;
4076 }
4077 sg_init_table(skreq->sg, skdev->sgs_per_request);
4078
4079 skreq->sksg_list = skd_cons_sg_list(skdev,
4080 skdev->sgs_per_request,
4081 &skreq->sksg_dma_address);
4082
4083 if (skreq->sksg_list == NULL) {
4084 rc = -ENOMEM;
4085 goto err_out;
4086 }
4087
4088 skreq->next = &skreq[1];
4089 }
4090
4091 /* Free list is in order starting with the 0th entry. */
4092 skdev->skreq_table[i - 1].next = NULL;
4093 skdev->skreq_free_list = skdev->skreq_table;
4094
4095err_out:
4096 return rc;
4097}
4098
4099static int skd_cons_skspcl(struct skd_device *skdev)
4100{
4101 int rc = 0;
4102 u32 i, nbytes;
4103
rchinthekindi2e44b422013-10-24 12:51:23 +01004104 pr_debug("%s:%s:%d skspcl_table kzalloc, struct %lu, count %u total %lu\n",
4105 skdev->name, __func__, __LINE__,
4106 sizeof(struct skd_special_context),
4107 skdev->n_special,
4108 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004109
4110 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
4111 * skdev->n_special, GFP_KERNEL);
4112 if (skdev->skspcl_table == NULL) {
4113 rc = -ENOMEM;
4114 goto err_out;
4115 }
4116
4117 for (i = 0; i < skdev->n_special; i++) {
4118 struct skd_special_context *skspcl;
4119
4120 skspcl = &skdev->skspcl_table[i];
4121
4122 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
4123 skspcl->req.state = SKD_REQ_STATE_IDLE;
4124
4125 skspcl->req.next = &skspcl[1].req;
4126
4127 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4128
Joe Perchesa5bbf612014-08-08 14:24:12 -07004129 skspcl->msg_buf =
4130 pci_zalloc_consistent(skdev->pdev, nbytes,
4131 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004132 if (skspcl->msg_buf == NULL) {
4133 rc = -ENOMEM;
4134 goto err_out;
4135 }
4136
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004137 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4138 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4139 if (skspcl->req.sg == NULL) {
4140 rc = -ENOMEM;
4141 goto err_out;
4142 }
4143
4144 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4145 SKD_N_SG_PER_SPECIAL,
4146 &skspcl->req.
4147 sksg_dma_address);
4148 if (skspcl->req.sksg_list == NULL) {
4149 rc = -ENOMEM;
4150 goto err_out;
4151 }
4152 }
4153
4154 /* Free list is in order starting with the 0th entry. */
4155 skdev->skspcl_table[i - 1].req.next = NULL;
4156 skdev->skspcl_free_list = skdev->skspcl_table;
4157
4158 return rc;
4159
4160err_out:
4161 return rc;
4162}
4163
4164static int skd_cons_sksb(struct skd_device *skdev)
4165{
4166 int rc = 0;
4167 struct skd_special_context *skspcl;
4168 u32 nbytes;
4169
4170 skspcl = &skdev->internal_skspcl;
4171
4172 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4173 skspcl->req.state = SKD_REQ_STATE_IDLE;
4174
4175 nbytes = SKD_N_INTERNAL_BYTES;
4176
Joe Perchesa5bbf612014-08-08 14:24:12 -07004177 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4178 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004179 if (skspcl->data_buf == NULL) {
4180 rc = -ENOMEM;
4181 goto err_out;
4182 }
4183
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004184 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004185 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4186 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004187 if (skspcl->msg_buf == NULL) {
4188 rc = -ENOMEM;
4189 goto err_out;
4190 }
4191
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004192 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4193 &skspcl->req.sksg_dma_address);
4194 if (skspcl->req.sksg_list == NULL) {
4195 rc = -ENOMEM;
4196 goto err_out;
4197 }
4198
4199 if (!skd_format_internal_skspcl(skdev)) {
4200 rc = -EINVAL;
4201 goto err_out;
4202 }
4203
4204err_out:
4205 return rc;
4206}
4207
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004208static int skd_cons_disk(struct skd_device *skdev)
4209{
4210 int rc = 0;
4211 struct gendisk *disk;
4212 struct request_queue *q;
4213 unsigned long flags;
4214
4215 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4216 if (!disk) {
4217 rc = -ENOMEM;
4218 goto err_out;
4219 }
4220
4221 skdev->disk = disk;
4222 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4223
4224 disk->major = skdev->major;
4225 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4226 disk->fops = &skd_blockdev_ops;
4227 disk->private_data = skdev;
4228
Jens Axboefcd37eb2013-11-01 10:14:56 -06004229 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004230 if (!q) {
4231 rc = -ENOMEM;
4232 goto err_out;
4233 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +02004234 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004235
4236 skdev->queue = q;
4237 disk->queue = q;
4238 q->queuedata = skdev;
4239
Jens Axboe6975f732016-03-30 10:11:42 -06004240 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004241 blk_queue_max_segments(q, skdev->sgs_per_request);
4242 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4243
Bart Van Asschea5c5b392017-08-17 13:12:53 -07004244 /* set optimal I/O size to 8KB */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004245 blk_queue_io_opt(q, 8192);
4246
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004247 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004248 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004249
4250 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01004251 pr_debug("%s:%s:%d stopping %s queue\n",
4252 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06004253 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004254 spin_unlock_irqrestore(&skdev->lock, flags);
4255
4256err_out:
4257 return rc;
4258}
4259
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004260#define SKD_N_DEV_TABLE 16u
4261static u32 skd_next_devno;
4262
4263static struct skd_device *skd_construct(struct pci_dev *pdev)
4264{
4265 struct skd_device *skdev;
4266 int blk_major = skd_major;
4267 int rc;
4268
4269 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4270
4271 if (!skdev) {
4272 pr_err(PFX "(%s): memory alloc failure\n",
4273 pci_name(pdev));
4274 return NULL;
4275 }
4276
4277 skdev->state = SKD_DRVR_STATE_LOAD;
4278 skdev->pdev = pdev;
4279 skdev->devno = skd_next_devno++;
4280 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004281 sprintf(skdev->name, DRV_NAME "%d", skdev->devno);
4282 skdev->dev_max_queue_depth = 0;
4283
4284 skdev->num_req_context = skd_max_queue_depth;
4285 skdev->num_fitmsg_context = skd_max_queue_depth;
4286 skdev->n_special = skd_max_pass_thru;
4287 skdev->cur_max_queue_depth = 1;
4288 skdev->queue_low_water_mark = 1;
4289 skdev->proto_ver = 99;
4290 skdev->sgs_per_request = skd_sgs_per_request;
4291 skdev->dbg_level = skd_dbg_level;
4292
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004293 spin_lock_init(&skdev->lock);
4294
4295 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4296
4297 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4298 rc = skd_cons_skcomp(skdev);
4299 if (rc < 0)
4300 goto err_out;
4301
4302 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4303 rc = skd_cons_skmsg(skdev);
4304 if (rc < 0)
4305 goto err_out;
4306
4307 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4308 rc = skd_cons_skreq(skdev);
4309 if (rc < 0)
4310 goto err_out;
4311
4312 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4313 rc = skd_cons_skspcl(skdev);
4314 if (rc < 0)
4315 goto err_out;
4316
4317 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4318 rc = skd_cons_sksb(skdev);
4319 if (rc < 0)
4320 goto err_out;
4321
4322 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4323 rc = skd_cons_disk(skdev);
4324 if (rc < 0)
4325 goto err_out;
4326
4327 pr_debug("%s:%s:%d VICTORY\n", skdev->name, __func__, __LINE__);
4328 return skdev;
4329
4330err_out:
4331 pr_debug("%s:%s:%d construct failed\n",
4332 skdev->name, __func__, __LINE__);
4333 skd_destruct(skdev);
4334 return NULL;
4335}
4336
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004337/*
4338 *****************************************************************************
4339 * DESTRUCT (FREE)
4340 *****************************************************************************
4341 */
4342
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004343static void skd_free_skcomp(struct skd_device *skdev)
4344{
4345 if (skdev->skcomp_table != NULL) {
4346 u32 nbytes;
4347
4348 nbytes = sizeof(skdev->skcomp_table[0]) *
4349 SKD_N_COMPLETION_ENTRY;
4350 pci_free_consistent(skdev->pdev, nbytes,
4351 skdev->skcomp_table, skdev->cq_dma_address);
4352 }
4353
4354 skdev->skcomp_table = NULL;
4355 skdev->cq_dma_address = 0;
4356}
4357
4358static void skd_free_skmsg(struct skd_device *skdev)
4359{
4360 u32 i;
4361
4362 if (skdev->skmsg_table == NULL)
4363 return;
4364
4365 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4366 struct skd_fitmsg_context *skmsg;
4367
4368 skmsg = &skdev->skmsg_table[i];
4369
4370 if (skmsg->msg_buf != NULL) {
4371 skmsg->msg_buf += skmsg->offset;
4372 skmsg->mb_dma_address += skmsg->offset;
4373 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4374 skmsg->msg_buf,
4375 skmsg->mb_dma_address);
4376 }
4377 skmsg->msg_buf = NULL;
4378 skmsg->mb_dma_address = 0;
4379 }
4380
4381 kfree(skdev->skmsg_table);
4382 skdev->skmsg_table = NULL;
4383}
4384
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004385static void skd_free_sg_list(struct skd_device *skdev,
4386 struct fit_sg_descriptor *sg_list,
4387 u32 n_sg, dma_addr_t dma_addr)
4388{
4389 if (sg_list != NULL) {
4390 u32 nbytes;
4391
4392 nbytes = sizeof(*sg_list) * n_sg;
4393
4394 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4395 }
4396}
4397
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004398static void skd_free_skreq(struct skd_device *skdev)
4399{
4400 u32 i;
4401
4402 if (skdev->skreq_table == NULL)
4403 return;
4404
4405 for (i = 0; i < skdev->num_req_context; i++) {
4406 struct skd_request_context *skreq;
4407
4408 skreq = &skdev->skreq_table[i];
4409
4410 skd_free_sg_list(skdev, skreq->sksg_list,
4411 skdev->sgs_per_request,
4412 skreq->sksg_dma_address);
4413
4414 skreq->sksg_list = NULL;
4415 skreq->sksg_dma_address = 0;
4416
4417 kfree(skreq->sg);
4418 }
4419
4420 kfree(skdev->skreq_table);
4421 skdev->skreq_table = NULL;
4422}
4423
4424static void skd_free_skspcl(struct skd_device *skdev)
4425{
4426 u32 i;
4427 u32 nbytes;
4428
4429 if (skdev->skspcl_table == NULL)
4430 return;
4431
4432 for (i = 0; i < skdev->n_special; i++) {
4433 struct skd_special_context *skspcl;
4434
4435 skspcl = &skdev->skspcl_table[i];
4436
4437 if (skspcl->msg_buf != NULL) {
4438 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4439 pci_free_consistent(skdev->pdev, nbytes,
4440 skspcl->msg_buf,
4441 skspcl->mb_dma_address);
4442 }
4443
4444 skspcl->msg_buf = NULL;
4445 skspcl->mb_dma_address = 0;
4446
4447 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4448 SKD_N_SG_PER_SPECIAL,
4449 skspcl->req.sksg_dma_address);
4450
4451 skspcl->req.sksg_list = NULL;
4452 skspcl->req.sksg_dma_address = 0;
4453
4454 kfree(skspcl->req.sg);
4455 }
4456
4457 kfree(skdev->skspcl_table);
4458 skdev->skspcl_table = NULL;
4459}
4460
4461static void skd_free_sksb(struct skd_device *skdev)
4462{
4463 struct skd_special_context *skspcl;
4464 u32 nbytes;
4465
4466 skspcl = &skdev->internal_skspcl;
4467
4468 if (skspcl->data_buf != NULL) {
4469 nbytes = SKD_N_INTERNAL_BYTES;
4470
4471 pci_free_consistent(skdev->pdev, nbytes,
4472 skspcl->data_buf, skspcl->db_dma_address);
4473 }
4474
4475 skspcl->data_buf = NULL;
4476 skspcl->db_dma_address = 0;
4477
4478 if (skspcl->msg_buf != NULL) {
4479 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4480 pci_free_consistent(skdev->pdev, nbytes,
4481 skspcl->msg_buf, skspcl->mb_dma_address);
4482 }
4483
4484 skspcl->msg_buf = NULL;
4485 skspcl->mb_dma_address = 0;
4486
4487 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4488 skspcl->req.sksg_dma_address);
4489
4490 skspcl->req.sksg_list = NULL;
4491 skspcl->req.sksg_dma_address = 0;
4492}
4493
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004494static void skd_free_disk(struct skd_device *skdev)
4495{
4496 struct gendisk *disk = skdev->disk;
4497
Bart Van Assche7277cc62017-08-17 13:12:45 -07004498 if (disk && (disk->flags & GENHD_FL_UP))
4499 del_gendisk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004500
Bart Van Assche7277cc62017-08-17 13:12:45 -07004501 if (skdev->queue) {
4502 blk_cleanup_queue(skdev->queue);
4503 skdev->queue = NULL;
4504 disk->queue = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004505 }
Bart Van Assche7277cc62017-08-17 13:12:45 -07004506
4507 put_disk(disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004508 skdev->disk = NULL;
4509}
4510
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004511static void skd_destruct(struct skd_device *skdev)
4512{
4513 if (skdev == NULL)
4514 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004515
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004516 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4517 skd_free_disk(skdev);
4518
4519 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4520 skd_free_sksb(skdev);
4521
4522 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4523 skd_free_skspcl(skdev);
4524
4525 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4526 skd_free_skreq(skdev);
4527
4528 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4529 skd_free_skmsg(skdev);
4530
4531 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4532 skd_free_skcomp(skdev);
4533
4534 pr_debug("%s:%s:%d skdev\n", skdev->name, __func__, __LINE__);
4535 kfree(skdev);
4536}
4537
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004538/*
4539 *****************************************************************************
4540 * BLOCK DEVICE (BDEV) GLUE
4541 *****************************************************************************
4542 */
4543
4544static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4545{
4546 struct skd_device *skdev;
4547 u64 capacity;
4548
4549 skdev = bdev->bd_disk->private_data;
4550
rchinthekindi2e44b422013-10-24 12:51:23 +01004551 pr_debug("%s:%s:%d %s: CMD[%s] getgeo device\n",
4552 skdev->name, __func__, __LINE__,
4553 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004554
4555 if (skdev->read_cap_is_valid) {
4556 capacity = get_capacity(skdev->disk);
4557 geo->heads = 64;
4558 geo->sectors = 255;
4559 geo->cylinders = (capacity) / (255 * 64);
4560
4561 return 0;
4562 }
4563 return -EIO;
4564}
4565
Dan Williams0d52c7562016-06-15 19:44:20 -07004566static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004567{
rchinthekindi2e44b422013-10-24 12:51:23 +01004568 pr_debug("%s:%s:%d add_disk\n", skdev->name, __func__, __LINE__);
Dan Williams0d52c7562016-06-15 19:44:20 -07004569 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004570 return 0;
4571}
4572
4573static const struct block_device_operations skd_blockdev_ops = {
4574 .owner = THIS_MODULE,
4575 .ioctl = skd_bdev_ioctl,
4576 .getgeo = skd_bdev_getgeo,
4577};
4578
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004579/*
4580 *****************************************************************************
4581 * PCIe DRIVER GLUE
4582 *****************************************************************************
4583 */
4584
Benoit Taine9baa3c32014-08-08 15:56:03 +02004585static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004586 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4587 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4588 { 0 } /* terminate list */
4589};
4590
4591MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4592
4593static char *skd_pci_info(struct skd_device *skdev, char *str)
4594{
4595 int pcie_reg;
4596
4597 strcpy(str, "PCIe (");
4598 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4599
4600 if (pcie_reg) {
4601
4602 char lwstr[6];
4603 uint16_t pcie_lstat, lspeed, lwidth;
4604
4605 pcie_reg += 0x12;
4606 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4607 lspeed = pcie_lstat & (0xF);
4608 lwidth = (pcie_lstat & 0x3F0) >> 4;
4609
4610 if (lspeed == 1)
4611 strcat(str, "2.5GT/s ");
4612 else if (lspeed == 2)
4613 strcat(str, "5.0GT/s ");
4614 else
4615 strcat(str, "<unknown> ");
4616 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4617 strcat(str, lwstr);
4618 }
4619 return str;
4620}
4621
4622static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4623{
4624 int i;
4625 int rc = 0;
4626 char pci_str[32];
4627 struct skd_device *skdev;
4628
4629 pr_info("STEC s1120 Driver(%s) version %s-b%s\n",
4630 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4631 pr_info("(skd?:??:[%s]): vendor=%04X device=%04x\n",
4632 pci_name(pdev), pdev->vendor, pdev->device);
4633
4634 rc = pci_enable_device(pdev);
4635 if (rc)
4636 return rc;
4637 rc = pci_request_regions(pdev, DRV_NAME);
4638 if (rc)
4639 goto err_out;
4640 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4641 if (!rc) {
4642 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4643
4644 pr_err("(%s): consistent DMA mask error %d\n",
4645 pci_name(pdev), rc);
4646 }
4647 } else {
4648 (rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)));
4649 if (rc) {
4650
4651 pr_err("(%s): DMA mask error %d\n",
4652 pci_name(pdev), rc);
4653 goto err_out_regions;
4654 }
4655 }
4656
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004657 if (!skd_major) {
4658 rc = register_blkdev(0, DRV_NAME);
4659 if (rc < 0)
4660 goto err_out_regions;
4661 BUG_ON(!rc);
4662 skd_major = rc;
4663 }
4664
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004665 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004666 if (skdev == NULL) {
4667 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004668 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004669 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004670
4671 skd_pci_info(skdev, pci_str);
4672 pr_info("(%s): %s 64bit\n", skd_name(skdev), pci_str);
4673
4674 pci_set_master(pdev);
4675 rc = pci_enable_pcie_error_reporting(pdev);
4676 if (rc) {
Bart Van Assche5477e1b2017-08-17 13:12:51 -07004677 pr_err("(%s): bad enable of PCIe error reporting rc=%d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004678 skd_name(skdev), rc);
4679 skdev->pcie_error_reporting_is_enabled = 0;
4680 } else
4681 skdev->pcie_error_reporting_is_enabled = 1;
4682
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004683 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004684
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004685 for (i = 0; i < SKD_MAX_BARS; i++) {
4686 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4687 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4688 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4689 skdev->mem_size[i]);
4690 if (!skdev->mem_map[i]) {
4691 pr_err("(%s): Unable to map adapter memory!\n",
4692 skd_name(skdev));
4693 rc = -ENODEV;
4694 goto err_out_iounmap;
4695 }
rchinthekindi2e44b422013-10-24 12:51:23 +01004696 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
4697 skdev->name, __func__, __LINE__,
4698 skdev->mem_map[i],
4699 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004700 }
4701
4702 rc = skd_acquire_irq(skdev);
4703 if (rc) {
4704 pr_err("(%s): interrupt resource error %d\n",
4705 skd_name(skdev), rc);
4706 goto err_out_iounmap;
4707 }
4708
4709 rc = skd_start_timer(skdev);
4710 if (rc)
4711 goto err_out_timer;
4712
4713 init_waitqueue_head(&skdev->waitq);
4714
4715 skd_start_device(skdev);
4716
4717 rc = wait_event_interruptible_timeout(skdev->waitq,
4718 (skdev->gendisk_on),
4719 (SKD_START_WAIT_SECONDS * HZ));
4720 if (skdev->gendisk_on > 0) {
4721 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004722 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004723 rc = 0;
4724 } else {
4725 /* we timed out, something is wrong with the device,
4726 don't add the disk structure */
Bart Van Assche5477e1b2017-08-17 13:12:51 -07004727 pr_err("(%s): error: waiting for s1120 timed out %d!\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004728 skd_name(skdev), rc);
4729 /* in case of no error; we timeout with ENXIO */
4730 if (!rc)
4731 rc = -ENXIO;
4732 goto err_out_timer;
4733 }
4734
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004735 return rc;
4736
4737err_out_timer:
4738 skd_stop_device(skdev);
4739 skd_release_irq(skdev);
4740
4741err_out_iounmap:
4742 for (i = 0; i < SKD_MAX_BARS; i++)
4743 if (skdev->mem_map[i])
4744 iounmap(skdev->mem_map[i]);
4745
4746 if (skdev->pcie_error_reporting_is_enabled)
4747 pci_disable_pcie_error_reporting(pdev);
4748
4749 skd_destruct(skdev);
4750
4751err_out_regions:
4752 pci_release_regions(pdev);
4753
4754err_out:
4755 pci_disable_device(pdev);
4756 pci_set_drvdata(pdev, NULL);
4757 return rc;
4758}
4759
4760static void skd_pci_remove(struct pci_dev *pdev)
4761{
4762 int i;
4763 struct skd_device *skdev;
4764
4765 skdev = pci_get_drvdata(pdev);
4766 if (!skdev) {
4767 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4768 return;
4769 }
4770 skd_stop_device(skdev);
4771 skd_release_irq(skdev);
4772
4773 for (i = 0; i < SKD_MAX_BARS; i++)
4774 if (skdev->mem_map[i])
4775 iounmap((u32 *)skdev->mem_map[i]);
4776
4777 if (skdev->pcie_error_reporting_is_enabled)
4778 pci_disable_pcie_error_reporting(pdev);
4779
4780 skd_destruct(skdev);
4781
4782 pci_release_regions(pdev);
4783 pci_disable_device(pdev);
4784 pci_set_drvdata(pdev, NULL);
4785
4786 return;
4787}
4788
4789static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4790{
4791 int i;
4792 struct skd_device *skdev;
4793
4794 skdev = pci_get_drvdata(pdev);
4795 if (!skdev) {
4796 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4797 return -EIO;
4798 }
4799
4800 skd_stop_device(skdev);
4801
4802 skd_release_irq(skdev);
4803
4804 for (i = 0; i < SKD_MAX_BARS; i++)
4805 if (skdev->mem_map[i])
4806 iounmap((u32 *)skdev->mem_map[i]);
4807
4808 if (skdev->pcie_error_reporting_is_enabled)
4809 pci_disable_pcie_error_reporting(pdev);
4810
4811 pci_release_regions(pdev);
4812 pci_save_state(pdev);
4813 pci_disable_device(pdev);
4814 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4815 return 0;
4816}
4817
4818static int skd_pci_resume(struct pci_dev *pdev)
4819{
4820 int i;
4821 int rc = 0;
4822 struct skd_device *skdev;
4823
4824 skdev = pci_get_drvdata(pdev);
4825 if (!skdev) {
4826 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4827 return -1;
4828 }
4829
4830 pci_set_power_state(pdev, PCI_D0);
4831 pci_enable_wake(pdev, PCI_D0, 0);
4832 pci_restore_state(pdev);
4833
4834 rc = pci_enable_device(pdev);
4835 if (rc)
4836 return rc;
4837 rc = pci_request_regions(pdev, DRV_NAME);
4838 if (rc)
4839 goto err_out;
4840 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4841 if (!rc) {
4842 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4843
4844 pr_err("(%s): consistent DMA mask error %d\n",
4845 pci_name(pdev), rc);
4846 }
4847 } else {
4848 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4849 if (rc) {
4850
4851 pr_err("(%s): DMA mask error %d\n",
4852 pci_name(pdev), rc);
4853 goto err_out_regions;
4854 }
4855 }
4856
4857 pci_set_master(pdev);
4858 rc = pci_enable_pcie_error_reporting(pdev);
4859 if (rc) {
4860 pr_err("(%s): bad enable of PCIe error reporting rc=%d\n",
4861 skdev->name, rc);
4862 skdev->pcie_error_reporting_is_enabled = 0;
4863 } else
4864 skdev->pcie_error_reporting_is_enabled = 1;
4865
4866 for (i = 0; i < SKD_MAX_BARS; i++) {
4867
4868 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4869 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4870 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4871 skdev->mem_size[i]);
4872 if (!skdev->mem_map[i]) {
4873 pr_err("(%s): Unable to map adapter memory!\n",
4874 skd_name(skdev));
4875 rc = -ENODEV;
4876 goto err_out_iounmap;
4877 }
rchinthekindi2e44b422013-10-24 12:51:23 +01004878 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
4879 skdev->name, __func__, __LINE__,
4880 skdev->mem_map[i],
4881 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004882 }
4883 rc = skd_acquire_irq(skdev);
4884 if (rc) {
4885
4886 pr_err("(%s): interrupt resource error %d\n",
4887 pci_name(pdev), rc);
4888 goto err_out_iounmap;
4889 }
4890
4891 rc = skd_start_timer(skdev);
4892 if (rc)
4893 goto err_out_timer;
4894
4895 init_waitqueue_head(&skdev->waitq);
4896
4897 skd_start_device(skdev);
4898
4899 return rc;
4900
4901err_out_timer:
4902 skd_stop_device(skdev);
4903 skd_release_irq(skdev);
4904
4905err_out_iounmap:
4906 for (i = 0; i < SKD_MAX_BARS; i++)
4907 if (skdev->mem_map[i])
4908 iounmap(skdev->mem_map[i]);
4909
4910 if (skdev->pcie_error_reporting_is_enabled)
4911 pci_disable_pcie_error_reporting(pdev);
4912
4913err_out_regions:
4914 pci_release_regions(pdev);
4915
4916err_out:
4917 pci_disable_device(pdev);
4918 return rc;
4919}
4920
4921static void skd_pci_shutdown(struct pci_dev *pdev)
4922{
4923 struct skd_device *skdev;
4924
4925 pr_err("skd_pci_shutdown called\n");
4926
4927 skdev = pci_get_drvdata(pdev);
4928 if (!skdev) {
4929 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4930 return;
4931 }
4932
4933 pr_err("%s: calling stop\n", skd_name(skdev));
4934 skd_stop_device(skdev);
4935}
4936
4937static struct pci_driver skd_driver = {
4938 .name = DRV_NAME,
4939 .id_table = skd_pci_tbl,
4940 .probe = skd_pci_probe,
4941 .remove = skd_pci_remove,
4942 .suspend = skd_pci_suspend,
4943 .resume = skd_pci_resume,
4944 .shutdown = skd_pci_shutdown,
4945};
4946
4947/*
4948 *****************************************************************************
4949 * LOGGING SUPPORT
4950 *****************************************************************************
4951 */
4952
4953static const char *skd_name(struct skd_device *skdev)
4954{
4955 memset(skdev->id_str, 0, sizeof(skdev->id_str));
4956
4957 if (skdev->inquiry_is_valid)
4958 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:%s:[%s]",
4959 skdev->name, skdev->inq_serial_num,
4960 pci_name(skdev->pdev));
4961 else
4962 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:??:[%s]",
4963 skdev->name, pci_name(skdev->pdev));
4964
4965 return skdev->id_str;
4966}
4967
4968const char *skd_drive_state_to_str(int state)
4969{
4970 switch (state) {
4971 case FIT_SR_DRIVE_OFFLINE:
4972 return "OFFLINE";
4973 case FIT_SR_DRIVE_INIT:
4974 return "INIT";
4975 case FIT_SR_DRIVE_ONLINE:
4976 return "ONLINE";
4977 case FIT_SR_DRIVE_BUSY:
4978 return "BUSY";
4979 case FIT_SR_DRIVE_FAULT:
4980 return "FAULT";
4981 case FIT_SR_DRIVE_DEGRADED:
4982 return "DEGRADED";
4983 case FIT_SR_PCIE_LINK_DOWN:
4984 return "INK_DOWN";
4985 case FIT_SR_DRIVE_SOFT_RESET:
4986 return "SOFT_RESET";
4987 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
4988 return "NEED_FW";
4989 case FIT_SR_DRIVE_INIT_FAULT:
4990 return "INIT_FAULT";
4991 case FIT_SR_DRIVE_BUSY_SANITIZE:
4992 return "BUSY_SANITIZE";
4993 case FIT_SR_DRIVE_BUSY_ERASE:
4994 return "BUSY_ERASE";
4995 case FIT_SR_DRIVE_FW_BOOTING:
4996 return "FW_BOOTING";
4997 default:
4998 return "???";
4999 }
5000}
5001
5002const char *skd_skdev_state_to_str(enum skd_drvr_state state)
5003{
5004 switch (state) {
5005 case SKD_DRVR_STATE_LOAD:
5006 return "LOAD";
5007 case SKD_DRVR_STATE_IDLE:
5008 return "IDLE";
5009 case SKD_DRVR_STATE_BUSY:
5010 return "BUSY";
5011 case SKD_DRVR_STATE_STARTING:
5012 return "STARTING";
5013 case SKD_DRVR_STATE_ONLINE:
5014 return "ONLINE";
5015 case SKD_DRVR_STATE_PAUSING:
5016 return "PAUSING";
5017 case SKD_DRVR_STATE_PAUSED:
5018 return "PAUSED";
5019 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
5020 return "DRAINING_TIMEOUT";
5021 case SKD_DRVR_STATE_RESTARTING:
5022 return "RESTARTING";
5023 case SKD_DRVR_STATE_RESUMING:
5024 return "RESUMING";
5025 case SKD_DRVR_STATE_STOPPING:
5026 return "STOPPING";
5027 case SKD_DRVR_STATE_SYNCING:
5028 return "SYNCING";
5029 case SKD_DRVR_STATE_FAULT:
5030 return "FAULT";
5031 case SKD_DRVR_STATE_DISAPPEARED:
5032 return "DISAPPEARED";
5033 case SKD_DRVR_STATE_BUSY_ERASE:
5034 return "BUSY_ERASE";
5035 case SKD_DRVR_STATE_BUSY_SANITIZE:
5036 return "BUSY_SANITIZE";
5037 case SKD_DRVR_STATE_BUSY_IMMINENT:
5038 return "BUSY_IMMINENT";
5039 case SKD_DRVR_STATE_WAIT_BOOT:
5040 return "WAIT_BOOT";
5041
5042 default:
5043 return "???";
5044 }
5045}
5046
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05305047static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005048{
5049 switch (state) {
5050 case SKD_MSG_STATE_IDLE:
5051 return "IDLE";
5052 case SKD_MSG_STATE_BUSY:
5053 return "BUSY";
5054 default:
5055 return "???";
5056 }
5057}
5058
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05305059static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005060{
5061 switch (state) {
5062 case SKD_REQ_STATE_IDLE:
5063 return "IDLE";
5064 case SKD_REQ_STATE_SETUP:
5065 return "SETUP";
5066 case SKD_REQ_STATE_BUSY:
5067 return "BUSY";
5068 case SKD_REQ_STATE_COMPLETED:
5069 return "COMPLETED";
5070 case SKD_REQ_STATE_TIMEOUT:
5071 return "TIMEOUT";
5072 case SKD_REQ_STATE_ABORTED:
5073 return "ABORTED";
5074 default:
5075 return "???";
5076 }
5077}
5078
5079static void skd_log_skdev(struct skd_device *skdev, const char *event)
5080{
rchinthekindi2e44b422013-10-24 12:51:23 +01005081 pr_debug("%s:%s:%d (%s) skdev=%p event='%s'\n",
5082 skdev->name, __func__, __LINE__, skdev->name, skdev, event);
5083 pr_debug("%s:%s:%d drive_state=%s(%d) driver_state=%s(%d)\n",
5084 skdev->name, __func__, __LINE__,
5085 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
5086 skd_skdev_state_to_str(skdev->state), skdev->state);
5087 pr_debug("%s:%s:%d busy=%d limit=%d dev=%d lowat=%d\n",
5088 skdev->name, __func__, __LINE__,
5089 skdev->in_flight, skdev->cur_max_queue_depth,
5090 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
5091 pr_debug("%s:%s:%d timestamp=0x%x cycle=%d cycle_ix=%d\n",
5092 skdev->name, __func__, __LINE__,
5093 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005094}
5095
5096static void skd_log_skmsg(struct skd_device *skdev,
5097 struct skd_fitmsg_context *skmsg, const char *event)
5098{
rchinthekindi2e44b422013-10-24 12:51:23 +01005099 pr_debug("%s:%s:%d (%s) skmsg=%p event='%s'\n",
5100 skdev->name, __func__, __LINE__, skdev->name, skmsg, event);
5101 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x length=%d\n",
5102 skdev->name, __func__, __LINE__,
5103 skd_skmsg_state_to_str(skmsg->state), skmsg->state,
5104 skmsg->id, skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005105}
5106
5107static void skd_log_skreq(struct skd_device *skdev,
5108 struct skd_request_context *skreq, const char *event)
5109{
rchinthekindi2e44b422013-10-24 12:51:23 +01005110 pr_debug("%s:%s:%d (%s) skreq=%p event='%s'\n",
5111 skdev->name, __func__, __LINE__, skdev->name, skreq, event);
5112 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
5113 skdev->name, __func__, __LINE__,
5114 skd_skreq_state_to_str(skreq->state), skreq->state,
5115 skreq->id, skreq->fitmsg_id);
5116 pr_debug("%s:%s:%d timo=0x%x sg_dir=%d n_sg=%d\n",
5117 skdev->name, __func__, __LINE__,
5118 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005119
Jens Axboefcd37eb2013-11-01 10:14:56 -06005120 if (skreq->req != NULL) {
5121 struct request *req = skreq->req;
5122 u32 lba = (u32)blk_rq_pos(req);
5123 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005124
Jens Axboefcd37eb2013-11-01 10:14:56 -06005125 pr_debug("%s:%s:%d "
5126 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
5127 skdev->name, __func__, __LINE__,
5128 req, lba, lba, count, count,
5129 (int)rq_data_dir(req));
5130 } else
5131 pr_debug("%s:%s:%d req=NULL\n",
5132 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005133}
5134
5135/*
5136 *****************************************************************************
5137 * MODULE GLUE
5138 *****************************************************************************
5139 */
5140
5141static int __init skd_init(void)
5142{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005143 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
5144
5145 switch (skd_isr_type) {
5146 case SKD_IRQ_LEGACY:
5147 case SKD_IRQ_MSI:
5148 case SKD_IRQ_MSIX:
5149 break;
5150 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005151 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005152 skd_isr_type, SKD_IRQ_DEFAULT);
5153 skd_isr_type = SKD_IRQ_DEFAULT;
5154 }
5155
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005156 if (skd_max_queue_depth < 1 ||
5157 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
5158 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005159 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
5160 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
5161 }
5162
5163 if (skd_max_req_per_msg < 1 || skd_max_req_per_msg > 14) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005164 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005165 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
5166 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
5167 }
5168
5169 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005170 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005171 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
5172 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5173 }
5174
5175 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005176 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005177 skd_dbg_level, 0);
5178 skd_dbg_level = 0;
5179 }
5180
5181 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005182 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005183 skd_isr_comp_limit, 0);
5184 skd_isr_comp_limit = 0;
5185 }
5186
5187 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005188 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005189 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5190 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5191 }
5192
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005193 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005194}
5195
5196static void __exit skd_exit(void)
5197{
5198 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5199
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005200 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005201
5202 if (skd_major)
5203 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005204}
5205
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005206module_init(skd_init);
5207module_exit(skd_exit);