ipoib_main.c 57 KB

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