blob: 3822eae102db33ebba2fe4e2ec9d80a59c077c21 [file] [log] [blame]
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001/* Copyright 2012 STEC, Inc.
2 *
3 * This file is licensed under the terms of the 3-clause
4 * BSD License (http://opensource.org/licenses/BSD-3-Clause)
5 * or the GNU GPL-2.0 (http://www.gnu.org/licenses/gpl-2.0.html),
6 * at your option. Both licenses are also available in the LICENSE file
7 * distributed with this project. This file may not be copied, modified,
8 * or distributed except in accordance with those terms.
9 * Gordoni Waidhofer <gwaidhofer@stec-inc.com>
10 * Initial Driver Design!
11 * Thomas Swann <tswann@stec-inc.com>
12 * Interrupt handling.
13 * Ramprasad Chinthekindi <rchinthekindi@stec-inc.com>
14 * biomode implementation.
15 * Akhil Bhansali <abhansali@stec-inc.com>
16 * Added support for DISCARD / FLUSH and FUA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/pci.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25#include <linux/blkdev.h>
26#include <linux/sched.h>
27#include <linux/interrupt.h>
28#include <linux/compiler.h>
29#include <linux/workqueue.h>
30#include <linux/bitops.h>
31#include <linux/delay.h>
32#include <linux/time.h>
33#include <linux/hdreg.h>
34#include <linux/dma-mapping.h>
35#include <linux/completion.h>
36#include <linux/scatterlist.h>
37#include <linux/version.h>
38#include <linux/err.h>
39#include <linux/scatterlist.h>
40#include <linux/aer.h>
41#include <linux/ctype.h>
42#include <linux/wait.h>
43#include <linux/uio.h>
44#include <scsi/scsi.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060045#include <scsi/sg.h>
46#include <linux/io.h>
47#include <linux/uaccess.h>
Bartlomiej Zolnierkiewicz4ca90b52013-11-05 12:37:04 +010048#include <asm/unaligned.h>
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060049
50#include "skd_s1120.h"
51
52static int skd_dbg_level;
53static int skd_isr_comp_limit = 4;
54
55enum {
56 STEC_LINK_2_5GTS = 0,
57 STEC_LINK_5GTS = 1,
58 STEC_LINK_8GTS = 2,
59 STEC_LINK_UNKNOWN = 0xFF
60};
61
62enum {
63 SKD_FLUSH_INITIALIZER,
64 SKD_FLUSH_ZERO_SIZE_FIRST,
65 SKD_FLUSH_DATA_SECOND,
66};
67
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060068#define SKD_ASSERT(expr) \
69 do { \
70 if (unlikely(!(expr))) { \
71 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
72 # expr, __FILE__, __func__, __LINE__); \
73 } \
74 } while (0)
75
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060076#define DRV_NAME "skd"
77#define DRV_VERSION "2.2.1"
78#define DRV_BUILD_ID "0260"
79#define PFX DRV_NAME ": "
80#define DRV_BIN_VERSION 0x100
81#define DRV_VER_COMPL "2.2.1." DRV_BUILD_ID
82
83MODULE_AUTHOR("bug-reports: support@stec-inc.com");
84MODULE_LICENSE("Dual BSD/GPL");
85
Mike Snitzer38d4a1b2013-11-01 15:05:10 -040086MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
Akhil Bhansalie67f86b2013-10-15 14:19:07 -060087MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
88
89#define PCI_VENDOR_ID_STEC 0x1B39
90#define PCI_DEVICE_ID_S1120 0x0001
91
92#define SKD_FUA_NV (1 << 1)
93#define SKD_MINORS_PER_DEVICE 16
94
95#define SKD_MAX_QUEUE_DEPTH 200u
96
97#define SKD_PAUSE_TIMEOUT (5 * 1000)
98
99#define SKD_N_FITMSG_BYTES (512u)
100
101#define SKD_N_SPECIAL_CONTEXT 32u
102#define SKD_N_SPECIAL_FITMSG_BYTES (128u)
103
104/* SG elements are 32 bytes, so we can make this 4096 and still be under the
105 * 128KB limit. That allows 4096*4K = 16M xfer size
106 */
107#define SKD_N_SG_PER_REQ_DEFAULT 256u
108#define SKD_N_SG_PER_SPECIAL 256u
109
110#define SKD_N_COMPLETION_ENTRY 256u
111#define SKD_N_READ_CAP_BYTES (8u)
112
113#define SKD_N_INTERNAL_BYTES (512u)
114
115/* 5 bits of uniqifier, 0xF800 */
116#define SKD_ID_INCR (0x400)
117#define SKD_ID_TABLE_MASK (3u << 8u)
118#define SKD_ID_RW_REQUEST (0u << 8u)
119#define SKD_ID_INTERNAL (1u << 8u)
120#define SKD_ID_SPECIAL_REQUEST (2u << 8u)
121#define SKD_ID_FIT_MSG (3u << 8u)
122#define SKD_ID_SLOT_MASK 0x00FFu
123#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
124
125#define SKD_N_TIMEOUT_SLOT 4u
126#define SKD_TIMEOUT_SLOT_MASK 3u
127
128#define SKD_N_MAX_SECTORS 2048u
129
130#define SKD_MAX_RETRIES 2u
131
132#define SKD_TIMER_SECONDS(seconds) (seconds)
133#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
134
135#define INQ_STD_NBYTES 36
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600136
137enum skd_drvr_state {
138 SKD_DRVR_STATE_LOAD,
139 SKD_DRVR_STATE_IDLE,
140 SKD_DRVR_STATE_BUSY,
141 SKD_DRVR_STATE_STARTING,
142 SKD_DRVR_STATE_ONLINE,
143 SKD_DRVR_STATE_PAUSING,
144 SKD_DRVR_STATE_PAUSED,
145 SKD_DRVR_STATE_DRAINING_TIMEOUT,
146 SKD_DRVR_STATE_RESTARTING,
147 SKD_DRVR_STATE_RESUMING,
148 SKD_DRVR_STATE_STOPPING,
149 SKD_DRVR_STATE_FAULT,
150 SKD_DRVR_STATE_DISAPPEARED,
151 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
152 SKD_DRVR_STATE_BUSY_ERASE,
153 SKD_DRVR_STATE_BUSY_SANITIZE,
154 SKD_DRVR_STATE_BUSY_IMMINENT,
155 SKD_DRVR_STATE_WAIT_BOOT,
156 SKD_DRVR_STATE_SYNCING,
157};
158
159#define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
160#define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
161#define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
162#define SKD_DRAINING_TIMO SKD_TIMER_SECONDS(6u)
163#define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
164#define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
165#define SKD_START_WAIT_SECONDS 90u
166
167enum skd_req_state {
168 SKD_REQ_STATE_IDLE,
169 SKD_REQ_STATE_SETUP,
170 SKD_REQ_STATE_BUSY,
171 SKD_REQ_STATE_COMPLETED,
172 SKD_REQ_STATE_TIMEOUT,
173 SKD_REQ_STATE_ABORTED,
174};
175
176enum skd_fit_msg_state {
177 SKD_MSG_STATE_IDLE,
178 SKD_MSG_STATE_BUSY,
179};
180
181enum skd_check_status_action {
182 SKD_CHECK_STATUS_REPORT_GOOD,
183 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
184 SKD_CHECK_STATUS_REQUEUE_REQUEST,
185 SKD_CHECK_STATUS_REPORT_ERROR,
186 SKD_CHECK_STATUS_BUSY_IMMINENT,
187};
188
189struct skd_fitmsg_context {
190 enum skd_fit_msg_state state;
191
192 struct skd_fitmsg_context *next;
193
194 u32 id;
195 u16 outstanding;
196
197 u32 length;
198 u32 offset;
199
200 u8 *msg_buf;
201 dma_addr_t mb_dma_address;
202};
203
204struct skd_request_context {
205 enum skd_req_state state;
206
207 struct skd_request_context *next;
208
209 u16 id;
210 u32 fitmsg_id;
211
212 struct request *req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600213 u8 flush_cmd;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600214
215 u32 timeout_stamp;
216 u8 sg_data_dir;
217 struct scatterlist *sg;
218 u32 n_sg;
219 u32 sg_byte_count;
220
221 struct fit_sg_descriptor *sksg_list;
222 dma_addr_t sksg_dma_address;
223
224 struct fit_completion_entry_v1 completion;
225
226 struct fit_comp_error_info err_info;
227
228};
229#define SKD_DATA_DIR_HOST_TO_CARD 1
230#define SKD_DATA_DIR_CARD_TO_HOST 2
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600231
232struct skd_special_context {
233 struct skd_request_context req;
234
235 u8 orphaned;
236
237 void *data_buf;
238 dma_addr_t db_dma_address;
239
240 u8 *msg_buf;
241 dma_addr_t mb_dma_address;
242};
243
244struct skd_sg_io {
245 fmode_t mode;
246 void __user *argp;
247
248 struct sg_io_hdr sg;
249
250 u8 cdb[16];
251
252 u32 dxfer_len;
253 u32 iovcnt;
254 struct sg_iovec *iov;
255 struct sg_iovec no_iov_iov;
256
257 struct skd_special_context *skspcl;
258};
259
260typedef enum skd_irq_type {
261 SKD_IRQ_LEGACY,
262 SKD_IRQ_MSI,
263 SKD_IRQ_MSIX
264} skd_irq_type_t;
265
266#define SKD_MAX_BARS 2
267
268struct skd_device {
269 volatile void __iomem *mem_map[SKD_MAX_BARS];
270 resource_size_t mem_phys[SKD_MAX_BARS];
271 u32 mem_size[SKD_MAX_BARS];
272
273 skd_irq_type_t irq_type;
274 u32 msix_count;
275 struct skd_msix_entry *msix_entries;
276
277 struct pci_dev *pdev;
278 int pcie_error_reporting_is_enabled;
279
280 spinlock_t lock;
281 struct gendisk *disk;
282 struct request_queue *queue;
283 struct device *class_dev;
284 int gendisk_on;
285 int sync_done;
286
287 atomic_t device_count;
288 u32 devno;
289 u32 major;
290 char name[32];
291 char isr_name[30];
292
293 enum skd_drvr_state state;
294 u32 drive_state;
295
296 u32 in_flight;
297 u32 cur_max_queue_depth;
298 u32 queue_low_water_mark;
299 u32 dev_max_queue_depth;
300
301 u32 num_fitmsg_context;
302 u32 num_req_context;
303
304 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
305 u32 timeout_stamp;
306 struct skd_fitmsg_context *skmsg_free_list;
307 struct skd_fitmsg_context *skmsg_table;
308
309 struct skd_request_context *skreq_free_list;
310 struct skd_request_context *skreq_table;
311
312 struct skd_special_context *skspcl_free_list;
313 struct skd_special_context *skspcl_table;
314
315 struct skd_special_context internal_skspcl;
316 u32 read_cap_blocksize;
317 u32 read_cap_last_lba;
318 int read_cap_is_valid;
319 int inquiry_is_valid;
320 u8 inq_serial_num[13]; /*12 chars plus null term */
321 u8 id_str[80]; /* holds a composite name (pci + sernum) */
322
323 u8 skcomp_cycle;
324 u32 skcomp_ix;
325 struct fit_completion_entry_v1 *skcomp_table;
326 struct fit_comp_error_info *skerr_table;
327 dma_addr_t cq_dma_address;
328
329 wait_queue_head_t waitq;
330
331 struct timer_list timer;
332 u32 timer_countdown;
333 u32 timer_substate;
334
335 int n_special;
336 int sgs_per_request;
337 u32 last_mtd;
338
339 u32 proto_ver;
340
341 int dbg_level;
342 u32 connect_time_stamp;
343 int connect_retries;
344#define SKD_MAX_CONNECT_RETRIES 16
345 u32 drive_jiffies;
346
347 u32 timo_slot;
348
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400349
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600350 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600351};
352
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600353#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
354#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
355#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
356
357static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
358{
359 u32 val;
360
361 if (likely(skdev->dbg_level < 2))
362 return readl(skdev->mem_map[1] + offset);
363 else {
364 barrier();
365 val = readl(skdev->mem_map[1] + offset);
366 barrier();
rchinthekindi2e44b422013-10-24 12:51:23 +0100367 pr_debug("%s:%s:%d offset %x = %x\n",
368 skdev->name, __func__, __LINE__, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600369 return val;
370 }
371
372}
373
374static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
375 u32 offset)
376{
377 if (likely(skdev->dbg_level < 2)) {
378 writel(val, skdev->mem_map[1] + offset);
379 barrier();
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600380 } else {
381 barrier();
382 writel(val, skdev->mem_map[1] + offset);
383 barrier();
rchinthekindi2e44b422013-10-24 12:51:23 +0100384 pr_debug("%s:%s:%d offset %x = %x\n",
385 skdev->name, __func__, __LINE__, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600386 }
387}
388
389static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
390 u32 offset)
391{
392 if (likely(skdev->dbg_level < 2)) {
393 writeq(val, skdev->mem_map[1] + offset);
394 barrier();
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600395 } else {
396 barrier();
397 writeq(val, skdev->mem_map[1] + offset);
398 barrier();
rchinthekindi2e44b422013-10-24 12:51:23 +0100399 pr_debug("%s:%s:%d offset %x = %016llx\n",
400 skdev->name, __func__, __LINE__, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600401 }
402}
403
404
405#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
406static int skd_isr_type = SKD_IRQ_DEFAULT;
407
408module_param(skd_isr_type, int, 0444);
409MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
410 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
411
412#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
413static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
414
415module_param(skd_max_req_per_msg, int, 0444);
416MODULE_PARM_DESC(skd_max_req_per_msg,
417 "Maximum SCSI requests packed in a single message."
418 " (1-14, default==1)");
419
420#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
421#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
422static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
423
424module_param(skd_max_queue_depth, int, 0444);
425MODULE_PARM_DESC(skd_max_queue_depth,
426 "Maximum SCSI requests issued to s1120."
427 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
428
429static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
430module_param(skd_sgs_per_request, int, 0444);
431MODULE_PARM_DESC(skd_sgs_per_request,
432 "Maximum SG elements per block request."
433 " (1-4096, default==256)");
434
435static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
436module_param(skd_max_pass_thru, int, 0444);
437MODULE_PARM_DESC(skd_max_pass_thru,
438 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
439
440module_param(skd_dbg_level, int, 0444);
441MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
442
443module_param(skd_isr_comp_limit, int, 0444);
444MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
445
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600446/* Major device number dynamically assigned. */
447static u32 skd_major;
448
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600449static void skd_destruct(struct skd_device *skdev);
450static const struct block_device_operations skd_blockdev_ops;
451static void skd_send_fitmsg(struct skd_device *skdev,
452 struct skd_fitmsg_context *skmsg);
453static void skd_send_special_fitmsg(struct skd_device *skdev,
454 struct skd_special_context *skspcl);
455static void skd_request_fn(struct request_queue *rq);
456static void skd_end_request(struct skd_device *skdev,
457 struct skd_request_context *skreq, int error);
458static int skd_preop_sg_list(struct skd_device *skdev,
459 struct skd_request_context *skreq);
460static void skd_postop_sg_list(struct skd_device *skdev,
461 struct skd_request_context *skreq);
462
463static void skd_restart_device(struct skd_device *skdev);
464static int skd_quiesce_dev(struct skd_device *skdev);
465static int skd_unquiesce_dev(struct skd_device *skdev);
466static void skd_release_special(struct skd_device *skdev,
467 struct skd_special_context *skspcl);
468static void skd_disable_interrupts(struct skd_device *skdev);
469static void skd_isr_fwstate(struct skd_device *skdev);
470static void skd_recover_requests(struct skd_device *skdev, int requeue);
471static void skd_soft_reset(struct skd_device *skdev);
472
473static const char *skd_name(struct skd_device *skdev);
474const char *skd_drive_state_to_str(int state);
475const char *skd_skdev_state_to_str(enum skd_drvr_state state);
476static void skd_log_skdev(struct skd_device *skdev, const char *event);
477static void skd_log_skmsg(struct skd_device *skdev,
478 struct skd_fitmsg_context *skmsg, const char *event);
479static void skd_log_skreq(struct skd_device *skdev,
480 struct skd_request_context *skreq, const char *event);
481
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600482/*
483 *****************************************************************************
484 * READ/WRITE REQUESTS
485 *****************************************************************************
486 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600487static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600488{
489 struct request_queue *q = skdev->queue;
490 struct request *req;
491
492 for (;; ) {
493 req = blk_peek_request(q);
494 if (req == NULL)
495 break;
496 blk_start_request(req);
497 __blk_end_request_all(req, -EIO);
498 }
499}
500
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600501static void
502skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
503 int data_dir, unsigned lba,
504 unsigned count)
505{
506 if (data_dir == READ)
507 scsi_req->cdb[0] = 0x28;
508 else
509 scsi_req->cdb[0] = 0x2a;
510
511 scsi_req->cdb[1] = 0;
512 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
513 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
514 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
515 scsi_req->cdb[5] = (lba & 0xff);
516 scsi_req->cdb[6] = 0;
517 scsi_req->cdb[7] = (count & 0xff00) >> 8;
518 scsi_req->cdb[8] = count & 0xff;
519 scsi_req->cdb[9] = 0;
520}
521
522static void
523skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400524 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600525{
526 skreq->flush_cmd = 1;
527
528 scsi_req->cdb[0] = 0x35;
529 scsi_req->cdb[1] = 0;
530 scsi_req->cdb[2] = 0;
531 scsi_req->cdb[3] = 0;
532 scsi_req->cdb[4] = 0;
533 scsi_req->cdb[5] = 0;
534 scsi_req->cdb[6] = 0;
535 scsi_req->cdb[7] = 0;
536 scsi_req->cdb[8] = 0;
537 scsi_req->cdb[9] = 0;
538}
539
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600540static void skd_request_fn_not_online(struct request_queue *q);
541
542static void skd_request_fn(struct request_queue *q)
543{
544 struct skd_device *skdev = q->queuedata;
545 struct skd_fitmsg_context *skmsg = NULL;
546 struct fit_msg_hdr *fmh = NULL;
547 struct skd_request_context *skreq;
548 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600549 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600550 unsigned long io_flags;
551 int error;
552 u32 lba;
553 u32 count;
554 int data_dir;
555 u32 be_lba;
556 u32 be_count;
557 u64 be_dmaa;
558 u64 cmdctxt;
559 u32 timo_slot;
560 void *cmd_ptr;
561 int flush, fua;
562
563 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
564 skd_request_fn_not_online(q);
565 return;
566 }
567
Jens Axboe6a5ec65b2013-11-01 10:38:45 -0600568 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600569 if (skdev->skmsg_free_list == NULL ||
570 skdev->skreq_free_list == NULL ||
571 skdev->in_flight >= skdev->queue_low_water_mark)
572 /* There is still some kind of shortage */
573 return;
574
Jens Axboe6a5ec65b2013-11-01 10:38:45 -0600575 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600576 }
577
578 /*
579 * Stop conditions:
580 * - There are no more native requests
581 * - There are already the maximum number of requests in progress
582 * - There are no more skd_request_context entries
583 * - There are no more FIT msg buffers
584 */
585 for (;; ) {
586
587 flush = fua = 0;
588
Jens Axboefcd37eb2013-11-01 10:14:56 -0600589 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600590
Jens Axboefcd37eb2013-11-01 10:14:56 -0600591 /* Are there any native requests to start? */
592 if (req == NULL)
593 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600594
Jens Axboefcd37eb2013-11-01 10:14:56 -0600595 lba = (u32)blk_rq_pos(req);
596 count = blk_rq_sectors(req);
597 data_dir = rq_data_dir(req);
598 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600599
Mike Christie3a5e02c2016-06-05 14:32:23 -0500600 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600601 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600602
Jens Axboefcd37eb2013-11-01 10:14:56 -0600603 if (io_flags & REQ_FUA)
604 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600605
Jens Axboefcd37eb2013-11-01 10:14:56 -0600606 pr_debug("%s:%s:%d new req=%p lba=%u(0x%x) "
607 "count=%u(0x%x) dir=%d\n",
608 skdev->name, __func__, __LINE__,
609 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600610
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400611 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600612
613 /* Are too many requets already in progress? */
614 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100615 pr_debug("%s:%s:%d qdepth %d, limit %d\n",
616 skdev->name, __func__, __LINE__,
617 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600618 break;
619 }
620
621 /* Is a skd_request_context available? */
622 skreq = skdev->skreq_free_list;
623 if (skreq == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100624 pr_debug("%s:%s:%d Out of req=%p\n",
625 skdev->name, __func__, __LINE__, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600626 break;
627 }
628 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
629 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
630
631 /* Now we check to see if we can get a fit msg */
632 if (skmsg == NULL) {
633 if (skdev->skmsg_free_list == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100634 pr_debug("%s:%s:%d Out of msg\n",
635 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600636 break;
637 }
638 }
639
640 skreq->flush_cmd = 0;
641 skreq->n_sg = 0;
642 skreq->sg_byte_count = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600643
644 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400645 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600646 *
647 * At this point we are comitted to either start or reject
648 * the native request. Note that skd_request_context is
649 * available but is still at the head of the free list.
650 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600651 blk_start_request(req);
652 skreq->req = req;
653 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600654
655 /* Either a FIT msg is in progress or we have to start one. */
656 if (skmsg == NULL) {
657 /* Are there any FIT msg buffers available? */
658 skmsg = skdev->skmsg_free_list;
659 if (skmsg == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100660 pr_debug("%s:%s:%d Out of msg skdev=%p\n",
661 skdev->name, __func__, __LINE__,
662 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600663 break;
664 }
665 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
666 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
667
668 skdev->skmsg_free_list = skmsg->next;
669
670 skmsg->state = SKD_MSG_STATE_BUSY;
671 skmsg->id += SKD_ID_INCR;
672
673 /* Initialize the FIT msg header */
674 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
675 memset(fmh, 0, sizeof(*fmh));
676 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
677 skmsg->length = sizeof(*fmh);
678 }
679
680 skreq->fitmsg_id = skmsg->id;
681
682 /*
683 * Note that a FIT msg may have just been started
684 * but contains no SoFIT requests yet.
685 */
686
687 /*
688 * Transcode the request, checking as we go. The outcome of
689 * the transcoding is represented by the error variable.
690 */
691 cmd_ptr = &skmsg->msg_buf[skmsg->length];
692 memset(cmd_ptr, 0, 32);
693
694 be_lba = cpu_to_be32(lba);
695 be_count = cpu_to_be32(count);
696 be_dmaa = cpu_to_be64((u64)skreq->sksg_dma_address);
697 cmdctxt = skreq->id + SKD_ID_INCR;
698
699 scsi_req = cmd_ptr;
700 scsi_req->hdr.tag = cmdctxt;
701 scsi_req->hdr.sg_list_dma_address = be_dmaa;
702
703 if (data_dir == READ)
704 skreq->sg_data_dir = SKD_DATA_DIR_CARD_TO_HOST;
705 else
706 skreq->sg_data_dir = SKD_DATA_DIR_HOST_TO_CARD;
707
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600708 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600709 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
710 SKD_ASSERT(skreq->flush_cmd == 1);
711
712 } else {
713 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
714 }
715
716 if (fua)
717 scsi_req->cdb[1] |= SKD_FUA_NV;
718
Jens Axboefcd37eb2013-11-01 10:14:56 -0600719 if (!req->bio)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600720 goto skip_sg;
721
722 error = skd_preop_sg_list(skdev, skreq);
723
724 if (error != 0) {
725 /*
726 * Complete the native request with error.
727 * Note that the request context is still at the
728 * head of the free list, and that the SoFIT request
729 * was encoded into the FIT msg buffer but the FIT
730 * msg length has not been updated. In short, the
731 * only resource that has been allocated but might
732 * not be used is that the FIT msg could be empty.
733 */
rchinthekindi2e44b422013-10-24 12:51:23 +0100734 pr_debug("%s:%s:%d error Out\n",
735 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600736 skd_end_request(skdev, skreq, error);
737 continue;
738 }
739
740skip_sg:
741 scsi_req->hdr.sg_list_len_bytes =
742 cpu_to_be32(skreq->sg_byte_count);
743
744 /* Complete resource allocations. */
745 skdev->skreq_free_list = skreq->next;
746 skreq->state = SKD_REQ_STATE_BUSY;
747 skreq->id += SKD_ID_INCR;
748
749 skmsg->length += sizeof(struct skd_scsi_request);
750 fmh->num_protocol_cmds_coalesced++;
751
752 /*
753 * Update the active request counts.
754 * Capture the timeout timestamp.
755 */
756 skreq->timeout_stamp = skdev->timeout_stamp;
757 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
758 skdev->timeout_slot[timo_slot]++;
759 skdev->in_flight++;
rchinthekindi2e44b422013-10-24 12:51:23 +0100760 pr_debug("%s:%s:%d req=0x%x busy=%d\n",
761 skdev->name, __func__, __LINE__,
762 skreq->id, skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600763
764 /*
765 * If the FIT msg buffer is full send it.
766 */
767 if (skmsg->length >= SKD_N_FITMSG_BYTES ||
768 fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
769 skd_send_fitmsg(skdev, skmsg);
770 skmsg = NULL;
771 fmh = NULL;
772 }
773 }
774
775 /*
776 * Is a FIT msg in progress? If it is empty put the buffer back
777 * on the free list. If it is non-empty send what we got.
778 * This minimizes latency when there are fewer requests than
779 * what fits in a FIT msg.
780 */
781 if (skmsg != NULL) {
782 /* Bigger than just a FIT msg header? */
783 if (skmsg->length > sizeof(struct fit_msg_hdr)) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100784 pr_debug("%s:%s:%d sending msg=%p, len %d\n",
785 skdev->name, __func__, __LINE__,
786 skmsg, skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600787 skd_send_fitmsg(skdev, skmsg);
788 } else {
789 /*
790 * The FIT msg is empty. It means we got started
791 * on the msg, but the requests were rejected.
792 */
793 skmsg->state = SKD_MSG_STATE_IDLE;
794 skmsg->id += SKD_ID_INCR;
795 skmsg->next = skdev->skmsg_free_list;
796 skdev->skmsg_free_list = skmsg;
797 }
798 skmsg = NULL;
799 fmh = NULL;
800 }
801
802 /*
803 * If req is non-NULL it means there is something to do but
804 * we are out of a resource.
805 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600806 if (req)
Jens Axboe6a5ec65b2013-11-01 10:38:45 -0600807 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600808}
809
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400810static void skd_end_request(struct skd_device *skdev,
811 struct skd_request_context *skreq, int error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600812{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600813 if (unlikely(error)) {
814 struct request *req = skreq->req;
815 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
816 u32 lba = (u32)blk_rq_pos(req);
817 u32 count = blk_rq_sectors(req);
818
819 pr_err("(%s): Error cmd=%s sect=%u count=%u id=0x%x\n",
820 skd_name(skdev), cmd, lba, count, skreq->id);
821 } else
rchinthekindi2e44b422013-10-24 12:51:23 +0100822 pr_debug("%s:%s:%d id=0x%x error=%d\n",
823 skdev->name, __func__, __LINE__, skreq->id, error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600824
825 __blk_end_request_all(skreq->req, error);
826}
827
Jens Axboefcd37eb2013-11-01 10:14:56 -0600828static int skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400829 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600830{
831 struct request *req = skreq->req;
832 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
833 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
834 struct scatterlist *sg = &skreq->sg[0];
835 int n_sg;
836 int i;
837
838 skreq->sg_byte_count = 0;
839
840 /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
841 skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */
842
843 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
844 if (n_sg <= 0)
845 return -EINVAL;
846
847 /*
848 * Map scatterlist to PCI bus addresses.
849 * Note PCI might change the number of entries.
850 */
851 n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
852 if (n_sg <= 0)
853 return -EINVAL;
854
855 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
856
857 skreq->n_sg = n_sg;
858
859 for (i = 0; i < n_sg; i++) {
860 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
861 u32 cnt = sg_dma_len(&sg[i]);
862 uint64_t dma_addr = sg_dma_address(&sg[i]);
863
864 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
865 sgd->byte_count = cnt;
866 skreq->sg_byte_count += cnt;
867 sgd->host_side_addr = dma_addr;
868 sgd->dev_side_addr = 0;
869 }
870
871 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
872 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
873
874 if (unlikely(skdev->dbg_level > 1)) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100875 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
876 skdev->name, __func__, __LINE__,
877 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600878 for (i = 0; i < n_sg; i++) {
879 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
rchinthekindi2e44b422013-10-24 12:51:23 +0100880 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
881 "addr=0x%llx next=0x%llx\n",
882 skdev->name, __func__, __LINE__,
883 i, sgd->byte_count, sgd->control,
884 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600885 }
886 }
887
888 return 0;
889}
890
Jens Axboefcd37eb2013-11-01 10:14:56 -0600891static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400892 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600893{
894 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
895 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
896
897 /*
898 * restore the next ptr for next IO request so we
899 * don't have to set it every time.
900 */
901 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
902 skreq->sksg_dma_address +
903 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
904 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
905}
906
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600907static void skd_request_fn_not_online(struct request_queue *q)
908{
909 struct skd_device *skdev = q->queuedata;
910 int error;
911
912 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
913
914 skd_log_skdev(skdev, "req_not_online");
915 switch (skdev->state) {
916 case SKD_DRVR_STATE_PAUSING:
917 case SKD_DRVR_STATE_PAUSED:
918 case SKD_DRVR_STATE_STARTING:
919 case SKD_DRVR_STATE_RESTARTING:
920 case SKD_DRVR_STATE_WAIT_BOOT:
921 /* In case of starting, we haven't started the queue,
922 * so we can't get here... but requests are
923 * possibly hanging out waiting for us because we
924 * reported the dev/skd0 already. They'll wait
925 * forever if connect doesn't complete.
926 * What to do??? delay dev/skd0 ??
927 */
928 case SKD_DRVR_STATE_BUSY:
929 case SKD_DRVR_STATE_BUSY_IMMINENT:
930 case SKD_DRVR_STATE_BUSY_ERASE:
931 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
932 return;
933
934 case SKD_DRVR_STATE_BUSY_SANITIZE:
935 case SKD_DRVR_STATE_STOPPING:
936 case SKD_DRVR_STATE_SYNCING:
937 case SKD_DRVR_STATE_FAULT:
938 case SKD_DRVR_STATE_DISAPPEARED:
939 default:
940 error = -EIO;
941 break;
942 }
943
944 /* If we get here, terminate all pending block requeusts
945 * with EIO and any scsi pass thru with appropriate sense
946 */
947
948 skd_fail_all_pending(skdev);
949}
950
951/*
952 *****************************************************************************
953 * TIMER
954 *****************************************************************************
955 */
956
957static void skd_timer_tick_not_online(struct skd_device *skdev);
958
959static void skd_timer_tick(ulong arg)
960{
961 struct skd_device *skdev = (struct skd_device *)arg;
962
963 u32 timo_slot;
964 u32 overdue_timestamp;
965 unsigned long reqflags;
966 u32 state;
967
968 if (skdev->state == SKD_DRVR_STATE_FAULT)
969 /* The driver has declared fault, and we want it to
970 * stay that way until driver is reloaded.
971 */
972 return;
973
974 spin_lock_irqsave(&skdev->lock, reqflags);
975
976 state = SKD_READL(skdev, FIT_STATUS);
977 state &= FIT_SR_DRIVE_STATE_MASK;
978 if (state != skdev->drive_state)
979 skd_isr_fwstate(skdev);
980
981 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
982 skd_timer_tick_not_online(skdev);
983 goto timer_func_out;
984 }
985 skdev->timeout_stamp++;
986 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
987
988 /*
989 * All requests that happened during the previous use of
990 * this slot should be done by now. The previous use was
991 * over 7 seconds ago.
992 */
993 if (skdev->timeout_slot[timo_slot] == 0)
994 goto timer_func_out;
995
996 /* Something is overdue */
997 overdue_timestamp = skdev->timeout_stamp - SKD_N_TIMEOUT_SLOT;
998
rchinthekindi2e44b422013-10-24 12:51:23 +0100999 pr_debug("%s:%s:%d found %d timeouts, draining busy=%d\n",
1000 skdev->name, __func__, __LINE__,
1001 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001002 pr_err("(%s): Overdue IOs (%d), busy %d\n",
1003 skd_name(skdev), skdev->timeout_slot[timo_slot],
1004 skdev->in_flight);
1005
1006 skdev->timer_countdown = SKD_DRAINING_TIMO;
1007 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
1008 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06001009 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001010
1011timer_func_out:
1012 mod_timer(&skdev->timer, (jiffies + HZ));
1013
1014 spin_unlock_irqrestore(&skdev->lock, reqflags);
1015}
1016
1017static void skd_timer_tick_not_online(struct skd_device *skdev)
1018{
1019 switch (skdev->state) {
1020 case SKD_DRVR_STATE_IDLE:
1021 case SKD_DRVR_STATE_LOAD:
1022 break;
1023 case SKD_DRVR_STATE_BUSY_SANITIZE:
rchinthekindi2e44b422013-10-24 12:51:23 +01001024 pr_debug("%s:%s:%d drive busy sanitize[%x], driver[%x]\n",
1025 skdev->name, __func__, __LINE__,
1026 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001027 /* If we've been in sanitize for 3 seconds, we figure we're not
1028 * going to get anymore completions, so recover requests now
1029 */
1030 if (skdev->timer_countdown > 0) {
1031 skdev->timer_countdown--;
1032 return;
1033 }
1034 skd_recover_requests(skdev, 0);
1035 break;
1036
1037 case SKD_DRVR_STATE_BUSY:
1038 case SKD_DRVR_STATE_BUSY_IMMINENT:
1039 case SKD_DRVR_STATE_BUSY_ERASE:
rchinthekindi2e44b422013-10-24 12:51:23 +01001040 pr_debug("%s:%s:%d busy[%x], countdown=%d\n",
1041 skdev->name, __func__, __LINE__,
1042 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001043 if (skdev->timer_countdown > 0) {
1044 skdev->timer_countdown--;
1045 return;
1046 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001047 pr_debug("%s:%s:%d busy[%x], timedout=%d, restarting device.",
1048 skdev->name, __func__, __LINE__,
1049 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001050 skd_restart_device(skdev);
1051 break;
1052
1053 case SKD_DRVR_STATE_WAIT_BOOT:
1054 case SKD_DRVR_STATE_STARTING:
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
1063 pr_err("(%s): DriveFault Connect Timeout (%x)\n",
1064 skd_name(skdev), skdev->drive_state);
1065
1066 /*start the queue so we can respond with error to requests */
1067 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06001068 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001069 skdev->gendisk_on = -1;
1070 wake_up_interruptible(&skdev->waitq);
1071 break;
1072
1073 case SKD_DRVR_STATE_ONLINE:
1074 /* shouldn't get here. */
1075 break;
1076
1077 case SKD_DRVR_STATE_PAUSING:
1078 case SKD_DRVR_STATE_PAUSED:
1079 break;
1080
1081 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
rchinthekindi2e44b422013-10-24 12:51:23 +01001082 pr_debug("%s:%s:%d "
1083 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
1084 skdev->name, __func__, __LINE__,
1085 skdev->timo_slot,
1086 skdev->timer_countdown,
1087 skdev->in_flight,
1088 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001089 /* if the slot has cleared we can let the I/O continue */
1090 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001091 pr_debug("%s:%s:%d Slot drained, starting queue.\n",
1092 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001093 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06001094 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001095 return;
1096 }
1097 if (skdev->timer_countdown > 0) {
1098 skdev->timer_countdown--;
1099 return;
1100 }
1101 skd_restart_device(skdev);
1102 break;
1103
1104 case SKD_DRVR_STATE_RESTARTING:
1105 if (skdev->timer_countdown > 0) {
1106 skdev->timer_countdown--;
1107 return;
1108 }
1109 /* For now, we fault the drive. Could attempt resets to
1110 * revcover at some point. */
1111 skdev->state = SKD_DRVR_STATE_FAULT;
1112 pr_err("(%s): DriveFault Reconnect Timeout (%x)\n",
1113 skd_name(skdev), skdev->drive_state);
1114
1115 /*
1116 * Recovering does two things:
1117 * 1. completes IO with error
1118 * 2. reclaims dma resources
1119 * When is it safe to recover requests?
1120 * - if the drive state is faulted
1121 * - if the state is still soft reset after out timeout
1122 * - if the drive registers are dead (state = FF)
1123 * If it is "unsafe", we still need to recover, so we will
1124 * disable pci bus mastering and disable our interrupts.
1125 */
1126
1127 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1128 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1129 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1130 /* It never came out of soft reset. Try to
1131 * recover the requests and then let them
1132 * fail. This is to mitigate hung processes. */
1133 skd_recover_requests(skdev, 0);
1134 else {
1135 pr_err("(%s): Disable BusMaster (%x)\n",
1136 skd_name(skdev), skdev->drive_state);
1137 pci_disable_device(skdev->pdev);
1138 skd_disable_interrupts(skdev);
1139 skd_recover_requests(skdev, 0);
1140 }
1141
1142 /*start the queue so we can respond with error to requests */
1143 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06001144 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001145 skdev->gendisk_on = -1;
1146 wake_up_interruptible(&skdev->waitq);
1147 break;
1148
1149 case SKD_DRVR_STATE_RESUMING:
1150 case SKD_DRVR_STATE_STOPPING:
1151 case SKD_DRVR_STATE_SYNCING:
1152 case SKD_DRVR_STATE_FAULT:
1153 case SKD_DRVR_STATE_DISAPPEARED:
1154 default:
1155 break;
1156 }
1157}
1158
1159static int skd_start_timer(struct skd_device *skdev)
1160{
1161 int rc;
1162
1163 init_timer(&skdev->timer);
1164 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1165
1166 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1167 if (rc)
1168 pr_err("%s: failed to start timer %d\n",
1169 __func__, rc);
1170 return rc;
1171}
1172
1173static void skd_kill_timer(struct skd_device *skdev)
1174{
1175 del_timer_sync(&skdev->timer);
1176}
1177
1178/*
1179 *****************************************************************************
1180 * IOCTL
1181 *****************************************************************************
1182 */
1183static int skd_ioctl_sg_io(struct skd_device *skdev,
1184 fmode_t mode, void __user *argp);
1185static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1186 struct skd_sg_io *sksgio);
1187static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1188 struct skd_sg_io *sksgio);
1189static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1190 struct skd_sg_io *sksgio);
1191static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1192 struct skd_sg_io *sksgio, int dxfer_dir);
1193static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1194 struct skd_sg_io *sksgio);
1195static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1196static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1197 struct skd_sg_io *sksgio);
1198static int skd_sg_io_put_status(struct skd_device *skdev,
1199 struct skd_sg_io *sksgio);
1200
1201static void skd_complete_special(struct skd_device *skdev,
1202 volatile struct fit_completion_entry_v1
1203 *skcomp,
1204 volatile struct fit_comp_error_info *skerr,
1205 struct skd_special_context *skspcl);
1206
1207static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1208 uint cmd_in, ulong arg)
1209{
1210 int rc = 0;
1211 struct gendisk *disk = bdev->bd_disk;
1212 struct skd_device *skdev = disk->private_data;
1213 void __user *p = (void *)arg;
1214
rchinthekindi2e44b422013-10-24 12:51:23 +01001215 pr_debug("%s:%s:%d %s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1216 skdev->name, __func__, __LINE__,
1217 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001218
1219 if (!capable(CAP_SYS_ADMIN))
1220 return -EPERM;
1221
1222 switch (cmd_in) {
1223 case SG_SET_TIMEOUT:
1224 case SG_GET_TIMEOUT:
1225 case SG_GET_VERSION_NUM:
1226 rc = scsi_cmd_ioctl(disk->queue, disk, mode, cmd_in, p);
1227 break;
1228 case SG_IO:
1229 rc = skd_ioctl_sg_io(skdev, mode, p);
1230 break;
1231
1232 default:
1233 rc = -ENOTTY;
1234 break;
1235 }
1236
rchinthekindi2e44b422013-10-24 12:51:23 +01001237 pr_debug("%s:%s:%d %s: completion rc %d\n",
1238 skdev->name, __func__, __LINE__, disk->disk_name, rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001239 return rc;
1240}
1241
1242static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1243 void __user *argp)
1244{
1245 int rc;
1246 struct skd_sg_io sksgio;
1247
1248 memset(&sksgio, 0, sizeof(sksgio));
1249 sksgio.mode = mode;
1250 sksgio.argp = argp;
1251 sksgio.iov = &sksgio.no_iov_iov;
1252
1253 switch (skdev->state) {
1254 case SKD_DRVR_STATE_ONLINE:
1255 case SKD_DRVR_STATE_BUSY_IMMINENT:
1256 break;
1257
1258 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01001259 pr_debug("%s:%s:%d drive not online\n",
1260 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001261 rc = -ENXIO;
1262 goto out;
1263 }
1264
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001265 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1266 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001267 goto out;
1268
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001269 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1270 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001271 goto out;
1272
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001273 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1274 if (rc)
1275 goto out;
1276
1277 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1278 if (rc)
1279 goto out;
1280
1281 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1282 if (rc)
1283 goto out;
1284
1285 rc = skd_sg_io_await(skdev, &sksgio);
1286 if (rc)
1287 goto out;
1288
1289 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1290 if (rc)
1291 goto out;
1292
1293 rc = skd_sg_io_put_status(skdev, &sksgio);
1294 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001295 goto out;
1296
1297 rc = 0;
1298
1299out:
1300 skd_sg_io_release_skspcl(skdev, &sksgio);
1301
1302 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1303 kfree(sksgio.iov);
1304 return rc;
1305}
1306
1307static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1308 struct skd_sg_io *sksgio)
1309{
1310 struct sg_io_hdr *sgp = &sksgio->sg;
1311 int i, acc;
1312
1313 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001314 pr_debug("%s:%s:%d access sg failed %p\n",
1315 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001316 return -EFAULT;
1317 }
1318
1319 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001320 pr_debug("%s:%s:%d copy_from_user sg failed %p\n",
1321 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001322 return -EFAULT;
1323 }
1324
1325 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001326 pr_debug("%s:%s:%d interface_id invalid 0x%x\n",
1327 skdev->name, __func__, __LINE__, sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001328 return -EINVAL;
1329 }
1330
1331 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001332 pr_debug("%s:%s:%d cmd_len invalid %d\n",
1333 skdev->name, __func__, __LINE__, sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001334 return -EINVAL;
1335 }
1336
1337 if (sgp->iovec_count > 256) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001338 pr_debug("%s:%s:%d iovec_count invalid %d\n",
1339 skdev->name, __func__, __LINE__, sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001340 return -EINVAL;
1341 }
1342
1343 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001344 pr_debug("%s:%s:%d dxfer_len invalid %d\n",
1345 skdev->name, __func__, __LINE__, sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001346 return -EINVAL;
1347 }
1348
1349 switch (sgp->dxfer_direction) {
1350 case SG_DXFER_NONE:
1351 acc = -1;
1352 break;
1353
1354 case SG_DXFER_TO_DEV:
1355 acc = VERIFY_READ;
1356 break;
1357
1358 case SG_DXFER_FROM_DEV:
1359 case SG_DXFER_TO_FROM_DEV:
1360 acc = VERIFY_WRITE;
1361 break;
1362
1363 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01001364 pr_debug("%s:%s:%d dxfer_dir invalid %d\n",
1365 skdev->name, __func__, __LINE__, sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001366 return -EINVAL;
1367 }
1368
1369 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001370 pr_debug("%s:%s:%d copy_from_user cmdp failed %p\n",
1371 skdev->name, __func__, __LINE__, sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001372 return -EFAULT;
1373 }
1374
1375 if (sgp->mx_sb_len != 0) {
1376 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001377 pr_debug("%s:%s:%d access sbp failed %p\n",
1378 skdev->name, __func__, __LINE__, sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001379 return -EFAULT;
1380 }
1381 }
1382
1383 if (sgp->iovec_count == 0) {
1384 sksgio->iov[0].iov_base = sgp->dxferp;
1385 sksgio->iov[0].iov_len = sgp->dxfer_len;
1386 sksgio->iovcnt = 1;
1387 sksgio->dxfer_len = sgp->dxfer_len;
1388 } else {
1389 struct sg_iovec *iov;
1390 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1391 size_t iov_data_len;
1392
1393 iov = kmalloc(nbytes, GFP_KERNEL);
1394 if (iov == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001395 pr_debug("%s:%s:%d alloc iovec failed %d\n",
1396 skdev->name, __func__, __LINE__,
1397 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001398 return -ENOMEM;
1399 }
1400 sksgio->iov = iov;
1401 sksgio->iovcnt = sgp->iovec_count;
1402
1403 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001404 pr_debug("%s:%s:%d copy_from_user iovec failed %p\n",
1405 skdev->name, __func__, __LINE__, sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001406 return -EFAULT;
1407 }
1408
1409 /*
1410 * Sum up the vecs, making sure they don't overflow
1411 */
1412 iov_data_len = 0;
1413 for (i = 0; i < sgp->iovec_count; i++) {
1414 if (iov_data_len + iov[i].iov_len < iov_data_len)
1415 return -EINVAL;
1416 iov_data_len += iov[i].iov_len;
1417 }
1418
1419 /* SG_IO howto says that the shorter of the two wins */
1420 if (sgp->dxfer_len < iov_data_len) {
1421 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1422 sgp->iovec_count,
1423 sgp->dxfer_len);
1424 sksgio->dxfer_len = sgp->dxfer_len;
1425 } else
1426 sksgio->dxfer_len = iov_data_len;
1427 }
1428
1429 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1430 struct sg_iovec *iov = sksgio->iov;
1431 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1432 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001433 pr_debug("%s:%s:%d access data failed %p/%d\n",
1434 skdev->name, __func__, __LINE__,
1435 iov->iov_base, (int)iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001436 return -EFAULT;
1437 }
1438 }
1439 }
1440
1441 return 0;
1442}
1443
1444static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1445 struct skd_sg_io *sksgio)
1446{
1447 struct skd_special_context *skspcl = NULL;
1448 int rc;
1449
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001450 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001451 ulong flags;
1452
1453 spin_lock_irqsave(&skdev->lock, flags);
1454 skspcl = skdev->skspcl_free_list;
1455 if (skspcl != NULL) {
1456 skdev->skspcl_free_list =
1457 (struct skd_special_context *)skspcl->req.next;
1458 skspcl->req.id += SKD_ID_INCR;
1459 skspcl->req.state = SKD_REQ_STATE_SETUP;
1460 skspcl->orphaned = 0;
1461 skspcl->req.n_sg = 0;
1462 }
1463 spin_unlock_irqrestore(&skdev->lock, flags);
1464
1465 if (skspcl != NULL) {
1466 rc = 0;
1467 break;
1468 }
1469
rchinthekindi2e44b422013-10-24 12:51:23 +01001470 pr_debug("%s:%s:%d blocking\n",
1471 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001472
1473 rc = wait_event_interruptible_timeout(
1474 skdev->waitq,
1475 (skdev->skspcl_free_list != NULL),
1476 msecs_to_jiffies(sksgio->sg.timeout));
1477
rchinthekindi2e44b422013-10-24 12:51:23 +01001478 pr_debug("%s:%s:%d unblocking, rc=%d\n",
1479 skdev->name, __func__, __LINE__, rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001480
1481 if (rc <= 0) {
1482 if (rc == 0)
1483 rc = -ETIMEDOUT;
1484 else
1485 rc = -EINTR;
1486 break;
1487 }
1488 /*
1489 * If we get here rc > 0 meaning the timeout to
1490 * wait_event_interruptible_timeout() had time left, hence the
1491 * sought event -- non-empty free list -- happened.
1492 * Retry the allocation.
1493 */
1494 }
1495 sksgio->skspcl = skspcl;
1496
1497 return rc;
1498}
1499
1500static int skd_skreq_prep_buffering(struct skd_device *skdev,
1501 struct skd_request_context *skreq,
1502 u32 dxfer_len)
1503{
1504 u32 resid = dxfer_len;
1505
1506 /*
1507 * The DMA engine must have aligned addresses and byte counts.
1508 */
1509 resid += (-resid) & 3;
1510 skreq->sg_byte_count = resid;
1511
1512 skreq->n_sg = 0;
1513
1514 while (resid > 0) {
1515 u32 nbytes = PAGE_SIZE;
1516 u32 ix = skreq->n_sg;
1517 struct scatterlist *sg = &skreq->sg[ix];
1518 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1519 struct page *page;
1520
1521 if (nbytes > resid)
1522 nbytes = resid;
1523
1524 page = alloc_page(GFP_KERNEL);
1525 if (page == NULL)
1526 return -ENOMEM;
1527
1528 sg_set_page(sg, page, nbytes, 0);
1529
1530 /* TODO: This should be going through a pci_???()
1531 * routine to do proper mapping. */
1532 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1533 sksg->byte_count = nbytes;
1534
1535 sksg->host_side_addr = sg_phys(sg);
1536
1537 sksg->dev_side_addr = 0;
1538 sksg->next_desc_ptr = skreq->sksg_dma_address +
1539 (ix + 1) * sizeof(*sksg);
1540
1541 skreq->n_sg++;
1542 resid -= nbytes;
1543 }
1544
1545 if (skreq->n_sg > 0) {
1546 u32 ix = skreq->n_sg - 1;
1547 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1548
1549 sksg->control = FIT_SGD_CONTROL_LAST;
1550 sksg->next_desc_ptr = 0;
1551 }
1552
1553 if (unlikely(skdev->dbg_level > 1)) {
1554 u32 i;
1555
rchinthekindi2e44b422013-10-24 12:51:23 +01001556 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
1557 skdev->name, __func__, __LINE__,
1558 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001559 for (i = 0; i < skreq->n_sg; i++) {
1560 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1561
rchinthekindi2e44b422013-10-24 12:51:23 +01001562 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
1563 "addr=0x%llx next=0x%llx\n",
1564 skdev->name, __func__, __LINE__,
1565 i, sgd->byte_count, sgd->control,
1566 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001567 }
1568 }
1569
1570 return 0;
1571}
1572
1573static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1574 struct skd_sg_io *sksgio)
1575{
1576 struct skd_special_context *skspcl = sksgio->skspcl;
1577 struct skd_request_context *skreq = &skspcl->req;
1578 u32 dxfer_len = sksgio->dxfer_len;
1579 int rc;
1580
1581 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1582 /*
1583 * Eventually, errors or not, skd_release_special() is called
1584 * to recover allocations including partial allocations.
1585 */
1586 return rc;
1587}
1588
1589static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1590 struct skd_sg_io *sksgio, int dxfer_dir)
1591{
1592 struct skd_special_context *skspcl = sksgio->skspcl;
1593 u32 iov_ix = 0;
1594 struct sg_iovec curiov;
1595 u32 sksg_ix = 0;
1596 u8 *bufp = NULL;
1597 u32 buf_len = 0;
1598 u32 resid = sksgio->dxfer_len;
1599 int rc;
1600
1601 curiov.iov_len = 0;
1602 curiov.iov_base = NULL;
1603
1604 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1605 if (dxfer_dir != SG_DXFER_TO_DEV ||
1606 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1607 return 0;
1608 }
1609
1610 while (resid > 0) {
1611 u32 nbytes = PAGE_SIZE;
1612
1613 if (curiov.iov_len == 0) {
1614 curiov = sksgio->iov[iov_ix++];
1615 continue;
1616 }
1617
1618 if (buf_len == 0) {
1619 struct page *page;
1620 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1621 bufp = page_address(page);
1622 buf_len = PAGE_SIZE;
1623 }
1624
1625 nbytes = min_t(u32, nbytes, resid);
1626 nbytes = min_t(u32, nbytes, curiov.iov_len);
1627 nbytes = min_t(u32, nbytes, buf_len);
1628
1629 if (dxfer_dir == SG_DXFER_TO_DEV)
1630 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1631 else
1632 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1633
1634 if (rc)
1635 return -EFAULT;
1636
1637 resid -= nbytes;
1638 curiov.iov_len -= nbytes;
1639 curiov.iov_base += nbytes;
1640 buf_len -= nbytes;
1641 }
1642
1643 return 0;
1644}
1645
1646static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1647 struct skd_sg_io *sksgio)
1648{
1649 struct skd_special_context *skspcl = sksgio->skspcl;
1650 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
1651 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
1652
1653 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1654
1655 /* Initialize the FIT msg header */
1656 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1657 fmh->num_protocol_cmds_coalesced = 1;
1658
1659 /* Initialize the SCSI request */
1660 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1661 scsi_req->hdr.sg_list_dma_address =
1662 cpu_to_be64(skspcl->req.sksg_dma_address);
1663 scsi_req->hdr.tag = skspcl->req.id;
1664 scsi_req->hdr.sg_list_len_bytes =
1665 cpu_to_be32(skspcl->req.sg_byte_count);
1666 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1667
1668 skspcl->req.state = SKD_REQ_STATE_BUSY;
1669 skd_send_special_fitmsg(skdev, skspcl);
1670
1671 return 0;
1672}
1673
1674static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1675{
1676 unsigned long flags;
1677 int rc;
1678
1679 rc = wait_event_interruptible_timeout(skdev->waitq,
1680 (sksgio->skspcl->req.state !=
1681 SKD_REQ_STATE_BUSY),
1682 msecs_to_jiffies(sksgio->sg.
1683 timeout));
1684
1685 spin_lock_irqsave(&skdev->lock, flags);
1686
1687 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001688 pr_debug("%s:%s:%d skspcl %p aborted\n",
1689 skdev->name, __func__, __LINE__, sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001690
1691 /* Build check cond, sense and let command finish. */
1692 /* For a timeout, we must fabricate completion and sense
1693 * data to complete the command */
1694 sksgio->skspcl->req.completion.status =
1695 SAM_STAT_CHECK_CONDITION;
1696
1697 memset(&sksgio->skspcl->req.err_info, 0,
1698 sizeof(sksgio->skspcl->req.err_info));
1699 sksgio->skspcl->req.err_info.type = 0x70;
1700 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1701 sksgio->skspcl->req.err_info.code = 0x44;
1702 sksgio->skspcl->req.err_info.qual = 0;
1703 rc = 0;
1704 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1705 /* No longer on the adapter. We finish. */
1706 rc = 0;
1707 else {
1708 /* Something's gone wrong. Still busy. Timeout or
1709 * user interrupted (control-C). Mark as an orphan
1710 * so it will be disposed when completed. */
1711 sksgio->skspcl->orphaned = 1;
1712 sksgio->skspcl = NULL;
1713 if (rc == 0) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001714 pr_debug("%s:%s:%d timed out %p (%u ms)\n",
1715 skdev->name, __func__, __LINE__,
1716 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001717 rc = -ETIMEDOUT;
1718 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01001719 pr_debug("%s:%s:%d cntlc %p\n",
1720 skdev->name, __func__, __LINE__, sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001721 rc = -EINTR;
1722 }
1723 }
1724
1725 spin_unlock_irqrestore(&skdev->lock, flags);
1726
1727 return rc;
1728}
1729
1730static int skd_sg_io_put_status(struct skd_device *skdev,
1731 struct skd_sg_io *sksgio)
1732{
1733 struct sg_io_hdr *sgp = &sksgio->sg;
1734 struct skd_special_context *skspcl = sksgio->skspcl;
1735 int resid = 0;
1736
1737 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1738
1739 sgp->status = skspcl->req.completion.status;
1740 resid = sksgio->dxfer_len - nb;
1741
1742 sgp->masked_status = sgp->status & STATUS_MASK;
1743 sgp->msg_status = 0;
1744 sgp->host_status = 0;
1745 sgp->driver_status = 0;
1746 sgp->resid = resid;
1747 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1748 sgp->info |= SG_INFO_CHECK;
1749
rchinthekindi2e44b422013-10-24 12:51:23 +01001750 pr_debug("%s:%s:%d status %x masked %x resid 0x%x\n",
1751 skdev->name, __func__, __LINE__,
1752 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001753
1754 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1755 if (sgp->mx_sb_len > 0) {
1756 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1757 u32 nbytes = sizeof(*ei);
1758
1759 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1760
1761 sgp->sb_len_wr = nbytes;
1762
1763 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001764 pr_debug("%s:%s:%d copy_to_user sense failed %p\n",
1765 skdev->name, __func__, __LINE__,
1766 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001767 return -EFAULT;
1768 }
1769 }
1770 }
1771
1772 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001773 pr_debug("%s:%s:%d copy_to_user sg failed %p\n",
1774 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001775 return -EFAULT;
1776 }
1777
1778 return 0;
1779}
1780
1781static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1782 struct skd_sg_io *sksgio)
1783{
1784 struct skd_special_context *skspcl = sksgio->skspcl;
1785
1786 if (skspcl != NULL) {
1787 ulong flags;
1788
1789 sksgio->skspcl = NULL;
1790
1791 spin_lock_irqsave(&skdev->lock, flags);
1792 skd_release_special(skdev, skspcl);
1793 spin_unlock_irqrestore(&skdev->lock, flags);
1794 }
1795
1796 return 0;
1797}
1798
1799/*
1800 *****************************************************************************
1801 * INTERNAL REQUESTS -- generated by driver itself
1802 *****************************************************************************
1803 */
1804
1805static int skd_format_internal_skspcl(struct skd_device *skdev)
1806{
1807 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1808 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1809 struct fit_msg_hdr *fmh;
1810 uint64_t dma_address;
1811 struct skd_scsi_request *scsi;
1812
1813 fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0];
1814 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1815 fmh->num_protocol_cmds_coalesced = 1;
1816
1817 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1818 memset(scsi, 0, sizeof(*scsi));
1819 dma_address = skspcl->req.sksg_dma_address;
1820 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1821 sgd->control = FIT_SGD_CONTROL_LAST;
1822 sgd->byte_count = 0;
1823 sgd->host_side_addr = skspcl->db_dma_address;
1824 sgd->dev_side_addr = 0;
1825 sgd->next_desc_ptr = 0LL;
1826
1827 return 1;
1828}
1829
1830#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1831
1832static void skd_send_internal_skspcl(struct skd_device *skdev,
1833 struct skd_special_context *skspcl,
1834 u8 opcode)
1835{
1836 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1837 struct skd_scsi_request *scsi;
1838 unsigned char *buf = skspcl->data_buf;
1839 int i;
1840
1841 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1842 /*
1843 * A refresh is already in progress.
1844 * Just wait for it to finish.
1845 */
1846 return;
1847
1848 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1849 skspcl->req.state = SKD_REQ_STATE_BUSY;
1850 skspcl->req.id += SKD_ID_INCR;
1851
1852 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1853 scsi->hdr.tag = skspcl->req.id;
1854
1855 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1856
1857 switch (opcode) {
1858 case TEST_UNIT_READY:
1859 scsi->cdb[0] = TEST_UNIT_READY;
1860 sgd->byte_count = 0;
1861 scsi->hdr.sg_list_len_bytes = 0;
1862 break;
1863
1864 case READ_CAPACITY:
1865 scsi->cdb[0] = READ_CAPACITY;
1866 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1867 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1868 break;
1869
1870 case INQUIRY:
1871 scsi->cdb[0] = INQUIRY;
1872 scsi->cdb[1] = 0x01; /* evpd */
1873 scsi->cdb[2] = 0x80; /* serial number page */
1874 scsi->cdb[4] = 0x10;
1875 sgd->byte_count = 16;
1876 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1877 break;
1878
1879 case SYNCHRONIZE_CACHE:
1880 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1881 sgd->byte_count = 0;
1882 scsi->hdr.sg_list_len_bytes = 0;
1883 break;
1884
1885 case WRITE_BUFFER:
1886 scsi->cdb[0] = WRITE_BUFFER;
1887 scsi->cdb[1] = 0x02;
1888 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1889 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1890 sgd->byte_count = WR_BUF_SIZE;
1891 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1892 /* fill incrementing byte pattern */
1893 for (i = 0; i < sgd->byte_count; i++)
1894 buf[i] = i & 0xFF;
1895 break;
1896
1897 case READ_BUFFER:
1898 scsi->cdb[0] = READ_BUFFER;
1899 scsi->cdb[1] = 0x02;
1900 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1901 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1902 sgd->byte_count = WR_BUF_SIZE;
1903 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1904 memset(skspcl->data_buf, 0, sgd->byte_count);
1905 break;
1906
1907 default:
1908 SKD_ASSERT("Don't know what to send");
1909 return;
1910
1911 }
1912 skd_send_special_fitmsg(skdev, skspcl);
1913}
1914
1915static void skd_refresh_device_data(struct skd_device *skdev)
1916{
1917 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1918
1919 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1920}
1921
1922static int skd_chk_read_buf(struct skd_device *skdev,
1923 struct skd_special_context *skspcl)
1924{
1925 unsigned char *buf = skspcl->data_buf;
1926 int i;
1927
1928 /* check for incrementing byte pattern */
1929 for (i = 0; i < WR_BUF_SIZE; i++)
1930 if (buf[i] != (i & 0xFF))
1931 return 1;
1932
1933 return 0;
1934}
1935
1936static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1937 u8 code, u8 qual, u8 fruc)
1938{
1939 /* If the check condition is of special interest, log a message */
1940 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1941 && (code == 0x04) && (qual == 0x06)) {
1942 pr_err("(%s): *** LOST_WRITE_DATA ERROR *** key/asc/"
1943 "ascq/fruc %02x/%02x/%02x/%02x\n",
1944 skd_name(skdev), key, code, qual, fruc);
1945 }
1946}
1947
1948static void skd_complete_internal(struct skd_device *skdev,
1949 volatile struct fit_completion_entry_v1
1950 *skcomp,
1951 volatile struct fit_comp_error_info *skerr,
1952 struct skd_special_context *skspcl)
1953{
1954 u8 *buf = skspcl->data_buf;
1955 u8 status;
1956 int i;
1957 struct skd_scsi_request *scsi =
1958 (struct skd_scsi_request *)&skspcl->msg_buf[64];
1959
1960 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1961
rchinthekindi2e44b422013-10-24 12:51:23 +01001962 pr_debug("%s:%s:%d complete internal %x\n",
1963 skdev->name, __func__, __LINE__, scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001964
1965 skspcl->req.completion = *skcomp;
1966 skspcl->req.state = SKD_REQ_STATE_IDLE;
1967 skspcl->req.id += SKD_ID_INCR;
1968
1969 status = skspcl->req.completion.status;
1970
1971 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1972 skerr->qual, skerr->fruc);
1973
1974 switch (scsi->cdb[0]) {
1975 case TEST_UNIT_READY:
1976 if (status == SAM_STAT_GOOD)
1977 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1978 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1979 (skerr->key == MEDIUM_ERROR))
1980 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1981 else {
1982 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001983 pr_debug("%s:%s:%d TUR failed, don't send anymore state 0x%x\n",
1984 skdev->name, __func__, __LINE__,
1985 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001986 return;
1987 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001988 pr_debug("%s:%s:%d **** TUR failed, retry skerr\n",
1989 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001990 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1991 }
1992 break;
1993
1994 case WRITE_BUFFER:
1995 if (status == SAM_STAT_GOOD)
1996 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1997 else {
1998 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001999 pr_debug("%s:%s:%d write buffer failed, don't send anymore state 0x%x\n",
2000 skdev->name, __func__, __LINE__,
2001 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002002 return;
2003 }
rchinthekindi2e44b422013-10-24 12:51:23 +01002004 pr_debug("%s:%s:%d **** write buffer failed, retry skerr\n",
2005 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002006 skd_send_internal_skspcl(skdev, skspcl, 0x00);
2007 }
2008 break;
2009
2010 case READ_BUFFER:
2011 if (status == SAM_STAT_GOOD) {
2012 if (skd_chk_read_buf(skdev, skspcl) == 0)
2013 skd_send_internal_skspcl(skdev, skspcl,
2014 READ_CAPACITY);
2015 else {
2016 pr_err(
2017 "(%s):*** W/R Buffer mismatch %d ***\n",
2018 skd_name(skdev), skdev->connect_retries);
2019 if (skdev->connect_retries <
2020 SKD_MAX_CONNECT_RETRIES) {
2021 skdev->connect_retries++;
2022 skd_soft_reset(skdev);
2023 } else {
2024 pr_err(
2025 "(%s): W/R Buffer Connect Error\n",
2026 skd_name(skdev));
2027 return;
2028 }
2029 }
2030
2031 } else {
2032 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002033 pr_debug("%s:%s:%d "
2034 "read buffer failed, don't send anymore state 0x%x\n",
2035 skdev->name, __func__, __LINE__,
2036 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002037 return;
2038 }
rchinthekindi2e44b422013-10-24 12:51:23 +01002039 pr_debug("%s:%s:%d "
2040 "**** read buffer failed, retry skerr\n",
2041 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002042 skd_send_internal_skspcl(skdev, skspcl, 0x00);
2043 }
2044 break;
2045
2046 case READ_CAPACITY:
2047 skdev->read_cap_is_valid = 0;
2048 if (status == SAM_STAT_GOOD) {
2049 skdev->read_cap_last_lba =
2050 (buf[0] << 24) | (buf[1] << 16) |
2051 (buf[2] << 8) | buf[3];
2052 skdev->read_cap_blocksize =
2053 (buf[4] << 24) | (buf[5] << 16) |
2054 (buf[6] << 8) | buf[7];
2055
rchinthekindi2e44b422013-10-24 12:51:23 +01002056 pr_debug("%s:%s:%d last lba %d, bs %d\n",
2057 skdev->name, __func__, __LINE__,
2058 skdev->read_cap_last_lba,
2059 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002060
2061 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
2062
2063 skdev->read_cap_is_valid = 1;
2064
2065 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2066 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
2067 (skerr->key == MEDIUM_ERROR)) {
2068 skdev->read_cap_last_lba = ~0;
2069 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
rchinthekindi2e44b422013-10-24 12:51:23 +01002070 pr_debug("%s:%s:%d "
2071 "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n",
2072 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002073 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2074 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01002075 pr_debug("%s:%s:%d **** READCAP failed, retry TUR\n",
2076 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002077 skd_send_internal_skspcl(skdev, skspcl,
2078 TEST_UNIT_READY);
2079 }
2080 break;
2081
2082 case INQUIRY:
2083 skdev->inquiry_is_valid = 0;
2084 if (status == SAM_STAT_GOOD) {
2085 skdev->inquiry_is_valid = 1;
2086
2087 for (i = 0; i < 12; i++)
2088 skdev->inq_serial_num[i] = buf[i + 4];
2089 skdev->inq_serial_num[12] = 0;
2090 }
2091
2092 if (skd_unquiesce_dev(skdev) < 0)
rchinthekindi2e44b422013-10-24 12:51:23 +01002093 pr_debug("%s:%s:%d **** failed, to ONLINE device\n",
2094 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002095 /* connection is complete */
2096 skdev->connect_retries = 0;
2097 break;
2098
2099 case SYNCHRONIZE_CACHE:
2100 if (status == SAM_STAT_GOOD)
2101 skdev->sync_done = 1;
2102 else
2103 skdev->sync_done = -1;
2104 wake_up_interruptible(&skdev->waitq);
2105 break;
2106
2107 default:
2108 SKD_ASSERT("we didn't send this");
2109 }
2110}
2111
2112/*
2113 *****************************************************************************
2114 * FIT MESSAGES
2115 *****************************************************************************
2116 */
2117
2118static void skd_send_fitmsg(struct skd_device *skdev,
2119 struct skd_fitmsg_context *skmsg)
2120{
2121 u64 qcmd;
2122 struct fit_msg_hdr *fmh;
2123
rchinthekindi2e44b422013-10-24 12:51:23 +01002124 pr_debug("%s:%s:%d dma address 0x%llx, busy=%d\n",
2125 skdev->name, __func__, __LINE__,
2126 skmsg->mb_dma_address, skdev->in_flight);
2127 pr_debug("%s:%s:%d msg_buf 0x%p, offset %x\n",
2128 skdev->name, __func__, __LINE__,
2129 skmsg->msg_buf, skmsg->offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002130
2131 qcmd = skmsg->mb_dma_address;
2132 qcmd |= FIT_QCMD_QID_NORMAL;
2133
2134 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
2135 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2136
2137 if (unlikely(skdev->dbg_level > 1)) {
2138 u8 *bp = (u8 *)skmsg->msg_buf;
2139 int i;
2140 for (i = 0; i < skmsg->length; i += 8) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002141 pr_debug("%s:%s:%d msg[%2d] %02x %02x %02x %02x "
2142 "%02x %02x %02x %02x\n",
2143 skdev->name, __func__, __LINE__,
2144 i, bp[i + 0], bp[i + 1], bp[i + 2],
2145 bp[i + 3], bp[i + 4], bp[i + 5],
2146 bp[i + 6], bp[i + 7]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002147 if (i == 0)
2148 i = 64 - 8;
2149 }
2150 }
2151
2152 if (skmsg->length > 256)
2153 qcmd |= FIT_QCMD_MSGSIZE_512;
2154 else if (skmsg->length > 128)
2155 qcmd |= FIT_QCMD_MSGSIZE_256;
2156 else if (skmsg->length > 64)
2157 qcmd |= FIT_QCMD_MSGSIZE_128;
2158 else
2159 /*
2160 * This makes no sense because the FIT msg header is
2161 * 64 bytes. If the msg is only 64 bytes long it has
2162 * no payload.
2163 */
2164 qcmd |= FIT_QCMD_MSGSIZE_64;
2165
2166 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2167
2168}
2169
2170static void skd_send_special_fitmsg(struct skd_device *skdev,
2171 struct skd_special_context *skspcl)
2172{
2173 u64 qcmd;
2174
2175 if (unlikely(skdev->dbg_level > 1)) {
2176 u8 *bp = (u8 *)skspcl->msg_buf;
2177 int i;
2178
2179 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002180 pr_debug("%s:%s:%d spcl[%2d] %02x %02x %02x %02x "
2181 "%02x %02x %02x %02x\n",
2182 skdev->name, __func__, __LINE__, i,
2183 bp[i + 0], bp[i + 1], bp[i + 2], bp[i + 3],
2184 bp[i + 4], bp[i + 5], bp[i + 6], bp[i + 7]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002185 if (i == 0)
2186 i = 64 - 8;
2187 }
2188
rchinthekindi2e44b422013-10-24 12:51:23 +01002189 pr_debug("%s:%s:%d skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2190 skdev->name, __func__, __LINE__,
2191 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2192 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002193 for (i = 0; i < skspcl->req.n_sg; i++) {
2194 struct fit_sg_descriptor *sgd =
2195 &skspcl->req.sksg_list[i];
2196
rchinthekindi2e44b422013-10-24 12:51:23 +01002197 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
2198 "addr=0x%llx next=0x%llx\n",
2199 skdev->name, __func__, __LINE__,
2200 i, sgd->byte_count, sgd->control,
2201 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002202 }
2203 }
2204
2205 /*
2206 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2207 * and one 64-byte SSDI command.
2208 */
2209 qcmd = skspcl->mb_dma_address;
2210 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2211
2212 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2213}
2214
2215/*
2216 *****************************************************************************
2217 * COMPLETION QUEUE
2218 *****************************************************************************
2219 */
2220
2221static void skd_complete_other(struct skd_device *skdev,
2222 volatile struct fit_completion_entry_v1 *skcomp,
2223 volatile struct fit_comp_error_info *skerr);
2224
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002225struct sns_info {
2226 u8 type;
2227 u8 stat;
2228 u8 key;
2229 u8 asc;
2230 u8 ascq;
2231 u8 mask;
2232 enum skd_check_status_action action;
2233};
2234
2235static struct sns_info skd_chkstat_table[] = {
2236 /* Good */
2237 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2238 SKD_CHECK_STATUS_REPORT_GOOD },
2239
2240 /* Smart alerts */
2241 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2242 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2243 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2244 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2245 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2246 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2247
2248 /* Retry (with limits) */
2249 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2250 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2251 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2252 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2253 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2254 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2255 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2256 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2257
2258 /* Busy (or about to be) */
2259 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2260 SKD_CHECK_STATUS_BUSY_IMMINENT },
2261};
2262
2263/*
2264 * Look up status and sense data to decide how to handle the error
2265 * from the device.
2266 * mask says which fields must match e.g., mask=0x18 means check
2267 * type and stat, ignore key, asc, ascq.
2268 */
2269
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002270static enum skd_check_status_action
2271skd_check_status(struct skd_device *skdev,
2272 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002273{
2274 int i, n;
2275
2276 pr_err("(%s): key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2277 skd_name(skdev), skerr->key, skerr->code, skerr->qual,
2278 skerr->fruc);
2279
rchinthekindi2e44b422013-10-24 12:51:23 +01002280 pr_debug("%s:%s:%d stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2281 skdev->name, __func__, __LINE__, skerr->type, cmp_status,
2282 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002283
2284 /* Does the info match an entry in the good category? */
2285 n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
2286 for (i = 0; i < n; i++) {
2287 struct sns_info *sns = &skd_chkstat_table[i];
2288
2289 if (sns->mask & 0x10)
2290 if (skerr->type != sns->type)
2291 continue;
2292
2293 if (sns->mask & 0x08)
2294 if (cmp_status != sns->stat)
2295 continue;
2296
2297 if (sns->mask & 0x04)
2298 if (skerr->key != sns->key)
2299 continue;
2300
2301 if (sns->mask & 0x02)
2302 if (skerr->code != sns->asc)
2303 continue;
2304
2305 if (sns->mask & 0x01)
2306 if (skerr->qual != sns->ascq)
2307 continue;
2308
2309 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
2310 pr_err("(%s): SMART Alert: sense key/asc/ascq "
2311 "%02x/%02x/%02x\n",
2312 skd_name(skdev), skerr->key,
2313 skerr->code, skerr->qual);
2314 }
2315 return sns->action;
2316 }
2317
2318 /* No other match, so nonzero status means error,
2319 * zero status means good
2320 */
2321 if (cmp_status) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002322 pr_debug("%s:%s:%d status check: error\n",
2323 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002324 return SKD_CHECK_STATUS_REPORT_ERROR;
2325 }
2326
rchinthekindi2e44b422013-10-24 12:51:23 +01002327 pr_debug("%s:%s:%d status check good default\n",
2328 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002329 return SKD_CHECK_STATUS_REPORT_GOOD;
2330}
2331
2332static void skd_resolve_req_exception(struct skd_device *skdev,
2333 struct skd_request_context *skreq)
2334{
2335 u8 cmp_status = skreq->completion.status;
2336
2337 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2338 case SKD_CHECK_STATUS_REPORT_GOOD:
2339 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
2340 skd_end_request(skdev, skreq, 0);
2341 break;
2342
2343 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2344 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002345 blk_requeue_request(skdev->queue, skreq->req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002346 pr_info("(%s) drive BUSY imminent\n", skd_name(skdev));
2347 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2348 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2349 skd_quiesce_dev(skdev);
2350 break;
2351
2352 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002353 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2354 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002355 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002356 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002357 }
2358 /* fall through to report error */
2359
2360 case SKD_CHECK_STATUS_REPORT_ERROR:
2361 default:
2362 skd_end_request(skdev, skreq, -EIO);
2363 break;
2364 }
2365}
2366
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002367/* assume spinlock is already held */
2368static void skd_release_skreq(struct skd_device *skdev,
2369 struct skd_request_context *skreq)
2370{
2371 u32 msg_slot;
2372 struct skd_fitmsg_context *skmsg;
2373
2374 u32 timo_slot;
2375
2376 /*
2377 * Reclaim the FIT msg buffer if this is
2378 * the first of the requests it carried to
2379 * be completed. The FIT msg buffer used to
2380 * send this request cannot be reused until
2381 * we are sure the s1120 card has copied
2382 * it to its memory. The FIT msg might have
2383 * contained several requests. As soon as
2384 * any of them are completed we know that
2385 * the entire FIT msg was transferred.
2386 * Only the first completed request will
2387 * match the FIT msg buffer id. The FIT
2388 * msg buffer id is immediately updated.
2389 * When subsequent requests complete the FIT
2390 * msg buffer id won't match, so we know
2391 * quite cheaply that it is already done.
2392 */
2393 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2394 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2395
2396 skmsg = &skdev->skmsg_table[msg_slot];
2397 if (skmsg->id == skreq->fitmsg_id) {
2398 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2399 SKD_ASSERT(skmsg->outstanding > 0);
2400 skmsg->outstanding--;
2401 if (skmsg->outstanding == 0) {
2402 skmsg->state = SKD_MSG_STATE_IDLE;
2403 skmsg->id += SKD_ID_INCR;
2404 skmsg->next = skdev->skmsg_free_list;
2405 skdev->skmsg_free_list = skmsg;
2406 }
2407 }
2408
2409 /*
2410 * Decrease the number of active requests.
2411 * Also decrements the count in the timeout slot.
2412 */
2413 SKD_ASSERT(skdev->in_flight > 0);
2414 skdev->in_flight -= 1;
2415
2416 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2417 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2418 skdev->timeout_slot[timo_slot] -= 1;
2419
2420 /*
2421 * Reset backpointer
2422 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002423 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002424
2425 /*
2426 * Reclaim the skd_request_context
2427 */
2428 skreq->state = SKD_REQ_STATE_IDLE;
2429 skreq->id += SKD_ID_INCR;
2430 skreq->next = skdev->skreq_free_list;
2431 skdev->skreq_free_list = skreq;
2432}
2433
2434#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2435
2436static void skd_do_inq_page_00(struct skd_device *skdev,
2437 volatile struct fit_completion_entry_v1 *skcomp,
2438 volatile struct fit_comp_error_info *skerr,
2439 uint8_t *cdb, uint8_t *buf)
2440{
2441 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2442
2443 /* Caller requested "supported pages". The driver needs to insert
2444 * its page.
2445 */
rchinthekindi2e44b422013-10-24 12:51:23 +01002446 pr_debug("%s:%s:%d skd_do_driver_inquiry: modify supported pages.\n",
2447 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002448
2449 /* If the device rejected the request because the CDB was
2450 * improperly formed, then just leave.
2451 */
2452 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2453 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2454 return;
2455
2456 /* Get the amount of space the caller allocated */
2457 max_bytes = (cdb[3] << 8) | cdb[4];
2458
2459 /* Get the number of pages actually returned by the device */
2460 drive_pages = (buf[2] << 8) | buf[3];
2461 drive_bytes = drive_pages + 4;
2462 new_size = drive_pages + 1;
2463
2464 /* Supported pages must be in numerical order, so find where
2465 * the driver page needs to be inserted into the list of
2466 * pages returned by the device.
2467 */
2468 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2469 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2470 return; /* Device using this page code. abort */
2471 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2472 break;
2473 }
2474
2475 if (insert_pt < max_bytes) {
2476 uint16_t u;
2477
2478 /* Shift everything up one byte to make room. */
2479 for (u = new_size + 3; u > insert_pt; u--)
2480 buf[u] = buf[u - 1];
2481 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2482
2483 /* SCSI byte order increment of num_returned_bytes by 1 */
2484 skcomp->num_returned_bytes =
2485 be32_to_cpu(skcomp->num_returned_bytes) + 1;
2486 skcomp->num_returned_bytes =
2487 be32_to_cpu(skcomp->num_returned_bytes);
2488 }
2489
2490 /* update page length field to reflect the driver's page too */
2491 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2492 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2493}
2494
2495static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2496{
2497 int pcie_reg;
2498 u16 pci_bus_speed;
2499 u8 pci_lanes;
2500
2501 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2502 if (pcie_reg) {
2503 u16 linksta;
2504 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2505
2506 pci_bus_speed = linksta & 0xF;
2507 pci_lanes = (linksta & 0x3F0) >> 4;
2508 } else {
2509 *speed = STEC_LINK_UNKNOWN;
2510 *width = 0xFF;
2511 return;
2512 }
2513
2514 switch (pci_bus_speed) {
2515 case 1:
2516 *speed = STEC_LINK_2_5GTS;
2517 break;
2518 case 2:
2519 *speed = STEC_LINK_5GTS;
2520 break;
2521 case 3:
2522 *speed = STEC_LINK_8GTS;
2523 break;
2524 default:
2525 *speed = STEC_LINK_UNKNOWN;
2526 break;
2527 }
2528
2529 if (pci_lanes <= 0x20)
2530 *width = pci_lanes;
2531 else
2532 *width = 0xFF;
2533}
2534
2535static void skd_do_inq_page_da(struct skd_device *skdev,
2536 volatile struct fit_completion_entry_v1 *skcomp,
2537 volatile struct fit_comp_error_info *skerr,
2538 uint8_t *cdb, uint8_t *buf)
2539{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002540 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002541 unsigned max_bytes;
2542 struct driver_inquiry_data inq;
2543 u16 val;
2544
rchinthekindi2e44b422013-10-24 12:51:23 +01002545 pr_debug("%s:%s:%d skd_do_driver_inquiry: return driver page\n",
2546 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002547
2548 memset(&inq, 0, sizeof(inq));
2549
2550 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2551
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002552 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2553 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2554 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2555 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002556
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002557 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2558 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002559
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002560 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2561 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002562
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002563 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2564 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002565
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002566 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2567 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002568
2569 /* Driver version, fixed lenth, padded with spaces on the right */
2570 inq.driver_version_length = sizeof(inq.driver_version);
2571 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2572 memcpy(inq.driver_version, DRV_VER_COMPL,
2573 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2574
2575 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2576
2577 /* Clear the error set by the device */
2578 skcomp->status = SAM_STAT_GOOD;
2579 memset((void *)skerr, 0, sizeof(*skerr));
2580
2581 /* copy response into output buffer */
2582 max_bytes = (cdb[3] << 8) | cdb[4];
2583 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2584
2585 skcomp->num_returned_bytes =
2586 be32_to_cpu(min_t(uint16_t, max_bytes, sizeof(inq)));
2587}
2588
2589static void skd_do_driver_inq(struct skd_device *skdev,
2590 volatile struct fit_completion_entry_v1 *skcomp,
2591 volatile struct fit_comp_error_info *skerr,
2592 uint8_t *cdb, uint8_t *buf)
2593{
2594 if (!buf)
2595 return;
2596 else if (cdb[0] != INQUIRY)
2597 return; /* Not an INQUIRY */
2598 else if ((cdb[1] & 1) == 0)
2599 return; /* EVPD not set */
2600 else if (cdb[2] == 0)
2601 /* Need to add driver's page to supported pages list */
2602 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2603 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2604 /* Caller requested driver's page */
2605 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2606}
2607
2608static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2609{
2610 if (!sg)
2611 return NULL;
2612 if (!sg_page(sg))
2613 return NULL;
2614 return sg_virt(sg);
2615}
2616
2617static void skd_process_scsi_inq(struct skd_device *skdev,
2618 volatile struct fit_completion_entry_v1
2619 *skcomp,
2620 volatile struct fit_comp_error_info *skerr,
2621 struct skd_special_context *skspcl)
2622{
2623 uint8_t *buf;
2624 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
2625 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
2626
2627 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2628 skspcl->req.sg_data_dir);
2629 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2630
2631 if (buf)
2632 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2633}
2634
2635
2636static int skd_isr_completion_posted(struct skd_device *skdev,
2637 int limit, int *enqueued)
2638{
2639 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2640 volatile struct fit_comp_error_info *skerr;
2641 u16 req_id;
2642 u32 req_slot;
2643 struct skd_request_context *skreq;
2644 u16 cmp_cntxt = 0;
2645 u8 cmp_status = 0;
2646 u8 cmp_cycle = 0;
2647 u32 cmp_bytes = 0;
2648 int rc = 0;
2649 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002650
2651 for (;; ) {
2652 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2653
2654 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2655 cmp_cycle = skcmp->cycle;
2656 cmp_cntxt = skcmp->tag;
2657 cmp_status = skcmp->status;
2658 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2659
2660 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2661
rchinthekindi2e44b422013-10-24 12:51:23 +01002662 pr_debug("%s:%s:%d "
2663 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d "
2664 "busy=%d rbytes=0x%x proto=%d\n",
2665 skdev->name, __func__, __LINE__, skdev->skcomp_cycle,
2666 skdev->skcomp_ix, cmp_cycle, cmp_cntxt, cmp_status,
2667 skdev->in_flight, cmp_bytes, skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002668
2669 if (cmp_cycle != skdev->skcomp_cycle) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002670 pr_debug("%s:%s:%d end of completions\n",
2671 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002672 break;
2673 }
2674 /*
2675 * Update the completion queue head index and possibly
2676 * the completion cycle count. 8-bit wrap-around.
2677 */
2678 skdev->skcomp_ix++;
2679 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2680 skdev->skcomp_ix = 0;
2681 skdev->skcomp_cycle++;
2682 }
2683
2684 /*
2685 * The command context is a unique 32-bit ID. The low order
2686 * bits help locate the request. The request is usually a
2687 * r/w request (see skd_start() above) or a special request.
2688 */
2689 req_id = cmp_cntxt;
2690 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2691
2692 /* Is this other than a r/w request? */
2693 if (req_slot >= skdev->num_req_context) {
2694 /*
2695 * This is not a completion for a r/w request.
2696 */
2697 skd_complete_other(skdev, skcmp, skerr);
2698 continue;
2699 }
2700
2701 skreq = &skdev->skreq_table[req_slot];
2702
2703 /*
2704 * Make sure the request ID for the slot matches.
2705 */
2706 if (skreq->id != req_id) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002707 pr_debug("%s:%s:%d mismatch comp_id=0x%x req_id=0x%x\n",
2708 skdev->name, __func__, __LINE__,
2709 req_id, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002710 {
2711 u16 new_id = cmp_cntxt;
2712 pr_err("(%s): Completion mismatch "
2713 "comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2714 skd_name(skdev), req_id,
2715 skreq->id, new_id);
2716
2717 continue;
2718 }
2719 }
2720
2721 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2722
2723 if (skreq->state == SKD_REQ_STATE_ABORTED) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002724 pr_debug("%s:%s:%d reclaim req %p id=%04x\n",
2725 skdev->name, __func__, __LINE__,
2726 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002727 /* a previously timed out command can
2728 * now be cleaned up */
2729 skd_release_skreq(skdev, skreq);
2730 continue;
2731 }
2732
2733 skreq->completion = *skcmp;
2734 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2735 skreq->err_info = *skerr;
2736 skd_log_check_status(skdev, cmp_status, skerr->key,
2737 skerr->code, skerr->qual,
2738 skerr->fruc);
2739 }
2740 /* Release DMA resources for the request. */
2741 if (skreq->n_sg > 0)
2742 skd_postop_sg_list(skdev, skreq);
2743
Jens Axboefcd37eb2013-11-01 10:14:56 -06002744 if (!skreq->req) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002745 pr_debug("%s:%s:%d NULL backptr skdreq %p, "
2746 "req=0x%x req_id=0x%x\n",
2747 skdev->name, __func__, __LINE__,
2748 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002749 } else {
2750 /*
2751 * Capture the outcome and post it back to the
2752 * native request.
2753 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002754 if (likely(cmp_status == SAM_STAT_GOOD))
2755 skd_end_request(skdev, skreq, 0);
2756 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002757 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002758 }
2759
2760 /*
2761 * Release the skreq, its FIT msg (if one), timeout slot,
2762 * and queue depth.
2763 */
2764 skd_release_skreq(skdev, skreq);
2765
2766 /* skd_isr_comp_limit equal zero means no limit */
2767 if (limit) {
2768 if (++processed >= limit) {
2769 rc = 1;
2770 break;
2771 }
2772 }
2773 }
2774
2775 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2776 && (skdev->in_flight) == 0) {
2777 skdev->state = SKD_DRVR_STATE_PAUSED;
2778 wake_up_interruptible(&skdev->waitq);
2779 }
2780
2781 return rc;
2782}
2783
2784static void skd_complete_other(struct skd_device *skdev,
2785 volatile struct fit_completion_entry_v1 *skcomp,
2786 volatile struct fit_comp_error_info *skerr)
2787{
2788 u32 req_id = 0;
2789 u32 req_table;
2790 u32 req_slot;
2791 struct skd_special_context *skspcl;
2792
2793 req_id = skcomp->tag;
2794 req_table = req_id & SKD_ID_TABLE_MASK;
2795 req_slot = req_id & SKD_ID_SLOT_MASK;
2796
rchinthekindi2e44b422013-10-24 12:51:23 +01002797 pr_debug("%s:%s:%d table=0x%x id=0x%x slot=%d\n",
2798 skdev->name, __func__, __LINE__,
2799 req_table, req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002800
2801 /*
2802 * Based on the request id, determine how to dispatch this completion.
2803 * This swich/case is finding the good cases and forwarding the
2804 * completion entry. Errors are reported below the switch.
2805 */
2806 switch (req_table) {
2807 case SKD_ID_RW_REQUEST:
2808 /*
2809 * The caller, skd_completion_posted_isr() above,
2810 * handles r/w requests. The only way we get here
2811 * is if the req_slot is out of bounds.
2812 */
2813 break;
2814
2815 case SKD_ID_SPECIAL_REQUEST:
2816 /*
2817 * Make sure the req_slot is in bounds and that the id
2818 * matches.
2819 */
2820 if (req_slot < skdev->n_special) {
2821 skspcl = &skdev->skspcl_table[req_slot];
2822 if (skspcl->req.id == req_id &&
2823 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2824 skd_complete_special(skdev,
2825 skcomp, skerr, skspcl);
2826 return;
2827 }
2828 }
2829 break;
2830
2831 case SKD_ID_INTERNAL:
2832 if (req_slot == 0) {
2833 skspcl = &skdev->internal_skspcl;
2834 if (skspcl->req.id == req_id &&
2835 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2836 skd_complete_internal(skdev,
2837 skcomp, skerr, skspcl);
2838 return;
2839 }
2840 }
2841 break;
2842
2843 case SKD_ID_FIT_MSG:
2844 /*
2845 * These id's should never appear in a completion record.
2846 */
2847 break;
2848
2849 default:
2850 /*
2851 * These id's should never appear anywhere;
2852 */
2853 break;
2854 }
2855
2856 /*
2857 * If we get here it is a bad or stale id.
2858 */
2859}
2860
2861static void skd_complete_special(struct skd_device *skdev,
2862 volatile struct fit_completion_entry_v1
2863 *skcomp,
2864 volatile struct fit_comp_error_info *skerr,
2865 struct skd_special_context *skspcl)
2866{
rchinthekindi2e44b422013-10-24 12:51:23 +01002867 pr_debug("%s:%s:%d completing special request %p\n",
2868 skdev->name, __func__, __LINE__, skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002869 if (skspcl->orphaned) {
2870 /* Discard orphaned request */
2871 /* ?: Can this release directly or does it need
2872 * to use a worker? */
rchinthekindi2e44b422013-10-24 12:51:23 +01002873 pr_debug("%s:%s:%d release orphaned %p\n",
2874 skdev->name, __func__, __LINE__, skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002875 skd_release_special(skdev, skspcl);
2876 return;
2877 }
2878
2879 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2880
2881 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2882 skspcl->req.completion = *skcomp;
2883 skspcl->req.err_info = *skerr;
2884
2885 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2886 skerr->code, skerr->qual, skerr->fruc);
2887
2888 wake_up_interruptible(&skdev->waitq);
2889}
2890
2891/* assume spinlock is already held */
2892static void skd_release_special(struct skd_device *skdev,
2893 struct skd_special_context *skspcl)
2894{
2895 int i, was_depleted;
2896
2897 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002898 struct page *page = sg_page(&skspcl->req.sg[i]);
2899 __free_page(page);
2900 }
2901
2902 was_depleted = (skdev->skspcl_free_list == NULL);
2903
2904 skspcl->req.state = SKD_REQ_STATE_IDLE;
2905 skspcl->req.id += SKD_ID_INCR;
2906 skspcl->req.next =
2907 (struct skd_request_context *)skdev->skspcl_free_list;
2908 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2909
2910 if (was_depleted) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002911 pr_debug("%s:%s:%d skspcl was depleted\n",
2912 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002913 /* Free list was depleted. Their might be waiters. */
2914 wake_up_interruptible(&skdev->waitq);
2915 }
2916}
2917
2918static void skd_reset_skcomp(struct skd_device *skdev)
2919{
2920 u32 nbytes;
2921 struct fit_completion_entry_v1 *skcomp;
2922
2923 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
2924 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
2925
2926 memset(skdev->skcomp_table, 0, nbytes);
2927
2928 skdev->skcomp_ix = 0;
2929 skdev->skcomp_cycle = 1;
2930}
2931
2932/*
2933 *****************************************************************************
2934 * INTERRUPTS
2935 *****************************************************************************
2936 */
2937static void skd_completion_worker(struct work_struct *work)
2938{
2939 struct skd_device *skdev =
2940 container_of(work, struct skd_device, completion_worker);
2941 unsigned long flags;
2942 int flush_enqueued = 0;
2943
2944 spin_lock_irqsave(&skdev->lock, flags);
2945
2946 /*
2947 * pass in limit=0, which means no limit..
2948 * process everything in compq
2949 */
2950 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2951 skd_request_fn(skdev->queue);
2952
2953 spin_unlock_irqrestore(&skdev->lock, flags);
2954}
2955
2956static void skd_isr_msg_from_dev(struct skd_device *skdev);
2957
2958irqreturn_t
2959static skd_isr(int irq, void *ptr)
2960{
2961 struct skd_device *skdev;
2962 u32 intstat;
2963 u32 ack;
2964 int rc = 0;
2965 int deferred = 0;
2966 int flush_enqueued = 0;
2967
2968 skdev = (struct skd_device *)ptr;
2969 spin_lock(&skdev->lock);
2970
2971 for (;; ) {
2972 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2973
2974 ack = FIT_INT_DEF_MASK;
2975 ack &= intstat;
2976
rchinthekindi2e44b422013-10-24 12:51:23 +01002977 pr_debug("%s:%s:%d intstat=0x%x ack=0x%x\n",
2978 skdev->name, __func__, __LINE__, intstat, ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002979
2980 /* As long as there is an int pending on device, keep
2981 * running loop. When none, get out, but if we've never
2982 * done any processing, call completion handler?
2983 */
2984 if (ack == 0) {
2985 /* No interrupts on device, but run the completion
2986 * processor anyway?
2987 */
2988 if (rc == 0)
2989 if (likely (skdev->state
2990 == SKD_DRVR_STATE_ONLINE))
2991 deferred = 1;
2992 break;
2993 }
2994
2995 rc = IRQ_HANDLED;
2996
2997 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2998
2999 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
3000 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
3001 if (intstat & FIT_ISH_COMPLETION_POSTED) {
3002 /*
3003 * If we have already deferred completion
3004 * processing, don't bother running it again
3005 */
3006 if (deferred == 0)
3007 deferred =
3008 skd_isr_completion_posted(skdev,
3009 skd_isr_comp_limit, &flush_enqueued);
3010 }
3011
3012 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
3013 skd_isr_fwstate(skdev);
3014 if (skdev->state == SKD_DRVR_STATE_FAULT ||
3015 skdev->state ==
3016 SKD_DRVR_STATE_DISAPPEARED) {
3017 spin_unlock(&skdev->lock);
3018 return rc;
3019 }
3020 }
3021
3022 if (intstat & FIT_ISH_MSG_FROM_DEV)
3023 skd_isr_msg_from_dev(skdev);
3024 }
3025 }
3026
3027 if (unlikely(flush_enqueued))
3028 skd_request_fn(skdev->queue);
3029
3030 if (deferred)
3031 schedule_work(&skdev->completion_worker);
3032 else if (!flush_enqueued)
3033 skd_request_fn(skdev->queue);
3034
3035 spin_unlock(&skdev->lock);
3036
3037 return rc;
3038}
3039
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003040static void skd_drive_fault(struct skd_device *skdev)
3041{
3042 skdev->state = SKD_DRVR_STATE_FAULT;
3043 pr_err("(%s): Drive FAULT\n", skd_name(skdev));
3044}
3045
3046static void skd_drive_disappeared(struct skd_device *skdev)
3047{
3048 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
3049 pr_err("(%s): Drive DISAPPEARED\n", skd_name(skdev));
3050}
3051
3052static void skd_isr_fwstate(struct skd_device *skdev)
3053{
3054 u32 sense;
3055 u32 state;
3056 u32 mtd;
3057 int prev_driver_state = skdev->state;
3058
3059 sense = SKD_READL(skdev, FIT_STATUS);
3060 state = sense & FIT_SR_DRIVE_STATE_MASK;
3061
3062 pr_err("(%s): s1120 state %s(%d)=>%s(%d)\n",
3063 skd_name(skdev),
3064 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3065 skd_drive_state_to_str(state), state);
3066
3067 skdev->drive_state = state;
3068
3069 switch (skdev->drive_state) {
3070 case FIT_SR_DRIVE_INIT:
3071 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
3072 skd_disable_interrupts(skdev);
3073 break;
3074 }
3075 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
3076 skd_recover_requests(skdev, 0);
3077 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
3078 skdev->timer_countdown = SKD_STARTING_TIMO;
3079 skdev->state = SKD_DRVR_STATE_STARTING;
3080 skd_soft_reset(skdev);
3081 break;
3082 }
3083 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
3084 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3085 skdev->last_mtd = mtd;
3086 break;
3087
3088 case FIT_SR_DRIVE_ONLINE:
3089 skdev->cur_max_queue_depth = skd_max_queue_depth;
3090 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
3091 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
3092
3093 skdev->queue_low_water_mark =
3094 skdev->cur_max_queue_depth * 2 / 3 + 1;
3095 if (skdev->queue_low_water_mark < 1)
3096 skdev->queue_low_water_mark = 1;
3097 pr_info(
3098 "(%s): Queue depth limit=%d dev=%d lowat=%d\n",
3099 skd_name(skdev),
3100 skdev->cur_max_queue_depth,
3101 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3102
3103 skd_refresh_device_data(skdev);
3104 break;
3105
3106 case FIT_SR_DRIVE_BUSY:
3107 skdev->state = SKD_DRVR_STATE_BUSY;
3108 skdev->timer_countdown = SKD_BUSY_TIMO;
3109 skd_quiesce_dev(skdev);
3110 break;
3111 case FIT_SR_DRIVE_BUSY_SANITIZE:
3112 /* set timer for 3 seconds, we'll abort any unfinished
3113 * commands after that expires
3114 */
3115 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3116 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003117 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003118 break;
3119 case FIT_SR_DRIVE_BUSY_ERASE:
3120 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3121 skdev->timer_countdown = SKD_BUSY_TIMO;
3122 break;
3123 case FIT_SR_DRIVE_OFFLINE:
3124 skdev->state = SKD_DRVR_STATE_IDLE;
3125 break;
3126 case FIT_SR_DRIVE_SOFT_RESET:
3127 switch (skdev->state) {
3128 case SKD_DRVR_STATE_STARTING:
3129 case SKD_DRVR_STATE_RESTARTING:
3130 /* Expected by a caller of skd_soft_reset() */
3131 break;
3132 default:
3133 skdev->state = SKD_DRVR_STATE_RESTARTING;
3134 break;
3135 }
3136 break;
3137 case FIT_SR_DRIVE_FW_BOOTING:
rchinthekindi2e44b422013-10-24 12:51:23 +01003138 pr_debug("%s:%s:%d ISR FIT_SR_DRIVE_FW_BOOTING %s\n",
3139 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003140 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3141 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3142 break;
3143
3144 case FIT_SR_DRIVE_DEGRADED:
3145 case FIT_SR_PCIE_LINK_DOWN:
3146 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3147 break;
3148
3149 case FIT_SR_DRIVE_FAULT:
3150 skd_drive_fault(skdev);
3151 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003152 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003153 break;
3154
3155 /* PCIe bus returned all Fs? */
3156 case 0xFF:
3157 pr_info("(%s): state=0x%x sense=0x%x\n",
3158 skd_name(skdev), state, sense);
3159 skd_drive_disappeared(skdev);
3160 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003161 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003162 break;
3163 default:
3164 /*
3165 * Uknown FW State. Wait for a state we recognize.
3166 */
3167 break;
3168 }
3169 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3170 skd_name(skdev),
3171 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3172 skd_skdev_state_to_str(skdev->state), skdev->state);
3173}
3174
3175static void skd_recover_requests(struct skd_device *skdev, int requeue)
3176{
3177 int i;
3178
3179 for (i = 0; i < skdev->num_req_context; i++) {
3180 struct skd_request_context *skreq = &skdev->skreq_table[i];
3181
3182 if (skreq->state == SKD_REQ_STATE_BUSY) {
3183 skd_log_skreq(skdev, skreq, "recover");
3184
3185 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003186 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003187
3188 /* Release DMA resources for the request. */
3189 if (skreq->n_sg > 0)
3190 skd_postop_sg_list(skdev, skreq);
3191
Jens Axboefcd37eb2013-11-01 10:14:56 -06003192 if (requeue &&
3193 (unsigned long) ++skreq->req->special <
3194 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003195 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003196 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003197 skd_end_request(skdev, skreq, -EIO);
3198
Jens Axboefcd37eb2013-11-01 10:14:56 -06003199 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003200
3201 skreq->state = SKD_REQ_STATE_IDLE;
3202 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003203 }
3204 if (i > 0)
3205 skreq[-1].next = skreq;
3206 skreq->next = NULL;
3207 }
3208 skdev->skreq_free_list = skdev->skreq_table;
3209
3210 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3211 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3212
3213 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3214 skd_log_skmsg(skdev, skmsg, "salvaged");
3215 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3216 skmsg->state = SKD_MSG_STATE_IDLE;
3217 skmsg->id += SKD_ID_INCR;
3218 }
3219 if (i > 0)
3220 skmsg[-1].next = skmsg;
3221 skmsg->next = NULL;
3222 }
3223 skdev->skmsg_free_list = skdev->skmsg_table;
3224
3225 for (i = 0; i < skdev->n_special; i++) {
3226 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3227
3228 /* If orphaned, reclaim it because it has already been reported
3229 * to the process as an error (it was just waiting for
3230 * a completion that didn't come, and now it will never come)
3231 * If busy, change to a state that will cause it to error
3232 * out in the wait routine and let it do the normal
3233 * reporting and reclaiming
3234 */
3235 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3236 if (skspcl->orphaned) {
rchinthekindi2e44b422013-10-24 12:51:23 +01003237 pr_debug("%s:%s:%d orphaned %p\n",
3238 skdev->name, __func__, __LINE__,
3239 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003240 skd_release_special(skdev, skspcl);
3241 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01003242 pr_debug("%s:%s:%d not orphaned %p\n",
3243 skdev->name, __func__, __LINE__,
3244 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003245 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3246 }
3247 }
3248 }
3249 skdev->skspcl_free_list = skdev->skspcl_table;
3250
3251 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3252 skdev->timeout_slot[i] = 0;
3253
3254 skdev->in_flight = 0;
3255}
3256
3257static void skd_isr_msg_from_dev(struct skd_device *skdev)
3258{
3259 u32 mfd;
3260 u32 mtd;
3261 u32 data;
3262
3263 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3264
rchinthekindi2e44b422013-10-24 12:51:23 +01003265 pr_debug("%s:%s:%d mfd=0x%x last_mtd=0x%x\n",
3266 skdev->name, __func__, __LINE__, mfd, skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003267
3268 /* ignore any mtd that is an ack for something we didn't send */
3269 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3270 return;
3271
3272 switch (FIT_MXD_TYPE(mfd)) {
3273 case FIT_MTD_FITFW_INIT:
3274 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3275
3276 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
3277 pr_err("(%s): protocol mismatch\n",
3278 skdev->name);
3279 pr_err("(%s): got=%d support=%d\n",
3280 skdev->name, skdev->proto_ver,
3281 FIT_PROTOCOL_VERSION_1);
3282 pr_err("(%s): please upgrade driver\n",
3283 skdev->name);
3284 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3285 skd_soft_reset(skdev);
3286 break;
3287 }
3288 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3289 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3290 skdev->last_mtd = mtd;
3291 break;
3292
3293 case FIT_MTD_GET_CMDQ_DEPTH:
3294 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3295 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3296 SKD_N_COMPLETION_ENTRY);
3297 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3298 skdev->last_mtd = mtd;
3299 break;
3300
3301 case FIT_MTD_SET_COMPQ_DEPTH:
3302 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3303 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3304 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3305 skdev->last_mtd = mtd;
3306 break;
3307
3308 case FIT_MTD_SET_COMPQ_ADDR:
3309 skd_reset_skcomp(skdev);
3310 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3311 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3312 skdev->last_mtd = mtd;
3313 break;
3314
3315 case FIT_MTD_CMD_LOG_HOST_ID:
3316 skdev->connect_time_stamp = get_seconds();
3317 data = skdev->connect_time_stamp & 0xFFFF;
3318 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3319 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3320 skdev->last_mtd = mtd;
3321 break;
3322
3323 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3324 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3325 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3326 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3327 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3328 skdev->last_mtd = mtd;
3329 break;
3330
3331 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3332 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3333 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3334 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3335 skdev->last_mtd = mtd;
3336
3337 pr_err("(%s): Time sync driver=0x%x device=0x%x\n",
3338 skd_name(skdev),
3339 skdev->connect_time_stamp, skdev->drive_jiffies);
3340 break;
3341
3342 case FIT_MTD_ARM_QUEUE:
3343 skdev->last_mtd = 0;
3344 /*
3345 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3346 */
3347 break;
3348
3349 default:
3350 break;
3351 }
3352}
3353
3354static void skd_disable_interrupts(struct skd_device *skdev)
3355{
3356 u32 sense;
3357
3358 sense = SKD_READL(skdev, FIT_CONTROL);
3359 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3360 SKD_WRITEL(skdev, sense, FIT_CONTROL);
rchinthekindi2e44b422013-10-24 12:51:23 +01003361 pr_debug("%s:%s:%d sense 0x%x\n",
3362 skdev->name, __func__, __LINE__, sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003363
3364 /* Note that the 1s is written. A 1-bit means
3365 * disable, a 0 means enable.
3366 */
3367 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3368}
3369
3370static void skd_enable_interrupts(struct skd_device *skdev)
3371{
3372 u32 val;
3373
3374 /* unmask interrupts first */
3375 val = FIT_ISH_FW_STATE_CHANGE +
3376 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3377
3378 /* Note that the compliment of mask is written. A 1-bit means
3379 * disable, a 0 means enable. */
3380 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003381 pr_debug("%s:%s:%d interrupt mask=0x%x\n",
3382 skdev->name, __func__, __LINE__, ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003383
3384 val = SKD_READL(skdev, FIT_CONTROL);
3385 val |= FIT_CR_ENABLE_INTERRUPTS;
rchinthekindi2e44b422013-10-24 12:51:23 +01003386 pr_debug("%s:%s:%d control=0x%x\n",
3387 skdev->name, __func__, __LINE__, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003388 SKD_WRITEL(skdev, val, FIT_CONTROL);
3389}
3390
3391/*
3392 *****************************************************************************
3393 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3394 *****************************************************************************
3395 */
3396
3397static void skd_soft_reset(struct skd_device *skdev)
3398{
3399 u32 val;
3400
3401 val = SKD_READL(skdev, FIT_CONTROL);
3402 val |= (FIT_CR_SOFT_RESET);
rchinthekindi2e44b422013-10-24 12:51:23 +01003403 pr_debug("%s:%s:%d control=0x%x\n",
3404 skdev->name, __func__, __LINE__, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003405 SKD_WRITEL(skdev, val, FIT_CONTROL);
3406}
3407
3408static void skd_start_device(struct skd_device *skdev)
3409{
3410 unsigned long flags;
3411 u32 sense;
3412 u32 state;
3413
3414 spin_lock_irqsave(&skdev->lock, flags);
3415
3416 /* ack all ghost interrupts */
3417 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3418
3419 sense = SKD_READL(skdev, FIT_STATUS);
3420
rchinthekindi2e44b422013-10-24 12:51:23 +01003421 pr_debug("%s:%s:%d initial status=0x%x\n",
3422 skdev->name, __func__, __LINE__, sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003423
3424 state = sense & FIT_SR_DRIVE_STATE_MASK;
3425 skdev->drive_state = state;
3426 skdev->last_mtd = 0;
3427
3428 skdev->state = SKD_DRVR_STATE_STARTING;
3429 skdev->timer_countdown = SKD_STARTING_TIMO;
3430
3431 skd_enable_interrupts(skdev);
3432
3433 switch (skdev->drive_state) {
3434 case FIT_SR_DRIVE_OFFLINE:
3435 pr_err("(%s): Drive offline...\n", skd_name(skdev));
3436 break;
3437
3438 case FIT_SR_DRIVE_FW_BOOTING:
rchinthekindi2e44b422013-10-24 12:51:23 +01003439 pr_debug("%s:%s:%d FIT_SR_DRIVE_FW_BOOTING %s\n",
3440 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003441 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3442 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3443 break;
3444
3445 case FIT_SR_DRIVE_BUSY_SANITIZE:
3446 pr_info("(%s): Start: BUSY_SANITIZE\n",
3447 skd_name(skdev));
3448 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3449 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3450 break;
3451
3452 case FIT_SR_DRIVE_BUSY_ERASE:
3453 pr_info("(%s): Start: BUSY_ERASE\n", skd_name(skdev));
3454 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3455 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3456 break;
3457
3458 case FIT_SR_DRIVE_INIT:
3459 case FIT_SR_DRIVE_ONLINE:
3460 skd_soft_reset(skdev);
3461 break;
3462
3463 case FIT_SR_DRIVE_BUSY:
3464 pr_err("(%s): Drive Busy...\n", skd_name(skdev));
3465 skdev->state = SKD_DRVR_STATE_BUSY;
3466 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3467 break;
3468
3469 case FIT_SR_DRIVE_SOFT_RESET:
3470 pr_err("(%s) drive soft reset in prog\n",
3471 skd_name(skdev));
3472 break;
3473
3474 case FIT_SR_DRIVE_FAULT:
3475 /* Fault state is bad...soft reset won't do it...
3476 * Hard reset, maybe, but does it work on device?
3477 * For now, just fault so the system doesn't hang.
3478 */
3479 skd_drive_fault(skdev);
3480 /*start the queue so we can respond with error to requests */
rchinthekindi2e44b422013-10-24 12:51:23 +01003481 pr_debug("%s:%s:%d starting %s queue\n",
3482 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003483 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003484 skdev->gendisk_on = -1;
3485 wake_up_interruptible(&skdev->waitq);
3486 break;
3487
3488 case 0xFF:
3489 /* Most likely the device isn't there or isn't responding
3490 * to the BAR1 addresses. */
3491 skd_drive_disappeared(skdev);
3492 /*start the queue so we can respond with error to requests */
rchinthekindi2e44b422013-10-24 12:51:23 +01003493 pr_debug("%s:%s:%d starting %s queue to error-out reqs\n",
3494 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003495 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003496 skdev->gendisk_on = -1;
3497 wake_up_interruptible(&skdev->waitq);
3498 break;
3499
3500 default:
3501 pr_err("(%s) Start: unknown state %x\n",
3502 skd_name(skdev), skdev->drive_state);
3503 break;
3504 }
3505
3506 state = SKD_READL(skdev, FIT_CONTROL);
rchinthekindi2e44b422013-10-24 12:51:23 +01003507 pr_debug("%s:%s:%d FIT Control Status=0x%x\n",
3508 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003509
3510 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003511 pr_debug("%s:%s:%d Intr Status=0x%x\n",
3512 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003513
3514 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003515 pr_debug("%s:%s:%d Intr Mask=0x%x\n",
3516 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003517
3518 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
rchinthekindi2e44b422013-10-24 12:51:23 +01003519 pr_debug("%s:%s:%d Msg from Dev=0x%x\n",
3520 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003521
3522 state = SKD_READL(skdev, FIT_HW_VERSION);
rchinthekindi2e44b422013-10-24 12:51:23 +01003523 pr_debug("%s:%s:%d HW version=0x%x\n",
3524 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003525
3526 spin_unlock_irqrestore(&skdev->lock, flags);
3527}
3528
3529static void skd_stop_device(struct skd_device *skdev)
3530{
3531 unsigned long flags;
3532 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3533 u32 dev_state;
3534 int i;
3535
3536 spin_lock_irqsave(&skdev->lock, flags);
3537
3538 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
3539 pr_err("(%s): skd_stop_device not online no sync\n",
3540 skd_name(skdev));
3541 goto stop_out;
3542 }
3543
3544 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
3545 pr_err("(%s): skd_stop_device no special\n",
3546 skd_name(skdev));
3547 goto stop_out;
3548 }
3549
3550 skdev->state = SKD_DRVR_STATE_SYNCING;
3551 skdev->sync_done = 0;
3552
3553 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3554
3555 spin_unlock_irqrestore(&skdev->lock, flags);
3556
3557 wait_event_interruptible_timeout(skdev->waitq,
3558 (skdev->sync_done), (10 * HZ));
3559
3560 spin_lock_irqsave(&skdev->lock, flags);
3561
3562 switch (skdev->sync_done) {
3563 case 0:
3564 pr_err("(%s): skd_stop_device no sync\n",
3565 skd_name(skdev));
3566 break;
3567 case 1:
3568 pr_err("(%s): skd_stop_device sync done\n",
3569 skd_name(skdev));
3570 break;
3571 default:
3572 pr_err("(%s): skd_stop_device sync error\n",
3573 skd_name(skdev));
3574 }
3575
3576stop_out:
3577 skdev->state = SKD_DRVR_STATE_STOPPING;
3578 spin_unlock_irqrestore(&skdev->lock, flags);
3579
3580 skd_kill_timer(skdev);
3581
3582 spin_lock_irqsave(&skdev->lock, flags);
3583 skd_disable_interrupts(skdev);
3584
3585 /* ensure all ints on device are cleared */
3586 /* soft reset the device to unload with a clean slate */
3587 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3588 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3589
3590 spin_unlock_irqrestore(&skdev->lock, flags);
3591
3592 /* poll every 100ms, 1 second timeout */
3593 for (i = 0; i < 10; i++) {
3594 dev_state =
3595 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3596 if (dev_state == FIT_SR_DRIVE_INIT)
3597 break;
3598 set_current_state(TASK_INTERRUPTIBLE);
3599 schedule_timeout(msecs_to_jiffies(100));
3600 }
3601
3602 if (dev_state != FIT_SR_DRIVE_INIT)
3603 pr_err("(%s): skd_stop_device state error 0x%02x\n",
3604 skd_name(skdev), dev_state);
3605}
3606
3607/* assume spinlock is held */
3608static void skd_restart_device(struct skd_device *skdev)
3609{
3610 u32 state;
3611
3612 /* ack all ghost interrupts */
3613 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3614
3615 state = SKD_READL(skdev, FIT_STATUS);
3616
rchinthekindi2e44b422013-10-24 12:51:23 +01003617 pr_debug("%s:%s:%d drive status=0x%x\n",
3618 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003619
3620 state &= FIT_SR_DRIVE_STATE_MASK;
3621 skdev->drive_state = state;
3622 skdev->last_mtd = 0;
3623
3624 skdev->state = SKD_DRVR_STATE_RESTARTING;
3625 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3626
3627 skd_soft_reset(skdev);
3628}
3629
3630/* assume spinlock is held */
3631static int skd_quiesce_dev(struct skd_device *skdev)
3632{
3633 int rc = 0;
3634
3635 switch (skdev->state) {
3636 case SKD_DRVR_STATE_BUSY:
3637 case SKD_DRVR_STATE_BUSY_IMMINENT:
rchinthekindi2e44b422013-10-24 12:51:23 +01003638 pr_debug("%s:%s:%d stopping %s queue\n",
3639 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003640 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003641 break;
3642 case SKD_DRVR_STATE_ONLINE:
3643 case SKD_DRVR_STATE_STOPPING:
3644 case SKD_DRVR_STATE_SYNCING:
3645 case SKD_DRVR_STATE_PAUSING:
3646 case SKD_DRVR_STATE_PAUSED:
3647 case SKD_DRVR_STATE_STARTING:
3648 case SKD_DRVR_STATE_RESTARTING:
3649 case SKD_DRVR_STATE_RESUMING:
3650 default:
3651 rc = -EINVAL;
rchinthekindi2e44b422013-10-24 12:51:23 +01003652 pr_debug("%s:%s:%d state [%d] not implemented\n",
3653 skdev->name, __func__, __LINE__, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003654 }
3655 return rc;
3656}
3657
3658/* assume spinlock is held */
3659static int skd_unquiesce_dev(struct skd_device *skdev)
3660{
3661 int prev_driver_state = skdev->state;
3662
3663 skd_log_skdev(skdev, "unquiesce");
3664 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
rchinthekindi2e44b422013-10-24 12:51:23 +01003665 pr_debug("%s:%s:%d **** device already ONLINE\n",
3666 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003667 return 0;
3668 }
3669 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3670 /*
3671 * If there has been an state change to other than
3672 * ONLINE, we will rely on controller state change
3673 * to come back online and restart the queue.
3674 * The BUSY state means that driver is ready to
3675 * continue normal processing but waiting for controller
3676 * to become available.
3677 */
3678 skdev->state = SKD_DRVR_STATE_BUSY;
rchinthekindi2e44b422013-10-24 12:51:23 +01003679 pr_debug("%s:%s:%d drive BUSY state\n",
3680 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003681 return 0;
3682 }
3683
3684 /*
3685 * Drive has just come online, driver is either in startup,
3686 * paused performing a task, or bust waiting for hardware.
3687 */
3688 switch (skdev->state) {
3689 case SKD_DRVR_STATE_PAUSED:
3690 case SKD_DRVR_STATE_BUSY:
3691 case SKD_DRVR_STATE_BUSY_IMMINENT:
3692 case SKD_DRVR_STATE_BUSY_ERASE:
3693 case SKD_DRVR_STATE_STARTING:
3694 case SKD_DRVR_STATE_RESTARTING:
3695 case SKD_DRVR_STATE_FAULT:
3696 case SKD_DRVR_STATE_IDLE:
3697 case SKD_DRVR_STATE_LOAD:
3698 skdev->state = SKD_DRVR_STATE_ONLINE;
3699 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3700 skd_name(skdev),
3701 skd_skdev_state_to_str(prev_driver_state),
3702 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3703 skdev->state);
rchinthekindi2e44b422013-10-24 12:51:23 +01003704 pr_debug("%s:%s:%d **** device ONLINE...starting block queue\n",
3705 skdev->name, __func__, __LINE__);
3706 pr_debug("%s:%s:%d starting %s queue\n",
3707 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003708 pr_info("(%s): STEC s1120 ONLINE\n", skd_name(skdev));
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06003709 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003710 skdev->gendisk_on = 1;
3711 wake_up_interruptible(&skdev->waitq);
3712 break;
3713
3714 case SKD_DRVR_STATE_DISAPPEARED:
3715 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01003716 pr_debug("%s:%s:%d **** driver state %d, not implemented \n",
3717 skdev->name, __func__, __LINE__,
3718 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003719 return -EBUSY;
3720 }
3721 return 0;
3722}
3723
3724/*
3725 *****************************************************************************
3726 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3727 *****************************************************************************
3728 */
3729
3730static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3731{
3732 struct skd_device *skdev = skd_host_data;
3733 unsigned long flags;
3734
3735 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003736 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3737 skdev->name, __func__, __LINE__,
3738 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003739 pr_err("(%s): MSIX reserved irq %d = 0x%x\n", skd_name(skdev),
3740 irq, SKD_READL(skdev, FIT_INT_STATUS_HOST));
3741 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3742 spin_unlock_irqrestore(&skdev->lock, flags);
3743 return IRQ_HANDLED;
3744}
3745
3746static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3747{
3748 struct skd_device *skdev = skd_host_data;
3749 unsigned long flags;
3750
3751 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003752 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3753 skdev->name, __func__, __LINE__,
3754 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003755 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3756 skd_isr_fwstate(skdev);
3757 spin_unlock_irqrestore(&skdev->lock, flags);
3758 return IRQ_HANDLED;
3759}
3760
3761static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3762{
3763 struct skd_device *skdev = skd_host_data;
3764 unsigned long flags;
3765 int flush_enqueued = 0;
3766 int deferred;
3767
3768 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003769 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3770 skdev->name, __func__, __LINE__,
3771 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003772 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3773 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3774 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003775 if (flush_enqueued)
3776 skd_request_fn(skdev->queue);
3777
3778 if (deferred)
3779 schedule_work(&skdev->completion_worker);
3780 else if (!flush_enqueued)
3781 skd_request_fn(skdev->queue);
3782
3783 spin_unlock_irqrestore(&skdev->lock, flags);
3784
3785 return IRQ_HANDLED;
3786}
3787
3788static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3789{
3790 struct skd_device *skdev = skd_host_data;
3791 unsigned long flags;
3792
3793 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003794 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3795 skdev->name, __func__, __LINE__,
3796 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003797 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3798 skd_isr_msg_from_dev(skdev);
3799 spin_unlock_irqrestore(&skdev->lock, flags);
3800 return IRQ_HANDLED;
3801}
3802
3803static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3804{
3805 struct skd_device *skdev = skd_host_data;
3806 unsigned long flags;
3807
3808 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003809 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3810 skdev->name, __func__, __LINE__,
3811 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003812 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3813 spin_unlock_irqrestore(&skdev->lock, flags);
3814 return IRQ_HANDLED;
3815}
3816
3817/*
3818 *****************************************************************************
3819 * PCIe MSI/MSI-X SETUP
3820 *****************************************************************************
3821 */
3822
3823struct skd_msix_entry {
3824 int have_irq;
3825 u32 vector;
3826 u32 entry;
3827 struct skd_device *rsp;
3828 char isr_name[30];
3829};
3830
3831struct skd_init_msix_entry {
3832 const char *name;
3833 irq_handler_t handler;
3834};
3835
3836#define SKD_MAX_MSIX_COUNT 13
3837#define SKD_MIN_MSIX_COUNT 7
3838#define SKD_BASE_MSIX_IRQ 4
3839
3840static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3841 { "(DMA 0)", skd_reserved_isr },
3842 { "(DMA 1)", skd_reserved_isr },
3843 { "(DMA 2)", skd_reserved_isr },
3844 { "(DMA 3)", skd_reserved_isr },
3845 { "(State Change)", skd_statec_isr },
3846 { "(COMPL_Q)", skd_comp_q },
3847 { "(MSG)", skd_msg_isr },
3848 { "(Reserved)", skd_reserved_isr },
3849 { "(Reserved)", skd_reserved_isr },
3850 { "(Queue Full 0)", skd_qfull_isr },
3851 { "(Queue Full 1)", skd_qfull_isr },
3852 { "(Queue Full 2)", skd_qfull_isr },
3853 { "(Queue Full 3)", skd_qfull_isr },
3854};
3855
3856static void skd_release_msix(struct skd_device *skdev)
3857{
3858 struct skd_msix_entry *qentry;
3859 int i;
3860
Alexander Gordeev46817762014-02-19 09:58:19 +01003861 if (skdev->msix_entries) {
3862 for (i = 0; i < skdev->msix_count; i++) {
3863 qentry = &skdev->msix_entries[i];
3864 skdev = qentry->rsp;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003865
Alexander Gordeev46817762014-02-19 09:58:19 +01003866 if (qentry->have_irq)
3867 devm_free_irq(&skdev->pdev->dev,
3868 qentry->vector, qentry->rsp);
3869 }
3870
3871 kfree(skdev->msix_entries);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003872 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003873
3874 if (skdev->msix_count)
3875 pci_disable_msix(skdev->pdev);
3876
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003877 skdev->msix_count = 0;
3878 skdev->msix_entries = NULL;
3879}
3880
3881static int skd_acquire_msix(struct skd_device *skdev)
3882{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003883 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003884 struct pci_dev *pdev = skdev->pdev;
3885 struct msix_entry *entries;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003886 struct skd_msix_entry *qentry;
3887
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003888 entries = kzalloc(sizeof(struct msix_entry) * SKD_MAX_MSIX_COUNT,
3889 GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003890 if (!entries)
3891 return -ENOMEM;
3892
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003893 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003894 entries[i].entry = i;
3895
Alexander Gordeev01aad3f2014-04-21 17:19:31 +02003896 rc = pci_enable_msix_exact(pdev, entries, SKD_MAX_MSIX_COUNT);
3897 if (rc) {
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003898 pr_err("(%s): failed to enable MSI-X %d\n",
3899 skd_name(skdev), rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003900 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003901 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003902
Alexander Gordeev01aad3f2014-04-21 17:19:31 +02003903 skdev->msix_count = SKD_MAX_MSIX_COUNT;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003904 skdev->msix_entries = kzalloc(sizeof(struct skd_msix_entry) *
3905 skdev->msix_count, GFP_KERNEL);
3906 if (!skdev->msix_entries) {
3907 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003908 pr_err("(%s): msix table allocation error\n",
3909 skd_name(skdev));
3910 goto msix_out;
3911 }
3912
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003913 for (i = 0; i < skdev->msix_count; i++) {
Alexander Gordeev1bc5ce52014-02-19 09:58:20 +01003914 qentry = &skdev->msix_entries[i];
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003915 qentry->vector = entries[i].vector;
3916 qentry->entry = entries[i].entry;
3917 qentry->rsp = NULL;
3918 qentry->have_irq = 0;
rchinthekindi2e44b422013-10-24 12:51:23 +01003919 pr_debug("%s:%s:%d %s: <%s> msix (%d) vec %d, entry %x\n",
3920 skdev->name, __func__, __LINE__,
3921 pci_name(pdev), skdev->name,
3922 i, qentry->vector, qentry->entry);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003923 }
3924
3925 /* Enable MSI-X vectors for the base queue */
Alexander Gordeevc5e30352014-02-19 09:58:18 +01003926 for (i = 0; i < skdev->msix_count; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003927 qentry = &skdev->msix_entries[i];
3928 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3929 "%s%d-msix %s", DRV_NAME, skdev->devno,
3930 msix_entries[i].name);
3931 rc = devm_request_irq(&skdev->pdev->dev, qentry->vector,
3932 msix_entries[i].handler, 0,
3933 qentry->isr_name, skdev);
3934 if (rc) {
3935 pr_err("(%s): Unable to register(%d) MSI-X "
3936 "handler %d: %s\n",
3937 skd_name(skdev), rc, i, qentry->isr_name);
3938 goto msix_out;
3939 } else {
3940 qentry->have_irq = 1;
3941 qentry->rsp = skdev;
3942 }
3943 }
rchinthekindi2e44b422013-10-24 12:51:23 +01003944 pr_debug("%s:%s:%d %s: <%s> msix %d irq(s) enabled\n",
3945 skdev->name, __func__, __LINE__,
3946 pci_name(pdev), skdev->name, skdev->msix_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003947 return 0;
3948
3949msix_out:
3950 if (entries)
3951 kfree(entries);
3952 skd_release_msix(skdev);
3953 return rc;
3954}
3955
3956static int skd_acquire_irq(struct skd_device *skdev)
3957{
3958 int rc;
3959 struct pci_dev *pdev;
3960
3961 pdev = skdev->pdev;
3962 skdev->msix_count = 0;
3963
3964RETRY_IRQ_TYPE:
3965 switch (skdev->irq_type) {
3966 case SKD_IRQ_MSIX:
3967 rc = skd_acquire_msix(skdev);
3968 if (!rc)
3969 pr_info("(%s): MSI-X %d irqs enabled\n",
3970 skd_name(skdev), skdev->msix_count);
3971 else {
3972 pr_err(
3973 "(%s): failed to enable MSI-X, re-trying with MSI %d\n",
3974 skd_name(skdev), rc);
3975 skdev->irq_type = SKD_IRQ_MSI;
3976 goto RETRY_IRQ_TYPE;
3977 }
3978 break;
3979 case SKD_IRQ_MSI:
3980 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d-msi",
3981 DRV_NAME, skdev->devno);
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003982 rc = pci_enable_msi_range(pdev, 1, 1);
3983 if (rc > 0) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003984 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr, 0,
3985 skdev->isr_name, skdev);
3986 if (rc) {
3987 pci_disable_msi(pdev);
3988 pr_err(
3989 "(%s): failed to allocate the MSI interrupt %d\n",
3990 skd_name(skdev), rc);
3991 goto RETRY_IRQ_LEGACY;
3992 }
3993 pr_info("(%s): MSI irq %d enabled\n",
3994 skd_name(skdev), pdev->irq);
3995 } else {
3996RETRY_IRQ_LEGACY:
3997 pr_err(
3998 "(%s): failed to enable MSI, re-trying with LEGACY %d\n",
3999 skd_name(skdev), rc);
4000 skdev->irq_type = SKD_IRQ_LEGACY;
4001 goto RETRY_IRQ_TYPE;
4002 }
4003 break;
4004 case SKD_IRQ_LEGACY:
4005 snprintf(skdev->isr_name, sizeof(skdev->isr_name),
4006 "%s%d-legacy", DRV_NAME, skdev->devno);
4007 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
4008 IRQF_SHARED, skdev->isr_name, skdev);
4009 if (!rc)
4010 pr_info("(%s): LEGACY irq %d enabled\n",
4011 skd_name(skdev), pdev->irq);
4012 else
4013 pr_err("(%s): request LEGACY irq error %d\n",
4014 skd_name(skdev), rc);
4015 break;
4016 default:
4017 pr_info("(%s): irq_type %d invalid, re-set to %d\n",
4018 skd_name(skdev), skdev->irq_type, SKD_IRQ_DEFAULT);
4019 skdev->irq_type = SKD_IRQ_LEGACY;
4020 goto RETRY_IRQ_TYPE;
4021 }
4022 return rc;
4023}
4024
4025static void skd_release_irq(struct skd_device *skdev)
4026{
4027 switch (skdev->irq_type) {
4028 case SKD_IRQ_MSIX:
4029 skd_release_msix(skdev);
4030 break;
4031 case SKD_IRQ_MSI:
4032 devm_free_irq(&skdev->pdev->dev, skdev->pdev->irq, skdev);
4033 pci_disable_msi(skdev->pdev);
4034 break;
4035 case SKD_IRQ_LEGACY:
4036 devm_free_irq(&skdev->pdev->dev, skdev->pdev->irq, skdev);
4037 break;
4038 default:
4039 pr_err("(%s): wrong irq type %d!",
4040 skd_name(skdev), skdev->irq_type);
4041 break;
4042 }
4043}
4044
4045/*
4046 *****************************************************************************
4047 * CONSTRUCT
4048 *****************************************************************************
4049 */
4050
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004051static int skd_cons_skcomp(struct skd_device *skdev)
4052{
4053 int rc = 0;
4054 struct fit_completion_entry_v1 *skcomp;
4055 u32 nbytes;
4056
4057 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
4058 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
4059
rchinthekindi2e44b422013-10-24 12:51:23 +01004060 pr_debug("%s:%s:%d comp pci_alloc, total bytes %d entries %d\n",
4061 skdev->name, __func__, __LINE__,
4062 nbytes, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004063
Joe Perchesa5bbf612014-08-08 14:24:12 -07004064 skcomp = pci_zalloc_consistent(skdev->pdev, nbytes,
4065 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004066
4067 if (skcomp == NULL) {
4068 rc = -ENOMEM;
4069 goto err_out;
4070 }
4071
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004072 skdev->skcomp_table = skcomp;
4073 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
4074 sizeof(*skcomp) *
4075 SKD_N_COMPLETION_ENTRY);
4076
4077err_out:
4078 return rc;
4079}
4080
4081static int skd_cons_skmsg(struct skd_device *skdev)
4082{
4083 int rc = 0;
4084 u32 i;
4085
rchinthekindi2e44b422013-10-24 12:51:23 +01004086 pr_debug("%s:%s:%d skmsg_table kzalloc, struct %lu, count %u total %lu\n",
4087 skdev->name, __func__, __LINE__,
4088 sizeof(struct skd_fitmsg_context),
4089 skdev->num_fitmsg_context,
4090 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004091
4092 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
4093 *skdev->num_fitmsg_context, GFP_KERNEL);
4094 if (skdev->skmsg_table == NULL) {
4095 rc = -ENOMEM;
4096 goto err_out;
4097 }
4098
4099 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4100 struct skd_fitmsg_context *skmsg;
4101
4102 skmsg = &skdev->skmsg_table[i];
4103
4104 skmsg->id = i + SKD_ID_FIT_MSG;
4105
4106 skmsg->state = SKD_MSG_STATE_IDLE;
4107 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
4108 SKD_N_FITMSG_BYTES + 64,
4109 &skmsg->mb_dma_address);
4110
4111 if (skmsg->msg_buf == NULL) {
4112 rc = -ENOMEM;
4113 goto err_out;
4114 }
4115
4116 skmsg->offset = (u32)((u64)skmsg->msg_buf &
4117 (~FIT_QCMD_BASE_ADDRESS_MASK));
4118 skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK;
4119 skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf &
4120 FIT_QCMD_BASE_ADDRESS_MASK);
4121 skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK;
4122 skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK;
4123 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
4124
4125 skmsg->next = &skmsg[1];
4126 }
4127
4128 /* Free list is in order starting with the 0th entry. */
4129 skdev->skmsg_table[i - 1].next = NULL;
4130 skdev->skmsg_free_list = skdev->skmsg_table;
4131
4132err_out:
4133 return rc;
4134}
4135
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004136static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
4137 u32 n_sg,
4138 dma_addr_t *ret_dma_addr)
4139{
4140 struct fit_sg_descriptor *sg_list;
4141 u32 nbytes;
4142
4143 nbytes = sizeof(*sg_list) * n_sg;
4144
4145 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
4146
4147 if (sg_list != NULL) {
4148 uint64_t dma_address = *ret_dma_addr;
4149 u32 i;
4150
4151 memset(sg_list, 0, nbytes);
4152
4153 for (i = 0; i < n_sg - 1; i++) {
4154 uint64_t ndp_off;
4155 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
4156
4157 sg_list[i].next_desc_ptr = dma_address + ndp_off;
4158 }
4159 sg_list[i].next_desc_ptr = 0LL;
4160 }
4161
4162 return sg_list;
4163}
4164
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004165static int skd_cons_skreq(struct skd_device *skdev)
4166{
4167 int rc = 0;
4168 u32 i;
4169
rchinthekindi2e44b422013-10-24 12:51:23 +01004170 pr_debug("%s:%s:%d skreq_table kzalloc, struct %lu, count %u total %lu\n",
4171 skdev->name, __func__, __LINE__,
4172 sizeof(struct skd_request_context),
4173 skdev->num_req_context,
4174 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004175
4176 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
4177 * skdev->num_req_context, GFP_KERNEL);
4178 if (skdev->skreq_table == NULL) {
4179 rc = -ENOMEM;
4180 goto err_out;
4181 }
4182
rchinthekindi2e44b422013-10-24 12:51:23 +01004183 pr_debug("%s:%s:%d alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
4184 skdev->name, __func__, __LINE__,
4185 skdev->sgs_per_request, sizeof(struct scatterlist),
4186 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004187
4188 for (i = 0; i < skdev->num_req_context; i++) {
4189 struct skd_request_context *skreq;
4190
4191 skreq = &skdev->skreq_table[i];
4192
4193 skreq->id = i + SKD_ID_RW_REQUEST;
4194 skreq->state = SKD_REQ_STATE_IDLE;
4195
4196 skreq->sg = kzalloc(sizeof(struct scatterlist) *
4197 skdev->sgs_per_request, GFP_KERNEL);
4198 if (skreq->sg == NULL) {
4199 rc = -ENOMEM;
4200 goto err_out;
4201 }
4202 sg_init_table(skreq->sg, skdev->sgs_per_request);
4203
4204 skreq->sksg_list = skd_cons_sg_list(skdev,
4205 skdev->sgs_per_request,
4206 &skreq->sksg_dma_address);
4207
4208 if (skreq->sksg_list == NULL) {
4209 rc = -ENOMEM;
4210 goto err_out;
4211 }
4212
4213 skreq->next = &skreq[1];
4214 }
4215
4216 /* Free list is in order starting with the 0th entry. */
4217 skdev->skreq_table[i - 1].next = NULL;
4218 skdev->skreq_free_list = skdev->skreq_table;
4219
4220err_out:
4221 return rc;
4222}
4223
4224static int skd_cons_skspcl(struct skd_device *skdev)
4225{
4226 int rc = 0;
4227 u32 i, nbytes;
4228
rchinthekindi2e44b422013-10-24 12:51:23 +01004229 pr_debug("%s:%s:%d skspcl_table kzalloc, struct %lu, count %u total %lu\n",
4230 skdev->name, __func__, __LINE__,
4231 sizeof(struct skd_special_context),
4232 skdev->n_special,
4233 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004234
4235 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
4236 * skdev->n_special, GFP_KERNEL);
4237 if (skdev->skspcl_table == NULL) {
4238 rc = -ENOMEM;
4239 goto err_out;
4240 }
4241
4242 for (i = 0; i < skdev->n_special; i++) {
4243 struct skd_special_context *skspcl;
4244
4245 skspcl = &skdev->skspcl_table[i];
4246
4247 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
4248 skspcl->req.state = SKD_REQ_STATE_IDLE;
4249
4250 skspcl->req.next = &skspcl[1].req;
4251
4252 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4253
Joe Perchesa5bbf612014-08-08 14:24:12 -07004254 skspcl->msg_buf =
4255 pci_zalloc_consistent(skdev->pdev, nbytes,
4256 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004257 if (skspcl->msg_buf == NULL) {
4258 rc = -ENOMEM;
4259 goto err_out;
4260 }
4261
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004262 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4263 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4264 if (skspcl->req.sg == NULL) {
4265 rc = -ENOMEM;
4266 goto err_out;
4267 }
4268
4269 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4270 SKD_N_SG_PER_SPECIAL,
4271 &skspcl->req.
4272 sksg_dma_address);
4273 if (skspcl->req.sksg_list == NULL) {
4274 rc = -ENOMEM;
4275 goto err_out;
4276 }
4277 }
4278
4279 /* Free list is in order starting with the 0th entry. */
4280 skdev->skspcl_table[i - 1].req.next = NULL;
4281 skdev->skspcl_free_list = skdev->skspcl_table;
4282
4283 return rc;
4284
4285err_out:
4286 return rc;
4287}
4288
4289static int skd_cons_sksb(struct skd_device *skdev)
4290{
4291 int rc = 0;
4292 struct skd_special_context *skspcl;
4293 u32 nbytes;
4294
4295 skspcl = &skdev->internal_skspcl;
4296
4297 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4298 skspcl->req.state = SKD_REQ_STATE_IDLE;
4299
4300 nbytes = SKD_N_INTERNAL_BYTES;
4301
Joe Perchesa5bbf612014-08-08 14:24:12 -07004302 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4303 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004304 if (skspcl->data_buf == NULL) {
4305 rc = -ENOMEM;
4306 goto err_out;
4307 }
4308
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004309 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004310 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4311 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004312 if (skspcl->msg_buf == NULL) {
4313 rc = -ENOMEM;
4314 goto err_out;
4315 }
4316
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004317 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4318 &skspcl->req.sksg_dma_address);
4319 if (skspcl->req.sksg_list == NULL) {
4320 rc = -ENOMEM;
4321 goto err_out;
4322 }
4323
4324 if (!skd_format_internal_skspcl(skdev)) {
4325 rc = -EINVAL;
4326 goto err_out;
4327 }
4328
4329err_out:
4330 return rc;
4331}
4332
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004333static int skd_cons_disk(struct skd_device *skdev)
4334{
4335 int rc = 0;
4336 struct gendisk *disk;
4337 struct request_queue *q;
4338 unsigned long flags;
4339
4340 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4341 if (!disk) {
4342 rc = -ENOMEM;
4343 goto err_out;
4344 }
4345
4346 skdev->disk = disk;
4347 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4348
4349 disk->major = skdev->major;
4350 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4351 disk->fops = &skd_blockdev_ops;
4352 disk->private_data = skdev;
4353
Jens Axboefcd37eb2013-11-01 10:14:56 -06004354 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004355 if (!q) {
4356 rc = -ENOMEM;
4357 goto err_out;
4358 }
4359
4360 skdev->queue = q;
4361 disk->queue = q;
4362 q->queuedata = skdev;
4363
Jens Axboe6975f732016-03-30 10:11:42 -06004364 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004365 blk_queue_max_segments(q, skdev->sgs_per_request);
4366 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4367
4368 /* set sysfs ptimal_io_size to 8K */
4369 blk_queue_io_opt(q, 8192);
4370
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004371 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004372 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004373
4374 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01004375 pr_debug("%s:%s:%d stopping %s queue\n",
4376 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec65b2013-11-01 10:38:45 -06004377 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004378 spin_unlock_irqrestore(&skdev->lock, flags);
4379
4380err_out:
4381 return rc;
4382}
4383
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004384#define SKD_N_DEV_TABLE 16u
4385static u32 skd_next_devno;
4386
4387static struct skd_device *skd_construct(struct pci_dev *pdev)
4388{
4389 struct skd_device *skdev;
4390 int blk_major = skd_major;
4391 int rc;
4392
4393 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4394
4395 if (!skdev) {
4396 pr_err(PFX "(%s): memory alloc failure\n",
4397 pci_name(pdev));
4398 return NULL;
4399 }
4400
4401 skdev->state = SKD_DRVR_STATE_LOAD;
4402 skdev->pdev = pdev;
4403 skdev->devno = skd_next_devno++;
4404 skdev->major = blk_major;
4405 skdev->irq_type = skd_isr_type;
4406 sprintf(skdev->name, DRV_NAME "%d", skdev->devno);
4407 skdev->dev_max_queue_depth = 0;
4408
4409 skdev->num_req_context = skd_max_queue_depth;
4410 skdev->num_fitmsg_context = skd_max_queue_depth;
4411 skdev->n_special = skd_max_pass_thru;
4412 skdev->cur_max_queue_depth = 1;
4413 skdev->queue_low_water_mark = 1;
4414 skdev->proto_ver = 99;
4415 skdev->sgs_per_request = skd_sgs_per_request;
4416 skdev->dbg_level = skd_dbg_level;
4417
4418 atomic_set(&skdev->device_count, 0);
4419
4420 spin_lock_init(&skdev->lock);
4421
4422 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4423
4424 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4425 rc = skd_cons_skcomp(skdev);
4426 if (rc < 0)
4427 goto err_out;
4428
4429 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4430 rc = skd_cons_skmsg(skdev);
4431 if (rc < 0)
4432 goto err_out;
4433
4434 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4435 rc = skd_cons_skreq(skdev);
4436 if (rc < 0)
4437 goto err_out;
4438
4439 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4440 rc = skd_cons_skspcl(skdev);
4441 if (rc < 0)
4442 goto err_out;
4443
4444 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4445 rc = skd_cons_sksb(skdev);
4446 if (rc < 0)
4447 goto err_out;
4448
4449 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4450 rc = skd_cons_disk(skdev);
4451 if (rc < 0)
4452 goto err_out;
4453
4454 pr_debug("%s:%s:%d VICTORY\n", skdev->name, __func__, __LINE__);
4455 return skdev;
4456
4457err_out:
4458 pr_debug("%s:%s:%d construct failed\n",
4459 skdev->name, __func__, __LINE__);
4460 skd_destruct(skdev);
4461 return NULL;
4462}
4463
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004464/*
4465 *****************************************************************************
4466 * DESTRUCT (FREE)
4467 *****************************************************************************
4468 */
4469
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004470static void skd_free_skcomp(struct skd_device *skdev)
4471{
4472 if (skdev->skcomp_table != NULL) {
4473 u32 nbytes;
4474
4475 nbytes = sizeof(skdev->skcomp_table[0]) *
4476 SKD_N_COMPLETION_ENTRY;
4477 pci_free_consistent(skdev->pdev, nbytes,
4478 skdev->skcomp_table, skdev->cq_dma_address);
4479 }
4480
4481 skdev->skcomp_table = NULL;
4482 skdev->cq_dma_address = 0;
4483}
4484
4485static void skd_free_skmsg(struct skd_device *skdev)
4486{
4487 u32 i;
4488
4489 if (skdev->skmsg_table == NULL)
4490 return;
4491
4492 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4493 struct skd_fitmsg_context *skmsg;
4494
4495 skmsg = &skdev->skmsg_table[i];
4496
4497 if (skmsg->msg_buf != NULL) {
4498 skmsg->msg_buf += skmsg->offset;
4499 skmsg->mb_dma_address += skmsg->offset;
4500 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4501 skmsg->msg_buf,
4502 skmsg->mb_dma_address);
4503 }
4504 skmsg->msg_buf = NULL;
4505 skmsg->mb_dma_address = 0;
4506 }
4507
4508 kfree(skdev->skmsg_table);
4509 skdev->skmsg_table = NULL;
4510}
4511
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004512static void skd_free_sg_list(struct skd_device *skdev,
4513 struct fit_sg_descriptor *sg_list,
4514 u32 n_sg, dma_addr_t dma_addr)
4515{
4516 if (sg_list != NULL) {
4517 u32 nbytes;
4518
4519 nbytes = sizeof(*sg_list) * n_sg;
4520
4521 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4522 }
4523}
4524
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004525static void skd_free_skreq(struct skd_device *skdev)
4526{
4527 u32 i;
4528
4529 if (skdev->skreq_table == NULL)
4530 return;
4531
4532 for (i = 0; i < skdev->num_req_context; i++) {
4533 struct skd_request_context *skreq;
4534
4535 skreq = &skdev->skreq_table[i];
4536
4537 skd_free_sg_list(skdev, skreq->sksg_list,
4538 skdev->sgs_per_request,
4539 skreq->sksg_dma_address);
4540
4541 skreq->sksg_list = NULL;
4542 skreq->sksg_dma_address = 0;
4543
4544 kfree(skreq->sg);
4545 }
4546
4547 kfree(skdev->skreq_table);
4548 skdev->skreq_table = NULL;
4549}
4550
4551static void skd_free_skspcl(struct skd_device *skdev)
4552{
4553 u32 i;
4554 u32 nbytes;
4555
4556 if (skdev->skspcl_table == NULL)
4557 return;
4558
4559 for (i = 0; i < skdev->n_special; i++) {
4560 struct skd_special_context *skspcl;
4561
4562 skspcl = &skdev->skspcl_table[i];
4563
4564 if (skspcl->msg_buf != NULL) {
4565 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4566 pci_free_consistent(skdev->pdev, nbytes,
4567 skspcl->msg_buf,
4568 skspcl->mb_dma_address);
4569 }
4570
4571 skspcl->msg_buf = NULL;
4572 skspcl->mb_dma_address = 0;
4573
4574 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4575 SKD_N_SG_PER_SPECIAL,
4576 skspcl->req.sksg_dma_address);
4577
4578 skspcl->req.sksg_list = NULL;
4579 skspcl->req.sksg_dma_address = 0;
4580
4581 kfree(skspcl->req.sg);
4582 }
4583
4584 kfree(skdev->skspcl_table);
4585 skdev->skspcl_table = NULL;
4586}
4587
4588static void skd_free_sksb(struct skd_device *skdev)
4589{
4590 struct skd_special_context *skspcl;
4591 u32 nbytes;
4592
4593 skspcl = &skdev->internal_skspcl;
4594
4595 if (skspcl->data_buf != NULL) {
4596 nbytes = SKD_N_INTERNAL_BYTES;
4597
4598 pci_free_consistent(skdev->pdev, nbytes,
4599 skspcl->data_buf, skspcl->db_dma_address);
4600 }
4601
4602 skspcl->data_buf = NULL;
4603 skspcl->db_dma_address = 0;
4604
4605 if (skspcl->msg_buf != NULL) {
4606 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4607 pci_free_consistent(skdev->pdev, nbytes,
4608 skspcl->msg_buf, skspcl->mb_dma_address);
4609 }
4610
4611 skspcl->msg_buf = NULL;
4612 skspcl->mb_dma_address = 0;
4613
4614 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4615 skspcl->req.sksg_dma_address);
4616
4617 skspcl->req.sksg_list = NULL;
4618 skspcl->req.sksg_dma_address = 0;
4619}
4620
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004621static void skd_free_disk(struct skd_device *skdev)
4622{
4623 struct gendisk *disk = skdev->disk;
4624
4625 if (disk != NULL) {
4626 struct request_queue *q = disk->queue;
4627
4628 if (disk->flags & GENHD_FL_UP)
4629 del_gendisk(disk);
4630 if (q)
4631 blk_cleanup_queue(q);
4632 put_disk(disk);
4633 }
4634 skdev->disk = NULL;
4635}
4636
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004637static void skd_destruct(struct skd_device *skdev)
4638{
4639 if (skdev == NULL)
4640 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004641
4642
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004643 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4644 skd_free_disk(skdev);
4645
4646 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4647 skd_free_sksb(skdev);
4648
4649 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4650 skd_free_skspcl(skdev);
4651
4652 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4653 skd_free_skreq(skdev);
4654
4655 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4656 skd_free_skmsg(skdev);
4657
4658 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4659 skd_free_skcomp(skdev);
4660
4661 pr_debug("%s:%s:%d skdev\n", skdev->name, __func__, __LINE__);
4662 kfree(skdev);
4663}
4664
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004665/*
4666 *****************************************************************************
4667 * BLOCK DEVICE (BDEV) GLUE
4668 *****************************************************************************
4669 */
4670
4671static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4672{
4673 struct skd_device *skdev;
4674 u64 capacity;
4675
4676 skdev = bdev->bd_disk->private_data;
4677
rchinthekindi2e44b422013-10-24 12:51:23 +01004678 pr_debug("%s:%s:%d %s: CMD[%s] getgeo device\n",
4679 skdev->name, __func__, __LINE__,
4680 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004681
4682 if (skdev->read_cap_is_valid) {
4683 capacity = get_capacity(skdev->disk);
4684 geo->heads = 64;
4685 geo->sectors = 255;
4686 geo->cylinders = (capacity) / (255 * 64);
4687
4688 return 0;
4689 }
4690 return -EIO;
4691}
4692
Dan Williams0d52c7562016-06-15 19:44:20 -07004693static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004694{
rchinthekindi2e44b422013-10-24 12:51:23 +01004695 pr_debug("%s:%s:%d add_disk\n", skdev->name, __func__, __LINE__);
Dan Williams0d52c7562016-06-15 19:44:20 -07004696 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004697 return 0;
4698}
4699
4700static const struct block_device_operations skd_blockdev_ops = {
4701 .owner = THIS_MODULE,
4702 .ioctl = skd_bdev_ioctl,
4703 .getgeo = skd_bdev_getgeo,
4704};
4705
4706
4707/*
4708 *****************************************************************************
4709 * PCIe DRIVER GLUE
4710 *****************************************************************************
4711 */
4712
Benoit Taine9baa3c32014-08-08 15:56:03 +02004713static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004714 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4715 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4716 { 0 } /* terminate list */
4717};
4718
4719MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4720
4721static char *skd_pci_info(struct skd_device *skdev, char *str)
4722{
4723 int pcie_reg;
4724
4725 strcpy(str, "PCIe (");
4726 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4727
4728 if (pcie_reg) {
4729
4730 char lwstr[6];
4731 uint16_t pcie_lstat, lspeed, lwidth;
4732
4733 pcie_reg += 0x12;
4734 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4735 lspeed = pcie_lstat & (0xF);
4736 lwidth = (pcie_lstat & 0x3F0) >> 4;
4737
4738 if (lspeed == 1)
4739 strcat(str, "2.5GT/s ");
4740 else if (lspeed == 2)
4741 strcat(str, "5.0GT/s ");
4742 else
4743 strcat(str, "<unknown> ");
4744 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4745 strcat(str, lwstr);
4746 }
4747 return str;
4748}
4749
4750static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4751{
4752 int i;
4753 int rc = 0;
4754 char pci_str[32];
4755 struct skd_device *skdev;
4756
4757 pr_info("STEC s1120 Driver(%s) version %s-b%s\n",
4758 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4759 pr_info("(skd?:??:[%s]): vendor=%04X device=%04x\n",
4760 pci_name(pdev), pdev->vendor, pdev->device);
4761
4762 rc = pci_enable_device(pdev);
4763 if (rc)
4764 return rc;
4765 rc = pci_request_regions(pdev, DRV_NAME);
4766 if (rc)
4767 goto err_out;
4768 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4769 if (!rc) {
4770 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4771
4772 pr_err("(%s): consistent DMA mask error %d\n",
4773 pci_name(pdev), rc);
4774 }
4775 } else {
4776 (rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)));
4777 if (rc) {
4778
4779 pr_err("(%s): DMA mask error %d\n",
4780 pci_name(pdev), rc);
4781 goto err_out_regions;
4782 }
4783 }
4784
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004785 if (!skd_major) {
4786 rc = register_blkdev(0, DRV_NAME);
4787 if (rc < 0)
4788 goto err_out_regions;
4789 BUG_ON(!rc);
4790 skd_major = rc;
4791 }
4792
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004793 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004794 if (skdev == NULL) {
4795 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004796 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004797 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004798
4799 skd_pci_info(skdev, pci_str);
4800 pr_info("(%s): %s 64bit\n", skd_name(skdev), pci_str);
4801
4802 pci_set_master(pdev);
4803 rc = pci_enable_pcie_error_reporting(pdev);
4804 if (rc) {
4805 pr_err(
4806 "(%s): bad enable of PCIe error reporting rc=%d\n",
4807 skd_name(skdev), rc);
4808 skdev->pcie_error_reporting_is_enabled = 0;
4809 } else
4810 skdev->pcie_error_reporting_is_enabled = 1;
4811
4812
4813 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004814
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004815 for (i = 0; i < SKD_MAX_BARS; i++) {
4816 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4817 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4818 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4819 skdev->mem_size[i]);
4820 if (!skdev->mem_map[i]) {
4821 pr_err("(%s): Unable to map adapter memory!\n",
4822 skd_name(skdev));
4823 rc = -ENODEV;
4824 goto err_out_iounmap;
4825 }
rchinthekindi2e44b422013-10-24 12:51:23 +01004826 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
4827 skdev->name, __func__, __LINE__,
4828 skdev->mem_map[i],
4829 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004830 }
4831
4832 rc = skd_acquire_irq(skdev);
4833 if (rc) {
4834 pr_err("(%s): interrupt resource error %d\n",
4835 skd_name(skdev), rc);
4836 goto err_out_iounmap;
4837 }
4838
4839 rc = skd_start_timer(skdev);
4840 if (rc)
4841 goto err_out_timer;
4842
4843 init_waitqueue_head(&skdev->waitq);
4844
4845 skd_start_device(skdev);
4846
4847 rc = wait_event_interruptible_timeout(skdev->waitq,
4848 (skdev->gendisk_on),
4849 (SKD_START_WAIT_SECONDS * HZ));
4850 if (skdev->gendisk_on > 0) {
4851 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004852 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004853 rc = 0;
4854 } else {
4855 /* we timed out, something is wrong with the device,
4856 don't add the disk structure */
4857 pr_err(
4858 "(%s): error: waiting for s1120 timed out %d!\n",
4859 skd_name(skdev), rc);
4860 /* in case of no error; we timeout with ENXIO */
4861 if (!rc)
4862 rc = -ENXIO;
4863 goto err_out_timer;
4864 }
4865
4866
4867#ifdef SKD_VMK_POLL_HANDLER
4868 if (skdev->irq_type == SKD_IRQ_MSIX) {
4869 /* MSIX completion handler is being used for coredump */
4870 vmklnx_scsi_register_poll_handler(skdev->scsi_host,
4871 skdev->msix_entries[5].vector,
4872 skd_comp_q, skdev);
4873 } else {
4874 vmklnx_scsi_register_poll_handler(skdev->scsi_host,
4875 skdev->pdev->irq, skd_isr,
4876 skdev);
4877 }
4878#endif /* SKD_VMK_POLL_HANDLER */
4879
4880 return rc;
4881
4882err_out_timer:
4883 skd_stop_device(skdev);
4884 skd_release_irq(skdev);
4885
4886err_out_iounmap:
4887 for (i = 0; i < SKD_MAX_BARS; i++)
4888 if (skdev->mem_map[i])
4889 iounmap(skdev->mem_map[i]);
4890
4891 if (skdev->pcie_error_reporting_is_enabled)
4892 pci_disable_pcie_error_reporting(pdev);
4893
4894 skd_destruct(skdev);
4895
4896err_out_regions:
4897 pci_release_regions(pdev);
4898
4899err_out:
4900 pci_disable_device(pdev);
4901 pci_set_drvdata(pdev, NULL);
4902 return rc;
4903}
4904
4905static void skd_pci_remove(struct pci_dev *pdev)
4906{
4907 int i;
4908 struct skd_device *skdev;
4909
4910 skdev = pci_get_drvdata(pdev);
4911 if (!skdev) {
4912 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4913 return;
4914 }
4915 skd_stop_device(skdev);
4916 skd_release_irq(skdev);
4917
4918 for (i = 0; i < SKD_MAX_BARS; i++)
4919 if (skdev->mem_map[i])
4920 iounmap((u32 *)skdev->mem_map[i]);
4921
4922 if (skdev->pcie_error_reporting_is_enabled)
4923 pci_disable_pcie_error_reporting(pdev);
4924
4925 skd_destruct(skdev);
4926
4927 pci_release_regions(pdev);
4928 pci_disable_device(pdev);
4929 pci_set_drvdata(pdev, NULL);
4930
4931 return;
4932}
4933
4934static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4935{
4936 int i;
4937 struct skd_device *skdev;
4938
4939 skdev = pci_get_drvdata(pdev);
4940 if (!skdev) {
4941 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4942 return -EIO;
4943 }
4944
4945 skd_stop_device(skdev);
4946
4947 skd_release_irq(skdev);
4948
4949 for (i = 0; i < SKD_MAX_BARS; i++)
4950 if (skdev->mem_map[i])
4951 iounmap((u32 *)skdev->mem_map[i]);
4952
4953 if (skdev->pcie_error_reporting_is_enabled)
4954 pci_disable_pcie_error_reporting(pdev);
4955
4956 pci_release_regions(pdev);
4957 pci_save_state(pdev);
4958 pci_disable_device(pdev);
4959 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4960 return 0;
4961}
4962
4963static int skd_pci_resume(struct pci_dev *pdev)
4964{
4965 int i;
4966 int rc = 0;
4967 struct skd_device *skdev;
4968
4969 skdev = pci_get_drvdata(pdev);
4970 if (!skdev) {
4971 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4972 return -1;
4973 }
4974
4975 pci_set_power_state(pdev, PCI_D0);
4976 pci_enable_wake(pdev, PCI_D0, 0);
4977 pci_restore_state(pdev);
4978
4979 rc = pci_enable_device(pdev);
4980 if (rc)
4981 return rc;
4982 rc = pci_request_regions(pdev, DRV_NAME);
4983 if (rc)
4984 goto err_out;
4985 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4986 if (!rc) {
4987 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4988
4989 pr_err("(%s): consistent DMA mask error %d\n",
4990 pci_name(pdev), rc);
4991 }
4992 } else {
4993 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4994 if (rc) {
4995
4996 pr_err("(%s): DMA mask error %d\n",
4997 pci_name(pdev), rc);
4998 goto err_out_regions;
4999 }
5000 }
5001
5002 pci_set_master(pdev);
5003 rc = pci_enable_pcie_error_reporting(pdev);
5004 if (rc) {
5005 pr_err("(%s): bad enable of PCIe error reporting rc=%d\n",
5006 skdev->name, rc);
5007 skdev->pcie_error_reporting_is_enabled = 0;
5008 } else
5009 skdev->pcie_error_reporting_is_enabled = 1;
5010
5011 for (i = 0; i < SKD_MAX_BARS; i++) {
5012
5013 skdev->mem_phys[i] = pci_resource_start(pdev, i);
5014 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
5015 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
5016 skdev->mem_size[i]);
5017 if (!skdev->mem_map[i]) {
5018 pr_err("(%s): Unable to map adapter memory!\n",
5019 skd_name(skdev));
5020 rc = -ENODEV;
5021 goto err_out_iounmap;
5022 }
rchinthekindi2e44b422013-10-24 12:51:23 +01005023 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
5024 skdev->name, __func__, __LINE__,
5025 skdev->mem_map[i],
5026 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005027 }
5028 rc = skd_acquire_irq(skdev);
5029 if (rc) {
5030
5031 pr_err("(%s): interrupt resource error %d\n",
5032 pci_name(pdev), rc);
5033 goto err_out_iounmap;
5034 }
5035
5036 rc = skd_start_timer(skdev);
5037 if (rc)
5038 goto err_out_timer;
5039
5040 init_waitqueue_head(&skdev->waitq);
5041
5042 skd_start_device(skdev);
5043
5044 return rc;
5045
5046err_out_timer:
5047 skd_stop_device(skdev);
5048 skd_release_irq(skdev);
5049
5050err_out_iounmap:
5051 for (i = 0; i < SKD_MAX_BARS; i++)
5052 if (skdev->mem_map[i])
5053 iounmap(skdev->mem_map[i]);
5054
5055 if (skdev->pcie_error_reporting_is_enabled)
5056 pci_disable_pcie_error_reporting(pdev);
5057
5058err_out_regions:
5059 pci_release_regions(pdev);
5060
5061err_out:
5062 pci_disable_device(pdev);
5063 return rc;
5064}
5065
5066static void skd_pci_shutdown(struct pci_dev *pdev)
5067{
5068 struct skd_device *skdev;
5069
5070 pr_err("skd_pci_shutdown called\n");
5071
5072 skdev = pci_get_drvdata(pdev);
5073 if (!skdev) {
5074 pr_err("%s: no device data for PCI\n", pci_name(pdev));
5075 return;
5076 }
5077
5078 pr_err("%s: calling stop\n", skd_name(skdev));
5079 skd_stop_device(skdev);
5080}
5081
5082static struct pci_driver skd_driver = {
5083 .name = DRV_NAME,
5084 .id_table = skd_pci_tbl,
5085 .probe = skd_pci_probe,
5086 .remove = skd_pci_remove,
5087 .suspend = skd_pci_suspend,
5088 .resume = skd_pci_resume,
5089 .shutdown = skd_pci_shutdown,
5090};
5091
5092/*
5093 *****************************************************************************
5094 * LOGGING SUPPORT
5095 *****************************************************************************
5096 */
5097
5098static const char *skd_name(struct skd_device *skdev)
5099{
5100 memset(skdev->id_str, 0, sizeof(skdev->id_str));
5101
5102 if (skdev->inquiry_is_valid)
5103 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:%s:[%s]",
5104 skdev->name, skdev->inq_serial_num,
5105 pci_name(skdev->pdev));
5106 else
5107 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:??:[%s]",
5108 skdev->name, pci_name(skdev->pdev));
5109
5110 return skdev->id_str;
5111}
5112
5113const char *skd_drive_state_to_str(int state)
5114{
5115 switch (state) {
5116 case FIT_SR_DRIVE_OFFLINE:
5117 return "OFFLINE";
5118 case FIT_SR_DRIVE_INIT:
5119 return "INIT";
5120 case FIT_SR_DRIVE_ONLINE:
5121 return "ONLINE";
5122 case FIT_SR_DRIVE_BUSY:
5123 return "BUSY";
5124 case FIT_SR_DRIVE_FAULT:
5125 return "FAULT";
5126 case FIT_SR_DRIVE_DEGRADED:
5127 return "DEGRADED";
5128 case FIT_SR_PCIE_LINK_DOWN:
5129 return "INK_DOWN";
5130 case FIT_SR_DRIVE_SOFT_RESET:
5131 return "SOFT_RESET";
5132 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
5133 return "NEED_FW";
5134 case FIT_SR_DRIVE_INIT_FAULT:
5135 return "INIT_FAULT";
5136 case FIT_SR_DRIVE_BUSY_SANITIZE:
5137 return "BUSY_SANITIZE";
5138 case FIT_SR_DRIVE_BUSY_ERASE:
5139 return "BUSY_ERASE";
5140 case FIT_SR_DRIVE_FW_BOOTING:
5141 return "FW_BOOTING";
5142 default:
5143 return "???";
5144 }
5145}
5146
5147const char *skd_skdev_state_to_str(enum skd_drvr_state state)
5148{
5149 switch (state) {
5150 case SKD_DRVR_STATE_LOAD:
5151 return "LOAD";
5152 case SKD_DRVR_STATE_IDLE:
5153 return "IDLE";
5154 case SKD_DRVR_STATE_BUSY:
5155 return "BUSY";
5156 case SKD_DRVR_STATE_STARTING:
5157 return "STARTING";
5158 case SKD_DRVR_STATE_ONLINE:
5159 return "ONLINE";
5160 case SKD_DRVR_STATE_PAUSING:
5161 return "PAUSING";
5162 case SKD_DRVR_STATE_PAUSED:
5163 return "PAUSED";
5164 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
5165 return "DRAINING_TIMEOUT";
5166 case SKD_DRVR_STATE_RESTARTING:
5167 return "RESTARTING";
5168 case SKD_DRVR_STATE_RESUMING:
5169 return "RESUMING";
5170 case SKD_DRVR_STATE_STOPPING:
5171 return "STOPPING";
5172 case SKD_DRVR_STATE_SYNCING:
5173 return "SYNCING";
5174 case SKD_DRVR_STATE_FAULT:
5175 return "FAULT";
5176 case SKD_DRVR_STATE_DISAPPEARED:
5177 return "DISAPPEARED";
5178 case SKD_DRVR_STATE_BUSY_ERASE:
5179 return "BUSY_ERASE";
5180 case SKD_DRVR_STATE_BUSY_SANITIZE:
5181 return "BUSY_SANITIZE";
5182 case SKD_DRVR_STATE_BUSY_IMMINENT:
5183 return "BUSY_IMMINENT";
5184 case SKD_DRVR_STATE_WAIT_BOOT:
5185 return "WAIT_BOOT";
5186
5187 default:
5188 return "???";
5189 }
5190}
5191
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05305192static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005193{
5194 switch (state) {
5195 case SKD_MSG_STATE_IDLE:
5196 return "IDLE";
5197 case SKD_MSG_STATE_BUSY:
5198 return "BUSY";
5199 default:
5200 return "???";
5201 }
5202}
5203
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05305204static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005205{
5206 switch (state) {
5207 case SKD_REQ_STATE_IDLE:
5208 return "IDLE";
5209 case SKD_REQ_STATE_SETUP:
5210 return "SETUP";
5211 case SKD_REQ_STATE_BUSY:
5212 return "BUSY";
5213 case SKD_REQ_STATE_COMPLETED:
5214 return "COMPLETED";
5215 case SKD_REQ_STATE_TIMEOUT:
5216 return "TIMEOUT";
5217 case SKD_REQ_STATE_ABORTED:
5218 return "ABORTED";
5219 default:
5220 return "???";
5221 }
5222}
5223
5224static void skd_log_skdev(struct skd_device *skdev, const char *event)
5225{
rchinthekindi2e44b422013-10-24 12:51:23 +01005226 pr_debug("%s:%s:%d (%s) skdev=%p event='%s'\n",
5227 skdev->name, __func__, __LINE__, skdev->name, skdev, event);
5228 pr_debug("%s:%s:%d drive_state=%s(%d) driver_state=%s(%d)\n",
5229 skdev->name, __func__, __LINE__,
5230 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
5231 skd_skdev_state_to_str(skdev->state), skdev->state);
5232 pr_debug("%s:%s:%d busy=%d limit=%d dev=%d lowat=%d\n",
5233 skdev->name, __func__, __LINE__,
5234 skdev->in_flight, skdev->cur_max_queue_depth,
5235 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
5236 pr_debug("%s:%s:%d timestamp=0x%x cycle=%d cycle_ix=%d\n",
5237 skdev->name, __func__, __LINE__,
5238 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005239}
5240
5241static void skd_log_skmsg(struct skd_device *skdev,
5242 struct skd_fitmsg_context *skmsg, const char *event)
5243{
rchinthekindi2e44b422013-10-24 12:51:23 +01005244 pr_debug("%s:%s:%d (%s) skmsg=%p event='%s'\n",
5245 skdev->name, __func__, __LINE__, skdev->name, skmsg, event);
5246 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x length=%d\n",
5247 skdev->name, __func__, __LINE__,
5248 skd_skmsg_state_to_str(skmsg->state), skmsg->state,
5249 skmsg->id, skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005250}
5251
5252static void skd_log_skreq(struct skd_device *skdev,
5253 struct skd_request_context *skreq, const char *event)
5254{
rchinthekindi2e44b422013-10-24 12:51:23 +01005255 pr_debug("%s:%s:%d (%s) skreq=%p event='%s'\n",
5256 skdev->name, __func__, __LINE__, skdev->name, skreq, event);
5257 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
5258 skdev->name, __func__, __LINE__,
5259 skd_skreq_state_to_str(skreq->state), skreq->state,
5260 skreq->id, skreq->fitmsg_id);
5261 pr_debug("%s:%s:%d timo=0x%x sg_dir=%d n_sg=%d\n",
5262 skdev->name, __func__, __LINE__,
5263 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005264
Jens Axboefcd37eb2013-11-01 10:14:56 -06005265 if (skreq->req != NULL) {
5266 struct request *req = skreq->req;
5267 u32 lba = (u32)blk_rq_pos(req);
5268 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005269
Jens Axboefcd37eb2013-11-01 10:14:56 -06005270 pr_debug("%s:%s:%d "
5271 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
5272 skdev->name, __func__, __LINE__,
5273 req, lba, lba, count, count,
5274 (int)rq_data_dir(req));
5275 } else
5276 pr_debug("%s:%s:%d req=NULL\n",
5277 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005278}
5279
5280/*
5281 *****************************************************************************
5282 * MODULE GLUE
5283 *****************************************************************************
5284 */
5285
5286static int __init skd_init(void)
5287{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005288 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
5289
5290 switch (skd_isr_type) {
5291 case SKD_IRQ_LEGACY:
5292 case SKD_IRQ_MSI:
5293 case SKD_IRQ_MSIX:
5294 break;
5295 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005296 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005297 skd_isr_type, SKD_IRQ_DEFAULT);
5298 skd_isr_type = SKD_IRQ_DEFAULT;
5299 }
5300
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005301 if (skd_max_queue_depth < 1 ||
5302 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
5303 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005304 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
5305 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
5306 }
5307
5308 if (skd_max_req_per_msg < 1 || skd_max_req_per_msg > 14) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005309 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005310 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
5311 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
5312 }
5313
5314 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005315 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005316 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
5317 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5318 }
5319
5320 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005321 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005322 skd_dbg_level, 0);
5323 skd_dbg_level = 0;
5324 }
5325
5326 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005327 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005328 skd_isr_comp_limit, 0);
5329 skd_isr_comp_limit = 0;
5330 }
5331
5332 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005333 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005334 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5335 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5336 }
5337
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005338 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005339}
5340
5341static void __exit skd_exit(void)
5342{
5343 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5344
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005345 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005346
5347 if (skd_major)
5348 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005349}
5350
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005351module_init(skd_init);
5352module_exit(skd_exit);