stex.c 48 KB

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