stex.c 45 KB

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