1 /*************************************************************************
2 Copyright (c) 2003-2007 Cavium Networks (support@cavium.com). All rights
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials provided
16 with the distribution.
18 * Neither the name of Cavium Networks nor the names of
19 its contributors may be used to endorse or promote products
20 derived from this software without specific prior written
23 This Software, including technical data, may be subject to U.S. export control laws, including the U.S. Export Administration Act and its associated regulations, and may be subject to export or import regulations in other countries.
25 TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
26 AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
28 *************************************************************************/
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
39 #include <sys/socket.h>
42 #include <net/ethernet.h>
45 #include "wrapper-cvmx-includes.h"
46 #include "ethernet-headers.h"
48 /* You can define GET_MBUF_QOS() to override how the mbuf output function
49 determines which output queue is used. The default implementation
50 always uses the base queue for the port. If, for example, you wanted
51 to use the m->priority fieid, define GET_MBUF_QOS as:
52 #define GET_MBUF_QOS(m) ((m)->priority) */
54 #define GET_MBUF_QOS(m) 0
61 * @param m Packet to send
62 * @param dev Device info structure
63 * @return Always returns zero
65 int cvm_oct_xmit(struct mbuf *m, struct ifnet *ifp)
67 cvmx_pko_command_word0_t pko_command;
68 cvmx_buf_ptr_t hw_buffer;
71 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
73 int32_t buffers_to_free;
76 /* Prefetch the private data structure.
77 It is larger that one cache line */
78 CVMX_PREFETCH(priv, 0);
80 /* Start off assuming no drop */
83 /* The check on CVMX_PKO_QUEUES_PER_PORT_* is designed to completely
84 remove "qos" in the event neither interface supports multiple queues
86 if ((CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 > 1) ||
87 (CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 > 1)) {
88 qos = GET_MBUF_QOS(m);
91 else if (qos >= cvmx_pko_get_num_queues(priv->port))
96 /* The CN3XXX series of parts has an errata (GMX-401) which causes the
97 GMX block to hang if a collision occurs towards the end of a
98 <68 byte packet. As a workaround for this, we pad packets to be
99 68 bytes whenever we are in half duplex mode. We don't handle
100 the case of having a small packet but no room to add the padding.
101 The kernel should always give us at least a cache line */
102 if (__predict_false(m->m_pkthdr.len < 64) && OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
103 cvmx_gmxx_prtx_cfg_t gmx_prt_cfg;
104 int interface = INTERFACE(priv->port);
105 int index = INDEX(priv->port);
108 /* We only need to pad packet in half duplex mode */
109 gmx_prt_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
110 if (gmx_prt_cfg.s.duplex == 0) {
111 static uint8_t pad[64];
113 if (!m_append(m, sizeof pad - m->m_pkthdr.len, pad))
114 printf("%s: unable to padd small packet.", __func__);
119 #ifdef OCTEON_VENDOR_RADISYS
121 * The RSYS4GBE will hang if asked to transmit a packet less than 60 bytes.
123 if (__predict_false(m->m_pkthdr.len < 60) &&
124 cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_CUST_RADISYS_RSYS4GBE) {
125 static uint8_t pad[60];
127 if (!m_append(m, sizeof pad - m->m_pkthdr.len, pad))
128 printf("%s: unable to pad small packet.", __func__);
133 * If the packet is not fragmented.
135 if (m->m_pkthdr.len == m->m_len) {
136 /* Build the PKO buffer pointer */
138 hw_buffer.s.addr = cvmx_ptr_to_phys(m->m_data);
139 hw_buffer.s.pool = 0;
140 hw_buffer.s.size = m->m_len;
142 /* Build the PKO command */
144 pko_command.s.segs = 1;
145 pko_command.s.dontfree = 1; /* Do not put this buffer into the FPA. */
154 * The packet is fragmented, we need to send a list of segments
155 * in memory we borrow from the WQE pool.
157 work = cvmx_fpa_alloc(CVMX_FPA_WQE_POOL);
165 gp = (uint64_t *)work;
166 for (n = m; n != NULL; n = n->m_next) {
167 if (segs == CVMX_FPA_WQE_POOL_SIZE / sizeof (uint64_t))
168 panic("%s: too many segments in packet; call m_collapse().", __func__);
170 /* Build the PKO buffer pointer */
172 hw_buffer.s.i = 1; /* Do not put this buffer into the FPA. */
173 hw_buffer.s.addr = cvmx_ptr_to_phys(n->m_data);
174 hw_buffer.s.pool = 0;
175 hw_buffer.s.size = n->m_len;
177 *gp++ = hw_buffer.u64;
181 /* Build the PKO buffer gather list pointer */
183 hw_buffer.s.addr = cvmx_ptr_to_phys(work);
184 hw_buffer.s.pool = CVMX_FPA_WQE_POOL;
185 hw_buffer.s.size = segs;
187 /* Build the PKO command */
189 pko_command.s.segs = segs;
190 pko_command.s.gather = 1;
191 pko_command.s.dontfree = 0; /* Put the WQE above back into the FPA. */
194 /* Finish building the PKO command */
195 pko_command.s.n2 = 1; /* Don't pollute L2 with the outgoing packet */
196 pko_command.s.reg0 = priv->fau+qos*4;
197 pko_command.s.total_bytes = m->m_pkthdr.len;
198 pko_command.s.size0 = CVMX_FAU_OP_SIZE_32;
199 pko_command.s.subone0 = 1;
201 /* Check if we can use the hardware checksumming */
202 if ((m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) != 0) {
203 /* Use hardware checksum calc */
204 pko_command.s.ipoffp1 = ETHER_HDR_LEN + 1;
209 * Could use a different free queue (and different FAU address) per
210 * core instead of per QoS, to reduce contention here.
212 IF_LOCK(&priv->tx_free_queue[qos]);
213 /* Get the number of mbufs in use by the hardware */
214 in_use = cvmx_fau_fetch_and_add32(priv->fau+qos*4, 1);
215 buffers_to_free = cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
217 cvmx_pko_send_packet_prepare(priv->port, priv->queue + qos, CVMX_PKO_LOCK_CMD_QUEUE);
219 /* Drop this packet if we have too many already queued to the HW */
220 if (_IF_QFULL(&priv->tx_free_queue[qos])) {
223 /* Send the packet to the output queue */
225 if (__predict_false(cvmx_pko_send_packet_finish(priv->port, priv->queue + qos, pko_command, hw_buffer, CVMX_PKO_LOCK_CMD_QUEUE))) {
226 DEBUGPRINT("%s: Failed to send the packet\n", if_name(ifp));
230 if (__predict_false(dropped)) {
232 cvmx_fau_atomic_add32(priv->fau+qos*4, -1);
235 /* Put this packet on the queue to be freed later */
236 _IF_ENQUEUE(&priv->tx_free_queue[qos], m);
238 /* Pass it to any BPF listeners. */
239 ETHER_BPF_MTAP(ifp, m);
242 ifp->if_obytes += m->m_pkthdr.len;
245 /* Free mbufs not in use by the hardware */
246 if (_IF_QLEN(&priv->tx_free_queue[qos]) > in_use) {
247 while (_IF_QLEN(&priv->tx_free_queue[qos]) > in_use) {
248 _IF_DEQUEUE(&priv->tx_free_queue[qos], m);
252 IF_UNLOCK(&priv->tx_free_queue[qos]);
259 * This function frees all mbufs that are currenty queued for TX.
261 * @param dev Device being shutdown
263 void cvm_oct_tx_shutdown(struct ifnet *ifp)
265 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
268 for (qos = 0; qos < 16; qos++) {
269 IF_DRAIN(&priv->tx_free_queue[qos]);