1 /**************************************************************************/ 2 /* */ 3 /* IBM System i and System p Virtual NIC Device Driver */ 4 /* Copyright (C) 2014 IBM Corp. */ 5 /* Santiago Leon (santi_leon@yahoo.com) */ 6 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */ 7 /* John Allen (jallen@linux.vnet.ibm.com) */ 8 /* */ 9 /* This program is free software; you can redistribute it and/or modify */ 10 /* it under the terms of the GNU General Public License as published by */ 11 /* the Free Software Foundation; either version 2 of the License, or */ 12 /* (at your option) any later version. */ 13 /* */ 14 /* This program is distributed in the hope that it will be useful, */ 15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 17 /* GNU General Public License for more details. */ 18 /* */ 19 /* You should have received a copy of the GNU General Public License */ 20 /* along with this program. */ 21 /* */ 22 /* This module contains the implementation of a virtual ethernet device */ 23 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */ 24 /* option of the RS/6000 Platform Architecture to interface with virtual */ 25 /* ethernet NICs that are presented to the partition by the hypervisor. */ 26 /* */ 27 /* Messages are passed between the VNIC driver and the VNIC server using */ 28 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */ 29 /* issue and receive commands that initiate communication with the server */ 30 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */ 31 /* are used by the driver to notify the server that a packet is */ 32 /* ready for transmission or that a buffer has been added to receive a */ 33 /* packet. Subsequently, sCRQs are used by the server to notify the */ 34 /* driver that a packet transmission has been completed or that a packet */ 35 /* has been received and placed in a waiting buffer. */ 36 /* */ 37 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */ 38 /* which skbs are DMA mapped and immediately unmapped when the transmit */ 39 /* or receive has been completed, the VNIC driver is required to use */ 40 /* "long term mapping". This entails that large, continuous DMA mapped */ 41 /* buffers are allocated on driver initialization and these buffers are */ 42 /* then continuously reused to pass skbs to and from the VNIC server. */ 43 /* */ 44 /**************************************************************************/ 45 46 #include <linux/module.h> 47 #include <linux/moduleparam.h> 48 #include <linux/types.h> 49 #include <linux/errno.h> 50 #include <linux/completion.h> 51 #include <linux/ioport.h> 52 #include <linux/dma-mapping.h> 53 #include <linux/kernel.h> 54 #include <linux/netdevice.h> 55 #include <linux/etherdevice.h> 56 #include <linux/skbuff.h> 57 #include <linux/init.h> 58 #include <linux/delay.h> 59 #include <linux/mm.h> 60 #include <linux/ethtool.h> 61 #include <linux/proc_fs.h> 62 #include <linux/if_arp.h> 63 #include <linux/in.h> 64 #include <linux/ip.h> 65 #include <linux/ipv6.h> 66 #include <linux/irq.h> 67 #include <linux/kthread.h> 68 #include <linux/seq_file.h> 69 #include <linux/interrupt.h> 70 #include <net/net_namespace.h> 71 #include <asm/hvcall.h> 72 #include <linux/atomic.h> 73 #include <asm/vio.h> 74 #include <asm/iommu.h> 75 #include <linux/uaccess.h> 76 #include <asm/firmware.h> 77 #include <linux/workqueue.h> 78 #include <linux/if_vlan.h> 79 #include <linux/utsname.h> 80 81 #include "ibmvnic.h" 82 83 static const char ibmvnic_driver_name[] = "ibmvnic"; 84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver"; 85 86 MODULE_AUTHOR("Santiago Leon"); 87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver"); 88 MODULE_LICENSE("GPL"); 89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION); 90 91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION; 92 static int ibmvnic_remove(struct vio_dev *); 93 static void release_sub_crqs(struct ibmvnic_adapter *); 94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *); 95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *); 96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *); 97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *); 98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle, 99 union sub_crq *sub_crq); 100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64); 101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance); 102 static int enable_scrq_irq(struct ibmvnic_adapter *, 103 struct ibmvnic_sub_crq_queue *); 104 static int disable_scrq_irq(struct ibmvnic_adapter *, 105 struct ibmvnic_sub_crq_queue *); 106 static int pending_scrq(struct ibmvnic_adapter *, 107 struct ibmvnic_sub_crq_queue *); 108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *, 109 struct ibmvnic_sub_crq_queue *); 110 static int ibmvnic_poll(struct napi_struct *napi, int data); 111 static void send_map_query(struct ibmvnic_adapter *adapter); 112 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8); 113 static void send_request_unmap(struct ibmvnic_adapter *, u8); 114 static void send_login(struct ibmvnic_adapter *adapter); 115 static void send_cap_queries(struct ibmvnic_adapter *adapter); 116 static int init_sub_crqs(struct ibmvnic_adapter *); 117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter); 118 static int ibmvnic_init(struct ibmvnic_adapter *); 119 static void release_crq_queue(struct ibmvnic_adapter *); 120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p); 121 122 struct ibmvnic_stat { 123 char name[ETH_GSTRING_LEN]; 124 int offset; 125 }; 126 127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \ 128 offsetof(struct ibmvnic_statistics, stat)) 129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off))) 130 131 static const struct ibmvnic_stat ibmvnic_stats[] = { 132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)}, 133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)}, 134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)}, 135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)}, 136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)}, 137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)}, 138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)}, 139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)}, 140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)}, 141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)}, 142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)}, 143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)}, 144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)}, 145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)}, 146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)}, 147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)}, 148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)}, 149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)}, 150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)}, 151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)}, 152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)}, 153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)}, 154 }; 155 156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token, 157 unsigned long length, unsigned long *number, 158 unsigned long *irq) 159 { 160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE]; 161 long rc; 162 163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length); 164 *number = retbuf[0]; 165 *irq = retbuf[1]; 166 167 return rc; 168 } 169 170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter, 171 struct ibmvnic_long_term_buff *ltb, int size) 172 { 173 struct device *dev = &adapter->vdev->dev; 174 175 ltb->size = size; 176 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr, 177 GFP_KERNEL); 178 179 if (!ltb->buff) { 180 dev_err(dev, "Couldn't alloc long term buffer\n"); 181 return -ENOMEM; 182 } 183 ltb->map_id = adapter->map_id; 184 adapter->map_id++; 185 186 init_completion(&adapter->fw_done); 187 send_request_map(adapter, ltb->addr, 188 ltb->size, ltb->map_id); 189 wait_for_completion(&adapter->fw_done); 190 191 if (adapter->fw_done_rc) { 192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n", 193 adapter->fw_done_rc); 194 return -1; 195 } 196 return 0; 197 } 198 199 static void free_long_term_buff(struct ibmvnic_adapter *adapter, 200 struct ibmvnic_long_term_buff *ltb) 201 { 202 struct device *dev = &adapter->vdev->dev; 203 204 if (!ltb->buff) 205 return; 206 207 if (adapter->reset_reason != VNIC_RESET_FAILOVER && 208 adapter->reset_reason != VNIC_RESET_MOBILITY) 209 send_request_unmap(adapter, ltb->map_id); 210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr); 211 } 212 213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter, 214 struct ibmvnic_long_term_buff *ltb) 215 { 216 memset(ltb->buff, 0, ltb->size); 217 218 init_completion(&adapter->fw_done); 219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id); 220 wait_for_completion(&adapter->fw_done); 221 222 if (adapter->fw_done_rc) { 223 dev_info(&adapter->vdev->dev, 224 "Reset failed, attempting to free and reallocate buffer\n"); 225 free_long_term_buff(adapter, ltb); 226 return alloc_long_term_buff(adapter, ltb, ltb->size); 227 } 228 return 0; 229 } 230 231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter) 232 { 233 int i; 234 235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs); 236 i++) 237 adapter->rx_pool[i].active = 0; 238 } 239 240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter, 241 struct ibmvnic_rx_pool *pool) 242 { 243 int count = pool->size - atomic_read(&pool->available); 244 struct device *dev = &adapter->vdev->dev; 245 int buffers_added = 0; 246 unsigned long lpar_rc; 247 union sub_crq sub_crq; 248 struct sk_buff *skb; 249 unsigned int offset; 250 dma_addr_t dma_addr; 251 unsigned char *dst; 252 u64 *handle_array; 253 int shift = 0; 254 int index; 255 int i; 256 257 if (!pool->active) 258 return; 259 260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 261 be32_to_cpu(adapter->login_rsp_buf-> 262 off_rxadd_subcrqs)); 263 264 for (i = 0; i < count; ++i) { 265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC); 266 if (!skb) { 267 dev_err(dev, "Couldn't replenish rx buff\n"); 268 adapter->replenish_no_mem++; 269 break; 270 } 271 272 index = pool->free_map[pool->next_free]; 273 274 if (pool->rx_buff[index].skb) 275 dev_err(dev, "Inconsistent free_map!\n"); 276 277 /* Copy the skb to the long term mapped DMA buffer */ 278 offset = index * pool->buff_size; 279 dst = pool->long_term_buff.buff + offset; 280 memset(dst, 0, pool->buff_size); 281 dma_addr = pool->long_term_buff.addr + offset; 282 pool->rx_buff[index].data = dst; 283 284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP; 285 pool->rx_buff[index].dma = dma_addr; 286 pool->rx_buff[index].skb = skb; 287 pool->rx_buff[index].pool_index = pool->index; 288 pool->rx_buff[index].size = pool->buff_size; 289 290 memset(&sub_crq, 0, sizeof(sub_crq)); 291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD; 292 sub_crq.rx_add.correlator = 293 cpu_to_be64((u64)&pool->rx_buff[index]); 294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr); 295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id; 296 297 /* The length field of the sCRQ is defined to be 24 bits so the 298 * buffer size needs to be left shifted by a byte before it is 299 * converted to big endian to prevent the last byte from being 300 * truncated. 301 */ 302 #ifdef __LITTLE_ENDIAN__ 303 shift = 8; 304 #endif 305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift); 306 307 lpar_rc = send_subcrq(adapter, handle_array[pool->index], 308 &sub_crq); 309 if (lpar_rc != H_SUCCESS) 310 goto failure; 311 312 buffers_added++; 313 adapter->replenish_add_buff_success++; 314 pool->next_free = (pool->next_free + 1) % pool->size; 315 } 316 atomic_add(buffers_added, &pool->available); 317 return; 318 319 failure: 320 dev_info(dev, "replenish pools failure\n"); 321 pool->free_map[pool->next_free] = index; 322 pool->rx_buff[index].skb = NULL; 323 if (!dma_mapping_error(dev, dma_addr)) 324 dma_unmap_single(dev, dma_addr, pool->buff_size, 325 DMA_FROM_DEVICE); 326 327 dev_kfree_skb_any(skb); 328 adapter->replenish_add_buff_failure++; 329 atomic_add(buffers_added, &pool->available); 330 331 if (lpar_rc == H_CLOSED) { 332 /* Disable buffer pool replenishment and report carrier off if 333 * queue is closed. Firmware guarantees that a signal will 334 * be sent to the driver, triggering a reset. 335 */ 336 deactivate_rx_pools(adapter); 337 netif_carrier_off(adapter->netdev); 338 } 339 } 340 341 static void replenish_pools(struct ibmvnic_adapter *adapter) 342 { 343 int i; 344 345 adapter->replenish_task_cycles++; 346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs); 347 i++) { 348 if (adapter->rx_pool[i].active) 349 replenish_rx_pool(adapter, &adapter->rx_pool[i]); 350 } 351 } 352 353 static void release_stats_buffers(struct ibmvnic_adapter *adapter) 354 { 355 kfree(adapter->tx_stats_buffers); 356 kfree(adapter->rx_stats_buffers); 357 adapter->tx_stats_buffers = NULL; 358 adapter->rx_stats_buffers = NULL; 359 } 360 361 static int init_stats_buffers(struct ibmvnic_adapter *adapter) 362 { 363 adapter->tx_stats_buffers = 364 kcalloc(adapter->req_tx_queues, 365 sizeof(struct ibmvnic_tx_queue_stats), 366 GFP_KERNEL); 367 if (!adapter->tx_stats_buffers) 368 return -ENOMEM; 369 370 adapter->rx_stats_buffers = 371 kcalloc(adapter->req_rx_queues, 372 sizeof(struct ibmvnic_rx_queue_stats), 373 GFP_KERNEL); 374 if (!adapter->rx_stats_buffers) 375 return -ENOMEM; 376 377 return 0; 378 } 379 380 static void release_stats_token(struct ibmvnic_adapter *adapter) 381 { 382 struct device *dev = &adapter->vdev->dev; 383 384 if (!adapter->stats_token) 385 return; 386 387 dma_unmap_single(dev, adapter->stats_token, 388 sizeof(struct ibmvnic_statistics), 389 DMA_FROM_DEVICE); 390 adapter->stats_token = 0; 391 } 392 393 static int init_stats_token(struct ibmvnic_adapter *adapter) 394 { 395 struct device *dev = &adapter->vdev->dev; 396 dma_addr_t stok; 397 398 stok = dma_map_single(dev, &adapter->stats, 399 sizeof(struct ibmvnic_statistics), 400 DMA_FROM_DEVICE); 401 if (dma_mapping_error(dev, stok)) { 402 dev_err(dev, "Couldn't map stats buffer\n"); 403 return -1; 404 } 405 406 adapter->stats_token = stok; 407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok); 408 return 0; 409 } 410 411 static int reset_rx_pools(struct ibmvnic_adapter *adapter) 412 { 413 struct ibmvnic_rx_pool *rx_pool; 414 int rx_scrqs; 415 int i, j, rc; 416 u64 *size_array; 417 418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size)); 420 421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs); 422 for (i = 0; i < rx_scrqs; i++) { 423 rx_pool = &adapter->rx_pool[i]; 424 425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i); 426 427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) { 428 free_long_term_buff(adapter, &rx_pool->long_term_buff); 429 rx_pool->buff_size = be64_to_cpu(size_array[i]); 430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff, 431 rx_pool->size * 432 rx_pool->buff_size); 433 } else { 434 rc = reset_long_term_buff(adapter, 435 &rx_pool->long_term_buff); 436 } 437 438 if (rc) 439 return rc; 440 441 for (j = 0; j < rx_pool->size; j++) 442 rx_pool->free_map[j] = j; 443 444 memset(rx_pool->rx_buff, 0, 445 rx_pool->size * sizeof(struct ibmvnic_rx_buff)); 446 447 atomic_set(&rx_pool->available, 0); 448 rx_pool->next_alloc = 0; 449 rx_pool->next_free = 0; 450 rx_pool->active = 1; 451 } 452 453 return 0; 454 } 455 456 static void release_rx_pools(struct ibmvnic_adapter *adapter) 457 { 458 struct ibmvnic_rx_pool *rx_pool; 459 int i, j; 460 461 if (!adapter->rx_pool) 462 return; 463 464 for (i = 0; i < adapter->num_active_rx_pools; i++) { 465 rx_pool = &adapter->rx_pool[i]; 466 467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i); 468 469 kfree(rx_pool->free_map); 470 free_long_term_buff(adapter, &rx_pool->long_term_buff); 471 472 if (!rx_pool->rx_buff) 473 continue; 474 475 for (j = 0; j < rx_pool->size; j++) { 476 if (rx_pool->rx_buff[j].skb) { 477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb); 478 rx_pool->rx_buff[i].skb = NULL; 479 } 480 } 481 482 kfree(rx_pool->rx_buff); 483 } 484 485 kfree(adapter->rx_pool); 486 adapter->rx_pool = NULL; 487 adapter->num_active_rx_pools = 0; 488 } 489 490 static int init_rx_pools(struct net_device *netdev) 491 { 492 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 493 struct device *dev = &adapter->vdev->dev; 494 struct ibmvnic_rx_pool *rx_pool; 495 int rxadd_subcrqs; 496 u64 *size_array; 497 int i, j; 498 499 rxadd_subcrqs = 500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs); 501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size)); 503 504 adapter->rx_pool = kcalloc(rxadd_subcrqs, 505 sizeof(struct ibmvnic_rx_pool), 506 GFP_KERNEL); 507 if (!adapter->rx_pool) { 508 dev_err(dev, "Failed to allocate rx pools\n"); 509 return -1; 510 } 511 512 adapter->num_active_rx_pools = 0; 513 514 for (i = 0; i < rxadd_subcrqs; i++) { 515 rx_pool = &adapter->rx_pool[i]; 516 517 netdev_dbg(adapter->netdev, 518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n", 519 i, adapter->req_rx_add_entries_per_subcrq, 520 be64_to_cpu(size_array[i])); 521 522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq; 523 rx_pool->index = i; 524 rx_pool->buff_size = be64_to_cpu(size_array[i]); 525 rx_pool->active = 1; 526 527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int), 528 GFP_KERNEL); 529 if (!rx_pool->free_map) { 530 release_rx_pools(adapter); 531 return -1; 532 } 533 534 rx_pool->rx_buff = kcalloc(rx_pool->size, 535 sizeof(struct ibmvnic_rx_buff), 536 GFP_KERNEL); 537 if (!rx_pool->rx_buff) { 538 dev_err(dev, "Couldn't alloc rx buffers\n"); 539 release_rx_pools(adapter); 540 return -1; 541 } 542 543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff, 544 rx_pool->size * rx_pool->buff_size)) { 545 release_rx_pools(adapter); 546 return -1; 547 } 548 549 for (j = 0; j < rx_pool->size; ++j) 550 rx_pool->free_map[j] = j; 551 552 atomic_set(&rx_pool->available, 0); 553 rx_pool->next_alloc = 0; 554 rx_pool->next_free = 0; 555 } 556 557 adapter->num_active_rx_pools = rxadd_subcrqs; 558 559 return 0; 560 } 561 562 static int reset_tx_pools(struct ibmvnic_adapter *adapter) 563 { 564 struct ibmvnic_tx_pool *tx_pool; 565 int tx_scrqs; 566 int i, j, rc; 567 568 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs); 569 for (i = 0; i < tx_scrqs; i++) { 570 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i); 571 572 tx_pool = &adapter->tx_pool[i]; 573 574 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff); 575 if (rc) 576 return rc; 577 578 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb); 579 if (rc) 580 return rc; 581 582 memset(tx_pool->tx_buff, 0, 583 adapter->req_tx_entries_per_subcrq * 584 sizeof(struct ibmvnic_tx_buff)); 585 586 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++) 587 tx_pool->free_map[j] = j; 588 589 tx_pool->consumer_index = 0; 590 tx_pool->producer_index = 0; 591 tx_pool->tso_index = 0; 592 } 593 594 return 0; 595 } 596 597 static void release_vpd_data(struct ibmvnic_adapter *adapter) 598 { 599 if (!adapter->vpd) 600 return; 601 602 kfree(adapter->vpd->buff); 603 kfree(adapter->vpd); 604 605 adapter->vpd = NULL; 606 } 607 608 static void release_tx_pools(struct ibmvnic_adapter *adapter) 609 { 610 struct ibmvnic_tx_pool *tx_pool; 611 int i; 612 613 if (!adapter->tx_pool) 614 return; 615 616 for (i = 0; i < adapter->num_active_tx_pools; i++) { 617 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i); 618 tx_pool = &adapter->tx_pool[i]; 619 kfree(tx_pool->tx_buff); 620 free_long_term_buff(adapter, &tx_pool->long_term_buff); 621 free_long_term_buff(adapter, &tx_pool->tso_ltb); 622 kfree(tx_pool->free_map); 623 } 624 625 kfree(adapter->tx_pool); 626 adapter->tx_pool = NULL; 627 adapter->num_active_tx_pools = 0; 628 } 629 630 static int init_tx_pools(struct net_device *netdev) 631 { 632 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 633 struct device *dev = &adapter->vdev->dev; 634 struct ibmvnic_tx_pool *tx_pool; 635 int tx_subcrqs; 636 int i, j; 637 638 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs); 639 adapter->tx_pool = kcalloc(tx_subcrqs, 640 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL); 641 if (!adapter->tx_pool) 642 return -1; 643 644 adapter->num_active_tx_pools = 0; 645 646 for (i = 0; i < tx_subcrqs; i++) { 647 tx_pool = &adapter->tx_pool[i]; 648 649 netdev_dbg(adapter->netdev, 650 "Initializing tx_pool[%d], %lld buffs\n", 651 i, adapter->req_tx_entries_per_subcrq); 652 653 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq, 654 sizeof(struct ibmvnic_tx_buff), 655 GFP_KERNEL); 656 if (!tx_pool->tx_buff) { 657 dev_err(dev, "tx pool buffer allocation failed\n"); 658 release_tx_pools(adapter); 659 return -1; 660 } 661 662 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff, 663 adapter->req_tx_entries_per_subcrq * 664 adapter->req_mtu)) { 665 release_tx_pools(adapter); 666 return -1; 667 } 668 669 /* alloc TSO ltb */ 670 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb, 671 IBMVNIC_TSO_BUFS * 672 IBMVNIC_TSO_BUF_SZ)) { 673 release_tx_pools(adapter); 674 return -1; 675 } 676 677 tx_pool->tso_index = 0; 678 679 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq, 680 sizeof(int), GFP_KERNEL); 681 if (!tx_pool->free_map) { 682 release_tx_pools(adapter); 683 return -1; 684 } 685 686 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++) 687 tx_pool->free_map[j] = j; 688 689 tx_pool->consumer_index = 0; 690 tx_pool->producer_index = 0; 691 } 692 693 adapter->num_active_tx_pools = tx_subcrqs; 694 695 return 0; 696 } 697 698 static void release_error_buffers(struct ibmvnic_adapter *adapter) 699 { 700 struct device *dev = &adapter->vdev->dev; 701 struct ibmvnic_error_buff *error_buff, *tmp; 702 unsigned long flags; 703 704 spin_lock_irqsave(&adapter->error_list_lock, flags); 705 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) { 706 list_del(&error_buff->list); 707 dma_unmap_single(dev, error_buff->dma, error_buff->len, 708 DMA_FROM_DEVICE); 709 kfree(error_buff->buff); 710 kfree(error_buff); 711 } 712 spin_unlock_irqrestore(&adapter->error_list_lock, flags); 713 } 714 715 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter) 716 { 717 int i; 718 719 if (adapter->napi_enabled) 720 return; 721 722 for (i = 0; i < adapter->req_rx_queues; i++) 723 napi_enable(&adapter->napi[i]); 724 725 adapter->napi_enabled = true; 726 } 727 728 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter) 729 { 730 int i; 731 732 if (!adapter->napi_enabled) 733 return; 734 735 for (i = 0; i < adapter->req_rx_queues; i++) { 736 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i); 737 napi_disable(&adapter->napi[i]); 738 } 739 740 adapter->napi_enabled = false; 741 } 742 743 static int ibmvnic_login(struct net_device *netdev) 744 { 745 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 746 unsigned long timeout = msecs_to_jiffies(30000); 747 struct device *dev = &adapter->vdev->dev; 748 int rc; 749 750 do { 751 if (adapter->renegotiate) { 752 adapter->renegotiate = false; 753 release_sub_crqs(adapter); 754 755 reinit_completion(&adapter->init_done); 756 send_cap_queries(adapter); 757 if (!wait_for_completion_timeout(&adapter->init_done, 758 timeout)) { 759 dev_err(dev, "Capabilities query timeout\n"); 760 return -1; 761 } 762 rc = init_sub_crqs(adapter); 763 if (rc) { 764 dev_err(dev, 765 "Initialization of SCRQ's failed\n"); 766 return -1; 767 } 768 rc = init_sub_crq_irqs(adapter); 769 if (rc) { 770 dev_err(dev, 771 "Initialization of SCRQ's irqs failed\n"); 772 return -1; 773 } 774 } 775 776 reinit_completion(&adapter->init_done); 777 send_login(adapter); 778 if (!wait_for_completion_timeout(&adapter->init_done, 779 timeout)) { 780 dev_err(dev, "Login timeout\n"); 781 return -1; 782 } 783 } while (adapter->renegotiate); 784 785 /* handle pending MAC address changes after successful login */ 786 if (adapter->mac_change_pending) { 787 __ibmvnic_set_mac(netdev, &adapter->desired.mac); 788 adapter->mac_change_pending = false; 789 } 790 791 return 0; 792 } 793 794 static void release_resources(struct ibmvnic_adapter *adapter) 795 { 796 int i; 797 798 release_vpd_data(adapter); 799 800 release_tx_pools(adapter); 801 release_rx_pools(adapter); 802 803 release_stats_token(adapter); 804 release_stats_buffers(adapter); 805 release_error_buffers(adapter); 806 807 if (adapter->napi) { 808 for (i = 0; i < adapter->req_rx_queues; i++) { 809 if (&adapter->napi[i]) { 810 netdev_dbg(adapter->netdev, 811 "Releasing napi[%d]\n", i); 812 netif_napi_del(&adapter->napi[i]); 813 } 814 } 815 } 816 } 817 818 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state) 819 { 820 struct net_device *netdev = adapter->netdev; 821 unsigned long timeout = msecs_to_jiffies(30000); 822 union ibmvnic_crq crq; 823 bool resend; 824 int rc; 825 826 netdev_dbg(netdev, "setting link state %d\n", link_state); 827 828 memset(&crq, 0, sizeof(crq)); 829 crq.logical_link_state.first = IBMVNIC_CRQ_CMD; 830 crq.logical_link_state.cmd = LOGICAL_LINK_STATE; 831 crq.logical_link_state.link_state = link_state; 832 833 do { 834 resend = false; 835 836 reinit_completion(&adapter->init_done); 837 rc = ibmvnic_send_crq(adapter, &crq); 838 if (rc) { 839 netdev_err(netdev, "Failed to set link state\n"); 840 return rc; 841 } 842 843 if (!wait_for_completion_timeout(&adapter->init_done, 844 timeout)) { 845 netdev_err(netdev, "timeout setting link state\n"); 846 return -1; 847 } 848 849 if (adapter->init_done_rc == 1) { 850 /* Partuial success, delay and re-send */ 851 mdelay(1000); 852 resend = true; 853 } 854 } while (resend); 855 856 return 0; 857 } 858 859 static int set_real_num_queues(struct net_device *netdev) 860 { 861 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 862 int rc; 863 864 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n", 865 adapter->req_tx_queues, adapter->req_rx_queues); 866 867 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues); 868 if (rc) { 869 netdev_err(netdev, "failed to set the number of tx queues\n"); 870 return rc; 871 } 872 873 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues); 874 if (rc) 875 netdev_err(netdev, "failed to set the number of rx queues\n"); 876 877 return rc; 878 } 879 880 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter) 881 { 882 struct device *dev = &adapter->vdev->dev; 883 union ibmvnic_crq crq; 884 int len = 0; 885 886 if (adapter->vpd->buff) 887 len = adapter->vpd->len; 888 889 init_completion(&adapter->fw_done); 890 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD; 891 crq.get_vpd_size.cmd = GET_VPD_SIZE; 892 ibmvnic_send_crq(adapter, &crq); 893 wait_for_completion(&adapter->fw_done); 894 895 if (!adapter->vpd->len) 896 return -ENODATA; 897 898 if (!adapter->vpd->buff) 899 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL); 900 else if (adapter->vpd->len != len) 901 adapter->vpd->buff = 902 krealloc(adapter->vpd->buff, 903 adapter->vpd->len, GFP_KERNEL); 904 905 if (!adapter->vpd->buff) { 906 dev_err(dev, "Could allocate VPD buffer\n"); 907 return -ENOMEM; 908 } 909 910 adapter->vpd->dma_addr = 911 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len, 912 DMA_FROM_DEVICE); 913 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) { 914 dev_err(dev, "Could not map VPD buffer\n"); 915 kfree(adapter->vpd->buff); 916 adapter->vpd->buff = NULL; 917 return -ENOMEM; 918 } 919 920 reinit_completion(&adapter->fw_done); 921 crq.get_vpd.first = IBMVNIC_CRQ_CMD; 922 crq.get_vpd.cmd = GET_VPD; 923 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr); 924 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len); 925 ibmvnic_send_crq(adapter, &crq); 926 wait_for_completion(&adapter->fw_done); 927 928 return 0; 929 } 930 931 static int init_resources(struct ibmvnic_adapter *adapter) 932 { 933 struct net_device *netdev = adapter->netdev; 934 int i, rc; 935 936 rc = set_real_num_queues(netdev); 937 if (rc) 938 return rc; 939 940 rc = init_stats_buffers(adapter); 941 if (rc) 942 return rc; 943 944 rc = init_stats_token(adapter); 945 if (rc) 946 return rc; 947 948 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL); 949 if (!adapter->vpd) 950 return -ENOMEM; 951 952 /* Vital Product Data (VPD) */ 953 rc = ibmvnic_get_vpd(adapter); 954 if (rc) { 955 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n"); 956 return rc; 957 } 958 959 adapter->map_id = 1; 960 adapter->napi = kcalloc(adapter->req_rx_queues, 961 sizeof(struct napi_struct), GFP_KERNEL); 962 if (!adapter->napi) 963 return -ENOMEM; 964 965 for (i = 0; i < adapter->req_rx_queues; i++) { 966 netdev_dbg(netdev, "Adding napi[%d]\n", i); 967 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll, 968 NAPI_POLL_WEIGHT); 969 } 970 971 send_map_query(adapter); 972 973 rc = init_rx_pools(netdev); 974 if (rc) 975 return rc; 976 977 rc = init_tx_pools(netdev); 978 return rc; 979 } 980 981 static int __ibmvnic_open(struct net_device *netdev) 982 { 983 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 984 enum vnic_state prev_state = adapter->state; 985 int i, rc; 986 987 adapter->state = VNIC_OPENING; 988 replenish_pools(adapter); 989 ibmvnic_napi_enable(adapter); 990 991 /* We're ready to receive frames, enable the sub-crq interrupts and 992 * set the logical link state to up 993 */ 994 for (i = 0; i < adapter->req_rx_queues; i++) { 995 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i); 996 if (prev_state == VNIC_CLOSED) 997 enable_irq(adapter->rx_scrq[i]->irq); 998 else 999 enable_scrq_irq(adapter, adapter->rx_scrq[i]); 1000 } 1001 1002 for (i = 0; i < adapter->req_tx_queues; i++) { 1003 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i); 1004 if (prev_state == VNIC_CLOSED) 1005 enable_irq(adapter->tx_scrq[i]->irq); 1006 else 1007 enable_scrq_irq(adapter, adapter->tx_scrq[i]); 1008 } 1009 1010 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP); 1011 if (rc) { 1012 for (i = 0; i < adapter->req_rx_queues; i++) 1013 napi_disable(&adapter->napi[i]); 1014 release_resources(adapter); 1015 return rc; 1016 } 1017 1018 netif_tx_start_all_queues(netdev); 1019 1020 if (prev_state == VNIC_CLOSED) { 1021 for (i = 0; i < adapter->req_rx_queues; i++) 1022 napi_schedule(&adapter->napi[i]); 1023 } 1024 1025 adapter->state = VNIC_OPEN; 1026 return rc; 1027 } 1028 1029 static int ibmvnic_open(struct net_device *netdev) 1030 { 1031 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1032 int rc; 1033 1034 mutex_lock(&adapter->reset_lock); 1035 1036 if (adapter->state != VNIC_CLOSED) { 1037 rc = ibmvnic_login(netdev); 1038 if (rc) { 1039 mutex_unlock(&adapter->reset_lock); 1040 return rc; 1041 } 1042 1043 rc = init_resources(adapter); 1044 if (rc) { 1045 netdev_err(netdev, "failed to initialize resources\n"); 1046 release_resources(adapter); 1047 mutex_unlock(&adapter->reset_lock); 1048 return rc; 1049 } 1050 } 1051 1052 rc = __ibmvnic_open(netdev); 1053 netif_carrier_on(netdev); 1054 1055 mutex_unlock(&adapter->reset_lock); 1056 1057 return rc; 1058 } 1059 1060 static void clean_tx_pools(struct ibmvnic_adapter *adapter) 1061 { 1062 struct ibmvnic_tx_pool *tx_pool; 1063 u64 tx_entries; 1064 int tx_scrqs; 1065 int i, j; 1066 1067 if (!adapter->tx_pool) 1068 return; 1069 1070 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs); 1071 tx_entries = adapter->req_tx_entries_per_subcrq; 1072 1073 /* Free any remaining skbs in the tx buffer pools */ 1074 for (i = 0; i < tx_scrqs; i++) { 1075 tx_pool = &adapter->tx_pool[i]; 1076 if (!tx_pool) 1077 continue; 1078 1079 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i); 1080 for (j = 0; j < tx_entries; j++) { 1081 if (tx_pool->tx_buff[j].skb) { 1082 dev_kfree_skb_any(tx_pool->tx_buff[j].skb); 1083 tx_pool->tx_buff[j].skb = NULL; 1084 } 1085 } 1086 } 1087 } 1088 1089 static int __ibmvnic_close(struct net_device *netdev) 1090 { 1091 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1092 int rc = 0; 1093 int i; 1094 1095 adapter->state = VNIC_CLOSING; 1096 1097 /* ensure that transmissions are stopped if called by do_reset */ 1098 if (adapter->resetting) 1099 netif_tx_disable(netdev); 1100 else 1101 netif_tx_stop_all_queues(netdev); 1102 1103 ibmvnic_napi_disable(adapter); 1104 1105 if (adapter->tx_scrq) { 1106 for (i = 0; i < adapter->req_tx_queues; i++) 1107 if (adapter->tx_scrq[i]->irq) { 1108 netdev_dbg(adapter->netdev, 1109 "Disabling tx_scrq[%d] irq\n", i); 1110 disable_irq(adapter->tx_scrq[i]->irq); 1111 } 1112 } 1113 1114 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN); 1115 if (rc) 1116 return rc; 1117 1118 if (adapter->rx_scrq) { 1119 for (i = 0; i < adapter->req_rx_queues; i++) { 1120 int retries = 10; 1121 1122 while (pending_scrq(adapter, adapter->rx_scrq[i])) { 1123 retries--; 1124 mdelay(100); 1125 1126 if (retries == 0) 1127 break; 1128 } 1129 1130 if (adapter->rx_scrq[i]->irq) { 1131 netdev_dbg(adapter->netdev, 1132 "Disabling rx_scrq[%d] irq\n", i); 1133 disable_irq(adapter->rx_scrq[i]->irq); 1134 } 1135 } 1136 } 1137 1138 clean_tx_pools(adapter); 1139 adapter->state = VNIC_CLOSED; 1140 return rc; 1141 } 1142 1143 static int ibmvnic_close(struct net_device *netdev) 1144 { 1145 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1146 int rc; 1147 1148 mutex_lock(&adapter->reset_lock); 1149 rc = __ibmvnic_close(netdev); 1150 mutex_unlock(&adapter->reset_lock); 1151 1152 return rc; 1153 } 1154 1155 /** 1156 * build_hdr_data - creates L2/L3/L4 header data buffer 1157 * @hdr_field - bitfield determining needed headers 1158 * @skb - socket buffer 1159 * @hdr_len - array of header lengths 1160 * @tot_len - total length of data 1161 * 1162 * Reads hdr_field to determine which headers are needed by firmware. 1163 * Builds a buffer containing these headers. Saves individual header 1164 * lengths and total buffer length to be used to build descriptors. 1165 */ 1166 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb, 1167 int *hdr_len, u8 *hdr_data) 1168 { 1169 int len = 0; 1170 u8 *hdr; 1171 1172 hdr_len[0] = sizeof(struct ethhdr); 1173 1174 if (skb->protocol == htons(ETH_P_IP)) { 1175 hdr_len[1] = ip_hdr(skb)->ihl * 4; 1176 if (ip_hdr(skb)->protocol == IPPROTO_TCP) 1177 hdr_len[2] = tcp_hdrlen(skb); 1178 else if (ip_hdr(skb)->protocol == IPPROTO_UDP) 1179 hdr_len[2] = sizeof(struct udphdr); 1180 } else if (skb->protocol == htons(ETH_P_IPV6)) { 1181 hdr_len[1] = sizeof(struct ipv6hdr); 1182 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) 1183 hdr_len[2] = tcp_hdrlen(skb); 1184 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP) 1185 hdr_len[2] = sizeof(struct udphdr); 1186 } else if (skb->protocol == htons(ETH_P_ARP)) { 1187 hdr_len[1] = arp_hdr_len(skb->dev); 1188 hdr_len[2] = 0; 1189 } 1190 1191 memset(hdr_data, 0, 120); 1192 if ((hdr_field >> 6) & 1) { 1193 hdr = skb_mac_header(skb); 1194 memcpy(hdr_data, hdr, hdr_len[0]); 1195 len += hdr_len[0]; 1196 } 1197 1198 if ((hdr_field >> 5) & 1) { 1199 hdr = skb_network_header(skb); 1200 memcpy(hdr_data + len, hdr, hdr_len[1]); 1201 len += hdr_len[1]; 1202 } 1203 1204 if ((hdr_field >> 4) & 1) { 1205 hdr = skb_transport_header(skb); 1206 memcpy(hdr_data + len, hdr, hdr_len[2]); 1207 len += hdr_len[2]; 1208 } 1209 return len; 1210 } 1211 1212 /** 1213 * create_hdr_descs - create header and header extension descriptors 1214 * @hdr_field - bitfield determining needed headers 1215 * @data - buffer containing header data 1216 * @len - length of data buffer 1217 * @hdr_len - array of individual header lengths 1218 * @scrq_arr - descriptor array 1219 * 1220 * Creates header and, if needed, header extension descriptors and 1221 * places them in a descriptor array, scrq_arr 1222 */ 1223 1224 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len, 1225 union sub_crq *scrq_arr) 1226 { 1227 union sub_crq hdr_desc; 1228 int tmp_len = len; 1229 int num_descs = 0; 1230 u8 *data, *cur; 1231 int tmp; 1232 1233 while (tmp_len > 0) { 1234 cur = hdr_data + len - tmp_len; 1235 1236 memset(&hdr_desc, 0, sizeof(hdr_desc)); 1237 if (cur != hdr_data) { 1238 data = hdr_desc.hdr_ext.data; 1239 tmp = tmp_len > 29 ? 29 : tmp_len; 1240 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD; 1241 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC; 1242 hdr_desc.hdr_ext.len = tmp; 1243 } else { 1244 data = hdr_desc.hdr.data; 1245 tmp = tmp_len > 24 ? 24 : tmp_len; 1246 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD; 1247 hdr_desc.hdr.type = IBMVNIC_HDR_DESC; 1248 hdr_desc.hdr.len = tmp; 1249 hdr_desc.hdr.l2_len = (u8)hdr_len[0]; 1250 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]); 1251 hdr_desc.hdr.l4_len = (u8)hdr_len[2]; 1252 hdr_desc.hdr.flag = hdr_field << 1; 1253 } 1254 memcpy(data, cur, tmp); 1255 tmp_len -= tmp; 1256 *scrq_arr = hdr_desc; 1257 scrq_arr++; 1258 num_descs++; 1259 } 1260 1261 return num_descs; 1262 } 1263 1264 /** 1265 * build_hdr_descs_arr - build a header descriptor array 1266 * @skb - socket buffer 1267 * @num_entries - number of descriptors to be sent 1268 * @subcrq - first TX descriptor 1269 * @hdr_field - bit field determining which headers will be sent 1270 * 1271 * This function will build a TX descriptor array with applicable 1272 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect. 1273 */ 1274 1275 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff, 1276 int *num_entries, u8 hdr_field) 1277 { 1278 int hdr_len[3] = {0, 0, 0}; 1279 int tot_len; 1280 u8 *hdr_data = txbuff->hdr_data; 1281 1282 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len, 1283 txbuff->hdr_data); 1284 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len, 1285 txbuff->indir_arr + 1); 1286 } 1287 1288 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev) 1289 { 1290 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1291 int queue_num = skb_get_queue_mapping(skb); 1292 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req; 1293 struct device *dev = &adapter->vdev->dev; 1294 struct ibmvnic_tx_buff *tx_buff = NULL; 1295 struct ibmvnic_sub_crq_queue *tx_scrq; 1296 struct ibmvnic_tx_pool *tx_pool; 1297 unsigned int tx_send_failed = 0; 1298 unsigned int tx_map_failed = 0; 1299 unsigned int tx_dropped = 0; 1300 unsigned int tx_packets = 0; 1301 unsigned int tx_bytes = 0; 1302 dma_addr_t data_dma_addr; 1303 struct netdev_queue *txq; 1304 unsigned long lpar_rc; 1305 union sub_crq tx_crq; 1306 unsigned int offset; 1307 int num_entries = 1; 1308 unsigned char *dst; 1309 u64 *handle_array; 1310 int index = 0; 1311 u8 proto = 0; 1312 int ret = 0; 1313 1314 if (adapter->resetting) { 1315 if (!netif_subqueue_stopped(netdev, skb)) 1316 netif_stop_subqueue(netdev, queue_num); 1317 dev_kfree_skb_any(skb); 1318 1319 tx_send_failed++; 1320 tx_dropped++; 1321 ret = NETDEV_TX_OK; 1322 goto out; 1323 } 1324 1325 tx_pool = &adapter->tx_pool[queue_num]; 1326 tx_scrq = adapter->tx_scrq[queue_num]; 1327 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb)); 1328 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) + 1329 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs)); 1330 1331 index = tx_pool->free_map[tx_pool->consumer_index]; 1332 1333 if (skb_is_gso(skb)) { 1334 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ; 1335 dst = tx_pool->tso_ltb.buff + offset; 1336 memset(dst, 0, IBMVNIC_TSO_BUF_SZ); 1337 data_dma_addr = tx_pool->tso_ltb.addr + offset; 1338 tx_pool->tso_index++; 1339 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS) 1340 tx_pool->tso_index = 0; 1341 } else { 1342 offset = index * adapter->req_mtu; 1343 dst = tx_pool->long_term_buff.buff + offset; 1344 memset(dst, 0, adapter->req_mtu); 1345 data_dma_addr = tx_pool->long_term_buff.addr + offset; 1346 } 1347 1348 if (skb_shinfo(skb)->nr_frags) { 1349 int cur, i; 1350 1351 /* Copy the head */ 1352 skb_copy_from_linear_data(skb, dst, skb_headlen(skb)); 1353 cur = skb_headlen(skb); 1354 1355 /* Copy the frags */ 1356 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1357 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1358 1359 memcpy(dst + cur, 1360 page_address(skb_frag_page(frag)) + 1361 frag->page_offset, skb_frag_size(frag)); 1362 cur += skb_frag_size(frag); 1363 } 1364 } else { 1365 skb_copy_from_linear_data(skb, dst, skb->len); 1366 } 1367 1368 tx_pool->consumer_index = 1369 (tx_pool->consumer_index + 1) % 1370 adapter->req_tx_entries_per_subcrq; 1371 1372 tx_buff = &tx_pool->tx_buff[index]; 1373 tx_buff->skb = skb; 1374 tx_buff->data_dma[0] = data_dma_addr; 1375 tx_buff->data_len[0] = skb->len; 1376 tx_buff->index = index; 1377 tx_buff->pool_index = queue_num; 1378 tx_buff->last_frag = true; 1379 1380 memset(&tx_crq, 0, sizeof(tx_crq)); 1381 tx_crq.v1.first = IBMVNIC_CRQ_CMD; 1382 tx_crq.v1.type = IBMVNIC_TX_DESC; 1383 tx_crq.v1.n_crq_elem = 1; 1384 tx_crq.v1.n_sge = 1; 1385 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED; 1386 tx_crq.v1.correlator = cpu_to_be32(index); 1387 if (skb_is_gso(skb)) 1388 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id); 1389 else 1390 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id); 1391 tx_crq.v1.sge_len = cpu_to_be32(skb->len); 1392 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr); 1393 1394 if (adapter->vlan_header_insertion) { 1395 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT; 1396 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci); 1397 } 1398 1399 if (skb->protocol == htons(ETH_P_IP)) { 1400 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4; 1401 proto = ip_hdr(skb)->protocol; 1402 } else if (skb->protocol == htons(ETH_P_IPV6)) { 1403 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6; 1404 proto = ipv6_hdr(skb)->nexthdr; 1405 } 1406 1407 if (proto == IPPROTO_TCP) 1408 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP; 1409 else if (proto == IPPROTO_UDP) 1410 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP; 1411 1412 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1413 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD; 1414 hdrs += 2; 1415 } 1416 if (skb_is_gso(skb)) { 1417 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO; 1418 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size); 1419 hdrs += 2; 1420 } 1421 /* determine if l2/3/4 headers are sent to firmware */ 1422 if ((*hdrs >> 7) & 1) { 1423 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs); 1424 tx_crq.v1.n_crq_elem = num_entries; 1425 tx_buff->indir_arr[0] = tx_crq; 1426 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr, 1427 sizeof(tx_buff->indir_arr), 1428 DMA_TO_DEVICE); 1429 if (dma_mapping_error(dev, tx_buff->indir_dma)) { 1430 dev_kfree_skb_any(skb); 1431 tx_buff->skb = NULL; 1432 if (!firmware_has_feature(FW_FEATURE_CMO)) 1433 dev_err(dev, "tx: unable to map descriptor array\n"); 1434 tx_map_failed++; 1435 tx_dropped++; 1436 ret = NETDEV_TX_OK; 1437 goto out; 1438 } 1439 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num], 1440 (u64)tx_buff->indir_dma, 1441 (u64)num_entries); 1442 } else { 1443 lpar_rc = send_subcrq(adapter, handle_array[queue_num], 1444 &tx_crq); 1445 } 1446 if (lpar_rc != H_SUCCESS) { 1447 dev_err(dev, "tx failed with code %ld\n", lpar_rc); 1448 1449 if (tx_pool->consumer_index == 0) 1450 tx_pool->consumer_index = 1451 adapter->req_tx_entries_per_subcrq - 1; 1452 else 1453 tx_pool->consumer_index--; 1454 1455 dev_kfree_skb_any(skb); 1456 tx_buff->skb = NULL; 1457 1458 if (lpar_rc == H_CLOSED) { 1459 /* Disable TX and report carrier off if queue is closed. 1460 * Firmware guarantees that a signal will be sent to the 1461 * driver, triggering a reset or some other action. 1462 */ 1463 netif_tx_stop_all_queues(netdev); 1464 netif_carrier_off(netdev); 1465 } 1466 1467 tx_send_failed++; 1468 tx_dropped++; 1469 ret = NETDEV_TX_OK; 1470 goto out; 1471 } 1472 1473 if (atomic_inc_return(&tx_scrq->used) 1474 >= adapter->req_tx_entries_per_subcrq) { 1475 netdev_info(netdev, "Stopping queue %d\n", queue_num); 1476 netif_stop_subqueue(netdev, queue_num); 1477 } 1478 1479 tx_packets++; 1480 tx_bytes += skb->len; 1481 txq->trans_start = jiffies; 1482 ret = NETDEV_TX_OK; 1483 1484 out: 1485 netdev->stats.tx_dropped += tx_dropped; 1486 netdev->stats.tx_bytes += tx_bytes; 1487 netdev->stats.tx_packets += tx_packets; 1488 adapter->tx_send_failed += tx_send_failed; 1489 adapter->tx_map_failed += tx_map_failed; 1490 adapter->tx_stats_buffers[queue_num].packets += tx_packets; 1491 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes; 1492 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped; 1493 1494 return ret; 1495 } 1496 1497 static void ibmvnic_set_multi(struct net_device *netdev) 1498 { 1499 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1500 struct netdev_hw_addr *ha; 1501 union ibmvnic_crq crq; 1502 1503 memset(&crq, 0, sizeof(crq)); 1504 crq.request_capability.first = IBMVNIC_CRQ_CMD; 1505 crq.request_capability.cmd = REQUEST_CAPABILITY; 1506 1507 if (netdev->flags & IFF_PROMISC) { 1508 if (!adapter->promisc_supported) 1509 return; 1510 } else { 1511 if (netdev->flags & IFF_ALLMULTI) { 1512 /* Accept all multicast */ 1513 memset(&crq, 0, sizeof(crq)); 1514 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD; 1515 crq.multicast_ctrl.cmd = MULTICAST_CTRL; 1516 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL; 1517 ibmvnic_send_crq(adapter, &crq); 1518 } else if (netdev_mc_empty(netdev)) { 1519 /* Reject all multicast */ 1520 memset(&crq, 0, sizeof(crq)); 1521 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD; 1522 crq.multicast_ctrl.cmd = MULTICAST_CTRL; 1523 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL; 1524 ibmvnic_send_crq(adapter, &crq); 1525 } else { 1526 /* Accept one or more multicast(s) */ 1527 netdev_for_each_mc_addr(ha, netdev) { 1528 memset(&crq, 0, sizeof(crq)); 1529 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD; 1530 crq.multicast_ctrl.cmd = MULTICAST_CTRL; 1531 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC; 1532 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0], 1533 ha->addr); 1534 ibmvnic_send_crq(adapter, &crq); 1535 } 1536 } 1537 } 1538 } 1539 1540 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p) 1541 { 1542 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1543 struct sockaddr *addr = p; 1544 union ibmvnic_crq crq; 1545 1546 if (!is_valid_ether_addr(addr->sa_data)) 1547 return -EADDRNOTAVAIL; 1548 1549 memset(&crq, 0, sizeof(crq)); 1550 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD; 1551 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR; 1552 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data); 1553 1554 init_completion(&adapter->fw_done); 1555 ibmvnic_send_crq(adapter, &crq); 1556 wait_for_completion(&adapter->fw_done); 1557 /* netdev->dev_addr is changed in handle_change_mac_rsp function */ 1558 return adapter->fw_done_rc ? -EIO : 0; 1559 } 1560 1561 static int ibmvnic_set_mac(struct net_device *netdev, void *p) 1562 { 1563 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1564 struct sockaddr *addr = p; 1565 int rc; 1566 1567 if (adapter->state == VNIC_PROBED) { 1568 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr)); 1569 adapter->mac_change_pending = true; 1570 return 0; 1571 } 1572 1573 rc = __ibmvnic_set_mac(netdev, addr); 1574 1575 return rc; 1576 } 1577 1578 /** 1579 * do_reset returns zero if we are able to keep processing reset events, or 1580 * non-zero if we hit a fatal error and must halt. 1581 */ 1582 static int do_reset(struct ibmvnic_adapter *adapter, 1583 struct ibmvnic_rwi *rwi, u32 reset_state) 1584 { 1585 u64 old_num_rx_queues, old_num_tx_queues; 1586 struct net_device *netdev = adapter->netdev; 1587 int i, rc; 1588 1589 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n", 1590 rwi->reset_reason); 1591 1592 netif_carrier_off(netdev); 1593 adapter->reset_reason = rwi->reset_reason; 1594 1595 old_num_rx_queues = adapter->req_rx_queues; 1596 old_num_tx_queues = adapter->req_tx_queues; 1597 1598 if (rwi->reset_reason == VNIC_RESET_MOBILITY) { 1599 rc = ibmvnic_reenable_crq_queue(adapter); 1600 if (rc) 1601 return 0; 1602 } 1603 1604 rc = __ibmvnic_close(netdev); 1605 if (rc) 1606 return rc; 1607 1608 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM || 1609 adapter->wait_for_reset) { 1610 release_resources(adapter); 1611 release_sub_crqs(adapter); 1612 release_crq_queue(adapter); 1613 } 1614 1615 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) { 1616 /* remove the closed state so when we call open it appears 1617 * we are coming from the probed state. 1618 */ 1619 adapter->state = VNIC_PROBED; 1620 1621 rc = ibmvnic_init(adapter); 1622 if (rc) 1623 return IBMVNIC_INIT_FAILED; 1624 1625 /* If the adapter was in PROBE state prior to the reset, 1626 * exit here. 1627 */ 1628 if (reset_state == VNIC_PROBED) 1629 return 0; 1630 1631 rc = ibmvnic_login(netdev); 1632 if (rc) { 1633 adapter->state = VNIC_PROBED; 1634 return 0; 1635 } 1636 1637 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM || 1638 adapter->wait_for_reset) { 1639 rc = init_resources(adapter); 1640 if (rc) 1641 return rc; 1642 } else if (adapter->req_rx_queues != old_num_rx_queues || 1643 adapter->req_tx_queues != old_num_tx_queues) { 1644 adapter->map_id = 1; 1645 release_rx_pools(adapter); 1646 release_tx_pools(adapter); 1647 init_rx_pools(netdev); 1648 init_tx_pools(netdev); 1649 } else { 1650 rc = reset_tx_pools(adapter); 1651 if (rc) 1652 return rc; 1653 1654 rc = reset_rx_pools(adapter); 1655 if (rc) 1656 return rc; 1657 1658 if (reset_state == VNIC_CLOSED) 1659 return 0; 1660 } 1661 } 1662 1663 rc = __ibmvnic_open(netdev); 1664 if (rc) { 1665 if (list_empty(&adapter->rwi_list)) 1666 adapter->state = VNIC_CLOSED; 1667 else 1668 adapter->state = reset_state; 1669 1670 return 0; 1671 } 1672 1673 netif_carrier_on(netdev); 1674 1675 /* kick napi */ 1676 for (i = 0; i < adapter->req_rx_queues; i++) 1677 napi_schedule(&adapter->napi[i]); 1678 1679 if (adapter->reset_reason != VNIC_RESET_FAILOVER) 1680 netdev_notify_peers(netdev); 1681 1682 return 0; 1683 } 1684 1685 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter) 1686 { 1687 struct ibmvnic_rwi *rwi; 1688 1689 mutex_lock(&adapter->rwi_lock); 1690 1691 if (!list_empty(&adapter->rwi_list)) { 1692 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi, 1693 list); 1694 list_del(&rwi->list); 1695 } else { 1696 rwi = NULL; 1697 } 1698 1699 mutex_unlock(&adapter->rwi_lock); 1700 return rwi; 1701 } 1702 1703 static void free_all_rwi(struct ibmvnic_adapter *adapter) 1704 { 1705 struct ibmvnic_rwi *rwi; 1706 1707 rwi = get_next_rwi(adapter); 1708 while (rwi) { 1709 kfree(rwi); 1710 rwi = get_next_rwi(adapter); 1711 } 1712 } 1713 1714 static void __ibmvnic_reset(struct work_struct *work) 1715 { 1716 struct ibmvnic_rwi *rwi; 1717 struct ibmvnic_adapter *adapter; 1718 struct net_device *netdev; 1719 u32 reset_state; 1720 int rc = 0; 1721 1722 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset); 1723 netdev = adapter->netdev; 1724 1725 mutex_lock(&adapter->reset_lock); 1726 adapter->resetting = true; 1727 reset_state = adapter->state; 1728 1729 rwi = get_next_rwi(adapter); 1730 while (rwi) { 1731 rc = do_reset(adapter, rwi, reset_state); 1732 kfree(rwi); 1733 if (rc && rc != IBMVNIC_INIT_FAILED) 1734 break; 1735 1736 rwi = get_next_rwi(adapter); 1737 } 1738 1739 if (adapter->wait_for_reset) { 1740 adapter->wait_for_reset = false; 1741 adapter->reset_done_rc = rc; 1742 complete(&adapter->reset_done); 1743 } 1744 1745 if (rc) { 1746 netdev_dbg(adapter->netdev, "Reset failed\n"); 1747 free_all_rwi(adapter); 1748 mutex_unlock(&adapter->reset_lock); 1749 return; 1750 } 1751 1752 adapter->resetting = false; 1753 mutex_unlock(&adapter->reset_lock); 1754 } 1755 1756 static void ibmvnic_reset(struct ibmvnic_adapter *adapter, 1757 enum ibmvnic_reset_reason reason) 1758 { 1759 struct ibmvnic_rwi *rwi, *tmp; 1760 struct net_device *netdev = adapter->netdev; 1761 struct list_head *entry; 1762 1763 if (adapter->state == VNIC_REMOVING || 1764 adapter->state == VNIC_REMOVED) { 1765 netdev_dbg(netdev, "Adapter removing, skipping reset\n"); 1766 return; 1767 } 1768 1769 if (adapter->state == VNIC_PROBING) { 1770 netdev_warn(netdev, "Adapter reset during probe\n"); 1771 adapter->init_done_rc = EAGAIN; 1772 return; 1773 } 1774 1775 mutex_lock(&adapter->rwi_lock); 1776 1777 list_for_each(entry, &adapter->rwi_list) { 1778 tmp = list_entry(entry, struct ibmvnic_rwi, list); 1779 if (tmp->reset_reason == reason) { 1780 netdev_dbg(netdev, "Skipping matching reset\n"); 1781 mutex_unlock(&adapter->rwi_lock); 1782 return; 1783 } 1784 } 1785 1786 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL); 1787 if (!rwi) { 1788 mutex_unlock(&adapter->rwi_lock); 1789 ibmvnic_close(netdev); 1790 return; 1791 } 1792 1793 rwi->reset_reason = reason; 1794 list_add_tail(&rwi->list, &adapter->rwi_list); 1795 mutex_unlock(&adapter->rwi_lock); 1796 1797 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason); 1798 schedule_work(&adapter->ibmvnic_reset); 1799 } 1800 1801 static void ibmvnic_tx_timeout(struct net_device *dev) 1802 { 1803 struct ibmvnic_adapter *adapter = netdev_priv(dev); 1804 1805 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT); 1806 } 1807 1808 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter, 1809 struct ibmvnic_rx_buff *rx_buff) 1810 { 1811 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index]; 1812 1813 rx_buff->skb = NULL; 1814 1815 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff); 1816 pool->next_alloc = (pool->next_alloc + 1) % pool->size; 1817 1818 atomic_dec(&pool->available); 1819 } 1820 1821 static int ibmvnic_poll(struct napi_struct *napi, int budget) 1822 { 1823 struct net_device *netdev = napi->dev; 1824 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1825 int scrq_num = (int)(napi - adapter->napi); 1826 int frames_processed = 0; 1827 1828 restart_poll: 1829 while (frames_processed < budget) { 1830 struct sk_buff *skb; 1831 struct ibmvnic_rx_buff *rx_buff; 1832 union sub_crq *next; 1833 u32 length; 1834 u16 offset; 1835 u8 flags = 0; 1836 1837 if (unlikely(adapter->resetting && 1838 adapter->reset_reason != VNIC_RESET_NON_FATAL)) { 1839 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]); 1840 napi_complete_done(napi, frames_processed); 1841 return frames_processed; 1842 } 1843 1844 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num])) 1845 break; 1846 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]); 1847 rx_buff = 1848 (struct ibmvnic_rx_buff *)be64_to_cpu(next-> 1849 rx_comp.correlator); 1850 /* do error checking */ 1851 if (next->rx_comp.rc) { 1852 netdev_dbg(netdev, "rx buffer returned with rc %x\n", 1853 be16_to_cpu(next->rx_comp.rc)); 1854 /* free the entry */ 1855 next->rx_comp.first = 0; 1856 remove_buff_from_pool(adapter, rx_buff); 1857 continue; 1858 } 1859 1860 length = be32_to_cpu(next->rx_comp.len); 1861 offset = be16_to_cpu(next->rx_comp.off_frame_data); 1862 flags = next->rx_comp.flags; 1863 skb = rx_buff->skb; 1864 skb_copy_to_linear_data(skb, rx_buff->data + offset, 1865 length); 1866 1867 /* VLAN Header has been stripped by the system firmware and 1868 * needs to be inserted by the driver 1869 */ 1870 if (adapter->rx_vlan_header_insertion && 1871 (flags & IBMVNIC_VLAN_STRIPPED)) 1872 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 1873 ntohs(next->rx_comp.vlan_tci)); 1874 1875 /* free the entry */ 1876 next->rx_comp.first = 0; 1877 remove_buff_from_pool(adapter, rx_buff); 1878 1879 skb_put(skb, length); 1880 skb->protocol = eth_type_trans(skb, netdev); 1881 skb_record_rx_queue(skb, scrq_num); 1882 1883 if (flags & IBMVNIC_IP_CHKSUM_GOOD && 1884 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) { 1885 skb->ip_summed = CHECKSUM_UNNECESSARY; 1886 } 1887 1888 length = skb->len; 1889 napi_gro_receive(napi, skb); /* send it up */ 1890 netdev->stats.rx_packets++; 1891 netdev->stats.rx_bytes += length; 1892 adapter->rx_stats_buffers[scrq_num].packets++; 1893 adapter->rx_stats_buffers[scrq_num].bytes += length; 1894 frames_processed++; 1895 } 1896 1897 if (adapter->state != VNIC_CLOSING) 1898 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]); 1899 1900 if (frames_processed < budget) { 1901 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]); 1902 napi_complete_done(napi, frames_processed); 1903 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) && 1904 napi_reschedule(napi)) { 1905 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]); 1906 goto restart_poll; 1907 } 1908 } 1909 return frames_processed; 1910 } 1911 1912 #ifdef CONFIG_NET_POLL_CONTROLLER 1913 static void ibmvnic_netpoll_controller(struct net_device *dev) 1914 { 1915 struct ibmvnic_adapter *adapter = netdev_priv(dev); 1916 int i; 1917 1918 replenish_pools(netdev_priv(dev)); 1919 for (i = 0; i < adapter->req_rx_queues; i++) 1920 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq, 1921 adapter->rx_scrq[i]); 1922 } 1923 #endif 1924 1925 static int wait_for_reset(struct ibmvnic_adapter *adapter) 1926 { 1927 adapter->fallback.mtu = adapter->req_mtu; 1928 adapter->fallback.rx_queues = adapter->req_rx_queues; 1929 adapter->fallback.tx_queues = adapter->req_tx_queues; 1930 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq; 1931 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq; 1932 1933 init_completion(&adapter->reset_done); 1934 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM); 1935 adapter->wait_for_reset = true; 1936 wait_for_completion(&adapter->reset_done); 1937 1938 if (adapter->reset_done_rc) { 1939 adapter->desired.mtu = adapter->fallback.mtu; 1940 adapter->desired.rx_queues = adapter->fallback.rx_queues; 1941 adapter->desired.tx_queues = adapter->fallback.tx_queues; 1942 adapter->desired.rx_entries = adapter->fallback.rx_entries; 1943 adapter->desired.tx_entries = adapter->fallback.tx_entries; 1944 1945 init_completion(&adapter->reset_done); 1946 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM); 1947 wait_for_completion(&adapter->reset_done); 1948 } 1949 adapter->wait_for_reset = false; 1950 1951 return adapter->reset_done_rc; 1952 } 1953 1954 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu) 1955 { 1956 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 1957 1958 adapter->desired.mtu = new_mtu + ETH_HLEN; 1959 1960 return wait_for_reset(adapter); 1961 } 1962 1963 static const struct net_device_ops ibmvnic_netdev_ops = { 1964 .ndo_open = ibmvnic_open, 1965 .ndo_stop = ibmvnic_close, 1966 .ndo_start_xmit = ibmvnic_xmit, 1967 .ndo_set_rx_mode = ibmvnic_set_multi, 1968 .ndo_set_mac_address = ibmvnic_set_mac, 1969 .ndo_validate_addr = eth_validate_addr, 1970 .ndo_tx_timeout = ibmvnic_tx_timeout, 1971 #ifdef CONFIG_NET_POLL_CONTROLLER 1972 .ndo_poll_controller = ibmvnic_netpoll_controller, 1973 #endif 1974 .ndo_change_mtu = ibmvnic_change_mtu, 1975 }; 1976 1977 /* ethtool functions */ 1978 1979 static int ibmvnic_get_link_ksettings(struct net_device *netdev, 1980 struct ethtool_link_ksettings *cmd) 1981 { 1982 u32 supported, advertising; 1983 1984 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | 1985 SUPPORTED_FIBRE); 1986 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | 1987 ADVERTISED_FIBRE); 1988 cmd->base.speed = SPEED_1000; 1989 cmd->base.duplex = DUPLEX_FULL; 1990 cmd->base.port = PORT_FIBRE; 1991 cmd->base.phy_address = 0; 1992 cmd->base.autoneg = AUTONEG_ENABLE; 1993 1994 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 1995 supported); 1996 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 1997 advertising); 1998 1999 return 0; 2000 } 2001 2002 static void ibmvnic_get_drvinfo(struct net_device *netdev, 2003 struct ethtool_drvinfo *info) 2004 { 2005 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2006 2007 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver)); 2008 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version)); 2009 strlcpy(info->fw_version, adapter->fw_version, 2010 sizeof(info->fw_version)); 2011 } 2012 2013 static u32 ibmvnic_get_msglevel(struct net_device *netdev) 2014 { 2015 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2016 2017 return adapter->msg_enable; 2018 } 2019 2020 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data) 2021 { 2022 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2023 2024 adapter->msg_enable = data; 2025 } 2026 2027 static u32 ibmvnic_get_link(struct net_device *netdev) 2028 { 2029 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2030 2031 /* Don't need to send a query because we request a logical link up at 2032 * init and then we wait for link state indications 2033 */ 2034 return adapter->logical_link_state; 2035 } 2036 2037 static void ibmvnic_get_ringparam(struct net_device *netdev, 2038 struct ethtool_ringparam *ring) 2039 { 2040 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2041 2042 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq; 2043 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq; 2044 ring->rx_mini_max_pending = 0; 2045 ring->rx_jumbo_max_pending = 0; 2046 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq; 2047 ring->tx_pending = adapter->req_tx_entries_per_subcrq; 2048 ring->rx_mini_pending = 0; 2049 ring->rx_jumbo_pending = 0; 2050 } 2051 2052 static int ibmvnic_set_ringparam(struct net_device *netdev, 2053 struct ethtool_ringparam *ring) 2054 { 2055 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2056 2057 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq || 2058 ring->tx_pending > adapter->max_tx_entries_per_subcrq) { 2059 netdev_err(netdev, "Invalid request.\n"); 2060 netdev_err(netdev, "Max tx buffers = %llu\n", 2061 adapter->max_rx_add_entries_per_subcrq); 2062 netdev_err(netdev, "Max rx buffers = %llu\n", 2063 adapter->max_tx_entries_per_subcrq); 2064 return -EINVAL; 2065 } 2066 2067 adapter->desired.rx_entries = ring->rx_pending; 2068 adapter->desired.tx_entries = ring->tx_pending; 2069 2070 return wait_for_reset(adapter); 2071 } 2072 2073 static void ibmvnic_get_channels(struct net_device *netdev, 2074 struct ethtool_channels *channels) 2075 { 2076 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2077 2078 channels->max_rx = adapter->max_rx_queues; 2079 channels->max_tx = adapter->max_tx_queues; 2080 channels->max_other = 0; 2081 channels->max_combined = 0; 2082 channels->rx_count = adapter->req_rx_queues; 2083 channels->tx_count = adapter->req_tx_queues; 2084 channels->other_count = 0; 2085 channels->combined_count = 0; 2086 } 2087 2088 static int ibmvnic_set_channels(struct net_device *netdev, 2089 struct ethtool_channels *channels) 2090 { 2091 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 2092 2093 adapter->desired.rx_queues = channels->rx_count; 2094 adapter->desired.tx_queues = channels->tx_count; 2095 2096 return wait_for_reset(adapter); 2097 } 2098 2099 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2100 { 2101 struct ibmvnic_adapter *adapter = netdev_priv(dev); 2102 int i; 2103 2104 if (stringset != ETH_SS_STATS) 2105 return; 2106 2107 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN) 2108 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN); 2109 2110 for (i = 0; i < adapter->req_tx_queues; i++) { 2111 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i); 2112 data += ETH_GSTRING_LEN; 2113 2114 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i); 2115 data += ETH_GSTRING_LEN; 2116 2117 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i); 2118 data += ETH_GSTRING_LEN; 2119 } 2120 2121 for (i = 0; i < adapter->req_rx_queues; i++) { 2122 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i); 2123 data += ETH_GSTRING_LEN; 2124 2125 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i); 2126 data += ETH_GSTRING_LEN; 2127 2128 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i); 2129 data += ETH_GSTRING_LEN; 2130 } 2131 } 2132 2133 static int ibmvnic_get_sset_count(struct net_device *dev, int sset) 2134 { 2135 struct ibmvnic_adapter *adapter = netdev_priv(dev); 2136 2137 switch (sset) { 2138 case ETH_SS_STATS: 2139 return ARRAY_SIZE(ibmvnic_stats) + 2140 adapter->req_tx_queues * NUM_TX_STATS + 2141 adapter->req_rx_queues * NUM_RX_STATS; 2142 default: 2143 return -EOPNOTSUPP; 2144 } 2145 } 2146 2147 static void ibmvnic_get_ethtool_stats(struct net_device *dev, 2148 struct ethtool_stats *stats, u64 *data) 2149 { 2150 struct ibmvnic_adapter *adapter = netdev_priv(dev); 2151 union ibmvnic_crq crq; 2152 int i, j; 2153 2154 memset(&crq, 0, sizeof(crq)); 2155 crq.request_statistics.first = IBMVNIC_CRQ_CMD; 2156 crq.request_statistics.cmd = REQUEST_STATISTICS; 2157 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token); 2158 crq.request_statistics.len = 2159 cpu_to_be32(sizeof(struct ibmvnic_statistics)); 2160 2161 /* Wait for data to be written */ 2162 init_completion(&adapter->stats_done); 2163 ibmvnic_send_crq(adapter, &crq); 2164 wait_for_completion(&adapter->stats_done); 2165 2166 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++) 2167 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter, 2168 ibmvnic_stats[i].offset)); 2169 2170 for (j = 0; j < adapter->req_tx_queues; j++) { 2171 data[i] = adapter->tx_stats_buffers[j].packets; 2172 i++; 2173 data[i] = adapter->tx_stats_buffers[j].bytes; 2174 i++; 2175 data[i] = adapter->tx_stats_buffers[j].dropped_packets; 2176 i++; 2177 } 2178 2179 for (j = 0; j < adapter->req_rx_queues; j++) { 2180 data[i] = adapter->rx_stats_buffers[j].packets; 2181 i++; 2182 data[i] = adapter->rx_stats_buffers[j].bytes; 2183 i++; 2184 data[i] = adapter->rx_stats_buffers[j].interrupts; 2185 i++; 2186 } 2187 } 2188 2189 static const struct ethtool_ops ibmvnic_ethtool_ops = { 2190 .get_drvinfo = ibmvnic_get_drvinfo, 2191 .get_msglevel = ibmvnic_get_msglevel, 2192 .set_msglevel = ibmvnic_set_msglevel, 2193 .get_link = ibmvnic_get_link, 2194 .get_ringparam = ibmvnic_get_ringparam, 2195 .set_ringparam = ibmvnic_set_ringparam, 2196 .get_channels = ibmvnic_get_channels, 2197 .set_channels = ibmvnic_set_channels, 2198 .get_strings = ibmvnic_get_strings, 2199 .get_sset_count = ibmvnic_get_sset_count, 2200 .get_ethtool_stats = ibmvnic_get_ethtool_stats, 2201 .get_link_ksettings = ibmvnic_get_link_ksettings, 2202 }; 2203 2204 /* Routines for managing CRQs/sCRQs */ 2205 2206 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter, 2207 struct ibmvnic_sub_crq_queue *scrq) 2208 { 2209 int rc; 2210 2211 if (scrq->irq) { 2212 free_irq(scrq->irq, scrq); 2213 irq_dispose_mapping(scrq->irq); 2214 scrq->irq = 0; 2215 } 2216 2217 memset(scrq->msgs, 0, 4 * PAGE_SIZE); 2218 scrq->cur = 0; 2219 2220 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token, 2221 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq); 2222 return rc; 2223 } 2224 2225 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter) 2226 { 2227 int i, rc; 2228 2229 for (i = 0; i < adapter->req_tx_queues; i++) { 2230 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i); 2231 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]); 2232 if (rc) 2233 return rc; 2234 } 2235 2236 for (i = 0; i < adapter->req_rx_queues; i++) { 2237 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i); 2238 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]); 2239 if (rc) 2240 return rc; 2241 } 2242 2243 return rc; 2244 } 2245 2246 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter, 2247 struct ibmvnic_sub_crq_queue *scrq) 2248 { 2249 struct device *dev = &adapter->vdev->dev; 2250 long rc; 2251 2252 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n"); 2253 2254 /* Close the sub-crqs */ 2255 do { 2256 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, 2257 adapter->vdev->unit_address, 2258 scrq->crq_num); 2259 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 2260 2261 if (rc) { 2262 netdev_err(adapter->netdev, 2263 "Failed to release sub-CRQ %16lx, rc = %ld\n", 2264 scrq->crq_num, rc); 2265 } 2266 2267 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE, 2268 DMA_BIDIRECTIONAL); 2269 free_pages((unsigned long)scrq->msgs, 2); 2270 kfree(scrq); 2271 } 2272 2273 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter 2274 *adapter) 2275 { 2276 struct device *dev = &adapter->vdev->dev; 2277 struct ibmvnic_sub_crq_queue *scrq; 2278 int rc; 2279 2280 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL); 2281 if (!scrq) 2282 return NULL; 2283 2284 scrq->msgs = 2285 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2); 2286 if (!scrq->msgs) { 2287 dev_warn(dev, "Couldn't allocate crq queue messages page\n"); 2288 goto zero_page_failed; 2289 } 2290 2291 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE, 2292 DMA_BIDIRECTIONAL); 2293 if (dma_mapping_error(dev, scrq->msg_token)) { 2294 dev_warn(dev, "Couldn't map crq queue messages page\n"); 2295 goto map_failed; 2296 } 2297 2298 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token, 2299 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq); 2300 2301 if (rc == H_RESOURCE) 2302 rc = ibmvnic_reset_crq(adapter); 2303 2304 if (rc == H_CLOSED) { 2305 dev_warn(dev, "Partner adapter not ready, waiting.\n"); 2306 } else if (rc) { 2307 dev_warn(dev, "Error %d registering sub-crq\n", rc); 2308 goto reg_failed; 2309 } 2310 2311 scrq->adapter = adapter; 2312 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs); 2313 spin_lock_init(&scrq->lock); 2314 2315 netdev_dbg(adapter->netdev, 2316 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n", 2317 scrq->crq_num, scrq->hw_irq, scrq->irq); 2318 2319 return scrq; 2320 2321 reg_failed: 2322 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE, 2323 DMA_BIDIRECTIONAL); 2324 map_failed: 2325 free_pages((unsigned long)scrq->msgs, 2); 2326 zero_page_failed: 2327 kfree(scrq); 2328 2329 return NULL; 2330 } 2331 2332 static void release_sub_crqs(struct ibmvnic_adapter *adapter) 2333 { 2334 int i; 2335 2336 if (adapter->tx_scrq) { 2337 for (i = 0; i < adapter->req_tx_queues; i++) { 2338 if (!adapter->tx_scrq[i]) 2339 continue; 2340 2341 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n", 2342 i); 2343 if (adapter->tx_scrq[i]->irq) { 2344 free_irq(adapter->tx_scrq[i]->irq, 2345 adapter->tx_scrq[i]); 2346 irq_dispose_mapping(adapter->tx_scrq[i]->irq); 2347 adapter->tx_scrq[i]->irq = 0; 2348 } 2349 2350 release_sub_crq_queue(adapter, adapter->tx_scrq[i]); 2351 } 2352 2353 kfree(adapter->tx_scrq); 2354 adapter->tx_scrq = NULL; 2355 } 2356 2357 if (adapter->rx_scrq) { 2358 for (i = 0; i < adapter->req_rx_queues; i++) { 2359 if (!adapter->rx_scrq[i]) 2360 continue; 2361 2362 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n", 2363 i); 2364 if (adapter->rx_scrq[i]->irq) { 2365 free_irq(adapter->rx_scrq[i]->irq, 2366 adapter->rx_scrq[i]); 2367 irq_dispose_mapping(adapter->rx_scrq[i]->irq); 2368 adapter->rx_scrq[i]->irq = 0; 2369 } 2370 2371 release_sub_crq_queue(adapter, adapter->rx_scrq[i]); 2372 } 2373 2374 kfree(adapter->rx_scrq); 2375 adapter->rx_scrq = NULL; 2376 } 2377 } 2378 2379 static int disable_scrq_irq(struct ibmvnic_adapter *adapter, 2380 struct ibmvnic_sub_crq_queue *scrq) 2381 { 2382 struct device *dev = &adapter->vdev->dev; 2383 unsigned long rc; 2384 2385 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address, 2386 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0); 2387 if (rc) 2388 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n", 2389 scrq->hw_irq, rc); 2390 return rc; 2391 } 2392 2393 static int enable_scrq_irq(struct ibmvnic_adapter *adapter, 2394 struct ibmvnic_sub_crq_queue *scrq) 2395 { 2396 struct device *dev = &adapter->vdev->dev; 2397 unsigned long rc; 2398 2399 if (scrq->hw_irq > 0x100000000ULL) { 2400 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq); 2401 return 1; 2402 } 2403 2404 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address, 2405 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0); 2406 if (rc) 2407 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n", 2408 scrq->hw_irq, rc); 2409 return rc; 2410 } 2411 2412 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter, 2413 struct ibmvnic_sub_crq_queue *scrq) 2414 { 2415 struct device *dev = &adapter->vdev->dev; 2416 struct ibmvnic_tx_buff *txbuff; 2417 union sub_crq *next; 2418 int index; 2419 int i, j; 2420 u8 first; 2421 2422 restart_loop: 2423 while (pending_scrq(adapter, scrq)) { 2424 unsigned int pool = scrq->pool_index; 2425 2426 next = ibmvnic_next_scrq(adapter, scrq); 2427 for (i = 0; i < next->tx_comp.num_comps; i++) { 2428 if (next->tx_comp.rcs[i]) { 2429 dev_err(dev, "tx error %x\n", 2430 next->tx_comp.rcs[i]); 2431 continue; 2432 } 2433 index = be32_to_cpu(next->tx_comp.correlators[i]); 2434 txbuff = &adapter->tx_pool[pool].tx_buff[index]; 2435 2436 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) { 2437 if (!txbuff->data_dma[j]) 2438 continue; 2439 2440 txbuff->data_dma[j] = 0; 2441 } 2442 /* if sub_crq was sent indirectly */ 2443 first = txbuff->indir_arr[0].generic.first; 2444 if (first == IBMVNIC_CRQ_CMD) { 2445 dma_unmap_single(dev, txbuff->indir_dma, 2446 sizeof(txbuff->indir_arr), 2447 DMA_TO_DEVICE); 2448 } 2449 2450 if (txbuff->last_frag) { 2451 dev_kfree_skb_any(txbuff->skb); 2452 txbuff->skb = NULL; 2453 } 2454 2455 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool]. 2456 producer_index] = index; 2457 adapter->tx_pool[pool].producer_index = 2458 (adapter->tx_pool[pool].producer_index + 1) % 2459 adapter->req_tx_entries_per_subcrq; 2460 } 2461 /* remove tx_comp scrq*/ 2462 next->tx_comp.first = 0; 2463 2464 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <= 2465 (adapter->req_tx_entries_per_subcrq / 2) && 2466 __netif_subqueue_stopped(adapter->netdev, 2467 scrq->pool_index)) { 2468 netif_wake_subqueue(adapter->netdev, scrq->pool_index); 2469 netdev_info(adapter->netdev, "Started queue %d\n", 2470 scrq->pool_index); 2471 } 2472 } 2473 2474 enable_scrq_irq(adapter, scrq); 2475 2476 if (pending_scrq(adapter, scrq)) { 2477 disable_scrq_irq(adapter, scrq); 2478 goto restart_loop; 2479 } 2480 2481 return 0; 2482 } 2483 2484 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance) 2485 { 2486 struct ibmvnic_sub_crq_queue *scrq = instance; 2487 struct ibmvnic_adapter *adapter = scrq->adapter; 2488 2489 disable_scrq_irq(adapter, scrq); 2490 ibmvnic_complete_tx(adapter, scrq); 2491 2492 return IRQ_HANDLED; 2493 } 2494 2495 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance) 2496 { 2497 struct ibmvnic_sub_crq_queue *scrq = instance; 2498 struct ibmvnic_adapter *adapter = scrq->adapter; 2499 2500 /* When booting a kdump kernel we can hit pending interrupts 2501 * prior to completing driver initialization. 2502 */ 2503 if (unlikely(adapter->state != VNIC_OPEN)) 2504 return IRQ_NONE; 2505 2506 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++; 2507 2508 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) { 2509 disable_scrq_irq(adapter, scrq); 2510 __napi_schedule(&adapter->napi[scrq->scrq_num]); 2511 } 2512 2513 return IRQ_HANDLED; 2514 } 2515 2516 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter) 2517 { 2518 struct device *dev = &adapter->vdev->dev; 2519 struct ibmvnic_sub_crq_queue *scrq; 2520 int i = 0, j = 0; 2521 int rc = 0; 2522 2523 for (i = 0; i < adapter->req_tx_queues; i++) { 2524 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n", 2525 i); 2526 scrq = adapter->tx_scrq[i]; 2527 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq); 2528 2529 if (!scrq->irq) { 2530 rc = -EINVAL; 2531 dev_err(dev, "Error mapping irq\n"); 2532 goto req_tx_irq_failed; 2533 } 2534 2535 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx, 2536 0, "ibmvnic_tx", scrq); 2537 2538 if (rc) { 2539 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n", 2540 scrq->irq, rc); 2541 irq_dispose_mapping(scrq->irq); 2542 goto req_rx_irq_failed; 2543 } 2544 } 2545 2546 for (i = 0; i < adapter->req_rx_queues; i++) { 2547 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n", 2548 i); 2549 scrq = adapter->rx_scrq[i]; 2550 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq); 2551 if (!scrq->irq) { 2552 rc = -EINVAL; 2553 dev_err(dev, "Error mapping irq\n"); 2554 goto req_rx_irq_failed; 2555 } 2556 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx, 2557 0, "ibmvnic_rx", scrq); 2558 if (rc) { 2559 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n", 2560 scrq->irq, rc); 2561 irq_dispose_mapping(scrq->irq); 2562 goto req_rx_irq_failed; 2563 } 2564 } 2565 return rc; 2566 2567 req_rx_irq_failed: 2568 for (j = 0; j < i; j++) { 2569 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]); 2570 irq_dispose_mapping(adapter->rx_scrq[j]->irq); 2571 } 2572 i = adapter->req_tx_queues; 2573 req_tx_irq_failed: 2574 for (j = 0; j < i; j++) { 2575 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]); 2576 irq_dispose_mapping(adapter->rx_scrq[j]->irq); 2577 } 2578 release_sub_crqs(adapter); 2579 return rc; 2580 } 2581 2582 static int init_sub_crqs(struct ibmvnic_adapter *adapter) 2583 { 2584 struct device *dev = &adapter->vdev->dev; 2585 struct ibmvnic_sub_crq_queue **allqueues; 2586 int registered_queues = 0; 2587 int total_queues; 2588 int more = 0; 2589 int i; 2590 2591 total_queues = adapter->req_tx_queues + adapter->req_rx_queues; 2592 2593 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL); 2594 if (!allqueues) 2595 return -1; 2596 2597 for (i = 0; i < total_queues; i++) { 2598 allqueues[i] = init_sub_crq_queue(adapter); 2599 if (!allqueues[i]) { 2600 dev_warn(dev, "Couldn't allocate all sub-crqs\n"); 2601 break; 2602 } 2603 registered_queues++; 2604 } 2605 2606 /* Make sure we were able to register the minimum number of queues */ 2607 if (registered_queues < 2608 adapter->min_tx_queues + adapter->min_rx_queues) { 2609 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n"); 2610 goto tx_failed; 2611 } 2612 2613 /* Distribute the failed allocated queues*/ 2614 for (i = 0; i < total_queues - registered_queues + more ; i++) { 2615 netdev_dbg(adapter->netdev, "Reducing number of queues\n"); 2616 switch (i % 3) { 2617 case 0: 2618 if (adapter->req_rx_queues > adapter->min_rx_queues) 2619 adapter->req_rx_queues--; 2620 else 2621 more++; 2622 break; 2623 case 1: 2624 if (adapter->req_tx_queues > adapter->min_tx_queues) 2625 adapter->req_tx_queues--; 2626 else 2627 more++; 2628 break; 2629 } 2630 } 2631 2632 adapter->tx_scrq = kcalloc(adapter->req_tx_queues, 2633 sizeof(*adapter->tx_scrq), GFP_KERNEL); 2634 if (!adapter->tx_scrq) 2635 goto tx_failed; 2636 2637 for (i = 0; i < adapter->req_tx_queues; i++) { 2638 adapter->tx_scrq[i] = allqueues[i]; 2639 adapter->tx_scrq[i]->pool_index = i; 2640 } 2641 2642 adapter->rx_scrq = kcalloc(adapter->req_rx_queues, 2643 sizeof(*adapter->rx_scrq), GFP_KERNEL); 2644 if (!adapter->rx_scrq) 2645 goto rx_failed; 2646 2647 for (i = 0; i < adapter->req_rx_queues; i++) { 2648 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues]; 2649 adapter->rx_scrq[i]->scrq_num = i; 2650 } 2651 2652 kfree(allqueues); 2653 return 0; 2654 2655 rx_failed: 2656 kfree(adapter->tx_scrq); 2657 adapter->tx_scrq = NULL; 2658 tx_failed: 2659 for (i = 0; i < registered_queues; i++) 2660 release_sub_crq_queue(adapter, allqueues[i]); 2661 kfree(allqueues); 2662 return -1; 2663 } 2664 2665 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry) 2666 { 2667 struct device *dev = &adapter->vdev->dev; 2668 union ibmvnic_crq crq; 2669 int max_entries; 2670 2671 if (!retry) { 2672 /* Sub-CRQ entries are 32 byte long */ 2673 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4); 2674 2675 if (adapter->min_tx_entries_per_subcrq > entries_page || 2676 adapter->min_rx_add_entries_per_subcrq > entries_page) { 2677 dev_err(dev, "Fatal, invalid entries per sub-crq\n"); 2678 return; 2679 } 2680 2681 if (adapter->desired.mtu) 2682 adapter->req_mtu = adapter->desired.mtu; 2683 else 2684 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN; 2685 2686 if (!adapter->desired.tx_entries) 2687 adapter->desired.tx_entries = 2688 adapter->max_tx_entries_per_subcrq; 2689 if (!adapter->desired.rx_entries) 2690 adapter->desired.rx_entries = 2691 adapter->max_rx_add_entries_per_subcrq; 2692 2693 max_entries = IBMVNIC_MAX_LTB_SIZE / 2694 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN); 2695 2696 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) * 2697 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) { 2698 adapter->desired.tx_entries = max_entries; 2699 } 2700 2701 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) * 2702 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) { 2703 adapter->desired.rx_entries = max_entries; 2704 } 2705 2706 if (adapter->desired.tx_entries) 2707 adapter->req_tx_entries_per_subcrq = 2708 adapter->desired.tx_entries; 2709 else 2710 adapter->req_tx_entries_per_subcrq = 2711 adapter->max_tx_entries_per_subcrq; 2712 2713 if (adapter->desired.rx_entries) 2714 adapter->req_rx_add_entries_per_subcrq = 2715 adapter->desired.rx_entries; 2716 else 2717 adapter->req_rx_add_entries_per_subcrq = 2718 adapter->max_rx_add_entries_per_subcrq; 2719 2720 if (adapter->desired.tx_queues) 2721 adapter->req_tx_queues = 2722 adapter->desired.tx_queues; 2723 else 2724 adapter->req_tx_queues = 2725 adapter->opt_tx_comp_sub_queues; 2726 2727 if (adapter->desired.rx_queues) 2728 adapter->req_rx_queues = 2729 adapter->desired.rx_queues; 2730 else 2731 adapter->req_rx_queues = 2732 adapter->opt_rx_comp_queues; 2733 2734 adapter->req_rx_add_queues = adapter->max_rx_add_queues; 2735 } 2736 2737 memset(&crq, 0, sizeof(crq)); 2738 crq.request_capability.first = IBMVNIC_CRQ_CMD; 2739 crq.request_capability.cmd = REQUEST_CAPABILITY; 2740 2741 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES); 2742 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues); 2743 atomic_inc(&adapter->running_cap_crqs); 2744 ibmvnic_send_crq(adapter, &crq); 2745 2746 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES); 2747 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues); 2748 atomic_inc(&adapter->running_cap_crqs); 2749 ibmvnic_send_crq(adapter, &crq); 2750 2751 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES); 2752 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues); 2753 atomic_inc(&adapter->running_cap_crqs); 2754 ibmvnic_send_crq(adapter, &crq); 2755 2756 crq.request_capability.capability = 2757 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ); 2758 crq.request_capability.number = 2759 cpu_to_be64(adapter->req_tx_entries_per_subcrq); 2760 atomic_inc(&adapter->running_cap_crqs); 2761 ibmvnic_send_crq(adapter, &crq); 2762 2763 crq.request_capability.capability = 2764 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ); 2765 crq.request_capability.number = 2766 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq); 2767 atomic_inc(&adapter->running_cap_crqs); 2768 ibmvnic_send_crq(adapter, &crq); 2769 2770 crq.request_capability.capability = cpu_to_be16(REQ_MTU); 2771 crq.request_capability.number = cpu_to_be64(adapter->req_mtu); 2772 atomic_inc(&adapter->running_cap_crqs); 2773 ibmvnic_send_crq(adapter, &crq); 2774 2775 if (adapter->netdev->flags & IFF_PROMISC) { 2776 if (adapter->promisc_supported) { 2777 crq.request_capability.capability = 2778 cpu_to_be16(PROMISC_REQUESTED); 2779 crq.request_capability.number = cpu_to_be64(1); 2780 atomic_inc(&adapter->running_cap_crqs); 2781 ibmvnic_send_crq(adapter, &crq); 2782 } 2783 } else { 2784 crq.request_capability.capability = 2785 cpu_to_be16(PROMISC_REQUESTED); 2786 crq.request_capability.number = cpu_to_be64(0); 2787 atomic_inc(&adapter->running_cap_crqs); 2788 ibmvnic_send_crq(adapter, &crq); 2789 } 2790 } 2791 2792 static int pending_scrq(struct ibmvnic_adapter *adapter, 2793 struct ibmvnic_sub_crq_queue *scrq) 2794 { 2795 union sub_crq *entry = &scrq->msgs[scrq->cur]; 2796 2797 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) 2798 return 1; 2799 else 2800 return 0; 2801 } 2802 2803 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter, 2804 struct ibmvnic_sub_crq_queue *scrq) 2805 { 2806 union sub_crq *entry; 2807 unsigned long flags; 2808 2809 spin_lock_irqsave(&scrq->lock, flags); 2810 entry = &scrq->msgs[scrq->cur]; 2811 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) { 2812 if (++scrq->cur == scrq->size) 2813 scrq->cur = 0; 2814 } else { 2815 entry = NULL; 2816 } 2817 spin_unlock_irqrestore(&scrq->lock, flags); 2818 2819 return entry; 2820 } 2821 2822 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter) 2823 { 2824 struct ibmvnic_crq_queue *queue = &adapter->crq; 2825 union ibmvnic_crq *crq; 2826 2827 crq = &queue->msgs[queue->cur]; 2828 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) { 2829 if (++queue->cur == queue->size) 2830 queue->cur = 0; 2831 } else { 2832 crq = NULL; 2833 } 2834 2835 return crq; 2836 } 2837 2838 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle, 2839 union sub_crq *sub_crq) 2840 { 2841 unsigned int ua = adapter->vdev->unit_address; 2842 struct device *dev = &adapter->vdev->dev; 2843 u64 *u64_crq = (u64 *)sub_crq; 2844 int rc; 2845 2846 netdev_dbg(adapter->netdev, 2847 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n", 2848 (unsigned long int)cpu_to_be64(remote_handle), 2849 (unsigned long int)cpu_to_be64(u64_crq[0]), 2850 (unsigned long int)cpu_to_be64(u64_crq[1]), 2851 (unsigned long int)cpu_to_be64(u64_crq[2]), 2852 (unsigned long int)cpu_to_be64(u64_crq[3])); 2853 2854 /* Make sure the hypervisor sees the complete request */ 2855 mb(); 2856 2857 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua, 2858 cpu_to_be64(remote_handle), 2859 cpu_to_be64(u64_crq[0]), 2860 cpu_to_be64(u64_crq[1]), 2861 cpu_to_be64(u64_crq[2]), 2862 cpu_to_be64(u64_crq[3])); 2863 2864 if (rc) { 2865 if (rc == H_CLOSED) 2866 dev_warn(dev, "CRQ Queue closed\n"); 2867 dev_err(dev, "Send error (rc=%d)\n", rc); 2868 } 2869 2870 return rc; 2871 } 2872 2873 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter, 2874 u64 remote_handle, u64 ioba, u64 num_entries) 2875 { 2876 unsigned int ua = adapter->vdev->unit_address; 2877 struct device *dev = &adapter->vdev->dev; 2878 int rc; 2879 2880 /* Make sure the hypervisor sees the complete request */ 2881 mb(); 2882 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua, 2883 cpu_to_be64(remote_handle), 2884 ioba, num_entries); 2885 2886 if (rc) { 2887 if (rc == H_CLOSED) 2888 dev_warn(dev, "CRQ Queue closed\n"); 2889 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc); 2890 } 2891 2892 return rc; 2893 } 2894 2895 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter, 2896 union ibmvnic_crq *crq) 2897 { 2898 unsigned int ua = adapter->vdev->unit_address; 2899 struct device *dev = &adapter->vdev->dev; 2900 u64 *u64_crq = (u64 *)crq; 2901 int rc; 2902 2903 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n", 2904 (unsigned long int)cpu_to_be64(u64_crq[0]), 2905 (unsigned long int)cpu_to_be64(u64_crq[1])); 2906 2907 /* Make sure the hypervisor sees the complete request */ 2908 mb(); 2909 2910 rc = plpar_hcall_norets(H_SEND_CRQ, ua, 2911 cpu_to_be64(u64_crq[0]), 2912 cpu_to_be64(u64_crq[1])); 2913 2914 if (rc) { 2915 if (rc == H_CLOSED) { 2916 dev_warn(dev, "CRQ Queue closed\n"); 2917 if (adapter->resetting) 2918 ibmvnic_reset(adapter, VNIC_RESET_FATAL); 2919 } 2920 2921 dev_warn(dev, "Send error (rc=%d)\n", rc); 2922 } 2923 2924 return rc; 2925 } 2926 2927 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter) 2928 { 2929 union ibmvnic_crq crq; 2930 2931 memset(&crq, 0, sizeof(crq)); 2932 crq.generic.first = IBMVNIC_CRQ_INIT_CMD; 2933 crq.generic.cmd = IBMVNIC_CRQ_INIT; 2934 netdev_dbg(adapter->netdev, "Sending CRQ init\n"); 2935 2936 return ibmvnic_send_crq(adapter, &crq); 2937 } 2938 2939 static int send_version_xchg(struct ibmvnic_adapter *adapter) 2940 { 2941 union ibmvnic_crq crq; 2942 2943 memset(&crq, 0, sizeof(crq)); 2944 crq.version_exchange.first = IBMVNIC_CRQ_CMD; 2945 crq.version_exchange.cmd = VERSION_EXCHANGE; 2946 crq.version_exchange.version = cpu_to_be16(ibmvnic_version); 2947 2948 return ibmvnic_send_crq(adapter, &crq); 2949 } 2950 2951 struct vnic_login_client_data { 2952 u8 type; 2953 __be16 len; 2954 char name; 2955 } __packed; 2956 2957 static int vnic_client_data_len(struct ibmvnic_adapter *adapter) 2958 { 2959 int len; 2960 2961 /* Calculate the amount of buffer space needed for the 2962 * vnic client data in the login buffer. There are four entries, 2963 * OS name, LPAR name, device name, and a null last entry. 2964 */ 2965 len = 4 * sizeof(struct vnic_login_client_data); 2966 len += 6; /* "Linux" plus NULL */ 2967 len += strlen(utsname()->nodename) + 1; 2968 len += strlen(adapter->netdev->name) + 1; 2969 2970 return len; 2971 } 2972 2973 static void vnic_add_client_data(struct ibmvnic_adapter *adapter, 2974 struct vnic_login_client_data *vlcd) 2975 { 2976 const char *os_name = "Linux"; 2977 int len; 2978 2979 /* Type 1 - LPAR OS */ 2980 vlcd->type = 1; 2981 len = strlen(os_name) + 1; 2982 vlcd->len = cpu_to_be16(len); 2983 strncpy(&vlcd->name, os_name, len); 2984 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len); 2985 2986 /* Type 2 - LPAR name */ 2987 vlcd->type = 2; 2988 len = strlen(utsname()->nodename) + 1; 2989 vlcd->len = cpu_to_be16(len); 2990 strncpy(&vlcd->name, utsname()->nodename, len); 2991 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len); 2992 2993 /* Type 3 - device name */ 2994 vlcd->type = 3; 2995 len = strlen(adapter->netdev->name) + 1; 2996 vlcd->len = cpu_to_be16(len); 2997 strncpy(&vlcd->name, adapter->netdev->name, len); 2998 } 2999 3000 static void send_login(struct ibmvnic_adapter *adapter) 3001 { 3002 struct ibmvnic_login_rsp_buffer *login_rsp_buffer; 3003 struct ibmvnic_login_buffer *login_buffer; 3004 struct device *dev = &adapter->vdev->dev; 3005 dma_addr_t rsp_buffer_token; 3006 dma_addr_t buffer_token; 3007 size_t rsp_buffer_size; 3008 union ibmvnic_crq crq; 3009 size_t buffer_size; 3010 __be64 *tx_list_p; 3011 __be64 *rx_list_p; 3012 int client_data_len; 3013 struct vnic_login_client_data *vlcd; 3014 int i; 3015 3016 client_data_len = vnic_client_data_len(adapter); 3017 3018 buffer_size = 3019 sizeof(struct ibmvnic_login_buffer) + 3020 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) + 3021 client_data_len; 3022 3023 login_buffer = kzalloc(buffer_size, GFP_ATOMIC); 3024 if (!login_buffer) 3025 goto buf_alloc_failed; 3026 3027 buffer_token = dma_map_single(dev, login_buffer, buffer_size, 3028 DMA_TO_DEVICE); 3029 if (dma_mapping_error(dev, buffer_token)) { 3030 dev_err(dev, "Couldn't map login buffer\n"); 3031 goto buf_map_failed; 3032 } 3033 3034 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) + 3035 sizeof(u64) * adapter->req_tx_queues + 3036 sizeof(u64) * adapter->req_rx_queues + 3037 sizeof(u64) * adapter->req_rx_queues + 3038 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS; 3039 3040 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC); 3041 if (!login_rsp_buffer) 3042 goto buf_rsp_alloc_failed; 3043 3044 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer, 3045 rsp_buffer_size, DMA_FROM_DEVICE); 3046 if (dma_mapping_error(dev, rsp_buffer_token)) { 3047 dev_err(dev, "Couldn't map login rsp buffer\n"); 3048 goto buf_rsp_map_failed; 3049 } 3050 3051 adapter->login_buf = login_buffer; 3052 adapter->login_buf_token = buffer_token; 3053 adapter->login_buf_sz = buffer_size; 3054 adapter->login_rsp_buf = login_rsp_buffer; 3055 adapter->login_rsp_buf_token = rsp_buffer_token; 3056 adapter->login_rsp_buf_sz = rsp_buffer_size; 3057 3058 login_buffer->len = cpu_to_be32(buffer_size); 3059 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB); 3060 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues); 3061 login_buffer->off_txcomp_subcrqs = 3062 cpu_to_be32(sizeof(struct ibmvnic_login_buffer)); 3063 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues); 3064 login_buffer->off_rxcomp_subcrqs = 3065 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) + 3066 sizeof(u64) * adapter->req_tx_queues); 3067 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token); 3068 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size); 3069 3070 tx_list_p = (__be64 *)((char *)login_buffer + 3071 sizeof(struct ibmvnic_login_buffer)); 3072 rx_list_p = (__be64 *)((char *)login_buffer + 3073 sizeof(struct ibmvnic_login_buffer) + 3074 sizeof(u64) * adapter->req_tx_queues); 3075 3076 for (i = 0; i < adapter->req_tx_queues; i++) { 3077 if (adapter->tx_scrq[i]) { 3078 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]-> 3079 crq_num); 3080 } 3081 } 3082 3083 for (i = 0; i < adapter->req_rx_queues; i++) { 3084 if (adapter->rx_scrq[i]) { 3085 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]-> 3086 crq_num); 3087 } 3088 } 3089 3090 /* Insert vNIC login client data */ 3091 vlcd = (struct vnic_login_client_data *) 3092 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues)); 3093 login_buffer->client_data_offset = 3094 cpu_to_be32((char *)vlcd - (char *)login_buffer); 3095 login_buffer->client_data_len = cpu_to_be32(client_data_len); 3096 3097 vnic_add_client_data(adapter, vlcd); 3098 3099 netdev_dbg(adapter->netdev, "Login Buffer:\n"); 3100 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) { 3101 netdev_dbg(adapter->netdev, "%016lx\n", 3102 ((unsigned long int *)(adapter->login_buf))[i]); 3103 } 3104 3105 memset(&crq, 0, sizeof(crq)); 3106 crq.login.first = IBMVNIC_CRQ_CMD; 3107 crq.login.cmd = LOGIN; 3108 crq.login.ioba = cpu_to_be32(buffer_token); 3109 crq.login.len = cpu_to_be32(buffer_size); 3110 ibmvnic_send_crq(adapter, &crq); 3111 3112 return; 3113 3114 buf_rsp_map_failed: 3115 kfree(login_rsp_buffer); 3116 buf_rsp_alloc_failed: 3117 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE); 3118 buf_map_failed: 3119 kfree(login_buffer); 3120 buf_alloc_failed: 3121 return; 3122 } 3123 3124 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr, 3125 u32 len, u8 map_id) 3126 { 3127 union ibmvnic_crq crq; 3128 3129 memset(&crq, 0, sizeof(crq)); 3130 crq.request_map.first = IBMVNIC_CRQ_CMD; 3131 crq.request_map.cmd = REQUEST_MAP; 3132 crq.request_map.map_id = map_id; 3133 crq.request_map.ioba = cpu_to_be32(addr); 3134 crq.request_map.len = cpu_to_be32(len); 3135 ibmvnic_send_crq(adapter, &crq); 3136 } 3137 3138 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id) 3139 { 3140 union ibmvnic_crq crq; 3141 3142 memset(&crq, 0, sizeof(crq)); 3143 crq.request_unmap.first = IBMVNIC_CRQ_CMD; 3144 crq.request_unmap.cmd = REQUEST_UNMAP; 3145 crq.request_unmap.map_id = map_id; 3146 ibmvnic_send_crq(adapter, &crq); 3147 } 3148 3149 static void send_map_query(struct ibmvnic_adapter *adapter) 3150 { 3151 union ibmvnic_crq crq; 3152 3153 memset(&crq, 0, sizeof(crq)); 3154 crq.query_map.first = IBMVNIC_CRQ_CMD; 3155 crq.query_map.cmd = QUERY_MAP; 3156 ibmvnic_send_crq(adapter, &crq); 3157 } 3158 3159 /* Send a series of CRQs requesting various capabilities of the VNIC server */ 3160 static void send_cap_queries(struct ibmvnic_adapter *adapter) 3161 { 3162 union ibmvnic_crq crq; 3163 3164 atomic_set(&adapter->running_cap_crqs, 0); 3165 memset(&crq, 0, sizeof(crq)); 3166 crq.query_capability.first = IBMVNIC_CRQ_CMD; 3167 crq.query_capability.cmd = QUERY_CAPABILITY; 3168 3169 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES); 3170 atomic_inc(&adapter->running_cap_crqs); 3171 ibmvnic_send_crq(adapter, &crq); 3172 3173 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES); 3174 atomic_inc(&adapter->running_cap_crqs); 3175 ibmvnic_send_crq(adapter, &crq); 3176 3177 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES); 3178 atomic_inc(&adapter->running_cap_crqs); 3179 ibmvnic_send_crq(adapter, &crq); 3180 3181 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES); 3182 atomic_inc(&adapter->running_cap_crqs); 3183 ibmvnic_send_crq(adapter, &crq); 3184 3185 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES); 3186 atomic_inc(&adapter->running_cap_crqs); 3187 ibmvnic_send_crq(adapter, &crq); 3188 3189 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES); 3190 atomic_inc(&adapter->running_cap_crqs); 3191 ibmvnic_send_crq(adapter, &crq); 3192 3193 crq.query_capability.capability = 3194 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ); 3195 atomic_inc(&adapter->running_cap_crqs); 3196 ibmvnic_send_crq(adapter, &crq); 3197 3198 crq.query_capability.capability = 3199 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ); 3200 atomic_inc(&adapter->running_cap_crqs); 3201 ibmvnic_send_crq(adapter, &crq); 3202 3203 crq.query_capability.capability = 3204 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ); 3205 atomic_inc(&adapter->running_cap_crqs); 3206 ibmvnic_send_crq(adapter, &crq); 3207 3208 crq.query_capability.capability = 3209 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ); 3210 atomic_inc(&adapter->running_cap_crqs); 3211 ibmvnic_send_crq(adapter, &crq); 3212 3213 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD); 3214 atomic_inc(&adapter->running_cap_crqs); 3215 ibmvnic_send_crq(adapter, &crq); 3216 3217 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED); 3218 atomic_inc(&adapter->running_cap_crqs); 3219 ibmvnic_send_crq(adapter, &crq); 3220 3221 crq.query_capability.capability = cpu_to_be16(MIN_MTU); 3222 atomic_inc(&adapter->running_cap_crqs); 3223 ibmvnic_send_crq(adapter, &crq); 3224 3225 crq.query_capability.capability = cpu_to_be16(MAX_MTU); 3226 atomic_inc(&adapter->running_cap_crqs); 3227 ibmvnic_send_crq(adapter, &crq); 3228 3229 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS); 3230 atomic_inc(&adapter->running_cap_crqs); 3231 ibmvnic_send_crq(adapter, &crq); 3232 3233 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION); 3234 atomic_inc(&adapter->running_cap_crqs); 3235 ibmvnic_send_crq(adapter, &crq); 3236 3237 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION); 3238 atomic_inc(&adapter->running_cap_crqs); 3239 ibmvnic_send_crq(adapter, &crq); 3240 3241 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES); 3242 atomic_inc(&adapter->running_cap_crqs); 3243 ibmvnic_send_crq(adapter, &crq); 3244 3245 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED); 3246 atomic_inc(&adapter->running_cap_crqs); 3247 ibmvnic_send_crq(adapter, &crq); 3248 3249 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES); 3250 atomic_inc(&adapter->running_cap_crqs); 3251 ibmvnic_send_crq(adapter, &crq); 3252 3253 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES); 3254 atomic_inc(&adapter->running_cap_crqs); 3255 ibmvnic_send_crq(adapter, &crq); 3256 3257 crq.query_capability.capability = 3258 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q); 3259 atomic_inc(&adapter->running_cap_crqs); 3260 ibmvnic_send_crq(adapter, &crq); 3261 3262 crq.query_capability.capability = 3263 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ); 3264 atomic_inc(&adapter->running_cap_crqs); 3265 ibmvnic_send_crq(adapter, &crq); 3266 3267 crq.query_capability.capability = 3268 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ); 3269 atomic_inc(&adapter->running_cap_crqs); 3270 ibmvnic_send_crq(adapter, &crq); 3271 3272 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ); 3273 atomic_inc(&adapter->running_cap_crqs); 3274 ibmvnic_send_crq(adapter, &crq); 3275 } 3276 3277 static void handle_vpd_size_rsp(union ibmvnic_crq *crq, 3278 struct ibmvnic_adapter *adapter) 3279 { 3280 struct device *dev = &adapter->vdev->dev; 3281 3282 if (crq->get_vpd_size_rsp.rc.code) { 3283 dev_err(dev, "Error retrieving VPD size, rc=%x\n", 3284 crq->get_vpd_size_rsp.rc.code); 3285 complete(&adapter->fw_done); 3286 return; 3287 } 3288 3289 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len); 3290 complete(&adapter->fw_done); 3291 } 3292 3293 static void handle_vpd_rsp(union ibmvnic_crq *crq, 3294 struct ibmvnic_adapter *adapter) 3295 { 3296 struct device *dev = &adapter->vdev->dev; 3297 unsigned char *substr = NULL; 3298 u8 fw_level_len = 0; 3299 3300 memset(adapter->fw_version, 0, 32); 3301 3302 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len, 3303 DMA_FROM_DEVICE); 3304 3305 if (crq->get_vpd_rsp.rc.code) { 3306 dev_err(dev, "Error retrieving VPD from device, rc=%x\n", 3307 crq->get_vpd_rsp.rc.code); 3308 goto complete; 3309 } 3310 3311 /* get the position of the firmware version info 3312 * located after the ASCII 'RM' substring in the buffer 3313 */ 3314 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len); 3315 if (!substr) { 3316 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n"); 3317 goto complete; 3318 } 3319 3320 /* get length of firmware level ASCII substring */ 3321 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) { 3322 fw_level_len = *(substr + 2); 3323 } else { 3324 dev_info(dev, "Length of FW substr extrapolated VDP buff\n"); 3325 goto complete; 3326 } 3327 3328 /* copy firmware version string from vpd into adapter */ 3329 if ((substr + 3 + fw_level_len) < 3330 (adapter->vpd->buff + adapter->vpd->len)) { 3331 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len); 3332 } else { 3333 dev_info(dev, "FW substr extrapolated VPD buff\n"); 3334 } 3335 3336 complete: 3337 if (adapter->fw_version[0] == '\0') 3338 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char)); 3339 complete(&adapter->fw_done); 3340 } 3341 3342 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter) 3343 { 3344 struct device *dev = &adapter->vdev->dev; 3345 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf; 3346 union ibmvnic_crq crq; 3347 int i; 3348 3349 dma_unmap_single(dev, adapter->ip_offload_tok, 3350 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE); 3351 3352 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n"); 3353 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++) 3354 netdev_dbg(adapter->netdev, "%016lx\n", 3355 ((unsigned long int *)(buf))[i]); 3356 3357 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum); 3358 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum); 3359 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n", 3360 buf->tcp_ipv4_chksum); 3361 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n", 3362 buf->tcp_ipv6_chksum); 3363 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n", 3364 buf->udp_ipv4_chksum); 3365 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n", 3366 buf->udp_ipv6_chksum); 3367 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n", 3368 buf->large_tx_ipv4); 3369 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n", 3370 buf->large_tx_ipv6); 3371 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n", 3372 buf->large_rx_ipv4); 3373 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n", 3374 buf->large_rx_ipv6); 3375 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n", 3376 buf->max_ipv4_header_size); 3377 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n", 3378 buf->max_ipv6_header_size); 3379 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n", 3380 buf->max_tcp_header_size); 3381 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n", 3382 buf->max_udp_header_size); 3383 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n", 3384 buf->max_large_tx_size); 3385 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n", 3386 buf->max_large_rx_size); 3387 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n", 3388 buf->ipv6_extension_header); 3389 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n", 3390 buf->tcp_pseudosum_req); 3391 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n", 3392 buf->num_ipv6_ext_headers); 3393 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n", 3394 buf->off_ipv6_ext_headers); 3395 3396 adapter->ip_offload_ctrl_tok = 3397 dma_map_single(dev, &adapter->ip_offload_ctrl, 3398 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE); 3399 3400 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) { 3401 dev_err(dev, "Couldn't map ip offload control buffer\n"); 3402 return; 3403 } 3404 3405 adapter->ip_offload_ctrl.len = 3406 cpu_to_be32(sizeof(adapter->ip_offload_ctrl)); 3407 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB); 3408 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum; 3409 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum; 3410 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum; 3411 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum; 3412 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum; 3413 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum; 3414 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4; 3415 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6; 3416 3417 /* large_rx disabled for now, additional features needed */ 3418 adapter->ip_offload_ctrl.large_rx_ipv4 = 0; 3419 adapter->ip_offload_ctrl.large_rx_ipv6 = 0; 3420 3421 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO; 3422 3423 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum) 3424 adapter->netdev->features |= NETIF_F_IP_CSUM; 3425 3426 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum) 3427 adapter->netdev->features |= NETIF_F_IPV6_CSUM; 3428 3429 if ((adapter->netdev->features & 3430 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))) 3431 adapter->netdev->features |= NETIF_F_RXCSUM; 3432 3433 if (buf->large_tx_ipv4) 3434 adapter->netdev->features |= NETIF_F_TSO; 3435 if (buf->large_tx_ipv6) 3436 adapter->netdev->features |= NETIF_F_TSO6; 3437 3438 adapter->netdev->hw_features |= adapter->netdev->features; 3439 3440 memset(&crq, 0, sizeof(crq)); 3441 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD; 3442 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD; 3443 crq.control_ip_offload.len = 3444 cpu_to_be32(sizeof(adapter->ip_offload_ctrl)); 3445 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok); 3446 ibmvnic_send_crq(adapter, &crq); 3447 } 3448 3449 static void handle_error_info_rsp(union ibmvnic_crq *crq, 3450 struct ibmvnic_adapter *adapter) 3451 { 3452 struct device *dev = &adapter->vdev->dev; 3453 struct ibmvnic_error_buff *error_buff, *tmp; 3454 unsigned long flags; 3455 bool found = false; 3456 int i; 3457 3458 if (!crq->request_error_rsp.rc.code) { 3459 dev_info(dev, "Request Error Rsp returned with rc=%x\n", 3460 crq->request_error_rsp.rc.code); 3461 return; 3462 } 3463 3464 spin_lock_irqsave(&adapter->error_list_lock, flags); 3465 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) 3466 if (error_buff->error_id == crq->request_error_rsp.error_id) { 3467 found = true; 3468 list_del(&error_buff->list); 3469 break; 3470 } 3471 spin_unlock_irqrestore(&adapter->error_list_lock, flags); 3472 3473 if (!found) { 3474 dev_err(dev, "Couldn't find error id %x\n", 3475 be32_to_cpu(crq->request_error_rsp.error_id)); 3476 return; 3477 } 3478 3479 dev_err(dev, "Detailed info for error id %x:", 3480 be32_to_cpu(crq->request_error_rsp.error_id)); 3481 3482 for (i = 0; i < error_buff->len; i++) { 3483 pr_cont("%02x", (int)error_buff->buff[i]); 3484 if (i % 8 == 7) 3485 pr_cont(" "); 3486 } 3487 pr_cont("\n"); 3488 3489 dma_unmap_single(dev, error_buff->dma, error_buff->len, 3490 DMA_FROM_DEVICE); 3491 kfree(error_buff->buff); 3492 kfree(error_buff); 3493 } 3494 3495 static void request_error_information(struct ibmvnic_adapter *adapter, 3496 union ibmvnic_crq *err_crq) 3497 { 3498 struct device *dev = &adapter->vdev->dev; 3499 struct net_device *netdev = adapter->netdev; 3500 struct ibmvnic_error_buff *error_buff; 3501 unsigned long timeout = msecs_to_jiffies(30000); 3502 union ibmvnic_crq crq; 3503 unsigned long flags; 3504 int rc, detail_len; 3505 3506 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC); 3507 if (!error_buff) 3508 return; 3509 3510 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz); 3511 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC); 3512 if (!error_buff->buff) { 3513 kfree(error_buff); 3514 return; 3515 } 3516 3517 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len, 3518 DMA_FROM_DEVICE); 3519 if (dma_mapping_error(dev, error_buff->dma)) { 3520 netdev_err(netdev, "Couldn't map error buffer\n"); 3521 kfree(error_buff->buff); 3522 kfree(error_buff); 3523 return; 3524 } 3525 3526 error_buff->len = detail_len; 3527 error_buff->error_id = err_crq->error_indication.error_id; 3528 3529 spin_lock_irqsave(&adapter->error_list_lock, flags); 3530 list_add_tail(&error_buff->list, &adapter->errors); 3531 spin_unlock_irqrestore(&adapter->error_list_lock, flags); 3532 3533 memset(&crq, 0, sizeof(crq)); 3534 crq.request_error_info.first = IBMVNIC_CRQ_CMD; 3535 crq.request_error_info.cmd = REQUEST_ERROR_INFO; 3536 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma); 3537 crq.request_error_info.len = cpu_to_be32(detail_len); 3538 crq.request_error_info.error_id = err_crq->error_indication.error_id; 3539 3540 rc = ibmvnic_send_crq(adapter, &crq); 3541 if (rc) { 3542 netdev_err(netdev, "failed to request error information\n"); 3543 goto err_info_fail; 3544 } 3545 3546 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) { 3547 netdev_err(netdev, "timeout waiting for error information\n"); 3548 goto err_info_fail; 3549 } 3550 3551 return; 3552 3553 err_info_fail: 3554 spin_lock_irqsave(&adapter->error_list_lock, flags); 3555 list_del(&error_buff->list); 3556 spin_unlock_irqrestore(&adapter->error_list_lock, flags); 3557 3558 kfree(error_buff->buff); 3559 kfree(error_buff); 3560 } 3561 3562 static void handle_error_indication(union ibmvnic_crq *crq, 3563 struct ibmvnic_adapter *adapter) 3564 { 3565 struct device *dev = &adapter->vdev->dev; 3566 3567 dev_err(dev, "Firmware reports %serror id %x, cause %d\n", 3568 crq->error_indication.flags 3569 & IBMVNIC_FATAL_ERROR ? "FATAL " : "", 3570 be32_to_cpu(crq->error_indication.error_id), 3571 be16_to_cpu(crq->error_indication.error_cause)); 3572 3573 if (be32_to_cpu(crq->error_indication.error_id)) 3574 request_error_information(adapter, crq); 3575 3576 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR) 3577 ibmvnic_reset(adapter, VNIC_RESET_FATAL); 3578 else 3579 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL); 3580 } 3581 3582 static int handle_change_mac_rsp(union ibmvnic_crq *crq, 3583 struct ibmvnic_adapter *adapter) 3584 { 3585 struct net_device *netdev = adapter->netdev; 3586 struct device *dev = &adapter->vdev->dev; 3587 long rc; 3588 3589 rc = crq->change_mac_addr_rsp.rc.code; 3590 if (rc) { 3591 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc); 3592 goto out; 3593 } 3594 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0], 3595 ETH_ALEN); 3596 out: 3597 complete(&adapter->fw_done); 3598 return rc; 3599 } 3600 3601 static void handle_request_cap_rsp(union ibmvnic_crq *crq, 3602 struct ibmvnic_adapter *adapter) 3603 { 3604 struct device *dev = &adapter->vdev->dev; 3605 u64 *req_value; 3606 char *name; 3607 3608 atomic_dec(&adapter->running_cap_crqs); 3609 switch (be16_to_cpu(crq->request_capability_rsp.capability)) { 3610 case REQ_TX_QUEUES: 3611 req_value = &adapter->req_tx_queues; 3612 name = "tx"; 3613 break; 3614 case REQ_RX_QUEUES: 3615 req_value = &adapter->req_rx_queues; 3616 name = "rx"; 3617 break; 3618 case REQ_RX_ADD_QUEUES: 3619 req_value = &adapter->req_rx_add_queues; 3620 name = "rx_add"; 3621 break; 3622 case REQ_TX_ENTRIES_PER_SUBCRQ: 3623 req_value = &adapter->req_tx_entries_per_subcrq; 3624 name = "tx_entries_per_subcrq"; 3625 break; 3626 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ: 3627 req_value = &adapter->req_rx_add_entries_per_subcrq; 3628 name = "rx_add_entries_per_subcrq"; 3629 break; 3630 case REQ_MTU: 3631 req_value = &adapter->req_mtu; 3632 name = "mtu"; 3633 break; 3634 case PROMISC_REQUESTED: 3635 req_value = &adapter->promisc; 3636 name = "promisc"; 3637 break; 3638 default: 3639 dev_err(dev, "Got invalid cap request rsp %d\n", 3640 crq->request_capability.capability); 3641 return; 3642 } 3643 3644 switch (crq->request_capability_rsp.rc.code) { 3645 case SUCCESS: 3646 break; 3647 case PARTIALSUCCESS: 3648 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n", 3649 *req_value, 3650 (long int)be64_to_cpu(crq->request_capability_rsp. 3651 number), name); 3652 3653 if (be16_to_cpu(crq->request_capability_rsp.capability) == 3654 REQ_MTU) { 3655 pr_err("mtu of %llu is not supported. Reverting.\n", 3656 *req_value); 3657 *req_value = adapter->fallback.mtu; 3658 } else { 3659 *req_value = 3660 be64_to_cpu(crq->request_capability_rsp.number); 3661 } 3662 3663 ibmvnic_send_req_caps(adapter, 1); 3664 return; 3665 default: 3666 dev_err(dev, "Error %d in request cap rsp\n", 3667 crq->request_capability_rsp.rc.code); 3668 return; 3669 } 3670 3671 /* Done receiving requested capabilities, query IP offload support */ 3672 if (atomic_read(&adapter->running_cap_crqs) == 0) { 3673 union ibmvnic_crq newcrq; 3674 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer); 3675 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf = 3676 &adapter->ip_offload_buf; 3677 3678 adapter->wait_capability = false; 3679 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf, 3680 buf_sz, 3681 DMA_FROM_DEVICE); 3682 3683 if (dma_mapping_error(dev, adapter->ip_offload_tok)) { 3684 if (!firmware_has_feature(FW_FEATURE_CMO)) 3685 dev_err(dev, "Couldn't map offload buffer\n"); 3686 return; 3687 } 3688 3689 memset(&newcrq, 0, sizeof(newcrq)); 3690 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD; 3691 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD; 3692 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz); 3693 newcrq.query_ip_offload.ioba = 3694 cpu_to_be32(adapter->ip_offload_tok); 3695 3696 ibmvnic_send_crq(adapter, &newcrq); 3697 } 3698 } 3699 3700 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq, 3701 struct ibmvnic_adapter *adapter) 3702 { 3703 struct device *dev = &adapter->vdev->dev; 3704 struct net_device *netdev = adapter->netdev; 3705 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf; 3706 struct ibmvnic_login_buffer *login = adapter->login_buf; 3707 int i; 3708 3709 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz, 3710 DMA_BIDIRECTIONAL); 3711 dma_unmap_single(dev, adapter->login_rsp_buf_token, 3712 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL); 3713 3714 /* If the number of queues requested can't be allocated by the 3715 * server, the login response will return with code 1. We will need 3716 * to resend the login buffer with fewer queues requested. 3717 */ 3718 if (login_rsp_crq->generic.rc.code) { 3719 adapter->renegotiate = true; 3720 complete(&adapter->init_done); 3721 return 0; 3722 } 3723 3724 netdev->mtu = adapter->req_mtu - ETH_HLEN; 3725 3726 netdev_dbg(adapter->netdev, "Login Response Buffer:\n"); 3727 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) { 3728 netdev_dbg(adapter->netdev, "%016lx\n", 3729 ((unsigned long int *)(adapter->login_rsp_buf))[i]); 3730 } 3731 3732 /* Sanity checks */ 3733 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs || 3734 (be32_to_cpu(login->num_rxcomp_subcrqs) * 3735 adapter->req_rx_add_queues != 3736 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) { 3737 dev_err(dev, "FATAL: Inconsistent login and login rsp\n"); 3738 ibmvnic_remove(adapter->vdev); 3739 return -EIO; 3740 } 3741 complete(&adapter->init_done); 3742 3743 return 0; 3744 } 3745 3746 static void handle_request_unmap_rsp(union ibmvnic_crq *crq, 3747 struct ibmvnic_adapter *adapter) 3748 { 3749 struct device *dev = &adapter->vdev->dev; 3750 long rc; 3751 3752 rc = crq->request_unmap_rsp.rc.code; 3753 if (rc) 3754 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc); 3755 } 3756 3757 static void handle_query_map_rsp(union ibmvnic_crq *crq, 3758 struct ibmvnic_adapter *adapter) 3759 { 3760 struct net_device *netdev = adapter->netdev; 3761 struct device *dev = &adapter->vdev->dev; 3762 long rc; 3763 3764 rc = crq->query_map_rsp.rc.code; 3765 if (rc) { 3766 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc); 3767 return; 3768 } 3769 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n", 3770 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages, 3771 crq->query_map_rsp.free_pages); 3772 } 3773 3774 static void handle_query_cap_rsp(union ibmvnic_crq *crq, 3775 struct ibmvnic_adapter *adapter) 3776 { 3777 struct net_device *netdev = adapter->netdev; 3778 struct device *dev = &adapter->vdev->dev; 3779 long rc; 3780 3781 atomic_dec(&adapter->running_cap_crqs); 3782 netdev_dbg(netdev, "Outstanding queries: %d\n", 3783 atomic_read(&adapter->running_cap_crqs)); 3784 rc = crq->query_capability.rc.code; 3785 if (rc) { 3786 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc); 3787 goto out; 3788 } 3789 3790 switch (be16_to_cpu(crq->query_capability.capability)) { 3791 case MIN_TX_QUEUES: 3792 adapter->min_tx_queues = 3793 be64_to_cpu(crq->query_capability.number); 3794 netdev_dbg(netdev, "min_tx_queues = %lld\n", 3795 adapter->min_tx_queues); 3796 break; 3797 case MIN_RX_QUEUES: 3798 adapter->min_rx_queues = 3799 be64_to_cpu(crq->query_capability.number); 3800 netdev_dbg(netdev, "min_rx_queues = %lld\n", 3801 adapter->min_rx_queues); 3802 break; 3803 case MIN_RX_ADD_QUEUES: 3804 adapter->min_rx_add_queues = 3805 be64_to_cpu(crq->query_capability.number); 3806 netdev_dbg(netdev, "min_rx_add_queues = %lld\n", 3807 adapter->min_rx_add_queues); 3808 break; 3809 case MAX_TX_QUEUES: 3810 adapter->max_tx_queues = 3811 be64_to_cpu(crq->query_capability.number); 3812 netdev_dbg(netdev, "max_tx_queues = %lld\n", 3813 adapter->max_tx_queues); 3814 break; 3815 case MAX_RX_QUEUES: 3816 adapter->max_rx_queues = 3817 be64_to_cpu(crq->query_capability.number); 3818 netdev_dbg(netdev, "max_rx_queues = %lld\n", 3819 adapter->max_rx_queues); 3820 break; 3821 case MAX_RX_ADD_QUEUES: 3822 adapter->max_rx_add_queues = 3823 be64_to_cpu(crq->query_capability.number); 3824 netdev_dbg(netdev, "max_rx_add_queues = %lld\n", 3825 adapter->max_rx_add_queues); 3826 break; 3827 case MIN_TX_ENTRIES_PER_SUBCRQ: 3828 adapter->min_tx_entries_per_subcrq = 3829 be64_to_cpu(crq->query_capability.number); 3830 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n", 3831 adapter->min_tx_entries_per_subcrq); 3832 break; 3833 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ: 3834 adapter->min_rx_add_entries_per_subcrq = 3835 be64_to_cpu(crq->query_capability.number); 3836 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n", 3837 adapter->min_rx_add_entries_per_subcrq); 3838 break; 3839 case MAX_TX_ENTRIES_PER_SUBCRQ: 3840 adapter->max_tx_entries_per_subcrq = 3841 be64_to_cpu(crq->query_capability.number); 3842 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n", 3843 adapter->max_tx_entries_per_subcrq); 3844 break; 3845 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ: 3846 adapter->max_rx_add_entries_per_subcrq = 3847 be64_to_cpu(crq->query_capability.number); 3848 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n", 3849 adapter->max_rx_add_entries_per_subcrq); 3850 break; 3851 case TCP_IP_OFFLOAD: 3852 adapter->tcp_ip_offload = 3853 be64_to_cpu(crq->query_capability.number); 3854 netdev_dbg(netdev, "tcp_ip_offload = %lld\n", 3855 adapter->tcp_ip_offload); 3856 break; 3857 case PROMISC_SUPPORTED: 3858 adapter->promisc_supported = 3859 be64_to_cpu(crq->query_capability.number); 3860 netdev_dbg(netdev, "promisc_supported = %lld\n", 3861 adapter->promisc_supported); 3862 break; 3863 case MIN_MTU: 3864 adapter->min_mtu = be64_to_cpu(crq->query_capability.number); 3865 netdev->min_mtu = adapter->min_mtu - ETH_HLEN; 3866 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu); 3867 break; 3868 case MAX_MTU: 3869 adapter->max_mtu = be64_to_cpu(crq->query_capability.number); 3870 netdev->max_mtu = adapter->max_mtu - ETH_HLEN; 3871 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu); 3872 break; 3873 case MAX_MULTICAST_FILTERS: 3874 adapter->max_multicast_filters = 3875 be64_to_cpu(crq->query_capability.number); 3876 netdev_dbg(netdev, "max_multicast_filters = %lld\n", 3877 adapter->max_multicast_filters); 3878 break; 3879 case VLAN_HEADER_INSERTION: 3880 adapter->vlan_header_insertion = 3881 be64_to_cpu(crq->query_capability.number); 3882 if (adapter->vlan_header_insertion) 3883 netdev->features |= NETIF_F_HW_VLAN_STAG_TX; 3884 netdev_dbg(netdev, "vlan_header_insertion = %lld\n", 3885 adapter->vlan_header_insertion); 3886 break; 3887 case RX_VLAN_HEADER_INSERTION: 3888 adapter->rx_vlan_header_insertion = 3889 be64_to_cpu(crq->query_capability.number); 3890 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n", 3891 adapter->rx_vlan_header_insertion); 3892 break; 3893 case MAX_TX_SG_ENTRIES: 3894 adapter->max_tx_sg_entries = 3895 be64_to_cpu(crq->query_capability.number); 3896 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n", 3897 adapter->max_tx_sg_entries); 3898 break; 3899 case RX_SG_SUPPORTED: 3900 adapter->rx_sg_supported = 3901 be64_to_cpu(crq->query_capability.number); 3902 netdev_dbg(netdev, "rx_sg_supported = %lld\n", 3903 adapter->rx_sg_supported); 3904 break; 3905 case OPT_TX_COMP_SUB_QUEUES: 3906 adapter->opt_tx_comp_sub_queues = 3907 be64_to_cpu(crq->query_capability.number); 3908 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n", 3909 adapter->opt_tx_comp_sub_queues); 3910 break; 3911 case OPT_RX_COMP_QUEUES: 3912 adapter->opt_rx_comp_queues = 3913 be64_to_cpu(crq->query_capability.number); 3914 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n", 3915 adapter->opt_rx_comp_queues); 3916 break; 3917 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q: 3918 adapter->opt_rx_bufadd_q_per_rx_comp_q = 3919 be64_to_cpu(crq->query_capability.number); 3920 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n", 3921 adapter->opt_rx_bufadd_q_per_rx_comp_q); 3922 break; 3923 case OPT_TX_ENTRIES_PER_SUBCRQ: 3924 adapter->opt_tx_entries_per_subcrq = 3925 be64_to_cpu(crq->query_capability.number); 3926 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n", 3927 adapter->opt_tx_entries_per_subcrq); 3928 break; 3929 case OPT_RXBA_ENTRIES_PER_SUBCRQ: 3930 adapter->opt_rxba_entries_per_subcrq = 3931 be64_to_cpu(crq->query_capability.number); 3932 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n", 3933 adapter->opt_rxba_entries_per_subcrq); 3934 break; 3935 case TX_RX_DESC_REQ: 3936 adapter->tx_rx_desc_req = crq->query_capability.number; 3937 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n", 3938 adapter->tx_rx_desc_req); 3939 break; 3940 3941 default: 3942 netdev_err(netdev, "Got invalid cap rsp %d\n", 3943 crq->query_capability.capability); 3944 } 3945 3946 out: 3947 if (atomic_read(&adapter->running_cap_crqs) == 0) { 3948 adapter->wait_capability = false; 3949 ibmvnic_send_req_caps(adapter, 0); 3950 } 3951 } 3952 3953 static void ibmvnic_handle_crq(union ibmvnic_crq *crq, 3954 struct ibmvnic_adapter *adapter) 3955 { 3956 struct ibmvnic_generic_crq *gen_crq = &crq->generic; 3957 struct net_device *netdev = adapter->netdev; 3958 struct device *dev = &adapter->vdev->dev; 3959 u64 *u64_crq = (u64 *)crq; 3960 long rc; 3961 3962 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n", 3963 (unsigned long int)cpu_to_be64(u64_crq[0]), 3964 (unsigned long int)cpu_to_be64(u64_crq[1])); 3965 switch (gen_crq->first) { 3966 case IBMVNIC_CRQ_INIT_RSP: 3967 switch (gen_crq->cmd) { 3968 case IBMVNIC_CRQ_INIT: 3969 dev_info(dev, "Partner initialized\n"); 3970 adapter->from_passive_init = true; 3971 complete(&adapter->init_done); 3972 break; 3973 case IBMVNIC_CRQ_INIT_COMPLETE: 3974 dev_info(dev, "Partner initialization complete\n"); 3975 send_version_xchg(adapter); 3976 break; 3977 default: 3978 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd); 3979 } 3980 return; 3981 case IBMVNIC_CRQ_XPORT_EVENT: 3982 netif_carrier_off(netdev); 3983 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) { 3984 dev_info(dev, "Migrated, re-enabling adapter\n"); 3985 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY); 3986 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) { 3987 dev_info(dev, "Backing device failover detected\n"); 3988 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER); 3989 } else { 3990 /* The adapter lost the connection */ 3991 dev_err(dev, "Virtual Adapter failed (rc=%d)\n", 3992 gen_crq->cmd); 3993 ibmvnic_reset(adapter, VNIC_RESET_FATAL); 3994 } 3995 return; 3996 case IBMVNIC_CRQ_CMD_RSP: 3997 break; 3998 default: 3999 dev_err(dev, "Got an invalid msg type 0x%02x\n", 4000 gen_crq->first); 4001 return; 4002 } 4003 4004 switch (gen_crq->cmd) { 4005 case VERSION_EXCHANGE_RSP: 4006 rc = crq->version_exchange_rsp.rc.code; 4007 if (rc) { 4008 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc); 4009 break; 4010 } 4011 dev_info(dev, "Partner protocol version is %d\n", 4012 crq->version_exchange_rsp.version); 4013 if (be16_to_cpu(crq->version_exchange_rsp.version) < 4014 ibmvnic_version) 4015 ibmvnic_version = 4016 be16_to_cpu(crq->version_exchange_rsp.version); 4017 send_cap_queries(adapter); 4018 break; 4019 case QUERY_CAPABILITY_RSP: 4020 handle_query_cap_rsp(crq, adapter); 4021 break; 4022 case QUERY_MAP_RSP: 4023 handle_query_map_rsp(crq, adapter); 4024 break; 4025 case REQUEST_MAP_RSP: 4026 adapter->fw_done_rc = crq->request_map_rsp.rc.code; 4027 complete(&adapter->fw_done); 4028 break; 4029 case REQUEST_UNMAP_RSP: 4030 handle_request_unmap_rsp(crq, adapter); 4031 break; 4032 case REQUEST_CAPABILITY_RSP: 4033 handle_request_cap_rsp(crq, adapter); 4034 break; 4035 case LOGIN_RSP: 4036 netdev_dbg(netdev, "Got Login Response\n"); 4037 handle_login_rsp(crq, adapter); 4038 break; 4039 case LOGICAL_LINK_STATE_RSP: 4040 netdev_dbg(netdev, 4041 "Got Logical Link State Response, state: %d rc: %d\n", 4042 crq->logical_link_state_rsp.link_state, 4043 crq->logical_link_state_rsp.rc.code); 4044 adapter->logical_link_state = 4045 crq->logical_link_state_rsp.link_state; 4046 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code; 4047 complete(&adapter->init_done); 4048 break; 4049 case LINK_STATE_INDICATION: 4050 netdev_dbg(netdev, "Got Logical Link State Indication\n"); 4051 adapter->phys_link_state = 4052 crq->link_state_indication.phys_link_state; 4053 adapter->logical_link_state = 4054 crq->link_state_indication.logical_link_state; 4055 break; 4056 case CHANGE_MAC_ADDR_RSP: 4057 netdev_dbg(netdev, "Got MAC address change Response\n"); 4058 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter); 4059 break; 4060 case ERROR_INDICATION: 4061 netdev_dbg(netdev, "Got Error Indication\n"); 4062 handle_error_indication(crq, adapter); 4063 break; 4064 case REQUEST_ERROR_RSP: 4065 netdev_dbg(netdev, "Got Error Detail Response\n"); 4066 handle_error_info_rsp(crq, adapter); 4067 break; 4068 case REQUEST_STATISTICS_RSP: 4069 netdev_dbg(netdev, "Got Statistics Response\n"); 4070 complete(&adapter->stats_done); 4071 break; 4072 case QUERY_IP_OFFLOAD_RSP: 4073 netdev_dbg(netdev, "Got Query IP offload Response\n"); 4074 handle_query_ip_offload_rsp(adapter); 4075 break; 4076 case MULTICAST_CTRL_RSP: 4077 netdev_dbg(netdev, "Got multicast control Response\n"); 4078 break; 4079 case CONTROL_IP_OFFLOAD_RSP: 4080 netdev_dbg(netdev, "Got Control IP offload Response\n"); 4081 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok, 4082 sizeof(adapter->ip_offload_ctrl), 4083 DMA_TO_DEVICE); 4084 complete(&adapter->init_done); 4085 break; 4086 case COLLECT_FW_TRACE_RSP: 4087 netdev_dbg(netdev, "Got Collect firmware trace Response\n"); 4088 complete(&adapter->fw_done); 4089 break; 4090 case GET_VPD_SIZE_RSP: 4091 handle_vpd_size_rsp(crq, adapter); 4092 break; 4093 case GET_VPD_RSP: 4094 handle_vpd_rsp(crq, adapter); 4095 break; 4096 default: 4097 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n", 4098 gen_crq->cmd); 4099 } 4100 } 4101 4102 static irqreturn_t ibmvnic_interrupt(int irq, void *instance) 4103 { 4104 struct ibmvnic_adapter *adapter = instance; 4105 4106 tasklet_schedule(&adapter->tasklet); 4107 return IRQ_HANDLED; 4108 } 4109 4110 static void ibmvnic_tasklet(void *data) 4111 { 4112 struct ibmvnic_adapter *adapter = data; 4113 struct ibmvnic_crq_queue *queue = &adapter->crq; 4114 union ibmvnic_crq *crq; 4115 unsigned long flags; 4116 bool done = false; 4117 4118 spin_lock_irqsave(&queue->lock, flags); 4119 while (!done) { 4120 /* Pull all the valid messages off the CRQ */ 4121 while ((crq = ibmvnic_next_crq(adapter)) != NULL) { 4122 ibmvnic_handle_crq(crq, adapter); 4123 crq->generic.first = 0; 4124 } 4125 4126 /* remain in tasklet until all 4127 * capabilities responses are received 4128 */ 4129 if (!adapter->wait_capability) 4130 done = true; 4131 } 4132 /* if capabilities CRQ's were sent in this tasklet, the following 4133 * tasklet must wait until all responses are received 4134 */ 4135 if (atomic_read(&adapter->running_cap_crqs) != 0) 4136 adapter->wait_capability = true; 4137 spin_unlock_irqrestore(&queue->lock, flags); 4138 } 4139 4140 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter) 4141 { 4142 struct vio_dev *vdev = adapter->vdev; 4143 int rc; 4144 4145 do { 4146 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address); 4147 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); 4148 4149 if (rc) 4150 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc); 4151 4152 return rc; 4153 } 4154 4155 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter) 4156 { 4157 struct ibmvnic_crq_queue *crq = &adapter->crq; 4158 struct device *dev = &adapter->vdev->dev; 4159 struct vio_dev *vdev = adapter->vdev; 4160 int rc; 4161 4162 /* Close the CRQ */ 4163 do { 4164 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 4165 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 4166 4167 /* Clean out the queue */ 4168 memset(crq->msgs, 0, PAGE_SIZE); 4169 crq->cur = 0; 4170 4171 /* And re-open it again */ 4172 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address, 4173 crq->msg_token, PAGE_SIZE); 4174 4175 if (rc == H_CLOSED) 4176 /* Adapter is good, but other end is not ready */ 4177 dev_warn(dev, "Partner adapter not ready\n"); 4178 else if (rc != 0) 4179 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc); 4180 4181 return rc; 4182 } 4183 4184 static void release_crq_queue(struct ibmvnic_adapter *adapter) 4185 { 4186 struct ibmvnic_crq_queue *crq = &adapter->crq; 4187 struct vio_dev *vdev = adapter->vdev; 4188 long rc; 4189 4190 if (!crq->msgs) 4191 return; 4192 4193 netdev_dbg(adapter->netdev, "Releasing CRQ\n"); 4194 free_irq(vdev->irq, adapter); 4195 tasklet_kill(&adapter->tasklet); 4196 do { 4197 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 4198 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 4199 4200 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE, 4201 DMA_BIDIRECTIONAL); 4202 free_page((unsigned long)crq->msgs); 4203 crq->msgs = NULL; 4204 } 4205 4206 static int init_crq_queue(struct ibmvnic_adapter *adapter) 4207 { 4208 struct ibmvnic_crq_queue *crq = &adapter->crq; 4209 struct device *dev = &adapter->vdev->dev; 4210 struct vio_dev *vdev = adapter->vdev; 4211 int rc, retrc = -ENOMEM; 4212 4213 if (crq->msgs) 4214 return 0; 4215 4216 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL); 4217 /* Should we allocate more than one page? */ 4218 4219 if (!crq->msgs) 4220 return -ENOMEM; 4221 4222 crq->size = PAGE_SIZE / sizeof(*crq->msgs); 4223 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE, 4224 DMA_BIDIRECTIONAL); 4225 if (dma_mapping_error(dev, crq->msg_token)) 4226 goto map_failed; 4227 4228 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address, 4229 crq->msg_token, PAGE_SIZE); 4230 4231 if (rc == H_RESOURCE) 4232 /* maybe kexecing and resource is busy. try a reset */ 4233 rc = ibmvnic_reset_crq(adapter); 4234 retrc = rc; 4235 4236 if (rc == H_CLOSED) { 4237 dev_warn(dev, "Partner adapter not ready\n"); 4238 } else if (rc) { 4239 dev_warn(dev, "Error %d opening adapter\n", rc); 4240 goto reg_crq_failed; 4241 } 4242 4243 retrc = 0; 4244 4245 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet, 4246 (unsigned long)adapter); 4247 4248 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq); 4249 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME, 4250 adapter); 4251 if (rc) { 4252 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", 4253 vdev->irq, rc); 4254 goto req_irq_failed; 4255 } 4256 4257 rc = vio_enable_interrupts(vdev); 4258 if (rc) { 4259 dev_err(dev, "Error %d enabling interrupts\n", rc); 4260 goto req_irq_failed; 4261 } 4262 4263 crq->cur = 0; 4264 spin_lock_init(&crq->lock); 4265 4266 return retrc; 4267 4268 req_irq_failed: 4269 tasklet_kill(&adapter->tasklet); 4270 do { 4271 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 4272 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 4273 reg_crq_failed: 4274 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL); 4275 map_failed: 4276 free_page((unsigned long)crq->msgs); 4277 crq->msgs = NULL; 4278 return retrc; 4279 } 4280 4281 static int ibmvnic_init(struct ibmvnic_adapter *adapter) 4282 { 4283 struct device *dev = &adapter->vdev->dev; 4284 unsigned long timeout = msecs_to_jiffies(30000); 4285 int rc; 4286 4287 if (adapter->resetting && !adapter->wait_for_reset) { 4288 rc = ibmvnic_reset_crq(adapter); 4289 if (!rc) 4290 rc = vio_enable_interrupts(adapter->vdev); 4291 } else { 4292 rc = init_crq_queue(adapter); 4293 } 4294 4295 if (rc) { 4296 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc); 4297 return rc; 4298 } 4299 4300 adapter->from_passive_init = false; 4301 4302 init_completion(&adapter->init_done); 4303 adapter->init_done_rc = 0; 4304 ibmvnic_send_crq_init(adapter); 4305 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) { 4306 dev_err(dev, "Initialization sequence timed out\n"); 4307 return -1; 4308 } 4309 4310 if (adapter->init_done_rc) { 4311 release_crq_queue(adapter); 4312 return adapter->init_done_rc; 4313 } 4314 4315 if (adapter->from_passive_init) { 4316 adapter->state = VNIC_OPEN; 4317 adapter->from_passive_init = false; 4318 return -1; 4319 } 4320 4321 if (adapter->resetting && !adapter->wait_for_reset) 4322 rc = reset_sub_crq_queues(adapter); 4323 else 4324 rc = init_sub_crqs(adapter); 4325 if (rc) { 4326 dev_err(dev, "Initialization of sub crqs failed\n"); 4327 release_crq_queue(adapter); 4328 return rc; 4329 } 4330 4331 rc = init_sub_crq_irqs(adapter); 4332 if (rc) { 4333 dev_err(dev, "Failed to initialize sub crq irqs\n"); 4334 release_crq_queue(adapter); 4335 } 4336 4337 return rc; 4338 } 4339 4340 static struct device_attribute dev_attr_failover; 4341 4342 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id) 4343 { 4344 struct ibmvnic_adapter *adapter; 4345 struct net_device *netdev; 4346 unsigned char *mac_addr_p; 4347 int rc; 4348 4349 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n", 4350 dev->unit_address); 4351 4352 mac_addr_p = (unsigned char *)vio_get_attribute(dev, 4353 VETH_MAC_ADDR, NULL); 4354 if (!mac_addr_p) { 4355 dev_err(&dev->dev, 4356 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n", 4357 __FILE__, __LINE__); 4358 return 0; 4359 } 4360 4361 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter), 4362 IBMVNIC_MAX_QUEUES); 4363 if (!netdev) 4364 return -ENOMEM; 4365 4366 adapter = netdev_priv(netdev); 4367 adapter->state = VNIC_PROBING; 4368 dev_set_drvdata(&dev->dev, netdev); 4369 adapter->vdev = dev; 4370 adapter->netdev = netdev; 4371 4372 ether_addr_copy(adapter->mac_addr, mac_addr_p); 4373 ether_addr_copy(netdev->dev_addr, adapter->mac_addr); 4374 netdev->irq = dev->irq; 4375 netdev->netdev_ops = &ibmvnic_netdev_ops; 4376 netdev->ethtool_ops = &ibmvnic_ethtool_ops; 4377 SET_NETDEV_DEV(netdev, &dev->dev); 4378 4379 spin_lock_init(&adapter->stats_lock); 4380 4381 INIT_LIST_HEAD(&adapter->errors); 4382 spin_lock_init(&adapter->error_list_lock); 4383 4384 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset); 4385 INIT_LIST_HEAD(&adapter->rwi_list); 4386 mutex_init(&adapter->reset_lock); 4387 mutex_init(&adapter->rwi_lock); 4388 adapter->resetting = false; 4389 4390 adapter->mac_change_pending = false; 4391 4392 do { 4393 rc = ibmvnic_init(adapter); 4394 if (rc && rc != EAGAIN) 4395 goto ibmvnic_init_fail; 4396 } while (rc == EAGAIN); 4397 4398 netdev->mtu = adapter->req_mtu - ETH_HLEN; 4399 netdev->min_mtu = adapter->min_mtu - ETH_HLEN; 4400 netdev->max_mtu = adapter->max_mtu - ETH_HLEN; 4401 4402 rc = device_create_file(&dev->dev, &dev_attr_failover); 4403 if (rc) 4404 goto ibmvnic_init_fail; 4405 4406 netif_carrier_off(netdev); 4407 rc = register_netdev(netdev); 4408 if (rc) { 4409 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc); 4410 goto ibmvnic_register_fail; 4411 } 4412 dev_info(&dev->dev, "ibmvnic registered\n"); 4413 4414 adapter->state = VNIC_PROBED; 4415 4416 adapter->wait_for_reset = false; 4417 4418 return 0; 4419 4420 ibmvnic_register_fail: 4421 device_remove_file(&dev->dev, &dev_attr_failover); 4422 4423 ibmvnic_init_fail: 4424 release_sub_crqs(adapter); 4425 release_crq_queue(adapter); 4426 free_netdev(netdev); 4427 4428 return rc; 4429 } 4430 4431 static int ibmvnic_remove(struct vio_dev *dev) 4432 { 4433 struct net_device *netdev = dev_get_drvdata(&dev->dev); 4434 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 4435 4436 adapter->state = VNIC_REMOVING; 4437 unregister_netdev(netdev); 4438 mutex_lock(&adapter->reset_lock); 4439 4440 release_resources(adapter); 4441 release_sub_crqs(adapter); 4442 release_crq_queue(adapter); 4443 4444 adapter->state = VNIC_REMOVED; 4445 4446 mutex_unlock(&adapter->reset_lock); 4447 device_remove_file(&dev->dev, &dev_attr_failover); 4448 free_netdev(netdev); 4449 dev_set_drvdata(&dev->dev, NULL); 4450 4451 return 0; 4452 } 4453 4454 static ssize_t failover_store(struct device *dev, struct device_attribute *attr, 4455 const char *buf, size_t count) 4456 { 4457 struct net_device *netdev = dev_get_drvdata(dev); 4458 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 4459 unsigned long retbuf[PLPAR_HCALL_BUFSIZE]; 4460 __be64 session_token; 4461 long rc; 4462 4463 if (!sysfs_streq(buf, "1")) 4464 return -EINVAL; 4465 4466 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address, 4467 H_GET_SESSION_TOKEN, 0, 0, 0); 4468 if (rc) { 4469 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n", 4470 rc); 4471 return -EINVAL; 4472 } 4473 4474 session_token = (__be64)retbuf[0]; 4475 netdev_dbg(netdev, "Initiating client failover, session id %llx\n", 4476 be64_to_cpu(session_token)); 4477 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address, 4478 H_SESSION_ERR_DETECTED, session_token, 0, 0); 4479 if (rc) { 4480 netdev_err(netdev, "Client initiated failover failed, rc %ld\n", 4481 rc); 4482 return -EINVAL; 4483 } 4484 4485 return count; 4486 } 4487 4488 static DEVICE_ATTR_WO(failover); 4489 4490 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev) 4491 { 4492 struct net_device *netdev = dev_get_drvdata(&vdev->dev); 4493 struct ibmvnic_adapter *adapter; 4494 struct iommu_table *tbl; 4495 unsigned long ret = 0; 4496 int i; 4497 4498 tbl = get_iommu_table_base(&vdev->dev); 4499 4500 /* netdev inits at probe time along with the structures we need below*/ 4501 if (!netdev) 4502 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl); 4503 4504 adapter = netdev_priv(netdev); 4505 4506 ret += PAGE_SIZE; /* the crq message queue */ 4507 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl); 4508 4509 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++) 4510 ret += 4 * PAGE_SIZE; /* the scrq message queue */ 4511 4512 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs); 4513 i++) 4514 ret += adapter->rx_pool[i].size * 4515 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl); 4516 4517 return ret; 4518 } 4519 4520 static int ibmvnic_resume(struct device *dev) 4521 { 4522 struct net_device *netdev = dev_get_drvdata(dev); 4523 struct ibmvnic_adapter *adapter = netdev_priv(netdev); 4524 4525 if (adapter->state != VNIC_OPEN) 4526 return 0; 4527 4528 tasklet_schedule(&adapter->tasklet); 4529 4530 return 0; 4531 } 4532 4533 static const struct vio_device_id ibmvnic_device_table[] = { 4534 {"network", "IBM,vnic"}, 4535 {"", "" } 4536 }; 4537 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table); 4538 4539 static const struct dev_pm_ops ibmvnic_pm_ops = { 4540 .resume = ibmvnic_resume 4541 }; 4542 4543 static struct vio_driver ibmvnic_driver = { 4544 .id_table = ibmvnic_device_table, 4545 .probe = ibmvnic_probe, 4546 .remove = ibmvnic_remove, 4547 .get_desired_dma = ibmvnic_get_desired_dma, 4548 .name = ibmvnic_driver_name, 4549 .pm = &ibmvnic_pm_ops, 4550 }; 4551 4552 /* module functions */ 4553 static int __init ibmvnic_module_init(void) 4554 { 4555 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string, 4556 IBMVNIC_DRIVER_VERSION); 4557 4558 return vio_register_driver(&ibmvnic_driver); 4559 } 4560 4561 static void __exit ibmvnic_module_exit(void) 4562 { 4563 vio_unregister_driver(&ibmvnic_driver); 4564 } 4565 4566 module_init(ibmvnic_module_init); 4567 module_exit(ibmvnic_module_exit); 4568