stex.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*
  2. * SuperTrak EX Series Storage Controller driver for Linux
  3. *
  4. * Copyright (C) 2005, 2006 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. * Version: 3.0.0.1
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <linux/time.h>
  23. #include <linux/pci.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/types.h>
  27. #include <linux/module.h>
  28. #include <linux/spinlock.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. #define DRV_NAME "stex"
  38. #define ST_DRIVER_VERSION "3.0.0.1"
  39. #define ST_VER_MAJOR 3
  40. #define ST_VER_MINOR 0
  41. #define ST_OEM 0
  42. #define ST_BUILD_VER 1
  43. enum {
  44. /* MU register offset */
  45. IMR0 = 0x10, /* MU_INBOUND_MESSAGE_REG0 */
  46. IMR1 = 0x14, /* MU_INBOUND_MESSAGE_REG1 */
  47. OMR0 = 0x18, /* MU_OUTBOUND_MESSAGE_REG0 */
  48. OMR1 = 0x1c, /* MU_OUTBOUND_MESSAGE_REG1 */
  49. IDBL = 0x20, /* MU_INBOUND_DOORBELL */
  50. IIS = 0x24, /* MU_INBOUND_INTERRUPT_STATUS */
  51. IIM = 0x28, /* MU_INBOUND_INTERRUPT_MASK */
  52. ODBL = 0x2c, /* MU_OUTBOUND_DOORBELL */
  53. OIS = 0x30, /* MU_OUTBOUND_INTERRUPT_STATUS */
  54. OIM = 0x3c, /* MU_OUTBOUND_INTERRUPT_MASK */
  55. /* MU register value */
  56. MU_INBOUND_DOORBELL_HANDSHAKE = 1,
  57. MU_INBOUND_DOORBELL_REQHEADCHANGED = 2,
  58. MU_INBOUND_DOORBELL_STATUSTAILCHANGED = 4,
  59. MU_INBOUND_DOORBELL_HMUSTOPPED = 8,
  60. MU_INBOUND_DOORBELL_RESET = 16,
  61. MU_OUTBOUND_DOORBELL_HANDSHAKE = 1,
  62. MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED = 2,
  63. MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED = 4,
  64. MU_OUTBOUND_DOORBELL_BUSCHANGE = 8,
  65. MU_OUTBOUND_DOORBELL_HASEVENT = 16,
  66. /* MU status code */
  67. MU_STATE_STARTING = 1,
  68. MU_STATE_FMU_READY_FOR_HANDSHAKE = 2,
  69. MU_STATE_SEND_HANDSHAKE_FRAME = 3,
  70. MU_STATE_STARTED = 4,
  71. MU_STATE_RESETTING = 5,
  72. MU_MAX_DELAY_TIME = 240000,
  73. MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
  74. HMU_PARTNER_TYPE = 2,
  75. /* firmware returned values */
  76. SRB_STATUS_SUCCESS = 0x01,
  77. SRB_STATUS_ERROR = 0x04,
  78. SRB_STATUS_BUSY = 0x05,
  79. SRB_STATUS_INVALID_REQUEST = 0x06,
  80. SRB_STATUS_SELECTION_TIMEOUT = 0x0A,
  81. SRB_SEE_SENSE = 0x80,
  82. /* task attribute */
  83. TASK_ATTRIBUTE_SIMPLE = 0x0,
  84. TASK_ATTRIBUTE_HEADOFQUEUE = 0x1,
  85. TASK_ATTRIBUTE_ORDERED = 0x2,
  86. TASK_ATTRIBUTE_ACA = 0x4,
  87. /* request count, etc. */
  88. MU_MAX_REQUEST = 32,
  89. /* one message wasted, use MU_MAX_REQUEST+1
  90. to handle MU_MAX_REQUEST messages */
  91. MU_REQ_COUNT = (MU_MAX_REQUEST + 1),
  92. MU_STATUS_COUNT = (MU_MAX_REQUEST + 1),
  93. STEX_CDB_LENGTH = MAX_COMMAND_SIZE,
  94. REQ_VARIABLE_LEN = 1024,
  95. STATUS_VAR_LEN = 128,
  96. ST_CAN_QUEUE = MU_MAX_REQUEST,
  97. ST_CMD_PER_LUN = MU_MAX_REQUEST,
  98. ST_MAX_SG = 32,
  99. /* sg flags */
  100. SG_CF_EOT = 0x80, /* end of table */
  101. SG_CF_64B = 0x40, /* 64 bit item */
  102. SG_CF_HOST = 0x20, /* sg in host memory */
  103. ST_MAX_ARRAY_SUPPORTED = 16,
  104. ST_MAX_TARGET_NUM = (ST_MAX_ARRAY_SUPPORTED+1),
  105. ST_MAX_LUN_PER_TARGET = 16,
  106. st_shasta = 0,
  107. st_vsc = 1,
  108. st_yosemite = 2,
  109. PASSTHRU_REQ_TYPE = 0x00000001,
  110. PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
  111. ST_INTERNAL_TIMEOUT = 30,
  112. ST_TO_CMD = 0,
  113. ST_FROM_CMD = 1,
  114. /* vendor specific commands of Promise */
  115. MGT_CMD = 0xd8,
  116. SINBAND_MGT_CMD = 0xd9,
  117. ARRAY_CMD = 0xe0,
  118. CONTROLLER_CMD = 0xe1,
  119. DEBUGGING_CMD = 0xe2,
  120. PASSTHRU_CMD = 0xe3,
  121. PASSTHRU_GET_ADAPTER = 0x05,
  122. PASSTHRU_GET_DRVVER = 0x10,
  123. CTLR_CONFIG_CMD = 0x03,
  124. CTLR_SHUTDOWN = 0x0d,
  125. CTLR_POWER_STATE_CHANGE = 0x0e,
  126. CTLR_POWER_SAVING = 0x01,
  127. PASSTHRU_SIGNATURE = 0x4e415041,
  128. MGT_CMD_SIGNATURE = 0xba,
  129. INQUIRY_EVPD = 0x01,
  130. };
  131. /* SCSI inquiry data */
  132. typedef struct st_inq {
  133. u8 DeviceType :5;
  134. u8 DeviceTypeQualifier :3;
  135. u8 DeviceTypeModifier :7;
  136. u8 RemovableMedia :1;
  137. u8 Versions;
  138. u8 ResponseDataFormat :4;
  139. u8 HiSupport :1;
  140. u8 NormACA :1;
  141. u8 ReservedBit :1;
  142. u8 AERC :1;
  143. u8 AdditionalLength;
  144. u8 Reserved[2];
  145. u8 SoftReset :1;
  146. u8 CommandQueue :1;
  147. u8 Reserved2 :1;
  148. u8 LinkedCommands :1;
  149. u8 Synchronous :1;
  150. u8 Wide16Bit :1;
  151. u8 Wide32Bit :1;
  152. u8 RelativeAddressing :1;
  153. u8 VendorId[8];
  154. u8 ProductId[16];
  155. u8 ProductRevisionLevel[4];
  156. u8 VendorSpecific[20];
  157. u8 Reserved3[40];
  158. } ST_INQ;
  159. struct st_sgitem {
  160. u8 ctrl; /* SG_CF_xxx */
  161. u8 reserved[3];
  162. __le32 count;
  163. __le32 addr;
  164. __le32 addr_hi;
  165. };
  166. struct st_sgtable {
  167. __le16 sg_count;
  168. __le16 max_sg_count;
  169. __le32 sz_in_byte;
  170. struct st_sgitem table[ST_MAX_SG];
  171. };
  172. struct handshake_frame {
  173. __le32 rb_phy; /* request payload queue physical address */
  174. __le32 rb_phy_hi;
  175. __le16 req_sz; /* size of each request payload */
  176. __le16 req_cnt; /* count of reqs the buffer can hold */
  177. __le16 status_sz; /* size of each status payload */
  178. __le16 status_cnt; /* count of status the buffer can hold */
  179. __le32 hosttime; /* seconds from Jan 1, 1970 (GMT) */
  180. __le32 hosttime_hi;
  181. u8 partner_type; /* who sends this frame */
  182. u8 reserved0[7];
  183. __le32 partner_ver_major;
  184. __le32 partner_ver_minor;
  185. __le32 partner_ver_oem;
  186. __le32 partner_ver_build;
  187. u32 reserved1[4];
  188. };
  189. struct req_msg {
  190. __le16 tag;
  191. u8 lun;
  192. u8 target;
  193. u8 task_attr;
  194. u8 task_manage;
  195. u8 prd_entry;
  196. u8 payload_sz; /* payload size in 4-byte, not used */
  197. u8 cdb[STEX_CDB_LENGTH];
  198. u8 variable[REQ_VARIABLE_LEN];
  199. };
  200. struct status_msg {
  201. __le16 tag;
  202. u8 lun;
  203. u8 target;
  204. u8 srb_status;
  205. u8 scsi_status;
  206. u8 reserved;
  207. u8 payload_sz; /* payload size in 4-byte */
  208. u8 variable[STATUS_VAR_LEN];
  209. };
  210. struct ver_info {
  211. u32 major;
  212. u32 minor;
  213. u32 oem;
  214. u32 build;
  215. u32 reserved[2];
  216. };
  217. struct st_frame {
  218. u32 base[6];
  219. u32 rom_addr;
  220. struct ver_info drv_ver;
  221. struct ver_info bios_ver;
  222. u32 bus;
  223. u32 slot;
  224. u32 irq_level;
  225. u32 irq_vec;
  226. u32 id;
  227. u32 subid;
  228. u32 dimm_size;
  229. u8 dimm_type;
  230. u8 reserved[3];
  231. u32 channel;
  232. u32 reserved1;
  233. };
  234. struct st_drvver {
  235. u32 major;
  236. u32 minor;
  237. u32 oem;
  238. u32 build;
  239. u32 signature[2];
  240. u8 console_id;
  241. u8 host_no;
  242. u8 reserved0[2];
  243. u32 reserved[3];
  244. };
  245. #define MU_REQ_BUFFER_SIZE (MU_REQ_COUNT * sizeof(struct req_msg))
  246. #define MU_STATUS_BUFFER_SIZE (MU_STATUS_COUNT * sizeof(struct status_msg))
  247. #define MU_BUFFER_SIZE (MU_REQ_BUFFER_SIZE + MU_STATUS_BUFFER_SIZE)
  248. #define STEX_EXTRA_SIZE max(sizeof(struct st_frame), sizeof(ST_INQ))
  249. #define STEX_BUFFER_SIZE (MU_BUFFER_SIZE + STEX_EXTRA_SIZE)
  250. struct st_ccb {
  251. struct req_msg *req;
  252. struct scsi_cmnd *cmd;
  253. void *sense_buffer;
  254. unsigned int sense_bufflen;
  255. int sg_count;
  256. u32 req_type;
  257. u8 srb_status;
  258. u8 scsi_status;
  259. };
  260. struct st_hba {
  261. void __iomem *mmio_base; /* iomapped PCI memory space */
  262. void *dma_mem;
  263. dma_addr_t dma_handle;
  264. struct Scsi_Host *host;
  265. struct pci_dev *pdev;
  266. u32 req_head;
  267. u32 req_tail;
  268. u32 status_head;
  269. u32 status_tail;
  270. struct status_msg *status_buffer;
  271. void *copy_buffer; /* temp buffer for driver-handled commands */
  272. struct st_ccb ccb[MU_MAX_REQUEST];
  273. struct st_ccb *wait_ccb;
  274. wait_queue_head_t waitq;
  275. unsigned int mu_status;
  276. int out_req_cnt;
  277. unsigned int cardtype;
  278. };
  279. static const char console_inq_page[] =
  280. {
  281. 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
  282. 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
  283. 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
  284. 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
  285. 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
  286. 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
  287. 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
  288. 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
  289. };
  290. MODULE_AUTHOR("Ed Lin");
  291. MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
  292. MODULE_LICENSE("GPL");
  293. MODULE_VERSION(ST_DRIVER_VERSION);
  294. static void stex_gettime(__le32 *time)
  295. {
  296. struct timeval tv;
  297. do_gettimeofday(&tv);
  298. *time = cpu_to_le32(tv.tv_sec & 0xffffffff);
  299. *(time + 1) = cpu_to_le32((tv.tv_sec >> 16) >> 16);
  300. }
  301. static struct status_msg *stex_get_status(struct st_hba *hba)
  302. {
  303. struct status_msg *status =
  304. hba->status_buffer + hba->status_tail;
  305. ++hba->status_tail;
  306. hba->status_tail %= MU_STATUS_COUNT;
  307. return status;
  308. }
  309. static void stex_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  310. {
  311. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  312. cmd->sense_buffer[0] = 0x70; /* fixed format, current */
  313. cmd->sense_buffer[2] = sk;
  314. cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
  315. cmd->sense_buffer[12] = asc;
  316. cmd->sense_buffer[13] = ascq;
  317. }
  318. static void stex_invalid_field(struct scsi_cmnd *cmd,
  319. void (*done)(struct scsi_cmnd *))
  320. {
  321. /* "Invalid field in cbd" */
  322. stex_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  323. done(cmd);
  324. }
  325. static struct req_msg *stex_alloc_req(struct st_hba *hba)
  326. {
  327. struct req_msg *req = ((struct req_msg *)hba->dma_mem) +
  328. hba->req_head;
  329. ++hba->req_head;
  330. hba->req_head %= MU_REQ_COUNT;
  331. return req;
  332. }
  333. static int stex_map_sg(struct st_hba *hba,
  334. struct req_msg *req, struct st_ccb *ccb)
  335. {
  336. struct pci_dev *pdev = hba->pdev;
  337. struct scsi_cmnd *cmd;
  338. dma_addr_t dma_handle;
  339. struct scatterlist *src;
  340. struct st_sgtable *dst;
  341. int i;
  342. cmd = ccb->cmd;
  343. dst = (struct st_sgtable *)req->variable;
  344. dst->max_sg_count = cpu_to_le16(ST_MAX_SG);
  345. dst->sz_in_byte = cpu_to_le32(cmd->request_bufflen);
  346. if (cmd->use_sg) {
  347. int n_elem;
  348. src = (struct scatterlist *) cmd->request_buffer;
  349. n_elem = pci_map_sg(pdev, src,
  350. cmd->use_sg, cmd->sc_data_direction);
  351. if (n_elem <= 0)
  352. return -EIO;
  353. ccb->sg_count = n_elem;
  354. dst->sg_count = cpu_to_le16((u16)n_elem);
  355. for (i = 0; i < n_elem; i++, src++) {
  356. dst->table[i].count = cpu_to_le32((u32)sg_dma_len(src));
  357. dst->table[i].addr =
  358. cpu_to_le32(sg_dma_address(src) & 0xffffffff);
  359. dst->table[i].addr_hi =
  360. cpu_to_le32((sg_dma_address(src) >> 16) >> 16);
  361. dst->table[i].ctrl = SG_CF_64B | SG_CF_HOST;
  362. }
  363. dst->table[--i].ctrl |= SG_CF_EOT;
  364. return 0;
  365. }
  366. dma_handle = pci_map_single(pdev, cmd->request_buffer,
  367. cmd->request_bufflen, cmd->sc_data_direction);
  368. cmd->SCp.dma_handle = dma_handle;
  369. ccb->sg_count = 1;
  370. dst->sg_count = cpu_to_le16(1);
  371. dst->table[0].addr = cpu_to_le32(dma_handle & 0xffffffff);
  372. dst->table[0].addr_hi = cpu_to_le32((dma_handle >> 16) >> 16);
  373. dst->table[0].count = cpu_to_le32((u32)cmd->request_bufflen);
  374. dst->table[0].ctrl = SG_CF_EOT | SG_CF_64B | SG_CF_HOST;
  375. return 0;
  376. }
  377. static void stex_internal_copy(struct scsi_cmnd *cmd,
  378. const void *src, size_t *count, int sg_count, int direction)
  379. {
  380. size_t lcount;
  381. size_t len;
  382. void *s, *d, *base = NULL;
  383. if (*count > cmd->request_bufflen)
  384. *count = cmd->request_bufflen;
  385. lcount = *count;
  386. while (lcount) {
  387. len = lcount;
  388. s = (void *)src;
  389. if (cmd->use_sg) {
  390. size_t offset = *count - lcount;
  391. s += offset;
  392. base = scsi_kmap_atomic_sg(cmd->request_buffer,
  393. sg_count, &offset, &len);
  394. if (base == NULL) {
  395. *count -= lcount;
  396. return;
  397. }
  398. d = base + offset;
  399. } else
  400. d = cmd->request_buffer;
  401. if (direction == ST_TO_CMD)
  402. memcpy(d, s, len);
  403. else
  404. memcpy(s, d, len);
  405. lcount -= len;
  406. if (cmd->use_sg)
  407. scsi_kunmap_atomic_sg(base);
  408. }
  409. }
  410. static int stex_direct_copy(struct scsi_cmnd *cmd,
  411. const void *src, size_t count)
  412. {
  413. struct st_hba *hba = (struct st_hba *) &cmd->device->host->hostdata[0];
  414. size_t cp_len = count;
  415. int n_elem = 0;
  416. if (cmd->use_sg) {
  417. n_elem = pci_map_sg(hba->pdev, cmd->request_buffer,
  418. cmd->use_sg, cmd->sc_data_direction);
  419. if (n_elem <= 0)
  420. return 0;
  421. }
  422. stex_internal_copy(cmd, src, &cp_len, n_elem, ST_TO_CMD);
  423. if (cmd->use_sg)
  424. pci_unmap_sg(hba->pdev, cmd->request_buffer,
  425. cmd->use_sg, cmd->sc_data_direction);
  426. return cp_len == count;
  427. }
  428. static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
  429. {
  430. struct st_frame *p;
  431. size_t count = sizeof(struct st_frame);
  432. p = hba->copy_buffer;
  433. memset(p->base, 0, sizeof(u32)*6);
  434. *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
  435. p->rom_addr = 0;
  436. p->drv_ver.major = ST_VER_MAJOR;
  437. p->drv_ver.minor = ST_VER_MINOR;
  438. p->drv_ver.oem = ST_OEM;
  439. p->drv_ver.build = ST_BUILD_VER;
  440. p->bus = hba->pdev->bus->number;
  441. p->slot = hba->pdev->devfn;
  442. p->irq_level = 0;
  443. p->irq_vec = hba->pdev->irq;
  444. p->id = hba->pdev->vendor << 16 | hba->pdev->device;
  445. p->subid =
  446. hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
  447. stex_internal_copy(ccb->cmd, p, &count, ccb->sg_count, ST_TO_CMD);
  448. }
  449. static void
  450. stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
  451. {
  452. req->tag = cpu_to_le16(tag);
  453. req->task_attr = TASK_ATTRIBUTE_SIMPLE;
  454. req->task_manage = 0; /* not supported yet */
  455. hba->ccb[tag].req = req;
  456. hba->out_req_cnt++;
  457. writel(hba->req_head, hba->mmio_base + IMR0);
  458. writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
  459. readl(hba->mmio_base + IDBL); /* flush */
  460. }
  461. static int
  462. stex_slave_alloc(struct scsi_device *sdev)
  463. {
  464. /* Cheat: usually extracted from Inquiry data */
  465. sdev->tagged_supported = 1;
  466. scsi_activate_tcq(sdev, sdev->host->can_queue);
  467. return 0;
  468. }
  469. static int
  470. stex_slave_config(struct scsi_device *sdev)
  471. {
  472. sdev->use_10_for_rw = 1;
  473. sdev->use_10_for_ms = 1;
  474. sdev->timeout = 60 * HZ;
  475. sdev->tagged_supported = 1;
  476. return 0;
  477. }
  478. static void
  479. stex_slave_destroy(struct scsi_device *sdev)
  480. {
  481. scsi_deactivate_tcq(sdev, 1);
  482. }
  483. static int
  484. stex_queuecommand(struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
  485. {
  486. struct st_hba *hba;
  487. struct Scsi_Host *host;
  488. unsigned int id,lun;
  489. struct req_msg *req;
  490. u16 tag;
  491. host = cmd->device->host;
  492. id = cmd->device->id;
  493. lun = cmd->device->channel; /* firmware lun issue work around */
  494. hba = (struct st_hba *) &host->hostdata[0];
  495. switch (cmd->cmnd[0]) {
  496. case MODE_SENSE_10:
  497. {
  498. static char ms10_caching_page[12] =
  499. { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
  500. unsigned char page;
  501. page = cmd->cmnd[2] & 0x3f;
  502. if (page == 0x8 || page == 0x3f) {
  503. stex_direct_copy(cmd, ms10_caching_page,
  504. sizeof(ms10_caching_page));
  505. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  506. done(cmd);
  507. } else
  508. stex_invalid_field(cmd, done);
  509. return 0;
  510. }
  511. case INQUIRY:
  512. if (id != ST_MAX_ARRAY_SUPPORTED)
  513. break;
  514. if (lun == 0 && (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
  515. stex_direct_copy(cmd, console_inq_page,
  516. sizeof(console_inq_page));
  517. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  518. done(cmd);
  519. } else
  520. stex_invalid_field(cmd, done);
  521. return 0;
  522. case PASSTHRU_CMD:
  523. if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
  524. struct st_drvver ver;
  525. ver.major = ST_VER_MAJOR;
  526. ver.minor = ST_VER_MINOR;
  527. ver.oem = ST_OEM;
  528. ver.build = ST_BUILD_VER;
  529. ver.signature[0] = PASSTHRU_SIGNATURE;
  530. ver.console_id = ST_MAX_ARRAY_SUPPORTED;
  531. ver.host_no = hba->host->host_no;
  532. cmd->result = stex_direct_copy(cmd, &ver, sizeof(ver)) ?
  533. DID_OK << 16 | COMMAND_COMPLETE << 8 :
  534. DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  535. done(cmd);
  536. return 0;
  537. }
  538. default:
  539. break;
  540. }
  541. cmd->scsi_done = done;
  542. tag = cmd->request->tag;
  543. if (unlikely(tag >= host->can_queue))
  544. return SCSI_MLQUEUE_HOST_BUSY;
  545. req = stex_alloc_req(hba);
  546. if (hba->cardtype == st_yosemite) {
  547. req->lun = lun * (ST_MAX_TARGET_NUM - 1) + id;
  548. req->target = 0;
  549. } else {
  550. req->lun = lun;
  551. req->target = id;
  552. }
  553. /* cdb */
  554. memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
  555. hba->ccb[tag].cmd = cmd;
  556. hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
  557. hba->ccb[tag].sense_buffer = cmd->sense_buffer;
  558. hba->ccb[tag].req_type = 0;
  559. if (cmd->sc_data_direction != DMA_NONE)
  560. stex_map_sg(hba, req, &hba->ccb[tag]);
  561. stex_send_cmd(hba, req, tag);
  562. return 0;
  563. }
  564. static void stex_unmap_sg(struct st_hba *hba, struct scsi_cmnd *cmd)
  565. {
  566. if (cmd->sc_data_direction != DMA_NONE) {
  567. if (cmd->use_sg)
  568. pci_unmap_sg(hba->pdev, cmd->request_buffer,
  569. cmd->use_sg, cmd->sc_data_direction);
  570. else
  571. pci_unmap_single(hba->pdev, cmd->SCp.dma_handle,
  572. cmd->request_bufflen, cmd->sc_data_direction);
  573. }
  574. }
  575. static void stex_scsi_done(struct st_ccb *ccb)
  576. {
  577. struct scsi_cmnd *cmd = ccb->cmd;
  578. int result;
  579. if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
  580. result = ccb->scsi_status;
  581. switch (ccb->scsi_status) {
  582. case SAM_STAT_GOOD:
  583. result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
  584. break;
  585. case SAM_STAT_CHECK_CONDITION:
  586. result |= DRIVER_SENSE << 24;
  587. break;
  588. case SAM_STAT_BUSY:
  589. result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  590. break;
  591. default:
  592. result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  593. break;
  594. }
  595. }
  596. else if (ccb->srb_status & SRB_SEE_SENSE)
  597. result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
  598. else switch (ccb->srb_status) {
  599. case SRB_STATUS_SELECTION_TIMEOUT:
  600. result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  601. break;
  602. case SRB_STATUS_BUSY:
  603. result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  604. break;
  605. case SRB_STATUS_INVALID_REQUEST:
  606. case SRB_STATUS_ERROR:
  607. default:
  608. result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  609. break;
  610. }
  611. cmd->result = result;
  612. cmd->scsi_done(cmd);
  613. }
  614. static void stex_copy_data(struct st_ccb *ccb,
  615. struct status_msg *resp, unsigned int variable)
  616. {
  617. size_t count = variable;
  618. if (resp->scsi_status != SAM_STAT_GOOD) {
  619. if (ccb->sense_buffer != NULL)
  620. memcpy(ccb->sense_buffer, resp->variable,
  621. min(variable, ccb->sense_bufflen));
  622. return;
  623. }
  624. if (ccb->cmd == NULL)
  625. return;
  626. stex_internal_copy(ccb->cmd,
  627. resp->variable, &count, ccb->sg_count, ST_TO_CMD);
  628. }
  629. static void stex_ys_commands(struct st_hba *hba,
  630. struct st_ccb *ccb, struct status_msg *resp)
  631. {
  632. size_t count;
  633. if (ccb->cmd->cmnd[0] == MGT_CMD &&
  634. resp->scsi_status != SAM_STAT_CHECK_CONDITION) {
  635. ccb->cmd->request_bufflen =
  636. le32_to_cpu(*(__le32 *)&resp->variable[0]);
  637. return;
  638. }
  639. if (resp->srb_status != 0)
  640. return;
  641. /* determine inquiry command status by DeviceTypeQualifier */
  642. if (ccb->cmd->cmnd[0] == INQUIRY &&
  643. resp->scsi_status == SAM_STAT_GOOD) {
  644. ST_INQ *inq_data;
  645. count = STEX_EXTRA_SIZE;
  646. stex_internal_copy(ccb->cmd, hba->copy_buffer,
  647. &count, ccb->sg_count, ST_FROM_CMD);
  648. inq_data = (ST_INQ *)hba->copy_buffer;
  649. if (inq_data->DeviceTypeQualifier != 0)
  650. ccb->srb_status = SRB_STATUS_SELECTION_TIMEOUT;
  651. else
  652. ccb->srb_status = SRB_STATUS_SUCCESS;
  653. } else if (ccb->cmd->cmnd[0] == REPORT_LUNS) {
  654. u8 *report_lun_data = (u8 *)hba->copy_buffer;
  655. count = STEX_EXTRA_SIZE;
  656. stex_internal_copy(ccb->cmd, report_lun_data,
  657. &count, ccb->sg_count, ST_FROM_CMD);
  658. if (report_lun_data[2] || report_lun_data[3]) {
  659. report_lun_data[2] = 0x00;
  660. report_lun_data[3] = 0x08;
  661. stex_internal_copy(ccb->cmd, report_lun_data,
  662. &count, ccb->sg_count, ST_TO_CMD);
  663. }
  664. }
  665. }
  666. static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
  667. {
  668. void __iomem *base = hba->mmio_base;
  669. struct status_msg *resp;
  670. struct st_ccb *ccb;
  671. unsigned int size;
  672. u16 tag;
  673. if (!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED))
  674. return;
  675. /* status payloads */
  676. hba->status_head = readl(base + OMR1);
  677. if (unlikely(hba->status_head >= MU_STATUS_COUNT)) {
  678. printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
  679. pci_name(hba->pdev));
  680. return;
  681. }
  682. /*
  683. * it's not a valid status payload if:
  684. * 1. there are no pending requests(e.g. during init stage)
  685. * 2. there are some pending requests, but the controller is in
  686. * reset status, and its type is not st_yosemite
  687. * firmware of st_yosemite in reset status will return pending requests
  688. * to driver, so we allow it to pass
  689. */
  690. if (unlikely(hba->out_req_cnt <= 0 ||
  691. (hba->mu_status == MU_STATE_RESETTING &&
  692. hba->cardtype != st_yosemite))) {
  693. hba->status_tail = hba->status_head;
  694. goto update_status;
  695. }
  696. while (hba->status_tail != hba->status_head) {
  697. resp = stex_get_status(hba);
  698. tag = le16_to_cpu(resp->tag);
  699. if (unlikely(tag >= hba->host->can_queue)) {
  700. printk(KERN_WARNING DRV_NAME
  701. "(%s): invalid tag\n", pci_name(hba->pdev));
  702. continue;
  703. }
  704. ccb = &hba->ccb[tag];
  705. if (hba->wait_ccb == ccb)
  706. hba->wait_ccb = NULL;
  707. if (unlikely(ccb->req == NULL)) {
  708. printk(KERN_WARNING DRV_NAME
  709. "(%s): lagging req\n", pci_name(hba->pdev));
  710. hba->out_req_cnt--;
  711. continue;
  712. }
  713. size = resp->payload_sz * sizeof(u32); /* payload size */
  714. if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
  715. size > sizeof(*resp))) {
  716. printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
  717. pci_name(hba->pdev));
  718. } else {
  719. size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
  720. if (size)
  721. stex_copy_data(ccb, resp, size);
  722. }
  723. ccb->srb_status = resp->srb_status;
  724. ccb->scsi_status = resp->scsi_status;
  725. if (likely(ccb->cmd != NULL)) {
  726. if (hba->cardtype == st_yosemite)
  727. stex_ys_commands(hba, ccb, resp);
  728. if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
  729. ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
  730. stex_controller_info(hba, ccb);
  731. stex_unmap_sg(hba, ccb->cmd);
  732. stex_scsi_done(ccb);
  733. hba->out_req_cnt--;
  734. } else if (ccb->req_type & PASSTHRU_REQ_TYPE) {
  735. hba->out_req_cnt--;
  736. if (ccb->req_type & PASSTHRU_REQ_NO_WAKEUP) {
  737. ccb->req_type = 0;
  738. continue;
  739. }
  740. ccb->req_type = 0;
  741. if (waitqueue_active(&hba->waitq))
  742. wake_up(&hba->waitq);
  743. }
  744. }
  745. update_status:
  746. writel(hba->status_head, base + IMR1);
  747. readl(base + IMR1); /* flush */
  748. }
  749. static irqreturn_t stex_intr(int irq, void *__hba)
  750. {
  751. struct st_hba *hba = __hba;
  752. void __iomem *base = hba->mmio_base;
  753. u32 data;
  754. unsigned long flags;
  755. int handled = 0;
  756. spin_lock_irqsave(hba->host->host_lock, flags);
  757. data = readl(base + ODBL);
  758. if (data && data != 0xffffffff) {
  759. /* clear the interrupt */
  760. writel(data, base + ODBL);
  761. readl(base + ODBL); /* flush */
  762. stex_mu_intr(hba, data);
  763. handled = 1;
  764. }
  765. spin_unlock_irqrestore(hba->host->host_lock, flags);
  766. return IRQ_RETVAL(handled);
  767. }
  768. static int stex_handshake(struct st_hba *hba)
  769. {
  770. void __iomem *base = hba->mmio_base;
  771. struct handshake_frame *h;
  772. dma_addr_t status_phys;
  773. int i;
  774. if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  775. writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
  776. readl(base + IDBL);
  777. for (i = 0; readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE
  778. && i < MU_MAX_DELAY_TIME; i++) {
  779. rmb();
  780. msleep(1);
  781. }
  782. if (i == MU_MAX_DELAY_TIME) {
  783. printk(KERN_ERR DRV_NAME
  784. "(%s): no handshake signature\n",
  785. pci_name(hba->pdev));
  786. return -1;
  787. }
  788. }
  789. udelay(10);
  790. h = (struct handshake_frame *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
  791. h->rb_phy = cpu_to_le32(hba->dma_handle);
  792. h->rb_phy_hi = cpu_to_le32((hba->dma_handle >> 16) >> 16);
  793. h->req_sz = cpu_to_le16(sizeof(struct req_msg));
  794. h->req_cnt = cpu_to_le16(MU_REQ_COUNT);
  795. h->status_sz = cpu_to_le16(sizeof(struct status_msg));
  796. h->status_cnt = cpu_to_le16(MU_STATUS_COUNT);
  797. stex_gettime(&h->hosttime);
  798. h->partner_type = HMU_PARTNER_TYPE;
  799. status_phys = hba->dma_handle + MU_REQ_BUFFER_SIZE;
  800. writel(status_phys, base + IMR0);
  801. readl(base + IMR0);
  802. writel((status_phys >> 16) >> 16, base + IMR1);
  803. readl(base + IMR1);
  804. writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
  805. readl(base + OMR0);
  806. writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
  807. readl(base + IDBL); /* flush */
  808. udelay(10);
  809. for (i = 0; readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE
  810. && i < MU_MAX_DELAY_TIME; i++) {
  811. rmb();
  812. msleep(1);
  813. }
  814. if (i == MU_MAX_DELAY_TIME) {
  815. printk(KERN_ERR DRV_NAME
  816. "(%s): no signature after handshake frame\n",
  817. pci_name(hba->pdev));
  818. return -1;
  819. }
  820. writel(0, base + IMR0);
  821. readl(base + IMR0);
  822. writel(0, base + OMR0);
  823. readl(base + OMR0);
  824. writel(0, base + IMR1);
  825. readl(base + IMR1);
  826. writel(0, base + OMR1);
  827. readl(base + OMR1); /* flush */
  828. hba->mu_status = MU_STATE_STARTED;
  829. return 0;
  830. }
  831. static int stex_abort(struct scsi_cmnd *cmd)
  832. {
  833. struct Scsi_Host *host = cmd->device->host;
  834. struct st_hba *hba = (struct st_hba *)host->hostdata;
  835. u16 tag = cmd->request->tag;
  836. void __iomem *base;
  837. u32 data;
  838. int result = SUCCESS;
  839. unsigned long flags;
  840. base = hba->mmio_base;
  841. spin_lock_irqsave(host->host_lock, flags);
  842. if (tag < host->can_queue && hba->ccb[tag].cmd == cmd)
  843. hba->wait_ccb = &hba->ccb[tag];
  844. else {
  845. for (tag = 0; tag < host->can_queue; tag++)
  846. if (hba->ccb[tag].cmd == cmd) {
  847. hba->wait_ccb = &hba->ccb[tag];
  848. break;
  849. }
  850. if (tag >= host->can_queue)
  851. goto out;
  852. }
  853. data = readl(base + ODBL);
  854. if (data == 0 || data == 0xffffffff)
  855. goto fail_out;
  856. writel(data, base + ODBL);
  857. readl(base + ODBL); /* flush */
  858. stex_mu_intr(hba, data);
  859. if (hba->wait_ccb == NULL) {
  860. printk(KERN_WARNING DRV_NAME
  861. "(%s): lost interrupt\n", pci_name(hba->pdev));
  862. goto out;
  863. }
  864. fail_out:
  865. stex_unmap_sg(hba, cmd);
  866. hba->wait_ccb->req = NULL; /* nullify the req's future return */
  867. hba->wait_ccb = NULL;
  868. result = FAILED;
  869. out:
  870. spin_unlock_irqrestore(host->host_lock, flags);
  871. return result;
  872. }
  873. static void stex_hard_reset(struct st_hba *hba)
  874. {
  875. struct pci_bus *bus;
  876. int i;
  877. u16 pci_cmd;
  878. u8 pci_bctl;
  879. for (i = 0; i < 16; i++)
  880. pci_read_config_dword(hba->pdev, i * 4,
  881. &hba->pdev->saved_config_space[i]);
  882. /* Reset secondary bus. Our controller(MU/ATU) is the only device on
  883. secondary bus. Consult Intel 80331/3 developer's manual for detail */
  884. bus = hba->pdev->bus;
  885. pci_read_config_byte(bus->self, PCI_BRIDGE_CONTROL, &pci_bctl);
  886. pci_bctl |= PCI_BRIDGE_CTL_BUS_RESET;
  887. pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
  888. msleep(1);
  889. pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  890. pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
  891. for (i = 0; i < MU_MAX_DELAY_TIME; i++) {
  892. pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
  893. if (pci_cmd & PCI_COMMAND_MASTER)
  894. break;
  895. msleep(1);
  896. }
  897. ssleep(5);
  898. for (i = 0; i < 16; i++)
  899. pci_write_config_dword(hba->pdev, i * 4,
  900. hba->pdev->saved_config_space[i]);
  901. }
  902. static int stex_reset(struct scsi_cmnd *cmd)
  903. {
  904. struct st_hba *hba;
  905. unsigned long flags;
  906. unsigned long before;
  907. hba = (struct st_hba *) &cmd->device->host->hostdata[0];
  908. hba->mu_status = MU_STATE_RESETTING;
  909. if (hba->cardtype == st_shasta)
  910. stex_hard_reset(hba);
  911. if (hba->cardtype != st_yosemite) {
  912. if (stex_handshake(hba)) {
  913. printk(KERN_WARNING DRV_NAME
  914. "(%s): resetting: handshake failed\n",
  915. pci_name(hba->pdev));
  916. return FAILED;
  917. }
  918. spin_lock_irqsave(hba->host->host_lock, flags);
  919. hba->req_head = 0;
  920. hba->req_tail = 0;
  921. hba->status_head = 0;
  922. hba->status_tail = 0;
  923. hba->out_req_cnt = 0;
  924. spin_unlock_irqrestore(hba->host->host_lock, flags);
  925. return SUCCESS;
  926. }
  927. /* st_yosemite */
  928. writel(MU_INBOUND_DOORBELL_RESET, hba->mmio_base + IDBL);
  929. readl(hba->mmio_base + IDBL); /* flush */
  930. before = jiffies;
  931. while (hba->out_req_cnt > 0) {
  932. if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
  933. printk(KERN_WARNING DRV_NAME
  934. "(%s): reset timeout\n", pci_name(hba->pdev));
  935. return FAILED;
  936. }
  937. msleep(1);
  938. }
  939. hba->mu_status = MU_STATE_STARTED;
  940. return SUCCESS;
  941. }
  942. static int stex_biosparam(struct scsi_device *sdev,
  943. struct block_device *bdev, sector_t capacity, int geom[])
  944. {
  945. int heads = 255, sectors = 63, cylinders;
  946. if (capacity < 0x200000) {
  947. heads = 64;
  948. sectors = 32;
  949. }
  950. cylinders = sector_div(capacity, heads * sectors);
  951. geom[0] = heads;
  952. geom[1] = sectors;
  953. geom[2] = cylinders;
  954. return 0;
  955. }
  956. static struct scsi_host_template driver_template = {
  957. .module = THIS_MODULE,
  958. .name = DRV_NAME,
  959. .proc_name = DRV_NAME,
  960. .bios_param = stex_biosparam,
  961. .queuecommand = stex_queuecommand,
  962. .slave_alloc = stex_slave_alloc,
  963. .slave_configure = stex_slave_config,
  964. .slave_destroy = stex_slave_destroy,
  965. .eh_abort_handler = stex_abort,
  966. .eh_host_reset_handler = stex_reset,
  967. .can_queue = ST_CAN_QUEUE,
  968. .this_id = -1,
  969. .sg_tablesize = ST_MAX_SG,
  970. .cmd_per_lun = ST_CMD_PER_LUN,
  971. };
  972. static int stex_set_dma_mask(struct pci_dev * pdev)
  973. {
  974. int ret;
  975. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)
  976. && !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
  977. return 0;
  978. ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  979. if (!ret)
  980. ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  981. return ret;
  982. }
  983. static int __devinit
  984. stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  985. {
  986. struct st_hba *hba;
  987. struct Scsi_Host *host;
  988. int err;
  989. err = pci_enable_device(pdev);
  990. if (err)
  991. return err;
  992. pci_set_master(pdev);
  993. host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
  994. if (!host) {
  995. printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
  996. pci_name(pdev));
  997. err = -ENOMEM;
  998. goto out_disable;
  999. }
  1000. hba = (struct st_hba *)host->hostdata;
  1001. memset(hba, 0, sizeof(struct st_hba));
  1002. err = pci_request_regions(pdev, DRV_NAME);
  1003. if (err < 0) {
  1004. printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
  1005. pci_name(pdev));
  1006. goto out_scsi_host_put;
  1007. }
  1008. hba->mmio_base = ioremap(pci_resource_start(pdev, 0),
  1009. pci_resource_len(pdev, 0));
  1010. if ( !hba->mmio_base) {
  1011. printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
  1012. pci_name(pdev));
  1013. err = -ENOMEM;
  1014. goto out_release_regions;
  1015. }
  1016. err = stex_set_dma_mask(pdev);
  1017. if (err) {
  1018. printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
  1019. pci_name(pdev));
  1020. goto out_iounmap;
  1021. }
  1022. hba->dma_mem = dma_alloc_coherent(&pdev->dev,
  1023. STEX_BUFFER_SIZE, &hba->dma_handle, GFP_KERNEL);
  1024. if (!hba->dma_mem) {
  1025. err = -ENOMEM;
  1026. printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
  1027. pci_name(pdev));
  1028. goto out_iounmap;
  1029. }
  1030. hba->status_buffer =
  1031. (struct status_msg *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
  1032. hba->copy_buffer = hba->dma_mem + MU_BUFFER_SIZE;
  1033. hba->mu_status = MU_STATE_STARTING;
  1034. hba->cardtype = (unsigned int) id->driver_data;
  1035. /* firmware uses id/lun pair for a logical drive, but lun would be
  1036. always 0 if CONFIG_SCSI_MULTI_LUN not configured, so we use
  1037. channel to map lun here */
  1038. host->max_channel = ST_MAX_LUN_PER_TARGET - 1;
  1039. host->max_id = ST_MAX_TARGET_NUM;
  1040. host->max_lun = 1;
  1041. host->unique_id = host->host_no;
  1042. host->max_cmd_len = STEX_CDB_LENGTH;
  1043. hba->host = host;
  1044. hba->pdev = pdev;
  1045. init_waitqueue_head(&hba->waitq);
  1046. err = request_irq(pdev->irq, stex_intr, IRQF_SHARED, DRV_NAME, hba);
  1047. if (err) {
  1048. printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
  1049. pci_name(pdev));
  1050. goto out_pci_free;
  1051. }
  1052. err = stex_handshake(hba);
  1053. if (err)
  1054. goto out_free_irq;
  1055. err = scsi_init_shared_tag_map(host, ST_CAN_QUEUE);
  1056. if (err) {
  1057. printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
  1058. pci_name(pdev));
  1059. goto out_free_irq;
  1060. }
  1061. pci_set_drvdata(pdev, hba);
  1062. err = scsi_add_host(host, &pdev->dev);
  1063. if (err) {
  1064. printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
  1065. pci_name(pdev));
  1066. goto out_free_irq;
  1067. }
  1068. scsi_scan_host(host);
  1069. return 0;
  1070. out_free_irq:
  1071. free_irq(pdev->irq, hba);
  1072. out_pci_free:
  1073. dma_free_coherent(&pdev->dev, STEX_BUFFER_SIZE,
  1074. hba->dma_mem, hba->dma_handle);
  1075. out_iounmap:
  1076. iounmap(hba->mmio_base);
  1077. out_release_regions:
  1078. pci_release_regions(pdev);
  1079. out_scsi_host_put:
  1080. scsi_host_put(host);
  1081. out_disable:
  1082. pci_disable_device(pdev);
  1083. return err;
  1084. }
  1085. static void stex_hba_stop(struct st_hba *hba)
  1086. {
  1087. struct req_msg *req;
  1088. unsigned long flags;
  1089. unsigned long before;
  1090. u16 tag = 0;
  1091. spin_lock_irqsave(hba->host->host_lock, flags);
  1092. req = stex_alloc_req(hba);
  1093. memset(req->cdb, 0, STEX_CDB_LENGTH);
  1094. if (hba->cardtype == st_yosemite) {
  1095. req->cdb[0] = MGT_CMD;
  1096. req->cdb[1] = MGT_CMD_SIGNATURE;
  1097. req->cdb[2] = CTLR_CONFIG_CMD;
  1098. req->cdb[3] = CTLR_SHUTDOWN;
  1099. } else {
  1100. req->cdb[0] = CONTROLLER_CMD;
  1101. req->cdb[1] = CTLR_POWER_STATE_CHANGE;
  1102. req->cdb[2] = CTLR_POWER_SAVING;
  1103. }
  1104. hba->ccb[tag].cmd = NULL;
  1105. hba->ccb[tag].sg_count = 0;
  1106. hba->ccb[tag].sense_bufflen = 0;
  1107. hba->ccb[tag].sense_buffer = NULL;
  1108. hba->ccb[tag].req_type |= PASSTHRU_REQ_TYPE;
  1109. stex_send_cmd(hba, req, tag);
  1110. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1111. before = jiffies;
  1112. while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
  1113. if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ))
  1114. return;
  1115. msleep(10);
  1116. }
  1117. }
  1118. static void stex_hba_free(struct st_hba *hba)
  1119. {
  1120. free_irq(hba->pdev->irq, hba);
  1121. iounmap(hba->mmio_base);
  1122. pci_release_regions(hba->pdev);
  1123. dma_free_coherent(&hba->pdev->dev, STEX_BUFFER_SIZE,
  1124. hba->dma_mem, hba->dma_handle);
  1125. }
  1126. static void stex_remove(struct pci_dev *pdev)
  1127. {
  1128. struct st_hba *hba = pci_get_drvdata(pdev);
  1129. scsi_remove_host(hba->host);
  1130. pci_set_drvdata(pdev, NULL);
  1131. stex_hba_stop(hba);
  1132. stex_hba_free(hba);
  1133. scsi_host_put(hba->host);
  1134. pci_disable_device(pdev);
  1135. }
  1136. static void stex_shutdown(struct pci_dev *pdev)
  1137. {
  1138. struct st_hba *hba = pci_get_drvdata(pdev);
  1139. stex_hba_stop(hba);
  1140. }
  1141. static struct pci_device_id stex_pci_tbl[] = {
  1142. { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1143. { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1144. { 0x105a, 0xf350, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1145. { 0x105a, 0x4301, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1146. { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1147. { 0x105a, 0x8301, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1148. { 0x105a, 0x8302, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_shasta },
  1149. { 0x1725, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
  1150. { 0x105a, 0x8650, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_yosemite },
  1151. { } /* terminate list */
  1152. };
  1153. MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
  1154. static struct pci_driver stex_pci_driver = {
  1155. .name = DRV_NAME,
  1156. .id_table = stex_pci_tbl,
  1157. .probe = stex_probe,
  1158. .remove = __devexit_p(stex_remove),
  1159. .shutdown = stex_shutdown,
  1160. };
  1161. static int __init stex_init(void)
  1162. {
  1163. printk(KERN_INFO DRV_NAME
  1164. ": Promise SuperTrak EX Driver version: %s\n",
  1165. ST_DRIVER_VERSION);
  1166. return pci_register_driver(&stex_pci_driver);
  1167. }
  1168. static void __exit stex_exit(void)
  1169. {
  1170. pci_unregister_driver(&stex_pci_driver);
  1171. }
  1172. module_init(stex_init);
  1173. module_exit(stex_exit);