skd_main.c 127.8 KB
Newer Older
B
Bart Van Assche 已提交
1 2 3
/*
 * Driver for sTec s1120 PCIe SSDs. sTec was acquired in 2013 by HGST and HGST
 * was acquired by Western Digital in 2012.
4
 *
B
Bart Van Assche 已提交
5 6 7 8 9
 * Copyright 2012 sTec, Inc.
 * Copyright (c) 2017 Western Digital Corporation or its affiliates.
 *
 * This file is part of the Linux kernel, and is made available under
 * the terms of the GNU General Public License version 2.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/compiler.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/hdreg.h>
#include <linux/dma-mapping.h>
#include <linux/completion.h>
#include <linux/scatterlist.h>
#include <linux/version.h>
#include <linux/err.h>
#include <linux/aer.h>
#include <linux/wait.h>
#include <linux/uio.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <linux/io.h>
#include <linux/uaccess.h>
38
#include <asm/unaligned.h>
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#include "skd_s1120.h"

static int skd_dbg_level;
static int skd_isr_comp_limit = 4;

enum {
	STEC_LINK_2_5GTS = 0,
	STEC_LINK_5GTS = 1,
	STEC_LINK_8GTS = 2,
	STEC_LINK_UNKNOWN = 0xFF
};

enum {
	SKD_FLUSH_INITIALIZER,
	SKD_FLUSH_ZERO_SIZE_FIRST,
	SKD_FLUSH_DATA_SECOND,
};

#define SKD_ASSERT(expr) \
	do { \
		if (unlikely(!(expr))) { \
			pr_err("Assertion failed! %s,%s,%s,line=%d\n",	\
			       # expr, __FILE__, __func__, __LINE__); \
		} \
	} while (0)

#define DRV_NAME "skd"
#define DRV_VERSION "2.2.1"
#define DRV_BUILD_ID "0260"
#define PFX DRV_NAME ": "
#define DRV_BIN_VERSION 0x100
#define DRV_VER_COMPL   "2.2.1." DRV_BUILD_ID

B
Bart Van Assche 已提交
73
MODULE_LICENSE("GPL");
74

M
Mike Snitzer 已提交
75
MODULE_DESCRIPTION("STEC s1120 PCIe SSD block driver (b" DRV_BUILD_ID ")");
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
MODULE_VERSION(DRV_VERSION "-" DRV_BUILD_ID);

#define PCI_VENDOR_ID_STEC      0x1B39
#define PCI_DEVICE_ID_S1120     0x0001

#define SKD_FUA_NV		(1 << 1)
#define SKD_MINORS_PER_DEVICE   16

#define SKD_MAX_QUEUE_DEPTH     200u

#define SKD_PAUSE_TIMEOUT       (5 * 1000)

#define SKD_N_FITMSG_BYTES      (512u)

#define SKD_N_SPECIAL_CONTEXT   32u
#define SKD_N_SPECIAL_FITMSG_BYTES      (128u)

/* SG elements are 32 bytes, so we can make this 4096 and still be under the
 * 128KB limit.  That allows 4096*4K = 16M xfer size
 */
#define SKD_N_SG_PER_REQ_DEFAULT 256u
#define SKD_N_SG_PER_SPECIAL    256u

#define SKD_N_COMPLETION_ENTRY  256u
#define SKD_N_READ_CAP_BYTES    (8u)

#define SKD_N_INTERNAL_BYTES    (512u)

/* 5 bits of uniqifier, 0xF800 */
#define SKD_ID_INCR             (0x400)
#define SKD_ID_TABLE_MASK       (3u << 8u)
#define  SKD_ID_RW_REQUEST      (0u << 8u)
#define  SKD_ID_INTERNAL        (1u << 8u)
#define  SKD_ID_SPECIAL_REQUEST (2u << 8u)
#define  SKD_ID_FIT_MSG         (3u << 8u)
#define SKD_ID_SLOT_MASK        0x00FFu
#define SKD_ID_SLOT_AND_TABLE_MASK 0x03FFu

#define SKD_N_TIMEOUT_SLOT      4u
#define SKD_TIMEOUT_SLOT_MASK   3u

#define SKD_N_MAX_SECTORS 2048u

#define SKD_MAX_RETRIES 2u

#define SKD_TIMER_SECONDS(seconds) (seconds)
#define SKD_TIMER_MINUTES(minutes) ((minutes) * (60))

#define INQ_STD_NBYTES 36

enum skd_drvr_state {
	SKD_DRVR_STATE_LOAD,
	SKD_DRVR_STATE_IDLE,
	SKD_DRVR_STATE_BUSY,
	SKD_DRVR_STATE_STARTING,
	SKD_DRVR_STATE_ONLINE,
	SKD_DRVR_STATE_PAUSING,
	SKD_DRVR_STATE_PAUSED,
	SKD_DRVR_STATE_DRAINING_TIMEOUT,
	SKD_DRVR_STATE_RESTARTING,
	SKD_DRVR_STATE_RESUMING,
	SKD_DRVR_STATE_STOPPING,
	SKD_DRVR_STATE_FAULT,
	SKD_DRVR_STATE_DISAPPEARED,
	SKD_DRVR_STATE_PROTOCOL_MISMATCH,
	SKD_DRVR_STATE_BUSY_ERASE,
	SKD_DRVR_STATE_BUSY_SANITIZE,
	SKD_DRVR_STATE_BUSY_IMMINENT,
	SKD_DRVR_STATE_WAIT_BOOT,
	SKD_DRVR_STATE_SYNCING,
};

#define SKD_WAIT_BOOT_TIMO      SKD_TIMER_SECONDS(90u)
#define SKD_STARTING_TIMO       SKD_TIMER_SECONDS(8u)
#define SKD_RESTARTING_TIMO     SKD_TIMER_MINUTES(4u)
#define SKD_DRAINING_TIMO       SKD_TIMER_SECONDS(6u)
#define SKD_BUSY_TIMO           SKD_TIMER_MINUTES(20u)
#define SKD_STARTED_BUSY_TIMO   SKD_TIMER_SECONDS(60u)
#define SKD_START_WAIT_SECONDS  90u

enum skd_req_state {
	SKD_REQ_STATE_IDLE,
	SKD_REQ_STATE_SETUP,
	SKD_REQ_STATE_BUSY,
	SKD_REQ_STATE_COMPLETED,
	SKD_REQ_STATE_TIMEOUT,
	SKD_REQ_STATE_ABORTED,
};

enum skd_fit_msg_state {
	SKD_MSG_STATE_IDLE,
	SKD_MSG_STATE_BUSY,
};

enum skd_check_status_action {
	SKD_CHECK_STATUS_REPORT_GOOD,
	SKD_CHECK_STATUS_REPORT_SMART_ALERT,
	SKD_CHECK_STATUS_REQUEUE_REQUEST,
	SKD_CHECK_STATUS_REPORT_ERROR,
	SKD_CHECK_STATUS_BUSY_IMMINENT,
};

struct skd_fitmsg_context {
	enum skd_fit_msg_state state;

	struct skd_fitmsg_context *next;

	u32 id;
	u16 outstanding;

	u32 length;
	u32 offset;

	u8 *msg_buf;
	dma_addr_t mb_dma_address;
};

struct skd_request_context {
	enum skd_req_state state;

	struct skd_request_context *next;

	u16 id;
	u32 fitmsg_id;

	struct request *req;
	u8 flush_cmd;

	u32 timeout_stamp;
	u8 sg_data_dir;
	struct scatterlist *sg;
	u32 n_sg;
	u32 sg_byte_count;

	struct fit_sg_descriptor *sksg_list;
	dma_addr_t sksg_dma_address;

	struct fit_completion_entry_v1 completion;

	struct fit_comp_error_info err_info;

};
#define SKD_DATA_DIR_HOST_TO_CARD       1
#define SKD_DATA_DIR_CARD_TO_HOST       2

struct skd_special_context {
	struct skd_request_context req;

	u8 orphaned;

	void *data_buf;
	dma_addr_t db_dma_address;

	u8 *msg_buf;
	dma_addr_t mb_dma_address;
};

struct skd_sg_io {
	fmode_t mode;
	void __user *argp;

	struct sg_io_hdr sg;

	u8 cdb[16];

	u32 dxfer_len;
	u32 iovcnt;
	struct sg_iovec *iov;
	struct sg_iovec no_iov_iov;

	struct skd_special_context *skspcl;
};

typedef enum skd_irq_type {
	SKD_IRQ_LEGACY,
	SKD_IRQ_MSI,
	SKD_IRQ_MSIX
} skd_irq_type_t;

#define SKD_MAX_BARS                    2

struct skd_device {
	volatile void __iomem *mem_map[SKD_MAX_BARS];
	resource_size_t mem_phys[SKD_MAX_BARS];
	u32 mem_size[SKD_MAX_BARS];

	struct skd_msix_entry *msix_entries;

	struct pci_dev *pdev;
	int pcie_error_reporting_is_enabled;

	spinlock_t lock;
	struct gendisk *disk;
	struct request_queue *queue;
	struct device *class_dev;
	int gendisk_on;
	int sync_done;

	u32 devno;
	u32 major;
	char isr_name[30];

	enum skd_drvr_state state;
	u32 drive_state;

	u32 in_flight;
	u32 cur_max_queue_depth;
	u32 queue_low_water_mark;
	u32 dev_max_queue_depth;

	u32 num_fitmsg_context;
	u32 num_req_context;

	u32 timeout_slot[SKD_N_TIMEOUT_SLOT];
	u32 timeout_stamp;
	struct skd_fitmsg_context *skmsg_free_list;
	struct skd_fitmsg_context *skmsg_table;

	struct skd_request_context *skreq_free_list;
	struct skd_request_context *skreq_table;

	struct skd_special_context *skspcl_free_list;
	struct skd_special_context *skspcl_table;

	struct skd_special_context internal_skspcl;
	u32 read_cap_blocksize;
	u32 read_cap_last_lba;
	int read_cap_is_valid;
	int inquiry_is_valid;
	u8 inq_serial_num[13];  /*12 chars plus null term */

	u8 skcomp_cycle;
	u32 skcomp_ix;
	struct fit_completion_entry_v1 *skcomp_table;
	struct fit_comp_error_info *skerr_table;
	dma_addr_t cq_dma_address;

	wait_queue_head_t waitq;

	struct timer_list timer;
	u32 timer_countdown;
	u32 timer_substate;

	int n_special;
	int sgs_per_request;
	u32 last_mtd;

	u32 proto_ver;

	int dbg_level;
	u32 connect_time_stamp;
	int connect_retries;
#define SKD_MAX_CONNECT_RETRIES 16
	u32 drive_jiffies;

	u32 timo_slot;

M
Mike Snitzer 已提交
333
	struct work_struct completion_worker;
334 335 336 337 338 339 340 341
};

#define SKD_WRITEL(DEV, VAL, OFF) skd_reg_write32(DEV, VAL, OFF)
#define SKD_READL(DEV, OFF)      skd_reg_read32(DEV, OFF)
#define SKD_WRITEQ(DEV, VAL, OFF) skd_reg_write64(DEV, VAL, OFF)

static inline u32 skd_reg_read32(struct skd_device *skdev, u32 offset)
{
342
	u32 val = readl(skdev->mem_map[1] + offset);
343

344
	if (unlikely(skdev->dbg_level >= 2))
345
		dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
346
	return val;
347 348 349 350 351
}

static inline void skd_reg_write32(struct skd_device *skdev, u32 val,
				   u32 offset)
{
352 353
	writel(val, skdev->mem_map[1] + offset);
	if (unlikely(skdev->dbg_level >= 2))
354
		dev_dbg(&skdev->pdev->dev, "offset %x = %x\n", offset, val);
355 356 357 358 359
}

static inline void skd_reg_write64(struct skd_device *skdev, u64 val,
				   u32 offset)
{
360 361
	writeq(val, skdev->mem_map[1] + offset);
	if (unlikely(skdev->dbg_level >= 2))
362 363
		dev_dbg(&skdev->pdev->dev, "offset %x = %016llx\n", offset,
			val);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
}


#define SKD_IRQ_DEFAULT SKD_IRQ_MSI
static int skd_isr_type = SKD_IRQ_DEFAULT;

module_param(skd_isr_type, int, 0444);
MODULE_PARM_DESC(skd_isr_type, "Interrupt type capability."
		 " (0==legacy, 1==MSI, 2==MSI-X, default==1)");

#define SKD_MAX_REQ_PER_MSG_DEFAULT 1
static int skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;

module_param(skd_max_req_per_msg, int, 0444);
MODULE_PARM_DESC(skd_max_req_per_msg,
		 "Maximum SCSI requests packed in a single message."
		 " (1-14, default==1)");

#define SKD_MAX_QUEUE_DEPTH_DEFAULT 64
#define SKD_MAX_QUEUE_DEPTH_DEFAULT_STR "64"
static int skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;

module_param(skd_max_queue_depth, int, 0444);
MODULE_PARM_DESC(skd_max_queue_depth,
		 "Maximum SCSI requests issued to s1120."
		 " (1-200, default==" SKD_MAX_QUEUE_DEPTH_DEFAULT_STR ")");

static int skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
module_param(skd_sgs_per_request, int, 0444);
MODULE_PARM_DESC(skd_sgs_per_request,
		 "Maximum SG elements per block request."
		 " (1-4096, default==256)");

static int skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
module_param(skd_max_pass_thru, int, 0444);
MODULE_PARM_DESC(skd_max_pass_thru,
		 "Maximum SCSI pass-thru at a time." " (1-50, default==32)");

module_param(skd_dbg_level, int, 0444);
MODULE_PARM_DESC(skd_dbg_level, "s1120 debug level (0,1,2)");

module_param(skd_isr_comp_limit, int, 0444);
MODULE_PARM_DESC(skd_isr_comp_limit, "s1120 isr comp limit (0=none) default=4");

/* Major device number dynamically assigned. */
static u32 skd_major;

static void skd_destruct(struct skd_device *skdev);
static const struct block_device_operations skd_blockdev_ops;
static void skd_send_fitmsg(struct skd_device *skdev,
			    struct skd_fitmsg_context *skmsg);
static void skd_send_special_fitmsg(struct skd_device *skdev,
				    struct skd_special_context *skspcl);
static void skd_request_fn(struct request_queue *rq);
static void skd_end_request(struct skd_device *skdev,
419 420
		struct skd_request_context *skreq, blk_status_t status);
static bool skd_preop_sg_list(struct skd_device *skdev,
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
			     struct skd_request_context *skreq);
static void skd_postop_sg_list(struct skd_device *skdev,
			       struct skd_request_context *skreq);

static void skd_restart_device(struct skd_device *skdev);
static int skd_quiesce_dev(struct skd_device *skdev);
static int skd_unquiesce_dev(struct skd_device *skdev);
static void skd_release_special(struct skd_device *skdev,
				struct skd_special_context *skspcl);
static void skd_disable_interrupts(struct skd_device *skdev);
static void skd_isr_fwstate(struct skd_device *skdev);
static void skd_recover_requests(struct skd_device *skdev, int requeue);
static void skd_soft_reset(struct skd_device *skdev);

const char *skd_drive_state_to_str(int state);
const char *skd_skdev_state_to_str(enum skd_drvr_state state);
static void skd_log_skdev(struct skd_device *skdev, const char *event);
static void skd_log_skmsg(struct skd_device *skdev,
			  struct skd_fitmsg_context *skmsg, const char *event);
static void skd_log_skreq(struct skd_device *skdev,
			  struct skd_request_context *skreq, const char *event);

/*
 *****************************************************************************
 * READ/WRITE REQUESTS
 *****************************************************************************
 */
J
Jens Axboe 已提交
448
static void skd_fail_all_pending(struct skd_device *skdev)
449 450 451 452 453 454 455 456 457
{
	struct request_queue *q = skdev->queue;
	struct request *req;

	for (;; ) {
		req = blk_peek_request(q);
		if (req == NULL)
			break;
		blk_start_request(req);
458
		__blk_end_request_all(req, BLK_STS_IOERR);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	}
}

static void
skd_prep_rw_cdb(struct skd_scsi_request *scsi_req,
		int data_dir, unsigned lba,
		unsigned count)
{
	if (data_dir == READ)
		scsi_req->cdb[0] = 0x28;
	else
		scsi_req->cdb[0] = 0x2a;

	scsi_req->cdb[1] = 0;
	scsi_req->cdb[2] = (lba & 0xff000000) >> 24;
	scsi_req->cdb[3] = (lba & 0xff0000) >> 16;
	scsi_req->cdb[4] = (lba & 0xff00) >> 8;
	scsi_req->cdb[5] = (lba & 0xff);
	scsi_req->cdb[6] = 0;
	scsi_req->cdb[7] = (count & 0xff00) >> 8;
	scsi_req->cdb[8] = count & 0xff;
	scsi_req->cdb[9] = 0;
}

static void
skd_prep_zerosize_flush_cdb(struct skd_scsi_request *scsi_req,
M
Mike Snitzer 已提交
485
			    struct skd_request_context *skreq)
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
{
	skreq->flush_cmd = 1;

	scsi_req->cdb[0] = 0x35;
	scsi_req->cdb[1] = 0;
	scsi_req->cdb[2] = 0;
	scsi_req->cdb[3] = 0;
	scsi_req->cdb[4] = 0;
	scsi_req->cdb[5] = 0;
	scsi_req->cdb[6] = 0;
	scsi_req->cdb[7] = 0;
	scsi_req->cdb[8] = 0;
	scsi_req->cdb[9] = 0;
}

static void skd_request_fn_not_online(struct request_queue *q);

static void skd_request_fn(struct request_queue *q)
{
	struct skd_device *skdev = q->queuedata;
	struct skd_fitmsg_context *skmsg = NULL;
	struct fit_msg_hdr *fmh = NULL;
	struct skd_request_context *skreq;
	struct request *req = NULL;
	struct skd_scsi_request *scsi_req;
	unsigned long io_flags;
	u32 lba;
	u32 count;
	int data_dir;
515
	__be64 be_dmaa;
516 517 518 519 520 521 522 523 524 525
	u64 cmdctxt;
	u32 timo_slot;
	void *cmd_ptr;
	int flush, fua;

	if (skdev->state != SKD_DRVR_STATE_ONLINE) {
		skd_request_fn_not_online(q);
		return;
	}

526
	if (blk_queue_stopped(skdev->queue)) {
527 528 529 530 531 532
		if (skdev->skmsg_free_list == NULL ||
		    skdev->skreq_free_list == NULL ||
		    skdev->in_flight >= skdev->queue_low_water_mark)
			/* There is still some kind of shortage */
			return;

533
		queue_flag_clear(QUEUE_FLAG_STOPPED, skdev->queue);
534 535 536 537 538 539 540 541 542 543 544 545 546
	}

	/*
	 * Stop conditions:
	 *  - There are no more native requests
	 *  - There are already the maximum number of requests in progress
	 *  - There are no more skd_request_context entries
	 *  - There are no more FIT msg buffers
	 */
	for (;; ) {

		flush = fua = 0;

J
Jens Axboe 已提交
547
		req = blk_peek_request(q);
548

J
Jens Axboe 已提交
549 550 551
		/* Are there any native requests to start? */
		if (req == NULL)
			break;
552

J
Jens Axboe 已提交
553 554 555 556
		lba = (u32)blk_rq_pos(req);
		count = blk_rq_sectors(req);
		data_dir = rq_data_dir(req);
		io_flags = req->cmd_flags;
557

558
		if (req_op(req) == REQ_OP_FLUSH)
J
Jens Axboe 已提交
559
			flush++;
560

J
Jens Axboe 已提交
561 562
		if (io_flags & REQ_FUA)
			fua++;
563

564 565 566
		dev_dbg(&skdev->pdev->dev,
			"new req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n",
			req, lba, lba, count, count, data_dir);
567

M
Mike Snitzer 已提交
568
		/* At this point we know there is a request */
569 570 571

		/* Are too many requets already in progress? */
		if (skdev->in_flight >= skdev->cur_max_queue_depth) {
572 573
			dev_dbg(&skdev->pdev->dev, "qdepth %d, limit %d\n",
				skdev->in_flight, skdev->cur_max_queue_depth);
574 575 576 577 578 579
			break;
		}

		/* Is a skd_request_context available? */
		skreq = skdev->skreq_free_list;
		if (skreq == NULL) {
580
			dev_dbg(&skdev->pdev->dev, "Out of req=%p\n", q);
581 582 583 584 585 586 587 588
			break;
		}
		SKD_ASSERT(skreq->state == SKD_REQ_STATE_IDLE);
		SKD_ASSERT((skreq->id & SKD_ID_INCR) == 0);

		/* Now we check to see if we can get a fit msg */
		if (skmsg == NULL) {
			if (skdev->skmsg_free_list == NULL) {
589
				dev_dbg(&skdev->pdev->dev, "Out of msg\n");
590 591 592 593 594 595 596 597 598
				break;
			}
		}

		skreq->flush_cmd = 0;
		skreq->n_sg = 0;
		skreq->sg_byte_count = 0;

		/*
M
Mike Snitzer 已提交
599
		 * OK to now dequeue request from q.
600 601 602 603 604
		 *
		 * At this point we are comitted to either start or reject
		 * the native request. Note that skd_request_context is
		 * available but is still at the head of the free list.
		 */
J
Jens Axboe 已提交
605 606 607
		blk_start_request(req);
		skreq->req = req;
		skreq->fitmsg_id = 0;
608 609 610 611 612 613

		/* Either a FIT msg is in progress or we have to start one. */
		if (skmsg == NULL) {
			/* Are there any FIT msg buffers available? */
			skmsg = skdev->skmsg_free_list;
			if (skmsg == NULL) {
614 615 616
				dev_dbg(&skdev->pdev->dev,
					"Out of msg skdev=%p\n",
					skdev);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
				break;
			}
			SKD_ASSERT(skmsg->state == SKD_MSG_STATE_IDLE);
			SKD_ASSERT((skmsg->id & SKD_ID_INCR) == 0);

			skdev->skmsg_free_list = skmsg->next;

			skmsg->state = SKD_MSG_STATE_BUSY;
			skmsg->id += SKD_ID_INCR;

			/* Initialize the FIT msg header */
			fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
			memset(fmh, 0, sizeof(*fmh));
			fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
			skmsg->length = sizeof(*fmh);
		}

		skreq->fitmsg_id = skmsg->id;

		/*
		 * Note that a FIT msg may have just been started
		 * but contains no SoFIT requests yet.
		 */

		/*
		 * Transcode the request, checking as we go. The outcome of
		 * the transcoding is represented by the error variable.
		 */
		cmd_ptr = &skmsg->msg_buf[skmsg->length];
		memset(cmd_ptr, 0, 32);

648
		be_dmaa = cpu_to_be64(skreq->sksg_dma_address);
649 650 651 652 653 654 655 656 657 658 659
		cmdctxt = skreq->id + SKD_ID_INCR;

		scsi_req = cmd_ptr;
		scsi_req->hdr.tag = cmdctxt;
		scsi_req->hdr.sg_list_dma_address = be_dmaa;

		if (data_dir == READ)
			skreq->sg_data_dir = SKD_DATA_DIR_CARD_TO_HOST;
		else
			skreq->sg_data_dir = SKD_DATA_DIR_HOST_TO_CARD;

J
Jeff Moyer 已提交
660
		if (flush == SKD_FLUSH_ZERO_SIZE_FIRST) {
661 662 663 664 665 666 667 668 669
			skd_prep_zerosize_flush_cdb(scsi_req, skreq);
			SKD_ASSERT(skreq->flush_cmd == 1);
		} else {
			skd_prep_rw_cdb(scsi_req, data_dir, lba, count);
		}

		if (fua)
			scsi_req->cdb[1] |= SKD_FUA_NV;

J
Jens Axboe 已提交
670
		if (!req->bio)
671 672
			goto skip_sg;

673
		if (!skd_preop_sg_list(skdev, skreq)) {
674 675 676 677 678 679 680 681 682
			/*
			 * Complete the native request with error.
			 * Note that the request context is still at the
			 * head of the free list, and that the SoFIT request
			 * was encoded into the FIT msg buffer but the FIT
			 * msg length has not been updated. In short, the
			 * only resource that has been allocated but might
			 * not be used is that the FIT msg could be empty.
			 */
683
			dev_dbg(&skdev->pdev->dev, "error Out\n");
684
			skd_end_request(skdev, skreq, BLK_STS_RESOURCE);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
			continue;
		}

skip_sg:
		scsi_req->hdr.sg_list_len_bytes =
			cpu_to_be32(skreq->sg_byte_count);

		/* Complete resource allocations. */
		skdev->skreq_free_list = skreq->next;
		skreq->state = SKD_REQ_STATE_BUSY;
		skreq->id += SKD_ID_INCR;

		skmsg->length += sizeof(struct skd_scsi_request);
		fmh->num_protocol_cmds_coalesced++;

		/*
		 * Update the active request counts.
		 * Capture the timeout timestamp.
		 */
		skreq->timeout_stamp = skdev->timeout_stamp;
		timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
		skdev->timeout_slot[timo_slot]++;
		skdev->in_flight++;
708 709
		dev_dbg(&skdev->pdev->dev, "req=0x%x busy=%d\n", skreq->id,
			skdev->in_flight);
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

		/*
		 * If the FIT msg buffer is full send it.
		 */
		if (skmsg->length >= SKD_N_FITMSG_BYTES ||
		    fmh->num_protocol_cmds_coalesced >= skd_max_req_per_msg) {
			skd_send_fitmsg(skdev, skmsg);
			skmsg = NULL;
			fmh = NULL;
		}
	}

	/*
	 * Is a FIT msg in progress? If it is empty put the buffer back
	 * on the free list. If it is non-empty send what we got.
	 * This minimizes latency when there are fewer requests than
	 * what fits in a FIT msg.
	 */
	if (skmsg != NULL) {
		/* Bigger than just a FIT msg header? */
		if (skmsg->length > sizeof(struct fit_msg_hdr)) {
731 732
			dev_dbg(&skdev->pdev->dev, "sending msg=%p, len %d\n",
				skmsg, skmsg->length);
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
			skd_send_fitmsg(skdev, skmsg);
		} else {
			/*
			 * The FIT msg is empty. It means we got started
			 * on the msg, but the requests were rejected.
			 */
			skmsg->state = SKD_MSG_STATE_IDLE;
			skmsg->id += SKD_ID_INCR;
			skmsg->next = skdev->skmsg_free_list;
			skdev->skmsg_free_list = skmsg;
		}
		skmsg = NULL;
		fmh = NULL;
	}

	/*
	 * If req is non-NULL it means there is something to do but
	 * we are out of a resource.
	 */
J
Jens Axboe 已提交
752
	if (req)
753
		blk_stop_queue(skdev->queue);
754 755
}

