]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/stex.c
[SCSI] stex: use config struct for parameters of different controllers
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / stex.c
CommitLineData
5a25ba16
JG
1/*
2 * SuperTrak EX Series Storage Controller driver for Linux
3 *
bd5cd9cd 4 * Copyright (C) 2005-2009 Promise Technology Inc.
5a25ba16
JG
5 *
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 *
5a25ba16
JG
14 */
15
16#include <linux/init.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/delay.h>
5a25ba16
JG
20#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>
cf355883 34#include <scsi/scsi_tcq.h>
c25da0af 35#include <scsi/scsi_dbg.h>
11002fbc 36#include <scsi/scsi_eh.h>
5a25ba16
JG
37
38#define DRV_NAME "stex"
bd5cd9cd
ELP
39#define ST_DRIVER_VERSION "4.6.0000.1"
40#define ST_VER_MAJOR 4
c25da0af 41#define ST_VER_MINOR 6
5a25ba16 42#define ST_OEM 0
fb4f66be 43#define ST_BUILD_VER 1
5a25ba16
JG
44
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
76fbf96f 78 MU_MAX_DELAY = 120,
5a25ba16 79 MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
529e7a62 80 MU_HANDSHAKE_SIGNATURE_HALF = 0x5a5a0000,
76fbf96f 81 MU_HARD_RESET_WAIT = 30000,
5a25ba16
JG
82 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
7cfe99a5 98 STEX_CDB_LENGTH = 16,
5a25ba16 99 STATUS_VAR_LEN = 128,
5a25ba16
JG
100 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 */
7cfe99a5
ELP
106 MSG_DATA_DIR_ND = 0,
107 MSG_DATA_DIR_IN = 1,
108 MSG_DATA_DIR_OUT = 2,
5a25ba16 109
5a25ba16
JG
110 st_shasta = 0,
111 st_vsc = 1,
591a3a5f
EL
112 st_yosemite = 2,
113 st_seq = 3,
5a25ba16
JG
114
115 PASSTHRU_REQ_TYPE = 0x00000001,
116 PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
7cfe99a5 117 ST_INTERNAL_TIMEOUT = 180,
5a25ba16 118
fb4f66be
EL
119 ST_TO_CMD = 0,
120 ST_FROM_CMD = 1,
121
5a25ba16 122 /* vendor specific commands of Promise */
fb4f66be
EL
123 MGT_CMD = 0xd8,
124 SINBAND_MGT_CMD = 0xd9,
5a25ba16
JG
125 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,
fb4f66be
EL
132
133 CTLR_CONFIG_CMD = 0x03,
134 CTLR_SHUTDOWN = 0x0d,
135
5a25ba16
JG
136 CTLR_POWER_STATE_CHANGE = 0x0e,
137 CTLR_POWER_SAVING = 0x01,
138
139 PASSTHRU_SIGNATURE = 0x4e415041,
fb4f66be 140 MGT_CMD_SIGNATURE = 0xba,
5a25ba16
JG
141
142 INQUIRY_EVPD = 0x01,
94e9108b
EL
143
144 ST_ADDITIONAL_MEM = 0x200000,
5a25ba16
JG
145};
146
147struct st_sgitem {
148 u8 ctrl; /* SG_CF_xxx */
149 u8 reserved[3];
150 __le32 count;
f1498161 151 __le64 addr;
5a25ba16
JG
152};
153
154struct st_sgtable {
155 __le16 sg_count;
156 __le16 max_sg_count;
157 __le32 sz_in_byte;
5a25ba16
JG
158};
159
160struct handshake_frame {
f1498161 161 __le64 rb_phy; /* request payload queue physical address */
5a25ba16
JG
162 __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 */
f1498161 166 __le64 hosttime; /* seconds from Jan 1, 1970 (GMT) */
5a25ba16
JG
167 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;
94e9108b
EL
173 __le32 extra_offset; /* NEW */
174 __le32 extra_size; /* NEW */
175 u32 reserved1[2];
5a25ba16
JG
176};
177
178struct req_msg {
179 __le16 tag;
180 u8 lun;
181 u8 target;
182 u8 task_attr;
183 u8 task_manage;
7cfe99a5 184 u8 data_dir;
f903d7b7 185 u8 payload_sz; /* payload size in 4-byte, not used */
5a25ba16 186 u8 cdb[STEX_CDB_LENGTH];
591a3a5f 187 u32 variable[0];
5a25ba16
JG
188};
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
5a25ba16
JG
243struct 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;
f1498161 254 u8 reserved[2];
5a25ba16
JG
255};
256
257struct st_hba {
258 void __iomem *mmio_base; /* iomapped PCI memory space */
259 void *dma_mem;
260 dma_addr_t dma_handle;
94e9108b 261 size_t dma_size;
5a25ba16
JG
262
263 struct Scsi_Host *host;
264 struct pci_dev *pdev;
265
5a25ba16
JG
266 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 */
591a3a5f 273 struct st_ccb *ccb;
5a25ba16 274 struct st_ccb *wait_ccb;
5a25ba16
JG
275
276 unsigned int mu_status;
5a25ba16 277 unsigned int cardtype;
99946f81
EL
278 int msi_enabled;
279 int out_req_cnt;
591a3a5f
EL
280 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;
5a25ba16
JG
293};
294
99946f81
EL
295static int msi;
296module_param(msi, int, 0);
297MODULE_PARM_DESC(msi, "Enable Message Signaled Interrupts(0=off, 1=on)");
298
5a25ba16
JG
299static 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
f1498161 316static void stex_gettime(__le64 *time)
5a25ba16
JG
317{
318 struct timeval tv;
5a25ba16 319
7cfe99a5 320 do_gettimeofday(&tv);
f1498161 321 *time = cpu_to_le64(tv.tv_sec);
5a25ba16
JG
322}
323
5a25ba16
JG
324static struct status_msg *stex_get_status(struct st_hba *hba)
325{
f1498161 326 struct status_msg *status = hba->status_buffer + hba->status_tail;
5a25ba16
JG
327
328 ++hba->status_tail;
591a3a5f 329 hba->status_tail %= hba->sts_count+1;
5a25ba16
JG
330
331 return status;
332}
333
5a25ba16
JG
334static void stex_invalid_field(struct scsi_cmnd *cmd,
335 void (*done)(struct scsi_cmnd *))
336{
11002fbc
FT
337 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
338
7cfe99a5 339 /* "Invalid field in cdb" */
11002fbc
FT
340 scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
341 0x0);
5a25ba16
JG
342 done(cmd);
343}
344
345static struct req_msg *stex_alloc_req(struct st_hba *hba)
346{
591a3a5f 347 struct req_msg *req = hba->dma_mem + hba->req_head * hba->rq_size;
5a25ba16
JG
348
349 ++hba->req_head;
591a3a5f 350 hba->req_head %= hba->rq_count+1;
5a25ba16
JG
351
352 return req;
353}
354
355static int stex_map_sg(struct st_hba *hba,
356 struct req_msg *req, struct st_ccb *ccb)
357{
5a25ba16 358 struct scsi_cmnd *cmd;
d5587d5d 359 struct scatterlist *sg;
5a25ba16 360 struct st_sgtable *dst;
f1498161 361 struct st_sgitem *table;
d5587d5d 362 int i, nseg;
5a25ba16
JG
363
364 cmd = ccb->cmd;
d5587d5d 365 nseg = scsi_dma_map(cmd);
f1498161 366 BUG_ON(nseg < 0);
d5587d5d 367 if (nseg) {
f1498161
EL
368 dst = (struct st_sgtable *)req->variable;
369
d5587d5d
FT
370 ccb->sg_count = nseg;
371 dst->sg_count = cpu_to_le16((u16)nseg);
f1498161
EL
372 dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
373 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
5a25ba16 374
f1498161 375 table = (struct st_sgitem *)(dst + 1);
d5587d5d 376 scsi_for_each_sg(cmd, sg, nseg, i) {
f1498161
EL
377 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;
5a25ba16 380 }
f1498161 381 table[--i].ctrl |= SG_CF_EOT;
5a25ba16
JG
382 }
383
f1498161 384 return nseg;
5a25ba16
JG
385}
386
5a25ba16
JG
387static 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;
f1498161 393 scsi_sg_copy_to_buffer(ccb->cmd, p, count);
5a25ba16
JG
394 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
f1498161 411 scsi_sg_copy_from_buffer(ccb->cmd, p, count);
5a25ba16
JG
412}
413
414static void
415stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
416{
417 req->tag = cpu_to_le16(tag);
5a25ba16
JG
418
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
cf355883
EL
427static int
428stex_slave_alloc(struct scsi_device *sdev)
429{
430 /* Cheat: usually extracted from Inquiry data */
431 sdev->tagged_supported = 1;
432
f1498161 433 scsi_activate_tcq(sdev, sdev->host->can_queue);
cf355883
EL
434
435 return 0;
436}
437
5a25ba16
JG
438static int
439stex_slave_config(struct scsi_device *sdev)
440{
441 sdev->use_10_for_rw = 1;
442 sdev->use_10_for_ms = 1;
dc5c49bf 443 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
cf355883
EL
444 sdev->tagged_supported = 1;
445
5a25ba16
JG
446 return 0;
447}
448
449static void
450stex_slave_destroy(struct scsi_device *sdev)
451{
cf355883 452 scsi_deactivate_tcq(sdev, 1);
5a25ba16
JG
453}
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;
f1498161 460 unsigned int id, lun;
5a25ba16
JG
461 struct req_msg *req;
462 u16 tag;
7cfe99a5 463
5a25ba16
JG
464 host = cmd->device->host;
465 id = cmd->device->id;
e0b2e597 466 lun = cmd->device->lun;
5a25ba16
JG
467 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;
7cfe99a5 475
5a25ba16
JG
476 page = cmd->cmnd[2] & 0x3f;
477 if (page == 0x8 || page == 0x3f) {
31fe47d4
FT
478 scsi_sg_copy_from_buffer(cmd, ms10_caching_page,
479 sizeof(ms10_caching_page));
5a25ba16
JG
480 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
481 done(cmd);
482 } else
483 stex_invalid_field(cmd, done);
484 return 0;
485 }
e0b2e597
EL
486 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;
d116a7bc
EL
497 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;
5a25ba16 504 case INQUIRY:
e0b2e597 505 if (id != host->max_id - 1)
5a25ba16
JG
506 break;
507 if (lun == 0 && (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
31fe47d4
FT
508 scsi_sg_copy_from_buffer(cmd, (void *)console_inq_page,
509 sizeof(console_inq_page));
5a25ba16
JG
510 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;
26106e3c 518 size_t cp_len = sizeof(ver);
7cfe99a5 519
5a25ba16
JG
520 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;
e0b2e597 525 ver.console_id = host->max_id - 1;
5a25ba16 526 ver.host_no = hba->host->host_no;
31fe47d4 527 cp_len = scsi_sg_copy_from_buffer(cmd, &ver, cp_len);
26106e3c 528 cmd->result = sizeof(ver) == cp_len ?
5a25ba16
JG
529 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
cf355883
EL
540 tag = cmd->request->tag;
541
542 if (unlikely(tag >= host->can_queue))
5a25ba16
JG
543 return SCSI_MLQUEUE_HOST_BUSY;
544
545 req = stex_alloc_req(hba);
fb4f66be 546
e0b2e597
EL
547 req->lun = lun;
548 req->target = id;
5a25ba16
JG
549
550 /* cdb */
551 memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
552
7cfe99a5
ELP
553 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
5a25ba16
JG
560 hba->ccb[tag].cmd = cmd;
561 hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
562 hba->ccb[tag].sense_buffer = cmd->sense_buffer;
5a25ba16
JG
563
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
5a25ba16
JG
571static void stex_scsi_done(struct st_ccb *ccb)
572{
573 struct scsi_cmnd *cmd = ccb->cmd;
574 int result;
575
f1498161 576 if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
5a25ba16
JG
577 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{
5a25ba16
JG
616 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;
f1498161 625 scsi_sg_copy_from_buffer(ccb->cmd, resp->variable, variable);
fb4f66be
EL
626}
627
f1498161 628static void stex_check_cmd(struct st_hba *hba,
fb4f66be
EL
629 struct st_ccb *ccb, struct status_msg *resp)
630{
fb4f66be 631 if (ccb->cmd->cmnd[0] == MGT_CMD &&
f1498161 632 resp->scsi_status != SAM_STAT_CHECK_CONDITION)
968a5763
EL
633 scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
634 le32_to_cpu(*(__le32 *)&resp->variable[0]));
5a25ba16
JG
635}
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
f1498161 645 if (unlikely(!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED)))
5a25ba16
JG
646 return;
647
648 /* status payloads */
649 hba->status_head = readl(base + OMR1);
591a3a5f 650 if (unlikely(hba->status_head > hba->sts_count)) {
5a25ba16
JG
651 printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
652 pci_name(hba->pdev));
653 return;
654 }
655
fb4f66be
EL
656 /*
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))) {
5a25ba16
JG
667 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);
cf355883 674 if (unlikely(tag >= hba->host->can_queue)) {
5a25ba16
JG
675 printk(KERN_WARNING DRV_NAME
676 "(%s): invalid tag\n", pci_name(hba->pdev));
677 continue;
678 }
5a25ba16 679
f1498161 680 hba->out_req_cnt--;
5a25ba16 681 ccb = &hba->ccb[tag];
f1498161 682 if (unlikely(hba->wait_ccb == ccb))
5a25ba16
JG
683 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));
5a25ba16
JG
687 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
dd48ebf7 701 ccb->req = NULL;
5a25ba16
JG
702 ccb->srb_status = resp->srb_status;
703 ccb->scsi_status = resp->scsi_status;
704
cf355883 705 if (likely(ccb->cmd != NULL)) {
fb4f66be 706 if (hba->cardtype == st_yosemite)
f1498161 707 stex_check_cmd(hba, ccb, resp);
fb4f66be 708
cf355883
EL
709 if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
710 ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
711 stex_controller_info(hba, ccb);
fb4f66be 712
d5587d5d 713 scsi_dma_unmap(ccb->cmd);
cf355883 714 stex_scsi_done(ccb);
f1498161 715 } else
5a25ba16 716 ccb->req_type = 0;
5a25ba16
JG
717 }
718
719update_status:
720 writel(hba->status_head, base + IMR1);
721 readl(base + IMR1); /* flush */
722}
723
7d12e780 724static irqreturn_t stex_intr(int irq, void *__hba)
5a25ba16
JG
725{
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;
529e7a62 754 u32 data;
76fbf96f 755 unsigned long before;
5a25ba16
JG
756
757 if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
758 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
759 readl(base + IDBL);
76fbf96f
EL
760 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 }
5a25ba16
JG
768 rmb();
769 msleep(1);
770 }
5a25ba16
JG
771 }
772
773 udelay(10);
774
529e7a62
EL
775 data = readl(base + OMR1);
776 if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
777 data &= 0x0000ffff;
f1498161 778 if (hba->host->can_queue > data) {
529e7a62 779 hba->host->can_queue = data;
f1498161
EL
780 hba->host->cmd_per_lun = data;
781 }
529e7a62
EL
782 }
783
f1498161
EL
784 h = (struct handshake_frame *)hba->status_buffer;
785 h->rb_phy = cpu_to_le64(hba->dma_handle);
591a3a5f
EL
786 h->req_sz = cpu_to_le16(hba->rq_size);
787 h->req_cnt = cpu_to_le16(hba->rq_count+1);
5a25ba16 788 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
591a3a5f 789 h->status_cnt = cpu_to_le16(hba->sts_count+1);
5a25ba16
JG
790 stex_gettime(&h->hosttime);
791 h->partner_type = HMU_PARTNER_TYPE;
591a3a5f
EL
792 if (hba->extra_offset) {
793 h->extra_offset = cpu_to_le32(hba->extra_offset);
94e9108b
EL
794 h->extra_size = cpu_to_le32(ST_ADDITIONAL_MEM);
795 } else
796 h->extra_offset = h->extra_size = 0;
5a25ba16 797
591a3a5f 798 status_phys = hba->dma_handle + (hba->rq_count+1) * hba->rq_size;
5a25ba16
JG
799 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);
76fbf96f
EL
810 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 }
5a25ba16
JG
818 rmb();
819 msleep(1);
820 }
821
5a25ba16
JG
822 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;
cf355883 838 u16 tag = cmd->request->tag;
5a25ba16
JG
839 void __iomem *base;
840 u32 data;
841 int result = SUCCESS;
842 unsigned long flags;
c25da0af
EL
843
844 printk(KERN_INFO DRV_NAME
845 "(%s): aborting command\n", pci_name(hba->pdev));
846 scsi_print_command(cmd);
847
5a25ba16
JG
848 base = hba->mmio_base;
849 spin_lock_irqsave(host->host_lock, flags);
cf355883
EL
850 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 }
5a25ba16
JG
861
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:
d5587d5d 878 scsi_dma_unmap(cmd);
5a25ba16
JG
879 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);
69f4a513
EL
904
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);
5a25ba16
JG
910 pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
911 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
912
76fbf96f 913 for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
5a25ba16 914 pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
47c4f997 915 if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
5a25ba16
JG
916 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;
f1498161
EL
929 void __iomem *base;
930 unsigned long flags, before;
7cfe99a5 931
5a25ba16
JG
932 hba = (struct st_hba *) &cmd->device->host->hostdata[0];
933
c25da0af
EL
934 printk(KERN_INFO DRV_NAME
935 "(%s): resetting host\n", pci_name(hba->pdev));
936 scsi_print_command(cmd);
937
5a25ba16
JG
938 hba->mu_status = MU_STATE_RESETTING;
939
940 if (hba->cardtype == st_shasta)
941 stex_hard_reset(hba);
942
fb4f66be
EL
943 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;
5a25ba16 958 }
5a25ba16 959
fb4f66be
EL
960 /* 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
f1498161
EL
973 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;
fb4f66be 988 hba->mu_status = MU_STATE_STARTED;
f1498161 989 spin_unlock_irqrestore(hba->host->host_lock, flags);
5a25ba16
JG
990 return SUCCESS;
991}
992
993static int stex_biosparam(struct scsi_device *sdev,
994 struct block_device *bdev, sector_t capacity, int geom[])
995{
b4b8bed1 996 int heads = 255, sectors = 63;
5a25ba16
JG
997
998 if (capacity < 0x200000) {
999 heads = 64;
1000 sectors = 32;
1001 }
1002
b4b8bed1 1003 sector_div(capacity, heads * sectors);
5a25ba16
JG
1004
1005 geom[0] = heads;
1006 geom[1] = sectors;
b4b8bed1 1007 geom[2] = capacity;
5a25ba16
JG
1008
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,
cf355883 1018 .slave_alloc = stex_slave_alloc,
5a25ba16
JG
1019 .slave_configure = stex_slave_config,
1020 .slave_destroy = stex_slave_destroy,
1021 .eh_abort_handler = stex_abort,
1022 .eh_host_reset_handler = stex_reset,
5a25ba16
JG
1023 .this_id = -1,
1024 .sg_tablesize = ST_MAX_SG,
591a3a5f
EL
1025};
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 },
5a25ba16
JG
1089};
1090
1091static int stex_set_dma_mask(struct pci_dev * pdev)
1092{
1093 int ret;
7cfe99a5 1094
5a25ba16
JG
1095 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
99946f81
EL
1104static 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
5a25ba16
JG
1138static 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;
591a3a5f
EL
1143 const struct st_card_info *ci = NULL;
1144 u32 sts_offset, cp_offset;
5a25ba16
JG
1145 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
25729a7f 1172 hba->mmio_base = pci_ioremap_bar(pdev, 0);
5a25ba16
JG
1173 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
94e9108b 1187 hba->cardtype = (unsigned int) id->driver_data;
591a3a5f
EL
1188 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 }
5a25ba16 1197 hba->dma_mem = dma_alloc_coherent(&pdev->dev,
94e9108b 1198 hba->dma_size, &hba->dma_handle, GFP_KERNEL);
5a25ba16
JG
1199 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
591a3a5f
EL
1206 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;
5a25ba16
JG
1219 hba->mu_status = MU_STATE_STARTING;
1220
591a3a5f
EL
1221 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;
5a25ba16
JG
1226 host->unique_id = host->host_no;
1227 host->max_cmd_len = STEX_CDB_LENGTH;
1228
1229 hba->host = host;
1230 hba->pdev = pdev;
5a25ba16 1231
99946f81 1232 err = stex_request_irq(hba);
5a25ba16
JG
1233 if (err) {
1234 printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
1235 pci_name(pdev));
591a3a5f 1236 goto out_ccb_free;
5a25ba16
JG
1237 }
1238
1239 err = stex_handshake(hba);
1240 if (err)
1241 goto out_free_irq;
1242
529e7a62 1243 err = scsi_init_shared_tag_map(host, host->can_queue);
deb81d80 1244 if (err) {
cf355883
EL
1245 printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
1246 pci_name(pdev));
1247 goto out_free_irq;
1248 }
1249
5a25ba16
JG
1250 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:
99946f81 1264 stex_free_irq(hba);
591a3a5f
EL
1265out_ccb_free:
1266 kfree(hba->ccb);
5a25ba16 1267out_pci_free:
94e9108b 1268 dma_free_coherent(&pdev->dev, hba->dma_size,
5a25ba16
JG
1269 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;
cf355883 1287 u16 tag = 0;
5a25ba16
JG
1288
1289 spin_lock_irqsave(hba->host->host_lock, flags);
1290 req = stex_alloc_req(hba);
1291 memset(req->cdb, 0, STEX_CDB_LENGTH);
1292
fb4f66be
EL
1293 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 }
5a25ba16
JG
1303
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;
f1498161 1308 hba->ccb[tag].req_type = PASSTHRU_REQ_TYPE;
5a25ba16
JG
1309
1310 stex_send_cmd(hba, req, tag);
1311 spin_unlock_irqrestore(hba->host->host_lock, flags);
1312
cf355883
EL
1313 before = jiffies;
1314 while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
f1498161
EL
1315 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1316 hba->ccb[tag].req_type = 0;
cf355883 1317 return;
f1498161
EL
1318 }
1319 msleep(1);
cf355883 1320 }
5a25ba16
JG
1321}
1322
1323static void stex_hba_free(struct st_hba *hba)
1324{
99946f81 1325 stex_free_irq(hba);
5a25ba16
JG
1326
1327 iounmap(hba->mmio_base);
1328
1329 pci_release_regions(hba->pdev);
1330
591a3a5f
EL
1331 kfree(hba->ccb);
1332
94e9108b 1333 dma_free_coherent(&hba->pdev->dev, hba->dma_size,
5a25ba16
JG
1334 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
5a25ba16
JG
1361MODULE_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);