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