| 1 |
/* Synopsys DesignWare Core Enterprise Ethernet (XLGMAC) Driver |
| 2 |
* |
| 3 |
* Copyright (c) 2017 Synopsys, Inc. (www.synopsys.com) |
| 4 |
* |
| 5 |
* This program is dual-licensed; you may select either version 2 of |
| 6 |
* the GNU General Public License ("GPL") or BSD license ("BSD"). |
| 7 |
* |
| 8 |
* This Synopsys DWC XLGMAC software driver and associated documentation |
| 9 |
* (hereinafter the "Software") is an unsupported proprietary work of |
| 10 |
* Synopsys, Inc. unless otherwise expressly agreed to in writing between |
| 11 |
* Synopsys and you. The Software IS NOT an item of Licensed Software or a |
| 12 |
* Licensed Product under any End User Software License Agreement or |
| 13 |
* Agreement for Licensed Products with Synopsys or any supplement thereto. |
| 14 |
* Synopsys is a registered trademark of Synopsys, Inc. Other names included |
| 15 |
* in the SOFTWARE may be the trademarks of their respective owners. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "dwc-xlgmac.h" |
| 19 |
#include "dwc-xlgmac-reg.h" |
| 20 |
|
| 21 |
static void xlgmac_unmap_desc_data(struct xlgmac_pdata *pdata, |
| 22 |
struct xlgmac_desc_data *desc_data) |
| 23 |
{ |
| 24 |
if (desc_data->skb_dma) { |
| 25 |
if (desc_data->mapped_as_page) { |
| 26 |
dma_unmap_page(pdata->dev, desc_data->skb_dma, |
| 27 |
desc_data->skb_dma_len, DMA_TO_DEVICE); |
| 28 |
} else { |
| 29 |
dma_unmap_single(pdata->dev, desc_data->skb_dma, |
| 30 |
desc_data->skb_dma_len, DMA_TO_DEVICE); |
| 31 |
} |
| 32 |
desc_data->skb_dma = 0; |
| 33 |
desc_data->skb_dma_len = 0; |
| 34 |
} |
| 35 |
|
| 36 |
if (desc_data->skb) { |
| 37 |
dev_kfree_skb_any(desc_data->skb); |
| 38 |
desc_data->skb = NULL; |
| 39 |
} |
| 40 |
|
| 41 |
if (desc_data->rx.hdr.pa.pages) |
| 42 |
put_page(desc_data->rx.hdr.pa.pages); |
| 43 |
|
| 44 |
if (desc_data->rx.hdr.pa_unmap.pages) { |
| 45 |
dma_unmap_page(pdata->dev, desc_data->rx.hdr.pa_unmap.pages_dma, |
| 46 |
desc_data->rx.hdr.pa_unmap.pages_len, |
| 47 |
DMA_FROM_DEVICE); |
| 48 |
put_page(desc_data->rx.hdr.pa_unmap.pages); |
| 49 |
} |
| 50 |
|
| 51 |
if (desc_data->rx.buf.pa.pages) |
| 52 |
put_page(desc_data->rx.buf.pa.pages); |
| 53 |
|
| 54 |
if (desc_data->rx.buf.pa_unmap.pages) { |
| 55 |
dma_unmap_page(pdata->dev, desc_data->rx.buf.pa_unmap.pages_dma, |
| 56 |
desc_data->rx.buf.pa_unmap.pages_len, |
| 57 |
DMA_FROM_DEVICE); |
| 58 |
put_page(desc_data->rx.buf.pa_unmap.pages); |
| 59 |
} |
| 60 |
|
| 61 |
memset(&desc_data->tx, 0, sizeof(desc_data->tx)); |
| 62 |
memset(&desc_data->rx, 0, sizeof(desc_data->rx)); |
| 63 |
|
| 64 |
desc_data->mapped_as_page = 0; |
| 65 |
|
| 66 |
if (desc_data->state_saved) { |
| 67 |
desc_data->state_saved = 0; |
| 68 |
desc_data->state.skb = NULL; |
| 69 |
desc_data->state.len = 0; |
| 70 |
desc_data->state.error = 0; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static void xlgmac_free_ring(struct xlgmac_pdata *pdata, |
| 75 |
struct xlgmac_ring *ring) |
| 76 |
{ |
| 77 |
struct xlgmac_desc_data *desc_data; |
| 78 |
unsigned int i; |
| 79 |
|
| 80 |
if (!ring) |
| 81 |
return; |
| 82 |
|
| 83 |
if (ring->desc_data_head) { |
| 84 |
for (i = 0; i < ring->dma_desc_count; i++) { |
| 85 |
desc_data = XLGMAC_GET_DESC_DATA(ring, i); |
| 86 |
xlgmac_unmap_desc_data(pdata, desc_data); |
| 87 |
} |
| 88 |
|
| 89 |
kfree(ring->desc_data_head); |
| 90 |
ring->desc_data_head = NULL; |
| 91 |
} |
| 92 |
|
| 93 |
if (ring->rx_hdr_pa.pages) { |
| 94 |
dma_unmap_page(pdata->dev, ring->rx_hdr_pa.pages_dma, |
| 95 |
ring->rx_hdr_pa.pages_len, DMA_FROM_DEVICE); |
| 96 |
put_page(ring->rx_hdr_pa.pages); |
| 97 |
|
| 98 |
ring->rx_hdr_pa.pages = NULL; |
| 99 |
ring->rx_hdr_pa.pages_len = 0; |
| 100 |
ring->rx_hdr_pa.pages_offset = 0; |
| 101 |
ring->rx_hdr_pa.pages_dma = 0; |
| 102 |
} |
| 103 |
|
| 104 |
if (ring->rx_buf_pa.pages) { |
| 105 |
dma_unmap_page(pdata->dev, ring->rx_buf_pa.pages_dma, |
| 106 |
ring->rx_buf_pa.pages_len, DMA_FROM_DEVICE); |
| 107 |
put_page(ring->rx_buf_pa.pages); |
| 108 |
|
| 109 |
ring->rx_buf_pa.pages = NULL; |
| 110 |
ring->rx_buf_pa.pages_len = 0; |
| 111 |
ring->rx_buf_pa.pages_offset = 0; |
| 112 |
ring->rx_buf_pa.pages_dma = 0; |
| 113 |
} |
| 114 |
|
| 115 |
if (ring->dma_desc_head) { |
| 116 |
dma_free_coherent(pdata->dev, |
| 117 |
(sizeof(struct xlgmac_dma_desc) * |
| 118 |
ring->dma_desc_count), |
| 119 |
ring->dma_desc_head, |
| 120 |
ring->dma_desc_head_addr); |
| 121 |
ring->dma_desc_head = NULL; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static int xlgmac_init_ring(struct xlgmac_pdata *pdata, |
| 126 |
struct xlgmac_ring *ring, |
| 127 |
unsigned int dma_desc_count) |
| 128 |
{ |
| 129 |
if (!ring) |
| 130 |
return 0; |
| 131 |
|
| 132 |
/* Descriptors */ |
| 133 |
ring->dma_desc_count = dma_desc_count; |
| 134 |
ring->dma_desc_head = dma_alloc_coherent(pdata->dev, |
| 135 |
(sizeof(struct xlgmac_dma_desc) * |
| 136 |
dma_desc_count), |
| 137 |
&ring->dma_desc_head_addr, |
| 138 |
GFP_KERNEL); |
| 139 |
if (!ring->dma_desc_head) |
| 140 |
return -ENOMEM; |
| 141 |
|
| 142 |
/* Array of descriptor data */ |
| 143 |
ring->desc_data_head = kzalloc_objs(struct xlgmac_desc_data, |
| 144 |
dma_desc_count); |
| 145 |
if (!ring->desc_data_head) |
| 146 |
return -ENOMEM; |
| 147 |
|
| 148 |
netif_dbg(pdata, drv, pdata->netdev, |
| 149 |
"dma_desc_head=%p, dma_desc_head_addr=%pad, desc_data_head=%p\n", |
| 150 |
ring->dma_desc_head, |
| 151 |
&ring->dma_desc_head_addr, |
| 152 |
ring->desc_data_head); |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
static void xlgmac_free_rings(struct xlgmac_pdata *pdata) |
| 158 |
{ |
| 159 |
struct xlgmac_channel *channel; |
| 160 |
unsigned int i; |
| 161 |
|
| 162 |
if (!pdata->channel_head) |
| 163 |
return; |
| 164 |
|
| 165 |
channel = pdata->channel_head; |
| 166 |
for (i = 0; i < pdata->channel_count; i++, channel++) { |
| 167 |
xlgmac_free_ring(pdata, channel->tx_ring); |
| 168 |
xlgmac_free_ring(pdata, channel->rx_ring); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
static int xlgmac_alloc_rings(struct xlgmac_pdata *pdata) |
| 173 |
{ |
| 174 |
struct xlgmac_channel *channel; |
| 175 |
unsigned int i; |
| 176 |
int ret; |
| 177 |
|
| 178 |
channel = pdata->channel_head; |
| 179 |
for (i = 0; i < pdata->channel_count; i++, channel++) { |
| 180 |
netif_dbg(pdata, drv, pdata->netdev, "%s - Tx ring:\n", |
| 181 |
channel->name); |
| 182 |
|
| 183 |
ret = xlgmac_init_ring(pdata, channel->tx_ring, |
| 184 |
pdata->tx_desc_count); |
| 185 |
|
| 186 |
if (ret) { |
| 187 |
netdev_alert(pdata->netdev, |
| 188 |
"error initializing Tx ring"); |
| 189 |
goto err_init_ring; |
| 190 |
} |
| 191 |
|
| 192 |
netif_dbg(pdata, drv, pdata->netdev, "%s - Rx ring:\n", |
| 193 |
channel->name); |
| 194 |
|
| 195 |
ret = xlgmac_init_ring(pdata, channel->rx_ring, |
| 196 |
pdata->rx_desc_count); |
| 197 |
if (ret) { |
| 198 |
netdev_alert(pdata->netdev, |
| 199 |
"error initializing Rx ring\n"); |
| 200 |
goto err_init_ring; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return 0; |
| 205 |
|
| 206 |
err_init_ring: |
| 207 |
xlgmac_free_rings(pdata); |
| 208 |
|
| 209 |
return ret; |
| 210 |
} |
| 211 |
|
| 212 |
static void xlgmac_free_channels(struct xlgmac_pdata *pdata) |
| 213 |
{ |
| 214 |
if (!pdata->channel_head) |
| 215 |
return; |
| 216 |
|
| 217 |
kfree(pdata->channel_head->tx_ring); |
| 218 |
pdata->channel_head->tx_ring = NULL; |
| 219 |
|
| 220 |
kfree(pdata->channel_head->rx_ring); |
| 221 |
pdata->channel_head->rx_ring = NULL; |
| 222 |
|
| 223 |
kfree(pdata->channel_head); |
| 224 |
|
| 225 |
pdata->channel_head = NULL; |
| 226 |
pdata->channel_count = 0; |
| 227 |
} |
| 228 |
|
| 229 |
static int xlgmac_alloc_channels(struct xlgmac_pdata *pdata) |
| 230 |
{ |
| 231 |
struct xlgmac_channel *channel_head, *channel; |
| 232 |
struct xlgmac_ring *tx_ring, *rx_ring; |
| 233 |
int ret = -ENOMEM; |
| 234 |
unsigned int i; |
| 235 |
|
| 236 |
channel_head = kzalloc_objs(struct xlgmac_channel, pdata->channel_count); |
| 237 |
if (!channel_head) |
| 238 |
return ret; |
| 239 |
|
| 240 |
netif_dbg(pdata, drv, pdata->netdev, |
| 241 |
"channel_head=%p\n", channel_head); |
| 242 |
|
| 243 |
tx_ring = kzalloc_objs(struct xlgmac_ring, pdata->tx_ring_count); |
| 244 |
if (!tx_ring) |
| 245 |
goto err_tx_ring; |
| 246 |
|
| 247 |
rx_ring = kzalloc_objs(struct xlgmac_ring, pdata->rx_ring_count); |
| 248 |
if (!rx_ring) |
| 249 |
goto err_rx_ring; |
| 250 |
|
| 251 |
for (i = 0, channel = channel_head; i < pdata->channel_count; |
| 252 |
i++, channel++) { |
| 253 |
snprintf(channel->name, sizeof(channel->name), "channel-%u", i); |
| 254 |
channel->pdata = pdata; |
| 255 |
channel->queue_index = i; |
| 256 |
channel->dma_regs = pdata->mac_regs + DMA_CH_BASE + |
| 257 |
(DMA_CH_INC * i); |
| 258 |
|
| 259 |
if (pdata->per_channel_irq) { |
| 260 |
/* Get the per DMA interrupt */ |
| 261 |
ret = pdata->channel_irq[i]; |
| 262 |
if (ret < 0) { |
| 263 |
netdev_err(pdata->netdev, |
| 264 |
"get_irq %u failed\n", |
| 265 |
i + 1); |
| 266 |
goto err_irq; |
| 267 |
} |
| 268 |
channel->dma_irq = ret; |
| 269 |
} |
| 270 |
|
| 271 |
if (i < pdata->tx_ring_count) |
| 272 |
channel->tx_ring = tx_ring++; |
| 273 |
|
| 274 |
if (i < pdata->rx_ring_count) |
| 275 |
channel->rx_ring = rx_ring++; |
| 276 |
|
| 277 |
netif_dbg(pdata, drv, pdata->netdev, |
| 278 |
"%s: dma_regs=%p, tx_ring=%p, rx_ring=%p\n", |
| 279 |
channel->name, channel->dma_regs, |
| 280 |
channel->tx_ring, channel->rx_ring); |
| 281 |
} |
| 282 |
|
| 283 |
pdata->channel_head = channel_head; |
| 284 |
|
| 285 |
return 0; |
| 286 |
|
| 287 |
err_irq: |
| 288 |
kfree(rx_ring); |
| 289 |
|
| 290 |
err_rx_ring: |
| 291 |
kfree(tx_ring); |
| 292 |
|
| 293 |
err_tx_ring: |
| 294 |
kfree(channel_head); |
| 295 |
|
| 296 |
return ret; |
| 297 |
} |
| 298 |
|
| 299 |
static void xlgmac_free_channels_and_rings(struct xlgmac_pdata *pdata) |
| 300 |
{ |
| 301 |
xlgmac_free_rings(pdata); |
| 302 |
|
| 303 |
xlgmac_free_channels(pdata); |
| 304 |
} |
| 305 |
|
| 306 |
static int xlgmac_alloc_channels_and_rings(struct xlgmac_pdata *pdata) |
| 307 |
{ |
| 308 |
int ret; |
| 309 |
|
| 310 |
ret = xlgmac_alloc_channels(pdata); |
| 311 |
if (ret) |
| 312 |
goto err_alloc; |
| 313 |
|
| 314 |
ret = xlgmac_alloc_rings(pdata); |
| 315 |
if (ret) |
| 316 |
goto err_alloc; |
| 317 |
|
| 318 |
return 0; |
| 319 |
|
| 320 |
err_alloc: |
| 321 |
xlgmac_free_channels_and_rings(pdata); |
| 322 |
|
| 323 |
return ret; |
| 324 |
} |
| 325 |
|
| 326 |
static int xlgmac_alloc_pages(struct xlgmac_pdata *pdata, |
| 327 |
struct xlgmac_page_alloc *pa, |
| 328 |
gfp_t gfp, int order) |
| 329 |
{ |
| 330 |
struct page *pages = NULL; |
| 331 |
dma_addr_t pages_dma; |
| 332 |
|
| 333 |
/* Try to obtain pages, decreasing order if necessary */ |
| 334 |
gfp |= __GFP_COMP | __GFP_NOWARN; |
| 335 |
while (order >= 0) { |
| 336 |
pages = alloc_pages(gfp, order); |
| 337 |
if (pages) |
| 338 |
break; |
| 339 |
|
| 340 |
order--; |
| 341 |
} |
| 342 |
if (!pages) |
| 343 |
return -ENOMEM; |
| 344 |
|
| 345 |
/* Map the pages */ |
| 346 |
pages_dma = dma_map_page(pdata->dev, pages, 0, |
| 347 |
PAGE_SIZE << order, DMA_FROM_DEVICE); |
| 348 |
if (dma_mapping_error(pdata->dev, pages_dma)) { |
| 349 |
put_page(pages); |
| 350 |
return -ENOMEM; |
| 351 |
} |
| 352 |
|
| 353 |
pa->pages = pages; |
| 354 |
pa->pages_len = PAGE_SIZE << order; |
| 355 |
pa->pages_offset = 0; |
| 356 |
pa->pages_dma = pages_dma; |
| 357 |
|
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
static void xlgmac_set_buffer_data(struct xlgmac_buffer_data *bd, |
| 362 |
struct xlgmac_page_alloc *pa, |
| 363 |
unsigned int len) |
| 364 |
{ |
| 365 |
get_page(pa->pages); |
| 366 |
bd->pa = *pa; |
| 367 |
|
| 368 |
bd->dma_base = pa->pages_dma; |
| 369 |
bd->dma_off = pa->pages_offset; |
| 370 |
bd->dma_len = len; |
| 371 |
|
| 372 |
pa->pages_offset += len; |
| 373 |
if ((pa->pages_offset + len) > pa->pages_len) { |
| 374 |
/* This data descriptor is responsible for unmapping page(s) */ |
| 375 |
bd->pa_unmap = *pa; |
| 376 |
|
| 377 |
/* Get a new allocation next time */ |
| 378 |
pa->pages = NULL; |
| 379 |
pa->pages_len = 0; |
| 380 |
pa->pages_offset = 0; |
| 381 |
pa->pages_dma = 0; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
static int xlgmac_map_rx_buffer(struct xlgmac_pdata *pdata, |
| 386 |
struct xlgmac_ring *ring, |
| 387 |
struct xlgmac_desc_data *desc_data) |
| 388 |
{ |
| 389 |
int order, ret; |
| 390 |
|
| 391 |
if (!ring->rx_hdr_pa.pages) { |
| 392 |
ret = xlgmac_alloc_pages(pdata, &ring->rx_hdr_pa, |
| 393 |
GFP_ATOMIC, 0); |
| 394 |
if (ret) |
| 395 |
return ret; |
| 396 |
} |
| 397 |
|
| 398 |
if (!ring->rx_buf_pa.pages) { |
| 399 |
order = max_t(int, PAGE_ALLOC_COSTLY_ORDER - 1, 0); |
| 400 |
ret = xlgmac_alloc_pages(pdata, &ring->rx_buf_pa, |
| 401 |
GFP_ATOMIC, order); |
| 402 |
if (ret) |
| 403 |
return ret; |
| 404 |
} |
| 405 |
|
| 406 |
/* Set up the header page info */ |
| 407 |
xlgmac_set_buffer_data(&desc_data->rx.hdr, &ring->rx_hdr_pa, |
| 408 |
XLGMAC_SKB_ALLOC_SIZE); |
| 409 |
|
| 410 |
/* Set up the buffer page info */ |
| 411 |
xlgmac_set_buffer_data(&desc_data->rx.buf, &ring->rx_buf_pa, |
| 412 |
pdata->rx_buf_size); |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static void xlgmac_tx_desc_init(struct xlgmac_pdata *pdata) |
| 418 |
{ |
| 419 |
struct xlgmac_hw_ops *hw_ops = &pdata->hw_ops; |
| 420 |
struct xlgmac_desc_data *desc_data; |
| 421 |
struct xlgmac_dma_desc *dma_desc; |
| 422 |
struct xlgmac_channel *channel; |
| 423 |
struct xlgmac_ring *ring; |
| 424 |
dma_addr_t dma_desc_addr; |
| 425 |
unsigned int i, j; |
| 426 |
|
| 427 |
channel = pdata->channel_head; |
| 428 |
for (i = 0; i < pdata->channel_count; i++, channel++) { |
| 429 |
ring = channel->tx_ring; |
| 430 |
if (!ring) |
| 431 |
break; |
| 432 |
|
| 433 |
dma_desc = ring->dma_desc_head; |
| 434 |
dma_desc_addr = ring->dma_desc_head_addr; |
| 435 |
|
| 436 |
for (j = 0; j < ring->dma_desc_count; j++) { |
| 437 |
desc_data = XLGMAC_GET_DESC_DATA(ring, j); |
| 438 |
|
| 439 |
desc_data->dma_desc = dma_desc; |
| 440 |
desc_data->dma_desc_addr = dma_desc_addr; |
| 441 |
|
| 442 |
dma_desc++; |
| 443 |
dma_desc_addr += sizeof(struct xlgmac_dma_desc); |
| 444 |
} |
| 445 |
|
| 446 |
ring->cur = 0; |
| 447 |
ring->dirty = 0; |
| 448 |
memset(&ring->tx, 0, sizeof(ring->tx)); |
| 449 |
|
| 450 |
hw_ops->tx_desc_init(channel); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
static void xlgmac_rx_desc_init(struct xlgmac_pdata *pdata) |
| 455 |
{ |
| 456 |
struct xlgmac_hw_ops *hw_ops = &pdata->hw_ops; |
| 457 |
struct xlgmac_desc_data *desc_data; |
| 458 |
struct xlgmac_dma_desc *dma_desc; |
| 459 |
struct xlgmac_channel *channel; |
| 460 |
struct xlgmac_ring *ring; |
| 461 |
dma_addr_t dma_desc_addr; |
| 462 |
unsigned int i, j; |
| 463 |
|
| 464 |
channel = pdata->channel_head; |
| 465 |
for (i = 0; i < pdata->channel_count; i++, channel++) { |
| 466 |
ring = channel->rx_ring; |
| 467 |
if (!ring) |
| 468 |
break; |
| 469 |
|
| 470 |
dma_desc = ring->dma_desc_head; |
| 471 |
dma_desc_addr = ring->dma_desc_head_addr; |
| 472 |
|
| 473 |
for (j = 0; j < ring->dma_desc_count; j++) { |
| 474 |
desc_data = XLGMAC_GET_DESC_DATA(ring, j); |
| 475 |
|
| 476 |
desc_data->dma_desc = dma_desc; |
| 477 |
desc_data->dma_desc_addr = dma_desc_addr; |
| 478 |
|
| 479 |
if (xlgmac_map_rx_buffer(pdata, ring, desc_data)) |
| 480 |
break; |
| 481 |
|
| 482 |
dma_desc++; |
| 483 |
dma_desc_addr += sizeof(struct xlgmac_dma_desc); |
| 484 |
} |
| 485 |
|
| 486 |
ring->cur = 0; |
| 487 |
ring->dirty = 0; |
| 488 |
|
| 489 |
hw_ops->rx_desc_init(channel); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
static int xlgmac_map_tx_skb(struct xlgmac_channel *channel, |
| 494 |
struct sk_buff *skb) |
| 495 |
{ |
| 496 |
struct xlgmac_pdata *pdata = channel->pdata; |
| 497 |
struct xlgmac_ring *ring = channel->tx_ring; |
| 498 |
unsigned int start_index, cur_index; |
| 499 |
struct xlgmac_desc_data *desc_data; |
| 500 |
unsigned int offset, datalen, len; |
| 501 |
struct xlgmac_pkt_info *pkt_info; |
| 502 |
skb_frag_t *frag; |
| 503 |
unsigned int tso, vlan; |
| 504 |
dma_addr_t skb_dma; |
| 505 |
unsigned int i; |
| 506 |
|
| 507 |
offset = 0; |
| 508 |
start_index = ring->cur; |
| 509 |
cur_index = ring->cur; |
| 510 |
|
| 511 |
pkt_info = &ring->pkt_info; |
| 512 |
pkt_info->desc_count = 0; |
| 513 |
pkt_info->length = 0; |
| 514 |
|
| 515 |
tso = XLGMAC_GET_REG_BITS(pkt_info->attributes, |
| 516 |
TX_PACKET_ATTRIBUTES_TSO_ENABLE_POS, |
| 517 |
TX_PACKET_ATTRIBUTES_TSO_ENABLE_LEN); |
| 518 |
vlan = XLGMAC_GET_REG_BITS(pkt_info->attributes, |
| 519 |
TX_PACKET_ATTRIBUTES_VLAN_CTAG_POS, |
| 520 |
TX_PACKET_ATTRIBUTES_VLAN_CTAG_LEN); |
| 521 |
|
| 522 |
/* Save space for a context descriptor if needed */ |
| 523 |
if ((tso && (pkt_info->mss != ring->tx.cur_mss)) || |
| 524 |
(vlan && (pkt_info->vlan_ctag != ring->tx.cur_vlan_ctag))) |
| 525 |
cur_index++; |
| 526 |
desc_data = XLGMAC_GET_DESC_DATA(ring, cur_index); |
| 527 |
|
| 528 |
if (tso) { |
| 529 |
/* Map the TSO header */ |
| 530 |
skb_dma = dma_map_single(pdata->dev, skb->data, |
| 531 |
pkt_info->header_len, DMA_TO_DEVICE); |
| 532 |
if (dma_mapping_error(pdata->dev, skb_dma)) { |
| 533 |
netdev_alert(pdata->netdev, "dma_map_single failed\n"); |
| 534 |
goto err_out; |
| 535 |
} |
| 536 |
desc_data->skb_dma = skb_dma; |
| 537 |
desc_data->skb_dma_len = pkt_info->header_len; |
| 538 |
netif_dbg(pdata, tx_queued, pdata->netdev, |
| 539 |
"skb header: index=%u, dma=%pad, len=%u\n", |
| 540 |
cur_index, &skb_dma, pkt_info->header_len); |
| 541 |
|
| 542 |
offset = pkt_info->header_len; |
| 543 |
|
| 544 |
pkt_info->length += pkt_info->header_len; |
| 545 |
|
| 546 |
cur_index++; |
| 547 |
desc_data = XLGMAC_GET_DESC_DATA(ring, cur_index); |
| 548 |
} |
| 549 |
|
| 550 |
/* Map the (remainder of the) packet */ |
| 551 |
for (datalen = skb_headlen(skb) - offset; datalen; ) { |
| 552 |
len = min_t(unsigned int, datalen, XLGMAC_TX_MAX_BUF_SIZE); |
| 553 |
|
| 554 |
skb_dma = dma_map_single(pdata->dev, skb->data + offset, len, |
| 555 |
DMA_TO_DEVICE); |
| 556 |
if (dma_mapping_error(pdata->dev, skb_dma)) { |
| 557 |
netdev_alert(pdata->netdev, "dma_map_single failed\n"); |
| 558 |
goto err_out; |
| 559 |
} |
| 560 |
desc_data->skb_dma = skb_dma; |
| 561 |
desc_data->skb_dma_len = len; |
| 562 |
netif_dbg(pdata, tx_queued, pdata->netdev, |
| 563 |
"skb data: index=%u, dma=%pad, len=%u\n", |
| 564 |
cur_index, &skb_dma, len); |
| 565 |
|
| 566 |
datalen -= len; |
| 567 |
offset += len; |
| 568 |
|
| 569 |
pkt_info->length += len; |
| 570 |
|
| 571 |
cur_index++; |
| 572 |
desc_data = XLGMAC_GET_DESC_DATA(ring, cur_index); |
| 573 |
} |
| 574 |
|
| 575 |
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 576 |
netif_dbg(pdata, tx_queued, pdata->netdev, |
| 577 |
"mapping frag %u\n", i); |
| 578 |
|
| 579 |
frag = &skb_shinfo(skb)->frags[i]; |
| 580 |
offset = 0; |
| 581 |
|
| 582 |
for (datalen = skb_frag_size(frag); datalen; ) { |
| 583 |
len = min_t(unsigned int, datalen, |
| 584 |
XLGMAC_TX_MAX_BUF_SIZE); |
| 585 |
|
| 586 |
skb_dma = skb_frag_dma_map(pdata->dev, frag, offset, |
| 587 |
len, DMA_TO_DEVICE); |
| 588 |
if (dma_mapping_error(pdata->dev, skb_dma)) { |
| 589 |
netdev_alert(pdata->netdev, |
| 590 |
"skb_frag_dma_map failed\n"); |
| 591 |
goto err_out; |
| 592 |
} |
| 593 |
desc_data->skb_dma = skb_dma; |
| 594 |
desc_data->skb_dma_len = len; |
| 595 |
desc_data->mapped_as_page = 1; |
| 596 |
netif_dbg(pdata, tx_queued, pdata->netdev, |
| 597 |
"skb frag: index=%u, dma=%pad, len=%u\n", |
| 598 |
cur_index, &skb_dma, len); |
| 599 |
|
| 600 |
datalen -= len; |
| 601 |
offset += len; |
| 602 |
|
| 603 |
pkt_info->length += len; |
| 604 |
|
| 605 |
cur_index++; |
| 606 |
desc_data = XLGMAC_GET_DESC_DATA(ring, cur_index); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/* Save the skb address in the last entry. We always have some data |
| 611 |
* that has been mapped so desc_data is always advanced past the last |
| 612 |
* piece of mapped data - use the entry pointed to by cur_index - 1. |
| 613 |
*/ |
| 614 |
desc_data = XLGMAC_GET_DESC_DATA(ring, cur_index - 1); |
| 615 |
desc_data->skb = skb; |
| 616 |
|
| 617 |
/* Save the number of descriptor entries used */ |
| 618 |
pkt_info->desc_count = cur_index - start_index; |
| 619 |
|
| 620 |
return pkt_info->desc_count; |
| 621 |
|
| 622 |
err_out: |
| 623 |
while (start_index < cur_index) { |
| 624 |
desc_data = XLGMAC_GET_DESC_DATA(ring, start_index++); |
| 625 |
xlgmac_unmap_desc_data(pdata, desc_data); |
| 626 |
} |
| 627 |
|
| 628 |
return 0; |
| 629 |
} |
| 630 |
|
| 631 |
void xlgmac_init_desc_ops(struct xlgmac_desc_ops *desc_ops) |
| 632 |
{ |
| 633 |
desc_ops->alloc_channels_and_rings = xlgmac_alloc_channels_and_rings; |
| 634 |
desc_ops->free_channels_and_rings = xlgmac_free_channels_and_rings; |
| 635 |
desc_ops->map_tx_skb = xlgmac_map_tx_skb; |
| 636 |
desc_ops->map_rx_buffer = xlgmac_map_rx_buffer; |
| 637 |
desc_ops->unmap_desc_data = xlgmac_unmap_desc_data; |
| 638 |
desc_ops->tx_desc_init = xlgmac_tx_desc_init; |
| 639 |
desc_ops->rx_desc_init = xlgmac_rx_desc_init; |
| 640 |
} |