ipoib_main.c 28 KB

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