M
Mike Snitzer 已提交
756
static void skd_end_request(struct skd_device *skdev,
757
		struct skd_request_context *skreq, blk_status_t error)
758 759 760 761 762 763 764
{
	if (unlikely(error)) {
		struct request *req = skreq->req;
		char *cmd = (rq_data_dir(req) == READ) ? "read" : "write";
		u32 lba = (u32)blk_rq_pos(req);
		u32 count = blk_rq_sectors(req);

765 766 767
		dev_err(&skdev->pdev->dev,
			"Error cmd=%s sect=%u count=%u id=0x%x\n", cmd, lba,
			count, skreq->id);
768
	} else
769 770
		dev_dbg(&skdev->pdev->dev, "id=0x%x error=%d\n", skreq->id,
			error);
771 772 773 774

	__blk_end_request_all(skreq->req, error);
}

775
static bool skd_preop_sg_list(struct skd_device *skdev,
M
Mike Snitzer 已提交
776
			     struct skd_request_context *skreq)
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
{
	struct request *req = skreq->req;
	int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
	int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
	struct scatterlist *sg = &skreq->sg[0];
	int n_sg;
	int i;

	skreq->sg_byte_count = 0;

	/* SKD_ASSERT(skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD ||
		   skreq->sg_data_dir == SKD_DATA_DIR_CARD_TO_HOST); */

	n_sg = blk_rq_map_sg(skdev->queue, req, sg);
	if (n_sg <= 0)
792
		return false;
793 794 795 796 797 798 799

	/*
	 * Map scatterlist to PCI bus addresses.
	 * Note PCI might change the number of entries.
	 */
	n_sg = pci_map_sg(skdev->pdev, sg, n_sg, pci_dir);
	if (n_sg <= 0)
800
		return false;
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

	SKD_ASSERT(n_sg <= skdev->sgs_per_request);

	skreq->n_sg = n_sg;

	for (i = 0; i < n_sg; i++) {
		struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
		u32 cnt = sg_dma_len(&sg[i]);
		uint64_t dma_addr = sg_dma_address(&sg[i]);

		sgd->control = FIT_SGD_CONTROL_NOT_LAST;
		sgd->byte_count = cnt;
		skreq->sg_byte_count += cnt;
		sgd->host_side_addr = dma_addr;
		sgd->dev_side_addr = 0;
	}

	skreq->sksg_list[n_sg - 1].next_desc_ptr = 0LL;
	skreq->sksg_list[n_sg - 1].control = FIT_SGD_CONTROL_LAST;

	if (unlikely(skdev->dbg_level > 1)) {
822 823 824
		dev_dbg(&skdev->pdev->dev,
			"skreq=%x sksg_list=%p sksg_dma=%llx\n",
			skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
825 826
		for (i = 0; i < n_sg; i++) {
			struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];
827 828 829 830 831

			dev_dbg(&skdev->pdev->dev,
				"  sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
				i, sgd->byte_count, sgd->control,
				sgd->host_side_addr, sgd->next_desc_ptr);
832 833 834
		}
	}

835
	return true;
836 837
}

J
Jens Axboe 已提交
838
static void skd_postop_sg_list(struct skd_device *skdev,
M
Mike Snitzer 已提交
839
			       struct skd_request_context *skreq)
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
{
	int writing = skreq->sg_data_dir == SKD_DATA_DIR_HOST_TO_CARD;
	int pci_dir = writing ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;

	/*
	 * restore the next ptr for next IO request so we
	 * don't have to set it every time.
	 */
	skreq->sksg_list[skreq->n_sg - 1].next_desc_ptr =
		skreq->sksg_dma_address +
		((skreq->n_sg) * sizeof(struct fit_sg_descriptor));
	pci_unmap_sg(skdev->pdev, &skreq->sg[0], skreq->n_sg, pci_dir);
}

static void skd_request_fn_not_online(struct request_queue *q)
{
	struct skd_device *skdev = q->queuedata;

	SKD_ASSERT(skdev->state != SKD_DRVR_STATE_ONLINE);

	skd_log_skdev(skdev, "req_not_online");
	switch (skdev->state) {
	case SKD_DRVR_STATE_PAUSING:
	case SKD_DRVR_STATE_PAUSED:
	case SKD_DRVR_STATE_STARTING:
	case SKD_DRVR_STATE_RESTARTING:
	case SKD_DRVR_STATE_WAIT_BOOT:
	/* In case of starting, we haven't started the queue,
	 * so we can't get here... but requests are
	 * possibly hanging out waiting for us because we
	 * reported the dev/skd0 already.  They'll wait
	 * forever if connect doesn't complete.
	 * What to do??? delay dev/skd0 ??
	 */
	case SKD_DRVR_STATE_BUSY:
	case SKD_DRVR_STATE_BUSY_IMMINENT:
	case SKD_DRVR_STATE_BUSY_ERASE:
	case SKD_DRVR_STATE_DRAINING_TIMEOUT:
		return;

	case SKD_DRVR_STATE_BUSY_SANITIZE:
	case SKD_DRVR_STATE_STOPPING:
	case SKD_DRVR_STATE_SYNCING:
	case SKD_DRVR_STATE_FAULT:
	case SKD_DRVR_STATE_DISAPPEARED:
	default:
		break;
	}

	/* If we get here, terminate all pending block requeusts
	 * with EIO and any scsi pass thru with appropriate sense
	 */

	skd_fail_all_pending(skdev);
}

/*
 *****************************************************************************
 * TIMER
 *****************************************************************************
 */

static void skd_timer_tick_not_online(struct skd_device *skdev);

static void skd_timer_tick(ulong arg)
{
	struct skd_device *skdev = (struct skd_device *)arg;

	u32 timo_slot;
	unsigned long reqflags;
	u32 state;

	if (skdev->state == SKD_DRVR_STATE_FAULT)
		/* The driver has declared fault, and we want it to
		 * stay that way until driver is reloaded.
		 */
		return;

	spin_lock_irqsave(&skdev->lock, reqflags);

	state = SKD_READL(skdev, FIT_STATUS);
	state &= FIT_SR_DRIVE_STATE_MASK;
	if (state != skdev->drive_state)
		skd_isr_fwstate(skdev);

	if (skdev->state != SKD_DRVR_STATE_ONLINE) {
		skd_timer_tick_not_online(skdev);
		goto timer_func_out;
	}
	skdev->timeout_stamp++;
	timo_slot = skdev->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;

	/*
	 * All requests that happened during the previous use of
	 * this slot should be done by now. The previous use was
	 * over 7 seconds ago.
	 */
	if (skdev->timeout_slot[timo_slot] == 0)
		goto timer_func_out;

	/* Something is overdue */
941 942 943 944
	dev_dbg(&skdev->pdev->dev, "found %d timeouts, draining busy=%d\n",
		skdev->timeout_slot[timo_slot], skdev->in_flight);
	dev_err(&skdev->pdev->dev, "Overdue IOs (%d), busy %d\n",
		skdev->timeout_slot[timo_slot], skdev->in_flight);
945 946 947 948

	skdev->timer_countdown = SKD_DRAINING_TIMO;
	skdev->state = SKD_DRVR_STATE_DRAINING_TIMEOUT;
	skdev->timo_slot = timo_slot;
949
	blk_stop_queue(skdev->queue);
950 951 952 953 954 955 956 957 958 959 960 961 962 963

timer_func_out:
	mod_timer(&skdev->timer, (jiffies + HZ));

	spin_unlock_irqrestore(&skdev->lock, reqflags);
}

static void skd_timer_tick_not_online(struct skd_device *skdev)
{
	switch (skdev->state) {
	case SKD_DRVR_STATE_IDLE:
	case SKD_DRVR_STATE_LOAD:
		break;
	case SKD_DRVR_STATE_BUSY_SANITIZE:
964 965 966
		dev_dbg(&skdev->pdev->dev,
			"drive busy sanitize[%x], driver[%x]\n",
			skdev->drive_state, skdev->state);
967 968 969 970 971 972 973 974 975 976 977 978 979
		/* If we've been in sanitize for 3 seconds, we figure we're not
		 * going to get anymore completions, so recover requests now
		 */
		if (skdev->timer_countdown > 0) {
			skdev->timer_countdown--;
			return;
		}
		skd_recover_requests(skdev, 0);
		break;

	case SKD_DRVR_STATE_BUSY:
	case SKD_DRVR_STATE_BUSY_IMMINENT:
	case SKD_DRVR_STATE_BUSY_ERASE:
980 981
		dev_dbg(&skdev->pdev->dev, "busy[%x], countdown=%d\n",
			skdev->state, skdev->timer_countdown);
982 983 984 985
		if (skdev->timer_countdown > 0) {
			skdev->timer_countdown--;
			return;
		}
986 987 988
		dev_dbg(&skdev->pdev->dev,
			"busy[%x], timedout=%d, restarting device.",
			skdev->state, skdev->timer_countdown);
989 990 991 992 993 994 995 996 997 998 999 1000 1001
		skd_restart_device(skdev);
		break;

	case SKD_DRVR_STATE_WAIT_BOOT:
	case SKD_DRVR_STATE_STARTING:
		if (skdev->timer_countdown > 0) {
			skdev->timer_countdown--;
			return;
		}
		/* For now, we fault the drive.  Could attempt resets to
		 * revcover at some point. */
		skdev->state = SKD_DRVR_STATE_FAULT;

1002 1003
		dev_err(&skdev->pdev->dev, "DriveFault Connect Timeout (%x)\n",
			skdev->drive_state);
1004 1005 1006

		/*start the queue so we can respond with error to requests */
		/* wakeup anyone waiting for startup complete */
1007
		blk_start_queue(skdev->queue);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
		skdev->gendisk_on = -1;
		wake_up_interruptible(&skdev->waitq);
		break;

	case SKD_DRVR_STATE_ONLINE:
		/* shouldn't get here. */
		break;

	case SKD_DRVR_STATE_PAUSING:
	case SKD_DRVR_STATE_PAUSED:
		break;

	case SKD_DRVR_STATE_DRAINING_TIMEOUT:
1021 1022 1023 1024 1025
		dev_dbg(&skdev->pdev->dev,
			"draining busy [%d] tick[%d] qdb[%d] tmls[%d]\n",
			skdev->timo_slot, skdev->timer_countdown,
			skdev->in_flight,
			skdev->timeout_slot[skdev->timo_slot]);
1026 1027
		/* if the slot has cleared we can let the I/O continue */
		if (skdev->timeout_slot[skdev->timo_slot] == 0) {
1028 1029
			dev_dbg(&skdev->pdev->dev,
				"Slot drained, starting queue.\n");
1030
			skdev->state = SKD_DRVR_STATE_ONLINE;
1031
			blk_start_queue(skdev->queue);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
			return;
		}
		if (skdev->timer_countdown > 0) {
			skdev->timer_countdown--;
			return;
		}
		skd_restart_device(skdev);
		break;

	case SKD_DRVR_STATE_RESTARTING:
		if (skdev->timer_countdown > 0) {
			skdev->timer_countdown--;
			return;
		}
		/* For now, we fault the drive. Could attempt resets to
		 * revcover at some point. */
		skdev->state = SKD_DRVR_STATE_FAULT;
1049 1050 1051
		dev_err(&skdev->pdev->dev,
			"DriveFault Reconnect Timeout (%x)\n",
			skdev->drive_state);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

		/*
		 * Recovering does two things:
		 * 1. completes IO with error
		 * 2. reclaims dma resources
		 * When is it safe to recover requests?
		 * - if the drive state is faulted
		 * - if the state is still soft reset after out timeout
		 * - if the drive registers are dead (state = FF)
		 * If it is "unsafe", we still need to recover, so we will
		 * disable pci bus mastering and disable our interrupts.
		 */

		if ((skdev->drive_state == FIT_SR_DRIVE_SOFT_RESET) ||
		    (skdev->drive_state == FIT_SR_DRIVE_FAULT) ||
		    (skdev->drive_state == FIT_SR_DRIVE_STATE_MASK))
			/* It never came out of soft reset. Try to
			 * recover the requests and then let them
			 * fail. This is to mitigate hung processes. */
			skd_recover_requests(skdev, 0);
		else {
1073 1074
			dev_err(&skdev->pdev->dev, "Disable BusMaster (%x)\n",
				skdev->drive_state);
1075 1076 1077 1078 1079 1080 1081
			pci_disable_device(skdev->pdev);
			skd_disable_interrupts(skdev);
			skd_recover_requests(skdev, 0);
		}

		/*start the queue so we can respond with error to requests */
		/* wakeup anyone waiting for startup complete */
1082
		blk_start_queue(skdev->queue);
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
		skdev->gendisk_on = -1;
		wake_up_interruptible(&skdev->waitq);
		break;

	case SKD_DRVR_STATE_RESUMING:
	case SKD_DRVR_STATE_STOPPING:
	case SKD_DRVR_STATE_SYNCING:
	case SKD_DRVR_STATE_FAULT:
	case SKD_DRVR_STATE_DISAPPEARED:
	default:
		break;
	}
}

static int skd_start_timer(struct skd_device *skdev)
{
	int rc;

	init_timer(&skdev->timer);
	setup_timer(&skdev->timer, skd_timer_tick, (ulong)skdev);

	rc = mod_timer(&skdev->timer, (jiffies + HZ));
	if (rc)
1106
		dev_err(&skdev->pdev->dev, "failed to start timer %d\n", rc);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
	return rc;
}

static void skd_kill_timer(struct skd_device *skdev)
{
	del_timer_sync(&skdev->timer);
}

/*
 *****************************************************************************
 * IOCTL
 *****************************************************************************
 */
static int skd_ioctl_sg_io(struct skd_device *skdev,
			   fmode_t mode, void __user *argp);
static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
					struct skd_sg_io *sksgio);
static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
				   struct skd_sg_io *sksgio);
static int skd_sg_io_prep_buffering(struct skd_device *skdev,
				    struct skd_sg_io *sksgio);
static int skd_sg_io_copy_buffer(struct skd_device *skdev,
				 struct skd_sg_io *sksgio, int dxfer_dir);
static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
				 struct skd_sg_io *sksgio);
static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio);
static int skd_sg_io_release_skspcl(struct skd_device *skdev,
				    struct skd_sg_io *sksgio);
static int skd_sg_io_put_status(struct skd_device *skdev,
				struct skd_sg_io *sksgio);

static void skd_complete_special(struct skd_device *skdev,
				 volatile struct fit_completion_entry_v1
				 *skcomp,
				 volatile struct fit_comp_error_info *skerr,
				 struct skd_special_context *skspcl);

static int skd_bdev_ioctl(struct block_device *bdev, fmode_t mode,
			  uint cmd_in, ulong arg)
{
1147 1148
	static const int sg_version_num = 30527;
	int rc = 0, timeout;
1149 1150
	struct gendisk *disk = bdev->bd_disk;
	struct skd_device *skdev = disk->private_data;
1151
	int __user *p = (int __user *)arg;
1152

1153 1154 1155
	dev_dbg(&skdev->pdev->dev,
		"%s: CMD[%s] ioctl  mode 0x%x, cmd 0x%x arg %0lx\n",
		disk->disk_name, current->comm, mode, cmd_in, arg);
1156 1157 1158 1159 1160 1161

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	switch (cmd_in) {
	case SG_SET_TIMEOUT:
1162 1163 1164 1165
		rc = get_user(timeout, p);
		if (!rc)
			disk->queue->sg_timeout = clock_t_to_jiffies(timeout);
		break;
1166
	case SG_GET_TIMEOUT:
1167 1168
		rc = jiffies_to_clock_t(disk->queue->sg_timeout);
		break;
1169
	case SG_GET_VERSION_NUM:
1170
		rc = put_user(sg_version_num, p);
1171 1172
		break;
	case SG_IO:
1173
		rc = skd_ioctl_sg_io(skdev, mode, (void __user *)arg);
1174 1175 1176 1177 1178 1179 1180
		break;

	default:
		rc = -ENOTTY;
		break;
	}

1181 1182
	dev_dbg(&skdev->pdev->dev, "%s:  completion rc %d\n", disk->disk_name,
		rc);
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
	return rc;
}

static int skd_ioctl_sg_io(struct skd_device *skdev, fmode_t mode,
			   void __user *argp)
{
	int rc;
	struct skd_sg_io sksgio;

	memset(&sksgio, 0, sizeof(sksgio));
	sksgio.mode = mode;
	sksgio.argp = argp;
	sksgio.iov = &sksgio.no_iov_iov;

	switch (skdev->state) {
	case SKD_DRVR_STATE_ONLINE:
	case SKD_DRVR_STATE_BUSY_IMMINENT:
		break;

	default:
1203
		dev_dbg(&skdev->pdev->dev, "drive not online\n");
1204 1205 1206 1207
		rc = -ENXIO;
		goto out;
	}

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	rc = skd_sg_io_get_and_check_args(skdev, &sksgio);
	if (rc)
		goto out;

	rc = skd_sg_io_obtain_skspcl(skdev, &sksgio);
	if (rc)
		goto out;

	rc = skd_sg_io_prep_buffering(skdev, &sksgio);
	if (rc)
		goto out;

	rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_TO_DEV);
	if (rc)
1222 1223
		goto out;

1224 1225
	rc = skd_sg_io_send_fitmsg(skdev, &sksgio);
	if (rc)
1226 1227
		goto out;

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	rc = skd_sg_io_await(skdev, &sksgio);
	if (rc)
		goto out;

	rc = skd_sg_io_copy_buffer(skdev, &sksgio, SG_DXFER_FROM_DEV);
	if (rc)
		goto out;

	rc = skd_sg_io_put_status(skdev, &sksgio);
	if (rc)
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
		goto out;

	rc = 0;

out:
	skd_sg_io_release_skspcl(skdev, &sksgio);

	if (sksgio.iov != NULL && sksgio.iov != &sksgio.no_iov_iov)
		kfree(sksgio.iov);
	return rc;
}

static int skd_sg_io_get_and_check_args(struct skd_device *skdev,
					struct skd_sg_io *sksgio)
{
	struct sg_io_hdr *sgp = &sksgio->sg;
1254
	int i, __maybe_unused acc;
1255 1256

	if (!access_ok(VERIFY_WRITE, sksgio->argp, sizeof(sg_io_hdr_t))) {
1257 1258
		dev_dbg(&skdev->pdev->dev, "access sg failed %p\n",
			sksgio->argp);
1259 1260 1261 1262
		return -EFAULT;
	}

	if (__copy_from_user(sgp, sksgio->argp, sizeof(sg_io_hdr_t))) {
1263 1264
		dev_dbg(&skdev->pdev->dev, "copy_from_user sg failed %p\n",
			sksgio->argp);
1265 1266 1267 1268
		return -EFAULT;
	}

	if (sgp->interface_id != SG_INTERFACE_ID_ORIG) {
1269 1270
		dev_dbg(&skdev->pdev->dev, "interface_id invalid 0x%x\n",
			sgp->interface_id);
1271 1272 1273 1274
		return -EINVAL;
	}

