netxen_nic_ctx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright (C) 2003 - 2009 NetXen, Inc.
  3. * Copyright (C) 2009 - QLogic Corporation.
  4. * All rights reserved.
  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 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  19. * MA 02111-1307, USA.
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called "COPYING".
  23. *
  24. */
  25. #include "netxen_nic_hw.h"
  26. #include "netxen_nic.h"
  27. #define NXHAL_VERSION 1
  28. static u32
  29. netxen_poll_rsp(struct netxen_adapter *adapter)
  30. {
  31. u32 rsp = NX_CDRP_RSP_OK;
  32. int timeout = 0;
  33. do {
  34. /* give atleast 1ms for firmware to respond */
  35. msleep(1);
  36. if (++timeout > NX_OS_CRB_RETRY_COUNT)
  37. return NX_CDRP_RSP_TIMEOUT;
  38. rsp = NXRD32(adapter, NX_CDRP_CRB_OFFSET);
  39. } while (!NX_CDRP_IS_RSP(rsp));
  40. return rsp;
  41. }
  42. static u32
  43. netxen_issue_cmd(struct netxen_adapter *adapter,
  44. u32 pci_fn, u32 version, u32 arg1, u32 arg2, u32 arg3, u32 cmd)
  45. {
  46. u32 rsp;
  47. u32 signature = 0;
  48. u32 rcode = NX_RCODE_SUCCESS;
  49. signature = NX_CDRP_SIGNATURE_MAKE(pci_fn, version);
  50. /* Acquire semaphore before accessing CRB */
  51. if (netxen_api_lock(adapter))
  52. return NX_RCODE_TIMEOUT;
  53. NXWR32(adapter, NX_SIGN_CRB_OFFSET, signature);
  54. NXWR32(adapter, NX_ARG1_CRB_OFFSET, arg1);
  55. NXWR32(adapter, NX_ARG2_CRB_OFFSET, arg2);
  56. NXWR32(adapter, NX_ARG3_CRB_OFFSET, arg3);
  57. NXWR32(adapter, NX_CDRP_CRB_OFFSET, NX_CDRP_FORM_CMD(cmd));
  58. rsp = netxen_poll_rsp(adapter);
  59. if (rsp == NX_CDRP_RSP_TIMEOUT) {
  60. printk(KERN_ERR "%s: card response timeout.\n",
  61. netxen_nic_driver_name);
  62. rcode = NX_RCODE_TIMEOUT;
  63. } else if (rsp == NX_CDRP_RSP_FAIL) {
  64. rcode = NXRD32(adapter, NX_ARG1_CRB_OFFSET);
  65. printk(KERN_ERR "%s: failed card response code:0x%x\n",
  66. netxen_nic_driver_name, rcode);
  67. }
  68. /* Release semaphore */
  69. netxen_api_unlock(adapter);
  70. return rcode;
  71. }
  72. int
  73. nx_fw_cmd_set_mtu(struct netxen_adapter *adapter, int mtu)
  74. {
  75. u32 rcode = NX_RCODE_SUCCESS;
  76. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  77. if (recv_ctx->state == NX_HOST_CTX_STATE_ACTIVE)
  78. rcode = netxen_issue_cmd(adapter,
  79. adapter->ahw.pci_func,
  80. NXHAL_VERSION,
  81. recv_ctx->context_id,
  82. mtu,
  83. 0,
  84. NX_CDRP_CMD_SET_MTU);
  85. if (rcode != NX_RCODE_SUCCESS)
  86. return -EIO;
  87. return 0;
  88. }
  89. int
  90. nx_fw_cmd_set_gbe_port(struct netxen_adapter *adapter,
  91. u32 speed, u32 duplex, u32 autoneg)
  92. {
  93. return netxen_issue_cmd(adapter,
  94. adapter->ahw.pci_func,
  95. NXHAL_VERSION,
  96. speed,
  97. duplex,
  98. autoneg,
  99. NX_CDRP_CMD_CONFIG_GBE_PORT);
  100. }
  101. static int
  102. nx_fw_cmd_create_rx_ctx(struct netxen_adapter *adapter)
  103. {
  104. void *addr;
  105. nx_hostrq_rx_ctx_t *prq;
  106. nx_cardrsp_rx_ctx_t *prsp;
  107. nx_hostrq_rds_ring_t *prq_rds;
  108. nx_hostrq_sds_ring_t *prq_sds;
  109. nx_cardrsp_rds_ring_t *prsp_rds;
  110. nx_cardrsp_sds_ring_t *prsp_sds;
  111. struct nx_host_rds_ring *rds_ring;
  112. struct nx_host_sds_ring *sds_ring;
  113. dma_addr_t hostrq_phys_addr, cardrsp_phys_addr;
  114. u64 phys_addr;
  115. int i, nrds_rings, nsds_rings;
  116. size_t rq_size, rsp_size;
  117. u32 cap, reg, val;
  118. int err;
  119. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  120. nrds_rings = adapter->max_rds_rings;
  121. nsds_rings = adapter->max_sds_rings;
  122. rq_size =
  123. SIZEOF_HOSTRQ_RX(nx_hostrq_rx_ctx_t, nrds_rings, nsds_rings);
  124. rsp_size =
  125. SIZEOF_CARDRSP_RX(nx_cardrsp_rx_ctx_t, nrds_rings, nsds_rings);
  126. addr = pci_alloc_consistent(adapter->pdev,
  127. rq_size, &hostrq_phys_addr);
  128. if (addr == NULL)
  129. return -ENOMEM;
  130. prq = (nx_hostrq_rx_ctx_t *)addr;
  131. addr = pci_alloc_consistent(adapter->pdev,
  132. rsp_size, &cardrsp_phys_addr);
  133. if (addr == NULL) {
  134. err = -ENOMEM;
  135. goto out_free_rq;
  136. }
  137. prsp = (nx_cardrsp_rx_ctx_t *)addr;
  138. prq->host_rsp_dma_addr = cpu_to_le64(cardrsp_phys_addr);
  139. cap = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN);
  140. cap |= (NX_CAP0_JUMBO_CONTIGUOUS | NX_CAP0_LRO_CONTIGUOUS);
  141. prq->capabilities[0] = cpu_to_le32(cap);
  142. prq->host_int_crb_mode =
  143. cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
  144. prq->host_rds_crb_mode =
  145. cpu_to_le32(NX_HOST_RDS_CRB_MODE_UNIQUE);
  146. prq->num_rds_rings = cpu_to_le16(nrds_rings);
  147. prq->num_sds_rings = cpu_to_le16(nsds_rings);
  148. prq->rds_ring_offset = cpu_to_le32(0);
  149. val = le32_to_cpu(prq->rds_ring_offset) +
  150. (sizeof(nx_hostrq_rds_ring_t) * nrds_rings);
  151. prq->sds_ring_offset = cpu_to_le32(val);
  152. prq_rds = (nx_hostrq_rds_ring_t *)(prq->data +
  153. le32_to_cpu(prq->rds_ring_offset));
  154. for (i = 0; i < nrds_rings; i++) {
  155. rds_ring = &recv_ctx->rds_rings[i];
  156. prq_rds[i].host_phys_addr = cpu_to_le64(rds_ring->phys_addr);
  157. prq_rds[i].ring_size = cpu_to_le32(rds_ring->num_desc);
  158. prq_rds[i].ring_kind = cpu_to_le32(i);
  159. prq_rds[i].buff_size = cpu_to_le64(rds_ring->dma_size);
  160. }
  161. prq_sds = (nx_hostrq_sds_ring_t *)(prq->data +
  162. le32_to_cpu(prq->sds_ring_offset));
  163. for (i = 0; i < nsds_rings; i++) {
  164. sds_ring = &recv_ctx->sds_rings[i];
  165. prq_sds[i].host_phys_addr = cpu_to_le64(sds_ring->phys_addr);
  166. prq_sds[i].ring_size = cpu_to_le32(sds_ring->num_desc);
  167. prq_sds[i].msi_index = cpu_to_le16(i);
  168. }
  169. phys_addr = hostrq_phys_addr;
  170. err = netxen_issue_cmd(adapter,
  171. adapter->ahw.pci_func,
  172. NXHAL_VERSION,
  173. (u32)(phys_addr >> 32),
  174. (u32)(phys_addr & 0xffffffff),
  175. rq_size,
  176. NX_CDRP_CMD_CREATE_RX_CTX);
  177. if (err) {
  178. printk(KERN_WARNING
  179. "Failed to create rx ctx in firmware%d\n", err);
  180. goto out_free_rsp;
  181. }
  182. prsp_rds = ((nx_cardrsp_rds_ring_t *)
  183. &prsp->data[le32_to_cpu(prsp->rds_ring_offset)]);
  184. for (i = 0; i < le16_to_cpu(prsp->num_rds_rings); i++) {
  185. rds_ring = &recv_ctx->rds_rings[i];
  186. reg = le32_to_cpu(prsp_rds[i].host_producer_crb);
  187. rds_ring->crb_rcv_producer = netxen_get_ioaddr(adapter,
  188. NETXEN_NIC_REG(reg - 0x200));
  189. }
  190. prsp_sds = ((nx_cardrsp_sds_ring_t *)
  191. &prsp->data[le32_to_cpu(prsp->sds_ring_offset)]);
  192. for (i = 0; i < le16_to_cpu(prsp->num_sds_rings); i++) {
  193. sds_ring = &recv_ctx->sds_rings[i];
  194. reg = le32_to_cpu(prsp_sds[i].host_consumer_crb);
  195. sds_ring->crb_sts_consumer = netxen_get_ioaddr(adapter,
  196. NETXEN_NIC_REG(reg - 0x200));
  197. reg = le32_to_cpu(prsp_sds[i].interrupt_crb);
  198. sds_ring->crb_intr_mask = netxen_get_ioaddr(adapter,
  199. NETXEN_NIC_REG(reg - 0x200));
  200. }
  201. recv_ctx->state = le32_to_cpu(prsp->host_ctx_state);
  202. recv_ctx->context_id = le16_to_cpu(prsp->context_id);
  203. recv_ctx->virt_port = prsp->virt_port;
  204. out_free_rsp:
  205. pci_free_consistent(adapter->pdev, rsp_size, prsp, cardrsp_phys_addr);
  206. out_free_rq:
  207. pci_free_consistent(adapter->pdev, rq_size, prq, hostrq_phys_addr);
  208. return err;
  209. }
  210. static void
  211. nx_fw_cmd_destroy_rx_ctx(struct netxen_adapter *adapter)
  212. {
  213. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  214. if (netxen_issue_cmd(adapter,
  215. adapter->ahw.pci_func,
  216. NXHAL_VERSION,
  217. recv_ctx->context_id,
  218. NX_DESTROY_CTX_RESET,
  219. 0,
  220. NX_CDRP_CMD_DESTROY_RX_CTX)) {
  221. printk(KERN_WARNING
  222. "%s: Failed to destroy rx ctx in firmware\n",
  223. netxen_nic_driver_name);
  224. }
  225. }
  226. static int
  227. nx_fw_cmd_create_tx_ctx(struct netxen_adapter *adapter)
  228. {
  229. nx_hostrq_tx_ctx_t *prq;
  230. nx_hostrq_cds_ring_t *prq_cds;
  231. nx_cardrsp_tx_ctx_t *prsp;
  232. void *rq_addr, *rsp_addr;
  233. size_t rq_size, rsp_size;
  234. u32 temp;
  235. int err = 0;
  236. u64 offset, phys_addr;
  237. dma_addr_t rq_phys_addr, rsp_phys_addr;
  238. struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
  239. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  240. rq_size = SIZEOF_HOSTRQ_TX(nx_hostrq_tx_ctx_t);
  241. rq_addr = pci_alloc_consistent(adapter->pdev,
  242. rq_size, &rq_phys_addr);
  243. if (!rq_addr)
  244. return -ENOMEM;
  245. rsp_size = SIZEOF_CARDRSP_TX(nx_cardrsp_tx_ctx_t);
  246. rsp_addr = pci_alloc_consistent(adapter->pdev,
  247. rsp_size, &rsp_phys_addr);
  248. if (!rsp_addr) {
  249. err = -ENOMEM;
  250. goto out_free_rq;
  251. }
  252. memset(rq_addr, 0, rq_size);
  253. prq = (nx_hostrq_tx_ctx_t *)rq_addr;
  254. memset(rsp_addr, 0, rsp_size);
  255. prsp = (nx_cardrsp_tx_ctx_t *)rsp_addr;
  256. prq->host_rsp_dma_addr = cpu_to_le64(rsp_phys_addr);
  257. temp = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN | NX_CAP0_LSO);
  258. prq->capabilities[0] = cpu_to_le32(temp);
  259. prq->host_int_crb_mode =
  260. cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
  261. prq->interrupt_ctl = 0;
  262. prq->msi_index = 0;
  263. prq->dummy_dma_addr = cpu_to_le64(adapter->dummy_dma.phys_addr);
  264. offset = recv_ctx->phys_addr + sizeof(struct netxen_ring_ctx);
  265. prq->cmd_cons_dma_addr = cpu_to_le64(offset);
  266. prq_cds = &prq->cds_ring;
  267. prq_cds->host_phys_addr = cpu_to_le64(tx_ring->phys_addr);
  268. prq_cds->ring_size = cpu_to_le32(tx_ring->num_desc);
  269. phys_addr = rq_phys_addr;
  270. err = netxen_issue_cmd(adapter,
  271. adapter->ahw.pci_func,
  272. NXHAL_VERSION,
  273. (u32)(phys_addr >> 32),
  274. ((u32)phys_addr & 0xffffffff),
  275. rq_size,
  276. NX_CDRP_CMD_CREATE_TX_CTX);
  277. if (err == NX_RCODE_SUCCESS) {
  278. temp = le32_to_cpu(prsp->cds_ring.host_producer_crb);
  279. tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
  280. NETXEN_NIC_REG(temp - 0x200));
  281. #if 0
  282. adapter->tx_state =
  283. le32_to_cpu(prsp->host_ctx_state);
  284. #endif
  285. adapter->tx_context_id =
  286. le16_to_cpu(prsp->context_id);
  287. } else {
  288. printk(KERN_WARNING
  289. "Failed to create tx ctx in firmware%d\n", err);
  290. err = -EIO;
  291. }
  292. pci_free_consistent(adapter->pdev, rsp_size, rsp_addr, rsp_phys_addr);
  293. out_free_rq:
  294. pci_free_consistent(adapter->pdev, rq_size, rq_addr, rq_phys_addr);
  295. return err;
  296. }
  297. static void
  298. nx_fw_cmd_destroy_tx_ctx(struct netxen_adapter *adapter)
  299. {
  300. if (netxen_issue_cmd(adapter,
  301. adapter->ahw.pci_func,
  302. NXHAL_VERSION,
  303. adapter->tx_context_id,
  304. NX_DESTROY_CTX_RESET,
  305. 0,
  306. NX_CDRP_CMD_DESTROY_TX_CTX)) {
  307. printk(KERN_WARNING
  308. "%s: Failed to destroy tx ctx in firmware\n",
  309. netxen_nic_driver_name);
  310. }
  311. }
  312. int
  313. nx_fw_cmd_query_phy(struct netxen_adapter *adapter, u32 reg, u32 *val)
  314. {
  315. u32 rcode;
  316. rcode = netxen_issue_cmd(adapter,
  317. adapter->ahw.pci_func,
  318. NXHAL_VERSION,
  319. reg,
  320. 0,
  321. 0,
  322. NX_CDRP_CMD_READ_PHY);
  323. if (rcode != NX_RCODE_SUCCESS)
  324. return -EIO;
  325. return NXRD32(adapter, NX_ARG1_CRB_OFFSET);
  326. }
  327. int
  328. nx_fw_cmd_set_phy(struct netxen_adapter *adapter, u32 reg, u32 val)
  329. {
  330. u32 rcode;
  331. rcode = netxen_issue_cmd(adapter,
  332. adapter->ahw.pci_func,
  333. NXHAL_VERSION,
  334. reg,
  335. val,
  336. 0,
  337. NX_CDRP_CMD_WRITE_PHY);
  338. if (rcode != NX_RCODE_SUCCESS)
  339. return -EIO;
  340. return 0;
  341. }
  342. static u64 ctx_addr_sig_regs[][3] = {
  343. {NETXEN_NIC_REG(0x188), NETXEN_NIC_REG(0x18c), NETXEN_NIC_REG(0x1c0)},
  344. {NETXEN_NIC_REG(0x190), NETXEN_NIC_REG(0x194), NETXEN_NIC_REG(0x1c4)},
  345. {NETXEN_NIC_REG(0x198), NETXEN_NIC_REG(0x19c), NETXEN_NIC_REG(0x1c8)},
  346. {NETXEN_NIC_REG(0x1a0), NETXEN_NIC_REG(0x1a4), NETXEN_NIC_REG(0x1cc)}
  347. };
  348. #define CRB_CTX_ADDR_REG_LO(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][0])
  349. #define CRB_CTX_ADDR_REG_HI(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][2])
  350. #define CRB_CTX_SIGNATURE_REG(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][1])
  351. #define lower32(x) ((u32)((x) & 0xffffffff))
  352. #define upper32(x) ((u32)(((u64)(x) >> 32) & 0xffffffff))
  353. static struct netxen_recv_crb recv_crb_registers[] = {
  354. /* Instance 0 */
  355. {
  356. /* crb_rcv_producer: */
  357. {
  358. NETXEN_NIC_REG(0x100),
  359. /* Jumbo frames */
  360. NETXEN_NIC_REG(0x110),
  361. /* LRO */
  362. NETXEN_NIC_REG(0x120)
  363. },
  364. /* crb_sts_consumer: */
  365. {
  366. NETXEN_NIC_REG(0x138),
  367. NETXEN_NIC_REG_2(0x000),
  368. NETXEN_NIC_REG_2(0x004),
  369. NETXEN_NIC_REG_2(0x008),
  370. },
  371. /* sw_int_mask */
  372. {
  373. CRB_SW_INT_MASK_0,
  374. NETXEN_NIC_REG_2(0x044),
  375. NETXEN_NIC_REG_2(0x048),
  376. NETXEN_NIC_REG_2(0x04c),
  377. },
  378. },
  379. /* Instance 1 */
  380. {
  381. /* crb_rcv_producer: */
  382. {
  383. NETXEN_NIC_REG(0x144),
  384. /* Jumbo frames */
  385. NETXEN_NIC_REG(0x154),
  386. /* LRO */
  387. NETXEN_NIC_REG(0x164)
  388. },
  389. /* crb_sts_consumer: */
  390. {
  391. NETXEN_NIC_REG(0x17c),
  392. NETXEN_NIC_REG_2(0x020),
  393. NETXEN_NIC_REG_2(0x024),
  394. NETXEN_NIC_REG_2(0x028),
  395. },
  396. /* sw_int_mask */
  397. {
  398. CRB_SW_INT_MASK_1,
  399. NETXEN_NIC_REG_2(0x064),
  400. NETXEN_NIC_REG_2(0x068),
  401. NETXEN_NIC_REG_2(0x06c),
  402. },
  403. },
  404. /* Instance 2 */
  405. {
  406. /* crb_rcv_producer: */
  407. {
  408. NETXEN_NIC_REG(0x1d8),
  409. /* Jumbo frames */
  410. NETXEN_NIC_REG(0x1f8),
  411. /* LRO */
  412. NETXEN_NIC_REG(0x208)
  413. },
  414. /* crb_sts_consumer: */
  415. {
  416. NETXEN_NIC_REG(0x220),
  417. NETXEN_NIC_REG_2(0x03c),
  418. NETXEN_NIC_REG_2(0x03c),
  419. NETXEN_NIC_REG_2(0x03c),
  420. },
  421. /* sw_int_mask */
  422. {
  423. CRB_SW_INT_MASK_2,
  424. NETXEN_NIC_REG_2(0x03c),
  425. NETXEN_NIC_REG_2(0x03c),
  426. NETXEN_NIC_REG_2(0x03c),
  427. },
  428. },
  429. /* Instance 3 */
  430. {
  431. /* crb_rcv_producer: */
  432. {
  433. NETXEN_NIC_REG(0x22c),
  434. /* Jumbo frames */
  435. NETXEN_NIC_REG(0x23c),
  436. /* LRO */
  437. NETXEN_NIC_REG(0x24c)
  438. },
  439. /* crb_sts_consumer: */
  440. {
  441. NETXEN_NIC_REG(0x264),
  442. NETXEN_NIC_REG_2(0x03c),
  443. NETXEN_NIC_REG_2(0x03c),
  444. NETXEN_NIC_REG_2(0x03c),
  445. },
  446. /* sw_int_mask */
  447. {
  448. CRB_SW_INT_MASK_3,
  449. NETXEN_NIC_REG_2(0x03c),
  450. NETXEN_NIC_REG_2(0x03c),
  451. NETXEN_NIC_REG_2(0x03c),
  452. },
  453. },
  454. };
  455. static int
  456. netxen_init_old_ctx(struct netxen_adapter *adapter)
  457. {
  458. struct netxen_recv_context *recv_ctx;
  459. struct nx_host_rds_ring *rds_ring;
  460. struct nx_host_sds_ring *sds_ring;
  461. struct nx_host_tx_ring *tx_ring;
  462. int ring;
  463. int port = adapter->portnum;
  464. struct netxen_ring_ctx *hwctx;
  465. u32 signature;
  466. tx_ring = adapter->tx_ring;
  467. recv_ctx = &adapter->recv_ctx;
  468. hwctx = recv_ctx->hwctx;
  469. hwctx->cmd_ring_addr = cpu_to_le64(tx_ring->phys_addr);
  470. hwctx->cmd_ring_size = cpu_to_le32(tx_ring->num_desc);
  471. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  472. rds_ring = &recv_ctx->rds_rings[ring];
  473. hwctx->rcv_rings[ring].addr =
  474. cpu_to_le64(rds_ring->phys_addr);
  475. hwctx->rcv_rings[ring].size =
  476. cpu_to_le32(rds_ring->num_desc);
  477. }
  478. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  479. sds_ring = &recv_ctx->sds_rings[ring];
  480. if (ring == 0) {
  481. hwctx->sts_ring_addr = cpu_to_le64(sds_ring->phys_addr);
  482. hwctx->sts_ring_size = cpu_to_le32(sds_ring->num_desc);
  483. }
  484. hwctx->sts_rings[ring].addr = cpu_to_le64(sds_ring->phys_addr);
  485. hwctx->sts_rings[ring].size = cpu_to_le32(sds_ring->num_desc);
  486. hwctx->sts_rings[ring].msi_index = cpu_to_le16(ring);
  487. }
  488. hwctx->sts_ring_count = cpu_to_le32(adapter->max_sds_rings);
  489. signature = (adapter->max_sds_rings > 1) ?
  490. NETXEN_CTX_SIGNATURE_V2 : NETXEN_CTX_SIGNATURE;
  491. NXWR32(adapter, CRB_CTX_ADDR_REG_LO(port),
  492. lower32(recv_ctx->phys_addr));
  493. NXWR32(adapter, CRB_CTX_ADDR_REG_HI(port),
  494. upper32(recv_ctx->phys_addr));
  495. NXWR32(adapter, CRB_CTX_SIGNATURE_REG(port),
  496. signature | port);
  497. return 0;
  498. }
  499. int netxen_alloc_hw_resources(struct netxen_adapter *adapter)
  500. {
  501. void *addr;
  502. int err = 0;
  503. int ring;
  504. struct netxen_recv_context *recv_ctx;
  505. struct nx_host_rds_ring *rds_ring;
  506. struct nx_host_sds_ring *sds_ring;
  507. struct nx_host_tx_ring *tx_ring;
  508. struct pci_dev *pdev = adapter->pdev;
  509. struct net_device *netdev = adapter->netdev;
  510. int port = adapter->portnum;
  511. recv_ctx = &adapter->recv_ctx;
  512. tx_ring = adapter->tx_ring;
  513. addr = pci_alloc_consistent(pdev,
  514. sizeof(struct netxen_ring_ctx) + sizeof(uint32_t),
  515. &recv_ctx->phys_addr);
  516. if (addr == NULL) {
  517. dev_err(&pdev->dev, "failed to allocate hw context\n");
  518. return -ENOMEM;
  519. }
  520. memset(addr, 0, sizeof(struct netxen_ring_ctx));
  521. recv_ctx->hwctx = (struct netxen_ring_ctx *)addr;
  522. recv_ctx->hwctx->ctx_id = cpu_to_le32(port);
  523. recv_ctx->hwctx->cmd_consumer_offset =
  524. cpu_to_le64(recv_ctx->phys_addr +
  525. sizeof(struct netxen_ring_ctx));
  526. tx_ring->hw_consumer =
  527. (__le32 *)(((char *)addr) + sizeof(struct netxen_ring_ctx));
  528. /* cmd desc ring */
  529. addr = pci_alloc_consistent(pdev, TX_DESC_RINGSIZE(tx_ring),
  530. &tx_ring->phys_addr);
  531. if (addr == NULL) {
  532. dev_err(&pdev->dev, "%s: failed to allocate tx desc ring\n",
  533. netdev->name);
  534. err = -ENOMEM;
  535. goto err_out_free;
  536. }
  537. tx_ring->desc_head = (struct cmd_desc_type0 *)addr;
  538. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  539. rds_ring = &recv_ctx->rds_rings[ring];
  540. addr = pci_alloc_consistent(adapter->pdev,
  541. RCV_DESC_RINGSIZE(rds_ring),
  542. &rds_ring->phys_addr);
  543. if (addr == NULL) {
  544. dev_err(&pdev->dev,
  545. "%s: failed to allocate rds ring [%d]\n",
  546. netdev->name, ring);
  547. err = -ENOMEM;
  548. goto err_out_free;
  549. }
  550. rds_ring->desc_head = (struct rcv_desc *)addr;
  551. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  552. rds_ring->crb_rcv_producer =
  553. netxen_get_ioaddr(adapter,
  554. recv_crb_registers[port].crb_rcv_producer[ring]);
  555. }
  556. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  557. sds_ring = &recv_ctx->sds_rings[ring];
  558. addr = pci_alloc_consistent(adapter->pdev,
  559. STATUS_DESC_RINGSIZE(sds_ring),
  560. &sds_ring->phys_addr);
  561. if (addr == NULL) {
  562. dev_err(&pdev->dev,
  563. "%s: failed to allocate sds ring [%d]\n",
  564. netdev->name, ring);
  565. err = -ENOMEM;
  566. goto err_out_free;
  567. }
  568. sds_ring->desc_head = (struct status_desc *)addr;
  569. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  570. sds_ring->crb_sts_consumer =
  571. netxen_get_ioaddr(adapter,
  572. recv_crb_registers[port].crb_sts_consumer[ring]);
  573. sds_ring->crb_intr_mask =
  574. netxen_get_ioaddr(adapter,
  575. recv_crb_registers[port].sw_int_mask[ring]);
  576. }
  577. }
  578. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  579. if (test_and_set_bit(__NX_FW_ATTACHED, &adapter->state))
  580. goto done;
  581. err = nx_fw_cmd_create_rx_ctx(adapter);
  582. if (err)
  583. goto err_out_free;
  584. err = nx_fw_cmd_create_tx_ctx(adapter);
  585. if (err)
  586. goto err_out_free;
  587. } else {
  588. err = netxen_init_old_ctx(adapter);
  589. if (err)
  590. goto err_out_free;
  591. }
  592. done:
  593. return 0;
  594. err_out_free:
  595. netxen_free_hw_resources(adapter);
  596. return err;
  597. }
  598. void netxen_free_hw_resources(struct netxen_adapter *adapter)
  599. {
  600. struct netxen_recv_context *recv_ctx;
  601. struct nx_host_rds_ring *rds_ring;
  602. struct nx_host_sds_ring *sds_ring;
  603. struct nx_host_tx_ring *tx_ring;
  604. int ring;
  605. int port = adapter->portnum;
  606. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  607. if (!test_and_clear_bit(__NX_FW_ATTACHED, &adapter->state))
  608. goto done;
  609. nx_fw_cmd_destroy_rx_ctx(adapter);
  610. nx_fw_cmd_destroy_tx_ctx(adapter);
  611. } else {
  612. netxen_api_lock(adapter);
  613. NXWR32(adapter, CRB_CTX_SIGNATURE_REG(port),
  614. NETXEN_CTX_D3_RESET | port);
  615. netxen_api_unlock(adapter);
  616. }
  617. /* Allow dma queues to drain after context reset */
  618. msleep(20);
  619. done:
  620. recv_ctx = &adapter->recv_ctx;
  621. if (recv_ctx->hwctx != NULL) {
  622. pci_free_consistent(adapter->pdev,
  623. sizeof(struct netxen_ring_ctx) +
  624. sizeof(uint32_t),
  625. recv_ctx->hwctx,
  626. recv_ctx->phys_addr);
  627. recv_ctx->hwctx = NULL;
  628. }
  629. tx_ring = adapter->tx_ring;
  630. if (tx_ring->desc_head != NULL) {
  631. pci_free_consistent(adapter->pdev,
  632. TX_DESC_RINGSIZE(tx_ring),
  633. tx_ring->desc_head, tx_ring->phys_addr);
  634. tx_ring->desc_head = NULL;
  635. }
  636. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  637. rds_ring = &recv_ctx->rds_rings[ring];
  638. if (rds_ring->desc_head != NULL) {
  639. pci_free_consistent(adapter->pdev,
  640. RCV_DESC_RINGSIZE(rds_ring),
  641. rds_ring->desc_head,
  642. rds_ring->phys_addr);
  643. rds_ring->desc_head = NULL;
  644. }
  645. }
  646. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  647. sds_ring = &recv_ctx->sds_rings[ring];
  648. if (sds_ring->desc_head != NULL) {
  649. pci_free_consistent(adapter->pdev,
  650. STATUS_DESC_RINGSIZE(sds_ring),
  651. sds_ring->desc_head,
  652. sds_ring->phys_addr);
  653. sds_ring->desc_head = NULL;
  654. }
  655. }
  656. }