stex.c 33 KB

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