ipoib_main.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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 <net/dst.h>
  44. MODULE_AUTHOR("Roland Dreier");
  45. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  46. MODULE_LICENSE("Dual BSD/GPL");
  47. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  48. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  49. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  50. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  51. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  52. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  53. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  54. int ipoib_debug_level;
  55. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  56. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  57. #endif
  58. struct ipoib_path_iter {
  59. struct net_device *dev;
  60. struct ipoib_path path;
  61. };
  62. static const u8 ipv4_bcast_addr[] = {
  63. 0x00, 0xff, 0xff, 0xff,
  64. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  66. };
  67. struct workqueue_struct *ipoib_workqueue;
  68. struct ib_sa_client ipoib_sa_client;
  69. static void ipoib_add_one(struct ib_device *device);
  70. static void ipoib_remove_one(struct ib_device *device);
  71. static struct ib_client ipoib_client = {
  72. .name = "ipoib",
  73. .add = ipoib_add_one,
  74. .remove = ipoib_remove_one
  75. };
  76. int ipoib_open(struct net_device *dev)
  77. {
  78. struct ipoib_dev_priv *priv = netdev_priv(dev);
  79. ipoib_dbg(priv, "bringing up interface\n");
  80. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  81. if (ipoib_pkey_dev_delay_open(dev))
  82. return 0;
  83. if (ipoib_ib_dev_open(dev))
  84. goto err_disable;
  85. if (ipoib_ib_dev_up(dev))
  86. goto err_stop;
  87. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  88. struct ipoib_dev_priv *cpriv;
  89. /* Bring up any child interfaces too */
  90. mutex_lock(&priv->vlan_mutex);
  91. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  92. int flags;
  93. flags = cpriv->dev->flags;
  94. if (flags & IFF_UP)
  95. continue;
  96. dev_change_flags(cpriv->dev, flags | IFF_UP);
  97. }
  98. mutex_unlock(&priv->vlan_mutex);
  99. }
  100. netif_start_queue(dev);
  101. return 0;
  102. err_stop:
  103. ipoib_ib_dev_stop(dev, 1);
  104. err_disable:
  105. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  106. return -EINVAL;
  107. }
  108. static int ipoib_stop(struct net_device *dev)
  109. {
  110. struct ipoib_dev_priv *priv = netdev_priv(dev);
  111. ipoib_dbg(priv, "stopping interface\n");
  112. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  113. netif_stop_queue(dev);
  114. ipoib_ib_dev_down(dev, 0);
  115. ipoib_ib_dev_stop(dev, 0);
  116. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  117. struct ipoib_dev_priv *cpriv;
  118. /* Bring down any child interfaces too */
  119. mutex_lock(&priv->vlan_mutex);
  120. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  121. int flags;
  122. flags = cpriv->dev->flags;
  123. if (!(flags & IFF_UP))
  124. continue;
  125. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  126. }
  127. mutex_unlock(&priv->vlan_mutex);
  128. }
  129. return 0;
  130. }
  131. static u32 ipoib_fix_features(struct net_device *dev, u32 features)
  132. {
  133. struct ipoib_dev_priv *priv = netdev_priv(dev);
  134. if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
  135. features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
  136. return features;
  137. }
  138. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  139. {
  140. struct ipoib_dev_priv *priv = netdev_priv(dev);
  141. /* dev->mtu > 2K ==> connected mode */
  142. if (ipoib_cm_admin_enabled(dev)) {
  143. if (new_mtu > ipoib_cm_max_mtu(dev))
  144. return -EINVAL;
  145. if (new_mtu > priv->mcast_mtu)
  146. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  147. priv->mcast_mtu);
  148. dev->mtu = new_mtu;
  149. return 0;
  150. }
  151. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  152. return -EINVAL;
  153. priv->admin_mtu = new_mtu;
  154. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  155. return 0;
  156. }
  157. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  158. {
  159. struct ipoib_dev_priv *priv = netdev_priv(dev);
  160. struct rb_node *n = priv->path_tree.rb_node;
  161. struct ipoib_path *path;
  162. int ret;
  163. while (n) {
  164. path = rb_entry(n, struct ipoib_path, rb_node);
  165. ret = memcmp(gid, path->pathrec.dgid.raw,
  166. sizeof (union ib_gid));
  167. if (ret < 0)
  168. n = n->rb_left;
  169. else if (ret > 0)
  170. n = n->rb_right;
  171. else
  172. return path;
  173. }
  174. return NULL;
  175. }
  176. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  177. {
  178. struct ipoib_dev_priv *priv = netdev_priv(dev);
  179. struct rb_node **n = &priv->path_tree.rb_node;
  180. struct rb_node *pn = NULL;
  181. struct ipoib_path *tpath;
  182. int ret;
  183. while (*n) {
  184. pn = *n;
  185. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  186. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  187. sizeof (union ib_gid));
  188. if (ret < 0)
  189. n = &pn->rb_left;
  190. else if (ret > 0)
  191. n = &pn->rb_right;
  192. else
  193. return -EEXIST;
  194. }
  195. rb_link_node(&path->rb_node, pn, n);
  196. rb_insert_color(&path->rb_node, &priv->path_tree);
  197. list_add_tail(&path->list, &priv->path_list);
  198. return 0;
  199. }
  200. static void path_free(struct net_device *dev, struct ipoib_path *path)
  201. {
  202. struct ipoib_dev_priv *priv = netdev_priv(dev);
  203. struct ipoib_neigh *neigh, *tn;
  204. struct sk_buff *skb;
  205. unsigned long flags;
  206. while ((skb = __skb_dequeue(&path->queue)))
  207. dev_kfree_skb_irq(skb);
  208. spin_lock_irqsave(&priv->lock, flags);
  209. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  210. /*
  211. * It's safe to call ipoib_put_ah() inside priv->lock
  212. * here, because we know that path->ah will always
  213. * hold one more reference, so ipoib_put_ah() will
  214. * never do more than decrement the ref count.
  215. */
  216. if (neigh->ah)
  217. ipoib_put_ah(neigh->ah);
  218. ipoib_neigh_free(dev, neigh);
  219. }
  220. spin_unlock_irqrestore(&priv->lock, flags);
  221. if (path->ah)
  222. ipoib_put_ah(path->ah);
  223. kfree(path);
  224. }
  225. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  226. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  227. {
  228. struct ipoib_path_iter *iter;
  229. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  230. if (!iter)
  231. return NULL;
  232. iter->dev = dev;
  233. memset(iter->path.pathrec.dgid.raw, 0, 16);
  234. if (ipoib_path_iter_next(iter)) {
  235. kfree(iter);
  236. return NULL;
  237. }
  238. return iter;
  239. }
  240. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  241. {
  242. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  243. struct rb_node *n;
  244. struct ipoib_path *path;
  245. int ret = 1;
  246. spin_lock_irq(&priv->lock);
  247. n = rb_first(&priv->path_tree);
  248. while (n) {
  249. path = rb_entry(n, struct ipoib_path, rb_node);
  250. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  251. sizeof (union ib_gid)) < 0) {
  252. iter->path = *path;
  253. ret = 0;
  254. break;
  255. }
  256. n = rb_next(n);
  257. }
  258. spin_unlock_irq(&priv->lock);
  259. return ret;
  260. }
  261. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  262. struct ipoib_path *path)
  263. {
  264. *path = iter->path;
  265. }
  266. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  267. void ipoib_mark_paths_invalid(struct net_device *dev)
  268. {
  269. struct ipoib_dev_priv *priv = netdev_priv(dev);
  270. struct ipoib_path *path, *tp;
  271. spin_lock_irq(&priv->lock);
  272. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  273. ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
  274. be16_to_cpu(path->pathrec.dlid),
  275. path->pathrec.dgid.raw);
  276. path->valid = 0;
  277. }
  278. spin_unlock_irq(&priv->lock);
  279. }
  280. void ipoib_flush_paths(struct net_device *dev)
  281. {
  282. struct ipoib_dev_priv *priv = netdev_priv(dev);
  283. struct ipoib_path *path, *tp;
  284. LIST_HEAD(remove_list);
  285. unsigned long flags;
  286. netif_tx_lock_bh(dev);
  287. spin_lock_irqsave(&priv->lock, flags);
  288. list_splice_init(&priv->path_list, &remove_list);
  289. list_for_each_entry(path, &remove_list, list)
  290. rb_erase(&path->rb_node, &priv->path_tree);
  291. list_for_each_entry_safe(path, tp, &remove_list, list) {
  292. if (path->query)
  293. ib_sa_cancel_query(path->query_id, path->query);
  294. spin_unlock_irqrestore(&priv->lock, flags);
  295. netif_tx_unlock_bh(dev);
  296. wait_for_completion(&path->done);
  297. path_free(dev, path);
  298. netif_tx_lock_bh(dev);
  299. spin_lock_irqsave(&priv->lock, flags);
  300. }
  301. spin_unlock_irqrestore(&priv->lock, flags);
  302. netif_tx_unlock_bh(dev);
  303. }
  304. static void path_rec_completion(int status,
  305. struct ib_sa_path_rec *pathrec,
  306. void *path_ptr)
  307. {
  308. struct ipoib_path *path = path_ptr;
  309. struct net_device *dev = path->dev;
  310. struct ipoib_dev_priv *priv = netdev_priv(dev);
  311. struct ipoib_ah *ah = NULL;
  312. struct ipoib_ah *old_ah = NULL;
  313. struct ipoib_neigh *neigh, *tn;
  314. struct sk_buff_head skqueue;
  315. struct sk_buff *skb;
  316. unsigned long flags;
  317. if (!status)
  318. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
  319. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  320. else
  321. ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
  322. status, path->pathrec.dgid.raw);
  323. skb_queue_head_init(&skqueue);
  324. if (!status) {
  325. struct ib_ah_attr av;
  326. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  327. ah = ipoib_create_ah(dev, priv->pd, &av);
  328. }
  329. spin_lock_irqsave(&priv->lock, flags);
  330. if (!IS_ERR_OR_NULL(ah)) {
  331. path->pathrec = *pathrec;
  332. old_ah = path->ah;
  333. path->ah = ah;
  334. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  335. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  336. while ((skb = __skb_dequeue(&path->queue)))
  337. __skb_queue_tail(&skqueue, skb);
  338. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  339. if (neigh->ah) {
  340. WARN_ON(neigh->ah != old_ah);
  341. /*
  342. * Dropping the ah reference inside
  343. * priv->lock is safe here, because we
  344. * will hold one more reference from
  345. * the original value of path->ah (ie
  346. * old_ah).
  347. */
  348. ipoib_put_ah(neigh->ah);
  349. }
  350. kref_get(&path->ah->ref);
  351. neigh->ah = path->ah;
  352. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  353. sizeof(union ib_gid));
  354. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  355. if (!ipoib_cm_get(neigh))
  356. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  357. path,
  358. neigh));
  359. if (!ipoib_cm_get(neigh)) {
  360. list_del(&neigh->list);
  361. if (neigh->ah)
  362. ipoib_put_ah(neigh->ah);
  363. ipoib_neigh_free(dev, neigh);
  364. continue;
  365. }
  366. }
  367. while ((skb = __skb_dequeue(&neigh->queue)))
  368. __skb_queue_tail(&skqueue, skb);
  369. }
  370. path->valid = 1;
  371. }
  372. path->query = NULL;
  373. complete(&path->done);
  374. spin_unlock_irqrestore(&priv->lock, flags);
  375. if (old_ah)
  376. ipoib_put_ah(old_ah);
  377. while ((skb = __skb_dequeue(&skqueue))) {
  378. skb->dev = dev;
  379. if (dev_queue_xmit(skb))
  380. ipoib_warn(priv, "dev_queue_xmit failed "
  381. "to requeue packet\n");
  382. }
  383. }
  384. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  385. {
  386. struct ipoib_dev_priv *priv = netdev_priv(dev);
  387. struct ipoib_path *path;
  388. if (!priv->broadcast)
  389. return NULL;
  390. path = kzalloc(sizeof *path, GFP_ATOMIC);
  391. if (!path)
  392. return NULL;
  393. path->dev = dev;
  394. skb_queue_head_init(&path->queue);
  395. INIT_LIST_HEAD(&path->neigh_list);
  396. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  397. path->pathrec.sgid = priv->local_gid;
  398. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  399. path->pathrec.numb_path = 1;
  400. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  401. return path;
  402. }
  403. static int path_rec_start(struct net_device *dev,
  404. struct ipoib_path *path)
  405. {
  406. struct ipoib_dev_priv *priv = netdev_priv(dev);
  407. ipoib_dbg(priv, "Start path record lookup for %pI6\n",
  408. path->pathrec.dgid.raw);
  409. init_completion(&path->done);
  410. path->query_id =
  411. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  412. &path->pathrec,
  413. IB_SA_PATH_REC_DGID |
  414. IB_SA_PATH_REC_SGID |
  415. IB_SA_PATH_REC_NUMB_PATH |
  416. IB_SA_PATH_REC_TRAFFIC_CLASS |
  417. IB_SA_PATH_REC_PKEY,
  418. 1000, GFP_ATOMIC,
  419. path_rec_completion,
  420. path, &path->query);
  421. if (path->query_id < 0) {
  422. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  423. path->query = NULL;
  424. complete(&path->done);
  425. return path->query_id;
  426. }
  427. return 0;
  428. }
  429. /* called with rcu_read_lock */
  430. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  431. {
  432. struct ipoib_dev_priv *priv = netdev_priv(dev);
  433. struct ipoib_path *path;
  434. struct ipoib_neigh *neigh;
  435. struct neighbour *n;
  436. unsigned long flags;
  437. n = dst_get_neighbour(skb_dst(skb));
  438. neigh = ipoib_neigh_alloc(n, skb->dev);
  439. if (!neigh) {
  440. ++dev->stats.tx_dropped;
  441. dev_kfree_skb_any(skb);
  442. return;
  443. }
  444. spin_lock_irqsave(&priv->lock, flags);
  445. path = __path_find(dev, n->ha + 4);
  446. if (!path) {
  447. path = path_rec_create(dev, n->ha + 4);
  448. if (!path)
  449. goto err_path;
  450. __path_add(dev, path);
  451. }
  452. list_add_tail(&neigh->list, &path->neigh_list);
  453. if (path->ah) {
  454. kref_get(&path->ah->ref);
  455. neigh->ah = path->ah;
  456. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  457. sizeof(union ib_gid));
  458. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  459. if (!ipoib_cm_get(neigh))
  460. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  461. if (!ipoib_cm_get(neigh)) {
  462. list_del(&neigh->list);
  463. if (neigh->ah)
  464. ipoib_put_ah(neigh->ah);
  465. ipoib_neigh_free(dev, neigh);
  466. goto err_drop;
  467. }
  468. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  469. __skb_queue_tail(&neigh->queue, skb);
  470. else {
  471. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  472. skb_queue_len(&neigh->queue));
  473. goto err_drop;
  474. }
  475. } else {
  476. spin_unlock_irqrestore(&priv->lock, flags);
  477. ipoib_send(dev, skb, path->ah, IPOIB_QPN(n->ha));
  478. return;
  479. }
  480. } else {
  481. neigh->ah = NULL;
  482. if (!path->query && path_rec_start(dev, path))
  483. goto err_list;
  484. __skb_queue_tail(&neigh->queue, skb);
  485. }
  486. spin_unlock_irqrestore(&priv->lock, flags);
  487. return;
  488. err_list:
  489. list_del(&neigh->list);
  490. err_path:
  491. ipoib_neigh_free(dev, neigh);
  492. err_drop:
  493. ++dev->stats.tx_dropped;
  494. dev_kfree_skb_any(skb);
  495. spin_unlock_irqrestore(&priv->lock, flags);
  496. }
  497. /* called with rcu_read_lock */
  498. static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
  499. {
  500. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  501. struct dst_entry *dst = skb_dst(skb);
  502. struct neighbour *n;
  503. /* Look up path record for unicasts */
  504. n = dst_get_neighbour(dst);
  505. if (n->ha[4] != 0xff) {
  506. neigh_add_path(skb, dev);
  507. return;
  508. }
  509. /* Add in the P_Key for multicasts */
  510. n->ha[8] = (priv->pkey >> 8) & 0xff;
  511. n->ha[9] = priv->pkey & 0xff;
  512. ipoib_mcast_send(dev, n->ha + 4, skb);
  513. }
  514. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  515. struct ipoib_pseudoheader *phdr)
  516. {
  517. struct ipoib_dev_priv *priv = netdev_priv(dev);
  518. struct ipoib_path *path;
  519. unsigned long flags;
  520. spin_lock_irqsave(&priv->lock, flags);
  521. path = __path_find(dev, phdr->hwaddr + 4);
  522. if (!path || !path->valid) {
  523. int new_path = 0;
  524. if (!path) {
  525. path = path_rec_create(dev, phdr->hwaddr + 4);
  526. new_path = 1;
  527. }
  528. if (path) {
  529. /* put pseudoheader back on for next time */
  530. skb_push(skb, sizeof *phdr);
  531. __skb_queue_tail(&path->queue, skb);
  532. if (!path->query && path_rec_start(dev, path)) {
  533. spin_unlock_irqrestore(&priv->lock, flags);
  534. if (new_path)
  535. path_free(dev, path);
  536. return;
  537. } else
  538. __path_add(dev, path);
  539. } else {
  540. ++dev->stats.tx_dropped;
  541. dev_kfree_skb_any(skb);
  542. }
  543. spin_unlock_irqrestore(&priv->lock, flags);
  544. return;
  545. }
  546. if (path->ah) {
  547. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  548. be16_to_cpu(path->pathrec.dlid));
  549. spin_unlock_irqrestore(&priv->lock, flags);
  550. ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
  551. return;
  552. } else if ((path->query || !path_rec_start(dev, path)) &&
  553. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  554. /* put pseudoheader back on for next time */
  555. skb_push(skb, sizeof *phdr);
  556. __skb_queue_tail(&path->queue, skb);
  557. } else {
  558. ++dev->stats.tx_dropped;
  559. dev_kfree_skb_any(skb);
  560. }
  561. spin_unlock_irqrestore(&priv->lock, flags);
  562. }
  563. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  564. {
  565. struct ipoib_dev_priv *priv = netdev_priv(dev);
  566. struct ipoib_neigh *neigh;
  567. struct neighbour *n = NULL;
  568. unsigned long flags;
  569. rcu_read_lock();
  570. if (likely(skb_dst(skb)))
  571. n = dst_get_neighbour(skb_dst(skb));
  572. if (likely(n)) {
  573. if (unlikely(!*to_ipoib_neigh(n))) {
  574. ipoib_path_lookup(skb, dev);
  575. goto unlock;
  576. }
  577. neigh = *to_ipoib_neigh(n);
  578. if (unlikely((memcmp(&neigh->dgid.raw,
  579. n->ha + 4,
  580. sizeof(union ib_gid))) ||
  581. (neigh->dev != dev))) {
  582. spin_lock_irqsave(&priv->lock, flags);
  583. /*
  584. * It's safe to call ipoib_put_ah() inside
  585. * priv->lock here, because we know that
  586. * path->ah will always hold one more reference,
  587. * so ipoib_put_ah() will never do more than
  588. * decrement the ref count.
  589. */
  590. if (neigh->ah)
  591. ipoib_put_ah(neigh->ah);
  592. list_del(&neigh->list);
  593. ipoib_neigh_free(dev, neigh);
  594. spin_unlock_irqrestore(&priv->lock, flags);
  595. ipoib_path_lookup(skb, dev);
  596. goto unlock;
  597. }
  598. if (ipoib_cm_get(neigh)) {
  599. if (ipoib_cm_up(neigh)) {
  600. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  601. goto unlock;
  602. }
  603. } else if (neigh->ah) {
  604. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(n->ha));
  605. goto unlock;
  606. }
  607. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  608. spin_lock_irqsave(&priv->lock, flags);
  609. __skb_queue_tail(&neigh->queue, skb);
  610. spin_unlock_irqrestore(&priv->lock, flags);
  611. } else {
  612. ++dev->stats.tx_dropped;
  613. dev_kfree_skb_any(skb);
  614. }
  615. } else {
  616. struct ipoib_pseudoheader *phdr =
  617. (struct ipoib_pseudoheader *) skb->data;
  618. skb_pull(skb, sizeof *phdr);
  619. if (phdr->hwaddr[4] == 0xff) {
  620. /* Add in the P_Key for multicast*/
  621. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  622. phdr->hwaddr[9] = priv->pkey & 0xff;
  623. ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
  624. } else {
  625. /* unicast GID -- should be ARP or RARP reply */
  626. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  627. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  628. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x %pI6\n",
  629. skb_dst(skb) ? "neigh" : "dst",
  630. be16_to_cpup((__be16 *) skb->data),
  631. IPOIB_QPN(phdr->hwaddr),
  632. phdr->hwaddr + 4);
  633. dev_kfree_skb_any(skb);
  634. ++dev->stats.tx_dropped;
  635. goto unlock;
  636. }
  637. unicast_arp_send(skb, dev, phdr);
  638. }
  639. }
  640. unlock:
  641. rcu_read_unlock();
  642. return NETDEV_TX_OK;
  643. }
  644. static void ipoib_timeout(struct net_device *dev)
  645. {
  646. struct ipoib_dev_priv *priv = netdev_priv(dev);
  647. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  648. jiffies_to_msecs(jiffies - dev->trans_start));
  649. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  650. netif_queue_stopped(dev),
  651. priv->tx_head, priv->tx_tail);
  652. /* XXX reset QP, etc. */
  653. }
  654. static int ipoib_hard_header(struct sk_buff *skb,
  655. struct net_device *dev,
  656. unsigned short type,
  657. const void *daddr, const void *saddr, unsigned len)
  658. {
  659. struct ipoib_header *header;
  660. struct dst_entry *dst;
  661. struct neighbour *n;
  662. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  663. header->proto = htons(type);
  664. header->reserved = 0;
  665. /*
  666. * If we don't have a neighbour structure, stuff the
  667. * destination address onto the front of the skb so we can
  668. * figure out where to send the packet later.
  669. */
  670. dst = skb_dst(skb);
  671. n = NULL;
  672. if (dst)
  673. n = dst_get_neighbour_raw(dst);
  674. if ((!dst || !n) && daddr) {
  675. struct ipoib_pseudoheader *phdr =
  676. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  677. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  678. }
  679. return 0;
  680. }
  681. static void ipoib_set_mcast_list(struct net_device *dev)
  682. {
  683. struct ipoib_dev_priv *priv = netdev_priv(dev);
  684. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  685. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  686. return;
  687. }
  688. queue_work(ipoib_workqueue, &priv->restart_task);
  689. }
  690. static void ipoib_neigh_cleanup(struct neighbour *n)
  691. {
  692. struct ipoib_neigh *neigh;
  693. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  694. unsigned long flags;
  695. struct ipoib_ah *ah = NULL;
  696. neigh = *to_ipoib_neigh(n);
  697. if (neigh)
  698. priv = netdev_priv(neigh->dev);
  699. else
  700. return;
  701. ipoib_dbg(priv,
  702. "neigh_cleanup for %06x %pI6\n",
  703. IPOIB_QPN(n->ha),
  704. n->ha + 4);
  705. spin_lock_irqsave(&priv->lock, flags);
  706. if (neigh->ah)
  707. ah = neigh->ah;
  708. list_del(&neigh->list);
  709. ipoib_neigh_free(n->dev, neigh);
  710. spin_unlock_irqrestore(&priv->lock, flags);
  711. if (ah)
  712. ipoib_put_ah(ah);
  713. }
  714. struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
  715. struct net_device *dev)
  716. {
  717. struct ipoib_neigh *neigh;
  718. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  719. if (!neigh)
  720. return NULL;
  721. neigh->neighbour = neighbour;
  722. neigh->dev = dev;
  723. memset(&neigh->dgid.raw, 0, sizeof (union ib_gid));
  724. *to_ipoib_neigh(neighbour) = neigh;
  725. skb_queue_head_init(&neigh->queue);
  726. ipoib_cm_set(neigh, NULL);
  727. return neigh;
  728. }
  729. void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
  730. {
  731. struct sk_buff *skb;
  732. *to_ipoib_neigh(neigh->neighbour) = NULL;
  733. while ((skb = __skb_dequeue(&neigh->queue))) {
  734. ++dev->stats.tx_dropped;
  735. dev_kfree_skb_any(skb);
  736. }
  737. if (ipoib_cm_get(neigh))
  738. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  739. kfree(neigh);
  740. }
  741. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  742. {
  743. parms->neigh_cleanup = ipoib_neigh_cleanup;
  744. return 0;
  745. }
  746. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  747. {
  748. struct ipoib_dev_priv *priv = netdev_priv(dev);
  749. /* Allocate RX/TX "rings" to hold queued skbs */
  750. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  751. GFP_KERNEL);
  752. if (!priv->rx_ring) {
  753. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  754. ca->name, ipoib_recvq_size);
  755. goto out;
  756. }
  757. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  758. if (!priv->tx_ring) {
  759. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  760. ca->name, ipoib_sendq_size);
  761. goto out_rx_ring_cleanup;
  762. }
  763. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  764. if (ipoib_ib_dev_init(dev, ca, port))
  765. goto out_tx_ring_cleanup;
  766. return 0;
  767. out_tx_ring_cleanup:
  768. vfree(priv->tx_ring);
  769. out_rx_ring_cleanup:
  770. kfree(priv->rx_ring);
  771. out:
  772. return -ENOMEM;
  773. }
  774. void ipoib_dev_cleanup(struct net_device *dev)
  775. {
  776. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  777. ipoib_delete_debug_files(dev);
  778. /* Delete any child interfaces first */
  779. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  780. unregister_netdev(cpriv->dev);
  781. ipoib_dev_cleanup(cpriv->dev);
  782. free_netdev(cpriv->dev);
  783. }
  784. ipoib_ib_dev_cleanup(dev);
  785. kfree(priv->rx_ring);
  786. vfree(priv->tx_ring);
  787. priv->rx_ring = NULL;
  788. priv->tx_ring = NULL;
  789. }
  790. static const struct header_ops ipoib_header_ops = {
  791. .create = ipoib_hard_header,
  792. };
  793. static const struct net_device_ops ipoib_netdev_ops = {
  794. .ndo_open = ipoib_open,
  795. .ndo_stop = ipoib_stop,
  796. .ndo_change_mtu = ipoib_change_mtu,
  797. .ndo_fix_features = ipoib_fix_features,
  798. .ndo_start_xmit = ipoib_start_xmit,
  799. .ndo_tx_timeout = ipoib_timeout,
  800. .ndo_set_rx_mode = ipoib_set_mcast_list,
  801. .ndo_neigh_setup = ipoib_neigh_setup_dev,
  802. };
  803. static void ipoib_setup(struct net_device *dev)
  804. {
  805. struct ipoib_dev_priv *priv = netdev_priv(dev);
  806. dev->netdev_ops = &ipoib_netdev_ops;
  807. dev->header_ops = &ipoib_header_ops;
  808. ipoib_set_ethtool_ops(dev);
  809. netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
  810. dev->watchdog_timeo = HZ;
  811. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  812. /*
  813. * We add in INFINIBAND_ALEN to allow for the destination
  814. * address "pseudoheader" for skbs without neighbour struct.
  815. */
  816. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  817. dev->addr_len = INFINIBAND_ALEN;
  818. dev->type = ARPHRD_INFINIBAND;
  819. dev->tx_queue_len = ipoib_sendq_size * 2;
  820. dev->features = (NETIF_F_VLAN_CHALLENGED |
  821. NETIF_F_HIGHDMA);
  822. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  823. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  824. netif_carrier_off(dev);
  825. priv->dev = dev;
  826. spin_lock_init(&priv->lock);
  827. mutex_init(&priv->vlan_mutex);
  828. INIT_LIST_HEAD(&priv->path_list);
  829. INIT_LIST_HEAD(&priv->child_intfs);
  830. INIT_LIST_HEAD(&priv->dead_ahs);
  831. INIT_LIST_HEAD(&priv->multicast_list);
  832. INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
  833. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  834. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  835. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  836. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  837. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  838. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  839. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  840. }
  841. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  842. {
  843. struct net_device *dev;
  844. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  845. ipoib_setup);
  846. if (!dev)
  847. return NULL;
  848. return netdev_priv(dev);
  849. }
  850. static ssize_t show_pkey(struct device *dev,
  851. struct device_attribute *attr, char *buf)
  852. {
  853. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  854. return sprintf(buf, "0x%04x\n", priv->pkey);
  855. }
  856. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  857. static ssize_t show_umcast(struct device *dev,
  858. struct device_attribute *attr, char *buf)
  859. {
  860. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  861. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  862. }
  863. static ssize_t set_umcast(struct device *dev,
  864. struct device_attribute *attr,
  865. const char *buf, size_t count)
  866. {
  867. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  868. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  869. if (umcast_val > 0) {
  870. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  871. ipoib_warn(priv, "ignoring multicast groups joined directly "
  872. "by userspace\n");
  873. } else
  874. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  875. return count;
  876. }
  877. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  878. int ipoib_add_umcast_attr(struct net_device *dev)
  879. {
  880. return device_create_file(&dev->dev, &dev_attr_umcast);
  881. }
  882. static ssize_t create_child(struct device *dev,
  883. struct device_attribute *attr,
  884. const char *buf, size_t count)
  885. {
  886. int pkey;
  887. int ret;
  888. if (sscanf(buf, "%i", &pkey) != 1)
  889. return -EINVAL;
  890. if (pkey < 0 || pkey > 0xffff)
  891. return -EINVAL;
  892. /*
  893. * Set the full membership bit, so that we join the right
  894. * broadcast group, etc.
  895. */
  896. pkey |= 0x8000;
  897. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  898. return ret ? ret : count;
  899. }
  900. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  901. static ssize_t delete_child(struct device *dev,
  902. struct device_attribute *attr,
  903. const char *buf, size_t count)
  904. {
  905. int pkey;
  906. int ret;
  907. if (sscanf(buf, "%i", &pkey) != 1)
  908. return -EINVAL;
  909. if (pkey < 0 || pkey > 0xffff)
  910. return -EINVAL;
  911. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  912. return ret ? ret : count;
  913. }
  914. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  915. int ipoib_add_pkey_attr(struct net_device *dev)
  916. {
  917. return device_create_file(&dev->dev, &dev_attr_pkey);
  918. }
  919. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  920. {
  921. struct ib_device_attr *device_attr;
  922. int result = -ENOMEM;
  923. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  924. if (!device_attr) {
  925. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  926. hca->name, sizeof *device_attr);
  927. return result;
  928. }
  929. result = ib_query_device(hca, device_attr);
  930. if (result) {
  931. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  932. hca->name, result);
  933. kfree(device_attr);
  934. return result;
  935. }
  936. priv->hca_caps = device_attr->device_cap_flags;
  937. kfree(device_attr);
  938. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  939. priv->dev->hw_features = NETIF_F_SG |
  940. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  941. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  942. priv->dev->hw_features |= NETIF_F_TSO;
  943. priv->dev->features |= priv->dev->hw_features;
  944. }
  945. return 0;
  946. }
  947. static struct net_device *ipoib_add_port(const char *format,
  948. struct ib_device *hca, u8 port)
  949. {
  950. struct ipoib_dev_priv *priv;
  951. struct ib_port_attr attr;
  952. int result = -ENOMEM;
  953. priv = ipoib_intf_alloc(format);
  954. if (!priv)
  955. goto alloc_mem_failed;
  956. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  957. priv->dev->dev_id = port - 1;
  958. if (!ib_query_port(hca, port, &attr))
  959. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  960. else {
  961. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  962. hca->name, port);
  963. goto device_init_failed;
  964. }
  965. /* MTU will be reset when mcast join happens */
  966. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  967. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  968. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  969. if (result) {
  970. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  971. hca->name, port, result);
  972. goto device_init_failed;
  973. }
  974. if (ipoib_set_dev_features(priv, hca))
  975. goto device_init_failed;
  976. /*
  977. * Set the full membership bit, so that we join the right
  978. * broadcast group, etc.
  979. */
  980. priv->pkey |= 0x8000;
  981. priv->dev->broadcast[8] = priv->pkey >> 8;
  982. priv->dev->broadcast[9] = priv->pkey & 0xff;
  983. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  984. if (result) {
  985. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  986. hca->name, port, result);
  987. goto device_init_failed;
  988. } else
  989. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  990. result = ipoib_dev_init(priv->dev, hca, port);
  991. if (result < 0) {
  992. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  993. hca->name, port, result);
  994. goto device_init_failed;
  995. }
  996. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  997. priv->ca, ipoib_event);
  998. result = ib_register_event_handler(&priv->event_handler);
  999. if (result < 0) {
  1000. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1001. "port %d (ret = %d)\n",
  1002. hca->name, port, result);
  1003. goto event_failed;
  1004. }
  1005. result = register_netdev(priv->dev);
  1006. if (result) {
  1007. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1008. hca->name, port, result);
  1009. goto register_failed;
  1010. }
  1011. ipoib_create_debug_files(priv->dev);
  1012. if (ipoib_cm_add_mode_attr(priv->dev))
  1013. goto sysfs_failed;
  1014. if (ipoib_add_pkey_attr(priv->dev))
  1015. goto sysfs_failed;
  1016. if (ipoib_add_umcast_attr(priv->dev))
  1017. goto sysfs_failed;
  1018. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1019. goto sysfs_failed;
  1020. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1021. goto sysfs_failed;
  1022. return priv->dev;
  1023. sysfs_failed:
  1024. ipoib_delete_debug_files(priv->dev);
  1025. unregister_netdev(priv->dev);
  1026. register_failed:
  1027. ib_unregister_event_handler(&priv->event_handler);
  1028. flush_workqueue(ipoib_workqueue);
  1029. event_failed:
  1030. ipoib_dev_cleanup(priv->dev);
  1031. device_init_failed:
  1032. free_netdev(priv->dev);
  1033. alloc_mem_failed:
  1034. return ERR_PTR(result);
  1035. }
  1036. static void ipoib_add_one(struct ib_device *device)
  1037. {
  1038. struct list_head *dev_list;
  1039. struct net_device *dev;
  1040. struct ipoib_dev_priv *priv;
  1041. int s, e, p;
  1042. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1043. return;
  1044. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1045. if (!dev_list)
  1046. return;
  1047. INIT_LIST_HEAD(dev_list);
  1048. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1049. s = 0;
  1050. e = 0;
  1051. } else {
  1052. s = 1;
  1053. e = device->phys_port_cnt;
  1054. }
  1055. for (p = s; p <= e; ++p) {
  1056. if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
  1057. continue;
  1058. dev = ipoib_add_port("ib%d", device, p);
  1059. if (!IS_ERR(dev)) {
  1060. priv = netdev_priv(dev);
  1061. list_add_tail(&priv->list, dev_list);
  1062. }
  1063. }
  1064. ib_set_client_data(device, &ipoib_client, dev_list);
  1065. }
  1066. static void ipoib_remove_one(struct ib_device *device)
  1067. {
  1068. struct ipoib_dev_priv *priv, *tmp;
  1069. struct list_head *dev_list;
  1070. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1071. return;
  1072. dev_list = ib_get_client_data(device, &ipoib_client);
  1073. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1074. ib_unregister_event_handler(&priv->event_handler);
  1075. rtnl_lock();
  1076. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1077. rtnl_unlock();
  1078. flush_workqueue(ipoib_workqueue);
  1079. unregister_netdev(priv->dev);
  1080. ipoib_dev_cleanup(priv->dev);
  1081. free_netdev(priv->dev);
  1082. }
  1083. kfree(dev_list);
  1084. }
  1085. static int __init ipoib_init_module(void)
  1086. {
  1087. int ret;
  1088. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1089. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1090. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1091. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1092. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1093. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1094. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1095. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1096. #endif
  1097. /*
  1098. * When copying small received packets, we only copy from the
  1099. * linear data part of the SKB, so we rely on this condition.
  1100. */
  1101. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1102. ret = ipoib_register_debugfs();
  1103. if (ret)
  1104. return ret;
  1105. /*
  1106. * We create our own workqueue mainly because we want to be
  1107. * able to flush it when devices are being removed. We can't
  1108. * use schedule_work()/flush_scheduled_work() because both
  1109. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1110. * so flush_scheduled_work() can deadlock during device
  1111. * removal.
  1112. */
  1113. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1114. if (!ipoib_workqueue) {
  1115. ret = -ENOMEM;
  1116. goto err_fs;
  1117. }
  1118. ib_sa_register_client(&ipoib_sa_client);
  1119. ret = ib_register_client(&ipoib_client);
  1120. if (ret)
  1121. goto err_sa;
  1122. return 0;
  1123. err_sa:
  1124. ib_sa_unregister_client(&ipoib_sa_client);
  1125. destroy_workqueue(ipoib_workqueue);
  1126. err_fs:
  1127. ipoib_unregister_debugfs();
  1128. return ret;
  1129. }
  1130. static void __exit ipoib_cleanup_module(void)
  1131. {
  1132. ib_unregister_client(&ipoib_client);
  1133. ib_sa_unregister_client(&ipoib_sa_client);
  1134. ipoib_unregister_debugfs();
  1135. destroy_workqueue(ipoib_workqueue);
  1136. }
  1137. module_init(ipoib_init_module);
  1138. module_exit(ipoib_cleanup_module);