ipoib_main.c 34 KB

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