ipoib_main.c 28 KB

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