ipoib_main.c 68 KB

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