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. */
  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 scsi_cmnd *cmd;
  339. struct scatterlist *sg;
  340. struct st_sgtable *dst;
  341. int i, nseg;
  342. cmd = ccb->cmd;
  343. dst = (struct st_sgtable *)req->variable;
  344. dst->max_sg_count = cpu_to_le16(ST_MAX_SG);
  345. dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
  346. nseg = scsi_dma_map(cmd);
  347. if (nseg < 0)
  348. return -EIO;
  349. if (nseg) {
  350. ccb->sg_count = nseg;
  351. dst->sg_count = cpu_to_le16((u16)nseg);
  352. scsi_for_each_sg(cmd, sg, nseg, i) {
  353. dst->table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
  354. dst->table[i].addr =
  355. cpu_to_le32(sg_dma_address(sg) & 0xffffffff);
  356. dst->table[i].addr_hi =
  357. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  358. dst->table[i].ctrl = SG_CF_64B | SG_CF_HOST;
  359. }
  360. dst->table[--i].ctrl |= SG_CF_EOT;
  361. }
  362. return 0;
  363. }
  364. static void stex_internal_copy(struct scsi_cmnd *cmd,
  365. const void *src, size_t *count, int sg_count, int direction)
  366. {
  367. size_t lcount;
  368. size_t len;
  369. void *s, *d, *base = NULL;
  370. size_t offset;
  371. if (*count > scsi_bufflen(cmd))
  372. *count = scsi_bufflen(cmd);
  373. lcount = *count;
  374. while (lcount) {
  375. len = lcount;
  376. s = (void *)src;
  377. offset = *count - lcount;
  378. s += offset;
  379. base = scsi_kmap_atomic_sg(scsi_sglist(cmd),
  380. sg_count, &offset, &len);
  381. if (!base) {
  382. *count -= lcount;
  383. return;
  384. }
  385. d = base + offset;
  386. if (direction == ST_TO_CMD)
  387. memcpy(d, s, len);
  388. else
  389. memcpy(s, d, len);
  390. lcount -= len;
  391. scsi_kunmap_atomic_sg(base);
  392. }
  393. }
  394. static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
  395. {
  396. struct st_frame *p;
  397. size_t count = sizeof(struct st_frame);
  398. p = hba->copy_buffer;
  399. stex_internal_copy(ccb->cmd, p, &count, scsi_sg_count(ccb->cmd),
  400. ST_FROM_CMD);
  401. memset(p->base, 0, sizeof(u32)*6);
  402. *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
  403. p->rom_addr = 0;
  404. p->drv_ver.major = ST_VER_MAJOR;
  405. p->drv_ver.minor = ST_VER_MINOR;
  406. p->drv_ver.oem = ST_OEM;
  407. p->drv_ver.build = ST_BUILD_VER;
  408. p->bus = hba->pdev->bus->number;
  409. p->slot = hba->pdev->devfn;
  410. p->irq_level = 0;
  411. p->irq_vec = hba->pdev->irq;
  412. p->id = hba->pdev->vendor << 16 | hba->pdev->device;
  413. p->subid =
  414. hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
  415. stex_internal_copy(ccb->cmd, p, &count, scsi_sg_count(ccb->cmd),
  416. ST_TO_CMD);
  417. }
  418. static void
  419. stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
  420. {
  421. req->tag = cpu_to_le16(tag);
  422. req->task_attr = TASK_ATTRIBUTE_SIMPLE;
  423. req->task_manage = 0; /* not supported yet */
  424. hba->ccb[tag].req = req;
  425. hba->out_req_cnt++;
  426. writel(hba->req_head, hba->mmio_base + IMR0);
  427. writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
  428. readl(hba->mmio_base + IDBL); /* flush */
  429. }
  430. static int
  431. stex_slave_alloc(struct scsi_device *sdev)
  432. {
  433. /* Cheat: usually extracted from Inquiry data */
  434. sdev->tagged_supported = 1;
  435. scsi_activate_tcq(sdev, sdev->host->can_queue);
  436. return 0;
  437. }
  438. static int
  439. stex_slave_config(struct scsi_device *sdev)
  440. {
  441. sdev->use_10_for_rw = 1;
  442. sdev->use_10_for_ms = 1;
  443. sdev->timeout = 60 * HZ;
  444. sdev->tagged_supported = 1;
  445. return 0;
  446. }
  447. static void
  448. stex_slave_destroy(struct scsi_device *sdev)
  449. {
  450. scsi_deactivate_tcq(sdev, 1);
  451. }
  452. static int
  453. stex_queuecommand(struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
  454. {
  455. struct st_hba *hba;
  456. struct Scsi_Host *host;
  457. unsigned int id,lun;
  458. struct req_msg *req;
  459. u16 tag;
  460. host = cmd->device->host;
  461. id = cmd->device->id;
  462. lun = cmd->device->lun;
  463. hba = (struct st_hba *) &host->hostdata[0];
  464. switch (cmd->cmnd[0]) {
  465. case MODE_SENSE_10:
  466. {
  467. static char ms10_caching_page[12] =
  468. { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
  469. unsigned char page;
  470. page = cmd->cmnd[2] & 0x3f;
  471. if (page == 0x8 || page == 0x3f) {
  472. size_t cp_len = sizeof(ms10_caching_page);
  473. stex_internal_copy(cmd, ms10_caching_page,
  474. &cp_len, scsi_sg_count(cmd),
  475. ST_TO_CMD);
  476. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  477. done(cmd);
  478. } else
  479. stex_invalid_field(cmd, done);
  480. return 0;
  481. }
  482. case REPORT_LUNS:
  483. /*
  484. * The shasta firmware does not report actual luns in the
  485. * target, so fail the command to force sequential lun scan.
  486. * Also, the console device does not support this command.
  487. */
  488. if (hba->cardtype == st_shasta || id == host->max_id - 1) {
  489. stex_invalid_field(cmd, done);
  490. return 0;
  491. }
  492. break;
  493. case TEST_UNIT_READY:
  494. if (id == host->max_id - 1) {
  495. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  496. done(cmd);
  497. return 0;
  498. }
  499. break;
  500. case INQUIRY:
  501. if (id != host->max_id - 1)
  502. break;
  503. if (lun == 0 && (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
  504. size_t cp_len = sizeof(console_inq_page);
  505. stex_internal_copy(cmd, console_inq_page,
  506. &cp_len, scsi_sg_count(cmd),
  507. ST_TO_CMD);
  508. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  509. done(cmd);
  510. } else
  511. stex_invalid_field(cmd, done);
  512. return 0;
  513. case PASSTHRU_CMD:
  514. if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
  515. struct st_drvver ver;
  516. size_t cp_len = sizeof(ver);
  517. ver.major = ST_VER_MAJOR;
  518. ver.minor = ST_VER_MINOR;
  519. ver.oem = ST_OEM;
  520. ver.build = ST_BUILD_VER;
  521. ver.signature[0] = PASSTHRU_SIGNATURE;
  522. ver.console_id = host->max_id - 1;
  523. ver.host_no = hba->host->host_no;
  524. stex_internal_copy(cmd, &ver, &cp_len,
  525. scsi_sg_count(cmd), ST_TO_CMD);
  526. cmd->result = sizeof(ver) == cp_len ?
  527. DID_OK << 16 | COMMAND_COMPLETE << 8 :
  528. DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  529. done(cmd);
  530. return 0;
  531. }
  532. default:
  533. break;
  534. }
  535. cmd->scsi_done = done;
  536. tag = cmd->request->tag;
  537. if (unlikely(tag >= host->can_queue))
  538. return SCSI_MLQUEUE_HOST_BUSY;
  539. req = stex_alloc_req(hba);
  540. req->lun = lun;
  541. req->target = id;
  542. /* cdb */
  543. memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
  544. hba->ccb[tag].cmd = cmd;
  545. hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
  546. hba->ccb[tag].sense_buffer = cmd->sense_buffer;
  547. hba->ccb[tag].req_type = 0;
  548. if (cmd->sc_data_direction != DMA_NONE)
  549. stex_map_sg(hba, req, &hba->ccb[tag]);
  550. stex_send_cmd(hba, req, tag);
  551. return 0;
  552. }
  553. static void stex_scsi_done(struct st_ccb *ccb)
  554. {
  555. struct scsi_cmnd *cmd = ccb->cmd;
  556. int result;
  557. if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
  558. result = ccb->scsi_status;
  559. switch (ccb->scsi_status) {
  560. case SAM_STAT_GOOD:
  561. result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
  562. break;
  563. case SAM_STAT_CHECK_CONDITION:
  564. result |= DRIVER_SENSE << 24;
  565. break;
  566. case SAM_STAT_BUSY:
  567. result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  568. break;
  569. default:
  570. result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  571. break;
  572. }
  573. }
  574. else if (ccb->srb_status & SRB_SEE_SENSE)
  575. result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
  576. else switch (ccb->srb_status) {
  577. case SRB_STATUS_SELECTION_TIMEOUT:
  578. result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  579. break;
  580. case SRB_STATUS_BUSY:
  581. result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  582. break;
  583. case SRB_STATUS_INVALID_REQUEST:
  584. case SRB_STATUS_ERROR:
  585. default:
  586. result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  587. break;
  588. }
  589. cmd->result = result;
  590. cmd->scsi_done(cmd);
  591. }
  592. static void stex_copy_data(struct st_ccb *ccb,
  593. struct status_msg *resp, unsigned int variable)
  594. {
  595. size_t count = variable;
  596. if (resp->scsi_status != SAM_STAT_GOOD) {
  597. if (ccb->sense_buffer != NULL)
  598. memcpy(ccb->sense_buffer, resp->variable,
  599. min(variable, ccb->sense_bufflen));
  600. return;
  601. }
  602. if (ccb->cmd == NULL)
  603. return;
  604. stex_internal_copy(ccb->cmd,
  605. resp->variable, &count, scsi_sg_count(ccb->cmd), ST_TO_CMD);
  606. }
  607. static void stex_ys_commands(struct st_hba *hba,
  608. struct st_ccb *ccb, struct status_msg *resp)
  609. {
  610. size_t count;
  611. if (ccb->cmd->cmnd[0] == MGT_CMD &&
  612. resp->scsi_status != SAM_STAT_CHECK_CONDITION) {
  613. scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
  614. le32_to_cpu(*(__le32 *)&resp->variable[0]));
  615. return;
  616. }
  617. if (resp->srb_status != 0)
  618. return;
  619. /* determine inquiry command status by DeviceTypeQualifier */
  620. if (ccb->cmd->cmnd[0] == INQUIRY &&
  621. resp->scsi_status == SAM_STAT_GOOD) {
  622. ST_INQ *inq_data;
  623. count = STEX_EXTRA_SIZE;
  624. stex_internal_copy(ccb->cmd, hba->copy_buffer,
  625. &count, scsi_sg_count(ccb->cmd), ST_FROM_CMD);
  626. inq_data = (ST_INQ *)hba->copy_buffer;
  627. if (inq_data->DeviceTypeQualifier != 0)
  628. ccb->srb_status = SRB_STATUS_SELECTION_TIMEOUT;
  629. else
  630. ccb->srb_status = SRB_STATUS_SUCCESS;
  631. }
  632. }
  633. static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
  634. {
  635. void __iomem *base = hba->mmio_base;
  636. struct status_msg *resp;
  637. struct st_ccb *ccb;
  638. unsigned int size;
  639. u16 tag;
  640. if (!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED))
  641. return;
  642. /* status payloads */
  643. hba->status_head = readl(base + OMR1);
  644. if (unlikely(hba->status_head >= MU_STATUS_COUNT)) {
  645. printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
  646. pci_name(hba->pdev));
  647. return;
  648. }
  649. /*
  650. * it's not a valid status payload if:
  651. * 1. there are no pending requests(e.g. during init stage)
  652. * 2. there are some pending requests, but the controller is in
  653. * reset status, and its type is not st_yosemite
  654. * firmware of st_yosemite in reset status will return pending requests
  655. * to driver, so we allow it to pass
  656. */
  657. if (unlikely(hba->out_req_cnt <= 0 ||
  658. (hba->mu_status == MU_STATE_RESETTING &&
  659. hba->cardtype != st_yosemite))) {
  660. hba->status_tail = hba->status_head;
  661. goto update_status;
  662. }
  663. while (hba->status_tail != hba->status_head) {
  664. resp = stex_get_status(hba);
  665. tag = le16_to_cpu(resp->tag);
  666. if (unlikely(tag >= hba->host->can_queue)) {
  667. printk(KERN_WARNING DRV_NAME
  668. "(%s): invalid tag\n", pci_name(hba->pdev));
  669. continue;
  670. }
  671. ccb = &hba->ccb[tag];
  672. if (hba->wait_ccb == ccb)
  673. hba->wait_ccb = NULL;
  674. if (unlikely(ccb->req == NULL)) {
  675. printk(KERN_WARNING DRV_NAME
  676. "(%s): lagging req\n", pci_name(hba->pdev));
  677. hba->out_req_cnt--;
  678. continue;
  679. }
  680. size = resp->payload_sz * sizeof(u32); /* payload size */
  681. if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
  682. size > sizeof(*resp))) {
  683. printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
  684. pci_name(hba->pdev));
  685. } else {
  686. size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
  687. if (size)
  688. stex_copy_data(ccb, resp, size);
  689. }
  690. ccb->srb_status = resp->srb_status;
  691. ccb->scsi_status = resp->scsi_status;
  692. if (likely(ccb->cmd != NULL)) {
  693. if (hba->cardtype == st_yosemite)
  694. stex_ys_commands(hba, ccb, resp);
  695. if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
  696. ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
  697. stex_controller_info(hba, ccb);
  698. scsi_dma_unmap(ccb->cmd);
  699. stex_scsi_done(ccb);
  700. hba->out_req_cnt--;
  701. } else if (ccb->req_type & PASSTHRU_REQ_TYPE) {
  702. hba->out_req_cnt--;
  703. if (ccb->req_type & PASSTHRU_REQ_NO_WAKEUP) {
  704. ccb->req_type = 0;
  705. continue;
  706. }
  707. ccb->req_type = 0;
  708. if (waitqueue_active(&hba->waitq))
  709. wake_up(&hba->waitq);
  710. }
  711. }
  712. update_status:
  713. writel(hba->status_head, base + IMR1);
  714. readl(base + IMR1); /* flush */
  715. }
  716. static irqreturn_t stex_intr(int irq, void *__hba)
  717. {
  718. struct st_hba *hba = __hba;
  719. void __iomem *base = hba->mmio_base;
  720. u32 data;
  721. unsigned long flags;
  722. int handled = 0;
  723. spin_lock_irqsave(hba->host->host_lock, flags);
  724. data = readl(base + ODBL);
  725. if (data && data != 0xffffffff) {
  726. /* clear the interrupt */
  727. writel(data, base + ODBL);
  728. readl(base + ODBL); /* flush */
  729. stex_mu_intr(hba, data);
  730. handled = 1;
  731. }
  732. spin_unlock_irqrestore(hba->host->host_lock, flags);
  733. return IRQ_RETVAL(handled);
  734. }
  735. static int stex_handshake(struct st_hba *hba)
  736. {
  737. void __iomem *base = hba->mmio_base;
  738. struct handshake_frame *h;
  739. dma_addr_t status_phys;
  740. u32 data;
  741. unsigned long before;
  742. if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  743. writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
  744. readl(base + IDBL);
  745. before = jiffies;
  746. while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  747. if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
  748. printk(KERN_ERR DRV_NAME
  749. "(%s): no handshake signature\n",
  750. pci_name(hba->pdev));
  751. return -1;
  752. }
  753. rmb();
  754. msleep(1);
  755. }
  756. }
  757. udelay(10);
  758. data = readl(base + OMR1);
  759. if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
  760. data &= 0x0000ffff;
  761. if (hba->host->can_queue > data)
  762. hba->host->can_queue = data;
  763. }
  764. h = (struct handshake_frame *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
  765. h->rb_phy = cpu_to_le32(hba->dma_handle);
  766. h->rb_phy_hi = cpu_to_le32((hba->dma_handle >> 16) >> 16);
  767. h->req_sz = cpu_to_le16(sizeof(struct req_msg));
  768. h->req_cnt = cpu_to_le16(MU_REQ_COUNT);
  769. h->status_sz = cpu_to_le16(sizeof(struct status_msg));
  770. h->status_cnt = cpu_to_le16(MU_STATUS_COUNT);
  771. stex_gettime(&h->hosttime);
  772. h->partner_type = HMU_PARTNER_TYPE;
  773. if (hba->dma_size > STEX_BUFFER_SIZE) {
  774. h->extra_offset = cpu_to_le32(STEX_BUFFER_SIZE);
  775. h->extra_size = cpu_to_le32(ST_ADDITIONAL_MEM);
  776. } else
  777. h->extra_offset = h->extra_size = 0;
  778. status_phys = hba->dma_handle + MU_REQ_BUFFER_SIZE;
  779. writel(status_phys, base + IMR0);
  780. readl(base + IMR0);
  781. writel((status_phys >> 16) >> 16, base + IMR1);
  782. readl(base + IMR1);
  783. writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
  784. readl(base + OMR0);
  785. writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
  786. readl(base + IDBL); /* flush */
  787. udelay(10);
  788. before = jiffies;
  789. while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
  790. if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
  791. printk(KERN_ERR DRV_NAME
  792. "(%s): no signature after handshake frame\n",
  793. pci_name(hba->pdev));
  794. return -1;
  795. }
  796. rmb();
  797. msleep(1);
  798. }
  799. writel(0, base + IMR0);
  800. readl(base + IMR0);
  801. writel(0, base + OMR0);
  802. readl(base + OMR0);
  803. writel(0, base + IMR1);
  804. readl(base + IMR1);
  805. writel(0, base + OMR1);
  806. readl(base + OMR1); /* flush */
  807. hba->mu_status = MU_STATE_STARTED;
  808. return 0;
  809. }
  810. static int stex_abort(struct scsi_cmnd *cmd)
  811. {
  812. struct Scsi_Host *host = cmd->device->host;
  813. struct st_hba *hba = (struct st_hba *)host->hostdata;
  814. u16 tag = cmd->request->tag;
  815. void __iomem *base;
  816. u32 data;
  817. int result = SUCCESS;
  818. unsigned long flags;
  819. printk(KERN_INFO DRV_NAME
  820. "(%s): aborting command\n", pci_name(hba->pdev));
  821. scsi_print_command(cmd);
  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. /*
  871. * 1 ms may be enough for 8-port controllers. But 16-port controllers
  872. * require more time to finish bus reset. Use 100 ms here for safety
  873. */
  874. msleep(100);
  875. pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  876. pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
  877. for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
  878. pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
  879. if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
  880. break;
  881. msleep(1);
  882. }
  883. ssleep(5);
  884. for (i = 0; i < 16; i++)
  885. pci_write_config_dword(hba->pdev, i * 4,
  886. hba->pdev->saved_config_space[i]);
  887. }
  888. static int stex_reset(struct scsi_cmnd *cmd)
  889. {
  890. struct st_hba *hba;
  891. unsigned long flags;
  892. unsigned long before;
  893. hba = (struct st_hba *) &cmd->device->host->hostdata[0];
  894. printk(KERN_INFO DRV_NAME
  895. "(%s): resetting host\n", pci_name(hba->pdev));
  896. scsi_print_command(cmd);
  897. hba->mu_status = MU_STATE_RESETTING;
  898. if (hba->cardtype == st_shasta)
  899. stex_hard_reset(hba);
  900. if (hba->cardtype != st_yosemite) {
  901. if (stex_handshake(hba)) {
  902. printk(KERN_WARNING DRV_NAME
  903. "(%s): resetting: handshake failed\n",
  904. pci_name(hba->pdev));
  905. return FAILED;
  906. }
  907. spin_lock_irqsave(hba->host->host_lock, flags);
  908. hba->req_head = 0;
  909. hba->req_tail = 0;
  910. hba->status_head = 0;
  911. hba->status_tail = 0;
  912. hba->out_req_cnt = 0;
  913. spin_unlock_irqrestore(hba->host->host_lock, flags);
  914. return SUCCESS;
  915. }
  916. /* st_yosemite */
  917. writel(MU_INBOUND_DOORBELL_RESET, hba->mmio_base + IDBL);
  918. readl(hba->mmio_base + IDBL); /* flush */
  919. before = jiffies;
  920. while (hba->out_req_cnt > 0) {
  921. if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
  922. printk(KERN_WARNING DRV_NAME
  923. "(%s): reset timeout\n", pci_name(hba->pdev));
  924. return FAILED;
  925. }
  926. msleep(1);
  927. }
  928. hba->mu_status = MU_STATE_STARTED;
  929. return SUCCESS;
  930. }
  931. static int stex_biosparam(struct scsi_device *sdev,
  932. struct block_device *bdev, sector_t capacity, int geom[])
  933. {
  934. int heads = 255, sectors = 63;
  935. if (capacity < 0x200000) {
  936. heads = 64;
  937. sectors = 32;
  938. }
  939. sector_div(capacity, heads * sectors);
  940. geom[0] = heads;
  941. geom[1] = sectors;
  942. geom[2] = capacity;
  943. return 0;
  944. }
  945. static struct scsi_host_template driver_template = {
  946. .module = THIS_MODULE,
  947. .name = DRV_NAME,
  948. .proc_name = DRV_NAME,
  949. .bios_param = stex_biosparam,
  950. .queuecommand = stex_queuecommand,
  951. .slave_alloc = stex_slave_alloc,
  952. .slave_configure = stex_slave_config,
  953. .slave_destroy = stex_slave_destroy,
  954. .eh_abort_handler = stex_abort,
  955. .eh_host_reset_handler = stex_reset,
  956. .can_queue = ST_CAN_QUEUE,
  957. .this_id = -1,
  958. .sg_tablesize = ST_MAX_SG,
  959. .cmd_per_lun = ST_CMD_PER_LUN,
  960. };
  961. static int stex_set_dma_mask(struct pci_dev * pdev)
  962. {
  963. int ret;
  964. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)
  965. && !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
  966. return 0;
  967. ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  968. if (!ret)
  969. ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  970. return ret;
  971. }
  972. static int __devinit
  973. stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  974. {
  975. struct st_hba *hba;
  976. struct Scsi_Host *host;
  977. int err;
  978. err = pci_enable_device(pdev);
  979. if (err)
  980. return err;
  981. pci_set_master(pdev);
  982. host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
  983. if (!host) {
  984. printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
  985. pci_name(pdev));
  986. err = -ENOMEM;
  987. goto out_disable;
  988. }
  989. hba = (struct st_hba *)host->hostdata;
  990. memset(hba, 0, sizeof(struct st_hba));
  991. err = pci_request_regions(pdev, DRV_NAME);
  992. if (err < 0) {
  993. printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
  994. pci_name(pdev));
  995. goto out_scsi_host_put;
  996. }
  997. hba->mmio_base = ioremap_nocache(pci_resource_start(pdev, 0),
  998. pci_resource_len(pdev, 0));
  999. if ( !hba->mmio_base) {
  1000. printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
  1001. pci_name(pdev));
  1002. err = -ENOMEM;
  1003. goto out_release_regions;
  1004. }
  1005. err = stex_set_dma_mask(pdev);
  1006. if (err) {
  1007. printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
  1008. pci_name(pdev));
  1009. goto out_iounmap;
  1010. }
  1011. hba->cardtype = (unsigned int) id->driver_data;
  1012. if (hba->cardtype == st_vsc && (pdev->subsystem_device & 0xf) == 0x1)
  1013. hba->cardtype = st_vsc1;
  1014. hba->dma_size = (hba->cardtype == st_vsc1) ?
  1015. (STEX_BUFFER_SIZE + ST_ADDITIONAL_MEM) : (STEX_BUFFER_SIZE);
  1016. hba->dma_mem = dma_alloc_coherent(&pdev->dev,
  1017. hba->dma_size, &hba->dma_handle, GFP_KERNEL);
  1018. if (!hba->dma_mem) {
  1019. err = -ENOMEM;
  1020. printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
  1021. pci_name(pdev));
  1022. goto out_iounmap;
  1023. }
  1024. hba->status_buffer =
  1025. (struct status_msg *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
  1026. hba->copy_buffer = hba->dma_mem + MU_BUFFER_SIZE;
  1027. hba->mu_status = MU_STATE_STARTING;
  1028. if (hba->cardtype == st_shasta) {
  1029. host->max_lun = 8;
  1030. host->max_id = 16 + 1;
  1031. } else if (hba->cardtype == st_yosemite) {
  1032. host->max_lun = 128;
  1033. host->max_id = 1 + 1;
  1034. } else {
  1035. /* st_vsc and st_vsc1 */
  1036. host->max_lun = 1;
  1037. host->max_id = 128 + 1;
  1038. }
  1039. host->max_channel = 0;
  1040. host->unique_id = host->host_no;
  1041. host->max_cmd_len = STEX_CDB_LENGTH;
  1042. hba->host = host;
  1043. hba->pdev = pdev;
  1044. init_waitqueue_head(&hba->waitq);
  1045. err = request_irq(pdev->irq, stex_intr, IRQF_SHARED, DRV_NAME, hba);
  1046. if (err) {
  1047. printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
  1048. pci_name(pdev));
  1049. goto out_pci_free;
  1050. }
  1051. err = stex_handshake(hba);
  1052. if (err)
  1053. goto out_free_irq;
  1054. err = scsi_init_shared_tag_map(host, host->can_queue);
  1055. if (err) {
  1056. printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
  1057. pci_name(pdev));
  1058. goto out_free_irq;
  1059. }
  1060. pci_set_drvdata(pdev, hba);
  1061. err = scsi_add_host(host, &pdev->dev);
  1062. if (err) {
  1063. printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
  1064. pci_name(pdev));
  1065. goto out_free_irq;
  1066. }
  1067. scsi_scan_host(host);
  1068. return 0;
  1069. out_free_irq:
  1070. free_irq(pdev->irq, hba);
  1071. out_pci_free:
  1072. dma_free_coherent(&pdev->dev, hba->dma_size,
  1073. hba->dma_mem, hba->dma_handle);
  1074. out_iounmap:
  1075. iounmap(hba->mmio_base);
  1076. out_release_regions:
  1077. pci_release_regions(pdev);
  1078. out_scsi_host_put:
  1079. scsi_host_put(host);
  1080. out_disable:
  1081. pci_disable_device(pdev);
  1082. return err;
  1083. }
  1084. static void stex_hba_stop(struct st_hba *hba)
  1085. {
  1086. struct req_msg *req;
  1087. unsigned long flags;
  1088. unsigned long before;
  1089. u16 tag = 0;
  1090. spin_lock_irqsave(hba->host->host_lock, flags);
  1091. req = stex_alloc_req(hba);
  1092. memset(req->cdb, 0, STEX_CDB_LENGTH);
  1093. if (hba->cardtype == st_yosemite) {
  1094. req->cdb[0] = MGT_CMD;
  1095. req->cdb[1] = MGT_CMD_SIGNATURE;
  1096. req->cdb[2] = CTLR_CONFIG_CMD;
  1097. req->cdb[3] = CTLR_SHUTDOWN;
  1098. } else {
  1099. req->cdb[0] = CONTROLLER_CMD;
  1100. req->cdb[1] = CTLR_POWER_STATE_CHANGE;
  1101. req->cdb[2] = CTLR_POWER_SAVING;
  1102. }
  1103. hba->ccb[tag].cmd = NULL;
  1104. hba->ccb[tag].sg_count = 0;
  1105. hba->ccb[tag].sense_bufflen = 0;
  1106. hba->ccb[tag].sense_buffer = NULL;
  1107. hba->ccb[tag].req_type |= PASSTHRU_REQ_TYPE;
  1108. stex_send_cmd(hba, req, tag);
  1109. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1110. before = jiffies;
  1111. while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
  1112. if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ))
  1113. return;
  1114. msleep(10);
  1115. }
  1116. }
  1117. static void stex_hba_free(struct st_hba *hba)
  1118. {
  1119. free_irq(hba->pdev->irq, hba);
  1120. iounmap(hba->mmio_base);
  1121. pci_release_regions(hba->pdev);
  1122. dma_free_coherent(&hba->pdev->dev, hba->dma_size,
  1123. hba->dma_mem, hba->dma_handle);
  1124. }
  1125. static void stex_remove(struct pci_dev *pdev)
  1126. {
  1127. struct st_hba *hba = pci_get_drvdata(pdev);
  1128. scsi_remove_host(hba->host);
  1129. pci_set_drvdata(pdev, NULL);
  1130. stex_hba_stop(hba);
  1131. stex_hba_free(hba);
  1132. scsi_host_put(hba->host);
  1133. pci_disable_device(pdev);
  1134. }
  1135. static void stex_shutdown(struct pci_dev *pdev)
  1136. {
  1137. struct st_hba *hba = pci_get_drvdata(pdev);
  1138. stex_hba_stop(hba);
  1139. }
  1140. static struct pci_device_id stex_pci_tbl[] = {
  1141. /* st_shasta */
  1142. { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1143. st_shasta }, /* SuperTrak EX8350/8300/16350/16300 */
  1144. { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1145. st_shasta }, /* SuperTrak EX12350 */
  1146. { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1147. st_shasta }, /* SuperTrak EX4350 */
  1148. { 0x105a, 0xe350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1149. st_shasta }, /* SuperTrak EX24350 */
  1150. /* st_vsc */
  1151. { 0x105a, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
  1152. /* st_yosemite */
  1153. { 0x105a, 0x8650, PCI_ANY_ID, 0x4600, 0, 0,
  1154. st_yosemite }, /* SuperTrak EX4650 */
  1155. { 0x105a, 0x8650, PCI_ANY_ID, 0x4610, 0, 0,
  1156. st_yosemite }, /* SuperTrak EX4650o */
  1157. { 0x105a, 0x8650, PCI_ANY_ID, 0x8600, 0, 0,
  1158. st_yosemite }, /* SuperTrak EX8650EL */
  1159. { 0x105a, 0x8650, PCI_ANY_ID, 0x8601, 0, 0,
  1160. st_yosemite }, /* SuperTrak EX8650 */
  1161. { 0x105a, 0x8650, PCI_ANY_ID, 0x8602, 0, 0,
  1162. st_yosemite }, /* SuperTrak EX8654 */
  1163. { 0x105a, 0x8650, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  1164. st_yosemite }, /* generic st_yosemite */
  1165. { } /* terminate list */
  1166. };
  1167. MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
  1168. static struct pci_driver stex_pci_driver = {
  1169. .name = DRV_NAME,
  1170. .id_table = stex_pci_tbl,
  1171. .probe = stex_probe,
  1172. .remove = __devexit_p(stex_remove),
  1173. .shutdown = stex_shutdown,
  1174. };
  1175. static int __init stex_init(void)
  1176. {
  1177. printk(KERN_INFO DRV_NAME
  1178. ": Promise SuperTrak EX Driver version: %s\n",
  1179. ST_DRIVER_VERSION);
  1180. return pci_register_driver(&stex_pci_driver);
  1181. }
  1182. static void __exit stex_exit(void)
  1183. {
  1184. pci_unregister_driver(&stex_pci_driver);
  1185. }
  1186. module_init(stex_init);
  1187. module_exit(stex_exit);