	if (sgp->cmd_len > sizeof(sksgio->cdb)) {
1275 1276
		dev_dbg(&skdev->pdev->dev, "cmd_len invalid %d\n",
			sgp->cmd_len);
1277 1278 1279 1280
		return -EINVAL;
	}

	if (sgp->iovec_count > 256) {
1281 1282
		dev_dbg(&skdev->pdev->dev, "iovec_count invalid %d\n",
			sgp->iovec_count);
1283 1284 1285 1286
		return -EINVAL;
	}

	if (sgp->dxfer_len > (PAGE_SIZE * SKD_N_SG_PER_SPECIAL)) {
1287 1288
		dev_dbg(&skdev->pdev->dev, "dxfer_len invalid %d\n",
			sgp->dxfer_len);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
		return -EINVAL;
	}

	switch (sgp->dxfer_direction) {
	case SG_DXFER_NONE:
		acc = -1;
		break;

	case SG_DXFER_TO_DEV:
		acc = VERIFY_READ;
		break;

	case SG_DXFER_FROM_DEV:
	case SG_DXFER_TO_FROM_DEV:
		acc = VERIFY_WRITE;
		break;

	default:
1307 1308
		dev_dbg(&skdev->pdev->dev, "dxfer_dir invalid %d\n",
			sgp->dxfer_direction);
1309 1310 1311 1312
		return -EINVAL;
	}

	if (copy_from_user(sksgio->cdb, sgp->cmdp, sgp->cmd_len)) {
1313 1314
		dev_dbg(&skdev->pdev->dev, "copy_from_user cmdp failed %p\n",
			sgp->cmdp);
1315 1316 1317 1318 1319
		return -EFAULT;
	}

	if (sgp->mx_sb_len != 0) {
		if (!access_ok(VERIFY_WRITE, sgp->sbp, sgp->mx_sb_len)) {
1320 1321
			dev_dbg(&skdev->pdev->dev, "access sbp failed %p\n",
				sgp->sbp);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
			return -EFAULT;
		}
	}

	if (sgp->iovec_count == 0) {
		sksgio->iov[0].iov_base = sgp->dxferp;
		sksgio->iov[0].iov_len = sgp->dxfer_len;
		sksgio->iovcnt = 1;
		sksgio->dxfer_len = sgp->dxfer_len;
	} else {
		struct sg_iovec *iov;
		uint nbytes = sizeof(*iov) * sgp->iovec_count;
		size_t iov_data_len;

		iov = kmalloc(nbytes, GFP_KERNEL);
		if (iov == NULL) {
1338 1339
			dev_dbg(&skdev->pdev->dev, "alloc iovec failed %d\n",
				sgp->iovec_count);
1340 1341 1342 1343 1344 1345
			return -ENOMEM;
		}
		sksgio->iov = iov;
		sksgio->iovcnt = sgp->iovec_count;

		if (copy_from_user(iov, sgp->dxferp, nbytes)) {
1346 1347 1348
			dev_dbg(&skdev->pdev->dev,
				"copy_from_user iovec failed %p\n",
				sgp->dxferp);
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
			return -EFAULT;
		}

		/*
		 * Sum up the vecs, making sure they don't overflow
		 */
		iov_data_len = 0;
		for (i = 0; i < sgp->iovec_count; i++) {
			if (iov_data_len + iov[i].iov_len < iov_data_len)
				return -EINVAL;
			iov_data_len += iov[i].iov_len;
		}

		/* SG_IO howto says that the shorter of the two wins */
		if (sgp->dxfer_len < iov_data_len) {
			sksgio->iovcnt = iov_shorten((struct iovec *)iov,
						     sgp->iovec_count,
						     sgp->dxfer_len);
			sksgio->dxfer_len = sgp->dxfer_len;
		} else
			sksgio->dxfer_len = iov_data_len;
	}

	if (sgp->dxfer_direction != SG_DXFER_NONE) {
		struct sg_iovec *iov = sksgio->iov;
		for (i = 0; i < sksgio->iovcnt; i++, iov++) {
			if (!access_ok(acc, iov->iov_base, iov->iov_len)) {
1376 1377 1378
				dev_dbg(&skdev->pdev->dev,
					"access data failed %p/%zd\n",
					iov->iov_base, iov->iov_len);
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
				return -EFAULT;
			}
		}
	}

	return 0;
}

static int skd_sg_io_obtain_skspcl(struct skd_device *skdev,
				   struct skd_sg_io *sksgio)
{
	struct skd_special_context *skspcl = NULL;
	int rc;

M
Mike Snitzer 已提交
1393
	for (;;) {
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
		ulong flags;

		spin_lock_irqsave(&skdev->lock, flags);
		skspcl = skdev->skspcl_free_list;
		if (skspcl != NULL) {
			skdev->skspcl_free_list =
				(struct skd_special_context *)skspcl->req.next;
			skspcl->req.id += SKD_ID_INCR;
			skspcl->req.state = SKD_REQ_STATE_SETUP;
			skspcl->orphaned = 0;
			skspcl->req.n_sg = 0;
		}
		spin_unlock_irqrestore(&skdev->lock, flags);

		if (skspcl != NULL) {
			rc = 0;
			break;
		}

1413
		dev_dbg(&skdev->pdev->dev, "blocking\n");
1414 1415 1416 1417 1418 1419

		rc = wait_event_interruptible_timeout(
				skdev->waitq,
				(skdev->skspcl_free_list != NULL),
				msecs_to_jiffies(sksgio->sg.timeout));

1420
		dev_dbg(&skdev->pdev->dev, "unblocking, rc=%d\n", rc);
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

		if (rc <= 0) {
			if (rc == 0)
				rc = -ETIMEDOUT;
			else
				rc = -EINTR;
			break;
		}
		/*
		 * If we get here rc > 0 meaning the timeout to
		 * wait_event_interruptible_timeout() had time left, hence the
		 * sought event -- non-empty free list -- happened.
		 * Retry the allocation.
		 */
	}
	sksgio->skspcl = skspcl;

	return rc;
}

static int skd_skreq_prep_buffering(struct skd_device *skdev,
				    struct skd_request_context *skreq,
				    u32 dxfer_len)
{
	u32 resid = dxfer_len;

	/*
	 * The DMA engine must have aligned addresses and byte counts.
	 */
	resid += (-resid) & 3;
	skreq->sg_byte_count = resid;

	skreq->n_sg = 0;

	while (resid > 0) {
		u32 nbytes = PAGE_SIZE;
		u32 ix = skreq->n_sg;
		struct scatterlist *sg = &skreq->sg[ix];
		struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];
		struct page *page;

		if (nbytes > resid)
			nbytes = resid;

		page = alloc_page(GFP_KERNEL);
		if (page == NULL)
			return -ENOMEM;

		sg_set_page(sg, page, nbytes, 0);

		/* TODO: This should be going through a pci_???()
		 * routine to do proper mapping. */
		sksg->control = FIT_SGD_CONTROL_NOT_LAST;
		sksg->byte_count = nbytes;

		sksg->host_side_addr = sg_phys(sg);

		sksg->dev_side_addr = 0;
		sksg->next_desc_ptr = skreq->sksg_dma_address +
				      (ix + 1) * sizeof(*sksg);

		skreq->n_sg++;
		resid -= nbytes;
	}

	if (skreq->n_sg > 0) {
		u32 ix = skreq->n_sg - 1;
		struct fit_sg_descriptor *sksg = &skreq->sksg_list[ix];

		sksg->control = FIT_SGD_CONTROL_LAST;
		sksg->next_desc_ptr = 0;
	}

	if (unlikely(skdev->dbg_level > 1)) {
		u32 i;

1497 1498 1499
		dev_dbg(&skdev->pdev->dev,
			"skreq=%x sksg_list=%p sksg_dma=%llx\n",
			skreq->id, skreq->sksg_list, skreq->sksg_dma_address);
1500 1501 1502
		for (i = 0; i < skreq->n_sg; i++) {
			struct fit_sg_descriptor *sgd = &skreq->sksg_list[i];

1503 1504 1505 1506
			dev_dbg(&skdev->pdev->dev,
				"  sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
				i, sgd->byte_count, sgd->control,
				sgd->host_side_addr, sgd->next_desc_ptr);
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
		}
	}

	return 0;
}

static int skd_sg_io_prep_buffering(struct skd_device *skdev,
				    struct skd_sg_io *sksgio)
{
	struct skd_special_context *skspcl = sksgio->skspcl;
	struct skd_request_context *skreq = &skspcl->req;
	u32 dxfer_len = sksgio->dxfer_len;
	int rc;

	rc = skd_skreq_prep_buffering(skdev, skreq, dxfer_len);
	/*
	 * Eventually, errors or not, skd_release_special() is called
	 * to recover allocations including partial allocations.
	 */
	return rc;
}

static int skd_sg_io_copy_buffer(struct skd_device *skdev,
				 struct skd_sg_io *sksgio, int dxfer_dir)
{
	struct skd_special_context *skspcl = sksgio->skspcl;
	u32 iov_ix = 0;
	struct sg_iovec curiov;
	u32 sksg_ix = 0;
	u8 *bufp = NULL;
	u32 buf_len = 0;
	u32 resid = sksgio->dxfer_len;
	int rc;

	curiov.iov_len = 0;
	curiov.iov_base = NULL;

	if (dxfer_dir != sksgio->sg.dxfer_direction) {
		if (dxfer_dir != SG_DXFER_TO_DEV ||
		    sksgio->sg.dxfer_direction != SG_DXFER_TO_FROM_DEV)
			return 0;
	}

	while (resid > 0) {
		u32 nbytes = PAGE_SIZE;

		if (curiov.iov_len == 0) {
			curiov = sksgio->iov[iov_ix++];
			continue;
		}

		if (buf_len == 0) {
			struct page *page;
			page = sg_page(&skspcl->req.sg[sksg_ix++]);
			bufp = page_address(page);
			buf_len = PAGE_SIZE;
		}

		nbytes = min_t(u32, nbytes, resid);
		nbytes = min_t(u32, nbytes, curiov.iov_len);
		nbytes = min_t(u32, nbytes, buf_len);

		if (dxfer_dir == SG_DXFER_TO_DEV)
			rc = __copy_from_user(bufp, curiov.iov_base, nbytes);
		else
			rc = __copy_to_user(curiov.iov_base, bufp, nbytes);

		if (rc)
			return -EFAULT;

		resid -= nbytes;
		curiov.iov_len -= nbytes;
		curiov.iov_base += nbytes;
		buf_len -= nbytes;
	}

	return 0;
}

static int skd_sg_io_send_fitmsg(struct skd_device *skdev,
				 struct skd_sg_io *sksgio)
{
	struct skd_special_context *skspcl = sksgio->skspcl;
	struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
	struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];

	memset(skspcl->msg_buf, 0, SKD_N_SPECIAL_FITMSG_BYTES);

	/* Initialize the FIT msg header */
	fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
	fmh->num_protocol_cmds_coalesced = 1;

	/* Initialize the SCSI request */
	if (sksgio->sg.dxfer_direction != SG_DXFER_NONE)
		scsi_req->hdr.sg_list_dma_address =
			cpu_to_be64(skspcl->req.sksg_dma_address);
	scsi_req->hdr.tag = skspcl->req.id;
	scsi_req->hdr.sg_list_len_bytes =
		cpu_to_be32(skspcl->req.sg_byte_count);
	memcpy(scsi_req->cdb, sksgio->cdb, sizeof(scsi_req->cdb));

	skspcl->req.state = SKD_REQ_STATE_BUSY;
	skd_send_special_fitmsg(skdev, skspcl);

	return 0;
}

static int skd_sg_io_await(struct skd_device *skdev, struct skd_sg_io *sksgio)
{
	unsigned long flags;
	int rc;

	rc = wait_event_interruptible_timeout(skdev->waitq,
					      (sksgio->skspcl->req.state !=
					       SKD_REQ_STATE_BUSY),
					      msecs_to_jiffies(sksgio->sg.
							       timeout));

	spin_lock_irqsave(&skdev->lock, flags);

	if (sksgio->skspcl->req.state == SKD_REQ_STATE_ABORTED) {
1628 1629
		dev_dbg(&skdev->pdev->dev, "skspcl %p aborted\n",
			sksgio->skspcl);
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

		/* Build check cond, sense and let command finish. */
		/* For a timeout, we must fabricate completion and sense
		 * data to complete the command */
		sksgio->skspcl->req.completion.status =
			SAM_STAT_CHECK_CONDITION;

		memset(&sksgio->skspcl->req.err_info, 0,
		       sizeof(sksgio->skspcl->req.err_info));
		sksgio->skspcl->req.err_info.type = 0x70;
		sksgio->skspcl->req.err_info.key = ABORTED_COMMAND;
		sksgio->skspcl->req.err_info.code = 0x44;
		sksgio->skspcl->req.err_info.qual = 0;
		rc = 0;
	} else if (sksgio->skspcl->req.state != SKD_REQ_STATE_BUSY)
		/* No longer on the adapter. We finish. */
		rc = 0;
	else {
		/* Something's gone wrong. Still busy. Timeout or
		 * user interrupted (control-C). Mark as an orphan
		 * so it will be disposed when completed. */
		sksgio->skspcl->orphaned = 1;
		sksgio->skspcl = NULL;
		if (rc == 0) {
1654 1655
			dev_dbg(&skdev->pdev->dev, "timed out %p (%u ms)\n",
				sksgio, sksgio->sg.timeout);
1656 1657
			rc = -ETIMEDOUT;
		} else {
1658
			dev_dbg(&skdev->pdev->dev, "cntlc %p\n", sksgio);
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
			rc = -EINTR;
		}
	}

	spin_unlock_irqrestore(&skdev->lock, flags);

	return rc;
}

static int skd_sg_io_put_status(struct skd_device *skdev,
				struct skd_sg_io *sksgio)
{
	struct sg_io_hdr *sgp = &sksgio->sg;
	struct skd_special_context *skspcl = sksgio->skspcl;
	int resid = 0;

	u32 nb = be32_to_cpu(skspcl->req.completion.num_returned_bytes);

	sgp->status = skspcl->req.completion.status;
	resid = sksgio->dxfer_len - nb;

	sgp->masked_status = sgp->status & STATUS_MASK;
	sgp->msg_status = 0;
	sgp->host_status = 0;
	sgp->driver_status = 0;
	sgp->resid = resid;
	if (sgp->masked_status || sgp->host_status || sgp->driver_status)
		sgp->info |= SG_INFO_CHECK;

1688 1689
	dev_dbg(&skdev->pdev->dev, "status %x masked %x resid 0x%x\n",
		sgp->status, sgp->masked_status, sgp->resid);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

	if (sgp->masked_status == SAM_STAT_CHECK_CONDITION) {
		if (sgp->mx_sb_len > 0) {
			struct fit_comp_error_info *ei = &skspcl->req.err_info;
			u32 nbytes = sizeof(*ei);

			nbytes = min_t(u32, nbytes, sgp->mx_sb_len);

			sgp->sb_len_wr = nbytes;

			if (__copy_to_user(sgp->sbp, ei, nbytes)) {
1701 1702 1703
				dev_dbg(&skdev->pdev->dev,
					"copy_to_user sense failed %p\n",
					sgp->sbp);
1704 1705 1706 1707 1708 1709
				return -EFAULT;
			}
		}
	}

	if (__copy_to_user(sksgio->argp, sgp, sizeof(sg_io_hdr_t))) {
1710 1711
		dev_dbg(&skdev->pdev->dev, "copy_to_user sg failed %p\n",
			sksgio->argp);
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
		return -EFAULT;
	}

	return 0;
}

static int skd_sg_io_release_skspcl(struct skd_device *skdev,
				    struct skd_sg_io *sksgio)
{
	struct skd_special_context *skspcl = sksgio->skspcl;

	if (skspcl != NULL) {
		ulong flags;

		sksgio->skspcl = NULL;

		spin_lock_irqsave(&skdev->lock, flags);
		skd_release_special(skdev, skspcl);
		spin_unlock_irqrestore(&skdev->lock, flags);
	}

	return 0;
}

/*
 *****************************************************************************
 * INTERNAL REQUESTS -- generated by driver itself
 *****************************************************************************
 */

static int skd_format_internal_skspcl(struct skd_device *skdev)
{
	struct skd_special_context *skspcl = &skdev->internal_skspcl;
	struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
	struct fit_msg_hdr *fmh;
	uint64_t dma_address;
	struct skd_scsi_request *scsi;

	fmh = (struct fit_msg_hdr *)&skspcl->msg_buf[0];
	fmh->protocol_id = FIT_PROTOCOL_ID_SOFIT;
	fmh->num_protocol_cmds_coalesced = 1;

	scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
	memset(scsi, 0, sizeof(*scsi));
	dma_address = skspcl->req.sksg_dma_address;
	scsi->hdr.sg_list_dma_address = cpu_to_be64(dma_address);
	sgd->control = FIT_SGD_CONTROL_LAST;
	sgd->byte_count = 0;
	sgd->host_side_addr = skspcl->db_dma_address;
	sgd->dev_side_addr = 0;
	sgd->next_desc_ptr = 0LL;

	return 1;
}

#define WR_BUF_SIZE SKD_N_INTERNAL_BYTES

static void skd_send_internal_skspcl(struct skd_device *skdev,
				     struct skd_special_context *skspcl,
				     u8 opcode)
{
	struct fit_sg_descriptor *sgd = &skspcl->req.sksg_list[0];
	struct skd_scsi_request *scsi;
	unsigned char *buf = skspcl->data_buf;
	int i;

	if (skspcl->req.state != SKD_REQ_STATE_IDLE)
		/*
		 * A refresh is already in progress.
		 * Just wait for it to finish.
		 */
		return;

	SKD_ASSERT((skspcl->req.id & SKD_ID_INCR) == 0);
	skspcl->req.state = SKD_REQ_STATE_BUSY;
	skspcl->req.id += SKD_ID_INCR;

	scsi = (struct skd_scsi_request *)&skspcl->msg_buf[64];
	scsi->hdr.tag = skspcl->req.id;

	memset(scsi->cdb, 0, sizeof(scsi->cdb));

	switch (opcode) {
	case TEST_UNIT_READY:
		scsi->cdb[0] = TEST_UNIT_READY;
		sgd->byte_count = 0;
		scsi->hdr.sg_list_len_bytes = 0;
		break;

	case READ_CAPACITY:
		scsi->cdb[0] = READ_CAPACITY;
		sgd->byte_count = SKD_N_READ_CAP_BYTES;
		scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
		break;

	case INQUIRY:
		scsi->cdb[0] = INQUIRY;
		scsi->cdb[1] = 0x01;    /* evpd */
		scsi->cdb[2] = 0x80;    /* serial number page */
		scsi->cdb[4] = 0x10;
		sgd->byte_count = 16;
		scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
		break;

	case SYNCHRONIZE_CACHE:
		scsi->cdb[0] = SYNCHRONIZE_CACHE;
		sgd->byte_count = 0;
		scsi->hdr.sg_list_len_bytes = 0;
		break;

	case WRITE_BUFFER:
		scsi->cdb[0] = WRITE_BUFFER;
		scsi->cdb[1] = 0x02;
		scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
		scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
		sgd->byte_count = WR_BUF_SIZE;
		scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
		/* fill incrementing byte pattern */
		for (i = 0; i < sgd->byte_count; i++)
			buf[i] = i & 0xFF;
		break;

	case READ_BUFFER:
		scsi->cdb[0] = READ_BUFFER;
		scsi->cdb[1] = 0x02;
		scsi->cdb[7] = (WR_BUF_SIZE & 0xFF00) >> 8;
		scsi->cdb[8] = WR_BUF_SIZE & 0xFF;
		sgd->byte_count = WR_BUF_SIZE;
		scsi->hdr.sg_list_len_bytes = cpu_to_be32(sgd->byte_count);
		memset(skspcl->data_buf, 0, sgd->byte_count);
		break;

	default:
		SKD_ASSERT("Don't know what to send");
		return;

	}
	skd_send_special_fitmsg(skdev, skspcl);
}

static void skd_refresh_device_data(struct skd_device *skdev)
{
	struct skd_special_context *skspcl = &skdev->internal_skspcl;

	skd_send_internal_skspcl(skdev, skspcl, TEST_UNIT_READY);
}

static int skd_chk_read_buf(struct skd_device *skdev,
			    struct skd_special_context *skspcl)
{
	unsigned char *buf = skspcl->data_buf;
	int i;

	/* check for incrementing byte pattern */
	for (i = 0; i < WR_BUF_SIZE; i++)
		if (buf[i] != (i & 0xFF))
			return 1;

	return 0;
}

static void skd_log_check_status(struct skd_device *skdev, u8 status, u8 key,
				 u8 code, u8 qual, u8 fruc)
{
	/* If the check condition is of special interest, log a message */
	if ((status == SAM_STAT_CHECK_CONDITION) && (key == 0x02)
	    && (code == 0x04) && (qual == 0x06)) {
1879 1880 1881
		dev_err(&skdev->pdev->dev,
			"*** LOST_WRITE_DATA ERROR *** key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
			key, code, qual, fruc);
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
	}
}

static void skd_complete_internal(struct skd_device *skdev,
				  volatile struct fit_completion_entry_v1
				  *skcomp,
				  volatile struct fit_comp_error_info *skerr,
				  struct skd_special_context *skspcl)
{
	u8 *buf = skspcl->data_buf;
	u8 status;
	int i;
	struct skd_scsi_request *scsi =
		(struct skd_scsi_request *)&skspcl->msg_buf[64];

	SKD_ASSERT(skspcl == &skdev->internal_skspcl);

1899
	dev_dbg(&skdev->pdev->dev, "complete internal %x\n", scsi->cdb[0]);
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

	skspcl->req.completion = *skcomp;
	skspcl->req.state = SKD_REQ_STATE_IDLE;
	skspcl->req.id += SKD_ID_INCR;

	status = skspcl->req.completion.status;

	skd_log_check_status(skdev, status, skerr->key, skerr->code,
			     skerr->qual, skerr->fruc);

	switch (scsi->cdb[0]) {
	case TEST_UNIT_READY:
		if (status == SAM_STAT_GOOD)
			skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
		else if ((status == SAM_STAT_CHECK_CONDITION) &&
			 (skerr->key == MEDIUM_ERROR))
			skd_send_internal_skspcl(skdev, skspcl, WRITE_BUFFER);
		else {
			if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1919 1920 1921
				dev_dbg(&skdev->pdev->dev,
					"TUR failed, don't send anymore state 0x%x\n",
					skdev->state);
1922 1923
				return;
			}
1924 1925
			dev_dbg(&skdev->pdev->dev,
				"**** TUR failed, retry skerr\n");
1926 1927 1928 1929 1930 1931 1932 1933 1934
			skd_send_internal_skspcl(skdev, skspcl, 0x00);
		}
		break;

