ipoib_main.c 35 KB

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