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 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, &rsp);
90 } while (!NX_CDRP_IS_RSP(rsp));
96 netxen_issue_cmd(struct netxen_adapter *adapter,
97 u32 pci_fn, u32 version, u32 arg1, u32 arg2, u32 arg3, u32 cmd)
101 u32 rcode = NX_RCODE_SUCCESS;
103 signature = NX_CDRP_SIGNATURE_MAKE(pci_fn, version);
105 /* Acquire semaphore before accessing CRB */
106 if (netxen_api_lock(adapter))
107 return NX_RCODE_TIMEOUT;
109 netxen_nic_write_w1(adapter, NX_SIGN_CRB_OFFSET, signature);
111 netxen_nic_write_w1(adapter, NX_ARG1_CRB_OFFSET, arg1);
113 netxen_nic_write_w1(adapter, NX_ARG2_CRB_OFFSET, arg2);
115 netxen_nic_write_w1(adapter, NX_ARG3_CRB_OFFSET, arg3);
117 netxen_nic_write_w1(adapter, NX_CDRP_CRB_OFFSET,
118 NX_CDRP_FORM_CMD(cmd));
120 rsp = netxen_poll_rsp(adapter);
122 if (rsp == NX_CDRP_RSP_TIMEOUT) {
123 printk(KERN_ERR "%s: card response timeout.\n",
124 netxen_nic_driver_name);
126 rcode = NX_RCODE_TIMEOUT;
127 } else if (rsp == NX_CDRP_RSP_FAIL) {
128 netxen_nic_read_w1(adapter, NX_ARG1_CRB_OFFSET, &rcode);
130 printk(KERN_ERR "%s: failed card response code:0x%x\n",
131 netxen_nic_driver_name, rcode);
134 /* Release semaphore */
135 netxen_api_unlock(adapter);
141 nx_fw_cmd_set_mtu(struct netxen_adapter *adapter, int mtu)
143 u32 rcode = NX_RCODE_SUCCESS;
144 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx[0];
146 if (recv_ctx->state == NX_HOST_CTX_STATE_ACTIVE)
147 rcode = netxen_issue_cmd(adapter,
148 adapter->ahw.pci_func,
150 recv_ctx->context_id,
153 NX_CDRP_CMD_SET_MTU);
155 if (rcode != NX_RCODE_SUCCESS)
162 nx_fw_cmd_create_rx_ctx(struct netxen_adapter *adapter)
165 nx_hostrq_rx_ctx_t *prq;
166 nx_cardrsp_rx_ctx_t *prsp;
167 nx_hostrq_rds_ring_t *prq_rds;
168 nx_hostrq_sds_ring_t *prq_sds;
169 nx_cardrsp_rds_ring_t *prsp_rds;
170 nx_cardrsp_sds_ring_t *prsp_sds;
171 struct nx_host_rds_ring *rds_ring;
173 dma_addr_t hostrq_phys_addr, cardrsp_phys_addr;
176 int i, nrds_rings, nsds_rings;
177 size_t rq_size, rsp_size;
182 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx[0];
184 /* only one sds ring for now */
185 nrds_rings = adapter->max_rds_rings;
189 SIZEOF_HOSTRQ_RX(nx_hostrq_rx_ctx_t, nrds_rings, nsds_rings);
191 SIZEOF_CARDRSP_RX(nx_cardrsp_rx_ctx_t, nrds_rings, nsds_rings);
193 addr = pci_alloc_consistent(adapter->pdev,
194 rq_size, &hostrq_phys_addr);
197 prq = (nx_hostrq_rx_ctx_t *)addr;
199 addr = pci_alloc_consistent(adapter->pdev,
200 rsp_size, &cardrsp_phys_addr);
205 prsp = (nx_cardrsp_rx_ctx_t *)addr;
207 prq->host_rsp_dma_addr = cpu_to_le64(cardrsp_phys_addr);
209 cap = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN);
210 cap |= (NX_CAP0_JUMBO_CONTIGUOUS | NX_CAP0_LRO_CONTIGUOUS);
212 prq->capabilities[0] = cpu_to_le32(cap);
213 prq->host_int_crb_mode =
214 cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
215 prq->host_rds_crb_mode =
216 cpu_to_le32(NX_HOST_RDS_CRB_MODE_UNIQUE);
218 prq->num_rds_rings = cpu_to_le16(nrds_rings);
219 prq->num_sds_rings = cpu_to_le16(nsds_rings);
220 prq->rds_ring_offset = cpu_to_le32(0);
222 val = le32_to_cpu(prq->rds_ring_offset) +
223 (sizeof(nx_hostrq_rds_ring_t) * nrds_rings);
224 prq->sds_ring_offset = cpu_to_le32(val);
226 prq_rds = (nx_hostrq_rds_ring_t *)(prq->data +
227 le32_to_cpu(prq->rds_ring_offset));
229 for (i = 0; i < nrds_rings; i++) {
231 rds_ring = &recv_ctx->rds_rings[i];
233 prq_rds[i].host_phys_addr = cpu_to_le64(rds_ring->phys_addr);
234 prq_rds[i].ring_size = cpu_to_le32(rds_ring->max_rx_desc_count);
235 prq_rds[i].ring_kind = cpu_to_le32(i);
236 prq_rds[i].buff_size = cpu_to_le64(rds_ring->dma_size);
239 prq_sds = (nx_hostrq_sds_ring_t *)(prq->data +
240 le32_to_cpu(prq->sds_ring_offset));
242 prq_sds[0].host_phys_addr =
243 cpu_to_le64(recv_ctx->rcv_status_desc_phys_addr);
244 prq_sds[0].ring_size = cpu_to_le32(adapter->max_rx_desc_count);
245 /* only one msix vector for now */
246 prq_sds[0].msi_index = cpu_to_le16(0);
248 phys_addr = hostrq_phys_addr;
249 err = netxen_issue_cmd(adapter,
250 adapter->ahw.pci_func,
252 (u32)(phys_addr >> 32),
253 (u32)(phys_addr & 0xffffffff),
255 NX_CDRP_CMD_CREATE_RX_CTX);
258 "Failed to create rx ctx in firmware%d\n", err);
263 prsp_rds = ((nx_cardrsp_rds_ring_t *)
264 &prsp->data[le32_to_cpu(prsp->rds_ring_offset)]);
266 for (i = 0; i < le16_to_cpu(prsp->num_rds_rings); i++) {
267 rds_ring = &recv_ctx->rds_rings[i];
269 reg = le32_to_cpu(prsp_rds[i].host_producer_crb);
270 rds_ring->crb_rcv_producer = NETXEN_NIC_REG(reg - 0x200);
273 prsp_sds = ((nx_cardrsp_sds_ring_t *)
274 &prsp->data[le32_to_cpu(prsp->sds_ring_offset)]);
275 reg = le32_to_cpu(prsp_sds[0].host_consumer_crb);
276 recv_ctx->crb_sts_consumer = NETXEN_NIC_REG(reg - 0x200);
278 reg = le32_to_cpu(prsp_sds[0].interrupt_crb);
279 adapter->crb_intr_mask = NETXEN_NIC_REG(reg - 0x200);
281 recv_ctx->state = le32_to_cpu(prsp->host_ctx_state);
282 recv_ctx->context_id = le16_to_cpu(prsp->context_id);
283 recv_ctx->virt_port = prsp->virt_port;
286 pci_free_consistent(adapter->pdev, rsp_size, prsp, cardrsp_phys_addr);
288 pci_free_consistent(adapter->pdev, rq_size, prq, hostrq_phys_addr);
293 nx_fw_cmd_destroy_rx_ctx(struct netxen_adapter *adapter)
295 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx[0];
297 if (netxen_issue_cmd(adapter,
298 adapter->ahw.pci_func,
300 recv_ctx->context_id,
301 NX_DESTROY_CTX_RESET,
303 NX_CDRP_CMD_DESTROY_RX_CTX)) {
306 "%s: Failed to destroy rx ctx in firmware\n",
307 netxen_nic_driver_name);
312 nx_fw_cmd_create_tx_ctx(struct netxen_adapter *adapter)
314 nx_hostrq_tx_ctx_t *prq;
315 nx_hostrq_cds_ring_t *prq_cds;
316 nx_cardrsp_tx_ctx_t *prsp;
317 void *rq_addr, *rsp_addr;
318 size_t rq_size, rsp_size;
321 u64 offset, phys_addr;
322 dma_addr_t rq_phys_addr, rsp_phys_addr;
324 rq_size = SIZEOF_HOSTRQ_TX(nx_hostrq_tx_ctx_t);
325 rq_addr = pci_alloc_consistent(adapter->pdev,
326 rq_size, &rq_phys_addr);
330 rsp_size = SIZEOF_CARDRSP_TX(nx_cardrsp_tx_ctx_t);
331 rsp_addr = pci_alloc_consistent(adapter->pdev,
332 rsp_size, &rsp_phys_addr);
338 memset(rq_addr, 0, rq_size);
339 prq = (nx_hostrq_tx_ctx_t *)rq_addr;
341 memset(rsp_addr, 0, rsp_size);
342 prsp = (nx_cardrsp_tx_ctx_t *)rsp_addr;
344 prq->host_rsp_dma_addr = cpu_to_le64(rsp_phys_addr);
346 temp = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN | NX_CAP0_LSO);
347 prq->capabilities[0] = cpu_to_le32(temp);
349 prq->host_int_crb_mode =
350 cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
352 prq->interrupt_ctl = 0;
355 prq->dummy_dma_addr = cpu_to_le64(adapter->dummy_dma.phys_addr);
357 offset = adapter->ctx_desc_phys_addr+sizeof(struct netxen_ring_ctx);
358 prq->cmd_cons_dma_addr = cpu_to_le64(offset);
360 prq_cds = &prq->cds_ring;
362 prq_cds->host_phys_addr =
363 cpu_to_le64(adapter->ahw.cmd_desc_phys_addr);
365 prq_cds->ring_size = cpu_to_le32(adapter->max_tx_desc_count);
367 phys_addr = rq_phys_addr;
368 err = netxen_issue_cmd(adapter,
369 adapter->ahw.pci_func,
371 (u32)(phys_addr >> 32),
372 ((u32)phys_addr & 0xffffffff),
374 NX_CDRP_CMD_CREATE_TX_CTX);
376 if (err == NX_RCODE_SUCCESS) {
377 temp = le32_to_cpu(prsp->cds_ring.host_producer_crb);
378 adapter->crb_addr_cmd_producer =
379 NETXEN_NIC_REG(temp - 0x200);
382 le32_to_cpu(prsp->host_ctx_state);
384 adapter->tx_context_id =
385 le16_to_cpu(prsp->context_id);
388 "Failed to create tx ctx in firmware%d\n", err);
392 pci_free_consistent(adapter->pdev, rsp_size, rsp_addr, rsp_phys_addr);
395 pci_free_consistent(adapter->pdev, rq_size, rq_addr, rq_phys_addr);
401 nx_fw_cmd_destroy_tx_ctx(struct netxen_adapter *adapter)
403 if (netxen_issue_cmd(adapter,
404 adapter->ahw.pci_func,
406 adapter->tx_context_id,
407 NX_DESTROY_CTX_RESET,
409 NX_CDRP_CMD_DESTROY_TX_CTX)) {
412 "%s: Failed to destroy tx ctx in firmware\n",
413 netxen_nic_driver_name);
417 static u64 ctx_addr_sig_regs[][3] = {
418 {NETXEN_NIC_REG(0x188), NETXEN_NIC_REG(0x18c), NETXEN_NIC_REG(0x1c0)},
419 {NETXEN_NIC_REG(0x190), NETXEN_NIC_REG(0x194), NETXEN_NIC_REG(0x1c4)},
420 {NETXEN_NIC_REG(0x198), NETXEN_NIC_REG(0x19c), NETXEN_NIC_REG(0x1c8)},
421 {NETXEN_NIC_REG(0x1a0), NETXEN_NIC_REG(0x1a4), NETXEN_NIC_REG(0x1cc)}
424 #define CRB_CTX_ADDR_REG_LO(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][0])
425 #define CRB_CTX_ADDR_REG_HI(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][2])
426 #define CRB_CTX_SIGNATURE_REG(FUNC_ID) (ctx_addr_sig_regs[FUNC_ID][1])
428 #define lower32(x) ((u32)((x) & 0xffffffff))
429 #define upper32(x) ((u32)(((u64)(x) >> 32) & 0xffffffff))
431 static struct netxen_recv_crb recv_crb_registers[] = {
434 /* crb_rcv_producer: */
436 NETXEN_NIC_REG(0x100),
438 NETXEN_NIC_REG(0x110),
440 NETXEN_NIC_REG(0x120)
442 /* crb_sts_consumer: */
443 NETXEN_NIC_REG(0x138),
447 /* crb_rcv_producer: */
449 NETXEN_NIC_REG(0x144),
451 NETXEN_NIC_REG(0x154),
453 NETXEN_NIC_REG(0x164)
455 /* crb_sts_consumer: */
456 NETXEN_NIC_REG(0x17c),
460 /* crb_rcv_producer: */
462 NETXEN_NIC_REG(0x1d8),
464 NETXEN_NIC_REG(0x1f8),
466 NETXEN_NIC_REG(0x208)
468 /* crb_sts_consumer: */
469 NETXEN_NIC_REG(0x220),
473 /* crb_rcv_producer: */
475 NETXEN_NIC_REG(0x22c),
477 NETXEN_NIC_REG(0x23c),
479 NETXEN_NIC_REG(0x24c)
481 /* crb_sts_consumer: */
482 NETXEN_NIC_REG(0x264),
487 netxen_init_old_ctx(struct netxen_adapter *adapter)
489 struct netxen_recv_context *recv_ctx;
490 struct nx_host_rds_ring *rds_ring;
492 int func_id = adapter->portnum;
494 adapter->ctx_desc->cmd_ring_addr =
495 cpu_to_le64(adapter->ahw.cmd_desc_phys_addr);
496 adapter->ctx_desc->cmd_ring_size =
497 cpu_to_le32(adapter->max_tx_desc_count);
499 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
500 recv_ctx = &adapter->recv_ctx[ctx];
502 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
503 rds_ring = &recv_ctx->rds_rings[ring];
505 adapter->ctx_desc->rcv_ctx[ring].rcv_ring_addr =
506 cpu_to_le64(rds_ring->phys_addr);
507 adapter->ctx_desc->rcv_ctx[ring].rcv_ring_size =
508 cpu_to_le32(rds_ring->max_rx_desc_count);
510 adapter->ctx_desc->sts_ring_addr =
511 cpu_to_le64(recv_ctx->rcv_status_desc_phys_addr);
512 adapter->ctx_desc->sts_ring_size =
513 cpu_to_le32(adapter->max_rx_desc_count);
516 adapter->pci_write_normalize(adapter, CRB_CTX_ADDR_REG_LO(func_id),
517 lower32(adapter->ctx_desc_phys_addr));
518 adapter->pci_write_normalize(adapter, CRB_CTX_ADDR_REG_HI(func_id),
519 upper32(adapter->ctx_desc_phys_addr));
520 adapter->pci_write_normalize(adapter, CRB_CTX_SIGNATURE_REG(func_id),
521 NETXEN_CTX_SIGNATURE | func_id);
525 static uint32_t sw_int_mask[4] = {
526 CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1,
527 CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3
530 int netxen_alloc_hw_resources(struct netxen_adapter *adapter)
532 struct netxen_hardware_context *hw = &adapter->ahw;
537 struct netxen_recv_context *recv_ctx;
538 struct nx_host_rds_ring *rds_ring;
540 err = netxen_receive_peg_ready(adapter);
542 printk(KERN_ERR "Rcv Peg initialization not complete:%x.\n",
547 addr = pci_alloc_consistent(adapter->pdev,
548 sizeof(struct netxen_ring_ctx) + sizeof(uint32_t),
549 &adapter->ctx_desc_phys_addr);
552 DPRINTK(ERR, "failed to allocate hw context\n");
555 memset(addr, 0, sizeof(struct netxen_ring_ctx));
556 adapter->ctx_desc = (struct netxen_ring_ctx *)addr;
557 adapter->ctx_desc->ctx_id = cpu_to_le32(adapter->portnum);
558 adapter->ctx_desc->cmd_consumer_offset =
559 cpu_to_le64(adapter->ctx_desc_phys_addr +
560 sizeof(struct netxen_ring_ctx));
561 adapter->cmd_consumer =
562 (__le32 *)(((char *)addr) + sizeof(struct netxen_ring_ctx));
565 addr = pci_alloc_consistent(adapter->pdev,
566 sizeof(struct cmd_desc_type0) *
567 adapter->max_tx_desc_count,
568 &hw->cmd_desc_phys_addr);
571 printk(KERN_ERR "%s failed to allocate tx desc ring\n",
572 netxen_nic_driver_name);
576 hw->cmd_desc_head = (struct cmd_desc_type0 *)addr;
578 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
579 recv_ctx = &adapter->recv_ctx[ctx];
581 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
583 rds_ring = &recv_ctx->rds_rings[ring];
584 addr = pci_alloc_consistent(adapter->pdev,
586 &rds_ring->phys_addr);
588 printk(KERN_ERR "%s failed to allocate rx "
590 netxen_nic_driver_name, ring);
594 rds_ring->desc_head = (struct rcv_desc *)addr;
596 if (adapter->fw_major < 4)
597 rds_ring->crb_rcv_producer =
598 recv_crb_registers[adapter->portnum].
599 crb_rcv_producer[ring];
602 /* status desc ring */
603 addr = pci_alloc_consistent(adapter->pdev,
604 STATUS_DESC_RINGSIZE,
605 &recv_ctx->rcv_status_desc_phys_addr);
607 printk(KERN_ERR "%s failed to allocate sts desc ring\n",
608 netxen_nic_driver_name);
612 recv_ctx->rcv_status_desc_head = (struct status_desc *)addr;
614 if (adapter->fw_major < 4)
615 recv_ctx->crb_sts_consumer =
616 recv_crb_registers[adapter->portnum].
620 if (adapter->fw_major >= 4) {
621 adapter->intr_scheme = INTR_SCHEME_PERPORT;
622 adapter->msi_mode = MSI_MODE_MULTIFUNC;
624 err = nx_fw_cmd_create_rx_ctx(adapter);
627 err = nx_fw_cmd_create_tx_ctx(adapter);
632 adapter->intr_scheme = adapter->pci_read_normalize(adapter,
633 CRB_NIC_CAPABILITIES_FW);
634 adapter->msi_mode = adapter->pci_read_normalize(adapter,
635 CRB_NIC_MSI_MODE_FW);
636 adapter->crb_intr_mask = sw_int_mask[adapter->portnum];
638 err = netxen_init_old_ctx(adapter);
640 netxen_free_hw_resources(adapter);
649 netxen_free_hw_resources(adapter);
653 void netxen_free_hw_resources(struct netxen_adapter *adapter)
655 struct netxen_recv_context *recv_ctx;
656 struct nx_host_rds_ring *rds_ring;
659 if (adapter->fw_major >= 4) {
660 nx_fw_cmd_destroy_tx_ctx(adapter);
661 nx_fw_cmd_destroy_rx_ctx(adapter);
664 if (adapter->ctx_desc != NULL) {
665 pci_free_consistent(adapter->pdev,
666 sizeof(struct netxen_ring_ctx) +
669 adapter->ctx_desc_phys_addr);
670 adapter->ctx_desc = NULL;
673 if (adapter->ahw.cmd_desc_head != NULL) {
674 pci_free_consistent(adapter->pdev,
675 sizeof(struct cmd_desc_type0) *
676 adapter->max_tx_desc_count,
677 adapter->ahw.cmd_desc_head,
678 adapter->ahw.cmd_desc_phys_addr);
679 adapter->ahw.cmd_desc_head = NULL;
682 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
683 recv_ctx = &adapter->recv_ctx[ctx];
684 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
685 rds_ring = &recv_ctx->rds_rings[ring];
687 if (rds_ring->desc_head != NULL) {
688 pci_free_consistent(adapter->pdev,
691 rds_ring->phys_addr);
692 rds_ring->desc_head = NULL;
696 if (recv_ctx->rcv_status_desc_head != NULL) {
697 pci_free_consistent(adapter->pdev,
698 STATUS_DESC_RINGSIZE,
699 recv_ctx->rcv_status_desc_head,
700 recv_ctx->rcv_status_desc_phys_addr);
701 recv_ctx->rcv_status_desc_head = NULL;