blob: 702f543a4a64ad08413f8cb4d48e92100b1a4c34 [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
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600273 struct skd_msix_entry *msix_entries;
274
275 struct pci_dev *pdev;
276 int pcie_error_reporting_is_enabled;
277
278 spinlock_t lock;
279 struct gendisk *disk;
280 struct request_queue *queue;
281 struct device *class_dev;
282 int gendisk_on;
283 int sync_done;
284
285 atomic_t device_count;
286 u32 devno;
287 u32 major;
288 char name[32];
289 char isr_name[30];
290
291 enum skd_drvr_state state;
292 u32 drive_state;
293
294 u32 in_flight;
295 u32 cur_max_queue_depth;
296 u32 queue_low_water_mark;
297 u32 dev_max_queue_depth;
298
299 u32 num_fitmsg_context;
300 u32 num_req_context;
301
302 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
303 u32 timeout_stamp;
304 struct skd_fitmsg_context *skmsg_free_list;
305 struct skd_fitmsg_context *skmsg_table;
306
307 struct skd_request_context *skreq_free_list;
308 struct skd_request_context *skreq_table;
309
310 struct skd_special_context *skspcl_free_list;
311 struct skd_special_context *skspcl_table;
312
313 struct skd_special_context internal_skspcl;
314 u32 read_cap_blocksize;
315 u32 read_cap_last_lba;
316 int read_cap_is_valid;
317 int inquiry_is_valid;
318 u8 inq_serial_num[13]; /*12 chars plus null term */
319 u8 id_str[80]; /* holds a composite name (pci + sernum) */
320
321 u8 skcomp_cycle;
322 u32 skcomp_ix;
323 struct fit_completion_entry_v1 *skcomp_table;
324 struct fit_comp_error_info *skerr_table;
325 dma_addr_t cq_dma_address;
326
327 wait_queue_head_t waitq;
328
329 struct timer_list timer;
330 u32 timer_countdown;
331 u32 timer_substate;
332
333 int n_special;
334 int sgs_per_request;
335 u32 last_mtd;
336
337 u32 proto_ver;
338
339 int dbg_level;
340 u32 connect_time_stamp;
341 int connect_retries;
342#define SKD_MAX_CONNECT_RETRIES 16
343 u32 drive_jiffies;
344
345 u32 timo_slot;
346
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400347
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600348 struct work_struct completion_worker;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600349};
350
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600351#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
352#define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
353#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
354
355static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
356{
357 u32 val;
358
359 if (likely(skdev->dbg_level < 2))
360 return readl(skdev->mem_map[1] + offset);
361 else {
362 barrier();
363 val = readl(skdev->mem_map[1] + offset);
364 barrier();
rchinthekindi2e44b422013-10-24 12:51:23 +0100365 pr_debug("%s:%s:%d offset %x = %x\n",
366 skdev->name, __func__, __LINE__, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600367 return val;
368 }
369
370}
371
372static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
373 u32 offset)
374{
375 if (likely(skdev->dbg_level < 2)) {
376 writel(val, skdev->mem_map[1] + offset);
377 barrier();
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600378 } else {
379 barrier();
380 writel(val, skdev->mem_map[1] + offset);
381 barrier();
rchinthekindi2e44b422013-10-24 12:51:23 +0100382 pr_debug("%s:%s:%d offset %x = %x\n",
383 skdev->name, __func__, __LINE__, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600384 }
385}
386
387static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
388 u32 offset)
389{
390 if (likely(skdev->dbg_level < 2)) {
391 writeq(val, skdev->mem_map[1] + offset);
392 barrier();
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600393 } else {
394 barrier();
395 writeq(val, skdev->mem_map[1] + offset);
396 barrier();
rchinthekindi2e44b422013-10-24 12:51:23 +0100397 pr_debug("%s:%s:%d offset %x = %016llx\n",
398 skdev->name, __func__, __LINE__, offset, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600399 }
400}
401
402
403#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
404static int skd_isr_type = SKD_IRQ_DEFAULT;
405
406module_param(skd_isr_type, int, 0444);
407MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
408 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
409
410#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
411static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
412
413module_param(skd_max_req_per_msg, int, 0444);
414MODULE_PARM_DESC(skd_max_req_per_msg,
415 "Maximum SCSI requests packed in a single message."
416 " (1-14, default==1)");
417
418#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
419#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
420static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
421
422module_param(skd_max_queue_depth, int, 0444);
423MODULE_PARM_DESC(skd_max_queue_depth,
424 "Maximum SCSI requests issued to s1120."
425 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
426
427static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
428module_param(skd_sgs_per_request, int, 0444);
429MODULE_PARM_DESC(skd_sgs_per_request,
430 "Maximum SG elements per block request."
431 " (1-4096, default==256)");
432
433static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
434module_param(skd_max_pass_thru, int, 0444);
435MODULE_PARM_DESC(skd_max_pass_thru,
436 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
437
438module_param(skd_dbg_level, int, 0444);
439MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
440
441module_param(skd_isr_comp_limit, int, 0444);
442MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
443
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600444/* Major device number dynamically assigned. */
445static u32 skd_major;
446
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600447static void skd_destruct(struct skd_device *skdev);
448static const struct block_device_operations skd_blockdev_ops;
449static void skd_send_fitmsg(struct skd_device *skdev,
450 struct skd_fitmsg_context *skmsg);
451static void skd_send_special_fitmsg(struct skd_device *skdev,
452 struct skd_special_context *skspcl);
453static void skd_request_fn(struct request_queue *rq);
454static void skd_end_request(struct skd_device *skdev,
455 struct skd_request_context *skreq, int error);
456static int skd_preop_sg_list(struct skd_device *skdev,
457 struct skd_request_context *skreq);
458static void skd_postop_sg_list(struct skd_device *skdev,
459 struct skd_request_context *skreq);
460
461static void skd_restart_device(struct skd_device *skdev);
462static int skd_quiesce_dev(struct skd_device *skdev);
463static int skd_unquiesce_dev(struct skd_device *skdev);
464static void skd_release_special(struct skd_device *skdev,
465 struct skd_special_context *skspcl);
466static void skd_disable_interrupts(struct skd_device *skdev);
467static void skd_isr_fwstate(struct skd_device *skdev);
468static void skd_recover_requests(struct skd_device *skdev, int requeue);
469static void skd_soft_reset(struct skd_device *skdev);
470
471static const char *skd_name(struct skd_device *skdev);
472const char *skd_drive_state_to_str(int state);
473const char *skd_skdev_state_to_str(enum skd_drvr_state state);
474static void skd_log_skdev(struct skd_device *skdev, const char *event);
475static void skd_log_skmsg(struct skd_device *skdev,
476 struct skd_fitmsg_context *skmsg, const char *event);
477static void skd_log_skreq(struct skd_device *skdev,
478 struct skd_request_context *skreq, const char *event);
479
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600480/*
481 *****************************************************************************
482 * READ/WRITE REQUESTS
483 *****************************************************************************
484 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600485static void skd_fail_all_pending(struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600486{
487 struct request_queue *q = skdev->queue;
488 struct request *req;
489
490 for (;; ) {
491 req = blk_peek_request(q);
492 if (req == NULL)
493 break;
494 blk_start_request(req);
495 __blk_end_request_all(req, -EIO);
496 }
497}
498
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600499static void
500skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
501 int data_dir, unsigned lba,
502 unsigned count)
503{
504 if (data_dir == READ)
505 scsi_req->cdb[0] = 0x28;
506 else
507 scsi_req->cdb[0] = 0x2a;
508
509 scsi_req->cdb[1] = 0;
510 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
511 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
512 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
513 scsi_req->cdb[5] = (lba & 0xff);
514 scsi_req->cdb[6] = 0;
515 scsi_req->cdb[7] = (count & 0xff00) >> 8;
516 scsi_req->cdb[8] = count & 0xff;
517 scsi_req->cdb[9] = 0;
518}
519
520static void
521skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400522 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600523{
524 skreq->flush_cmd = 1;
525
526 scsi_req->cdb[0] = 0x35;
527 scsi_req->cdb[1] = 0;
528 scsi_req->cdb[2] = 0;
529 scsi_req->cdb[3] = 0;
530 scsi_req->cdb[4] = 0;
531 scsi_req->cdb[5] = 0;
532 scsi_req->cdb[6] = 0;
533 scsi_req->cdb[7] = 0;
534 scsi_req->cdb[8] = 0;
535 scsi_req->cdb[9] = 0;
536}
537
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600538static void skd_request_fn_not_online(struct request_queue *q);
539
540static void skd_request_fn(struct request_queue *q)
541{
542 struct skd_device *skdev = q->queuedata;
543 struct skd_fitmsg_context *skmsg = NULL;
544 struct fit_msg_hdr *fmh = NULL;
545 struct skd_request_context *skreq;
546 struct request *req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600547 struct skd_scsi_request *scsi_req;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600548 unsigned long io_flags;
549 int error;
550 u32 lba;
551 u32 count;
552 int data_dir;
553 u32 be_lba;
554 u32 be_count;
555 u64 be_dmaa;
556 u64 cmdctxt;
557 u32 timo_slot;
558 void *cmd_ptr;
559 int flush, fua;
560
561 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
562 skd_request_fn_not_online(q);
563 return;
564 }
565
Jens Axboe6a5ec652013-11-01 10:38:45 -0600566 if (blk_queue_stopped(skdev->queue)) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600567 if (skdev->skmsg_free_list == NULL ||
568 skdev->skreq_free_list == NULL ||
569 skdev->in_flight >= skdev->queue_low_water_mark)
570 /* There is still some kind of shortage */
571 return;
572
Jens Axboe6a5ec652013-11-01 10:38:45 -0600573 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600574 }
575
576 /*
577 * Stop conditions:
578 * - There are no more native requests
579 * - There are already the maximum number of requests in progress
580 * - There are no more skd_request_context entries
581 * - There are no more FIT msg buffers
582 */
583 for (;; ) {
584
585 flush = fua = 0;
586
Jens Axboefcd37eb2013-11-01 10:14:56 -0600587 req = blk_peek_request(q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600588
Jens Axboefcd37eb2013-11-01 10:14:56 -0600589 /* Are there any native requests to start? */
590 if (req == NULL)
591 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600592
Jens Axboefcd37eb2013-11-01 10:14:56 -0600593 lba = (u32)blk_rq_pos(req);
594 count = blk_rq_sectors(req);
595 data_dir = rq_data_dir(req);
596 io_flags = req->cmd_flags;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600597
Mike Christie3a5e02c2016-06-05 14:32:23 -0500598 if (req_op(req) == REQ_OP_FLUSH)
Jens Axboefcd37eb2013-11-01 10:14:56 -0600599 flush++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600600
Jens Axboefcd37eb2013-11-01 10:14:56 -0600601 if (io_flags & REQ_FUA)
602 fua++;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600603
Jens Axboefcd37eb2013-11-01 10:14:56 -0600604 pr_debug("%s:%s:%d new req=%p lba=%u(0x%x) "
605 "count=%u(0x%x) dir=%d\n",
606 skdev->name, __func__, __LINE__,
607 req, lba, lba, count, count, data_dir);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600608
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400609 /* At this point we know there is a request */
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600610
611 /* Are too many requets already in progress? */
612 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100613 pr_debug("%s:%s:%d qdepth %d, limit %d\n",
614 skdev->name, __func__, __LINE__,
615 skdev->in_flight, skdev->cur_max_queue_depth);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600616 break;
617 }
618
619 /* Is a skd_request_context available? */
620 skreq = skdev->skreq_free_list;
621 if (skreq == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100622 pr_debug("%s:%s:%d Out of req=%p\n",
623 skdev->name, __func__, __LINE__, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600624 break;
625 }
626 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
627 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
628
629 /* Now we check to see if we can get a fit msg */
630 if (skmsg == NULL) {
631 if (skdev->skmsg_free_list == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100632 pr_debug("%s:%s:%d Out of msg\n",
633 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600634 break;
635 }
636 }
637
638 skreq->flush_cmd = 0;
639 skreq->n_sg = 0;
640 skreq->sg_byte_count = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600641
642 /*
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400643 * OK to now dequeue request from q.
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600644 *
645 * At this point we are comitted to either start or reject
646 * the native request. Note that skd_request_context is
647 * available but is still at the head of the free list.
648 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600649 blk_start_request(req);
650 skreq->req = req;
651 skreq->fitmsg_id = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600652
653 /* Either a FIT msg is in progress or we have to start one. */
654 if (skmsg == NULL) {
655 /* Are there any FIT msg buffers available? */
656 skmsg = skdev->skmsg_free_list;
657 if (skmsg == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100658 pr_debug("%s:%s:%d Out of msg skdev=%p\n",
659 skdev->name, __func__, __LINE__,
660 skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600661 break;
662 }
663 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
664 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
665
666 skdev->skmsg_free_list = skmsg->next;
667
668 skmsg->state = SKD_MSG_STATE_BUSY;
669 skmsg->id += SKD_ID_INCR;
670
671 /* Initialize the FIT msg header */
672 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
673 memset(fmh, 0, sizeof(*fmh));
674 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
675 skmsg->length = sizeof(*fmh);
676 }
677
678 skreq->fitmsg_id = skmsg->id;
679
680 /*
681 * Note that a FIT msg may have just been started
682 * but contains no SoFIT requests yet.
683 */
684
685 /*
686 * Transcode the request, checking as we go. The outcome of
687 * the transcoding is represented by the error variable.
688 */
689 cmd_ptr = &skmsg->msg_buf[skmsg->length];
690 memset(cmd_ptr, 0, 32);
691
692 be_lba = cpu_to_be32(lba);
693 be_count = cpu_to_be32(count);
694 be_dmaa = cpu_to_be64((u64)skreq->sksg_dma_address);
695 cmdctxt = skreq->id + SKD_ID_INCR;
696
697 scsi_req = cmd_ptr;
698 scsi_req->hdr.tag = cmdctxt;
699 scsi_req->hdr.sg_list_dma_address = be_dmaa;
700
701 if (data_dir == READ)
702 skreq->sg_data_dir = SKD_DATA_DIR_CARD_TO_HOST;
703 else
704 skreq->sg_data_dir = SKD_DATA_DIR_HOST_TO_CARD;
705
Jeff Moyer49bdedb2016-04-25 19:12:38 -0600706 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600707 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
708 SKD_ASSERT(skreq->flush_cmd == 1);
709
710 } else {
711 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
712 }
713
714 if (fua)
715 scsi_req->cdb[1] |= SKD_FUA_NV;
716
Jens Axboefcd37eb2013-11-01 10:14:56 -0600717 if (!req->bio)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600718 goto skip_sg;
719
720 error = skd_preop_sg_list(skdev, skreq);
721
722 if (error != 0) {
723 /*
724 * Complete the native request with error.
725 * Note that the request context is still at the
726 * head of the free list, and that the SoFIT request
727 * was encoded into the FIT msg buffer but the FIT
728 * msg length has not been updated. In short, the
729 * only resource that has been allocated but might
730 * not be used is that the FIT msg could be empty.
731 */
rchinthekindi2e44b422013-10-24 12:51:23 +0100732 pr_debug("%s:%s:%d error Out\n",
733 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600734 skd_end_request(skdev, skreq, error);
735 continue;
736 }
737
738skip_sg:
739 scsi_req->hdr.sg_list_len_bytes =
740 cpu_to_be32(skreq->sg_byte_count);
741
742 /* Complete resource allocations. */
743 skdev->skreq_free_list = skreq->next;
744 skreq->state = SKD_REQ_STATE_BUSY;
745 skreq->id += SKD_ID_INCR;
746
747 skmsg->length += sizeof(struct skd_scsi_request);
748 fmh->num_protocol_cmds_coalesced++;
749
750 /*
751 * Update the active request counts.
752 * Capture the timeout timestamp.
753 */
754 skreq->timeout_stamp = skdev->timeout_stamp;
755 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
756 skdev->timeout_slot[timo_slot]++;
757 skdev->in_flight++;
rchinthekindi2e44b422013-10-24 12:51:23 +0100758 pr_debug("%s:%s:%d req=0x%x busy=%d\n",
759 skdev->name, __func__, __LINE__,
760 skreq->id, skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600761
762 /*
763 * If the FIT msg buffer is full send it.
764 */
765 if (skmsg->length >= SKD_N_FITMSG_BYTES ||
766 fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
767 skd_send_fitmsg(skdev, skmsg);
768 skmsg = NULL;
769 fmh = NULL;
770 }
771 }
772
773 /*
774 * Is a FIT msg in progress? If it is empty put the buffer back
775 * on the free list. If it is non-empty send what we got.
776 * This minimizes latency when there are fewer requests than
777 * what fits in a FIT msg.
778 */
779 if (skmsg != NULL) {
780 /* Bigger than just a FIT msg header? */
781 if (skmsg->length > sizeof(struct fit_msg_hdr)) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100782 pr_debug("%s:%s:%d sending msg=%p, len %d\n",
783 skdev->name, __func__, __LINE__,
784 skmsg, skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600785 skd_send_fitmsg(skdev, skmsg);
786 } else {
787 /*
788 * The FIT msg is empty. It means we got started
789 * on the msg, but the requests were rejected.
790 */
791 skmsg->state = SKD_MSG_STATE_IDLE;
792 skmsg->id += SKD_ID_INCR;
793 skmsg->next = skdev->skmsg_free_list;
794 skdev->skmsg_free_list = skmsg;
795 }
796 skmsg = NULL;
797 fmh = NULL;
798 }
799
800 /*
801 * If req is non-NULL it means there is something to do but
802 * we are out of a resource.
803 */
Jens Axboefcd37eb2013-11-01 10:14:56 -0600804 if (req)
Jens Axboe6a5ec652013-11-01 10:38:45 -0600805 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600806}
807
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400808static void skd_end_request(struct skd_device *skdev,
809 struct skd_request_context *skreq, int error)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600810{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600811 if (unlikely(error)) {
812 struct request *req = skreq->req;
813 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
814 u32 lba = (u32)blk_rq_pos(req);
815 u32 count = blk_rq_sectors(req);
816
817 pr_err("(%s): Error cmd=%s sect=%u count=%u id=0x%x\n",
818 skd_name(skdev), cmd, lba, count, skreq->id);
819 } else
rchinthekindi2e44b422013-10-24 12:51:23 +0100820 pr_debug("%s:%s:%d id=0x%x error=%d\n",
821 skdev->name, __func__, __LINE__, skreq->id, error);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600822
823 __blk_end_request_all(skreq->req, error);
824}
825
Jens Axboefcd37eb2013-11-01 10:14:56 -0600826static int skd_preop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400827 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600828{
829 struct request *req = skreq->req;
830 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
831 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
832 struct scatterlist *sg = &skreq->sg[0];
833 int n_sg;
834 int i;
835
836 skreq->sg_byte_count = 0;
837
838 /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
839 skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */
840
841 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
842 if (n_sg <= 0)
843 return -EINVAL;
844
845 /*
846 * Map scatterlist to PCI bus addresses.
847 * Note PCI might change the number of entries.
848 */
849 n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
850 if (n_sg <= 0)
851 return -EINVAL;
852
853 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
854
855 skreq->n_sg = n_sg;
856
857 for (i = 0; i < n_sg; i++) {
858 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
859 u32 cnt = sg_dma_len(&sg[i]);
860 uint64_t dma_addr = sg_dma_address(&sg[i]);
861
862 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
863 sgd->byte_count = cnt;
864 skreq->sg_byte_count += cnt;
865 sgd->host_side_addr = dma_addr;
866 sgd->dev_side_addr = 0;
867 }
868
869 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
870 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
871
872 if (unlikely(skdev->dbg_level > 1)) {
rchinthekindi2e44b422013-10-24 12:51:23 +0100873 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
874 skdev->name, __func__, __LINE__,
875 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600876 for (i = 0; i < n_sg; i++) {
877 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
rchinthekindi2e44b422013-10-24 12:51:23 +0100878 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
879 "addr=0x%llx next=0x%llx\n",
880 skdev->name, __func__, __LINE__,
881 i, sgd->byte_count, sgd->control,
882 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600883 }
884 }
885
886 return 0;
887}
888
Jens Axboefcd37eb2013-11-01 10:14:56 -0600889static void skd_postop_sg_list(struct skd_device *skdev,
Mike Snitzer38d4a1b2013-11-01 15:05:10 -0400890 struct skd_request_context *skreq)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600891{
892 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
893 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
894
895 /*
896 * restore the next ptr for next IO request so we
897 * don't have to set it every time.
898 */
899 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
900 skreq->sksg_dma_address +
901 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
902 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
903}
904
Akhil Bhansalie67f86b2013-10-15 14:19:07 -0600905static void skd_request_fn_not_online(struct request_queue *q)
906{
907 struct skd_device *skdev = q->queuedata;
908 int error;
909
910 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
911
912 skd_log_skdev(skdev, "req_not_online");
913 switch (skdev->state) {
914 case SKD_DRVR_STATE_PAUSING:
915 case SKD_DRVR_STATE_PAUSED:
916 case SKD_DRVR_STATE_STARTING:
917 case SKD_DRVR_STATE_RESTARTING:
918 case SKD_DRVR_STATE_WAIT_BOOT:
919 /* In case of starting, we haven't started the queue,
920 * so we can't get here... but requests are
921 * possibly hanging out waiting for us because we
922 * reported the dev/skd0 already. They'll wait
923 * forever if connect doesn't complete.
924 * What to do??? delay dev/skd0 ??
925 */
926 case SKD_DRVR_STATE_BUSY:
927 case SKD_DRVR_STATE_BUSY_IMMINENT:
928 case SKD_DRVR_STATE_BUSY_ERASE:
929 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
930 return;
931
932 case SKD_DRVR_STATE_BUSY_SANITIZE:
933 case SKD_DRVR_STATE_STOPPING:
934 case SKD_DRVR_STATE_SYNCING:
935 case SKD_DRVR_STATE_FAULT:
936 case SKD_DRVR_STATE_DISAPPEARED:
937 default:
938 error = -EIO;
939 break;
940 }
941
942 /* If we get here, terminate all pending block requeusts
943 * with EIO and any scsi pass thru with appropriate sense
944 */
945
946 skd_fail_all_pending(skdev);
947}
948
949/*
950 *****************************************************************************
951 * TIMER
952 *****************************************************************************
953 */
954
955static void skd_timer_tick_not_online(struct skd_device *skdev);
956
957static void skd_timer_tick(ulong arg)
958{
959 struct skd_device *skdev = (struct skd_device *)arg;
960
961 u32 timo_slot;
962 u32 overdue_timestamp;
963 unsigned long reqflags;
964 u32 state;
965
966 if (skdev->state == SKD_DRVR_STATE_FAULT)
967 /* The driver has declared fault, and we want it to
968 * stay that way until driver is reloaded.
969 */
970 return;
971
972 spin_lock_irqsave(&skdev->lock, reqflags);
973
974 state = SKD_READL(skdev, FIT_STATUS);
975 state &= FIT_SR_DRIVE_STATE_MASK;
976 if (state != skdev->drive_state)
977 skd_isr_fwstate(skdev);
978
979 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
980 skd_timer_tick_not_online(skdev);
981 goto timer_func_out;
982 }
983 skdev->timeout_stamp++;
984 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
985
986 /*
987 * All requests that happened during the previous use of
988 * this slot should be done by now. The previous use was
989 * over 7 seconds ago.
990 */
991 if (skdev->timeout_slot[timo_slot] == 0)
992 goto timer_func_out;
993
994 /* Something is overdue */
995 overdue_timestamp = skdev->timeout_stamp - SKD_N_TIMEOUT_SLOT;
996
rchinthekindi2e44b422013-10-24 12:51:23 +0100997 pr_debug("%s:%s:%d found %d timeouts, draining busy=%d\n",
998 skdev->name, __func__, __LINE__,
999 skdev->timeout_slot[timo_slot], skdev->in_flight);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001000 pr_err("(%s): Overdue IOs (%d), busy %d\n",
1001 skd_name(skdev), skdev->timeout_slot[timo_slot],
1002 skdev->in_flight);
1003
1004 skdev->timer_countdown = SKD_DRAINING_TIMO;
1005 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
1006 skdev->timo_slot = timo_slot;
Jens Axboe6a5ec652013-11-01 10:38:45 -06001007 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001008
1009timer_func_out:
1010 mod_timer(&skdev->timer, (jiffies + HZ));
1011
1012 spin_unlock_irqrestore(&skdev->lock, reqflags);
1013}
1014
1015static void skd_timer_tick_not_online(struct skd_device *skdev)
1016{
1017 switch (skdev->state) {
1018 case SKD_DRVR_STATE_IDLE:
1019 case SKD_DRVR_STATE_LOAD:
1020 break;
1021 case SKD_DRVR_STATE_BUSY_SANITIZE:
rchinthekindi2e44b422013-10-24 12:51:23 +01001022 pr_debug("%s:%s:%d drive busy sanitize[%x], driver[%x]\n",
1023 skdev->name, __func__, __LINE__,
1024 skdev->drive_state, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001025 /* If we've been in sanitize for 3 seconds, we figure we're not
1026 * going to get anymore completions, so recover requests now
1027 */
1028 if (skdev->timer_countdown > 0) {
1029 skdev->timer_countdown--;
1030 return;
1031 }
1032 skd_recover_requests(skdev, 0);
1033 break;
1034
1035 case SKD_DRVR_STATE_BUSY:
1036 case SKD_DRVR_STATE_BUSY_IMMINENT:
1037 case SKD_DRVR_STATE_BUSY_ERASE:
rchinthekindi2e44b422013-10-24 12:51:23 +01001038 pr_debug("%s:%s:%d busy[%x], countdown=%d\n",
1039 skdev->name, __func__, __LINE__,
1040 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001041 if (skdev->timer_countdown > 0) {
1042 skdev->timer_countdown--;
1043 return;
1044 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001045 pr_debug("%s:%s:%d busy[%x], timedout=%d, restarting device.",
1046 skdev->name, __func__, __LINE__,
1047 skdev->state, skdev->timer_countdown);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001048 skd_restart_device(skdev);
1049 break;
1050
1051 case SKD_DRVR_STATE_WAIT_BOOT:
1052 case SKD_DRVR_STATE_STARTING:
1053 if (skdev->timer_countdown > 0) {
1054 skdev->timer_countdown--;
1055 return;
1056 }
1057 /* For now, we fault the drive. Could attempt resets to
1058 * revcover at some point. */
1059 skdev->state = SKD_DRVR_STATE_FAULT;
1060
1061 pr_err("(%s): DriveFault Connect Timeout (%x)\n",
1062 skd_name(skdev), skdev->drive_state);
1063
1064 /*start the queue so we can respond with error to requests */
1065 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001066 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001067 skdev->gendisk_on = -1;
1068 wake_up_interruptible(&skdev->waitq);
1069 break;
1070
1071 case SKD_DRVR_STATE_ONLINE:
1072 /* shouldn't get here. */
1073 break;
1074
1075 case SKD_DRVR_STATE_PAUSING:
1076 case SKD_DRVR_STATE_PAUSED:
1077 break;
1078
1079 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
rchinthekindi2e44b422013-10-24 12:51:23 +01001080 pr_debug("%s:%s:%d "
1081 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
1082 skdev->name, __func__, __LINE__,
1083 skdev->timo_slot,
1084 skdev->timer_countdown,
1085 skdev->in_flight,
1086 skdev->timeout_slot[skdev->timo_slot]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001087 /* if the slot has cleared we can let the I/O continue */
1088 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001089 pr_debug("%s:%s:%d Slot drained, starting queue.\n",
1090 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001091 skdev->state = SKD_DRVR_STATE_ONLINE;
Jens Axboe6a5ec652013-11-01 10:38:45 -06001092 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001093 return;
1094 }
1095 if (skdev->timer_countdown > 0) {
1096 skdev->timer_countdown--;
1097 return;
1098 }
1099 skd_restart_device(skdev);
1100 break;
1101
1102 case SKD_DRVR_STATE_RESTARTING:
1103 if (skdev->timer_countdown > 0) {
1104 skdev->timer_countdown--;
1105 return;
1106 }
1107 /* For now, we fault the drive. Could attempt resets to
1108 * revcover at some point. */
1109 skdev->state = SKD_DRVR_STATE_FAULT;
1110 pr_err("(%s): DriveFault Reconnect Timeout (%x)\n",
1111 skd_name(skdev), skdev->drive_state);
1112
1113 /*
1114 * Recovering does two things:
1115 * 1. completes IO with error
1116 * 2. reclaims dma resources
1117 * When is it safe to recover requests?
1118 * - if the drive state is faulted
1119 * - if the state is still soft reset after out timeout
1120 * - if the drive registers are dead (state = FF)
1121 * If it is "unsafe", we still need to recover, so we will
1122 * disable pci bus mastering and disable our interrupts.
1123 */
1124
1125 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1126 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1127 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1128 /* It never came out of soft reset. Try to
1129 * recover the requests and then let them
1130 * fail. This is to mitigate hung processes. */
1131 skd_recover_requests(skdev, 0);
1132 else {
1133 pr_err("(%s): Disable BusMaster (%x)\n",
1134 skd_name(skdev), skdev->drive_state);
1135 pci_disable_device(skdev->pdev);
1136 skd_disable_interrupts(skdev);
1137 skd_recover_requests(skdev, 0);
1138 }
1139
1140 /*start the queue so we can respond with error to requests */
1141 /* wakeup anyone waiting for startup complete */
Jens Axboe6a5ec652013-11-01 10:38:45 -06001142 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001143 skdev->gendisk_on = -1;
1144 wake_up_interruptible(&skdev->waitq);
1145 break;
1146
1147 case SKD_DRVR_STATE_RESUMING:
1148 case SKD_DRVR_STATE_STOPPING:
1149 case SKD_DRVR_STATE_SYNCING:
1150 case SKD_DRVR_STATE_FAULT:
1151 case SKD_DRVR_STATE_DISAPPEARED:
1152 default:
1153 break;
1154 }
1155}
1156
1157static int skd_start_timer(struct skd_device *skdev)
1158{
1159 int rc;
1160
1161 init_timer(&skdev->timer);
1162 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1163
1164 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1165 if (rc)
1166 pr_err("%s: failed to start timer %d\n",
1167 __func__, rc);
1168 return rc;
1169}
1170
1171static void skd_kill_timer(struct skd_device *skdev)
1172{
1173 del_timer_sync(&skdev->timer);
1174}
1175
1176/*
1177 *****************************************************************************
1178 * IOCTL
1179 *****************************************************************************
1180 */
1181static int skd_ioctl_sg_io(struct skd_device *skdev,
1182 fmode_t mode, void __user *argp);
1183static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1184 struct skd_sg_io *sksgio);
1185static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1186 struct skd_sg_io *sksgio);
1187static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1188 struct skd_sg_io *sksgio);
1189static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1190 struct skd_sg_io *sksgio, int dxfer_dir);
1191static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1192 struct skd_sg_io *sksgio);
1193static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1194static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1195 struct skd_sg_io *sksgio);
1196static int skd_sg_io_put_status(struct skd_device *skdev,
1197 struct skd_sg_io *sksgio);
1198
1199static void skd_complete_special(struct skd_device *skdev,
1200 volatile struct fit_completion_entry_v1
1201 *skcomp,
1202 volatile struct fit_comp_error_info *skerr,
1203 struct skd_special_context *skspcl);
1204
1205static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1206 uint cmd_in, ulong arg)
1207{
1208 int rc = 0;
1209 struct gendisk *disk = bdev->bd_disk;
1210 struct skd_device *skdev = disk->private_data;
1211 void __user *p = (void *)arg;
1212
rchinthekindi2e44b422013-10-24 12:51:23 +01001213 pr_debug("%s:%s:%d %s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1214 skdev->name, __func__, __LINE__,
1215 disk->disk_name, current->comm, mode, cmd_in, arg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001216
1217 if (!capable(CAP_SYS_ADMIN))
1218 return -EPERM;
1219
1220 switch (cmd_in) {
1221 case SG_SET_TIMEOUT:
1222 case SG_GET_TIMEOUT:
1223 case SG_GET_VERSION_NUM:
1224 rc = scsi_cmd_ioctl(disk->queue, disk, mode, cmd_in, p);
1225 break;
1226 case SG_IO:
1227 rc = skd_ioctl_sg_io(skdev, mode, p);
1228 break;
1229
1230 default:
1231 rc = -ENOTTY;
1232 break;
1233 }
1234
rchinthekindi2e44b422013-10-24 12:51:23 +01001235 pr_debug("%s:%s:%d %s: completion rc %d\n",
1236 skdev->name, __func__, __LINE__, disk->disk_name, rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001237 return rc;
1238}
1239
1240static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1241 void __user *argp)
1242{
1243 int rc;
1244 struct skd_sg_io sksgio;
1245
1246 memset(&sksgio, 0, sizeof(sksgio));
1247 sksgio.mode = mode;
1248 sksgio.argp = argp;
1249 sksgio.iov = &sksgio.no_iov_iov;
1250
1251 switch (skdev->state) {
1252 case SKD_DRVR_STATE_ONLINE:
1253 case SKD_DRVR_STATE_BUSY_IMMINENT:
1254 break;
1255
1256 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01001257 pr_debug("%s:%s:%d drive not online\n",
1258 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001259 rc = -ENXIO;
1260 goto out;
1261 }
1262
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001263 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1264 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001265 goto out;
1266
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001267 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1268 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001269 goto out;
1270
Akhil Bhansalif721bb02013-10-23 13:00:08 +01001271 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1272 if (rc)
1273 goto out;
1274
1275 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1276 if (rc)
1277 goto out;
1278
1279 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1280 if (rc)
1281 goto out;
1282
1283 rc = skd_sg_io_await(skdev, &sksgio);
1284 if (rc)
1285 goto out;
1286
1287 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1288 if (rc)
1289 goto out;
1290
1291 rc = skd_sg_io_put_status(skdev, &sksgio);
1292 if (rc)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001293 goto out;
1294
1295 rc = 0;
1296
1297out:
1298 skd_sg_io_release_skspcl(skdev, &sksgio);
1299
1300 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1301 kfree(sksgio.iov);
1302 return rc;
1303}
1304
1305static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1306 struct skd_sg_io *sksgio)
1307{
1308 struct sg_io_hdr *sgp = &sksgio->sg;
1309 int i, acc;
1310
1311 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001312 pr_debug("%s:%s:%d access sg failed %p\n",
1313 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001314 return -EFAULT;
1315 }
1316
1317 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001318 pr_debug("%s:%s:%d copy_from_user sg failed %p\n",
1319 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001320 return -EFAULT;
1321 }
1322
1323 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001324 pr_debug("%s:%s:%d interface_id invalid 0x%x\n",
1325 skdev->name, __func__, __LINE__, sgp->interface_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001326 return -EINVAL;
1327 }
1328
1329 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001330 pr_debug("%s:%s:%d cmd_len invalid %d\n",
1331 skdev->name, __func__, __LINE__, sgp->cmd_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001332 return -EINVAL;
1333 }
1334
1335 if (sgp->iovec_count > 256) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001336 pr_debug("%s:%s:%d iovec_count invalid %d\n",
1337 skdev->name, __func__, __LINE__, sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001338 return -EINVAL;
1339 }
1340
1341 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001342 pr_debug("%s:%s:%d dxfer_len invalid %d\n",
1343 skdev->name, __func__, __LINE__, sgp->dxfer_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001344 return -EINVAL;
1345 }
1346
1347 switch (sgp->dxfer_direction) {
1348 case SG_DXFER_NONE:
1349 acc = -1;
1350 break;
1351
1352 case SG_DXFER_TO_DEV:
1353 acc = VERIFY_READ;
1354 break;
1355
1356 case SG_DXFER_FROM_DEV:
1357 case SG_DXFER_TO_FROM_DEV:
1358 acc = VERIFY_WRITE;
1359 break;
1360
1361 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01001362 pr_debug("%s:%s:%d dxfer_dir invalid %d\n",
1363 skdev->name, __func__, __LINE__, sgp->dxfer_direction);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001364 return -EINVAL;
1365 }
1366
1367 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001368 pr_debug("%s:%s:%d copy_from_user cmdp failed %p\n",
1369 skdev->name, __func__, __LINE__, sgp->cmdp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001370 return -EFAULT;
1371 }
1372
1373 if (sgp->mx_sb_len != 0) {
1374 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001375 pr_debug("%s:%s:%d access sbp failed %p\n",
1376 skdev->name, __func__, __LINE__, sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001377 return -EFAULT;
1378 }
1379 }
1380
1381 if (sgp->iovec_count == 0) {
1382 sksgio->iov[0].iov_base = sgp->dxferp;
1383 sksgio->iov[0].iov_len = sgp->dxfer_len;
1384 sksgio->iovcnt = 1;
1385 sksgio->dxfer_len = sgp->dxfer_len;
1386 } else {
1387 struct sg_iovec *iov;
1388 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1389 size_t iov_data_len;
1390
1391 iov = kmalloc(nbytes, GFP_KERNEL);
1392 if (iov == NULL) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001393 pr_debug("%s:%s:%d alloc iovec failed %d\n",
1394 skdev->name, __func__, __LINE__,
1395 sgp->iovec_count);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001396 return -ENOMEM;
1397 }
1398 sksgio->iov = iov;
1399 sksgio->iovcnt = sgp->iovec_count;
1400
1401 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001402 pr_debug("%s:%s:%d copy_from_user iovec failed %p\n",
1403 skdev->name, __func__, __LINE__, sgp->dxferp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001404 return -EFAULT;
1405 }
1406
1407 /*
1408 * Sum up the vecs, making sure they don't overflow
1409 */
1410 iov_data_len = 0;
1411 for (i = 0; i < sgp->iovec_count; i++) {
1412 if (iov_data_len + iov[i].iov_len < iov_data_len)
1413 return -EINVAL;
1414 iov_data_len += iov[i].iov_len;
1415 }
1416
1417 /* SG_IO howto says that the shorter of the two wins */
1418 if (sgp->dxfer_len < iov_data_len) {
1419 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1420 sgp->iovec_count,
1421 sgp->dxfer_len);
1422 sksgio->dxfer_len = sgp->dxfer_len;
1423 } else
1424 sksgio->dxfer_len = iov_data_len;
1425 }
1426
1427 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1428 struct sg_iovec *iov = sksgio->iov;
1429 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1430 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001431 pr_debug("%s:%s:%d access data failed %p/%d\n",
1432 skdev->name, __func__, __LINE__,
1433 iov->iov_base, (int)iov->iov_len);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001434 return -EFAULT;
1435 }
1436 }
1437 }
1438
1439 return 0;
1440}
1441
1442static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1443 struct skd_sg_io *sksgio)
1444{
1445 struct skd_special_context *skspcl = NULL;
1446 int rc;
1447
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04001448 for (;;) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001449 ulong flags;
1450
1451 spin_lock_irqsave(&skdev->lock, flags);
1452 skspcl = skdev->skspcl_free_list;
1453 if (skspcl != NULL) {
1454 skdev->skspcl_free_list =
1455 (struct skd_special_context *)skspcl->req.next;
1456 skspcl->req.id += SKD_ID_INCR;
1457 skspcl->req.state = SKD_REQ_STATE_SETUP;
1458 skspcl->orphaned = 0;
1459 skspcl->req.n_sg = 0;
1460 }
1461 spin_unlock_irqrestore(&skdev->lock, flags);
1462
1463 if (skspcl != NULL) {
1464 rc = 0;
1465 break;
1466 }
1467
rchinthekindi2e44b422013-10-24 12:51:23 +01001468 pr_debug("%s:%s:%d blocking\n",
1469 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001470
1471 rc = wait_event_interruptible_timeout(
1472 skdev->waitq,
1473 (skdev->skspcl_free_list != NULL),
1474 msecs_to_jiffies(sksgio->sg.timeout));
1475
rchinthekindi2e44b422013-10-24 12:51:23 +01001476 pr_debug("%s:%s:%d unblocking, rc=%d\n",
1477 skdev->name, __func__, __LINE__, rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001478
1479 if (rc <= 0) {
1480 if (rc == 0)
1481 rc = -ETIMEDOUT;
1482 else
1483 rc = -EINTR;
1484 break;
1485 }
1486 /*
1487 * If we get here rc > 0 meaning the timeout to
1488 * wait_event_interruptible_timeout() had time left, hence the
1489 * sought event -- non-empty free list -- happened.
1490 * Retry the allocation.
1491 */
1492 }
1493 sksgio->skspcl = skspcl;
1494
1495 return rc;
1496}
1497
1498static int skd_skreq_prep_buffering(struct skd_device *skdev,
1499 struct skd_request_context *skreq,
1500 u32 dxfer_len)
1501{
1502 u32 resid = dxfer_len;
1503
1504 /*
1505 * The DMA engine must have aligned addresses and byte counts.
1506 */
1507 resid += (-resid) & 3;
1508 skreq->sg_byte_count = resid;
1509
1510 skreq->n_sg = 0;
1511
1512 while (resid > 0) {
1513 u32 nbytes = PAGE_SIZE;
1514 u32 ix = skreq->n_sg;
1515 struct scatterlist *sg = &skreq->sg[ix];
1516 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1517 struct page *page;
1518
1519 if (nbytes > resid)
1520 nbytes = resid;
1521
1522 page = alloc_page(GFP_KERNEL);
1523 if (page == NULL)
1524 return -ENOMEM;
1525
1526 sg_set_page(sg, page, nbytes, 0);
1527
1528 /* TODO: This should be going through a pci_???()
1529 * routine to do proper mapping. */
1530 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1531 sksg->byte_count = nbytes;
1532
1533 sksg->host_side_addr = sg_phys(sg);
1534
1535 sksg->dev_side_addr = 0;
1536 sksg->next_desc_ptr = skreq->sksg_dma_address +
1537 (ix + 1) * sizeof(*sksg);
1538
1539 skreq->n_sg++;
1540 resid -= nbytes;
1541 }
1542
1543 if (skreq->n_sg > 0) {
1544 u32 ix = skreq->n_sg - 1;
1545 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1546
1547 sksg->control = FIT_SGD_CONTROL_LAST;
1548 sksg->next_desc_ptr = 0;
1549 }
1550
1551 if (unlikely(skdev->dbg_level > 1)) {
1552 u32 i;
1553
rchinthekindi2e44b422013-10-24 12:51:23 +01001554 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
1555 skdev->name, __func__, __LINE__,
1556 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001557 for (i = 0; i < skreq->n_sg; i++) {
1558 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1559
rchinthekindi2e44b422013-10-24 12:51:23 +01001560 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
1561 "addr=0x%llx next=0x%llx\n",
1562 skdev->name, __func__, __LINE__,
1563 i, sgd->byte_count, sgd->control,
1564 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001565 }
1566 }
1567
1568 return 0;
1569}
1570
1571static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1572 struct skd_sg_io *sksgio)
1573{
1574 struct skd_special_context *skspcl = sksgio->skspcl;
1575 struct skd_request_context *skreq = &skspcl->req;
1576 u32 dxfer_len = sksgio->dxfer_len;
1577 int rc;
1578
1579 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1580 /*
1581 * Eventually, errors or not, skd_release_special() is called
1582 * to recover allocations including partial allocations.
1583 */
1584 return rc;
1585}
1586
1587static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1588 struct skd_sg_io *sksgio, int dxfer_dir)
1589{
1590 struct skd_special_context *skspcl = sksgio->skspcl;
1591 u32 iov_ix = 0;
1592 struct sg_iovec curiov;
1593 u32 sksg_ix = 0;
1594 u8 *bufp = NULL;
1595 u32 buf_len = 0;
1596 u32 resid = sksgio->dxfer_len;
1597 int rc;
1598
1599 curiov.iov_len = 0;
1600 curiov.iov_base = NULL;
1601
1602 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1603 if (dxfer_dir != SG_DXFER_TO_DEV ||
1604 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1605 return 0;
1606 }
1607
1608 while (resid > 0) {
1609 u32 nbytes = PAGE_SIZE;
1610
1611 if (curiov.iov_len == 0) {
1612 curiov = sksgio->iov[iov_ix++];
1613 continue;
1614 }
1615
1616 if (buf_len == 0) {
1617 struct page *page;
1618 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1619 bufp = page_address(page);
1620 buf_len = PAGE_SIZE;
1621 }
1622
1623 nbytes = min_t(u32, nbytes, resid);
1624 nbytes = min_t(u32, nbytes, curiov.iov_len);
1625 nbytes = min_t(u32, nbytes, buf_len);
1626
1627 if (dxfer_dir == SG_DXFER_TO_DEV)
1628 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1629 else
1630 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1631
1632 if (rc)
1633 return -EFAULT;
1634
1635 resid -= nbytes;
1636 curiov.iov_len -= nbytes;
1637 curiov.iov_base += nbytes;
1638 buf_len -= nbytes;
1639 }
1640
1641 return 0;
1642}
1643
1644static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1645 struct skd_sg_io *sksgio)
1646{
1647 struct skd_special_context *skspcl = sksgio->skspcl;
1648 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
1649 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
1650
1651 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1652
1653 /* Initialize the FIT msg header */
1654 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1655 fmh->num_protocol_cmds_coalesced = 1;
1656
1657 /* Initialize the SCSI request */
1658 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1659 scsi_req->hdr.sg_list_dma_address =
1660 cpu_to_be64(skspcl->req.sksg_dma_address);
1661 scsi_req->hdr.tag = skspcl->req.id;
1662 scsi_req->hdr.sg_list_len_bytes =
1663 cpu_to_be32(skspcl->req.sg_byte_count);
1664 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1665
1666 skspcl->req.state = SKD_REQ_STATE_BUSY;
1667 skd_send_special_fitmsg(skdev, skspcl);
1668
1669 return 0;
1670}
1671
1672static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1673{
1674 unsigned long flags;
1675 int rc;
1676
1677 rc = wait_event_interruptible_timeout(skdev->waitq,
1678 (sksgio->skspcl->req.state !=
1679 SKD_REQ_STATE_BUSY),
1680 msecs_to_jiffies(sksgio->sg.
1681 timeout));
1682
1683 spin_lock_irqsave(&skdev->lock, flags);
1684
1685 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001686 pr_debug("%s:%s:%d skspcl %p aborted\n",
1687 skdev->name, __func__, __LINE__, sksgio->skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001688
1689 /* Build check cond, sense and let command finish. */
1690 /* For a timeout, we must fabricate completion and sense
1691 * data to complete the command */
1692 sksgio->skspcl->req.completion.status =
1693 SAM_STAT_CHECK_CONDITION;
1694
1695 memset(&sksgio->skspcl->req.err_info, 0,
1696 sizeof(sksgio->skspcl->req.err_info));
1697 sksgio->skspcl->req.err_info.type = 0x70;
1698 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1699 sksgio->skspcl->req.err_info.code = 0x44;
1700 sksgio->skspcl->req.err_info.qual = 0;
1701 rc = 0;
1702 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1703 /* No longer on the adapter. We finish. */
1704 rc = 0;
1705 else {
1706 /* Something's gone wrong. Still busy. Timeout or
1707 * user interrupted (control-C). Mark as an orphan
1708 * so it will be disposed when completed. */
1709 sksgio->skspcl->orphaned = 1;
1710 sksgio->skspcl = NULL;
1711 if (rc == 0) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001712 pr_debug("%s:%s:%d timed out %p (%u ms)\n",
1713 skdev->name, __func__, __LINE__,
1714 sksgio, sksgio->sg.timeout);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001715 rc = -ETIMEDOUT;
1716 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01001717 pr_debug("%s:%s:%d cntlc %p\n",
1718 skdev->name, __func__, __LINE__, sksgio);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001719 rc = -EINTR;
1720 }
1721 }
1722
1723 spin_unlock_irqrestore(&skdev->lock, flags);
1724
1725 return rc;
1726}
1727
1728static int skd_sg_io_put_status(struct skd_device *skdev,
1729 struct skd_sg_io *sksgio)
1730{
1731 struct sg_io_hdr *sgp = &sksgio->sg;
1732 struct skd_special_context *skspcl = sksgio->skspcl;
1733 int resid = 0;
1734
1735 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1736
1737 sgp->status = skspcl->req.completion.status;
1738 resid = sksgio->dxfer_len - nb;
1739
1740 sgp->masked_status = sgp->status & STATUS_MASK;
1741 sgp->msg_status = 0;
1742 sgp->host_status = 0;
1743 sgp->driver_status = 0;
1744 sgp->resid = resid;
1745 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1746 sgp->info |= SG_INFO_CHECK;
1747
rchinthekindi2e44b422013-10-24 12:51:23 +01001748 pr_debug("%s:%s:%d status %x masked %x resid 0x%x\n",
1749 skdev->name, __func__, __LINE__,
1750 sgp->status, sgp->masked_status, sgp->resid);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001751
1752 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1753 if (sgp->mx_sb_len > 0) {
1754 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1755 u32 nbytes = sizeof(*ei);
1756
1757 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1758
1759 sgp->sb_len_wr = nbytes;
1760
1761 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001762 pr_debug("%s:%s:%d copy_to_user sense failed %p\n",
1763 skdev->name, __func__, __LINE__,
1764 sgp->sbp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001765 return -EFAULT;
1766 }
1767 }
1768 }
1769
1770 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001771 pr_debug("%s:%s:%d copy_to_user sg failed %p\n",
1772 skdev->name, __func__, __LINE__, sksgio->argp);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001773 return -EFAULT;
1774 }
1775
1776 return 0;
1777}
1778
1779static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1780 struct skd_sg_io *sksgio)
1781{
1782 struct skd_special_context *skspcl = sksgio->skspcl;
1783
1784 if (skspcl != NULL) {
1785 ulong flags;
1786
1787 sksgio->skspcl = NULL;
1788
1789 spin_lock_irqsave(&skdev->lock, flags);
1790 skd_release_special(skdev, skspcl);
1791 spin_unlock_irqrestore(&skdev->lock, flags);
1792 }
1793
1794 return 0;
1795}
1796
1797/*
1798 *****************************************************************************
1799 * INTERNAL REQUESTS -- generated by driver itself
1800 *****************************************************************************
1801 */
1802
1803static int skd_format_internal_skspcl(struct skd_device *skdev)
1804{
1805 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1806 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1807 struct fit_msg_hdr *fmh;
1808 uint64_t dma_address;
1809 struct skd_scsi_request *scsi;
1810
1811 fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0];
1812 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1813 fmh->num_protocol_cmds_coalesced = 1;
1814
1815 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1816 memset(scsi, 0, sizeof(*scsi));
1817 dma_address = skspcl->req.sksg_dma_address;
1818 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1819 sgd->control = FIT_SGD_CONTROL_LAST;
1820 sgd->byte_count = 0;
1821 sgd->host_side_addr = skspcl->db_dma_address;
1822 sgd->dev_side_addr = 0;
1823 sgd->next_desc_ptr = 0LL;
1824
1825 return 1;
1826}
1827
1828#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1829
1830static void skd_send_internal_skspcl(struct skd_device *skdev,
1831 struct skd_special_context *skspcl,
1832 u8 opcode)
1833{
1834 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1835 struct skd_scsi_request *scsi;
1836 unsigned char *buf = skspcl->data_buf;
1837 int i;
1838
1839 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1840 /*
1841 * A refresh is already in progress.
1842 * Just wait for it to finish.
1843 */
1844 return;
1845
1846 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1847 skspcl->req.state = SKD_REQ_STATE_BUSY;
1848 skspcl->req.id += SKD_ID_INCR;
1849
1850 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1851 scsi->hdr.tag = skspcl->req.id;
1852
1853 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1854
1855 switch (opcode) {
1856 case TEST_UNIT_READY:
1857 scsi->cdb[0] = TEST_UNIT_READY;
1858 sgd->byte_count = 0;
1859 scsi->hdr.sg_list_len_bytes = 0;
1860 break;
1861
1862 case READ_CAPACITY:
1863 scsi->cdb[0] = READ_CAPACITY;
1864 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1865 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1866 break;
1867
1868 case INQUIRY:
1869 scsi->cdb[0] = INQUIRY;
1870 scsi->cdb[1] = 0x01; /* evpd */
1871 scsi->cdb[2] = 0x80; /* serial number page */
1872 scsi->cdb[4] = 0x10;
1873 sgd->byte_count = 16;
1874 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1875 break;
1876
1877 case SYNCHRONIZE_CACHE:
1878 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1879 sgd->byte_count = 0;
1880 scsi->hdr.sg_list_len_bytes = 0;
1881 break;
1882
1883 case WRITE_BUFFER:
1884 scsi->cdb[0] = WRITE_BUFFER;
1885 scsi->cdb[1] = 0x02;
1886 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1887 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1888 sgd->byte_count = WR_BUF_SIZE;
1889 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1890 /* fill incrementing byte pattern */
1891 for (i = 0; i < sgd->byte_count; i++)
1892 buf[i] = i & 0xFF;
1893 break;
1894
1895 case READ_BUFFER:
1896 scsi->cdb[0] = READ_BUFFER;
1897 scsi->cdb[1] = 0x02;
1898 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1899 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1900 sgd->byte_count = WR_BUF_SIZE;
1901 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1902 memset(skspcl->data_buf, 0, sgd->byte_count);
1903 break;
1904
1905 default:
1906 SKD_ASSERT("Don't know what to send");
1907 return;
1908
1909 }
1910 skd_send_special_fitmsg(skdev, skspcl);
1911}
1912
1913static void skd_refresh_device_data(struct skd_device *skdev)
1914{
1915 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1916
1917 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1918}
1919
1920static int skd_chk_read_buf(struct skd_device *skdev,
1921 struct skd_special_context *skspcl)
1922{
1923 unsigned char *buf = skspcl->data_buf;
1924 int i;
1925
1926 /* check for incrementing byte pattern */
1927 for (i = 0; i < WR_BUF_SIZE; i++)
1928 if (buf[i] != (i & 0xFF))
1929 return 1;
1930
1931 return 0;
1932}
1933
1934static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1935 u8 code, u8 qual, u8 fruc)
1936{
1937 /* If the check condition is of special interest, log a message */
1938 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1939 && (code == 0x04) && (qual == 0x06)) {
1940 pr_err("(%s): *** LOST_WRITE_DATA ERROR *** key/asc/"
1941 "ascq/fruc %02x/%02x/%02x/%02x\n",
1942 skd_name(skdev), key, code, qual, fruc);
1943 }
1944}
1945
1946static void skd_complete_internal(struct skd_device *skdev,
1947 volatile struct fit_completion_entry_v1
1948 *skcomp,
1949 volatile struct fit_comp_error_info *skerr,
1950 struct skd_special_context *skspcl)
1951{
1952 u8 *buf = skspcl->data_buf;
1953 u8 status;
1954 int i;
1955 struct skd_scsi_request *scsi =
1956 (struct skd_scsi_request *)&skspcl->msg_buf[64];
1957
1958 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1959
rchinthekindi2e44b422013-10-24 12:51:23 +01001960 pr_debug("%s:%s:%d complete internal %x\n",
1961 skdev->name, __func__, __LINE__, scsi->cdb[0]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001962
1963 skspcl->req.completion = *skcomp;
1964 skspcl->req.state = SKD_REQ_STATE_IDLE;
1965 skspcl->req.id += SKD_ID_INCR;
1966
1967 status = skspcl->req.completion.status;
1968
1969 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1970 skerr->qual, skerr->fruc);
1971
1972 switch (scsi->cdb[0]) {
1973 case TEST_UNIT_READY:
1974 if (status == SAM_STAT_GOOD)
1975 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1976 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1977 (skerr->key == MEDIUM_ERROR))
1978 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1979 else {
1980 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001981 pr_debug("%s:%s:%d TUR failed, don't send anymore state 0x%x\n",
1982 skdev->name, __func__, __LINE__,
1983 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001984 return;
1985 }
rchinthekindi2e44b422013-10-24 12:51:23 +01001986 pr_debug("%s:%s:%d **** TUR failed, retry skerr\n",
1987 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06001988 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1989 }
1990 break;
1991
1992 case WRITE_BUFFER:
1993 if (status == SAM_STAT_GOOD)
1994 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1995 else {
1996 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01001997 pr_debug("%s:%s:%d write buffer failed, don't send anymore state 0x%x\n",
1998 skdev->name, __func__, __LINE__,
1999 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002000 return;
2001 }
rchinthekindi2e44b422013-10-24 12:51:23 +01002002 pr_debug("%s:%s:%d **** write buffer failed, retry skerr\n",
2003 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002004 skd_send_internal_skspcl(skdev, skspcl, 0x00);
2005 }
2006 break;
2007
2008 case READ_BUFFER:
2009 if (status == SAM_STAT_GOOD) {
2010 if (skd_chk_read_buf(skdev, skspcl) == 0)
2011 skd_send_internal_skspcl(skdev, skspcl,
2012 READ_CAPACITY);
2013 else {
2014 pr_err(
2015 "(%s):*** W/R Buffer mismatch %d ***\n",
2016 skd_name(skdev), skdev->connect_retries);
2017 if (skdev->connect_retries <
2018 SKD_MAX_CONNECT_RETRIES) {
2019 skdev->connect_retries++;
2020 skd_soft_reset(skdev);
2021 } else {
2022 pr_err(
2023 "(%s): W/R Buffer Connect Error\n",
2024 skd_name(skdev));
2025 return;
2026 }
2027 }
2028
2029 } else {
2030 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002031 pr_debug("%s:%s:%d "
2032 "read buffer failed, don't send anymore state 0x%x\n",
2033 skdev->name, __func__, __LINE__,
2034 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002035 return;
2036 }
rchinthekindi2e44b422013-10-24 12:51:23 +01002037 pr_debug("%s:%s:%d "
2038 "**** read buffer failed, retry skerr\n",
2039 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002040 skd_send_internal_skspcl(skdev, skspcl, 0x00);
2041 }
2042 break;
2043
2044 case READ_CAPACITY:
2045 skdev->read_cap_is_valid = 0;
2046 if (status == SAM_STAT_GOOD) {
2047 skdev->read_cap_last_lba =
2048 (buf[0] << 24) | (buf[1] << 16) |
2049 (buf[2] << 8) | buf[3];
2050 skdev->read_cap_blocksize =
2051 (buf[4] << 24) | (buf[5] << 16) |
2052 (buf[6] << 8) | buf[7];
2053
rchinthekindi2e44b422013-10-24 12:51:23 +01002054 pr_debug("%s:%s:%d last lba %d, bs %d\n",
2055 skdev->name, __func__, __LINE__,
2056 skdev->read_cap_last_lba,
2057 skdev->read_cap_blocksize);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002058
2059 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
2060
2061 skdev->read_cap_is_valid = 1;
2062
2063 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2064 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
2065 (skerr->key == MEDIUM_ERROR)) {
2066 skdev->read_cap_last_lba = ~0;
2067 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
rchinthekindi2e44b422013-10-24 12:51:23 +01002068 pr_debug("%s:%s:%d "
2069 "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n",
2070 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002071 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2072 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01002073 pr_debug("%s:%s:%d **** READCAP failed, retry TUR\n",
2074 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002075 skd_send_internal_skspcl(skdev, skspcl,
2076 TEST_UNIT_READY);
2077 }
2078 break;
2079
2080 case INQUIRY:
2081 skdev->inquiry_is_valid = 0;
2082 if (status == SAM_STAT_GOOD) {
2083 skdev->inquiry_is_valid = 1;
2084
2085 for (i = 0; i < 12; i++)
2086 skdev->inq_serial_num[i] = buf[i + 4];
2087 skdev->inq_serial_num[12] = 0;
2088 }
2089
2090 if (skd_unquiesce_dev(skdev) < 0)
rchinthekindi2e44b422013-10-24 12:51:23 +01002091 pr_debug("%s:%s:%d **** failed, to ONLINE device\n",
2092 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002093 /* connection is complete */
2094 skdev->connect_retries = 0;
2095 break;
2096
2097 case SYNCHRONIZE_CACHE:
2098 if (status == SAM_STAT_GOOD)
2099 skdev->sync_done = 1;
2100 else
2101 skdev->sync_done = -1;
2102 wake_up_interruptible(&skdev->waitq);
2103 break;
2104
2105 default:
2106 SKD_ASSERT("we didn't send this");
2107 }
2108}
2109
2110/*
2111 *****************************************************************************
2112 * FIT MESSAGES
2113 *****************************************************************************
2114 */
2115
2116static void skd_send_fitmsg(struct skd_device *skdev,
2117 struct skd_fitmsg_context *skmsg)
2118{
2119 u64 qcmd;
2120 struct fit_msg_hdr *fmh;
2121
rchinthekindi2e44b422013-10-24 12:51:23 +01002122 pr_debug("%s:%s:%d dma address 0x%llx, busy=%d\n",
2123 skdev->name, __func__, __LINE__,
2124 skmsg->mb_dma_address, skdev->in_flight);
2125 pr_debug("%s:%s:%d msg_buf 0x%p, offset %x\n",
2126 skdev->name, __func__, __LINE__,
2127 skmsg->msg_buf, skmsg->offset);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002128
2129 qcmd = skmsg->mb_dma_address;
2130 qcmd |= FIT_QCMD_QID_NORMAL;
2131
2132 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
2133 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2134
2135 if (unlikely(skdev->dbg_level > 1)) {
2136 u8 *bp = (u8 *)skmsg->msg_buf;
2137 int i;
2138 for (i = 0; i < skmsg->length; i += 8) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002139 pr_debug("%s:%s:%d msg[%2d] %02x %02x %02x %02x "
2140 "%02x %02x %02x %02x\n",
2141 skdev->name, __func__, __LINE__,
2142 i, bp[i + 0], bp[i + 1], bp[i + 2],
2143 bp[i + 3], bp[i + 4], bp[i + 5],
2144 bp[i + 6], bp[i + 7]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002145 if (i == 0)
2146 i = 64 - 8;
2147 }
2148 }
2149
2150 if (skmsg->length > 256)
2151 qcmd |= FIT_QCMD_MSGSIZE_512;
2152 else if (skmsg->length > 128)
2153 qcmd |= FIT_QCMD_MSGSIZE_256;
2154 else if (skmsg->length > 64)
2155 qcmd |= FIT_QCMD_MSGSIZE_128;
2156 else
2157 /*
2158 * This makes no sense because the FIT msg header is
2159 * 64 bytes. If the msg is only 64 bytes long it has
2160 * no payload.
2161 */
2162 qcmd |= FIT_QCMD_MSGSIZE_64;
2163
2164 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2165
2166}
2167
2168static void skd_send_special_fitmsg(struct skd_device *skdev,
2169 struct skd_special_context *skspcl)
2170{
2171 u64 qcmd;
2172
2173 if (unlikely(skdev->dbg_level > 1)) {
2174 u8 *bp = (u8 *)skspcl->msg_buf;
2175 int i;
2176
2177 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002178 pr_debug("%s:%s:%d spcl[%2d] %02x %02x %02x %02x "
2179 "%02x %02x %02x %02x\n",
2180 skdev->name, __func__, __LINE__, i,
2181 bp[i + 0], bp[i + 1], bp[i + 2], bp[i + 3],
2182 bp[i + 4], bp[i + 5], bp[i + 6], bp[i + 7]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002183 if (i == 0)
2184 i = 64 - 8;
2185 }
2186
rchinthekindi2e44b422013-10-24 12:51:23 +01002187 pr_debug("%s:%s:%d skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2188 skdev->name, __func__, __LINE__,
2189 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2190 skspcl->req.sksg_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002191 for (i = 0; i < skspcl->req.n_sg; i++) {
2192 struct fit_sg_descriptor *sgd =
2193 &skspcl->req.sksg_list[i];
2194
rchinthekindi2e44b422013-10-24 12:51:23 +01002195 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
2196 "addr=0x%llx next=0x%llx\n",
2197 skdev->name, __func__, __LINE__,
2198 i, sgd->byte_count, sgd->control,
2199 sgd->host_side_addr, sgd->next_desc_ptr);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002200 }
2201 }
2202
2203 /*
2204 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2205 * and one 64-byte SSDI command.
2206 */
2207 qcmd = skspcl->mb_dma_address;
2208 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2209
2210 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2211}
2212
2213/*
2214 *****************************************************************************
2215 * COMPLETION QUEUE
2216 *****************************************************************************
2217 */
2218
2219static void skd_complete_other(struct skd_device *skdev,
2220 volatile struct fit_completion_entry_v1 *skcomp,
2221 volatile struct fit_comp_error_info *skerr);
2222
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002223struct sns_info {
2224 u8 type;
2225 u8 stat;
2226 u8 key;
2227 u8 asc;
2228 u8 ascq;
2229 u8 mask;
2230 enum skd_check_status_action action;
2231};
2232
2233static struct sns_info skd_chkstat_table[] = {
2234 /* Good */
2235 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2236 SKD_CHECK_STATUS_REPORT_GOOD },
2237
2238 /* Smart alerts */
2239 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2240 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2241 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2242 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2243 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2244 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2245
2246 /* Retry (with limits) */
2247 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2248 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2249 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2250 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2251 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2252 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2253 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2254 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2255
2256 /* Busy (or about to be) */
2257 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2258 SKD_CHECK_STATUS_BUSY_IMMINENT },
2259};
2260
2261/*
2262 * Look up status and sense data to decide how to handle the error
2263 * from the device.
2264 * mask says which fields must match e.g., mask=0x18 means check
2265 * type and stat, ignore key, asc, ascq.
2266 */
2267
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002268static enum skd_check_status_action
2269skd_check_status(struct skd_device *skdev,
2270 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002271{
2272 int i, n;
2273
2274 pr_err("(%s): key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2275 skd_name(skdev), skerr->key, skerr->code, skerr->qual,
2276 skerr->fruc);
2277
rchinthekindi2e44b422013-10-24 12:51:23 +01002278 pr_debug("%s:%s:%d stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2279 skdev->name, __func__, __LINE__, skerr->type, cmp_status,
2280 skerr->key, skerr->code, skerr->qual, skerr->fruc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002281
2282 /* Does the info match an entry in the good category? */
2283 n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
2284 for (i = 0; i < n; i++) {
2285 struct sns_info *sns = &skd_chkstat_table[i];
2286
2287 if (sns->mask & 0x10)
2288 if (skerr->type != sns->type)
2289 continue;
2290
2291 if (sns->mask & 0x08)
2292 if (cmp_status != sns->stat)
2293 continue;
2294
2295 if (sns->mask & 0x04)
2296 if (skerr->key != sns->key)
2297 continue;
2298
2299 if (sns->mask & 0x02)
2300 if (skerr->code != sns->asc)
2301 continue;
2302
2303 if (sns->mask & 0x01)
2304 if (skerr->qual != sns->ascq)
2305 continue;
2306
2307 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
2308 pr_err("(%s): SMART Alert: sense key/asc/ascq "
2309 "%02x/%02x/%02x\n",
2310 skd_name(skdev), skerr->key,
2311 skerr->code, skerr->qual);
2312 }
2313 return sns->action;
2314 }
2315
2316 /* No other match, so nonzero status means error,
2317 * zero status means good
2318 */
2319 if (cmp_status) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002320 pr_debug("%s:%s:%d status check: error\n",
2321 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002322 return SKD_CHECK_STATUS_REPORT_ERROR;
2323 }
2324
rchinthekindi2e44b422013-10-24 12:51:23 +01002325 pr_debug("%s:%s:%d status check good default\n",
2326 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002327 return SKD_CHECK_STATUS_REPORT_GOOD;
2328}
2329
2330static void skd_resolve_req_exception(struct skd_device *skdev,
2331 struct skd_request_context *skreq)
2332{
2333 u8 cmp_status = skreq->completion.status;
2334
2335 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2336 case SKD_CHECK_STATUS_REPORT_GOOD:
2337 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
2338 skd_end_request(skdev, skreq, 0);
2339 break;
2340
2341 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2342 skd_log_skreq(skdev, skreq, "retry(busy)");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002343 blk_requeue_request(skdev->queue, skreq->req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002344 pr_info("(%s) drive BUSY imminent\n", skd_name(skdev));
2345 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2346 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2347 skd_quiesce_dev(skdev);
2348 break;
2349
2350 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
Jens Axboefcd37eb2013-11-01 10:14:56 -06002351 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2352 skd_log_skreq(skdev, skreq, "retry");
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04002353 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06002354 break;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002355 }
2356 /* fall through to report error */
2357
2358 case SKD_CHECK_STATUS_REPORT_ERROR:
2359 default:
2360 skd_end_request(skdev, skreq, -EIO);
2361 break;
2362 }
2363}
2364
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002365/* assume spinlock is already held */
2366static void skd_release_skreq(struct skd_device *skdev,
2367 struct skd_request_context *skreq)
2368{
2369 u32 msg_slot;
2370 struct skd_fitmsg_context *skmsg;
2371
2372 u32 timo_slot;
2373
2374 /*
2375 * Reclaim the FIT msg buffer if this is
2376 * the first of the requests it carried to
2377 * be completed. The FIT msg buffer used to
2378 * send this request cannot be reused until
2379 * we are sure the s1120 card has copied
2380 * it to its memory. The FIT msg might have
2381 * contained several requests. As soon as
2382 * any of them are completed we know that
2383 * the entire FIT msg was transferred.
2384 * Only the first completed request will
2385 * match the FIT msg buffer id. The FIT
2386 * msg buffer id is immediately updated.
2387 * When subsequent requests complete the FIT
2388 * msg buffer id won't match, so we know
2389 * quite cheaply that it is already done.
2390 */
2391 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2392 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2393
2394 skmsg = &skdev->skmsg_table[msg_slot];
2395 if (skmsg->id == skreq->fitmsg_id) {
2396 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2397 SKD_ASSERT(skmsg->outstanding > 0);
2398 skmsg->outstanding--;
2399 if (skmsg->outstanding == 0) {
2400 skmsg->state = SKD_MSG_STATE_IDLE;
2401 skmsg->id += SKD_ID_INCR;
2402 skmsg->next = skdev->skmsg_free_list;
2403 skdev->skmsg_free_list = skmsg;
2404 }
2405 }
2406
2407 /*
2408 * Decrease the number of active requests.
2409 * Also decrements the count in the timeout slot.
2410 */
2411 SKD_ASSERT(skdev->in_flight > 0);
2412 skdev->in_flight -= 1;
2413
2414 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2415 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2416 skdev->timeout_slot[timo_slot] -= 1;
2417
2418 /*
2419 * Reset backpointer
2420 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002421 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002422
2423 /*
2424 * Reclaim the skd_request_context
2425 */
2426 skreq->state = SKD_REQ_STATE_IDLE;
2427 skreq->id += SKD_ID_INCR;
2428 skreq->next = skdev->skreq_free_list;
2429 skdev->skreq_free_list = skreq;
2430}
2431
2432#define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2433
2434static void skd_do_inq_page_00(struct skd_device *skdev,
2435 volatile struct fit_completion_entry_v1 *skcomp,
2436 volatile struct fit_comp_error_info *skerr,
2437 uint8_t *cdb, uint8_t *buf)
2438{
2439 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2440
2441 /* Caller requested "supported pages". The driver needs to insert
2442 * its page.
2443 */
rchinthekindi2e44b422013-10-24 12:51:23 +01002444 pr_debug("%s:%s:%d skd_do_driver_inquiry: modify supported pages.\n",
2445 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002446
2447 /* If the device rejected the request because the CDB was
2448 * improperly formed, then just leave.
2449 */
2450 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2451 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2452 return;
2453
2454 /* Get the amount of space the caller allocated */
2455 max_bytes = (cdb[3] << 8) | cdb[4];
2456
2457 /* Get the number of pages actually returned by the device */
2458 drive_pages = (buf[2] << 8) | buf[3];
2459 drive_bytes = drive_pages + 4;
2460 new_size = drive_pages + 1;
2461
2462 /* Supported pages must be in numerical order, so find where
2463 * the driver page needs to be inserted into the list of
2464 * pages returned by the device.
2465 */
2466 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2467 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2468 return; /* Device using this page code. abort */
2469 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2470 break;
2471 }
2472
2473 if (insert_pt < max_bytes) {
2474 uint16_t u;
2475
2476 /* Shift everything up one byte to make room. */
2477 for (u = new_size + 3; u > insert_pt; u--)
2478 buf[u] = buf[u - 1];
2479 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2480
2481 /* SCSI byte order increment of num_returned_bytes by 1 */
2482 skcomp->num_returned_bytes =
2483 be32_to_cpu(skcomp->num_returned_bytes) + 1;
2484 skcomp->num_returned_bytes =
2485 be32_to_cpu(skcomp->num_returned_bytes);
2486 }
2487
2488 /* update page length field to reflect the driver's page too */
2489 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2490 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2491}
2492
2493static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2494{
2495 int pcie_reg;
2496 u16 pci_bus_speed;
2497 u8 pci_lanes;
2498
2499 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2500 if (pcie_reg) {
2501 u16 linksta;
2502 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2503
2504 pci_bus_speed = linksta & 0xF;
2505 pci_lanes = (linksta & 0x3F0) >> 4;
2506 } else {
2507 *speed = STEC_LINK_UNKNOWN;
2508 *width = 0xFF;
2509 return;
2510 }
2511
2512 switch (pci_bus_speed) {
2513 case 1:
2514 *speed = STEC_LINK_2_5GTS;
2515 break;
2516 case 2:
2517 *speed = STEC_LINK_5GTS;
2518 break;
2519 case 3:
2520 *speed = STEC_LINK_8GTS;
2521 break;
2522 default:
2523 *speed = STEC_LINK_UNKNOWN;
2524 break;
2525 }
2526
2527 if (pci_lanes <= 0x20)
2528 *width = pci_lanes;
2529 else
2530 *width = 0xFF;
2531}
2532
2533static void skd_do_inq_page_da(struct skd_device *skdev,
2534 volatile struct fit_completion_entry_v1 *skcomp,
2535 volatile struct fit_comp_error_info *skerr,
2536 uint8_t *cdb, uint8_t *buf)
2537{
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002538 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002539 unsigned max_bytes;
2540 struct driver_inquiry_data inq;
2541 u16 val;
2542
rchinthekindi2e44b422013-10-24 12:51:23 +01002543 pr_debug("%s:%s:%d skd_do_driver_inquiry: return driver page\n",
2544 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002545
2546 memset(&inq, 0, sizeof(inq));
2547
2548 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2549
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002550 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2551 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2552 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2553 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002554
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002555 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2556 inq.pcie_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002557
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002558 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2559 inq.pcie_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002560
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002561 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2562 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002563
Bartlomiej Zolnierkiewiczfec23f62013-11-05 12:37:07 +01002564 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2565 inq.pcie_subsystem_device_id = cpu_to_be16(val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002566
2567 /* Driver version, fixed lenth, padded with spaces on the right */
2568 inq.driver_version_length = sizeof(inq.driver_version);
2569 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2570 memcpy(inq.driver_version, DRV_VER_COMPL,
2571 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2572
2573 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2574
2575 /* Clear the error set by the device */
2576 skcomp->status = SAM_STAT_GOOD;
2577 memset((void *)skerr, 0, sizeof(*skerr));
2578
2579 /* copy response into output buffer */
2580 max_bytes = (cdb[3] << 8) | cdb[4];
2581 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2582
2583 skcomp->num_returned_bytes =
2584 be32_to_cpu(min_t(uint16_t, max_bytes, sizeof(inq)));
2585}
2586
2587static void skd_do_driver_inq(struct skd_device *skdev,
2588 volatile struct fit_completion_entry_v1 *skcomp,
2589 volatile struct fit_comp_error_info *skerr,
2590 uint8_t *cdb, uint8_t *buf)
2591{
2592 if (!buf)
2593 return;
2594 else if (cdb[0] != INQUIRY)
2595 return; /* Not an INQUIRY */
2596 else if ((cdb[1] & 1) == 0)
2597 return; /* EVPD not set */
2598 else if (cdb[2] == 0)
2599 /* Need to add driver's page to supported pages list */
2600 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2601 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2602 /* Caller requested driver's page */
2603 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2604}
2605
2606static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2607{
2608 if (!sg)
2609 return NULL;
2610 if (!sg_page(sg))
2611 return NULL;
2612 return sg_virt(sg);
2613}
2614
2615static void skd_process_scsi_inq(struct skd_device *skdev,
2616 volatile struct fit_completion_entry_v1
2617 *skcomp,
2618 volatile struct fit_comp_error_info *skerr,
2619 struct skd_special_context *skspcl)
2620{
2621 uint8_t *buf;
2622 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
2623 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
2624
2625 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2626 skspcl->req.sg_data_dir);
2627 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2628
2629 if (buf)
2630 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2631}
2632
2633
2634static int skd_isr_completion_posted(struct skd_device *skdev,
2635 int limit, int *enqueued)
2636{
2637 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2638 volatile struct fit_comp_error_info *skerr;
2639 u16 req_id;
2640 u32 req_slot;
2641 struct skd_request_context *skreq;
2642 u16 cmp_cntxt = 0;
2643 u8 cmp_status = 0;
2644 u8 cmp_cycle = 0;
2645 u32 cmp_bytes = 0;
2646 int rc = 0;
2647 int processed = 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002648
2649 for (;; ) {
2650 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2651
2652 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2653 cmp_cycle = skcmp->cycle;
2654 cmp_cntxt = skcmp->tag;
2655 cmp_status = skcmp->status;
2656 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2657
2658 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2659
rchinthekindi2e44b422013-10-24 12:51:23 +01002660 pr_debug("%s:%s:%d "
2661 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d "
2662 "busy=%d rbytes=0x%x proto=%d\n",
2663 skdev->name, __func__, __LINE__, skdev->skcomp_cycle,
2664 skdev->skcomp_ix, cmp_cycle, cmp_cntxt, cmp_status,
2665 skdev->in_flight, cmp_bytes, skdev->proto_ver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002666
2667 if (cmp_cycle != skdev->skcomp_cycle) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002668 pr_debug("%s:%s:%d end of completions\n",
2669 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002670 break;
2671 }
2672 /*
2673 * Update the completion queue head index and possibly
2674 * the completion cycle count. 8-bit wrap-around.
2675 */
2676 skdev->skcomp_ix++;
2677 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2678 skdev->skcomp_ix = 0;
2679 skdev->skcomp_cycle++;
2680 }
2681
2682 /*
2683 * The command context is a unique 32-bit ID. The low order
2684 * bits help locate the request. The request is usually a
2685 * r/w request (see skd_start() above) or a special request.
2686 */
2687 req_id = cmp_cntxt;
2688 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2689
2690 /* Is this other than a r/w request? */
2691 if (req_slot >= skdev->num_req_context) {
2692 /*
2693 * This is not a completion for a r/w request.
2694 */
2695 skd_complete_other(skdev, skcmp, skerr);
2696 continue;
2697 }
2698
2699 skreq = &skdev->skreq_table[req_slot];
2700
2701 /*
2702 * Make sure the request ID for the slot matches.
2703 */
2704 if (skreq->id != req_id) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002705 pr_debug("%s:%s:%d mismatch comp_id=0x%x req_id=0x%x\n",
2706 skdev->name, __func__, __LINE__,
2707 req_id, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002708 {
2709 u16 new_id = cmp_cntxt;
2710 pr_err("(%s): Completion mismatch "
2711 "comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2712 skd_name(skdev), req_id,
2713 skreq->id, new_id);
2714
2715 continue;
2716 }
2717 }
2718
2719 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2720
2721 if (skreq->state == SKD_REQ_STATE_ABORTED) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002722 pr_debug("%s:%s:%d reclaim req %p id=%04x\n",
2723 skdev->name, __func__, __LINE__,
2724 skreq, skreq->id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002725 /* a previously timed out command can
2726 * now be cleaned up */
2727 skd_release_skreq(skdev, skreq);
2728 continue;
2729 }
2730
2731 skreq->completion = *skcmp;
2732 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2733 skreq->err_info = *skerr;
2734 skd_log_check_status(skdev, cmp_status, skerr->key,
2735 skerr->code, skerr->qual,
2736 skerr->fruc);
2737 }
2738 /* Release DMA resources for the request. */
2739 if (skreq->n_sg > 0)
2740 skd_postop_sg_list(skdev, skreq);
2741
Jens Axboefcd37eb2013-11-01 10:14:56 -06002742 if (!skreq->req) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002743 pr_debug("%s:%s:%d NULL backptr skdreq %p, "
2744 "req=0x%x req_id=0x%x\n",
2745 skdev->name, __func__, __LINE__,
2746 skreq, skreq->id, req_id);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002747 } else {
2748 /*
2749 * Capture the outcome and post it back to the
2750 * native request.
2751 */
Jens Axboefcd37eb2013-11-01 10:14:56 -06002752 if (likely(cmp_status == SAM_STAT_GOOD))
2753 skd_end_request(skdev, skreq, 0);
2754 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002755 skd_resolve_req_exception(skdev, skreq);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002756 }
2757
2758 /*
2759 * Release the skreq, its FIT msg (if one), timeout slot,
2760 * and queue depth.
2761 */
2762 skd_release_skreq(skdev, skreq);
2763
2764 /* skd_isr_comp_limit equal zero means no limit */
2765 if (limit) {
2766 if (++processed >= limit) {
2767 rc = 1;
2768 break;
2769 }
2770 }
2771 }
2772
2773 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2774 && (skdev->in_flight) == 0) {
2775 skdev->state = SKD_DRVR_STATE_PAUSED;
2776 wake_up_interruptible(&skdev->waitq);
2777 }
2778
2779 return rc;
2780}
2781
2782static void skd_complete_other(struct skd_device *skdev,
2783 volatile struct fit_completion_entry_v1 *skcomp,
2784 volatile struct fit_comp_error_info *skerr)
2785{
2786 u32 req_id = 0;
2787 u32 req_table;
2788 u32 req_slot;
2789 struct skd_special_context *skspcl;
2790
2791 req_id = skcomp->tag;
2792 req_table = req_id & SKD_ID_TABLE_MASK;
2793 req_slot = req_id & SKD_ID_SLOT_MASK;
2794
rchinthekindi2e44b422013-10-24 12:51:23 +01002795 pr_debug("%s:%s:%d table=0x%x id=0x%x slot=%d\n",
2796 skdev->name, __func__, __LINE__,
2797 req_table, req_id, req_slot);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002798
2799 /*
2800 * Based on the request id, determine how to dispatch this completion.
2801 * This swich/case is finding the good cases and forwarding the
2802 * completion entry. Errors are reported below the switch.
2803 */
2804 switch (req_table) {
2805 case SKD_ID_RW_REQUEST:
2806 /*
2807 * The caller, skd_completion_posted_isr() above,
2808 * handles r/w requests. The only way we get here
2809 * is if the req_slot is out of bounds.
2810 */
2811 break;
2812
2813 case SKD_ID_SPECIAL_REQUEST:
2814 /*
2815 * Make sure the req_slot is in bounds and that the id
2816 * matches.
2817 */
2818 if (req_slot < skdev->n_special) {
2819 skspcl = &skdev->skspcl_table[req_slot];
2820 if (skspcl->req.id == req_id &&
2821 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2822 skd_complete_special(skdev,
2823 skcomp, skerr, skspcl);
2824 return;
2825 }
2826 }
2827 break;
2828
2829 case SKD_ID_INTERNAL:
2830 if (req_slot == 0) {
2831 skspcl = &skdev->internal_skspcl;
2832 if (skspcl->req.id == req_id &&
2833 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2834 skd_complete_internal(skdev,
2835 skcomp, skerr, skspcl);
2836 return;
2837 }
2838 }
2839 break;
2840
2841 case SKD_ID_FIT_MSG:
2842 /*
2843 * These id's should never appear in a completion record.
2844 */
2845 break;
2846
2847 default:
2848 /*
2849 * These id's should never appear anywhere;
2850 */
2851 break;
2852 }
2853
2854 /*
2855 * If we get here it is a bad or stale id.
2856 */
2857}
2858
2859static void skd_complete_special(struct skd_device *skdev,
2860 volatile struct fit_completion_entry_v1
2861 *skcomp,
2862 volatile struct fit_comp_error_info *skerr,
2863 struct skd_special_context *skspcl)
2864{
rchinthekindi2e44b422013-10-24 12:51:23 +01002865 pr_debug("%s:%s:%d completing special request %p\n",
2866 skdev->name, __func__, __LINE__, skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002867 if (skspcl->orphaned) {
2868 /* Discard orphaned request */
2869 /* ?: Can this release directly or does it need
2870 * to use a worker? */
rchinthekindi2e44b422013-10-24 12:51:23 +01002871 pr_debug("%s:%s:%d release orphaned %p\n",
2872 skdev->name, __func__, __LINE__, skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002873 skd_release_special(skdev, skspcl);
2874 return;
2875 }
2876
2877 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2878
2879 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2880 skspcl->req.completion = *skcomp;
2881 skspcl->req.err_info = *skerr;
2882
2883 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2884 skerr->code, skerr->qual, skerr->fruc);
2885
2886 wake_up_interruptible(&skdev->waitq);
2887}
2888
2889/* assume spinlock is already held */
2890static void skd_release_special(struct skd_device *skdev,
2891 struct skd_special_context *skspcl)
2892{
2893 int i, was_depleted;
2894
2895 for (i = 0; i < skspcl->req.n_sg; i++) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002896 struct page *page = sg_page(&skspcl->req.sg[i]);
2897 __free_page(page);
2898 }
2899
2900 was_depleted = (skdev->skspcl_free_list == NULL);
2901
2902 skspcl->req.state = SKD_REQ_STATE_IDLE;
2903 skspcl->req.id += SKD_ID_INCR;
2904 skspcl->req.next =
2905 (struct skd_request_context *)skdev->skspcl_free_list;
2906 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2907
2908 if (was_depleted) {
rchinthekindi2e44b422013-10-24 12:51:23 +01002909 pr_debug("%s:%s:%d skspcl was depleted\n",
2910 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002911 /* Free list was depleted. Their might be waiters. */
2912 wake_up_interruptible(&skdev->waitq);
2913 }
2914}
2915
2916static void skd_reset_skcomp(struct skd_device *skdev)
2917{
2918 u32 nbytes;
2919 struct fit_completion_entry_v1 *skcomp;
2920
2921 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
2922 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
2923
2924 memset(skdev->skcomp_table, 0, nbytes);
2925
2926 skdev->skcomp_ix = 0;
2927 skdev->skcomp_cycle = 1;
2928}
2929
2930/*
2931 *****************************************************************************
2932 * INTERRUPTS
2933 *****************************************************************************
2934 */
2935static void skd_completion_worker(struct work_struct *work)
2936{
2937 struct skd_device *skdev =
2938 container_of(work, struct skd_device, completion_worker);
2939 unsigned long flags;
2940 int flush_enqueued = 0;
2941
2942 spin_lock_irqsave(&skdev->lock, flags);
2943
2944 /*
2945 * pass in limit=0, which means no limit..
2946 * process everything in compq
2947 */
2948 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2949 skd_request_fn(skdev->queue);
2950
2951 spin_unlock_irqrestore(&skdev->lock, flags);
2952}
2953
2954static void skd_isr_msg_from_dev(struct skd_device *skdev);
2955
2956irqreturn_t
2957static skd_isr(int irq, void *ptr)
2958{
2959 struct skd_device *skdev;
2960 u32 intstat;
2961 u32 ack;
2962 int rc = 0;
2963 int deferred = 0;
2964 int flush_enqueued = 0;
2965
2966 skdev = (struct skd_device *)ptr;
2967 spin_lock(&skdev->lock);
2968
2969 for (;; ) {
2970 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2971
2972 ack = FIT_INT_DEF_MASK;
2973 ack &= intstat;
2974
rchinthekindi2e44b422013-10-24 12:51:23 +01002975 pr_debug("%s:%s:%d intstat=0x%x ack=0x%x\n",
2976 skdev->name, __func__, __LINE__, intstat, ack);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06002977
2978 /* As long as there is an int pending on device, keep
2979 * running loop. When none, get out, but if we've never
2980 * done any processing, call completion handler?
2981 */
2982 if (ack == 0) {
2983 /* No interrupts on device, but run the completion
2984 * processor anyway?
2985 */
2986 if (rc == 0)
2987 if (likely (skdev->state
2988 == SKD_DRVR_STATE_ONLINE))
2989 deferred = 1;
2990 break;
2991 }
2992
2993 rc = IRQ_HANDLED;
2994
2995 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2996
2997 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
2998 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
2999 if (intstat & FIT_ISH_COMPLETION_POSTED) {
3000 /*
3001 * If we have already deferred completion
3002 * processing, don't bother running it again
3003 */
3004 if (deferred == 0)
3005 deferred =
3006 skd_isr_completion_posted(skdev,
3007 skd_isr_comp_limit, &flush_enqueued);
3008 }
3009
3010 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
3011 skd_isr_fwstate(skdev);
3012 if (skdev->state == SKD_DRVR_STATE_FAULT ||
3013 skdev->state ==
3014 SKD_DRVR_STATE_DISAPPEARED) {
3015 spin_unlock(&skdev->lock);
3016 return rc;
3017 }
3018 }
3019
3020 if (intstat & FIT_ISH_MSG_FROM_DEV)
3021 skd_isr_msg_from_dev(skdev);
3022 }
3023 }
3024
3025 if (unlikely(flush_enqueued))
3026 skd_request_fn(skdev->queue);
3027
3028 if (deferred)
3029 schedule_work(&skdev->completion_worker);
3030 else if (!flush_enqueued)
3031 skd_request_fn(skdev->queue);
3032
3033 spin_unlock(&skdev->lock);
3034
3035 return rc;
3036}
3037
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003038static void skd_drive_fault(struct skd_device *skdev)
3039{
3040 skdev->state = SKD_DRVR_STATE_FAULT;
3041 pr_err("(%s): Drive FAULT\n", skd_name(skdev));
3042}
3043
3044static void skd_drive_disappeared(struct skd_device *skdev)
3045{
3046 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
3047 pr_err("(%s): Drive DISAPPEARED\n", skd_name(skdev));
3048}
3049
3050static void skd_isr_fwstate(struct skd_device *skdev)
3051{
3052 u32 sense;
3053 u32 state;
3054 u32 mtd;
3055 int prev_driver_state = skdev->state;
3056
3057 sense = SKD_READL(skdev, FIT_STATUS);
3058 state = sense & FIT_SR_DRIVE_STATE_MASK;
3059
3060 pr_err("(%s): s1120 state %s(%d)=>%s(%d)\n",
3061 skd_name(skdev),
3062 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3063 skd_drive_state_to_str(state), state);
3064
3065 skdev->drive_state = state;
3066
3067 switch (skdev->drive_state) {
3068 case FIT_SR_DRIVE_INIT:
3069 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
3070 skd_disable_interrupts(skdev);
3071 break;
3072 }
3073 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
3074 skd_recover_requests(skdev, 0);
3075 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
3076 skdev->timer_countdown = SKD_STARTING_TIMO;
3077 skdev->state = SKD_DRVR_STATE_STARTING;
3078 skd_soft_reset(skdev);
3079 break;
3080 }
3081 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
3082 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3083 skdev->last_mtd = mtd;
3084 break;
3085
3086 case FIT_SR_DRIVE_ONLINE:
3087 skdev->cur_max_queue_depth = skd_max_queue_depth;
3088 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
3089 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
3090
3091 skdev->queue_low_water_mark =
3092 skdev->cur_max_queue_depth * 2 / 3 + 1;
3093 if (skdev->queue_low_water_mark < 1)
3094 skdev->queue_low_water_mark = 1;
3095 pr_info(
3096 "(%s): Queue depth limit=%d dev=%d lowat=%d\n",
3097 skd_name(skdev),
3098 skdev->cur_max_queue_depth,
3099 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3100
3101 skd_refresh_device_data(skdev);
3102 break;
3103
3104 case FIT_SR_DRIVE_BUSY:
3105 skdev->state = SKD_DRVR_STATE_BUSY;
3106 skdev->timer_countdown = SKD_BUSY_TIMO;
3107 skd_quiesce_dev(skdev);
3108 break;
3109 case FIT_SR_DRIVE_BUSY_SANITIZE:
3110 /* set timer for 3 seconds, we'll abort any unfinished
3111 * commands after that expires
3112 */
3113 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3114 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003115 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003116 break;
3117 case FIT_SR_DRIVE_BUSY_ERASE:
3118 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3119 skdev->timer_countdown = SKD_BUSY_TIMO;
3120 break;
3121 case FIT_SR_DRIVE_OFFLINE:
3122 skdev->state = SKD_DRVR_STATE_IDLE;
3123 break;
3124 case FIT_SR_DRIVE_SOFT_RESET:
3125 switch (skdev->state) {
3126 case SKD_DRVR_STATE_STARTING:
3127 case SKD_DRVR_STATE_RESTARTING:
3128 /* Expected by a caller of skd_soft_reset() */
3129 break;
3130 default:
3131 skdev->state = SKD_DRVR_STATE_RESTARTING;
3132 break;
3133 }
3134 break;
3135 case FIT_SR_DRIVE_FW_BOOTING:
rchinthekindi2e44b422013-10-24 12:51:23 +01003136 pr_debug("%s:%s:%d ISR FIT_SR_DRIVE_FW_BOOTING %s\n",
3137 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003138 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3139 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3140 break;
3141
3142 case FIT_SR_DRIVE_DEGRADED:
3143 case FIT_SR_PCIE_LINK_DOWN:
3144 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3145 break;
3146
3147 case FIT_SR_DRIVE_FAULT:
3148 skd_drive_fault(skdev);
3149 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003150 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003151 break;
3152
3153 /* PCIe bus returned all Fs? */
3154 case 0xFF:
3155 pr_info("(%s): state=0x%x sense=0x%x\n",
3156 skd_name(skdev), state, sense);
3157 skd_drive_disappeared(skdev);
3158 skd_recover_requests(skdev, 0);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003159 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003160 break;
3161 default:
3162 /*
3163 * Uknown FW State. Wait for a state we recognize.
3164 */
3165 break;
3166 }
3167 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3168 skd_name(skdev),
3169 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3170 skd_skdev_state_to_str(skdev->state), skdev->state);
3171}
3172
3173static void skd_recover_requests(struct skd_device *skdev, int requeue)
3174{
3175 int i;
3176
3177 for (i = 0; i < skdev->num_req_context; i++) {
3178 struct skd_request_context *skreq = &skdev->skreq_table[i];
3179
3180 if (skreq->state == SKD_REQ_STATE_BUSY) {
3181 skd_log_skreq(skdev, skreq, "recover");
3182
3183 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003184 SKD_ASSERT(skreq->req != NULL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003185
3186 /* Release DMA resources for the request. */
3187 if (skreq->n_sg > 0)
3188 skd_postop_sg_list(skdev, skreq);
3189
Jens Axboefcd37eb2013-11-01 10:14:56 -06003190 if (requeue &&
3191 (unsigned long) ++skreq->req->special <
3192 SKD_MAX_RETRIES)
Mike Snitzer38d4a1b2013-11-01 15:05:10 -04003193 blk_requeue_request(skdev->queue, skreq->req);
Jens Axboefcd37eb2013-11-01 10:14:56 -06003194 else
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003195 skd_end_request(skdev, skreq, -EIO);
3196
Jens Axboefcd37eb2013-11-01 10:14:56 -06003197 skreq->req = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003198
3199 skreq->state = SKD_REQ_STATE_IDLE;
3200 skreq->id += SKD_ID_INCR;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003201 }
3202 if (i > 0)
3203 skreq[-1].next = skreq;
3204 skreq->next = NULL;
3205 }
3206 skdev->skreq_free_list = skdev->skreq_table;
3207
3208 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3209 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3210
3211 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3212 skd_log_skmsg(skdev, skmsg, "salvaged");
3213 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3214 skmsg->state = SKD_MSG_STATE_IDLE;
3215 skmsg->id += SKD_ID_INCR;
3216 }
3217 if (i > 0)
3218 skmsg[-1].next = skmsg;
3219 skmsg->next = NULL;
3220 }
3221 skdev->skmsg_free_list = skdev->skmsg_table;
3222
3223 for (i = 0; i < skdev->n_special; i++) {
3224 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3225
3226 /* If orphaned, reclaim it because it has already been reported
3227 * to the process as an error (it was just waiting for
3228 * a completion that didn't come, and now it will never come)
3229 * If busy, change to a state that will cause it to error
3230 * out in the wait routine and let it do the normal
3231 * reporting and reclaiming
3232 */
3233 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3234 if (skspcl->orphaned) {
rchinthekindi2e44b422013-10-24 12:51:23 +01003235 pr_debug("%s:%s:%d orphaned %p\n",
3236 skdev->name, __func__, __LINE__,
3237 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003238 skd_release_special(skdev, skspcl);
3239 } else {
rchinthekindi2e44b422013-10-24 12:51:23 +01003240 pr_debug("%s:%s:%d not orphaned %p\n",
3241 skdev->name, __func__, __LINE__,
3242 skspcl);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003243 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3244 }
3245 }
3246 }
3247 skdev->skspcl_free_list = skdev->skspcl_table;
3248
3249 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3250 skdev->timeout_slot[i] = 0;
3251
3252 skdev->in_flight = 0;
3253}
3254
3255static void skd_isr_msg_from_dev(struct skd_device *skdev)
3256{
3257 u32 mfd;
3258 u32 mtd;
3259 u32 data;
3260
3261 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3262
rchinthekindi2e44b422013-10-24 12:51:23 +01003263 pr_debug("%s:%s:%d mfd=0x%x last_mtd=0x%x\n",
3264 skdev->name, __func__, __LINE__, mfd, skdev->last_mtd);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003265
3266 /* ignore any mtd that is an ack for something we didn't send */
3267 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3268 return;
3269
3270 switch (FIT_MXD_TYPE(mfd)) {
3271 case FIT_MTD_FITFW_INIT:
3272 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3273
3274 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
3275 pr_err("(%s): protocol mismatch\n",
3276 skdev->name);
3277 pr_err("(%s): got=%d support=%d\n",
3278 skdev->name, skdev->proto_ver,
3279 FIT_PROTOCOL_VERSION_1);
3280 pr_err("(%s): please upgrade driver\n",
3281 skdev->name);
3282 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3283 skd_soft_reset(skdev);
3284 break;
3285 }
3286 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3287 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3288 skdev->last_mtd = mtd;
3289 break;
3290
3291 case FIT_MTD_GET_CMDQ_DEPTH:
3292 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3293 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3294 SKD_N_COMPLETION_ENTRY);
3295 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3296 skdev->last_mtd = mtd;
3297 break;
3298
3299 case FIT_MTD_SET_COMPQ_DEPTH:
3300 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3301 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3302 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3303 skdev->last_mtd = mtd;
3304 break;
3305
3306 case FIT_MTD_SET_COMPQ_ADDR:
3307 skd_reset_skcomp(skdev);
3308 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3309 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3310 skdev->last_mtd = mtd;
3311 break;
3312
3313 case FIT_MTD_CMD_LOG_HOST_ID:
3314 skdev->connect_time_stamp = get_seconds();
3315 data = skdev->connect_time_stamp & 0xFFFF;
3316 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3317 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3318 skdev->last_mtd = mtd;
3319 break;
3320
3321 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3322 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3323 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3324 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3325 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3326 skdev->last_mtd = mtd;
3327 break;
3328
3329 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3330 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3331 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3332 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3333 skdev->last_mtd = mtd;
3334
3335 pr_err("(%s): Time sync driver=0x%x device=0x%x\n",
3336 skd_name(skdev),
3337 skdev->connect_time_stamp, skdev->drive_jiffies);
3338 break;
3339
3340 case FIT_MTD_ARM_QUEUE:
3341 skdev->last_mtd = 0;
3342 /*
3343 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3344 */
3345 break;
3346
3347 default:
3348 break;
3349 }
3350}
3351
3352static void skd_disable_interrupts(struct skd_device *skdev)
3353{
3354 u32 sense;
3355
3356 sense = SKD_READL(skdev, FIT_CONTROL);
3357 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3358 SKD_WRITEL(skdev, sense, FIT_CONTROL);
rchinthekindi2e44b422013-10-24 12:51:23 +01003359 pr_debug("%s:%s:%d sense 0x%x\n",
3360 skdev->name, __func__, __LINE__, sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003361
3362 /* Note that the 1s is written. A 1-bit means
3363 * disable, a 0 means enable.
3364 */
3365 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3366}
3367
3368static void skd_enable_interrupts(struct skd_device *skdev)
3369{
3370 u32 val;
3371
3372 /* unmask interrupts first */
3373 val = FIT_ISH_FW_STATE_CHANGE +
3374 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3375
3376 /* Note that the compliment of mask is written. A 1-bit means
3377 * disable, a 0 means enable. */
3378 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003379 pr_debug("%s:%s:%d interrupt mask=0x%x\n",
3380 skdev->name, __func__, __LINE__, ~val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003381
3382 val = SKD_READL(skdev, FIT_CONTROL);
3383 val |= FIT_CR_ENABLE_INTERRUPTS;
rchinthekindi2e44b422013-10-24 12:51:23 +01003384 pr_debug("%s:%s:%d control=0x%x\n",
3385 skdev->name, __func__, __LINE__, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003386 SKD_WRITEL(skdev, val, FIT_CONTROL);
3387}
3388
3389/*
3390 *****************************************************************************
3391 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3392 *****************************************************************************
3393 */
3394
3395static void skd_soft_reset(struct skd_device *skdev)
3396{
3397 u32 val;
3398
3399 val = SKD_READL(skdev, FIT_CONTROL);
3400 val |= (FIT_CR_SOFT_RESET);
rchinthekindi2e44b422013-10-24 12:51:23 +01003401 pr_debug("%s:%s:%d control=0x%x\n",
3402 skdev->name, __func__, __LINE__, val);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003403 SKD_WRITEL(skdev, val, FIT_CONTROL);
3404}
3405
3406static void skd_start_device(struct skd_device *skdev)
3407{
3408 unsigned long flags;
3409 u32 sense;
3410 u32 state;
3411
3412 spin_lock_irqsave(&skdev->lock, flags);
3413
3414 /* ack all ghost interrupts */
3415 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3416
3417 sense = SKD_READL(skdev, FIT_STATUS);
3418
rchinthekindi2e44b422013-10-24 12:51:23 +01003419 pr_debug("%s:%s:%d initial status=0x%x\n",
3420 skdev->name, __func__, __LINE__, sense);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003421
3422 state = sense & FIT_SR_DRIVE_STATE_MASK;
3423 skdev->drive_state = state;
3424 skdev->last_mtd = 0;
3425
3426 skdev->state = SKD_DRVR_STATE_STARTING;
3427 skdev->timer_countdown = SKD_STARTING_TIMO;
3428
3429 skd_enable_interrupts(skdev);
3430
3431 switch (skdev->drive_state) {
3432 case FIT_SR_DRIVE_OFFLINE:
3433 pr_err("(%s): Drive offline...\n", skd_name(skdev));
3434 break;
3435
3436 case FIT_SR_DRIVE_FW_BOOTING:
rchinthekindi2e44b422013-10-24 12:51:23 +01003437 pr_debug("%s:%s:%d FIT_SR_DRIVE_FW_BOOTING %s\n",
3438 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003439 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3440 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3441 break;
3442
3443 case FIT_SR_DRIVE_BUSY_SANITIZE:
3444 pr_info("(%s): Start: BUSY_SANITIZE\n",
3445 skd_name(skdev));
3446 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3447 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3448 break;
3449
3450 case FIT_SR_DRIVE_BUSY_ERASE:
3451 pr_info("(%s): Start: BUSY_ERASE\n", skd_name(skdev));
3452 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3453 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3454 break;
3455
3456 case FIT_SR_DRIVE_INIT:
3457 case FIT_SR_DRIVE_ONLINE:
3458 skd_soft_reset(skdev);
3459 break;
3460
3461 case FIT_SR_DRIVE_BUSY:
3462 pr_err("(%s): Drive Busy...\n", skd_name(skdev));
3463 skdev->state = SKD_DRVR_STATE_BUSY;
3464 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3465 break;
3466
3467 case FIT_SR_DRIVE_SOFT_RESET:
3468 pr_err("(%s) drive soft reset in prog\n",
3469 skd_name(skdev));
3470 break;
3471
3472 case FIT_SR_DRIVE_FAULT:
3473 /* Fault state is bad...soft reset won't do it...
3474 * Hard reset, maybe, but does it work on device?
3475 * For now, just fault so the system doesn't hang.
3476 */
3477 skd_drive_fault(skdev);
3478 /*start the queue so we can respond with error to requests */
rchinthekindi2e44b422013-10-24 12:51:23 +01003479 pr_debug("%s:%s:%d starting %s queue\n",
3480 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003481 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003482 skdev->gendisk_on = -1;
3483 wake_up_interruptible(&skdev->waitq);
3484 break;
3485
3486 case 0xFF:
3487 /* Most likely the device isn't there or isn't responding
3488 * to the BAR1 addresses. */
3489 skd_drive_disappeared(skdev);
3490 /*start the queue so we can respond with error to requests */
rchinthekindi2e44b422013-10-24 12:51:23 +01003491 pr_debug("%s:%s:%d starting %s queue to error-out reqs\n",
3492 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003493 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003494 skdev->gendisk_on = -1;
3495 wake_up_interruptible(&skdev->waitq);
3496 break;
3497
3498 default:
3499 pr_err("(%s) Start: unknown state %x\n",
3500 skd_name(skdev), skdev->drive_state);
3501 break;
3502 }
3503
3504 state = SKD_READL(skdev, FIT_CONTROL);
rchinthekindi2e44b422013-10-24 12:51:23 +01003505 pr_debug("%s:%s:%d FIT Control Status=0x%x\n",
3506 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003507
3508 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003509 pr_debug("%s:%s:%d Intr Status=0x%x\n",
3510 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003511
3512 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
rchinthekindi2e44b422013-10-24 12:51:23 +01003513 pr_debug("%s:%s:%d Intr Mask=0x%x\n",
3514 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003515
3516 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
rchinthekindi2e44b422013-10-24 12:51:23 +01003517 pr_debug("%s:%s:%d Msg from Dev=0x%x\n",
3518 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003519
3520 state = SKD_READL(skdev, FIT_HW_VERSION);
rchinthekindi2e44b422013-10-24 12:51:23 +01003521 pr_debug("%s:%s:%d HW version=0x%x\n",
3522 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003523
3524 spin_unlock_irqrestore(&skdev->lock, flags);
3525}
3526
3527static void skd_stop_device(struct skd_device *skdev)
3528{
3529 unsigned long flags;
3530 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3531 u32 dev_state;
3532 int i;
3533
3534 spin_lock_irqsave(&skdev->lock, flags);
3535
3536 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
3537 pr_err("(%s): skd_stop_device not online no sync\n",
3538 skd_name(skdev));
3539 goto stop_out;
3540 }
3541
3542 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
3543 pr_err("(%s): skd_stop_device no special\n",
3544 skd_name(skdev));
3545 goto stop_out;
3546 }
3547
3548 skdev->state = SKD_DRVR_STATE_SYNCING;
3549 skdev->sync_done = 0;
3550
3551 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3552
3553 spin_unlock_irqrestore(&skdev->lock, flags);
3554
3555 wait_event_interruptible_timeout(skdev->waitq,
3556 (skdev->sync_done), (10 * HZ));
3557
3558 spin_lock_irqsave(&skdev->lock, flags);
3559
3560 switch (skdev->sync_done) {
3561 case 0:
3562 pr_err("(%s): skd_stop_device no sync\n",
3563 skd_name(skdev));
3564 break;
3565 case 1:
3566 pr_err("(%s): skd_stop_device sync done\n",
3567 skd_name(skdev));
3568 break;
3569 default:
3570 pr_err("(%s): skd_stop_device sync error\n",
3571 skd_name(skdev));
3572 }
3573
3574stop_out:
3575 skdev->state = SKD_DRVR_STATE_STOPPING;
3576 spin_unlock_irqrestore(&skdev->lock, flags);
3577
3578 skd_kill_timer(skdev);
3579
3580 spin_lock_irqsave(&skdev->lock, flags);
3581 skd_disable_interrupts(skdev);
3582
3583 /* ensure all ints on device are cleared */
3584 /* soft reset the device to unload with a clean slate */
3585 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3586 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3587
3588 spin_unlock_irqrestore(&skdev->lock, flags);
3589
3590 /* poll every 100ms, 1 second timeout */
3591 for (i = 0; i < 10; i++) {
3592 dev_state =
3593 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3594 if (dev_state == FIT_SR_DRIVE_INIT)
3595 break;
3596 set_current_state(TASK_INTERRUPTIBLE);
3597 schedule_timeout(msecs_to_jiffies(100));
3598 }
3599
3600 if (dev_state != FIT_SR_DRIVE_INIT)
3601 pr_err("(%s): skd_stop_device state error 0x%02x\n",
3602 skd_name(skdev), dev_state);
3603}
3604
3605/* assume spinlock is held */
3606static void skd_restart_device(struct skd_device *skdev)
3607{
3608 u32 state;
3609
3610 /* ack all ghost interrupts */
3611 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3612
3613 state = SKD_READL(skdev, FIT_STATUS);
3614
rchinthekindi2e44b422013-10-24 12:51:23 +01003615 pr_debug("%s:%s:%d drive status=0x%x\n",
3616 skdev->name, __func__, __LINE__, state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003617
3618 state &= FIT_SR_DRIVE_STATE_MASK;
3619 skdev->drive_state = state;
3620 skdev->last_mtd = 0;
3621
3622 skdev->state = SKD_DRVR_STATE_RESTARTING;
3623 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3624
3625 skd_soft_reset(skdev);
3626}
3627
3628/* assume spinlock is held */
3629static int skd_quiesce_dev(struct skd_device *skdev)
3630{
3631 int rc = 0;
3632
3633 switch (skdev->state) {
3634 case SKD_DRVR_STATE_BUSY:
3635 case SKD_DRVR_STATE_BUSY_IMMINENT:
rchinthekindi2e44b422013-10-24 12:51:23 +01003636 pr_debug("%s:%s:%d stopping %s queue\n",
3637 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06003638 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003639 break;
3640 case SKD_DRVR_STATE_ONLINE:
3641 case SKD_DRVR_STATE_STOPPING:
3642 case SKD_DRVR_STATE_SYNCING:
3643 case SKD_DRVR_STATE_PAUSING:
3644 case SKD_DRVR_STATE_PAUSED:
3645 case SKD_DRVR_STATE_STARTING:
3646 case SKD_DRVR_STATE_RESTARTING:
3647 case SKD_DRVR_STATE_RESUMING:
3648 default:
3649 rc = -EINVAL;
rchinthekindi2e44b422013-10-24 12:51:23 +01003650 pr_debug("%s:%s:%d state [%d] not implemented\n",
3651 skdev->name, __func__, __LINE__, skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003652 }
3653 return rc;
3654}
3655
3656/* assume spinlock is held */
3657static int skd_unquiesce_dev(struct skd_device *skdev)
3658{
3659 int prev_driver_state = skdev->state;
3660
3661 skd_log_skdev(skdev, "unquiesce");
3662 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
rchinthekindi2e44b422013-10-24 12:51:23 +01003663 pr_debug("%s:%s:%d **** device already ONLINE\n",
3664 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003665 return 0;
3666 }
3667 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3668 /*
3669 * If there has been an state change to other than
3670 * ONLINE, we will rely on controller state change
3671 * to come back online and restart the queue.
3672 * The BUSY state means that driver is ready to
3673 * continue normal processing but waiting for controller
3674 * to become available.
3675 */
3676 skdev->state = SKD_DRVR_STATE_BUSY;
rchinthekindi2e44b422013-10-24 12:51:23 +01003677 pr_debug("%s:%s:%d drive BUSY state\n",
3678 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003679 return 0;
3680 }
3681
3682 /*
3683 * Drive has just come online, driver is either in startup,
3684 * paused performing a task, or bust waiting for hardware.
3685 */
3686 switch (skdev->state) {
3687 case SKD_DRVR_STATE_PAUSED:
3688 case SKD_DRVR_STATE_BUSY:
3689 case SKD_DRVR_STATE_BUSY_IMMINENT:
3690 case SKD_DRVR_STATE_BUSY_ERASE:
3691 case SKD_DRVR_STATE_STARTING:
3692 case SKD_DRVR_STATE_RESTARTING:
3693 case SKD_DRVR_STATE_FAULT:
3694 case SKD_DRVR_STATE_IDLE:
3695 case SKD_DRVR_STATE_LOAD:
3696 skdev->state = SKD_DRVR_STATE_ONLINE;
3697 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3698 skd_name(skdev),
3699 skd_skdev_state_to_str(prev_driver_state),
3700 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3701 skdev->state);
rchinthekindi2e44b422013-10-24 12:51:23 +01003702 pr_debug("%s:%s:%d **** device ONLINE...starting block queue\n",
3703 skdev->name, __func__, __LINE__);
3704 pr_debug("%s:%s:%d starting %s queue\n",
3705 skdev->name, __func__, __LINE__, skdev->name);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003706 pr_info("(%s): STEC s1120 ONLINE\n", skd_name(skdev));
Jens Axboe6a5ec652013-11-01 10:38:45 -06003707 blk_start_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003708 skdev->gendisk_on = 1;
3709 wake_up_interruptible(&skdev->waitq);
3710 break;
3711
3712 case SKD_DRVR_STATE_DISAPPEARED:
3713 default:
rchinthekindi2e44b422013-10-24 12:51:23 +01003714 pr_debug("%s:%s:%d **** driver state %d, not implemented \n",
3715 skdev->name, __func__, __LINE__,
3716 skdev->state);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003717 return -EBUSY;
3718 }
3719 return 0;
3720}
3721
3722/*
3723 *****************************************************************************
3724 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3725 *****************************************************************************
3726 */
3727
3728static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3729{
3730 struct skd_device *skdev = skd_host_data;
3731 unsigned long flags;
3732
3733 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003734 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3735 skdev->name, __func__, __LINE__,
3736 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003737 pr_err("(%s): MSIX reserved irq %d = 0x%x\n", skd_name(skdev),
3738 irq, SKD_READL(skdev, FIT_INT_STATUS_HOST));
3739 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3740 spin_unlock_irqrestore(&skdev->lock, flags);
3741 return IRQ_HANDLED;
3742}
3743
3744static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3745{
3746 struct skd_device *skdev = skd_host_data;
3747 unsigned long flags;
3748
3749 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003750 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3751 skdev->name, __func__, __LINE__,
3752 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003753 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3754 skd_isr_fwstate(skdev);
3755 spin_unlock_irqrestore(&skdev->lock, flags);
3756 return IRQ_HANDLED;
3757}
3758
3759static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3760{
3761 struct skd_device *skdev = skd_host_data;
3762 unsigned long flags;
3763 int flush_enqueued = 0;
3764 int deferred;
3765
3766 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003767 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3768 skdev->name, __func__, __LINE__,
3769 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003770 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3771 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3772 &flush_enqueued);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003773 if (flush_enqueued)
3774 skd_request_fn(skdev->queue);
3775
3776 if (deferred)
3777 schedule_work(&skdev->completion_worker);
3778 else if (!flush_enqueued)
3779 skd_request_fn(skdev->queue);
3780
3781 spin_unlock_irqrestore(&skdev->lock, flags);
3782
3783 return IRQ_HANDLED;
3784}
3785
3786static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3787{
3788 struct skd_device *skdev = skd_host_data;
3789 unsigned long flags;
3790
3791 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003792 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3793 skdev->name, __func__, __LINE__,
3794 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003795 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3796 skd_isr_msg_from_dev(skdev);
3797 spin_unlock_irqrestore(&skdev->lock, flags);
3798 return IRQ_HANDLED;
3799}
3800
3801static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3802{
3803 struct skd_device *skdev = skd_host_data;
3804 unsigned long flags;
3805
3806 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01003807 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3808 skdev->name, __func__, __LINE__,
3809 SKD_READL(skdev, FIT_INT_STATUS_HOST));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003810 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3811 spin_unlock_irqrestore(&skdev->lock, flags);
3812 return IRQ_HANDLED;
3813}
3814
3815/*
3816 *****************************************************************************
3817 * PCIe MSI/MSI-X SETUP
3818 *****************************************************************************
3819 */
3820
3821struct skd_msix_entry {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003822 char isr_name[30];
3823};
3824
3825struct skd_init_msix_entry {
3826 const char *name;
3827 irq_handler_t handler;
3828};
3829
3830#define SKD_MAX_MSIX_COUNT 13
3831#define SKD_MIN_MSIX_COUNT 7
3832#define SKD_BASE_MSIX_IRQ 4
3833
3834static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3835 { "(DMA 0)", skd_reserved_isr },
3836 { "(DMA 1)", skd_reserved_isr },
3837 { "(DMA 2)", skd_reserved_isr },
3838 { "(DMA 3)", skd_reserved_isr },
3839 { "(State Change)", skd_statec_isr },
3840 { "(COMPL_Q)", skd_comp_q },
3841 { "(MSG)", skd_msg_isr },
3842 { "(Reserved)", skd_reserved_isr },
3843 { "(Reserved)", skd_reserved_isr },
3844 { "(Queue Full 0)", skd_qfull_isr },
3845 { "(Queue Full 1)", skd_qfull_isr },
3846 { "(Queue Full 2)", skd_qfull_isr },
3847 { "(Queue Full 3)", skd_qfull_isr },
3848};
3849
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003850static int skd_acquire_msix(struct skd_device *skdev)
3851{
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003852 int i, rc;
Alexander Gordeev46817762014-02-19 09:58:19 +01003853 struct pci_dev *pdev = skdev->pdev;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003854
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003855 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
3856 PCI_IRQ_MSIX);
3857 if (rc < 0) {
Alexander Gordeeva9df8622014-02-19 09:58:21 +01003858 pr_err("(%s): failed to enable MSI-X %d\n",
3859 skd_name(skdev), rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003860 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003861 }
Alexander Gordeev46817762014-02-19 09:58:19 +01003862
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003863 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
3864 sizeof(struct skd_msix_entry), GFP_KERNEL);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003865 if (!skdev->msix_entries) {
3866 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003867 pr_err("(%s): msix table allocation error\n",
3868 skd_name(skdev));
3869 goto msix_out;
3870 }
3871
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003872 /* Enable MSI-X vectors for the base queue */
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003873 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3874 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
3875
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003876 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3877 "%s%d-msix %s", DRV_NAME, skdev->devno,
3878 msix_entries[i].name);
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003879
3880 rc = devm_request_irq(&skdev->pdev->dev,
3881 pci_irq_vector(skdev->pdev, i),
3882 msix_entries[i].handler, 0,
3883 qentry->isr_name, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003884 if (rc) {
3885 pr_err("(%s): Unable to register(%d) MSI-X "
3886 "handler %d: %s\n",
3887 skd_name(skdev), rc, i, qentry->isr_name);
3888 goto msix_out;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003889 }
3890 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003891
rchinthekindi2e44b422013-10-24 12:51:23 +01003892 pr_debug("%s:%s:%d %s: <%s> msix %d irq(s) enabled\n",
3893 skdev->name, __func__, __LINE__,
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003894 pci_name(pdev), skdev->name, SKD_MAX_MSIX_COUNT);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003895 return 0;
3896
3897msix_out:
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003898 while (--i >= 0)
3899 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
3900 kfree(skdev->msix_entries);
3901 skdev->msix_entries = NULL;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003902 return rc;
3903}
3904
3905static int skd_acquire_irq(struct skd_device *skdev)
3906{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003907 struct pci_dev *pdev = skdev->pdev;
3908 unsigned int irq_flag = PCI_IRQ_LEGACY;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003909 int rc;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003910
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003911 if (skd_isr_type == SKD_IRQ_MSIX) {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003912 rc = skd_acquire_msix(skdev);
3913 if (!rc)
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003914 return 0;
3915
3916 pr_err("(%s): failed to enable MSI-X, re-trying with MSI %d\n",
3917 skd_name(skdev), rc);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003918 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003919
3920 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
3921 skdev->devno);
3922
3923 if (skd_isr_type != SKD_IRQ_LEGACY)
3924 irq_flag |= PCI_IRQ_MSI;
3925 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
3926 if (rc < 0) {
3927 pr_err("(%s): failed to allocate the MSI interrupt %d\n",
3928 skd_name(skdev), rc);
3929 return rc;
3930 }
3931
3932 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
3933 pdev->msi_enabled ? 0 : IRQF_SHARED,
3934 skdev->isr_name, skdev);
3935 if (rc) {
3936 pci_free_irq_vectors(pdev);
3937 pr_err("(%s): failed to allocate interrupt %d\n",
3938 skd_name(skdev), rc);
3939 return rc;
3940 }
3941
3942 return 0;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003943}
3944
3945static void skd_release_irq(struct skd_device *skdev)
3946{
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003947 struct pci_dev *pdev = skdev->pdev;
3948
3949 if (skdev->msix_entries) {
3950 int i;
3951
3952 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
3953 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
3954 skdev);
3955 }
3956
3957 kfree(skdev->msix_entries);
3958 skdev->msix_entries = NULL;
3959 } else {
3960 devm_free_irq(&pdev->dev, pdev->irq, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003961 }
Christoph Hellwig180b0ae2016-11-07 11:14:07 -08003962
3963 pci_free_irq_vectors(pdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003964}
3965
3966/*
3967 *****************************************************************************
3968 * CONSTRUCT
3969 *****************************************************************************
3970 */
3971
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003972static int skd_cons_skcomp(struct skd_device *skdev)
3973{
3974 int rc = 0;
3975 struct fit_completion_entry_v1 *skcomp;
3976 u32 nbytes;
3977
3978 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
3979 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
3980
rchinthekindi2e44b422013-10-24 12:51:23 +01003981 pr_debug("%s:%s:%d comp pci_alloc, total bytes %d entries %d\n",
3982 skdev->name, __func__, __LINE__,
3983 nbytes, SKD_N_COMPLETION_ENTRY);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003984
Joe Perchesa5bbf612014-08-08 14:24:12 -07003985 skcomp = pci_zalloc_consistent(skdev->pdev, nbytes,
3986 &skdev->cq_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003987
3988 if (skcomp == NULL) {
3989 rc = -ENOMEM;
3990 goto err_out;
3991 }
3992
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06003993 skdev->skcomp_table = skcomp;
3994 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
3995 sizeof(*skcomp) *
3996 SKD_N_COMPLETION_ENTRY);
3997
3998err_out:
3999 return rc;
4000}
4001
4002static int skd_cons_skmsg(struct skd_device *skdev)
4003{
4004 int rc = 0;
4005 u32 i;
4006
rchinthekindi2e44b422013-10-24 12:51:23 +01004007 pr_debug("%s:%s:%d skmsg_table kzalloc, struct %lu, count %u total %lu\n",
4008 skdev->name, __func__, __LINE__,
4009 sizeof(struct skd_fitmsg_context),
4010 skdev->num_fitmsg_context,
4011 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004012
4013 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
4014 *skdev->num_fitmsg_context, GFP_KERNEL);
4015 if (skdev->skmsg_table == NULL) {
4016 rc = -ENOMEM;
4017 goto err_out;
4018 }
4019
4020 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4021 struct skd_fitmsg_context *skmsg;
4022
4023 skmsg = &skdev->skmsg_table[i];
4024
4025 skmsg->id = i + SKD_ID_FIT_MSG;
4026
4027 skmsg->state = SKD_MSG_STATE_IDLE;
4028 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
4029 SKD_N_FITMSG_BYTES + 64,
4030 &skmsg->mb_dma_address);
4031
4032 if (skmsg->msg_buf == NULL) {
4033 rc = -ENOMEM;
4034 goto err_out;
4035 }
4036
4037 skmsg->offset = (u32)((u64)skmsg->msg_buf &
4038 (~FIT_QCMD_BASE_ADDRESS_MASK));
4039 skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK;
4040 skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf &
4041 FIT_QCMD_BASE_ADDRESS_MASK);
4042 skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK;
4043 skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK;
4044 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
4045
4046 skmsg->next = &skmsg[1];
4047 }
4048
4049 /* Free list is in order starting with the 0th entry. */
4050 skdev->skmsg_table[i - 1].next = NULL;
4051 skdev->skmsg_free_list = skdev->skmsg_table;
4052
4053err_out:
4054 return rc;
4055}
4056
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004057static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
4058 u32 n_sg,
4059 dma_addr_t *ret_dma_addr)
4060{
4061 struct fit_sg_descriptor *sg_list;
4062 u32 nbytes;
4063
4064 nbytes = sizeof(*sg_list) * n_sg;
4065
4066 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
4067
4068 if (sg_list != NULL) {
4069 uint64_t dma_address = *ret_dma_addr;
4070 u32 i;
4071
4072 memset(sg_list, 0, nbytes);
4073
4074 for (i = 0; i < n_sg - 1; i++) {
4075 uint64_t ndp_off;
4076 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
4077
4078 sg_list[i].next_desc_ptr = dma_address + ndp_off;
4079 }
4080 sg_list[i].next_desc_ptr = 0LL;
4081 }
4082
4083 return sg_list;
4084}
4085
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004086static int skd_cons_skreq(struct skd_device *skdev)
4087{
4088 int rc = 0;
4089 u32 i;
4090
rchinthekindi2e44b422013-10-24 12:51:23 +01004091 pr_debug("%s:%s:%d skreq_table kzalloc, struct %lu, count %u total %lu\n",
4092 skdev->name, __func__, __LINE__,
4093 sizeof(struct skd_request_context),
4094 skdev->num_req_context,
4095 sizeof(struct skd_request_context) * skdev->num_req_context);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004096
4097 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
4098 * skdev->num_req_context, GFP_KERNEL);
4099 if (skdev->skreq_table == NULL) {
4100 rc = -ENOMEM;
4101 goto err_out;
4102 }
4103
rchinthekindi2e44b422013-10-24 12:51:23 +01004104 pr_debug("%s:%s:%d alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
4105 skdev->name, __func__, __LINE__,
4106 skdev->sgs_per_request, sizeof(struct scatterlist),
4107 skdev->sgs_per_request * sizeof(struct scatterlist));
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004108
4109 for (i = 0; i < skdev->num_req_context; i++) {
4110 struct skd_request_context *skreq;
4111
4112 skreq = &skdev->skreq_table[i];
4113
4114 skreq->id = i + SKD_ID_RW_REQUEST;
4115 skreq->state = SKD_REQ_STATE_IDLE;
4116
4117 skreq->sg = kzalloc(sizeof(struct scatterlist) *
4118 skdev->sgs_per_request, GFP_KERNEL);
4119 if (skreq->sg == NULL) {
4120 rc = -ENOMEM;
4121 goto err_out;
4122 }
4123 sg_init_table(skreq->sg, skdev->sgs_per_request);
4124
4125 skreq->sksg_list = skd_cons_sg_list(skdev,
4126 skdev->sgs_per_request,
4127 &skreq->sksg_dma_address);
4128
4129 if (skreq->sksg_list == NULL) {
4130 rc = -ENOMEM;
4131 goto err_out;
4132 }
4133
4134 skreq->next = &skreq[1];
4135 }
4136
4137 /* Free list is in order starting with the 0th entry. */
4138 skdev->skreq_table[i - 1].next = NULL;
4139 skdev->skreq_free_list = skdev->skreq_table;
4140
4141err_out:
4142 return rc;
4143}
4144
4145static int skd_cons_skspcl(struct skd_device *skdev)
4146{
4147 int rc = 0;
4148 u32 i, nbytes;
4149
rchinthekindi2e44b422013-10-24 12:51:23 +01004150 pr_debug("%s:%s:%d skspcl_table kzalloc, struct %lu, count %u total %lu\n",
4151 skdev->name, __func__, __LINE__,
4152 sizeof(struct skd_special_context),
4153 skdev->n_special,
4154 sizeof(struct skd_special_context) * skdev->n_special);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004155
4156 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
4157 * skdev->n_special, GFP_KERNEL);
4158 if (skdev->skspcl_table == NULL) {
4159 rc = -ENOMEM;
4160 goto err_out;
4161 }
4162
4163 for (i = 0; i < skdev->n_special; i++) {
4164 struct skd_special_context *skspcl;
4165
4166 skspcl = &skdev->skspcl_table[i];
4167
4168 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
4169 skspcl->req.state = SKD_REQ_STATE_IDLE;
4170
4171 skspcl->req.next = &skspcl[1].req;
4172
4173 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4174
Joe Perchesa5bbf612014-08-08 14:24:12 -07004175 skspcl->msg_buf =
4176 pci_zalloc_consistent(skdev->pdev, nbytes,
4177 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004178 if (skspcl->msg_buf == NULL) {
4179 rc = -ENOMEM;
4180 goto err_out;
4181 }
4182
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004183 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4184 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4185 if (skspcl->req.sg == NULL) {
4186 rc = -ENOMEM;
4187 goto err_out;
4188 }
4189
4190 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4191 SKD_N_SG_PER_SPECIAL,
4192 &skspcl->req.
4193 sksg_dma_address);
4194 if (skspcl->req.sksg_list == NULL) {
4195 rc = -ENOMEM;
4196 goto err_out;
4197 }
4198 }
4199
4200 /* Free list is in order starting with the 0th entry. */
4201 skdev->skspcl_table[i - 1].req.next = NULL;
4202 skdev->skspcl_free_list = skdev->skspcl_table;
4203
4204 return rc;
4205
4206err_out:
4207 return rc;
4208}
4209
4210static int skd_cons_sksb(struct skd_device *skdev)
4211{
4212 int rc = 0;
4213 struct skd_special_context *skspcl;
4214 u32 nbytes;
4215
4216 skspcl = &skdev->internal_skspcl;
4217
4218 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4219 skspcl->req.state = SKD_REQ_STATE_IDLE;
4220
4221 nbytes = SKD_N_INTERNAL_BYTES;
4222
Joe Perchesa5bbf612014-08-08 14:24:12 -07004223 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4224 &skspcl->db_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004225 if (skspcl->data_buf == NULL) {
4226 rc = -ENOMEM;
4227 goto err_out;
4228 }
4229
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004230 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
Joe Perchesa5bbf612014-08-08 14:24:12 -07004231 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4232 &skspcl->mb_dma_address);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004233 if (skspcl->msg_buf == NULL) {
4234 rc = -ENOMEM;
4235 goto err_out;
4236 }
4237
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004238 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4239 &skspcl->req.sksg_dma_address);
4240 if (skspcl->req.sksg_list == NULL) {
4241 rc = -ENOMEM;
4242 goto err_out;
4243 }
4244
4245 if (!skd_format_internal_skspcl(skdev)) {
4246 rc = -EINVAL;
4247 goto err_out;
4248 }
4249
4250err_out:
4251 return rc;
4252}
4253
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004254static int skd_cons_disk(struct skd_device *skdev)
4255{
4256 int rc = 0;
4257 struct gendisk *disk;
4258 struct request_queue *q;
4259 unsigned long flags;
4260
4261 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4262 if (!disk) {
4263 rc = -ENOMEM;
4264 goto err_out;
4265 }
4266
4267 skdev->disk = disk;
4268 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4269
4270 disk->major = skdev->major;
4271 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4272 disk->fops = &skd_blockdev_ops;
4273 disk->private_data = skdev;
4274
Jens Axboefcd37eb2013-11-01 10:14:56 -06004275 q = blk_init_queue(skd_request_fn, &skdev->lock);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004276 if (!q) {
4277 rc = -ENOMEM;
4278 goto err_out;
4279 }
4280
4281 skdev->queue = q;
4282 disk->queue = q;
4283 q->queuedata = skdev;
4284
Jens Axboe6975f732016-03-30 10:11:42 -06004285 blk_queue_write_cache(q, true, true);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004286 blk_queue_max_segments(q, skdev->sgs_per_request);
4287 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4288
4289 /* set sysfs ptimal_io_size to 8K */
4290 blk_queue_io_opt(q, 8192);
4291
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004292 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06004293 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004294
4295 spin_lock_irqsave(&skdev->lock, flags);
rchinthekindi2e44b422013-10-24 12:51:23 +01004296 pr_debug("%s:%s:%d stopping %s queue\n",
4297 skdev->name, __func__, __LINE__, skdev->name);
Jens Axboe6a5ec652013-11-01 10:38:45 -06004298 blk_stop_queue(skdev->queue);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004299 spin_unlock_irqrestore(&skdev->lock, flags);
4300
4301err_out:
4302 return rc;
4303}
4304
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004305#define SKD_N_DEV_TABLE 16u
4306static u32 skd_next_devno;
4307
4308static struct skd_device *skd_construct(struct pci_dev *pdev)
4309{
4310 struct skd_device *skdev;
4311 int blk_major = skd_major;
4312 int rc;
4313
4314 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4315
4316 if (!skdev) {
4317 pr_err(PFX "(%s): memory alloc failure\n",
4318 pci_name(pdev));
4319 return NULL;
4320 }
4321
4322 skdev->state = SKD_DRVR_STATE_LOAD;
4323 skdev->pdev = pdev;
4324 skdev->devno = skd_next_devno++;
4325 skdev->major = blk_major;
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004326 sprintf(skdev->name, DRV_NAME "%d", skdev->devno);
4327 skdev->dev_max_queue_depth = 0;
4328
4329 skdev->num_req_context = skd_max_queue_depth;
4330 skdev->num_fitmsg_context = skd_max_queue_depth;
4331 skdev->n_special = skd_max_pass_thru;
4332 skdev->cur_max_queue_depth = 1;
4333 skdev->queue_low_water_mark = 1;
4334 skdev->proto_ver = 99;
4335 skdev->sgs_per_request = skd_sgs_per_request;
4336 skdev->dbg_level = skd_dbg_level;
4337
4338 atomic_set(&skdev->device_count, 0);
4339
4340 spin_lock_init(&skdev->lock);
4341
4342 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4343
4344 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4345 rc = skd_cons_skcomp(skdev);
4346 if (rc < 0)
4347 goto err_out;
4348
4349 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4350 rc = skd_cons_skmsg(skdev);
4351 if (rc < 0)
4352 goto err_out;
4353
4354 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4355 rc = skd_cons_skreq(skdev);
4356 if (rc < 0)
4357 goto err_out;
4358
4359 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4360 rc = skd_cons_skspcl(skdev);
4361 if (rc < 0)
4362 goto err_out;
4363
4364 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4365 rc = skd_cons_sksb(skdev);
4366 if (rc < 0)
4367 goto err_out;
4368
4369 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4370 rc = skd_cons_disk(skdev);
4371 if (rc < 0)
4372 goto err_out;
4373
4374 pr_debug("%s:%s:%d VICTORY\n", skdev->name, __func__, __LINE__);
4375 return skdev;
4376
4377err_out:
4378 pr_debug("%s:%s:%d construct failed\n",
4379 skdev->name, __func__, __LINE__);
4380 skd_destruct(skdev);
4381 return NULL;
4382}
4383
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004384/*
4385 *****************************************************************************
4386 * DESTRUCT (FREE)
4387 *****************************************************************************
4388 */
4389
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004390static void skd_free_skcomp(struct skd_device *skdev)
4391{
4392 if (skdev->skcomp_table != NULL) {
4393 u32 nbytes;
4394
4395 nbytes = sizeof(skdev->skcomp_table[0]) *
4396 SKD_N_COMPLETION_ENTRY;
4397 pci_free_consistent(skdev->pdev, nbytes,
4398 skdev->skcomp_table, skdev->cq_dma_address);
4399 }
4400
4401 skdev->skcomp_table = NULL;
4402 skdev->cq_dma_address = 0;
4403}
4404
4405static void skd_free_skmsg(struct skd_device *skdev)
4406{
4407 u32 i;
4408
4409 if (skdev->skmsg_table == NULL)
4410 return;
4411
4412 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4413 struct skd_fitmsg_context *skmsg;
4414
4415 skmsg = &skdev->skmsg_table[i];
4416
4417 if (skmsg->msg_buf != NULL) {
4418 skmsg->msg_buf += skmsg->offset;
4419 skmsg->mb_dma_address += skmsg->offset;
4420 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4421 skmsg->msg_buf,
4422 skmsg->mb_dma_address);
4423 }
4424 skmsg->msg_buf = NULL;
4425 skmsg->mb_dma_address = 0;
4426 }
4427
4428 kfree(skdev->skmsg_table);
4429 skdev->skmsg_table = NULL;
4430}
4431
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004432static void skd_free_sg_list(struct skd_device *skdev,
4433 struct fit_sg_descriptor *sg_list,
4434 u32 n_sg, dma_addr_t dma_addr)
4435{
4436 if (sg_list != NULL) {
4437 u32 nbytes;
4438
4439 nbytes = sizeof(*sg_list) * n_sg;
4440
4441 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4442 }
4443}
4444
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004445static void skd_free_skreq(struct skd_device *skdev)
4446{
4447 u32 i;
4448
4449 if (skdev->skreq_table == NULL)
4450 return;
4451
4452 for (i = 0; i < skdev->num_req_context; i++) {
4453 struct skd_request_context *skreq;
4454
4455 skreq = &skdev->skreq_table[i];
4456
4457 skd_free_sg_list(skdev, skreq->sksg_list,
4458 skdev->sgs_per_request,
4459 skreq->sksg_dma_address);
4460
4461 skreq->sksg_list = NULL;
4462 skreq->sksg_dma_address = 0;
4463
4464 kfree(skreq->sg);
4465 }
4466
4467 kfree(skdev->skreq_table);
4468 skdev->skreq_table = NULL;
4469}
4470
4471static void skd_free_skspcl(struct skd_device *skdev)
4472{
4473 u32 i;
4474 u32 nbytes;
4475
4476 if (skdev->skspcl_table == NULL)
4477 return;
4478
4479 for (i = 0; i < skdev->n_special; i++) {
4480 struct skd_special_context *skspcl;
4481
4482 skspcl = &skdev->skspcl_table[i];
4483
4484 if (skspcl->msg_buf != NULL) {
4485 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4486 pci_free_consistent(skdev->pdev, nbytes,
4487 skspcl->msg_buf,
4488 skspcl->mb_dma_address);
4489 }
4490
4491 skspcl->msg_buf = NULL;
4492 skspcl->mb_dma_address = 0;
4493
4494 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4495 SKD_N_SG_PER_SPECIAL,
4496 skspcl->req.sksg_dma_address);
4497
4498 skspcl->req.sksg_list = NULL;
4499 skspcl->req.sksg_dma_address = 0;
4500
4501 kfree(skspcl->req.sg);
4502 }
4503
4504 kfree(skdev->skspcl_table);
4505 skdev->skspcl_table = NULL;
4506}
4507
4508static void skd_free_sksb(struct skd_device *skdev)
4509{
4510 struct skd_special_context *skspcl;
4511 u32 nbytes;
4512
4513 skspcl = &skdev->internal_skspcl;
4514
4515 if (skspcl->data_buf != NULL) {
4516 nbytes = SKD_N_INTERNAL_BYTES;
4517
4518 pci_free_consistent(skdev->pdev, nbytes,
4519 skspcl->data_buf, skspcl->db_dma_address);
4520 }
4521
4522 skspcl->data_buf = NULL;
4523 skspcl->db_dma_address = 0;
4524
4525 if (skspcl->msg_buf != NULL) {
4526 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4527 pci_free_consistent(skdev->pdev, nbytes,
4528 skspcl->msg_buf, skspcl->mb_dma_address);
4529 }
4530
4531 skspcl->msg_buf = NULL;
4532 skspcl->mb_dma_address = 0;
4533
4534 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4535 skspcl->req.sksg_dma_address);
4536
4537 skspcl->req.sksg_list = NULL;
4538 skspcl->req.sksg_dma_address = 0;
4539}
4540
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004541static void skd_free_disk(struct skd_device *skdev)
4542{
4543 struct gendisk *disk = skdev->disk;
4544
4545 if (disk != NULL) {
4546 struct request_queue *q = disk->queue;
4547
4548 if (disk->flags & GENHD_FL_UP)
4549 del_gendisk(disk);
4550 if (q)
4551 blk_cleanup_queue(q);
4552 put_disk(disk);
4553 }
4554 skdev->disk = NULL;
4555}
4556
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004557static void skd_destruct(struct skd_device *skdev)
4558{
4559 if (skdev == NULL)
4560 return;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004561
4562
Bartlomiej Zolnierkiewicz542d7b02013-11-05 12:37:08 +01004563 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4564 skd_free_disk(skdev);
4565
4566 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4567 skd_free_sksb(skdev);
4568
4569 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4570 skd_free_skspcl(skdev);
4571
4572 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4573 skd_free_skreq(skdev);
4574
4575 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4576 skd_free_skmsg(skdev);
4577
4578 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4579 skd_free_skcomp(skdev);
4580
4581 pr_debug("%s:%s:%d skdev\n", skdev->name, __func__, __LINE__);
4582 kfree(skdev);
4583}
4584
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004585/*
4586 *****************************************************************************
4587 * BLOCK DEVICE (BDEV) GLUE
4588 *****************************************************************************
4589 */
4590
4591static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4592{
4593 struct skd_device *skdev;
4594 u64 capacity;
4595
4596 skdev = bdev->bd_disk->private_data;
4597
rchinthekindi2e44b422013-10-24 12:51:23 +01004598 pr_debug("%s:%s:%d %s: CMD[%s] getgeo device\n",
4599 skdev->name, __func__, __LINE__,
4600 bdev->bd_disk->disk_name, current->comm);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004601
4602 if (skdev->read_cap_is_valid) {
4603 capacity = get_capacity(skdev->disk);
4604 geo->heads = 64;
4605 geo->sectors = 255;
4606 geo->cylinders = (capacity) / (255 * 64);
4607
4608 return 0;
4609 }
4610 return -EIO;
4611}
4612
Dan Williams0d52c7562016-06-15 19:44:20 -07004613static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004614{
rchinthekindi2e44b422013-10-24 12:51:23 +01004615 pr_debug("%s:%s:%d add_disk\n", skdev->name, __func__, __LINE__);
Dan Williams0d52c7562016-06-15 19:44:20 -07004616 device_add_disk(parent, skdev->disk);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004617 return 0;
4618}
4619
4620static const struct block_device_operations skd_blockdev_ops = {
4621 .owner = THIS_MODULE,
4622 .ioctl = skd_bdev_ioctl,
4623 .getgeo = skd_bdev_getgeo,
4624};
4625
4626
4627/*
4628 *****************************************************************************
4629 * PCIe DRIVER GLUE
4630 *****************************************************************************
4631 */
4632
Benoit Taine9baa3c32014-08-08 15:56:03 +02004633static const struct pci_device_id skd_pci_tbl[] = {
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004634 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4635 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4636 { 0 } /* terminate list */
4637};
4638
4639MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4640
4641static char *skd_pci_info(struct skd_device *skdev, char *str)
4642{
4643 int pcie_reg;
4644
4645 strcpy(str, "PCIe (");
4646 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4647
4648 if (pcie_reg) {
4649
4650 char lwstr[6];
4651 uint16_t pcie_lstat, lspeed, lwidth;
4652
4653 pcie_reg += 0x12;
4654 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4655 lspeed = pcie_lstat & (0xF);
4656 lwidth = (pcie_lstat & 0x3F0) >> 4;
4657
4658 if (lspeed == 1)
4659 strcat(str, "2.5GT/s ");
4660 else if (lspeed == 2)
4661 strcat(str, "5.0GT/s ");
4662 else
4663 strcat(str, "<unknown> ");
4664 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4665 strcat(str, lwstr);
4666 }
4667 return str;
4668}
4669
4670static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4671{
4672 int i;
4673 int rc = 0;
4674 char pci_str[32];
4675 struct skd_device *skdev;
4676
4677 pr_info("STEC s1120 Driver(%s) version %s-b%s\n",
4678 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4679 pr_info("(skd?:??:[%s]): vendor=%04X device=%04x\n",
4680 pci_name(pdev), pdev->vendor, pdev->device);
4681
4682 rc = pci_enable_device(pdev);
4683 if (rc)
4684 return rc;
4685 rc = pci_request_regions(pdev, DRV_NAME);
4686 if (rc)
4687 goto err_out;
4688 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4689 if (!rc) {
4690 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4691
4692 pr_err("(%s): consistent DMA mask error %d\n",
4693 pci_name(pdev), rc);
4694 }
4695 } else {
4696 (rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)));
4697 if (rc) {
4698
4699 pr_err("(%s): DMA mask error %d\n",
4700 pci_name(pdev), rc);
4701 goto err_out_regions;
4702 }
4703 }
4704
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01004705 if (!skd_major) {
4706 rc = register_blkdev(0, DRV_NAME);
4707 if (rc < 0)
4708 goto err_out_regions;
4709 BUG_ON(!rc);
4710 skd_major = rc;
4711 }
4712
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004713 skdev = skd_construct(pdev);
Wei Yongjun1762b572013-10-30 13:23:53 +08004714 if (skdev == NULL) {
4715 rc = -ENOMEM;
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004716 goto err_out_regions;
Wei Yongjun1762b572013-10-30 13:23:53 +08004717 }
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004718
4719 skd_pci_info(skdev, pci_str);
4720 pr_info("(%s): %s 64bit\n", skd_name(skdev), pci_str);
4721
4722 pci_set_master(pdev);
4723 rc = pci_enable_pcie_error_reporting(pdev);
4724 if (rc) {
4725 pr_err(
4726 "(%s): bad enable of PCIe error reporting rc=%d\n",
4727 skd_name(skdev), rc);
4728 skdev->pcie_error_reporting_is_enabled = 0;
4729 } else
4730 skdev->pcie_error_reporting_is_enabled = 1;
4731
4732
4733 pci_set_drvdata(pdev, skdev);
Bartlomiej Zolnierkiewiczebedd162013-11-05 12:37:05 +01004734
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004735 for (i = 0; i < SKD_MAX_BARS; i++) {
4736 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4737 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4738 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4739 skdev->mem_size[i]);
4740 if (!skdev->mem_map[i]) {
4741 pr_err("(%s): Unable to map adapter memory!\n",
4742 skd_name(skdev));
4743 rc = -ENODEV;
4744 goto err_out_iounmap;
4745 }
rchinthekindi2e44b422013-10-24 12:51:23 +01004746 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
4747 skdev->name, __func__, __LINE__,
4748 skdev->mem_map[i],
4749 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004750 }
4751
4752 rc = skd_acquire_irq(skdev);
4753 if (rc) {
4754 pr_err("(%s): interrupt resource error %d\n",
4755 skd_name(skdev), rc);
4756 goto err_out_iounmap;
4757 }
4758
4759 rc = skd_start_timer(skdev);
4760 if (rc)
4761 goto err_out_timer;
4762
4763 init_waitqueue_head(&skdev->waitq);
4764
4765 skd_start_device(skdev);
4766
4767 rc = wait_event_interruptible_timeout(skdev->waitq,
4768 (skdev->gendisk_on),
4769 (SKD_START_WAIT_SECONDS * HZ));
4770 if (skdev->gendisk_on > 0) {
4771 /* device came on-line after reset */
Dan Williams0d52c7562016-06-15 19:44:20 -07004772 skd_bdev_attach(&pdev->dev, skdev);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004773 rc = 0;
4774 } else {
4775 /* we timed out, something is wrong with the device,
4776 don't add the disk structure */
4777 pr_err(
4778 "(%s): error: waiting for s1120 timed out %d!\n",
4779 skd_name(skdev), rc);
4780 /* in case of no error; we timeout with ENXIO */
4781 if (!rc)
4782 rc = -ENXIO;
4783 goto err_out_timer;
4784 }
4785
4786
4787#ifdef SKD_VMK_POLL_HANDLER
4788 if (skdev->irq_type == SKD_IRQ_MSIX) {
4789 /* MSIX completion handler is being used for coredump */
4790 vmklnx_scsi_register_poll_handler(skdev->scsi_host,
4791 skdev->msix_entries[5].vector,
4792 skd_comp_q, skdev);
4793 } else {
4794 vmklnx_scsi_register_poll_handler(skdev->scsi_host,
4795 skdev->pdev->irq, skd_isr,
4796 skdev);
4797 }
4798#endif /* SKD_VMK_POLL_HANDLER */
4799
4800 return rc;
4801
4802err_out_timer:
4803 skd_stop_device(skdev);
4804 skd_release_irq(skdev);
4805
4806err_out_iounmap:
4807 for (i = 0; i < SKD_MAX_BARS; i++)
4808 if (skdev->mem_map[i])
4809 iounmap(skdev->mem_map[i]);
4810
4811 if (skdev->pcie_error_reporting_is_enabled)
4812 pci_disable_pcie_error_reporting(pdev);
4813
4814 skd_destruct(skdev);
4815
4816err_out_regions:
4817 pci_release_regions(pdev);
4818
4819err_out:
4820 pci_disable_device(pdev);
4821 pci_set_drvdata(pdev, NULL);
4822 return rc;
4823}
4824
4825static void skd_pci_remove(struct pci_dev *pdev)
4826{
4827 int i;
4828 struct skd_device *skdev;
4829
4830 skdev = pci_get_drvdata(pdev);
4831 if (!skdev) {
4832 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4833 return;
4834 }
4835 skd_stop_device(skdev);
4836 skd_release_irq(skdev);
4837
4838 for (i = 0; i < SKD_MAX_BARS; i++)
4839 if (skdev->mem_map[i])
4840 iounmap((u32 *)skdev->mem_map[i]);
4841
4842 if (skdev->pcie_error_reporting_is_enabled)
4843 pci_disable_pcie_error_reporting(pdev);
4844
4845 skd_destruct(skdev);
4846
4847 pci_release_regions(pdev);
4848 pci_disable_device(pdev);
4849 pci_set_drvdata(pdev, NULL);
4850
4851 return;
4852}
4853
4854static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4855{
4856 int i;
4857 struct skd_device *skdev;
4858
4859 skdev = pci_get_drvdata(pdev);
4860 if (!skdev) {
4861 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4862 return -EIO;
4863 }
4864
4865 skd_stop_device(skdev);
4866
4867 skd_release_irq(skdev);
4868
4869 for (i = 0; i < SKD_MAX_BARS; i++)
4870 if (skdev->mem_map[i])
4871 iounmap((u32 *)skdev->mem_map[i]);
4872
4873 if (skdev->pcie_error_reporting_is_enabled)
4874 pci_disable_pcie_error_reporting(pdev);
4875
4876 pci_release_regions(pdev);
4877 pci_save_state(pdev);
4878 pci_disable_device(pdev);
4879 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4880 return 0;
4881}
4882
4883static int skd_pci_resume(struct pci_dev *pdev)
4884{
4885 int i;
4886 int rc = 0;
4887 struct skd_device *skdev;
4888
4889 skdev = pci_get_drvdata(pdev);
4890 if (!skdev) {
4891 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4892 return -1;
4893 }
4894
4895 pci_set_power_state(pdev, PCI_D0);
4896 pci_enable_wake(pdev, PCI_D0, 0);
4897 pci_restore_state(pdev);
4898
4899 rc = pci_enable_device(pdev);
4900 if (rc)
4901 return rc;
4902 rc = pci_request_regions(pdev, DRV_NAME);
4903 if (rc)
4904 goto err_out;
4905 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4906 if (!rc) {
4907 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4908
4909 pr_err("(%s): consistent DMA mask error %d\n",
4910 pci_name(pdev), rc);
4911 }
4912 } else {
4913 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4914 if (rc) {
4915
4916 pr_err("(%s): DMA mask error %d\n",
4917 pci_name(pdev), rc);
4918 goto err_out_regions;
4919 }
4920 }
4921
4922 pci_set_master(pdev);
4923 rc = pci_enable_pcie_error_reporting(pdev);
4924 if (rc) {
4925 pr_err("(%s): bad enable of PCIe error reporting rc=%d\n",
4926 skdev->name, rc);
4927 skdev->pcie_error_reporting_is_enabled = 0;
4928 } else
4929 skdev->pcie_error_reporting_is_enabled = 1;
4930
4931 for (i = 0; i < SKD_MAX_BARS; i++) {
4932
4933 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4934 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4935 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4936 skdev->mem_size[i]);
4937 if (!skdev->mem_map[i]) {
4938 pr_err("(%s): Unable to map adapter memory!\n",
4939 skd_name(skdev));
4940 rc = -ENODEV;
4941 goto err_out_iounmap;
4942 }
rchinthekindi2e44b422013-10-24 12:51:23 +01004943 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
4944 skdev->name, __func__, __LINE__,
4945 skdev->mem_map[i],
4946 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06004947 }
4948 rc = skd_acquire_irq(skdev);
4949 if (rc) {
4950
4951 pr_err("(%s): interrupt resource error %d\n",
4952 pci_name(pdev), rc);
4953 goto err_out_iounmap;
4954 }
4955
4956 rc = skd_start_timer(skdev);
4957 if (rc)
4958 goto err_out_timer;
4959
4960 init_waitqueue_head(&skdev->waitq);
4961
4962 skd_start_device(skdev);
4963
4964 return rc;
4965
4966err_out_timer:
4967 skd_stop_device(skdev);
4968 skd_release_irq(skdev);
4969
4970err_out_iounmap:
4971 for (i = 0; i < SKD_MAX_BARS; i++)
4972 if (skdev->mem_map[i])
4973 iounmap(skdev->mem_map[i]);
4974
4975 if (skdev->pcie_error_reporting_is_enabled)
4976 pci_disable_pcie_error_reporting(pdev);
4977
4978err_out_regions:
4979 pci_release_regions(pdev);
4980
4981err_out:
4982 pci_disable_device(pdev);
4983 return rc;
4984}
4985
4986static void skd_pci_shutdown(struct pci_dev *pdev)
4987{
4988 struct skd_device *skdev;
4989
4990 pr_err("skd_pci_shutdown called\n");
4991
4992 skdev = pci_get_drvdata(pdev);
4993 if (!skdev) {
4994 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4995 return;
4996 }
4997
4998 pr_err("%s: calling stop\n", skd_name(skdev));
4999 skd_stop_device(skdev);
5000}
5001
5002static struct pci_driver skd_driver = {
5003 .name = DRV_NAME,
5004 .id_table = skd_pci_tbl,
5005 .probe = skd_pci_probe,
5006 .remove = skd_pci_remove,
5007 .suspend = skd_pci_suspend,
5008 .resume = skd_pci_resume,
5009 .shutdown = skd_pci_shutdown,
5010};
5011
5012/*
5013 *****************************************************************************
5014 * LOGGING SUPPORT
5015 *****************************************************************************
5016 */
5017
5018static const char *skd_name(struct skd_device *skdev)
5019{
5020 memset(skdev->id_str, 0, sizeof(skdev->id_str));
5021
5022 if (skdev->inquiry_is_valid)
5023 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:%s:[%s]",
5024 skdev->name, skdev->inq_serial_num,
5025 pci_name(skdev->pdev));
5026 else
5027 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:??:[%s]",
5028 skdev->name, pci_name(skdev->pdev));
5029
5030 return skdev->id_str;
5031}
5032
5033const char *skd_drive_state_to_str(int state)
5034{
5035 switch (state) {
5036 case FIT_SR_DRIVE_OFFLINE:
5037 return "OFFLINE";
5038 case FIT_SR_DRIVE_INIT:
5039 return "INIT";
5040 case FIT_SR_DRIVE_ONLINE:
5041 return "ONLINE";
5042 case FIT_SR_DRIVE_BUSY:
5043 return "BUSY";
5044 case FIT_SR_DRIVE_FAULT:
5045 return "FAULT";
5046 case FIT_SR_DRIVE_DEGRADED:
5047 return "DEGRADED";
5048 case FIT_SR_PCIE_LINK_DOWN:
5049 return "INK_DOWN";
5050 case FIT_SR_DRIVE_SOFT_RESET:
5051 return "SOFT_RESET";
5052 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
5053 return "NEED_FW";
5054 case FIT_SR_DRIVE_INIT_FAULT:
5055 return "INIT_FAULT";
5056 case FIT_SR_DRIVE_BUSY_SANITIZE:
5057 return "BUSY_SANITIZE";
5058 case FIT_SR_DRIVE_BUSY_ERASE:
5059 return "BUSY_ERASE";
5060 case FIT_SR_DRIVE_FW_BOOTING:
5061 return "FW_BOOTING";
5062 default:
5063 return "???";
5064 }
5065}
5066
5067const char *skd_skdev_state_to_str(enum skd_drvr_state state)
5068{
5069 switch (state) {
5070 case SKD_DRVR_STATE_LOAD:
5071 return "LOAD";
5072 case SKD_DRVR_STATE_IDLE:
5073 return "IDLE";
5074 case SKD_DRVR_STATE_BUSY:
5075 return "BUSY";
5076 case SKD_DRVR_STATE_STARTING:
5077 return "STARTING";
5078 case SKD_DRVR_STATE_ONLINE:
5079 return "ONLINE";
5080 case SKD_DRVR_STATE_PAUSING:
5081 return "PAUSING";
5082 case SKD_DRVR_STATE_PAUSED:
5083 return "PAUSED";
5084 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
5085 return "DRAINING_TIMEOUT";
5086 case SKD_DRVR_STATE_RESTARTING:
5087 return "RESTARTING";
5088 case SKD_DRVR_STATE_RESUMING:
5089 return "RESUMING";
5090 case SKD_DRVR_STATE_STOPPING:
5091 return "STOPPING";
5092 case SKD_DRVR_STATE_SYNCING:
5093 return "SYNCING";
5094 case SKD_DRVR_STATE_FAULT:
5095 return "FAULT";
5096 case SKD_DRVR_STATE_DISAPPEARED:
5097 return "DISAPPEARED";
5098 case SKD_DRVR_STATE_BUSY_ERASE:
5099 return "BUSY_ERASE";
5100 case SKD_DRVR_STATE_BUSY_SANITIZE:
5101 return "BUSY_SANITIZE";
5102 case SKD_DRVR_STATE_BUSY_IMMINENT:
5103 return "BUSY_IMMINENT";
5104 case SKD_DRVR_STATE_WAIT_BOOT:
5105 return "WAIT_BOOT";
5106
5107 default:
5108 return "???";
5109 }
5110}
5111
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05305112static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005113{
5114 switch (state) {
5115 case SKD_MSG_STATE_IDLE:
5116 return "IDLE";
5117 case SKD_MSG_STATE_BUSY:
5118 return "BUSY";
5119 default:
5120 return "???";
5121 }
5122}
5123
Rashika Kheriaa26ba7f2013-12-19 15:02:22 +05305124static const char *skd_skreq_state_to_str(enum skd_req_state state)
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005125{
5126 switch (state) {
5127 case SKD_REQ_STATE_IDLE:
5128 return "IDLE";
5129 case SKD_REQ_STATE_SETUP:
5130 return "SETUP";
5131 case SKD_REQ_STATE_BUSY:
5132 return "BUSY";
5133 case SKD_REQ_STATE_COMPLETED:
5134 return "COMPLETED";
5135 case SKD_REQ_STATE_TIMEOUT:
5136 return "TIMEOUT";
5137 case SKD_REQ_STATE_ABORTED:
5138 return "ABORTED";
5139 default:
5140 return "???";
5141 }
5142}
5143
5144static void skd_log_skdev(struct skd_device *skdev, const char *event)
5145{
rchinthekindi2e44b422013-10-24 12:51:23 +01005146 pr_debug("%s:%s:%d (%s) skdev=%p event='%s'\n",
5147 skdev->name, __func__, __LINE__, skdev->name, skdev, event);
5148 pr_debug("%s:%s:%d drive_state=%s(%d) driver_state=%s(%d)\n",
5149 skdev->name, __func__, __LINE__,
5150 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
5151 skd_skdev_state_to_str(skdev->state), skdev->state);
5152 pr_debug("%s:%s:%d busy=%d limit=%d dev=%d lowat=%d\n",
5153 skdev->name, __func__, __LINE__,
5154 skdev->in_flight, skdev->cur_max_queue_depth,
5155 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
5156 pr_debug("%s:%s:%d timestamp=0x%x cycle=%d cycle_ix=%d\n",
5157 skdev->name, __func__, __LINE__,
5158 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005159}
5160
5161static void skd_log_skmsg(struct skd_device *skdev,
5162 struct skd_fitmsg_context *skmsg, const char *event)
5163{
rchinthekindi2e44b422013-10-24 12:51:23 +01005164 pr_debug("%s:%s:%d (%s) skmsg=%p event='%s'\n",
5165 skdev->name, __func__, __LINE__, skdev->name, skmsg, event);
5166 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x length=%d\n",
5167 skdev->name, __func__, __LINE__,
5168 skd_skmsg_state_to_str(skmsg->state), skmsg->state,
5169 skmsg->id, skmsg->length);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005170}
5171
5172static void skd_log_skreq(struct skd_device *skdev,
5173 struct skd_request_context *skreq, const char *event)
5174{
rchinthekindi2e44b422013-10-24 12:51:23 +01005175 pr_debug("%s:%s:%d (%s) skreq=%p event='%s'\n",
5176 skdev->name, __func__, __LINE__, skdev->name, skreq, event);
5177 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
5178 skdev->name, __func__, __LINE__,
5179 skd_skreq_state_to_str(skreq->state), skreq->state,
5180 skreq->id, skreq->fitmsg_id);
5181 pr_debug("%s:%s:%d timo=0x%x sg_dir=%d n_sg=%d\n",
5182 skdev->name, __func__, __LINE__,
5183 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005184
Jens Axboefcd37eb2013-11-01 10:14:56 -06005185 if (skreq->req != NULL) {
5186 struct request *req = skreq->req;
5187 u32 lba = (u32)blk_rq_pos(req);
5188 u32 count = blk_rq_sectors(req);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005189
Jens Axboefcd37eb2013-11-01 10:14:56 -06005190 pr_debug("%s:%s:%d "
5191 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
5192 skdev->name, __func__, __LINE__,
5193 req, lba, lba, count, count,
5194 (int)rq_data_dir(req));
5195 } else
5196 pr_debug("%s:%s:%d req=NULL\n",
5197 skdev->name, __func__, __LINE__);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005198}
5199
5200/*
5201 *****************************************************************************
5202 * MODULE GLUE
5203 *****************************************************************************
5204 */
5205
5206static int __init skd_init(void)
5207{
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005208 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
5209
5210 switch (skd_isr_type) {
5211 case SKD_IRQ_LEGACY:
5212 case SKD_IRQ_MSI:
5213 case SKD_IRQ_MSIX:
5214 break;
5215 default:
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005216 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005217 skd_isr_type, SKD_IRQ_DEFAULT);
5218 skd_isr_type = SKD_IRQ_DEFAULT;
5219 }
5220
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005221 if (skd_max_queue_depth < 1 ||
5222 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
5223 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005224 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
5225 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
5226 }
5227
5228 if (skd_max_req_per_msg < 1 || skd_max_req_per_msg > 14) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005229 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005230 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
5231 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
5232 }
5233
5234 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005235 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005236 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
5237 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5238 }
5239
5240 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005241 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005242 skd_dbg_level, 0);
5243 skd_dbg_level = 0;
5244 }
5245
5246 if (skd_isr_comp_limit < 0) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005247 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005248 skd_isr_comp_limit, 0);
5249 skd_isr_comp_limit = 0;
5250 }
5251
5252 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
Bartlomiej Zolnierkiewiczfbed1492013-11-05 12:37:01 +01005253 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005254 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5255 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5256 }
5257
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005258 return pci_register_driver(&skd_driver);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005259}
5260
5261static void __exit skd_exit(void)
5262{
5263 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5264
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005265 pci_unregister_driver(&skd_driver);
Bartlomiej Zolnierkiewiczb8df6642013-11-05 12:37:02 +01005266
5267 if (skd_major)
5268 unregister_blkdev(skd_major, DRV_NAME);
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005269}
5270
Akhil Bhansalie67f86b2013-10-15 14:19:07 -06005271module_init(skd_init);
5272module_exit(skd_exit);