stex.c 34 KB

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