ipoib_main.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include "ipoib.h"
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/kernel.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  41. #include <linux/ip.h>
  42. #include <linux/in.h>
  43. #include <linux/jhash.h>
  44. #include <net/arp.h>
  45. #include <net/addrconf.h>
  46. #include <linux/inetdevice.h>
  47. #include <rdma/ib_cache.h>
  48. #define DRV_VERSION "1.0.0"
  49. const char ipoib_driver_version[] = DRV_VERSION;
  50. MODULE_AUTHOR("Roland Dreier");
  51. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. MODULE_VERSION(DRV_VERSION);
  54. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  55. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  56. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  57. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  58. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  59. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  60. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  61. int ipoib_debug_level;
  62. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  63. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  64. #endif
  65. struct ipoib_path_iter {
  66. struct net_device *dev;
  67. struct ipoib_path path;
  68. };
  69. static const u8 ipv4_bcast_addr[] = {
  70. 0x00, 0xff, 0xff, 0xff,
  71. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  73. };
  74. struct workqueue_struct *ipoib_workqueue;
  75. struct ib_sa_client ipoib_sa_client;
  76. static void ipoib_add_one(struct ib_device *device);
  77. static void ipoib_remove_one(struct ib_device *device, void *client_data);
  78. static void ipoib_neigh_reclaim(struct rcu_head *rp);
  79. static struct net_device *ipoib_get_net_dev_by_params(
  80. struct ib_device *dev, u8 port, u16 pkey,
  81. const union ib_gid *gid, const struct sockaddr *addr,
  82. void *client_data);
  83. static struct ib_client ipoib_client = {
  84. .name = "ipoib",
  85. .add = ipoib_add_one,
  86. .remove = ipoib_remove_one,
  87. .get_net_dev_by_params = ipoib_get_net_dev_by_params,
  88. };
  89. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  90. static int ipoib_netdev_event(struct notifier_block *this,
  91. unsigned long event, void *ptr)
  92. {
  93. struct netdev_notifier_info *ni = ptr;
  94. struct net_device *dev = ni->dev;
  95. if (dev->netdev_ops->ndo_open != ipoib_open)
  96. return NOTIFY_DONE;
  97. switch (event) {
  98. case NETDEV_REGISTER:
  99. ipoib_create_debug_files(dev);
  100. break;
  101. case NETDEV_CHANGENAME:
  102. ipoib_delete_debug_files(dev);
  103. ipoib_create_debug_files(dev);
  104. break;
  105. case NETDEV_UNREGISTER:
  106. ipoib_delete_debug_files(dev);
  107. break;
  108. }
  109. return NOTIFY_DONE;
  110. }
  111. #endif
  112. int ipoib_open(struct net_device *dev)
  113. {
  114. struct ipoib_dev_priv *priv = netdev_priv(dev);
  115. ipoib_dbg(priv, "bringing up interface\n");
  116. netif_carrier_off(dev);
  117. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  118. if (ipoib_ib_dev_open(dev)) {
  119. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  120. return 0;
  121. goto err_disable;
  122. }
  123. if (ipoib_ib_dev_up(dev))
  124. goto err_stop;
  125. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  126. struct ipoib_dev_priv *cpriv;
  127. /* Bring up any child interfaces too */
  128. down_read(&priv->vlan_rwsem);
  129. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  130. int flags;
  131. flags = cpriv->dev->flags;
  132. if (flags & IFF_UP)
  133. continue;
  134. dev_change_flags(cpriv->dev, flags | IFF_UP);
  135. }
  136. up_read(&priv->vlan_rwsem);
  137. }
  138. netif_start_queue(dev);
  139. return 0;
  140. err_stop:
  141. ipoib_ib_dev_stop(dev);
  142. err_disable:
  143. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  144. return -EINVAL;
  145. }
  146. static int ipoib_stop(struct net_device *dev)
  147. {
  148. struct ipoib_dev_priv *priv = netdev_priv(dev);
  149. ipoib_dbg(priv, "stopping interface\n");
  150. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  151. netif_stop_queue(dev);
  152. ipoib_ib_dev_down(dev);
  153. ipoib_ib_dev_stop(dev);
  154. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  155. struct ipoib_dev_priv *cpriv;
  156. /* Bring down any child interfaces too */
  157. down_read(&priv->vlan_rwsem);
  158. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  159. int flags;
  160. flags = cpriv->dev->flags;
  161. if (!(flags & IFF_UP))
  162. continue;
  163. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  164. }
  165. up_read(&priv->vlan_rwsem);
  166. }
  167. return 0;
  168. }
  169. static void ipoib_uninit(struct net_device *dev)
  170. {
  171. ipoib_dev_cleanup(dev);
  172. }
  173. static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
  174. {
  175. struct ipoib_dev_priv *priv = netdev_priv(dev);
  176. if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
  177. features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  178. return features;
  179. }
  180. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  181. {
  182. struct ipoib_dev_priv *priv = netdev_priv(dev);
  183. /* dev->mtu > 2K ==> connected mode */
  184. if (ipoib_cm_admin_enabled(dev)) {
  185. if (new_mtu > ipoib_cm_max_mtu(dev))
  186. return -EINVAL;
  187. if (new_mtu > priv->mcast_mtu)
  188. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  189. priv->mcast_mtu);
  190. dev->mtu = new_mtu;
  191. return 0;
  192. }
  193. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  194. return -EINVAL;
  195. priv->admin_mtu = new_mtu;
  196. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  197. return 0;
  198. }
  199. /* Called with an RCU read lock taken */
  200. static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
  201. struct net_device *dev)
  202. {
  203. struct net *net = dev_net(dev);
  204. struct in_device *in_dev;
  205. struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
  206. struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
  207. __be32 ret_addr;
  208. switch (addr->sa_family) {
  209. case AF_INET:
  210. in_dev = in_dev_get(dev);
  211. if (!in_dev)
  212. return false;
  213. ret_addr = inet_confirm_addr(net, in_dev, 0,
  214. addr_in->sin_addr.s_addr,
  215. RT_SCOPE_HOST);
  216. in_dev_put(in_dev);
  217. if (ret_addr)
  218. return true;
  219. break;
  220. case AF_INET6:
  221. if (IS_ENABLED(CONFIG_IPV6) &&
  222. ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
  223. return true;
  224. break;
  225. }
  226. return false;
  227. }
  228. /**
  229. * Find the master net_device on top of the given net_device.
  230. * @dev: base IPoIB net_device
  231. *
  232. * Returns the master net_device with a reference held, or the same net_device
  233. * if no master exists.
  234. */
  235. static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
  236. {
  237. struct net_device *master;
  238. rcu_read_lock();
  239. master = netdev_master_upper_dev_get_rcu(dev);
  240. if (master)
  241. dev_hold(master);
  242. rcu_read_unlock();
  243. if (master)
  244. return master;
  245. dev_hold(dev);
  246. return dev;
  247. }
  248. /**
  249. * Find a net_device matching the given address, which is an upper device of
  250. * the given net_device.
  251. * @addr: IP address to look for.
  252. * @dev: base IPoIB net_device
  253. *
  254. * If found, returns the net_device with a reference held. Otherwise return
  255. * NULL.
  256. */
  257. static struct net_device *ipoib_get_net_dev_match_addr(
  258. const struct sockaddr *addr, struct net_device *dev)
  259. {
  260. struct net_device *upper,
  261. *result = NULL;
  262. struct list_head *iter;
  263. rcu_read_lock();
  264. if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
  265. dev_hold(dev);
  266. result = dev;
  267. goto out;
  268. }
  269. netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
  270. if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
  271. dev_hold(upper);
  272. result = upper;
  273. break;
  274. }
  275. }
  276. out:
  277. rcu_read_unlock();
  278. return result;
  279. }
  280. /* returns the number of IPoIB netdevs on top a given ipoib device matching a
  281. * pkey_index and address, if one exists.
  282. *
  283. * @found_net_dev: contains a matching net_device if the return value >= 1,
  284. * with a reference held. */
  285. static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
  286. const union ib_gid *gid,
  287. u16 pkey_index,
  288. const struct sockaddr *addr,
  289. int nesting,
  290. struct net_device **found_net_dev)
  291. {
  292. struct ipoib_dev_priv *child_priv;
  293. struct net_device *net_dev = NULL;
  294. int matches = 0;
  295. if (priv->pkey_index == pkey_index &&
  296. (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
  297. if (!addr) {
  298. net_dev = ipoib_get_master_net_dev(priv->dev);
  299. } else {
  300. /* Verify the net_device matches the IP address, as
  301. * IPoIB child devices currently share a GID. */
  302. net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
  303. }
  304. if (net_dev) {
  305. if (!*found_net_dev)
  306. *found_net_dev = net_dev;
  307. else
  308. dev_put(net_dev);
  309. ++matches;
  310. }
  311. }
  312. /* Check child interfaces */
  313. down_read_nested(&priv->vlan_rwsem, nesting);
  314. list_for_each_entry(child_priv, &priv->child_intfs, list) {
  315. matches += ipoib_match_gid_pkey_addr(child_priv, gid,
  316. pkey_index, addr,
  317. nesting + 1,
  318. found_net_dev);
  319. if (matches > 1)
  320. break;
  321. }
  322. up_read(&priv->vlan_rwsem);
  323. return matches;
  324. }
  325. /* Returns the number of matching net_devs found (between 0 and 2). Also
  326. * return the matching net_device in the @net_dev parameter, holding a
  327. * reference to the net_device, if the number of matches >= 1 */
  328. static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
  329. u16 pkey_index,
  330. const union ib_gid *gid,
  331. const struct sockaddr *addr,
  332. struct net_device **net_dev)
  333. {
  334. struct ipoib_dev_priv *priv;
  335. int matches = 0;
  336. *net_dev = NULL;
  337. list_for_each_entry(priv, dev_list, list) {
  338. if (priv->port != port)
  339. continue;
  340. matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
  341. addr, 0, net_dev);
  342. if (matches > 1)
  343. break;
  344. }
  345. return matches;
  346. }
  347. static struct net_device *ipoib_get_net_dev_by_params(
  348. struct ib_device *dev, u8 port, u16 pkey,
  349. const union ib_gid *gid, const struct sockaddr *addr,
  350. void *client_data)
  351. {
  352. struct net_device *net_dev;
  353. struct list_head *dev_list = client_data;
  354. u16 pkey_index;
  355. int matches;
  356. int ret;
  357. if (!rdma_protocol_ib(dev, port))
  358. return NULL;
  359. ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
  360. if (ret)
  361. return NULL;
  362. if (!dev_list)
  363. return NULL;
  364. /* See if we can find a unique device matching the L2 parameters */
  365. matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
  366. gid, NULL, &net_dev);
  367. switch (matches) {
  368. case 0:
  369. return NULL;
  370. case 1:
  371. return net_dev;
  372. }
  373. dev_put(net_dev);
  374. /* Couldn't find a unique device with L2 parameters only. Use L3
  375. * address to uniquely match the net device */
  376. matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
  377. gid, addr, &net_dev);
  378. switch (matches) {
  379. case 0:
  380. return NULL;
  381. default:
  382. dev_warn_ratelimited(&dev->dev,
  383. "duplicate IP address detected\n");
  384. /* Fall through */
  385. case 1:
  386. return net_dev;
  387. }
  388. }
  389. int ipoib_set_mode(struct net_device *dev, const char *buf)
  390. {
  391. struct ipoib_dev_priv *priv = netdev_priv(dev);
  392. /* flush paths if we switch modes so that connections are restarted */
  393. if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
  394. set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  395. ipoib_warn(priv, "enabling connected mode "
  396. "will cause multicast packet drops\n");
  397. netdev_update_features(dev);
  398. dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
  399. rtnl_unlock();
  400. priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
  401. ipoib_flush_paths(dev);
  402. return (!rtnl_trylock()) ? -EBUSY : 0;
  403. }
  404. if (!strcmp(buf, "datagram\n")) {
  405. clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  406. netdev_update_features(dev);
  407. dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
  408. rtnl_unlock();
  409. ipoib_flush_paths(dev);
  410. return (!rtnl_trylock()) ? -EBUSY : 0;
  411. }
  412. return -EINVAL;
  413. }
  414. struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  415. {
  416. struct ipoib_dev_priv *priv = netdev_priv(dev);
  417. struct rb_node *n = priv->path_tree.rb_node;
  418. struct ipoib_path *path;
  419. int ret;
  420. while (n) {
  421. path = rb_entry(n, struct ipoib_path, rb_node);
  422. ret = memcmp(gid, path->pathrec.dgid.raw,
  423. sizeof (union ib_gid));
  424. if (ret < 0)
  425. n = n->rb_left;
  426. else if (ret > 0)
  427. n = n->rb_right;
  428. else
  429. return path;
  430. }
  431. return NULL;
  432. }
  433. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  434. {
  435. struct ipoib_dev_priv *priv = netdev_priv(dev);
  436. struct rb_node **n = &priv->path_tree.rb_node;
  437. struct rb_node *pn = NULL;
  438. struct ipoib_path *tpath;
  439. int ret;
  440. while (*n) {
  441. pn = *n;
  442. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  443. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  444. sizeof (union ib_gid));
  445. if (ret < 0)
  446. n = &pn->rb_left;
  447. else if (ret > 0)
  448. n = &pn->rb_right;
  449. else
  450. return -EEXIST;
  451. }
  452. rb_link_node(&path->rb_node, pn, n);
  453. rb_insert_color(&path->rb_node, &priv->path_tree);
  454. list_add_tail(&path->list, &priv->path_list);
  455. return 0;
  456. }
  457. static void path_free(struct net_device *dev, struct ipoib_path *path)
  458. {
  459. struct sk_buff *skb;
  460. while ((skb = __skb_dequeue(&path->queue)))
  461. dev_kfree_skb_irq(skb);
  462. ipoib_dbg(netdev_priv(dev), "path_free\n");
  463. /* remove all neigh connected to this path */
  464. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  465. if (path->ah)
  466. ipoib_put_ah(path->ah);
  467. kfree(path);
  468. }
  469. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  470. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  471. {
  472. struct ipoib_path_iter *iter;
  473. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  474. if (!iter)
  475. return NULL;
  476. iter->dev = dev;
  477. memset(iter->path.pathrec.dgid.raw, 0, 16);
  478. if (ipoib_path_iter_next(iter)) {
  479. kfree(iter);
  480. return NULL;
  481. }
  482. return iter;
  483. }
  484. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  485. {
  486. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  487. struct rb_node *n;
  488. struct ipoib_path *path;
  489. int ret = 1;
  490. spin_lock_irq(&priv->lock);
  491. n = rb_first(&priv->path_tree);
  492. while (n) {
  493. path = rb_entry(n, struct ipoib_path, rb_node);
  494. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  495. sizeof (union ib_gid)) < 0) {
  496. iter->path = *path;
  497. ret = 0;
  498. break;
  499. }
  500. n = rb_next(n);
  501. }
  502. spin_unlock_irq(&priv->lock);
  503. return ret;
  504. }
  505. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  506. struct ipoib_path *path)
  507. {
  508. *path = iter->path;
  509. }
  510. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  511. void ipoib_mark_paths_invalid(struct net_device *dev)
  512. {
  513. struct ipoib_dev_priv *priv = netdev_priv(dev);
  514. struct ipoib_path *path, *tp;
  515. spin_lock_irq(&priv->lock);
  516. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  517. ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
  518. be16_to_cpu(path->pathrec.dlid),
  519. path->pathrec.dgid.raw);
  520. path->valid = 0;
  521. }
  522. spin_unlock_irq(&priv->lock);
  523. }
  524. static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
  525. {
  526. struct ipoib_pseudo_header *phdr;
  527. phdr = (struct ipoib_pseudo_header *)skb_push(skb, sizeof(*phdr));
  528. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  529. }
  530. void ipoib_flush_paths(struct net_device *dev)
  531. {
  532. struct ipoib_dev_priv *priv = netdev_priv(dev);
  533. struct ipoib_path *path, *tp;
  534. LIST_HEAD(remove_list);
  535. unsigned long flags;
  536. netif_tx_lock_bh(dev);
  537. spin_lock_irqsave(&priv->lock, flags);
  538. list_splice_init(&priv->path_list, &remove_list);
  539. list_for_each_entry(path, &remove_list, list)
  540. rb_erase(&path->rb_node, &priv->path_tree);
  541. list_for_each_entry_safe(path, tp, &remove_list, list) {
  542. if (path->query)
  543. ib_sa_cancel_query(path->query_id, path->query);
  544. spin_unlock_irqrestore(&priv->lock, flags);
  545. netif_tx_unlock_bh(dev);
  546. wait_for_completion(&path->done);
  547. path_free(dev, path);
  548. netif_tx_lock_bh(dev);
  549. spin_lock_irqsave(&priv->lock, flags);
  550. }
  551. spin_unlock_irqrestore(&priv->lock, flags);
  552. netif_tx_unlock_bh(dev);
  553. }
  554. static void path_rec_completion(int status,
  555. struct ib_sa_path_rec *pathrec,
  556. void *path_ptr)
  557. {
  558. struct ipoib_path *path = path_ptr;
  559. struct net_device *dev = path->dev;
  560. struct ipoib_dev_priv *priv = netdev_priv(dev);
  561. struct ipoib_ah *ah = NULL;
  562. struct ipoib_ah *old_ah = NULL;
  563. struct ipoib_neigh *neigh, *tn;
  564. struct sk_buff_head skqueue;
  565. struct sk_buff *skb;
  566. unsigned long flags;
  567. if (!status)
  568. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
  569. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  570. else
  571. ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
  572. status, path->pathrec.dgid.raw);
  573. skb_queue_head_init(&skqueue);
  574. if (!status) {
  575. struct ib_ah_attr av;
  576. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  577. ah = ipoib_create_ah(dev, priv->pd, &av);
  578. }
  579. spin_lock_irqsave(&priv->lock, flags);
  580. if (!IS_ERR_OR_NULL(ah)) {
  581. /*
  582. * pathrec.dgid is used as the database key from the LLADDR,
  583. * it must remain unchanged even if the SA returns a different
  584. * GID to use in the AH.
  585. */
  586. if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
  587. sizeof(union ib_gid))) {
  588. ipoib_dbg(
  589. priv,
  590. "%s got PathRec for gid %pI6 while asked for %pI6\n",
  591. dev->name, pathrec->dgid.raw,
  592. path->pathrec.dgid.raw);
  593. memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
  594. sizeof(union ib_gid));
  595. }
  596. path->pathrec = *pathrec;
  597. old_ah = path->ah;
  598. path->ah = ah;
  599. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  600. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  601. while ((skb = __skb_dequeue(&path->queue)))
  602. __skb_queue_tail(&skqueue, skb);
  603. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  604. if (neigh->ah) {
  605. WARN_ON(neigh->ah != old_ah);
  606. /*
  607. * Dropping the ah reference inside
  608. * priv->lock is safe here, because we
  609. * will hold one more reference from
  610. * the original value of path->ah (ie
  611. * old_ah).
  612. */
  613. ipoib_put_ah(neigh->ah);
  614. }
  615. kref_get(&path->ah->ref);
  616. neigh->ah = path->ah;
  617. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  618. if (!ipoib_cm_get(neigh))
  619. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  620. path,
  621. neigh));
  622. if (!ipoib_cm_get(neigh)) {
  623. ipoib_neigh_free(neigh);
  624. continue;
  625. }
  626. }
  627. while ((skb = __skb_dequeue(&neigh->queue)))
  628. __skb_queue_tail(&skqueue, skb);
  629. }
  630. path->valid = 1;
  631. }
  632. path->query = NULL;
  633. complete(&path->done);
  634. spin_unlock_irqrestore(&priv->lock, flags);
  635. if (IS_ERR_OR_NULL(ah))
  636. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  637. if (old_ah)
  638. ipoib_put_ah(old_ah);
  639. while ((skb = __skb_dequeue(&skqueue))) {
  640. skb->dev = dev;
  641. if (dev_queue_xmit(skb))
  642. ipoib_warn(priv, "dev_queue_xmit failed "
  643. "to requeue packet\n");
  644. }
  645. }
  646. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  647. {
  648. struct ipoib_dev_priv *priv = netdev_priv(dev);
  649. struct ipoib_path *path;
  650. if (!priv->broadcast)
  651. return NULL;
  652. path = kzalloc(sizeof *path, GFP_ATOMIC);
  653. if (!path)
  654. return NULL;
  655. path->dev = dev;
  656. skb_queue_head_init(&path->queue);
  657. INIT_LIST_HEAD(&path->neigh_list);
  658. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  659. path->pathrec.sgid = priv->local_gid;
  660. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  661. path->pathrec.numb_path = 1;
  662. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  663. return path;
  664. }
  665. static int path_rec_start(struct net_device *dev,
  666. struct ipoib_path *path)
  667. {
  668. struct ipoib_dev_priv *priv = netdev_priv(dev);
  669. ipoib_dbg(priv, "Start path record lookup for %pI6\n",
  670. path->pathrec.dgid.raw);
  671. init_completion(&path->done);
  672. path->query_id =
  673. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  674. &path->pathrec,
  675. IB_SA_PATH_REC_DGID |
  676. IB_SA_PATH_REC_SGID |
  677. IB_SA_PATH_REC_NUMB_PATH |
  678. IB_SA_PATH_REC_TRAFFIC_CLASS |
  679. IB_SA_PATH_REC_PKEY,
  680. 1000, GFP_ATOMIC,
  681. path_rec_completion,
  682. path, &path->query);
  683. if (path->query_id < 0) {
  684. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  685. path->query = NULL;
  686. complete(&path->done);
  687. return path->query_id;
  688. }
  689. return 0;
  690. }
  691. static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
  692. struct net_device *dev)
  693. {
  694. struct ipoib_dev_priv *priv = netdev_priv(dev);
  695. struct ipoib_path *path;
  696. struct ipoib_neigh *neigh;
  697. unsigned long flags;
  698. spin_lock_irqsave(&priv->lock, flags);
  699. neigh = ipoib_neigh_alloc(daddr, dev);
  700. if (!neigh) {
  701. spin_unlock_irqrestore(&priv->lock, flags);
  702. ++dev->stats.tx_dropped;
  703. dev_kfree_skb_any(skb);
  704. return NULL;
  705. }
  706. /* To avoid race condition, make sure that the
  707. * neigh will be added only once.
  708. */
  709. if (unlikely(!list_empty(&neigh->list))) {
  710. spin_unlock_irqrestore(&priv->lock, flags);
  711. return neigh;
  712. }
  713. path = __path_find(dev, daddr + 4);
  714. if (!path) {
  715. path = path_rec_create(dev, daddr + 4);
  716. if (!path)
  717. goto err_path;
  718. __path_add(dev, path);
  719. }
  720. list_add_tail(&neigh->list, &path->neigh_list);
  721. if (path->ah) {
  722. kref_get(&path->ah->ref);
  723. neigh->ah = path->ah;
  724. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  725. if (!ipoib_cm_get(neigh))
  726. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  727. if (!ipoib_cm_get(neigh)) {
  728. ipoib_neigh_free(neigh);
  729. goto err_drop;
  730. }
  731. if (skb_queue_len(&neigh->queue) <
  732. IPOIB_MAX_PATH_REC_QUEUE) {
  733. push_pseudo_header(skb, neigh->daddr);
  734. __skb_queue_tail(&neigh->queue, skb);
  735. } else {
  736. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  737. skb_queue_len(&neigh->queue));
  738. goto err_drop;
  739. }
  740. } else {
  741. spin_unlock_irqrestore(&priv->lock, flags);
  742. ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
  743. ipoib_neigh_put(neigh);
  744. return NULL;
  745. }
  746. } else {
  747. neigh->ah = NULL;
  748. if (!path->query && path_rec_start(dev, path))
  749. goto err_path;
  750. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  751. push_pseudo_header(skb, neigh->daddr);
  752. __skb_queue_tail(&neigh->queue, skb);
  753. } else {
  754. goto err_drop;
  755. }
  756. }
  757. spin_unlock_irqrestore(&priv->lock, flags);
  758. ipoib_neigh_put(neigh);
  759. return NULL;
  760. err_path:
  761. ipoib_neigh_free(neigh);
  762. err_drop:
  763. ++dev->stats.tx_dropped;
  764. dev_kfree_skb_any(skb);
  765. spin_unlock_irqrestore(&priv->lock, flags);
  766. ipoib_neigh_put(neigh);
  767. return NULL;
  768. }
  769. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  770. struct ipoib_pseudo_header *phdr)
  771. {
  772. struct ipoib_dev_priv *priv = netdev_priv(dev);
  773. struct ipoib_path *path;
  774. unsigned long flags;
  775. spin_lock_irqsave(&priv->lock, flags);
  776. path = __path_find(dev, phdr->hwaddr + 4);
  777. if (!path || !path->valid) {
  778. int new_path = 0;
  779. if (!path) {
  780. path = path_rec_create(dev, phdr->hwaddr + 4);
  781. new_path = 1;
  782. }
  783. if (path) {
  784. if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  785. push_pseudo_header(skb, phdr->hwaddr);
  786. __skb_queue_tail(&path->queue, skb);
  787. } else {
  788. ++dev->stats.tx_dropped;
  789. dev_kfree_skb_any(skb);
  790. }
  791. if (!path->query && path_rec_start(dev, path)) {
  792. spin_unlock_irqrestore(&priv->lock, flags);
  793. if (new_path)
  794. path_free(dev, path);
  795. return;
  796. } else
  797. __path_add(dev, path);
  798. } else {
  799. ++dev->stats.tx_dropped;
  800. dev_kfree_skb_any(skb);
  801. }
  802. spin_unlock_irqrestore(&priv->lock, flags);
  803. return;
  804. }
  805. if (path->ah) {
  806. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  807. be16_to_cpu(path->pathrec.dlid));
  808. spin_unlock_irqrestore(&priv->lock, flags);
  809. ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
  810. return;
  811. } else if ((path->query || !path_rec_start(dev, path)) &&
  812. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  813. push_pseudo_header(skb, phdr->hwaddr);
  814. __skb_queue_tail(&path->queue, skb);
  815. } else {
  816. ++dev->stats.tx_dropped;
  817. dev_kfree_skb_any(skb);
  818. }
  819. spin_unlock_irqrestore(&priv->lock, flags);
  820. }
  821. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  822. {
  823. struct ipoib_dev_priv *priv = netdev_priv(dev);
  824. struct ipoib_neigh *neigh;
  825. struct ipoib_pseudo_header *phdr;
  826. struct ipoib_header *header;
  827. unsigned long flags;
  828. phdr = (struct ipoib_pseudo_header *) skb->data;
  829. skb_pull(skb, sizeof(*phdr));
  830. header = (struct ipoib_header *) skb->data;
  831. if (unlikely(phdr->hwaddr[4] == 0xff)) {
  832. /* multicast, arrange "if" according to probability */
  833. if ((header->proto != htons(ETH_P_IP)) &&
  834. (header->proto != htons(ETH_P_IPV6)) &&
  835. (header->proto != htons(ETH_P_ARP)) &&
  836. (header->proto != htons(ETH_P_RARP)) &&
  837. (header->proto != htons(ETH_P_TIPC))) {
  838. /* ethertype not supported by IPoIB */
  839. ++dev->stats.tx_dropped;
  840. dev_kfree_skb_any(skb);
  841. return NETDEV_TX_OK;
  842. }
  843. /* Add in the P_Key for multicast*/
  844. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  845. phdr->hwaddr[9] = priv->pkey & 0xff;
  846. neigh = ipoib_neigh_get(dev, phdr->hwaddr);
  847. if (likely(neigh))
  848. goto send_using_neigh;
  849. ipoib_mcast_send(dev, phdr->hwaddr, skb);
  850. return NETDEV_TX_OK;
  851. }
  852. /* unicast, arrange "switch" according to probability */
  853. switch (header->proto) {
  854. case htons(ETH_P_IP):
  855. case htons(ETH_P_IPV6):
  856. case htons(ETH_P_TIPC):
  857. neigh = ipoib_neigh_get(dev, phdr->hwaddr);
  858. if (unlikely(!neigh)) {
  859. neigh = neigh_add_path(skb, phdr->hwaddr, dev);
  860. if (likely(!neigh))
  861. return NETDEV_TX_OK;
  862. }
  863. break;
  864. case htons(ETH_P_ARP):
  865. case htons(ETH_P_RARP):
  866. /* for unicast ARP and RARP should always perform path find */
  867. unicast_arp_send(skb, dev, phdr);
  868. return NETDEV_TX_OK;
  869. default:
  870. /* ethertype not supported by IPoIB */
  871. ++dev->stats.tx_dropped;
  872. dev_kfree_skb_any(skb);
  873. return NETDEV_TX_OK;
  874. }
  875. send_using_neigh:
  876. /* note we now hold a ref to neigh */
  877. if (ipoib_cm_get(neigh)) {
  878. if (ipoib_cm_up(neigh)) {
  879. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  880. goto unref;
  881. }
  882. } else if (neigh->ah) {
  883. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(phdr->hwaddr));
  884. goto unref;
  885. }
  886. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  887. push_pseudo_header(skb, phdr->hwaddr);
  888. spin_lock_irqsave(&priv->lock, flags);
  889. __skb_queue_tail(&neigh->queue, skb);
  890. spin_unlock_irqrestore(&priv->lock, flags);
  891. } else {
  892. ++dev->stats.tx_dropped;
  893. dev_kfree_skb_any(skb);
  894. }
  895. unref:
  896. ipoib_neigh_put(neigh);
  897. return NETDEV_TX_OK;
  898. }
  899. static void ipoib_timeout(struct net_device *dev)
  900. {
  901. struct ipoib_dev_priv *priv = netdev_priv(dev);
  902. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  903. jiffies_to_msecs(jiffies - dev->trans_start));
  904. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  905. netif_queue_stopped(dev),
  906. priv->tx_head, priv->tx_tail);
  907. /* XXX reset QP, etc. */
  908. }
  909. static int ipoib_hard_header(struct sk_buff *skb,
  910. struct net_device *dev,
  911. unsigned short type,
  912. const void *daddr, const void *saddr, unsigned len)
  913. {
  914. struct ipoib_header *header;
  915. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  916. header->proto = htons(type);
  917. header->reserved = 0;
  918. /*
  919. * we don't rely on dst_entry structure, always stuff the
  920. * destination address into skb hard header so we can figure out where
  921. * to send the packet later.
  922. */
  923. push_pseudo_header(skb, daddr);
  924. return IPOIB_HARD_LEN;
  925. }
  926. static void ipoib_set_mcast_list(struct net_device *dev)
  927. {
  928. struct ipoib_dev_priv *priv = netdev_priv(dev);
  929. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  930. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  931. return;
  932. }
  933. queue_work(priv->wq, &priv->restart_task);
  934. }
  935. static int ipoib_get_iflink(const struct net_device *dev)
  936. {
  937. struct ipoib_dev_priv *priv = netdev_priv(dev);
  938. /* parent interface */
  939. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
  940. return dev->ifindex;
  941. /* child/vlan interface */
  942. return priv->parent->ifindex;
  943. }
  944. static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
  945. {
  946. /*
  947. * Use only the address parts that contributes to spreading
  948. * The subnet prefix is not used as one can not connect to
  949. * same remote port (GUID) using the same remote QPN via two
  950. * different subnets.
  951. */
  952. /* qpn octets[1:4) & port GUID octets[12:20) */
  953. u32 *d32 = (u32 *) daddr;
  954. u32 hv;
  955. hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
  956. return hv & htbl->mask;
  957. }
  958. struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
  959. {
  960. struct ipoib_dev_priv *priv = netdev_priv(dev);
  961. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  962. struct ipoib_neigh_hash *htbl;
  963. struct ipoib_neigh *neigh = NULL;
  964. u32 hash_val;
  965. rcu_read_lock_bh();
  966. htbl = rcu_dereference_bh(ntbl->htbl);
  967. if (!htbl)
  968. goto out_unlock;
  969. hash_val = ipoib_addr_hash(htbl, daddr);
  970. for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
  971. neigh != NULL;
  972. neigh = rcu_dereference_bh(neigh->hnext)) {
  973. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  974. /* found, take one ref on behalf of the caller */
  975. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  976. /* deleted */
  977. neigh = NULL;
  978. goto out_unlock;
  979. }
  980. if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
  981. neigh->alive = jiffies;
  982. goto out_unlock;
  983. }
  984. }
  985. out_unlock:
  986. rcu_read_unlock_bh();
  987. return neigh;
  988. }
  989. static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
  990. {
  991. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  992. struct ipoib_neigh_hash *htbl;
  993. unsigned long neigh_obsolete;
  994. unsigned long dt;
  995. unsigned long flags;
  996. int i;
  997. LIST_HEAD(remove_list);
  998. struct ipoib_mcast *mcast, *tmcast;
  999. struct net_device *dev = priv->dev;
  1000. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  1001. return;
  1002. spin_lock_irqsave(&priv->lock, flags);
  1003. htbl = rcu_dereference_protected(ntbl->htbl,
  1004. lockdep_is_held(&priv->lock));
  1005. if (!htbl)
  1006. goto out_unlock;
  1007. /* neigh is obsolete if it was idle for two GC periods */
  1008. dt = 2 * arp_tbl.gc_interval;
  1009. neigh_obsolete = jiffies - dt;
  1010. /* handle possible race condition */
  1011. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  1012. goto out_unlock;
  1013. for (i = 0; i < htbl->size; i++) {
  1014. struct ipoib_neigh *neigh;
  1015. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  1016. while ((neigh = rcu_dereference_protected(*np,
  1017. lockdep_is_held(&priv->lock))) != NULL) {
  1018. /* was the neigh idle for two GC periods */
  1019. if (time_after(neigh_obsolete, neigh->alive)) {
  1020. u8 *mgid = neigh->daddr + 4;
  1021. /* Is this multicast ? */
  1022. if (*mgid == 0xff) {
  1023. mcast = __ipoib_mcast_find(dev, mgid);
  1024. if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  1025. list_del(&mcast->list);
  1026. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  1027. list_add_tail(&mcast->list, &remove_list);
  1028. }
  1029. }
  1030. rcu_assign_pointer(*np,
  1031. rcu_dereference_protected(neigh->hnext,
  1032. lockdep_is_held(&priv->lock)));
  1033. /* remove from path/mc list */
  1034. list_del_init(&neigh->list);
  1035. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1036. } else {
  1037. np = &neigh->hnext;
  1038. }
  1039. }
  1040. }
  1041. out_unlock:
  1042. spin_unlock_irqrestore(&priv->lock, flags);
  1043. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  1044. ipoib_mcast_leave(dev, mcast);
  1045. ipoib_mcast_free(mcast);
  1046. }
  1047. }
  1048. static void ipoib_reap_neigh(struct work_struct *work)
  1049. {
  1050. struct ipoib_dev_priv *priv =
  1051. container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
  1052. __ipoib_reap_neigh(priv);
  1053. if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  1054. queue_delayed_work(priv->wq, &priv->neigh_reap_task,
  1055. arp_tbl.gc_interval);
  1056. }
  1057. static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
  1058. struct net_device *dev)
  1059. {
  1060. struct ipoib_neigh *neigh;
  1061. neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
  1062. if (!neigh)
  1063. return NULL;
  1064. neigh->dev = dev;
  1065. memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
  1066. skb_queue_head_init(&neigh->queue);
  1067. INIT_LIST_HEAD(&neigh->list);
  1068. ipoib_cm_set(neigh, NULL);
  1069. /* one ref on behalf of the caller */
  1070. atomic_set(&neigh->refcnt, 1);
  1071. return neigh;
  1072. }
  1073. struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
  1074. struct net_device *dev)
  1075. {
  1076. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1077. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1078. struct ipoib_neigh_hash *htbl;
  1079. struct ipoib_neigh *neigh;
  1080. u32 hash_val;
  1081. htbl = rcu_dereference_protected(ntbl->htbl,
  1082. lockdep_is_held(&priv->lock));
  1083. if (!htbl) {
  1084. neigh = NULL;
  1085. goto out_unlock;
  1086. }
  1087. /* need to add a new neigh, but maybe some other thread succeeded?
  1088. * recalc hash, maybe hash resize took place so we do a search
  1089. */
  1090. hash_val = ipoib_addr_hash(htbl, daddr);
  1091. for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
  1092. lockdep_is_held(&priv->lock));
  1093. neigh != NULL;
  1094. neigh = rcu_dereference_protected(neigh->hnext,
  1095. lockdep_is_held(&priv->lock))) {
  1096. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  1097. /* found, take one ref on behalf of the caller */
  1098. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  1099. /* deleted */
  1100. neigh = NULL;
  1101. break;
  1102. }
  1103. neigh->alive = jiffies;
  1104. goto out_unlock;
  1105. }
  1106. }
  1107. neigh = ipoib_neigh_ctor(daddr, dev);
  1108. if (!neigh)
  1109. goto out_unlock;
  1110. /* one ref on behalf of the hash table */
  1111. atomic_inc(&neigh->refcnt);
  1112. neigh->alive = jiffies;
  1113. /* put in hash */
  1114. rcu_assign_pointer(neigh->hnext,
  1115. rcu_dereference_protected(htbl->buckets[hash_val],
  1116. lockdep_is_held(&priv->lock)));
  1117. rcu_assign_pointer(htbl->buckets[hash_val], neigh);
  1118. atomic_inc(&ntbl->entries);
  1119. out_unlock:
  1120. return neigh;
  1121. }
  1122. void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
  1123. {
  1124. /* neigh reference count was dropprd to zero */
  1125. struct net_device *dev = neigh->dev;
  1126. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1127. struct sk_buff *skb;
  1128. if (neigh->ah)
  1129. ipoib_put_ah(neigh->ah);
  1130. while ((skb = __skb_dequeue(&neigh->queue))) {
  1131. ++dev->stats.tx_dropped;
  1132. dev_kfree_skb_any(skb);
  1133. }
  1134. if (ipoib_cm_get(neigh))
  1135. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  1136. ipoib_dbg(netdev_priv(dev),
  1137. "neigh free for %06x %pI6\n",
  1138. IPOIB_QPN(neigh->daddr),
  1139. neigh->daddr + 4);
  1140. kfree(neigh);
  1141. if (atomic_dec_and_test(&priv->ntbl.entries)) {
  1142. if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
  1143. complete(&priv->ntbl.flushed);
  1144. }
  1145. }
  1146. static void ipoib_neigh_reclaim(struct rcu_head *rp)
  1147. {
  1148. /* Called as a result of removal from hash table */
  1149. struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
  1150. /* note TX context may hold another ref */
  1151. ipoib_neigh_put(neigh);
  1152. }
  1153. void ipoib_neigh_free(struct ipoib_neigh *neigh)
  1154. {
  1155. struct net_device *dev = neigh->dev;
  1156. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1157. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1158. struct ipoib_neigh_hash *htbl;
  1159. struct ipoib_neigh __rcu **np;
  1160. struct ipoib_neigh *n;
  1161. u32 hash_val;
  1162. htbl = rcu_dereference_protected(ntbl->htbl,
  1163. lockdep_is_held(&priv->lock));
  1164. if (!htbl)
  1165. return;
  1166. hash_val = ipoib_addr_hash(htbl, neigh->daddr);
  1167. np = &htbl->buckets[hash_val];
  1168. for (n = rcu_dereference_protected(*np,
  1169. lockdep_is_held(&priv->lock));
  1170. n != NULL;
  1171. n = rcu_dereference_protected(*np,
  1172. lockdep_is_held(&priv->lock))) {
  1173. if (n == neigh) {
  1174. /* found */
  1175. rcu_assign_pointer(*np,
  1176. rcu_dereference_protected(neigh->hnext,
  1177. lockdep_is_held(&priv->lock)));
  1178. /* remove from parent list */
  1179. list_del_init(&neigh->list);
  1180. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1181. return;
  1182. } else {
  1183. np = &n->hnext;
  1184. }
  1185. }
  1186. }
  1187. static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
  1188. {
  1189. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1190. struct ipoib_neigh_hash *htbl;
  1191. struct ipoib_neigh __rcu **buckets;
  1192. u32 size;
  1193. clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1194. ntbl->htbl = NULL;
  1195. htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
  1196. if (!htbl)
  1197. return -ENOMEM;
  1198. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1199. size = roundup_pow_of_two(arp_tbl.gc_thresh3);
  1200. buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
  1201. if (!buckets) {
  1202. kfree(htbl);
  1203. return -ENOMEM;
  1204. }
  1205. htbl->size = size;
  1206. htbl->mask = (size - 1);
  1207. htbl->buckets = buckets;
  1208. RCU_INIT_POINTER(ntbl->htbl, htbl);
  1209. htbl->ntbl = ntbl;
  1210. atomic_set(&ntbl->entries, 0);
  1211. /* start garbage collection */
  1212. clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1213. queue_delayed_work(priv->wq, &priv->neigh_reap_task,
  1214. arp_tbl.gc_interval);
  1215. return 0;
  1216. }
  1217. static void neigh_hash_free_rcu(struct rcu_head *head)
  1218. {
  1219. struct ipoib_neigh_hash *htbl = container_of(head,
  1220. struct ipoib_neigh_hash,
  1221. rcu);
  1222. struct ipoib_neigh __rcu **buckets = htbl->buckets;
  1223. struct ipoib_neigh_table *ntbl = htbl->ntbl;
  1224. kfree(buckets);
  1225. kfree(htbl);
  1226. complete(&ntbl->deleted);
  1227. }
  1228. void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
  1229. {
  1230. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1231. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1232. struct ipoib_neigh_hash *htbl;
  1233. unsigned long flags;
  1234. int i;
  1235. /* remove all neigh connected to a given path or mcast */
  1236. spin_lock_irqsave(&priv->lock, flags);
  1237. htbl = rcu_dereference_protected(ntbl->htbl,
  1238. lockdep_is_held(&priv->lock));
  1239. if (!htbl)
  1240. goto out_unlock;
  1241. for (i = 0; i < htbl->size; i++) {
  1242. struct ipoib_neigh *neigh;
  1243. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  1244. while ((neigh = rcu_dereference_protected(*np,
  1245. lockdep_is_held(&priv->lock))) != NULL) {
  1246. /* delete neighs belong to this parent */
  1247. if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
  1248. rcu_assign_pointer(*np,
  1249. rcu_dereference_protected(neigh->hnext,
  1250. lockdep_is_held(&priv->lock)));
  1251. /* remove from parent list */
  1252. list_del_init(&neigh->list);
  1253. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1254. } else {
  1255. np = &neigh->hnext;
  1256. }
  1257. }
  1258. }
  1259. out_unlock:
  1260. spin_unlock_irqrestore(&priv->lock, flags);
  1261. }
  1262. static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
  1263. {
  1264. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1265. struct ipoib_neigh_hash *htbl;
  1266. unsigned long flags;
  1267. int i, wait_flushed = 0;
  1268. init_completion(&priv->ntbl.flushed);
  1269. spin_lock_irqsave(&priv->lock, flags);
  1270. htbl = rcu_dereference_protected(ntbl->htbl,
  1271. lockdep_is_held(&priv->lock));
  1272. if (!htbl)
  1273. goto out_unlock;
  1274. wait_flushed = atomic_read(&priv->ntbl.entries);
  1275. if (!wait_flushed)
  1276. goto free_htbl;
  1277. for (i = 0; i < htbl->size; i++) {
  1278. struct ipoib_neigh *neigh;
  1279. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  1280. while ((neigh = rcu_dereference_protected(*np,
  1281. lockdep_is_held(&priv->lock))) != NULL) {
  1282. rcu_assign_pointer(*np,
  1283. rcu_dereference_protected(neigh->hnext,
  1284. lockdep_is_held(&priv->lock)));
  1285. /* remove from path/mc list */
  1286. list_del_init(&neigh->list);
  1287. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1288. }
  1289. }
  1290. free_htbl:
  1291. rcu_assign_pointer(ntbl->htbl, NULL);
  1292. call_rcu(&htbl->rcu, neigh_hash_free_rcu);
  1293. out_unlock:
  1294. spin_unlock_irqrestore(&priv->lock, flags);
  1295. if (wait_flushed)
  1296. wait_for_completion(&priv->ntbl.flushed);
  1297. }
  1298. static void ipoib_neigh_hash_uninit(struct net_device *dev)
  1299. {
  1300. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1301. int stopped;
  1302. ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
  1303. init_completion(&priv->ntbl.deleted);
  1304. set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1305. /* Stop GC if called at init fail need to cancel work */
  1306. stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1307. if (!stopped)
  1308. cancel_delayed_work(&priv->neigh_reap_task);
  1309. ipoib_flush_neighs(priv);
  1310. wait_for_completion(&priv->ntbl.deleted);
  1311. }
  1312. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  1313. {
  1314. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1315. /* Allocate RX/TX "rings" to hold queued skbs */
  1316. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  1317. GFP_KERNEL);
  1318. if (!priv->rx_ring) {
  1319. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  1320. ca->name, ipoib_recvq_size);
  1321. goto out;
  1322. }
  1323. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  1324. if (!priv->tx_ring) {
  1325. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  1326. ca->name, ipoib_sendq_size);
  1327. goto out_rx_ring_cleanup;
  1328. }
  1329. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  1330. if (ipoib_ib_dev_init(dev, ca, port))
  1331. goto out_tx_ring_cleanup;
  1332. /*
  1333. * Must be after ipoib_ib_dev_init so we can allocate a per
  1334. * device wq there and use it here
  1335. */
  1336. if (ipoib_neigh_hash_init(priv) < 0)
  1337. goto out_dev_uninit;
  1338. return 0;
  1339. out_dev_uninit:
  1340. ipoib_ib_dev_cleanup(dev);
  1341. out_tx_ring_cleanup:
  1342. vfree(priv->tx_ring);
  1343. out_rx_ring_cleanup:
  1344. kfree(priv->rx_ring);
  1345. out:
  1346. return -ENOMEM;
  1347. }
  1348. void ipoib_dev_cleanup(struct net_device *dev)
  1349. {
  1350. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  1351. LIST_HEAD(head);
  1352. ASSERT_RTNL();
  1353. /* Delete any child interfaces first */
  1354. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  1355. /* Stop GC on child */
  1356. set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
  1357. cancel_delayed_work(&cpriv->neigh_reap_task);
  1358. unregister_netdevice_queue(cpriv->dev, &head);
  1359. }
  1360. unregister_netdevice_many(&head);
  1361. /*
  1362. * Must be before ipoib_ib_dev_cleanup or we delete an in use
  1363. * work queue
  1364. */
  1365. ipoib_neigh_hash_uninit(dev);
  1366. ipoib_ib_dev_cleanup(dev);
  1367. kfree(priv->rx_ring);
  1368. vfree(priv->tx_ring);
  1369. priv->rx_ring = NULL;
  1370. priv->tx_ring = NULL;
  1371. }
  1372. static const struct header_ops ipoib_header_ops = {
  1373. .create = ipoib_hard_header,
  1374. };
  1375. static const struct net_device_ops ipoib_netdev_ops = {
  1376. .ndo_uninit = ipoib_uninit,
  1377. .ndo_open = ipoib_open,
  1378. .ndo_stop = ipoib_stop,
  1379. .ndo_change_mtu = ipoib_change_mtu,
  1380. .ndo_fix_features = ipoib_fix_features,
  1381. .ndo_start_xmit = ipoib_start_xmit,
  1382. .ndo_tx_timeout = ipoib_timeout,
  1383. .ndo_set_rx_mode = ipoib_set_mcast_list,
  1384. .ndo_get_iflink = ipoib_get_iflink,
  1385. };
  1386. void ipoib_setup(struct net_device *dev)
  1387. {
  1388. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1389. dev->netdev_ops = &ipoib_netdev_ops;
  1390. dev->header_ops = &ipoib_header_ops;
  1391. ipoib_set_ethtool_ops(dev);
  1392. netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
  1393. dev->watchdog_timeo = HZ;
  1394. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1395. dev->hard_header_len = IPOIB_HARD_LEN;
  1396. dev->addr_len = INFINIBAND_ALEN;
  1397. dev->type = ARPHRD_INFINIBAND;
  1398. dev->tx_queue_len = ipoib_sendq_size * 2;
  1399. dev->features = (NETIF_F_VLAN_CHALLENGED |
  1400. NETIF_F_HIGHDMA);
  1401. netif_keep_dst(dev);
  1402. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  1403. priv->dev = dev;
  1404. spin_lock_init(&priv->lock);
  1405. init_rwsem(&priv->vlan_rwsem);
  1406. INIT_LIST_HEAD(&priv->path_list);
  1407. INIT_LIST_HEAD(&priv->child_intfs);
  1408. INIT_LIST_HEAD(&priv->dead_ahs);
  1409. INIT_LIST_HEAD(&priv->multicast_list);
  1410. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  1411. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  1412. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  1413. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  1414. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  1415. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  1416. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  1417. INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
  1418. }
  1419. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  1420. {
  1421. struct net_device *dev;
  1422. dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
  1423. NET_NAME_UNKNOWN, ipoib_setup);
  1424. if (!dev)
  1425. return NULL;
  1426. return netdev_priv(dev);
  1427. }
  1428. static ssize_t show_pkey(struct device *dev,
  1429. struct device_attribute *attr, char *buf)
  1430. {
  1431. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1432. return sprintf(buf, "0x%04x\n", priv->pkey);
  1433. }
  1434. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1435. static ssize_t show_umcast(struct device *dev,
  1436. struct device_attribute *attr, char *buf)
  1437. {
  1438. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1439. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  1440. }
  1441. void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
  1442. {
  1443. struct ipoib_dev_priv *priv = netdev_priv(ndev);
  1444. if (umcast_val > 0) {
  1445. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1446. ipoib_warn(priv, "ignoring multicast groups joined directly "
  1447. "by userspace\n");
  1448. } else
  1449. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1450. }
  1451. static ssize_t set_umcast(struct device *dev,
  1452. struct device_attribute *attr,
  1453. const char *buf, size_t count)
  1454. {
  1455. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  1456. ipoib_set_umcast(to_net_dev(dev), umcast_val);
  1457. return count;
  1458. }
  1459. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  1460. int ipoib_add_umcast_attr(struct net_device *dev)
  1461. {
  1462. return device_create_file(&dev->dev, &dev_attr_umcast);
  1463. }
  1464. static ssize_t create_child(struct device *dev,
  1465. struct device_attribute *attr,
  1466. const char *buf, size_t count)
  1467. {
  1468. int pkey;
  1469. int ret;
  1470. if (sscanf(buf, "%i", &pkey) != 1)
  1471. return -EINVAL;
  1472. if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
  1473. return -EINVAL;
  1474. /*
  1475. * Set the full membership bit, so that we join the right
  1476. * broadcast group, etc.
  1477. */
  1478. pkey |= 0x8000;
  1479. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  1480. return ret ? ret : count;
  1481. }
  1482. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  1483. static ssize_t delete_child(struct device *dev,
  1484. struct device_attribute *attr,
  1485. const char *buf, size_t count)
  1486. {
  1487. int pkey;
  1488. int ret;
  1489. if (sscanf(buf, "%i", &pkey) != 1)
  1490. return -EINVAL;
  1491. if (pkey < 0 || pkey > 0xffff)
  1492. return -EINVAL;
  1493. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  1494. return ret ? ret : count;
  1495. }
  1496. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  1497. int ipoib_add_pkey_attr(struct net_device *dev)
  1498. {
  1499. return device_create_file(&dev->dev, &dev_attr_pkey);
  1500. }
  1501. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  1502. {
  1503. struct ib_device_attr *device_attr;
  1504. int result = -ENOMEM;
  1505. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  1506. if (!device_attr) {
  1507. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  1508. hca->name, sizeof *device_attr);
  1509. return result;
  1510. }
  1511. result = ib_query_device(hca, device_attr);
  1512. if (result) {
  1513. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  1514. hca->name, result);
  1515. kfree(device_attr);
  1516. return result;
  1517. }
  1518. priv->hca_caps = device_attr->device_cap_flags;
  1519. kfree(device_attr);
  1520. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  1521. priv->dev->hw_features = NETIF_F_SG |
  1522. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  1523. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  1524. priv->dev->hw_features |= NETIF_F_TSO;
  1525. priv->dev->features |= priv->dev->hw_features;
  1526. }
  1527. return 0;
  1528. }
  1529. static struct net_device *ipoib_add_port(const char *format,
  1530. struct ib_device *hca, u8 port)
  1531. {
  1532. struct ipoib_dev_priv *priv;
  1533. struct ib_port_attr attr;
  1534. int result = -ENOMEM;
  1535. priv = ipoib_intf_alloc(format);
  1536. if (!priv)
  1537. goto alloc_mem_failed;
  1538. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  1539. priv->dev->dev_id = port - 1;
  1540. result = ib_query_port(hca, port, &attr);
  1541. if (!result)
  1542. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  1543. else {
  1544. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  1545. hca->name, port);
  1546. goto device_init_failed;
  1547. }
  1548. /* MTU will be reset when mcast join happens */
  1549. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  1550. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  1551. priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
  1552. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  1553. if (result) {
  1554. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  1555. hca->name, port, result);
  1556. goto device_init_failed;
  1557. }
  1558. result = ipoib_set_dev_features(priv, hca);
  1559. if (result)
  1560. goto device_init_failed;
  1561. /*
  1562. * Set the full membership bit, so that we join the right
  1563. * broadcast group, etc.
  1564. */
  1565. priv->pkey |= 0x8000;
  1566. priv->dev->broadcast[8] = priv->pkey >> 8;
  1567. priv->dev->broadcast[9] = priv->pkey & 0xff;
  1568. result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
  1569. if (result) {
  1570. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  1571. hca->name, port, result);
  1572. goto device_init_failed;
  1573. } else
  1574. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  1575. result = ipoib_dev_init(priv->dev, hca, port);
  1576. if (result < 0) {
  1577. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1578. hca->name, port, result);
  1579. goto device_init_failed;
  1580. }
  1581. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1582. priv->ca, ipoib_event);
  1583. result = ib_register_event_handler(&priv->event_handler);
  1584. if (result < 0) {
  1585. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1586. "port %d (ret = %d)\n",
  1587. hca->name, port, result);
  1588. goto event_failed;
  1589. }
  1590. /* call event handler to ensure pkey in sync */
  1591. queue_work(ipoib_workqueue, &priv->flush_heavy);
  1592. result = register_netdev(priv->dev);
  1593. if (result) {
  1594. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1595. hca->name, port, result);
  1596. goto register_failed;
  1597. }
  1598. if (ipoib_cm_add_mode_attr(priv->dev))
  1599. goto sysfs_failed;
  1600. if (ipoib_add_pkey_attr(priv->dev))
  1601. goto sysfs_failed;
  1602. if (ipoib_add_umcast_attr(priv->dev))
  1603. goto sysfs_failed;
  1604. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1605. goto sysfs_failed;
  1606. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1607. goto sysfs_failed;
  1608. return priv->dev;
  1609. sysfs_failed:
  1610. unregister_netdev(priv->dev);
  1611. register_failed:
  1612. ib_unregister_event_handler(&priv->event_handler);
  1613. flush_workqueue(ipoib_workqueue);
  1614. /* Stop GC if started before flush */
  1615. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1616. cancel_delayed_work(&priv->neigh_reap_task);
  1617. flush_workqueue(priv->wq);
  1618. event_failed:
  1619. ipoib_dev_cleanup(priv->dev);
  1620. device_init_failed:
  1621. free_netdev(priv->dev);
  1622. alloc_mem_failed:
  1623. return ERR_PTR(result);
  1624. }
  1625. static void ipoib_add_one(struct ib_device *device)
  1626. {
  1627. struct list_head *dev_list;
  1628. struct net_device *dev;
  1629. struct ipoib_dev_priv *priv;
  1630. int p;
  1631. int count = 0;
  1632. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1633. if (!dev_list)
  1634. return;
  1635. INIT_LIST_HEAD(dev_list);
  1636. for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
  1637. if (!rdma_protocol_ib(device, p))
  1638. continue;
  1639. dev = ipoib_add_port("ib%d", device, p);
  1640. if (!IS_ERR(dev)) {
  1641. priv = netdev_priv(dev);
  1642. list_add_tail(&priv->list, dev_list);
  1643. count++;
  1644. }
  1645. }
  1646. if (!count) {
  1647. kfree(dev_list);
  1648. return;
  1649. }
  1650. ib_set_client_data(device, &ipoib_client, dev_list);
  1651. }
  1652. static void ipoib_remove_one(struct ib_device *device, void *client_data)
  1653. {
  1654. struct ipoib_dev_priv *priv, *tmp;
  1655. struct list_head *dev_list = client_data;
  1656. if (!dev_list)
  1657. return;
  1658. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1659. ib_unregister_event_handler(&priv->event_handler);
  1660. flush_workqueue(ipoib_workqueue);
  1661. rtnl_lock();
  1662. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1663. rtnl_unlock();
  1664. /* Stop GC */
  1665. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1666. cancel_delayed_work(&priv->neigh_reap_task);
  1667. flush_workqueue(priv->wq);
  1668. unregister_netdev(priv->dev);
  1669. free_netdev(priv->dev);
  1670. }
  1671. kfree(dev_list);
  1672. }
  1673. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  1674. static struct notifier_block ipoib_netdev_notifier = {
  1675. .notifier_call = ipoib_netdev_event,
  1676. };
  1677. #endif
  1678. static int __init ipoib_init_module(void)
  1679. {
  1680. int ret;
  1681. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1682. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1683. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1684. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1685. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1686. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1687. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1688. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1689. #endif
  1690. /*
  1691. * When copying small received packets, we only copy from the
  1692. * linear data part of the SKB, so we rely on this condition.
  1693. */
  1694. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1695. ret = ipoib_register_debugfs();
  1696. if (ret)
  1697. return ret;
  1698. /*
  1699. * We create a global workqueue here that is used for all flush
  1700. * operations. However, if you attempt to flush a workqueue
  1701. * from a task on that same workqueue, it deadlocks the system.
  1702. * We want to be able to flush the tasks associated with a
  1703. * specific net device, so we also create a workqueue for each
  1704. * netdevice. We queue up the tasks for that device only on
  1705. * its private workqueue, and we only queue up flush events
  1706. * on our global flush workqueue. This avoids the deadlocks.
  1707. */
  1708. ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
  1709. if (!ipoib_workqueue) {
  1710. ret = -ENOMEM;
  1711. goto err_fs;
  1712. }
  1713. ib_sa_register_client(&ipoib_sa_client);
  1714. ret = ib_register_client(&ipoib_client);
  1715. if (ret)
  1716. goto err_sa;
  1717. ret = ipoib_netlink_init();
  1718. if (ret)
  1719. goto err_client;
  1720. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  1721. register_netdevice_notifier(&ipoib_netdev_notifier);
  1722. #endif
  1723. return 0;
  1724. err_client:
  1725. ib_unregister_client(&ipoib_client);
  1726. err_sa:
  1727. ib_sa_unregister_client(&ipoib_sa_client);
  1728. destroy_workqueue(ipoib_workqueue);
  1729. err_fs:
  1730. ipoib_unregister_debugfs();
  1731. return ret;
  1732. }
  1733. static void __exit ipoib_cleanup_module(void)
  1734. {
  1735. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  1736. unregister_netdevice_notifier(&ipoib_netdev_notifier);
  1737. #endif
  1738. ipoib_netlink_fini();
  1739. ib_unregister_client(&ipoib_client);
  1740. ib_sa_unregister_client(&ipoib_sa_client);
  1741. ipoib_unregister_debugfs();
  1742. destroy_workqueue(ipoib_workqueue);
  1743. }
  1744. module_init(ipoib_init_module);
  1745. module_exit(ipoib_cleanup_module);