ipoib_main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. if (!spin_trylock_irqsave(&priv->tx_lock, flags))
  437. return NETDEV_TX_LOCKED;
  438. /*
  439. * Check if our queue is stopped. Since we have the LLTX bit
  440. * set, we can't rely on netif_stop_queue() preventing our
  441. * xmit function from being called with a full queue.
  442. */
  443. if (unlikely(netif_queue_stopped(dev))) {
  444. spin_unlock_irqrestore(&priv->tx_lock, flags);
  445. return NETDEV_TX_BUSY;
  446. }
  447. if (skb->dst && skb->dst->neighbour) {
  448. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  449. ipoib_path_lookup(skb, dev);
  450. goto out;
  451. }
  452. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  453. if (likely(neigh->ah)) {
  454. ipoib_send(dev, skb, neigh->ah,
  455. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  456. goto out;
  457. }
  458. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  459. spin_lock(&priv->lock);
  460. __skb_queue_tail(&neigh->queue, skb);
  461. spin_unlock(&priv->lock);
  462. } else {
  463. ++priv->stats.tx_dropped;
  464. dev_kfree_skb_any(skb);
  465. }
  466. } else {
  467. struct ipoib_pseudoheader *phdr =
  468. (struct ipoib_pseudoheader *) skb->data;
  469. skb_pull(skb, sizeof *phdr);
  470. if (phdr->hwaddr[4] == 0xff) {
  471. /* Add in the P_Key for multicast*/
  472. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  473. phdr->hwaddr[9] = priv->pkey & 0xff;
  474. ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
  475. } else {
  476. /* unicast GID -- should be ARP or RARP reply */
  477. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  478. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  479. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
  480. IPOIB_GID_FMT "\n",
  481. skb->dst ? "neigh" : "dst",
  482. be16_to_cpup((__be16 *) skb->data),
  483. be32_to_cpup((__be32 *) phdr->hwaddr),
  484. IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
  485. dev_kfree_skb_any(skb);
  486. ++priv->stats.tx_dropped;
  487. goto out;
  488. }
  489. unicast_arp_send(skb, dev, phdr);
  490. }
  491. }
  492. out:
  493. spin_unlock_irqrestore(&priv->tx_lock, flags);
  494. return NETDEV_TX_OK;
  495. }
  496. static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
  497. {
  498. struct ipoib_dev_priv *priv = netdev_priv(dev);
  499. return &priv->stats;
  500. }
  501. static void ipoib_timeout(struct net_device *dev)
  502. {
  503. struct ipoib_dev_priv *priv = netdev_priv(dev);
  504. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  505. jiffies_to_msecs(jiffies - dev->trans_start));
  506. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  507. netif_queue_stopped(dev),
  508. priv->tx_head, priv->tx_tail);
  509. /* XXX reset QP, etc. */
  510. }
  511. static int ipoib_hard_header(struct sk_buff *skb,
  512. struct net_device *dev,
  513. unsigned short type,
  514. void *daddr, void *saddr, unsigned len)
  515. {
  516. struct ipoib_header *header;
  517. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  518. header->proto = htons(type);
  519. header->reserved = 0;
  520. /*
  521. * If we don't have a neighbour structure, stuff the
  522. * destination address onto the front of the skb so we can
  523. * figure out where to send the packet later.
  524. */
  525. if (!skb->dst || !skb->dst->neighbour) {
  526. struct ipoib_pseudoheader *phdr =
  527. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  528. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  529. }
  530. return 0;
  531. }
  532. static void ipoib_set_mcast_list(struct net_device *dev)
  533. {
  534. struct ipoib_dev_priv *priv = netdev_priv(dev);
  535. queue_work(ipoib_workqueue, &priv->restart_task);
  536. }
  537. static void ipoib_neigh_destructor(struct neighbour *n)
  538. {
  539. struct ipoib_neigh *neigh;
  540. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  541. unsigned long flags;
  542. struct ipoib_ah *ah = NULL;
  543. ipoib_dbg(priv,
  544. "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
  545. be32_to_cpup((__be32 *) n->ha),
  546. IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
  547. spin_lock_irqsave(&priv->lock, flags);
  548. neigh = *to_ipoib_neigh(n);
  549. if (neigh) {
  550. if (neigh->ah)
  551. ah = neigh->ah;
  552. list_del(&neigh->list);
  553. *to_ipoib_neigh(n) = NULL;
  554. kfree(neigh);
  555. }
  556. spin_unlock_irqrestore(&priv->lock, flags);
  557. if (ah)
  558. ipoib_put_ah(ah);
  559. }
  560. static int ipoib_neigh_setup(struct neighbour *neigh)
  561. {
  562. /*
  563. * Is this kosher? I can't find anybody in the kernel that
  564. * sets neigh->destructor, so we should be able to set it here
  565. * without trouble.
  566. */
  567. neigh->ops->destructor = ipoib_neigh_destructor;
  568. return 0;
  569. }
  570. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  571. {
  572. parms->neigh_setup = ipoib_neigh_setup;
  573. return 0;
  574. }
  575. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  576. {
  577. struct ipoib_dev_priv *priv = netdev_priv(dev);
  578. /* Allocate RX/TX "rings" to hold queued skbs */
  579. priv->rx_ring = kmalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
  580. GFP_KERNEL);
  581. if (!priv->rx_ring) {
  582. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  583. ca->name, IPOIB_RX_RING_SIZE);
  584. goto out;
  585. }
  586. memset(priv->rx_ring, 0,
  587. IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf));
  588. priv->tx_ring = kmalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
  589. GFP_KERNEL);
  590. if (!priv->tx_ring) {
  591. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  592. ca->name, IPOIB_TX_RING_SIZE);
  593. goto out_rx_ring_cleanup;
  594. }
  595. memset(priv->tx_ring, 0,
  596. IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf));
  597. /* priv->tx_head & tx_tail are already 0 */
  598. if (ipoib_ib_dev_init(dev, ca, port))
  599. goto out_tx_ring_cleanup;
  600. return 0;
  601. out_tx_ring_cleanup:
  602. kfree(priv->tx_ring);
  603. out_rx_ring_cleanup:
  604. kfree(priv->rx_ring);
  605. out:
  606. return -ENOMEM;
  607. }
  608. void ipoib_dev_cleanup(struct net_device *dev)
  609. {
  610. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  611. ipoib_delete_debug_file(dev);
  612. /* Delete any child interfaces first */
  613. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  614. unregister_netdev(cpriv->dev);
  615. ipoib_dev_cleanup(cpriv->dev);
  616. free_netdev(cpriv->dev);
  617. }
  618. ipoib_ib_dev_cleanup(dev);
  619. kfree(priv->rx_ring);
  620. kfree(priv->tx_ring);
  621. priv->rx_ring = NULL;
  622. priv->tx_ring = NULL;
  623. }
  624. static void ipoib_setup(struct net_device *dev)
  625. {
  626. struct ipoib_dev_priv *priv = netdev_priv(dev);
  627. dev->open = ipoib_open;
  628. dev->stop = ipoib_stop;
  629. dev->change_mtu = ipoib_change_mtu;
  630. dev->hard_start_xmit = ipoib_start_xmit;
  631. dev->get_stats = ipoib_get_stats;
  632. dev->tx_timeout = ipoib_timeout;
  633. dev->hard_header = ipoib_hard_header;
  634. dev->set_multicast_list = ipoib_set_mcast_list;
  635. dev->neigh_setup = ipoib_neigh_setup_dev;
  636. dev->watchdog_timeo = HZ;
  637. dev->rebuild_header = NULL;
  638. dev->set_mac_address = NULL;
  639. dev->header_cache_update = NULL;
  640. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  641. /*
  642. * We add in INFINIBAND_ALEN to allow for the destination
  643. * address "pseudoheader" for skbs without neighbour struct.
  644. */
  645. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  646. dev->addr_len = INFINIBAND_ALEN;
  647. dev->type = ARPHRD_INFINIBAND;
  648. dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
  649. dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
  650. /* MTU will be reset when mcast join happens */
  651. dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
  652. priv->mcast_mtu = priv->admin_mtu = dev->mtu;
  653. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  654. netif_carrier_off(dev);
  655. SET_MODULE_OWNER(dev);
  656. priv->dev = dev;
  657. spin_lock_init(&priv->lock);
  658. spin_lock_init(&priv->tx_lock);
  659. init_MUTEX(&priv->mcast_mutex);
  660. init_MUTEX(&priv->vlan_mutex);
  661. INIT_LIST_HEAD(&priv->path_list);
  662. INIT_LIST_HEAD(&priv->child_intfs);
  663. INIT_LIST_HEAD(&priv->dead_ahs);
  664. INIT_LIST_HEAD(&priv->multicast_list);
  665. INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
  666. INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
  667. INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
  668. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
  669. INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
  670. }
  671. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  672. {
  673. struct net_device *dev;
  674. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  675. ipoib_setup);
  676. if (!dev)
  677. return NULL;
  678. return netdev_priv(dev);
  679. }
  680. static ssize_t show_pkey(struct class_device *cdev, char *buf)
  681. {
  682. struct ipoib_dev_priv *priv =
  683. netdev_priv(container_of(cdev, struct net_device, class_dev));
  684. return sprintf(buf, "0x%04x\n", priv->pkey);
  685. }
  686. static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  687. static ssize_t create_child(struct class_device *cdev,
  688. const char *buf, size_t count)
  689. {
  690. int pkey;
  691. int ret;
  692. if (sscanf(buf, "%i", &pkey) != 1)
  693. return -EINVAL;
  694. if (pkey < 0 || pkey > 0xffff)
  695. return -EINVAL;
  696. /*
  697. * Set the full membership bit, so that we join the right
  698. * broadcast group, etc.
  699. */
  700. pkey |= 0x8000;
  701. ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
  702. pkey);
  703. return ret ? ret : count;
  704. }
  705. static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  706. static ssize_t delete_child(struct class_device *cdev,
  707. const char *buf, size_t count)
  708. {
  709. int pkey;
  710. int ret;
  711. if (sscanf(buf, "%i", &pkey) != 1)
  712. return -EINVAL;
  713. if (pkey < 0 || pkey > 0xffff)
  714. return -EINVAL;
  715. ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
  716. pkey);
  717. return ret ? ret : count;
  718. }
  719. static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  720. int ipoib_add_pkey_attr(struct net_device *dev)
  721. {
  722. return class_device_create_file(&dev->class_dev,
  723. &class_device_attr_pkey);
  724. }
  725. static struct net_device *ipoib_add_port(const char *format,
  726. struct ib_device *hca, u8 port)
  727. {
  728. struct ipoib_dev_priv *priv;
  729. int result = -ENOMEM;
  730. priv = ipoib_intf_alloc(format);
  731. if (!priv)
  732. goto alloc_mem_failed;
  733. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  734. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  735. if (result) {
  736. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  737. hca->name, port, result);
  738. goto alloc_mem_failed;
  739. }
  740. /*
  741. * Set the full membership bit, so that we join the right
  742. * broadcast group, etc.
  743. */
  744. priv->pkey |= 0x8000;
  745. priv->dev->broadcast[8] = priv->pkey >> 8;
  746. priv->dev->broadcast[9] = priv->pkey & 0xff;
  747. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  748. if (result) {
  749. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  750. hca->name, port, result);
  751. goto alloc_mem_failed;
  752. } else
  753. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  754. result = ipoib_dev_init(priv->dev, hca, port);
  755. if (result < 0) {
  756. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  757. hca->name, port, result);
  758. goto device_init_failed;
  759. }
  760. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  761. priv->ca, ipoib_event);
  762. result = ib_register_event_handler(&priv->event_handler);
  763. if (result < 0) {
  764. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  765. "port %d (ret = %d)\n",
  766. hca->name, port, result);
  767. goto event_failed;
  768. }
  769. result = register_netdev(priv->dev);
  770. if (result) {
  771. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  772. hca->name, port, result);
  773. goto register_failed;
  774. }
  775. if (ipoib_create_debug_file(priv->dev))
  776. goto debug_failed;
  777. if (ipoib_add_pkey_attr(priv->dev))
  778. goto sysfs_failed;
  779. if (class_device_create_file(&priv->dev->class_dev,
  780. &class_device_attr_create_child))
  781. goto sysfs_failed;
  782. if (class_device_create_file(&priv->dev->class_dev,
  783. &class_device_attr_delete_child))
  784. goto sysfs_failed;
  785. return priv->dev;
  786. sysfs_failed:
  787. ipoib_delete_debug_file(priv->dev);
  788. debug_failed:
  789. unregister_netdev(priv->dev);
  790. register_failed:
  791. ib_unregister_event_handler(&priv->event_handler);
  792. flush_scheduled_work();
  793. event_failed:
  794. ipoib_dev_cleanup(priv->dev);
  795. device_init_failed:
  796. free_netdev(priv->dev);
  797. alloc_mem_failed:
  798. return ERR_PTR(result);
  799. }
  800. static void ipoib_add_one(struct ib_device *device)
  801. {
  802. struct list_head *dev_list;
  803. struct net_device *dev;
  804. struct ipoib_dev_priv *priv;
  805. int s, e, p;
  806. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  807. if (!dev_list)
  808. return;
  809. INIT_LIST_HEAD(dev_list);
  810. if (device->node_type == IB_NODE_SWITCH) {
  811. s = 0;
  812. e = 0;
  813. } else {
  814. s = 1;
  815. e = device->phys_port_cnt;
  816. }
  817. for (p = s; p <= e; ++p) {
  818. dev = ipoib_add_port("ib%d", device, p);
  819. if (!IS_ERR(dev)) {
  820. priv = netdev_priv(dev);
  821. list_add_tail(&priv->list, dev_list);
  822. }
  823. }
  824. ib_set_client_data(device, &ipoib_client, dev_list);
  825. }
  826. static void ipoib_remove_one(struct ib_device *device)
  827. {
  828. struct ipoib_dev_priv *priv, *tmp;
  829. struct list_head *dev_list;
  830. dev_list = ib_get_client_data(device, &ipoib_client);
  831. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  832. ib_unregister_event_handler(&priv->event_handler);
  833. flush_scheduled_work();
  834. unregister_netdev(priv->dev);
  835. ipoib_dev_cleanup(priv->dev);
  836. free_netdev(priv->dev);
  837. }
  838. kfree(dev_list);
  839. }
  840. static int __init ipoib_init_module(void)
  841. {
  842. int ret;
  843. ret = ipoib_register_debugfs();
  844. if (ret)
  845. return ret;
  846. /*
  847. * We create our own workqueue mainly because we want to be
  848. * able to flush it when devices are being removed. We can't
  849. * use schedule_work()/flush_scheduled_work() because both
  850. * unregister_netdev() and linkwatch_event take the rtnl lock,
  851. * so flush_scheduled_work() can deadlock during device
  852. * removal.
  853. */
  854. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  855. if (!ipoib_workqueue) {
  856. ret = -ENOMEM;
  857. goto err_fs;
  858. }
  859. ret = ib_register_client(&ipoib_client);
  860. if (ret)
  861. goto err_wq;
  862. return 0;
  863. err_wq:
  864. destroy_workqueue(ipoib_workqueue);
  865. err_fs:
  866. ipoib_unregister_debugfs();
  867. return ret;
  868. }
  869. static void __exit ipoib_cleanup_module(void)
  870. {
  871. ib_unregister_client(&ipoib_client);
  872. ipoib_unregister_debugfs();
  873. destroy_workqueue(ipoib_workqueue);
  874. }
  875. module_init(ipoib_init_module);
  876. module_exit(ipoib_cleanup_module);