blob: b918bc2f2e4fa059d84b3c276d9b7a62c41d4f15 [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
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/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040077#include <linux/if_vlan.h>
Nathan Fontenot37798d02017-11-08 11:23:56 -060078#include <linux/utsname.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060079
80#include "ibmvnic.h"
81
82static const char ibmvnic_driver_name[] = "ibmvnic";
83static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
84
Thomas Falcon78b07ac2017-06-01 15:32:34 -050085MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060086MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
89
90static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
91static int ibmvnic_remove(struct vio_dev *);
92static void release_sub_crqs(struct ibmvnic_adapter *);
93static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
94static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
95static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
96static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
97static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
98 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050099static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600100static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
101static int enable_scrq_irq(struct ibmvnic_adapter *,
102 struct ibmvnic_sub_crq_queue *);
103static int disable_scrq_irq(struct ibmvnic_adapter *,
104 struct ibmvnic_sub_crq_queue *);
105static int pending_scrq(struct ibmvnic_adapter *,
106 struct ibmvnic_sub_crq_queue *);
107static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
108 struct ibmvnic_sub_crq_queue *);
109static int ibmvnic_poll(struct napi_struct *napi, int data);
110static void send_map_query(struct ibmvnic_adapter *adapter);
111static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
112static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500113static void send_login(struct ibmvnic_adapter *adapter);
114static void send_cap_queries(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500115static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500116static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500117static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400118static void release_crq_queue(struct ibmvnic_adapter *);
John Allenc26eba02017-10-26 16:23:25 -0500119static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600120
121struct ibmvnic_stat {
122 char name[ETH_GSTRING_LEN];
123 int offset;
124};
125
126#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
127 offsetof(struct ibmvnic_statistics, stat))
128#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
129
130static const struct ibmvnic_stat ibmvnic_stats[] = {
131 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
132 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
133 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
134 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
135 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
136 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
137 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
138 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
139 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
140 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
141 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
142 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
143 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
144 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
145 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
146 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
147 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
148 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
149 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
150 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
151 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
152 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
153};
154
155static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
156 unsigned long length, unsigned long *number,
157 unsigned long *irq)
158{
159 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
160 long rc;
161
162 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
163 *number = retbuf[0];
164 *irq = retbuf[1];
165
166 return rc;
167}
168
Thomas Falcon032c5e82015-12-21 11:26:06 -0600169static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
170 struct ibmvnic_long_term_buff *ltb, int size)
171{
172 struct device *dev = &adapter->vdev->dev;
173
174 ltb->size = size;
175 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
176 GFP_KERNEL);
177
178 if (!ltb->buff) {
179 dev_err(dev, "Couldn't alloc long term buffer\n");
180 return -ENOMEM;
181 }
182 ltb->map_id = adapter->map_id;
183 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500184
185 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600186 send_request_map(adapter, ltb->addr,
187 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600188 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500189
190 if (adapter->fw_done_rc) {
191 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
192 adapter->fw_done_rc);
193 return -1;
194 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600195 return 0;
196}
197
198static void free_long_term_buff(struct ibmvnic_adapter *adapter,
199 struct ibmvnic_long_term_buff *ltb)
200{
201 struct device *dev = &adapter->vdev->dev;
202
Nathan Fontenotc657e322017-03-30 02:49:06 -0400203 if (!ltb->buff)
204 return;
205
Nathan Fontenoted651a12017-05-03 14:04:38 -0400206 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
207 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500208 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400209 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600210}
211
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500212static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
213 struct ibmvnic_long_term_buff *ltb)
214{
215 memset(ltb->buff, 0, ltb->size);
216
217 init_completion(&adapter->fw_done);
218 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
219 wait_for_completion(&adapter->fw_done);
220
221 if (adapter->fw_done_rc) {
222 dev_info(&adapter->vdev->dev,
223 "Reset failed, attempting to free and reallocate buffer\n");
224 free_long_term_buff(adapter, ltb);
225 return alloc_long_term_buff(adapter, ltb, ltb->size);
226 }
227 return 0;
228}
229
Thomas Falconf185a492017-05-26 10:30:48 -0400230static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
231{
232 int i;
233
234 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
235 i++)
236 adapter->rx_pool[i].active = 0;
237}
238
Thomas Falcon032c5e82015-12-21 11:26:06 -0600239static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
240 struct ibmvnic_rx_pool *pool)
241{
242 int count = pool->size - atomic_read(&pool->available);
243 struct device *dev = &adapter->vdev->dev;
244 int buffers_added = 0;
245 unsigned long lpar_rc;
246 union sub_crq sub_crq;
247 struct sk_buff *skb;
248 unsigned int offset;
249 dma_addr_t dma_addr;
250 unsigned char *dst;
251 u64 *handle_array;
252 int shift = 0;
253 int index;
254 int i;
255
Thomas Falconf185a492017-05-26 10:30:48 -0400256 if (!pool->active)
257 return;
258
Thomas Falcon032c5e82015-12-21 11:26:06 -0600259 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
260 be32_to_cpu(adapter->login_rsp_buf->
261 off_rxadd_subcrqs));
262
263 for (i = 0; i < count; ++i) {
264 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
265 if (!skb) {
266 dev_err(dev, "Couldn't replenish rx buff\n");
267 adapter->replenish_no_mem++;
268 break;
269 }
270
271 index = pool->free_map[pool->next_free];
272
273 if (pool->rx_buff[index].skb)
274 dev_err(dev, "Inconsistent free_map!\n");
275
276 /* Copy the skb to the long term mapped DMA buffer */
277 offset = index * pool->buff_size;
278 dst = pool->long_term_buff.buff + offset;
279 memset(dst, 0, pool->buff_size);
280 dma_addr = pool->long_term_buff.addr + offset;
281 pool->rx_buff[index].data = dst;
282
283 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
284 pool->rx_buff[index].dma = dma_addr;
285 pool->rx_buff[index].skb = skb;
286 pool->rx_buff[index].pool_index = pool->index;
287 pool->rx_buff[index].size = pool->buff_size;
288
289 memset(&sub_crq, 0, sizeof(sub_crq));
290 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
291 sub_crq.rx_add.correlator =
292 cpu_to_be64((u64)&pool->rx_buff[index]);
293 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
294 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
295
296 /* The length field of the sCRQ is defined to be 24 bits so the
297 * buffer size needs to be left shifted by a byte before it is
298 * converted to big endian to prevent the last byte from being
299 * truncated.
300 */
301#ifdef __LITTLE_ENDIAN__
302 shift = 8;
303#endif
304 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
305
306 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
307 &sub_crq);
308 if (lpar_rc != H_SUCCESS)
309 goto failure;
310
311 buffers_added++;
312 adapter->replenish_add_buff_success++;
313 pool->next_free = (pool->next_free + 1) % pool->size;
314 }
315 atomic_add(buffers_added, &pool->available);
316 return;
317
318failure:
319 dev_info(dev, "replenish pools failure\n");
320 pool->free_map[pool->next_free] = index;
321 pool->rx_buff[index].skb = NULL;
322 if (!dma_mapping_error(dev, dma_addr))
323 dma_unmap_single(dev, dma_addr, pool->buff_size,
324 DMA_FROM_DEVICE);
325
326 dev_kfree_skb_any(skb);
327 adapter->replenish_add_buff_failure++;
328 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400329
330 if (lpar_rc == H_CLOSED) {
331 /* Disable buffer pool replenishment and report carrier off if
332 * queue is closed. Firmware guarantees that a signal will
333 * be sent to the driver, triggering a reset.
334 */
335 deactivate_rx_pools(adapter);
336 netif_carrier_off(adapter->netdev);
337 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600338}
339
340static void replenish_pools(struct ibmvnic_adapter *adapter)
341{
342 int i;
343
Thomas Falcon032c5e82015-12-21 11:26:06 -0600344 adapter->replenish_task_cycles++;
345 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
346 i++) {
347 if (adapter->rx_pool[i].active)
348 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
349 }
350}
351
John Allen3d52b592017-08-02 16:44:14 -0500352static void release_stats_buffers(struct ibmvnic_adapter *adapter)
353{
354 kfree(adapter->tx_stats_buffers);
355 kfree(adapter->rx_stats_buffers);
356}
357
358static int init_stats_buffers(struct ibmvnic_adapter *adapter)
359{
360 adapter->tx_stats_buffers =
361 kcalloc(adapter->req_tx_queues,
362 sizeof(struct ibmvnic_tx_queue_stats),
363 GFP_KERNEL);
364 if (!adapter->tx_stats_buffers)
365 return -ENOMEM;
366
367 adapter->rx_stats_buffers =
368 kcalloc(adapter->req_rx_queues,
369 sizeof(struct ibmvnic_rx_queue_stats),
370 GFP_KERNEL);
371 if (!adapter->rx_stats_buffers)
372 return -ENOMEM;
373
374 return 0;
375}
376
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400377static void release_stats_token(struct ibmvnic_adapter *adapter)
378{
379 struct device *dev = &adapter->vdev->dev;
380
381 if (!adapter->stats_token)
382 return;
383
384 dma_unmap_single(dev, adapter->stats_token,
385 sizeof(struct ibmvnic_statistics),
386 DMA_FROM_DEVICE);
387 adapter->stats_token = 0;
388}
389
390static int init_stats_token(struct ibmvnic_adapter *adapter)
391{
392 struct device *dev = &adapter->vdev->dev;
393 dma_addr_t stok;
394
395 stok = dma_map_single(dev, &adapter->stats,
396 sizeof(struct ibmvnic_statistics),
397 DMA_FROM_DEVICE);
398 if (dma_mapping_error(dev, stok)) {
399 dev_err(dev, "Couldn't map stats buffer\n");
400 return -1;
401 }
402
403 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500404 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400405 return 0;
406}
407
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400408static int reset_rx_pools(struct ibmvnic_adapter *adapter)
409{
410 struct ibmvnic_rx_pool *rx_pool;
411 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500412 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400413
414 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
415 for (i = 0; i < rx_scrqs; i++) {
416 rx_pool = &adapter->rx_pool[i];
417
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500418 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
419
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500420 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
421 if (rc)
422 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400423
424 for (j = 0; j < rx_pool->size; j++)
425 rx_pool->free_map[j] = j;
426
427 memset(rx_pool->rx_buff, 0,
428 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
429
430 atomic_set(&rx_pool->available, 0);
431 rx_pool->next_alloc = 0;
432 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500433 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400434 }
435
436 return 0;
437}
438
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400439static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600440{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400441 struct ibmvnic_rx_pool *rx_pool;
442 int rx_scrqs;
443 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600444
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400445 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600446 return;
447
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400448 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
449 for (i = 0; i < rx_scrqs; i++) {
450 rx_pool = &adapter->rx_pool[i];
451
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500452 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
453
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400454 kfree(rx_pool->free_map);
455 free_long_term_buff(adapter, &rx_pool->long_term_buff);
456
457 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400458 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400459
460 for (j = 0; j < rx_pool->size; j++) {
461 if (rx_pool->rx_buff[j].skb) {
462 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
463 rx_pool->rx_buff[i].skb = NULL;
464 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600465 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400466
467 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600468 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400469
470 kfree(adapter->rx_pool);
471 adapter->rx_pool = NULL;
472}
473
474static int init_rx_pools(struct net_device *netdev)
475{
476 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
477 struct device *dev = &adapter->vdev->dev;
478 struct ibmvnic_rx_pool *rx_pool;
479 int rxadd_subcrqs;
480 u64 *size_array;
481 int i, j;
482
483 rxadd_subcrqs =
484 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
485 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
486 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
487
488 adapter->rx_pool = kcalloc(rxadd_subcrqs,
489 sizeof(struct ibmvnic_rx_pool),
490 GFP_KERNEL);
491 if (!adapter->rx_pool) {
492 dev_err(dev, "Failed to allocate rx pools\n");
493 return -1;
494 }
495
496 for (i = 0; i < rxadd_subcrqs; i++) {
497 rx_pool = &adapter->rx_pool[i];
498
499 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500500 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400501 i, adapter->req_rx_add_entries_per_subcrq,
502 be64_to_cpu(size_array[i]));
503
504 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
505 rx_pool->index = i;
506 rx_pool->buff_size = be64_to_cpu(size_array[i]);
507 rx_pool->active = 1;
508
509 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
510 GFP_KERNEL);
511 if (!rx_pool->free_map) {
512 release_rx_pools(adapter);
513 return -1;
514 }
515
516 rx_pool->rx_buff = kcalloc(rx_pool->size,
517 sizeof(struct ibmvnic_rx_buff),
518 GFP_KERNEL);
519 if (!rx_pool->rx_buff) {
520 dev_err(dev, "Couldn't alloc rx buffers\n");
521 release_rx_pools(adapter);
522 return -1;
523 }
524
525 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
526 rx_pool->size * rx_pool->buff_size)) {
527 release_rx_pools(adapter);
528 return -1;
529 }
530
531 for (j = 0; j < rx_pool->size; ++j)
532 rx_pool->free_map[j] = j;
533
534 atomic_set(&rx_pool->available, 0);
535 rx_pool->next_alloc = 0;
536 rx_pool->next_free = 0;
537 }
538
539 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600540}
541
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400542static int reset_tx_pools(struct ibmvnic_adapter *adapter)
543{
544 struct ibmvnic_tx_pool *tx_pool;
545 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500546 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400547
548 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
549 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500550 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
551
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400552 tx_pool = &adapter->tx_pool[i];
553
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500554 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
555 if (rc)
556 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400557
Thomas Falconfdb06102017-10-17 12:36:55 -0500558 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
559 if (rc)
560 return rc;
561
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400562 memset(tx_pool->tx_buff, 0,
563 adapter->req_tx_entries_per_subcrq *
564 sizeof(struct ibmvnic_tx_buff));
565
566 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
567 tx_pool->free_map[j] = j;
568
569 tx_pool->consumer_index = 0;
570 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500571 tx_pool->tso_index = 0;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400572 }
573
574 return 0;
575}
576
Nathan Fontenotc657e322017-03-30 02:49:06 -0400577static void release_tx_pools(struct ibmvnic_adapter *adapter)
578{
579 struct ibmvnic_tx_pool *tx_pool;
580 int i, tx_scrqs;
581
582 if (!adapter->tx_pool)
583 return;
584
585 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
586 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500587 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400588 tx_pool = &adapter->tx_pool[i];
589 kfree(tx_pool->tx_buff);
590 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500591 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400592 kfree(tx_pool->free_map);
593 }
594
595 kfree(adapter->tx_pool);
596 adapter->tx_pool = NULL;
597}
598
599static int init_tx_pools(struct net_device *netdev)
600{
601 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
602 struct device *dev = &adapter->vdev->dev;
603 struct ibmvnic_tx_pool *tx_pool;
604 int tx_subcrqs;
605 int i, j;
606
607 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
608 adapter->tx_pool = kcalloc(tx_subcrqs,
609 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
610 if (!adapter->tx_pool)
611 return -1;
612
613 for (i = 0; i < tx_subcrqs; i++) {
614 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500615
616 netdev_dbg(adapter->netdev,
617 "Initializing tx_pool[%d], %lld buffs\n",
618 i, adapter->req_tx_entries_per_subcrq);
619
Nathan Fontenotc657e322017-03-30 02:49:06 -0400620 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
621 sizeof(struct ibmvnic_tx_buff),
622 GFP_KERNEL);
623 if (!tx_pool->tx_buff) {
624 dev_err(dev, "tx pool buffer allocation failed\n");
625 release_tx_pools(adapter);
626 return -1;
627 }
628
629 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
630 adapter->req_tx_entries_per_subcrq *
631 adapter->req_mtu)) {
632 release_tx_pools(adapter);
633 return -1;
634 }
635
Thomas Falconfdb06102017-10-17 12:36:55 -0500636 /* alloc TSO ltb */
637 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
638 IBMVNIC_TSO_BUFS *
639 IBMVNIC_TSO_BUF_SZ)) {
640 release_tx_pools(adapter);
641 return -1;
642 }
643
644 tx_pool->tso_index = 0;
645
Nathan Fontenotc657e322017-03-30 02:49:06 -0400646 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
647 sizeof(int), GFP_KERNEL);
648 if (!tx_pool->free_map) {
649 release_tx_pools(adapter);
650 return -1;
651 }
652
653 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
654 tx_pool->free_map[j] = j;
655
656 tx_pool->consumer_index = 0;
657 tx_pool->producer_index = 0;
658 }
659
660 return 0;
661}
662
Nathan Fontenot661a2622017-04-19 13:44:58 -0400663static void release_error_buffers(struct ibmvnic_adapter *adapter)
664{
665 struct device *dev = &adapter->vdev->dev;
666 struct ibmvnic_error_buff *error_buff, *tmp;
667 unsigned long flags;
668
669 spin_lock_irqsave(&adapter->error_list_lock, flags);
670 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
671 list_del(&error_buff->list);
672 dma_unmap_single(dev, error_buff->dma, error_buff->len,
673 DMA_FROM_DEVICE);
674 kfree(error_buff->buff);
675 kfree(error_buff);
676 }
677 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
678}
679
John Allend944c3d62017-05-26 10:30:13 -0400680static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
681{
682 int i;
683
684 if (adapter->napi_enabled)
685 return;
686
687 for (i = 0; i < adapter->req_rx_queues; i++)
688 napi_enable(&adapter->napi[i]);
689
690 adapter->napi_enabled = true;
691}
692
693static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
694{
695 int i;
696
697 if (!adapter->napi_enabled)
698 return;
699
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500700 for (i = 0; i < adapter->req_rx_queues; i++) {
701 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400702 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500703 }
John Allend944c3d62017-05-26 10:30:13 -0400704
705 adapter->napi_enabled = false;
706}
707
John Allena57a5d22017-03-17 17:13:41 -0500708static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600709{
710 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500711 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600712 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500713 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600714
John Allenbd0b6722017-03-17 17:13:40 -0500715 do {
716 if (adapter->renegotiate) {
717 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400718 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500719
720 reinit_completion(&adapter->init_done);
721 send_cap_queries(adapter);
722 if (!wait_for_completion_timeout(&adapter->init_done,
723 timeout)) {
724 dev_err(dev, "Capabilities query timeout\n");
725 return -1;
726 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500727 rc = init_sub_crqs(adapter);
728 if (rc) {
729 dev_err(dev,
730 "Initialization of SCRQ's failed\n");
731 return -1;
732 }
733 rc = init_sub_crq_irqs(adapter);
734 if (rc) {
735 dev_err(dev,
736 "Initialization of SCRQ's irqs failed\n");
737 return -1;
738 }
John Allenbd0b6722017-03-17 17:13:40 -0500739 }
740
741 reinit_completion(&adapter->init_done);
742 send_login(adapter);
743 if (!wait_for_completion_timeout(&adapter->init_done,
744 timeout)) {
745 dev_err(dev, "Login timeout\n");
746 return -1;
747 }
748 } while (adapter->renegotiate);
749
John Allena57a5d22017-03-17 17:13:41 -0500750 return 0;
751}
752
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400753static void release_resources(struct ibmvnic_adapter *adapter)
754{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400755 int i;
756
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400757 release_tx_pools(adapter);
758 release_rx_pools(adapter);
759
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400760 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500761 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400762 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400763
764 if (adapter->napi) {
765 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500766 if (&adapter->napi[i]) {
767 netdev_dbg(adapter->netdev,
768 "Releasing napi[%d]\n", i);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400769 netif_napi_del(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500770 }
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400771 }
772 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400773}
774
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400775static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
776{
777 struct net_device *netdev = adapter->netdev;
778 unsigned long timeout = msecs_to_jiffies(30000);
779 union ibmvnic_crq crq;
780 bool resend;
781 int rc;
782
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500783 netdev_dbg(netdev, "setting link state %d\n", link_state);
784
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400785 memset(&crq, 0, sizeof(crq));
786 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
787 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
788 crq.logical_link_state.link_state = link_state;
789
790 do {
791 resend = false;
792
793 reinit_completion(&adapter->init_done);
794 rc = ibmvnic_send_crq(adapter, &crq);
795 if (rc) {
796 netdev_err(netdev, "Failed to set link state\n");
797 return rc;
798 }
799
800 if (!wait_for_completion_timeout(&adapter->init_done,
801 timeout)) {
802 netdev_err(netdev, "timeout setting link state\n");
803 return -1;
804 }
805
806 if (adapter->init_done_rc == 1) {
807 /* Partuial success, delay and re-send */
808 mdelay(1000);
809 resend = true;
810 }
811 } while (resend);
812
813 return 0;
814}
815
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400816static int set_real_num_queues(struct net_device *netdev)
817{
818 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
819 int rc;
820
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500821 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
822 adapter->req_tx_queues, adapter->req_rx_queues);
823
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400824 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
825 if (rc) {
826 netdev_err(netdev, "failed to set the number of tx queues\n");
827 return rc;
828 }
829
830 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
831 if (rc)
832 netdev_err(netdev, "failed to set the number of rx queues\n");
833
834 return rc;
835}
836
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400837static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500838{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400839 struct net_device *netdev = adapter->netdev;
840 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500841
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400842 rc = set_real_num_queues(netdev);
843 if (rc)
844 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500845
John Allen3d52b592017-08-02 16:44:14 -0500846 rc = init_stats_buffers(adapter);
847 if (rc)
848 return rc;
849
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400850 rc = init_stats_token(adapter);
851 if (rc)
852 return rc;
853
Thomas Falcon032c5e82015-12-21 11:26:06 -0600854 adapter->map_id = 1;
855 adapter->napi = kcalloc(adapter->req_rx_queues,
856 sizeof(struct napi_struct), GFP_KERNEL);
857 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400858 return -ENOMEM;
859
Thomas Falcon032c5e82015-12-21 11:26:06 -0600860 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500861 netdev_dbg(netdev, "Adding napi[%d]\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600862 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
863 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600864 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600865
Thomas Falcon032c5e82015-12-21 11:26:06 -0600866 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400867
868 rc = init_rx_pools(netdev);
869 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400870 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600871
Nathan Fontenotc657e322017-03-30 02:49:06 -0400872 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400873 return rc;
874}
875
Nathan Fontenoted651a12017-05-03 14:04:38 -0400876static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400877{
878 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400879 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400880 int i, rc;
881
Nathan Fontenot90c80142017-05-03 14:04:32 -0400882 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600883 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400884 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400885
Thomas Falcon032c5e82015-12-21 11:26:06 -0600886 /* We're ready to receive frames, enable the sub-crq interrupts and
887 * set the logical link state to up
888 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400889 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500890 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400891 if (prev_state == VNIC_CLOSED)
892 enable_irq(adapter->rx_scrq[i]->irq);
893 else
894 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
895 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600896
Nathan Fontenoted651a12017-05-03 14:04:38 -0400897 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500898 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400899 if (prev_state == VNIC_CLOSED)
900 enable_irq(adapter->tx_scrq[i]->irq);
901 else
902 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
903 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600904
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400905 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400906 if (rc) {
907 for (i = 0; i < adapter->req_rx_queues; i++)
908 napi_disable(&adapter->napi[i]);
909 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400910 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400911 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600912
Nathan Fontenoted651a12017-05-03 14:04:38 -0400913 netif_tx_start_all_queues(netdev);
914
915 if (prev_state == VNIC_CLOSED) {
916 for (i = 0; i < adapter->req_rx_queues; i++)
917 napi_schedule(&adapter->napi[i]);
918 }
919
920 adapter->state = VNIC_OPEN;
921 return rc;
922}
923
924static int ibmvnic_open(struct net_device *netdev)
925{
926 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
927 int rc;
928
929 mutex_lock(&adapter->reset_lock);
930
John Allenc26eba02017-10-26 16:23:25 -0500931 if (adapter->mac_change_pending) {
932 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
933 adapter->mac_change_pending = false;
934 }
935
Nathan Fontenoted651a12017-05-03 14:04:38 -0400936 if (adapter->state != VNIC_CLOSED) {
937 rc = ibmvnic_login(netdev);
938 if (rc) {
939 mutex_unlock(&adapter->reset_lock);
940 return rc;
941 }
942
943 rc = init_resources(adapter);
944 if (rc) {
945 netdev_err(netdev, "failed to initialize resources\n");
946 release_resources(adapter);
947 mutex_unlock(&adapter->reset_lock);
948 return rc;
949 }
950 }
951
952 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -0700953 netif_carrier_on(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400954 mutex_unlock(&adapter->reset_lock);
955
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400956 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600957}
958
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400959static void clean_tx_pools(struct ibmvnic_adapter *adapter)
960{
961 struct ibmvnic_tx_pool *tx_pool;
962 u64 tx_entries;
963 int tx_scrqs;
964 int i, j;
965
966 if (!adapter->tx_pool)
967 return;
968
969 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
970 tx_entries = adapter->req_tx_entries_per_subcrq;
971
972 /* Free any remaining skbs in the tx buffer pools */
973 for (i = 0; i < tx_scrqs; i++) {
974 tx_pool = &adapter->tx_pool[i];
975 if (!tx_pool)
976 continue;
977
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500978 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400979 for (j = 0; j < tx_entries; j++) {
980 if (tx_pool->tx_buff[j].skb) {
981 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
982 tx_pool->tx_buff[j].skb = NULL;
983 }
984 }
985 }
986}
987
Nathan Fontenoted651a12017-05-03 14:04:38 -0400988static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500989{
990 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400991 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500992 int i;
993
Nathan Fontenot90c80142017-05-03 14:04:32 -0400994 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -0500995
996 /* ensure that transmissions are stopped if called by do_reset */
997 if (adapter->resetting)
998 netif_tx_disable(netdev);
999 else
1000 netif_tx_stop_all_queues(netdev);
1001
John Allend944c3d62017-05-26 10:30:13 -04001002 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001003
1004 if (adapter->tx_scrq) {
1005 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001006 if (adapter->tx_scrq[i]->irq) {
1007 netdev_dbg(adapter->netdev,
1008 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001009 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001010 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001011 }
1012
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001013 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001014 if (rc)
1015 return rc;
1016
1017 if (adapter->rx_scrq) {
1018 for (i = 0; i < adapter->req_rx_queues; i++) {
1019 int retries = 10;
1020
1021 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1022 retries--;
1023 mdelay(100);
1024
1025 if (retries == 0)
1026 break;
1027 }
1028
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001029 if (adapter->rx_scrq[i]->irq) {
1030 netdev_dbg(adapter->netdev,
1031 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001032 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001033 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001034 }
1035 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001036
Thomas Falcon10f76212017-05-26 10:30:31 -04001037 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001038 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001039 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001040}
1041
Nathan Fontenoted651a12017-05-03 14:04:38 -04001042static int ibmvnic_close(struct net_device *netdev)
1043{
1044 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1045 int rc;
1046
1047 mutex_lock(&adapter->reset_lock);
1048 rc = __ibmvnic_close(netdev);
1049 mutex_unlock(&adapter->reset_lock);
1050
1051 return rc;
1052}
1053
Thomas Falconad7775d2016-04-01 17:20:34 -05001054/**
1055 * build_hdr_data - creates L2/L3/L4 header data buffer
1056 * @hdr_field - bitfield determining needed headers
1057 * @skb - socket buffer
1058 * @hdr_len - array of header lengths
1059 * @tot_len - total length of data
1060 *
1061 * Reads hdr_field to determine which headers are needed by firmware.
1062 * Builds a buffer containing these headers. Saves individual header
1063 * lengths and total buffer length to be used to build descriptors.
1064 */
1065static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1066 int *hdr_len, u8 *hdr_data)
1067{
1068 int len = 0;
1069 u8 *hdr;
1070
1071 hdr_len[0] = sizeof(struct ethhdr);
1072
1073 if (skb->protocol == htons(ETH_P_IP)) {
1074 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1075 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1076 hdr_len[2] = tcp_hdrlen(skb);
1077 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1078 hdr_len[2] = sizeof(struct udphdr);
1079 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1080 hdr_len[1] = sizeof(struct ipv6hdr);
1081 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1082 hdr_len[2] = tcp_hdrlen(skb);
1083 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1084 hdr_len[2] = sizeof(struct udphdr);
1085 }
1086
1087 memset(hdr_data, 0, 120);
1088 if ((hdr_field >> 6) & 1) {
1089 hdr = skb_mac_header(skb);
1090 memcpy(hdr_data, hdr, hdr_len[0]);
1091 len += hdr_len[0];
1092 }
1093
1094 if ((hdr_field >> 5) & 1) {
1095 hdr = skb_network_header(skb);
1096 memcpy(hdr_data + len, hdr, hdr_len[1]);
1097 len += hdr_len[1];
1098 }
1099
1100 if ((hdr_field >> 4) & 1) {
1101 hdr = skb_transport_header(skb);
1102 memcpy(hdr_data + len, hdr, hdr_len[2]);
1103 len += hdr_len[2];
1104 }
1105 return len;
1106}
1107
1108/**
1109 * create_hdr_descs - create header and header extension descriptors
1110 * @hdr_field - bitfield determining needed headers
1111 * @data - buffer containing header data
1112 * @len - length of data buffer
1113 * @hdr_len - array of individual header lengths
1114 * @scrq_arr - descriptor array
1115 *
1116 * Creates header and, if needed, header extension descriptors and
1117 * places them in a descriptor array, scrq_arr
1118 */
1119
Thomas Falcon2de09682017-10-16 10:02:11 -05001120static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1121 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001122{
1123 union sub_crq hdr_desc;
1124 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001125 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001126 u8 *data, *cur;
1127 int tmp;
1128
1129 while (tmp_len > 0) {
1130 cur = hdr_data + len - tmp_len;
1131
1132 memset(&hdr_desc, 0, sizeof(hdr_desc));
1133 if (cur != hdr_data) {
1134 data = hdr_desc.hdr_ext.data;
1135 tmp = tmp_len > 29 ? 29 : tmp_len;
1136 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1137 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1138 hdr_desc.hdr_ext.len = tmp;
1139 } else {
1140 data = hdr_desc.hdr.data;
1141 tmp = tmp_len > 24 ? 24 : tmp_len;
1142 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1143 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1144 hdr_desc.hdr.len = tmp;
1145 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1146 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1147 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1148 hdr_desc.hdr.flag = hdr_field << 1;
1149 }
1150 memcpy(data, cur, tmp);
1151 tmp_len -= tmp;
1152 *scrq_arr = hdr_desc;
1153 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001154 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001155 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001156
1157 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001158}
1159
1160/**
1161 * build_hdr_descs_arr - build a header descriptor array
1162 * @skb - socket buffer
1163 * @num_entries - number of descriptors to be sent
1164 * @subcrq - first TX descriptor
1165 * @hdr_field - bit field determining which headers will be sent
1166 *
1167 * This function will build a TX descriptor array with applicable
1168 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1169 */
1170
1171static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1172 int *num_entries, u8 hdr_field)
1173{
1174 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001175 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001176 u8 *hdr_data = txbuff->hdr_data;
1177
1178 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1179 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001180 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001181 txbuff->indir_arr + 1);
1182}
1183
Thomas Falcon032c5e82015-12-21 11:26:06 -06001184static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1185{
1186 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1187 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001188 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001189 struct device *dev = &adapter->vdev->dev;
1190 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001191 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001192 struct ibmvnic_tx_pool *tx_pool;
1193 unsigned int tx_send_failed = 0;
1194 unsigned int tx_map_failed = 0;
1195 unsigned int tx_dropped = 0;
1196 unsigned int tx_packets = 0;
1197 unsigned int tx_bytes = 0;
1198 dma_addr_t data_dma_addr;
1199 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001200 unsigned long lpar_rc;
1201 union sub_crq tx_crq;
1202 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001203 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001204 unsigned char *dst;
1205 u64 *handle_array;
1206 int index = 0;
1207 int ret = 0;
1208
Nathan Fontenoted651a12017-05-03 14:04:38 -04001209 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001210 if (!netif_subqueue_stopped(netdev, skb))
1211 netif_stop_subqueue(netdev, queue_num);
1212 dev_kfree_skb_any(skb);
1213
Thomas Falcon032c5e82015-12-21 11:26:06 -06001214 tx_send_failed++;
1215 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001216 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001217 goto out;
1218 }
1219
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001220 tx_pool = &adapter->tx_pool[queue_num];
1221 tx_scrq = adapter->tx_scrq[queue_num];
1222 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1223 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1224 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1225
Thomas Falcon032c5e82015-12-21 11:26:06 -06001226 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001227
1228 if (skb_is_gso(skb)) {
1229 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1230 dst = tx_pool->tso_ltb.buff + offset;
1231 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1232 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1233 tx_pool->tso_index++;
1234 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1235 tx_pool->tso_index = 0;
1236 } else {
1237 offset = index * adapter->req_mtu;
1238 dst = tx_pool->long_term_buff.buff + offset;
1239 memset(dst, 0, adapter->req_mtu);
1240 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1241 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001242
Thomas Falcon15482052017-10-17 12:36:54 -05001243 if (skb_shinfo(skb)->nr_frags) {
1244 int cur, i;
1245
1246 /* Copy the head */
1247 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1248 cur = skb_headlen(skb);
1249
1250 /* Copy the frags */
1251 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1252 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1253
1254 memcpy(dst + cur,
1255 page_address(skb_frag_page(frag)) +
1256 frag->page_offset, skb_frag_size(frag));
1257 cur += skb_frag_size(frag);
1258 }
1259 } else {
1260 skb_copy_from_linear_data(skb, dst, skb->len);
1261 }
1262
Thomas Falcon032c5e82015-12-21 11:26:06 -06001263 tx_pool->consumer_index =
1264 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001265 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001266
1267 tx_buff = &tx_pool->tx_buff[index];
1268 tx_buff->skb = skb;
1269 tx_buff->data_dma[0] = data_dma_addr;
1270 tx_buff->data_len[0] = skb->len;
1271 tx_buff->index = index;
1272 tx_buff->pool_index = queue_num;
1273 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001274
1275 memset(&tx_crq, 0, sizeof(tx_crq));
1276 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1277 tx_crq.v1.type = IBMVNIC_TX_DESC;
1278 tx_crq.v1.n_crq_elem = 1;
1279 tx_crq.v1.n_sge = 1;
1280 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1281 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001282 if (skb_is_gso(skb))
1283 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1284 else
1285 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001286 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1287 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1288
1289 if (adapter->vlan_header_insertion) {
1290 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1291 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1292 }
1293
1294 if (skb->protocol == htons(ETH_P_IP)) {
1295 if (ip_hdr(skb)->version == 4)
1296 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1297 else if (ip_hdr(skb)->version == 6)
1298 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1299
1300 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1301 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1302 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1303 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1304 }
1305
Thomas Falconad7775d2016-04-01 17:20:34 -05001306 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001307 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001308 hdrs += 2;
1309 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001310 if (skb_is_gso(skb)) {
1311 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1312 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1313 hdrs += 2;
1314 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001315 /* determine if l2/3/4 headers are sent to firmware */
1316 if ((*hdrs >> 7) & 1 &&
1317 (skb->protocol == htons(ETH_P_IP) ||
1318 skb->protocol == htons(ETH_P_IPV6))) {
1319 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1320 tx_crq.v1.n_crq_elem = num_entries;
1321 tx_buff->indir_arr[0] = tx_crq;
1322 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1323 sizeof(tx_buff->indir_arr),
1324 DMA_TO_DEVICE);
1325 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001326 dev_kfree_skb_any(skb);
1327 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001328 if (!firmware_has_feature(FW_FEATURE_CMO))
1329 dev_err(dev, "tx: unable to map descriptor array\n");
1330 tx_map_failed++;
1331 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001332 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001333 goto out;
1334 }
John Allen498cd8e2016-04-06 11:49:55 -05001335 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001336 (u64)tx_buff->indir_dma,
1337 (u64)num_entries);
1338 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001339 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1340 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001341 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001342 if (lpar_rc != H_SUCCESS) {
1343 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1344
1345 if (tx_pool->consumer_index == 0)
1346 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001347 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001348 else
1349 tx_pool->consumer_index--;
1350
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001351 dev_kfree_skb_any(skb);
1352 tx_buff->skb = NULL;
1353
Thomas Falconb8c80b82017-05-26 10:30:42 -04001354 if (lpar_rc == H_CLOSED) {
1355 /* Disable TX and report carrier off if queue is closed.
1356 * Firmware guarantees that a signal will be sent to the
1357 * driver, triggering a reset or some other action.
1358 */
1359 netif_tx_stop_all_queues(netdev);
1360 netif_carrier_off(netdev);
1361 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001362
Thomas Falcon032c5e82015-12-21 11:26:06 -06001363 tx_send_failed++;
1364 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001365 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001366 goto out;
1367 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001368
Brian King58c8c0c2017-04-19 13:44:47 -04001369 if (atomic_inc_return(&tx_scrq->used)
1370 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001371 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1372 netif_stop_subqueue(netdev, queue_num);
1373 }
1374
Thomas Falcon032c5e82015-12-21 11:26:06 -06001375 tx_packets++;
1376 tx_bytes += skb->len;
1377 txq->trans_start = jiffies;
1378 ret = NETDEV_TX_OK;
1379
1380out:
1381 netdev->stats.tx_dropped += tx_dropped;
1382 netdev->stats.tx_bytes += tx_bytes;
1383 netdev->stats.tx_packets += tx_packets;
1384 adapter->tx_send_failed += tx_send_failed;
1385 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001386 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1387 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1388 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001389
1390 return ret;
1391}
1392
1393static void ibmvnic_set_multi(struct net_device *netdev)
1394{
1395 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1396 struct netdev_hw_addr *ha;
1397 union ibmvnic_crq crq;
1398
1399 memset(&crq, 0, sizeof(crq));
1400 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1401 crq.request_capability.cmd = REQUEST_CAPABILITY;
1402
1403 if (netdev->flags & IFF_PROMISC) {
1404 if (!adapter->promisc_supported)
1405 return;
1406 } else {
1407 if (netdev->flags & IFF_ALLMULTI) {
1408 /* Accept all multicast */
1409 memset(&crq, 0, sizeof(crq));
1410 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1411 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1412 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1413 ibmvnic_send_crq(adapter, &crq);
1414 } else if (netdev_mc_empty(netdev)) {
1415 /* Reject all multicast */
1416 memset(&crq, 0, sizeof(crq));
1417 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1418 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1419 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1420 ibmvnic_send_crq(adapter, &crq);
1421 } else {
1422 /* Accept one or more multicast(s) */
1423 netdev_for_each_mc_addr(ha, netdev) {
1424 memset(&crq, 0, sizeof(crq));
1425 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1426 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1427 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1428 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1429 ha->addr);
1430 ibmvnic_send_crq(adapter, &crq);
1431 }
1432 }
1433 }
1434}
1435
John Allenc26eba02017-10-26 16:23:25 -05001436static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001437{
1438 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1439 struct sockaddr *addr = p;
1440 union ibmvnic_crq crq;
1441
1442 if (!is_valid_ether_addr(addr->sa_data))
1443 return -EADDRNOTAVAIL;
1444
1445 memset(&crq, 0, sizeof(crq));
1446 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1447 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1448 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1449 ibmvnic_send_crq(adapter, &crq);
1450 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1451 return 0;
1452}
1453
John Allenc26eba02017-10-26 16:23:25 -05001454static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1455{
1456 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1457 struct sockaddr *addr = p;
1458
1459 if (adapter->state != VNIC_OPEN) {
1460 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1461 adapter->mac_change_pending = true;
1462 return 0;
1463 }
1464
1465 __ibmvnic_set_mac(netdev, addr);
1466
1467 return 0;
1468}
1469
Nathan Fontenoted651a12017-05-03 14:04:38 -04001470/**
1471 * do_reset returns zero if we are able to keep processing reset events, or
1472 * non-zero if we hit a fatal error and must halt.
1473 */
1474static int do_reset(struct ibmvnic_adapter *adapter,
1475 struct ibmvnic_rwi *rwi, u32 reset_state)
1476{
1477 struct net_device *netdev = adapter->netdev;
1478 int i, rc;
1479
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001480 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1481 rwi->reset_reason);
1482
Nathan Fontenoted651a12017-05-03 14:04:38 -04001483 netif_carrier_off(netdev);
1484 adapter->reset_reason = rwi->reset_reason;
1485
1486 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1487 rc = ibmvnic_reenable_crq_queue(adapter);
1488 if (rc)
1489 return 0;
1490 }
1491
1492 rc = __ibmvnic_close(netdev);
1493 if (rc)
1494 return rc;
1495
John Allenc26eba02017-10-26 16:23:25 -05001496 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1497 adapter->wait_for_reset) {
1498 release_resources(adapter);
1499 release_sub_crqs(adapter);
1500 release_crq_queue(adapter);
1501 }
1502
John Allen8cb31cf2017-05-26 10:30:37 -04001503 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1504 /* remove the closed state so when we call open it appears
1505 * we are coming from the probed state.
1506 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001507 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001508
John Allen8cb31cf2017-05-26 10:30:37 -04001509 rc = ibmvnic_init(adapter);
1510 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001511 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001512
1513 /* If the adapter was in PROBE state prior to the reset,
1514 * exit here.
1515 */
1516 if (reset_state == VNIC_PROBED)
1517 return 0;
1518
1519 rc = ibmvnic_login(netdev);
1520 if (rc) {
1521 adapter->state = VNIC_PROBED;
1522 return 0;
1523 }
1524
John Allenc26eba02017-10-26 16:23:25 -05001525 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1526 adapter->wait_for_reset) {
1527 rc = init_resources(adapter);
1528 if (rc)
1529 return rc;
1530 } else {
1531 rc = reset_tx_pools(adapter);
1532 if (rc)
1533 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001534
John Allenc26eba02017-10-26 16:23:25 -05001535 rc = reset_rx_pools(adapter);
1536 if (rc)
1537 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001538
John Allenc26eba02017-10-26 16:23:25 -05001539 if (reset_state == VNIC_CLOSED)
1540 return 0;
1541 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001542 }
1543
Nathan Fontenoted651a12017-05-03 14:04:38 -04001544 rc = __ibmvnic_open(netdev);
1545 if (rc) {
1546 if (list_empty(&adapter->rwi_list))
1547 adapter->state = VNIC_CLOSED;
1548 else
1549 adapter->state = reset_state;
1550
1551 return 0;
1552 }
1553
1554 netif_carrier_on(netdev);
1555
1556 /* kick napi */
1557 for (i = 0; i < adapter->req_rx_queues; i++)
1558 napi_schedule(&adapter->napi[i]);
1559
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001560 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1561 netdev_notify_peers(netdev);
1562
Nathan Fontenoted651a12017-05-03 14:04:38 -04001563 return 0;
1564}
1565
1566static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1567{
1568 struct ibmvnic_rwi *rwi;
1569
1570 mutex_lock(&adapter->rwi_lock);
1571
1572 if (!list_empty(&adapter->rwi_list)) {
1573 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1574 list);
1575 list_del(&rwi->list);
1576 } else {
1577 rwi = NULL;
1578 }
1579
1580 mutex_unlock(&adapter->rwi_lock);
1581 return rwi;
1582}
1583
1584static void free_all_rwi(struct ibmvnic_adapter *adapter)
1585{
1586 struct ibmvnic_rwi *rwi;
1587
1588 rwi = get_next_rwi(adapter);
1589 while (rwi) {
1590 kfree(rwi);
1591 rwi = get_next_rwi(adapter);
1592 }
1593}
1594
1595static void __ibmvnic_reset(struct work_struct *work)
1596{
1597 struct ibmvnic_rwi *rwi;
1598 struct ibmvnic_adapter *adapter;
1599 struct net_device *netdev;
1600 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001601 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001602
1603 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1604 netdev = adapter->netdev;
1605
1606 mutex_lock(&adapter->reset_lock);
1607 adapter->resetting = true;
1608 reset_state = adapter->state;
1609
1610 rwi = get_next_rwi(adapter);
1611 while (rwi) {
1612 rc = do_reset(adapter, rwi, reset_state);
1613 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001614 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001615 break;
1616
1617 rwi = get_next_rwi(adapter);
1618 }
1619
John Allenc26eba02017-10-26 16:23:25 -05001620 if (adapter->wait_for_reset) {
1621 adapter->wait_for_reset = false;
1622 adapter->reset_done_rc = rc;
1623 complete(&adapter->reset_done);
1624 }
1625
Nathan Fontenoted651a12017-05-03 14:04:38 -04001626 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001627 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001628 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001629 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001630 return;
1631 }
1632
1633 adapter->resetting = false;
1634 mutex_unlock(&adapter->reset_lock);
1635}
1636
1637static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1638 enum ibmvnic_reset_reason reason)
1639{
1640 struct ibmvnic_rwi *rwi, *tmp;
1641 struct net_device *netdev = adapter->netdev;
1642 struct list_head *entry;
1643
1644 if (adapter->state == VNIC_REMOVING ||
1645 adapter->state == VNIC_REMOVED) {
1646 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1647 return;
1648 }
1649
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001650 if (adapter->state == VNIC_PROBING) {
1651 netdev_warn(netdev, "Adapter reset during probe\n");
1652 adapter->init_done_rc = EAGAIN;
1653 return;
1654 }
1655
Nathan Fontenoted651a12017-05-03 14:04:38 -04001656 mutex_lock(&adapter->rwi_lock);
1657
1658 list_for_each(entry, &adapter->rwi_list) {
1659 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1660 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001661 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001662 mutex_unlock(&adapter->rwi_lock);
1663 return;
1664 }
1665 }
1666
1667 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1668 if (!rwi) {
1669 mutex_unlock(&adapter->rwi_lock);
1670 ibmvnic_close(netdev);
1671 return;
1672 }
1673
1674 rwi->reset_reason = reason;
1675 list_add_tail(&rwi->list, &adapter->rwi_list);
1676 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001677
1678 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001679 schedule_work(&adapter->ibmvnic_reset);
1680}
1681
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682static void ibmvnic_tx_timeout(struct net_device *dev)
1683{
1684 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001685
Nathan Fontenoted651a12017-05-03 14:04:38 -04001686 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001687}
1688
1689static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1690 struct ibmvnic_rx_buff *rx_buff)
1691{
1692 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1693
1694 rx_buff->skb = NULL;
1695
1696 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1697 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1698
1699 atomic_dec(&pool->available);
1700}
1701
1702static int ibmvnic_poll(struct napi_struct *napi, int budget)
1703{
1704 struct net_device *netdev = napi->dev;
1705 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1706 int scrq_num = (int)(napi - adapter->napi);
1707 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001708
Thomas Falcon032c5e82015-12-21 11:26:06 -06001709restart_poll:
1710 while (frames_processed < budget) {
1711 struct sk_buff *skb;
1712 struct ibmvnic_rx_buff *rx_buff;
1713 union sub_crq *next;
1714 u32 length;
1715 u16 offset;
1716 u8 flags = 0;
1717
Thomas Falcon21ecba62017-06-14 23:50:09 -05001718 if (unlikely(adapter->resetting)) {
1719 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1720 napi_complete_done(napi, frames_processed);
1721 return frames_processed;
1722 }
1723
Thomas Falcon032c5e82015-12-21 11:26:06 -06001724 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1725 break;
1726 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1727 rx_buff =
1728 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1729 rx_comp.correlator);
1730 /* do error checking */
1731 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001732 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1733 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001734 /* free the entry */
1735 next->rx_comp.first = 0;
1736 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001737 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001738 }
1739
1740 length = be32_to_cpu(next->rx_comp.len);
1741 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1742 flags = next->rx_comp.flags;
1743 skb = rx_buff->skb;
1744 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1745 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001746
1747 /* VLAN Header has been stripped by the system firmware and
1748 * needs to be inserted by the driver
1749 */
1750 if (adapter->rx_vlan_header_insertion &&
1751 (flags & IBMVNIC_VLAN_STRIPPED))
1752 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1753 ntohs(next->rx_comp.vlan_tci));
1754
Thomas Falcon032c5e82015-12-21 11:26:06 -06001755 /* free the entry */
1756 next->rx_comp.first = 0;
1757 remove_buff_from_pool(adapter, rx_buff);
1758
1759 skb_put(skb, length);
1760 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001761 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001762
1763 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1764 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1765 skb->ip_summed = CHECKSUM_UNNECESSARY;
1766 }
1767
1768 length = skb->len;
1769 napi_gro_receive(napi, skb); /* send it up */
1770 netdev->stats.rx_packets++;
1771 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001772 adapter->rx_stats_buffers[scrq_num].packets++;
1773 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774 frames_processed++;
1775 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001776
1777 if (adapter->state != VNIC_CLOSING)
1778 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001779
1780 if (frames_processed < budget) {
1781 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001782 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001783 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1784 napi_reschedule(napi)) {
1785 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1786 goto restart_poll;
1787 }
1788 }
1789 return frames_processed;
1790}
1791
1792#ifdef CONFIG_NET_POLL_CONTROLLER
1793static void ibmvnic_netpoll_controller(struct net_device *dev)
1794{
1795 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1796 int i;
1797
1798 replenish_pools(netdev_priv(dev));
1799 for (i = 0; i < adapter->req_rx_queues; i++)
1800 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1801 adapter->rx_scrq[i]);
1802}
1803#endif
1804
John Allenc26eba02017-10-26 16:23:25 -05001805static int wait_for_reset(struct ibmvnic_adapter *adapter)
1806{
1807 adapter->fallback.mtu = adapter->req_mtu;
1808 adapter->fallback.rx_queues = adapter->req_rx_queues;
1809 adapter->fallback.tx_queues = adapter->req_tx_queues;
1810 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1811 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1812
1813 init_completion(&adapter->reset_done);
1814 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1815 adapter->wait_for_reset = true;
1816 wait_for_completion(&adapter->reset_done);
1817
1818 if (adapter->reset_done_rc) {
1819 adapter->desired.mtu = adapter->fallback.mtu;
1820 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1821 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1822 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1823 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1824
1825 init_completion(&adapter->reset_done);
1826 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1827 wait_for_completion(&adapter->reset_done);
1828 }
1829 adapter->wait_for_reset = false;
1830
1831 return adapter->reset_done_rc;
1832}
1833
John Allen3a807b72017-06-06 16:55:52 -05001834static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1835{
John Allenc26eba02017-10-26 16:23:25 -05001836 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1837
1838 adapter->desired.mtu = new_mtu + ETH_HLEN;
1839
1840 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05001841}
1842
Thomas Falcon032c5e82015-12-21 11:26:06 -06001843static const struct net_device_ops ibmvnic_netdev_ops = {
1844 .ndo_open = ibmvnic_open,
1845 .ndo_stop = ibmvnic_close,
1846 .ndo_start_xmit = ibmvnic_xmit,
1847 .ndo_set_rx_mode = ibmvnic_set_multi,
1848 .ndo_set_mac_address = ibmvnic_set_mac,
1849 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001850 .ndo_tx_timeout = ibmvnic_tx_timeout,
1851#ifdef CONFIG_NET_POLL_CONTROLLER
1852 .ndo_poll_controller = ibmvnic_netpoll_controller,
1853#endif
John Allen3a807b72017-06-06 16:55:52 -05001854 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001855};
1856
1857/* ethtool functions */
1858
Philippe Reynes8a433792017-01-07 22:37:29 +01001859static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1860 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001861{
Philippe Reynes8a433792017-01-07 22:37:29 +01001862 u32 supported, advertising;
1863
1864 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001865 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001866 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001868 cmd->base.speed = SPEED_1000;
1869 cmd->base.duplex = DUPLEX_FULL;
1870 cmd->base.port = PORT_FIBRE;
1871 cmd->base.phy_address = 0;
1872 cmd->base.autoneg = AUTONEG_ENABLE;
1873
1874 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1875 supported);
1876 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1877 advertising);
1878
Thomas Falcon032c5e82015-12-21 11:26:06 -06001879 return 0;
1880}
1881
1882static void ibmvnic_get_drvinfo(struct net_device *dev,
1883 struct ethtool_drvinfo *info)
1884{
1885 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1886 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1887}
1888
1889static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1890{
1891 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1892
1893 return adapter->msg_enable;
1894}
1895
1896static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1897{
1898 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1899
1900 adapter->msg_enable = data;
1901}
1902
1903static u32 ibmvnic_get_link(struct net_device *netdev)
1904{
1905 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1906
1907 /* Don't need to send a query because we request a logical link up at
1908 * init and then we wait for link state indications
1909 */
1910 return adapter->logical_link_state;
1911}
1912
1913static void ibmvnic_get_ringparam(struct net_device *netdev,
1914 struct ethtool_ringparam *ring)
1915{
John Allenbc131b32017-08-02 16:46:30 -05001916 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1917
1918 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1919 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001920 ring->rx_mini_max_pending = 0;
1921 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05001922 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1923 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001924 ring->rx_mini_pending = 0;
1925 ring->rx_jumbo_pending = 0;
1926}
1927
John Allenc26eba02017-10-26 16:23:25 -05001928static int ibmvnic_set_ringparam(struct net_device *netdev,
1929 struct ethtool_ringparam *ring)
1930{
1931 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1932
1933 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
1934 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
1935 netdev_err(netdev, "Invalid request.\n");
1936 netdev_err(netdev, "Max tx buffers = %llu\n",
1937 adapter->max_rx_add_entries_per_subcrq);
1938 netdev_err(netdev, "Max rx buffers = %llu\n",
1939 adapter->max_tx_entries_per_subcrq);
1940 return -EINVAL;
1941 }
1942
1943 adapter->desired.rx_entries = ring->rx_pending;
1944 adapter->desired.tx_entries = ring->tx_pending;
1945
1946 return wait_for_reset(adapter);
1947}
1948
John Allenc2dbeb62017-08-02 16:47:17 -05001949static void ibmvnic_get_channels(struct net_device *netdev,
1950 struct ethtool_channels *channels)
1951{
1952 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1953
1954 channels->max_rx = adapter->max_rx_queues;
1955 channels->max_tx = adapter->max_tx_queues;
1956 channels->max_other = 0;
1957 channels->max_combined = 0;
1958 channels->rx_count = adapter->req_rx_queues;
1959 channels->tx_count = adapter->req_tx_queues;
1960 channels->other_count = 0;
1961 channels->combined_count = 0;
1962}
1963
John Allenc26eba02017-10-26 16:23:25 -05001964static int ibmvnic_set_channels(struct net_device *netdev,
1965 struct ethtool_channels *channels)
1966{
1967 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1968
1969 adapter->desired.rx_queues = channels->rx_count;
1970 adapter->desired.tx_queues = channels->tx_count;
1971
1972 return wait_for_reset(adapter);
1973}
1974
Thomas Falcon032c5e82015-12-21 11:26:06 -06001975static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1976{
John Allen3d52b592017-08-02 16:44:14 -05001977 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001978 int i;
1979
1980 if (stringset != ETH_SS_STATS)
1981 return;
1982
1983 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1984 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001985
1986 for (i = 0; i < adapter->req_tx_queues; i++) {
1987 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1988 data += ETH_GSTRING_LEN;
1989
1990 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1991 data += ETH_GSTRING_LEN;
1992
1993 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1994 data += ETH_GSTRING_LEN;
1995 }
1996
1997 for (i = 0; i < adapter->req_rx_queues; i++) {
1998 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1999 data += ETH_GSTRING_LEN;
2000
2001 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2002 data += ETH_GSTRING_LEN;
2003
2004 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2005 data += ETH_GSTRING_LEN;
2006 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002007}
2008
2009static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2010{
John Allen3d52b592017-08-02 16:44:14 -05002011 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2012
Thomas Falcon032c5e82015-12-21 11:26:06 -06002013 switch (sset) {
2014 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002015 return ARRAY_SIZE(ibmvnic_stats) +
2016 adapter->req_tx_queues * NUM_TX_STATS +
2017 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002018 default:
2019 return -EOPNOTSUPP;
2020 }
2021}
2022
2023static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2024 struct ethtool_stats *stats, u64 *data)
2025{
2026 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2027 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002028 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002029
2030 memset(&crq, 0, sizeof(crq));
2031 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2032 crq.request_statistics.cmd = REQUEST_STATISTICS;
2033 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2034 crq.request_statistics.len =
2035 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002036
2037 /* Wait for data to be written */
2038 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002039 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002040 wait_for_completion(&adapter->stats_done);
2041
2042 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002043 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2044 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002045
2046 for (j = 0; j < adapter->req_tx_queues; j++) {
2047 data[i] = adapter->tx_stats_buffers[j].packets;
2048 i++;
2049 data[i] = adapter->tx_stats_buffers[j].bytes;
2050 i++;
2051 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2052 i++;
2053 }
2054
2055 for (j = 0; j < adapter->req_rx_queues; j++) {
2056 data[i] = adapter->rx_stats_buffers[j].packets;
2057 i++;
2058 data[i] = adapter->rx_stats_buffers[j].bytes;
2059 i++;
2060 data[i] = adapter->rx_stats_buffers[j].interrupts;
2061 i++;
2062 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002063}
2064
2065static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002066 .get_drvinfo = ibmvnic_get_drvinfo,
2067 .get_msglevel = ibmvnic_get_msglevel,
2068 .set_msglevel = ibmvnic_set_msglevel,
2069 .get_link = ibmvnic_get_link,
2070 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002071 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002072 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002073 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002074 .get_strings = ibmvnic_get_strings,
2075 .get_sset_count = ibmvnic_get_sset_count,
2076 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002077 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002078};
2079
2080/* Routines for managing CRQs/sCRQs */
2081
Nathan Fontenot57a49432017-05-26 10:31:12 -04002082static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2083 struct ibmvnic_sub_crq_queue *scrq)
2084{
2085 int rc;
2086
2087 if (scrq->irq) {
2088 free_irq(scrq->irq, scrq);
2089 irq_dispose_mapping(scrq->irq);
2090 scrq->irq = 0;
2091 }
2092
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002093 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002094 scrq->cur = 0;
2095
2096 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2097 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2098 return rc;
2099}
2100
2101static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2102{
2103 int i, rc;
2104
2105 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002106 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002107 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2108 if (rc)
2109 return rc;
2110 }
2111
2112 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002113 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002114 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2115 if (rc)
2116 return rc;
2117 }
2118
Nathan Fontenot57a49432017-05-26 10:31:12 -04002119 return rc;
2120}
2121
Thomas Falcon032c5e82015-12-21 11:26:06 -06002122static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2123 struct ibmvnic_sub_crq_queue *scrq)
2124{
2125 struct device *dev = &adapter->vdev->dev;
2126 long rc;
2127
2128 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2129
2130 /* Close the sub-crqs */
2131 do {
2132 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2133 adapter->vdev->unit_address,
2134 scrq->crq_num);
2135 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2136
Thomas Falconffa73852017-04-19 13:44:29 -04002137 if (rc) {
2138 netdev_err(adapter->netdev,
2139 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2140 scrq->crq_num, rc);
2141 }
2142
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2144 DMA_BIDIRECTIONAL);
2145 free_pages((unsigned long)scrq->msgs, 2);
2146 kfree(scrq);
2147}
2148
2149static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2150 *adapter)
2151{
2152 struct device *dev = &adapter->vdev->dev;
2153 struct ibmvnic_sub_crq_queue *scrq;
2154 int rc;
2155
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002156 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002157 if (!scrq)
2158 return NULL;
2159
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002160 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002161 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002162 if (!scrq->msgs) {
2163 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2164 goto zero_page_failed;
2165 }
2166
2167 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2168 DMA_BIDIRECTIONAL);
2169 if (dma_mapping_error(dev, scrq->msg_token)) {
2170 dev_warn(dev, "Couldn't map crq queue messages page\n");
2171 goto map_failed;
2172 }
2173
2174 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2175 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2176
2177 if (rc == H_RESOURCE)
2178 rc = ibmvnic_reset_crq(adapter);
2179
2180 if (rc == H_CLOSED) {
2181 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2182 } else if (rc) {
2183 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2184 goto reg_failed;
2185 }
2186
Thomas Falcon032c5e82015-12-21 11:26:06 -06002187 scrq->adapter = adapter;
2188 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002189 spin_lock_init(&scrq->lock);
2190
2191 netdev_dbg(adapter->netdev,
2192 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2193 scrq->crq_num, scrq->hw_irq, scrq->irq);
2194
2195 return scrq;
2196
Thomas Falcon032c5e82015-12-21 11:26:06 -06002197reg_failed:
2198 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2199 DMA_BIDIRECTIONAL);
2200map_failed:
2201 free_pages((unsigned long)scrq->msgs, 2);
2202zero_page_failed:
2203 kfree(scrq);
2204
2205 return NULL;
2206}
2207
2208static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2209{
2210 int i;
2211
2212 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002213 for (i = 0; i < adapter->req_tx_queues; i++) {
2214 if (!adapter->tx_scrq[i])
2215 continue;
2216
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002217 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2218 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002219 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002220 free_irq(adapter->tx_scrq[i]->irq,
2221 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002222 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002223 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002224 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002225
2226 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2227 }
2228
Nathan Fontenot9501df32017-03-15 23:38:07 -04002229 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002230 adapter->tx_scrq = NULL;
2231 }
2232
2233 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002234 for (i = 0; i < adapter->req_rx_queues; i++) {
2235 if (!adapter->rx_scrq[i])
2236 continue;
2237
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002238 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2239 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002240 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002241 free_irq(adapter->rx_scrq[i]->irq,
2242 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002243 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002244 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002245 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002246
2247 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2248 }
2249
Nathan Fontenot9501df32017-03-15 23:38:07 -04002250 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002251 adapter->rx_scrq = NULL;
2252 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002253}
2254
2255static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2256 struct ibmvnic_sub_crq_queue *scrq)
2257{
2258 struct device *dev = &adapter->vdev->dev;
2259 unsigned long rc;
2260
2261 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2262 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2263 if (rc)
2264 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2265 scrq->hw_irq, rc);
2266 return rc;
2267}
2268
2269static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2270 struct ibmvnic_sub_crq_queue *scrq)
2271{
2272 struct device *dev = &adapter->vdev->dev;
2273 unsigned long rc;
2274
2275 if (scrq->hw_irq > 0x100000000ULL) {
2276 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2277 return 1;
2278 }
2279
2280 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2281 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2282 if (rc)
2283 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2284 scrq->hw_irq, rc);
2285 return rc;
2286}
2287
2288static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2289 struct ibmvnic_sub_crq_queue *scrq)
2290{
2291 struct device *dev = &adapter->vdev->dev;
2292 struct ibmvnic_tx_buff *txbuff;
2293 union sub_crq *next;
2294 int index;
2295 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002296 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002297
2298restart_loop:
2299 while (pending_scrq(adapter, scrq)) {
2300 unsigned int pool = scrq->pool_index;
2301
2302 next = ibmvnic_next_scrq(adapter, scrq);
2303 for (i = 0; i < next->tx_comp.num_comps; i++) {
2304 if (next->tx_comp.rcs[i]) {
2305 dev_err(dev, "tx error %x\n",
2306 next->tx_comp.rcs[i]);
2307 continue;
2308 }
2309 index = be32_to_cpu(next->tx_comp.correlators[i]);
2310 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2311
2312 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2313 if (!txbuff->data_dma[j])
2314 continue;
2315
2316 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002317 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002318 /* if sub_crq was sent indirectly */
2319 first = txbuff->indir_arr[0].generic.first;
2320 if (first == IBMVNIC_CRQ_CMD) {
2321 dma_unmap_single(dev, txbuff->indir_dma,
2322 sizeof(txbuff->indir_arr),
2323 DMA_TO_DEVICE);
2324 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002325
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002326 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002327 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002328 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002329 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002330
2331 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2332 producer_index] = index;
2333 adapter->tx_pool[pool].producer_index =
2334 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002335 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002336 }
2337 /* remove tx_comp scrq*/
2338 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002339
2340 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2341 (adapter->req_tx_entries_per_subcrq / 2) &&
2342 __netif_subqueue_stopped(adapter->netdev,
2343 scrq->pool_index)) {
2344 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2345 netdev_info(adapter->netdev, "Started queue %d\n",
2346 scrq->pool_index);
2347 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002348 }
2349
2350 enable_scrq_irq(adapter, scrq);
2351
2352 if (pending_scrq(adapter, scrq)) {
2353 disable_scrq_irq(adapter, scrq);
2354 goto restart_loop;
2355 }
2356
2357 return 0;
2358}
2359
2360static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2361{
2362 struct ibmvnic_sub_crq_queue *scrq = instance;
2363 struct ibmvnic_adapter *adapter = scrq->adapter;
2364
2365 disable_scrq_irq(adapter, scrq);
2366 ibmvnic_complete_tx(adapter, scrq);
2367
2368 return IRQ_HANDLED;
2369}
2370
2371static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2372{
2373 struct ibmvnic_sub_crq_queue *scrq = instance;
2374 struct ibmvnic_adapter *adapter = scrq->adapter;
2375
John Allen3d52b592017-08-02 16:44:14 -05002376 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2377
Thomas Falcon032c5e82015-12-21 11:26:06 -06002378 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2379 disable_scrq_irq(adapter, scrq);
2380 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2381 }
2382
2383 return IRQ_HANDLED;
2384}
2385
Thomas Falconea22d512016-07-06 15:35:17 -05002386static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2387{
2388 struct device *dev = &adapter->vdev->dev;
2389 struct ibmvnic_sub_crq_queue *scrq;
2390 int i = 0, j = 0;
2391 int rc = 0;
2392
2393 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002394 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2395 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002396 scrq = adapter->tx_scrq[i];
2397 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2398
Michael Ellerman99c17902016-09-10 19:59:05 +10002399 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002400 rc = -EINVAL;
2401 dev_err(dev, "Error mapping irq\n");
2402 goto req_tx_irq_failed;
2403 }
2404
2405 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2406 0, "ibmvnic_tx", scrq);
2407
2408 if (rc) {
2409 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2410 scrq->irq, rc);
2411 irq_dispose_mapping(scrq->irq);
2412 goto req_rx_irq_failed;
2413 }
2414 }
2415
2416 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002417 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2418 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002419 scrq = adapter->rx_scrq[i];
2420 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002421 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002422 rc = -EINVAL;
2423 dev_err(dev, "Error mapping irq\n");
2424 goto req_rx_irq_failed;
2425 }
2426 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2427 0, "ibmvnic_rx", scrq);
2428 if (rc) {
2429 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2430 scrq->irq, rc);
2431 irq_dispose_mapping(scrq->irq);
2432 goto req_rx_irq_failed;
2433 }
2434 }
2435 return rc;
2436
2437req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002438 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002439 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2440 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002441 }
Thomas Falconea22d512016-07-06 15:35:17 -05002442 i = adapter->req_tx_queues;
2443req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002444 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002445 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2446 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002447 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002448 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002449 return rc;
2450}
2451
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002452static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002453{
2454 struct device *dev = &adapter->vdev->dev;
2455 struct ibmvnic_sub_crq_queue **allqueues;
2456 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002457 int total_queues;
2458 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002459 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002460
Thomas Falcon032c5e82015-12-21 11:26:06 -06002461 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2462
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002463 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002465 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002466
2467 for (i = 0; i < total_queues; i++) {
2468 allqueues[i] = init_sub_crq_queue(adapter);
2469 if (!allqueues[i]) {
2470 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2471 break;
2472 }
2473 registered_queues++;
2474 }
2475
2476 /* Make sure we were able to register the minimum number of queues */
2477 if (registered_queues <
2478 adapter->min_tx_queues + adapter->min_rx_queues) {
2479 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2480 goto tx_failed;
2481 }
2482
2483 /* Distribute the failed allocated queues*/
2484 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2485 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2486 switch (i % 3) {
2487 case 0:
2488 if (adapter->req_rx_queues > adapter->min_rx_queues)
2489 adapter->req_rx_queues--;
2490 else
2491 more++;
2492 break;
2493 case 1:
2494 if (adapter->req_tx_queues > adapter->min_tx_queues)
2495 adapter->req_tx_queues--;
2496 else
2497 more++;
2498 break;
2499 }
2500 }
2501
2502 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002503 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002504 if (!adapter->tx_scrq)
2505 goto tx_failed;
2506
2507 for (i = 0; i < adapter->req_tx_queues; i++) {
2508 adapter->tx_scrq[i] = allqueues[i];
2509 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002510 }
2511
2512 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002513 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002514 if (!adapter->rx_scrq)
2515 goto rx_failed;
2516
2517 for (i = 0; i < adapter->req_rx_queues; i++) {
2518 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2519 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520 }
2521
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002522 kfree(allqueues);
2523 return 0;
2524
2525rx_failed:
2526 kfree(adapter->tx_scrq);
2527 adapter->tx_scrq = NULL;
2528tx_failed:
2529 for (i = 0; i < registered_queues; i++)
2530 release_sub_crq_queue(adapter, allqueues[i]);
2531 kfree(allqueues);
2532 return -1;
2533}
2534
2535static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2536{
2537 struct device *dev = &adapter->vdev->dev;
2538 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002539 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002540
2541 if (!retry) {
2542 /* Sub-CRQ entries are 32 byte long */
2543 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2544
2545 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2546 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2547 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2548 return;
2549 }
2550
John Allenc26eba02017-10-26 16:23:25 -05002551 if (adapter->desired.mtu)
2552 adapter->req_mtu = adapter->desired.mtu;
2553 else
2554 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002555
John Allenc26eba02017-10-26 16:23:25 -05002556 if (!adapter->desired.tx_entries)
2557 adapter->desired.tx_entries =
2558 adapter->max_tx_entries_per_subcrq;
2559 if (!adapter->desired.rx_entries)
2560 adapter->desired.rx_entries =
2561 adapter->max_rx_add_entries_per_subcrq;
2562
2563 max_entries = IBMVNIC_MAX_LTB_SIZE /
2564 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2565
2566 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2567 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2568 adapter->desired.tx_entries = max_entries;
2569 }
2570
2571 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2572 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2573 adapter->desired.rx_entries = max_entries;
2574 }
2575
2576 if (adapter->desired.tx_entries)
2577 adapter->req_tx_entries_per_subcrq =
2578 adapter->desired.tx_entries;
2579 else
2580 adapter->req_tx_entries_per_subcrq =
2581 adapter->max_tx_entries_per_subcrq;
2582
2583 if (adapter->desired.rx_entries)
2584 adapter->req_rx_add_entries_per_subcrq =
2585 adapter->desired.rx_entries;
2586 else
2587 adapter->req_rx_add_entries_per_subcrq =
2588 adapter->max_rx_add_entries_per_subcrq;
2589
2590 if (adapter->desired.tx_queues)
2591 adapter->req_tx_queues =
2592 adapter->desired.tx_queues;
2593 else
2594 adapter->req_tx_queues =
2595 adapter->opt_tx_comp_sub_queues;
2596
2597 if (adapter->desired.rx_queues)
2598 adapter->req_rx_queues =
2599 adapter->desired.rx_queues;
2600 else
2601 adapter->req_rx_queues =
2602 adapter->opt_rx_comp_queues;
2603
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002604 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002605 }
2606
Thomas Falcon032c5e82015-12-21 11:26:06 -06002607 memset(&crq, 0, sizeof(crq));
2608 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2609 crq.request_capability.cmd = REQUEST_CAPABILITY;
2610
2611 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002612 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002613 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614 ibmvnic_send_crq(adapter, &crq);
2615
2616 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002617 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002618 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002619 ibmvnic_send_crq(adapter, &crq);
2620
2621 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002622 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002623 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002624 ibmvnic_send_crq(adapter, &crq);
2625
2626 crq.request_capability.capability =
2627 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2628 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002629 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002630 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002631 ibmvnic_send_crq(adapter, &crq);
2632
2633 crq.request_capability.capability =
2634 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2635 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002636 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002637 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002638 ibmvnic_send_crq(adapter, &crq);
2639
2640 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002641 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002642 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002643 ibmvnic_send_crq(adapter, &crq);
2644
2645 if (adapter->netdev->flags & IFF_PROMISC) {
2646 if (adapter->promisc_supported) {
2647 crq.request_capability.capability =
2648 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002649 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002650 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002651 ibmvnic_send_crq(adapter, &crq);
2652 }
2653 } else {
2654 crq.request_capability.capability =
2655 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002656 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002657 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 ibmvnic_send_crq(adapter, &crq);
2659 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002660}
2661
2662static int pending_scrq(struct ibmvnic_adapter *adapter,
2663 struct ibmvnic_sub_crq_queue *scrq)
2664{
2665 union sub_crq *entry = &scrq->msgs[scrq->cur];
2666
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002667 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668 return 1;
2669 else
2670 return 0;
2671}
2672
2673static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2674 struct ibmvnic_sub_crq_queue *scrq)
2675{
2676 union sub_crq *entry;
2677 unsigned long flags;
2678
2679 spin_lock_irqsave(&scrq->lock, flags);
2680 entry = &scrq->msgs[scrq->cur];
2681 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2682 if (++scrq->cur == scrq->size)
2683 scrq->cur = 0;
2684 } else {
2685 entry = NULL;
2686 }
2687 spin_unlock_irqrestore(&scrq->lock, flags);
2688
2689 return entry;
2690}
2691
2692static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2693{
2694 struct ibmvnic_crq_queue *queue = &adapter->crq;
2695 union ibmvnic_crq *crq;
2696
2697 crq = &queue->msgs[queue->cur];
2698 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2699 if (++queue->cur == queue->size)
2700 queue->cur = 0;
2701 } else {
2702 crq = NULL;
2703 }
2704
2705 return crq;
2706}
2707
2708static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2709 union sub_crq *sub_crq)
2710{
2711 unsigned int ua = adapter->vdev->unit_address;
2712 struct device *dev = &adapter->vdev->dev;
2713 u64 *u64_crq = (u64 *)sub_crq;
2714 int rc;
2715
2716 netdev_dbg(adapter->netdev,
2717 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2718 (unsigned long int)cpu_to_be64(remote_handle),
2719 (unsigned long int)cpu_to_be64(u64_crq[0]),
2720 (unsigned long int)cpu_to_be64(u64_crq[1]),
2721 (unsigned long int)cpu_to_be64(u64_crq[2]),
2722 (unsigned long int)cpu_to_be64(u64_crq[3]));
2723
2724 /* Make sure the hypervisor sees the complete request */
2725 mb();
2726
2727 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2728 cpu_to_be64(remote_handle),
2729 cpu_to_be64(u64_crq[0]),
2730 cpu_to_be64(u64_crq[1]),
2731 cpu_to_be64(u64_crq[2]),
2732 cpu_to_be64(u64_crq[3]));
2733
2734 if (rc) {
2735 if (rc == H_CLOSED)
2736 dev_warn(dev, "CRQ Queue closed\n");
2737 dev_err(dev, "Send error (rc=%d)\n", rc);
2738 }
2739
2740 return rc;
2741}
2742
Thomas Falconad7775d2016-04-01 17:20:34 -05002743static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2744 u64 remote_handle, u64 ioba, u64 num_entries)
2745{
2746 unsigned int ua = adapter->vdev->unit_address;
2747 struct device *dev = &adapter->vdev->dev;
2748 int rc;
2749
2750 /* Make sure the hypervisor sees the complete request */
2751 mb();
2752 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2753 cpu_to_be64(remote_handle),
2754 ioba, num_entries);
2755
2756 if (rc) {
2757 if (rc == H_CLOSED)
2758 dev_warn(dev, "CRQ Queue closed\n");
2759 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2760 }
2761
2762 return rc;
2763}
2764
Thomas Falcon032c5e82015-12-21 11:26:06 -06002765static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2766 union ibmvnic_crq *crq)
2767{
2768 unsigned int ua = adapter->vdev->unit_address;
2769 struct device *dev = &adapter->vdev->dev;
2770 u64 *u64_crq = (u64 *)crq;
2771 int rc;
2772
2773 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2774 (unsigned long int)cpu_to_be64(u64_crq[0]),
2775 (unsigned long int)cpu_to_be64(u64_crq[1]));
2776
2777 /* Make sure the hypervisor sees the complete request */
2778 mb();
2779
2780 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2781 cpu_to_be64(u64_crq[0]),
2782 cpu_to_be64(u64_crq[1]));
2783
2784 if (rc) {
2785 if (rc == H_CLOSED)
2786 dev_warn(dev, "CRQ Queue closed\n");
2787 dev_warn(dev, "Send error (rc=%d)\n", rc);
2788 }
2789
2790 return rc;
2791}
2792
2793static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2794{
2795 union ibmvnic_crq crq;
2796
2797 memset(&crq, 0, sizeof(crq));
2798 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2799 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2800 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2801
2802 return ibmvnic_send_crq(adapter, &crq);
2803}
2804
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805static int send_version_xchg(struct ibmvnic_adapter *adapter)
2806{
2807 union ibmvnic_crq crq;
2808
2809 memset(&crq, 0, sizeof(crq));
2810 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2811 crq.version_exchange.cmd = VERSION_EXCHANGE;
2812 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2813
2814 return ibmvnic_send_crq(adapter, &crq);
2815}
2816
Nathan Fontenot37798d02017-11-08 11:23:56 -06002817struct vnic_login_client_data {
2818 u8 type;
2819 __be16 len;
2820 char name;
2821} __packed;
2822
2823static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2824{
2825 int len;
2826
2827 /* Calculate the amount of buffer space needed for the
2828 * vnic client data in the login buffer. There are four entries,
2829 * OS name, LPAR name, device name, and a null last entry.
2830 */
2831 len = 4 * sizeof(struct vnic_login_client_data);
2832 len += 6; /* "Linux" plus NULL */
2833 len += strlen(utsname()->nodename) + 1;
2834 len += strlen(adapter->netdev->name) + 1;
2835
2836 return len;
2837}
2838
2839static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2840 struct vnic_login_client_data *vlcd)
2841{
2842 const char *os_name = "Linux";
2843 int len;
2844
2845 /* Type 1 - LPAR OS */
2846 vlcd->type = 1;
2847 len = strlen(os_name) + 1;
2848 vlcd->len = cpu_to_be16(len);
2849 strncpy(&vlcd->name, os_name, len);
2850 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2851
2852 /* Type 2 - LPAR name */
2853 vlcd->type = 2;
2854 len = strlen(utsname()->nodename) + 1;
2855 vlcd->len = cpu_to_be16(len);
2856 strncpy(&vlcd->name, utsname()->nodename, len);
2857 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2858
2859 /* Type 3 - device name */
2860 vlcd->type = 3;
2861 len = strlen(adapter->netdev->name) + 1;
2862 vlcd->len = cpu_to_be16(len);
2863 strncpy(&vlcd->name, adapter->netdev->name, len);
2864}
2865
Thomas Falcon032c5e82015-12-21 11:26:06 -06002866static void send_login(struct ibmvnic_adapter *adapter)
2867{
2868 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2869 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002870 struct device *dev = &adapter->vdev->dev;
2871 dma_addr_t rsp_buffer_token;
2872 dma_addr_t buffer_token;
2873 size_t rsp_buffer_size;
2874 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002875 size_t buffer_size;
2876 __be64 *tx_list_p;
2877 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06002878 int client_data_len;
2879 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002880 int i;
2881
Nathan Fontenot37798d02017-11-08 11:23:56 -06002882 client_data_len = vnic_client_data_len(adapter);
2883
Thomas Falcon032c5e82015-12-21 11:26:06 -06002884 buffer_size =
2885 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06002886 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
2887 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002888
Nathan Fontenot37798d02017-11-08 11:23:56 -06002889 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002890 if (!login_buffer)
2891 goto buf_alloc_failed;
2892
2893 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2894 DMA_TO_DEVICE);
2895 if (dma_mapping_error(dev, buffer_token)) {
2896 dev_err(dev, "Couldn't map login buffer\n");
2897 goto buf_map_failed;
2898 }
2899
John Allen498cd8e2016-04-06 11:49:55 -05002900 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2901 sizeof(u64) * adapter->req_tx_queues +
2902 sizeof(u64) * adapter->req_rx_queues +
2903 sizeof(u64) * adapter->req_rx_queues +
2904 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002905
2906 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2907 if (!login_rsp_buffer)
2908 goto buf_rsp_alloc_failed;
2909
2910 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2911 rsp_buffer_size, DMA_FROM_DEVICE);
2912 if (dma_mapping_error(dev, rsp_buffer_token)) {
2913 dev_err(dev, "Couldn't map login rsp buffer\n");
2914 goto buf_rsp_map_failed;
2915 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002916
Thomas Falcon032c5e82015-12-21 11:26:06 -06002917 adapter->login_buf = login_buffer;
2918 adapter->login_buf_token = buffer_token;
2919 adapter->login_buf_sz = buffer_size;
2920 adapter->login_rsp_buf = login_rsp_buffer;
2921 adapter->login_rsp_buf_token = rsp_buffer_token;
2922 adapter->login_rsp_buf_sz = rsp_buffer_size;
2923
2924 login_buffer->len = cpu_to_be32(buffer_size);
2925 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2926 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2927 login_buffer->off_txcomp_subcrqs =
2928 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2929 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2930 login_buffer->off_rxcomp_subcrqs =
2931 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2932 sizeof(u64) * adapter->req_tx_queues);
2933 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2934 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2935
2936 tx_list_p = (__be64 *)((char *)login_buffer +
2937 sizeof(struct ibmvnic_login_buffer));
2938 rx_list_p = (__be64 *)((char *)login_buffer +
2939 sizeof(struct ibmvnic_login_buffer) +
2940 sizeof(u64) * adapter->req_tx_queues);
2941
2942 for (i = 0; i < adapter->req_tx_queues; i++) {
2943 if (adapter->tx_scrq[i]) {
2944 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2945 crq_num);
2946 }
2947 }
2948
2949 for (i = 0; i < adapter->req_rx_queues; i++) {
2950 if (adapter->rx_scrq[i]) {
2951 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2952 crq_num);
2953 }
2954 }
2955
Nathan Fontenot37798d02017-11-08 11:23:56 -06002956 /* Insert vNIC login client data */
2957 vlcd = (struct vnic_login_client_data *)
2958 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
2959 login_buffer->client_data_offset =
2960 cpu_to_be32((char *)vlcd - (char *)login_buffer);
2961 login_buffer->client_data_len = cpu_to_be32(client_data_len);
2962
2963 vnic_add_client_data(adapter, vlcd);
2964
Thomas Falcon032c5e82015-12-21 11:26:06 -06002965 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2966 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2967 netdev_dbg(adapter->netdev, "%016lx\n",
2968 ((unsigned long int *)(adapter->login_buf))[i]);
2969 }
2970
2971 memset(&crq, 0, sizeof(crq));
2972 crq.login.first = IBMVNIC_CRQ_CMD;
2973 crq.login.cmd = LOGIN;
2974 crq.login.ioba = cpu_to_be32(buffer_token);
2975 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 ibmvnic_send_crq(adapter, &crq);
2977
2978 return;
2979
Thomas Falcon032c5e82015-12-21 11:26:06 -06002980buf_rsp_map_failed:
2981 kfree(login_rsp_buffer);
2982buf_rsp_alloc_failed:
2983 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2984buf_map_failed:
2985 kfree(login_buffer);
2986buf_alloc_failed:
2987 return;
2988}
2989
2990static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2991 u32 len, u8 map_id)
2992{
2993 union ibmvnic_crq crq;
2994
2995 memset(&crq, 0, sizeof(crq));
2996 crq.request_map.first = IBMVNIC_CRQ_CMD;
2997 crq.request_map.cmd = REQUEST_MAP;
2998 crq.request_map.map_id = map_id;
2999 crq.request_map.ioba = cpu_to_be32(addr);
3000 crq.request_map.len = cpu_to_be32(len);
3001 ibmvnic_send_crq(adapter, &crq);
3002}
3003
3004static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3005{
3006 union ibmvnic_crq crq;
3007
3008 memset(&crq, 0, sizeof(crq));
3009 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3010 crq.request_unmap.cmd = REQUEST_UNMAP;
3011 crq.request_unmap.map_id = map_id;
3012 ibmvnic_send_crq(adapter, &crq);
3013}
3014
3015static void send_map_query(struct ibmvnic_adapter *adapter)
3016{
3017 union ibmvnic_crq crq;
3018
3019 memset(&crq, 0, sizeof(crq));
3020 crq.query_map.first = IBMVNIC_CRQ_CMD;
3021 crq.query_map.cmd = QUERY_MAP;
3022 ibmvnic_send_crq(adapter, &crq);
3023}
3024
3025/* Send a series of CRQs requesting various capabilities of the VNIC server */
3026static void send_cap_queries(struct ibmvnic_adapter *adapter)
3027{
3028 union ibmvnic_crq crq;
3029
Thomas Falcon901e0402017-02-15 12:17:59 -06003030 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003031 memset(&crq, 0, sizeof(crq));
3032 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3033 crq.query_capability.cmd = QUERY_CAPABILITY;
3034
3035 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003036 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003037 ibmvnic_send_crq(adapter, &crq);
3038
3039 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003040 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003041 ibmvnic_send_crq(adapter, &crq);
3042
3043 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003044 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 ibmvnic_send_crq(adapter, &crq);
3046
3047 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003048 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003049 ibmvnic_send_crq(adapter, &crq);
3050
3051 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003052 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 ibmvnic_send_crq(adapter, &crq);
3054
3055 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003056 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003057 ibmvnic_send_crq(adapter, &crq);
3058
3059 crq.query_capability.capability =
3060 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003061 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003062 ibmvnic_send_crq(adapter, &crq);
3063
3064 crq.query_capability.capability =
3065 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003066 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003067 ibmvnic_send_crq(adapter, &crq);
3068
3069 crq.query_capability.capability =
3070 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003071 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003072 ibmvnic_send_crq(adapter, &crq);
3073
3074 crq.query_capability.capability =
3075 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003076 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003077 ibmvnic_send_crq(adapter, &crq);
3078
3079 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003080 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003081 ibmvnic_send_crq(adapter, &crq);
3082
3083 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003084 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003085 ibmvnic_send_crq(adapter, &crq);
3086
3087 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003088 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 ibmvnic_send_crq(adapter, &crq);
3090
3091 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003092 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003093 ibmvnic_send_crq(adapter, &crq);
3094
3095 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003096 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003097 ibmvnic_send_crq(adapter, &crq);
3098
3099 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003100 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003101 ibmvnic_send_crq(adapter, &crq);
3102
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003103 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3104 atomic_inc(&adapter->running_cap_crqs);
3105 ibmvnic_send_crq(adapter, &crq);
3106
Thomas Falcon032c5e82015-12-21 11:26:06 -06003107 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003108 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109 ibmvnic_send_crq(adapter, &crq);
3110
3111 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003112 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003113 ibmvnic_send_crq(adapter, &crq);
3114
3115 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003116 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003117 ibmvnic_send_crq(adapter, &crq);
3118
3119 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003120 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003121 ibmvnic_send_crq(adapter, &crq);
3122
3123 crq.query_capability.capability =
3124 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003125 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 ibmvnic_send_crq(adapter, &crq);
3127
3128 crq.query_capability.capability =
3129 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003130 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003131 ibmvnic_send_crq(adapter, &crq);
3132
3133 crq.query_capability.capability =
3134 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003135 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003136 ibmvnic_send_crq(adapter, &crq);
3137
3138 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003139 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003140 ibmvnic_send_crq(adapter, &crq);
3141}
3142
3143static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3144{
3145 struct device *dev = &adapter->vdev->dev;
3146 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3147 union ibmvnic_crq crq;
3148 int i;
3149
3150 dma_unmap_single(dev, adapter->ip_offload_tok,
3151 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3152
3153 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3154 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3155 netdev_dbg(adapter->netdev, "%016lx\n",
3156 ((unsigned long int *)(buf))[i]);
3157
3158 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3159 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3160 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3161 buf->tcp_ipv4_chksum);
3162 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3163 buf->tcp_ipv6_chksum);
3164 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3165 buf->udp_ipv4_chksum);
3166 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3167 buf->udp_ipv6_chksum);
3168 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3169 buf->large_tx_ipv4);
3170 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3171 buf->large_tx_ipv6);
3172 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3173 buf->large_rx_ipv4);
3174 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3175 buf->large_rx_ipv6);
3176 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3177 buf->max_ipv4_header_size);
3178 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3179 buf->max_ipv6_header_size);
3180 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3181 buf->max_tcp_header_size);
3182 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3183 buf->max_udp_header_size);
3184 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3185 buf->max_large_tx_size);
3186 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3187 buf->max_large_rx_size);
3188 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3189 buf->ipv6_extension_header);
3190 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3191 buf->tcp_pseudosum_req);
3192 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3193 buf->num_ipv6_ext_headers);
3194 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3195 buf->off_ipv6_ext_headers);
3196
3197 adapter->ip_offload_ctrl_tok =
3198 dma_map_single(dev, &adapter->ip_offload_ctrl,
3199 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3200
3201 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3202 dev_err(dev, "Couldn't map ip offload control buffer\n");
3203 return;
3204 }
3205
3206 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3207 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3208 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3209 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3210 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003211 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3212 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003213
Thomas Falconfdb06102017-10-17 12:36:55 -05003214 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003215 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3216 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3217
Thomas Falcon15482052017-10-17 12:36:54 -05003218 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003219
3220 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3221 adapter->netdev->features |= NETIF_F_IP_CSUM;
3222
3223 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3224 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3225
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003226 if ((adapter->netdev->features &
3227 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3228 adapter->netdev->features |= NETIF_F_RXCSUM;
3229
Thomas Falconfdb06102017-10-17 12:36:55 -05003230 if (buf->large_tx_ipv4)
3231 adapter->netdev->features |= NETIF_F_TSO;
3232 if (buf->large_tx_ipv6)
3233 adapter->netdev->features |= NETIF_F_TSO6;
3234
Thomas Falconaa0bf852017-10-17 12:36:56 -05003235 adapter->netdev->hw_features |= adapter->netdev->features;
3236
Thomas Falcon032c5e82015-12-21 11:26:06 -06003237 memset(&crq, 0, sizeof(crq));
3238 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3239 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3240 crq.control_ip_offload.len =
3241 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3242 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3243 ibmvnic_send_crq(adapter, &crq);
3244}
3245
3246static void handle_error_info_rsp(union ibmvnic_crq *crq,
3247 struct ibmvnic_adapter *adapter)
3248{
3249 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003250 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003251 unsigned long flags;
3252 bool found = false;
3253 int i;
3254
3255 if (!crq->request_error_rsp.rc.code) {
3256 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3257 crq->request_error_rsp.rc.code);
3258 return;
3259 }
3260
3261 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003262 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003263 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3264 found = true;
3265 list_del(&error_buff->list);
3266 break;
3267 }
3268 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3269
3270 if (!found) {
3271 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003272 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003273 return;
3274 }
3275
3276 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003277 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278
3279 for (i = 0; i < error_buff->len; i++) {
3280 pr_cont("%02x", (int)error_buff->buff[i]);
3281 if (i % 8 == 7)
3282 pr_cont(" ");
3283 }
3284 pr_cont("\n");
3285
3286 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3287 DMA_FROM_DEVICE);
3288 kfree(error_buff->buff);
3289 kfree(error_buff);
3290}
3291
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003292static void request_error_information(struct ibmvnic_adapter *adapter,
3293 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003294{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003295 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003296 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003298 unsigned long timeout = msecs_to_jiffies(30000);
3299 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003300 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003301 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302
3303 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3304 if (!error_buff)
3305 return;
3306
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003307 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003308 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3309 if (!error_buff->buff) {
3310 kfree(error_buff);
3311 return;
3312 }
3313
3314 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3315 DMA_FROM_DEVICE);
3316 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003317 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003318 kfree(error_buff->buff);
3319 kfree(error_buff);
3320 return;
3321 }
3322
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003324 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003325
3326 spin_lock_irqsave(&adapter->error_list_lock, flags);
3327 list_add_tail(&error_buff->list, &adapter->errors);
3328 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3329
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003330 memset(&crq, 0, sizeof(crq));
3331 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3332 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3333 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3334 crq.request_error_info.len = cpu_to_be32(detail_len);
3335 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3336
3337 rc = ibmvnic_send_crq(adapter, &crq);
3338 if (rc) {
3339 netdev_err(netdev, "failed to request error information\n");
3340 goto err_info_fail;
3341 }
3342
3343 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3344 netdev_err(netdev, "timeout waiting for error information\n");
3345 goto err_info_fail;
3346 }
3347
3348 return;
3349
3350err_info_fail:
3351 spin_lock_irqsave(&adapter->error_list_lock, flags);
3352 list_del(&error_buff->list);
3353 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3354
3355 kfree(error_buff->buff);
3356 kfree(error_buff);
3357}
3358
3359static void handle_error_indication(union ibmvnic_crq *crq,
3360 struct ibmvnic_adapter *adapter)
3361{
3362 struct device *dev = &adapter->vdev->dev;
3363
3364 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3365 crq->error_indication.flags
3366 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3367 be32_to_cpu(crq->error_indication.error_id),
3368 be16_to_cpu(crq->error_indication.error_cause));
3369
3370 if (be32_to_cpu(crq->error_indication.error_id))
3371 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003372
3373 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3374 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003375 else
3376 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377}
3378
3379static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3380 struct ibmvnic_adapter *adapter)
3381{
3382 struct net_device *netdev = adapter->netdev;
3383 struct device *dev = &adapter->vdev->dev;
3384 long rc;
3385
3386 rc = crq->change_mac_addr_rsp.rc.code;
3387 if (rc) {
3388 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3389 return;
3390 }
3391 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3392 ETH_ALEN);
3393}
3394
3395static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3396 struct ibmvnic_adapter *adapter)
3397{
3398 struct device *dev = &adapter->vdev->dev;
3399 u64 *req_value;
3400 char *name;
3401
Thomas Falcon901e0402017-02-15 12:17:59 -06003402 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003403 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3404 case REQ_TX_QUEUES:
3405 req_value = &adapter->req_tx_queues;
3406 name = "tx";
3407 break;
3408 case REQ_RX_QUEUES:
3409 req_value = &adapter->req_rx_queues;
3410 name = "rx";
3411 break;
3412 case REQ_RX_ADD_QUEUES:
3413 req_value = &adapter->req_rx_add_queues;
3414 name = "rx_add";
3415 break;
3416 case REQ_TX_ENTRIES_PER_SUBCRQ:
3417 req_value = &adapter->req_tx_entries_per_subcrq;
3418 name = "tx_entries_per_subcrq";
3419 break;
3420 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3421 req_value = &adapter->req_rx_add_entries_per_subcrq;
3422 name = "rx_add_entries_per_subcrq";
3423 break;
3424 case REQ_MTU:
3425 req_value = &adapter->req_mtu;
3426 name = "mtu";
3427 break;
3428 case PROMISC_REQUESTED:
3429 req_value = &adapter->promisc;
3430 name = "promisc";
3431 break;
3432 default:
3433 dev_err(dev, "Got invalid cap request rsp %d\n",
3434 crq->request_capability.capability);
3435 return;
3436 }
3437
3438 switch (crq->request_capability_rsp.rc.code) {
3439 case SUCCESS:
3440 break;
3441 case PARTIALSUCCESS:
3442 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3443 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003444 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003445 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003446 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003447 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003448 return;
3449 default:
3450 dev_err(dev, "Error %d in request cap rsp\n",
3451 crq->request_capability_rsp.rc.code);
3452 return;
3453 }
3454
3455 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003456 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003457 union ibmvnic_crq newcrq;
3458 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3459 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3460 &adapter->ip_offload_buf;
3461
Thomas Falcon249168a2017-02-15 12:18:00 -06003462 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003463 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3464 buf_sz,
3465 DMA_FROM_DEVICE);
3466
3467 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3468 if (!firmware_has_feature(FW_FEATURE_CMO))
3469 dev_err(dev, "Couldn't map offload buffer\n");
3470 return;
3471 }
3472
3473 memset(&newcrq, 0, sizeof(newcrq));
3474 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3475 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3476 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3477 newcrq.query_ip_offload.ioba =
3478 cpu_to_be32(adapter->ip_offload_tok);
3479
3480 ibmvnic_send_crq(adapter, &newcrq);
3481 }
3482}
3483
3484static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3485 struct ibmvnic_adapter *adapter)
3486{
3487 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003488 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003489 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3490 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003491 int i;
3492
3493 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3494 DMA_BIDIRECTIONAL);
3495 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3496 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3497
John Allen498cd8e2016-04-06 11:49:55 -05003498 /* If the number of queues requested can't be allocated by the
3499 * server, the login response will return with code 1. We will need
3500 * to resend the login buffer with fewer queues requested.
3501 */
3502 if (login_rsp_crq->generic.rc.code) {
3503 adapter->renegotiate = true;
3504 complete(&adapter->init_done);
3505 return 0;
3506 }
3507
John Allenc26eba02017-10-26 16:23:25 -05003508 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3509
Thomas Falcon032c5e82015-12-21 11:26:06 -06003510 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3511 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3512 netdev_dbg(adapter->netdev, "%016lx\n",
3513 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3514 }
3515
3516 /* Sanity checks */
3517 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3518 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3519 adapter->req_rx_add_queues !=
3520 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3521 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3522 ibmvnic_remove(adapter->vdev);
3523 return -EIO;
3524 }
3525 complete(&adapter->init_done);
3526
Thomas Falcon032c5e82015-12-21 11:26:06 -06003527 return 0;
3528}
3529
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3531 struct ibmvnic_adapter *adapter)
3532{
3533 struct device *dev = &adapter->vdev->dev;
3534 long rc;
3535
3536 rc = crq->request_unmap_rsp.rc.code;
3537 if (rc)
3538 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3539}
3540
3541static void handle_query_map_rsp(union ibmvnic_crq *crq,
3542 struct ibmvnic_adapter *adapter)
3543{
3544 struct net_device *netdev = adapter->netdev;
3545 struct device *dev = &adapter->vdev->dev;
3546 long rc;
3547
3548 rc = crq->query_map_rsp.rc.code;
3549 if (rc) {
3550 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3551 return;
3552 }
3553 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3554 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3555 crq->query_map_rsp.free_pages);
3556}
3557
3558static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3559 struct ibmvnic_adapter *adapter)
3560{
3561 struct net_device *netdev = adapter->netdev;
3562 struct device *dev = &adapter->vdev->dev;
3563 long rc;
3564
Thomas Falcon901e0402017-02-15 12:17:59 -06003565 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003566 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003567 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003568 rc = crq->query_capability.rc.code;
3569 if (rc) {
3570 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3571 goto out;
3572 }
3573
3574 switch (be16_to_cpu(crq->query_capability.capability)) {
3575 case MIN_TX_QUEUES:
3576 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003577 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003578 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3579 adapter->min_tx_queues);
3580 break;
3581 case MIN_RX_QUEUES:
3582 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003583 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003584 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3585 adapter->min_rx_queues);
3586 break;
3587 case MIN_RX_ADD_QUEUES:
3588 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003589 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3591 adapter->min_rx_add_queues);
3592 break;
3593 case MAX_TX_QUEUES:
3594 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003595 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003596 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3597 adapter->max_tx_queues);
3598 break;
3599 case MAX_RX_QUEUES:
3600 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003601 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003602 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3603 adapter->max_rx_queues);
3604 break;
3605 case MAX_RX_ADD_QUEUES:
3606 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003607 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3609 adapter->max_rx_add_queues);
3610 break;
3611 case MIN_TX_ENTRIES_PER_SUBCRQ:
3612 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003613 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3615 adapter->min_tx_entries_per_subcrq);
3616 break;
3617 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3618 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003619 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003620 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3621 adapter->min_rx_add_entries_per_subcrq);
3622 break;
3623 case MAX_TX_ENTRIES_PER_SUBCRQ:
3624 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003625 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003626 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3627 adapter->max_tx_entries_per_subcrq);
3628 break;
3629 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3630 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003631 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003632 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3633 adapter->max_rx_add_entries_per_subcrq);
3634 break;
3635 case TCP_IP_OFFLOAD:
3636 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003637 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003638 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3639 adapter->tcp_ip_offload);
3640 break;
3641 case PROMISC_SUPPORTED:
3642 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003643 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003644 netdev_dbg(netdev, "promisc_supported = %lld\n",
3645 adapter->promisc_supported);
3646 break;
3647 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003648 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003649 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003650 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3651 break;
3652 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003653 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003654 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003655 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3656 break;
3657 case MAX_MULTICAST_FILTERS:
3658 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003659 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003660 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3661 adapter->max_multicast_filters);
3662 break;
3663 case VLAN_HEADER_INSERTION:
3664 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003665 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003666 if (adapter->vlan_header_insertion)
3667 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3668 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3669 adapter->vlan_header_insertion);
3670 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003671 case RX_VLAN_HEADER_INSERTION:
3672 adapter->rx_vlan_header_insertion =
3673 be64_to_cpu(crq->query_capability.number);
3674 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3675 adapter->rx_vlan_header_insertion);
3676 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003677 case MAX_TX_SG_ENTRIES:
3678 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003679 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003680 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3681 adapter->max_tx_sg_entries);
3682 break;
3683 case RX_SG_SUPPORTED:
3684 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003685 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003686 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3687 adapter->rx_sg_supported);
3688 break;
3689 case OPT_TX_COMP_SUB_QUEUES:
3690 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003691 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003692 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3693 adapter->opt_tx_comp_sub_queues);
3694 break;
3695 case OPT_RX_COMP_QUEUES:
3696 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003697 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003698 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3699 adapter->opt_rx_comp_queues);
3700 break;
3701 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3702 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003703 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003704 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3705 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3706 break;
3707 case OPT_TX_ENTRIES_PER_SUBCRQ:
3708 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003709 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003710 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3711 adapter->opt_tx_entries_per_subcrq);
3712 break;
3713 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3714 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003715 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3717 adapter->opt_rxba_entries_per_subcrq);
3718 break;
3719 case TX_RX_DESC_REQ:
3720 adapter->tx_rx_desc_req = crq->query_capability.number;
3721 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3722 adapter->tx_rx_desc_req);
3723 break;
3724
3725 default:
3726 netdev_err(netdev, "Got invalid cap rsp %d\n",
3727 crq->query_capability.capability);
3728 }
3729
3730out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003731 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3732 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003733 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003734 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003735}
3736
Thomas Falcon032c5e82015-12-21 11:26:06 -06003737static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3738 struct ibmvnic_adapter *adapter)
3739{
3740 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3741 struct net_device *netdev = adapter->netdev;
3742 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003743 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003744 long rc;
3745
3746 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003747 (unsigned long int)cpu_to_be64(u64_crq[0]),
3748 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003749 switch (gen_crq->first) {
3750 case IBMVNIC_CRQ_INIT_RSP:
3751 switch (gen_crq->cmd) {
3752 case IBMVNIC_CRQ_INIT:
3753 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003754 adapter->from_passive_init = true;
3755 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003756 break;
3757 case IBMVNIC_CRQ_INIT_COMPLETE:
3758 dev_info(dev, "Partner initialization complete\n");
3759 send_version_xchg(adapter);
3760 break;
3761 default:
3762 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3763 }
3764 return;
3765 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003766 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003767 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003768 dev_info(dev, "Migrated, re-enabling adapter\n");
3769 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003770 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3771 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003772 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003773 } else {
3774 /* The adapter lost the connection */
3775 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3776 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003777 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003778 }
3779 return;
3780 case IBMVNIC_CRQ_CMD_RSP:
3781 break;
3782 default:
3783 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3784 gen_crq->first);
3785 return;
3786 }
3787
3788 switch (gen_crq->cmd) {
3789 case VERSION_EXCHANGE_RSP:
3790 rc = crq->version_exchange_rsp.rc.code;
3791 if (rc) {
3792 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3793 break;
3794 }
3795 dev_info(dev, "Partner protocol version is %d\n",
3796 crq->version_exchange_rsp.version);
3797 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3798 ibmvnic_version)
3799 ibmvnic_version =
3800 be16_to_cpu(crq->version_exchange_rsp.version);
3801 send_cap_queries(adapter);
3802 break;
3803 case QUERY_CAPABILITY_RSP:
3804 handle_query_cap_rsp(crq, adapter);
3805 break;
3806 case QUERY_MAP_RSP:
3807 handle_query_map_rsp(crq, adapter);
3808 break;
3809 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003810 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3811 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003812 break;
3813 case REQUEST_UNMAP_RSP:
3814 handle_request_unmap_rsp(crq, adapter);
3815 break;
3816 case REQUEST_CAPABILITY_RSP:
3817 handle_request_cap_rsp(crq, adapter);
3818 break;
3819 case LOGIN_RSP:
3820 netdev_dbg(netdev, "Got Login Response\n");
3821 handle_login_rsp(crq, adapter);
3822 break;
3823 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003824 netdev_dbg(netdev,
3825 "Got Logical Link State Response, state: %d rc: %d\n",
3826 crq->logical_link_state_rsp.link_state,
3827 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003828 adapter->logical_link_state =
3829 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003830 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3831 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003832 break;
3833 case LINK_STATE_INDICATION:
3834 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3835 adapter->phys_link_state =
3836 crq->link_state_indication.phys_link_state;
3837 adapter->logical_link_state =
3838 crq->link_state_indication.logical_link_state;
3839 break;
3840 case CHANGE_MAC_ADDR_RSP:
3841 netdev_dbg(netdev, "Got MAC address change Response\n");
3842 handle_change_mac_rsp(crq, adapter);
3843 break;
3844 case ERROR_INDICATION:
3845 netdev_dbg(netdev, "Got Error Indication\n");
3846 handle_error_indication(crq, adapter);
3847 break;
3848 case REQUEST_ERROR_RSP:
3849 netdev_dbg(netdev, "Got Error Detail Response\n");
3850 handle_error_info_rsp(crq, adapter);
3851 break;
3852 case REQUEST_STATISTICS_RSP:
3853 netdev_dbg(netdev, "Got Statistics Response\n");
3854 complete(&adapter->stats_done);
3855 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003856 case QUERY_IP_OFFLOAD_RSP:
3857 netdev_dbg(netdev, "Got Query IP offload Response\n");
3858 handle_query_ip_offload_rsp(adapter);
3859 break;
3860 case MULTICAST_CTRL_RSP:
3861 netdev_dbg(netdev, "Got multicast control Response\n");
3862 break;
3863 case CONTROL_IP_OFFLOAD_RSP:
3864 netdev_dbg(netdev, "Got Control IP offload Response\n");
3865 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3866 sizeof(adapter->ip_offload_ctrl),
3867 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003868 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003869 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003870 case COLLECT_FW_TRACE_RSP:
3871 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3872 complete(&adapter->fw_done);
3873 break;
3874 default:
3875 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3876 gen_crq->cmd);
3877 }
3878}
3879
3880static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3881{
3882 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003883
Thomas Falcon6c267b32017-02-15 12:17:58 -06003884 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003885 return IRQ_HANDLED;
3886}
3887
3888static void ibmvnic_tasklet(void *data)
3889{
3890 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003891 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003892 union ibmvnic_crq *crq;
3893 unsigned long flags;
3894 bool done = false;
3895
3896 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897 while (!done) {
3898 /* Pull all the valid messages off the CRQ */
3899 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3900 ibmvnic_handle_crq(crq, adapter);
3901 crq->generic.first = 0;
3902 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003903
3904 /* remain in tasklet until all
3905 * capabilities responses are received
3906 */
3907 if (!adapter->wait_capability)
3908 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003910 /* if capabilities CRQ's were sent in this tasklet, the following
3911 * tasklet must wait until all responses are received
3912 */
3913 if (atomic_read(&adapter->running_cap_crqs) != 0)
3914 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003916}
3917
3918static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3919{
3920 struct vio_dev *vdev = adapter->vdev;
3921 int rc;
3922
3923 do {
3924 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3925 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3926
3927 if (rc)
3928 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3929
3930 return rc;
3931}
3932
3933static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3934{
3935 struct ibmvnic_crq_queue *crq = &adapter->crq;
3936 struct device *dev = &adapter->vdev->dev;
3937 struct vio_dev *vdev = adapter->vdev;
3938 int rc;
3939
3940 /* Close the CRQ */
3941 do {
3942 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3943 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3944
3945 /* Clean out the queue */
3946 memset(crq->msgs, 0, PAGE_SIZE);
3947 crq->cur = 0;
3948
3949 /* And re-open it again */
3950 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3951 crq->msg_token, PAGE_SIZE);
3952
3953 if (rc == H_CLOSED)
3954 /* Adapter is good, but other end is not ready */
3955 dev_warn(dev, "Partner adapter not ready\n");
3956 else if (rc != 0)
3957 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3958
3959 return rc;
3960}
3961
Nathan Fontenotf9928872017-03-30 02:48:54 -04003962static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003963{
3964 struct ibmvnic_crq_queue *crq = &adapter->crq;
3965 struct vio_dev *vdev = adapter->vdev;
3966 long rc;
3967
Nathan Fontenotf9928872017-03-30 02:48:54 -04003968 if (!crq->msgs)
3969 return;
3970
Thomas Falcon032c5e82015-12-21 11:26:06 -06003971 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3972 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003973 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003974 do {
3975 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3976 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3977
3978 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3979 DMA_BIDIRECTIONAL);
3980 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003981 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003982}
3983
Nathan Fontenotf9928872017-03-30 02:48:54 -04003984static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985{
3986 struct ibmvnic_crq_queue *crq = &adapter->crq;
3987 struct device *dev = &adapter->vdev->dev;
3988 struct vio_dev *vdev = adapter->vdev;
3989 int rc, retrc = -ENOMEM;
3990
Nathan Fontenotf9928872017-03-30 02:48:54 -04003991 if (crq->msgs)
3992 return 0;
3993
Thomas Falcon032c5e82015-12-21 11:26:06 -06003994 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3995 /* Should we allocate more than one page? */
3996
3997 if (!crq->msgs)
3998 return -ENOMEM;
3999
4000 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4001 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4002 DMA_BIDIRECTIONAL);
4003 if (dma_mapping_error(dev, crq->msg_token))
4004 goto map_failed;
4005
4006 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4007 crq->msg_token, PAGE_SIZE);
4008
4009 if (rc == H_RESOURCE)
4010 /* maybe kexecing and resource is busy. try a reset */
4011 rc = ibmvnic_reset_crq(adapter);
4012 retrc = rc;
4013
4014 if (rc == H_CLOSED) {
4015 dev_warn(dev, "Partner adapter not ready\n");
4016 } else if (rc) {
4017 dev_warn(dev, "Error %d opening adapter\n", rc);
4018 goto reg_crq_failed;
4019 }
4020
4021 retrc = 0;
4022
Thomas Falcon6c267b32017-02-15 12:17:58 -06004023 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4024 (unsigned long)adapter);
4025
Thomas Falcon032c5e82015-12-21 11:26:06 -06004026 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4027 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4028 adapter);
4029 if (rc) {
4030 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4031 vdev->irq, rc);
4032 goto req_irq_failed;
4033 }
4034
4035 rc = vio_enable_interrupts(vdev);
4036 if (rc) {
4037 dev_err(dev, "Error %d enabling interrupts\n", rc);
4038 goto req_irq_failed;
4039 }
4040
4041 crq->cur = 0;
4042 spin_lock_init(&crq->lock);
4043
4044 return retrc;
4045
4046req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004047 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004048 do {
4049 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4050 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4051reg_crq_failed:
4052 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4053map_failed:
4054 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004055 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004056 return retrc;
4057}
4058
John Allenf6ef6402017-03-17 17:13:42 -05004059static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4060{
4061 struct device *dev = &adapter->vdev->dev;
4062 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05004063 int rc;
4064
John Allenc26eba02017-10-26 16:23:25 -05004065 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004066 rc = ibmvnic_reset_crq(adapter);
4067 if (!rc)
4068 rc = vio_enable_interrupts(adapter->vdev);
4069 } else {
4070 rc = init_crq_queue(adapter);
4071 }
4072
John Allenf6ef6402017-03-17 17:13:42 -05004073 if (rc) {
4074 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4075 return rc;
4076 }
4077
John Allen017892c12017-05-26 10:30:19 -04004078 adapter->from_passive_init = false;
4079
John Allenf6ef6402017-03-17 17:13:42 -05004080 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004081 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004082 ibmvnic_send_crq_init(adapter);
4083 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4084 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004085 return -1;
4086 }
4087
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004088 if (adapter->init_done_rc) {
4089 release_crq_queue(adapter);
4090 return adapter->init_done_rc;
4091 }
4092
John Allen017892c12017-05-26 10:30:19 -04004093 if (adapter->from_passive_init) {
4094 adapter->state = VNIC_OPEN;
4095 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004096 return -1;
4097 }
4098
John Allenc26eba02017-10-26 16:23:25 -05004099 if (adapter->resetting && !adapter->wait_for_reset)
Nathan Fontenot57a49432017-05-26 10:31:12 -04004100 rc = reset_sub_crq_queues(adapter);
4101 else
4102 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004103 if (rc) {
4104 dev_err(dev, "Initialization of sub crqs failed\n");
4105 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004106 return rc;
4107 }
4108
4109 rc = init_sub_crq_irqs(adapter);
4110 if (rc) {
4111 dev_err(dev, "Failed to initialize sub crq irqs\n");
4112 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004113 }
4114
4115 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004116}
4117
Thomas Falcon40c9db82017-06-12 12:35:04 -05004118static struct device_attribute dev_attr_failover;
4119
Thomas Falcon032c5e82015-12-21 11:26:06 -06004120static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4121{
4122 struct ibmvnic_adapter *adapter;
4123 struct net_device *netdev;
4124 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125 int rc;
4126
4127 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4128 dev->unit_address);
4129
4130 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4131 VETH_MAC_ADDR, NULL);
4132 if (!mac_addr_p) {
4133 dev_err(&dev->dev,
4134 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4135 __FILE__, __LINE__);
4136 return 0;
4137 }
4138
4139 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4140 IBMVNIC_MAX_TX_QUEUES);
4141 if (!netdev)
4142 return -ENOMEM;
4143
4144 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004145 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004146 dev_set_drvdata(&dev->dev, netdev);
4147 adapter->vdev = dev;
4148 adapter->netdev = netdev;
4149
4150 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4151 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4152 netdev->irq = dev->irq;
4153 netdev->netdev_ops = &ibmvnic_netdev_ops;
4154 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4155 SET_NETDEV_DEV(netdev, &dev->dev);
4156
4157 spin_lock_init(&adapter->stats_lock);
4158
Thomas Falcon032c5e82015-12-21 11:26:06 -06004159 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004160 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004161
Nathan Fontenoted651a12017-05-03 14:04:38 -04004162 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4163 INIT_LIST_HEAD(&adapter->rwi_list);
4164 mutex_init(&adapter->reset_lock);
4165 mutex_init(&adapter->rwi_lock);
4166 adapter->resetting = false;
4167
John Allenc26eba02017-10-26 16:23:25 -05004168 adapter->mac_change_pending = false;
4169
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004170 do {
4171 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004172 if (rc && rc != EAGAIN)
4173 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004174 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175
Thomas Falconf39f0d12017-02-14 10:22:59 -06004176 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004177 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4178 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179
Thomas Falcon40c9db82017-06-12 12:35:04 -05004180 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004181 if (rc)
4182 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004183
Mick Tarsele876a8a2017-09-28 13:53:18 -07004184 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004185 rc = register_netdev(netdev);
4186 if (rc) {
4187 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004188 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004189 }
4190 dev_info(&dev->dev, "ibmvnic registered\n");
4191
Nathan Fontenot90c80142017-05-03 14:04:32 -04004192 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004193
4194 adapter->wait_for_reset = false;
4195
Thomas Falcon032c5e82015-12-21 11:26:06 -06004196 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004197
4198ibmvnic_register_fail:
4199 device_remove_file(&dev->dev, &dev_attr_failover);
4200
4201ibmvnic_init_fail:
4202 release_sub_crqs(adapter);
4203 release_crq_queue(adapter);
4204 free_netdev(netdev);
4205
4206 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004207}
4208
4209static int ibmvnic_remove(struct vio_dev *dev)
4210{
4211 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004212 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004213
Nathan Fontenot90c80142017-05-03 14:04:32 -04004214 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004215 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004216 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004217
4218 release_resources(adapter);
4219 release_sub_crqs(adapter);
4220 release_crq_queue(adapter);
4221
Nathan Fontenot90c80142017-05-03 14:04:32 -04004222 adapter->state = VNIC_REMOVED;
4223
Nathan Fontenoted651a12017-05-03 14:04:38 -04004224 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004225 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004226 free_netdev(netdev);
4227 dev_set_drvdata(&dev->dev, NULL);
4228
4229 return 0;
4230}
4231
Thomas Falcon40c9db82017-06-12 12:35:04 -05004232static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4233 const char *buf, size_t count)
4234{
4235 struct net_device *netdev = dev_get_drvdata(dev);
4236 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4237 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4238 __be64 session_token;
4239 long rc;
4240
4241 if (!sysfs_streq(buf, "1"))
4242 return -EINVAL;
4243
4244 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4245 H_GET_SESSION_TOKEN, 0, 0, 0);
4246 if (rc) {
4247 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4248 rc);
4249 return -EINVAL;
4250 }
4251
4252 session_token = (__be64)retbuf[0];
4253 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4254 be64_to_cpu(session_token));
4255 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4256 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4257 if (rc) {
4258 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4259 rc);
4260 return -EINVAL;
4261 }
4262
4263 return count;
4264}
4265
4266static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4267
Thomas Falcon032c5e82015-12-21 11:26:06 -06004268static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4269{
4270 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4271 struct ibmvnic_adapter *adapter;
4272 struct iommu_table *tbl;
4273 unsigned long ret = 0;
4274 int i;
4275
4276 tbl = get_iommu_table_base(&vdev->dev);
4277
4278 /* netdev inits at probe time along with the structures we need below*/
4279 if (!netdev)
4280 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4281
4282 adapter = netdev_priv(netdev);
4283
4284 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004285 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4286
4287 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4288 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4289
4290 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4291 i++)
4292 ret += adapter->rx_pool[i].size *
4293 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4294
4295 return ret;
4296}
4297
4298static int ibmvnic_resume(struct device *dev)
4299{
4300 struct net_device *netdev = dev_get_drvdata(dev);
4301 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004302
John Allencb89ba22017-06-19 11:27:53 -05004303 if (adapter->state != VNIC_OPEN)
4304 return 0;
4305
John Allena2488782017-07-24 13:26:06 -05004306 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004307
4308 return 0;
4309}
4310
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304311static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312 {"network", "IBM,vnic"},
4313 {"", "" }
4314};
4315MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4316
4317static const struct dev_pm_ops ibmvnic_pm_ops = {
4318 .resume = ibmvnic_resume
4319};
4320
4321static struct vio_driver ibmvnic_driver = {
4322 .id_table = ibmvnic_device_table,
4323 .probe = ibmvnic_probe,
4324 .remove = ibmvnic_remove,
4325 .get_desired_dma = ibmvnic_get_desired_dma,
4326 .name = ibmvnic_driver_name,
4327 .pm = &ibmvnic_pm_ops,
4328};
4329
4330/* module functions */
4331static int __init ibmvnic_module_init(void)
4332{
4333 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4334 IBMVNIC_DRIVER_VERSION);
4335
4336 return vio_register_driver(&ibmvnic_driver);
4337}
4338
4339static void __exit ibmvnic_module_exit(void)
4340{
4341 vio_unregister_driver(&ibmvnic_driver);
4342}
4343
4344module_init(ibmvnic_module_init);
4345module_exit(ibmvnic_module_exit);