ipoib_main.c 62 KB

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