blob: 5497a8b44287fa6af971d7a87d8fd2a2e2bb71aa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Richter6812baa2017-01-09 16:55:15 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Generic netlink support functions to configure an SMC-R PNET table
6 *
7 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com>
10 */
11
12#include <linux/module.h>
13#include <linux/list.h>
14#include <linux/ctype.h>
15#include <net/netlink.h>
16#include <net/genetlink.h>
17
18#include <uapi/linux/if.h>
19#include <uapi/linux/smc.h>
20
21#include <rdma/ib_verbs.h>
22
23#include "smc_pnet.h"
24#include "smc_ib.h"
Hans Wippel1619f772018-06-28 19:05:08 +020025#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010026
Hans Wippel890a2cb2019-02-21 13:01:00 +010027#define SMC_ASCII_BLANK 32
28
29static struct net_device *pnet_find_base_ndev(struct net_device *ndev);
30
Thomas Richter6812baa2017-01-09 16:55:15 +010031static struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
32 [SMC_PNETID_NAME] = {
33 .type = NLA_NUL_STRING,
Hans Wippelca8dc132019-01-30 18:51:01 +010034 .len = SMC_MAX_PNETID_LEN
Thomas Richter6812baa2017-01-09 16:55:15 +010035 },
36 [SMC_PNETID_ETHNAME] = {
37 .type = NLA_NUL_STRING,
38 .len = IFNAMSIZ - 1
39 },
40 [SMC_PNETID_IBNAME] = {
41 .type = NLA_NUL_STRING,
42 .len = IB_DEVICE_NAME_MAX - 1
43 },
44 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
45};
46
47static struct genl_family smc_pnet_nl_family;
48
49/**
50 * struct smc_pnettable - SMC PNET table anchor
51 * @lock: Lock for list action
52 * @pnetlist: List of PNETIDs
53 */
54static struct smc_pnettable {
55 rwlock_t lock;
56 struct list_head pnetlist;
57} smc_pnettable = {
58 .pnetlist = LIST_HEAD_INIT(smc_pnettable.pnetlist),
59 .lock = __RW_LOCK_UNLOCKED(smc_pnettable.lock)
60};
61
62/**
Hans Wippel890a2cb2019-02-21 13:01:00 +010063 * struct smc_user_pnetentry - pnet identifier name entry for/from user
Thomas Richter6812baa2017-01-09 16:55:15 +010064 * @list: List node.
65 * @pnet_name: Pnet identifier name
66 * @ndev: pointer to network device.
67 * @smcibdev: Pointer to IB device.
Hans Wippel890a2cb2019-02-21 13:01:00 +010068 * @ib_port: Port of IB device.
Hans Wippelf3d74b22019-02-21 13:01:01 +010069 * @smcd_dev: Pointer to smcd device.
Thomas Richter6812baa2017-01-09 16:55:15 +010070 */
Hans Wippel890a2cb2019-02-21 13:01:00 +010071struct smc_user_pnetentry {
Thomas Richter6812baa2017-01-09 16:55:15 +010072 struct list_head list;
Ursula Braun0afff912018-06-28 19:05:05 +020073 char pnet_name[SMC_MAX_PNETID_LEN + 1];
Thomas Richter6812baa2017-01-09 16:55:15 +010074 struct net_device *ndev;
75 struct smc_ib_device *smcibdev;
76 u8 ib_port;
Hans Wippelf3d74b22019-02-21 13:01:01 +010077 struct smcd_dev *smcd_dev;
Thomas Richter6812baa2017-01-09 16:55:15 +010078};
79
Hans Wippel890a2cb2019-02-21 13:01:00 +010080/* pnet entry stored in pnet table */
81struct smc_pnetentry {
82 struct list_head list;
83 char pnet_name[SMC_MAX_PNETID_LEN + 1];
84 struct net_device *ndev;
85};
Thomas Richter6812baa2017-01-09 16:55:15 +010086
Hans Wippel890a2cb2019-02-21 13:01:00 +010087/* Check if two given pnetids match */
88static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
Thomas Richter6812baa2017-01-09 16:55:15 +010089{
Hans Wippel890a2cb2019-02-21 13:01:00 +010090 int i;
Thomas Richter6812baa2017-01-09 16:55:15 +010091
Hans Wippel890a2cb2019-02-21 13:01:00 +010092 for (i = 0; i < SMC_MAX_PNETID_LEN; i++) {
93 if ((pnetid1[i] == 0 || pnetid1[i] == SMC_ASCII_BLANK) &&
94 (pnetid2[i] == 0 || pnetid2[i] == SMC_ASCII_BLANK))
Thomas Richter6812baa2017-01-09 16:55:15 +010095 break;
Hans Wippel890a2cb2019-02-21 13:01:00 +010096 if (pnetid1[i] != pnetid2[i])
97 return false;
Thomas Richter6812baa2017-01-09 16:55:15 +010098 }
Hans Wippel890a2cb2019-02-21 13:01:00 +010099 return true;
Thomas Richter6812baa2017-01-09 16:55:15 +0100100}
101
102/* Remove a pnetid from the pnet table.
103 */
104static int smc_pnet_remove_by_pnetid(char *pnet_name)
105{
106 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100107 struct smc_ib_device *ibdev;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100108 struct smcd_dev *smcd_dev;
Thomas Richter6812baa2017-01-09 16:55:15 +0100109 int rc = -ENOENT;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100110 int ibport;
Thomas Richter6812baa2017-01-09 16:55:15 +0100111
Hans Wippel890a2cb2019-02-21 13:01:00 +0100112 /* remove netdevices */
Thomas Richter6812baa2017-01-09 16:55:15 +0100113 write_lock(&smc_pnettable.lock);
114 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
115 list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100116 if (!pnet_name ||
117 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100118 list_del(&pnetelem->list);
119 dev_put(pnetelem->ndev);
120 kfree(pnetelem);
121 rc = 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100122 }
123 }
124 write_unlock(&smc_pnettable.lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100125 /* remove ib devices */
126 spin_lock(&smc_ib_devices.lock);
127 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
128 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
129 if (ibdev->pnetid_by_user[ibport] &&
130 (!pnet_name ||
131 smc_pnet_match(pnet_name,
132 ibdev->pnetid[ibport]))) {
133 memset(ibdev->pnetid[ibport], 0,
134 SMC_MAX_PNETID_LEN);
135 ibdev->pnetid_by_user[ibport] = false;
136 rc = 0;
137 }
138 }
139 }
140 spin_unlock(&smc_ib_devices.lock);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100141 /* remove smcd devices */
142 spin_lock(&smcd_dev_list.lock);
143 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
144 if (smcd_dev->pnetid_by_user &&
145 (!pnet_name ||
146 smc_pnet_match(pnet_name, smcd_dev->pnetid))) {
147 memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN);
148 smcd_dev->pnetid_by_user = false;
149 rc = 0;
150 }
151 }
152 spin_unlock(&smcd_dev_list.lock);
Thomas Richter6812baa2017-01-09 16:55:15 +0100153 return rc;
154}
155
156/* Remove a pnet entry mentioning a given network device from the pnet table.
157 */
158static int smc_pnet_remove_by_ndev(struct net_device *ndev)
159{
160 struct smc_pnetentry *pnetelem, *tmp_pe;
161 int rc = -ENOENT;
162
163 write_lock(&smc_pnettable.lock);
164 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
165 list) {
166 if (pnetelem->ndev == ndev) {
167 list_del(&pnetelem->list);
168 dev_put(pnetelem->ndev);
169 kfree(pnetelem);
170 rc = 0;
171 break;
172 }
173 }
174 write_unlock(&smc_pnettable.lock);
175 return rc;
176}
177
Thomas Richter6812baa2017-01-09 16:55:15 +0100178/* Append a pnetid to the end of the pnet table if not already on this list.
179 */
Hans Wippel890a2cb2019-02-21 13:01:00 +0100180static int smc_pnet_enter(struct smc_user_pnetentry *new_pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100181{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100182 u8 pnet_null[SMC_MAX_PNETID_LEN] = {0};
183 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
184 struct smc_pnetentry *tmp_pnetelem;
Thomas Richter6812baa2017-01-09 16:55:15 +0100185 struct smc_pnetentry *pnetelem;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100186 bool new_smcddev = false;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100187 struct net_device *ndev;
188 bool new_netdev = true;
189 bool new_ibdev = false;
190
191 if (new_pnetelem->smcibdev) {
192 struct smc_ib_device *ib_dev = new_pnetelem->smcibdev;
193 int ib_port = new_pnetelem->ib_port;
194
195 spin_lock(&smc_ib_devices.lock);
196 if (smc_pnet_match(ib_dev->pnetid[ib_port - 1], pnet_null)) {
197 memcpy(ib_dev->pnetid[ib_port - 1],
198 new_pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
199 ib_dev->pnetid_by_user[ib_port - 1] = true;
200 new_ibdev = true;
201 }
202 spin_unlock(&smc_ib_devices.lock);
203 }
Hans Wippelf3d74b22019-02-21 13:01:01 +0100204 if (new_pnetelem->smcd_dev) {
205 struct smcd_dev *smcd_dev = new_pnetelem->smcd_dev;
206
207 spin_lock(&smcd_dev_list.lock);
208 if (smc_pnet_match(smcd_dev->pnetid, pnet_null)) {
209 memcpy(smcd_dev->pnetid, new_pnetelem->pnet_name,
210 SMC_MAX_PNETID_LEN);
211 smcd_dev->pnetid_by_user = true;
212 new_smcddev = true;
213 }
214 spin_unlock(&smcd_dev_list.lock);
215 }
Hans Wippel890a2cb2019-02-21 13:01:00 +0100216
217 if (!new_pnetelem->ndev)
Hans Wippelf3d74b22019-02-21 13:01:01 +0100218 return (new_ibdev || new_smcddev) ? 0 : -EEXIST;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100219
220 /* check if (base) netdev already has a pnetid. If there is one, we do
221 * not want to add a pnet table entry
222 */
223 ndev = pnet_find_base_ndev(new_pnetelem->ndev);
224 if (!smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
225 ndev_pnetid))
Hans Wippelf3d74b22019-02-21 13:01:01 +0100226 return (new_ibdev || new_smcddev) ? 0 : -EEXIST;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100227
228 /* add a new netdev entry to the pnet table if there isn't one */
229 tmp_pnetelem = kzalloc(sizeof(*pnetelem), GFP_KERNEL);
230 if (!tmp_pnetelem)
231 return -ENOMEM;
232 memcpy(tmp_pnetelem->pnet_name, new_pnetelem->pnet_name,
233 SMC_MAX_PNETID_LEN);
234 tmp_pnetelem->ndev = new_pnetelem->ndev;
Thomas Richter6812baa2017-01-09 16:55:15 +0100235
236 write_lock(&smc_pnettable.lock);
237 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100238 if (pnetelem->ndev == new_pnetelem->ndev)
239 new_netdev = false;
Thomas Richter6812baa2017-01-09 16:55:15 +0100240 }
Hans Wippel890a2cb2019-02-21 13:01:00 +0100241 if (new_netdev) {
242 dev_hold(tmp_pnetelem->ndev);
243 list_add_tail(&tmp_pnetelem->list, &smc_pnettable.pnetlist);
244 write_unlock(&smc_pnettable.lock);
245 } else {
246 write_unlock(&smc_pnettable.lock);
247 kfree(tmp_pnetelem);
248 }
249
Hans Wippelf3d74b22019-02-21 13:01:01 +0100250 return (new_netdev || new_ibdev || new_smcddev) ? 0 : -EEXIST;
Thomas Richter6812baa2017-01-09 16:55:15 +0100251}
252
253/* The limit for pnetid is 16 characters.
254 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
255 * Lower case letters are converted to upper case.
256 * Interior blanks should not be used.
257 */
258static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
259{
260 char *bf = skip_spaces(pnet_name);
261 size_t len = strlen(bf);
262 char *end = bf + len;
263
264 if (!len)
265 return false;
266 while (--end >= bf && isspace(*end))
267 ;
Ursula Braun0afff912018-06-28 19:05:05 +0200268 if (end - bf >= SMC_MAX_PNETID_LEN)
Thomas Richter6812baa2017-01-09 16:55:15 +0100269 return false;
270 while (bf <= end) {
271 if (!isalnum(*bf))
272 return false;
273 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
274 bf++;
275 }
276 *pnetid = '\0';
277 return true;
278}
279
280/* Find an infiniband device by a given name. The device might not exist. */
Ursula Braun249633a2017-04-10 14:57:57 +0200281static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100282{
283 struct smc_ib_device *ibdev;
284
285 spin_lock(&smc_ib_devices.lock);
286 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
287 if (!strncmp(ibdev->ibdev->name, ib_name,
288 sizeof(ibdev->ibdev->name))) {
289 goto out;
290 }
291 }
292 ibdev = NULL;
293out:
294 spin_unlock(&smc_ib_devices.lock);
295 return ibdev;
296}
297
Hans Wippelf3d74b22019-02-21 13:01:01 +0100298/* Find an smcd device by a given name. The device might not exist. */
299static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name)
300{
301 struct smcd_dev *smcd_dev;
302
303 spin_lock(&smcd_dev_list.lock);
304 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
305 if (!strncmp(dev_name(&smcd_dev->dev), smcd_name,
306 IB_DEVICE_NAME_MAX - 1))
307 goto out;
308 }
309 smcd_dev = NULL;
310out:
311 spin_unlock(&smcd_dev_list.lock);
312 return smcd_dev;
313}
314
Thomas Richter6812baa2017-01-09 16:55:15 +0100315/* Parse the supplied netlink attributes and fill a pnetentry structure.
316 * For ethernet and infiniband device names verify that the devices exist.
317 */
Hans Wippel890a2cb2019-02-21 13:01:00 +0100318static int smc_pnet_fill_entry(struct net *net,
319 struct smc_user_pnetentry *pnetelem,
Thomas Richter6812baa2017-01-09 16:55:15 +0100320 struct nlattr *tb[])
321{
Eric Biggersd49baa72018-05-13 17:01:30 -0700322 char *string, *ibname;
323 int rc;
Thomas Richter6812baa2017-01-09 16:55:15 +0100324
325 memset(pnetelem, 0, sizeof(*pnetelem));
326 INIT_LIST_HEAD(&pnetelem->list);
Eric Biggersd49baa72018-05-13 17:01:30 -0700327
328 rc = -EINVAL;
329 if (!tb[SMC_PNETID_NAME])
330 goto error;
331 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
332 if (!smc_pnetid_valid(string, pnetelem->pnet_name))
333 goto error;
334
335 rc = -EINVAL;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100336 if (tb[SMC_PNETID_ETHNAME]) {
337 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
338 pnetelem->ndev = dev_get_by_name(net, string);
339 if (!pnetelem->ndev)
340 goto error;
341 }
Eric Biggersd49baa72018-05-13 17:01:30 -0700342
343 rc = -EINVAL;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100344 if (tb[SMC_PNETID_IBNAME]) {
345 ibname = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
346 ibname = strim(ibname);
347 pnetelem->smcibdev = smc_pnet_find_ib(ibname);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100348 pnetelem->smcd_dev = smc_pnet_find_smcd(ibname);
349 if (!pnetelem->smcibdev && !pnetelem->smcd_dev)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100350 goto error;
351 if (pnetelem->smcibdev) {
352 if (!tb[SMC_PNETID_IBPORT])
353 goto error;
354 pnetelem->ib_port = nla_get_u8(tb[SMC_PNETID_IBPORT]);
355 if (pnetelem->ib_port < 1 ||
356 pnetelem->ib_port > SMC_MAX_PORTS)
357 goto error;
358 }
359 }
Eric Biggersd49baa72018-05-13 17:01:30 -0700360
Thomas Richter6812baa2017-01-09 16:55:15 +0100361 return 0;
362
363error:
364 if (pnetelem->ndev)
365 dev_put(pnetelem->ndev);
366 return rc;
367}
368
369/* Convert an smc_pnetentry to a netlink attribute sequence */
Hans Wippel890a2cb2019-02-21 13:01:00 +0100370static int smc_pnet_set_nla(struct sk_buff *msg,
371 struct smc_user_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100372{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100373 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
Thomas Richter6812baa2017-01-09 16:55:15 +0100374 return -1;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100375 if (pnetelem->ndev) {
376 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
377 pnetelem->ndev->name))
378 return -1;
379 } else {
380 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
381 return -1;
382 }
383 if (pnetelem->smcibdev) {
384 if (nla_put_string(msg, SMC_PNETID_IBNAME,
385 pnetelem->smcibdev->ibdev->name) ||
386 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
387 return -1;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100388 } else if (pnetelem->smcd_dev) {
389 if (nla_put_string(msg, SMC_PNETID_IBNAME,
390 dev_name(&pnetelem->smcd_dev->dev)) ||
391 nla_put_u8(msg, SMC_PNETID_IBPORT, 1))
392 return -1;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100393 } else {
394 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
395 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
396 return -1;
397 }
398
Thomas Richter6812baa2017-01-09 16:55:15 +0100399 return 0;
400}
401
Thomas Richter6812baa2017-01-09 16:55:15 +0100402static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
403{
404 struct net *net = genl_info_net(info);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100405 struct smc_user_pnetentry pnetelem;
Thomas Richter6812baa2017-01-09 16:55:15 +0100406 int rc;
407
Hans Wippel890a2cb2019-02-21 13:01:00 +0100408 rc = smc_pnet_fill_entry(net, &pnetelem, info->attrs);
Thomas Richter6812baa2017-01-09 16:55:15 +0100409 if (!rc)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100410 rc = smc_pnet_enter(&pnetelem);
411 if (pnetelem.ndev)
412 dev_put(pnetelem.ndev);
Thomas Richter6812baa2017-01-09 16:55:15 +0100413 return rc;
414}
415
416static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
417{
Eric Biggersd49baa72018-05-13 17:01:30 -0700418 if (!info->attrs[SMC_PNETID_NAME])
419 return -EINVAL;
Thomas Richter6812baa2017-01-09 16:55:15 +0100420 return smc_pnet_remove_by_pnetid(
421 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
422}
423
424static int smc_pnet_dump_start(struct netlink_callback *cb)
425{
426 cb->args[0] = 0;
427 return 0;
428}
429
430static int smc_pnet_dumpinfo(struct sk_buff *skb,
431 u32 portid, u32 seq, u32 flags,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100432 struct smc_user_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100433{
434 void *hdr;
435
436 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
437 flags, SMC_PNETID_GET);
438 if (!hdr)
439 return -ENOMEM;
440 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
441 genlmsg_cancel(skb, hdr);
442 return -EMSGSIZE;
443 }
444 genlmsg_end(skb, hdr);
445 return 0;
446}
447
Hans Wippel890a2cb2019-02-21 13:01:00 +0100448static int _smc_pnet_dump(struct sk_buff *skb, u32 portid, u32 seq, u8 *pnetid,
449 int start_idx)
Thomas Richter6812baa2017-01-09 16:55:15 +0100450{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100451 struct smc_user_pnetentry tmp_entry;
Thomas Richter6812baa2017-01-09 16:55:15 +0100452 struct smc_pnetentry *pnetelem;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100453 struct smc_ib_device *ibdev;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100454 struct smcd_dev *smcd_dev;
Thomas Richter6812baa2017-01-09 16:55:15 +0100455 int idx = 0;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100456 int ibport;
Thomas Richter6812baa2017-01-09 16:55:15 +0100457
Hans Wippel890a2cb2019-02-21 13:01:00 +0100458 /* dump netdevices */
Thomas Richter6812baa2017-01-09 16:55:15 +0100459 read_lock(&smc_pnettable.lock);
460 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100461 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
Thomas Richter6812baa2017-01-09 16:55:15 +0100462 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100463 if (idx++ < start_idx)
464 continue;
465 memset(&tmp_entry, 0, sizeof(tmp_entry));
466 memcpy(&tmp_entry.pnet_name, pnetelem->pnet_name,
467 SMC_MAX_PNETID_LEN);
468 tmp_entry.ndev = pnetelem->ndev;
469 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
470 &tmp_entry)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100471 --idx;
472 break;
473 }
474 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100475 read_unlock(&smc_pnettable.lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100476
477 /* dump ib devices */
478 spin_lock(&smc_ib_devices.lock);
479 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
480 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
481 if (ibdev->pnetid_by_user[ibport]) {
482 if (pnetid &&
483 !smc_pnet_match(ibdev->pnetid[ibport],
484 pnetid))
485 continue;
486 if (idx++ < start_idx)
487 continue;
488 memset(&tmp_entry, 0, sizeof(tmp_entry));
489 memcpy(&tmp_entry.pnet_name,
490 ibdev->pnetid[ibport],
491 SMC_MAX_PNETID_LEN);
492 tmp_entry.smcibdev = ibdev;
493 tmp_entry.ib_port = ibport + 1;
494 if (smc_pnet_dumpinfo(skb, portid, seq,
495 NLM_F_MULTI,
496 &tmp_entry)) {
497 --idx;
498 break;
499 }
500 }
501 }
502 }
503 spin_unlock(&smc_ib_devices.lock);
504
Hans Wippelf3d74b22019-02-21 13:01:01 +0100505 /* dump smcd devices */
506 spin_lock(&smcd_dev_list.lock);
507 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
508 if (smcd_dev->pnetid_by_user) {
509 if (pnetid && !smc_pnet_match(smcd_dev->pnetid, pnetid))
510 continue;
511 if (idx++ < start_idx)
512 continue;
513 memset(&tmp_entry, 0, sizeof(tmp_entry));
514 memcpy(&tmp_entry.pnet_name, smcd_dev->pnetid,
515 SMC_MAX_PNETID_LEN);
516 tmp_entry.smcd_dev = smcd_dev;
517 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
518 &tmp_entry)) {
519 --idx;
520 break;
521 }
522 }
523 }
524 spin_unlock(&smcd_dev_list.lock);
525
Hans Wippel890a2cb2019-02-21 13:01:00 +0100526 return idx;
527}
528
529static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
530{
531 int idx;
532
533 idx = _smc_pnet_dump(skb, NETLINK_CB(cb->skb).portid,
534 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
535
536 cb->args[0] = idx;
Thomas Richter6812baa2017-01-09 16:55:15 +0100537 return skb->len;
538}
539
Hans Wippel890a2cb2019-02-21 13:01:00 +0100540/* Retrieve one PNETID entry */
541static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
542{
543 struct sk_buff *msg;
544 void *hdr;
545
546 if (!info->attrs[SMC_PNETID_NAME])
547 return -EINVAL;
548
549 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
550 if (!msg)
551 return -ENOMEM;
552
553 _smc_pnet_dump(msg, info->snd_portid, info->snd_seq,
554 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
555
556 /* finish multi part message and send it */
557 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
558 NLM_F_MULTI);
559 if (!hdr) {
560 nlmsg_free(msg);
561 return -EMSGSIZE;
562 }
563 return genlmsg_reply(msg, info);
564}
565
Thomas Richter6812baa2017-01-09 16:55:15 +0100566/* Remove and delete all pnetids from pnet table.
567 */
568static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
569{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100570 return smc_pnet_remove_by_pnetid(NULL);
Thomas Richter6812baa2017-01-09 16:55:15 +0100571}
572
573/* SMC_PNETID generic netlink operation definition */
574static const struct genl_ops smc_pnet_ops[] = {
575 {
576 .cmd = SMC_PNETID_GET,
577 .flags = GENL_ADMIN_PERM,
578 .policy = smc_pnet_policy,
579 .doit = smc_pnet_get,
580 .dumpit = smc_pnet_dump,
581 .start = smc_pnet_dump_start
582 },
583 {
584 .cmd = SMC_PNETID_ADD,
585 .flags = GENL_ADMIN_PERM,
586 .policy = smc_pnet_policy,
587 .doit = smc_pnet_add
588 },
589 {
590 .cmd = SMC_PNETID_DEL,
591 .flags = GENL_ADMIN_PERM,
592 .policy = smc_pnet_policy,
593 .doit = smc_pnet_del
594 },
595 {
596 .cmd = SMC_PNETID_FLUSH,
597 .flags = GENL_ADMIN_PERM,
598 .policy = smc_pnet_policy,
599 .doit = smc_pnet_flush
600 }
601};
602
603/* SMC_PNETID family definition */
Johannes Berg56ce3c52018-09-20 09:27:30 +0200604static struct genl_family smc_pnet_nl_family __ro_after_init = {
Thomas Richter6812baa2017-01-09 16:55:15 +0100605 .hdrsize = 0,
606 .name = SMCR_GENL_FAMILY_NAME,
607 .version = SMCR_GENL_FAMILY_VERSION,
608 .maxattr = SMC_PNETID_MAX,
609 .netnsok = true,
610 .module = THIS_MODULE,
611 .ops = smc_pnet_ops,
612 .n_ops = ARRAY_SIZE(smc_pnet_ops)
613};
614
615static int smc_pnet_netdev_event(struct notifier_block *this,
616 unsigned long event, void *ptr)
617{
618 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
619
620 switch (event) {
621 case NETDEV_REBOOT:
622 case NETDEV_UNREGISTER:
623 smc_pnet_remove_by_ndev(event_dev);
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200624 return NOTIFY_OK;
Thomas Richter6812baa2017-01-09 16:55:15 +0100625 default:
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200626 return NOTIFY_DONE;
Thomas Richter6812baa2017-01-09 16:55:15 +0100627 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100628}
629
630static struct notifier_block smc_netdev_notifier = {
631 .notifier_call = smc_pnet_netdev_event
632};
633
634int __init smc_pnet_init(void)
635{
636 int rc;
637
638 rc = genl_register_family(&smc_pnet_nl_family);
639 if (rc)
640 return rc;
641 rc = register_netdevice_notifier(&smc_netdev_notifier);
642 if (rc)
643 genl_unregister_family(&smc_pnet_nl_family);
644 return rc;
645}
646
647void smc_pnet_exit(void)
648{
649 smc_pnet_flush(NULL, NULL);
650 unregister_netdevice_notifier(&smc_netdev_notifier);
651 genl_unregister_family(&smc_pnet_nl_family);
652}
653
Ursula Braun0afff912018-06-28 19:05:05 +0200654/* Determine one base device for stacked net devices.
655 * If the lower device level contains more than one devices
656 * (for instance with bonding slaves), just the first device
657 * is used to reach a base device.
Thomas Richter6812baa2017-01-09 16:55:15 +0100658 */
Ursula Braun0afff912018-06-28 19:05:05 +0200659static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
Thomas Richter6812baa2017-01-09 16:55:15 +0100660{
Ursula Braun0afff912018-06-28 19:05:05 +0200661 int i, nest_lvl;
662
663 rtnl_lock();
664 nest_lvl = dev_get_nest_level(ndev);
665 for (i = 0; i < nest_lvl; i++) {
666 struct list_head *lower = &ndev->adj_list.lower;
667
668 if (list_empty(lower))
669 break;
670 lower = lower->next;
671 ndev = netdev_lower_get_next(ndev, &lower);
672 }
673 rtnl_unlock();
674 return ndev;
675}
676
Hans Wippel890a2cb2019-02-21 13:01:00 +0100677static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *netdev,
678 u8 *pnetid)
679{
680 struct smc_pnetentry *pnetelem;
681 int rc = -ENOENT;
682
683 read_lock(&smc_pnettable.lock);
684 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
685 if (netdev == pnetelem->ndev) {
686 /* get pnetid of netdev device */
687 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
688 rc = 0;
689 break;
690 }
691 }
692 read_unlock(&smc_pnettable.lock);
693 return rc;
694}
695
Ursula Braun0afff912018-06-28 19:05:05 +0200696/* Determine the corresponding IB device port based on the hardware PNETID.
Ursula Braun7005ada2018-07-25 16:35:31 +0200697 * Searching stops at the first matching active IB device port with vlan_id
698 * configured.
Ursula Braun0afff912018-06-28 19:05:05 +0200699 */
700static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
701 struct smc_ib_device **smcibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200702 u8 *ibport, unsigned short vlan_id,
703 u8 gid[])
Ursula Braun0afff912018-06-28 19:05:05 +0200704{
705 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
706 struct smc_ib_device *ibdev;
707 int i;
708
709 ndev = pnet_find_base_ndev(ndev);
710 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100711 ndev_pnetid) &&
712 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
Ursula Braun0afff912018-06-28 19:05:05 +0200713 return; /* pnetid could not be determined */
714
715 spin_lock(&smc_ib_devices.lock);
716 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
717 for (i = 1; i <= SMC_MAX_PORTS; i++) {
Ursula Braun7005ada2018-07-25 16:35:31 +0200718 if (!rdma_is_port_valid(ibdev->ibdev, i))
719 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100720 if (smc_pnet_match(ibdev->pnetid[i - 1], ndev_pnetid) &&
Ursula Braun7005ada2018-07-25 16:35:31 +0200721 smc_ib_port_active(ibdev, i) &&
722 !smc_ib_determine_gid(ibdev, i, vlan_id, gid,
723 NULL)) {
Ursula Braun0afff912018-06-28 19:05:05 +0200724 *smcibdev = ibdev;
725 *ibport = i;
Ursula Braun7005ada2018-07-25 16:35:31 +0200726 goto out;
Ursula Braun0afff912018-06-28 19:05:05 +0200727 }
728 }
729 }
Ursula Braun7005ada2018-07-25 16:35:31 +0200730out:
Ursula Braun0afff912018-06-28 19:05:05 +0200731 spin_unlock(&smc_ib_devices.lock);
732}
733
Hans Wippel1619f772018-06-28 19:05:08 +0200734static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
735 struct smcd_dev **smcismdev)
736{
737 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
738 struct smcd_dev *ismdev;
739
740 ndev = pnet_find_base_ndev(ndev);
741 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippelf3d74b22019-02-21 13:01:01 +0100742 ndev_pnetid) &&
743 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
Hans Wippel1619f772018-06-28 19:05:08 +0200744 return; /* pnetid could not be determined */
745
746 spin_lock(&smcd_dev_list.lock);
747 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
Hans Wippelf3d74b22019-02-21 13:01:01 +0100748 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid)) {
Hans Wippel1619f772018-06-28 19:05:08 +0200749 *smcismdev = ismdev;
750 break;
751 }
752 }
753 spin_unlock(&smcd_dev_list.lock);
754}
755
Ursula Braun0afff912018-06-28 19:05:05 +0200756/* PNET table analysis for a given sock:
757 * determine ib_device and port belonging to used internal TCP socket
758 * ethernet interface.
759 */
760void smc_pnet_find_roce_resource(struct sock *sk,
Ursula Braun7005ada2018-07-25 16:35:31 +0200761 struct smc_ib_device **smcibdev, u8 *ibport,
762 unsigned short vlan_id, u8 gid[])
Ursula Braun0afff912018-06-28 19:05:05 +0200763{
764 struct dst_entry *dst = sk_dst_get(sk);
765
766 *smcibdev = NULL;
767 *ibport = 0;
768
769 if (!dst)
770 goto out;
771 if (!dst->dev)
772 goto out_rel;
773
Ursula Braun7005ada2018-07-25 16:35:31 +0200774 smc_pnet_find_roce_by_pnetid(dst->dev, smcibdev, ibport, vlan_id, gid);
Ursula Braun0afff912018-06-28 19:05:05 +0200775
Thomas Richter6812baa2017-01-09 16:55:15 +0100776out_rel:
777 dst_release(dst);
Ursula Braun0afff912018-06-28 19:05:05 +0200778out:
779 return;
Thomas Richter6812baa2017-01-09 16:55:15 +0100780}
Hans Wippel1619f772018-06-28 19:05:08 +0200781
782void smc_pnet_find_ism_resource(struct sock *sk, struct smcd_dev **smcismdev)
783{
784 struct dst_entry *dst = sk_dst_get(sk);
785
786 *smcismdev = NULL;
787 if (!dst)
788 goto out;
789 if (!dst->dev)
790 goto out_rel;
791
Hans Wippel1619f772018-06-28 19:05:08 +0200792 smc_pnet_find_ism_by_pnetid(dst->dev, smcismdev);
793
794out_rel:
795 dst_release(dst);
796out:
797 return;
798}