stex.c 33 KB

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