stex.c 32 KB

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