stex.c 49 KB

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