ipoib_main.c 33 KB

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