	case WRITE_BUFFER:
		if (status == SAM_STAT_GOOD)
			skd_send_internal_skspcl(skdev, skspcl, READ_BUFFER);
		else {
			if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1935 1936 1937
				dev_dbg(&skdev->pdev->dev,
					"write buffer failed, don't send anymore state 0x%x\n",
					skdev->state);
1938 1939
				return;
			}
1940 1941
			dev_dbg(&skdev->pdev->dev,
				"**** write buffer failed, retry skerr\n");
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
			skd_send_internal_skspcl(skdev, skspcl, 0x00);
		}
		break;

	case READ_BUFFER:
		if (status == SAM_STAT_GOOD) {
			if (skd_chk_read_buf(skdev, skspcl) == 0)
				skd_send_internal_skspcl(skdev, skspcl,
							 READ_CAPACITY);
			else {
1952 1953 1954
				dev_err(&skdev->pdev->dev,
					"*** W/R Buffer mismatch %d ***\n",
					skdev->connect_retries);
1955 1956 1957 1958 1959
				if (skdev->connect_retries <
				    SKD_MAX_CONNECT_RETRIES) {
					skdev->connect_retries++;
					skd_soft_reset(skdev);
				} else {
1960 1961
					dev_err(&skdev->pdev->dev,
						"W/R Buffer Connect Error\n");
1962 1963 1964 1965 1966 1967
					return;
				}
			}

		} else {
			if (skdev->state == SKD_DRVR_STATE_STOPPING) {
1968 1969 1970
				dev_dbg(&skdev->pdev->dev,
					"read buffer failed, don't send anymore state 0x%x\n",
					skdev->state);
1971 1972
				return;
			}
1973 1974
			dev_dbg(&skdev->pdev->dev,
				"**** read buffer failed, retry skerr\n");
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
			skd_send_internal_skspcl(skdev, skspcl, 0x00);
		}
		break;

	case READ_CAPACITY:
		skdev->read_cap_is_valid = 0;
		if (status == SAM_STAT_GOOD) {
			skdev->read_cap_last_lba =
				(buf[0] << 24) | (buf[1] << 16) |
				(buf[2] << 8) | buf[3];
			skdev->read_cap_blocksize =
				(buf[4] << 24) | (buf[5] << 16) |
				(buf[6] << 8) | buf[7];

1989 1990 1991
			dev_dbg(&skdev->pdev->dev, "last lba %d, bs %d\n",
				skdev->read_cap_last_lba,
				skdev->read_cap_blocksize);
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001

			set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);

			skdev->read_cap_is_valid = 1;

			skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
		} else if ((status == SAM_STAT_CHECK_CONDITION) &&
			   (skerr->key == MEDIUM_ERROR)) {
			skdev->read_cap_last_lba = ~0;
			set_capacity(skdev->disk, skdev->read_cap_last_lba + 1);
2002
			dev_dbg(&skdev->pdev->dev, "**** MEDIUM ERROR caused READCAP to fail, ignore failure and continue to inquiry\n");
2003 2004
			skd_send_internal_skspcl(skdev, skspcl, INQUIRY);
		} else {
2005
			dev_dbg(&skdev->pdev->dev, "**** READCAP failed, retry TUR\n");
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
			skd_send_internal_skspcl(skdev, skspcl,
						 TEST_UNIT_READY);
		}
		break;

	case INQUIRY:
		skdev->inquiry_is_valid = 0;
		if (status == SAM_STAT_GOOD) {
			skdev->inquiry_is_valid = 1;

			for (i = 0; i < 12; i++)
				skdev->inq_serial_num[i] = buf[i + 4];
			skdev->inq_serial_num[12] = 0;
		}

		if (skd_unquiesce_dev(skdev) < 0)
2022
			dev_dbg(&skdev->pdev->dev, "**** failed, to ONLINE device\n");
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
		 /* connection is complete */
		skdev->connect_retries = 0;
		break;

	case SYNCHRONIZE_CACHE:
		if (status == SAM_STAT_GOOD)
			skdev->sync_done = 1;
		else
			skdev->sync_done = -1;
		wake_up_interruptible(&skdev->waitq);
		break;

	default:
		SKD_ASSERT("we didn't send this");
	}
}

/*
 *****************************************************************************
 * FIT MESSAGES
 *****************************************************************************
 */

static void skd_send_fitmsg(struct skd_device *skdev,
			    struct skd_fitmsg_context *skmsg)
{
	u64 qcmd;
	struct fit_msg_hdr *fmh;

2052 2053 2054 2055
	dev_dbg(&skdev->pdev->dev, "dma address 0x%llx, busy=%d\n",
		skmsg->mb_dma_address, skdev->in_flight);
	dev_dbg(&skdev->pdev->dev, "msg_buf 0x%p, offset %x\n", skmsg->msg_buf,
		skmsg->offset);
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066

	qcmd = skmsg->mb_dma_address;
	qcmd |= FIT_QCMD_QID_NORMAL;

	fmh = (struct fit_msg_hdr *)skmsg->msg_buf;
	skmsg->outstanding = fmh->num_protocol_cmds_coalesced;

	if (unlikely(skdev->dbg_level > 1)) {
		u8 *bp = (u8 *)skmsg->msg_buf;
		int i;
		for (i = 0; i < skmsg->length; i += 8) {
2067 2068
			dev_dbg(&skdev->pdev->dev, "msg[%2d] %8ph\n", i,
				&bp[i]);
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
			if (i == 0)
				i = 64 - 8;
		}
	}

	if (skmsg->length > 256)
		qcmd |= FIT_QCMD_MSGSIZE_512;
	else if (skmsg->length > 128)
		qcmd |= FIT_QCMD_MSGSIZE_256;
	else if (skmsg->length > 64)
		qcmd |= FIT_QCMD_MSGSIZE_128;
	else
		/*
		 * This makes no sense because the FIT msg header is
		 * 64 bytes. If the msg is only 64 bytes long it has
		 * no payload.
		 */
		qcmd |= FIT_QCMD_MSGSIZE_64;

2088 2089 2090
	/* Make sure skd_msg_buf is written before the doorbell is triggered. */
	smp_wmb();

2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
}

static void skd_send_special_fitmsg(struct skd_device *skdev,
				    struct skd_special_context *skspcl)
{
	u64 qcmd;

	if (unlikely(skdev->dbg_level > 1)) {
		u8 *bp = (u8 *)skspcl->msg_buf;
		int i;

		for (i = 0; i < SKD_N_SPECIAL_FITMSG_BYTES; i += 8) {
2104 2105
			dev_dbg(&skdev->pdev->dev, " spcl[%2d] %8ph\n", i,
				&bp[i]);
2106 2107 2108 2109
			if (i == 0)
				i = 64 - 8;
		}

2110 2111 2112 2113
		dev_dbg(&skdev->pdev->dev,
			"skspcl=%p id=%04x sksg_list=%p sksg_dma=%llx\n",
			skspcl, skspcl->req.id, skspcl->req.sksg_list,
			skspcl->req.sksg_dma_address);
2114 2115 2116 2117
		for (i = 0; i < skspcl->req.n_sg; i++) {
			struct fit_sg_descriptor *sgd =
				&skspcl->req.sksg_list[i];

2118 2119 2120 2121
			dev_dbg(&skdev->pdev->dev,
				"  sg[%d] count=%u ctrl=0x%x addr=0x%llx next=0x%llx\n",
				i, sgd->byte_count, sgd->control,
				sgd->host_side_addr, sgd->next_desc_ptr);
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
		}
	}

	/*
	 * Special FIT msgs are always 128 bytes: a 64-byte FIT hdr
	 * and one 64-byte SSDI command.
	 */
	qcmd = skspcl->mb_dma_address;
	qcmd |= FIT_QCMD_QID_NORMAL + FIT_QCMD_MSGSIZE_128;

2132 2133 2134
	/* Make sure skd_msg_buf is written before the doorbell is triggered. */
	smp_wmb();

2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
	SKD_WRITEQ(skdev, qcmd, FIT_Q_COMMAND);
}

/*
 *****************************************************************************
 * COMPLETION QUEUE
 *****************************************************************************
 */

static void skd_complete_other(struct skd_device *skdev,
			       volatile struct fit_completion_entry_v1 *skcomp,
			       volatile struct fit_comp_error_info *skerr);

struct sns_info {
	u8 type;
	u8 stat;
	u8 key;
	u8 asc;
	u8 ascq;
	u8 mask;
	enum skd_check_status_action action;
};

static struct sns_info skd_chkstat_table[] = {
	/* Good */
	{ 0x70, 0x02, RECOVERED_ERROR, 0,    0,	   0x1c,
	  SKD_CHECK_STATUS_REPORT_GOOD },

	/* Smart alerts */
	{ 0x70, 0x02, NO_SENSE,	       0x0B, 0x00, 0x1E,	/* warnings */
	  SKD_CHECK_STATUS_REPORT_SMART_ALERT },
	{ 0x70, 0x02, NO_SENSE,	       0x5D, 0x00, 0x1E,	/* thresholds */
	  SKD_CHECK_STATUS_REPORT_SMART_ALERT },
	{ 0x70, 0x02, RECOVERED_ERROR, 0x0B, 0x01, 0x1F,        /* temperature over trigger */
	  SKD_CHECK_STATUS_REPORT_SMART_ALERT },

	/* Retry (with limits) */
	{ 0x70, 0x02, 0x0B,	       0,    0,	   0x1C,        /* This one is for DMA ERROR */
	  SKD_CHECK_STATUS_REQUEUE_REQUEST },
	{ 0x70, 0x02, 0x06,	       0x0B, 0x00, 0x1E,        /* warnings */
	  SKD_CHECK_STATUS_REQUEUE_REQUEST },
	{ 0x70, 0x02, 0x06,	       0x5D, 0x00, 0x1E,        /* thresholds */
	  SKD_CHECK_STATUS_REQUEUE_REQUEST },
	{ 0x70, 0x02, 0x06,	       0x80, 0x30, 0x1F,        /* backup power */
	  SKD_CHECK_STATUS_REQUEUE_REQUEST },

	/* Busy (or about to be) */
	{ 0x70, 0x02, 0x06,	       0x3f, 0x01, 0x1F, /* fw changed */
	  SKD_CHECK_STATUS_BUSY_IMMINENT },
};

/*
 * Look up status and sense data to decide how to handle the error
 * from the device.
 * mask says which fields must match e.g., mask=0x18 means check
 * type and stat, ignore key, asc, ascq.
 */

M
Mike Snitzer 已提交
2193 2194 2195
static enum skd_check_status_action
skd_check_status(struct skd_device *skdev,
		 u8 cmp_status, volatile struct fit_comp_error_info *skerr)
2196 2197 2198
{
	int i, n;

2199 2200
	dev_err(&skdev->pdev->dev, "key/asc/ascq/fruc %02x/%02x/%02x/%02x\n",
		skerr->key, skerr->code, skerr->qual, skerr->fruc);
2201

2202 2203 2204 2205
	dev_dbg(&skdev->pdev->dev,
		"stat: t=%02x stat=%02x k=%02x c=%02x q=%02x fruc=%02x\n",
		skerr->type, cmp_status, skerr->key, skerr->code, skerr->qual,
		skerr->fruc);
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232

	/* Does the info match an entry in the good category? */
	n = sizeof(skd_chkstat_table) / sizeof(skd_chkstat_table[0]);
	for (i = 0; i < n; i++) {
		struct sns_info *sns = &skd_chkstat_table[i];

		if (sns->mask & 0x10)
			if (skerr->type != sns->type)
				continue;

		if (sns->mask & 0x08)
			if (cmp_status != sns->stat)
				continue;

		if (sns->mask & 0x04)
			if (skerr->key != sns->key)
				continue;

		if (sns->mask & 0x02)
			if (skerr->code != sns->asc)
				continue;

		if (sns->mask & 0x01)
			if (skerr->qual != sns->ascq)
				continue;

		if (sns->action == SKD_CHECK_STATUS_REPORT_SMART_ALERT) {
2233 2234 2235
			dev_err(&skdev->pdev->dev,
				"SMART Alert: sense key/asc/ascq %02x/%02x/%02x\n",
				skerr->key, skerr->code, skerr->qual);
2236 2237 2238 2239 2240 2241 2242 2243
		}
		return sns->action;
	}

	/* No other match, so nonzero status means error,
	 * zero status means good
	 */
	if (cmp_status) {
2244
		dev_dbg(&skdev->pdev->dev, "status check: error\n");
2245 2246 2247
		return SKD_CHECK_STATUS_REPORT_ERROR;
	}

2248
	dev_dbg(&skdev->pdev->dev, "status check good default\n");
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
	return SKD_CHECK_STATUS_REPORT_GOOD;
}

static void skd_resolve_req_exception(struct skd_device *skdev,
				      struct skd_request_context *skreq)
{
	u8 cmp_status = skreq->completion.status;

	switch (skd_check_status(skdev, cmp_status, &skreq->err_info)) {
	case SKD_CHECK_STATUS_REPORT_GOOD:
	case SKD_CHECK_STATUS_REPORT_SMART_ALERT:
2260
		skd_end_request(skdev, skreq, BLK_STS_OK);
2261 2262 2263 2264
		break;

	case SKD_CHECK_STATUS_BUSY_IMMINENT:
		skd_log_skreq(skdev, skreq, "retry(busy)");
M
Mike Snitzer 已提交
2265
		blk_requeue_request(skdev->queue, skreq->req);
2266
		dev_info(&skdev->pdev->dev, "drive BUSY imminent\n");
2267 2268 2269 2270 2271 2272
		skdev->state = SKD_DRVR_STATE_BUSY_IMMINENT;
		skdev->timer_countdown = SKD_TIMER_MINUTES(20);
		skd_quiesce_dev(skdev);
		break;

	case SKD_CHECK_STATUS_REQUEUE_REQUEST:
J
Jens Axboe 已提交
2273 2274
		if ((unsigned long) ++skreq->req->special < SKD_MAX_RETRIES) {
			skd_log_skreq(skdev, skreq, "retry");
M
Mike Snitzer 已提交
2275
			blk_requeue_request(skdev->queue, skreq->req);
J
Jens Axboe 已提交
2276
			break;
2277
		}
2278
		/* fall through */
2279 2280 2281

	case SKD_CHECK_STATUS_REPORT_ERROR:
	default:
2282
		skd_end_request(skdev, skreq, BLK_STS_IOERR);
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
		break;
	}
}

/* assume spinlock is already held */
static void skd_release_skreq(struct skd_device *skdev,
			      struct skd_request_context *skreq)
{
	u32 msg_slot;
	struct skd_fitmsg_context *skmsg;

	u32 timo_slot;

	/*
	 * Reclaim the FIT msg buffer if this is
	 * the first of the requests it carried to
	 * be completed. The FIT msg buffer used to
	 * send this request cannot be reused until
	 * we are sure the s1120 card has copied
	 * it to its memory. The FIT msg might have
	 * contained several requests. As soon as
	 * any of them are completed we know that
	 * the entire FIT msg was transferred.
	 * Only the first completed request will
	 * match the FIT msg buffer id. The FIT
	 * msg buffer id is immediately updated.
	 * When subsequent requests complete the FIT
	 * msg buffer id won't match, so we know
	 * quite cheaply that it is already done.
	 */
	msg_slot = skreq->fitmsg_id & SKD_ID_SLOT_MASK;
	SKD_ASSERT(msg_slot < skdev->num_fitmsg_context);

	skmsg = &skdev->skmsg_table[msg_slot];
	if (skmsg->id == skreq->fitmsg_id) {
		SKD_ASSERT(skmsg->state == SKD_MSG_STATE_BUSY);
		SKD_ASSERT(skmsg->outstanding > 0);
		skmsg->outstanding--;
		if (skmsg->outstanding == 0) {
			skmsg->state = SKD_MSG_STATE_IDLE;
			skmsg->id += SKD_ID_INCR;
			skmsg->next = skdev->skmsg_free_list;
			skdev->skmsg_free_list = skmsg;
		}
	}

	/*
	 * Decrease the number of active requests.
	 * Also decrements the count in the timeout slot.
	 */
	SKD_ASSERT(skdev->in_flight > 0);
	skdev->in_flight -= 1;

	timo_slot = skreq->timeout_stamp & SKD_TIMEOUT_SLOT_MASK;
	SKD_ASSERT(skdev->timeout_slot[timo_slot] > 0);
	skdev->timeout_slot[timo_slot] -= 1;

	/*
	 * Reset backpointer
	 */
J
Jens Axboe 已提交
2343
	skreq->req = NULL;
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365

	/*
	 * Reclaim the skd_request_context
	 */
	skreq->state = SKD_REQ_STATE_IDLE;
	skreq->id += SKD_ID_INCR;
	skreq->next = skdev->skreq_free_list;
	skdev->skreq_free_list = skreq;
}

#define DRIVER_INQ_EVPD_PAGE_CODE   0xDA

static void skd_do_inq_page_00(struct skd_device *skdev,
			       volatile struct fit_completion_entry_v1 *skcomp,
			       volatile struct fit_comp_error_info *skerr,
			       uint8_t *cdb, uint8_t *buf)
{
	uint16_t insert_pt, max_bytes, drive_pages, drive_bytes, new_size;

	/* Caller requested "supported pages".  The driver needs to insert
	 * its page.
	 */
2366 2367
	dev_dbg(&skdev->pdev->dev,
		"skd_do_driver_inquiry: modify supported pages.\n");
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404

	/* If the device rejected the request because the CDB was
	 * improperly formed, then just leave.
	 */
	if (skcomp->status == SAM_STAT_CHECK_CONDITION &&
	    skerr->key == ILLEGAL_REQUEST && skerr->code == 0x24)
		return;

	/* Get the amount of space the caller allocated */
	max_bytes = (cdb[3] << 8) | cdb[4];

	/* Get the number of pages actually returned by the device */
	drive_pages = (buf[2] << 8) | buf[3];
	drive_bytes = drive_pages + 4;
	new_size = drive_pages + 1;

	/* Supported pages must be in numerical order, so find where
	 * the driver page needs to be inserted into the list of
	 * pages returned by the device.
	 */
	for (insert_pt = 4; insert_pt < drive_bytes; insert_pt++) {
		if (buf[insert_pt] == DRIVER_INQ_EVPD_PAGE_CODE)
			return; /* Device using this page code. abort */
		else if (buf[insert_pt] > DRIVER_INQ_EVPD_PAGE_CODE)
			break;
	}

	if (insert_pt < max_bytes) {
		uint16_t u;

		/* Shift everything up one byte to make room. */
		for (u = new_size + 3; u > insert_pt; u--)
			buf[u] = buf[u - 1];
		buf[insert_pt] = DRIVER_INQ_EVPD_PAGE_CODE;

		/* SCSI byte order increment of num_returned_bytes by 1 */
		skcomp->num_returned_bytes =
2405
			cpu_to_be32(be32_to_cpu(skcomp->num_returned_bytes) + 1);
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
	}

	/* update page length field to reflect the driver's page too */
	buf[2] = (uint8_t)((new_size >> 8) & 0xFF);
	buf[3] = (uint8_t)((new_size >> 0) & 0xFF);
}

static void skd_get_link_info(struct pci_dev *pdev, u8 *speed, u8 *width)
{
	int pcie_reg;
	u16 pci_bus_speed;
	u8 pci_lanes;

	pcie_reg = pci_find_capability(pdev, PCI_CAP_ID_EXP);
	if (pcie_reg) {
		u16 linksta;
		pci_read_config_word(pdev, pcie_reg + PCI_EXP_LNKSTA, &linksta);

		pci_bus_speed = linksta & 0xF;
		pci_lanes = (linksta & 0x3F0) >> 4;
	} else {
		*speed = STEC_LINK_UNKNOWN;
		*width = 0xFF;
		return;
	}

	switch (pci_bus_speed) {
	case 1:
		*speed = STEC_LINK_2_5GTS;
		break;
	case 2:
		*speed = STEC_LINK_5GTS;
		break;
	case 3:
		*speed = STEC_LINK_8GTS;
		break;
	default:
		*speed = STEC_LINK_UNKNOWN;
		break;
	}

	if (pci_lanes <= 0x20)
		*width = pci_lanes;
	else
		*width = 0xFF;
}

static void skd_do_inq_page_da(struct skd_device *skdev,
			       volatile struct fit_completion_entry_v1 *skcomp,
			       volatile struct fit_comp_error_info *skerr,
			       uint8_t *cdb, uint8_t *buf)
{
2458
	struct pci_dev *pdev = skdev->pdev;
2459 2460 2461 2462
	unsigned max_bytes;
	struct driver_inquiry_data inq;
	u16 val;

2463
	dev_dbg(&skdev->pdev->dev, "skd_do_driver_inquiry: return driver page\n");
2464 2465 2466 2467 2468

	memset(&inq, 0, sizeof(inq));

	inq.page_code = DRIVER_INQ_EVPD_PAGE_CODE;

2469 2470 2471 2472
	skd_get_link_info(pdev, &inq.pcie_link_speed, &inq.pcie_link_lanes);
	inq.pcie_bus_number = cpu_to_be16(pdev->bus->number);
	inq.pcie_device_number = PCI_SLOT(pdev->devfn);
	inq.pcie_function_number = PCI_FUNC(pdev->devfn);
2473

2474 2475
	pci_read_config_word(pdev, PCI_VENDOR_ID, &val);
	inq.pcie_vendor_id = cpu_to_be16(val);
2476

2477 2478
	pci_read_config_word(pdev, PCI_DEVICE_ID, &val);
	inq.pcie_device_id = cpu_to_be16(val);
2479

2480 2481
	pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &val);
	inq.pcie_subsystem_vendor_id = cpu_to_be16(val);
2482

2483 2484
	pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &val);
	inq.pcie_subsystem_device_id = cpu_to_be16(val);
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502

	/* Driver version, fixed lenth, padded with spaces on the right */
	inq.driver_version_length = sizeof(inq.driver_version);
	memset(&inq.driver_version, ' ', sizeof(inq.driver_version));
	memcpy(inq.driver_version, DRV_VER_COMPL,
	       min(sizeof(inq.driver_version), strlen(DRV_VER_COMPL)));

	inq.page_length = cpu_to_be16((sizeof(inq) - 4));

	/* Clear the error set by the device */
	skcomp->status = SAM_STAT_GOOD;
	memset((void *)skerr, 0, sizeof(*skerr));

	/* copy response into output buffer */
	max_bytes = (cdb[3] << 8) | cdb[4];
	memcpy(buf, &inq, min_t(unsigned, max_bytes, sizeof(inq)));

	skcomp->num_returned_bytes =
2503
		cpu_to_be32(min_t(uint16_t, max_bytes, sizeof(inq)));
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
}

