ipoib_main.c 64 KB

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