blob: 1b3cc8bb07050f6856eb4f3e7fe28e4296219d49 [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 *);
93static void release_sub_crqs(struct ibmvnic_adapter *);
94static 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);
John Allenbd0b6722017-03-17 17:13:40 -0500114static void send_login(struct ibmvnic_adapter *adapter);
115static 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;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
325 DMA_FROM_DEVICE);
326
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400330
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
335 */
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
338 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600339}
340
341static void replenish_pools(struct ibmvnic_adapter *adapter)
342{
343 int i;
344
Thomas Falcon032c5e82015-12-21 11:26:06 -0600345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
347 i++) {
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
350 }
351}
352
John Allen3d52b592017-08-02 16:44:14 -0500353static void release_stats_buffers(struct ibmvnic_adapter *adapter)
354{
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500359}
360
361static int init_stats_buffers(struct ibmvnic_adapter *adapter)
362{
363 adapter->tx_stats_buffers =
364 kcalloc(adapter->req_tx_queues,
365 sizeof(struct ibmvnic_tx_queue_stats),
366 GFP_KERNEL);
367 if (!adapter->tx_stats_buffers)
368 return -ENOMEM;
369
370 adapter->rx_stats_buffers =
371 kcalloc(adapter->req_rx_queues,
372 sizeof(struct ibmvnic_rx_queue_stats),
373 GFP_KERNEL);
374 if (!adapter->rx_stats_buffers)
375 return -ENOMEM;
376
377 return 0;
378}
379
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400380static void release_stats_token(struct ibmvnic_adapter *adapter)
381{
382 struct device *dev = &adapter->vdev->dev;
383
384 if (!adapter->stats_token)
385 return;
386
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
389 DMA_FROM_DEVICE);
390 adapter->stats_token = 0;
391}
392
393static int init_stats_token(struct ibmvnic_adapter *adapter)
394{
395 struct device *dev = &adapter->vdev->dev;
396 dma_addr_t stok;
397
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
400 DMA_FROM_DEVICE);
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
403 return -1;
404 }
405
406 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400408 return 0;
409}
410
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400411static int reset_rx_pools(struct ibmvnic_adapter *adapter)
412{
413 struct ibmvnic_rx_pool *rx_pool;
414 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500415 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600416 u64 *size_array;
417
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400420
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
424
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
426
John Allen896d8692018-01-18 16:26:31 -0600427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
431 rx_pool->size *
432 rx_pool->buff_size);
433 } else {
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
436 }
437
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500438 if (rc)
439 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400440
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
443
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
446
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500450 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400451 }
452
453 return 0;
454}
455
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400456static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600457{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400458 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400459 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600460
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400461 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600462 return;
463
John Allen896d8692018-01-18 16:26:31 -0600464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400465 rx_pool = &adapter->rx_pool[i];
466
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
468
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
471
472 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400473 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400474
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
479 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600480 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400481
482 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600483 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400484
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
John Allen896d8692018-01-18 16:26:31 -0600487 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400488}
489
490static int init_rx_pools(struct net_device *netdev)
491{
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
495 int rxadd_subcrqs;
496 u64 *size_array;
497 int i, j;
498
499 rxadd_subcrqs =
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
503
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
506 GFP_KERNEL);
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
509 return -1;
510 }
511
John Allen896d8692018-01-18 16:26:31 -0600512 adapter->num_active_rx_pools = 0;
513
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
516
517 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
521
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
523 rx_pool->index = i;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
525 rx_pool->active = 1;
526
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
528 GFP_KERNEL);
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
531 return -1;
532 }
533
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
536 GFP_KERNEL);
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
540 return -1;
541 }
542
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
546 return -1;
547 }
548
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
551
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
555 }
556
John Allen896d8692018-01-18 16:26:31 -0600557 adapter->num_active_rx_pools = rxadd_subcrqs;
558
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400559 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600560}
561
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400562static int reset_tx_pools(struct ibmvnic_adapter *adapter)
563{
564 struct ibmvnic_tx_pool *tx_pool;
565 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500566 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400567
568 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
569 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500570 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
571
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400572 tx_pool = &adapter->tx_pool[i];
573
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500574 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
575 if (rc)
576 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400577
Thomas Falconfdb06102017-10-17 12:36:55 -0500578 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
579 if (rc)
580 return rc;
581
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400582 memset(tx_pool->tx_buff, 0,
583 adapter->req_tx_entries_per_subcrq *
584 sizeof(struct ibmvnic_tx_buff));
585
586 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
587 tx_pool->free_map[j] = j;
588
589 tx_pool->consumer_index = 0;
590 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500591 tx_pool->tso_index = 0;
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
Nathan Fontenotc657e322017-03-30 02:49:06 -0400608static void release_tx_pools(struct ibmvnic_adapter *adapter)
609{
610 struct ibmvnic_tx_pool *tx_pool;
John Allen896d8692018-01-18 16:26:31 -0600611 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400612
613 if (!adapter->tx_pool)
614 return;
615
John Allen896d8692018-01-18 16:26:31 -0600616 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500617 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400618 tx_pool = &adapter->tx_pool[i];
619 kfree(tx_pool->tx_buff);
620 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500621 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400622 kfree(tx_pool->free_map);
623 }
624
625 kfree(adapter->tx_pool);
626 adapter->tx_pool = NULL;
John Allen896d8692018-01-18 16:26:31 -0600627 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400628}
629
630static int init_tx_pools(struct net_device *netdev)
631{
632 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
633 struct device *dev = &adapter->vdev->dev;
634 struct ibmvnic_tx_pool *tx_pool;
635 int tx_subcrqs;
636 int i, j;
637
638 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
639 adapter->tx_pool = kcalloc(tx_subcrqs,
640 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
641 if (!adapter->tx_pool)
642 return -1;
643
John Allen896d8692018-01-18 16:26:31 -0600644 adapter->num_active_tx_pools = 0;
645
Nathan Fontenotc657e322017-03-30 02:49:06 -0400646 for (i = 0; i < tx_subcrqs; i++) {
647 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500648
649 netdev_dbg(adapter->netdev,
650 "Initializing tx_pool[%d], %lld buffs\n",
651 i, adapter->req_tx_entries_per_subcrq);
652
Nathan Fontenotc657e322017-03-30 02:49:06 -0400653 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
654 sizeof(struct ibmvnic_tx_buff),
655 GFP_KERNEL);
656 if (!tx_pool->tx_buff) {
657 dev_err(dev, "tx pool buffer allocation failed\n");
658 release_tx_pools(adapter);
659 return -1;
660 }
661
662 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
663 adapter->req_tx_entries_per_subcrq *
664 adapter->req_mtu)) {
665 release_tx_pools(adapter);
666 return -1;
667 }
668
Thomas Falconfdb06102017-10-17 12:36:55 -0500669 /* alloc TSO ltb */
670 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
671 IBMVNIC_TSO_BUFS *
672 IBMVNIC_TSO_BUF_SZ)) {
673 release_tx_pools(adapter);
674 return -1;
675 }
676
677 tx_pool->tso_index = 0;
678
Nathan Fontenotc657e322017-03-30 02:49:06 -0400679 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
680 sizeof(int), GFP_KERNEL);
681 if (!tx_pool->free_map) {
682 release_tx_pools(adapter);
683 return -1;
684 }
685
686 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
687 tx_pool->free_map[j] = j;
688
689 tx_pool->consumer_index = 0;
690 tx_pool->producer_index = 0;
691 }
692
John Allen896d8692018-01-18 16:26:31 -0600693 adapter->num_active_tx_pools = tx_subcrqs;
694
Nathan Fontenotc657e322017-03-30 02:49:06 -0400695 return 0;
696}
697
Nathan Fontenot661a2622017-04-19 13:44:58 -0400698static void release_error_buffers(struct ibmvnic_adapter *adapter)
699{
700 struct device *dev = &adapter->vdev->dev;
701 struct ibmvnic_error_buff *error_buff, *tmp;
702 unsigned long flags;
703
704 spin_lock_irqsave(&adapter->error_list_lock, flags);
705 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
706 list_del(&error_buff->list);
707 dma_unmap_single(dev, error_buff->dma, error_buff->len,
708 DMA_FROM_DEVICE);
709 kfree(error_buff->buff);
710 kfree(error_buff);
711 }
712 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
713}
714
John Allend944c3d62017-05-26 10:30:13 -0400715static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
716{
717 int i;
718
719 if (adapter->napi_enabled)
720 return;
721
722 for (i = 0; i < adapter->req_rx_queues; i++)
723 napi_enable(&adapter->napi[i]);
724
725 adapter->napi_enabled = true;
726}
727
728static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
729{
730 int i;
731
732 if (!adapter->napi_enabled)
733 return;
734
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500735 for (i = 0; i < adapter->req_rx_queues; i++) {
736 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400737 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500738 }
John Allend944c3d62017-05-26 10:30:13 -0400739
740 adapter->napi_enabled = false;
741}
742
John Allena57a5d22017-03-17 17:13:41 -0500743static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600744{
745 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500746 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600747 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500748 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600749
John Allenbd0b6722017-03-17 17:13:40 -0500750 do {
751 if (adapter->renegotiate) {
752 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400753 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500754
755 reinit_completion(&adapter->init_done);
756 send_cap_queries(adapter);
757 if (!wait_for_completion_timeout(&adapter->init_done,
758 timeout)) {
759 dev_err(dev, "Capabilities query timeout\n");
760 return -1;
761 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500762 rc = init_sub_crqs(adapter);
763 if (rc) {
764 dev_err(dev,
765 "Initialization of SCRQ's failed\n");
766 return -1;
767 }
768 rc = init_sub_crq_irqs(adapter);
769 if (rc) {
770 dev_err(dev,
771 "Initialization of SCRQ's irqs failed\n");
772 return -1;
773 }
John Allenbd0b6722017-03-17 17:13:40 -0500774 }
775
776 reinit_completion(&adapter->init_done);
777 send_login(adapter);
778 if (!wait_for_completion_timeout(&adapter->init_done,
779 timeout)) {
780 dev_err(dev, "Login timeout\n");
781 return -1;
782 }
783 } while (adapter->renegotiate);
784
Thomas Falcon3d166132018-01-10 19:39:52 -0600785 /* handle pending MAC address changes after successful login */
786 if (adapter->mac_change_pending) {
787 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
788 adapter->mac_change_pending = false;
789 }
790
John Allena57a5d22017-03-17 17:13:41 -0500791 return 0;
792}
793
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600794static void release_login_buffer(struct ibmvnic_adapter *adapter)
795{
796 kfree(adapter->login_buf);
797 adapter->login_buf = NULL;
798}
799
800static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
801{
802 kfree(adapter->login_rsp_buf);
803 adapter->login_rsp_buf = NULL;
804}
805
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400806static void release_resources(struct ibmvnic_adapter *adapter)
807{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400808 int i;
809
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200810 release_vpd_data(adapter);
811
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400812 release_tx_pools(adapter);
813 release_rx_pools(adapter);
814
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400815 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500816 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400817 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400818
819 if (adapter->napi) {
820 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500821 if (&adapter->napi[i]) {
822 netdev_dbg(adapter->netdev,
823 "Releasing napi[%d]\n", i);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400824 netif_napi_del(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500825 }
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400826 }
827 }
Thomas Falcon6e4842dd2018-02-13 18:23:41 -0600828 kfree(adapter->napi);
829 adapter->napi = NULL;
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600830
831 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400832}
833
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400834static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
835{
836 struct net_device *netdev = adapter->netdev;
837 unsigned long timeout = msecs_to_jiffies(30000);
838 union ibmvnic_crq crq;
839 bool resend;
840 int rc;
841
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500842 netdev_dbg(netdev, "setting link state %d\n", link_state);
843
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400844 memset(&crq, 0, sizeof(crq));
845 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
846 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
847 crq.logical_link_state.link_state = link_state;
848
849 do {
850 resend = false;
851
852 reinit_completion(&adapter->init_done);
853 rc = ibmvnic_send_crq(adapter, &crq);
854 if (rc) {
855 netdev_err(netdev, "Failed to set link state\n");
856 return rc;
857 }
858
859 if (!wait_for_completion_timeout(&adapter->init_done,
860 timeout)) {
861 netdev_err(netdev, "timeout setting link state\n");
862 return -1;
863 }
864
865 if (adapter->init_done_rc == 1) {
866 /* Partuial success, delay and re-send */
867 mdelay(1000);
868 resend = true;
869 }
870 } while (resend);
871
872 return 0;
873}
874
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400875static int set_real_num_queues(struct net_device *netdev)
876{
877 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
878 int rc;
879
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500880 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
881 adapter->req_tx_queues, adapter->req_rx_queues);
882
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400883 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
884 if (rc) {
885 netdev_err(netdev, "failed to set the number of tx queues\n");
886 return rc;
887 }
888
889 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
890 if (rc)
891 netdev_err(netdev, "failed to set the number of rx queues\n");
892
893 return rc;
894}
895
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200896static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
897{
898 struct device *dev = &adapter->vdev->dev;
899 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200900 int len = 0;
901
902 if (adapter->vpd->buff)
903 len = adapter->vpd->len;
904
John Allen69d08dc2018-01-18 16:27:58 -0600905 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200906 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
907 crq.get_vpd_size.cmd = GET_VPD_SIZE;
908 ibmvnic_send_crq(adapter, &crq);
909 wait_for_completion(&adapter->fw_done);
910
911 if (!adapter->vpd->len)
912 return -ENODATA;
913
914 if (!adapter->vpd->buff)
915 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
916 else if (adapter->vpd->len != len)
917 adapter->vpd->buff =
918 krealloc(adapter->vpd->buff,
919 adapter->vpd->len, GFP_KERNEL);
920
921 if (!adapter->vpd->buff) {
922 dev_err(dev, "Could allocate VPD buffer\n");
923 return -ENOMEM;
924 }
925
926 adapter->vpd->dma_addr =
927 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
928 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200929 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200930 dev_err(dev, "Could not map VPD buffer\n");
931 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600932 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200933 return -ENOMEM;
934 }
935
936 reinit_completion(&adapter->fw_done);
937 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
938 crq.get_vpd.cmd = GET_VPD;
939 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
940 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
941 ibmvnic_send_crq(adapter, &crq);
942 wait_for_completion(&adapter->fw_done);
943
944 return 0;
945}
946
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400947static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500948{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400949 struct net_device *netdev = adapter->netdev;
950 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500951
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400952 rc = set_real_num_queues(netdev);
953 if (rc)
954 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500955
John Allen3d52b592017-08-02 16:44:14 -0500956 rc = init_stats_buffers(adapter);
957 if (rc)
958 return rc;
959
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400960 rc = init_stats_token(adapter);
961 if (rc)
962 return rc;
963
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200964 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
965 if (!adapter->vpd)
966 return -ENOMEM;
967
John Allen69d08dc2018-01-18 16:27:58 -0600968 /* Vital Product Data (VPD) */
969 rc = ibmvnic_get_vpd(adapter);
970 if (rc) {
971 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
972 return rc;
973 }
974
Thomas Falcon032c5e82015-12-21 11:26:06 -0600975 adapter->map_id = 1;
976 adapter->napi = kcalloc(adapter->req_rx_queues,
977 sizeof(struct napi_struct), GFP_KERNEL);
978 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400979 return -ENOMEM;
980
Thomas Falcon032c5e82015-12-21 11:26:06 -0600981 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500982 netdev_dbg(netdev, "Adding napi[%d]\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600983 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
984 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600985 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600986
Thomas Falcon032c5e82015-12-21 11:26:06 -0600987 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400988
989 rc = init_rx_pools(netdev);
990 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400991 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600992
Nathan Fontenotc657e322017-03-30 02:49:06 -0400993 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400994 return rc;
995}
996
Nathan Fontenoted651a12017-05-03 14:04:38 -0400997static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400998{
999 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001000 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001001 int i, rc;
1002
Nathan Fontenot90c80142017-05-03 14:04:32 -04001003 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001004 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001005 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001006
Thomas Falcon032c5e82015-12-21 11:26:06 -06001007 /* We're ready to receive frames, enable the sub-crq interrupts and
1008 * set the logical link state to up
1009 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001010 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001011 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001012 if (prev_state == VNIC_CLOSED)
1013 enable_irq(adapter->rx_scrq[i]->irq);
1014 else
1015 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1016 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001017
Nathan Fontenoted651a12017-05-03 14:04:38 -04001018 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001019 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001020 if (prev_state == VNIC_CLOSED)
1021 enable_irq(adapter->tx_scrq[i]->irq);
1022 else
1023 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1024 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001025
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001026 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001027 if (rc) {
1028 for (i = 0; i < adapter->req_rx_queues; i++)
1029 napi_disable(&adapter->napi[i]);
1030 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001031 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001032 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001033
Nathan Fontenoted651a12017-05-03 14:04:38 -04001034 netif_tx_start_all_queues(netdev);
1035
1036 if (prev_state == VNIC_CLOSED) {
1037 for (i = 0; i < adapter->req_rx_queues; i++)
1038 napi_schedule(&adapter->napi[i]);
1039 }
1040
1041 adapter->state = VNIC_OPEN;
1042 return rc;
1043}
1044
1045static int ibmvnic_open(struct net_device *netdev)
1046{
1047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001048 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001049
1050 mutex_lock(&adapter->reset_lock);
1051
1052 if (adapter->state != VNIC_CLOSED) {
1053 rc = ibmvnic_login(netdev);
1054 if (rc) {
1055 mutex_unlock(&adapter->reset_lock);
1056 return rc;
1057 }
1058
1059 rc = init_resources(adapter);
1060 if (rc) {
1061 netdev_err(netdev, "failed to initialize resources\n");
1062 release_resources(adapter);
1063 mutex_unlock(&adapter->reset_lock);
1064 return rc;
1065 }
1066 }
1067
1068 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001069 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001070
Nathan Fontenoted651a12017-05-03 14:04:38 -04001071 mutex_unlock(&adapter->reset_lock);
1072
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001073 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001074}
1075
Thomas Falcond0869c02018-02-13 18:23:43 -06001076static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1077{
1078 struct ibmvnic_rx_pool *rx_pool;
1079 u64 rx_entries;
1080 int rx_scrqs;
1081 int i, j;
1082
1083 if (!adapter->rx_pool)
1084 return;
1085
1086 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1087 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1088
1089 /* Free any remaining skbs in the rx buffer pools */
1090 for (i = 0; i < rx_scrqs; i++) {
1091 rx_pool = &adapter->rx_pool[i];
1092 if (!rx_pool)
1093 continue;
1094
1095 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1096 for (j = 0; j < rx_entries; j++) {
1097 if (rx_pool->rx_buff[j].skb) {
1098 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
1099 rx_pool->rx_buff[j].skb = NULL;
1100 }
1101 }
1102 }
1103}
1104
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001105static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1106{
1107 struct ibmvnic_tx_pool *tx_pool;
1108 u64 tx_entries;
1109 int tx_scrqs;
1110 int i, j;
1111
1112 if (!adapter->tx_pool)
1113 return;
1114
1115 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1116 tx_entries = adapter->req_tx_entries_per_subcrq;
1117
1118 /* Free any remaining skbs in the tx buffer pools */
1119 for (i = 0; i < tx_scrqs; i++) {
1120 tx_pool = &adapter->tx_pool[i];
1121 if (!tx_pool)
1122 continue;
1123
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001124 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001125 for (j = 0; j < tx_entries; j++) {
1126 if (tx_pool->tx_buff[j].skb) {
1127 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1128 tx_pool->tx_buff[j].skb = NULL;
1129 }
1130 }
1131 }
1132}
1133
Nathan Fontenoted651a12017-05-03 14:04:38 -04001134static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001135{
1136 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001137 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -05001138 int i;
1139
Nathan Fontenot90c80142017-05-03 14:04:32 -04001140 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001141
1142 /* ensure that transmissions are stopped if called by do_reset */
1143 if (adapter->resetting)
1144 netif_tx_disable(netdev);
1145 else
1146 netif_tx_stop_all_queues(netdev);
1147
John Allend944c3d62017-05-26 10:30:13 -04001148 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001149
1150 if (adapter->tx_scrq) {
1151 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001152 if (adapter->tx_scrq[i]->irq) {
1153 netdev_dbg(adapter->netdev,
1154 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001155 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001156 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001157 }
1158
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001159 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001160 if (rc)
1161 return rc;
1162
1163 if (adapter->rx_scrq) {
1164 for (i = 0; i < adapter->req_rx_queues; i++) {
1165 int retries = 10;
1166
1167 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1168 retries--;
1169 mdelay(100);
1170
1171 if (retries == 0)
1172 break;
1173 }
1174
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001175 if (adapter->rx_scrq[i]->irq) {
1176 netdev_dbg(adapter->netdev,
1177 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001178 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001179 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001180 }
1181 }
Thomas Falcond0869c02018-02-13 18:23:43 -06001182 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001183 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001184 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001185 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001186}
1187
Nathan Fontenoted651a12017-05-03 14:04:38 -04001188static int ibmvnic_close(struct net_device *netdev)
1189{
1190 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1191 int rc;
1192
1193 mutex_lock(&adapter->reset_lock);
1194 rc = __ibmvnic_close(netdev);
1195 mutex_unlock(&adapter->reset_lock);
1196
1197 return rc;
1198}
1199
Thomas Falconad7775d2016-04-01 17:20:34 -05001200/**
1201 * build_hdr_data - creates L2/L3/L4 header data buffer
1202 * @hdr_field - bitfield determining needed headers
1203 * @skb - socket buffer
1204 * @hdr_len - array of header lengths
1205 * @tot_len - total length of data
1206 *
1207 * Reads hdr_field to determine which headers are needed by firmware.
1208 * Builds a buffer containing these headers. Saves individual header
1209 * lengths and total buffer length to be used to build descriptors.
1210 */
1211static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1212 int *hdr_len, u8 *hdr_data)
1213{
1214 int len = 0;
1215 u8 *hdr;
1216
1217 hdr_len[0] = sizeof(struct ethhdr);
1218
1219 if (skb->protocol == htons(ETH_P_IP)) {
1220 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1221 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1222 hdr_len[2] = tcp_hdrlen(skb);
1223 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1224 hdr_len[2] = sizeof(struct udphdr);
1225 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1226 hdr_len[1] = sizeof(struct ipv6hdr);
1227 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1228 hdr_len[2] = tcp_hdrlen(skb);
1229 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1230 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001231 } else if (skb->protocol == htons(ETH_P_ARP)) {
1232 hdr_len[1] = arp_hdr_len(skb->dev);
1233 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001234 }
1235
1236 memset(hdr_data, 0, 120);
1237 if ((hdr_field >> 6) & 1) {
1238 hdr = skb_mac_header(skb);
1239 memcpy(hdr_data, hdr, hdr_len[0]);
1240 len += hdr_len[0];
1241 }
1242
1243 if ((hdr_field >> 5) & 1) {
1244 hdr = skb_network_header(skb);
1245 memcpy(hdr_data + len, hdr, hdr_len[1]);
1246 len += hdr_len[1];
1247 }
1248
1249 if ((hdr_field >> 4) & 1) {
1250 hdr = skb_transport_header(skb);
1251 memcpy(hdr_data + len, hdr, hdr_len[2]);
1252 len += hdr_len[2];
1253 }
1254 return len;
1255}
1256
1257/**
1258 * create_hdr_descs - create header and header extension descriptors
1259 * @hdr_field - bitfield determining needed headers
1260 * @data - buffer containing header data
1261 * @len - length of data buffer
1262 * @hdr_len - array of individual header lengths
1263 * @scrq_arr - descriptor array
1264 *
1265 * Creates header and, if needed, header extension descriptors and
1266 * places them in a descriptor array, scrq_arr
1267 */
1268
Thomas Falcon2de09682017-10-16 10:02:11 -05001269static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1270 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001271{
1272 union sub_crq hdr_desc;
1273 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001274 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001275 u8 *data, *cur;
1276 int tmp;
1277
1278 while (tmp_len > 0) {
1279 cur = hdr_data + len - tmp_len;
1280
1281 memset(&hdr_desc, 0, sizeof(hdr_desc));
1282 if (cur != hdr_data) {
1283 data = hdr_desc.hdr_ext.data;
1284 tmp = tmp_len > 29 ? 29 : tmp_len;
1285 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1286 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1287 hdr_desc.hdr_ext.len = tmp;
1288 } else {
1289 data = hdr_desc.hdr.data;
1290 tmp = tmp_len > 24 ? 24 : tmp_len;
1291 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1292 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1293 hdr_desc.hdr.len = tmp;
1294 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1295 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1296 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1297 hdr_desc.hdr.flag = hdr_field << 1;
1298 }
1299 memcpy(data, cur, tmp);
1300 tmp_len -= tmp;
1301 *scrq_arr = hdr_desc;
1302 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001303 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001304 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001305
1306 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001307}
1308
1309/**
1310 * build_hdr_descs_arr - build a header descriptor array
1311 * @skb - socket buffer
1312 * @num_entries - number of descriptors to be sent
1313 * @subcrq - first TX descriptor
1314 * @hdr_field - bit field determining which headers will be sent
1315 *
1316 * This function will build a TX descriptor array with applicable
1317 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1318 */
1319
1320static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1321 int *num_entries, u8 hdr_field)
1322{
1323 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001324 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001325 u8 *hdr_data = txbuff->hdr_data;
1326
1327 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1328 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001329 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001330 txbuff->indir_arr + 1);
1331}
1332
Thomas Falcon032c5e82015-12-21 11:26:06 -06001333static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1334{
1335 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1336 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001337 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001338 struct device *dev = &adapter->vdev->dev;
1339 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001340 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001341 struct ibmvnic_tx_pool *tx_pool;
1342 unsigned int tx_send_failed = 0;
1343 unsigned int tx_map_failed = 0;
1344 unsigned int tx_dropped = 0;
1345 unsigned int tx_packets = 0;
1346 unsigned int tx_bytes = 0;
1347 dma_addr_t data_dma_addr;
1348 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001349 unsigned long lpar_rc;
1350 union sub_crq tx_crq;
1351 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001352 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001353 unsigned char *dst;
1354 u64 *handle_array;
1355 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001356 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001357 int ret = 0;
1358
Nathan Fontenoted651a12017-05-03 14:04:38 -04001359 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001360 if (!netif_subqueue_stopped(netdev, skb))
1361 netif_stop_subqueue(netdev, queue_num);
1362 dev_kfree_skb_any(skb);
1363
Thomas Falcon032c5e82015-12-21 11:26:06 -06001364 tx_send_failed++;
1365 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001366 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001367 goto out;
1368 }
1369
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001370 tx_pool = &adapter->tx_pool[queue_num];
1371 tx_scrq = adapter->tx_scrq[queue_num];
1372 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1373 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1374 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1375
Thomas Falcon032c5e82015-12-21 11:26:06 -06001376 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001377
1378 if (skb_is_gso(skb)) {
1379 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1380 dst = tx_pool->tso_ltb.buff + offset;
1381 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1382 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1383 tx_pool->tso_index++;
1384 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1385 tx_pool->tso_index = 0;
1386 } else {
1387 offset = index * adapter->req_mtu;
1388 dst = tx_pool->long_term_buff.buff + offset;
1389 memset(dst, 0, adapter->req_mtu);
1390 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1391 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001392
Thomas Falcon15482052017-10-17 12:36:54 -05001393 if (skb_shinfo(skb)->nr_frags) {
1394 int cur, i;
1395
1396 /* Copy the head */
1397 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1398 cur = skb_headlen(skb);
1399
1400 /* Copy the frags */
1401 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1402 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1403
1404 memcpy(dst + cur,
1405 page_address(skb_frag_page(frag)) +
1406 frag->page_offset, skb_frag_size(frag));
1407 cur += skb_frag_size(frag);
1408 }
1409 } else {
1410 skb_copy_from_linear_data(skb, dst, skb->len);
1411 }
1412
Thomas Falcon032c5e82015-12-21 11:26:06 -06001413 tx_pool->consumer_index =
1414 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001415 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001416
1417 tx_buff = &tx_pool->tx_buff[index];
1418 tx_buff->skb = skb;
1419 tx_buff->data_dma[0] = data_dma_addr;
1420 tx_buff->data_len[0] = skb->len;
1421 tx_buff->index = index;
1422 tx_buff->pool_index = queue_num;
1423 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001424
1425 memset(&tx_crq, 0, sizeof(tx_crq));
1426 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1427 tx_crq.v1.type = IBMVNIC_TX_DESC;
1428 tx_crq.v1.n_crq_elem = 1;
1429 tx_crq.v1.n_sge = 1;
1430 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1431 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001432 if (skb_is_gso(skb))
1433 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1434 else
1435 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001436 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1437 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1438
1439 if (adapter->vlan_header_insertion) {
1440 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1441 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1442 }
1443
1444 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001445 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1446 proto = ip_hdr(skb)->protocol;
1447 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1448 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1449 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001450 }
1451
Thomas Falcona0dca102018-01-18 19:29:48 -06001452 if (proto == IPPROTO_TCP)
1453 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1454 else if (proto == IPPROTO_UDP)
1455 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1456
Thomas Falconad7775d2016-04-01 17:20:34 -05001457 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001458 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001459 hdrs += 2;
1460 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001461 if (skb_is_gso(skb)) {
1462 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1463 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1464 hdrs += 2;
1465 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001466 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001467 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001468 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1469 tx_crq.v1.n_crq_elem = num_entries;
1470 tx_buff->indir_arr[0] = tx_crq;
1471 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1472 sizeof(tx_buff->indir_arr),
1473 DMA_TO_DEVICE);
1474 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001475 dev_kfree_skb_any(skb);
1476 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001477 if (!firmware_has_feature(FW_FEATURE_CMO))
1478 dev_err(dev, "tx: unable to map descriptor array\n");
1479 tx_map_failed++;
1480 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001481 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001482 goto out;
1483 }
John Allen498cd8e2016-04-06 11:49:55 -05001484 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001485 (u64)tx_buff->indir_dma,
1486 (u64)num_entries);
1487 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001488 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1489 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001490 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001491 if (lpar_rc != H_SUCCESS) {
1492 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1493
1494 if (tx_pool->consumer_index == 0)
1495 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001496 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001497 else
1498 tx_pool->consumer_index--;
1499
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001500 dev_kfree_skb_any(skb);
1501 tx_buff->skb = NULL;
1502
Thomas Falconb8c80b82017-05-26 10:30:42 -04001503 if (lpar_rc == H_CLOSED) {
1504 /* Disable TX and report carrier off if queue is closed.
1505 * Firmware guarantees that a signal will be sent to the
1506 * driver, triggering a reset or some other action.
1507 */
1508 netif_tx_stop_all_queues(netdev);
1509 netif_carrier_off(netdev);
1510 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001511
Thomas Falcon032c5e82015-12-21 11:26:06 -06001512 tx_send_failed++;
1513 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001514 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001515 goto out;
1516 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001517
Brian King58c8c0c2017-04-19 13:44:47 -04001518 if (atomic_inc_return(&tx_scrq->used)
1519 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001520 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1521 netif_stop_subqueue(netdev, queue_num);
1522 }
1523
Thomas Falcon032c5e82015-12-21 11:26:06 -06001524 tx_packets++;
1525 tx_bytes += skb->len;
1526 txq->trans_start = jiffies;
1527 ret = NETDEV_TX_OK;
1528
1529out:
1530 netdev->stats.tx_dropped += tx_dropped;
1531 netdev->stats.tx_bytes += tx_bytes;
1532 netdev->stats.tx_packets += tx_packets;
1533 adapter->tx_send_failed += tx_send_failed;
1534 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001535 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1536 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1537 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001538
1539 return ret;
1540}
1541
1542static void ibmvnic_set_multi(struct net_device *netdev)
1543{
1544 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1545 struct netdev_hw_addr *ha;
1546 union ibmvnic_crq crq;
1547
1548 memset(&crq, 0, sizeof(crq));
1549 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1550 crq.request_capability.cmd = REQUEST_CAPABILITY;
1551
1552 if (netdev->flags & IFF_PROMISC) {
1553 if (!adapter->promisc_supported)
1554 return;
1555 } else {
1556 if (netdev->flags & IFF_ALLMULTI) {
1557 /* Accept all multicast */
1558 memset(&crq, 0, sizeof(crq));
1559 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1560 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1561 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1562 ibmvnic_send_crq(adapter, &crq);
1563 } else if (netdev_mc_empty(netdev)) {
1564 /* Reject all multicast */
1565 memset(&crq, 0, sizeof(crq));
1566 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1567 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1568 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1569 ibmvnic_send_crq(adapter, &crq);
1570 } else {
1571 /* Accept one or more multicast(s) */
1572 netdev_for_each_mc_addr(ha, netdev) {
1573 memset(&crq, 0, sizeof(crq));
1574 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1575 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1576 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1577 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1578 ha->addr);
1579 ibmvnic_send_crq(adapter, &crq);
1580 }
1581 }
1582 }
1583}
1584
John Allenc26eba02017-10-26 16:23:25 -05001585static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001586{
1587 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1588 struct sockaddr *addr = p;
1589 union ibmvnic_crq crq;
1590
1591 if (!is_valid_ether_addr(addr->sa_data))
1592 return -EADDRNOTAVAIL;
1593
1594 memset(&crq, 0, sizeof(crq));
1595 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1596 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1597 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001598
1599 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001600 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001601 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001602 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001603 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001604}
1605
John Allenc26eba02017-10-26 16:23:25 -05001606static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1607{
1608 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1609 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001610 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001611
Thomas Falcon3d166132018-01-10 19:39:52 -06001612 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001613 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1614 adapter->mac_change_pending = true;
1615 return 0;
1616 }
1617
Thomas Falconf8136142018-01-29 13:45:05 -06001618 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001619
Thomas Falconf8136142018-01-29 13:45:05 -06001620 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001621}
1622
Nathan Fontenoted651a12017-05-03 14:04:38 -04001623/**
1624 * do_reset returns zero if we are able to keep processing reset events, or
1625 * non-zero if we hit a fatal error and must halt.
1626 */
1627static int do_reset(struct ibmvnic_adapter *adapter,
1628 struct ibmvnic_rwi *rwi, u32 reset_state)
1629{
John Allen896d8692018-01-18 16:26:31 -06001630 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001631 struct net_device *netdev = adapter->netdev;
1632 int i, rc;
1633
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001634 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1635 rwi->reset_reason);
1636
Nathan Fontenoted651a12017-05-03 14:04:38 -04001637 netif_carrier_off(netdev);
1638 adapter->reset_reason = rwi->reset_reason;
1639
John Allen896d8692018-01-18 16:26:31 -06001640 old_num_rx_queues = adapter->req_rx_queues;
1641 old_num_tx_queues = adapter->req_tx_queues;
1642
Nathan Fontenoted651a12017-05-03 14:04:38 -04001643 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1644 rc = ibmvnic_reenable_crq_queue(adapter);
1645 if (rc)
1646 return 0;
1647 }
1648
1649 rc = __ibmvnic_close(netdev);
1650 if (rc)
1651 return rc;
1652
John Allenc26eba02017-10-26 16:23:25 -05001653 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1654 adapter->wait_for_reset) {
1655 release_resources(adapter);
1656 release_sub_crqs(adapter);
1657 release_crq_queue(adapter);
1658 }
1659
John Allen8cb31cf2017-05-26 10:30:37 -04001660 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1661 /* remove the closed state so when we call open it appears
1662 * we are coming from the probed state.
1663 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001664 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001665
John Allen8cb31cf2017-05-26 10:30:37 -04001666 rc = ibmvnic_init(adapter);
1667 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001668 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001669
1670 /* If the adapter was in PROBE state prior to the reset,
1671 * exit here.
1672 */
1673 if (reset_state == VNIC_PROBED)
1674 return 0;
1675
1676 rc = ibmvnic_login(netdev);
1677 if (rc) {
1678 adapter->state = VNIC_PROBED;
1679 return 0;
1680 }
1681
John Allenc26eba02017-10-26 16:23:25 -05001682 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1683 adapter->wait_for_reset) {
1684 rc = init_resources(adapter);
1685 if (rc)
1686 return rc;
John Allen896d8692018-01-18 16:26:31 -06001687 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1688 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001689 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001690 release_rx_pools(adapter);
1691 release_tx_pools(adapter);
1692 init_rx_pools(netdev);
1693 init_tx_pools(netdev);
John Allenc26eba02017-10-26 16:23:25 -05001694 } else {
1695 rc = reset_tx_pools(adapter);
1696 if (rc)
1697 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001698
John Allenc26eba02017-10-26 16:23:25 -05001699 rc = reset_rx_pools(adapter);
1700 if (rc)
1701 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001702
John Allenc26eba02017-10-26 16:23:25 -05001703 if (reset_state == VNIC_CLOSED)
1704 return 0;
1705 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001706 }
1707
Nathan Fontenoted651a12017-05-03 14:04:38 -04001708 rc = __ibmvnic_open(netdev);
1709 if (rc) {
1710 if (list_empty(&adapter->rwi_list))
1711 adapter->state = VNIC_CLOSED;
1712 else
1713 adapter->state = reset_state;
1714
1715 return 0;
1716 }
1717
Nathan Fontenoted651a12017-05-03 14:04:38 -04001718 /* kick napi */
1719 for (i = 0; i < adapter->req_rx_queues; i++)
1720 napi_schedule(&adapter->napi[i]);
1721
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001722 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1723 netdev_notify_peers(netdev);
1724
Thomas Falconcc85c022018-02-13 15:32:50 -06001725 netif_carrier_on(netdev);
1726
Nathan Fontenoted651a12017-05-03 14:04:38 -04001727 return 0;
1728}
1729
1730static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1731{
1732 struct ibmvnic_rwi *rwi;
1733
1734 mutex_lock(&adapter->rwi_lock);
1735
1736 if (!list_empty(&adapter->rwi_list)) {
1737 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1738 list);
1739 list_del(&rwi->list);
1740 } else {
1741 rwi = NULL;
1742 }
1743
1744 mutex_unlock(&adapter->rwi_lock);
1745 return rwi;
1746}
1747
1748static void free_all_rwi(struct ibmvnic_adapter *adapter)
1749{
1750 struct ibmvnic_rwi *rwi;
1751
1752 rwi = get_next_rwi(adapter);
1753 while (rwi) {
1754 kfree(rwi);
1755 rwi = get_next_rwi(adapter);
1756 }
1757}
1758
1759static void __ibmvnic_reset(struct work_struct *work)
1760{
1761 struct ibmvnic_rwi *rwi;
1762 struct ibmvnic_adapter *adapter;
1763 struct net_device *netdev;
1764 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001765 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001766
1767 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1768 netdev = adapter->netdev;
1769
1770 mutex_lock(&adapter->reset_lock);
1771 adapter->resetting = true;
1772 reset_state = adapter->state;
1773
1774 rwi = get_next_rwi(adapter);
1775 while (rwi) {
1776 rc = do_reset(adapter, rwi, reset_state);
1777 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001778 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001779 break;
1780
1781 rwi = get_next_rwi(adapter);
1782 }
1783
John Allenc26eba02017-10-26 16:23:25 -05001784 if (adapter->wait_for_reset) {
1785 adapter->wait_for_reset = false;
1786 adapter->reset_done_rc = rc;
1787 complete(&adapter->reset_done);
1788 }
1789
Nathan Fontenoted651a12017-05-03 14:04:38 -04001790 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001791 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001792 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001793 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001794 return;
1795 }
1796
1797 adapter->resetting = false;
1798 mutex_unlock(&adapter->reset_lock);
1799}
1800
1801static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1802 enum ibmvnic_reset_reason reason)
1803{
1804 struct ibmvnic_rwi *rwi, *tmp;
1805 struct net_device *netdev = adapter->netdev;
1806 struct list_head *entry;
1807
1808 if (adapter->state == VNIC_REMOVING ||
1809 adapter->state == VNIC_REMOVED) {
1810 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1811 return;
1812 }
1813
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001814 if (adapter->state == VNIC_PROBING) {
1815 netdev_warn(netdev, "Adapter reset during probe\n");
1816 adapter->init_done_rc = EAGAIN;
1817 return;
1818 }
1819
Nathan Fontenoted651a12017-05-03 14:04:38 -04001820 mutex_lock(&adapter->rwi_lock);
1821
1822 list_for_each(entry, &adapter->rwi_list) {
1823 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1824 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001825 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001826 mutex_unlock(&adapter->rwi_lock);
1827 return;
1828 }
1829 }
1830
1831 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1832 if (!rwi) {
1833 mutex_unlock(&adapter->rwi_lock);
1834 ibmvnic_close(netdev);
1835 return;
1836 }
1837
1838 rwi->reset_reason = reason;
1839 list_add_tail(&rwi->list, &adapter->rwi_list);
1840 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001841
1842 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001843 schedule_work(&adapter->ibmvnic_reset);
1844}
1845
Thomas Falcon032c5e82015-12-21 11:26:06 -06001846static void ibmvnic_tx_timeout(struct net_device *dev)
1847{
1848 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001849
Nathan Fontenoted651a12017-05-03 14:04:38 -04001850 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001851}
1852
1853static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1854 struct ibmvnic_rx_buff *rx_buff)
1855{
1856 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1857
1858 rx_buff->skb = NULL;
1859
1860 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1861 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1862
1863 atomic_dec(&pool->available);
1864}
1865
1866static int ibmvnic_poll(struct napi_struct *napi, int budget)
1867{
1868 struct net_device *netdev = napi->dev;
1869 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1870 int scrq_num = (int)(napi - adapter->napi);
1871 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001872
Thomas Falcon032c5e82015-12-21 11:26:06 -06001873restart_poll:
1874 while (frames_processed < budget) {
1875 struct sk_buff *skb;
1876 struct ibmvnic_rx_buff *rx_buff;
1877 union sub_crq *next;
1878 u32 length;
1879 u16 offset;
1880 u8 flags = 0;
1881
John Allen34686562018-02-06 16:21:49 -06001882 if (unlikely(adapter->resetting &&
1883 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001884 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1885 napi_complete_done(napi, frames_processed);
1886 return frames_processed;
1887 }
1888
Thomas Falcon032c5e82015-12-21 11:26:06 -06001889 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1890 break;
1891 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1892 rx_buff =
1893 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1894 rx_comp.correlator);
1895 /* do error checking */
1896 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001897 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1898 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001899 /* free the entry */
1900 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001901 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001902 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001903 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06001904 } else if (!rx_buff->skb) {
1905 /* free the entry */
1906 next->rx_comp.first = 0;
1907 remove_buff_from_pool(adapter, rx_buff);
1908 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001909 }
1910
1911 length = be32_to_cpu(next->rx_comp.len);
1912 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1913 flags = next->rx_comp.flags;
1914 skb = rx_buff->skb;
1915 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1916 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001917
1918 /* VLAN Header has been stripped by the system firmware and
1919 * needs to be inserted by the driver
1920 */
1921 if (adapter->rx_vlan_header_insertion &&
1922 (flags & IBMVNIC_VLAN_STRIPPED))
1923 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1924 ntohs(next->rx_comp.vlan_tci));
1925
Thomas Falcon032c5e82015-12-21 11:26:06 -06001926 /* free the entry */
1927 next->rx_comp.first = 0;
1928 remove_buff_from_pool(adapter, rx_buff);
1929
1930 skb_put(skb, length);
1931 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001932 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001933
1934 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1935 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1936 skb->ip_summed = CHECKSUM_UNNECESSARY;
1937 }
1938
1939 length = skb->len;
1940 napi_gro_receive(napi, skb); /* send it up */
1941 netdev->stats.rx_packets++;
1942 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001943 adapter->rx_stats_buffers[scrq_num].packets++;
1944 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001945 frames_processed++;
1946 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001947
1948 if (adapter->state != VNIC_CLOSING)
1949 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001950
1951 if (frames_processed < budget) {
1952 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001953 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001954 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1955 napi_reschedule(napi)) {
1956 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1957 goto restart_poll;
1958 }
1959 }
1960 return frames_processed;
1961}
1962
1963#ifdef CONFIG_NET_POLL_CONTROLLER
1964static void ibmvnic_netpoll_controller(struct net_device *dev)
1965{
1966 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1967 int i;
1968
1969 replenish_pools(netdev_priv(dev));
1970 for (i = 0; i < adapter->req_rx_queues; i++)
1971 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1972 adapter->rx_scrq[i]);
1973}
1974#endif
1975
John Allenc26eba02017-10-26 16:23:25 -05001976static int wait_for_reset(struct ibmvnic_adapter *adapter)
1977{
1978 adapter->fallback.mtu = adapter->req_mtu;
1979 adapter->fallback.rx_queues = adapter->req_rx_queues;
1980 adapter->fallback.tx_queues = adapter->req_tx_queues;
1981 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1982 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1983
1984 init_completion(&adapter->reset_done);
1985 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1986 adapter->wait_for_reset = true;
1987 wait_for_completion(&adapter->reset_done);
1988
1989 if (adapter->reset_done_rc) {
1990 adapter->desired.mtu = adapter->fallback.mtu;
1991 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1992 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1993 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1994 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1995
1996 init_completion(&adapter->reset_done);
1997 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1998 wait_for_completion(&adapter->reset_done);
1999 }
2000 adapter->wait_for_reset = false;
2001
2002 return adapter->reset_done_rc;
2003}
2004
John Allen3a807b72017-06-06 16:55:52 -05002005static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2006{
John Allenc26eba02017-10-26 16:23:25 -05002007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2008
2009 adapter->desired.mtu = new_mtu + ETH_HLEN;
2010
2011 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002012}
2013
Thomas Falcon032c5e82015-12-21 11:26:06 -06002014static const struct net_device_ops ibmvnic_netdev_ops = {
2015 .ndo_open = ibmvnic_open,
2016 .ndo_stop = ibmvnic_close,
2017 .ndo_start_xmit = ibmvnic_xmit,
2018 .ndo_set_rx_mode = ibmvnic_set_multi,
2019 .ndo_set_mac_address = ibmvnic_set_mac,
2020 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002021 .ndo_tx_timeout = ibmvnic_tx_timeout,
2022#ifdef CONFIG_NET_POLL_CONTROLLER
2023 .ndo_poll_controller = ibmvnic_netpoll_controller,
2024#endif
John Allen3a807b72017-06-06 16:55:52 -05002025 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002026};
2027
2028/* ethtool functions */
2029
Philippe Reynes8a433792017-01-07 22:37:29 +01002030static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2031 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002032{
Philippe Reynes8a433792017-01-07 22:37:29 +01002033 u32 supported, advertising;
2034
2035 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002036 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002037 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002038 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002039 cmd->base.speed = SPEED_1000;
2040 cmd->base.duplex = DUPLEX_FULL;
2041 cmd->base.port = PORT_FIBRE;
2042 cmd->base.phy_address = 0;
2043 cmd->base.autoneg = AUTONEG_ENABLE;
2044
2045 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2046 supported);
2047 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2048 advertising);
2049
Thomas Falcon032c5e82015-12-21 11:26:06 -06002050 return 0;
2051}
2052
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002053static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002054 struct ethtool_drvinfo *info)
2055{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002056 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2057
Thomas Falcon032c5e82015-12-21 11:26:06 -06002058 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2059 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002060 strlcpy(info->fw_version, adapter->fw_version,
2061 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002062}
2063
2064static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2065{
2066 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2067
2068 return adapter->msg_enable;
2069}
2070
2071static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2072{
2073 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2074
2075 adapter->msg_enable = data;
2076}
2077
2078static u32 ibmvnic_get_link(struct net_device *netdev)
2079{
2080 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2081
2082 /* Don't need to send a query because we request a logical link up at
2083 * init and then we wait for link state indications
2084 */
2085 return adapter->logical_link_state;
2086}
2087
2088static void ibmvnic_get_ringparam(struct net_device *netdev,
2089 struct ethtool_ringparam *ring)
2090{
John Allenbc131b32017-08-02 16:46:30 -05002091 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2092
2093 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2094 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002095 ring->rx_mini_max_pending = 0;
2096 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002097 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2098 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002099 ring->rx_mini_pending = 0;
2100 ring->rx_jumbo_pending = 0;
2101}
2102
John Allenc26eba02017-10-26 16:23:25 -05002103static int ibmvnic_set_ringparam(struct net_device *netdev,
2104 struct ethtool_ringparam *ring)
2105{
2106 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2107
2108 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2109 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2110 netdev_err(netdev, "Invalid request.\n");
2111 netdev_err(netdev, "Max tx buffers = %llu\n",
2112 adapter->max_rx_add_entries_per_subcrq);
2113 netdev_err(netdev, "Max rx buffers = %llu\n",
2114 adapter->max_tx_entries_per_subcrq);
2115 return -EINVAL;
2116 }
2117
2118 adapter->desired.rx_entries = ring->rx_pending;
2119 adapter->desired.tx_entries = ring->tx_pending;
2120
2121 return wait_for_reset(adapter);
2122}
2123
John Allenc2dbeb62017-08-02 16:47:17 -05002124static void ibmvnic_get_channels(struct net_device *netdev,
2125 struct ethtool_channels *channels)
2126{
2127 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2128
2129 channels->max_rx = adapter->max_rx_queues;
2130 channels->max_tx = adapter->max_tx_queues;
2131 channels->max_other = 0;
2132 channels->max_combined = 0;
2133 channels->rx_count = adapter->req_rx_queues;
2134 channels->tx_count = adapter->req_tx_queues;
2135 channels->other_count = 0;
2136 channels->combined_count = 0;
2137}
2138
John Allenc26eba02017-10-26 16:23:25 -05002139static int ibmvnic_set_channels(struct net_device *netdev,
2140 struct ethtool_channels *channels)
2141{
2142 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2143
2144 adapter->desired.rx_queues = channels->rx_count;
2145 adapter->desired.tx_queues = channels->tx_count;
2146
2147 return wait_for_reset(adapter);
2148}
2149
Thomas Falcon032c5e82015-12-21 11:26:06 -06002150static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2151{
John Allen3d52b592017-08-02 16:44:14 -05002152 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002153 int i;
2154
2155 if (stringset != ETH_SS_STATS)
2156 return;
2157
2158 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2159 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002160
2161 for (i = 0; i < adapter->req_tx_queues; i++) {
2162 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2163 data += ETH_GSTRING_LEN;
2164
2165 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2166 data += ETH_GSTRING_LEN;
2167
2168 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2169 data += ETH_GSTRING_LEN;
2170 }
2171
2172 for (i = 0; i < adapter->req_rx_queues; i++) {
2173 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2174 data += ETH_GSTRING_LEN;
2175
2176 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2177 data += ETH_GSTRING_LEN;
2178
2179 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2180 data += ETH_GSTRING_LEN;
2181 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002182}
2183
2184static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2185{
John Allen3d52b592017-08-02 16:44:14 -05002186 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2187
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188 switch (sset) {
2189 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002190 return ARRAY_SIZE(ibmvnic_stats) +
2191 adapter->req_tx_queues * NUM_TX_STATS +
2192 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002193 default:
2194 return -EOPNOTSUPP;
2195 }
2196}
2197
2198static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2199 struct ethtool_stats *stats, u64 *data)
2200{
2201 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2202 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002203 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002204
2205 memset(&crq, 0, sizeof(crq));
2206 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2207 crq.request_statistics.cmd = REQUEST_STATISTICS;
2208 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2209 crq.request_statistics.len =
2210 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002211
2212 /* Wait for data to be written */
2213 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002214 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002215 wait_for_completion(&adapter->stats_done);
2216
2217 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002218 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2219 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002220
2221 for (j = 0; j < adapter->req_tx_queues; j++) {
2222 data[i] = adapter->tx_stats_buffers[j].packets;
2223 i++;
2224 data[i] = adapter->tx_stats_buffers[j].bytes;
2225 i++;
2226 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2227 i++;
2228 }
2229
2230 for (j = 0; j < adapter->req_rx_queues; j++) {
2231 data[i] = adapter->rx_stats_buffers[j].packets;
2232 i++;
2233 data[i] = adapter->rx_stats_buffers[j].bytes;
2234 i++;
2235 data[i] = adapter->rx_stats_buffers[j].interrupts;
2236 i++;
2237 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002238}
2239
2240static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002241 .get_drvinfo = ibmvnic_get_drvinfo,
2242 .get_msglevel = ibmvnic_get_msglevel,
2243 .set_msglevel = ibmvnic_set_msglevel,
2244 .get_link = ibmvnic_get_link,
2245 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002246 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002247 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002248 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002249 .get_strings = ibmvnic_get_strings,
2250 .get_sset_count = ibmvnic_get_sset_count,
2251 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002252 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002253};
2254
2255/* Routines for managing CRQs/sCRQs */
2256
Nathan Fontenot57a49432017-05-26 10:31:12 -04002257static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2258 struct ibmvnic_sub_crq_queue *scrq)
2259{
2260 int rc;
2261
2262 if (scrq->irq) {
2263 free_irq(scrq->irq, scrq);
2264 irq_dispose_mapping(scrq->irq);
2265 scrq->irq = 0;
2266 }
2267
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002268 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002269 scrq->cur = 0;
2270
2271 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2272 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2273 return rc;
2274}
2275
2276static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2277{
2278 int i, rc;
2279
2280 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002281 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002282 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2283 if (rc)
2284 return rc;
2285 }
2286
2287 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002288 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002289 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2290 if (rc)
2291 return rc;
2292 }
2293
Nathan Fontenot57a49432017-05-26 10:31:12 -04002294 return rc;
2295}
2296
Thomas Falcon032c5e82015-12-21 11:26:06 -06002297static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2298 struct ibmvnic_sub_crq_queue *scrq)
2299{
2300 struct device *dev = &adapter->vdev->dev;
2301 long rc;
2302
2303 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2304
2305 /* Close the sub-crqs */
2306 do {
2307 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2308 adapter->vdev->unit_address,
2309 scrq->crq_num);
2310 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2311
Thomas Falconffa73852017-04-19 13:44:29 -04002312 if (rc) {
2313 netdev_err(adapter->netdev,
2314 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2315 scrq->crq_num, rc);
2316 }
2317
Thomas Falcon032c5e82015-12-21 11:26:06 -06002318 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2319 DMA_BIDIRECTIONAL);
2320 free_pages((unsigned long)scrq->msgs, 2);
2321 kfree(scrq);
2322}
2323
2324static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2325 *adapter)
2326{
2327 struct device *dev = &adapter->vdev->dev;
2328 struct ibmvnic_sub_crq_queue *scrq;
2329 int rc;
2330
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002331 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002332 if (!scrq)
2333 return NULL;
2334
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002335 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002336 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002337 if (!scrq->msgs) {
2338 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2339 goto zero_page_failed;
2340 }
2341
2342 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2343 DMA_BIDIRECTIONAL);
2344 if (dma_mapping_error(dev, scrq->msg_token)) {
2345 dev_warn(dev, "Couldn't map crq queue messages page\n");
2346 goto map_failed;
2347 }
2348
2349 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2350 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2351
2352 if (rc == H_RESOURCE)
2353 rc = ibmvnic_reset_crq(adapter);
2354
2355 if (rc == H_CLOSED) {
2356 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2357 } else if (rc) {
2358 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2359 goto reg_failed;
2360 }
2361
Thomas Falcon032c5e82015-12-21 11:26:06 -06002362 scrq->adapter = adapter;
2363 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002364 spin_lock_init(&scrq->lock);
2365
2366 netdev_dbg(adapter->netdev,
2367 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2368 scrq->crq_num, scrq->hw_irq, scrq->irq);
2369
2370 return scrq;
2371
Thomas Falcon032c5e82015-12-21 11:26:06 -06002372reg_failed:
2373 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2374 DMA_BIDIRECTIONAL);
2375map_failed:
2376 free_pages((unsigned long)scrq->msgs, 2);
2377zero_page_failed:
2378 kfree(scrq);
2379
2380 return NULL;
2381}
2382
2383static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2384{
2385 int i;
2386
2387 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002388 for (i = 0; i < adapter->req_tx_queues; i++) {
2389 if (!adapter->tx_scrq[i])
2390 continue;
2391
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002392 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2393 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002394 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002395 free_irq(adapter->tx_scrq[i]->irq,
2396 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002397 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002398 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002399 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002400
2401 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2402 }
2403
Nathan Fontenot9501df32017-03-15 23:38:07 -04002404 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002405 adapter->tx_scrq = NULL;
2406 }
2407
2408 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002409 for (i = 0; i < adapter->req_rx_queues; i++) {
2410 if (!adapter->rx_scrq[i])
2411 continue;
2412
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002413 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2414 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002415 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 free_irq(adapter->rx_scrq[i]->irq,
2417 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002418 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002419 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002421
2422 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2423 }
2424
Nathan Fontenot9501df32017-03-15 23:38:07 -04002425 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002426 adapter->rx_scrq = NULL;
2427 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002428}
2429
2430static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2431 struct ibmvnic_sub_crq_queue *scrq)
2432{
2433 struct device *dev = &adapter->vdev->dev;
2434 unsigned long rc;
2435
2436 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2437 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2438 if (rc)
2439 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2440 scrq->hw_irq, rc);
2441 return rc;
2442}
2443
2444static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2445 struct ibmvnic_sub_crq_queue *scrq)
2446{
2447 struct device *dev = &adapter->vdev->dev;
2448 unsigned long rc;
2449
2450 if (scrq->hw_irq > 0x100000000ULL) {
2451 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2452 return 1;
2453 }
2454
2455 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2456 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2457 if (rc)
2458 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2459 scrq->hw_irq, rc);
2460 return rc;
2461}
2462
2463static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2464 struct ibmvnic_sub_crq_queue *scrq)
2465{
2466 struct device *dev = &adapter->vdev->dev;
2467 struct ibmvnic_tx_buff *txbuff;
2468 union sub_crq *next;
2469 int index;
2470 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002471 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002472
2473restart_loop:
2474 while (pending_scrq(adapter, scrq)) {
2475 unsigned int pool = scrq->pool_index;
2476
2477 next = ibmvnic_next_scrq(adapter, scrq);
2478 for (i = 0; i < next->tx_comp.num_comps; i++) {
2479 if (next->tx_comp.rcs[i]) {
2480 dev_err(dev, "tx error %x\n",
2481 next->tx_comp.rcs[i]);
2482 continue;
2483 }
2484 index = be32_to_cpu(next->tx_comp.correlators[i]);
2485 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2486
2487 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2488 if (!txbuff->data_dma[j])
2489 continue;
2490
2491 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002492 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002493 /* if sub_crq was sent indirectly */
2494 first = txbuff->indir_arr[0].generic.first;
2495 if (first == IBMVNIC_CRQ_CMD) {
2496 dma_unmap_single(dev, txbuff->indir_dma,
2497 sizeof(txbuff->indir_arr),
2498 DMA_TO_DEVICE);
2499 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002500
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002501 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002502 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002503 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002504 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002505
2506 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2507 producer_index] = index;
2508 adapter->tx_pool[pool].producer_index =
2509 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002510 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002511 }
2512 /* remove tx_comp scrq*/
2513 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002514
2515 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2516 (adapter->req_tx_entries_per_subcrq / 2) &&
2517 __netif_subqueue_stopped(adapter->netdev,
2518 scrq->pool_index)) {
2519 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2520 netdev_info(adapter->netdev, "Started queue %d\n",
2521 scrq->pool_index);
2522 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002523 }
2524
2525 enable_scrq_irq(adapter, scrq);
2526
2527 if (pending_scrq(adapter, scrq)) {
2528 disable_scrq_irq(adapter, scrq);
2529 goto restart_loop;
2530 }
2531
2532 return 0;
2533}
2534
2535static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2536{
2537 struct ibmvnic_sub_crq_queue *scrq = instance;
2538 struct ibmvnic_adapter *adapter = scrq->adapter;
2539
2540 disable_scrq_irq(adapter, scrq);
2541 ibmvnic_complete_tx(adapter, scrq);
2542
2543 return IRQ_HANDLED;
2544}
2545
2546static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2547{
2548 struct ibmvnic_sub_crq_queue *scrq = instance;
2549 struct ibmvnic_adapter *adapter = scrq->adapter;
2550
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002551 /* When booting a kdump kernel we can hit pending interrupts
2552 * prior to completing driver initialization.
2553 */
2554 if (unlikely(adapter->state != VNIC_OPEN))
2555 return IRQ_NONE;
2556
John Allen3d52b592017-08-02 16:44:14 -05002557 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2558
Thomas Falcon032c5e82015-12-21 11:26:06 -06002559 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2560 disable_scrq_irq(adapter, scrq);
2561 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2562 }
2563
2564 return IRQ_HANDLED;
2565}
2566
Thomas Falconea22d512016-07-06 15:35:17 -05002567static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2568{
2569 struct device *dev = &adapter->vdev->dev;
2570 struct ibmvnic_sub_crq_queue *scrq;
2571 int i = 0, j = 0;
2572 int rc = 0;
2573
2574 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002575 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2576 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002577 scrq = adapter->tx_scrq[i];
2578 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2579
Michael Ellerman99c17902016-09-10 19:59:05 +10002580 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002581 rc = -EINVAL;
2582 dev_err(dev, "Error mapping irq\n");
2583 goto req_tx_irq_failed;
2584 }
2585
2586 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2587 0, "ibmvnic_tx", scrq);
2588
2589 if (rc) {
2590 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2591 scrq->irq, rc);
2592 irq_dispose_mapping(scrq->irq);
2593 goto req_rx_irq_failed;
2594 }
2595 }
2596
2597 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002598 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2599 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002600 scrq = adapter->rx_scrq[i];
2601 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002602 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002603 rc = -EINVAL;
2604 dev_err(dev, "Error mapping irq\n");
2605 goto req_rx_irq_failed;
2606 }
2607 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2608 0, "ibmvnic_rx", scrq);
2609 if (rc) {
2610 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2611 scrq->irq, rc);
2612 irq_dispose_mapping(scrq->irq);
2613 goto req_rx_irq_failed;
2614 }
2615 }
2616 return rc;
2617
2618req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002619 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002620 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2621 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002622 }
Thomas Falconea22d512016-07-06 15:35:17 -05002623 i = adapter->req_tx_queues;
2624req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002625 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002626 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2627 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002628 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002629 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002630 return rc;
2631}
2632
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002633static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002634{
2635 struct device *dev = &adapter->vdev->dev;
2636 struct ibmvnic_sub_crq_queue **allqueues;
2637 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002638 int total_queues;
2639 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002640 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002641
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2643
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002644 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002645 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002646 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002647
2648 for (i = 0; i < total_queues; i++) {
2649 allqueues[i] = init_sub_crq_queue(adapter);
2650 if (!allqueues[i]) {
2651 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2652 break;
2653 }
2654 registered_queues++;
2655 }
2656
2657 /* Make sure we were able to register the minimum number of queues */
2658 if (registered_queues <
2659 adapter->min_tx_queues + adapter->min_rx_queues) {
2660 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2661 goto tx_failed;
2662 }
2663
2664 /* Distribute the failed allocated queues*/
2665 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2666 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2667 switch (i % 3) {
2668 case 0:
2669 if (adapter->req_rx_queues > adapter->min_rx_queues)
2670 adapter->req_rx_queues--;
2671 else
2672 more++;
2673 break;
2674 case 1:
2675 if (adapter->req_tx_queues > adapter->min_tx_queues)
2676 adapter->req_tx_queues--;
2677 else
2678 more++;
2679 break;
2680 }
2681 }
2682
2683 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002684 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002685 if (!adapter->tx_scrq)
2686 goto tx_failed;
2687
2688 for (i = 0; i < adapter->req_tx_queues; i++) {
2689 adapter->tx_scrq[i] = allqueues[i];
2690 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002691 }
2692
2693 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002694 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 if (!adapter->rx_scrq)
2696 goto rx_failed;
2697
2698 for (i = 0; i < adapter->req_rx_queues; i++) {
2699 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2700 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002701 }
2702
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002703 kfree(allqueues);
2704 return 0;
2705
2706rx_failed:
2707 kfree(adapter->tx_scrq);
2708 adapter->tx_scrq = NULL;
2709tx_failed:
2710 for (i = 0; i < registered_queues; i++)
2711 release_sub_crq_queue(adapter, allqueues[i]);
2712 kfree(allqueues);
2713 return -1;
2714}
2715
2716static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2717{
2718 struct device *dev = &adapter->vdev->dev;
2719 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002720 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002721
2722 if (!retry) {
2723 /* Sub-CRQ entries are 32 byte long */
2724 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2725
2726 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2727 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2728 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2729 return;
2730 }
2731
John Allenc26eba02017-10-26 16:23:25 -05002732 if (adapter->desired.mtu)
2733 adapter->req_mtu = adapter->desired.mtu;
2734 else
2735 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002736
John Allenc26eba02017-10-26 16:23:25 -05002737 if (!adapter->desired.tx_entries)
2738 adapter->desired.tx_entries =
2739 adapter->max_tx_entries_per_subcrq;
2740 if (!adapter->desired.rx_entries)
2741 adapter->desired.rx_entries =
2742 adapter->max_rx_add_entries_per_subcrq;
2743
2744 max_entries = IBMVNIC_MAX_LTB_SIZE /
2745 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2746
2747 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2748 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2749 adapter->desired.tx_entries = max_entries;
2750 }
2751
2752 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2753 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2754 adapter->desired.rx_entries = max_entries;
2755 }
2756
2757 if (adapter->desired.tx_entries)
2758 adapter->req_tx_entries_per_subcrq =
2759 adapter->desired.tx_entries;
2760 else
2761 adapter->req_tx_entries_per_subcrq =
2762 adapter->max_tx_entries_per_subcrq;
2763
2764 if (adapter->desired.rx_entries)
2765 adapter->req_rx_add_entries_per_subcrq =
2766 adapter->desired.rx_entries;
2767 else
2768 adapter->req_rx_add_entries_per_subcrq =
2769 adapter->max_rx_add_entries_per_subcrq;
2770
2771 if (adapter->desired.tx_queues)
2772 adapter->req_tx_queues =
2773 adapter->desired.tx_queues;
2774 else
2775 adapter->req_tx_queues =
2776 adapter->opt_tx_comp_sub_queues;
2777
2778 if (adapter->desired.rx_queues)
2779 adapter->req_rx_queues =
2780 adapter->desired.rx_queues;
2781 else
2782 adapter->req_rx_queues =
2783 adapter->opt_rx_comp_queues;
2784
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002785 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002786 }
2787
Thomas Falcon032c5e82015-12-21 11:26:06 -06002788 memset(&crq, 0, sizeof(crq));
2789 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2790 crq.request_capability.cmd = REQUEST_CAPABILITY;
2791
2792 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002793 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002794 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002795 ibmvnic_send_crq(adapter, &crq);
2796
2797 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002798 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002799 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002800 ibmvnic_send_crq(adapter, &crq);
2801
2802 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002803 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002804 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 ibmvnic_send_crq(adapter, &crq);
2806
2807 crq.request_capability.capability =
2808 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2809 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002810 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002811 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002812 ibmvnic_send_crq(adapter, &crq);
2813
2814 crq.request_capability.capability =
2815 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2816 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002817 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002818 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 ibmvnic_send_crq(adapter, &crq);
2820
2821 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002822 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002823 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002824 ibmvnic_send_crq(adapter, &crq);
2825
2826 if (adapter->netdev->flags & IFF_PROMISC) {
2827 if (adapter->promisc_supported) {
2828 crq.request_capability.capability =
2829 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002830 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002831 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002832 ibmvnic_send_crq(adapter, &crq);
2833 }
2834 } else {
2835 crq.request_capability.capability =
2836 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002837 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002838 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002839 ibmvnic_send_crq(adapter, &crq);
2840 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841}
2842
2843static int pending_scrq(struct ibmvnic_adapter *adapter,
2844 struct ibmvnic_sub_crq_queue *scrq)
2845{
2846 union sub_crq *entry = &scrq->msgs[scrq->cur];
2847
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002848 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002849 return 1;
2850 else
2851 return 0;
2852}
2853
2854static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2855 struct ibmvnic_sub_crq_queue *scrq)
2856{
2857 union sub_crq *entry;
2858 unsigned long flags;
2859
2860 spin_lock_irqsave(&scrq->lock, flags);
2861 entry = &scrq->msgs[scrq->cur];
2862 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2863 if (++scrq->cur == scrq->size)
2864 scrq->cur = 0;
2865 } else {
2866 entry = NULL;
2867 }
2868 spin_unlock_irqrestore(&scrq->lock, flags);
2869
2870 return entry;
2871}
2872
2873static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2874{
2875 struct ibmvnic_crq_queue *queue = &adapter->crq;
2876 union ibmvnic_crq *crq;
2877
2878 crq = &queue->msgs[queue->cur];
2879 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2880 if (++queue->cur == queue->size)
2881 queue->cur = 0;
2882 } else {
2883 crq = NULL;
2884 }
2885
2886 return crq;
2887}
2888
2889static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2890 union sub_crq *sub_crq)
2891{
2892 unsigned int ua = adapter->vdev->unit_address;
2893 struct device *dev = &adapter->vdev->dev;
2894 u64 *u64_crq = (u64 *)sub_crq;
2895 int rc;
2896
2897 netdev_dbg(adapter->netdev,
2898 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2899 (unsigned long int)cpu_to_be64(remote_handle),
2900 (unsigned long int)cpu_to_be64(u64_crq[0]),
2901 (unsigned long int)cpu_to_be64(u64_crq[1]),
2902 (unsigned long int)cpu_to_be64(u64_crq[2]),
2903 (unsigned long int)cpu_to_be64(u64_crq[3]));
2904
2905 /* Make sure the hypervisor sees the complete request */
2906 mb();
2907
2908 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2909 cpu_to_be64(remote_handle),
2910 cpu_to_be64(u64_crq[0]),
2911 cpu_to_be64(u64_crq[1]),
2912 cpu_to_be64(u64_crq[2]),
2913 cpu_to_be64(u64_crq[3]));
2914
2915 if (rc) {
2916 if (rc == H_CLOSED)
2917 dev_warn(dev, "CRQ Queue closed\n");
2918 dev_err(dev, "Send error (rc=%d)\n", rc);
2919 }
2920
2921 return rc;
2922}
2923
Thomas Falconad7775d2016-04-01 17:20:34 -05002924static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2925 u64 remote_handle, u64 ioba, u64 num_entries)
2926{
2927 unsigned int ua = adapter->vdev->unit_address;
2928 struct device *dev = &adapter->vdev->dev;
2929 int rc;
2930
2931 /* Make sure the hypervisor sees the complete request */
2932 mb();
2933 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2934 cpu_to_be64(remote_handle),
2935 ioba, num_entries);
2936
2937 if (rc) {
2938 if (rc == H_CLOSED)
2939 dev_warn(dev, "CRQ Queue closed\n");
2940 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2941 }
2942
2943 return rc;
2944}
2945
Thomas Falcon032c5e82015-12-21 11:26:06 -06002946static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2947 union ibmvnic_crq *crq)
2948{
2949 unsigned int ua = adapter->vdev->unit_address;
2950 struct device *dev = &adapter->vdev->dev;
2951 u64 *u64_crq = (u64 *)crq;
2952 int rc;
2953
2954 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2955 (unsigned long int)cpu_to_be64(u64_crq[0]),
2956 (unsigned long int)cpu_to_be64(u64_crq[1]));
2957
2958 /* Make sure the hypervisor sees the complete request */
2959 mb();
2960
2961 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2962 cpu_to_be64(u64_crq[0]),
2963 cpu_to_be64(u64_crq[1]));
2964
2965 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002966 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002968 if (adapter->resetting)
2969 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2970 }
2971
Thomas Falcon032c5e82015-12-21 11:26:06 -06002972 dev_warn(dev, "Send error (rc=%d)\n", rc);
2973 }
2974
2975 return rc;
2976}
2977
2978static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2979{
2980 union ibmvnic_crq crq;
2981
2982 memset(&crq, 0, sizeof(crq));
2983 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2984 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2985 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2986
2987 return ibmvnic_send_crq(adapter, &crq);
2988}
2989
Thomas Falcon032c5e82015-12-21 11:26:06 -06002990static int send_version_xchg(struct ibmvnic_adapter *adapter)
2991{
2992 union ibmvnic_crq crq;
2993
2994 memset(&crq, 0, sizeof(crq));
2995 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2996 crq.version_exchange.cmd = VERSION_EXCHANGE;
2997 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2998
2999 return ibmvnic_send_crq(adapter, &crq);
3000}
3001
Nathan Fontenot37798d02017-11-08 11:23:56 -06003002struct vnic_login_client_data {
3003 u8 type;
3004 __be16 len;
3005 char name;
3006} __packed;
3007
3008static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3009{
3010 int len;
3011
3012 /* Calculate the amount of buffer space needed for the
3013 * vnic client data in the login buffer. There are four entries,
3014 * OS name, LPAR name, device name, and a null last entry.
3015 */
3016 len = 4 * sizeof(struct vnic_login_client_data);
3017 len += 6; /* "Linux" plus NULL */
3018 len += strlen(utsname()->nodename) + 1;
3019 len += strlen(adapter->netdev->name) + 1;
3020
3021 return len;
3022}
3023
3024static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3025 struct vnic_login_client_data *vlcd)
3026{
3027 const char *os_name = "Linux";
3028 int len;
3029
3030 /* Type 1 - LPAR OS */
3031 vlcd->type = 1;
3032 len = strlen(os_name) + 1;
3033 vlcd->len = cpu_to_be16(len);
3034 strncpy(&vlcd->name, os_name, len);
3035 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3036
3037 /* Type 2 - LPAR name */
3038 vlcd->type = 2;
3039 len = strlen(utsname()->nodename) + 1;
3040 vlcd->len = cpu_to_be16(len);
3041 strncpy(&vlcd->name, utsname()->nodename, len);
3042 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3043
3044 /* Type 3 - device name */
3045 vlcd->type = 3;
3046 len = strlen(adapter->netdev->name) + 1;
3047 vlcd->len = cpu_to_be16(len);
3048 strncpy(&vlcd->name, adapter->netdev->name, len);
3049}
3050
Thomas Falcon032c5e82015-12-21 11:26:06 -06003051static void send_login(struct ibmvnic_adapter *adapter)
3052{
3053 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3054 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003055 struct device *dev = &adapter->vdev->dev;
3056 dma_addr_t rsp_buffer_token;
3057 dma_addr_t buffer_token;
3058 size_t rsp_buffer_size;
3059 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003060 size_t buffer_size;
3061 __be64 *tx_list_p;
3062 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003063 int client_data_len;
3064 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 int i;
3066
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003067 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003068 client_data_len = vnic_client_data_len(adapter);
3069
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070 buffer_size =
3071 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003072 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3073 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003074
Nathan Fontenot37798d02017-11-08 11:23:56 -06003075 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003076 if (!login_buffer)
3077 goto buf_alloc_failed;
3078
3079 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3080 DMA_TO_DEVICE);
3081 if (dma_mapping_error(dev, buffer_token)) {
3082 dev_err(dev, "Couldn't map login buffer\n");
3083 goto buf_map_failed;
3084 }
3085
John Allen498cd8e2016-04-06 11:49:55 -05003086 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3087 sizeof(u64) * adapter->req_tx_queues +
3088 sizeof(u64) * adapter->req_rx_queues +
3089 sizeof(u64) * adapter->req_rx_queues +
3090 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003091
3092 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3093 if (!login_rsp_buffer)
3094 goto buf_rsp_alloc_failed;
3095
3096 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3097 rsp_buffer_size, DMA_FROM_DEVICE);
3098 if (dma_mapping_error(dev, rsp_buffer_token)) {
3099 dev_err(dev, "Couldn't map login rsp buffer\n");
3100 goto buf_rsp_map_failed;
3101 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003102
Thomas Falcon032c5e82015-12-21 11:26:06 -06003103 adapter->login_buf = login_buffer;
3104 adapter->login_buf_token = buffer_token;
3105 adapter->login_buf_sz = buffer_size;
3106 adapter->login_rsp_buf = login_rsp_buffer;
3107 adapter->login_rsp_buf_token = rsp_buffer_token;
3108 adapter->login_rsp_buf_sz = rsp_buffer_size;
3109
3110 login_buffer->len = cpu_to_be32(buffer_size);
3111 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3112 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3113 login_buffer->off_txcomp_subcrqs =
3114 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3115 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3116 login_buffer->off_rxcomp_subcrqs =
3117 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3118 sizeof(u64) * adapter->req_tx_queues);
3119 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3120 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3121
3122 tx_list_p = (__be64 *)((char *)login_buffer +
3123 sizeof(struct ibmvnic_login_buffer));
3124 rx_list_p = (__be64 *)((char *)login_buffer +
3125 sizeof(struct ibmvnic_login_buffer) +
3126 sizeof(u64) * adapter->req_tx_queues);
3127
3128 for (i = 0; i < adapter->req_tx_queues; i++) {
3129 if (adapter->tx_scrq[i]) {
3130 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3131 crq_num);
3132 }
3133 }
3134
3135 for (i = 0; i < adapter->req_rx_queues; i++) {
3136 if (adapter->rx_scrq[i]) {
3137 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3138 crq_num);
3139 }
3140 }
3141
Nathan Fontenot37798d02017-11-08 11:23:56 -06003142 /* Insert vNIC login client data */
3143 vlcd = (struct vnic_login_client_data *)
3144 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3145 login_buffer->client_data_offset =
3146 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3147 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3148
3149 vnic_add_client_data(adapter, vlcd);
3150
Thomas Falcon032c5e82015-12-21 11:26:06 -06003151 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3152 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3153 netdev_dbg(adapter->netdev, "%016lx\n",
3154 ((unsigned long int *)(adapter->login_buf))[i]);
3155 }
3156
3157 memset(&crq, 0, sizeof(crq));
3158 crq.login.first = IBMVNIC_CRQ_CMD;
3159 crq.login.cmd = LOGIN;
3160 crq.login.ioba = cpu_to_be32(buffer_token);
3161 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003162 ibmvnic_send_crq(adapter, &crq);
3163
3164 return;
3165
Thomas Falcon032c5e82015-12-21 11:26:06 -06003166buf_rsp_map_failed:
3167 kfree(login_rsp_buffer);
3168buf_rsp_alloc_failed:
3169 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3170buf_map_failed:
3171 kfree(login_buffer);
3172buf_alloc_failed:
3173 return;
3174}
3175
3176static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3177 u32 len, u8 map_id)
3178{
3179 union ibmvnic_crq crq;
3180
3181 memset(&crq, 0, sizeof(crq));
3182 crq.request_map.first = IBMVNIC_CRQ_CMD;
3183 crq.request_map.cmd = REQUEST_MAP;
3184 crq.request_map.map_id = map_id;
3185 crq.request_map.ioba = cpu_to_be32(addr);
3186 crq.request_map.len = cpu_to_be32(len);
3187 ibmvnic_send_crq(adapter, &crq);
3188}
3189
3190static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3191{
3192 union ibmvnic_crq crq;
3193
3194 memset(&crq, 0, sizeof(crq));
3195 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3196 crq.request_unmap.cmd = REQUEST_UNMAP;
3197 crq.request_unmap.map_id = map_id;
3198 ibmvnic_send_crq(adapter, &crq);
3199}
3200
3201static void send_map_query(struct ibmvnic_adapter *adapter)
3202{
3203 union ibmvnic_crq crq;
3204
3205 memset(&crq, 0, sizeof(crq));
3206 crq.query_map.first = IBMVNIC_CRQ_CMD;
3207 crq.query_map.cmd = QUERY_MAP;
3208 ibmvnic_send_crq(adapter, &crq);
3209}
3210
3211/* Send a series of CRQs requesting various capabilities of the VNIC server */
3212static void send_cap_queries(struct ibmvnic_adapter *adapter)
3213{
3214 union ibmvnic_crq crq;
3215
Thomas Falcon901e0402017-02-15 12:17:59 -06003216 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003217 memset(&crq, 0, sizeof(crq));
3218 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3219 crq.query_capability.cmd = QUERY_CAPABILITY;
3220
3221 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003222 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003223 ibmvnic_send_crq(adapter, &crq);
3224
3225 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003226 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003227 ibmvnic_send_crq(adapter, &crq);
3228
3229 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003230 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003231 ibmvnic_send_crq(adapter, &crq);
3232
3233 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003234 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003235 ibmvnic_send_crq(adapter, &crq);
3236
3237 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003238 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003239 ibmvnic_send_crq(adapter, &crq);
3240
3241 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003242 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003243 ibmvnic_send_crq(adapter, &crq);
3244
3245 crq.query_capability.capability =
3246 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003247 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003248 ibmvnic_send_crq(adapter, &crq);
3249
3250 crq.query_capability.capability =
3251 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003252 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003253 ibmvnic_send_crq(adapter, &crq);
3254
3255 crq.query_capability.capability =
3256 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003257 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003258 ibmvnic_send_crq(adapter, &crq);
3259
3260 crq.query_capability.capability =
3261 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003262 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003263 ibmvnic_send_crq(adapter, &crq);
3264
3265 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003266 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003267 ibmvnic_send_crq(adapter, &crq);
3268
3269 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003270 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003271 ibmvnic_send_crq(adapter, &crq);
3272
3273 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003274 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003275 ibmvnic_send_crq(adapter, &crq);
3276
3277 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003278 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003279 ibmvnic_send_crq(adapter, &crq);
3280
3281 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003282 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003283 ibmvnic_send_crq(adapter, &crq);
3284
3285 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003286 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 ibmvnic_send_crq(adapter, &crq);
3288
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003289 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3290 atomic_inc(&adapter->running_cap_crqs);
3291 ibmvnic_send_crq(adapter, &crq);
3292
Thomas Falcon032c5e82015-12-21 11:26:06 -06003293 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003294 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003295 ibmvnic_send_crq(adapter, &crq);
3296
3297 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003298 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299 ibmvnic_send_crq(adapter, &crq);
3300
3301 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003302 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303 ibmvnic_send_crq(adapter, &crq);
3304
3305 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003306 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003307 ibmvnic_send_crq(adapter, &crq);
3308
3309 crq.query_capability.capability =
3310 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003311 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003312 ibmvnic_send_crq(adapter, &crq);
3313
3314 crq.query_capability.capability =
3315 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003316 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003317 ibmvnic_send_crq(adapter, &crq);
3318
3319 crq.query_capability.capability =
3320 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003321 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 ibmvnic_send_crq(adapter, &crq);
3323
3324 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003325 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003326 ibmvnic_send_crq(adapter, &crq);
3327}
3328
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003329static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3330 struct ibmvnic_adapter *adapter)
3331{
3332 struct device *dev = &adapter->vdev->dev;
3333
3334 if (crq->get_vpd_size_rsp.rc.code) {
3335 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3336 crq->get_vpd_size_rsp.rc.code);
3337 complete(&adapter->fw_done);
3338 return;
3339 }
3340
3341 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3342 complete(&adapter->fw_done);
3343}
3344
3345static void handle_vpd_rsp(union ibmvnic_crq *crq,
3346 struct ibmvnic_adapter *adapter)
3347{
3348 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003349 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003350 u8 fw_level_len = 0;
3351
3352 memset(adapter->fw_version, 0, 32);
3353
3354 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3355 DMA_FROM_DEVICE);
3356
3357 if (crq->get_vpd_rsp.rc.code) {
3358 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3359 crq->get_vpd_rsp.rc.code);
3360 goto complete;
3361 }
3362
3363 /* get the position of the firmware version info
3364 * located after the ASCII 'RM' substring in the buffer
3365 */
3366 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3367 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003368 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 -02003369 goto complete;
3370 }
3371
3372 /* get length of firmware level ASCII substring */
3373 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3374 fw_level_len = *(substr + 2);
3375 } else {
3376 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3377 goto complete;
3378 }
3379
3380 /* copy firmware version string from vpd into adapter */
3381 if ((substr + 3 + fw_level_len) <
3382 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003383 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003384 } else {
3385 dev_info(dev, "FW substr extrapolated VPD buff\n");
3386 }
3387
3388complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003389 if (adapter->fw_version[0] == '\0')
3390 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003391 complete(&adapter->fw_done);
3392}
3393
Thomas Falcon032c5e82015-12-21 11:26:06 -06003394static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3395{
3396 struct device *dev = &adapter->vdev->dev;
3397 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3398 union ibmvnic_crq crq;
3399 int i;
3400
3401 dma_unmap_single(dev, adapter->ip_offload_tok,
3402 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3403
3404 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3405 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3406 netdev_dbg(adapter->netdev, "%016lx\n",
3407 ((unsigned long int *)(buf))[i]);
3408
3409 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3410 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3411 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3412 buf->tcp_ipv4_chksum);
3413 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3414 buf->tcp_ipv6_chksum);
3415 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3416 buf->udp_ipv4_chksum);
3417 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3418 buf->udp_ipv6_chksum);
3419 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3420 buf->large_tx_ipv4);
3421 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3422 buf->large_tx_ipv6);
3423 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3424 buf->large_rx_ipv4);
3425 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3426 buf->large_rx_ipv6);
3427 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3428 buf->max_ipv4_header_size);
3429 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3430 buf->max_ipv6_header_size);
3431 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3432 buf->max_tcp_header_size);
3433 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3434 buf->max_udp_header_size);
3435 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3436 buf->max_large_tx_size);
3437 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3438 buf->max_large_rx_size);
3439 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3440 buf->ipv6_extension_header);
3441 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3442 buf->tcp_pseudosum_req);
3443 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3444 buf->num_ipv6_ext_headers);
3445 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3446 buf->off_ipv6_ext_headers);
3447
3448 adapter->ip_offload_ctrl_tok =
3449 dma_map_single(dev, &adapter->ip_offload_ctrl,
3450 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3451
3452 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3453 dev_err(dev, "Couldn't map ip offload control buffer\n");
3454 return;
3455 }
3456
Thomas Falconf6897942018-01-18 19:05:01 -06003457 adapter->ip_offload_ctrl.len =
3458 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003459 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003460 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3461 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003462 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3463 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3464 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3465 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003466 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3467 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003468
Thomas Falconfdb06102017-10-17 12:36:55 -05003469 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003470 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3471 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3472
Thomas Falcon15482052017-10-17 12:36:54 -05003473 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003474
3475 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3476 adapter->netdev->features |= NETIF_F_IP_CSUM;
3477
3478 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3479 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3480
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003481 if ((adapter->netdev->features &
3482 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3483 adapter->netdev->features |= NETIF_F_RXCSUM;
3484
Thomas Falconfdb06102017-10-17 12:36:55 -05003485 if (buf->large_tx_ipv4)
3486 adapter->netdev->features |= NETIF_F_TSO;
3487 if (buf->large_tx_ipv6)
3488 adapter->netdev->features |= NETIF_F_TSO6;
3489
Thomas Falconaa0bf852017-10-17 12:36:56 -05003490 adapter->netdev->hw_features |= adapter->netdev->features;
3491
Thomas Falcon032c5e82015-12-21 11:26:06 -06003492 memset(&crq, 0, sizeof(crq));
3493 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3494 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3495 crq.control_ip_offload.len =
3496 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3497 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3498 ibmvnic_send_crq(adapter, &crq);
3499}
3500
3501static void handle_error_info_rsp(union ibmvnic_crq *crq,
3502 struct ibmvnic_adapter *adapter)
3503{
3504 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003505 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003506 unsigned long flags;
3507 bool found = false;
3508 int i;
3509
3510 if (!crq->request_error_rsp.rc.code) {
3511 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3512 crq->request_error_rsp.rc.code);
3513 return;
3514 }
3515
3516 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003517 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003518 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3519 found = true;
3520 list_del(&error_buff->list);
3521 break;
3522 }
3523 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3524
3525 if (!found) {
3526 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003527 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003528 return;
3529 }
3530
3531 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003532 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003533
3534 for (i = 0; i < error_buff->len; i++) {
3535 pr_cont("%02x", (int)error_buff->buff[i]);
3536 if (i % 8 == 7)
3537 pr_cont(" ");
3538 }
3539 pr_cont("\n");
3540
3541 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3542 DMA_FROM_DEVICE);
3543 kfree(error_buff->buff);
3544 kfree(error_buff);
3545}
3546
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003547static void request_error_information(struct ibmvnic_adapter *adapter,
3548 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003549{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003551 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003552 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003553 unsigned long timeout = msecs_to_jiffies(30000);
3554 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003555 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003556 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557
3558 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3559 if (!error_buff)
3560 return;
3561
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003562 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003563 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3564 if (!error_buff->buff) {
3565 kfree(error_buff);
3566 return;
3567 }
3568
3569 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3570 DMA_FROM_DEVICE);
3571 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003572 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003573 kfree(error_buff->buff);
3574 kfree(error_buff);
3575 return;
3576 }
3577
Thomas Falcon032c5e82015-12-21 11:26:06 -06003578 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003579 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003580
3581 spin_lock_irqsave(&adapter->error_list_lock, flags);
3582 list_add_tail(&error_buff->list, &adapter->errors);
3583 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3584
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003585 memset(&crq, 0, sizeof(crq));
3586 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3587 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3588 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3589 crq.request_error_info.len = cpu_to_be32(detail_len);
3590 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3591
3592 rc = ibmvnic_send_crq(adapter, &crq);
3593 if (rc) {
3594 netdev_err(netdev, "failed to request error information\n");
3595 goto err_info_fail;
3596 }
3597
3598 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3599 netdev_err(netdev, "timeout waiting for error information\n");
3600 goto err_info_fail;
3601 }
3602
3603 return;
3604
3605err_info_fail:
3606 spin_lock_irqsave(&adapter->error_list_lock, flags);
3607 list_del(&error_buff->list);
3608 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3609
3610 kfree(error_buff->buff);
3611 kfree(error_buff);
3612}
3613
3614static void handle_error_indication(union ibmvnic_crq *crq,
3615 struct ibmvnic_adapter *adapter)
3616{
3617 struct device *dev = &adapter->vdev->dev;
3618
3619 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3620 crq->error_indication.flags
3621 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3622 be32_to_cpu(crq->error_indication.error_id),
3623 be16_to_cpu(crq->error_indication.error_cause));
3624
3625 if (be32_to_cpu(crq->error_indication.error_id))
3626 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003627
3628 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3629 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003630 else
3631 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003632}
3633
Thomas Falconf8136142018-01-29 13:45:05 -06003634static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3635 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636{
3637 struct net_device *netdev = adapter->netdev;
3638 struct device *dev = &adapter->vdev->dev;
3639 long rc;
3640
3641 rc = crq->change_mac_addr_rsp.rc.code;
3642 if (rc) {
3643 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003644 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003645 }
3646 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3647 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003648out:
3649 complete(&adapter->fw_done);
3650 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003651}
3652
3653static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3654 struct ibmvnic_adapter *adapter)
3655{
3656 struct device *dev = &adapter->vdev->dev;
3657 u64 *req_value;
3658 char *name;
3659
Thomas Falcon901e0402017-02-15 12:17:59 -06003660 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003661 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3662 case REQ_TX_QUEUES:
3663 req_value = &adapter->req_tx_queues;
3664 name = "tx";
3665 break;
3666 case REQ_RX_QUEUES:
3667 req_value = &adapter->req_rx_queues;
3668 name = "rx";
3669 break;
3670 case REQ_RX_ADD_QUEUES:
3671 req_value = &adapter->req_rx_add_queues;
3672 name = "rx_add";
3673 break;
3674 case REQ_TX_ENTRIES_PER_SUBCRQ:
3675 req_value = &adapter->req_tx_entries_per_subcrq;
3676 name = "tx_entries_per_subcrq";
3677 break;
3678 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3679 req_value = &adapter->req_rx_add_entries_per_subcrq;
3680 name = "rx_add_entries_per_subcrq";
3681 break;
3682 case REQ_MTU:
3683 req_value = &adapter->req_mtu;
3684 name = "mtu";
3685 break;
3686 case PROMISC_REQUESTED:
3687 req_value = &adapter->promisc;
3688 name = "promisc";
3689 break;
3690 default:
3691 dev_err(dev, "Got invalid cap request rsp %d\n",
3692 crq->request_capability.capability);
3693 return;
3694 }
3695
3696 switch (crq->request_capability_rsp.rc.code) {
3697 case SUCCESS:
3698 break;
3699 case PARTIALSUCCESS:
3700 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3701 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003702 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003703 number), name);
John Allene7913802018-01-18 16:27:12 -06003704
3705 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3706 REQ_MTU) {
3707 pr_err("mtu of %llu is not supported. Reverting.\n",
3708 *req_value);
3709 *req_value = adapter->fallback.mtu;
3710 } else {
3711 *req_value =
3712 be64_to_cpu(crq->request_capability_rsp.number);
3713 }
3714
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003715 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716 return;
3717 default:
3718 dev_err(dev, "Error %d in request cap rsp\n",
3719 crq->request_capability_rsp.rc.code);
3720 return;
3721 }
3722
3723 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003724 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003725 union ibmvnic_crq newcrq;
3726 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3727 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3728 &adapter->ip_offload_buf;
3729
Thomas Falcon249168a2017-02-15 12:18:00 -06003730 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003731 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3732 buf_sz,
3733 DMA_FROM_DEVICE);
3734
3735 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3736 if (!firmware_has_feature(FW_FEATURE_CMO))
3737 dev_err(dev, "Couldn't map offload buffer\n");
3738 return;
3739 }
3740
3741 memset(&newcrq, 0, sizeof(newcrq));
3742 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3743 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3744 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3745 newcrq.query_ip_offload.ioba =
3746 cpu_to_be32(adapter->ip_offload_tok);
3747
3748 ibmvnic_send_crq(adapter, &newcrq);
3749 }
3750}
3751
3752static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3753 struct ibmvnic_adapter *adapter)
3754{
3755 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003756 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003757 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3758 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759 int i;
3760
3761 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3762 DMA_BIDIRECTIONAL);
3763 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3764 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3765
John Allen498cd8e2016-04-06 11:49:55 -05003766 /* If the number of queues requested can't be allocated by the
3767 * server, the login response will return with code 1. We will need
3768 * to resend the login buffer with fewer queues requested.
3769 */
3770 if (login_rsp_crq->generic.rc.code) {
3771 adapter->renegotiate = true;
3772 complete(&adapter->init_done);
3773 return 0;
3774 }
3775
John Allenc26eba02017-10-26 16:23:25 -05003776 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3777
Thomas Falcon032c5e82015-12-21 11:26:06 -06003778 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3779 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3780 netdev_dbg(adapter->netdev, "%016lx\n",
3781 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3782 }
3783
3784 /* Sanity checks */
3785 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3786 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3787 adapter->req_rx_add_queues !=
3788 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3789 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3790 ibmvnic_remove(adapter->vdev);
3791 return -EIO;
3792 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003793 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003794 complete(&adapter->init_done);
3795
Thomas Falcon032c5e82015-12-21 11:26:06 -06003796 return 0;
3797}
3798
Thomas Falcon032c5e82015-12-21 11:26:06 -06003799static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3800 struct ibmvnic_adapter *adapter)
3801{
3802 struct device *dev = &adapter->vdev->dev;
3803 long rc;
3804
3805 rc = crq->request_unmap_rsp.rc.code;
3806 if (rc)
3807 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3808}
3809
3810static void handle_query_map_rsp(union ibmvnic_crq *crq,
3811 struct ibmvnic_adapter *adapter)
3812{
3813 struct net_device *netdev = adapter->netdev;
3814 struct device *dev = &adapter->vdev->dev;
3815 long rc;
3816
3817 rc = crq->query_map_rsp.rc.code;
3818 if (rc) {
3819 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3820 return;
3821 }
3822 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3823 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3824 crq->query_map_rsp.free_pages);
3825}
3826
3827static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3828 struct ibmvnic_adapter *adapter)
3829{
3830 struct net_device *netdev = adapter->netdev;
3831 struct device *dev = &adapter->vdev->dev;
3832 long rc;
3833
Thomas Falcon901e0402017-02-15 12:17:59 -06003834 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003835 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003836 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003837 rc = crq->query_capability.rc.code;
3838 if (rc) {
3839 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3840 goto out;
3841 }
3842
3843 switch (be16_to_cpu(crq->query_capability.capability)) {
3844 case MIN_TX_QUEUES:
3845 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003846 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003847 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3848 adapter->min_tx_queues);
3849 break;
3850 case MIN_RX_QUEUES:
3851 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003852 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003853 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3854 adapter->min_rx_queues);
3855 break;
3856 case MIN_RX_ADD_QUEUES:
3857 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003858 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003859 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3860 adapter->min_rx_add_queues);
3861 break;
3862 case MAX_TX_QUEUES:
3863 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003864 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003865 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3866 adapter->max_tx_queues);
3867 break;
3868 case MAX_RX_QUEUES:
3869 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003870 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003871 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3872 adapter->max_rx_queues);
3873 break;
3874 case MAX_RX_ADD_QUEUES:
3875 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003876 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003877 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3878 adapter->max_rx_add_queues);
3879 break;
3880 case MIN_TX_ENTRIES_PER_SUBCRQ:
3881 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003882 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3884 adapter->min_tx_entries_per_subcrq);
3885 break;
3886 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3887 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003888 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003889 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3890 adapter->min_rx_add_entries_per_subcrq);
3891 break;
3892 case MAX_TX_ENTRIES_PER_SUBCRQ:
3893 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003894 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003895 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3896 adapter->max_tx_entries_per_subcrq);
3897 break;
3898 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3899 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003900 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3902 adapter->max_rx_add_entries_per_subcrq);
3903 break;
3904 case TCP_IP_OFFLOAD:
3905 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003906 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3908 adapter->tcp_ip_offload);
3909 break;
3910 case PROMISC_SUPPORTED:
3911 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003912 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003913 netdev_dbg(netdev, "promisc_supported = %lld\n",
3914 adapter->promisc_supported);
3915 break;
3916 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003917 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003918 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003919 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3920 break;
3921 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003922 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003923 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3925 break;
3926 case MAX_MULTICAST_FILTERS:
3927 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003928 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3930 adapter->max_multicast_filters);
3931 break;
3932 case VLAN_HEADER_INSERTION:
3933 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003934 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003935 if (adapter->vlan_header_insertion)
3936 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3937 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3938 adapter->vlan_header_insertion);
3939 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003940 case RX_VLAN_HEADER_INSERTION:
3941 adapter->rx_vlan_header_insertion =
3942 be64_to_cpu(crq->query_capability.number);
3943 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3944 adapter->rx_vlan_header_insertion);
3945 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003946 case MAX_TX_SG_ENTRIES:
3947 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003948 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003949 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3950 adapter->max_tx_sg_entries);
3951 break;
3952 case RX_SG_SUPPORTED:
3953 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003954 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003955 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3956 adapter->rx_sg_supported);
3957 break;
3958 case OPT_TX_COMP_SUB_QUEUES:
3959 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003960 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003961 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3962 adapter->opt_tx_comp_sub_queues);
3963 break;
3964 case OPT_RX_COMP_QUEUES:
3965 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003966 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003967 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3968 adapter->opt_rx_comp_queues);
3969 break;
3970 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3971 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003972 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003973 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3974 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3975 break;
3976 case OPT_TX_ENTRIES_PER_SUBCRQ:
3977 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003978 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003979 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3980 adapter->opt_tx_entries_per_subcrq);
3981 break;
3982 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3983 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003984 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3986 adapter->opt_rxba_entries_per_subcrq);
3987 break;
3988 case TX_RX_DESC_REQ:
3989 adapter->tx_rx_desc_req = crq->query_capability.number;
3990 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3991 adapter->tx_rx_desc_req);
3992 break;
3993
3994 default:
3995 netdev_err(netdev, "Got invalid cap rsp %d\n",
3996 crq->query_capability.capability);
3997 }
3998
3999out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004000 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4001 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004002 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004003 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004004}
4005
Thomas Falcon032c5e82015-12-21 11:26:06 -06004006static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4007 struct ibmvnic_adapter *adapter)
4008{
4009 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4010 struct net_device *netdev = adapter->netdev;
4011 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004012 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004013 long rc;
4014
4015 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004016 (unsigned long int)cpu_to_be64(u64_crq[0]),
4017 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004018 switch (gen_crq->first) {
4019 case IBMVNIC_CRQ_INIT_RSP:
4020 switch (gen_crq->cmd) {
4021 case IBMVNIC_CRQ_INIT:
4022 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004023 adapter->from_passive_init = true;
4024 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004025 break;
4026 case IBMVNIC_CRQ_INIT_COMPLETE:
4027 dev_info(dev, "Partner initialization complete\n");
4028 send_version_xchg(adapter);
4029 break;
4030 default:
4031 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4032 }
4033 return;
4034 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004035 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004036 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004037 dev_info(dev, "Migrated, re-enabling adapter\n");
4038 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004039 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4040 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004041 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004042 } else {
4043 /* The adapter lost the connection */
4044 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4045 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004046 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004047 }
4048 return;
4049 case IBMVNIC_CRQ_CMD_RSP:
4050 break;
4051 default:
4052 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4053 gen_crq->first);
4054 return;
4055 }
4056
4057 switch (gen_crq->cmd) {
4058 case VERSION_EXCHANGE_RSP:
4059 rc = crq->version_exchange_rsp.rc.code;
4060 if (rc) {
4061 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4062 break;
4063 }
4064 dev_info(dev, "Partner protocol version is %d\n",
4065 crq->version_exchange_rsp.version);
4066 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4067 ibmvnic_version)
4068 ibmvnic_version =
4069 be16_to_cpu(crq->version_exchange_rsp.version);
4070 send_cap_queries(adapter);
4071 break;
4072 case QUERY_CAPABILITY_RSP:
4073 handle_query_cap_rsp(crq, adapter);
4074 break;
4075 case QUERY_MAP_RSP:
4076 handle_query_map_rsp(crq, adapter);
4077 break;
4078 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004079 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4080 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004081 break;
4082 case REQUEST_UNMAP_RSP:
4083 handle_request_unmap_rsp(crq, adapter);
4084 break;
4085 case REQUEST_CAPABILITY_RSP:
4086 handle_request_cap_rsp(crq, adapter);
4087 break;
4088 case LOGIN_RSP:
4089 netdev_dbg(netdev, "Got Login Response\n");
4090 handle_login_rsp(crq, adapter);
4091 break;
4092 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004093 netdev_dbg(netdev,
4094 "Got Logical Link State Response, state: %d rc: %d\n",
4095 crq->logical_link_state_rsp.link_state,
4096 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004097 adapter->logical_link_state =
4098 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004099 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4100 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004101 break;
4102 case LINK_STATE_INDICATION:
4103 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4104 adapter->phys_link_state =
4105 crq->link_state_indication.phys_link_state;
4106 adapter->logical_link_state =
4107 crq->link_state_indication.logical_link_state;
4108 break;
4109 case CHANGE_MAC_ADDR_RSP:
4110 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004111 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004112 break;
4113 case ERROR_INDICATION:
4114 netdev_dbg(netdev, "Got Error Indication\n");
4115 handle_error_indication(crq, adapter);
4116 break;
4117 case REQUEST_ERROR_RSP:
4118 netdev_dbg(netdev, "Got Error Detail Response\n");
4119 handle_error_info_rsp(crq, adapter);
4120 break;
4121 case REQUEST_STATISTICS_RSP:
4122 netdev_dbg(netdev, "Got Statistics Response\n");
4123 complete(&adapter->stats_done);
4124 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125 case QUERY_IP_OFFLOAD_RSP:
4126 netdev_dbg(netdev, "Got Query IP offload Response\n");
4127 handle_query_ip_offload_rsp(adapter);
4128 break;
4129 case MULTICAST_CTRL_RSP:
4130 netdev_dbg(netdev, "Got multicast control Response\n");
4131 break;
4132 case CONTROL_IP_OFFLOAD_RSP:
4133 netdev_dbg(netdev, "Got Control IP offload Response\n");
4134 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4135 sizeof(adapter->ip_offload_ctrl),
4136 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004137 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004138 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004139 case COLLECT_FW_TRACE_RSP:
4140 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4141 complete(&adapter->fw_done);
4142 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004143 case GET_VPD_SIZE_RSP:
4144 handle_vpd_size_rsp(crq, adapter);
4145 break;
4146 case GET_VPD_RSP:
4147 handle_vpd_rsp(crq, adapter);
4148 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149 default:
4150 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4151 gen_crq->cmd);
4152 }
4153}
4154
4155static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4156{
4157 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004158
Thomas Falcon6c267b32017-02-15 12:17:58 -06004159 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004160 return IRQ_HANDLED;
4161}
4162
4163static void ibmvnic_tasklet(void *data)
4164{
4165 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004166 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004167 union ibmvnic_crq *crq;
4168 unsigned long flags;
4169 bool done = false;
4170
4171 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004172 while (!done) {
4173 /* Pull all the valid messages off the CRQ */
4174 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4175 ibmvnic_handle_crq(crq, adapter);
4176 crq->generic.first = 0;
4177 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004178
4179 /* remain in tasklet until all
4180 * capabilities responses are received
4181 */
4182 if (!adapter->wait_capability)
4183 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004184 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004185 /* if capabilities CRQ's were sent in this tasklet, the following
4186 * tasklet must wait until all responses are received
4187 */
4188 if (atomic_read(&adapter->running_cap_crqs) != 0)
4189 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004190 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004191}
4192
4193static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4194{
4195 struct vio_dev *vdev = adapter->vdev;
4196 int rc;
4197
4198 do {
4199 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4200 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4201
4202 if (rc)
4203 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4204
4205 return rc;
4206}
4207
4208static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4209{
4210 struct ibmvnic_crq_queue *crq = &adapter->crq;
4211 struct device *dev = &adapter->vdev->dev;
4212 struct vio_dev *vdev = adapter->vdev;
4213 int rc;
4214
4215 /* Close the CRQ */
4216 do {
4217 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4218 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4219
4220 /* Clean out the queue */
4221 memset(crq->msgs, 0, PAGE_SIZE);
4222 crq->cur = 0;
4223
4224 /* And re-open it again */
4225 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4226 crq->msg_token, PAGE_SIZE);
4227
4228 if (rc == H_CLOSED)
4229 /* Adapter is good, but other end is not ready */
4230 dev_warn(dev, "Partner adapter not ready\n");
4231 else if (rc != 0)
4232 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4233
4234 return rc;
4235}
4236
Nathan Fontenotf9928872017-03-30 02:48:54 -04004237static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004238{
4239 struct ibmvnic_crq_queue *crq = &adapter->crq;
4240 struct vio_dev *vdev = adapter->vdev;
4241 long rc;
4242
Nathan Fontenotf9928872017-03-30 02:48:54 -04004243 if (!crq->msgs)
4244 return;
4245
Thomas Falcon032c5e82015-12-21 11:26:06 -06004246 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4247 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004248 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004249 do {
4250 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4251 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4252
4253 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4254 DMA_BIDIRECTIONAL);
4255 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004256 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004257}
4258
Nathan Fontenotf9928872017-03-30 02:48:54 -04004259static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004260{
4261 struct ibmvnic_crq_queue *crq = &adapter->crq;
4262 struct device *dev = &adapter->vdev->dev;
4263 struct vio_dev *vdev = adapter->vdev;
4264 int rc, retrc = -ENOMEM;
4265
Nathan Fontenotf9928872017-03-30 02:48:54 -04004266 if (crq->msgs)
4267 return 0;
4268
Thomas Falcon032c5e82015-12-21 11:26:06 -06004269 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4270 /* Should we allocate more than one page? */
4271
4272 if (!crq->msgs)
4273 return -ENOMEM;
4274
4275 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4276 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4277 DMA_BIDIRECTIONAL);
4278 if (dma_mapping_error(dev, crq->msg_token))
4279 goto map_failed;
4280
4281 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4282 crq->msg_token, PAGE_SIZE);
4283
4284 if (rc == H_RESOURCE)
4285 /* maybe kexecing and resource is busy. try a reset */
4286 rc = ibmvnic_reset_crq(adapter);
4287 retrc = rc;
4288
4289 if (rc == H_CLOSED) {
4290 dev_warn(dev, "Partner adapter not ready\n");
4291 } else if (rc) {
4292 dev_warn(dev, "Error %d opening adapter\n", rc);
4293 goto reg_crq_failed;
4294 }
4295
4296 retrc = 0;
4297
Thomas Falcon6c267b32017-02-15 12:17:58 -06004298 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4299 (unsigned long)adapter);
4300
Thomas Falcon032c5e82015-12-21 11:26:06 -06004301 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4302 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4303 adapter);
4304 if (rc) {
4305 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4306 vdev->irq, rc);
4307 goto req_irq_failed;
4308 }
4309
4310 rc = vio_enable_interrupts(vdev);
4311 if (rc) {
4312 dev_err(dev, "Error %d enabling interrupts\n", rc);
4313 goto req_irq_failed;
4314 }
4315
4316 crq->cur = 0;
4317 spin_lock_init(&crq->lock);
4318
4319 return retrc;
4320
4321req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004322 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004323 do {
4324 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4325 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4326reg_crq_failed:
4327 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4328map_failed:
4329 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004330 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004331 return retrc;
4332}
4333
John Allenf6ef6402017-03-17 17:13:42 -05004334static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4335{
4336 struct device *dev = &adapter->vdev->dev;
4337 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05004338 int rc;
4339
John Allenc26eba02017-10-26 16:23:25 -05004340 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004341 rc = ibmvnic_reset_crq(adapter);
4342 if (!rc)
4343 rc = vio_enable_interrupts(adapter->vdev);
4344 } else {
4345 rc = init_crq_queue(adapter);
4346 }
4347
John Allenf6ef6402017-03-17 17:13:42 -05004348 if (rc) {
4349 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4350 return rc;
4351 }
4352
John Allen017892c12017-05-26 10:30:19 -04004353 adapter->from_passive_init = false;
4354
John Allenf6ef6402017-03-17 17:13:42 -05004355 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004356 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004357 ibmvnic_send_crq_init(adapter);
4358 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4359 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004360 return -1;
4361 }
4362
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004363 if (adapter->init_done_rc) {
4364 release_crq_queue(adapter);
4365 return adapter->init_done_rc;
4366 }
4367
John Allen017892c12017-05-26 10:30:19 -04004368 if (adapter->from_passive_init) {
4369 adapter->state = VNIC_OPEN;
4370 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004371 return -1;
4372 }
4373
John Allenc26eba02017-10-26 16:23:25 -05004374 if (adapter->resetting && !adapter->wait_for_reset)
Nathan Fontenot57a49432017-05-26 10:31:12 -04004375 rc = reset_sub_crq_queues(adapter);
4376 else
4377 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004378 if (rc) {
4379 dev_err(dev, "Initialization of sub crqs failed\n");
4380 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004381 return rc;
4382 }
4383
4384 rc = init_sub_crq_irqs(adapter);
4385 if (rc) {
4386 dev_err(dev, "Failed to initialize sub crq irqs\n");
4387 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004388 }
4389
4390 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004391}
4392
Thomas Falcon40c9db82017-06-12 12:35:04 -05004393static struct device_attribute dev_attr_failover;
4394
Thomas Falcon032c5e82015-12-21 11:26:06 -06004395static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4396{
4397 struct ibmvnic_adapter *adapter;
4398 struct net_device *netdev;
4399 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004400 int rc;
4401
4402 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4403 dev->unit_address);
4404
4405 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4406 VETH_MAC_ADDR, NULL);
4407 if (!mac_addr_p) {
4408 dev_err(&dev->dev,
4409 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4410 __FILE__, __LINE__);
4411 return 0;
4412 }
4413
4414 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004415 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004416 if (!netdev)
4417 return -ENOMEM;
4418
4419 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004420 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004421 dev_set_drvdata(&dev->dev, netdev);
4422 adapter->vdev = dev;
4423 adapter->netdev = netdev;
4424
4425 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4426 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4427 netdev->irq = dev->irq;
4428 netdev->netdev_ops = &ibmvnic_netdev_ops;
4429 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4430 SET_NETDEV_DEV(netdev, &dev->dev);
4431
4432 spin_lock_init(&adapter->stats_lock);
4433
Thomas Falcon032c5e82015-12-21 11:26:06 -06004434 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004435 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004436
Nathan Fontenoted651a12017-05-03 14:04:38 -04004437 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4438 INIT_LIST_HEAD(&adapter->rwi_list);
4439 mutex_init(&adapter->reset_lock);
4440 mutex_init(&adapter->rwi_lock);
4441 adapter->resetting = false;
4442
John Allenc26eba02017-10-26 16:23:25 -05004443 adapter->mac_change_pending = false;
4444
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004445 do {
4446 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004447 if (rc && rc != EAGAIN)
4448 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004449 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450
Thomas Falconf39f0d12017-02-14 10:22:59 -06004451 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004452 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4453 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004454
Thomas Falcon40c9db82017-06-12 12:35:04 -05004455 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004456 if (rc)
4457 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004458
Mick Tarsele876a8a2017-09-28 13:53:18 -07004459 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004460 rc = register_netdev(netdev);
4461 if (rc) {
4462 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004463 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004464 }
4465 dev_info(&dev->dev, "ibmvnic registered\n");
4466
Nathan Fontenot90c80142017-05-03 14:04:32 -04004467 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004468
4469 adapter->wait_for_reset = false;
4470
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004472
4473ibmvnic_register_fail:
4474 device_remove_file(&dev->dev, &dev_attr_failover);
4475
4476ibmvnic_init_fail:
4477 release_sub_crqs(adapter);
4478 release_crq_queue(adapter);
4479 free_netdev(netdev);
4480
4481 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004482}
4483
4484static int ibmvnic_remove(struct vio_dev *dev)
4485{
4486 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004487 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004488
Nathan Fontenot90c80142017-05-03 14:04:32 -04004489 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004490 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004491 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004492
4493 release_resources(adapter);
4494 release_sub_crqs(adapter);
4495 release_crq_queue(adapter);
4496
Nathan Fontenot90c80142017-05-03 14:04:32 -04004497 adapter->state = VNIC_REMOVED;
4498
Nathan Fontenoted651a12017-05-03 14:04:38 -04004499 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004500 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004501 free_netdev(netdev);
4502 dev_set_drvdata(&dev->dev, NULL);
4503
4504 return 0;
4505}
4506
Thomas Falcon40c9db82017-06-12 12:35:04 -05004507static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4508 const char *buf, size_t count)
4509{
4510 struct net_device *netdev = dev_get_drvdata(dev);
4511 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4512 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4513 __be64 session_token;
4514 long rc;
4515
4516 if (!sysfs_streq(buf, "1"))
4517 return -EINVAL;
4518
4519 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4520 H_GET_SESSION_TOKEN, 0, 0, 0);
4521 if (rc) {
4522 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4523 rc);
4524 return -EINVAL;
4525 }
4526
4527 session_token = (__be64)retbuf[0];
4528 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4529 be64_to_cpu(session_token));
4530 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4531 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4532 if (rc) {
4533 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4534 rc);
4535 return -EINVAL;
4536 }
4537
4538 return count;
4539}
4540
Joe Perches6cbaefb2017-12-19 10:15:09 -08004541static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004542
Thomas Falcon032c5e82015-12-21 11:26:06 -06004543static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4544{
4545 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4546 struct ibmvnic_adapter *adapter;
4547 struct iommu_table *tbl;
4548 unsigned long ret = 0;
4549 int i;
4550
4551 tbl = get_iommu_table_base(&vdev->dev);
4552
4553 /* netdev inits at probe time along with the structures we need below*/
4554 if (!netdev)
4555 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4556
4557 adapter = netdev_priv(netdev);
4558
4559 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004560 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4561
4562 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4563 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4564
4565 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4566 i++)
4567 ret += adapter->rx_pool[i].size *
4568 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4569
4570 return ret;
4571}
4572
4573static int ibmvnic_resume(struct device *dev)
4574{
4575 struct net_device *netdev = dev_get_drvdata(dev);
4576 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004577
John Allencb89ba22017-06-19 11:27:53 -05004578 if (adapter->state != VNIC_OPEN)
4579 return 0;
4580
John Allena2488782017-07-24 13:26:06 -05004581 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004582
4583 return 0;
4584}
4585
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304586static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004587 {"network", "IBM,vnic"},
4588 {"", "" }
4589};
4590MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4591
4592static const struct dev_pm_ops ibmvnic_pm_ops = {
4593 .resume = ibmvnic_resume
4594};
4595
4596static struct vio_driver ibmvnic_driver = {
4597 .id_table = ibmvnic_device_table,
4598 .probe = ibmvnic_probe,
4599 .remove = ibmvnic_remove,
4600 .get_desired_dma = ibmvnic_get_desired_dma,
4601 .name = ibmvnic_driver_name,
4602 .pm = &ibmvnic_pm_ops,
4603};
4604
4605/* module functions */
4606static int __init ibmvnic_module_init(void)
4607{
4608 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4609 IBMVNIC_DRIVER_VERSION);
4610
4611 return vio_register_driver(&ibmvnic_driver);
4612}
4613
4614static void __exit ibmvnic_module_exit(void)
4615{
4616 vio_unregister_driver(&ibmvnic_driver);
4617}
4618
4619module_init(ibmvnic_module_init);
4620module_exit(ibmvnic_module_exit);