Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / 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         if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
207                 cap |= NX_CAP0_HW_LRO;
208
209         prq->capabilities[0] = cpu_to_le32(cap);
210         prq->host_int_crb_mode =
211                 cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
212         prq->host_rds_crb_mode =
213                 cpu_to_le32(NX_HOST_RDS_CRB_MODE_UNIQUE);
214
215         prq->num_rds_rings = cpu_to_le16(nrds_rings);
216         prq->num_sds_rings = cpu_to_le16(nsds_rings);
217         prq->rds_ring_offset = cpu_to_le32(0);
218
219         val = le32_to_cpu(prq->rds_ring_offset) +
220                 (sizeof(nx_hostrq_rds_ring_t) * nrds_rings);
221         prq->sds_ring_offset = cpu_to_le32(val);
222
223         prq_rds = (nx_hostrq_rds_ring_t *)(prq->data +
224                         le32_to_cpu(prq->rds_ring_offset));
225
226         for (i = 0; i < nrds_rings; i++) {
227
228                 rds_ring = &recv_ctx->rds_rings[i];
229
230                 prq_rds[i].host_phys_addr = cpu_to_le64(rds_ring->phys_addr);
231                 prq_rds[i].ring_size = cpu_to_le32(rds_ring->num_desc);
232                 prq_rds[i].ring_kind = cpu_to_le32(i);
233                 prq_rds[i].buff_size = cpu_to_le64(rds_ring->dma_size);
234         }
235
236         prq_sds = (nx_hostrq_sds_ring_t *)(prq->data +
237                         le32_to_cpu(prq->sds_ring_offset));
238
239         for (i = 0; i < nsds_rings; i++) {
240
241                 sds_ring = &recv_ctx->sds_rings[i];
242
243                 prq_sds[i].host_phys_addr = cpu_to_le64(sds_ring->phys_addr);
244                 prq_sds[i].ring_size = cpu_to_le32(sds_ring->num_desc);
245                 prq_sds[i].msi_index = cpu_to_le16(i);
246         }
247
248         phys_addr = hostrq_phys_addr;
249         err = netxen_issue_cmd(adapter,
250                         adapter->ahw.pci_func,
251                         NXHAL_VERSION,
252                         (u32)(phys_addr >> 32),
253                         (u32)(phys_addr & 0xffffffff),
254                         rq_size,
255                         NX_CDRP_CMD_CREATE_RX_CTX);
256         if (err) {
257                 printk(KERN_WARNING
258                         "Failed to create rx ctx in firmware%d\n", err);
259                 goto out_free_rsp;
260         }
261
262
263         prsp_rds = ((nx_cardrsp_rds_ring_t *)
264                          &prsp->data[le32_to_cpu(prsp->rds_ring_offset)]);
265
266         for (i = 0; i < le16_to_cpu(prsp->num_rds_rings); i++) {
267                 rds_ring = &recv_ctx->rds_rings[i];
268
269                 reg = le32_to_cpu(prsp_rds[i].host_producer_crb);
270                 rds_ring->crb_rcv_producer = NETXEN_NIC_REG(reg - 0x200);
271         }
272
273         prsp_sds = ((nx_cardrsp_sds_ring_t *)
274                         &prsp->data[le32_to_cpu(prsp->sds_ring_offset)]);
275
276         for (i = 0; i < le16_to_cpu(prsp->num_sds_rings); i++) {
277                 sds_ring = &recv_ctx->sds_rings[i];
278
279                 reg = le32_to_cpu(prsp_sds[i].host_consumer_crb);
280                 sds_ring->crb_sts_consumer = NETXEN_NIC_REG(reg - 0x200);
281
282                 reg = le32_to_cpu(prsp_sds[i].interrupt_crb);
283                 sds_ring->crb_intr_mask = NETXEN_NIC_REG(reg - 0x200);
284         }
285
286         recv_ctx->state = le32_to_cpu(prsp->host_ctx_state);
287         recv_ctx->context_id = le16_to_cpu(prsp->context_id);
288         recv_ctx->virt_port = prsp->virt_port;
289
290 out_free_rsp:
291         pci_free_consistent(adapter->pdev, rsp_size, prsp, cardrsp_phys_addr);
292 out_free_rq:
293         pci_free_consistent(adapter->pdev, rq_size, prq, hostrq_phys_addr);
294         return err;
295 }
296
297 static void
298 nx_fw_cmd_destroy_rx_ctx(struct netxen_adapter *adapter)
299 {
300         struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
301
302         if (netxen_issue_cmd(adapter,
303                         adapter->ahw.pci_func,
304                         NXHAL_VERSION,
305                         recv_ctx->context_id,
306                         NX_DESTROY_CTX_RESET,
307                         0,
308                         NX_CDRP_CMD_DESTROY_RX_CTX)) {
309
310                 printk(KERN_WARNING
311                         "%s: Failed to destroy rx ctx in firmware\n",
312                         netxen_nic_driver_name);
313         }
314 }
315
316 static int
317 nx_fw_cmd_create_tx_ctx(struct netxen_adapter *adapter)
318 {
319         nx_hostrq_tx_ctx_t      *prq;
320         nx_hostrq_cds_ring_t    *prq_cds;
321         nx_cardrsp_tx_ctx_t     *prsp;
322         void    *rq_addr, *rsp_addr;
323         size_t  rq_size, rsp_size;
324         u32     temp;
325         int     err = 0;
326         u64     offset, phys_addr;
327         dma_addr_t      rq_phys_addr, rsp_phys_addr;
328         struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
329         struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
330
331         rq_size = SIZEOF_HOSTRQ_TX(nx_hostrq_tx_ctx_t);
332         rq_addr = pci_alloc_consistent(adapter->pdev,
333                 rq_size, &rq_phys_addr);
334         if (!rq_addr)
335                 return -ENOMEM;
336
337         rsp_size = SIZEOF_CARDRSP_TX(nx_cardrsp_tx_ctx_t);
338         rsp_addr = pci_alloc_consistent(adapter->pdev,
339                 rsp_size, &rsp_phys_addr);
340         if (!rsp_addr) {
341                 err = -ENOMEM;
342                 goto out_free_rq;
343         }
344
345         memset(rq_addr, 0, rq_size);
346         prq = (nx_hostrq_tx_ctx_t *)rq_addr;
347
348         memset(rsp_addr, 0, rsp_size);
349         prsp = (nx_cardrsp_tx_ctx_t *)rsp_addr;
350
351         prq->host_rsp_dma_addr = cpu_to_le64(rsp_phys_addr);
352
353         temp = (NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN | NX_CAP0_LSO);
354         prq->capabilities[0] = cpu_to_le32(temp);
355
356         prq->host_int_crb_mode =
357                 cpu_to_le32(NX_HOST_INT_CRB_MODE_SHARED);
358
359         prq->interrupt_ctl = 0;
360         prq->msi_index = 0;
361
362         prq->dummy_dma_addr = cpu_to_le64(adapter->dummy_dma.phys_addr);
363
364         offset = recv_ctx->phys_addr + sizeof(struct netxen_ring_ctx);
365         prq->cmd_cons_dma_addr = cpu_to_le64(offset);
366
367         prq_cds = &prq->cds_ring;
368
369         prq_cds->host_phys_addr = cpu_to_le64(tx_ring->phys_addr);
370         prq_cds->ring_size = cpu_to_le32(tx_ring->num_desc);
371
372         phys_addr = rq_phys_addr;
373         err = netxen_issue_cmd(adapter,
374                         adapter->ahw.pci_func,
375                         NXHAL_VERSION,
376                         (u32)(phys_addr >> 32),
377                         ((u32)phys_addr & 0xffffffff),
378                         rq_size,
379                         NX_CDRP_CMD_CREATE_TX_CTX);
380
381         if (err == NX_RCODE_SUCCESS) {
382                 temp = le32_to_cpu(prsp->cds_ring.host_producer_crb);
383                 tx_ring->crb_cmd_producer = NETXEN_NIC_REG(temp - 0x200);
384 #if 0
385                 adapter->tx_state =
386                         le32_to_cpu(prsp->host_ctx_state);
387 #endif
388                 adapter->tx_context_id =
389                         le16_to_cpu(prsp->context_id);
390         } else {
391                 printk(KERN_WARNING
392                         "Failed to create tx ctx in firmware%d\n", err);
393                 err = -EIO;
394         }
395
396         pci_free_consistent(adapter->pdev, rsp_size, rsp_addr, rsp_phys_addr);
397
398 out_free_rq:
399         pci_free_consistent(adapter->pdev, rq_size, rq_addr, rq_phys_addr);
400
401         return err;
402 }
403
404 static void
405 nx_fw_cmd_destroy_tx_ctx(struct netxen_adapter *adapter)
406 {
407         if (netxen_issue_cmd(adapter,
408                         adapter->ahw.pci_func,
409                         NXHAL_VERSION,
410                         adapter->tx_context_id,
411                         NX_DESTROY_CTX_RESET,
412                         0,
413                         NX_CDRP_CMD_DESTROY_TX_CTX)) {
414
415                 printk(KERN_WARNING
416                         "%s: Failed to destroy tx ctx in firmware\n",
417                         netxen_nic_driver_name);
418         }
419 }
420
421 static u64 ctx_addr_sig_regs[][3] = {
422         {NETXEN_NIC_REG(0x188), NETXEN_NIC_REG(0x18c), NETXEN_NIC_REG(0x1c0)},
423         {NETXEN_NIC_REG(0x190), NETXEN_NIC_REG(0x194), NETXEN_NIC_REG(0x1c4)},
424         {NETXEN_NIC_REG(0x198), NETXEN_NIC_REG(0x19c), NETXEN_NIC_REG(0x1c8)},
425         {NETXEN_NIC_REG(0x1a0), NETXEN_NIC_REG(0x1a4), NETXEN_NIC_REG(0x1cc)}
426 };
427
428 #define CRB_CTX_ADDR_REG_LO(FUNC_ID)    (ctx_addr_sig_regs[FUNC_ID][0])
429 #define CRB_CTX_ADDR_REG_HI(FUNC_ID)    (ctx_addr_sig_regs[FUNC_ID][2])
430 #define CRB_CTX_SIGNATURE_REG(FUNC_ID)  (ctx_addr_sig_regs[FUNC_ID][1])
431
432 #define lower32(x)      ((u32)((x) & 0xffffffff))
433 #define upper32(x)      ((u32)(((u64)(x) >> 32) & 0xffffffff))
434
435 static struct netxen_recv_crb recv_crb_registers[] = {
436         /* Instance 0 */
437         {
438                 /* crb_rcv_producer: */
439                 {
440                         NETXEN_NIC_REG(0x100),
441                         /* Jumbo frames */
442                         NETXEN_NIC_REG(0x110),
443                         /* LRO */
444                         NETXEN_NIC_REG(0x120)
445                 },
446                 /* crb_sts_consumer: */
447                 {
448                         NETXEN_NIC_REG(0x138),
449                         NETXEN_NIC_REG_2(0x000),
450                         NETXEN_NIC_REG_2(0x004),
451                         NETXEN_NIC_REG_2(0x008),
452                 },
453                 /* sw_int_mask */
454                 {
455                         CRB_SW_INT_MASK_0,
456                         NETXEN_NIC_REG_2(0x044),
457                         NETXEN_NIC_REG_2(0x048),
458                         NETXEN_NIC_REG_2(0x04c),
459                 },
460         },
461         /* Instance 1 */
462         {
463                 /* crb_rcv_producer: */
464                 {
465                         NETXEN_NIC_REG(0x144),
466                         /* Jumbo frames */
467                         NETXEN_NIC_REG(0x154),
468                         /* LRO */
469                         NETXEN_NIC_REG(0x164)
470                 },
471                 /* crb_sts_consumer: */
472                 {
473                         NETXEN_NIC_REG(0x17c),
474                         NETXEN_NIC_REG_2(0x020),
475                         NETXEN_NIC_REG_2(0x024),
476                         NETXEN_NIC_REG_2(0x028),
477                 },
478                 /* sw_int_mask */
479                 {
480                         CRB_SW_INT_MASK_1,
481                         NETXEN_NIC_REG_2(0x064),
482                         NETXEN_NIC_REG_2(0x068),
483                         NETXEN_NIC_REG_2(0x06c),
484                 },
485         },
486         /* Instance 2 */
487         {
488                 /* crb_rcv_producer: */
489                 {
490                         NETXEN_NIC_REG(0x1d8),
491                         /* Jumbo frames */
492                         NETXEN_NIC_REG(0x1f8),
493                         /* LRO */
494                         NETXEN_NIC_REG(0x208)
495                 },
496                 /* crb_sts_consumer: */
497                 {
498                         NETXEN_NIC_REG(0x220),
499                         NETXEN_NIC_REG_2(0x03c),
500                         NETXEN_NIC_REG_2(0x03c),
501                         NETXEN_NIC_REG_2(0x03c),
502                 },
503                 /* sw_int_mask */
504                 {
505                         CRB_SW_INT_MASK_2,
506                         NETXEN_NIC_REG_2(0x03c),
507                         NETXEN_NIC_REG_2(0x03c),
508                         NETXEN_NIC_REG_2(0x03c),
509                 },
510         },
511         /* Instance 3 */
512         {
513                 /* crb_rcv_producer: */
514                 {
515                         NETXEN_NIC_REG(0x22c),
516                         /* Jumbo frames */
517                         NETXEN_NIC_REG(0x23c),
518                         /* LRO */
519                         NETXEN_NIC_REG(0x24c)
520                 },
521                 /* crb_sts_consumer: */
522                 {
523                         NETXEN_NIC_REG(0x264),
524                         NETXEN_NIC_REG_2(0x03c),
525                         NETXEN_NIC_REG_2(0x03c),
526                         NETXEN_NIC_REG_2(0x03c),
527                 },
528                 /* sw_int_mask */
529                 {
530                         CRB_SW_INT_MASK_3,
531                         NETXEN_NIC_REG_2(0x03c),
532                         NETXEN_NIC_REG_2(0x03c),
533                         NETXEN_NIC_REG_2(0x03c),
534                 },
535         },
536 };
537
538 static int
539 netxen_init_old_ctx(struct netxen_adapter *adapter)
540 {
541         struct netxen_recv_context *recv_ctx;
542         struct nx_host_rds_ring *rds_ring;
543         struct nx_host_sds_ring *sds_ring;
544         struct nx_host_tx_ring *tx_ring;
545         int ring;
546         int port = adapter->portnum;
547         struct netxen_ring_ctx *hwctx;
548         u32 signature;
549
550         tx_ring = adapter->tx_ring;
551         recv_ctx = &adapter->recv_ctx;
552         hwctx = recv_ctx->hwctx;
553
554         hwctx->cmd_ring_addr = cpu_to_le64(tx_ring->phys_addr);
555         hwctx->cmd_ring_size = cpu_to_le32(tx_ring->num_desc);
556
557
558         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
559                 rds_ring = &recv_ctx->rds_rings[ring];
560
561                 hwctx->rcv_rings[ring].addr =
562                         cpu_to_le64(rds_ring->phys_addr);
563                 hwctx->rcv_rings[ring].size =
564                         cpu_to_le32(rds_ring->num_desc);
565         }
566
567         for (ring = 0; ring < adapter->max_sds_rings; ring++) {
568                 sds_ring = &recv_ctx->sds_rings[ring];
569
570                 if (ring == 0) {
571                         hwctx->sts_ring_addr = cpu_to_le64(sds_ring->phys_addr);
572                         hwctx->sts_ring_size = cpu_to_le32(sds_ring->num_desc);
573                 }
574                 hwctx->sts_rings[ring].addr = cpu_to_le64(sds_ring->phys_addr);
575                 hwctx->sts_rings[ring].size = cpu_to_le32(sds_ring->num_desc);
576                 hwctx->sts_rings[ring].msi_index = cpu_to_le16(ring);
577         }
578         hwctx->sts_ring_count = cpu_to_le32(adapter->max_sds_rings);
579
580         signature = (adapter->max_sds_rings > 1) ?
581                 NETXEN_CTX_SIGNATURE_V2 : NETXEN_CTX_SIGNATURE;
582
583         NXWR32(adapter, CRB_CTX_ADDR_REG_LO(port),
584                         lower32(recv_ctx->phys_addr));
585         NXWR32(adapter, CRB_CTX_ADDR_REG_HI(port),
586                         upper32(recv_ctx->phys_addr));
587         NXWR32(adapter, CRB_CTX_SIGNATURE_REG(port),
588                         signature | port);
589         return 0;
590 }
591
592 int netxen_alloc_hw_resources(struct netxen_adapter *adapter)
593 {
594         void *addr;
595         int err = 0;
596         int ring;
597         struct netxen_recv_context *recv_ctx;
598         struct nx_host_rds_ring *rds_ring;
599         struct nx_host_sds_ring *sds_ring;
600         struct nx_host_tx_ring *tx_ring;
601
602         struct pci_dev *pdev = adapter->pdev;
603         struct net_device *netdev = adapter->netdev;
604         int port = adapter->portnum;
605
606         recv_ctx = &adapter->recv_ctx;
607         tx_ring = adapter->tx_ring;
608
609         addr = pci_alloc_consistent(pdev,
610                         sizeof(struct netxen_ring_ctx) + sizeof(uint32_t),
611                         &recv_ctx->phys_addr);
612         if (addr == NULL) {
613                 dev_err(&pdev->dev, "failed to allocate hw context\n");
614                 return -ENOMEM;
615         }
616
617         memset(addr, 0, sizeof(struct netxen_ring_ctx));
618         recv_ctx->hwctx = (struct netxen_ring_ctx *)addr;
619         recv_ctx->hwctx->ctx_id = cpu_to_le32(port);
620         recv_ctx->hwctx->cmd_consumer_offset =
621                 cpu_to_le64(recv_ctx->phys_addr +
622                         sizeof(struct netxen_ring_ctx));
623         tx_ring->hw_consumer =
624                 (__le32 *)(((char *)addr) + sizeof(struct netxen_ring_ctx));
625
626         /* cmd desc ring */
627         addr = pci_alloc_consistent(pdev, TX_DESC_RINGSIZE(tx_ring),
628                         &tx_ring->phys_addr);
629
630         if (addr == NULL) {
631                 dev_err(&pdev->dev, "%s: failed to allocate tx desc ring\n",
632                                 netdev->name);
633                 return -ENOMEM;
634         }
635
636         tx_ring->desc_head = (struct cmd_desc_type0 *)addr;
637
638         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
639                 rds_ring = &recv_ctx->rds_rings[ring];
640                 addr = pci_alloc_consistent(adapter->pdev,
641                                 RCV_DESC_RINGSIZE(rds_ring),
642                                 &rds_ring->phys_addr);
643                 if (addr == NULL) {
644                         dev_err(&pdev->dev,
645                                 "%s: failed to allocate rds ring [%d]\n",
646                                 netdev->name, ring);
647                         err = -ENOMEM;
648                         goto err_out_free;
649                 }
650                 rds_ring->desc_head = (struct rcv_desc *)addr;
651
652                 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
653                         rds_ring->crb_rcv_producer =
654                                 recv_crb_registers[port].crb_rcv_producer[ring];
655         }
656
657         for (ring = 0; ring < adapter->max_sds_rings; ring++) {
658                 sds_ring = &recv_ctx->sds_rings[ring];
659
660                 addr = pci_alloc_consistent(adapter->pdev,
661                                 STATUS_DESC_RINGSIZE(sds_ring),
662                                 &sds_ring->phys_addr);
663                 if (addr == NULL) {
664                         dev_err(&pdev->dev,
665                                 "%s: failed to allocate sds ring [%d]\n",
666                                 netdev->name, ring);
667                         err = -ENOMEM;
668                         goto err_out_free;
669                 }
670                 sds_ring->desc_head = (struct status_desc *)addr;
671
672                 sds_ring->crb_sts_consumer =
673                         recv_crb_registers[port].crb_sts_consumer[ring];
674
675                 sds_ring->crb_intr_mask =
676                         recv_crb_registers[port].sw_int_mask[ring];
677         }
678
679
680         if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
681                 err = nx_fw_cmd_create_rx_ctx(adapter);
682                 if (err)
683                         goto err_out_free;
684                 err = nx_fw_cmd_create_tx_ctx(adapter);
685                 if (err)
686                         goto err_out_free;
687         } else {
688                 err = netxen_init_old_ctx(adapter);
689                 if (err)
690                         goto err_out_free;
691         }
692
693         return 0;
694
695 err_out_free:
696         netxen_free_hw_resources(adapter);
697         return err;
698 }
699
700 void netxen_free_hw_resources(struct netxen_adapter *adapter)
701 {
702         struct netxen_recv_context *recv_ctx;
703         struct nx_host_rds_ring *rds_ring;
704         struct nx_host_sds_ring *sds_ring;
705         struct nx_host_tx_ring *tx_ring;
706         int ring;
707
708         int port = adapter->portnum;
709
710         if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
711                 nx_fw_cmd_destroy_rx_ctx(adapter);
712                 nx_fw_cmd_destroy_tx_ctx(adapter);
713         } else {
714                 netxen_api_lock(adapter);
715                 NXWR32(adapter, CRB_CTX_SIGNATURE_REG(port),
716                                 NETXEN_CTX_D3_RESET | port);
717                 netxen_api_unlock(adapter);
718         }
719
720         /* Allow dma queues to drain after context reset */
721         msleep(20);
722
723         recv_ctx = &adapter->recv_ctx;
724
725         if (recv_ctx->hwctx != NULL) {
726                 pci_free_consistent(adapter->pdev,
727                                 sizeof(struct netxen_ring_ctx) +
728                                 sizeof(uint32_t),
729                                 recv_ctx->hwctx,
730                                 recv_ctx->phys_addr);
731                 recv_ctx->hwctx = NULL;
732         }
733
734         tx_ring = adapter->tx_ring;
735         if (tx_ring->desc_head != NULL) {
736                 pci_free_consistent(adapter->pdev,
737                                 TX_DESC_RINGSIZE(tx_ring),
738                                 tx_ring->desc_head, tx_ring->phys_addr);
739                 tx_ring->desc_head = NULL;
740         }
741
742         for (ring = 0; ring < adapter->max_rds_rings; ring++) {
743                 rds_ring = &recv_ctx->rds_rings[ring];
744
745                 if (rds_ring->desc_head != NULL) {
746                         pci_free_consistent(adapter->pdev,
747                                         RCV_DESC_RINGSIZE(rds_ring),
748                                         rds_ring->desc_head,
749                                         rds_ring->phys_addr);
750                         rds_ring->desc_head = NULL;
751                 }
752         }
753
754         for (ring = 0; ring < adapter->max_sds_rings; ring++) {
755                 sds_ring = &recv_ctx->sds_rings[ring];
756
757                 if (sds_ring->desc_head != NULL) {
758                         pci_free_consistent(adapter->pdev,
759                                 STATUS_DESC_RINGSIZE(sds_ring),
760                                 sds_ring->desc_head,
761                                 sds_ring->phys_addr);
762                         sds_ring->desc_head = NULL;
763                 }
764         }
765 }
766