ipoib_main.c 27 KB

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