Linux 5.1.15
[linux/fpc-iii.git] / drivers / block / skd_main.c
blob7d3ad6c22ee5171a8e55515bea06a3c224a2382a
1 /*
2 * Driver for sTec s1120 PCIe SSDs. sTec was acquired in 2013 by HGST and HGST
3 * was acquired by Western Digital in 2012.
5 * Copyright 2012 sTec, Inc.
6 * Copyright (c) 2017 Western Digital Corporation or its affiliates.
8 * This file is part of the Linux kernel, and is made available under
9 * the terms of the GNU General Public License version 2.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/pci.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/blkdev.h>
19 #include <linux/blk-mq.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/compiler.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/time.h>
26 #include <linux/hdreg.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/completion.h>
29 #include <linux/scatterlist.h>
30 #include <linux/version.h>
31 #include <linux/err.h>
32 #include <linux/aer.h>
33 #include <linux/wait.h>
34 #include <linux/stringify.h>
35 #include <scsi/scsi.h>
36 #include <scsi/sg.h>
37 #include <linux/io.h>
38 #include <linux/uaccess.h>
39 #include <asm/unaligned.h>
41 #include "skd_s1120.h"
43 static int skd_dbg_level;
44 static int skd_isr_comp_limit = 4;
46 #define SKD_ASSERT(expr) \
47 do { \
48 if (unlikely(!(expr))) { \
49 pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
50 # expr, __FILE__, __func__, __LINE__); \
51 } \
52 } while (0)
54 #define DRV_NAME "skd"
55 #define PFX DRV_NAME ": "
57 MODULE_LICENSE("GPL");
59 MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver");
61 #define PCI_VENDOR_ID_STEC 0x1B39
62 #define PCI_DEVICE_ID_S1120 0x0001
64 #define SKD_FUA_NV (1 << 1)
65 #define SKD_MINORS_PER_DEVICE 16
67 #define SKD_MAX_QUEUE_DEPTH 200u
69 #define SKD_PAUSE_TIMEOUT (5 * 1000)
71 #define SKD_N_FITMSG_BYTES (512u)
72 #define SKD_MAX_REQ_PER_MSG 14
74 #define SKD_N_SPECIAL_FITMSG_BYTES (128u)
76 /* SG elements are 32 bytes, so we can make this 4096 and still be under the
77 * 128KB limit. That allows 4096*4K = 16M xfer size
79 #define SKD_N_SG_PER_REQ_DEFAULT 256u
81 #define SKD_N_COMPLETION_ENTRY 256u
82 #define SKD_N_READ_CAP_BYTES (8u)
84 #define SKD_N_INTERNAL_BYTES (512u)
86 #define SKD_SKCOMP_SIZE \
87 ((sizeof(struct fit_completion_entry_v1) + \
88 sizeof(struct fit_comp_error_info)) * SKD_N_COMPLETION_ENTRY)
90 /* 5 bits of uniqifier, 0xF800 */
91 #define SKD_ID_TABLE_MASK (3u << 8u)
92 #define SKD_ID_RW_REQUEST (0u << 8u)
93 #define SKD_ID_INTERNAL (1u << 8u)
94 #define SKD_ID_FIT_MSG (3u << 8u)
95 #define SKD_ID_SLOT_MASK 0x00FFu
96 #define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu
98 #define SKD_N_MAX_SECTORS 2048u
100 #define SKD_MAX_RETRIES 2u
102 #define SKD_TIMER_SECONDS(seconds) (seconds)
103 #define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))
105 #define INQ_STD_NBYTES 36
107 enum skd_drvr_state {
108 SKD_DRVR_STATE_LOAD,
109 SKD_DRVR_STATE_IDLE,
110 SKD_DRVR_STATE_BUSY,
111 SKD_DRVR_STATE_STARTING,
112 SKD_DRVR_STATE_ONLINE,
113 SKD_DRVR_STATE_PAUSING,
114 SKD_DRVR_STATE_PAUSED,
115 SKD_DRVR_STATE_RESTARTING,
116 SKD_DRVR_STATE_RESUMING,
117 SKD_DRVR_STATE_STOPPING,
118 SKD_DRVR_STATE_FAULT,
119 SKD_DRVR_STATE_DISAPPEARED,
120 SKD_DRVR_STATE_PROTOCOL_MISMATCH,
121 SKD_DRVR_STATE_BUSY_ERASE,
122 SKD_DRVR_STATE_BUSY_SANITIZE,
123 SKD_DRVR_STATE_BUSY_IMMINENT,
124 SKD_DRVR_STATE_WAIT_BOOT,
125 SKD_DRVR_STATE_SYNCING,
128 #define SKD_WAIT_BOOT_TIMO SKD_TIMER_SECONDS(90u)
129 #define SKD_STARTING_TIMO SKD_TIMER_SECONDS(8u)
130 #define SKD_RESTARTING_TIMO SKD_TIMER_MINUTES(4u)
131 #define SKD_BUSY_TIMO SKD_TIMER_MINUTES(20u)
132 #define SKD_STARTED_BUSY_TIMO SKD_TIMER_SECONDS(60u)
133 #define SKD_START_WAIT_SECONDS 90u
135 enum skd_req_state {
136 SKD_REQ_STATE_IDLE,
137 SKD_REQ_STATE_SETUP,
138 SKD_REQ_STATE_BUSY,
139 SKD_REQ_STATE_COMPLETED,
140 SKD_REQ_STATE_TIMEOUT,
143 enum skd_check_status_action {
144 SKD_CHECK_STATUS_REPORT_GOOD,
145 SKD_CHECK_STATUS_REPORT_SMART_ALERT,
146 SKD_CHECK_STATUS_REQUEUE_REQUEST,
147 SKD_CHECK_STATUS_REPORT_ERROR,
148 SKD_CHECK_STATUS_BUSY_IMMINENT,
151 struct skd_msg_buf {
152 struct fit_msg_hdr fmh;
153 struct skd_scsi_request scsi[SKD_MAX_REQ_PER_MSG];
156 struct skd_fitmsg_context {
157 u32 id;
159 u32 length;
161 struct skd_msg_buf *msg_buf;
162 dma_addr_t mb_dma_address;
165 struct skd_request_context {
166 enum skd_req_state state;
168 u16 id;
169 u32 fitmsg_id;
171 u8 flush_cmd;
173 enum dma_data_direction data_dir;
174 struct scatterlist *sg;
175 u32 n_sg;
176 u32 sg_byte_count;
178 struct fit_sg_descriptor *sksg_list;
179 dma_addr_t sksg_dma_address;
181 struct fit_completion_entry_v1 completion;
183 struct fit_comp_error_info err_info;
184 int retries;
186 blk_status_t status;
189 struct skd_special_context {
190 struct skd_request_context req;
192 void *data_buf;
193 dma_addr_t db_dma_address;
195 struct skd_msg_buf *msg_buf;
196 dma_addr_t mb_dma_address;
199 typedef enum skd_irq_type {
200 SKD_IRQ_LEGACY,
201 SKD_IRQ_MSI,
202 SKD_IRQ_MSIX
203 } skd_irq_type_t;
205 #define SKD_MAX_BARS 2
207 struct skd_device {
208 void __iomem *mem_map[SKD_MAX_BARS];
209 resource_size_t mem_phys[SKD_MAX_BARS];
210 u32 mem_size[SKD_MAX_BARS];
212 struct skd_msix_entry *msix_entries;
214 struct pci_dev *pdev;
215 int pcie_error_reporting_is_enabled;
217 spinlock_t lock;
218 struct gendisk *disk;
219 struct blk_mq_tag_set tag_set;
220 struct request_queue *queue;
221 struct skd_fitmsg_context *skmsg;
222 struct device *class_dev;
223 int gendisk_on;
224 int sync_done;
226 u32 devno;
227 u32 major;
228 char isr_name[30];
230 enum skd_drvr_state state;
231 u32 drive_state;
233 u32 cur_max_queue_depth;
234 u32 queue_low_water_mark;
235 u32 dev_max_queue_depth;
237 u32 num_fitmsg_context;
238 u32 num_req_context;
240 struct skd_fitmsg_context *skmsg_table;
242 struct skd_special_context internal_skspcl;
243 u32 read_cap_blocksize;
244 u32 read_cap_last_lba;
245 int read_cap_is_valid;
246 int inquiry_is_valid;
247 u8 inq_serial_num[13]; /*12 chars plus null term */
249 u8 skcomp_cycle;
250 u32 skcomp_ix;
251 struct kmem_cache *msgbuf_cache;
252 struct kmem_cache *sglist_cache;
253 struct kmem_cache *databuf_cache;
254 struct fit_completion_entry_v1 *skcomp_table;
255 struct fit_comp_error_info *skerr_table;
256 dma_addr_t cq_dma_address;
258 wait_queue_head_t waitq;
260 struct timer_list timer;
261 u32 timer_countdown;
262 u32 timer_substate;
264 int sgs_per_request;
265 u32 last_mtd;
267 u32 proto_ver;
269 int dbg_level;
270 u32 connect_time_stamp;
271 int connect_retries;
272 #define SKD_MAX_CONNECT_RETRIES 16
273 u32 drive_jiffies;
275 u32 timo_slot;
277 struct work_struct start_queue;
278 struct work_struct completion_worker;
281 #define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
282 #define SKD_READL(DEV, OFF) skd_reg_read32(DEV, OFF)
283 #define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)
285 static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
287 u32 val = readl(skdev->mem_map[1] + offset);
289 if (unlikely(skdev->dbg_level >= 2))
290 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
291 return val;
294 static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
295 u32 offset)
297 writel(val, skdev->mem_map[1] + offset);
298 if (unlikely(skdev->dbg_level >= 2))
299 dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
302 static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
303 u32 offset)
305 writeq(val, skdev->mem_map[1] + offset);
306 if (unlikely(skdev->dbg_level >= 2))
307 dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
308 val);
312 #define SKD_IRQ_DEFAULT SKD_IRQ_MSIX
313 static int skd_isr_type = SKD_IRQ_DEFAULT;
315 module_param(skd_isr_type, int, 0444);
316 MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
317 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");
319 #define SKD_MAX_REQ_PER_MSG_DEFAULT 1
320 static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
322 module_param(skd_max_req_per_msg, int, 0444);
323 MODULE_PARM_DESC(skd_max_req_per_msg,
324 "Maximum SCSI requests packed in a single message."
325 " (1-" __stringify(SKD_MAX_REQ_PER_MSG) ", default==1)");
327 #define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
328 #define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
329 static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
331 module_param(skd_max_queue_depth, int, 0444);
332 MODULE_PARM_DESC(skd_max_queue_depth,
333 "Maximum SCSI requests issued to s1120."
334 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");
336 static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
337 module_param(skd_sgs_per_request, int, 0444);
338 MODULE_PARM_DESC(skd_sgs_per_request,
339 "Maximum SG elements per block request."
340 " (1-4096, default==256)");
342 static int skd_max_pass_thru = 1;
343 module_param(skd_max_pass_thru, int, 0444);
344 MODULE_PARM_DESC(skd_max_pass_thru,
345 "Maximum SCSI pass-thru at a time. IGNORED");
347 module_param(skd_dbg_level, int, 0444);
348 MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");
350 module_param(skd_isr_comp_limit, int, 0444);
351 MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");
353 /* Major device number dynamically assigned. */
354 static u32 skd_major;
356 static void skd_destruct(struct skd_device *skdev);
357 static const struct block_device_operations skd_blockdev_ops;
358 static void skd_send_fitmsg(struct skd_device *skdev,
359 struct skd_fitmsg_context *skmsg);
360 static void skd_send_special_fitmsg(struct skd_device *skdev,
361 struct skd_special_context *skspcl);
362 static bool skd_preop_sg_list(struct skd_device *skdev,
363 struct skd_request_context *skreq);
364 static void skd_postop_sg_list(struct skd_device *skdev,
365 struct skd_request_context *skreq);
367 static void skd_restart_device(struct skd_device *skdev);
368 static int skd_quiesce_dev(struct skd_device *skdev);
369 static int skd_unquiesce_dev(struct skd_device *skdev);
370 static void skd_disable_interrupts(struct skd_device *skdev);
371 static void skd_isr_fwstate(struct skd_device *skdev);
372 static void skd_recover_requests(struct skd_device *skdev);
373 static void skd_soft_reset(struct skd_device *skdev);
375 const char *skd_drive_state_to_str(int state);
376 const char *skd_skdev_state_to_str(enum skd_drvr_state state);
377 static void skd_log_skdev(struct skd_device *skdev, const char *event);
378 static void skd_log_skreq(struct skd_device *skdev,
379 struct skd_request_context *skreq, const char *event);
382 *****************************************************************************
383 * READ/WRITE REQUESTS
384 *****************************************************************************
386 static bool skd_inc_in_flight(struct request *rq, void *data, bool reserved)
388 int *count = data;
390 count++;
391 return true;
394 static int skd_in_flight(struct skd_device *skdev)
396 int count = 0;
398 blk_mq_tagset_busy_iter(&skdev->tag_set, skd_inc_in_flight, &count);
400 return count;
403 static void
404 skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
405 int data_dir, unsigned lba,
406 unsigned count)
408 if (data_dir == READ)
409 scsi_req->cdb[0] = READ_10;
410 else
411 scsi_req->cdb[0] = WRITE_10;
413 scsi_req->cdb[1] = 0;
414 scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
415 scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
416 scsi_req->cdb[4] = (lba & 0xff00) >> 8;
417 scsi_req->cdb[5] = (lba & 0xff);
418 scsi_req->cdb[6] = 0;
419 scsi_req->cdb[7] = (count & 0xff00) >> 8;
420 scsi_req->cdb[8] = count & 0xff;
421 scsi_req->cdb[9] = 0;
424 static void
425 skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
426 struct skd_request_context *skreq)
428 skreq->flush_cmd = 1;
430 scsi_req->cdb[0] = SYNCHRONIZE_CACHE;
431 scsi_req->cdb[1] = 0;
432 scsi_req->cdb[2] = 0;
433 scsi_req->cdb[3] = 0;
434 scsi_req->cdb[4] = 0;
435 scsi_req->cdb[5] = 0;
436 scsi_req->cdb[6] = 0;
437 scsi_req->cdb[7] = 0;
438 scsi_req->cdb[8] = 0;
439 scsi_req->cdb[9] = 0;
443 * Return true if and only if all pending requests should be failed.
445 static bool skd_fail_all(struct request_queue *q)
447 struct skd_device *skdev = q->queuedata;
449 SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);
451 skd_log_skdev(skdev, "req_not_online");
452 switch (skdev->state) {
453 case SKD_DRVR_STATE_PAUSING:
454 case SKD_DRVR_STATE_PAUSED:
455 case SKD_DRVR_STATE_STARTING:
456 case SKD_DRVR_STATE_RESTARTING:
457 case SKD_DRVR_STATE_WAIT_BOOT:
458 /* In case of starting, we haven't started the queue,
459 * so we can't get here... but requests are
460 * possibly hanging out waiting for us because we
461 * reported the dev/skd0 already. They'll wait
462 * forever if connect doesn't complete.
463 * What to do??? delay dev/skd0 ??
465 case SKD_DRVR_STATE_BUSY:
466 case SKD_DRVR_STATE_BUSY_IMMINENT:
467 case SKD_DRVR_STATE_BUSY_ERASE:
468 return false;
470 case SKD_DRVR_STATE_BUSY_SANITIZE:
471 case SKD_DRVR_STATE_STOPPING:
472 case SKD_DRVR_STATE_SYNCING:
473 case SKD_DRVR_STATE_FAULT:
474 case SKD_DRVR_STATE_DISAPPEARED:
475 default:
476 return true;
480 static blk_status_t skd_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
481 const struct blk_mq_queue_data *mqd)
483 struct request *const req = mqd->rq;
484 struct request_queue *const q = req->q;
485 struct skd_device *skdev = q->queuedata;
486 struct skd_fitmsg_context *skmsg;
487 struct fit_msg_hdr *fmh;
488 const u32 tag = blk_mq_unique_tag(req);
489 struct skd_request_context *const skreq = blk_mq_rq_to_pdu(req);
490 struct skd_scsi_request *scsi_req;
491 unsigned long flags = 0;
492 const u32 lba = blk_rq_pos(req);
493 const u32 count = blk_rq_sectors(req);
494 const int data_dir = rq_data_dir(req);
496 if (unlikely(skdev->state != SKD_DRVR_STATE_ONLINE))
497 return skd_fail_all(q) ? BLK_STS_IOERR : BLK_STS_RESOURCE;
499 if (!(req->rq_flags & RQF_DONTPREP)) {
500 skreq->retries = 0;
501 req->rq_flags |= RQF_DONTPREP;
504 blk_mq_start_request(req);
506 WARN_ONCE(tag >= skd_max_queue_depth, "%#x > %#x (nr_requests = %lu)\n",
507 tag, skd_max_queue_depth, q->nr_requests);
509 SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
511 dev_dbg(&skdev->pdev->dev,
512 "new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba,
513 lba, count, count, data_dir);
515 skreq->id = tag + SKD_ID_RW_REQUEST;
516 skreq->flush_cmd = 0;
517 skreq->n_sg = 0;
518 skreq->sg_byte_count = 0;
520 skreq->fitmsg_id = 0;
522 skreq->data_dir = data_dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
524 if (req->bio && !skd_preop_sg_list(skdev, skreq)) {
525 dev_dbg(&skdev->pdev->dev, "error Out\n");
526 skreq->status = BLK_STS_RESOURCE;
527 blk_mq_complete_request(req);
528 return BLK_STS_OK;
531 dma_sync_single_for_device(&skdev->pdev->dev, skreq->sksg_dma_address,
532 skreq->n_sg *
533 sizeof(struct fit_sg_descriptor),
534 DMA_TO_DEVICE);
536 /* Either a FIT msg is in progress or we have to start one. */
537 if (skd_max_req_per_msg == 1) {
538 skmsg = NULL;
539 } else {
540 spin_lock_irqsave(&skdev->lock, flags);
541 skmsg = skdev->skmsg;
543 if (!skmsg) {
544 skmsg = &skdev->skmsg_table[tag];
545 skdev->skmsg = skmsg;
547 /* Initialize the FIT msg header */
548 fmh = &skmsg->msg_buf->fmh;
549 memset(fmh, 0, sizeof(*fmh));
550 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
551 skmsg->length = sizeof(*fmh);
552 } else {
553 fmh = &skmsg->msg_buf->fmh;
556 skreq->fitmsg_id = skmsg->id;
558 scsi_req = &skmsg->msg_buf->scsi[fmh->num_protocol_cmds_coalesced];
559 memset(scsi_req, 0, sizeof(*scsi_req));
561 scsi_req->hdr.tag = skreq->id;
562 scsi_req->hdr.sg_list_dma_address =
563 cpu_to_be64(skreq->sksg_dma_address);
565 if (req_op(req) == REQ_OP_FLUSH) {
566 skd_prep_zerosize_flush_cdb(scsi_req, skreq);
567 SKD_ASSERT(skreq->flush_cmd == 1);
568 } else {
569 skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
572 if (req->cmd_flags & REQ_FUA)
573 scsi_req->cdb[1] |= SKD_FUA_NV;
575 scsi_req->hdr.sg_list_len_bytes = cpu_to_be32(skreq->sg_byte_count);
577 /* Complete resource allocations. */
578 skreq->state = SKD_REQ_STATE_BUSY;
580 skmsg->length += sizeof(struct skd_scsi_request);
581 fmh->num_protocol_cmds_coalesced++;
583 dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
584 skd_in_flight(skdev));
587 * If the FIT msg buffer is full send it.
589 if (skd_max_req_per_msg == 1) {
590 skd_send_fitmsg(skdev, skmsg);
591 } else {
592 if (mqd->last ||
593 fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
594 skd_send_fitmsg(skdev, skmsg);
595 skdev->skmsg = NULL;
597 spin_unlock_irqrestore(&skdev->lock, flags);
600 return BLK_STS_OK;
603 static enum blk_eh_timer_return skd_timed_out(struct request *req,
604 bool reserved)
606 struct skd_device *skdev = req->q->queuedata;
608 dev_err(&skdev->pdev->dev, "request with tag %#x timed out\n",
609 blk_mq_unique_tag(req));
611 return BLK_EH_RESET_TIMER;
614 static void skd_complete_rq(struct request *req)
616 struct skd_request_context *skreq = blk_mq_rq_to_pdu(req);
618 blk_mq_end_request(req, skreq->status);
621 static bool skd_preop_sg_list(struct skd_device *skdev,
622 struct skd_request_context *skreq)
624 struct request *req = blk_mq_rq_from_pdu(skreq);
625 struct scatterlist *sgl = &skreq->sg[0], *sg;
626 int n_sg;
627 int i;
629 skreq->sg_byte_count = 0;
631 WARN_ON_ONCE(skreq->data_dir != DMA_TO_DEVICE &&
632 skreq->data_dir != DMA_FROM_DEVICE);
634 n_sg = blk_rq_map_sg(skdev->queue, req, sgl);
635 if (n_sg <= 0)
636 return false;
639 * Map scatterlist to PCI bus addresses.
640 * Note PCI might change the number of entries.
642 n_sg = dma_map_sg(&skdev->pdev->dev, sgl, n_sg, skreq->data_dir);
643 if (n_sg <= 0)
644 return false;
646 SKD_ASSERT(n_sg <= skdev->sgs_per_request);
648 skreq->n_sg = n_sg;
650 for_each_sg(sgl, sg, n_sg, i) {
651 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
652 u32 cnt = sg_dma_len(sg);
653 uint64_t dma_addr = sg_dma_address(sg);
655 sgd->control = FIT_SGD_CONTROL_NOT_LAST;
656 sgd->byte_count = cnt;
657 skreq->sg_byte_count += cnt;
658 sgd->host_side_addr = dma_addr;
659 sgd->dev_side_addr = 0;
662 skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
663 skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;
665 if (unlikely(skdev->dbg_level > 1)) {
666 dev_dbg(&skdev->pdev->dev,
667 "skreq=%x sksg_list=%p sksg_dma=%pad\n",
668 skreq->id, skreq->sksg_list, &skreq->sksg_dma_address);
669 for (i = 0; i < n_sg; i++) {
670 struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
672 dev_dbg(&skdev->pdev->dev,
673 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
674 i, sgd->byte_count, sgd->control,
675 sgd->host_side_addr, sgd->next_desc_ptr);
679 return true;
682 static void skd_postop_sg_list(struct skd_device *skdev,
683 struct skd_request_context *skreq)
686 * restore the next ptr for next IO request so we
687 * don't have to set it every time.
689 skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
690 skreq->sksg_dma_address +
691 ((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
692 dma_unmap_sg(&skdev->pdev->dev, &skreq->sg[0], skreq->n_sg,
693 skreq->data_dir);
697 *****************************************************************************
698 * TIMER
699 *****************************************************************************
702 static void skd_timer_tick_not_online(struct skd_device *skdev);
704 static void skd_start_queue(struct work_struct *work)
706 struct skd_device *skdev = container_of(work, typeof(*skdev),
707 start_queue);
710 * Although it is safe to call blk_start_queue() from interrupt
711 * context, blk_mq_start_hw_queues() must not be called from
712 * interrupt context.
714 blk_mq_start_hw_queues(skdev->queue);
717 static void skd_timer_tick(struct timer_list *t)
719 struct skd_device *skdev = from_timer(skdev, t, timer);
720 unsigned long reqflags;
721 u32 state;
723 if (skdev->state == SKD_DRVR_STATE_FAULT)
724 /* The driver has declared fault, and we want it to
725 * stay that way until driver is reloaded.
727 return;
729 spin_lock_irqsave(&skdev->lock, reqflags);
731 state = SKD_READL(skdev, FIT_STATUS);
732 state &= FIT_SR_DRIVE_STATE_MASK;
733 if (state != skdev->drive_state)
734 skd_isr_fwstate(skdev);
736 if (skdev->state != SKD_DRVR_STATE_ONLINE)
737 skd_timer_tick_not_online(skdev);
739 mod_timer(&skdev->timer, (jiffies + HZ));
741 spin_unlock_irqrestore(&skdev->lock, reqflags);
744 static void skd_timer_tick_not_online(struct skd_device *skdev)
746 switch (skdev->state) {
747 case SKD_DRVR_STATE_IDLE:
748 case SKD_DRVR_STATE_LOAD:
749 break;
750 case SKD_DRVR_STATE_BUSY_SANITIZE:
751 dev_dbg(&skdev->pdev->dev,
752 "drive busy sanitize[%x], driver[%x]\n",
753 skdev->drive_state, skdev->state);
754 /* If we've been in sanitize for 3 seconds, we figure we're not
755 * going to get anymore completions, so recover requests now
757 if (skdev->timer_countdown > 0) {
758 skdev->timer_countdown--;
759 return;
761 skd_recover_requests(skdev);
762 break;
764 case SKD_DRVR_STATE_BUSY:
765 case SKD_DRVR_STATE_BUSY_IMMINENT:
766 case SKD_DRVR_STATE_BUSY_ERASE:
767 dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
768 skdev->state, skdev->timer_countdown);
769 if (skdev->timer_countdown > 0) {
770 skdev->timer_countdown--;
771 return;
773 dev_dbg(&skdev->pdev->dev,
774 "busy[%x], timedout=%d, restarting device.",
775 skdev->state, skdev->timer_countdown);
776 skd_restart_device(skdev);
777 break;
779 case SKD_DRVR_STATE_WAIT_BOOT:
780 case SKD_DRVR_STATE_STARTING:
781 if (skdev->timer_countdown > 0) {
782 skdev->timer_countdown--;
783 return;
785 /* For now, we fault the drive. Could attempt resets to
786 * revcover at some point. */
787 skdev->state = SKD_DRVR_STATE_FAULT;
789 dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
790 skdev->drive_state);
792 /*start the queue so we can respond with error to requests */
793 /* wakeup anyone waiting for startup complete */
794 schedule_work(&skdev->start_queue);
795 skdev->gendisk_on = -1;
796 wake_up_interruptible(&skdev->waitq);
797 break;
799 case SKD_DRVR_STATE_ONLINE:
800 /* shouldn't get here. */
801 break;
803 case SKD_DRVR_STATE_PAUSING:
804 case SKD_DRVR_STATE_PAUSED:
805 break;
807 case SKD_DRVR_STATE_RESTARTING:
808 if (skdev->timer_countdown > 0) {
809 skdev->timer_countdown--;
810 return;
812 /* For now, we fault the drive. Could attempt resets to
813 * revcover at some point. */
814 skdev->state = SKD_DRVR_STATE_FAULT;
815 dev_err(&skdev->pdev->dev,
816 "DriveFault Reconnect Timeout (%x)\n",
817 skdev->drive_state);
820 * Recovering does two things:
821 * 1. completes IO with error
822 * 2. reclaims dma resources
823 * When is it safe to recover requests?
824 * - if the drive state is faulted
825 * - if the state is still soft reset after out timeout
826 * - if the drive registers are dead (state = FF)
827 * If it is "unsafe", we still need to recover, so we will
828 * disable pci bus mastering and disable our interrupts.
831 if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
832 (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
833 (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
834 /* It never came out of soft reset. Try to
835 * recover the requests and then let them
836 * fail. This is to mitigate hung processes. */
837 skd_recover_requests(skdev);
838 else {
839 dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
840 skdev->drive_state);
841 pci_disable_device(skdev->pdev);
842 skd_disable_interrupts(skdev);
843 skd_recover_requests(skdev);
846 /*start the queue so we can respond with error to requests */
847 /* wakeup anyone waiting for startup complete */
848 schedule_work(&skdev->start_queue);
849 skdev->gendisk_on = -1;
850 wake_up_interruptible(&skdev->waitq);
851 break;
853 case SKD_DRVR_STATE_RESUMING:
854 case SKD_DRVR_STATE_STOPPING:
855 case SKD_DRVR_STATE_SYNCING:
856 case SKD_DRVR_STATE_FAULT:
857 case SKD_DRVR_STATE_DISAPPEARED:
858 default:
859 break;
863 static int skd_start_timer(struct skd_device *skdev)
865 int rc;
867 timer_setup(&skdev->timer, skd_timer_tick, 0);
869 rc = mod_timer(&skdev->timer, (jiffies + HZ));
870 if (rc)
871 dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
872 return rc;
875 static void skd_kill_timer(struct skd_device *skdev)
877 del_timer_sync(&skdev->timer);
881 *****************************************************************************
882 * INTERNAL REQUESTS -- generated by driver itself
883 *****************************************************************************
886 static int skd_format_internal_skspcl(struct skd_device *skdev)
888 struct skd_special_context *skspcl = &skdev->internal_skspcl;
889 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
890 struct fit_msg_hdr *fmh;
891 uint64_t dma_address;
892 struct skd_scsi_request *scsi;
894 fmh = &skspcl->msg_buf->fmh;
895 fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
896 fmh->num_protocol_cmds_coalesced = 1;
898 scsi = &skspcl->msg_buf->scsi[0];
899 memset(scsi, 0, sizeof(*scsi));
900 dma_address = skspcl->req.sksg_dma_address;
901 scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
902 skspcl->req.n_sg = 1;
903 sgd->control = FIT_SGD_CONTROL_LAST;
904 sgd->byte_count = 0;
905 sgd->host_side_addr = skspcl->db_dma_address;
906 sgd->dev_side_addr = 0;
907 sgd->next_desc_ptr = 0LL;
909 return 1;
912 #define WR_BUF_SIZE SKD_N_INTERNAL_BYTES
914 static void skd_send_internal_skspcl(struct skd_device *skdev,
915 struct skd_special_context *skspcl,
916 u8 opcode)
918 struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
919 struct skd_scsi_request *scsi;
920 unsigned char *buf = skspcl->data_buf;
921 int i;
923 if (skspcl->req.state != SKD_REQ_STATE_IDLE)
925 * A refresh is already in progress.
926 * Just wait for it to finish.
928 return;
930 skspcl->req.state = SKD_REQ_STATE_BUSY;
932 scsi = &skspcl->msg_buf->scsi[0];
933 scsi->hdr.tag = skspcl->req.id;
935 memset(scsi->cdb, 0, sizeof(scsi->cdb));
937 switch (opcode) {
938 case TEST_UNIT_READY:
939 scsi->cdb[0] = TEST_UNIT_READY;
940 sgd->byte_count = 0;
941 scsi->hdr.sg_list_len_bytes = 0;
942 break;
944 case READ_CAPACITY:
945 scsi->cdb[0] = READ_CAPACITY;
946 sgd->byte_count = SKD_N_READ_CAP_BYTES;
947 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
948 break;
950 case INQUIRY:
951 scsi->cdb[0] = INQUIRY;
952 scsi->cdb[1] = 0x01; /* evpd */
953 scsi->cdb[2] = 0x80; /* serial number page */
954 scsi->cdb[4] = 0x10;
955 sgd->byte_count = 16;
956 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
957 break;
959 case SYNCHRONIZE_CACHE:
960 scsi->cdb[0] = SYNCHRONIZE_CACHE;
961 sgd->byte_count = 0;
962 scsi->hdr.sg_list_len_bytes = 0;
963 break;
965 case WRITE_BUFFER:
966 scsi->cdb[0] = WRITE_BUFFER;
967 scsi->cdb[1] = 0x02;
968 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
969 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
970 sgd->byte_count = WR_BUF_SIZE;
971 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
972 /* fill incrementing byte pattern */
973 for (i = 0; i < sgd->byte_count; i++)
974 buf[i] = i & 0xFF;
975 break;
977 case READ_BUFFER:
978 scsi->cdb[0] = READ_BUFFER;
979 scsi->cdb[1] = 0x02;
980 scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
981 scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
982 sgd->byte_count = WR_BUF_SIZE;
983 scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
984 memset(skspcl->data_buf, 0, sgd->byte_count);
985 break;
987 default:
988 SKD_ASSERT("Don't know what to send");
989 return;
992 skd_send_special_fitmsg(skdev, skspcl);
995 static void skd_refresh_device_data(struct skd_device *skdev)
997 struct skd_special_context *skspcl = &skdev->internal_skspcl;
999 skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
1002 static int skd_chk_read_buf(struct skd_device *skdev,
1003 struct skd_special_context *skspcl)
1005 unsigned char *buf = skspcl->data_buf;
1006 int i;
1008 /* check for incrementing byte pattern */
1009 for (i = 0; i < WR_BUF_SIZE; i++)
1010 if (buf[i] != (i & 0xFF))
1011 return 1;
1013 return 0;
1016 static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
1017 u8 code, u8 qual, u8 fruc)
1019 /* If the check condition is of special interest, log a message */
1020 if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
1021 && (code == 0x04) && (qual == 0x06)) {
1022 dev_err(&skdev->pdev->dev,
1023 "*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1024 key, code, qual, fruc);
1028 static void skd_complete_internal(struct skd_device *skdev,
1029 struct fit_completion_entry_v1 *skcomp,
1030 struct fit_comp_error_info *skerr,
1031 struct skd_special_context *skspcl)
1033 u8 *buf = skspcl->data_buf;
1034 u8 status;
1035 int i;
1036 struct skd_scsi_request *scsi = &skspcl->msg_buf->scsi[0];
1038 lockdep_assert_held(&skdev->lock);
1040 SKD_ASSERT(skspcl == &skdev->internal_skspcl);
1042 dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
1044 dma_sync_single_for_cpu(&skdev->pdev->dev,
1045 skspcl->db_dma_address,
1046 skspcl->req.sksg_list[0].byte_count,
1047 DMA_BIDIRECTIONAL);
1049 skspcl->req.completion = *skcomp;
1050 skspcl->req.state = SKD_REQ_STATE_IDLE;
1052 status = skspcl->req.completion.status;
1054 skd_log_check_status(skdev, status, skerr->key, skerr->code,
1055 skerr->qual, skerr->fruc);
1057 switch (scsi->cdb[0]) {
1058 case TEST_UNIT_READY:
1059 if (status == SAM_STAT_GOOD)
1060 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1061 else if ((status == SAM_STAT_CHECK_CONDITION) &&
1062 (skerr->key == MEDIUM_ERROR))
1063 skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
1064 else {
1065 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1066 dev_dbg(&skdev->pdev->dev,
1067 "TUR failed, don't send anymore state 0x%x\n",
1068 skdev->state);
1069 return;
1071 dev_dbg(&skdev->pdev->dev,
1072 "**** TUR failed, retry skerr\n");
1073 skd_send_internal_skspcl(skdev, skspcl,
1074 TEST_UNIT_READY);
1076 break;
1078 case WRITE_BUFFER:
1079 if (status == SAM_STAT_GOOD)
1080 skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
1081 else {
1082 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1083 dev_dbg(&skdev->pdev->dev,
1084 "write buffer failed, don't send anymore state 0x%x\n",
1085 skdev->state);
1086 return;
1088 dev_dbg(&skdev->pdev->dev,
1089 "**** write buffer failed, retry skerr\n");
1090 skd_send_internal_skspcl(skdev, skspcl,
1091 TEST_UNIT_READY);
1093 break;
1095 case READ_BUFFER:
1096 if (status == SAM_STAT_GOOD) {
1097 if (skd_chk_read_buf(skdev, skspcl) == 0)
1098 skd_send_internal_skspcl(skdev, skspcl,
1099 READ_CAPACITY);
1100 else {
1101 dev_err(&skdev->pdev->dev,
1102 "*** W/R Buffer mismatch %d ***\n",
1103 skdev->connect_retries);
1104 if (skdev->connect_retries <
1105 SKD_MAX_CONNECT_RETRIES) {
1106 skdev->connect_retries++;
1107 skd_soft_reset(skdev);
1108 } else {
1109 dev_err(&skdev->pdev->dev,
1110 "W/R Buffer Connect Error\n");
1111 return;
1115 } else {
1116 if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1117 dev_dbg(&skdev->pdev->dev,
1118 "read buffer failed, don't send anymore state 0x%x\n",
1119 skdev->state);
1120 return;
1122 dev_dbg(&skdev->pdev->dev,
1123 "**** read buffer failed, retry skerr\n");
1124 skd_send_internal_skspcl(skdev, skspcl,
1125 TEST_UNIT_READY);
1127 break;
1129 case READ_CAPACITY:
1130 skdev->read_cap_is_valid = 0;
1131 if (status == SAM_STAT_GOOD) {
1132 skdev->read_cap_last_lba =
1133 (buf[0] << 24) | (buf[1] << 16) |
1134 (buf[2] << 8) | buf[3];
1135 skdev->read_cap_blocksize =
1136 (buf[4] << 24) | (buf[5] << 16) |
1137 (buf[6] << 8) | buf[7];
1139 dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
1140 skdev->read_cap_last_lba,
1141 skdev->read_cap_blocksize);
1143 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1145 skdev->read_cap_is_valid = 1;
1147 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1148 } else if ((status == SAM_STAT_CHECK_CONDITION) &&
1149 (skerr->key == MEDIUM_ERROR)) {
1150 skdev->read_cap_last_lba = ~0;
1151 set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
1152 dev_dbg(&skdev->pdev->dev, "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n");
1153 skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
1154 } else {
1155 dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
1156 skd_send_internal_skspcl(skdev, skspcl,
1157 TEST_UNIT_READY);
1159 break;
1161 case INQUIRY:
1162 skdev->inquiry_is_valid = 0;
1163 if (status == SAM_STAT_GOOD) {
1164 skdev->inquiry_is_valid = 1;
1166 for (i = 0; i < 12; i++)
1167 skdev->inq_serial_num[i] = buf[i + 4];
1168 skdev->inq_serial_num[12] = 0;
1171 if (skd_unquiesce_dev(skdev) < 0)
1172 dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
1173 /* connection is complete */
1174 skdev->connect_retries = 0;
1175 break;
1177 case SYNCHRONIZE_CACHE:
1178 if (status == SAM_STAT_GOOD)
1179 skdev->sync_done = 1;
1180 else
1181 skdev->sync_done = -1;
1182 wake_up_interruptible(&skdev->waitq);
1183 break;
1185 default:
1186 SKD_ASSERT("we didn't send this");
1191 *****************************************************************************
1192 * FIT MESSAGES
1193 *****************************************************************************
1196 static void skd_send_fitmsg(struct skd_device *skdev,
1197 struct skd_fitmsg_context *skmsg)
1199 u64 qcmd;
1201 dev_dbg(&skdev->pdev->dev, "dma address %pad, busy=%d\n",
1202 &skmsg->mb_dma_address, skd_in_flight(skdev));
1203 dev_dbg(&skdev->pdev->dev, "msg_buf %p\n", skmsg->msg_buf);
1205 qcmd = skmsg->mb_dma_address;
1206 qcmd |= FIT_QCMD_QID_NORMAL;
1208 if (unlikely(skdev->dbg_level > 1)) {
1209 u8 *bp = (u8 *)skmsg->msg_buf;
1210 int i;
1211 for (i = 0; i < skmsg->length; i += 8) {
1212 dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
1213 &bp[i]);
1214 if (i == 0)
1215 i = 64 - 8;
1219 if (skmsg->length > 256)
1220 qcmd |= FIT_QCMD_MSGSIZE_512;
1221 else if (skmsg->length > 128)
1222 qcmd |= FIT_QCMD_MSGSIZE_256;
1223 else if (skmsg->length > 64)
1224 qcmd |= FIT_QCMD_MSGSIZE_128;
1225 else
1227 * This makes no sense because the FIT msg header is
1228 * 64 bytes. If the msg is only 64 bytes long it has
1229 * no payload.
1231 qcmd |= FIT_QCMD_MSGSIZE_64;
1233 dma_sync_single_for_device(&skdev->pdev->dev, skmsg->mb_dma_address,
1234 skmsg->length, DMA_TO_DEVICE);
1236 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1237 smp_wmb();
1239 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
1242 static void skd_send_special_fitmsg(struct skd_device *skdev,
1243 struct skd_special_context *skspcl)
1245 u64 qcmd;
1247 WARN_ON_ONCE(skspcl->req.n_sg != 1);
1249 if (unlikely(skdev->dbg_level > 1)) {
1250 u8 *bp = (u8 *)skspcl->msg_buf;
1251 int i;
1253 for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
1254 dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
1255 &bp[i]);
1256 if (i == 0)
1257 i = 64 - 8;
1260 dev_dbg(&skdev->pdev->dev,
1261 "skspcl=%p id=%04x sksg_list=%p sksg_dma=%pad\n",
1262 skspcl, skspcl->req.id, skspcl->req.sksg_list,
1263 &skspcl->req.sksg_dma_address);
1264 for (i = 0; i < skspcl->req.n_sg; i++) {
1265 struct fit_sg_descriptor *sgd =
1266 &skspcl->req.sksg_list[i];
1268 dev_dbg(&skdev->pdev->dev,
1269 " sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
1270 i, sgd->byte_count, sgd->control,
1271 sgd->host_side_addr, sgd->next_desc_ptr);
1276 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
1277 * and one 64-byte SSDI command.
1279 qcmd = skspcl->mb_dma_address;
1280 qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;
1282 dma_sync_single_for_device(&skdev->pdev->dev, skspcl->mb_dma_address,
1283 SKD_N_SPECIAL_FITMSG_BYTES, DMA_TO_DEVICE);
1284 dma_sync_single_for_device(&skdev->pdev->dev,
1285 skspcl->req.sksg_dma_address,
1286 1 * sizeof(struct fit_sg_descriptor),
1287 DMA_TO_DEVICE);
1288 dma_sync_single_for_device(&skdev->pdev->dev,
1289 skspcl->db_dma_address,
1290 skspcl->req.sksg_list[0].byte_count,
1291 DMA_BIDIRECTIONAL);
1293 /* Make sure skd_msg_buf is written before the doorbell is triggered. */
1294 smp_wmb();
1296 SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
1300 *****************************************************************************
1301 * COMPLETION QUEUE
1302 *****************************************************************************
1305 static void skd_complete_other(struct skd_device *skdev,
1306 struct fit_completion_entry_v1 *skcomp,
1307 struct fit_comp_error_info *skerr);
1309 struct sns_info {
1310 u8 type;
1311 u8 stat;
1312 u8 key;
1313 u8 asc;
1314 u8 ascq;
1315 u8 mask;
1316 enum skd_check_status_action action;
1319 static struct sns_info skd_chkstat_table[] = {
1320 /* Good */
1321 { 0x70, 0x02, RECOVERED_ERROR, 0, 0, 0x1c,
1322 SKD_CHECK_STATUS_REPORT_GOOD },
1324 /* Smart alerts */
1325 { 0x70, 0x02, NO_SENSE, 0x0B, 0x00, 0x1E, /* warnings */
1326 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1327 { 0x70, 0x02, NO_SENSE, 0x5D, 0x00, 0x1E, /* thresholds */
1328 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1329 { 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F, /* temperature over trigger */
1330 SKD_CHECK_STATUS_REPORT_SMART_ALERT },
1332 /* Retry (with limits) */
1333 { 0x70, 0x02, 0x0B, 0, 0, 0x1C, /* This one is for DMA ERROR */
1334 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1335 { 0x70, 0x02, 0x06, 0x0B, 0x00, 0x1E, /* warnings */
1336 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1337 { 0x70, 0x02, 0x06, 0x5D, 0x00, 0x1E, /* thresholds */
1338 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1339 { 0x70, 0x02, 0x06, 0x80, 0x30, 0x1F, /* backup power */
1340 SKD_CHECK_STATUS_REQUEUE_REQUEST },
1342 /* Busy (or about to be) */
1343 { 0x70, 0x02, 0x06, 0x3f, 0x01, 0x1F, /* fw changed */
1344 SKD_CHECK_STATUS_BUSY_IMMINENT },
1348 * Look up status and sense data to decide how to handle the error
1349 * from the device.
1350 * mask says which fields must match e.g., mask=0x18 means check
1351 * type and stat, ignore key, asc, ascq.
1354 static enum skd_check_status_action
1355 skd_check_status(struct skd_device *skdev,
1356 u8 cmp_status, struct fit_comp_error_info *skerr)
1358 int i;
1360 dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
1361 skerr->key, skerr->code, skerr->qual, skerr->fruc);
1363 dev_dbg(&skdev->pdev->dev,
1364 "stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
1365 skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
1366 skerr->fruc);
1368 /* Does the info match an entry in the good category? */
1369 for (i = 0; i < ARRAY_SIZE(skd_chkstat_table); i++) {
1370 struct sns_info *sns = &skd_chkstat_table[i];
1372 if (sns->mask & 0x10)
1373 if (skerr->type != sns->type)
1374 continue;
1376 if (sns->mask & 0x08)
1377 if (cmp_status != sns->stat)
1378 continue;
1380 if (sns->mask & 0x04)
1381 if (skerr->key != sns->key)
1382 continue;
1384 if (sns->mask & 0x02)
1385 if (skerr->code != sns->asc)
1386 continue;
1388 if (sns->mask & 0x01)
1389 if (skerr->qual != sns->ascq)
1390 continue;
1392 if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
1393 dev_err(&skdev->pdev->dev,
1394 "SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
1395 skerr->key, skerr->code, skerr->qual);
1397 return sns->action;
1400 /* No other match, so nonzero status means error,
1401 * zero status means good
1403 if (cmp_status) {
1404 dev_dbg(&skdev->pdev->dev, "status check: error\n");
1405 return SKD_CHECK_STATUS_REPORT_ERROR;
1408 dev_dbg(&skdev->pdev->dev, "status check good default\n");
1409 return SKD_CHECK_STATUS_REPORT_GOOD;
1412 static void skd_resolve_req_exception(struct skd_device *skdev,
1413 struct skd_request_context *skreq,
1414 struct request *req)
1416 u8 cmp_status = skreq->completion.status;
1418 switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
1419 case SKD_CHECK_STATUS_REPORT_GOOD:
1420 case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
1421 skreq->status = BLK_STS_OK;
1422 blk_mq_complete_request(req);
1423 break;
1425 case SKD_CHECK_STATUS_BUSY_IMMINENT:
1426 skd_log_skreq(skdev, skreq, "retry(busy)");
1427 blk_mq_requeue_request(req, true);
1428 dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
1429 skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
1430 skdev->timer_countdown = SKD_TIMER_MINUTES(20);
1431 skd_quiesce_dev(skdev);
1432 break;
1434 case SKD_CHECK_STATUS_REQUEUE_REQUEST:
1435 if (++skreq->retries < SKD_MAX_RETRIES) {
1436 skd_log_skreq(skdev, skreq, "retry");
1437 blk_mq_requeue_request(req, true);
1438 break;
1440 /* fall through */
1442 case SKD_CHECK_STATUS_REPORT_ERROR:
1443 default:
1444 skreq->status = BLK_STS_IOERR;
1445 blk_mq_complete_request(req);
1446 break;
1450 static void skd_release_skreq(struct skd_device *skdev,
1451 struct skd_request_context *skreq)
1454 * Reclaim the skd_request_context
1456 skreq->state = SKD_REQ_STATE_IDLE;
1459 static int skd_isr_completion_posted(struct skd_device *skdev,
1460 int limit, int *enqueued)
1462 struct fit_completion_entry_v1 *skcmp;
1463 struct fit_comp_error_info *skerr;
1464 u16 req_id;
1465 u32 tag;
1466 u16 hwq = 0;
1467 struct request *rq;
1468 struct skd_request_context *skreq;
1469 u16 cmp_cntxt;
1470 u8 cmp_status;
1471 u8 cmp_cycle;
1472 u32 cmp_bytes;
1473 int rc = 0;
1474 int processed = 0;
1476 lockdep_assert_held(&skdev->lock);
1478 for (;; ) {
1479 SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);
1481 skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
1482 cmp_cycle = skcmp->cycle;
1483 cmp_cntxt = skcmp->tag;
1484 cmp_status = skcmp->status;
1485 cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);
1487 skerr = &skdev->skerr_table[skdev->skcomp_ix];
1489 dev_dbg(&skdev->pdev->dev,
1490 "cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
1491 skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
1492 cmp_cntxt, cmp_status, skd_in_flight(skdev),
1493 cmp_bytes, skdev->proto_ver);
1495 if (cmp_cycle != skdev->skcomp_cycle) {
1496 dev_dbg(&skdev->pdev->dev, "end of completions\n");
1497 break;
1500 * Update the completion queue head index and possibly
1501 * the completion cycle count. 8-bit wrap-around.
1503 skdev->skcomp_ix++;
1504 if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
1505 skdev->skcomp_ix = 0;
1506 skdev->skcomp_cycle++;
1510 * The command context is a unique 32-bit ID. The low order
1511 * bits help locate the request. The request is usually a
1512 * r/w request (see skd_start() above) or a special request.
1514 req_id = cmp_cntxt;
1515 tag = req_id & SKD_ID_SLOT_AND_TABLE_MASK;
1517 /* Is this other than a r/w request? */
1518 if (tag >= skdev->num_req_context) {
1520 * This is not a completion for a r/w request.
1522 WARN_ON_ONCE(blk_mq_tag_to_rq(skdev->tag_set.tags[hwq],
1523 tag));
1524 skd_complete_other(skdev, skcmp, skerr);
1525 continue;
1528 rq = blk_mq_tag_to_rq(skdev->tag_set.tags[hwq], tag);
1529 if (WARN(!rq, "No request for tag %#x -> %#x\n", cmp_cntxt,
1530 tag))
1531 continue;
1532 skreq = blk_mq_rq_to_pdu(rq);
1535 * Make sure the request ID for the slot matches.
1537 if (skreq->id != req_id) {
1538 dev_err(&skdev->pdev->dev,
1539 "Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
1540 req_id, skreq->id, cmp_cntxt);
1542 continue;
1545 SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);
1547 skreq->completion = *skcmp;
1548 if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
1549 skreq->err_info = *skerr;
1550 skd_log_check_status(skdev, cmp_status, skerr->key,
1551 skerr->code, skerr->qual,
1552 skerr->fruc);
1554 /* Release DMA resources for the request. */
1555 if (skreq->n_sg > 0)
1556 skd_postop_sg_list(skdev, skreq);
1558 skd_release_skreq(skdev, skreq);
1561 * Capture the outcome and post it back to the native request.
1563 if (likely(cmp_status == SAM_STAT_GOOD)) {
1564 skreq->status = BLK_STS_OK;
1565 blk_mq_complete_request(rq);
1566 } else {
1567 skd_resolve_req_exception(skdev, skreq, rq);
1570 /* skd_isr_comp_limit equal zero means no limit */
1571 if (limit) {
1572 if (++processed >= limit) {
1573 rc = 1;
1574 break;
1579 if (skdev->state == SKD_DRVR_STATE_PAUSING &&
1580 skd_in_flight(skdev) == 0) {
1581 skdev->state = SKD_DRVR_STATE_PAUSED;
1582 wake_up_interruptible(&skdev->waitq);
1585 return rc;
1588 static void skd_complete_other(struct skd_device *skdev,
1589 struct fit_completion_entry_v1 *skcomp,
1590 struct fit_comp_error_info *skerr)
1592 u32 req_id = 0;
1593 u32 req_table;
1594 u32 req_slot;
1595 struct skd_special_context *skspcl;
1597 lockdep_assert_held(&skdev->lock);
1599 req_id = skcomp->tag;
1600 req_table = req_id & SKD_ID_TABLE_MASK;
1601 req_slot = req_id & SKD_ID_SLOT_MASK;
1603 dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
1604 req_id, req_slot);
1607 * Based on the request id, determine how to dispatch this completion.
1608 * This swich/case is finding the good cases and forwarding the
1609 * completion entry. Errors are reported below the switch.
1611 switch (req_table) {
1612 case SKD_ID_RW_REQUEST:
1614 * The caller, skd_isr_completion_posted() above,
1615 * handles r/w requests. The only way we get here
1616 * is if the req_slot is out of bounds.
1618 break;
1620 case SKD_ID_INTERNAL:
1621 if (req_slot == 0) {
1622 skspcl = &skdev->internal_skspcl;
1623 if (skspcl->req.id == req_id &&
1624 skspcl->req.state == SKD_REQ_STATE_BUSY) {
1625 skd_complete_internal(skdev,
1626 skcomp, skerr, skspcl);
1627 return;
1630 break;
1632 case SKD_ID_FIT_MSG:
1634 * These id's should never appear in a completion record.
1636 break;
1638 default:
1640 * These id's should never appear anywhere;
1642 break;
1646 * If we get here it is a bad or stale id.
1650 static void skd_reset_skcomp(struct skd_device *skdev)
1652 memset(skdev->skcomp_table, 0, SKD_SKCOMP_SIZE);
1654 skdev->skcomp_ix = 0;
1655 skdev->skcomp_cycle = 1;
1659 *****************************************************************************
1660 * INTERRUPTS
1661 *****************************************************************************
1663 static void skd_completion_worker(struct work_struct *work)
1665 struct skd_device *skdev =
1666 container_of(work, struct skd_device, completion_worker);
1667 unsigned long flags;
1668 int flush_enqueued = 0;
1670 spin_lock_irqsave(&skdev->lock, flags);
1673 * pass in limit=0, which means no limit..
1674 * process everything in compq
1676 skd_isr_completion_posted(skdev, 0, &flush_enqueued);
1677 schedule_work(&skdev->start_queue);
1679 spin_unlock_irqrestore(&skdev->lock, flags);
1682 static void skd_isr_msg_from_dev(struct skd_device *skdev);
1684 static irqreturn_t
1685 skd_isr(int irq, void *ptr)
1687 struct skd_device *skdev = ptr;
1688 u32 intstat;
1689 u32 ack;
1690 int rc = 0;
1691 int deferred = 0;
1692 int flush_enqueued = 0;
1694 spin_lock(&skdev->lock);
1696 for (;; ) {
1697 intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);
1699 ack = FIT_INT_DEF_MASK;
1700 ack &= intstat;
1702 dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
1703 ack);
1705 /* As long as there is an int pending on device, keep
1706 * running loop. When none, get out, but if we've never
1707 * done any processing, call completion handler?
1709 if (ack == 0) {
1710 /* No interrupts on device, but run the completion
1711 * processor anyway?
1713 if (rc == 0)
1714 if (likely (skdev->state
1715 == SKD_DRVR_STATE_ONLINE))
1716 deferred = 1;
1717 break;
1720 rc = IRQ_HANDLED;
1722 SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);
1724 if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
1725 (skdev->state != SKD_DRVR_STATE_STOPPING))) {
1726 if (intstat & FIT_ISH_COMPLETION_POSTED) {
1728 * If we have already deferred completion
1729 * processing, don't bother running it again
1731 if (deferred == 0)
1732 deferred =
1733 skd_isr_completion_posted(skdev,
1734 skd_isr_comp_limit, &flush_enqueued);
1737 if (intstat & FIT_ISH_FW_STATE_CHANGE) {
1738 skd_isr_fwstate(skdev);
1739 if (skdev->state == SKD_DRVR_STATE_FAULT ||
1740 skdev->state ==
1741 SKD_DRVR_STATE_DISAPPEARED) {
1742 spin_unlock(&skdev->lock);
1743 return rc;
1747 if (intstat & FIT_ISH_MSG_FROM_DEV)
1748 skd_isr_msg_from_dev(skdev);
1752 if (unlikely(flush_enqueued))
1753 schedule_work(&skdev->start_queue);
1755 if (deferred)
1756 schedule_work(&skdev->completion_worker);
1757 else if (!flush_enqueued)
1758 schedule_work(&skdev->start_queue);
1760 spin_unlock(&skdev->lock);
1762 return rc;
1765 static void skd_drive_fault(struct skd_device *skdev)
1767 skdev->state = SKD_DRVR_STATE_FAULT;
1768 dev_err(&skdev->pdev->dev, "Drive FAULT\n");
1771 static void skd_drive_disappeared(struct skd_device *skdev)
1773 skdev->state = SKD_DRVR_STATE_DISAPPEARED;
1774 dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
1777 static void skd_isr_fwstate(struct skd_device *skdev)
1779 u32 sense;
1780 u32 state;
1781 u32 mtd;
1782 int prev_driver_state = skdev->state;
1784 sense = SKD_READL(skdev, FIT_STATUS);
1785 state = sense & FIT_SR_DRIVE_STATE_MASK;
1787 dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
1788 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
1789 skd_drive_state_to_str(state), state);
1791 skdev->drive_state = state;
1793 switch (skdev->drive_state) {
1794 case FIT_SR_DRIVE_INIT:
1795 if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
1796 skd_disable_interrupts(skdev);
1797 break;
1799 if (skdev->state == SKD_DRVR_STATE_RESTARTING)
1800 skd_recover_requests(skdev);
1801 if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
1802 skdev->timer_countdown = SKD_STARTING_TIMO;
1803 skdev->state = SKD_DRVR_STATE_STARTING;
1804 skd_soft_reset(skdev);
1805 break;
1807 mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
1808 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1809 skdev->last_mtd = mtd;
1810 break;
1812 case FIT_SR_DRIVE_ONLINE:
1813 skdev->cur_max_queue_depth = skd_max_queue_depth;
1814 if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
1815 skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;
1817 skdev->queue_low_water_mark =
1818 skdev->cur_max_queue_depth * 2 / 3 + 1;
1819 if (skdev->queue_low_water_mark < 1)
1820 skdev->queue_low_water_mark = 1;
1821 dev_info(&skdev->pdev->dev,
1822 "Queue depth limit=%d dev=%d lowat=%d\n",
1823 skdev->cur_max_queue_depth,
1824 skdev->dev_max_queue_depth,
1825 skdev->queue_low_water_mark);
1827 skd_refresh_device_data(skdev);
1828 break;
1830 case FIT_SR_DRIVE_BUSY:
1831 skdev->state = SKD_DRVR_STATE_BUSY;
1832 skdev->timer_countdown = SKD_BUSY_TIMO;
1833 skd_quiesce_dev(skdev);
1834 break;
1835 case FIT_SR_DRIVE_BUSY_SANITIZE:
1836 /* set timer for 3 seconds, we'll abort any unfinished
1837 * commands after that expires
1839 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
1840 skdev->timer_countdown = SKD_TIMER_SECONDS(3);
1841 schedule_work(&skdev->start_queue);
1842 break;
1843 case FIT_SR_DRIVE_BUSY_ERASE:
1844 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
1845 skdev->timer_countdown = SKD_BUSY_TIMO;
1846 break;
1847 case FIT_SR_DRIVE_OFFLINE:
1848 skdev->state = SKD_DRVR_STATE_IDLE;
1849 break;
1850 case FIT_SR_DRIVE_SOFT_RESET:
1851 switch (skdev->state) {
1852 case SKD_DRVR_STATE_STARTING:
1853 case SKD_DRVR_STATE_RESTARTING:
1854 /* Expected by a caller of skd_soft_reset() */
1855 break;
1856 default:
1857 skdev->state = SKD_DRVR_STATE_RESTARTING;
1858 break;
1860 break;
1861 case FIT_SR_DRIVE_FW_BOOTING:
1862 dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
1863 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
1864 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
1865 break;
1867 case FIT_SR_DRIVE_DEGRADED:
1868 case FIT_SR_PCIE_LINK_DOWN:
1869 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
1870 break;
1872 case FIT_SR_DRIVE_FAULT:
1873 skd_drive_fault(skdev);
1874 skd_recover_requests(skdev);
1875 schedule_work(&skdev->start_queue);
1876 break;
1878 /* PCIe bus returned all Fs? */
1879 case 0xFF:
1880 dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
1881 sense);
1882 skd_drive_disappeared(skdev);
1883 skd_recover_requests(skdev);
1884 schedule_work(&skdev->start_queue);
1885 break;
1886 default:
1888 * Uknown FW State. Wait for a state we recognize.
1890 break;
1892 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
1893 skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
1894 skd_skdev_state_to_str(skdev->state), skdev->state);
1897 static bool skd_recover_request(struct request *req, void *data, bool reserved)
1899 struct skd_device *const skdev = data;
1900 struct skd_request_context *skreq = blk_mq_rq_to_pdu(req);
1902 if (skreq->state != SKD_REQ_STATE_BUSY)
1903 return true;
1905 skd_log_skreq(skdev, skreq, "recover");
1907 /* Release DMA resources for the request. */
1908 if (skreq->n_sg > 0)
1909 skd_postop_sg_list(skdev, skreq);
1911 skreq->state = SKD_REQ_STATE_IDLE;
1912 skreq->status = BLK_STS_IOERR;
1913 blk_mq_complete_request(req);
1914 return true;
1917 static void skd_recover_requests(struct skd_device *skdev)
1919 blk_mq_tagset_busy_iter(&skdev->tag_set, skd_recover_request, skdev);
1922 static void skd_isr_msg_from_dev(struct skd_device *skdev)
1924 u32 mfd;
1925 u32 mtd;
1926 u32 data;
1928 mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
1930 dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
1931 skdev->last_mtd);
1933 /* ignore any mtd that is an ack for something we didn't send */
1934 if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
1935 return;
1937 switch (FIT_MXD_TYPE(mfd)) {
1938 case FIT_MTD_FITFW_INIT:
1939 skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);
1941 if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
1942 dev_err(&skdev->pdev->dev, "protocol mismatch\n");
1943 dev_err(&skdev->pdev->dev, " got=%d support=%d\n",
1944 skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
1945 dev_err(&skdev->pdev->dev, " please upgrade driver\n");
1946 skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
1947 skd_soft_reset(skdev);
1948 break;
1950 mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
1951 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1952 skdev->last_mtd = mtd;
1953 break;
1955 case FIT_MTD_GET_CMDQ_DEPTH:
1956 skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
1957 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
1958 SKD_N_COMPLETION_ENTRY);
1959 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1960 skdev->last_mtd = mtd;
1961 break;
1963 case FIT_MTD_SET_COMPQ_DEPTH:
1964 SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
1965 mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
1966 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1967 skdev->last_mtd = mtd;
1968 break;
1970 case FIT_MTD_SET_COMPQ_ADDR:
1971 skd_reset_skcomp(skdev);
1972 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
1973 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1974 skdev->last_mtd = mtd;
1975 break;
1977 case FIT_MTD_CMD_LOG_HOST_ID:
1978 /* hardware interface overflows in y2106 */
1979 skdev->connect_time_stamp = (u32)ktime_get_real_seconds();
1980 data = skdev->connect_time_stamp & 0xFFFF;
1981 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
1982 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1983 skdev->last_mtd = mtd;
1984 break;
1986 case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
1987 skdev->drive_jiffies = FIT_MXD_DATA(mfd);
1988 data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
1989 mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
1990 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1991 skdev->last_mtd = mtd;
1992 break;
1994 case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
1995 skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
1996 mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
1997 SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
1998 skdev->last_mtd = mtd;
2000 dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
2001 skdev->connect_time_stamp, skdev->drive_jiffies);
2002 break;
2004 case FIT_MTD_ARM_QUEUE:
2005 skdev->last_mtd = 0;
2007 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
2009 break;
2011 default:
2012 break;
2016 static void skd_disable_interrupts(struct skd_device *skdev)
2018 u32 sense;
2020 sense = SKD_READL(skdev, FIT_CONTROL);
2021 sense &= ~FIT_CR_ENABLE_INTERRUPTS;
2022 SKD_WRITEL(skdev, sense, FIT_CONTROL);
2023 dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
2025 /* Note that the 1s is written. A 1-bit means
2026 * disable, a 0 means enable.
2028 SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
2031 static void skd_enable_interrupts(struct skd_device *skdev)
2033 u32 val;
2035 /* unmask interrupts first */
2036 val = FIT_ISH_FW_STATE_CHANGE +
2037 FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;
2039 /* Note that the compliment of mask is written. A 1-bit means
2040 * disable, a 0 means enable. */
2041 SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
2042 dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
2044 val = SKD_READL(skdev, FIT_CONTROL);
2045 val |= FIT_CR_ENABLE_INTERRUPTS;
2046 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
2047 SKD_WRITEL(skdev, val, FIT_CONTROL);
2051 *****************************************************************************
2052 * START, STOP, RESTART, QUIESCE, UNQUIESCE
2053 *****************************************************************************
2056 static void skd_soft_reset(struct skd_device *skdev)
2058 u32 val;
2060 val = SKD_READL(skdev, FIT_CONTROL);
2061 val |= (FIT_CR_SOFT_RESET);
2062 dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
2063 SKD_WRITEL(skdev, val, FIT_CONTROL);
2066 static void skd_start_device(struct skd_device *skdev)
2068 unsigned long flags;
2069 u32 sense;
2070 u32 state;
2072 spin_lock_irqsave(&skdev->lock, flags);
2074 /* ack all ghost interrupts */
2075 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2077 sense = SKD_READL(skdev, FIT_STATUS);
2079 dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
2081 state = sense & FIT_SR_DRIVE_STATE_MASK;
2082 skdev->drive_state = state;
2083 skdev->last_mtd = 0;
2085 skdev->state = SKD_DRVR_STATE_STARTING;
2086 skdev->timer_countdown = SKD_STARTING_TIMO;
2088 skd_enable_interrupts(skdev);
2090 switch (skdev->drive_state) {
2091 case FIT_SR_DRIVE_OFFLINE:
2092 dev_err(&skdev->pdev->dev, "Drive offline...\n");
2093 break;
2095 case FIT_SR_DRIVE_FW_BOOTING:
2096 dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
2097 skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
2098 skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
2099 break;
2101 case FIT_SR_DRIVE_BUSY_SANITIZE:
2102 dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
2103 skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
2104 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2105 break;
2107 case FIT_SR_DRIVE_BUSY_ERASE:
2108 dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
2109 skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
2110 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2111 break;
2113 case FIT_SR_DRIVE_INIT:
2114 case FIT_SR_DRIVE_ONLINE:
2115 skd_soft_reset(skdev);
2116 break;
2118 case FIT_SR_DRIVE_BUSY:
2119 dev_err(&skdev->pdev->dev, "Drive Busy...\n");
2120 skdev->state = SKD_DRVR_STATE_BUSY;
2121 skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
2122 break;
2124 case FIT_SR_DRIVE_SOFT_RESET:
2125 dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
2126 break;
2128 case FIT_SR_DRIVE_FAULT:
2129 /* Fault state is bad...soft reset won't do it...
2130 * Hard reset, maybe, but does it work on device?
2131 * For now, just fault so the system doesn't hang.
2133 skd_drive_fault(skdev);
2134 /*start the queue so we can respond with error to requests */
2135 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2136 schedule_work(&skdev->start_queue);
2137 skdev->gendisk_on = -1;
2138 wake_up_interruptible(&skdev->waitq);
2139 break;
2141 case 0xFF:
2142 /* Most likely the device isn't there or isn't responding
2143 * to the BAR1 addresses. */
2144 skd_drive_disappeared(skdev);
2145 /*start the queue so we can respond with error to requests */
2146 dev_dbg(&skdev->pdev->dev,
2147 "starting queue to error-out reqs\n");
2148 schedule_work(&skdev->start_queue);
2149 skdev->gendisk_on = -1;
2150 wake_up_interruptible(&skdev->waitq);
2151 break;
2153 default:
2154 dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
2155 skdev->drive_state);
2156 break;
2159 state = SKD_READL(skdev, FIT_CONTROL);
2160 dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
2162 state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
2163 dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
2165 state = SKD_READL(skdev, FIT_INT_MASK_HOST);
2166 dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
2168 state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
2169 dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
2171 state = SKD_READL(skdev, FIT_HW_VERSION);
2172 dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
2174 spin_unlock_irqrestore(&skdev->lock, flags);
2177 static void skd_stop_device(struct skd_device *skdev)
2179 unsigned long flags;
2180 struct skd_special_context *skspcl = &skdev->internal_skspcl;
2181 u32 dev_state;
2182 int i;
2184 spin_lock_irqsave(&skdev->lock, flags);
2186 if (skdev->state != SKD_DRVR_STATE_ONLINE) {
2187 dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
2188 goto stop_out;
2191 if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
2192 dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
2193 goto stop_out;
2196 skdev->state = SKD_DRVR_STATE_SYNCING;
2197 skdev->sync_done = 0;
2199 skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);
2201 spin_unlock_irqrestore(&skdev->lock, flags);
2203 wait_event_interruptible_timeout(skdev->waitq,
2204 (skdev->sync_done), (10 * HZ));
2206 spin_lock_irqsave(&skdev->lock, flags);
2208 switch (skdev->sync_done) {
2209 case 0:
2210 dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
2211 break;
2212 case 1:
2213 dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
2214 break;
2215 default:
2216 dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
2219 stop_out:
2220 skdev->state = SKD_DRVR_STATE_STOPPING;
2221 spin_unlock_irqrestore(&skdev->lock, flags);
2223 skd_kill_timer(skdev);
2225 spin_lock_irqsave(&skdev->lock, flags);
2226 skd_disable_interrupts(skdev);
2228 /* ensure all ints on device are cleared */
2229 /* soft reset the device to unload with a clean slate */
2230 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2231 SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);
2233 spin_unlock_irqrestore(&skdev->lock, flags);
2235 /* poll every 100ms, 1 second timeout */
2236 for (i = 0; i < 10; i++) {
2237 dev_state =
2238 SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
2239 if (dev_state == FIT_SR_DRIVE_INIT)
2240 break;
2241 set_current_state(TASK_INTERRUPTIBLE);
2242 schedule_timeout(msecs_to_jiffies(100));
2245 if (dev_state != FIT_SR_DRIVE_INIT)
2246 dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
2247 dev_state);
2250 /* assume spinlock is held */
2251 static void skd_restart_device(struct skd_device *skdev)
2253 u32 state;
2255 /* ack all ghost interrupts */
2256 SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
2258 state = SKD_READL(skdev, FIT_STATUS);
2260 dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
2262 state &= FIT_SR_DRIVE_STATE_MASK;
2263 skdev->drive_state = state;
2264 skdev->last_mtd = 0;
2266 skdev->state = SKD_DRVR_STATE_RESTARTING;
2267 skdev->timer_countdown = SKD_RESTARTING_TIMO;
2269 skd_soft_reset(skdev);
2272 /* assume spinlock is held */
2273 static int skd_quiesce_dev(struct skd_device *skdev)
2275 int rc = 0;
2277 switch (skdev->state) {
2278 case SKD_DRVR_STATE_BUSY:
2279 case SKD_DRVR_STATE_BUSY_IMMINENT:
2280 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
2281 blk_mq_stop_hw_queues(skdev->queue);
2282 break;
2283 case SKD_DRVR_STATE_ONLINE:
2284 case SKD_DRVR_STATE_STOPPING:
2285 case SKD_DRVR_STATE_SYNCING:
2286 case SKD_DRVR_STATE_PAUSING:
2287 case SKD_DRVR_STATE_PAUSED:
2288 case SKD_DRVR_STATE_STARTING:
2289 case SKD_DRVR_STATE_RESTARTING:
2290 case SKD_DRVR_STATE_RESUMING:
2291 default:
2292 rc = -EINVAL;
2293 dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
2294 skdev->state);
2296 return rc;
2299 /* assume spinlock is held */
2300 static int skd_unquiesce_dev(struct skd_device *skdev)
2302 int prev_driver_state = skdev->state;
2304 skd_log_skdev(skdev, "unquiesce");
2305 if (skdev->state == SKD_DRVR_STATE_ONLINE) {
2306 dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
2307 return 0;
2309 if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
2311 * If there has been an state change to other than
2312 * ONLINE, we will rely on controller state change
2313 * to come back online and restart the queue.
2314 * The BUSY state means that driver is ready to
2315 * continue normal processing but waiting for controller
2316 * to become available.
2318 skdev->state = SKD_DRVR_STATE_BUSY;
2319 dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
2320 return 0;
2324 * Drive has just come online, driver is either in startup,
2325 * paused performing a task, or bust waiting for hardware.
2327 switch (skdev->state) {
2328 case SKD_DRVR_STATE_PAUSED:
2329 case SKD_DRVR_STATE_BUSY:
2330 case SKD_DRVR_STATE_BUSY_IMMINENT:
2331 case SKD_DRVR_STATE_BUSY_ERASE:
2332 case SKD_DRVR_STATE_STARTING:
2333 case SKD_DRVR_STATE_RESTARTING:
2334 case SKD_DRVR_STATE_FAULT:
2335 case SKD_DRVR_STATE_IDLE:
2336 case SKD_DRVR_STATE_LOAD:
2337 skdev->state = SKD_DRVR_STATE_ONLINE;
2338 dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
2339 skd_skdev_state_to_str(prev_driver_state),
2340 prev_driver_state, skd_skdev_state_to_str(skdev->state),
2341 skdev->state);
2342 dev_dbg(&skdev->pdev->dev,
2343 "**** device ONLINE...starting block queue\n");
2344 dev_dbg(&skdev->pdev->dev, "starting queue\n");
2345 dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
2346 schedule_work(&skdev->start_queue);
2347 skdev->gendisk_on = 1;
2348 wake_up_interruptible(&skdev->waitq);
2349 break;
2351 case SKD_DRVR_STATE_DISAPPEARED:
2352 default:
2353 dev_dbg(&skdev->pdev->dev,
2354 "**** driver state %d, not implemented\n",
2355 skdev->state);
2356 return -EBUSY;
2358 return 0;
2362 *****************************************************************************
2363 * PCIe MSI/MSI-X INTERRUPT HANDLERS
2364 *****************************************************************************
2367 static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
2369 struct skd_device *skdev = skd_host_data;
2370 unsigned long flags;
2372 spin_lock_irqsave(&skdev->lock, flags);
2373 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2374 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2375 dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
2376 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2377 SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
2378 spin_unlock_irqrestore(&skdev->lock, flags);
2379 return IRQ_HANDLED;
2382 static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
2384 struct skd_device *skdev = skd_host_data;
2385 unsigned long flags;
2387 spin_lock_irqsave(&skdev->lock, flags);
2388 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2389 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2390 SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
2391 skd_isr_fwstate(skdev);
2392 spin_unlock_irqrestore(&skdev->lock, flags);
2393 return IRQ_HANDLED;
2396 static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
2398 struct skd_device *skdev = skd_host_data;
2399 unsigned long flags;
2400 int flush_enqueued = 0;
2401 int deferred;
2403 spin_lock_irqsave(&skdev->lock, flags);
2404 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2405 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2406 SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
2407 deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
2408 &flush_enqueued);
2409 if (flush_enqueued)
2410 schedule_work(&skdev->start_queue);
2412 if (deferred)
2413 schedule_work(&skdev->completion_worker);
2414 else if (!flush_enqueued)
2415 schedule_work(&skdev->start_queue);
2417 spin_unlock_irqrestore(&skdev->lock, flags);
2419 return IRQ_HANDLED;
2422 static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
2424 struct skd_device *skdev = skd_host_data;
2425 unsigned long flags;
2427 spin_lock_irqsave(&skdev->lock, flags);
2428 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2429 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2430 SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
2431 skd_isr_msg_from_dev(skdev);
2432 spin_unlock_irqrestore(&skdev->lock, flags);
2433 return IRQ_HANDLED;
2436 static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
2438 struct skd_device *skdev = skd_host_data;
2439 unsigned long flags;
2441 spin_lock_irqsave(&skdev->lock, flags);
2442 dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
2443 SKD_READL(skdev, FIT_INT_STATUS_HOST));
2444 SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
2445 spin_unlock_irqrestore(&skdev->lock, flags);
2446 return IRQ_HANDLED;
2450 *****************************************************************************
2451 * PCIe MSI/MSI-X SETUP
2452 *****************************************************************************
2455 struct skd_msix_entry {
2456 char isr_name[30];
2459 struct skd_init_msix_entry {
2460 const char *name;
2461 irq_handler_t handler;
2464 #define SKD_MAX_MSIX_COUNT 13
2465 #define SKD_MIN_MSIX_COUNT 7
2466 #define SKD_BASE_MSIX_IRQ 4
2468 static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
2469 { "(DMA 0)", skd_reserved_isr },
2470 { "(DMA 1)", skd_reserved_isr },
2471 { "(DMA 2)", skd_reserved_isr },
2472 { "(DMA 3)", skd_reserved_isr },
2473 { "(State Change)", skd_statec_isr },
2474 { "(COMPL_Q)", skd_comp_q },
2475 { "(MSG)", skd_msg_isr },
2476 { "(Reserved)", skd_reserved_isr },
2477 { "(Reserved)", skd_reserved_isr },
2478 { "(Queue Full 0)", skd_qfull_isr },
2479 { "(Queue Full 1)", skd_qfull_isr },
2480 { "(Queue Full 2)", skd_qfull_isr },
2481 { "(Queue Full 3)", skd_qfull_isr },
2484 static int skd_acquire_msix(struct skd_device *skdev)
2486 int i, rc;
2487 struct pci_dev *pdev = skdev->pdev;
2489 rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
2490 PCI_IRQ_MSIX);
2491 if (rc < 0) {
2492 dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
2493 goto out;
2496 skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
2497 sizeof(struct skd_msix_entry), GFP_KERNEL);
2498 if (!skdev->msix_entries) {
2499 rc = -ENOMEM;
2500 dev_err(&skdev->pdev->dev, "msix table allocation error\n");
2501 goto out;
2504 /* Enable MSI-X vectors for the base queue */
2505 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2506 struct skd_msix_entry *qentry = &skdev->msix_entries[i];
2508 snprintf(qentry->isr_name, sizeof(qentry->isr_name),
2509 "%s%d-msix %s", DRV_NAME, skdev->devno,
2510 msix_entries[i].name);
2512 rc = devm_request_irq(&skdev->pdev->dev,
2513 pci_irq_vector(skdev->pdev, i),
2514 msix_entries[i].handler, 0,
2515 qentry->isr_name, skdev);
2516 if (rc) {
2517 dev_err(&skdev->pdev->dev,
2518 "Unable to register(%d) MSI-X handler %d: %s\n",
2519 rc, i, qentry->isr_name);
2520 goto msix_out;
2524 dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
2525 SKD_MAX_MSIX_COUNT);
2526 return 0;
2528 msix_out:
2529 while (--i >= 0)
2530 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
2531 out:
2532 kfree(skdev->msix_entries);
2533 skdev->msix_entries = NULL;
2534 return rc;
2537 static int skd_acquire_irq(struct skd_device *skdev)
2539 struct pci_dev *pdev = skdev->pdev;
2540 unsigned int irq_flag = PCI_IRQ_LEGACY;
2541 int rc;
2543 if (skd_isr_type == SKD_IRQ_MSIX) {
2544 rc = skd_acquire_msix(skdev);
2545 if (!rc)
2546 return 0;
2548 dev_err(&skdev->pdev->dev,
2549 "failed to enable MSI-X, re-trying with MSI %d\n", rc);
2552 snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
2553 skdev->devno);
2555 if (skd_isr_type != SKD_IRQ_LEGACY)
2556 irq_flag |= PCI_IRQ_MSI;
2557 rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
2558 if (rc < 0) {
2559 dev_err(&skdev->pdev->dev,
2560 "failed to allocate the MSI interrupt %d\n", rc);
2561 return rc;
2564 rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
2565 pdev->msi_enabled ? 0 : IRQF_SHARED,
2566 skdev->isr_name, skdev);
2567 if (rc) {
2568 pci_free_irq_vectors(pdev);
2569 dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
2570 rc);
2571 return rc;
2574 return 0;
2577 static void skd_release_irq(struct skd_device *skdev)
2579 struct pci_dev *pdev = skdev->pdev;
2581 if (skdev->msix_entries) {
2582 int i;
2584 for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
2585 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
2586 skdev);
2589 kfree(skdev->msix_entries);
2590 skdev->msix_entries = NULL;
2591 } else {
2592 devm_free_irq(&pdev->dev, pdev->irq, skdev);
2595 pci_free_irq_vectors(pdev);
2599 *****************************************************************************
2600 * CONSTRUCT
2601 *****************************************************************************
2604 static void *skd_alloc_dma(struct skd_device *skdev, struct kmem_cache *s,
2605 dma_addr_t *dma_handle, gfp_t gfp,
2606 enum dma_data_direction dir)
2608 struct device *dev = &skdev->pdev->dev;
2609 void *buf;
2611 buf = kmem_cache_alloc(s, gfp);
2612 if (!buf)
2613 return NULL;
2614 *dma_handle = dma_map_single(dev, buf,
2615 kmem_cache_size(s), dir);
2616 if (dma_mapping_error(dev, *dma_handle)) {
2617 kmem_cache_free(s, buf);
2618 buf = NULL;
2620 return buf;
2623 static void skd_free_dma(struct skd_device *skdev, struct kmem_cache *s,
2624 void *vaddr, dma_addr_t dma_handle,
2625 enum dma_data_direction dir)
2627 if (!vaddr)
2628 return;
2630 dma_unmap_single(&skdev->pdev->dev, dma_handle,
2631 kmem_cache_size(s), dir);
2632 kmem_cache_free(s, vaddr);
2635 static int skd_cons_skcomp(struct skd_device *skdev)
2637 int rc = 0;
2638 struct fit_completion_entry_v1 *skcomp;
2640 dev_dbg(&skdev->pdev->dev,
2641 "comp pci_alloc, total bytes %zd entries %d\n",
2642 SKD_SKCOMP_SIZE, SKD_N_COMPLETION_ENTRY);
2644 skcomp = dma_alloc_coherent(&skdev->pdev->dev, SKD_SKCOMP_SIZE,
2645 &skdev->cq_dma_address, GFP_KERNEL);
2647 if (skcomp == NULL) {
2648 rc = -ENOMEM;
2649 goto err_out;
2652 skdev->skcomp_table = skcomp;
2653 skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
2654 sizeof(*skcomp) *
2655 SKD_N_COMPLETION_ENTRY);
2657 err_out:
2658 return rc;
2661 static int skd_cons_skmsg(struct skd_device *skdev)
2663 int rc = 0;
2664 u32 i;
2666 dev_dbg(&skdev->pdev->dev,
2667 "skmsg_table kcalloc, struct %lu, count %u total %lu\n",
2668 sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
2669 sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
2671 skdev->skmsg_table = kcalloc(skdev->num_fitmsg_context,
2672 sizeof(struct skd_fitmsg_context),
2673 GFP_KERNEL);
2674 if (skdev->skmsg_table == NULL) {
2675 rc = -ENOMEM;
2676 goto err_out;
2679 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2680 struct skd_fitmsg_context *skmsg;
2682 skmsg = &skdev->skmsg_table[i];
2684 skmsg->id = i + SKD_ID_FIT_MSG;
2686 skmsg->msg_buf = dma_alloc_coherent(&skdev->pdev->dev,
2687 SKD_N_FITMSG_BYTES,
2688 &skmsg->mb_dma_address,
2689 GFP_KERNEL);
2690 if (skmsg->msg_buf == NULL) {
2691 rc = -ENOMEM;
2692 goto err_out;
2695 WARN(((uintptr_t)skmsg->msg_buf | skmsg->mb_dma_address) &
2696 (FIT_QCMD_ALIGN - 1),
2697 "not aligned: msg_buf %p mb_dma_address %pad\n",
2698 skmsg->msg_buf, &skmsg->mb_dma_address);
2699 memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);
2702 err_out:
2703 return rc;
2706 static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
2707 u32 n_sg,
2708 dma_addr_t *ret_dma_addr)
2710 struct fit_sg_descriptor *sg_list;
2712 sg_list = skd_alloc_dma(skdev, skdev->sglist_cache, ret_dma_addr,
2713 GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE);
2715 if (sg_list != NULL) {
2716 uint64_t dma_address = *ret_dma_addr;
2717 u32 i;
2719 for (i = 0; i < n_sg - 1; i++) {
2720 uint64_t ndp_off;
2721 ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);
2723 sg_list[i].next_desc_ptr = dma_address + ndp_off;
2725 sg_list[i].next_desc_ptr = 0LL;
2728 return sg_list;
2731 static void skd_free_sg_list(struct skd_device *skdev,
2732 struct fit_sg_descriptor *sg_list,
2733 dma_addr_t dma_addr)
2735 if (WARN_ON_ONCE(!sg_list))
2736 return;
2738 skd_free_dma(skdev, skdev->sglist_cache, sg_list, dma_addr,
2739 DMA_TO_DEVICE);
2742 static int skd_init_request(struct blk_mq_tag_set *set, struct request *rq,
2743 unsigned int hctx_idx, unsigned int numa_node)
2745 struct skd_device *skdev = set->driver_data;
2746 struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq);
2748 skreq->state = SKD_REQ_STATE_IDLE;
2749 skreq->sg = (void *)(skreq + 1);
2750 sg_init_table(skreq->sg, skd_sgs_per_request);
2751 skreq->sksg_list = skd_cons_sg_list(skdev, skd_sgs_per_request,
2752 &skreq->sksg_dma_address);
2754 return skreq->sksg_list ? 0 : -ENOMEM;
2757 static void skd_exit_request(struct blk_mq_tag_set *set, struct request *rq,
2758 unsigned int hctx_idx)
2760 struct skd_device *skdev = set->driver_data;
2761 struct skd_request_context *skreq = blk_mq_rq_to_pdu(rq);
2763 skd_free_sg_list(skdev, skreq->sksg_list, skreq->sksg_dma_address);
2766 static int skd_cons_sksb(struct skd_device *skdev)
2768 int rc = 0;
2769 struct skd_special_context *skspcl;
2771 skspcl = &skdev->internal_skspcl;
2773 skspcl->req.id = 0 + SKD_ID_INTERNAL;
2774 skspcl->req.state = SKD_REQ_STATE_IDLE;
2776 skspcl->data_buf = skd_alloc_dma(skdev, skdev->databuf_cache,
2777 &skspcl->db_dma_address,
2778 GFP_DMA | __GFP_ZERO,
2779 DMA_BIDIRECTIONAL);
2780 if (skspcl->data_buf == NULL) {
2781 rc = -ENOMEM;
2782 goto err_out;
2785 skspcl->msg_buf = skd_alloc_dma(skdev, skdev->msgbuf_cache,
2786 &skspcl->mb_dma_address,
2787 GFP_DMA | __GFP_ZERO, DMA_TO_DEVICE);
2788 if (skspcl->msg_buf == NULL) {
2789 rc = -ENOMEM;
2790 goto err_out;
2793 skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
2794 &skspcl->req.sksg_dma_address);
2795 if (skspcl->req.sksg_list == NULL) {
2796 rc = -ENOMEM;
2797 goto err_out;
2800 if (!skd_format_internal_skspcl(skdev)) {
2801 rc = -EINVAL;
2802 goto err_out;
2805 err_out:
2806 return rc;
2809 static const struct blk_mq_ops skd_mq_ops = {
2810 .queue_rq = skd_mq_queue_rq,
2811 .complete = skd_complete_rq,
2812 .timeout = skd_timed_out,
2813 .init_request = skd_init_request,
2814 .exit_request = skd_exit_request,
2817 static int skd_cons_disk(struct skd_device *skdev)
2819 int rc = 0;
2820 struct gendisk *disk;
2821 struct request_queue *q;
2822 unsigned long flags;
2824 disk = alloc_disk(SKD_MINORS_PER_DEVICE);
2825 if (!disk) {
2826 rc = -ENOMEM;
2827 goto err_out;
2830 skdev->disk = disk;
2831 sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);
2833 disk->major = skdev->major;
2834 disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
2835 disk->fops = &skd_blockdev_ops;
2836 disk->private_data = skdev;
2838 memset(&skdev->tag_set, 0, sizeof(skdev->tag_set));
2839 skdev->tag_set.ops = &skd_mq_ops;
2840 skdev->tag_set.nr_hw_queues = 1;
2841 skdev->tag_set.queue_depth = skd_max_queue_depth;
2842 skdev->tag_set.cmd_size = sizeof(struct skd_request_context) +
2843 skdev->sgs_per_request * sizeof(struct scatterlist);
2844 skdev->tag_set.numa_node = NUMA_NO_NODE;
2845 skdev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
2846 BLK_ALLOC_POLICY_TO_MQ_FLAG(BLK_TAG_ALLOC_FIFO);
2847 skdev->tag_set.driver_data = skdev;
2848 rc = blk_mq_alloc_tag_set(&skdev->tag_set);
2849 if (rc)
2850 goto err_out;
2851 q = blk_mq_init_queue(&skdev->tag_set);
2852 if (IS_ERR(q)) {
2853 blk_mq_free_tag_set(&skdev->tag_set);
2854 rc = PTR_ERR(q);
2855 goto err_out;
2857 q->queuedata = skdev;
2859 skdev->queue = q;
2860 disk->queue = q;
2862 blk_queue_write_cache(q, true, true);
2863 blk_queue_max_segments(q, skdev->sgs_per_request);
2864 blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);
2866 /* set optimal I/O size to 8KB */
2867 blk_queue_io_opt(q, 8192);
2869 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
2870 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
2872 blk_queue_rq_timeout(q, 8 * HZ);
2874 spin_lock_irqsave(&skdev->lock, flags);
2875 dev_dbg(&skdev->pdev->dev, "stopping queue\n");
2876 blk_mq_stop_hw_queues(skdev->queue);
2877 spin_unlock_irqrestore(&skdev->lock, flags);
2879 err_out:
2880 return rc;
2883 #define SKD_N_DEV_TABLE 16u
2884 static u32 skd_next_devno;
2886 static struct skd_device *skd_construct(struct pci_dev *pdev)
2888 struct skd_device *skdev;
2889 int blk_major = skd_major;
2890 size_t size;
2891 int rc;
2893 skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
2895 if (!skdev) {
2896 dev_err(&pdev->dev, "memory alloc failure\n");
2897 return NULL;
2900 skdev->state = SKD_DRVR_STATE_LOAD;
2901 skdev->pdev = pdev;
2902 skdev->devno = skd_next_devno++;
2903 skdev->major = blk_major;
2904 skdev->dev_max_queue_depth = 0;
2906 skdev->num_req_context = skd_max_queue_depth;
2907 skdev->num_fitmsg_context = skd_max_queue_depth;
2908 skdev->cur_max_queue_depth = 1;
2909 skdev->queue_low_water_mark = 1;
2910 skdev->proto_ver = 99;
2911 skdev->sgs_per_request = skd_sgs_per_request;
2912 skdev->dbg_level = skd_dbg_level;
2914 spin_lock_init(&skdev->lock);
2916 INIT_WORK(&skdev->start_queue, skd_start_queue);
2917 INIT_WORK(&skdev->completion_worker, skd_completion_worker);
2919 size = max(SKD_N_FITMSG_BYTES, SKD_N_SPECIAL_FITMSG_BYTES);
2920 skdev->msgbuf_cache = kmem_cache_create("skd-msgbuf", size, 0,
2921 SLAB_HWCACHE_ALIGN, NULL);
2922 if (!skdev->msgbuf_cache)
2923 goto err_out;
2924 WARN_ONCE(kmem_cache_size(skdev->msgbuf_cache) < size,
2925 "skd-msgbuf: %d < %zd\n",
2926 kmem_cache_size(skdev->msgbuf_cache), size);
2927 size = skd_sgs_per_request * sizeof(struct fit_sg_descriptor);
2928 skdev->sglist_cache = kmem_cache_create("skd-sglist", size, 0,
2929 SLAB_HWCACHE_ALIGN, NULL);
2930 if (!skdev->sglist_cache)
2931 goto err_out;
2932 WARN_ONCE(kmem_cache_size(skdev->sglist_cache) < size,
2933 "skd-sglist: %d < %zd\n",
2934 kmem_cache_size(skdev->sglist_cache), size);
2935 size = SKD_N_INTERNAL_BYTES;
2936 skdev->databuf_cache = kmem_cache_create("skd-databuf", size, 0,
2937 SLAB_HWCACHE_ALIGN, NULL);
2938 if (!skdev->databuf_cache)
2939 goto err_out;
2940 WARN_ONCE(kmem_cache_size(skdev->databuf_cache) < size,
2941 "skd-databuf: %d < %zd\n",
2942 kmem_cache_size(skdev->databuf_cache), size);
2944 dev_dbg(&skdev->pdev->dev, "skcomp\n");
2945 rc = skd_cons_skcomp(skdev);
2946 if (rc < 0)
2947 goto err_out;
2949 dev_dbg(&skdev->pdev->dev, "skmsg\n");
2950 rc = skd_cons_skmsg(skdev);
2951 if (rc < 0)
2952 goto err_out;
2954 dev_dbg(&skdev->pdev->dev, "sksb\n");
2955 rc = skd_cons_sksb(skdev);
2956 if (rc < 0)
2957 goto err_out;
2959 dev_dbg(&skdev->pdev->dev, "disk\n");
2960 rc = skd_cons_disk(skdev);
2961 if (rc < 0)
2962 goto err_out;
2964 dev_dbg(&skdev->pdev->dev, "VICTORY\n");
2965 return skdev;
2967 err_out:
2968 dev_dbg(&skdev->pdev->dev, "construct failed\n");
2969 skd_destruct(skdev);
2970 return NULL;
2974 *****************************************************************************
2975 * DESTRUCT (FREE)
2976 *****************************************************************************
2979 static void skd_free_skcomp(struct skd_device *skdev)
2981 if (skdev->skcomp_table)
2982 dma_free_coherent(&skdev->pdev->dev, SKD_SKCOMP_SIZE,
2983 skdev->skcomp_table, skdev->cq_dma_address);
2985 skdev->skcomp_table = NULL;
2986 skdev->cq_dma_address = 0;
2989 static void skd_free_skmsg(struct skd_device *skdev)
2991 u32 i;
2993 if (skdev->skmsg_table == NULL)
2994 return;
2996 for (i = 0; i < skdev->num_fitmsg_context; i++) {
2997 struct skd_fitmsg_context *skmsg;
2999 skmsg = &skdev->skmsg_table[i];
3001 if (skmsg->msg_buf != NULL) {
3002 dma_free_coherent(&skdev->pdev->dev, SKD_N_FITMSG_BYTES,
3003 skmsg->msg_buf,
3004 skmsg->mb_dma_address);
3006 skmsg->msg_buf = NULL;
3007 skmsg->mb_dma_address = 0;
3010 kfree(skdev->skmsg_table);
3011 skdev->skmsg_table = NULL;
3014 static void skd_free_sksb(struct skd_device *skdev)
3016 struct skd_special_context *skspcl = &skdev->internal_skspcl;
3018 skd_free_dma(skdev, skdev->databuf_cache, skspcl->data_buf,
3019 skspcl->db_dma_address, DMA_BIDIRECTIONAL);
3021 skspcl->data_buf = NULL;
3022 skspcl->db_dma_address = 0;
3024 skd_free_dma(skdev, skdev->msgbuf_cache, skspcl->msg_buf,
3025 skspcl->mb_dma_address, DMA_TO_DEVICE);
3027 skspcl->msg_buf = NULL;
3028 skspcl->mb_dma_address = 0;
3030 skd_free_sg_list(skdev, skspcl->req.sksg_list,
3031 skspcl->req.sksg_dma_address);
3033 skspcl->req.sksg_list = NULL;
3034 skspcl->req.sksg_dma_address = 0;
3037 static void skd_free_disk(struct skd_device *skdev)
3039 struct gendisk *disk = skdev->disk;
3041 if (disk && (disk->flags & GENHD_FL_UP))
3042 del_gendisk(disk);
3044 if (skdev->queue) {
3045 blk_cleanup_queue(skdev->queue);
3046 skdev->queue = NULL;
3047 if (disk)
3048 disk->queue = NULL;
3051 if (skdev->tag_set.tags)
3052 blk_mq_free_tag_set(&skdev->tag_set);
3054 put_disk(disk);
3055 skdev->disk = NULL;
3058 static void skd_destruct(struct skd_device *skdev)
3060 if (skdev == NULL)
3061 return;
3063 cancel_work_sync(&skdev->start_queue);
3065 dev_dbg(&skdev->pdev->dev, "disk\n");
3066 skd_free_disk(skdev);
3068 dev_dbg(&skdev->pdev->dev, "sksb\n");
3069 skd_free_sksb(skdev);
3071 dev_dbg(&skdev->pdev->dev, "skmsg\n");
3072 skd_free_skmsg(skdev);
3074 dev_dbg(&skdev->pdev->dev, "skcomp\n");
3075 skd_free_skcomp(skdev);
3077 kmem_cache_destroy(skdev->databuf_cache);
3078 kmem_cache_destroy(skdev->sglist_cache);
3079 kmem_cache_destroy(skdev->msgbuf_cache);
3081 dev_dbg(&skdev->pdev->dev, "skdev\n");
3082 kfree(skdev);
3086 *****************************************************************************
3087 * BLOCK DEVICE (BDEV) GLUE
3088 *****************************************************************************
3091 static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3093 struct skd_device *skdev;
3094 u64 capacity;
3096 skdev = bdev->bd_disk->private_data;
3098 dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
3099 bdev->bd_disk->disk_name, current->comm);
3101 if (skdev->read_cap_is_valid) {
3102 capacity = get_capacity(skdev->disk);
3103 geo->heads = 64;
3104 geo->sectors = 255;
3105 geo->cylinders = (capacity) / (255 * 64);
3107 return 0;
3109 return -EIO;
3112 static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
3114 dev_dbg(&skdev->pdev->dev, "add_disk\n");
3115 device_add_disk(parent, skdev->disk, NULL);
3116 return 0;
3119 static const struct block_device_operations skd_blockdev_ops = {
3120 .owner = THIS_MODULE,
3121 .getgeo = skd_bdev_getgeo,
3125 *****************************************************************************
3126 * PCIe DRIVER GLUE
3127 *****************************************************************************
3130 static const struct pci_device_id skd_pci_tbl[] = {
3131 { PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
3132 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
3133 { 0 } /* terminate list */
3136 MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
3138 static char *skd_pci_info(struct skd_device *skdev, char *str)
3140 int pcie_reg;
3142 strcpy(str, "PCIe (");
3143 pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
3145 if (pcie_reg) {
3147 char lwstr[6];
3148 uint16_t pcie_lstat, lspeed, lwidth;
3150 pcie_reg += 0x12;
3151 pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
3152 lspeed = pcie_lstat & (0xF);
3153 lwidth = (pcie_lstat & 0x3F0) >> 4;
3155 if (lspeed == 1)
3156 strcat(str, "2.5GT/s ");
3157 else if (lspeed == 2)
3158 strcat(str, "5.0GT/s ");
3159 else
3160 strcat(str, "<unknown> ");
3161 snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
3162 strcat(str, lwstr);
3164 return str;
3167 static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3169 int i;
3170 int rc = 0;
3171 char pci_str[32];
3172 struct skd_device *skdev;
3174 dev_dbg(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
3175 pdev->device);
3177 rc = pci_enable_device(pdev);
3178 if (rc)
3179 return rc;
3180 rc = pci_request_regions(pdev, DRV_NAME);
3181 if (rc)
3182 goto err_out;
3183 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3184 if (rc)
3185 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3186 if (rc) {
3187 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
3188 goto err_out_regions;
3191 if (!skd_major) {
3192 rc = register_blkdev(0, DRV_NAME);
3193 if (rc < 0)
3194 goto err_out_regions;
3195 BUG_ON(!rc);
3196 skd_major = rc;
3199 skdev = skd_construct(pdev);
3200 if (skdev == NULL) {
3201 rc = -ENOMEM;
3202 goto err_out_regions;
3205 skd_pci_info(skdev, pci_str);
3206 dev_info(&pdev->dev, "%s 64bit\n", pci_str);
3208 pci_set_master(pdev);
3209 rc = pci_enable_pcie_error_reporting(pdev);
3210 if (rc) {
3211 dev_err(&pdev->dev,
3212 "bad enable of PCIe error reporting rc=%d\n", rc);
3213 skdev->pcie_error_reporting_is_enabled = 0;
3214 } else
3215 skdev->pcie_error_reporting_is_enabled = 1;
3217 pci_set_drvdata(pdev, skdev);
3219 for (i = 0; i < SKD_MAX_BARS; i++) {
3220 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3221 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3222 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3223 skdev->mem_size[i]);
3224 if (!skdev->mem_map[i]) {
3225 dev_err(&pdev->dev,
3226 "Unable to map adapter memory!\n");
3227 rc = -ENODEV;
3228 goto err_out_iounmap;
3230 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3231 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3232 skdev->mem_size[i]);
3235 rc = skd_acquire_irq(skdev);
3236 if (rc) {
3237 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
3238 goto err_out_iounmap;
3241 rc = skd_start_timer(skdev);
3242 if (rc)
3243 goto err_out_timer;
3245 init_waitqueue_head(&skdev->waitq);
3247 skd_start_device(skdev);
3249 rc = wait_event_interruptible_timeout(skdev->waitq,
3250 (skdev->gendisk_on),
3251 (SKD_START_WAIT_SECONDS * HZ));
3252 if (skdev->gendisk_on > 0) {
3253 /* device came on-line after reset */
3254 skd_bdev_attach(&pdev->dev, skdev);
3255 rc = 0;
3256 } else {
3257 /* we timed out, something is wrong with the device,
3258 don't add the disk structure */
3259 dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
3260 rc);
3261 /* in case of no error; we timeout with ENXIO */
3262 if (!rc)
3263 rc = -ENXIO;
3264 goto err_out_timer;
3267 return rc;
3269 err_out_timer:
3270 skd_stop_device(skdev);
3271 skd_release_irq(skdev);
3273 err_out_iounmap:
3274 for (i = 0; i < SKD_MAX_BARS; i++)
3275 if (skdev->mem_map[i])
3276 iounmap(skdev->mem_map[i]);
3278 if (skdev->pcie_error_reporting_is_enabled)
3279 pci_disable_pcie_error_reporting(pdev);
3281 skd_destruct(skdev);
3283 err_out_regions:
3284 pci_release_regions(pdev);
3286 err_out:
3287 pci_disable_device(pdev);
3288 pci_set_drvdata(pdev, NULL);
3289 return rc;
3292 static void skd_pci_remove(struct pci_dev *pdev)
3294 int i;
3295 struct skd_device *skdev;
3297 skdev = pci_get_drvdata(pdev);
3298 if (!skdev) {
3299 dev_err(&pdev->dev, "no device data for PCI\n");
3300 return;
3302 skd_stop_device(skdev);
3303 skd_release_irq(skdev);
3305 for (i = 0; i < SKD_MAX_BARS; i++)
3306 if (skdev->mem_map[i])
3307 iounmap(skdev->mem_map[i]);
3309 if (skdev->pcie_error_reporting_is_enabled)
3310 pci_disable_pcie_error_reporting(pdev);
3312 skd_destruct(skdev);
3314 pci_release_regions(pdev);
3315 pci_disable_device(pdev);
3316 pci_set_drvdata(pdev, NULL);
3318 return;
3321 static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3323 int i;
3324 struct skd_device *skdev;
3326 skdev = pci_get_drvdata(pdev);
3327 if (!skdev) {
3328 dev_err(&pdev->dev, "no device data for PCI\n");
3329 return -EIO;
3332 skd_stop_device(skdev);
3334 skd_release_irq(skdev);
3336 for (i = 0; i < SKD_MAX_BARS; i++)
3337 if (skdev->mem_map[i])
3338 iounmap(skdev->mem_map[i]);
3340 if (skdev->pcie_error_reporting_is_enabled)
3341 pci_disable_pcie_error_reporting(pdev);
3343 pci_release_regions(pdev);
3344 pci_save_state(pdev);
3345 pci_disable_device(pdev);
3346 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3347 return 0;
3350 static int skd_pci_resume(struct pci_dev *pdev)
3352 int i;
3353 int rc = 0;
3354 struct skd_device *skdev;
3356 skdev = pci_get_drvdata(pdev);
3357 if (!skdev) {
3358 dev_err(&pdev->dev, "no device data for PCI\n");
3359 return -1;
3362 pci_set_power_state(pdev, PCI_D0);
3363 pci_enable_wake(pdev, PCI_D0, 0);
3364 pci_restore_state(pdev);
3366 rc = pci_enable_device(pdev);
3367 if (rc)
3368 return rc;
3369 rc = pci_request_regions(pdev, DRV_NAME);
3370 if (rc)
3371 goto err_out;
3372 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3373 if (rc)
3374 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3375 if (rc) {
3376 dev_err(&pdev->dev, "DMA mask error %d\n", rc);
3377 goto err_out_regions;
3380 pci_set_master(pdev);
3381 rc = pci_enable_pcie_error_reporting(pdev);
3382 if (rc) {
3383 dev_err(&pdev->dev,
3384 "bad enable of PCIe error reporting rc=%d\n", rc);
3385 skdev->pcie_error_reporting_is_enabled = 0;
3386 } else
3387 skdev->pcie_error_reporting_is_enabled = 1;
3389 for (i = 0; i < SKD_MAX_BARS; i++) {
3391 skdev->mem_phys[i] = pci_resource_start(pdev, i);
3392 skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
3393 skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
3394 skdev->mem_size[i]);
3395 if (!skdev->mem_map[i]) {
3396 dev_err(&pdev->dev, "Unable to map adapter memory!\n");
3397 rc = -ENODEV;
3398 goto err_out_iounmap;
3400 dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
3401 skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
3402 skdev->mem_size[i]);
3404 rc = skd_acquire_irq(skdev);
3405 if (rc) {
3406 dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
3407 goto err_out_iounmap;
3410 rc = skd_start_timer(skdev);
3411 if (rc)
3412 goto err_out_timer;
3414 init_waitqueue_head(&skdev->waitq);
3416 skd_start_device(skdev);
3418 return rc;
3420 err_out_timer:
3421 skd_stop_device(skdev);
3422 skd_release_irq(skdev);
3424 err_out_iounmap:
3425 for (i = 0; i < SKD_MAX_BARS; i++)
3426 if (skdev->mem_map[i])
3427 iounmap(skdev->mem_map[i]);
3429 if (skdev->pcie_error_reporting_is_enabled)
3430 pci_disable_pcie_error_reporting(pdev);
3432 err_out_regions:
3433 pci_release_regions(pdev);
3435 err_out:
3436 pci_disable_device(pdev);
3437 return rc;
3440 static void skd_pci_shutdown(struct pci_dev *pdev)
3442 struct skd_device *skdev;
3444 dev_err(&pdev->dev, "%s called\n", __func__);
3446 skdev = pci_get_drvdata(pdev);
3447 if (!skdev) {
3448 dev_err(&pdev->dev, "no device data for PCI\n");
3449 return;
3452 dev_err(&pdev->dev, "calling stop\n");
3453 skd_stop_device(skdev);
3456 static struct pci_driver skd_driver = {
3457 .name = DRV_NAME,
3458 .id_table = skd_pci_tbl,
3459 .probe = skd_pci_probe,
3460 .remove = skd_pci_remove,
3461 .suspend = skd_pci_suspend,
3462 .resume = skd_pci_resume,
3463 .shutdown = skd_pci_shutdown,
3467 *****************************************************************************
3468 * LOGGING SUPPORT
3469 *****************************************************************************
3472 const char *skd_drive_state_to_str(int state)
3474 switch (state) {
3475 case FIT_SR_DRIVE_OFFLINE:
3476 return "OFFLINE";
3477 case FIT_SR_DRIVE_INIT:
3478 return "INIT";
3479 case FIT_SR_DRIVE_ONLINE:
3480 return "ONLINE";
3481 case FIT_SR_DRIVE_BUSY:
3482 return "BUSY";
3483 case FIT_SR_DRIVE_FAULT:
3484 return "FAULT";
3485 case FIT_SR_DRIVE_DEGRADED:
3486 return "DEGRADED";
3487 case FIT_SR_PCIE_LINK_DOWN:
3488 return "INK_DOWN";
3489 case FIT_SR_DRIVE_SOFT_RESET:
3490 return "SOFT_RESET";
3491 case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
3492 return "NEED_FW";
3493 case FIT_SR_DRIVE_INIT_FAULT:
3494 return "INIT_FAULT";
3495 case FIT_SR_DRIVE_BUSY_SANITIZE:
3496 return "BUSY_SANITIZE";
3497 case FIT_SR_DRIVE_BUSY_ERASE:
3498 return "BUSY_ERASE";
3499 case FIT_SR_DRIVE_FW_BOOTING:
3500 return "FW_BOOTING";
3501 default:
3502 return "???";
3506 const char *skd_skdev_state_to_str(enum skd_drvr_state state)
3508 switch (state) {
3509 case SKD_DRVR_STATE_LOAD:
3510 return "LOAD";
3511 case SKD_DRVR_STATE_IDLE:
3512 return "IDLE";
3513 case SKD_DRVR_STATE_BUSY:
3514 return "BUSY";
3515 case SKD_DRVR_STATE_STARTING:
3516 return "STARTING";
3517 case SKD_DRVR_STATE_ONLINE:
3518 return "ONLINE";
3519 case SKD_DRVR_STATE_PAUSING:
3520 return "PAUSING";
3521 case SKD_DRVR_STATE_PAUSED:
3522 return "PAUSED";
3523 case SKD_DRVR_STATE_RESTARTING:
3524 return "RESTARTING";
3525 case SKD_DRVR_STATE_RESUMING:
3526 return "RESUMING";
3527 case SKD_DRVR_STATE_STOPPING:
3528 return "STOPPING";
3529 case SKD_DRVR_STATE_SYNCING:
3530 return "SYNCING";
3531 case SKD_DRVR_STATE_FAULT:
3532 return "FAULT";
3533 case SKD_DRVR_STATE_DISAPPEARED:
3534 return "DISAPPEARED";
3535 case SKD_DRVR_STATE_BUSY_ERASE:
3536 return "BUSY_ERASE";
3537 case SKD_DRVR_STATE_BUSY_SANITIZE:
3538 return "BUSY_SANITIZE";
3539 case SKD_DRVR_STATE_BUSY_IMMINENT:
3540 return "BUSY_IMMINENT";
3541 case SKD_DRVR_STATE_WAIT_BOOT:
3542 return "WAIT_BOOT";
3544 default:
3545 return "???";
3549 static const char *skd_skreq_state_to_str(enum skd_req_state state)
3551 switch (state) {
3552 case SKD_REQ_STATE_IDLE:
3553 return "IDLE";
3554 case SKD_REQ_STATE_SETUP:
3555 return "SETUP";
3556 case SKD_REQ_STATE_BUSY:
3557 return "BUSY";
3558 case SKD_REQ_STATE_COMPLETED:
3559 return "COMPLETED";
3560 case SKD_REQ_STATE_TIMEOUT:
3561 return "TIMEOUT";
3562 default:
3563 return "???";
3567 static void skd_log_skdev(struct skd_device *skdev, const char *event)
3569 dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
3570 dev_dbg(&skdev->pdev->dev, " drive_state=%s(%d) driver_state=%s(%d)\n",
3571 skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
3572 skd_skdev_state_to_str(skdev->state), skdev->state);
3573 dev_dbg(&skdev->pdev->dev, " busy=%d limit=%d dev=%d lowat=%d\n",
3574 skd_in_flight(skdev), skdev->cur_max_queue_depth,
3575 skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
3576 dev_dbg(&skdev->pdev->dev, " cycle=%d cycle_ix=%d\n",
3577 skdev->skcomp_cycle, skdev->skcomp_ix);
3580 static void skd_log_skreq(struct skd_device *skdev,
3581 struct skd_request_context *skreq, const char *event)
3583 struct request *req = blk_mq_rq_from_pdu(skreq);
3584 u32 lba = blk_rq_pos(req);
3585 u32 count = blk_rq_sectors(req);
3587 dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
3588 dev_dbg(&skdev->pdev->dev, " state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
3589 skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
3590 skreq->fitmsg_id);
3591 dev_dbg(&skdev->pdev->dev, " sg_dir=%d n_sg=%d\n",
3592 skreq->data_dir, skreq->n_sg);
3594 dev_dbg(&skdev->pdev->dev,
3595 "req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req, lba, lba,
3596 count, count, (int)rq_data_dir(req));
3600 *****************************************************************************
3601 * MODULE GLUE
3602 *****************************************************************************
3605 static int __init skd_init(void)
3607 BUILD_BUG_ON(sizeof(struct fit_completion_entry_v1) != 8);
3608 BUILD_BUG_ON(sizeof(struct fit_comp_error_info) != 32);
3609 BUILD_BUG_ON(sizeof(struct skd_command_header) != 16);
3610 BUILD_BUG_ON(sizeof(struct skd_scsi_request) != 32);
3611 BUILD_BUG_ON(sizeof(struct driver_inquiry_data) != 44);
3612 BUILD_BUG_ON(offsetof(struct skd_msg_buf, fmh) != 0);
3613 BUILD_BUG_ON(offsetof(struct skd_msg_buf, scsi) != 64);
3614 BUILD_BUG_ON(sizeof(struct skd_msg_buf) != SKD_N_FITMSG_BYTES);
3616 switch (skd_isr_type) {
3617 case SKD_IRQ_LEGACY:
3618 case SKD_IRQ_MSI:
3619 case SKD_IRQ_MSIX:
3620 break;
3621 default:
3622 pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
3623 skd_isr_type, SKD_IRQ_DEFAULT);
3624 skd_isr_type = SKD_IRQ_DEFAULT;
3627 if (skd_max_queue_depth < 1 ||
3628 skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
3629 pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
3630 skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
3631 skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
3634 if (skd_max_req_per_msg < 1 ||
3635 skd_max_req_per_msg > SKD_MAX_REQ_PER_MSG) {
3636 pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
3637 skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
3638 skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
3641 if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
3642 pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
3643 skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
3644 skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
3647 if (skd_dbg_level < 0 || skd_dbg_level > 2) {
3648 pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
3649 skd_dbg_level, 0);
3650 skd_dbg_level = 0;
3653 if (skd_isr_comp_limit < 0) {
3654 pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
3655 skd_isr_comp_limit, 0);
3656 skd_isr_comp_limit = 0;
3659 return pci_register_driver(&skd_driver);
3662 static void __exit skd_exit(void)
3664 pci_unregister_driver(&skd_driver);
3666 if (skd_major)
3667 unregister_blkdev(skd_major, DRV_NAME);
3670 module_init(skd_init);
3671 module_exit(skd_exit);