ipoib_main.c 27 KB

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