ipoib_main.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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. #include "ipoib.h"
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/kernel.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  41. #include <linux/ip.h>
  42. #include <linux/in.h>
  43. #include <net/dst.h>
  44. MODULE_AUTHOR("Roland Dreier");
  45. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  46. MODULE_LICENSE("Dual BSD/GPL");
  47. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  48. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  49. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  50. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  51. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  52. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  53. static int lro;
  54. module_param(lro, bool, 0444);
  55. MODULE_PARM_DESC(lro, "Enable LRO (Large Receive Offload)");
  56. static int lro_max_aggr = IPOIB_LRO_MAX_AGGR;
  57. module_param(lro_max_aggr, int, 0644);
  58. MODULE_PARM_DESC(lro_max_aggr, "LRO: Max packets to be aggregated "
  59. "(default = 64)");
  60. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  61. int ipoib_debug_level;
  62. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  63. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  64. #endif
  65. struct ipoib_path_iter {
  66. struct net_device *dev;
  67. struct ipoib_path path;
  68. };
  69. static const u8 ipv4_bcast_addr[] = {
  70. 0x00, 0xff, 0xff, 0xff,
  71. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  73. };
  74. struct workqueue_struct *ipoib_workqueue;
  75. struct ib_sa_client ipoib_sa_client;
  76. static void ipoib_add_one(struct ib_device *device);
  77. static void ipoib_remove_one(struct ib_device *device);
  78. static struct ib_client ipoib_client = {
  79. .name = "ipoib",
  80. .add = ipoib_add_one,
  81. .remove = ipoib_remove_one
  82. };
  83. int ipoib_open(struct net_device *dev)
  84. {
  85. struct ipoib_dev_priv *priv = netdev_priv(dev);
  86. ipoib_dbg(priv, "bringing up interface\n");
  87. napi_enable(&priv->napi);
  88. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  89. if (ipoib_pkey_dev_delay_open(dev))
  90. return 0;
  91. if (ipoib_ib_dev_open(dev)) {
  92. napi_disable(&priv->napi);
  93. return -EINVAL;
  94. }
  95. if (ipoib_ib_dev_up(dev)) {
  96. ipoib_ib_dev_stop(dev, 1);
  97. napi_disable(&priv->napi);
  98. return -EINVAL;
  99. }
  100. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  101. struct ipoib_dev_priv *cpriv;
  102. /* Bring up any child interfaces too */
  103. mutex_lock(&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. mutex_unlock(&priv->vlan_mutex);
  112. }
  113. netif_start_queue(dev);
  114. return 0;
  115. }
  116. static int ipoib_stop(struct net_device *dev)
  117. {
  118. struct ipoib_dev_priv *priv = netdev_priv(dev);
  119. ipoib_dbg(priv, "stopping interface\n");
  120. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  121. napi_disable(&priv->napi);
  122. netif_stop_queue(dev);
  123. ipoib_ib_dev_down(dev, 0);
  124. ipoib_ib_dev_stop(dev, 0);
  125. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  126. struct ipoib_dev_priv *cpriv;
  127. /* Bring down any child interfaces too */
  128. mutex_lock(&priv->vlan_mutex);
  129. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  130. int flags;
  131. flags = cpriv->dev->flags;
  132. if (!(flags & IFF_UP))
  133. continue;
  134. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  135. }
  136. mutex_unlock(&priv->vlan_mutex);
  137. }
  138. return 0;
  139. }
  140. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  141. {
  142. struct ipoib_dev_priv *priv = netdev_priv(dev);
  143. /* dev->mtu > 2K ==> connected mode */
  144. if (ipoib_cm_admin_enabled(dev)) {
  145. if (new_mtu > ipoib_cm_max_mtu(dev))
  146. return -EINVAL;
  147. if (new_mtu > priv->mcast_mtu)
  148. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  149. priv->mcast_mtu);
  150. dev->mtu = new_mtu;
  151. return 0;
  152. }
  153. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  154. return -EINVAL;
  155. priv->admin_mtu = new_mtu;
  156. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  157. return 0;
  158. }
  159. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  160. {
  161. struct ipoib_dev_priv *priv = netdev_priv(dev);
  162. struct rb_node *n = priv->path_tree.rb_node;
  163. struct ipoib_path *path;
  164. int ret;
  165. while (n) {
  166. path = rb_entry(n, struct ipoib_path, rb_node);
  167. ret = memcmp(gid, path->pathrec.dgid.raw,
  168. sizeof (union ib_gid));
  169. if (ret < 0)
  170. n = n->rb_left;
  171. else if (ret > 0)
  172. n = n->rb_right;
  173. else
  174. return path;
  175. }
  176. return NULL;
  177. }
  178. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  179. {
  180. struct ipoib_dev_priv *priv = netdev_priv(dev);
  181. struct rb_node **n = &priv->path_tree.rb_node;
  182. struct rb_node *pn = NULL;
  183. struct ipoib_path *tpath;
  184. int ret;
  185. while (*n) {
  186. pn = *n;
  187. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  188. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  189. sizeof (union ib_gid));
  190. if (ret < 0)
  191. n = &pn->rb_left;
  192. else if (ret > 0)
  193. n = &pn->rb_right;
  194. else
  195. return -EEXIST;
  196. }
  197. rb_link_node(&path->rb_node, pn, n);
  198. rb_insert_color(&path->rb_node, &priv->path_tree);
  199. list_add_tail(&path->list, &priv->path_list);
  200. return 0;
  201. }
  202. static void path_free(struct net_device *dev, struct ipoib_path *path)
  203. {
  204. struct ipoib_dev_priv *priv = netdev_priv(dev);
  205. struct ipoib_neigh *neigh, *tn;
  206. struct sk_buff *skb;
  207. unsigned long flags;
  208. while ((skb = __skb_dequeue(&path->queue)))
  209. dev_kfree_skb_irq(skb);
  210. spin_lock_irqsave(&priv->lock, flags);
  211. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  212. /*
  213. * It's safe to call ipoib_put_ah() inside priv->lock
  214. * here, because we know that path->ah will always
  215. * hold one more reference, so ipoib_put_ah() will
  216. * never do more than decrement the ref count.
  217. */
  218. if (neigh->ah)
  219. ipoib_put_ah(neigh->ah);
  220. ipoib_neigh_free(dev, neigh);
  221. }
  222. spin_unlock_irqrestore(&priv->lock, flags);
  223. if (path->ah)
  224. ipoib_put_ah(path->ah);
  225. kfree(path);
  226. }
  227. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  228. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  229. {
  230. struct ipoib_path_iter *iter;
  231. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  232. if (!iter)
  233. return NULL;
  234. iter->dev = dev;
  235. memset(iter->path.pathrec.dgid.raw, 0, 16);
  236. if (ipoib_path_iter_next(iter)) {
  237. kfree(iter);
  238. return NULL;
  239. }
  240. return iter;
  241. }
  242. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  243. {
  244. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  245. struct rb_node *n;
  246. struct ipoib_path *path;
  247. int ret = 1;
  248. spin_lock_irq(&priv->lock);
  249. n = rb_first(&priv->path_tree);
  250. while (n) {
  251. path = rb_entry(n, struct ipoib_path, rb_node);
  252. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  253. sizeof (union ib_gid)) < 0) {
  254. iter->path = *path;
  255. ret = 0;
  256. break;
  257. }
  258. n = rb_next(n);
  259. }
  260. spin_unlock_irq(&priv->lock);
  261. return ret;
  262. }
  263. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  264. struct ipoib_path *path)
  265. {
  266. *path = iter->path;
  267. }
  268. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  269. void ipoib_mark_paths_invalid(struct net_device *dev)
  270. {
  271. struct ipoib_dev_priv *priv = netdev_priv(dev);
  272. struct ipoib_path *path, *tp;
  273. spin_lock_irq(&priv->lock);
  274. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  275. ipoib_dbg(priv, "mark path LID 0x%04x GID " IPOIB_GID_FMT " invalid\n",
  276. be16_to_cpu(path->pathrec.dlid),
  277. IPOIB_GID_ARG(path->pathrec.dgid));
  278. path->valid = 0;
  279. }
  280. spin_unlock_irq(&priv->lock);
  281. }
  282. void ipoib_flush_paths(struct net_device *dev)
  283. {
  284. struct ipoib_dev_priv *priv = netdev_priv(dev);
  285. struct ipoib_path *path, *tp;
  286. LIST_HEAD(remove_list);
  287. spin_lock_irq(&priv->tx_lock);
  288. spin_lock(&priv->lock);
  289. list_splice_init(&priv->path_list, &remove_list);
  290. list_for_each_entry(path, &remove_list, list)
  291. rb_erase(&path->rb_node, &priv->path_tree);
  292. list_for_each_entry_safe(path, tp, &remove_list, list) {
  293. if (path->query)
  294. ib_sa_cancel_query(path->query_id, path->query);
  295. spin_unlock(&priv->lock);
  296. spin_unlock_irq(&priv->tx_lock);
  297. wait_for_completion(&path->done);
  298. path_free(dev, path);
  299. spin_lock_irq(&priv->tx_lock);
  300. spin_lock(&priv->lock);
  301. }
  302. spin_unlock(&priv->lock);
  303. spin_unlock_irq(&priv->tx_lock);
  304. }
  305. static void path_rec_completion(int status,
  306. struct ib_sa_path_rec *pathrec,
  307. void *path_ptr)
  308. {
  309. struct ipoib_path *path = path_ptr;
  310. struct net_device *dev = path->dev;
  311. struct ipoib_dev_priv *priv = netdev_priv(dev);
  312. struct ipoib_ah *ah = NULL;
  313. struct ipoib_ah *old_ah;
  314. struct ipoib_neigh *neigh, *tn;
  315. struct sk_buff_head skqueue;
  316. struct sk_buff *skb;
  317. unsigned long flags;
  318. if (!status)
  319. ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
  320. be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
  321. else
  322. ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
  323. status, IPOIB_GID_ARG(path->pathrec.dgid));
  324. skb_queue_head_init(&skqueue);
  325. if (!status) {
  326. struct ib_ah_attr av;
  327. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  328. ah = ipoib_create_ah(dev, priv->pd, &av);
  329. }
  330. spin_lock_irqsave(&priv->lock, flags);
  331. old_ah = path->ah;
  332. path->ah = ah;
  333. if (ah) {
  334. path->pathrec = *pathrec;
  335. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  336. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  337. while ((skb = __skb_dequeue(&path->queue)))
  338. __skb_queue_tail(&skqueue, skb);
  339. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  340. if (neigh->ah) {
  341. WARN_ON(neigh->ah != old_ah);
  342. /*
  343. * Dropping the ah reference inside
  344. * priv->lock is safe here, because we
  345. * will hold one more reference from
  346. * the original value of path->ah (ie
  347. * old_ah).
  348. */
  349. ipoib_put_ah(neigh->ah);
  350. }
  351. kref_get(&path->ah->ref);
  352. neigh->ah = path->ah;
  353. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  354. sizeof(union ib_gid));
  355. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  356. if (!ipoib_cm_get(neigh))
  357. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  358. path,
  359. neigh));
  360. if (!ipoib_cm_get(neigh)) {
  361. list_del(&neigh->list);
  362. if (neigh->ah)
  363. ipoib_put_ah(neigh->ah);
  364. ipoib_neigh_free(dev, neigh);
  365. continue;
  366. }
  367. }
  368. while ((skb = __skb_dequeue(&neigh->queue)))
  369. __skb_queue_tail(&skqueue, skb);
  370. }
  371. path->valid = 1;
  372. }
  373. path->query = NULL;
  374. complete(&path->done);
  375. spin_unlock_irqrestore(&priv->lock, flags);
  376. if (old_ah)
  377. ipoib_put_ah(old_ah);
  378. while ((skb = __skb_dequeue(&skqueue))) {
  379. skb->dev = dev;
  380. if (dev_queue_xmit(skb))
  381. ipoib_warn(priv, "dev_queue_xmit failed "
  382. "to requeue packet\n");
  383. }
  384. }
  385. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  386. {
  387. struct ipoib_dev_priv *priv = netdev_priv(dev);
  388. struct ipoib_path *path;
  389. if (!priv->broadcast)
  390. return NULL;
  391. path = kzalloc(sizeof *path, GFP_ATOMIC);
  392. if (!path)
  393. return NULL;
  394. path->dev = dev;
  395. skb_queue_head_init(&path->queue);
  396. INIT_LIST_HEAD(&path->neigh_list);
  397. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  398. path->pathrec.sgid = priv->local_gid;
  399. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  400. path->pathrec.numb_path = 1;
  401. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  402. return path;
  403. }
  404. static int path_rec_start(struct net_device *dev,
  405. struct ipoib_path *path)
  406. {
  407. struct ipoib_dev_priv *priv = netdev_priv(dev);
  408. ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
  409. IPOIB_GID_ARG(path->pathrec.dgid));
  410. init_completion(&path->done);
  411. path->query_id =
  412. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  413. &path->pathrec,
  414. IB_SA_PATH_REC_DGID |
  415. IB_SA_PATH_REC_SGID |
  416. IB_SA_PATH_REC_NUMB_PATH |
  417. IB_SA_PATH_REC_TRAFFIC_CLASS |
  418. IB_SA_PATH_REC_PKEY,
  419. 1000, GFP_ATOMIC,
  420. path_rec_completion,
  421. path, &path->query);
  422. if (path->query_id < 0) {
  423. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  424. path->query = NULL;
  425. return path->query_id;
  426. }
  427. return 0;
  428. }
  429. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  430. {
  431. struct ipoib_dev_priv *priv = netdev_priv(dev);
  432. struct ipoib_path *path;
  433. struct ipoib_neigh *neigh;
  434. neigh = ipoib_neigh_alloc(skb->dst->neighbour, skb->dev);
  435. if (!neigh) {
  436. ++dev->stats.tx_dropped;
  437. dev_kfree_skb_any(skb);
  438. return;
  439. }
  440. /*
  441. * We can only be called from ipoib_start_xmit, so we're
  442. * inside tx_lock -- no need to save/restore flags.
  443. */
  444. spin_lock(&priv->lock);
  445. path = __path_find(dev, skb->dst->neighbour->ha + 4);
  446. if (!path) {
  447. path = path_rec_create(dev, skb->dst->neighbour->ha + 4);
  448. if (!path)
  449. goto err_path;
  450. __path_add(dev, path);
  451. }
  452. list_add_tail(&neigh->list, &path->neigh_list);
  453. if (path->ah) {
  454. kref_get(&path->ah->ref);
  455. neigh->ah = path->ah;
  456. memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
  457. sizeof(union ib_gid));
  458. if (ipoib_cm_enabled(dev, neigh->neighbour)) {
  459. if (!ipoib_cm_get(neigh))
  460. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  461. if (!ipoib_cm_get(neigh)) {
  462. list_del(&neigh->list);
  463. if (neigh->ah)
  464. ipoib_put_ah(neigh->ah);
  465. ipoib_neigh_free(dev, neigh);
  466. goto err_drop;
  467. }
  468. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  469. __skb_queue_tail(&neigh->queue, skb);
  470. else {
  471. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  472. skb_queue_len(&neigh->queue));
  473. goto err_drop;
  474. }
  475. } else
  476. ipoib_send(dev, skb, path->ah, IPOIB_QPN(skb->dst->neighbour->ha));
  477. } else {
  478. neigh->ah = NULL;
  479. if (!path->query && path_rec_start(dev, path))
  480. goto err_list;
  481. __skb_queue_tail(&neigh->queue, skb);
  482. }
  483. spin_unlock(&priv->lock);
  484. return;
  485. err_list:
  486. list_del(&neigh->list);
  487. err_path:
  488. ipoib_neigh_free(dev, neigh);
  489. err_drop:
  490. ++dev->stats.tx_dropped;
  491. dev_kfree_skb_any(skb);
  492. spin_unlock(&priv->lock);
  493. }
  494. static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
  495. {
  496. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  497. /* Look up path record for unicasts */
  498. if (skb->dst->neighbour->ha[4] != 0xff) {
  499. neigh_add_path(skb, dev);
  500. return;
  501. }
  502. /* Add in the P_Key for multicasts */
  503. skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
  504. skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
  505. ipoib_mcast_send(dev, skb->dst->neighbour->ha + 4, skb);
  506. }
  507. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  508. struct ipoib_pseudoheader *phdr)
  509. {
  510. struct ipoib_dev_priv *priv = netdev_priv(dev);
  511. struct ipoib_path *path;
  512. /*
  513. * We can only be called from ipoib_start_xmit, so we're
  514. * inside tx_lock -- no need to save/restore flags.
  515. */
  516. spin_lock(&priv->lock);
  517. path = __path_find(dev, phdr->hwaddr + 4);
  518. if (!path || !path->valid) {
  519. if (!path)
  520. path = path_rec_create(dev, phdr->hwaddr + 4);
  521. if (path) {
  522. /* put pseudoheader back on for next time */
  523. skb_push(skb, sizeof *phdr);
  524. __skb_queue_tail(&path->queue, skb);
  525. if (path_rec_start(dev, path)) {
  526. spin_unlock(&priv->lock);
  527. path_free(dev, path);
  528. return;
  529. } else
  530. __path_add(dev, path);
  531. } else {
  532. ++dev->stats.tx_dropped;
  533. dev_kfree_skb_any(skb);
  534. }
  535. spin_unlock(&priv->lock);
  536. return;
  537. }
  538. if (path->ah) {
  539. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  540. be16_to_cpu(path->pathrec.dlid));
  541. ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
  542. } else if ((path->query || !path_rec_start(dev, path)) &&
  543. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  544. /* put pseudoheader back on for next time */
  545. skb_push(skb, sizeof *phdr);
  546. __skb_queue_tail(&path->queue, skb);
  547. } else {
  548. ++dev->stats.tx_dropped;
  549. dev_kfree_skb_any(skb);
  550. }
  551. spin_unlock(&priv->lock);
  552. }
  553. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  554. {
  555. struct ipoib_dev_priv *priv = netdev_priv(dev);
  556. struct ipoib_neigh *neigh;
  557. unsigned long flags;
  558. if (unlikely(!spin_trylock_irqsave(&priv->tx_lock, flags)))
  559. return NETDEV_TX_LOCKED;
  560. if (likely(skb->dst && skb->dst->neighbour)) {
  561. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  562. ipoib_path_lookup(skb, dev);
  563. goto out;
  564. }
  565. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  566. if (neigh->ah)
  567. if (unlikely((memcmp(&neigh->dgid.raw,
  568. skb->dst->neighbour->ha + 4,
  569. sizeof(union ib_gid))) ||
  570. (neigh->dev != dev))) {
  571. spin_lock(&priv->lock);
  572. /*
  573. * It's safe to call ipoib_put_ah() inside
  574. * priv->lock here, because we know that
  575. * path->ah will always hold one more reference,
  576. * so ipoib_put_ah() will never do more than
  577. * decrement the ref count.
  578. */
  579. ipoib_put_ah(neigh->ah);
  580. list_del(&neigh->list);
  581. ipoib_neigh_free(dev, neigh);
  582. spin_unlock(&priv->lock);
  583. ipoib_path_lookup(skb, dev);
  584. goto out;
  585. }
  586. if (ipoib_cm_get(neigh)) {
  587. if (ipoib_cm_up(neigh)) {
  588. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  589. goto out;
  590. }
  591. } else if (neigh->ah) {
  592. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(skb->dst->neighbour->ha));
  593. goto out;
  594. }
  595. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  596. spin_lock(&priv->lock);
  597. __skb_queue_tail(&neigh->queue, skb);
  598. spin_unlock(&priv->lock);
  599. } else {
  600. ++dev->stats.tx_dropped;
  601. dev_kfree_skb_any(skb);
  602. }
  603. } else {
  604. struct ipoib_pseudoheader *phdr =
  605. (struct ipoib_pseudoheader *) skb->data;
  606. skb_pull(skb, sizeof *phdr);
  607. if (phdr->hwaddr[4] == 0xff) {
  608. /* Add in the P_Key for multicast*/
  609. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  610. phdr->hwaddr[9] = priv->pkey & 0xff;
  611. ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
  612. } else {
  613. /* unicast GID -- should be ARP or RARP reply */
  614. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  615. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  616. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
  617. IPOIB_GID_FMT "\n",
  618. skb->dst ? "neigh" : "dst",
  619. be16_to_cpup((__be16 *) skb->data),
  620. IPOIB_QPN(phdr->hwaddr),
  621. IPOIB_GID_RAW_ARG(phdr->hwaddr + 4));
  622. dev_kfree_skb_any(skb);
  623. ++dev->stats.tx_dropped;
  624. goto out;
  625. }
  626. unicast_arp_send(skb, dev, phdr);
  627. }
  628. }
  629. out:
  630. spin_unlock_irqrestore(&priv->tx_lock, flags);
  631. return NETDEV_TX_OK;
  632. }
  633. static void ipoib_timeout(struct net_device *dev)
  634. {
  635. struct ipoib_dev_priv *priv = netdev_priv(dev);
  636. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  637. jiffies_to_msecs(jiffies - dev->trans_start));
  638. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  639. netif_queue_stopped(dev),
  640. priv->tx_head, priv->tx_tail);
  641. /* XXX reset QP, etc. */
  642. }
  643. static int ipoib_hard_header(struct sk_buff *skb,
  644. struct net_device *dev,
  645. unsigned short type,
  646. const void *daddr, const void *saddr, unsigned len)
  647. {
  648. struct ipoib_header *header;
  649. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  650. header->proto = htons(type);
  651. header->reserved = 0;
  652. /*
  653. * If we don't have a neighbour structure, stuff the
  654. * destination address onto the front of the skb so we can
  655. * figure out where to send the packet later.
  656. */
  657. if ((!skb->dst || !skb->dst->neighbour) && daddr) {
  658. struct ipoib_pseudoheader *phdr =
  659. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  660. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  661. }
  662. return 0;
  663. }
  664. static void ipoib_set_mcast_list(struct net_device *dev)
  665. {
  666. struct ipoib_dev_priv *priv = netdev_priv(dev);
  667. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  668. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  669. return;
  670. }
  671. queue_work(ipoib_workqueue, &priv->restart_task);
  672. }
  673. static void ipoib_neigh_cleanup(struct neighbour *n)
  674. {
  675. struct ipoib_neigh *neigh;
  676. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  677. unsigned long flags;
  678. struct ipoib_ah *ah = NULL;
  679. neigh = *to_ipoib_neigh(n);
  680. if (neigh)
  681. priv = netdev_priv(neigh->dev);
  682. else
  683. return;
  684. ipoib_dbg(priv,
  685. "neigh_cleanup for %06x " IPOIB_GID_FMT "\n",
  686. IPOIB_QPN(n->ha),
  687. IPOIB_GID_RAW_ARG(n->ha + 4));
  688. spin_lock_irqsave(&priv->lock, flags);
  689. if (neigh->ah)
  690. ah = neigh->ah;
  691. list_del(&neigh->list);
  692. ipoib_neigh_free(n->dev, neigh);
  693. spin_unlock_irqrestore(&priv->lock, flags);
  694. if (ah)
  695. ipoib_put_ah(ah);
  696. }
  697. struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
  698. struct net_device *dev)
  699. {
  700. struct ipoib_neigh *neigh;
  701. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  702. if (!neigh)
  703. return NULL;
  704. neigh->neighbour = neighbour;
  705. neigh->dev = dev;
  706. *to_ipoib_neigh(neighbour) = neigh;
  707. skb_queue_head_init(&neigh->queue);
  708. ipoib_cm_set(neigh, NULL);
  709. return neigh;
  710. }
  711. void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
  712. {
  713. struct sk_buff *skb;
  714. *to_ipoib_neigh(neigh->neighbour) = NULL;
  715. while ((skb = __skb_dequeue(&neigh->queue))) {
  716. ++dev->stats.tx_dropped;
  717. dev_kfree_skb_any(skb);
  718. }
  719. if (ipoib_cm_get(neigh))
  720. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  721. kfree(neigh);
  722. }
  723. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  724. {
  725. parms->neigh_cleanup = ipoib_neigh_cleanup;
  726. return 0;
  727. }
  728. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  729. {
  730. struct ipoib_dev_priv *priv = netdev_priv(dev);
  731. /* Allocate RX/TX "rings" to hold queued skbs */
  732. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  733. GFP_KERNEL);
  734. if (!priv->rx_ring) {
  735. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  736. ca->name, ipoib_recvq_size);
  737. goto out;
  738. }
  739. priv->tx_ring = vmalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  740. if (!priv->tx_ring) {
  741. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  742. ca->name, ipoib_sendq_size);
  743. goto out_rx_ring_cleanup;
  744. }
  745. memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
  746. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  747. if (ipoib_ib_dev_init(dev, ca, port))
  748. goto out_tx_ring_cleanup;
  749. return 0;
  750. out_tx_ring_cleanup:
  751. vfree(priv->tx_ring);
  752. out_rx_ring_cleanup:
  753. kfree(priv->rx_ring);
  754. out:
  755. return -ENOMEM;
  756. }
  757. void ipoib_dev_cleanup(struct net_device *dev)
  758. {
  759. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  760. ipoib_delete_debug_files(dev);
  761. /* Delete any child interfaces first */
  762. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  763. unregister_netdev(cpriv->dev);
  764. ipoib_dev_cleanup(cpriv->dev);
  765. free_netdev(cpriv->dev);
  766. }
  767. ipoib_ib_dev_cleanup(dev);
  768. kfree(priv->rx_ring);
  769. vfree(priv->tx_ring);
  770. priv->rx_ring = NULL;
  771. priv->tx_ring = NULL;
  772. }
  773. static const struct header_ops ipoib_header_ops = {
  774. .create = ipoib_hard_header,
  775. };
  776. static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
  777. void **tcph, u64 *hdr_flags, void *priv)
  778. {
  779. unsigned int ip_len;
  780. struct iphdr *iph;
  781. if (unlikely(skb->protocol != htons(ETH_P_IP)))
  782. return -1;
  783. /*
  784. * In the future we may add an else clause that verifies the
  785. * checksum and allows devices which do not calculate checksum
  786. * to use LRO.
  787. */
  788. if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY))
  789. return -1;
  790. /* Check for non-TCP packet */
  791. skb_reset_network_header(skb);
  792. iph = ip_hdr(skb);
  793. if (iph->protocol != IPPROTO_TCP)
  794. return -1;
  795. ip_len = ip_hdrlen(skb);
  796. skb_set_transport_header(skb, ip_len);
  797. *tcph = tcp_hdr(skb);
  798. /* check if IP header and TCP header are complete */
  799. if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
  800. return -1;
  801. *hdr_flags = LRO_IPV4 | LRO_TCP;
  802. *iphdr = iph;
  803. return 0;
  804. }
  805. static void ipoib_lro_setup(struct ipoib_dev_priv *priv)
  806. {
  807. priv->lro.lro_mgr.max_aggr = lro_max_aggr;
  808. priv->lro.lro_mgr.max_desc = IPOIB_MAX_LRO_DESCRIPTORS;
  809. priv->lro.lro_mgr.lro_arr = priv->lro.lro_desc;
  810. priv->lro.lro_mgr.get_skb_header = get_skb_hdr;
  811. priv->lro.lro_mgr.features = LRO_F_NAPI;
  812. priv->lro.lro_mgr.dev = priv->dev;
  813. priv->lro.lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  814. }
  815. static void ipoib_setup(struct net_device *dev)
  816. {
  817. struct ipoib_dev_priv *priv = netdev_priv(dev);
  818. dev->open = ipoib_open;
  819. dev->stop = ipoib_stop;
  820. dev->change_mtu = ipoib_change_mtu;
  821. dev->hard_start_xmit = ipoib_start_xmit;
  822. dev->tx_timeout = ipoib_timeout;
  823. dev->header_ops = &ipoib_header_ops;
  824. dev->set_multicast_list = ipoib_set_mcast_list;
  825. dev->neigh_setup = ipoib_neigh_setup_dev;
  826. ipoib_set_ethtool_ops(dev);
  827. netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
  828. dev->watchdog_timeo = HZ;
  829. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  830. /*
  831. * We add in INFINIBAND_ALEN to allow for the destination
  832. * address "pseudoheader" for skbs without neighbour struct.
  833. */
  834. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  835. dev->addr_len = INFINIBAND_ALEN;
  836. dev->type = ARPHRD_INFINIBAND;
  837. dev->tx_queue_len = ipoib_sendq_size * 2;
  838. dev->features = (NETIF_F_VLAN_CHALLENGED |
  839. NETIF_F_LLTX |
  840. NETIF_F_HIGHDMA);
  841. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  842. netif_carrier_off(dev);
  843. priv->dev = dev;
  844. ipoib_lro_setup(priv);
  845. spin_lock_init(&priv->lock);
  846. spin_lock_init(&priv->tx_lock);
  847. mutex_init(&priv->vlan_mutex);
  848. INIT_LIST_HEAD(&priv->path_list);
  849. INIT_LIST_HEAD(&priv->child_intfs);
  850. INIT_LIST_HEAD(&priv->dead_ahs);
  851. INIT_LIST_HEAD(&priv->multicast_list);
  852. INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
  853. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  854. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  855. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  856. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  857. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  858. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  859. }
  860. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  861. {
  862. struct net_device *dev;
  863. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  864. ipoib_setup);
  865. if (!dev)
  866. return NULL;
  867. return netdev_priv(dev);
  868. }
  869. static ssize_t show_pkey(struct device *dev,
  870. struct device_attribute *attr, char *buf)
  871. {
  872. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  873. return sprintf(buf, "0x%04x\n", priv->pkey);
  874. }
  875. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  876. static ssize_t show_umcast(struct device *dev,
  877. struct device_attribute *attr, char *buf)
  878. {
  879. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  880. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  881. }
  882. static ssize_t set_umcast(struct device *dev,
  883. struct device_attribute *attr,
  884. const char *buf, size_t count)
  885. {
  886. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  887. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  888. if (umcast_val > 0) {
  889. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  890. ipoib_warn(priv, "ignoring multicast groups joined directly "
  891. "by userspace\n");
  892. } else
  893. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  894. return count;
  895. }
  896. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  897. int ipoib_add_umcast_attr(struct net_device *dev)
  898. {
  899. return device_create_file(&dev->dev, &dev_attr_umcast);
  900. }
  901. static ssize_t create_child(struct device *dev,
  902. struct device_attribute *attr,
  903. const char *buf, size_t count)
  904. {
  905. int pkey;
  906. int ret;
  907. if (sscanf(buf, "%i", &pkey) != 1)
  908. return -EINVAL;
  909. if (pkey < 0 || pkey > 0xffff)
  910. return -EINVAL;
  911. /*
  912. * Set the full membership bit, so that we join the right
  913. * broadcast group, etc.
  914. */
  915. pkey |= 0x8000;
  916. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  917. return ret ? ret : count;
  918. }
  919. static DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  920. static ssize_t delete_child(struct device *dev,
  921. struct device_attribute *attr,
  922. const char *buf, size_t count)
  923. {
  924. int pkey;
  925. int ret;
  926. if (sscanf(buf, "%i", &pkey) != 1)
  927. return -EINVAL;
  928. if (pkey < 0 || pkey > 0xffff)
  929. return -EINVAL;
  930. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  931. return ret ? ret : count;
  932. }
  933. static DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  934. int ipoib_add_pkey_attr(struct net_device *dev)
  935. {
  936. return device_create_file(&dev->dev, &dev_attr_pkey);
  937. }
  938. static struct net_device *ipoib_add_port(const char *format,
  939. struct ib_device *hca, u8 port)
  940. {
  941. struct ipoib_dev_priv *priv;
  942. struct ib_device_attr *device_attr;
  943. struct ib_port_attr attr;
  944. int result = -ENOMEM;
  945. priv = ipoib_intf_alloc(format);
  946. if (!priv)
  947. goto alloc_mem_failed;
  948. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  949. if (!ib_query_port(hca, port, &attr))
  950. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  951. else {
  952. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  953. hca->name, port);
  954. goto device_init_failed;
  955. }
  956. /* MTU will be reset when mcast join happens */
  957. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  958. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  959. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  960. if (result) {
  961. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  962. hca->name, port, result);
  963. goto device_init_failed;
  964. }
  965. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  966. if (!device_attr) {
  967. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  968. hca->name, sizeof *device_attr);
  969. goto device_init_failed;
  970. }
  971. result = ib_query_device(hca, device_attr);
  972. if (result) {
  973. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  974. hca->name, result);
  975. kfree(device_attr);
  976. goto device_init_failed;
  977. }
  978. priv->hca_caps = device_attr->device_cap_flags;
  979. kfree(device_attr);
  980. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  981. set_bit(IPOIB_FLAG_CSUM, &priv->flags);
  982. priv->dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
  983. }
  984. if (lro)
  985. priv->dev->features |= NETIF_F_LRO;
  986. /*
  987. * Set the full membership bit, so that we join the right
  988. * broadcast group, etc.
  989. */
  990. priv->pkey |= 0x8000;
  991. priv->dev->broadcast[8] = priv->pkey >> 8;
  992. priv->dev->broadcast[9] = priv->pkey & 0xff;
  993. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  994. if (result) {
  995. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  996. hca->name, port, result);
  997. goto device_init_failed;
  998. } else
  999. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  1000. result = ipoib_dev_init(priv->dev, hca, port);
  1001. if (result < 0) {
  1002. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1003. hca->name, port, result);
  1004. goto device_init_failed;
  1005. }
  1006. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1007. priv->ca, ipoib_event);
  1008. result = ib_register_event_handler(&priv->event_handler);
  1009. if (result < 0) {
  1010. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1011. "port %d (ret = %d)\n",
  1012. hca->name, port, result);
  1013. goto event_failed;
  1014. }
  1015. if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO)
  1016. priv->dev->features |= NETIF_F_TSO;
  1017. result = register_netdev(priv->dev);
  1018. if (result) {
  1019. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1020. hca->name, port, result);
  1021. goto register_failed;
  1022. }
  1023. ipoib_create_debug_files(priv->dev);
  1024. if (ipoib_cm_add_mode_attr(priv->dev))
  1025. goto sysfs_failed;
  1026. if (ipoib_add_pkey_attr(priv->dev))
  1027. goto sysfs_failed;
  1028. if (ipoib_add_umcast_attr(priv->dev))
  1029. goto sysfs_failed;
  1030. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1031. goto sysfs_failed;
  1032. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1033. goto sysfs_failed;
  1034. return priv->dev;
  1035. sysfs_failed:
  1036. ipoib_delete_debug_files(priv->dev);
  1037. unregister_netdev(priv->dev);
  1038. register_failed:
  1039. ib_unregister_event_handler(&priv->event_handler);
  1040. flush_workqueue(ipoib_workqueue);
  1041. event_failed:
  1042. ipoib_dev_cleanup(priv->dev);
  1043. device_init_failed:
  1044. free_netdev(priv->dev);
  1045. alloc_mem_failed:
  1046. return ERR_PTR(result);
  1047. }
  1048. static void ipoib_add_one(struct ib_device *device)
  1049. {
  1050. struct list_head *dev_list;
  1051. struct net_device *dev;
  1052. struct ipoib_dev_priv *priv;
  1053. int s, e, p;
  1054. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1055. return;
  1056. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1057. if (!dev_list)
  1058. return;
  1059. INIT_LIST_HEAD(dev_list);
  1060. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1061. s = 0;
  1062. e = 0;
  1063. } else {
  1064. s = 1;
  1065. e = device->phys_port_cnt;
  1066. }
  1067. for (p = s; p <= e; ++p) {
  1068. dev = ipoib_add_port("ib%d", device, p);
  1069. if (!IS_ERR(dev)) {
  1070. priv = netdev_priv(dev);
  1071. list_add_tail(&priv->list, dev_list);
  1072. }
  1073. }
  1074. ib_set_client_data(device, &ipoib_client, dev_list);
  1075. }
  1076. static void ipoib_remove_one(struct ib_device *device)
  1077. {
  1078. struct ipoib_dev_priv *priv, *tmp;
  1079. struct list_head *dev_list;
  1080. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1081. return;
  1082. dev_list = ib_get_client_data(device, &ipoib_client);
  1083. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1084. ib_unregister_event_handler(&priv->event_handler);
  1085. rtnl_lock();
  1086. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1087. rtnl_unlock();
  1088. flush_workqueue(ipoib_workqueue);
  1089. unregister_netdev(priv->dev);
  1090. ipoib_dev_cleanup(priv->dev);
  1091. free_netdev(priv->dev);
  1092. }
  1093. kfree(dev_list);
  1094. }
  1095. static int __init ipoib_init_module(void)
  1096. {
  1097. int ret;
  1098. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1099. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1100. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1101. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1102. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1103. ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
  1104. IPOIB_MIN_QUEUE_SIZE));
  1105. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1106. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1107. #endif
  1108. /*
  1109. * When copying small received packets, we only copy from the
  1110. * linear data part of the SKB, so we rely on this condition.
  1111. */
  1112. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1113. ret = ipoib_register_debugfs();
  1114. if (ret)
  1115. return ret;
  1116. /*
  1117. * We create our own workqueue mainly because we want to be
  1118. * able to flush it when devices are being removed. We can't
  1119. * use schedule_work()/flush_scheduled_work() because both
  1120. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1121. * so flush_scheduled_work() can deadlock during device
  1122. * removal.
  1123. */
  1124. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1125. if (!ipoib_workqueue) {
  1126. ret = -ENOMEM;
  1127. goto err_fs;
  1128. }
  1129. ib_sa_register_client(&ipoib_sa_client);
  1130. ret = ib_register_client(&ipoib_client);
  1131. if (ret)
  1132. goto err_sa;
  1133. return 0;
  1134. err_sa:
  1135. ib_sa_unregister_client(&ipoib_sa_client);
  1136. destroy_workqueue(ipoib_workqueue);
  1137. err_fs:
  1138. ipoib_unregister_debugfs();
  1139. return ret;
  1140. }
  1141. static void __exit ipoib_cleanup_module(void)
  1142. {
  1143. ib_unregister_client(&ipoib_client);
  1144. ib_sa_unregister_client(&ipoib_sa_client);
  1145. ipoib_unregister_debugfs();
  1146. destroy_workqueue(ipoib_workqueue);
  1147. }
  1148. module_init(ipoib_init_module);
  1149. module_exit(ipoib_cleanup_module);