ipoib_main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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 netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t 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 neighbour *n, 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. unsigned long flags;
  436. neigh = ipoib_neigh_alloc(n, skb->dev);
  437. if (!neigh) {
  438. ++dev->stats.tx_dropped;
  439. dev_kfree_skb_any(skb);
  440. return;
  441. }
  442. spin_lock_irqsave(&priv->lock, flags);
  443. path = __path_find(dev, n->ha + 4);
  444. if (!path) {
  445. path = path_rec_create(dev, n->ha + 4);
  446. if (!path)
  447. goto err_path;
  448. __path_add(dev, path);
  449. }
  450. list_add_tail(&neigh->list, &path->neigh_list);
  451. if (path->ah) {
  452. kref_get(&path->ah->ref);
  453. neigh->ah = path->ah;
  454. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  455. sizeof(union ib_gid));
  456. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  457. if (!ipoib_cm_get(neigh))
  458. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  459. if (!ipoib_cm_get(neigh)) {
  460. list_del(&neigh->list);
  461. if (neigh->ah)
  462. ipoib_put_ah(neigh->ah);
  463. ipoib_neigh_free(dev, neigh);
  464. goto err_drop;
  465. }
  466. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  467. __skb_queue_tail(&neigh->queue, skb);
  468. else {
  469. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  470. skb_queue_len(&neigh->queue));
  471. goto err_drop;
  472. }
  473. } else {
  474. spin_unlock_irqrestore(&priv->lock, flags);
  475. ipoib_send(dev, skb, path->ah, IPOIB_QPN(n->ha));
  476. return;
  477. }
  478. } else {
  479. neigh->ah = NULL;
  480. if (!path->query && path_rec_start(dev, path))
  481. goto err_list;
  482. __skb_queue_tail(&neigh->queue, skb);
  483. }
  484. spin_unlock_irqrestore(&priv->lock, flags);
  485. return;
  486. err_list:
  487. list_del(&neigh->list);
  488. err_path:
  489. ipoib_neigh_free(dev, neigh);
  490. err_drop:
  491. ++dev->stats.tx_dropped;
  492. dev_kfree_skb_any(skb);
  493. spin_unlock_irqrestore(&priv->lock, flags);
  494. }
  495. /* called with rcu_read_lock */
  496. static void ipoib_path_lookup(struct sk_buff *skb, struct neighbour *n, struct net_device *dev)
  497. {
  498. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  499. /* Look up path record for unicasts */
  500. if (n->ha[4] != 0xff) {
  501. neigh_add_path(skb, n, dev);
  502. return;
  503. }
  504. /* Add in the P_Key for multicasts */
  505. n->ha[8] = (priv->pkey >> 8) & 0xff;
  506. n->ha[9] = priv->pkey & 0xff;
  507. ipoib_mcast_send(dev, n->ha + 4, skb);
  508. }
  509. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  510. struct ipoib_pseudoheader *phdr)
  511. {
  512. struct ipoib_dev_priv *priv = netdev_priv(dev);
  513. struct ipoib_path *path;
  514. unsigned long flags;
  515. spin_lock_irqsave(&priv->lock, flags);
  516. path = __path_find(dev, phdr->hwaddr + 4);
  517. if (!path || !path->valid) {
  518. int new_path = 0;
  519. if (!path) {
  520. path = path_rec_create(dev, phdr->hwaddr + 4);
  521. new_path = 1;
  522. }
  523. if (path) {
  524. /* put pseudoheader back on for next time */
  525. skb_push(skb, sizeof *phdr);
  526. __skb_queue_tail(&path->queue, skb);
  527. if (!path->query && path_rec_start(dev, path)) {
  528. spin_unlock_irqrestore(&priv->lock, flags);
  529. if (new_path)
  530. path_free(dev, path);
  531. return;
  532. } else
  533. __path_add(dev, path);
  534. } else {
  535. ++dev->stats.tx_dropped;
  536. dev_kfree_skb_any(skb);
  537. }
  538. spin_unlock_irqrestore(&priv->lock, flags);
  539. return;
  540. }
  541. if (path->ah) {
  542. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  543. be16_to_cpu(path->pathrec.dlid));
  544. spin_unlock_irqrestore(&priv->lock, flags);
  545. ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
  546. return;
  547. } else if ((path->query || !path_rec_start(dev, path)) &&
  548. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  549. /* put pseudoheader back on for next time */
  550. skb_push(skb, sizeof *phdr);
  551. __skb_queue_tail(&path->queue, skb);
  552. } else {
  553. ++dev->stats.tx_dropped;
  554. dev_kfree_skb_any(skb);
  555. }
  556. spin_unlock_irqrestore(&priv->lock, flags);
  557. }
  558. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  559. {
  560. struct ipoib_dev_priv *priv = netdev_priv(dev);
  561. struct ipoib_neigh *neigh;
  562. struct neighbour *n = NULL;
  563. unsigned long flags;
  564. rcu_read_lock();
  565. if (likely(skb_dst(skb))) {
  566. n = dst_get_neighbour_noref(skb_dst(skb));
  567. if (!n) {
  568. ++dev->stats.tx_dropped;
  569. dev_kfree_skb_any(skb);
  570. goto unlock;
  571. }
  572. }
  573. if (likely(n)) {
  574. if (unlikely(!*to_ipoib_neigh(n))) {
  575. ipoib_path_lookup(skb, n, dev);
  576. goto unlock;
  577. }
  578. neigh = *to_ipoib_neigh(n);
  579. if (unlikely((memcmp(&neigh->dgid.raw,
  580. n->ha + 4,
  581. sizeof(union ib_gid))) ||
  582. (neigh->dev != dev))) {
  583. spin_lock_irqsave(&priv->lock, flags);
  584. /*
  585. * It's safe to call ipoib_put_ah() inside
  586. * priv->lock here, because we know that
  587. * path->ah will always hold one more reference,
  588. * so ipoib_put_ah() will never do more than
  589. * decrement the ref count.
  590. */
  591. if (neigh->ah)
  592. ipoib_put_ah(neigh->ah);
  593. list_del(&neigh->list);
  594. ipoib_neigh_free(dev, neigh);
  595. spin_unlock_irqrestore(&priv->lock, flags);
  596. ipoib_path_lookup(skb, n, dev);
  597. goto unlock;
  598. }
  599. if (ipoib_cm_get(neigh)) {
  600. if (ipoib_cm_up(neigh)) {
  601. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  602. goto unlock;
  603. }
  604. } else if (neigh->ah) {
  605. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(n->ha));
  606. goto unlock;
  607. }
  608. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  609. spin_lock_irqsave(&priv->lock, flags);
  610. __skb_queue_tail(&neigh->queue, skb);
  611. spin_unlock_irqrestore(&priv->lock, flags);
  612. } else {
  613. ++dev->stats.tx_dropped;
  614. dev_kfree_skb_any(skb);
  615. }
  616. } else {
  617. struct ipoib_pseudoheader *phdr =
  618. (struct ipoib_pseudoheader *) skb->data;
  619. skb_pull(skb, sizeof *phdr);
  620. if (phdr->hwaddr[4] == 0xff) {
  621. /* Add in the P_Key for multicast*/
  622. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  623. phdr->hwaddr[9] = priv->pkey & 0xff;
  624. ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
  625. } else {
  626. /* unicast GID -- should be ARP or RARP reply */
  627. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  628. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  629. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x %pI6\n",
  630. skb_dst(skb) ? "neigh" : "dst",
  631. be16_to_cpup((__be16 *) skb->data),
  632. IPOIB_QPN(phdr->hwaddr),
  633. phdr->hwaddr + 4);
  634. dev_kfree_skb_any(skb);
  635. ++dev->stats.tx_dropped;
  636. goto unlock;
  637. }
  638. unicast_arp_send(skb, dev, phdr);
  639. }
  640. }
  641. unlock:
  642. rcu_read_unlock();
  643. return NETDEV_TX_OK;
  644. }
  645. static void ipoib_timeout(struct net_device *dev)
  646. {
  647. struct ipoib_dev_priv *priv = netdev_priv(dev);
  648. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  649. jiffies_to_msecs(jiffies - dev->trans_start));
  650. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  651. netif_queue_stopped(dev),
  652. priv->tx_head, priv->tx_tail);
  653. /* XXX reset QP, etc. */
  654. }
  655. static int ipoib_hard_header(struct sk_buff *skb,
  656. struct net_device *dev,
  657. unsigned short type,
  658. const void *daddr, const void *saddr, unsigned len)
  659. {
  660. struct ipoib_header *header;
  661. struct dst_entry *dst;
  662. struct neighbour *n;
  663. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  664. header->proto = htons(type);
  665. header->reserved = 0;
  666. /*
  667. * If we don't have a neighbour structure, stuff the
  668. * destination address onto the front of the skb so we can
  669. * figure out where to send the packet later.
  670. */
  671. dst = skb_dst(skb);
  672. n = NULL;
  673. if (dst)
  674. n = dst_get_neighbour_noref_raw(dst);
  675. if ((!dst || !n) && daddr) {
  676. struct ipoib_pseudoheader *phdr =
  677. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  678. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  679. }
  680. return 0;
  681. }
  682. static void ipoib_set_mcast_list(struct net_device *dev)
  683. {
  684. struct ipoib_dev_priv *priv = netdev_priv(dev);
  685. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  686. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  687. return;
  688. }
  689. queue_work(ipoib_workqueue, &priv->restart_task);
  690. }
  691. static void ipoib_neigh_cleanup(struct neighbour *n)
  692. {
  693. struct ipoib_neigh *neigh;
  694. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  695. unsigned long flags;
  696. struct ipoib_ah *ah = NULL;
  697. neigh = *to_ipoib_neigh(n);
  698. if (neigh)
  699. priv = netdev_priv(neigh->dev);
  700. else
  701. return;
  702. ipoib_dbg(priv,
  703. "neigh_cleanup for %06x %pI6\n",
  704. IPOIB_QPN(n->ha),
  705. n->ha + 4);
  706. spin_lock_irqsave(&priv->lock, flags);
  707. if (neigh->ah)
  708. ah = neigh->ah;
  709. list_del(&neigh->list);
  710. ipoib_neigh_free(n->dev, neigh);
  711. spin_unlock_irqrestore(&priv->lock, flags);
  712. if (ah)
  713. ipoib_put_ah(ah);
  714. }
  715. struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
  716. struct net_device *dev)
  717. {
  718. struct ipoib_neigh *neigh;
  719. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  720. if (!neigh)
  721. return NULL;
  722. neigh->neighbour = neighbour;
  723. neigh->dev = dev;
  724. memset(&neigh->dgid.raw, 0, sizeof (union ib_gid));
  725. *to_ipoib_neigh(neighbour) = neigh;
  726. skb_queue_head_init(&neigh->queue);
  727. ipoib_cm_set(neigh, NULL);
  728. return neigh;
  729. }
  730. void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
  731. {
  732. struct sk_buff *skb;
  733. *to_ipoib_neigh(neigh->neighbour) = NULL;
  734. while ((skb = __skb_dequeue(&neigh->queue))) {
  735. ++dev->stats.tx_dropped;
  736. dev_kfree_skb_any(skb);
  737. }
  738. if (ipoib_cm_get(neigh))
  739. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  740. kfree(neigh);
  741. }
  742. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  743. {
  744. parms->neigh_cleanup = ipoib_neigh_cleanup;
  745. return 0;
  746. }
  747. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  748. {
  749. struct ipoib_dev_priv *priv = netdev_priv(dev);
  750. /* Allocate RX/TX "rings" to hold queued skbs */
  751. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  752. GFP_KERNEL);
  753. if (!priv->rx_ring) {
  754. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  755. ca->name, ipoib_recvq_size);
  756. goto out;
  757. }
  758. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  759. if (!priv->tx_ring) {
  760. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  761. ca->name, ipoib_sendq_size);
  762. goto out_rx_ring_cleanup;
  763. }
  764. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  765. if (ipoib_ib_dev_init(dev, ca, port))
  766. goto out_tx_ring_cleanup;
  767. return 0;
  768. out_tx_ring_cleanup:
  769. vfree(priv->tx_ring);
  770. out_rx_ring_cleanup:
  771. kfree(priv->rx_ring);
  772. out:
  773. return -ENOMEM;
  774. }
  775. void ipoib_dev_cleanup(struct net_device *dev)
  776. {
  777. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  778. ipoib_delete_debug_files(dev);
  779. /* Delete any child interfaces first */
  780. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  781. unregister_netdev(cpriv->dev);
  782. ipoib_dev_cleanup(cpriv->dev);
  783. free_netdev(cpriv->dev);
  784. }
  785. ipoib_ib_dev_cleanup(dev);
  786. kfree(priv->rx_ring);
  787. vfree(priv->tx_ring);
  788. priv->rx_ring = NULL;
  789. priv->tx_ring = NULL;
  790. }
  791. static const struct header_ops ipoib_header_ops = {
  792. .create = ipoib_hard_header,
  793. };
  794. static const struct net_device_ops ipoib_netdev_ops = {
  795. .ndo_open = ipoib_open,
  796. .ndo_stop = ipoib_stop,
  797. .ndo_change_mtu = ipoib_change_mtu,
  798. .ndo_fix_features = ipoib_fix_features,
  799. .ndo_start_xmit = ipoib_start_xmit,
  800. .ndo_tx_timeout = ipoib_timeout,
  801. .ndo_set_rx_mode = ipoib_set_mcast_list,
  802. .ndo_neigh_setup = ipoib_neigh_setup_dev,
  803. };
  804. static void ipoib_setup(struct net_device *dev)
  805. {
  806. struct ipoib_dev_priv *priv = netdev_priv(dev);
  807. dev->netdev_ops = &ipoib_netdev_ops;
  808. dev->header_ops = &ipoib_header_ops;
  809. ipoib_set_ethtool_ops(dev);
  810. netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
  811. dev->watchdog_timeo = HZ;
  812. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  813. /*
  814. * We add in INFINIBAND_ALEN to allow for the destination
  815. * address "pseudoheader" for skbs without neighbour struct.
  816. */
  817. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  818. dev->addr_len = INFINIBAND_ALEN;
  819. dev->type = ARPHRD_INFINIBAND;
  820. dev->tx_queue_len = ipoib_sendq_size * 2;
  821. dev->features = (NETIF_F_VLAN_CHALLENGED |
  822. NETIF_F_HIGHDMA);
  823. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  824. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  825. netif_carrier_off(dev);
  826. priv->dev = dev;
  827. spin_lock_init(&priv->lock);
  828. mutex_init(&priv->vlan_mutex);
  829. INIT_LIST_HEAD(&priv->path_list);
  830. INIT_LIST_HEAD(&priv->child_intfs);
  831. INIT_LIST_HEAD(&priv->dead_ahs);
  832. INIT_LIST_HEAD(&priv->multicast_list);
  833. INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
  834. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  835. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  836. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  837. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  838. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  839. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  840. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  841. }
  842. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  843. {
  844. struct net_device *dev;
  845. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  846. ipoib_setup);
  847. if (!dev)
  848. return NULL;
  849. return netdev_priv(dev);
  850. }
  851. static ssize_t show_pkey(struct device *dev,
  852. struct device_attribute *attr, char *buf)
  853. {
  854. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  855. return sprintf(buf, "0x%04x\n", priv->pkey);
  856. }
  857. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  858. static ssize_t show_umcast(struct device *dev,
  859. struct device_attribute *attr, char *buf)
  860. {
  861. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  862. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  863. }
  864. static ssize_t set_umcast(struct device *dev,
  865. struct device_attribute *attr,
  866. const char *buf, size_t count)
  867. {
  868. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  869. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  870. if (umcast_val > 0) {
  871. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  872. ipoib_warn(priv, "ignoring multicast groups joined directly "
  873. "by userspace\n");
  874. } else
  875. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  876. return count;
  877. }
  878. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  879. int ipoib_add_umcast_attr(struct net_device *dev)
  880. {
  881. return device_create_file(&dev->dev, &dev_attr_umcast);
  882. }
  883. static ssize_t create_child(struct device *dev,
  884. struct device_attribute *attr,
  885. const char *buf, size_t count)
  886. {
  887. int pkey;
  888. int ret;
  889. if (sscanf(buf, "%i", &pkey) != 1)
  890. return -EINVAL;
  891. if (pkey < 0 || pkey > 0xffff)
  892. return -EINVAL;
  893. /*
  894. * Set the full membership bit, so that we join the right
  895. * broadcast group, etc.
  896. */
  897. pkey |= 0x8000;
  898. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  899. return ret ? ret : count;
  900. }
  901. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  902. static ssize_t delete_child(struct device *dev,
  903. struct device_attribute *attr,
  904. const char *buf, size_t count)
  905. {
  906. int pkey;
  907. int ret;
  908. if (sscanf(buf, "%i", &pkey) != 1)
  909. return -EINVAL;
  910. if (pkey < 0 || pkey > 0xffff)
  911. return -EINVAL;
  912. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  913. return ret ? ret : count;
  914. }
  915. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  916. int ipoib_add_pkey_attr(struct net_device *dev)
  917. {
  918. return device_create_file(&dev->dev, &dev_attr_pkey);
  919. }
  920. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  921. {
  922. struct ib_device_attr *device_attr;
  923. int result = -ENOMEM;
  924. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  925. if (!device_attr) {
  926. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  927. hca->name, sizeof *device_attr);
  928. return result;
  929. }
  930. result = ib_query_device(hca, device_attr);
  931. if (result) {
  932. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  933. hca->name, result);
  934. kfree(device_attr);
  935. return result;
  936. }
  937. priv->hca_caps = device_attr->device_cap_flags;
  938. kfree(device_attr);
  939. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  940. priv->dev->hw_features = NETIF_F_SG |
  941. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  942. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  943. priv->dev->hw_features |= NETIF_F_TSO;
  944. priv->dev->features |= priv->dev->hw_features;
  945. }
  946. return 0;
  947. }
  948. static struct net_device *ipoib_add_port(const char *format,
  949. struct ib_device *hca, u8 port)
  950. {
  951. struct ipoib_dev_priv *priv;
  952. struct ib_port_attr attr;
  953. int result = -ENOMEM;
  954. priv = ipoib_intf_alloc(format);
  955. if (!priv)
  956. goto alloc_mem_failed;
  957. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  958. priv->dev->dev_id = port - 1;
  959. if (!ib_query_port(hca, port, &attr))
  960. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  961. else {
  962. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  963. hca->name, port);
  964. goto device_init_failed;
  965. }
  966. /* MTU will be reset when mcast join happens */
  967. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  968. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  969. priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
  970. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  971. if (result) {
  972. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  973. hca->name, port, result);
  974. goto device_init_failed;
  975. }
  976. if (ipoib_set_dev_features(priv, hca))
  977. goto device_init_failed;
  978. /*
  979. * Set the full membership bit, so that we join the right
  980. * broadcast group, etc.
  981. */
  982. priv->pkey |= 0x8000;
  983. priv->dev->broadcast[8] = priv->pkey >> 8;
  984. priv->dev->broadcast[9] = priv->pkey & 0xff;
  985. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  986. if (result) {
  987. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  988. hca->name, port, result);
  989. goto device_init_failed;
  990. } else
  991. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  992. result = ipoib_dev_init(priv->dev, hca, port);
  993. if (result < 0) {
  994. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  995. hca->name, port, result);
  996. goto device_init_failed;
  997. }
  998. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  999. priv->ca, ipoib_event);
  1000. result = ib_register_event_handler(&priv->event_handler);
  1001. if (result < 0) {
  1002. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1003. "port %d (ret = %d)\n",
  1004. hca->name, port, result);
  1005. goto event_failed;
  1006. }
  1007. result = register_netdev(priv->dev);
  1008. if (result) {
  1009. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1010. hca->name, port, result);
  1011. goto register_failed;
  1012. }
  1013. ipoib_create_debug_files(priv->dev);
  1014. if (ipoib_cm_add_mode_attr(priv->dev))
  1015. goto sysfs_failed;
  1016. if (ipoib_add_pkey_attr(priv->dev))
  1017. goto sysfs_failed;
  1018. if (ipoib_add_umcast_attr(priv->dev))
  1019. goto sysfs_failed;
  1020. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1021. goto sysfs_failed;
  1022. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1023. goto sysfs_failed;
  1024. return priv->dev;
  1025. sysfs_failed:
  1026. ipoib_delete_debug_files(priv->dev);
  1027. unregister_netdev(priv->dev);
  1028. register_failed:
  1029. ib_unregister_event_handler(&priv->event_handler);
  1030. flush_workqueue(ipoib_workqueue);
  1031. event_failed:
  1032. ipoib_dev_cleanup(priv->dev);
  1033. device_init_failed:
  1034. free_netdev(priv->dev);
  1035. alloc_mem_failed:
  1036. return ERR_PTR(result);
  1037. }
  1038. static void ipoib_add_one(struct ib_device *device)
  1039. {
  1040. struct list_head *dev_list;
  1041. struct net_device *dev;
  1042. struct ipoib_dev_priv *priv;
  1043. int s, e, p;
  1044. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1045. return;
  1046. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1047. if (!dev_list)
  1048. return;
  1049. INIT_LIST_HEAD(dev_list);
  1050. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1051. s = 0;
  1052. e = 0;
  1053. } else {
  1054. s = 1;
  1055. e = device->phys_port_cnt;
  1056. }
  1057. for (p = s; p <= e; ++p) {
  1058. if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
  1059. continue;
  1060. dev = ipoib_add_port("ib%d", device, p);
  1061. if (!IS_ERR(dev)) {
  1062. priv = netdev_priv(dev);
  1063. list_add_tail(&priv->list, dev_list);
  1064. }
  1065. }
  1066. ib_set_client_data(device, &ipoib_client, dev_list);
  1067. }
  1068. static void ipoib_remove_one(struct ib_device *device)
  1069. {
  1070. struct ipoib_dev_priv *priv, *tmp;
  1071. struct list_head *dev_list;
  1072. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1073. return;
  1074. dev_list = ib_get_client_data(device, &ipoib_client);
  1075. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1076. ib_unregister_event_handler(&priv->event_handler);
  1077. rtnl_lock();
  1078. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1079. rtnl_unlock();
  1080. flush_workqueue(ipoib_workqueue);
  1081. unregister_netdev(priv->dev);
  1082. ipoib_dev_cleanup(priv->dev);
  1083. free_netdev(priv->dev);
  1084. }
  1085. kfree(dev_list);
  1086. }
  1087. static int __init ipoib_init_module(void)
  1088. {
  1089. int ret;
  1090. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1091. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1092. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1093. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1094. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1095. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1096. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1097. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1098. #endif
  1099. /*
  1100. * When copying small received packets, we only copy from the
  1101. * linear data part of the SKB, so we rely on this condition.
  1102. */
  1103. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1104. ret = ipoib_register_debugfs();
  1105. if (ret)
  1106. return ret;
  1107. /*
  1108. * We create our own workqueue mainly because we want to be
  1109. * able to flush it when devices are being removed. We can't
  1110. * use schedule_work()/flush_scheduled_work() because both
  1111. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1112. * so flush_scheduled_work() can deadlock during device
  1113. * removal.
  1114. */
  1115. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1116. if (!ipoib_workqueue) {
  1117. ret = -ENOMEM;
  1118. goto err_fs;
  1119. }
  1120. ib_sa_register_client(&ipoib_sa_client);
  1121. ret = ib_register_client(&ipoib_client);
  1122. if (ret)
  1123. goto err_sa;
  1124. return 0;
  1125. err_sa:
  1126. ib_sa_unregister_client(&ipoib_sa_client);
  1127. destroy_workqueue(ipoib_workqueue);
  1128. err_fs:
  1129. ipoib_unregister_debugfs();
  1130. return ret;
  1131. }
  1132. static void __exit ipoib_cleanup_module(void)
  1133. {
  1134. ib_unregister_client(&ipoib_client);
  1135. ib_sa_unregister_client(&ipoib_sa_client);
  1136. ipoib_unregister_debugfs();
  1137. destroy_workqueue(ipoib_workqueue);
  1138. }
  1139. module_init(ipoib_init_module);
  1140. module_exit(ipoib_cleanup_module);