ipoib_main.c 59 KB

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