ipoib_main.c 35 KB

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