stex.c 31 KB

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