stex.c 33 KB

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