stex.c 48 KB

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