ipoib_main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. * $Id: ipoib_main.c 1377 2004-12-23 19:57:12Z roland $
  35. */
  36. #include "ipoib.h"
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  42. #include <linux/ip.h>
  43. #include <linux/in.h>
  44. #include <net/dst.h>
  45. MODULE_AUTHOR("Roland Dreier");
  46. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  49. int ipoib_debug_level;
  50. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  51. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  52. #endif
  53. struct ipoib_path_iter {
  54. struct net_device *dev;
  55. struct ipoib_path path;
  56. };
  57. static const u8 ipv4_bcast_addr[] = {
  58. 0x00, 0xff, 0xff, 0xff,
  59. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  61. };
  62. struct workqueue_struct *ipoib_workqueue;
  63. static void ipoib_add_one(struct ib_device *device);
  64. static void ipoib_remove_one(struct ib_device *device);
  65. static struct ib_client ipoib_client = {
  66. .name = "ipoib",
  67. .add = ipoib_add_one,
  68. .remove = ipoib_remove_one
  69. };
  70. int ipoib_open(struct net_device *dev)
  71. {
  72. struct ipoib_dev_priv *priv = netdev_priv(dev);
  73. ipoib_dbg(priv, "bringing up interface\n");
  74. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  75. if (ipoib_pkey_dev_delay_open(dev))
  76. return 0;
  77. if (ipoib_ib_dev_open(dev))
  78. return -EINVAL;
  79. if (ipoib_ib_dev_up(dev)) {
  80. ipoib_ib_dev_stop(dev);
  81. return -EINVAL;
  82. }
  83. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  84. struct ipoib_dev_priv *cpriv;
  85. /* Bring up any child interfaces too */
  86. mutex_lock(&priv->vlan_mutex);
  87. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  88. int flags;
  89. flags = cpriv->dev->flags;
  90. if (flags & IFF_UP)
  91. continue;
  92. dev_change_flags(cpriv->dev, flags | IFF_UP);
  93. }
  94. mutex_unlock(&priv->vlan_mutex);
  95. }
  96. netif_start_queue(dev);
  97. return 0;
  98. }
  99. static int ipoib_stop(struct net_device *dev)
  100. {
  101. struct ipoib_dev_priv *priv = netdev_priv(dev);
  102. ipoib_dbg(priv, "stopping interface\n");
  103. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  104. netif_stop_queue(dev);
  105. ipoib_ib_dev_down(dev);
  106. ipoib_ib_dev_stop(dev);
  107. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  108. struct ipoib_dev_priv *cpriv;
  109. /* Bring down any child interfaces too */
  110. mutex_lock(&priv->vlan_mutex);
  111. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  112. int flags;
  113. flags = cpriv->dev->flags;
  114. if (!(flags & IFF_UP))
  115. continue;
  116. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  117. }
  118. mutex_unlock(&priv->vlan_mutex);
  119. }
  120. return 0;
  121. }
  122. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  123. {
  124. struct ipoib_dev_priv *priv = netdev_priv(dev);
  125. if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
  126. return -EINVAL;
  127. priv->admin_mtu = new_mtu;
  128. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  129. return 0;
  130. }
  131. static struct ipoib_path *__path_find(struct net_device *dev,
  132. union ib_gid *gid)
  133. {
  134. struct ipoib_dev_priv *priv = netdev_priv(dev);
  135. struct rb_node *n = priv->path_tree.rb_node;
  136. struct ipoib_path *path;
  137. int ret;
  138. while (n) {
  139. path = rb_entry(n, struct ipoib_path, rb_node);
  140. ret = memcmp(gid->raw, path->pathrec.dgid.raw,
  141. sizeof (union ib_gid));
  142. if (ret < 0)
  143. n = n->rb_left;
  144. else if (ret > 0)
  145. n = n->rb_right;
  146. else
  147. return path;
  148. }
  149. return NULL;
  150. }
  151. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  152. {
  153. struct ipoib_dev_priv *priv = netdev_priv(dev);
  154. struct rb_node **n = &priv->path_tree.rb_node;
  155. struct rb_node *pn = NULL;
  156. struct ipoib_path *tpath;
  157. int ret;
  158. while (*n) {
  159. pn = *n;
  160. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  161. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  162. sizeof (union ib_gid));
  163. if (ret < 0)
  164. n = &pn->rb_left;
  165. else if (ret > 0)
  166. n = &pn->rb_right;
  167. else
  168. return -EEXIST;
  169. }
  170. rb_link_node(&path->rb_node, pn, n);
  171. rb_insert_color(&path->rb_node, &priv->path_tree);
  172. list_add_tail(&path->list, &priv->path_list);
  173. return 0;
  174. }
  175. static void path_free(struct net_device *dev, struct ipoib_path *path)
  176. {
  177. struct ipoib_dev_priv *priv = netdev_priv(dev);
  178. struct ipoib_neigh *neigh, *tn;
  179. struct sk_buff *skb;
  180. unsigned long flags;
  181. while ((skb = __skb_dequeue(&path->queue)))
  182. dev_kfree_skb_irq(skb);
  183. spin_lock_irqsave(&priv->lock, flags);
  184. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  185. /*
  186. * It's safe to call ipoib_put_ah() inside priv->lock
  187. * here, because we know that path->ah will always
  188. * hold one more reference, so ipoib_put_ah() will
  189. * never do more than decrement the ref count.
  190. */
  191. if (neigh->ah)
  192. ipoib_put_ah(neigh->ah);
  193. *to_ipoib_neigh(neigh->neighbour) = NULL;
  194. neigh->neighbour->ops->destructor = NULL;
  195. kfree(neigh);
  196. }
  197. spin_unlock_irqrestore(&priv->lock, flags);
  198. if (path->ah)
  199. ipoib_put_ah(path->ah);
  200. kfree(path);
  201. }
  202. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  203. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  204. {
  205. struct ipoib_path_iter *iter;
  206. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  207. if (!iter)
  208. return NULL;
  209. iter->dev = dev;
  210. memset(iter->path.pathrec.dgid.raw, 0, 16);
  211. if (ipoib_path_iter_next(iter)) {
  212. kfree(iter);
  213. return NULL;
  214. }
  215. return iter;
  216. }
  217. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  218. {
  219. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  220. struct rb_node *n;
  221. struct ipoib_path *path;
  222. int ret = 1;
  223. spin_lock_irq(&priv->lock);
  224. n = rb_first(&priv->path_tree);
  225. while (n) {
  226. path = rb_entry(n, struct ipoib_path, rb_node);
  227. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  228. sizeof (union ib_gid)) < 0) {
  229. iter->path = *path;
  230. ret = 0;
  231. break;
  232. }
  233. n = rb_next(n);
  234. }
  235. spin_unlock_irq(&priv->lock);
  236. return ret;
  237. }
  238. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  239. struct ipoib_path *path)
  240. {
  241. *path = iter->path;
  242. }
  243. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  244. void ipoib_flush_paths(struct net_device *dev)
  245. {
  246. struct ipoib_dev_priv *priv = netdev_priv(dev);
  247. struct ipoib_path *path, *tp;
  248. LIST_HEAD(remove_list);
  249. unsigned long flags;
  250. spin_lock_irqsave(&priv->lock, flags);
  251. list_splice(&priv->path_list, &remove_list);
  252. INIT_LIST_HEAD(&priv->path_list);
  253. list_for_each_entry(path, &remove_list, list)
  254. rb_erase(&path->rb_node, &priv->path_tree);
  255. spin_unlock_irqrestore(&priv->lock, flags);
  256. list_for_each_entry_safe(path, tp, &remove_list, list) {
  257. if (path->query)
  258. ib_sa_cancel_query(path->query_id, path->query);
  259. wait_for_completion(&path->done);
  260. path_free(dev, path);
  261. }
  262. }
  263. static void path_rec_completion(int status,
  264. struct ib_sa_path_rec *pathrec,
  265. void *path_ptr)
  266. {
  267. struct ipoib_path *path = path_ptr;
  268. struct net_device *dev = path->dev;
  269. struct ipoib_dev_priv *priv = netdev_priv(dev);
  270. struct ipoib_ah *ah = NULL;
  271. struct ipoib_neigh *neigh;
  272. struct sk_buff_head skqueue;
  273. struct sk_buff *skb;
  274. unsigned long flags;
  275. if (pathrec)
  276. ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
  277. be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
  278. else
  279. ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
  280. status, IPOIB_GID_ARG(path->pathrec.dgid));
  281. skb_queue_head_init(&skqueue);
  282. if (!status) {
  283. struct ib_ah_attr av = {
  284. .dlid = be16_to_cpu(pathrec->dlid),
  285. .sl = pathrec->sl,
  286. .port_num = priv->port
  287. };
  288. int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
  289. if (path_rate > 0 && priv->local_rate > path_rate)
  290. av.static_rate = (priv->local_rate - 1) / path_rate;
  291. ipoib_dbg(priv, "static_rate %d for local port %dX, path %dX\n",
  292. av.static_rate, priv->local_rate,
  293. ib_sa_rate_enum_to_int(pathrec->rate));
  294. ah = ipoib_create_ah(dev, priv->pd, &av);
  295. }
  296. spin_lock_irqsave(&priv->lock, flags);
  297. path->ah = ah;
  298. if (ah) {
  299. path->pathrec = *pathrec;
  300. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  301. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  302. while ((skb = __skb_dequeue(&path->queue)))
  303. __skb_queue_tail(&skqueue, skb);
  304. list_for_each_entry(neigh, &path->neigh_list, list) {
  305. kref_get(&path->ah->ref);
  306. neigh->ah = path->ah;
  307. while ((skb = __skb_dequeue(&neigh->queue)))
  308. __skb_queue_tail(&skqueue, skb);
  309. }
  310. }
  311. path->query = NULL;
  312. complete(&path->done);
  313. spin_unlock_irqrestore(&priv->lock, flags);
  314. while ((skb = __skb_dequeue(&skqueue))) {
  315. skb->dev = dev;
  316. if (dev_queue_xmit(skb))
  317. ipoib_warn(priv, "dev_queue_xmit failed "
  318. "to requeue packet\n");
  319. }
  320. }
  321. static struct ipoib_path *path_rec_create(struct net_device *dev,
  322. union ib_gid *gid)
  323. {
  324. struct ipoib_dev_priv *priv = netdev_priv(dev);
  325. struct ipoib_path *path;
  326. path = kzalloc(sizeof *path, GFP_ATOMIC);
  327. if (!path)
  328. return NULL;
  329. path->dev = dev;
  330. skb_queue_head_init(&path->queue);
  331. INIT_LIST_HEAD(&path->neigh_list);
  332. memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
  333. path->pathrec.sgid = priv->local_gid;
  334. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  335. path->pathrec.numb_path = 1;
  336. return path;
  337. }
  338. static int path_rec_start(struct net_device *dev,
  339. struct ipoib_path *path)
  340. {
  341. struct ipoib_dev_priv *priv = netdev_priv(dev);
  342. ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
  343. IPOIB_GID_ARG(path->pathrec.dgid));
  344. init_completion(&path->done);
  345. path->query_id =
  346. ib_sa_path_rec_get(priv->ca, priv->port,
  347. &path->pathrec,
  348. IB_SA_PATH_REC_DGID |
  349. IB_SA_PATH_REC_SGID |
  350. IB_SA_PATH_REC_NUMB_PATH |
  351. IB_SA_PATH_REC_PKEY,
  352. 1000, GFP_ATOMIC,
  353. path_rec_completion,
  354. path, &path->query);
  355. if (path->query_id < 0) {
  356. ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
  357. path->query = NULL;
  358. return path->query_id;
  359. }
  360. return 0;
  361. }
  362. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  363. {
  364. struct ipoib_dev_priv *priv = netdev_priv(dev);
  365. struct ipoib_path *path;
  366. struct ipoib_neigh *neigh;
  367. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  368. if (!neigh) {
  369. ++priv->stats.tx_dropped;
  370. dev_kfree_skb_any(skb);
  371. return;
  372. }
  373. skb_queue_head_init(&neigh->queue);
  374. neigh->neighbour = skb->dst->neighbour;
  375. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  376. /*
  377. * We can only be called from ipoib_start_xmit, so we're
  378. * inside tx_lock -- no need to save/restore flags.
  379. */
  380. spin_lock(&priv->lock);
  381. path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
  382. if (!path) {
  383. path = path_rec_create(dev,
  384. (union ib_gid *) (skb->dst->neighbour->ha + 4));
  385. if (!path)
  386. goto err;
  387. __path_add(dev, path);
  388. }
  389. list_add_tail(&neigh->list, &path->neigh_list);
  390. if (path->ah) {
  391. kref_get(&path->ah->ref);
  392. neigh->ah = path->ah;
  393. ipoib_send(dev, skb, path->ah,
  394. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  395. } else {
  396. neigh->ah = NULL;
  397. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  398. __skb_queue_tail(&neigh->queue, skb);
  399. } else {
  400. ++priv->stats.tx_dropped;
  401. dev_kfree_skb_any(skb);
  402. }
  403. if (!path->query && path_rec_start(dev, path))
  404. goto err;
  405. }
  406. spin_unlock(&priv->lock);
  407. return;
  408. err:
  409. *to_ipoib_neigh(skb->dst->neighbour) = NULL;
  410. list_del(&neigh->list);
  411. neigh->neighbour->ops->destructor = NULL;
  412. kfree(neigh);
  413. ++priv->stats.tx_dropped;
  414. dev_kfree_skb_any(skb);
  415. spin_unlock(&priv->lock);
  416. }
  417. static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
  418. {
  419. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  420. /* Look up path record for unicasts */
  421. if (skb->dst->neighbour->ha[4] != 0xff) {
  422. neigh_add_path(skb, dev);
  423. return;
  424. }
  425. /* Add in the P_Key for multicasts */
  426. skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
  427. skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
  428. ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
  429. }
  430. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  431. struct ipoib_pseudoheader *phdr)
  432. {
  433. struct ipoib_dev_priv *priv = netdev_priv(dev);
  434. struct ipoib_path *path;
  435. /*
  436. * We can only be called from ipoib_start_xmit, so we're
  437. * inside tx_lock -- no need to save/restore flags.
  438. */
  439. spin_lock(&priv->lock);
  440. path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
  441. if (!path) {
  442. path = path_rec_create(dev,
  443. (union ib_gid *) (phdr->hwaddr + 4));
  444. if (path) {
  445. /* put pseudoheader back on for next time */
  446. skb_push(skb, sizeof *phdr);
  447. __skb_queue_tail(&path->queue, skb);
  448. if (path_rec_start(dev, path)) {
  449. spin_unlock(&priv->lock);
  450. path_free(dev, path);
  451. return;
  452. } else
  453. __path_add(dev, path);
  454. } else {
  455. ++priv->stats.tx_dropped;
  456. dev_kfree_skb_any(skb);
  457. }
  458. spin_unlock(&priv->lock);
  459. return;
  460. }
  461. if (path->ah) {
  462. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  463. be16_to_cpu(path->pathrec.dlid));
  464. ipoib_send(dev, skb, path->ah,
  465. be32_to_cpup((__be32 *) phdr->hwaddr));
  466. } else if ((path->query || !path_rec_start(dev, path)) &&
  467. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  468. /* put pseudoheader back on for next time */
  469. skb_push(skb, sizeof *phdr);
  470. __skb_queue_tail(&path->queue, skb);
  471. } else {
  472. ++priv->stats.tx_dropped;
  473. dev_kfree_skb_any(skb);
  474. }
  475. spin_unlock(&priv->lock);
  476. }
  477. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  478. {
  479. struct ipoib_dev_priv *priv = netdev_priv(dev);
  480. struct ipoib_neigh *neigh;
  481. unsigned long flags;
  482. if (!spin_trylock_irqsave(&priv->tx_lock, flags))
  483. return NETDEV_TX_LOCKED;
  484. /*
  485. * Check if our queue is stopped. Since we have the LLTX bit
  486. * set, we can't rely on netif_stop_queue() preventing our
  487. * xmit function from being called with a full queue.
  488. */
  489. if (unlikely(netif_queue_stopped(dev))) {
  490. spin_unlock_irqrestore(&priv->tx_lock, flags);
  491. return NETDEV_TX_BUSY;
  492. }
  493. if (skb->dst && skb->dst->neighbour) {
  494. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  495. ipoib_path_lookup(skb, dev);
  496. goto out;
  497. }
  498. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  499. if (likely(neigh->ah)) {
  500. ipoib_send(dev, skb, neigh->ah,
  501. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  502. goto out;
  503. }
  504. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  505. spin_lock(&priv->lock);
  506. __skb_queue_tail(&neigh->queue, skb);
  507. spin_unlock(&priv->lock);
  508. } else {
  509. ++priv->stats.tx_dropped;
  510. dev_kfree_skb_any(skb);
  511. }
  512. } else {
  513. struct ipoib_pseudoheader *phdr =
  514. (struct ipoib_pseudoheader *) skb->data;
  515. skb_pull(skb, sizeof *phdr);
  516. if (phdr->hwaddr[4] == 0xff) {
  517. /* Add in the P_Key for multicast*/
  518. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  519. phdr->hwaddr[9] = priv->pkey & 0xff;
  520. ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
  521. } else {
  522. /* unicast GID -- should be ARP or RARP reply */
  523. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  524. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  525. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
  526. IPOIB_GID_FMT "\n",
  527. skb->dst ? "neigh" : "dst",
  528. be16_to_cpup((__be16 *) skb->data),
  529. be32_to_cpup((__be32 *) phdr->hwaddr),
  530. IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
  531. dev_kfree_skb_any(skb);
  532. ++priv->stats.tx_dropped;
  533. goto out;
  534. }
  535. unicast_arp_send(skb, dev, phdr);
  536. }
  537. }
  538. out:
  539. spin_unlock_irqrestore(&priv->tx_lock, flags);
  540. return NETDEV_TX_OK;
  541. }
  542. static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
  543. {
  544. struct ipoib_dev_priv *priv = netdev_priv(dev);
  545. return &priv->stats;
  546. }
  547. static void ipoib_timeout(struct net_device *dev)
  548. {
  549. struct ipoib_dev_priv *priv = netdev_priv(dev);
  550. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  551. jiffies_to_msecs(jiffies - dev->trans_start));
  552. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  553. netif_queue_stopped(dev),
  554. priv->tx_head, priv->tx_tail);
  555. /* XXX reset QP, etc. */
  556. }
  557. static int ipoib_hard_header(struct sk_buff *skb,
  558. struct net_device *dev,
  559. unsigned short type,
  560. void *daddr, void *saddr, unsigned len)
  561. {
  562. struct ipoib_header *header;
  563. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  564. header->proto = htons(type);
  565. header->reserved = 0;
  566. /*
  567. * If we don't have a neighbour structure, stuff the
  568. * destination address onto the front of the skb so we can
  569. * figure out where to send the packet later.
  570. */
  571. if (!skb->dst || !skb->dst->neighbour) {
  572. struct ipoib_pseudoheader *phdr =
  573. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  574. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  575. }
  576. return 0;
  577. }
  578. static void ipoib_set_mcast_list(struct net_device *dev)
  579. {
  580. struct ipoib_dev_priv *priv = netdev_priv(dev);
  581. queue_work(ipoib_workqueue, &priv->restart_task);
  582. }
  583. static void ipoib_neigh_destructor(struct neighbour *n)
  584. {
  585. struct ipoib_neigh *neigh;
  586. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  587. unsigned long flags;
  588. struct ipoib_ah *ah = NULL;
  589. ipoib_dbg(priv,
  590. "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
  591. be32_to_cpup((__be32 *) n->ha),
  592. IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
  593. spin_lock_irqsave(&priv->lock, flags);
  594. neigh = *to_ipoib_neigh(n);
  595. if (neigh) {
  596. if (neigh->ah)
  597. ah = neigh->ah;
  598. list_del(&neigh->list);
  599. *to_ipoib_neigh(n) = NULL;
  600. kfree(neigh);
  601. }
  602. spin_unlock_irqrestore(&priv->lock, flags);
  603. if (ah)
  604. ipoib_put_ah(ah);
  605. }
  606. static int ipoib_neigh_setup(struct neighbour *neigh)
  607. {
  608. /*
  609. * Is this kosher? I can't find anybody in the kernel that
  610. * sets neigh->destructor, so we should be able to set it here
  611. * without trouble.
  612. */
  613. neigh->ops->destructor = ipoib_neigh_destructor;
  614. return 0;
  615. }
  616. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  617. {
  618. parms->neigh_setup = ipoib_neigh_setup;
  619. return 0;
  620. }
  621. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  622. {
  623. struct ipoib_dev_priv *priv = netdev_priv(dev);
  624. /* Allocate RX/TX "rings" to hold queued skbs */
  625. priv->rx_ring = kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
  626. GFP_KERNEL);
  627. if (!priv->rx_ring) {
  628. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  629. ca->name, IPOIB_RX_RING_SIZE);
  630. goto out;
  631. }
  632. priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
  633. GFP_KERNEL);
  634. if (!priv->tx_ring) {
  635. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  636. ca->name, IPOIB_TX_RING_SIZE);
  637. goto out_rx_ring_cleanup;
  638. }
  639. /* priv->tx_head & tx_tail are already 0 */
  640. if (ipoib_ib_dev_init(dev, ca, port))
  641. goto out_tx_ring_cleanup;
  642. return 0;
  643. out_tx_ring_cleanup:
  644. kfree(priv->tx_ring);
  645. out_rx_ring_cleanup:
  646. kfree(priv->rx_ring);
  647. out:
  648. return -ENOMEM;
  649. }
  650. void ipoib_dev_cleanup(struct net_device *dev)
  651. {
  652. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  653. ipoib_delete_debug_files(dev);
  654. /* Delete any child interfaces first */
  655. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  656. unregister_netdev(cpriv->dev);
  657. ipoib_dev_cleanup(cpriv->dev);
  658. free_netdev(cpriv->dev);
  659. }
  660. ipoib_ib_dev_cleanup(dev);
  661. kfree(priv->rx_ring);
  662. kfree(priv->tx_ring);
  663. priv->rx_ring = NULL;
  664. priv->tx_ring = NULL;
  665. }
  666. static void ipoib_setup(struct net_device *dev)
  667. {
  668. struct ipoib_dev_priv *priv = netdev_priv(dev);
  669. dev->open = ipoib_open;
  670. dev->stop = ipoib_stop;
  671. dev->change_mtu = ipoib_change_mtu;
  672. dev->hard_start_xmit = ipoib_start_xmit;
  673. dev->get_stats = ipoib_get_stats;
  674. dev->tx_timeout = ipoib_timeout;
  675. dev->hard_header = ipoib_hard_header;
  676. dev->set_multicast_list = ipoib_set_mcast_list;
  677. dev->neigh_setup = ipoib_neigh_setup_dev;
  678. dev->watchdog_timeo = HZ;
  679. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  680. /*
  681. * We add in INFINIBAND_ALEN to allow for the destination
  682. * address "pseudoheader" for skbs without neighbour struct.
  683. */
  684. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  685. dev->addr_len = INFINIBAND_ALEN;
  686. dev->type = ARPHRD_INFINIBAND;
  687. dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
  688. dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
  689. /* MTU will be reset when mcast join happens */
  690. dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
  691. priv->mcast_mtu = priv->admin_mtu = dev->mtu;
  692. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  693. netif_carrier_off(dev);
  694. SET_MODULE_OWNER(dev);
  695. priv->dev = dev;
  696. spin_lock_init(&priv->lock);
  697. spin_lock_init(&priv->tx_lock);
  698. mutex_init(&priv->mcast_mutex);
  699. mutex_init(&priv->vlan_mutex);
  700. INIT_LIST_HEAD(&priv->path_list);
  701. INIT_LIST_HEAD(&priv->child_intfs);
  702. INIT_LIST_HEAD(&priv->dead_ahs);
  703. INIT_LIST_HEAD(&priv->multicast_list);
  704. INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
  705. INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
  706. INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
  707. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
  708. INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
  709. }
  710. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  711. {
  712. struct net_device *dev;
  713. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  714. ipoib_setup);
  715. if (!dev)
  716. return NULL;
  717. return netdev_priv(dev);
  718. }
  719. static ssize_t show_pkey(struct class_device *cdev, char *buf)
  720. {
  721. struct ipoib_dev_priv *priv =
  722. netdev_priv(container_of(cdev, struct net_device, class_dev));
  723. return sprintf(buf, "0x%04x\n", priv->pkey);
  724. }
  725. static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  726. static ssize_t create_child(struct class_device *cdev,
  727. const char *buf, size_t count)
  728. {
  729. int pkey;
  730. int ret;
  731. if (sscanf(buf, "%i", &pkey) != 1)
  732. return -EINVAL;
  733. if (pkey < 0 || pkey > 0xffff)
  734. return -EINVAL;
  735. /*
  736. * Set the full membership bit, so that we join the right
  737. * broadcast group, etc.
  738. */
  739. pkey |= 0x8000;
  740. ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
  741. pkey);
  742. return ret ? ret : count;
  743. }
  744. static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  745. static ssize_t delete_child(struct class_device *cdev,
  746. const char *buf, size_t count)
  747. {
  748. int pkey;
  749. int ret;
  750. if (sscanf(buf, "%i", &pkey) != 1)
  751. return -EINVAL;
  752. if (pkey < 0 || pkey > 0xffff)
  753. return -EINVAL;
  754. ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
  755. pkey);
  756. return ret ? ret : count;
  757. }
  758. static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  759. int ipoib_add_pkey_attr(struct net_device *dev)
  760. {
  761. return class_device_create_file(&dev->class_dev,
  762. &class_device_attr_pkey);
  763. }
  764. static struct net_device *ipoib_add_port(const char *format,
  765. struct ib_device *hca, u8 port)
  766. {
  767. struct ipoib_dev_priv *priv;
  768. int result = -ENOMEM;
  769. priv = ipoib_intf_alloc(format);
  770. if (!priv)
  771. goto alloc_mem_failed;
  772. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  773. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  774. if (result) {
  775. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  776. hca->name, port, result);
  777. goto alloc_mem_failed;
  778. }
  779. /*
  780. * Set the full membership bit, so that we join the right
  781. * broadcast group, etc.
  782. */
  783. priv->pkey |= 0x8000;
  784. priv->dev->broadcast[8] = priv->pkey >> 8;
  785. priv->dev->broadcast[9] = priv->pkey & 0xff;
  786. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  787. if (result) {
  788. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  789. hca->name, port, result);
  790. goto alloc_mem_failed;
  791. } else
  792. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  793. result = ipoib_dev_init(priv->dev, hca, port);
  794. if (result < 0) {
  795. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  796. hca->name, port, result);
  797. goto device_init_failed;
  798. }
  799. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  800. priv->ca, ipoib_event);
  801. result = ib_register_event_handler(&priv->event_handler);
  802. if (result < 0) {
  803. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  804. "port %d (ret = %d)\n",
  805. hca->name, port, result);
  806. goto event_failed;
  807. }
  808. result = register_netdev(priv->dev);
  809. if (result) {
  810. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  811. hca->name, port, result);
  812. goto register_failed;
  813. }
  814. ipoib_create_debug_files(priv->dev);
  815. if (ipoib_add_pkey_attr(priv->dev))
  816. goto sysfs_failed;
  817. if (class_device_create_file(&priv->dev->class_dev,
  818. &class_device_attr_create_child))
  819. goto sysfs_failed;
  820. if (class_device_create_file(&priv->dev->class_dev,
  821. &class_device_attr_delete_child))
  822. goto sysfs_failed;
  823. return priv->dev;
  824. sysfs_failed:
  825. ipoib_delete_debug_files(priv->dev);
  826. unregister_netdev(priv->dev);
  827. register_failed:
  828. ib_unregister_event_handler(&priv->event_handler);
  829. flush_scheduled_work();
  830. event_failed:
  831. ipoib_dev_cleanup(priv->dev);
  832. device_init_failed:
  833. free_netdev(priv->dev);
  834. alloc_mem_failed:
  835. return ERR_PTR(result);
  836. }
  837. static void ipoib_add_one(struct ib_device *device)
  838. {
  839. struct list_head *dev_list;
  840. struct net_device *dev;
  841. struct ipoib_dev_priv *priv;
  842. int s, e, p;
  843. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  844. if (!dev_list)
  845. return;
  846. INIT_LIST_HEAD(dev_list);
  847. if (device->node_type == IB_NODE_SWITCH) {
  848. s = 0;
  849. e = 0;
  850. } else {
  851. s = 1;
  852. e = device->phys_port_cnt;
  853. }
  854. for (p = s; p <= e; ++p) {
  855. dev = ipoib_add_port("ib%d", device, p);
  856. if (!IS_ERR(dev)) {
  857. priv = netdev_priv(dev);
  858. list_add_tail(&priv->list, dev_list);
  859. }
  860. }
  861. ib_set_client_data(device, &ipoib_client, dev_list);
  862. }
  863. static void ipoib_remove_one(struct ib_device *device)
  864. {
  865. struct ipoib_dev_priv *priv, *tmp;
  866. struct list_head *dev_list;
  867. dev_list = ib_get_client_data(device, &ipoib_client);
  868. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  869. ib_unregister_event_handler(&priv->event_handler);
  870. flush_scheduled_work();
  871. unregister_netdev(priv->dev);
  872. ipoib_dev_cleanup(priv->dev);
  873. free_netdev(priv->dev);
  874. }
  875. kfree(dev_list);
  876. }
  877. static int __init ipoib_init_module(void)
  878. {
  879. int ret;
  880. ret = ipoib_register_debugfs();
  881. if (ret)
  882. return ret;
  883. /*
  884. * We create our own workqueue mainly because we want to be
  885. * able to flush it when devices are being removed. We can't
  886. * use schedule_work()/flush_scheduled_work() because both
  887. * unregister_netdev() and linkwatch_event take the rtnl lock,
  888. * so flush_scheduled_work() can deadlock during device
  889. * removal.
  890. */
  891. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  892. if (!ipoib_workqueue) {
  893. ret = -ENOMEM;
  894. goto err_fs;
  895. }
  896. ret = ib_register_client(&ipoib_client);
  897. if (ret)
  898. goto err_wq;
  899. return 0;
  900. err_wq:
  901. destroy_workqueue(ipoib_workqueue);
  902. err_fs:
  903. ipoib_unregister_debugfs();
  904. return ret;
  905. }
  906. static void __exit ipoib_cleanup_module(void)
  907. {
  908. ib_unregister_client(&ipoib_client);
  909. ipoib_unregister_debugfs();
  910. destroy_workqueue(ipoib_workqueue);
  911. }
  912. module_init(ipoib_init_module);
  913. module_exit(ipoib_cleanup_module);