static void skd_do_driver_inq(struct skd_device *skdev,
			      volatile struct fit_completion_entry_v1 *skcomp,
			      volatile struct fit_comp_error_info *skerr,
			      uint8_t *cdb, uint8_t *buf)
{
	if (!buf)
		return;
	else if (cdb[0] != INQUIRY)
		return;         /* Not an INQUIRY */
	else if ((cdb[1] & 1) == 0)
		return;         /* EVPD not set */
	else if (cdb[2] == 0)
		/* Need to add driver's page to supported pages list */
		skd_do_inq_page_00(skdev, skcomp, skerr, cdb, buf);
	else if (cdb[2] == DRIVER_INQ_EVPD_PAGE_CODE)
		/* Caller requested driver's page */
		skd_do_inq_page_da(skdev, skcomp, skerr, cdb, buf);
}

static unsigned char *skd_sg_1st_page_ptr(struct scatterlist *sg)
{
	if (!sg)
		return NULL;
	if (!sg_page(sg))
		return NULL;
	return sg_virt(sg);
}

static void skd_process_scsi_inq(struct skd_device *skdev,
				 volatile struct fit_completion_entry_v1
				 *skcomp,
				 volatile struct fit_comp_error_info *skerr,
				 struct skd_special_context *skspcl)
{
	uint8_t *buf;
	struct fit_msg_hdr *fmh = (struct fit_msg_hdr *)skspcl->msg_buf;
	struct skd_scsi_request *scsi_req = (struct skd_scsi_request *)&fmh[1];

	dma_sync_sg_for_cpu(skdev->class_dev, skspcl->req.sg, skspcl->req.n_sg,
			    skspcl->req.sg_data_dir);
	buf = skd_sg_1st_page_ptr(skspcl->req.sg);

	if (buf)
		skd_do_driver_inq(skdev, skcomp, skerr, scsi_req->cdb, buf);
}

static int skd_isr_completion_posted(struct skd_device *skdev,
					int limit, int *enqueued)
{
	volatile struct fit_completion_entry_v1 *skcmp = NULL;
	volatile struct fit_comp_error_info *skerr;
	u16 req_id;
	u32 req_slot;
	struct skd_request_context *skreq;
	u16 cmp_cntxt = 0;
	u8 cmp_status = 0;
	u8 cmp_cycle = 0;
	u32 cmp_bytes = 0;
	int rc = 0;
	int processed = 0;

	for (;; ) {
		SKD_ASSERT(skdev->skcomp_ix < SKD_N_COMPLETION_ENTRY);

		skcmp = &skdev->skcomp_table[skdev->skcomp_ix];
		cmp_cycle = skcmp->cycle;
		cmp_cntxt = skcmp->tag;
		cmp_status = skcmp->status;
		cmp_bytes = be32_to_cpu(skcmp->num_returned_bytes);

		skerr = &skdev->skerr_table[skdev->skcomp_ix];

2578 2579 2580 2581 2582
		dev_dbg(&skdev->pdev->dev,
			"cycle=%d ix=%d got cycle=%d cmdctxt=0x%x stat=%d busy=%d rbytes=0x%x proto=%d\n",
			skdev->skcomp_cycle, skdev->skcomp_ix, cmp_cycle,
			cmp_cntxt, cmp_status, skdev->in_flight, cmp_bytes,
			skdev->proto_ver);
2583 2584

		if (cmp_cycle != skdev->skcomp_cycle) {
2585
			dev_dbg(&skdev->pdev->dev, "end of completions\n");
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
			break;
		}
		/*
		 * Update the completion queue head index and possibly
		 * the completion cycle count. 8-bit wrap-around.
		 */
		skdev->skcomp_ix++;
		if (skdev->skcomp_ix >= SKD_N_COMPLETION_ENTRY) {
			skdev->skcomp_ix = 0;
			skdev->skcomp_cycle++;
		}

		/*
		 * The command context is a unique 32-bit ID. The low order
		 * bits help locate the request. The request is usually a
		 * r/w request (see skd_start() above) or a special request.
		 */
		req_id = cmp_cntxt;
		req_slot = req_id & SKD_ID_SLOT_AND_TABLE_MASK;

		/* Is this other than a r/w request? */
		if (req_slot >= skdev->num_req_context) {
			/*
			 * This is not a completion for a r/w request.
			 */
			skd_complete_other(skdev, skcmp, skerr);
			continue;
		}

		skreq = &skdev->skreq_table[req_slot];

		/*
		 * Make sure the request ID for the slot matches.
		 */
		if (skreq->id != req_id) {
2621 2622 2623
			dev_dbg(&skdev->pdev->dev,
				"mismatch comp_id=0x%x req_id=0x%x\n", req_id,
				skreq->id);
2624 2625
			{
				u16 new_id = cmp_cntxt;
2626 2627 2628
				dev_err(&skdev->pdev->dev,
					"Completion mismatch comp_id=0x%04x skreq=0x%04x new=0x%04x\n",
					req_id, skreq->id, new_id);
2629 2630 2631 2632 2633 2634 2635 2636

				continue;
			}
		}

		SKD_ASSERT(skreq->state == SKD_REQ_STATE_BUSY);

		if (skreq->state == SKD_REQ_STATE_ABORTED) {
2637 2638
			dev_dbg(&skdev->pdev->dev, "reclaim req %p id=%04x\n",
				skreq, skreq->id);
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
			/* a previously timed out command can
			 * now be cleaned up */
			skd_release_skreq(skdev, skreq);
			continue;
		}

		skreq->completion = *skcmp;
		if (unlikely(cmp_status == SAM_STAT_CHECK_CONDITION)) {
			skreq->err_info = *skerr;
			skd_log_check_status(skdev, cmp_status, skerr->key,
					     skerr->code, skerr->qual,
					     skerr->fruc);
		}
		/* Release DMA resources for the request. */
		if (skreq->n_sg > 0)
			skd_postop_sg_list(skdev, skreq);

J
Jens Axboe 已提交
2656
		if (!skreq->req) {
2657 2658 2659
			dev_dbg(&skdev->pdev->dev,
				"NULL backptr skdreq %p, req=0x%x req_id=0x%x\n",
				skreq, skreq->id, req_id);
2660 2661 2662 2663 2664
		} else {
			/*
			 * Capture the outcome and post it back to the
			 * native request.
			 */
J
Jens Axboe 已提交
2665
			if (likely(cmp_status == SAM_STAT_GOOD))
2666
				skd_end_request(skdev, skreq, BLK_STS_OK);
J
Jens Axboe 已提交
2667
			else
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
				skd_resolve_req_exception(skdev, skreq);
		}

		/*
		 * Release the skreq, its FIT msg (if one), timeout slot,
		 * and queue depth.
		 */
		skd_release_skreq(skdev, skreq);

		/* skd_isr_comp_limit equal zero means no limit */
		if (limit) {
			if (++processed >= limit) {
				rc = 1;
				break;
			}
		}
	}

	if ((skdev->state == SKD_DRVR_STATE_PAUSING)
		&& (skdev->in_flight) == 0) {
		skdev->state = SKD_DRVR_STATE_PAUSED;
		wake_up_interruptible(&skdev->waitq);
	}

	return rc;
}

static void skd_complete_other(struct skd_device *skdev,
			       volatile struct fit_completion_entry_v1 *skcomp,
			       volatile struct fit_comp_error_info *skerr)
{
	u32 req_id = 0;
	u32 req_table;
	u32 req_slot;
	struct skd_special_context *skspcl;

	req_id = skcomp->tag;
	req_table = req_id & SKD_ID_TABLE_MASK;
	req_slot = req_id & SKD_ID_SLOT_MASK;

2708 2709
	dev_dbg(&skdev->pdev->dev, "table=0x%x id=0x%x slot=%d\n", req_table,
		req_id, req_slot);
2710 2711 2712 2713 2714 2715 2716 2717 2718

	/*
	 * Based on the request id, determine how to dispatch this completion.
	 * This swich/case is finding the good cases and forwarding the
	 * completion entry. Errors are reported below the switch.
	 */
	switch (req_table) {
	case SKD_ID_RW_REQUEST:
		/*
2719
		 * The caller, skd_isr_completion_posted() above,
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
		 * handles r/w requests. The only way we get here
		 * is if the req_slot is out of bounds.
		 */
		break;

	case SKD_ID_SPECIAL_REQUEST:
		/*
		 * Make sure the req_slot is in bounds and that the id
		 * matches.
		 */
		if (req_slot < skdev->n_special) {
			skspcl = &skdev->skspcl_table[req_slot];
			if (skspcl->req.id == req_id &&
			    skspcl->req.state == SKD_REQ_STATE_BUSY) {
				skd_complete_special(skdev,
						     skcomp, skerr, skspcl);
				return;
			}
		}
		break;

	case SKD_ID_INTERNAL:
		if (req_slot == 0) {
			skspcl = &skdev->internal_skspcl;
			if (skspcl->req.id == req_id &&
			    skspcl->req.state == SKD_REQ_STATE_BUSY) {
				skd_complete_internal(skdev,
						      skcomp, skerr, skspcl);
				return;
			}
		}
		break;

	case SKD_ID_FIT_MSG:
		/*
		 * These id's should never appear in a completion record.
		 */
		break;

	default:
		/*
		 * These id's should never appear anywhere;
		 */
		break;
	}

	/*
	 * If we get here it is a bad or stale id.
	 */
}

static void skd_complete_special(struct skd_device *skdev,
				 volatile struct fit_completion_entry_v1
				 *skcomp,
				 volatile struct fit_comp_error_info *skerr,
				 struct skd_special_context *skspcl)
{
2777
	dev_dbg(&skdev->pdev->dev, " completing special request %p\n", skspcl);
2778 2779 2780 2781
	if (skspcl->orphaned) {
		/* Discard orphaned request */
		/* ?: Can this release directly or does it need
		 * to use a worker? */
2782
		dev_dbg(&skdev->pdev->dev, "release orphaned %p\n", skspcl);
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
		skd_release_special(skdev, skspcl);
		return;
	}

	skd_process_scsi_inq(skdev, skcomp, skerr, skspcl);

	skspcl->req.state = SKD_REQ_STATE_COMPLETED;
	skspcl->req.completion = *skcomp;
	skspcl->req.err_info = *skerr;

	skd_log_check_status(skdev, skspcl->req.completion.status, skerr->key,
			     skerr->code, skerr->qual, skerr->fruc);

	wake_up_interruptible(&skdev->waitq);
}

/* assume spinlock is already held */
static void skd_release_special(struct skd_device *skdev,
				struct skd_special_context *skspcl)
{
	int i, was_depleted;

	for (i = 0; i < skspcl->req.n_sg; i++) {
		struct page *page = sg_page(&skspcl->req.sg[i]);
		__free_page(page);
	}

	was_depleted = (skdev->skspcl_free_list == NULL);

	skspcl->req.state = SKD_REQ_STATE_IDLE;
	skspcl->req.id += SKD_ID_INCR;
	skspcl->req.next =
		(struct skd_request_context *)skdev->skspcl_free_list;
	skdev->skspcl_free_list = (struct skd_special_context *)skspcl;

	if (was_depleted) {
2819
		dev_dbg(&skdev->pdev->dev, "skspcl was depleted\n");
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
		/* Free list was depleted. Their might be waiters. */
		wake_up_interruptible(&skdev->waitq);
	}
}

static void skd_reset_skcomp(struct skd_device *skdev)
{
	u32 nbytes;
	struct fit_completion_entry_v1 *skcomp;

	nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
	nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;

	memset(skdev->skcomp_table, 0, nbytes);

	skdev->skcomp_ix = 0;
	skdev->skcomp_cycle = 1;
}

/*
 *****************************************************************************
 * INTERRUPTS
 *****************************************************************************
 */
static void skd_completion_worker(struct work_struct *work)
{
	struct skd_device *skdev =
		container_of(work, struct skd_device, completion_worker);
	unsigned long flags;
	int flush_enqueued = 0;

	spin_lock_irqsave(&skdev->lock, flags);

	/*
	 * pass in limit=0, which means no limit..
	 * process everything in compq
	 */
	skd_isr_completion_posted(skdev, 0, &flush_enqueued);
	skd_request_fn(skdev->queue);

	spin_unlock_irqrestore(&skdev->lock, flags);
}

static void skd_isr_msg_from_dev(struct skd_device *skdev);

A
Arnd Bergmann 已提交
2865 2866
static irqreturn_t
skd_isr(int irq, void *ptr)
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
{
	struct skd_device *skdev;
	u32 intstat;
	u32 ack;
	int rc = 0;
	int deferred = 0;
	int flush_enqueued = 0;

	skdev = (struct skd_device *)ptr;
	spin_lock(&skdev->lock);

	for (;; ) {
		intstat = SKD_READL(skdev, FIT_INT_STATUS_HOST);

		ack = FIT_INT_DEF_MASK;
		ack &= intstat;

2884 2885
		dev_dbg(&skdev->pdev->dev, "intstat=0x%x ack=0x%x\n", intstat,
			ack);
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949

		/* As long as there is an int pending on device, keep
		 * running loop.  When none, get out, but if we've never
		 * done any processing, call completion handler?
		 */
		if (ack == 0) {
			/* No interrupts on device, but run the completion
			 * processor anyway?
			 */
			if (rc == 0)
				if (likely (skdev->state
					== SKD_DRVR_STATE_ONLINE))
					deferred = 1;
			break;
		}

		rc = IRQ_HANDLED;

		SKD_WRITEL(skdev, ack, FIT_INT_STATUS_HOST);

		if (likely((skdev->state != SKD_DRVR_STATE_LOAD) &&
			   (skdev->state != SKD_DRVR_STATE_STOPPING))) {
			if (intstat & FIT_ISH_COMPLETION_POSTED) {
				/*
				 * If we have already deferred completion
				 * processing, don't bother running it again
				 */
				if (deferred == 0)
					deferred =
						skd_isr_completion_posted(skdev,
						skd_isr_comp_limit, &flush_enqueued);
			}

			if (intstat & FIT_ISH_FW_STATE_CHANGE) {
				skd_isr_fwstate(skdev);
				if (skdev->state == SKD_DRVR_STATE_FAULT ||
				    skdev->state ==
				    SKD_DRVR_STATE_DISAPPEARED) {
					spin_unlock(&skdev->lock);
					return rc;
				}
			}

			if (intstat & FIT_ISH_MSG_FROM_DEV)
				skd_isr_msg_from_dev(skdev);
		}
	}

	if (unlikely(flush_enqueued))
		skd_request_fn(skdev->queue);

	if (deferred)
		schedule_work(&skdev->completion_worker);
	else if (!flush_enqueued)
		skd_request_fn(skdev->queue);

	spin_unlock(&skdev->lock);

	return rc;
}

static void skd_drive_fault(struct skd_device *skdev)
{
	skdev->state = SKD_DRVR_STATE_FAULT;
2950
	dev_err(&skdev->pdev->dev, "Drive FAULT\n");
2951 2952 2953 2954 2955
}

static void skd_drive_disappeared(struct skd_device *skdev)
{
	skdev->state = SKD_DRVR_STATE_DISAPPEARED;
2956
	dev_err(&skdev->pdev->dev, "Drive DISAPPEARED\n");
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
}

static void skd_isr_fwstate(struct skd_device *skdev)
{
	u32 sense;
	u32 state;
	u32 mtd;
	int prev_driver_state = skdev->state;

	sense = SKD_READL(skdev, FIT_STATUS);
	state = sense & FIT_SR_DRIVE_STATE_MASK;

2969 2970 2971
	dev_err(&skdev->pdev->dev, "s1120 state %s(%d)=>%s(%d)\n",
		skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
		skd_drive_state_to_str(state), state);
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002

	skdev->drive_state = state;

	switch (skdev->drive_state) {
	case FIT_SR_DRIVE_INIT:
		if (skdev->state == SKD_DRVR_STATE_PROTOCOL_MISMATCH) {
			skd_disable_interrupts(skdev);
			break;
		}
		if (skdev->state == SKD_DRVR_STATE_RESTARTING)
			skd_recover_requests(skdev, 0);
		if (skdev->state == SKD_DRVR_STATE_WAIT_BOOT) {
			skdev->timer_countdown = SKD_STARTING_TIMO;
			skdev->state = SKD_DRVR_STATE_STARTING;
			skd_soft_reset(skdev);
			break;
		}
		mtd = FIT_MXD_CONS(FIT_MTD_FITFW_INIT, 0, 0);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_SR_DRIVE_ONLINE:
		skdev->cur_max_queue_depth = skd_max_queue_depth;
		if (skdev->cur_max_queue_depth > skdev->dev_max_queue_depth)
			skdev->cur_max_queue_depth = skdev->dev_max_queue_depth;

		skdev->queue_low_water_mark =
			skdev->cur_max_queue_depth * 2 / 3 + 1;
		if (skdev->queue_low_water_mark < 1)
			skdev->queue_low_water_mark = 1;
3003 3004 3005 3006 3007
		dev_info(&skdev->pdev->dev,
			 "Queue depth limit=%d dev=%d lowat=%d\n",
			 skdev->cur_max_queue_depth,
			 skdev->dev_max_queue_depth,
			 skdev->queue_low_water_mark);
3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022

		skd_refresh_device_data(skdev);
		break;

	case FIT_SR_DRIVE_BUSY:
		skdev->state = SKD_DRVR_STATE_BUSY;
		skdev->timer_countdown = SKD_BUSY_TIMO;
		skd_quiesce_dev(skdev);
		break;
	case FIT_SR_DRIVE_BUSY_SANITIZE:
		/* set timer for 3 seconds, we'll abort any unfinished
		 * commands after that expires
		 */
		skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
		skdev->timer_countdown = SKD_TIMER_SECONDS(3);
3023
		blk_start_queue(skdev->queue);
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
		break;
	case FIT_SR_DRIVE_BUSY_ERASE:
		skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
		skdev->timer_countdown = SKD_BUSY_TIMO;
		break;
	case FIT_SR_DRIVE_OFFLINE:
		skdev->state = SKD_DRVR_STATE_IDLE;
		break;
	case FIT_SR_DRIVE_SOFT_RESET:
		switch (skdev->state) {
		case SKD_DRVR_STATE_STARTING:
		case SKD_DRVR_STATE_RESTARTING:
			/* Expected by a caller of skd_soft_reset() */
			break;
		default:
			skdev->state = SKD_DRVR_STATE_RESTARTING;
			break;
		}
		break;
	case FIT_SR_DRIVE_FW_BOOTING:
3044
		dev_dbg(&skdev->pdev->dev, "ISR FIT_SR_DRIVE_FW_BOOTING\n");
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
		skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
		skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
		break;

	case FIT_SR_DRIVE_DEGRADED:
	case FIT_SR_PCIE_LINK_DOWN:
	case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
		break;

	case FIT_SR_DRIVE_FAULT:
		skd_drive_fault(skdev);
		skd_recover_requests(skdev, 0);
3057
		blk_start_queue(skdev->queue);
3058 3059 3060 3061
		break;

	/* PCIe bus returned all Fs? */
	case 0xFF:
3062 3063
		dev_info(&skdev->pdev->dev, "state=0x%x sense=0x%x\n", state,
			 sense);
3064 3065
		skd_drive_disappeared(skdev);
		skd_recover_requests(skdev, 0);
3066
		blk_start_queue(skdev->queue);
3067 3068 3069 3070 3071 3072 3073
		break;
	default:
		/*
		 * Uknown FW State. Wait for a state we recognize.
		 */
		break;
	}
3074 3075 3076
	dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
		skd_skdev_state_to_str(prev_driver_state), prev_driver_state,
		skd_skdev_state_to_str(skdev->state), skdev->state);
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
}

static void skd_recover_requests(struct skd_device *skdev, int requeue)
{
	int i;

	for (i = 0; i < skdev->num_req_context; i++) {
		struct skd_request_context *skreq = &skdev->skreq_table[i];

		if (skreq->state == SKD_REQ_STATE_BUSY) {
			skd_log_skreq(skdev, skreq, "recover");

			SKD_ASSERT((skreq->id & SKD_ID_INCR) != 0);
J
Jens Axboe 已提交
3090
			SKD_ASSERT(skreq->req != NULL);
3091 3092 3093 3094 3095

			/* Release DMA resources for the request. */
			if (skreq->n_sg > 0)
				skd_postop_sg_list(skdev, skreq);

J
Jens Axboe 已提交
3096 3097 3098
			if (requeue &&
			    (unsigned long) ++skreq->req->special <
			    SKD_MAX_RETRIES)
M
Mike Snitzer 已提交
3099
				blk_requeue_request(skdev->queue, skreq->req);
J
Jens Axboe 已提交
3100
			else
3101
				skd_end_request(skdev, skreq, BLK_STS_IOERR);
3102

J
Jens Axboe 已提交
3103
			skreq->req = NULL;
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140

			skreq->state = SKD_REQ_STATE_IDLE;
			skreq->id += SKD_ID_INCR;
		}
		if (i > 0)
			skreq[-1].next = skreq;
		skreq->next = NULL;
	}
	skdev->skreq_free_list = skdev->skreq_table;

	for (i = 0; i < skdev->num_fitmsg_context; i++) {
		struct skd_fitmsg_context *skmsg = &skdev->skmsg_table[i];

		if (skmsg->state == SKD_MSG_STATE_BUSY) {
			skd_log_skmsg(skdev, skmsg, "salvaged");
			SKD_ASSERT((skmsg->id & SKD_ID_INCR) != 0);
			skmsg->state = SKD_MSG_STATE_IDLE;
			skmsg->id += SKD_ID_INCR;
		}
		if (i > 0)
			skmsg[-1].next = skmsg;
		skmsg->next = NULL;
	}
	skdev->skmsg_free_list = skdev->skmsg_table;

	for (i = 0; i < skdev->n_special; i++) {
		struct skd_special_context *skspcl = &skdev->skspcl_table[i];

		/* If orphaned, reclaim it because it has already been reported
		 * to the process as an error (it was just waiting for
		 * a completion that didn't come, and now it will never come)
		 * If busy, change to a state that will cause it to error
		 * out in the wait routine and let it do the normal
		 * reporting and reclaiming
		 */
		if (skspcl->req.state == SKD_REQ_STATE_BUSY) {
			if (skspcl->orphaned) {
3141 3142
				dev_dbg(&skdev->pdev->dev, "orphaned %p\n",
					skspcl);
3143 3144
				skd_release_special(skdev, skspcl);
			} else {
3145 3146
				dev_dbg(&skdev->pdev->dev, "not orphaned %p\n",
					skspcl);
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
				skspcl->req.state = SKD_REQ_STATE_ABORTED;
			}
		}
	}
	skdev->skspcl_free_list = skdev->skspcl_table;

	for (i = 0; i < SKD_N_TIMEOUT_SLOT; i++)
		skdev->timeout_slot[i] = 0;

	skdev->in_flight = 0;
}

static void skd_isr_msg_from_dev(struct skd_device *skdev)
{
	u32 mfd;
	u32 mtd;
	u32 data;

	mfd = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);

