powerpc: Put exception configuration in a common place
[linux/fpc-iii.git] / drivers / block / skd_main.c
blob910e065918af13d2536f6fe6736a4fad3303e243
1 /* Copyright 2012 STEC, Inc.
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.
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>
45 #include <scsi/sg.h>
46 #include <linux/io.h>
47 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
50 #include "skd_s1120.h"
52 static int skd_dbg_level;
53 static int skd_isr_comp_limit = 4;
55 enum {
56 STEC_LINK_2_5GTS = 0,
57 STEC_LINK_5GTS = 1,
58 STEC_LINK_8GTS = 2,
59 STEC_LINK_UNKNOWN = 0xFF
62 enum {
63 SKD_FLUSH_INITIALIZER,
64 SKD_FLUSH_ZERO_SIZE_FIRST,
65 SKD_FLUSH_DATA_SECOND,
68 #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)
76 #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
83 MODULE_AUTHOR("bug-reports: support@stec-inc.com");
84 MODULE_LICENSE("Dual BSD/GPL");
86 MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
87 MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);
89 #define PCI_VENDOR_ID_STEC 0x1B39
90 #define PCI_DEVICE_ID_S1120 0x0001
92 #define SKD_FUA_NV (1 << 1)
93 #define SKD_MINORS_PER_DEVICE 16
95 #define SKD_MAX_QUEUE_DEPTH 200u
97 #define SKD_PAUSE_TIMEOUT (5 * 1000)
99 #define SKD_N_FITMSG_BYTES (512u)
101 #define SKD_N_SPECIAL_CONTEXT 32u
102 #define SKD_N_SPECIAL_FITMSG_BYTES (128u)
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
107 #define SKD_N_SG_PER_REQ_DEFAULT 256u
108 #define SKD_N_SG_PER_SPECIAL 256u
110 #define SKD_N_COMPLETION_ENTRY 256u
111 #define SKD_N_READ_CAP_BYTES (8u)
113 #define SKD_N_INTERNAL_BYTES (512u)
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
125 #define SKD_N_TIMEOUT_SLOT 4u
126 #define SKD_TIMEOUT_SLOT_MASK 3u
128 #define SKD_N_MAX_SECTORS 2048u
130 #define SKD_MAX_RETRIES 2u
132 #define SKD_TIMER_SECONDS(seconds) (seconds)
133 #define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
135 #define INQ_STD_NBYTES 36
137 enum 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,
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
167 enum 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,
176 enum skd_fit_msg_state {
177 SKD_MSG_STATE_IDLE,
178 SKD_MSG_STATE_BUSY,
181 enum 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,
189 struct skd_fitmsg_context {
190 enum skd_fit_msg_state state;
192 struct skd_fitmsg_context *next;
194 u32 id;
195 u16 outstanding;
197 u32 length;
198 u32 offset;
200 u8 *msg_buf;
201 dma_addr_t mb_dma_address;
204 struct skd_request_context {
205 enum skd_req_state state;
207 struct skd_request_context *next;
209 u16 id;
210 u32 fitmsg_id;
212 struct request *req;
213 u8 flush_cmd;
215 u32 timeout_stamp;
216 u8 sg_data_dir;
217 struct scatterlist *sg;
218 u32 n_sg;
219 u32 sg_byte_count;
221 struct fit_sg_descriptor *sksg_list;
222 dma_addr_t sksg_dma_address;
224 struct fit_completion_entry_v1 completion;
226 struct fit_comp_error_info err_info;
229 #define SKD_DATA_DIR_HOST_TO_CARD 1
230 #define SKD_DATA_DIR_CARD_TO_HOST 2
232 struct skd_special_context {
233 struct skd_request_context req;
235 u8 orphaned;
237 void *data_buf;
238 dma_addr_t db_dma_address;
240 u8 *msg_buf;
241 dma_addr_t mb_dma_address;
244 struct skd_sg_io {
245 fmode_t mode;
246 void __user *argp;
248 struct sg_io_hdr sg;
250 u8 cdb[16];
252 u32 dxfer_len;
253 u32 iovcnt;
254 struct sg_iovec *iov;
255 struct sg_iovec no_iov_iov;
257 struct skd_special_context *skspcl;
260 typedef enum skd_irq_type {
261 SKD_IRQ_LEGACY,
262 SKD_IRQ_MSI,
263 SKD_IRQ_MSIX
264 } skd_irq_type_t;
266 #define SKD_MAX_BARS 2
268 struct 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];
273 skd_irq_type_t irq_type;
274 u32 msix_count;
275 struct skd_msix_entry *msix_entries;
277 struct pci_dev *pdev;
278 int pcie_error_reporting_is_enabled;
280 spinlock_t lock;
281 struct gendisk *disk;
282 struct request_queue *queue;
283 struct device *class_dev;
284 int gendisk_on;
285 int sync_done;
287 atomic_t device_count;
288 u32 devno;
289 u32 major;
290 char name[32];
291 char isr_name[30];
293 enum skd_drvr_state state;
294 u32 drive_state;
296 u32 in_flight;
297 u32 cur_max_queue_depth;
298 u32 queue_low_water_mark;
299 u32 dev_max_queue_depth;
301 u32 num_fitmsg_context;
302 u32 num_req_context;
304 u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
305 u32 timeout_stamp;
306 struct skd_fitmsg_context *skmsg_free_list;
307 struct skd_fitmsg_context *skmsg_table;
309 struct skd_request_context *skreq_free_list;
310 struct skd_request_context *skreq_table;
312 struct skd_special_context *skspcl_free_list;
313 struct skd_special_context *skspcl_table;
315 struct skd_special_context internal_skspcl;
316 u32 read_cap_blocksize;
317 u32 read_cap_last_lba;
318 int read_cap_is_valid;
319 int inquiry_is_valid;
320 u8 inq_serial_num[13]; /*12 chars plus null term */
321 u8 id_str[80]; /* holds a composite name (pci + sernum) */
323 u8 skcomp_cycle;
324 u32 skcomp_ix;
325 struct fit_completion_entry_v1 *skcomp_table;
326 struct fit_comp_error_info *skerr_table;
327 dma_addr_t cq_dma_address;
329 wait_queue_head_t waitq;
331 struct timer_list timer;
332 u32 timer_countdown;
333 u32 timer_substate;
335 int n_special;
336 int sgs_per_request;
337 u32 last_mtd;
339 u32 proto_ver;
341 int dbg_level;
342 u32 connect_time_stamp;
343 int connect_retries;
344 #define SKD_MAX_CONNECT_RETRIES 16
345 u32 drive_jiffies;
347 u32 timo_slot;
350 struct work_struct completion_worker;
353 #define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
354 #define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
355 #define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
357 static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
359 u32 val;
361 if (likely(skdev->dbg_level < 2))
362 return readl(skdev->mem_map[1] + offset);
363 else {
364 barrier();
365 val = readl(skdev->mem_map[1] + offset);
366 barrier();
367 pr_debug("%s:%s:%d offset %x = %x\n",
368 skdev->name, __func__, __LINE__, offset, val);
369 return val;
374 static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
375 u32 offset)
377 if (likely(skdev->dbg_level < 2)) {
378 writel(val, skdev->mem_map[1] + offset);
379 barrier();
380 } else {
381 barrier();
382 writel(val, skdev->mem_map[1] + offset);
383 barrier();
384 pr_debug("%s:%s:%d offset %x = %x\n",
385 skdev->name, __func__, __LINE__, offset, val);
389 static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
390 u32 offset)
392 if (likely(skdev->dbg_level < 2)) {
393 writeq(val, skdev->mem_map[1] + offset);
394 barrier();
395 } else {
396 barrier();
397 writeq(val, skdev->mem_map[1] + offset);
398 barrier();
399 pr_debug("%s:%s:%d offset %x = %016llx\n",
400 skdev->name, __func__, __LINE__, offset, val);
405 #define SKD_IRQ_DEFAULT SKD_IRQ_MSI
406 static int skd_isr_type = SKD_IRQ_DEFAULT;
408 module_param(skd_isr_type, int, 0444);
409 MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
410 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
412 #define SKD_MAX_REQ_PER_MSG_DEFAULT 1
413 static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
415 module_param(skd_max_req_per_msg, int, 0444);
416 MODULE_PARM_DESC(skd_max_req_per_msg,
417 "Maximum SCSI requests packed in a single message."
418 " (1-14, default==1)");
420 #define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
421 #define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
422 static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
424 module_param(skd_max_queue_depth, int, 0444);
425 MODULE_PARM_DESC(skd_max_queue_depth,
426 "Maximum SCSI requests issued to s1120."
427 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
429 static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
430 module_param(skd_sgs_per_request, int, 0444);
431 MODULE_PARM_DESC(skd_sgs_per_request,
432 "Maximum SG elements per block request."
433 " (1-4096, default==256)");
435 static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
436 module_param(skd_max_pass_thru, int, 0444);
437 MODULE_PARM_DESC(skd_max_pass_thru,
438 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");
440 module_param(skd_dbg_level, int, 0444);
441 MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
443 module_param(skd_isr_comp_limit, int, 0444);
444 MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
446 /* Major device number dynamically assigned. */
447 static u32 skd_major;
449 static void skd_destruct(struct skd_device *skdev);
450 static const struct block_device_operations skd_blockdev_ops;
451 static void skd_send_fitmsg(struct skd_device *skdev,
452 struct skd_fitmsg_context *skmsg);
453 static void skd_send_special_fitmsg(struct skd_device *skdev,
454 struct skd_special_context *skspcl);
455 static void skd_request_fn(struct request_queue *rq);
456 static void skd_end_request(struct skd_device *skdev,
457 struct skd_request_context *skreq, int error);
458 static int skd_preop_sg_list(struct skd_device *skdev,
459 struct skd_request_context *skreq);
460 static void skd_postop_sg_list(struct skd_device *skdev,
461 struct skd_request_context *skreq);
463 static void skd_restart_device(struct skd_device *skdev);
464 static int skd_quiesce_dev(struct skd_device *skdev);
465 static int skd_unquiesce_dev(struct skd_device *skdev);
466 static void skd_release_special(struct skd_device *skdev,
467 struct skd_special_context *skspcl);
468 static void skd_disable_interrupts(struct skd_device *skdev);
469 static void skd_isr_fwstate(struct skd_device *skdev);
470 static void skd_recover_requests(struct skd_device *skdev, int requeue);
471 static void skd_soft_reset(struct skd_device *skdev);
473 static const char *skd_name(struct skd_device *skdev);
474 const char *skd_drive_state_to_str(int state);
475 const char *skd_skdev_state_to_str(enum skd_drvr_state state);
476 static void skd_log_skdev(struct skd_device *skdev, const char *event);
477 static void skd_log_skmsg(struct skd_device *skdev,
478 struct skd_fitmsg_context *skmsg, const char *event);
479 static void skd_log_skreq(struct skd_device *skdev,
480 struct skd_request_context *skreq, const char *event);
483 *****************************************************************************
484 * READ/WRITE REQUESTS
485 *****************************************************************************
487 static void skd_fail_all_pending(struct skd_device *skdev)
489 struct request_queue *q = skdev->queue;
490 struct request *req;
492 for (;; ) {
493 req = blk_peek_request(q);
494 if (req == NULL)
495 break;
496 blk_start_request(req);
497 __blk_end_request_all(req, -EIO);
501 static void
502 skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
503 int data_dir, unsigned lba,
504 unsigned count)
506 if (data_dir == READ)
507 scsi_req->cdb[0] = 0x28;
508 else
509 scsi_req->cdb[0] = 0x2a;
511 scsi_req->cdb[1] = 0;
512 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
513 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
514 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
515 scsi_req->cdb[5] = (lba & 0xff);
516 scsi_req->cdb[6] = 0;
517 scsi_req->cdb[7] = (count & 0xff00) >> 8;
518 scsi_req->cdb[8] = count & 0xff;
519 scsi_req->cdb[9] = 0;
522 static void
523 skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
524 struct skd_request_context *skreq)
526 skreq->flush_cmd = 1;
528 scsi_req->cdb[0] = 0x35;
529 scsi_req->cdb[1] = 0;
530 scsi_req->cdb[2] = 0;
531 scsi_req->cdb[3] = 0;
532 scsi_req->cdb[4] = 0;
533 scsi_req->cdb[5] = 0;
534 scsi_req->cdb[6] = 0;
535 scsi_req->cdb[7] = 0;
536 scsi_req->cdb[8] = 0;
537 scsi_req->cdb[9] = 0;
540 static void skd_request_fn_not_online(struct request_queue *q);
542 static void skd_request_fn(struct request_queue *q)
544 struct skd_device *skdev = q->queuedata;
545 struct skd_fitmsg_context *skmsg = NULL;
546 struct fit_msg_hdr *fmh = NULL;
547 struct skd_request_context *skreq;
548 struct request *req = NULL;
549 struct skd_scsi_request *scsi_req;
550 unsigned long io_flags;
551 int error;
552 u32 lba;
553 u32 count;
554 int data_dir;
555 u32 be_lba;
556 u32 be_count;
557 u64 be_dmaa;
558 u64 cmdctxt;
559 u32 timo_slot;
560 void *cmd_ptr;
561 int flush, fua;
563 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
564 skd_request_fn_not_online(q);
565 return;
568 if (blk_queue_stopped(skdev->queue)) {
569 if (skdev->skmsg_free_list == NULL ||
570 skdev->skreq_free_list == NULL ||
571 skdev->in_flight >= skdev->queue_low_water_mark)
572 /* There is still some kind of shortage */
573 return;
575 queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
579 * Stop conditions:
580 * - There are no more native requests
581 * - There are already the maximum number of requests in progress
582 * - There are no more skd_request_context entries
583 * - There are no more FIT msg buffers
585 for (;; ) {
587 flush = fua = 0;
589 req = blk_peek_request(q);
591 /* Are there any native requests to start? */
592 if (req == NULL)
593 break;
595 lba = (u32)blk_rq_pos(req);
596 count = blk_rq_sectors(req);
597 data_dir = rq_data_dir(req);
598 io_flags = req->cmd_flags;
600 if (io_flags & REQ_FLUSH)
601 flush++;
603 if (io_flags & REQ_FUA)
604 fua++;
606 pr_debug("%s:%s:%d new req=%p lba=%u(0x%x) "
607 "count=%u(0x%x) dir=%d\n",
608 skdev->name, __func__, __LINE__,
609 req, lba, lba, count, count, data_dir);
611 /* At this point we know there is a request */
613 /* Are too many requets already in progress? */
614 if (skdev->in_flight >= skdev->cur_max_queue_depth) {
615 pr_debug("%s:%s:%d qdepth %d, limit %d\n",
616 skdev->name, __func__, __LINE__,
617 skdev->in_flight, skdev->cur_max_queue_depth);
618 break;
621 /* Is a skd_request_context available? */
622 skreq = skdev->skreq_free_list;
623 if (skreq == NULL) {
624 pr_debug("%s:%s:%d Out of req=%p\n",
625 skdev->name, __func__, __LINE__, q);
626 break;
628 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
629 SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);
631 /* Now we check to see if we can get a fit msg */
632 if (skmsg == NULL) {
633 if (skdev->skmsg_free_list == NULL) {
634 pr_debug("%s:%s:%d Out of msg\n",
635 skdev->name, __func__, __LINE__);
636 break;
640 skreq->flush_cmd = 0;
641 skreq->n_sg = 0;
642 skreq->sg_byte_count = 0;
645 * OK to now dequeue request from q.
647 * At this point we are comitted to either start or reject
648 * the native request. Note that skd_request_context is
649 * available but is still at the head of the free list.
651 blk_start_request(req);
652 skreq->req = req;
653 skreq->fitmsg_id = 0;
655 /* Either a FIT msg is in progress or we have to start one. */
656 if (skmsg == NULL) {
657 /* Are there any FIT msg buffers available? */
658 skmsg = skdev->skmsg_free_list;
659 if (skmsg == NULL) {
660 pr_debug("%s:%s:%d Out of msg skdev=%p\n",
661 skdev->name, __func__, __LINE__,
662 skdev);
663 break;
665 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
666 SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);
668 skdev->skmsg_free_list = skmsg->next;
670 skmsg->state = SKD_MSG_STATE_BUSY;
671 skmsg->id += SKD_ID_INCR;
673 /* Initialize the FIT msg header */
674 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
675 memset(fmh, 0, sizeof(*fmh));
676 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
677 skmsg->length = sizeof(*fmh);
680 skreq->fitmsg_id = skmsg->id;
683 * Note that a FIT msg may have just been started
684 * but contains no SoFIT requests yet.
688 * Transcode the request, checking as we go. The outcome of
689 * the transcoding is represented by the error variable.
691 cmd_ptr = &skmsg->msg_buf[skmsg->length];
692 memset(cmd_ptr, 0, 32);
694 be_lba = cpu_to_be32(lba);
695 be_count = cpu_to_be32(count);
696 be_dmaa = cpu_to_be64((u64)skreq->sksg_dma_address);
697 cmdctxt = skreq->id + SKD_ID_INCR;
699 scsi_req = cmd_ptr;
700 scsi_req->hdr.tag = cmdctxt;
701 scsi_req->hdr.sg_list_dma_address = be_dmaa;
703 if (data_dir == READ)
704 skreq->sg_data_dir = SKD_DATA_DIR_CARD_TO_HOST;
705 else
706 skreq->sg_data_dir = SKD_DATA_DIR_HOST_TO_CARD;
708 if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
709 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
710 SKD_ASSERT(skreq->flush_cmd == 1);
712 } else {
713 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
716 if (fua)
717 scsi_req->cdb[1] |= SKD_FUA_NV;
719 if (!req->bio)
720 goto skip_sg;
722 error = skd_preop_sg_list(skdev, skreq);
724 if (error != 0) {
726 * Complete the native request with error.
727 * Note that the request context is still at the
728 * head of the free list, and that the SoFIT request
729 * was encoded into the FIT msg buffer but the FIT
730 * msg length has not been updated. In short, the
731 * only resource that has been allocated but might
732 * not be used is that the FIT msg could be empty.
734 pr_debug("%s:%s:%d error Out\n",
735 skdev->name, __func__, __LINE__);
736 skd_end_request(skdev, skreq, error);
737 continue;
740 skip_sg:
741 scsi_req->hdr.sg_list_len_bytes =
742 cpu_to_be32(skreq->sg_byte_count);
744 /* Complete resource allocations. */
745 skdev->skreq_free_list = skreq->next;
746 skreq->state = SKD_REQ_STATE_BUSY;
747 skreq->id += SKD_ID_INCR;
749 skmsg->length += sizeof(struct skd_scsi_request);
750 fmh->num_protocol_cmds_coalesced++;
753 * Update the active request counts.
754 * Capture the timeout timestamp.
756 skreq->timeout_stamp = skdev->timeout_stamp;
757 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
758 skdev->timeout_slot[timo_slot]++;
759 skdev->in_flight++;
760 pr_debug("%s:%s:%d req=0x%x busy=%d\n",
761 skdev->name, __func__, __LINE__,
762 skreq->id, skdev->in_flight);
765 * If the FIT msg buffer is full send it.
767 if (skmsg->length >= SKD_N_FITMSG_BYTES ||
768 fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
769 skd_send_fitmsg(skdev, skmsg);
770 skmsg = NULL;
771 fmh = NULL;
776 * Is a FIT msg in progress? If it is empty put the buffer back
777 * on the free list. If it is non-empty send what we got.
778 * This minimizes latency when there are fewer requests than
779 * what fits in a FIT msg.
781 if (skmsg != NULL) {
782 /* Bigger than just a FIT msg header? */
783 if (skmsg->length > sizeof(struct fit_msg_hdr)) {
784 pr_debug("%s:%s:%d sending msg=%p, len %d\n",
785 skdev->name, __func__, __LINE__,
786 skmsg, skmsg->length);
787 skd_send_fitmsg(skdev, skmsg);
788 } else {
790 * The FIT msg is empty. It means we got started
791 * on the msg, but the requests were rejected.
793 skmsg->state = SKD_MSG_STATE_IDLE;
794 skmsg->id += SKD_ID_INCR;
795 skmsg->next = skdev->skmsg_free_list;
796 skdev->skmsg_free_list = skmsg;
798 skmsg = NULL;
799 fmh = NULL;
803 * If req is non-NULL it means there is something to do but
804 * we are out of a resource.
806 if (req)
807 blk_stop_queue(skdev->queue);
810 static void skd_end_request(struct skd_device *skdev,
811 struct skd_request_context *skreq, int error)
813 if (unlikely(error)) {
814 struct request *req = skreq->req;
815 char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
816 u32 lba = (u32)blk_rq_pos(req);
817 u32 count = blk_rq_sectors(req);
819 pr_err("(%s): Error cmd=%s sect=%u count=%u id=0x%x\n",
820 skd_name(skdev), cmd, lba, count, skreq->id);
821 } else
822 pr_debug("%s:%s:%d id=0x%x error=%d\n",
823 skdev->name, __func__, __LINE__, skreq->id, error);
825 __blk_end_request_all(skreq->req, error);
828 static int skd_preop_sg_list(struct skd_device *skdev,
829 struct skd_request_context *skreq)
831 struct request *req = skreq->req;
832 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
833 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
834 struct scatterlist *sg = &skreq->sg[0];
835 int n_sg;
836 int i;
838 skreq->sg_byte_count = 0;
840 /* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
841 skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */
843 n_sg = blk_rq_map_sg(skdev->queue, req, sg);
844 if (n_sg <= 0)
845 return -EINVAL;
848 * Map scatterlist to PCI bus addresses.
849 * Note PCI might change the number of entries.
851 n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
852 if (n_sg <= 0)
853 return -EINVAL;
855 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
857 skreq->n_sg = n_sg;
859 for (i = 0; i < n_sg; i++) {
860 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
861 u32 cnt = sg_dma_len(&sg[i]);
862 uint64_t dma_addr = sg_dma_address(&sg[i]);
864 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
865 sgd->byte_count = cnt;
866 skreq->sg_byte_count += cnt;
867 sgd->host_side_addr = dma_addr;
868 sgd->dev_side_addr = 0;
871 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
872 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
874 if (unlikely(skdev->dbg_level > 1)) {
875 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
876 skdev->name, __func__, __LINE__,
877 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
878 for (i = 0; i < n_sg; i++) {
879 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
880 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
881 "addr=0x%llx next=0x%llx\n",
882 skdev->name, __func__, __LINE__,
883 i, sgd->byte_count, sgd->control,
884 sgd->host_side_addr, sgd->next_desc_ptr);
888 return 0;
891 static void skd_postop_sg_list(struct skd_device *skdev,
892 struct skd_request_context *skreq)
894 int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
895 int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
898 * restore the next ptr for next IO request so we
899 * don't have to set it every time.
901 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
902 skreq->sksg_dma_address +
903 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
904 pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
907 static void skd_request_fn_not_online(struct request_queue *q)
909 struct skd_device *skdev = q->queuedata;
910 int error;
912 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
914 skd_log_skdev(skdev, "req_not_online");
915 switch (skdev->state) {
916 case SKD_DRVR_STATE_PAUSING:
917 case SKD_DRVR_STATE_PAUSED:
918 case SKD_DRVR_STATE_STARTING:
919 case SKD_DRVR_STATE_RESTARTING:
920 case SKD_DRVR_STATE_WAIT_BOOT:
921 /* In case of starting, we haven't started the queue,
922 * so we can't get here... but requests are
923 * possibly hanging out waiting for us because we
924 * reported the dev/skd0 already. They'll wait
925 * forever if connect doesn't complete.
926 * What to do??? delay dev/skd0 ??
928 case SKD_DRVR_STATE_BUSY:
929 case SKD_DRVR_STATE_BUSY_IMMINENT:
930 case SKD_DRVR_STATE_BUSY_ERASE:
931 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
932 return;
934 case SKD_DRVR_STATE_BUSY_SANITIZE:
935 case SKD_DRVR_STATE_STOPPING:
936 case SKD_DRVR_STATE_SYNCING:
937 case SKD_DRVR_STATE_FAULT:
938 case SKD_DRVR_STATE_DISAPPEARED:
939 default:
940 error = -EIO;
941 break;
944 /* If we get here, terminate all pending block requeusts
945 * with EIO and any scsi pass thru with appropriate sense
948 skd_fail_all_pending(skdev);
952 *****************************************************************************
953 * TIMER
954 *****************************************************************************
957 static void skd_timer_tick_not_online(struct skd_device *skdev);
959 static void skd_timer_tick(ulong arg)
961 struct skd_device *skdev = (struct skd_device *)arg;
963 u32 timo_slot;
964 u32 overdue_timestamp;
965 unsigned long reqflags;
966 u32 state;
968 if (skdev->state == SKD_DRVR_STATE_FAULT)
969 /* The driver has declared fault, and we want it to
970 * stay that way until driver is reloaded.
972 return;
974 spin_lock_irqsave(&skdev->lock, reqflags);
976 state = SKD_READL(skdev, FIT_STATUS);
977 state &= FIT_SR_DRIVE_STATE_MASK;
978 if (state != skdev->drive_state)
979 skd_isr_fwstate(skdev);
981 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
982 skd_timer_tick_not_online(skdev);
983 goto timer_func_out;
985 skdev->timeout_stamp++;
986 timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
989 * All requests that happened during the previous use of
990 * this slot should be done by now. The previous use was
991 * over 7 seconds ago.
993 if (skdev->timeout_slot[timo_slot] == 0)
994 goto timer_func_out;
996 /* Something is overdue */
997 overdue_timestamp = skdev->timeout_stamp - SKD_N_TIMEOUT_SLOT;
999 pr_debug("%s:%s:%d found %d timeouts, draining busy=%d\n",
1000 skdev->name, __func__, __LINE__,
1001 skdev->timeout_slot[timo_slot], skdev->in_flight);
1002 pr_err("(%s): Overdue IOs (%d), busy %d\n",
1003 skd_name(skdev), skdev->timeout_slot[timo_slot],
1004 skdev->in_flight);
1006 skdev->timer_countdown = SKD_DRAINING_TIMO;
1007 skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
1008 skdev->timo_slot = timo_slot;
1009 blk_stop_queue(skdev->queue);
1011 timer_func_out:
1012 mod_timer(&skdev->timer, (jiffies + HZ));
1014 spin_unlock_irqrestore(&skdev->lock, reqflags);
1017 static void skd_timer_tick_not_online(struct skd_device *skdev)
1019 switch (skdev->state) {
1020 case SKD_DRVR_STATE_IDLE:
1021 case SKD_DRVR_STATE_LOAD:
1022 break;
1023 case SKD_DRVR_STATE_BUSY_SANITIZE:
1024 pr_debug("%s:%s:%d drive busy sanitize[%x], driver[%x]\n",
1025 skdev->name, __func__, __LINE__,
1026 skdev->drive_state, skdev->state);
1027 /* If we've been in sanitize for 3 seconds, we figure we're not
1028 * going to get anymore completions, so recover requests now
1030 if (skdev->timer_countdown > 0) {
1031 skdev->timer_countdown--;
1032 return;
1034 skd_recover_requests(skdev, 0);
1035 break;
1037 case SKD_DRVR_STATE_BUSY:
1038 case SKD_DRVR_STATE_BUSY_IMMINENT:
1039 case SKD_DRVR_STATE_BUSY_ERASE:
1040 pr_debug("%s:%s:%d busy[%x], countdown=%d\n",
1041 skdev->name, __func__, __LINE__,
1042 skdev->state, skdev->timer_countdown);
1043 if (skdev->timer_countdown > 0) {
1044 skdev->timer_countdown--;
1045 return;
1047 pr_debug("%s:%s:%d busy[%x], timedout=%d, restarting device.",
1048 skdev->name, __func__, __LINE__,
1049 skdev->state, skdev->timer_countdown);
1050 skd_restart_device(skdev);
1051 break;
1053 case SKD_DRVR_STATE_WAIT_BOOT:
1054 case SKD_DRVR_STATE_STARTING:
1055 if (skdev->timer_countdown > 0) {
1056 skdev->timer_countdown--;
1057 return;
1059 /* For now, we fault the drive. Could attempt resets to
1060 * revcover at some point. */
1061 skdev->state = SKD_DRVR_STATE_FAULT;
1063 pr_err("(%s): DriveFault Connect Timeout (%x)\n",
1064 skd_name(skdev), skdev->drive_state);
1066 /*start the queue so we can respond with error to requests */
1067 /* wakeup anyone waiting for startup complete */
1068 blk_start_queue(skdev->queue);
1069 skdev->gendisk_on = -1;
1070 wake_up_interruptible(&skdev->waitq);
1071 break;
1073 case SKD_DRVR_STATE_ONLINE:
1074 /* shouldn't get here. */
1075 break;
1077 case SKD_DRVR_STATE_PAUSING:
1078 case SKD_DRVR_STATE_PAUSED:
1079 break;
1081 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
1082 pr_debug("%s:%s:%d "
1083 "draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
1084 skdev->name, __func__, __LINE__,
1085 skdev->timo_slot,
1086 skdev->timer_countdown,
1087 skdev->in_flight,
1088 skdev->timeout_slot[skdev->timo_slot]);
1089 /* if the slot has cleared we can let the I/O continue */
1090 if (skdev->timeout_slot[skdev->timo_slot] == 0) {
1091 pr_debug("%s:%s:%d Slot drained, starting queue.\n",
1092 skdev->name, __func__, __LINE__);
1093 skdev->state = SKD_DRVR_STATE_ONLINE;
1094 blk_start_queue(skdev->queue);
1095 return;
1097 if (skdev->timer_countdown > 0) {
1098 skdev->timer_countdown--;
1099 return;
1101 skd_restart_device(skdev);
1102 break;
1104 case SKD_DRVR_STATE_RESTARTING:
1105 if (skdev->timer_countdown > 0) {
1106 skdev->timer_countdown--;
1107 return;
1109 /* For now, we fault the drive. Could attempt resets to
1110 * revcover at some point. */
1111 skdev->state = SKD_DRVR_STATE_FAULT;
1112 pr_err("(%s): DriveFault Reconnect Timeout (%x)\n",
1113 skd_name(skdev), skdev->drive_state);
1116 * Recovering does two things:
1117 * 1. completes IO with error
1118 * 2. reclaims dma resources
1119 * When is it safe to recover requests?
1120 * - if the drive state is faulted
1121 * - if the state is still soft reset after out timeout
1122 * - if the drive registers are dead (state = FF)
1123 * If it is "unsafe", we still need to recover, so we will
1124 * disable pci bus mastering and disable our interrupts.
1127 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
1128 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
1129 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
1130 /* It never came out of soft reset. Try to
1131 * recover the requests and then let them
1132 * fail. This is to mitigate hung processes. */
1133 skd_recover_requests(skdev, 0);
1134 else {
1135 pr_err("(%s): Disable BusMaster (%x)\n",
1136 skd_name(skdev), skdev->drive_state);
1137 pci_disable_device(skdev->pdev);
1138 skd_disable_interrupts(skdev);
1139 skd_recover_requests(skdev, 0);
1142 /*start the queue so we can respond with error to requests */
1143 /* wakeup anyone waiting for startup complete */
1144 blk_start_queue(skdev->queue);
1145 skdev->gendisk_on = -1;
1146 wake_up_interruptible(&skdev->waitq);
1147 break;
1149 case SKD_DRVR_STATE_RESUMING:
1150 case SKD_DRVR_STATE_STOPPING:
1151 case SKD_DRVR_STATE_SYNCING:
1152 case SKD_DRVR_STATE_FAULT:
1153 case SKD_DRVR_STATE_DISAPPEARED:
1154 default:
1155 break;
1159 static int skd_start_timer(struct skd_device *skdev)
1161 int rc;
1163 init_timer(&skdev->timer);
1164 setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);
1166 rc = mod_timer(&skdev->timer, (jiffies + HZ));
1167 if (rc)
1168 pr_err("%s: failed to start timer %d\n",
1169 __func__, rc);
1170 return rc;
1173 static void skd_kill_timer(struct skd_device *skdev)
1175 del_timer_sync(&skdev->timer);
1179 *****************************************************************************
1180 * IOCTL
1181 *****************************************************************************
1183 static int skd_ioctl_sg_io(struct skd_device *skdev,
1184 fmode_t mode, void __user *argp);
1185 static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1186 struct skd_sg_io *sksgio);
1187 static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1188 struct skd_sg_io *sksgio);
1189 static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1190 struct skd_sg_io *sksgio);
1191 static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1192 struct skd_sg_io *sksgio, int dxfer_dir);
1193 static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1194 struct skd_sg_io *sksgio);
1195 static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
1196 static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1197 struct skd_sg_io *sksgio);
1198 static int skd_sg_io_put_status(struct skd_device *skdev,
1199 struct skd_sg_io *sksgio);
1201 static void skd_complete_special(struct skd_device *skdev,
1202 volatile struct fit_completion_entry_v1
1203 *skcomp,
1204 volatile struct fit_comp_error_info *skerr,
1205 struct skd_special_context *skspcl);
1207 static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
1208 uint cmd_in, ulong arg)
1210 int rc = 0;
1211 struct gendisk *disk = bdev->bd_disk;
1212 struct skd_device *skdev = disk->private_data;
1213 void __user *p = (void *)arg;
1215 pr_debug("%s:%s:%d %s: CMD[%s] ioctl mode 0x%x, cmd 0x%x arg %0lx\n",
1216 skdev->name, __func__, __LINE__,
1217 disk->disk_name, current->comm, mode, cmd_in, arg);
1219 if (!capable(CAP_SYS_ADMIN))
1220 return -EPERM;
1222 switch (cmd_in) {
1223 case SG_SET_TIMEOUT:
1224 case SG_GET_TIMEOUT:
1225 case SG_GET_VERSION_NUM:
1226 rc = scsi_cmd_ioctl(disk->queue, disk, mode, cmd_in, p);
1227 break;
1228 case SG_IO:
1229 rc = skd_ioctl_sg_io(skdev, mode, p);
1230 break;
1232 default:
1233 rc = -ENOTTY;
1234 break;
1237 pr_debug("%s:%s:%d %s: completion rc %d\n",
1238 skdev->name, __func__, __LINE__, disk->disk_name, rc);
1239 return rc;
1242 static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
1243 void __user *argp)
1245 int rc;
1246 struct skd_sg_io sksgio;
1248 memset(&sksgio, 0, sizeof(sksgio));
1249 sksgio.mode = mode;
1250 sksgio.argp = argp;
1251 sksgio.iov = &sksgio.no_iov_iov;
1253 switch (skdev->state) {
1254 case SKD_DRVR_STATE_ONLINE:
1255 case SKD_DRVR_STATE_BUSY_IMMINENT:
1256 break;
1258 default:
1259 pr_debug("%s:%s:%d drive not online\n",
1260 skdev->name, __func__, __LINE__);
1261 rc = -ENXIO;
1262 goto out;
1265 rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
1266 if (rc)
1267 goto out;
1269 rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
1270 if (rc)
1271 goto out;
1273 rc = skd_sg_io_prep_buffering(skdev, &sksgio);
1274 if (rc)
1275 goto out;
1277 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
1278 if (rc)
1279 goto out;
1281 rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
1282 if (rc)
1283 goto out;
1285 rc = skd_sg_io_await(skdev, &sksgio);
1286 if (rc)
1287 goto out;
1289 rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
1290 if (rc)
1291 goto out;
1293 rc = skd_sg_io_put_status(skdev, &sksgio);
1294 if (rc)
1295 goto out;
1297 rc = 0;
1299 out:
1300 skd_sg_io_release_skspcl(skdev, &sksgio);
1302 if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
1303 kfree(sksgio.iov);
1304 return rc;
1307 static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
1308 struct skd_sg_io *sksgio)
1310 struct sg_io_hdr *sgp = &sksgio->sg;
1311 int i, acc;
1313 if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
1314 pr_debug("%s:%s:%d access sg failed %p\n",
1315 skdev->name, __func__, __LINE__, sksgio->argp);
1316 return -EFAULT;
1319 if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
1320 pr_debug("%s:%s:%d copy_from_user sg failed %p\n",
1321 skdev->name, __func__, __LINE__, sksgio->argp);
1322 return -EFAULT;
1325 if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
1326 pr_debug("%s:%s:%d interface_id invalid 0x%x\n",
1327 skdev->name, __func__, __LINE__, sgp->interface_id);
1328 return -EINVAL;
1331 if (sgp->cmd_len > sizeof(sksgio->cdb)) {
1332 pr_debug("%s:%s:%d cmd_len invalid %d\n",
1333 skdev->name, __func__, __LINE__, sgp->cmd_len);
1334 return -EINVAL;
1337 if (sgp->iovec_count > 256) {
1338 pr_debug("%s:%s:%d iovec_count invalid %d\n",
1339 skdev->name, __func__, __LINE__, sgp->iovec_count);
1340 return -EINVAL;
1343 if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
1344 pr_debug("%s:%s:%d dxfer_len invalid %d\n",
1345 skdev->name, __func__, __LINE__, sgp->dxfer_len);
1346 return -EINVAL;
1349 switch (sgp->dxfer_direction) {
1350 case SG_DXFER_NONE:
1351 acc = -1;
1352 break;
1354 case SG_DXFER_TO_DEV:
1355 acc = VERIFY_READ;
1356 break;
1358 case SG_DXFER_FROM_DEV:
1359 case SG_DXFER_TO_FROM_DEV:
1360 acc = VERIFY_WRITE;
1361 break;
1363 default:
1364 pr_debug("%s:%s:%d dxfer_dir invalid %d\n",
1365 skdev->name, __func__, __LINE__, sgp->dxfer_direction);
1366 return -EINVAL;
1369 if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
1370 pr_debug("%s:%s:%d copy_from_user cmdp failed %p\n",
1371 skdev->name, __func__, __LINE__, sgp->cmdp);
1372 return -EFAULT;
1375 if (sgp->mx_sb_len != 0) {
1376 if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
1377 pr_debug("%s:%s:%d access sbp failed %p\n",
1378 skdev->name, __func__, __LINE__, sgp->sbp);
1379 return -EFAULT;
1383 if (sgp->iovec_count == 0) {
1384 sksgio->iov[0].iov_base = sgp->dxferp;
1385 sksgio->iov[0].iov_len = sgp->dxfer_len;
1386 sksgio->iovcnt = 1;
1387 sksgio->dxfer_len = sgp->dxfer_len;
1388 } else {
1389 struct sg_iovec *iov;
1390 uint nbytes = sizeof(*iov) * sgp->iovec_count;
1391 size_t iov_data_len;
1393 iov = kmalloc(nbytes, GFP_KERNEL);
1394 if (iov == NULL) {
1395 pr_debug("%s:%s:%d alloc iovec failed %d\n",
1396 skdev->name, __func__, __LINE__,
1397 sgp->iovec_count);
1398 return -ENOMEM;
1400 sksgio->iov = iov;
1401 sksgio->iovcnt = sgp->iovec_count;
1403 if (copy_from_user(iov, sgp->dxferp, nbytes)) {
1404 pr_debug("%s:%s:%d copy_from_user iovec failed %p\n",
1405 skdev->name, __func__, __LINE__, sgp->dxferp);
1406 return -EFAULT;
1410 * Sum up the vecs, making sure they don't overflow
1412 iov_data_len = 0;
1413 for (i = 0; i < sgp->iovec_count; i++) {
1414 if (iov_data_len + iov[i].iov_len < iov_data_len)
1415 return -EINVAL;
1416 iov_data_len += iov[i].iov_len;
1419 /* SG_IO howto says that the shorter of the two wins */
1420 if (sgp->dxfer_len < iov_data_len) {
1421 sksgio->iovcnt = iov_shorten((struct iovec *)iov,
1422 sgp->iovec_count,
1423 sgp->dxfer_len);
1424 sksgio->dxfer_len = sgp->dxfer_len;
1425 } else
1426 sksgio->dxfer_len = iov_data_len;
1429 if (sgp->dxfer_direction != SG_DXFER_NONE) {
1430 struct sg_iovec *iov = sksgio->iov;
1431 for (i = 0; i < sksgio->iovcnt; i++, iov++) {
1432 if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
1433 pr_debug("%s:%s:%d access data failed %p/%d\n",
1434 skdev->name, __func__, __LINE__,
1435 iov->iov_base, (int)iov->iov_len);
1436 return -EFAULT;
1441 return 0;
1444 static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
1445 struct skd_sg_io *sksgio)
1447 struct skd_special_context *skspcl = NULL;
1448 int rc;
1450 for (;;) {
1451 ulong flags;
1453 spin_lock_irqsave(&skdev->lock, flags);
1454 skspcl = skdev->skspcl_free_list;
1455 if (skspcl != NULL) {
1456 skdev->skspcl_free_list =
1457 (struct skd_special_context *)skspcl->req.next;
1458 skspcl->req.id += SKD_ID_INCR;
1459 skspcl->req.state = SKD_REQ_STATE_SETUP;
1460 skspcl->orphaned = 0;
1461 skspcl->req.n_sg = 0;
1463 spin_unlock_irqrestore(&skdev->lock, flags);
1465 if (skspcl != NULL) {
1466 rc = 0;
1467 break;
1470 pr_debug("%s:%s:%d blocking\n",
1471 skdev->name, __func__, __LINE__);
1473 rc = wait_event_interruptible_timeout(
1474 skdev->waitq,
1475 (skdev->skspcl_free_list != NULL),
1476 msecs_to_jiffies(sksgio->sg.timeout));
1478 pr_debug("%s:%s:%d unblocking, rc=%d\n",
1479 skdev->name, __func__, __LINE__, rc);
1481 if (rc <= 0) {
1482 if (rc == 0)
1483 rc = -ETIMEDOUT;
1484 else
1485 rc = -EINTR;
1486 break;
1489 * If we get here rc > 0 meaning the timeout to
1490 * wait_event_interruptible_timeout() had time left, hence the
1491 * sought event -- non-empty free list -- happened.
1492 * Retry the allocation.
1495 sksgio->skspcl = skspcl;
1497 return rc;
1500 static int skd_skreq_prep_buffering(struct skd_device *skdev,
1501 struct skd_request_context *skreq,
1502 u32 dxfer_len)
1504 u32 resid = dxfer_len;
1507 * The DMA engine must have aligned addresses and byte counts.
1509 resid += (-resid) & 3;
1510 skreq->sg_byte_count = resid;
1512 skreq->n_sg = 0;
1514 while (resid > 0) {
1515 u32 nbytes = PAGE_SIZE;
1516 u32 ix = skreq->n_sg;
1517 struct scatterlist *sg = &skreq->sg[ix];
1518 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1519 struct page *page;
1521 if (nbytes > resid)
1522 nbytes = resid;
1524 page = alloc_page(GFP_KERNEL);
1525 if (page == NULL)
1526 return -ENOMEM;
1528 sg_set_page(sg, page, nbytes, 0);
1530 /* TODO: This should be going through a pci_???()
1531 * routine to do proper mapping. */
1532 sksg->control = FIT_SGD_CONTROL_NOT_LAST;
1533 sksg->byte_count = nbytes;
1535 sksg->host_side_addr = sg_phys(sg);
1537 sksg->dev_side_addr = 0;
1538 sksg->next_desc_ptr = skreq->sksg_dma_address +
1539 (ix + 1) * sizeof(*sksg);
1541 skreq->n_sg++;
1542 resid -= nbytes;
1545 if (skreq->n_sg > 0) {
1546 u32 ix = skreq->n_sg - 1;
1547 struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
1549 sksg->control = FIT_SGD_CONTROL_LAST;
1550 sksg->next_desc_ptr = 0;
1553 if (unlikely(skdev->dbg_level > 1)) {
1554 u32 i;
1556 pr_debug("%s:%s:%d skreq=%x sksg_list=%p sksg_dma=%llx\n",
1557 skdev->name, __func__, __LINE__,
1558 skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
1559 for (i = 0; i < skreq->n_sg; i++) {
1560 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
1562 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
1563 "addr=0x%llx next=0x%llx\n",
1564 skdev->name, __func__, __LINE__,
1565 i, sgd->byte_count, sgd->control,
1566 sgd->host_side_addr, sgd->next_desc_ptr);
1570 return 0;
1573 static int skd_sg_io_prep_buffering(struct skd_device *skdev,
1574 struct skd_sg_io *sksgio)
1576 struct skd_special_context *skspcl = sksgio->skspcl;
1577 struct skd_request_context *skreq = &skspcl->req;
1578 u32 dxfer_len = sksgio->dxfer_len;
1579 int rc;
1581 rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
1583 * Eventually, errors or not, skd_release_special() is called
1584 * to recover allocations including partial allocations.
1586 return rc;
1589 static int skd_sg_io_copy_buffer(struct skd_device *skdev,
1590 struct skd_sg_io *sksgio, int dxfer_dir)
1592 struct skd_special_context *skspcl = sksgio->skspcl;
1593 u32 iov_ix = 0;
1594 struct sg_iovec curiov;
1595 u32 sksg_ix = 0;
1596 u8 *bufp = NULL;
1597 u32 buf_len = 0;
1598 u32 resid = sksgio->dxfer_len;
1599 int rc;
1601 curiov.iov_len = 0;
1602 curiov.iov_base = NULL;
1604 if (dxfer_dir != sksgio->sg.dxfer_direction) {
1605 if (dxfer_dir != SG_DXFER_TO_DEV ||
1606 sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
1607 return 0;
1610 while (resid > 0) {
1611 u32 nbytes = PAGE_SIZE;
1613 if (curiov.iov_len == 0) {
1614 curiov = sksgio->iov[iov_ix++];
1615 continue;
1618 if (buf_len == 0) {
1619 struct page *page;
1620 page = sg_page(&skspcl->req.sg[sksg_ix++]);
1621 bufp = page_address(page);
1622 buf_len = PAGE_SIZE;
1625 nbytes = min_t(u32, nbytes, resid);
1626 nbytes = min_t(u32, nbytes, curiov.iov_len);
1627 nbytes = min_t(u32, nbytes, buf_len);
1629 if (dxfer_dir == SG_DXFER_TO_DEV)
1630 rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
1631 else
1632 rc = __copy_to_user(curiov.iov_base, bufp, nbytes);
1634 if (rc)
1635 return -EFAULT;
1637 resid -= nbytes;
1638 curiov.iov_len -= nbytes;
1639 curiov.iov_base += nbytes;
1640 buf_len -= nbytes;
1643 return 0;
1646 static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
1647 struct skd_sg_io *sksgio)
1649 struct skd_special_context *skspcl = sksgio->skspcl;
1650 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
1651 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
1653 memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);
1655 /* Initialize the FIT msg header */
1656 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1657 fmh->num_protocol_cmds_coalesced = 1;
1659 /* Initialize the SCSI request */
1660 if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
1661 scsi_req->hdr.sg_list_dma_address =
1662 cpu_to_be64(skspcl->req.sksg_dma_address);
1663 scsi_req->hdr.tag = skspcl->req.id;
1664 scsi_req->hdr.sg_list_len_bytes =
1665 cpu_to_be32(skspcl->req.sg_byte_count);
1666 memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));
1668 skspcl->req.state = SKD_REQ_STATE_BUSY;
1669 skd_send_special_fitmsg(skdev, skspcl);
1671 return 0;
1674 static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
1676 unsigned long flags;
1677 int rc;
1679 rc = wait_event_interruptible_timeout(skdev->waitq,
1680 (sksgio->skspcl->req.state !=
1681 SKD_REQ_STATE_BUSY),
1682 msecs_to_jiffies(sksgio->sg.
1683 timeout));
1685 spin_lock_irqsave(&skdev->lock, flags);
1687 if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
1688 pr_debug("%s:%s:%d skspcl %p aborted\n",
1689 skdev->name, __func__, __LINE__, sksgio->skspcl);
1691 /* Build check cond, sense and let command finish. */
1692 /* For a timeout, we must fabricate completion and sense
1693 * data to complete the command */
1694 sksgio->skspcl->req.completion.status =
1695 SAM_STAT_CHECK_CONDITION;
1697 memset(&sksgio->skspcl->req.err_info, 0,
1698 sizeof(sksgio->skspcl->req.err_info));
1699 sksgio->skspcl->req.err_info.type = 0x70;
1700 sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
1701 sksgio->skspcl->req.err_info.code = 0x44;
1702 sksgio->skspcl->req.err_info.qual = 0;
1703 rc = 0;
1704 } else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
1705 /* No longer on the adapter. We finish. */
1706 rc = 0;
1707 else {
1708 /* Something's gone wrong. Still busy. Timeout or
1709 * user interrupted (control-C). Mark as an orphan
1710 * so it will be disposed when completed. */
1711 sksgio->skspcl->orphaned = 1;
1712 sksgio->skspcl = NULL;
1713 if (rc == 0) {
1714 pr_debug("%s:%s:%d timed out %p (%u ms)\n",
1715 skdev->name, __func__, __LINE__,
1716 sksgio, sksgio->sg.timeout);
1717 rc = -ETIMEDOUT;
1718 } else {
1719 pr_debug("%s:%s:%d cntlc %p\n",
1720 skdev->name, __func__, __LINE__, sksgio);
1721 rc = -EINTR;
1725 spin_unlock_irqrestore(&skdev->lock, flags);
1727 return rc;
1730 static int skd_sg_io_put_status(struct skd_device *skdev,
1731 struct skd_sg_io *sksgio)
1733 struct sg_io_hdr *sgp = &sksgio->sg;
1734 struct skd_special_context *skspcl = sksgio->skspcl;
1735 int resid = 0;
1737 u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);
1739 sgp->status = skspcl->req.completion.status;
1740 resid = sksgio->dxfer_len - nb;
1742 sgp->masked_status = sgp->status & STATUS_MASK;
1743 sgp->msg_status = 0;
1744 sgp->host_status = 0;
1745 sgp->driver_status = 0;
1746 sgp->resid = resid;
1747 if (sgp->masked_status || sgp->host_status || sgp->driver_status)
1748 sgp->info |= SG_INFO_CHECK;
1750 pr_debug("%s:%s:%d status %x masked %x resid 0x%x\n",
1751 skdev->name, __func__, __LINE__,
1752 sgp->status, sgp->masked_status, sgp->resid);
1754 if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
1755 if (sgp->mx_sb_len > 0) {
1756 struct fit_comp_error_info *ei = &skspcl->req.err_info;
1757 u32 nbytes = sizeof(*ei);
1759 nbytes = min_t(u32, nbytes, sgp->mx_sb_len);
1761 sgp->sb_len_wr = nbytes;
1763 if (__copy_to_user(sgp->sbp, ei, nbytes)) {
1764 pr_debug("%s:%s:%d copy_to_user sense failed %p\n",
1765 skdev->name, __func__, __LINE__,
1766 sgp->sbp);
1767 return -EFAULT;
1772 if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
1773 pr_debug("%s:%s:%d copy_to_user sg failed %p\n",
1774 skdev->name, __func__, __LINE__, sksgio->argp);
1775 return -EFAULT;
1778 return 0;
1781 static int skd_sg_io_release_skspcl(struct skd_device *skdev,
1782 struct skd_sg_io *sksgio)
1784 struct skd_special_context *skspcl = sksgio->skspcl;
1786 if (skspcl != NULL) {
1787 ulong flags;
1789 sksgio->skspcl = NULL;
1791 spin_lock_irqsave(&skdev->lock, flags);
1792 skd_release_special(skdev, skspcl);
1793 spin_unlock_irqrestore(&skdev->lock, flags);
1796 return 0;
1800 *****************************************************************************
1801 * INTERNAL REQUESTS -- generated by driver itself
1802 *****************************************************************************
1805 static int skd_format_internal_skspcl(struct skd_device *skdev)
1807 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1808 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1809 struct fit_msg_hdr *fmh;
1810 uint64_t dma_address;
1811 struct skd_scsi_request *scsi;
1813 fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0];
1814 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
1815 fmh->num_protocol_cmds_coalesced = 1;
1817 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1818 memset(scsi, 0, sizeof(*scsi));
1819 dma_address = skspcl->req.sksg_dma_address;
1820 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
1821 sgd->control = FIT_SGD_CONTROL_LAST;
1822 sgd->byte_count = 0;
1823 sgd->host_side_addr = skspcl->db_dma_address;
1824 sgd->dev_side_addr = 0;
1825 sgd->next_desc_ptr = 0LL;
1827 return 1;
1830 #define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
1832 static void skd_send_internal_skspcl(struct skd_device *skdev,
1833 struct skd_special_context *skspcl,
1834 u8 opcode)
1836 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
1837 struct skd_scsi_request *scsi;
1838 unsigned char *buf = skspcl->data_buf;
1839 int i;
1841 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
1843 * A refresh is already in progress.
1844 * Just wait for it to finish.
1846 return;
1848 SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
1849 skspcl->req.state = SKD_REQ_STATE_BUSY;
1850 skspcl->req.id += SKD_ID_INCR;
1852 scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
1853 scsi->hdr.tag = skspcl->req.id;
1855 memset(scsi->cdb, 0, sizeof(scsi->cdb));
1857 switch (opcode) {
1858 case TEST_UNIT_READY:
1859 scsi->cdb[0] = TEST_UNIT_READY;
1860 sgd->byte_count = 0;
1861 scsi->hdr.sg_list_len_bytes = 0;
1862 break;
1864 case READ_CAPACITY:
1865 scsi->cdb[0] = READ_CAPACITY;
1866 sgd->byte_count = SKD_N_READ_CAP_BYTES;
1867 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1868 break;
1870 case INQUIRY:
1871 scsi->cdb[0] = INQUIRY;
1872 scsi->cdb[1] = 0x01; /* evpd */
1873 scsi->cdb[2] = 0x80; /* serial number page */
1874 scsi->cdb[4] = 0x10;
1875 sgd->byte_count = 16;
1876 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1877 break;
1879 case SYNCHRONIZE_CACHE:
1880 scsi->cdb[0] = SYNCHRONIZE_CACHE;
1881 sgd->byte_count = 0;
1882 scsi->hdr.sg_list_len_bytes = 0;
1883 break;
1885 case WRITE_BUFFER:
1886 scsi->cdb[0] = WRITE_BUFFER;
1887 scsi->cdb[1] = 0x02;
1888 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1889 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1890 sgd->byte_count = WR_BUF_SIZE;
1891 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1892 /* fill incrementing byte pattern */
1893 for (i = 0; i < sgd->byte_count; i++)
1894 buf[i] = i & 0xFF;
1895 break;
1897 case READ_BUFFER:
1898 scsi->cdb[0] = READ_BUFFER;
1899 scsi->cdb[1] = 0x02;
1900 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
1901 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
1902 sgd->byte_count = WR_BUF_SIZE;
1903 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
1904 memset(skspcl->data_buf, 0, sgd->byte_count);
1905 break;
1907 default:
1908 SKD_ASSERT("Don't know what to send");
1909 return;
1912 skd_send_special_fitmsg(skdev, skspcl);
1915 static void skd_refresh_device_data(struct skd_device *skdev)
1917 struct skd_special_context *skspcl = &skdev->internal_skspcl;
1919 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1922 static int skd_chk_read_buf(struct skd_device *skdev,
1923 struct skd_special_context *skspcl)
1925 unsigned char *buf = skspcl->data_buf;
1926 int i;
1928 /* check for incrementing byte pattern */
1929 for (i = 0; i < WR_BUF_SIZE; i++)
1930 if (buf[i] != (i & 0xFF))
1931 return 1;
1933 return 0;
1936 static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1937 u8 code, u8 qual, u8 fruc)
1939 /* If the check condition is of special interest, log a message */
1940 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1941 && (code == 0x04) && (qual == 0x06)) {
1942 pr_err("(%s): *** LOST_WRITE_DATA ERROR *** key/asc/"
1943 "ascq/fruc %02x/%02x/%02x/%02x\n",
1944 skd_name(skdev), key, code, qual, fruc);
1948 static void skd_complete_internal(struct skd_device *skdev,
1949 volatile struct fit_completion_entry_v1
1950 *skcomp,
1951 volatile struct fit_comp_error_info *skerr,
1952 struct skd_special_context *skspcl)
1954 u8 *buf = skspcl->data_buf;
1955 u8 status;
1956 int i;
1957 struct skd_scsi_request *scsi =
1958 (struct skd_scsi_request *)&skspcl->msg_buf[64];
1960 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1962 pr_debug("%s:%s:%d complete internal %x\n",
1963 skdev->name, __func__, __LINE__, scsi->cdb[0]);
1965 skspcl->req.completion = *skcomp;
1966 skspcl->req.state = SKD_REQ_STATE_IDLE;
1967 skspcl->req.id += SKD_ID_INCR;
1969 status = skspcl->req.completion.status;
1971 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1972 skerr->qual, skerr->fruc);
1974 switch (scsi->cdb[0]) {
1975 case TEST_UNIT_READY:
1976 if (status == SAM_STAT_GOOD)
1977 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1978 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1979 (skerr->key == MEDIUM_ERROR))
1980 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1981 else {
1982 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1983 pr_debug("%s:%s:%d TUR failed, don't send anymore state 0x%x\n",
1984 skdev->name, __func__, __LINE__,
1985 skdev->state);
1986 return;
1988 pr_debug("%s:%s:%d **** TUR failed, retry skerr\n",
1989 skdev->name, __func__, __LINE__);
1990 skd_send_internal_skspcl(skdev, skspcl, 0x00);
1992 break;
1994 case WRITE_BUFFER:
1995 if (status == SAM_STAT_GOOD)
1996 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1997 else {
1998 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1999 pr_debug("%s:%s:%d write buffer failed, don't send anymore state 0x%x\n",
2000 skdev->name, __func__, __LINE__,
2001 skdev->state);
2002 return;
2004 pr_debug("%s:%s:%d **** write buffer failed, retry skerr\n",
2005 skdev->name, __func__, __LINE__);
2006 skd_send_internal_skspcl(skdev, skspcl, 0x00);
2008 break;
2010 case READ_BUFFER:
2011 if (status == SAM_STAT_GOOD) {
2012 if (skd_chk_read_buf(skdev, skspcl) == 0)
2013 skd_send_internal_skspcl(skdev, skspcl,
2014 READ_CAPACITY);
2015 else {
2016 pr_err(
2017 "(%s):*** W/R Buffer mismatch %d ***\n",
2018 skd_name(skdev), skdev->connect_retries);
2019 if (skdev->connect_retries <
2020 SKD_MAX_CONNECT_RETRIES) {
2021 skdev->connect_retries++;
2022 skd_soft_reset(skdev);
2023 } else {
2024 pr_err(
2025 "(%s): W/R Buffer Connect Error\n",
2026 skd_name(skdev));
2027 return;
2031 } else {
2032 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
2033 pr_debug("%s:%s:%d "
2034 "read buffer failed, don't send anymore state 0x%x\n",
2035 skdev->name, __func__, __LINE__,
2036 skdev->state);
2037 return;
2039 pr_debug("%s:%s:%d "
2040 "**** read buffer failed, retry skerr\n",
2041 skdev->name, __func__, __LINE__);
2042 skd_send_internal_skspcl(skdev, skspcl, 0x00);
2044 break;
2046 case READ_CAPACITY:
2047 skdev->read_cap_is_valid = 0;
2048 if (status == SAM_STAT_GOOD) {
2049 skdev->read_cap_last_lba =
2050 (buf[0] << 24) | (buf[1] << 16) |
2051 (buf[2] << 8) | buf[3];
2052 skdev->read_cap_blocksize =
2053 (buf[4] << 24) | (buf[5] << 16) |
2054 (buf[6] << 8) | buf[7];
2056 pr_debug("%s:%s:%d last lba %d, bs %d\n",
2057 skdev->name, __func__, __LINE__,
2058 skdev->read_cap_last_lba,
2059 skdev->read_cap_blocksize);
2061 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
2063 skdev->read_cap_is_valid = 1;
2065 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2066 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
2067 (skerr->key == MEDIUM_ERROR)) {
2068 skdev->read_cap_last_lba = ~0;
2069 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
2070 pr_debug("%s:%s:%d "
2071 "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n",
2072 skdev->name, __func__, __LINE__);
2073 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
2074 } else {
2075 pr_debug("%s:%s:%d **** READCAP failed, retry TUR\n",
2076 skdev->name, __func__, __LINE__);
2077 skd_send_internal_skspcl(skdev, skspcl,
2078 TEST_UNIT_READY);
2080 break;
2082 case INQUIRY:
2083 skdev->inquiry_is_valid = 0;
2084 if (status == SAM_STAT_GOOD) {
2085 skdev->inquiry_is_valid = 1;
2087 for (i = 0; i < 12; i++)
2088 skdev->inq_serial_num[i] = buf[i + 4];
2089 skdev->inq_serial_num[12] = 0;
2092 if (skd_unquiesce_dev(skdev) < 0)
2093 pr_debug("%s:%s:%d **** failed, to ONLINE device\n",
2094 skdev->name, __func__, __LINE__);
2095 /* connection is complete */
2096 skdev->connect_retries = 0;
2097 break;
2099 case SYNCHRONIZE_CACHE:
2100 if (status == SAM_STAT_GOOD)
2101 skdev->sync_done = 1;
2102 else
2103 skdev->sync_done = -1;
2104 wake_up_interruptible(&skdev->waitq);
2105 break;
2107 default:
2108 SKD_ASSERT("we didn't send this");
2113 *****************************************************************************
2114 * FIT MESSAGES
2115 *****************************************************************************
2118 static void skd_send_fitmsg(struct skd_device *skdev,
2119 struct skd_fitmsg_context *skmsg)
2121 u64 qcmd;
2122 struct fit_msg_hdr *fmh;
2124 pr_debug("%s:%s:%d dma address 0x%llx, busy=%d\n",
2125 skdev->name, __func__, __LINE__,
2126 skmsg->mb_dma_address, skdev->in_flight);
2127 pr_debug("%s:%s:%d msg_buf 0x%p, offset %x\n",
2128 skdev->name, __func__, __LINE__,
2129 skmsg->msg_buf, skmsg->offset);
2131 qcmd = skmsg->mb_dma_address;
2132 qcmd |= FIT_QCMD_QID_NORMAL;
2134 fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
2135 skmsg->outstanding = fmh->num_protocol_cmds_coalesced;
2137 if (unlikely(skdev->dbg_level > 1)) {
2138 u8 *bp = (u8 *)skmsg->msg_buf;
2139 int i;
2140 for (i = 0; i < skmsg->length; i += 8) {
2141 pr_debug("%s:%s:%d msg[%2d] %02x %02x %02x %02x "
2142 "%02x %02x %02x %02x\n",
2143 skdev->name, __func__, __LINE__,
2144 i, bp[i + 0], bp[i + 1], bp[i + 2],
2145 bp[i + 3], bp[i + 4], bp[i + 5],
2146 bp[i + 6], bp[i + 7]);
2147 if (i == 0)
2148 i = 64 - 8;
2152 if (skmsg->length > 256)
2153 qcmd |= FIT_QCMD_MSGSIZE_512;
2154 else if (skmsg->length > 128)
2155 qcmd |= FIT_QCMD_MSGSIZE_256;
2156 else if (skmsg->length > 64)
2157 qcmd |= FIT_QCMD_MSGSIZE_128;
2158 else
2160 * This makes no sense because the FIT msg header is
2161 * 64 bytes. If the msg is only 64 bytes long it has
2162 * no payload.
2164 qcmd |= FIT_QCMD_MSGSIZE_64;
2166 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2170 static void skd_send_special_fitmsg(struct skd_device *skdev,
2171 struct skd_special_context *skspcl)
2173 u64 qcmd;
2175 if (unlikely(skdev->dbg_level > 1)) {
2176 u8 *bp = (u8 *)skspcl->msg_buf;
2177 int i;
2179 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
2180 pr_debug("%s:%s:%d spcl[%2d] %02x %02x %02x %02x "
2181 "%02x %02x %02x %02x\n",
2182 skdev->name, __func__, __LINE__, i,
2183 bp[i + 0], bp[i + 1], bp[i + 2], bp[i + 3],
2184 bp[i + 4], bp[i + 5], bp[i + 6], bp[i + 7]);
2185 if (i == 0)
2186 i = 64 - 8;
2189 pr_debug("%s:%s:%d skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
2190 skdev->name, __func__, __LINE__,
2191 skspcl, skspcl->req.id, skspcl->req.sksg_list,
2192 skspcl->req.sksg_dma_address);
2193 for (i = 0; i < skspcl->req.n_sg; i++) {
2194 struct fit_sg_descriptor *sgd =
2195 &skspcl->req.sksg_list[i];
2197 pr_debug("%s:%s:%d sg[%d] count=%u ctrl=0x%x "
2198 "addr=0x%llx next=0x%llx\n",
2199 skdev->name, __func__, __LINE__,
2200 i, sgd->byte_count, sgd->control,
2201 sgd->host_side_addr, sgd->next_desc_ptr);
2206 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
2207 * and one 64-byte SSDI command.
2209 qcmd = skspcl->mb_dma_address;
2210 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
2212 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
2216 *****************************************************************************
2217 * COMPLETION QUEUE
2218 *****************************************************************************
2221 static void skd_complete_other(struct skd_device *skdev,
2222 volatile struct fit_completion_entry_v1 *skcomp,
2223 volatile struct fit_comp_error_info *skerr);
2225 struct sns_info {
2226 u8 type;
2227 u8 stat;
2228 u8 key;
2229 u8 asc;
2230 u8 ascq;
2231 u8 mask;
2232 enum skd_check_status_action action;
2235 static struct sns_info skd_chkstat_table[] = {
2236 /* Good */
2237 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
2238 SKD_CHECK_STATUS_REPORT_GOOD },
2240 /* Smart alerts */
2241 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
2242 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2243 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
2244 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2245 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
2246 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
2248 /* Retry (with limits) */
2249 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
2250 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2251 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
2252 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2253 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
2254 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2255 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
2256 SKD_CHECK_STATUS_REQUEUE_REQUEST },
2258 /* Busy (or about to be) */
2259 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
2260 SKD_CHECK_STATUS_BUSY_IMMINENT },
2264 * Look up status and sense data to decide how to handle the error
2265 * from the device.
2266 * mask says which fields must match e.g., mask=0x18 means check
2267 * type and stat, ignore key, asc, ascq.
2270 static enum skd_check_status_action
2271 skd_check_status(struct skd_device *skdev,
2272 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
2274 int i, n;
2276 pr_err("(%s): key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
2277 skd_name(skdev), skerr->key, skerr->code, skerr->qual,
2278 skerr->fruc);
2280 pr_debug("%s:%s:%d stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
2281 skdev->name, __func__, __LINE__, skerr->type, cmp_status,
2282 skerr->key, skerr->code, skerr->qual, skerr->fruc);
2284 /* Does the info match an entry in the good category? */
2285 n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
2286 for (i = 0; i < n; i++) {
2287 struct sns_info *sns = &skd_chkstat_table[i];
2289 if (sns->mask & 0x10)
2290 if (skerr->type != sns->type)
2291 continue;
2293 if (sns->mask & 0x08)
2294 if (cmp_status != sns->stat)
2295 continue;
2297 if (sns->mask & 0x04)
2298 if (skerr->key != sns->key)
2299 continue;
2301 if (sns->mask & 0x02)
2302 if (skerr->code != sns->asc)
2303 continue;
2305 if (sns->mask & 0x01)
2306 if (skerr->qual != sns->ascq)
2307 continue;
2309 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
2310 pr_err("(%s): SMART Alert: sense key/asc/ascq "
2311 "%02x/%02x/%02x\n",
2312 skd_name(skdev), skerr->key,
2313 skerr->code, skerr->qual);
2315 return sns->action;
2318 /* No other match, so nonzero status means error,
2319 * zero status means good
2321 if (cmp_status) {
2322 pr_debug("%s:%s:%d status check: error\n",
2323 skdev->name, __func__, __LINE__);
2324 return SKD_CHECK_STATUS_REPORT_ERROR;
2327 pr_debug("%s:%s:%d status check good default\n",
2328 skdev->name, __func__, __LINE__);
2329 return SKD_CHECK_STATUS_REPORT_GOOD;
2332 static void skd_resolve_req_exception(struct skd_device *skdev,
2333 struct skd_request_context *skreq)
2335 u8 cmp_status = skreq->completion.status;
2337 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
2338 case SKD_CHECK_STATUS_REPORT_GOOD:
2339 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
2340 skd_end_request(skdev, skreq, 0);
2341 break;
2343 case SKD_CHECK_STATUS_BUSY_IMMINENT:
2344 skd_log_skreq(skdev, skreq, "retry(busy)");
2345 blk_requeue_request(skdev->queue, skreq->req);
2346 pr_info("(%s) drive BUSY imminent\n", skd_name(skdev));
2347 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
2348 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
2349 skd_quiesce_dev(skdev);
2350 break;
2352 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
2353 if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
2354 skd_log_skreq(skdev, skreq, "retry");
2355 blk_requeue_request(skdev->queue, skreq->req);
2356 break;
2358 /* fall through to report error */
2360 case SKD_CHECK_STATUS_REPORT_ERROR:
2361 default:
2362 skd_end_request(skdev, skreq, -EIO);
2363 break;
2367 /* assume spinlock is already held */
2368 static void skd_release_skreq(struct skd_device *skdev,
2369 struct skd_request_context *skreq)
2371 u32 msg_slot;
2372 struct skd_fitmsg_context *skmsg;
2374 u32 timo_slot;
2377 * Reclaim the FIT msg buffer if this is
2378 * the first of the requests it carried to
2379 * be completed. The FIT msg buffer used to
2380 * send this request cannot be reused until
2381 * we are sure the s1120 card has copied
2382 * it to its memory. The FIT msg might have
2383 * contained several requests. As soon as
2384 * any of them are completed we know that
2385 * the entire FIT msg was transferred.
2386 * Only the first completed request will
2387 * match the FIT msg buffer id. The FIT
2388 * msg buffer id is immediately updated.
2389 * When subsequent requests complete the FIT
2390 * msg buffer id won't match, so we know
2391 * quite cheaply that it is already done.
2393 msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
2394 SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);
2396 skmsg = &skdev->skmsg_table[msg_slot];
2397 if (skmsg->id == skreq->fitmsg_id) {
2398 SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
2399 SKD_ASSERT(skmsg->outstanding > 0);
2400 skmsg->outstanding--;
2401 if (skmsg->outstanding == 0) {
2402 skmsg->state = SKD_MSG_STATE_IDLE;
2403 skmsg->id += SKD_ID_INCR;
2404 skmsg->next = skdev->skmsg_free_list;
2405 skdev->skmsg_free_list = skmsg;
2410 * Decrease the number of active requests.
2411 * Also decrements the count in the timeout slot.
2413 SKD_ASSERT(skdev->in_flight > 0);
2414 skdev->in_flight -= 1;
2416 timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
2417 SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
2418 skdev->timeout_slot[timo_slot] -= 1;
2421 * Reset backpointer
2423 skreq->req = NULL;
2426 * Reclaim the skd_request_context
2428 skreq->state = SKD_REQ_STATE_IDLE;
2429 skreq->id += SKD_ID_INCR;
2430 skreq->next = skdev->skreq_free_list;
2431 skdev->skreq_free_list = skreq;
2434 #define DRIVER_INQ_EVPD_PAGE_CODE 0xDA
2436 static void skd_do_inq_page_00(struct skd_device *skdev,
2437 volatile struct fit_completion_entry_v1 *skcomp,
2438 volatile struct fit_comp_error_info *skerr,
2439 uint8_t *cdb, uint8_t *buf)
2441 uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;
2443 /* Caller requested "supported pages". The driver needs to insert
2444 * its page.
2446 pr_debug("%s:%s:%d skd_do_driver_inquiry: modify supported pages.\n",
2447 skdev->name, __func__, __LINE__);
2449 /* If the device rejected the request because the CDB was
2450 * improperly formed, then just leave.
2452 if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
2453 skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
2454 return;
2456 /* Get the amount of space the caller allocated */
2457 max_bytes = (cdb[3] << 8) | cdb[4];
2459 /* Get the number of pages actually returned by the device */
2460 drive_pages = (buf[2] << 8) | buf[3];
2461 drive_bytes = drive_pages + 4;
2462 new_size = drive_pages + 1;
2464 /* Supported pages must be in numerical order, so find where
2465 * the driver page needs to be inserted into the list of
2466 * pages returned by the device.
2468 for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
2469 if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
2470 return; /* Device using this page code. abort */
2471 else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
2472 break;
2475 if (insert_pt < max_bytes) {
2476 uint16_t u;
2478 /* Shift everything up one byte to make room. */
2479 for (u = new_size + 3; u > insert_pt; u--)
2480 buf[u] = buf[u - 1];
2481 buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;
2483 /* SCSI byte order increment of num_returned_bytes by 1 */
2484 skcomp->num_returned_bytes =
2485 be32_to_cpu(skcomp->num_returned_bytes) + 1;
2486 skcomp->num_returned_bytes =
2487 be32_to_cpu(skcomp->num_returned_bytes);
2490 /* update page length field to reflect the driver's page too */
2491 buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
2492 buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
2495 static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
2497 int pcie_reg;
2498 u16 pci_bus_speed;
2499 u8 pci_lanes;
2501 pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2502 if (pcie_reg) {
2503 u16 linksta;
2504 pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);
2506 pci_bus_speed = linksta & 0xF;
2507 pci_lanes = (linksta & 0x3F0) >> 4;
2508 } else {
2509 *speed = STEC_LINK_UNKNOWN;
2510 *width = 0xFF;
2511 return;
2514 switch (pci_bus_speed) {
2515 case 1:
2516 *speed = STEC_LINK_2_5GTS;
2517 break;
2518 case 2:
2519 *speed = STEC_LINK_5GTS;
2520 break;
2521 case 3:
2522 *speed = STEC_LINK_8GTS;
2523 break;
2524 default:
2525 *speed = STEC_LINK_UNKNOWN;
2526 break;
2529 if (pci_lanes <= 0x20)
2530 *width = pci_lanes;
2531 else
2532 *width = 0xFF;
2535 static void skd_do_inq_page_da(struct skd_device *skdev,
2536 volatile struct fit_completion_entry_v1 *skcomp,
2537 volatile struct fit_comp_error_info *skerr,
2538 uint8_t *cdb, uint8_t *buf)
2540 struct pci_dev *pdev = skdev->pdev;
2541 unsigned max_bytes;
2542 struct driver_inquiry_data inq;
2543 u16 val;
2545 pr_debug("%s:%s:%d skd_do_driver_inquiry: return driver page\n",
2546 skdev->name, __func__, __LINE__);
2548 memset(&inq, 0, sizeof(inq));
2550 inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;
2552 skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
2553 inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
2554 inq.pcie_device_number = PCI_SLOT(pdev->devfn);
2555 inq.pcie_function_number = PCI_FUNC(pdev->devfn);
2557 pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
2558 inq.pcie_vendor_id = cpu_to_be16(val);
2560 pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
2561 inq.pcie_device_id = cpu_to_be16(val);
2563 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
2564 inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
2566 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
2567 inq.pcie_subsystem_device_id = cpu_to_be16(val);
2569 /* Driver version, fixed lenth, padded with spaces on the right */
2570 inq.driver_version_length = sizeof(inq.driver_version);
2571 memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
2572 memcpy(inq.driver_version, DRV_VER_COMPL,
2573 min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));
2575 inq.page_length = cpu_to_be16((sizeof(inq) - 4));
2577 /* Clear the error set by the device */
2578 skcomp->status = SAM_STAT_GOOD;
2579 memset((void *)skerr, 0, sizeof(*skerr));
2581 /* copy response into output buffer */
2582 max_bytes = (cdb[3] << 8) | cdb[4];
2583 memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));
2585 skcomp->num_returned_bytes =
2586 be32_to_cpu(min_t(uint16_t, max_bytes, sizeof(inq)));
2589 static void skd_do_driver_inq(struct skd_device *skdev,
2590 volatile struct fit_completion_entry_v1 *skcomp,
2591 volatile struct fit_comp_error_info *skerr,
2592 uint8_t *cdb, uint8_t *buf)
2594 if (!buf)
2595 return;
2596 else if (cdb[0] != INQUIRY)
2597 return; /* Not an INQUIRY */
2598 else if ((cdb[1] & 1) == 0)
2599 return; /* EVPD not set */
2600 else if (cdb[2] == 0)
2601 /* Need to add driver's page to supported pages list */
2602 skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
2603 else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
2604 /* Caller requested driver's page */
2605 skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
2608 static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
2610 if (!sg)
2611 return NULL;
2612 if (!sg_page(sg))
2613 return NULL;
2614 return sg_virt(sg);
2617 static void skd_process_scsi_inq(struct skd_device *skdev,
2618 volatile struct fit_completion_entry_v1
2619 *skcomp,
2620 volatile struct fit_comp_error_info *skerr,
2621 struct skd_special_context *skspcl)
2623 uint8_t *buf;
2624 struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
2625 struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];
2627 dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
2628 skspcl->req.sg_data_dir);
2629 buf = skd_sg_1st_page_ptr(skspcl->req.sg);
2631 if (buf)
2632 skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
2636 static int skd_isr_completion_posted(struct skd_device *skdev,
2637 int limit, int *enqueued)
2639 volatile struct fit_completion_entry_v1 *skcmp = NULL;
2640 volatile struct fit_comp_error_info *skerr;
2641 u16 req_id;
2642 u32 req_slot;
2643 struct skd_request_context *skreq;
2644 u16 cmp_cntxt = 0;
2645 u8 cmp_status = 0;
2646 u8 cmp_cycle = 0;
2647 u32 cmp_bytes = 0;
2648 int rc = 0;
2649 int processed = 0;
2651 for (;; ) {
2652 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
2654 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
2655 cmp_cycle = skcmp->cycle;
2656 cmp_cntxt = skcmp->tag;
2657 cmp_status = skcmp->status;
2658 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
2660 skerr = &skdev->skerr_table[skdev->skcomp_ix];
2662 pr_debug("%s:%s:%d "
2663 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d "
2664 "busy=%d rbytes=0x%x proto=%d\n",
2665 skdev->name, __func__, __LINE__, skdev->skcomp_cycle,
2666 skdev->skcomp_ix, cmp_cycle, cmp_cntxt, cmp_status,
2667 skdev->in_flight, cmp_bytes, skdev->proto_ver);
2669 if (cmp_cycle != skdev->skcomp_cycle) {
2670 pr_debug("%s:%s:%d end of completions\n",
2671 skdev->name, __func__, __LINE__);
2672 break;
2675 * Update the completion queue head index and possibly
2676 * the completion cycle count. 8-bit wrap-around.
2678 skdev->skcomp_ix++;
2679 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
2680 skdev->skcomp_ix = 0;
2681 skdev->skcomp_cycle++;
2685 * The command context is a unique 32-bit ID. The low order
2686 * bits help locate the request. The request is usually a
2687 * r/w request (see skd_start() above) or a special request.
2689 req_id = cmp_cntxt;
2690 req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
2692 /* Is this other than a r/w request? */
2693 if (req_slot >= skdev->num_req_context) {
2695 * This is not a completion for a r/w request.
2697 skd_complete_other(skdev, skcmp, skerr);
2698 continue;
2701 skreq = &skdev->skreq_table[req_slot];
2704 * Make sure the request ID for the slot matches.
2706 if (skreq->id != req_id) {
2707 pr_debug("%s:%s:%d mismatch comp_id=0x%x req_id=0x%x\n",
2708 skdev->name, __func__, __LINE__,
2709 req_id, skreq->id);
2711 u16 new_id = cmp_cntxt;
2712 pr_err("(%s): Completion mismatch "
2713 "comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
2714 skd_name(skdev), req_id,
2715 skreq->id, new_id);
2717 continue;
2721 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
2723 if (skreq->state == SKD_REQ_STATE_ABORTED) {
2724 pr_debug("%s:%s:%d reclaim req %p id=%04x\n",
2725 skdev->name, __func__, __LINE__,
2726 skreq, skreq->id);
2727 /* a previously timed out command can
2728 * now be cleaned up */
2729 skd_release_skreq(skdev, skreq);
2730 continue;
2733 skreq->completion = *skcmp;
2734 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
2735 skreq->err_info = *skerr;
2736 skd_log_check_status(skdev, cmp_status, skerr->key,
2737 skerr->code, skerr->qual,
2738 skerr->fruc);
2740 /* Release DMA resources for the request. */
2741 if (skreq->n_sg > 0)
2742 skd_postop_sg_list(skdev, skreq);
2744 if (!skreq->req) {
2745 pr_debug("%s:%s:%d NULL backptr skdreq %p, "
2746 "req=0x%x req_id=0x%x\n",
2747 skdev->name, __func__, __LINE__,
2748 skreq, skreq->id, req_id);
2749 } else {
2751 * Capture the outcome and post it back to the
2752 * native request.
2754 if (likely(cmp_status == SAM_STAT_GOOD))
2755 skd_end_request(skdev, skreq, 0);
2756 else
2757 skd_resolve_req_exception(skdev, skreq);
2761 * Release the skreq, its FIT msg (if one), timeout slot,
2762 * and queue depth.
2764 skd_release_skreq(skdev, skreq);
2766 /* skd_isr_comp_limit equal zero means no limit */
2767 if (limit) {
2768 if (++processed >= limit) {
2769 rc = 1;
2770 break;
2775 if ((skdev->state == SKD_DRVR_STATE_PAUSING)
2776 && (skdev->in_flight) == 0) {
2777 skdev->state = SKD_DRVR_STATE_PAUSED;
2778 wake_up_interruptible(&skdev->waitq);
2781 return rc;
2784 static void skd_complete_other(struct skd_device *skdev,
2785 volatile struct fit_completion_entry_v1 *skcomp,
2786 volatile struct fit_comp_error_info *skerr)
2788 u32 req_id = 0;
2789 u32 req_table;
2790 u32 req_slot;
2791 struct skd_special_context *skspcl;
2793 req_id = skcomp->tag;
2794 req_table = req_id & SKD_ID_TABLE_MASK;
2795 req_slot = req_id & SKD_ID_SLOT_MASK;
2797 pr_debug("%s:%s:%d table=0x%x id=0x%x slot=%d\n",
2798 skdev->name, __func__, __LINE__,
2799 req_table, req_id, req_slot);
2802 * Based on the request id, determine how to dispatch this completion.
2803 * This swich/case is finding the good cases and forwarding the
2804 * completion entry. Errors are reported below the switch.
2806 switch (req_table) {
2807 case SKD_ID_RW_REQUEST:
2809 * The caller, skd_completion_posted_isr() above,
2810 * handles r/w requests. The only way we get here
2811 * is if the req_slot is out of bounds.
2813 break;
2815 case SKD_ID_SPECIAL_REQUEST:
2817 * Make sure the req_slot is in bounds and that the id
2818 * matches.
2820 if (req_slot < skdev->n_special) {
2821 skspcl = &skdev->skspcl_table[req_slot];
2822 if (skspcl->req.id == req_id &&
2823 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2824 skd_complete_special(skdev,
2825 skcomp, skerr, skspcl);
2826 return;
2829 break;
2831 case SKD_ID_INTERNAL:
2832 if (req_slot == 0) {
2833 skspcl = &skdev->internal_skspcl;
2834 if (skspcl->req.id == req_id &&
2835 skspcl->req.state == SKD_REQ_STATE_BUSY) {
2836 skd_complete_internal(skdev,
2837 skcomp, skerr, skspcl);
2838 return;
2841 break;
2843 case SKD_ID_FIT_MSG:
2845 * These id's should never appear in a completion record.
2847 break;
2849 default:
2851 * These id's should never appear anywhere;
2853 break;
2857 * If we get here it is a bad or stale id.
2861 static void skd_complete_special(struct skd_device *skdev,
2862 volatile struct fit_completion_entry_v1
2863 *skcomp,
2864 volatile struct fit_comp_error_info *skerr,
2865 struct skd_special_context *skspcl)
2867 pr_debug("%s:%s:%d completing special request %p\n",
2868 skdev->name, __func__, __LINE__, skspcl);
2869 if (skspcl->orphaned) {
2870 /* Discard orphaned request */
2871 /* ?: Can this release directly or does it need
2872 * to use a worker? */
2873 pr_debug("%s:%s:%d release orphaned %p\n",
2874 skdev->name, __func__, __LINE__, skspcl);
2875 skd_release_special(skdev, skspcl);
2876 return;
2879 skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);
2881 skspcl->req.state = SKD_REQ_STATE_COMPLETED;
2882 skspcl->req.completion = *skcomp;
2883 skspcl->req.err_info = *skerr;
2885 skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
2886 skerr->code, skerr->qual, skerr->fruc);
2888 wake_up_interruptible(&skdev->waitq);
2891 /* assume spinlock is already held */
2892 static void skd_release_special(struct skd_device *skdev,
2893 struct skd_special_context *skspcl)
2895 int i, was_depleted;
2897 for (i = 0; i < skspcl->req.n_sg; i++) {
2898 struct page *page = sg_page(&skspcl->req.sg[i]);
2899 __free_page(page);
2902 was_depleted = (skdev->skspcl_free_list == NULL);
2904 skspcl->req.state = SKD_REQ_STATE_IDLE;
2905 skspcl->req.id += SKD_ID_INCR;
2906 skspcl->req.next =
2907 (struct skd_request_context *)skdev->skspcl_free_list;
2908 skdev->skspcl_free_list = (struct skd_special_context *)skspcl;
2910 if (was_depleted) {
2911 pr_debug("%s:%s:%d skspcl was depleted\n",
2912 skdev->name, __func__, __LINE__);
2913 /* Free list was depleted. Their might be waiters. */
2914 wake_up_interruptible(&skdev->waitq);
2918 static void skd_reset_skcomp(struct skd_device *skdev)
2920 u32 nbytes;
2921 struct fit_completion_entry_v1 *skcomp;
2923 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
2924 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
2926 memset(skdev->skcomp_table, 0, nbytes);
2928 skdev->skcomp_ix = 0;
2929 skdev->skcomp_cycle = 1;
2933 *****************************************************************************
2934 * INTERRUPTS
2935 *****************************************************************************
2937 static void skd_completion_worker(struct work_struct *work)
2939 struct skd_device *skdev =
2940 container_of(work, struct skd_device, completion_worker);
2941 unsigned long flags;
2942 int flush_enqueued = 0;
2944 spin_lock_irqsave(&skdev->lock, flags);
2947 * pass in limit=0, which means no limit..
2948 * process everything in compq
2950 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
2951 skd_request_fn(skdev->queue);
2953 spin_unlock_irqrestore(&skdev->lock, flags);
2956 static void skd_isr_msg_from_dev(struct skd_device *skdev);
2958 irqreturn_t
2959 static skd_isr(int irq, void *ptr)
2961 struct skd_device *skdev;
2962 u32 intstat;
2963 u32 ack;
2964 int rc = 0;
2965 int deferred = 0;
2966 int flush_enqueued = 0;
2968 skdev = (struct skd_device *)ptr;
2969 spin_lock(&skdev->lock);
2971 for (;; ) {
2972 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2974 ack = FIT_INT_DEF_MASK;
2975 ack &= intstat;
2977 pr_debug("%s:%s:%d intstat=0x%x ack=0x%x\n",
2978 skdev->name, __func__, __LINE__, intstat, ack);
2980 /* As long as there is an int pending on device, keep
2981 * running loop. When none, get out, but if we've never
2982 * done any processing, call completion handler?
2984 if (ack == 0) {
2985 /* No interrupts on device, but run the completion
2986 * processor anyway?
2988 if (rc == 0)
2989 if (likely (skdev->state
2990 == SKD_DRVR_STATE_ONLINE))
2991 deferred = 1;
2992 break;
2995 rc = IRQ_HANDLED;
2997 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
2999 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
3000 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
3001 if (intstat & FIT_ISH_COMPLETION_POSTED) {
3003 * If we have already deferred completion
3004 * processing, don't bother running it again
3006 if (deferred == 0)
3007 deferred =
3008 skd_isr_completion_posted(skdev,
3009 skd_isr_comp_limit, &flush_enqueued);
3012 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
3013 skd_isr_fwstate(skdev);
3014 if (skdev->state == SKD_DRVR_STATE_FAULT ||
3015 skdev->state ==
3016 SKD_DRVR_STATE_DISAPPEARED) {
3017 spin_unlock(&skdev->lock);
3018 return rc;
3022 if (intstat & FIT_ISH_MSG_FROM_DEV)
3023 skd_isr_msg_from_dev(skdev);
3027 if (unlikely(flush_enqueued))
3028 skd_request_fn(skdev->queue);
3030 if (deferred)
3031 schedule_work(&skdev->completion_worker);
3032 else if (!flush_enqueued)
3033 skd_request_fn(skdev->queue);
3035 spin_unlock(&skdev->lock);
3037 return rc;
3040 static void skd_drive_fault(struct skd_device *skdev)
3042 skdev->state = SKD_DRVR_STATE_FAULT;
3043 pr_err("(%s): Drive FAULT\n", skd_name(skdev));
3046 static void skd_drive_disappeared(struct skd_device *skdev)
3048 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
3049 pr_err("(%s): Drive DISAPPEARED\n", skd_name(skdev));
3052 static void skd_isr_fwstate(struct skd_device *skdev)
3054 u32 sense;
3055 u32 state;
3056 u32 mtd;
3057 int prev_driver_state = skdev->state;
3059 sense = SKD_READL(skdev, FIT_STATUS);
3060 state = sense & FIT_SR_DRIVE_STATE_MASK;
3062 pr_err("(%s): s1120 state %s(%d)=>%s(%d)\n",
3063 skd_name(skdev),
3064 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3065 skd_drive_state_to_str(state), state);
3067 skdev->drive_state = state;
3069 switch (skdev->drive_state) {
3070 case FIT_SR_DRIVE_INIT:
3071 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
3072 skd_disable_interrupts(skdev);
3073 break;
3075 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
3076 skd_recover_requests(skdev, 0);
3077 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
3078 skdev->timer_countdown = SKD_STARTING_TIMO;
3079 skdev->state = SKD_DRVR_STATE_STARTING;
3080 skd_soft_reset(skdev);
3081 break;
3083 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
3084 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3085 skdev->last_mtd = mtd;
3086 break;
3088 case FIT_SR_DRIVE_ONLINE:
3089 skdev->cur_max_queue_depth = skd_max_queue_depth;
3090 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
3091 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
3093 skdev->queue_low_water_mark =
3094 skdev->cur_max_queue_depth * 2 / 3 + 1;
3095 if (skdev->queue_low_water_mark < 1)
3096 skdev->queue_low_water_mark = 1;
3097 pr_info(
3098 "(%s): Queue depth limit=%d dev=%d lowat=%d\n",
3099 skd_name(skdev),
3100 skdev->cur_max_queue_depth,
3101 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3103 skd_refresh_device_data(skdev);
3104 break;
3106 case FIT_SR_DRIVE_BUSY:
3107 skdev->state = SKD_DRVR_STATE_BUSY;
3108 skdev->timer_countdown = SKD_BUSY_TIMO;
3109 skd_quiesce_dev(skdev);
3110 break;
3111 case FIT_SR_DRIVE_BUSY_SANITIZE:
3112 /* set timer for 3 seconds, we'll abort any unfinished
3113 * commands after that expires
3115 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3116 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
3117 blk_start_queue(skdev->queue);
3118 break;
3119 case FIT_SR_DRIVE_BUSY_ERASE:
3120 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3121 skdev->timer_countdown = SKD_BUSY_TIMO;
3122 break;
3123 case FIT_SR_DRIVE_OFFLINE:
3124 skdev->state = SKD_DRVR_STATE_IDLE;
3125 break;
3126 case FIT_SR_DRIVE_SOFT_RESET:
3127 switch (skdev->state) {
3128 case SKD_DRVR_STATE_STARTING:
3129 case SKD_DRVR_STATE_RESTARTING:
3130 /* Expected by a caller of skd_soft_reset() */
3131 break;
3132 default:
3133 skdev->state = SKD_DRVR_STATE_RESTARTING;
3134 break;
3136 break;
3137 case FIT_SR_DRIVE_FW_BOOTING:
3138 pr_debug("%s:%s:%d ISR FIT_SR_DRIVE_FW_BOOTING %s\n",
3139 skdev->name, __func__, __LINE__, skdev->name);
3140 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3141 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3142 break;
3144 case FIT_SR_DRIVE_DEGRADED:
3145 case FIT_SR_PCIE_LINK_DOWN:
3146 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3147 break;
3149 case FIT_SR_DRIVE_FAULT:
3150 skd_drive_fault(skdev);
3151 skd_recover_requests(skdev, 0);
3152 blk_start_queue(skdev->queue);
3153 break;
3155 /* PCIe bus returned all Fs? */
3156 case 0xFF:
3157 pr_info("(%s): state=0x%x sense=0x%x\n",
3158 skd_name(skdev), state, sense);
3159 skd_drive_disappeared(skdev);
3160 skd_recover_requests(skdev, 0);
3161 blk_start_queue(skdev->queue);
3162 break;
3163 default:
3165 * Uknown FW State. Wait for a state we recognize.
3167 break;
3169 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3170 skd_name(skdev),
3171 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
3172 skd_skdev_state_to_str(skdev->state), skdev->state);
3175 static void skd_recover_requests(struct skd_device *skdev, int requeue)
3177 int i;
3179 for (i = 0; i < skdev->num_req_context; i++) {
3180 struct skd_request_context *skreq = &skdev->skreq_table[i];
3182 if (skreq->state == SKD_REQ_STATE_BUSY) {
3183 skd_log_skreq(skdev, skreq, "recover");
3185 SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
3186 SKD_ASSERT(skreq->req != NULL);
3188 /* Release DMA resources for the request. */
3189 if (skreq->n_sg > 0)
3190 skd_postop_sg_list(skdev, skreq);
3192 if (requeue &&
3193 (unsigned long) ++skreq->req->special <
3194 SKD_MAX_RETRIES)
3195 blk_requeue_request(skdev->queue, skreq->req);
3196 else
3197 skd_end_request(skdev, skreq, -EIO);
3199 skreq->req = NULL;
3201 skreq->state = SKD_REQ_STATE_IDLE;
3202 skreq->id += SKD_ID_INCR;
3204 if (i > 0)
3205 skreq[-1].next = skreq;
3206 skreq->next = NULL;
3208 skdev->skreq_free_list = skdev->skreq_table;
3210 for (i = 0; i < skdev->num_fitmsg_context; i++) {
3211 struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];
3213 if (skmsg->state == SKD_MSG_STATE_BUSY) {
3214 skd_log_skmsg(skdev, skmsg, "salvaged");
3215 SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
3216 skmsg->state = SKD_MSG_STATE_IDLE;
3217 skmsg->id += SKD_ID_INCR;
3219 if (i > 0)
3220 skmsg[-1].next = skmsg;
3221 skmsg->next = NULL;
3223 skdev->skmsg_free_list = skdev->skmsg_table;
3225 for (i = 0; i < skdev->n_special; i++) {
3226 struct skd_special_context *skspcl = &skdev->skspcl_table[i];
3228 /* If orphaned, reclaim it because it has already been reported
3229 * to the process as an error (it was just waiting for
3230 * a completion that didn't come, and now it will never come)
3231 * If busy, change to a state that will cause it to error
3232 * out in the wait routine and let it do the normal
3233 * reporting and reclaiming
3235 if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
3236 if (skspcl->orphaned) {
3237 pr_debug("%s:%s:%d orphaned %p\n",
3238 skdev->name, __func__, __LINE__,
3239 skspcl);
3240 skd_release_special(skdev, skspcl);
3241 } else {
3242 pr_debug("%s:%s:%d not orphaned %p\n",
3243 skdev->name, __func__, __LINE__,
3244 skspcl);
3245 skspcl->req.state = SKD_REQ_STATE_ABORTED;
3249 skdev->skspcl_free_list = skdev->skspcl_table;
3251 for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
3252 skdev->timeout_slot[i] = 0;
3254 skdev->in_flight = 0;
3257 static void skd_isr_msg_from_dev(struct skd_device *skdev)
3259 u32 mfd;
3260 u32 mtd;
3261 u32 data;
3263 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3265 pr_debug("%s:%s:%d mfd=0x%x last_mtd=0x%x\n",
3266 skdev->name, __func__, __LINE__, mfd, skdev->last_mtd);
3268 /* ignore any mtd that is an ack for something we didn't send */
3269 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
3270 return;
3272 switch (FIT_MXD_TYPE(mfd)) {
3273 case FIT_MTD_FITFW_INIT:
3274 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
3276 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
3277 pr_err("(%s): protocol mismatch\n",
3278 skdev->name);
3279 pr_err("(%s): got=%d support=%d\n",
3280 skdev->name, skdev->proto_ver,
3281 FIT_PROTOCOL_VERSION_1);
3282 pr_err("(%s): please upgrade driver\n",
3283 skdev->name);
3284 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
3285 skd_soft_reset(skdev);
3286 break;
3288 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
3289 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3290 skdev->last_mtd = mtd;
3291 break;
3293 case FIT_MTD_GET_CMDQ_DEPTH:
3294 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
3295 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
3296 SKD_N_COMPLETION_ENTRY);
3297 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3298 skdev->last_mtd = mtd;
3299 break;
3301 case FIT_MTD_SET_COMPQ_DEPTH:
3302 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
3303 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
3304 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3305 skdev->last_mtd = mtd;
3306 break;
3308 case FIT_MTD_SET_COMPQ_ADDR:
3309 skd_reset_skcomp(skdev);
3310 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
3311 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3312 skdev->last_mtd = mtd;
3313 break;
3315 case FIT_MTD_CMD_LOG_HOST_ID:
3316 skdev->connect_time_stamp = get_seconds();
3317 data = skdev->connect_time_stamp & 0xFFFF;
3318 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
3319 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3320 skdev->last_mtd = mtd;
3321 break;
3323 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
3324 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
3325 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
3326 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
3327 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3328 skdev->last_mtd = mtd;
3329 break;
3331 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
3332 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
3333 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
3334 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
3335 skdev->last_mtd = mtd;
3337 pr_err("(%s): Time sync driver=0x%x device=0x%x\n",
3338 skd_name(skdev),
3339 skdev->connect_time_stamp, skdev->drive_jiffies);
3340 break;
3342 case FIT_MTD_ARM_QUEUE:
3343 skdev->last_mtd = 0;
3345 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
3347 break;
3349 default:
3350 break;
3354 static void skd_disable_interrupts(struct skd_device *skdev)
3356 u32 sense;
3358 sense = SKD_READL(skdev, FIT_CONTROL);
3359 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
3360 SKD_WRITEL(skdev, sense, FIT_CONTROL);
3361 pr_debug("%s:%s:%d sense 0x%x\n",
3362 skdev->name, __func__, __LINE__, sense);
3364 /* Note that the 1s is written. A 1-bit means
3365 * disable, a 0 means enable.
3367 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
3370 static void skd_enable_interrupts(struct skd_device *skdev)
3372 u32 val;
3374 /* unmask interrupts first */
3375 val = FIT_ISH_FW_STATE_CHANGE +
3376 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
3378 /* Note that the compliment of mask is written. A 1-bit means
3379 * disable, a 0 means enable. */
3380 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
3381 pr_debug("%s:%s:%d interrupt mask=0x%x\n",
3382 skdev->name, __func__, __LINE__, ~val);
3384 val = SKD_READL(skdev, FIT_CONTROL);
3385 val |= FIT_CR_ENABLE_INTERRUPTS;
3386 pr_debug("%s:%s:%d control=0x%x\n",
3387 skdev->name, __func__, __LINE__, val);
3388 SKD_WRITEL(skdev, val, FIT_CONTROL);
3392 *****************************************************************************
3393 * START, STOP, RESTART, QUIESCE, UNQUIESCE
3394 *****************************************************************************
3397 static void skd_soft_reset(struct skd_device *skdev)
3399 u32 val;
3401 val = SKD_READL(skdev, FIT_CONTROL);
3402 val |= (FIT_CR_SOFT_RESET);
3403 pr_debug("%s:%s:%d control=0x%x\n",
3404 skdev->name, __func__, __LINE__, val);
3405 SKD_WRITEL(skdev, val, FIT_CONTROL);
3408 static void skd_start_device(struct skd_device *skdev)
3410 unsigned long flags;
3411 u32 sense;
3412 u32 state;
3414 spin_lock_irqsave(&skdev->lock, flags);
3416 /* ack all ghost interrupts */
3417 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3419 sense = SKD_READL(skdev, FIT_STATUS);
3421 pr_debug("%s:%s:%d initial status=0x%x\n",
3422 skdev->name, __func__, __LINE__, sense);
3424 state = sense & FIT_SR_DRIVE_STATE_MASK;
3425 skdev->drive_state = state;
3426 skdev->last_mtd = 0;
3428 skdev->state = SKD_DRVR_STATE_STARTING;
3429 skdev->timer_countdown = SKD_STARTING_TIMO;
3431 skd_enable_interrupts(skdev);
3433 switch (skdev->drive_state) {
3434 case FIT_SR_DRIVE_OFFLINE:
3435 pr_err("(%s): Drive offline...\n", skd_name(skdev));
3436 break;
3438 case FIT_SR_DRIVE_FW_BOOTING:
3439 pr_debug("%s:%s:%d FIT_SR_DRIVE_FW_BOOTING %s\n",
3440 skdev->name, __func__, __LINE__, skdev->name);
3441 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
3442 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
3443 break;
3445 case FIT_SR_DRIVE_BUSY_SANITIZE:
3446 pr_info("(%s): Start: BUSY_SANITIZE\n",
3447 skd_name(skdev));
3448 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
3449 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3450 break;
3452 case FIT_SR_DRIVE_BUSY_ERASE:
3453 pr_info("(%s): Start: BUSY_ERASE\n", skd_name(skdev));
3454 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
3455 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3456 break;
3458 case FIT_SR_DRIVE_INIT:
3459 case FIT_SR_DRIVE_ONLINE:
3460 skd_soft_reset(skdev);
3461 break;
3463 case FIT_SR_DRIVE_BUSY:
3464 pr_err("(%s): Drive Busy...\n", skd_name(skdev));
3465 skdev->state = SKD_DRVR_STATE_BUSY;
3466 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
3467 break;
3469 case FIT_SR_DRIVE_SOFT_RESET:
3470 pr_err("(%s) drive soft reset in prog\n",
3471 skd_name(skdev));
3472 break;
3474 case FIT_SR_DRIVE_FAULT:
3475 /* Fault state is bad...soft reset won't do it...
3476 * Hard reset, maybe, but does it work on device?
3477 * For now, just fault so the system doesn't hang.
3479 skd_drive_fault(skdev);
3480 /*start the queue so we can respond with error to requests */
3481 pr_debug("%s:%s:%d starting %s queue\n",
3482 skdev->name, __func__, __LINE__, skdev->name);
3483 blk_start_queue(skdev->queue);
3484 skdev->gendisk_on = -1;
3485 wake_up_interruptible(&skdev->waitq);
3486 break;
3488 case 0xFF:
3489 /* Most likely the device isn't there or isn't responding
3490 * to the BAR1 addresses. */
3491 skd_drive_disappeared(skdev);
3492 /*start the queue so we can respond with error to requests */
3493 pr_debug("%s:%s:%d starting %s queue to error-out reqs\n",
3494 skdev->name, __func__, __LINE__, skdev->name);
3495 blk_start_queue(skdev->queue);
3496 skdev->gendisk_on = -1;
3497 wake_up_interruptible(&skdev->waitq);
3498 break;
3500 default:
3501 pr_err("(%s) Start: unknown state %x\n",
3502 skd_name(skdev), skdev->drive_state);
3503 break;
3506 state = SKD_READL(skdev, FIT_CONTROL);
3507 pr_debug("%s:%s:%d FIT Control Status=0x%x\n",
3508 skdev->name, __func__, __LINE__, state);
3510 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
3511 pr_debug("%s:%s:%d Intr Status=0x%x\n",
3512 skdev->name, __func__, __LINE__, state);
3514 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
3515 pr_debug("%s:%s:%d Intr Mask=0x%x\n",
3516 skdev->name, __func__, __LINE__, state);
3518 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3519 pr_debug("%s:%s:%d Msg from Dev=0x%x\n",
3520 skdev->name, __func__, __LINE__, state);
3522 state = SKD_READL(skdev, FIT_HW_VERSION);
3523 pr_debug("%s:%s:%d HW version=0x%x\n",
3524 skdev->name, __func__, __LINE__, state);
3526 spin_unlock_irqrestore(&skdev->lock, flags);
3529 static void skd_stop_device(struct skd_device *skdev)
3531 unsigned long flags;
3532 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3533 u32 dev_state;
3534 int i;
3536 spin_lock_irqsave(&skdev->lock, flags);
3538 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
3539 pr_err("(%s): skd_stop_device not online no sync\n",
3540 skd_name(skdev));
3541 goto stop_out;
3544 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
3545 pr_err("(%s): skd_stop_device no special\n",
3546 skd_name(skdev));
3547 goto stop_out;
3550 skdev->state = SKD_DRVR_STATE_SYNCING;
3551 skdev->sync_done = 0;
3553 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
3555 spin_unlock_irqrestore(&skdev->lock, flags);
3557 wait_event_interruptible_timeout(skdev->waitq,
3558 (skdev->sync_done), (10 * HZ));
3560 spin_lock_irqsave(&skdev->lock, flags);
3562 switch (skdev->sync_done) {
3563 case 0:
3564 pr_err("(%s): skd_stop_device no sync\n",
3565 skd_name(skdev));
3566 break;
3567 case 1:
3568 pr_err("(%s): skd_stop_device sync done\n",
3569 skd_name(skdev));
3570 break;
3571 default:
3572 pr_err("(%s): skd_stop_device sync error\n",
3573 skd_name(skdev));
3576 stop_out:
3577 skdev->state = SKD_DRVR_STATE_STOPPING;
3578 spin_unlock_irqrestore(&skdev->lock, flags);
3580 skd_kill_timer(skdev);
3582 spin_lock_irqsave(&skdev->lock, flags);
3583 skd_disable_interrupts(skdev);
3585 /* ensure all ints on device are cleared */
3586 /* soft reset the device to unload with a clean slate */
3587 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3588 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
3590 spin_unlock_irqrestore(&skdev->lock, flags);
3592 /* poll every 100ms, 1 second timeout */
3593 for (i = 0; i < 10; i++) {
3594 dev_state =
3595 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
3596 if (dev_state == FIT_SR_DRIVE_INIT)
3597 break;
3598 set_current_state(TASK_INTERRUPTIBLE);
3599 schedule_timeout(msecs_to_jiffies(100));
3602 if (dev_state != FIT_SR_DRIVE_INIT)
3603 pr_err("(%s): skd_stop_device state error 0x%02x\n",
3604 skd_name(skdev), dev_state);
3607 /* assume spinlock is held */
3608 static void skd_restart_device(struct skd_device *skdev)
3610 u32 state;
3612 /* ack all ghost interrupts */
3613 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
3615 state = SKD_READL(skdev, FIT_STATUS);
3617 pr_debug("%s:%s:%d drive status=0x%x\n",
3618 skdev->name, __func__, __LINE__, state);
3620 state &= FIT_SR_DRIVE_STATE_MASK;
3621 skdev->drive_state = state;
3622 skdev->last_mtd = 0;
3624 skdev->state = SKD_DRVR_STATE_RESTARTING;
3625 skdev->timer_countdown = SKD_RESTARTING_TIMO;
3627 skd_soft_reset(skdev);
3630 /* assume spinlock is held */
3631 static int skd_quiesce_dev(struct skd_device *skdev)
3633 int rc = 0;
3635 switch (skdev->state) {
3636 case SKD_DRVR_STATE_BUSY:
3637 case SKD_DRVR_STATE_BUSY_IMMINENT:
3638 pr_debug("%s:%s:%d stopping %s queue\n",
3639 skdev->name, __func__, __LINE__, skdev->name);
3640 blk_stop_queue(skdev->queue);
3641 break;
3642 case SKD_DRVR_STATE_ONLINE:
3643 case SKD_DRVR_STATE_STOPPING:
3644 case SKD_DRVR_STATE_SYNCING:
3645 case SKD_DRVR_STATE_PAUSING:
3646 case SKD_DRVR_STATE_PAUSED:
3647 case SKD_DRVR_STATE_STARTING:
3648 case SKD_DRVR_STATE_RESTARTING:
3649 case SKD_DRVR_STATE_RESUMING:
3650 default:
3651 rc = -EINVAL;
3652 pr_debug("%s:%s:%d state [%d] not implemented\n",
3653 skdev->name, __func__, __LINE__, skdev->state);
3655 return rc;
3658 /* assume spinlock is held */
3659 static int skd_unquiesce_dev(struct skd_device *skdev)
3661 int prev_driver_state = skdev->state;
3663 skd_log_skdev(skdev, "unquiesce");
3664 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
3665 pr_debug("%s:%s:%d **** device already ONLINE\n",
3666 skdev->name, __func__, __LINE__);
3667 return 0;
3669 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
3671 * If there has been an state change to other than
3672 * ONLINE, we will rely on controller state change
3673 * to come back online and restart the queue.
3674 * The BUSY state means that driver is ready to
3675 * continue normal processing but waiting for controller
3676 * to become available.
3678 skdev->state = SKD_DRVR_STATE_BUSY;
3679 pr_debug("%s:%s:%d drive BUSY state\n",
3680 skdev->name, __func__, __LINE__);
3681 return 0;
3685 * Drive has just come online, driver is either in startup,
3686 * paused performing a task, or bust waiting for hardware.
3688 switch (skdev->state) {
3689 case SKD_DRVR_STATE_PAUSED:
3690 case SKD_DRVR_STATE_BUSY:
3691 case SKD_DRVR_STATE_BUSY_IMMINENT:
3692 case SKD_DRVR_STATE_BUSY_ERASE:
3693 case SKD_DRVR_STATE_STARTING:
3694 case SKD_DRVR_STATE_RESTARTING:
3695 case SKD_DRVR_STATE_FAULT:
3696 case SKD_DRVR_STATE_IDLE:
3697 case SKD_DRVR_STATE_LOAD:
3698 skdev->state = SKD_DRVR_STATE_ONLINE;
3699 pr_err("(%s): Driver state %s(%d)=>%s(%d)\n",
3700 skd_name(skdev),
3701 skd_skdev_state_to_str(prev_driver_state),
3702 prev_driver_state, skd_skdev_state_to_str(skdev->state),
3703 skdev->state);
3704 pr_debug("%s:%s:%d **** device ONLINE...starting block queue\n",
3705 skdev->name, __func__, __LINE__);
3706 pr_debug("%s:%s:%d starting %s queue\n",
3707 skdev->name, __func__, __LINE__, skdev->name);
3708 pr_info("(%s): STEC s1120 ONLINE\n", skd_name(skdev));
3709 blk_start_queue(skdev->queue);
3710 skdev->gendisk_on = 1;
3711 wake_up_interruptible(&skdev->waitq);
3712 break;
3714 case SKD_DRVR_STATE_DISAPPEARED:
3715 default:
3716 pr_debug("%s:%s:%d **** driver state %d, not implemented \n",
3717 skdev->name, __func__, __LINE__,
3718 skdev->state);
3719 return -EBUSY;
3721 return 0;
3725 *****************************************************************************
3726 * PCIe MSI/MSI-X INTERRUPT HANDLERS
3727 *****************************************************************************
3730 static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
3732 struct skd_device *skdev = skd_host_data;
3733 unsigned long flags;
3735 spin_lock_irqsave(&skdev->lock, flags);
3736 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3737 skdev->name, __func__, __LINE__,
3738 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3739 pr_err("(%s): MSIX reserved irq %d = 0x%x\n", skd_name(skdev),
3740 irq, SKD_READL(skdev, FIT_INT_STATUS_HOST));
3741 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
3742 spin_unlock_irqrestore(&skdev->lock, flags);
3743 return IRQ_HANDLED;
3746 static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
3748 struct skd_device *skdev = skd_host_data;
3749 unsigned long flags;
3751 spin_lock_irqsave(&skdev->lock, flags);
3752 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3753 skdev->name, __func__, __LINE__,
3754 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3755 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
3756 skd_isr_fwstate(skdev);
3757 spin_unlock_irqrestore(&skdev->lock, flags);
3758 return IRQ_HANDLED;
3761 static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
3763 struct skd_device *skdev = skd_host_data;
3764 unsigned long flags;
3765 int flush_enqueued = 0;
3766 int deferred;
3768 spin_lock_irqsave(&skdev->lock, flags);
3769 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3770 skdev->name, __func__, __LINE__,
3771 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3772 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
3773 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
3774 &flush_enqueued);
3775 if (flush_enqueued)
3776 skd_request_fn(skdev->queue);
3778 if (deferred)
3779 schedule_work(&skdev->completion_worker);
3780 else if (!flush_enqueued)
3781 skd_request_fn(skdev->queue);
3783 spin_unlock_irqrestore(&skdev->lock, flags);
3785 return IRQ_HANDLED;
3788 static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
3790 struct skd_device *skdev = skd_host_data;
3791 unsigned long flags;
3793 spin_lock_irqsave(&skdev->lock, flags);
3794 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3795 skdev->name, __func__, __LINE__,
3796 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3797 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
3798 skd_isr_msg_from_dev(skdev);
3799 spin_unlock_irqrestore(&skdev->lock, flags);
3800 return IRQ_HANDLED;
3803 static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
3805 struct skd_device *skdev = skd_host_data;
3806 unsigned long flags;
3808 spin_lock_irqsave(&skdev->lock, flags);
3809 pr_debug("%s:%s:%d MSIX = 0x%x\n",
3810 skdev->name, __func__, __LINE__,
3811 SKD_READL(skdev, FIT_INT_STATUS_HOST));
3812 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
3813 spin_unlock_irqrestore(&skdev->lock, flags);
3814 return IRQ_HANDLED;
3818 *****************************************************************************
3819 * PCIe MSI/MSI-X SETUP
3820 *****************************************************************************
3823 struct skd_msix_entry {
3824 int have_irq;
3825 u32 vector;
3826 u32 entry;
3827 struct skd_device *rsp;
3828 char isr_name[30];
3831 struct skd_init_msix_entry {
3832 const char *name;
3833 irq_handler_t handler;
3836 #define SKD_MAX_MSIX_COUNT 13
3837 #define SKD_MIN_MSIX_COUNT 7
3838 #define SKD_BASE_MSIX_IRQ 4
3840 static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
3841 { "(DMA 0)", skd_reserved_isr },
3842 { "(DMA 1)", skd_reserved_isr },
3843 { "(DMA 2)", skd_reserved_isr },
3844 { "(DMA 3)", skd_reserved_isr },
3845 { "(State Change)", skd_statec_isr },
3846 { "(COMPL_Q)", skd_comp_q },
3847 { "(MSG)", skd_msg_isr },
3848 { "(Reserved)", skd_reserved_isr },
3849 { "(Reserved)", skd_reserved_isr },
3850 { "(Queue Full 0)", skd_qfull_isr },
3851 { "(Queue Full 1)", skd_qfull_isr },
3852 { "(Queue Full 2)", skd_qfull_isr },
3853 { "(Queue Full 3)", skd_qfull_isr },
3856 static void skd_release_msix(struct skd_device *skdev)
3858 struct skd_msix_entry *qentry;
3859 int i;
3861 if (skdev->msix_entries) {
3862 for (i = 0; i < skdev->msix_count; i++) {
3863 qentry = &skdev->msix_entries[i];
3864 skdev = qentry->rsp;
3866 if (qentry->have_irq)
3867 devm_free_irq(&skdev->pdev->dev,
3868 qentry->vector, qentry->rsp);
3871 kfree(skdev->msix_entries);
3874 if (skdev->msix_count)
3875 pci_disable_msix(skdev->pdev);
3877 skdev->msix_count = 0;
3878 skdev->msix_entries = NULL;
3881 static int skd_acquire_msix(struct skd_device *skdev)
3883 int i, rc;
3884 struct pci_dev *pdev = skdev->pdev;
3885 struct msix_entry *entries;
3886 struct skd_msix_entry *qentry;
3888 entries = kzalloc(sizeof(struct msix_entry) * SKD_MAX_MSIX_COUNT,
3889 GFP_KERNEL);
3890 if (!entries)
3891 return -ENOMEM;
3893 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++)
3894 entries[i].entry = i;
3896 rc = pci_enable_msix_exact(pdev, entries, SKD_MAX_MSIX_COUNT);
3897 if (rc) {
3898 pr_err("(%s): failed to enable MSI-X %d\n",
3899 skd_name(skdev), rc);
3900 goto msix_out;
3903 skdev->msix_count = SKD_MAX_MSIX_COUNT;
3904 skdev->msix_entries = kzalloc(sizeof(struct skd_msix_entry) *
3905 skdev->msix_count, GFP_KERNEL);
3906 if (!skdev->msix_entries) {
3907 rc = -ENOMEM;
3908 pr_err("(%s): msix table allocation error\n",
3909 skd_name(skdev));
3910 goto msix_out;
3913 for (i = 0; i < skdev->msix_count; i++) {
3914 qentry = &skdev->msix_entries[i];
3915 qentry->vector = entries[i].vector;
3916 qentry->entry = entries[i].entry;
3917 qentry->rsp = NULL;
3918 qentry->have_irq = 0;
3919 pr_debug("%s:%s:%d %s: <%s> msix (%d) vec %d, entry %x\n",
3920 skdev->name, __func__, __LINE__,
3921 pci_name(pdev), skdev->name,
3922 i, qentry->vector, qentry->entry);
3925 /* Enable MSI-X vectors for the base queue */
3926 for (i = 0; i < skdev->msix_count; i++) {
3927 qentry = &skdev->msix_entries[i];
3928 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
3929 "%s%d-msix %s", DRV_NAME, skdev->devno,
3930 msix_entries[i].name);
3931 rc = devm_request_irq(&skdev->pdev->dev, qentry->vector,
3932 msix_entries[i].handler, 0,
3933 qentry->isr_name, skdev);
3934 if (rc) {
3935 pr_err("(%s): Unable to register(%d) MSI-X "
3936 "handler %d: %s\n",
3937 skd_name(skdev), rc, i, qentry->isr_name);
3938 goto msix_out;
3939 } else {
3940 qentry->have_irq = 1;
3941 qentry->rsp = skdev;
3944 pr_debug("%s:%s:%d %s: <%s> msix %d irq(s) enabled\n",
3945 skdev->name, __func__, __LINE__,
3946 pci_name(pdev), skdev->name, skdev->msix_count);
3947 return 0;
3949 msix_out:
3950 if (entries)
3951 kfree(entries);
3952 skd_release_msix(skdev);
3953 return rc;
3956 static int skd_acquire_irq(struct skd_device *skdev)
3958 int rc;
3959 struct pci_dev *pdev;
3961 pdev = skdev->pdev;
3962 skdev->msix_count = 0;
3964 RETRY_IRQ_TYPE:
3965 switch (skdev->irq_type) {
3966 case SKD_IRQ_MSIX:
3967 rc = skd_acquire_msix(skdev);
3968 if (!rc)
3969 pr_info("(%s): MSI-X %d irqs enabled\n",
3970 skd_name(skdev), skdev->msix_count);
3971 else {
3972 pr_err(
3973 "(%s): failed to enable MSI-X, re-trying with MSI %d\n",
3974 skd_name(skdev), rc);
3975 skdev->irq_type = SKD_IRQ_MSI;
3976 goto RETRY_IRQ_TYPE;
3978 break;
3979 case SKD_IRQ_MSI:
3980 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d-msi",
3981 DRV_NAME, skdev->devno);
3982 rc = pci_enable_msi_range(pdev, 1, 1);
3983 if (rc > 0) {
3984 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr, 0,
3985 skdev->isr_name, skdev);
3986 if (rc) {
3987 pci_disable_msi(pdev);
3988 pr_err(
3989 "(%s): failed to allocate the MSI interrupt %d\n",
3990 skd_name(skdev), rc);
3991 goto RETRY_IRQ_LEGACY;
3993 pr_info("(%s): MSI irq %d enabled\n",
3994 skd_name(skdev), pdev->irq);
3995 } else {
3996 RETRY_IRQ_LEGACY:
3997 pr_err(
3998 "(%s): failed to enable MSI, re-trying with LEGACY %d\n",
3999 skd_name(skdev), rc);
4000 skdev->irq_type = SKD_IRQ_LEGACY;
4001 goto RETRY_IRQ_TYPE;
4003 break;
4004 case SKD_IRQ_LEGACY:
4005 snprintf(skdev->isr_name, sizeof(skdev->isr_name),
4006 "%s%d-legacy", DRV_NAME, skdev->devno);
4007 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
4008 IRQF_SHARED, skdev->isr_name, skdev);
4009 if (!rc)
4010 pr_info("(%s): LEGACY irq %d enabled\n",
4011 skd_name(skdev), pdev->irq);
4012 else
4013 pr_err("(%s): request LEGACY irq error %d\n",
4014 skd_name(skdev), rc);
4015 break;
4016 default:
4017 pr_info("(%s): irq_type %d invalid, re-set to %d\n",
4018 skd_name(skdev), skdev->irq_type, SKD_IRQ_DEFAULT);
4019 skdev->irq_type = SKD_IRQ_LEGACY;
4020 goto RETRY_IRQ_TYPE;
4022 return rc;
4025 static void skd_release_irq(struct skd_device *skdev)
4027 switch (skdev->irq_type) {
4028 case SKD_IRQ_MSIX:
4029 skd_release_msix(skdev);
4030 break;
4031 case SKD_IRQ_MSI:
4032 devm_free_irq(&skdev->pdev->dev, skdev->pdev->irq, skdev);
4033 pci_disable_msi(skdev->pdev);
4034 break;
4035 case SKD_IRQ_LEGACY:
4036 devm_free_irq(&skdev->pdev->dev, skdev->pdev->irq, skdev);
4037 break;
4038 default:
4039 pr_err("(%s): wrong irq type %d!",
4040 skd_name(skdev), skdev->irq_type);
4041 break;
4046 *****************************************************************************
4047 * CONSTRUCT
4048 *****************************************************************************
4051 static int skd_cons_skcomp(struct skd_device *skdev)
4053 int rc = 0;
4054 struct fit_completion_entry_v1 *skcomp;
4055 u32 nbytes;
4057 nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
4058 nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;
4060 pr_debug("%s:%s:%d comp pci_alloc, total bytes %d entries %d\n",
4061 skdev->name, __func__, __LINE__,
4062 nbytes, SKD_N_COMPLETION_ENTRY);
4064 skcomp = pci_zalloc_consistent(skdev->pdev, nbytes,
4065 &skdev->cq_dma_address);
4067 if (skcomp == NULL) {
4068 rc = -ENOMEM;
4069 goto err_out;
4072 skdev->skcomp_table = skcomp;
4073 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
4074 sizeof(*skcomp) *
4075 SKD_N_COMPLETION_ENTRY);
4077 err_out:
4078 return rc;
4081 static int skd_cons_skmsg(struct skd_device *skdev)
4083 int rc = 0;
4084 u32 i;
4086 pr_debug("%s:%s:%d skmsg_table kzalloc, struct %lu, count %u total %lu\n",
4087 skdev->name, __func__, __LINE__,
4088 sizeof(struct skd_fitmsg_context),
4089 skdev->num_fitmsg_context,
4090 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
4092 skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
4093 *skdev->num_fitmsg_context, GFP_KERNEL);
4094 if (skdev->skmsg_table == NULL) {
4095 rc = -ENOMEM;
4096 goto err_out;
4099 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4100 struct skd_fitmsg_context *skmsg;
4102 skmsg = &skdev->skmsg_table[i];
4104 skmsg->id = i + SKD_ID_FIT_MSG;
4106 skmsg->state = SKD_MSG_STATE_IDLE;
4107 skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
4108 SKD_N_FITMSG_BYTES + 64,
4109 &skmsg->mb_dma_address);
4111 if (skmsg->msg_buf == NULL) {
4112 rc = -ENOMEM;
4113 goto err_out;
4116 skmsg->offset = (u32)((u64)skmsg->msg_buf &
4117 (~FIT_QCMD_BASE_ADDRESS_MASK));
4118 skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK;
4119 skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf &
4120 FIT_QCMD_BASE_ADDRESS_MASK);
4121 skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK;
4122 skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK;
4123 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
4125 skmsg->next = &skmsg[1];
4128 /* Free list is in order starting with the 0th entry. */
4129 skdev->skmsg_table[i - 1].next = NULL;
4130 skdev->skmsg_free_list = skdev->skmsg_table;
4132 err_out:
4133 return rc;
4136 static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
4137 u32 n_sg,
4138 dma_addr_t *ret_dma_addr)
4140 struct fit_sg_descriptor *sg_list;
4141 u32 nbytes;
4143 nbytes = sizeof(*sg_list) * n_sg;
4145 sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);
4147 if (sg_list != NULL) {
4148 uint64_t dma_address = *ret_dma_addr;
4149 u32 i;
4151 memset(sg_list, 0, nbytes);
4153 for (i = 0; i < n_sg - 1; i++) {
4154 uint64_t ndp_off;
4155 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
4157 sg_list[i].next_desc_ptr = dma_address + ndp_off;
4159 sg_list[i].next_desc_ptr = 0LL;
4162 return sg_list;
4165 static int skd_cons_skreq(struct skd_device *skdev)
4167 int rc = 0;
4168 u32 i;
4170 pr_debug("%s:%s:%d skreq_table kzalloc, struct %lu, count %u total %lu\n",
4171 skdev->name, __func__, __LINE__,
4172 sizeof(struct skd_request_context),
4173 skdev->num_req_context,
4174 sizeof(struct skd_request_context) * skdev->num_req_context);
4176 skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
4177 * skdev->num_req_context, GFP_KERNEL);
4178 if (skdev->skreq_table == NULL) {
4179 rc = -ENOMEM;
4180 goto err_out;
4183 pr_debug("%s:%s:%d alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
4184 skdev->name, __func__, __LINE__,
4185 skdev->sgs_per_request, sizeof(struct scatterlist),
4186 skdev->sgs_per_request * sizeof(struct scatterlist));
4188 for (i = 0; i < skdev->num_req_context; i++) {
4189 struct skd_request_context *skreq;
4191 skreq = &skdev->skreq_table[i];
4193 skreq->id = i + SKD_ID_RW_REQUEST;
4194 skreq->state = SKD_REQ_STATE_IDLE;
4196 skreq->sg = kzalloc(sizeof(struct scatterlist) *
4197 skdev->sgs_per_request, GFP_KERNEL);
4198 if (skreq->sg == NULL) {
4199 rc = -ENOMEM;
4200 goto err_out;
4202 sg_init_table(skreq->sg, skdev->sgs_per_request);
4204 skreq->sksg_list = skd_cons_sg_list(skdev,
4205 skdev->sgs_per_request,
4206 &skreq->sksg_dma_address);
4208 if (skreq->sksg_list == NULL) {
4209 rc = -ENOMEM;
4210 goto err_out;
4213 skreq->next = &skreq[1];
4216 /* Free list is in order starting with the 0th entry. */
4217 skdev->skreq_table[i - 1].next = NULL;
4218 skdev->skreq_free_list = skdev->skreq_table;
4220 err_out:
4221 return rc;
4224 static int skd_cons_skspcl(struct skd_device *skdev)
4226 int rc = 0;
4227 u32 i, nbytes;
4229 pr_debug("%s:%s:%d skspcl_table kzalloc, struct %lu, count %u total %lu\n",
4230 skdev->name, __func__, __LINE__,
4231 sizeof(struct skd_special_context),
4232 skdev->n_special,
4233 sizeof(struct skd_special_context) * skdev->n_special);
4235 skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
4236 * skdev->n_special, GFP_KERNEL);
4237 if (skdev->skspcl_table == NULL) {
4238 rc = -ENOMEM;
4239 goto err_out;
4242 for (i = 0; i < skdev->n_special; i++) {
4243 struct skd_special_context *skspcl;
4245 skspcl = &skdev->skspcl_table[i];
4247 skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
4248 skspcl->req.state = SKD_REQ_STATE_IDLE;
4250 skspcl->req.next = &skspcl[1].req;
4252 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4254 skspcl->msg_buf =
4255 pci_zalloc_consistent(skdev->pdev, nbytes,
4256 &skspcl->mb_dma_address);
4257 if (skspcl->msg_buf == NULL) {
4258 rc = -ENOMEM;
4259 goto err_out;
4262 skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
4263 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
4264 if (skspcl->req.sg == NULL) {
4265 rc = -ENOMEM;
4266 goto err_out;
4269 skspcl->req.sksg_list = skd_cons_sg_list(skdev,
4270 SKD_N_SG_PER_SPECIAL,
4271 &skspcl->req.
4272 sksg_dma_address);
4273 if (skspcl->req.sksg_list == NULL) {
4274 rc = -ENOMEM;
4275 goto err_out;
4279 /* Free list is in order starting with the 0th entry. */
4280 skdev->skspcl_table[i - 1].req.next = NULL;
4281 skdev->skspcl_free_list = skdev->skspcl_table;
4283 return rc;
4285 err_out:
4286 return rc;
4289 static int skd_cons_sksb(struct skd_device *skdev)
4291 int rc = 0;
4292 struct skd_special_context *skspcl;
4293 u32 nbytes;
4295 skspcl = &skdev->internal_skspcl;
4297 skspcl->req.id = 0 + SKD_ID_INTERNAL;
4298 skspcl->req.state = SKD_REQ_STATE_IDLE;
4300 nbytes = SKD_N_INTERNAL_BYTES;
4302 skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4303 &skspcl->db_dma_address);
4304 if (skspcl->data_buf == NULL) {
4305 rc = -ENOMEM;
4306 goto err_out;
4309 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4310 skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
4311 &skspcl->mb_dma_address);
4312 if (skspcl->msg_buf == NULL) {
4313 rc = -ENOMEM;
4314 goto err_out;
4317 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
4318 &skspcl->req.sksg_dma_address);
4319 if (skspcl->req.sksg_list == NULL) {
4320 rc = -ENOMEM;
4321 goto err_out;
4324 if (!skd_format_internal_skspcl(skdev)) {
4325 rc = -EINVAL;
4326 goto err_out;
4329 err_out:
4330 return rc;
4333 static int skd_cons_disk(struct skd_device *skdev)
4335 int rc = 0;
4336 struct gendisk *disk;
4337 struct request_queue *q;
4338 unsigned long flags;
4340 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
4341 if (!disk) {
4342 rc = -ENOMEM;
4343 goto err_out;
4346 skdev->disk = disk;
4347 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
4349 disk->major = skdev->major;
4350 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
4351 disk->fops = &skd_blockdev_ops;
4352 disk->private_data = skdev;
4354 q = blk_init_queue(skd_request_fn, &skdev->lock);
4355 if (!q) {
4356 rc = -ENOMEM;
4357 goto err_out;
4360 skdev->queue = q;
4361 disk->queue = q;
4362 q->queuedata = skdev;
4364 blk_queue_write_cache(q, true, true);
4365 blk_queue_max_segments(q, skdev->sgs_per_request);
4366 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
4368 /* set sysfs ptimal_io_size to 8K */
4369 blk_queue_io_opt(q, 8192);
4371 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4372 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
4374 spin_lock_irqsave(&skdev->lock, flags);
4375 pr_debug("%s:%s:%d stopping %s queue\n",
4376 skdev->name, __func__, __LINE__, skdev->name);
4377 blk_stop_queue(skdev->queue);
4378 spin_unlock_irqrestore(&skdev->lock, flags);
4380 err_out:
4381 return rc;
4384 #define SKD_N_DEV_TABLE 16u
4385 static u32 skd_next_devno;
4387 static struct skd_device *skd_construct(struct pci_dev *pdev)
4389 struct skd_device *skdev;
4390 int blk_major = skd_major;
4391 int rc;
4393 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4395 if (!skdev) {
4396 pr_err(PFX "(%s): memory alloc failure\n",
4397 pci_name(pdev));
4398 return NULL;
4401 skdev->state = SKD_DRVR_STATE_LOAD;
4402 skdev->pdev = pdev;
4403 skdev->devno = skd_next_devno++;
4404 skdev->major = blk_major;
4405 skdev->irq_type = skd_isr_type;
4406 sprintf(skdev->name, DRV_NAME "%d", skdev->devno);
4407 skdev->dev_max_queue_depth = 0;
4409 skdev->num_req_context = skd_max_queue_depth;
4410 skdev->num_fitmsg_context = skd_max_queue_depth;
4411 skdev->n_special = skd_max_pass_thru;
4412 skdev->cur_max_queue_depth = 1;
4413 skdev->queue_low_water_mark = 1;
4414 skdev->proto_ver = 99;
4415 skdev->sgs_per_request = skd_sgs_per_request;
4416 skdev->dbg_level = skd_dbg_level;
4418 atomic_set(&skdev->device_count, 0);
4420 spin_lock_init(&skdev->lock);
4422 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4424 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4425 rc = skd_cons_skcomp(skdev);
4426 if (rc < 0)
4427 goto err_out;
4429 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4430 rc = skd_cons_skmsg(skdev);
4431 if (rc < 0)
4432 goto err_out;
4434 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4435 rc = skd_cons_skreq(skdev);
4436 if (rc < 0)
4437 goto err_out;
4439 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4440 rc = skd_cons_skspcl(skdev);
4441 if (rc < 0)
4442 goto err_out;
4444 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4445 rc = skd_cons_sksb(skdev);
4446 if (rc < 0)
4447 goto err_out;
4449 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4450 rc = skd_cons_disk(skdev);
4451 if (rc < 0)
4452 goto err_out;
4454 pr_debug("%s:%s:%d VICTORY\n", skdev->name, __func__, __LINE__);
4455 return skdev;
4457 err_out:
4458 pr_debug("%s:%s:%d construct failed\n",
4459 skdev->name, __func__, __LINE__);
4460 skd_destruct(skdev);
4461 return NULL;
4465 *****************************************************************************
4466 * DESTRUCT (FREE)
4467 *****************************************************************************
4470 static void skd_free_skcomp(struct skd_device *skdev)
4472 if (skdev->skcomp_table != NULL) {
4473 u32 nbytes;
4475 nbytes = sizeof(skdev->skcomp_table[0]) *
4476 SKD_N_COMPLETION_ENTRY;
4477 pci_free_consistent(skdev->pdev, nbytes,
4478 skdev->skcomp_table, skdev->cq_dma_address);
4481 skdev->skcomp_table = NULL;
4482 skdev->cq_dma_address = 0;
4485 static void skd_free_skmsg(struct skd_device *skdev)
4487 u32 i;
4489 if (skdev->skmsg_table == NULL)
4490 return;
4492 for (i = 0; i < skdev->num_fitmsg_context; i++) {
4493 struct skd_fitmsg_context *skmsg;
4495 skmsg = &skdev->skmsg_table[i];
4497 if (skmsg->msg_buf != NULL) {
4498 skmsg->msg_buf += skmsg->offset;
4499 skmsg->mb_dma_address += skmsg->offset;
4500 pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
4501 skmsg->msg_buf,
4502 skmsg->mb_dma_address);
4504 skmsg->msg_buf = NULL;
4505 skmsg->mb_dma_address = 0;
4508 kfree(skdev->skmsg_table);
4509 skdev->skmsg_table = NULL;
4512 static void skd_free_sg_list(struct skd_device *skdev,
4513 struct fit_sg_descriptor *sg_list,
4514 u32 n_sg, dma_addr_t dma_addr)
4516 if (sg_list != NULL) {
4517 u32 nbytes;
4519 nbytes = sizeof(*sg_list) * n_sg;
4521 pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
4525 static void skd_free_skreq(struct skd_device *skdev)
4527 u32 i;
4529 if (skdev->skreq_table == NULL)
4530 return;
4532 for (i = 0; i < skdev->num_req_context; i++) {
4533 struct skd_request_context *skreq;
4535 skreq = &skdev->skreq_table[i];
4537 skd_free_sg_list(skdev, skreq->sksg_list,
4538 skdev->sgs_per_request,
4539 skreq->sksg_dma_address);
4541 skreq->sksg_list = NULL;
4542 skreq->sksg_dma_address = 0;
4544 kfree(skreq->sg);
4547 kfree(skdev->skreq_table);
4548 skdev->skreq_table = NULL;
4551 static void skd_free_skspcl(struct skd_device *skdev)
4553 u32 i;
4554 u32 nbytes;
4556 if (skdev->skspcl_table == NULL)
4557 return;
4559 for (i = 0; i < skdev->n_special; i++) {
4560 struct skd_special_context *skspcl;
4562 skspcl = &skdev->skspcl_table[i];
4564 if (skspcl->msg_buf != NULL) {
4565 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4566 pci_free_consistent(skdev->pdev, nbytes,
4567 skspcl->msg_buf,
4568 skspcl->mb_dma_address);
4571 skspcl->msg_buf = NULL;
4572 skspcl->mb_dma_address = 0;
4574 skd_free_sg_list(skdev, skspcl->req.sksg_list,
4575 SKD_N_SG_PER_SPECIAL,
4576 skspcl->req.sksg_dma_address);
4578 skspcl->req.sksg_list = NULL;
4579 skspcl->req.sksg_dma_address = 0;
4581 kfree(skspcl->req.sg);
4584 kfree(skdev->skspcl_table);
4585 skdev->skspcl_table = NULL;
4588 static void skd_free_sksb(struct skd_device *skdev)
4590 struct skd_special_context *skspcl;
4591 u32 nbytes;
4593 skspcl = &skdev->internal_skspcl;
4595 if (skspcl->data_buf != NULL) {
4596 nbytes = SKD_N_INTERNAL_BYTES;
4598 pci_free_consistent(skdev->pdev, nbytes,
4599 skspcl->data_buf, skspcl->db_dma_address);
4602 skspcl->data_buf = NULL;
4603 skspcl->db_dma_address = 0;
4605 if (skspcl->msg_buf != NULL) {
4606 nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
4607 pci_free_consistent(skdev->pdev, nbytes,
4608 skspcl->msg_buf, skspcl->mb_dma_address);
4611 skspcl->msg_buf = NULL;
4612 skspcl->mb_dma_address = 0;
4614 skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
4615 skspcl->req.sksg_dma_address);
4617 skspcl->req.sksg_list = NULL;
4618 skspcl->req.sksg_dma_address = 0;
4621 static void skd_free_disk(struct skd_device *skdev)
4623 struct gendisk *disk = skdev->disk;
4625 if (disk != NULL) {
4626 struct request_queue *q = disk->queue;
4628 if (disk->flags & GENHD_FL_UP)
4629 del_gendisk(disk);
4630 if (q)
4631 blk_cleanup_queue(q);
4632 put_disk(disk);
4634 skdev->disk = NULL;
4637 static void skd_destruct(struct skd_device *skdev)
4639 if (skdev == NULL)
4640 return;
4643 pr_debug("%s:%s:%d disk\n", skdev->name, __func__, __LINE__);
4644 skd_free_disk(skdev);
4646 pr_debug("%s:%s:%d sksb\n", skdev->name, __func__, __LINE__);
4647 skd_free_sksb(skdev);
4649 pr_debug("%s:%s:%d skspcl\n", skdev->name, __func__, __LINE__);
4650 skd_free_skspcl(skdev);
4652 pr_debug("%s:%s:%d skreq\n", skdev->name, __func__, __LINE__);
4653 skd_free_skreq(skdev);
4655 pr_debug("%s:%s:%d skmsg\n", skdev->name, __func__, __LINE__);
4656 skd_free_skmsg(skdev);
4658 pr_debug("%s:%s:%d skcomp\n", skdev->name, __func__, __LINE__);
4659 skd_free_skcomp(skdev);
4661 pr_debug("%s:%s:%d skdev\n", skdev->name, __func__, __LINE__);
4662 kfree(skdev);
4666 *****************************************************************************
4667 * BLOCK DEVICE (BDEV) GLUE
4668 *****************************************************************************
4671 static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4673 struct skd_device *skdev;
4674 u64 capacity;
4676 skdev = bdev->bd_disk->private_data;
4678 pr_debug("%s:%s:%d %s: CMD[%s] getgeo device\n",
4679 skdev->name, __func__, __LINE__,
4680 bdev->bd_disk->disk_name, current->comm);
4682 if (skdev->read_cap_is_valid) {
4683 capacity = get_capacity(skdev->disk);
4684 geo->heads = 64;
4685 geo->sectors = 255;
4686 geo->cylinders = (capacity) / (255 * 64);
4688 return 0;
4690 return -EIO;
4693 static int skd_bdev_attach(struct skd_device *skdev)
4695 pr_debug("%s:%s:%d add_disk\n", skdev->name, __func__, __LINE__);
4696 add_disk(skdev->disk);
4697 return 0;
4700 static const struct block_device_operations skd_blockdev_ops = {
4701 .owner = THIS_MODULE,
4702 .ioctl = skd_bdev_ioctl,
4703 .getgeo = skd_bdev_getgeo,
4708 *****************************************************************************
4709 * PCIe DRIVER GLUE
4710 *****************************************************************************
4713 static const struct pci_device_id skd_pci_tbl[] = {
4714 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
4715 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
4716 { 0 } /* terminate list */
4719 MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
4721 static char *skd_pci_info(struct skd_device *skdev, char *str)
4723 int pcie_reg;
4725 strcpy(str, "PCIe (");
4726 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
4728 if (pcie_reg) {
4730 char lwstr[6];
4731 uint16_t pcie_lstat, lspeed, lwidth;
4733 pcie_reg += 0x12;
4734 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
4735 lspeed = pcie_lstat & (0xF);
4736 lwidth = (pcie_lstat & 0x3F0) >> 4;
4738 if (lspeed == 1)
4739 strcat(str, "2.5GT/s ");
4740 else if (lspeed == 2)
4741 strcat(str, "5.0GT/s ");
4742 else
4743 strcat(str, "<unknown> ");
4744 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
4745 strcat(str, lwstr);
4747 return str;
4750 static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4752 int i;
4753 int rc = 0;
4754 char pci_str[32];
4755 struct skd_device *skdev;
4757 pr_info("STEC s1120 Driver(%s) version %s-b%s\n",
4758 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
4759 pr_info("(skd?:??:[%s]): vendor=%04X device=%04x\n",
4760 pci_name(pdev), pdev->vendor, pdev->device);
4762 rc = pci_enable_device(pdev);
4763 if (rc)
4764 return rc;
4765 rc = pci_request_regions(pdev, DRV_NAME);
4766 if (rc)
4767 goto err_out;
4768 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4769 if (!rc) {
4770 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4772 pr_err("(%s): consistent DMA mask error %d\n",
4773 pci_name(pdev), rc);
4775 } else {
4776 (rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)));
4777 if (rc) {
4779 pr_err("(%s): DMA mask error %d\n",
4780 pci_name(pdev), rc);
4781 goto err_out_regions;
4785 if (!skd_major) {
4786 rc = register_blkdev(0, DRV_NAME);
4787 if (rc < 0)
4788 goto err_out_regions;
4789 BUG_ON(!rc);
4790 skd_major = rc;
4793 skdev = skd_construct(pdev);
4794 if (skdev == NULL) {
4795 rc = -ENOMEM;
4796 goto err_out_regions;
4799 skd_pci_info(skdev, pci_str);
4800 pr_info("(%s): %s 64bit\n", skd_name(skdev), pci_str);
4802 pci_set_master(pdev);
4803 rc = pci_enable_pcie_error_reporting(pdev);
4804 if (rc) {
4805 pr_err(
4806 "(%s): bad enable of PCIe error reporting rc=%d\n",
4807 skd_name(skdev), rc);
4808 skdev->pcie_error_reporting_is_enabled = 0;
4809 } else
4810 skdev->pcie_error_reporting_is_enabled = 1;
4813 pci_set_drvdata(pdev, skdev);
4815 skdev->disk->driverfs_dev = &pdev->dev;
4817 for (i = 0; i < SKD_MAX_BARS; i++) {
4818 skdev->mem_phys[i] = pci_resource_start(pdev, i);
4819 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
4820 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
4821 skdev->mem_size[i]);
4822 if (!skdev->mem_map[i]) {
4823 pr_err("(%s): Unable to map adapter memory!\n",
4824 skd_name(skdev));
4825 rc = -ENODEV;
4826 goto err_out_iounmap;
4828 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
4829 skdev->name, __func__, __LINE__,
4830 skdev->mem_map[i],
4831 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
4834 rc = skd_acquire_irq(skdev);
4835 if (rc) {
4836 pr_err("(%s): interrupt resource error %d\n",
4837 skd_name(skdev), rc);
4838 goto err_out_iounmap;
4841 rc = skd_start_timer(skdev);
4842 if (rc)
4843 goto err_out_timer;
4845 init_waitqueue_head(&skdev->waitq);
4847 skd_start_device(skdev);
4849 rc = wait_event_interruptible_timeout(skdev->waitq,
4850 (skdev->gendisk_on),
4851 (SKD_START_WAIT_SECONDS * HZ));
4852 if (skdev->gendisk_on > 0) {
4853 /* device came on-line after reset */
4854 skd_bdev_attach(skdev);
4855 rc = 0;
4856 } else {
4857 /* we timed out, something is wrong with the device,
4858 don't add the disk structure */
4859 pr_err(
4860 "(%s): error: waiting for s1120 timed out %d!\n",
4861 skd_name(skdev), rc);
4862 /* in case of no error; we timeout with ENXIO */
4863 if (!rc)
4864 rc = -ENXIO;
4865 goto err_out_timer;
4869 #ifdef SKD_VMK_POLL_HANDLER
4870 if (skdev->irq_type == SKD_IRQ_MSIX) {
4871 /* MSIX completion handler is being used for coredump */
4872 vmklnx_scsi_register_poll_handler(skdev->scsi_host,
4873 skdev->msix_entries[5].vector,
4874 skd_comp_q, skdev);
4875 } else {
4876 vmklnx_scsi_register_poll_handler(skdev->scsi_host,
4877 skdev->pdev->irq, skd_isr,
4878 skdev);
4880 #endif /* SKD_VMK_POLL_HANDLER */
4882 return rc;
4884 err_out_timer:
4885 skd_stop_device(skdev);
4886 skd_release_irq(skdev);
4888 err_out_iounmap:
4889 for (i = 0; i < SKD_MAX_BARS; i++)
4890 if (skdev->mem_map[i])
4891 iounmap(skdev->mem_map[i]);
4893 if (skdev->pcie_error_reporting_is_enabled)
4894 pci_disable_pcie_error_reporting(pdev);
4896 skd_destruct(skdev);
4898 err_out_regions:
4899 pci_release_regions(pdev);
4901 err_out:
4902 pci_disable_device(pdev);
4903 pci_set_drvdata(pdev, NULL);
4904 return rc;
4907 static void skd_pci_remove(struct pci_dev *pdev)
4909 int i;
4910 struct skd_device *skdev;
4912 skdev = pci_get_drvdata(pdev);
4913 if (!skdev) {
4914 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4915 return;
4917 skd_stop_device(skdev);
4918 skd_release_irq(skdev);
4920 for (i = 0; i < SKD_MAX_BARS; i++)
4921 if (skdev->mem_map[i])
4922 iounmap((u32 *)skdev->mem_map[i]);
4924 if (skdev->pcie_error_reporting_is_enabled)
4925 pci_disable_pcie_error_reporting(pdev);
4927 skd_destruct(skdev);
4929 pci_release_regions(pdev);
4930 pci_disable_device(pdev);
4931 pci_set_drvdata(pdev, NULL);
4933 return;
4936 static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4938 int i;
4939 struct skd_device *skdev;
4941 skdev = pci_get_drvdata(pdev);
4942 if (!skdev) {
4943 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4944 return -EIO;
4947 skd_stop_device(skdev);
4949 skd_release_irq(skdev);
4951 for (i = 0; i < SKD_MAX_BARS; i++)
4952 if (skdev->mem_map[i])
4953 iounmap((u32 *)skdev->mem_map[i]);
4955 if (skdev->pcie_error_reporting_is_enabled)
4956 pci_disable_pcie_error_reporting(pdev);
4958 pci_release_regions(pdev);
4959 pci_save_state(pdev);
4960 pci_disable_device(pdev);
4961 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4962 return 0;
4965 static int skd_pci_resume(struct pci_dev *pdev)
4967 int i;
4968 int rc = 0;
4969 struct skd_device *skdev;
4971 skdev = pci_get_drvdata(pdev);
4972 if (!skdev) {
4973 pr_err("%s: no device data for PCI\n", pci_name(pdev));
4974 return -1;
4977 pci_set_power_state(pdev, PCI_D0);
4978 pci_enable_wake(pdev, PCI_D0, 0);
4979 pci_restore_state(pdev);
4981 rc = pci_enable_device(pdev);
4982 if (rc)
4983 return rc;
4984 rc = pci_request_regions(pdev, DRV_NAME);
4985 if (rc)
4986 goto err_out;
4987 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4988 if (!rc) {
4989 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4991 pr_err("(%s): consistent DMA mask error %d\n",
4992 pci_name(pdev), rc);
4994 } else {
4995 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4996 if (rc) {
4998 pr_err("(%s): DMA mask error %d\n",
4999 pci_name(pdev), rc);
5000 goto err_out_regions;
5004 pci_set_master(pdev);
5005 rc = pci_enable_pcie_error_reporting(pdev);
5006 if (rc) {
5007 pr_err("(%s): bad enable of PCIe error reporting rc=%d\n",
5008 skdev->name, rc);
5009 skdev->pcie_error_reporting_is_enabled = 0;
5010 } else
5011 skdev->pcie_error_reporting_is_enabled = 1;
5013 for (i = 0; i < SKD_MAX_BARS; i++) {
5015 skdev->mem_phys[i] = pci_resource_start(pdev, i);
5016 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
5017 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
5018 skdev->mem_size[i]);
5019 if (!skdev->mem_map[i]) {
5020 pr_err("(%s): Unable to map adapter memory!\n",
5021 skd_name(skdev));
5022 rc = -ENODEV;
5023 goto err_out_iounmap;
5025 pr_debug("%s:%s:%d mem_map=%p, phyd=%016llx, size=%d\n",
5026 skdev->name, __func__, __LINE__,
5027 skdev->mem_map[i],
5028 (uint64_t)skdev->mem_phys[i], skdev->mem_size[i]);
5030 rc = skd_acquire_irq(skdev);
5031 if (rc) {
5033 pr_err("(%s): interrupt resource error %d\n",
5034 pci_name(pdev), rc);
5035 goto err_out_iounmap;
5038 rc = skd_start_timer(skdev);
5039 if (rc)
5040 goto err_out_timer;
5042 init_waitqueue_head(&skdev->waitq);
5044 skd_start_device(skdev);
5046 return rc;
5048 err_out_timer:
5049 skd_stop_device(skdev);
5050 skd_release_irq(skdev);
5052 err_out_iounmap:
5053 for (i = 0; i < SKD_MAX_BARS; i++)
5054 if (skdev->mem_map[i])
5055 iounmap(skdev->mem_map[i]);
5057 if (skdev->pcie_error_reporting_is_enabled)
5058 pci_disable_pcie_error_reporting(pdev);
5060 err_out_regions:
5061 pci_release_regions(pdev);
5063 err_out:
5064 pci_disable_device(pdev);
5065 return rc;
5068 static void skd_pci_shutdown(struct pci_dev *pdev)
5070 struct skd_device *skdev;
5072 pr_err("skd_pci_shutdown called\n");
5074 skdev = pci_get_drvdata(pdev);
5075 if (!skdev) {
5076 pr_err("%s: no device data for PCI\n", pci_name(pdev));
5077 return;
5080 pr_err("%s: calling stop\n", skd_name(skdev));
5081 skd_stop_device(skdev);
5084 static struct pci_driver skd_driver = {
5085 .name = DRV_NAME,
5086 .id_table = skd_pci_tbl,
5087 .probe = skd_pci_probe,
5088 .remove = skd_pci_remove,
5089 .suspend = skd_pci_suspend,
5090 .resume = skd_pci_resume,
5091 .shutdown = skd_pci_shutdown,
5095 *****************************************************************************
5096 * LOGGING SUPPORT
5097 *****************************************************************************
5100 static const char *skd_name(struct skd_device *skdev)
5102 memset(skdev->id_str, 0, sizeof(skdev->id_str));
5104 if (skdev->inquiry_is_valid)
5105 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:%s:[%s]",
5106 skdev->name, skdev->inq_serial_num,
5107 pci_name(skdev->pdev));
5108 else
5109 snprintf(skdev->id_str, sizeof(skdev->id_str), "%s:??:[%s]",
5110 skdev->name, pci_name(skdev->pdev));
5112 return skdev->id_str;
5115 const char *skd_drive_state_to_str(int state)
5117 switch (state) {
5118 case FIT_SR_DRIVE_OFFLINE:
5119 return "OFFLINE";
5120 case FIT_SR_DRIVE_INIT:
5121 return "INIT";
5122 case FIT_SR_DRIVE_ONLINE:
5123 return "ONLINE";
5124 case FIT_SR_DRIVE_BUSY:
5125 return "BUSY";
5126 case FIT_SR_DRIVE_FAULT:
5127 return "FAULT";
5128 case FIT_SR_DRIVE_DEGRADED:
5129 return "DEGRADED";
5130 case FIT_SR_PCIE_LINK_DOWN:
5131 return "INK_DOWN";
5132 case FIT_SR_DRIVE_SOFT_RESET:
5133 return "SOFT_RESET";
5134 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
5135 return "NEED_FW";
5136 case FIT_SR_DRIVE_INIT_FAULT:
5137 return "INIT_FAULT";
5138 case FIT_SR_DRIVE_BUSY_SANITIZE:
5139 return "BUSY_SANITIZE";
5140 case FIT_SR_DRIVE_BUSY_ERASE:
5141 return "BUSY_ERASE";
5142 case FIT_SR_DRIVE_FW_BOOTING:
5143 return "FW_BOOTING";
5144 default:
5145 return "???";
5149 const char *skd_skdev_state_to_str(enum skd_drvr_state state)
5151 switch (state) {
5152 case SKD_DRVR_STATE_LOAD:
5153 return "LOAD";
5154 case SKD_DRVR_STATE_IDLE:
5155 return "IDLE";
5156 case SKD_DRVR_STATE_BUSY:
5157 return "BUSY";
5158 case SKD_DRVR_STATE_STARTING:
5159 return "STARTING";
5160 case SKD_DRVR_STATE_ONLINE:
5161 return "ONLINE";
5162 case SKD_DRVR_STATE_PAUSING:
5163 return "PAUSING";
5164 case SKD_DRVR_STATE_PAUSED:
5165 return "PAUSED";
5166 case SKD_DRVR_STATE_DRAINING_TIMEOUT:
5167 return "DRAINING_TIMEOUT";
5168 case SKD_DRVR_STATE_RESTARTING:
5169 return "RESTARTING";
5170 case SKD_DRVR_STATE_RESUMING:
5171 return "RESUMING";
5172 case SKD_DRVR_STATE_STOPPING:
5173 return "STOPPING";
5174 case SKD_DRVR_STATE_SYNCING:
5175 return "SYNCING";
5176 case SKD_DRVR_STATE_FAULT:
5177 return "FAULT";
5178 case SKD_DRVR_STATE_DISAPPEARED:
5179 return "DISAPPEARED";
5180 case SKD_DRVR_STATE_BUSY_ERASE:
5181 return "BUSY_ERASE";
5182 case SKD_DRVR_STATE_BUSY_SANITIZE:
5183 return "BUSY_SANITIZE";
5184 case SKD_DRVR_STATE_BUSY_IMMINENT:
5185 return "BUSY_IMMINENT";
5186 case SKD_DRVR_STATE_WAIT_BOOT:
5187 return "WAIT_BOOT";
5189 default:
5190 return "???";
5194 static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
5196 switch (state) {
5197 case SKD_MSG_STATE_IDLE:
5198 return "IDLE";
5199 case SKD_MSG_STATE_BUSY:
5200 return "BUSY";
5201 default:
5202 return "???";
5206 static const char *skd_skreq_state_to_str(enum skd_req_state state)
5208 switch (state) {
5209 case SKD_REQ_STATE_IDLE:
5210 return "IDLE";
5211 case SKD_REQ_STATE_SETUP:
5212 return "SETUP";
5213 case SKD_REQ_STATE_BUSY:
5214 return "BUSY";
5215 case SKD_REQ_STATE_COMPLETED:
5216 return "COMPLETED";
5217 case SKD_REQ_STATE_TIMEOUT:
5218 return "TIMEOUT";
5219 case SKD_REQ_STATE_ABORTED:
5220 return "ABORTED";
5221 default:
5222 return "???";
5226 static void skd_log_skdev(struct skd_device *skdev, const char *event)
5228 pr_debug("%s:%s:%d (%s) skdev=%p event='%s'\n",
5229 skdev->name, __func__, __LINE__, skdev->name, skdev, event);
5230 pr_debug("%s:%s:%d drive_state=%s(%d) driver_state=%s(%d)\n",
5231 skdev->name, __func__, __LINE__,
5232 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
5233 skd_skdev_state_to_str(skdev->state), skdev->state);
5234 pr_debug("%s:%s:%d busy=%d limit=%d dev=%d lowat=%d\n",
5235 skdev->name, __func__, __LINE__,
5236 skdev->in_flight, skdev->cur_max_queue_depth,
5237 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
5238 pr_debug("%s:%s:%d timestamp=0x%x cycle=%d cycle_ix=%d\n",
5239 skdev->name, __func__, __LINE__,
5240 skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
5243 static void skd_log_skmsg(struct skd_device *skdev,
5244 struct skd_fitmsg_context *skmsg, const char *event)
5246 pr_debug("%s:%s:%d (%s) skmsg=%p event='%s'\n",
5247 skdev->name, __func__, __LINE__, skdev->name, skmsg, event);
5248 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x length=%d\n",
5249 skdev->name, __func__, __LINE__,
5250 skd_skmsg_state_to_str(skmsg->state), skmsg->state,
5251 skmsg->id, skmsg->length);
5254 static void skd_log_skreq(struct skd_device *skdev,
5255 struct skd_request_context *skreq, const char *event)
5257 pr_debug("%s:%s:%d (%s) skreq=%p event='%s'\n",
5258 skdev->name, __func__, __LINE__, skdev->name, skreq, event);
5259 pr_debug("%s:%s:%d state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
5260 skdev->name, __func__, __LINE__,
5261 skd_skreq_state_to_str(skreq->state), skreq->state,
5262 skreq->id, skreq->fitmsg_id);
5263 pr_debug("%s:%s:%d timo=0x%x sg_dir=%d n_sg=%d\n",
5264 skdev->name, __func__, __LINE__,
5265 skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
5267 if (skreq->req != NULL) {
5268 struct request *req = skreq->req;
5269 u32 lba = (u32)blk_rq_pos(req);
5270 u32 count = blk_rq_sectors(req);
5272 pr_debug("%s:%s:%d "
5273 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
5274 skdev->name, __func__, __LINE__,
5275 req, lba, lba, count, count,
5276 (int)rq_data_dir(req));
5277 } else
5278 pr_debug("%s:%s:%d req=NULL\n",
5279 skdev->name, __func__, __LINE__);
5283 *****************************************************************************
5284 * MODULE GLUE
5285 *****************************************************************************
5288 static int __init skd_init(void)
5290 pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);
5292 switch (skd_isr_type) {
5293 case SKD_IRQ_LEGACY:
5294 case SKD_IRQ_MSI:
5295 case SKD_IRQ_MSIX:
5296 break;
5297 default:
5298 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
5299 skd_isr_type, SKD_IRQ_DEFAULT);
5300 skd_isr_type = SKD_IRQ_DEFAULT;
5303 if (skd_max_queue_depth < 1 ||
5304 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
5305 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
5306 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
5307 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
5310 if (skd_max_req_per_msg < 1 || skd_max_req_per_msg > 14) {
5311 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
5312 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
5313 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
5316 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
5317 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
5318 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
5319 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
5322 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
5323 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
5324 skd_dbg_level, 0);
5325 skd_dbg_level = 0;
5328 if (skd_isr_comp_limit < 0) {
5329 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
5330 skd_isr_comp_limit, 0);
5331 skd_isr_comp_limit = 0;
5334 if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
5335 pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
5336 skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
5337 skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
5340 return pci_register_driver(&skd_driver);
5343 static void __exit skd_exit(void)
5345 pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);
5347 pci_unregister_driver(&skd_driver);
5349 if (skd_major)
5350 unregister_blkdev(skd_major, DRV_NAME);
5353 module_init(skd_init);
5354 module_exit(skd_exit);