stex.c 40 KB

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