3167 3168
	dev_dbg(&skdev->pdev->dev, "mfd=0x%x last_mtd=0x%x\n", mfd,
		skdev->last_mtd);
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178

	/* ignore any mtd that is an ack for something we didn't send */
	if (FIT_MXD_TYPE(mfd) != FIT_MXD_TYPE(skdev->last_mtd))
		return;

	switch (FIT_MXD_TYPE(mfd)) {
	case FIT_MTD_FITFW_INIT:
		skdev->proto_ver = FIT_PROTOCOL_MAJOR_VER(mfd);

		if (skdev->proto_ver != FIT_PROTOCOL_VERSION_1) {
3179 3180 3181 3182
			dev_err(&skdev->pdev->dev, "protocol mismatch\n");
			dev_err(&skdev->pdev->dev, "  got=%d support=%d\n",
				skdev->proto_ver, FIT_PROTOCOL_VERSION_1);
			dev_err(&skdev->pdev->dev, "  please upgrade driver\n");
3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
			skdev->state = SKD_DRVR_STATE_PROTOCOL_MISMATCH;
			skd_soft_reset(skdev);
			break;
		}
		mtd = FIT_MXD_CONS(FIT_MTD_GET_CMDQ_DEPTH, 0, 0);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_MTD_GET_CMDQ_DEPTH:
		skdev->dev_max_queue_depth = FIT_MXD_DATA(mfd);
		mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_DEPTH, 0,
				   SKD_N_COMPLETION_ENTRY);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_MTD_SET_COMPQ_DEPTH:
		SKD_WRITEQ(skdev, skdev->cq_dma_address, FIT_MSG_TO_DEVICE_ARG);
		mtd = FIT_MXD_CONS(FIT_MTD_SET_COMPQ_ADDR, 0, 0);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_MTD_SET_COMPQ_ADDR:
		skd_reset_skcomp(skdev);
		mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_HOST_ID, 0, skdev->devno);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_MTD_CMD_LOG_HOST_ID:
		skdev->connect_time_stamp = get_seconds();
		data = skdev->connect_time_stamp & 0xFFFF;
		mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_LO, 0, data);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_MTD_CMD_LOG_TIME_STAMP_LO:
		skdev->drive_jiffies = FIT_MXD_DATA(mfd);
		data = (skdev->connect_time_stamp >> 16) & 0xFFFF;
		mtd = FIT_MXD_CONS(FIT_MTD_CMD_LOG_TIME_STAMP_HI, 0, data);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;
		break;

	case FIT_MTD_CMD_LOG_TIME_STAMP_HI:
		skdev->drive_jiffies |= (FIT_MXD_DATA(mfd) << 16);
		mtd = FIT_MXD_CONS(FIT_MTD_ARM_QUEUE, 0, 0);
		SKD_WRITEL(skdev, mtd, FIT_MSG_TO_DEVICE);
		skdev->last_mtd = mtd;

3236 3237
		dev_err(&skdev->pdev->dev, "Time sync driver=0x%x device=0x%x\n",
			skdev->connect_time_stamp, skdev->drive_jiffies);
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
		break;

	case FIT_MTD_ARM_QUEUE:
		skdev->last_mtd = 0;
		/*
		 * State should be, or soon will be, FIT_SR_DRIVE_ONLINE.
		 */
		break;

	default:
		break;
	}
}

static void skd_disable_interrupts(struct skd_device *skdev)
{
	u32 sense;

	sense = SKD_READL(skdev, FIT_CONTROL);
	sense &= ~FIT_CR_ENABLE_INTERRUPTS;
	SKD_WRITEL(skdev, sense, FIT_CONTROL);
3259
	dev_dbg(&skdev->pdev->dev, "sense 0x%x\n", sense);
3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277

	/* Note that the 1s is written. A 1-bit means
	 * disable, a 0 means enable.
	 */
	SKD_WRITEL(skdev, ~0, FIT_INT_MASK_HOST);
}

static void skd_enable_interrupts(struct skd_device *skdev)
{
	u32 val;

	/* unmask interrupts first */
	val = FIT_ISH_FW_STATE_CHANGE +
	      FIT_ISH_COMPLETION_POSTED + FIT_ISH_MSG_FROM_DEV;

	/* Note that the compliment of mask is written. A 1-bit means
	 * disable, a 0 means enable. */
	SKD_WRITEL(skdev, ~val, FIT_INT_MASK_HOST);
3278
	dev_dbg(&skdev->pdev->dev, "interrupt mask=0x%x\n", ~val);
3279 3280 3281

	val = SKD_READL(skdev, FIT_CONTROL);
	val |= FIT_CR_ENABLE_INTERRUPTS;
3282
	dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297
	SKD_WRITEL(skdev, val, FIT_CONTROL);
}

/*
 *****************************************************************************
 * START, STOP, RESTART, QUIESCE, UNQUIESCE
 *****************************************************************************
 */

static void skd_soft_reset(struct skd_device *skdev)
{
	u32 val;

	val = SKD_READL(skdev, FIT_CONTROL);
	val |= (FIT_CR_SOFT_RESET);
3298
	dev_dbg(&skdev->pdev->dev, "control=0x%x\n", val);
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
	SKD_WRITEL(skdev, val, FIT_CONTROL);
}

static void skd_start_device(struct skd_device *skdev)
{
	unsigned long flags;
	u32 sense;
	u32 state;

	spin_lock_irqsave(&skdev->lock, flags);

	/* ack all ghost interrupts */
	SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);

	sense = SKD_READL(skdev, FIT_STATUS);

3315
	dev_dbg(&skdev->pdev->dev, "initial status=0x%x\n", sense);
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327

	state = sense & FIT_SR_DRIVE_STATE_MASK;
	skdev->drive_state = state;
	skdev->last_mtd = 0;

	skdev->state = SKD_DRVR_STATE_STARTING;
	skdev->timer_countdown = SKD_STARTING_TIMO;

	skd_enable_interrupts(skdev);

	switch (skdev->drive_state) {
	case FIT_SR_DRIVE_OFFLINE:
3328
		dev_err(&skdev->pdev->dev, "Drive offline...\n");
3329 3330 3331
		break;

	case FIT_SR_DRIVE_FW_BOOTING:
3332
		dev_dbg(&skdev->pdev->dev, "FIT_SR_DRIVE_FW_BOOTING\n");
3333 3334 3335 3336 3337
		skdev->state = SKD_DRVR_STATE_WAIT_BOOT;
		skdev->timer_countdown = SKD_WAIT_BOOT_TIMO;
		break;

	case FIT_SR_DRIVE_BUSY_SANITIZE:
3338
		dev_info(&skdev->pdev->dev, "Start: BUSY_SANITIZE\n");
3339 3340 3341 3342 3343
		skdev->state = SKD_DRVR_STATE_BUSY_SANITIZE;
		skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
		break;

	case FIT_SR_DRIVE_BUSY_ERASE:
3344
		dev_info(&skdev->pdev->dev, "Start: BUSY_ERASE\n");
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
		skdev->state = SKD_DRVR_STATE_BUSY_ERASE;
		skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
		break;

	case FIT_SR_DRIVE_INIT:
	case FIT_SR_DRIVE_ONLINE:
		skd_soft_reset(skdev);
		break;

	case FIT_SR_DRIVE_BUSY:
3355
		dev_err(&skdev->pdev->dev, "Drive Busy...\n");
3356 3357 3358 3359 3360
		skdev->state = SKD_DRVR_STATE_BUSY;
		skdev->timer_countdown = SKD_STARTED_BUSY_TIMO;
		break;

	case FIT_SR_DRIVE_SOFT_RESET:
3361
		dev_err(&skdev->pdev->dev, "drive soft reset in prog\n");
3362 3363 3364 3365 3366 3367 3368 3369 3370
		break;

	case FIT_SR_DRIVE_FAULT:
		/* Fault state is bad...soft reset won't do it...
		 * Hard reset, maybe, but does it work on device?
		 * For now, just fault so the system doesn't hang.
		 */
		skd_drive_fault(skdev);
		/*start the queue so we can respond with error to requests */
3371
		dev_dbg(&skdev->pdev->dev, "starting queue\n");
3372
		blk_start_queue(skdev->queue);
3373 3374 3375 3376 3377 3378 3379 3380 3381
		skdev->gendisk_on = -1;
		wake_up_interruptible(&skdev->waitq);
		break;

	case 0xFF:
		/* Most likely the device isn't there or isn't responding
		 * to the BAR1 addresses. */
		skd_drive_disappeared(skdev);
		/*start the queue so we can respond with error to requests */
3382 3383
		dev_dbg(&skdev->pdev->dev,
			"starting queue to error-out reqs\n");
3384
		blk_start_queue(skdev->queue);
3385 3386 3387 3388 3389
		skdev->gendisk_on = -1;
		wake_up_interruptible(&skdev->waitq);
		break;

	default:
3390 3391
		dev_err(&skdev->pdev->dev, "Start: unknown state %x\n",
			skdev->drive_state);
3392 3393 3394 3395
		break;
	}

	state = SKD_READL(skdev, FIT_CONTROL);
3396
	dev_dbg(&skdev->pdev->dev, "FIT Control Status=0x%x\n", state);
3397 3398

	state = SKD_READL(skdev, FIT_INT_STATUS_HOST);
3399
	dev_dbg(&skdev->pdev->dev, "Intr Status=0x%x\n", state);
3400 3401

	state = SKD_READL(skdev, FIT_INT_MASK_HOST);
3402
	dev_dbg(&skdev->pdev->dev, "Intr Mask=0x%x\n", state);
3403 3404

	state = SKD_READL(skdev, FIT_MSG_FROM_DEVICE);
3405
	dev_dbg(&skdev->pdev->dev, "Msg from Dev=0x%x\n", state);
3406 3407

	state = SKD_READL(skdev, FIT_HW_VERSION);
3408
	dev_dbg(&skdev->pdev->dev, "HW version=0x%x\n", state);
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422

	spin_unlock_irqrestore(&skdev->lock, flags);
}

static void skd_stop_device(struct skd_device *skdev)
{
	unsigned long flags;
	struct skd_special_context *skspcl = &skdev->internal_skspcl;
	u32 dev_state;
	int i;

	spin_lock_irqsave(&skdev->lock, flags);

	if (skdev->state != SKD_DRVR_STATE_ONLINE) {
3423
		dev_err(&skdev->pdev->dev, "%s not online no sync\n", __func__);
3424 3425 3426 3427
		goto stop_out;
	}

	if (skspcl->req.state != SKD_REQ_STATE_IDLE) {
3428
		dev_err(&skdev->pdev->dev, "%s no special\n", __func__);
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
		goto stop_out;
	}

	skdev->state = SKD_DRVR_STATE_SYNCING;
	skdev->sync_done = 0;

	skd_send_internal_skspcl(skdev, skspcl, SYNCHRONIZE_CACHE);

	spin_unlock_irqrestore(&skdev->lock, flags);

	wait_event_interruptible_timeout(skdev->waitq,
					 (skdev->sync_done), (10 * HZ));

	spin_lock_irqsave(&skdev->lock, flags);

	switch (skdev->sync_done) {
	case 0:
3446
		dev_err(&skdev->pdev->dev, "%s no sync\n", __func__);
3447 3448
		break;
	case 1:
3449
		dev_err(&skdev->pdev->dev, "%s sync done\n", __func__);
3450 3451
		break;
	default:
3452
		dev_err(&skdev->pdev->dev, "%s sync error\n", __func__);
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
	}

stop_out:
	skdev->state = SKD_DRVR_STATE_STOPPING;
	spin_unlock_irqrestore(&skdev->lock, flags);

	skd_kill_timer(skdev);

	spin_lock_irqsave(&skdev->lock, flags);
	skd_disable_interrupts(skdev);

	/* ensure all ints on device are cleared */
	/* soft reset the device to unload with a clean slate */
	SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);
	SKD_WRITEL(skdev, FIT_CR_SOFT_RESET, FIT_CONTROL);

	spin_unlock_irqrestore(&skdev->lock, flags);

	/* poll every 100ms, 1 second timeout */
	for (i = 0; i < 10; i++) {
		dev_state =
			SKD_READL(skdev, FIT_STATUS) & FIT_SR_DRIVE_STATE_MASK;
		if (dev_state == FIT_SR_DRIVE_INIT)
			break;
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(msecs_to_jiffies(100));
	}

	if (dev_state != FIT_SR_DRIVE_INIT)
3482 3483
		dev_err(&skdev->pdev->dev, "%s state error 0x%02x\n", __func__,
			dev_state);
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
}

/* assume spinlock is held */
static void skd_restart_device(struct skd_device *skdev)
{
	u32 state;

	/* ack all ghost interrupts */
	SKD_WRITEL(skdev, FIT_INT_DEF_MASK, FIT_INT_STATUS_HOST);

	state = SKD_READL(skdev, FIT_STATUS);

3496
	dev_dbg(&skdev->pdev->dev, "drive status=0x%x\n", state);
3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515

	state &= FIT_SR_DRIVE_STATE_MASK;
	skdev->drive_state = state;
	skdev->last_mtd = 0;

	skdev->state = SKD_DRVR_STATE_RESTARTING;
	skdev->timer_countdown = SKD_RESTARTING_TIMO;

	skd_soft_reset(skdev);
}

/* assume spinlock is held */
static int skd_quiesce_dev(struct skd_device *skdev)
{
	int rc = 0;

	switch (skdev->state) {
	case SKD_DRVR_STATE_BUSY:
	case SKD_DRVR_STATE_BUSY_IMMINENT:
3516
		dev_dbg(&skdev->pdev->dev, "stopping queue\n");
3517
		blk_stop_queue(skdev->queue);
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
		break;
	case SKD_DRVR_STATE_ONLINE:
	case SKD_DRVR_STATE_STOPPING:
	case SKD_DRVR_STATE_SYNCING:
	case SKD_DRVR_STATE_PAUSING:
	case SKD_DRVR_STATE_PAUSED:
	case SKD_DRVR_STATE_STARTING:
	case SKD_DRVR_STATE_RESTARTING:
	case SKD_DRVR_STATE_RESUMING:
	default:
		rc = -EINVAL;
3529 3530
		dev_dbg(&skdev->pdev->dev, "state [%d] not implemented\n",
			skdev->state);
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541
	}
	return rc;
}

/* assume spinlock is held */
static int skd_unquiesce_dev(struct skd_device *skdev)
{
	int prev_driver_state = skdev->state;

	skd_log_skdev(skdev, "unquiesce");
	if (skdev->state == SKD_DRVR_STATE_ONLINE) {
3542
		dev_dbg(&skdev->pdev->dev, "**** device already ONLINE\n");
3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
		return 0;
	}
	if (skdev->drive_state != FIT_SR_DRIVE_ONLINE) {
		/*
		 * If there has been an state change to other than
		 * ONLINE, we will rely on controller state change
		 * to come back online and restart the queue.
		 * The BUSY state means that driver is ready to
		 * continue normal processing but waiting for controller
		 * to become available.
		 */
		skdev->state = SKD_DRVR_STATE_BUSY;
3555
		dev_dbg(&skdev->pdev->dev, "drive BUSY state\n");
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
		return 0;
	}

	/*
	 * Drive has just come online, driver is either in startup,
	 * paused performing a task, or bust waiting for hardware.
	 */
	switch (skdev->state) {
	case SKD_DRVR_STATE_PAUSED:
	case SKD_DRVR_STATE_BUSY:
	case SKD_DRVR_STATE_BUSY_IMMINENT:
	case SKD_DRVR_STATE_BUSY_ERASE:
	case SKD_DRVR_STATE_STARTING:
	case SKD_DRVR_STATE_RESTARTING:
	case SKD_DRVR_STATE_FAULT:
	case SKD_DRVR_STATE_IDLE:
	case SKD_DRVR_STATE_LOAD:
		skdev->state = SKD_DRVR_STATE_ONLINE;
3574 3575 3576 3577 3578 3579 3580 3581
		dev_err(&skdev->pdev->dev, "Driver state %s(%d)=>%s(%d)\n",
			skd_skdev_state_to_str(prev_driver_state),
			prev_driver_state, skd_skdev_state_to_str(skdev->state),
			skdev->state);
		dev_dbg(&skdev->pdev->dev,
			"**** device ONLINE...starting block queue\n");
		dev_dbg(&skdev->pdev->dev, "starting queue\n");
		dev_info(&skdev->pdev->dev, "STEC s1120 ONLINE\n");
3582
		blk_start_queue(skdev->queue);
3583 3584 3585 3586 3587 3588
		skdev->gendisk_on = 1;
		wake_up_interruptible(&skdev->waitq);
		break;

	case SKD_DRVR_STATE_DISAPPEARED:
	default:
3589 3590 3591
		dev_dbg(&skdev->pdev->dev,
			"**** driver state %d, not implemented\n",
			skdev->state);
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
		return -EBUSY;
	}
	return 0;
}

/*
 *****************************************************************************
 * PCIe MSI/MSI-X INTERRUPT HANDLERS
 *****************************************************************************
 */

static irqreturn_t skd_reserved_isr(int irq, void *skd_host_data)
{
	struct skd_device *skdev = skd_host_data;
	unsigned long flags;

	spin_lock_irqsave(&skdev->lock, flags);
3609 3610 3611 3612
	dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
		SKD_READL(skdev, FIT_INT_STATUS_HOST));
	dev_err(&skdev->pdev->dev, "MSIX reserved irq %d = 0x%x\n", irq,
		SKD_READL(skdev, FIT_INT_STATUS_HOST));
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
	SKD_WRITEL(skdev, FIT_INT_RESERVED_MASK, FIT_INT_STATUS_HOST);
	spin_unlock_irqrestore(&skdev->lock, flags);
	return IRQ_HANDLED;
}

static irqreturn_t skd_statec_isr(int irq, void *skd_host_data)
{
	struct skd_device *skdev = skd_host_data;
	unsigned long flags;

	spin_lock_irqsave(&skdev->lock, flags);
3624 3625
	dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
		SKD_READL(skdev, FIT_INT_STATUS_HOST));
3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
	SKD_WRITEL(skdev, FIT_ISH_FW_STATE_CHANGE, FIT_INT_STATUS_HOST);
	skd_isr_fwstate(skdev);
	spin_unlock_irqrestore(&skdev->lock, flags);
	return IRQ_HANDLED;
}

static irqreturn_t skd_comp_q(int irq, void *skd_host_data)
{
	struct skd_device *skdev = skd_host_data;
	unsigned long flags;
	int flush_enqueued = 0;
	int deferred;

	spin_lock_irqsave(&skdev->lock, flags);
3640 3641
	dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
		SKD_READL(skdev, FIT_INT_STATUS_HOST));
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663
	SKD_WRITEL(skdev, FIT_ISH_COMPLETION_POSTED, FIT_INT_STATUS_HOST);
	deferred = skd_isr_completion_posted(skdev, skd_isr_comp_limit,
						&flush_enqueued);
	if (flush_enqueued)
		skd_request_fn(skdev->queue);

	if (deferred)
		schedule_work(&skdev->completion_worker);
	else if (!flush_enqueued)
		skd_request_fn(skdev->queue);

	spin_unlock_irqrestore(&skdev->lock, flags);

	return IRQ_HANDLED;
}

static irqreturn_t skd_msg_isr(int irq, void *skd_host_data)
{
	struct skd_device *skdev = skd_host_data;
	unsigned long flags;

	spin_lock_irqsave(&skdev->lock, flags);
3664 3665
	dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
		SKD_READL(skdev, FIT_INT_STATUS_HOST));
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
	SKD_WRITEL(skdev, FIT_ISH_MSG_FROM_DEV, FIT_INT_STATUS_HOST);
	skd_isr_msg_from_dev(skdev);
	spin_unlock_irqrestore(&skdev->lock, flags);
	return IRQ_HANDLED;
}

static irqreturn_t skd_qfull_isr(int irq, void *skd_host_data)
{
	struct skd_device *skdev = skd_host_data;
	unsigned long flags;

	spin_lock_irqsave(&skdev->lock, flags);
3678 3679
	dev_dbg(&skdev->pdev->dev, "MSIX = 0x%x\n",
		SKD_READL(skdev, FIT_INT_STATUS_HOST));
3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721
	SKD_WRITEL(skdev, FIT_INT_QUEUE_FULL, FIT_INT_STATUS_HOST);
	spin_unlock_irqrestore(&skdev->lock, flags);
	return IRQ_HANDLED;
}

/*
 *****************************************************************************
 * PCIe MSI/MSI-X SETUP
 *****************************************************************************
 */

struct skd_msix_entry {
	char isr_name[30];
};

struct skd_init_msix_entry {
	const char *name;
	irq_handler_t handler;
};

#define SKD_MAX_MSIX_COUNT              13
#define SKD_MIN_MSIX_COUNT              7
#define SKD_BASE_MSIX_IRQ               4

static struct skd_init_msix_entry msix_entries[SKD_MAX_MSIX_COUNT] = {
	{ "(DMA 0)",	    skd_reserved_isr },
	{ "(DMA 1)",	    skd_reserved_isr },
	{ "(DMA 2)",	    skd_reserved_isr },
	{ "(DMA 3)",	    skd_reserved_isr },
	{ "(State Change)", skd_statec_isr   },
	{ "(COMPL_Q)",	    skd_comp_q	     },
	{ "(MSG)",	    skd_msg_isr	     },
	{ "(Reserved)",	    skd_reserved_isr },
	{ "(Reserved)",	    skd_reserved_isr },
	{ "(Queue Full 0)", skd_qfull_isr    },
	{ "(Queue Full 1)", skd_qfull_isr    },
	{ "(Queue Full 2)", skd_qfull_isr    },
	{ "(Queue Full 3)", skd_qfull_isr    },
};

static int skd_acquire_msix(struct skd_device *skdev)
{
3722
	int i, rc;
3723
	struct pci_dev *pdev = skdev->pdev;
3724

C
Christoph Hellwig 已提交
3725 3726 3727
	rc = pci_alloc_irq_vectors(pdev, SKD_MAX_MSIX_COUNT, SKD_MAX_MSIX_COUNT,
			PCI_IRQ_MSIX);
	if (rc < 0) {
3728
		dev_err(&skdev->pdev->dev, "failed to enable MSI-X %d\n", rc);
A
Arnd Bergmann 已提交
3729
		goto out;
3730
	}
3731

C
Christoph Hellwig 已提交
3732 3733
	skdev->msix_entries = kcalloc(SKD_MAX_MSIX_COUNT,
			sizeof(struct skd_msix_entry), GFP_KERNEL);
3734 3735
	if (!skdev->msix_entries) {
		rc = -ENOMEM;
3736
		dev_err(&skdev->pdev->dev, "msix table allocation error\n");
A
Arnd Bergmann 已提交
3737
		goto out;
3738 3739 3740
	}

	/* Enable MSI-X vectors for the base queue */
C
Christoph Hellwig 已提交
3741 3742 3743
	for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
		struct skd_msix_entry *qentry = &skdev->msix_entries[i];

3744 3745 3746
		snprintf(qentry->isr_name, sizeof(qentry->isr_name),
			 "%s%d-msix %s", DRV_NAME, skdev->devno,
			 msix_entries[i].name);
C
Christoph Hellwig 已提交
3747 3748 3749 3750 3751

		rc = devm_request_irq(&skdev->pdev->dev,
				pci_irq_vector(skdev->pdev, i),
				msix_entries[i].handler, 0,
				qentry->isr_name, skdev);
3752
		if (rc) {
3753 3754 3755
			dev_err(&skdev->pdev->dev,
				"Unable to register(%d) MSI-X handler %d: %s\n",
				rc, i, qentry->isr_name);
3756 3757 3758
			goto msix_out;
		}
	}
