blob: 09fa8861fc580503bf92836e178a3106427bcd6b [file] [log] [blame]
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001/*
2 * SuperTrak EX Series Storage Controller driver for Linux
3 *
Ed Lin - PTUbd5cd9c2009-01-26 02:42:11 -08004 * Copyright (C) 2005-2009 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>
Jeff Garzik5a25ba12006-09-01 03:12:19 -040020#include <linux/time.h>
21#include <linux/pci.h>
22#include <linux/blkdev.h>
23#include <linux/interrupt.h>
24#include <linux/types.h>
25#include <linux/module.h>
26#include <linux/spinlock.h>
27#include <asm/io.h>
28#include <asm/irq.h>
29#include <asm/byteorder.h>
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_host.h>
Ed Lincf355882006-09-01 14:31:51 +080034#include <scsi/scsi_tcq.h>
Ed Linc25da0a2007-05-09 20:50:42 -080035#include <scsi/scsi_dbg.h>
FUJITA Tomonori11002fb2008-03-25 09:26:52 +090036#include <scsi/scsi_eh.h>
Jeff Garzik5a25ba12006-09-01 03:12:19 -040037
38#define DRV_NAME "stex"
Ed Lin05b44602009-03-31 17:30:41 -080039#define ST_DRIVER_VERSION "4.6.0000.3"
40#define ST_VER_MAJOR 4
41#define ST_VER_MINOR 6
42#define ST_OEM 0
43#define ST_BUILD_VER 3
Jeff Garzik5a25ba12006-09-01 03:12:19 -040044
45enum {
46 /* MU register offset */
47 IMR0 = 0x10, /* MU_INBOUND_MESSAGE_REG0 */
48 IMR1 = 0x14, /* MU_INBOUND_MESSAGE_REG1 */
49 OMR0 = 0x18, /* MU_OUTBOUND_MESSAGE_REG0 */
50 OMR1 = 0x1c, /* MU_OUTBOUND_MESSAGE_REG1 */
51 IDBL = 0x20, /* MU_INBOUND_DOORBELL */
52 IIS = 0x24, /* MU_INBOUND_INTERRUPT_STATUS */
53 IIM = 0x28, /* MU_INBOUND_INTERRUPT_MASK */
54 ODBL = 0x2c, /* MU_OUTBOUND_DOORBELL */
55 OIS = 0x30, /* MU_OUTBOUND_INTERRUPT_STATUS */
56 OIM = 0x3c, /* MU_OUTBOUND_INTERRUPT_MASK */
57
Ed Lin69cb4872009-08-18 12:15:14 -070058 YIOA_STATUS = 0x00,
Ed Lin0f3f6ee2009-03-31 17:30:36 -080059 YH2I_INT = 0x20,
60 YINT_EN = 0x34,
61 YI2H_INT = 0x9c,
62 YI2H_INT_C = 0xa0,
63 YH2I_REQ = 0xc0,
64 YH2I_REQ_HI = 0xc4,
65
Jeff Garzik5a25ba12006-09-01 03:12:19 -040066 /* MU register value */
67 MU_INBOUND_DOORBELL_HANDSHAKE = 1,
68 MU_INBOUND_DOORBELL_REQHEADCHANGED = 2,
69 MU_INBOUND_DOORBELL_STATUSTAILCHANGED = 4,
70 MU_INBOUND_DOORBELL_HMUSTOPPED = 8,
71 MU_INBOUND_DOORBELL_RESET = 16,
72
73 MU_OUTBOUND_DOORBELL_HANDSHAKE = 1,
74 MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED = 2,
75 MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED = 4,
76 MU_OUTBOUND_DOORBELL_BUSCHANGE = 8,
77 MU_OUTBOUND_DOORBELL_HASEVENT = 16,
78
79 /* MU status code */
80 MU_STATE_STARTING = 1,
81 MU_STATE_FMU_READY_FOR_HANDSHAKE = 2,
82 MU_STATE_SEND_HANDSHAKE_FRAME = 3,
83 MU_STATE_STARTED = 4,
84 MU_STATE_RESETTING = 5,
85
Ed Lin76fbf962006-12-04 17:49:42 -080086 MU_MAX_DELAY = 120,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040087 MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
Ed Lin529e7a62006-12-04 17:49:34 -080088 MU_HANDSHAKE_SIGNATURE_HALF = 0x5a5a0000,
Ed Lin76fbf962006-12-04 17:49:42 -080089 MU_HARD_RESET_WAIT = 30000,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040090 HMU_PARTNER_TYPE = 2,
91
92 /* firmware returned values */
93 SRB_STATUS_SUCCESS = 0x01,
94 SRB_STATUS_ERROR = 0x04,
95 SRB_STATUS_BUSY = 0x05,
96 SRB_STATUS_INVALID_REQUEST = 0x06,
97 SRB_STATUS_SELECTION_TIMEOUT = 0x0A,
98 SRB_SEE_SENSE = 0x80,
99
100 /* task attribute */
101 TASK_ATTRIBUTE_SIMPLE = 0x0,
102 TASK_ATTRIBUTE_HEADOFQUEUE = 0x1,
103 TASK_ATTRIBUTE_ORDERED = 0x2,
104 TASK_ATTRIBUTE_ACA = 0x4,
105
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800106 SS_STS_NORMAL = 0x80000000,
107 SS_STS_DONE = 0x40000000,
108 SS_STS_HANDSHAKE = 0x20000000,
109
110 SS_HEAD_HANDSHAKE = 0x80,
111
Ed Lin69cb4872009-08-18 12:15:14 -0700112 SS_H2I_INT_RESET = 0x100,
113
114 SS_MU_OPERATIONAL = 0x80000000,
115
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800116 STEX_CDB_LENGTH = 16,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400117 STATUS_VAR_LEN = 128,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400118
119 /* sg flags */
120 SG_CF_EOT = 0x80, /* end of table */
121 SG_CF_64B = 0x40, /* 64 bit item */
122 SG_CF_HOST = 0x20, /* sg in host memory */
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800123 MSG_DATA_DIR_ND = 0,
124 MSG_DATA_DIR_IN = 1,
125 MSG_DATA_DIR_OUT = 2,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400126
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400127 st_shasta = 0,
128 st_vsc = 1,
Ed Lin591a3a52009-03-31 17:30:31 -0800129 st_yosemite = 2,
130 st_seq = 3,
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800131 st_yel = 4,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400132
133 PASSTHRU_REQ_TYPE = 0x00000001,
134 PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800135 ST_INTERNAL_TIMEOUT = 180,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400136
Ed Linfb4f66b2006-09-27 19:23:41 +0800137 ST_TO_CMD = 0,
138 ST_FROM_CMD = 1,
139
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400140 /* vendor specific commands of Promise */
Ed Linfb4f66b2006-09-27 19:23:41 +0800141 MGT_CMD = 0xd8,
142 SINBAND_MGT_CMD = 0xd9,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400143 ARRAY_CMD = 0xe0,
144 CONTROLLER_CMD = 0xe1,
145 DEBUGGING_CMD = 0xe2,
146 PASSTHRU_CMD = 0xe3,
147
148 PASSTHRU_GET_ADAPTER = 0x05,
149 PASSTHRU_GET_DRVVER = 0x10,
Ed Linfb4f66b2006-09-27 19:23:41 +0800150
151 CTLR_CONFIG_CMD = 0x03,
152 CTLR_SHUTDOWN = 0x0d,
153
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400154 CTLR_POWER_STATE_CHANGE = 0x0e,
155 CTLR_POWER_SAVING = 0x01,
156
157 PASSTHRU_SIGNATURE = 0x4e415041,
Ed Linfb4f66b2006-09-27 19:23:41 +0800158 MGT_CMD_SIGNATURE = 0xba,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400159
160 INQUIRY_EVPD = 0x01,
Ed Lin94e91082006-12-04 17:49:39 -0800161
162 ST_ADDITIONAL_MEM = 0x200000,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400163};
164
165struct st_sgitem {
166 u8 ctrl; /* SG_CF_xxx */
167 u8 reserved[3];
168 __le32 count;
Ed Linf1498162009-03-31 17:30:19 -0800169 __le64 addr;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400170};
171
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800172struct st_ss_sgitem {
173 __le32 addr;
174 __le32 addr_hi;
175 __le32 count;
176};
177
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400178struct st_sgtable {
179 __le16 sg_count;
180 __le16 max_sg_count;
181 __le32 sz_in_byte;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400182};
183
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800184struct st_msg_header {
185 __le64 handle;
186 u8 flag;
187 u8 channel;
188 __le16 timeout;
189 u32 reserved;
190};
191
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400192struct handshake_frame {
Ed Linf1498162009-03-31 17:30:19 -0800193 __le64 rb_phy; /* request payload queue physical address */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400194 __le16 req_sz; /* size of each request payload */
195 __le16 req_cnt; /* count of reqs the buffer can hold */
196 __le16 status_sz; /* size of each status payload */
197 __le16 status_cnt; /* count of status the buffer can hold */
Ed Linf1498162009-03-31 17:30:19 -0800198 __le64 hosttime; /* seconds from Jan 1, 1970 (GMT) */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400199 u8 partner_type; /* who sends this frame */
200 u8 reserved0[7];
201 __le32 partner_ver_major;
202 __le32 partner_ver_minor;
203 __le32 partner_ver_oem;
204 __le32 partner_ver_build;
Ed Lin94e91082006-12-04 17:49:39 -0800205 __le32 extra_offset; /* NEW */
206 __le32 extra_size; /* NEW */
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800207 __le32 scratch_size;
208 u32 reserved1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400209};
210
211struct req_msg {
212 __le16 tag;
213 u8 lun;
214 u8 target;
215 u8 task_attr;
216 u8 task_manage;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800217 u8 data_dir;
Ed Linf903d7b72006-09-27 19:23:33 +0800218 u8 payload_sz; /* payload size in 4-byte, not used */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400219 u8 cdb[STEX_CDB_LENGTH];
Ed Lin591a3a52009-03-31 17:30:31 -0800220 u32 variable[0];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400221};
222
223struct status_msg {
224 __le16 tag;
225 u8 lun;
226 u8 target;
227 u8 srb_status;
228 u8 scsi_status;
229 u8 reserved;
230 u8 payload_sz; /* payload size in 4-byte */
231 u8 variable[STATUS_VAR_LEN];
232};
233
234struct ver_info {
235 u32 major;
236 u32 minor;
237 u32 oem;
238 u32 build;
239 u32 reserved[2];
240};
241
242struct st_frame {
243 u32 base[6];
244 u32 rom_addr;
245
246 struct ver_info drv_ver;
247 struct ver_info bios_ver;
248
249 u32 bus;
250 u32 slot;
251 u32 irq_level;
252 u32 irq_vec;
253 u32 id;
254 u32 subid;
255
256 u32 dimm_size;
257 u8 dimm_type;
258 u8 reserved[3];
259
260 u32 channel;
261 u32 reserved1;
262};
263
264struct st_drvver {
265 u32 major;
266 u32 minor;
267 u32 oem;
268 u32 build;
269 u32 signature[2];
270 u8 console_id;
271 u8 host_no;
272 u8 reserved0[2];
273 u32 reserved[3];
274};
275
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400276struct st_ccb {
277 struct req_msg *req;
278 struct scsi_cmnd *cmd;
279
280 void *sense_buffer;
281 unsigned int sense_bufflen;
282 int sg_count;
283
284 u32 req_type;
285 u8 srb_status;
286 u8 scsi_status;
Ed Linf1498162009-03-31 17:30:19 -0800287 u8 reserved[2];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400288};
289
290struct st_hba {
291 void __iomem *mmio_base; /* iomapped PCI memory space */
292 void *dma_mem;
293 dma_addr_t dma_handle;
Ed Lin94e91082006-12-04 17:49:39 -0800294 size_t dma_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400295
296 struct Scsi_Host *host;
297 struct pci_dev *pdev;
298
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800299 struct req_msg * (*alloc_rq) (struct st_hba *);
300 int (*map_sg)(struct st_hba *, struct req_msg *, struct st_ccb *);
301 void (*send) (struct st_hba *, struct req_msg *, u16);
302
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400303 u32 req_head;
304 u32 req_tail;
305 u32 status_head;
306 u32 status_tail;
307
308 struct status_msg *status_buffer;
309 void *copy_buffer; /* temp buffer for driver-handled commands */
Ed Lin591a3a52009-03-31 17:30:31 -0800310 struct st_ccb *ccb;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400311 struct st_ccb *wait_ccb;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800312 __le32 *scratch;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400313
314 unsigned int mu_status;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400315 unsigned int cardtype;
Ed Lin99946f82009-03-31 17:30:25 -0800316 int msi_enabled;
317 int out_req_cnt;
Ed Lin591a3a52009-03-31 17:30:31 -0800318 u32 extra_offset;
319 u16 rq_count;
320 u16 rq_size;
321 u16 sts_count;
322};
323
324struct st_card_info {
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800325 struct req_msg * (*alloc_rq) (struct st_hba *);
326 int (*map_sg)(struct st_hba *, struct req_msg *, struct st_ccb *);
327 void (*send) (struct st_hba *, struct req_msg *, u16);
Ed Lin591a3a52009-03-31 17:30:31 -0800328 unsigned int max_id;
329 unsigned int max_lun;
330 unsigned int max_channel;
331 u16 rq_count;
332 u16 rq_size;
333 u16 sts_count;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400334};
335
Ed Lin99946f82009-03-31 17:30:25 -0800336static int msi;
337module_param(msi, int, 0);
338MODULE_PARM_DESC(msi, "Enable Message Signaled Interrupts(0=off, 1=on)");
339
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400340static const char console_inq_page[] =
341{
342 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
343 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
344 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
345 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
346 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
347 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
348 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
349 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
350};
351
352MODULE_AUTHOR("Ed Lin");
353MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
354MODULE_LICENSE("GPL");
355MODULE_VERSION(ST_DRIVER_VERSION);
356
Ed Linf1498162009-03-31 17:30:19 -0800357static void stex_gettime(__le64 *time)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400358{
359 struct timeval tv;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400360
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800361 do_gettimeofday(&tv);
Ed Linf1498162009-03-31 17:30:19 -0800362 *time = cpu_to_le64(tv.tv_sec);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400363}
364
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400365static struct status_msg *stex_get_status(struct st_hba *hba)
366{
Ed Linf1498162009-03-31 17:30:19 -0800367 struct status_msg *status = hba->status_buffer + hba->status_tail;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400368
369 ++hba->status_tail;
Ed Lin591a3a52009-03-31 17:30:31 -0800370 hba->status_tail %= hba->sts_count+1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400371
372 return status;
373}
374
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400375static void stex_invalid_field(struct scsi_cmnd *cmd,
376 void (*done)(struct scsi_cmnd *))
377{
FUJITA Tomonori11002fb2008-03-25 09:26:52 +0900378 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
379
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800380 /* "Invalid field in cdb" */
FUJITA Tomonori11002fb2008-03-25 09:26:52 +0900381 scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
382 0x0);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400383 done(cmd);
384}
385
386static struct req_msg *stex_alloc_req(struct st_hba *hba)
387{
Ed Lin591a3a52009-03-31 17:30:31 -0800388 struct req_msg *req = hba->dma_mem + hba->req_head * hba->rq_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400389
390 ++hba->req_head;
Ed Lin591a3a52009-03-31 17:30:31 -0800391 hba->req_head %= hba->rq_count+1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400392
393 return req;
394}
395
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800396static struct req_msg *stex_ss_alloc_req(struct st_hba *hba)
397{
398 return (struct req_msg *)(hba->dma_mem +
399 hba->req_head * hba->rq_size + sizeof(struct st_msg_header));
400}
401
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400402static int stex_map_sg(struct st_hba *hba,
403 struct req_msg *req, struct st_ccb *ccb)
404{
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400405 struct scsi_cmnd *cmd;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900406 struct scatterlist *sg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400407 struct st_sgtable *dst;
Ed Linf1498162009-03-31 17:30:19 -0800408 struct st_sgitem *table;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900409 int i, nseg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400410
411 cmd = ccb->cmd;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900412 nseg = scsi_dma_map(cmd);
Ed Linf1498162009-03-31 17:30:19 -0800413 BUG_ON(nseg < 0);
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900414 if (nseg) {
Ed Linf1498162009-03-31 17:30:19 -0800415 dst = (struct st_sgtable *)req->variable;
416
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900417 ccb->sg_count = nseg;
418 dst->sg_count = cpu_to_le16((u16)nseg);
Ed Linf1498162009-03-31 17:30:19 -0800419 dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
420 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400421
Ed Linf1498162009-03-31 17:30:19 -0800422 table = (struct st_sgitem *)(dst + 1);
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900423 scsi_for_each_sg(cmd, sg, nseg, i) {
Ed Linf1498162009-03-31 17:30:19 -0800424 table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
425 table[i].addr = cpu_to_le64(sg_dma_address(sg));
426 table[i].ctrl = SG_CF_64B | SG_CF_HOST;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400427 }
Ed Linf1498162009-03-31 17:30:19 -0800428 table[--i].ctrl |= SG_CF_EOT;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400429 }
430
Ed Linf1498162009-03-31 17:30:19 -0800431 return nseg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400432}
433
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800434static int stex_ss_map_sg(struct st_hba *hba,
435 struct req_msg *req, struct st_ccb *ccb)
436{
437 struct scsi_cmnd *cmd;
438 struct scatterlist *sg;
439 struct st_sgtable *dst;
440 struct st_ss_sgitem *table;
441 int i, nseg;
442
443 cmd = ccb->cmd;
444 nseg = scsi_dma_map(cmd);
445 BUG_ON(nseg < 0);
446 if (nseg) {
447 dst = (struct st_sgtable *)req->variable;
448
449 ccb->sg_count = nseg;
450 dst->sg_count = cpu_to_le16((u16)nseg);
451 dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
452 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
453
454 table = (struct st_ss_sgitem *)(dst + 1);
455 scsi_for_each_sg(cmd, sg, nseg, i) {
456 table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
457 table[i].addr =
458 cpu_to_le32(sg_dma_address(sg) & 0xffffffff);
459 table[i].addr_hi =
460 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
461 }
462 }
463
464 return nseg;
465}
466
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400467static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
468{
469 struct st_frame *p;
470 size_t count = sizeof(struct st_frame);
471
472 p = hba->copy_buffer;
Ed Linf1498162009-03-31 17:30:19 -0800473 scsi_sg_copy_to_buffer(ccb->cmd, p, count);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400474 memset(p->base, 0, sizeof(u32)*6);
475 *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
476 p->rom_addr = 0;
477
478 p->drv_ver.major = ST_VER_MAJOR;
479 p->drv_ver.minor = ST_VER_MINOR;
480 p->drv_ver.oem = ST_OEM;
481 p->drv_ver.build = ST_BUILD_VER;
482
483 p->bus = hba->pdev->bus->number;
484 p->slot = hba->pdev->devfn;
485 p->irq_level = 0;
486 p->irq_vec = hba->pdev->irq;
487 p->id = hba->pdev->vendor << 16 | hba->pdev->device;
488 p->subid =
489 hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
490
Ed Linf1498162009-03-31 17:30:19 -0800491 scsi_sg_copy_from_buffer(ccb->cmd, p, count);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400492}
493
494static void
495stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
496{
497 req->tag = cpu_to_le16(tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400498
499 hba->ccb[tag].req = req;
500 hba->out_req_cnt++;
501
502 writel(hba->req_head, hba->mmio_base + IMR0);
503 writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
504 readl(hba->mmio_base + IDBL); /* flush */
505}
506
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800507static void
508stex_ss_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
509{
510 struct scsi_cmnd *cmd;
511 struct st_msg_header *msg_h;
512 dma_addr_t addr;
513
514 req->tag = cpu_to_le16(tag);
515
516 hba->ccb[tag].req = req;
517 hba->out_req_cnt++;
518
519 cmd = hba->ccb[tag].cmd;
520 msg_h = (struct st_msg_header *)req - 1;
521 if (likely(cmd)) {
522 msg_h->channel = (u8)cmd->device->channel;
523 msg_h->timeout = cpu_to_le16(cmd->request->timeout/HZ);
524 }
525 addr = hba->dma_handle + hba->req_head * hba->rq_size;
526 addr += (hba->ccb[tag].sg_count+4)/11;
527 msg_h->handle = cpu_to_le64(addr);
528
529 ++hba->req_head;
530 hba->req_head %= hba->rq_count+1;
531
532 writel((addr >> 16) >> 16, hba->mmio_base + YH2I_REQ_HI);
533 readl(hba->mmio_base + YH2I_REQ_HI); /* flush */
534 writel(addr, hba->mmio_base + YH2I_REQ);
535 readl(hba->mmio_base + YH2I_REQ); /* flush */
536}
537
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400538static int
Ed Lincf355882006-09-01 14:31:51 +0800539stex_slave_alloc(struct scsi_device *sdev)
540{
541 /* Cheat: usually extracted from Inquiry data */
542 sdev->tagged_supported = 1;
543
Ed Linf1498162009-03-31 17:30:19 -0800544 scsi_activate_tcq(sdev, sdev->host->can_queue);
Ed Lincf355882006-09-01 14:31:51 +0800545
546 return 0;
547}
548
549static int
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400550stex_slave_config(struct scsi_device *sdev)
551{
552 sdev->use_10_for_rw = 1;
553 sdev->use_10_for_ms = 1;
James Bottomleydc5c49b2008-11-30 10:38:08 -0600554 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
Ed Lincf355882006-09-01 14:31:51 +0800555 sdev->tagged_supported = 1;
556
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400557 return 0;
558}
559
560static void
561stex_slave_destroy(struct scsi_device *sdev)
562{
Ed Lincf355882006-09-01 14:31:51 +0800563 scsi_deactivate_tcq(sdev, 1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400564}
565
566static int
567stex_queuecommand(struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
568{
569 struct st_hba *hba;
570 struct Scsi_Host *host;
Ed Linf1498162009-03-31 17:30:19 -0800571 unsigned int id, lun;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400572 struct req_msg *req;
573 u16 tag;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800574
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400575 host = cmd->device->host;
576 id = cmd->device->id;
Ed Line0b2e592007-05-09 20:50:33 -0800577 lun = cmd->device->lun;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400578 hba = (struct st_hba *) &host->hostdata[0];
579
580 switch (cmd->cmnd[0]) {
581 case MODE_SENSE_10:
582 {
583 static char ms10_caching_page[12] =
584 { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
585 unsigned char page;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800586
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400587 page = cmd->cmnd[2] & 0x3f;
588 if (page == 0x8 || page == 0x3f) {
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900589 scsi_sg_copy_from_buffer(cmd, ms10_caching_page,
590 sizeof(ms10_caching_page));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400591 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
592 done(cmd);
593 } else
594 stex_invalid_field(cmd, done);
595 return 0;
596 }
Ed Line0b2e592007-05-09 20:50:33 -0800597 case REPORT_LUNS:
598 /*
599 * The shasta firmware does not report actual luns in the
600 * target, so fail the command to force sequential lun scan.
601 * Also, the console device does not support this command.
602 */
603 if (hba->cardtype == st_shasta || id == host->max_id - 1) {
604 stex_invalid_field(cmd, done);
605 return 0;
606 }
607 break;
Ed Lind116a7b2007-05-09 20:50:40 -0800608 case TEST_UNIT_READY:
609 if (id == host->max_id - 1) {
610 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
611 done(cmd);
612 return 0;
613 }
614 break;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400615 case INQUIRY:
Ed Line0b2e592007-05-09 20:50:33 -0800616 if (id != host->max_id - 1)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400617 break;
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800618 if (!lun && !cmd->device->channel &&
619 (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900620 scsi_sg_copy_from_buffer(cmd, (void *)console_inq_page,
621 sizeof(console_inq_page));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400622 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
623 done(cmd);
624 } else
625 stex_invalid_field(cmd, done);
626 return 0;
627 case PASSTHRU_CMD:
628 if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
629 struct st_drvver ver;
FUJITA Tomonori26106e32008-02-22 23:11:03 +0900630 size_t cp_len = sizeof(ver);
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800631
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400632 ver.major = ST_VER_MAJOR;
633 ver.minor = ST_VER_MINOR;
634 ver.oem = ST_OEM;
635 ver.build = ST_BUILD_VER;
636 ver.signature[0] = PASSTHRU_SIGNATURE;
Ed Line0b2e592007-05-09 20:50:33 -0800637 ver.console_id = host->max_id - 1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400638 ver.host_no = hba->host->host_no;
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900639 cp_len = scsi_sg_copy_from_buffer(cmd, &ver, cp_len);
FUJITA Tomonori26106e32008-02-22 23:11:03 +0900640 cmd->result = sizeof(ver) == cp_len ?
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400641 DID_OK << 16 | COMMAND_COMPLETE << 8 :
642 DID_ERROR << 16 | COMMAND_COMPLETE << 8;
643 done(cmd);
644 return 0;
645 }
646 default:
647 break;
648 }
649
650 cmd->scsi_done = done;
651
Ed Lincf355882006-09-01 14:31:51 +0800652 tag = cmd->request->tag;
653
654 if (unlikely(tag >= host->can_queue))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400655 return SCSI_MLQUEUE_HOST_BUSY;
656
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800657 req = hba->alloc_rq(hba);
Ed Linfb4f66b2006-09-27 19:23:41 +0800658
Ed Line0b2e592007-05-09 20:50:33 -0800659 req->lun = lun;
660 req->target = id;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400661
662 /* cdb */
663 memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
664
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800665 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
666 req->data_dir = MSG_DATA_DIR_IN;
667 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
668 req->data_dir = MSG_DATA_DIR_OUT;
669 else
670 req->data_dir = MSG_DATA_DIR_ND;
671
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400672 hba->ccb[tag].cmd = cmd;
673 hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
674 hba->ccb[tag].sense_buffer = cmd->sense_buffer;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400675
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800676 if (!hba->map_sg(hba, req, &hba->ccb[tag])) {
677 hba->ccb[tag].sg_count = 0;
678 memset(&req->variable[0], 0, 8);
679 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400680
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800681 hba->send(hba, req, tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400682 return 0;
683}
684
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400685static void stex_scsi_done(struct st_ccb *ccb)
686{
687 struct scsi_cmnd *cmd = ccb->cmd;
688 int result;
689
Ed Linf1498162009-03-31 17:30:19 -0800690 if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400691 result = ccb->scsi_status;
692 switch (ccb->scsi_status) {
693 case SAM_STAT_GOOD:
694 result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
695 break;
696 case SAM_STAT_CHECK_CONDITION:
697 result |= DRIVER_SENSE << 24;
698 break;
699 case SAM_STAT_BUSY:
700 result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
701 break;
702 default:
703 result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
704 break;
705 }
706 }
707 else if (ccb->srb_status & SRB_SEE_SENSE)
708 result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
709 else switch (ccb->srb_status) {
710 case SRB_STATUS_SELECTION_TIMEOUT:
711 result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
712 break;
713 case SRB_STATUS_BUSY:
714 result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
715 break;
716 case SRB_STATUS_INVALID_REQUEST:
717 case SRB_STATUS_ERROR:
718 default:
719 result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
720 break;
721 }
722
723 cmd->result = result;
724 cmd->scsi_done(cmd);
725}
726
727static void stex_copy_data(struct st_ccb *ccb,
728 struct status_msg *resp, unsigned int variable)
729{
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400730 if (resp->scsi_status != SAM_STAT_GOOD) {
731 if (ccb->sense_buffer != NULL)
732 memcpy(ccb->sense_buffer, resp->variable,
733 min(variable, ccb->sense_bufflen));
734 return;
735 }
736
737 if (ccb->cmd == NULL)
738 return;
Ed Linf1498162009-03-31 17:30:19 -0800739 scsi_sg_copy_from_buffer(ccb->cmd, resp->variable, variable);
Ed Linfb4f66b2006-09-27 19:23:41 +0800740}
741
Ed Linf1498162009-03-31 17:30:19 -0800742static void stex_check_cmd(struct st_hba *hba,
Ed Linfb4f66b2006-09-27 19:23:41 +0800743 struct st_ccb *ccb, struct status_msg *resp)
744{
Ed Linfb4f66b2006-09-27 19:23:41 +0800745 if (ccb->cmd->cmnd[0] == MGT_CMD &&
Ed Linf1498162009-03-31 17:30:19 -0800746 resp->scsi_status != SAM_STAT_CHECK_CONDITION)
Ed Lin968a5762007-07-05 12:09:06 -0700747 scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
748 le32_to_cpu(*(__le32 *)&resp->variable[0]));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400749}
750
751static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
752{
753 void __iomem *base = hba->mmio_base;
754 struct status_msg *resp;
755 struct st_ccb *ccb;
756 unsigned int size;
757 u16 tag;
758
Ed Linf1498162009-03-31 17:30:19 -0800759 if (unlikely(!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED)))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400760 return;
761
762 /* status payloads */
763 hba->status_head = readl(base + OMR1);
Ed Lin591a3a52009-03-31 17:30:31 -0800764 if (unlikely(hba->status_head > hba->sts_count)) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400765 printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
766 pci_name(hba->pdev));
767 return;
768 }
769
Ed Linfb4f66b2006-09-27 19:23:41 +0800770 /*
771 * it's not a valid status payload if:
772 * 1. there are no pending requests(e.g. during init stage)
773 * 2. there are some pending requests, but the controller is in
774 * reset status, and its type is not st_yosemite
775 * firmware of st_yosemite in reset status will return pending requests
776 * to driver, so we allow it to pass
777 */
778 if (unlikely(hba->out_req_cnt <= 0 ||
779 (hba->mu_status == MU_STATE_RESETTING &&
780 hba->cardtype != st_yosemite))) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400781 hba->status_tail = hba->status_head;
782 goto update_status;
783 }
784
785 while (hba->status_tail != hba->status_head) {
786 resp = stex_get_status(hba);
787 tag = le16_to_cpu(resp->tag);
Ed Lincf355882006-09-01 14:31:51 +0800788 if (unlikely(tag >= hba->host->can_queue)) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400789 printk(KERN_WARNING DRV_NAME
790 "(%s): invalid tag\n", pci_name(hba->pdev));
791 continue;
792 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400793
Ed Linf1498162009-03-31 17:30:19 -0800794 hba->out_req_cnt--;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400795 ccb = &hba->ccb[tag];
Ed Linf1498162009-03-31 17:30:19 -0800796 if (unlikely(hba->wait_ccb == ccb))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400797 hba->wait_ccb = NULL;
798 if (unlikely(ccb->req == NULL)) {
799 printk(KERN_WARNING DRV_NAME
800 "(%s): lagging req\n", pci_name(hba->pdev));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400801 continue;
802 }
803
804 size = resp->payload_sz * sizeof(u32); /* payload size */
805 if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
806 size > sizeof(*resp))) {
807 printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
808 pci_name(hba->pdev));
809 } else {
810 size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
811 if (size)
812 stex_copy_data(ccb, resp, size);
813 }
814
Ed Lin - PTUdd48ebf2009-01-26 02:40:11 -0800815 ccb->req = NULL;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400816 ccb->srb_status = resp->srb_status;
817 ccb->scsi_status = resp->scsi_status;
818
Ed Lincf355882006-09-01 14:31:51 +0800819 if (likely(ccb->cmd != NULL)) {
Ed Linfb4f66b2006-09-27 19:23:41 +0800820 if (hba->cardtype == st_yosemite)
Ed Linf1498162009-03-31 17:30:19 -0800821 stex_check_cmd(hba, ccb, resp);
Ed Linfb4f66b2006-09-27 19:23:41 +0800822
Ed Lincf355882006-09-01 14:31:51 +0800823 if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
824 ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
825 stex_controller_info(hba, ccb);
Ed Linfb4f66b2006-09-27 19:23:41 +0800826
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900827 scsi_dma_unmap(ccb->cmd);
Ed Lincf355882006-09-01 14:31:51 +0800828 stex_scsi_done(ccb);
Ed Linf1498162009-03-31 17:30:19 -0800829 } else
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400830 ccb->req_type = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400831 }
832
833update_status:
834 writel(hba->status_head, base + IMR1);
835 readl(base + IMR1); /* flush */
836}
837
David Howells7d12e782006-10-05 14:55:46 +0100838static irqreturn_t stex_intr(int irq, void *__hba)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400839{
840 struct st_hba *hba = __hba;
841 void __iomem *base = hba->mmio_base;
842 u32 data;
843 unsigned long flags;
844 int handled = 0;
845
846 spin_lock_irqsave(hba->host->host_lock, flags);
847
848 data = readl(base + ODBL);
849
850 if (data && data != 0xffffffff) {
851 /* clear the interrupt */
852 writel(data, base + ODBL);
853 readl(base + ODBL); /* flush */
854 stex_mu_intr(hba, data);
855 handled = 1;
856 }
857
858 spin_unlock_irqrestore(hba->host->host_lock, flags);
859
860 return IRQ_RETVAL(handled);
861}
862
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800863static void stex_ss_mu_intr(struct st_hba *hba)
864{
865 struct status_msg *resp;
866 struct st_ccb *ccb;
867 __le32 *scratch;
868 unsigned int size;
869 int count = 0;
870 u32 value;
871 u16 tag;
872
873 if (unlikely(hba->out_req_cnt <= 0 ||
874 hba->mu_status == MU_STATE_RESETTING))
875 return;
876
877 while (count < hba->sts_count) {
878 scratch = hba->scratch + hba->status_tail;
879 value = le32_to_cpu(*scratch);
880 if (unlikely(!(value & SS_STS_NORMAL)))
881 return;
882
883 resp = hba->status_buffer + hba->status_tail;
884 *scratch = 0;
885 ++count;
886 ++hba->status_tail;
887 hba->status_tail %= hba->sts_count+1;
888
889 tag = (u16)value;
890 if (unlikely(tag >= hba->host->can_queue)) {
891 printk(KERN_WARNING DRV_NAME
Ed Lin69cb4872009-08-18 12:15:14 -0700892 "(%s): invalid tag\n", pci_name(hba->pdev));
Ed Lin0f3f6ee2009-03-31 17:30:36 -0800893 continue;
894 }
895
896 hba->out_req_cnt--;
897 ccb = &hba->ccb[tag];
898 if (unlikely(hba->wait_ccb == ccb))
899 hba->wait_ccb = NULL;
900 if (unlikely(ccb->req == NULL)) {
901 printk(KERN_WARNING DRV_NAME
902 "(%s): lagging req\n", pci_name(hba->pdev));
903 continue;
904 }
905
906 ccb->req = NULL;
907 if (likely(value & SS_STS_DONE)) { /* normal case */
908 ccb->srb_status = SRB_STATUS_SUCCESS;
909 ccb->scsi_status = SAM_STAT_GOOD;
910 } else {
911 ccb->srb_status = resp->srb_status;
912 ccb->scsi_status = resp->scsi_status;
913 size = resp->payload_sz * sizeof(u32);
914 if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
915 size > sizeof(*resp))) {
916 printk(KERN_WARNING DRV_NAME
917 "(%s): bad status size\n",
918 pci_name(hba->pdev));
919 } else {
920 size -= sizeof(*resp) - STATUS_VAR_LEN;
921 if (size)
922 stex_copy_data(ccb, resp, size);
923 }
924 if (likely(ccb->cmd != NULL))
925 stex_check_cmd(hba, ccb, resp);
926 }
927
928 if (likely(ccb->cmd != NULL)) {
929 scsi_dma_unmap(ccb->cmd);
930 stex_scsi_done(ccb);
931 } else
932 ccb->req_type = 0;
933 }
934}
935
936static irqreturn_t stex_ss_intr(int irq, void *__hba)
937{
938 struct st_hba *hba = __hba;
939 void __iomem *base = hba->mmio_base;
940 u32 data;
941 unsigned long flags;
942 int handled = 0;
943
944 spin_lock_irqsave(hba->host->host_lock, flags);
945
946 data = readl(base + YI2H_INT);
947 if (data && data != 0xffffffff) {
948 /* clear the interrupt */
949 writel(data, base + YI2H_INT_C);
950 stex_ss_mu_intr(hba);
951 handled = 1;
952 }
953
954 spin_unlock_irqrestore(hba->host->host_lock, flags);
955
956 return IRQ_RETVAL(handled);
957}
958
959static int stex_common_handshake(struct st_hba *hba)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400960{
961 void __iomem *base = hba->mmio_base;
962 struct handshake_frame *h;
963 dma_addr_t status_phys;
Ed Lin529e7a62006-12-04 17:49:34 -0800964 u32 data;
Ed Lin76fbf962006-12-04 17:49:42 -0800965 unsigned long before;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400966
967 if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
968 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
969 readl(base + IDBL);
Ed Lin76fbf962006-12-04 17:49:42 -0800970 before = jiffies;
971 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
972 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
973 printk(KERN_ERR DRV_NAME
974 "(%s): no handshake signature\n",
975 pci_name(hba->pdev));
976 return -1;
977 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400978 rmb();
979 msleep(1);
980 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400981 }
982
983 udelay(10);
984
Ed Lin529e7a62006-12-04 17:49:34 -0800985 data = readl(base + OMR1);
986 if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
987 data &= 0x0000ffff;
Ed Linf1498162009-03-31 17:30:19 -0800988 if (hba->host->can_queue > data) {
Ed Lin529e7a62006-12-04 17:49:34 -0800989 hba->host->can_queue = data;
Ed Linf1498162009-03-31 17:30:19 -0800990 hba->host->cmd_per_lun = data;
991 }
Ed Lin529e7a62006-12-04 17:49:34 -0800992 }
993
Ed Linf1498162009-03-31 17:30:19 -0800994 h = (struct handshake_frame *)hba->status_buffer;
995 h->rb_phy = cpu_to_le64(hba->dma_handle);
Ed Lin591a3a52009-03-31 17:30:31 -0800996 h->req_sz = cpu_to_le16(hba->rq_size);
997 h->req_cnt = cpu_to_le16(hba->rq_count+1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400998 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
Ed Lin591a3a52009-03-31 17:30:31 -0800999 h->status_cnt = cpu_to_le16(hba->sts_count+1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001000 stex_gettime(&h->hosttime);
1001 h->partner_type = HMU_PARTNER_TYPE;
Ed Lin591a3a52009-03-31 17:30:31 -08001002 if (hba->extra_offset) {
1003 h->extra_offset = cpu_to_le32(hba->extra_offset);
Ed Lin94e91082006-12-04 17:49:39 -08001004 h->extra_size = cpu_to_le32(ST_ADDITIONAL_MEM);
1005 } else
1006 h->extra_offset = h->extra_size = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001007
Ed Lin591a3a52009-03-31 17:30:31 -08001008 status_phys = hba->dma_handle + (hba->rq_count+1) * hba->rq_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001009 writel(status_phys, base + IMR0);
1010 readl(base + IMR0);
1011 writel((status_phys >> 16) >> 16, base + IMR1);
1012 readl(base + IMR1);
1013
1014 writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
1015 readl(base + OMR0);
1016 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
1017 readl(base + IDBL); /* flush */
1018
1019 udelay(10);
Ed Lin76fbf962006-12-04 17:49:42 -08001020 before = jiffies;
1021 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
1022 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
1023 printk(KERN_ERR DRV_NAME
1024 "(%s): no signature after handshake frame\n",
1025 pci_name(hba->pdev));
1026 return -1;
1027 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001028 rmb();
1029 msleep(1);
1030 }
1031
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001032 writel(0, base + IMR0);
1033 readl(base + IMR0);
1034 writel(0, base + OMR0);
1035 readl(base + OMR0);
1036 writel(0, base + IMR1);
1037 readl(base + IMR1);
1038 writel(0, base + OMR1);
1039 readl(base + OMR1); /* flush */
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001040 return 0;
1041}
1042
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001043static int stex_ss_handshake(struct st_hba *hba)
1044{
1045 void __iomem *base = hba->mmio_base;
1046 struct st_msg_header *msg_h;
1047 struct handshake_frame *h;
Ed Lin69cb4872009-08-18 12:15:14 -07001048 __le32 *scratch;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001049 u32 data;
1050 unsigned long before;
1051 int ret = 0;
1052
Ed Lin69cb4872009-08-18 12:15:14 -07001053 before = jiffies;
1054 while ((readl(base + YIOA_STATUS) & SS_MU_OPERATIONAL) == 0) {
1055 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
1056 printk(KERN_ERR DRV_NAME
1057 "(%s): firmware not operational\n",
1058 pci_name(hba->pdev));
1059 return -1;
1060 }
1061 msleep(1);
1062 }
1063
1064 msg_h = (struct st_msg_header *)hba->dma_mem;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001065 msg_h->handle = cpu_to_le64(hba->dma_handle);
1066 msg_h->flag = SS_HEAD_HANDSHAKE;
1067
Ed Lin69cb4872009-08-18 12:15:14 -07001068 h = (struct handshake_frame *)(msg_h + 1);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001069 h->rb_phy = cpu_to_le64(hba->dma_handle);
1070 h->req_sz = cpu_to_le16(hba->rq_size);
1071 h->req_cnt = cpu_to_le16(hba->rq_count+1);
1072 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
1073 h->status_cnt = cpu_to_le16(hba->sts_count+1);
1074 stex_gettime(&h->hosttime);
1075 h->partner_type = HMU_PARTNER_TYPE;
1076 h->extra_offset = h->extra_size = 0;
1077 h->scratch_size = cpu_to_le32((hba->sts_count+1)*sizeof(u32));
1078
1079 data = readl(base + YINT_EN);
1080 data &= ~4;
1081 writel(data, base + YINT_EN);
1082 writel((hba->dma_handle >> 16) >> 16, base + YH2I_REQ_HI);
1083 writel(hba->dma_handle, base + YH2I_REQ);
1084
1085 scratch = hba->scratch;
1086 before = jiffies;
1087 while (!(le32_to_cpu(*scratch) & SS_STS_HANDSHAKE)) {
1088 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
1089 printk(KERN_ERR DRV_NAME
1090 "(%s): no signature after handshake frame\n",
1091 pci_name(hba->pdev));
1092 ret = -1;
1093 break;
1094 }
1095 rmb();
1096 msleep(1);
1097 }
1098
1099 *scratch = 0;
1100 msg_h->flag = 0;
1101 return ret;
1102}
1103
1104static int stex_handshake(struct st_hba *hba)
1105{
1106 int err;
1107 unsigned long flags;
1108
1109 err = (hba->cardtype == st_yel) ?
1110 stex_ss_handshake(hba) : stex_common_handshake(hba);
1111 if (err == 0) {
1112 spin_lock_irqsave(hba->host->host_lock, flags);
1113 hba->req_head = 0;
1114 hba->req_tail = 0;
1115 hba->status_head = 0;
1116 hba->status_tail = 0;
1117 hba->out_req_cnt = 0;
1118 hba->mu_status = MU_STATE_STARTED;
1119 spin_unlock_irqrestore(hba->host->host_lock, flags);
1120 }
1121 return err;
1122}
1123
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001124static int stex_abort(struct scsi_cmnd *cmd)
1125{
1126 struct Scsi_Host *host = cmd->device->host;
1127 struct st_hba *hba = (struct st_hba *)host->hostdata;
Ed Lincf355882006-09-01 14:31:51 +08001128 u16 tag = cmd->request->tag;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001129 void __iomem *base;
1130 u32 data;
1131 int result = SUCCESS;
1132 unsigned long flags;
Ed Linc25da0a2007-05-09 20:50:42 -08001133
1134 printk(KERN_INFO DRV_NAME
1135 "(%s): aborting command\n", pci_name(hba->pdev));
1136 scsi_print_command(cmd);
1137
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001138 base = hba->mmio_base;
1139 spin_lock_irqsave(host->host_lock, flags);
Ed Lincf355882006-09-01 14:31:51 +08001140 if (tag < host->can_queue && hba->ccb[tag].cmd == cmd)
1141 hba->wait_ccb = &hba->ccb[tag];
1142 else {
1143 for (tag = 0; tag < host->can_queue; tag++)
1144 if (hba->ccb[tag].cmd == cmd) {
1145 hba->wait_ccb = &hba->ccb[tag];
1146 break;
1147 }
1148 if (tag >= host->can_queue)
1149 goto out;
1150 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001151
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001152 if (hba->cardtype == st_yel) {
1153 data = readl(base + YI2H_INT);
1154 if (data == 0 || data == 0xffffffff)
1155 goto fail_out;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001156
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001157 writel(data, base + YI2H_INT_C);
1158 stex_ss_mu_intr(hba);
1159 } else {
1160 data = readl(base + ODBL);
1161 if (data == 0 || data == 0xffffffff)
1162 goto fail_out;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001163
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001164 writel(data, base + ODBL);
1165 readl(base + ODBL); /* flush */
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001166
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001167 stex_mu_intr(hba, data);
1168 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001169 if (hba->wait_ccb == NULL) {
1170 printk(KERN_WARNING DRV_NAME
1171 "(%s): lost interrupt\n", pci_name(hba->pdev));
1172 goto out;
1173 }
1174
1175fail_out:
FUJITA Tomonorid5587d52007-05-26 10:01:24 +09001176 scsi_dma_unmap(cmd);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001177 hba->wait_ccb->req = NULL; /* nullify the req's future return */
1178 hba->wait_ccb = NULL;
1179 result = FAILED;
1180out:
1181 spin_unlock_irqrestore(host->host_lock, flags);
1182 return result;
1183}
1184
1185static void stex_hard_reset(struct st_hba *hba)
1186{
1187 struct pci_bus *bus;
1188 int i;
1189 u16 pci_cmd;
1190 u8 pci_bctl;
1191
1192 for (i = 0; i < 16; i++)
1193 pci_read_config_dword(hba->pdev, i * 4,
1194 &hba->pdev->saved_config_space[i]);
1195
1196 /* Reset secondary bus. Our controller(MU/ATU) is the only device on
1197 secondary bus. Consult Intel 80331/3 developer's manual for detail */
1198 bus = hba->pdev->bus;
1199 pci_read_config_byte(bus->self, PCI_BRIDGE_CONTROL, &pci_bctl);
1200 pci_bctl |= PCI_BRIDGE_CTL_BUS_RESET;
1201 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
Ed Lin69f4a512007-05-09 20:50:37 -08001202
1203 /*
1204 * 1 ms may be enough for 8-port controllers. But 16-port controllers
1205 * require more time to finish bus reset. Use 100 ms here for safety
1206 */
1207 msleep(100);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001208 pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
1209 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
1210
Ed Lin76fbf962006-12-04 17:49:42 -08001211 for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001212 pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
Ed Lin47c4f992006-12-04 17:49:31 -08001213 if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001214 break;
1215 msleep(1);
1216 }
1217
1218 ssleep(5);
1219 for (i = 0; i < 16; i++)
1220 pci_write_config_dword(hba->pdev, i * 4,
1221 hba->pdev->saved_config_space[i]);
1222}
1223
Ed Lin69cb4872009-08-18 12:15:14 -07001224static void stex_ss_reset(struct st_hba *hba)
1225{
1226 writel(SS_H2I_INT_RESET, hba->mmio_base + YH2I_INT);
1227 readl(hba->mmio_base + YH2I_INT);
1228 ssleep(5);
1229}
1230
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001231static int stex_reset(struct scsi_cmnd *cmd)
1232{
1233 struct st_hba *hba;
Ed Linf1498162009-03-31 17:30:19 -08001234 void __iomem *base;
1235 unsigned long flags, before;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -08001236
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001237 hba = (struct st_hba *) &cmd->device->host->hostdata[0];
1238
Ed Linc25da0a2007-05-09 20:50:42 -08001239 printk(KERN_INFO DRV_NAME
1240 "(%s): resetting host\n", pci_name(hba->pdev));
1241 scsi_print_command(cmd);
1242
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001243 hba->mu_status = MU_STATE_RESETTING;
1244
1245 if (hba->cardtype == st_shasta)
1246 stex_hard_reset(hba);
Ed Lin69cb4872009-08-18 12:15:14 -07001247 else if (hba->cardtype == st_yel)
1248 stex_ss_reset(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001249
Ed Linfb4f66b2006-09-27 19:23:41 +08001250 if (hba->cardtype != st_yosemite) {
1251 if (stex_handshake(hba)) {
1252 printk(KERN_WARNING DRV_NAME
1253 "(%s): resetting: handshake failed\n",
1254 pci_name(hba->pdev));
1255 return FAILED;
1256 }
Ed Linfb4f66b2006-09-27 19:23:41 +08001257 return SUCCESS;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001258 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001259
Ed Linfb4f66b2006-09-27 19:23:41 +08001260 /* st_yosemite */
1261 writel(MU_INBOUND_DOORBELL_RESET, hba->mmio_base + IDBL);
1262 readl(hba->mmio_base + IDBL); /* flush */
1263 before = jiffies;
1264 while (hba->out_req_cnt > 0) {
1265 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1266 printk(KERN_WARNING DRV_NAME
1267 "(%s): reset timeout\n", pci_name(hba->pdev));
1268 return FAILED;
1269 }
1270 msleep(1);
1271 }
1272
Ed Linf1498162009-03-31 17:30:19 -08001273 base = hba->mmio_base;
1274 writel(0, base + IMR0);
1275 readl(base + IMR0);
1276 writel(0, base + OMR0);
1277 readl(base + OMR0);
1278 writel(0, base + IMR1);
1279 readl(base + IMR1);
1280 writel(0, base + OMR1);
1281 readl(base + OMR1); /* flush */
1282 spin_lock_irqsave(hba->host->host_lock, flags);
1283 hba->req_head = 0;
1284 hba->req_tail = 0;
1285 hba->status_head = 0;
1286 hba->status_tail = 0;
1287 hba->out_req_cnt = 0;
Ed Linfb4f66b2006-09-27 19:23:41 +08001288 hba->mu_status = MU_STATE_STARTED;
Ed Linf1498162009-03-31 17:30:19 -08001289 spin_unlock_irqrestore(hba->host->host_lock, flags);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001290 return SUCCESS;
1291}
1292
1293static int stex_biosparam(struct scsi_device *sdev,
1294 struct block_device *bdev, sector_t capacity, int geom[])
1295{
Ed Linb4b8bed2006-12-04 17:49:24 -08001296 int heads = 255, sectors = 63;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001297
1298 if (capacity < 0x200000) {
1299 heads = 64;
1300 sectors = 32;
1301 }
1302
Ed Linb4b8bed2006-12-04 17:49:24 -08001303 sector_div(capacity, heads * sectors);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001304
1305 geom[0] = heads;
1306 geom[1] = sectors;
Ed Linb4b8bed2006-12-04 17:49:24 -08001307 geom[2] = capacity;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001308
1309 return 0;
1310}
1311
1312static struct scsi_host_template driver_template = {
1313 .module = THIS_MODULE,
1314 .name = DRV_NAME,
1315 .proc_name = DRV_NAME,
1316 .bios_param = stex_biosparam,
1317 .queuecommand = stex_queuecommand,
Ed Lincf355882006-09-01 14:31:51 +08001318 .slave_alloc = stex_slave_alloc,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001319 .slave_configure = stex_slave_config,
1320 .slave_destroy = stex_slave_destroy,
1321 .eh_abort_handler = stex_abort,
1322 .eh_host_reset_handler = stex_reset,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001323 .this_id = -1,
Ed Lin591a3a52009-03-31 17:30:31 -08001324};
1325
1326static struct pci_device_id stex_pci_tbl[] = {
1327 /* st_shasta */
1328 { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1329 st_shasta }, /* SuperTrak EX8350/8300/16350/16300 */
1330 { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1331 st_shasta }, /* SuperTrak EX12350 */
1332 { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1333 st_shasta }, /* SuperTrak EX4350 */
1334 { 0x105a, 0xe350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1335 st_shasta }, /* SuperTrak EX24350 */
1336
1337 /* st_vsc */
1338 { 0x105a, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
1339
1340 /* st_yosemite */
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001341 { 0x105a, 0x8650, 0x105a, PCI_ANY_ID, 0, 0, st_yosemite },
Ed Lin591a3a52009-03-31 17:30:31 -08001342
1343 /* st_seq */
1344 { 0x105a, 0x3360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_seq },
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001345
1346 /* st_yel */
1347 { 0x105a, 0x8650, 0x1033, PCI_ANY_ID, 0, 0, st_yel },
1348 { 0x105a, 0x8760, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_yel },
Ed Lin591a3a52009-03-31 17:30:31 -08001349 { } /* terminate list */
1350};
1351
1352static struct st_card_info stex_card_info[] = {
1353 /* st_shasta */
1354 {
1355 .max_id = 17,
1356 .max_lun = 8,
1357 .max_channel = 0,
1358 .rq_count = 32,
1359 .rq_size = 1048,
1360 .sts_count = 32,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001361 .alloc_rq = stex_alloc_req,
1362 .map_sg = stex_map_sg,
1363 .send = stex_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001364 },
1365
1366 /* st_vsc */
1367 {
1368 .max_id = 129,
1369 .max_lun = 1,
1370 .max_channel = 0,
1371 .rq_count = 32,
1372 .rq_size = 1048,
1373 .sts_count = 32,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001374 .alloc_rq = stex_alloc_req,
1375 .map_sg = stex_map_sg,
1376 .send = stex_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001377 },
1378
1379 /* st_yosemite */
1380 {
1381 .max_id = 2,
1382 .max_lun = 256,
1383 .max_channel = 0,
1384 .rq_count = 256,
1385 .rq_size = 1048,
1386 .sts_count = 256,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001387 .alloc_rq = stex_alloc_req,
1388 .map_sg = stex_map_sg,
1389 .send = stex_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001390 },
1391
1392 /* st_seq */
1393 {
1394 .max_id = 129,
1395 .max_lun = 1,
1396 .max_channel = 0,
1397 .rq_count = 32,
1398 .rq_size = 1048,
1399 .sts_count = 32,
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001400 .alloc_rq = stex_alloc_req,
1401 .map_sg = stex_map_sg,
1402 .send = stex_send_cmd,
1403 },
1404
1405 /* st_yel */
1406 {
1407 .max_id = 129,
1408 .max_lun = 256,
1409 .max_channel = 3,
1410 .rq_count = 801,
1411 .rq_size = 512,
1412 .sts_count = 801,
1413 .alloc_rq = stex_ss_alloc_req,
1414 .map_sg = stex_ss_map_sg,
1415 .send = stex_ss_send_cmd,
Ed Lin591a3a52009-03-31 17:30:31 -08001416 },
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001417};
1418
1419static int stex_set_dma_mask(struct pci_dev * pdev)
1420{
1421 int ret;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -08001422
Yang Hongyang6a355282009-04-06 19:01:13 -07001423 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
1424 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001425 return 0;
Yang Hongyang284901a2009-04-06 19:01:15 -07001426 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001427 if (!ret)
Yang Hongyang284901a2009-04-06 19:01:15 -07001428 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001429 return ret;
1430}
1431
Ed Lin99946f82009-03-31 17:30:25 -08001432static int stex_request_irq(struct st_hba *hba)
1433{
1434 struct pci_dev *pdev = hba->pdev;
1435 int status;
1436
1437 if (msi) {
1438 status = pci_enable_msi(pdev);
1439 if (status != 0)
1440 printk(KERN_ERR DRV_NAME
1441 "(%s): error %d setting up MSI\n",
1442 pci_name(pdev), status);
1443 else
1444 hba->msi_enabled = 1;
1445 } else
1446 hba->msi_enabled = 0;
1447
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001448 status = request_irq(pdev->irq, hba->cardtype == st_yel ?
1449 stex_ss_intr : stex_intr, IRQF_SHARED, DRV_NAME, hba);
Ed Lin99946f82009-03-31 17:30:25 -08001450
1451 if (status != 0) {
1452 if (hba->msi_enabled)
1453 pci_disable_msi(pdev);
1454 }
1455 return status;
1456}
1457
1458static void stex_free_irq(struct st_hba *hba)
1459{
1460 struct pci_dev *pdev = hba->pdev;
1461
1462 free_irq(pdev->irq, hba);
1463 if (hba->msi_enabled)
1464 pci_disable_msi(pdev);
1465}
1466
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001467static int __devinit
1468stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1469{
1470 struct st_hba *hba;
1471 struct Scsi_Host *host;
Ed Lin591a3a52009-03-31 17:30:31 -08001472 const struct st_card_info *ci = NULL;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001473 u32 sts_offset, cp_offset, scratch_offset;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001474 int err;
1475
1476 err = pci_enable_device(pdev);
1477 if (err)
1478 return err;
1479
1480 pci_set_master(pdev);
1481
1482 host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
1483
1484 if (!host) {
1485 printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
1486 pci_name(pdev));
1487 err = -ENOMEM;
1488 goto out_disable;
1489 }
1490
1491 hba = (struct st_hba *)host->hostdata;
1492 memset(hba, 0, sizeof(struct st_hba));
1493
1494 err = pci_request_regions(pdev, DRV_NAME);
1495 if (err < 0) {
1496 printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
1497 pci_name(pdev));
1498 goto out_scsi_host_put;
1499 }
1500
Arjan van de Ven25729a72008-09-28 16:18:02 -07001501 hba->mmio_base = pci_ioremap_bar(pdev, 0);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001502 if ( !hba->mmio_base) {
1503 printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
1504 pci_name(pdev));
1505 err = -ENOMEM;
1506 goto out_release_regions;
1507 }
1508
1509 err = stex_set_dma_mask(pdev);
1510 if (err) {
1511 printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
1512 pci_name(pdev));
1513 goto out_iounmap;
1514 }
1515
Ed Lin94e91082006-12-04 17:49:39 -08001516 hba->cardtype = (unsigned int) id->driver_data;
Ed Lin591a3a52009-03-31 17:30:31 -08001517 ci = &stex_card_info[hba->cardtype];
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001518 sts_offset = scratch_offset = (ci->rq_count+1) * ci->rq_size;
1519 if (hba->cardtype == st_yel)
1520 sts_offset += (ci->sts_count+1) * sizeof(u32);
Ed Lin591a3a52009-03-31 17:30:31 -08001521 cp_offset = sts_offset + (ci->sts_count+1) * sizeof(struct status_msg);
1522 hba->dma_size = cp_offset + sizeof(struct st_frame);
1523 if (hba->cardtype == st_seq ||
1524 (hba->cardtype == st_vsc && (pdev->subsystem_device & 1))) {
1525 hba->extra_offset = hba->dma_size;
1526 hba->dma_size += ST_ADDITIONAL_MEM;
1527 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001528 hba->dma_mem = dma_alloc_coherent(&pdev->dev,
Ed Lin94e91082006-12-04 17:49:39 -08001529 hba->dma_size, &hba->dma_handle, GFP_KERNEL);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001530 if (!hba->dma_mem) {
1531 err = -ENOMEM;
1532 printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
1533 pci_name(pdev));
1534 goto out_iounmap;
1535 }
1536
Ed Lin591a3a52009-03-31 17:30:31 -08001537 hba->ccb = kcalloc(ci->rq_count, sizeof(struct st_ccb), GFP_KERNEL);
1538 if (!hba->ccb) {
1539 err = -ENOMEM;
1540 printk(KERN_ERR DRV_NAME "(%s): ccb alloc failed\n",
1541 pci_name(pdev));
1542 goto out_pci_free;
1543 }
1544
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001545 if (hba->cardtype == st_yel)
1546 hba->scratch = (__le32 *)(hba->dma_mem + scratch_offset);
Ed Lin591a3a52009-03-31 17:30:31 -08001547 hba->status_buffer = (struct status_msg *)(hba->dma_mem + sts_offset);
1548 hba->copy_buffer = hba->dma_mem + cp_offset;
1549 hba->rq_count = ci->rq_count;
1550 hba->rq_size = ci->rq_size;
1551 hba->sts_count = ci->sts_count;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001552 hba->alloc_rq = ci->alloc_rq;
1553 hba->map_sg = ci->map_sg;
1554 hba->send = ci->send;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001555 hba->mu_status = MU_STATE_STARTING;
1556
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001557 if (hba->cardtype == st_yel)
1558 host->sg_tablesize = 38;
1559 else
1560 host->sg_tablesize = 32;
Ed Lin591a3a52009-03-31 17:30:31 -08001561 host->can_queue = ci->rq_count;
1562 host->cmd_per_lun = ci->rq_count;
1563 host->max_id = ci->max_id;
1564 host->max_lun = ci->max_lun;
1565 host->max_channel = ci->max_channel;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001566 host->unique_id = host->host_no;
1567 host->max_cmd_len = STEX_CDB_LENGTH;
1568
1569 hba->host = host;
1570 hba->pdev = pdev;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001571
Ed Lin99946f82009-03-31 17:30:25 -08001572 err = stex_request_irq(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001573 if (err) {
1574 printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
1575 pci_name(pdev));
Ed Lin591a3a52009-03-31 17:30:31 -08001576 goto out_ccb_free;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001577 }
1578
1579 err = stex_handshake(hba);
1580 if (err)
1581 goto out_free_irq;
1582
Ed Lin529e7a62006-12-04 17:49:34 -08001583 err = scsi_init_shared_tag_map(host, host->can_queue);
James Bottomleydeb81d82006-09-01 09:28:48 -04001584 if (err) {
Ed Lincf355882006-09-01 14:31:51 +08001585 printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
1586 pci_name(pdev));
1587 goto out_free_irq;
1588 }
1589
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001590 pci_set_drvdata(pdev, hba);
1591
1592 err = scsi_add_host(host, &pdev->dev);
1593 if (err) {
1594 printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
1595 pci_name(pdev));
1596 goto out_free_irq;
1597 }
1598
1599 scsi_scan_host(host);
1600
1601 return 0;
1602
1603out_free_irq:
Ed Lin99946f82009-03-31 17:30:25 -08001604 stex_free_irq(hba);
Ed Lin591a3a52009-03-31 17:30:31 -08001605out_ccb_free:
1606 kfree(hba->ccb);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001607out_pci_free:
Ed Lin94e91082006-12-04 17:49:39 -08001608 dma_free_coherent(&pdev->dev, hba->dma_size,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001609 hba->dma_mem, hba->dma_handle);
1610out_iounmap:
1611 iounmap(hba->mmio_base);
1612out_release_regions:
1613 pci_release_regions(pdev);
1614out_scsi_host_put:
1615 scsi_host_put(host);
1616out_disable:
1617 pci_disable_device(pdev);
1618
1619 return err;
1620}
1621
1622static void stex_hba_stop(struct st_hba *hba)
1623{
1624 struct req_msg *req;
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001625 struct st_msg_header *msg_h;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001626 unsigned long flags;
1627 unsigned long before;
Ed Lincf355882006-09-01 14:31:51 +08001628 u16 tag = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001629
1630 spin_lock_irqsave(hba->host->host_lock, flags);
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001631 req = hba->alloc_rq(hba);
1632 if (hba->cardtype == st_yel) {
1633 msg_h = (struct st_msg_header *)req - 1;
1634 memset(msg_h, 0, hba->rq_size);
1635 } else
1636 memset(req, 0, hba->rq_size);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001637
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001638 if (hba->cardtype == st_yosemite || hba->cardtype == st_yel) {
Ed Linfb4f66b2006-09-27 19:23:41 +08001639 req->cdb[0] = MGT_CMD;
1640 req->cdb[1] = MGT_CMD_SIGNATURE;
1641 req->cdb[2] = CTLR_CONFIG_CMD;
1642 req->cdb[3] = CTLR_SHUTDOWN;
1643 } else {
1644 req->cdb[0] = CONTROLLER_CMD;
1645 req->cdb[1] = CTLR_POWER_STATE_CHANGE;
1646 req->cdb[2] = CTLR_POWER_SAVING;
1647 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001648
1649 hba->ccb[tag].cmd = NULL;
1650 hba->ccb[tag].sg_count = 0;
1651 hba->ccb[tag].sense_bufflen = 0;
1652 hba->ccb[tag].sense_buffer = NULL;
Ed Linf1498162009-03-31 17:30:19 -08001653 hba->ccb[tag].req_type = PASSTHRU_REQ_TYPE;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001654
Ed Lin0f3f6ee2009-03-31 17:30:36 -08001655 hba->send(hba, req, tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001656 spin_unlock_irqrestore(hba->host->host_lock, flags);
1657
Ed Lincf355882006-09-01 14:31:51 +08001658 before = jiffies;
1659 while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
Ed Linf1498162009-03-31 17:30:19 -08001660 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1661 hba->ccb[tag].req_type = 0;
Ed Lincf355882006-09-01 14:31:51 +08001662 return;
Ed Linf1498162009-03-31 17:30:19 -08001663 }
1664 msleep(1);
Ed Lincf355882006-09-01 14:31:51 +08001665 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001666}
1667
1668static void stex_hba_free(struct st_hba *hba)
1669{
Ed Lin99946f82009-03-31 17:30:25 -08001670 stex_free_irq(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001671
1672 iounmap(hba->mmio_base);
1673
1674 pci_release_regions(hba->pdev);
1675
Ed Lin591a3a52009-03-31 17:30:31 -08001676 kfree(hba->ccb);
1677
Ed Lin94e91082006-12-04 17:49:39 -08001678 dma_free_coherent(&hba->pdev->dev, hba->dma_size,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001679 hba->dma_mem, hba->dma_handle);
1680}
1681
1682static void stex_remove(struct pci_dev *pdev)
1683{
1684 struct st_hba *hba = pci_get_drvdata(pdev);
1685
1686 scsi_remove_host(hba->host);
1687
1688 pci_set_drvdata(pdev, NULL);
1689
1690 stex_hba_stop(hba);
1691
1692 stex_hba_free(hba);
1693
1694 scsi_host_put(hba->host);
1695
1696 pci_disable_device(pdev);
1697}
1698
1699static void stex_shutdown(struct pci_dev *pdev)
1700{
1701 struct st_hba *hba = pci_get_drvdata(pdev);
1702
1703 stex_hba_stop(hba);
1704}
1705
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001706MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
1707
1708static struct pci_driver stex_pci_driver = {
1709 .name = DRV_NAME,
1710 .id_table = stex_pci_tbl,
1711 .probe = stex_probe,
1712 .remove = __devexit_p(stex_remove),
1713 .shutdown = stex_shutdown,
1714};
1715
1716static int __init stex_init(void)
1717{
1718 printk(KERN_INFO DRV_NAME
1719 ": Promise SuperTrak EX Driver version: %s\n",
1720 ST_DRIVER_VERSION);
1721
1722 return pci_register_driver(&stex_pci_driver);
1723}
1724
1725static void __exit stex_exit(void)
1726{
1727 pci_unregister_driver(&stex_pci_driver);
1728}
1729
1730module_init(stex_init);
1731module_exit(stex_exit);