Home | History | Annotate | Line # | Download | only in ixgbe
ixgbe_netbsd.c revision 1.3.2.1
      1  1.3.2.1  pgoyette /* $NetBSD: ixgbe_netbsd.c,v 1.3.2.1 2017/01/07 08:56:40 pgoyette Exp $ */
      2      1.1    dyoung /*
      3      1.1    dyoung  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      4      1.1    dyoung  * All rights reserved.
      5      1.1    dyoung  *
      6      1.1    dyoung  * This code is derived from software contributed to The NetBSD Foundation
      7      1.1    dyoung  * by Coyote Point Systems, Inc.
      8      1.1    dyoung  *
      9      1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     10      1.1    dyoung  * modification, are permitted provided that the following conditions
     11      1.1    dyoung  * are met:
     12      1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     13      1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     14      1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     16      1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     17      1.1    dyoung  *
     18      1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19      1.1    dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20      1.1    dyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21      1.1    dyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22      1.1    dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23      1.1    dyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24      1.1    dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25      1.1    dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26      1.1    dyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27      1.1    dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28      1.1    dyoung  * POSSIBILITY OF SUCH DAMAGE.
     29      1.1    dyoung  */
     30      1.1    dyoung #include <sys/param.h>
     31      1.1    dyoung 
     32      1.1    dyoung #include <sys/atomic.h>
     33      1.1    dyoung #include <sys/bus.h>
     34      1.1    dyoung #include <sys/condvar.h>
     35      1.1    dyoung #include <sys/cpu.h>
     36      1.1    dyoung #include <sys/kmem.h>
     37      1.1    dyoung #include <sys/mbuf.h>
     38      1.1    dyoung #include <sys/mutex.h>
     39      1.1    dyoung #include <sys/queue.h>
     40      1.1    dyoung #include <sys/workqueue.h>
     41  1.3.2.1  pgoyette #include <dev/pci/pcivar.h>
     42      1.1    dyoung 
     43      1.1    dyoung #include "ixgbe_netbsd.h"
     44      1.1    dyoung 
     45      1.1    dyoung void
     46      1.1    dyoung ixgbe_dma_tag_destroy(ixgbe_dma_tag_t *dt)
     47      1.1    dyoung {
     48      1.1    dyoung 	kmem_free(dt, sizeof(*dt));
     49      1.1    dyoung }
     50      1.1    dyoung 
     51      1.1    dyoung int
     52      1.1    dyoung ixgbe_dma_tag_create(bus_dma_tag_t dmat, bus_size_t alignment,
     53      1.1    dyoung     bus_size_t boundary, bus_size_t maxsize, int nsegments,
     54      1.1    dyoung     bus_size_t maxsegsize, int flags, ixgbe_dma_tag_t **dtp)
     55      1.1    dyoung {
     56      1.1    dyoung 	ixgbe_dma_tag_t *dt;
     57      1.1    dyoung 
     58      1.1    dyoung 	*dtp = NULL;
     59      1.1    dyoung 
     60      1.3   msaitoh 	if ((dt = kmem_zalloc(sizeof(*dt), KM_SLEEP)) == NULL)
     61      1.1    dyoung 		return ENOMEM;
     62      1.1    dyoung 
     63      1.1    dyoung 	dt->dt_dmat = dmat;
     64      1.1    dyoung 	dt->dt_alignment = alignment;
     65      1.1    dyoung 	dt->dt_boundary = boundary;
     66      1.1    dyoung 	dt->dt_maxsize = maxsize;
     67      1.1    dyoung 	dt->dt_nsegments = nsegments;
     68      1.1    dyoung 	dt->dt_maxsegsize = maxsegsize;
     69      1.1    dyoung 	dt->dt_flags = flags;
     70      1.1    dyoung 	*dtp = dt;
     71      1.1    dyoung 
     72      1.1    dyoung 	return 0;
     73      1.1    dyoung }
     74      1.1    dyoung 
     75      1.1    dyoung void
     76      1.1    dyoung ixgbe_dmamap_destroy(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam)
     77      1.1    dyoung {
     78      1.1    dyoung 	bus_dmamap_destroy(dt->dt_dmat, dmam);
     79      1.1    dyoung }
     80      1.1    dyoung 
     81      1.1    dyoung void
     82      1.1    dyoung ixgbe_dmamap_sync(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam, int ops)
     83      1.1    dyoung {
     84      1.1    dyoung         bus_dmamap_sync(dt->dt_dmat, dmam, 0, dt->dt_maxsize, ops);
     85      1.1    dyoung }
     86      1.1    dyoung 
     87      1.1    dyoung void
     88      1.1    dyoung ixgbe_dmamap_unload(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam)
     89      1.1    dyoung {
     90      1.1    dyoung 	bus_dmamap_unload(dt->dt_dmat, dmam);
     91      1.1    dyoung }
     92      1.1    dyoung 
     93      1.1    dyoung int
     94      1.1    dyoung ixgbe_dmamap_create(ixgbe_dma_tag_t *dt, int flags, bus_dmamap_t *dmamp)
     95      1.1    dyoung {
     96      1.1    dyoung 	return bus_dmamap_create(dt->dt_dmat, dt->dt_maxsize, dt->dt_nsegments,
     97      1.1    dyoung 	    dt->dt_maxsegsize, dt->dt_boundary, flags, dmamp);
     98      1.1    dyoung }
     99      1.1    dyoung 
    100      1.1    dyoung static void
    101      1.1    dyoung ixgbe_putext(ixgbe_extmem_t *em)
    102      1.1    dyoung {
    103      1.1    dyoung 	ixgbe_extmem_head_t *eh = em->em_head;
    104      1.1    dyoung 
    105      1.1    dyoung 	mutex_enter(&eh->eh_mtx);
    106      1.1    dyoung 
    107      1.1    dyoung 	TAILQ_INSERT_HEAD(&eh->eh_freelist, em, em_link);
    108      1.1    dyoung 
    109      1.1    dyoung 	mutex_exit(&eh->eh_mtx);
    110      1.1    dyoung 
    111      1.1    dyoung 	return;
    112      1.1    dyoung }
    113      1.1    dyoung 
    114      1.1    dyoung static ixgbe_extmem_t *
    115      1.1    dyoung ixgbe_getext(ixgbe_extmem_head_t *eh, size_t size)
    116      1.1    dyoung {
    117      1.1    dyoung 	ixgbe_extmem_t *em;
    118      1.1    dyoung 
    119      1.1    dyoung 	mutex_enter(&eh->eh_mtx);
    120      1.1    dyoung 
    121      1.1    dyoung 	TAILQ_FOREACH(em, &eh->eh_freelist, em_link) {
    122      1.1    dyoung 		if (em->em_size >= size)
    123      1.1    dyoung 			break;
    124      1.1    dyoung 	}
    125      1.1    dyoung 
    126      1.1    dyoung 	if (em != NULL)
    127      1.1    dyoung 		TAILQ_REMOVE(&eh->eh_freelist, em, em_link);
    128      1.1    dyoung 
    129      1.1    dyoung 	mutex_exit(&eh->eh_mtx);
    130      1.1    dyoung 
    131      1.1    dyoung 	return em;
    132      1.1    dyoung }
    133      1.1    dyoung 
    134      1.1    dyoung static ixgbe_extmem_t *
    135      1.1    dyoung ixgbe_newext(ixgbe_extmem_head_t *eh, bus_dma_tag_t dmat, size_t size)
    136      1.1    dyoung {
    137      1.1    dyoung 	ixgbe_extmem_t *em;
    138      1.1    dyoung 	int nseg, rc;
    139      1.1    dyoung 
    140      1.3   msaitoh 	em = kmem_zalloc(sizeof(*em), KM_SLEEP);
    141      1.1    dyoung 
    142      1.1    dyoung 	if (em == NULL)
    143      1.1    dyoung 		return NULL;
    144      1.1    dyoung 
    145      1.1    dyoung 	rc = bus_dmamem_alloc(dmat, size, PAGE_SIZE, 0, &em->em_seg, 1, &nseg,
    146      1.3   msaitoh 	    BUS_DMA_WAITOK);
    147      1.1    dyoung 
    148      1.1    dyoung 	if (rc != 0)
    149      1.1    dyoung 		goto post_zalloc_err;
    150      1.1    dyoung 
    151      1.1    dyoung 	rc = bus_dmamem_map(dmat, &em->em_seg, 1, size, &em->em_vaddr,
    152      1.3   msaitoh 	    BUS_DMA_WAITOK);
    153      1.1    dyoung 
    154      1.1    dyoung 	if (rc != 0)
    155      1.1    dyoung 		goto post_dmamem_err;
    156      1.1    dyoung 
    157      1.1    dyoung 	em->em_dmat = dmat;
    158      1.1    dyoung 	em->em_size = size;
    159      1.1    dyoung 	em->em_head = eh;
    160      1.1    dyoung 
    161      1.1    dyoung 	return em;
    162      1.1    dyoung post_dmamem_err:
    163      1.1    dyoung 	bus_dmamem_free(dmat, &em->em_seg, 1);
    164      1.1    dyoung post_zalloc_err:
    165      1.1    dyoung 	kmem_free(em, sizeof(*em));
    166      1.1    dyoung 	return NULL;
    167      1.1    dyoung }
    168      1.1    dyoung 
    169      1.1    dyoung void
    170      1.1    dyoung ixgbe_jcl_reinit(ixgbe_extmem_head_t *eh, bus_dma_tag_t dmat, int nbuf,
    171      1.1    dyoung     size_t size)
    172      1.1    dyoung {
    173      1.1    dyoung 	int i;
    174      1.1    dyoung 	ixgbe_extmem_t *em;
    175      1.1    dyoung 
    176      1.1    dyoung 	if (!eh->eh_initialized) {
    177      1.1    dyoung 		TAILQ_INIT(&eh->eh_freelist);
    178      1.1    dyoung 		mutex_init(&eh->eh_mtx, MUTEX_DEFAULT, IPL_NET);
    179      1.1    dyoung 		eh->eh_initialized = true;
    180      1.1    dyoung 	}
    181      1.1    dyoung 
    182      1.1    dyoung 	while ((em = ixgbe_getext(eh, 0)) != NULL) {
    183      1.1    dyoung 		KASSERT(em->em_vaddr != NULL);
    184      1.1    dyoung 		bus_dmamem_unmap(dmat, em->em_vaddr, em->em_size);
    185      1.1    dyoung 		bus_dmamem_free(dmat, &em->em_seg, 1);
    186      1.1    dyoung 		memset(em, 0, sizeof(*em));
    187      1.1    dyoung 		kmem_free(em, sizeof(*em));
    188      1.1    dyoung 	}
    189      1.1    dyoung 
    190      1.1    dyoung 	for (i = 0; i < nbuf; i++) {
    191      1.1    dyoung 		if ((em = ixgbe_newext(eh, dmat, size)) == NULL) {
    192      1.1    dyoung 			printf("%s: only %d of %d jumbo buffers allocated\n",
    193      1.1    dyoung 			    __func__, i, nbuf);
    194      1.1    dyoung 			break;
    195      1.1    dyoung 		}
    196      1.1    dyoung 		ixgbe_putext(em);
    197      1.1    dyoung 	}
    198      1.1    dyoung }
    199      1.1    dyoung 
    200      1.1    dyoung static void
    201      1.1    dyoung ixgbe_jcl_free(struct mbuf *m, void *buf, size_t size, void *arg)
    202      1.1    dyoung {
    203      1.1    dyoung 	ixgbe_extmem_t *em = arg;
    204      1.1    dyoung 
    205      1.1    dyoung 	KASSERT(em->em_size == size);
    206      1.1    dyoung 
    207      1.1    dyoung 	ixgbe_putext(em);
    208      1.1    dyoung 	/* this is an abstraction violation, but it does not lead to a
    209      1.1    dyoung 	 * double-free
    210      1.1    dyoung 	 */
    211      1.1    dyoung 	if (__predict_true(m != NULL)) {
    212      1.1    dyoung 		KASSERT(m->m_type != MT_FREE);
    213      1.1    dyoung 		m->m_type = MT_FREE;
    214      1.1    dyoung 		pool_cache_put(mb_cache, m);
    215      1.1    dyoung 	}
    216      1.1    dyoung }
    217      1.1    dyoung 
    218      1.1    dyoung /* XXX need to wait for the system to finish with each jumbo mbuf and
    219      1.1    dyoung  * free it before detaching the driver from the device.
    220      1.1    dyoung  */
    221      1.1    dyoung struct mbuf *
    222      1.1    dyoung ixgbe_getjcl(ixgbe_extmem_head_t *eh, int nowait /* M_DONTWAIT */,
    223      1.1    dyoung     int type /* MT_DATA */, int flags /* M_PKTHDR */, size_t size)
    224      1.1    dyoung {
    225      1.1    dyoung 	ixgbe_extmem_t *em;
    226      1.1    dyoung 	struct mbuf *m;
    227      1.1    dyoung 
    228      1.1    dyoung 	if ((flags & M_PKTHDR) != 0)
    229      1.1    dyoung 		m = m_gethdr(nowait, type);
    230      1.1    dyoung 	else
    231      1.1    dyoung 		m = m_get(nowait, type);
    232      1.1    dyoung 
    233      1.1    dyoung 	if (m == NULL)
    234      1.1    dyoung 		return NULL;
    235      1.1    dyoung 
    236      1.1    dyoung 	em = ixgbe_getext(eh, size);
    237      1.1    dyoung 	if (em == NULL) {
    238      1.1    dyoung 		m_freem(m);
    239      1.1    dyoung 		return NULL;
    240      1.1    dyoung 	}
    241      1.1    dyoung 
    242      1.1    dyoung 	MEXTADD(m, em->em_vaddr, em->em_size, M_DEVBUF, &ixgbe_jcl_free, em);
    243      1.1    dyoung 
    244      1.1    dyoung 	if ((m->m_flags & M_EXT) == 0) {
    245      1.1    dyoung 		ixgbe_putext(em);
    246      1.1    dyoung 		m_freem(m);
    247      1.1    dyoung 		return NULL;
    248      1.1    dyoung 	}
    249      1.1    dyoung 
    250      1.1    dyoung 	return m;
    251      1.1    dyoung }
    252  1.3.2.1  pgoyette 
    253  1.3.2.1  pgoyette void
    254  1.3.2.1  pgoyette ixgbe_pci_enable_busmaster(pci_chipset_tag_t pc, pcitag_t tag)
    255  1.3.2.1  pgoyette {
    256  1.3.2.1  pgoyette 	pcireg_t	pci_cmd_word;
    257  1.3.2.1  pgoyette 
    258  1.3.2.1  pgoyette 	pci_cmd_word = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    259  1.3.2.1  pgoyette 	if (!(pci_cmd_word & PCI_COMMAND_MASTER_ENABLE)) {
    260  1.3.2.1  pgoyette 		pci_cmd_word |= PCI_COMMAND_MASTER_ENABLE;
    261  1.3.2.1  pgoyette 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, pci_cmd_word);
    262  1.3.2.1  pgoyette 	}
    263  1.3.2.1  pgoyette }
    264