netxen_nic_ctx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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, see <http://www.gnu.org/licenses/>.
  18. *
  19. * The full GNU General Public License is included in this distribution
  20. * in the file called "COPYING".
  21. *
  22. */
  23. #include "netxen_nic_hw.h"
  24. #include "netxen_nic.h"
  25. #define NXHAL_VERSION 1
  26. static u32
  27. netxen_poll_rsp(struct netxen_adapter *adapter)
  28. {
  29. u32 rsp = NX_CDRP_RSP_OK;
  30. int timeout = 0;
  31. do {
  32. /* give atleast 1ms for firmware to respond */
  33. msleep(1);
  34. if (++timeout > NX_OS_CRB_RETRY_COUNT)
  35. return NX_CDRP_RSP_TIMEOUT;
  36. rsp = NXRD32(adapter, NX_CDRP_CRB_OFFSET);
  37. } while (!NX_CDRP_IS_RSP(rsp));
  38. return rsp;
  39. }
  40. static u32
  41. netxen_issue_cmd(struct netxen_adapter *adapter, struct netxen_cmd_args *cmd)
  42. {
  43. u32 rsp;
  44. u32 signature = 0;
  45. u32 rcode = NX_RCODE_SUCCESS;
  46. signature = NX_CDRP_SIGNATURE_MAKE(adapter->ahw.pci_func,
  47. NXHAL_VERSION);
  48. /* Acquire semaphore before accessing CRB */
  49. if (netxen_api_lock(adapter))
  50. return NX_RCODE_TIMEOUT;
  51. NXWR32(adapter, NX_SIGN_CRB_OFFSET, signature);
  52. NXWR32(adapter, NX_ARG1_CRB_OFFSET, cmd->req.arg1);
  53. NXWR32(adapter, NX_ARG2_CRB_OFFSET, cmd->req.arg2);
  54. NXWR32(adapter, NX_ARG3_CRB_OFFSET, cmd->req.arg3);
  55. NXWR32(adapter, NX_CDRP_CRB_OFFSET, NX_CDRP_FORM_CMD(cmd->req.cmd));
  56. rsp = netxen_poll_rsp(adapter);
  57. if (rsp == NX_CDRP_RSP_TIMEOUT) {
  58. printk(KERN_ERR "%s: card response timeout.\n",
  59. netxen_nic_driver_name);
  60. rcode = NX_RCODE_TIMEOUT;
  61. } else if (rsp == NX_CDRP_RSP_FAIL) {
  62. rcode = NXRD32(adapter, NX_ARG1_CRB_OFFSET);
  63. printk(KERN_ERR "%s: failed card response code:0x%x\n",
  64. netxen_nic_driver_name, rcode);
  65. } else if (rsp == NX_CDRP_RSP_OK) {
  66. cmd->rsp.cmd = NX_RCODE_SUCCESS;
  67. if (cmd->rsp.arg2)
  68. cmd->rsp.arg2 = NXRD32(adapter, NX_ARG2_CRB_OFFSET);
  69. if (cmd->rsp.arg3)
  70. cmd->rsp.arg3 = NXRD32(adapter, NX_ARG3_CRB_OFFSET);
  71. }
  72. if (cmd->rsp.arg1)
  73. cmd->rsp.arg1 = NXRD32(adapter, NX_ARG1_CRB_OFFSET);
  74. /* Release semaphore */
  75. netxen_api_unlock(adapter);
  76. return rcode;
  77. }
  78. static int
  79. netxen_get_minidump_template_size(struct netxen_adapter *adapter)
  80. {
  81. struct netxen_cmd_args cmd;
  82. memset(&cmd, 0, sizeof(cmd));
  83. cmd.req.cmd = NX_CDRP_CMD_TEMP_SIZE;
  84. memset(&cmd.rsp, 1, sizeof(struct _cdrp_cmd));
  85. netxen_issue_cmd(adapter, &cmd);
  86. if (cmd.rsp.cmd != NX_RCODE_SUCCESS) {
  87. dev_info(&adapter->pdev->dev,
  88. "Can't get template size %d\n", cmd.rsp.cmd);
  89. return -EIO;
  90. }
  91. adapter->mdump.md_template_size = cmd.rsp.arg2;
  92. adapter->mdump.md_template_ver = cmd.rsp.arg3;
  93. return 0;
  94. }
  95. static int
  96. netxen_get_minidump_template(struct netxen_adapter *adapter)
  97. {
  98. dma_addr_t md_template_addr;
  99. void *addr;
  100. u32 size;
  101. struct netxen_cmd_args cmd;
  102. size = adapter->mdump.md_template_size;
  103. if (size == 0) {
  104. dev_err(&adapter->pdev->dev, "Can not capture Minidump "
  105. "template. Invalid template size.\n");
  106. return NX_RCODE_INVALID_ARGS;
  107. }
  108. addr = pci_zalloc_consistent(adapter->pdev, size, &md_template_addr);
  109. if (!addr) {
  110. dev_err(&adapter->pdev->dev, "Unable to allocate dmable memory for template.\n");
  111. return -ENOMEM;
  112. }
  113. memset(&cmd, 0, sizeof(cmd));
  114. memset(&cmd.rsp, 1, sizeof(struct _cdrp_cmd));
  115. cmd.req.cmd = NX_CDRP_CMD_GET_TEMP_HDR;
  116. cmd.req.arg1 = LSD(md_template_addr);
  117. cmd.req.arg2 = MSD(md_template_addr);
  118. cmd.req.arg3 |= size;
  119. netxen_issue_cmd(adapter, &cmd);
  120. if ((cmd.rsp.cmd == NX_RCODE_SUCCESS) && (size == cmd.rsp.arg2)) {
  121. memcpy(adapter->mdump.md_template, addr, size);
  122. } else {
  123. dev_err(&adapter->pdev->dev, "Failed to get minidump template, "
  124. "err_code : %d, requested_size : %d, actual_size : %d\n ",
  125. cmd.rsp.cmd, size, cmd.rsp.arg2);
  126. }
  127. pci_free_consistent(adapter->pdev, size, addr, md_template_addr);
  128. return 0;
  129. }
  130. static u32
  131. netxen_check_template_checksum(struct netxen_adapter *adapter)
  132. {
  133. u64 sum = 0 ;
  134. u32 *buff = adapter->mdump.md_template;
  135. int count = adapter->mdump.md_template_size/sizeof(uint32_t) ;
  136. while (count-- > 0)
  137. sum += *buff++ ;
  138. while (sum >> 32)
  139. sum = (sum & 0xFFFFFFFF) + (sum >> 32) ;
  140. return ~sum;
  141. }
  142. int
  143. netxen_setup_minidump(struct netxen_adapter *adapter)
  144. {
  145. int err = 0, i;
  146. u32 *template, *tmp_buf;
  147. struct netxen_minidump_template_hdr *hdr;
  148. err = netxen_get_minidump_template_size(adapter);
  149. if (err) {
  150. adapter->mdump.fw_supports_md = 0;
  151. if ((err == NX_RCODE_CMD_INVALID) ||
  152. (err == NX_RCODE_CMD_NOT_IMPL)) {
  153. dev_info(&adapter->pdev->dev,
  154. "Flashed firmware version does not support minidump, "
  155. "minimum version required is [ %u.%u.%u ].\n ",
  156. NX_MD_SUPPORT_MAJOR, NX_MD_SUPPORT_MINOR,
  157. NX_MD_SUPPORT_SUBVERSION);
  158. }
  159. return err;
  160. }
  161. if (!adapter->mdump.md_template_size) {
  162. dev_err(&adapter->pdev->dev, "Error : Invalid template size "
  163. ",should be non-zero.\n");
  164. return -EIO;
  165. }
  166. adapter->mdump.md_template =
  167. kmalloc(adapter->mdump.md_template_size, GFP_KERNEL);
  168. if (!adapter->mdump.md_template)
  169. return -ENOMEM;
  170. err = netxen_get_minidump_template(adapter);
  171. if (err) {
  172. if (err == NX_RCODE_CMD_NOT_IMPL)
  173. adapter->mdump.fw_supports_md = 0;
  174. goto free_template;
  175. }
  176. if (netxen_check_template_checksum(adapter)) {
  177. dev_err(&adapter->pdev->dev, "Minidump template checksum Error\n");
  178. err = -EIO;
  179. goto free_template;
  180. }
  181. adapter->mdump.md_capture_mask = NX_DUMP_MASK_DEF;
  182. tmp_buf = (u32 *) adapter->mdump.md_template;
  183. template = (u32 *) adapter->mdump.md_template;
  184. for (i = 0; i < adapter->mdump.md_template_size/sizeof(u32); i++)
  185. *template++ = __le32_to_cpu(*tmp_buf++);
  186. hdr = (struct netxen_minidump_template_hdr *)
  187. adapter->mdump.md_template;
  188. adapter->mdump.md_capture_buff = NULL;
  189. adapter->mdump.fw_supports_md = 1;
  190. adapter->mdump.md_enabled = 0;
  191. return err;
  192. free_template:
  193. kfree(adapter->mdump.md_template);
  194. adapter->mdump.md_template = NULL;
  195. return err;
  196. }
  197. int
  198. nx_fw_cmd_set_mtu(struct netxen_adapter *adapter, int mtu)
  199. {
  200. u32 rcode = NX_RCODE_SUCCESS;
  201. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  202. struct netxen_cmd_args cmd;
  203. memset(&cmd, 0, sizeof(cmd));
  204. cmd.req.cmd = NX_CDRP_CMD_SET_MTU;
  205. cmd.req.arg1 = recv_ctx->context_id;
  206. cmd.req.arg2 = mtu;
  207. cmd.req.arg3 = 0;
  208. if (recv_ctx->state == NX_HOST_CTX_STATE_ACTIVE)
  209. rcode = netxen_issue_cmd(adapter, &cmd);
  210. if (rcode != NX_RCODE_SUCCESS)
  211. return -EIO;
  212. return 0;
  213. }
  214. int
  215. nx_fw_cmd_set_gbe_port(struct netxen_adapter *adapter,
  216. u32 speed, u32 duplex, u32 autoneg)
  217. {
  218. struct netxen_cmd_args cmd;
  219. memset(&cmd, 0, sizeof(cmd));
  220. cmd.req.cmd = NX_CDRP_CMD_CONFIG_GBE_PORT;
  221. cmd.req.arg1 = speed;
  222. cmd.req.arg2 = duplex;
  223. cmd.req.arg3 = autoneg;
  224. return netxen_issue_cmd(adapter, &cmd);
  225. }
  226. static int
  227. nx_fw_cmd_create_rx_ctx(struct netxen_adapter *adapter)
  228. {
  229. void *addr;
  230. nx_hostrq_rx_ctx_t *prq;
  231. nx_cardrsp_rx_ctx_t *prsp;
  232. nx_hostrq_rds_ring_t *prq_rds;
  233. nx_hostrq_sds_ring_t *prq_sds;
  234. nx_cardrsp_rds_ring_t *prsp_rds;
  235. nx_cardrsp_sds_ring_t *prsp_sds;
  236. struct nx_host_rds_ring *rds_ring;
  237. struct nx_host_sds_ring *sds_ring;
  238. struct netxen_cmd_args cmd;
  239. dma_addr_t hostrq_phys_addr, cardrsp_phys_addr;
  240. u64 phys_addr;
  241. int i, nrds_rings, nsds_rings;
  242. size_t rq_size, rsp_size;
  243. u32 cap, reg, val;
  244. int err;
  245. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  246. nrds_rings = adapter->max_rds_rings;
  247. nsds_rings = adapter->max_sds_rings;
  248. rq_size =
  249. SIZEOF_HOSTRQ_RX(nx_hostrq_rx_ctx_t, nrds_rings, nsds_rings);
  250. rsp_size =
  251. SIZEOF_CARDRSP_RX(nx_cardrsp_rx_ctx_t, nrds_rings, nsds_rings);
  252. addr = pci_alloc_consistent(adapter->pdev,
  253. rq_size, &hostrq_phys_addr);
  254. if (addr == NULL)
  255. return -ENOMEM;
  256. prq = addr;
  257. addr = pci_alloc_consistent(adapter->pdev,
  258. rsp_size, &cardrsp_phys_addr);
  259. if (addr == NULL) {
  260. err = -ENOMEM;
  261. goto out_free_rq;
  262. }
  263. prsp = addr;
  264. prq->host_rsp_dma_addr = cpu_to_le64(cardrsp_phys_addr);
  265. cap = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN);
  266. cap |= (NX_CAP0_JUMBO_CONTIGUOUS | NX_CAP0_LRO_CONTIGUOUS);
  267. if (adapter->flags & NETXEN_FW_MSS_CAP)
  268. cap |= NX_CAP0_HW_LRO_MSS;
  269. prq->capabilities[0] = cpu_to_le32(cap);
  270. prq->host_int_crb_mode =
  271. cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
  272. prq->host_rds_crb_mode =
  273. cpu_to_le32(NX_HOST_RDS_CRB_MODE_UNIQUE);
  274. prq->num_rds_rings = cpu_to_le16(nrds_rings);
  275. prq->num_sds_rings = cpu_to_le16(nsds_rings);
  276. prq->rds_ring_offset = cpu_to_le32(0);
  277. val = le32_to_cpu(prq->rds_ring_offset) +
  278. (sizeof(nx_hostrq_rds_ring_t) * nrds_rings);
  279. prq->sds_ring_offset = cpu_to_le32(val);
  280. prq_rds = (nx_hostrq_rds_ring_t *)(prq->data +
  281. le32_to_cpu(prq->rds_ring_offset));
  282. for (i = 0; i < nrds_rings; i++) {
  283. rds_ring = &recv_ctx->rds_rings[i];
  284. prq_rds[i].host_phys_addr = cpu_to_le64(rds_ring->phys_addr);
  285. prq_rds[i].ring_size = cpu_to_le32(rds_ring->num_desc);
  286. prq_rds[i].ring_kind = cpu_to_le32(i);
  287. prq_rds[i].buff_size = cpu_to_le64(rds_ring->dma_size);
  288. }
  289. prq_sds = (nx_hostrq_sds_ring_t *)(prq->data +
  290. le32_to_cpu(prq->sds_ring_offset));
  291. for (i = 0; i < nsds_rings; i++) {
  292. sds_ring = &recv_ctx->sds_rings[i];
  293. prq_sds[i].host_phys_addr = cpu_to_le64(sds_ring->phys_addr);
  294. prq_sds[i].ring_size = cpu_to_le32(sds_ring->num_desc);
  295. prq_sds[i].msi_index = cpu_to_le16(i);
  296. }
  297. phys_addr = hostrq_phys_addr;
  298. memset(&cmd, 0, sizeof(cmd));
  299. cmd.req.arg1 = (u32)(phys_addr >> 32);
  300. cmd.req.arg2 = (u32)(phys_addr & 0xffffffff);
  301. cmd.req.arg3 = rq_size;
  302. cmd.req.cmd = NX_CDRP_CMD_CREATE_RX_CTX;
  303. err = netxen_issue_cmd(adapter, &cmd);
  304. if (err) {
  305. printk(KERN_WARNING
  306. "Failed to create rx ctx in firmware%d\n", err);
  307. goto out_free_rsp;
  308. }
  309. prsp_rds = ((nx_cardrsp_rds_ring_t *)
  310. &prsp->data[le32_to_cpu(prsp->rds_ring_offset)]);
  311. for (i = 0; i < le16_to_cpu(prsp->num_rds_rings); i++) {
  312. rds_ring = &recv_ctx->rds_rings[i];
  313. reg = le32_to_cpu(prsp_rds[i].host_producer_crb);
  314. rds_ring->crb_rcv_producer = netxen_get_ioaddr(adapter,
  315. NETXEN_NIC_REG(reg - 0x200));
  316. }
  317. prsp_sds = ((nx_cardrsp_sds_ring_t *)
  318. &prsp->data[le32_to_cpu(prsp->sds_ring_offset)]);
  319. for (i = 0; i < le16_to_cpu(prsp->num_sds_rings); i++) {
  320. sds_ring = &recv_ctx->sds_rings[i];
  321. reg = le32_to_cpu(prsp_sds[i].host_consumer_crb);
  322. sds_ring->crb_sts_consumer = netxen_get_ioaddr(adapter,
  323. NETXEN_NIC_REG(reg - 0x200));
  324. reg = le32_to_cpu(prsp_sds[i].interrupt_crb);
  325. sds_ring->crb_intr_mask = netxen_get_ioaddr(adapter,
  326. NETXEN_NIC_REG(reg - 0x200));
  327. }
  328. recv_ctx->state = le32_to_cpu(prsp->host_ctx_state);
  329. recv_ctx->context_id = le16_to_cpu(prsp->context_id);
  330. recv_ctx->virt_port = prsp->virt_port;
  331. out_free_rsp:
  332. pci_free_consistent(adapter->pdev, rsp_size, prsp, cardrsp_phys_addr);
  333. out_free_rq:
  334. pci_free_consistent(adapter->pdev, rq_size, prq, hostrq_phys_addr);
  335. return err;
  336. }
  337. static void
  338. nx_fw_cmd_destroy_rx_ctx(struct netxen_adapter *adapter)
  339. {
  340. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  341. struct netxen_cmd_args cmd;
  342. memset(&cmd, 0, sizeof(cmd));
  343. cmd.req.arg1 = recv_ctx->context_id;
  344. cmd.req.arg2 = NX_DESTROY_CTX_RESET;
  345. cmd.req.arg3 = 0;
  346. cmd.req.cmd = NX_CDRP_CMD_DESTROY_RX_CTX;
  347. if (netxen_issue_cmd(adapter, &cmd)) {
  348. printk(KERN_WARNING
  349. "%s: Failed to destroy rx ctx in firmware\n",
  350. netxen_nic_driver_name);
  351. }
  352. }
  353. static int
  354. nx_fw_cmd_create_tx_ctx(struct netxen_adapter *adapter)
  355. {
  356. nx_hostrq_tx_ctx_t *prq;
  357. nx_hostrq_cds_ring_t *prq_cds;
  358. nx_cardrsp_tx_ctx_t *prsp;
  359. void *rq_addr, *rsp_addr;
  360. size_t rq_size, rsp_size;
  361. u32 temp;
  362. int err = 0;
  363. u64 offset, phys_addr;
  364. dma_addr_t rq_phys_addr, rsp_phys_addr;
  365. struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
  366. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  367. struct netxen_cmd_args cmd;
  368. rq_size = SIZEOF_HOSTRQ_TX(nx_hostrq_tx_ctx_t);
  369. rq_addr = pci_alloc_consistent(adapter->pdev,
  370. rq_size, &rq_phys_addr);
  371. if (!rq_addr)
  372. return -ENOMEM;
  373. rsp_size = SIZEOF_CARDRSP_TX(nx_cardrsp_tx_ctx_t);
  374. rsp_addr = pci_alloc_consistent(adapter->pdev,
  375. rsp_size, &rsp_phys_addr);
  376. if (!rsp_addr) {
  377. err = -ENOMEM;
  378. goto out_free_rq;
  379. }
  380. memset(rq_addr, 0, rq_size);
  381. prq = rq_addr;
  382. memset(rsp_addr, 0, rsp_size);
  383. prsp = rsp_addr;
  384. prq->host_rsp_dma_addr = cpu_to_le64(rsp_phys_addr);
  385. temp = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN | NX_CAP0_LSO);
  386. prq->capabilities[0] = cpu_to_le32(temp);
  387. prq->host_int_crb_mode =
  388. cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
  389. prq->interrupt_ctl = 0;
  390. prq->msi_index = 0;
  391. prq->dummy_dma_addr = cpu_to_le64(adapter->dummy_dma.phys_addr);
  392. offset = recv_ctx->phys_addr + sizeof(struct netxen_ring_ctx);
  393. prq->cmd_cons_dma_addr = cpu_to_le64(offset);
  394. prq_cds = &prq->cds_ring;
  395. prq_cds->host_phys_addr = cpu_to_le64(tx_ring->phys_addr);
  396. prq_cds->ring_size = cpu_to_le32(tx_ring->num_desc);
  397. phys_addr = rq_phys_addr;
  398. memset(&cmd, 0, sizeof(cmd));
  399. cmd.req.arg1 = (u32)(phys_addr >> 32);
  400. cmd.req.arg2 = ((u32)phys_addr & 0xffffffff);
  401. cmd.req.arg3 = rq_size;
  402. cmd.req.cmd = NX_CDRP_CMD_CREATE_TX_CTX;
  403. err = netxen_issue_cmd(adapter, &cmd);
  404. if (err == NX_RCODE_SUCCESS) {
  405. temp = le32_to_cpu(prsp->cds_ring.host_producer_crb);
  406. tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
  407. NETXEN_NIC_REG(temp - 0x200));
  408. #if 0
  409. adapter->tx_state =
  410. le32_to_cpu(prsp->host_ctx_state);
  411. #endif
  412. adapter->tx_context_id =
  413. le16_to_cpu(prsp->context_id);
  414. } else {
  415. printk(KERN_WARNING
  416. "Failed to create tx ctx in firmware%d\n", err);
  417. err = -EIO;
  418. }
  419. pci_free_consistent(adapter->pdev, rsp_size, rsp_addr, rsp_phys_addr);
  420. out_free_rq:
  421. pci_free_consistent(adapter->pdev, rq_size, rq_addr, rq_phys_addr);
  422. return err;
  423. }
  424. static void
  425. nx_fw_cmd_destroy_tx_ctx(struct netxen_adapter *adapter)
  426. {
  427. struct netxen_cmd_args cmd;
  428. memset(&cmd, 0, sizeof(cmd));
  429. cmd.req.arg1 = adapter->tx_context_id;
  430. cmd.req.arg2 = NX_DESTROY_CTX_RESET;
  431. cmd.req.arg3 = 0;
  432. cmd.req.cmd = NX_CDRP_CMD_DESTROY_TX_CTX;
  433. if (netxen_issue_cmd(adapter, &cmd)) {
  434. printk(KERN_WARNING
  435. "%s: Failed to destroy tx ctx in firmware\n",
  436. netxen_nic_driver_name);
  437. }
  438. }
  439. int
  440. nx_fw_cmd_query_phy(struct netxen_adapter *adapter, u32 reg, u32 *val)
  441. {
  442. u32 rcode;
  443. struct netxen_cmd_args cmd;
  444. memset(&cmd, 0, sizeof(cmd));
  445. cmd.req.arg1 = reg;
  446. cmd.req.arg2 = 0;
  447. cmd.req.arg3 = 0;
  448. cmd.req.cmd = NX_CDRP_CMD_READ_PHY;
  449. cmd.rsp.arg1 = 1;
  450. rcode = netxen_issue_cmd(adapter, &cmd);
  451. if (rcode != NX_RCODE_SUCCESS)
  452. return -EIO;
  453. if (val == NULL)
  454. return -EIO;
  455. *val = cmd.rsp.arg1;
  456. return 0;
  457. }
  458. int
  459. nx_fw_cmd_set_phy(struct netxen_adapter *adapter, u32 reg, u32 val)
  460. {
  461. u32 rcode;
  462. struct netxen_cmd_args cmd;
  463. memset(&cmd, 0, sizeof(cmd));
  464. cmd.req.arg1 = reg;
  465. cmd.req.arg2 = val;
  466. cmd.req.arg3 = 0;
  467. cmd.req.cmd = NX_CDRP_CMD_WRITE_PHY;
  468. rcode = netxen_issue_cmd(adapter, &cmd);
  469. if (rcode != NX_RCODE_SUCCESS)
  470. return -EIO;
  471. return 0;
  472. }
  473. static u64 ctx_addr_sig_regs[][3] = {
  474. {NETXEN_NIC_REG(0x188), NETXEN_NIC_REG(0x18c), NETXEN_NIC_REG(0x1c0)},
  475. {NETXEN_NIC_REG(0x190), NETXEN_NIC_REG(0x194), NETXEN_NIC_REG(0x1c4)},
  476. {NETXEN_NIC_REG(0x198), NETXEN_NIC_REG(0x19c), NETXEN_NIC_REG(0x1c8)},
  477. {NETXEN_NIC_REG(0x1a0), NETXEN_NIC_REG(0x1a4), NETXEN_NIC_REG(0x1cc)}
  478. };
  479. #define CRB_CTX_ADDR_REG_LO(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][0])
  480. #define CRB_CTX_ADDR_REG_HI(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][2])
  481. #define CRB_CTX_SIGNATURE_REG(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][1])
  482. #define lower32(x) ((u32)((x) & 0xffffffff))
  483. #define upper32(x) ((u32)(((u64)(x) >> 32) & 0xffffffff))
  484. static struct netxen_recv_crb recv_crb_registers[] = {
  485. /* Instance 0 */
  486. {
  487. /* crb_rcv_producer: */
  488. {
  489. NETXEN_NIC_REG(0x100),
  490. /* Jumbo frames */
  491. NETXEN_NIC_REG(0x110),
  492. /* LRO */
  493. NETXEN_NIC_REG(0x120)
  494. },
  495. /* crb_sts_consumer: */
  496. {
  497. NETXEN_NIC_REG(0x138),
  498. NETXEN_NIC_REG_2(0x000),
  499. NETXEN_NIC_REG_2(0x004),
  500. NETXEN_NIC_REG_2(0x008),
  501. },
  502. /* sw_int_mask */
  503. {
  504. CRB_SW_INT_MASK_0,
  505. NETXEN_NIC_REG_2(0x044),
  506. NETXEN_NIC_REG_2(0x048),
  507. NETXEN_NIC_REG_2(0x04c),
  508. },
  509. },
  510. /* Instance 1 */
  511. {
  512. /* crb_rcv_producer: */
  513. {
  514. NETXEN_NIC_REG(0x144),
  515. /* Jumbo frames */
  516. NETXEN_NIC_REG(0x154),
  517. /* LRO */
  518. NETXEN_NIC_REG(0x164)
  519. },
  520. /* crb_sts_consumer: */
  521. {
  522. NETXEN_NIC_REG(0x17c),
  523. NETXEN_NIC_REG_2(0x020),
  524. NETXEN_NIC_REG_2(0x024),
  525. NETXEN_NIC_REG_2(0x028),
  526. },
  527. /* sw_int_mask */
  528. {
  529. CRB_SW_INT_MASK_1,
  530. NETXEN_NIC_REG_2(0x064),
  531. NETXEN_NIC_REG_2(0x068),
  532. NETXEN_NIC_REG_2(0x06c),
  533. },
  534. },
  535. /* Instance 2 */
  536. {
  537. /* crb_rcv_producer: */
  538. {
  539. NETXEN_NIC_REG(0x1d8),
  540. /* Jumbo frames */
  541. NETXEN_NIC_REG(0x1f8),
  542. /* LRO */
  543. NETXEN_NIC_REG(0x208)
  544. },
  545. /* crb_sts_consumer: */
  546. {
  547. NETXEN_NIC_REG(0x220),
  548. NETXEN_NIC_REG_2(0x03c),
  549. NETXEN_NIC_REG_2(0x03c),
  550. NETXEN_NIC_REG_2(0x03c),
  551. },
  552. /* sw_int_mask */
  553. {
  554. CRB_SW_INT_MASK_2,
  555. NETXEN_NIC_REG_2(0x03c),
  556. NETXEN_NIC_REG_2(0x03c),
  557. NETXEN_NIC_REG_2(0x03c),
  558. },
  559. },
  560. /* Instance 3 */
  561. {
  562. /* crb_rcv_producer: */
  563. {
  564. NETXEN_NIC_REG(0x22c),
  565. /* Jumbo frames */
  566. NETXEN_NIC_REG(0x23c),
  567. /* LRO */
  568. NETXEN_NIC_REG(0x24c)
  569. },
  570. /* crb_sts_consumer: */
  571. {
  572. NETXEN_NIC_REG(0x264),
  573. NETXEN_NIC_REG_2(0x03c),
  574. NETXEN_NIC_REG_2(0x03c),
  575. NETXEN_NIC_REG_2(0x03c),
  576. },
  577. /* sw_int_mask */
  578. {
  579. CRB_SW_INT_MASK_3,
  580. NETXEN_NIC_REG_2(0x03c),
  581. NETXEN_NIC_REG_2(0x03c),
  582. NETXEN_NIC_REG_2(0x03c),
  583. },
  584. },
  585. };
  586. static int
  587. netxen_init_old_ctx(struct netxen_adapter *adapter)
  588. {
  589. struct netxen_recv_context *recv_ctx;
  590. struct nx_host_rds_ring *rds_ring;
  591. struct nx_host_sds_ring *sds_ring;
  592. struct nx_host_tx_ring *tx_ring;
  593. int ring;
  594. int port = adapter->portnum;
  595. struct netxen_ring_ctx *hwctx;
  596. u32 signature;
  597. tx_ring = adapter->tx_ring;
  598. recv_ctx = &adapter->recv_ctx;
  599. hwctx = recv_ctx->hwctx;
  600. hwctx->cmd_ring_addr = cpu_to_le64(tx_ring->phys_addr);
  601. hwctx->cmd_ring_size = cpu_to_le32(tx_ring->num_desc);
  602. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  603. rds_ring = &recv_ctx->rds_rings[ring];
  604. hwctx->rcv_rings[ring].addr =
  605. cpu_to_le64(rds_ring->phys_addr);
  606. hwctx->rcv_rings[ring].size =
  607. cpu_to_le32(rds_ring->num_desc);
  608. }
  609. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  610. sds_ring = &recv_ctx->sds_rings[ring];
  611. if (ring == 0) {
  612. hwctx->sts_ring_addr = cpu_to_le64(sds_ring->phys_addr);
  613. hwctx->sts_ring_size = cpu_to_le32(sds_ring->num_desc);
  614. }
  615. hwctx->sts_rings[ring].addr = cpu_to_le64(sds_ring->phys_addr);
  616. hwctx->sts_rings[ring].size = cpu_to_le32(sds_ring->num_desc);
  617. hwctx->sts_rings[ring].msi_index = cpu_to_le16(ring);
  618. }
  619. hwctx->sts_ring_count = cpu_to_le32(adapter->max_sds_rings);
  620. signature = (adapter->max_sds_rings > 1) ?
  621. NETXEN_CTX_SIGNATURE_V2 : NETXEN_CTX_SIGNATURE;
  622. NXWR32(adapter, CRB_CTX_ADDR_REG_LO(port),
  623. lower32(recv_ctx->phys_addr));
  624. NXWR32(adapter, CRB_CTX_ADDR_REG_HI(port),
  625. upper32(recv_ctx->phys_addr));
  626. NXWR32(adapter, CRB_CTX_SIGNATURE_REG(port),
  627. signature | port);
  628. return 0;
  629. }
  630. int netxen_alloc_hw_resources(struct netxen_adapter *adapter)
  631. {
  632. void *addr;
  633. int err = 0;
  634. int ring;
  635. struct netxen_recv_context *recv_ctx;
  636. struct nx_host_rds_ring *rds_ring;
  637. struct nx_host_sds_ring *sds_ring;
  638. struct nx_host_tx_ring *tx_ring;
  639. struct pci_dev *pdev = adapter->pdev;
  640. struct net_device *netdev = adapter->netdev;
  641. int port = adapter->portnum;
  642. recv_ctx = &adapter->recv_ctx;
  643. tx_ring = adapter->tx_ring;
  644. addr = pci_alloc_consistent(pdev,
  645. sizeof(struct netxen_ring_ctx) + sizeof(uint32_t),
  646. &recv_ctx->phys_addr);
  647. if (addr == NULL) {
  648. dev_err(&pdev->dev, "failed to allocate hw context\n");
  649. return -ENOMEM;
  650. }
  651. memset(addr, 0, sizeof(struct netxen_ring_ctx));
  652. recv_ctx->hwctx = addr;
  653. recv_ctx->hwctx->ctx_id = cpu_to_le32(port);
  654. recv_ctx->hwctx->cmd_consumer_offset =
  655. cpu_to_le64(recv_ctx->phys_addr +
  656. sizeof(struct netxen_ring_ctx));
  657. tx_ring->hw_consumer =
  658. (__le32 *)(((char *)addr) + sizeof(struct netxen_ring_ctx));
  659. /* cmd desc ring */
  660. addr = pci_alloc_consistent(pdev, TX_DESC_RINGSIZE(tx_ring),
  661. &tx_ring->phys_addr);
  662. if (addr == NULL) {
  663. dev_err(&pdev->dev, "%s: failed to allocate tx desc ring\n",
  664. netdev->name);
  665. err = -ENOMEM;
  666. goto err_out_free;
  667. }
  668. tx_ring->desc_head = addr;
  669. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  670. rds_ring = &recv_ctx->rds_rings[ring];
  671. addr = pci_alloc_consistent(adapter->pdev,
  672. RCV_DESC_RINGSIZE(rds_ring),
  673. &rds_ring->phys_addr);
  674. if (addr == NULL) {
  675. dev_err(&pdev->dev,
  676. "%s: failed to allocate rds ring [%d]\n",
  677. netdev->name, ring);
  678. err = -ENOMEM;
  679. goto err_out_free;
  680. }
  681. rds_ring->desc_head = addr;
  682. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  683. rds_ring->crb_rcv_producer =
  684. netxen_get_ioaddr(adapter,
  685. recv_crb_registers[port].crb_rcv_producer[ring]);
  686. }
  687. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  688. sds_ring = &recv_ctx->sds_rings[ring];
  689. addr = pci_alloc_consistent(adapter->pdev,
  690. STATUS_DESC_RINGSIZE(sds_ring),
  691. &sds_ring->phys_addr);
  692. if (addr == NULL) {
  693. dev_err(&pdev->dev,
  694. "%s: failed to allocate sds ring [%d]\n",
  695. netdev->name, ring);
  696. err = -ENOMEM;
  697. goto err_out_free;
  698. }
  699. sds_ring->desc_head = addr;
  700. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  701. sds_ring->crb_sts_consumer =
  702. netxen_get_ioaddr(adapter,
  703. recv_crb_registers[port].crb_sts_consumer[ring]);
  704. sds_ring->crb_intr_mask =
  705. netxen_get_ioaddr(adapter,
  706. recv_crb_registers[port].sw_int_mask[ring]);
  707. }
  708. }
  709. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  710. if (test_and_set_bit(__NX_FW_ATTACHED, &adapter->state))
  711. goto done;
  712. err = nx_fw_cmd_create_rx_ctx(adapter);
  713. if (err)
  714. goto err_out_free;
  715. err = nx_fw_cmd_create_tx_ctx(adapter);
  716. if (err)
  717. goto err_out_free;
  718. } else {
  719. err = netxen_init_old_ctx(adapter);
  720. if (err)
  721. goto err_out_free;
  722. }
  723. done:
  724. return 0;
  725. err_out_free:
  726. netxen_free_hw_resources(adapter);
  727. return err;
  728. }
  729. void netxen_free_hw_resources(struct netxen_adapter *adapter)
  730. {
  731. struct netxen_recv_context *recv_ctx;
  732. struct nx_host_rds_ring *rds_ring;
  733. struct nx_host_sds_ring *sds_ring;
  734. struct nx_host_tx_ring *tx_ring;
  735. int ring;
  736. int port = adapter->portnum;
  737. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  738. if (!test_and_clear_bit(__NX_FW_ATTACHED, &adapter->state))
  739. goto done;
  740. nx_fw_cmd_destroy_rx_ctx(adapter);
  741. nx_fw_cmd_destroy_tx_ctx(adapter);
  742. } else {
  743. netxen_api_lock(adapter);
  744. NXWR32(adapter, CRB_CTX_SIGNATURE_REG(port),
  745. NETXEN_CTX_D3_RESET | port);
  746. netxen_api_unlock(adapter);
  747. }
  748. /* Allow dma queues to drain after context reset */
  749. msleep(20);
  750. done:
  751. recv_ctx = &adapter->recv_ctx;
  752. if (recv_ctx->hwctx != NULL) {
  753. pci_free_consistent(adapter->pdev,
  754. sizeof(struct netxen_ring_ctx) +
  755. sizeof(uint32_t),
  756. recv_ctx->hwctx,
  757. recv_ctx->phys_addr);
  758. recv_ctx->hwctx = NULL;
  759. }
  760. tx_ring = adapter->tx_ring;
  761. if (tx_ring->desc_head != NULL) {
  762. pci_free_consistent(adapter->pdev,
  763. TX_DESC_RINGSIZE(tx_ring),
  764. tx_ring->desc_head, tx_ring->phys_addr);
  765. tx_ring->desc_head = NULL;
  766. }
  767. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  768. rds_ring = &recv_ctx->rds_rings[ring];
  769. if (rds_ring->desc_head != NULL) {
  770. pci_free_consistent(adapter->pdev,
  771. RCV_DESC_RINGSIZE(rds_ring),
  772. rds_ring->desc_head,
  773. rds_ring->phys_addr);
  774. rds_ring->desc_head = NULL;
  775. }
  776. }
  777. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  778. sds_ring = &recv_ctx->sds_rings[ring];
  779. if (sds_ring->desc_head != NULL) {
  780. pci_free_consistent(adapter->pdev,
  781. STATUS_DESC_RINGSIZE(sds_ring),
  782. sds_ring->desc_head,
  783. sds_ring->phys_addr);
  784. sds_ring->desc_head = NULL;
  785. }
  786. }
  787. }