ipoib_main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. *to_ipoib_neigh(neigh->neighbour) = NULL;
  199. kfree(neigh);
  200. }
  201. spin_unlock_irqrestore(&priv->lock, flags);
  202. if (path->ah)
  203. ipoib_put_ah(path->ah);
  204. kfree(path);
  205. }
  206. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  207. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  208. {
  209. struct ipoib_path_iter *iter;
  210. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  211. if (!iter)
  212. return NULL;
  213. iter->dev = dev;
  214. memset(iter->path.pathrec.dgid.raw, 0, 16);
  215. if (ipoib_path_iter_next(iter)) {
  216. kfree(iter);
  217. return NULL;
  218. }
  219. return iter;
  220. }
  221. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  222. {
  223. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  224. struct rb_node *n;
  225. struct ipoib_path *path;
  226. int ret = 1;
  227. spin_lock_irq(&priv->lock);
  228. n = rb_first(&priv->path_tree);
  229. while (n) {
  230. path = rb_entry(n, struct ipoib_path, rb_node);
  231. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  232. sizeof (union ib_gid)) < 0) {
  233. iter->path = *path;
  234. ret = 0;
  235. break;
  236. }
  237. n = rb_next(n);
  238. }
  239. spin_unlock_irq(&priv->lock);
  240. return ret;
  241. }
  242. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  243. struct ipoib_path *path)
  244. {
  245. *path = iter->path;
  246. }
  247. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  248. void ipoib_flush_paths(struct net_device *dev)
  249. {
  250. struct ipoib_dev_priv *priv = netdev_priv(dev);
  251. struct ipoib_path *path, *tp;
  252. LIST_HEAD(remove_list);
  253. unsigned long flags;
  254. spin_lock_irqsave(&priv->lock, flags);
  255. list_splice(&priv->path_list, &remove_list);
  256. INIT_LIST_HEAD(&priv->path_list);
  257. list_for_each_entry(path, &remove_list, list)
  258. rb_erase(&path->rb_node, &priv->path_tree);
  259. spin_unlock_irqrestore(&priv->lock, flags);
  260. list_for_each_entry_safe(path, tp, &remove_list, list) {
  261. if (path->query)
  262. ib_sa_cancel_query(path->query_id, path->query);
  263. wait_for_completion(&path->done);
  264. path_free(dev, path);
  265. }
  266. }
  267. static void path_rec_completion(int status,
  268. struct ib_sa_path_rec *pathrec,
  269. void *path_ptr)
  270. {
  271. struct ipoib_path *path = path_ptr;
  272. struct net_device *dev = path->dev;
  273. struct ipoib_dev_priv *priv = netdev_priv(dev);
  274. struct ipoib_ah *ah = NULL;
  275. struct ipoib_neigh *neigh;
  276. struct sk_buff_head skqueue;
  277. struct sk_buff *skb;
  278. unsigned long flags;
  279. if (pathrec)
  280. ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
  281. be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
  282. else
  283. ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
  284. status, IPOIB_GID_ARG(path->pathrec.dgid));
  285. skb_queue_head_init(&skqueue);
  286. if (!status) {
  287. struct ib_ah_attr av = {
  288. .dlid = be16_to_cpu(pathrec->dlid),
  289. .sl = pathrec->sl,
  290. .port_num = priv->port
  291. };
  292. int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
  293. if (path_rate > 0 && priv->local_rate > path_rate)
  294. av.static_rate = (priv->local_rate - 1) / path_rate;
  295. ipoib_dbg(priv, "static_rate %d for local port %dX, path %dX\n",
  296. av.static_rate, priv->local_rate,
  297. ib_sa_rate_enum_to_int(pathrec->rate));
  298. ah = ipoib_create_ah(dev, priv->pd, &av);
  299. }
  300. spin_lock_irqsave(&priv->lock, flags);
  301. path->ah = ah;
  302. if (ah) {
  303. path->pathrec = *pathrec;
  304. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  305. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  306. while ((skb = __skb_dequeue(&path->queue)))
  307. __skb_queue_tail(&skqueue, skb);
  308. list_for_each_entry(neigh, &path->neigh_list, list) {
  309. kref_get(&path->ah->ref);
  310. neigh->ah = path->ah;
  311. while ((skb = __skb_dequeue(&neigh->queue)))
  312. __skb_queue_tail(&skqueue, skb);
  313. }
  314. }
  315. path->query = NULL;
  316. complete(&path->done);
  317. spin_unlock_irqrestore(&priv->lock, flags);
  318. while ((skb = __skb_dequeue(&skqueue))) {
  319. skb->dev = dev;
  320. if (dev_queue_xmit(skb))
  321. ipoib_warn(priv, "dev_queue_xmit failed "
  322. "to requeue packet\n");
  323. }
  324. }
  325. static struct ipoib_path *path_rec_create(struct net_device *dev,
  326. union ib_gid *gid)
  327. {
  328. struct ipoib_dev_priv *priv = netdev_priv(dev);
  329. struct ipoib_path *path;
  330. path = kzalloc(sizeof *path, GFP_ATOMIC);
  331. if (!path)
  332. return NULL;
  333. path->dev = dev;
  334. skb_queue_head_init(&path->queue);
  335. INIT_LIST_HEAD(&path->neigh_list);
  336. memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
  337. path->pathrec.sgid = priv->local_gid;
  338. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  339. path->pathrec.numb_path = 1;
  340. return path;
  341. }
  342. static int path_rec_start(struct net_device *dev,
  343. struct ipoib_path *path)
  344. {
  345. struct ipoib_dev_priv *priv = netdev_priv(dev);
  346. ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
  347. IPOIB_GID_ARG(path->pathrec.dgid));
  348. init_completion(&path->done);
  349. path->query_id =
  350. ib_sa_path_rec_get(priv->ca, priv->port,
  351. &path->pathrec,
  352. IB_SA_PATH_REC_DGID |
  353. IB_SA_PATH_REC_SGID |
  354. IB_SA_PATH_REC_NUMB_PATH |
  355. IB_SA_PATH_REC_PKEY,
  356. 1000, GFP_ATOMIC,
  357. path_rec_completion,
  358. path, &path->query);
  359. if (path->query_id < 0) {
  360. ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
  361. path->query = NULL;
  362. return path->query_id;
  363. }
  364. return 0;
  365. }
  366. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  367. {
  368. struct ipoib_dev_priv *priv = netdev_priv(dev);
  369. struct ipoib_path *path;
  370. struct ipoib_neigh *neigh;
  371. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  372. if (!neigh) {
  373. ++priv->stats.tx_dropped;
  374. dev_kfree_skb_any(skb);
  375. return;
  376. }
  377. skb_queue_head_init(&neigh->queue);
  378. neigh->neighbour = skb->dst->neighbour;
  379. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  380. /*
  381. * We can only be called from ipoib_start_xmit, so we're
  382. * inside tx_lock -- no need to save/restore flags.
  383. */
  384. spin_lock(&priv->lock);
  385. path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
  386. if (!path) {
  387. path = path_rec_create(dev,
  388. (union ib_gid *) (skb->dst->neighbour->ha + 4));
  389. if (!path)
  390. goto err;
  391. __path_add(dev, path);
  392. }
  393. list_add_tail(&neigh->list, &path->neigh_list);
  394. if (path->ah) {
  395. kref_get(&path->ah->ref);
  396. neigh->ah = path->ah;
  397. ipoib_send(dev, skb, path->ah,
  398. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  399. } else {
  400. neigh->ah = NULL;
  401. __skb_queue_tail(&neigh->queue, skb);
  402. if (!path->query && path_rec_start(dev, path))
  403. goto err;
  404. }
  405. spin_unlock(&priv->lock);
  406. return;
  407. err:
  408. *to_ipoib_neigh(skb->dst->neighbour) = NULL;
  409. list_del(&neigh->list);
  410. kfree(neigh);
  411. ++priv->stats.tx_dropped;
  412. dev_kfree_skb_any(skb);
  413. spin_unlock(&priv->lock);
  414. }
  415. static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
  416. {
  417. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  418. /* Look up path record for unicasts */
  419. if (skb->dst->neighbour->ha[4] != 0xff) {
  420. neigh_add_path(skb, dev);
  421. return;
  422. }
  423. /* Add in the P_Key for multicasts */
  424. skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
  425. skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
  426. ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
  427. }
  428. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  429. struct ipoib_pseudoheader *phdr)
  430. {
  431. struct ipoib_dev_priv *priv = netdev_priv(dev);
  432. struct ipoib_path *path;
  433. /*
  434. * We can only be called from ipoib_start_xmit, so we're
  435. * inside tx_lock -- no need to save/restore flags.
  436. */
  437. spin_lock(&priv->lock);
  438. path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
  439. if (!path) {
  440. path = path_rec_create(dev,
  441. (union ib_gid *) (phdr->hwaddr + 4));
  442. if (path) {
  443. /* put pseudoheader back on for next time */
  444. skb_push(skb, sizeof *phdr);
  445. __skb_queue_tail(&path->queue, skb);
  446. if (path_rec_start(dev, path)) {
  447. spin_unlock(&priv->lock);
  448. path_free(dev, path);
  449. return;
  450. } else
  451. __path_add(dev, path);
  452. } else {
  453. ++priv->stats.tx_dropped;
  454. dev_kfree_skb_any(skb);
  455. }
  456. spin_unlock(&priv->lock);
  457. return;
  458. }
  459. if (path->ah) {
  460. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  461. be16_to_cpu(path->pathrec.dlid));
  462. ipoib_send(dev, skb, path->ah,
  463. be32_to_cpup((__be32 *) phdr->hwaddr));
  464. } else if ((path->query || !path_rec_start(dev, path)) &&
  465. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  466. /* put pseudoheader back on for next time */
  467. skb_push(skb, sizeof *phdr);
  468. __skb_queue_tail(&path->queue, skb);
  469. } else {
  470. ++priv->stats.tx_dropped;
  471. dev_kfree_skb_any(skb);
  472. }
  473. spin_unlock(&priv->lock);
  474. }
  475. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  476. {
  477. struct ipoib_dev_priv *priv = netdev_priv(dev);
  478. struct ipoib_neigh *neigh;
  479. unsigned long flags;
  480. if (!spin_trylock_irqsave(&priv->tx_lock, flags))
  481. return NETDEV_TX_LOCKED;
  482. /*
  483. * Check if our queue is stopped. Since we have the LLTX bit
  484. * set, we can't rely on netif_stop_queue() preventing our
  485. * xmit function from being called with a full queue.
  486. */
  487. if (unlikely(netif_queue_stopped(dev))) {
  488. spin_unlock_irqrestore(&priv->tx_lock, flags);
  489. return NETDEV_TX_BUSY;
  490. }
  491. if (skb->dst && skb->dst->neighbour) {
  492. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  493. ipoib_path_lookup(skb, dev);
  494. goto out;
  495. }
  496. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  497. if (likely(neigh->ah)) {
  498. ipoib_send(dev, skb, neigh->ah,
  499. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  500. goto out;
  501. }
  502. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  503. spin_lock(&priv->lock);
  504. __skb_queue_tail(&neigh->queue, skb);
  505. spin_unlock(&priv->lock);
  506. } else {
  507. ++priv->stats.tx_dropped;
  508. dev_kfree_skb_any(skb);
  509. }
  510. } else {
  511. struct ipoib_pseudoheader *phdr =
  512. (struct ipoib_pseudoheader *) skb->data;
  513. skb_pull(skb, sizeof *phdr);
  514. if (phdr->hwaddr[4] == 0xff) {
  515. /* Add in the P_Key for multicast*/
  516. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  517. phdr->hwaddr[9] = priv->pkey & 0xff;
  518. ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
  519. } else {
  520. /* unicast GID -- should be ARP or RARP reply */
  521. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  522. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  523. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
  524. IPOIB_GID_FMT "\n",
  525. skb->dst ? "neigh" : "dst",
  526. be16_to_cpup((__be16 *) skb->data),
  527. be32_to_cpup((__be32 *) phdr->hwaddr),
  528. IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
  529. dev_kfree_skb_any(skb);
  530. ++priv->stats.tx_dropped;
  531. goto out;
  532. }
  533. unicast_arp_send(skb, dev, phdr);
  534. }
  535. }
  536. out:
  537. spin_unlock_irqrestore(&priv->tx_lock, flags);
  538. return NETDEV_TX_OK;
  539. }
  540. static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
  541. {
  542. struct ipoib_dev_priv *priv = netdev_priv(dev);
  543. return &priv->stats;
  544. }
  545. static void ipoib_timeout(struct net_device *dev)
  546. {
  547. struct ipoib_dev_priv *priv = netdev_priv(dev);
  548. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  549. jiffies_to_msecs(jiffies - dev->trans_start));
  550. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  551. netif_queue_stopped(dev),
  552. priv->tx_head, priv->tx_tail);
  553. /* XXX reset QP, etc. */
  554. }
  555. static int ipoib_hard_header(struct sk_buff *skb,
  556. struct net_device *dev,
  557. unsigned short type,
  558. void *daddr, void *saddr, unsigned len)
  559. {
  560. struct ipoib_header *header;
  561. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  562. header->proto = htons(type);
  563. header->reserved = 0;
  564. /*
  565. * If we don't have a neighbour structure, stuff the
  566. * destination address onto the front of the skb so we can
  567. * figure out where to send the packet later.
  568. */
  569. if (!skb->dst || !skb->dst->neighbour) {
  570. struct ipoib_pseudoheader *phdr =
  571. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  572. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  573. }
  574. return 0;
  575. }
  576. static void ipoib_set_mcast_list(struct net_device *dev)
  577. {
  578. struct ipoib_dev_priv *priv = netdev_priv(dev);
  579. queue_work(ipoib_workqueue, &priv->restart_task);
  580. }
  581. static void ipoib_neigh_destructor(struct neighbour *n)
  582. {
  583. struct ipoib_neigh *neigh;
  584. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  585. unsigned long flags;
  586. struct ipoib_ah *ah = NULL;
  587. ipoib_dbg(priv,
  588. "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
  589. be32_to_cpup((__be32 *) n->ha),
  590. IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
  591. spin_lock_irqsave(&priv->lock, flags);
  592. neigh = *to_ipoib_neigh(n);
  593. if (neigh) {
  594. if (neigh->ah)
  595. ah = neigh->ah;
  596. list_del(&neigh->list);
  597. *to_ipoib_neigh(n) = NULL;
  598. kfree(neigh);
  599. }
  600. spin_unlock_irqrestore(&priv->lock, flags);
  601. if (ah)
  602. ipoib_put_ah(ah);
  603. }
  604. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  605. {
  606. parms->neigh_destructor = ipoib_neigh_destructor;
  607. return 0;
  608. }
  609. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  610. {
  611. struct ipoib_dev_priv *priv = netdev_priv(dev);
  612. /* Allocate RX/TX "rings" to hold queued skbs */
  613. priv->rx_ring = kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
  614. GFP_KERNEL);
  615. if (!priv->rx_ring) {
  616. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  617. ca->name, IPOIB_RX_RING_SIZE);
  618. goto out;
  619. }
  620. priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
  621. GFP_KERNEL);
  622. if (!priv->tx_ring) {
  623. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  624. ca->name, IPOIB_TX_RING_SIZE);
  625. goto out_rx_ring_cleanup;
  626. }
  627. /* priv->tx_head & tx_tail are already 0 */
  628. if (ipoib_ib_dev_init(dev, ca, port))
  629. goto out_tx_ring_cleanup;
  630. return 0;
  631. out_tx_ring_cleanup:
  632. kfree(priv->tx_ring);
  633. out_rx_ring_cleanup:
  634. kfree(priv->rx_ring);
  635. out:
  636. return -ENOMEM;
  637. }
  638. void ipoib_dev_cleanup(struct net_device *dev)
  639. {
  640. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  641. ipoib_delete_debug_files(dev);
  642. /* Delete any child interfaces first */
  643. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  644. unregister_netdev(cpriv->dev);
  645. ipoib_dev_cleanup(cpriv->dev);
  646. free_netdev(cpriv->dev);
  647. }
  648. ipoib_ib_dev_cleanup(dev);
  649. kfree(priv->rx_ring);
  650. kfree(priv->tx_ring);
  651. priv->rx_ring = NULL;
  652. priv->tx_ring = NULL;
  653. }
  654. static void ipoib_setup(struct net_device *dev)
  655. {
  656. struct ipoib_dev_priv *priv = netdev_priv(dev);
  657. dev->open = ipoib_open;
  658. dev->stop = ipoib_stop;
  659. dev->change_mtu = ipoib_change_mtu;
  660. dev->hard_start_xmit = ipoib_start_xmit;
  661. dev->get_stats = ipoib_get_stats;
  662. dev->tx_timeout = ipoib_timeout;
  663. dev->hard_header = ipoib_hard_header;
  664. dev->set_multicast_list = ipoib_set_mcast_list;
  665. dev->neigh_setup = ipoib_neigh_setup_dev;
  666. dev->watchdog_timeo = HZ;
  667. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  668. /*
  669. * We add in INFINIBAND_ALEN to allow for the destination
  670. * address "pseudoheader" for skbs without neighbour struct.
  671. */
  672. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  673. dev->addr_len = INFINIBAND_ALEN;
  674. dev->type = ARPHRD_INFINIBAND;
  675. dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
  676. dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
  677. /* MTU will be reset when mcast join happens */
  678. dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
  679. priv->mcast_mtu = priv->admin_mtu = dev->mtu;
  680. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  681. netif_carrier_off(dev);
  682. SET_MODULE_OWNER(dev);
  683. priv->dev = dev;
  684. spin_lock_init(&priv->lock);
  685. spin_lock_init(&priv->tx_lock);
  686. mutex_init(&priv->mcast_mutex);
  687. mutex_init(&priv->vlan_mutex);
  688. INIT_LIST_HEAD(&priv->path_list);
  689. INIT_LIST_HEAD(&priv->child_intfs);
  690. INIT_LIST_HEAD(&priv->dead_ahs);
  691. INIT_LIST_HEAD(&priv->multicast_list);
  692. INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
  693. INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
  694. INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
  695. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
  696. INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
  697. }
  698. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  699. {
  700. struct net_device *dev;
  701. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  702. ipoib_setup);
  703. if (!dev)
  704. return NULL;
  705. return netdev_priv(dev);
  706. }
  707. static ssize_t show_pkey(struct class_device *cdev, char *buf)
  708. {
  709. struct ipoib_dev_priv *priv =
  710. netdev_priv(container_of(cdev, struct net_device, class_dev));
  711. return sprintf(buf, "0x%04x\n", priv->pkey);
  712. }
  713. static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  714. static ssize_t create_child(struct class_device *cdev,
  715. const char *buf, size_t count)
  716. {
  717. int pkey;
  718. int ret;
  719. if (sscanf(buf, "%i", &pkey) != 1)
  720. return -EINVAL;
  721. if (pkey < 0 || pkey > 0xffff)
  722. return -EINVAL;
  723. /*
  724. * Set the full membership bit, so that we join the right
  725. * broadcast group, etc.
  726. */
  727. pkey |= 0x8000;
  728. ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
  729. pkey);
  730. return ret ? ret : count;
  731. }
  732. static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  733. static ssize_t delete_child(struct class_device *cdev,
  734. const char *buf, size_t count)
  735. {
  736. int pkey;
  737. int ret;
  738. if (sscanf(buf, "%i", &pkey) != 1)
  739. return -EINVAL;
  740. if (pkey < 0 || pkey > 0xffff)
  741. return -EINVAL;
  742. ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
  743. pkey);
  744. return ret ? ret : count;
  745. }
  746. static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  747. int ipoib_add_pkey_attr(struct net_device *dev)
  748. {
  749. return class_device_create_file(&dev->class_dev,
  750. &class_device_attr_pkey);
  751. }
  752. static struct net_device *ipoib_add_port(const char *format,
  753. struct ib_device *hca, u8 port)
  754. {
  755. struct ipoib_dev_priv *priv;
  756. int result = -ENOMEM;
  757. priv = ipoib_intf_alloc(format);
  758. if (!priv)
  759. goto alloc_mem_failed;
  760. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  761. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  762. if (result) {
  763. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  764. hca->name, port, result);
  765. goto alloc_mem_failed;
  766. }
  767. /*
  768. * Set the full membership bit, so that we join the right
  769. * broadcast group, etc.
  770. */
  771. priv->pkey |= 0x8000;
  772. priv->dev->broadcast[8] = priv->pkey >> 8;
  773. priv->dev->broadcast[9] = priv->pkey & 0xff;
  774. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  775. if (result) {
  776. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  777. hca->name, port, result);
  778. goto alloc_mem_failed;
  779. } else
  780. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  781. result = ipoib_dev_init(priv->dev, hca, port);
  782. if (result < 0) {
  783. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  784. hca->name, port, result);
  785. goto device_init_failed;
  786. }
  787. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  788. priv->ca, ipoib_event);
  789. result = ib_register_event_handler(&priv->event_handler);
  790. if (result < 0) {
  791. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  792. "port %d (ret = %d)\n",
  793. hca->name, port, result);
  794. goto event_failed;
  795. }
  796. result = register_netdev(priv->dev);
  797. if (result) {
  798. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  799. hca->name, port, result);
  800. goto register_failed;
  801. }
  802. ipoib_create_debug_files(priv->dev);
  803. if (ipoib_add_pkey_attr(priv->dev))
  804. goto sysfs_failed;
  805. if (class_device_create_file(&priv->dev->class_dev,
  806. &class_device_attr_create_child))
  807. goto sysfs_failed;
  808. if (class_device_create_file(&priv->dev->class_dev,
  809. &class_device_attr_delete_child))
  810. goto sysfs_failed;
  811. return priv->dev;
  812. sysfs_failed:
  813. ipoib_delete_debug_files(priv->dev);
  814. unregister_netdev(priv->dev);
  815. register_failed:
  816. ib_unregister_event_handler(&priv->event_handler);
  817. flush_scheduled_work();
  818. event_failed:
  819. ipoib_dev_cleanup(priv->dev);
  820. device_init_failed:
  821. free_netdev(priv->dev);
  822. alloc_mem_failed:
  823. return ERR_PTR(result);
  824. }
  825. static void ipoib_add_one(struct ib_device *device)
  826. {
  827. struct list_head *dev_list;
  828. struct net_device *dev;
  829. struct ipoib_dev_priv *priv;
  830. int s, e, p;
  831. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  832. if (!dev_list)
  833. return;
  834. INIT_LIST_HEAD(dev_list);
  835. if (device->node_type == IB_NODE_SWITCH) {
  836. s = 0;
  837. e = 0;
  838. } else {
  839. s = 1;
  840. e = device->phys_port_cnt;
  841. }
  842. for (p = s; p <= e; ++p) {
  843. dev = ipoib_add_port("ib%d", device, p);
  844. if (!IS_ERR(dev)) {
  845. priv = netdev_priv(dev);
  846. list_add_tail(&priv->list, dev_list);
  847. }
  848. }
  849. ib_set_client_data(device, &ipoib_client, dev_list);
  850. }
  851. static void ipoib_remove_one(struct ib_device *device)
  852. {
  853. struct ipoib_dev_priv *priv, *tmp;
  854. struct list_head *dev_list;
  855. dev_list = ib_get_client_data(device, &ipoib_client);
  856. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  857. ib_unregister_event_handler(&priv->event_handler);
  858. flush_scheduled_work();
  859. unregister_netdev(priv->dev);
  860. ipoib_dev_cleanup(priv->dev);
  861. free_netdev(priv->dev);
  862. }
  863. kfree(dev_list);
  864. }
  865. static int __init ipoib_init_module(void)
  866. {
  867. int ret;
  868. ret = ipoib_register_debugfs();
  869. if (ret)
  870. return ret;
  871. /*
  872. * We create our own workqueue mainly because we want to be
  873. * able to flush it when devices are being removed. We can't
  874. * use schedule_work()/flush_scheduled_work() because both
  875. * unregister_netdev() and linkwatch_event take the rtnl lock,
  876. * so flush_scheduled_work() can deadlock during device
  877. * removal.
  878. */
  879. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  880. if (!ipoib_workqueue) {
  881. ret = -ENOMEM;
  882. goto err_fs;
  883. }
  884. ret = ib_register_client(&ipoib_client);
  885. if (ret)
  886. goto err_wq;
  887. return 0;
  888. err_wq:
  889. destroy_workqueue(ipoib_workqueue);
  890. err_fs:
  891. ipoib_unregister_debugfs();
  892. return ret;
  893. }
  894. static void __exit ipoib_cleanup_module(void)
  895. {
  896. ib_unregister_client(&ipoib_client);
  897. ipoib_unregister_debugfs();
  898. destroy_workqueue(ipoib_workqueue);
  899. }
  900. module_init(ipoib_init_module);
  901. module_exit(ipoib_cleanup_module);