blob: 19946031fa9af97b73c2e2c16812a7f7385811ef [file] [log] [blame]
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001/*
2 * SuperTrak EX Series Storage Controller driver for Linux
3 *
Charles1ec364e2016-02-22 20:02:02 +08004 * Copyright (C) 2005-2015 Promise Technology Inc.
Jeff Garzik5a25ba12006-09-01 03:12:19 -04005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * Written By:
12 * Ed Lin <promise_linux@promise.com>
13 *
Jeff Garzik5a25ba12006-09-01 03:12:19 -040014 */
15
16#include <linux/init.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jeff Garzik5a25ba12006-09-01 03:12:19 -040021#include <linux/time.h>
22#include <linux/pci.h>
23#include <linux/blkdev.h>
24#include <linux/interrupt.h>
25#include <linux/types.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
Tina Ruchandani0da39682015-10-30 01:30:40 -070028#include <linux/ktime.h>
Jeff Garzik5a25ba12006-09-01 03:12:19 -040029#include <asm/io.h>
30#include <asm/irq.h>
31#include <asm/byteorder.h>
32#include <scsi/scsi.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_cmnd.h>
35#include <scsi/scsi_host.h>
Ed Lincf355882006-09-01 14:31:51 +080036#include <scsi/scsi_tcq.h>
Ed Linc25da0a2007-05-09 20:50:42 -080037#include <scsi/scsi_dbg.h>
FUJITA Tomonori11002fb2008-03-25 09:26:52 +090038#include <scsi/scsi_eh.h>
Jeff Garzik5a25ba12006-09-01 03:12:19 -040039
40#define DRV_NAME "stex"
Charles1ec364e2016-02-22 20:02:02 +080041#define ST_DRIVER_VERSION "5.00.0000.01"
42#define ST_VER_MAJOR 5
43#define ST_VER_MINOR 00
44#define ST_OEM 0000
45#define ST_BUILD_VER 01
Jeff Garzik5a25ba12006-09-01 03:12:19 -040046
47enum {
48 /* MU register offset */
49 IMR0 = 0x10, /* MU_INBOUND_MESSAGE_REG0 */
50 IMR1 = 0x14, /* MU_INBOUND_MESSAGE_REG1 */
51 OMR0 = 0x18, /* MU_OUTBOUND_MESSAGE_REG0 */
52 OMR1 = 0x1c, /* MU_OUTBOUND_MESSAGE_REG1 */
53 IDBL = 0x20, /* MU_INBOUND_DOORBELL */
54 IIS = 0x24, /* MU_INBOUND_INTERRUPT_STATUS */
55 IIM = 0x28, /* MU_INBOUND_INTERRUPT_MASK */
56 ODBL = 0x2c, /* MU_OUTBOUND_DOORBELL */
57 OIS = 0x30, /* MU_OUTBOUND_INTERRUPT_STATUS */
58 OIM = 0x3c, /* MU_OUTBOUND_INTERRUPT_MASK */
59
Ed Lin69cb4872009-08-18 12:15:14 -070060 YIOA_STATUS = 0x00,
Ed Lin0f3f6ee2009-03-31 17:30:36 -080061 YH2I_INT = 0x20,
62 YINT_EN = 0x34,
63 YI2H_INT = 0x9c,
64 YI2H_INT_C = 0xa0,
65 YH2I_REQ = 0xc0,
66 YH2I_REQ_HI = 0xc4,
67
Jeff Garzik5a25ba12006-09-01 03:12:19 -040068 /* MU register value */
Ed Lin9eb46d22009-09-28 22:58:33 -080069 MU_INBOUND_DOORBELL_HANDSHAKE = (1 << 0),
70 MU_INBOUND_DOORBELL_REQHEADCHANGED = (1 << 1),
71 MU_INBOUND_DOORBELL_STATUSTAILCHANGED = (1 << 2),
72 MU_INBOUND_DOORBELL_HMUSTOPPED = (1 << 3),
73 MU_INBOUND_DOORBELL_RESET = (1 << 4),
Jeff Garzik5a25ba12006-09-01 03:12:19 -040074
Ed Lin9eb46d22009-09-28 22:58:33 -080075 MU_OUTBOUND_DOORBELL_HANDSHAKE = (1 << 0),
76 MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED = (1 << 1),
77 MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED = (1 << 2),
78 MU_OUTBOUND_DOORBELL_BUSCHANGE = (1 << 3),
79 MU_OUTBOUND_DOORBELL_HASEVENT = (1 << 4),
80 MU_OUTBOUND_DOORBELL_REQUEST_RESET = (1 << 27),
Jeff Garzik5a25ba12006-09-01 03:12:19 -040081
82 /* MU status code */
83 MU_STATE_STARTING = 1,
Ed Lin9eb46d22009-09-28 22:58:33 -080084 MU_STATE_STARTED = 2,
85 MU_STATE_RESETTING = 3,
86 MU_STATE_FAILED = 4,
Charles45b42ad2016-02-22 20:04:25 +080087 MU_STATE_STOP = 5,
88 MU_STATE_NOCONNECT = 6,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040089
Ed Lin76fbf962006-12-04 17:49:42 -080090 MU_MAX_DELAY = 120,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040091 MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
Ed Lin529e7a62006-12-04 17:49:34 -080092 MU_HANDSHAKE_SIGNATURE_HALF = 0x5a5a0000,
Ed Lin76fbf962006-12-04 17:49:42 -080093 MU_HARD_RESET_WAIT = 30000,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040094 HMU_PARTNER_TYPE = 2,
95
96 /* firmware returned values */
97 SRB_STATUS_SUCCESS = 0x01,
98 SRB_STATUS_ERROR = 0x04,
99 SRB_STATUS_BUSY = 0x05,
100 SRB_STATUS_INVALID_REQUEST = 0x06,
101 SRB_STATUS_SELECTION_TIMEOUT = 0x0A,
102 SRB_SEE_SENSE = 0x80,
103
104 /* task attribute */
105 TASK_ATTRIBUTE_SIMPLE = 0x0,
106 TASK_ATTRIBUTE_HEADOFQUEUE = 0x1,
107 TASK_ATTRIBUTE_ORDERED = 0x2,
108 TASK_ATTRIBUTE_ACA = 0x4,
109
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800110 SS_STS_NORMAL = 0x80000000,
111 SS_STS_DONE = 0x40000000,
112 SS_STS_HANDSHAKE = 0x20000000,
113
114 SS_HEAD_HANDSHAKE = 0x80,
115
Ed Lin69cb4872009-08-18 12:15:14 -0700116 SS_H2I_INT_RESET = 0x100,
117
Ed Lin9eb46d22009-09-28 22:58:33 -0800118 SS_I2H_REQUEST_RESET = 0x2000,
119
Ed Lin69cb4872009-08-18 12:15:14 -0700120 SS_MU_OPERATIONAL = 0x80000000,
121
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800122 STEX_CDB_LENGTH = 16,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400123 STATUS_VAR_LEN = 128,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400124
125 /* sg flags */
126 SG_CF_EOT = 0x80, /* end of table */
127 SG_CF_64B = 0x40, /* 64 bit item */
128 SG_CF_HOST = 0x20, /* sg in host memory */
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800129 MSG_DATA_DIR_ND = 0,
130 MSG_DATA_DIR_IN = 1,
131 MSG_DATA_DIR_OUT = 2,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400132
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400133 st_shasta = 0,
134 st_vsc = 1,
Ed Lin591a3a52009-03-31 17:30:31 -0800135 st_yosemite = 2,
136 st_seq = 3,
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800137 st_yel = 4,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400138
139 PASSTHRU_REQ_TYPE = 0x00000001,
140 PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800141 ST_INTERNAL_TIMEOUT = 180,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400142
Ed Linfb4f66b2006-09-27 19:23:41 +0800143 ST_TO_CMD = 0,
144 ST_FROM_CMD = 1,
145
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400146 /* vendor specific commands of Promise */
Ed Linfb4f66b2006-09-27 19:23:41 +0800147 MGT_CMD = 0xd8,
148 SINBAND_MGT_CMD = 0xd9,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400149 ARRAY_CMD = 0xe0,
150 CONTROLLER_CMD = 0xe1,
151 DEBUGGING_CMD = 0xe2,
152 PASSTHRU_CMD = 0xe3,
153
154 PASSTHRU_GET_ADAPTER = 0x05,
155 PASSTHRU_GET_DRVVER = 0x10,
Ed Linfb4f66b2006-09-27 19:23:41 +0800156
157 CTLR_CONFIG_CMD = 0x03,
158 CTLR_SHUTDOWN = 0x0d,
159
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400160 CTLR_POWER_STATE_CHANGE = 0x0e,
161 CTLR_POWER_SAVING = 0x01,
162
163 PASSTHRU_SIGNATURE = 0x4e415041,
Ed Linfb4f66b2006-09-27 19:23:41 +0800164 MGT_CMD_SIGNATURE = 0xba,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400165
166 INQUIRY_EVPD = 0x01,
Ed Lin94e91082006-12-04 17:49:39 -0800167
168 ST_ADDITIONAL_MEM = 0x200000,
Ed Lincbacfb52009-09-28 22:58:17 -0800169 ST_ADDITIONAL_MEM_MIN = 0x80000,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400170};
171
172struct st_sgitem {
173 u8 ctrl; /* SG_CF_xxx */
174 u8 reserved[3];
175 __le32 count;
Ed Linf1498162009-03-31 17:30:19 -0800176 __le64 addr;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400177};
178
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800179struct st_ss_sgitem {
180 __le32 addr;
181 __le32 addr_hi;
182 __le32 count;
183};
184
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400185struct st_sgtable {
186 __le16 sg_count;
187 __le16 max_sg_count;
188 __le32 sz_in_byte;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400189};
190
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800191struct st_msg_header {
192 __le64 handle;
193 u8 flag;
194 u8 channel;
195 __le16 timeout;
196 u32 reserved;
197};
198
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400199struct handshake_frame {
Ed Linf1498162009-03-31 17:30:19 -0800200 __le64 rb_phy; /* request payload queue physical address */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400201 __le16 req_sz; /* size of each request payload */
202 __le16 req_cnt; /* count of reqs the buffer can hold */
203 __le16 status_sz; /* size of each status payload */
204 __le16 status_cnt; /* count of status the buffer can hold */
Ed Linf1498162009-03-31 17:30:19 -0800205 __le64 hosttime; /* seconds from Jan 1, 1970 (GMT) */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400206 u8 partner_type; /* who sends this frame */
207 u8 reserved0[7];
208 __le32 partner_ver_major;
209 __le32 partner_ver_minor;
210 __le32 partner_ver_oem;
211 __le32 partner_ver_build;
Ed Lin94e91082006-12-04 17:49:39 -0800212 __le32 extra_offset; /* NEW */
213 __le32 extra_size; /* NEW */
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800214 __le32 scratch_size;
215 u32 reserved1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400216};
217
218struct req_msg {
219 __le16 tag;
220 u8 lun;
221 u8 target;
222 u8 task_attr;
223 u8 task_manage;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800224 u8 data_dir;
Ed Linf903d7b72006-09-27 19:23:33 +0800225 u8 payload_sz; /* payload size in 4-byte, not used */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400226 u8 cdb[STEX_CDB_LENGTH];
Ed Lin591a3a52009-03-31 17:30:31 -0800227 u32 variable[0];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400228};
229
230struct status_msg {
231 __le16 tag;
232 u8 lun;
233 u8 target;
234 u8 srb_status;
235 u8 scsi_status;
236 u8 reserved;
237 u8 payload_sz; /* payload size in 4-byte */
238 u8 variable[STATUS_VAR_LEN];
239};
240
241struct ver_info {
242 u32 major;
243 u32 minor;
244 u32 oem;
245 u32 build;
246 u32 reserved[2];
247};
248
249struct st_frame {
250 u32 base[6];
251 u32 rom_addr;
252
253 struct ver_info drv_ver;
254 struct ver_info bios_ver;
255
256 u32 bus;
257 u32 slot;
258 u32 irq_level;
259 u32 irq_vec;
260 u32 id;
261 u32 subid;
262
263 u32 dimm_size;
264 u8 dimm_type;
265 u8 reserved[3];
266
267 u32 channel;
268 u32 reserved1;
269};
270
271struct st_drvver {
272 u32 major;
273 u32 minor;
274 u32 oem;
275 u32 build;
276 u32 signature[2];
277 u8 console_id;
278 u8 host_no;
279 u8 reserved0[2];
280 u32 reserved[3];
281};
282
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400283struct st_ccb {
284 struct req_msg *req;
285 struct scsi_cmnd *cmd;
286
287 void *sense_buffer;
288 unsigned int sense_bufflen;
289 int sg_count;
290
291 u32 req_type;
292 u8 srb_status;
293 u8 scsi_status;
Ed Linf1498162009-03-31 17:30:19 -0800294 u8 reserved[2];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400295};
296
297struct st_hba {
298 void __iomem *mmio_base; /* iomapped PCI memory space */
299 void *dma_mem;
300 dma_addr_t dma_handle;
Ed Lin94e91082006-12-04 17:49:39 -0800301 size_t dma_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400302
303 struct Scsi_Host *host;
304 struct pci_dev *pdev;
305
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800306 struct req_msg * (*alloc_rq) (struct st_hba *);
307 int (*map_sg)(struct st_hba *, struct req_msg *, struct st_ccb *);
308 void (*send) (struct st_hba *, struct req_msg *, u16);
309
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400310 u32 req_head;
311 u32 req_tail;
312 u32 status_head;
313 u32 status_tail;
314
315 struct status_msg *status_buffer;
316 void *copy_buffer; /* temp buffer for driver-handled commands */
Ed Lin591a3a52009-03-31 17:30:31 -0800317 struct st_ccb *ccb;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400318 struct st_ccb *wait_ccb;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800319 __le32 *scratch;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400320
Ed Lin9eb46d22009-09-28 22:58:33 -0800321 char work_q_name[20];
322 struct workqueue_struct *work_q;
323 struct work_struct reset_work;
324 wait_queue_head_t reset_waitq;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400325 unsigned int mu_status;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400326 unsigned int cardtype;
Ed Lin99946f82009-03-31 17:30:25 -0800327 int msi_enabled;
328 int out_req_cnt;
Ed Lin591a3a52009-03-31 17:30:31 -0800329 u32 extra_offset;
330 u16 rq_count;
331 u16 rq_size;
332 u16 sts_count;
Charles1ec364e2016-02-22 20:02:02 +0800333 u8 supports_pm;
Ed Lin591a3a52009-03-31 17:30:31 -0800334};
335
336struct st_card_info {
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800337 struct req_msg * (*alloc_rq) (struct st_hba *);
338 int (*map_sg)(struct st_hba *, struct req_msg *, struct st_ccb *);
339 void (*send) (struct st_hba *, struct req_msg *, u16);
Ed Lin591a3a52009-03-31 17:30:31 -0800340 unsigned int max_id;
341 unsigned int max_lun;
342 unsigned int max_channel;
343 u16 rq_count;
344 u16 rq_size;
345 u16 sts_count;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400346};
347
Ed Lin99946f82009-03-31 17:30:25 -0800348static int msi;
349module_param(msi, int, 0);
350MODULE_PARM_DESC(msi, "Enable Message Signaled Interrupts(0=off, 1=on)");
351
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400352static const char console_inq_page[] =
353{
354 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
355 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
356 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
357 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
358 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
359 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
360 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
361 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
362};
363
364MODULE_AUTHOR("Ed Lin");
365MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
366MODULE_LICENSE("GPL");
367MODULE_VERSION(ST_DRIVER_VERSION);
368
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400369static struct status_msg *stex_get_status(struct st_hba *hba)
370{
Ed Linf1498162009-03-31 17:30:19 -0800371 struct status_msg *status = hba->status_buffer + hba->status_tail;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400372
373 ++hba->status_tail;
Ed Lin591a3a52009-03-31 17:30:31 -0800374 hba->status_tail %= hba->sts_count+1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400375
376 return status;
377}
378
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400379static void stex_invalid_field(struct scsi_cmnd *cmd,
380 void (*done)(struct scsi_cmnd *))
381{
FUJITA Tomonori11002fb2008-03-25 09:26:52 +0900382 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
383
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800384 /* "Invalid field in cdb" */
FUJITA Tomonori11002fb2008-03-25 09:26:52 +0900385 scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
386 0x0);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400387 done(cmd);
388}
389
390static struct req_msg *stex_alloc_req(struct st_hba *hba)
391{
Ed Lin591a3a52009-03-31 17:30:31 -0800392 struct req_msg *req = hba->dma_mem + hba->req_head * hba->rq_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400393
394 ++hba->req_head;
Ed Lin591a3a52009-03-31 17:30:31 -0800395 hba->req_head %= hba->rq_count+1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400396
397 return req;
398}
399
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800400static struct req_msg *stex_ss_alloc_req(struct st_hba *hba)
401{
402 return (struct req_msg *)(hba->dma_mem +
403 hba->req_head * hba->rq_size + sizeof(struct st_msg_header));
404}
405
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400406static int stex_map_sg(struct st_hba *hba,
407 struct req_msg *req, struct st_ccb *ccb)
408{
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400409 struct scsi_cmnd *cmd;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900410 struct scatterlist *sg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400411 struct st_sgtable *dst;
Ed Linf1498162009-03-31 17:30:19 -0800412 struct st_sgitem *table;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900413 int i, nseg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400414
415 cmd = ccb->cmd;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900416 nseg = scsi_dma_map(cmd);
Ed Linf1498162009-03-31 17:30:19 -0800417 BUG_ON(nseg < 0);
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900418 if (nseg) {
Ed Linf1498162009-03-31 17:30:19 -0800419 dst = (struct st_sgtable *)req->variable;
420
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900421 ccb->sg_count = nseg;
422 dst->sg_count = cpu_to_le16((u16)nseg);
Ed Linf1498162009-03-31 17:30:19 -0800423 dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
424 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400425
Ed Linf1498162009-03-31 17:30:19 -0800426 table = (struct st_sgitem *)(dst + 1);
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900427 scsi_for_each_sg(cmd, sg, nseg, i) {
Ed Linf1498162009-03-31 17:30:19 -0800428 table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
429 table[i].addr = cpu_to_le64(sg_dma_address(sg));
430 table[i].ctrl = SG_CF_64B | SG_CF_HOST;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400431 }
Ed Linf1498162009-03-31 17:30:19 -0800432 table[--i].ctrl |= SG_CF_EOT;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400433 }
434
Ed Linf1498162009-03-31 17:30:19 -0800435 return nseg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400436}
437
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800438static int stex_ss_map_sg(struct st_hba *hba,
439 struct req_msg *req, struct st_ccb *ccb)
440{
441 struct scsi_cmnd *cmd;
442 struct scatterlist *sg;
443 struct st_sgtable *dst;
444 struct st_ss_sgitem *table;
445 int i, nseg;
446
447 cmd = ccb->cmd;
448 nseg = scsi_dma_map(cmd);
449 BUG_ON(nseg < 0);
450 if (nseg) {
451 dst = (struct st_sgtable *)req->variable;
452
453 ccb->sg_count = nseg;
454 dst->sg_count = cpu_to_le16((u16)nseg);
455 dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
456 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
457
458 table = (struct st_ss_sgitem *)(dst + 1);
459 scsi_for_each_sg(cmd, sg, nseg, i) {
460 table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
461 table[i].addr =
462 cpu_to_le32(sg_dma_address(sg) & 0xffffffff);
463 table[i].addr_hi =
464 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
465 }
466 }
467
468 return nseg;
469}
470
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400471static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
472{
473 struct st_frame *p;
474 size_t count = sizeof(struct st_frame);
475
476 p = hba->copy_buffer;
Ed Linf1498162009-03-31 17:30:19 -0800477 scsi_sg_copy_to_buffer(ccb->cmd, p, count);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400478 memset(p->base, 0, sizeof(u32)*6);
479 *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
480 p->rom_addr = 0;
481
482 p->drv_ver.major = ST_VER_MAJOR;
483 p->drv_ver.minor = ST_VER_MINOR;
484 p->drv_ver.oem = ST_OEM;
485 p->drv_ver.build = ST_BUILD_VER;
486
487 p->bus = hba->pdev->bus->number;
488 p->slot = hba->pdev->devfn;
489 p->irq_level = 0;
490 p->irq_vec = hba->pdev->irq;
491 p->id = hba->pdev->vendor << 16 | hba->pdev->device;
492 p->subid =
493 hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
494
Ed Linf1498162009-03-31 17:30:19 -0800495 scsi_sg_copy_from_buffer(ccb->cmd, p, count);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400496}
497
498static void
499stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
500{
501 req->tag = cpu_to_le16(tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400502
503 hba->ccb[tag].req = req;
504 hba->out_req_cnt++;
505
506 writel(hba->req_head, hba->mmio_base + IMR0);
507 writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
508 readl(hba->mmio_base + IDBL); /* flush */
509}
510
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800511static void
512stex_ss_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
513{
514 struct scsi_cmnd *cmd;
515 struct st_msg_header *msg_h;
516 dma_addr_t addr;
517
518 req->tag = cpu_to_le16(tag);
519
520 hba->ccb[tag].req = req;
521 hba->out_req_cnt++;
522
523 cmd = hba->ccb[tag].cmd;
524 msg_h = (struct st_msg_header *)req - 1;
525 if (likely(cmd)) {
526 msg_h->channel = (u8)cmd->device->channel;
527 msg_h->timeout = cpu_to_le16(cmd->request->timeout/HZ);
528 }
529 addr = hba->dma_handle + hba->req_head * hba->rq_size;
530 addr += (hba->ccb[tag].sg_count+4)/11;
531 msg_h->handle = cpu_to_le64(addr);
532
533 ++hba->req_head;
534 hba->req_head %= hba->rq_count+1;
535
536 writel((addr >> 16) >> 16, hba->mmio_base + YH2I_REQ_HI);
537 readl(hba->mmio_base + YH2I_REQ_HI); /* flush */
538 writel(addr, hba->mmio_base + YH2I_REQ);
539 readl(hba->mmio_base + YH2I_REQ); /* flush */
540}
541
Charles45b42ad2016-02-22 20:04:25 +0800542static void return_abnormal_state(struct st_hba *hba, int status)
543{
544 struct st_ccb *ccb;
545 unsigned long flags;
546 u16 tag;
547
548 spin_lock_irqsave(hba->host->host_lock, flags);
549 for (tag = 0; tag < hba->host->can_queue; tag++) {
550 ccb = &hba->ccb[tag];
551 if (ccb->req == NULL)
552 continue;
553 ccb->req = NULL;
554 if (ccb->cmd) {
555 scsi_dma_unmap(ccb->cmd);
556 ccb->cmd->result = status << 16;
557 ccb->cmd->scsi_done(ccb->cmd);
558 ccb->cmd = NULL;
559 }
560 }
561 spin_unlock_irqrestore(hba->host->host_lock, flags);
562}
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400563static int
564stex_slave_config(struct scsi_device *sdev)
565{
566 sdev->use_10_for_rw = 1;
567 sdev->use_10_for_ms = 1;
James Bottomleydc5c49b2008-11-30 10:38:08 -0600568 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
Ed Lincf355882006-09-01 14:31:51 +0800569
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400570 return 0;
571}
572
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400573static int
Jeff Garzikf2812332010-11-16 02:10:29 -0500574stex_queuecommand_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400575{
576 struct st_hba *hba;
577 struct Scsi_Host *host;
Ed Linf1498162009-03-31 17:30:19 -0800578 unsigned int id, lun;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400579 struct req_msg *req;
580 u16 tag;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800581
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400582 host = cmd->device->host;
583 id = cmd->device->id;
Ed Line0b2e592007-05-09 20:50:33 -0800584 lun = cmd->device->lun;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400585 hba = (struct st_hba *) &host->hostdata[0];
Charles45b42ad2016-02-22 20:04:25 +0800586 if (hba->mu_status == MU_STATE_NOCONNECT) {
587 cmd->result = DID_NO_CONNECT;
588 done(cmd);
589 return 0;
590 }
591 if (unlikely(hba->mu_status != MU_STATE_STARTED))
Ed Lin9eb46d22009-09-28 22:58:33 -0800592 return SCSI_MLQUEUE_HOST_BUSY;
593
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400594 switch (cmd->cmnd[0]) {
595 case MODE_SENSE_10:
596 {
597 static char ms10_caching_page[12] =
598 { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
599 unsigned char page;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800600
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400601 page = cmd->cmnd[2] & 0x3f;
602 if (page == 0x8 || page == 0x3f) {
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900603 scsi_sg_copy_from_buffer(cmd, ms10_caching_page,
604 sizeof(ms10_caching_page));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400605 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
606 done(cmd);
607 } else
608 stex_invalid_field(cmd, done);
609 return 0;
610 }
Ed Line0b2e592007-05-09 20:50:33 -0800611 case REPORT_LUNS:
612 /*
613 * The shasta firmware does not report actual luns in the
614 * target, so fail the command to force sequential lun scan.
615 * Also, the console device does not support this command.
616 */
617 if (hba->cardtype == st_shasta || id == host->max_id - 1) {
618 stex_invalid_field(cmd, done);
619 return 0;
620 }
621 break;
Ed Lind116a7b2007-05-09 20:50:40 -0800622 case TEST_UNIT_READY:
623 if (id == host->max_id - 1) {
624 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
625 done(cmd);
626 return 0;
627 }
628 break;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400629 case INQUIRY:
Ed Lin91e6eca2009-12-18 17:34:51 -0800630 if (lun >= host->max_lun) {
631 cmd->result = DID_NO_CONNECT << 16;
632 done(cmd);
633 return 0;
634 }
Ed Line0b2e592007-05-09 20:50:33 -0800635 if (id != host->max_id - 1)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400636 break;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800637 if (!lun && !cmd->device->channel &&
638 (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900639 scsi_sg_copy_from_buffer(cmd, (void *)console_inq_page,
640 sizeof(console_inq_page));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400641 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
642 done(cmd);
643 } else
644 stex_invalid_field(cmd, done);
645 return 0;
646 case PASSTHRU_CMD:
647 if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
648 struct st_drvver ver;
FUJITA Tomonori26106e32008-02-22 23:11:03 +0900649 size_t cp_len = sizeof(ver);
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800650
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400651 ver.major = ST_VER_MAJOR;
652 ver.minor = ST_VER_MINOR;
653 ver.oem = ST_OEM;
654 ver.build = ST_BUILD_VER;
655 ver.signature[0] = PASSTHRU_SIGNATURE;
Ed Line0b2e592007-05-09 20:50:33 -0800656 ver.console_id = host->max_id - 1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400657 ver.host_no = hba->host->host_no;
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900658 cp_len = scsi_sg_copy_from_buffer(cmd, &ver, cp_len);
FUJITA Tomonori26106e32008-02-22 23:11:03 +0900659 cmd->result = sizeof(ver) == cp_len ?
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400660 DID_OK << 16 | COMMAND_COMPLETE << 8 :
661 DID_ERROR << 16 | COMMAND_COMPLETE << 8;
662 done(cmd);
663 return 0;
664 }
665 default:
666 break;
667 }
668
669 cmd->scsi_done = done;
670
Ed Lincf355882006-09-01 14:31:51 +0800671 tag = cmd->request->tag;
672
673 if (unlikely(tag >= host->can_queue))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400674 return SCSI_MLQUEUE_HOST_BUSY;
675
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800676 req = hba->alloc_rq(hba);
Ed Linfb4f66b2006-09-27 19:23:41 +0800677
Ed Line0b2e592007-05-09 20:50:33 -0800678 req->lun = lun;
679 req->target = id;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400680
681 /* cdb */
682 memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
683
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800684 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
685 req->data_dir = MSG_DATA_DIR_IN;
686 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
687 req->data_dir = MSG_DATA_DIR_OUT;
688 else
689 req->data_dir = MSG_DATA_DIR_ND;
690
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400691 hba->ccb[tag].cmd = cmd;
692 hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
693 hba->ccb[tag].sense_buffer = cmd->sense_buffer;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400694
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800695 if (!hba->map_sg(hba, req, &hba->ccb[tag])) {
696 hba->ccb[tag].sg_count = 0;
697 memset(&req->variable[0], 0, 8);
698 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400699
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800700 hba->send(hba, req, tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400701 return 0;
702}
703
Jeff Garzikf2812332010-11-16 02:10:29 -0500704static DEF_SCSI_QCMD(stex_queuecommand)
705
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400706static void stex_scsi_done(struct st_ccb *ccb)
707{
708 struct scsi_cmnd *cmd = ccb->cmd;
709 int result;
710
Ed Linf1498162009-03-31 17:30:19 -0800711 if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400712 result = ccb->scsi_status;
713 switch (ccb->scsi_status) {
714 case SAM_STAT_GOOD:
715 result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
716 break;
717 case SAM_STAT_CHECK_CONDITION:
718 result |= DRIVER_SENSE << 24;
719 break;
720 case SAM_STAT_BUSY:
721 result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
722 break;
723 default:
724 result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
725 break;
726 }
727 }
728 else if (ccb->srb_status & SRB_SEE_SENSE)
729 result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
730 else switch (ccb->srb_status) {
731 case SRB_STATUS_SELECTION_TIMEOUT:
732 result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
733 break;
734 case SRB_STATUS_BUSY:
735 result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
736 break;
737 case SRB_STATUS_INVALID_REQUEST:
738 case SRB_STATUS_ERROR:
739 default:
740 result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
741 break;
742 }
743
744 cmd->result = result;
745 cmd->scsi_done(cmd);
746}
747
748static void stex_copy_data(struct st_ccb *ccb,
749 struct status_msg *resp, unsigned int variable)
750{
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400751 if (resp->scsi_status != SAM_STAT_GOOD) {
752 if (ccb->sense_buffer != NULL)
753 memcpy(ccb->sense_buffer, resp->variable,
754 min(variable, ccb->sense_bufflen));
755 return;
756 }
757
758 if (ccb->cmd == NULL)
759 return;
Ed Linf1498162009-03-31 17:30:19 -0800760 scsi_sg_copy_from_buffer(ccb->cmd, resp->variable, variable);
Ed Linfb4f66b2006-09-27 19:23:41 +0800761}
762
Ed Linf1498162009-03-31 17:30:19 -0800763static void stex_check_cmd(struct st_hba *hba,
Ed Linfb4f66b2006-09-27 19:23:41 +0800764 struct st_ccb *ccb, struct status_msg *resp)
765{
Ed Linfb4f66b2006-09-27 19:23:41 +0800766 if (ccb->cmd->cmnd[0] == MGT_CMD &&
Ed Linf1498162009-03-31 17:30:19 -0800767 resp->scsi_status != SAM_STAT_CHECK_CONDITION)
Ed Lin968a5762007-07-05 12:09:06 -0700768 scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
769 le32_to_cpu(*(__le32 *)&resp->variable[0]));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400770}
771
772static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
773{
774 void __iomem *base = hba->mmio_base;
775 struct status_msg *resp;
776 struct st_ccb *ccb;
777 unsigned int size;
778 u16 tag;
779
Ed Linf1498162009-03-31 17:30:19 -0800780 if (unlikely(!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED)))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400781 return;
782
783 /* status payloads */
784 hba->status_head = readl(base + OMR1);
Ed Lin591a3a52009-03-31 17:30:31 -0800785 if (unlikely(hba->status_head > hba->sts_count)) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400786 printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
787 pci_name(hba->pdev));
788 return;
789 }
790
Ed Linfb4f66b2006-09-27 19:23:41 +0800791 /*
792 * it's not a valid status payload if:
793 * 1. there are no pending requests(e.g. during init stage)
794 * 2. there are some pending requests, but the controller is in
795 * reset status, and its type is not st_yosemite
796 * firmware of st_yosemite in reset status will return pending requests
797 * to driver, so we allow it to pass
798 */
799 if (unlikely(hba->out_req_cnt <= 0 ||
800 (hba->mu_status == MU_STATE_RESETTING &&
801 hba->cardtype != st_yosemite))) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400802 hba->status_tail = hba->status_head;
803 goto update_status;
804 }
805
806 while (hba->status_tail != hba->status_head) {
807 resp = stex_get_status(hba);
808 tag = le16_to_cpu(resp->tag);
Ed Lincf355882006-09-01 14:31:51 +0800809 if (unlikely(tag >= hba->host->can_queue)) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400810 printk(KERN_WARNING DRV_NAME
811 "(%s): invalid tag\n", pci_name(hba->pdev));
812 continue;
813 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400814
Ed Linf1498162009-03-31 17:30:19 -0800815 hba->out_req_cnt--;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400816 ccb = &hba->ccb[tag];
Ed Linf1498162009-03-31 17:30:19 -0800817 if (unlikely(hba->wait_ccb == ccb))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400818 hba->wait_ccb = NULL;
819 if (unlikely(ccb->req == NULL)) {
820 printk(KERN_WARNING DRV_NAME
821 "(%s): lagging req\n", pci_name(hba->pdev));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400822 continue;
823 }
824
825 size = resp->payload_sz * sizeof(u32); /* payload size */
826 if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
827 size > sizeof(*resp))) {
828 printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
829 pci_name(hba->pdev));
830 } else {
831 size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
832 if (size)
833 stex_copy_data(ccb, resp, size);
834 }
835
Ed Lin - PTUdd48ebf2009-01-26 02:40:11 -0800836 ccb->req = NULL;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400837 ccb->srb_status = resp->srb_status;
838 ccb->scsi_status = resp->scsi_status;
839
Ed Lincf355882006-09-01 14:31:51 +0800840 if (likely(ccb->cmd != NULL)) {
Ed Linfb4f66b2006-09-27 19:23:41 +0800841 if (hba->cardtype == st_yosemite)
Ed Linf1498162009-03-31 17:30:19 -0800842 stex_check_cmd(hba, ccb, resp);
Ed Linfb4f66b2006-09-27 19:23:41 +0800843
Ed Lincf355882006-09-01 14:31:51 +0800844 if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
845 ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
846 stex_controller_info(hba, ccb);
Ed Linfb4f66b2006-09-27 19:23:41 +0800847
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900848 scsi_dma_unmap(ccb->cmd);
Ed Lincf355882006-09-01 14:31:51 +0800849 stex_scsi_done(ccb);
Ed Linf1498162009-03-31 17:30:19 -0800850 } else
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400851 ccb->req_type = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400852 }
853
854update_status:
855 writel(hba->status_head, base + IMR1);
856 readl(base + IMR1); /* flush */
857}
858
David Howells7d12e782006-10-05 14:55:46 +0100859static irqreturn_t stex_intr(int irq, void *__hba)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400860{
861 struct st_hba *hba = __hba;
862 void __iomem *base = hba->mmio_base;
863 u32 data;
864 unsigned long flags;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400865
866 spin_lock_irqsave(hba->host->host_lock, flags);
867
868 data = readl(base + ODBL);
869
870 if (data && data != 0xffffffff) {
871 /* clear the interrupt */
872 writel(data, base + ODBL);
873 readl(base + ODBL); /* flush */
874 stex_mu_intr(hba, data);
Ed Lin9eb46d22009-09-28 22:58:33 -0800875 spin_unlock_irqrestore(hba->host->host_lock, flags);
876 if (unlikely(data & MU_OUTBOUND_DOORBELL_REQUEST_RESET &&
877 hba->cardtype == st_shasta))
878 queue_work(hba->work_q, &hba->reset_work);
879 return IRQ_HANDLED;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400880 }
881
882 spin_unlock_irqrestore(hba->host->host_lock, flags);
883
Ed Lin9eb46d22009-09-28 22:58:33 -0800884 return IRQ_NONE;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400885}
886
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800887static void stex_ss_mu_intr(struct st_hba *hba)
888{
889 struct status_msg *resp;
890 struct st_ccb *ccb;
891 __le32 *scratch;
892 unsigned int size;
893 int count = 0;
894 u32 value;
895 u16 tag;
896
897 if (unlikely(hba->out_req_cnt <= 0 ||
898 hba->mu_status == MU_STATE_RESETTING))
899 return;
900
901 while (count < hba->sts_count) {
902 scratch = hba->scratch + hba->status_tail;
903 value = le32_to_cpu(*scratch);
904 if (unlikely(!(value & SS_STS_NORMAL)))
905 return;
906
907 resp = hba->status_buffer + hba->status_tail;
908 *scratch = 0;
909 ++count;
910 ++hba->status_tail;
911 hba->status_tail %= hba->sts_count+1;
912
913 tag = (u16)value;
914 if (unlikely(tag >= hba->host->can_queue)) {
915 printk(KERN_WARNING DRV_NAME
Ed Lin69cb4872009-08-18 12:15:14 -0700916 "(%s): invalid tag\n", pci_name(hba->pdev));
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800917 continue;
918 }
919
920 hba->out_req_cnt--;
921 ccb = &hba->ccb[tag];
922 if (unlikely(hba->wait_ccb == ccb))
923 hba->wait_ccb = NULL;
924 if (unlikely(ccb->req == NULL)) {
925 printk(KERN_WARNING DRV_NAME
926 "(%s): lagging req\n", pci_name(hba->pdev));
927 continue;
928 }
929
930 ccb->req = NULL;
931 if (likely(value & SS_STS_DONE)) { /* normal case */
932 ccb->srb_status = SRB_STATUS_SUCCESS;
933 ccb->scsi_status = SAM_STAT_GOOD;
934 } else {
935 ccb->srb_status = resp->srb_status;
936 ccb->scsi_status = resp->scsi_status;
937 size = resp->payload_sz * sizeof(u32);
938 if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
939 size > sizeof(*resp))) {
940 printk(KERN_WARNING DRV_NAME
941 "(%s): bad status size\n",
942 pci_name(hba->pdev));
943 } else {
944 size -= sizeof(*resp) - STATUS_VAR_LEN;
945 if (size)
946 stex_copy_data(ccb, resp, size);
947 }
948 if (likely(ccb->cmd != NULL))
949 stex_check_cmd(hba, ccb, resp);
950 }
951
952 if (likely(ccb->cmd != NULL)) {
953 scsi_dma_unmap(ccb->cmd);
954 stex_scsi_done(ccb);
955 } else
956 ccb->req_type = 0;
957 }
958}
959
960static irqreturn_t stex_ss_intr(int irq, void *__hba)
961{
962 struct st_hba *hba = __hba;
963 void __iomem *base = hba->mmio_base;
964 u32 data;
965 unsigned long flags;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800966
967 spin_lock_irqsave(hba->host->host_lock, flags);
968
969 data = readl(base + YI2H_INT);
970 if (data && data != 0xffffffff) {
971 /* clear the interrupt */
972 writel(data, base + YI2H_INT_C);
973 stex_ss_mu_intr(hba);
Ed Lin9eb46d22009-09-28 22:58:33 -0800974 spin_unlock_irqrestore(hba->host->host_lock, flags);
975 if (unlikely(data & SS_I2H_REQUEST_RESET))
976 queue_work(hba->work_q, &hba->reset_work);
977 return IRQ_HANDLED;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800978 }
979
980 spin_unlock_irqrestore(hba->host->host_lock, flags);
981
Ed Lin9eb46d22009-09-28 22:58:33 -0800982 return IRQ_NONE;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800983}
984
985static int stex_common_handshake(struct st_hba *hba)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400986{
987 void __iomem *base = hba->mmio_base;
988 struct handshake_frame *h;
989 dma_addr_t status_phys;
Ed Lin529e7a62006-12-04 17:49:34 -0800990 u32 data;
Ed Lin76fbf962006-12-04 17:49:42 -0800991 unsigned long before;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400992
993 if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
994 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
995 readl(base + IDBL);
Ed Lin76fbf962006-12-04 17:49:42 -0800996 before = jiffies;
997 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
998 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
999 printk(KERN_ERR DRV_NAME
1000 "(%s): no handshake signature\n",
1001 pci_name(hba->pdev));
1002 return -1;
1003 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001004 rmb();
1005 msleep(1);
1006 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001007 }
1008
1009 udelay(10);
1010
Ed Lin529e7a62006-12-04 17:49:34 -08001011 data = readl(base + OMR1);
1012 if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
1013 data &= 0x0000ffff;
Ed Linf1498162009-03-31 17:30:19 -08001014 if (hba->host->can_queue > data) {
Ed Lin529e7a62006-12-04 17:49:34 -08001015 hba->host->can_queue = data;
Ed Linf1498162009-03-31 17:30:19 -08001016 hba->host->cmd_per_lun = data;
1017 }
Ed Lin529e7a62006-12-04 17:49:34 -08001018 }
1019
Ed Linf1498162009-03-31 17:30:19 -08001020 h = (struct handshake_frame *)hba->status_buffer;
1021 h->rb_phy = cpu_to_le64(hba->dma_handle);
Ed Lin591a3a52009-03-31 17:30:31 -08001022 h->req_sz = cpu_to_le16(hba->rq_size);
1023 h->req_cnt = cpu_to_le16(hba->rq_count+1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001024 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
Ed Lin591a3a52009-03-31 17:30:31 -08001025 h->status_cnt = cpu_to_le16(hba->sts_count+1);
Tina Ruchandani0da39682015-10-30 01:30:40 -07001026 h->hosttime = cpu_to_le64(ktime_get_real_seconds());
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001027 h->partner_type = HMU_PARTNER_TYPE;
Ed Lin591a3a52009-03-31 17:30:31 -08001028 if (hba->extra_offset) {
1029 h->extra_offset = cpu_to_le32(hba->extra_offset);
Ed Lincbacfb52009-09-28 22:58:17 -08001030 h->extra_size = cpu_to_le32(hba->dma_size - hba->extra_offset);
Ed Lin94e91082006-12-04 17:49:39 -08001031 } else
1032 h->extra_offset = h->extra_size = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001033
Ed Lin591a3a52009-03-31 17:30:31 -08001034 status_phys = hba->dma_handle + (hba->rq_count+1) * hba->rq_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001035 writel(status_phys, base + IMR0);
1036 readl(base + IMR0);
1037 writel((status_phys >> 16) >> 16, base + IMR1);
1038 readl(base + IMR1);
1039
1040 writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
1041 readl(base + OMR0);
1042 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
1043 readl(base + IDBL); /* flush */
1044
1045 udelay(10);
Ed Lin76fbf962006-12-04 17:49:42 -08001046 before = jiffies;
1047 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
1048 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
1049 printk(KERN_ERR DRV_NAME
1050 "(%s): no signature after handshake frame\n",
1051 pci_name(hba->pdev));
1052 return -1;
1053 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001054 rmb();
1055 msleep(1);
1056 }
1057
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001058 writel(0, base + IMR0);
1059 readl(base + IMR0);
1060 writel(0, base + OMR0);
1061 readl(base + OMR0);
1062 writel(0, base + IMR1);
1063 readl(base + IMR1);
1064 writel(0, base + OMR1);
1065 readl(base + OMR1); /* flush */
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001066 return 0;
1067}
1068
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001069static int stex_ss_handshake(struct st_hba *hba)
1070{
1071 void __iomem *base = hba->mmio_base;
1072 struct st_msg_header *msg_h;
1073 struct handshake_frame *h;
Ed Lin69cb4872009-08-18 12:15:14 -07001074 __le32 *scratch;
Ed Lin9eb46d22009-09-28 22:58:33 -08001075 u32 data, scratch_size;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001076 unsigned long before;
1077 int ret = 0;
1078
Ed Lin69cb4872009-08-18 12:15:14 -07001079 before = jiffies;
1080 while ((readl(base + YIOA_STATUS) & SS_MU_OPERATIONAL) == 0) {
1081 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
1082 printk(KERN_ERR DRV_NAME
1083 "(%s): firmware not operational\n",
1084 pci_name(hba->pdev));
1085 return -1;
1086 }
1087 msleep(1);
1088 }
1089
1090 msg_h = (struct st_msg_header *)hba->dma_mem;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001091 msg_h->handle = cpu_to_le64(hba->dma_handle);
1092 msg_h->flag = SS_HEAD_HANDSHAKE;
1093
Ed Lin69cb4872009-08-18 12:15:14 -07001094 h = (struct handshake_frame *)(msg_h + 1);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001095 h->rb_phy = cpu_to_le64(hba->dma_handle);
1096 h->req_sz = cpu_to_le16(hba->rq_size);
1097 h->req_cnt = cpu_to_le16(hba->rq_count+1);
1098 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
1099 h->status_cnt = cpu_to_le16(hba->sts_count+1);
Tina Ruchandani0da39682015-10-30 01:30:40 -07001100 h->hosttime = cpu_to_le64(ktime_get_real_seconds());
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001101 h->partner_type = HMU_PARTNER_TYPE;
1102 h->extra_offset = h->extra_size = 0;
Ed Lin9eb46d22009-09-28 22:58:33 -08001103 scratch_size = (hba->sts_count+1)*sizeof(u32);
1104 h->scratch_size = cpu_to_le32(scratch_size);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001105
1106 data = readl(base + YINT_EN);
1107 data &= ~4;
1108 writel(data, base + YINT_EN);
1109 writel((hba->dma_handle >> 16) >> 16, base + YH2I_REQ_HI);
Ed Lin9eb46d22009-09-28 22:58:33 -08001110 readl(base + YH2I_REQ_HI);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001111 writel(hba->dma_handle, base + YH2I_REQ);
Ed Lin9eb46d22009-09-28 22:58:33 -08001112 readl(base + YH2I_REQ); /* flush */
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001113
1114 scratch = hba->scratch;
1115 before = jiffies;
1116 while (!(le32_to_cpu(*scratch) & SS_STS_HANDSHAKE)) {
1117 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
1118 printk(KERN_ERR DRV_NAME
1119 "(%s): no signature after handshake frame\n",
1120 pci_name(hba->pdev));
1121 ret = -1;
1122 break;
1123 }
1124 rmb();
1125 msleep(1);
1126 }
1127
Ed Lin9eb46d22009-09-28 22:58:33 -08001128 memset(scratch, 0, scratch_size);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001129 msg_h->flag = 0;
1130 return ret;
1131}
1132
1133static int stex_handshake(struct st_hba *hba)
1134{
1135 int err;
1136 unsigned long flags;
Ed Lin9eb46d22009-09-28 22:58:33 -08001137 unsigned int mu_status;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001138
1139 err = (hba->cardtype == st_yel) ?
1140 stex_ss_handshake(hba) : stex_common_handshake(hba);
Ed Lin9eb46d22009-09-28 22:58:33 -08001141 spin_lock_irqsave(hba->host->host_lock, flags);
1142 mu_status = hba->mu_status;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001143 if (err == 0) {
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001144 hba->req_head = 0;
1145 hba->req_tail = 0;
1146 hba->status_head = 0;
1147 hba->status_tail = 0;
1148 hba->out_req_cnt = 0;
1149 hba->mu_status = MU_STATE_STARTED;
Ed Lin9eb46d22009-09-28 22:58:33 -08001150 } else
1151 hba->mu_status = MU_STATE_FAILED;
1152 if (mu_status == MU_STATE_RESETTING)
1153 wake_up_all(&hba->reset_waitq);
1154 spin_unlock_irqrestore(hba->host->host_lock, flags);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001155 return err;
1156}
1157
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001158static int stex_abort(struct scsi_cmnd *cmd)
1159{
1160 struct Scsi_Host *host = cmd->device->host;
1161 struct st_hba *hba = (struct st_hba *)host->hostdata;
Ed Lincf355882006-09-01 14:31:51 +08001162 u16 tag = cmd->request->tag;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001163 void __iomem *base;
1164 u32 data;
1165 int result = SUCCESS;
1166 unsigned long flags;
Ed Linc25da0a2007-05-09 20:50:42 -08001167
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02001168 scmd_printk(KERN_INFO, cmd, "aborting command\n");
Ed Linc25da0a2007-05-09 20:50:42 -08001169
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001170 base = hba->mmio_base;
1171 spin_lock_irqsave(host->host_lock, flags);
Ed Lin9eb46d22009-09-28 22:58:33 -08001172 if (tag < host->can_queue &&
1173 hba->ccb[tag].req && hba->ccb[tag].cmd == cmd)
Ed Lincf355882006-09-01 14:31:51 +08001174 hba->wait_ccb = &hba->ccb[tag];
Ed Lin9eb46d22009-09-28 22:58:33 -08001175 else
1176 goto out;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001177
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001178 if (hba->cardtype == st_yel) {
1179 data = readl(base + YI2H_INT);
1180 if (data == 0 || data == 0xffffffff)
1181 goto fail_out;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001182
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001183 writel(data, base + YI2H_INT_C);
1184 stex_ss_mu_intr(hba);
1185 } else {
1186 data = readl(base + ODBL);
1187 if (data == 0 || data == 0xffffffff)
1188 goto fail_out;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001189
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001190 writel(data, base + ODBL);
1191 readl(base + ODBL); /* flush */
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001192
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001193 stex_mu_intr(hba, data);
1194 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001195 if (hba->wait_ccb == NULL) {
1196 printk(KERN_WARNING DRV_NAME
1197 "(%s): lost interrupt\n", pci_name(hba->pdev));
1198 goto out;
1199 }
1200
1201fail_out:
FUJITA Tomonorid5587d52007-05-26 10:01:24 +09001202 scsi_dma_unmap(cmd);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001203 hba->wait_ccb->req = NULL; /* nullify the req's future return */
1204 hba->wait_ccb = NULL;
1205 result = FAILED;
1206out:
1207 spin_unlock_irqrestore(host->host_lock, flags);
1208 return result;
1209}
1210
1211static void stex_hard_reset(struct st_hba *hba)
1212{
1213 struct pci_bus *bus;
1214 int i;
1215 u16 pci_cmd;
1216 u8 pci_bctl;
1217
1218 for (i = 0; i < 16; i++)
1219 pci_read_config_dword(hba->pdev, i * 4,
1220 &hba->pdev->saved_config_space[i]);
1221
1222 /* Reset secondary bus. Our controller(MU/ATU) is the only device on
1223 secondary bus. Consult Intel 80331/3 developer's manual for detail */
1224 bus = hba->pdev->bus;
1225 pci_read_config_byte(bus->self, PCI_BRIDGE_CONTROL, &pci_bctl);
1226 pci_bctl |= PCI_BRIDGE_CTL_BUS_RESET;
1227 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
Ed Lin69f4a512007-05-09 20:50:37 -08001228
1229 /*
1230 * 1 ms may be enough for 8-port controllers. But 16-port controllers
1231 * require more time to finish bus reset. Use 100 ms here for safety
1232 */
1233 msleep(100);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001234 pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
1235 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
1236
Ed Lin76fbf962006-12-04 17:49:42 -08001237 for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001238 pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
Ed Lin47c4f992006-12-04 17:49:31 -08001239 if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001240 break;
1241 msleep(1);
1242 }
1243
1244 ssleep(5);
1245 for (i = 0; i < 16; i++)
1246 pci_write_config_dword(hba->pdev, i * 4,
1247 hba->pdev->saved_config_space[i]);
1248}
1249
Ed Lin9eb46d22009-09-28 22:58:33 -08001250static int stex_yos_reset(struct st_hba *hba)
1251{
1252 void __iomem *base;
1253 unsigned long flags, before;
1254 int ret = 0;
1255
1256 base = hba->mmio_base;
1257 writel(MU_INBOUND_DOORBELL_RESET, base + IDBL);
1258 readl(base + IDBL); /* flush */
1259 before = jiffies;
1260 while (hba->out_req_cnt > 0) {
1261 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1262 printk(KERN_WARNING DRV_NAME
1263 "(%s): reset timeout\n", pci_name(hba->pdev));
1264 ret = -1;
1265 break;
1266 }
1267 msleep(1);
1268 }
1269
1270 spin_lock_irqsave(hba->host->host_lock, flags);
1271 if (ret == -1)
1272 hba->mu_status = MU_STATE_FAILED;
1273 else
1274 hba->mu_status = MU_STATE_STARTED;
1275 wake_up_all(&hba->reset_waitq);
1276 spin_unlock_irqrestore(hba->host->host_lock, flags);
1277
1278 return ret;
1279}
1280
Ed Lin69cb4872009-08-18 12:15:14 -07001281static void stex_ss_reset(struct st_hba *hba)
1282{
1283 writel(SS_H2I_INT_RESET, hba->mmio_base + YH2I_INT);
1284 readl(hba->mmio_base + YH2I_INT);
1285 ssleep(5);
1286}
1287
Ed Lin9eb46d22009-09-28 22:58:33 -08001288static int stex_do_reset(struct st_hba *hba)
1289{
Ed Lin9eb46d22009-09-28 22:58:33 -08001290 unsigned long flags;
1291 unsigned int mu_status = MU_STATE_RESETTING;
Ed Lin9eb46d22009-09-28 22:58:33 -08001292
1293 spin_lock_irqsave(hba->host->host_lock, flags);
1294 if (hba->mu_status == MU_STATE_STARTING) {
1295 spin_unlock_irqrestore(hba->host->host_lock, flags);
1296 printk(KERN_INFO DRV_NAME "(%s): request reset during init\n",
1297 pci_name(hba->pdev));
1298 return 0;
1299 }
1300 while (hba->mu_status == MU_STATE_RESETTING) {
1301 spin_unlock_irqrestore(hba->host->host_lock, flags);
1302 wait_event_timeout(hba->reset_waitq,
1303 hba->mu_status != MU_STATE_RESETTING,
1304 MU_MAX_DELAY * HZ);
1305 spin_lock_irqsave(hba->host->host_lock, flags);
1306 mu_status = hba->mu_status;
1307 }
1308
1309 if (mu_status != MU_STATE_RESETTING) {
1310 spin_unlock_irqrestore(hba->host->host_lock, flags);
1311 return (mu_status == MU_STATE_STARTED) ? 0 : -1;
1312 }
1313
1314 hba->mu_status = MU_STATE_RESETTING;
1315 spin_unlock_irqrestore(hba->host->host_lock, flags);
1316
1317 if (hba->cardtype == st_yosemite)
1318 return stex_yos_reset(hba);
1319
1320 if (hba->cardtype == st_shasta)
1321 stex_hard_reset(hba);
1322 else if (hba->cardtype == st_yel)
1323 stex_ss_reset(hba);
1324
Charles45b42ad2016-02-22 20:04:25 +08001325
1326 return_abnormal_state(hba, DID_RESET);
Ed Lin9eb46d22009-09-28 22:58:33 -08001327
1328 if (stex_handshake(hba) == 0)
1329 return 0;
1330
1331 printk(KERN_WARNING DRV_NAME "(%s): resetting: handshake failed\n",
1332 pci_name(hba->pdev));
1333 return -1;
1334}
1335
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001336static int stex_reset(struct scsi_cmnd *cmd)
1337{
1338 struct st_hba *hba;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -08001339
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001340 hba = (struct st_hba *) &cmd->device->host->hostdata[0];
1341
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02001342 shost_printk(KERN_INFO, cmd->device->host,
1343 "resetting host\n");
Ed Linc25da0a2007-05-09 20:50:42 -08001344
Ed Lin9eb46d22009-09-28 22:58:33 -08001345 return stex_do_reset(hba) ? FAILED : SUCCESS;
1346}
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001347
Ed Lin9eb46d22009-09-28 22:58:33 -08001348static void stex_reset_work(struct work_struct *work)
1349{
1350 struct st_hba *hba = container_of(work, struct st_hba, reset_work);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001351
Ed Lin9eb46d22009-09-28 22:58:33 -08001352 stex_do_reset(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001353}
1354
1355static int stex_biosparam(struct scsi_device *sdev,
1356 struct block_device *bdev, sector_t capacity, int geom[])
1357{
Ed Linb4b8bed2006-12-04 17:49:24 -08001358 int heads = 255, sectors = 63;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001359
1360 if (capacity < 0x200000) {
1361 heads = 64;
1362 sectors = 32;
1363 }
1364
Ed Linb4b8bed2006-12-04 17:49:24 -08001365 sector_div(capacity, heads * sectors);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001366
1367 geom[0] = heads;
1368 geom[1] = sectors;
Ed Linb4b8bed2006-12-04 17:49:24 -08001369 geom[2] = capacity;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001370
1371 return 0;
1372}
1373
1374static struct scsi_host_template driver_template = {
1375 .module = THIS_MODULE,
1376 .name = DRV_NAME,
1377 .proc_name = DRV_NAME,
1378 .bios_param = stex_biosparam,
1379 .queuecommand = stex_queuecommand,
1380 .slave_configure = stex_slave_config,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001381 .eh_abort_handler = stex_abort,
1382 .eh_host_reset_handler = stex_reset,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001383 .this_id = -1,
Ed Lin591a3a52009-03-31 17:30:31 -08001384};
1385
1386static struct pci_device_id stex_pci_tbl[] = {
1387 /* st_shasta */
1388 { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1389 st_shasta }, /* SuperTrak EX8350/8300/16350/16300 */
1390 { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1391 st_shasta }, /* SuperTrak EX12350 */
1392 { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1393 st_shasta }, /* SuperTrak EX4350 */
1394 { 0x105a, 0xe350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1395 st_shasta }, /* SuperTrak EX24350 */
1396
1397 /* st_vsc */
1398 { 0x105a, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
1399
1400 /* st_yosemite */
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001401 { 0x105a, 0x8650, 0x105a, PCI_ANY_ID, 0, 0, st_yosemite },
Ed Lin591a3a52009-03-31 17:30:31 -08001402
1403 /* st_seq */
1404 { 0x105a, 0x3360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_seq },
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001405
1406 /* st_yel */
1407 { 0x105a, 0x8650, 0x1033, PCI_ANY_ID, 0, 0, st_yel },
1408 { 0x105a, 0x8760, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_yel },
Ed Lin591a3a52009-03-31 17:30:31 -08001409 { } /* terminate list */
1410};
1411
1412static struct st_card_info stex_card_info[] = {
1413 /* st_shasta */
1414 {
1415 .max_id = 17,
1416 .max_lun = 8,
1417 .max_channel = 0,
1418 .rq_count = 32,
1419 .rq_size = 1048,
1420 .sts_count = 32,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001421 .alloc_rq = stex_alloc_req,
1422 .map_sg = stex_map_sg,
1423 .send = stex_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001424 },
1425
1426 /* st_vsc */
1427 {
1428 .max_id = 129,
1429 .max_lun = 1,
1430 .max_channel = 0,
1431 .rq_count = 32,
1432 .rq_size = 1048,
1433 .sts_count = 32,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001434 .alloc_rq = stex_alloc_req,
1435 .map_sg = stex_map_sg,
1436 .send = stex_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001437 },
1438
1439 /* st_yosemite */
1440 {
1441 .max_id = 2,
1442 .max_lun = 256,
1443 .max_channel = 0,
1444 .rq_count = 256,
1445 .rq_size = 1048,
1446 .sts_count = 256,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001447 .alloc_rq = stex_alloc_req,
1448 .map_sg = stex_map_sg,
1449 .send = stex_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001450 },
1451
1452 /* st_seq */
1453 {
1454 .max_id = 129,
1455 .max_lun = 1,
1456 .max_channel = 0,
1457 .rq_count = 32,
1458 .rq_size = 1048,
1459 .sts_count = 32,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001460 .alloc_rq = stex_alloc_req,
1461 .map_sg = stex_map_sg,
1462 .send = stex_send_cmd,
1463 },
1464
1465 /* st_yel */
1466 {
1467 .max_id = 129,
1468 .max_lun = 256,
1469 .max_channel = 3,
1470 .rq_count = 801,
1471 .rq_size = 512,
1472 .sts_count = 801,
1473 .alloc_rq = stex_ss_alloc_req,
1474 .map_sg = stex_ss_map_sg,
1475 .send = stex_ss_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001476 },
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001477};
1478
1479static int stex_set_dma_mask(struct pci_dev * pdev)
1480{
1481 int ret;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -08001482
Ed Lincce9c8a2009-09-28 22:58:36 -08001483 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
1484 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001485 return 0;
Yang Hongyang284901a2009-04-06 19:01:15 -07001486 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001487 if (!ret)
Yang Hongyang284901a2009-04-06 19:01:15 -07001488 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001489 return ret;
1490}
1491
Ed Lin99946f82009-03-31 17:30:25 -08001492static int stex_request_irq(struct st_hba *hba)
1493{
1494 struct pci_dev *pdev = hba->pdev;
1495 int status;
1496
1497 if (msi) {
1498 status = pci_enable_msi(pdev);
1499 if (status != 0)
1500 printk(KERN_ERR DRV_NAME
1501 "(%s): error %d setting up MSI\n",
1502 pci_name(pdev), status);
1503 else
1504 hba->msi_enabled = 1;
1505 } else
1506 hba->msi_enabled = 0;
1507
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001508 status = request_irq(pdev->irq, hba->cardtype == st_yel ?
1509 stex_ss_intr : stex_intr, IRQF_SHARED, DRV_NAME, hba);
Ed Lin99946f82009-03-31 17:30:25 -08001510
1511 if (status != 0) {
1512 if (hba->msi_enabled)
1513 pci_disable_msi(pdev);
1514 }
1515 return status;
1516}
1517
1518static void stex_free_irq(struct st_hba *hba)
1519{
1520 struct pci_dev *pdev = hba->pdev;
1521
1522 free_irq(pdev->irq, hba);
1523 if (hba->msi_enabled)
1524 pci_disable_msi(pdev);
1525}
1526
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001527static int stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001528{
1529 struct st_hba *hba;
1530 struct Scsi_Host *host;
Ed Lin591a3a52009-03-31 17:30:31 -08001531 const struct st_card_info *ci = NULL;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001532 u32 sts_offset, cp_offset, scratch_offset;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001533 int err;
1534
1535 err = pci_enable_device(pdev);
1536 if (err)
1537 return err;
1538
1539 pci_set_master(pdev);
1540
1541 host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
1542
1543 if (!host) {
1544 printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
1545 pci_name(pdev));
1546 err = -ENOMEM;
1547 goto out_disable;
1548 }
1549
1550 hba = (struct st_hba *)host->hostdata;
1551 memset(hba, 0, sizeof(struct st_hba));
1552
1553 err = pci_request_regions(pdev, DRV_NAME);
1554 if (err < 0) {
1555 printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
1556 pci_name(pdev));
1557 goto out_scsi_host_put;
1558 }
1559
Arjan van de Ven25729a72008-09-28 16:18:02 -07001560 hba->mmio_base = pci_ioremap_bar(pdev, 0);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001561 if ( !hba->mmio_base) {
1562 printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
1563 pci_name(pdev));
1564 err = -ENOMEM;
1565 goto out_release_regions;
1566 }
1567
1568 err = stex_set_dma_mask(pdev);
1569 if (err) {
1570 printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
1571 pci_name(pdev));
1572 goto out_iounmap;
1573 }
1574
Ed Lin94e91082006-12-04 17:49:39 -08001575 hba->cardtype = (unsigned int) id->driver_data;
Ed Lin591a3a52009-03-31 17:30:31 -08001576 ci = &stex_card_info[hba->cardtype];
Charles1ec364e2016-02-22 20:02:02 +08001577 switch (id->subdevice) {
1578 case 0x4221:
1579 case 0x4222:
1580 case 0x4223:
1581 case 0x4224:
1582 case 0x4225:
1583 case 0x4226:
1584 case 0x4227:
1585 case 0x4261:
1586 case 0x4262:
1587 case 0x4263:
1588 case 0x4264:
1589 case 0x4265:
1590 break;
1591 default:
1592 if (hba->cardtype == st_yel)
1593 hba->supports_pm = 1;
1594 }
1595
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001596 sts_offset = scratch_offset = (ci->rq_count+1) * ci->rq_size;
1597 if (hba->cardtype == st_yel)
1598 sts_offset += (ci->sts_count+1) * sizeof(u32);
Ed Lin591a3a52009-03-31 17:30:31 -08001599 cp_offset = sts_offset + (ci->sts_count+1) * sizeof(struct status_msg);
1600 hba->dma_size = cp_offset + sizeof(struct st_frame);
1601 if (hba->cardtype == st_seq ||
1602 (hba->cardtype == st_vsc && (pdev->subsystem_device & 1))) {
1603 hba->extra_offset = hba->dma_size;
1604 hba->dma_size += ST_ADDITIONAL_MEM;
1605 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001606 hba->dma_mem = dma_alloc_coherent(&pdev->dev,
Ed Lin94e91082006-12-04 17:49:39 -08001607 hba->dma_size, &hba->dma_handle, GFP_KERNEL);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001608 if (!hba->dma_mem) {
Ed Lincbacfb52009-09-28 22:58:17 -08001609 /* Retry minimum coherent mapping for st_seq and st_vsc */
1610 if (hba->cardtype == st_seq ||
1611 (hba->cardtype == st_vsc && (pdev->subsystem_device & 1))) {
1612 printk(KERN_WARNING DRV_NAME
1613 "(%s): allocating min buffer for controller\n",
1614 pci_name(pdev));
1615 hba->dma_size = hba->extra_offset
1616 + ST_ADDITIONAL_MEM_MIN;
1617 hba->dma_mem = dma_alloc_coherent(&pdev->dev,
1618 hba->dma_size, &hba->dma_handle, GFP_KERNEL);
1619 }
1620
1621 if (!hba->dma_mem) {
1622 err = -ENOMEM;
1623 printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
1624 pci_name(pdev));
1625 goto out_iounmap;
1626 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001627 }
1628
Ed Lin591a3a52009-03-31 17:30:31 -08001629 hba->ccb = kcalloc(ci->rq_count, sizeof(struct st_ccb), GFP_KERNEL);
1630 if (!hba->ccb) {
1631 err = -ENOMEM;
1632 printk(KERN_ERR DRV_NAME "(%s): ccb alloc failed\n",
1633 pci_name(pdev));
1634 goto out_pci_free;
1635 }
1636
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001637 if (hba->cardtype == st_yel)
1638 hba->scratch = (__le32 *)(hba->dma_mem + scratch_offset);
Ed Lin591a3a52009-03-31 17:30:31 -08001639 hba->status_buffer = (struct status_msg *)(hba->dma_mem + sts_offset);
1640 hba->copy_buffer = hba->dma_mem + cp_offset;
1641 hba->rq_count = ci->rq_count;
1642 hba->rq_size = ci->rq_size;
1643 hba->sts_count = ci->sts_count;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001644 hba->alloc_rq = ci->alloc_rq;
1645 hba->map_sg = ci->map_sg;
1646 hba->send = ci->send;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001647 hba->mu_status = MU_STATE_STARTING;
1648
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001649 if (hba->cardtype == st_yel)
1650 host->sg_tablesize = 38;
1651 else
1652 host->sg_tablesize = 32;
Ed Lin591a3a52009-03-31 17:30:31 -08001653 host->can_queue = ci->rq_count;
1654 host->cmd_per_lun = ci->rq_count;
1655 host->max_id = ci->max_id;
1656 host->max_lun = ci->max_lun;
1657 host->max_channel = ci->max_channel;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001658 host->unique_id = host->host_no;
1659 host->max_cmd_len = STEX_CDB_LENGTH;
1660
1661 hba->host = host;
1662 hba->pdev = pdev;
Ed Lin9eb46d22009-09-28 22:58:33 -08001663 init_waitqueue_head(&hba->reset_waitq);
1664
1665 snprintf(hba->work_q_name, sizeof(hba->work_q_name),
1666 "stex_wq_%d", host->host_no);
1667 hba->work_q = create_singlethread_workqueue(hba->work_q_name);
1668 if (!hba->work_q) {
1669 printk(KERN_ERR DRV_NAME "(%s): create workqueue failed\n",
1670 pci_name(pdev));
1671 err = -ENOMEM;
1672 goto out_ccb_free;
1673 }
1674 INIT_WORK(&hba->reset_work, stex_reset_work);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001675
Ed Lin99946f82009-03-31 17:30:25 -08001676 err = stex_request_irq(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001677 if (err) {
1678 printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
1679 pci_name(pdev));
Ed Lin9eb46d22009-09-28 22:58:33 -08001680 goto out_free_wq;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001681 }
1682
1683 err = stex_handshake(hba);
1684 if (err)
1685 goto out_free_irq;
1686
1687 pci_set_drvdata(pdev, hba);
1688
1689 err = scsi_add_host(host, &pdev->dev);
1690 if (err) {
1691 printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
1692 pci_name(pdev));
1693 goto out_free_irq;
1694 }
1695
1696 scsi_scan_host(host);
1697
1698 return 0;
1699
1700out_free_irq:
Ed Lin99946f82009-03-31 17:30:25 -08001701 stex_free_irq(hba);
Ed Lin9eb46d22009-09-28 22:58:33 -08001702out_free_wq:
1703 destroy_workqueue(hba->work_q);
Ed Lin591a3a52009-03-31 17:30:31 -08001704out_ccb_free:
1705 kfree(hba->ccb);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001706out_pci_free:
Ed Lin94e91082006-12-04 17:49:39 -08001707 dma_free_coherent(&pdev->dev, hba->dma_size,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001708 hba->dma_mem, hba->dma_handle);
1709out_iounmap:
1710 iounmap(hba->mmio_base);
1711out_release_regions:
1712 pci_release_regions(pdev);
1713out_scsi_host_put:
1714 scsi_host_put(host);
1715out_disable:
1716 pci_disable_device(pdev);
1717
1718 return err;
1719}
1720
1721static void stex_hba_stop(struct st_hba *hba)
1722{
1723 struct req_msg *req;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001724 struct st_msg_header *msg_h;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001725 unsigned long flags;
1726 unsigned long before;
Ed Lincf355882006-09-01 14:31:51 +08001727 u16 tag = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001728
1729 spin_lock_irqsave(hba->host->host_lock, flags);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001730 req = hba->alloc_rq(hba);
1731 if (hba->cardtype == st_yel) {
1732 msg_h = (struct st_msg_header *)req - 1;
1733 memset(msg_h, 0, hba->rq_size);
1734 } else
1735 memset(req, 0, hba->rq_size);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001736
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001737 if (hba->cardtype == st_yosemite || hba->cardtype == st_yel) {
Ed Linfb4f66b2006-09-27 19:23:41 +08001738 req->cdb[0] = MGT_CMD;
1739 req->cdb[1] = MGT_CMD_SIGNATURE;
1740 req->cdb[2] = CTLR_CONFIG_CMD;
1741 req->cdb[3] = CTLR_SHUTDOWN;
1742 } else {
1743 req->cdb[0] = CONTROLLER_CMD;
1744 req->cdb[1] = CTLR_POWER_STATE_CHANGE;
1745 req->cdb[2] = CTLR_POWER_SAVING;
1746 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001747
1748 hba->ccb[tag].cmd = NULL;
1749 hba->ccb[tag].sg_count = 0;
1750 hba->ccb[tag].sense_bufflen = 0;
1751 hba->ccb[tag].sense_buffer = NULL;
Ed Linf1498162009-03-31 17:30:19 -08001752 hba->ccb[tag].req_type = PASSTHRU_REQ_TYPE;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001753
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001754 hba->send(hba, req, tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001755 spin_unlock_irqrestore(hba->host->host_lock, flags);
1756
Ed Lincf355882006-09-01 14:31:51 +08001757 before = jiffies;
1758 while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
Ed Linf1498162009-03-31 17:30:19 -08001759 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1760 hba->ccb[tag].req_type = 0;
Ed Lincf355882006-09-01 14:31:51 +08001761 return;
Ed Linf1498162009-03-31 17:30:19 -08001762 }
1763 msleep(1);
Ed Lincf355882006-09-01 14:31:51 +08001764 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001765}
1766
1767static void stex_hba_free(struct st_hba *hba)
1768{
Ed Lin99946f82009-03-31 17:30:25 -08001769 stex_free_irq(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001770
Ed Lin9eb46d22009-09-28 22:58:33 -08001771 destroy_workqueue(hba->work_q);
1772
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001773 iounmap(hba->mmio_base);
1774
1775 pci_release_regions(hba->pdev);
1776
Ed Lin591a3a52009-03-31 17:30:31 -08001777 kfree(hba->ccb);
1778
Ed Lin94e91082006-12-04 17:49:39 -08001779 dma_free_coherent(&hba->pdev->dev, hba->dma_size,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001780 hba->dma_mem, hba->dma_handle);
1781}
1782
1783static void stex_remove(struct pci_dev *pdev)
1784{
1785 struct st_hba *hba = pci_get_drvdata(pdev);
1786
Charles45b42ad2016-02-22 20:04:25 +08001787 hba->mu_status = MU_STATE_NOCONNECT;
1788 return_abnormal_state(hba, DID_NO_CONNECT);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001789 scsi_remove_host(hba->host);
1790
Charles45b42ad2016-02-22 20:04:25 +08001791 scsi_block_requests(hba->host);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001792
1793 stex_hba_free(hba);
1794
1795 scsi_host_put(hba->host);
1796
1797 pci_disable_device(pdev);
1798}
1799
1800static void stex_shutdown(struct pci_dev *pdev)
1801{
1802 struct st_hba *hba = pci_get_drvdata(pdev);
1803
1804 stex_hba_stop(hba);
1805}
1806
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001807MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
1808
1809static struct pci_driver stex_pci_driver = {
1810 .name = DRV_NAME,
1811 .id_table = stex_pci_tbl,
1812 .probe = stex_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001813 .remove = stex_remove,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001814 .shutdown = stex_shutdown,
1815};
1816
1817static int __init stex_init(void)
1818{
1819 printk(KERN_INFO DRV_NAME
1820 ": Promise SuperTrak EX Driver version: %s\n",
1821 ST_DRIVER_VERSION);
1822
1823 return pci_register_driver(&stex_pci_driver);
1824}
1825
1826static void __exit stex_exit(void)
1827{
1828 pci_unregister_driver(&stex_pci_driver);
1829}
1830
1831module_init(stex_init);
1832module_exit(stex_exit);