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