ipoib_main.c 28 KB

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