2 * Copyright (C) 2003 - 2008 NetXen, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
31 #include "netxen_nic_hw.h"
32 #include "netxen_nic.h"
33 #include "netxen_nic_phan_reg.h"
35 #define NXHAL_VERSION 1
38 netxen_api_lock(struct netxen_adapter *adapter)
40 u32 done = 0, timeout = 0;
43 /* Acquire PCIE HW semaphore5 */
44 netxen_nic_read_w0(adapter,
45 NETXEN_PCIE_REG(PCIE_SEM5_LOCK), &done);
50 if (++timeout >= NX_OS_CRB_RETRY_COUNT) {
51 printk(KERN_ERR "%s: lock timeout.\n", __func__);
59 netxen_nic_write_w1(adapter,
60 NETXEN_API_LOCK_ID, NX_OS_API_LOCK_DRIVER);
66 netxen_api_unlock(struct netxen_adapter *adapter)
70 /* Release PCIE HW semaphore5 */
71 netxen_nic_read_w0(adapter,
72 NETXEN_PCIE_REG(PCIE_SEM5_UNLOCK), &val);
77 netxen_poll_rsp(struct netxen_adapter *adapter)
79 u32 raw_rsp, rsp = NX_CDRP_RSP_OK;
83 /* give atleast 1ms for firmware to respond */
86 if (++timeout > NX_OS_CRB_RETRY_COUNT)
87 return NX_CDRP_RSP_TIMEOUT;
89 netxen_nic_read_w1(adapter, NX_CDRP_CRB_OFFSET,
92 rsp = le32_to_cpu(raw_rsp);
93 } while (!NX_CDRP_IS_RSP(rsp));
99 netxen_issue_cmd(struct netxen_adapter *adapter,
100 u32 pci_fn, u32 version, u32 arg1, u32 arg2, u32 arg3, u32 cmd)
104 u32 rcode = NX_RCODE_SUCCESS;
106 signature = NX_CDRP_SIGNATURE_MAKE(pci_fn, version);
108 /* Acquire semaphore before accessing CRB */
109 if (netxen_api_lock(adapter))
110 return NX_RCODE_TIMEOUT;
112 netxen_nic_write_w1(adapter, NX_SIGN_CRB_OFFSET,
113 cpu_to_le32(signature));
115 netxen_nic_write_w1(adapter, NX_ARG1_CRB_OFFSET,
118 netxen_nic_write_w1(adapter, NX_ARG2_CRB_OFFSET,
121 netxen_nic_write_w1(adapter, NX_ARG3_CRB_OFFSET,
124 netxen_nic_write_w1(adapter, NX_CDRP_CRB_OFFSET,
125 cpu_to_le32(NX_CDRP_FORM_CMD(cmd)));
127 rsp = netxen_poll_rsp(adapter);
129 if (rsp == NX_CDRP_RSP_TIMEOUT) {
130 printk(KERN_ERR "%s: card response timeout.\n",
131 netxen_nic_driver_name);
133 rcode = NX_RCODE_TIMEOUT;
134 } else if (rsp == NX_CDRP_RSP_FAIL) {
135 netxen_nic_read_w1(adapter, NX_ARG1_CRB_OFFSET, &rcode);
136 rcode = le32_to_cpu(rcode);
138 printk(KERN_ERR "%s: failed card response code:0x%x\n",
139 netxen_nic_driver_name, rcode);
142 /* Release semaphore */
143 netxen_api_unlock(adapter);
149 nx_fw_cmd_set_mtu(struct netxen_adapter *adapter, int mtu)
151 u32 rcode = NX_RCODE_SUCCESS;
152 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx[0];
154 if (recv_ctx->state == NX_HOST_CTX_STATE_ACTIVE)
155 rcode = netxen_issue_cmd(adapter,
156 adapter->ahw.pci_func,
158 recv_ctx->context_id,
161 NX_CDRP_CMD_SET_MTU);
163 if (rcode != NX_RCODE_SUCCESS)
170 nx_fw_cmd_create_rx_ctx(struct netxen_adapter *adapter)
173 nx_hostrq_rx_ctx_t *prq;
174 nx_cardrsp_rx_ctx_t *prsp;
175 nx_hostrq_rds_ring_t *prq_rds;
176 nx_hostrq_sds_ring_t *prq_sds;
177 nx_cardrsp_rds_ring_t *prsp_rds;
178 nx_cardrsp_sds_ring_t *prsp_sds;
179 struct nx_host_rds_ring *rds_ring;
181 dma_addr_t hostrq_phys_addr, cardrsp_phys_addr;
184 int i, nrds_rings, nsds_rings;
185 size_t rq_size, rsp_size;
190 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx[0];
192 /* only one sds ring for now */
193 nrds_rings = adapter->max_rds_rings;
197 SIZEOF_HOSTRQ_RX(nx_hostrq_rx_ctx_t, nrds_rings, nsds_rings);
199 SIZEOF_CARDRSP_RX(nx_cardrsp_rx_ctx_t, nrds_rings, nsds_rings);
201 addr = pci_alloc_consistent(adapter->pdev,
202 rq_size, &hostrq_phys_addr);
205 prq = (nx_hostrq_rx_ctx_t *)addr;
207 addr = pci_alloc_consistent(adapter->pdev,
208 rsp_size, &cardrsp_phys_addr);
213 prsp = (nx_cardrsp_rx_ctx_t *)addr;
215 prq->host_rsp_dma_addr = cpu_to_le64(cardrsp_phys_addr);
217 cap = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN);
218 cap |= (NX_CAP0_JUMBO_CONTIGUOUS | NX_CAP0_LRO_CONTIGUOUS);
220 prq->capabilities[0] = cpu_to_le32(cap);
221 prq->host_int_crb_mode =
222 cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
223 prq->host_rds_crb_mode =
224 cpu_to_le32(NX_HOST_RDS_CRB_MODE_UNIQUE);
226 prq->num_rds_rings = cpu_to_le16(nrds_rings);
227 prq->num_sds_rings = cpu_to_le16(nsds_rings);
228 prq->rds_ring_offset = 0;
229 prq->sds_ring_offset = prq->rds_ring_offset +
230 (sizeof(nx_hostrq_rds_ring_t) * nrds_rings);
232 prq_rds = (nx_hostrq_rds_ring_t *)(prq->data + prq->rds_ring_offset);
234 for (i = 0; i < nrds_rings; i++) {
236 rds_ring = &recv_ctx->rds_rings[i];
238 prq_rds[i].host_phys_addr = cpu_to_le64(rds_ring->phys_addr);
239 prq_rds[i].ring_size = cpu_to_le32(rds_ring->max_rx_desc_count);
240 prq_rds[i].ring_kind = cpu_to_le32(i);
241 prq_rds[i].buff_size = cpu_to_le64(rds_ring->dma_size);
244 prq_sds = (nx_hostrq_sds_ring_t *)(prq->data + prq->sds_ring_offset);
246 prq_sds[0].host_phys_addr =
247 cpu_to_le64(recv_ctx->rcv_status_desc_phys_addr);
248 prq_sds[0].ring_size = cpu_to_le32(adapter->max_rx_desc_count);
249 /* only one msix vector for now */
250 prq_sds[0].msi_index = cpu_to_le32(0);
252 /* now byteswap offsets */
253 prq->rds_ring_offset = cpu_to_le32(prq->rds_ring_offset);
254 prq->sds_ring_offset = cpu_to_le32(prq->sds_ring_offset);
256 phys_addr = hostrq_phys_addr;
257 err = netxen_issue_cmd(adapter,
258 adapter->ahw.pci_func,
260 (u32)(phys_addr >> 32),
261 (u32)(phys_addr & 0xffffffff),
263 NX_CDRP_CMD_CREATE_RX_CTX);
266 "Failed to create rx ctx in firmware%d\n", err);
271 prsp_rds = ((nx_cardrsp_rds_ring_t *)
272 &prsp->data[prsp->rds_ring_offset]);
274 for (i = 0; i < le32_to_cpu(prsp->num_rds_rings); i++) {
275 rds_ring = &recv_ctx->rds_rings[i];
277 reg = le32_to_cpu(prsp_rds[i].host_producer_crb);
278 rds_ring->crb_rcv_producer = NETXEN_NIC_REG(reg - 0x200);
281 prsp_sds = ((nx_cardrsp_sds_ring_t *)
282 &prsp->data[prsp->sds_ring_offset]);
283 reg = le32_to_cpu(prsp_sds[0].host_consumer_crb);
284 recv_ctx->crb_sts_consumer = NETXEN_NIC_REG(reg - 0x200);
286 reg = le32_to_cpu(prsp_sds[0].interrupt_crb);
287 adapter->crb_intr_mask = NETXEN_NIC_REG(reg - 0x200);
289 recv_ctx->state = le32_to_cpu(prsp->host_ctx_state);
290 recv_ctx->context_id = le16_to_cpu(prsp->context_id);
291 recv_ctx->virt_port = le16_to_cpu(prsp->virt_port);
294 pci_free_consistent(adapter->pdev, rsp_size, prsp, cardrsp_phys_addr);
296 pci_free_consistent(adapter->pdev, rq_size, prq, hostrq_phys_addr);
301 nx_fw_cmd_destroy_rx_ctx(struct netxen_adapter *adapter)
303 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx[0];
305 if (netxen_issue_cmd(adapter,
306 adapter->ahw.pci_func,
308 recv_ctx->context_id,
309 NX_DESTROY_CTX_RESET,
311 NX_CDRP_CMD_DESTROY_RX_CTX)) {
314 "%s: Failed to destroy rx ctx in firmware\n",
315 netxen_nic_driver_name);
320 nx_fw_cmd_create_tx_ctx(struct netxen_adapter *adapter)
322 nx_hostrq_tx_ctx_t *prq;
323 nx_hostrq_cds_ring_t *prq_cds;
324 nx_cardrsp_tx_ctx_t *prsp;
325 void *rq_addr, *rsp_addr;
326 size_t rq_size, rsp_size;
329 u64 offset, phys_addr;
330 dma_addr_t rq_phys_addr, rsp_phys_addr;
332 rq_size = SIZEOF_HOSTRQ_TX(nx_hostrq_tx_ctx_t);
333 rq_addr = pci_alloc_consistent(adapter->pdev,
334 rq_size, &rq_phys_addr);
338 rsp_size = SIZEOF_CARDRSP_TX(nx_cardrsp_tx_ctx_t);
339 rsp_addr = pci_alloc_consistent(adapter->pdev,
340 rsp_size, &rsp_phys_addr);
346 memset(rq_addr, 0, rq_size);
347 prq = (nx_hostrq_tx_ctx_t *)rq_addr;
349 memset(rsp_addr, 0, rsp_size);
350 prsp = (nx_cardrsp_tx_ctx_t *)rsp_addr;
352 prq->host_rsp_dma_addr = cpu_to_le64(rsp_phys_addr);
354 temp = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN | NX_CAP0_LSO);
355 prq->capabilities[0] = cpu_to_le32(temp);
357 prq->host_int_crb_mode =
358 cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
360 prq->interrupt_ctl = 0;
363 prq->dummy_dma_addr = cpu_to_le64(adapter->dummy_dma.phys_addr);
365 offset = adapter->ctx_desc_phys_addr+sizeof(struct netxen_ring_ctx);
366 prq->cmd_cons_dma_addr = cpu_to_le64(offset);
368 prq_cds = &prq->cds_ring;
370 prq_cds->host_phys_addr =
371 cpu_to_le64(adapter->ahw.cmd_desc_phys_addr);
373 prq_cds->ring_size = cpu_to_le32(adapter->max_tx_desc_count);
375 phys_addr = rq_phys_addr;
376 err = netxen_issue_cmd(adapter,
377 adapter->ahw.pci_func,
379 (u32)(phys_addr >> 32),
380 ((u32)phys_addr & 0xffffffff),
382 NX_CDRP_CMD_CREATE_TX_CTX);
384 if (err == NX_RCODE_SUCCESS) {
385 temp = le32_to_cpu(prsp->cds_ring.host_producer_crb);
386 adapter->crb_addr_cmd_producer =
387 NETXEN_NIC_REG(temp - 0x200);
390 le32_to_cpu(prsp->host_ctx_state);
392 adapter->tx_context_id =
393 le16_to_cpu(prsp->context_id);
396 "Failed to create tx ctx in firmware%d\n", err);
400 pci_free_consistent(adapter->pdev, rsp_size, rsp_addr, rsp_phys_addr);
403 pci_free_consistent(adapter->pdev, rq_size, rq_addr, rq_phys_addr);
409 nx_fw_cmd_destroy_tx_ctx(struct netxen_adapter *adapter)
411 if (netxen_issue_cmd(adapter,
412 adapter->ahw.pci_func,
414 adapter->tx_context_id,
415 NX_DESTROY_CTX_RESET,
417 NX_CDRP_CMD_DESTROY_TX_CTX)) {
420 "%s: Failed to destroy tx ctx in firmware\n",
421 netxen_nic_driver_name);
425 static u64 ctx_addr_sig_regs[][3] = {
426 {NETXEN_NIC_REG(0x188), NETXEN_NIC_REG(0x18c), NETXEN_NIC_REG(0x1c0)},
427 {NETXEN_NIC_REG(0x190), NETXEN_NIC_REG(0x194), NETXEN_NIC_REG(0x1c4)},
428 {NETXEN_NIC_REG(0x198), NETXEN_NIC_REG(0x19c), NETXEN_NIC_REG(0x1c8)},
429 {NETXEN_NIC_REG(0x1a0), NETXEN_NIC_REG(0x1a4), NETXEN_NIC_REG(0x1cc)}
432 #define CRB_CTX_ADDR_REG_LO(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][0])
433 #define CRB_CTX_ADDR_REG_HI(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][2])
434 #define CRB_CTX_SIGNATURE_REG(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][1])
436 #define lower32(x) ((u32)((x) & 0xffffffff))
437 #define upper32(x) ((u32)(((u64)(x) >> 32) & 0xffffffff))
439 static struct netxen_recv_crb recv_crb_registers[] = {
442 /* crb_rcv_producer: */
444 NETXEN_NIC_REG(0x100),
446 NETXEN_NIC_REG(0x110),
448 NETXEN_NIC_REG(0x120)
450 /* crb_sts_consumer: */
451 NETXEN_NIC_REG(0x138),
455 /* crb_rcv_producer: */
457 NETXEN_NIC_REG(0x144),
459 NETXEN_NIC_REG(0x154),
461 NETXEN_NIC_REG(0x164)
463 /* crb_sts_consumer: */
464 NETXEN_NIC_REG(0x17c),
468 /* crb_rcv_producer: */
470 NETXEN_NIC_REG(0x1d8),
472 NETXEN_NIC_REG(0x1f8),
474 NETXEN_NIC_REG(0x208)
476 /* crb_sts_consumer: */
477 NETXEN_NIC_REG(0x220),
481 /* crb_rcv_producer: */
483 NETXEN_NIC_REG(0x22c),
485 NETXEN_NIC_REG(0x23c),
487 NETXEN_NIC_REG(0x24c)
489 /* crb_sts_consumer: */
490 NETXEN_NIC_REG(0x264),
495 netxen_init_old_ctx(struct netxen_adapter *adapter)
497 struct netxen_recv_context *recv_ctx;
498 struct nx_host_rds_ring *rds_ring;
500 int func_id = adapter->portnum;
502 adapter->ctx_desc->cmd_ring_addr =
503 cpu_to_le64(adapter->ahw.cmd_desc_phys_addr);
504 adapter->ctx_desc->cmd_ring_size =
505 cpu_to_le32(adapter->max_tx_desc_count);
507 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
508 recv_ctx = &adapter->recv_ctx[ctx];
510 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
511 rds_ring = &recv_ctx->rds_rings[ring];
513 adapter->ctx_desc->rcv_ctx[ring].rcv_ring_addr =
514 cpu_to_le64(rds_ring->phys_addr);
515 adapter->ctx_desc->rcv_ctx[ring].rcv_ring_size =
516 cpu_to_le32(rds_ring->max_rx_desc_count);
518 adapter->ctx_desc->sts_ring_addr =
519 cpu_to_le64(recv_ctx->rcv_status_desc_phys_addr);
520 adapter->ctx_desc->sts_ring_size =
521 cpu_to_le32(adapter->max_rx_desc_count);
524 adapter->pci_write_normalize(adapter, CRB_CTX_ADDR_REG_LO(func_id),
525 lower32(adapter->ctx_desc_phys_addr));
526 adapter->pci_write_normalize(adapter, CRB_CTX_ADDR_REG_HI(func_id),
527 upper32(adapter->ctx_desc_phys_addr));
528 adapter->pci_write_normalize(adapter, CRB_CTX_SIGNATURE_REG(func_id),
529 NETXEN_CTX_SIGNATURE | func_id);
533 static uint32_t sw_int_mask[4] = {
534 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
535 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
538 int netxen_alloc_hw_resources(struct netxen_adapter *adapter)
540 struct netxen_hardware_context *hw = &adapter->ahw;
545 struct netxen_recv_context *recv_ctx;
546 struct nx_host_rds_ring *rds_ring;
548 err = netxen_receive_peg_ready(adapter);
550 printk(KERN_ERR "Rcv Peg initialization not complete:%x.\n",
555 addr = pci_alloc_consistent(adapter->pdev,
556 sizeof(struct netxen_ring_ctx) + sizeof(uint32_t),
557 &adapter->ctx_desc_phys_addr);
560 DPRINTK(ERR, "failed to allocate hw context\n");
563 memset(addr, 0, sizeof(struct netxen_ring_ctx));
564 adapter->ctx_desc = (struct netxen_ring_ctx *)addr;
565 adapter->ctx_desc->ctx_id = cpu_to_le32(adapter->portnum);
566 adapter->ctx_desc->cmd_consumer_offset =
567 cpu_to_le64(adapter->ctx_desc_phys_addr +
568 sizeof(struct netxen_ring_ctx));
569 adapter->cmd_consumer =
570 (__le32 *)(((char *)addr) + sizeof(struct netxen_ring_ctx));
573 addr = pci_alloc_consistent(adapter->pdev,
574 sizeof(struct cmd_desc_type0) *
575 adapter->max_tx_desc_count,
576 &hw->cmd_desc_phys_addr);
579 printk(KERN_ERR "%s failed to allocate tx desc ring\n",
580 netxen_nic_driver_name);
584 hw->cmd_desc_head = (struct cmd_desc_type0 *)addr;
586 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
587 recv_ctx = &adapter->recv_ctx[ctx];
589 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
591 rds_ring = &recv_ctx->rds_rings[ring];
592 addr = pci_alloc_consistent(adapter->pdev,
594 &rds_ring->phys_addr);
596 printk(KERN_ERR "%s failed to allocate rx "
598 netxen_nic_driver_name, ring);
602 rds_ring->desc_head = (struct rcv_desc *)addr;
604 if (adapter->fw_major < 4)
605 rds_ring->crb_rcv_producer =
606 recv_crb_registers[adapter->portnum].
607 crb_rcv_producer[ring];
610 /* status desc ring */
611 addr = pci_alloc_consistent(adapter->pdev,
612 STATUS_DESC_RINGSIZE,
613 &recv_ctx->rcv_status_desc_phys_addr);
615 printk(KERN_ERR "%s failed to allocate sts desc ring\n",
616 netxen_nic_driver_name);
620 recv_ctx->rcv_status_desc_head = (struct status_desc *)addr;
622 if (adapter->fw_major < 4)
623 recv_ctx->crb_sts_consumer =
624 recv_crb_registers[adapter->portnum].
628 if (adapter->fw_major >= 4) {
629 adapter->intr_scheme = INTR_SCHEME_PERPORT;
630 adapter->msi_mode = MSI_MODE_MULTIFUNC;
632 err = nx_fw_cmd_create_rx_ctx(adapter);
635 err = nx_fw_cmd_create_tx_ctx(adapter);
640 adapter->intr_scheme = adapter->pci_read_normalize(adapter,
641 CRB_NIC_CAPABILITIES_FW);
642 adapter->msi_mode = adapter->pci_read_normalize(adapter,
643 CRB_NIC_MSI_MODE_FW);
644 adapter->crb_intr_mask = sw_int_mask[adapter->portnum];
646 err = netxen_init_old_ctx(adapter);
648 netxen_free_hw_resources(adapter);
657 netxen_free_hw_resources(adapter);
661 void netxen_free_hw_resources(struct netxen_adapter *adapter)
663 struct netxen_recv_context *recv_ctx;
664 struct nx_host_rds_ring *rds_ring;
667 if (adapter->fw_major >= 4) {
668 nx_fw_cmd_destroy_tx_ctx(adapter);
669 nx_fw_cmd_destroy_rx_ctx(adapter);
672 if (adapter->ctx_desc != NULL) {
673 pci_free_consistent(adapter->pdev,
674 sizeof(struct netxen_ring_ctx) +
677 adapter->ctx_desc_phys_addr);
678 adapter->ctx_desc = NULL;
681 if (adapter->ahw.cmd_desc_head != NULL) {
682 pci_free_consistent(adapter->pdev,
683 sizeof(struct cmd_desc_type0) *
684 adapter->max_tx_desc_count,
685 adapter->ahw.cmd_desc_head,
686 adapter->ahw.cmd_desc_phys_addr);
687 adapter->ahw.cmd_desc_head = NULL;
690 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
691 recv_ctx = &adapter->recv_ctx[ctx];
692 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
693 rds_ring = &recv_ctx->rds_rings[ring];
695 if (rds_ring->desc_head != NULL) {
696 pci_free_consistent(adapter->pdev,
699 rds_ring->phys_addr);
700 rds_ring->desc_head = NULL;
704 if (recv_ctx->rcv_status_desc_head != NULL) {
705 pci_free_consistent(adapter->pdev,
706 STATUS_DESC_RINGSIZE,
707 recv_ctx->rcv_status_desc_head,
708 recv_ctx->rcv_status_desc_phys_addr);
709 recv_ctx->rcv_status_desc_head = NULL;