stex.c 40 KB

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