ipoib_main.c 29 KB

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