C
Christoph Hellwig 已提交
3759

3760 3761
	dev_dbg(&skdev->pdev->dev, "%d msix irq(s) enabled\n",
		SKD_MAX_MSIX_COUNT);
3762 3763 3764
	return 0;

msix_out:
C
Christoph Hellwig 已提交
3765 3766
	while (--i >= 0)
		devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), skdev);
A
Arnd Bergmann 已提交
3767
out:
C
Christoph Hellwig 已提交
3768 3769
	kfree(skdev->msix_entries);
	skdev->msix_entries = NULL;
3770 3771 3772 3773 3774
	return rc;
}

static int skd_acquire_irq(struct skd_device *skdev)
{
C
Christoph Hellwig 已提交
3775 3776
	struct pci_dev *pdev = skdev->pdev;
	unsigned int irq_flag = PCI_IRQ_LEGACY;
3777 3778
	int rc;

C
Christoph Hellwig 已提交
3779
	if (skd_isr_type == SKD_IRQ_MSIX) {
3780 3781
		rc = skd_acquire_msix(skdev);
		if (!rc)
C
Christoph Hellwig 已提交
3782 3783
			return 0;

3784 3785
		dev_err(&skdev->pdev->dev,
			"failed to enable MSI-X, re-trying with MSI %d\n", rc);
3786
	}
C
Christoph Hellwig 已提交
3787 3788 3789 3790 3791 3792 3793 3794

	snprintf(skdev->isr_name, sizeof(skdev->isr_name), "%s%d", DRV_NAME,
			skdev->devno);

	if (skd_isr_type != SKD_IRQ_LEGACY)
		irq_flag |= PCI_IRQ_MSI;
	rc = pci_alloc_irq_vectors(pdev, 1, 1, irq_flag);
	if (rc < 0) {
3795 3796
		dev_err(&skdev->pdev->dev,
			"failed to allocate the MSI interrupt %d\n", rc);
C
Christoph Hellwig 已提交
3797 3798 3799 3800 3801 3802 3803 3804
		return rc;
	}

	rc = devm_request_irq(&pdev->dev, pdev->irq, skd_isr,
			pdev->msi_enabled ? 0 : IRQF_SHARED,
			skdev->isr_name, skdev);
	if (rc) {
		pci_free_irq_vectors(pdev);
3805 3806
		dev_err(&skdev->pdev->dev, "failed to allocate interrupt %d\n",
			rc);
C
Christoph Hellwig 已提交
3807 3808 3809 3810
		return rc;
	}

	return 0;
3811 3812 3813 3814
}

static void skd_release_irq(struct skd_device *skdev)
{
C
Christoph Hellwig 已提交
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
	struct pci_dev *pdev = skdev->pdev;

	if (skdev->msix_entries) {
		int i;

		for (i = 0; i < SKD_MAX_MSIX_COUNT; i++) {
			devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
					skdev);
		}

		kfree(skdev->msix_entries);
		skdev->msix_entries = NULL;
	} else {
		devm_free_irq(&pdev->dev, pdev->irq, skdev);
3829
	}
C
Christoph Hellwig 已提交
3830 3831

	pci_free_irq_vectors(pdev);
3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
}

/*
 *****************************************************************************
 * CONSTRUCT
 *****************************************************************************
 */

static int skd_cons_skcomp(struct skd_device *skdev)
{
	int rc = 0;
	struct fit_completion_entry_v1 *skcomp;
	u32 nbytes;

	nbytes = sizeof(*skcomp) * SKD_N_COMPLETION_ENTRY;
	nbytes += sizeof(struct fit_comp_error_info) * SKD_N_COMPLETION_ENTRY;

3849 3850 3851
	dev_dbg(&skdev->pdev->dev,
		"comp pci_alloc, total bytes %d entries %d\n",
		nbytes, SKD_N_COMPLETION_ENTRY);
3852

J
Joe Perches 已提交
3853 3854
	skcomp = pci_zalloc_consistent(skdev->pdev, nbytes,
				       &skdev->cq_dma_address);
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874

	if (skcomp == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

	skdev->skcomp_table = skcomp;
	skdev->skerr_table = (struct fit_comp_error_info *)((char *)skcomp +
							   sizeof(*skcomp) *
							   SKD_N_COMPLETION_ENTRY);

err_out:
	return rc;
}

static int skd_cons_skmsg(struct skd_device *skdev)
{
	int rc = 0;
	u32 i;

3875 3876 3877 3878
	dev_dbg(&skdev->pdev->dev,
		"skmsg_table kzalloc, struct %lu, count %u total %lu\n",
		sizeof(struct skd_fitmsg_context), skdev->num_fitmsg_context,
		sizeof(struct skd_fitmsg_context) * skdev->num_fitmsg_context);
3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923

	skdev->skmsg_table = kzalloc(sizeof(struct skd_fitmsg_context)
				     *skdev->num_fitmsg_context, GFP_KERNEL);
	if (skdev->skmsg_table == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

	for (i = 0; i < skdev->num_fitmsg_context; i++) {
		struct skd_fitmsg_context *skmsg;

		skmsg = &skdev->skmsg_table[i];

		skmsg->id = i + SKD_ID_FIT_MSG;

		skmsg->state = SKD_MSG_STATE_IDLE;
		skmsg->msg_buf = pci_alloc_consistent(skdev->pdev,
						      SKD_N_FITMSG_BYTES + 64,
						      &skmsg->mb_dma_address);

		if (skmsg->msg_buf == NULL) {
			rc = -ENOMEM;
			goto err_out;
		}

		skmsg->offset = (u32)((u64)skmsg->msg_buf &
				      (~FIT_QCMD_BASE_ADDRESS_MASK));
		skmsg->msg_buf += ~FIT_QCMD_BASE_ADDRESS_MASK;
		skmsg->msg_buf = (u8 *)((u64)skmsg->msg_buf &
				       FIT_QCMD_BASE_ADDRESS_MASK);
		skmsg->mb_dma_address += ~FIT_QCMD_BASE_ADDRESS_MASK;
		skmsg->mb_dma_address &= FIT_QCMD_BASE_ADDRESS_MASK;
		memset(skmsg->msg_buf, 0, SKD_N_FITMSG_BYTES);

		skmsg->next = &skmsg[1];
	}

	/* Free list is in order starting with the 0th entry. */
	skdev->skmsg_table[i - 1].next = NULL;
	skdev->skmsg_free_list = skdev->skmsg_table;

err_out:
	return rc;
}

3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
static struct fit_sg_descriptor *skd_cons_sg_list(struct skd_device *skdev,
						  u32 n_sg,
						  dma_addr_t *ret_dma_addr)
{
	struct fit_sg_descriptor *sg_list;
	u32 nbytes;

	nbytes = sizeof(*sg_list) * n_sg;

	sg_list = pci_alloc_consistent(skdev->pdev, nbytes, ret_dma_addr);

	if (sg_list != NULL) {
		uint64_t dma_address = *ret_dma_addr;
		u32 i;

		memset(sg_list, 0, nbytes);

		for (i = 0; i < n_sg - 1; i++) {
			uint64_t ndp_off;
			ndp_off = (i + 1) * sizeof(struct fit_sg_descriptor);

			sg_list[i].next_desc_ptr = dma_address + ndp_off;
		}
		sg_list[i].next_desc_ptr = 0LL;
	}

	return sg_list;
}

3953 3954 3955 3956 3957
static int skd_cons_skreq(struct skd_device *skdev)
{
	int rc = 0;
	u32 i;

3958 3959 3960 3961
	dev_dbg(&skdev->pdev->dev,
		"skreq_table kzalloc, struct %lu, count %u total %lu\n",
		sizeof(struct skd_request_context), skdev->num_req_context,
		sizeof(struct skd_request_context) * skdev->num_req_context);
3962 3963 3964 3965 3966 3967 3968 3969

	skdev->skreq_table = kzalloc(sizeof(struct skd_request_context)
				     * skdev->num_req_context, GFP_KERNEL);
	if (skdev->skreq_table == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

3970 3971 3972
	dev_dbg(&skdev->pdev->dev, "alloc sg_table sg_per_req %u scatlist %lu total %lu\n",
		skdev->sgs_per_request, sizeof(struct scatterlist),
		skdev->sgs_per_request * sizeof(struct scatterlist));
3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014

	for (i = 0; i < skdev->num_req_context; i++) {
		struct skd_request_context *skreq;

		skreq = &skdev->skreq_table[i];

		skreq->id = i + SKD_ID_RW_REQUEST;
		skreq->state = SKD_REQ_STATE_IDLE;

		skreq->sg = kzalloc(sizeof(struct scatterlist) *
				    skdev->sgs_per_request, GFP_KERNEL);
		if (skreq->sg == NULL) {
			rc = -ENOMEM;
			goto err_out;
		}
		sg_init_table(skreq->sg, skdev->sgs_per_request);

		skreq->sksg_list = skd_cons_sg_list(skdev,
						    skdev->sgs_per_request,
						    &skreq->sksg_dma_address);

		if (skreq->sksg_list == NULL) {
			rc = -ENOMEM;
			goto err_out;
		}

		skreq->next = &skreq[1];
	}

	/* Free list is in order starting with the 0th entry. */
	skdev->skreq_table[i - 1].next = NULL;
	skdev->skreq_free_list = skdev->skreq_table;

err_out:
	return rc;
}

static int skd_cons_skspcl(struct skd_device *skdev)
{
	int rc = 0;
	u32 i, nbytes;

4015 4016 4017 4018
	dev_dbg(&skdev->pdev->dev,
		"skspcl_table kzalloc, struct %lu, count %u total %lu\n",
		sizeof(struct skd_special_context), skdev->n_special,
		sizeof(struct skd_special_context) * skdev->n_special);
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038

	skdev->skspcl_table = kzalloc(sizeof(struct skd_special_context)
				      * skdev->n_special, GFP_KERNEL);
	if (skdev->skspcl_table == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

	for (i = 0; i < skdev->n_special; i++) {
		struct skd_special_context *skspcl;

		skspcl = &skdev->skspcl_table[i];

		skspcl->req.id = i + SKD_ID_SPECIAL_REQUEST;
		skspcl->req.state = SKD_REQ_STATE_IDLE;

		skspcl->req.next = &skspcl[1].req;

		nbytes = SKD_N_SPECIAL_FITMSG_BYTES;

J
Joe Perches 已提交
4039 4040 4041
		skspcl->msg_buf =
			pci_zalloc_consistent(skdev->pdev, nbytes,
					      &skspcl->mb_dma_address);
4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086
		if (skspcl->msg_buf == NULL) {
			rc = -ENOMEM;
			goto err_out;
		}

		skspcl->req.sg = kzalloc(sizeof(struct scatterlist) *
					 SKD_N_SG_PER_SPECIAL, GFP_KERNEL);
		if (skspcl->req.sg == NULL) {
			rc = -ENOMEM;
			goto err_out;
		}

		skspcl->req.sksg_list = skd_cons_sg_list(skdev,
							 SKD_N_SG_PER_SPECIAL,
							 &skspcl->req.
							 sksg_dma_address);
		if (skspcl->req.sksg_list == NULL) {
			rc = -ENOMEM;
			goto err_out;
		}
	}

	/* Free list is in order starting with the 0th entry. */
	skdev->skspcl_table[i - 1].req.next = NULL;
	skdev->skspcl_free_list = skdev->skspcl_table;

	return rc;

err_out:
	return rc;
}

static int skd_cons_sksb(struct skd_device *skdev)
{
	int rc = 0;
	struct skd_special_context *skspcl;
	u32 nbytes;

	skspcl = &skdev->internal_skspcl;

	skspcl->req.id = 0 + SKD_ID_INTERNAL;
	skspcl->req.state = SKD_REQ_STATE_IDLE;

	nbytes = SKD_N_INTERNAL_BYTES;

J
Joe Perches 已提交
4087 4088
	skspcl->data_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
						 &skspcl->db_dma_address);
4089 4090 4091 4092 4093 4094
	if (skspcl->data_buf == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

	nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
J
Joe Perches 已提交
4095 4096
	skspcl->msg_buf = pci_zalloc_consistent(skdev->pdev, nbytes,
						&skspcl->mb_dma_address);
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
	if (skspcl->msg_buf == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

	skspcl->req.sksg_list = skd_cons_sg_list(skdev, 1,
						 &skspcl->req.sksg_dma_address);
	if (skspcl->req.sksg_list == NULL) {
		rc = -ENOMEM;
		goto err_out;
	}

	if (!skd_format_internal_skspcl(skdev)) {
		rc = -EINVAL;
		goto err_out;
	}

err_out:
	return rc;
}

static int skd_cons_disk(struct skd_device *skdev)
{
	int rc = 0;
	struct gendisk *disk;
	struct request_queue *q;
	unsigned long flags;

	disk = alloc_disk(SKD_MINORS_PER_DEVICE);
	if (!disk) {
		rc = -ENOMEM;
		goto err_out;
	}

	skdev->disk = disk;
	sprintf(disk->disk_name, DRV_NAME "%u", skdev->devno);

	disk->major = skdev->major;
	disk->first_minor = skdev->devno * SKD_MINORS_PER_DEVICE;
	disk->fops = &skd_blockdev_ops;
	disk->private_data = skdev;

J
Jens Axboe 已提交
4139
	q = blk_init_queue(skd_request_fn, &skdev->lock);
4140 4141 4142 4143
	if (!q) {
		rc = -ENOMEM;
		goto err_out;
	}
4144
	blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
4145 4146 4147 4148 4149

	skdev->queue = q;
	disk->queue = q;
	q->queuedata = skdev;

4150
	blk_queue_write_cache(q, true, true);
4151 4152 4153
	blk_queue_max_segments(q, skdev->sgs_per_request);
	blk_queue_max_hw_sectors(q, SKD_N_MAX_SECTORS);

4154
	/* set optimal I/O size to 8KB */
4155 4156 4157
	blk_queue_io_opt(q, 8192);

	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4158
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
4159 4160

	spin_lock_irqsave(&skdev->lock, flags);
4161
	dev_dbg(&skdev->pdev->dev, "stopping queue\n");
4162
	blk_stop_queue(skdev->queue);
4163 4164 4165 4166 4167 4168
	spin_unlock_irqrestore(&skdev->lock, flags);

err_out:
	return rc;
}

4169 4170
#define SKD_N_DEV_TABLE         16u
static u32 skd_next_devno;
4171

4172
static struct skd_device *skd_construct(struct pci_dev *pdev)
4173
{
4174 4175 4176
	struct skd_device *skdev;
	int blk_major = skd_major;
	int rc;
4177

4178
	skdev = kzalloc(sizeof(*skdev), GFP_KERNEL);
4179

4180
	if (!skdev) {
4181
		dev_err(&pdev->dev, "memory alloc failure\n");
4182 4183
		return NULL;
	}
4184

4185 4186 4187 4188 4189
	skdev->state = SKD_DRVR_STATE_LOAD;
	skdev->pdev = pdev;
	skdev->devno = skd_next_devno++;
	skdev->major = blk_major;
	skdev->dev_max_queue_depth = 0;
4190

4191 4192 4193 4194 4195 4196 4197 4198
	skdev->num_req_context = skd_max_queue_depth;
	skdev->num_fitmsg_context = skd_max_queue_depth;
	skdev->n_special = skd_max_pass_thru;
	skdev->cur_max_queue_depth = 1;
	skdev->queue_low_water_mark = 1;
	skdev->proto_ver = 99;
	skdev->sgs_per_request = skd_sgs_per_request;
	skdev->dbg_level = skd_dbg_level;
4199

4200 4201 4202
	spin_lock_init(&skdev->lock);

	INIT_WORK(&skdev->completion_worker, skd_completion_worker);
4203

4204
	dev_dbg(&skdev->pdev->dev, "skcomp\n");
4205 4206 4207
	rc = skd_cons_skcomp(skdev);
	if (rc < 0)
		goto err_out;
4208

4209
	dev_dbg(&skdev->pdev->dev, "skmsg\n");
4210 4211 4212 4213
	rc = skd_cons_skmsg(skdev);
	if (rc < 0)
		goto err_out;

4214
	dev_dbg(&skdev->pdev->dev, "skreq\n");
4215 4216 4217 4218
	rc = skd_cons_skreq(skdev);
	if (rc < 0)
		goto err_out;

4219
	dev_dbg(&skdev->pdev->dev, "skspcl\n");
4220 4221 4222 4223
	rc = skd_cons_skspcl(skdev);
	if (rc < 0)
		goto err_out;

4224
	dev_dbg(&skdev->pdev->dev, "sksb\n");
4225 4226 4227 4228
	rc = skd_cons_sksb(skdev);
	if (rc < 0)
		goto err_out;

4229
	dev_dbg(&skdev->pdev->dev, "disk\n");
4230 4231 4232 4233
	rc = skd_cons_disk(skdev);
	if (rc < 0)
		goto err_out;

4234
	dev_dbg(&skdev->pdev->dev, "VICTORY\n");
4235 4236 4237
	return skdev;

err_out:
4238
	dev_dbg(&skdev->pdev->dev, "construct failed\n");
4239 4240
	skd_destruct(skdev);
	return NULL;
4241 4242
}

4243 4244 4245 4246 4247 4248
/*
 *****************************************************************************
 * DESTRUCT (FREE)
 *****************************************************************************
 */

4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290
static void skd_free_skcomp(struct skd_device *skdev)
{
	if (skdev->skcomp_table != NULL) {
		u32 nbytes;

		nbytes = sizeof(skdev->skcomp_table[0]) *
			 SKD_N_COMPLETION_ENTRY;
		pci_free_consistent(skdev->pdev, nbytes,
				    skdev->skcomp_table, skdev->cq_dma_address);
	}

	skdev->skcomp_table = NULL;
	skdev->cq_dma_address = 0;
}

static void skd_free_skmsg(struct skd_device *skdev)
{
	u32 i;

	if (skdev->skmsg_table == NULL)
		return;

	for (i = 0; i < skdev->num_fitmsg_context; i++) {
		struct skd_fitmsg_context *skmsg;

		skmsg = &skdev->skmsg_table[i];

		if (skmsg->msg_buf != NULL) {
			skmsg->msg_buf += skmsg->offset;
			skmsg->mb_dma_address += skmsg->offset;
			pci_free_consistent(skdev->pdev, SKD_N_FITMSG_BYTES,
					    skmsg->msg_buf,
					    skmsg->mb_dma_address);
		}
		skmsg->msg_buf = NULL;
		skmsg->mb_dma_address = 0;
	}

	kfree(skdev->skmsg_table);
	skdev->skmsg_table = NULL;
}

4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303
static void skd_free_sg_list(struct skd_device *skdev,
			     struct fit_sg_descriptor *sg_list,
			     u32 n_sg, dma_addr_t dma_addr)
{
	if (sg_list != NULL) {
		u32 nbytes;

		nbytes = sizeof(*sg_list) * n_sg;

		pci_free_consistent(skdev->pdev, nbytes, sg_list, dma_addr);
	}
}

4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
static void skd_free_skreq(struct skd_device *skdev)
{
	u32 i;

	if (skdev->skreq_table == NULL)
		return;

	for (i = 0; i < skdev->num_req_context; i++) {
		struct skd_request_context *skreq;

		skreq = &skdev->skreq_table[i];

		skd_free_sg_list(skdev, skreq->sksg_list,
				 skdev->sgs_per_request,
				 skreq->sksg_dma_address);

		skreq->sksg_list = NULL;
		skreq->sksg_dma_address = 0;

		kfree(skreq->sg);
	}

	kfree(skdev->skreq_table);
	skdev->skreq_table = NULL;
}

static void skd_free_skspcl(struct skd_device *skdev)
{
	u32 i;
	u32 nbytes;

	if (skdev->skspcl_table == NULL)
		return;

	for (i = 0; i < skdev->n_special; i++) {
		struct skd_special_context *skspcl;

		skspcl = &skdev->skspcl_table[i];

		if (skspcl->msg_buf != NULL) {
			nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
			pci_free_consistent(skdev->pdev, nbytes,
					    skspcl->msg_buf,
					    skspcl->mb_dma_address);
		}

		skspcl->msg_buf = NULL;
		skspcl->mb_dma_address = 0;

		skd_free_sg_list(skdev, skspcl->req.sksg_list,
				 SKD_N_SG_PER_SPECIAL,
				 skspcl->req.sksg_dma_address);

		skspcl->req.sksg_list = NULL;
		skspcl->req.sksg_dma_address = 0;

		kfree(skspcl->req.sg);
	}

	kfree(skdev->skspcl_table);
	skdev->skspcl_table = NULL;
}

static void skd_free_sksb(struct skd_device *skdev)
{
	struct skd_special_context *skspcl;
	u32 nbytes;

	skspcl = &skdev->internal_skspcl;

	if (skspcl->data_buf != NULL) {
		nbytes = SKD_N_INTERNAL_BYTES;

		pci_free_consistent(skdev->pdev, nbytes,
				    skspcl->data_buf, skspcl->db_dma_address);
	}

	skspcl->data_buf = NULL;
	skspcl->db_dma_address = 0;

	if (skspcl->msg_buf != NULL) {
		nbytes = SKD_N_SPECIAL_FITMSG_BYTES;
		pci_free_consistent(skdev->pdev, nbytes,
				    skspcl->msg_buf, skspcl->mb_dma_address);
	}

	skspcl->msg_buf = NULL;
	skspcl->mb_dma_address = 0;

	skd_free_sg_list(skdev, skspcl->req.sksg_list, 1,
			 skspcl->req.sksg_dma_address);

	skspcl->req.sksg_list = NULL;
	skspcl->req.sksg_dma_address = 0;
}

static void skd_free_disk(struct skd_device *skdev)
{
	struct gendisk *disk = skdev->disk;

4404 4405 4406 4407 4408 4409 4410
	if (disk && (disk->flags & GENHD_FL_UP))
		del_gendisk(disk);

	if (skdev->queue) {
		blk_cleanup_queue(skdev->queue);
		skdev->queue = NULL;
		disk->queue = NULL;
4411
	}
4412 4413

	put_disk(disk);
4414 4415 4416
	skdev->disk = NULL;
}

4417 4418 4419 4420 4421
static void skd_destruct(struct skd_device *skdev)
{
	if (skdev == NULL)
		return;

4422
	dev_dbg(&skdev->pdev->dev, "disk\n");
4423 4424
	skd_free_disk(skdev);

4425
	dev_dbg(&skdev->pdev->dev, "sksb\n");
4426 4427
	skd_free_sksb(skdev);

4428
	dev_dbg(&skdev->pdev->dev, "skspcl\n");
4429 4430
	skd_free_skspcl(skdev);

4431
	dev_dbg(&skdev->pdev->dev, "skreq\n");
4432 4433
	skd_free_skreq(skdev);

4434
	dev_dbg(&skdev->pdev->dev, "skmsg\n");
4435
	skd_free_skmsg(skdev);
4436

4437
	dev_dbg(&skdev->pdev->dev, "skcomp\n");
4438 4439
	skd_free_skcomp(skdev);

4440
	dev_dbg(&skdev->pdev->dev, "skdev\n");
4441 4442
	kfree(skdev);
}
4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456

/*
 *****************************************************************************
 * BLOCK DEVICE (BDEV) GLUE
 *****************************************************************************
 */

static int skd_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
	struct skd_device *skdev;
	u64 capacity;

	skdev = bdev->bd_disk->private_data;

4457 4458
	dev_dbg(&skdev->pdev->dev, "%s: CMD[%s] getgeo device\n",
		bdev->bd_disk->disk_name, current->comm);
4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470

	if (skdev->read_cap_is_valid) {
		capacity = get_capacity(skdev->disk);
		geo->heads = 64;
		geo->sectors = 255;
		geo->cylinders = (capacity) / (255 * 64);

		return 0;
	}
	return -EIO;
}

4471
static int skd_bdev_attach(struct device *parent, struct skd_device *skdev)
4472
{
4473
	dev_dbg(&skdev->pdev->dev, "add_disk\n");
4474
	device_add_disk(parent, skdev->disk);
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489
	return 0;
}

static const struct block_device_operations skd_blockdev_ops = {
	.owner		= THIS_MODULE,
	.ioctl		= skd_bdev_ioctl,
	.getgeo		= skd_bdev_getgeo,
};

/*
 *****************************************************************************
 * PCIe DRIVER GLUE
 *****************************************************************************
 */

4490
static const struct pci_device_id skd_pci_tbl[] = {
4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
	{ PCI_VENDOR_ID_STEC, PCI_DEVICE_ID_S1120,
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
	{ 0 }                     /* terminate list */
};

MODULE_DEVICE_TABLE(pci, skd_pci_tbl);

static char *skd_pci_info(struct skd_device *skdev, char *str)
{
	int pcie_reg;

	strcpy(str, "PCIe (");
	pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);

	if (pcie_reg) {

		char lwstr[6];
		uint16_t pcie_lstat, lspeed, lwidth;

		pcie_reg += 0x12;
		pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
		lspeed = pcie_lstat & (0xF);
		lwidth = (pcie_lstat & 0x3F0) >> 4;

		if (lspeed == 1)
			strcat(str, "2.5GT/s ");
		else if (lspeed == 2)
			strcat(str, "5.0GT/s ");
		else
			strcat(str, "<unknown> ");
		snprintf(lwstr, sizeof(lwstr), "%dX)", lwidth);
		strcat(str, lwstr);
	}
	return str;
}

static int skd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	int i;
	int rc = 0;
	char pci_str[32];
	struct skd_device *skdev;

4534 4535 4536 4537
	dev_info(&pdev->dev, "STEC s1120 Driver(%s) version %s-b%s\n",
		 DRV_NAME, DRV_VERSION, DRV_BUILD_ID);
	dev_info(&pdev->dev, "vendor=%04X device=%04x\n", pdev->vendor,
		 pdev->device);
4538 4539 4540 4541 4542 4543 4544 4545 4546 4547

	rc = pci_enable_device(pdev);
	if (rc)
		return rc;
	rc = pci_request_regions(pdev, DRV_NAME);
	if (rc)
		goto err_out;
	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
	if (!rc) {
		if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
4548 4549
			dev_err(&pdev->dev, "consistent DMA mask error %d\n",
				rc);
4550 4551
		}
	} else {
4552
		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4553
		if (rc) {
4554
			dev_err(&pdev->dev, "DMA mask error %d\n", rc);
4555 4556 4557 4558
			goto err_out_regions;
		}
	}

4559 4560 4561 4562 4563 4564 4565 4566
	if (!skd_major) {
		rc = register_blkdev(0, DRV_NAME);
		if (rc < 0)
			goto err_out_regions;
		BUG_ON(!rc);
		skd_major = rc;
	}

4567
	skdev = skd_construct(pdev);
4568 4569
	if (skdev == NULL) {
		rc = -ENOMEM;
4570
		goto err_out_regions;
4571
	}
4572 4573

	skd_pci_info(skdev, pci_str);
4574
	dev_info(&pdev->dev, "%s 64bit\n", pci_str);
4575 4576 4577 4578

	pci_set_master(pdev);
	rc = pci_enable_pcie_error_reporting(pdev);
	if (rc) {
4579 4580
		dev_err(&pdev->dev,
			"bad enable of PCIe error reporting rc=%d\n", rc);
4581 4582 4583 4584 4585
		skdev->pcie_error_reporting_is_enabled = 0;
	} else
		skdev->pcie_error_reporting_is_enabled = 1;

	pci_set_drvdata(pdev, skdev);
4586

4587 4588 4589 4590 4591 4592
	for (i = 0; i < SKD_MAX_BARS; i++) {
		skdev->mem_phys[i] = pci_resource_start(pdev, i);
		skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
		skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
					    skdev->mem_size[i]);
		if (!skdev->mem_map[i]) {
4593 4594
			dev_err(&pdev->dev,
				"Unable to map adapter memory!\n");
4595 4596 4597
			rc = -ENODEV;
			goto err_out_iounmap;
		}
4598 4599 4600
		dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
			skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
			skdev->mem_size[i]);
4601 4602 4603 4604
	}

	rc = skd_acquire_irq(skdev);
	if (rc) {
4605
		dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621
		goto err_out_iounmap;
	}

	rc = skd_start_timer(skdev);
	if (rc)
		goto err_out_timer;

	init_waitqueue_head(&skdev->waitq);

	skd_start_device(skdev);

	rc = wait_event_interruptible_timeout(skdev->waitq,
					      (skdev->gendisk_on),
					      (SKD_START_WAIT_SECONDS * HZ));
	if (skdev->gendisk_on > 0) {
		/* device came on-line after reset */
4622
		skd_bdev_attach(&pdev->dev, skdev);
4623 4624 4625 4626
		rc = 0;
	} else {
		/* we timed out, something is wrong with the device,
		   don't add the disk structure */
4627 4628
		dev_err(&pdev->dev, "error: waiting for s1120 timed out %d!\n",
			rc);
4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666
		/* in case of no error; we timeout with ENXIO */
		if (!rc)
			rc = -ENXIO;
		goto err_out_timer;
	}

	return rc;

err_out_timer:
	skd_stop_device(skdev);
	skd_release_irq(skdev);

err_out_iounmap:
	for (i = 0; i < SKD_MAX_BARS; i++)
		if (skdev->mem_map[i])
			iounmap(skdev->mem_map[i]);

	if (skdev->pcie_error_reporting_is_enabled)
		pci_disable_pcie_error_reporting(pdev);

	skd_destruct(skdev);

err_out_regions:
	pci_release_regions(pdev);

err_out:
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	return rc;
}

