stex.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /*
  2. * SuperTrak EX Series Storage Controller driver for Linux
  3. *
  4. * Copyright (C) 2005-2009 Promise Technology Inc.
  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. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/delay.h>
  19. #include <linux/time.h>
  20. #include <linux/pci.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #include <asm/byteorder.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_dbg.h>
  35. #include <scsi/scsi_eh.h>
  36. #define DRV_NAME "stex"
  37. #define ST_DRIVER_VERSION "4.6.0000.1"
  38. #define ST_VER_MAJOR 4
  39. #define ST_VER_MINOR 6
  40. #define ST_OEM 0
  41. #define ST_BUILD_VER 1
  42. enum {
  43. /* MU register offset */
  44. IMR0 = 0x10, /* MU_INBOUND_MESSAGE_REG0 */
  45. IMR1 = 0x14, /* MU_INBOUND_MESSAGE_REG1 */
  46. OMR0 = 0x18, /* MU_OUTBOUND_MESSAGE_REG0 */
  47. OMR1 = 0x1c, /* MU_OUTBOUND_MESSAGE_REG1 */
  48. IDBL = 0x20, /* MU_INBOUND_DOORBELL */
  49. IIS = 0x24, /* MU_INBOUND_INTERRUPT_STATUS */
  50. IIM = 0x28, /* MU_INBOUND_INTERRUPT_MASK */
  51. ODBL = 0x2c, /* MU_OUTBOUND_DOORBELL */
  52. OIS = 0x30, /* MU_OUTBOUND_INTERRUPT_STATUS */
  53. OIM = 0x3c, /* MU_OUTBOUND_INTERRUPT_MASK */
  54. /* MU register value */
  55. MU_INBOUND_DOORBELL_HANDSHAKE = 1,
  56. MU_INBOUND_DOORBELL_REQHEADCHANGED = 2,
  57. MU_INBOUND_DOORBELL_STATUSTAILCHANGED = 4,
  58. MU_INBOUND_DOORBELL_HMUSTOPPED = 8,
  59. MU_INBOUND_DOORBELL_RESET = 16,
  60. MU_OUTBOUND_DOORBELL_HANDSHAKE = 1,
  61. MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED = 2,
  62. MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED = 4,
  63. MU_OUTBOUND_DOORBELL_BUSCHANGE = 8,
  64. MU_OUTBOUND_DOORBELL_HASEVENT = 16,
  65. /* MU status code */
  66. MU_STATE_STARTING = 1,
  67. MU_STATE_FMU_READY_FOR_HANDSHAKE = 2,
  68. MU_STATE_SEND_HANDSHAKE_FRAME = 3,
  69. MU_STATE_STARTED = 4,
  70. MU_STATE_RESETTING = 5,
  71. MU_MAX_DELAY = 120,
  72. MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
  73. MU_HANDSHAKE_SIGNATURE_HALF = 0x5a5a0000,
  74. MU_HARD_RESET_WAIT = 30000,
  75. HMU_PARTNER_TYPE = 2,
  76. /* firmware returned values */
  77. SRB_STATUS_SUCCESS = 0x01,
  78. SRB_STATUS_ERROR = 0x04,
  79. SRB_STATUS_BUSY = 0x05,
  80. SRB_STATUS_INVALID_REQUEST = 0x06,
  81. SRB_STATUS_SELECTION_TIMEOUT = 0x0A,
  82. SRB_SEE_SENSE = 0x80,
  83. /* task attribute */
  84. TASK_ATTRIBUTE_SIMPLE = 0x0,
  85. TASK_ATTRIBUTE_HEADOFQUEUE = 0x1,
  86. TASK_ATTRIBUTE_ORDERED = 0x2,
  87. TASK_ATTRIBUTE_ACA = 0x4,
  88. /* request count, etc. */
  89. MU_MAX_REQUEST = 32,
  90. /* one message wasted, use MU_MAX_REQUEST+1
  91. to handle MU_MAX_REQUEST messages */
  92. MU_REQ_COUNT = (MU_MAX_REQUEST + 1),
  93. MU_STATUS_COUNT = (MU_MAX_REQUEST + 1),
  94. STEX_CDB_LENGTH = 16,
  95. REQ_VARIABLE_LEN = 1024,
  96. STATUS_VAR_LEN = 128,
  97. ST_CAN_QUEUE = MU_MAX_REQUEST,
  98. ST_CMD_PER_LUN = MU_MAX_REQUEST,
  99. ST_MAX_SG = 32,
  100. /* sg flags */
  101. SG_CF_EOT = 0x80, /* end of table */
  102. SG_CF_64B = 0x40, /* 64 bit item */
  103. SG_CF_HOST = 0x20, /* sg in host memory */
  104. MSG_DATA_DIR_ND = 0,
  105. MSG_DATA_DIR_IN = 1,
  106. MSG_DATA_DIR_OUT = 2,
  107. st_shasta = 0,
  108. st_vsc = 1,
  109. st_vsc1 = 2,
  110. st_yosemite = 3,
  111. st_seq = 4,
  112. PASSTHRU_REQ_TYPE = 0x00000001,
  113. PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
  114. ST_INTERNAL_TIMEOUT = 180,
  115. ST_TO_CMD = 0,
  116. ST_FROM_CMD = 1,
  117. /* vendor specific commands of Promise */
  118. MGT_CMD = 0xd8,
  119. SINBAND_MGT_CMD = 0xd9,
  120. ARRAY_CMD = 0xe0,
  121. CONTROLLER_CMD = 0xe1,
  122. DEBUGGING_CMD = 0xe2,
  123. PASSTHRU_CMD = 0xe3,
  124. PASSTHRU_GET_ADAPTER = 0x05,
  125. PASSTHRU_GET_DRVVER = 0x10,
  126. CTLR_CONFIG_CMD = 0x03,
  127. CTLR_SHUTDOWN = 0x0d,
  128. CTLR_POWER_STATE_CHANGE = 0x0e,
  129. CTLR_POWER_SAVING = 0x01,
  130. PASSTHRU_SIGNATURE = 0x4e415041,
  131. MGT_CMD_SIGNATURE = 0xba,
  132. INQUIRY_EVPD = 0x01,
  133. ST_ADDITIONAL_MEM = 0x200000,
  134. };
  135. struct st_sgitem {
  136. u8 ctrl; /* SG_CF_xxx */
  137. u8 reserved[3];
  138. __le32 count;
  139. __le64 addr;
  140. };
  141. struct st_sgtable {
  142. __le16 sg_count;
  143. __le16 max_sg_count;
  144. __le32 sz_in_byte;
  145. };
  146. struct handshake_frame {
  147. __le64 rb_phy; /* request payload queue physical address */
  148. __le16 req_sz; /* size of each request payload */
  149. __le16 req_cnt; /* count of reqs the buffer can hold */
  150. __le16 status_sz; /* size of each status payload */
  151. __le16 status_cnt; /* count of status the buffer can hold */
  152. __le64 hosttime; /* seconds from Jan 1, 1970 (GMT) */
  153. u8 partner_type; /* who sends this frame */
  154. u8 reserved0[7];
  155. __le32 partner_ver_major;
  156. __le32 partner_ver_minor;
  157. __le32 partner_ver_oem;
  158. __le32 partner_ver_build;
  159. __le32 extra_offset; /* NEW */
  160. __le32 extra_size; /* NEW */
  161. u32 reserved1[2];
  162. };
  163. struct req_msg {
  164. __le16 tag;
  165. u8 lun;
  166. u8 target;
  167. u8 task_attr;
  168. u8 task_manage;
  169. u8 data_dir;
  170. u8 payload_sz; /* payload size in 4-byte, not used */
  171. u8 cdb[STEX_CDB_LENGTH];
  172. u8 variable[REQ_VARIABLE_LEN];
  173. };
  174. struct status_msg {
  175. __le16 tag;
  176. u8 lun;
  177. u8 target;
  178. u8 srb_status;
  179. u8 scsi_status;
  180. u8 reserved;
  181. u8 payload_sz; /* payload size in 4-byte */
  182. u8 variable[STATUS_VAR_LEN];
  183. };
  184. struct ver_info {
  185. u32 major;
  186. u32 minor;
  187. u32 oem;
  188. u32 build;
  189. u32 reserved[2];
  190. };
  191. struct st_frame {
  192. u32 base[6];
  193. u32 rom_addr;
  194. struct ver_info drv_ver;
  195. struct ver_info bios_ver;
  196. u32 bus;
  197. u32 slot;
  198. u32 irq_level;
  199. u32 irq_vec;
  200. u32 id;
  201. u32 subid;
  202. u32 dimm_size;
  203. u8 dimm_type;
  204. u8 reserved[3];
  205. u32 channel;
  206. u32 reserved1;
  207. };
  208. struct st_drvver {
  209. u32 major;
  210. u32 minor;
  211. u32 oem;
  212. u32 build;
  213. u32 signature[2];
  214. u8 console_id;
  215. u8 host_no;
  216. u8 reserved0[2];
  217. u32 reserved[3];
  218. };
  219. #define MU_REQ_BUFFER_SIZE (MU_REQ_COUNT * sizeof(struct req_msg))
  220. #define MU_STATUS_BUFFER_SIZE (MU_STATUS_COUNT * sizeof(struct status_msg))
  221. #define MU_BUFFER_SIZE (MU_REQ_BUFFER_SIZE + MU_STATUS_BUFFER_SIZE)
  222. #define STEX_EXTRA_SIZE sizeof(struct st_frame)
  223. #define STEX_BUFFER_SIZE (MU_BUFFER_SIZE + STEX_EXTRA_SIZE)
  224. struct st_ccb {
  225. struct req_msg *req;
  226. struct scsi_cmnd *cmd;
  227. void *sense_buffer;
  228. unsigned int sense_bufflen;
  229. int sg_count;
  230. u32 req_type;
  231. u8 srb_status;
  232. u8 scsi_status;
  233. u8 reserved[2];
  234. };
  235. struct st_hba {
  236. void __iomem *mmio_base; /* iomapped PCI memory space */
  237. void *dma_mem;
  238. dma_addr_t dma_handle;
  239. size_t dma_size;
  240. struct Scsi_Host *host;
  241. struct pci_dev *pdev;
  242. u32 req_head;
  243. u32 req_tail;
  244. u32 status_head;
  245. u32 status_tail;
  246. struct status_msg *status_buffer;
  247. void *copy_buffer; /* temp buffer for driver-handled commands */
  248. struct st_ccb ccb[MU_MAX_REQUEST];
  249. struct st_ccb *wait_ccb;
  250. unsigned int mu_status;
  251. unsigned int cardtype;
  252. int msi_enabled;
  253. int out_req_cnt;
  254. };
  255. static int msi;
  256. module_param(msi, int, 0);
  257. MODULE_PARM_DESC(msi, "Enable Message Signaled Interrupts(0=off, 1=on)");
  258. static const char console_inq_page[] =
  259. {
  260. 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
  261. 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
  262. 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
  263. 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
  264. 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
  265. 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
  266. 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
  267. 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
  268. };
  269. MODULE_AUTHOR("Ed Lin");
  270. MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
  271. MODULE_LICENSE("GPL");
  272. MODULE_VERSION(ST_DRIVER_VERSION);
  273. static void stex_gettime(__le64 *time)
  274. {
  275. struct timeval tv;
  276. do_gettimeofday(&tv);
  277. *time = cpu_to_le64(tv.tv_sec);
  278. }
  279. static struct status_msg *stex_get_status(struct st_hba *hba)
  280. {
  281. struct status_msg *status = hba->status_buffer + hba->status_tail;
  282. ++hba->status_tail;
  283. hba->status_tail %= MU_STATUS_COUNT;
  284. return status;
  285. }
  286. static void stex_invalid_field(struct scsi_cmnd *cmd,
  287. void (*done)(struct scsi_cmnd *))
  288. {
  289. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  290. /* "Invalid field in cdb" */
  291. scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
  292. 0x0);
  293. done(cmd);
  294. }
  295. static struct req_msg *stex_alloc_req(struct st_hba *hba)
  296. {
  297. struct req_msg *req = ((struct req_msg *)hba->dma_mem) +
  298. hba->req_head;
  299. ++hba->req_head;
  300. hba->req_head %= MU_REQ_COUNT;
  301. return req;
  302. }
  303. static int stex_map_sg(struct st_hba *hba,
  304. struct req_msg *req, struct st_ccb *ccb)
  305. {
  306. struct scsi_cmnd *cmd;
  307. struct scatterlist *sg;
  308. struct st_sgtable *dst;
  309. struct st_sgitem *table;
  310. int i, nseg;
  311. cmd = ccb->cmd;
  312. nseg = scsi_dma_map(cmd);
  313. BUG_ON(nseg < 0);
  314. if (nseg) {
  315. dst = (struct st_sgtable *)req->variable;
  316. ccb->sg_count = nseg;
  317. dst->sg_count = cpu_to_le16((u16)nseg);
  318. dst->max_sg_count = cpu_to_le16(hba->host->sg_tablesize);
  319. dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
  320. table = (struct st_sgitem *)(dst + 1);
  321. scsi_for_each_sg(cmd, sg, nseg, i) {
  322. table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
  323. table[i].addr = cpu_to_le64(sg_dma_address(sg));
  324. table[i].ctrl = SG_CF_64B | SG_CF_HOST;
  325. }
  326. table[--i].ctrl |= SG_CF_EOT;
  327. }
  328. return nseg;
  329. }
  330. static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
  331. {
  332. struct st_frame *p;
  333. size_t count = sizeof(struct st_frame);
  334. p = hba->copy_buffer;
  335. scsi_sg_copy_to_buffer(ccb->cmd, p, count);
  336. memset(p->base, 0, sizeof(u32)*6);
  337. *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
  338. p->rom_addr = 0;
  339. p->drv_ver.major = ST_VER_MAJOR;
  340. p->drv_ver.minor = ST_VER_MINOR;
  341. p->drv_ver.oem = ST_OEM;
  342. p->drv_ver.build = ST_BUILD_VER;
  343. p->bus = hba->pdev->bus->number;
  344. p->slot = hba->pdev->devfn;
  345. p->irq_level = 0;
  346. p->irq_vec = hba->pdev->irq;
  347. p->id = hba->pdev->vendor << 16 | hba->pdev->device;
  348. p->subid =
  349. hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
  350. scsi_sg_copy_from_buffer(ccb->cmd, p, count);
  351. }
  352. static void
  353. stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
  354. {
  355. req->tag = cpu_to_le16(tag);
  356. hba->ccb[tag].req = req;
  357. hba->out_req_cnt++;
  358. writel(hba->req_head, hba->mmio_base + IMR0);
  359. writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
  360. readl(hba->mmio_base + IDBL); /* flush */
  361. }
  362. static int
  363. stex_slave_alloc(struct scsi_device *sdev)
  364. {
  365. /* Cheat: usually extracted from Inquiry data */
  366. sdev->tagged_supported = 1;
  367. scsi_activate_tcq(sdev, sdev->host->can_queue);
  368. return 0;
  369. }
  370. static int
  371. stex_slave_config(struct scsi_device *sdev)
  372. {
  373. sdev->use_10_for_rw = 1;
  374. sdev->use_10_for_ms = 1;
  375. blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
  376. sdev->tagged_supported = 1;
  377. return 0;
  378. }
  379. static void
  380. stex_slave_destroy(struct scsi_device *sdev)
  381. {
  382. scsi_deactivate_tcq(sdev, 1);
  383. }
  384. static int
  385. stex_queuecommand(struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
  386. {
  387. struct st_hba *hba;
  388. struct Scsi_Host *host;
  389. unsigned int id, lun;
  390. struct req_msg *req;
  391. u16 tag;
  392. host = cmd->device->host;
  393. id = cmd->device->id;
  394. lun = cmd->device->lun;
  395. hba = (struct st_hba *) &host->hostdata[0];
  396. switch (cmd->cmnd[0]) {
  397. case MODE_SENSE_10:
  398. {
  399. static char ms10_caching_page[12] =
  400. { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
  401. unsigned char page;
  402. page = cmd->cmnd[2] & 0x3f;
  403. if (page == 0x8 || page == 0x3f) {
  404. scsi_sg_copy_from_buffer(cmd, ms10_caching_page,
  405. sizeof(ms10_caching_page));
  406. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  407. done(cmd);
  408. } else
  409. stex_invalid_field(cmd, done);
  410. return 0;
  411. }
  412. case REPORT_LUNS:
  413. /*
  414. * The shasta firmware does not report actual luns in the
  415. * target, so fail the command to force sequential lun scan.
  416. * Also, the console device does not support this command.
  417. */
  418. if (hba->cardtype == st_shasta || id == host->max_id - 1) {
  419. stex_invalid_field(cmd, done);
  420. return 0;
  421. }
  422. break;
  423. case TEST_UNIT_READY:
  424. if (id == host->max_id - 1) {
  425. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  426. done(cmd);
  427. return 0;
  428. }
  429. break;
  430. case INQUIRY:
  431. if (id != host->max_id - 1)
  432. break;
  433. if (lun == 0 && (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
  434. scsi_sg_copy_from_buffer(cmd, (void *)console_inq_page,
  435. sizeof(console_inq_page));
  436. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  437. done(cmd);
  438. } else
  439. stex_invalid_field(cmd, done);
  440. return 0;
  441. case PASSTHRU_CMD:
  442. if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
  443. struct st_drvver ver;
  444. size_t cp_len = sizeof(ver);
  445. ver.major = ST_VER_MAJOR;
  446. ver.minor = ST_VER_MINOR;
  447. ver.oem = ST_OEM;
  448. ver.build = ST_BUILD_VER;
  449. ver.signature[0] = PASSTHRU_SIGNATURE;
  450. ver.console_id = host->max_id - 1;
  451. ver.host_no = hba->host->host_no;
  452. cp_len = scsi_sg_copy_from_buffer(cmd, &ver, cp_len);
  453. cmd->result = sizeof(ver) == cp_len ?
  454. DID_OK << 16 | COMMAND_COMPLETE << 8 :
  455. DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  456. done(cmd);
  457. return 0;
  458. }
  459. default:
  460. break;
  461. }
  462. cmd->scsi_done = done;
  463. tag = cmd->request->tag;
  464. if (unlikely(tag >= host->can_queue))
  465. return SCSI_MLQUEUE_HOST_BUSY;
  466. req = stex_alloc_req(hba);
  467. req->lun = lun;
  468. req->target = id;
  469. /* cdb */
  470. memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
  471. if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  472. req->data_dir = MSG_DATA_DIR_IN;
  473. else if (cmd->sc_data_direction == DMA_TO_DEVICE)
  474. req->data_dir = MSG_DATA_DIR_OUT;
  475. else
  476. req->data_dir = MSG_DATA_DIR_ND;
  477. hba->ccb[tag].cmd = cmd;
  478. hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
  479. hba->ccb[tag].sense_buffer = cmd->sense_buffer;
  480. if (cmd->sc_data_direction != DMA_NONE)
  481. stex_map_sg(hba, req, &hba->ccb[tag]);
  482. stex_send_cmd(hba, req, tag);
  483. return 0;
  484. }
  485. static void stex_scsi_done(struct st_ccb *ccb)
  486. {
  487. struct scsi_cmnd *cmd = ccb->cmd;
  488. int result;
  489. if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
  490. result = ccb->scsi_status;
  491. switch (ccb->scsi_status) {
  492. case SAM_STAT_GOOD:
  493. result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
  494. break;
  495. case SAM_STAT_CHECK_CONDITION:
  496. result |= DRIVER_SENSE << 24;
  497. break;
  498. case SAM_STAT_BUSY:
  499. result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  500. break;
  501. default:
  502. result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  503. break;
  504. }
  505. }
  506. else if (ccb->srb_status & SRB_SEE_SENSE)
  507. result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
  508. else switch (ccb->srb_status) {
  509. case SRB_STATUS_SELECTION_TIMEOUT:
  510. result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  511. break;
  512. case SRB_STATUS_BUSY:
  513. result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  514. break;
  515. case SRB_STATUS_INVALID_REQUEST:
  516. case SRB_STATUS_ERROR:
  517. default:
  518. result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  519. break;
  520. }
  521. cmd->result = result;
  522. cmd->scsi_done(cmd);
  523. }
  524. static void stex_copy_data(struct st_ccb *ccb,
  525. struct status_msg *resp, unsigned int variable)
  526. {
  527. if (resp->scsi_status != SAM_STAT_GOOD) {
  528. if (ccb->sense_buffer != NULL)
  529. memcpy(ccb->sense_buffer, resp->variable,
  530. min(variable, ccb->sense_bufflen));
  531. return;
  532. }
  533. if (ccb->cmd == NULL)
  534. return;
  535. scsi_sg_copy_from_buffer(ccb->cmd, resp->variable, variable);
  536. }
  537. static void stex_check_cmd(struct st_hba *hba,
  538. struct st_ccb *ccb, struct status_msg *resp)
  539. {
  540. if (ccb->cmd->cmnd[0] == MGT_CMD &&
  541. resp->scsi_status != SAM_STAT_CHECK_CONDITION)
  542. scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
  543. le32_to_cpu(*(__le32 *)&resp->variable[0]));
  544. }
  545. static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
  546. {
  547. void __iomem *base = hba->mmio_base;
  548. struct status_msg *resp;
  549. struct st_ccb *ccb;
  550. unsigned int size;
  551. u16 tag;
  552. if (unlikely(!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED)))
  553. return;
  554. /* status payloads */
  555. hba->status_head = readl(base + OMR1);
  556. if (unlikely(hba->status_head >= MU_STATUS_COUNT)) {
  557. printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
  558. pci_name(hba->pdev));
  559. return;
  560. }
  561. /*
  562. * it's not a valid status payload if:
  563. * 1. there are no pending requests(e.g. during init stage)
  564. * 2. there are some pending requests, but the controller is in
  565. * reset status, and its type is not st_yosemite
  566. * firmware of st_yosemite in reset status will return pending requests
  567. * to driver, so we allow it to pass
  568. */
  569. if (unlikely(hba->out_req_cnt <= 0 ||
  570. (hba->mu_status == MU_STATE_RESETTING &&
  571. hba->cardtype != st_yosemite))) {
  572. hba->status_tail = hba->status_head;
  573. goto update_status;
  574. }
  575. while (hba->status_tail != hba->status_head) {
  576. resp = stex_get_status(hba);
  577. tag = le16_to_cpu(resp->tag);
  578. if (unlikely(tag >= hba->host->can_queue)) {
  579. printk(KERN_WARNING DRV_NAME
  580. "(%s): invalid tag\n", pci_name(hba->pdev));
  581. continue;
  582. }
  583. hba->out_req_cnt--;
  584. ccb = &hba->ccb[tag];
  585. if (unlikely(hba->wait_ccb == ccb))
  586. hba->wait_ccb = NULL;
  587. if (unlikely(ccb->req == NULL)) {
  588. printk(KERN_WARNING DRV_NAME
  589. "(%s): lagging req\n", pci_name(hba->pdev));
  590. continue;
  591. }
  592. size = resp->payload_sz * sizeof(u32); /* payload size */
  593. if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
  594. size > sizeof(*resp))) {
  595. printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
  596. pci_name(hba->pdev));
  597. } else {
  598. size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
  599. if (size)
  600. stex_copy_data(ccb, resp, size);
  601. }
  602. ccb->req = NULL;
  603. ccb->srb_status = resp->srb_status;
  604. ccb->scsi_status = resp->scsi_status;
  605. if (likely(ccb->cmd != NULL)) {
  606. if (hba->cardtype == st_yosemite)
  607. stex_check_cmd(hba, ccb, resp);
  608. if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
  609. ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
  610. stex_controller_info(hba, ccb);
  611. scsi_dma_unmap(ccb->cmd);
  612. stex_scsi_done(ccb);
  613. } else
  614. ccb->req_type = 0;
  615. }
  616. update_status:
  617. writel(hba->status_head, base + IMR1);
  618. readl(base + IMR1); /* flush */
  619. }
  620. static irqreturn_t stex_intr(int irq, void *__hba)
  621. {
  622. struct st_hba *hba = __hba;
  623. void __iomem *base = hba->mmio_base;
  624. u32 data;
  625. unsigned long flags;
  626. int handled = 0;
  627. spin_lock_irqsave(hba->host->host_lock, flags);
  628. data = readl(base + ODBL);
  629. if (data && data != 0xffffffff) {
  630. /* clear the interrupt */
  631. writel(data, base + ODBL);
  632. readl(base + ODBL); /* flush */
  633. stex_mu_intr(hba, data);
  634. handled = 1;
  635. }
  636. spin_unlock_irqrestore(hba->host->host_lock, flags);
  637. return IRQ_RETVAL(handled);
  638. }
  639. static int stex_handshake(struct st_hba *hba)
  640. {
  641. void __iomem *base = hba->mmio_base;
  642. struct handshake_frame *h;
  643. dma_addr_t status_phys;
  644. u32 data;
  645. unsigned long before;
  646. if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  647. writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
  648. readl(base + IDBL);
  649. before = jiffies;
  650. while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  651. if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
  652. printk(KERN_ERR DRV_NAME
  653. "(%s): no handshake signature\n",
  654. pci_name(hba->pdev));
  655. return -1;
  656. }
  657. rmb();
  658. msleep(1);
  659. }
  660. }
  661. udelay(10);
  662. data = readl(base + OMR1);
  663. if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
  664. data &= 0x0000ffff;
  665. if (hba->host->can_queue > data) {
  666. hba->host->can_queue = data;
  667. hba->host->cmd_per_lun = data;
  668. }
  669. }
  670. h = (struct handshake_frame *)hba->status_buffer;
  671. h->rb_phy = cpu_to_le64(hba->dma_handle);
  672. h->req_sz = cpu_to_le16(sizeof(struct req_msg));
  673. h->req_cnt = cpu_to_le16(MU_REQ_COUNT);
  674. h->status_sz = cpu_to_le16(sizeof(struct status_msg));
  675. h->status_cnt = cpu_to_le16(MU_STATUS_COUNT);
  676. stex_gettime(&h->hosttime);
  677. h->partner_type = HMU_PARTNER_TYPE;
  678. if (hba->dma_size > STEX_BUFFER_SIZE) {
  679. h->extra_offset = cpu_to_le32(STEX_BUFFER_SIZE);
  680. h->extra_size = cpu_to_le32(ST_ADDITIONAL_MEM);
  681. } else
  682. h->extra_offset = h->extra_size = 0;
  683. status_phys = hba->dma_handle + MU_REQ_BUFFER_SIZE;
  684. writel(status_phys, base + IMR0);
  685. readl(base + IMR0);
  686. writel((status_phys >> 16) >> 16, base + IMR1);
  687. readl(base + IMR1);
  688. writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
  689. readl(base + OMR0);
  690. writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
  691. readl(base + IDBL); /* flush */
  692. udelay(10);
  693. before = jiffies;
  694. while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  695. if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
  696. printk(KERN_ERR DRV_NAME
  697. "(%s): no signature after handshake frame\n",
  698. pci_name(hba->pdev));
  699. return -1;
  700. }
  701. rmb();
  702. msleep(1);
  703. }
  704. writel(0, base + IMR0);
  705. readl(base + IMR0);
  706. writel(0, base + OMR0);
  707. readl(base + OMR0);
  708. writel(0, base + IMR1);
  709. readl(base + IMR1);
  710. writel(0, base + OMR1);
  711. readl(base + OMR1); /* flush */
  712. hba->mu_status = MU_STATE_STARTED;
  713. return 0;
  714. }
  715. static int stex_abort(struct scsi_cmnd *cmd)
  716. {
  717. struct Scsi_Host *host = cmd->device->host;
  718. struct st_hba *hba = (struct st_hba *)host->hostdata;
  719. u16 tag = cmd->request->tag;
  720. void __iomem *base;
  721. u32 data;
  722. int result = SUCCESS;
  723. unsigned long flags;
  724. printk(KERN_INFO DRV_NAME
  725. "(%s): aborting command\n", pci_name(hba->pdev));
  726. scsi_print_command(cmd);
  727. base = hba->mmio_base;
  728. spin_lock_irqsave(host->host_lock, flags);
  729. if (tag < host->can_queue && hba->ccb[tag].cmd == cmd)
  730. hba->wait_ccb = &hba->ccb[tag];
  731. else {
  732. for (tag = 0; tag < host->can_queue; tag++)
  733. if (hba->ccb[tag].cmd == cmd) {
  734. hba->wait_ccb = &hba->ccb[tag];
  735. break;
  736. }
  737. if (tag >= host->can_queue)
  738. goto out;
  739. }
  740. data = readl(base + ODBL);
  741. if (data == 0 || data == 0xffffffff)
  742. goto fail_out;
  743. writel(data, base + ODBL);
  744. readl(base + ODBL); /* flush */
  745. stex_mu_intr(hba, data);
  746. if (hba->wait_ccb == NULL) {
  747. printk(KERN_WARNING DRV_NAME
  748. "(%s): lost interrupt\n", pci_name(hba->pdev));
  749. goto out;
  750. }
  751. fail_out:
  752. scsi_dma_unmap(cmd);
  753. hba->wait_ccb->req = NULL; /* nullify the req's future return */
  754. hba->wait_ccb = NULL;
  755. result = FAILED;
  756. out:
  757. spin_unlock_irqrestore(host->host_lock, flags);
  758. return result;
  759. }
  760. static void stex_hard_reset(struct st_hba *hba)
  761. {
  762. struct pci_bus *bus;
  763. int i;
  764. u16 pci_cmd;
  765. u8 pci_bctl;
  766. for (i = 0; i < 16; i++)
  767. pci_read_config_dword(hba->pdev, i * 4,
  768. &hba->pdev->saved_config_space[i]);
  769. /* Reset secondary bus. Our controller(MU/ATU) is the only device on
  770. secondary bus. Consult Intel 80331/3 developer's manual for detail */
  771. bus = hba->pdev->bus;
  772. pci_read_config_byte(bus->self, PCI_BRIDGE_CONTROL, &pci_bctl);
  773. pci_bctl |= PCI_BRIDGE_CTL_BUS_RESET;
  774. pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
  775. /*
  776. * 1 ms may be enough for 8-port controllers. But 16-port controllers
  777. * require more time to finish bus reset. Use 100 ms here for safety
  778. */
  779. msleep(100);
  780. pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  781. pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
  782. for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
  783. pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
  784. if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
  785. break;
  786. msleep(1);
  787. }
  788. ssleep(5);
  789. for (i = 0; i < 16; i++)
  790. pci_write_config_dword(hba->pdev, i * 4,
  791. hba->pdev->saved_config_space[i]);
  792. }
  793. static int stex_reset(struct scsi_cmnd *cmd)
  794. {
  795. struct st_hba *hba;
  796. void __iomem *base;
  797. unsigned long flags, before;
  798. hba = (struct st_hba *) &cmd->device->host->hostdata[0];
  799. printk(KERN_INFO DRV_NAME
  800. "(%s): resetting host\n", pci_name(hba->pdev));
  801. scsi_print_command(cmd);
  802. hba->mu_status = MU_STATE_RESETTING;
  803. if (hba->cardtype == st_shasta)
  804. stex_hard_reset(hba);
  805. if (hba->cardtype != st_yosemite) {
  806. if (stex_handshake(hba)) {
  807. printk(KERN_WARNING DRV_NAME
  808. "(%s): resetting: handshake failed\n",
  809. pci_name(hba->pdev));
  810. return FAILED;
  811. }
  812. spin_lock_irqsave(hba->host->host_lock, flags);
  813. hba->req_head = 0;
  814. hba->req_tail = 0;
  815. hba->status_head = 0;
  816. hba->status_tail = 0;
  817. hba->out_req_cnt = 0;
  818. spin_unlock_irqrestore(hba->host->host_lock, flags);
  819. return SUCCESS;
  820. }
  821. /* st_yosemite */
  822. writel(MU_INBOUND_DOORBELL_RESET, hba->mmio_base + IDBL);
  823. readl(hba->mmio_base + IDBL); /* flush */
  824. before = jiffies;
  825. while (hba->out_req_cnt > 0) {
  826. if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
  827. printk(KERN_WARNING DRV_NAME
  828. "(%s): reset timeout\n", pci_name(hba->pdev));
  829. return FAILED;
  830. }
  831. msleep(1);
  832. }
  833. base = hba->mmio_base;
  834. writel(0, base + IMR0);
  835. readl(base + IMR0);
  836. writel(0, base + OMR0);
  837. readl(base + OMR0);
  838. writel(0, base + IMR1);
  839. readl(base + IMR1);
  840. writel(0, base + OMR1);
  841. readl(base + OMR1); /* flush */
  842. spin_lock_irqsave(hba->host->host_lock, flags);
  843. hba->req_head = 0;
  844. hba->req_tail = 0;
  845. hba->status_head = 0;
  846. hba->status_tail = 0;
  847. hba->out_req_cnt = 0;
  848. hba->mu_status = MU_STATE_STARTED;
  849. spin_unlock_irqrestore(hba->host->host_lock, flags);
  850. return SUCCESS;
  851. }
  852. static int stex_biosparam(struct scsi_device *sdev,
  853. struct block_device *bdev, sector_t capacity, int geom[])
  854. {
  855. int heads = 255, sectors = 63;
  856. if (capacity < 0x200000) {
  857. heads = 64;
  858. sectors = 32;
  859. }
  860. sector_div(capacity, heads * sectors);
  861. geom[0] = heads;
  862. geom[1] = sectors;
  863. geom[2] = capacity;
  864. return 0;
  865. }
  866. static struct scsi_host_template driver_template = {
  867. .module = THIS_MODULE,
  868. .name = DRV_NAME,
  869. .proc_name = DRV_NAME,
  870. .bios_param = stex_biosparam,
  871. .queuecommand = stex_queuecommand,
  872. .slave_alloc = stex_slave_alloc,
  873. .slave_configure = stex_slave_config,
  874. .slave_destroy = stex_slave_destroy,
  875. .eh_abort_handler = stex_abort,
  876. .eh_host_reset_handler = stex_reset,
  877. .can_queue = ST_CAN_QUEUE,
  878. .this_id = -1,
  879. .sg_tablesize = ST_MAX_SG,
  880. .cmd_per_lun = ST_CMD_PER_LUN,
  881. };
  882. static int stex_set_dma_mask(struct pci_dev * pdev)
  883. {
  884. int ret;
  885. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)
  886. && !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
  887. return 0;
  888. ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  889. if (!ret)
  890. ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  891. return ret;
  892. }
  893. static int stex_request_irq(struct st_hba *hba)
  894. {
  895. struct pci_dev *pdev = hba->pdev;
  896. int status;
  897. if (msi) {
  898. status = pci_enable_msi(pdev);
  899. if (status != 0)
  900. printk(KERN_ERR DRV_NAME
  901. "(%s): error %d setting up MSI\n",
  902. pci_name(pdev), status);
  903. else
  904. hba->msi_enabled = 1;
  905. } else
  906. hba->msi_enabled = 0;
  907. status = request_irq(pdev->irq, stex_intr, IRQF_SHARED, DRV_NAME, hba);
  908. if (status != 0) {
  909. if (hba->msi_enabled)
  910. pci_disable_msi(pdev);
  911. }
  912. return status;
  913. }
  914. static void stex_free_irq(struct st_hba *hba)
  915. {
  916. struct pci_dev *pdev = hba->pdev;
  917. free_irq(pdev->irq, hba);
  918. if (hba->msi_enabled)
  919. pci_disable_msi(pdev);
  920. }
  921. static int __devinit
  922. stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  923. {
  924. struct st_hba *hba;
  925. struct Scsi_Host *host;
  926. int err;
  927. err = pci_enable_device(pdev);
  928. if (err)
  929. return err;
  930. pci_set_master(pdev);
  931. host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
  932. if (!host) {
  933. printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
  934. pci_name(pdev));
  935. err = -ENOMEM;
  936. goto out_disable;
  937. }
  938. hba = (struct st_hba *)host->hostdata;
  939. memset(hba, 0, sizeof(struct st_hba));
  940. err = pci_request_regions(pdev, DRV_NAME);
  941. if (err < 0) {
  942. printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
  943. pci_name(pdev));
  944. goto out_scsi_host_put;
  945. }
  946. hba->mmio_base = pci_ioremap_bar(pdev, 0);
  947. if ( !hba->mmio_base) {
  948. printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
  949. pci_name(pdev));
  950. err = -ENOMEM;
  951. goto out_release_regions;
  952. }
  953. err = stex_set_dma_mask(pdev);
  954. if (err) {
  955. printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
  956. pci_name(pdev));
  957. goto out_iounmap;
  958. }
  959. hba->cardtype = (unsigned int) id->driver_data;
  960. if (hba->cardtype == st_vsc && (pdev->subsystem_device & 1))
  961. hba->cardtype = st_vsc1;
  962. hba->dma_size = (hba->cardtype == st_vsc1 || hba->cardtype == st_seq) ?
  963. (STEX_BUFFER_SIZE + ST_ADDITIONAL_MEM) : (STEX_BUFFER_SIZE);
  964. hba->dma_mem = dma_alloc_coherent(&pdev->dev,
  965. hba->dma_size, &hba->dma_handle, GFP_KERNEL);
  966. if (!hba->dma_mem) {
  967. err = -ENOMEM;
  968. printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
  969. pci_name(pdev));
  970. goto out_iounmap;
  971. }
  972. hba->status_buffer =
  973. (struct status_msg *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
  974. hba->copy_buffer = hba->dma_mem + MU_BUFFER_SIZE;
  975. hba->mu_status = MU_STATE_STARTING;
  976. if (hba->cardtype == st_shasta) {
  977. host->max_lun = 8;
  978. host->max_id = 16 + 1;
  979. } else if (hba->cardtype == st_yosemite) {
  980. host->max_lun = 256;
  981. host->max_id = 1 + 1;
  982. } else {
  983. /* st_vsc , st_vsc1 and st_seq */
  984. host->max_lun = 1;
  985. host->max_id = 128 + 1;
  986. }
  987. host->max_channel = 0;
  988. host->unique_id = host->host_no;
  989. host->max_cmd_len = STEX_CDB_LENGTH;
  990. hba->host = host;
  991. hba->pdev = pdev;
  992. err = stex_request_irq(hba);
  993. if (err) {
  994. printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
  995. pci_name(pdev));
  996. goto out_pci_free;
  997. }
  998. err = stex_handshake(hba);
  999. if (err)
  1000. goto out_free_irq;
  1001. err = scsi_init_shared_tag_map(host, host->can_queue);
  1002. if (err) {
  1003. printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
  1004. pci_name(pdev));
  1005. goto out_free_irq;
  1006. }
  1007. pci_set_drvdata(pdev, hba);
  1008. err = scsi_add_host(host, &pdev->dev);
  1009. if (err) {
  1010. printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
  1011. pci_name(pdev));
  1012. goto out_free_irq;
  1013. }
  1014. scsi_scan_host(host);
  1015. return 0;
  1016. out_free_irq:
  1017. stex_free_irq(hba);
  1018. out_pci_free:
  1019. dma_free_coherent(&pdev->dev, hba->dma_size,
  1020. hba->dma_mem, hba->dma_handle);
  1021. out_iounmap:
  1022. iounmap(hba->mmio_base);
  1023. out_release_regions:
  1024. pci_release_regions(pdev);
  1025. out_scsi_host_put:
  1026. scsi_host_put(host);
  1027. out_disable:
  1028. pci_disable_device(pdev);
  1029. return err;
  1030. }
  1031. static void stex_hba_stop(struct st_hba *hba)
  1032. {
  1033. struct req_msg *req;
  1034. unsigned long flags;
  1035. unsigned long before;
  1036. u16 tag = 0;
  1037. spin_lock_irqsave(hba->host->host_lock, flags);
  1038. req = stex_alloc_req(hba);
  1039. memset(req->cdb, 0, STEX_CDB_LENGTH);
  1040. if (hba->cardtype == st_yosemite) {
  1041. req->cdb[0] = MGT_CMD;
  1042. req->cdb[1] = MGT_CMD_SIGNATURE;
  1043. req->cdb[2] = CTLR_CONFIG_CMD;
  1044. req->cdb[3] = CTLR_SHUTDOWN;
  1045. } else {
  1046. req->cdb[0] = CONTROLLER_CMD;
  1047. req->cdb[1] = CTLR_POWER_STATE_CHANGE;
  1048. req->cdb[2] = CTLR_POWER_SAVING;
  1049. }
  1050. hba->ccb[tag].cmd = NULL;
  1051. hba->ccb[tag].sg_count = 0;
  1052. hba->ccb[tag].sense_bufflen = 0;
  1053. hba->ccb[tag].sense_buffer = NULL;
  1054. hba->ccb[tag].req_type = PASSTHRU_REQ_TYPE;
  1055. stex_send_cmd(hba, req, tag);
  1056. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1057. before = jiffies;
  1058. while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
  1059. if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
  1060. hba->ccb[tag].req_type = 0;
  1061. return;
  1062. }
  1063. msleep(1);
  1064. }
  1065. }
  1066. static void stex_hba_free(struct st_hba *hba)
  1067. {
  1068. stex_free_irq(hba);
  1069. iounmap(hba->mmio_base);
  1070. pci_release_regions(hba->pdev);
  1071. dma_free_coherent(&hba->pdev->dev, hba->dma_size,
  1072. hba->dma_mem, hba->dma_handle);
  1073. }
  1074. static void stex_remove(struct pci_dev *pdev)
  1075. {
  1076. struct st_hba *hba = pci_get_drvdata(pdev);
  1077. scsi_remove_host(hba->host);
  1078. pci_set_drvdata(pdev, NULL);
  1079. stex_hba_stop(hba);
  1080. stex_hba_free(hba);
  1081. scsi_host_put(hba->host);
  1082. pci_disable_device(pdev);
  1083. }
  1084. static void stex_shutdown(struct pci_dev *pdev)
  1085. {
  1086. struct st_hba *hba = pci_get_drvdata(pdev);
  1087. stex_hba_stop(hba);
  1088. }
  1089. static struct pci_device_id stex_pci_tbl[] = {
  1090. /* st_shasta */
  1091. { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1092. st_shasta }, /* SuperTrak EX8350/8300/16350/16300 */
  1093. { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1094. st_shasta }, /* SuperTrak EX12350 */
  1095. { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1096. st_shasta }, /* SuperTrak EX4350 */
  1097. { 0x105a, 0xe350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1098. st_shasta }, /* SuperTrak EX24350 */
  1099. /* st_vsc */
  1100. { 0x105a, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
  1101. /* st_yosemite */
  1102. { 0x105a, 0x8650, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_yosemite },
  1103. /* st_seq */
  1104. { 0x105a, 0x3360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_seq },
  1105. { } /* terminate list */
  1106. };
  1107. MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
  1108. static struct pci_driver stex_pci_driver = {
  1109. .name = DRV_NAME,
  1110. .id_table = stex_pci_tbl,
  1111. .probe = stex_probe,
  1112. .remove = __devexit_p(stex_remove),
  1113. .shutdown = stex_shutdown,
  1114. };
  1115. static int __init stex_init(void)
  1116. {
  1117. printk(KERN_INFO DRV_NAME
  1118. ": Promise SuperTrak EX Driver version: %s\n",
  1119. ST_DRIVER_VERSION);
  1120. return pci_register_driver(&stex_pci_driver);
  1121. }
  1122. static void __exit stex_exit(void)
  1123. {
  1124. pci_unregister_driver(&stex_pci_driver);
  1125. }
  1126. module_init(stex_init);
  1127. module_exit(stex_exit);