blob: 58e01432c2aaf5d9ea4238572fc517ea6f9166a0 [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>
Thomas Falcon4eb50ce2017-12-18 12:52:40 -060062#include <linux/if_arp.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060063#include <linux/in.h>
64#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050065#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060066#include <linux/irq.h>
67#include <linux/kthread.h>
68#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060069#include <linux/interrupt.h>
70#include <net/net_namespace.h>
71#include <asm/hvcall.h>
72#include <linux/atomic.h>
73#include <asm/vio.h>
74#include <asm/iommu.h>
75#include <linux/uaccess.h>
76#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050077#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040078#include <linux/if_vlan.h>
Nathan Fontenot37798d02017-11-08 11:23:56 -060079#include <linux/utsname.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060080
81#include "ibmvnic.h"
82
83static const char ibmvnic_driver_name[] = "ibmvnic";
84static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
85
Thomas Falcon78b07ac2017-06-01 15:32:34 -050086MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060087MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
90
91static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92static int ibmvnic_remove(struct vio_dev *);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -060093static void release_sub_crqs(struct ibmvnic_adapter *, bool);
Thomas Falcon032c5e82015-12-21 11:26:06 -060094static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500100static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600101static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110static int ibmvnic_poll(struct napi_struct *napi, int data);
111static void send_map_query(struct ibmvnic_adapter *adapter);
112static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113static void send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600114static int send_login(struct ibmvnic_adapter *adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500115static void send_cap_queries(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500116static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500117static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500118static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400119static void release_crq_queue(struct ibmvnic_adapter *);
John Allenc26eba02017-10-26 16:23:25 -0500120static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600121
122struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
124 int offset;
125};
126
127#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
130
131static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
154};
155
156static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
158 unsigned long *irq)
159{
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
161 long rc;
162
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
164 *number = retbuf[0];
165 *irq = retbuf[1];
166
167 return rc;
168}
169
Thomas Falcon032c5e82015-12-21 11:26:06 -0600170static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
172{
173 struct device *dev = &adapter->vdev->dev;
174
175 ltb->size = size;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
177 GFP_KERNEL);
178
179 if (!ltb->buff) {
180 dev_err(dev, "Couldn't alloc long term buffer\n");
181 return -ENOMEM;
182 }
183 ltb->map_id = adapter->map_id;
184 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500185
186 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600189 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500190
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
194 return -1;
195 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600196 return 0;
197}
198
199static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
201{
202 struct device *dev = &adapter->vdev->dev;
203
Nathan Fontenotc657e322017-03-30 02:49:06 -0400204 if (!ltb->buff)
205 return;
206
Nathan Fontenoted651a12017-05-03 14:04:38 -0400207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500209 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600211}
212
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500213static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
215{
216 memset(ltb->buff, 0, ltb->size);
217
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
221
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
227 }
228 return 0;
229}
230
Thomas Falconf185a492017-05-26 10:30:48 -0400231static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
232{
233 int i;
234
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
236 i++)
237 adapter->rx_pool[i].active = 0;
238}
239
Thomas Falcon032c5e82015-12-21 11:26:06 -0600240static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
242{
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
248 struct sk_buff *skb;
249 unsigned int offset;
250 dma_addr_t dma_addr;
251 unsigned char *dst;
252 u64 *handle_array;
253 int shift = 0;
254 int index;
255 int i;
256
Thomas Falconf185a492017-05-26 10:30:48 -0400257 if (!pool->active)
258 return;
259
Thomas Falcon032c5e82015-12-21 11:26:06 -0600260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
262 off_rxadd_subcrqs));
263
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
266 if (!skb) {
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
269 break;
270 }
271
272 index = pool->free_map[pool->next_free];
273
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
276
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
283
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
289
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
296
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
300 * truncated.
301 */
302#ifdef __LITTLE_ENDIAN__
303 shift = 8;
304#endif
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
306
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
308 &sub_crq);
309 if (lpar_rc != H_SUCCESS)
310 goto failure;
311
312 buffers_added++;
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
315 }
316 atomic_add(buffers_added, &pool->available);
317 return;
318
319failure:
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600323
324 dev_kfree_skb_any(skb);
325 adapter->replenish_add_buff_failure++;
326 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400327
328 if (lpar_rc == H_CLOSED) {
329 /* Disable buffer pool replenishment and report carrier off if
330 * queue is closed. Firmware guarantees that a signal will
331 * be sent to the driver, triggering a reset.
332 */
333 deactivate_rx_pools(adapter);
334 netif_carrier_off(adapter->netdev);
335 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600336}
337
338static void replenish_pools(struct ibmvnic_adapter *adapter)
339{
340 int i;
341
Thomas Falcon032c5e82015-12-21 11:26:06 -0600342 adapter->replenish_task_cycles++;
343 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
344 i++) {
345 if (adapter->rx_pool[i].active)
346 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
347 }
348}
349
John Allen3d52b592017-08-02 16:44:14 -0500350static void release_stats_buffers(struct ibmvnic_adapter *adapter)
351{
352 kfree(adapter->tx_stats_buffers);
353 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600354 adapter->tx_stats_buffers = NULL;
355 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500356}
357
358static int init_stats_buffers(struct ibmvnic_adapter *adapter)
359{
360 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600361 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500362 sizeof(struct ibmvnic_tx_queue_stats),
363 GFP_KERNEL);
364 if (!adapter->tx_stats_buffers)
365 return -ENOMEM;
366
367 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600368 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500369 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;
John Allen896d8692018-01-18 16:26:31 -0600413 u64 *size_array;
414
415 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
416 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400417
418 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
419 for (i = 0; i < rx_scrqs; i++) {
420 rx_pool = &adapter->rx_pool[i];
421
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500422 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
423
John Allen896d8692018-01-18 16:26:31 -0600424 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
425 free_long_term_buff(adapter, &rx_pool->long_term_buff);
426 rx_pool->buff_size = be64_to_cpu(size_array[i]);
427 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
428 rx_pool->size *
429 rx_pool->buff_size);
430 } else {
431 rc = reset_long_term_buff(adapter,
432 &rx_pool->long_term_buff);
433 }
434
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500435 if (rc)
436 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400437
438 for (j = 0; j < rx_pool->size; j++)
439 rx_pool->free_map[j] = j;
440
441 memset(rx_pool->rx_buff, 0,
442 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
443
444 atomic_set(&rx_pool->available, 0);
445 rx_pool->next_alloc = 0;
446 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500447 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400448 }
449
450 return 0;
451}
452
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400453static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600454{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400455 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400456 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600457
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400458 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600459 return;
460
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600461 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400462 rx_pool = &adapter->rx_pool[i];
463
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500464 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
465
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400466 kfree(rx_pool->free_map);
467 free_long_term_buff(adapter, &rx_pool->long_term_buff);
468
469 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400470 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400471
472 for (j = 0; j < rx_pool->size; j++) {
473 if (rx_pool->rx_buff[j].skb) {
474 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
475 rx_pool->rx_buff[i].skb = NULL;
476 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600477 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400478
479 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600480 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400481
482 kfree(adapter->rx_pool);
483 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600484 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400485}
486
487static int init_rx_pools(struct net_device *netdev)
488{
489 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
490 struct device *dev = &adapter->vdev->dev;
491 struct ibmvnic_rx_pool *rx_pool;
492 int rxadd_subcrqs;
493 u64 *size_array;
494 int i, j;
495
496 rxadd_subcrqs =
497 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
498 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
499 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
500
501 adapter->rx_pool = kcalloc(rxadd_subcrqs,
502 sizeof(struct ibmvnic_rx_pool),
503 GFP_KERNEL);
504 if (!adapter->rx_pool) {
505 dev_err(dev, "Failed to allocate rx pools\n");
506 return -1;
507 }
508
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600509 adapter->num_active_rx_pools = rxadd_subcrqs;
510
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400511 for (i = 0; i < rxadd_subcrqs; i++) {
512 rx_pool = &adapter->rx_pool[i];
513
514 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500515 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400516 i, adapter->req_rx_add_entries_per_subcrq,
517 be64_to_cpu(size_array[i]));
518
519 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
520 rx_pool->index = i;
521 rx_pool->buff_size = be64_to_cpu(size_array[i]);
522 rx_pool->active = 1;
523
524 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
525 GFP_KERNEL);
526 if (!rx_pool->free_map) {
527 release_rx_pools(adapter);
528 return -1;
529 }
530
531 rx_pool->rx_buff = kcalloc(rx_pool->size,
532 sizeof(struct ibmvnic_rx_buff),
533 GFP_KERNEL);
534 if (!rx_pool->rx_buff) {
535 dev_err(dev, "Couldn't alloc rx buffers\n");
536 release_rx_pools(adapter);
537 return -1;
538 }
539
540 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
541 rx_pool->size * rx_pool->buff_size)) {
542 release_rx_pools(adapter);
543 return -1;
544 }
545
546 for (j = 0; j < rx_pool->size; ++j)
547 rx_pool->free_map[j] = j;
548
549 atomic_set(&rx_pool->available, 0);
550 rx_pool->next_alloc = 0;
551 rx_pool->next_free = 0;
552 }
553
554 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600555}
556
Thomas Falcone26dc252018-03-16 20:00:25 -0500557static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
558 struct ibmvnic_tx_pool *tx_pool)
559{
560 int rc, i;
561
562 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
563 if (rc)
564 return rc;
565
566 memset(tx_pool->tx_buff, 0,
567 tx_pool->num_buffers *
568 sizeof(struct ibmvnic_tx_buff));
569
570 for (i = 0; i < tx_pool->num_buffers; i++)
571 tx_pool->free_map[i] = i;
572
573 tx_pool->consumer_index = 0;
574 tx_pool->producer_index = 0;
575
576 return 0;
577}
578
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400579static int reset_tx_pools(struct ibmvnic_adapter *adapter)
580{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400581 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500582 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400583
584 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
585 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500586 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500587 if (rc)
588 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500589 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500590 if (rc)
591 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400592 }
593
594 return 0;
595}
596
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200597static void release_vpd_data(struct ibmvnic_adapter *adapter)
598{
599 if (!adapter->vpd)
600 return;
601
602 kfree(adapter->vpd->buff);
603 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600604
605 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200606}
607
Thomas Falconfb794212018-03-16 20:00:26 -0500608static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
609 struct ibmvnic_tx_pool *tx_pool)
610{
611 kfree(tx_pool->tx_buff);
612 kfree(tx_pool->free_map);
613 free_long_term_buff(adapter, &tx_pool->long_term_buff);
614}
615
Nathan Fontenotc657e322017-03-30 02:49:06 -0400616static void release_tx_pools(struct ibmvnic_adapter *adapter)
617{
John Allen896d8692018-01-18 16:26:31 -0600618 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400619
620 if (!adapter->tx_pool)
621 return;
622
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600623 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500624 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
625 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400626 }
627
628 kfree(adapter->tx_pool);
629 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500630 kfree(adapter->tso_pool);
631 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600632 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400633}
634
Thomas Falcon32053062018-03-16 20:00:27 -0500635static int init_one_tx_pool(struct net_device *netdev,
636 struct ibmvnic_tx_pool *tx_pool,
637 int num_entries, int buf_size)
638{
639 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
640 int i;
641
642 tx_pool->tx_buff = kcalloc(num_entries,
643 sizeof(struct ibmvnic_tx_buff),
644 GFP_KERNEL);
645 if (!tx_pool->tx_buff)
646 return -1;
647
648 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
649 num_entries * buf_size))
650 return -1;
651
652 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
653 if (!tx_pool->free_map)
654 return -1;
655
656 for (i = 0; i < num_entries; i++)
657 tx_pool->free_map[i] = i;
658
659 tx_pool->consumer_index = 0;
660 tx_pool->producer_index = 0;
661 tx_pool->num_buffers = num_entries;
662 tx_pool->buf_size = buf_size;
663
664 return 0;
665}
666
Nathan Fontenotc657e322017-03-30 02:49:06 -0400667static int init_tx_pools(struct net_device *netdev)
668{
669 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400670 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500671 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400672
673 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
674 adapter->tx_pool = kcalloc(tx_subcrqs,
675 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
676 if (!adapter->tx_pool)
677 return -1;
678
Thomas Falcon32053062018-03-16 20:00:27 -0500679 adapter->tso_pool = kcalloc(tx_subcrqs,
680 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
681 if (!adapter->tso_pool)
682 return -1;
683
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600684 adapter->num_active_tx_pools = tx_subcrqs;
685
Nathan Fontenotc657e322017-03-30 02:49:06 -0400686 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500687 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
688 adapter->req_tx_entries_per_subcrq,
689 adapter->req_mtu + VLAN_HLEN);
690 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400691 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500692 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400693 }
694
Thomas Falcon32053062018-03-16 20:00:27 -0500695 init_one_tx_pool(netdev, &adapter->tso_pool[i],
696 IBMVNIC_TSO_BUFS,
697 IBMVNIC_TSO_BUF_SZ);
698 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400699 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500700 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400701 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400702 }
703
704 return 0;
705}
706
Nathan Fontenot661a2622017-04-19 13:44:58 -0400707static void release_error_buffers(struct ibmvnic_adapter *adapter)
708{
709 struct device *dev = &adapter->vdev->dev;
710 struct ibmvnic_error_buff *error_buff, *tmp;
711 unsigned long flags;
712
713 spin_lock_irqsave(&adapter->error_list_lock, flags);
714 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
715 list_del(&error_buff->list);
716 dma_unmap_single(dev, error_buff->dma, error_buff->len,
717 DMA_FROM_DEVICE);
718 kfree(error_buff->buff);
719 kfree(error_buff);
720 }
721 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
722}
723
John Allend944c3d62017-05-26 10:30:13 -0400724static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
725{
726 int i;
727
728 if (adapter->napi_enabled)
729 return;
730
731 for (i = 0; i < adapter->req_rx_queues; i++)
732 napi_enable(&adapter->napi[i]);
733
734 adapter->napi_enabled = true;
735}
736
737static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
738{
739 int i;
740
741 if (!adapter->napi_enabled)
742 return;
743
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500744 for (i = 0; i < adapter->req_rx_queues; i++) {
745 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400746 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500747 }
John Allend944c3d62017-05-26 10:30:13 -0400748
749 adapter->napi_enabled = false;
750}
751
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600752static int init_napi(struct ibmvnic_adapter *adapter)
753{
754 int i;
755
756 adapter->napi = kcalloc(adapter->req_rx_queues,
757 sizeof(struct napi_struct), GFP_KERNEL);
758 if (!adapter->napi)
759 return -ENOMEM;
760
761 for (i = 0; i < adapter->req_rx_queues; i++) {
762 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
763 netif_napi_add(adapter->netdev, &adapter->napi[i],
764 ibmvnic_poll, NAPI_POLL_WEIGHT);
765 }
766
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600767 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600768 return 0;
769}
770
771static void release_napi(struct ibmvnic_adapter *adapter)
772{
773 int i;
774
775 if (!adapter->napi)
776 return;
777
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600778 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600779 if (&adapter->napi[i]) {
780 netdev_dbg(adapter->netdev,
781 "Releasing napi[%d]\n", i);
782 netif_napi_del(&adapter->napi[i]);
783 }
784 }
785
786 kfree(adapter->napi);
787 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600788 adapter->num_active_rx_napi = 0;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600789}
790
John Allena57a5d22017-03-17 17:13:41 -0500791static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600792{
793 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500794 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600795 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500796 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600797
John Allenbd0b6722017-03-17 17:13:40 -0500798 do {
799 if (adapter->renegotiate) {
800 adapter->renegotiate = false;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600801 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500802
803 reinit_completion(&adapter->init_done);
804 send_cap_queries(adapter);
805 if (!wait_for_completion_timeout(&adapter->init_done,
806 timeout)) {
807 dev_err(dev, "Capabilities query timeout\n");
808 return -1;
809 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500810 rc = init_sub_crqs(adapter);
811 if (rc) {
812 dev_err(dev,
813 "Initialization of SCRQ's failed\n");
814 return -1;
815 }
816 rc = init_sub_crq_irqs(adapter);
817 if (rc) {
818 dev_err(dev,
819 "Initialization of SCRQ's irqs failed\n");
820 return -1;
821 }
John Allenbd0b6722017-03-17 17:13:40 -0500822 }
823
824 reinit_completion(&adapter->init_done);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600825 rc = send_login(adapter);
826 if (rc) {
827 dev_err(dev, "Unable to attempt device login\n");
828 return rc;
829 } else if (!wait_for_completion_timeout(&adapter->init_done,
John Allenbd0b6722017-03-17 17:13:40 -0500830 timeout)) {
831 dev_err(dev, "Login timeout\n");
832 return -1;
833 }
834 } while (adapter->renegotiate);
835
Thomas Falcon3d166132018-01-10 19:39:52 -0600836 /* handle pending MAC address changes after successful login */
837 if (adapter->mac_change_pending) {
838 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
839 adapter->mac_change_pending = false;
840 }
841
John Allena57a5d22017-03-17 17:13:41 -0500842 return 0;
843}
844
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600845static void release_login_buffer(struct ibmvnic_adapter *adapter)
846{
847 kfree(adapter->login_buf);
848 adapter->login_buf = NULL;
849}
850
851static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
852{
853 kfree(adapter->login_rsp_buf);
854 adapter->login_rsp_buf = NULL;
855}
856
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400857static void release_resources(struct ibmvnic_adapter *adapter)
858{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200859 release_vpd_data(adapter);
860
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400861 release_tx_pools(adapter);
862 release_rx_pools(adapter);
863
Nathan Fontenot661a2622017-04-19 13:44:58 -0400864 release_error_buffers(adapter);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600865 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600866 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400867}
868
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400869static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
870{
871 struct net_device *netdev = adapter->netdev;
872 unsigned long timeout = msecs_to_jiffies(30000);
873 union ibmvnic_crq crq;
874 bool resend;
875 int rc;
876
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500877 netdev_dbg(netdev, "setting link state %d\n", link_state);
878
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400879 memset(&crq, 0, sizeof(crq));
880 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
881 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
882 crq.logical_link_state.link_state = link_state;
883
884 do {
885 resend = false;
886
887 reinit_completion(&adapter->init_done);
888 rc = ibmvnic_send_crq(adapter, &crq);
889 if (rc) {
890 netdev_err(netdev, "Failed to set link state\n");
891 return rc;
892 }
893
894 if (!wait_for_completion_timeout(&adapter->init_done,
895 timeout)) {
896 netdev_err(netdev, "timeout setting link state\n");
897 return -1;
898 }
899
900 if (adapter->init_done_rc == 1) {
901 /* Partuial success, delay and re-send */
902 mdelay(1000);
903 resend = true;
904 }
905 } while (resend);
906
907 return 0;
908}
909
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400910static int set_real_num_queues(struct net_device *netdev)
911{
912 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
913 int rc;
914
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500915 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
916 adapter->req_tx_queues, adapter->req_rx_queues);
917
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400918 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
919 if (rc) {
920 netdev_err(netdev, "failed to set the number of tx queues\n");
921 return rc;
922 }
923
924 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
925 if (rc)
926 netdev_err(netdev, "failed to set the number of rx queues\n");
927
928 return rc;
929}
930
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200931static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
932{
933 struct device *dev = &adapter->vdev->dev;
934 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200935 int len = 0;
936
937 if (adapter->vpd->buff)
938 len = adapter->vpd->len;
939
John Allen69d08dc2018-01-18 16:27:58 -0600940 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200941 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
942 crq.get_vpd_size.cmd = GET_VPD_SIZE;
943 ibmvnic_send_crq(adapter, &crq);
944 wait_for_completion(&adapter->fw_done);
945
946 if (!adapter->vpd->len)
947 return -ENODATA;
948
949 if (!adapter->vpd->buff)
950 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
951 else if (adapter->vpd->len != len)
952 adapter->vpd->buff =
953 krealloc(adapter->vpd->buff,
954 adapter->vpd->len, GFP_KERNEL);
955
956 if (!adapter->vpd->buff) {
957 dev_err(dev, "Could allocate VPD buffer\n");
958 return -ENOMEM;
959 }
960
961 adapter->vpd->dma_addr =
962 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
963 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200964 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200965 dev_err(dev, "Could not map VPD buffer\n");
966 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600967 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200968 return -ENOMEM;
969 }
970
971 reinit_completion(&adapter->fw_done);
972 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
973 crq.get_vpd.cmd = GET_VPD;
974 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
975 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
976 ibmvnic_send_crq(adapter, &crq);
977 wait_for_completion(&adapter->fw_done);
978
979 return 0;
980}
981
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400982static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500983{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400984 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600985 int rc;
John Allena57a5d22017-03-17 17:13:41 -0500986
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400987 rc = set_real_num_queues(netdev);
988 if (rc)
989 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500990
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200991 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
992 if (!adapter->vpd)
993 return -ENOMEM;
994
John Allen69d08dc2018-01-18 16:27:58 -0600995 /* Vital Product Data (VPD) */
996 rc = ibmvnic_get_vpd(adapter);
997 if (rc) {
998 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
999 return rc;
1000 }
1001
Thomas Falcon032c5e82015-12-21 11:26:06 -06001002 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001003
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001004 rc = init_napi(adapter);
1005 if (rc)
1006 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001007
Thomas Falcon032c5e82015-12-21 11:26:06 -06001008 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001009
1010 rc = init_rx_pools(netdev);
1011 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001012 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001013
Nathan Fontenotc657e322017-03-30 02:49:06 -04001014 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001015 return rc;
1016}
1017
Nathan Fontenoted651a12017-05-03 14:04:38 -04001018static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001019{
1020 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001021 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001022 int i, rc;
1023
Nathan Fontenot90c80142017-05-03 14:04:32 -04001024 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001025 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001026 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001027
Thomas Falcon032c5e82015-12-21 11:26:06 -06001028 /* We're ready to receive frames, enable the sub-crq interrupts and
1029 * set the logical link state to up
1030 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001031 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001032 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001033 if (prev_state == VNIC_CLOSED)
1034 enable_irq(adapter->rx_scrq[i]->irq);
1035 else
1036 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1037 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001038
Nathan Fontenoted651a12017-05-03 14:04:38 -04001039 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001040 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001041 if (prev_state == VNIC_CLOSED)
1042 enable_irq(adapter->tx_scrq[i]->irq);
1043 else
1044 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1045 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001046
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001047 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001048 if (rc) {
1049 for (i = 0; i < adapter->req_rx_queues; i++)
1050 napi_disable(&adapter->napi[i]);
1051 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001052 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001053 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001054
Nathan Fontenoted651a12017-05-03 14:04:38 -04001055 netif_tx_start_all_queues(netdev);
1056
1057 if (prev_state == VNIC_CLOSED) {
1058 for (i = 0; i < adapter->req_rx_queues; i++)
1059 napi_schedule(&adapter->napi[i]);
1060 }
1061
1062 adapter->state = VNIC_OPEN;
1063 return rc;
1064}
1065
1066static int ibmvnic_open(struct net_device *netdev)
1067{
1068 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001069 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001070
1071 mutex_lock(&adapter->reset_lock);
1072
1073 if (adapter->state != VNIC_CLOSED) {
1074 rc = ibmvnic_login(netdev);
1075 if (rc) {
1076 mutex_unlock(&adapter->reset_lock);
1077 return rc;
1078 }
1079
1080 rc = init_resources(adapter);
1081 if (rc) {
1082 netdev_err(netdev, "failed to initialize resources\n");
1083 release_resources(adapter);
1084 mutex_unlock(&adapter->reset_lock);
1085 return rc;
1086 }
1087 }
1088
1089 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001090 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001091
Nathan Fontenoted651a12017-05-03 14:04:38 -04001092 mutex_unlock(&adapter->reset_lock);
1093
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001094 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001095}
1096
Thomas Falcond0869c02018-02-13 18:23:43 -06001097static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1098{
1099 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001100 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001101 u64 rx_entries;
1102 int rx_scrqs;
1103 int i, j;
1104
1105 if (!adapter->rx_pool)
1106 return;
1107
1108 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1109 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1110
1111 /* Free any remaining skbs in the rx buffer pools */
1112 for (i = 0; i < rx_scrqs; i++) {
1113 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001114 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001115 continue;
1116
1117 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1118 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001119 rx_buff = &rx_pool->rx_buff[j];
1120 if (rx_buff && rx_buff->skb) {
1121 dev_kfree_skb_any(rx_buff->skb);
1122 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001123 }
1124 }
1125 }
1126}
1127
Thomas Falcone9e1e972018-03-16 20:00:30 -05001128static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1129 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001130{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001131 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001132 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001133 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001134
Dan Carpenter050e85c2018-03-23 14:36:15 +03001135 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001136 return;
1137
1138 tx_entries = tx_pool->num_buffers;
1139
1140 for (i = 0; i < tx_entries; i++) {
1141 tx_buff = &tx_pool->tx_buff[i];
1142 if (tx_buff && tx_buff->skb) {
1143 dev_kfree_skb_any(tx_buff->skb);
1144 tx_buff->skb = NULL;
1145 }
1146 }
1147}
1148
1149static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1150{
1151 int tx_scrqs;
1152 int i;
1153
1154 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001155 return;
1156
1157 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001158
1159 /* Free any remaining skbs in the tx buffer pools */
1160 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001161 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001162 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1163 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001164 }
1165}
1166
John Allen6095e592018-03-30 13:44:21 -05001167static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001168{
John Allen6095e592018-03-30 13:44:21 -05001169 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001170 int i;
1171
Nathan Fontenot46293b92017-05-03 14:05:02 -04001172 if (adapter->tx_scrq) {
1173 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001174 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001175 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001176 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001177 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001178 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001179 }
1180
Nathan Fontenot46293b92017-05-03 14:05:02 -04001181 if (adapter->rx_scrq) {
1182 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001183 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001184 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001185 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001186 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001187 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001188 }
1189 }
John Allen6095e592018-03-30 13:44:21 -05001190}
1191
1192static void ibmvnic_cleanup(struct net_device *netdev)
1193{
1194 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1195
1196 /* ensure that transmissions are stopped if called by do_reset */
1197 if (adapter->resetting)
1198 netif_tx_disable(netdev);
1199 else
1200 netif_tx_stop_all_queues(netdev);
1201
1202 ibmvnic_napi_disable(adapter);
1203 ibmvnic_disable_irqs(adapter);
1204
Thomas Falcond0869c02018-02-13 18:23:43 -06001205 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001206 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001207}
1208
1209static int __ibmvnic_close(struct net_device *netdev)
1210{
1211 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1212 int rc = 0;
1213
1214 adapter->state = VNIC_CLOSING;
1215 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1216 if (rc)
1217 return rc;
1218 ibmvnic_cleanup(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001219 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001220 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001221}
1222
Nathan Fontenoted651a12017-05-03 14:04:38 -04001223static int ibmvnic_close(struct net_device *netdev)
1224{
1225 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1226 int rc;
1227
1228 mutex_lock(&adapter->reset_lock);
1229 rc = __ibmvnic_close(netdev);
1230 mutex_unlock(&adapter->reset_lock);
1231
1232 return rc;
1233}
1234
Thomas Falconad7775d2016-04-01 17:20:34 -05001235/**
1236 * build_hdr_data - creates L2/L3/L4 header data buffer
1237 * @hdr_field - bitfield determining needed headers
1238 * @skb - socket buffer
1239 * @hdr_len - array of header lengths
1240 * @tot_len - total length of data
1241 *
1242 * Reads hdr_field to determine which headers are needed by firmware.
1243 * Builds a buffer containing these headers. Saves individual header
1244 * lengths and total buffer length to be used to build descriptors.
1245 */
1246static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1247 int *hdr_len, u8 *hdr_data)
1248{
1249 int len = 0;
1250 u8 *hdr;
1251
Thomas Falconda75e3b2018-03-12 11:51:02 -05001252 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1253 hdr_len[0] = sizeof(struct vlan_ethhdr);
1254 else
1255 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001256
1257 if (skb->protocol == htons(ETH_P_IP)) {
1258 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1259 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1260 hdr_len[2] = tcp_hdrlen(skb);
1261 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1262 hdr_len[2] = sizeof(struct udphdr);
1263 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1264 hdr_len[1] = sizeof(struct ipv6hdr);
1265 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1266 hdr_len[2] = tcp_hdrlen(skb);
1267 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1268 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001269 } else if (skb->protocol == htons(ETH_P_ARP)) {
1270 hdr_len[1] = arp_hdr_len(skb->dev);
1271 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001272 }
1273
1274 memset(hdr_data, 0, 120);
1275 if ((hdr_field >> 6) & 1) {
1276 hdr = skb_mac_header(skb);
1277 memcpy(hdr_data, hdr, hdr_len[0]);
1278 len += hdr_len[0];
1279 }
1280
1281 if ((hdr_field >> 5) & 1) {
1282 hdr = skb_network_header(skb);
1283 memcpy(hdr_data + len, hdr, hdr_len[1]);
1284 len += hdr_len[1];
1285 }
1286
1287 if ((hdr_field >> 4) & 1) {
1288 hdr = skb_transport_header(skb);
1289 memcpy(hdr_data + len, hdr, hdr_len[2]);
1290 len += hdr_len[2];
1291 }
1292 return len;
1293}
1294
1295/**
1296 * create_hdr_descs - create header and header extension descriptors
1297 * @hdr_field - bitfield determining needed headers
1298 * @data - buffer containing header data
1299 * @len - length of data buffer
1300 * @hdr_len - array of individual header lengths
1301 * @scrq_arr - descriptor array
1302 *
1303 * Creates header and, if needed, header extension descriptors and
1304 * places them in a descriptor array, scrq_arr
1305 */
1306
Thomas Falcon2de09682017-10-16 10:02:11 -05001307static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1308 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001309{
1310 union sub_crq hdr_desc;
1311 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001312 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001313 u8 *data, *cur;
1314 int tmp;
1315
1316 while (tmp_len > 0) {
1317 cur = hdr_data + len - tmp_len;
1318
1319 memset(&hdr_desc, 0, sizeof(hdr_desc));
1320 if (cur != hdr_data) {
1321 data = hdr_desc.hdr_ext.data;
1322 tmp = tmp_len > 29 ? 29 : tmp_len;
1323 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1324 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1325 hdr_desc.hdr_ext.len = tmp;
1326 } else {
1327 data = hdr_desc.hdr.data;
1328 tmp = tmp_len > 24 ? 24 : tmp_len;
1329 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1330 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1331 hdr_desc.hdr.len = tmp;
1332 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1333 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1334 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1335 hdr_desc.hdr.flag = hdr_field << 1;
1336 }
1337 memcpy(data, cur, tmp);
1338 tmp_len -= tmp;
1339 *scrq_arr = hdr_desc;
1340 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001341 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001342 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001343
1344 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001345}
1346
1347/**
1348 * build_hdr_descs_arr - build a header descriptor array
1349 * @skb - socket buffer
1350 * @num_entries - number of descriptors to be sent
1351 * @subcrq - first TX descriptor
1352 * @hdr_field - bit field determining which headers will be sent
1353 *
1354 * This function will build a TX descriptor array with applicable
1355 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1356 */
1357
1358static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1359 int *num_entries, u8 hdr_field)
1360{
1361 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001362 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001363 u8 *hdr_data = txbuff->hdr_data;
1364
1365 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1366 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001367 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001368 txbuff->indir_arr + 1);
1369}
1370
Thomas Falcon1f247a62018-03-12 11:51:04 -05001371static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1372 struct net_device *netdev)
1373{
1374 /* For some backing devices, mishandling of small packets
1375 * can result in a loss of connection or TX stall. Device
1376 * architects recommend that no packet should be smaller
1377 * than the minimum MTU value provided to the driver, so
1378 * pad any packets to that length
1379 */
1380 if (skb->len < netdev->min_mtu)
1381 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001382
1383 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001384}
1385
Thomas Falcon032c5e82015-12-21 11:26:06 -06001386static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1387{
1388 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1389 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001390 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001391 struct device *dev = &adapter->vdev->dev;
1392 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001393 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001394 struct ibmvnic_tx_pool *tx_pool;
1395 unsigned int tx_send_failed = 0;
1396 unsigned int tx_map_failed = 0;
1397 unsigned int tx_dropped = 0;
1398 unsigned int tx_packets = 0;
1399 unsigned int tx_bytes = 0;
1400 dma_addr_t data_dma_addr;
1401 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001402 unsigned long lpar_rc;
1403 union sub_crq tx_crq;
1404 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001405 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001406 unsigned char *dst;
1407 u64 *handle_array;
1408 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001409 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001410 int ret = 0;
1411
Nathan Fontenoted651a12017-05-03 14:04:38 -04001412 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001413 if (!netif_subqueue_stopped(netdev, skb))
1414 netif_stop_subqueue(netdev, queue_num);
1415 dev_kfree_skb_any(skb);
1416
Thomas Falcon032c5e82015-12-21 11:26:06 -06001417 tx_send_failed++;
1418 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001419 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001420 goto out;
1421 }
1422
Thomas Falcon7083a452018-03-12 21:05:26 -05001423 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001424 tx_dropped++;
1425 tx_send_failed++;
1426 ret = NETDEV_TX_OK;
1427 goto out;
1428 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001429 if (skb_is_gso(skb))
1430 tx_pool = &adapter->tso_pool[queue_num];
1431 else
1432 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001433
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001434 tx_scrq = adapter->tx_scrq[queue_num];
1435 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1436 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1437 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1438
Thomas Falcon032c5e82015-12-21 11:26:06 -06001439 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001440
Thomas Falcon86b61a52018-03-16 20:00:29 -05001441 if (index == IBMVNIC_INVALID_MAP) {
1442 dev_kfree_skb_any(skb);
1443 tx_send_failed++;
1444 tx_dropped++;
1445 ret = NETDEV_TX_OK;
1446 goto out;
1447 }
1448
1449 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1450
Thomas Falcon06b3e352018-03-16 20:00:28 -05001451 offset = index * tx_pool->buf_size;
1452 dst = tx_pool->long_term_buff.buff + offset;
1453 memset(dst, 0, tx_pool->buf_size);
1454 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001455
Thomas Falcon15482052017-10-17 12:36:54 -05001456 if (skb_shinfo(skb)->nr_frags) {
1457 int cur, i;
1458
1459 /* Copy the head */
1460 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1461 cur = skb_headlen(skb);
1462
1463 /* Copy the frags */
1464 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1465 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1466
1467 memcpy(dst + cur,
1468 page_address(skb_frag_page(frag)) +
1469 frag->page_offset, skb_frag_size(frag));
1470 cur += skb_frag_size(frag);
1471 }
1472 } else {
1473 skb_copy_from_linear_data(skb, dst, skb->len);
1474 }
1475
Thomas Falcon032c5e82015-12-21 11:26:06 -06001476 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001477 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001478
1479 tx_buff = &tx_pool->tx_buff[index];
1480 tx_buff->skb = skb;
1481 tx_buff->data_dma[0] = data_dma_addr;
1482 tx_buff->data_len[0] = skb->len;
1483 tx_buff->index = index;
1484 tx_buff->pool_index = queue_num;
1485 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001486
1487 memset(&tx_crq, 0, sizeof(tx_crq));
1488 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1489 tx_crq.v1.type = IBMVNIC_TX_DESC;
1490 tx_crq.v1.n_crq_elem = 1;
1491 tx_crq.v1.n_sge = 1;
1492 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001493
Thomas Falconfdb06102017-10-17 12:36:55 -05001494 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001495 tx_crq.v1.correlator =
1496 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001497 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001498 tx_crq.v1.correlator = cpu_to_be32(index);
1499 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001500 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1501 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1502
1503 if (adapter->vlan_header_insertion) {
1504 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1505 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1506 }
1507
1508 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001509 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1510 proto = ip_hdr(skb)->protocol;
1511 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1512 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1513 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001514 }
1515
Thomas Falcona0dca102018-01-18 19:29:48 -06001516 if (proto == IPPROTO_TCP)
1517 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1518 else if (proto == IPPROTO_UDP)
1519 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1520
Thomas Falconad7775d2016-04-01 17:20:34 -05001521 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001522 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001523 hdrs += 2;
1524 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001525 if (skb_is_gso(skb)) {
1526 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1527 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1528 hdrs += 2;
1529 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001530 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001531 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001532 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1533 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001534 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001535 tx_buff->indir_arr[0] = tx_crq;
1536 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1537 sizeof(tx_buff->indir_arr),
1538 DMA_TO_DEVICE);
1539 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001540 dev_kfree_skb_any(skb);
1541 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001542 if (!firmware_has_feature(FW_FEATURE_CMO))
1543 dev_err(dev, "tx: unable to map descriptor array\n");
1544 tx_map_failed++;
1545 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001546 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001547 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001548 }
John Allen498cd8e2016-04-06 11:49:55 -05001549 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001550 (u64)tx_buff->indir_dma,
1551 (u64)num_entries);
1552 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001553 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001554 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1555 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001556 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001557 if (lpar_rc != H_SUCCESS) {
1558 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001559 dev_kfree_skb_any(skb);
1560 tx_buff->skb = NULL;
1561
Thomas Falconb8c80b82017-05-26 10:30:42 -04001562 if (lpar_rc == H_CLOSED) {
1563 /* Disable TX and report carrier off if queue is closed.
1564 * Firmware guarantees that a signal will be sent to the
1565 * driver, triggering a reset or some other action.
1566 */
1567 netif_tx_stop_all_queues(netdev);
1568 netif_carrier_off(netdev);
1569 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001570
Thomas Falcon032c5e82015-12-21 11:26:06 -06001571 tx_send_failed++;
1572 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001573 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001574 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001575 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001576
Thomas Falconffc385b2018-02-18 10:08:41 -06001577 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001578 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001579 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001580 netif_stop_subqueue(netdev, queue_num);
1581 }
1582
Thomas Falcon032c5e82015-12-21 11:26:06 -06001583 tx_packets++;
1584 tx_bytes += skb->len;
1585 txq->trans_start = jiffies;
1586 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001587 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001588
Thomas Falcon86b61a52018-03-16 20:00:29 -05001589tx_err_out:
1590 /* roll back consumer index and map array*/
1591 if (tx_pool->consumer_index == 0)
1592 tx_pool->consumer_index =
1593 tx_pool->num_buffers - 1;
1594 else
1595 tx_pool->consumer_index--;
1596 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001597out:
1598 netdev->stats.tx_dropped += tx_dropped;
1599 netdev->stats.tx_bytes += tx_bytes;
1600 netdev->stats.tx_packets += tx_packets;
1601 adapter->tx_send_failed += tx_send_failed;
1602 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001603 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1604 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1605 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001606
1607 return ret;
1608}
1609
1610static void ibmvnic_set_multi(struct net_device *netdev)
1611{
1612 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1613 struct netdev_hw_addr *ha;
1614 union ibmvnic_crq crq;
1615
1616 memset(&crq, 0, sizeof(crq));
1617 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1618 crq.request_capability.cmd = REQUEST_CAPABILITY;
1619
1620 if (netdev->flags & IFF_PROMISC) {
1621 if (!adapter->promisc_supported)
1622 return;
1623 } else {
1624 if (netdev->flags & IFF_ALLMULTI) {
1625 /* Accept all multicast */
1626 memset(&crq, 0, sizeof(crq));
1627 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1628 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1629 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1630 ibmvnic_send_crq(adapter, &crq);
1631 } else if (netdev_mc_empty(netdev)) {
1632 /* Reject all multicast */
1633 memset(&crq, 0, sizeof(crq));
1634 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1635 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1636 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1637 ibmvnic_send_crq(adapter, &crq);
1638 } else {
1639 /* Accept one or more multicast(s) */
1640 netdev_for_each_mc_addr(ha, netdev) {
1641 memset(&crq, 0, sizeof(crq));
1642 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1643 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1644 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1645 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1646 ha->addr);
1647 ibmvnic_send_crq(adapter, &crq);
1648 }
1649 }
1650 }
1651}
1652
John Allenc26eba02017-10-26 16:23:25 -05001653static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001654{
1655 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1656 struct sockaddr *addr = p;
1657 union ibmvnic_crq crq;
1658
1659 if (!is_valid_ether_addr(addr->sa_data))
1660 return -EADDRNOTAVAIL;
1661
1662 memset(&crq, 0, sizeof(crq));
1663 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1664 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1665 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001666
1667 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001668 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001669 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001670 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001671 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001672}
1673
John Allenc26eba02017-10-26 16:23:25 -05001674static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1675{
1676 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1677 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001678 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001679
Thomas Falcon3d166132018-01-10 19:39:52 -06001680 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001681 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1682 adapter->mac_change_pending = true;
1683 return 0;
1684 }
1685
Thomas Falconf8136142018-01-29 13:45:05 -06001686 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001687
Thomas Falconf8136142018-01-29 13:45:05 -06001688 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001689}
1690
Nathan Fontenoted651a12017-05-03 14:04:38 -04001691/**
1692 * do_reset returns zero if we are able to keep processing reset events, or
1693 * non-zero if we hit a fatal error and must halt.
1694 */
1695static int do_reset(struct ibmvnic_adapter *adapter,
1696 struct ibmvnic_rwi *rwi, u32 reset_state)
1697{
John Allen896d8692018-01-18 16:26:31 -06001698 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001699 struct net_device *netdev = adapter->netdev;
1700 int i, rc;
1701
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001702 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1703 rwi->reset_reason);
1704
Nathan Fontenoted651a12017-05-03 14:04:38 -04001705 netif_carrier_off(netdev);
1706 adapter->reset_reason = rwi->reset_reason;
1707
John Allen896d8692018-01-18 16:26:31 -06001708 old_num_rx_queues = adapter->req_rx_queues;
1709 old_num_tx_queues = adapter->req_tx_queues;
1710
Nathan Fontenoted651a12017-05-03 14:04:38 -04001711 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1712 rc = ibmvnic_reenable_crq_queue(adapter);
1713 if (rc)
1714 return 0;
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001715 ibmvnic_cleanup(netdev);
1716 } else if (rwi->reset_reason == VNIC_RESET_FAILOVER) {
1717 ibmvnic_cleanup(netdev);
1718 } else {
1719 rc = __ibmvnic_close(netdev);
1720 if (rc)
1721 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001722 }
1723
John Allenc26eba02017-10-26 16:23:25 -05001724 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1725 adapter->wait_for_reset) {
1726 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001727 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001728 release_crq_queue(adapter);
1729 }
1730
John Allen8cb31cf2017-05-26 10:30:37 -04001731 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1732 /* remove the closed state so when we call open it appears
1733 * we are coming from the probed state.
1734 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001735 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001736
John Allen8cb31cf2017-05-26 10:30:37 -04001737 rc = ibmvnic_init(adapter);
1738 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001739 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001740
1741 /* If the adapter was in PROBE state prior to the reset,
1742 * exit here.
1743 */
1744 if (reset_state == VNIC_PROBED)
1745 return 0;
1746
1747 rc = ibmvnic_login(netdev);
1748 if (rc) {
1749 adapter->state = VNIC_PROBED;
1750 return 0;
1751 }
1752
John Allenc26eba02017-10-26 16:23:25 -05001753 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1754 adapter->wait_for_reset) {
1755 rc = init_resources(adapter);
1756 if (rc)
1757 return rc;
John Allen896d8692018-01-18 16:26:31 -06001758 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1759 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001760 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001761 release_rx_pools(adapter);
1762 release_tx_pools(adapter);
1763 init_rx_pools(netdev);
1764 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001765
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001766 release_napi(adapter);
1767 init_napi(adapter);
John Allenc26eba02017-10-26 16:23:25 -05001768 } else {
1769 rc = reset_tx_pools(adapter);
1770 if (rc)
1771 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001772
John Allenc26eba02017-10-26 16:23:25 -05001773 rc = reset_rx_pools(adapter);
1774 if (rc)
1775 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001776 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001777 }
1778
John Allen6095e592018-03-30 13:44:21 -05001779 ibmvnic_disable_irqs(adapter);
John Allene676d812018-03-14 10:41:29 -05001780 adapter->state = VNIC_CLOSED;
1781
1782 if (reset_state == VNIC_CLOSED)
1783 return 0;
1784
Nathan Fontenoted651a12017-05-03 14:04:38 -04001785 rc = __ibmvnic_open(netdev);
1786 if (rc) {
1787 if (list_empty(&adapter->rwi_list))
1788 adapter->state = VNIC_CLOSED;
1789 else
1790 adapter->state = reset_state;
1791
1792 return 0;
1793 }
1794
Nathan Fontenoted651a12017-05-03 14:04:38 -04001795 /* kick napi */
1796 for (i = 0; i < adapter->req_rx_queues; i++)
1797 napi_schedule(&adapter->napi[i]);
1798
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001799 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1800 netdev_notify_peers(netdev);
1801
Thomas Falconcc85c022018-02-13 15:32:50 -06001802 netif_carrier_on(netdev);
1803
Nathan Fontenoted651a12017-05-03 14:04:38 -04001804 return 0;
1805}
1806
1807static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1808{
1809 struct ibmvnic_rwi *rwi;
1810
1811 mutex_lock(&adapter->rwi_lock);
1812
1813 if (!list_empty(&adapter->rwi_list)) {
1814 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1815 list);
1816 list_del(&rwi->list);
1817 } else {
1818 rwi = NULL;
1819 }
1820
1821 mutex_unlock(&adapter->rwi_lock);
1822 return rwi;
1823}
1824
1825static void free_all_rwi(struct ibmvnic_adapter *adapter)
1826{
1827 struct ibmvnic_rwi *rwi;
1828
1829 rwi = get_next_rwi(adapter);
1830 while (rwi) {
1831 kfree(rwi);
1832 rwi = get_next_rwi(adapter);
1833 }
1834}
1835
1836static void __ibmvnic_reset(struct work_struct *work)
1837{
1838 struct ibmvnic_rwi *rwi;
1839 struct ibmvnic_adapter *adapter;
1840 struct net_device *netdev;
1841 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001842 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001843
1844 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1845 netdev = adapter->netdev;
1846
1847 mutex_lock(&adapter->reset_lock);
1848 adapter->resetting = true;
1849 reset_state = adapter->state;
1850
1851 rwi = get_next_rwi(adapter);
1852 while (rwi) {
1853 rc = do_reset(adapter, rwi, reset_state);
1854 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001855 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001856 break;
1857
1858 rwi = get_next_rwi(adapter);
1859 }
1860
John Allenc26eba02017-10-26 16:23:25 -05001861 if (adapter->wait_for_reset) {
1862 adapter->wait_for_reset = false;
1863 adapter->reset_done_rc = rc;
1864 complete(&adapter->reset_done);
1865 }
1866
Nathan Fontenoted651a12017-05-03 14:04:38 -04001867 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001868 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001869 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001870 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001871 return;
1872 }
1873
1874 adapter->resetting = false;
1875 mutex_unlock(&adapter->reset_lock);
1876}
1877
1878static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1879 enum ibmvnic_reset_reason reason)
1880{
1881 struct ibmvnic_rwi *rwi, *tmp;
1882 struct net_device *netdev = adapter->netdev;
1883 struct list_head *entry;
1884
1885 if (adapter->state == VNIC_REMOVING ||
1886 adapter->state == VNIC_REMOVED) {
1887 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1888 return;
1889 }
1890
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001891 if (adapter->state == VNIC_PROBING) {
1892 netdev_warn(netdev, "Adapter reset during probe\n");
1893 adapter->init_done_rc = EAGAIN;
1894 return;
1895 }
1896
Nathan Fontenoted651a12017-05-03 14:04:38 -04001897 mutex_lock(&adapter->rwi_lock);
1898
1899 list_for_each(entry, &adapter->rwi_list) {
1900 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1901 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001902 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001903 mutex_unlock(&adapter->rwi_lock);
1904 return;
1905 }
1906 }
1907
1908 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1909 if (!rwi) {
1910 mutex_unlock(&adapter->rwi_lock);
1911 ibmvnic_close(netdev);
1912 return;
1913 }
1914
1915 rwi->reset_reason = reason;
1916 list_add_tail(&rwi->list, &adapter->rwi_list);
1917 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001918
1919 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001920 schedule_work(&adapter->ibmvnic_reset);
1921}
1922
Thomas Falcon032c5e82015-12-21 11:26:06 -06001923static void ibmvnic_tx_timeout(struct net_device *dev)
1924{
1925 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001926
Nathan Fontenoted651a12017-05-03 14:04:38 -04001927 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001928}
1929
1930static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1931 struct ibmvnic_rx_buff *rx_buff)
1932{
1933 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1934
1935 rx_buff->skb = NULL;
1936
1937 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1938 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1939
1940 atomic_dec(&pool->available);
1941}
1942
1943static int ibmvnic_poll(struct napi_struct *napi, int budget)
1944{
1945 struct net_device *netdev = napi->dev;
1946 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1947 int scrq_num = (int)(napi - adapter->napi);
1948 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001949
Thomas Falcon032c5e82015-12-21 11:26:06 -06001950restart_poll:
1951 while (frames_processed < budget) {
1952 struct sk_buff *skb;
1953 struct ibmvnic_rx_buff *rx_buff;
1954 union sub_crq *next;
1955 u32 length;
1956 u16 offset;
1957 u8 flags = 0;
1958
John Allen34686562018-02-06 16:21:49 -06001959 if (unlikely(adapter->resetting &&
1960 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001961 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1962 napi_complete_done(napi, frames_processed);
1963 return frames_processed;
1964 }
1965
Thomas Falcon032c5e82015-12-21 11:26:06 -06001966 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1967 break;
1968 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1969 rx_buff =
1970 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1971 rx_comp.correlator);
1972 /* do error checking */
1973 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001974 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1975 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001976 /* free the entry */
1977 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001978 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001979 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001980 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06001981 } else if (!rx_buff->skb) {
1982 /* free the entry */
1983 next->rx_comp.first = 0;
1984 remove_buff_from_pool(adapter, rx_buff);
1985 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001986 }
1987
1988 length = be32_to_cpu(next->rx_comp.len);
1989 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1990 flags = next->rx_comp.flags;
1991 skb = rx_buff->skb;
1992 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1993 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001994
1995 /* VLAN Header has been stripped by the system firmware and
1996 * needs to be inserted by the driver
1997 */
1998 if (adapter->rx_vlan_header_insertion &&
1999 (flags & IBMVNIC_VLAN_STRIPPED))
2000 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2001 ntohs(next->rx_comp.vlan_tci));
2002
Thomas Falcon032c5e82015-12-21 11:26:06 -06002003 /* free the entry */
2004 next->rx_comp.first = 0;
2005 remove_buff_from_pool(adapter, rx_buff);
2006
2007 skb_put(skb, length);
2008 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002009 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002010
2011 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2012 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2013 skb->ip_summed = CHECKSUM_UNNECESSARY;
2014 }
2015
2016 length = skb->len;
2017 napi_gro_receive(napi, skb); /* send it up */
2018 netdev->stats.rx_packets++;
2019 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002020 adapter->rx_stats_buffers[scrq_num].packets++;
2021 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002022 frames_processed++;
2023 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002024
2025 if (adapter->state != VNIC_CLOSING)
2026 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002027
2028 if (frames_processed < budget) {
2029 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002030 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002031 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2032 napi_reschedule(napi)) {
2033 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2034 goto restart_poll;
2035 }
2036 }
2037 return frames_processed;
2038}
2039
2040#ifdef CONFIG_NET_POLL_CONTROLLER
2041static void ibmvnic_netpoll_controller(struct net_device *dev)
2042{
2043 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2044 int i;
2045
2046 replenish_pools(netdev_priv(dev));
2047 for (i = 0; i < adapter->req_rx_queues; i++)
2048 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2049 adapter->rx_scrq[i]);
2050}
2051#endif
2052
John Allenc26eba02017-10-26 16:23:25 -05002053static int wait_for_reset(struct ibmvnic_adapter *adapter)
2054{
2055 adapter->fallback.mtu = adapter->req_mtu;
2056 adapter->fallback.rx_queues = adapter->req_rx_queues;
2057 adapter->fallback.tx_queues = adapter->req_tx_queues;
2058 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2059 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2060
2061 init_completion(&adapter->reset_done);
2062 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2063 adapter->wait_for_reset = true;
2064 wait_for_completion(&adapter->reset_done);
2065
2066 if (adapter->reset_done_rc) {
2067 adapter->desired.mtu = adapter->fallback.mtu;
2068 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2069 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2070 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2071 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2072
2073 init_completion(&adapter->reset_done);
2074 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2075 wait_for_completion(&adapter->reset_done);
2076 }
2077 adapter->wait_for_reset = false;
2078
2079 return adapter->reset_done_rc;
2080}
2081
John Allen3a807b72017-06-06 16:55:52 -05002082static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2083{
John Allenc26eba02017-10-26 16:23:25 -05002084 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2085
2086 adapter->desired.mtu = new_mtu + ETH_HLEN;
2087
2088 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002089}
2090
Thomas Falconf10b09e2018-03-12 11:51:05 -05002091static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2092 struct net_device *dev,
2093 netdev_features_t features)
2094{
2095 /* Some backing hardware adapters can not
2096 * handle packets with a MSS less than 224
2097 * or with only one segment.
2098 */
2099 if (skb_is_gso(skb)) {
2100 if (skb_shinfo(skb)->gso_size < 224 ||
2101 skb_shinfo(skb)->gso_segs == 1)
2102 features &= ~NETIF_F_GSO_MASK;
2103 }
2104
2105 return features;
2106}
2107
Thomas Falcon032c5e82015-12-21 11:26:06 -06002108static const struct net_device_ops ibmvnic_netdev_ops = {
2109 .ndo_open = ibmvnic_open,
2110 .ndo_stop = ibmvnic_close,
2111 .ndo_start_xmit = ibmvnic_xmit,
2112 .ndo_set_rx_mode = ibmvnic_set_multi,
2113 .ndo_set_mac_address = ibmvnic_set_mac,
2114 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002115 .ndo_tx_timeout = ibmvnic_tx_timeout,
2116#ifdef CONFIG_NET_POLL_CONTROLLER
2117 .ndo_poll_controller = ibmvnic_netpoll_controller,
2118#endif
John Allen3a807b72017-06-06 16:55:52 -05002119 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002120 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002121};
2122
2123/* ethtool functions */
2124
Philippe Reynes8a433792017-01-07 22:37:29 +01002125static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2126 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002127{
Philippe Reynes8a433792017-01-07 22:37:29 +01002128 u32 supported, advertising;
2129
2130 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002131 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002132 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002134 cmd->base.speed = SPEED_1000;
2135 cmd->base.duplex = DUPLEX_FULL;
2136 cmd->base.port = PORT_FIBRE;
2137 cmd->base.phy_address = 0;
2138 cmd->base.autoneg = AUTONEG_ENABLE;
2139
2140 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2141 supported);
2142 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2143 advertising);
2144
Thomas Falcon032c5e82015-12-21 11:26:06 -06002145 return 0;
2146}
2147
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002148static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002149 struct ethtool_drvinfo *info)
2150{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002151 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2152
Thomas Falcon032c5e82015-12-21 11:26:06 -06002153 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2154 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002155 strlcpy(info->fw_version, adapter->fw_version,
2156 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002157}
2158
2159static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2160{
2161 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2162
2163 return adapter->msg_enable;
2164}
2165
2166static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2167{
2168 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2169
2170 adapter->msg_enable = data;
2171}
2172
2173static u32 ibmvnic_get_link(struct net_device *netdev)
2174{
2175 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2176
2177 /* Don't need to send a query because we request a logical link up at
2178 * init and then we wait for link state indications
2179 */
2180 return adapter->logical_link_state;
2181}
2182
2183static void ibmvnic_get_ringparam(struct net_device *netdev,
2184 struct ethtool_ringparam *ring)
2185{
John Allenbc131b32017-08-02 16:46:30 -05002186 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2187
2188 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2189 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002190 ring->rx_mini_max_pending = 0;
2191 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002192 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2193 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002194 ring->rx_mini_pending = 0;
2195 ring->rx_jumbo_pending = 0;
2196}
2197
John Allenc26eba02017-10-26 16:23:25 -05002198static int ibmvnic_set_ringparam(struct net_device *netdev,
2199 struct ethtool_ringparam *ring)
2200{
2201 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2202
2203 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2204 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2205 netdev_err(netdev, "Invalid request.\n");
2206 netdev_err(netdev, "Max tx buffers = %llu\n",
2207 adapter->max_rx_add_entries_per_subcrq);
2208 netdev_err(netdev, "Max rx buffers = %llu\n",
2209 adapter->max_tx_entries_per_subcrq);
2210 return -EINVAL;
2211 }
2212
2213 adapter->desired.rx_entries = ring->rx_pending;
2214 adapter->desired.tx_entries = ring->tx_pending;
2215
2216 return wait_for_reset(adapter);
2217}
2218
John Allenc2dbeb62017-08-02 16:47:17 -05002219static void ibmvnic_get_channels(struct net_device *netdev,
2220 struct ethtool_channels *channels)
2221{
2222 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2223
2224 channels->max_rx = adapter->max_rx_queues;
2225 channels->max_tx = adapter->max_tx_queues;
2226 channels->max_other = 0;
2227 channels->max_combined = 0;
2228 channels->rx_count = adapter->req_rx_queues;
2229 channels->tx_count = adapter->req_tx_queues;
2230 channels->other_count = 0;
2231 channels->combined_count = 0;
2232}
2233
John Allenc26eba02017-10-26 16:23:25 -05002234static int ibmvnic_set_channels(struct net_device *netdev,
2235 struct ethtool_channels *channels)
2236{
2237 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2238
2239 adapter->desired.rx_queues = channels->rx_count;
2240 adapter->desired.tx_queues = channels->tx_count;
2241
2242 return wait_for_reset(adapter);
2243}
2244
Thomas Falcon032c5e82015-12-21 11:26:06 -06002245static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2246{
John Allen3d52b592017-08-02 16:44:14 -05002247 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002248 int i;
2249
2250 if (stringset != ETH_SS_STATS)
2251 return;
2252
2253 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2254 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002255
2256 for (i = 0; i < adapter->req_tx_queues; i++) {
2257 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2258 data += ETH_GSTRING_LEN;
2259
2260 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2261 data += ETH_GSTRING_LEN;
2262
2263 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2264 data += ETH_GSTRING_LEN;
2265 }
2266
2267 for (i = 0; i < adapter->req_rx_queues; i++) {
2268 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2269 data += ETH_GSTRING_LEN;
2270
2271 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2272 data += ETH_GSTRING_LEN;
2273
2274 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2275 data += ETH_GSTRING_LEN;
2276 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002277}
2278
2279static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2280{
John Allen3d52b592017-08-02 16:44:14 -05002281 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2282
Thomas Falcon032c5e82015-12-21 11:26:06 -06002283 switch (sset) {
2284 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002285 return ARRAY_SIZE(ibmvnic_stats) +
2286 adapter->req_tx_queues * NUM_TX_STATS +
2287 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002288 default:
2289 return -EOPNOTSUPP;
2290 }
2291}
2292
2293static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2294 struct ethtool_stats *stats, u64 *data)
2295{
2296 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2297 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002298 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002299
2300 memset(&crq, 0, sizeof(crq));
2301 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2302 crq.request_statistics.cmd = REQUEST_STATISTICS;
2303 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2304 crq.request_statistics.len =
2305 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002306
2307 /* Wait for data to be written */
2308 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002309 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002310 wait_for_completion(&adapter->stats_done);
2311
2312 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002313 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2314 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002315
2316 for (j = 0; j < adapter->req_tx_queues; j++) {
2317 data[i] = adapter->tx_stats_buffers[j].packets;
2318 i++;
2319 data[i] = adapter->tx_stats_buffers[j].bytes;
2320 i++;
2321 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2322 i++;
2323 }
2324
2325 for (j = 0; j < adapter->req_rx_queues; j++) {
2326 data[i] = adapter->rx_stats_buffers[j].packets;
2327 i++;
2328 data[i] = adapter->rx_stats_buffers[j].bytes;
2329 i++;
2330 data[i] = adapter->rx_stats_buffers[j].interrupts;
2331 i++;
2332 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002333}
2334
2335static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002336 .get_drvinfo = ibmvnic_get_drvinfo,
2337 .get_msglevel = ibmvnic_get_msglevel,
2338 .set_msglevel = ibmvnic_set_msglevel,
2339 .get_link = ibmvnic_get_link,
2340 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002341 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002342 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002343 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002344 .get_strings = ibmvnic_get_strings,
2345 .get_sset_count = ibmvnic_get_sset_count,
2346 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002347 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002348};
2349
2350/* Routines for managing CRQs/sCRQs */
2351
Nathan Fontenot57a49432017-05-26 10:31:12 -04002352static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2353 struct ibmvnic_sub_crq_queue *scrq)
2354{
2355 int rc;
2356
2357 if (scrq->irq) {
2358 free_irq(scrq->irq, scrq);
2359 irq_dispose_mapping(scrq->irq);
2360 scrq->irq = 0;
2361 }
2362
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002363 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002364 scrq->cur = 0;
2365
2366 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2367 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2368 return rc;
2369}
2370
2371static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2372{
2373 int i, rc;
2374
2375 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002376 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002377 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2378 if (rc)
2379 return rc;
2380 }
2381
2382 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002383 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002384 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2385 if (rc)
2386 return rc;
2387 }
2388
Nathan Fontenot57a49432017-05-26 10:31:12 -04002389 return rc;
2390}
2391
Thomas Falcon032c5e82015-12-21 11:26:06 -06002392static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002393 struct ibmvnic_sub_crq_queue *scrq,
2394 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002395{
2396 struct device *dev = &adapter->vdev->dev;
2397 long rc;
2398
2399 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2400
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002401 if (do_h_free) {
2402 /* Close the sub-crqs */
2403 do {
2404 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2405 adapter->vdev->unit_address,
2406 scrq->crq_num);
2407 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002408
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002409 if (rc) {
2410 netdev_err(adapter->netdev,
2411 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2412 scrq->crq_num, rc);
2413 }
Thomas Falconffa73852017-04-19 13:44:29 -04002414 }
2415
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2417 DMA_BIDIRECTIONAL);
2418 free_pages((unsigned long)scrq->msgs, 2);
2419 kfree(scrq);
2420}
2421
2422static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2423 *adapter)
2424{
2425 struct device *dev = &adapter->vdev->dev;
2426 struct ibmvnic_sub_crq_queue *scrq;
2427 int rc;
2428
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002429 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002430 if (!scrq)
2431 return NULL;
2432
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002433 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002434 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002435 if (!scrq->msgs) {
2436 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2437 goto zero_page_failed;
2438 }
2439
2440 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2441 DMA_BIDIRECTIONAL);
2442 if (dma_mapping_error(dev, scrq->msg_token)) {
2443 dev_warn(dev, "Couldn't map crq queue messages page\n");
2444 goto map_failed;
2445 }
2446
2447 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2448 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2449
2450 if (rc == H_RESOURCE)
2451 rc = ibmvnic_reset_crq(adapter);
2452
2453 if (rc == H_CLOSED) {
2454 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2455 } else if (rc) {
2456 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2457 goto reg_failed;
2458 }
2459
Thomas Falcon032c5e82015-12-21 11:26:06 -06002460 scrq->adapter = adapter;
2461 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002462 spin_lock_init(&scrq->lock);
2463
2464 netdev_dbg(adapter->netdev,
2465 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2466 scrq->crq_num, scrq->hw_irq, scrq->irq);
2467
2468 return scrq;
2469
Thomas Falcon032c5e82015-12-21 11:26:06 -06002470reg_failed:
2471 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2472 DMA_BIDIRECTIONAL);
2473map_failed:
2474 free_pages((unsigned long)scrq->msgs, 2);
2475zero_page_failed:
2476 kfree(scrq);
2477
2478 return NULL;
2479}
2480
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002481static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002482{
2483 int i;
2484
2485 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002486 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002487 if (!adapter->tx_scrq[i])
2488 continue;
2489
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002490 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2491 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002492 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002493 free_irq(adapter->tx_scrq[i]->irq,
2494 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002495 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002496 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002497 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002498
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002499 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2500 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002501 }
2502
Nathan Fontenot9501df32017-03-15 23:38:07 -04002503 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002504 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002505 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002506 }
2507
2508 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002509 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002510 if (!adapter->rx_scrq[i])
2511 continue;
2512
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002513 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2514 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002515 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002516 free_irq(adapter->rx_scrq[i]->irq,
2517 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002518 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002519 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002521
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002522 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2523 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002524 }
2525
Nathan Fontenot9501df32017-03-15 23:38:07 -04002526 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002528 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002529 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530}
2531
2532static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2533 struct ibmvnic_sub_crq_queue *scrq)
2534{
2535 struct device *dev = &adapter->vdev->dev;
2536 unsigned long rc;
2537
2538 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2539 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2540 if (rc)
2541 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2542 scrq->hw_irq, rc);
2543 return rc;
2544}
2545
2546static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2547 struct ibmvnic_sub_crq_queue *scrq)
2548{
2549 struct device *dev = &adapter->vdev->dev;
2550 unsigned long rc;
2551
2552 if (scrq->hw_irq > 0x100000000ULL) {
2553 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2554 return 1;
2555 }
2556
2557 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2558 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2559 if (rc)
2560 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2561 scrq->hw_irq, rc);
2562 return rc;
2563}
2564
2565static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2566 struct ibmvnic_sub_crq_queue *scrq)
2567{
2568 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05002569 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002570 struct ibmvnic_tx_buff *txbuff;
2571 union sub_crq *next;
2572 int index;
2573 int i, j;
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002574 u8 *first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002575
2576restart_loop:
2577 while (pending_scrq(adapter, scrq)) {
2578 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002579 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002580
2581 next = ibmvnic_next_scrq(adapter, scrq);
2582 for (i = 0; i < next->tx_comp.num_comps; i++) {
2583 if (next->tx_comp.rcs[i]) {
2584 dev_err(dev, "tx error %x\n",
2585 next->tx_comp.rcs[i]);
2586 continue;
2587 }
2588 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05002589 if (index & IBMVNIC_TSO_POOL_MASK) {
2590 tx_pool = &adapter->tso_pool[pool];
2591 index &= ~IBMVNIC_TSO_POOL_MASK;
2592 } else {
2593 tx_pool = &adapter->tx_pool[pool];
2594 }
2595
2596 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06002597
2598 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2599 if (!txbuff->data_dma[j])
2600 continue;
2601
2602 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002603 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002604 /* if sub_crq was sent indirectly */
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002605 first = &txbuff->indir_arr[0].generic.first;
2606 if (*first == IBMVNIC_CRQ_CMD) {
Thomas Falconad7775d2016-04-01 17:20:34 -05002607 dma_unmap_single(dev, txbuff->indir_dma,
2608 sizeof(txbuff->indir_arr),
2609 DMA_TO_DEVICE);
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002610 *first = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05002611 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002612
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002613 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002615 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002616 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002617
Thomas Falconffc385b2018-02-18 10:08:41 -06002618 num_entries += txbuff->num_entries;
2619
Thomas Falcon06b3e352018-03-16 20:00:28 -05002620 tx_pool->free_map[tx_pool->producer_index] = index;
2621 tx_pool->producer_index =
2622 (tx_pool->producer_index + 1) %
2623 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002624 }
2625 /* remove tx_comp scrq*/
2626 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002627
Thomas Falconffc385b2018-02-18 10:08:41 -06002628 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002629 (adapter->req_tx_entries_per_subcrq / 2) &&
2630 __netif_subqueue_stopped(adapter->netdev,
2631 scrq->pool_index)) {
2632 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002633 netdev_dbg(adapter->netdev, "Started queue %d\n",
2634 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002635 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002636 }
2637
2638 enable_scrq_irq(adapter, scrq);
2639
2640 if (pending_scrq(adapter, scrq)) {
2641 disable_scrq_irq(adapter, scrq);
2642 goto restart_loop;
2643 }
2644
2645 return 0;
2646}
2647
2648static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2649{
2650 struct ibmvnic_sub_crq_queue *scrq = instance;
2651 struct ibmvnic_adapter *adapter = scrq->adapter;
2652
2653 disable_scrq_irq(adapter, scrq);
2654 ibmvnic_complete_tx(adapter, scrq);
2655
2656 return IRQ_HANDLED;
2657}
2658
2659static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2660{
2661 struct ibmvnic_sub_crq_queue *scrq = instance;
2662 struct ibmvnic_adapter *adapter = scrq->adapter;
2663
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002664 /* When booting a kdump kernel we can hit pending interrupts
2665 * prior to completing driver initialization.
2666 */
2667 if (unlikely(adapter->state != VNIC_OPEN))
2668 return IRQ_NONE;
2669
John Allen3d52b592017-08-02 16:44:14 -05002670 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2671
Thomas Falcon032c5e82015-12-21 11:26:06 -06002672 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2673 disable_scrq_irq(adapter, scrq);
2674 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2675 }
2676
2677 return IRQ_HANDLED;
2678}
2679
Thomas Falconea22d512016-07-06 15:35:17 -05002680static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2681{
2682 struct device *dev = &adapter->vdev->dev;
2683 struct ibmvnic_sub_crq_queue *scrq;
2684 int i = 0, j = 0;
2685 int rc = 0;
2686
2687 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002688 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2689 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002690 scrq = adapter->tx_scrq[i];
2691 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2692
Michael Ellerman99c17902016-09-10 19:59:05 +10002693 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002694 rc = -EINVAL;
2695 dev_err(dev, "Error mapping irq\n");
2696 goto req_tx_irq_failed;
2697 }
2698
2699 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2700 0, "ibmvnic_tx", scrq);
2701
2702 if (rc) {
2703 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2704 scrq->irq, rc);
2705 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002706 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002707 }
2708 }
2709
2710 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002711 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2712 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002713 scrq = adapter->rx_scrq[i];
2714 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002715 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002716 rc = -EINVAL;
2717 dev_err(dev, "Error mapping irq\n");
2718 goto req_rx_irq_failed;
2719 }
2720 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2721 0, "ibmvnic_rx", scrq);
2722 if (rc) {
2723 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2724 scrq->irq, rc);
2725 irq_dispose_mapping(scrq->irq);
2726 goto req_rx_irq_failed;
2727 }
2728 }
2729 return rc;
2730
2731req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002732 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002733 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2734 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002735 }
Thomas Falconea22d512016-07-06 15:35:17 -05002736 i = adapter->req_tx_queues;
2737req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002738 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002739 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2740 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002741 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002742 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002743 return rc;
2744}
2745
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002746static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002747{
2748 struct device *dev = &adapter->vdev->dev;
2749 struct ibmvnic_sub_crq_queue **allqueues;
2750 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 int total_queues;
2752 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002753 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002754
Thomas Falcon032c5e82015-12-21 11:26:06 -06002755 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2756
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002757 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002759 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002760
2761 for (i = 0; i < total_queues; i++) {
2762 allqueues[i] = init_sub_crq_queue(adapter);
2763 if (!allqueues[i]) {
2764 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2765 break;
2766 }
2767 registered_queues++;
2768 }
2769
2770 /* Make sure we were able to register the minimum number of queues */
2771 if (registered_queues <
2772 adapter->min_tx_queues + adapter->min_rx_queues) {
2773 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2774 goto tx_failed;
2775 }
2776
2777 /* Distribute the failed allocated queues*/
2778 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2779 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2780 switch (i % 3) {
2781 case 0:
2782 if (adapter->req_rx_queues > adapter->min_rx_queues)
2783 adapter->req_rx_queues--;
2784 else
2785 more++;
2786 break;
2787 case 1:
2788 if (adapter->req_tx_queues > adapter->min_tx_queues)
2789 adapter->req_tx_queues--;
2790 else
2791 more++;
2792 break;
2793 }
2794 }
2795
2796 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002797 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002798 if (!adapter->tx_scrq)
2799 goto tx_failed;
2800
2801 for (i = 0; i < adapter->req_tx_queues; i++) {
2802 adapter->tx_scrq[i] = allqueues[i];
2803 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002804 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 }
2806
2807 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002808 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809 if (!adapter->rx_scrq)
2810 goto rx_failed;
2811
2812 for (i = 0; i < adapter->req_rx_queues; i++) {
2813 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2814 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002815 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002816 }
2817
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002818 kfree(allqueues);
2819 return 0;
2820
2821rx_failed:
2822 kfree(adapter->tx_scrq);
2823 adapter->tx_scrq = NULL;
2824tx_failed:
2825 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002826 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002827 kfree(allqueues);
2828 return -1;
2829}
2830
2831static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2832{
2833 struct device *dev = &adapter->vdev->dev;
2834 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002835 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002836
2837 if (!retry) {
2838 /* Sub-CRQ entries are 32 byte long */
2839 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2840
2841 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2842 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2843 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2844 return;
2845 }
2846
John Allenc26eba02017-10-26 16:23:25 -05002847 if (adapter->desired.mtu)
2848 adapter->req_mtu = adapter->desired.mtu;
2849 else
2850 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002851
John Allenc26eba02017-10-26 16:23:25 -05002852 if (!adapter->desired.tx_entries)
2853 adapter->desired.tx_entries =
2854 adapter->max_tx_entries_per_subcrq;
2855 if (!adapter->desired.rx_entries)
2856 adapter->desired.rx_entries =
2857 adapter->max_rx_add_entries_per_subcrq;
2858
2859 max_entries = IBMVNIC_MAX_LTB_SIZE /
2860 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2861
2862 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2863 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2864 adapter->desired.tx_entries = max_entries;
2865 }
2866
2867 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2868 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2869 adapter->desired.rx_entries = max_entries;
2870 }
2871
2872 if (adapter->desired.tx_entries)
2873 adapter->req_tx_entries_per_subcrq =
2874 adapter->desired.tx_entries;
2875 else
2876 adapter->req_tx_entries_per_subcrq =
2877 adapter->max_tx_entries_per_subcrq;
2878
2879 if (adapter->desired.rx_entries)
2880 adapter->req_rx_add_entries_per_subcrq =
2881 adapter->desired.rx_entries;
2882 else
2883 adapter->req_rx_add_entries_per_subcrq =
2884 adapter->max_rx_add_entries_per_subcrq;
2885
2886 if (adapter->desired.tx_queues)
2887 adapter->req_tx_queues =
2888 adapter->desired.tx_queues;
2889 else
2890 adapter->req_tx_queues =
2891 adapter->opt_tx_comp_sub_queues;
2892
2893 if (adapter->desired.rx_queues)
2894 adapter->req_rx_queues =
2895 adapter->desired.rx_queues;
2896 else
2897 adapter->req_rx_queues =
2898 adapter->opt_rx_comp_queues;
2899
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002900 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002901 }
2902
Thomas Falcon032c5e82015-12-21 11:26:06 -06002903 memset(&crq, 0, sizeof(crq));
2904 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2905 crq.request_capability.cmd = REQUEST_CAPABILITY;
2906
2907 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002908 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002909 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002910 ibmvnic_send_crq(adapter, &crq);
2911
2912 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002913 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002914 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002915 ibmvnic_send_crq(adapter, &crq);
2916
2917 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002918 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002919 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002920 ibmvnic_send_crq(adapter, &crq);
2921
2922 crq.request_capability.capability =
2923 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2924 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002925 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002926 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002927 ibmvnic_send_crq(adapter, &crq);
2928
2929 crq.request_capability.capability =
2930 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2931 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002932 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002933 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002934 ibmvnic_send_crq(adapter, &crq);
2935
2936 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002937 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002938 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002939 ibmvnic_send_crq(adapter, &crq);
2940
2941 if (adapter->netdev->flags & IFF_PROMISC) {
2942 if (adapter->promisc_supported) {
2943 crq.request_capability.capability =
2944 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002945 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002946 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002947 ibmvnic_send_crq(adapter, &crq);
2948 }
2949 } else {
2950 crq.request_capability.capability =
2951 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002952 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002953 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002954 ibmvnic_send_crq(adapter, &crq);
2955 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002956}
2957
2958static int pending_scrq(struct ibmvnic_adapter *adapter,
2959 struct ibmvnic_sub_crq_queue *scrq)
2960{
2961 union sub_crq *entry = &scrq->msgs[scrq->cur];
2962
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002963 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002964 return 1;
2965 else
2966 return 0;
2967}
2968
2969static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2970 struct ibmvnic_sub_crq_queue *scrq)
2971{
2972 union sub_crq *entry;
2973 unsigned long flags;
2974
2975 spin_lock_irqsave(&scrq->lock, flags);
2976 entry = &scrq->msgs[scrq->cur];
2977 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2978 if (++scrq->cur == scrq->size)
2979 scrq->cur = 0;
2980 } else {
2981 entry = NULL;
2982 }
2983 spin_unlock_irqrestore(&scrq->lock, flags);
2984
2985 return entry;
2986}
2987
2988static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2989{
2990 struct ibmvnic_crq_queue *queue = &adapter->crq;
2991 union ibmvnic_crq *crq;
2992
2993 crq = &queue->msgs[queue->cur];
2994 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2995 if (++queue->cur == queue->size)
2996 queue->cur = 0;
2997 } else {
2998 crq = NULL;
2999 }
3000
3001 return crq;
3002}
3003
3004static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3005 union sub_crq *sub_crq)
3006{
3007 unsigned int ua = adapter->vdev->unit_address;
3008 struct device *dev = &adapter->vdev->dev;
3009 u64 *u64_crq = (u64 *)sub_crq;
3010 int rc;
3011
3012 netdev_dbg(adapter->netdev,
3013 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3014 (unsigned long int)cpu_to_be64(remote_handle),
3015 (unsigned long int)cpu_to_be64(u64_crq[0]),
3016 (unsigned long int)cpu_to_be64(u64_crq[1]),
3017 (unsigned long int)cpu_to_be64(u64_crq[2]),
3018 (unsigned long int)cpu_to_be64(u64_crq[3]));
3019
3020 /* Make sure the hypervisor sees the complete request */
3021 mb();
3022
3023 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3024 cpu_to_be64(remote_handle),
3025 cpu_to_be64(u64_crq[0]),
3026 cpu_to_be64(u64_crq[1]),
3027 cpu_to_be64(u64_crq[2]),
3028 cpu_to_be64(u64_crq[3]));
3029
3030 if (rc) {
3031 if (rc == H_CLOSED)
3032 dev_warn(dev, "CRQ Queue closed\n");
3033 dev_err(dev, "Send error (rc=%d)\n", rc);
3034 }
3035
3036 return rc;
3037}
3038
Thomas Falconad7775d2016-04-01 17:20:34 -05003039static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3040 u64 remote_handle, u64 ioba, u64 num_entries)
3041{
3042 unsigned int ua = adapter->vdev->unit_address;
3043 struct device *dev = &adapter->vdev->dev;
3044 int rc;
3045
3046 /* Make sure the hypervisor sees the complete request */
3047 mb();
3048 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3049 cpu_to_be64(remote_handle),
3050 ioba, num_entries);
3051
3052 if (rc) {
3053 if (rc == H_CLOSED)
3054 dev_warn(dev, "CRQ Queue closed\n");
3055 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3056 }
3057
3058 return rc;
3059}
3060
Thomas Falcon032c5e82015-12-21 11:26:06 -06003061static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3062 union ibmvnic_crq *crq)
3063{
3064 unsigned int ua = adapter->vdev->unit_address;
3065 struct device *dev = &adapter->vdev->dev;
3066 u64 *u64_crq = (u64 *)crq;
3067 int rc;
3068
3069 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3070 (unsigned long int)cpu_to_be64(u64_crq[0]),
3071 (unsigned long int)cpu_to_be64(u64_crq[1]));
3072
3073 /* Make sure the hypervisor sees the complete request */
3074 mb();
3075
3076 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3077 cpu_to_be64(u64_crq[0]),
3078 cpu_to_be64(u64_crq[1]));
3079
3080 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003081 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003083 if (adapter->resetting)
3084 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3085 }
3086
Thomas Falcon032c5e82015-12-21 11:26:06 -06003087 dev_warn(dev, "Send error (rc=%d)\n", rc);
3088 }
3089
3090 return rc;
3091}
3092
3093static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3094{
3095 union ibmvnic_crq crq;
3096
3097 memset(&crq, 0, sizeof(crq));
3098 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3099 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3100 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3101
3102 return ibmvnic_send_crq(adapter, &crq);
3103}
3104
Thomas Falcon032c5e82015-12-21 11:26:06 -06003105static int send_version_xchg(struct ibmvnic_adapter *adapter)
3106{
3107 union ibmvnic_crq crq;
3108
3109 memset(&crq, 0, sizeof(crq));
3110 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3111 crq.version_exchange.cmd = VERSION_EXCHANGE;
3112 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3113
3114 return ibmvnic_send_crq(adapter, &crq);
3115}
3116
Nathan Fontenot37798d02017-11-08 11:23:56 -06003117struct vnic_login_client_data {
3118 u8 type;
3119 __be16 len;
3120 char name;
3121} __packed;
3122
3123static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3124{
3125 int len;
3126
3127 /* Calculate the amount of buffer space needed for the
3128 * vnic client data in the login buffer. There are four entries,
3129 * OS name, LPAR name, device name, and a null last entry.
3130 */
3131 len = 4 * sizeof(struct vnic_login_client_data);
3132 len += 6; /* "Linux" plus NULL */
3133 len += strlen(utsname()->nodename) + 1;
3134 len += strlen(adapter->netdev->name) + 1;
3135
3136 return len;
3137}
3138
3139static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3140 struct vnic_login_client_data *vlcd)
3141{
3142 const char *os_name = "Linux";
3143 int len;
3144
3145 /* Type 1 - LPAR OS */
3146 vlcd->type = 1;
3147 len = strlen(os_name) + 1;
3148 vlcd->len = cpu_to_be16(len);
3149 strncpy(&vlcd->name, os_name, len);
3150 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3151
3152 /* Type 2 - LPAR name */
3153 vlcd->type = 2;
3154 len = strlen(utsname()->nodename) + 1;
3155 vlcd->len = cpu_to_be16(len);
3156 strncpy(&vlcd->name, utsname()->nodename, len);
3157 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3158
3159 /* Type 3 - device name */
3160 vlcd->type = 3;
3161 len = strlen(adapter->netdev->name) + 1;
3162 vlcd->len = cpu_to_be16(len);
3163 strncpy(&vlcd->name, adapter->netdev->name, len);
3164}
3165
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003166static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003167{
3168 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3169 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003170 struct device *dev = &adapter->vdev->dev;
3171 dma_addr_t rsp_buffer_token;
3172 dma_addr_t buffer_token;
3173 size_t rsp_buffer_size;
3174 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003175 size_t buffer_size;
3176 __be64 *tx_list_p;
3177 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003178 int client_data_len;
3179 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003180 int i;
3181
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003182 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3183 netdev_err(adapter->netdev,
3184 "RX or TX queues are not allocated, device login failed\n");
3185 return -1;
3186 }
3187
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003188 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003189 client_data_len = vnic_client_data_len(adapter);
3190
Thomas Falcon032c5e82015-12-21 11:26:06 -06003191 buffer_size =
3192 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003193 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3194 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003195
Nathan Fontenot37798d02017-11-08 11:23:56 -06003196 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003197 if (!login_buffer)
3198 goto buf_alloc_failed;
3199
3200 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3201 DMA_TO_DEVICE);
3202 if (dma_mapping_error(dev, buffer_token)) {
3203 dev_err(dev, "Couldn't map login buffer\n");
3204 goto buf_map_failed;
3205 }
3206
John Allen498cd8e2016-04-06 11:49:55 -05003207 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3208 sizeof(u64) * adapter->req_tx_queues +
3209 sizeof(u64) * adapter->req_rx_queues +
3210 sizeof(u64) * adapter->req_rx_queues +
3211 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003212
3213 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3214 if (!login_rsp_buffer)
3215 goto buf_rsp_alloc_failed;
3216
3217 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3218 rsp_buffer_size, DMA_FROM_DEVICE);
3219 if (dma_mapping_error(dev, rsp_buffer_token)) {
3220 dev_err(dev, "Couldn't map login rsp buffer\n");
3221 goto buf_rsp_map_failed;
3222 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003223
Thomas Falcon032c5e82015-12-21 11:26:06 -06003224 adapter->login_buf = login_buffer;
3225 adapter->login_buf_token = buffer_token;
3226 adapter->login_buf_sz = buffer_size;
3227 adapter->login_rsp_buf = login_rsp_buffer;
3228 adapter->login_rsp_buf_token = rsp_buffer_token;
3229 adapter->login_rsp_buf_sz = rsp_buffer_size;
3230
3231 login_buffer->len = cpu_to_be32(buffer_size);
3232 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3233 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3234 login_buffer->off_txcomp_subcrqs =
3235 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3236 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3237 login_buffer->off_rxcomp_subcrqs =
3238 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3239 sizeof(u64) * adapter->req_tx_queues);
3240 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3241 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3242
3243 tx_list_p = (__be64 *)((char *)login_buffer +
3244 sizeof(struct ibmvnic_login_buffer));
3245 rx_list_p = (__be64 *)((char *)login_buffer +
3246 sizeof(struct ibmvnic_login_buffer) +
3247 sizeof(u64) * adapter->req_tx_queues);
3248
3249 for (i = 0; i < adapter->req_tx_queues; i++) {
3250 if (adapter->tx_scrq[i]) {
3251 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3252 crq_num);
3253 }
3254 }
3255
3256 for (i = 0; i < adapter->req_rx_queues; i++) {
3257 if (adapter->rx_scrq[i]) {
3258 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3259 crq_num);
3260 }
3261 }
3262
Nathan Fontenot37798d02017-11-08 11:23:56 -06003263 /* Insert vNIC login client data */
3264 vlcd = (struct vnic_login_client_data *)
3265 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3266 login_buffer->client_data_offset =
3267 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3268 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3269
3270 vnic_add_client_data(adapter, vlcd);
3271
Thomas Falcon032c5e82015-12-21 11:26:06 -06003272 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3273 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3274 netdev_dbg(adapter->netdev, "%016lx\n",
3275 ((unsigned long int *)(adapter->login_buf))[i]);
3276 }
3277
3278 memset(&crq, 0, sizeof(crq));
3279 crq.login.first = IBMVNIC_CRQ_CMD;
3280 crq.login.cmd = LOGIN;
3281 crq.login.ioba = cpu_to_be32(buffer_token);
3282 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003283 ibmvnic_send_crq(adapter, &crq);
3284
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003285 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003286
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287buf_rsp_map_failed:
3288 kfree(login_rsp_buffer);
3289buf_rsp_alloc_failed:
3290 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3291buf_map_failed:
3292 kfree(login_buffer);
3293buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003294 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003295}
3296
3297static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3298 u32 len, u8 map_id)
3299{
3300 union ibmvnic_crq crq;
3301
3302 memset(&crq, 0, sizeof(crq));
3303 crq.request_map.first = IBMVNIC_CRQ_CMD;
3304 crq.request_map.cmd = REQUEST_MAP;
3305 crq.request_map.map_id = map_id;
3306 crq.request_map.ioba = cpu_to_be32(addr);
3307 crq.request_map.len = cpu_to_be32(len);
3308 ibmvnic_send_crq(adapter, &crq);
3309}
3310
3311static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3312{
3313 union ibmvnic_crq crq;
3314
3315 memset(&crq, 0, sizeof(crq));
3316 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3317 crq.request_unmap.cmd = REQUEST_UNMAP;
3318 crq.request_unmap.map_id = map_id;
3319 ibmvnic_send_crq(adapter, &crq);
3320}
3321
3322static void send_map_query(struct ibmvnic_adapter *adapter)
3323{
3324 union ibmvnic_crq crq;
3325
3326 memset(&crq, 0, sizeof(crq));
3327 crq.query_map.first = IBMVNIC_CRQ_CMD;
3328 crq.query_map.cmd = QUERY_MAP;
3329 ibmvnic_send_crq(adapter, &crq);
3330}
3331
3332/* Send a series of CRQs requesting various capabilities of the VNIC server */
3333static void send_cap_queries(struct ibmvnic_adapter *adapter)
3334{
3335 union ibmvnic_crq crq;
3336
Thomas Falcon901e0402017-02-15 12:17:59 -06003337 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003338 memset(&crq, 0, sizeof(crq));
3339 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3340 crq.query_capability.cmd = QUERY_CAPABILITY;
3341
3342 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003343 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344 ibmvnic_send_crq(adapter, &crq);
3345
3346 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003347 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348 ibmvnic_send_crq(adapter, &crq);
3349
3350 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003351 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352 ibmvnic_send_crq(adapter, &crq);
3353
3354 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003355 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003356 ibmvnic_send_crq(adapter, &crq);
3357
3358 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003359 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003360 ibmvnic_send_crq(adapter, &crq);
3361
3362 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003363 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003364 ibmvnic_send_crq(adapter, &crq);
3365
3366 crq.query_capability.capability =
3367 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003368 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003369 ibmvnic_send_crq(adapter, &crq);
3370
3371 crq.query_capability.capability =
3372 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003373 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003374 ibmvnic_send_crq(adapter, &crq);
3375
3376 crq.query_capability.capability =
3377 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003378 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003379 ibmvnic_send_crq(adapter, &crq);
3380
3381 crq.query_capability.capability =
3382 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003383 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003384 ibmvnic_send_crq(adapter, &crq);
3385
3386 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003387 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003388 ibmvnic_send_crq(adapter, &crq);
3389
3390 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003391 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003392 ibmvnic_send_crq(adapter, &crq);
3393
3394 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003395 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003396 ibmvnic_send_crq(adapter, &crq);
3397
3398 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003399 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003400 ibmvnic_send_crq(adapter, &crq);
3401
3402 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003403 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003404 ibmvnic_send_crq(adapter, &crq);
3405
3406 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003407 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003408 ibmvnic_send_crq(adapter, &crq);
3409
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003410 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3411 atomic_inc(&adapter->running_cap_crqs);
3412 ibmvnic_send_crq(adapter, &crq);
3413
Thomas Falcon032c5e82015-12-21 11:26:06 -06003414 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003415 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003416 ibmvnic_send_crq(adapter, &crq);
3417
3418 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003419 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003420 ibmvnic_send_crq(adapter, &crq);
3421
3422 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003423 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003424 ibmvnic_send_crq(adapter, &crq);
3425
3426 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003427 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003428 ibmvnic_send_crq(adapter, &crq);
3429
3430 crq.query_capability.capability =
3431 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003432 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433 ibmvnic_send_crq(adapter, &crq);
3434
3435 crq.query_capability.capability =
3436 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003437 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003438 ibmvnic_send_crq(adapter, &crq);
3439
3440 crq.query_capability.capability =
3441 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003442 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443 ibmvnic_send_crq(adapter, &crq);
3444
3445 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003446 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447 ibmvnic_send_crq(adapter, &crq);
3448}
3449
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003450static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3451 struct ibmvnic_adapter *adapter)
3452{
3453 struct device *dev = &adapter->vdev->dev;
3454
3455 if (crq->get_vpd_size_rsp.rc.code) {
3456 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3457 crq->get_vpd_size_rsp.rc.code);
3458 complete(&adapter->fw_done);
3459 return;
3460 }
3461
3462 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3463 complete(&adapter->fw_done);
3464}
3465
3466static void handle_vpd_rsp(union ibmvnic_crq *crq,
3467 struct ibmvnic_adapter *adapter)
3468{
3469 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003470 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003471 u8 fw_level_len = 0;
3472
3473 memset(adapter->fw_version, 0, 32);
3474
3475 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3476 DMA_FROM_DEVICE);
3477
3478 if (crq->get_vpd_rsp.rc.code) {
3479 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3480 crq->get_vpd_rsp.rc.code);
3481 goto complete;
3482 }
3483
3484 /* get the position of the firmware version info
3485 * located after the ASCII 'RM' substring in the buffer
3486 */
3487 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3488 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003489 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003490 goto complete;
3491 }
3492
3493 /* get length of firmware level ASCII substring */
3494 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3495 fw_level_len = *(substr + 2);
3496 } else {
3497 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3498 goto complete;
3499 }
3500
3501 /* copy firmware version string from vpd into adapter */
3502 if ((substr + 3 + fw_level_len) <
3503 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003504 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003505 } else {
3506 dev_info(dev, "FW substr extrapolated VPD buff\n");
3507 }
3508
3509complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003510 if (adapter->fw_version[0] == '\0')
3511 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003512 complete(&adapter->fw_done);
3513}
3514
Thomas Falcon032c5e82015-12-21 11:26:06 -06003515static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3516{
3517 struct device *dev = &adapter->vdev->dev;
3518 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3519 union ibmvnic_crq crq;
3520 int i;
3521
3522 dma_unmap_single(dev, adapter->ip_offload_tok,
3523 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3524
3525 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3526 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3527 netdev_dbg(adapter->netdev, "%016lx\n",
3528 ((unsigned long int *)(buf))[i]);
3529
3530 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3531 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3532 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3533 buf->tcp_ipv4_chksum);
3534 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3535 buf->tcp_ipv6_chksum);
3536 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3537 buf->udp_ipv4_chksum);
3538 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3539 buf->udp_ipv6_chksum);
3540 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3541 buf->large_tx_ipv4);
3542 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3543 buf->large_tx_ipv6);
3544 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3545 buf->large_rx_ipv4);
3546 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3547 buf->large_rx_ipv6);
3548 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3549 buf->max_ipv4_header_size);
3550 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3551 buf->max_ipv6_header_size);
3552 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3553 buf->max_tcp_header_size);
3554 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3555 buf->max_udp_header_size);
3556 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3557 buf->max_large_tx_size);
3558 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3559 buf->max_large_rx_size);
3560 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3561 buf->ipv6_extension_header);
3562 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3563 buf->tcp_pseudosum_req);
3564 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3565 buf->num_ipv6_ext_headers);
3566 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3567 buf->off_ipv6_ext_headers);
3568
3569 adapter->ip_offload_ctrl_tok =
3570 dma_map_single(dev, &adapter->ip_offload_ctrl,
3571 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3572
3573 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3574 dev_err(dev, "Couldn't map ip offload control buffer\n");
3575 return;
3576 }
3577
Thomas Falconf6897942018-01-18 19:05:01 -06003578 adapter->ip_offload_ctrl.len =
3579 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003580 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003581 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3582 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3584 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3585 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3586 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003587 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3588 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589
Thomas Falconfdb06102017-10-17 12:36:55 -05003590 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3592 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3593
Thomas Falcon15482052017-10-17 12:36:54 -05003594 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595
3596 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3597 adapter->netdev->features |= NETIF_F_IP_CSUM;
3598
3599 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3600 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3601
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003602 if ((adapter->netdev->features &
3603 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3604 adapter->netdev->features |= NETIF_F_RXCSUM;
3605
Thomas Falconfdb06102017-10-17 12:36:55 -05003606 if (buf->large_tx_ipv4)
3607 adapter->netdev->features |= NETIF_F_TSO;
3608 if (buf->large_tx_ipv6)
3609 adapter->netdev->features |= NETIF_F_TSO6;
3610
Thomas Falconaa0bf852017-10-17 12:36:56 -05003611 adapter->netdev->hw_features |= adapter->netdev->features;
3612
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613 memset(&crq, 0, sizeof(crq));
3614 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3615 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3616 crq.control_ip_offload.len =
3617 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3618 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3619 ibmvnic_send_crq(adapter, &crq);
3620}
3621
3622static void handle_error_info_rsp(union ibmvnic_crq *crq,
3623 struct ibmvnic_adapter *adapter)
3624{
3625 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003626 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627 unsigned long flags;
3628 bool found = false;
3629 int i;
3630
3631 if (!crq->request_error_rsp.rc.code) {
3632 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3633 crq->request_error_rsp.rc.code);
3634 return;
3635 }
3636
3637 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003638 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3640 found = true;
3641 list_del(&error_buff->list);
3642 break;
3643 }
3644 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3645
3646 if (!found) {
3647 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003648 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003649 return;
3650 }
3651
3652 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003653 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003654
3655 for (i = 0; i < error_buff->len; i++) {
3656 pr_cont("%02x", (int)error_buff->buff[i]);
3657 if (i % 8 == 7)
3658 pr_cont(" ");
3659 }
3660 pr_cont("\n");
3661
3662 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3663 DMA_FROM_DEVICE);
3664 kfree(error_buff->buff);
3665 kfree(error_buff);
3666}
3667
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003668static void request_error_information(struct ibmvnic_adapter *adapter,
3669 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003670{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003672 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003673 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003674 unsigned long timeout = msecs_to_jiffies(30000);
3675 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003676 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003677 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003678
3679 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3680 if (!error_buff)
3681 return;
3682
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003683 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003684 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3685 if (!error_buff->buff) {
3686 kfree(error_buff);
3687 return;
3688 }
3689
3690 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3691 DMA_FROM_DEVICE);
3692 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003693 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003694 kfree(error_buff->buff);
3695 kfree(error_buff);
3696 return;
3697 }
3698
Thomas Falcon032c5e82015-12-21 11:26:06 -06003699 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003700 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003701
3702 spin_lock_irqsave(&adapter->error_list_lock, flags);
3703 list_add_tail(&error_buff->list, &adapter->errors);
3704 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3705
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003706 memset(&crq, 0, sizeof(crq));
3707 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3708 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3709 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3710 crq.request_error_info.len = cpu_to_be32(detail_len);
3711 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3712
3713 rc = ibmvnic_send_crq(adapter, &crq);
3714 if (rc) {
3715 netdev_err(netdev, "failed to request error information\n");
3716 goto err_info_fail;
3717 }
3718
3719 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3720 netdev_err(netdev, "timeout waiting for error information\n");
3721 goto err_info_fail;
3722 }
3723
3724 return;
3725
3726err_info_fail:
3727 spin_lock_irqsave(&adapter->error_list_lock, flags);
3728 list_del(&error_buff->list);
3729 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3730
3731 kfree(error_buff->buff);
3732 kfree(error_buff);
3733}
3734
3735static void handle_error_indication(union ibmvnic_crq *crq,
3736 struct ibmvnic_adapter *adapter)
3737{
3738 struct device *dev = &adapter->vdev->dev;
3739
3740 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3741 crq->error_indication.flags
3742 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3743 be32_to_cpu(crq->error_indication.error_id),
3744 be16_to_cpu(crq->error_indication.error_cause));
3745
3746 if (be32_to_cpu(crq->error_indication.error_id))
3747 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003748
3749 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3750 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003751 else
3752 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003753}
3754
Thomas Falconf8136142018-01-29 13:45:05 -06003755static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3756 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003757{
3758 struct net_device *netdev = adapter->netdev;
3759 struct device *dev = &adapter->vdev->dev;
3760 long rc;
3761
3762 rc = crq->change_mac_addr_rsp.rc.code;
3763 if (rc) {
3764 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003765 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003766 }
3767 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3768 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003769out:
3770 complete(&adapter->fw_done);
3771 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003772}
3773
3774static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3775 struct ibmvnic_adapter *adapter)
3776{
3777 struct device *dev = &adapter->vdev->dev;
3778 u64 *req_value;
3779 char *name;
3780
Thomas Falcon901e0402017-02-15 12:17:59 -06003781 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003782 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3783 case REQ_TX_QUEUES:
3784 req_value = &adapter->req_tx_queues;
3785 name = "tx";
3786 break;
3787 case REQ_RX_QUEUES:
3788 req_value = &adapter->req_rx_queues;
3789 name = "rx";
3790 break;
3791 case REQ_RX_ADD_QUEUES:
3792 req_value = &adapter->req_rx_add_queues;
3793 name = "rx_add";
3794 break;
3795 case REQ_TX_ENTRIES_PER_SUBCRQ:
3796 req_value = &adapter->req_tx_entries_per_subcrq;
3797 name = "tx_entries_per_subcrq";
3798 break;
3799 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3800 req_value = &adapter->req_rx_add_entries_per_subcrq;
3801 name = "rx_add_entries_per_subcrq";
3802 break;
3803 case REQ_MTU:
3804 req_value = &adapter->req_mtu;
3805 name = "mtu";
3806 break;
3807 case PROMISC_REQUESTED:
3808 req_value = &adapter->promisc;
3809 name = "promisc";
3810 break;
3811 default:
3812 dev_err(dev, "Got invalid cap request rsp %d\n",
3813 crq->request_capability.capability);
3814 return;
3815 }
3816
3817 switch (crq->request_capability_rsp.rc.code) {
3818 case SUCCESS:
3819 break;
3820 case PARTIALSUCCESS:
3821 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3822 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003823 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003824 number), name);
John Allene7913802018-01-18 16:27:12 -06003825
3826 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3827 REQ_MTU) {
3828 pr_err("mtu of %llu is not supported. Reverting.\n",
3829 *req_value);
3830 *req_value = adapter->fallback.mtu;
3831 } else {
3832 *req_value =
3833 be64_to_cpu(crq->request_capability_rsp.number);
3834 }
3835
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003836 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003837 return;
3838 default:
3839 dev_err(dev, "Error %d in request cap rsp\n",
3840 crq->request_capability_rsp.rc.code);
3841 return;
3842 }
3843
3844 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003845 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003846 union ibmvnic_crq newcrq;
3847 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3848 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3849 &adapter->ip_offload_buf;
3850
Thomas Falcon249168a2017-02-15 12:18:00 -06003851 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003852 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3853 buf_sz,
3854 DMA_FROM_DEVICE);
3855
3856 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3857 if (!firmware_has_feature(FW_FEATURE_CMO))
3858 dev_err(dev, "Couldn't map offload buffer\n");
3859 return;
3860 }
3861
3862 memset(&newcrq, 0, sizeof(newcrq));
3863 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3864 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3865 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3866 newcrq.query_ip_offload.ioba =
3867 cpu_to_be32(adapter->ip_offload_tok);
3868
3869 ibmvnic_send_crq(adapter, &newcrq);
3870 }
3871}
3872
3873static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3874 struct ibmvnic_adapter *adapter)
3875{
3876 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003877 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003878 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3879 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003880 int i;
3881
3882 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05003883 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003884 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05003885 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003886
John Allen498cd8e2016-04-06 11:49:55 -05003887 /* If the number of queues requested can't be allocated by the
3888 * server, the login response will return with code 1. We will need
3889 * to resend the login buffer with fewer queues requested.
3890 */
3891 if (login_rsp_crq->generic.rc.code) {
3892 adapter->renegotiate = true;
3893 complete(&adapter->init_done);
3894 return 0;
3895 }
3896
John Allenc26eba02017-10-26 16:23:25 -05003897 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3898
Thomas Falcon032c5e82015-12-21 11:26:06 -06003899 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3900 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3901 netdev_dbg(adapter->netdev, "%016lx\n",
3902 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3903 }
3904
3905 /* Sanity checks */
3906 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3907 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3908 adapter->req_rx_add_queues !=
3909 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3910 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3911 ibmvnic_remove(adapter->vdev);
3912 return -EIO;
3913 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003914 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915 complete(&adapter->init_done);
3916
Thomas Falcon032c5e82015-12-21 11:26:06 -06003917 return 0;
3918}
3919
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3921 struct ibmvnic_adapter *adapter)
3922{
3923 struct device *dev = &adapter->vdev->dev;
3924 long rc;
3925
3926 rc = crq->request_unmap_rsp.rc.code;
3927 if (rc)
3928 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3929}
3930
3931static void handle_query_map_rsp(union ibmvnic_crq *crq,
3932 struct ibmvnic_adapter *adapter)
3933{
3934 struct net_device *netdev = adapter->netdev;
3935 struct device *dev = &adapter->vdev->dev;
3936 long rc;
3937
3938 rc = crq->query_map_rsp.rc.code;
3939 if (rc) {
3940 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3941 return;
3942 }
3943 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3944 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3945 crq->query_map_rsp.free_pages);
3946}
3947
3948static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3949 struct ibmvnic_adapter *adapter)
3950{
3951 struct net_device *netdev = adapter->netdev;
3952 struct device *dev = &adapter->vdev->dev;
3953 long rc;
3954
Thomas Falcon901e0402017-02-15 12:17:59 -06003955 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003956 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003957 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003958 rc = crq->query_capability.rc.code;
3959 if (rc) {
3960 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3961 goto out;
3962 }
3963
3964 switch (be16_to_cpu(crq->query_capability.capability)) {
3965 case MIN_TX_QUEUES:
3966 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003967 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003968 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3969 adapter->min_tx_queues);
3970 break;
3971 case MIN_RX_QUEUES:
3972 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003973 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003974 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3975 adapter->min_rx_queues);
3976 break;
3977 case MIN_RX_ADD_QUEUES:
3978 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003979 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003980 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3981 adapter->min_rx_add_queues);
3982 break;
3983 case MAX_TX_QUEUES:
3984 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003985 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003986 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3987 adapter->max_tx_queues);
3988 break;
3989 case MAX_RX_QUEUES:
3990 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003991 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003992 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3993 adapter->max_rx_queues);
3994 break;
3995 case MAX_RX_ADD_QUEUES:
3996 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003997 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003998 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3999 adapter->max_rx_add_queues);
4000 break;
4001 case MIN_TX_ENTRIES_PER_SUBCRQ:
4002 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004003 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004004 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4005 adapter->min_tx_entries_per_subcrq);
4006 break;
4007 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4008 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004009 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004010 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4011 adapter->min_rx_add_entries_per_subcrq);
4012 break;
4013 case MAX_TX_ENTRIES_PER_SUBCRQ:
4014 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004015 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4017 adapter->max_tx_entries_per_subcrq);
4018 break;
4019 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4020 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004021 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004022 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4023 adapter->max_rx_add_entries_per_subcrq);
4024 break;
4025 case TCP_IP_OFFLOAD:
4026 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004027 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004028 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4029 adapter->tcp_ip_offload);
4030 break;
4031 case PROMISC_SUPPORTED:
4032 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004033 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004034 netdev_dbg(netdev, "promisc_supported = %lld\n",
4035 adapter->promisc_supported);
4036 break;
4037 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004038 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004039 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004040 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4041 break;
4042 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004043 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004044 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004045 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4046 break;
4047 case MAX_MULTICAST_FILTERS:
4048 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004049 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4051 adapter->max_multicast_filters);
4052 break;
4053 case VLAN_HEADER_INSERTION:
4054 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004055 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004056 if (adapter->vlan_header_insertion)
4057 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4058 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4059 adapter->vlan_header_insertion);
4060 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004061 case RX_VLAN_HEADER_INSERTION:
4062 adapter->rx_vlan_header_insertion =
4063 be64_to_cpu(crq->query_capability.number);
4064 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4065 adapter->rx_vlan_header_insertion);
4066 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004067 case MAX_TX_SG_ENTRIES:
4068 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004069 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004070 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4071 adapter->max_tx_sg_entries);
4072 break;
4073 case RX_SG_SUPPORTED:
4074 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004075 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004076 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4077 adapter->rx_sg_supported);
4078 break;
4079 case OPT_TX_COMP_SUB_QUEUES:
4080 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004081 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004082 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4083 adapter->opt_tx_comp_sub_queues);
4084 break;
4085 case OPT_RX_COMP_QUEUES:
4086 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004087 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004088 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4089 adapter->opt_rx_comp_queues);
4090 break;
4091 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4092 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004093 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004094 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4095 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4096 break;
4097 case OPT_TX_ENTRIES_PER_SUBCRQ:
4098 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004099 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004100 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4101 adapter->opt_tx_entries_per_subcrq);
4102 break;
4103 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4104 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004105 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004106 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4107 adapter->opt_rxba_entries_per_subcrq);
4108 break;
4109 case TX_RX_DESC_REQ:
4110 adapter->tx_rx_desc_req = crq->query_capability.number;
4111 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4112 adapter->tx_rx_desc_req);
4113 break;
4114
4115 default:
4116 netdev_err(netdev, "Got invalid cap rsp %d\n",
4117 crq->query_capability.capability);
4118 }
4119
4120out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004121 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4122 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004123 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004124 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125}
4126
Thomas Falcon032c5e82015-12-21 11:26:06 -06004127static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4128 struct ibmvnic_adapter *adapter)
4129{
4130 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4131 struct net_device *netdev = adapter->netdev;
4132 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004133 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004134 long rc;
4135
4136 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004137 (unsigned long int)cpu_to_be64(u64_crq[0]),
4138 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004139 switch (gen_crq->first) {
4140 case IBMVNIC_CRQ_INIT_RSP:
4141 switch (gen_crq->cmd) {
4142 case IBMVNIC_CRQ_INIT:
4143 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004144 adapter->from_passive_init = true;
4145 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004146 break;
4147 case IBMVNIC_CRQ_INIT_COMPLETE:
4148 dev_info(dev, "Partner initialization complete\n");
4149 send_version_xchg(adapter);
4150 break;
4151 default:
4152 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4153 }
4154 return;
4155 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004156 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004157 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004158 dev_info(dev, "Migrated, re-enabling adapter\n");
4159 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004160 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4161 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004162 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004163 } else {
4164 /* The adapter lost the connection */
4165 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4166 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004167 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168 }
4169 return;
4170 case IBMVNIC_CRQ_CMD_RSP:
4171 break;
4172 default:
4173 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4174 gen_crq->first);
4175 return;
4176 }
4177
4178 switch (gen_crq->cmd) {
4179 case VERSION_EXCHANGE_RSP:
4180 rc = crq->version_exchange_rsp.rc.code;
4181 if (rc) {
4182 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4183 break;
4184 }
4185 dev_info(dev, "Partner protocol version is %d\n",
4186 crq->version_exchange_rsp.version);
4187 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4188 ibmvnic_version)
4189 ibmvnic_version =
4190 be16_to_cpu(crq->version_exchange_rsp.version);
4191 send_cap_queries(adapter);
4192 break;
4193 case QUERY_CAPABILITY_RSP:
4194 handle_query_cap_rsp(crq, adapter);
4195 break;
4196 case QUERY_MAP_RSP:
4197 handle_query_map_rsp(crq, adapter);
4198 break;
4199 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004200 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4201 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202 break;
4203 case REQUEST_UNMAP_RSP:
4204 handle_request_unmap_rsp(crq, adapter);
4205 break;
4206 case REQUEST_CAPABILITY_RSP:
4207 handle_request_cap_rsp(crq, adapter);
4208 break;
4209 case LOGIN_RSP:
4210 netdev_dbg(netdev, "Got Login Response\n");
4211 handle_login_rsp(crq, adapter);
4212 break;
4213 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004214 netdev_dbg(netdev,
4215 "Got Logical Link State Response, state: %d rc: %d\n",
4216 crq->logical_link_state_rsp.link_state,
4217 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004218 adapter->logical_link_state =
4219 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004220 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4221 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004222 break;
4223 case LINK_STATE_INDICATION:
4224 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4225 adapter->phys_link_state =
4226 crq->link_state_indication.phys_link_state;
4227 adapter->logical_link_state =
4228 crq->link_state_indication.logical_link_state;
4229 break;
4230 case CHANGE_MAC_ADDR_RSP:
4231 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004232 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004233 break;
4234 case ERROR_INDICATION:
4235 netdev_dbg(netdev, "Got Error Indication\n");
4236 handle_error_indication(crq, adapter);
4237 break;
4238 case REQUEST_ERROR_RSP:
4239 netdev_dbg(netdev, "Got Error Detail Response\n");
4240 handle_error_info_rsp(crq, adapter);
4241 break;
4242 case REQUEST_STATISTICS_RSP:
4243 netdev_dbg(netdev, "Got Statistics Response\n");
4244 complete(&adapter->stats_done);
4245 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004246 case QUERY_IP_OFFLOAD_RSP:
4247 netdev_dbg(netdev, "Got Query IP offload Response\n");
4248 handle_query_ip_offload_rsp(adapter);
4249 break;
4250 case MULTICAST_CTRL_RSP:
4251 netdev_dbg(netdev, "Got multicast control Response\n");
4252 break;
4253 case CONTROL_IP_OFFLOAD_RSP:
4254 netdev_dbg(netdev, "Got Control IP offload Response\n");
4255 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4256 sizeof(adapter->ip_offload_ctrl),
4257 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004258 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004259 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004260 case COLLECT_FW_TRACE_RSP:
4261 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4262 complete(&adapter->fw_done);
4263 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004264 case GET_VPD_SIZE_RSP:
4265 handle_vpd_size_rsp(crq, adapter);
4266 break;
4267 case GET_VPD_RSP:
4268 handle_vpd_rsp(crq, adapter);
4269 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004270 default:
4271 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4272 gen_crq->cmd);
4273 }
4274}
4275
4276static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4277{
4278 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004279
Thomas Falcon6c267b32017-02-15 12:17:58 -06004280 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004281 return IRQ_HANDLED;
4282}
4283
4284static void ibmvnic_tasklet(void *data)
4285{
4286 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004287 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004288 union ibmvnic_crq *crq;
4289 unsigned long flags;
4290 bool done = false;
4291
4292 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004293 while (!done) {
4294 /* Pull all the valid messages off the CRQ */
4295 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4296 ibmvnic_handle_crq(crq, adapter);
4297 crq->generic.first = 0;
4298 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004299
4300 /* remain in tasklet until all
4301 * capabilities responses are received
4302 */
4303 if (!adapter->wait_capability)
4304 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004305 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004306 /* if capabilities CRQ's were sent in this tasklet, the following
4307 * tasklet must wait until all responses are received
4308 */
4309 if (atomic_read(&adapter->running_cap_crqs) != 0)
4310 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004311 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312}
4313
4314static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4315{
4316 struct vio_dev *vdev = adapter->vdev;
4317 int rc;
4318
4319 do {
4320 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4321 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4322
4323 if (rc)
4324 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4325
4326 return rc;
4327}
4328
4329static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4330{
4331 struct ibmvnic_crq_queue *crq = &adapter->crq;
4332 struct device *dev = &adapter->vdev->dev;
4333 struct vio_dev *vdev = adapter->vdev;
4334 int rc;
4335
4336 /* Close the CRQ */
4337 do {
4338 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4339 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4340
4341 /* Clean out the queue */
4342 memset(crq->msgs, 0, PAGE_SIZE);
4343 crq->cur = 0;
4344
4345 /* And re-open it again */
4346 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4347 crq->msg_token, PAGE_SIZE);
4348
4349 if (rc == H_CLOSED)
4350 /* Adapter is good, but other end is not ready */
4351 dev_warn(dev, "Partner adapter not ready\n");
4352 else if (rc != 0)
4353 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4354
4355 return rc;
4356}
4357
Nathan Fontenotf9928872017-03-30 02:48:54 -04004358static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004359{
4360 struct ibmvnic_crq_queue *crq = &adapter->crq;
4361 struct vio_dev *vdev = adapter->vdev;
4362 long rc;
4363
Nathan Fontenotf9928872017-03-30 02:48:54 -04004364 if (!crq->msgs)
4365 return;
4366
Thomas Falcon032c5e82015-12-21 11:26:06 -06004367 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4368 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004369 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004370 do {
4371 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4372 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4373
4374 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4375 DMA_BIDIRECTIONAL);
4376 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004377 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004378}
4379
Nathan Fontenotf9928872017-03-30 02:48:54 -04004380static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004381{
4382 struct ibmvnic_crq_queue *crq = &adapter->crq;
4383 struct device *dev = &adapter->vdev->dev;
4384 struct vio_dev *vdev = adapter->vdev;
4385 int rc, retrc = -ENOMEM;
4386
Nathan Fontenotf9928872017-03-30 02:48:54 -04004387 if (crq->msgs)
4388 return 0;
4389
Thomas Falcon032c5e82015-12-21 11:26:06 -06004390 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4391 /* Should we allocate more than one page? */
4392
4393 if (!crq->msgs)
4394 return -ENOMEM;
4395
4396 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4397 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4398 DMA_BIDIRECTIONAL);
4399 if (dma_mapping_error(dev, crq->msg_token))
4400 goto map_failed;
4401
4402 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4403 crq->msg_token, PAGE_SIZE);
4404
4405 if (rc == H_RESOURCE)
4406 /* maybe kexecing and resource is busy. try a reset */
4407 rc = ibmvnic_reset_crq(adapter);
4408 retrc = rc;
4409
4410 if (rc == H_CLOSED) {
4411 dev_warn(dev, "Partner adapter not ready\n");
4412 } else if (rc) {
4413 dev_warn(dev, "Error %d opening adapter\n", rc);
4414 goto reg_crq_failed;
4415 }
4416
4417 retrc = 0;
4418
Thomas Falcon6c267b32017-02-15 12:17:58 -06004419 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4420 (unsigned long)adapter);
4421
Thomas Falcon032c5e82015-12-21 11:26:06 -06004422 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4423 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4424 adapter);
4425 if (rc) {
4426 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4427 vdev->irq, rc);
4428 goto req_irq_failed;
4429 }
4430
4431 rc = vio_enable_interrupts(vdev);
4432 if (rc) {
4433 dev_err(dev, "Error %d enabling interrupts\n", rc);
4434 goto req_irq_failed;
4435 }
4436
4437 crq->cur = 0;
4438 spin_lock_init(&crq->lock);
4439
4440 return retrc;
4441
4442req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004443 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004444 do {
4445 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4446 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4447reg_crq_failed:
4448 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4449map_failed:
4450 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004451 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004452 return retrc;
4453}
4454
John Allenf6ef6402017-03-17 17:13:42 -05004455static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4456{
4457 struct device *dev = &adapter->vdev->dev;
4458 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004459 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004460 int rc;
4461
John Allenc26eba02017-10-26 16:23:25 -05004462 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004463 rc = ibmvnic_reset_crq(adapter);
4464 if (!rc)
4465 rc = vio_enable_interrupts(adapter->vdev);
4466 } else {
4467 rc = init_crq_queue(adapter);
4468 }
4469
John Allenf6ef6402017-03-17 17:13:42 -05004470 if (rc) {
4471 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4472 return rc;
4473 }
4474
John Allen017892c12017-05-26 10:30:19 -04004475 adapter->from_passive_init = false;
4476
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004477 old_num_rx_queues = adapter->req_rx_queues;
4478 old_num_tx_queues = adapter->req_tx_queues;
4479
John Allenf6ef6402017-03-17 17:13:42 -05004480 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004481 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004482 ibmvnic_send_crq_init(adapter);
4483 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4484 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004485 return -1;
4486 }
4487
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004488 if (adapter->init_done_rc) {
4489 release_crq_queue(adapter);
4490 return adapter->init_done_rc;
4491 }
4492
John Allen017892c12017-05-26 10:30:19 -04004493 if (adapter->from_passive_init) {
4494 adapter->state = VNIC_OPEN;
4495 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004496 return -1;
4497 }
4498
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004499 if (adapter->resetting && !adapter->wait_for_reset) {
4500 if (adapter->req_rx_queues != old_num_rx_queues ||
4501 adapter->req_tx_queues != old_num_tx_queues) {
4502 release_sub_crqs(adapter, 0);
4503 rc = init_sub_crqs(adapter);
4504 } else {
4505 rc = reset_sub_crq_queues(adapter);
4506 }
4507 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004508 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004509 }
4510
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004511 if (rc) {
4512 dev_err(dev, "Initialization of sub crqs failed\n");
4513 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004514 return rc;
4515 }
4516
4517 rc = init_sub_crq_irqs(adapter);
4518 if (rc) {
4519 dev_err(dev, "Failed to initialize sub crq irqs\n");
4520 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004521 }
4522
Thomas Falcon53cc7722018-02-26 18:10:56 -06004523 rc = init_stats_buffers(adapter);
4524 if (rc)
4525 return rc;
4526
4527 rc = init_stats_token(adapter);
4528 if (rc)
4529 return rc;
4530
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004531 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004532}
4533
Thomas Falcon40c9db82017-06-12 12:35:04 -05004534static struct device_attribute dev_attr_failover;
4535
Thomas Falcon032c5e82015-12-21 11:26:06 -06004536static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4537{
4538 struct ibmvnic_adapter *adapter;
4539 struct net_device *netdev;
4540 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004541 int rc;
4542
4543 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4544 dev->unit_address);
4545
4546 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4547 VETH_MAC_ADDR, NULL);
4548 if (!mac_addr_p) {
4549 dev_err(&dev->dev,
4550 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4551 __FILE__, __LINE__);
4552 return 0;
4553 }
4554
4555 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004556 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004557 if (!netdev)
4558 return -ENOMEM;
4559
4560 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004561 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004562 dev_set_drvdata(&dev->dev, netdev);
4563 adapter->vdev = dev;
4564 adapter->netdev = netdev;
4565
4566 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4567 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4568 netdev->irq = dev->irq;
4569 netdev->netdev_ops = &ibmvnic_netdev_ops;
4570 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4571 SET_NETDEV_DEV(netdev, &dev->dev);
4572
4573 spin_lock_init(&adapter->stats_lock);
4574
Thomas Falcon032c5e82015-12-21 11:26:06 -06004575 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004576 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004577
Nathan Fontenoted651a12017-05-03 14:04:38 -04004578 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4579 INIT_LIST_HEAD(&adapter->rwi_list);
4580 mutex_init(&adapter->reset_lock);
4581 mutex_init(&adapter->rwi_lock);
4582 adapter->resetting = false;
4583
John Allenc26eba02017-10-26 16:23:25 -05004584 adapter->mac_change_pending = false;
4585
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004586 do {
4587 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004588 if (rc && rc != EAGAIN)
4589 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004590 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004591
Thomas Falconf39f0d12017-02-14 10:22:59 -06004592 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004593 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4594 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004595
Thomas Falcon40c9db82017-06-12 12:35:04 -05004596 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004597 if (rc)
4598 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004599
Mick Tarsele876a8a2017-09-28 13:53:18 -07004600 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004601 rc = register_netdev(netdev);
4602 if (rc) {
4603 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004604 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004605 }
4606 dev_info(&dev->dev, "ibmvnic registered\n");
4607
Nathan Fontenot90c80142017-05-03 14:04:32 -04004608 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004609
4610 adapter->wait_for_reset = false;
4611
Thomas Falcon032c5e82015-12-21 11:26:06 -06004612 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004613
4614ibmvnic_register_fail:
4615 device_remove_file(&dev->dev, &dev_attr_failover);
4616
4617ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004618 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004619 release_crq_queue(adapter);
4620 free_netdev(netdev);
4621
4622 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004623}
4624
4625static int ibmvnic_remove(struct vio_dev *dev)
4626{
4627 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004628 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004629
Nathan Fontenot90c80142017-05-03 14:04:32 -04004630 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004631 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004632 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004633
4634 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004635 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004636 release_crq_queue(adapter);
4637
Thomas Falcon53cc7722018-02-26 18:10:56 -06004638 release_stats_token(adapter);
4639 release_stats_buffers(adapter);
4640
Nathan Fontenot90c80142017-05-03 14:04:32 -04004641 adapter->state = VNIC_REMOVED;
4642
Nathan Fontenoted651a12017-05-03 14:04:38 -04004643 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004644 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004645 free_netdev(netdev);
4646 dev_set_drvdata(&dev->dev, NULL);
4647
4648 return 0;
4649}
4650
Thomas Falcon40c9db82017-06-12 12:35:04 -05004651static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4652 const char *buf, size_t count)
4653{
4654 struct net_device *netdev = dev_get_drvdata(dev);
4655 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4656 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4657 __be64 session_token;
4658 long rc;
4659
4660 if (!sysfs_streq(buf, "1"))
4661 return -EINVAL;
4662
4663 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4664 H_GET_SESSION_TOKEN, 0, 0, 0);
4665 if (rc) {
4666 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4667 rc);
4668 return -EINVAL;
4669 }
4670
4671 session_token = (__be64)retbuf[0];
4672 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4673 be64_to_cpu(session_token));
4674 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4675 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4676 if (rc) {
4677 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4678 rc);
4679 return -EINVAL;
4680 }
4681
4682 return count;
4683}
4684
Joe Perches6cbaefb2017-12-19 10:15:09 -08004685static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004686
Thomas Falcon032c5e82015-12-21 11:26:06 -06004687static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4688{
4689 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4690 struct ibmvnic_adapter *adapter;
4691 struct iommu_table *tbl;
4692 unsigned long ret = 0;
4693 int i;
4694
4695 tbl = get_iommu_table_base(&vdev->dev);
4696
4697 /* netdev inits at probe time along with the structures we need below*/
4698 if (!netdev)
4699 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4700
4701 adapter = netdev_priv(netdev);
4702
4703 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004704 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4705
4706 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4707 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4708
4709 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4710 i++)
4711 ret += adapter->rx_pool[i].size *
4712 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4713
4714 return ret;
4715}
4716
4717static int ibmvnic_resume(struct device *dev)
4718{
4719 struct net_device *netdev = dev_get_drvdata(dev);
4720 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004721
John Allencb89ba22017-06-19 11:27:53 -05004722 if (adapter->state != VNIC_OPEN)
4723 return 0;
4724
John Allena2488782017-07-24 13:26:06 -05004725 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004726
4727 return 0;
4728}
4729
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304730static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004731 {"network", "IBM,vnic"},
4732 {"", "" }
4733};
4734MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4735
4736static const struct dev_pm_ops ibmvnic_pm_ops = {
4737 .resume = ibmvnic_resume
4738};
4739
4740static struct vio_driver ibmvnic_driver = {
4741 .id_table = ibmvnic_device_table,
4742 .probe = ibmvnic_probe,
4743 .remove = ibmvnic_remove,
4744 .get_desired_dma = ibmvnic_get_desired_dma,
4745 .name = ibmvnic_driver_name,
4746 .pm = &ibmvnic_pm_ops,
4747};
4748
4749/* module functions */
4750static int __init ibmvnic_module_init(void)
4751{
4752 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4753 IBMVNIC_DRIVER_VERSION);
4754
4755 return vio_register_driver(&ibmvnic_driver);
4756}
4757
4758static void __exit ibmvnic_module_exit(void)
4759{
4760 vio_unregister_driver(&ibmvnic_driver);
4761}
4762
4763module_init(ibmvnic_module_init);
4764module_exit(ibmvnic_module_exit);