1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /**************************************************************************/ 3 /* */ 4 /* IBM System i and System p Virtual NIC Device Driver */ 5 /* Copyright (C) 2014 IBM Corp. */ 6 /* Santiago Leon (santi_leon@yahoo.com) */ 7 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */ 8 /* John Allen (jallen@linux.vnet.ibm.com) */ 9 /* */ 10 /* */ 11 /* This module contains the implementation of a virtual ethernet device */ 12 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */ 13 /* option of the RS/6000 Platform Architecture to interface with virtual */ 14 /* ethernet NICs that are presented to the partition by the hypervisor. */ 15 /* */ 16 /* Messages are passed between the VNIC driver and the VNIC server using */ 17 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */ 18 /* issue and receive commands that initiate communication with the server */ 19 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */ 20 /* are used by the driver to notify the server that a packet is */ 21 /* ready for transmission or that a buffer has been added to receive a */ 22 /* packet. Subsequently, sCRQs are used by the server to notify the */ 23 /* driver that a packet transmission has been completed or that a packet */ 24 /* has been received and placed in a waiting buffer. */ 25 /* */ 26 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */ 27 /* which skbs are DMA mapped and immediately unmapped when the transmit */ 28 /* or receive has been completed, the VNIC driver is required to use */ 29 /* "long term mapping". This entails that large, continuous DMA mapped */ 30 /* buffers are allocated on driver initialization and these buffers are */ 31 /* then continuously reused to pass skbs to and from the VNIC server. */ 32 /* */ 33 /**************************************************************************/ 34 35 #include <linux/module.h> 36 #include <linux/moduleparam.h> 37 #include <linux/types.h> 38 #include <linux/errno.h> 39 #include <linux/completion.h> 40 #include <linux/ioport.h> 41 #include <linux/dma-mapping.h> 42 #include <linux/kernel.h> 43 #include <linux/netdevice.h> 44 #include <linux/etherdevice.h> 45 #include <linux/skbuff.h> 46 #include <linux/init.h> 47 #include <linux/delay.h> 48 #include <linux/mm.h> 49 #include <linux/ethtool.h> 50 #include <linux/proc_fs.h> 51 #include <linux/if_arp.h> 52 #include <linux/in.h> 53 #include <linux/ip.h> 54 #include <linux/ipv6.h> 55 #include <linux/irq.h> 56 #include <linux/kthread.h> 57 #include <linux/seq_file.h> 58 #include <linux/interrupt.h> 59 #include <net/net_namespace.h> 60 #include <asm/hvcall.h> 61 #include <linux/atomic.h> 62 #include <asm/vio.h> 63 #include <asm/xive.h> 64 #include <asm/iommu.h> 65 #include <linux/uaccess.h> 66 #include <asm/firmware.h> 67 #include <linux/workqueue.h> 68 #include <linux/if_vlan.h> 69 #include <linux/utsname.h> 70 71 #include "ibmvnic.h" 72 73 static const char ibmvnic_driver_name[] = "ibmvnic"; 74 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver"; 75 76 MODULE_AUTHOR("Santiago Leon"); 77 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver"); 78 MODULE_LICENSE("GPL"); 79 MODULE_VERSION(IBMVNIC_DRIVER_VERSION); 80 81 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION; 82 static void release_sub_crqs(struct ibmvnic_adapter *, bool); 83 static int ibmvnic_reset_crq(struct ibmvnic_adapter *); 84 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *); 85 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *); 86 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *); 87 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64); 88 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance); 89 static int enable_scrq_irq(struct ibmvnic_adapter *, 90 struct ibmvnic_sub_crq_queue *); 91 static int disable_scrq_irq(struct ibmvnic_adapter *, 92 struct ibmvnic_sub_crq_queue *); 93 static int pending_scrq(struct ibmvnic_adapter *, 94 struct ibmvnic_sub_crq_queue *); 95 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *, 96 struct ibmvnic_sub_crq_queue *); 97 static int ibmvnic_poll(struct napi_struct *napi, int data); 98 static void send_query_map(struct ibmvnic_adapter *adapter); 99 static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, u32, u8); 100 static int send_request_unmap(struct ibmvnic_adapter *, u8); 101 static int send_login(struct ibmvnic_adapter *adapter); 102 static void send_query_cap(struct ibmvnic_adapter *adapter); 103 static int init_sub_crqs(struct ibmvnic_adapter *); 104 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter); 105 static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset); 106 static void release_crq_queue(struct ibmvnic_adapter *); 107 static int __ibmvnic_set_mac(struct net_device *, u8 *); 108 static int init_crq_queue(struct ibmvnic_adapter *adapter); 109 static int send_query_phys_parms(struct ibmvnic_adapter *adapter); 110 static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter, 111 struct ibmvnic_sub_crq_queue *tx_scrq); 112 static void free_long_term_buff(struct ibmvnic_adapter *adapter, 113 struct ibmvnic_long_term_buff *ltb); 114 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter); 115 116 struct ibmvnic_stat { 117 char name[ETH_GSTRING_LEN]; 118 int offset; 119 }; 120 121 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \ 122 offsetof(struct ibmvnic_statistics, stat)) 123 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off)))) 124 125 static const struct ibmvnic_stat ibmvnic_stats[] = { 126 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)}, 127 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)}, 128 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)}, 129 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)}, 130 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)}, 131 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)}, 132 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)}, 133 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)}, 134 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)}, 135 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)}, 136 {"align_errors", IBMVNIC_STAT_OFF(align_errors)}, 137 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)}, 138 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)}, 139 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)}, 140 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)}, 141 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)}, 142 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)}, 143 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)}, 144 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)}, 145 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)}, 146 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)}, 147 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)}, 148 }; 149 150 static int send_crq_init_complete(struct ibmvnic_adapter *adapter) 151 { 152 union ibmvnic_crq crq; 153 154 memset(&crq, 0, sizeof(crq)); 155 crq.generic.first = IBMVNIC_CRQ_INIT_CMD; 156 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE; 157 158 return ibmvnic_send_crq(adapter, &crq); 159 } 160 161 static int send_version_xchg(struct ibmvnic_adapter *adapter) 162 { 163 union ibmvnic_crq crq; 164 165 memset(&crq, 0, sizeof(crq)); 166 crq.version_exchange.first = IBMVNIC_CRQ_CMD; 167 crq.version_exchange.cmd = VERSION_EXCHANGE; 168 crq.version_exchange.version = cpu_to_be16(ibmvnic_version); 169 170 return ibmvnic_send_crq(adapter, &crq); 171 } 172 173 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token, 174 unsigned long length, unsigned long *number, 175 unsigned long *irq) 176 { 177 unsigned long retbuf[PLPAR_HCALL_BUFSIZE]; 178 long rc; 179 180 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length); 181 *number = retbuf[0]; 182 *irq = retbuf[1]; 183 184 return rc; 185 } 186 187 /** 188 * ibmvnic_wait_for_completion - Check device state and wait for completion 189 * @adapter: private device data 190 * @comp_done: completion structure to wait for 191 * @timeout: time to wait in milliseconds 192 * 193 * Wait for a completion signal or until the timeout limit is reached 194 * while checking that the device is still active. 195 */ 196 static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter, 197 struct completion *comp_done, 198 unsigned long timeout) 199 { 200 struct net_device *netdev; 201 unsigned long div_timeout; 202 u8 retry; 203 204 netdev = adapter->netdev; 205 retry = 5; 206 div_timeout = msecs_to_jiffies(timeout / retry); 207 while (true) { 208 if (!adapter->crq.active) { 209 netdev_err(netdev, "Device down!\n"); 210 return -ENODEV; 211 } 212 if (!retry--) 213 break; 214 if (wait_for_completion_timeout(comp_done, div_timeout)) 215 return 0; 216 } 217 netdev_err(netdev, "Operation timed out.\n"); 218 return -ETIMEDOUT; 219 } 220 221 /** 222 * reuse_ltb() - Check if a long term buffer can be reused 223 * @ltb: The long term buffer to be checked 224 * @size: The size of the long term buffer. 225 * 226 * An LTB can be reused unless its size has changed. 227 * 228 * Return: Return true if the LTB can be reused, false otherwise. 229 */ 230 static bool reuse_ltb(struct ibmvnic_long_term_buff *ltb, int size) 231 { 232 return (ltb->buff && ltb->size == size); 233 } 234 235 /** 236 * alloc_long_term_buff() - Allocate a long term buffer (LTB) 237 * 238 * @adapter: ibmvnic adapter associated to the LTB 239 * @ltb: container object for the LTB 240 * @size: size of the LTB 241 * 242 * Allocate an LTB of the specified size and notify VIOS. 243 * 244 * If the given @ltb already has the correct size, reuse it. Otherwise if 245 * its non-NULL, free it. Then allocate a new one of the correct size. 246 * Notify the VIOS either way since we may now be working with a new VIOS. 247 * 248 * Allocating larger chunks of memory during resets, specially LPM or under 249 * low memory situations can cause resets to fail/timeout and for LPAR to 250 * lose connectivity. So hold onto the LTB even if we fail to communicate 251 * with the VIOS and reuse it on next open. Free LTB when adapter is closed. 252 * 253 * Return: 0 if we were able to allocate the LTB and notify the VIOS and 254 * a negative value otherwise. 255 */ 256 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter, 257 struct ibmvnic_long_term_buff *ltb, int size) 258 { 259 struct device *dev = &adapter->vdev->dev; 260 u64 prev = 0; 261 int rc; 262 263 if (!reuse_ltb(ltb, size)) { 264 dev_dbg(dev, 265 "LTB size changed from 0x%llx to 0x%x, reallocating\n", 266 ltb->size, size); 267 prev = ltb->size; 268 free_long_term_buff(adapter, ltb); 269 } 270 271 if (ltb->buff) { 272 dev_dbg(dev, "Reusing LTB [map %d, size 0x%llx]\n", 273 ltb->map_id, ltb->size); 274 } else { 275 ltb->buff = dma_alloc_coherent(dev, size, <b->addr, 276 GFP_KERNEL); 277 if (!ltb->buff) { 278 dev_err(dev, "Couldn't alloc long term buffer\n"); 279 return -ENOMEM; 280 } 281 ltb->size = size; 282 283 ltb->map_id = find_first_zero_bit(adapter->map_ids, 284 MAX_MAP_ID); 285 bitmap_set(adapter->map_ids, ltb->map_id, 1); 286 287 dev_dbg(dev, 288 "Allocated new LTB [map %d, size 0x%llx was 0x%llx]\n", 289 ltb->map_id, ltb->size, prev); 290 } 291 292 /* Ensure ltb is zeroed - specially when reusing it. */ 293 memset(ltb->buff, 0, ltb->size); 294 295 mutex_lock(&adapter->fw_lock); 296 adapter->fw_done_rc = 0; 297 reinit_completion(&adapter->fw_done); 298 299 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id); 300 if (rc) { 301 dev_err(dev, "send_request_map failed, rc = %d\n", rc); 302 goto out; 303 } 304 305 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000); 306 if (rc) { 307 dev_err(dev, "LTB map request aborted or timed out, rc = %d\n", 308 rc); 309 goto out; 310 } 311 312 if (adapter->fw_done_rc) { 313 dev_err(dev, "Couldn't map LTB, rc = %d\n", 314 adapter->fw_done_rc); 315 rc = -EIO; 316 goto out; 317 } 318 rc = 0; 319 out: 320 /* don't free LTB on communication error - see function header */ 321 mutex_unlock(&adapter->fw_lock); 322 return rc; 323 } 324 325 static void free_long_term_buff(struct ibmvnic_adapter *adapter, 326 struct ibmvnic_long_term_buff *ltb) 327 { 328 struct device *dev = &adapter->vdev->dev; 329 330 if (!ltb->buff) 331 return; 332 333 /* VIOS automatically unmaps the long term buffer at remote 334 * end for the following resets: 335 * FAILOVER, MOBILITY, TIMEOUT. 336 */ 337 if (adapter->reset_reason != VNIC_RESET_FAILOVER && 338 adapter->reset_reason != VNIC_RESET_MOBILITY && 339 adapter->reset_reason != VNIC_RESET_TIMEOUT) 340 send_request_unmap(adapter, ltb->map_id); 341 342 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr); 343 344 ltb->buff = NULL; 345 /* mark this map_id free */ 346 bitmap_clear(adapter->map_ids, ltb->map_id, 1); 347 ltb->map_id = 0; 348 } 349 350 /** 351 * free_ltb_set - free the given set of long term buffers (LTBS) 352 * @adapter: The ibmvnic adapter containing this ltb set 353 * @ltb_set: The ltb_set to be freed 354 * 355 * Free the set of LTBs in the given set. 356 */ 357 358 static void free_ltb_set(struct ibmvnic_adapter *adapter, 359 struct ibmvnic_ltb_set *ltb_set) 360 { 361 int i; 362 363 for (i = 0; i < ltb_set->num_ltbs; i++) 364 free_long_term_buff(adapter, <b_set->ltbs[i]); 365 366 kfree(ltb_set->ltbs); 367 ltb_set->ltbs = NULL; 368 ltb_set->num_ltbs = 0; 369 } 370 371 /** 372 * alloc_ltb_set() - Allocate a set of long term buffers (LTBs) 373 * 374 * @adapter: ibmvnic adapter associated to the LTB 375 * @ltb_set: container object for the set of LTBs 376 * @num_buffs: Number of buffers in the LTB 377 * @buff_size: Size of each buffer in the LTB 378 * 379 * Allocate a set of LTBs to accommodate @num_buffs buffers of @buff_size 380 * each. We currently cap size each LTB to IBMVNIC_ONE_LTB_SIZE. If the 381 * new set of LTBs have fewer LTBs than the old set, free the excess LTBs. 382 * If new set needs more than in old set, allocate the remaining ones. 383 * Try and reuse as many LTBs as possible and avoid reallocation. 384 * 385 * Any changes to this allocation strategy must be reflected in 386 * map_rxpool_buff_to_ltb() and map_txpool_buff_to_ltb(). 387 */ 388 static int alloc_ltb_set(struct ibmvnic_adapter *adapter, 389 struct ibmvnic_ltb_set *ltb_set, int num_buffs, 390 int buff_size) 391 { 392 struct device *dev = &adapter->vdev->dev; 393 struct ibmvnic_ltb_set old_set; 394 struct ibmvnic_ltb_set new_set; 395 int rem_size; 396 int tot_size; /* size of all ltbs */ 397 int ltb_size; /* size of one ltb */ 398 int nltbs; 399 int rc; 400 int n; 401 int i; 402 403 dev_dbg(dev, "%s() num_buffs %d, buff_size %d\n", __func__, num_buffs, 404 buff_size); 405 406 ltb_size = rounddown(IBMVNIC_ONE_LTB_SIZE, buff_size); 407 tot_size = num_buffs * buff_size; 408 409 if (ltb_size > tot_size) 410 ltb_size = tot_size; 411 412 nltbs = tot_size / ltb_size; 413 if (tot_size % ltb_size) 414 nltbs++; 415 416 old_set = *ltb_set; 417 418 if (old_set.num_ltbs == nltbs) { 419 new_set = old_set; 420 } else { 421 int tmp = nltbs * sizeof(struct ibmvnic_long_term_buff); 422 423 new_set.ltbs = kzalloc(tmp, GFP_KERNEL); 424 if (!new_set.ltbs) 425 return -ENOMEM; 426 427 new_set.num_ltbs = nltbs; 428 429 /* Free any excess ltbs in old set */ 430 for (i = new_set.num_ltbs; i < old_set.num_ltbs; i++) 431 free_long_term_buff(adapter, &old_set.ltbs[i]); 432 433 /* Copy remaining ltbs to new set. All LTBs except the 434 * last one are of the same size. alloc_long_term_buff() 435 * will realloc if the size changes. 436 */ 437 n = min(old_set.num_ltbs, new_set.num_ltbs); 438 for (i = 0; i < n; i++) 439 new_set.ltbs[i] = old_set.ltbs[i]; 440 441 /* Any additional ltbs in new set will have NULL ltbs for 442 * now and will be allocated in alloc_long_term_buff(). 443 */ 444 445 /* We no longer need the old_set so free it. Note that we 446 * may have reused some ltbs from old set and freed excess 447 * ltbs above. So we only need to free the container now 448 * not the LTBs themselves. (i.e. dont free_ltb_set()!) 449 */ 450 kfree(old_set.ltbs); 451 old_set.ltbs = NULL; 452 old_set.num_ltbs = 0; 453 454 /* Install the new set. If allocations fail below, we will 455 * retry later and know what size LTBs we need. 456 */ 457 *ltb_set = new_set; 458 } 459 460 i = 0; 461 rem_size = tot_size; 462 while (rem_size) { 463 if (ltb_size > rem_size) 464 ltb_size = rem_size; 465 466 rem_size -= ltb_size; 467 468 rc = alloc_long_term_buff(adapter, &new_set.ltbs[i], ltb_size); 469 if (rc) 470 goto out; 471 i++; 472 } 473 474 WARN_ON(i != new_set.num_ltbs); 475 476 return 0; 477 out: 478 /* We may have allocated one/more LTBs before failing and we 479 * want to try and reuse on next reset. So don't free ltb set. 480 */ 481 return rc; 482 } 483 484 /** 485 * map_rxpool_buf_to_ltb - Map given rxpool buffer to offset in an LTB. 486 * @rxpool: The receive buffer pool containing buffer 487 * @bufidx: Index of buffer in rxpool 488 * @ltbp: (Output) pointer to the long term buffer containing the buffer 489 * @offset: (Output) offset of buffer in the LTB from @ltbp 490 * 491 * Map the given buffer identified by [rxpool, bufidx] to an LTB in the 492 * pool and its corresponding offset. Assume for now that each LTB is of 493 * different size but could possibly be optimized based on the allocation 494 * strategy in alloc_ltb_set(). 495 */ 496 static void map_rxpool_buf_to_ltb(struct ibmvnic_rx_pool *rxpool, 497 unsigned int bufidx, 498 struct ibmvnic_long_term_buff **ltbp, 499 unsigned int *offset) 500 { 501 struct ibmvnic_long_term_buff *ltb; 502 int nbufs; /* # of buffers in one ltb */ 503 int i; 504 505 WARN_ON(bufidx >= rxpool->size); 506 507 for (i = 0; i < rxpool->ltb_set.num_ltbs; i++) { 508 ltb = &rxpool->ltb_set.ltbs[i]; 509 nbufs = ltb->size / rxpool->buff_size; 510 if (bufidx < nbufs) 511 break; 512 bufidx -= nbufs; 513 } 514 515 *ltbp = ltb; 516 *offset = bufidx * rxpool->buff_size; 517 } 518 519 /** 520 * map_txpool_buf_to_ltb - Map given txpool buffer to offset in an LTB. 521 * @txpool: The transmit buffer pool containing buffer 522 * @bufidx: Index of buffer in txpool 523 * @ltbp: (Output) pointer to the long term buffer (LTB) containing the buffer 524 * @offset: (Output) offset of buffer in the LTB from @ltbp 525 * 526 * Map the given buffer identified by [txpool, bufidx] to an LTB in the 527 * pool and its corresponding offset. 528 */ 529 static void map_txpool_buf_to_ltb(struct ibmvnic_tx_pool *txpool, 530 unsigned int bufidx, 531 struct ibmvnic_long_term_buff **ltbp, 532 unsigned int *offset) 533 { 534 struct ibmvnic_long_term_buff *ltb; 535 int nbufs; /* # of buffers in one ltb */ 536 int i; 537 538 WARN_ON_ONCE(bufidx >= txpool->num_buffers); 539 540 for (i = 0; i < txpool->ltb_set.num_ltbs; i++) { 541 ltb = &txpool->ltb_set.ltbs[i]; 542 nbufs = ltb->size / txpool->buf_size; 543 if (bufidx < nbufs) 544 break; 545 bufidx -= nbufs; 546 } 547 548 *ltbp = ltb; 549 *offset = bufidx * txpool->buf_size; 550 } 551 552 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter) 553 { 554 int i; 555 556 for (i = 0; i < adapter->num_active_rx_pools; i++) 557 adapter->rx_pool[i].active = 0; 558 } 559 560 static void replenish_rx_pool(struct ibmvnic_adapter *adapter, 561 struct ibmvnic_rx_pool *pool) 562 { 563 int count = pool->size - atomic_read(&pool->available); 564 u64 handle = adapter->rx_scrq[pool->index]->handle; 565 struct device *dev = &adapter->vdev->dev; 566 struct ibmvnic_ind_xmit_queue *ind_bufp; 567 struct ibmvnic_sub_crq_queue *rx_scrq; 568 struct ibmvnic_long_term_buff *ltb; 569 union sub_crq *sub_crq; 570 int buffers_added = 0; 571 unsigned long lpar_rc; 572 struct sk_buff *skb; 573 unsigned int offset; 574 dma_addr_t dma_addr; 575 unsigned char *dst; 576 int shift = 0; 577 int bufidx; 578 int i; 579 580 if (!pool->active) 581 return; 582 583 rx_scrq = adapter->rx_scrq[pool->index]; 584 ind_bufp = &rx_scrq->ind_buf; 585 586 /* netdev_skb_alloc() could have failed after we saved a few skbs 587 * in the indir_buf and we would not have sent them to VIOS yet. 588 * To account for them, start the loop at ind_bufp->index rather 589 * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will 590 * be 0. 591 */ 592 for (i = ind_bufp->index; i < count; ++i) { 593 bufidx = pool->free_map[pool->next_free]; 594 595 /* We maybe reusing the skb from earlier resets. Allocate 596 * only if necessary. But since the LTB may have changed 597 * during reset (see init_rx_pools()), update LTB below 598 * even if reusing skb. 599 */ 600 skb = pool->rx_buff[bufidx].skb; 601 if (!skb) { 602 skb = netdev_alloc_skb(adapter->netdev, 603 pool->buff_size); 604 if (!skb) { 605 dev_err(dev, "Couldn't replenish rx buff\n"); 606 adapter->replenish_no_mem++; 607 break; 608 } 609 } 610 611 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP; 612 pool->next_free = (pool->next_free + 1) % pool->size; 613 614 /* Copy the skb to the long term mapped DMA buffer */ 615 map_rxpool_buf_to_ltb(pool, bufidx, <b, &offset); 616 dst = ltb->buff + offset; 617 memset(dst, 0, pool->buff_size); 618 dma_addr = ltb->addr + offset; 619 620 /* add the skb to an rx_buff in the pool */ 621 pool->rx_buff[bufidx].data = dst; 622 pool->rx_buff[bufidx].dma = dma_addr; 623 pool->rx_buff[bufidx].skb = skb; 624 pool->rx_buff[bufidx].pool_index = pool->index; 625 pool->rx_buff[bufidx].size = pool->buff_size; 626 627 /* queue the rx_buff for the next send_subcrq_indirect */ 628 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++]; 629 memset(sub_crq, 0, sizeof(*sub_crq)); 630 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD; 631 sub_crq->rx_add.correlator = 632 cpu_to_be64((u64)&pool->rx_buff[bufidx]); 633 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr); 634 sub_crq->rx_add.map_id = ltb->map_id; 635 636 /* The length field of the sCRQ is defined to be 24 bits so the 637 * buffer size needs to be left shifted by a byte before it is 638 * converted to big endian to prevent the last byte from being 639 * truncated. 640 */ 641 #ifdef __LITTLE_ENDIAN__ 642 shift = 8; 643 #endif 644 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift); 645 646 /* if send_subcrq_indirect queue is full, flush to VIOS */ 647 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS || 648 i == count - 1) { 649 lpar_rc = 650 send_subcrq_indirect(adapter, handle, 651 (u64)ind_bufp->indir_dma, 652 (u64)ind_bufp->index); 653 if (lpar_rc != H_SUCCESS) 654 goto failure; 655 buffers_added += ind_bufp->index; 656 adapter->replenish_add_buff_success += ind_bufp->index; 657 ind_bufp->index = 0; 658 } 659 } 660 atomic_add(buffers_added, &pool->available); 661 return; 662 663 failure: 664 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED) 665 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n"); 666 for (i = ind_bufp->index - 1; i >= 0; --i) { 667 struct ibmvnic_rx_buff *rx_buff; 668 669 pool->next_free = pool->next_free == 0 ? 670 pool->size - 1 : pool->next_free - 1; 671 sub_crq = &ind_bufp->indir_arr[i]; 672 rx_buff = (struct ibmvnic_rx_buff *) 673 be64_to_cpu(sub_crq->rx_add.correlator); 674 bufidx = (int)(rx_buff - pool->rx_buff); 675 pool->free_map[pool->next_free] = bufidx; 676 dev_kfree_skb_any(pool->rx_buff[bufidx].skb); 677 pool->rx_buff[bufidx].skb = NULL; 678 } 679 adapter->replenish_add_buff_failure += ind_bufp->index; 680 atomic_add(buffers_added, &pool->available); 681 ind_bufp->index = 0; 682 if (lpar_rc == H_CLOSED || adapter->failover_pending) { 683 /* Disable buffer pool replenishment and report carrier off if 684 * queue is closed or pending failover. 685 * Firmware guarantees that a signal will be sent to the 686 * driver, triggering a reset. 687 */ 688 deactivate_rx_pools(adapter); 689 netif_carrier_off(adapter->netdev); 690 } 691 } 692 693 static void replenish_pools(struct ibmvnic_adapter *adapter) 694 { 695 int i; 696 697 adapter->replenish_task_cycles++; 698 for (i = 0; i < adapter->num_active_rx_pools; i++) { 699 if (adapter->rx_pool[i].active) 700 replenish_rx_pool(adapter, &adapter->rx_pool[i]); 701 } 702 703 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i); 704 } 705 706 static void release_stats_buffers(struct ibmvnic_adapter *adapter) 707 { 708 kfree(adapter->tx_stats_buffers); 709 kfree(adapter->rx_stats_buffers); 710 adapter->tx_stats_buffers = NULL; 711 adapter->rx_stats_buffers = NULL; 712 } 713 714 static int init_stats_buffers(struct ibmvnic_adapter *adapter) 715 { 716 adapter->tx_stats_buffers = 717 kcalloc(IBMVNIC_MAX_QUEUES, 718 sizeof(struct ibmvnic_tx_queue_stats), 719 GFP_KERNEL); 720 if (!adapter->tx_stats_buffers) 721 return -ENOMEM; 722 723 adapter->rx_stats_buffers = 724 kcalloc(IBMVNIC_MAX_QUEUES, 725 sizeof(struct ibmvnic_rx_queue_stats), 726 GFP_KERNEL); 727 if (!adapter->rx_stats_buffers) 728 return -ENOMEM; 729 730 return 0; 731 } 732 733 static void release_stats_token(struct ibmvnic_adapter *adapter) 734 { 735 struct device *dev = &adapter->vdev->dev; 736 737 if (!adapter->stats_token) 738 return; 739 740 dma_unmap_single(dev, adapter->stats_token, 741 sizeof(struct ibmvnic_statistics), 742 DMA_FROM_DEVICE); 743 adapter->stats_token = 0; 744 } 745 746 static int init_stats_token(struct ibmvnic_adapter *adapter) 747 { 748 struct device *dev = &adapter->vdev->dev; 749 dma_addr_t stok; 750 int rc; 751 752 stok = dma_map_single(dev, &adapter->stats, 753 sizeof(struct ibmvnic_statistics), 754 DMA_FROM_DEVICE); 755 rc = dma_mapping_error(dev, stok); 756 if (rc) { 757 dev_err(dev, "Couldn't map stats buffer, rc = %d\n", rc); 758 return rc; 759 } 760 761 adapter->stats_token = stok; 762 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok); 763 return 0; 764 } 765 766 /** 767 * release_rx_pools() - Release any rx pools attached to @adapter. 768 * @adapter: ibmvnic adapter 769 * 770 * Safe to call this multiple times - even if no pools are attached. 771 */ 772 static void release_rx_pools(struct ibmvnic_adapter *adapter) 773 { 774 struct ibmvnic_rx_pool *rx_pool; 775 int i, j; 776 777 if (!adapter->rx_pool) 778 return; 779 780 for (i = 0; i < adapter->num_active_rx_pools; i++) { 781 rx_pool = &adapter->rx_pool[i]; 782 783 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i); 784 785 kfree(rx_pool->free_map); 786 787 free_ltb_set(adapter, &rx_pool->ltb_set); 788 789 if (!rx_pool->rx_buff) 790 continue; 791 792 for (j = 0; j < rx_pool->size; j++) { 793 if (rx_pool->rx_buff[j].skb) { 794 dev_kfree_skb_any(rx_pool->rx_buff[j].skb); 795 rx_pool->rx_buff[j].skb = NULL; 796 } 797 } 798 799 kfree(rx_pool->rx_buff); 800 } 801 802 kfree(adapter->rx_pool); 803 adapter->rx_pool = NULL; 804 adapter->num_active_rx_pools = 0; 805 adapter->prev_rx_pool_size = 0; 806 } 807 808 /** 809 * reuse_rx_pools() - Check if the existing rx pools can be reused. 810 * @adapter: ibmvnic adapter 811 * 812 * Check if the existing rx pools in the adapter can be reused. The 813 * pools can be reused if the pool parameters (number of pools, 814 * number of buffers in the pool and size of each buffer) have not 815 * changed. 816 * 817 * NOTE: This assumes that all pools have the same number of buffers 818 * which is the case currently. If that changes, we must fix this. 819 * 820 * Return: true if the rx pools can be reused, false otherwise. 821 */ 822 static bool reuse_rx_pools(struct ibmvnic_adapter *adapter) 823 { 824 u64 old_num_pools, new_num_pools; 825 u64 old_pool_size, new_pool_size; 826 u64 old_buff_size, new_buff_size; 827 828 if (!adapter->rx_pool) 829 return false; 830 831 old_num_pools = adapter->num_active_rx_pools; 832 new_num_pools = adapter->req_rx_queues; 833 834 old_pool_size = adapter->prev_rx_pool_size; 835 new_pool_size = adapter->req_rx_add_entries_per_subcrq; 836 837 old_buff_size = adapter->prev_rx_buf_sz; 838 new_buff_size = adapter->cur_rx_buf_sz; 839 840 if (old_buff_size != new_buff_size || 841 old_num_pools != new_num_pools || 842 old_pool_size != new_pool_size) 843 return false; 844 845 return true; 846 } 847 848 /** 849 * init_rx_pools(): Initialize the set of receiver pools in the adapter. 850 * @netdev: net device associated with the vnic interface 851 * 852 * Initialize the set of receiver pools in the ibmvnic adapter associated 853 * with the net_device @netdev. If possible, reuse the existing rx pools. 854 * Otherwise free any existing pools and allocate a new set of pools 855 * before initializing them. 856 * 857 * Return: 0 on success and negative value on error. 858 */ 859 static int init_rx_pools(struct net_device *netdev) 860 { 861 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 862 struct device *dev = &adapter->vdev->dev; 863 struct ibmvnic_rx_pool *rx_pool; 864 u64 num_pools; 865 u64 pool_size; /* # of buffers in one pool */ 866 u64 buff_size; 867 int i, j, rc; 868 869 pool_size = adapter->req_rx_add_entries_per_subcrq; 870 num_pools = adapter->req_rx_queues; 871 buff_size = adapter->cur_rx_buf_sz; 872 873 if (reuse_rx_pools(adapter)) { 874 dev_dbg(dev, "Reusing rx pools\n"); 875 goto update_ltb; 876 } 877 878 /* Allocate/populate the pools. */ 879 release_rx_pools(adapter); 880 881 adapter->rx_pool = kcalloc(num_pools, 882 sizeof(struct ibmvnic_rx_pool), 883 GFP_KERNEL); 884 if (!adapter->rx_pool) { 885 dev_err(dev, "Failed to allocate rx pools\n"); 886 return -ENOMEM; 887 } 888 889 /* Set num_active_rx_pools early. If we fail below after partial 890 * allocation, release_rx_pools() will know how many to look for. 891 */ 892 adapter->num_active_rx_pools = num_pools; 893 894 for (i = 0; i < num_pools; i++) { 895 rx_pool = &adapter->rx_pool[i]; 896 897 netdev_dbg(adapter->netdev, 898 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n", 899 i, pool_size, buff_size); 900 901 rx_pool->size = pool_size; 902 rx_pool->index = i; 903 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES); 904 905 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int), 906 GFP_KERNEL); 907 if (!rx_pool->free_map) { 908 dev_err(dev, "Couldn't alloc free_map %d\n", i); 909 rc = -ENOMEM; 910 goto out_release; 911 } 912 913 rx_pool->rx_buff = kcalloc(rx_pool->size, 914 sizeof(struct ibmvnic_rx_buff), 915 GFP_KERNEL); 916 if (!rx_pool->rx_buff) { 917 dev_err(dev, "Couldn't alloc rx buffers\n"); 918 rc = -ENOMEM; 919 goto out_release; 920 } 921 } 922 923 adapter->prev_rx_pool_size = pool_size; 924 adapter->prev_rx_buf_sz = adapter->cur_rx_buf_sz; 925 926 update_ltb: 927 for (i = 0; i < num_pools; i++) { 928 rx_pool = &adapter->rx_pool[i]; 929 dev_dbg(dev, "Updating LTB for rx pool %d [%d, %d]\n", 930 i, rx_pool->size, rx_pool->buff_size); 931 932 rc = alloc_ltb_set(adapter, &rx_pool->ltb_set, 933 rx_pool->size, rx_pool->buff_size); 934 if (rc) 935 goto out; 936 937 for (j = 0; j < rx_pool->size; ++j) { 938 struct ibmvnic_rx_buff *rx_buff; 939 940 rx_pool->free_map[j] = j; 941 942 /* NOTE: Don't clear rx_buff->skb here - will leak 943 * memory! replenish_rx_pool() will reuse skbs or 944 * allocate as necessary. 945 */ 946 rx_buff = &rx_pool->rx_buff[j]; 947 rx_buff->dma = 0; 948 rx_buff->data = 0; 949 rx_buff->size = 0; 950 rx_buff->pool_index = 0; 951 } 952 953 /* Mark pool "empty" so replenish_rx_pools() will 954 * update the LTB info for each buffer 955 */ 956 atomic_set(&rx_pool->available, 0); 957 rx_pool->next_alloc = 0; 958 rx_pool->next_free = 0; 959 /* replenish_rx_pool() may have called deactivate_rx_pools() 960 * on failover. Ensure pool is active now. 961 */ 962 rx_pool->active = 1; 963 } 964 return 0; 965 out_release: 966 release_rx_pools(adapter); 967 out: 968 /* We failed to allocate one or more LTBs or map them on the VIOS. 969 * Hold onto the pools and any LTBs that we did allocate/map. 970 */ 971 return rc; 972 } 973 974 static void release_vpd_data(struct ibmvnic_adapter *adapter) 975 { 976 if (!adapter->vpd) 977 return; 978 979 kfree(adapter->vpd->buff); 980 kfree(adapter->vpd); 981 982 adapter->vpd = NULL; 983 } 984 985 static void release_one_tx_pool(struct ibmvnic_adapter *adapter, 986 struct ibmvnic_tx_pool *tx_pool) 987 { 988 kfree(tx_pool->tx_buff); 989 kfree(tx_pool->free_map); 990 free_ltb_set(adapter, &tx_pool->ltb_set); 991 } 992 993 /** 994 * release_tx_pools() - Release any tx pools attached to @adapter. 995 * @adapter: ibmvnic adapter 996 * 997 * Safe to call this multiple times - even if no pools are attached. 998 */ 999 static void release_tx_pools(struct ibmvnic_adapter *adapter) 1000 { 1001 int i; 1002 1003 /* init_tx_pools() ensures that ->tx_pool and ->tso_pool are 1004 * both NULL or both non-NULL. So we only need to check one. 1005 */ 1006 if (!adapter->tx_pool) 1007 return; 1008 1009 for (i = 0; i < adapter->num_active_tx_pools; i++) { 1010 release_one_tx_pool(adapter, &adapter->tx_pool[i]); 1011 release_one_tx_pool(adapter, &adapter->tso_pool[i]); 1012 } 1013 1014 kfree(adapter->tx_pool); 1015 adapter->tx_pool = NULL; 1016 kfree(adapter->tso_pool); 1017 adapter->tso_pool = NULL; 1018 adapter->num_active_tx_pools = 0; 1019 adapter->prev_tx_pool_size = 0; 1020 } 1021 1022 static int init_one_tx_pool(struct net_device *netdev, 1023 struct ibmvnic_tx_pool *tx_pool, 1024 int pool_size, int buf_size) 1025 { 1026 int i; 1027 1028 tx_pool->tx_buff = kcalloc(pool_size, 1029 sizeof(struct ibmvnic_tx_buff), 1030 GFP_KERNEL); 1031 if (!tx_pool->tx_buff) 1032 return -ENOMEM; 1033 1034 tx_pool->free_map = kcalloc(pool_size, sizeof(int), GFP_KERNEL); 1035 if (!tx_pool->free_map) { 1036 kfree(tx_pool->tx_buff); 1037 tx_pool->tx_buff = NULL; 1038 return -ENOMEM; 1039 } 1040 1041 for (i = 0; i < pool_size; i++) 1042 tx_pool->free_map[i] = i; 1043 1044 tx_pool->consumer_index = 0; 1045 tx_pool->producer_index = 0; 1046 tx_pool->num_buffers = pool_size; 1047 tx_pool->buf_size = buf_size; 1048 1049 return 0; 1050 } 1051 1052 /** 1053 * reuse_tx_pools() - Check if the existing tx pools can be reused. 1054 * @adapter: ibmvnic adapter 1055 * 1056 * Check if the existing tx pools in the adapter can be reused. The 1057 * pools can be reused if the pool parameters (number of pools, 1058 * number of buffers in the pool and mtu) have not changed. 1059 * 1060 * NOTE: This assumes that all pools have the same number of buffers 1061 * which is the case currently. If that changes, we must fix this. 1062 * 1063 * Return: true if the tx pools can be reused, false otherwise. 1064 */ 1065 static bool reuse_tx_pools(struct ibmvnic_adapter *adapter) 1066 { 1067 u64 old_num_pools, new_num_pools; 1068 u64 old_pool_size, new_pool_size; 1069 u64 old_mtu, new_mtu; 1070 1071 if (!adapter->tx_pool) 1072 return false; 1073 1074 old_num_pools = adapter->num_active_tx_pools; 1075 new_num_pools = adapter->num_active_tx_scrqs; 1076 old_pool_size = adapter->prev_tx_pool_size; 1077 new_pool_size = adapter->req_tx_entries_per_subcrq; 1078 old_mtu = adapter->prev_mtu; 1079 new_mtu = adapter->req_mtu; 1080 1081 if (old_mtu != new_mtu || 1082 old_num_pools != new_num_pools || 1083 old_pool_size != new_pool_size) 1084 return false; 1085 1086 return true; 1087 } 1088 1089 /** 1090 * init_tx_pools(): Initialize the set of transmit pools in the adapter. 1091 * @netdev: net device associated with the vnic interface 1092 * 1093 * Initialize the set of transmit pools in the ibmvnic adapter associated 1094 * with the net_device @netdev. If possible, reuse the existing tx pools. 1095 * Otherwise free any existing pools and allocate a new set of pools 1096 * before initializing them. 1097 * 1098 * Return: 0 on success and negative value on error. 1099 */ 1100 static int init_tx_pools(struct net_device *netdev) 1101 { 1102 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1103 struct device *dev = &adapter->vdev->dev; 1104 int num_pools; 1105 u64 pool_size; /* # of buffers in pool */ 1106 u64 buff_size; 1107 int i, j, rc; 1108 1109 num_pools = adapter->req_tx_queues; 1110 1111 /* We must notify the VIOS about the LTB on all resets - but we only 1112 * need to alloc/populate pools if either the number of buffers or 1113 * size of each buffer in the pool has changed. 1114 */ 1115 if (reuse_tx_pools(adapter)) { 1116 netdev_dbg(netdev, "Reusing tx pools\n"); 1117 goto update_ltb; 1118 } 1119 1120 /* Allocate/populate the pools. */ 1121 release_tx_pools(adapter); 1122 1123 pool_size = adapter->req_tx_entries_per_subcrq; 1124 num_pools = adapter->num_active_tx_scrqs; 1125 1126 adapter->tx_pool = kcalloc(num_pools, 1127 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL); 1128 if (!adapter->tx_pool) 1129 return -ENOMEM; 1130 1131 adapter->tso_pool = kcalloc(num_pools, 1132 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL); 1133 /* To simplify release_tx_pools() ensure that ->tx_pool and 1134 * ->tso_pool are either both NULL or both non-NULL. 1135 */ 1136 if (!adapter->tso_pool) { 1137 kfree(adapter->tx_pool); 1138 adapter->tx_pool = NULL; 1139 return -ENOMEM; 1140 } 1141 1142 /* Set num_active_tx_pools early. If we fail below after partial 1143 * allocation, release_tx_pools() will know how many to look for. 1144 */ 1145 adapter->num_active_tx_pools = num_pools; 1146 1147 buff_size = adapter->req_mtu + VLAN_HLEN; 1148 buff_size = ALIGN(buff_size, L1_CACHE_BYTES); 1149 1150 for (i = 0; i < num_pools; i++) { 1151 dev_dbg(dev, "Init tx pool %d [%llu, %llu]\n", 1152 i, adapter->req_tx_entries_per_subcrq, buff_size); 1153 1154 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i], 1155 pool_size, buff_size); 1156 if (rc) 1157 goto out_release; 1158 1159 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i], 1160 IBMVNIC_TSO_BUFS, 1161 IBMVNIC_TSO_BUF_SZ); 1162 if (rc) 1163 goto out_release; 1164 } 1165 1166 adapter->prev_tx_pool_size = pool_size; 1167 adapter->prev_mtu = adapter->req_mtu; 1168 1169 update_ltb: 1170 /* NOTE: All tx_pools have the same number of buffers (which is 1171 * same as pool_size). All tso_pools have IBMVNIC_TSO_BUFS 1172 * buffers (see calls init_one_tx_pool() for these). 1173 * For consistency, we use tx_pool->num_buffers and 1174 * tso_pool->num_buffers below. 1175 */ 1176 rc = -1; 1177 for (i = 0; i < num_pools; i++) { 1178 struct ibmvnic_tx_pool *tso_pool; 1179 struct ibmvnic_tx_pool *tx_pool; 1180 1181 tx_pool = &adapter->tx_pool[i]; 1182 1183 dev_dbg(dev, "Updating LTB for tx pool %d [%d, %d]\n", 1184 i, tx_pool->num_buffers, tx_pool->buf_size); 1185 1186 rc = alloc_ltb_set(adapter, &tx_pool->ltb_set, 1187 tx_pool->num_buffers, tx_pool->buf_size); 1188 if (rc) 1189 goto out; 1190 1191 tx_pool->consumer_index = 0; 1192 tx_pool->producer_index = 0; 1193 1194 for (j = 0; j < tx_pool->num_buffers; j++) 1195 tx_pool->free_map[j] = j; 1196 1197 tso_pool = &adapter->tso_pool[i]; 1198 1199 dev_dbg(dev, "Updating LTB for tso pool %d [%d, %d]\n", 1200 i, tso_pool->num_buffers, tso_pool->buf_size); 1201 1202 rc = alloc_ltb_set(adapter, &tso_pool->ltb_set, 1203 tso_pool->num_buffers, tso_pool->buf_size); 1204 if (rc) 1205 goto out; 1206 1207 tso_pool->consumer_index = 0; 1208 tso_pool->producer_index = 0; 1209 1210 for (j = 0; j < tso_pool->num_buffers; j++) 1211 tso_pool->free_map[j] = j; 1212 } 1213 1214 return 0; 1215 out_release: 1216 release_tx_pools(adapter); 1217 out: 1218 /* We failed to allocate one or more LTBs or map them on the VIOS. 1219 * Hold onto the pools and any LTBs that we did allocate/map. 1220 */ 1221 return rc; 1222 } 1223 1224 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter) 1225 { 1226 int i; 1227 1228 if (adapter->napi_enabled) 1229 return; 1230 1231 for (i = 0; i < adapter->req_rx_queues; i++) 1232 napi_enable(&adapter->napi[i]); 1233 1234 adapter->napi_enabled = true; 1235 } 1236 1237 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter) 1238 { 1239 int i; 1240 1241 if (!adapter->napi_enabled) 1242 return; 1243 1244 for (i = 0; i < adapter->req_rx_queues; i++) { 1245 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i); 1246 napi_disable(&adapter->napi[i]); 1247 } 1248 1249 adapter->napi_enabled = false; 1250 } 1251 1252 static int init_napi(struct ibmvnic_adapter *adapter) 1253 { 1254 int i; 1255 1256 adapter->napi = kcalloc(adapter->req_rx_queues, 1257 sizeof(struct napi_struct), GFP_KERNEL); 1258 if (!adapter->napi) 1259 return -ENOMEM; 1260 1261 for (i = 0; i < adapter->req_rx_queues; i++) { 1262 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i); 1263 netif_napi_add(adapter->netdev, &adapter->napi[i], 1264 ibmvnic_poll, NAPI_POLL_WEIGHT); 1265 } 1266 1267 adapter->num_active_rx_napi = adapter->req_rx_queues; 1268 return 0; 1269 } 1270 1271 static void release_napi(struct ibmvnic_adapter *adapter) 1272 { 1273 int i; 1274 1275 if (!adapter->napi) 1276 return; 1277 1278 for (i = 0; i < adapter->num_active_rx_napi; i++) { 1279 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i); 1280 netif_napi_del(&adapter->napi[i]); 1281 } 1282 1283 kfree(adapter->napi); 1284 adapter->napi = NULL; 1285 adapter->num_active_rx_napi = 0; 1286 adapter->napi_enabled = false; 1287 } 1288 1289 static const char *adapter_state_to_string(enum vnic_state state) 1290 { 1291 switch (state) { 1292 case VNIC_PROBING: 1293 return "PROBING"; 1294 case VNIC_PROBED: 1295 return "PROBED"; 1296 case VNIC_OPENING: 1297 return "OPENING"; 1298 case VNIC_OPEN: 1299 return "OPEN"; 1300 case VNIC_CLOSING: 1301 return "CLOSING"; 1302 case VNIC_CLOSED: 1303 return "CLOSED"; 1304 case VNIC_REMOVING: 1305 return "REMOVING"; 1306 case VNIC_REMOVED: 1307 return "REMOVED"; 1308 case VNIC_DOWN: 1309 return "DOWN"; 1310 } 1311 return "UNKNOWN"; 1312 } 1313 1314 static int ibmvnic_login(struct net_device *netdev) 1315 { 1316 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1317 unsigned long timeout = msecs_to_jiffies(20000); 1318 int retry_count = 0; 1319 int retries = 10; 1320 bool retry; 1321 int rc; 1322 1323 do { 1324 retry = false; 1325 if (retry_count > retries) { 1326 netdev_warn(netdev, "Login attempts exceeded\n"); 1327 return -EACCES; 1328 } 1329 1330 adapter->init_done_rc = 0; 1331 reinit_completion(&adapter->init_done); 1332 rc = send_login(adapter); 1333 if (rc) 1334 return rc; 1335 1336 if (!wait_for_completion_timeout(&adapter->init_done, 1337 timeout)) { 1338 netdev_warn(netdev, "Login timed out, retrying...\n"); 1339 retry = true; 1340 adapter->init_done_rc = 0; 1341 retry_count++; 1342 continue; 1343 } 1344 1345 if (adapter->init_done_rc == ABORTED) { 1346 netdev_warn(netdev, "Login aborted, retrying...\n"); 1347 retry = true; 1348 adapter->init_done_rc = 0; 1349 retry_count++; 1350 /* FW or device may be busy, so 1351 * wait a bit before retrying login 1352 */ 1353 msleep(500); 1354 } else if (adapter->init_done_rc == PARTIALSUCCESS) { 1355 retry_count++; 1356 release_sub_crqs(adapter, 1); 1357 1358 retry = true; 1359 netdev_dbg(netdev, 1360 "Received partial success, retrying...\n"); 1361 adapter->init_done_rc = 0; 1362 reinit_completion(&adapter->init_done); 1363 send_query_cap(adapter); 1364 if (!wait_for_completion_timeout(&adapter->init_done, 1365 timeout)) { 1366 netdev_warn(netdev, 1367 "Capabilities query timed out\n"); 1368 return -ETIMEDOUT; 1369 } 1370 1371 rc = init_sub_crqs(adapter); 1372 if (rc) { 1373 netdev_warn(netdev, 1374 "SCRQ initialization failed\n"); 1375 return rc; 1376 } 1377 1378 rc = init_sub_crq_irqs(adapter); 1379 if (rc) { 1380 netdev_warn(netdev, 1381 "SCRQ irq initialization failed\n"); 1382 return rc; 1383 } 1384 } else if (adapter->init_done_rc) { 1385 netdev_warn(netdev, "Adapter login failed, init_done_rc = %d\n", 1386 adapter->init_done_rc); 1387 return -EIO; 1388 } 1389 } while (retry); 1390 1391 __ibmvnic_set_mac(netdev, adapter->mac_addr); 1392 1393 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state)); 1394 return 0; 1395 } 1396 1397 static void release_login_buffer(struct ibmvnic_adapter *adapter) 1398 { 1399 kfree(adapter->login_buf); 1400 adapter->login_buf = NULL; 1401 } 1402 1403 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter) 1404 { 1405 kfree(adapter->login_rsp_buf); 1406 adapter->login_rsp_buf = NULL; 1407 } 1408 1409 static void release_resources(struct ibmvnic_adapter *adapter) 1410 { 1411 release_vpd_data(adapter); 1412 1413 release_napi(adapter); 1414 release_login_buffer(adapter); 1415 release_login_rsp_buffer(adapter); 1416 } 1417 1418 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state) 1419 { 1420 struct net_device *netdev = adapter->netdev; 1421 unsigned long timeout = msecs_to_jiffies(20000); 1422 union ibmvnic_crq crq; 1423 bool resend; 1424 int rc; 1425 1426 netdev_dbg(netdev, "setting link state %d\n", link_state); 1427 1428 memset(&crq, 0, sizeof(crq)); 1429 crq.logical_link_state.first = IBMVNIC_CRQ_CMD; 1430 crq.logical_link_state.cmd = LOGICAL_LINK_STATE; 1431 crq.logical_link_state.link_state = link_state; 1432 1433 do { 1434 resend = false; 1435 1436 reinit_completion(&adapter->init_done); 1437 rc = ibmvnic_send_crq(adapter, &crq); 1438 if (rc) { 1439 netdev_err(netdev, "Failed to set link state\n"); 1440 return rc; 1441 } 1442 1443 if (!wait_for_completion_timeout(&adapter->init_done, 1444 timeout)) { 1445 netdev_err(netdev, "timeout setting link state\n"); 1446 return -ETIMEDOUT; 1447 } 1448 1449 if (adapter->init_done_rc == PARTIALSUCCESS) { 1450 /* Partuial success, delay and re-send */ 1451 mdelay(1000); 1452 resend = true; 1453 } else if (adapter->init_done_rc) { 1454 netdev_warn(netdev, "Unable to set link state, rc=%d\n", 1455 adapter->init_done_rc); 1456 return adapter->init_done_rc; 1457 } 1458 } while (resend); 1459 1460 return 0; 1461 } 1462 1463 static int set_real_num_queues(struct net_device *netdev) 1464 { 1465 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1466 int rc; 1467 1468 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n", 1469 adapter->req_tx_queues, adapter->req_rx_queues); 1470 1471 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues); 1472 if (rc) { 1473 netdev_err(netdev, "failed to set the number of tx queues\n"); 1474 return rc; 1475 } 1476 1477 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues); 1478 if (rc) 1479 netdev_err(netdev, "failed to set the number of rx queues\n"); 1480 1481 return rc; 1482 } 1483 1484 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter) 1485 { 1486 struct device *dev = &adapter->vdev->dev; 1487 union ibmvnic_crq crq; 1488 int len = 0; 1489 int rc; 1490 1491 if (adapter->vpd->buff) 1492 len = adapter->vpd->len; 1493 1494 mutex_lock(&adapter->fw_lock); 1495 adapter->fw_done_rc = 0; 1496 reinit_completion(&adapter->fw_done); 1497 1498 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD; 1499 crq.get_vpd_size.cmd = GET_VPD_SIZE; 1500 rc = ibmvnic_send_crq(adapter, &crq); 1501 if (rc) { 1502 mutex_unlock(&adapter->fw_lock); 1503 return rc; 1504 } 1505 1506 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000); 1507 if (rc) { 1508 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc); 1509 mutex_unlock(&adapter->fw_lock); 1510 return rc; 1511 } 1512 mutex_unlock(&adapter->fw_lock); 1513 1514 if (!adapter->vpd->len) 1515 return -ENODATA; 1516 1517 if (!adapter->vpd->buff) 1518 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL); 1519 else if (adapter->vpd->len != len) 1520 adapter->vpd->buff = 1521 krealloc(adapter->vpd->buff, 1522 adapter->vpd->len, GFP_KERNEL); 1523 1524 if (!adapter->vpd->buff) { 1525 dev_err(dev, "Could allocate VPD buffer\n"); 1526 return -ENOMEM; 1527 } 1528 1529 adapter->vpd->dma_addr = 1530 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len, 1531 DMA_FROM_DEVICE); 1532 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) { 1533 dev_err(dev, "Could not map VPD buffer\n"); 1534 kfree(adapter->vpd->buff); 1535 adapter->vpd->buff = NULL; 1536 return -ENOMEM; 1537 } 1538 1539 mutex_lock(&adapter->fw_lock); 1540 adapter->fw_done_rc = 0; 1541 reinit_completion(&adapter->fw_done); 1542 1543 crq.get_vpd.first = IBMVNIC_CRQ_CMD; 1544 crq.get_vpd.cmd = GET_VPD; 1545 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr); 1546 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len); 1547 rc = ibmvnic_send_crq(adapter, &crq); 1548 if (rc) { 1549 kfree(adapter->vpd->buff); 1550 adapter->vpd->buff = NULL; 1551 mutex_unlock(&adapter->fw_lock); 1552 return rc; 1553 } 1554 1555 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000); 1556 if (rc) { 1557 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc); 1558 kfree(adapter->vpd->buff); 1559 adapter->vpd->buff = NULL; 1560 mutex_unlock(&adapter->fw_lock); 1561 return rc; 1562 } 1563 1564 mutex_unlock(&adapter->fw_lock); 1565 return 0; 1566 } 1567 1568 static int init_resources(struct ibmvnic_adapter *adapter) 1569 { 1570 struct net_device *netdev = adapter->netdev; 1571 int rc; 1572 1573 rc = set_real_num_queues(netdev); 1574 if (rc) 1575 return rc; 1576 1577 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL); 1578 if (!adapter->vpd) 1579 return -ENOMEM; 1580 1581 /* Vital Product Data (VPD) */ 1582 rc = ibmvnic_get_vpd(adapter); 1583 if (rc) { 1584 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n"); 1585 return rc; 1586 } 1587 1588 rc = init_napi(adapter); 1589 if (rc) 1590 return rc; 1591 1592 send_query_map(adapter); 1593 1594 rc = init_rx_pools(netdev); 1595 if (rc) 1596 return rc; 1597 1598 rc = init_tx_pools(netdev); 1599 return rc; 1600 } 1601 1602 static int __ibmvnic_open(struct net_device *netdev) 1603 { 1604 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1605 enum vnic_state prev_state = adapter->state; 1606 int i, rc; 1607 1608 adapter->state = VNIC_OPENING; 1609 replenish_pools(adapter); 1610 ibmvnic_napi_enable(adapter); 1611 1612 /* We're ready to receive frames, enable the sub-crq interrupts and 1613 * set the logical link state to up 1614 */ 1615 for (i = 0; i < adapter->req_rx_queues; i++) { 1616 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i); 1617 if (prev_state == VNIC_CLOSED) 1618 enable_irq(adapter->rx_scrq[i]->irq); 1619 enable_scrq_irq(adapter, adapter->rx_scrq[i]); 1620 } 1621 1622 for (i = 0; i < adapter->req_tx_queues; i++) { 1623 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i); 1624 if (prev_state == VNIC_CLOSED) 1625 enable_irq(adapter->tx_scrq[i]->irq); 1626 enable_scrq_irq(adapter, adapter->tx_scrq[i]); 1627 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i)); 1628 } 1629 1630 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP); 1631 if (rc) { 1632 ibmvnic_napi_disable(adapter); 1633 ibmvnic_disable_irqs(adapter); 1634 return rc; 1635 } 1636 1637 adapter->tx_queues_active = true; 1638 1639 /* Since queues were stopped until now, there shouldn't be any 1640 * one in ibmvnic_complete_tx() or ibmvnic_xmit() so maybe we 1641 * don't need the synchronize_rcu()? Leaving it for consistency 1642 * with setting ->tx_queues_active = false. 1643 */ 1644 synchronize_rcu(); 1645 1646 netif_tx_start_all_queues(netdev); 1647 1648 if (prev_state == VNIC_CLOSED) { 1649 for (i = 0; i < adapter->req_rx_queues; i++) 1650 napi_schedule(&adapter->napi[i]); 1651 } 1652 1653 adapter->state = VNIC_OPEN; 1654 return rc; 1655 } 1656 1657 static int ibmvnic_open(struct net_device *netdev) 1658 { 1659 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1660 int rc; 1661 1662 ASSERT_RTNL(); 1663 1664 /* If device failover is pending or we are about to reset, just set 1665 * device state and return. Device operation will be handled by reset 1666 * routine. 1667 * 1668 * It should be safe to overwrite the adapter->state here. Since 1669 * we hold the rtnl, either the reset has not actually started or 1670 * the rtnl got dropped during the set_link_state() in do_reset(). 1671 * In the former case, no one else is changing the state (again we 1672 * have the rtnl) and in the latter case, do_reset() will detect and 1673 * honor our setting below. 1674 */ 1675 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) { 1676 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n", 1677 adapter_state_to_string(adapter->state), 1678 adapter->failover_pending); 1679 adapter->state = VNIC_OPEN; 1680 rc = 0; 1681 goto out; 1682 } 1683 1684 if (adapter->state != VNIC_CLOSED) { 1685 rc = ibmvnic_login(netdev); 1686 if (rc) 1687 goto out; 1688 1689 rc = init_resources(adapter); 1690 if (rc) { 1691 netdev_err(netdev, "failed to initialize resources\n"); 1692 goto out; 1693 } 1694 } 1695 1696 rc = __ibmvnic_open(netdev); 1697 1698 out: 1699 /* If open failed and there is a pending failover or in-progress reset, 1700 * set device state and return. Device operation will be handled by 1701 * reset routine. See also comments above regarding rtnl. 1702 */ 1703 if (rc && 1704 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) { 1705 adapter->state = VNIC_OPEN; 1706 rc = 0; 1707 } 1708 1709 if (rc) { 1710 release_resources(adapter); 1711 release_rx_pools(adapter); 1712 release_tx_pools(adapter); 1713 } 1714 1715 return rc; 1716 } 1717 1718 static void clean_rx_pools(struct ibmvnic_adapter *adapter) 1719 { 1720 struct ibmvnic_rx_pool *rx_pool; 1721 struct ibmvnic_rx_buff *rx_buff; 1722 u64 rx_entries; 1723 int rx_scrqs; 1724 int i, j; 1725 1726 if (!adapter->rx_pool) 1727 return; 1728 1729 rx_scrqs = adapter->num_active_rx_pools; 1730 rx_entries = adapter->req_rx_add_entries_per_subcrq; 1731 1732 /* Free any remaining skbs in the rx buffer pools */ 1733 for (i = 0; i < rx_scrqs; i++) { 1734 rx_pool = &adapter->rx_pool[i]; 1735 if (!rx_pool || !rx_pool->rx_buff) 1736 continue; 1737 1738 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i); 1739 for (j = 0; j < rx_entries; j++) { 1740 rx_buff = &rx_pool->rx_buff[j]; 1741 if (rx_buff && rx_buff->skb) { 1742 dev_kfree_skb_any(rx_buff->skb); 1743 rx_buff->skb = NULL; 1744 } 1745 } 1746 } 1747 } 1748 1749 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter, 1750 struct ibmvnic_tx_pool *tx_pool) 1751 { 1752 struct ibmvnic_tx_buff *tx_buff; 1753 u64 tx_entries; 1754 int i; 1755 1756 if (!tx_pool || !tx_pool->tx_buff) 1757 return; 1758 1759 tx_entries = tx_pool->num_buffers; 1760 1761 for (i = 0; i < tx_entries; i++) { 1762 tx_buff = &tx_pool->tx_buff[i]; 1763 if (tx_buff && tx_buff->skb) { 1764 dev_kfree_skb_any(tx_buff->skb); 1765 tx_buff->skb = NULL; 1766 } 1767 } 1768 } 1769 1770 static void clean_tx_pools(struct ibmvnic_adapter *adapter) 1771 { 1772 int tx_scrqs; 1773 int i; 1774 1775 if (!adapter->tx_pool || !adapter->tso_pool) 1776 return; 1777 1778 tx_scrqs = adapter->num_active_tx_pools; 1779 1780 /* Free any remaining skbs in the tx buffer pools */ 1781 for (i = 0; i < tx_scrqs; i++) { 1782 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i); 1783 clean_one_tx_pool(adapter, &adapter->tx_pool[i]); 1784 clean_one_tx_pool(adapter, &adapter->tso_pool[i]); 1785 } 1786 } 1787 1788 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter) 1789 { 1790 struct net_device *netdev = adapter->netdev; 1791 int i; 1792 1793 if (adapter->tx_scrq) { 1794 for (i = 0; i < adapter->req_tx_queues; i++) 1795 if (adapter->tx_scrq[i]->irq) { 1796 netdev_dbg(netdev, 1797 "Disabling tx_scrq[%d] irq\n", i); 1798 disable_scrq_irq(adapter, adapter->tx_scrq[i]); 1799 disable_irq(adapter->tx_scrq[i]->irq); 1800 } 1801 } 1802 1803 if (adapter->rx_scrq) { 1804 for (i = 0; i < adapter->req_rx_queues; i++) { 1805 if (adapter->rx_scrq[i]->irq) { 1806 netdev_dbg(netdev, 1807 "Disabling rx_scrq[%d] irq\n", i); 1808 disable_scrq_irq(adapter, adapter->rx_scrq[i]); 1809 disable_irq(adapter->rx_scrq[i]->irq); 1810 } 1811 } 1812 } 1813 } 1814 1815 static void ibmvnic_cleanup(struct net_device *netdev) 1816 { 1817 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1818 1819 /* ensure that transmissions are stopped if called by do_reset */ 1820 1821 adapter->tx_queues_active = false; 1822 1823 /* Ensure complete_tx() and ibmvnic_xmit() see ->tx_queues_active 1824 * update so they don't restart a queue after we stop it below. 1825 */ 1826 synchronize_rcu(); 1827 1828 if (test_bit(0, &adapter->resetting)) 1829 netif_tx_disable(netdev); 1830 else 1831 netif_tx_stop_all_queues(netdev); 1832 1833 ibmvnic_napi_disable(adapter); 1834 ibmvnic_disable_irqs(adapter); 1835 } 1836 1837 static int __ibmvnic_close(struct net_device *netdev) 1838 { 1839 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1840 int rc = 0; 1841 1842 adapter->state = VNIC_CLOSING; 1843 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN); 1844 adapter->state = VNIC_CLOSED; 1845 return rc; 1846 } 1847 1848 static int ibmvnic_close(struct net_device *netdev) 1849 { 1850 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1851 int rc; 1852 1853 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n", 1854 adapter_state_to_string(adapter->state), 1855 adapter->failover_pending, 1856 adapter->force_reset_recovery); 1857 1858 /* If device failover is pending, just set device state and return. 1859 * Device operation will be handled by reset routine. 1860 */ 1861 if (adapter->failover_pending) { 1862 adapter->state = VNIC_CLOSED; 1863 return 0; 1864 } 1865 1866 rc = __ibmvnic_close(netdev); 1867 ibmvnic_cleanup(netdev); 1868 clean_rx_pools(adapter); 1869 clean_tx_pools(adapter); 1870 1871 return rc; 1872 } 1873 1874 /** 1875 * build_hdr_data - creates L2/L3/L4 header data buffer 1876 * @hdr_field: bitfield determining needed headers 1877 * @skb: socket buffer 1878 * @hdr_len: array of header lengths 1879 * @hdr_data: buffer to write the header to 1880 * 1881 * Reads hdr_field to determine which headers are needed by firmware. 1882 * Builds a buffer containing these headers. Saves individual header 1883 * lengths and total buffer length to be used to build descriptors. 1884 */ 1885 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb, 1886 int *hdr_len, u8 *hdr_data) 1887 { 1888 int len = 0; 1889 u8 *hdr; 1890 1891 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb)) 1892 hdr_len[0] = sizeof(struct vlan_ethhdr); 1893 else 1894 hdr_len[0] = sizeof(struct ethhdr); 1895 1896 if (skb->protocol == htons(ETH_P_IP)) { 1897 hdr_len[1] = ip_hdr(skb)->ihl * 4; 1898 if (ip_hdr(skb)->protocol == IPPROTO_TCP) 1899 hdr_len[2] = tcp_hdrlen(skb); 1900 else if (ip_hdr(skb)->protocol == IPPROTO_UDP) 1901 hdr_len[2] = sizeof(struct udphdr); 1902 } else if (skb->protocol == htons(ETH_P_IPV6)) { 1903 hdr_len[1] = sizeof(struct ipv6hdr); 1904 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) 1905 hdr_len[2] = tcp_hdrlen(skb); 1906 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP) 1907 hdr_len[2] = sizeof(struct udphdr); 1908 } else if (skb->protocol == htons(ETH_P_ARP)) { 1909 hdr_len[1] = arp_hdr_len(skb->dev); 1910 hdr_len[2] = 0; 1911 } 1912 1913 memset(hdr_data, 0, 120); 1914 if ((hdr_field >> 6) & 1) { 1915 hdr = skb_mac_header(skb); 1916 memcpy(hdr_data, hdr, hdr_len[0]); 1917 len += hdr_len[0]; 1918 } 1919 1920 if ((hdr_field >> 5) & 1) { 1921 hdr = skb_network_header(skb); 1922 memcpy(hdr_data + len, hdr, hdr_len[1]); 1923 len += hdr_len[1]; 1924 } 1925 1926 if ((hdr_field >> 4) & 1) { 1927 hdr = skb_transport_header(skb); 1928 memcpy(hdr_data + len, hdr, hdr_len[2]); 1929 len += hdr_len[2]; 1930 } 1931 return len; 1932 } 1933 1934 /** 1935 * create_hdr_descs - create header and header extension descriptors 1936 * @hdr_field: bitfield determining needed headers 1937 * @hdr_data: buffer containing header data 1938 * @len: length of data buffer 1939 * @hdr_len: array of individual header lengths 1940 * @scrq_arr: descriptor array 1941 * 1942 * Creates header and, if needed, header extension descriptors and 1943 * places them in a descriptor array, scrq_arr 1944 */ 1945 1946 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len, 1947 union sub_crq *scrq_arr) 1948 { 1949 union sub_crq hdr_desc; 1950 int tmp_len = len; 1951 int num_descs = 0; 1952 u8 *data, *cur; 1953 int tmp; 1954 1955 while (tmp_len > 0) { 1956 cur = hdr_data + len - tmp_len; 1957 1958 memset(&hdr_desc, 0, sizeof(hdr_desc)); 1959 if (cur != hdr_data) { 1960 data = hdr_desc.hdr_ext.data; 1961 tmp = tmp_len > 29 ? 29 : tmp_len; 1962 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD; 1963 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC; 1964 hdr_desc.hdr_ext.len = tmp; 1965 } else { 1966 data = hdr_desc.hdr.data; 1967 tmp = tmp_len > 24 ? 24 : tmp_len; 1968 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD; 1969 hdr_desc.hdr.type = IBMVNIC_HDR_DESC; 1970 hdr_desc.hdr.len = tmp; 1971 hdr_desc.hdr.l2_len = (u8)hdr_len[0]; 1972 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]); 1973 hdr_desc.hdr.l4_len = (u8)hdr_len[2]; 1974 hdr_desc.hdr.flag = hdr_field << 1; 1975 } 1976 memcpy(data, cur, tmp); 1977 tmp_len -= tmp; 1978 *scrq_arr = hdr_desc; 1979 scrq_arr++; 1980 num_descs++; 1981 } 1982 1983 return num_descs; 1984 } 1985 1986 /** 1987 * build_hdr_descs_arr - build a header descriptor array 1988 * @skb: tx socket buffer 1989 * @indir_arr: indirect array 1990 * @num_entries: number of descriptors to be sent 1991 * @hdr_field: bit field determining which headers will be sent 1992 * 1993 * This function will build a TX descriptor array with applicable 1994 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect. 1995 */ 1996 1997 static void build_hdr_descs_arr(struct sk_buff *skb, 1998 union sub_crq *indir_arr, 1999 int *num_entries, u8 hdr_field) 2000 { 2001 int hdr_len[3] = {0, 0, 0}; 2002 u8 hdr_data[140] = {0}; 2003 int tot_len; 2004 2005 tot_len = build_hdr_data(hdr_field, skb, hdr_len, 2006 hdr_data); 2007 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len, 2008 indir_arr + 1); 2009 } 2010 2011 static int ibmvnic_xmit_workarounds(struct sk_buff *skb, 2012 struct net_device *netdev) 2013 { 2014 /* For some backing devices, mishandling of small packets 2015 * can result in a loss of connection or TX stall. Device 2016 * architects recommend that no packet should be smaller 2017 * than the minimum MTU value provided to the driver, so 2018 * pad any packets to that length 2019 */ 2020 if (skb->len < netdev->min_mtu) 2021 return skb_put_padto(skb, netdev->min_mtu); 2022 2023 return 0; 2024 } 2025 2026 static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter, 2027 struct ibmvnic_sub_crq_queue *tx_scrq) 2028 { 2029 struct ibmvnic_ind_xmit_queue *ind_bufp; 2030 struct ibmvnic_tx_buff *tx_buff; 2031 struct ibmvnic_tx_pool *tx_pool; 2032 union sub_crq tx_scrq_entry; 2033 int queue_num; 2034 int entries; 2035 int index; 2036 int i; 2037 2038 ind_bufp = &tx_scrq->ind_buf; 2039 entries = (u64)ind_bufp->index; 2040 queue_num = tx_scrq->pool_index; 2041 2042 for (i = entries - 1; i >= 0; --i) { 2043 tx_scrq_entry = ind_bufp->indir_arr[i]; 2044 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC) 2045 continue; 2046 index = be32_to_cpu(tx_scrq_entry.v1.correlator); 2047 if (index & IBMVNIC_TSO_POOL_MASK) { 2048 tx_pool = &adapter->tso_pool[queue_num]; 2049 index &= ~IBMVNIC_TSO_POOL_MASK; 2050 } else { 2051 tx_pool = &adapter->tx_pool[queue_num]; 2052 } 2053 tx_pool->free_map[tx_pool->consumer_index] = index; 2054 tx_pool->consumer_index = tx_pool->consumer_index == 0 ? 2055 tx_pool->num_buffers - 1 : 2056 tx_pool->consumer_index - 1; 2057 tx_buff = &tx_pool->tx_buff[index]; 2058 adapter->netdev->stats.tx_packets--; 2059 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len; 2060 adapter->tx_stats_buffers[queue_num].packets--; 2061 adapter->tx_stats_buffers[queue_num].bytes -= 2062 tx_buff->skb->len; 2063 dev_kfree_skb_any(tx_buff->skb); 2064 tx_buff->skb = NULL; 2065 adapter->netdev->stats.tx_dropped++; 2066 } 2067 2068 ind_bufp->index = 0; 2069 2070 if (atomic_sub_return(entries, &tx_scrq->used) <= 2071 (adapter->req_tx_entries_per_subcrq / 2) && 2072 __netif_subqueue_stopped(adapter->netdev, queue_num)) { 2073 rcu_read_lock(); 2074 2075 if (adapter->tx_queues_active) { 2076 netif_wake_subqueue(adapter->netdev, queue_num); 2077 netdev_dbg(adapter->netdev, "Started queue %d\n", 2078 queue_num); 2079 } 2080 2081 rcu_read_unlock(); 2082 } 2083 } 2084 2085 static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter, 2086 struct ibmvnic_sub_crq_queue *tx_scrq) 2087 { 2088 struct ibmvnic_ind_xmit_queue *ind_bufp; 2089 u64 dma_addr; 2090 u64 entries; 2091 u64 handle; 2092 int rc; 2093 2094 ind_bufp = &tx_scrq->ind_buf; 2095 dma_addr = (u64)ind_bufp->indir_dma; 2096 entries = (u64)ind_bufp->index; 2097 handle = tx_scrq->handle; 2098 2099 if (!entries) 2100 return 0; 2101 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries); 2102 if (rc) 2103 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq); 2104 else 2105 ind_bufp->index = 0; 2106 return 0; 2107 } 2108 2109 static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev) 2110 { 2111 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2112 int queue_num = skb_get_queue_mapping(skb); 2113 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req; 2114 struct device *dev = &adapter->vdev->dev; 2115 struct ibmvnic_ind_xmit_queue *ind_bufp; 2116 struct ibmvnic_tx_buff *tx_buff = NULL; 2117 struct ibmvnic_sub_crq_queue *tx_scrq; 2118 struct ibmvnic_long_term_buff *ltb; 2119 struct ibmvnic_tx_pool *tx_pool; 2120 unsigned int tx_send_failed = 0; 2121 netdev_tx_t ret = NETDEV_TX_OK; 2122 unsigned int tx_map_failed = 0; 2123 union sub_crq indir_arr[16]; 2124 unsigned int tx_dropped = 0; 2125 unsigned int tx_packets = 0; 2126 unsigned int tx_bytes = 0; 2127 dma_addr_t data_dma_addr; 2128 struct netdev_queue *txq; 2129 unsigned long lpar_rc; 2130 union sub_crq tx_crq; 2131 unsigned int offset; 2132 int num_entries = 1; 2133 unsigned char *dst; 2134 int bufidx = 0; 2135 u8 proto = 0; 2136 2137 /* If a reset is in progress, drop the packet since 2138 * the scrqs may get torn down. Otherwise use the 2139 * rcu to ensure reset waits for us to complete. 2140 */ 2141 rcu_read_lock(); 2142 if (!adapter->tx_queues_active) { 2143 dev_kfree_skb_any(skb); 2144 2145 tx_send_failed++; 2146 tx_dropped++; 2147 ret = NETDEV_TX_OK; 2148 goto out; 2149 } 2150 2151 tx_scrq = adapter->tx_scrq[queue_num]; 2152 txq = netdev_get_tx_queue(netdev, queue_num); 2153 ind_bufp = &tx_scrq->ind_buf; 2154 2155 if (ibmvnic_xmit_workarounds(skb, netdev)) { 2156 tx_dropped++; 2157 tx_send_failed++; 2158 ret = NETDEV_TX_OK; 2159 ibmvnic_tx_scrq_flush(adapter, tx_scrq); 2160 goto out; 2161 } 2162 2163 if (skb_is_gso(skb)) 2164 tx_pool = &adapter->tso_pool[queue_num]; 2165 else 2166 tx_pool = &adapter->tx_pool[queue_num]; 2167 2168 bufidx = tx_pool->free_map[tx_pool->consumer_index]; 2169 2170 if (bufidx == IBMVNIC_INVALID_MAP) { 2171 dev_kfree_skb_any(skb); 2172 tx_send_failed++; 2173 tx_dropped++; 2174 ibmvnic_tx_scrq_flush(adapter, tx_scrq); 2175 ret = NETDEV_TX_OK; 2176 goto out; 2177 } 2178 2179 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP; 2180 2181 map_txpool_buf_to_ltb(tx_pool, bufidx, <b, &offset); 2182 2183 dst = ltb->buff + offset; 2184 memset(dst, 0, tx_pool->buf_size); 2185 data_dma_addr = ltb->addr + offset; 2186 2187 if (skb_shinfo(skb)->nr_frags) { 2188 int cur, i; 2189 2190 /* Copy the head */ 2191 skb_copy_from_linear_data(skb, dst, skb_headlen(skb)); 2192 cur = skb_headlen(skb); 2193 2194 /* Copy the frags */ 2195 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2196 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2197 2198 memcpy(dst + cur, skb_frag_address(frag), 2199 skb_frag_size(frag)); 2200 cur += skb_frag_size(frag); 2201 } 2202 } else { 2203 skb_copy_from_linear_data(skb, dst, skb->len); 2204 } 2205 2206 /* post changes to long_term_buff *dst before VIOS accessing it */ 2207 dma_wmb(); 2208 2209 tx_pool->consumer_index = 2210 (tx_pool->consumer_index + 1) % tx_pool->num_buffers; 2211 2212 tx_buff = &tx_pool->tx_buff[bufidx]; 2213 tx_buff->skb = skb; 2214 tx_buff->index = bufidx; 2215 tx_buff->pool_index = queue_num; 2216 2217 memset(&tx_crq, 0, sizeof(tx_crq)); 2218 tx_crq.v1.first = IBMVNIC_CRQ_CMD; 2219 tx_crq.v1.type = IBMVNIC_TX_DESC; 2220 tx_crq.v1.n_crq_elem = 1; 2221 tx_crq.v1.n_sge = 1; 2222 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED; 2223 2224 if (skb_is_gso(skb)) 2225 tx_crq.v1.correlator = 2226 cpu_to_be32(bufidx | IBMVNIC_TSO_POOL_MASK); 2227 else 2228 tx_crq.v1.correlator = cpu_to_be32(bufidx); 2229 tx_crq.v1.dma_reg = cpu_to_be16(ltb->map_id); 2230 tx_crq.v1.sge_len = cpu_to_be32(skb->len); 2231 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr); 2232 2233 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) { 2234 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT; 2235 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci); 2236 } 2237 2238 if (skb->protocol == htons(ETH_P_IP)) { 2239 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4; 2240 proto = ip_hdr(skb)->protocol; 2241 } else if (skb->protocol == htons(ETH_P_IPV6)) { 2242 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6; 2243 proto = ipv6_hdr(skb)->nexthdr; 2244 } 2245 2246 if (proto == IPPROTO_TCP) 2247 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP; 2248 else if (proto == IPPROTO_UDP) 2249 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP; 2250 2251 if (skb->ip_summed == CHECKSUM_PARTIAL) { 2252 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD; 2253 hdrs += 2; 2254 } 2255 if (skb_is_gso(skb)) { 2256 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO; 2257 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size); 2258 hdrs += 2; 2259 } 2260 2261 if ((*hdrs >> 7) & 1) 2262 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs); 2263 2264 tx_crq.v1.n_crq_elem = num_entries; 2265 tx_buff->num_entries = num_entries; 2266 /* flush buffer if current entry can not fit */ 2267 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) { 2268 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq); 2269 if (lpar_rc != H_SUCCESS) 2270 goto tx_flush_err; 2271 } 2272 2273 indir_arr[0] = tx_crq; 2274 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0], 2275 num_entries * sizeof(struct ibmvnic_generic_scrq)); 2276 ind_bufp->index += num_entries; 2277 if (__netdev_tx_sent_queue(txq, skb->len, 2278 netdev_xmit_more() && 2279 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) { 2280 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq); 2281 if (lpar_rc != H_SUCCESS) 2282 goto tx_err; 2283 } 2284 2285 if (atomic_add_return(num_entries, &tx_scrq->used) 2286 >= adapter->req_tx_entries_per_subcrq) { 2287 netdev_dbg(netdev, "Stopping queue %d\n", queue_num); 2288 netif_stop_subqueue(netdev, queue_num); 2289 } 2290 2291 tx_packets++; 2292 tx_bytes += skb->len; 2293 txq_trans_cond_update(txq); 2294 ret = NETDEV_TX_OK; 2295 goto out; 2296 2297 tx_flush_err: 2298 dev_kfree_skb_any(skb); 2299 tx_buff->skb = NULL; 2300 tx_pool->consumer_index = tx_pool->consumer_index == 0 ? 2301 tx_pool->num_buffers - 1 : 2302 tx_pool->consumer_index - 1; 2303 tx_dropped++; 2304 tx_err: 2305 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER) 2306 dev_err_ratelimited(dev, "tx: send failed\n"); 2307 2308 if (lpar_rc == H_CLOSED || adapter->failover_pending) { 2309 /* Disable TX and report carrier off if queue is closed 2310 * or pending failover. 2311 * Firmware guarantees that a signal will be sent to the 2312 * driver, triggering a reset or some other action. 2313 */ 2314 netif_tx_stop_all_queues(netdev); 2315 netif_carrier_off(netdev); 2316 } 2317 out: 2318 rcu_read_unlock(); 2319 netdev->stats.tx_dropped += tx_dropped; 2320 netdev->stats.tx_bytes += tx_bytes; 2321 netdev->stats.tx_packets += tx_packets; 2322 adapter->tx_send_failed += tx_send_failed; 2323 adapter->tx_map_failed += tx_map_failed; 2324 adapter->tx_stats_buffers[queue_num].packets += tx_packets; 2325 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes; 2326 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped; 2327 2328 return ret; 2329 } 2330 2331 static void ibmvnic_set_multi(struct net_device *netdev) 2332 { 2333 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2334 struct netdev_hw_addr *ha; 2335 union ibmvnic_crq crq; 2336 2337 memset(&crq, 0, sizeof(crq)); 2338 crq.request_capability.first = IBMVNIC_CRQ_CMD; 2339 crq.request_capability.cmd = REQUEST_CAPABILITY; 2340 2341 if (netdev->flags & IFF_PROMISC) { 2342 if (!adapter->promisc_supported) 2343 return; 2344 } else { 2345 if (netdev->flags & IFF_ALLMULTI) { 2346 /* Accept all multicast */ 2347 memset(&crq, 0, sizeof(crq)); 2348 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD; 2349 crq.multicast_ctrl.cmd = MULTICAST_CTRL; 2350 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL; 2351 ibmvnic_send_crq(adapter, &crq); 2352 } else if (netdev_mc_empty(netdev)) { 2353 /* Reject all multicast */ 2354 memset(&crq, 0, sizeof(crq)); 2355 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD; 2356 crq.multicast_ctrl.cmd = MULTICAST_CTRL; 2357 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL; 2358 ibmvnic_send_crq(adapter, &crq); 2359 } else { 2360 /* Accept one or more multicast(s) */ 2361 netdev_for_each_mc_addr(ha, netdev) { 2362 memset(&crq, 0, sizeof(crq)); 2363 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD; 2364 crq.multicast_ctrl.cmd = MULTICAST_CTRL; 2365 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC; 2366 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0], 2367 ha->addr); 2368 ibmvnic_send_crq(adapter, &crq); 2369 } 2370 } 2371 } 2372 } 2373 2374 static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr) 2375 { 2376 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2377 union ibmvnic_crq crq; 2378 int rc; 2379 2380 if (!is_valid_ether_addr(dev_addr)) { 2381 rc = -EADDRNOTAVAIL; 2382 goto err; 2383 } 2384 2385 memset(&crq, 0, sizeof(crq)); 2386 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD; 2387 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR; 2388 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr); 2389 2390 mutex_lock(&adapter->fw_lock); 2391 adapter->fw_done_rc = 0; 2392 reinit_completion(&adapter->fw_done); 2393 2394 rc = ibmvnic_send_crq(adapter, &crq); 2395 if (rc) { 2396 rc = -EIO; 2397 mutex_unlock(&adapter->fw_lock); 2398 goto err; 2399 } 2400 2401 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000); 2402 /* netdev->dev_addr is changed in handle_change_mac_rsp function */ 2403 if (rc || adapter->fw_done_rc) { 2404 rc = -EIO; 2405 mutex_unlock(&adapter->fw_lock); 2406 goto err; 2407 } 2408 mutex_unlock(&adapter->fw_lock); 2409 return 0; 2410 err: 2411 ether_addr_copy(adapter->mac_addr, netdev->dev_addr); 2412 return rc; 2413 } 2414 2415 static int ibmvnic_set_mac(struct net_device *netdev, void *p) 2416 { 2417 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2418 struct sockaddr *addr = p; 2419 int rc; 2420 2421 rc = 0; 2422 if (!is_valid_ether_addr(addr->sa_data)) 2423 return -EADDRNOTAVAIL; 2424 2425 ether_addr_copy(adapter->mac_addr, addr->sa_data); 2426 if (adapter->state != VNIC_PROBED) 2427 rc = __ibmvnic_set_mac(netdev, addr->sa_data); 2428 2429 return rc; 2430 } 2431 2432 static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason) 2433 { 2434 switch (reason) { 2435 case VNIC_RESET_FAILOVER: 2436 return "FAILOVER"; 2437 case VNIC_RESET_MOBILITY: 2438 return "MOBILITY"; 2439 case VNIC_RESET_FATAL: 2440 return "FATAL"; 2441 case VNIC_RESET_NON_FATAL: 2442 return "NON_FATAL"; 2443 case VNIC_RESET_TIMEOUT: 2444 return "TIMEOUT"; 2445 case VNIC_RESET_CHANGE_PARAM: 2446 return "CHANGE_PARAM"; 2447 case VNIC_RESET_PASSIVE_INIT: 2448 return "PASSIVE_INIT"; 2449 } 2450 return "UNKNOWN"; 2451 } 2452 2453 /* 2454 * Initialize the init_done completion and return code values. We 2455 * can get a transport event just after registering the CRQ and the 2456 * tasklet will use this to communicate the transport event. To ensure 2457 * we don't miss the notification/error, initialize these _before_ 2458 * regisering the CRQ. 2459 */ 2460 static inline void reinit_init_done(struct ibmvnic_adapter *adapter) 2461 { 2462 reinit_completion(&adapter->init_done); 2463 adapter->init_done_rc = 0; 2464 } 2465 2466 /* 2467 * do_reset returns zero if we are able to keep processing reset events, or 2468 * non-zero if we hit a fatal error and must halt. 2469 */ 2470 static int do_reset(struct ibmvnic_adapter *adapter, 2471 struct ibmvnic_rwi *rwi, u32 reset_state) 2472 { 2473 struct net_device *netdev = adapter->netdev; 2474 u64 old_num_rx_queues, old_num_tx_queues; 2475 u64 old_num_rx_slots, old_num_tx_slots; 2476 int rc; 2477 2478 netdev_dbg(adapter->netdev, 2479 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n", 2480 adapter_state_to_string(adapter->state), 2481 adapter->failover_pending, 2482 reset_reason_to_string(rwi->reset_reason), 2483 adapter_state_to_string(reset_state)); 2484 2485 adapter->reset_reason = rwi->reset_reason; 2486 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */ 2487 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM)) 2488 rtnl_lock(); 2489 2490 /* Now that we have the rtnl lock, clear any pending failover. 2491 * This will ensure ibmvnic_open() has either completed or will 2492 * block until failover is complete. 2493 */ 2494 if (rwi->reset_reason == VNIC_RESET_FAILOVER) 2495 adapter->failover_pending = false; 2496 2497 /* read the state and check (again) after getting rtnl */ 2498 reset_state = adapter->state; 2499 2500 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) { 2501 rc = -EBUSY; 2502 goto out; 2503 } 2504 2505 netif_carrier_off(netdev); 2506 2507 old_num_rx_queues = adapter->req_rx_queues; 2508 old_num_tx_queues = adapter->req_tx_queues; 2509 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq; 2510 old_num_tx_slots = adapter->req_tx_entries_per_subcrq; 2511 2512 ibmvnic_cleanup(netdev); 2513 2514 if (reset_state == VNIC_OPEN && 2515 adapter->reset_reason != VNIC_RESET_MOBILITY && 2516 adapter->reset_reason != VNIC_RESET_FAILOVER) { 2517 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) { 2518 rc = __ibmvnic_close(netdev); 2519 if (rc) 2520 goto out; 2521 } else { 2522 adapter->state = VNIC_CLOSING; 2523 2524 /* Release the RTNL lock before link state change and 2525 * re-acquire after the link state change to allow 2526 * linkwatch_event to grab the RTNL lock and run during 2527 * a reset. 2528 */ 2529 rtnl_unlock(); 2530 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN); 2531 rtnl_lock(); 2532 if (rc) 2533 goto out; 2534 2535 if (adapter->state == VNIC_OPEN) { 2536 /* When we dropped rtnl, ibmvnic_open() got 2537 * it and noticed that we are resetting and 2538 * set the adapter state to OPEN. Update our 2539 * new "target" state, and resume the reset 2540 * from VNIC_CLOSING state. 2541 */ 2542 netdev_dbg(netdev, 2543 "Open changed state from %s, updating.\n", 2544 adapter_state_to_string(reset_state)); 2545 reset_state = VNIC_OPEN; 2546 adapter->state = VNIC_CLOSING; 2547 } 2548 2549 if (adapter->state != VNIC_CLOSING) { 2550 /* If someone else changed the adapter state 2551 * when we dropped the rtnl, fail the reset 2552 */ 2553 rc = -EAGAIN; 2554 goto out; 2555 } 2556 adapter->state = VNIC_CLOSED; 2557 } 2558 } 2559 2560 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) { 2561 release_resources(adapter); 2562 release_sub_crqs(adapter, 1); 2563 release_crq_queue(adapter); 2564 } 2565 2566 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) { 2567 /* remove the closed state so when we call open it appears 2568 * we are coming from the probed state. 2569 */ 2570 adapter->state = VNIC_PROBED; 2571 2572 reinit_init_done(adapter); 2573 2574 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) { 2575 rc = init_crq_queue(adapter); 2576 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) { 2577 rc = ibmvnic_reenable_crq_queue(adapter); 2578 release_sub_crqs(adapter, 1); 2579 } else { 2580 rc = ibmvnic_reset_crq(adapter); 2581 if (rc == H_CLOSED || rc == H_SUCCESS) { 2582 rc = vio_enable_interrupts(adapter->vdev); 2583 if (rc) 2584 netdev_err(adapter->netdev, 2585 "Reset failed to enable interrupts. rc=%d\n", 2586 rc); 2587 } 2588 } 2589 2590 if (rc) { 2591 netdev_err(adapter->netdev, 2592 "Reset couldn't initialize crq. rc=%d\n", rc); 2593 goto out; 2594 } 2595 2596 rc = ibmvnic_reset_init(adapter, true); 2597 if (rc) 2598 goto out; 2599 2600 /* If the adapter was in PROBE or DOWN state prior to the reset, 2601 * exit here. 2602 */ 2603 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) { 2604 rc = 0; 2605 goto out; 2606 } 2607 2608 rc = ibmvnic_login(netdev); 2609 if (rc) 2610 goto out; 2611 2612 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) { 2613 rc = init_resources(adapter); 2614 if (rc) 2615 goto out; 2616 } else if (adapter->req_rx_queues != old_num_rx_queues || 2617 adapter->req_tx_queues != old_num_tx_queues || 2618 adapter->req_rx_add_entries_per_subcrq != 2619 old_num_rx_slots || 2620 adapter->req_tx_entries_per_subcrq != 2621 old_num_tx_slots || 2622 !adapter->rx_pool || 2623 !adapter->tso_pool || 2624 !adapter->tx_pool) { 2625 release_napi(adapter); 2626 release_vpd_data(adapter); 2627 2628 rc = init_resources(adapter); 2629 if (rc) 2630 goto out; 2631 2632 } else { 2633 rc = init_tx_pools(netdev); 2634 if (rc) { 2635 netdev_dbg(netdev, 2636 "init tx pools failed (%d)\n", 2637 rc); 2638 goto out; 2639 } 2640 2641 rc = init_rx_pools(netdev); 2642 if (rc) { 2643 netdev_dbg(netdev, 2644 "init rx pools failed (%d)\n", 2645 rc); 2646 goto out; 2647 } 2648 } 2649 ibmvnic_disable_irqs(adapter); 2650 } 2651 adapter->state = VNIC_CLOSED; 2652 2653 if (reset_state == VNIC_CLOSED) { 2654 rc = 0; 2655 goto out; 2656 } 2657 2658 rc = __ibmvnic_open(netdev); 2659 if (rc) { 2660 rc = IBMVNIC_OPEN_FAILED; 2661 goto out; 2662 } 2663 2664 /* refresh device's multicast list */ 2665 ibmvnic_set_multi(netdev); 2666 2667 if (adapter->reset_reason == VNIC_RESET_FAILOVER || 2668 adapter->reset_reason == VNIC_RESET_MOBILITY) 2669 __netdev_notify_peers(netdev); 2670 2671 rc = 0; 2672 2673 out: 2674 /* restore the adapter state if reset failed */ 2675 if (rc) 2676 adapter->state = reset_state; 2677 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */ 2678 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM)) 2679 rtnl_unlock(); 2680 2681 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n", 2682 adapter_state_to_string(adapter->state), 2683 adapter->failover_pending, rc); 2684 return rc; 2685 } 2686 2687 static int do_hard_reset(struct ibmvnic_adapter *adapter, 2688 struct ibmvnic_rwi *rwi, u32 reset_state) 2689 { 2690 struct net_device *netdev = adapter->netdev; 2691 int rc; 2692 2693 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n", 2694 reset_reason_to_string(rwi->reset_reason)); 2695 2696 /* read the state and check (again) after getting rtnl */ 2697 reset_state = adapter->state; 2698 2699 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) { 2700 rc = -EBUSY; 2701 goto out; 2702 } 2703 2704 netif_carrier_off(netdev); 2705 adapter->reset_reason = rwi->reset_reason; 2706 2707 ibmvnic_cleanup(netdev); 2708 release_resources(adapter); 2709 release_sub_crqs(adapter, 0); 2710 release_crq_queue(adapter); 2711 2712 /* remove the closed state so when we call open it appears 2713 * we are coming from the probed state. 2714 */ 2715 adapter->state = VNIC_PROBED; 2716 2717 reinit_init_done(adapter); 2718 2719 rc = init_crq_queue(adapter); 2720 if (rc) { 2721 netdev_err(adapter->netdev, 2722 "Couldn't initialize crq. rc=%d\n", rc); 2723 goto out; 2724 } 2725 2726 rc = ibmvnic_reset_init(adapter, false); 2727 if (rc) 2728 goto out; 2729 2730 /* If the adapter was in PROBE or DOWN state prior to the reset, 2731 * exit here. 2732 */ 2733 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) 2734 goto out; 2735 2736 rc = ibmvnic_login(netdev); 2737 if (rc) 2738 goto out; 2739 2740 rc = init_resources(adapter); 2741 if (rc) 2742 goto out; 2743 2744 ibmvnic_disable_irqs(adapter); 2745 adapter->state = VNIC_CLOSED; 2746 2747 if (reset_state == VNIC_CLOSED) 2748 goto out; 2749 2750 rc = __ibmvnic_open(netdev); 2751 if (rc) { 2752 rc = IBMVNIC_OPEN_FAILED; 2753 goto out; 2754 } 2755 2756 __netdev_notify_peers(netdev); 2757 out: 2758 /* restore adapter state if reset failed */ 2759 if (rc) 2760 adapter->state = reset_state; 2761 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n", 2762 adapter_state_to_string(adapter->state), 2763 adapter->failover_pending, rc); 2764 return rc; 2765 } 2766 2767 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter) 2768 { 2769 struct ibmvnic_rwi *rwi; 2770 unsigned long flags; 2771 2772 spin_lock_irqsave(&adapter->rwi_lock, flags); 2773 2774 if (!list_empty(&adapter->rwi_list)) { 2775 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi, 2776 list); 2777 list_del(&rwi->list); 2778 } else { 2779 rwi = NULL; 2780 } 2781 2782 spin_unlock_irqrestore(&adapter->rwi_lock, flags); 2783 return rwi; 2784 } 2785 2786 /** 2787 * do_passive_init - complete probing when partner device is detected. 2788 * @adapter: ibmvnic_adapter struct 2789 * 2790 * If the ibmvnic device does not have a partner device to communicate with at boot 2791 * and that partner device comes online at a later time, this function is called 2792 * to complete the initialization process of ibmvnic device. 2793 * Caller is expected to hold rtnl_lock(). 2794 * 2795 * Returns non-zero if sub-CRQs are not initialized properly leaving the device 2796 * in the down state. 2797 * Returns 0 upon success and the device is in PROBED state. 2798 */ 2799 2800 static int do_passive_init(struct ibmvnic_adapter *adapter) 2801 { 2802 unsigned long timeout = msecs_to_jiffies(30000); 2803 struct net_device *netdev = adapter->netdev; 2804 struct device *dev = &adapter->vdev->dev; 2805 int rc; 2806 2807 netdev_dbg(netdev, "Partner device found, probing.\n"); 2808 2809 adapter->state = VNIC_PROBING; 2810 reinit_completion(&adapter->init_done); 2811 adapter->init_done_rc = 0; 2812 adapter->crq.active = true; 2813 2814 rc = send_crq_init_complete(adapter); 2815 if (rc) 2816 goto out; 2817 2818 rc = send_version_xchg(adapter); 2819 if (rc) 2820 netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc); 2821 2822 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) { 2823 dev_err(dev, "Initialization sequence timed out\n"); 2824 rc = -ETIMEDOUT; 2825 goto out; 2826 } 2827 2828 rc = init_sub_crqs(adapter); 2829 if (rc) { 2830 dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc); 2831 goto out; 2832 } 2833 2834 rc = init_sub_crq_irqs(adapter); 2835 if (rc) { 2836 dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc); 2837 goto init_failed; 2838 } 2839 2840 netdev->mtu = adapter->req_mtu - ETH_HLEN; 2841 netdev->min_mtu = adapter->min_mtu - ETH_HLEN; 2842 netdev->max_mtu = adapter->max_mtu - ETH_HLEN; 2843 2844 adapter->state = VNIC_PROBED; 2845 netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n"); 2846 2847 return 0; 2848 2849 init_failed: 2850 release_sub_crqs(adapter, 1); 2851 out: 2852 adapter->state = VNIC_DOWN; 2853 return rc; 2854 } 2855 2856 static void __ibmvnic_reset(struct work_struct *work) 2857 { 2858 struct ibmvnic_adapter *adapter; 2859 unsigned int timeout = 5000; 2860 struct ibmvnic_rwi *tmprwi; 2861 bool saved_state = false; 2862 struct ibmvnic_rwi *rwi; 2863 unsigned long flags; 2864 struct device *dev; 2865 bool need_reset; 2866 int num_fails = 0; 2867 u32 reset_state; 2868 int rc = 0; 2869 2870 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset); 2871 dev = &adapter->vdev->dev; 2872 2873 /* Wait for ibmvnic_probe() to complete. If probe is taking too long 2874 * or if another reset is in progress, defer work for now. If probe 2875 * eventually fails it will flush and terminate our work. 2876 * 2877 * Three possibilities here: 2878 * 1. Adpater being removed - just return 2879 * 2. Timed out on probe or another reset in progress - delay the work 2880 * 3. Completed probe - perform any resets in queue 2881 */ 2882 if (adapter->state == VNIC_PROBING && 2883 !wait_for_completion_timeout(&adapter->probe_done, timeout)) { 2884 dev_err(dev, "Reset thread timed out on probe"); 2885 queue_delayed_work(system_long_wq, 2886 &adapter->ibmvnic_delayed_reset, 2887 IBMVNIC_RESET_DELAY); 2888 return; 2889 } 2890 2891 /* adapter is done with probe (i.e state is never VNIC_PROBING now) */ 2892 if (adapter->state == VNIC_REMOVING) 2893 return; 2894 2895 /* ->rwi_list is stable now (no one else is removing entries) */ 2896 2897 /* ibmvnic_probe() may have purged the reset queue after we were 2898 * scheduled to process a reset so there maybe no resets to process. 2899 * Before setting the ->resetting bit though, we have to make sure 2900 * that there is infact a reset to process. Otherwise we may race 2901 * with ibmvnic_open() and end up leaving the vnic down: 2902 * 2903 * __ibmvnic_reset() ibmvnic_open() 2904 * ----------------- -------------- 2905 * 2906 * set ->resetting bit 2907 * find ->resetting bit is set 2908 * set ->state to IBMVNIC_OPEN (i.e 2909 * assume reset will open device) 2910 * return 2911 * find reset queue empty 2912 * return 2913 * 2914 * Neither performed vnic login/open and vnic stays down 2915 * 2916 * If we hold the lock and conditionally set the bit, either we 2917 * or ibmvnic_open() will complete the open. 2918 */ 2919 need_reset = false; 2920 spin_lock(&adapter->rwi_lock); 2921 if (!list_empty(&adapter->rwi_list)) { 2922 if (test_and_set_bit_lock(0, &adapter->resetting)) { 2923 queue_delayed_work(system_long_wq, 2924 &adapter->ibmvnic_delayed_reset, 2925 IBMVNIC_RESET_DELAY); 2926 } else { 2927 need_reset = true; 2928 } 2929 } 2930 spin_unlock(&adapter->rwi_lock); 2931 2932 if (!need_reset) 2933 return; 2934 2935 rwi = get_next_rwi(adapter); 2936 while (rwi) { 2937 spin_lock_irqsave(&adapter->state_lock, flags); 2938 2939 if (adapter->state == VNIC_REMOVING || 2940 adapter->state == VNIC_REMOVED) { 2941 spin_unlock_irqrestore(&adapter->state_lock, flags); 2942 kfree(rwi); 2943 rc = EBUSY; 2944 break; 2945 } 2946 2947 if (!saved_state) { 2948 reset_state = adapter->state; 2949 saved_state = true; 2950 } 2951 spin_unlock_irqrestore(&adapter->state_lock, flags); 2952 2953 if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) { 2954 rtnl_lock(); 2955 rc = do_passive_init(adapter); 2956 rtnl_unlock(); 2957 if (!rc) 2958 netif_carrier_on(adapter->netdev); 2959 } else if (adapter->force_reset_recovery) { 2960 /* Since we are doing a hard reset now, clear the 2961 * failover_pending flag so we don't ignore any 2962 * future MOBILITY or other resets. 2963 */ 2964 adapter->failover_pending = false; 2965 2966 /* Transport event occurred during previous reset */ 2967 if (adapter->wait_for_reset) { 2968 /* Previous was CHANGE_PARAM; caller locked */ 2969 adapter->force_reset_recovery = false; 2970 rc = do_hard_reset(adapter, rwi, reset_state); 2971 } else { 2972 rtnl_lock(); 2973 adapter->force_reset_recovery = false; 2974 rc = do_hard_reset(adapter, rwi, reset_state); 2975 rtnl_unlock(); 2976 } 2977 if (rc) 2978 num_fails++; 2979 else 2980 num_fails = 0; 2981 2982 /* If auto-priority-failover is enabled we can get 2983 * back to back failovers during resets, resulting 2984 * in at least two failed resets (from high-priority 2985 * backing device to low-priority one and then back) 2986 * If resets continue to fail beyond that, give the 2987 * adapter some time to settle down before retrying. 2988 */ 2989 if (num_fails >= 3) { 2990 netdev_dbg(adapter->netdev, 2991 "[S:%s] Hard reset failed %d times, waiting 60 secs\n", 2992 adapter_state_to_string(adapter->state), 2993 num_fails); 2994 set_current_state(TASK_UNINTERRUPTIBLE); 2995 schedule_timeout(60 * HZ); 2996 } 2997 } else { 2998 rc = do_reset(adapter, rwi, reset_state); 2999 } 3000 tmprwi = rwi; 3001 adapter->last_reset_time = jiffies; 3002 3003 if (rc) 3004 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc); 3005 3006 rwi = get_next_rwi(adapter); 3007 3008 /* 3009 * If there is another reset queued, free the previous rwi 3010 * and process the new reset even if previous reset failed 3011 * (the previous reset could have failed because of a fail 3012 * over for instance, so process the fail over). 3013 * 3014 * If there are no resets queued and the previous reset failed, 3015 * the adapter would be in an undefined state. So retry the 3016 * previous reset as a hard reset. 3017 */ 3018 if (rwi) 3019 kfree(tmprwi); 3020 else if (rc) 3021 rwi = tmprwi; 3022 3023 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER || 3024 rwi->reset_reason == VNIC_RESET_MOBILITY || rc)) 3025 adapter->force_reset_recovery = true; 3026 } 3027 3028 if (adapter->wait_for_reset) { 3029 adapter->reset_done_rc = rc; 3030 complete(&adapter->reset_done); 3031 } 3032 3033 clear_bit_unlock(0, &adapter->resetting); 3034 3035 netdev_dbg(adapter->netdev, 3036 "[S:%s FRR:%d WFR:%d] Done processing resets\n", 3037 adapter_state_to_string(adapter->state), 3038 adapter->force_reset_recovery, 3039 adapter->wait_for_reset); 3040 } 3041 3042 static void __ibmvnic_delayed_reset(struct work_struct *work) 3043 { 3044 struct ibmvnic_adapter *adapter; 3045 3046 adapter = container_of(work, struct ibmvnic_adapter, 3047 ibmvnic_delayed_reset.work); 3048 __ibmvnic_reset(&adapter->ibmvnic_reset); 3049 } 3050 3051 static void flush_reset_queue(struct ibmvnic_adapter *adapter) 3052 { 3053 struct list_head *entry, *tmp_entry; 3054 3055 if (!list_empty(&adapter->rwi_list)) { 3056 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list) { 3057 list_del(entry); 3058 kfree(list_entry(entry, struct ibmvnic_rwi, list)); 3059 } 3060 } 3061 } 3062 3063 static int ibmvnic_reset(struct ibmvnic_adapter *adapter, 3064 enum ibmvnic_reset_reason reason) 3065 { 3066 struct net_device *netdev = adapter->netdev; 3067 struct ibmvnic_rwi *rwi, *tmp; 3068 unsigned long flags; 3069 int ret; 3070 3071 spin_lock_irqsave(&adapter->rwi_lock, flags); 3072 3073 /* If failover is pending don't schedule any other reset. 3074 * Instead let the failover complete. If there is already a 3075 * a failover reset scheduled, we will detect and drop the 3076 * duplicate reset when walking the ->rwi_list below. 3077 */ 3078 if (adapter->state == VNIC_REMOVING || 3079 adapter->state == VNIC_REMOVED || 3080 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) { 3081 ret = EBUSY; 3082 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n"); 3083 goto err; 3084 } 3085 3086 list_for_each_entry(tmp, &adapter->rwi_list, list) { 3087 if (tmp->reset_reason == reason) { 3088 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n", 3089 reset_reason_to_string(reason)); 3090 ret = EBUSY; 3091 goto err; 3092 } 3093 } 3094 3095 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC); 3096 if (!rwi) { 3097 ret = ENOMEM; 3098 goto err; 3099 } 3100 /* if we just received a transport event, 3101 * flush reset queue and process this reset 3102 */ 3103 if (adapter->force_reset_recovery) 3104 flush_reset_queue(adapter); 3105 3106 rwi->reset_reason = reason; 3107 list_add_tail(&rwi->list, &adapter->rwi_list); 3108 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n", 3109 reset_reason_to_string(reason)); 3110 queue_work(system_long_wq, &adapter->ibmvnic_reset); 3111 3112 ret = 0; 3113 err: 3114 /* ibmvnic_close() below can block, so drop the lock first */ 3115 spin_unlock_irqrestore(&adapter->rwi_lock, flags); 3116 3117 if (ret == ENOMEM) 3118 ibmvnic_close(netdev); 3119 3120 return -ret; 3121 } 3122 3123 static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue) 3124 { 3125 struct ibmvnic_adapter *adapter = netdev_priv(dev); 3126 3127 if (test_bit(0, &adapter->resetting)) { 3128 netdev_err(adapter->netdev, 3129 "Adapter is resetting, skip timeout reset\n"); 3130 return; 3131 } 3132 /* No queuing up reset until at least 5 seconds (default watchdog val) 3133 * after last reset 3134 */ 3135 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) { 3136 netdev_dbg(dev, "Not yet time to tx timeout.\n"); 3137 return; 3138 } 3139 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT); 3140 } 3141 3142 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter, 3143 struct ibmvnic_rx_buff *rx_buff) 3144 { 3145 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index]; 3146 3147 rx_buff->skb = NULL; 3148 3149 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff); 3150 pool->next_alloc = (pool->next_alloc + 1) % pool->size; 3151 3152 atomic_dec(&pool->available); 3153 } 3154 3155 static int ibmvnic_poll(struct napi_struct *napi, int budget) 3156 { 3157 struct ibmvnic_sub_crq_queue *rx_scrq; 3158 struct ibmvnic_adapter *adapter; 3159 struct net_device *netdev; 3160 int frames_processed; 3161 int scrq_num; 3162 3163 netdev = napi->dev; 3164 adapter = netdev_priv(netdev); 3165 scrq_num = (int)(napi - adapter->napi); 3166 frames_processed = 0; 3167 rx_scrq = adapter->rx_scrq[scrq_num]; 3168 3169 restart_poll: 3170 while (frames_processed < budget) { 3171 struct sk_buff *skb; 3172 struct ibmvnic_rx_buff *rx_buff; 3173 union sub_crq *next; 3174 u32 length; 3175 u16 offset; 3176 u8 flags = 0; 3177 3178 if (unlikely(test_bit(0, &adapter->resetting) && 3179 adapter->reset_reason != VNIC_RESET_NON_FATAL)) { 3180 enable_scrq_irq(adapter, rx_scrq); 3181 napi_complete_done(napi, frames_processed); 3182 return frames_processed; 3183 } 3184 3185 if (!pending_scrq(adapter, rx_scrq)) 3186 break; 3187 next = ibmvnic_next_scrq(adapter, rx_scrq); 3188 rx_buff = (struct ibmvnic_rx_buff *) 3189 be64_to_cpu(next->rx_comp.correlator); 3190 /* do error checking */ 3191 if (next->rx_comp.rc) { 3192 netdev_dbg(netdev, "rx buffer returned with rc %x\n", 3193 be16_to_cpu(next->rx_comp.rc)); 3194 /* free the entry */ 3195 next->rx_comp.first = 0; 3196 dev_kfree_skb_any(rx_buff->skb); 3197 remove_buff_from_pool(adapter, rx_buff); 3198 continue; 3199 } else if (!rx_buff->skb) { 3200 /* free the entry */ 3201 next->rx_comp.first = 0; 3202 remove_buff_from_pool(adapter, rx_buff); 3203 continue; 3204 } 3205 3206 length = be32_to_cpu(next->rx_comp.len); 3207 offset = be16_to_cpu(next->rx_comp.off_frame_data); 3208 flags = next->rx_comp.flags; 3209 skb = rx_buff->skb; 3210 /* load long_term_buff before copying to skb */ 3211 dma_rmb(); 3212 skb_copy_to_linear_data(skb, rx_buff->data + offset, 3213 length); 3214 3215 /* VLAN Header has been stripped by the system firmware and 3216 * needs to be inserted by the driver 3217 */ 3218 if (adapter->rx_vlan_header_insertion && 3219 (flags & IBMVNIC_VLAN_STRIPPED)) 3220 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 3221 ntohs(next->rx_comp.vlan_tci)); 3222 3223 /* free the entry */ 3224 next->rx_comp.first = 0; 3225 remove_buff_from_pool(adapter, rx_buff); 3226 3227 skb_put(skb, length); 3228 skb->protocol = eth_type_trans(skb, netdev); 3229 skb_record_rx_queue(skb, scrq_num); 3230 3231 if (flags & IBMVNIC_IP_CHKSUM_GOOD && 3232 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) { 3233 skb->ip_summed = CHECKSUM_UNNECESSARY; 3234 } 3235 3236 length = skb->len; 3237 napi_gro_receive(napi, skb); /* send it up */ 3238 netdev->stats.rx_packets++; 3239 netdev->stats.rx_bytes += length; 3240 adapter->rx_stats_buffers[scrq_num].packets++; 3241 adapter->rx_stats_buffers[scrq_num].bytes += length; 3242 frames_processed++; 3243 } 3244 3245 if (adapter->state != VNIC_CLOSING && 3246 ((atomic_read(&adapter->rx_pool[scrq_num].available) < 3247 adapter->req_rx_add_entries_per_subcrq / 2) || 3248 frames_processed < budget)) 3249 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]); 3250 if (frames_processed < budget) { 3251 if (napi_complete_done(napi, frames_processed)) { 3252 enable_scrq_irq(adapter, rx_scrq); 3253 if (pending_scrq(adapter, rx_scrq)) { 3254 if (napi_reschedule(napi)) { 3255 disable_scrq_irq(adapter, rx_scrq); 3256 goto restart_poll; 3257 } 3258 } 3259 } 3260 } 3261 return frames_processed; 3262 } 3263 3264 static int wait_for_reset(struct ibmvnic_adapter *adapter) 3265 { 3266 int rc, ret; 3267 3268 adapter->fallback.mtu = adapter->req_mtu; 3269 adapter->fallback.rx_queues = adapter->req_rx_queues; 3270 adapter->fallback.tx_queues = adapter->req_tx_queues; 3271 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq; 3272 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq; 3273 3274 reinit_completion(&adapter->reset_done); 3275 adapter->wait_for_reset = true; 3276 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM); 3277 3278 if (rc) { 3279 ret = rc; 3280 goto out; 3281 } 3282 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000); 3283 if (rc) { 3284 ret = -ENODEV; 3285 goto out; 3286 } 3287 3288 ret = 0; 3289 if (adapter->reset_done_rc) { 3290 ret = -EIO; 3291 adapter->desired.mtu = adapter->fallback.mtu; 3292 adapter->desired.rx_queues = adapter->fallback.rx_queues; 3293 adapter->desired.tx_queues = adapter->fallback.tx_queues; 3294 adapter->desired.rx_entries = adapter->fallback.rx_entries; 3295 adapter->desired.tx_entries = adapter->fallback.tx_entries; 3296 3297 reinit_completion(&adapter->reset_done); 3298 adapter->wait_for_reset = true; 3299 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM); 3300 if (rc) { 3301 ret = rc; 3302 goto out; 3303 } 3304 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 3305 60000); 3306 if (rc) { 3307 ret = -ENODEV; 3308 goto out; 3309 } 3310 } 3311 out: 3312 adapter->wait_for_reset = false; 3313 3314 return ret; 3315 } 3316 3317 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu) 3318 { 3319 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3320 3321 adapter->desired.mtu = new_mtu + ETH_HLEN; 3322 3323 return wait_for_reset(adapter); 3324 } 3325 3326 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb, 3327 struct net_device *dev, 3328 netdev_features_t features) 3329 { 3330 /* Some backing hardware adapters can not 3331 * handle packets with a MSS less than 224 3332 * or with only one segment. 3333 */ 3334 if (skb_is_gso(skb)) { 3335 if (skb_shinfo(skb)->gso_size < 224 || 3336 skb_shinfo(skb)->gso_segs == 1) 3337 features &= ~NETIF_F_GSO_MASK; 3338 } 3339 3340 return features; 3341 } 3342 3343 static const struct net_device_ops ibmvnic_netdev_ops = { 3344 .ndo_open = ibmvnic_open, 3345 .ndo_stop = ibmvnic_close, 3346 .ndo_start_xmit = ibmvnic_xmit, 3347 .ndo_set_rx_mode = ibmvnic_set_multi, 3348 .ndo_set_mac_address = ibmvnic_set_mac, 3349 .ndo_validate_addr = eth_validate_addr, 3350 .ndo_tx_timeout = ibmvnic_tx_timeout, 3351 .ndo_change_mtu = ibmvnic_change_mtu, 3352 .ndo_features_check = ibmvnic_features_check, 3353 }; 3354 3355 /* ethtool functions */ 3356 3357 static int ibmvnic_get_link_ksettings(struct net_device *netdev, 3358 struct ethtool_link_ksettings *cmd) 3359 { 3360 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3361 int rc; 3362 3363 rc = send_query_phys_parms(adapter); 3364 if (rc) { 3365 adapter->speed = SPEED_UNKNOWN; 3366 adapter->duplex = DUPLEX_UNKNOWN; 3367 } 3368 cmd->base.speed = adapter->speed; 3369 cmd->base.duplex = adapter->duplex; 3370 cmd->base.port = PORT_FIBRE; 3371 cmd->base.phy_address = 0; 3372 cmd->base.autoneg = AUTONEG_ENABLE; 3373 3374 return 0; 3375 } 3376 3377 static void ibmvnic_get_drvinfo(struct net_device *netdev, 3378 struct ethtool_drvinfo *info) 3379 { 3380 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3381 3382 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver)); 3383 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version)); 3384 strscpy(info->fw_version, adapter->fw_version, 3385 sizeof(info->fw_version)); 3386 } 3387 3388 static u32 ibmvnic_get_msglevel(struct net_device *netdev) 3389 { 3390 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3391 3392 return adapter->msg_enable; 3393 } 3394 3395 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data) 3396 { 3397 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3398 3399 adapter->msg_enable = data; 3400 } 3401 3402 static u32 ibmvnic_get_link(struct net_device *netdev) 3403 { 3404 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3405 3406 /* Don't need to send a query because we request a logical link up at 3407 * init and then we wait for link state indications 3408 */ 3409 return adapter->logical_link_state; 3410 } 3411 3412 static void ibmvnic_get_ringparam(struct net_device *netdev, 3413 struct ethtool_ringparam *ring, 3414 struct kernel_ethtool_ringparam *kernel_ring, 3415 struct netlink_ext_ack *extack) 3416 { 3417 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3418 3419 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) { 3420 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq; 3421 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq; 3422 } else { 3423 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ; 3424 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ; 3425 } 3426 ring->rx_mini_max_pending = 0; 3427 ring->rx_jumbo_max_pending = 0; 3428 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq; 3429 ring->tx_pending = adapter->req_tx_entries_per_subcrq; 3430 ring->rx_mini_pending = 0; 3431 ring->rx_jumbo_pending = 0; 3432 } 3433 3434 static int ibmvnic_set_ringparam(struct net_device *netdev, 3435 struct ethtool_ringparam *ring, 3436 struct kernel_ethtool_ringparam *kernel_ring, 3437 struct netlink_ext_ack *extack) 3438 { 3439 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3440 int ret; 3441 3442 ret = 0; 3443 adapter->desired.rx_entries = ring->rx_pending; 3444 adapter->desired.tx_entries = ring->tx_pending; 3445 3446 ret = wait_for_reset(adapter); 3447 3448 if (!ret && 3449 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending || 3450 adapter->req_tx_entries_per_subcrq != ring->tx_pending)) 3451 netdev_info(netdev, 3452 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n", 3453 ring->rx_pending, ring->tx_pending, 3454 adapter->req_rx_add_entries_per_subcrq, 3455 adapter->req_tx_entries_per_subcrq); 3456 return ret; 3457 } 3458 3459 static void ibmvnic_get_channels(struct net_device *netdev, 3460 struct ethtool_channels *channels) 3461 { 3462 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3463 3464 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) { 3465 channels->max_rx = adapter->max_rx_queues; 3466 channels->max_tx = adapter->max_tx_queues; 3467 } else { 3468 channels->max_rx = IBMVNIC_MAX_QUEUES; 3469 channels->max_tx = IBMVNIC_MAX_QUEUES; 3470 } 3471 3472 channels->max_other = 0; 3473 channels->max_combined = 0; 3474 channels->rx_count = adapter->req_rx_queues; 3475 channels->tx_count = adapter->req_tx_queues; 3476 channels->other_count = 0; 3477 channels->combined_count = 0; 3478 } 3479 3480 static int ibmvnic_set_channels(struct net_device *netdev, 3481 struct ethtool_channels *channels) 3482 { 3483 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3484 int ret; 3485 3486 ret = 0; 3487 adapter->desired.rx_queues = channels->rx_count; 3488 adapter->desired.tx_queues = channels->tx_count; 3489 3490 ret = wait_for_reset(adapter); 3491 3492 if (!ret && 3493 (adapter->req_rx_queues != channels->rx_count || 3494 adapter->req_tx_queues != channels->tx_count)) 3495 netdev_info(netdev, 3496 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n", 3497 channels->rx_count, channels->tx_count, 3498 adapter->req_rx_queues, adapter->req_tx_queues); 3499 return ret; 3500 } 3501 3502 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data) 3503 { 3504 struct ibmvnic_adapter *adapter = netdev_priv(dev); 3505 int i; 3506 3507 switch (stringset) { 3508 case ETH_SS_STATS: 3509 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); 3510 i++, data += ETH_GSTRING_LEN) 3511 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN); 3512 3513 for (i = 0; i < adapter->req_tx_queues; i++) { 3514 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i); 3515 data += ETH_GSTRING_LEN; 3516 3517 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i); 3518 data += ETH_GSTRING_LEN; 3519 3520 snprintf(data, ETH_GSTRING_LEN, 3521 "tx%d_dropped_packets", i); 3522 data += ETH_GSTRING_LEN; 3523 } 3524 3525 for (i = 0; i < adapter->req_rx_queues; i++) { 3526 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i); 3527 data += ETH_GSTRING_LEN; 3528 3529 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i); 3530 data += ETH_GSTRING_LEN; 3531 3532 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i); 3533 data += ETH_GSTRING_LEN; 3534 } 3535 break; 3536 3537 case ETH_SS_PRIV_FLAGS: 3538 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++) 3539 strcpy(data + i * ETH_GSTRING_LEN, 3540 ibmvnic_priv_flags[i]); 3541 break; 3542 default: 3543 return; 3544 } 3545 } 3546 3547 static int ibmvnic_get_sset_count(struct net_device *dev, int sset) 3548 { 3549 struct ibmvnic_adapter *adapter = netdev_priv(dev); 3550 3551 switch (sset) { 3552 case ETH_SS_STATS: 3553 return ARRAY_SIZE(ibmvnic_stats) + 3554 adapter->req_tx_queues * NUM_TX_STATS + 3555 adapter->req_rx_queues * NUM_RX_STATS; 3556 case ETH_SS_PRIV_FLAGS: 3557 return ARRAY_SIZE(ibmvnic_priv_flags); 3558 default: 3559 return -EOPNOTSUPP; 3560 } 3561 } 3562 3563 static void ibmvnic_get_ethtool_stats(struct net_device *dev, 3564 struct ethtool_stats *stats, u64 *data) 3565 { 3566 struct ibmvnic_adapter *adapter = netdev_priv(dev); 3567 union ibmvnic_crq crq; 3568 int i, j; 3569 int rc; 3570 3571 memset(&crq, 0, sizeof(crq)); 3572 crq.request_statistics.first = IBMVNIC_CRQ_CMD; 3573 crq.request_statistics.cmd = REQUEST_STATISTICS; 3574 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token); 3575 crq.request_statistics.len = 3576 cpu_to_be32(sizeof(struct ibmvnic_statistics)); 3577 3578 /* Wait for data to be written */ 3579 reinit_completion(&adapter->stats_done); 3580 rc = ibmvnic_send_crq(adapter, &crq); 3581 if (rc) 3582 return; 3583 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000); 3584 if (rc) 3585 return; 3586 3587 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++) 3588 data[i] = be64_to_cpu(IBMVNIC_GET_STAT 3589 (adapter, ibmvnic_stats[i].offset)); 3590 3591 for (j = 0; j < adapter->req_tx_queues; j++) { 3592 data[i] = adapter->tx_stats_buffers[j].packets; 3593 i++; 3594 data[i] = adapter->tx_stats_buffers[j].bytes; 3595 i++; 3596 data[i] = adapter->tx_stats_buffers[j].dropped_packets; 3597 i++; 3598 } 3599 3600 for (j = 0; j < adapter->req_rx_queues; j++) { 3601 data[i] = adapter->rx_stats_buffers[j].packets; 3602 i++; 3603 data[i] = adapter->rx_stats_buffers[j].bytes; 3604 i++; 3605 data[i] = adapter->rx_stats_buffers[j].interrupts; 3606 i++; 3607 } 3608 } 3609 3610 static u32 ibmvnic_get_priv_flags(struct net_device *netdev) 3611 { 3612 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3613 3614 return adapter->priv_flags; 3615 } 3616 3617 static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags) 3618 { 3619 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 3620 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES); 3621 3622 if (which_maxes) 3623 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES; 3624 else 3625 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES; 3626 3627 return 0; 3628 } 3629 3630 static const struct ethtool_ops ibmvnic_ethtool_ops = { 3631 .get_drvinfo = ibmvnic_get_drvinfo, 3632 .get_msglevel = ibmvnic_get_msglevel, 3633 .set_msglevel = ibmvnic_set_msglevel, 3634 .get_link = ibmvnic_get_link, 3635 .get_ringparam = ibmvnic_get_ringparam, 3636 .set_ringparam = ibmvnic_set_ringparam, 3637 .get_channels = ibmvnic_get_channels, 3638 .set_channels = ibmvnic_set_channels, 3639 .get_strings = ibmvnic_get_strings, 3640 .get_sset_count = ibmvnic_get_sset_count, 3641 .get_ethtool_stats = ibmvnic_get_ethtool_stats, 3642 .get_link_ksettings = ibmvnic_get_link_ksettings, 3643 .get_priv_flags = ibmvnic_get_priv_flags, 3644 .set_priv_flags = ibmvnic_set_priv_flags, 3645 }; 3646 3647 /* Routines for managing CRQs/sCRQs */ 3648 3649 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter, 3650 struct ibmvnic_sub_crq_queue *scrq) 3651 { 3652 int rc; 3653 3654 if (!scrq) { 3655 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n"); 3656 return -EINVAL; 3657 } 3658 3659 if (scrq->irq) { 3660 free_irq(scrq->irq, scrq); 3661 irq_dispose_mapping(scrq->irq); 3662 scrq->irq = 0; 3663 } 3664 3665 if (scrq->msgs) { 3666 memset(scrq->msgs, 0, 4 * PAGE_SIZE); 3667 atomic_set(&scrq->used, 0); 3668 scrq->cur = 0; 3669 scrq->ind_buf.index = 0; 3670 } else { 3671 netdev_dbg(adapter->netdev, "Invalid scrq reset\n"); 3672 return -EINVAL; 3673 } 3674 3675 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token, 3676 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq); 3677 return rc; 3678 } 3679 3680 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter) 3681 { 3682 int i, rc; 3683 3684 if (!adapter->tx_scrq || !adapter->rx_scrq) 3685 return -EINVAL; 3686 3687 for (i = 0; i < adapter->req_tx_queues; i++) { 3688 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i); 3689 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]); 3690 if (rc) 3691 return rc; 3692 } 3693 3694 for (i = 0; i < adapter->req_rx_queues; i++) { 3695 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i); 3696 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]); 3697 if (rc) 3698 return rc; 3699 } 3700 3701 return rc; 3702 } 3703 3704 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter, 3705 struct ibmvnic_sub_crq_queue *scrq, 3706 bool do_h_free) 3707 { 3708 struct device *dev = &adapter->vdev->dev; 3709 long rc; 3710 3711 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n"); 3712 3713 if (do_h_free) { 3714 /* Close the sub-crqs */ 3715 do { 3716 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, 3717 adapter->vdev->unit_address, 3718 scrq->crq_num); 3719 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 3720 3721 if (rc) { 3722 netdev_err(adapter->netdev, 3723 "Failed to release sub-CRQ %16lx, rc = %ld\n", 3724 scrq->crq_num, rc); 3725 } 3726 } 3727 3728 dma_free_coherent(dev, 3729 IBMVNIC_IND_ARR_SZ, 3730 scrq->ind_buf.indir_arr, 3731 scrq->ind_buf.indir_dma); 3732 3733 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE, 3734 DMA_BIDIRECTIONAL); 3735 free_pages((unsigned long)scrq->msgs, 2); 3736 kfree(scrq); 3737 } 3738 3739 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter 3740 *adapter) 3741 { 3742 struct device *dev = &adapter->vdev->dev; 3743 struct ibmvnic_sub_crq_queue *scrq; 3744 int rc; 3745 3746 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL); 3747 if (!scrq) 3748 return NULL; 3749 3750 scrq->msgs = 3751 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2); 3752 if (!scrq->msgs) { 3753 dev_warn(dev, "Couldn't allocate crq queue messages page\n"); 3754 goto zero_page_failed; 3755 } 3756 3757 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE, 3758 DMA_BIDIRECTIONAL); 3759 if (dma_mapping_error(dev, scrq->msg_token)) { 3760 dev_warn(dev, "Couldn't map crq queue messages page\n"); 3761 goto map_failed; 3762 } 3763 3764 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token, 3765 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq); 3766 3767 if (rc == H_RESOURCE) 3768 rc = ibmvnic_reset_crq(adapter); 3769 3770 if (rc == H_CLOSED) { 3771 dev_warn(dev, "Partner adapter not ready, waiting.\n"); 3772 } else if (rc) { 3773 dev_warn(dev, "Error %d registering sub-crq\n", rc); 3774 goto reg_failed; 3775 } 3776 3777 scrq->adapter = adapter; 3778 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs); 3779 scrq->ind_buf.index = 0; 3780 3781 scrq->ind_buf.indir_arr = 3782 dma_alloc_coherent(dev, 3783 IBMVNIC_IND_ARR_SZ, 3784 &scrq->ind_buf.indir_dma, 3785 GFP_KERNEL); 3786 3787 if (!scrq->ind_buf.indir_arr) 3788 goto indir_failed; 3789 3790 spin_lock_init(&scrq->lock); 3791 3792 netdev_dbg(adapter->netdev, 3793 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n", 3794 scrq->crq_num, scrq->hw_irq, scrq->irq); 3795 3796 return scrq; 3797 3798 indir_failed: 3799 do { 3800 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, 3801 adapter->vdev->unit_address, 3802 scrq->crq_num); 3803 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc)); 3804 reg_failed: 3805 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE, 3806 DMA_BIDIRECTIONAL); 3807 map_failed: 3808 free_pages((unsigned long)scrq->msgs, 2); 3809 zero_page_failed: 3810 kfree(scrq); 3811 3812 return NULL; 3813 } 3814 3815 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free) 3816 { 3817 int i; 3818 3819 if (adapter->tx_scrq) { 3820 for (i = 0; i < adapter->num_active_tx_scrqs; i++) { 3821 if (!adapter->tx_scrq[i]) 3822 continue; 3823 3824 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n", 3825 i); 3826 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]); 3827 if (adapter->tx_scrq[i]->irq) { 3828 free_irq(adapter->tx_scrq[i]->irq, 3829 adapter->tx_scrq[i]); 3830 irq_dispose_mapping(adapter->tx_scrq[i]->irq); 3831 adapter->tx_scrq[i]->irq = 0; 3832 } 3833 3834 release_sub_crq_queue(adapter, adapter->tx_scrq[i], 3835 do_h_free); 3836 } 3837 3838 kfree(adapter->tx_scrq); 3839 adapter->tx_scrq = NULL; 3840 adapter->num_active_tx_scrqs = 0; 3841 } 3842 3843 if (adapter->rx_scrq) { 3844 for (i = 0; i < adapter->num_active_rx_scrqs; i++) { 3845 if (!adapter->rx_scrq[i]) 3846 continue; 3847 3848 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n", 3849 i); 3850 if (adapter->rx_scrq[i]->irq) { 3851 free_irq(adapter->rx_scrq[i]->irq, 3852 adapter->rx_scrq[i]); 3853 irq_dispose_mapping(adapter->rx_scrq[i]->irq); 3854 adapter->rx_scrq[i]->irq = 0; 3855 } 3856 3857 release_sub_crq_queue(adapter, adapter->rx_scrq[i], 3858 do_h_free); 3859 } 3860 3861 kfree(adapter->rx_scrq); 3862 adapter->rx_scrq = NULL; 3863 adapter->num_active_rx_scrqs = 0; 3864 } 3865 } 3866 3867 static int disable_scrq_irq(struct ibmvnic_adapter *adapter, 3868 struct ibmvnic_sub_crq_queue *scrq) 3869 { 3870 struct device *dev = &adapter->vdev->dev; 3871 unsigned long rc; 3872 3873 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address, 3874 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0); 3875 if (rc) 3876 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n", 3877 scrq->hw_irq, rc); 3878 return rc; 3879 } 3880 3881 /* We can not use the IRQ chip EOI handler because that has the 3882 * unintended effect of changing the interrupt priority. 3883 */ 3884 static void ibmvnic_xics_eoi(struct device *dev, struct ibmvnic_sub_crq_queue *scrq) 3885 { 3886 u64 val = 0xff000000 | scrq->hw_irq; 3887 unsigned long rc; 3888 3889 rc = plpar_hcall_norets(H_EOI, val); 3890 if (rc) 3891 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n", val, rc); 3892 } 3893 3894 /* Due to a firmware bug, the hypervisor can send an interrupt to a 3895 * transmit or receive queue just prior to a partition migration. 3896 * Force an EOI after migration. 3897 */ 3898 static void ibmvnic_clear_pending_interrupt(struct device *dev, 3899 struct ibmvnic_sub_crq_queue *scrq) 3900 { 3901 if (!xive_enabled()) 3902 ibmvnic_xics_eoi(dev, scrq); 3903 } 3904 3905 static int enable_scrq_irq(struct ibmvnic_adapter *adapter, 3906 struct ibmvnic_sub_crq_queue *scrq) 3907 { 3908 struct device *dev = &adapter->vdev->dev; 3909 unsigned long rc; 3910 3911 if (scrq->hw_irq > 0x100000000ULL) { 3912 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq); 3913 return 1; 3914 } 3915 3916 if (test_bit(0, &adapter->resetting) && 3917 adapter->reset_reason == VNIC_RESET_MOBILITY) { 3918 ibmvnic_clear_pending_interrupt(dev, scrq); 3919 } 3920 3921 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address, 3922 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0); 3923 if (rc) 3924 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n", 3925 scrq->hw_irq, rc); 3926 return rc; 3927 } 3928 3929 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter, 3930 struct ibmvnic_sub_crq_queue *scrq) 3931 { 3932 struct device *dev = &adapter->vdev->dev; 3933 struct ibmvnic_tx_pool *tx_pool; 3934 struct ibmvnic_tx_buff *txbuff; 3935 struct netdev_queue *txq; 3936 union sub_crq *next; 3937 int index; 3938 int i; 3939 3940 restart_loop: 3941 while (pending_scrq(adapter, scrq)) { 3942 unsigned int pool = scrq->pool_index; 3943 int num_entries = 0; 3944 int total_bytes = 0; 3945 int num_packets = 0; 3946 3947 next = ibmvnic_next_scrq(adapter, scrq); 3948 for (i = 0; i < next->tx_comp.num_comps; i++) { 3949 index = be32_to_cpu(next->tx_comp.correlators[i]); 3950 if (index & IBMVNIC_TSO_POOL_MASK) { 3951 tx_pool = &adapter->tso_pool[pool]; 3952 index &= ~IBMVNIC_TSO_POOL_MASK; 3953 } else { 3954 tx_pool = &adapter->tx_pool[pool]; 3955 } 3956 3957 txbuff = &tx_pool->tx_buff[index]; 3958 num_packets++; 3959 num_entries += txbuff->num_entries; 3960 if (txbuff->skb) { 3961 total_bytes += txbuff->skb->len; 3962 if (next->tx_comp.rcs[i]) { 3963 dev_err(dev, "tx error %x\n", 3964 next->tx_comp.rcs[i]); 3965 dev_kfree_skb_irq(txbuff->skb); 3966 } else { 3967 dev_consume_skb_irq(txbuff->skb); 3968 } 3969 txbuff->skb = NULL; 3970 } else { 3971 netdev_warn(adapter->netdev, 3972 "TX completion received with NULL socket buffer\n"); 3973 } 3974 tx_pool->free_map[tx_pool->producer_index] = index; 3975 tx_pool->producer_index = 3976 (tx_pool->producer_index + 1) % 3977 tx_pool->num_buffers; 3978 } 3979 /* remove tx_comp scrq*/ 3980 next->tx_comp.first = 0; 3981 3982 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index); 3983 netdev_tx_completed_queue(txq, num_packets, total_bytes); 3984 3985 if (atomic_sub_return(num_entries, &scrq->used) <= 3986 (adapter->req_tx_entries_per_subcrq / 2) && 3987 __netif_subqueue_stopped(adapter->netdev, 3988 scrq->pool_index)) { 3989 rcu_read_lock(); 3990 if (adapter->tx_queues_active) { 3991 netif_wake_subqueue(adapter->netdev, 3992 scrq->pool_index); 3993 netdev_dbg(adapter->netdev, 3994 "Started queue %d\n", 3995 scrq->pool_index); 3996 } 3997 rcu_read_unlock(); 3998 } 3999 } 4000 4001 enable_scrq_irq(adapter, scrq); 4002 4003 if (pending_scrq(adapter, scrq)) { 4004 disable_scrq_irq(adapter, scrq); 4005 goto restart_loop; 4006 } 4007 4008 return 0; 4009 } 4010 4011 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance) 4012 { 4013 struct ibmvnic_sub_crq_queue *scrq = instance; 4014 struct ibmvnic_adapter *adapter = scrq->adapter; 4015 4016 disable_scrq_irq(adapter, scrq); 4017 ibmvnic_complete_tx(adapter, scrq); 4018 4019 return IRQ_HANDLED; 4020 } 4021 4022 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance) 4023 { 4024 struct ibmvnic_sub_crq_queue *scrq = instance; 4025 struct ibmvnic_adapter *adapter = scrq->adapter; 4026 4027 /* When booting a kdump kernel we can hit pending interrupts 4028 * prior to completing driver initialization. 4029 */ 4030 if (unlikely(adapter->state != VNIC_OPEN)) 4031 return IRQ_NONE; 4032 4033 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++; 4034 4035 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) { 4036 disable_scrq_irq(adapter, scrq); 4037 __napi_schedule(&adapter->napi[scrq->scrq_num]); 4038 } 4039 4040 return IRQ_HANDLED; 4041 } 4042 4043 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter) 4044 { 4045 struct device *dev = &adapter->vdev->dev; 4046 struct ibmvnic_sub_crq_queue *scrq; 4047 int i = 0, j = 0; 4048 int rc = 0; 4049 4050 for (i = 0; i < adapter->req_tx_queues; i++) { 4051 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n", 4052 i); 4053 scrq = adapter->tx_scrq[i]; 4054 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq); 4055 4056 if (!scrq->irq) { 4057 rc = -EINVAL; 4058 dev_err(dev, "Error mapping irq\n"); 4059 goto req_tx_irq_failed; 4060 } 4061 4062 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d", 4063 adapter->vdev->unit_address, i); 4064 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx, 4065 0, scrq->name, scrq); 4066 4067 if (rc) { 4068 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n", 4069 scrq->irq, rc); 4070 irq_dispose_mapping(scrq->irq); 4071 goto req_tx_irq_failed; 4072 } 4073 } 4074 4075 for (i = 0; i < adapter->req_rx_queues; i++) { 4076 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n", 4077 i); 4078 scrq = adapter->rx_scrq[i]; 4079 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq); 4080 if (!scrq->irq) { 4081 rc = -EINVAL; 4082 dev_err(dev, "Error mapping irq\n"); 4083 goto req_rx_irq_failed; 4084 } 4085 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d", 4086 adapter->vdev->unit_address, i); 4087 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx, 4088 0, scrq->name, scrq); 4089 if (rc) { 4090 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n", 4091 scrq->irq, rc); 4092 irq_dispose_mapping(scrq->irq); 4093 goto req_rx_irq_failed; 4094 } 4095 } 4096 return rc; 4097 4098 req_rx_irq_failed: 4099 for (j = 0; j < i; j++) { 4100 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]); 4101 irq_dispose_mapping(adapter->rx_scrq[j]->irq); 4102 } 4103 i = adapter->req_tx_queues; 4104 req_tx_irq_failed: 4105 for (j = 0; j < i; j++) { 4106 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]); 4107 irq_dispose_mapping(adapter->tx_scrq[j]->irq); 4108 } 4109 release_sub_crqs(adapter, 1); 4110 return rc; 4111 } 4112 4113 static int init_sub_crqs(struct ibmvnic_adapter *adapter) 4114 { 4115 struct device *dev = &adapter->vdev->dev; 4116 struct ibmvnic_sub_crq_queue **allqueues; 4117 int registered_queues = 0; 4118 int total_queues; 4119 int more = 0; 4120 int i; 4121 4122 total_queues = adapter->req_tx_queues + adapter->req_rx_queues; 4123 4124 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL); 4125 if (!allqueues) 4126 return -ENOMEM; 4127 4128 for (i = 0; i < total_queues; i++) { 4129 allqueues[i] = init_sub_crq_queue(adapter); 4130 if (!allqueues[i]) { 4131 dev_warn(dev, "Couldn't allocate all sub-crqs\n"); 4132 break; 4133 } 4134 registered_queues++; 4135 } 4136 4137 /* Make sure we were able to register the minimum number of queues */ 4138 if (registered_queues < 4139 adapter->min_tx_queues + adapter->min_rx_queues) { 4140 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n"); 4141 goto tx_failed; 4142 } 4143 4144 /* Distribute the failed allocated queues*/ 4145 for (i = 0; i < total_queues - registered_queues + more ; i++) { 4146 netdev_dbg(adapter->netdev, "Reducing number of queues\n"); 4147 switch (i % 3) { 4148 case 0: 4149 if (adapter->req_rx_queues > adapter->min_rx_queues) 4150 adapter->req_rx_queues--; 4151 else 4152 more++; 4153 break; 4154 case 1: 4155 if (adapter->req_tx_queues > adapter->min_tx_queues) 4156 adapter->req_tx_queues--; 4157 else 4158 more++; 4159 break; 4160 } 4161 } 4162 4163 adapter->tx_scrq = kcalloc(adapter->req_tx_queues, 4164 sizeof(*adapter->tx_scrq), GFP_KERNEL); 4165 if (!adapter->tx_scrq) 4166 goto tx_failed; 4167 4168 for (i = 0; i < adapter->req_tx_queues; i++) { 4169 adapter->tx_scrq[i] = allqueues[i]; 4170 adapter->tx_scrq[i]->pool_index = i; 4171 adapter->num_active_tx_scrqs++; 4172 } 4173 4174 adapter->rx_scrq = kcalloc(adapter->req_rx_queues, 4175 sizeof(*adapter->rx_scrq), GFP_KERNEL); 4176 if (!adapter->rx_scrq) 4177 goto rx_failed; 4178 4179 for (i = 0; i < adapter->req_rx_queues; i++) { 4180 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues]; 4181 adapter->rx_scrq[i]->scrq_num = i; 4182 adapter->num_active_rx_scrqs++; 4183 } 4184 4185 kfree(allqueues); 4186 return 0; 4187 4188 rx_failed: 4189 kfree(adapter->tx_scrq); 4190 adapter->tx_scrq = NULL; 4191 tx_failed: 4192 for (i = 0; i < registered_queues; i++) 4193 release_sub_crq_queue(adapter, allqueues[i], 1); 4194 kfree(allqueues); 4195 return -ENOMEM; 4196 } 4197 4198 static void send_request_cap(struct ibmvnic_adapter *adapter, int retry) 4199 { 4200 struct device *dev = &adapter->vdev->dev; 4201 union ibmvnic_crq crq; 4202 int max_entries; 4203 int cap_reqs; 4204 4205 /* We send out 6 or 7 REQUEST_CAPABILITY CRQs below (depending on 4206 * the PROMISC flag). Initialize this count upfront. When the tasklet 4207 * receives a response to all of these, it will send the next protocol 4208 * message (QUERY_IP_OFFLOAD). 4209 */ 4210 if (!(adapter->netdev->flags & IFF_PROMISC) || 4211 adapter->promisc_supported) 4212 cap_reqs = 7; 4213 else 4214 cap_reqs = 6; 4215 4216 if (!retry) { 4217 /* Sub-CRQ entries are 32 byte long */ 4218 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4); 4219 4220 atomic_set(&adapter->running_cap_crqs, cap_reqs); 4221 4222 if (adapter->min_tx_entries_per_subcrq > entries_page || 4223 adapter->min_rx_add_entries_per_subcrq > entries_page) { 4224 dev_err(dev, "Fatal, invalid entries per sub-crq\n"); 4225 return; 4226 } 4227 4228 if (adapter->desired.mtu) 4229 adapter->req_mtu = adapter->desired.mtu; 4230 else 4231 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN; 4232 4233 if (!adapter->desired.tx_entries) 4234 adapter->desired.tx_entries = 4235 adapter->max_tx_entries_per_subcrq; 4236 if (!adapter->desired.rx_entries) 4237 adapter->desired.rx_entries = 4238 adapter->max_rx_add_entries_per_subcrq; 4239 4240 max_entries = IBMVNIC_LTB_SET_SIZE / 4241 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN); 4242 4243 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) * 4244 adapter->desired.tx_entries > IBMVNIC_LTB_SET_SIZE) { 4245 adapter->desired.tx_entries = max_entries; 4246 } 4247 4248 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) * 4249 adapter->desired.rx_entries > IBMVNIC_LTB_SET_SIZE) { 4250 adapter->desired.rx_entries = max_entries; 4251 } 4252 4253 if (adapter->desired.tx_entries) 4254 adapter->req_tx_entries_per_subcrq = 4255 adapter->desired.tx_entries; 4256 else 4257 adapter->req_tx_entries_per_subcrq = 4258 adapter->max_tx_entries_per_subcrq; 4259 4260 if (adapter->desired.rx_entries) 4261 adapter->req_rx_add_entries_per_subcrq = 4262 adapter->desired.rx_entries; 4263 else 4264 adapter->req_rx_add_entries_per_subcrq = 4265 adapter->max_rx_add_entries_per_subcrq; 4266 4267 if (adapter->desired.tx_queues) 4268 adapter->req_tx_queues = 4269 adapter->desired.tx_queues; 4270 else 4271 adapter->req_tx_queues = 4272 adapter->opt_tx_comp_sub_queues; 4273 4274 if (adapter->desired.rx_queues) 4275 adapter->req_rx_queues = 4276 adapter->desired.rx_queues; 4277 else 4278 adapter->req_rx_queues = 4279 adapter->opt_rx_comp_queues; 4280 4281 adapter->req_rx_add_queues = adapter->max_rx_add_queues; 4282 } else { 4283 atomic_add(cap_reqs, &adapter->running_cap_crqs); 4284 } 4285 memset(&crq, 0, sizeof(crq)); 4286 crq.request_capability.first = IBMVNIC_CRQ_CMD; 4287 crq.request_capability.cmd = REQUEST_CAPABILITY; 4288 4289 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES); 4290 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues); 4291 cap_reqs--; 4292 ibmvnic_send_crq(adapter, &crq); 4293 4294 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES); 4295 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues); 4296 cap_reqs--; 4297 ibmvnic_send_crq(adapter, &crq); 4298 4299 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES); 4300 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues); 4301 cap_reqs--; 4302 ibmvnic_send_crq(adapter, &crq); 4303 4304 crq.request_capability.capability = 4305 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ); 4306 crq.request_capability.number = 4307 cpu_to_be64(adapter->req_tx_entries_per_subcrq); 4308 cap_reqs--; 4309 ibmvnic_send_crq(adapter, &crq); 4310 4311 crq.request_capability.capability = 4312 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ); 4313 crq.request_capability.number = 4314 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq); 4315 cap_reqs--; 4316 ibmvnic_send_crq(adapter, &crq); 4317 4318 crq.request_capability.capability = cpu_to_be16(REQ_MTU); 4319 crq.request_capability.number = cpu_to_be64(adapter->req_mtu); 4320 cap_reqs--; 4321 ibmvnic_send_crq(adapter, &crq); 4322 4323 if (adapter->netdev->flags & IFF_PROMISC) { 4324 if (adapter->promisc_supported) { 4325 crq.request_capability.capability = 4326 cpu_to_be16(PROMISC_REQUESTED); 4327 crq.request_capability.number = cpu_to_be64(1); 4328 cap_reqs--; 4329 ibmvnic_send_crq(adapter, &crq); 4330 } 4331 } else { 4332 crq.request_capability.capability = 4333 cpu_to_be16(PROMISC_REQUESTED); 4334 crq.request_capability.number = cpu_to_be64(0); 4335 cap_reqs--; 4336 ibmvnic_send_crq(adapter, &crq); 4337 } 4338 4339 /* Keep at end to catch any discrepancy between expected and actual 4340 * CRQs sent. 4341 */ 4342 WARN_ON(cap_reqs != 0); 4343 } 4344 4345 static int pending_scrq(struct ibmvnic_adapter *adapter, 4346 struct ibmvnic_sub_crq_queue *scrq) 4347 { 4348 union sub_crq *entry = &scrq->msgs[scrq->cur]; 4349 int rc; 4350 4351 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP); 4352 4353 /* Ensure that the SCRQ valid flag is loaded prior to loading the 4354 * contents of the SCRQ descriptor 4355 */ 4356 dma_rmb(); 4357 4358 return rc; 4359 } 4360 4361 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter, 4362 struct ibmvnic_sub_crq_queue *scrq) 4363 { 4364 union sub_crq *entry; 4365 unsigned long flags; 4366 4367 spin_lock_irqsave(&scrq->lock, flags); 4368 entry = &scrq->msgs[scrq->cur]; 4369 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) { 4370 if (++scrq->cur == scrq->size) 4371 scrq->cur = 0; 4372 } else { 4373 entry = NULL; 4374 } 4375 spin_unlock_irqrestore(&scrq->lock, flags); 4376 4377 /* Ensure that the SCRQ valid flag is loaded prior to loading the 4378 * contents of the SCRQ descriptor 4379 */ 4380 dma_rmb(); 4381 4382 return entry; 4383 } 4384 4385 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter) 4386 { 4387 struct ibmvnic_crq_queue *queue = &adapter->crq; 4388 union ibmvnic_crq *crq; 4389 4390 crq = &queue->msgs[queue->cur]; 4391 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) { 4392 if (++queue->cur == queue->size) 4393 queue->cur = 0; 4394 } else { 4395 crq = NULL; 4396 } 4397 4398 return crq; 4399 } 4400 4401 static void print_subcrq_error(struct device *dev, int rc, const char *func) 4402 { 4403 switch (rc) { 4404 case H_PARAMETER: 4405 dev_warn_ratelimited(dev, 4406 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n", 4407 func, rc); 4408 break; 4409 case H_CLOSED: 4410 dev_warn_ratelimited(dev, 4411 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n", 4412 func, rc); 4413 break; 4414 default: 4415 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc); 4416 break; 4417 } 4418 } 4419 4420 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter, 4421 u64 remote_handle, u64 ioba, u64 num_entries) 4422 { 4423 unsigned int ua = adapter->vdev->unit_address; 4424 struct device *dev = &adapter->vdev->dev; 4425 int rc; 4426 4427 /* Make sure the hypervisor sees the complete request */ 4428 dma_wmb(); 4429 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua, 4430 cpu_to_be64(remote_handle), 4431 ioba, num_entries); 4432 4433 if (rc) 4434 print_subcrq_error(dev, rc, __func__); 4435 4436 return rc; 4437 } 4438 4439 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter, 4440 union ibmvnic_crq *crq) 4441 { 4442 unsigned int ua = adapter->vdev->unit_address; 4443 struct device *dev = &adapter->vdev->dev; 4444 u64 *u64_crq = (u64 *)crq; 4445 int rc; 4446 4447 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n", 4448 (unsigned long)cpu_to_be64(u64_crq[0]), 4449 (unsigned long)cpu_to_be64(u64_crq[1])); 4450 4451 if (!adapter->crq.active && 4452 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) { 4453 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n"); 4454 return -EINVAL; 4455 } 4456 4457 /* Make sure the hypervisor sees the complete request */ 4458 dma_wmb(); 4459 4460 rc = plpar_hcall_norets(H_SEND_CRQ, ua, 4461 cpu_to_be64(u64_crq[0]), 4462 cpu_to_be64(u64_crq[1])); 4463 4464 if (rc) { 4465 if (rc == H_CLOSED) { 4466 dev_warn(dev, "CRQ Queue closed\n"); 4467 /* do not reset, report the fail, wait for passive init from server */ 4468 } 4469 4470 dev_warn(dev, "Send error (rc=%d)\n", rc); 4471 } 4472 4473 return rc; 4474 } 4475 4476 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter) 4477 { 4478 struct device *dev = &adapter->vdev->dev; 4479 union ibmvnic_crq crq; 4480 int retries = 100; 4481 int rc; 4482 4483 memset(&crq, 0, sizeof(crq)); 4484 crq.generic.first = IBMVNIC_CRQ_INIT_CMD; 4485 crq.generic.cmd = IBMVNIC_CRQ_INIT; 4486 netdev_dbg(adapter->netdev, "Sending CRQ init\n"); 4487 4488 do { 4489 rc = ibmvnic_send_crq(adapter, &crq); 4490 if (rc != H_CLOSED) 4491 break; 4492 retries--; 4493 msleep(50); 4494 4495 } while (retries > 0); 4496 4497 if (rc) { 4498 dev_err(dev, "Failed to send init request, rc = %d\n", rc); 4499 return rc; 4500 } 4501 4502 return 0; 4503 } 4504 4505 struct vnic_login_client_data { 4506 u8 type; 4507 __be16 len; 4508 char name[]; 4509 } __packed; 4510 4511 static int vnic_client_data_len(struct ibmvnic_adapter *adapter) 4512 { 4513 int len; 4514 4515 /* Calculate the amount of buffer space needed for the 4516 * vnic client data in the login buffer. There are four entries, 4517 * OS name, LPAR name, device name, and a null last entry. 4518 */ 4519 len = 4 * sizeof(struct vnic_login_client_data); 4520 len += 6; /* "Linux" plus NULL */ 4521 len += strlen(utsname()->nodename) + 1; 4522 len += strlen(adapter->netdev->name) + 1; 4523 4524 return len; 4525 } 4526 4527 static void vnic_add_client_data(struct ibmvnic_adapter *adapter, 4528 struct vnic_login_client_data *vlcd) 4529 { 4530 const char *os_name = "Linux"; 4531 int len; 4532 4533 /* Type 1 - LPAR OS */ 4534 vlcd->type = 1; 4535 len = strlen(os_name) + 1; 4536 vlcd->len = cpu_to_be16(len); 4537 strscpy(vlcd->name, os_name, len); 4538 vlcd = (struct vnic_login_client_data *)(vlcd->name + len); 4539 4540 /* Type 2 - LPAR name */ 4541 vlcd->type = 2; 4542 len = strlen(utsname()->nodename) + 1; 4543 vlcd->len = cpu_to_be16(len); 4544 strscpy(vlcd->name, utsname()->nodename, len); 4545 vlcd = (struct vnic_login_client_data *)(vlcd->name + len); 4546 4547 /* Type 3 - device name */ 4548 vlcd->type = 3; 4549 len = strlen(adapter->netdev->name) + 1; 4550 vlcd->len = cpu_to_be16(len); 4551 strscpy(vlcd->name, adapter->netdev->name, len); 4552 } 4553 4554 static int send_login(struct ibmvnic_adapter *adapter) 4555 { 4556 struct ibmvnic_login_rsp_buffer *login_rsp_buffer; 4557 struct ibmvnic_login_buffer *login_buffer; 4558 struct device *dev = &adapter->vdev->dev; 4559 struct vnic_login_client_data *vlcd; 4560 dma_addr_t rsp_buffer_token; 4561 dma_addr_t buffer_token; 4562 size_t rsp_buffer_size; 4563 union ibmvnic_crq crq; 4564 int client_data_len; 4565 size_t buffer_size; 4566 __be64 *tx_list_p; 4567 __be64 *rx_list_p; 4568 int rc; 4569 int i; 4570 4571 if (!adapter->tx_scrq || !adapter->rx_scrq) { 4572 netdev_err(adapter->netdev, 4573 "RX or TX queues are not allocated, device login failed\n"); 4574 return -ENOMEM; 4575 } 4576 4577 release_login_buffer(adapter); 4578 release_login_rsp_buffer(adapter); 4579 4580 client_data_len = vnic_client_data_len(adapter); 4581 4582 buffer_size = 4583 sizeof(struct ibmvnic_login_buffer) + 4584 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) + 4585 client_data_len; 4586 4587 login_buffer = kzalloc(buffer_size, GFP_ATOMIC); 4588 if (!login_buffer) 4589 goto buf_alloc_failed; 4590 4591 buffer_token = dma_map_single(dev, login_buffer, buffer_size, 4592 DMA_TO_DEVICE); 4593 if (dma_mapping_error(dev, buffer_token)) { 4594 dev_err(dev, "Couldn't map login buffer\n"); 4595 goto buf_map_failed; 4596 } 4597 4598 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) + 4599 sizeof(u64) * adapter->req_tx_queues + 4600 sizeof(u64) * adapter->req_rx_queues + 4601 sizeof(u64) * adapter->req_rx_queues + 4602 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS; 4603 4604 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC); 4605 if (!login_rsp_buffer) 4606 goto buf_rsp_alloc_failed; 4607 4608 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer, 4609 rsp_buffer_size, DMA_FROM_DEVICE); 4610 if (dma_mapping_error(dev, rsp_buffer_token)) { 4611 dev_err(dev, "Couldn't map login rsp buffer\n"); 4612 goto buf_rsp_map_failed; 4613 } 4614 4615 adapter->login_buf = login_buffer; 4616 adapter->login_buf_token = buffer_token; 4617 adapter->login_buf_sz = buffer_size; 4618 adapter->login_rsp_buf = login_rsp_buffer; 4619 adapter->login_rsp_buf_token = rsp_buffer_token; 4620 adapter->login_rsp_buf_sz = rsp_buffer_size; 4621 4622 login_buffer->len = cpu_to_be32(buffer_size); 4623 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB); 4624 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues); 4625 login_buffer->off_txcomp_subcrqs = 4626 cpu_to_be32(sizeof(struct ibmvnic_login_buffer)); 4627 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues); 4628 login_buffer->off_rxcomp_subcrqs = 4629 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) + 4630 sizeof(u64) * adapter->req_tx_queues); 4631 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token); 4632 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size); 4633 4634 tx_list_p = (__be64 *)((char *)login_buffer + 4635 sizeof(struct ibmvnic_login_buffer)); 4636 rx_list_p = (__be64 *)((char *)login_buffer + 4637 sizeof(struct ibmvnic_login_buffer) + 4638 sizeof(u64) * adapter->req_tx_queues); 4639 4640 for (i = 0; i < adapter->req_tx_queues; i++) { 4641 if (adapter->tx_scrq[i]) { 4642 tx_list_p[i] = 4643 cpu_to_be64(adapter->tx_scrq[i]->crq_num); 4644 } 4645 } 4646 4647 for (i = 0; i < adapter->req_rx_queues; i++) { 4648 if (adapter->rx_scrq[i]) { 4649 rx_list_p[i] = 4650 cpu_to_be64(adapter->rx_scrq[i]->crq_num); 4651 } 4652 } 4653 4654 /* Insert vNIC login client data */ 4655 vlcd = (struct vnic_login_client_data *) 4656 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues)); 4657 login_buffer->client_data_offset = 4658 cpu_to_be32((char *)vlcd - (char *)login_buffer); 4659 login_buffer->client_data_len = cpu_to_be32(client_data_len); 4660 4661 vnic_add_client_data(adapter, vlcd); 4662 4663 netdev_dbg(adapter->netdev, "Login Buffer:\n"); 4664 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) { 4665 netdev_dbg(adapter->netdev, "%016lx\n", 4666 ((unsigned long *)(adapter->login_buf))[i]); 4667 } 4668 4669 memset(&crq, 0, sizeof(crq)); 4670 crq.login.first = IBMVNIC_CRQ_CMD; 4671 crq.login.cmd = LOGIN; 4672 crq.login.ioba = cpu_to_be32(buffer_token); 4673 crq.login.len = cpu_to_be32(buffer_size); 4674 4675 adapter->login_pending = true; 4676 rc = ibmvnic_send_crq(adapter, &crq); 4677 if (rc) { 4678 adapter->login_pending = false; 4679 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc); 4680 goto buf_rsp_map_failed; 4681 } 4682 4683 return 0; 4684 4685 buf_rsp_map_failed: 4686 kfree(login_rsp_buffer); 4687 adapter->login_rsp_buf = NULL; 4688 buf_rsp_alloc_failed: 4689 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE); 4690 buf_map_failed: 4691 kfree(login_buffer); 4692 adapter->login_buf = NULL; 4693 buf_alloc_failed: 4694 return -ENOMEM; 4695 } 4696 4697 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr, 4698 u32 len, u8 map_id) 4699 { 4700 union ibmvnic_crq crq; 4701 4702 memset(&crq, 0, sizeof(crq)); 4703 crq.request_map.first = IBMVNIC_CRQ_CMD; 4704 crq.request_map.cmd = REQUEST_MAP; 4705 crq.request_map.map_id = map_id; 4706 crq.request_map.ioba = cpu_to_be32(addr); 4707 crq.request_map.len = cpu_to_be32(len); 4708 return ibmvnic_send_crq(adapter, &crq); 4709 } 4710 4711 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id) 4712 { 4713 union ibmvnic_crq crq; 4714 4715 memset(&crq, 0, sizeof(crq)); 4716 crq.request_unmap.first = IBMVNIC_CRQ_CMD; 4717 crq.request_unmap.cmd = REQUEST_UNMAP; 4718 crq.request_unmap.map_id = map_id; 4719 return ibmvnic_send_crq(adapter, &crq); 4720 } 4721 4722 static void send_query_map(struct ibmvnic_adapter *adapter) 4723 { 4724 union ibmvnic_crq crq; 4725 4726 memset(&crq, 0, sizeof(crq)); 4727 crq.query_map.first = IBMVNIC_CRQ_CMD; 4728 crq.query_map.cmd = QUERY_MAP; 4729 ibmvnic_send_crq(adapter, &crq); 4730 } 4731 4732 /* Send a series of CRQs requesting various capabilities of the VNIC server */ 4733 static void send_query_cap(struct ibmvnic_adapter *adapter) 4734 { 4735 union ibmvnic_crq crq; 4736 int cap_reqs; 4737 4738 /* We send out 25 QUERY_CAPABILITY CRQs below. Initialize this count 4739 * upfront. When the tasklet receives a response to all of these, it 4740 * can send out the next protocol messaage (REQUEST_CAPABILITY). 4741 */ 4742 cap_reqs = 25; 4743 4744 atomic_set(&adapter->running_cap_crqs, cap_reqs); 4745 4746 memset(&crq, 0, sizeof(crq)); 4747 crq.query_capability.first = IBMVNIC_CRQ_CMD; 4748 crq.query_capability.cmd = QUERY_CAPABILITY; 4749 4750 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES); 4751 ibmvnic_send_crq(adapter, &crq); 4752 cap_reqs--; 4753 4754 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES); 4755 ibmvnic_send_crq(adapter, &crq); 4756 cap_reqs--; 4757 4758 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES); 4759 ibmvnic_send_crq(adapter, &crq); 4760 cap_reqs--; 4761 4762 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES); 4763 ibmvnic_send_crq(adapter, &crq); 4764 cap_reqs--; 4765 4766 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES); 4767 ibmvnic_send_crq(adapter, &crq); 4768 cap_reqs--; 4769 4770 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES); 4771 ibmvnic_send_crq(adapter, &crq); 4772 cap_reqs--; 4773 4774 crq.query_capability.capability = 4775 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ); 4776 ibmvnic_send_crq(adapter, &crq); 4777 cap_reqs--; 4778 4779 crq.query_capability.capability = 4780 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ); 4781 ibmvnic_send_crq(adapter, &crq); 4782 cap_reqs--; 4783 4784 crq.query_capability.capability = 4785 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ); 4786 ibmvnic_send_crq(adapter, &crq); 4787 cap_reqs--; 4788 4789 crq.query_capability.capability = 4790 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ); 4791 ibmvnic_send_crq(adapter, &crq); 4792 cap_reqs--; 4793 4794 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD); 4795 ibmvnic_send_crq(adapter, &crq); 4796 cap_reqs--; 4797 4798 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED); 4799 ibmvnic_send_crq(adapter, &crq); 4800 cap_reqs--; 4801 4802 crq.query_capability.capability = cpu_to_be16(MIN_MTU); 4803 ibmvnic_send_crq(adapter, &crq); 4804 cap_reqs--; 4805 4806 crq.query_capability.capability = cpu_to_be16(MAX_MTU); 4807 ibmvnic_send_crq(adapter, &crq); 4808 cap_reqs--; 4809 4810 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS); 4811 ibmvnic_send_crq(adapter, &crq); 4812 cap_reqs--; 4813 4814 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION); 4815 ibmvnic_send_crq(adapter, &crq); 4816 cap_reqs--; 4817 4818 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION); 4819 ibmvnic_send_crq(adapter, &crq); 4820 cap_reqs--; 4821 4822 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES); 4823 ibmvnic_send_crq(adapter, &crq); 4824 cap_reqs--; 4825 4826 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED); 4827 ibmvnic_send_crq(adapter, &crq); 4828 cap_reqs--; 4829 4830 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES); 4831 ibmvnic_send_crq(adapter, &crq); 4832 cap_reqs--; 4833 4834 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES); 4835 ibmvnic_send_crq(adapter, &crq); 4836 cap_reqs--; 4837 4838 crq.query_capability.capability = 4839 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q); 4840 ibmvnic_send_crq(adapter, &crq); 4841 cap_reqs--; 4842 4843 crq.query_capability.capability = 4844 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ); 4845 ibmvnic_send_crq(adapter, &crq); 4846 cap_reqs--; 4847 4848 crq.query_capability.capability = 4849 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ); 4850 ibmvnic_send_crq(adapter, &crq); 4851 cap_reqs--; 4852 4853 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ); 4854 4855 ibmvnic_send_crq(adapter, &crq); 4856 cap_reqs--; 4857 4858 /* Keep at end to catch any discrepancy between expected and actual 4859 * CRQs sent. 4860 */ 4861 WARN_ON(cap_reqs != 0); 4862 } 4863 4864 static void send_query_ip_offload(struct ibmvnic_adapter *adapter) 4865 { 4866 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer); 4867 struct device *dev = &adapter->vdev->dev; 4868 union ibmvnic_crq crq; 4869 4870 adapter->ip_offload_tok = 4871 dma_map_single(dev, 4872 &adapter->ip_offload_buf, 4873 buf_sz, 4874 DMA_FROM_DEVICE); 4875 4876 if (dma_mapping_error(dev, adapter->ip_offload_tok)) { 4877 if (!firmware_has_feature(FW_FEATURE_CMO)) 4878 dev_err(dev, "Couldn't map offload buffer\n"); 4879 return; 4880 } 4881 4882 memset(&crq, 0, sizeof(crq)); 4883 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD; 4884 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD; 4885 crq.query_ip_offload.len = cpu_to_be32(buf_sz); 4886 crq.query_ip_offload.ioba = 4887 cpu_to_be32(adapter->ip_offload_tok); 4888 4889 ibmvnic_send_crq(adapter, &crq); 4890 } 4891 4892 static void send_control_ip_offload(struct ibmvnic_adapter *adapter) 4893 { 4894 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl; 4895 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf; 4896 struct device *dev = &adapter->vdev->dev; 4897 netdev_features_t old_hw_features = 0; 4898 union ibmvnic_crq crq; 4899 4900 adapter->ip_offload_ctrl_tok = 4901 dma_map_single(dev, 4902 ctrl_buf, 4903 sizeof(adapter->ip_offload_ctrl), 4904 DMA_TO_DEVICE); 4905 4906 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) { 4907 dev_err(dev, "Couldn't map ip offload control buffer\n"); 4908 return; 4909 } 4910 4911 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl)); 4912 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB); 4913 ctrl_buf->ipv4_chksum = buf->ipv4_chksum; 4914 ctrl_buf->ipv6_chksum = buf->ipv6_chksum; 4915 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum; 4916 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum; 4917 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum; 4918 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum; 4919 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4; 4920 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6; 4921 4922 /* large_rx disabled for now, additional features needed */ 4923 ctrl_buf->large_rx_ipv4 = 0; 4924 ctrl_buf->large_rx_ipv6 = 0; 4925 4926 if (adapter->state != VNIC_PROBING) { 4927 old_hw_features = adapter->netdev->hw_features; 4928 adapter->netdev->hw_features = 0; 4929 } 4930 4931 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO; 4932 4933 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum) 4934 adapter->netdev->hw_features |= NETIF_F_IP_CSUM; 4935 4936 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum) 4937 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM; 4938 4939 if ((adapter->netdev->features & 4940 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))) 4941 adapter->netdev->hw_features |= NETIF_F_RXCSUM; 4942 4943 if (buf->large_tx_ipv4) 4944 adapter->netdev->hw_features |= NETIF_F_TSO; 4945 if (buf->large_tx_ipv6) 4946 adapter->netdev->hw_features |= NETIF_F_TSO6; 4947 4948 if (adapter->state == VNIC_PROBING) { 4949 adapter->netdev->features |= adapter->netdev->hw_features; 4950 } else if (old_hw_features != adapter->netdev->hw_features) { 4951 netdev_features_t tmp = 0; 4952 4953 /* disable features no longer supported */ 4954 adapter->netdev->features &= adapter->netdev->hw_features; 4955 /* turn on features now supported if previously enabled */ 4956 tmp = (old_hw_features ^ adapter->netdev->hw_features) & 4957 adapter->netdev->hw_features; 4958 adapter->netdev->features |= 4959 tmp & adapter->netdev->wanted_features; 4960 } 4961 4962 memset(&crq, 0, sizeof(crq)); 4963 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD; 4964 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD; 4965 crq.control_ip_offload.len = 4966 cpu_to_be32(sizeof(adapter->ip_offload_ctrl)); 4967 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok); 4968 ibmvnic_send_crq(adapter, &crq); 4969 } 4970 4971 static void handle_vpd_size_rsp(union ibmvnic_crq *crq, 4972 struct ibmvnic_adapter *adapter) 4973 { 4974 struct device *dev = &adapter->vdev->dev; 4975 4976 if (crq->get_vpd_size_rsp.rc.code) { 4977 dev_err(dev, "Error retrieving VPD size, rc=%x\n", 4978 crq->get_vpd_size_rsp.rc.code); 4979 complete(&adapter->fw_done); 4980 return; 4981 } 4982 4983 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len); 4984 complete(&adapter->fw_done); 4985 } 4986 4987 static void handle_vpd_rsp(union ibmvnic_crq *crq, 4988 struct ibmvnic_adapter *adapter) 4989 { 4990 struct device *dev = &adapter->vdev->dev; 4991 unsigned char *substr = NULL; 4992 u8 fw_level_len = 0; 4993 4994 memset(adapter->fw_version, 0, 32); 4995 4996 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len, 4997 DMA_FROM_DEVICE); 4998 4999 if (crq->get_vpd_rsp.rc.code) { 5000 dev_err(dev, "Error retrieving VPD from device, rc=%x\n", 5001 crq->get_vpd_rsp.rc.code); 5002 goto complete; 5003 } 5004 5005 /* get the position of the firmware version info 5006 * located after the ASCII 'RM' substring in the buffer 5007 */ 5008 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len); 5009 if (!substr) { 5010 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n"); 5011 goto complete; 5012 } 5013 5014 /* get length of firmware level ASCII substring */ 5015 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) { 5016 fw_level_len = *(substr + 2); 5017 } else { 5018 dev_info(dev, "Length of FW substr extrapolated VDP buff\n"); 5019 goto complete; 5020 } 5021 5022 /* copy firmware version string from vpd into adapter */ 5023 if ((substr + 3 + fw_level_len) < 5024 (adapter->vpd->buff + adapter->vpd->len)) { 5025 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len); 5026 } else { 5027 dev_info(dev, "FW substr extrapolated VPD buff\n"); 5028 } 5029 5030 complete: 5031 if (adapter->fw_version[0] == '\0') 5032 strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version)); 5033 complete(&adapter->fw_done); 5034 } 5035 5036 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter) 5037 { 5038 struct device *dev = &adapter->vdev->dev; 5039 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf; 5040 int i; 5041 5042 dma_unmap_single(dev, adapter->ip_offload_tok, 5043 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE); 5044 5045 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n"); 5046 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++) 5047 netdev_dbg(adapter->netdev, "%016lx\n", 5048 ((unsigned long *)(buf))[i]); 5049 5050 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum); 5051 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum); 5052 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n", 5053 buf->tcp_ipv4_chksum); 5054 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n", 5055 buf->tcp_ipv6_chksum); 5056 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n", 5057 buf->udp_ipv4_chksum); 5058 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n", 5059 buf->udp_ipv6_chksum); 5060 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n", 5061 buf->large_tx_ipv4); 5062 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n", 5063 buf->large_tx_ipv6); 5064 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n", 5065 buf->large_rx_ipv4); 5066 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n", 5067 buf->large_rx_ipv6); 5068 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n", 5069 buf->max_ipv4_header_size); 5070 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n", 5071 buf->max_ipv6_header_size); 5072 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n", 5073 buf->max_tcp_header_size); 5074 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n", 5075 buf->max_udp_header_size); 5076 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n", 5077 buf->max_large_tx_size); 5078 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n", 5079 buf->max_large_rx_size); 5080 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n", 5081 buf->ipv6_extension_header); 5082 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n", 5083 buf->tcp_pseudosum_req); 5084 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n", 5085 buf->num_ipv6_ext_headers); 5086 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n", 5087 buf->off_ipv6_ext_headers); 5088 5089 send_control_ip_offload(adapter); 5090 } 5091 5092 static const char *ibmvnic_fw_err_cause(u16 cause) 5093 { 5094 switch (cause) { 5095 case ADAPTER_PROBLEM: 5096 return "adapter problem"; 5097 case BUS_PROBLEM: 5098 return "bus problem"; 5099 case FW_PROBLEM: 5100 return "firmware problem"; 5101 case DD_PROBLEM: 5102 return "device driver problem"; 5103 case EEH_RECOVERY: 5104 return "EEH recovery"; 5105 case FW_UPDATED: 5106 return "firmware updated"; 5107 case LOW_MEMORY: 5108 return "low Memory"; 5109 default: 5110 return "unknown"; 5111 } 5112 } 5113 5114 static void handle_error_indication(union ibmvnic_crq *crq, 5115 struct ibmvnic_adapter *adapter) 5116 { 5117 struct device *dev = &adapter->vdev->dev; 5118 u16 cause; 5119 5120 cause = be16_to_cpu(crq->error_indication.error_cause); 5121 5122 dev_warn_ratelimited(dev, 5123 "Firmware reports %serror, cause: %s. Starting recovery...\n", 5124 crq->error_indication.flags 5125 & IBMVNIC_FATAL_ERROR ? "FATAL " : "", 5126 ibmvnic_fw_err_cause(cause)); 5127 5128 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR) 5129 ibmvnic_reset(adapter, VNIC_RESET_FATAL); 5130 else 5131 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL); 5132 } 5133 5134 static int handle_change_mac_rsp(union ibmvnic_crq *crq, 5135 struct ibmvnic_adapter *adapter) 5136 { 5137 struct net_device *netdev = adapter->netdev; 5138 struct device *dev = &adapter->vdev->dev; 5139 long rc; 5140 5141 rc = crq->change_mac_addr_rsp.rc.code; 5142 if (rc) { 5143 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc); 5144 goto out; 5145 } 5146 /* crq->change_mac_addr.mac_addr is the requested one 5147 * crq->change_mac_addr_rsp.mac_addr is the returned valid one. 5148 */ 5149 eth_hw_addr_set(netdev, &crq->change_mac_addr_rsp.mac_addr[0]); 5150 ether_addr_copy(adapter->mac_addr, 5151 &crq->change_mac_addr_rsp.mac_addr[0]); 5152 out: 5153 complete(&adapter->fw_done); 5154 return rc; 5155 } 5156 5157 static void handle_request_cap_rsp(union ibmvnic_crq *crq, 5158 struct ibmvnic_adapter *adapter) 5159 { 5160 struct device *dev = &adapter->vdev->dev; 5161 u64 *req_value; 5162 char *name; 5163 5164 atomic_dec(&adapter->running_cap_crqs); 5165 netdev_dbg(adapter->netdev, "Outstanding request-caps: %d\n", 5166 atomic_read(&adapter->running_cap_crqs)); 5167 switch (be16_to_cpu(crq->request_capability_rsp.capability)) { 5168 case REQ_TX_QUEUES: 5169 req_value = &adapter->req_tx_queues; 5170 name = "tx"; 5171 break; 5172 case REQ_RX_QUEUES: 5173 req_value = &adapter->req_rx_queues; 5174 name = "rx"; 5175 break; 5176 case REQ_RX_ADD_QUEUES: 5177 req_value = &adapter->req_rx_add_queues; 5178 name = "rx_add"; 5179 break; 5180 case REQ_TX_ENTRIES_PER_SUBCRQ: 5181 req_value = &adapter->req_tx_entries_per_subcrq; 5182 name = "tx_entries_per_subcrq"; 5183 break; 5184 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ: 5185 req_value = &adapter->req_rx_add_entries_per_subcrq; 5186 name = "rx_add_entries_per_subcrq"; 5187 break; 5188 case REQ_MTU: 5189 req_value = &adapter->req_mtu; 5190 name = "mtu"; 5191 break; 5192 case PROMISC_REQUESTED: 5193 req_value = &adapter->promisc; 5194 name = "promisc"; 5195 break; 5196 default: 5197 dev_err(dev, "Got invalid cap request rsp %d\n", 5198 crq->request_capability.capability); 5199 return; 5200 } 5201 5202 switch (crq->request_capability_rsp.rc.code) { 5203 case SUCCESS: 5204 break; 5205 case PARTIALSUCCESS: 5206 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n", 5207 *req_value, 5208 (long)be64_to_cpu(crq->request_capability_rsp.number), 5209 name); 5210 5211 if (be16_to_cpu(crq->request_capability_rsp.capability) == 5212 REQ_MTU) { 5213 pr_err("mtu of %llu is not supported. Reverting.\n", 5214 *req_value); 5215 *req_value = adapter->fallback.mtu; 5216 } else { 5217 *req_value = 5218 be64_to_cpu(crq->request_capability_rsp.number); 5219 } 5220 5221 send_request_cap(adapter, 1); 5222 return; 5223 default: 5224 dev_err(dev, "Error %d in request cap rsp\n", 5225 crq->request_capability_rsp.rc.code); 5226 return; 5227 } 5228 5229 /* Done receiving requested capabilities, query IP offload support */ 5230 if (atomic_read(&adapter->running_cap_crqs) == 0) 5231 send_query_ip_offload(adapter); 5232 } 5233 5234 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq, 5235 struct ibmvnic_adapter *adapter) 5236 { 5237 struct device *dev = &adapter->vdev->dev; 5238 struct net_device *netdev = adapter->netdev; 5239 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf; 5240 struct ibmvnic_login_buffer *login = adapter->login_buf; 5241 u64 *tx_handle_array; 5242 u64 *rx_handle_array; 5243 int num_tx_pools; 5244 int num_rx_pools; 5245 u64 *size_array; 5246 int i; 5247 5248 /* CHECK: Test/set of login_pending does not need to be atomic 5249 * because only ibmvnic_tasklet tests/clears this. 5250 */ 5251 if (!adapter->login_pending) { 5252 netdev_warn(netdev, "Ignoring unexpected login response\n"); 5253 return 0; 5254 } 5255 adapter->login_pending = false; 5256 5257 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz, 5258 DMA_TO_DEVICE); 5259 dma_unmap_single(dev, adapter->login_rsp_buf_token, 5260 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE); 5261 5262 /* If the number of queues requested can't be allocated by the 5263 * server, the login response will return with code 1. We will need 5264 * to resend the login buffer with fewer queues requested. 5265 */ 5266 if (login_rsp_crq->generic.rc.code) { 5267 adapter->init_done_rc = login_rsp_crq->generic.rc.code; 5268 complete(&adapter->init_done); 5269 return 0; 5270 } 5271 5272 if (adapter->failover_pending) { 5273 adapter->init_done_rc = -EAGAIN; 5274 netdev_dbg(netdev, "Failover pending, ignoring login response\n"); 5275 complete(&adapter->init_done); 5276 /* login response buffer will be released on reset */ 5277 return 0; 5278 } 5279 5280 netdev->mtu = adapter->req_mtu - ETH_HLEN; 5281 5282 netdev_dbg(adapter->netdev, "Login Response Buffer:\n"); 5283 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) { 5284 netdev_dbg(adapter->netdev, "%016lx\n", 5285 ((unsigned long *)(adapter->login_rsp_buf))[i]); 5286 } 5287 5288 /* Sanity checks */ 5289 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs || 5290 (be32_to_cpu(login->num_rxcomp_subcrqs) * 5291 adapter->req_rx_add_queues != 5292 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) { 5293 dev_err(dev, "FATAL: Inconsistent login and login rsp\n"); 5294 ibmvnic_reset(adapter, VNIC_RESET_FATAL); 5295 return -EIO; 5296 } 5297 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 5298 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size)); 5299 /* variable buffer sizes are not supported, so just read the 5300 * first entry. 5301 */ 5302 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]); 5303 5304 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs); 5305 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs); 5306 5307 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 5308 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs)); 5309 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 5310 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs)); 5311 5312 for (i = 0; i < num_tx_pools; i++) 5313 adapter->tx_scrq[i]->handle = tx_handle_array[i]; 5314 5315 for (i = 0; i < num_rx_pools; i++) 5316 adapter->rx_scrq[i]->handle = rx_handle_array[i]; 5317 5318 adapter->num_active_tx_scrqs = num_tx_pools; 5319 adapter->num_active_rx_scrqs = num_rx_pools; 5320 release_login_rsp_buffer(adapter); 5321 release_login_buffer(adapter); 5322 complete(&adapter->init_done); 5323 5324 return 0; 5325 } 5326 5327 static void handle_request_unmap_rsp(union ibmvnic_crq *crq, 5328 struct ibmvnic_adapter *adapter) 5329 { 5330 struct device *dev = &adapter->vdev->dev; 5331 long rc; 5332 5333 rc = crq->request_unmap_rsp.rc.code; 5334 if (rc) 5335 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc); 5336 } 5337 5338 static void handle_query_map_rsp(union ibmvnic_crq *crq, 5339 struct ibmvnic_adapter *adapter) 5340 { 5341 struct net_device *netdev = adapter->netdev; 5342 struct device *dev = &adapter->vdev->dev; 5343 long rc; 5344 5345 rc = crq->query_map_rsp.rc.code; 5346 if (rc) { 5347 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc); 5348 return; 5349 } 5350 netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n", 5351 crq->query_map_rsp.page_size, 5352 __be32_to_cpu(crq->query_map_rsp.tot_pages), 5353 __be32_to_cpu(crq->query_map_rsp.free_pages)); 5354 } 5355 5356 static void handle_query_cap_rsp(union ibmvnic_crq *crq, 5357 struct ibmvnic_adapter *adapter) 5358 { 5359 struct net_device *netdev = adapter->netdev; 5360 struct device *dev = &adapter->vdev->dev; 5361 long rc; 5362 5363 atomic_dec(&adapter->running_cap_crqs); 5364 netdev_dbg(netdev, "Outstanding queries: %d\n", 5365 atomic_read(&adapter->running_cap_crqs)); 5366 rc = crq->query_capability.rc.code; 5367 if (rc) { 5368 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc); 5369 goto out; 5370 } 5371 5372 switch (be16_to_cpu(crq->query_capability.capability)) { 5373 case MIN_TX_QUEUES: 5374 adapter->min_tx_queues = 5375 be64_to_cpu(crq->query_capability.number); 5376 netdev_dbg(netdev, "min_tx_queues = %lld\n", 5377 adapter->min_tx_queues); 5378 break; 5379 case MIN_RX_QUEUES: 5380 adapter->min_rx_queues = 5381 be64_to_cpu(crq->query_capability.number); 5382 netdev_dbg(netdev, "min_rx_queues = %lld\n", 5383 adapter->min_rx_queues); 5384 break; 5385 case MIN_RX_ADD_QUEUES: 5386 adapter->min_rx_add_queues = 5387 be64_to_cpu(crq->query_capability.number); 5388 netdev_dbg(netdev, "min_rx_add_queues = %lld\n", 5389 adapter->min_rx_add_queues); 5390 break; 5391 case MAX_TX_QUEUES: 5392 adapter->max_tx_queues = 5393 be64_to_cpu(crq->query_capability.number); 5394 netdev_dbg(netdev, "max_tx_queues = %lld\n", 5395 adapter->max_tx_queues); 5396 break; 5397 case MAX_RX_QUEUES: 5398 adapter->max_rx_queues = 5399 be64_to_cpu(crq->query_capability.number); 5400 netdev_dbg(netdev, "max_rx_queues = %lld\n", 5401 adapter->max_rx_queues); 5402 break; 5403 case MAX_RX_ADD_QUEUES: 5404 adapter->max_rx_add_queues = 5405 be64_to_cpu(crq->query_capability.number); 5406 netdev_dbg(netdev, "max_rx_add_queues = %lld\n", 5407 adapter->max_rx_add_queues); 5408 break; 5409 case MIN_TX_ENTRIES_PER_SUBCRQ: 5410 adapter->min_tx_entries_per_subcrq = 5411 be64_to_cpu(crq->query_capability.number); 5412 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n", 5413 adapter->min_tx_entries_per_subcrq); 5414 break; 5415 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ: 5416 adapter->min_rx_add_entries_per_subcrq = 5417 be64_to_cpu(crq->query_capability.number); 5418 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n", 5419 adapter->min_rx_add_entries_per_subcrq); 5420 break; 5421 case MAX_TX_ENTRIES_PER_SUBCRQ: 5422 adapter->max_tx_entries_per_subcrq = 5423 be64_to_cpu(crq->query_capability.number); 5424 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n", 5425 adapter->max_tx_entries_per_subcrq); 5426 break; 5427 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ: 5428 adapter->max_rx_add_entries_per_subcrq = 5429 be64_to_cpu(crq->query_capability.number); 5430 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n", 5431 adapter->max_rx_add_entries_per_subcrq); 5432 break; 5433 case TCP_IP_OFFLOAD: 5434 adapter->tcp_ip_offload = 5435 be64_to_cpu(crq->query_capability.number); 5436 netdev_dbg(netdev, "tcp_ip_offload = %lld\n", 5437 adapter->tcp_ip_offload); 5438 break; 5439 case PROMISC_SUPPORTED: 5440 adapter->promisc_supported = 5441 be64_to_cpu(crq->query_capability.number); 5442 netdev_dbg(netdev, "promisc_supported = %lld\n", 5443 adapter->promisc_supported); 5444 break; 5445 case MIN_MTU: 5446 adapter->min_mtu = be64_to_cpu(crq->query_capability.number); 5447 netdev->min_mtu = adapter->min_mtu - ETH_HLEN; 5448 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu); 5449 break; 5450 case MAX_MTU: 5451 adapter->max_mtu = be64_to_cpu(crq->query_capability.number); 5452 netdev->max_mtu = adapter->max_mtu - ETH_HLEN; 5453 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu); 5454 break; 5455 case MAX_MULTICAST_FILTERS: 5456 adapter->max_multicast_filters = 5457 be64_to_cpu(crq->query_capability.number); 5458 netdev_dbg(netdev, "max_multicast_filters = %lld\n", 5459 adapter->max_multicast_filters); 5460 break; 5461 case VLAN_HEADER_INSERTION: 5462 adapter->vlan_header_insertion = 5463 be64_to_cpu(crq->query_capability.number); 5464 if (adapter->vlan_header_insertion) 5465 netdev->features |= NETIF_F_HW_VLAN_STAG_TX; 5466 netdev_dbg(netdev, "vlan_header_insertion = %lld\n", 5467 adapter->vlan_header_insertion); 5468 break; 5469 case RX_VLAN_HEADER_INSERTION: 5470 adapter->rx_vlan_header_insertion = 5471 be64_to_cpu(crq->query_capability.number); 5472 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n", 5473 adapter->rx_vlan_header_insertion); 5474 break; 5475 case MAX_TX_SG_ENTRIES: 5476 adapter->max_tx_sg_entries = 5477 be64_to_cpu(crq->query_capability.number); 5478 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n", 5479 adapter->max_tx_sg_entries); 5480 break; 5481 case RX_SG_SUPPORTED: 5482 adapter->rx_sg_supported = 5483 be64_to_cpu(crq->query_capability.number); 5484 netdev_dbg(netdev, "rx_sg_supported = %lld\n", 5485 adapter->rx_sg_supported); 5486 break; 5487 case OPT_TX_COMP_SUB_QUEUES: 5488 adapter->opt_tx_comp_sub_queues = 5489 be64_to_cpu(crq->query_capability.number); 5490 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n", 5491 adapter->opt_tx_comp_sub_queues); 5492 break; 5493 case OPT_RX_COMP_QUEUES: 5494 adapter->opt_rx_comp_queues = 5495 be64_to_cpu(crq->query_capability.number); 5496 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n", 5497 adapter->opt_rx_comp_queues); 5498 break; 5499 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q: 5500 adapter->opt_rx_bufadd_q_per_rx_comp_q = 5501 be64_to_cpu(crq->query_capability.number); 5502 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n", 5503 adapter->opt_rx_bufadd_q_per_rx_comp_q); 5504 break; 5505 case OPT_TX_ENTRIES_PER_SUBCRQ: 5506 adapter->opt_tx_entries_per_subcrq = 5507 be64_to_cpu(crq->query_capability.number); 5508 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n", 5509 adapter->opt_tx_entries_per_subcrq); 5510 break; 5511 case OPT_RXBA_ENTRIES_PER_SUBCRQ: 5512 adapter->opt_rxba_entries_per_subcrq = 5513 be64_to_cpu(crq->query_capability.number); 5514 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n", 5515 adapter->opt_rxba_entries_per_subcrq); 5516 break; 5517 case TX_RX_DESC_REQ: 5518 adapter->tx_rx_desc_req = crq->query_capability.number; 5519 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n", 5520 adapter->tx_rx_desc_req); 5521 break; 5522 5523 default: 5524 netdev_err(netdev, "Got invalid cap rsp %d\n", 5525 crq->query_capability.capability); 5526 } 5527 5528 out: 5529 if (atomic_read(&adapter->running_cap_crqs) == 0) 5530 send_request_cap(adapter, 0); 5531 } 5532 5533 static int send_query_phys_parms(struct ibmvnic_adapter *adapter) 5534 { 5535 union ibmvnic_crq crq; 5536 int rc; 5537 5538 memset(&crq, 0, sizeof(crq)); 5539 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD; 5540 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS; 5541 5542 mutex_lock(&adapter->fw_lock); 5543 adapter->fw_done_rc = 0; 5544 reinit_completion(&adapter->fw_done); 5545 5546 rc = ibmvnic_send_crq(adapter, &crq); 5547 if (rc) { 5548 mutex_unlock(&adapter->fw_lock); 5549 return rc; 5550 } 5551 5552 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000); 5553 if (rc) { 5554 mutex_unlock(&adapter->fw_lock); 5555 return rc; 5556 } 5557 5558 mutex_unlock(&adapter->fw_lock); 5559 return adapter->fw_done_rc ? -EIO : 0; 5560 } 5561 5562 static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq, 5563 struct ibmvnic_adapter *adapter) 5564 { 5565 struct net_device *netdev = adapter->netdev; 5566 int rc; 5567 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed); 5568 5569 rc = crq->query_phys_parms_rsp.rc.code; 5570 if (rc) { 5571 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc); 5572 return rc; 5573 } 5574 switch (rspeed) { 5575 case IBMVNIC_10MBPS: 5576 adapter->speed = SPEED_10; 5577 break; 5578 case IBMVNIC_100MBPS: 5579 adapter->speed = SPEED_100; 5580 break; 5581 case IBMVNIC_1GBPS: 5582 adapter->speed = SPEED_1000; 5583 break; 5584 case IBMVNIC_10GBPS: 5585 adapter->speed = SPEED_10000; 5586 break; 5587 case IBMVNIC_25GBPS: 5588 adapter->speed = SPEED_25000; 5589 break; 5590 case IBMVNIC_40GBPS: 5591 adapter->speed = SPEED_40000; 5592 break; 5593 case IBMVNIC_50GBPS: 5594 adapter->speed = SPEED_50000; 5595 break; 5596 case IBMVNIC_100GBPS: 5597 adapter->speed = SPEED_100000; 5598 break; 5599 case IBMVNIC_200GBPS: 5600 adapter->speed = SPEED_200000; 5601 break; 5602 default: 5603 if (netif_carrier_ok(netdev)) 5604 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed); 5605 adapter->speed = SPEED_UNKNOWN; 5606 } 5607 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX) 5608 adapter->duplex = DUPLEX_FULL; 5609 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX) 5610 adapter->duplex = DUPLEX_HALF; 5611 else 5612 adapter->duplex = DUPLEX_UNKNOWN; 5613 5614 return rc; 5615 } 5616 5617 static void ibmvnic_handle_crq(union ibmvnic_crq *crq, 5618 struct ibmvnic_adapter *adapter) 5619 { 5620 struct ibmvnic_generic_crq *gen_crq = &crq->generic; 5621 struct net_device *netdev = adapter->netdev; 5622 struct device *dev = &adapter->vdev->dev; 5623 u64 *u64_crq = (u64 *)crq; 5624 long rc; 5625 5626 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n", 5627 (unsigned long)cpu_to_be64(u64_crq[0]), 5628 (unsigned long)cpu_to_be64(u64_crq[1])); 5629 switch (gen_crq->first) { 5630 case IBMVNIC_CRQ_INIT_RSP: 5631 switch (gen_crq->cmd) { 5632 case IBMVNIC_CRQ_INIT: 5633 dev_info(dev, "Partner initialized\n"); 5634 adapter->from_passive_init = true; 5635 /* Discard any stale login responses from prev reset. 5636 * CHECK: should we clear even on INIT_COMPLETE? 5637 */ 5638 adapter->login_pending = false; 5639 5640 if (adapter->state == VNIC_DOWN) 5641 rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT); 5642 else 5643 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER); 5644 5645 if (rc && rc != -EBUSY) { 5646 /* We were unable to schedule the failover 5647 * reset either because the adapter was still 5648 * probing (eg: during kexec) or we could not 5649 * allocate memory. Clear the failover_pending 5650 * flag since no one else will. We ignore 5651 * EBUSY because it means either FAILOVER reset 5652 * is already scheduled or the adapter is 5653 * being removed. 5654 */ 5655 netdev_err(netdev, 5656 "Error %ld scheduling failover reset\n", 5657 rc); 5658 adapter->failover_pending = false; 5659 } 5660 5661 if (!completion_done(&adapter->init_done)) { 5662 if (!adapter->init_done_rc) 5663 adapter->init_done_rc = -EAGAIN; 5664 complete(&adapter->init_done); 5665 } 5666 5667 break; 5668 case IBMVNIC_CRQ_INIT_COMPLETE: 5669 dev_info(dev, "Partner initialization complete\n"); 5670 adapter->crq.active = true; 5671 send_version_xchg(adapter); 5672 break; 5673 default: 5674 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd); 5675 } 5676 return; 5677 case IBMVNIC_CRQ_XPORT_EVENT: 5678 netif_carrier_off(netdev); 5679 adapter->crq.active = false; 5680 /* terminate any thread waiting for a response 5681 * from the device 5682 */ 5683 if (!completion_done(&adapter->fw_done)) { 5684 adapter->fw_done_rc = -EIO; 5685 complete(&adapter->fw_done); 5686 } 5687 5688 /* if we got here during crq-init, retry crq-init */ 5689 if (!completion_done(&adapter->init_done)) { 5690 adapter->init_done_rc = -EAGAIN; 5691 complete(&adapter->init_done); 5692 } 5693 5694 if (!completion_done(&adapter->stats_done)) 5695 complete(&adapter->stats_done); 5696 if (test_bit(0, &adapter->resetting)) 5697 adapter->force_reset_recovery = true; 5698 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) { 5699 dev_info(dev, "Migrated, re-enabling adapter\n"); 5700 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY); 5701 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) { 5702 dev_info(dev, "Backing device failover detected\n"); 5703 adapter->failover_pending = true; 5704 } else { 5705 /* The adapter lost the connection */ 5706 dev_err(dev, "Virtual Adapter failed (rc=%d)\n", 5707 gen_crq->cmd); 5708 ibmvnic_reset(adapter, VNIC_RESET_FATAL); 5709 } 5710 return; 5711 case IBMVNIC_CRQ_CMD_RSP: 5712 break; 5713 default: 5714 dev_err(dev, "Got an invalid msg type 0x%02x\n", 5715 gen_crq->first); 5716 return; 5717 } 5718 5719 switch (gen_crq->cmd) { 5720 case VERSION_EXCHANGE_RSP: 5721 rc = crq->version_exchange_rsp.rc.code; 5722 if (rc) { 5723 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc); 5724 break; 5725 } 5726 ibmvnic_version = 5727 be16_to_cpu(crq->version_exchange_rsp.version); 5728 dev_info(dev, "Partner protocol version is %d\n", 5729 ibmvnic_version); 5730 send_query_cap(adapter); 5731 break; 5732 case QUERY_CAPABILITY_RSP: 5733 handle_query_cap_rsp(crq, adapter); 5734 break; 5735 case QUERY_MAP_RSP: 5736 handle_query_map_rsp(crq, adapter); 5737 break; 5738 case REQUEST_MAP_RSP: 5739 adapter->fw_done_rc = crq->request_map_rsp.rc.code; 5740 complete(&adapter->fw_done); 5741 break; 5742 case REQUEST_UNMAP_RSP: 5743 handle_request_unmap_rsp(crq, adapter); 5744 break; 5745 case REQUEST_CAPABILITY_RSP: 5746 handle_request_cap_rsp(crq, adapter); 5747 break; 5748 case LOGIN_RSP: 5749 netdev_dbg(netdev, "Got Login Response\n"); 5750 handle_login_rsp(crq, adapter); 5751 break; 5752 case LOGICAL_LINK_STATE_RSP: 5753 netdev_dbg(netdev, 5754 "Got Logical Link State Response, state: %d rc: %d\n", 5755 crq->logical_link_state_rsp.link_state, 5756 crq->logical_link_state_rsp.rc.code); 5757 adapter->logical_link_state = 5758 crq->logical_link_state_rsp.link_state; 5759 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code; 5760 complete(&adapter->init_done); 5761 break; 5762 case LINK_STATE_INDICATION: 5763 netdev_dbg(netdev, "Got Logical Link State Indication\n"); 5764 adapter->phys_link_state = 5765 crq->link_state_indication.phys_link_state; 5766 adapter->logical_link_state = 5767 crq->link_state_indication.logical_link_state; 5768 if (adapter->phys_link_state && adapter->logical_link_state) 5769 netif_carrier_on(netdev); 5770 else 5771 netif_carrier_off(netdev); 5772 break; 5773 case CHANGE_MAC_ADDR_RSP: 5774 netdev_dbg(netdev, "Got MAC address change Response\n"); 5775 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter); 5776 break; 5777 case ERROR_INDICATION: 5778 netdev_dbg(netdev, "Got Error Indication\n"); 5779 handle_error_indication(crq, adapter); 5780 break; 5781 case REQUEST_STATISTICS_RSP: 5782 netdev_dbg(netdev, "Got Statistics Response\n"); 5783 complete(&adapter->stats_done); 5784 break; 5785 case QUERY_IP_OFFLOAD_RSP: 5786 netdev_dbg(netdev, "Got Query IP offload Response\n"); 5787 handle_query_ip_offload_rsp(adapter); 5788 break; 5789 case MULTICAST_CTRL_RSP: 5790 netdev_dbg(netdev, "Got multicast control Response\n"); 5791 break; 5792 case CONTROL_IP_OFFLOAD_RSP: 5793 netdev_dbg(netdev, "Got Control IP offload Response\n"); 5794 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok, 5795 sizeof(adapter->ip_offload_ctrl), 5796 DMA_TO_DEVICE); 5797 complete(&adapter->init_done); 5798 break; 5799 case COLLECT_FW_TRACE_RSP: 5800 netdev_dbg(netdev, "Got Collect firmware trace Response\n"); 5801 complete(&adapter->fw_done); 5802 break; 5803 case GET_VPD_SIZE_RSP: 5804 handle_vpd_size_rsp(crq, adapter); 5805 break; 5806 case GET_VPD_RSP: 5807 handle_vpd_rsp(crq, adapter); 5808 break; 5809 case QUERY_PHYS_PARMS_RSP: 5810 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter); 5811 complete(&adapter->fw_done); 5812 break; 5813 default: 5814 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n", 5815 gen_crq->cmd); 5816 } 5817 } 5818 5819 static irqreturn_t ibmvnic_interrupt(int irq, void *instance) 5820 { 5821 struct ibmvnic_adapter *adapter = instance; 5822 5823 tasklet_schedule(&adapter->tasklet); 5824 return IRQ_HANDLED; 5825 } 5826 5827 static void ibmvnic_tasklet(struct tasklet_struct *t) 5828 { 5829 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet); 5830 struct ibmvnic_crq_queue *queue = &adapter->crq; 5831 union ibmvnic_crq *crq; 5832 unsigned long flags; 5833 5834 spin_lock_irqsave(&queue->lock, flags); 5835 5836 /* Pull all the valid messages off the CRQ */ 5837 while ((crq = ibmvnic_next_crq(adapter)) != NULL) { 5838 /* This barrier makes sure ibmvnic_next_crq()'s 5839 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded 5840 * before ibmvnic_handle_crq()'s 5841 * switch(gen_crq->first) and switch(gen_crq->cmd). 5842 */ 5843 dma_rmb(); 5844 ibmvnic_handle_crq(crq, adapter); 5845 crq->generic.first = 0; 5846 } 5847 5848 spin_unlock_irqrestore(&queue->lock, flags); 5849 } 5850 5851 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter) 5852 { 5853 struct vio_dev *vdev = adapter->vdev; 5854 int rc; 5855 5856 do { 5857 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address); 5858 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); 5859 5860 if (rc) 5861 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc); 5862 5863 return rc; 5864 } 5865 5866 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter) 5867 { 5868 struct ibmvnic_crq_queue *crq = &adapter->crq; 5869 struct device *dev = &adapter->vdev->dev; 5870 struct vio_dev *vdev = adapter->vdev; 5871 int rc; 5872 5873 /* Close the CRQ */ 5874 do { 5875 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 5876 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 5877 5878 /* Clean out the queue */ 5879 if (!crq->msgs) 5880 return -EINVAL; 5881 5882 memset(crq->msgs, 0, PAGE_SIZE); 5883 crq->cur = 0; 5884 crq->active = false; 5885 5886 /* And re-open it again */ 5887 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address, 5888 crq->msg_token, PAGE_SIZE); 5889 5890 if (rc == H_CLOSED) 5891 /* Adapter is good, but other end is not ready */ 5892 dev_warn(dev, "Partner adapter not ready\n"); 5893 else if (rc != 0) 5894 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc); 5895 5896 return rc; 5897 } 5898 5899 static void release_crq_queue(struct ibmvnic_adapter *adapter) 5900 { 5901 struct ibmvnic_crq_queue *crq = &adapter->crq; 5902 struct vio_dev *vdev = adapter->vdev; 5903 long rc; 5904 5905 if (!crq->msgs) 5906 return; 5907 5908 netdev_dbg(adapter->netdev, "Releasing CRQ\n"); 5909 free_irq(vdev->irq, adapter); 5910 tasklet_kill(&adapter->tasklet); 5911 do { 5912 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 5913 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 5914 5915 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE, 5916 DMA_BIDIRECTIONAL); 5917 free_page((unsigned long)crq->msgs); 5918 crq->msgs = NULL; 5919 crq->active = false; 5920 } 5921 5922 static int init_crq_queue(struct ibmvnic_adapter *adapter) 5923 { 5924 struct ibmvnic_crq_queue *crq = &adapter->crq; 5925 struct device *dev = &adapter->vdev->dev; 5926 struct vio_dev *vdev = adapter->vdev; 5927 int rc, retrc = -ENOMEM; 5928 5929 if (crq->msgs) 5930 return 0; 5931 5932 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL); 5933 /* Should we allocate more than one page? */ 5934 5935 if (!crq->msgs) 5936 return -ENOMEM; 5937 5938 crq->size = PAGE_SIZE / sizeof(*crq->msgs); 5939 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE, 5940 DMA_BIDIRECTIONAL); 5941 if (dma_mapping_error(dev, crq->msg_token)) 5942 goto map_failed; 5943 5944 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address, 5945 crq->msg_token, PAGE_SIZE); 5946 5947 if (rc == H_RESOURCE) 5948 /* maybe kexecing and resource is busy. try a reset */ 5949 rc = ibmvnic_reset_crq(adapter); 5950 retrc = rc; 5951 5952 if (rc == H_CLOSED) { 5953 dev_warn(dev, "Partner adapter not ready\n"); 5954 } else if (rc) { 5955 dev_warn(dev, "Error %d opening adapter\n", rc); 5956 goto reg_crq_failed; 5957 } 5958 5959 retrc = 0; 5960 5961 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet); 5962 5963 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq); 5964 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x", 5965 adapter->vdev->unit_address); 5966 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter); 5967 if (rc) { 5968 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", 5969 vdev->irq, rc); 5970 goto req_irq_failed; 5971 } 5972 5973 rc = vio_enable_interrupts(vdev); 5974 if (rc) { 5975 dev_err(dev, "Error %d enabling interrupts\n", rc); 5976 goto req_irq_failed; 5977 } 5978 5979 crq->cur = 0; 5980 spin_lock_init(&crq->lock); 5981 5982 /* process any CRQs that were queued before we enabled interrupts */ 5983 tasklet_schedule(&adapter->tasklet); 5984 5985 return retrc; 5986 5987 req_irq_failed: 5988 tasklet_kill(&adapter->tasklet); 5989 do { 5990 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 5991 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 5992 reg_crq_failed: 5993 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL); 5994 map_failed: 5995 free_page((unsigned long)crq->msgs); 5996 crq->msgs = NULL; 5997 return retrc; 5998 } 5999 6000 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset) 6001 { 6002 struct device *dev = &adapter->vdev->dev; 6003 unsigned long timeout = msecs_to_jiffies(20000); 6004 u64 old_num_rx_queues = adapter->req_rx_queues; 6005 u64 old_num_tx_queues = adapter->req_tx_queues; 6006 int rc; 6007 6008 adapter->from_passive_init = false; 6009 6010 rc = ibmvnic_send_crq_init(adapter); 6011 if (rc) { 6012 dev_err(dev, "Send crq init failed with error %d\n", rc); 6013 return rc; 6014 } 6015 6016 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) { 6017 dev_err(dev, "Initialization sequence timed out\n"); 6018 return -ETIMEDOUT; 6019 } 6020 6021 if (adapter->init_done_rc) { 6022 release_crq_queue(adapter); 6023 dev_err(dev, "CRQ-init failed, %d\n", adapter->init_done_rc); 6024 return adapter->init_done_rc; 6025 } 6026 6027 if (adapter->from_passive_init) { 6028 adapter->state = VNIC_OPEN; 6029 adapter->from_passive_init = false; 6030 dev_err(dev, "CRQ-init failed, passive-init\n"); 6031 return -EINVAL; 6032 } 6033 6034 if (reset && 6035 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset && 6036 adapter->reset_reason != VNIC_RESET_MOBILITY) { 6037 if (adapter->req_rx_queues != old_num_rx_queues || 6038 adapter->req_tx_queues != old_num_tx_queues) { 6039 release_sub_crqs(adapter, 0); 6040 rc = init_sub_crqs(adapter); 6041 } else { 6042 rc = reset_sub_crq_queues(adapter); 6043 } 6044 } else { 6045 rc = init_sub_crqs(adapter); 6046 } 6047 6048 if (rc) { 6049 dev_err(dev, "Initialization of sub crqs failed\n"); 6050 release_crq_queue(adapter); 6051 return rc; 6052 } 6053 6054 rc = init_sub_crq_irqs(adapter); 6055 if (rc) { 6056 dev_err(dev, "Failed to initialize sub crq irqs\n"); 6057 release_crq_queue(adapter); 6058 } 6059 6060 return rc; 6061 } 6062 6063 static struct device_attribute dev_attr_failover; 6064 6065 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id) 6066 { 6067 struct ibmvnic_adapter *adapter; 6068 struct net_device *netdev; 6069 unsigned char *mac_addr_p; 6070 unsigned long flags; 6071 bool init_success; 6072 int rc; 6073 6074 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n", 6075 dev->unit_address); 6076 6077 mac_addr_p = (unsigned char *)vio_get_attribute(dev, 6078 VETH_MAC_ADDR, NULL); 6079 if (!mac_addr_p) { 6080 dev_err(&dev->dev, 6081 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n", 6082 __FILE__, __LINE__); 6083 return 0; 6084 } 6085 6086 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter), 6087 IBMVNIC_MAX_QUEUES); 6088 if (!netdev) 6089 return -ENOMEM; 6090 6091 adapter = netdev_priv(netdev); 6092 adapter->state = VNIC_PROBING; 6093 dev_set_drvdata(&dev->dev, netdev); 6094 adapter->vdev = dev; 6095 adapter->netdev = netdev; 6096 adapter->login_pending = false; 6097 memset(&adapter->map_ids, 0, sizeof(adapter->map_ids)); 6098 /* map_ids start at 1, so ensure map_id 0 is always "in-use" */ 6099 bitmap_set(adapter->map_ids, 0, 1); 6100 6101 ether_addr_copy(adapter->mac_addr, mac_addr_p); 6102 eth_hw_addr_set(netdev, adapter->mac_addr); 6103 netdev->irq = dev->irq; 6104 netdev->netdev_ops = &ibmvnic_netdev_ops; 6105 netdev->ethtool_ops = &ibmvnic_ethtool_ops; 6106 SET_NETDEV_DEV(netdev, &dev->dev); 6107 6108 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset); 6109 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset, 6110 __ibmvnic_delayed_reset); 6111 INIT_LIST_HEAD(&adapter->rwi_list); 6112 spin_lock_init(&adapter->rwi_lock); 6113 spin_lock_init(&adapter->state_lock); 6114 mutex_init(&adapter->fw_lock); 6115 init_completion(&adapter->probe_done); 6116 init_completion(&adapter->init_done); 6117 init_completion(&adapter->fw_done); 6118 init_completion(&adapter->reset_done); 6119 init_completion(&adapter->stats_done); 6120 clear_bit(0, &adapter->resetting); 6121 adapter->prev_rx_buf_sz = 0; 6122 adapter->prev_mtu = 0; 6123 6124 init_success = false; 6125 do { 6126 reinit_init_done(adapter); 6127 6128 /* clear any failovers we got in the previous pass 6129 * since we are reinitializing the CRQ 6130 */ 6131 adapter->failover_pending = false; 6132 6133 /* If we had already initialized CRQ, we may have one or 6134 * more resets queued already. Discard those and release 6135 * the CRQ before initializing the CRQ again. 6136 */ 6137 release_crq_queue(adapter); 6138 6139 /* Since we are still in PROBING state, __ibmvnic_reset() 6140 * will not access the ->rwi_list and since we released CRQ, 6141 * we won't get _new_ transport events. But there maybe an 6142 * ongoing ibmvnic_reset() call. So serialize access to 6143 * rwi_list. If we win the race, ibvmnic_reset() could add 6144 * a reset after we purged but thats ok - we just may end 6145 * up with an extra reset (i.e similar to having two or more 6146 * resets in the queue at once). 6147 * CHECK. 6148 */ 6149 spin_lock_irqsave(&adapter->rwi_lock, flags); 6150 flush_reset_queue(adapter); 6151 spin_unlock_irqrestore(&adapter->rwi_lock, flags); 6152 6153 rc = init_crq_queue(adapter); 6154 if (rc) { 6155 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n", 6156 rc); 6157 goto ibmvnic_init_fail; 6158 } 6159 6160 rc = ibmvnic_reset_init(adapter, false); 6161 } while (rc == -EAGAIN); 6162 6163 /* We are ignoring the error from ibmvnic_reset_init() assuming that the 6164 * partner is not ready. CRQ is not active. When the partner becomes 6165 * ready, we will do the passive init reset. 6166 */ 6167 6168 if (!rc) 6169 init_success = true; 6170 6171 rc = init_stats_buffers(adapter); 6172 if (rc) 6173 goto ibmvnic_init_fail; 6174 6175 rc = init_stats_token(adapter); 6176 if (rc) 6177 goto ibmvnic_stats_fail; 6178 6179 rc = device_create_file(&dev->dev, &dev_attr_failover); 6180 if (rc) 6181 goto ibmvnic_dev_file_err; 6182 6183 netif_carrier_off(netdev); 6184 6185 if (init_success) { 6186 adapter->state = VNIC_PROBED; 6187 netdev->mtu = adapter->req_mtu - ETH_HLEN; 6188 netdev->min_mtu = adapter->min_mtu - ETH_HLEN; 6189 netdev->max_mtu = adapter->max_mtu - ETH_HLEN; 6190 } else { 6191 adapter->state = VNIC_DOWN; 6192 } 6193 6194 adapter->wait_for_reset = false; 6195 adapter->last_reset_time = jiffies; 6196 6197 rc = register_netdev(netdev); 6198 if (rc) { 6199 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc); 6200 goto ibmvnic_register_fail; 6201 } 6202 dev_info(&dev->dev, "ibmvnic registered\n"); 6203 6204 complete(&adapter->probe_done); 6205 6206 return 0; 6207 6208 ibmvnic_register_fail: 6209 device_remove_file(&dev->dev, &dev_attr_failover); 6210 6211 ibmvnic_dev_file_err: 6212 release_stats_token(adapter); 6213 6214 ibmvnic_stats_fail: 6215 release_stats_buffers(adapter); 6216 6217 ibmvnic_init_fail: 6218 release_sub_crqs(adapter, 1); 6219 release_crq_queue(adapter); 6220 6221 /* cleanup worker thread after releasing CRQ so we don't get 6222 * transport events (i.e new work items for the worker thread). 6223 */ 6224 adapter->state = VNIC_REMOVING; 6225 complete(&adapter->probe_done); 6226 flush_work(&adapter->ibmvnic_reset); 6227 flush_delayed_work(&adapter->ibmvnic_delayed_reset); 6228 6229 flush_reset_queue(adapter); 6230 6231 mutex_destroy(&adapter->fw_lock); 6232 free_netdev(netdev); 6233 6234 return rc; 6235 } 6236 6237 static void ibmvnic_remove(struct vio_dev *dev) 6238 { 6239 struct net_device *netdev = dev_get_drvdata(&dev->dev); 6240 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 6241 unsigned long flags; 6242 6243 spin_lock_irqsave(&adapter->state_lock, flags); 6244 6245 /* If ibmvnic_reset() is scheduling a reset, wait for it to 6246 * finish. Then, set the state to REMOVING to prevent it from 6247 * scheduling any more work and to have reset functions ignore 6248 * any resets that have already been scheduled. Drop the lock 6249 * after setting state, so __ibmvnic_reset() which is called 6250 * from the flush_work() below, can make progress. 6251 */ 6252 spin_lock(&adapter->rwi_lock); 6253 adapter->state = VNIC_REMOVING; 6254 spin_unlock(&adapter->rwi_lock); 6255 6256 spin_unlock_irqrestore(&adapter->state_lock, flags); 6257 6258 flush_work(&adapter->ibmvnic_reset); 6259 flush_delayed_work(&adapter->ibmvnic_delayed_reset); 6260 6261 rtnl_lock(); 6262 unregister_netdevice(netdev); 6263 6264 release_resources(adapter); 6265 release_rx_pools(adapter); 6266 release_tx_pools(adapter); 6267 release_sub_crqs(adapter, 1); 6268 release_crq_queue(adapter); 6269 6270 release_stats_token(adapter); 6271 release_stats_buffers(adapter); 6272 6273 adapter->state = VNIC_REMOVED; 6274 6275 rtnl_unlock(); 6276 mutex_destroy(&adapter->fw_lock); 6277 device_remove_file(&dev->dev, &dev_attr_failover); 6278 free_netdev(netdev); 6279 dev_set_drvdata(&dev->dev, NULL); 6280 } 6281 6282 static ssize_t failover_store(struct device *dev, struct device_attribute *attr, 6283 const char *buf, size_t count) 6284 { 6285 struct net_device *netdev = dev_get_drvdata(dev); 6286 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 6287 unsigned long retbuf[PLPAR_HCALL_BUFSIZE]; 6288 __be64 session_token; 6289 long rc; 6290 6291 if (!sysfs_streq(buf, "1")) 6292 return -EINVAL; 6293 6294 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address, 6295 H_GET_SESSION_TOKEN, 0, 0, 0); 6296 if (rc) { 6297 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n", 6298 rc); 6299 goto last_resort; 6300 } 6301 6302 session_token = (__be64)retbuf[0]; 6303 netdev_dbg(netdev, "Initiating client failover, session id %llx\n", 6304 be64_to_cpu(session_token)); 6305 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address, 6306 H_SESSION_ERR_DETECTED, session_token, 0, 0); 6307 if (rc) { 6308 netdev_err(netdev, 6309 "H_VIOCTL initiated failover failed, rc %ld\n", 6310 rc); 6311 goto last_resort; 6312 } 6313 6314 return count; 6315 6316 last_resort: 6317 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n"); 6318 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER); 6319 6320 return count; 6321 } 6322 static DEVICE_ATTR_WO(failover); 6323 6324 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev) 6325 { 6326 struct net_device *netdev = dev_get_drvdata(&vdev->dev); 6327 struct ibmvnic_adapter *adapter; 6328 struct iommu_table *tbl; 6329 unsigned long ret = 0; 6330 int i; 6331 6332 tbl = get_iommu_table_base(&vdev->dev); 6333 6334 /* netdev inits at probe time along with the structures we need below*/ 6335 if (!netdev) 6336 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl); 6337 6338 adapter = netdev_priv(netdev); 6339 6340 ret += PAGE_SIZE; /* the crq message queue */ 6341 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl); 6342 6343 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++) 6344 ret += 4 * PAGE_SIZE; /* the scrq message queue */ 6345 6346 for (i = 0; i < adapter->num_active_rx_pools; i++) 6347 ret += adapter->rx_pool[i].size * 6348 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl); 6349 6350 return ret; 6351 } 6352 6353 static int ibmvnic_resume(struct device *dev) 6354 { 6355 struct net_device *netdev = dev_get_drvdata(dev); 6356 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 6357 6358 if (adapter->state != VNIC_OPEN) 6359 return 0; 6360 6361 tasklet_schedule(&adapter->tasklet); 6362 6363 return 0; 6364 } 6365 6366 static const struct vio_device_id ibmvnic_device_table[] = { 6367 {"network", "IBM,vnic"}, 6368 {"", "" } 6369 }; 6370 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table); 6371 6372 static const struct dev_pm_ops ibmvnic_pm_ops = { 6373 .resume = ibmvnic_resume 6374 }; 6375 6376 static struct vio_driver ibmvnic_driver = { 6377 .id_table = ibmvnic_device_table, 6378 .probe = ibmvnic_probe, 6379 .remove = ibmvnic_remove, 6380 .get_desired_dma = ibmvnic_get_desired_dma, 6381 .name = ibmvnic_driver_name, 6382 .pm = &ibmvnic_pm_ops, 6383 }; 6384 6385 /* module functions */ 6386 static int __init ibmvnic_module_init(void) 6387 { 6388 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string, 6389 IBMVNIC_DRIVER_VERSION); 6390 6391 return vio_register_driver(&ibmvnic_driver); 6392 } 6393 6394 static void __exit ibmvnic_module_exit(void) 6395 { 6396 vio_unregister_driver(&ibmvnic_driver); 6397 } 6398 6399 module_init(ibmvnic_module_init); 6400 module_exit(ibmvnic_module_exit); 6401