blob: 75f5079e56962bcc6d91a0cb87512cec18371035 [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 Lin - PTUbd5cd9c2009-01-26 02:42:11 -080039#define ST_DRIVER_VERSION "4.6.0000.1"
40#define ST_VER_MAJOR 4
Ed Linc25da0a2007-05-09 20:50:42 -080041#define ST_VER_MINOR 6
Jeff Garzik5a25ba12006-09-01 03:12:19 -040042#define ST_OEM 0
Ed Linfb4f66b2006-09-27 19:23:41 +080043#define ST_BUILD_VER 1
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
58 /* MU register value */
59 MU_INBOUND_DOORBELL_HANDSHAKE = 1,
60 MU_INBOUND_DOORBELL_REQHEADCHANGED = 2,
61 MU_INBOUND_DOORBELL_STATUSTAILCHANGED = 4,
62 MU_INBOUND_DOORBELL_HMUSTOPPED = 8,
63 MU_INBOUND_DOORBELL_RESET = 16,
64
65 MU_OUTBOUND_DOORBELL_HANDSHAKE = 1,
66 MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED = 2,
67 MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED = 4,
68 MU_OUTBOUND_DOORBELL_BUSCHANGE = 8,
69 MU_OUTBOUND_DOORBELL_HASEVENT = 16,
70
71 /* MU status code */
72 MU_STATE_STARTING = 1,
73 MU_STATE_FMU_READY_FOR_HANDSHAKE = 2,
74 MU_STATE_SEND_HANDSHAKE_FRAME = 3,
75 MU_STATE_STARTED = 4,
76 MU_STATE_RESETTING = 5,
77
Ed Lin76fbf962006-12-04 17:49:42 -080078 MU_MAX_DELAY = 120,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040079 MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
Ed Lin529e7a62006-12-04 17:49:34 -080080 MU_HANDSHAKE_SIGNATURE_HALF = 0x5a5a0000,
Ed Lin76fbf962006-12-04 17:49:42 -080081 MU_HARD_RESET_WAIT = 30000,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040082 HMU_PARTNER_TYPE = 2,
83
84 /* firmware returned values */
85 SRB_STATUS_SUCCESS = 0x01,
86 SRB_STATUS_ERROR = 0x04,
87 SRB_STATUS_BUSY = 0x05,
88 SRB_STATUS_INVALID_REQUEST = 0x06,
89 SRB_STATUS_SELECTION_TIMEOUT = 0x0A,
90 SRB_SEE_SENSE = 0x80,
91
92 /* task attribute */
93 TASK_ATTRIBUTE_SIMPLE = 0x0,
94 TASK_ATTRIBUTE_HEADOFQUEUE = 0x1,
95 TASK_ATTRIBUTE_ORDERED = 0x2,
96 TASK_ATTRIBUTE_ACA = 0x4,
97
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -080098 STEX_CDB_LENGTH = 16,
Jeff Garzik5a25ba12006-09-01 03:12:19 -040099 STATUS_VAR_LEN = 128,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400100 ST_MAX_SG = 32,
101
102 /* sg flags */
103 SG_CF_EOT = 0x80, /* end of table */
104 SG_CF_64B = 0x40, /* 64 bit item */
105 SG_CF_HOST = 0x20, /* sg in host memory */
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800106 MSG_DATA_DIR_ND = 0,
107 MSG_DATA_DIR_IN = 1,
108 MSG_DATA_DIR_OUT = 2,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400109
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400110 st_shasta = 0,
111 st_vsc = 1,
Ed Lin591a3a52009-03-31 17:30:31 -0800112 st_yosemite = 2,
113 st_seq = 3,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400114
115 PASSTHRU_REQ_TYPE = 0x00000001,
116 PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800117 ST_INTERNAL_TIMEOUT = 180,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400118
Ed Linfb4f66b2006-09-27 19:23:41 +0800119 ST_TO_CMD = 0,
120 ST_FROM_CMD = 1,
121
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400122 /* vendor specific commands of Promise */
Ed Linfb4f66b2006-09-27 19:23:41 +0800123 MGT_CMD = 0xd8,
124 SINBAND_MGT_CMD = 0xd9,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400125 ARRAY_CMD = 0xe0,
126 CONTROLLER_CMD = 0xe1,
127 DEBUGGING_CMD = 0xe2,
128 PASSTHRU_CMD = 0xe3,
129
130 PASSTHRU_GET_ADAPTER = 0x05,
131 PASSTHRU_GET_DRVVER = 0x10,
Ed Linfb4f66b2006-09-27 19:23:41 +0800132
133 CTLR_CONFIG_CMD = 0x03,
134 CTLR_SHUTDOWN = 0x0d,
135
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400136 CTLR_POWER_STATE_CHANGE = 0x0e,
137 CTLR_POWER_SAVING = 0x01,
138
139 PASSTHRU_SIGNATURE = 0x4e415041,
Ed Linfb4f66b2006-09-27 19:23:41 +0800140 MGT_CMD_SIGNATURE = 0xba,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400141
142 INQUIRY_EVPD = 0x01,
Ed Lin94e91082006-12-04 17:49:39 -0800143
144 ST_ADDITIONAL_MEM = 0x200000,
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400145};
146
147struct st_sgitem {
148 u8 ctrl; /* SG_CF_xxx */
149 u8 reserved[3];
150 __le32 count;
Ed Linf1498162009-03-31 17:30:19 -0800151 __le64 addr;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400152};
153
154struct st_sgtable {
155 __le16 sg_count;
156 __le16 max_sg_count;
157 __le32 sz_in_byte;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400158};
159
160struct handshake_frame {
Ed Linf1498162009-03-31 17:30:19 -0800161 __le64 rb_phy; /* request payload queue physical address */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400162 __le16 req_sz; /* size of each request payload */
163 __le16 req_cnt; /* count of reqs the buffer can hold */
164 __le16 status_sz; /* size of each status payload */
165 __le16 status_cnt; /* count of status the buffer can hold */
Ed Linf1498162009-03-31 17:30:19 -0800166 __le64 hosttime; /* seconds from Jan 1, 1970 (GMT) */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400167 u8 partner_type; /* who sends this frame */
168 u8 reserved0[7];
169 __le32 partner_ver_major;
170 __le32 partner_ver_minor;
171 __le32 partner_ver_oem;
172 __le32 partner_ver_build;
Ed Lin94e91082006-12-04 17:49:39 -0800173 __le32 extra_offset; /* NEW */
174 __le32 extra_size; /* NEW */
175 u32 reserved1[2];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400176};
177
178struct req_msg {
179 __le16 tag;
180 u8 lun;
181 u8 target;
182 u8 task_attr;
183 u8 task_manage;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800184 u8 data_dir;
Ed Linf903d7b72006-09-27 19:23:33 +0800185 u8 payload_sz; /* payload size in 4-byte, not used */
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400186 u8 cdb[STEX_CDB_LENGTH];
Ed Lin591a3a52009-03-31 17:30:31 -0800187 u32 variable[0];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400188};
189
190struct status_msg {
191 __le16 tag;
192 u8 lun;
193 u8 target;
194 u8 srb_status;
195 u8 scsi_status;
196 u8 reserved;
197 u8 payload_sz; /* payload size in 4-byte */
198 u8 variable[STATUS_VAR_LEN];
199};
200
201struct ver_info {
202 u32 major;
203 u32 minor;
204 u32 oem;
205 u32 build;
206 u32 reserved[2];
207};
208
209struct st_frame {
210 u32 base[6];
211 u32 rom_addr;
212
213 struct ver_info drv_ver;
214 struct ver_info bios_ver;
215
216 u32 bus;
217 u32 slot;
218 u32 irq_level;
219 u32 irq_vec;
220 u32 id;
221 u32 subid;
222
223 u32 dimm_size;
224 u8 dimm_type;
225 u8 reserved[3];
226
227 u32 channel;
228 u32 reserved1;
229};
230
231struct st_drvver {
232 u32 major;
233 u32 minor;
234 u32 oem;
235 u32 build;
236 u32 signature[2];
237 u8 console_id;
238 u8 host_no;
239 u8 reserved0[2];
240 u32 reserved[3];
241};
242
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400243struct st_ccb {
244 struct req_msg *req;
245 struct scsi_cmnd *cmd;
246
247 void *sense_buffer;
248 unsigned int sense_bufflen;
249 int sg_count;
250
251 u32 req_type;
252 u8 srb_status;
253 u8 scsi_status;
Ed Linf1498162009-03-31 17:30:19 -0800254 u8 reserved[2];
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400255};
256
257struct st_hba {
258 void __iomem *mmio_base; /* iomapped PCI memory space */
259 void *dma_mem;
260 dma_addr_t dma_handle;
Ed Lin94e91082006-12-04 17:49:39 -0800261 size_t dma_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400262
263 struct Scsi_Host *host;
264 struct pci_dev *pdev;
265
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400266 u32 req_head;
267 u32 req_tail;
268 u32 status_head;
269 u32 status_tail;
270
271 struct status_msg *status_buffer;
272 void *copy_buffer; /* temp buffer for driver-handled commands */
Ed Lin591a3a52009-03-31 17:30:31 -0800273 struct st_ccb *ccb;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400274 struct st_ccb *wait_ccb;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400275
276 unsigned int mu_status;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400277 unsigned int cardtype;
Ed Lin99946f82009-03-31 17:30:25 -0800278 int msi_enabled;
279 int out_req_cnt;
Ed Lin591a3a52009-03-31 17:30:31 -0800280 u32 extra_offset;
281 u16 rq_count;
282 u16 rq_size;
283 u16 sts_count;
284};
285
286struct st_card_info {
287 unsigned int max_id;
288 unsigned int max_lun;
289 unsigned int max_channel;
290 u16 rq_count;
291 u16 rq_size;
292 u16 sts_count;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400293};
294
Ed Lin99946f82009-03-31 17:30:25 -0800295static int msi;
296module_param(msi, int, 0);
297MODULE_PARM_DESC(msi, "Enable Message Signaled Interrupts(0=off, 1=on)");
298
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400299static const char console_inq_page[] =
300{
301 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
302 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
303 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
304 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
305 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
306 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
307 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
308 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
309};
310
311MODULE_AUTHOR("Ed Lin");
312MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
313MODULE_LICENSE("GPL");
314MODULE_VERSION(ST_DRIVER_VERSION);
315
Ed Linf1498162009-03-31 17:30:19 -0800316static void stex_gettime(__le64 *time)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400317{
318 struct timeval tv;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400319
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800320 do_gettimeofday(&tv);
Ed Linf1498162009-03-31 17:30:19 -0800321 *time = cpu_to_le64(tv.tv_sec);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400322}
323
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400324static struct status_msg *stex_get_status(struct st_hba *hba)
325{
Ed Linf1498162009-03-31 17:30:19 -0800326 struct status_msg *status = hba->status_buffer + hba->status_tail;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400327
328 ++hba->status_tail;
Ed Lin591a3a52009-03-31 17:30:31 -0800329 hba->status_tail %= hba->sts_count+1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400330
331 return status;
332}
333
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400334static void stex_invalid_field(struct scsi_cmnd *cmd,
335 void (*done)(struct scsi_cmnd *))
336{
FUJITA Tomonori11002fb2008-03-25 09:26:52 +0900337 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
338
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800339 /* "Invalid field in cdb" */
FUJITA Tomonori11002fb2008-03-25 09:26:52 +0900340 scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
341 0x0);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400342 done(cmd);
343}
344
345static struct req_msg *stex_alloc_req(struct st_hba *hba)
346{
Ed Lin591a3a52009-03-31 17:30:31 -0800347 struct req_msg *req = hba->dma_mem + hba->req_head * hba->rq_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400348
349 ++hba->req_head;
Ed Lin591a3a52009-03-31 17:30:31 -0800350 hba->req_head %= hba->rq_count+1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400351
352 return req;
353}
354
355static int stex_map_sg(struct st_hba *hba,
356 struct req_msg *req, struct st_ccb *ccb)
357{
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400358 struct scsi_cmnd *cmd;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900359 struct scatterlist *sg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400360 struct st_sgtable *dst;
Ed Linf1498162009-03-31 17:30:19 -0800361 struct st_sgitem *table;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900362 int i, nseg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400363
364 cmd = ccb->cmd;
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900365 nseg = scsi_dma_map(cmd);
Ed Linf1498162009-03-31 17:30:19 -0800366 BUG_ON(nseg < 0);
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900367 if (nseg) {
Ed Linf1498162009-03-31 17:30:19 -0800368 dst = (struct st_sgtable *)req->variable;
369
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900370 ccb->sg_count = nseg;
371 dst->sg_count = cpu_to_le16((u16)nseg);
Ed Linf1498162009-03-31 17:30:19 -0800372 dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
373 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400374
Ed Linf1498162009-03-31 17:30:19 -0800375 table = (struct st_sgitem *)(dst + 1);
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900376 scsi_for_each_sg(cmd, sg, nseg, i) {
Ed Linf1498162009-03-31 17:30:19 -0800377 table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
378 table[i].addr = cpu_to_le64(sg_dma_address(sg));
379 table[i].ctrl = SG_CF_64B | SG_CF_HOST;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400380 }
Ed Linf1498162009-03-31 17:30:19 -0800381 table[--i].ctrl |= SG_CF_EOT;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400382 }
383
Ed Linf1498162009-03-31 17:30:19 -0800384 return nseg;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400385}
386
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400387static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
388{
389 struct st_frame *p;
390 size_t count = sizeof(struct st_frame);
391
392 p = hba->copy_buffer;
Ed Linf1498162009-03-31 17:30:19 -0800393 scsi_sg_copy_to_buffer(ccb->cmd, p, count);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400394 memset(p->base, 0, sizeof(u32)*6);
395 *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
396 p->rom_addr = 0;
397
398 p->drv_ver.major = ST_VER_MAJOR;
399 p->drv_ver.minor = ST_VER_MINOR;
400 p->drv_ver.oem = ST_OEM;
401 p->drv_ver.build = ST_BUILD_VER;
402
403 p->bus = hba->pdev->bus->number;
404 p->slot = hba->pdev->devfn;
405 p->irq_level = 0;
406 p->irq_vec = hba->pdev->irq;
407 p->id = hba->pdev->vendor << 16 | hba->pdev->device;
408 p->subid =
409 hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
410
Ed Linf1498162009-03-31 17:30:19 -0800411 scsi_sg_copy_from_buffer(ccb->cmd, p, count);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400412}
413
414static void
415stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
416{
417 req->tag = cpu_to_le16(tag);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400418
419 hba->ccb[tag].req = req;
420 hba->out_req_cnt++;
421
422 writel(hba->req_head, hba->mmio_base + IMR0);
423 writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
424 readl(hba->mmio_base + IDBL); /* flush */
425}
426
427static int
Ed Lincf355882006-09-01 14:31:51 +0800428stex_slave_alloc(struct scsi_device *sdev)
429{
430 /* Cheat: usually extracted from Inquiry data */
431 sdev->tagged_supported = 1;
432
Ed Linf1498162009-03-31 17:30:19 -0800433 scsi_activate_tcq(sdev, sdev->host->can_queue);
Ed Lincf355882006-09-01 14:31:51 +0800434
435 return 0;
436}
437
438static int
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400439stex_slave_config(struct scsi_device *sdev)
440{
441 sdev->use_10_for_rw = 1;
442 sdev->use_10_for_ms = 1;
James Bottomleydc5c49b2008-11-30 10:38:08 -0600443 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
Ed Lincf355882006-09-01 14:31:51 +0800444 sdev->tagged_supported = 1;
445
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400446 return 0;
447}
448
449static void
450stex_slave_destroy(struct scsi_device *sdev)
451{
Ed Lincf355882006-09-01 14:31:51 +0800452 scsi_deactivate_tcq(sdev, 1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400453}
454
455static int
456stex_queuecommand(struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
457{
458 struct st_hba *hba;
459 struct Scsi_Host *host;
Ed Linf1498162009-03-31 17:30:19 -0800460 unsigned int id, lun;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400461 struct req_msg *req;
462 u16 tag;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800463
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400464 host = cmd->device->host;
465 id = cmd->device->id;
Ed Line0b2e592007-05-09 20:50:33 -0800466 lun = cmd->device->lun;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400467 hba = (struct st_hba *) &host->hostdata[0];
468
469 switch (cmd->cmnd[0]) {
470 case MODE_SENSE_10:
471 {
472 static char ms10_caching_page[12] =
473 { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
474 unsigned char page;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800475
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400476 page = cmd->cmnd[2] & 0x3f;
477 if (page == 0x8 || page == 0x3f) {
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900478 scsi_sg_copy_from_buffer(cmd, ms10_caching_page,
479 sizeof(ms10_caching_page));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400480 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
481 done(cmd);
482 } else
483 stex_invalid_field(cmd, done);
484 return 0;
485 }
Ed Line0b2e592007-05-09 20:50:33 -0800486 case REPORT_LUNS:
487 /*
488 * The shasta firmware does not report actual luns in the
489 * target, so fail the command to force sequential lun scan.
490 * Also, the console device does not support this command.
491 */
492 if (hba->cardtype == st_shasta || id == host->max_id - 1) {
493 stex_invalid_field(cmd, done);
494 return 0;
495 }
496 break;
Ed Lind116a7b2007-05-09 20:50:40 -0800497 case TEST_UNIT_READY:
498 if (id == host->max_id - 1) {
499 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
500 done(cmd);
501 return 0;
502 }
503 break;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400504 case INQUIRY:
Ed Line0b2e592007-05-09 20:50:33 -0800505 if (id != host->max_id - 1)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400506 break;
507 if (lun == 0 && (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900508 scsi_sg_copy_from_buffer(cmd, (void *)console_inq_page,
509 sizeof(console_inq_page));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400510 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
511 done(cmd);
512 } else
513 stex_invalid_field(cmd, done);
514 return 0;
515 case PASSTHRU_CMD:
516 if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
517 struct st_drvver ver;
FUJITA Tomonori26106e32008-02-22 23:11:03 +0900518 size_t cp_len = sizeof(ver);
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800519
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400520 ver.major = ST_VER_MAJOR;
521 ver.minor = ST_VER_MINOR;
522 ver.oem = ST_OEM;
523 ver.build = ST_BUILD_VER;
524 ver.signature[0] = PASSTHRU_SIGNATURE;
Ed Line0b2e592007-05-09 20:50:33 -0800525 ver.console_id = host->max_id - 1;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400526 ver.host_no = hba->host->host_no;
FUJITA Tomonori31fe47d2008-03-09 13:44:35 +0900527 cp_len = scsi_sg_copy_from_buffer(cmd, &ver, cp_len);
FUJITA Tomonori26106e32008-02-22 23:11:03 +0900528 cmd->result = sizeof(ver) == cp_len ?
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400529 DID_OK << 16 | COMMAND_COMPLETE << 8 :
530 DID_ERROR << 16 | COMMAND_COMPLETE << 8;
531 done(cmd);
532 return 0;
533 }
534 default:
535 break;
536 }
537
538 cmd->scsi_done = done;
539
Ed Lincf355882006-09-01 14:31:51 +0800540 tag = cmd->request->tag;
541
542 if (unlikely(tag >= host->can_queue))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400543 return SCSI_MLQUEUE_HOST_BUSY;
544
545 req = stex_alloc_req(hba);
Ed Linfb4f66b2006-09-27 19:23:41 +0800546
Ed Line0b2e592007-05-09 20:50:33 -0800547 req->lun = lun;
548 req->target = id;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400549
550 /* cdb */
551 memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
552
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800553 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
554 req->data_dir = MSG_DATA_DIR_IN;
555 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
556 req->data_dir = MSG_DATA_DIR_OUT;
557 else
558 req->data_dir = MSG_DATA_DIR_ND;
559
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400560 hba->ccb[tag].cmd = cmd;
561 hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
562 hba->ccb[tag].sense_buffer = cmd->sense_buffer;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400563
564 if (cmd->sc_data_direction != DMA_NONE)
565 stex_map_sg(hba, req, &hba->ccb[tag]);
566
567 stex_send_cmd(hba, req, tag);
568 return 0;
569}
570
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400571static void stex_scsi_done(struct st_ccb *ccb)
572{
573 struct scsi_cmnd *cmd = ccb->cmd;
574 int result;
575
Ed Linf1498162009-03-31 17:30:19 -0800576 if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400577 result = ccb->scsi_status;
578 switch (ccb->scsi_status) {
579 case SAM_STAT_GOOD:
580 result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
581 break;
582 case SAM_STAT_CHECK_CONDITION:
583 result |= DRIVER_SENSE << 24;
584 break;
585 case SAM_STAT_BUSY:
586 result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
587 break;
588 default:
589 result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
590 break;
591 }
592 }
593 else if (ccb->srb_status & SRB_SEE_SENSE)
594 result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
595 else switch (ccb->srb_status) {
596 case SRB_STATUS_SELECTION_TIMEOUT:
597 result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
598 break;
599 case SRB_STATUS_BUSY:
600 result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
601 break;
602 case SRB_STATUS_INVALID_REQUEST:
603 case SRB_STATUS_ERROR:
604 default:
605 result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
606 break;
607 }
608
609 cmd->result = result;
610 cmd->scsi_done(cmd);
611}
612
613static void stex_copy_data(struct st_ccb *ccb,
614 struct status_msg *resp, unsigned int variable)
615{
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400616 if (resp->scsi_status != SAM_STAT_GOOD) {
617 if (ccb->sense_buffer != NULL)
618 memcpy(ccb->sense_buffer, resp->variable,
619 min(variable, ccb->sense_bufflen));
620 return;
621 }
622
623 if (ccb->cmd == NULL)
624 return;
Ed Linf1498162009-03-31 17:30:19 -0800625 scsi_sg_copy_from_buffer(ccb->cmd, resp->variable, variable);
Ed Linfb4f66b2006-09-27 19:23:41 +0800626}
627
Ed Linf1498162009-03-31 17:30:19 -0800628static void stex_check_cmd(struct st_hba *hba,
Ed Linfb4f66b2006-09-27 19:23:41 +0800629 struct st_ccb *ccb, struct status_msg *resp)
630{
Ed Linfb4f66b2006-09-27 19:23:41 +0800631 if (ccb->cmd->cmnd[0] == MGT_CMD &&
Ed Linf1498162009-03-31 17:30:19 -0800632 resp->scsi_status != SAM_STAT_CHECK_CONDITION)
Ed Lin968a5762007-07-05 12:09:06 -0700633 scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
634 le32_to_cpu(*(__le32 *)&resp->variable[0]));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400635}
636
637static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
638{
639 void __iomem *base = hba->mmio_base;
640 struct status_msg *resp;
641 struct st_ccb *ccb;
642 unsigned int size;
643 u16 tag;
644
Ed Linf1498162009-03-31 17:30:19 -0800645 if (unlikely(!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED)))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400646 return;
647
648 /* status payloads */
649 hba->status_head = readl(base + OMR1);
Ed Lin591a3a52009-03-31 17:30:31 -0800650 if (unlikely(hba->status_head > hba->sts_count)) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400651 printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
652 pci_name(hba->pdev));
653 return;
654 }
655
Ed Linfb4f66b2006-09-27 19:23:41 +0800656 /*
657 * it's not a valid status payload if:
658 * 1. there are no pending requests(e.g. during init stage)
659 * 2. there are some pending requests, but the controller is in
660 * reset status, and its type is not st_yosemite
661 * firmware of st_yosemite in reset status will return pending requests
662 * to driver, so we allow it to pass
663 */
664 if (unlikely(hba->out_req_cnt <= 0 ||
665 (hba->mu_status == MU_STATE_RESETTING &&
666 hba->cardtype != st_yosemite))) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400667 hba->status_tail = hba->status_head;
668 goto update_status;
669 }
670
671 while (hba->status_tail != hba->status_head) {
672 resp = stex_get_status(hba);
673 tag = le16_to_cpu(resp->tag);
Ed Lincf355882006-09-01 14:31:51 +0800674 if (unlikely(tag >= hba->host->can_queue)) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400675 printk(KERN_WARNING DRV_NAME
676 "(%s): invalid tag\n", pci_name(hba->pdev));
677 continue;
678 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400679
Ed Linf1498162009-03-31 17:30:19 -0800680 hba->out_req_cnt--;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400681 ccb = &hba->ccb[tag];
Ed Linf1498162009-03-31 17:30:19 -0800682 if (unlikely(hba->wait_ccb == ccb))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400683 hba->wait_ccb = NULL;
684 if (unlikely(ccb->req == NULL)) {
685 printk(KERN_WARNING DRV_NAME
686 "(%s): lagging req\n", pci_name(hba->pdev));
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400687 continue;
688 }
689
690 size = resp->payload_sz * sizeof(u32); /* payload size */
691 if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
692 size > sizeof(*resp))) {
693 printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
694 pci_name(hba->pdev));
695 } else {
696 size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
697 if (size)
698 stex_copy_data(ccb, resp, size);
699 }
700
Ed Lin - PTUdd48ebf2009-01-26 02:40:11 -0800701 ccb->req = NULL;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400702 ccb->srb_status = resp->srb_status;
703 ccb->scsi_status = resp->scsi_status;
704
Ed Lincf355882006-09-01 14:31:51 +0800705 if (likely(ccb->cmd != NULL)) {
Ed Linfb4f66b2006-09-27 19:23:41 +0800706 if (hba->cardtype == st_yosemite)
Ed Linf1498162009-03-31 17:30:19 -0800707 stex_check_cmd(hba, ccb, resp);
Ed Linfb4f66b2006-09-27 19:23:41 +0800708
Ed Lincf355882006-09-01 14:31:51 +0800709 if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
710 ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
711 stex_controller_info(hba, ccb);
Ed Linfb4f66b2006-09-27 19:23:41 +0800712
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900713 scsi_dma_unmap(ccb->cmd);
Ed Lincf355882006-09-01 14:31:51 +0800714 stex_scsi_done(ccb);
Ed Linf1498162009-03-31 17:30:19 -0800715 } else
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400716 ccb->req_type = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400717 }
718
719update_status:
720 writel(hba->status_head, base + IMR1);
721 readl(base + IMR1); /* flush */
722}
723
David Howells7d12e782006-10-05 14:55:46 +0100724static irqreturn_t stex_intr(int irq, void *__hba)
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400725{
726 struct st_hba *hba = __hba;
727 void __iomem *base = hba->mmio_base;
728 u32 data;
729 unsigned long flags;
730 int handled = 0;
731
732 spin_lock_irqsave(hba->host->host_lock, flags);
733
734 data = readl(base + ODBL);
735
736 if (data && data != 0xffffffff) {
737 /* clear the interrupt */
738 writel(data, base + ODBL);
739 readl(base + ODBL); /* flush */
740 stex_mu_intr(hba, data);
741 handled = 1;
742 }
743
744 spin_unlock_irqrestore(hba->host->host_lock, flags);
745
746 return IRQ_RETVAL(handled);
747}
748
749static int stex_handshake(struct st_hba *hba)
750{
751 void __iomem *base = hba->mmio_base;
752 struct handshake_frame *h;
753 dma_addr_t status_phys;
Ed Lin529e7a62006-12-04 17:49:34 -0800754 u32 data;
Ed Lin76fbf962006-12-04 17:49:42 -0800755 unsigned long before;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400756
757 if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
758 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
759 readl(base + IDBL);
Ed Lin76fbf962006-12-04 17:49:42 -0800760 before = jiffies;
761 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
762 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
763 printk(KERN_ERR DRV_NAME
764 "(%s): no handshake signature\n",
765 pci_name(hba->pdev));
766 return -1;
767 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400768 rmb();
769 msleep(1);
770 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400771 }
772
773 udelay(10);
774
Ed Lin529e7a62006-12-04 17:49:34 -0800775 data = readl(base + OMR1);
776 if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
777 data &= 0x0000ffff;
Ed Linf1498162009-03-31 17:30:19 -0800778 if (hba->host->can_queue > data) {
Ed Lin529e7a62006-12-04 17:49:34 -0800779 hba->host->can_queue = data;
Ed Linf1498162009-03-31 17:30:19 -0800780 hba->host->cmd_per_lun = data;
781 }
Ed Lin529e7a62006-12-04 17:49:34 -0800782 }
783
Ed Linf1498162009-03-31 17:30:19 -0800784 h = (struct handshake_frame *)hba->status_buffer;
785 h->rb_phy = cpu_to_le64(hba->dma_handle);
Ed Lin591a3a52009-03-31 17:30:31 -0800786 h->req_sz = cpu_to_le16(hba->rq_size);
787 h->req_cnt = cpu_to_le16(hba->rq_count+1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400788 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
Ed Lin591a3a52009-03-31 17:30:31 -0800789 h->status_cnt = cpu_to_le16(hba->sts_count+1);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400790 stex_gettime(&h->hosttime);
791 h->partner_type = HMU_PARTNER_TYPE;
Ed Lin591a3a52009-03-31 17:30:31 -0800792 if (hba->extra_offset) {
793 h->extra_offset = cpu_to_le32(hba->extra_offset);
Ed Lin94e91082006-12-04 17:49:39 -0800794 h->extra_size = cpu_to_le32(ST_ADDITIONAL_MEM);
795 } else
796 h->extra_offset = h->extra_size = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400797
Ed Lin591a3a52009-03-31 17:30:31 -0800798 status_phys = hba->dma_handle + (hba->rq_count+1) * hba->rq_size;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400799 writel(status_phys, base + IMR0);
800 readl(base + IMR0);
801 writel((status_phys >> 16) >> 16, base + IMR1);
802 readl(base + IMR1);
803
804 writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
805 readl(base + OMR0);
806 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
807 readl(base + IDBL); /* flush */
808
809 udelay(10);
Ed Lin76fbf962006-12-04 17:49:42 -0800810 before = jiffies;
811 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
812 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
813 printk(KERN_ERR DRV_NAME
814 "(%s): no signature after handshake frame\n",
815 pci_name(hba->pdev));
816 return -1;
817 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400818 rmb();
819 msleep(1);
820 }
821
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400822 writel(0, base + IMR0);
823 readl(base + IMR0);
824 writel(0, base + OMR0);
825 readl(base + OMR0);
826 writel(0, base + IMR1);
827 readl(base + IMR1);
828 writel(0, base + OMR1);
829 readl(base + OMR1); /* flush */
830 hba->mu_status = MU_STATE_STARTED;
831 return 0;
832}
833
834static int stex_abort(struct scsi_cmnd *cmd)
835{
836 struct Scsi_Host *host = cmd->device->host;
837 struct st_hba *hba = (struct st_hba *)host->hostdata;
Ed Lincf355882006-09-01 14:31:51 +0800838 u16 tag = cmd->request->tag;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400839 void __iomem *base;
840 u32 data;
841 int result = SUCCESS;
842 unsigned long flags;
Ed Linc25da0a2007-05-09 20:50:42 -0800843
844 printk(KERN_INFO DRV_NAME
845 "(%s): aborting command\n", pci_name(hba->pdev));
846 scsi_print_command(cmd);
847
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400848 base = hba->mmio_base;
849 spin_lock_irqsave(host->host_lock, flags);
Ed Lincf355882006-09-01 14:31:51 +0800850 if (tag < host->can_queue && hba->ccb[tag].cmd == cmd)
851 hba->wait_ccb = &hba->ccb[tag];
852 else {
853 for (tag = 0; tag < host->can_queue; tag++)
854 if (hba->ccb[tag].cmd == cmd) {
855 hba->wait_ccb = &hba->ccb[tag];
856 break;
857 }
858 if (tag >= host->can_queue)
859 goto out;
860 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400861
862 data = readl(base + ODBL);
863 if (data == 0 || data == 0xffffffff)
864 goto fail_out;
865
866 writel(data, base + ODBL);
867 readl(base + ODBL); /* flush */
868
869 stex_mu_intr(hba, data);
870
871 if (hba->wait_ccb == NULL) {
872 printk(KERN_WARNING DRV_NAME
873 "(%s): lost interrupt\n", pci_name(hba->pdev));
874 goto out;
875 }
876
877fail_out:
FUJITA Tomonorid5587d52007-05-26 10:01:24 +0900878 scsi_dma_unmap(cmd);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400879 hba->wait_ccb->req = NULL; /* nullify the req's future return */
880 hba->wait_ccb = NULL;
881 result = FAILED;
882out:
883 spin_unlock_irqrestore(host->host_lock, flags);
884 return result;
885}
886
887static void stex_hard_reset(struct st_hba *hba)
888{
889 struct pci_bus *bus;
890 int i;
891 u16 pci_cmd;
892 u8 pci_bctl;
893
894 for (i = 0; i < 16; i++)
895 pci_read_config_dword(hba->pdev, i * 4,
896 &hba->pdev->saved_config_space[i]);
897
898 /* Reset secondary bus. Our controller(MU/ATU) is the only device on
899 secondary bus. Consult Intel 80331/3 developer's manual for detail */
900 bus = hba->pdev->bus;
901 pci_read_config_byte(bus->self, PCI_BRIDGE_CONTROL, &pci_bctl);
902 pci_bctl |= PCI_BRIDGE_CTL_BUS_RESET;
903 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
Ed Lin69f4a512007-05-09 20:50:37 -0800904
905 /*
906 * 1 ms may be enough for 8-port controllers. But 16-port controllers
907 * require more time to finish bus reset. Use 100 ms here for safety
908 */
909 msleep(100);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400910 pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
911 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
912
Ed Lin76fbf962006-12-04 17:49:42 -0800913 for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400914 pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
Ed Lin47c4f992006-12-04 17:49:31 -0800915 if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400916 break;
917 msleep(1);
918 }
919
920 ssleep(5);
921 for (i = 0; i < 16; i++)
922 pci_write_config_dword(hba->pdev, i * 4,
923 hba->pdev->saved_config_space[i]);
924}
925
926static int stex_reset(struct scsi_cmnd *cmd)
927{
928 struct st_hba *hba;
Ed Linf1498162009-03-31 17:30:19 -0800929 void __iomem *base;
930 unsigned long flags, before;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -0800931
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400932 hba = (struct st_hba *) &cmd->device->host->hostdata[0];
933
Ed Linc25da0a2007-05-09 20:50:42 -0800934 printk(KERN_INFO DRV_NAME
935 "(%s): resetting host\n", pci_name(hba->pdev));
936 scsi_print_command(cmd);
937
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400938 hba->mu_status = MU_STATE_RESETTING;
939
940 if (hba->cardtype == st_shasta)
941 stex_hard_reset(hba);
942
Ed Linfb4f66b2006-09-27 19:23:41 +0800943 if (hba->cardtype != st_yosemite) {
944 if (stex_handshake(hba)) {
945 printk(KERN_WARNING DRV_NAME
946 "(%s): resetting: handshake failed\n",
947 pci_name(hba->pdev));
948 return FAILED;
949 }
950 spin_lock_irqsave(hba->host->host_lock, flags);
951 hba->req_head = 0;
952 hba->req_tail = 0;
953 hba->status_head = 0;
954 hba->status_tail = 0;
955 hba->out_req_cnt = 0;
956 spin_unlock_irqrestore(hba->host->host_lock, flags);
957 return SUCCESS;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400958 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400959
Ed Linfb4f66b2006-09-27 19:23:41 +0800960 /* st_yosemite */
961 writel(MU_INBOUND_DOORBELL_RESET, hba->mmio_base + IDBL);
962 readl(hba->mmio_base + IDBL); /* flush */
963 before = jiffies;
964 while (hba->out_req_cnt > 0) {
965 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
966 printk(KERN_WARNING DRV_NAME
967 "(%s): reset timeout\n", pci_name(hba->pdev));
968 return FAILED;
969 }
970 msleep(1);
971 }
972
Ed Linf1498162009-03-31 17:30:19 -0800973 base = hba->mmio_base;
974 writel(0, base + IMR0);
975 readl(base + IMR0);
976 writel(0, base + OMR0);
977 readl(base + OMR0);
978 writel(0, base + IMR1);
979 readl(base + IMR1);
980 writel(0, base + OMR1);
981 readl(base + OMR1); /* flush */
982 spin_lock_irqsave(hba->host->host_lock, flags);
983 hba->req_head = 0;
984 hba->req_tail = 0;
985 hba->status_head = 0;
986 hba->status_tail = 0;
987 hba->out_req_cnt = 0;
Ed Linfb4f66b2006-09-27 19:23:41 +0800988 hba->mu_status = MU_STATE_STARTED;
Ed Linf1498162009-03-31 17:30:19 -0800989 spin_unlock_irqrestore(hba->host->host_lock, flags);
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400990 return SUCCESS;
991}
992
993static int stex_biosparam(struct scsi_device *sdev,
994 struct block_device *bdev, sector_t capacity, int geom[])
995{
Ed Linb4b8bed2006-12-04 17:49:24 -0800996 int heads = 255, sectors = 63;
Jeff Garzik5a25ba12006-09-01 03:12:19 -0400997
998 if (capacity < 0x200000) {
999 heads = 64;
1000 sectors = 32;
1001 }
1002
Ed Linb4b8bed2006-12-04 17:49:24 -08001003 sector_div(capacity, heads * sectors);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001004
1005 geom[0] = heads;
1006 geom[1] = sectors;
Ed Linb4b8bed2006-12-04 17:49:24 -08001007 geom[2] = capacity;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001008
1009 return 0;
1010}
1011
1012static struct scsi_host_template driver_template = {
1013 .module = THIS_MODULE,
1014 .name = DRV_NAME,
1015 .proc_name = DRV_NAME,
1016 .bios_param = stex_biosparam,
1017 .queuecommand = stex_queuecommand,
Ed Lincf355882006-09-01 14:31:51 +08001018 .slave_alloc = stex_slave_alloc,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001019 .slave_configure = stex_slave_config,
1020 .slave_destroy = stex_slave_destroy,
1021 .eh_abort_handler = stex_abort,
1022 .eh_host_reset_handler = stex_reset,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001023 .this_id = -1,
1024 .sg_tablesize = ST_MAX_SG,
Ed Lin591a3a52009-03-31 17:30:31 -08001025};
1026
1027static struct pci_device_id stex_pci_tbl[] = {
1028 /* st_shasta */
1029 { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1030 st_shasta }, /* SuperTrak EX8350/8300/16350/16300 */
1031 { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1032 st_shasta }, /* SuperTrak EX12350 */
1033 { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1034 st_shasta }, /* SuperTrak EX4350 */
1035 { 0x105a, 0xe350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1036 st_shasta }, /* SuperTrak EX24350 */
1037
1038 /* st_vsc */
1039 { 0x105a, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
1040
1041 /* st_yosemite */
1042 { 0x105a, 0x8650, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_yosemite },
1043
1044 /* st_seq */
1045 { 0x105a, 0x3360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_seq },
1046 { } /* terminate list */
1047};
1048
1049static struct st_card_info stex_card_info[] = {
1050 /* st_shasta */
1051 {
1052 .max_id = 17,
1053 .max_lun = 8,
1054 .max_channel = 0,
1055 .rq_count = 32,
1056 .rq_size = 1048,
1057 .sts_count = 32,
1058 },
1059
1060 /* st_vsc */
1061 {
1062 .max_id = 129,
1063 .max_lun = 1,
1064 .max_channel = 0,
1065 .rq_count = 32,
1066 .rq_size = 1048,
1067 .sts_count = 32,
1068 },
1069
1070 /* st_yosemite */
1071 {
1072 .max_id = 2,
1073 .max_lun = 256,
1074 .max_channel = 0,
1075 .rq_count = 256,
1076 .rq_size = 1048,
1077 .sts_count = 256,
1078 },
1079
1080 /* st_seq */
1081 {
1082 .max_id = 129,
1083 .max_lun = 1,
1084 .max_channel = 0,
1085 .rq_count = 32,
1086 .rq_size = 1048,
1087 .sts_count = 32,
1088 },
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001089};
1090
1091static int stex_set_dma_mask(struct pci_dev * pdev)
1092{
1093 int ret;
Ed Lin - PTU7cfe99a2009-01-26 02:41:53 -08001094
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001095 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)
1096 && !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
1097 return 0;
1098 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1099 if (!ret)
1100 ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1101 return ret;
1102}
1103
Ed Lin99946f82009-03-31 17:30:25 -08001104static int stex_request_irq(struct st_hba *hba)
1105{
1106 struct pci_dev *pdev = hba->pdev;
1107 int status;
1108
1109 if (msi) {
1110 status = pci_enable_msi(pdev);
1111 if (status != 0)
1112 printk(KERN_ERR DRV_NAME
1113 "(%s): error %d setting up MSI\n",
1114 pci_name(pdev), status);
1115 else
1116 hba->msi_enabled = 1;
1117 } else
1118 hba->msi_enabled = 0;
1119
1120 status = request_irq(pdev->irq, stex_intr, IRQF_SHARED, DRV_NAME, hba);
1121
1122 if (status != 0) {
1123 if (hba->msi_enabled)
1124 pci_disable_msi(pdev);
1125 }
1126 return status;
1127}
1128
1129static void stex_free_irq(struct st_hba *hba)
1130{
1131 struct pci_dev *pdev = hba->pdev;
1132
1133 free_irq(pdev->irq, hba);
1134 if (hba->msi_enabled)
1135 pci_disable_msi(pdev);
1136}
1137
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001138static int __devinit
1139stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1140{
1141 struct st_hba *hba;
1142 struct Scsi_Host *host;
Ed Lin591a3a52009-03-31 17:30:31 -08001143 const struct st_card_info *ci = NULL;
1144 u32 sts_offset, cp_offset;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001145 int err;
1146
1147 err = pci_enable_device(pdev);
1148 if (err)
1149 return err;
1150
1151 pci_set_master(pdev);
1152
1153 host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
1154
1155 if (!host) {
1156 printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
1157 pci_name(pdev));
1158 err = -ENOMEM;
1159 goto out_disable;
1160 }
1161
1162 hba = (struct st_hba *)host->hostdata;
1163 memset(hba, 0, sizeof(struct st_hba));
1164
1165 err = pci_request_regions(pdev, DRV_NAME);
1166 if (err < 0) {
1167 printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
1168 pci_name(pdev));
1169 goto out_scsi_host_put;
1170 }
1171
Arjan van de Ven25729a72008-09-28 16:18:02 -07001172 hba->mmio_base = pci_ioremap_bar(pdev, 0);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001173 if ( !hba->mmio_base) {
1174 printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
1175 pci_name(pdev));
1176 err = -ENOMEM;
1177 goto out_release_regions;
1178 }
1179
1180 err = stex_set_dma_mask(pdev);
1181 if (err) {
1182 printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
1183 pci_name(pdev));
1184 goto out_iounmap;
1185 }
1186
Ed Lin94e91082006-12-04 17:49:39 -08001187 hba->cardtype = (unsigned int) id->driver_data;
Ed Lin591a3a52009-03-31 17:30:31 -08001188 ci = &stex_card_info[hba->cardtype];
1189 sts_offset = (ci->rq_count+1) * ci->rq_size;
1190 cp_offset = sts_offset + (ci->sts_count+1) * sizeof(struct status_msg);
1191 hba->dma_size = cp_offset + sizeof(struct st_frame);
1192 if (hba->cardtype == st_seq ||
1193 (hba->cardtype == st_vsc && (pdev->subsystem_device & 1))) {
1194 hba->extra_offset = hba->dma_size;
1195 hba->dma_size += ST_ADDITIONAL_MEM;
1196 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001197 hba->dma_mem = dma_alloc_coherent(&pdev->dev,
Ed Lin94e91082006-12-04 17:49:39 -08001198 hba->dma_size, &hba->dma_handle, GFP_KERNEL);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001199 if (!hba->dma_mem) {
1200 err = -ENOMEM;
1201 printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
1202 pci_name(pdev));
1203 goto out_iounmap;
1204 }
1205
Ed Lin591a3a52009-03-31 17:30:31 -08001206 hba->ccb = kcalloc(ci->rq_count, sizeof(struct st_ccb), GFP_KERNEL);
1207 if (!hba->ccb) {
1208 err = -ENOMEM;
1209 printk(KERN_ERR DRV_NAME "(%s): ccb alloc failed\n",
1210 pci_name(pdev));
1211 goto out_pci_free;
1212 }
1213
1214 hba->status_buffer = (struct status_msg *)(hba->dma_mem + sts_offset);
1215 hba->copy_buffer = hba->dma_mem + cp_offset;
1216 hba->rq_count = ci->rq_count;
1217 hba->rq_size = ci->rq_size;
1218 hba->sts_count = ci->sts_count;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001219 hba->mu_status = MU_STATE_STARTING;
1220
Ed Lin591a3a52009-03-31 17:30:31 -08001221 host->can_queue = ci->rq_count;
1222 host->cmd_per_lun = ci->rq_count;
1223 host->max_id = ci->max_id;
1224 host->max_lun = ci->max_lun;
1225 host->max_channel = ci->max_channel;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001226 host->unique_id = host->host_no;
1227 host->max_cmd_len = STEX_CDB_LENGTH;
1228
1229 hba->host = host;
1230 hba->pdev = pdev;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001231
Ed Lin99946f82009-03-31 17:30:25 -08001232 err = stex_request_irq(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001233 if (err) {
1234 printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
1235 pci_name(pdev));
Ed Lin591a3a52009-03-31 17:30:31 -08001236 goto out_ccb_free;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001237 }
1238
1239 err = stex_handshake(hba);
1240 if (err)
1241 goto out_free_irq;
1242
Ed Lin529e7a62006-12-04 17:49:34 -08001243 err = scsi_init_shared_tag_map(host, host->can_queue);
James Bottomleydeb81d82006-09-01 09:28:48 -04001244 if (err) {
Ed Lincf355882006-09-01 14:31:51 +08001245 printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
1246 pci_name(pdev));
1247 goto out_free_irq;
1248 }
1249
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001250 pci_set_drvdata(pdev, hba);
1251
1252 err = scsi_add_host(host, &pdev->dev);
1253 if (err) {
1254 printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
1255 pci_name(pdev));
1256 goto out_free_irq;
1257 }
1258
1259 scsi_scan_host(host);
1260
1261 return 0;
1262
1263out_free_irq:
Ed Lin99946f82009-03-31 17:30:25 -08001264 stex_free_irq(hba);
Ed Lin591a3a52009-03-31 17:30:31 -08001265out_ccb_free:
1266 kfree(hba->ccb);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001267out_pci_free:
Ed Lin94e91082006-12-04 17:49:39 -08001268 dma_free_coherent(&pdev->dev, hba->dma_size,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001269 hba->dma_mem, hba->dma_handle);
1270out_iounmap:
1271 iounmap(hba->mmio_base);
1272out_release_regions:
1273 pci_release_regions(pdev);
1274out_scsi_host_put:
1275 scsi_host_put(host);
1276out_disable:
1277 pci_disable_device(pdev);
1278
1279 return err;
1280}
1281
1282static void stex_hba_stop(struct st_hba *hba)
1283{
1284 struct req_msg *req;
1285 unsigned long flags;
1286 unsigned long before;
Ed Lincf355882006-09-01 14:31:51 +08001287 u16 tag = 0;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001288
1289 spin_lock_irqsave(hba->host->host_lock, flags);
1290 req = stex_alloc_req(hba);
1291 memset(req->cdb, 0, STEX_CDB_LENGTH);
1292
Ed Linfb4f66b2006-09-27 19:23:41 +08001293 if (hba->cardtype == st_yosemite) {
1294 req->cdb[0] = MGT_CMD;
1295 req->cdb[1] = MGT_CMD_SIGNATURE;
1296 req->cdb[2] = CTLR_CONFIG_CMD;
1297 req->cdb[3] = CTLR_SHUTDOWN;
1298 } else {
1299 req->cdb[0] = CONTROLLER_CMD;
1300 req->cdb[1] = CTLR_POWER_STATE_CHANGE;
1301 req->cdb[2] = CTLR_POWER_SAVING;
1302 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001303
1304 hba->ccb[tag].cmd = NULL;
1305 hba->ccb[tag].sg_count = 0;
1306 hba->ccb[tag].sense_bufflen = 0;
1307 hba->ccb[tag].sense_buffer = NULL;
Ed Linf1498162009-03-31 17:30:19 -08001308 hba->ccb[tag].req_type = PASSTHRU_REQ_TYPE;
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001309
1310 stex_send_cmd(hba, req, tag);
1311 spin_unlock_irqrestore(hba->host->host_lock, flags);
1312
Ed Lincf355882006-09-01 14:31:51 +08001313 before = jiffies;
1314 while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
Ed Linf1498162009-03-31 17:30:19 -08001315 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1316 hba->ccb[tag].req_type = 0;
Ed Lincf355882006-09-01 14:31:51 +08001317 return;
Ed Linf1498162009-03-31 17:30:19 -08001318 }
1319 msleep(1);
Ed Lincf355882006-09-01 14:31:51 +08001320 }
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001321}
1322
1323static void stex_hba_free(struct st_hba *hba)
1324{
Ed Lin99946f82009-03-31 17:30:25 -08001325 stex_free_irq(hba);
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001326
1327 iounmap(hba->mmio_base);
1328
1329 pci_release_regions(hba->pdev);
1330
Ed Lin591a3a52009-03-31 17:30:31 -08001331 kfree(hba->ccb);
1332
Ed Lin94e91082006-12-04 17:49:39 -08001333 dma_free_coherent(&hba->pdev->dev, hba->dma_size,
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001334 hba->dma_mem, hba->dma_handle);
1335}
1336
1337static void stex_remove(struct pci_dev *pdev)
1338{
1339 struct st_hba *hba = pci_get_drvdata(pdev);
1340
1341 scsi_remove_host(hba->host);
1342
1343 pci_set_drvdata(pdev, NULL);
1344
1345 stex_hba_stop(hba);
1346
1347 stex_hba_free(hba);
1348
1349 scsi_host_put(hba->host);
1350
1351 pci_disable_device(pdev);
1352}
1353
1354static void stex_shutdown(struct pci_dev *pdev)
1355{
1356 struct st_hba *hba = pci_get_drvdata(pdev);
1357
1358 stex_hba_stop(hba);
1359}
1360
Jeff Garzik5a25ba12006-09-01 03:12:19 -04001361MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
1362
1363static struct pci_driver stex_pci_driver = {
1364 .name = DRV_NAME,
1365 .id_table = stex_pci_tbl,
1366 .probe = stex_probe,
1367 .remove = __devexit_p(stex_remove),
1368 .shutdown = stex_shutdown,
1369};
1370
1371static int __init stex_init(void)
1372{
1373 printk(KERN_INFO DRV_NAME
1374 ": Promise SuperTrak EX Driver version: %s\n",
1375 ST_DRIVER_VERSION);
1376
1377 return pci_register_driver(&stex_pci_driver);
1378}
1379
1380static void __exit stex_exit(void)
1381{
1382 pci_unregister_driver(&stex_pci_driver);
1383}
1384
1385module_init(stex_init);
1386module_exit(stex_exit);