stex.c 32 KB

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