blob: b907c0a607e6d5d48767870461e7fdcec35c663f [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
Thomas Falcon4eb50ce2017-12-18 12:52:40 -060062#include <linux/if_arp.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060063#include <linux/in.h>
64#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050065#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060066#include <linux/irq.h>
67#include <linux/kthread.h>
68#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060069#include <linux/interrupt.h>
70#include <net/net_namespace.h>
71#include <asm/hvcall.h>
72#include <linux/atomic.h>
73#include <asm/vio.h>
74#include <asm/iommu.h>
75#include <linux/uaccess.h>
76#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050077#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040078#include <linux/if_vlan.h>
Nathan Fontenot37798d02017-11-08 11:23:56 -060079#include <linux/utsname.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060080
81#include "ibmvnic.h"
82
83static const char ibmvnic_driver_name[] = "ibmvnic";
84static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
85
Thomas Falcon78b07ac2017-06-01 15:32:34 -050086MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060087MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
90
91static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92static int ibmvnic_remove(struct vio_dev *);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -060093static void release_sub_crqs(struct ibmvnic_adapter *, bool);
Thomas Falcon032c5e82015-12-21 11:26:06 -060094static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500100static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600101static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110static int ibmvnic_poll(struct napi_struct *napi, int data);
111static void send_map_query(struct ibmvnic_adapter *adapter);
112static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113static void send_request_unmap(struct ibmvnic_adapter *, u8);
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 =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600364 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500365 sizeof(struct ibmvnic_tx_queue_stats),
366 GFP_KERNEL);
367 if (!adapter->tx_stats_buffers)
368 return -ENOMEM;
369
370 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600371 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500372 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
Nathan Fontenot82e3be32018-02-21 21:33:56 -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;
Nathan Fontenot82e3be32018-02-21 21:33:56 -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
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600512 adapter->num_active_rx_pools = rxadd_subcrqs;
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
557 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600558}
559
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400560static int reset_tx_pools(struct ibmvnic_adapter *adapter)
561{
562 struct ibmvnic_tx_pool *tx_pool;
563 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500564 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400565
566 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
567 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500568 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
569
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400570 tx_pool = &adapter->tx_pool[i];
571
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500572 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
573 if (rc)
574 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400575
Thomas Falconfdb06102017-10-17 12:36:55 -0500576 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
577 if (rc)
578 return rc;
579
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400580 memset(tx_pool->tx_buff, 0,
581 adapter->req_tx_entries_per_subcrq *
582 sizeof(struct ibmvnic_tx_buff));
583
584 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
585 tx_pool->free_map[j] = j;
586
587 tx_pool->consumer_index = 0;
588 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500589 tx_pool->tso_index = 0;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400590 }
591
592 return 0;
593}
594
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200595static void release_vpd_data(struct ibmvnic_adapter *adapter)
596{
597 if (!adapter->vpd)
598 return;
599
600 kfree(adapter->vpd->buff);
601 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600602
603 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200604}
605
Nathan Fontenotc657e322017-03-30 02:49:06 -0400606static void release_tx_pools(struct ibmvnic_adapter *adapter)
607{
608 struct ibmvnic_tx_pool *tx_pool;
John Allen896d8692018-01-18 16:26:31 -0600609 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400610
611 if (!adapter->tx_pool)
612 return;
613
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600614 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500615 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400616 tx_pool = &adapter->tx_pool[i];
617 kfree(tx_pool->tx_buff);
618 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500619 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400620 kfree(tx_pool->free_map);
621 }
622
623 kfree(adapter->tx_pool);
624 adapter->tx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600625 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400626}
627
628static int init_tx_pools(struct net_device *netdev)
629{
630 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
631 struct device *dev = &adapter->vdev->dev;
632 struct ibmvnic_tx_pool *tx_pool;
633 int tx_subcrqs;
634 int i, j;
635
636 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
637 adapter->tx_pool = kcalloc(tx_subcrqs,
638 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
639 if (!adapter->tx_pool)
640 return -1;
641
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600642 adapter->num_active_tx_pools = tx_subcrqs;
643
Nathan Fontenotc657e322017-03-30 02:49:06 -0400644 for (i = 0; i < tx_subcrqs; i++) {
645 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500646
647 netdev_dbg(adapter->netdev,
648 "Initializing tx_pool[%d], %lld buffs\n",
649 i, adapter->req_tx_entries_per_subcrq);
650
Nathan Fontenotc657e322017-03-30 02:49:06 -0400651 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
652 sizeof(struct ibmvnic_tx_buff),
653 GFP_KERNEL);
654 if (!tx_pool->tx_buff) {
655 dev_err(dev, "tx pool buffer allocation failed\n");
656 release_tx_pools(adapter);
657 return -1;
658 }
659
660 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
661 adapter->req_tx_entries_per_subcrq *
662 adapter->req_mtu)) {
663 release_tx_pools(adapter);
664 return -1;
665 }
666
Thomas Falconfdb06102017-10-17 12:36:55 -0500667 /* alloc TSO ltb */
668 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
669 IBMVNIC_TSO_BUFS *
670 IBMVNIC_TSO_BUF_SZ)) {
671 release_tx_pools(adapter);
672 return -1;
673 }
674
675 tx_pool->tso_index = 0;
676
Nathan Fontenotc657e322017-03-30 02:49:06 -0400677 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
678 sizeof(int), GFP_KERNEL);
679 if (!tx_pool->free_map) {
680 release_tx_pools(adapter);
681 return -1;
682 }
683
684 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
685 tx_pool->free_map[j] = j;
686
687 tx_pool->consumer_index = 0;
688 tx_pool->producer_index = 0;
689 }
690
691 return 0;
692}
693
Nathan Fontenot661a2622017-04-19 13:44:58 -0400694static void release_error_buffers(struct ibmvnic_adapter *adapter)
695{
696 struct device *dev = &adapter->vdev->dev;
697 struct ibmvnic_error_buff *error_buff, *tmp;
698 unsigned long flags;
699
700 spin_lock_irqsave(&adapter->error_list_lock, flags);
701 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
702 list_del(&error_buff->list);
703 dma_unmap_single(dev, error_buff->dma, error_buff->len,
704 DMA_FROM_DEVICE);
705 kfree(error_buff->buff);
706 kfree(error_buff);
707 }
708 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
709}
710
John Allend944c3d62017-05-26 10:30:13 -0400711static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
712{
713 int i;
714
715 if (adapter->napi_enabled)
716 return;
717
718 for (i = 0; i < adapter->req_rx_queues; i++)
719 napi_enable(&adapter->napi[i]);
720
721 adapter->napi_enabled = true;
722}
723
724static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
725{
726 int i;
727
728 if (!adapter->napi_enabled)
729 return;
730
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500731 for (i = 0; i < adapter->req_rx_queues; i++) {
732 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400733 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500734 }
John Allend944c3d62017-05-26 10:30:13 -0400735
736 adapter->napi_enabled = false;
737}
738
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600739static int init_napi(struct ibmvnic_adapter *adapter)
740{
741 int i;
742
743 adapter->napi = kcalloc(adapter->req_rx_queues,
744 sizeof(struct napi_struct), GFP_KERNEL);
745 if (!adapter->napi)
746 return -ENOMEM;
747
748 for (i = 0; i < adapter->req_rx_queues; i++) {
749 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
750 netif_napi_add(adapter->netdev, &adapter->napi[i],
751 ibmvnic_poll, NAPI_POLL_WEIGHT);
752 }
753
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600754 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600755 return 0;
756}
757
758static void release_napi(struct ibmvnic_adapter *adapter)
759{
760 int i;
761
762 if (!adapter->napi)
763 return;
764
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600765 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600766 if (&adapter->napi[i]) {
767 netdev_dbg(adapter->netdev,
768 "Releasing napi[%d]\n", i);
769 netif_napi_del(&adapter->napi[i]);
770 }
771 }
772
773 kfree(adapter->napi);
774 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600775 adapter->num_active_rx_napi = 0;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600776}
777
John Allena57a5d22017-03-17 17:13:41 -0500778static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600779{
780 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500781 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600782 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500783 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600784
John Allenbd0b6722017-03-17 17:13:40 -0500785 do {
786 if (adapter->renegotiate) {
787 adapter->renegotiate = false;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600788 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500789
790 reinit_completion(&adapter->init_done);
791 send_cap_queries(adapter);
792 if (!wait_for_completion_timeout(&adapter->init_done,
793 timeout)) {
794 dev_err(dev, "Capabilities query timeout\n");
795 return -1;
796 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500797 rc = init_sub_crqs(adapter);
798 if (rc) {
799 dev_err(dev,
800 "Initialization of SCRQ's failed\n");
801 return -1;
802 }
803 rc = init_sub_crq_irqs(adapter);
804 if (rc) {
805 dev_err(dev,
806 "Initialization of SCRQ's irqs failed\n");
807 return -1;
808 }
John Allenbd0b6722017-03-17 17:13:40 -0500809 }
810
811 reinit_completion(&adapter->init_done);
812 send_login(adapter);
813 if (!wait_for_completion_timeout(&adapter->init_done,
814 timeout)) {
815 dev_err(dev, "Login timeout\n");
816 return -1;
817 }
818 } while (adapter->renegotiate);
819
Thomas Falcon3d166132018-01-10 19:39:52 -0600820 /* handle pending MAC address changes after successful login */
821 if (adapter->mac_change_pending) {
822 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
823 adapter->mac_change_pending = false;
824 }
825
John Allena57a5d22017-03-17 17:13:41 -0500826 return 0;
827}
828
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600829static void release_login_buffer(struct ibmvnic_adapter *adapter)
830{
831 kfree(adapter->login_buf);
832 adapter->login_buf = NULL;
833}
834
835static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
836{
837 kfree(adapter->login_rsp_buf);
838 adapter->login_rsp_buf = NULL;
839}
840
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400841static void release_resources(struct ibmvnic_adapter *adapter)
842{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200843 release_vpd_data(adapter);
844
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400845 release_tx_pools(adapter);
846 release_rx_pools(adapter);
847
Nathan Fontenot661a2622017-04-19 13:44:58 -0400848 release_error_buffers(adapter);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600849 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600850 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400851}
852
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400853static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
854{
855 struct net_device *netdev = adapter->netdev;
856 unsigned long timeout = msecs_to_jiffies(30000);
857 union ibmvnic_crq crq;
858 bool resend;
859 int rc;
860
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500861 netdev_dbg(netdev, "setting link state %d\n", link_state);
862
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400863 memset(&crq, 0, sizeof(crq));
864 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
865 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
866 crq.logical_link_state.link_state = link_state;
867
868 do {
869 resend = false;
870
871 reinit_completion(&adapter->init_done);
872 rc = ibmvnic_send_crq(adapter, &crq);
873 if (rc) {
874 netdev_err(netdev, "Failed to set link state\n");
875 return rc;
876 }
877
878 if (!wait_for_completion_timeout(&adapter->init_done,
879 timeout)) {
880 netdev_err(netdev, "timeout setting link state\n");
881 return -1;
882 }
883
884 if (adapter->init_done_rc == 1) {
885 /* Partuial success, delay and re-send */
886 mdelay(1000);
887 resend = true;
888 }
889 } while (resend);
890
891 return 0;
892}
893
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400894static int set_real_num_queues(struct net_device *netdev)
895{
896 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
897 int rc;
898
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500899 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
900 adapter->req_tx_queues, adapter->req_rx_queues);
901
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400902 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
903 if (rc) {
904 netdev_err(netdev, "failed to set the number of tx queues\n");
905 return rc;
906 }
907
908 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
909 if (rc)
910 netdev_err(netdev, "failed to set the number of rx queues\n");
911
912 return rc;
913}
914
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200915static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
916{
917 struct device *dev = &adapter->vdev->dev;
918 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200919 int len = 0;
920
921 if (adapter->vpd->buff)
922 len = adapter->vpd->len;
923
John Allen69d08dc2018-01-18 16:27:58 -0600924 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200925 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
926 crq.get_vpd_size.cmd = GET_VPD_SIZE;
927 ibmvnic_send_crq(adapter, &crq);
928 wait_for_completion(&adapter->fw_done);
929
930 if (!adapter->vpd->len)
931 return -ENODATA;
932
933 if (!adapter->vpd->buff)
934 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
935 else if (adapter->vpd->len != len)
936 adapter->vpd->buff =
937 krealloc(adapter->vpd->buff,
938 adapter->vpd->len, GFP_KERNEL);
939
940 if (!adapter->vpd->buff) {
941 dev_err(dev, "Could allocate VPD buffer\n");
942 return -ENOMEM;
943 }
944
945 adapter->vpd->dma_addr =
946 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
947 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200948 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200949 dev_err(dev, "Could not map VPD buffer\n");
950 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600951 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200952 return -ENOMEM;
953 }
954
955 reinit_completion(&adapter->fw_done);
956 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
957 crq.get_vpd.cmd = GET_VPD;
958 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
959 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
960 ibmvnic_send_crq(adapter, &crq);
961 wait_for_completion(&adapter->fw_done);
962
963 return 0;
964}
965
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400966static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500967{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400968 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600969 int rc;
John Allena57a5d22017-03-17 17:13:41 -0500970
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400971 rc = set_real_num_queues(netdev);
972 if (rc)
973 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500974
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200975 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
976 if (!adapter->vpd)
977 return -ENOMEM;
978
John Allen69d08dc2018-01-18 16:27:58 -0600979 /* Vital Product Data (VPD) */
980 rc = ibmvnic_get_vpd(adapter);
981 if (rc) {
982 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
983 return rc;
984 }
985
Thomas Falcon032c5e82015-12-21 11:26:06 -0600986 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400987
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600988 rc = init_napi(adapter);
989 if (rc)
990 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600991
Thomas Falcon032c5e82015-12-21 11:26:06 -0600992 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400993
994 rc = init_rx_pools(netdev);
995 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400996 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600997
Nathan Fontenotc657e322017-03-30 02:49:06 -0400998 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400999 return rc;
1000}
1001
Nathan Fontenoted651a12017-05-03 14:04:38 -04001002static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001003{
1004 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001005 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001006 int i, rc;
1007
Nathan Fontenot90c80142017-05-03 14:04:32 -04001008 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001009 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001010 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001011
Thomas Falcon032c5e82015-12-21 11:26:06 -06001012 /* We're ready to receive frames, enable the sub-crq interrupts and
1013 * set the logical link state to up
1014 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001015 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001016 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001017 if (prev_state == VNIC_CLOSED)
1018 enable_irq(adapter->rx_scrq[i]->irq);
1019 else
1020 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1021 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001022
Nathan Fontenoted651a12017-05-03 14:04:38 -04001023 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001024 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001025 if (prev_state == VNIC_CLOSED)
1026 enable_irq(adapter->tx_scrq[i]->irq);
1027 else
1028 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1029 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001030
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001031 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001032 if (rc) {
1033 for (i = 0; i < adapter->req_rx_queues; i++)
1034 napi_disable(&adapter->napi[i]);
1035 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001036 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001037 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001038
Nathan Fontenoted651a12017-05-03 14:04:38 -04001039 netif_tx_start_all_queues(netdev);
1040
1041 if (prev_state == VNIC_CLOSED) {
1042 for (i = 0; i < adapter->req_rx_queues; i++)
1043 napi_schedule(&adapter->napi[i]);
1044 }
1045
1046 adapter->state = VNIC_OPEN;
1047 return rc;
1048}
1049
1050static int ibmvnic_open(struct net_device *netdev)
1051{
1052 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001053 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001054
1055 mutex_lock(&adapter->reset_lock);
1056
1057 if (adapter->state != VNIC_CLOSED) {
1058 rc = ibmvnic_login(netdev);
1059 if (rc) {
1060 mutex_unlock(&adapter->reset_lock);
1061 return rc;
1062 }
1063
1064 rc = init_resources(adapter);
1065 if (rc) {
1066 netdev_err(netdev, "failed to initialize resources\n");
1067 release_resources(adapter);
1068 mutex_unlock(&adapter->reset_lock);
1069 return rc;
1070 }
1071 }
1072
1073 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001074 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001075
Nathan Fontenoted651a12017-05-03 14:04:38 -04001076 mutex_unlock(&adapter->reset_lock);
1077
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001078 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001079}
1080
Thomas Falcond0869c02018-02-13 18:23:43 -06001081static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1082{
1083 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001084 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001085 u64 rx_entries;
1086 int rx_scrqs;
1087 int i, j;
1088
1089 if (!adapter->rx_pool)
1090 return;
1091
1092 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1093 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1094
1095 /* Free any remaining skbs in the rx buffer pools */
1096 for (i = 0; i < rx_scrqs; i++) {
1097 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001098 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001099 continue;
1100
1101 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1102 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001103 rx_buff = &rx_pool->rx_buff[j];
1104 if (rx_buff && rx_buff->skb) {
1105 dev_kfree_skb_any(rx_buff->skb);
1106 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001107 }
1108 }
1109 }
1110}
1111
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001112static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1113{
1114 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001115 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001116 u64 tx_entries;
1117 int tx_scrqs;
1118 int i, j;
1119
1120 if (!adapter->tx_pool)
1121 return;
1122
1123 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1124 tx_entries = adapter->req_tx_entries_per_subcrq;
1125
1126 /* Free any remaining skbs in the tx buffer pools */
1127 for (i = 0; i < tx_scrqs; i++) {
1128 tx_pool = &adapter->tx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001129 if (!tx_pool && !tx_pool->tx_buff)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001130 continue;
1131
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001132 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001133 for (j = 0; j < tx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001134 tx_buff = &tx_pool->tx_buff[j];
1135 if (tx_buff && tx_buff->skb) {
1136 dev_kfree_skb_any(tx_buff->skb);
1137 tx_buff->skb = NULL;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001138 }
1139 }
1140 }
1141}
1142
Nathan Fontenoted651a12017-05-03 14:04:38 -04001143static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001144{
1145 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001146 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -05001147 int i;
1148
Nathan Fontenot90c80142017-05-03 14:04:32 -04001149 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001150
1151 /* ensure that transmissions are stopped if called by do_reset */
1152 if (adapter->resetting)
1153 netif_tx_disable(netdev);
1154 else
1155 netif_tx_stop_all_queues(netdev);
1156
John Allend944c3d62017-05-26 10:30:13 -04001157 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001158
1159 if (adapter->tx_scrq) {
1160 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001161 if (adapter->tx_scrq[i]->irq) {
1162 netdev_dbg(adapter->netdev,
1163 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001164 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001165 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001166 }
1167
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001168 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001169 if (rc)
1170 return rc;
1171
1172 if (adapter->rx_scrq) {
1173 for (i = 0; i < adapter->req_rx_queues; i++) {
1174 int retries = 10;
1175
1176 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1177 retries--;
1178 mdelay(100);
1179
1180 if (retries == 0)
1181 break;
1182 }
1183
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001184 if (adapter->rx_scrq[i]->irq) {
1185 netdev_dbg(adapter->netdev,
1186 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001187 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001188 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001189 }
1190 }
Thomas Falcond0869c02018-02-13 18:23:43 -06001191 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001192 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001193 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001194 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001195}
1196
Nathan Fontenoted651a12017-05-03 14:04:38 -04001197static int ibmvnic_close(struct net_device *netdev)
1198{
1199 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1200 int rc;
1201
1202 mutex_lock(&adapter->reset_lock);
1203 rc = __ibmvnic_close(netdev);
1204 mutex_unlock(&adapter->reset_lock);
1205
1206 return rc;
1207}
1208
Thomas Falconad7775d2016-04-01 17:20:34 -05001209/**
1210 * build_hdr_data - creates L2/L3/L4 header data buffer
1211 * @hdr_field - bitfield determining needed headers
1212 * @skb - socket buffer
1213 * @hdr_len - array of header lengths
1214 * @tot_len - total length of data
1215 *
1216 * Reads hdr_field to determine which headers are needed by firmware.
1217 * Builds a buffer containing these headers. Saves individual header
1218 * lengths and total buffer length to be used to build descriptors.
1219 */
1220static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1221 int *hdr_len, u8 *hdr_data)
1222{
1223 int len = 0;
1224 u8 *hdr;
1225
1226 hdr_len[0] = sizeof(struct ethhdr);
1227
1228 if (skb->protocol == htons(ETH_P_IP)) {
1229 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1230 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1231 hdr_len[2] = tcp_hdrlen(skb);
1232 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1233 hdr_len[2] = sizeof(struct udphdr);
1234 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1235 hdr_len[1] = sizeof(struct ipv6hdr);
1236 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1237 hdr_len[2] = tcp_hdrlen(skb);
1238 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1239 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001240 } else if (skb->protocol == htons(ETH_P_ARP)) {
1241 hdr_len[1] = arp_hdr_len(skb->dev);
1242 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001243 }
1244
1245 memset(hdr_data, 0, 120);
1246 if ((hdr_field >> 6) & 1) {
1247 hdr = skb_mac_header(skb);
1248 memcpy(hdr_data, hdr, hdr_len[0]);
1249 len += hdr_len[0];
1250 }
1251
1252 if ((hdr_field >> 5) & 1) {
1253 hdr = skb_network_header(skb);
1254 memcpy(hdr_data + len, hdr, hdr_len[1]);
1255 len += hdr_len[1];
1256 }
1257
1258 if ((hdr_field >> 4) & 1) {
1259 hdr = skb_transport_header(skb);
1260 memcpy(hdr_data + len, hdr, hdr_len[2]);
1261 len += hdr_len[2];
1262 }
1263 return len;
1264}
1265
1266/**
1267 * create_hdr_descs - create header and header extension descriptors
1268 * @hdr_field - bitfield determining needed headers
1269 * @data - buffer containing header data
1270 * @len - length of data buffer
1271 * @hdr_len - array of individual header lengths
1272 * @scrq_arr - descriptor array
1273 *
1274 * Creates header and, if needed, header extension descriptors and
1275 * places them in a descriptor array, scrq_arr
1276 */
1277
Thomas Falcon2de09682017-10-16 10:02:11 -05001278static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1279 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001280{
1281 union sub_crq hdr_desc;
1282 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001283 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001284 u8 *data, *cur;
1285 int tmp;
1286
1287 while (tmp_len > 0) {
1288 cur = hdr_data + len - tmp_len;
1289
1290 memset(&hdr_desc, 0, sizeof(hdr_desc));
1291 if (cur != hdr_data) {
1292 data = hdr_desc.hdr_ext.data;
1293 tmp = tmp_len > 29 ? 29 : tmp_len;
1294 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1295 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1296 hdr_desc.hdr_ext.len = tmp;
1297 } else {
1298 data = hdr_desc.hdr.data;
1299 tmp = tmp_len > 24 ? 24 : tmp_len;
1300 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1301 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1302 hdr_desc.hdr.len = tmp;
1303 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1304 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1305 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1306 hdr_desc.hdr.flag = hdr_field << 1;
1307 }
1308 memcpy(data, cur, tmp);
1309 tmp_len -= tmp;
1310 *scrq_arr = hdr_desc;
1311 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001312 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001313 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001314
1315 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001316}
1317
1318/**
1319 * build_hdr_descs_arr - build a header descriptor array
1320 * @skb - socket buffer
1321 * @num_entries - number of descriptors to be sent
1322 * @subcrq - first TX descriptor
1323 * @hdr_field - bit field determining which headers will be sent
1324 *
1325 * This function will build a TX descriptor array with applicable
1326 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1327 */
1328
1329static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1330 int *num_entries, u8 hdr_field)
1331{
1332 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001333 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001334 u8 *hdr_data = txbuff->hdr_data;
1335
1336 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1337 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001338 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001339 txbuff->indir_arr + 1);
1340}
1341
Thomas Falcon032c5e82015-12-21 11:26:06 -06001342static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1343{
1344 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1345 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001346 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001347 struct device *dev = &adapter->vdev->dev;
1348 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001349 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001350 struct ibmvnic_tx_pool *tx_pool;
1351 unsigned int tx_send_failed = 0;
1352 unsigned int tx_map_failed = 0;
1353 unsigned int tx_dropped = 0;
1354 unsigned int tx_packets = 0;
1355 unsigned int tx_bytes = 0;
1356 dma_addr_t data_dma_addr;
1357 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001358 unsigned long lpar_rc;
1359 union sub_crq tx_crq;
1360 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001361 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001362 unsigned char *dst;
1363 u64 *handle_array;
1364 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001365 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001366 int ret = 0;
1367
Nathan Fontenoted651a12017-05-03 14:04:38 -04001368 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001369 if (!netif_subqueue_stopped(netdev, skb))
1370 netif_stop_subqueue(netdev, queue_num);
1371 dev_kfree_skb_any(skb);
1372
Thomas Falcon032c5e82015-12-21 11:26:06 -06001373 tx_send_failed++;
1374 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001375 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001376 goto out;
1377 }
1378
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001379 tx_pool = &adapter->tx_pool[queue_num];
1380 tx_scrq = adapter->tx_scrq[queue_num];
1381 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1382 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1383 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1384
Thomas Falcon032c5e82015-12-21 11:26:06 -06001385 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001386
1387 if (skb_is_gso(skb)) {
1388 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1389 dst = tx_pool->tso_ltb.buff + offset;
1390 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1391 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1392 tx_pool->tso_index++;
1393 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1394 tx_pool->tso_index = 0;
1395 } else {
1396 offset = index * adapter->req_mtu;
1397 dst = tx_pool->long_term_buff.buff + offset;
1398 memset(dst, 0, adapter->req_mtu);
1399 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1400 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001401
Thomas Falcon15482052017-10-17 12:36:54 -05001402 if (skb_shinfo(skb)->nr_frags) {
1403 int cur, i;
1404
1405 /* Copy the head */
1406 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1407 cur = skb_headlen(skb);
1408
1409 /* Copy the frags */
1410 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1411 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1412
1413 memcpy(dst + cur,
1414 page_address(skb_frag_page(frag)) +
1415 frag->page_offset, skb_frag_size(frag));
1416 cur += skb_frag_size(frag);
1417 }
1418 } else {
1419 skb_copy_from_linear_data(skb, dst, skb->len);
1420 }
1421
Thomas Falcon032c5e82015-12-21 11:26:06 -06001422 tx_pool->consumer_index =
1423 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001424 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001425
1426 tx_buff = &tx_pool->tx_buff[index];
1427 tx_buff->skb = skb;
1428 tx_buff->data_dma[0] = data_dma_addr;
1429 tx_buff->data_len[0] = skb->len;
1430 tx_buff->index = index;
1431 tx_buff->pool_index = queue_num;
1432 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001433
1434 memset(&tx_crq, 0, sizeof(tx_crq));
1435 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1436 tx_crq.v1.type = IBMVNIC_TX_DESC;
1437 tx_crq.v1.n_crq_elem = 1;
1438 tx_crq.v1.n_sge = 1;
1439 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1440 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001441 if (skb_is_gso(skb))
1442 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1443 else
1444 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001445 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1446 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1447
1448 if (adapter->vlan_header_insertion) {
1449 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1450 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1451 }
1452
1453 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001454 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1455 proto = ip_hdr(skb)->protocol;
1456 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1457 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1458 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001459 }
1460
Thomas Falcona0dca102018-01-18 19:29:48 -06001461 if (proto == IPPROTO_TCP)
1462 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1463 else if (proto == IPPROTO_UDP)
1464 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1465
Thomas Falconad7775d2016-04-01 17:20:34 -05001466 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001467 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001468 hdrs += 2;
1469 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001470 if (skb_is_gso(skb)) {
1471 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1472 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1473 hdrs += 2;
1474 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001475 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001476 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001477 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1478 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001479 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001480 tx_buff->indir_arr[0] = tx_crq;
1481 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1482 sizeof(tx_buff->indir_arr),
1483 DMA_TO_DEVICE);
1484 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001485 dev_kfree_skb_any(skb);
1486 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001487 if (!firmware_has_feature(FW_FEATURE_CMO))
1488 dev_err(dev, "tx: unable to map descriptor array\n");
1489 tx_map_failed++;
1490 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001491 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001492 goto out;
1493 }
John Allen498cd8e2016-04-06 11:49:55 -05001494 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001495 (u64)tx_buff->indir_dma,
1496 (u64)num_entries);
1497 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001498 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001499 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1500 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001501 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001502 if (lpar_rc != H_SUCCESS) {
1503 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1504
1505 if (tx_pool->consumer_index == 0)
1506 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001507 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001508 else
1509 tx_pool->consumer_index--;
1510
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001511 dev_kfree_skb_any(skb);
1512 tx_buff->skb = NULL;
1513
Thomas Falconb8c80b82017-05-26 10:30:42 -04001514 if (lpar_rc == H_CLOSED) {
1515 /* Disable TX and report carrier off if queue is closed.
1516 * Firmware guarantees that a signal will be sent to the
1517 * driver, triggering a reset or some other action.
1518 */
1519 netif_tx_stop_all_queues(netdev);
1520 netif_carrier_off(netdev);
1521 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001522
Thomas Falcon032c5e82015-12-21 11:26:06 -06001523 tx_send_failed++;
1524 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001525 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001526 goto out;
1527 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001528
Thomas Falconffc385b2018-02-18 10:08:41 -06001529 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001530 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001531 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1532 netif_stop_subqueue(netdev, queue_num);
1533 }
1534
Thomas Falcon032c5e82015-12-21 11:26:06 -06001535 tx_packets++;
1536 tx_bytes += skb->len;
1537 txq->trans_start = jiffies;
1538 ret = NETDEV_TX_OK;
1539
1540out:
1541 netdev->stats.tx_dropped += tx_dropped;
1542 netdev->stats.tx_bytes += tx_bytes;
1543 netdev->stats.tx_packets += tx_packets;
1544 adapter->tx_send_failed += tx_send_failed;
1545 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001546 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1547 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1548 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001549
1550 return ret;
1551}
1552
1553static void ibmvnic_set_multi(struct net_device *netdev)
1554{
1555 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1556 struct netdev_hw_addr *ha;
1557 union ibmvnic_crq crq;
1558
1559 memset(&crq, 0, sizeof(crq));
1560 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1561 crq.request_capability.cmd = REQUEST_CAPABILITY;
1562
1563 if (netdev->flags & IFF_PROMISC) {
1564 if (!adapter->promisc_supported)
1565 return;
1566 } else {
1567 if (netdev->flags & IFF_ALLMULTI) {
1568 /* Accept all multicast */
1569 memset(&crq, 0, sizeof(crq));
1570 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1571 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1572 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1573 ibmvnic_send_crq(adapter, &crq);
1574 } else if (netdev_mc_empty(netdev)) {
1575 /* Reject all multicast */
1576 memset(&crq, 0, sizeof(crq));
1577 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1578 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1579 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1580 ibmvnic_send_crq(adapter, &crq);
1581 } else {
1582 /* Accept one or more multicast(s) */
1583 netdev_for_each_mc_addr(ha, netdev) {
1584 memset(&crq, 0, sizeof(crq));
1585 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1586 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1587 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1588 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1589 ha->addr);
1590 ibmvnic_send_crq(adapter, &crq);
1591 }
1592 }
1593 }
1594}
1595
John Allenc26eba02017-10-26 16:23:25 -05001596static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001597{
1598 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1599 struct sockaddr *addr = p;
1600 union ibmvnic_crq crq;
1601
1602 if (!is_valid_ether_addr(addr->sa_data))
1603 return -EADDRNOTAVAIL;
1604
1605 memset(&crq, 0, sizeof(crq));
1606 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1607 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1608 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001609
1610 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001611 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001612 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001613 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001614 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001615}
1616
John Allenc26eba02017-10-26 16:23:25 -05001617static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1618{
1619 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1620 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001621 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001622
Thomas Falcon3d166132018-01-10 19:39:52 -06001623 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001624 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1625 adapter->mac_change_pending = true;
1626 return 0;
1627 }
1628
Thomas Falconf8136142018-01-29 13:45:05 -06001629 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001630
Thomas Falconf8136142018-01-29 13:45:05 -06001631 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001632}
1633
Nathan Fontenoted651a12017-05-03 14:04:38 -04001634/**
1635 * do_reset returns zero if we are able to keep processing reset events, or
1636 * non-zero if we hit a fatal error and must halt.
1637 */
1638static int do_reset(struct ibmvnic_adapter *adapter,
1639 struct ibmvnic_rwi *rwi, u32 reset_state)
1640{
John Allen896d8692018-01-18 16:26:31 -06001641 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001642 struct net_device *netdev = adapter->netdev;
1643 int i, rc;
1644
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001645 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1646 rwi->reset_reason);
1647
Nathan Fontenoted651a12017-05-03 14:04:38 -04001648 netif_carrier_off(netdev);
1649 adapter->reset_reason = rwi->reset_reason;
1650
John Allen896d8692018-01-18 16:26:31 -06001651 old_num_rx_queues = adapter->req_rx_queues;
1652 old_num_tx_queues = adapter->req_tx_queues;
1653
Nathan Fontenoted651a12017-05-03 14:04:38 -04001654 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1655 rc = ibmvnic_reenable_crq_queue(adapter);
1656 if (rc)
1657 return 0;
1658 }
1659
1660 rc = __ibmvnic_close(netdev);
1661 if (rc)
1662 return rc;
1663
John Allenc26eba02017-10-26 16:23:25 -05001664 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1665 adapter->wait_for_reset) {
1666 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001667 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001668 release_crq_queue(adapter);
1669 }
1670
John Allen8cb31cf2017-05-26 10:30:37 -04001671 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1672 /* remove the closed state so when we call open it appears
1673 * we are coming from the probed state.
1674 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001675 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001676
John Allen8cb31cf2017-05-26 10:30:37 -04001677 rc = ibmvnic_init(adapter);
1678 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001679 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001680
1681 /* If the adapter was in PROBE state prior to the reset,
1682 * exit here.
1683 */
1684 if (reset_state == VNIC_PROBED)
1685 return 0;
1686
1687 rc = ibmvnic_login(netdev);
1688 if (rc) {
1689 adapter->state = VNIC_PROBED;
1690 return 0;
1691 }
1692
John Allenc26eba02017-10-26 16:23:25 -05001693 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1694 adapter->wait_for_reset) {
1695 rc = init_resources(adapter);
1696 if (rc)
1697 return rc;
John Allen896d8692018-01-18 16:26:31 -06001698 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1699 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001700 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001701 release_rx_pools(adapter);
1702 release_tx_pools(adapter);
1703 init_rx_pools(netdev);
1704 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001705
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001706 release_napi(adapter);
1707 init_napi(adapter);
John Allenc26eba02017-10-26 16:23:25 -05001708 } else {
1709 rc = reset_tx_pools(adapter);
1710 if (rc)
1711 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001712
John Allenc26eba02017-10-26 16:23:25 -05001713 rc = reset_rx_pools(adapter);
1714 if (rc)
1715 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001716
John Allenc26eba02017-10-26 16:23:25 -05001717 if (reset_state == VNIC_CLOSED)
1718 return 0;
1719 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001720 }
1721
Nathan Fontenoted651a12017-05-03 14:04:38 -04001722 rc = __ibmvnic_open(netdev);
1723 if (rc) {
1724 if (list_empty(&adapter->rwi_list))
1725 adapter->state = VNIC_CLOSED;
1726 else
1727 adapter->state = reset_state;
1728
1729 return 0;
1730 }
1731
Nathan Fontenoted651a12017-05-03 14:04:38 -04001732 /* kick napi */
1733 for (i = 0; i < adapter->req_rx_queues; i++)
1734 napi_schedule(&adapter->napi[i]);
1735
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001736 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1737 netdev_notify_peers(netdev);
1738
Thomas Falconcc85c022018-02-13 15:32:50 -06001739 netif_carrier_on(netdev);
1740
Nathan Fontenoted651a12017-05-03 14:04:38 -04001741 return 0;
1742}
1743
1744static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1745{
1746 struct ibmvnic_rwi *rwi;
1747
1748 mutex_lock(&adapter->rwi_lock);
1749
1750 if (!list_empty(&adapter->rwi_list)) {
1751 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1752 list);
1753 list_del(&rwi->list);
1754 } else {
1755 rwi = NULL;
1756 }
1757
1758 mutex_unlock(&adapter->rwi_lock);
1759 return rwi;
1760}
1761
1762static void free_all_rwi(struct ibmvnic_adapter *adapter)
1763{
1764 struct ibmvnic_rwi *rwi;
1765
1766 rwi = get_next_rwi(adapter);
1767 while (rwi) {
1768 kfree(rwi);
1769 rwi = get_next_rwi(adapter);
1770 }
1771}
1772
1773static void __ibmvnic_reset(struct work_struct *work)
1774{
1775 struct ibmvnic_rwi *rwi;
1776 struct ibmvnic_adapter *adapter;
1777 struct net_device *netdev;
1778 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001779 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001780
1781 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1782 netdev = adapter->netdev;
1783
1784 mutex_lock(&adapter->reset_lock);
1785 adapter->resetting = true;
1786 reset_state = adapter->state;
1787
1788 rwi = get_next_rwi(adapter);
1789 while (rwi) {
1790 rc = do_reset(adapter, rwi, reset_state);
1791 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001792 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001793 break;
1794
1795 rwi = get_next_rwi(adapter);
1796 }
1797
John Allenc26eba02017-10-26 16:23:25 -05001798 if (adapter->wait_for_reset) {
1799 adapter->wait_for_reset = false;
1800 adapter->reset_done_rc = rc;
1801 complete(&adapter->reset_done);
1802 }
1803
Nathan Fontenoted651a12017-05-03 14:04:38 -04001804 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001805 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001806 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001807 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001808 return;
1809 }
1810
1811 adapter->resetting = false;
1812 mutex_unlock(&adapter->reset_lock);
1813}
1814
1815static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1816 enum ibmvnic_reset_reason reason)
1817{
1818 struct ibmvnic_rwi *rwi, *tmp;
1819 struct net_device *netdev = adapter->netdev;
1820 struct list_head *entry;
1821
1822 if (adapter->state == VNIC_REMOVING ||
1823 adapter->state == VNIC_REMOVED) {
1824 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1825 return;
1826 }
1827
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001828 if (adapter->state == VNIC_PROBING) {
1829 netdev_warn(netdev, "Adapter reset during probe\n");
1830 adapter->init_done_rc = EAGAIN;
1831 return;
1832 }
1833
Nathan Fontenoted651a12017-05-03 14:04:38 -04001834 mutex_lock(&adapter->rwi_lock);
1835
1836 list_for_each(entry, &adapter->rwi_list) {
1837 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1838 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001839 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001840 mutex_unlock(&adapter->rwi_lock);
1841 return;
1842 }
1843 }
1844
1845 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1846 if (!rwi) {
1847 mutex_unlock(&adapter->rwi_lock);
1848 ibmvnic_close(netdev);
1849 return;
1850 }
1851
1852 rwi->reset_reason = reason;
1853 list_add_tail(&rwi->list, &adapter->rwi_list);
1854 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001855
1856 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001857 schedule_work(&adapter->ibmvnic_reset);
1858}
1859
Thomas Falcon032c5e82015-12-21 11:26:06 -06001860static void ibmvnic_tx_timeout(struct net_device *dev)
1861{
1862 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001863
Nathan Fontenoted651a12017-05-03 14:04:38 -04001864 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001865}
1866
1867static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1868 struct ibmvnic_rx_buff *rx_buff)
1869{
1870 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1871
1872 rx_buff->skb = NULL;
1873
1874 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1875 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1876
1877 atomic_dec(&pool->available);
1878}
1879
1880static int ibmvnic_poll(struct napi_struct *napi, int budget)
1881{
1882 struct net_device *netdev = napi->dev;
1883 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1884 int scrq_num = (int)(napi - adapter->napi);
1885 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001886
Thomas Falcon032c5e82015-12-21 11:26:06 -06001887restart_poll:
1888 while (frames_processed < budget) {
1889 struct sk_buff *skb;
1890 struct ibmvnic_rx_buff *rx_buff;
1891 union sub_crq *next;
1892 u32 length;
1893 u16 offset;
1894 u8 flags = 0;
1895
John Allen34686562018-02-06 16:21:49 -06001896 if (unlikely(adapter->resetting &&
1897 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001898 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1899 napi_complete_done(napi, frames_processed);
1900 return frames_processed;
1901 }
1902
Thomas Falcon032c5e82015-12-21 11:26:06 -06001903 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1904 break;
1905 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1906 rx_buff =
1907 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1908 rx_comp.correlator);
1909 /* do error checking */
1910 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001911 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1912 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001913 /* free the entry */
1914 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001915 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001916 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001917 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06001918 } else if (!rx_buff->skb) {
1919 /* free the entry */
1920 next->rx_comp.first = 0;
1921 remove_buff_from_pool(adapter, rx_buff);
1922 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001923 }
1924
1925 length = be32_to_cpu(next->rx_comp.len);
1926 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1927 flags = next->rx_comp.flags;
1928 skb = rx_buff->skb;
1929 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1930 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001931
1932 /* VLAN Header has been stripped by the system firmware and
1933 * needs to be inserted by the driver
1934 */
1935 if (adapter->rx_vlan_header_insertion &&
1936 (flags & IBMVNIC_VLAN_STRIPPED))
1937 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1938 ntohs(next->rx_comp.vlan_tci));
1939
Thomas Falcon032c5e82015-12-21 11:26:06 -06001940 /* free the entry */
1941 next->rx_comp.first = 0;
1942 remove_buff_from_pool(adapter, rx_buff);
1943
1944 skb_put(skb, length);
1945 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001946 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001947
1948 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1949 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1950 skb->ip_summed = CHECKSUM_UNNECESSARY;
1951 }
1952
1953 length = skb->len;
1954 napi_gro_receive(napi, skb); /* send it up */
1955 netdev->stats.rx_packets++;
1956 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001957 adapter->rx_stats_buffers[scrq_num].packets++;
1958 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001959 frames_processed++;
1960 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001961
1962 if (adapter->state != VNIC_CLOSING)
1963 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001964
1965 if (frames_processed < budget) {
1966 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001967 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001968 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1969 napi_reschedule(napi)) {
1970 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1971 goto restart_poll;
1972 }
1973 }
1974 return frames_processed;
1975}
1976
1977#ifdef CONFIG_NET_POLL_CONTROLLER
1978static void ibmvnic_netpoll_controller(struct net_device *dev)
1979{
1980 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1981 int i;
1982
1983 replenish_pools(netdev_priv(dev));
1984 for (i = 0; i < adapter->req_rx_queues; i++)
1985 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1986 adapter->rx_scrq[i]);
1987}
1988#endif
1989
John Allenc26eba02017-10-26 16:23:25 -05001990static int wait_for_reset(struct ibmvnic_adapter *adapter)
1991{
1992 adapter->fallback.mtu = adapter->req_mtu;
1993 adapter->fallback.rx_queues = adapter->req_rx_queues;
1994 adapter->fallback.tx_queues = adapter->req_tx_queues;
1995 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1996 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1997
1998 init_completion(&adapter->reset_done);
1999 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2000 adapter->wait_for_reset = true;
2001 wait_for_completion(&adapter->reset_done);
2002
2003 if (adapter->reset_done_rc) {
2004 adapter->desired.mtu = adapter->fallback.mtu;
2005 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2006 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2007 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2008 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2009
2010 init_completion(&adapter->reset_done);
2011 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2012 wait_for_completion(&adapter->reset_done);
2013 }
2014 adapter->wait_for_reset = false;
2015
2016 return adapter->reset_done_rc;
2017}
2018
John Allen3a807b72017-06-06 16:55:52 -05002019static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2020{
John Allenc26eba02017-10-26 16:23:25 -05002021 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2022
2023 adapter->desired.mtu = new_mtu + ETH_HLEN;
2024
2025 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002026}
2027
Thomas Falcon032c5e82015-12-21 11:26:06 -06002028static const struct net_device_ops ibmvnic_netdev_ops = {
2029 .ndo_open = ibmvnic_open,
2030 .ndo_stop = ibmvnic_close,
2031 .ndo_start_xmit = ibmvnic_xmit,
2032 .ndo_set_rx_mode = ibmvnic_set_multi,
2033 .ndo_set_mac_address = ibmvnic_set_mac,
2034 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002035 .ndo_tx_timeout = ibmvnic_tx_timeout,
2036#ifdef CONFIG_NET_POLL_CONTROLLER
2037 .ndo_poll_controller = ibmvnic_netpoll_controller,
2038#endif
John Allen3a807b72017-06-06 16:55:52 -05002039 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002040};
2041
2042/* ethtool functions */
2043
Philippe Reynes8a433792017-01-07 22:37:29 +01002044static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2045 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002046{
Philippe Reynes8a433792017-01-07 22:37:29 +01002047 u32 supported, advertising;
2048
2049 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002050 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002051 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002052 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002053 cmd->base.speed = SPEED_1000;
2054 cmd->base.duplex = DUPLEX_FULL;
2055 cmd->base.port = PORT_FIBRE;
2056 cmd->base.phy_address = 0;
2057 cmd->base.autoneg = AUTONEG_ENABLE;
2058
2059 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2060 supported);
2061 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2062 advertising);
2063
Thomas Falcon032c5e82015-12-21 11:26:06 -06002064 return 0;
2065}
2066
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002067static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002068 struct ethtool_drvinfo *info)
2069{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002070 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2071
Thomas Falcon032c5e82015-12-21 11:26:06 -06002072 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2073 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002074 strlcpy(info->fw_version, adapter->fw_version,
2075 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002076}
2077
2078static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2079{
2080 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2081
2082 return adapter->msg_enable;
2083}
2084
2085static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2086{
2087 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2088
2089 adapter->msg_enable = data;
2090}
2091
2092static u32 ibmvnic_get_link(struct net_device *netdev)
2093{
2094 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2095
2096 /* Don't need to send a query because we request a logical link up at
2097 * init and then we wait for link state indications
2098 */
2099 return adapter->logical_link_state;
2100}
2101
2102static void ibmvnic_get_ringparam(struct net_device *netdev,
2103 struct ethtool_ringparam *ring)
2104{
John Allenbc131b32017-08-02 16:46:30 -05002105 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2106
2107 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2108 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002109 ring->rx_mini_max_pending = 0;
2110 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002111 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2112 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002113 ring->rx_mini_pending = 0;
2114 ring->rx_jumbo_pending = 0;
2115}
2116
John Allenc26eba02017-10-26 16:23:25 -05002117static int ibmvnic_set_ringparam(struct net_device *netdev,
2118 struct ethtool_ringparam *ring)
2119{
2120 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2121
2122 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2123 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2124 netdev_err(netdev, "Invalid request.\n");
2125 netdev_err(netdev, "Max tx buffers = %llu\n",
2126 adapter->max_rx_add_entries_per_subcrq);
2127 netdev_err(netdev, "Max rx buffers = %llu\n",
2128 adapter->max_tx_entries_per_subcrq);
2129 return -EINVAL;
2130 }
2131
2132 adapter->desired.rx_entries = ring->rx_pending;
2133 adapter->desired.tx_entries = ring->tx_pending;
2134
2135 return wait_for_reset(adapter);
2136}
2137
John Allenc2dbeb62017-08-02 16:47:17 -05002138static void ibmvnic_get_channels(struct net_device *netdev,
2139 struct ethtool_channels *channels)
2140{
2141 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2142
2143 channels->max_rx = adapter->max_rx_queues;
2144 channels->max_tx = adapter->max_tx_queues;
2145 channels->max_other = 0;
2146 channels->max_combined = 0;
2147 channels->rx_count = adapter->req_rx_queues;
2148 channels->tx_count = adapter->req_tx_queues;
2149 channels->other_count = 0;
2150 channels->combined_count = 0;
2151}
2152
John Allenc26eba02017-10-26 16:23:25 -05002153static int ibmvnic_set_channels(struct net_device *netdev,
2154 struct ethtool_channels *channels)
2155{
2156 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2157
2158 adapter->desired.rx_queues = channels->rx_count;
2159 adapter->desired.tx_queues = channels->tx_count;
2160
2161 return wait_for_reset(adapter);
2162}
2163
Thomas Falcon032c5e82015-12-21 11:26:06 -06002164static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2165{
John Allen3d52b592017-08-02 16:44:14 -05002166 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002167 int i;
2168
2169 if (stringset != ETH_SS_STATS)
2170 return;
2171
2172 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2173 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002174
2175 for (i = 0; i < adapter->req_tx_queues; i++) {
2176 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2177 data += ETH_GSTRING_LEN;
2178
2179 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2180 data += ETH_GSTRING_LEN;
2181
2182 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2183 data += ETH_GSTRING_LEN;
2184 }
2185
2186 for (i = 0; i < adapter->req_rx_queues; i++) {
2187 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2188 data += ETH_GSTRING_LEN;
2189
2190 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2191 data += ETH_GSTRING_LEN;
2192
2193 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2194 data += ETH_GSTRING_LEN;
2195 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002196}
2197
2198static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2199{
John Allen3d52b592017-08-02 16:44:14 -05002200 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2201
Thomas Falcon032c5e82015-12-21 11:26:06 -06002202 switch (sset) {
2203 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002204 return ARRAY_SIZE(ibmvnic_stats) +
2205 adapter->req_tx_queues * NUM_TX_STATS +
2206 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002207 default:
2208 return -EOPNOTSUPP;
2209 }
2210}
2211
2212static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2213 struct ethtool_stats *stats, u64 *data)
2214{
2215 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2216 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002217 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002218
2219 memset(&crq, 0, sizeof(crq));
2220 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2221 crq.request_statistics.cmd = REQUEST_STATISTICS;
2222 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2223 crq.request_statistics.len =
2224 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002225
2226 /* Wait for data to be written */
2227 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002228 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002229 wait_for_completion(&adapter->stats_done);
2230
2231 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002232 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2233 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002234
2235 for (j = 0; j < adapter->req_tx_queues; j++) {
2236 data[i] = adapter->tx_stats_buffers[j].packets;
2237 i++;
2238 data[i] = adapter->tx_stats_buffers[j].bytes;
2239 i++;
2240 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2241 i++;
2242 }
2243
2244 for (j = 0; j < adapter->req_rx_queues; j++) {
2245 data[i] = adapter->rx_stats_buffers[j].packets;
2246 i++;
2247 data[i] = adapter->rx_stats_buffers[j].bytes;
2248 i++;
2249 data[i] = adapter->rx_stats_buffers[j].interrupts;
2250 i++;
2251 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002252}
2253
2254static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002255 .get_drvinfo = ibmvnic_get_drvinfo,
2256 .get_msglevel = ibmvnic_get_msglevel,
2257 .set_msglevel = ibmvnic_set_msglevel,
2258 .get_link = ibmvnic_get_link,
2259 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002260 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002261 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002262 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002263 .get_strings = ibmvnic_get_strings,
2264 .get_sset_count = ibmvnic_get_sset_count,
2265 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002266 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267};
2268
2269/* Routines for managing CRQs/sCRQs */
2270
Nathan Fontenot57a49432017-05-26 10:31:12 -04002271static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2272 struct ibmvnic_sub_crq_queue *scrq)
2273{
2274 int rc;
2275
2276 if (scrq->irq) {
2277 free_irq(scrq->irq, scrq);
2278 irq_dispose_mapping(scrq->irq);
2279 scrq->irq = 0;
2280 }
2281
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002282 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002283 scrq->cur = 0;
2284
2285 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2286 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2287 return rc;
2288}
2289
2290static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2291{
2292 int i, rc;
2293
2294 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002295 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002296 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2297 if (rc)
2298 return rc;
2299 }
2300
2301 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002302 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002303 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2304 if (rc)
2305 return rc;
2306 }
2307
Nathan Fontenot57a49432017-05-26 10:31:12 -04002308 return rc;
2309}
2310
Thomas Falcon032c5e82015-12-21 11:26:06 -06002311static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002312 struct ibmvnic_sub_crq_queue *scrq,
2313 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002314{
2315 struct device *dev = &adapter->vdev->dev;
2316 long rc;
2317
2318 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2319
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002320 if (do_h_free) {
2321 /* Close the sub-crqs */
2322 do {
2323 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2324 adapter->vdev->unit_address,
2325 scrq->crq_num);
2326 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002327
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002328 if (rc) {
2329 netdev_err(adapter->netdev,
2330 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2331 scrq->crq_num, rc);
2332 }
Thomas Falconffa73852017-04-19 13:44:29 -04002333 }
2334
Thomas Falcon032c5e82015-12-21 11:26:06 -06002335 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2336 DMA_BIDIRECTIONAL);
2337 free_pages((unsigned long)scrq->msgs, 2);
2338 kfree(scrq);
2339}
2340
2341static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2342 *adapter)
2343{
2344 struct device *dev = &adapter->vdev->dev;
2345 struct ibmvnic_sub_crq_queue *scrq;
2346 int rc;
2347
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002348 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002349 if (!scrq)
2350 return NULL;
2351
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002352 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002353 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002354 if (!scrq->msgs) {
2355 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2356 goto zero_page_failed;
2357 }
2358
2359 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2360 DMA_BIDIRECTIONAL);
2361 if (dma_mapping_error(dev, scrq->msg_token)) {
2362 dev_warn(dev, "Couldn't map crq queue messages page\n");
2363 goto map_failed;
2364 }
2365
2366 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2367 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2368
2369 if (rc == H_RESOURCE)
2370 rc = ibmvnic_reset_crq(adapter);
2371
2372 if (rc == H_CLOSED) {
2373 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2374 } else if (rc) {
2375 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2376 goto reg_failed;
2377 }
2378
Thomas Falcon032c5e82015-12-21 11:26:06 -06002379 scrq->adapter = adapter;
2380 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002381 spin_lock_init(&scrq->lock);
2382
2383 netdev_dbg(adapter->netdev,
2384 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2385 scrq->crq_num, scrq->hw_irq, scrq->irq);
2386
2387 return scrq;
2388
Thomas Falcon032c5e82015-12-21 11:26:06 -06002389reg_failed:
2390 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2391 DMA_BIDIRECTIONAL);
2392map_failed:
2393 free_pages((unsigned long)scrq->msgs, 2);
2394zero_page_failed:
2395 kfree(scrq);
2396
2397 return NULL;
2398}
2399
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002400static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002401{
2402 int i;
2403
2404 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002405 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002406 if (!adapter->tx_scrq[i])
2407 continue;
2408
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002409 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2410 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002411 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002412 free_irq(adapter->tx_scrq[i]->irq,
2413 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002414 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002415 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002417
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002418 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2419 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002420 }
2421
Nathan Fontenot9501df32017-03-15 23:38:07 -04002422 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002423 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002424 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002425 }
2426
2427 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002428 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002429 if (!adapter->rx_scrq[i])
2430 continue;
2431
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002432 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2433 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002434 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002435 free_irq(adapter->rx_scrq[i]->irq,
2436 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002437 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002438 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002440
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002441 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2442 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002443 }
2444
Nathan Fontenot9501df32017-03-15 23:38:07 -04002445 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002447 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002448 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002449}
2450
2451static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2452 struct ibmvnic_sub_crq_queue *scrq)
2453{
2454 struct device *dev = &adapter->vdev->dev;
2455 unsigned long rc;
2456
2457 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2458 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2459 if (rc)
2460 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2461 scrq->hw_irq, rc);
2462 return rc;
2463}
2464
2465static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2466 struct ibmvnic_sub_crq_queue *scrq)
2467{
2468 struct device *dev = &adapter->vdev->dev;
2469 unsigned long rc;
2470
2471 if (scrq->hw_irq > 0x100000000ULL) {
2472 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2473 return 1;
2474 }
2475
2476 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2477 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2478 if (rc)
2479 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2480 scrq->hw_irq, rc);
2481 return rc;
2482}
2483
2484static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2485 struct ibmvnic_sub_crq_queue *scrq)
2486{
2487 struct device *dev = &adapter->vdev->dev;
2488 struct ibmvnic_tx_buff *txbuff;
2489 union sub_crq *next;
2490 int index;
2491 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002492 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002493
2494restart_loop:
2495 while (pending_scrq(adapter, scrq)) {
2496 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002497 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002498
2499 next = ibmvnic_next_scrq(adapter, scrq);
2500 for (i = 0; i < next->tx_comp.num_comps; i++) {
2501 if (next->tx_comp.rcs[i]) {
2502 dev_err(dev, "tx error %x\n",
2503 next->tx_comp.rcs[i]);
2504 continue;
2505 }
2506 index = be32_to_cpu(next->tx_comp.correlators[i]);
2507 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2508
2509 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2510 if (!txbuff->data_dma[j])
2511 continue;
2512
2513 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002514 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002515 /* if sub_crq was sent indirectly */
2516 first = txbuff->indir_arr[0].generic.first;
2517 if (first == IBMVNIC_CRQ_CMD) {
2518 dma_unmap_single(dev, txbuff->indir_dma,
2519 sizeof(txbuff->indir_arr),
2520 DMA_TO_DEVICE);
2521 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002523 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002524 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002525 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002526 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527
Thomas Falconffc385b2018-02-18 10:08:41 -06002528 num_entries += txbuff->num_entries;
2529
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2531 producer_index] = index;
2532 adapter->tx_pool[pool].producer_index =
2533 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002534 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002535 }
2536 /* remove tx_comp scrq*/
2537 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002538
Thomas Falconffc385b2018-02-18 10:08:41 -06002539 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002540 (adapter->req_tx_entries_per_subcrq / 2) &&
2541 __netif_subqueue_stopped(adapter->netdev,
2542 scrq->pool_index)) {
2543 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2544 netdev_info(adapter->netdev, "Started queue %d\n",
2545 scrq->pool_index);
2546 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002547 }
2548
2549 enable_scrq_irq(adapter, scrq);
2550
2551 if (pending_scrq(adapter, scrq)) {
2552 disable_scrq_irq(adapter, scrq);
2553 goto restart_loop;
2554 }
2555
2556 return 0;
2557}
2558
2559static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2560{
2561 struct ibmvnic_sub_crq_queue *scrq = instance;
2562 struct ibmvnic_adapter *adapter = scrq->adapter;
2563
2564 disable_scrq_irq(adapter, scrq);
2565 ibmvnic_complete_tx(adapter, scrq);
2566
2567 return IRQ_HANDLED;
2568}
2569
2570static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2571{
2572 struct ibmvnic_sub_crq_queue *scrq = instance;
2573 struct ibmvnic_adapter *adapter = scrq->adapter;
2574
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002575 /* When booting a kdump kernel we can hit pending interrupts
2576 * prior to completing driver initialization.
2577 */
2578 if (unlikely(adapter->state != VNIC_OPEN))
2579 return IRQ_NONE;
2580
John Allen3d52b592017-08-02 16:44:14 -05002581 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2582
Thomas Falcon032c5e82015-12-21 11:26:06 -06002583 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2584 disable_scrq_irq(adapter, scrq);
2585 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2586 }
2587
2588 return IRQ_HANDLED;
2589}
2590
Thomas Falconea22d512016-07-06 15:35:17 -05002591static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2592{
2593 struct device *dev = &adapter->vdev->dev;
2594 struct ibmvnic_sub_crq_queue *scrq;
2595 int i = 0, j = 0;
2596 int rc = 0;
2597
2598 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002599 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2600 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002601 scrq = adapter->tx_scrq[i];
2602 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2603
Michael Ellerman99c17902016-09-10 19:59:05 +10002604 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002605 rc = -EINVAL;
2606 dev_err(dev, "Error mapping irq\n");
2607 goto req_tx_irq_failed;
2608 }
2609
2610 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2611 0, "ibmvnic_tx", scrq);
2612
2613 if (rc) {
2614 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2615 scrq->irq, rc);
2616 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002617 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002618 }
2619 }
2620
2621 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002622 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2623 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002624 scrq = adapter->rx_scrq[i];
2625 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002626 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002627 rc = -EINVAL;
2628 dev_err(dev, "Error mapping irq\n");
2629 goto req_rx_irq_failed;
2630 }
2631 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2632 0, "ibmvnic_rx", scrq);
2633 if (rc) {
2634 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2635 scrq->irq, rc);
2636 irq_dispose_mapping(scrq->irq);
2637 goto req_rx_irq_failed;
2638 }
2639 }
2640 return rc;
2641
2642req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002643 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002644 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2645 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002646 }
Thomas Falconea22d512016-07-06 15:35:17 -05002647 i = adapter->req_tx_queues;
2648req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002649 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002650 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2651 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002652 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002653 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002654 return rc;
2655}
2656
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002657static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658{
2659 struct device *dev = &adapter->vdev->dev;
2660 struct ibmvnic_sub_crq_queue **allqueues;
2661 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002662 int total_queues;
2663 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002664 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002665
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2667
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002668 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002669 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002670 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002671
2672 for (i = 0; i < total_queues; i++) {
2673 allqueues[i] = init_sub_crq_queue(adapter);
2674 if (!allqueues[i]) {
2675 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2676 break;
2677 }
2678 registered_queues++;
2679 }
2680
2681 /* Make sure we were able to register the minimum number of queues */
2682 if (registered_queues <
2683 adapter->min_tx_queues + adapter->min_rx_queues) {
2684 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2685 goto tx_failed;
2686 }
2687
2688 /* Distribute the failed allocated queues*/
2689 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2690 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2691 switch (i % 3) {
2692 case 0:
2693 if (adapter->req_rx_queues > adapter->min_rx_queues)
2694 adapter->req_rx_queues--;
2695 else
2696 more++;
2697 break;
2698 case 1:
2699 if (adapter->req_tx_queues > adapter->min_tx_queues)
2700 adapter->req_tx_queues--;
2701 else
2702 more++;
2703 break;
2704 }
2705 }
2706
2707 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002708 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709 if (!adapter->tx_scrq)
2710 goto tx_failed;
2711
2712 for (i = 0; i < adapter->req_tx_queues; i++) {
2713 adapter->tx_scrq[i] = allqueues[i];
2714 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002715 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002716 }
2717
2718 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002719 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720 if (!adapter->rx_scrq)
2721 goto rx_failed;
2722
2723 for (i = 0; i < adapter->req_rx_queues; i++) {
2724 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2725 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002726 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 }
2728
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002729 kfree(allqueues);
2730 return 0;
2731
2732rx_failed:
2733 kfree(adapter->tx_scrq);
2734 adapter->tx_scrq = NULL;
2735tx_failed:
2736 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002737 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002738 kfree(allqueues);
2739 return -1;
2740}
2741
2742static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2743{
2744 struct device *dev = &adapter->vdev->dev;
2745 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002746 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002747
2748 if (!retry) {
2749 /* Sub-CRQ entries are 32 byte long */
2750 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2751
2752 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2753 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2754 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2755 return;
2756 }
2757
John Allenc26eba02017-10-26 16:23:25 -05002758 if (adapter->desired.mtu)
2759 adapter->req_mtu = adapter->desired.mtu;
2760 else
2761 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002762
John Allenc26eba02017-10-26 16:23:25 -05002763 if (!adapter->desired.tx_entries)
2764 adapter->desired.tx_entries =
2765 adapter->max_tx_entries_per_subcrq;
2766 if (!adapter->desired.rx_entries)
2767 adapter->desired.rx_entries =
2768 adapter->max_rx_add_entries_per_subcrq;
2769
2770 max_entries = IBMVNIC_MAX_LTB_SIZE /
2771 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2772
2773 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2774 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2775 adapter->desired.tx_entries = max_entries;
2776 }
2777
2778 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2779 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2780 adapter->desired.rx_entries = max_entries;
2781 }
2782
2783 if (adapter->desired.tx_entries)
2784 adapter->req_tx_entries_per_subcrq =
2785 adapter->desired.tx_entries;
2786 else
2787 adapter->req_tx_entries_per_subcrq =
2788 adapter->max_tx_entries_per_subcrq;
2789
2790 if (adapter->desired.rx_entries)
2791 adapter->req_rx_add_entries_per_subcrq =
2792 adapter->desired.rx_entries;
2793 else
2794 adapter->req_rx_add_entries_per_subcrq =
2795 adapter->max_rx_add_entries_per_subcrq;
2796
2797 if (adapter->desired.tx_queues)
2798 adapter->req_tx_queues =
2799 adapter->desired.tx_queues;
2800 else
2801 adapter->req_tx_queues =
2802 adapter->opt_tx_comp_sub_queues;
2803
2804 if (adapter->desired.rx_queues)
2805 adapter->req_rx_queues =
2806 adapter->desired.rx_queues;
2807 else
2808 adapter->req_rx_queues =
2809 adapter->opt_rx_comp_queues;
2810
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002811 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002812 }
2813
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814 memset(&crq, 0, sizeof(crq));
2815 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2816 crq.request_capability.cmd = REQUEST_CAPABILITY;
2817
2818 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002819 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002820 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821 ibmvnic_send_crq(adapter, &crq);
2822
2823 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002824 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002825 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826 ibmvnic_send_crq(adapter, &crq);
2827
2828 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002829 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002830 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002831 ibmvnic_send_crq(adapter, &crq);
2832
2833 crq.request_capability.capability =
2834 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2835 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002836 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002837 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002838 ibmvnic_send_crq(adapter, &crq);
2839
2840 crq.request_capability.capability =
2841 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2842 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002843 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002844 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845 ibmvnic_send_crq(adapter, &crq);
2846
2847 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002848 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002849 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002850 ibmvnic_send_crq(adapter, &crq);
2851
2852 if (adapter->netdev->flags & IFF_PROMISC) {
2853 if (adapter->promisc_supported) {
2854 crq.request_capability.capability =
2855 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002856 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002857 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002858 ibmvnic_send_crq(adapter, &crq);
2859 }
2860 } else {
2861 crq.request_capability.capability =
2862 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002863 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002864 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002865 ibmvnic_send_crq(adapter, &crq);
2866 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002867}
2868
2869static int pending_scrq(struct ibmvnic_adapter *adapter,
2870 struct ibmvnic_sub_crq_queue *scrq)
2871{
2872 union sub_crq *entry = &scrq->msgs[scrq->cur];
2873
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002874 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002875 return 1;
2876 else
2877 return 0;
2878}
2879
2880static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2881 struct ibmvnic_sub_crq_queue *scrq)
2882{
2883 union sub_crq *entry;
2884 unsigned long flags;
2885
2886 spin_lock_irqsave(&scrq->lock, flags);
2887 entry = &scrq->msgs[scrq->cur];
2888 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2889 if (++scrq->cur == scrq->size)
2890 scrq->cur = 0;
2891 } else {
2892 entry = NULL;
2893 }
2894 spin_unlock_irqrestore(&scrq->lock, flags);
2895
2896 return entry;
2897}
2898
2899static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2900{
2901 struct ibmvnic_crq_queue *queue = &adapter->crq;
2902 union ibmvnic_crq *crq;
2903
2904 crq = &queue->msgs[queue->cur];
2905 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2906 if (++queue->cur == queue->size)
2907 queue->cur = 0;
2908 } else {
2909 crq = NULL;
2910 }
2911
2912 return crq;
2913}
2914
2915static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2916 union sub_crq *sub_crq)
2917{
2918 unsigned int ua = adapter->vdev->unit_address;
2919 struct device *dev = &adapter->vdev->dev;
2920 u64 *u64_crq = (u64 *)sub_crq;
2921 int rc;
2922
2923 netdev_dbg(adapter->netdev,
2924 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2925 (unsigned long int)cpu_to_be64(remote_handle),
2926 (unsigned long int)cpu_to_be64(u64_crq[0]),
2927 (unsigned long int)cpu_to_be64(u64_crq[1]),
2928 (unsigned long int)cpu_to_be64(u64_crq[2]),
2929 (unsigned long int)cpu_to_be64(u64_crq[3]));
2930
2931 /* Make sure the hypervisor sees the complete request */
2932 mb();
2933
2934 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2935 cpu_to_be64(remote_handle),
2936 cpu_to_be64(u64_crq[0]),
2937 cpu_to_be64(u64_crq[1]),
2938 cpu_to_be64(u64_crq[2]),
2939 cpu_to_be64(u64_crq[3]));
2940
2941 if (rc) {
2942 if (rc == H_CLOSED)
2943 dev_warn(dev, "CRQ Queue closed\n");
2944 dev_err(dev, "Send error (rc=%d)\n", rc);
2945 }
2946
2947 return rc;
2948}
2949
Thomas Falconad7775d2016-04-01 17:20:34 -05002950static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2951 u64 remote_handle, u64 ioba, u64 num_entries)
2952{
2953 unsigned int ua = adapter->vdev->unit_address;
2954 struct device *dev = &adapter->vdev->dev;
2955 int rc;
2956
2957 /* Make sure the hypervisor sees the complete request */
2958 mb();
2959 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2960 cpu_to_be64(remote_handle),
2961 ioba, num_entries);
2962
2963 if (rc) {
2964 if (rc == H_CLOSED)
2965 dev_warn(dev, "CRQ Queue closed\n");
2966 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2967 }
2968
2969 return rc;
2970}
2971
Thomas Falcon032c5e82015-12-21 11:26:06 -06002972static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2973 union ibmvnic_crq *crq)
2974{
2975 unsigned int ua = adapter->vdev->unit_address;
2976 struct device *dev = &adapter->vdev->dev;
2977 u64 *u64_crq = (u64 *)crq;
2978 int rc;
2979
2980 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2981 (unsigned long int)cpu_to_be64(u64_crq[0]),
2982 (unsigned long int)cpu_to_be64(u64_crq[1]));
2983
2984 /* Make sure the hypervisor sees the complete request */
2985 mb();
2986
2987 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2988 cpu_to_be64(u64_crq[0]),
2989 cpu_to_be64(u64_crq[1]));
2990
2991 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002992 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002993 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002994 if (adapter->resetting)
2995 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2996 }
2997
Thomas Falcon032c5e82015-12-21 11:26:06 -06002998 dev_warn(dev, "Send error (rc=%d)\n", rc);
2999 }
3000
3001 return rc;
3002}
3003
3004static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3005{
3006 union ibmvnic_crq crq;
3007
3008 memset(&crq, 0, sizeof(crq));
3009 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3010 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3011 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3012
3013 return ibmvnic_send_crq(adapter, &crq);
3014}
3015
Thomas Falcon032c5e82015-12-21 11:26:06 -06003016static int send_version_xchg(struct ibmvnic_adapter *adapter)
3017{
3018 union ibmvnic_crq crq;
3019
3020 memset(&crq, 0, sizeof(crq));
3021 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3022 crq.version_exchange.cmd = VERSION_EXCHANGE;
3023 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3024
3025 return ibmvnic_send_crq(adapter, &crq);
3026}
3027
Nathan Fontenot37798d02017-11-08 11:23:56 -06003028struct vnic_login_client_data {
3029 u8 type;
3030 __be16 len;
3031 char name;
3032} __packed;
3033
3034static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3035{
3036 int len;
3037
3038 /* Calculate the amount of buffer space needed for the
3039 * vnic client data in the login buffer. There are four entries,
3040 * OS name, LPAR name, device name, and a null last entry.
3041 */
3042 len = 4 * sizeof(struct vnic_login_client_data);
3043 len += 6; /* "Linux" plus NULL */
3044 len += strlen(utsname()->nodename) + 1;
3045 len += strlen(adapter->netdev->name) + 1;
3046
3047 return len;
3048}
3049
3050static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3051 struct vnic_login_client_data *vlcd)
3052{
3053 const char *os_name = "Linux";
3054 int len;
3055
3056 /* Type 1 - LPAR OS */
3057 vlcd->type = 1;
3058 len = strlen(os_name) + 1;
3059 vlcd->len = cpu_to_be16(len);
3060 strncpy(&vlcd->name, os_name, len);
3061 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3062
3063 /* Type 2 - LPAR name */
3064 vlcd->type = 2;
3065 len = strlen(utsname()->nodename) + 1;
3066 vlcd->len = cpu_to_be16(len);
3067 strncpy(&vlcd->name, utsname()->nodename, len);
3068 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3069
3070 /* Type 3 - device name */
3071 vlcd->type = 3;
3072 len = strlen(adapter->netdev->name) + 1;
3073 vlcd->len = cpu_to_be16(len);
3074 strncpy(&vlcd->name, adapter->netdev->name, len);
3075}
3076
Thomas Falcon032c5e82015-12-21 11:26:06 -06003077static void send_login(struct ibmvnic_adapter *adapter)
3078{
3079 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3080 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003081 struct device *dev = &adapter->vdev->dev;
3082 dma_addr_t rsp_buffer_token;
3083 dma_addr_t buffer_token;
3084 size_t rsp_buffer_size;
3085 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003086 size_t buffer_size;
3087 __be64 *tx_list_p;
3088 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003089 int client_data_len;
3090 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003091 int i;
3092
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003093 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003094 client_data_len = vnic_client_data_len(adapter);
3095
Thomas Falcon032c5e82015-12-21 11:26:06 -06003096 buffer_size =
3097 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003098 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3099 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003100
Nathan Fontenot37798d02017-11-08 11:23:56 -06003101 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 if (!login_buffer)
3103 goto buf_alloc_failed;
3104
3105 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3106 DMA_TO_DEVICE);
3107 if (dma_mapping_error(dev, buffer_token)) {
3108 dev_err(dev, "Couldn't map login buffer\n");
3109 goto buf_map_failed;
3110 }
3111
John Allen498cd8e2016-04-06 11:49:55 -05003112 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3113 sizeof(u64) * adapter->req_tx_queues +
3114 sizeof(u64) * adapter->req_rx_queues +
3115 sizeof(u64) * adapter->req_rx_queues +
3116 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003117
3118 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3119 if (!login_rsp_buffer)
3120 goto buf_rsp_alloc_failed;
3121
3122 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3123 rsp_buffer_size, DMA_FROM_DEVICE);
3124 if (dma_mapping_error(dev, rsp_buffer_token)) {
3125 dev_err(dev, "Couldn't map login rsp buffer\n");
3126 goto buf_rsp_map_failed;
3127 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003128
Thomas Falcon032c5e82015-12-21 11:26:06 -06003129 adapter->login_buf = login_buffer;
3130 adapter->login_buf_token = buffer_token;
3131 adapter->login_buf_sz = buffer_size;
3132 adapter->login_rsp_buf = login_rsp_buffer;
3133 adapter->login_rsp_buf_token = rsp_buffer_token;
3134 adapter->login_rsp_buf_sz = rsp_buffer_size;
3135
3136 login_buffer->len = cpu_to_be32(buffer_size);
3137 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3138 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3139 login_buffer->off_txcomp_subcrqs =
3140 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3141 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3142 login_buffer->off_rxcomp_subcrqs =
3143 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3144 sizeof(u64) * adapter->req_tx_queues);
3145 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3146 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3147
3148 tx_list_p = (__be64 *)((char *)login_buffer +
3149 sizeof(struct ibmvnic_login_buffer));
3150 rx_list_p = (__be64 *)((char *)login_buffer +
3151 sizeof(struct ibmvnic_login_buffer) +
3152 sizeof(u64) * adapter->req_tx_queues);
3153
3154 for (i = 0; i < adapter->req_tx_queues; i++) {
3155 if (adapter->tx_scrq[i]) {
3156 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3157 crq_num);
3158 }
3159 }
3160
3161 for (i = 0; i < adapter->req_rx_queues; i++) {
3162 if (adapter->rx_scrq[i]) {
3163 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3164 crq_num);
3165 }
3166 }
3167
Nathan Fontenot37798d02017-11-08 11:23:56 -06003168 /* Insert vNIC login client data */
3169 vlcd = (struct vnic_login_client_data *)
3170 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3171 login_buffer->client_data_offset =
3172 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3173 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3174
3175 vnic_add_client_data(adapter, vlcd);
3176
Thomas Falcon032c5e82015-12-21 11:26:06 -06003177 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3178 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3179 netdev_dbg(adapter->netdev, "%016lx\n",
3180 ((unsigned long int *)(adapter->login_buf))[i]);
3181 }
3182
3183 memset(&crq, 0, sizeof(crq));
3184 crq.login.first = IBMVNIC_CRQ_CMD;
3185 crq.login.cmd = LOGIN;
3186 crq.login.ioba = cpu_to_be32(buffer_token);
3187 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003188 ibmvnic_send_crq(adapter, &crq);
3189
3190 return;
3191
Thomas Falcon032c5e82015-12-21 11:26:06 -06003192buf_rsp_map_failed:
3193 kfree(login_rsp_buffer);
3194buf_rsp_alloc_failed:
3195 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3196buf_map_failed:
3197 kfree(login_buffer);
3198buf_alloc_failed:
3199 return;
3200}
3201
3202static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3203 u32 len, u8 map_id)
3204{
3205 union ibmvnic_crq crq;
3206
3207 memset(&crq, 0, sizeof(crq));
3208 crq.request_map.first = IBMVNIC_CRQ_CMD;
3209 crq.request_map.cmd = REQUEST_MAP;
3210 crq.request_map.map_id = map_id;
3211 crq.request_map.ioba = cpu_to_be32(addr);
3212 crq.request_map.len = cpu_to_be32(len);
3213 ibmvnic_send_crq(adapter, &crq);
3214}
3215
3216static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3217{
3218 union ibmvnic_crq crq;
3219
3220 memset(&crq, 0, sizeof(crq));
3221 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3222 crq.request_unmap.cmd = REQUEST_UNMAP;
3223 crq.request_unmap.map_id = map_id;
3224 ibmvnic_send_crq(adapter, &crq);
3225}
3226
3227static void send_map_query(struct ibmvnic_adapter *adapter)
3228{
3229 union ibmvnic_crq crq;
3230
3231 memset(&crq, 0, sizeof(crq));
3232 crq.query_map.first = IBMVNIC_CRQ_CMD;
3233 crq.query_map.cmd = QUERY_MAP;
3234 ibmvnic_send_crq(adapter, &crq);
3235}
3236
3237/* Send a series of CRQs requesting various capabilities of the VNIC server */
3238static void send_cap_queries(struct ibmvnic_adapter *adapter)
3239{
3240 union ibmvnic_crq crq;
3241
Thomas Falcon901e0402017-02-15 12:17:59 -06003242 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003243 memset(&crq, 0, sizeof(crq));
3244 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3245 crq.query_capability.cmd = QUERY_CAPABILITY;
3246
3247 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003248 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003249 ibmvnic_send_crq(adapter, &crq);
3250
3251 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
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 = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003256 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003257 ibmvnic_send_crq(adapter, &crq);
3258
3259 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003260 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003261 ibmvnic_send_crq(adapter, &crq);
3262
3263 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003264 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003265 ibmvnic_send_crq(adapter, &crq);
3266
3267 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003268 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003269 ibmvnic_send_crq(adapter, &crq);
3270
3271 crq.query_capability.capability =
3272 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003273 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 ibmvnic_send_crq(adapter, &crq);
3275
3276 crq.query_capability.capability =
3277 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
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 =
3282 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003283 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003284 ibmvnic_send_crq(adapter, &crq);
3285
3286 crq.query_capability.capability =
3287 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003288 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003289 ibmvnic_send_crq(adapter, &crq);
3290
3291 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003292 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003293 ibmvnic_send_crq(adapter, &crq);
3294
3295 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003296 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 ibmvnic_send_crq(adapter, &crq);
3298
3299 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003300 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 ibmvnic_send_crq(adapter, &crq);
3302
3303 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003304 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003305 ibmvnic_send_crq(adapter, &crq);
3306
3307 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003308 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003309 ibmvnic_send_crq(adapter, &crq);
3310
3311 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003312 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 ibmvnic_send_crq(adapter, &crq);
3314
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003315 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3316 atomic_inc(&adapter->running_cap_crqs);
3317 ibmvnic_send_crq(adapter, &crq);
3318
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003320 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003321 ibmvnic_send_crq(adapter, &crq);
3322
3323 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003324 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003325 ibmvnic_send_crq(adapter, &crq);
3326
3327 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003328 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003329 ibmvnic_send_crq(adapter, &crq);
3330
3331 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003332 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333 ibmvnic_send_crq(adapter, &crq);
3334
3335 crq.query_capability.capability =
3336 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003337 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003338 ibmvnic_send_crq(adapter, &crq);
3339
3340 crq.query_capability.capability =
3341 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003342 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343 ibmvnic_send_crq(adapter, &crq);
3344
3345 crq.query_capability.capability =
3346 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003347 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348 ibmvnic_send_crq(adapter, &crq);
3349
3350 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003351 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352 ibmvnic_send_crq(adapter, &crq);
3353}
3354
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003355static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3356 struct ibmvnic_adapter *adapter)
3357{
3358 struct device *dev = &adapter->vdev->dev;
3359
3360 if (crq->get_vpd_size_rsp.rc.code) {
3361 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3362 crq->get_vpd_size_rsp.rc.code);
3363 complete(&adapter->fw_done);
3364 return;
3365 }
3366
3367 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3368 complete(&adapter->fw_done);
3369}
3370
3371static void handle_vpd_rsp(union ibmvnic_crq *crq,
3372 struct ibmvnic_adapter *adapter)
3373{
3374 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003375 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003376 u8 fw_level_len = 0;
3377
3378 memset(adapter->fw_version, 0, 32);
3379
3380 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3381 DMA_FROM_DEVICE);
3382
3383 if (crq->get_vpd_rsp.rc.code) {
3384 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3385 crq->get_vpd_rsp.rc.code);
3386 goto complete;
3387 }
3388
3389 /* get the position of the firmware version info
3390 * located after the ASCII 'RM' substring in the buffer
3391 */
3392 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3393 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003394 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 -02003395 goto complete;
3396 }
3397
3398 /* get length of firmware level ASCII substring */
3399 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3400 fw_level_len = *(substr + 2);
3401 } else {
3402 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3403 goto complete;
3404 }
3405
3406 /* copy firmware version string from vpd into adapter */
3407 if ((substr + 3 + fw_level_len) <
3408 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003409 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003410 } else {
3411 dev_info(dev, "FW substr extrapolated VPD buff\n");
3412 }
3413
3414complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003415 if (adapter->fw_version[0] == '\0')
3416 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003417 complete(&adapter->fw_done);
3418}
3419
Thomas Falcon032c5e82015-12-21 11:26:06 -06003420static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3421{
3422 struct device *dev = &adapter->vdev->dev;
3423 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3424 union ibmvnic_crq crq;
3425 int i;
3426
3427 dma_unmap_single(dev, adapter->ip_offload_tok,
3428 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3429
3430 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3431 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3432 netdev_dbg(adapter->netdev, "%016lx\n",
3433 ((unsigned long int *)(buf))[i]);
3434
3435 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3436 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3437 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3438 buf->tcp_ipv4_chksum);
3439 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3440 buf->tcp_ipv6_chksum);
3441 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3442 buf->udp_ipv4_chksum);
3443 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3444 buf->udp_ipv6_chksum);
3445 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3446 buf->large_tx_ipv4);
3447 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3448 buf->large_tx_ipv6);
3449 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3450 buf->large_rx_ipv4);
3451 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3452 buf->large_rx_ipv6);
3453 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3454 buf->max_ipv4_header_size);
3455 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3456 buf->max_ipv6_header_size);
3457 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3458 buf->max_tcp_header_size);
3459 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3460 buf->max_udp_header_size);
3461 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3462 buf->max_large_tx_size);
3463 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3464 buf->max_large_rx_size);
3465 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3466 buf->ipv6_extension_header);
3467 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3468 buf->tcp_pseudosum_req);
3469 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3470 buf->num_ipv6_ext_headers);
3471 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3472 buf->off_ipv6_ext_headers);
3473
3474 adapter->ip_offload_ctrl_tok =
3475 dma_map_single(dev, &adapter->ip_offload_ctrl,
3476 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3477
3478 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3479 dev_err(dev, "Couldn't map ip offload control buffer\n");
3480 return;
3481 }
3482
Thomas Falconf6897942018-01-18 19:05:01 -06003483 adapter->ip_offload_ctrl.len =
3484 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003485 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003486 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3487 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003488 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3489 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3490 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3491 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003492 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3493 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003494
Thomas Falconfdb06102017-10-17 12:36:55 -05003495 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003496 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3497 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3498
Thomas Falcon15482052017-10-17 12:36:54 -05003499 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003500
3501 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3502 adapter->netdev->features |= NETIF_F_IP_CSUM;
3503
3504 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3505 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3506
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003507 if ((adapter->netdev->features &
3508 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3509 adapter->netdev->features |= NETIF_F_RXCSUM;
3510
Thomas Falconfdb06102017-10-17 12:36:55 -05003511 if (buf->large_tx_ipv4)
3512 adapter->netdev->features |= NETIF_F_TSO;
3513 if (buf->large_tx_ipv6)
3514 adapter->netdev->features |= NETIF_F_TSO6;
3515
Thomas Falconaa0bf852017-10-17 12:36:56 -05003516 adapter->netdev->hw_features |= adapter->netdev->features;
3517
Thomas Falcon032c5e82015-12-21 11:26:06 -06003518 memset(&crq, 0, sizeof(crq));
3519 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3520 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3521 crq.control_ip_offload.len =
3522 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3523 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3524 ibmvnic_send_crq(adapter, &crq);
3525}
3526
3527static void handle_error_info_rsp(union ibmvnic_crq *crq,
3528 struct ibmvnic_adapter *adapter)
3529{
3530 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003531 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003532 unsigned long flags;
3533 bool found = false;
3534 int i;
3535
3536 if (!crq->request_error_rsp.rc.code) {
3537 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3538 crq->request_error_rsp.rc.code);
3539 return;
3540 }
3541
3542 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003543 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003544 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3545 found = true;
3546 list_del(&error_buff->list);
3547 break;
3548 }
3549 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3550
3551 if (!found) {
3552 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003553 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003554 return;
3555 }
3556
3557 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003558 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003559
3560 for (i = 0; i < error_buff->len; i++) {
3561 pr_cont("%02x", (int)error_buff->buff[i]);
3562 if (i % 8 == 7)
3563 pr_cont(" ");
3564 }
3565 pr_cont("\n");
3566
3567 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3568 DMA_FROM_DEVICE);
3569 kfree(error_buff->buff);
3570 kfree(error_buff);
3571}
3572
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003573static void request_error_information(struct ibmvnic_adapter *adapter,
3574 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003575{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003576 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003577 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003578 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003579 unsigned long timeout = msecs_to_jiffies(30000);
3580 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003581 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003582 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583
3584 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3585 if (!error_buff)
3586 return;
3587
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003588 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3590 if (!error_buff->buff) {
3591 kfree(error_buff);
3592 return;
3593 }
3594
3595 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3596 DMA_FROM_DEVICE);
3597 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003598 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599 kfree(error_buff->buff);
3600 kfree(error_buff);
3601 return;
3602 }
3603
Thomas Falcon032c5e82015-12-21 11:26:06 -06003604 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003605 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003606
3607 spin_lock_irqsave(&adapter->error_list_lock, flags);
3608 list_add_tail(&error_buff->list, &adapter->errors);
3609 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3610
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003611 memset(&crq, 0, sizeof(crq));
3612 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3613 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3614 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3615 crq.request_error_info.len = cpu_to_be32(detail_len);
3616 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3617
3618 rc = ibmvnic_send_crq(adapter, &crq);
3619 if (rc) {
3620 netdev_err(netdev, "failed to request error information\n");
3621 goto err_info_fail;
3622 }
3623
3624 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3625 netdev_err(netdev, "timeout waiting for error information\n");
3626 goto err_info_fail;
3627 }
3628
3629 return;
3630
3631err_info_fail:
3632 spin_lock_irqsave(&adapter->error_list_lock, flags);
3633 list_del(&error_buff->list);
3634 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3635
3636 kfree(error_buff->buff);
3637 kfree(error_buff);
3638}
3639
3640static void handle_error_indication(union ibmvnic_crq *crq,
3641 struct ibmvnic_adapter *adapter)
3642{
3643 struct device *dev = &adapter->vdev->dev;
3644
3645 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3646 crq->error_indication.flags
3647 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3648 be32_to_cpu(crq->error_indication.error_id),
3649 be16_to_cpu(crq->error_indication.error_cause));
3650
3651 if (be32_to_cpu(crq->error_indication.error_id))
3652 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003653
3654 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3655 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003656 else
3657 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003658}
3659
Thomas Falconf8136142018-01-29 13:45:05 -06003660static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3661 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003662{
3663 struct net_device *netdev = adapter->netdev;
3664 struct device *dev = &adapter->vdev->dev;
3665 long rc;
3666
3667 rc = crq->change_mac_addr_rsp.rc.code;
3668 if (rc) {
3669 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003670 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671 }
3672 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3673 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003674out:
3675 complete(&adapter->fw_done);
3676 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003677}
3678
3679static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3680 struct ibmvnic_adapter *adapter)
3681{
3682 struct device *dev = &adapter->vdev->dev;
3683 u64 *req_value;
3684 char *name;
3685
Thomas Falcon901e0402017-02-15 12:17:59 -06003686 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003687 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3688 case REQ_TX_QUEUES:
3689 req_value = &adapter->req_tx_queues;
3690 name = "tx";
3691 break;
3692 case REQ_RX_QUEUES:
3693 req_value = &adapter->req_rx_queues;
3694 name = "rx";
3695 break;
3696 case REQ_RX_ADD_QUEUES:
3697 req_value = &adapter->req_rx_add_queues;
3698 name = "rx_add";
3699 break;
3700 case REQ_TX_ENTRIES_PER_SUBCRQ:
3701 req_value = &adapter->req_tx_entries_per_subcrq;
3702 name = "tx_entries_per_subcrq";
3703 break;
3704 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3705 req_value = &adapter->req_rx_add_entries_per_subcrq;
3706 name = "rx_add_entries_per_subcrq";
3707 break;
3708 case REQ_MTU:
3709 req_value = &adapter->req_mtu;
3710 name = "mtu";
3711 break;
3712 case PROMISC_REQUESTED:
3713 req_value = &adapter->promisc;
3714 name = "promisc";
3715 break;
3716 default:
3717 dev_err(dev, "Got invalid cap request rsp %d\n",
3718 crq->request_capability.capability);
3719 return;
3720 }
3721
3722 switch (crq->request_capability_rsp.rc.code) {
3723 case SUCCESS:
3724 break;
3725 case PARTIALSUCCESS:
3726 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3727 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003728 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003729 number), name);
John Allene7913802018-01-18 16:27:12 -06003730
3731 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3732 REQ_MTU) {
3733 pr_err("mtu of %llu is not supported. Reverting.\n",
3734 *req_value);
3735 *req_value = adapter->fallback.mtu;
3736 } else {
3737 *req_value =
3738 be64_to_cpu(crq->request_capability_rsp.number);
3739 }
3740
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003741 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003742 return;
3743 default:
3744 dev_err(dev, "Error %d in request cap rsp\n",
3745 crq->request_capability_rsp.rc.code);
3746 return;
3747 }
3748
3749 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003750 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003751 union ibmvnic_crq newcrq;
3752 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3753 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3754 &adapter->ip_offload_buf;
3755
Thomas Falcon249168a2017-02-15 12:18:00 -06003756 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003757 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3758 buf_sz,
3759 DMA_FROM_DEVICE);
3760
3761 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3762 if (!firmware_has_feature(FW_FEATURE_CMO))
3763 dev_err(dev, "Couldn't map offload buffer\n");
3764 return;
3765 }
3766
3767 memset(&newcrq, 0, sizeof(newcrq));
3768 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3769 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3770 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3771 newcrq.query_ip_offload.ioba =
3772 cpu_to_be32(adapter->ip_offload_tok);
3773
3774 ibmvnic_send_crq(adapter, &newcrq);
3775 }
3776}
3777
3778static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3779 struct ibmvnic_adapter *adapter)
3780{
3781 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003782 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003783 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3784 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003785 int i;
3786
3787 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3788 DMA_BIDIRECTIONAL);
3789 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3790 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3791
John Allen498cd8e2016-04-06 11:49:55 -05003792 /* If the number of queues requested can't be allocated by the
3793 * server, the login response will return with code 1. We will need
3794 * to resend the login buffer with fewer queues requested.
3795 */
3796 if (login_rsp_crq->generic.rc.code) {
3797 adapter->renegotiate = true;
3798 complete(&adapter->init_done);
3799 return 0;
3800 }
3801
John Allenc26eba02017-10-26 16:23:25 -05003802 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3803
Thomas Falcon032c5e82015-12-21 11:26:06 -06003804 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3805 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3806 netdev_dbg(adapter->netdev, "%016lx\n",
3807 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3808 }
3809
3810 /* Sanity checks */
3811 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3812 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3813 adapter->req_rx_add_queues !=
3814 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3815 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3816 ibmvnic_remove(adapter->vdev);
3817 return -EIO;
3818 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003819 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003820 complete(&adapter->init_done);
3821
Thomas Falcon032c5e82015-12-21 11:26:06 -06003822 return 0;
3823}
3824
Thomas Falcon032c5e82015-12-21 11:26:06 -06003825static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3826 struct ibmvnic_adapter *adapter)
3827{
3828 struct device *dev = &adapter->vdev->dev;
3829 long rc;
3830
3831 rc = crq->request_unmap_rsp.rc.code;
3832 if (rc)
3833 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3834}
3835
3836static void handle_query_map_rsp(union ibmvnic_crq *crq,
3837 struct ibmvnic_adapter *adapter)
3838{
3839 struct net_device *netdev = adapter->netdev;
3840 struct device *dev = &adapter->vdev->dev;
3841 long rc;
3842
3843 rc = crq->query_map_rsp.rc.code;
3844 if (rc) {
3845 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3846 return;
3847 }
3848 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3849 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3850 crq->query_map_rsp.free_pages);
3851}
3852
3853static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3854 struct ibmvnic_adapter *adapter)
3855{
3856 struct net_device *netdev = adapter->netdev;
3857 struct device *dev = &adapter->vdev->dev;
3858 long rc;
3859
Thomas Falcon901e0402017-02-15 12:17:59 -06003860 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003861 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003862 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003863 rc = crq->query_capability.rc.code;
3864 if (rc) {
3865 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3866 goto out;
3867 }
3868
3869 switch (be16_to_cpu(crq->query_capability.capability)) {
3870 case MIN_TX_QUEUES:
3871 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003872 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003873 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3874 adapter->min_tx_queues);
3875 break;
3876 case MIN_RX_QUEUES:
3877 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003878 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003879 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3880 adapter->min_rx_queues);
3881 break;
3882 case MIN_RX_ADD_QUEUES:
3883 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003884 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003885 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3886 adapter->min_rx_add_queues);
3887 break;
3888 case MAX_TX_QUEUES:
3889 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003890 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003891 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3892 adapter->max_tx_queues);
3893 break;
3894 case MAX_RX_QUEUES:
3895 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003896 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3898 adapter->max_rx_queues);
3899 break;
3900 case MAX_RX_ADD_QUEUES:
3901 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003902 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003903 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3904 adapter->max_rx_add_queues);
3905 break;
3906 case MIN_TX_ENTRIES_PER_SUBCRQ:
3907 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003908 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3910 adapter->min_tx_entries_per_subcrq);
3911 break;
3912 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3913 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003914 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3916 adapter->min_rx_add_entries_per_subcrq);
3917 break;
3918 case MAX_TX_ENTRIES_PER_SUBCRQ:
3919 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003920 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3922 adapter->max_tx_entries_per_subcrq);
3923 break;
3924 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3925 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003926 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003927 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3928 adapter->max_rx_add_entries_per_subcrq);
3929 break;
3930 case TCP_IP_OFFLOAD:
3931 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003932 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003933 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3934 adapter->tcp_ip_offload);
3935 break;
3936 case PROMISC_SUPPORTED:
3937 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003938 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 netdev_dbg(netdev, "promisc_supported = %lld\n",
3940 adapter->promisc_supported);
3941 break;
3942 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003943 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003944 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003945 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3946 break;
3947 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003948 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003949 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003950 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3951 break;
3952 case MAX_MULTICAST_FILTERS:
3953 adapter->max_multicast_filters =
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, "max_multicast_filters = %lld\n",
3956 adapter->max_multicast_filters);
3957 break;
3958 case VLAN_HEADER_INSERTION:
3959 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003960 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003961 if (adapter->vlan_header_insertion)
3962 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3963 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3964 adapter->vlan_header_insertion);
3965 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003966 case RX_VLAN_HEADER_INSERTION:
3967 adapter->rx_vlan_header_insertion =
3968 be64_to_cpu(crq->query_capability.number);
3969 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3970 adapter->rx_vlan_header_insertion);
3971 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003972 case MAX_TX_SG_ENTRIES:
3973 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003974 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003975 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3976 adapter->max_tx_sg_entries);
3977 break;
3978 case RX_SG_SUPPORTED:
3979 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003980 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3982 adapter->rx_sg_supported);
3983 break;
3984 case OPT_TX_COMP_SUB_QUEUES:
3985 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003986 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003987 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3988 adapter->opt_tx_comp_sub_queues);
3989 break;
3990 case OPT_RX_COMP_QUEUES:
3991 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003992 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003993 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3994 adapter->opt_rx_comp_queues);
3995 break;
3996 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3997 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003998 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003999 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4000 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4001 break;
4002 case OPT_TX_ENTRIES_PER_SUBCRQ:
4003 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004004 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004005 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4006 adapter->opt_tx_entries_per_subcrq);
4007 break;
4008 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4009 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004010 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004011 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4012 adapter->opt_rxba_entries_per_subcrq);
4013 break;
4014 case TX_RX_DESC_REQ:
4015 adapter->tx_rx_desc_req = crq->query_capability.number;
4016 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4017 adapter->tx_rx_desc_req);
4018 break;
4019
4020 default:
4021 netdev_err(netdev, "Got invalid cap rsp %d\n",
4022 crq->query_capability.capability);
4023 }
4024
4025out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004026 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4027 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004028 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004029 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004030}
4031
Thomas Falcon032c5e82015-12-21 11:26:06 -06004032static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4033 struct ibmvnic_adapter *adapter)
4034{
4035 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4036 struct net_device *netdev = adapter->netdev;
4037 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004038 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004039 long rc;
4040
4041 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004042 (unsigned long int)cpu_to_be64(u64_crq[0]),
4043 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004044 switch (gen_crq->first) {
4045 case IBMVNIC_CRQ_INIT_RSP:
4046 switch (gen_crq->cmd) {
4047 case IBMVNIC_CRQ_INIT:
4048 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004049 adapter->from_passive_init = true;
4050 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004051 break;
4052 case IBMVNIC_CRQ_INIT_COMPLETE:
4053 dev_info(dev, "Partner initialization complete\n");
4054 send_version_xchg(adapter);
4055 break;
4056 default:
4057 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4058 }
4059 return;
4060 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004061 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004062 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004063 dev_info(dev, "Migrated, re-enabling adapter\n");
4064 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004065 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4066 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004067 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068 } else {
4069 /* The adapter lost the connection */
4070 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4071 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004072 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004073 }
4074 return;
4075 case IBMVNIC_CRQ_CMD_RSP:
4076 break;
4077 default:
4078 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4079 gen_crq->first);
4080 return;
4081 }
4082
4083 switch (gen_crq->cmd) {
4084 case VERSION_EXCHANGE_RSP:
4085 rc = crq->version_exchange_rsp.rc.code;
4086 if (rc) {
4087 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4088 break;
4089 }
4090 dev_info(dev, "Partner protocol version is %d\n",
4091 crq->version_exchange_rsp.version);
4092 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4093 ibmvnic_version)
4094 ibmvnic_version =
4095 be16_to_cpu(crq->version_exchange_rsp.version);
4096 send_cap_queries(adapter);
4097 break;
4098 case QUERY_CAPABILITY_RSP:
4099 handle_query_cap_rsp(crq, adapter);
4100 break;
4101 case QUERY_MAP_RSP:
4102 handle_query_map_rsp(crq, adapter);
4103 break;
4104 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004105 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4106 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004107 break;
4108 case REQUEST_UNMAP_RSP:
4109 handle_request_unmap_rsp(crq, adapter);
4110 break;
4111 case REQUEST_CAPABILITY_RSP:
4112 handle_request_cap_rsp(crq, adapter);
4113 break;
4114 case LOGIN_RSP:
4115 netdev_dbg(netdev, "Got Login Response\n");
4116 handle_login_rsp(crq, adapter);
4117 break;
4118 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004119 netdev_dbg(netdev,
4120 "Got Logical Link State Response, state: %d rc: %d\n",
4121 crq->logical_link_state_rsp.link_state,
4122 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004123 adapter->logical_link_state =
4124 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004125 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4126 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004127 break;
4128 case LINK_STATE_INDICATION:
4129 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4130 adapter->phys_link_state =
4131 crq->link_state_indication.phys_link_state;
4132 adapter->logical_link_state =
4133 crq->link_state_indication.logical_link_state;
4134 break;
4135 case CHANGE_MAC_ADDR_RSP:
4136 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004137 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004138 break;
4139 case ERROR_INDICATION:
4140 netdev_dbg(netdev, "Got Error Indication\n");
4141 handle_error_indication(crq, adapter);
4142 break;
4143 case REQUEST_ERROR_RSP:
4144 netdev_dbg(netdev, "Got Error Detail Response\n");
4145 handle_error_info_rsp(crq, adapter);
4146 break;
4147 case REQUEST_STATISTICS_RSP:
4148 netdev_dbg(netdev, "Got Statistics Response\n");
4149 complete(&adapter->stats_done);
4150 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004151 case QUERY_IP_OFFLOAD_RSP:
4152 netdev_dbg(netdev, "Got Query IP offload Response\n");
4153 handle_query_ip_offload_rsp(adapter);
4154 break;
4155 case MULTICAST_CTRL_RSP:
4156 netdev_dbg(netdev, "Got multicast control Response\n");
4157 break;
4158 case CONTROL_IP_OFFLOAD_RSP:
4159 netdev_dbg(netdev, "Got Control IP offload Response\n");
4160 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4161 sizeof(adapter->ip_offload_ctrl),
4162 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004163 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004164 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004165 case COLLECT_FW_TRACE_RSP:
4166 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4167 complete(&adapter->fw_done);
4168 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004169 case GET_VPD_SIZE_RSP:
4170 handle_vpd_size_rsp(crq, adapter);
4171 break;
4172 case GET_VPD_RSP:
4173 handle_vpd_rsp(crq, adapter);
4174 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175 default:
4176 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4177 gen_crq->cmd);
4178 }
4179}
4180
4181static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4182{
4183 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004184
Thomas Falcon6c267b32017-02-15 12:17:58 -06004185 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004186 return IRQ_HANDLED;
4187}
4188
4189static void ibmvnic_tasklet(void *data)
4190{
4191 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004192 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004193 union ibmvnic_crq *crq;
4194 unsigned long flags;
4195 bool done = false;
4196
4197 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004198 while (!done) {
4199 /* Pull all the valid messages off the CRQ */
4200 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4201 ibmvnic_handle_crq(crq, adapter);
4202 crq->generic.first = 0;
4203 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004204
4205 /* remain in tasklet until all
4206 * capabilities responses are received
4207 */
4208 if (!adapter->wait_capability)
4209 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004210 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004211 /* if capabilities CRQ's were sent in this tasklet, the following
4212 * tasklet must wait until all responses are received
4213 */
4214 if (atomic_read(&adapter->running_cap_crqs) != 0)
4215 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004216 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004217}
4218
4219static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4220{
4221 struct vio_dev *vdev = adapter->vdev;
4222 int rc;
4223
4224 do {
4225 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4226 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4227
4228 if (rc)
4229 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4230
4231 return rc;
4232}
4233
4234static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4235{
4236 struct ibmvnic_crq_queue *crq = &adapter->crq;
4237 struct device *dev = &adapter->vdev->dev;
4238 struct vio_dev *vdev = adapter->vdev;
4239 int rc;
4240
4241 /* Close the CRQ */
4242 do {
4243 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4244 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4245
4246 /* Clean out the queue */
4247 memset(crq->msgs, 0, PAGE_SIZE);
4248 crq->cur = 0;
4249
4250 /* And re-open it again */
4251 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4252 crq->msg_token, PAGE_SIZE);
4253
4254 if (rc == H_CLOSED)
4255 /* Adapter is good, but other end is not ready */
4256 dev_warn(dev, "Partner adapter not ready\n");
4257 else if (rc != 0)
4258 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4259
4260 return rc;
4261}
4262
Nathan Fontenotf9928872017-03-30 02:48:54 -04004263static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004264{
4265 struct ibmvnic_crq_queue *crq = &adapter->crq;
4266 struct vio_dev *vdev = adapter->vdev;
4267 long rc;
4268
Nathan Fontenotf9928872017-03-30 02:48:54 -04004269 if (!crq->msgs)
4270 return;
4271
Thomas Falcon032c5e82015-12-21 11:26:06 -06004272 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4273 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004274 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004275 do {
4276 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4277 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4278
4279 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4280 DMA_BIDIRECTIONAL);
4281 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004282 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004283}
4284
Nathan Fontenotf9928872017-03-30 02:48:54 -04004285static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004286{
4287 struct ibmvnic_crq_queue *crq = &adapter->crq;
4288 struct device *dev = &adapter->vdev->dev;
4289 struct vio_dev *vdev = adapter->vdev;
4290 int rc, retrc = -ENOMEM;
4291
Nathan Fontenotf9928872017-03-30 02:48:54 -04004292 if (crq->msgs)
4293 return 0;
4294
Thomas Falcon032c5e82015-12-21 11:26:06 -06004295 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4296 /* Should we allocate more than one page? */
4297
4298 if (!crq->msgs)
4299 return -ENOMEM;
4300
4301 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4302 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4303 DMA_BIDIRECTIONAL);
4304 if (dma_mapping_error(dev, crq->msg_token))
4305 goto map_failed;
4306
4307 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4308 crq->msg_token, PAGE_SIZE);
4309
4310 if (rc == H_RESOURCE)
4311 /* maybe kexecing and resource is busy. try a reset */
4312 rc = ibmvnic_reset_crq(adapter);
4313 retrc = rc;
4314
4315 if (rc == H_CLOSED) {
4316 dev_warn(dev, "Partner adapter not ready\n");
4317 } else if (rc) {
4318 dev_warn(dev, "Error %d opening adapter\n", rc);
4319 goto reg_crq_failed;
4320 }
4321
4322 retrc = 0;
4323
Thomas Falcon6c267b32017-02-15 12:17:58 -06004324 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4325 (unsigned long)adapter);
4326
Thomas Falcon032c5e82015-12-21 11:26:06 -06004327 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4328 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4329 adapter);
4330 if (rc) {
4331 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4332 vdev->irq, rc);
4333 goto req_irq_failed;
4334 }
4335
4336 rc = vio_enable_interrupts(vdev);
4337 if (rc) {
4338 dev_err(dev, "Error %d enabling interrupts\n", rc);
4339 goto req_irq_failed;
4340 }
4341
4342 crq->cur = 0;
4343 spin_lock_init(&crq->lock);
4344
4345 return retrc;
4346
4347req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004348 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004349 do {
4350 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4351 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4352reg_crq_failed:
4353 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4354map_failed:
4355 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004356 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004357 return retrc;
4358}
4359
John Allenf6ef6402017-03-17 17:13:42 -05004360static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4361{
4362 struct device *dev = &adapter->vdev->dev;
4363 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004364 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004365 int rc;
4366
John Allenc26eba02017-10-26 16:23:25 -05004367 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004368 rc = ibmvnic_reset_crq(adapter);
4369 if (!rc)
4370 rc = vio_enable_interrupts(adapter->vdev);
4371 } else {
4372 rc = init_crq_queue(adapter);
4373 }
4374
John Allenf6ef6402017-03-17 17:13:42 -05004375 if (rc) {
4376 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4377 return rc;
4378 }
4379
John Allen017892c12017-05-26 10:30:19 -04004380 adapter->from_passive_init = false;
4381
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004382 old_num_rx_queues = adapter->req_rx_queues;
4383 old_num_tx_queues = adapter->req_tx_queues;
4384
John Allenf6ef6402017-03-17 17:13:42 -05004385 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004386 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004387 ibmvnic_send_crq_init(adapter);
4388 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4389 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004390 return -1;
4391 }
4392
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004393 if (adapter->init_done_rc) {
4394 release_crq_queue(adapter);
4395 return adapter->init_done_rc;
4396 }
4397
John Allen017892c12017-05-26 10:30:19 -04004398 if (adapter->from_passive_init) {
4399 adapter->state = VNIC_OPEN;
4400 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004401 return -1;
4402 }
4403
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004404 if (adapter->resetting && !adapter->wait_for_reset) {
4405 if (adapter->req_rx_queues != old_num_rx_queues ||
4406 adapter->req_tx_queues != old_num_tx_queues) {
4407 release_sub_crqs(adapter, 0);
4408 rc = init_sub_crqs(adapter);
4409 } else {
4410 rc = reset_sub_crq_queues(adapter);
4411 }
4412 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004413 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004414 }
4415
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004416 if (rc) {
4417 dev_err(dev, "Initialization of sub crqs failed\n");
4418 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004419 return rc;
4420 }
4421
4422 rc = init_sub_crq_irqs(adapter);
4423 if (rc) {
4424 dev_err(dev, "Failed to initialize sub crq irqs\n");
4425 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004426 }
4427
Thomas Falcon53cc7722018-02-26 18:10:56 -06004428 rc = init_stats_buffers(adapter);
4429 if (rc)
4430 return rc;
4431
4432 rc = init_stats_token(adapter);
4433 if (rc)
4434 return rc;
4435
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004436 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004437}
4438
Thomas Falcon40c9db82017-06-12 12:35:04 -05004439static struct device_attribute dev_attr_failover;
4440
Thomas Falcon032c5e82015-12-21 11:26:06 -06004441static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4442{
4443 struct ibmvnic_adapter *adapter;
4444 struct net_device *netdev;
4445 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004446 int rc;
4447
4448 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4449 dev->unit_address);
4450
4451 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4452 VETH_MAC_ADDR, NULL);
4453 if (!mac_addr_p) {
4454 dev_err(&dev->dev,
4455 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4456 __FILE__, __LINE__);
4457 return 0;
4458 }
4459
4460 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004461 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004462 if (!netdev)
4463 return -ENOMEM;
4464
4465 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004466 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004467 dev_set_drvdata(&dev->dev, netdev);
4468 adapter->vdev = dev;
4469 adapter->netdev = netdev;
4470
4471 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4472 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4473 netdev->irq = dev->irq;
4474 netdev->netdev_ops = &ibmvnic_netdev_ops;
4475 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4476 SET_NETDEV_DEV(netdev, &dev->dev);
4477
4478 spin_lock_init(&adapter->stats_lock);
4479
Thomas Falcon032c5e82015-12-21 11:26:06 -06004480 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004481 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004482
Nathan Fontenoted651a12017-05-03 14:04:38 -04004483 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4484 INIT_LIST_HEAD(&adapter->rwi_list);
4485 mutex_init(&adapter->reset_lock);
4486 mutex_init(&adapter->rwi_lock);
4487 adapter->resetting = false;
4488
John Allenc26eba02017-10-26 16:23:25 -05004489 adapter->mac_change_pending = false;
4490
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004491 do {
4492 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004493 if (rc && rc != EAGAIN)
4494 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004495 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004496
Thomas Falconf39f0d12017-02-14 10:22:59 -06004497 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004498 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4499 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004500
Thomas Falcon40c9db82017-06-12 12:35:04 -05004501 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004502 if (rc)
4503 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004504
Mick Tarsele876a8a2017-09-28 13:53:18 -07004505 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004506 rc = register_netdev(netdev);
4507 if (rc) {
4508 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004509 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004510 }
4511 dev_info(&dev->dev, "ibmvnic registered\n");
4512
Nathan Fontenot90c80142017-05-03 14:04:32 -04004513 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004514
4515 adapter->wait_for_reset = false;
4516
Thomas Falcon032c5e82015-12-21 11:26:06 -06004517 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004518
4519ibmvnic_register_fail:
4520 device_remove_file(&dev->dev, &dev_attr_failover);
4521
4522ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004523 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004524 release_crq_queue(adapter);
4525 free_netdev(netdev);
4526
4527 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004528}
4529
4530static int ibmvnic_remove(struct vio_dev *dev)
4531{
4532 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004533 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004534
Nathan Fontenot90c80142017-05-03 14:04:32 -04004535 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004536 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004537 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004538
4539 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004540 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004541 release_crq_queue(adapter);
4542
Thomas Falcon53cc7722018-02-26 18:10:56 -06004543 release_stats_token(adapter);
4544 release_stats_buffers(adapter);
4545
Nathan Fontenot90c80142017-05-03 14:04:32 -04004546 adapter->state = VNIC_REMOVED;
4547
Nathan Fontenoted651a12017-05-03 14:04:38 -04004548 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004549 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004550 free_netdev(netdev);
4551 dev_set_drvdata(&dev->dev, NULL);
4552
4553 return 0;
4554}
4555
Thomas Falcon40c9db82017-06-12 12:35:04 -05004556static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4557 const char *buf, size_t count)
4558{
4559 struct net_device *netdev = dev_get_drvdata(dev);
4560 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4561 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4562 __be64 session_token;
4563 long rc;
4564
4565 if (!sysfs_streq(buf, "1"))
4566 return -EINVAL;
4567
4568 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4569 H_GET_SESSION_TOKEN, 0, 0, 0);
4570 if (rc) {
4571 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4572 rc);
4573 return -EINVAL;
4574 }
4575
4576 session_token = (__be64)retbuf[0];
4577 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4578 be64_to_cpu(session_token));
4579 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4580 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4581 if (rc) {
4582 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4583 rc);
4584 return -EINVAL;
4585 }
4586
4587 return count;
4588}
4589
Joe Perches6cbaefb2017-12-19 10:15:09 -08004590static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004591
Thomas Falcon032c5e82015-12-21 11:26:06 -06004592static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4593{
4594 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4595 struct ibmvnic_adapter *adapter;
4596 struct iommu_table *tbl;
4597 unsigned long ret = 0;
4598 int i;
4599
4600 tbl = get_iommu_table_base(&vdev->dev);
4601
4602 /* netdev inits at probe time along with the structures we need below*/
4603 if (!netdev)
4604 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4605
4606 adapter = netdev_priv(netdev);
4607
4608 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004609 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4610
4611 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4612 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4613
4614 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4615 i++)
4616 ret += adapter->rx_pool[i].size *
4617 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4618
4619 return ret;
4620}
4621
4622static int ibmvnic_resume(struct device *dev)
4623{
4624 struct net_device *netdev = dev_get_drvdata(dev);
4625 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004626
John Allencb89ba22017-06-19 11:27:53 -05004627 if (adapter->state != VNIC_OPEN)
4628 return 0;
4629
John Allena2488782017-07-24 13:26:06 -05004630 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004631
4632 return 0;
4633}
4634
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304635static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004636 {"network", "IBM,vnic"},
4637 {"", "" }
4638};
4639MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4640
4641static const struct dev_pm_ops ibmvnic_pm_ops = {
4642 .resume = ibmvnic_resume
4643};
4644
4645static struct vio_driver ibmvnic_driver = {
4646 .id_table = ibmvnic_device_table,
4647 .probe = ibmvnic_probe,
4648 .remove = ibmvnic_remove,
4649 .get_desired_dma = ibmvnic_get_desired_dma,
4650 .name = ibmvnic_driver_name,
4651 .pm = &ibmvnic_pm_ops,
4652};
4653
4654/* module functions */
4655static int __init ibmvnic_module_init(void)
4656{
4657 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4658 IBMVNIC_DRIVER_VERSION);
4659
4660 return vio_register_driver(&ibmvnic_driver);
4661}
4662
4663static void __exit ibmvnic_module_exit(void)
4664{
4665 vio_unregister_driver(&ibmvnic_driver);
4666}
4667
4668module_init(ibmvnic_module_init);
4669module_exit(ibmvnic_module_exit);