static void skd_pci_remove(struct pci_dev *pdev)
{
	int i;
	struct skd_device *skdev;

	skdev = pci_get_drvdata(pdev);
	if (!skdev) {
4667
		dev_err(&pdev->dev, "no device data for PCI\n");
4668 4669 4670 4671 4672 4673 4674
		return;
	}
	skd_stop_device(skdev);
	skd_release_irq(skdev);

	for (i = 0; i < SKD_MAX_BARS; i++)
		if (skdev->mem_map[i])
4675
			iounmap(skdev->mem_map[i]);
4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695

	if (skdev->pcie_error_reporting_is_enabled)
		pci_disable_pcie_error_reporting(pdev);

	skd_destruct(skdev);

	pci_release_regions(pdev);
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);

	return;
}

static int skd_pci_suspend(struct pci_dev *pdev, pm_message_t state)
{
	int i;
	struct skd_device *skdev;

	skdev = pci_get_drvdata(pdev);
	if (!skdev) {
4696
		dev_err(&pdev->dev, "no device data for PCI\n");
4697 4698 4699 4700 4701 4702 4703 4704 4705
		return -EIO;
	}

	skd_stop_device(skdev);

	skd_release_irq(skdev);

	for (i = 0; i < SKD_MAX_BARS; i++)
		if (skdev->mem_map[i])
4706
			iounmap(skdev->mem_map[i]);
4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725

	if (skdev->pcie_error_reporting_is_enabled)
		pci_disable_pcie_error_reporting(pdev);

	pci_release_regions(pdev);
	pci_save_state(pdev);
	pci_disable_device(pdev);
	pci_set_power_state(pdev, pci_choose_state(pdev, state));
	return 0;
}

static int skd_pci_resume(struct pci_dev *pdev)
{
	int i;
	int rc = 0;
	struct skd_device *skdev;

	skdev = pci_get_drvdata(pdev);
	if (!skdev) {
4726
		dev_err(&pdev->dev, "no device data for PCI\n");
4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743
		return -1;
	}

	pci_set_power_state(pdev, PCI_D0);
	pci_enable_wake(pdev, PCI_D0, 0);
	pci_restore_state(pdev);

	rc = pci_enable_device(pdev);
	if (rc)
		return rc;
	rc = pci_request_regions(pdev, DRV_NAME);
	if (rc)
		goto err_out;
	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
	if (!rc) {
		if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {

4744 4745
			dev_err(&pdev->dev, "consistent DMA mask error %d\n",
				rc);
4746 4747 4748 4749 4750
		}
	} else {
		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
		if (rc) {

4751
			dev_err(&pdev->dev, "DMA mask error %d\n", rc);
4752 4753 4754 4755 4756 4757 4758
			goto err_out_regions;
		}
	}

	pci_set_master(pdev);
	rc = pci_enable_pcie_error_reporting(pdev);
	if (rc) {
4759 4760
		dev_err(&pdev->dev,
			"bad enable of PCIe error reporting rc=%d\n", rc);
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
		skdev->pcie_error_reporting_is_enabled = 0;
	} else
		skdev->pcie_error_reporting_is_enabled = 1;

	for (i = 0; i < SKD_MAX_BARS; i++) {

		skdev->mem_phys[i] = pci_resource_start(pdev, i);
		skdev->mem_size[i] = (u32)pci_resource_len(pdev, i);
		skdev->mem_map[i] = ioremap(skdev->mem_phys[i],
					    skdev->mem_size[i]);
		if (!skdev->mem_map[i]) {
4772
			dev_err(&pdev->dev, "Unable to map adapter memory!\n");
4773 4774 4775
			rc = -ENODEV;
			goto err_out_iounmap;
		}
4776 4777 4778
		dev_dbg(&pdev->dev, "mem_map=%p, phyd=%016llx, size=%d\n",
			skdev->mem_map[i], (uint64_t)skdev->mem_phys[i],
			skdev->mem_size[i]);
4779 4780 4781
	}
	rc = skd_acquire_irq(skdev);
	if (rc) {
4782
		dev_err(&pdev->dev, "interrupt resource error %d\n", rc);
4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819
		goto err_out_iounmap;
	}

	rc = skd_start_timer(skdev);
	if (rc)
		goto err_out_timer;

	init_waitqueue_head(&skdev->waitq);

	skd_start_device(skdev);

	return rc;

err_out_timer:
	skd_stop_device(skdev);
	skd_release_irq(skdev);

err_out_iounmap:
	for (i = 0; i < SKD_MAX_BARS; i++)
		if (skdev->mem_map[i])
			iounmap(skdev->mem_map[i]);

	if (skdev->pcie_error_reporting_is_enabled)
		pci_disable_pcie_error_reporting(pdev);

err_out_regions:
	pci_release_regions(pdev);

err_out:
	pci_disable_device(pdev);
	return rc;
}

static void skd_pci_shutdown(struct pci_dev *pdev)
{
	struct skd_device *skdev;

4820
	dev_err(&pdev->dev, "%s called\n", __func__);
4821 4822 4823

	skdev = pci_get_drvdata(pdev);
	if (!skdev) {
4824
		dev_err(&pdev->dev, "no device data for PCI\n");
4825 4826 4827
		return;
	}

4828
	dev_err(&pdev->dev, "calling stop\n");
4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926
	skd_stop_device(skdev);
}

static struct pci_driver skd_driver = {
	.name		= DRV_NAME,
	.id_table	= skd_pci_tbl,
	.probe		= skd_pci_probe,
	.remove		= skd_pci_remove,
	.suspend	= skd_pci_suspend,
	.resume		= skd_pci_resume,
	.shutdown	= skd_pci_shutdown,
};

/*
 *****************************************************************************
 * LOGGING SUPPORT
 *****************************************************************************
 */

const char *skd_drive_state_to_str(int state)
{
	switch (state) {
	case FIT_SR_DRIVE_OFFLINE:
		return "OFFLINE";
	case FIT_SR_DRIVE_INIT:
		return "INIT";
	case FIT_SR_DRIVE_ONLINE:
		return "ONLINE";
	case FIT_SR_DRIVE_BUSY:
		return "BUSY";
	case FIT_SR_DRIVE_FAULT:
		return "FAULT";
	case FIT_SR_DRIVE_DEGRADED:
		return "DEGRADED";
	case FIT_SR_PCIE_LINK_DOWN:
		return "INK_DOWN";
	case FIT_SR_DRIVE_SOFT_RESET:
		return "SOFT_RESET";
	case FIT_SR_DRIVE_NEED_FW_DOWNLOAD:
		return "NEED_FW";
	case FIT_SR_DRIVE_INIT_FAULT:
		return "INIT_FAULT";
	case FIT_SR_DRIVE_BUSY_SANITIZE:
		return "BUSY_SANITIZE";
	case FIT_SR_DRIVE_BUSY_ERASE:
		return "BUSY_ERASE";
	case FIT_SR_DRIVE_FW_BOOTING:
		return "FW_BOOTING";
	default:
		return "???";
	}
}

const char *skd_skdev_state_to_str(enum skd_drvr_state state)
{
	switch (state) {
	case SKD_DRVR_STATE_LOAD:
		return "LOAD";
	case SKD_DRVR_STATE_IDLE:
		return "IDLE";
	case SKD_DRVR_STATE_BUSY:
		return "BUSY";
	case SKD_DRVR_STATE_STARTING:
		return "STARTING";
	case SKD_DRVR_STATE_ONLINE:
		return "ONLINE";
	case SKD_DRVR_STATE_PAUSING:
		return "PAUSING";
	case SKD_DRVR_STATE_PAUSED:
		return "PAUSED";
	case SKD_DRVR_STATE_DRAINING_TIMEOUT:
		return "DRAINING_TIMEOUT";
	case SKD_DRVR_STATE_RESTARTING:
		return "RESTARTING";
	case SKD_DRVR_STATE_RESUMING:
		return "RESUMING";
	case SKD_DRVR_STATE_STOPPING:
		return "STOPPING";
	case SKD_DRVR_STATE_SYNCING:
		return "SYNCING";
	case SKD_DRVR_STATE_FAULT:
		return "FAULT";
	case SKD_DRVR_STATE_DISAPPEARED:
		return "DISAPPEARED";
	case SKD_DRVR_STATE_BUSY_ERASE:
		return "BUSY_ERASE";
	case SKD_DRVR_STATE_BUSY_SANITIZE:
		return "BUSY_SANITIZE";
	case SKD_DRVR_STATE_BUSY_IMMINENT:
		return "BUSY_IMMINENT";
	case SKD_DRVR_STATE_WAIT_BOOT:
		return "WAIT_BOOT";

	default:
		return "???";
	}
}

4927
static const char *skd_skmsg_state_to_str(enum skd_fit_msg_state state)
4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938
{
	switch (state) {
	case SKD_MSG_STATE_IDLE:
		return "IDLE";
	case SKD_MSG_STATE_BUSY:
		return "BUSY";
	default:
		return "???";
	}
}

4939
static const char *skd_skreq_state_to_str(enum skd_req_state state)
4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960
{
	switch (state) {
	case SKD_REQ_STATE_IDLE:
		return "IDLE";
	case SKD_REQ_STATE_SETUP:
		return "SETUP";
	case SKD_REQ_STATE_BUSY:
		return "BUSY";
	case SKD_REQ_STATE_COMPLETED:
		return "COMPLETED";
	case SKD_REQ_STATE_TIMEOUT:
		return "TIMEOUT";
	case SKD_REQ_STATE_ABORTED:
		return "ABORTED";
	default:
		return "???";
	}
}

static void skd_log_skdev(struct skd_device *skdev, const char *event)
{
4961 4962 4963 4964 4965 4966 4967 4968 4969
	dev_dbg(&skdev->pdev->dev, "skdev=%p event='%s'\n", skdev, event);
	dev_dbg(&skdev->pdev->dev, "  drive_state=%s(%d) driver_state=%s(%d)\n",
		skd_drive_state_to_str(skdev->drive_state), skdev->drive_state,
		skd_skdev_state_to_str(skdev->state), skdev->state);
	dev_dbg(&skdev->pdev->dev, "  busy=%d limit=%d dev=%d lowat=%d\n",
		skdev->in_flight, skdev->cur_max_queue_depth,
		skdev->dev_max_queue_depth, skdev->queue_low_water_mark);
	dev_dbg(&skdev->pdev->dev, "  timestamp=0x%x cycle=%d cycle_ix=%d\n",
		skdev->timeout_stamp, skdev->skcomp_cycle, skdev->skcomp_ix);
4970 4971 4972 4973 4974
}

static void skd_log_skmsg(struct skd_device *skdev,
			  struct skd_fitmsg_context *skmsg, const char *event)
{
4975 4976 4977 4978
	dev_dbg(&skdev->pdev->dev, "skmsg=%p event='%s'\n", skmsg, event);
	dev_dbg(&skdev->pdev->dev, "  state=%s(%d) id=0x%04x length=%d\n",
		skd_skmsg_state_to_str(skmsg->state), skmsg->state, skmsg->id,
		skmsg->length);
4979 4980 4981 4982 4983
}

static void skd_log_skreq(struct skd_device *skdev,
			  struct skd_request_context *skreq, const char *event)
{
4984 4985 4986 4987 4988 4989
	dev_dbg(&skdev->pdev->dev, "skreq=%p event='%s'\n", skreq, event);
	dev_dbg(&skdev->pdev->dev, "  state=%s(%d) id=0x%04x fitmsg=0x%04x\n",
		skd_skreq_state_to_str(skreq->state), skreq->state, skreq->id,
		skreq->fitmsg_id);
	dev_dbg(&skdev->pdev->dev, "  timo=0x%x sg_dir=%d n_sg=%d\n",
		skreq->timeout_stamp, skreq->sg_data_dir, skreq->n_sg);
4990

J
Jens Axboe 已提交
4991 4992 4993 4994
	if (skreq->req != NULL) {
		struct request *req = skreq->req;
		u32 lba = (u32)blk_rq_pos(req);
		u32 count = blk_rq_sectors(req);
4995

4996 4997 4998
		dev_dbg(&skdev->pdev->dev,
			"req=%p lba=%u(0x%x) count=%u(0x%x) dir=%d\n", req,
			lba, lba, count, count, (int)rq_data_dir(req));
J
Jens Axboe 已提交
4999
	} else
5000
		dev_dbg(&skdev->pdev->dev, "req=NULL\n");
5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018
}

/*
 *****************************************************************************
 * MODULE GLUE
 *****************************************************************************
 */

static int __init skd_init(void)
{
	pr_info(PFX " v%s-b%s loaded\n", DRV_VERSION, DRV_BUILD_ID);

	switch (skd_isr_type) {
	case SKD_IRQ_LEGACY:
	case SKD_IRQ_MSI:
	case SKD_IRQ_MSIX:
		break;
	default:
5019
		pr_err(PFX "skd_isr_type %d invalid, re-set to %d\n",
5020 5021 5022 5023
		       skd_isr_type, SKD_IRQ_DEFAULT);
		skd_isr_type = SKD_IRQ_DEFAULT;
	}

5024 5025 5026
	if (skd_max_queue_depth < 1 ||
	    skd_max_queue_depth > SKD_MAX_QUEUE_DEPTH) {
		pr_err(PFX "skd_max_queue_depth %d invalid, re-set to %d\n",
5027 5028 5029 5030 5031
		       skd_max_queue_depth, SKD_MAX_QUEUE_DEPTH_DEFAULT);
		skd_max_queue_depth = SKD_MAX_QUEUE_DEPTH_DEFAULT;
	}

	if (skd_max_req_per_msg < 1 || skd_max_req_per_msg > 14) {
5032
		pr_err(PFX "skd_max_req_per_msg %d invalid, re-set to %d\n",
5033 5034 5035 5036 5037
		       skd_max_req_per_msg, SKD_MAX_REQ_PER_MSG_DEFAULT);
		skd_max_req_per_msg = SKD_MAX_REQ_PER_MSG_DEFAULT;
	}

	if (skd_sgs_per_request < 1 || skd_sgs_per_request > 4096) {
5038
		pr_err(PFX "skd_sg_per_request %d invalid, re-set to %d\n",
5039 5040 5041 5042 5043
		       skd_sgs_per_request, SKD_N_SG_PER_REQ_DEFAULT);
		skd_sgs_per_request = SKD_N_SG_PER_REQ_DEFAULT;
	}

	if (skd_dbg_level < 0 || skd_dbg_level > 2) {
5044
		pr_err(PFX "skd_dbg_level %d invalid, re-set to %d\n",
5045 5046 5047 5048 5049
		       skd_dbg_level, 0);
		skd_dbg_level = 0;
	}

	if (skd_isr_comp_limit < 0) {
5050
		pr_err(PFX "skd_isr_comp_limit %d invalid, set to %d\n",
5051 5052 5053 5054 5055
		       skd_isr_comp_limit, 0);
		skd_isr_comp_limit = 0;
	}

	if (skd_max_pass_thru < 1 || skd_max_pass_thru > 50) {
5056
		pr_err(PFX "skd_max_pass_thru %d invalid, re-set to %d\n",
5057 5058 5059 5060
		       skd_max_pass_thru, SKD_N_SPECIAL_CONTEXT);
		skd_max_pass_thru = SKD_N_SPECIAL_CONTEXT;
	}

5061
	return pci_register_driver(&skd_driver);
5062 5063 5064 5065 5066 5067 5068
}

static void __exit skd_exit(void)
{
	pr_info(PFX " v%s-b%s unloading\n", DRV_VERSION, DRV_BUILD_ID);

	pci_unregister_driver(&skd_driver);
5069 5070 5071

	if (skd_major)
		unregister_blkdev(skd_major, DRV_NAME);
5072 5073 5074 5075
}

module_init(skd_init);
module_exit(skd_exit);