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