1 /* 2 * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet 3 * driver for Linux. 4 * 5 * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved. 6 * 7 * This software is available to you under a choice of one of two 8 * licenses. You may choose to be licensed under the terms of the GNU 9 * General Public License (GPL) Version 2, available from the file 10 * COPYING in the main directory of this source tree, or the 11 * OpenIB.org BSD license below: 12 * 13 * Redistribution and use in source and binary forms, with or 14 * without modification, are permitted provided that the following 15 * conditions are met: 16 * 17 * - Redistributions of source code must retain the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer. 20 * 21 * - Redistributions in binary form must reproduce the above 22 * copyright notice, this list of conditions and the following 23 * disclaimer in the documentation and/or other materials 24 * provided with the distribution. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 33 * SOFTWARE. 34 */ 35 36 #include <linux/skbuff.h> 37 #include <linux/netdevice.h> 38 #include <linux/etherdevice.h> 39 #include <linux/if_vlan.h> 40 #include <linux/ip.h> 41 #include <net/ipv6.h> 42 #include <net/tcp.h> 43 #include <linux/dma-mapping.h> 44 #include <linux/prefetch.h> 45 46 #include "t4vf_common.h" 47 #include "t4vf_defs.h" 48 49 #include "../cxgb4/t4_regs.h" 50 #include "../cxgb4/t4_values.h" 51 #include "../cxgb4/t4fw_api.h" 52 #include "../cxgb4/t4_msg.h" 53 54 /* 55 * Constants ... 56 */ 57 enum { 58 /* 59 * Egress Queue sizes, producer and consumer indices are all in units 60 * of Egress Context Units bytes. Note that as far as the hardware is 61 * concerned, the free list is an Egress Queue (the host produces free 62 * buffers which the hardware consumes) and free list entries are 63 * 64-bit PCI DMA addresses. 64 */ 65 EQ_UNIT = SGE_EQ_IDXSIZE, 66 FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64), 67 TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64), 68 69 /* 70 * Max number of TX descriptors we clean up at a time. Should be 71 * modest as freeing skbs isn't cheap and it happens while holding 72 * locks. We just need to free packets faster than they arrive, we 73 * eventually catch up and keep the amortized cost reasonable. 74 */ 75 MAX_TX_RECLAIM = 16, 76 77 /* 78 * Max number of Rx buffers we replenish at a time. Again keep this 79 * modest, allocating buffers isn't cheap either. 80 */ 81 MAX_RX_REFILL = 16, 82 83 /* 84 * Period of the Rx queue check timer. This timer is infrequent as it 85 * has something to do only when the system experiences severe memory 86 * shortage. 87 */ 88 RX_QCHECK_PERIOD = (HZ / 2), 89 90 /* 91 * Period of the TX queue check timer and the maximum number of TX 92 * descriptors to be reclaimed by the TX timer. 93 */ 94 TX_QCHECK_PERIOD = (HZ / 2), 95 MAX_TIMER_TX_RECLAIM = 100, 96 97 /* 98 * Suspend an Ethernet TX queue with fewer available descriptors than 99 * this. We always want to have room for a maximum sized packet: 100 * inline immediate data + MAX_SKB_FRAGS. This is the same as 101 * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS 102 * (see that function and its helpers for a description of the 103 * calculation). 104 */ 105 ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1, 106 ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 + 107 ((ETHTXQ_MAX_FRAGS-1) & 1) + 108 2), 109 ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) + 110 sizeof(struct cpl_tx_pkt_lso_core) + 111 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64), 112 ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR, 113 114 ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT), 115 116 /* 117 * Max TX descriptor space we allow for an Ethernet packet to be 118 * inlined into a WR. This is limited by the maximum value which 119 * we can specify for immediate data in the firmware Ethernet TX 120 * Work Request. 121 */ 122 MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_M, 123 124 /* 125 * Max size of a WR sent through a control TX queue. 126 */ 127 MAX_CTRL_WR_LEN = 256, 128 129 /* 130 * Maximum amount of data which we'll ever need to inline into a 131 * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN). 132 */ 133 MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN 134 ? MAX_IMM_TX_PKT_LEN 135 : MAX_CTRL_WR_LEN), 136 137 /* 138 * For incoming packets less than RX_COPY_THRES, we copy the data into 139 * an skb rather than referencing the data. We allocate enough 140 * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes 141 * of the data (header). 142 */ 143 RX_COPY_THRES = 256, 144 RX_PULL_LEN = 128, 145 146 /* 147 * Main body length for sk_buffs used for RX Ethernet packets with 148 * fragments. Should be >= RX_PULL_LEN but possibly bigger to give 149 * pskb_may_pull() some room. 150 */ 151 RX_SKB_LEN = 512, 152 }; 153 154 /* 155 * Software state per TX descriptor. 156 */ 157 struct tx_sw_desc { 158 struct sk_buff *skb; /* socket buffer of TX data source */ 159 struct ulptx_sgl *sgl; /* scatter/gather list in TX Queue */ 160 }; 161 162 /* 163 * Software state per RX Free List descriptor. We keep track of the allocated 164 * FL page, its size, and its PCI DMA address (if the page is mapped). The FL 165 * page size and its PCI DMA mapped state are stored in the low bits of the 166 * PCI DMA address as per below. 167 */ 168 struct rx_sw_desc { 169 struct page *page; /* Free List page buffer */ 170 dma_addr_t dma_addr; /* PCI DMA address (if mapped) */ 171 /* and flags (see below) */ 172 }; 173 174 /* 175 * The low bits of rx_sw_desc.dma_addr have special meaning. Note that the 176 * SGE also uses the low 4 bits to determine the size of the buffer. It uses 177 * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array. 178 * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4 179 * bits can only contain a 0 or a 1 to indicate which size buffer we're giving 180 * to the SGE. Thus, our software state of "is the buffer mapped for DMA" is 181 * maintained in an inverse sense so the hardware never sees that bit high. 182 */ 183 enum { 184 RX_LARGE_BUF = 1 << 0, /* buffer is SGE_FL_BUFFER_SIZE[1] */ 185 RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */ 186 }; 187 188 /** 189 * get_buf_addr - return DMA buffer address of software descriptor 190 * @sdesc: pointer to the software buffer descriptor 191 * 192 * Return the DMA buffer address of a software descriptor (stripping out 193 * our low-order flag bits). 194 */ 195 static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc) 196 { 197 return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF); 198 } 199 200 /** 201 * is_buf_mapped - is buffer mapped for DMA? 202 * @sdesc: pointer to the software buffer descriptor 203 * 204 * Determine whether the buffer associated with a software descriptor in 205 * mapped for DMA or not. 206 */ 207 static inline bool is_buf_mapped(const struct rx_sw_desc *sdesc) 208 { 209 return !(sdesc->dma_addr & RX_UNMAPPED_BUF); 210 } 211 212 /** 213 * need_skb_unmap - does the platform need unmapping of sk_buffs? 214 * 215 * Returns true if the platform needs sk_buff unmapping. The compiler 216 * optimizes away unnecessary code if this returns true. 217 */ 218 static inline int need_skb_unmap(void) 219 { 220 #ifdef CONFIG_NEED_DMA_MAP_STATE 221 return 1; 222 #else 223 return 0; 224 #endif 225 } 226 227 /** 228 * txq_avail - return the number of available slots in a TX queue 229 * @tq: the TX queue 230 * 231 * Returns the number of available descriptors in a TX queue. 232 */ 233 static inline unsigned int txq_avail(const struct sge_txq *tq) 234 { 235 return tq->size - 1 - tq->in_use; 236 } 237 238 /** 239 * fl_cap - return the capacity of a Free List 240 * @fl: the Free List 241 * 242 * Returns the capacity of a Free List. The capacity is less than the 243 * size because an Egress Queue Index Unit worth of descriptors needs to 244 * be left unpopulated, otherwise the Producer and Consumer indices PIDX 245 * and CIDX will match and the hardware will think the FL is empty. 246 */ 247 static inline unsigned int fl_cap(const struct sge_fl *fl) 248 { 249 return fl->size - FL_PER_EQ_UNIT; 250 } 251 252 /** 253 * fl_starving - return whether a Free List is starving. 254 * @adapter: pointer to the adapter 255 * @fl: the Free List 256 * 257 * Tests specified Free List to see whether the number of buffers 258 * available to the hardware has falled below our "starvation" 259 * threshold. 260 */ 261 static inline bool fl_starving(const struct adapter *adapter, 262 const struct sge_fl *fl) 263 { 264 const struct sge *s = &adapter->sge; 265 266 return fl->avail - fl->pend_cred <= s->fl_starve_thres; 267 } 268 269 /** 270 * map_skb - map an skb for DMA to the device 271 * @dev: the egress net device 272 * @skb: the packet to map 273 * @addr: a pointer to the base of the DMA mapping array 274 * 275 * Map an skb for DMA to the device and return an array of DMA addresses. 276 */ 277 static int map_skb(struct device *dev, const struct sk_buff *skb, 278 dma_addr_t *addr) 279 { 280 const skb_frag_t *fp, *end; 281 const struct skb_shared_info *si; 282 283 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE); 284 if (dma_mapping_error(dev, *addr)) 285 goto out_err; 286 287 si = skb_shinfo(skb); 288 end = &si->frags[si->nr_frags]; 289 for (fp = si->frags; fp < end; fp++) { 290 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp), 291 DMA_TO_DEVICE); 292 if (dma_mapping_error(dev, *addr)) 293 goto unwind; 294 } 295 return 0; 296 297 unwind: 298 while (fp-- > si->frags) 299 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE); 300 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE); 301 302 out_err: 303 return -ENOMEM; 304 } 305 306 static void unmap_sgl(struct device *dev, const struct sk_buff *skb, 307 const struct ulptx_sgl *sgl, const struct sge_txq *tq) 308 { 309 const struct ulptx_sge_pair *p; 310 unsigned int nfrags = skb_shinfo(skb)->nr_frags; 311 312 if (likely(skb_headlen(skb))) 313 dma_unmap_single(dev, be64_to_cpu(sgl->addr0), 314 be32_to_cpu(sgl->len0), DMA_TO_DEVICE); 315 else { 316 dma_unmap_page(dev, be64_to_cpu(sgl->addr0), 317 be32_to_cpu(sgl->len0), DMA_TO_DEVICE); 318 nfrags--; 319 } 320 321 /* 322 * the complexity below is because of the possibility of a wrap-around 323 * in the middle of an SGL 324 */ 325 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) { 326 if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) { 327 unmap: 328 dma_unmap_page(dev, be64_to_cpu(p->addr[0]), 329 be32_to_cpu(p->len[0]), DMA_TO_DEVICE); 330 dma_unmap_page(dev, be64_to_cpu(p->addr[1]), 331 be32_to_cpu(p->len[1]), DMA_TO_DEVICE); 332 p++; 333 } else if ((u8 *)p == (u8 *)tq->stat) { 334 p = (const struct ulptx_sge_pair *)tq->desc; 335 goto unmap; 336 } else if ((u8 *)p + 8 == (u8 *)tq->stat) { 337 const __be64 *addr = (const __be64 *)tq->desc; 338 339 dma_unmap_page(dev, be64_to_cpu(addr[0]), 340 be32_to_cpu(p->len[0]), DMA_TO_DEVICE); 341 dma_unmap_page(dev, be64_to_cpu(addr[1]), 342 be32_to_cpu(p->len[1]), DMA_TO_DEVICE); 343 p = (const struct ulptx_sge_pair *)&addr[2]; 344 } else { 345 const __be64 *addr = (const __be64 *)tq->desc; 346 347 dma_unmap_page(dev, be64_to_cpu(p->addr[0]), 348 be32_to_cpu(p->len[0]), DMA_TO_DEVICE); 349 dma_unmap_page(dev, be64_to_cpu(addr[0]), 350 be32_to_cpu(p->len[1]), DMA_TO_DEVICE); 351 p = (const struct ulptx_sge_pair *)&addr[1]; 352 } 353 } 354 if (nfrags) { 355 __be64 addr; 356 357 if ((u8 *)p == (u8 *)tq->stat) 358 p = (const struct ulptx_sge_pair *)tq->desc; 359 addr = ((u8 *)p + 16 <= (u8 *)tq->stat 360 ? p->addr[0] 361 : *(const __be64 *)tq->desc); 362 dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]), 363 DMA_TO_DEVICE); 364 } 365 } 366 367 /** 368 * free_tx_desc - reclaims TX descriptors and their buffers 369 * @adapter: the adapter 370 * @tq: the TX queue to reclaim descriptors from 371 * @n: the number of descriptors to reclaim 372 * @unmap: whether the buffers should be unmapped for DMA 373 * 374 * Reclaims TX descriptors from an SGE TX queue and frees the associated 375 * TX buffers. Called with the TX queue lock held. 376 */ 377 static void free_tx_desc(struct adapter *adapter, struct sge_txq *tq, 378 unsigned int n, bool unmap) 379 { 380 struct tx_sw_desc *sdesc; 381 unsigned int cidx = tq->cidx; 382 struct device *dev = adapter->pdev_dev; 383 384 const int need_unmap = need_skb_unmap() && unmap; 385 386 sdesc = &tq->sdesc[cidx]; 387 while (n--) { 388 /* 389 * If we kept a reference to the original TX skb, we need to 390 * unmap it from PCI DMA space (if required) and free it. 391 */ 392 if (sdesc->skb) { 393 if (need_unmap) 394 unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq); 395 dev_consume_skb_any(sdesc->skb); 396 sdesc->skb = NULL; 397 } 398 399 sdesc++; 400 if (++cidx == tq->size) { 401 cidx = 0; 402 sdesc = tq->sdesc; 403 } 404 } 405 tq->cidx = cidx; 406 } 407 408 /* 409 * Return the number of reclaimable descriptors in a TX queue. 410 */ 411 static inline int reclaimable(const struct sge_txq *tq) 412 { 413 int hw_cidx = be16_to_cpu(tq->stat->cidx); 414 int reclaimable = hw_cidx - tq->cidx; 415 if (reclaimable < 0) 416 reclaimable += tq->size; 417 return reclaimable; 418 } 419 420 /** 421 * reclaim_completed_tx - reclaims completed TX descriptors 422 * @adapter: the adapter 423 * @tq: the TX queue to reclaim completed descriptors from 424 * @unmap: whether the buffers should be unmapped for DMA 425 * 426 * Reclaims TX descriptors that the SGE has indicated it has processed, 427 * and frees the associated buffers if possible. Called with the TX 428 * queue locked. 429 */ 430 static inline void reclaim_completed_tx(struct adapter *adapter, 431 struct sge_txq *tq, 432 bool unmap) 433 { 434 int avail = reclaimable(tq); 435 436 if (avail) { 437 /* 438 * Limit the amount of clean up work we do at a time to keep 439 * the TX lock hold time O(1). 440 */ 441 if (avail > MAX_TX_RECLAIM) 442 avail = MAX_TX_RECLAIM; 443 444 free_tx_desc(adapter, tq, avail, unmap); 445 tq->in_use -= avail; 446 } 447 } 448 449 /** 450 * get_buf_size - return the size of an RX Free List buffer. 451 * @adapter: pointer to the associated adapter 452 * @sdesc: pointer to the software buffer descriptor 453 */ 454 static inline int get_buf_size(const struct adapter *adapter, 455 const struct rx_sw_desc *sdesc) 456 { 457 const struct sge *s = &adapter->sge; 458 459 return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF) 460 ? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE); 461 } 462 463 /** 464 * free_rx_bufs - free RX buffers on an SGE Free List 465 * @adapter: the adapter 466 * @fl: the SGE Free List to free buffers from 467 * @n: how many buffers to free 468 * 469 * Release the next @n buffers on an SGE Free List RX queue. The 470 * buffers must be made inaccessible to hardware before calling this 471 * function. 472 */ 473 static void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n) 474 { 475 while (n--) { 476 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx]; 477 478 if (is_buf_mapped(sdesc)) 479 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc), 480 get_buf_size(adapter, sdesc), 481 PCI_DMA_FROMDEVICE); 482 put_page(sdesc->page); 483 sdesc->page = NULL; 484 if (++fl->cidx == fl->size) 485 fl->cidx = 0; 486 fl->avail--; 487 } 488 } 489 490 /** 491 * unmap_rx_buf - unmap the current RX buffer on an SGE Free List 492 * @adapter: the adapter 493 * @fl: the SGE Free List 494 * 495 * Unmap the current buffer on an SGE Free List RX queue. The 496 * buffer must be made inaccessible to HW before calling this function. 497 * 498 * This is similar to @free_rx_bufs above but does not free the buffer. 499 * Do note that the FL still loses any further access to the buffer. 500 * This is used predominantly to "transfer ownership" of an FL buffer 501 * to another entity (typically an skb's fragment list). 502 */ 503 static void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl) 504 { 505 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx]; 506 507 if (is_buf_mapped(sdesc)) 508 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc), 509 get_buf_size(adapter, sdesc), 510 PCI_DMA_FROMDEVICE); 511 sdesc->page = NULL; 512 if (++fl->cidx == fl->size) 513 fl->cidx = 0; 514 fl->avail--; 515 } 516 517 /** 518 * ring_fl_db - righ doorbell on free list 519 * @adapter: the adapter 520 * @fl: the Free List whose doorbell should be rung ... 521 * 522 * Tell the Scatter Gather Engine that there are new free list entries 523 * available. 524 */ 525 static inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl) 526 { 527 u32 val = adapter->params.arch.sge_fl_db; 528 529 /* The SGE keeps track of its Producer and Consumer Indices in terms 530 * of Egress Queue Units so we can only tell it about integral numbers 531 * of multiples of Free List Entries per Egress Queue Units ... 532 */ 533 if (fl->pend_cred >= FL_PER_EQ_UNIT) { 534 if (is_t4(adapter->params.chip)) 535 val |= PIDX_V(fl->pend_cred / FL_PER_EQ_UNIT); 536 else 537 val |= PIDX_T5_V(fl->pend_cred / FL_PER_EQ_UNIT); 538 539 /* Make sure all memory writes to the Free List queue are 540 * committed before we tell the hardware about them. 541 */ 542 wmb(); 543 544 /* If we don't have access to the new User Doorbell (T5+), use 545 * the old doorbell mechanism; otherwise use the new BAR2 546 * mechanism. 547 */ 548 if (unlikely(fl->bar2_addr == NULL)) { 549 t4_write_reg(adapter, 550 T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL, 551 QID_V(fl->cntxt_id) | val); 552 } else { 553 writel(val | QID_V(fl->bar2_qid), 554 fl->bar2_addr + SGE_UDB_KDOORBELL); 555 556 /* This Write memory Barrier will force the write to 557 * the User Doorbell area to be flushed. 558 */ 559 wmb(); 560 } 561 fl->pend_cred %= FL_PER_EQ_UNIT; 562 } 563 } 564 565 /** 566 * set_rx_sw_desc - initialize software RX buffer descriptor 567 * @sdesc: pointer to the softwore RX buffer descriptor 568 * @page: pointer to the page data structure backing the RX buffer 569 * @dma_addr: PCI DMA address (possibly with low-bit flags) 570 */ 571 static inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page, 572 dma_addr_t dma_addr) 573 { 574 sdesc->page = page; 575 sdesc->dma_addr = dma_addr; 576 } 577 578 /* 579 * Support for poisoning RX buffers ... 580 */ 581 #define POISON_BUF_VAL -1 582 583 static inline void poison_buf(struct page *page, size_t sz) 584 { 585 #if POISON_BUF_VAL >= 0 586 memset(page_address(page), POISON_BUF_VAL, sz); 587 #endif 588 } 589 590 /** 591 * refill_fl - refill an SGE RX buffer ring 592 * @adapter: the adapter 593 * @fl: the Free List ring to refill 594 * @n: the number of new buffers to allocate 595 * @gfp: the gfp flags for the allocations 596 * 597 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers, 598 * allocated with the supplied gfp flags. The caller must assure that 599 * @n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN 600 * EGRESS QUEUE UNITS_ indicates an empty Free List! Returns the number 601 * of buffers allocated. If afterwards the queue is found critically low, 602 * mark it as starving in the bitmap of starving FLs. 603 */ 604 static unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl, 605 int n, gfp_t gfp) 606 { 607 struct sge *s = &adapter->sge; 608 struct page *page; 609 dma_addr_t dma_addr; 610 unsigned int cred = fl->avail; 611 __be64 *d = &fl->desc[fl->pidx]; 612 struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx]; 613 614 /* 615 * Sanity: ensure that the result of adding n Free List buffers 616 * won't result in wrapping the SGE's Producer Index around to 617 * it's Consumer Index thereby indicating an empty Free List ... 618 */ 619 BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT); 620 621 gfp |= __GFP_NOWARN; 622 623 /* 624 * If we support large pages, prefer large buffers and fail over to 625 * small pages if we can't allocate large pages to satisfy the refill. 626 * If we don't support large pages, drop directly into the small page 627 * allocation code. 628 */ 629 if (s->fl_pg_order == 0) 630 goto alloc_small_pages; 631 632 while (n) { 633 page = __dev_alloc_pages(gfp, s->fl_pg_order); 634 if (unlikely(!page)) { 635 /* 636 * We've failed inour attempt to allocate a "large 637 * page". Fail over to the "small page" allocation 638 * below. 639 */ 640 fl->large_alloc_failed++; 641 break; 642 } 643 poison_buf(page, PAGE_SIZE << s->fl_pg_order); 644 645 dma_addr = dma_map_page(adapter->pdev_dev, page, 0, 646 PAGE_SIZE << s->fl_pg_order, 647 PCI_DMA_FROMDEVICE); 648 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) { 649 /* 650 * We've run out of DMA mapping space. Free up the 651 * buffer and return with what we've managed to put 652 * into the free list. We don't want to fail over to 653 * the small page allocation below in this case 654 * because DMA mapping resources are typically 655 * critical resources once they become scarse. 656 */ 657 __free_pages(page, s->fl_pg_order); 658 goto out; 659 } 660 dma_addr |= RX_LARGE_BUF; 661 *d++ = cpu_to_be64(dma_addr); 662 663 set_rx_sw_desc(sdesc, page, dma_addr); 664 sdesc++; 665 666 fl->avail++; 667 if (++fl->pidx == fl->size) { 668 fl->pidx = 0; 669 sdesc = fl->sdesc; 670 d = fl->desc; 671 } 672 n--; 673 } 674 675 alloc_small_pages: 676 while (n--) { 677 page = __dev_alloc_page(gfp); 678 if (unlikely(!page)) { 679 fl->alloc_failed++; 680 break; 681 } 682 poison_buf(page, PAGE_SIZE); 683 684 dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE, 685 PCI_DMA_FROMDEVICE); 686 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) { 687 put_page(page); 688 break; 689 } 690 *d++ = cpu_to_be64(dma_addr); 691 692 set_rx_sw_desc(sdesc, page, dma_addr); 693 sdesc++; 694 695 fl->avail++; 696 if (++fl->pidx == fl->size) { 697 fl->pidx = 0; 698 sdesc = fl->sdesc; 699 d = fl->desc; 700 } 701 } 702 703 out: 704 /* 705 * Update our accounting state to incorporate the new Free List 706 * buffers, tell the hardware about them and return the number of 707 * buffers which we were able to allocate. 708 */ 709 cred = fl->avail - cred; 710 fl->pend_cred += cred; 711 ring_fl_db(adapter, fl); 712 713 if (unlikely(fl_starving(adapter, fl))) { 714 smp_wmb(); 715 set_bit(fl->cntxt_id, adapter->sge.starving_fl); 716 } 717 718 return cred; 719 } 720 721 /* 722 * Refill a Free List to its capacity or the Maximum Refill Increment, 723 * whichever is smaller ... 724 */ 725 static inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl) 726 { 727 refill_fl(adapter, fl, 728 min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail), 729 GFP_ATOMIC); 730 } 731 732 /** 733 * alloc_ring - allocate resources for an SGE descriptor ring 734 * @dev: the PCI device's core device 735 * @nelem: the number of descriptors 736 * @hwsize: the size of each hardware descriptor 737 * @swsize: the size of each software descriptor 738 * @busaddrp: the physical PCI bus address of the allocated ring 739 * @swringp: return address pointer for software ring 740 * @stat_size: extra space in hardware ring for status information 741 * 742 * Allocates resources for an SGE descriptor ring, such as TX queues, 743 * free buffer lists, response queues, etc. Each SGE ring requires 744 * space for its hardware descriptors plus, optionally, space for software 745 * state associated with each hardware entry (the metadata). The function 746 * returns three values: the virtual address for the hardware ring (the 747 * return value of the function), the PCI bus address of the hardware 748 * ring (in *busaddrp), and the address of the software ring (in swringp). 749 * Both the hardware and software rings are returned zeroed out. 750 */ 751 static void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize, 752 size_t swsize, dma_addr_t *busaddrp, void *swringp, 753 size_t stat_size) 754 { 755 /* 756 * Allocate the hardware ring and PCI DMA bus address space for said. 757 */ 758 size_t hwlen = nelem * hwsize + stat_size; 759 void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL); 760 761 if (!hwring) 762 return NULL; 763 764 /* 765 * If the caller wants a software ring, allocate it and return a 766 * pointer to it in *swringp. 767 */ 768 BUG_ON((swsize != 0) != (swringp != NULL)); 769 if (swsize) { 770 void *swring = kcalloc(nelem, swsize, GFP_KERNEL); 771 772 if (!swring) { 773 dma_free_coherent(dev, hwlen, hwring, *busaddrp); 774 return NULL; 775 } 776 *(void **)swringp = swring; 777 } 778 779 /* 780 * Zero out the hardware ring and return its address as our function 781 * value. 782 */ 783 memset(hwring, 0, hwlen); 784 return hwring; 785 } 786 787 /** 788 * sgl_len - calculates the size of an SGL of the given capacity 789 * @n: the number of SGL entries 790 * 791 * Calculates the number of flits (8-byte units) needed for a Direct 792 * Scatter/Gather List that can hold the given number of entries. 793 */ 794 static inline unsigned int sgl_len(unsigned int n) 795 { 796 /* 797 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA 798 * addresses. The DSGL Work Request starts off with a 32-bit DSGL 799 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N, 800 * repeated sequences of { Length[i], Length[i+1], Address[i], 801 * Address[i+1] } (this ensures that all addresses are on 64-bit 802 * boundaries). If N is even, then Length[N+1] should be set to 0 and 803 * Address[N+1] is omitted. 804 * 805 * The following calculation incorporates all of the above. It's 806 * somewhat hard to follow but, briefly: the "+2" accounts for the 807 * first two flits which include the DSGL header, Length0 and 808 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3 809 * flits for every pair of the remaining N) +1 if (n-1) is odd; and 810 * finally the "+((n-1)&1)" adds the one remaining flit needed if 811 * (n-1) is odd ... 812 */ 813 n--; 814 return (3 * n) / 2 + (n & 1) + 2; 815 } 816 817 /** 818 * flits_to_desc - returns the num of TX descriptors for the given flits 819 * @flits: the number of flits 820 * 821 * Returns the number of TX descriptors needed for the supplied number 822 * of flits. 823 */ 824 static inline unsigned int flits_to_desc(unsigned int flits) 825 { 826 BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64)); 827 return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT); 828 } 829 830 /** 831 * is_eth_imm - can an Ethernet packet be sent as immediate data? 832 * @skb: the packet 833 * 834 * Returns whether an Ethernet packet is small enough to fit completely as 835 * immediate data. 836 */ 837 static inline int is_eth_imm(const struct sk_buff *skb) 838 { 839 /* 840 * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request 841 * which does not accommodate immediate data. We could dike out all 842 * of the support code for immediate data but that would tie our hands 843 * too much if we ever want to enhace the firmware. It would also 844 * create more differences between the PF and VF Drivers. 845 */ 846 return false; 847 } 848 849 /** 850 * calc_tx_flits - calculate the number of flits for a packet TX WR 851 * @skb: the packet 852 * 853 * Returns the number of flits needed for a TX Work Request for the 854 * given Ethernet packet, including the needed WR and CPL headers. 855 */ 856 static inline unsigned int calc_tx_flits(const struct sk_buff *skb) 857 { 858 unsigned int flits; 859 860 /* 861 * If the skb is small enough, we can pump it out as a work request 862 * with only immediate data. In that case we just have to have the 863 * TX Packet header plus the skb data in the Work Request. 864 */ 865 if (is_eth_imm(skb)) 866 return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 867 sizeof(__be64)); 868 869 /* 870 * Otherwise, we're going to have to construct a Scatter gather list 871 * of the skb body and fragments. We also include the flits necessary 872 * for the TX Packet Work Request and CPL. We always have a firmware 873 * Write Header (incorporated as part of the cpl_tx_pkt_lso and 874 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL 875 * message or, if we're doing a Large Send Offload, an LSO CPL message 876 * with an embedded TX Packet Write CPL message. 877 */ 878 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1); 879 if (skb_shinfo(skb)->gso_size) 880 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) + 881 sizeof(struct cpl_tx_pkt_lso_core) + 882 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64); 883 else 884 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) + 885 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64); 886 return flits; 887 } 888 889 /** 890 * write_sgl - populate a Scatter/Gather List for a packet 891 * @skb: the packet 892 * @tq: the TX queue we are writing into 893 * @sgl: starting location for writing the SGL 894 * @end: points right after the end of the SGL 895 * @start: start offset into skb main-body data to include in the SGL 896 * @addr: the list of DMA bus addresses for the SGL elements 897 * 898 * Generates a Scatter/Gather List for the buffers that make up a packet. 899 * The caller must provide adequate space for the SGL that will be written. 900 * The SGL includes all of the packet's page fragments and the data in its 901 * main body except for the first @start bytes. @pos must be 16-byte 902 * aligned and within a TX descriptor with available space. @end points 903 * write after the end of the SGL but does not account for any potential 904 * wrap around, i.e., @end > @tq->stat. 905 */ 906 static void write_sgl(const struct sk_buff *skb, struct sge_txq *tq, 907 struct ulptx_sgl *sgl, u64 *end, unsigned int start, 908 const dma_addr_t *addr) 909 { 910 unsigned int i, len; 911 struct ulptx_sge_pair *to; 912 const struct skb_shared_info *si = skb_shinfo(skb); 913 unsigned int nfrags = si->nr_frags; 914 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1]; 915 916 len = skb_headlen(skb) - start; 917 if (likely(len)) { 918 sgl->len0 = htonl(len); 919 sgl->addr0 = cpu_to_be64(addr[0] + start); 920 nfrags++; 921 } else { 922 sgl->len0 = htonl(skb_frag_size(&si->frags[0])); 923 sgl->addr0 = cpu_to_be64(addr[1]); 924 } 925 926 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) | 927 ULPTX_NSGE_V(nfrags)); 928 if (likely(--nfrags == 0)) 929 return; 930 /* 931 * Most of the complexity below deals with the possibility we hit the 932 * end of the queue in the middle of writing the SGL. For this case 933 * only we create the SGL in a temporary buffer and then copy it. 934 */ 935 to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge; 936 937 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) { 938 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i])); 939 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i])); 940 to->addr[0] = cpu_to_be64(addr[i]); 941 to->addr[1] = cpu_to_be64(addr[++i]); 942 } 943 if (nfrags) { 944 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i])); 945 to->len[1] = cpu_to_be32(0); 946 to->addr[0] = cpu_to_be64(addr[i + 1]); 947 } 948 if (unlikely((u8 *)end > (u8 *)tq->stat)) { 949 unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1; 950 951 if (likely(part0)) 952 memcpy(sgl->sge, buf, part0); 953 part1 = (u8 *)end - (u8 *)tq->stat; 954 memcpy(tq->desc, (u8 *)buf + part0, part1); 955 end = (void *)tq->desc + part1; 956 } 957 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */ 958 *end = 0; 959 } 960 961 /** 962 * check_ring_tx_db - check and potentially ring a TX queue's doorbell 963 * @adapter: the adapter 964 * @tq: the TX queue 965 * @n: number of new descriptors to give to HW 966 * 967 * Ring the doorbel for a TX queue. 968 */ 969 static inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq, 970 int n) 971 { 972 /* Make sure that all writes to the TX Descriptors are committed 973 * before we tell the hardware about them. 974 */ 975 wmb(); 976 977 /* If we don't have access to the new User Doorbell (T5+), use the old 978 * doorbell mechanism; otherwise use the new BAR2 mechanism. 979 */ 980 if (unlikely(tq->bar2_addr == NULL)) { 981 u32 val = PIDX_V(n); 982 983 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL, 984 QID_V(tq->cntxt_id) | val); 985 } else { 986 u32 val = PIDX_T5_V(n); 987 988 /* T4 and later chips share the same PIDX field offset within 989 * the doorbell, but T5 and later shrank the field in order to 990 * gain a bit for Doorbell Priority. The field was absurdly 991 * large in the first place (14 bits) so we just use the T5 992 * and later limits and warn if a Queue ID is too large. 993 */ 994 WARN_ON(val & DBPRIO_F); 995 996 /* If we're only writing a single Egress Unit and the BAR2 997 * Queue ID is 0, we can use the Write Combining Doorbell 998 * Gather Buffer; otherwise we use the simple doorbell. 999 */ 1000 if (n == 1 && tq->bar2_qid == 0) { 1001 unsigned int index = (tq->pidx 1002 ? (tq->pidx - 1) 1003 : (tq->size - 1)); 1004 __be64 *src = (__be64 *)&tq->desc[index]; 1005 __be64 __iomem *dst = (__be64 __iomem *)(tq->bar2_addr + 1006 SGE_UDB_WCDOORBELL); 1007 unsigned int count = EQ_UNIT / sizeof(__be64); 1008 1009 /* Copy the TX Descriptor in a tight loop in order to 1010 * try to get it to the adapter in a single Write 1011 * Combined transfer on the PCI-E Bus. If the Write 1012 * Combine fails (say because of an interrupt, etc.) 1013 * the hardware will simply take the last write as a 1014 * simple doorbell write with a PIDX Increment of 1 1015 * and will fetch the TX Descriptor from memory via 1016 * DMA. 1017 */ 1018 while (count) { 1019 /* the (__force u64) is because the compiler 1020 * doesn't understand the endian swizzling 1021 * going on 1022 */ 1023 writeq((__force u64)*src, dst); 1024 src++; 1025 dst++; 1026 count--; 1027 } 1028 } else 1029 writel(val | QID_V(tq->bar2_qid), 1030 tq->bar2_addr + SGE_UDB_KDOORBELL); 1031 1032 /* This Write Memory Barrier will force the write to the User 1033 * Doorbell area to be flushed. This is needed to prevent 1034 * writes on different CPUs for the same queue from hitting 1035 * the adapter out of order. This is required when some Work 1036 * Requests take the Write Combine Gather Buffer path (user 1037 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some 1038 * take the traditional path where we simply increment the 1039 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the 1040 * hardware DMA read the actual Work Request. 1041 */ 1042 wmb(); 1043 } 1044 } 1045 1046 /** 1047 * inline_tx_skb - inline a packet's data into TX descriptors 1048 * @skb: the packet 1049 * @tq: the TX queue where the packet will be inlined 1050 * @pos: starting position in the TX queue to inline the packet 1051 * 1052 * Inline a packet's contents directly into TX descriptors, starting at 1053 * the given position within the TX DMA ring. 1054 * Most of the complexity of this operation is dealing with wrap arounds 1055 * in the middle of the packet we want to inline. 1056 */ 1057 static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq, 1058 void *pos) 1059 { 1060 u64 *p; 1061 int left = (void *)tq->stat - pos; 1062 1063 if (likely(skb->len <= left)) { 1064 if (likely(!skb->data_len)) 1065 skb_copy_from_linear_data(skb, pos, skb->len); 1066 else 1067 skb_copy_bits(skb, 0, pos, skb->len); 1068 pos += skb->len; 1069 } else { 1070 skb_copy_bits(skb, 0, pos, left); 1071 skb_copy_bits(skb, left, tq->desc, skb->len - left); 1072 pos = (void *)tq->desc + (skb->len - left); 1073 } 1074 1075 /* 0-pad to multiple of 16 */ 1076 p = PTR_ALIGN(pos, 8); 1077 if ((uintptr_t)p & 8) 1078 *p = 0; 1079 } 1080 1081 /* 1082 * Figure out what HW csum a packet wants and return the appropriate control 1083 * bits. 1084 */ 1085 static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb) 1086 { 1087 int csum_type; 1088 const struct iphdr *iph = ip_hdr(skb); 1089 1090 if (iph->version == 4) { 1091 if (iph->protocol == IPPROTO_TCP) 1092 csum_type = TX_CSUM_TCPIP; 1093 else if (iph->protocol == IPPROTO_UDP) 1094 csum_type = TX_CSUM_UDPIP; 1095 else { 1096 nocsum: 1097 /* 1098 * unknown protocol, disable HW csum 1099 * and hope a bad packet is detected 1100 */ 1101 return TXPKT_L4CSUM_DIS_F; 1102 } 1103 } else { 1104 /* 1105 * this doesn't work with extension headers 1106 */ 1107 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph; 1108 1109 if (ip6h->nexthdr == IPPROTO_TCP) 1110 csum_type = TX_CSUM_TCPIP6; 1111 else if (ip6h->nexthdr == IPPROTO_UDP) 1112 csum_type = TX_CSUM_UDPIP6; 1113 else 1114 goto nocsum; 1115 } 1116 1117 if (likely(csum_type >= TX_CSUM_TCPIP)) { 1118 u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb)); 1119 int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN; 1120 1121 if (chip <= CHELSIO_T5) 1122 hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len); 1123 else 1124 hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len); 1125 return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len; 1126 } else { 1127 int start = skb_transport_offset(skb); 1128 1129 return TXPKT_CSUM_TYPE_V(csum_type) | 1130 TXPKT_CSUM_START_V(start) | 1131 TXPKT_CSUM_LOC_V(start + skb->csum_offset); 1132 } 1133 } 1134 1135 /* 1136 * Stop an Ethernet TX queue and record that state change. 1137 */ 1138 static void txq_stop(struct sge_eth_txq *txq) 1139 { 1140 netif_tx_stop_queue(txq->txq); 1141 txq->q.stops++; 1142 } 1143 1144 /* 1145 * Advance our software state for a TX queue by adding n in use descriptors. 1146 */ 1147 static inline void txq_advance(struct sge_txq *tq, unsigned int n) 1148 { 1149 tq->in_use += n; 1150 tq->pidx += n; 1151 if (tq->pidx >= tq->size) 1152 tq->pidx -= tq->size; 1153 } 1154 1155 /** 1156 * t4vf_eth_xmit - add a packet to an Ethernet TX queue 1157 * @skb: the packet 1158 * @dev: the egress net device 1159 * 1160 * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled. 1161 */ 1162 int t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev) 1163 { 1164 u32 wr_mid; 1165 u64 cntrl, *end; 1166 int qidx, credits, max_pkt_len; 1167 unsigned int flits, ndesc; 1168 struct adapter *adapter; 1169 struct sge_eth_txq *txq; 1170 const struct port_info *pi; 1171 struct fw_eth_tx_pkt_vm_wr *wr; 1172 struct cpl_tx_pkt_core *cpl; 1173 const struct skb_shared_info *ssi; 1174 dma_addr_t addr[MAX_SKB_FRAGS + 1]; 1175 const size_t fw_hdr_copy_len = (sizeof(wr->ethmacdst) + 1176 sizeof(wr->ethmacsrc) + 1177 sizeof(wr->ethtype) + 1178 sizeof(wr->vlantci)); 1179 1180 /* 1181 * The chip minimum packet length is 10 octets but the firmware 1182 * command that we are using requires that we copy the Ethernet header 1183 * (including the VLAN tag) into the header so we reject anything 1184 * smaller than that ... 1185 */ 1186 if (unlikely(skb->len < fw_hdr_copy_len)) 1187 goto out_free; 1188 1189 /* Discard the packet if the length is greater than mtu */ 1190 max_pkt_len = ETH_HLEN + dev->mtu; 1191 if (skb_vlan_tagged(skb)) 1192 max_pkt_len += VLAN_HLEN; 1193 if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len))) 1194 goto out_free; 1195 1196 /* 1197 * Figure out which TX Queue we're going to use. 1198 */ 1199 pi = netdev_priv(dev); 1200 adapter = pi->adapter; 1201 qidx = skb_get_queue_mapping(skb); 1202 BUG_ON(qidx >= pi->nqsets); 1203 txq = &adapter->sge.ethtxq[pi->first_qset + qidx]; 1204 1205 if (pi->vlan_id && !skb_vlan_tag_present(skb)) 1206 __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q), 1207 pi->vlan_id); 1208 1209 /* 1210 * Take this opportunity to reclaim any TX Descriptors whose DMA 1211 * transfers have completed. 1212 */ 1213 reclaim_completed_tx(adapter, &txq->q, true); 1214 1215 /* 1216 * Calculate the number of flits and TX Descriptors we're going to 1217 * need along with how many TX Descriptors will be left over after 1218 * we inject our Work Request. 1219 */ 1220 flits = calc_tx_flits(skb); 1221 ndesc = flits_to_desc(flits); 1222 credits = txq_avail(&txq->q) - ndesc; 1223 1224 if (unlikely(credits < 0)) { 1225 /* 1226 * Not enough room for this packet's Work Request. Stop the 1227 * TX Queue and return a "busy" condition. The queue will get 1228 * started later on when the firmware informs us that space 1229 * has opened up. 1230 */ 1231 txq_stop(txq); 1232 dev_err(adapter->pdev_dev, 1233 "%s: TX ring %u full while queue awake!\n", 1234 dev->name, qidx); 1235 return NETDEV_TX_BUSY; 1236 } 1237 1238 if (!is_eth_imm(skb) && 1239 unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) { 1240 /* 1241 * We need to map the skb into PCI DMA space (because it can't 1242 * be in-lined directly into the Work Request) and the mapping 1243 * operation failed. Record the error and drop the packet. 1244 */ 1245 txq->mapping_err++; 1246 goto out_free; 1247 } 1248 1249 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2)); 1250 if (unlikely(credits < ETHTXQ_STOP_THRES)) { 1251 /* 1252 * After we're done injecting the Work Request for this 1253 * packet, we'll be below our "stop threshold" so stop the TX 1254 * Queue now and schedule a request for an SGE Egress Queue 1255 * Update message. The queue will get started later on when 1256 * the firmware processes this Work Request and sends us an 1257 * Egress Queue Status Update message indicating that space 1258 * has opened up. 1259 */ 1260 txq_stop(txq); 1261 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F; 1262 } 1263 1264 /* 1265 * Start filling in our Work Request. Note that we do _not_ handle 1266 * the WR Header wrapping around the TX Descriptor Ring. If our 1267 * maximum header size ever exceeds one TX Descriptor, we'll need to 1268 * do something else here. 1269 */ 1270 BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1); 1271 wr = (void *)&txq->q.desc[txq->q.pidx]; 1272 wr->equiq_to_len16 = cpu_to_be32(wr_mid); 1273 wr->r3[0] = cpu_to_be32(0); 1274 wr->r3[1] = cpu_to_be32(0); 1275 skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len); 1276 end = (u64 *)wr + flits; 1277 1278 /* 1279 * If this is a Large Send Offload packet we'll put in an LSO CPL 1280 * message with an encapsulated TX Packet CPL message. Otherwise we 1281 * just use a TX Packet CPL message. 1282 */ 1283 ssi = skb_shinfo(skb); 1284 if (ssi->gso_size) { 1285 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1); 1286 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0; 1287 int l3hdr_len = skb_network_header_len(skb); 1288 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN; 1289 1290 wr->op_immdlen = 1291 cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) | 1292 FW_WR_IMMDLEN_V(sizeof(*lso) + 1293 sizeof(*cpl))); 1294 /* 1295 * Fill in the LSO CPL message. 1296 */ 1297 lso->lso_ctrl = 1298 cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) | 1299 LSO_FIRST_SLICE_F | 1300 LSO_LAST_SLICE_F | 1301 LSO_IPV6_V(v6) | 1302 LSO_ETHHDR_LEN_V(eth_xtra_len / 4) | 1303 LSO_IPHDR_LEN_V(l3hdr_len / 4) | 1304 LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff)); 1305 lso->ipid_ofst = cpu_to_be16(0); 1306 lso->mss = cpu_to_be16(ssi->gso_size); 1307 lso->seqno_offset = cpu_to_be32(0); 1308 if (is_t4(adapter->params.chip)) 1309 lso->len = cpu_to_be32(skb->len); 1310 else 1311 lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len)); 1312 1313 /* 1314 * Set up TX Packet CPL pointer, control word and perform 1315 * accounting. 1316 */ 1317 cpl = (void *)(lso + 1); 1318 1319 if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) 1320 cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len); 1321 else 1322 cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len); 1323 1324 cntrl |= TXPKT_CSUM_TYPE_V(v6 ? 1325 TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) | 1326 TXPKT_IPHDR_LEN_V(l3hdr_len); 1327 txq->tso++; 1328 txq->tx_cso += ssi->gso_segs; 1329 } else { 1330 int len; 1331 1332 len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl); 1333 wr->op_immdlen = 1334 cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) | 1335 FW_WR_IMMDLEN_V(len)); 1336 1337 /* 1338 * Set up TX Packet CPL pointer, control word and perform 1339 * accounting. 1340 */ 1341 cpl = (void *)(wr + 1); 1342 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1343 cntrl = hwcsum(adapter->params.chip, skb) | 1344 TXPKT_IPCSUM_DIS_F; 1345 txq->tx_cso++; 1346 } else 1347 cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F; 1348 } 1349 1350 /* 1351 * If there's a VLAN tag present, add that to the list of things to 1352 * do in this Work Request. 1353 */ 1354 if (skb_vlan_tag_present(skb)) { 1355 txq->vlan_ins++; 1356 cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb)); 1357 } 1358 1359 /* 1360 * Fill in the TX Packet CPL message header. 1361 */ 1362 cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) | 1363 TXPKT_INTF_V(pi->port_id) | 1364 TXPKT_PF_V(0)); 1365 cpl->pack = cpu_to_be16(0); 1366 cpl->len = cpu_to_be16(skb->len); 1367 cpl->ctrl1 = cpu_to_be64(cntrl); 1368 1369 #ifdef T4_TRACE 1370 T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7], 1371 "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u", 1372 ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags); 1373 #endif 1374 1375 /* 1376 * Fill in the body of the TX Packet CPL message with either in-lined 1377 * data or a Scatter/Gather List. 1378 */ 1379 if (is_eth_imm(skb)) { 1380 /* 1381 * In-line the packet's data and free the skb since we don't 1382 * need it any longer. 1383 */ 1384 inline_tx_skb(skb, &txq->q, cpl + 1); 1385 dev_consume_skb_any(skb); 1386 } else { 1387 /* 1388 * Write the skb's Scatter/Gather list into the TX Packet CPL 1389 * message and retain a pointer to the skb so we can free it 1390 * later when its DMA completes. (We store the skb pointer 1391 * in the Software Descriptor corresponding to the last TX 1392 * Descriptor used by the Work Request.) 1393 * 1394 * The retained skb will be freed when the corresponding TX 1395 * Descriptors are reclaimed after their DMAs complete. 1396 * However, this could take quite a while since, in general, 1397 * the hardware is set up to be lazy about sending DMA 1398 * completion notifications to us and we mostly perform TX 1399 * reclaims in the transmit routine. 1400 * 1401 * This is good for performamce but means that we rely on new 1402 * TX packets arriving to run the destructors of completed 1403 * packets, which open up space in their sockets' send queues. 1404 * Sometimes we do not get such new packets causing TX to 1405 * stall. A single UDP transmitter is a good example of this 1406 * situation. We have a clean up timer that periodically 1407 * reclaims completed packets but it doesn't run often enough 1408 * (nor do we want it to) to prevent lengthy stalls. A 1409 * solution to this problem is to run the destructor early, 1410 * after the packet is queued but before it's DMAd. A con is 1411 * that we lie to socket memory accounting, but the amount of 1412 * extra memory is reasonable (limited by the number of TX 1413 * descriptors), the packets do actually get freed quickly by 1414 * new packets almost always, and for protocols like TCP that 1415 * wait for acks to really free up the data the extra memory 1416 * is even less. On the positive side we run the destructors 1417 * on the sending CPU rather than on a potentially different 1418 * completing CPU, usually a good thing. 1419 * 1420 * Run the destructor before telling the DMA engine about the 1421 * packet to make sure it doesn't complete and get freed 1422 * prematurely. 1423 */ 1424 struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1); 1425 struct sge_txq *tq = &txq->q; 1426 int last_desc; 1427 1428 /* 1429 * If the Work Request header was an exact multiple of our TX 1430 * Descriptor length, then it's possible that the starting SGL 1431 * pointer lines up exactly with the end of our TX Descriptor 1432 * ring. If that's the case, wrap around to the beginning 1433 * here ... 1434 */ 1435 if (unlikely((void *)sgl == (void *)tq->stat)) { 1436 sgl = (void *)tq->desc; 1437 end = ((void *)tq->desc + ((void *)end - (void *)tq->stat)); 1438 } 1439 1440 write_sgl(skb, tq, sgl, end, 0, addr); 1441 skb_orphan(skb); 1442 1443 last_desc = tq->pidx + ndesc - 1; 1444 if (last_desc >= tq->size) 1445 last_desc -= tq->size; 1446 tq->sdesc[last_desc].skb = skb; 1447 tq->sdesc[last_desc].sgl = sgl; 1448 } 1449 1450 /* 1451 * Advance our internal TX Queue state, tell the hardware about 1452 * the new TX descriptors and return success. 1453 */ 1454 txq_advance(&txq->q, ndesc); 1455 netif_trans_update(dev); 1456 ring_tx_db(adapter, &txq->q, ndesc); 1457 return NETDEV_TX_OK; 1458 1459 out_free: 1460 /* 1461 * An error of some sort happened. Free the TX skb and tell the 1462 * OS that we've "dealt" with the packet ... 1463 */ 1464 dev_kfree_skb_any(skb); 1465 return NETDEV_TX_OK; 1466 } 1467 1468 /** 1469 * copy_frags - copy fragments from gather list into skb_shared_info 1470 * @skb: destination skb 1471 * @gl: source internal packet gather list 1472 * @offset: packet start offset in first page 1473 * 1474 * Copy an internal packet gather list into a Linux skb_shared_info 1475 * structure. 1476 */ 1477 static inline void copy_frags(struct sk_buff *skb, 1478 const struct pkt_gl *gl, 1479 unsigned int offset) 1480 { 1481 int i; 1482 1483 /* usually there's just one frag */ 1484 __skb_fill_page_desc(skb, 0, gl->frags[0].page, 1485 gl->frags[0].offset + offset, 1486 gl->frags[0].size - offset); 1487 skb_shinfo(skb)->nr_frags = gl->nfrags; 1488 for (i = 1; i < gl->nfrags; i++) 1489 __skb_fill_page_desc(skb, i, gl->frags[i].page, 1490 gl->frags[i].offset, 1491 gl->frags[i].size); 1492 1493 /* get a reference to the last page, we don't own it */ 1494 get_page(gl->frags[gl->nfrags - 1].page); 1495 } 1496 1497 /** 1498 * t4vf_pktgl_to_skb - build an sk_buff from a packet gather list 1499 * @gl: the gather list 1500 * @skb_len: size of sk_buff main body if it carries fragments 1501 * @pull_len: amount of data to move to the sk_buff's main body 1502 * 1503 * Builds an sk_buff from the given packet gather list. Returns the 1504 * sk_buff or %NULL if sk_buff allocation failed. 1505 */ 1506 static struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl, 1507 unsigned int skb_len, 1508 unsigned int pull_len) 1509 { 1510 struct sk_buff *skb; 1511 1512 /* 1513 * If the ingress packet is small enough, allocate an skb large enough 1514 * for all of the data and copy it inline. Otherwise, allocate an skb 1515 * with enough room to pull in the header and reference the rest of 1516 * the data via the skb fragment list. 1517 * 1518 * Below we rely on RX_COPY_THRES being less than the smallest Rx 1519 * buff! size, which is expected since buffers are at least 1520 * PAGE_SIZEd. In this case packets up to RX_COPY_THRES have only one 1521 * fragment. 1522 */ 1523 if (gl->tot_len <= RX_COPY_THRES) { 1524 /* small packets have only one fragment */ 1525 skb = alloc_skb(gl->tot_len, GFP_ATOMIC); 1526 if (unlikely(!skb)) 1527 goto out; 1528 __skb_put(skb, gl->tot_len); 1529 skb_copy_to_linear_data(skb, gl->va, gl->tot_len); 1530 } else { 1531 skb = alloc_skb(skb_len, GFP_ATOMIC); 1532 if (unlikely(!skb)) 1533 goto out; 1534 __skb_put(skb, pull_len); 1535 skb_copy_to_linear_data(skb, gl->va, pull_len); 1536 1537 copy_frags(skb, gl, pull_len); 1538 skb->len = gl->tot_len; 1539 skb->data_len = skb->len - pull_len; 1540 skb->truesize += skb->data_len; 1541 } 1542 1543 out: 1544 return skb; 1545 } 1546 1547 /** 1548 * t4vf_pktgl_free - free a packet gather list 1549 * @gl: the gather list 1550 * 1551 * Releases the pages of a packet gather list. We do not own the last 1552 * page on the list and do not free it. 1553 */ 1554 static void t4vf_pktgl_free(const struct pkt_gl *gl) 1555 { 1556 int frag; 1557 1558 frag = gl->nfrags - 1; 1559 while (frag--) 1560 put_page(gl->frags[frag].page); 1561 } 1562 1563 /** 1564 * do_gro - perform Generic Receive Offload ingress packet processing 1565 * @rxq: ingress RX Ethernet Queue 1566 * @gl: gather list for ingress packet 1567 * @pkt: CPL header for last packet fragment 1568 * 1569 * Perform Generic Receive Offload (GRO) ingress packet processing. 1570 * We use the standard Linux GRO interfaces for this. 1571 */ 1572 static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl, 1573 const struct cpl_rx_pkt *pkt) 1574 { 1575 struct adapter *adapter = rxq->rspq.adapter; 1576 struct sge *s = &adapter->sge; 1577 struct port_info *pi; 1578 int ret; 1579 struct sk_buff *skb; 1580 1581 skb = napi_get_frags(&rxq->rspq.napi); 1582 if (unlikely(!skb)) { 1583 t4vf_pktgl_free(gl); 1584 rxq->stats.rx_drops++; 1585 return; 1586 } 1587 1588 copy_frags(skb, gl, s->pktshift); 1589 skb->len = gl->tot_len - s->pktshift; 1590 skb->data_len = skb->len; 1591 skb->truesize += skb->data_len; 1592 skb->ip_summed = CHECKSUM_UNNECESSARY; 1593 skb_record_rx_queue(skb, rxq->rspq.idx); 1594 pi = netdev_priv(skb->dev); 1595 1596 if (pkt->vlan_ex && !pi->vlan_id) { 1597 __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q), 1598 be16_to_cpu(pkt->vlan)); 1599 rxq->stats.vlan_ex++; 1600 } 1601 ret = napi_gro_frags(&rxq->rspq.napi); 1602 1603 if (ret == GRO_HELD) 1604 rxq->stats.lro_pkts++; 1605 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE) 1606 rxq->stats.lro_merged++; 1607 rxq->stats.pkts++; 1608 rxq->stats.rx_cso++; 1609 } 1610 1611 /** 1612 * t4vf_ethrx_handler - process an ingress ethernet packet 1613 * @rspq: the response queue that received the packet 1614 * @rsp: the response queue descriptor holding the RX_PKT message 1615 * @gl: the gather list of packet fragments 1616 * 1617 * Process an ingress ethernet packet and deliver it to the stack. 1618 */ 1619 int t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp, 1620 const struct pkt_gl *gl) 1621 { 1622 struct sk_buff *skb; 1623 const struct cpl_rx_pkt *pkt = (void *)rsp; 1624 bool csum_ok = pkt->csum_calc && !pkt->err_vec && 1625 (rspq->netdev->features & NETIF_F_RXCSUM); 1626 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq); 1627 struct adapter *adapter = rspq->adapter; 1628 struct sge *s = &adapter->sge; 1629 struct port_info *pi; 1630 1631 /* 1632 * If this is a good TCP packet and we have Generic Receive Offload 1633 * enabled, handle the packet in the GRO path. 1634 */ 1635 if ((pkt->l2info & cpu_to_be32(RXF_TCP_F)) && 1636 (rspq->netdev->features & NETIF_F_GRO) && csum_ok && 1637 !pkt->ip_frag) { 1638 do_gro(rxq, gl, pkt); 1639 return 0; 1640 } 1641 1642 /* 1643 * Convert the Packet Gather List into an skb. 1644 */ 1645 skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN); 1646 if (unlikely(!skb)) { 1647 t4vf_pktgl_free(gl); 1648 rxq->stats.rx_drops++; 1649 return 0; 1650 } 1651 __skb_pull(skb, s->pktshift); 1652 skb->protocol = eth_type_trans(skb, rspq->netdev); 1653 skb_record_rx_queue(skb, rspq->idx); 1654 pi = netdev_priv(skb->dev); 1655 rxq->stats.pkts++; 1656 1657 if (csum_ok && !pkt->err_vec && 1658 (be32_to_cpu(pkt->l2info) & (RXF_UDP_F | RXF_TCP_F))) { 1659 if (!pkt->ip_frag) { 1660 skb->ip_summed = CHECKSUM_UNNECESSARY; 1661 rxq->stats.rx_cso++; 1662 } else if (pkt->l2info & htonl(RXF_IP_F)) { 1663 __sum16 c = (__force __sum16)pkt->csum; 1664 skb->csum = csum_unfold(c); 1665 skb->ip_summed = CHECKSUM_COMPLETE; 1666 rxq->stats.rx_cso++; 1667 } 1668 } else 1669 skb_checksum_none_assert(skb); 1670 1671 if (pkt->vlan_ex && !pi->vlan_id) { 1672 rxq->stats.vlan_ex++; 1673 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 1674 be16_to_cpu(pkt->vlan)); 1675 } 1676 1677 netif_receive_skb(skb); 1678 1679 return 0; 1680 } 1681 1682 /** 1683 * is_new_response - check if a response is newly written 1684 * @rc: the response control descriptor 1685 * @rspq: the response queue 1686 * 1687 * Returns true if a response descriptor contains a yet unprocessed 1688 * response. 1689 */ 1690 static inline bool is_new_response(const struct rsp_ctrl *rc, 1691 const struct sge_rspq *rspq) 1692 { 1693 return ((rc->type_gen >> RSPD_GEN_S) & 0x1) == rspq->gen; 1694 } 1695 1696 /** 1697 * restore_rx_bufs - put back a packet's RX buffers 1698 * @gl: the packet gather list 1699 * @fl: the SGE Free List 1700 * @nfrags: how many fragments in @si 1701 * 1702 * Called when we find out that the current packet, @si, can't be 1703 * processed right away for some reason. This is a very rare event and 1704 * there's no effort to make this suspension/resumption process 1705 * particularly efficient. 1706 * 1707 * We implement the suspension by putting all of the RX buffers associated 1708 * with the current packet back on the original Free List. The buffers 1709 * have already been unmapped and are left unmapped, we mark them as 1710 * unmapped in order to prevent further unmapping attempts. (Effectively 1711 * this function undoes the series of @unmap_rx_buf calls which were done 1712 * to create the current packet's gather list.) This leaves us ready to 1713 * restart processing of the packet the next time we start processing the 1714 * RX Queue ... 1715 */ 1716 static void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl, 1717 int frags) 1718 { 1719 struct rx_sw_desc *sdesc; 1720 1721 while (frags--) { 1722 if (fl->cidx == 0) 1723 fl->cidx = fl->size - 1; 1724 else 1725 fl->cidx--; 1726 sdesc = &fl->sdesc[fl->cidx]; 1727 sdesc->page = gl->frags[frags].page; 1728 sdesc->dma_addr |= RX_UNMAPPED_BUF; 1729 fl->avail++; 1730 } 1731 } 1732 1733 /** 1734 * rspq_next - advance to the next entry in a response queue 1735 * @rspq: the queue 1736 * 1737 * Updates the state of a response queue to advance it to the next entry. 1738 */ 1739 static inline void rspq_next(struct sge_rspq *rspq) 1740 { 1741 rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len; 1742 if (unlikely(++rspq->cidx == rspq->size)) { 1743 rspq->cidx = 0; 1744 rspq->gen ^= 1; 1745 rspq->cur_desc = rspq->desc; 1746 } 1747 } 1748 1749 /** 1750 * process_responses - process responses from an SGE response queue 1751 * @rspq: the ingress response queue to process 1752 * @budget: how many responses can be processed in this round 1753 * 1754 * Process responses from a Scatter Gather Engine response queue up to 1755 * the supplied budget. Responses include received packets as well as 1756 * control messages from firmware or hardware. 1757 * 1758 * Additionally choose the interrupt holdoff time for the next interrupt 1759 * on this queue. If the system is under memory shortage use a fairly 1760 * long delay to help recovery. 1761 */ 1762 static int process_responses(struct sge_rspq *rspq, int budget) 1763 { 1764 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq); 1765 struct adapter *adapter = rspq->adapter; 1766 struct sge *s = &adapter->sge; 1767 int budget_left = budget; 1768 1769 while (likely(budget_left)) { 1770 int ret, rsp_type; 1771 const struct rsp_ctrl *rc; 1772 1773 rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc)); 1774 if (!is_new_response(rc, rspq)) 1775 break; 1776 1777 /* 1778 * Figure out what kind of response we've received from the 1779 * SGE. 1780 */ 1781 dma_rmb(); 1782 rsp_type = RSPD_TYPE_G(rc->type_gen); 1783 if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) { 1784 struct page_frag *fp; 1785 struct pkt_gl gl; 1786 const struct rx_sw_desc *sdesc; 1787 u32 bufsz, frag; 1788 u32 len = be32_to_cpu(rc->pldbuflen_qid); 1789 1790 /* 1791 * If we get a "new buffer" message from the SGE we 1792 * need to move on to the next Free List buffer. 1793 */ 1794 if (len & RSPD_NEWBUF_F) { 1795 /* 1796 * We get one "new buffer" message when we 1797 * first start up a queue so we need to ignore 1798 * it when our offset into the buffer is 0. 1799 */ 1800 if (likely(rspq->offset > 0)) { 1801 free_rx_bufs(rspq->adapter, &rxq->fl, 1802 1); 1803 rspq->offset = 0; 1804 } 1805 len = RSPD_LEN_G(len); 1806 } 1807 gl.tot_len = len; 1808 1809 /* 1810 * Gather packet fragments. 1811 */ 1812 for (frag = 0, fp = gl.frags; /**/; frag++, fp++) { 1813 BUG_ON(frag >= MAX_SKB_FRAGS); 1814 BUG_ON(rxq->fl.avail == 0); 1815 sdesc = &rxq->fl.sdesc[rxq->fl.cidx]; 1816 bufsz = get_buf_size(adapter, sdesc); 1817 fp->page = sdesc->page; 1818 fp->offset = rspq->offset; 1819 fp->size = min(bufsz, len); 1820 len -= fp->size; 1821 if (!len) 1822 break; 1823 unmap_rx_buf(rspq->adapter, &rxq->fl); 1824 } 1825 gl.nfrags = frag+1; 1826 1827 /* 1828 * Last buffer remains mapped so explicitly make it 1829 * coherent for CPU access and start preloading first 1830 * cache line ... 1831 */ 1832 dma_sync_single_for_cpu(rspq->adapter->pdev_dev, 1833 get_buf_addr(sdesc), 1834 fp->size, DMA_FROM_DEVICE); 1835 gl.va = (page_address(gl.frags[0].page) + 1836 gl.frags[0].offset); 1837 prefetch(gl.va); 1838 1839 /* 1840 * Hand the new ingress packet to the handler for 1841 * this Response Queue. 1842 */ 1843 ret = rspq->handler(rspq, rspq->cur_desc, &gl); 1844 if (likely(ret == 0)) 1845 rspq->offset += ALIGN(fp->size, s->fl_align); 1846 else 1847 restore_rx_bufs(&gl, &rxq->fl, frag); 1848 } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) { 1849 ret = rspq->handler(rspq, rspq->cur_desc, NULL); 1850 } else { 1851 WARN_ON(rsp_type > RSPD_TYPE_CPL_X); 1852 ret = 0; 1853 } 1854 1855 if (unlikely(ret)) { 1856 /* 1857 * Couldn't process descriptor, back off for recovery. 1858 * We use the SGE's last timer which has the longest 1859 * interrupt coalescing value ... 1860 */ 1861 const int NOMEM_TIMER_IDX = SGE_NTIMERS-1; 1862 rspq->next_intr_params = 1863 QINTR_TIMER_IDX_V(NOMEM_TIMER_IDX); 1864 break; 1865 } 1866 1867 rspq_next(rspq); 1868 budget_left--; 1869 } 1870 1871 /* 1872 * If this is a Response Queue with an associated Free List and 1873 * at least two Egress Queue units available in the Free List 1874 * for new buffer pointers, refill the Free List. 1875 */ 1876 if (rspq->offset >= 0 && 1877 fl_cap(&rxq->fl) - rxq->fl.avail >= 2*FL_PER_EQ_UNIT) 1878 __refill_fl(rspq->adapter, &rxq->fl); 1879 return budget - budget_left; 1880 } 1881 1882 /** 1883 * napi_rx_handler - the NAPI handler for RX processing 1884 * @napi: the napi instance 1885 * @budget: how many packets we can process in this round 1886 * 1887 * Handler for new data events when using NAPI. This does not need any 1888 * locking or protection from interrupts as data interrupts are off at 1889 * this point and other adapter interrupts do not interfere (the latter 1890 * in not a concern at all with MSI-X as non-data interrupts then have 1891 * a separate handler). 1892 */ 1893 static int napi_rx_handler(struct napi_struct *napi, int budget) 1894 { 1895 unsigned int intr_params; 1896 struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi); 1897 int work_done = process_responses(rspq, budget); 1898 u32 val; 1899 1900 if (likely(work_done < budget)) { 1901 napi_complete_done(napi, work_done); 1902 intr_params = rspq->next_intr_params; 1903 rspq->next_intr_params = rspq->intr_params; 1904 } else 1905 intr_params = QINTR_TIMER_IDX_V(SGE_TIMER_UPD_CIDX); 1906 1907 if (unlikely(work_done == 0)) 1908 rspq->unhandled_irqs++; 1909 1910 val = CIDXINC_V(work_done) | SEINTARM_V(intr_params); 1911 /* If we don't have access to the new User GTS (T5+), use the old 1912 * doorbell mechanism; otherwise use the new BAR2 mechanism. 1913 */ 1914 if (unlikely(!rspq->bar2_addr)) { 1915 t4_write_reg(rspq->adapter, 1916 T4VF_SGE_BASE_ADDR + SGE_VF_GTS, 1917 val | INGRESSQID_V((u32)rspq->cntxt_id)); 1918 } else { 1919 writel(val | INGRESSQID_V(rspq->bar2_qid), 1920 rspq->bar2_addr + SGE_UDB_GTS); 1921 wmb(); 1922 } 1923 return work_done; 1924 } 1925 1926 /* 1927 * The MSI-X interrupt handler for an SGE response queue for the NAPI case 1928 * (i.e., response queue serviced by NAPI polling). 1929 */ 1930 irqreturn_t t4vf_sge_intr_msix(int irq, void *cookie) 1931 { 1932 struct sge_rspq *rspq = cookie; 1933 1934 napi_schedule(&rspq->napi); 1935 return IRQ_HANDLED; 1936 } 1937 1938 /* 1939 * Process the indirect interrupt entries in the interrupt queue and kick off 1940 * NAPI for each queue that has generated an entry. 1941 */ 1942 static unsigned int process_intrq(struct adapter *adapter) 1943 { 1944 struct sge *s = &adapter->sge; 1945 struct sge_rspq *intrq = &s->intrq; 1946 unsigned int work_done; 1947 u32 val; 1948 1949 spin_lock(&adapter->sge.intrq_lock); 1950 for (work_done = 0; ; work_done++) { 1951 const struct rsp_ctrl *rc; 1952 unsigned int qid, iq_idx; 1953 struct sge_rspq *rspq; 1954 1955 /* 1956 * Grab the next response from the interrupt queue and bail 1957 * out if it's not a new response. 1958 */ 1959 rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc)); 1960 if (!is_new_response(rc, intrq)) 1961 break; 1962 1963 /* 1964 * If the response isn't a forwarded interrupt message issue a 1965 * error and go on to the next response message. This should 1966 * never happen ... 1967 */ 1968 dma_rmb(); 1969 if (unlikely(RSPD_TYPE_G(rc->type_gen) != RSPD_TYPE_INTR_X)) { 1970 dev_err(adapter->pdev_dev, 1971 "Unexpected INTRQ response type %d\n", 1972 RSPD_TYPE_G(rc->type_gen)); 1973 continue; 1974 } 1975 1976 /* 1977 * Extract the Queue ID from the interrupt message and perform 1978 * sanity checking to make sure it really refers to one of our 1979 * Ingress Queues which is active and matches the queue's ID. 1980 * None of these error conditions should ever happen so we may 1981 * want to either make them fatal and/or conditionalized under 1982 * DEBUG. 1983 */ 1984 qid = RSPD_QID_G(be32_to_cpu(rc->pldbuflen_qid)); 1985 iq_idx = IQ_IDX(s, qid); 1986 if (unlikely(iq_idx >= MAX_INGQ)) { 1987 dev_err(adapter->pdev_dev, 1988 "Ingress QID %d out of range\n", qid); 1989 continue; 1990 } 1991 rspq = s->ingr_map[iq_idx]; 1992 if (unlikely(rspq == NULL)) { 1993 dev_err(adapter->pdev_dev, 1994 "Ingress QID %d RSPQ=NULL\n", qid); 1995 continue; 1996 } 1997 if (unlikely(rspq->abs_id != qid)) { 1998 dev_err(adapter->pdev_dev, 1999 "Ingress QID %d refers to RSPQ %d\n", 2000 qid, rspq->abs_id); 2001 continue; 2002 } 2003 2004 /* 2005 * Schedule NAPI processing on the indicated Response Queue 2006 * and move on to the next entry in the Forwarded Interrupt 2007 * Queue. 2008 */ 2009 napi_schedule(&rspq->napi); 2010 rspq_next(intrq); 2011 } 2012 2013 val = CIDXINC_V(work_done) | SEINTARM_V(intrq->intr_params); 2014 /* If we don't have access to the new User GTS (T5+), use the old 2015 * doorbell mechanism; otherwise use the new BAR2 mechanism. 2016 */ 2017 if (unlikely(!intrq->bar2_addr)) { 2018 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS, 2019 val | INGRESSQID_V(intrq->cntxt_id)); 2020 } else { 2021 writel(val | INGRESSQID_V(intrq->bar2_qid), 2022 intrq->bar2_addr + SGE_UDB_GTS); 2023 wmb(); 2024 } 2025 2026 spin_unlock(&adapter->sge.intrq_lock); 2027 2028 return work_done; 2029 } 2030 2031 /* 2032 * The MSI interrupt handler handles data events from SGE response queues as 2033 * well as error and other async events as they all use the same MSI vector. 2034 */ 2035 static irqreturn_t t4vf_intr_msi(int irq, void *cookie) 2036 { 2037 struct adapter *adapter = cookie; 2038 2039 process_intrq(adapter); 2040 return IRQ_HANDLED; 2041 } 2042 2043 /** 2044 * t4vf_intr_handler - select the top-level interrupt handler 2045 * @adapter: the adapter 2046 * 2047 * Selects the top-level interrupt handler based on the type of interrupts 2048 * (MSI-X or MSI). 2049 */ 2050 irq_handler_t t4vf_intr_handler(struct adapter *adapter) 2051 { 2052 BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0); 2053 if (adapter->flags & USING_MSIX) 2054 return t4vf_sge_intr_msix; 2055 else 2056 return t4vf_intr_msi; 2057 } 2058 2059 /** 2060 * sge_rx_timer_cb - perform periodic maintenance of SGE RX queues 2061 * @data: the adapter 2062 * 2063 * Runs periodically from a timer to perform maintenance of SGE RX queues. 2064 * 2065 * a) Replenishes RX queues that have run out due to memory shortage. 2066 * Normally new RX buffers are added when existing ones are consumed but 2067 * when out of memory a queue can become empty. We schedule NAPI to do 2068 * the actual refill. 2069 */ 2070 static void sge_rx_timer_cb(struct timer_list *t) 2071 { 2072 struct adapter *adapter = from_timer(adapter, t, sge.rx_timer); 2073 struct sge *s = &adapter->sge; 2074 unsigned int i; 2075 2076 /* 2077 * Scan the "Starving Free Lists" flag array looking for any Free 2078 * Lists in need of more free buffers. If we find one and it's not 2079 * being actively polled, then bump its "starving" counter and attempt 2080 * to refill it. If we're successful in adding enough buffers to push 2081 * the Free List over the starving threshold, then we can clear its 2082 * "starving" status. 2083 */ 2084 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) { 2085 unsigned long m; 2086 2087 for (m = s->starving_fl[i]; m; m &= m - 1) { 2088 unsigned int id = __ffs(m) + i * BITS_PER_LONG; 2089 struct sge_fl *fl = s->egr_map[id]; 2090 2091 clear_bit(id, s->starving_fl); 2092 smp_mb__after_atomic(); 2093 2094 /* 2095 * Since we are accessing fl without a lock there's a 2096 * small probability of a false positive where we 2097 * schedule napi but the FL is no longer starving. 2098 * No biggie. 2099 */ 2100 if (fl_starving(adapter, fl)) { 2101 struct sge_eth_rxq *rxq; 2102 2103 rxq = container_of(fl, struct sge_eth_rxq, fl); 2104 if (napi_reschedule(&rxq->rspq.napi)) 2105 fl->starving++; 2106 else 2107 set_bit(id, s->starving_fl); 2108 } 2109 } 2110 } 2111 2112 /* 2113 * Reschedule the next scan for starving Free Lists ... 2114 */ 2115 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD); 2116 } 2117 2118 /** 2119 * sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues 2120 * @data: the adapter 2121 * 2122 * Runs periodically from a timer to perform maintenance of SGE TX queues. 2123 * 2124 * b) Reclaims completed Tx packets for the Ethernet queues. Normally 2125 * packets are cleaned up by new Tx packets, this timer cleans up packets 2126 * when no new packets are being submitted. This is essential for pktgen, 2127 * at least. 2128 */ 2129 static void sge_tx_timer_cb(struct timer_list *t) 2130 { 2131 struct adapter *adapter = from_timer(adapter, t, sge.tx_timer); 2132 struct sge *s = &adapter->sge; 2133 unsigned int i, budget; 2134 2135 budget = MAX_TIMER_TX_RECLAIM; 2136 i = s->ethtxq_rover; 2137 do { 2138 struct sge_eth_txq *txq = &s->ethtxq[i]; 2139 2140 if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) { 2141 int avail = reclaimable(&txq->q); 2142 2143 if (avail > budget) 2144 avail = budget; 2145 2146 free_tx_desc(adapter, &txq->q, avail, true); 2147 txq->q.in_use -= avail; 2148 __netif_tx_unlock(txq->txq); 2149 2150 budget -= avail; 2151 if (!budget) 2152 break; 2153 } 2154 2155 i++; 2156 if (i >= s->ethqsets) 2157 i = 0; 2158 } while (i != s->ethtxq_rover); 2159 s->ethtxq_rover = i; 2160 2161 /* 2162 * If we found too many reclaimable packets schedule a timer in the 2163 * near future to continue where we left off. Otherwise the next timer 2164 * will be at its normal interval. 2165 */ 2166 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2)); 2167 } 2168 2169 /** 2170 * bar2_address - return the BAR2 address for an SGE Queue's Registers 2171 * @adapter: the adapter 2172 * @qid: the SGE Queue ID 2173 * @qtype: the SGE Queue Type (Egress or Ingress) 2174 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues 2175 * 2176 * Returns the BAR2 address for the SGE Queue Registers associated with 2177 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also 2178 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE 2179 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID" 2180 * Registers are supported (e.g. the Write Combining Doorbell Buffer). 2181 */ 2182 static void __iomem *bar2_address(struct adapter *adapter, 2183 unsigned int qid, 2184 enum t4_bar2_qtype qtype, 2185 unsigned int *pbar2_qid) 2186 { 2187 u64 bar2_qoffset; 2188 int ret; 2189 2190 ret = t4vf_bar2_sge_qregs(adapter, qid, qtype, 2191 &bar2_qoffset, pbar2_qid); 2192 if (ret) 2193 return NULL; 2194 2195 return adapter->bar2 + bar2_qoffset; 2196 } 2197 2198 /** 2199 * t4vf_sge_alloc_rxq - allocate an SGE RX Queue 2200 * @adapter: the adapter 2201 * @rspq: pointer to to the new rxq's Response Queue to be filled in 2202 * @iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue 2203 * @dev: the network device associated with the new rspq 2204 * @intr_dest: MSI-X vector index (overriden in MSI mode) 2205 * @fl: pointer to the new rxq's Free List to be filled in 2206 * @hnd: the interrupt handler to invoke for the rspq 2207 */ 2208 int t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq, 2209 bool iqasynch, struct net_device *dev, 2210 int intr_dest, 2211 struct sge_fl *fl, rspq_handler_t hnd) 2212 { 2213 struct sge *s = &adapter->sge; 2214 struct port_info *pi = netdev_priv(dev); 2215 struct fw_iq_cmd cmd, rpl; 2216 int ret, iqandst, flsz = 0; 2217 int relaxed = !(adapter->flags & ROOT_NO_RELAXED_ORDERING); 2218 2219 /* 2220 * If we're using MSI interrupts and we're not initializing the 2221 * Forwarded Interrupt Queue itself, then set up this queue for 2222 * indirect interrupts to the Forwarded Interrupt Queue. Obviously 2223 * the Forwarded Interrupt Queue must be set up before any other 2224 * ingress queue ... 2225 */ 2226 if ((adapter->flags & USING_MSI) && rspq != &adapter->sge.intrq) { 2227 iqandst = SGE_INTRDST_IQ; 2228 intr_dest = adapter->sge.intrq.abs_id; 2229 } else 2230 iqandst = SGE_INTRDST_PCI; 2231 2232 /* 2233 * Allocate the hardware ring for the Response Queue. The size needs 2234 * to be a multiple of 16 which includes the mandatory status entry 2235 * (regardless of whether the Status Page capabilities are enabled or 2236 * not). 2237 */ 2238 rspq->size = roundup(rspq->size, 16); 2239 rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len, 2240 0, &rspq->phys_addr, NULL, 0); 2241 if (!rspq->desc) 2242 return -ENOMEM; 2243 2244 /* 2245 * Fill in the Ingress Queue Command. Note: Ideally this code would 2246 * be in t4vf_hw.c but there are so many parameters and dependencies 2247 * on our Linux SGE state that we would end up having to pass tons of 2248 * parameters. We'll have to think about how this might be migrated 2249 * into OS-independent common code ... 2250 */ 2251 memset(&cmd, 0, sizeof(cmd)); 2252 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) | 2253 FW_CMD_REQUEST_F | 2254 FW_CMD_WRITE_F | 2255 FW_CMD_EXEC_F); 2256 cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC_F | 2257 FW_IQ_CMD_IQSTART_F | 2258 FW_LEN16(cmd)); 2259 cmd.type_to_iqandstindex = 2260 cpu_to_be32(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) | 2261 FW_IQ_CMD_IQASYNCH_V(iqasynch) | 2262 FW_IQ_CMD_VIID_V(pi->viid) | 2263 FW_IQ_CMD_IQANDST_V(iqandst) | 2264 FW_IQ_CMD_IQANUS_V(1) | 2265 FW_IQ_CMD_IQANUD_V(SGE_UPDATEDEL_INTR) | 2266 FW_IQ_CMD_IQANDSTINDEX_V(intr_dest)); 2267 cmd.iqdroprss_to_iqesize = 2268 cpu_to_be16(FW_IQ_CMD_IQPCIECH_V(pi->port_id) | 2269 FW_IQ_CMD_IQGTSMODE_F | 2270 FW_IQ_CMD_IQINTCNTTHRESH_V(rspq->pktcnt_idx) | 2271 FW_IQ_CMD_IQESIZE_V(ilog2(rspq->iqe_len) - 4)); 2272 cmd.iqsize = cpu_to_be16(rspq->size); 2273 cmd.iqaddr = cpu_to_be64(rspq->phys_addr); 2274 2275 if (fl) { 2276 enum chip_type chip = 2277 CHELSIO_CHIP_VERSION(adapter->params.chip); 2278 /* 2279 * Allocate the ring for the hardware free list (with space 2280 * for its status page) along with the associated software 2281 * descriptor ring. The free list size needs to be a multiple 2282 * of the Egress Queue Unit and at least 2 Egress Units larger 2283 * than the SGE's Egress Congrestion Threshold 2284 * (fl_starve_thres - 1). 2285 */ 2286 if (fl->size < s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT) 2287 fl->size = s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT; 2288 fl->size = roundup(fl->size, FL_PER_EQ_UNIT); 2289 fl->desc = alloc_ring(adapter->pdev_dev, fl->size, 2290 sizeof(__be64), sizeof(struct rx_sw_desc), 2291 &fl->addr, &fl->sdesc, s->stat_len); 2292 if (!fl->desc) { 2293 ret = -ENOMEM; 2294 goto err; 2295 } 2296 2297 /* 2298 * Calculate the size of the hardware free list ring plus 2299 * Status Page (which the SGE will place after the end of the 2300 * free list ring) in Egress Queue Units. 2301 */ 2302 flsz = (fl->size / FL_PER_EQ_UNIT + 2303 s->stat_len / EQ_UNIT); 2304 2305 /* 2306 * Fill in all the relevant firmware Ingress Queue Command 2307 * fields for the free list. 2308 */ 2309 cmd.iqns_to_fl0congen = 2310 cpu_to_be32( 2311 FW_IQ_CMD_FL0HOSTFCMODE_V(SGE_HOSTFCMODE_NONE) | 2312 FW_IQ_CMD_FL0PACKEN_F | 2313 FW_IQ_CMD_FL0FETCHRO_V(relaxed) | 2314 FW_IQ_CMD_FL0DATARO_V(relaxed) | 2315 FW_IQ_CMD_FL0PADEN_F); 2316 2317 /* In T6, for egress queue type FL there is internal overhead 2318 * of 16B for header going into FLM module. Hence the maximum 2319 * allowed burst size is 448 bytes. For T4/T5, the hardware 2320 * doesn't coalesce fetch requests if more than 64 bytes of 2321 * Free List pointers are provided, so we use a 128-byte Fetch 2322 * Burst Minimum there (T6 implements coalescing so we can use 2323 * the smaller 64-byte value there). 2324 */ 2325 cmd.fl0dcaen_to_fl0cidxfthresh = 2326 cpu_to_be16( 2327 FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ? 2328 FETCHBURSTMIN_128B_X : 2329 FETCHBURSTMIN_64B_X) | 2330 FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ? 2331 FETCHBURSTMAX_512B_X : 2332 FETCHBURSTMAX_256B_X)); 2333 cmd.fl0size = cpu_to_be16(flsz); 2334 cmd.fl0addr = cpu_to_be64(fl->addr); 2335 } 2336 2337 /* 2338 * Issue the firmware Ingress Queue Command and extract the results if 2339 * it completes successfully. 2340 */ 2341 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl); 2342 if (ret) 2343 goto err; 2344 2345 netif_napi_add(dev, &rspq->napi, napi_rx_handler, 64); 2346 rspq->cur_desc = rspq->desc; 2347 rspq->cidx = 0; 2348 rspq->gen = 1; 2349 rspq->next_intr_params = rspq->intr_params; 2350 rspq->cntxt_id = be16_to_cpu(rpl.iqid); 2351 rspq->bar2_addr = bar2_address(adapter, 2352 rspq->cntxt_id, 2353 T4_BAR2_QTYPE_INGRESS, 2354 &rspq->bar2_qid); 2355 rspq->abs_id = be16_to_cpu(rpl.physiqid); 2356 rspq->size--; /* subtract status entry */ 2357 rspq->adapter = adapter; 2358 rspq->netdev = dev; 2359 rspq->handler = hnd; 2360 2361 /* set offset to -1 to distinguish ingress queues without FL */ 2362 rspq->offset = fl ? 0 : -1; 2363 2364 if (fl) { 2365 fl->cntxt_id = be16_to_cpu(rpl.fl0id); 2366 fl->avail = 0; 2367 fl->pend_cred = 0; 2368 fl->pidx = 0; 2369 fl->cidx = 0; 2370 fl->alloc_failed = 0; 2371 fl->large_alloc_failed = 0; 2372 fl->starving = 0; 2373 2374 /* Note, we must initialize the BAR2 Free List User Doorbell 2375 * information before refilling the Free List! 2376 */ 2377 fl->bar2_addr = bar2_address(adapter, 2378 fl->cntxt_id, 2379 T4_BAR2_QTYPE_EGRESS, 2380 &fl->bar2_qid); 2381 2382 refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL); 2383 } 2384 2385 return 0; 2386 2387 err: 2388 /* 2389 * An error occurred. Clean up our partial allocation state and 2390 * return the error. 2391 */ 2392 if (rspq->desc) { 2393 dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len, 2394 rspq->desc, rspq->phys_addr); 2395 rspq->desc = NULL; 2396 } 2397 if (fl && fl->desc) { 2398 kfree(fl->sdesc); 2399 fl->sdesc = NULL; 2400 dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT, 2401 fl->desc, fl->addr); 2402 fl->desc = NULL; 2403 } 2404 return ret; 2405 } 2406 2407 /** 2408 * t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue 2409 * @adapter: the adapter 2410 * @txq: pointer to the new txq to be filled in 2411 * @devq: the network TX queue associated with the new txq 2412 * @iqid: the relative ingress queue ID to which events relating to 2413 * the new txq should be directed 2414 */ 2415 int t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq, 2416 struct net_device *dev, struct netdev_queue *devq, 2417 unsigned int iqid) 2418 { 2419 struct sge *s = &adapter->sge; 2420 int ret, nentries; 2421 struct fw_eq_eth_cmd cmd, rpl; 2422 struct port_info *pi = netdev_priv(dev); 2423 2424 /* 2425 * Calculate the size of the hardware TX Queue (including the Status 2426 * Page on the end of the TX Queue) in units of TX Descriptors. 2427 */ 2428 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc); 2429 2430 /* 2431 * Allocate the hardware ring for the TX ring (with space for its 2432 * status page) along with the associated software descriptor ring. 2433 */ 2434 txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size, 2435 sizeof(struct tx_desc), 2436 sizeof(struct tx_sw_desc), 2437 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len); 2438 if (!txq->q.desc) 2439 return -ENOMEM; 2440 2441 /* 2442 * Fill in the Egress Queue Command. Note: As with the direct use of 2443 * the firmware Ingress Queue COmmand above in our RXQ allocation 2444 * routine, ideally, this code would be in t4vf_hw.c. Again, we'll 2445 * have to see if there's some reasonable way to parameterize it 2446 * into the common code ... 2447 */ 2448 memset(&cmd, 0, sizeof(cmd)); 2449 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) | 2450 FW_CMD_REQUEST_F | 2451 FW_CMD_WRITE_F | 2452 FW_CMD_EXEC_F); 2453 cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC_F | 2454 FW_EQ_ETH_CMD_EQSTART_F | 2455 FW_LEN16(cmd)); 2456 cmd.viid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE_F | 2457 FW_EQ_ETH_CMD_VIID_V(pi->viid)); 2458 cmd.fetchszm_to_iqid = 2459 cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE_V(SGE_HOSTFCMODE_STPG) | 2460 FW_EQ_ETH_CMD_PCIECHN_V(pi->port_id) | 2461 FW_EQ_ETH_CMD_IQID_V(iqid)); 2462 cmd.dcaen_to_eqsize = 2463 cpu_to_be32(FW_EQ_ETH_CMD_FBMIN_V(SGE_FETCHBURSTMIN_64B) | 2464 FW_EQ_ETH_CMD_FBMAX_V(SGE_FETCHBURSTMAX_512B) | 2465 FW_EQ_ETH_CMD_CIDXFTHRESH_V( 2466 SGE_CIDXFLUSHTHRESH_32) | 2467 FW_EQ_ETH_CMD_EQSIZE_V(nentries)); 2468 cmd.eqaddr = cpu_to_be64(txq->q.phys_addr); 2469 2470 /* 2471 * Issue the firmware Egress Queue Command and extract the results if 2472 * it completes successfully. 2473 */ 2474 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl); 2475 if (ret) { 2476 /* 2477 * The girmware Ingress Queue Command failed for some reason. 2478 * Free up our partial allocation state and return the error. 2479 */ 2480 kfree(txq->q.sdesc); 2481 txq->q.sdesc = NULL; 2482 dma_free_coherent(adapter->pdev_dev, 2483 nentries * sizeof(struct tx_desc), 2484 txq->q.desc, txq->q.phys_addr); 2485 txq->q.desc = NULL; 2486 return ret; 2487 } 2488 2489 txq->q.in_use = 0; 2490 txq->q.cidx = 0; 2491 txq->q.pidx = 0; 2492 txq->q.stat = (void *)&txq->q.desc[txq->q.size]; 2493 txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_G(be32_to_cpu(rpl.eqid_pkd)); 2494 txq->q.bar2_addr = bar2_address(adapter, 2495 txq->q.cntxt_id, 2496 T4_BAR2_QTYPE_EGRESS, 2497 &txq->q.bar2_qid); 2498 txq->q.abs_id = 2499 FW_EQ_ETH_CMD_PHYSEQID_G(be32_to_cpu(rpl.physeqid_pkd)); 2500 txq->txq = devq; 2501 txq->tso = 0; 2502 txq->tx_cso = 0; 2503 txq->vlan_ins = 0; 2504 txq->q.stops = 0; 2505 txq->q.restarts = 0; 2506 txq->mapping_err = 0; 2507 return 0; 2508 } 2509 2510 /* 2511 * Free the DMA map resources associated with a TX queue. 2512 */ 2513 static void free_txq(struct adapter *adapter, struct sge_txq *tq) 2514 { 2515 struct sge *s = &adapter->sge; 2516 2517 dma_free_coherent(adapter->pdev_dev, 2518 tq->size * sizeof(*tq->desc) + s->stat_len, 2519 tq->desc, tq->phys_addr); 2520 tq->cntxt_id = 0; 2521 tq->sdesc = NULL; 2522 tq->desc = NULL; 2523 } 2524 2525 /* 2526 * Free the resources associated with a response queue (possibly including a 2527 * free list). 2528 */ 2529 static void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq, 2530 struct sge_fl *fl) 2531 { 2532 struct sge *s = &adapter->sge; 2533 unsigned int flid = fl ? fl->cntxt_id : 0xffff; 2534 2535 t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP, 2536 rspq->cntxt_id, flid, 0xffff); 2537 dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len, 2538 rspq->desc, rspq->phys_addr); 2539 netif_napi_del(&rspq->napi); 2540 rspq->netdev = NULL; 2541 rspq->cntxt_id = 0; 2542 rspq->abs_id = 0; 2543 rspq->desc = NULL; 2544 2545 if (fl) { 2546 free_rx_bufs(adapter, fl, fl->avail); 2547 dma_free_coherent(adapter->pdev_dev, 2548 fl->size * sizeof(*fl->desc) + s->stat_len, 2549 fl->desc, fl->addr); 2550 kfree(fl->sdesc); 2551 fl->sdesc = NULL; 2552 fl->cntxt_id = 0; 2553 fl->desc = NULL; 2554 } 2555 } 2556 2557 /** 2558 * t4vf_free_sge_resources - free SGE resources 2559 * @adapter: the adapter 2560 * 2561 * Frees resources used by the SGE queue sets. 2562 */ 2563 void t4vf_free_sge_resources(struct adapter *adapter) 2564 { 2565 struct sge *s = &adapter->sge; 2566 struct sge_eth_rxq *rxq = s->ethrxq; 2567 struct sge_eth_txq *txq = s->ethtxq; 2568 struct sge_rspq *evtq = &s->fw_evtq; 2569 struct sge_rspq *intrq = &s->intrq; 2570 int qs; 2571 2572 for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) { 2573 if (rxq->rspq.desc) 2574 free_rspq_fl(adapter, &rxq->rspq, &rxq->fl); 2575 if (txq->q.desc) { 2576 t4vf_eth_eq_free(adapter, txq->q.cntxt_id); 2577 free_tx_desc(adapter, &txq->q, txq->q.in_use, true); 2578 kfree(txq->q.sdesc); 2579 free_txq(adapter, &txq->q); 2580 } 2581 } 2582 if (evtq->desc) 2583 free_rspq_fl(adapter, evtq, NULL); 2584 if (intrq->desc) 2585 free_rspq_fl(adapter, intrq, NULL); 2586 } 2587 2588 /** 2589 * t4vf_sge_start - enable SGE operation 2590 * @adapter: the adapter 2591 * 2592 * Start tasklets and timers associated with the DMA engine. 2593 */ 2594 void t4vf_sge_start(struct adapter *adapter) 2595 { 2596 adapter->sge.ethtxq_rover = 0; 2597 mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD); 2598 mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD); 2599 } 2600 2601 /** 2602 * t4vf_sge_stop - disable SGE operation 2603 * @adapter: the adapter 2604 * 2605 * Stop tasklets and timers associated with the DMA engine. Note that 2606 * this is effective only if measures have been taken to disable any HW 2607 * events that may restart them. 2608 */ 2609 void t4vf_sge_stop(struct adapter *adapter) 2610 { 2611 struct sge *s = &adapter->sge; 2612 2613 if (s->rx_timer.function) 2614 del_timer_sync(&s->rx_timer); 2615 if (s->tx_timer.function) 2616 del_timer_sync(&s->tx_timer); 2617 } 2618 2619 /** 2620 * t4vf_sge_init - initialize SGE 2621 * @adapter: the adapter 2622 * 2623 * Performs SGE initialization needed every time after a chip reset. 2624 * We do not initialize any of the queue sets here, instead the driver 2625 * top-level must request those individually. We also do not enable DMA 2626 * here, that should be done after the queues have been set up. 2627 */ 2628 int t4vf_sge_init(struct adapter *adapter) 2629 { 2630 struct sge_params *sge_params = &adapter->params.sge; 2631 u32 fl_small_pg = sge_params->sge_fl_buffer_size[0]; 2632 u32 fl_large_pg = sge_params->sge_fl_buffer_size[1]; 2633 struct sge *s = &adapter->sge; 2634 2635 /* 2636 * Start by vetting the basic SGE parameters which have been set up by 2637 * the Physical Function Driver. Ideally we should be able to deal 2638 * with _any_ configuration. Practice is different ... 2639 */ 2640 2641 /* We only bother using the Large Page logic if the Large Page Buffer 2642 * is larger than our Page Size Buffer. 2643 */ 2644 if (fl_large_pg <= fl_small_pg) 2645 fl_large_pg = 0; 2646 2647 /* The Page Size Buffer must be exactly equal to our Page Size and the 2648 * Large Page Size Buffer should be 0 (per above) or a power of 2. 2649 */ 2650 if (fl_small_pg != PAGE_SIZE || 2651 (fl_large_pg & (fl_large_pg - 1)) != 0) { 2652 dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n", 2653 fl_small_pg, fl_large_pg); 2654 return -EINVAL; 2655 } 2656 if ((sge_params->sge_control & RXPKTCPLMODE_F) != 2657 RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) { 2658 dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n"); 2659 return -EINVAL; 2660 } 2661 2662 /* 2663 * Now translate the adapter parameters into our internal forms. 2664 */ 2665 if (fl_large_pg) 2666 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT; 2667 s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_F) 2668 ? 128 : 64); 2669 s->pktshift = PKTSHIFT_G(sge_params->sge_control); 2670 s->fl_align = t4vf_fl_pkt_align(adapter); 2671 2672 /* A FL with <= fl_starve_thres buffers is starving and a periodic 2673 * timer will attempt to refill it. This needs to be larger than the 2674 * SGE's Egress Congestion Threshold. If it isn't, then we can get 2675 * stuck waiting for new packets while the SGE is waiting for us to 2676 * give it more Free List entries. (Note that the SGE's Egress 2677 * Congestion Threshold is in units of 2 Free List pointers.) 2678 */ 2679 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) { 2680 case CHELSIO_T4: 2681 s->fl_starve_thres = 2682 EGRTHRESHOLD_G(sge_params->sge_congestion_control); 2683 break; 2684 case CHELSIO_T5: 2685 s->fl_starve_thres = 2686 EGRTHRESHOLDPACKING_G(sge_params->sge_congestion_control); 2687 break; 2688 case CHELSIO_T6: 2689 default: 2690 s->fl_starve_thres = 2691 T6_EGRTHRESHOLDPACKING_G(sge_params->sge_congestion_control); 2692 break; 2693 } 2694 s->fl_starve_thres = s->fl_starve_thres * 2 + 1; 2695 2696 /* 2697 * Set up tasklet timers. 2698 */ 2699 timer_setup(&s->rx_timer, sge_rx_timer_cb, 0); 2700 timer_setup(&s->tx_timer, sge_tx_timer_cb, 0); 2701 2702 /* 2703 * Initialize Forwarded Interrupt Queue lock. 2704 */ 2705 spin_lock_init(&s->intrq_lock); 2706 2707 return 0; 2708 } 2709