e1000g_alloc.c revision 080575042aba2197b425ebfd52061dea061a9aa1
/*
* This file is provided under a CDDLv1 license. When using or
* redistributing this file, you may do so under this license.
* In redistributing this file this license must be included
* and no other modification of this header file is permitted.
*
* CDDL LICENSE SUMMARY
*
* Copyright(c) 1999 - 2007 Intel Corporation. All rights reserved.
*
* The contents of this file are subject to the terms of Version
* 1.0 of the Common Development and Distribution License (the "License").
*
* You should have received a copy of the License with this software.
* You can obtain a copy of the License at
* See the License for the specific language governing permissions
* and limitations under the License.
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms of the CDDLv1.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* **********************************************************************
* Module Name: *
* e1000galloc.c *
* *
* Abstract: *
* This file contains some routines that take care of init, *
* uninit, and memory allocation. *
* *
* *
* This driver runs on the following hardware: *
* - Wiseman based PCI gigabit ethernet adapters *
* *
* Environment: *
* Kernel Mode - *
* *
* **********************************************************************
*/
#include "e1000g_sw.h"
#include "e1000g_debug.h"
#define TX_SW_PKT_AREA_SZ \
static int e1000g_alloc_tx_descriptors(e1000g_tx_ring_t *);
static int e1000g_alloc_rx_descriptors(e1000g_rx_ring_t *);
static void e1000g_free_tx_descriptors(e1000g_tx_ring_t *);
static void e1000g_free_rx_descriptors(e1000g_rx_ring_t *);
static int e1000g_alloc_tx_packets(e1000g_tx_ring_t *);
static int e1000g_alloc_rx_packets(e1000g_rx_ring_t *);
static void e1000g_free_tx_packets(e1000g_tx_ring_t *);
static void e1000g_free_rx_packets(e1000g_rx_ring_t *);
static void e1000g_free_dma_buffer(dma_buffer_t *);
#ifdef __sparc
static void e1000g_free_dvma_buffer(dma_buffer_t *);
#endif
#ifdef __sparc
static ddi_dma_lim_t e1000g_dma_limits = {
(uint_t)0, /* dlim_addr_lo */
0x1, /* dlim_minxfer */
1024 /* dlim_speed */
};
#endif
#ifdef __sparc
#else
#endif
extern krwlock_t e1000g_dma_type_lock;
int
{
return (DDI_FAILURE);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* **********************************************************************
* Name: e1000g_alloc_descriptors *
* *
* Description: *
* This routine Allocates Neccesary Buffers for the device *
* It allocates memory for *
* Transmit Descriptor Area *
* Receive Descrpitor Area *
* *
* NOTE -- The device must have been reset before this routine *
* is called. *
* *
* Author: Hari Seshadri *
* Functions Called : *
* DDI mem functions called *
* ddi_dma_alloc_handle() allocates a new DMA handle. A DMA *
* handle is an opaque object used as a reference to subse- *
* quently allocated DMA resources. ddi_dma_alloc_handle() *
* accepts as parameters the device information referred to by *
* dip and the device's DMA attributes described by a *
* ddi_dma_attr(9S) structure. A successful call to *
* ddi_dma_alloc_handle() fills in the value pointed to by *
* handlep. A DMA handle must only be used by the device for *
* which it was allocated and is only valid for one I/O tran- *
* saction at a time. *
* *
* ddi_dma_mem_alloc() allocates memory for DMA transfers to or *
* from a device. The allocation will obey the alignment, pad- *
* ding constraints and device granularity as specified by the *
* DMA attributes (see ddi_dma_attr(9S)) passed to *
* ddi_dma_alloc_handle(9F) and the more restrictive attributes *
* imposed by the system.Flags should be set to DDI_DMA_STREAMING *
* if the device is doing sequential, unidirectional, *
* block-sized, and block- aligned transfers to or from memory. *
* *
* *
* ddi_dma_addr_bind_handle() allocates DMA resources for a *
* memory object such that a device can perform DMA to or from *
* the object. DMA resources are allocated considering the *
* device's DMA attributes as expressed by ddi_dma_attr(9S) *
* (see ddi_dma_alloc_handle(9F)). *
* ddi_dma_addr_bind_handle() fills in the first DMA cookie *
* pointed to by cookiep with the appropriate address, length, *
* and bus type. *ccountp is set to the number of DMA cookies *
* representing this DMA object. Subsequent DMA cookies must be *
* retrieved by calling ddi_dma_nextcookie(9F) the number of *
* times specified by *countp - 1. *
* *
* Arguments: *
* Adapter - A pointer to context sensitive "Adapter" structure. *
* *
* *
* Returns: *
* DDI_SUCCESS on success *
* DDI_FAILURE on error *
* *
* Modification log: *
* Date Who Description *
* -------- --- ----------------------------------------------------- *
* 11/11/98 Vinay Cleaned the entire function to prevents panics and *
* memory corruption *
* 17/11/98 Vinay Optimized it for proper usages of function calls *
* 30/04/99 Vinay Resolved some more memory problems related to race *
* conditions *
* **********************************************************************
*/
static int
{
int result;
if (result != DDI_SUCCESS)
return (DDI_FAILURE);
if (result != DDI_SUCCESS) {
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
{
int mystat;
/*
* Solaris 7 has a problem with allocating physically contiguous memory
* that is aligned on a 4K boundary. The transmit and rx descriptors
* need to aligned on a 4kbyte boundary. We first try to allocate the
* memory with DMA attributes set to 4K alignment and also no scatter/
* gather mechanism specified. In most cases, this does not allocate
* memory aligned at a 4Kbyte boundary. We then try asking for memory
* the amount of memory is less than 4k i.e a page size. If neither of
* these options work or if the number of descriptors is greater than
* 4K, ie more than 256 descriptors, we allocate 4k extra memory and
* and then align the memory at a 4k boundary.
*/
/*
* Memory allocation for the transmit buffer descriptors.
*/
/*
* DMA attributes set to asking for 4k alignment and no
* This typically does not succeed for Solaris 7, but
* might work for Solaris 2.6
*/
/*
* Allocate a new DMA handle for the transmit descriptor
* memory area.
*/
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
"Could not allocate tbd dma handle: %d", mystat);
return (DDI_FAILURE);
}
/*
* Allocate memory to DMA data to and from the transmit
* descriptors.
*/
size,
DDI_DMA_DONTWAIT, 0,
if ((mystat != DDI_SUCCESS) ||
if (mystat == DDI_SUCCESS) {
}
}
} else
alloc_flag = B_TRUE;
/*
* Initialize the entire transmit buffer descriptor area to zero
*/
if (alloc_flag)
/*
* If the previous DMA attributes setting could not give us contiguous
* memory or the number of descriptors is greater than the page size,
* we allocate 4K extra memory and then align it at a 4k boundary.
*/
if (!alloc_flag) {
/*
*/
/*
* Allocate a new DMA handle for the transmit descriptor memory
* area.
*/
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
"Could not re-allocate tbd dma handle: %d", mystat);
return (DDI_FAILURE);
}
/*
* Allocate memory to DMA data to and from the transmit
* descriptors.
*/
size,
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
"Could not allocate tbd dma memory: %d", mystat);
}
return (DDI_FAILURE);
} else
alloc_flag = B_TRUE;
/*
* Initialize the entire transmit buffer descriptor area to zero
*/
/*
* Memory has been allocated with the ddi_dma_mem_alloc call,
* but has not been aligned. We now align it on a 4k boundary.
*/
} /* alignment workaround */
/*
* Transmit buffer descriptor memory allocation succeeded
*/
/*
* Allocates DMA resources for the memory that was allocated by
* the ddi_dma_mem_alloc call. The DMA resources then get bound to the
* the memory address
*/
if (mystat != DDI_SUCCESS) {
"Could not bind tbd dma resource: %d", mystat);
}
}
return (DDI_FAILURE);
}
if (cookie_count != 1) {
"Could not bind tbd dma resource in a single frag. "
return (DDI_FAILURE);
}
/*
* The FirstTxDescriptor is initialized to the physical address that
* is obtained from the ddi_dma_addr_bind_handle call
*/
return (DDI_SUCCESS);
}
static int
{
int mystat;
/*
* Memory allocation for the receive buffer descriptors.
*/
/*
* Asking for aligned memory with DMA attributes set for 4k alignment
*/
/*
* Allocate a new DMA handle for the receive descriptor
* memory area. re-use the tbd_dma_attr since rbd has
* same attributes.
*/
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
"Could not allocate rbd dma handle: %d", mystat);
return (DDI_FAILURE);
}
/*
* Allocate memory to DMA data to and from the receive
* descriptors.
*/
size,
DDI_DMA_DONTWAIT, 0,
/*
* Check if memory allocation succeeded and also if the
* allocated memory is aligned correctly.
*/
if ((mystat != DDI_SUCCESS) ||
if (mystat == DDI_SUCCESS) {
}
}
} else
alloc_flag = B_TRUE;
/*
* Initialize the allocated receive descriptor memory to zero.
*/
if (alloc_flag)
/*
* If memory allocation did not succeed or if number of descriptors is
* greater than a page size ( more than 256 descriptors ), do the
* alignment yourself
*/
if (!alloc_flag) {
/*
* Allocate a new DMA handle for the receive descriptor memory
* area. re-use the tbd_dma_attr since rbd has same attributes.
*/
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
"Could not re-allocate rbd dma handle: %d", mystat);
return (DDI_FAILURE);
}
/*
* Allocate memory to DMA data to and from the receive
* descriptors.
*/
size,
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
"Could not allocate rbd dma memory: %d", mystat);
}
return (DDI_FAILURE);
} else
alloc_flag = B_TRUE;
/*
* Initialize the allocated receive descriptor memory to zero.
*/
} /* alignment workaround */
/*
* The memory allocation of the receive descriptors succeeded
*/
/*
* Allocates DMA resources for the memory that was allocated by
* the ddi_dma_mem_alloc call.
*/
if (mystat != DDI_SUCCESS) {
"Could not bind rbd dma resource: %d", mystat);
}
}
return (DDI_FAILURE);
}
if (cookie_count != 1) {
"Could not bind rbd dma resource in a single frag. "
return (DDI_FAILURE);
}
/*
* Initialize the FirstRxDescriptor to the cookie address obtained
* from the ddi_dma_addr_bind_handle call.
*/
return (DDI_SUCCESS);
}
static void
{
}
}
}
}
static void
{
}
}
}
}
/*
* **********************************************************************
* Name: e1000g_alloc_packets *
* *
* Description: This routine Allocates Neccesary Buffers for the device *
* It allocates memory for *
* *
* Transmit packet Structure *
* Handle for Transmit buffers *
* Receive packet structure *
* Buffer for Receive packet *
* *
* *
* For ddi memory alloc routine see e1000g_Txalloc description *
* NOTE -- The device must have been reset before this routine *
* is called. *
* *
* Author: Hari Seshadri *
* Functions Called : *
* *
* *
* *
* Arguments: *
* Adapter - A pointer to our context sensitive "Adapter" *
* structure. *
* *
* *
* Returns: *
* DDI_SUCCESS on sucess *
* DDI_FAILURE on error *
* *
* *
* *
* Modification log: *
* Date Who Description *
* -------- --- ----------------------------------------------------- *
* 30/04/99 VA Cleaned code for memory corruptions, invalid DMA *
* attributes and prevent panics *
* **********************************************************************
*/
static int
{
int result;
if (result != DDI_SUCCESS) {
if (e1000g_dma_type == USE_DVMA) {
"No enough dvma resource for Tx packets, "
"trying to allocate dma buffers...\n");
goto again;
}
"Failed to allocate dma buffers for Tx packets\n");
return (DDI_FAILURE);
}
if (result != DDI_SUCCESS) {
if (e1000g_dma_type == USE_DVMA) {
"No enough dvma resource for Rx packets, "
"trying to allocate dma buffers...\n");
goto again;
}
"Failed to allocate dma buffers for Rx packets\n");
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
#ifdef __sparc
static int
{
int mystat;
&buf->dma_handle);
if (mystat != DDI_SUCCESS) {
"Could not allocate dvma buffer handle: %d\n", mystat);
return (DDI_FAILURE);
}
}
"Could not allocate dvma buffer memory\n");
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static void
{
} else {
return;
}
}
}
}
#endif
static int
{
int mystat;
DDI_DMA_DONTWAIT, 0,
&buf->dma_handle);
if (mystat != DDI_SUCCESS) {
"Could not allocate dma buffer handle: %d\n", mystat);
return (DDI_FAILURE);
}
DDI_DMA_DONTWAIT, 0,
if (mystat != DDI_SUCCESS) {
}
"Could not allocate dma buffer memory: %d\n", mystat);
return (DDI_FAILURE);
}
if (mystat != DDI_SUCCESS) {
}
}
"Could not bind buffer dma handle: %d\n", mystat);
return (DDI_FAILURE);
}
if (count != 1) {
}
}
}
"Could not bind buffer as a single frag. "
"Count = %d\n", count);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static void
{
} else {
return;
}
}
}
}
static int
{
int j;
int mystat;
/*
* Memory allocation for the Transmit software structure, the transmit
* software packet. This structure stores all the relevant information
* for transmitting a single packet.
*/
return (DDI_FAILURE);
/*
* Pre-allocate dma handles for transmit. These dma handles
* will be dynamically bound to the data buffers passed down
* from the upper layers at the time of transmitting. The
* dynamic binding only applies for the packets that are larger
* than the tx_bcopy_thresh.
*/
switch (e1000g_dma_type) {
#ifdef __sparc
case USE_DVMA:
&packet->tx_dma_handle);
break;
#endif
case USE_DMA:
DDI_DMA_DONTWAIT, 0,
&packet->tx_dma_handle);
break;
default:
break;
}
if (mystat != DDI_SUCCESS) {
"Could not allocate tx dma handle: %d\n", mystat);
goto tx_pkt_fail;
}
/*
* Pre-allocate transmit buffers for small packets that the
* size is less than tx_bcopy_thresh. The data of those small
* packets will be bcopy() to the transmit buffers instead of
* using dynamical DMA binding. For small packets, bcopy will
* bring better performance than DMA binding.
*/
switch (e1000g_dma_type) {
#ifdef __sparc
case USE_DVMA:
break;
#endif
case USE_DMA:
break;
default:
break;
}
if (mystat != DDI_SUCCESS) {
switch (e1000g_dma_type) {
#ifdef __sparc
case USE_DVMA:
break;
#endif
case USE_DMA:
break;
default:
break;
}
"Allocate Tx buffer fail\n");
goto tx_pkt_fail;
}
} /* for */
return (DDI_SUCCESS);
return (DDI_FAILURE);
}
static int
{
int i;
/*
* Allocate memory for the RX_SW_PACKET structures. Each one of these
* structures will contain a virtual and physical address to an actual
* receive buffer in host memory. Since we use one RX_SW_PACKET per
* received packet, the maximum number of RX_SW_PACKETs that we'll
* need is equal to the number of receive descriptors that we've
* allocated.
*
* Pre allocation for recv packet buffer. The Recv intr constructs
* a new mp using this buffer
*
* On Wiseman these Receive buffers must be aligned with 256 byte
* boundary
* Vinay, Apr19,2000
*/
for (i = 0; i < packet_num; i++) {
goto rx_pkt_fail;
}
return (DDI_SUCCESS);
return (DDI_FAILURE);
}
static PRX_SW_PACKET
{
int mystat;
"Cound not allocate memory for Rx SwPacket\n");
return (NULL);
}
/*
* Make sure that receive buffers are 256 byte aligned
*/
switch (e1000g_dma_type) {
#ifdef __sparc
case USE_DVMA:
break;
#endif
case USE_DMA:
break;
default:
break;
}
if (mystat != DDI_SUCCESS) {
"Failed to allocate Rx buffer\n");
return (NULL);
}
/*
* esballoc is changed to desballoc which
* is undocumented call but as per sun,
* we can use it. It gives better efficiency.
*/
}
return (packet);
}
void
{
}
#ifdef __sparc
case USE_DVMA:
break;
#endif
case USE_DMA:
break;
default:
break;
}
}
static void
{
}
}
continue;
}
}
static void
{
int j;
break;
/* Free the Tx DMA handle for dynamical binding */
#ifdef __sparc
case USE_DVMA:
break;
#endif
case USE_DMA:
break;
default:
break;
}
} else {
/*
* If the dma handle is NULL, then we don't
* need to check the packets left. For they
* have not been initialized or have been freed.
*/
break;
}
#ifdef __sparc
case USE_DVMA:
break;
#endif
case USE_DMA:
break;
default:
break;
}
}
}
}
/*
* **********************************************************************
* Name: e1000g_release_dma_resources *
* *
* Description: *
* This function release any pending buffers. that has been *
* previously allocated *
* *
* Parameter Passed: *
* *
* Return Value: *
* *
* Functions called: *
* *
* *
* **********************************************************************
*/
void
{
/*
* Release all the handles, memory and DMA resources that are
* allocated for the transmit buffer descriptors.
*/
/*
* Release all the handles, memory and DMA resources that are
* allocated for the receive buffer descriptors.
*/
/*
* Free Tx packet resources
*/
/*
* TX resources done, now free RX resources
*/
}